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,
23 void tcf_chain_put(struct tcf_chain *chain);
24 int tcf_block_get(struct tcf_block **p_block,
25 struct tcf_proto __rcu **p_filter_chain);
26 void tcf_block_put(struct tcf_block *block);
27 int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
28 struct tcf_result *res, bool compat_mode);
32 int tcf_block_get(struct tcf_block **p_block,
33 struct tcf_proto __rcu **p_filter_chain)
38 static inline void tcf_block_put(struct tcf_block *block)
42 static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
43 struct tcf_result *res, bool compat_mode)
49 static inline unsigned long
50 __cls_set_class(unsigned long *clp, unsigned long cl)
55 static inline unsigned long
56 cls_set_class(struct tcf_proto *tp, unsigned long *clp,
62 old_cl = __cls_set_class(clp, cl);
69 tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
73 cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
74 cl = cls_set_class(tp, &r->class, cl);
76 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
80 tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
84 if ((cl = __cls_set_class(&r->class, 0)) != 0)
85 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
89 #ifdef CONFIG_NET_CLS_ACT
90 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
92 struct tc_action **actions;
94 /* Map to export classifier specific extension TLV types to the
95 * generic extensions API. Unsupported extensions must be set to 0.
101 static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
103 #ifdef CONFIG_NET_CLS_ACT
105 exts->nr_actions = 0;
106 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
111 exts->action = action;
112 exts->police = police;
116 static inline void tcf_exts_to_list(const struct tcf_exts *exts,
117 struct list_head *actions)
119 #ifdef CONFIG_NET_CLS_ACT
122 for (i = 0; i < exts->nr_actions; i++) {
123 struct tc_action *a = exts->actions[i];
125 list_add_tail(&a->list, actions);
131 tcf_exts_stats_update(const struct tcf_exts *exts,
132 u64 bytes, u64 packets, u64 lastuse)
134 #ifdef CONFIG_NET_CLS_ACT
139 for (i = 0; i < exts->nr_actions; i++) {
140 struct tc_action *a = exts->actions[i];
142 tcf_action_stats_update(a, bytes, packets, lastuse);
150 * tcf_exts_has_actions - check if at least one action is present
151 * @exts: tc filter extensions handle
153 * Returns true if at least one action is present.
155 static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
157 #ifdef CONFIG_NET_CLS_ACT
158 return exts->nr_actions;
165 * tcf_exts_has_one_action - check if exactly one action is present
166 * @exts: tc filter extensions handle
168 * Returns true if exactly one action is present.
170 static inline bool tcf_exts_has_one_action(struct tcf_exts *exts)
172 #ifdef CONFIG_NET_CLS_ACT
173 return exts->nr_actions == 1;
180 * tcf_exts_exec - execute tc filter extensions
181 * @skb: socket buffer
182 * @exts: tc filter extensions handle
183 * @res: desired result
185 * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
186 * a negative number if the filter must be considered unmatched or
187 * a positive action code (TC_ACT_*) which must be returned to the
191 tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
192 struct tcf_result *res)
194 #ifdef CONFIG_NET_CLS_ACT
195 return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
200 int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
201 struct nlattr **tb, struct nlattr *rate_tlv,
202 struct tcf_exts *exts, bool ovr);
203 void tcf_exts_destroy(struct tcf_exts *exts);
204 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
205 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
206 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
207 int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
208 struct net_device **hw_dev);
211 * struct tcf_pkt_info - packet information
213 struct tcf_pkt_info {
218 #ifdef CONFIG_NET_EMATCH
220 struct tcf_ematch_ops;
223 * struct tcf_ematch - extended match (ematch)
225 * @matchid: identifier to allow userspace to reidentify a match
226 * @flags: flags specifying attributes and the relation to other matches
227 * @ops: the operations lookup table of the corresponding ematch module
228 * @datalen: length of the ematch specific configuration data
229 * @data: ematch specific data
232 struct tcf_ematch_ops * ops;
234 unsigned int datalen;
240 static inline int tcf_em_is_container(struct tcf_ematch *em)
245 static inline int tcf_em_is_simple(struct tcf_ematch *em)
247 return em->flags & TCF_EM_SIMPLE;
250 static inline int tcf_em_is_inverted(struct tcf_ematch *em)
252 return em->flags & TCF_EM_INVERT;
255 static inline int tcf_em_last_match(struct tcf_ematch *em)
257 return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
260 static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
262 if (tcf_em_last_match(em))
265 if (result == 0 && em->flags & TCF_EM_REL_AND)
268 if (result != 0 && em->flags & TCF_EM_REL_OR)
275 * struct tcf_ematch_tree - ematch tree handle
277 * @hdr: ematch tree header supplied by userspace
278 * @matches: array of ematches
280 struct tcf_ematch_tree {
281 struct tcf_ematch_tree_hdr hdr;
282 struct tcf_ematch * matches;
287 * struct tcf_ematch_ops - ematch module operations
289 * @kind: identifier (kind) of this ematch module
290 * @datalen: length of expected configuration data (optional)
291 * @change: called during validation (optional)
292 * @match: called during ematch tree evaluation, must return 1/0
293 * @destroy: called during destroyage (optional)
294 * @dump: called during dumping process (optional)
295 * @owner: owner, must be set to THIS_MODULE
296 * @link: link to previous/next ematch module (internal use)
298 struct tcf_ematch_ops {
301 int (*change)(struct net *net, void *,
302 int, struct tcf_ematch *);
303 int (*match)(struct sk_buff *, struct tcf_ematch *,
304 struct tcf_pkt_info *);
305 void (*destroy)(struct tcf_ematch *);
306 int (*dump)(struct sk_buff *, struct tcf_ematch *);
307 struct module *owner;
308 struct list_head link;
311 int tcf_em_register(struct tcf_ematch_ops *);
312 void tcf_em_unregister(struct tcf_ematch_ops *);
313 int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
314 struct tcf_ematch_tree *);
315 void tcf_em_tree_destroy(struct tcf_ematch_tree *);
316 int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
317 int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
318 struct tcf_pkt_info *);
321 * tcf_em_tree_match - evaulate an ematch tree
323 * @skb: socket buffer of the packet in question
324 * @tree: ematch tree to be used for evaluation
325 * @info: packet information examined by classifier
327 * This function matches @skb against the ematch tree in @tree by going
328 * through all ematches respecting their logic relations returning
329 * as soon as the result is obvious.
331 * Returns 1 if the ematch tree as-one matches, no ematches are configured
332 * or ematch is not enabled in the kernel, otherwise 0 is returned.
334 static inline int tcf_em_tree_match(struct sk_buff *skb,
335 struct tcf_ematch_tree *tree,
336 struct tcf_pkt_info *info)
338 if (tree->hdr.nmatches)
339 return __tcf_em_tree_match(skb, tree, info);
344 #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
346 #else /* CONFIG_NET_EMATCH */
348 struct tcf_ematch_tree {
351 #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
352 #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
353 #define tcf_em_tree_dump(skb, t, tlv) (0)
354 #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
356 #endif /* CONFIG_NET_EMATCH */
358 static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
363 case TCF_LAYER_NETWORK:
364 return skb_network_header(skb);
365 case TCF_LAYER_TRANSPORT:
366 return skb_transport_header(skb);
372 static inline int tcf_valid_offset(const struct sk_buff *skb,
373 const unsigned char *ptr, const int len)
375 return likely((ptr + len) <= skb_tail_pointer(skb) &&
377 (ptr <= (ptr + len)));
380 #ifdef CONFIG_NET_CLS_IND
381 #include <net/net_namespace.h>
384 tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
386 char indev[IFNAMSIZ];
387 struct net_device *dev;
389 if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
391 dev = __dev_get_by_name(net, indev);
398 tcf_match_indev(struct sk_buff *skb, int ifindex)
404 return ifindex == skb->skb_iif;
406 #endif /* CONFIG_NET_CLS_IND */
408 struct tc_cls_u32_knode {
409 struct tcf_exts *exts;
410 struct tc_u32_sel *sel;
418 struct tc_cls_u32_hnode {
421 unsigned int divisor;
424 enum tc_clsu32_command {
426 TC_CLSU32_REPLACE_KNODE,
427 TC_CLSU32_DELETE_KNODE,
429 TC_CLSU32_REPLACE_HNODE,
430 TC_CLSU32_DELETE_HNODE,
433 struct tc_cls_u32_offload {
435 enum tc_clsu32_command command;
437 struct tc_cls_u32_knode knode;
438 struct tc_cls_u32_hnode hnode;
442 static inline bool tc_can_offload(const struct net_device *dev,
443 const struct tcf_proto *tp)
445 const struct Qdisc *sch = tp->q;
446 const struct Qdisc_class_ops *cops = sch->ops->cl_ops;
448 if (!(dev->features & NETIF_F_HW_TC))
450 if (!dev->netdev_ops->ndo_setup_tc)
452 if (cops && cops->tcf_cl_offload)
453 return cops->tcf_cl_offload(tp->classid);
458 static inline bool tc_skip_hw(u32 flags)
460 return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
463 static inline bool tc_should_offload(const struct net_device *dev,
464 const struct tcf_proto *tp, u32 flags)
466 if (tc_skip_hw(flags))
468 return tc_can_offload(dev, tp);
471 static inline bool tc_skip_sw(u32 flags)
473 return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
476 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
477 static inline bool tc_flags_valid(u32 flags)
479 if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
482 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
488 static inline bool tc_in_hw(u32 flags)
490 return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
494 TC_CLSFLOWER_REPLACE,
495 TC_CLSFLOWER_DESTROY,
499 struct tc_cls_flower_offload {
500 enum tc_fl_command command;
502 unsigned long cookie;
503 struct flow_dissector *dissector;
504 struct fl_flow_key *mask;
505 struct fl_flow_key *key;
506 struct tcf_exts *exts;
510 enum tc_matchall_command {
511 TC_CLSMATCHALL_REPLACE,
512 TC_CLSMATCHALL_DESTROY,
515 struct tc_cls_matchall_offload {
516 enum tc_matchall_command command;
517 struct tcf_exts *exts;
518 unsigned long cookie;
521 enum tc_clsbpf_command {
528 struct tc_cls_bpf_offload {
529 enum tc_clsbpf_command command;
530 struct tcf_exts *exts;
531 struct bpf_prog *prog;
533 bool exts_integrated;
538 /* This structure holds cookie structure that is passed from user
539 * to the kernel for actions and classifiers