xref: /openbmc/linux/include/net/pkt_cls.h (revision 79a93295)
1 #ifndef __NET_PKT_CLS_H
2 #define __NET_PKT_CLS_H
3 
4 #include <linux/pkt_cls.h>
5 #include <net/sch_generic.h>
6 #include <net/act_api.h>
7 
8 /* Basic packet classifier frontend definitions. */
9 
10 struct tcf_walker {
11 	int	stop;
12 	int	skip;
13 	int	count;
14 	int	(*fn)(struct tcf_proto *, unsigned long node, struct tcf_walker *);
15 };
16 
17 int register_tcf_proto_ops(struct tcf_proto_ops *ops);
18 int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
19 
20 static inline unsigned long
21 __cls_set_class(unsigned long *clp, unsigned long cl)
22 {
23 	return xchg(clp, cl);
24 }
25 
26 static inline unsigned long
27 cls_set_class(struct tcf_proto *tp, unsigned long *clp,
28 	unsigned long cl)
29 {
30 	unsigned long old_cl;
31 
32 	tcf_tree_lock(tp);
33 	old_cl = __cls_set_class(clp, cl);
34 	tcf_tree_unlock(tp);
35 
36 	return old_cl;
37 }
38 
39 static inline void
40 tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
41 {
42 	unsigned long cl;
43 
44 	cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
45 	cl = cls_set_class(tp, &r->class, cl);
46 	if (cl)
47 		tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
48 }
49 
50 static inline void
51 tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
52 {
53 	unsigned long cl;
54 
55 	if ((cl = __cls_set_class(&r->class, 0)) != 0)
56 		tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
57 }
58 
59 struct tcf_exts {
60 #ifdef CONFIG_NET_CLS_ACT
61 	__u32	type; /* for backward compat(TCA_OLD_COMPAT) */
62 	int nr_actions;
63 	struct tc_action **actions;
64 #endif
65 	/* Map to export classifier specific extension TLV types to the
66 	 * generic extensions API. Unsupported extensions must be set to 0.
67 	 */
68 	int action;
69 	int police;
70 };
71 
72 static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
73 {
74 #ifdef CONFIG_NET_CLS_ACT
75 	exts->type = 0;
76 	exts->nr_actions = 0;
77 	exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
78 				GFP_KERNEL);
79 	if (!exts->actions)
80 		return -ENOMEM;
81 #endif
82 	exts->action = action;
83 	exts->police = police;
84 	return 0;
85 }
86 
87 /**
88  * tcf_exts_is_predicative - check if a predicative extension is present
89  * @exts: tc filter extensions handle
90  *
91  * Returns 1 if a predicative extension is present, i.e. an extension which
92  * might cause further actions and thus overrule the regular tcf_result.
93  */
94 static inline int
95 tcf_exts_is_predicative(struct tcf_exts *exts)
96 {
97 #ifdef CONFIG_NET_CLS_ACT
98 	return exts->nr_actions;
99 #else
100 	return 0;
101 #endif
102 }
103 
104 /**
105  * tcf_exts_is_available - check if at least one extension is present
106  * @exts: tc filter extensions handle
107  *
108  * Returns 1 if at least one extension is present.
109  */
110 static inline int
111 tcf_exts_is_available(struct tcf_exts *exts)
112 {
113 	/* All non-predicative extensions must be added here. */
114 	return tcf_exts_is_predicative(exts);
115 }
116 
117 static inline void tcf_exts_to_list(const struct tcf_exts *exts,
118 				    struct list_head *actions)
119 {
120 #ifdef CONFIG_NET_CLS_ACT
121 	int i;
122 
123 	for (i = 0; i < exts->nr_actions; i++) {
124 		struct tc_action *a = exts->actions[i];
125 
126 		list_add_tail(&a->list, actions);
127 	}
128 #endif
129 }
130 
131 /**
132  * tcf_exts_exec - execute tc filter extensions
133  * @skb: socket buffer
134  * @exts: tc filter extensions handle
135  * @res: desired result
136  *
137  * Executes all configured extensions. Returns 0 on a normal execution,
138  * a negative number if the filter must be considered unmatched or
139  * a positive action code (TC_ACT_*) which must be returned to the
140  * underlying layer.
141  */
142 static inline int
143 tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
144 	       struct tcf_result *res)
145 {
146 #ifdef CONFIG_NET_CLS_ACT
147 	if (exts->nr_actions)
148 		return tcf_action_exec(skb, exts->actions, exts->nr_actions,
149 				       res);
150 #endif
151 	return 0;
152 }
153 
154 #ifdef CONFIG_NET_CLS_ACT
155 
156 #define tc_no_actions(_exts)  ((_exts)->nr_actions == 0)
157 #define tc_single_action(_exts) ((_exts)->nr_actions == 1)
158 
159 #else /* CONFIG_NET_CLS_ACT */
160 
161 #define tc_no_actions(_exts) true
162 #define tc_single_action(_exts) false
163 
164 #endif /* CONFIG_NET_CLS_ACT */
165 
166 int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
167 		      struct nlattr **tb, struct nlattr *rate_tlv,
168 		      struct tcf_exts *exts, bool ovr);
169 void tcf_exts_destroy(struct tcf_exts *exts);
170 void tcf_exts_change(struct tcf_proto *tp, struct tcf_exts *dst,
171 		     struct tcf_exts *src);
172 int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
173 int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
174 int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
175 		     struct net_device **hw_dev);
176 
177 /**
178  * struct tcf_pkt_info - packet information
179  */
180 struct tcf_pkt_info {
181 	unsigned char *		ptr;
182 	int			nexthdr;
183 };
184 
185 #ifdef CONFIG_NET_EMATCH
186 
187 struct tcf_ematch_ops;
188 
189 /**
190  * struct tcf_ematch - extended match (ematch)
191  *
192  * @matchid: identifier to allow userspace to reidentify a match
193  * @flags: flags specifying attributes and the relation to other matches
194  * @ops: the operations lookup table of the corresponding ematch module
195  * @datalen: length of the ematch specific configuration data
196  * @data: ematch specific data
197  */
198 struct tcf_ematch {
199 	struct tcf_ematch_ops * ops;
200 	unsigned long		data;
201 	unsigned int		datalen;
202 	u16			matchid;
203 	u16			flags;
204 	struct net		*net;
205 };
206 
207 static inline int tcf_em_is_container(struct tcf_ematch *em)
208 {
209 	return !em->ops;
210 }
211 
212 static inline int tcf_em_is_simple(struct tcf_ematch *em)
213 {
214 	return em->flags & TCF_EM_SIMPLE;
215 }
216 
217 static inline int tcf_em_is_inverted(struct tcf_ematch *em)
218 {
219 	return em->flags & TCF_EM_INVERT;
220 }
221 
222 static inline int tcf_em_last_match(struct tcf_ematch *em)
223 {
224 	return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
225 }
226 
227 static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
228 {
229 	if (tcf_em_last_match(em))
230 		return 1;
231 
232 	if (result == 0 && em->flags & TCF_EM_REL_AND)
233 		return 1;
234 
235 	if (result != 0 && em->flags & TCF_EM_REL_OR)
236 		return 1;
237 
238 	return 0;
239 }
240 
241 /**
242  * struct tcf_ematch_tree - ematch tree handle
243  *
244  * @hdr: ematch tree header supplied by userspace
245  * @matches: array of ematches
246  */
247 struct tcf_ematch_tree {
248 	struct tcf_ematch_tree_hdr hdr;
249 	struct tcf_ematch *	matches;
250 
251 };
252 
253 /**
254  * struct tcf_ematch_ops - ematch module operations
255  *
256  * @kind: identifier (kind) of this ematch module
257  * @datalen: length of expected configuration data (optional)
258  * @change: called during validation (optional)
259  * @match: called during ematch tree evaluation, must return 1/0
260  * @destroy: called during destroyage (optional)
261  * @dump: called during dumping process (optional)
262  * @owner: owner, must be set to THIS_MODULE
263  * @link: link to previous/next ematch module (internal use)
264  */
265 struct tcf_ematch_ops {
266 	int			kind;
267 	int			datalen;
268 	int			(*change)(struct net *net, void *,
269 					  int, struct tcf_ematch *);
270 	int			(*match)(struct sk_buff *, struct tcf_ematch *,
271 					 struct tcf_pkt_info *);
272 	void			(*destroy)(struct tcf_ematch *);
273 	int			(*dump)(struct sk_buff *, struct tcf_ematch *);
274 	struct module		*owner;
275 	struct list_head	link;
276 };
277 
278 int tcf_em_register(struct tcf_ematch_ops *);
279 void tcf_em_unregister(struct tcf_ematch_ops *);
280 int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
281 			 struct tcf_ematch_tree *);
282 void tcf_em_tree_destroy(struct tcf_ematch_tree *);
283 int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
284 int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
285 			struct tcf_pkt_info *);
286 
287 /**
288  * tcf_em_tree_change - replace ematch tree of a running classifier
289  *
290  * @tp: classifier kind handle
291  * @dst: destination ematch tree variable
292  * @src: source ematch tree (temporary tree from tcf_em_tree_validate)
293  *
294  * This functions replaces the ematch tree in @dst with the ematch
295  * tree in @src. The classifier in charge of the ematch tree may be
296  * running.
297  */
298 static inline void tcf_em_tree_change(struct tcf_proto *tp,
299 				      struct tcf_ematch_tree *dst,
300 				      struct tcf_ematch_tree *src)
301 {
302 	tcf_tree_lock(tp);
303 	memcpy(dst, src, sizeof(*dst));
304 	tcf_tree_unlock(tp);
305 }
306 
307 /**
308  * tcf_em_tree_match - evaulate an ematch tree
309  *
310  * @skb: socket buffer of the packet in question
311  * @tree: ematch tree to be used for evaluation
312  * @info: packet information examined by classifier
313  *
314  * This function matches @skb against the ematch tree in @tree by going
315  * through all ematches respecting their logic relations returning
316  * as soon as the result is obvious.
317  *
318  * Returns 1 if the ematch tree as-one matches, no ematches are configured
319  * or ematch is not enabled in the kernel, otherwise 0 is returned.
320  */
321 static inline int tcf_em_tree_match(struct sk_buff *skb,
322 				    struct tcf_ematch_tree *tree,
323 				    struct tcf_pkt_info *info)
324 {
325 	if (tree->hdr.nmatches)
326 		return __tcf_em_tree_match(skb, tree, info);
327 	else
328 		return 1;
329 }
330 
331 #define MODULE_ALIAS_TCF_EMATCH(kind)	MODULE_ALIAS("ematch-kind-" __stringify(kind))
332 
333 #else /* CONFIG_NET_EMATCH */
334 
335 struct tcf_ematch_tree {
336 };
337 
338 #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
339 #define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
340 #define tcf_em_tree_dump(skb, t, tlv) (0)
341 #define tcf_em_tree_change(tp, dst, src) do { } while(0)
342 #define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
343 
344 #endif /* CONFIG_NET_EMATCH */
345 
346 static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
347 {
348 	switch (layer) {
349 		case TCF_LAYER_LINK:
350 			return skb->data;
351 		case TCF_LAYER_NETWORK:
352 			return skb_network_header(skb);
353 		case TCF_LAYER_TRANSPORT:
354 			return skb_transport_header(skb);
355 	}
356 
357 	return NULL;
358 }
359 
360 static inline int tcf_valid_offset(const struct sk_buff *skb,
361 				   const unsigned char *ptr, const int len)
362 {
363 	return likely((ptr + len) <= skb_tail_pointer(skb) &&
364 		      ptr >= skb->head &&
365 		      (ptr <= (ptr + len)));
366 }
367 
368 #ifdef CONFIG_NET_CLS_IND
369 #include <net/net_namespace.h>
370 
371 static inline int
372 tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
373 {
374 	char indev[IFNAMSIZ];
375 	struct net_device *dev;
376 
377 	if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
378 		return -EINVAL;
379 	dev = __dev_get_by_name(net, indev);
380 	if (!dev)
381 		return -ENODEV;
382 	return dev->ifindex;
383 }
384 
385 static inline bool
386 tcf_match_indev(struct sk_buff *skb, int ifindex)
387 {
388 	if (!ifindex)
389 		return true;
390 	if  (!skb->skb_iif)
391 		return false;
392 	return ifindex == skb->skb_iif;
393 }
394 #endif /* CONFIG_NET_CLS_IND */
395 
396 struct tc_cls_u32_knode {
397 	struct tcf_exts *exts;
398 	struct tc_u32_sel *sel;
399 	u32 handle;
400 	u32 val;
401 	u32 mask;
402 	u32 link_handle;
403 	u8 fshift;
404 };
405 
406 struct tc_cls_u32_hnode {
407 	u32 handle;
408 	u32 prio;
409 	unsigned int divisor;
410 };
411 
412 enum tc_clsu32_command {
413 	TC_CLSU32_NEW_KNODE,
414 	TC_CLSU32_REPLACE_KNODE,
415 	TC_CLSU32_DELETE_KNODE,
416 	TC_CLSU32_NEW_HNODE,
417 	TC_CLSU32_REPLACE_HNODE,
418 	TC_CLSU32_DELETE_HNODE,
419 };
420 
421 struct tc_cls_u32_offload {
422 	/* knode values */
423 	enum tc_clsu32_command command;
424 	union {
425 		struct tc_cls_u32_knode knode;
426 		struct tc_cls_u32_hnode hnode;
427 	};
428 };
429 
430 static inline bool tc_can_offload(const struct net_device *dev,
431 				  const struct tcf_proto *tp)
432 {
433 	const struct Qdisc *sch = tp->q;
434 	const struct Qdisc_class_ops *cops = sch->ops->cl_ops;
435 
436 	if (!(dev->features & NETIF_F_HW_TC))
437 		return false;
438 	if (!dev->netdev_ops->ndo_setup_tc)
439 		return false;
440 	if (cops && cops->tcf_cl_offload)
441 		return cops->tcf_cl_offload(tp->classid);
442 
443 	return true;
444 }
445 
446 static inline bool tc_skip_hw(u32 flags)
447 {
448 	return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
449 }
450 
451 static inline bool tc_should_offload(const struct net_device *dev,
452 				     const struct tcf_proto *tp, u32 flags)
453 {
454 	if (tc_skip_hw(flags))
455 		return false;
456 	return tc_can_offload(dev, tp);
457 }
458 
459 static inline bool tc_skip_sw(u32 flags)
460 {
461 	return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
462 }
463 
464 /* SKIP_HW and SKIP_SW are mutually exclusive flags. */
465 static inline bool tc_flags_valid(u32 flags)
466 {
467 	if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
468 		return false;
469 
470 	if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
471 		return false;
472 
473 	return true;
474 }
475 
476 enum tc_fl_command {
477 	TC_CLSFLOWER_REPLACE,
478 	TC_CLSFLOWER_DESTROY,
479 	TC_CLSFLOWER_STATS,
480 };
481 
482 struct tc_cls_flower_offload {
483 	enum tc_fl_command command;
484 	unsigned long cookie;
485 	struct flow_dissector *dissector;
486 	struct fl_flow_key *mask;
487 	struct fl_flow_key *key;
488 	struct tcf_exts *exts;
489 };
490 
491 enum tc_matchall_command {
492 	TC_CLSMATCHALL_REPLACE,
493 	TC_CLSMATCHALL_DESTROY,
494 };
495 
496 struct tc_cls_matchall_offload {
497 	enum tc_matchall_command command;
498 	struct tcf_exts *exts;
499 	unsigned long cookie;
500 };
501 
502 enum tc_clsbpf_command {
503 	TC_CLSBPF_ADD,
504 	TC_CLSBPF_REPLACE,
505 	TC_CLSBPF_DESTROY,
506 	TC_CLSBPF_STATS,
507 };
508 
509 struct tc_cls_bpf_offload {
510 	enum tc_clsbpf_command command;
511 	struct tcf_exts *exts;
512 	struct bpf_prog *prog;
513 	const char *name;
514 	bool exts_integrated;
515 	u32 gen_flags;
516 };
517 
518 #endif
519