1 #ifndef __NET_PKT_CLS_H
2 #define __NET_PKT_CLS_H
4 #include <linux/pkt_cls.h>
5 #include <net/sch_generic.h>
6 #include <net/act_api.h>
8 /* Basic packet classifier frontend definitions. */
14 int (*fn)(struct tcf_proto *, unsigned long node, struct tcf_walker *);
17 int register_tcf_proto_ops(struct tcf_proto_ops *ops);
18 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
21 int tcf_block_get(struct tcf_block **p_block,
22 struct tcf_proto __rcu **p_filter_chain);
23 void tcf_block_put(struct tcf_block *block);
24 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
25 struct tcf_result *res, bool compat_mode);
29 int tcf_block_get(struct tcf_block **p_block,
30 struct tcf_proto __rcu **p_filter_chain)
35 static inline void tcf_block_put(struct tcf_block *block)
39 static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
40 struct tcf_result *res, bool compat_mode)
46 static inline unsigned long
47 __cls_set_class(unsigned long *clp, unsigned long cl)
52 static inline unsigned long
53 cls_set_class(struct tcf_proto *tp, unsigned long *clp,
59 old_cl = __cls_set_class(clp, cl);
66 tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
70 cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
71 cl = cls_set_class(tp, &r->class, cl);
73 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
77 tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
81 if ((cl = __cls_set_class(&r->class, 0)) != 0)
82 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
86 #ifdef CONFIG_NET_CLS_ACT
87 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
89 struct tc_action **actions;
91 /* Map to export classifier specific extension TLV types to the
92 * generic extensions API. Unsupported extensions must be set to 0.
98 static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
100 #ifdef CONFIG_NET_CLS_ACT
102 exts->nr_actions = 0;
103 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
108 exts->action = action;
109 exts->police = police;
114 * tcf_exts_is_predicative - check if a predicative extension is present
115 * @exts: tc filter extensions handle
117 * Returns 1 if a predicative extension is present, i.e. an extension which
118 * might cause further actions and thus overrule the regular tcf_result.
121 tcf_exts_is_predicative(struct tcf_exts *exts)
123 #ifdef CONFIG_NET_CLS_ACT
124 return exts->nr_actions;
131 * tcf_exts_is_available - check if at least one extension is present
132 * @exts: tc filter extensions handle
134 * Returns 1 if at least one extension is present.
137 tcf_exts_is_available(struct tcf_exts *exts)
139 /* All non-predicative extensions must be added here. */
140 return tcf_exts_is_predicative(exts);
143 static inline void tcf_exts_to_list(const struct tcf_exts *exts,
144 struct list_head *actions)
146 #ifdef CONFIG_NET_CLS_ACT
149 for (i = 0; i < exts->nr_actions; i++) {
150 struct tc_action *a = exts->actions[i];
152 list_add_tail(&a->list, actions);
158 * tcf_exts_exec - execute tc filter extensions
159 * @skb: socket buffer
160 * @exts: tc filter extensions handle
161 * @res: desired result
163 * Executes all configured extensions. Returns 0 on a normal execution,
164 * a negative number if the filter must be considered unmatched or
165 * a positive action code (TC_ACT_*) which must be returned to the
169 tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
170 struct tcf_result *res)
172 #ifdef CONFIG_NET_CLS_ACT
173 if (exts->nr_actions)
174 return tcf_action_exec(skb, exts->actions, exts->nr_actions,
180 #ifdef CONFIG_NET_CLS_ACT
182 #define tc_no_actions(_exts) ((_exts)->nr_actions == 0)
183 #define tc_single_action(_exts) ((_exts)->nr_actions == 1)
185 #else /* CONFIG_NET_CLS_ACT */
187 #define tc_no_actions(_exts) true
188 #define tc_single_action(_exts) false
190 #endif /* CONFIG_NET_CLS_ACT */
192 int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
193 struct nlattr **tb, struct nlattr *rate_tlv,
194 struct tcf_exts *exts, bool ovr);
195 void tcf_exts_destroy(struct tcf_exts *exts);
196 void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
197 struct tcf_exts *src);
198 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
199 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
200 int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
201 struct net_device **hw_dev);
204 * struct tcf_pkt_info - packet information
206 struct tcf_pkt_info {
211 #ifdef CONFIG_NET_EMATCH
213 struct tcf_ematch_ops;
216 * struct tcf_ematch - extended match (ematch)
218 * @matchid: identifier to allow userspace to reidentify a match
219 * @flags: flags specifying attributes and the relation to other matches
220 * @ops: the operations lookup table of the corresponding ematch module
221 * @datalen: length of the ematch specific configuration data
222 * @data: ematch specific data
225 struct tcf_ematch_ops * ops;
227 unsigned int datalen;
233 static inline int tcf_em_is_container(struct tcf_ematch *em)
238 static inline int tcf_em_is_simple(struct tcf_ematch *em)
240 return em->flags & TCF_EM_SIMPLE;
243 static inline int tcf_em_is_inverted(struct tcf_ematch *em)
245 return em->flags & TCF_EM_INVERT;
248 static inline int tcf_em_last_match(struct tcf_ematch *em)
250 return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
253 static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
255 if (tcf_em_last_match(em))
258 if (result == 0 && em->flags & TCF_EM_REL_AND)
261 if (result != 0 && em->flags & TCF_EM_REL_OR)
268 * struct tcf_ematch_tree - ematch tree handle
270 * @hdr: ematch tree header supplied by userspace
271 * @matches: array of ematches
273 struct tcf_ematch_tree {
274 struct tcf_ematch_tree_hdr hdr;
275 struct tcf_ematch * matches;
280 * struct tcf_ematch_ops - ematch module operations
282 * @kind: identifier (kind) of this ematch module
283 * @datalen: length of expected configuration data (optional)
284 * @change: called during validation (optional)
285 * @match: called during ematch tree evaluation, must return 1/0
286 * @destroy: called during destroyage (optional)
287 * @dump: called during dumping process (optional)
288 * @owner: owner, must be set to THIS_MODULE
289 * @link: link to previous/next ematch module (internal use)
291 struct tcf_ematch_ops {
294 int (*change)(struct net *net, void *,
295 int, struct tcf_ematch *);
296 int (*match)(struct sk_buff *, struct tcf_ematch *,
297 struct tcf_pkt_info *);
298 void (*destroy)(struct tcf_ematch *);
299 int (*dump)(struct sk_buff *, struct tcf_ematch *);
300 struct module *owner;
301 struct list_head link;
304 int tcf_em_register(struct tcf_ematch_ops *);
305 void tcf_em_unregister(struct tcf_ematch_ops *);
306 int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
307 struct tcf_ematch_tree *);
308 void tcf_em_tree_destroy(struct tcf_ematch_tree *);
309 int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
310 int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
311 struct tcf_pkt_info *);
314 * tcf_em_tree_change - replace ematch tree of a running classifier
316 * @tp: classifier kind handle
317 * @dst: destination ematch tree variable
318 * @src: source ematch tree (temporary tree from tcf_em_tree_validate)
320 * This functions replaces the ematch tree in @dst with the ematch
321 * tree in @src. The classifier in charge of the ematch tree may be
324 static inline void tcf_em_tree_change(struct tcf_proto *tp,
325 struct tcf_ematch_tree *dst,
326 struct tcf_ematch_tree *src)
329 memcpy(dst, src, sizeof(*dst));
334 * tcf_em_tree_match - evaulate an ematch tree
336 * @skb: socket buffer of the packet in question
337 * @tree: ematch tree to be used for evaluation
338 * @info: packet information examined by classifier
340 * This function matches @skb against the ematch tree in @tree by going
341 * through all ematches respecting their logic relations returning
342 * as soon as the result is obvious.
344 * Returns 1 if the ematch tree as-one matches, no ematches are configured
345 * or ematch is not enabled in the kernel, otherwise 0 is returned.
347 static inline int tcf_em_tree_match(struct sk_buff *skb,
348 struct tcf_ematch_tree *tree,
349 struct tcf_pkt_info *info)
351 if (tree->hdr.nmatches)
352 return __tcf_em_tree_match(skb, tree, info);
357 #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
359 #else /* CONFIG_NET_EMATCH */
361 struct tcf_ematch_tree {
364 #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
365 #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
366 #define tcf_em_tree_dump(skb, t, tlv) (0)
367 #define tcf_em_tree_change(tp, dst, src) do { } while(0)
368 #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
370 #endif /* CONFIG_NET_EMATCH */
372 static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
377 case TCF_LAYER_NETWORK:
378 return skb_network_header(skb);
379 case TCF_LAYER_TRANSPORT:
380 return skb_transport_header(skb);
386 static inline int tcf_valid_offset(const struct sk_buff *skb,
387 const unsigned char *ptr, const int len)
389 return likely((ptr + len) <= skb_tail_pointer(skb) &&
391 (ptr <= (ptr + len)));
394 #ifdef CONFIG_NET_CLS_IND
395 #include <net/net_namespace.h>
398 tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
400 char indev[IFNAMSIZ];
401 struct net_device *dev;
403 if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
405 dev = __dev_get_by_name(net, indev);
412 tcf_match_indev(struct sk_buff *skb, int ifindex)
418 return ifindex == skb->skb_iif;
420 #endif /* CONFIG_NET_CLS_IND */
422 struct tc_cls_u32_knode {
423 struct tcf_exts *exts;
424 struct tc_u32_sel *sel;
432 struct tc_cls_u32_hnode {
435 unsigned int divisor;
438 enum tc_clsu32_command {
440 TC_CLSU32_REPLACE_KNODE,
441 TC_CLSU32_DELETE_KNODE,
443 TC_CLSU32_REPLACE_HNODE,
444 TC_CLSU32_DELETE_HNODE,
447 struct tc_cls_u32_offload {
449 enum tc_clsu32_command command;
451 struct tc_cls_u32_knode knode;
452 struct tc_cls_u32_hnode hnode;
456 static inline bool tc_can_offload(const struct net_device *dev,
457 const struct tcf_proto *tp)
459 const struct Qdisc *sch = tp->q;
460 const struct Qdisc_class_ops *cops = sch->ops->cl_ops;
462 if (!(dev->features & NETIF_F_HW_TC))
464 if (!dev->netdev_ops->ndo_setup_tc)
466 if (cops && cops->tcf_cl_offload)
467 return cops->tcf_cl_offload(tp->classid);
472 static inline bool tc_skip_hw(u32 flags)
474 return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
477 static inline bool tc_should_offload(const struct net_device *dev,
478 const struct tcf_proto *tp, u32 flags)
480 if (tc_skip_hw(flags))
482 return tc_can_offload(dev, tp);
485 static inline bool tc_skip_sw(u32 flags)
487 return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
490 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
491 static inline bool tc_flags_valid(u32 flags)
493 if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
496 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
502 static inline bool tc_in_hw(u32 flags)
504 return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
508 TC_CLSFLOWER_REPLACE,
509 TC_CLSFLOWER_DESTROY,
513 struct tc_cls_flower_offload {
514 enum tc_fl_command command;
516 unsigned long cookie;
517 struct flow_dissector *dissector;
518 struct fl_flow_key *mask;
519 struct fl_flow_key *key;
520 struct tcf_exts *exts;
523 enum tc_matchall_command {
524 TC_CLSMATCHALL_REPLACE,
525 TC_CLSMATCHALL_DESTROY,
528 struct tc_cls_matchall_offload {
529 enum tc_matchall_command command;
530 struct tcf_exts *exts;
531 unsigned long cookie;
534 enum tc_clsbpf_command {
541 struct tc_cls_bpf_offload {
542 enum tc_clsbpf_command command;
543 struct tcf_exts *exts;
544 struct bpf_prog *prog;
546 bool exts_integrated;
551 /* This structure holds cookie structure that is passed from user
552 * to the kernel for actions and classifiers