00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <netlink-local.h>
00019 #include <netlink-tc.h>
00020 #include <netlink/netlink.h>
00021 #include <netlink/utils.h>
00022 #include <netlink/route/tc-api.h>
00023 #include <netlink/route/classifier.h>
00024 #include <netlink/route/link.h>
00025
00026
00027 #define CLS_ATTR_PRIO (TCA_ATTR_MAX << 1)
00028 #define CLS_ATTR_PROTOCOL (TCA_ATTR_MAX << 2)
00029
00030
00031 static struct nl_object_ops cls_obj_ops;
00032 static struct nl_cache_ops rtnl_cls_ops;
00033
00034
00035 static int cls_build(struct rtnl_cls *cls, int type, int flags,
00036 struct nl_msg **result)
00037 {
00038 int err, prio, proto;
00039 struct tcmsg *tchdr;
00040 int required = TCA_ATTR_IFINDEX;
00041
00042 if ((cls->ce_mask & required) != required) {
00043 APPBUG("ifindex must be specified");
00044 return -NLE_MISSING_ATTR;
00045 }
00046
00047 err = rtnl_tc_msg_build(TC_CAST(cls), type, flags, result);
00048 if (err < 0)
00049 return err;
00050
00051 tchdr = nlmsg_data(nlmsg_hdr(*result));
00052 prio = rtnl_cls_get_prio(cls);
00053 proto = rtnl_cls_get_protocol(cls);
00054 tchdr->tcm_info = TC_H_MAKE(prio << 16, htons(proto));
00055
00056 return 0;
00057 }
00058
00059
00060
00061
00062
00063
00064 struct rtnl_cls *rtnl_cls_alloc(void)
00065 {
00066 struct rtnl_tc *tc;
00067
00068 tc = TC_CAST(nl_object_alloc(&cls_obj_ops));
00069 if (tc)
00070 tc->tc_type = RTNL_TC_TYPE_CLS;
00071
00072 return (struct rtnl_cls *) tc;
00073 }
00074
00075 void rtnl_cls_put(struct rtnl_cls *cls)
00076 {
00077 nl_object_put((struct nl_object *) cls);
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087 void rtnl_cls_set_prio(struct rtnl_cls *cls, uint16_t prio)
00088 {
00089 cls->c_prio = prio;
00090 cls->ce_mask |= CLS_ATTR_PRIO;
00091 }
00092
00093 uint16_t rtnl_cls_get_prio(struct rtnl_cls *cls)
00094 {
00095 if (cls->ce_mask & CLS_ATTR_PRIO)
00096 return cls->c_prio;
00097 else
00098 return 0;
00099 }
00100
00101 void rtnl_cls_set_protocol(struct rtnl_cls *cls, uint16_t protocol)
00102 {
00103 cls->c_protocol = protocol;
00104 cls->ce_mask |= CLS_ATTR_PROTOCOL;
00105 }
00106
00107 uint16_t rtnl_cls_get_protocol(struct rtnl_cls *cls)
00108 {
00109 if (cls->ce_mask & CLS_ATTR_PROTOCOL)
00110 return cls->c_protocol;
00111 else
00112 return ETH_P_ALL;
00113 }
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 int rtnl_cls_build_add_request(struct rtnl_cls *cls, int flags,
00138 struct nl_msg **result)
00139 {
00140 if (!(flags & NLM_F_CREATE) && !(cls->ce_mask & CLS_ATTR_PRIO)) {
00141 APPBUG("prio must be specified if not a new classifier");
00142 return -NLE_MISSING_ATTR;
00143 }
00144
00145 return cls_build(cls, RTM_NEWTFILTER, flags, result);
00146 }
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184 int rtnl_cls_add(struct nl_sock *sk, struct rtnl_cls *cls, int flags)
00185 {
00186 struct nl_msg *msg;
00187 int err;
00188
00189 if ((err = rtnl_cls_build_add_request(cls, flags, &msg)) < 0)
00190 return err;
00191
00192 return nl_send_sync(sk, msg);
00193 }
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 int rtnl_cls_build_change_request(struct rtnl_cls *cls, int flags,
00209 struct nl_msg **result)
00210 {
00211 return cls_build(cls, RTM_NEWTFILTER, NLM_F_REPLACE | flags, result);
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 int rtnl_cls_change(struct nl_sock *sk, struct rtnl_cls *cls, int flags)
00227 {
00228 struct nl_msg *msg;
00229 int err;
00230
00231 if ((err = rtnl_cls_build_change_request(cls, flags, &msg)) < 0)
00232 return err;
00233
00234 return nl_send_sync(sk, msg);
00235 }
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 int rtnl_cls_build_delete_request(struct rtnl_cls *cls, int flags,
00252 struct nl_msg **result)
00253 {
00254 int required = CLS_ATTR_PRIO;
00255
00256 if ((cls->ce_mask & required) != required) {
00257 APPBUG("prio must be specified");
00258 return -NLE_MISSING_ATTR;
00259 }
00260
00261 return cls_build(cls, RTM_DELTFILTER, flags, result);
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 int rtnl_cls_delete(struct nl_sock *sk, struct rtnl_cls *cls, int flags)
00296 {
00297 struct nl_msg *msg;
00298 int err;
00299
00300 if ((err = rtnl_cls_build_delete_request(cls, flags, &msg)) < 0)
00301 return err;
00302
00303 return nl_send_sync(sk, msg);
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 int rtnl_cls_alloc_cache(struct nl_sock *sk, int ifindex, uint32_t parent, struct nl_cache **result)
00328 {
00329 struct nl_cache * cache;
00330 int err;
00331
00332 if (!(cache = nl_cache_alloc(&rtnl_cls_ops)))
00333 return -NLE_NOMEM;
00334
00335 cache->c_iarg1 = ifindex;
00336 cache->c_iarg2 = parent;
00337
00338 if (sk && (err = nl_cache_refill(sk, cache)) < 0) {
00339 nl_cache_free(cache);
00340 return err;
00341 }
00342
00343 *result = cache;
00344 return 0;
00345 }
00346
00347
00348
00349 static void cls_dump_line(struct rtnl_tc *tc, struct nl_dump_params *p)
00350 {
00351 struct rtnl_cls *cls = (struct rtnl_cls *) tc;
00352 char buf[32];
00353
00354 nl_dump(p, " prio %u protocol %s", cls->c_prio,
00355 nl_ether_proto2str(cls->c_protocol, buf, sizeof(buf)));
00356 }
00357
00358 static int cls_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
00359 struct nlmsghdr *nlh, struct nl_parser_param *pp)
00360 {
00361 struct rtnl_cls *cls;
00362 int err;
00363
00364 if (!(cls = rtnl_cls_alloc()))
00365 return -NLE_NOMEM;
00366
00367 if ((err = rtnl_tc_msg_parse(nlh, TC_CAST(cls))) < 0)
00368 goto errout;
00369
00370 cls->c_prio = TC_H_MAJ(cls->c_info) >> 16;
00371 cls->c_protocol = ntohs(TC_H_MIN(cls->c_info));
00372
00373 err = pp->pp_cb(OBJ_CAST(cls), pp);
00374 errout:
00375 rtnl_cls_put(cls);
00376
00377 return err;
00378 }
00379
00380 static int cls_request_update(struct nl_cache *cache, struct nl_sock *sk)
00381 {
00382 struct tcmsg tchdr = {
00383 .tcm_family = AF_UNSPEC,
00384 .tcm_ifindex = cache->c_iarg1,
00385 .tcm_parent = cache->c_iarg2,
00386 };
00387
00388 return nl_send_simple(sk, RTM_GETTFILTER, NLM_F_DUMP, &tchdr,
00389 sizeof(tchdr));
00390 }
00391
00392 static struct rtnl_tc_type_ops cls_ops = {
00393 .tt_type = RTNL_TC_TYPE_CLS,
00394 .tt_dump_prefix = "cls",
00395 .tt_dump = {
00396 [NL_DUMP_LINE] = cls_dump_line,
00397 },
00398 };
00399
00400 static struct nl_cache_ops rtnl_cls_ops = {
00401 .co_name = "route/cls",
00402 .co_hdrsize = sizeof(struct tcmsg),
00403 .co_msgtypes = {
00404 { RTM_NEWTFILTER, NL_ACT_NEW, "new" },
00405 { RTM_DELTFILTER, NL_ACT_DEL, "del" },
00406 { RTM_GETTFILTER, NL_ACT_GET, "get" },
00407 END_OF_MSGTYPES_LIST,
00408 },
00409 .co_protocol = NETLINK_ROUTE,
00410 .co_request_update = cls_request_update,
00411 .co_msg_parser = cls_msg_parser,
00412 .co_obj_ops = &cls_obj_ops,
00413 };
00414
00415 static struct nl_object_ops cls_obj_ops = {
00416 .oo_name = "route/cls",
00417 .oo_size = sizeof(struct rtnl_cls),
00418 .oo_free_data = rtnl_tc_free_data,
00419 .oo_clone = rtnl_tc_clone,
00420 .oo_dump = {
00421 [NL_DUMP_LINE] = rtnl_tc_dump_line,
00422 [NL_DUMP_DETAILS] = rtnl_tc_dump_details,
00423 [NL_DUMP_STATS] = rtnl_tc_dump_stats,
00424 },
00425 .oo_compare = rtnl_tc_compare,
00426 .oo_id_attrs = (TCA_ATTR_IFINDEX | TCA_ATTR_HANDLE),
00427 };
00428
00429 static void __init cls_init(void)
00430 {
00431 rtnl_tc_type_register(&cls_ops);
00432 nl_cache_mngt_register(&rtnl_cls_ops);
00433 }
00434
00435 static void __exit cls_exit(void)
00436 {
00437 nl_cache_mngt_unregister(&rtnl_cls_ops);
00438 rtnl_tc_type_unregister(&cls_ops);
00439 }
00440
00441