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 struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index);
22 void tcf_chain_put(struct tcf_chain *chain);
23 int tcf_block_get(struct tcf_block **p_block,
24 struct tcf_proto __rcu **p_filter_chain);
25 void tcf_block_put(struct tcf_block *block);
26 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
27 struct tcf_result *res, bool compat_mode);
31 int tcf_block_get(struct tcf_block **p_block,
32 struct tcf_proto __rcu **p_filter_chain)
37 static inline void tcf_block_put(struct tcf_block *block)
41 static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
42 struct tcf_result *res, bool compat_mode)
48 static inline unsigned long
49 __cls_set_class(unsigned long *clp, unsigned long cl)
54 static inline unsigned long
55 cls_set_class(struct tcf_proto *tp, unsigned long *clp,
61 old_cl = __cls_set_class(clp, cl);
68 tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
72 cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
73 cl = cls_set_class(tp, &r->class, cl);
75 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
79 tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
83 if ((cl = __cls_set_class(&r->class, 0)) != 0)
84 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
88 #ifdef CONFIG_NET_CLS_ACT
89 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
91 struct tc_action **actions;
93 /* Map to export classifier specific extension TLV types to the
94 * generic extensions API. Unsupported extensions must be set to 0.
100 static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
102 #ifdef CONFIG_NET_CLS_ACT
104 exts->nr_actions = 0;
105 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
110 exts->action = action;
111 exts->police = police;
116 * tcf_exts_is_predicative - check if a predicative extension is present
117 * @exts: tc filter extensions handle
119 * Returns 1 if a predicative extension is present, i.e. an extension which
120 * might cause further actions and thus overrule the regular tcf_result.
123 tcf_exts_is_predicative(struct tcf_exts *exts)
125 #ifdef CONFIG_NET_CLS_ACT
126 return exts->nr_actions;
133 * tcf_exts_is_available - check if at least one extension is present
134 * @exts: tc filter extensions handle
136 * Returns 1 if at least one extension is present.
139 tcf_exts_is_available(struct tcf_exts *exts)
141 /* All non-predicative extensions must be added here. */
142 return tcf_exts_is_predicative(exts);
145 static inline void tcf_exts_to_list(const struct tcf_exts *exts,
146 struct list_head *actions)
148 #ifdef CONFIG_NET_CLS_ACT
151 for (i = 0; i < exts->nr_actions; i++) {
152 struct tc_action *a = exts->actions[i];
154 list_add_tail(&a->list, actions);
160 * tcf_exts_exec - execute tc filter extensions
161 * @skb: socket buffer
162 * @exts: tc filter extensions handle
163 * @res: desired result
165 * Executes all configured extensions. Returns 0 on a normal execution,
166 * a negative number if the filter must be considered unmatched or
167 * a positive action code (TC_ACT_*) which must be returned to the
171 tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
172 struct tcf_result *res)
174 #ifdef CONFIG_NET_CLS_ACT
175 if (exts->nr_actions)
176 return tcf_action_exec(skb, exts->actions, exts->nr_actions,
182 #ifdef CONFIG_NET_CLS_ACT
184 #define tc_no_actions(_exts) ((_exts)->nr_actions == 0)
185 #define tc_single_action(_exts) ((_exts)->nr_actions == 1)
187 #else /* CONFIG_NET_CLS_ACT */
189 #define tc_no_actions(_exts) true
190 #define tc_single_action(_exts) false
192 #endif /* CONFIG_NET_CLS_ACT */
194 int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
195 struct nlattr **tb, struct nlattr *rate_tlv,
196 struct tcf_exts *exts, bool ovr);
197 void tcf_exts_destroy(struct tcf_exts *exts);
198 void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
199 struct tcf_exts *src);
200 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
201 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
202 int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
203 struct net_device **hw_dev);
206 * struct tcf_pkt_info - packet information
208 struct tcf_pkt_info {
213 #ifdef CONFIG_NET_EMATCH
215 struct tcf_ematch_ops;
218 * struct tcf_ematch - extended match (ematch)
220 * @matchid: identifier to allow userspace to reidentify a match
221 * @flags: flags specifying attributes and the relation to other matches
222 * @ops: the operations lookup table of the corresponding ematch module
223 * @datalen: length of the ematch specific configuration data
224 * @data: ematch specific data
227 struct tcf_ematch_ops * ops;
229 unsigned int datalen;
235 static inline int tcf_em_is_container(struct tcf_ematch *em)
240 static inline int tcf_em_is_simple(struct tcf_ematch *em)
242 return em->flags & TCF_EM_SIMPLE;
245 static inline int tcf_em_is_inverted(struct tcf_ematch *em)
247 return em->flags & TCF_EM_INVERT;
250 static inline int tcf_em_last_match(struct tcf_ematch *em)
252 return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
255 static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
257 if (tcf_em_last_match(em))
260 if (result == 0 && em->flags & TCF_EM_REL_AND)
263 if (result != 0 && em->flags & TCF_EM_REL_OR)
270 * struct tcf_ematch_tree - ematch tree handle
272 * @hdr: ematch tree header supplied by userspace
273 * @matches: array of ematches
275 struct tcf_ematch_tree {
276 struct tcf_ematch_tree_hdr hdr;
277 struct tcf_ematch * matches;
282 * struct tcf_ematch_ops - ematch module operations
284 * @kind: identifier (kind) of this ematch module
285 * @datalen: length of expected configuration data (optional)
286 * @change: called during validation (optional)
287 * @match: called during ematch tree evaluation, must return 1/0
288 * @destroy: called during destroyage (optional)
289 * @dump: called during dumping process (optional)
290 * @owner: owner, must be set to THIS_MODULE
291 * @link: link to previous/next ematch module (internal use)
293 struct tcf_ematch_ops {
296 int (*change)(struct net *net, void *,
297 int, struct tcf_ematch *);
298 int (*match)(struct sk_buff *, struct tcf_ematch *,
299 struct tcf_pkt_info *);
300 void (*destroy)(struct tcf_ematch *);
301 int (*dump)(struct sk_buff *, struct tcf_ematch *);
302 struct module *owner;
303 struct list_head link;
306 int tcf_em_register(struct tcf_ematch_ops *);
307 void tcf_em_unregister(struct tcf_ematch_ops *);
308 int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
309 struct tcf_ematch_tree *);
310 void tcf_em_tree_destroy(struct tcf_ematch_tree *);
311 int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
312 int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
313 struct tcf_pkt_info *);
316 * tcf_em_tree_change - replace ematch tree of a running classifier
318 * @tp: classifier kind handle
319 * @dst: destination ematch tree variable
320 * @src: source ematch tree (temporary tree from tcf_em_tree_validate)
322 * This functions replaces the ematch tree in @dst with the ematch
323 * tree in @src. The classifier in charge of the ematch tree may be
326 static inline void tcf_em_tree_change(struct tcf_proto *tp,
327 struct tcf_ematch_tree *dst,
328 struct tcf_ematch_tree *src)
331 memcpy(dst, src, sizeof(*dst));
336 * tcf_em_tree_match - evaulate an ematch tree
338 * @skb: socket buffer of the packet in question
339 * @tree: ematch tree to be used for evaluation
340 * @info: packet information examined by classifier
342 * This function matches @skb against the ematch tree in @tree by going
343 * through all ematches respecting their logic relations returning
344 * as soon as the result is obvious.
346 * Returns 1 if the ematch tree as-one matches, no ematches are configured
347 * or ematch is not enabled in the kernel, otherwise 0 is returned.
349 static inline int tcf_em_tree_match(struct sk_buff *skb,
350 struct tcf_ematch_tree *tree,
351 struct tcf_pkt_info *info)
353 if (tree->hdr.nmatches)
354 return __tcf_em_tree_match(skb, tree, info);
359 #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
361 #else /* CONFIG_NET_EMATCH */
363 struct tcf_ematch_tree {
366 #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
367 #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
368 #define tcf_em_tree_dump(skb, t, tlv) (0)
369 #define tcf_em_tree_change(tp, dst, src) do { } while(0)
370 #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
372 #endif /* CONFIG_NET_EMATCH */
374 static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
379 case TCF_LAYER_NETWORK:
380 return skb_network_header(skb);
381 case TCF_LAYER_TRANSPORT:
382 return skb_transport_header(skb);
388 static inline int tcf_valid_offset(const struct sk_buff *skb,
389 const unsigned char *ptr, const int len)
391 return likely((ptr + len) <= skb_tail_pointer(skb) &&
393 (ptr <= (ptr + len)));
396 #ifdef CONFIG_NET_CLS_IND
397 #include <net/net_namespace.h>
400 tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
402 char indev[IFNAMSIZ];
403 struct net_device *dev;
405 if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
407 dev = __dev_get_by_name(net, indev);
414 tcf_match_indev(struct sk_buff *skb, int ifindex)
420 return ifindex == skb->skb_iif;
422 #endif /* CONFIG_NET_CLS_IND */
424 struct tc_cls_u32_knode {
425 struct tcf_exts *exts;
426 struct tc_u32_sel *sel;
434 struct tc_cls_u32_hnode {
437 unsigned int divisor;
440 enum tc_clsu32_command {
442 TC_CLSU32_REPLACE_KNODE,
443 TC_CLSU32_DELETE_KNODE,
445 TC_CLSU32_REPLACE_HNODE,
446 TC_CLSU32_DELETE_HNODE,
449 struct tc_cls_u32_offload {
451 enum tc_clsu32_command command;
453 struct tc_cls_u32_knode knode;
454 struct tc_cls_u32_hnode hnode;
458 static inline bool tc_can_offload(const struct net_device *dev,
459 const struct tcf_proto *tp)
461 const struct Qdisc *sch = tp->q;
462 const struct Qdisc_class_ops *cops = sch->ops->cl_ops;
464 if (!(dev->features & NETIF_F_HW_TC))
466 if (!dev->netdev_ops->ndo_setup_tc)
468 if (cops && cops->tcf_cl_offload)
469 return cops->tcf_cl_offload(tp->classid);
474 static inline bool tc_skip_hw(u32 flags)
476 return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
479 static inline bool tc_should_offload(const struct net_device *dev,
480 const struct tcf_proto *tp, u32 flags)
482 if (tc_skip_hw(flags))
484 return tc_can_offload(dev, tp);
487 static inline bool tc_skip_sw(u32 flags)
489 return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
492 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
493 static inline bool tc_flags_valid(u32 flags)
495 if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
498 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
504 static inline bool tc_in_hw(u32 flags)
506 return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
510 TC_CLSFLOWER_REPLACE,
511 TC_CLSFLOWER_DESTROY,
515 struct tc_cls_flower_offload {
516 enum tc_fl_command command;
518 unsigned long cookie;
519 struct flow_dissector *dissector;
520 struct fl_flow_key *mask;
521 struct fl_flow_key *key;
522 struct tcf_exts *exts;
525 enum tc_matchall_command {
526 TC_CLSMATCHALL_REPLACE,
527 TC_CLSMATCHALL_DESTROY,
530 struct tc_cls_matchall_offload {
531 enum tc_matchall_command command;
532 struct tcf_exts *exts;
533 unsigned long cookie;
536 enum tc_clsbpf_command {
543 struct tc_cls_bpf_offload {
544 enum tc_clsbpf_command command;
545 struct tcf_exts *exts;
546 struct bpf_prog *prog;
548 bool exts_integrated;
553 /* This structure holds cookie structure that is passed from user
554 * to the kernel for actions and classifiers