xref: /linux/include/net/pkt_cls.h (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_PKT_CLS_H
3 #define __NET_PKT_CLS_H
4 
5 #include <linux/pkt_cls.h>
6 #include <linux/workqueue.h>
7 #include <net/sch_generic.h>
8 #include <net/act_api.h>
9 #include <net/net_namespace.h>
10 
11 /* TC action not accessible from user space */
12 #define TC_ACT_CONSUMED		(TC_ACT_VALUE_MAX + 1)
13 
14 /* Basic packet classifier frontend definitions. */
15 
16 struct tcf_walker {
17 	int	stop;
18 	int	skip;
19 	int	count;
20 	bool	nonempty;
21 	unsigned long cookie;
22 	int	(*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
23 };
24 
25 int register_tcf_proto_ops(struct tcf_proto_ops *ops);
26 void unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
27 
28 struct tcf_block_ext_info {
29 	enum flow_block_binder_type binder_type;
30 	tcf_chain_head_change_t *chain_head_change;
31 	void *chain_head_change_priv;
32 	u32 block_index;
33 };
34 
35 struct tcf_qevent {
36 	struct tcf_block	*block;
37 	struct tcf_block_ext_info info;
38 	struct tcf_proto __rcu *filter_chain;
39 };
40 
41 struct tcf_block_cb;
42 bool tcf_queue_work(struct rcu_work *rwork, work_func_t func);
43 
44 #ifdef CONFIG_NET_CLS
45 struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block,
46 				       u32 chain_index);
47 void tcf_chain_put_by_act(struct tcf_chain *chain);
48 struct tcf_chain *tcf_get_next_chain(struct tcf_block *block,
49 				     struct tcf_chain *chain);
50 struct tcf_proto *tcf_get_next_proto(struct tcf_chain *chain,
51 				     struct tcf_proto *tp);
52 void tcf_block_netif_keep_dst(struct tcf_block *block);
53 int tcf_block_get(struct tcf_block **p_block,
54 		  struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
55 		  struct netlink_ext_ack *extack);
56 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
57 		      struct tcf_block_ext_info *ei,
58 		      struct netlink_ext_ack *extack);
59 void tcf_block_put(struct tcf_block *block);
60 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
61 		       struct tcf_block_ext_info *ei);
62 
63 static inline bool tcf_block_shared(struct tcf_block *block)
64 {
65 	return block->index;
66 }
67 
68 static inline bool tcf_block_non_null_shared(struct tcf_block *block)
69 {
70 	return block && block->index;
71 }
72 
73 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
74 {
75 	WARN_ON(tcf_block_shared(block));
76 	return block->q;
77 }
78 
79 int tcf_classify(struct sk_buff *skb,
80 		 const struct tcf_block *block,
81 		 const struct tcf_proto *tp, struct tcf_result *res,
82 		 bool compat_mode);
83 
84 #else
85 static inline bool tcf_block_shared(struct tcf_block *block)
86 {
87 	return false;
88 }
89 
90 static inline bool tcf_block_non_null_shared(struct tcf_block *block)
91 {
92 	return false;
93 }
94 
95 static inline
96 int tcf_block_get(struct tcf_block **p_block,
97 		  struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
98 		  struct netlink_ext_ack *extack)
99 {
100 	return 0;
101 }
102 
103 static inline
104 int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q,
105 		      struct tcf_block_ext_info *ei,
106 		      struct netlink_ext_ack *extack)
107 {
108 	return 0;
109 }
110 
111 static inline void tcf_block_put(struct tcf_block *block)
112 {
113 }
114 
115 static inline
116 void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q,
117 		       struct tcf_block_ext_info *ei)
118 {
119 }
120 
121 static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
122 {
123 	return NULL;
124 }
125 
126 static inline
127 int tc_setup_cb_block_register(struct tcf_block *block, flow_setup_cb_t *cb,
128 			       void *cb_priv)
129 {
130 	return 0;
131 }
132 
133 static inline
134 void tc_setup_cb_block_unregister(struct tcf_block *block, flow_setup_cb_t *cb,
135 				  void *cb_priv)
136 {
137 }
138 
139 static inline int tcf_classify(struct sk_buff *skb,
140 			       const struct tcf_block *block,
141 			       const struct tcf_proto *tp,
142 			       struct tcf_result *res, bool compat_mode)
143 {
144 	return TC_ACT_UNSPEC;
145 }
146 
147 #endif
148 
149 static inline unsigned long
150 __cls_set_class(unsigned long *clp, unsigned long cl)
151 {
152 	return xchg(clp, cl);
153 }
154 
155 static inline void
156 __tcf_bind_filter(struct Qdisc *q, struct tcf_result *r, unsigned long base)
157 {
158 	unsigned long cl;
159 
160 	cl = q->ops->cl_ops->bind_tcf(q, base, r->classid);
161 	cl = __cls_set_class(&r->class, cl);
162 	if (cl)
163 		q->ops->cl_ops->unbind_tcf(q, cl);
164 }
165 
166 static inline void
167 tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
168 {
169 	struct Qdisc *q = tp->chain->block->q;
170 
171 	/* Check q as it is not set for shared blocks. In that case,
172 	 * setting class is not supported.
173 	 */
174 	if (!q)
175 		return;
176 	sch_tree_lock(q);
177 	__tcf_bind_filter(q, r, base);
178 	sch_tree_unlock(q);
179 }
180 
181 static inline void
182 __tcf_unbind_filter(struct Qdisc *q, struct tcf_result *r)
183 {
184 	unsigned long cl;
185 
186 	if ((cl = __cls_set_class(&r->class, 0)) != 0)
187 		q->ops->cl_ops->unbind_tcf(q, cl);
188 }
189 
190 static inline void
191 tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
192 {
193 	struct Qdisc *q = tp->chain->block->q;
194 
195 	if (!q)
196 		return;
197 	__tcf_unbind_filter(q, r);
198 }
199 
200 struct tcf_exts {
201 #ifdef CONFIG_NET_CLS_ACT
202 	__u32	type; /* for backward compat(TCA_OLD_COMPAT) */
203 	int nr_actions;
204 	struct tc_action **actions;
205 	struct net	*net;
206 	netns_tracker	ns_tracker;
207 #endif
208 	/* Map to export classifier specific extension TLV types to the
209 	 * generic extensions API. Unsupported extensions must be set to 0.
210 	 */
211 	int action;
212 	int police;
213 };
214 
215 static inline int tcf_exts_init(struct tcf_exts *exts, struct net *net,
216 				int action, int police)
217 {
218 #ifdef CONFIG_NET_CLS_ACT
219 	exts->type = 0;
220 	exts->nr_actions = 0;
221 	/* Note: we do not own yet a reference on net.
222 	 * This reference might be taken later from tcf_exts_get_net().
223 	 */
224 	exts->net = net;
225 	exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
226 				GFP_KERNEL);
227 	if (!exts->actions)
228 		return -ENOMEM;
229 #endif
230 	exts->action = action;
231 	exts->police = police;
232 	return 0;
233 }
234 
235 /* Return false if the netns is being destroyed in cleanup_net(). Callers
236  * need to do cleanup synchronously in this case, otherwise may race with
237  * tc_action_net_exit(). Return true for other cases.
238  */
239 static inline bool tcf_exts_get_net(struct tcf_exts *exts)
240 {
241 #ifdef CONFIG_NET_CLS_ACT
242 	exts->net = maybe_get_net(exts->net);
243 	if (exts->net)
244 		netns_tracker_alloc(exts->net, &exts->ns_tracker, GFP_KERNEL);
245 	return exts->net != NULL;
246 #else
247 	return true;
248 #endif
249 }
250 
251 static inline void tcf_exts_put_net(struct tcf_exts *exts)
252 {
253 #ifdef CONFIG_NET_CLS_ACT
254 	if (exts->net)
255 		put_net_track(exts->net, &exts->ns_tracker);
256 #endif
257 }
258 
259 #ifdef CONFIG_NET_CLS_ACT
260 #define tcf_exts_for_each_action(i, a, exts) \
261 	for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = (exts)->actions[i]); i++)
262 #else
263 #define tcf_exts_for_each_action(i, a, exts) \
264 	for (; 0; (void)(i), (void)(a), (void)(exts))
265 #endif
266 
267 #define tcf_act_for_each_action(i, a, actions) \
268 	for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = actions[i]); i++)
269 
270 static inline void
271 tcf_exts_hw_stats_update(const struct tcf_exts *exts,
272 			 u64 bytes, u64 packets, u64 drops, u64 lastuse,
273 			 u8 used_hw_stats, bool used_hw_stats_valid)
274 {
275 #ifdef CONFIG_NET_CLS_ACT
276 	int i;
277 
278 	for (i = 0; i < exts->nr_actions; i++) {
279 		struct tc_action *a = exts->actions[i];
280 
281 		/* if stats from hw, just skip */
282 		if (tcf_action_update_hw_stats(a)) {
283 			preempt_disable();
284 			tcf_action_stats_update(a, bytes, packets, drops,
285 						lastuse, true);
286 			preempt_enable();
287 
288 			a->used_hw_stats = used_hw_stats;
289 			a->used_hw_stats_valid = used_hw_stats_valid;
290 		}
291 	}
292 #endif
293 }
294 
295 /**
296  * tcf_exts_has_actions - check if at least one action is present
297  * @exts: tc filter extensions handle
298  *
299  * Returns true if at least one action is present.
300  */
301 static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
302 {
303 #ifdef CONFIG_NET_CLS_ACT
304 	return exts->nr_actions;
305 #else
306 	return false;
307 #endif
308 }
309 
310 /**
311  * tcf_exts_exec - execute tc filter extensions
312  * @skb: socket buffer
313  * @exts: tc filter extensions handle
314  * @res: desired result
315  *
316  * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
317  * a negative number if the filter must be considered unmatched or
318  * a positive action code (TC_ACT_*) which must be returned to the
319  * underlying layer.
320  */
321 static inline int
322 tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
323 	      struct tcf_result *res)
324 {
325 #ifdef CONFIG_NET_CLS_ACT
326 	return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
327 #endif
328 	return TC_ACT_OK;
329 }
330 
331 int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
332 		      struct nlattr **tb, struct nlattr *rate_tlv,
333 		      struct tcf_exts *exts, u32 flags,
334 		      struct netlink_ext_ack *extack);
335 int tcf_exts_validate_ex(struct net *net, struct tcf_proto *tp, struct nlattr **tb,
336 			 struct nlattr *rate_tlv, struct tcf_exts *exts,
337 			 u32 flags, u32 fl_flags, struct netlink_ext_ack *extack);
338 void tcf_exts_destroy(struct tcf_exts *exts);
339 void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
340 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
341 int tcf_exts_terse_dump(struct sk_buff *skb, struct tcf_exts *exts);
342 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
343 
344 /**
345  * struct tcf_pkt_info - packet information
346  *
347  * @ptr: start of the pkt data
348  * @nexthdr: offset of the next header
349  */
350 struct tcf_pkt_info {
351 	unsigned char *		ptr;
352 	int			nexthdr;
353 };
354 
355 #ifdef CONFIG_NET_EMATCH
356 
357 struct tcf_ematch_ops;
358 
359 /**
360  * struct tcf_ematch - extended match (ematch)
361  *
362  * @matchid: identifier to allow userspace to reidentify a match
363  * @flags: flags specifying attributes and the relation to other matches
364  * @ops: the operations lookup table of the corresponding ematch module
365  * @datalen: length of the ematch specific configuration data
366  * @data: ematch specific data
367  * @net: the network namespace
368  */
369 struct tcf_ematch {
370 	struct tcf_ematch_ops * ops;
371 	unsigned long		data;
372 	unsigned int		datalen;
373 	u16			matchid;
374 	u16			flags;
375 	struct net		*net;
376 };
377 
378 static inline int tcf_em_is_container(struct tcf_ematch *em)
379 {
380 	return !em->ops;
381 }
382 
383 static inline int tcf_em_is_simple(struct tcf_ematch *em)
384 {
385 	return em->flags & TCF_EM_SIMPLE;
386 }
387 
388 static inline int tcf_em_is_inverted(struct tcf_ematch *em)
389 {
390 	return em->flags & TCF_EM_INVERT;
391 }
392 
393 static inline int tcf_em_last_match(struct tcf_ematch *em)
394 {
395 	return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
396 }
397 
398 static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
399 {
400 	if (tcf_em_last_match(em))
401 		return 1;
402 
403 	if (result == 0 && em->flags & TCF_EM_REL_AND)
404 		return 1;
405 
406 	if (result != 0 && em->flags & TCF_EM_REL_OR)
407 		return 1;
408 
409 	return 0;
410 }
411 
412 /**
413  * struct tcf_ematch_tree - ematch tree handle
414  *
415  * @hdr: ematch tree header supplied by userspace
416  * @matches: array of ematches
417  */
418 struct tcf_ematch_tree {
419 	struct tcf_ematch_tree_hdr hdr;
420 	struct tcf_ematch *	matches;
421 
422 };
423 
424 /**
425  * struct tcf_ematch_ops - ematch module operations
426  *
427  * @kind: identifier (kind) of this ematch module
428  * @datalen: length of expected configuration data (optional)
429  * @change: called during validation (optional)
430  * @match: called during ematch tree evaluation, must return 1/0
431  * @destroy: called during destroyage (optional)
432  * @dump: called during dumping process (optional)
433  * @owner: owner, must be set to THIS_MODULE
434  * @link: link to previous/next ematch module (internal use)
435  */
436 struct tcf_ematch_ops {
437 	int			kind;
438 	int			datalen;
439 	int			(*change)(struct net *net, void *,
440 					  int, struct tcf_ematch *);
441 	int			(*match)(struct sk_buff *, struct tcf_ematch *,
442 					 struct tcf_pkt_info *);
443 	void			(*destroy)(struct tcf_ematch *);
444 	int			(*dump)(struct sk_buff *, struct tcf_ematch *);
445 	struct module		*owner;
446 	struct list_head	link;
447 };
448 
449 int tcf_em_register(struct tcf_ematch_ops *);
450 void tcf_em_unregister(struct tcf_ematch_ops *);
451 int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
452 			 struct tcf_ematch_tree *);
453 void tcf_em_tree_destroy(struct tcf_ematch_tree *);
454 int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
455 int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
456 			struct tcf_pkt_info *);
457 
458 /**
459  * tcf_em_tree_match - evaulate an ematch tree
460  *
461  * @skb: socket buffer of the packet in question
462  * @tree: ematch tree to be used for evaluation
463  * @info: packet information examined by classifier
464  *
465  * This function matches @skb against the ematch tree in @tree by going
466  * through all ematches respecting their logic relations returning
467  * as soon as the result is obvious.
468  *
469  * Returns 1 if the ematch tree as-one matches, no ematches are configured
470  * or ematch is not enabled in the kernel, otherwise 0 is returned.
471  */
472 static inline int tcf_em_tree_match(struct sk_buff *skb,
473 				    struct tcf_ematch_tree *tree,
474 				    struct tcf_pkt_info *info)
475 {
476 	if (tree->hdr.nmatches)
477 		return __tcf_em_tree_match(skb, tree, info);
478 	else
479 		return 1;
480 }
481 
482 #define MODULE_ALIAS_TCF_EMATCH(kind)	MODULE_ALIAS("ematch-kind-" __stringify(kind))
483 
484 #else /* CONFIG_NET_EMATCH */
485 
486 struct tcf_ematch_tree {
487 };
488 
489 #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
490 #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
491 #define tcf_em_tree_dump(skb, t, tlv) (0)
492 #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
493 
494 #endif /* CONFIG_NET_EMATCH */
495 
496 static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
497 {
498 	switch (layer) {
499 		case TCF_LAYER_LINK:
500 			return skb_mac_header(skb);
501 		case TCF_LAYER_NETWORK:
502 			return skb_network_header(skb);
503 		case TCF_LAYER_TRANSPORT:
504 			return skb_transport_header(skb);
505 	}
506 
507 	return NULL;
508 }
509 
510 static inline int tcf_valid_offset(const struct sk_buff *skb,
511 				   const unsigned char *ptr, const int len)
512 {
513 	return likely((ptr + len) <= skb_tail_pointer(skb) &&
514 		      ptr >= skb->head &&
515 		      (ptr <= (ptr + len)));
516 }
517 
518 static inline int
519 tcf_change_indev(struct net *net, struct nlattr *indev_tlv,
520 		 struct netlink_ext_ack *extack)
521 {
522 	char indev[IFNAMSIZ];
523 	struct net_device *dev;
524 
525 	if (nla_strscpy(indev, indev_tlv, IFNAMSIZ) < 0) {
526 		NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
527 				    "Interface name too long");
528 		return -EINVAL;
529 	}
530 	dev = __dev_get_by_name(net, indev);
531 	if (!dev) {
532 		NL_SET_ERR_MSG_ATTR(extack, indev_tlv,
533 				    "Network device not found");
534 		return -ENODEV;
535 	}
536 	return dev->ifindex;
537 }
538 
539 static inline bool
540 tcf_match_indev(struct sk_buff *skb, int ifindex)
541 {
542 	if (!ifindex)
543 		return true;
544 	if  (!skb->skb_iif)
545 		return false;
546 	return ifindex == skb->skb_iif;
547 }
548 
549 int tc_setup_offload_action(struct flow_action *flow_action,
550 			    const struct tcf_exts *exts,
551 			    struct netlink_ext_ack *extack);
552 void tc_cleanup_offload_action(struct flow_action *flow_action);
553 int tc_setup_action(struct flow_action *flow_action,
554 		    struct tc_action *actions[],
555 		    struct netlink_ext_ack *extack);
556 
557 int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type,
558 		     void *type_data, bool err_stop, bool rtnl_held);
559 int tc_setup_cb_add(struct tcf_block *block, struct tcf_proto *tp,
560 		    enum tc_setup_type type, void *type_data, bool err_stop,
561 		    u32 *flags, unsigned int *in_hw_count, bool rtnl_held);
562 int tc_setup_cb_replace(struct tcf_block *block, struct tcf_proto *tp,
563 			enum tc_setup_type type, void *type_data, bool err_stop,
564 			u32 *old_flags, unsigned int *old_in_hw_count,
565 			u32 *new_flags, unsigned int *new_in_hw_count,
566 			bool rtnl_held);
567 int tc_setup_cb_destroy(struct tcf_block *block, struct tcf_proto *tp,
568 			enum tc_setup_type type, void *type_data, bool err_stop,
569 			u32 *flags, unsigned int *in_hw_count, bool rtnl_held);
570 int tc_setup_cb_reoffload(struct tcf_block *block, struct tcf_proto *tp,
571 			  bool add, flow_setup_cb_t *cb,
572 			  enum tc_setup_type type, void *type_data,
573 			  void *cb_priv, u32 *flags, unsigned int *in_hw_count);
574 unsigned int tcf_exts_num_actions(struct tcf_exts *exts);
575 
576 #ifdef CONFIG_NET_CLS_ACT
577 int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
578 		    enum flow_block_binder_type binder_type,
579 		    struct nlattr *block_index_attr,
580 		    struct netlink_ext_ack *extack);
581 void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch);
582 int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
583 			       struct netlink_ext_ack *extack);
584 struct sk_buff *tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
585 				  struct sk_buff **to_free, int *ret);
586 int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe);
587 #else
588 static inline int tcf_qevent_init(struct tcf_qevent *qe, struct Qdisc *sch,
589 				  enum flow_block_binder_type binder_type,
590 				  struct nlattr *block_index_attr,
591 				  struct netlink_ext_ack *extack)
592 {
593 	return 0;
594 }
595 
596 static inline void tcf_qevent_destroy(struct tcf_qevent *qe, struct Qdisc *sch)
597 {
598 }
599 
600 static inline int tcf_qevent_validate_change(struct tcf_qevent *qe, struct nlattr *block_index_attr,
601 					     struct netlink_ext_ack *extack)
602 {
603 	return 0;
604 }
605 
606 static inline struct sk_buff *
607 tcf_qevent_handle(struct tcf_qevent *qe, struct Qdisc *sch, struct sk_buff *skb,
608 		  struct sk_buff **to_free, int *ret)
609 {
610 	return skb;
611 }
612 
613 static inline int tcf_qevent_dump(struct sk_buff *skb, int attr_name, struct tcf_qevent *qe)
614 {
615 	return 0;
616 }
617 #endif
618 
619 struct tc_cls_u32_knode {
620 	struct tcf_exts *exts;
621 	struct tcf_result *res;
622 	struct tc_u32_sel *sel;
623 	u32 handle;
624 	u32 val;
625 	u32 mask;
626 	u32 link_handle;
627 	u8 fshift;
628 };
629 
630 struct tc_cls_u32_hnode {
631 	u32 handle;
632 	u32 prio;
633 	unsigned int divisor;
634 };
635 
636 enum tc_clsu32_command {
637 	TC_CLSU32_NEW_KNODE,
638 	TC_CLSU32_REPLACE_KNODE,
639 	TC_CLSU32_DELETE_KNODE,
640 	TC_CLSU32_NEW_HNODE,
641 	TC_CLSU32_REPLACE_HNODE,
642 	TC_CLSU32_DELETE_HNODE,
643 };
644 
645 struct tc_cls_u32_offload {
646 	struct flow_cls_common_offload common;
647 	/* knode values */
648 	enum tc_clsu32_command command;
649 	union {
650 		struct tc_cls_u32_knode knode;
651 		struct tc_cls_u32_hnode hnode;
652 	};
653 };
654 
655 static inline bool tc_can_offload(const struct net_device *dev)
656 {
657 	return dev->features & NETIF_F_HW_TC;
658 }
659 
660 static inline bool tc_can_offload_extack(const struct net_device *dev,
661 					 struct netlink_ext_ack *extack)
662 {
663 	bool can = tc_can_offload(dev);
664 
665 	if (!can)
666 		NL_SET_ERR_MSG(extack, "TC offload is disabled on net device");
667 
668 	return can;
669 }
670 
671 static inline bool
672 tc_cls_can_offload_and_chain0(const struct net_device *dev,
673 			      struct flow_cls_common_offload *common)
674 {
675 	if (!tc_can_offload_extack(dev, common->extack))
676 		return false;
677 	if (common->chain_index) {
678 		NL_SET_ERR_MSG(common->extack,
679 			       "Driver supports only offload of chain 0");
680 		return false;
681 	}
682 	return true;
683 }
684 
685 static inline bool tc_skip_hw(u32 flags)
686 {
687 	return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
688 }
689 
690 static inline bool tc_skip_sw(u32 flags)
691 {
692 	return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
693 }
694 
695 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
696 static inline bool tc_flags_valid(u32 flags)
697 {
698 	if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW |
699 		      TCA_CLS_FLAGS_VERBOSE))
700 		return false;
701 
702 	flags &= TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW;
703 	if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
704 		return false;
705 
706 	return true;
707 }
708 
709 static inline bool tc_in_hw(u32 flags)
710 {
711 	return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
712 }
713 
714 static inline void
715 tc_cls_common_offload_init(struct flow_cls_common_offload *cls_common,
716 			   const struct tcf_proto *tp, u32 flags,
717 			   struct netlink_ext_ack *extack)
718 {
719 	cls_common->chain_index = tp->chain->index;
720 	cls_common->protocol = tp->protocol;
721 	cls_common->prio = tp->prio >> 16;
722 	if (tc_skip_sw(flags) || flags & TCA_CLS_FLAGS_VERBOSE)
723 		cls_common->extack = extack;
724 }
725 
726 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
727 static inline struct tc_skb_ext *tc_skb_ext_alloc(struct sk_buff *skb)
728 {
729 	struct tc_skb_ext *tc_skb_ext = skb_ext_add(skb, TC_SKB_EXT);
730 
731 	if (tc_skb_ext)
732 		memset(tc_skb_ext, 0, sizeof(*tc_skb_ext));
733 	return tc_skb_ext;
734 }
735 #endif
736 
737 enum tc_matchall_command {
738 	TC_CLSMATCHALL_REPLACE,
739 	TC_CLSMATCHALL_DESTROY,
740 	TC_CLSMATCHALL_STATS,
741 };
742 
743 struct tc_cls_matchall_offload {
744 	struct flow_cls_common_offload common;
745 	enum tc_matchall_command command;
746 	struct flow_rule *rule;
747 	struct flow_stats stats;
748 	unsigned long cookie;
749 };
750 
751 enum tc_clsbpf_command {
752 	TC_CLSBPF_OFFLOAD,
753 	TC_CLSBPF_STATS,
754 };
755 
756 struct tc_cls_bpf_offload {
757 	struct flow_cls_common_offload common;
758 	enum tc_clsbpf_command command;
759 	struct tcf_exts *exts;
760 	struct bpf_prog *prog;
761 	struct bpf_prog *oldprog;
762 	const char *name;
763 	bool exts_integrated;
764 };
765 
766 struct tc_mqprio_qopt_offload {
767 	/* struct tc_mqprio_qopt must always be the first element */
768 	struct tc_mqprio_qopt qopt;
769 	u16 mode;
770 	u16 shaper;
771 	u32 flags;
772 	u64 min_rate[TC_QOPT_MAX_QUEUE];
773 	u64 max_rate[TC_QOPT_MAX_QUEUE];
774 };
775 
776 /* This structure holds cookie structure that is passed from user
777  * to the kernel for actions and classifiers
778  */
779 struct tc_cookie {
780 	u8  *data;
781 	u32 len;
782 	struct rcu_head rcu;
783 };
784 
785 struct tc_qopt_offload_stats {
786 	struct gnet_stats_basic_sync *bstats;
787 	struct gnet_stats_queue *qstats;
788 };
789 
790 enum tc_mq_command {
791 	TC_MQ_CREATE,
792 	TC_MQ_DESTROY,
793 	TC_MQ_STATS,
794 	TC_MQ_GRAFT,
795 };
796 
797 struct tc_mq_opt_offload_graft_params {
798 	unsigned long queue;
799 	u32 child_handle;
800 };
801 
802 struct tc_mq_qopt_offload {
803 	enum tc_mq_command command;
804 	u32 handle;
805 	union {
806 		struct tc_qopt_offload_stats stats;
807 		struct tc_mq_opt_offload_graft_params graft_params;
808 	};
809 };
810 
811 enum tc_htb_command {
812 	/* Root */
813 	TC_HTB_CREATE, /* Initialize HTB offload. */
814 	TC_HTB_DESTROY, /* Destroy HTB offload. */
815 
816 	/* Classes */
817 	/* Allocate qid and create leaf. */
818 	TC_HTB_LEAF_ALLOC_QUEUE,
819 	/* Convert leaf to inner, preserve and return qid, create new leaf. */
820 	TC_HTB_LEAF_TO_INNER,
821 	/* Delete leaf, while siblings remain. */
822 	TC_HTB_LEAF_DEL,
823 	/* Delete leaf, convert parent to leaf, preserving qid. */
824 	TC_HTB_LEAF_DEL_LAST,
825 	/* TC_HTB_LEAF_DEL_LAST, but delete driver data on hardware errors. */
826 	TC_HTB_LEAF_DEL_LAST_FORCE,
827 	/* Modify parameters of a node. */
828 	TC_HTB_NODE_MODIFY,
829 
830 	/* Class qdisc */
831 	TC_HTB_LEAF_QUERY_QUEUE, /* Query qid by classid. */
832 };
833 
834 struct tc_htb_qopt_offload {
835 	struct netlink_ext_ack *extack;
836 	enum tc_htb_command command;
837 	u32 parent_classid;
838 	u16 classid;
839 	u16 qid;
840 	u64 rate;
841 	u64 ceil;
842 };
843 
844 #define TC_HTB_CLASSID_ROOT U32_MAX
845 
846 enum tc_red_command {
847 	TC_RED_REPLACE,
848 	TC_RED_DESTROY,
849 	TC_RED_STATS,
850 	TC_RED_XSTATS,
851 	TC_RED_GRAFT,
852 };
853 
854 struct tc_red_qopt_offload_params {
855 	u32 min;
856 	u32 max;
857 	u32 probability;
858 	u32 limit;
859 	bool is_ecn;
860 	bool is_harddrop;
861 	bool is_nodrop;
862 	struct gnet_stats_queue *qstats;
863 };
864 
865 struct tc_red_qopt_offload {
866 	enum tc_red_command command;
867 	u32 handle;
868 	u32 parent;
869 	union {
870 		struct tc_red_qopt_offload_params set;
871 		struct tc_qopt_offload_stats stats;
872 		struct red_stats *xstats;
873 		u32 child_handle;
874 	};
875 };
876 
877 enum tc_gred_command {
878 	TC_GRED_REPLACE,
879 	TC_GRED_DESTROY,
880 	TC_GRED_STATS,
881 };
882 
883 struct tc_gred_vq_qopt_offload_params {
884 	bool present;
885 	u32 limit;
886 	u32 prio;
887 	u32 min;
888 	u32 max;
889 	bool is_ecn;
890 	bool is_harddrop;
891 	u32 probability;
892 	/* Only need backlog, see struct tc_prio_qopt_offload_params */
893 	u32 *backlog;
894 };
895 
896 struct tc_gred_qopt_offload_params {
897 	bool grio_on;
898 	bool wred_on;
899 	unsigned int dp_cnt;
900 	unsigned int dp_def;
901 	struct gnet_stats_queue *qstats;
902 	struct tc_gred_vq_qopt_offload_params tab[MAX_DPs];
903 };
904 
905 struct tc_gred_qopt_offload_stats {
906 	struct gnet_stats_basic_sync bstats[MAX_DPs];
907 	struct gnet_stats_queue qstats[MAX_DPs];
908 	struct red_stats *xstats[MAX_DPs];
909 };
910 
911 struct tc_gred_qopt_offload {
912 	enum tc_gred_command command;
913 	u32 handle;
914 	u32 parent;
915 	union {
916 		struct tc_gred_qopt_offload_params set;
917 		struct tc_gred_qopt_offload_stats stats;
918 	};
919 };
920 
921 enum tc_prio_command {
922 	TC_PRIO_REPLACE,
923 	TC_PRIO_DESTROY,
924 	TC_PRIO_STATS,
925 	TC_PRIO_GRAFT,
926 };
927 
928 struct tc_prio_qopt_offload_params {
929 	int bands;
930 	u8 priomap[TC_PRIO_MAX + 1];
931 	/* At the point of un-offloading the Qdisc, the reported backlog and
932 	 * qlen need to be reduced by the portion that is in HW.
933 	 */
934 	struct gnet_stats_queue *qstats;
935 };
936 
937 struct tc_prio_qopt_offload_graft_params {
938 	u8 band;
939 	u32 child_handle;
940 };
941 
942 struct tc_prio_qopt_offload {
943 	enum tc_prio_command command;
944 	u32 handle;
945 	u32 parent;
946 	union {
947 		struct tc_prio_qopt_offload_params replace_params;
948 		struct tc_qopt_offload_stats stats;
949 		struct tc_prio_qopt_offload_graft_params graft_params;
950 	};
951 };
952 
953 enum tc_root_command {
954 	TC_ROOT_GRAFT,
955 };
956 
957 struct tc_root_qopt_offload {
958 	enum tc_root_command command;
959 	u32 handle;
960 	bool ingress;
961 };
962 
963 enum tc_ets_command {
964 	TC_ETS_REPLACE,
965 	TC_ETS_DESTROY,
966 	TC_ETS_STATS,
967 	TC_ETS_GRAFT,
968 };
969 
970 struct tc_ets_qopt_offload_replace_params {
971 	unsigned int bands;
972 	u8 priomap[TC_PRIO_MAX + 1];
973 	unsigned int quanta[TCQ_ETS_MAX_BANDS];	/* 0 for strict bands. */
974 	unsigned int weights[TCQ_ETS_MAX_BANDS];
975 	struct gnet_stats_queue *qstats;
976 };
977 
978 struct tc_ets_qopt_offload_graft_params {
979 	u8 band;
980 	u32 child_handle;
981 };
982 
983 struct tc_ets_qopt_offload {
984 	enum tc_ets_command command;
985 	u32 handle;
986 	u32 parent;
987 	union {
988 		struct tc_ets_qopt_offload_replace_params replace_params;
989 		struct tc_qopt_offload_stats stats;
990 		struct tc_ets_qopt_offload_graft_params graft_params;
991 	};
992 };
993 
994 enum tc_tbf_command {
995 	TC_TBF_REPLACE,
996 	TC_TBF_DESTROY,
997 	TC_TBF_STATS,
998 	TC_TBF_GRAFT,
999 };
1000 
1001 struct tc_tbf_qopt_offload_replace_params {
1002 	struct psched_ratecfg rate;
1003 	u32 max_size;
1004 	struct gnet_stats_queue *qstats;
1005 };
1006 
1007 struct tc_tbf_qopt_offload {
1008 	enum tc_tbf_command command;
1009 	u32 handle;
1010 	u32 parent;
1011 	union {
1012 		struct tc_tbf_qopt_offload_replace_params replace_params;
1013 		struct tc_qopt_offload_stats stats;
1014 		u32 child_handle;
1015 	};
1016 };
1017 
1018 enum tc_fifo_command {
1019 	TC_FIFO_REPLACE,
1020 	TC_FIFO_DESTROY,
1021 	TC_FIFO_STATS,
1022 };
1023 
1024 struct tc_fifo_qopt_offload {
1025 	enum tc_fifo_command command;
1026 	u32 handle;
1027 	u32 parent;
1028 	union {
1029 		struct tc_qopt_offload_stats stats;
1030 	};
1031 };
1032 
1033 #ifdef CONFIG_NET_CLS_ACT
1034 DECLARE_STATIC_KEY_FALSE(tc_skb_ext_tc);
1035 void tc_skb_ext_tc_enable(void);
1036 void tc_skb_ext_tc_disable(void);
1037 #define tc_skb_ext_tc_enabled() static_branch_unlikely(&tc_skb_ext_tc)
1038 #else /* CONFIG_NET_CLS_ACT */
1039 static inline void tc_skb_ext_tc_enable(void) { }
1040 static inline void tc_skb_ext_tc_disable(void) { }
1041 #define tc_skb_ext_tc_enabled() false
1042 #endif
1043 
1044 #endif
1045