11da177e4SLinus Torvalds #ifndef __NET_PKT_CLS_H 21da177e4SLinus Torvalds #define __NET_PKT_CLS_H 31da177e4SLinus Torvalds 41da177e4SLinus Torvalds #include <linux/pkt_cls.h> 51da177e4SLinus Torvalds #include <net/sch_generic.h> 61da177e4SLinus Torvalds #include <net/act_api.h> 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* Basic packet classifier frontend definitions. */ 91da177e4SLinus Torvalds 10fd2c3ef7SEric Dumazet struct tcf_walker { 111da177e4SLinus Torvalds int stop; 121da177e4SLinus Torvalds int skip; 131da177e4SLinus Torvalds int count; 141da177e4SLinus Torvalds int (*fn)(struct tcf_proto *, unsigned long node, struct tcf_walker *); 151da177e4SLinus Torvalds }; 161da177e4SLinus Torvalds 175c15257fSJoe Perches int register_tcf_proto_ops(struct tcf_proto_ops *ops); 185c15257fSJoe Perches int unregister_tcf_proto_ops(struct tcf_proto_ops *ops); 191da177e4SLinus Torvalds 208ae70032SJiri Pirko #ifdef CONFIG_NET_CLS 21cf1facdaSJiri Pirko void tcf_destroy_chain(struct tcf_proto __rcu **fl); 2287d83093SJiri Pirko int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, 2387d83093SJiri Pirko struct tcf_result *res, bool compat_mode); 2487d83093SJiri Pirko 258ae70032SJiri Pirko #else 268ae70032SJiri Pirko static inline void tcf_destroy_chain(struct tcf_proto __rcu **fl) 278ae70032SJiri Pirko { 288ae70032SJiri Pirko } 2987d83093SJiri Pirko 3087d83093SJiri Pirko static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, 3187d83093SJiri Pirko struct tcf_result *res, bool compat_mode) 3287d83093SJiri Pirko { 3387d83093SJiri Pirko return TC_ACT_UNSPEC; 3487d83093SJiri Pirko } 358ae70032SJiri Pirko #endif 36cf1facdaSJiri Pirko 371da177e4SLinus Torvalds static inline unsigned long 381da177e4SLinus Torvalds __cls_set_class(unsigned long *clp, unsigned long cl) 391da177e4SLinus Torvalds { 40a0efb80cSWANG Cong return xchg(clp, cl); 411da177e4SLinus Torvalds } 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds static inline unsigned long 441da177e4SLinus Torvalds cls_set_class(struct tcf_proto *tp, unsigned long *clp, 451da177e4SLinus Torvalds unsigned long cl) 461da177e4SLinus Torvalds { 471da177e4SLinus Torvalds unsigned long old_cl; 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds tcf_tree_lock(tp); 501da177e4SLinus Torvalds old_cl = __cls_set_class(clp, cl); 511da177e4SLinus Torvalds tcf_tree_unlock(tp); 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds return old_cl; 541da177e4SLinus Torvalds } 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds static inline void 571da177e4SLinus Torvalds tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base) 581da177e4SLinus Torvalds { 591da177e4SLinus Torvalds unsigned long cl; 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid); 621da177e4SLinus Torvalds cl = cls_set_class(tp, &r->class, cl); 631da177e4SLinus Torvalds if (cl) 641da177e4SLinus Torvalds tp->q->ops->cl_ops->unbind_tcf(tp->q, cl); 651da177e4SLinus Torvalds } 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds static inline void 681da177e4SLinus Torvalds tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r) 691da177e4SLinus Torvalds { 701da177e4SLinus Torvalds unsigned long cl; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds if ((cl = __cls_set_class(&r->class, 0)) != 0) 731da177e4SLinus Torvalds tp->q->ops->cl_ops->unbind_tcf(tp->q, cl); 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 76fd2c3ef7SEric Dumazet struct tcf_exts { 771da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 7833be6271SWANG Cong __u32 type; /* for backward compat(TCA_OLD_COMPAT) */ 7922dc13c8SWANG Cong int nr_actions; 8022dc13c8SWANG Cong struct tc_action **actions; 811da177e4SLinus Torvalds #endif 821da177e4SLinus Torvalds /* Map to export classifier specific extension TLV types to the 831da177e4SLinus Torvalds * generic extensions API. Unsupported extensions must be set to 0. 841da177e4SLinus Torvalds */ 851da177e4SLinus Torvalds int action; 861da177e4SLinus Torvalds int police; 871da177e4SLinus Torvalds }; 881da177e4SLinus Torvalds 89b9a24bb7SWANG Cong static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police) 9033be6271SWANG Cong { 9133be6271SWANG Cong #ifdef CONFIG_NET_CLS_ACT 925da57f42SWANG Cong exts->type = 0; 9322dc13c8SWANG Cong exts->nr_actions = 0; 9422dc13c8SWANG Cong exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *), 9522dc13c8SWANG Cong GFP_KERNEL); 96b9a24bb7SWANG Cong if (!exts->actions) 97b9a24bb7SWANG Cong return -ENOMEM; 9833be6271SWANG Cong #endif 995da57f42SWANG Cong exts->action = action; 1005da57f42SWANG Cong exts->police = police; 101b9a24bb7SWANG Cong return 0; 10233be6271SWANG Cong } 10333be6271SWANG Cong 1041da177e4SLinus Torvalds /** 1051da177e4SLinus Torvalds * tcf_exts_is_predicative - check if a predicative extension is present 1061da177e4SLinus Torvalds * @exts: tc filter extensions handle 1071da177e4SLinus Torvalds * 1081da177e4SLinus Torvalds * Returns 1 if a predicative extension is present, i.e. an extension which 1091da177e4SLinus Torvalds * might cause further actions and thus overrule the regular tcf_result. 1101da177e4SLinus Torvalds */ 1111da177e4SLinus Torvalds static inline int 1121da177e4SLinus Torvalds tcf_exts_is_predicative(struct tcf_exts *exts) 1131da177e4SLinus Torvalds { 1141da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 11522dc13c8SWANG Cong return exts->nr_actions; 1161da177e4SLinus Torvalds #else 1171da177e4SLinus Torvalds return 0; 1181da177e4SLinus Torvalds #endif 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds /** 1221da177e4SLinus Torvalds * tcf_exts_is_available - check if at least one extension is present 1231da177e4SLinus Torvalds * @exts: tc filter extensions handle 1241da177e4SLinus Torvalds * 1251da177e4SLinus Torvalds * Returns 1 if at least one extension is present. 1261da177e4SLinus Torvalds */ 1271da177e4SLinus Torvalds static inline int 1281da177e4SLinus Torvalds tcf_exts_is_available(struct tcf_exts *exts) 1291da177e4SLinus Torvalds { 1301da177e4SLinus Torvalds /* All non-predicative extensions must be added here. */ 1311da177e4SLinus Torvalds return tcf_exts_is_predicative(exts); 1321da177e4SLinus Torvalds } 1331da177e4SLinus Torvalds 13422dc13c8SWANG Cong static inline void tcf_exts_to_list(const struct tcf_exts *exts, 13522dc13c8SWANG Cong struct list_head *actions) 13622dc13c8SWANG Cong { 13722dc13c8SWANG Cong #ifdef CONFIG_NET_CLS_ACT 13822dc13c8SWANG Cong int i; 13922dc13c8SWANG Cong 14022dc13c8SWANG Cong for (i = 0; i < exts->nr_actions; i++) { 14122dc13c8SWANG Cong struct tc_action *a = exts->actions[i]; 14222dc13c8SWANG Cong 143fa5effe7SHadar Hen Zion list_add_tail(&a->list, actions); 14422dc13c8SWANG Cong } 14522dc13c8SWANG Cong #endif 14622dc13c8SWANG Cong } 14722dc13c8SWANG Cong 1481da177e4SLinus Torvalds /** 1491da177e4SLinus Torvalds * tcf_exts_exec - execute tc filter extensions 1501da177e4SLinus Torvalds * @skb: socket buffer 1511da177e4SLinus Torvalds * @exts: tc filter extensions handle 1521da177e4SLinus Torvalds * @res: desired result 1531da177e4SLinus Torvalds * 1541da177e4SLinus Torvalds * Executes all configured extensions. Returns 0 on a normal execution, 1551da177e4SLinus Torvalds * a negative number if the filter must be considered unmatched or 1561da177e4SLinus Torvalds * a positive action code (TC_ACT_*) which must be returned to the 1571da177e4SLinus Torvalds * underlying layer. 1581da177e4SLinus Torvalds */ 1591da177e4SLinus Torvalds static inline int 1601da177e4SLinus Torvalds tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts, 1611da177e4SLinus Torvalds struct tcf_result *res) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 16422dc13c8SWANG Cong if (exts->nr_actions) 16522dc13c8SWANG Cong return tcf_action_exec(skb, exts->actions, exts->nr_actions, 16622dc13c8SWANG Cong res); 1671da177e4SLinus Torvalds #endif 1681da177e4SLinus Torvalds return 0; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds 1712734437eSWANG Cong #ifdef CONFIG_NET_CLS_ACT 1722734437eSWANG Cong 17322dc13c8SWANG Cong #define tc_no_actions(_exts) ((_exts)->nr_actions == 0) 17422dc13c8SWANG Cong #define tc_single_action(_exts) ((_exts)->nr_actions == 1) 1752734437eSWANG Cong 1762734437eSWANG Cong #else /* CONFIG_NET_CLS_ACT */ 1772734437eSWANG Cong 1782734437eSWANG Cong #define tc_no_actions(_exts) true 1792734437eSWANG Cong #define tc_single_action(_exts) false 1802734437eSWANG Cong 1812734437eSWANG Cong #endif /* CONFIG_NET_CLS_ACT */ 1822734437eSWANG Cong 1835c15257fSJoe Perches int tcf_exts_validate(struct net *net, struct tcf_proto *tp, 184c1b52739SBenjamin LaHaise struct nlattr **tb, struct nlattr *rate_tlv, 1852f7ef2f8SCong Wang struct tcf_exts *exts, bool ovr); 18618d0264fSWANG Cong void tcf_exts_destroy(struct tcf_exts *exts); 1875c15257fSJoe Perches void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst, 1881da177e4SLinus Torvalds struct tcf_exts *src); 1895da57f42SWANG Cong int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts); 1905da57f42SWANG Cong int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts); 1917091d8c7SHadar Hen Zion int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts, 1927091d8c7SHadar Hen Zion struct net_device **hw_dev); 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /** 1951da177e4SLinus Torvalds * struct tcf_pkt_info - packet information 1961da177e4SLinus Torvalds */ 197fd2c3ef7SEric Dumazet struct tcf_pkt_info { 1981da177e4SLinus Torvalds unsigned char * ptr; 1991da177e4SLinus Torvalds int nexthdr; 2001da177e4SLinus Torvalds }; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds #ifdef CONFIG_NET_EMATCH 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds struct tcf_ematch_ops; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds /** 2071da177e4SLinus Torvalds * struct tcf_ematch - extended match (ematch) 2081da177e4SLinus Torvalds * 2091da177e4SLinus Torvalds * @matchid: identifier to allow userspace to reidentify a match 2101da177e4SLinus Torvalds * @flags: flags specifying attributes and the relation to other matches 2111da177e4SLinus Torvalds * @ops: the operations lookup table of the corresponding ematch module 2121da177e4SLinus Torvalds * @datalen: length of the ematch specific configuration data 2131da177e4SLinus Torvalds * @data: ematch specific data 2141da177e4SLinus Torvalds */ 215fd2c3ef7SEric Dumazet struct tcf_ematch { 2161da177e4SLinus Torvalds struct tcf_ematch_ops * ops; 2171da177e4SLinus Torvalds unsigned long data; 2181da177e4SLinus Torvalds unsigned int datalen; 2191da177e4SLinus Torvalds u16 matchid; 2201da177e4SLinus Torvalds u16 flags; 22182a470f1SJohn Fastabend struct net *net; 2221da177e4SLinus Torvalds }; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds static inline int tcf_em_is_container(struct tcf_ematch *em) 2251da177e4SLinus Torvalds { 2261da177e4SLinus Torvalds return !em->ops; 2271da177e4SLinus Torvalds } 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds static inline int tcf_em_is_simple(struct tcf_ematch *em) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds return em->flags & TCF_EM_SIMPLE; 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds static inline int tcf_em_is_inverted(struct tcf_ematch *em) 2351da177e4SLinus Torvalds { 2361da177e4SLinus Torvalds return em->flags & TCF_EM_INVERT; 2371da177e4SLinus Torvalds } 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds static inline int tcf_em_last_match(struct tcf_ematch *em) 2401da177e4SLinus Torvalds { 2411da177e4SLinus Torvalds return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END; 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds static inline int tcf_em_early_end(struct tcf_ematch *em, int result) 2451da177e4SLinus Torvalds { 2461da177e4SLinus Torvalds if (tcf_em_last_match(em)) 2471da177e4SLinus Torvalds return 1; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds if (result == 0 && em->flags & TCF_EM_REL_AND) 2501da177e4SLinus Torvalds return 1; 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds if (result != 0 && em->flags & TCF_EM_REL_OR) 2531da177e4SLinus Torvalds return 1; 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds return 0; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds /** 2591da177e4SLinus Torvalds * struct tcf_ematch_tree - ematch tree handle 2601da177e4SLinus Torvalds * 2611da177e4SLinus Torvalds * @hdr: ematch tree header supplied by userspace 2621da177e4SLinus Torvalds * @matches: array of ematches 2631da177e4SLinus Torvalds */ 264fd2c3ef7SEric Dumazet struct tcf_ematch_tree { 2651da177e4SLinus Torvalds struct tcf_ematch_tree_hdr hdr; 2661da177e4SLinus Torvalds struct tcf_ematch * matches; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds }; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds /** 2711da177e4SLinus Torvalds * struct tcf_ematch_ops - ematch module operations 2721da177e4SLinus Torvalds * 2731da177e4SLinus Torvalds * @kind: identifier (kind) of this ematch module 2741da177e4SLinus Torvalds * @datalen: length of expected configuration data (optional) 2751da177e4SLinus Torvalds * @change: called during validation (optional) 2761da177e4SLinus Torvalds * @match: called during ematch tree evaluation, must return 1/0 2771da177e4SLinus Torvalds * @destroy: called during destroyage (optional) 2781da177e4SLinus Torvalds * @dump: called during dumping process (optional) 2791da177e4SLinus Torvalds * @owner: owner, must be set to THIS_MODULE 2801da177e4SLinus Torvalds * @link: link to previous/next ematch module (internal use) 2811da177e4SLinus Torvalds */ 282fd2c3ef7SEric Dumazet struct tcf_ematch_ops { 2831da177e4SLinus Torvalds int kind; 2841da177e4SLinus Torvalds int datalen; 28582a470f1SJohn Fastabend int (*change)(struct net *net, void *, 2861da177e4SLinus Torvalds int, struct tcf_ematch *); 2871da177e4SLinus Torvalds int (*match)(struct sk_buff *, struct tcf_ematch *, 2881da177e4SLinus Torvalds struct tcf_pkt_info *); 28982a470f1SJohn Fastabend void (*destroy)(struct tcf_ematch *); 2901da177e4SLinus Torvalds int (*dump)(struct sk_buff *, struct tcf_ematch *); 2911da177e4SLinus Torvalds struct module *owner; 2921da177e4SLinus Torvalds struct list_head link; 2931da177e4SLinus Torvalds }; 2941da177e4SLinus Torvalds 2955c15257fSJoe Perches int tcf_em_register(struct tcf_ematch_ops *); 2965c15257fSJoe Perches void tcf_em_unregister(struct tcf_ematch_ops *); 2975c15257fSJoe Perches int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *, 2981da177e4SLinus Torvalds struct tcf_ematch_tree *); 29982a470f1SJohn Fastabend void tcf_em_tree_destroy(struct tcf_ematch_tree *); 3005c15257fSJoe Perches int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int); 3015c15257fSJoe Perches int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *, 3021da177e4SLinus Torvalds struct tcf_pkt_info *); 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds /** 3051da177e4SLinus Torvalds * tcf_em_tree_change - replace ematch tree of a running classifier 3061da177e4SLinus Torvalds * 3071da177e4SLinus Torvalds * @tp: classifier kind handle 3081da177e4SLinus Torvalds * @dst: destination ematch tree variable 3091da177e4SLinus Torvalds * @src: source ematch tree (temporary tree from tcf_em_tree_validate) 3101da177e4SLinus Torvalds * 3111da177e4SLinus Torvalds * This functions replaces the ematch tree in @dst with the ematch 3121da177e4SLinus Torvalds * tree in @src. The classifier in charge of the ematch tree may be 3131da177e4SLinus Torvalds * running. 3141da177e4SLinus Torvalds */ 3151da177e4SLinus Torvalds static inline void tcf_em_tree_change(struct tcf_proto *tp, 3161da177e4SLinus Torvalds struct tcf_ematch_tree *dst, 3171da177e4SLinus Torvalds struct tcf_ematch_tree *src) 3181da177e4SLinus Torvalds { 3191da177e4SLinus Torvalds tcf_tree_lock(tp); 3201da177e4SLinus Torvalds memcpy(dst, src, sizeof(*dst)); 3211da177e4SLinus Torvalds tcf_tree_unlock(tp); 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds /** 3251da177e4SLinus Torvalds * tcf_em_tree_match - evaulate an ematch tree 3261da177e4SLinus Torvalds * 3271da177e4SLinus Torvalds * @skb: socket buffer of the packet in question 3281da177e4SLinus Torvalds * @tree: ematch tree to be used for evaluation 3291da177e4SLinus Torvalds * @info: packet information examined by classifier 3301da177e4SLinus Torvalds * 3311da177e4SLinus Torvalds * This function matches @skb against the ematch tree in @tree by going 3321da177e4SLinus Torvalds * through all ematches respecting their logic relations returning 3331da177e4SLinus Torvalds * as soon as the result is obvious. 3341da177e4SLinus Torvalds * 3351da177e4SLinus Torvalds * Returns 1 if the ematch tree as-one matches, no ematches are configured 3361da177e4SLinus Torvalds * or ematch is not enabled in the kernel, otherwise 0 is returned. 3371da177e4SLinus Torvalds */ 3381da177e4SLinus Torvalds static inline int tcf_em_tree_match(struct sk_buff *skb, 3391da177e4SLinus Torvalds struct tcf_ematch_tree *tree, 3401da177e4SLinus Torvalds struct tcf_pkt_info *info) 3411da177e4SLinus Torvalds { 3421da177e4SLinus Torvalds if (tree->hdr.nmatches) 3431da177e4SLinus Torvalds return __tcf_em_tree_match(skb, tree, info); 3441da177e4SLinus Torvalds else 3451da177e4SLinus Torvalds return 1; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds 348db3d99c0SPatrick McHardy #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind)) 349db3d99c0SPatrick McHardy 3501da177e4SLinus Torvalds #else /* CONFIG_NET_EMATCH */ 3511da177e4SLinus Torvalds 352fd2c3ef7SEric Dumazet struct tcf_ematch_tree { 3531da177e4SLinus Torvalds }; 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0) 35682a470f1SJohn Fastabend #define tcf_em_tree_destroy(t) do { (void)(t); } while(0) 3571da177e4SLinus Torvalds #define tcf_em_tree_dump(skb, t, tlv) (0) 3581da177e4SLinus Torvalds #define tcf_em_tree_change(tp, dst, src) do { } while(0) 3591da177e4SLinus Torvalds #define tcf_em_tree_match(skb, t, info) ((void)(info), 1) 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds #endif /* CONFIG_NET_EMATCH */ 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer) 3641da177e4SLinus Torvalds { 3651da177e4SLinus Torvalds switch (layer) { 3661da177e4SLinus Torvalds case TCF_LAYER_LINK: 3671da177e4SLinus Torvalds return skb->data; 3681da177e4SLinus Torvalds case TCF_LAYER_NETWORK: 369d56f90a7SArnaldo Carvalho de Melo return skb_network_header(skb); 3701da177e4SLinus Torvalds case TCF_LAYER_TRANSPORT: 3719c70220bSArnaldo Carvalho de Melo return skb_transport_header(skb); 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds return NULL; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 377eddc9ec5SArnaldo Carvalho de Melo static inline int tcf_valid_offset(const struct sk_buff *skb, 378eddc9ec5SArnaldo Carvalho de Melo const unsigned char *ptr, const int len) 3791da177e4SLinus Torvalds { 380da521b2cSDavid S. Miller return likely((ptr + len) <= skb_tail_pointer(skb) && 381da521b2cSDavid S. Miller ptr >= skb->head && 382da521b2cSDavid S. Miller (ptr <= (ptr + len))); 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_IND 3860eeb8ffcSDenis V. Lunev #include <net/net_namespace.h> 3870eeb8ffcSDenis V. Lunev 3881da177e4SLinus Torvalds static inline int 3892519a602SWANG Cong tcf_change_indev(struct net *net, struct nlattr *indev_tlv) 3901da177e4SLinus Torvalds { 3912519a602SWANG Cong char indev[IFNAMSIZ]; 392c01003c2SPatrick McHardy struct net_device *dev; 393c01003c2SPatrick McHardy 3942519a602SWANG Cong if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ) 3952519a602SWANG Cong return -EINVAL; 3962519a602SWANG Cong dev = __dev_get_by_name(net, indev); 3972519a602SWANG Cong if (!dev) 3982519a602SWANG Cong return -ENODEV; 3992519a602SWANG Cong return dev->ifindex; 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 4022519a602SWANG Cong static inline bool 4032519a602SWANG Cong tcf_match_indev(struct sk_buff *skb, int ifindex) 4042519a602SWANG Cong { 4052519a602SWANG Cong if (!ifindex) 4062519a602SWANG Cong return true; 4072519a602SWANG Cong if (!skb->skb_iif) 4082519a602SWANG Cong return false; 4092519a602SWANG Cong return ifindex == skb->skb_iif; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds #endif /* CONFIG_NET_CLS_IND */ 4121da177e4SLinus Torvalds 413a1b7c5fdSJohn Fastabend struct tc_cls_u32_knode { 414a1b7c5fdSJohn Fastabend struct tcf_exts *exts; 415e014860eSJohn Fastabend struct tc_u32_sel *sel; 416a1b7c5fdSJohn Fastabend u32 handle; 417a1b7c5fdSJohn Fastabend u32 val; 418a1b7c5fdSJohn Fastabend u32 mask; 419a1b7c5fdSJohn Fastabend u32 link_handle; 420e014860eSJohn Fastabend u8 fshift; 421a1b7c5fdSJohn Fastabend }; 422a1b7c5fdSJohn Fastabend 423a1b7c5fdSJohn Fastabend struct tc_cls_u32_hnode { 424a1b7c5fdSJohn Fastabend u32 handle; 425a1b7c5fdSJohn Fastabend u32 prio; 426a1b7c5fdSJohn Fastabend unsigned int divisor; 427a1b7c5fdSJohn Fastabend }; 428a1b7c5fdSJohn Fastabend 429a1b7c5fdSJohn Fastabend enum tc_clsu32_command { 430a1b7c5fdSJohn Fastabend TC_CLSU32_NEW_KNODE, 431a1b7c5fdSJohn Fastabend TC_CLSU32_REPLACE_KNODE, 432a1b7c5fdSJohn Fastabend TC_CLSU32_DELETE_KNODE, 433a1b7c5fdSJohn Fastabend TC_CLSU32_NEW_HNODE, 434a1b7c5fdSJohn Fastabend TC_CLSU32_REPLACE_HNODE, 435a1b7c5fdSJohn Fastabend TC_CLSU32_DELETE_HNODE, 436a1b7c5fdSJohn Fastabend }; 437a1b7c5fdSJohn Fastabend 438a1b7c5fdSJohn Fastabend struct tc_cls_u32_offload { 439a1b7c5fdSJohn Fastabend /* knode values */ 440a1b7c5fdSJohn Fastabend enum tc_clsu32_command command; 441a1b7c5fdSJohn Fastabend union { 442a1b7c5fdSJohn Fastabend struct tc_cls_u32_knode knode; 443a1b7c5fdSJohn Fastabend struct tc_cls_u32_hnode hnode; 444a1b7c5fdSJohn Fastabend }; 445a1b7c5fdSJohn Fastabend }; 446a1b7c5fdSJohn Fastabend 44755330f05SHadar Hen Zion static inline bool tc_can_offload(const struct net_device *dev, 44855330f05SHadar Hen Zion const struct tcf_proto *tp) 4496843e7a2SJohn Fastabend { 45092c075dbSDaniel Borkmann const struct Qdisc *sch = tp->q; 45192c075dbSDaniel Borkmann const struct Qdisc_class_ops *cops = sch->ops->cl_ops; 45292c075dbSDaniel Borkmann 4532b6ab0d3SJohn Fastabend if (!(dev->features & NETIF_F_HW_TC)) 4542b6ab0d3SJohn Fastabend return false; 4559e8ce79cSJohn Fastabend if (!dev->netdev_ops->ndo_setup_tc) 4569e8ce79cSJohn Fastabend return false; 45792c075dbSDaniel Borkmann if (cops && cops->tcf_cl_offload) 45892c075dbSDaniel Borkmann return cops->tcf_cl_offload(tp->classid); 4599e8ce79cSJohn Fastabend 4609e8ce79cSJohn Fastabend return true; 4616843e7a2SJohn Fastabend } 4626843e7a2SJohn Fastabend 46355330f05SHadar Hen Zion static inline bool tc_skip_hw(u32 flags) 46455330f05SHadar Hen Zion { 46555330f05SHadar Hen Zion return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false; 46655330f05SHadar Hen Zion } 46755330f05SHadar Hen Zion 46855330f05SHadar Hen Zion static inline bool tc_should_offload(const struct net_device *dev, 46955330f05SHadar Hen Zion const struct tcf_proto *tp, u32 flags) 47055330f05SHadar Hen Zion { 47155330f05SHadar Hen Zion if (tc_skip_hw(flags)) 47255330f05SHadar Hen Zion return false; 47355330f05SHadar Hen Zion return tc_can_offload(dev, tp); 47455330f05SHadar Hen Zion } 47555330f05SHadar Hen Zion 476d34e3e18SSamudrala, Sridhar static inline bool tc_skip_sw(u32 flags) 477d34e3e18SSamudrala, Sridhar { 478d34e3e18SSamudrala, Sridhar return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false; 479d34e3e18SSamudrala, Sridhar } 480d34e3e18SSamudrala, Sridhar 481d34e3e18SSamudrala, Sridhar /* SKIP_HW and SKIP_SW are mutually exclusive flags. */ 482d34e3e18SSamudrala, Sridhar static inline bool tc_flags_valid(u32 flags) 483d34e3e18SSamudrala, Sridhar { 484d34e3e18SSamudrala, Sridhar if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)) 485d34e3e18SSamudrala, Sridhar return false; 486d34e3e18SSamudrala, Sridhar 487d34e3e18SSamudrala, Sridhar if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))) 488d34e3e18SSamudrala, Sridhar return false; 489d34e3e18SSamudrala, Sridhar 490d34e3e18SSamudrala, Sridhar return true; 491d34e3e18SSamudrala, Sridhar } 492d34e3e18SSamudrala, Sridhar 493e696028aSOr Gerlitz static inline bool tc_in_hw(u32 flags) 494e696028aSOr Gerlitz { 495e696028aSOr Gerlitz return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false; 496e696028aSOr Gerlitz } 497e696028aSOr Gerlitz 4985b33f488SAmir Vadai enum tc_fl_command { 4995b33f488SAmir Vadai TC_CLSFLOWER_REPLACE, 5005b33f488SAmir Vadai TC_CLSFLOWER_DESTROY, 50110cbc684SAmir Vadai TC_CLSFLOWER_STATS, 5025b33f488SAmir Vadai }; 5035b33f488SAmir Vadai 5045b33f488SAmir Vadai struct tc_cls_flower_offload { 5055b33f488SAmir Vadai enum tc_fl_command command; 50669ca05ceSJiri Pirko u32 prio; 5078208d21bSAmir Vadai unsigned long cookie; 5085b33f488SAmir Vadai struct flow_dissector *dissector; 5095b33f488SAmir Vadai struct fl_flow_key *mask; 5105b33f488SAmir Vadai struct fl_flow_key *key; 5115b33f488SAmir Vadai struct tcf_exts *exts; 5125b33f488SAmir Vadai }; 5135b33f488SAmir Vadai 514b87f7936SYotam Gigi enum tc_matchall_command { 515b87f7936SYotam Gigi TC_CLSMATCHALL_REPLACE, 516b87f7936SYotam Gigi TC_CLSMATCHALL_DESTROY, 517b87f7936SYotam Gigi }; 518b87f7936SYotam Gigi 519b87f7936SYotam Gigi struct tc_cls_matchall_offload { 520b87f7936SYotam Gigi enum tc_matchall_command command; 521b87f7936SYotam Gigi struct tcf_exts *exts; 522b87f7936SYotam Gigi unsigned long cookie; 523b87f7936SYotam Gigi }; 524b87f7936SYotam Gigi 525332ae8e2SJakub Kicinski enum tc_clsbpf_command { 526332ae8e2SJakub Kicinski TC_CLSBPF_ADD, 527332ae8e2SJakub Kicinski TC_CLSBPF_REPLACE, 528332ae8e2SJakub Kicinski TC_CLSBPF_DESTROY, 52968d64063SJakub Kicinski TC_CLSBPF_STATS, 530332ae8e2SJakub Kicinski }; 531332ae8e2SJakub Kicinski 532332ae8e2SJakub Kicinski struct tc_cls_bpf_offload { 533332ae8e2SJakub Kicinski enum tc_clsbpf_command command; 534332ae8e2SJakub Kicinski struct tcf_exts *exts; 535332ae8e2SJakub Kicinski struct bpf_prog *prog; 536332ae8e2SJakub Kicinski const char *name; 537332ae8e2SJakub Kicinski bool exts_integrated; 5380d01d45fSJakub Kicinski u32 gen_flags; 539332ae8e2SJakub Kicinski }; 540332ae8e2SJakub Kicinski 5411045ba77SJamal Hadi Salim 5421045ba77SJamal Hadi Salim /* This structure holds cookie structure that is passed from user 5431045ba77SJamal Hadi Salim * to the kernel for actions and classifiers 5441045ba77SJamal Hadi Salim */ 5451045ba77SJamal Hadi Salim struct tc_cookie { 5461045ba77SJamal Hadi Salim u8 *data; 5471045ba77SJamal Hadi Salim u32 len; 5481045ba77SJamal Hadi Salim }; 5491da177e4SLinus Torvalds #endif 550