xref: /openbmc/linux/include/net/pkt_cls.h (revision e4b95c41)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
21da177e4SLinus Torvalds #ifndef __NET_PKT_CLS_H
31da177e4SLinus Torvalds #define __NET_PKT_CLS_H
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/pkt_cls.h>
67aa0045dSCong Wang #include <linux/workqueue.h>
71da177e4SLinus Torvalds #include <net/sch_generic.h>
81da177e4SLinus Torvalds #include <net/act_api.h>
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds /* Basic packet classifier frontend definitions. */
111da177e4SLinus Torvalds 
12fd2c3ef7SEric Dumazet struct tcf_walker {
131da177e4SLinus Torvalds 	int	stop;
141da177e4SLinus Torvalds 	int	skip;
151da177e4SLinus Torvalds 	int	count;
168113c095SWANG Cong 	int	(*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
171da177e4SLinus Torvalds };
181da177e4SLinus Torvalds 
195c15257fSJoe Perches int register_tcf_proto_ops(struct tcf_proto_ops *ops);
205c15257fSJoe Perches int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
211da177e4SLinus Torvalds 
227aa0045dSCong Wang bool tcf_queue_work(struct work_struct *work);
237aa0045dSCong Wang 
248ae70032SJiri Pirko #ifdef CONFIG_NET_CLS
25367a8ce8SWANG Cong struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
26367a8ce8SWANG Cong 				bool create);
275bc17018SJiri Pirko void tcf_chain_put(struct tcf_chain *chain);
286529eabaSJiri Pirko int tcf_block_get(struct tcf_block **p_block,
296529eabaSJiri Pirko 		  struct tcf_proto __rcu **p_filter_chain);
306529eabaSJiri Pirko void tcf_block_put(struct tcf_block *block);
3187d83093SJiri Pirko int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
3287d83093SJiri Pirko 		 struct tcf_result *res, bool compat_mode);
3387d83093SJiri Pirko 
348ae70032SJiri Pirko #else
356529eabaSJiri Pirko static inline
366529eabaSJiri Pirko int tcf_block_get(struct tcf_block **p_block,
376529eabaSJiri Pirko 		  struct tcf_proto __rcu **p_filter_chain)
386529eabaSJiri Pirko {
396529eabaSJiri Pirko 	return 0;
406529eabaSJiri Pirko }
416529eabaSJiri Pirko 
426529eabaSJiri Pirko static inline void tcf_block_put(struct tcf_block *block)
438ae70032SJiri Pirko {
448ae70032SJiri Pirko }
4587d83093SJiri Pirko 
4687d83093SJiri Pirko static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
4787d83093SJiri Pirko 			       struct tcf_result *res, bool compat_mode)
4887d83093SJiri Pirko {
4987d83093SJiri Pirko 	return TC_ACT_UNSPEC;
5087d83093SJiri Pirko }
518ae70032SJiri Pirko #endif
52cf1facdaSJiri Pirko 
531da177e4SLinus Torvalds static inline unsigned long
541da177e4SLinus Torvalds __cls_set_class(unsigned long *clp, unsigned long cl)
551da177e4SLinus Torvalds {
56a0efb80cSWANG Cong 	return xchg(clp, cl);
571da177e4SLinus Torvalds }
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds static inline unsigned long
601da177e4SLinus Torvalds cls_set_class(struct tcf_proto *tp, unsigned long *clp,
611da177e4SLinus Torvalds 	unsigned long cl)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	unsigned long old_cl;
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	tcf_tree_lock(tp);
661da177e4SLinus Torvalds 	old_cl = __cls_set_class(clp, cl);
671da177e4SLinus Torvalds 	tcf_tree_unlock(tp);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	return old_cl;
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds static inline void
731da177e4SLinus Torvalds tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	unsigned long cl;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
781da177e4SLinus Torvalds 	cl = cls_set_class(tp, &r->class, cl);
791da177e4SLinus Torvalds 	if (cl)
801da177e4SLinus Torvalds 		tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
811da177e4SLinus Torvalds }
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds static inline void
841da177e4SLinus Torvalds tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	unsigned long cl;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	if ((cl = __cls_set_class(&r->class, 0)) != 0)
891da177e4SLinus Torvalds 		tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
901da177e4SLinus Torvalds }
911da177e4SLinus Torvalds 
92fd2c3ef7SEric Dumazet struct tcf_exts {
931da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
9433be6271SWANG Cong 	__u32	type; /* for backward compat(TCA_OLD_COMPAT) */
9522dc13c8SWANG Cong 	int nr_actions;
9622dc13c8SWANG Cong 	struct tc_action **actions;
97e4b95c41SCong Wang 	struct net *net;
981da177e4SLinus Torvalds #endif
991da177e4SLinus Torvalds 	/* Map to export classifier specific extension TLV types to the
1001da177e4SLinus Torvalds 	 * generic extensions API. Unsupported extensions must be set to 0.
1011da177e4SLinus Torvalds 	 */
1021da177e4SLinus Torvalds 	int action;
1031da177e4SLinus Torvalds 	int police;
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds 
106b9a24bb7SWANG Cong static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
10733be6271SWANG Cong {
10833be6271SWANG Cong #ifdef CONFIG_NET_CLS_ACT
1095da57f42SWANG Cong 	exts->type = 0;
11022dc13c8SWANG Cong 	exts->nr_actions = 0;
111e4b95c41SCong Wang 	exts->net = NULL;
11222dc13c8SWANG Cong 	exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
11322dc13c8SWANG Cong 				GFP_KERNEL);
114b9a24bb7SWANG Cong 	if (!exts->actions)
115b9a24bb7SWANG Cong 		return -ENOMEM;
11633be6271SWANG Cong #endif
1175da57f42SWANG Cong 	exts->action = action;
1185da57f42SWANG Cong 	exts->police = police;
119b9a24bb7SWANG Cong 	return 0;
12033be6271SWANG Cong }
12133be6271SWANG Cong 
122e4b95c41SCong Wang /* Return false if the netns is being destroyed in cleanup_net(). Callers
123e4b95c41SCong Wang  * need to do cleanup synchronously in this case, otherwise may race with
124e4b95c41SCong Wang  * tc_action_net_exit(). Return true for other cases.
125e4b95c41SCong Wang  */
126e4b95c41SCong Wang static inline bool tcf_exts_get_net(struct tcf_exts *exts)
127e4b95c41SCong Wang {
128e4b95c41SCong Wang #ifdef CONFIG_NET_CLS_ACT
129e4b95c41SCong Wang 	exts->net = maybe_get_net(exts->net);
130e4b95c41SCong Wang 	return exts->net != NULL;
131e4b95c41SCong Wang #else
132e4b95c41SCong Wang 	return true;
133e4b95c41SCong Wang #endif
134e4b95c41SCong Wang }
135e4b95c41SCong Wang 
136e4b95c41SCong Wang static inline void tcf_exts_put_net(struct tcf_exts *exts)
137e4b95c41SCong Wang {
138e4b95c41SCong Wang #ifdef CONFIG_NET_CLS_ACT
139e4b95c41SCong Wang 	if (exts->net)
140e4b95c41SCong Wang 		put_net(exts->net);
141e4b95c41SCong Wang #endif
142e4b95c41SCong Wang }
143e4b95c41SCong Wang 
14422dc13c8SWANG Cong static inline void tcf_exts_to_list(const struct tcf_exts *exts,
14522dc13c8SWANG Cong 				    struct list_head *actions)
14622dc13c8SWANG Cong {
14722dc13c8SWANG Cong #ifdef CONFIG_NET_CLS_ACT
14822dc13c8SWANG Cong 	int i;
14922dc13c8SWANG Cong 
15022dc13c8SWANG Cong 	for (i = 0; i < exts->nr_actions; i++) {
15122dc13c8SWANG Cong 		struct tc_action *a = exts->actions[i];
15222dc13c8SWANG Cong 
153fa5effe7SHadar Hen Zion 		list_add_tail(&a->list, actions);
15422dc13c8SWANG Cong 	}
15522dc13c8SWANG Cong #endif
15622dc13c8SWANG Cong }
15722dc13c8SWANG Cong 
158d897a638SJakub Kicinski static inline void
159d897a638SJakub Kicinski tcf_exts_stats_update(const struct tcf_exts *exts,
160d897a638SJakub Kicinski 		      u64 bytes, u64 packets, u64 lastuse)
161d897a638SJakub Kicinski {
162d897a638SJakub Kicinski #ifdef CONFIG_NET_CLS_ACT
163d897a638SJakub Kicinski 	int i;
164d897a638SJakub Kicinski 
165d897a638SJakub Kicinski 	preempt_disable();
166d897a638SJakub Kicinski 
167d897a638SJakub Kicinski 	for (i = 0; i < exts->nr_actions; i++) {
168d897a638SJakub Kicinski 		struct tc_action *a = exts->actions[i];
169d897a638SJakub Kicinski 
170d897a638SJakub Kicinski 		tcf_action_stats_update(a, bytes, packets, lastuse);
171d897a638SJakub Kicinski 	}
172d897a638SJakub Kicinski 
173d897a638SJakub Kicinski 	preempt_enable();
174d897a638SJakub Kicinski #endif
175d897a638SJakub Kicinski }
176d897a638SJakub Kicinski 
1771da177e4SLinus Torvalds /**
1783bcc0cecSJiri Pirko  * tcf_exts_has_actions - check if at least one action is present
1793bcc0cecSJiri Pirko  * @exts: tc filter extensions handle
1803bcc0cecSJiri Pirko  *
1813bcc0cecSJiri Pirko  * Returns true if at least one action is present.
1823bcc0cecSJiri Pirko  */
1833bcc0cecSJiri Pirko static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
1843bcc0cecSJiri Pirko {
1852734437eSWANG Cong #ifdef CONFIG_NET_CLS_ACT
1863bcc0cecSJiri Pirko 	return exts->nr_actions;
1873bcc0cecSJiri Pirko #else
1883bcc0cecSJiri Pirko 	return false;
1893bcc0cecSJiri Pirko #endif
1903bcc0cecSJiri Pirko }
1912734437eSWANG Cong 
1923bcc0cecSJiri Pirko /**
1933bcc0cecSJiri Pirko  * tcf_exts_has_one_action - check if exactly one action is present
1943bcc0cecSJiri Pirko  * @exts: tc filter extensions handle
1953bcc0cecSJiri Pirko  *
1963bcc0cecSJiri Pirko  * Returns true if exactly one action is present.
1973bcc0cecSJiri Pirko  */
1983bcc0cecSJiri Pirko static inline bool tcf_exts_has_one_action(struct tcf_exts *exts)
1993bcc0cecSJiri Pirko {
2003bcc0cecSJiri Pirko #ifdef CONFIG_NET_CLS_ACT
2013bcc0cecSJiri Pirko 	return exts->nr_actions == 1;
2023bcc0cecSJiri Pirko #else
2033bcc0cecSJiri Pirko 	return false;
2043bcc0cecSJiri Pirko #endif
2053bcc0cecSJiri Pirko }
2062734437eSWANG Cong 
207af69afc5SJiri Pirko /**
208af69afc5SJiri Pirko  * tcf_exts_exec - execute tc filter extensions
209af69afc5SJiri Pirko  * @skb: socket buffer
210af69afc5SJiri Pirko  * @exts: tc filter extensions handle
211af69afc5SJiri Pirko  * @res: desired result
212af69afc5SJiri Pirko  *
213af089e70SJiri Pirko  * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
214af69afc5SJiri Pirko  * a negative number if the filter must be considered unmatched or
215af69afc5SJiri Pirko  * a positive action code (TC_ACT_*) which must be returned to the
216af69afc5SJiri Pirko  * underlying layer.
217af69afc5SJiri Pirko  */
218af69afc5SJiri Pirko static inline int
219af69afc5SJiri Pirko tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
220af69afc5SJiri Pirko 	      struct tcf_result *res)
221af69afc5SJiri Pirko {
222af69afc5SJiri Pirko #ifdef CONFIG_NET_CLS_ACT
223ec1a9ccaSJiri Pirko 	return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
224af69afc5SJiri Pirko #endif
225af089e70SJiri Pirko 	return TC_ACT_OK;
226af69afc5SJiri Pirko }
227af69afc5SJiri Pirko 
2285c15257fSJoe Perches int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
229c1b52739SBenjamin LaHaise 		      struct nlattr **tb, struct nlattr *rate_tlv,
2302f7ef2f8SCong Wang 		      struct tcf_exts *exts, bool ovr);
23118d0264fSWANG Cong void tcf_exts_destroy(struct tcf_exts *exts);
2329b0d4446SJiri Pirko void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
2335da57f42SWANG Cong int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
2345da57f42SWANG Cong int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
2357091d8c7SHadar Hen Zion int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
2367091d8c7SHadar Hen Zion 		     struct net_device **hw_dev);
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds /**
2391da177e4SLinus Torvalds  * struct tcf_pkt_info - packet information
2401da177e4SLinus Torvalds  */
241fd2c3ef7SEric Dumazet struct tcf_pkt_info {
2421da177e4SLinus Torvalds 	unsigned char *		ptr;
2431da177e4SLinus Torvalds 	int			nexthdr;
2441da177e4SLinus Torvalds };
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds #ifdef CONFIG_NET_EMATCH
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds struct tcf_ematch_ops;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds /**
2511da177e4SLinus Torvalds  * struct tcf_ematch - extended match (ematch)
2521da177e4SLinus Torvalds  *
2531da177e4SLinus Torvalds  * @matchid: identifier to allow userspace to reidentify a match
2541da177e4SLinus Torvalds  * @flags: flags specifying attributes and the relation to other matches
2551da177e4SLinus Torvalds  * @ops: the operations lookup table of the corresponding ematch module
2561da177e4SLinus Torvalds  * @datalen: length of the ematch specific configuration data
2571da177e4SLinus Torvalds  * @data: ematch specific data
2581da177e4SLinus Torvalds  */
259fd2c3ef7SEric Dumazet struct tcf_ematch {
2601da177e4SLinus Torvalds 	struct tcf_ematch_ops * ops;
2611da177e4SLinus Torvalds 	unsigned long		data;
2621da177e4SLinus Torvalds 	unsigned int		datalen;
2631da177e4SLinus Torvalds 	u16			matchid;
2641da177e4SLinus Torvalds 	u16			flags;
26582a470f1SJohn Fastabend 	struct net		*net;
2661da177e4SLinus Torvalds };
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static inline int tcf_em_is_container(struct tcf_ematch *em)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	return !em->ops;
2711da177e4SLinus Torvalds }
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds static inline int tcf_em_is_simple(struct tcf_ematch *em)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	return em->flags & TCF_EM_SIMPLE;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static inline int tcf_em_is_inverted(struct tcf_ematch *em)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	return em->flags & TCF_EM_INVERT;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds static inline int tcf_em_last_match(struct tcf_ematch *em)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	if (tcf_em_last_match(em))
2911da177e4SLinus Torvalds 		return 1;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	if (result == 0 && em->flags & TCF_EM_REL_AND)
2941da177e4SLinus Torvalds 		return 1;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds 	if (result != 0 && em->flags & TCF_EM_REL_OR)
2971da177e4SLinus Torvalds 		return 1;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	return 0;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds /**
3031da177e4SLinus Torvalds  * struct tcf_ematch_tree - ematch tree handle
3041da177e4SLinus Torvalds  *
3051da177e4SLinus Torvalds  * @hdr: ematch tree header supplied by userspace
3061da177e4SLinus Torvalds  * @matches: array of ematches
3071da177e4SLinus Torvalds  */
308fd2c3ef7SEric Dumazet struct tcf_ematch_tree {
3091da177e4SLinus Torvalds 	struct tcf_ematch_tree_hdr hdr;
3101da177e4SLinus Torvalds 	struct tcf_ematch *	matches;
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds };
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds /**
3151da177e4SLinus Torvalds  * struct tcf_ematch_ops - ematch module operations
3161da177e4SLinus Torvalds  *
3171da177e4SLinus Torvalds  * @kind: identifier (kind) of this ematch module
3181da177e4SLinus Torvalds  * @datalen: length of expected configuration data (optional)
3191da177e4SLinus Torvalds  * @change: called during validation (optional)
3201da177e4SLinus Torvalds  * @match: called during ematch tree evaluation, must return 1/0
3211da177e4SLinus Torvalds  * @destroy: called during destroyage (optional)
3221da177e4SLinus Torvalds  * @dump: called during dumping process (optional)
3231da177e4SLinus Torvalds  * @owner: owner, must be set to THIS_MODULE
3241da177e4SLinus Torvalds  * @link: link to previous/next ematch module (internal use)
3251da177e4SLinus Torvalds  */
326fd2c3ef7SEric Dumazet struct tcf_ematch_ops {
3271da177e4SLinus Torvalds 	int			kind;
3281da177e4SLinus Torvalds 	int			datalen;
32982a470f1SJohn Fastabend 	int			(*change)(struct net *net, void *,
3301da177e4SLinus Torvalds 					  int, struct tcf_ematch *);
3311da177e4SLinus Torvalds 	int			(*match)(struct sk_buff *, struct tcf_ematch *,
3321da177e4SLinus Torvalds 					 struct tcf_pkt_info *);
33382a470f1SJohn Fastabend 	void			(*destroy)(struct tcf_ematch *);
3341da177e4SLinus Torvalds 	int			(*dump)(struct sk_buff *, struct tcf_ematch *);
3351da177e4SLinus Torvalds 	struct module		*owner;
3361da177e4SLinus Torvalds 	struct list_head	link;
3371da177e4SLinus Torvalds };
3381da177e4SLinus Torvalds 
3395c15257fSJoe Perches int tcf_em_register(struct tcf_ematch_ops *);
3405c15257fSJoe Perches void tcf_em_unregister(struct tcf_ematch_ops *);
3415c15257fSJoe Perches int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
3421da177e4SLinus Torvalds 			 struct tcf_ematch_tree *);
34382a470f1SJohn Fastabend void tcf_em_tree_destroy(struct tcf_ematch_tree *);
3445c15257fSJoe Perches int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
3455c15257fSJoe Perches int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
3461da177e4SLinus Torvalds 			struct tcf_pkt_info *);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds /**
3491da177e4SLinus Torvalds  * tcf_em_tree_match - evaulate an ematch tree
3501da177e4SLinus Torvalds  *
3511da177e4SLinus Torvalds  * @skb: socket buffer of the packet in question
3521da177e4SLinus Torvalds  * @tree: ematch tree to be used for evaluation
3531da177e4SLinus Torvalds  * @info: packet information examined by classifier
3541da177e4SLinus Torvalds  *
3551da177e4SLinus Torvalds  * This function matches @skb against the ematch tree in @tree by going
3561da177e4SLinus Torvalds  * through all ematches respecting their logic relations returning
3571da177e4SLinus Torvalds  * as soon as the result is obvious.
3581da177e4SLinus Torvalds  *
3591da177e4SLinus Torvalds  * Returns 1 if the ematch tree as-one matches, no ematches are configured
3601da177e4SLinus Torvalds  * or ematch is not enabled in the kernel, otherwise 0 is returned.
3611da177e4SLinus Torvalds  */
3621da177e4SLinus Torvalds static inline int tcf_em_tree_match(struct sk_buff *skb,
3631da177e4SLinus Torvalds 				    struct tcf_ematch_tree *tree,
3641da177e4SLinus Torvalds 				    struct tcf_pkt_info *info)
3651da177e4SLinus Torvalds {
3661da177e4SLinus Torvalds 	if (tree->hdr.nmatches)
3671da177e4SLinus Torvalds 		return __tcf_em_tree_match(skb, tree, info);
3681da177e4SLinus Torvalds 	else
3691da177e4SLinus Torvalds 		return 1;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
372db3d99c0SPatrick McHardy #define MODULE_ALIAS_TCF_EMATCH(kind)	MODULE_ALIAS("ematch-kind-" __stringify(kind))
373db3d99c0SPatrick McHardy 
3741da177e4SLinus Torvalds #else /* CONFIG_NET_EMATCH */
3751da177e4SLinus Torvalds 
376fd2c3ef7SEric Dumazet struct tcf_ematch_tree {
3771da177e4SLinus Torvalds };
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
38082a470f1SJohn Fastabend #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
3811da177e4SLinus Torvalds #define tcf_em_tree_dump(skb, t, tlv) (0)
3821da177e4SLinus Torvalds #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds #endif /* CONFIG_NET_EMATCH */
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	switch (layer) {
3891da177e4SLinus Torvalds 		case TCF_LAYER_LINK:
3901da177e4SLinus Torvalds 			return skb->data;
3911da177e4SLinus Torvalds 		case TCF_LAYER_NETWORK:
392d56f90a7SArnaldo Carvalho de Melo 			return skb_network_header(skb);
3931da177e4SLinus Torvalds 		case TCF_LAYER_TRANSPORT:
3949c70220bSArnaldo Carvalho de Melo 			return skb_transport_header(skb);
3951da177e4SLinus Torvalds 	}
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	return NULL;
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds 
400eddc9ec5SArnaldo Carvalho de Melo static inline int tcf_valid_offset(const struct sk_buff *skb,
401eddc9ec5SArnaldo Carvalho de Melo 				   const unsigned char *ptr, const int len)
4021da177e4SLinus Torvalds {
403da521b2cSDavid S. Miller 	return likely((ptr + len) <= skb_tail_pointer(skb) &&
404da521b2cSDavid S. Miller 		      ptr >= skb->head &&
405da521b2cSDavid S. Miller 		      (ptr <= (ptr + len)));
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_IND
4090eeb8ffcSDenis V. Lunev #include <net/net_namespace.h>
4100eeb8ffcSDenis V. Lunev 
4111da177e4SLinus Torvalds static inline int
4122519a602SWANG Cong tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
4131da177e4SLinus Torvalds {
4142519a602SWANG Cong 	char indev[IFNAMSIZ];
415c01003c2SPatrick McHardy 	struct net_device *dev;
416c01003c2SPatrick McHardy 
4172519a602SWANG Cong 	if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
4182519a602SWANG Cong 		return -EINVAL;
4192519a602SWANG Cong 	dev = __dev_get_by_name(net, indev);
4202519a602SWANG Cong 	if (!dev)
4212519a602SWANG Cong 		return -ENODEV;
4222519a602SWANG Cong 	return dev->ifindex;
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds 
4252519a602SWANG Cong static inline bool
4262519a602SWANG Cong tcf_match_indev(struct sk_buff *skb, int ifindex)
4272519a602SWANG Cong {
4282519a602SWANG Cong 	if (!ifindex)
4292519a602SWANG Cong 		return true;
4302519a602SWANG Cong 	if  (!skb->skb_iif)
4312519a602SWANG Cong 		return false;
4322519a602SWANG Cong 	return ifindex == skb->skb_iif;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds #endif /* CONFIG_NET_CLS_IND */
4351da177e4SLinus Torvalds 
4365fd9fc4eSJiri Pirko struct tc_cls_common_offload {
4375fd9fc4eSJiri Pirko 	u32 chain_index;
4385fd9fc4eSJiri Pirko 	__be16 protocol;
439d7c1c8d2SJiri Pirko 	u32 prio;
4407690f2a5SJiri Pirko 	u32 classid;
4415fd9fc4eSJiri Pirko };
4425fd9fc4eSJiri Pirko 
4435fd9fc4eSJiri Pirko static inline void
4445fd9fc4eSJiri Pirko tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common,
4455fd9fc4eSJiri Pirko 			   const struct tcf_proto *tp)
4465fd9fc4eSJiri Pirko {
4475fd9fc4eSJiri Pirko 	cls_common->chain_index = tp->chain->index;
4485fd9fc4eSJiri Pirko 	cls_common->protocol = tp->protocol;
449d7c1c8d2SJiri Pirko 	cls_common->prio = tp->prio;
4507690f2a5SJiri Pirko 	cls_common->classid = tp->classid;
4515fd9fc4eSJiri Pirko }
4525fd9fc4eSJiri Pirko 
453a1b7c5fdSJohn Fastabend struct tc_cls_u32_knode {
454a1b7c5fdSJohn Fastabend 	struct tcf_exts *exts;
455e014860eSJohn Fastabend 	struct tc_u32_sel *sel;
456a1b7c5fdSJohn Fastabend 	u32 handle;
457a1b7c5fdSJohn Fastabend 	u32 val;
458a1b7c5fdSJohn Fastabend 	u32 mask;
459a1b7c5fdSJohn Fastabend 	u32 link_handle;
460e014860eSJohn Fastabend 	u8 fshift;
461a1b7c5fdSJohn Fastabend };
462a1b7c5fdSJohn Fastabend 
463a1b7c5fdSJohn Fastabend struct tc_cls_u32_hnode {
464a1b7c5fdSJohn Fastabend 	u32 handle;
465a1b7c5fdSJohn Fastabend 	u32 prio;
466a1b7c5fdSJohn Fastabend 	unsigned int divisor;
467a1b7c5fdSJohn Fastabend };
468a1b7c5fdSJohn Fastabend 
469a1b7c5fdSJohn Fastabend enum tc_clsu32_command {
470a1b7c5fdSJohn Fastabend 	TC_CLSU32_NEW_KNODE,
471a1b7c5fdSJohn Fastabend 	TC_CLSU32_REPLACE_KNODE,
472a1b7c5fdSJohn Fastabend 	TC_CLSU32_DELETE_KNODE,
473a1b7c5fdSJohn Fastabend 	TC_CLSU32_NEW_HNODE,
474a1b7c5fdSJohn Fastabend 	TC_CLSU32_REPLACE_HNODE,
475a1b7c5fdSJohn Fastabend 	TC_CLSU32_DELETE_HNODE,
476a1b7c5fdSJohn Fastabend };
477a1b7c5fdSJohn Fastabend 
478a1b7c5fdSJohn Fastabend struct tc_cls_u32_offload {
4795fd9fc4eSJiri Pirko 	struct tc_cls_common_offload common;
480a1b7c5fdSJohn Fastabend 	/* knode values */
481a1b7c5fdSJohn Fastabend 	enum tc_clsu32_command command;
482a1b7c5fdSJohn Fastabend 	union {
483a1b7c5fdSJohn Fastabend 		struct tc_cls_u32_knode knode;
484a1b7c5fdSJohn Fastabend 		struct tc_cls_u32_hnode hnode;
485a1b7c5fdSJohn Fastabend 	};
486a1b7c5fdSJohn Fastabend };
487a1b7c5fdSJohn Fastabend 
4887b06e8aeSJiri Pirko static inline bool tc_can_offload(const struct net_device *dev)
4896843e7a2SJohn Fastabend {
4902b6ab0d3SJohn Fastabend 	if (!(dev->features & NETIF_F_HW_TC))
4912b6ab0d3SJohn Fastabend 		return false;
4929e8ce79cSJohn Fastabend 	if (!dev->netdev_ops->ndo_setup_tc)
4939e8ce79cSJohn Fastabend 		return false;
4949e8ce79cSJohn Fastabend 	return true;
4956843e7a2SJohn Fastabend }
4966843e7a2SJohn Fastabend 
49755330f05SHadar Hen Zion static inline bool tc_skip_hw(u32 flags)
49855330f05SHadar Hen Zion {
49955330f05SHadar Hen Zion 	return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
50055330f05SHadar Hen Zion }
50155330f05SHadar Hen Zion 
5027b06e8aeSJiri Pirko static inline bool tc_should_offload(const struct net_device *dev, u32 flags)
50355330f05SHadar Hen Zion {
50455330f05SHadar Hen Zion 	if (tc_skip_hw(flags))
50555330f05SHadar Hen Zion 		return false;
5067b06e8aeSJiri Pirko 	return tc_can_offload(dev);
50755330f05SHadar Hen Zion }
50855330f05SHadar Hen Zion 
509d34e3e18SSamudrala, Sridhar static inline bool tc_skip_sw(u32 flags)
510d34e3e18SSamudrala, Sridhar {
511d34e3e18SSamudrala, Sridhar 	return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
512d34e3e18SSamudrala, Sridhar }
513d34e3e18SSamudrala, Sridhar 
514d34e3e18SSamudrala, Sridhar /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
515d34e3e18SSamudrala, Sridhar static inline bool tc_flags_valid(u32 flags)
516d34e3e18SSamudrala, Sridhar {
517d34e3e18SSamudrala, Sridhar 	if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
518d34e3e18SSamudrala, Sridhar 		return false;
519d34e3e18SSamudrala, Sridhar 
520d34e3e18SSamudrala, Sridhar 	if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
521d34e3e18SSamudrala, Sridhar 		return false;
522d34e3e18SSamudrala, Sridhar 
523d34e3e18SSamudrala, Sridhar 	return true;
524d34e3e18SSamudrala, Sridhar }
525d34e3e18SSamudrala, Sridhar 
526e696028aSOr Gerlitz static inline bool tc_in_hw(u32 flags)
527e696028aSOr Gerlitz {
528e696028aSOr Gerlitz 	return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
529e696028aSOr Gerlitz }
530e696028aSOr Gerlitz 
5315b33f488SAmir Vadai enum tc_fl_command {
5325b33f488SAmir Vadai 	TC_CLSFLOWER_REPLACE,
5335b33f488SAmir Vadai 	TC_CLSFLOWER_DESTROY,
53410cbc684SAmir Vadai 	TC_CLSFLOWER_STATS,
5355b33f488SAmir Vadai };
5365b33f488SAmir Vadai 
5375b33f488SAmir Vadai struct tc_cls_flower_offload {
5385fd9fc4eSJiri Pirko 	struct tc_cls_common_offload common;
5395b33f488SAmir Vadai 	enum tc_fl_command command;
5408208d21bSAmir Vadai 	unsigned long cookie;
5415b33f488SAmir Vadai 	struct flow_dissector *dissector;
5425b33f488SAmir Vadai 	struct fl_flow_key *mask;
5435b33f488SAmir Vadai 	struct fl_flow_key *key;
5445b33f488SAmir Vadai 	struct tcf_exts *exts;
5453e0e8266SJiri Pirko 	bool egress_dev;
5465b33f488SAmir Vadai };
5475b33f488SAmir Vadai 
548b87f7936SYotam Gigi enum tc_matchall_command {
549b87f7936SYotam Gigi 	TC_CLSMATCHALL_REPLACE,
550b87f7936SYotam Gigi 	TC_CLSMATCHALL_DESTROY,
551b87f7936SYotam Gigi };
552b87f7936SYotam Gigi 
553b87f7936SYotam Gigi struct tc_cls_matchall_offload {
5545fd9fc4eSJiri Pirko 	struct tc_cls_common_offload common;
555b87f7936SYotam Gigi 	enum tc_matchall_command command;
556b87f7936SYotam Gigi 	struct tcf_exts *exts;
557b87f7936SYotam Gigi 	unsigned long cookie;
558b87f7936SYotam Gigi };
559b87f7936SYotam Gigi 
560332ae8e2SJakub Kicinski enum tc_clsbpf_command {
561332ae8e2SJakub Kicinski 	TC_CLSBPF_ADD,
562332ae8e2SJakub Kicinski 	TC_CLSBPF_REPLACE,
563332ae8e2SJakub Kicinski 	TC_CLSBPF_DESTROY,
56468d64063SJakub Kicinski 	TC_CLSBPF_STATS,
565332ae8e2SJakub Kicinski };
566332ae8e2SJakub Kicinski 
567332ae8e2SJakub Kicinski struct tc_cls_bpf_offload {
5685fd9fc4eSJiri Pirko 	struct tc_cls_common_offload common;
569332ae8e2SJakub Kicinski 	enum tc_clsbpf_command command;
570332ae8e2SJakub Kicinski 	struct tcf_exts *exts;
571332ae8e2SJakub Kicinski 	struct bpf_prog *prog;
572332ae8e2SJakub Kicinski 	const char *name;
573332ae8e2SJakub Kicinski 	bool exts_integrated;
5740d01d45fSJakub Kicinski 	u32 gen_flags;
575332ae8e2SJakub Kicinski };
576332ae8e2SJakub Kicinski 
5771045ba77SJamal Hadi Salim 
5781045ba77SJamal Hadi Salim /* This structure holds cookie structure that is passed from user
5791045ba77SJamal Hadi Salim  * to the kernel for actions and classifiers
5801045ba77SJamal Hadi Salim  */
5811045ba77SJamal Hadi Salim struct tc_cookie {
5821045ba77SJamal Hadi Salim 	u8  *data;
5831045ba77SJamal Hadi Salim 	u32 len;
5841045ba77SJamal Hadi Salim };
5851da177e4SLinus Torvalds #endif
586