]> git.proxmox.com Git - mirror_ubuntu-disco-kernel.git/blame - include/net/pkt_cls.h
License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[mirror_ubuntu-disco-kernel.git] / include / net / pkt_cls.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef __NET_PKT_CLS_H
3#define __NET_PKT_CLS_H
4
5#include <linux/pkt_cls.h>
6#include <net/sch_generic.h>
7#include <net/act_api.h>
8
9/* Basic packet classifier frontend definitions. */
10
fd2c3ef7 11struct tcf_walker {
1da177e4
LT
12 int stop;
13 int skip;
14 int count;
8113c095 15 int (*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
1da177e4
LT
16};
17
5c15257f
JP
18int register_tcf_proto_ops(struct tcf_proto_ops *ops);
19int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
1da177e4 20
8ae70032 21#ifdef CONFIG_NET_CLS
367a8ce8
WC
22struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
23 bool create);
5bc17018 24void tcf_chain_put(struct tcf_chain *chain);
6529eaba
JP
25int tcf_block_get(struct tcf_block **p_block,
26 struct tcf_proto __rcu **p_filter_chain);
27void tcf_block_put(struct tcf_block *block);
87d83093
JP
28int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
29 struct tcf_result *res, bool compat_mode);
30
8ae70032 31#else
6529eaba
JP
32static inline
33int tcf_block_get(struct tcf_block **p_block,
34 struct tcf_proto __rcu **p_filter_chain)
35{
36 return 0;
37}
38
39static inline void tcf_block_put(struct tcf_block *block)
8ae70032
JP
40{
41}
87d83093
JP
42
43static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
44 struct tcf_result *res, bool compat_mode)
45{
46 return TC_ACT_UNSPEC;
47}
8ae70032 48#endif
cf1facda 49
1da177e4
LT
50static inline unsigned long
51__cls_set_class(unsigned long *clp, unsigned long cl)
52{
a0efb80c 53 return xchg(clp, cl);
1da177e4
LT
54}
55
56static inline unsigned long
57cls_set_class(struct tcf_proto *tp, unsigned long *clp,
58 unsigned long cl)
59{
60 unsigned long old_cl;
61
62 tcf_tree_lock(tp);
63 old_cl = __cls_set_class(clp, cl);
64 tcf_tree_unlock(tp);
65
66 return old_cl;
67}
68
69static inline void
70tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
71{
72 unsigned long cl;
73
74 cl = tp->q->ops->cl_ops->bind_tcf(tp->q, base, r->classid);
75 cl = cls_set_class(tp, &r->class, cl);
76 if (cl)
77 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
78}
79
80static inline void
81tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
82{
83 unsigned long cl;
84
85 if ((cl = __cls_set_class(&r->class, 0)) != 0)
86 tp->q->ops->cl_ops->unbind_tcf(tp->q, cl);
87}
88
fd2c3ef7 89struct tcf_exts {
1da177e4 90#ifdef CONFIG_NET_CLS_ACT
33be6271 91 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
22dc13c8
WC
92 int nr_actions;
93 struct tc_action **actions;
1da177e4 94#endif
5da57f42
WC
95 /* Map to export classifier specific extension TLV types to the
96 * generic extensions API. Unsupported extensions must be set to 0.
97 */
1da177e4
LT
98 int action;
99 int police;
100};
101
b9a24bb7 102static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
33be6271
WC
103{
104#ifdef CONFIG_NET_CLS_ACT
5da57f42 105 exts->type = 0;
22dc13c8
WC
106 exts->nr_actions = 0;
107 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
108 GFP_KERNEL);
b9a24bb7
WC
109 if (!exts->actions)
110 return -ENOMEM;
33be6271 111#endif
5da57f42
WC
112 exts->action = action;
113 exts->police = police;
b9a24bb7 114 return 0;
33be6271
WC
115}
116
22dc13c8
WC
117static 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
fa5effe7 126 list_add_tail(&a->list, actions);
22dc13c8
WC
127 }
128#endif
129}
130
d897a638
JK
131static inline void
132tcf_exts_stats_update(const struct tcf_exts *exts,
133 u64 bytes, u64 packets, u64 lastuse)
134{
135#ifdef CONFIG_NET_CLS_ACT
136 int i;
137
138 preempt_disable();
139
140 for (i = 0; i < exts->nr_actions; i++) {
141 struct tc_action *a = exts->actions[i];
142
143 tcf_action_stats_update(a, bytes, packets, lastuse);
144 }
145
146 preempt_enable();
147#endif
148}
149
3bcc0cec
JP
150/**
151 * tcf_exts_has_actions - check if at least one action is present
152 * @exts: tc filter extensions handle
153 *
154 * Returns true if at least one action is present.
155 */
156static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
157{
2734437e 158#ifdef CONFIG_NET_CLS_ACT
3bcc0cec
JP
159 return exts->nr_actions;
160#else
161 return false;
162#endif
163}
2734437e 164
3bcc0cec
JP
165/**
166 * tcf_exts_has_one_action - check if exactly one action is present
167 * @exts: tc filter extensions handle
168 *
169 * Returns true if exactly one action is present.
170 */
171static inline bool tcf_exts_has_one_action(struct tcf_exts *exts)
172{
173#ifdef CONFIG_NET_CLS_ACT
174 return exts->nr_actions == 1;
175#else
176 return false;
177#endif
178}
2734437e 179
af69afc5
JP
180/**
181 * tcf_exts_exec - execute tc filter extensions
182 * @skb: socket buffer
183 * @exts: tc filter extensions handle
184 * @res: desired result
185 *
af089e70 186 * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
af69afc5
JP
187 * a negative number if the filter must be considered unmatched or
188 * a positive action code (TC_ACT_*) which must be returned to the
189 * underlying layer.
190 */
191static inline int
192tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
193 struct tcf_result *res)
194{
195#ifdef CONFIG_NET_CLS_ACT
ec1a9cca 196 return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
af69afc5 197#endif
af089e70 198 return TC_ACT_OK;
af69afc5
JP
199}
200
5c15257f
JP
201int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
202 struct nlattr **tb, struct nlattr *rate_tlv,
2f7ef2f8 203 struct tcf_exts *exts, bool ovr);
18d0264f 204void tcf_exts_destroy(struct tcf_exts *exts);
9b0d4446 205void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
5da57f42
WC
206int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
207int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
7091d8c7
HHZ
208int tcf_exts_get_dev(struct net_device *dev, struct tcf_exts *exts,
209 struct net_device **hw_dev);
1da177e4
LT
210
211/**
212 * struct tcf_pkt_info - packet information
213 */
fd2c3ef7 214struct tcf_pkt_info {
1da177e4
LT
215 unsigned char * ptr;
216 int nexthdr;
217};
218
219#ifdef CONFIG_NET_EMATCH
220
221struct tcf_ematch_ops;
222
223/**
224 * struct tcf_ematch - extended match (ematch)
225 *
226 * @matchid: identifier to allow userspace to reidentify a match
227 * @flags: flags specifying attributes and the relation to other matches
228 * @ops: the operations lookup table of the corresponding ematch module
229 * @datalen: length of the ematch specific configuration data
230 * @data: ematch specific data
231 */
fd2c3ef7 232struct tcf_ematch {
1da177e4
LT
233 struct tcf_ematch_ops * ops;
234 unsigned long data;
235 unsigned int datalen;
236 u16 matchid;
237 u16 flags;
82a470f1 238 struct net *net;
1da177e4
LT
239};
240
241static inline int tcf_em_is_container(struct tcf_ematch *em)
242{
243 return !em->ops;
244}
245
246static inline int tcf_em_is_simple(struct tcf_ematch *em)
247{
248 return em->flags & TCF_EM_SIMPLE;
249}
250
251static inline int tcf_em_is_inverted(struct tcf_ematch *em)
252{
253 return em->flags & TCF_EM_INVERT;
254}
255
256static inline int tcf_em_last_match(struct tcf_ematch *em)
257{
258 return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
259}
260
261static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
262{
263 if (tcf_em_last_match(em))
264 return 1;
265
266 if (result == 0 && em->flags & TCF_EM_REL_AND)
267 return 1;
268
269 if (result != 0 && em->flags & TCF_EM_REL_OR)
270 return 1;
271
272 return 0;
273}
274
275/**
276 * struct tcf_ematch_tree - ematch tree handle
277 *
278 * @hdr: ematch tree header supplied by userspace
279 * @matches: array of ematches
280 */
fd2c3ef7 281struct tcf_ematch_tree {
1da177e4
LT
282 struct tcf_ematch_tree_hdr hdr;
283 struct tcf_ematch * matches;
284
285};
286
287/**
288 * struct tcf_ematch_ops - ematch module operations
289 *
290 * @kind: identifier (kind) of this ematch module
291 * @datalen: length of expected configuration data (optional)
292 * @change: called during validation (optional)
293 * @match: called during ematch tree evaluation, must return 1/0
294 * @destroy: called during destroyage (optional)
295 * @dump: called during dumping process (optional)
296 * @owner: owner, must be set to THIS_MODULE
297 * @link: link to previous/next ematch module (internal use)
298 */
fd2c3ef7 299struct tcf_ematch_ops {
1da177e4
LT
300 int kind;
301 int datalen;
82a470f1 302 int (*change)(struct net *net, void *,
1da177e4
LT
303 int, struct tcf_ematch *);
304 int (*match)(struct sk_buff *, struct tcf_ematch *,
305 struct tcf_pkt_info *);
82a470f1 306 void (*destroy)(struct tcf_ematch *);
1da177e4
LT
307 int (*dump)(struct sk_buff *, struct tcf_ematch *);
308 struct module *owner;
309 struct list_head link;
310};
311
5c15257f
JP
312int tcf_em_register(struct tcf_ematch_ops *);
313void tcf_em_unregister(struct tcf_ematch_ops *);
314int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
315 struct tcf_ematch_tree *);
82a470f1 316void tcf_em_tree_destroy(struct tcf_ematch_tree *);
5c15257f
JP
317int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
318int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
319 struct tcf_pkt_info *);
1da177e4 320
1da177e4
LT
321/**
322 * tcf_em_tree_match - evaulate an ematch tree
323 *
324 * @skb: socket buffer of the packet in question
325 * @tree: ematch tree to be used for evaluation
326 * @info: packet information examined by classifier
327 *
328 * This function matches @skb against the ematch tree in @tree by going
329 * through all ematches respecting their logic relations returning
330 * as soon as the result is obvious.
331 *
332 * Returns 1 if the ematch tree as-one matches, no ematches are configured
333 * or ematch is not enabled in the kernel, otherwise 0 is returned.
334 */
335static inline int tcf_em_tree_match(struct sk_buff *skb,
336 struct tcf_ematch_tree *tree,
337 struct tcf_pkt_info *info)
338{
339 if (tree->hdr.nmatches)
340 return __tcf_em_tree_match(skb, tree, info);
341 else
342 return 1;
343}
344
db3d99c0
PM
345#define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
346
1da177e4
LT
347#else /* CONFIG_NET_EMATCH */
348
fd2c3ef7 349struct tcf_ematch_tree {
1da177e4
LT
350};
351
352#define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
82a470f1 353#define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
1da177e4 354#define tcf_em_tree_dump(skb, t, tlv) (0)
1da177e4
LT
355#define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
356
357#endif /* CONFIG_NET_EMATCH */
358
359static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
360{
361 switch (layer) {
362 case TCF_LAYER_LINK:
363 return skb->data;
364 case TCF_LAYER_NETWORK:
d56f90a7 365 return skb_network_header(skb);
1da177e4 366 case TCF_LAYER_TRANSPORT:
9c70220b 367 return skb_transport_header(skb);
1da177e4
LT
368 }
369
370 return NULL;
371}
372
eddc9ec5
ACM
373static inline int tcf_valid_offset(const struct sk_buff *skb,
374 const unsigned char *ptr, const int len)
1da177e4 375{
da521b2c
DM
376 return likely((ptr + len) <= skb_tail_pointer(skb) &&
377 ptr >= skb->head &&
378 (ptr <= (ptr + len)));
1da177e4
LT
379}
380
381#ifdef CONFIG_NET_CLS_IND
0eeb8ffc
DL
382#include <net/net_namespace.h>
383
1da177e4 384static inline int
2519a602 385tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
1da177e4 386{
2519a602
WC
387 char indev[IFNAMSIZ];
388 struct net_device *dev;
389
add93b61 390 if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
1da177e4 391 return -EINVAL;
2519a602
WC
392 dev = __dev_get_by_name(net, indev);
393 if (!dev)
394 return -ENODEV;
395 return dev->ifindex;
1da177e4
LT
396}
397
2519a602
WC
398static inline bool
399tcf_match_indev(struct sk_buff *skb, int ifindex)
1da177e4 400{
2519a602
WC
401 if (!ifindex)
402 return true;
403 if (!skb->skb_iif)
404 return false;
405 return ifindex == skb->skb_iif;
1da177e4
LT
406}
407#endif /* CONFIG_NET_CLS_IND */
408
5fd9fc4e 409struct tc_cls_common_offload {
5fd9fc4e
JP
410 u32 chain_index;
411 __be16 protocol;
d7c1c8d2 412 u32 prio;
7690f2a5 413 u32 classid;
5fd9fc4e
JP
414};
415
416static inline void
417tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common,
418 const struct tcf_proto *tp)
419{
5fd9fc4e
JP
420 cls_common->chain_index = tp->chain->index;
421 cls_common->protocol = tp->protocol;
d7c1c8d2 422 cls_common->prio = tp->prio;
7690f2a5 423 cls_common->classid = tp->classid;
5fd9fc4e
JP
424}
425
a1b7c5fd
JF
426struct tc_cls_u32_knode {
427 struct tcf_exts *exts;
e014860e 428 struct tc_u32_sel *sel;
a1b7c5fd
JF
429 u32 handle;
430 u32 val;
431 u32 mask;
432 u32 link_handle;
e014860e 433 u8 fshift;
a1b7c5fd
JF
434};
435
436struct tc_cls_u32_hnode {
437 u32 handle;
438 u32 prio;
439 unsigned int divisor;
440};
441
442enum tc_clsu32_command {
443 TC_CLSU32_NEW_KNODE,
444 TC_CLSU32_REPLACE_KNODE,
445 TC_CLSU32_DELETE_KNODE,
446 TC_CLSU32_NEW_HNODE,
447 TC_CLSU32_REPLACE_HNODE,
448 TC_CLSU32_DELETE_HNODE,
449};
450
451struct tc_cls_u32_offload {
5fd9fc4e 452 struct tc_cls_common_offload common;
a1b7c5fd
JF
453 /* knode values */
454 enum tc_clsu32_command command;
455 union {
456 struct tc_cls_u32_knode knode;
457 struct tc_cls_u32_hnode hnode;
458 };
459};
460
7b06e8ae 461static inline bool tc_can_offload(const struct net_device *dev)
6843e7a2 462{
2b6ab0d3
JF
463 if (!(dev->features & NETIF_F_HW_TC))
464 return false;
9e8ce79c
JF
465 if (!dev->netdev_ops->ndo_setup_tc)
466 return false;
9e8ce79c 467 return true;
6843e7a2
JF
468}
469
55330f05
HHZ
470static inline bool tc_skip_hw(u32 flags)
471{
472 return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
473}
474
7b06e8ae 475static inline bool tc_should_offload(const struct net_device *dev, u32 flags)
55330f05
HHZ
476{
477 if (tc_skip_hw(flags))
478 return false;
7b06e8ae 479 return tc_can_offload(dev);
55330f05
HHZ
480}
481
d34e3e18
SS
482static inline bool tc_skip_sw(u32 flags)
483{
484 return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
485}
486
487/* SKIP_HW and SKIP_SW are mutually exclusive flags. */
488static inline bool tc_flags_valid(u32 flags)
489{
490 if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
491 return false;
492
493 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
494 return false;
495
496 return true;
497}
498
e696028a
OG
499static inline bool tc_in_hw(u32 flags)
500{
501 return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
502}
503
5b33f488
AV
504enum tc_fl_command {
505 TC_CLSFLOWER_REPLACE,
506 TC_CLSFLOWER_DESTROY,
10cbc684 507 TC_CLSFLOWER_STATS,
5b33f488
AV
508};
509
510struct tc_cls_flower_offload {
5fd9fc4e 511 struct tc_cls_common_offload common;
5b33f488 512 enum tc_fl_command command;
8208d21b 513 unsigned long cookie;
5b33f488
AV
514 struct flow_dissector *dissector;
515 struct fl_flow_key *mask;
516 struct fl_flow_key *key;
517 struct tcf_exts *exts;
3e0e8266 518 bool egress_dev;
5b33f488
AV
519};
520
b87f7936
YG
521enum tc_matchall_command {
522 TC_CLSMATCHALL_REPLACE,
523 TC_CLSMATCHALL_DESTROY,
524};
525
526struct tc_cls_matchall_offload {
5fd9fc4e 527 struct tc_cls_common_offload common;
b87f7936
YG
528 enum tc_matchall_command command;
529 struct tcf_exts *exts;
530 unsigned long cookie;
531};
532
332ae8e2
JK
533enum tc_clsbpf_command {
534 TC_CLSBPF_ADD,
535 TC_CLSBPF_REPLACE,
536 TC_CLSBPF_DESTROY,
68d64063 537 TC_CLSBPF_STATS,
332ae8e2
JK
538};
539
540struct tc_cls_bpf_offload {
5fd9fc4e 541 struct tc_cls_common_offload common;
332ae8e2
JK
542 enum tc_clsbpf_command command;
543 struct tcf_exts *exts;
544 struct bpf_prog *prog;
545 const char *name;
546 bool exts_integrated;
0d01d45f 547 u32 gen_flags;
332ae8e2
JK
548};
549
1045ba77
JHS
550
551/* This structure holds cookie structure that is passed from user
552 * to the kernel for actions and classifiers
553 */
554struct tc_cookie {
555 u8 *data;
556 u32 len;
557};
1da177e4 558#endif