2 * lib/route/classifier.c Classifier
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
14 * @defgroup cls Classifiers
16 * @par Classifier Identification
27 #include <netlink-local.h>
28 #include <netlink-tc.h>
29 #include <netlink/netlink.h>
30 #include <netlink/utils.h>
31 #include <netlink/route/tc.h>
32 #include <netlink/route/classifier.h>
33 #include <netlink/route/classifier-modules.h>
34 #include <netlink/route/link.h>
37 #define CLS_ATTR_PRIO (TCA_ATTR_MAX << 1)
38 #define CLS_ATTR_PROTOCOL (TCA_ATTR_MAX << 2)
40 static struct nl_cache_ops rtnl_cls_ops;
43 static struct rtnl_cls_ops *cls_ops_list;
45 static struct rtnl_cls_ops * cls_lookup_ops(char *kind)
47 struct rtnl_cls_ops *ops;
49 for (ops = cls_ops_list; ops; ops = ops->co_next)
50 if (!strcmp(kind, ops->co_kind))
56 static inline struct rtnl_cls_ops *cls_ops(struct rtnl_cls *cls)
59 cls->c_ops = cls_lookup_ops(cls->c_kind);
65 * @name Classifier Module API
70 * Register a classifier module
71 * @arg ops classifier module operations
73 int rtnl_cls_register(struct rtnl_cls_ops *ops)
75 struct rtnl_cls_ops *o, **op;
80 for (op = &cls_ops_list; (o = *op) != NULL; op = &o->co_next)
81 if (!strcasecmp(ops->co_kind, o->co_kind))
82 return nl_errno(EEXIST);
91 * Unregister a classifier module
92 * @arg ops classifier module operations
94 int rtnl_cls_unregister(struct rtnl_cls_ops *ops)
96 struct rtnl_cls_ops *o, **op;
98 for (op = &cls_ops_list; (o = *op) != NULL; op = &o->co_next)
99 if (!strcasecmp(ops->co_kind, o->co_kind))
103 return nl_errno(ENOENT);
112 static int cls_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *nlh,
116 struct nl_parser_param *pp = arg;
117 struct rtnl_cls *cls;
118 struct rtnl_cls_ops *ops;
120 cls = rtnl_cls_alloc();
122 err = nl_errno(ENOMEM);
125 cls->ce_msgtype = nlh->nlmsg_type;
127 err = tca_msg_parser(nlh, (struct rtnl_tca *) cls);
131 cls->c_prio = TC_H_MAJ(cls->c_info) >> 16;
132 cls->c_protocol = ntohs(TC_H_MIN(cls->c_info));
135 if (ops && ops->co_msg_parser) {
136 err = ops->co_msg_parser(cls);
141 err = pp->pp_cb((struct nl_object *) cls, pp);
153 static int cls_request_update(struct nl_cache *cache, struct nl_handle *handle)
155 struct tcmsg tchdr = {
156 .tcm_family = AF_UNSPEC,
157 .tcm_ifindex = cache->c_iarg1,
158 .tcm_parent = cache->c_iarg2,
161 return nl_send_simple(handle, RTM_GETTFILTER, NLM_F_DUMP, &tchdr,
166 static void cls_free_data(struct nl_object *obj)
168 struct rtnl_cls *cls = (struct rtnl_cls *) obj;
169 struct rtnl_cls_ops *ops;
171 tca_free_data((struct rtnl_tca *) cls);
174 if (ops && ops->co_free_data)
175 ops->co_free_data(cls);
178 static int cls_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
181 struct rtnl_cls *cls = (struct rtnl_cls *) obj;
182 struct rtnl_cls_ops *ops;
185 line = tca_dump_brief((struct rtnl_tca *) cls, "cls", p, 0);
187 dp_dump(p, " prio %u protocol %s", cls->c_prio,
188 nl_ether_proto2str(cls->c_protocol, buf, sizeof(buf)));
191 if (ops && ops->co_dump[NL_DUMP_BRIEF])
192 line = ops->co_dump[NL_DUMP_BRIEF](cls, p, line);
198 static int cls_dump_full(struct nl_object *obj, struct nl_dump_params *p)
200 struct rtnl_cls *cls = (struct rtnl_cls *) obj;
201 struct rtnl_cls_ops *ops;
204 line = cls_dump_brief(obj, p);
205 line = tca_dump_full((struct rtnl_tca *) cls, p, line);
208 if (ops && ops->co_dump[NL_DUMP_FULL])
209 line = ops->co_dump[NL_DUMP_FULL](cls, p, line);
211 dp_dump(p, "no options\n");
216 static int cls_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
218 struct rtnl_cls *cls = (struct rtnl_cls *) obj;
219 struct rtnl_cls_ops *ops;
222 line = cls_dump_full(obj, p);
223 line = tca_dump_stats((struct rtnl_tca *) cls, p, line);
227 if (ops && ops->co_dump[NL_DUMP_STATS])
228 line = ops->co_dump[NL_DUMP_STATS](cls, p, line);
233 static int cls_filter(struct nl_object *obj, struct nl_object *filter)
235 return tca_filter((struct rtnl_tca *) obj, (struct rtnl_tca *) filter);
238 static struct nl_msg *cls_build(struct rtnl_cls *cls, int type, int flags)
241 struct rtnl_cls_ops *ops;
242 int err, prio, proto;
245 msg = tca_build_msg((struct rtnl_tca *) cls, type, flags);
249 tchdr = nlmsg_data(nlmsg_hdr(msg));
250 prio = cls->c_mask & CLS_ATTR_PRIO ? cls->c_prio : 0;
251 proto = cls->c_mask & CLS_ATTR_PROTOCOL ? cls->c_protocol : ETH_P_ALL;
252 tchdr->tcm_info = TC_H_MAKE(prio << 16, htons(proto)),
255 if (ops && ops->co_get_opts) {
258 opts = ops->co_get_opts(cls);
260 err = nla_put_nested(msg, TCA_OPTIONS, opts);
274 * @name Classifier Addition/Modification/Deletion
279 * Build a netlink message to add a new classifier
280 * @arg cls classifier to add
281 * @arg flags additional netlink message flags
283 * Builds a new netlink message requesting an addition of a classifier
284 * The netlink message header isn't fully equipped with all relevant
285 * fields and must be sent out via nl_send_auto_complete() or
286 * supplemented as needed. \a classifier must contain the attributes of
287 * the new classifier set via \c rtnl_cls_set_* functions. \a opts
288 * may point to the clsasifier specific options.
290 * @return New netlink message
292 struct nl_msg * rtnl_cls_build_add_request(struct rtnl_cls *cls, int flags)
294 return cls_build(cls, RTM_NEWTFILTER, NLM_F_CREATE | flags);
298 * Add a new classifier
299 * @arg handle netlink handle
300 * @arg cls classifier to add
301 * @arg flags additional netlink message flags
303 * Builds a netlink message by calling rtnl_cls_build_add_request(),
304 * sends the request to the kernel and waits for the next ACK to be
305 * received and thus blocks until the request has been processed.
307 * @return 0 on sucess or a negative error if an error occured.
309 int rtnl_cls_add(struct nl_handle *handle, struct rtnl_cls *cls, int flags)
314 msg = rtnl_cls_build_add_request(cls, flags);
316 return nl_errno(ENOMEM);
318 err = nl_send_auto_complete(handle, msg);
323 return nl_wait_for_ack(handle);
327 * Build a netlink message to change classifier attributes
328 * @arg cls classifier to change
329 * @arg flags additional netlink message flags
331 * Builds a new netlink message requesting a change of a neigh
332 * attributes. The netlink message header isn't fully equipped with
333 * all relevant fields and must thus be sent out via nl_send_auto_complete()
334 * or supplemented as needed.
336 * @return The netlink message
338 struct nl_msg *rtnl_cls_build_change_request(struct rtnl_cls *cls, int flags)
340 return cls_build(cls, RTM_NEWTFILTER, NLM_F_REPLACE | flags);
344 * Change a classifier
345 * @arg handle netlink handle
346 * @arg cls classifier to change
347 * @arg flags additional netlink message flags
349 * Builds a netlink message by calling rtnl_cls_build_change_request(),
350 * sends the request to the kernel and waits for the next ACK to be
351 * received and thus blocks until the request has been processed.
353 * @return 0 on sucess or a negative error if an error occured.
355 int rtnl_cls_change(struct nl_handle *handle, struct rtnl_cls *cls,
361 msg = rtnl_cls_build_change_request(cls, flags);
363 return nl_errno(ENOMEM);
365 err = nl_send_auto_complete(handle, msg);
370 return nl_wait_for_ack(handle);
374 * Build a netlink request message to delete a classifier
375 * @arg cls classifier to delete
376 * @arg flags additional netlink message flags
378 * Builds a new netlink message requesting a deletion of a classifier.
379 * The netlink message header isn't fully equipped with all relevant
380 * fields and must thus be sent out via nl_send_auto_complete()
381 * or supplemented as needed.
383 * @return New netlink message
385 struct nl_msg *rtnl_cls_build_delete_request(struct rtnl_cls *cls, int flags)
387 return cls_build(cls, RTM_DELTFILTER, flags);
392 * Delete a classifier
393 * @arg handle netlink handle
394 * @arg cls classifier to delete
395 * @arg flags additional netlink message flags
397 * Builds a netlink message by calling rtnl_cls_build_delete_request(),
398 * sends the request to the kernel and waits for the next ACK to be
399 * received and thus blocks until the request has been processed.
401 * @return 0 on sucess or a negative error if an error occured.
403 int rtnl_cls_delete(struct nl_handle *handle, struct rtnl_cls *cls, int flags)
408 msg = rtnl_cls_build_delete_request(cls, flags);
410 return nl_errno(ENOMEM);
412 err = nl_send_auto_complete(handle, msg);
417 return nl_wait_for_ack(handle);
428 * Allocate a new classifier object
429 * @return New classifier object
431 struct rtnl_cls *rtnl_cls_alloc(void)
433 return (struct rtnl_cls *) nl_object_alloc_from_ops(&rtnl_cls_ops);
437 * Give back reference on classifier object.
438 * @arg cls Classifier object to be given back.
440 * Decrements the reference counter and frees the object if the
441 * last reference has been released.
443 void rtnl_cls_put(struct rtnl_cls *cls)
445 nl_object_put((struct nl_object *) cls);
449 * Free classifier object.
450 * @arg cls Classifier object to be freed.
452 * @note Always use rtnl_cls_put() unless you're absolutely sure
453 * that no other user may have a reference on this object.
455 void rtnl_cls_free(struct rtnl_cls *cls)
457 nl_object_free((struct nl_object *) cls);
461 * Build a classifier cache including all classifiers attached to the
462 * specified class/qdisc on eht specified interface.
463 * @arg handle netlink handle
464 * @arg ifindex interface index of the link the classes are
466 * @arg parent parent qdisc/class
468 * Allocates a new cache, initializes it properly and updates it to
469 * include all classes attached to the specified interface.
471 * @note The caller is responsible for destroying and freeing the
472 * cache after using it.
473 * @return The cache or NULL if an error has occured.
475 struct nl_cache *rtnl_cls_alloc_cache(struct nl_handle *handle,
476 int ifindex, uint32_t parent)
478 struct nl_cache * cache;
480 cache = nl_cache_alloc_from_ops(&rtnl_cls_ops);
484 cache->c_iarg1 = ifindex;
485 cache->c_iarg2 = parent;
487 if (nl_cache_update(handle, cache) < 0) {
488 nl_cache_free(cache);
495 void rtnl_cls_set_ifindex(struct rtnl_cls *f, int ifindex)
497 tca_set_ifindex((struct rtnl_tca *) f, ifindex);
500 void rtnl_cls_set_handle(struct rtnl_cls *f, uint32_t handle)
502 tca_set_handle((struct rtnl_tca *) f, handle);
505 void rtnl_cls_set_parent(struct rtnl_cls *f, uint32_t parent)
507 tca_set_parent((struct rtnl_tca *) f, parent);
510 void rtnl_cls_set_kind(struct rtnl_cls *f, const char *kind)
512 tca_set_kind((struct rtnl_tca *) f, kind);
516 * Set prioroty of a classifier
517 * @arg cls classifier to change
518 * @arg prio new priority
520 void rtnl_cls_set_prio(struct rtnl_cls *cls, int prio)
523 cls->c_mask |= CLS_ATTR_PRIO;
527 * Get priority of a classifier
528 * @arg cls classifier
530 int rtnl_cls_get_prio(struct rtnl_cls *cls)
532 if (cls->c_mask & CLS_ATTR_PRIO)
539 * Set protocol of a classifier
540 * @arg cls classifier to change
541 * @arg protocol protocol identifier (ETH_P_xxx) in host byte-order
543 void rtnl_cls_set_protocol(struct rtnl_cls *cls, int protocol)
545 cls->c_protocol = protocol;
546 cls->c_mask |= CLS_ATTR_PROTOCOL;
550 * Get protocol of a classifier
551 * @arg cls classifier
553 int rtnl_cls_get_protocol(struct rtnl_cls *cls)
555 if (cls->c_mask & CLS_ATTR_PROTOCOL)
556 return cls->c_protocol;
563 static struct nl_cache_ops rtnl_cls_ops = {
564 .co_name = "route/cls",
565 .co_size = sizeof(struct rtnl_cls),
566 .co_hdrsize = sizeof(struct tcmsg),
568 { RTM_NEWTFILTER, "new" },
569 { RTM_DELTFILTER, "delete" },
570 { RTM_GETTFILTER, "get" },
573 .co_protocol = NETLINK_ROUTE,
574 .co_request_update = cls_request_update,
575 .co_filter = cls_filter,
576 .co_free_data = cls_free_data,
577 .co_msg_parser = cls_msg_parser,
578 .co_dump[NL_DUMP_BRIEF] = cls_dump_brief,
579 .co_dump[NL_DUMP_FULL] = cls_dump_full,
580 .co_dump[NL_DUMP_STATS] = cls_dump_stats,
583 static void __init cls_init(void)
585 nl_cache_mngt_register(&rtnl_cls_ops);
588 static void __exit cls_exit(void)
590 nl_cache_mngt_unregister(&rtnl_cls_ops);