xref: /openbmc/linux/include/net/pkt_cls.h (revision 9b0d4446)
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
21367a8ce8SWANG Cong struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
22367a8ce8SWANG Cong 				bool create);
235bc17018SJiri Pirko void tcf_chain_put(struct tcf_chain *chain);
246529eabaSJiri Pirko int tcf_block_get(struct tcf_block **p_block,
256529eabaSJiri Pirko 		  struct tcf_proto __rcu **p_filter_chain);
266529eabaSJiri Pirko void tcf_block_put(struct tcf_block *block);
2787d83093SJiri Pirko int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
2887d83093SJiri Pirko 		 struct tcf_result *res, bool compat_mode);
2987d83093SJiri Pirko 
308ae70032SJiri Pirko #else
316529eabaSJiri Pirko static inline
326529eabaSJiri Pirko int tcf_block_get(struct tcf_block **p_block,
336529eabaSJiri Pirko 		  struct tcf_proto __rcu **p_filter_chain)
346529eabaSJiri Pirko {
356529eabaSJiri Pirko 	return 0;
366529eabaSJiri Pirko }
376529eabaSJiri Pirko 
386529eabaSJiri Pirko static inline void tcf_block_put(struct tcf_block *block)
398ae70032SJiri Pirko {
408ae70032SJiri Pirko }
4187d83093SJiri Pirko 
4287d83093SJiri Pirko static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
4387d83093SJiri Pirko 			       struct tcf_result *res, bool compat_mode)
4487d83093SJiri Pirko {
4587d83093SJiri Pirko 	return TC_ACT_UNSPEC;
4687d83093SJiri Pirko }
478ae70032SJiri Pirko #endif
48cf1facdaSJiri Pirko 
491da177e4SLinus Torvalds static inline unsigned long
501da177e4SLinus Torvalds __cls_set_class(unsigned long *clp, unsigned long cl)
511da177e4SLinus Torvalds {
52a0efb80cSWANG Cong 	return xchg(clp, cl);
531da177e4SLinus Torvalds }
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds static inline unsigned long
561da177e4SLinus Torvalds cls_set_class(struct tcf_proto *tp, unsigned long *clp,
571da177e4SLinus Torvalds 	unsigned long cl)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds 	unsigned long old_cl;
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds 	tcf_tree_lock(tp);
621da177e4SLinus Torvalds 	old_cl = __cls_set_class(clp, cl);
631da177e4SLinus Torvalds 	tcf_tree_unlock(tp);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds 	return old_cl;
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds static inline void
691da177e4SLinus Torvalds tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds 	unsigned long cl;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
741da177e4SLinus Torvalds 	cl = cls_set_class(tp, &r->class, cl);
751da177e4SLinus Torvalds 	if (cl)
761da177e4SLinus Torvalds 		tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static inline void
801da177e4SLinus Torvalds tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	unsigned long cl;
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	if ((cl = __cls_set_class(&r->class, 0)) != 0)
851da177e4SLinus Torvalds 		tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
88fd2c3ef7SEric Dumazet struct tcf_exts {
891da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ACT
9033be6271SWANG Cong 	__u32	type; /* for backward compat(TCA_OLD_COMPAT) */
9122dc13c8SWANG Cong 	int nr_actions;
9222dc13c8SWANG Cong 	struct tc_action **actions;
931da177e4SLinus Torvalds #endif
941da177e4SLinus Torvalds 	/* Map to export classifier specific extension TLV types to the
951da177e4SLinus Torvalds 	 * generic extensions API. Unsupported extensions must be set to 0.
961da177e4SLinus Torvalds 	 */
971da177e4SLinus Torvalds 	int action;
981da177e4SLinus Torvalds 	int police;
991da177e4SLinus Torvalds };
1001da177e4SLinus Torvalds 
101b9a24bb7SWANG Cong static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
10233be6271SWANG Cong {
10333be6271SWANG Cong #ifdef CONFIG_NET_CLS_ACT
1045da57f42SWANG Cong 	exts->type = 0;
10522dc13c8SWANG Cong 	exts->nr_actions = 0;
10622dc13c8SWANG Cong 	exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
10722dc13c8SWANG Cong 				GFP_KERNEL);
108b9a24bb7SWANG Cong 	if (!exts->actions)
109b9a24bb7SWANG Cong 		return -ENOMEM;
11033be6271SWANG Cong #endif
1115da57f42SWANG Cong 	exts->action = action;
1125da57f42SWANG Cong 	exts->police = police;
113b9a24bb7SWANG Cong 	return 0;
11433be6271SWANG Cong }
11533be6271SWANG Cong 
11622dc13c8SWANG Cong static inline void tcf_exts_to_list(const struct tcf_exts *exts,
11722dc13c8SWANG Cong 				    struct list_head *actions)
11822dc13c8SWANG Cong {
11922dc13c8SWANG Cong #ifdef CONFIG_NET_CLS_ACT
12022dc13c8SWANG Cong 	int i;
12122dc13c8SWANG Cong 
12222dc13c8SWANG Cong 	for (i = 0; i < exts->nr_actions; i++) {
12322dc13c8SWANG Cong 		struct tc_action *a = exts->actions[i];
12422dc13c8SWANG Cong 
125fa5effe7SHadar Hen Zion 		list_add_tail(&a->list, actions);
12622dc13c8SWANG Cong 	}
12722dc13c8SWANG Cong #endif
12822dc13c8SWANG Cong }
12922dc13c8SWANG Cong 
130d897a638SJakub Kicinski static inline void
131d897a638SJakub Kicinski tcf_exts_stats_update(const struct tcf_exts *exts,
132d897a638SJakub Kicinski 		      u64 bytes, u64 packets, u64 lastuse)
133d897a638SJakub Kicinski {
134d897a638SJakub Kicinski #ifdef CONFIG_NET_CLS_ACT
135d897a638SJakub Kicinski 	int i;
136d897a638SJakub Kicinski 
137d897a638SJakub Kicinski 	preempt_disable();
138d897a638SJakub Kicinski 
139d897a638SJakub Kicinski 	for (i = 0; i < exts->nr_actions; i++) {
140d897a638SJakub Kicinski 		struct tc_action *a = exts->actions[i];
141d897a638SJakub Kicinski 
142d897a638SJakub Kicinski 		tcf_action_stats_update(a, bytes, packets, lastuse);
143d897a638SJakub Kicinski 	}
144d897a638SJakub Kicinski 
145d897a638SJakub Kicinski 	preempt_enable();
146d897a638SJakub Kicinski #endif
147d897a638SJakub Kicinski }
148d897a638SJakub Kicinski 
1491da177e4SLinus Torvalds /**
1503bcc0cecSJiri Pirko  * tcf_exts_has_actions - check if at least one action is present
1513bcc0cecSJiri Pirko  * @exts: tc filter extensions handle
1523bcc0cecSJiri Pirko  *
1533bcc0cecSJiri Pirko  * Returns true if at least one action is present.
1543bcc0cecSJiri Pirko  */
1553bcc0cecSJiri Pirko static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
1563bcc0cecSJiri Pirko {
1572734437eSWANG Cong #ifdef CONFIG_NET_CLS_ACT
1583bcc0cecSJiri Pirko 	return exts->nr_actions;
1593bcc0cecSJiri Pirko #else
1603bcc0cecSJiri Pirko 	return false;
1613bcc0cecSJiri Pirko #endif
1623bcc0cecSJiri Pirko }
1632734437eSWANG Cong 
1643bcc0cecSJiri Pirko /**
1653bcc0cecSJiri Pirko  * tcf_exts_has_one_action - check if exactly one action is present
1663bcc0cecSJiri Pirko  * @exts: tc filter extensions handle
1673bcc0cecSJiri Pirko  *
1683bcc0cecSJiri Pirko  * Returns true if exactly one action is present.
1693bcc0cecSJiri Pirko  */
1703bcc0cecSJiri Pirko static inline bool tcf_exts_has_one_action(struct tcf_exts *exts)
1713bcc0cecSJiri Pirko {
1723bcc0cecSJiri Pirko #ifdef CONFIG_NET_CLS_ACT
1733bcc0cecSJiri Pirko 	return exts->nr_actions == 1;
1743bcc0cecSJiri Pirko #else
1753bcc0cecSJiri Pirko 	return false;
1763bcc0cecSJiri Pirko #endif
1773bcc0cecSJiri Pirko }
1782734437eSWANG Cong 
179af69afc5SJiri Pirko /**
180af69afc5SJiri Pirko  * tcf_exts_exec - execute tc filter extensions
181af69afc5SJiri Pirko  * @skb: socket buffer
182af69afc5SJiri Pirko  * @exts: tc filter extensions handle
183af69afc5SJiri Pirko  * @res: desired result
184af69afc5SJiri Pirko  *
185af089e70SJiri Pirko  * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
186af69afc5SJiri Pirko  * a negative number if the filter must be considered unmatched or
187af69afc5SJiri Pirko  * a positive action code (TC_ACT_*) which must be returned to the
188af69afc5SJiri Pirko  * underlying layer.
189af69afc5SJiri Pirko  */
190af69afc5SJiri Pirko static inline int
191af69afc5SJiri Pirko tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
192af69afc5SJiri Pirko 	      struct tcf_result *res)
193af69afc5SJiri Pirko {
194af69afc5SJiri Pirko #ifdef CONFIG_NET_CLS_ACT
195ec1a9ccaSJiri Pirko 	return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
196af69afc5SJiri Pirko #endif
197af089e70SJiri Pirko 	return TC_ACT_OK;
198af69afc5SJiri Pirko }
199af69afc5SJiri Pirko 
2005c15257fSJoe Perches int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
201c1b52739SBenjamin LaHaise 		      struct nlattr **tb, struct nlattr *rate_tlv,
2022f7ef2f8SCong Wang 		      struct tcf_exts *exts, bool ovr);
20318d0264fSWANG Cong void tcf_exts_destroy(struct tcf_exts *exts);
2049b0d4446SJiri Pirko void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
2055da57f42SWANG Cong int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
2065da57f42SWANG Cong int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
2077091d8c7SHadar Hen Zion int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
2087091d8c7SHadar Hen Zion 		     struct net_device **hw_dev);
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds /**
2111da177e4SLinus Torvalds  * struct tcf_pkt_info - packet information
2121da177e4SLinus Torvalds  */
213fd2c3ef7SEric Dumazet struct tcf_pkt_info {
2141da177e4SLinus Torvalds 	unsigned char *		ptr;
2151da177e4SLinus Torvalds 	int			nexthdr;
2161da177e4SLinus Torvalds };
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds #ifdef CONFIG_NET_EMATCH
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds struct tcf_ematch_ops;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds /**
2231da177e4SLinus Torvalds  * struct tcf_ematch - extended match (ematch)
2241da177e4SLinus Torvalds  *
2251da177e4SLinus Torvalds  * @matchid: identifier to allow userspace to reidentify a match
2261da177e4SLinus Torvalds  * @flags: flags specifying attributes and the relation to other matches
2271da177e4SLinus Torvalds  * @ops: the operations lookup table of the corresponding ematch module
2281da177e4SLinus Torvalds  * @datalen: length of the ematch specific configuration data
2291da177e4SLinus Torvalds  * @data: ematch specific data
2301da177e4SLinus Torvalds  */
231fd2c3ef7SEric Dumazet struct tcf_ematch {
2321da177e4SLinus Torvalds 	struct tcf_ematch_ops * ops;
2331da177e4SLinus Torvalds 	unsigned long		data;
2341da177e4SLinus Torvalds 	unsigned int		datalen;
2351da177e4SLinus Torvalds 	u16			matchid;
2361da177e4SLinus Torvalds 	u16			flags;
23782a470f1SJohn Fastabend 	struct net		*net;
2381da177e4SLinus Torvalds };
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds static inline int tcf_em_is_container(struct tcf_ematch *em)
2411da177e4SLinus Torvalds {
2421da177e4SLinus Torvalds 	return !em->ops;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds static inline int tcf_em_is_simple(struct tcf_ematch *em)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	return em->flags & TCF_EM_SIMPLE;
2481da177e4SLinus Torvalds }
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds static inline int tcf_em_is_inverted(struct tcf_ematch *em)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds 	return em->flags & TCF_EM_INVERT;
2531da177e4SLinus Torvalds }
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds static inline int tcf_em_last_match(struct tcf_ematch *em)
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
2581da177e4SLinus Torvalds }
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	if (tcf_em_last_match(em))
2631da177e4SLinus Torvalds 		return 1;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	if (result == 0 && em->flags & TCF_EM_REL_AND)
2661da177e4SLinus Torvalds 		return 1;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	if (result != 0 && em->flags & TCF_EM_REL_OR)
2691da177e4SLinus Torvalds 		return 1;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	return 0;
2721da177e4SLinus Torvalds }
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds /**
2751da177e4SLinus Torvalds  * struct tcf_ematch_tree - ematch tree handle
2761da177e4SLinus Torvalds  *
2771da177e4SLinus Torvalds  * @hdr: ematch tree header supplied by userspace
2781da177e4SLinus Torvalds  * @matches: array of ematches
2791da177e4SLinus Torvalds  */
280fd2c3ef7SEric Dumazet struct tcf_ematch_tree {
2811da177e4SLinus Torvalds 	struct tcf_ematch_tree_hdr hdr;
2821da177e4SLinus Torvalds 	struct tcf_ematch *	matches;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds };
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds /**
2871da177e4SLinus Torvalds  * struct tcf_ematch_ops - ematch module operations
2881da177e4SLinus Torvalds  *
2891da177e4SLinus Torvalds  * @kind: identifier (kind) of this ematch module
2901da177e4SLinus Torvalds  * @datalen: length of expected configuration data (optional)
2911da177e4SLinus Torvalds  * @change: called during validation (optional)
2921da177e4SLinus Torvalds  * @match: called during ematch tree evaluation, must return 1/0
2931da177e4SLinus Torvalds  * @destroy: called during destroyage (optional)
2941da177e4SLinus Torvalds  * @dump: called during dumping process (optional)
2951da177e4SLinus Torvalds  * @owner: owner, must be set to THIS_MODULE
2961da177e4SLinus Torvalds  * @link: link to previous/next ematch module (internal use)
2971da177e4SLinus Torvalds  */
298fd2c3ef7SEric Dumazet struct tcf_ematch_ops {
2991da177e4SLinus Torvalds 	int			kind;
3001da177e4SLinus Torvalds 	int			datalen;
30182a470f1SJohn Fastabend 	int			(*change)(struct net *net, void *,
3021da177e4SLinus Torvalds 					  int, struct tcf_ematch *);
3031da177e4SLinus Torvalds 	int			(*match)(struct sk_buff *, struct tcf_ematch *,
3041da177e4SLinus Torvalds 					 struct tcf_pkt_info *);
30582a470f1SJohn Fastabend 	void			(*destroy)(struct tcf_ematch *);
3061da177e4SLinus Torvalds 	int			(*dump)(struct sk_buff *, struct tcf_ematch *);
3071da177e4SLinus Torvalds 	struct module		*owner;
3081da177e4SLinus Torvalds 	struct list_head	link;
3091da177e4SLinus Torvalds };
3101da177e4SLinus Torvalds 
3115c15257fSJoe Perches int tcf_em_register(struct tcf_ematch_ops *);
3125c15257fSJoe Perches void tcf_em_unregister(struct tcf_ematch_ops *);
3135c15257fSJoe Perches int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
3141da177e4SLinus Torvalds 			 struct tcf_ematch_tree *);
31582a470f1SJohn Fastabend void tcf_em_tree_destroy(struct tcf_ematch_tree *);
3165c15257fSJoe Perches int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
3175c15257fSJoe Perches int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
3181da177e4SLinus Torvalds 			struct tcf_pkt_info *);
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds /**
3211da177e4SLinus Torvalds  * tcf_em_tree_match - evaulate an ematch tree
3221da177e4SLinus Torvalds  *
3231da177e4SLinus Torvalds  * @skb: socket buffer of the packet in question
3241da177e4SLinus Torvalds  * @tree: ematch tree to be used for evaluation
3251da177e4SLinus Torvalds  * @info: packet information examined by classifier
3261da177e4SLinus Torvalds  *
3271da177e4SLinus Torvalds  * This function matches @skb against the ematch tree in @tree by going
3281da177e4SLinus Torvalds  * through all ematches respecting their logic relations returning
3291da177e4SLinus Torvalds  * as soon as the result is obvious.
3301da177e4SLinus Torvalds  *
3311da177e4SLinus Torvalds  * Returns 1 if the ematch tree as-one matches, no ematches are configured
3321da177e4SLinus Torvalds  * or ematch is not enabled in the kernel, otherwise 0 is returned.
3331da177e4SLinus Torvalds  */
3341da177e4SLinus Torvalds static inline int tcf_em_tree_match(struct sk_buff *skb,
3351da177e4SLinus Torvalds 				    struct tcf_ematch_tree *tree,
3361da177e4SLinus Torvalds 				    struct tcf_pkt_info *info)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	if (tree->hdr.nmatches)
3391da177e4SLinus Torvalds 		return __tcf_em_tree_match(skb, tree, info);
3401da177e4SLinus Torvalds 	else
3411da177e4SLinus Torvalds 		return 1;
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
344db3d99c0SPatrick McHardy #define MODULE_ALIAS_TCF_EMATCH(kind)	MODULE_ALIAS("ematch-kind-" __stringify(kind))
345db3d99c0SPatrick McHardy 
3461da177e4SLinus Torvalds #else /* CONFIG_NET_EMATCH */
3471da177e4SLinus Torvalds 
348fd2c3ef7SEric Dumazet struct tcf_ematch_tree {
3491da177e4SLinus Torvalds };
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
35282a470f1SJohn Fastabend #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
3531da177e4SLinus Torvalds #define tcf_em_tree_dump(skb, t, tlv) (0)
3541da177e4SLinus Torvalds #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds #endif /* CONFIG_NET_EMATCH */
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
3591da177e4SLinus Torvalds {
3601da177e4SLinus Torvalds 	switch (layer) {
3611da177e4SLinus Torvalds 		case TCF_LAYER_LINK:
3621da177e4SLinus Torvalds 			return skb->data;
3631da177e4SLinus Torvalds 		case TCF_LAYER_NETWORK:
364d56f90a7SArnaldo Carvalho de Melo 			return skb_network_header(skb);
3651da177e4SLinus Torvalds 		case TCF_LAYER_TRANSPORT:
3669c70220bSArnaldo Carvalho de Melo 			return skb_transport_header(skb);
3671da177e4SLinus Torvalds 	}
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	return NULL;
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
372eddc9ec5SArnaldo Carvalho de Melo static inline int tcf_valid_offset(const struct sk_buff *skb,
373eddc9ec5SArnaldo Carvalho de Melo 				   const unsigned char *ptr, const int len)
3741da177e4SLinus Torvalds {
375da521b2cSDavid S. Miller 	return likely((ptr + len) <= skb_tail_pointer(skb) &&
376da521b2cSDavid S. Miller 		      ptr >= skb->head &&
377da521b2cSDavid S. Miller 		      (ptr <= (ptr + len)));
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_IND
3810eeb8ffcSDenis V. Lunev #include <net/net_namespace.h>
3820eeb8ffcSDenis V. Lunev 
3831da177e4SLinus Torvalds static inline int
3842519a602SWANG Cong tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
3851da177e4SLinus Torvalds {
3862519a602SWANG Cong 	char indev[IFNAMSIZ];
387c01003c2SPatrick McHardy 	struct net_device *dev;
388c01003c2SPatrick McHardy 
3892519a602SWANG Cong 	if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
3902519a602SWANG Cong 		return -EINVAL;
3912519a602SWANG Cong 	dev = __dev_get_by_name(net, indev);
3922519a602SWANG Cong 	if (!dev)
3932519a602SWANG Cong 		return -ENODEV;
3942519a602SWANG Cong 	return dev->ifindex;
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3972519a602SWANG Cong static inline bool
3982519a602SWANG Cong tcf_match_indev(struct sk_buff *skb, int ifindex)
3992519a602SWANG Cong {
4002519a602SWANG Cong 	if (!ifindex)
4012519a602SWANG Cong 		return true;
4022519a602SWANG Cong 	if  (!skb->skb_iif)
4032519a602SWANG Cong 		return false;
4042519a602SWANG Cong 	return ifindex == skb->skb_iif;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds #endif /* CONFIG_NET_CLS_IND */
4071da177e4SLinus Torvalds 
408a1b7c5fdSJohn Fastabend struct tc_cls_u32_knode {
409a1b7c5fdSJohn Fastabend 	struct tcf_exts *exts;
410e014860eSJohn Fastabend 	struct tc_u32_sel *sel;
411a1b7c5fdSJohn Fastabend 	u32 handle;
412a1b7c5fdSJohn Fastabend 	u32 val;
413a1b7c5fdSJohn Fastabend 	u32 mask;
414a1b7c5fdSJohn Fastabend 	u32 link_handle;
415e014860eSJohn Fastabend 	u8 fshift;
416a1b7c5fdSJohn Fastabend };
417a1b7c5fdSJohn Fastabend 
418a1b7c5fdSJohn Fastabend struct tc_cls_u32_hnode {
419a1b7c5fdSJohn Fastabend 	u32 handle;
420a1b7c5fdSJohn Fastabend 	u32 prio;
421a1b7c5fdSJohn Fastabend 	unsigned int divisor;
422a1b7c5fdSJohn Fastabend };
423a1b7c5fdSJohn Fastabend 
424a1b7c5fdSJohn Fastabend enum tc_clsu32_command {
425a1b7c5fdSJohn Fastabend 	TC_CLSU32_NEW_KNODE,
426a1b7c5fdSJohn Fastabend 	TC_CLSU32_REPLACE_KNODE,
427a1b7c5fdSJohn Fastabend 	TC_CLSU32_DELETE_KNODE,
428a1b7c5fdSJohn Fastabend 	TC_CLSU32_NEW_HNODE,
429a1b7c5fdSJohn Fastabend 	TC_CLSU32_REPLACE_HNODE,
430a1b7c5fdSJohn Fastabend 	TC_CLSU32_DELETE_HNODE,
431a1b7c5fdSJohn Fastabend };
432a1b7c5fdSJohn Fastabend 
433a1b7c5fdSJohn Fastabend struct tc_cls_u32_offload {
434a1b7c5fdSJohn Fastabend 	/* knode values */
435a1b7c5fdSJohn Fastabend 	enum tc_clsu32_command command;
436a1b7c5fdSJohn Fastabend 	union {
437a1b7c5fdSJohn Fastabend 		struct tc_cls_u32_knode knode;
438a1b7c5fdSJohn Fastabend 		struct tc_cls_u32_hnode hnode;
439a1b7c5fdSJohn Fastabend 	};
440a1b7c5fdSJohn Fastabend };
441a1b7c5fdSJohn Fastabend 
44255330f05SHadar Hen Zion static inline bool tc_can_offload(const struct net_device *dev,
44355330f05SHadar Hen Zion 				  const struct tcf_proto *tp)
4446843e7a2SJohn Fastabend {
44592c075dbSDaniel Borkmann 	const struct Qdisc *sch = tp->q;
44692c075dbSDaniel Borkmann 	const struct Qdisc_class_ops *cops = sch->ops->cl_ops;
44792c075dbSDaniel Borkmann 
4482b6ab0d3SJohn Fastabend 	if (!(dev->features & NETIF_F_HW_TC))
4492b6ab0d3SJohn Fastabend 		return false;
4509e8ce79cSJohn Fastabend 	if (!dev->netdev_ops->ndo_setup_tc)
4519e8ce79cSJohn Fastabend 		return false;
45292c075dbSDaniel Borkmann 	if (cops && cops->tcf_cl_offload)
45392c075dbSDaniel Borkmann 		return cops->tcf_cl_offload(tp->classid);
4549e8ce79cSJohn Fastabend 
4559e8ce79cSJohn Fastabend 	return true;
4566843e7a2SJohn Fastabend }
4576843e7a2SJohn Fastabend 
45855330f05SHadar Hen Zion static inline bool tc_skip_hw(u32 flags)
45955330f05SHadar Hen Zion {
46055330f05SHadar Hen Zion 	return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
46155330f05SHadar Hen Zion }
46255330f05SHadar Hen Zion 
46355330f05SHadar Hen Zion static inline bool tc_should_offload(const struct net_device *dev,
46455330f05SHadar Hen Zion 				     const struct tcf_proto *tp, u32 flags)
46555330f05SHadar Hen Zion {
46655330f05SHadar Hen Zion 	if (tc_skip_hw(flags))
46755330f05SHadar Hen Zion 		return false;
46855330f05SHadar Hen Zion 	return tc_can_offload(dev, tp);
46955330f05SHadar Hen Zion }
47055330f05SHadar Hen Zion 
471d34e3e18SSamudrala, Sridhar static inline bool tc_skip_sw(u32 flags)
472d34e3e18SSamudrala, Sridhar {
473d34e3e18SSamudrala, Sridhar 	return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
474d34e3e18SSamudrala, Sridhar }
475d34e3e18SSamudrala, Sridhar 
476d34e3e18SSamudrala, Sridhar /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
477d34e3e18SSamudrala, Sridhar static inline bool tc_flags_valid(u32 flags)
478d34e3e18SSamudrala, Sridhar {
479d34e3e18SSamudrala, Sridhar 	if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
480d34e3e18SSamudrala, Sridhar 		return false;
481d34e3e18SSamudrala, Sridhar 
482d34e3e18SSamudrala, Sridhar 	if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
483d34e3e18SSamudrala, Sridhar 		return false;
484d34e3e18SSamudrala, Sridhar 
485d34e3e18SSamudrala, Sridhar 	return true;
486d34e3e18SSamudrala, Sridhar }
487d34e3e18SSamudrala, Sridhar 
488e696028aSOr Gerlitz static inline bool tc_in_hw(u32 flags)
489e696028aSOr Gerlitz {
490e696028aSOr Gerlitz 	return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
491e696028aSOr Gerlitz }
492e696028aSOr Gerlitz 
4935b33f488SAmir Vadai enum tc_fl_command {
4945b33f488SAmir Vadai 	TC_CLSFLOWER_REPLACE,
4955b33f488SAmir Vadai 	TC_CLSFLOWER_DESTROY,
49610cbc684SAmir Vadai 	TC_CLSFLOWER_STATS,
4975b33f488SAmir Vadai };
4985b33f488SAmir Vadai 
4995b33f488SAmir Vadai struct tc_cls_flower_offload {
5005b33f488SAmir Vadai 	enum tc_fl_command command;
50169ca05ceSJiri Pirko 	u32 prio;
5028208d21bSAmir Vadai 	unsigned long cookie;
5035b33f488SAmir Vadai 	struct flow_dissector *dissector;
5045b33f488SAmir Vadai 	struct fl_flow_key *mask;
5055b33f488SAmir Vadai 	struct fl_flow_key *key;
5065b33f488SAmir Vadai 	struct tcf_exts *exts;
5075b33f488SAmir Vadai };
5085b33f488SAmir Vadai 
509b87f7936SYotam Gigi enum tc_matchall_command {
510b87f7936SYotam Gigi 	TC_CLSMATCHALL_REPLACE,
511b87f7936SYotam Gigi 	TC_CLSMATCHALL_DESTROY,
512b87f7936SYotam Gigi };
513b87f7936SYotam Gigi 
514b87f7936SYotam Gigi struct tc_cls_matchall_offload {
515b87f7936SYotam Gigi 	enum tc_matchall_command command;
516b87f7936SYotam Gigi 	struct tcf_exts *exts;
517b87f7936SYotam Gigi 	unsigned long cookie;
518b87f7936SYotam Gigi };
519b87f7936SYotam Gigi 
520332ae8e2SJakub Kicinski enum tc_clsbpf_command {
521332ae8e2SJakub Kicinski 	TC_CLSBPF_ADD,
522332ae8e2SJakub Kicinski 	TC_CLSBPF_REPLACE,
523332ae8e2SJakub Kicinski 	TC_CLSBPF_DESTROY,
52468d64063SJakub Kicinski 	TC_CLSBPF_STATS,
525332ae8e2SJakub Kicinski };
526332ae8e2SJakub Kicinski 
527332ae8e2SJakub Kicinski struct tc_cls_bpf_offload {
528332ae8e2SJakub Kicinski 	enum tc_clsbpf_command command;
529332ae8e2SJakub Kicinski 	struct tcf_exts *exts;
530332ae8e2SJakub Kicinski 	struct bpf_prog *prog;
531332ae8e2SJakub Kicinski 	const char *name;
532332ae8e2SJakub Kicinski 	bool exts_integrated;
5330d01d45fSJakub Kicinski 	u32 gen_flags;
534332ae8e2SJakub Kicinski };
535332ae8e2SJakub Kicinski 
5361045ba77SJamal Hadi Salim 
5371045ba77SJamal Hadi Salim /* This structure holds cookie structure that is passed from user
5381045ba77SJamal Hadi Salim  * to the kernel for actions and classifiers
5391045ba77SJamal Hadi Salim  */
5401045ba77SJamal Hadi Salim struct tc_cookie {
5411045ba77SJamal Hadi Salim 	u8  *data;
5421045ba77SJamal Hadi Salim 	u32 len;
5431045ba77SJamal Hadi Salim };
5441da177e4SLinus Torvalds #endif
545