11da177e4SLinus Torvalds #ifndef __NET_PKT_CLS_H 21da177e4SLinus Torvalds #define __NET_PKT_CLS_H 31da177e4SLinus Torvalds 41da177e4SLinus Torvalds #include <linux/pkt_cls.h> 57aa0045dSCong Wang #include <linux/workqueue.h> 61da177e4SLinus Torvalds #include <net/sch_generic.h> 71da177e4SLinus Torvalds #include <net/act_api.h> 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds /* Basic packet classifier frontend definitions. */ 101da177e4SLinus Torvalds 11fd2c3ef7SEric Dumazet struct tcf_walker { 121da177e4SLinus Torvalds int stop; 131da177e4SLinus Torvalds int skip; 141da177e4SLinus Torvalds int count; 158113c095SWANG Cong int (*fn)(struct tcf_proto *, void *node, struct tcf_walker *); 161da177e4SLinus Torvalds }; 171da177e4SLinus Torvalds 185c15257fSJoe Perches int register_tcf_proto_ops(struct tcf_proto_ops *ops); 195c15257fSJoe Perches int unregister_tcf_proto_ops(struct tcf_proto_ops *ops); 201da177e4SLinus Torvalds 218c4083b3SJiri Pirko enum tcf_block_binder_type { 228c4083b3SJiri Pirko TCF_BLOCK_BINDER_TYPE_UNSPEC, 236e40cf2dSJiri Pirko TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS, 246e40cf2dSJiri Pirko TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS, 258c4083b3SJiri Pirko }; 268c4083b3SJiri Pirko 278c4083b3SJiri Pirko struct tcf_block_ext_info { 288c4083b3SJiri Pirko enum tcf_block_binder_type binder_type; 29c7eb7d72SJiri Pirko tcf_chain_head_change_t *chain_head_change; 30c7eb7d72SJiri Pirko void *chain_head_change_priv; 318c4083b3SJiri Pirko }; 328c4083b3SJiri Pirko 33acb67442SJiri Pirko struct tcf_block_cb; 347aa0045dSCong Wang bool tcf_queue_work(struct work_struct *work); 35acb67442SJiri Pirko 368ae70032SJiri Pirko #ifdef CONFIG_NET_CLS 37367a8ce8SWANG Cong struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index, 38367a8ce8SWANG Cong bool create); 395bc17018SJiri Pirko void tcf_chain_put(struct tcf_chain *chain); 406529eabaSJiri Pirko int tcf_block_get(struct tcf_block **p_block, 4169d78ef2SJiri Pirko struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q); 42c7eb7d72SJiri Pirko int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q, 438c4083b3SJiri Pirko struct tcf_block_ext_info *ei); 446529eabaSJiri Pirko void tcf_block_put(struct tcf_block *block); 45c7eb7d72SJiri Pirko void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q, 468c4083b3SJiri Pirko struct tcf_block_ext_info *ei); 4744186460SJiri Pirko 4844186460SJiri Pirko static inline struct Qdisc *tcf_block_q(struct tcf_block *block) 4944186460SJiri Pirko { 5044186460SJiri Pirko return block->q; 5144186460SJiri Pirko } 5244186460SJiri Pirko 5344186460SJiri Pirko static inline struct net_device *tcf_block_dev(struct tcf_block *block) 5444186460SJiri Pirko { 5544186460SJiri Pirko return tcf_block_q(block)->dev_queue->dev; 5644186460SJiri Pirko } 5744186460SJiri Pirko 58acb67442SJiri Pirko void *tcf_block_cb_priv(struct tcf_block_cb *block_cb); 59acb67442SJiri Pirko struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block, 60acb67442SJiri Pirko tc_setup_cb_t *cb, void *cb_ident); 61acb67442SJiri Pirko void tcf_block_cb_incref(struct tcf_block_cb *block_cb); 62acb67442SJiri Pirko unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb); 63acb67442SJiri Pirko struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block, 64acb67442SJiri Pirko tc_setup_cb_t *cb, void *cb_ident, 65acb67442SJiri Pirko void *cb_priv); 66acb67442SJiri Pirko int tcf_block_cb_register(struct tcf_block *block, 67acb67442SJiri Pirko tc_setup_cb_t *cb, void *cb_ident, 68acb67442SJiri Pirko void *cb_priv); 69acb67442SJiri Pirko void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb); 70acb67442SJiri Pirko void tcf_block_cb_unregister(struct tcf_block *block, 71acb67442SJiri Pirko tc_setup_cb_t *cb, void *cb_ident); 72acb67442SJiri Pirko 7387d83093SJiri Pirko int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, 7487d83093SJiri Pirko struct tcf_result *res, bool compat_mode); 7587d83093SJiri Pirko 768ae70032SJiri Pirko #else 776529eabaSJiri Pirko static inline 786529eabaSJiri Pirko int tcf_block_get(struct tcf_block **p_block, 7969d78ef2SJiri Pirko struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q) 806529eabaSJiri Pirko { 816529eabaSJiri Pirko return 0; 826529eabaSJiri Pirko } 836529eabaSJiri Pirko 848c4083b3SJiri Pirko static inline 85c7eb7d72SJiri Pirko int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q, 868c4083b3SJiri Pirko struct tcf_block_ext_info *ei) 878c4083b3SJiri Pirko { 888c4083b3SJiri Pirko return 0; 898c4083b3SJiri Pirko } 908c4083b3SJiri Pirko 916529eabaSJiri Pirko static inline void tcf_block_put(struct tcf_block *block) 928ae70032SJiri Pirko { 938ae70032SJiri Pirko } 9487d83093SJiri Pirko 958c4083b3SJiri Pirko static inline 96c7eb7d72SJiri Pirko void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q, 978c4083b3SJiri Pirko struct tcf_block_ext_info *ei) 988c4083b3SJiri Pirko { 998c4083b3SJiri Pirko } 1008c4083b3SJiri Pirko 10144186460SJiri Pirko static inline struct Qdisc *tcf_block_q(struct tcf_block *block) 10244186460SJiri Pirko { 10344186460SJiri Pirko return NULL; 10444186460SJiri Pirko } 10544186460SJiri Pirko 10644186460SJiri Pirko static inline struct net_device *tcf_block_dev(struct tcf_block *block) 10744186460SJiri Pirko { 10844186460SJiri Pirko return NULL; 10944186460SJiri Pirko } 11044186460SJiri Pirko 111acb67442SJiri Pirko static inline 112acb67442SJiri Pirko int tc_setup_cb_block_register(struct tcf_block *block, tc_setup_cb_t *cb, 113acb67442SJiri Pirko void *cb_priv) 114acb67442SJiri Pirko { 115acb67442SJiri Pirko return 0; 116acb67442SJiri Pirko } 117acb67442SJiri Pirko 118acb67442SJiri Pirko static inline 119acb67442SJiri Pirko void tc_setup_cb_block_unregister(struct tcf_block *block, tc_setup_cb_t *cb, 120acb67442SJiri Pirko void *cb_priv) 121acb67442SJiri Pirko { 122acb67442SJiri Pirko } 123acb67442SJiri Pirko 124acb67442SJiri Pirko static inline 125acb67442SJiri Pirko void *tcf_block_cb_priv(struct tcf_block_cb *block_cb) 126acb67442SJiri Pirko { 127acb67442SJiri Pirko return NULL; 128acb67442SJiri Pirko } 129acb67442SJiri Pirko 130acb67442SJiri Pirko static inline 131acb67442SJiri Pirko struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block, 132acb67442SJiri Pirko tc_setup_cb_t *cb, void *cb_ident) 133acb67442SJiri Pirko { 134acb67442SJiri Pirko return NULL; 135acb67442SJiri Pirko } 136acb67442SJiri Pirko 137acb67442SJiri Pirko static inline 138acb67442SJiri Pirko void tcf_block_cb_incref(struct tcf_block_cb *block_cb) 139acb67442SJiri Pirko { 140acb67442SJiri Pirko } 141acb67442SJiri Pirko 142acb67442SJiri Pirko static inline 143acb67442SJiri Pirko unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb) 144acb67442SJiri Pirko { 145acb67442SJiri Pirko return 0; 146acb67442SJiri Pirko } 147acb67442SJiri Pirko 148acb67442SJiri Pirko static inline 149acb67442SJiri Pirko struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block, 150acb67442SJiri Pirko tc_setup_cb_t *cb, void *cb_ident, 151acb67442SJiri Pirko void *cb_priv) 152acb67442SJiri Pirko { 153acb67442SJiri Pirko return NULL; 154acb67442SJiri Pirko } 155acb67442SJiri Pirko 156acb67442SJiri Pirko static inline 157acb67442SJiri Pirko int tcf_block_cb_register(struct tcf_block *block, 158acb67442SJiri Pirko tc_setup_cb_t *cb, void *cb_ident, 159acb67442SJiri Pirko void *cb_priv) 160acb67442SJiri Pirko { 161acb67442SJiri Pirko return 0; 162acb67442SJiri Pirko } 163acb67442SJiri Pirko 164acb67442SJiri Pirko static inline 165acb67442SJiri Pirko void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb) 166acb67442SJiri Pirko { 167acb67442SJiri Pirko } 168acb67442SJiri Pirko 169acb67442SJiri Pirko static inline 170acb67442SJiri Pirko void tcf_block_cb_unregister(struct tcf_block *block, 171acb67442SJiri Pirko tc_setup_cb_t *cb, void *cb_ident) 172acb67442SJiri Pirko { 173acb67442SJiri Pirko } 174acb67442SJiri Pirko 17587d83093SJiri Pirko static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, 17687d83093SJiri Pirko struct tcf_result *res, bool compat_mode) 17787d83093SJiri Pirko { 17887d83093SJiri Pirko return TC_ACT_UNSPEC; 17987d83093SJiri Pirko } 1808ae70032SJiri Pirko #endif 181cf1facdaSJiri Pirko 1821da177e4SLinus Torvalds static inline unsigned long 1831da177e4SLinus Torvalds __cls_set_class(unsigned long *clp, unsigned long cl) 1841da177e4SLinus Torvalds { 185a0efb80cSWANG Cong return xchg(clp, cl); 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds static inline unsigned long 18934e3759cSJiri Pirko cls_set_class(struct Qdisc *q, unsigned long *clp, unsigned long cl) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds unsigned long old_cl; 1921da177e4SLinus Torvalds 19334e3759cSJiri Pirko sch_tree_lock(q); 1941da177e4SLinus Torvalds old_cl = __cls_set_class(clp, cl); 19534e3759cSJiri Pirko sch_tree_unlock(q); 1961da177e4SLinus Torvalds return old_cl; 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds static inline void 2001da177e4SLinus Torvalds tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base) 2011da177e4SLinus Torvalds { 20234e3759cSJiri Pirko struct Qdisc *q = tp->chain->block->q; 2031da177e4SLinus Torvalds unsigned long cl; 2041da177e4SLinus Torvalds 20534e3759cSJiri Pirko /* Check q as it is not set for shared blocks. In that case, 20634e3759cSJiri Pirko * setting class is not supported. 20734e3759cSJiri Pirko */ 20834e3759cSJiri Pirko if (!q) 20934e3759cSJiri Pirko return; 21034e3759cSJiri Pirko cl = q->ops->cl_ops->bind_tcf(q, base, r->classid); 21134e3759cSJiri Pirko cl = cls_set_class(q, &r->class, cl); 2121da177e4SLinus Torvalds if (cl) 21334e3759cSJiri Pirko q->ops->cl_ops->unbind_tcf(q, cl); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds static inline void 2171da177e4SLinus Torvalds tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r) 2181da177e4SLinus Torvalds { 21934e3759cSJiri Pirko struct Qdisc *q = tp->chain->block->q; 2201da177e4SLinus Torvalds unsigned long cl; 2211da177e4SLinus Torvalds 22234e3759cSJiri Pirko if (!q) 22334e3759cSJiri Pirko return; 2241da177e4SLinus Torvalds if ((cl = __cls_set_class(&r->class, 0)) != 0) 22534e3759cSJiri Pirko q->ops->cl_ops->unbind_tcf(q, cl); 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 228fd2c3ef7SEric Dumazet struct tcf_exts { 2291da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT 23033be6271SWANG Cong __u32 type; /* for backward compat(TCA_OLD_COMPAT) */ 23122dc13c8SWANG Cong int nr_actions; 23222dc13c8SWANG Cong struct tc_action **actions; 2331da177e4SLinus Torvalds #endif 2341da177e4SLinus Torvalds /* Map to export classifier specific extension TLV types to the 2351da177e4SLinus Torvalds * generic extensions API. Unsupported extensions must be set to 0. 2361da177e4SLinus Torvalds */ 2371da177e4SLinus Torvalds int action; 2381da177e4SLinus Torvalds int police; 2391da177e4SLinus Torvalds }; 2401da177e4SLinus Torvalds 241b9a24bb7SWANG Cong static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police) 24233be6271SWANG Cong { 24333be6271SWANG Cong #ifdef CONFIG_NET_CLS_ACT 2445da57f42SWANG Cong exts->type = 0; 24522dc13c8SWANG Cong exts->nr_actions = 0; 24622dc13c8SWANG Cong exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *), 24722dc13c8SWANG Cong GFP_KERNEL); 248b9a24bb7SWANG Cong if (!exts->actions) 249b9a24bb7SWANG Cong return -ENOMEM; 25033be6271SWANG Cong #endif 2515da57f42SWANG Cong exts->action = action; 2525da57f42SWANG Cong exts->police = police; 253b9a24bb7SWANG Cong return 0; 25433be6271SWANG Cong } 25533be6271SWANG Cong 25622dc13c8SWANG Cong static inline void tcf_exts_to_list(const struct tcf_exts *exts, 25722dc13c8SWANG Cong struct list_head *actions) 25822dc13c8SWANG Cong { 25922dc13c8SWANG Cong #ifdef CONFIG_NET_CLS_ACT 26022dc13c8SWANG Cong int i; 26122dc13c8SWANG Cong 26222dc13c8SWANG Cong for (i = 0; i < exts->nr_actions; i++) { 26322dc13c8SWANG Cong struct tc_action *a = exts->actions[i]; 26422dc13c8SWANG Cong 265fa5effe7SHadar Hen Zion list_add_tail(&a->list, actions); 26622dc13c8SWANG Cong } 26722dc13c8SWANG Cong #endif 26822dc13c8SWANG Cong } 26922dc13c8SWANG Cong 270d897a638SJakub Kicinski static inline void 271d897a638SJakub Kicinski tcf_exts_stats_update(const struct tcf_exts *exts, 272d897a638SJakub Kicinski u64 bytes, u64 packets, u64 lastuse) 273d897a638SJakub Kicinski { 274d897a638SJakub Kicinski #ifdef CONFIG_NET_CLS_ACT 275d897a638SJakub Kicinski int i; 276d897a638SJakub Kicinski 277d897a638SJakub Kicinski preempt_disable(); 278d897a638SJakub Kicinski 279d897a638SJakub Kicinski for (i = 0; i < exts->nr_actions; i++) { 280d897a638SJakub Kicinski struct tc_action *a = exts->actions[i]; 281d897a638SJakub Kicinski 282d897a638SJakub Kicinski tcf_action_stats_update(a, bytes, packets, lastuse); 283d897a638SJakub Kicinski } 284d897a638SJakub Kicinski 285d897a638SJakub Kicinski preempt_enable(); 286d897a638SJakub Kicinski #endif 287d897a638SJakub Kicinski } 288d897a638SJakub Kicinski 2891da177e4SLinus Torvalds /** 2903bcc0cecSJiri Pirko * tcf_exts_has_actions - check if at least one action is present 2913bcc0cecSJiri Pirko * @exts: tc filter extensions handle 2923bcc0cecSJiri Pirko * 2933bcc0cecSJiri Pirko * Returns true if at least one action is present. 2943bcc0cecSJiri Pirko */ 2953bcc0cecSJiri Pirko static inline bool tcf_exts_has_actions(struct tcf_exts *exts) 2963bcc0cecSJiri Pirko { 2972734437eSWANG Cong #ifdef CONFIG_NET_CLS_ACT 2983bcc0cecSJiri Pirko return exts->nr_actions; 2993bcc0cecSJiri Pirko #else 3003bcc0cecSJiri Pirko return false; 3013bcc0cecSJiri Pirko #endif 3023bcc0cecSJiri Pirko } 3032734437eSWANG Cong 3043bcc0cecSJiri Pirko /** 3053bcc0cecSJiri Pirko * tcf_exts_has_one_action - check if exactly one action is present 3063bcc0cecSJiri Pirko * @exts: tc filter extensions handle 3073bcc0cecSJiri Pirko * 3083bcc0cecSJiri Pirko * Returns true if exactly one action is present. 3093bcc0cecSJiri Pirko */ 3103bcc0cecSJiri Pirko static inline bool tcf_exts_has_one_action(struct tcf_exts *exts) 3113bcc0cecSJiri Pirko { 3123bcc0cecSJiri Pirko #ifdef CONFIG_NET_CLS_ACT 3133bcc0cecSJiri Pirko return exts->nr_actions == 1; 3143bcc0cecSJiri Pirko #else 3153bcc0cecSJiri Pirko return false; 3163bcc0cecSJiri Pirko #endif 3173bcc0cecSJiri Pirko } 3182734437eSWANG Cong 319af69afc5SJiri Pirko /** 320af69afc5SJiri Pirko * tcf_exts_exec - execute tc filter extensions 321af69afc5SJiri Pirko * @skb: socket buffer 322af69afc5SJiri Pirko * @exts: tc filter extensions handle 323af69afc5SJiri Pirko * @res: desired result 324af69afc5SJiri Pirko * 325af089e70SJiri Pirko * Executes all configured extensions. Returns TC_ACT_OK on a normal execution, 326af69afc5SJiri Pirko * a negative number if the filter must be considered unmatched or 327af69afc5SJiri Pirko * a positive action code (TC_ACT_*) which must be returned to the 328af69afc5SJiri Pirko * underlying layer. 329af69afc5SJiri Pirko */ 330af69afc5SJiri Pirko static inline int 331af69afc5SJiri Pirko tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts, 332af69afc5SJiri Pirko struct tcf_result *res) 333af69afc5SJiri Pirko { 334af69afc5SJiri Pirko #ifdef CONFIG_NET_CLS_ACT 335ec1a9ccaSJiri Pirko return tcf_action_exec(skb, exts->actions, exts->nr_actions, res); 336af69afc5SJiri Pirko #endif 337af089e70SJiri Pirko return TC_ACT_OK; 338af69afc5SJiri Pirko } 339af69afc5SJiri Pirko 3405c15257fSJoe Perches int tcf_exts_validate(struct net *net, struct tcf_proto *tp, 341c1b52739SBenjamin LaHaise struct nlattr **tb, struct nlattr *rate_tlv, 3422f7ef2f8SCong Wang struct tcf_exts *exts, bool ovr); 34318d0264fSWANG Cong void tcf_exts_destroy(struct tcf_exts *exts); 3449b0d4446SJiri Pirko void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src); 3455da57f42SWANG Cong int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts); 3465da57f42SWANG Cong int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts); 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds /** 3491da177e4SLinus Torvalds * struct tcf_pkt_info - packet information 3501da177e4SLinus Torvalds */ 351fd2c3ef7SEric Dumazet struct tcf_pkt_info { 3521da177e4SLinus Torvalds unsigned char * ptr; 3531da177e4SLinus Torvalds int nexthdr; 3541da177e4SLinus Torvalds }; 3551da177e4SLinus Torvalds 3561da177e4SLinus Torvalds #ifdef CONFIG_NET_EMATCH 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds struct tcf_ematch_ops; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds /** 3611da177e4SLinus Torvalds * struct tcf_ematch - extended match (ematch) 3621da177e4SLinus Torvalds * 3631da177e4SLinus Torvalds * @matchid: identifier to allow userspace to reidentify a match 3641da177e4SLinus Torvalds * @flags: flags specifying attributes and the relation to other matches 3651da177e4SLinus Torvalds * @ops: the operations lookup table of the corresponding ematch module 3661da177e4SLinus Torvalds * @datalen: length of the ematch specific configuration data 3671da177e4SLinus Torvalds * @data: ematch specific data 3681da177e4SLinus Torvalds */ 369fd2c3ef7SEric Dumazet struct tcf_ematch { 3701da177e4SLinus Torvalds struct tcf_ematch_ops * ops; 3711da177e4SLinus Torvalds unsigned long data; 3721da177e4SLinus Torvalds unsigned int datalen; 3731da177e4SLinus Torvalds u16 matchid; 3741da177e4SLinus Torvalds u16 flags; 37582a470f1SJohn Fastabend struct net *net; 3761da177e4SLinus Torvalds }; 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds static inline int tcf_em_is_container(struct tcf_ematch *em) 3791da177e4SLinus Torvalds { 3801da177e4SLinus Torvalds return !em->ops; 3811da177e4SLinus Torvalds } 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds static inline int tcf_em_is_simple(struct tcf_ematch *em) 3841da177e4SLinus Torvalds { 3851da177e4SLinus Torvalds return em->flags & TCF_EM_SIMPLE; 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds static inline int tcf_em_is_inverted(struct tcf_ematch *em) 3891da177e4SLinus Torvalds { 3901da177e4SLinus Torvalds return em->flags & TCF_EM_INVERT; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds static inline int tcf_em_last_match(struct tcf_ematch *em) 3941da177e4SLinus Torvalds { 3951da177e4SLinus Torvalds return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds static inline int tcf_em_early_end(struct tcf_ematch *em, int result) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds if (tcf_em_last_match(em)) 4011da177e4SLinus Torvalds return 1; 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds if (result == 0 && em->flags & TCF_EM_REL_AND) 4041da177e4SLinus Torvalds return 1; 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds if (result != 0 && em->flags & TCF_EM_REL_OR) 4071da177e4SLinus Torvalds return 1; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds return 0; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds /** 4131da177e4SLinus Torvalds * struct tcf_ematch_tree - ematch tree handle 4141da177e4SLinus Torvalds * 4151da177e4SLinus Torvalds * @hdr: ematch tree header supplied by userspace 4161da177e4SLinus Torvalds * @matches: array of ematches 4171da177e4SLinus Torvalds */ 418fd2c3ef7SEric Dumazet struct tcf_ematch_tree { 4191da177e4SLinus Torvalds struct tcf_ematch_tree_hdr hdr; 4201da177e4SLinus Torvalds struct tcf_ematch * matches; 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds }; 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds /** 4251da177e4SLinus Torvalds * struct tcf_ematch_ops - ematch module operations 4261da177e4SLinus Torvalds * 4271da177e4SLinus Torvalds * @kind: identifier (kind) of this ematch module 4281da177e4SLinus Torvalds * @datalen: length of expected configuration data (optional) 4291da177e4SLinus Torvalds * @change: called during validation (optional) 4301da177e4SLinus Torvalds * @match: called during ematch tree evaluation, must return 1/0 4311da177e4SLinus Torvalds * @destroy: called during destroyage (optional) 4321da177e4SLinus Torvalds * @dump: called during dumping process (optional) 4331da177e4SLinus Torvalds * @owner: owner, must be set to THIS_MODULE 4341da177e4SLinus Torvalds * @link: link to previous/next ematch module (internal use) 4351da177e4SLinus Torvalds */ 436fd2c3ef7SEric Dumazet struct tcf_ematch_ops { 4371da177e4SLinus Torvalds int kind; 4381da177e4SLinus Torvalds int datalen; 43982a470f1SJohn Fastabend int (*change)(struct net *net, void *, 4401da177e4SLinus Torvalds int, struct tcf_ematch *); 4411da177e4SLinus Torvalds int (*match)(struct sk_buff *, struct tcf_ematch *, 4421da177e4SLinus Torvalds struct tcf_pkt_info *); 44382a470f1SJohn Fastabend void (*destroy)(struct tcf_ematch *); 4441da177e4SLinus Torvalds int (*dump)(struct sk_buff *, struct tcf_ematch *); 4451da177e4SLinus Torvalds struct module *owner; 4461da177e4SLinus Torvalds struct list_head link; 4471da177e4SLinus Torvalds }; 4481da177e4SLinus Torvalds 4495c15257fSJoe Perches int tcf_em_register(struct tcf_ematch_ops *); 4505c15257fSJoe Perches void tcf_em_unregister(struct tcf_ematch_ops *); 4515c15257fSJoe Perches int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *, 4521da177e4SLinus Torvalds struct tcf_ematch_tree *); 45382a470f1SJohn Fastabend void tcf_em_tree_destroy(struct tcf_ematch_tree *); 4545c15257fSJoe Perches int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int); 4555c15257fSJoe Perches int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *, 4561da177e4SLinus Torvalds struct tcf_pkt_info *); 4571da177e4SLinus Torvalds 4581da177e4SLinus Torvalds /** 4591da177e4SLinus Torvalds * tcf_em_tree_match - evaulate an ematch tree 4601da177e4SLinus Torvalds * 4611da177e4SLinus Torvalds * @skb: socket buffer of the packet in question 4621da177e4SLinus Torvalds * @tree: ematch tree to be used for evaluation 4631da177e4SLinus Torvalds * @info: packet information examined by classifier 4641da177e4SLinus Torvalds * 4651da177e4SLinus Torvalds * This function matches @skb against the ematch tree in @tree by going 4661da177e4SLinus Torvalds * through all ematches respecting their logic relations returning 4671da177e4SLinus Torvalds * as soon as the result is obvious. 4681da177e4SLinus Torvalds * 4691da177e4SLinus Torvalds * Returns 1 if the ematch tree as-one matches, no ematches are configured 4701da177e4SLinus Torvalds * or ematch is not enabled in the kernel, otherwise 0 is returned. 4711da177e4SLinus Torvalds */ 4721da177e4SLinus Torvalds static inline int tcf_em_tree_match(struct sk_buff *skb, 4731da177e4SLinus Torvalds struct tcf_ematch_tree *tree, 4741da177e4SLinus Torvalds struct tcf_pkt_info *info) 4751da177e4SLinus Torvalds { 4761da177e4SLinus Torvalds if (tree->hdr.nmatches) 4771da177e4SLinus Torvalds return __tcf_em_tree_match(skb, tree, info); 4781da177e4SLinus Torvalds else 4791da177e4SLinus Torvalds return 1; 4801da177e4SLinus Torvalds } 4811da177e4SLinus Torvalds 482db3d99c0SPatrick McHardy #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind)) 483db3d99c0SPatrick McHardy 4841da177e4SLinus Torvalds #else /* CONFIG_NET_EMATCH */ 4851da177e4SLinus Torvalds 486fd2c3ef7SEric Dumazet struct tcf_ematch_tree { 4871da177e4SLinus Torvalds }; 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0) 49082a470f1SJohn Fastabend #define tcf_em_tree_destroy(t) do { (void)(t); } while(0) 4911da177e4SLinus Torvalds #define tcf_em_tree_dump(skb, t, tlv) (0) 4921da177e4SLinus Torvalds #define tcf_em_tree_match(skb, t, info) ((void)(info), 1) 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds #endif /* CONFIG_NET_EMATCH */ 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer) 4971da177e4SLinus Torvalds { 4981da177e4SLinus Torvalds switch (layer) { 4991da177e4SLinus Torvalds case TCF_LAYER_LINK: 5001da177e4SLinus Torvalds return skb->data; 5011da177e4SLinus Torvalds case TCF_LAYER_NETWORK: 502d56f90a7SArnaldo Carvalho de Melo return skb_network_header(skb); 5031da177e4SLinus Torvalds case TCF_LAYER_TRANSPORT: 5049c70220bSArnaldo Carvalho de Melo return skb_transport_header(skb); 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds return NULL; 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds 510eddc9ec5SArnaldo Carvalho de Melo static inline int tcf_valid_offset(const struct sk_buff *skb, 511eddc9ec5SArnaldo Carvalho de Melo const unsigned char *ptr, const int len) 5121da177e4SLinus Torvalds { 513da521b2cSDavid S. Miller return likely((ptr + len) <= skb_tail_pointer(skb) && 514da521b2cSDavid S. Miller ptr >= skb->head && 515da521b2cSDavid S. Miller (ptr <= (ptr + len))); 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_IND 5190eeb8ffcSDenis V. Lunev #include <net/net_namespace.h> 5200eeb8ffcSDenis V. Lunev 5211da177e4SLinus Torvalds static inline int 5222519a602SWANG Cong tcf_change_indev(struct net *net, struct nlattr *indev_tlv) 5231da177e4SLinus Torvalds { 5242519a602SWANG Cong char indev[IFNAMSIZ]; 525c01003c2SPatrick McHardy struct net_device *dev; 526c01003c2SPatrick McHardy 5272519a602SWANG Cong if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ) 5282519a602SWANG Cong return -EINVAL; 5292519a602SWANG Cong dev = __dev_get_by_name(net, indev); 5302519a602SWANG Cong if (!dev) 5312519a602SWANG Cong return -ENODEV; 5322519a602SWANG Cong return dev->ifindex; 5331da177e4SLinus Torvalds } 5341da177e4SLinus Torvalds 5352519a602SWANG Cong static inline bool 5362519a602SWANG Cong tcf_match_indev(struct sk_buff *skb, int ifindex) 5372519a602SWANG Cong { 5382519a602SWANG Cong if (!ifindex) 5392519a602SWANG Cong return true; 5402519a602SWANG Cong if (!skb->skb_iif) 5412519a602SWANG Cong return false; 5422519a602SWANG Cong return ifindex == skb->skb_iif; 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds #endif /* CONFIG_NET_CLS_IND */ 5451da177e4SLinus Torvalds 546208c0f4bSJiri Pirko int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts, 547208c0f4bSJiri Pirko enum tc_setup_type type, void *type_data, bool err_stop); 548717503b9SJiri Pirko 5498c4083b3SJiri Pirko enum tc_block_command { 5508c4083b3SJiri Pirko TC_BLOCK_BIND, 5518c4083b3SJiri Pirko TC_BLOCK_UNBIND, 5528c4083b3SJiri Pirko }; 5538c4083b3SJiri Pirko 5548c4083b3SJiri Pirko struct tc_block_offload { 5558c4083b3SJiri Pirko enum tc_block_command command; 5568c4083b3SJiri Pirko enum tcf_block_binder_type binder_type; 5578c4083b3SJiri Pirko struct tcf_block *block; 5588c4083b3SJiri Pirko }; 5598c4083b3SJiri Pirko 5605fd9fc4eSJiri Pirko struct tc_cls_common_offload { 5615fd9fc4eSJiri Pirko u32 chain_index; 5625fd9fc4eSJiri Pirko __be16 protocol; 563d7c1c8d2SJiri Pirko u32 prio; 5645fd9fc4eSJiri Pirko }; 5655fd9fc4eSJiri Pirko 5665fd9fc4eSJiri Pirko static inline void 5675fd9fc4eSJiri Pirko tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common, 5685fd9fc4eSJiri Pirko const struct tcf_proto *tp) 5695fd9fc4eSJiri Pirko { 5705fd9fc4eSJiri Pirko cls_common->chain_index = tp->chain->index; 5715fd9fc4eSJiri Pirko cls_common->protocol = tp->protocol; 572d7c1c8d2SJiri Pirko cls_common->prio = tp->prio; 5735fd9fc4eSJiri Pirko } 5745fd9fc4eSJiri Pirko 575a1b7c5fdSJohn Fastabend struct tc_cls_u32_knode { 576a1b7c5fdSJohn Fastabend struct tcf_exts *exts; 577e014860eSJohn Fastabend struct tc_u32_sel *sel; 578a1b7c5fdSJohn Fastabend u32 handle; 579a1b7c5fdSJohn Fastabend u32 val; 580a1b7c5fdSJohn Fastabend u32 mask; 581a1b7c5fdSJohn Fastabend u32 link_handle; 582e014860eSJohn Fastabend u8 fshift; 583a1b7c5fdSJohn Fastabend }; 584a1b7c5fdSJohn Fastabend 585a1b7c5fdSJohn Fastabend struct tc_cls_u32_hnode { 586a1b7c5fdSJohn Fastabend u32 handle; 587a1b7c5fdSJohn Fastabend u32 prio; 588a1b7c5fdSJohn Fastabend unsigned int divisor; 589a1b7c5fdSJohn Fastabend }; 590a1b7c5fdSJohn Fastabend 591a1b7c5fdSJohn Fastabend enum tc_clsu32_command { 592a1b7c5fdSJohn Fastabend TC_CLSU32_NEW_KNODE, 593a1b7c5fdSJohn Fastabend TC_CLSU32_REPLACE_KNODE, 594a1b7c5fdSJohn Fastabend TC_CLSU32_DELETE_KNODE, 595a1b7c5fdSJohn Fastabend TC_CLSU32_NEW_HNODE, 596a1b7c5fdSJohn Fastabend TC_CLSU32_REPLACE_HNODE, 597a1b7c5fdSJohn Fastabend TC_CLSU32_DELETE_HNODE, 598a1b7c5fdSJohn Fastabend }; 599a1b7c5fdSJohn Fastabend 600a1b7c5fdSJohn Fastabend struct tc_cls_u32_offload { 6015fd9fc4eSJiri Pirko struct tc_cls_common_offload common; 602a1b7c5fdSJohn Fastabend /* knode values */ 603a1b7c5fdSJohn Fastabend enum tc_clsu32_command command; 604a1b7c5fdSJohn Fastabend union { 605a1b7c5fdSJohn Fastabend struct tc_cls_u32_knode knode; 606a1b7c5fdSJohn Fastabend struct tc_cls_u32_hnode hnode; 607a1b7c5fdSJohn Fastabend }; 608a1b7c5fdSJohn Fastabend }; 609a1b7c5fdSJohn Fastabend 6107b06e8aeSJiri Pirko static inline bool tc_can_offload(const struct net_device *dev) 6116843e7a2SJohn Fastabend { 61270b5aee4SJiri Pirko return dev->features & NETIF_F_HW_TC; 6136843e7a2SJohn Fastabend } 6146843e7a2SJohn Fastabend 61555330f05SHadar Hen Zion static inline bool tc_skip_hw(u32 flags) 61655330f05SHadar Hen Zion { 61755330f05SHadar Hen Zion return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false; 61855330f05SHadar Hen Zion } 61955330f05SHadar Hen Zion 620d34e3e18SSamudrala, Sridhar static inline bool tc_skip_sw(u32 flags) 621d34e3e18SSamudrala, Sridhar { 622d34e3e18SSamudrala, Sridhar return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false; 623d34e3e18SSamudrala, Sridhar } 624d34e3e18SSamudrala, Sridhar 625d34e3e18SSamudrala, Sridhar /* SKIP_HW and SKIP_SW are mutually exclusive flags. */ 626d34e3e18SSamudrala, Sridhar static inline bool tc_flags_valid(u32 flags) 627d34e3e18SSamudrala, Sridhar { 628d34e3e18SSamudrala, Sridhar if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)) 629d34e3e18SSamudrala, Sridhar return false; 630d34e3e18SSamudrala, Sridhar 631d34e3e18SSamudrala, Sridhar if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))) 632d34e3e18SSamudrala, Sridhar return false; 633d34e3e18SSamudrala, Sridhar 634d34e3e18SSamudrala, Sridhar return true; 635d34e3e18SSamudrala, Sridhar } 636d34e3e18SSamudrala, Sridhar 637e696028aSOr Gerlitz static inline bool tc_in_hw(u32 flags) 638e696028aSOr Gerlitz { 639e696028aSOr Gerlitz return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false; 640e696028aSOr Gerlitz } 641e696028aSOr Gerlitz 6425b33f488SAmir Vadai enum tc_fl_command { 6435b33f488SAmir Vadai TC_CLSFLOWER_REPLACE, 6445b33f488SAmir Vadai TC_CLSFLOWER_DESTROY, 64510cbc684SAmir Vadai TC_CLSFLOWER_STATS, 6465b33f488SAmir Vadai }; 6475b33f488SAmir Vadai 6485b33f488SAmir Vadai struct tc_cls_flower_offload { 6495fd9fc4eSJiri Pirko struct tc_cls_common_offload common; 6505b33f488SAmir Vadai enum tc_fl_command command; 6518208d21bSAmir Vadai unsigned long cookie; 6525b33f488SAmir Vadai struct flow_dissector *dissector; 6535b33f488SAmir Vadai struct fl_flow_key *mask; 6545b33f488SAmir Vadai struct fl_flow_key *key; 6555b33f488SAmir Vadai struct tcf_exts *exts; 656384c181eSAmritha Nambiar u32 classid; 6575b33f488SAmir Vadai }; 6585b33f488SAmir Vadai 659b87f7936SYotam Gigi enum tc_matchall_command { 660b87f7936SYotam Gigi TC_CLSMATCHALL_REPLACE, 661b87f7936SYotam Gigi TC_CLSMATCHALL_DESTROY, 662b87f7936SYotam Gigi }; 663b87f7936SYotam Gigi 664b87f7936SYotam Gigi struct tc_cls_matchall_offload { 6655fd9fc4eSJiri Pirko struct tc_cls_common_offload common; 666b87f7936SYotam Gigi enum tc_matchall_command command; 667b87f7936SYotam Gigi struct tcf_exts *exts; 668b87f7936SYotam Gigi unsigned long cookie; 669b87f7936SYotam Gigi }; 670b87f7936SYotam Gigi 671332ae8e2SJakub Kicinski enum tc_clsbpf_command { 672332ae8e2SJakub Kicinski TC_CLSBPF_ADD, 673332ae8e2SJakub Kicinski TC_CLSBPF_REPLACE, 674332ae8e2SJakub Kicinski TC_CLSBPF_DESTROY, 67568d64063SJakub Kicinski TC_CLSBPF_STATS, 676332ae8e2SJakub Kicinski }; 677332ae8e2SJakub Kicinski 678332ae8e2SJakub Kicinski struct tc_cls_bpf_offload { 6795fd9fc4eSJiri Pirko struct tc_cls_common_offload common; 680332ae8e2SJakub Kicinski enum tc_clsbpf_command command; 681332ae8e2SJakub Kicinski struct tcf_exts *exts; 682332ae8e2SJakub Kicinski struct bpf_prog *prog; 683332ae8e2SJakub Kicinski const char *name; 684332ae8e2SJakub Kicinski bool exts_integrated; 6850d01d45fSJakub Kicinski u32 gen_flags; 686332ae8e2SJakub Kicinski }; 687332ae8e2SJakub Kicinski 6884e8b86c0SAmritha Nambiar struct tc_mqprio_qopt_offload { 6894e8b86c0SAmritha Nambiar /* struct tc_mqprio_qopt must always be the first element */ 6904e8b86c0SAmritha Nambiar struct tc_mqprio_qopt qopt; 6914e8b86c0SAmritha Nambiar u16 mode; 6924e8b86c0SAmritha Nambiar u16 shaper; 6934e8b86c0SAmritha Nambiar u32 flags; 6944e8b86c0SAmritha Nambiar u64 min_rate[TC_QOPT_MAX_QUEUE]; 6954e8b86c0SAmritha Nambiar u64 max_rate[TC_QOPT_MAX_QUEUE]; 6964e8b86c0SAmritha Nambiar }; 6971045ba77SJamal Hadi Salim 6981045ba77SJamal Hadi Salim /* This structure holds cookie structure that is passed from user 6991045ba77SJamal Hadi Salim * to the kernel for actions and classifiers 7001045ba77SJamal Hadi Salim */ 7011045ba77SJamal Hadi Salim struct tc_cookie { 7021045ba77SJamal Hadi Salim u8 *data; 7031045ba77SJamal Hadi Salim u32 len; 7041045ba77SJamal Hadi Salim }; 7051da177e4SLinus Torvalds #endif 706