Commit | Line | Data |
---|---|---|
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> | |
7aa0045d | 6 | #include <linux/workqueue.h> |
1da177e4 LT |
7 | #include <net/sch_generic.h> |
8 | #include <net/act_api.h> | |
8f256622 | 9 | #include <net/flow_offload.h> |
a5148626 | 10 | #include <net/net_namespace.h> |
1da177e4 | 11 | |
cd11b164 PA |
12 | /* TC action not accessible from user space */ |
13 | #define TC_ACT_REINSERT (TC_ACT_VALUE_MAX + 1) | |
14 | ||
1da177e4 LT |
15 | /* Basic packet classifier frontend definitions. */ |
16 | ||
fd2c3ef7 | 17 | struct tcf_walker { |
1da177e4 LT |
18 | int stop; |
19 | int skip; | |
20 | int count; | |
6676d5e4 | 21 | bool nonempty; |
01683a14 | 22 | unsigned long cookie; |
8113c095 | 23 | int (*fn)(struct tcf_proto *, void *node, struct tcf_walker *); |
1da177e4 LT |
24 | }; |
25 | ||
5c15257f JP |
26 | int register_tcf_proto_ops(struct tcf_proto_ops *ops); |
27 | int unregister_tcf_proto_ops(struct tcf_proto_ops *ops); | |
1da177e4 | 28 | |
8c4083b3 JP |
29 | enum tcf_block_binder_type { |
30 | TCF_BLOCK_BINDER_TYPE_UNSPEC, | |
6e40cf2d JP |
31 | TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS, |
32 | TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS, | |
8c4083b3 JP |
33 | }; |
34 | ||
35 | struct tcf_block_ext_info { | |
36 | enum tcf_block_binder_type binder_type; | |
c7eb7d72 JP |
37 | tcf_chain_head_change_t *chain_head_change; |
38 | void *chain_head_change_priv; | |
48617387 | 39 | u32 block_index; |
8c4083b3 JP |
40 | }; |
41 | ||
acb67442 | 42 | struct tcf_block_cb; |
aaa908ff | 43 | bool tcf_queue_work(struct rcu_work *rwork, work_func_t func); |
acb67442 | 44 | |
8ae70032 | 45 | #ifdef CONFIG_NET_CLS |
1f3ed383 JP |
46 | struct tcf_chain *tcf_chain_get_by_act(struct tcf_block *block, |
47 | u32 chain_index); | |
1f3ed383 | 48 | void tcf_chain_put_by_act(struct tcf_chain *chain); |
bbf73830 VB |
49 | struct tcf_chain *tcf_get_next_chain(struct tcf_block *block, |
50 | struct tcf_chain *chain); | |
fe2923af | 51 | struct tcf_proto *tcf_get_next_proto(struct tcf_chain *chain, |
12db03b6 | 52 | struct tcf_proto *tp, bool rtnl_held); |
f36fe1c4 | 53 | void tcf_block_netif_keep_dst(struct tcf_block *block); |
6529eaba | 54 | int tcf_block_get(struct tcf_block **p_block, |
8d1a77f9 AA |
55 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, |
56 | struct netlink_ext_ack *extack); | |
c7eb7d72 | 57 | int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q, |
8d1a77f9 AA |
58 | struct tcf_block_ext_info *ei, |
59 | struct netlink_ext_ack *extack); | |
6529eaba | 60 | void tcf_block_put(struct tcf_block *block); |
c7eb7d72 | 61 | void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q, |
8c4083b3 | 62 | struct tcf_block_ext_info *ei); |
44186460 | 63 | |
48617387 JP |
64 | static inline bool tcf_block_shared(struct tcf_block *block) |
65 | { | |
66 | return block->index; | |
67 | } | |
68 | ||
44186460 JP |
69 | static inline struct Qdisc *tcf_block_q(struct tcf_block *block) |
70 | { | |
48617387 | 71 | WARN_ON(tcf_block_shared(block)); |
44186460 JP |
72 | return block->q; |
73 | } | |
74 | ||
acb67442 JP |
75 | void *tcf_block_cb_priv(struct tcf_block_cb *block_cb); |
76 | struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block, | |
77 | tc_setup_cb_t *cb, void *cb_ident); | |
78 | void tcf_block_cb_incref(struct tcf_block_cb *block_cb); | |
79 | unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb); | |
80 | struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block, | |
81 | tc_setup_cb_t *cb, void *cb_ident, | |
60513bd8 JH |
82 | void *cb_priv, |
83 | struct netlink_ext_ack *extack); | |
acb67442 JP |
84 | int tcf_block_cb_register(struct tcf_block *block, |
85 | tc_setup_cb_t *cb, void *cb_ident, | |
60513bd8 | 86 | void *cb_priv, struct netlink_ext_ack *extack); |
32636742 JH |
87 | void __tcf_block_cb_unregister(struct tcf_block *block, |
88 | struct tcf_block_cb *block_cb); | |
acb67442 JP |
89 | void tcf_block_cb_unregister(struct tcf_block *block, |
90 | tc_setup_cb_t *cb, void *cb_ident); | |
7f76fa36 JH |
91 | int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv, |
92 | tc_indr_block_bind_cb_t *cb, void *cb_ident); | |
93 | int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv, | |
94 | tc_indr_block_bind_cb_t *cb, void *cb_ident); | |
95 | void __tc_indr_block_cb_unregister(struct net_device *dev, | |
96 | tc_indr_block_bind_cb_t *cb, void *cb_ident); | |
97 | void tc_indr_block_cb_unregister(struct net_device *dev, | |
98 | tc_indr_block_bind_cb_t *cb, void *cb_ident); | |
acb67442 | 99 | |
87d83093 JP |
100 | int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
101 | struct tcf_result *res, bool compat_mode); | |
102 | ||
8ae70032 | 103 | #else |
88c44a52 PJV |
104 | static inline bool tcf_block_shared(struct tcf_block *block) |
105 | { | |
106 | return false; | |
107 | } | |
108 | ||
6529eaba JP |
109 | static inline |
110 | int tcf_block_get(struct tcf_block **p_block, | |
3c149091 SM |
111 | struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q, |
112 | struct netlink_ext_ack *extack) | |
6529eaba JP |
113 | { |
114 | return 0; | |
115 | } | |
116 | ||
8c4083b3 | 117 | static inline |
c7eb7d72 | 118 | int tcf_block_get_ext(struct tcf_block **p_block, struct Qdisc *q, |
33c30a8b QM |
119 | struct tcf_block_ext_info *ei, |
120 | struct netlink_ext_ack *extack) | |
8c4083b3 JP |
121 | { |
122 | return 0; | |
123 | } | |
124 | ||
6529eaba | 125 | static inline void tcf_block_put(struct tcf_block *block) |
8ae70032 JP |
126 | { |
127 | } | |
87d83093 | 128 | |
8c4083b3 | 129 | static inline |
c7eb7d72 | 130 | void tcf_block_put_ext(struct tcf_block *block, struct Qdisc *q, |
8c4083b3 JP |
131 | struct tcf_block_ext_info *ei) |
132 | { | |
133 | } | |
134 | ||
44186460 JP |
135 | static inline struct Qdisc *tcf_block_q(struct tcf_block *block) |
136 | { | |
137 | return NULL; | |
138 | } | |
139 | ||
acb67442 JP |
140 | static inline |
141 | int tc_setup_cb_block_register(struct tcf_block *block, tc_setup_cb_t *cb, | |
142 | void *cb_priv) | |
143 | { | |
144 | return 0; | |
145 | } | |
146 | ||
147 | static inline | |
148 | void tc_setup_cb_block_unregister(struct tcf_block *block, tc_setup_cb_t *cb, | |
149 | void *cb_priv) | |
150 | { | |
151 | } | |
152 | ||
153 | static inline | |
154 | void *tcf_block_cb_priv(struct tcf_block_cb *block_cb) | |
155 | { | |
156 | return NULL; | |
157 | } | |
158 | ||
159 | static inline | |
160 | struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block, | |
161 | tc_setup_cb_t *cb, void *cb_ident) | |
162 | { | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | static inline | |
167 | void tcf_block_cb_incref(struct tcf_block_cb *block_cb) | |
168 | { | |
169 | } | |
170 | ||
171 | static inline | |
172 | unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb) | |
173 | { | |
174 | return 0; | |
175 | } | |
176 | ||
177 | static inline | |
178 | struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block, | |
179 | tc_setup_cb_t *cb, void *cb_ident, | |
60513bd8 JH |
180 | void *cb_priv, |
181 | struct netlink_ext_ack *extack) | |
acb67442 JP |
182 | { |
183 | return NULL; | |
184 | } | |
185 | ||
186 | static inline | |
187 | int tcf_block_cb_register(struct tcf_block *block, | |
188 | tc_setup_cb_t *cb, void *cb_ident, | |
60513bd8 | 189 | void *cb_priv, struct netlink_ext_ack *extack) |
acb67442 JP |
190 | { |
191 | return 0; | |
192 | } | |
193 | ||
194 | static inline | |
32636742 JH |
195 | void __tcf_block_cb_unregister(struct tcf_block *block, |
196 | struct tcf_block_cb *block_cb) | |
acb67442 JP |
197 | { |
198 | } | |
199 | ||
200 | static inline | |
201 | void tcf_block_cb_unregister(struct tcf_block *block, | |
202 | tc_setup_cb_t *cb, void *cb_ident) | |
203 | { | |
204 | } | |
205 | ||
7f76fa36 JH |
206 | static inline |
207 | int __tc_indr_block_cb_register(struct net_device *dev, void *cb_priv, | |
208 | tc_indr_block_bind_cb_t *cb, void *cb_ident) | |
209 | { | |
210 | return 0; | |
211 | } | |
212 | ||
213 | static inline | |
214 | int tc_indr_block_cb_register(struct net_device *dev, void *cb_priv, | |
215 | tc_indr_block_bind_cb_t *cb, void *cb_ident) | |
216 | { | |
217 | return 0; | |
218 | } | |
219 | ||
220 | static inline | |
221 | void __tc_indr_block_cb_unregister(struct net_device *dev, | |
222 | tc_indr_block_bind_cb_t *cb, void *cb_ident) | |
223 | { | |
224 | } | |
225 | ||
226 | static inline | |
227 | void tc_indr_block_cb_unregister(struct net_device *dev, | |
228 | tc_indr_block_bind_cb_t *cb, void *cb_ident) | |
229 | { | |
230 | } | |
231 | ||
87d83093 JP |
232 | static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
233 | struct tcf_result *res, bool compat_mode) | |
234 | { | |
235 | return TC_ACT_UNSPEC; | |
236 | } | |
8ae70032 | 237 | #endif |
cf1facda | 238 | |
1da177e4 LT |
239 | static inline unsigned long |
240 | __cls_set_class(unsigned long *clp, unsigned long cl) | |
241 | { | |
a0efb80c | 242 | return xchg(clp, cl); |
1da177e4 LT |
243 | } |
244 | ||
245 | static inline unsigned long | |
34e3759c | 246 | cls_set_class(struct Qdisc *q, unsigned long *clp, unsigned long cl) |
1da177e4 LT |
247 | { |
248 | unsigned long old_cl; | |
34e3759c JP |
249 | |
250 | sch_tree_lock(q); | |
1da177e4 | 251 | old_cl = __cls_set_class(clp, cl); |
34e3759c | 252 | sch_tree_unlock(q); |
1da177e4 LT |
253 | return old_cl; |
254 | } | |
255 | ||
256 | static inline void | |
257 | tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base) | |
258 | { | |
34e3759c | 259 | struct Qdisc *q = tp->chain->block->q; |
1da177e4 LT |
260 | unsigned long cl; |
261 | ||
34e3759c JP |
262 | /* Check q as it is not set for shared blocks. In that case, |
263 | * setting class is not supported. | |
264 | */ | |
265 | if (!q) | |
266 | return; | |
267 | cl = q->ops->cl_ops->bind_tcf(q, base, r->classid); | |
268 | cl = cls_set_class(q, &r->class, cl); | |
1da177e4 | 269 | if (cl) |
34e3759c | 270 | q->ops->cl_ops->unbind_tcf(q, cl); |
1da177e4 LT |
271 | } |
272 | ||
273 | static inline void | |
274 | tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r) | |
275 | { | |
34e3759c | 276 | struct Qdisc *q = tp->chain->block->q; |
1da177e4 LT |
277 | unsigned long cl; |
278 | ||
34e3759c JP |
279 | if (!q) |
280 | return; | |
1da177e4 | 281 | if ((cl = __cls_set_class(&r->class, 0)) != 0) |
34e3759c | 282 | q->ops->cl_ops->unbind_tcf(q, cl); |
1da177e4 LT |
283 | } |
284 | ||
fd2c3ef7 | 285 | struct tcf_exts { |
1da177e4 | 286 | #ifdef CONFIG_NET_CLS_ACT |
33be6271 | 287 | __u32 type; /* for backward compat(TCA_OLD_COMPAT) */ |
22dc13c8 WC |
288 | int nr_actions; |
289 | struct tc_action **actions; | |
e4b95c41 | 290 | struct net *net; |
1da177e4 | 291 | #endif |
5da57f42 WC |
292 | /* Map to export classifier specific extension TLV types to the |
293 | * generic extensions API. Unsupported extensions must be set to 0. | |
294 | */ | |
1da177e4 LT |
295 | int action; |
296 | int police; | |
297 | }; | |
298 | ||
14215108 CW |
299 | static inline int tcf_exts_init(struct tcf_exts *exts, struct net *net, |
300 | int action, int police) | |
33be6271 WC |
301 | { |
302 | #ifdef CONFIG_NET_CLS_ACT | |
5da57f42 | 303 | exts->type = 0; |
22dc13c8 | 304 | exts->nr_actions = 0; |
14215108 | 305 | exts->net = net; |
22dc13c8 WC |
306 | exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *), |
307 | GFP_KERNEL); | |
b9a24bb7 WC |
308 | if (!exts->actions) |
309 | return -ENOMEM; | |
33be6271 | 310 | #endif |
5da57f42 WC |
311 | exts->action = action; |
312 | exts->police = police; | |
b9a24bb7 | 313 | return 0; |
33be6271 WC |
314 | } |
315 | ||
e4b95c41 CW |
316 | /* Return false if the netns is being destroyed in cleanup_net(). Callers |
317 | * need to do cleanup synchronously in this case, otherwise may race with | |
318 | * tc_action_net_exit(). Return true for other cases. | |
319 | */ | |
320 | static inline bool tcf_exts_get_net(struct tcf_exts *exts) | |
321 | { | |
322 | #ifdef CONFIG_NET_CLS_ACT | |
323 | exts->net = maybe_get_net(exts->net); | |
324 | return exts->net != NULL; | |
325 | #else | |
326 | return true; | |
327 | #endif | |
328 | } | |
329 | ||
330 | static inline void tcf_exts_put_net(struct tcf_exts *exts) | |
331 | { | |
332 | #ifdef CONFIG_NET_CLS_ACT | |
333 | if (exts->net) | |
334 | put_net(exts->net); | |
335 | #endif | |
336 | } | |
337 | ||
22dc13c8 | 338 | #ifdef CONFIG_NET_CLS_ACT |
244cd96a CW |
339 | #define tcf_exts_for_each_action(i, a, exts) \ |
340 | for (i = 0; i < TCA_ACT_MAX_PRIO && ((a) = (exts)->actions[i]); i++) | |
341 | #else | |
342 | #define tcf_exts_for_each_action(i, a, exts) \ | |
191672ca | 343 | for (; 0; (void)(i), (void)(a), (void)(exts)) |
22dc13c8 | 344 | #endif |
22dc13c8 | 345 | |
d897a638 JK |
346 | static inline void |
347 | tcf_exts_stats_update(const struct tcf_exts *exts, | |
348 | u64 bytes, u64 packets, u64 lastuse) | |
349 | { | |
350 | #ifdef CONFIG_NET_CLS_ACT | |
351 | int i; | |
352 | ||
353 | preempt_disable(); | |
354 | ||
355 | for (i = 0; i < exts->nr_actions; i++) { | |
356 | struct tc_action *a = exts->actions[i]; | |
357 | ||
28169aba | 358 | tcf_action_stats_update(a, bytes, packets, lastuse, true); |
d897a638 JK |
359 | } |
360 | ||
361 | preempt_enable(); | |
362 | #endif | |
363 | } | |
364 | ||
3bcc0cec JP |
365 | /** |
366 | * tcf_exts_has_actions - check if at least one action is present | |
367 | * @exts: tc filter extensions handle | |
368 | * | |
369 | * Returns true if at least one action is present. | |
370 | */ | |
371 | static inline bool tcf_exts_has_actions(struct tcf_exts *exts) | |
372 | { | |
2734437e | 373 | #ifdef CONFIG_NET_CLS_ACT |
3bcc0cec JP |
374 | return exts->nr_actions; |
375 | #else | |
376 | return false; | |
377 | #endif | |
378 | } | |
2734437e | 379 | |
af69afc5 JP |
380 | /** |
381 | * tcf_exts_exec - execute tc filter extensions | |
382 | * @skb: socket buffer | |
383 | * @exts: tc filter extensions handle | |
384 | * @res: desired result | |
385 | * | |
af089e70 | 386 | * Executes all configured extensions. Returns TC_ACT_OK on a normal execution, |
af69afc5 JP |
387 | * a negative number if the filter must be considered unmatched or |
388 | * a positive action code (TC_ACT_*) which must be returned to the | |
389 | * underlying layer. | |
390 | */ | |
391 | static inline int | |
392 | tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts, | |
393 | struct tcf_result *res) | |
394 | { | |
395 | #ifdef CONFIG_NET_CLS_ACT | |
ec1a9cca | 396 | return tcf_action_exec(skb, exts->actions, exts->nr_actions, res); |
af69afc5 | 397 | #endif |
af089e70 | 398 | return TC_ACT_OK; |
af69afc5 JP |
399 | } |
400 | ||
5c15257f JP |
401 | int tcf_exts_validate(struct net *net, struct tcf_proto *tp, |
402 | struct nlattr **tb, struct nlattr *rate_tlv, | |
ec6743a1 | 403 | struct tcf_exts *exts, bool ovr, bool rtnl_held, |
50a56190 | 404 | struct netlink_ext_ack *extack); |
18d0264f | 405 | void tcf_exts_destroy(struct tcf_exts *exts); |
9b0d4446 | 406 | void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src); |
5da57f42 WC |
407 | int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts); |
408 | int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts); | |
1da177e4 LT |
409 | |
410 | /** | |
411 | * struct tcf_pkt_info - packet information | |
412 | */ | |
fd2c3ef7 | 413 | struct tcf_pkt_info { |
1da177e4 LT |
414 | unsigned char * ptr; |
415 | int nexthdr; | |
416 | }; | |
417 | ||
418 | #ifdef CONFIG_NET_EMATCH | |
419 | ||
420 | struct tcf_ematch_ops; | |
421 | ||
422 | /** | |
423 | * struct tcf_ematch - extended match (ematch) | |
424 | * | |
425 | * @matchid: identifier to allow userspace to reidentify a match | |
426 | * @flags: flags specifying attributes and the relation to other matches | |
427 | * @ops: the operations lookup table of the corresponding ematch module | |
428 | * @datalen: length of the ematch specific configuration data | |
429 | * @data: ematch specific data | |
430 | */ | |
fd2c3ef7 | 431 | struct tcf_ematch { |
1da177e4 LT |
432 | struct tcf_ematch_ops * ops; |
433 | unsigned long data; | |
434 | unsigned int datalen; | |
435 | u16 matchid; | |
436 | u16 flags; | |
82a470f1 | 437 | struct net *net; |
1da177e4 LT |
438 | }; |
439 | ||
440 | static inline int tcf_em_is_container(struct tcf_ematch *em) | |
441 | { | |
442 | return !em->ops; | |
443 | } | |
444 | ||
445 | static inline int tcf_em_is_simple(struct tcf_ematch *em) | |
446 | { | |
447 | return em->flags & TCF_EM_SIMPLE; | |
448 | } | |
449 | ||
450 | static inline int tcf_em_is_inverted(struct tcf_ematch *em) | |
451 | { | |
452 | return em->flags & TCF_EM_INVERT; | |
453 | } | |
454 | ||
455 | static inline int tcf_em_last_match(struct tcf_ematch *em) | |
456 | { | |
457 | return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END; | |
458 | } | |
459 | ||
460 | static inline int tcf_em_early_end(struct tcf_ematch *em, int result) | |
461 | { | |
462 | if (tcf_em_last_match(em)) | |
463 | return 1; | |
464 | ||
465 | if (result == 0 && em->flags & TCF_EM_REL_AND) | |
466 | return 1; | |
467 | ||
468 | if (result != 0 && em->flags & TCF_EM_REL_OR) | |
469 | return 1; | |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
474 | /** | |
475 | * struct tcf_ematch_tree - ematch tree handle | |
476 | * | |
477 | * @hdr: ematch tree header supplied by userspace | |
478 | * @matches: array of ematches | |
479 | */ | |
fd2c3ef7 | 480 | struct tcf_ematch_tree { |
1da177e4 LT |
481 | struct tcf_ematch_tree_hdr hdr; |
482 | struct tcf_ematch * matches; | |
483 | ||
484 | }; | |
485 | ||
486 | /** | |
487 | * struct tcf_ematch_ops - ematch module operations | |
488 | * | |
489 | * @kind: identifier (kind) of this ematch module | |
490 | * @datalen: length of expected configuration data (optional) | |
491 | * @change: called during validation (optional) | |
492 | * @match: called during ematch tree evaluation, must return 1/0 | |
493 | * @destroy: called during destroyage (optional) | |
494 | * @dump: called during dumping process (optional) | |
495 | * @owner: owner, must be set to THIS_MODULE | |
496 | * @link: link to previous/next ematch module (internal use) | |
497 | */ | |
fd2c3ef7 | 498 | struct tcf_ematch_ops { |
1da177e4 LT |
499 | int kind; |
500 | int datalen; | |
82a470f1 | 501 | int (*change)(struct net *net, void *, |
1da177e4 LT |
502 | int, struct tcf_ematch *); |
503 | int (*match)(struct sk_buff *, struct tcf_ematch *, | |
504 | struct tcf_pkt_info *); | |
82a470f1 | 505 | void (*destroy)(struct tcf_ematch *); |
1da177e4 LT |
506 | int (*dump)(struct sk_buff *, struct tcf_ematch *); |
507 | struct module *owner; | |
508 | struct list_head link; | |
509 | }; | |
510 | ||
5c15257f JP |
511 | int tcf_em_register(struct tcf_ematch_ops *); |
512 | void tcf_em_unregister(struct tcf_ematch_ops *); | |
513 | int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *, | |
514 | struct tcf_ematch_tree *); | |
82a470f1 | 515 | void tcf_em_tree_destroy(struct tcf_ematch_tree *); |
5c15257f JP |
516 | int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int); |
517 | int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *, | |
518 | struct tcf_pkt_info *); | |
1da177e4 | 519 | |
1da177e4 LT |
520 | /** |
521 | * tcf_em_tree_match - evaulate an ematch tree | |
522 | * | |
523 | * @skb: socket buffer of the packet in question | |
524 | * @tree: ematch tree to be used for evaluation | |
525 | * @info: packet information examined by classifier | |
526 | * | |
527 | * This function matches @skb against the ematch tree in @tree by going | |
528 | * through all ematches respecting their logic relations returning | |
529 | * as soon as the result is obvious. | |
530 | * | |
531 | * Returns 1 if the ematch tree as-one matches, no ematches are configured | |
532 | * or ematch is not enabled in the kernel, otherwise 0 is returned. | |
533 | */ | |
534 | static inline int tcf_em_tree_match(struct sk_buff *skb, | |
535 | struct tcf_ematch_tree *tree, | |
536 | struct tcf_pkt_info *info) | |
537 | { | |
538 | if (tree->hdr.nmatches) | |
539 | return __tcf_em_tree_match(skb, tree, info); | |
540 | else | |
541 | return 1; | |
542 | } | |
543 | ||
db3d99c0 PM |
544 | #define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind)) |
545 | ||
1da177e4 LT |
546 | #else /* CONFIG_NET_EMATCH */ |
547 | ||
fd2c3ef7 | 548 | struct tcf_ematch_tree { |
1da177e4 LT |
549 | }; |
550 | ||
551 | #define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0) | |
82a470f1 | 552 | #define tcf_em_tree_destroy(t) do { (void)(t); } while(0) |
1da177e4 | 553 | #define tcf_em_tree_dump(skb, t, tlv) (0) |
1da177e4 LT |
554 | #define tcf_em_tree_match(skb, t, info) ((void)(info), 1) |
555 | ||
556 | #endif /* CONFIG_NET_EMATCH */ | |
557 | ||
558 | static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer) | |
559 | { | |
560 | switch (layer) { | |
561 | case TCF_LAYER_LINK: | |
d3303a65 | 562 | return skb_mac_header(skb); |
1da177e4 | 563 | case TCF_LAYER_NETWORK: |
d56f90a7 | 564 | return skb_network_header(skb); |
1da177e4 | 565 | case TCF_LAYER_TRANSPORT: |
9c70220b | 566 | return skb_transport_header(skb); |
1da177e4 LT |
567 | } |
568 | ||
569 | return NULL; | |
570 | } | |
571 | ||
eddc9ec5 ACM |
572 | static inline int tcf_valid_offset(const struct sk_buff *skb, |
573 | const unsigned char *ptr, const int len) | |
1da177e4 | 574 | { |
da521b2c DM |
575 | return likely((ptr + len) <= skb_tail_pointer(skb) && |
576 | ptr >= skb->head && | |
577 | (ptr <= (ptr + len))); | |
1da177e4 LT |
578 | } |
579 | ||
1da177e4 | 580 | static inline int |
1057c55f AA |
581 | tcf_change_indev(struct net *net, struct nlattr *indev_tlv, |
582 | struct netlink_ext_ack *extack) | |
1da177e4 | 583 | { |
2519a602 WC |
584 | char indev[IFNAMSIZ]; |
585 | struct net_device *dev; | |
586 | ||
1057c55f AA |
587 | if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ) { |
588 | NL_SET_ERR_MSG(extack, "Interface name too long"); | |
1da177e4 | 589 | return -EINVAL; |
1057c55f | 590 | } |
2519a602 WC |
591 | dev = __dev_get_by_name(net, indev); |
592 | if (!dev) | |
593 | return -ENODEV; | |
594 | return dev->ifindex; | |
1da177e4 LT |
595 | } |
596 | ||
2519a602 WC |
597 | static inline bool |
598 | tcf_match_indev(struct sk_buff *skb, int ifindex) | |
1da177e4 | 599 | { |
2519a602 WC |
600 | if (!ifindex) |
601 | return true; | |
602 | if (!skb->skb_iif) | |
603 | return false; | |
604 | return ifindex == skb->skb_iif; | |
1da177e4 | 605 | } |
1da177e4 | 606 | |
3a7b6861 PNA |
607 | int tc_setup_flow_action(struct flow_action *flow_action, |
608 | const struct tcf_exts *exts); | |
aeb3fecd CW |
609 | int tc_setup_cb_call(struct tcf_block *block, enum tc_setup_type type, |
610 | void *type_data, bool err_stop); | |
e3ab786b | 611 | unsigned int tcf_exts_num_actions(struct tcf_exts *exts); |
717503b9 | 612 | |
8c4083b3 JP |
613 | enum tc_block_command { |
614 | TC_BLOCK_BIND, | |
615 | TC_BLOCK_UNBIND, | |
616 | }; | |
617 | ||
618 | struct tc_block_offload { | |
619 | enum tc_block_command command; | |
620 | enum tcf_block_binder_type binder_type; | |
621 | struct tcf_block *block; | |
60513bd8 | 622 | struct netlink_ext_ack *extack; |
8c4083b3 JP |
623 | }; |
624 | ||
5fd9fc4e | 625 | struct tc_cls_common_offload { |
5fd9fc4e JP |
626 | u32 chain_index; |
627 | __be16 protocol; | |
d7c1c8d2 | 628 | u32 prio; |
8f0b425a | 629 | struct netlink_ext_ack *extack; |
5fd9fc4e JP |
630 | }; |
631 | ||
a1b7c5fd JF |
632 | struct tc_cls_u32_knode { |
633 | struct tcf_exts *exts; | |
068ceb35 | 634 | struct tcf_result *res; |
e014860e | 635 | struct tc_u32_sel *sel; |
a1b7c5fd JF |
636 | u32 handle; |
637 | u32 val; | |
638 | u32 mask; | |
639 | u32 link_handle; | |
e014860e | 640 | u8 fshift; |
a1b7c5fd JF |
641 | }; |
642 | ||
643 | struct tc_cls_u32_hnode { | |
644 | u32 handle; | |
645 | u32 prio; | |
646 | unsigned int divisor; | |
647 | }; | |
648 | ||
649 | enum tc_clsu32_command { | |
650 | TC_CLSU32_NEW_KNODE, | |
651 | TC_CLSU32_REPLACE_KNODE, | |
652 | TC_CLSU32_DELETE_KNODE, | |
653 | TC_CLSU32_NEW_HNODE, | |
654 | TC_CLSU32_REPLACE_HNODE, | |
655 | TC_CLSU32_DELETE_HNODE, | |
656 | }; | |
657 | ||
658 | struct tc_cls_u32_offload { | |
5fd9fc4e | 659 | struct tc_cls_common_offload common; |
a1b7c5fd JF |
660 | /* knode values */ |
661 | enum tc_clsu32_command command; | |
662 | union { | |
663 | struct tc_cls_u32_knode knode; | |
664 | struct tc_cls_u32_hnode hnode; | |
665 | }; | |
666 | }; | |
667 | ||
7b06e8ae | 668 | static inline bool tc_can_offload(const struct net_device *dev) |
6843e7a2 | 669 | { |
70b5aee4 | 670 | return dev->features & NETIF_F_HW_TC; |
6843e7a2 JF |
671 | } |
672 | ||
f9eda14f QM |
673 | static inline bool tc_can_offload_extack(const struct net_device *dev, |
674 | struct netlink_ext_ack *extack) | |
675 | { | |
676 | bool can = tc_can_offload(dev); | |
677 | ||
678 | if (!can) | |
679 | NL_SET_ERR_MSG(extack, "TC offload is disabled on net device"); | |
680 | ||
681 | return can; | |
682 | } | |
683 | ||
878db9f0 JK |
684 | static inline bool |
685 | tc_cls_can_offload_and_chain0(const struct net_device *dev, | |
686 | struct tc_cls_common_offload *common) | |
687 | { | |
688 | if (!tc_can_offload_extack(dev, common->extack)) | |
689 | return false; | |
690 | if (common->chain_index) { | |
691 | NL_SET_ERR_MSG(common->extack, | |
692 | "Driver supports only offload of chain 0"); | |
693 | return false; | |
694 | } | |
695 | return true; | |
696 | } | |
697 | ||
55330f05 HHZ |
698 | static inline bool tc_skip_hw(u32 flags) |
699 | { | |
700 | return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false; | |
701 | } | |
702 | ||
d34e3e18 SS |
703 | static inline bool tc_skip_sw(u32 flags) |
704 | { | |
705 | return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false; | |
706 | } | |
707 | ||
708 | /* SKIP_HW and SKIP_SW are mutually exclusive flags. */ | |
709 | static inline bool tc_flags_valid(u32 flags) | |
710 | { | |
81c7288b MRL |
711 | if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW | |
712 | TCA_CLS_FLAGS_VERBOSE)) | |
d34e3e18 SS |
713 | return false; |
714 | ||
81c7288b | 715 | flags &= TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW; |
d34e3e18 SS |
716 | if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))) |
717 | return false; | |
718 | ||
719 | return true; | |
720 | } | |
721 | ||
e696028a OG |
722 | static inline bool tc_in_hw(u32 flags) |
723 | { | |
724 | return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false; | |
725 | } | |
726 | ||
34832e1c JK |
727 | static inline void |
728 | tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common, | |
729 | const struct tcf_proto *tp, u32 flags, | |
730 | struct netlink_ext_ack *extack) | |
731 | { | |
732 | cls_common->chain_index = tp->chain->index; | |
733 | cls_common->protocol = tp->protocol; | |
734 | cls_common->prio = tp->prio; | |
81c7288b | 735 | if (tc_skip_sw(flags) || flags & TCA_CLS_FLAGS_VERBOSE) |
34832e1c JK |
736 | cls_common->extack = extack; |
737 | } | |
738 | ||
5b33f488 AV |
739 | enum tc_fl_command { |
740 | TC_CLSFLOWER_REPLACE, | |
741 | TC_CLSFLOWER_DESTROY, | |
10cbc684 | 742 | TC_CLSFLOWER_STATS, |
34738452 JP |
743 | TC_CLSFLOWER_TMPLT_CREATE, |
744 | TC_CLSFLOWER_TMPLT_DESTROY, | |
5b33f488 AV |
745 | }; |
746 | ||
747 | struct tc_cls_flower_offload { | |
5fd9fc4e | 748 | struct tc_cls_common_offload common; |
5b33f488 | 749 | enum tc_fl_command command; |
8208d21b | 750 | unsigned long cookie; |
8f256622 | 751 | struct flow_rule *rule; |
3b1903ef | 752 | struct flow_stats stats; |
384c181e | 753 | u32 classid; |
5b33f488 AV |
754 | }; |
755 | ||
8f256622 PNA |
756 | static inline struct flow_rule * |
757 | tc_cls_flower_offload_flow_rule(struct tc_cls_flower_offload *tc_flow_cmd) | |
758 | { | |
759 | return tc_flow_cmd->rule; | |
760 | } | |
761 | ||
b87f7936 YG |
762 | enum tc_matchall_command { |
763 | TC_CLSMATCHALL_REPLACE, | |
764 | TC_CLSMATCHALL_DESTROY, | |
b7fe4ab8 | 765 | TC_CLSMATCHALL_STATS, |
b87f7936 YG |
766 | }; |
767 | ||
768 | struct tc_cls_matchall_offload { | |
5fd9fc4e | 769 | struct tc_cls_common_offload common; |
b87f7936 | 770 | enum tc_matchall_command command; |
f00cbf19 | 771 | struct flow_rule *rule; |
b7fe4ab8 | 772 | struct flow_stats stats; |
b87f7936 YG |
773 | unsigned long cookie; |
774 | }; | |
775 | ||
332ae8e2 | 776 | enum tc_clsbpf_command { |
102740bd | 777 | TC_CLSBPF_OFFLOAD, |
68d64063 | 778 | TC_CLSBPF_STATS, |
332ae8e2 JK |
779 | }; |
780 | ||
781 | struct tc_cls_bpf_offload { | |
5fd9fc4e | 782 | struct tc_cls_common_offload common; |
332ae8e2 JK |
783 | enum tc_clsbpf_command command; |
784 | struct tcf_exts *exts; | |
785 | struct bpf_prog *prog; | |
102740bd | 786 | struct bpf_prog *oldprog; |
332ae8e2 JK |
787 | const char *name; |
788 | bool exts_integrated; | |
789 | }; | |
790 | ||
4e8b86c0 AN |
791 | struct tc_mqprio_qopt_offload { |
792 | /* struct tc_mqprio_qopt must always be the first element */ | |
793 | struct tc_mqprio_qopt qopt; | |
794 | u16 mode; | |
795 | u16 shaper; | |
796 | u32 flags; | |
797 | u64 min_rate[TC_QOPT_MAX_QUEUE]; | |
798 | u64 max_rate[TC_QOPT_MAX_QUEUE]; | |
799 | }; | |
1045ba77 JHS |
800 | |
801 | /* This structure holds cookie structure that is passed from user | |
802 | * to the kernel for actions and classifiers | |
803 | */ | |
804 | struct tc_cookie { | |
805 | u8 *data; | |
806 | u32 len; | |
eec94fdb | 807 | struct rcu_head rcu; |
1045ba77 | 808 | }; |
602f3baf | 809 | |
f34b4aac NF |
810 | struct tc_qopt_offload_stats { |
811 | struct gnet_stats_basic_packed *bstats; | |
812 | struct gnet_stats_queue *qstats; | |
813 | }; | |
814 | ||
f971b132 JK |
815 | enum tc_mq_command { |
816 | TC_MQ_CREATE, | |
817 | TC_MQ_DESTROY, | |
47c669a4 | 818 | TC_MQ_STATS, |
d577a3d2 JK |
819 | TC_MQ_GRAFT, |
820 | }; | |
821 | ||
822 | struct tc_mq_opt_offload_graft_params { | |
823 | unsigned long queue; | |
824 | u32 child_handle; | |
f971b132 JK |
825 | }; |
826 | ||
827 | struct tc_mq_qopt_offload { | |
828 | enum tc_mq_command command; | |
829 | u32 handle; | |
d577a3d2 JK |
830 | union { |
831 | struct tc_qopt_offload_stats stats; | |
832 | struct tc_mq_opt_offload_graft_params graft_params; | |
833 | }; | |
f971b132 JK |
834 | }; |
835 | ||
602f3baf NF |
836 | enum tc_red_command { |
837 | TC_RED_REPLACE, | |
838 | TC_RED_DESTROY, | |
839 | TC_RED_STATS, | |
840 | TC_RED_XSTATS, | |
bf2a752b | 841 | TC_RED_GRAFT, |
602f3baf NF |
842 | }; |
843 | ||
844 | struct tc_red_qopt_offload_params { | |
845 | u32 min; | |
846 | u32 max; | |
847 | u32 probability; | |
c0b7490b | 848 | u32 limit; |
602f3baf | 849 | bool is_ecn; |
190852a5 | 850 | bool is_harddrop; |
416ef9b1 | 851 | struct gnet_stats_queue *qstats; |
602f3baf | 852 | }; |
602f3baf NF |
853 | |
854 | struct tc_red_qopt_offload { | |
855 | enum tc_red_command command; | |
856 | u32 handle; | |
857 | u32 parent; | |
858 | union { | |
859 | struct tc_red_qopt_offload_params set; | |
f34b4aac | 860 | struct tc_qopt_offload_stats stats; |
602f3baf | 861 | struct red_stats *xstats; |
bf2a752b | 862 | u32 child_handle; |
602f3baf NF |
863 | }; |
864 | }; | |
865 | ||
890d8d23 JK |
866 | enum tc_gred_command { |
867 | TC_GRED_REPLACE, | |
868 | TC_GRED_DESTROY, | |
e49efd52 | 869 | TC_GRED_STATS, |
890d8d23 JK |
870 | }; |
871 | ||
872 | struct tc_gred_vq_qopt_offload_params { | |
873 | bool present; | |
874 | u32 limit; | |
875 | u32 prio; | |
876 | u32 min; | |
877 | u32 max; | |
878 | bool is_ecn; | |
879 | bool is_harddrop; | |
880 | u32 probability; | |
881 | /* Only need backlog, see struct tc_prio_qopt_offload_params */ | |
882 | u32 *backlog; | |
883 | }; | |
884 | ||
885 | struct tc_gred_qopt_offload_params { | |
886 | bool grio_on; | |
887 | bool wred_on; | |
888 | unsigned int dp_cnt; | |
889 | unsigned int dp_def; | |
890 | struct gnet_stats_queue *qstats; | |
891 | struct tc_gred_vq_qopt_offload_params tab[MAX_DPs]; | |
892 | }; | |
893 | ||
e49efd52 JK |
894 | struct tc_gred_qopt_offload_stats { |
895 | struct gnet_stats_basic_packed bstats[MAX_DPs]; | |
896 | struct gnet_stats_queue qstats[MAX_DPs]; | |
897 | struct red_stats *xstats[MAX_DPs]; | |
898 | }; | |
899 | ||
890d8d23 JK |
900 | struct tc_gred_qopt_offload { |
901 | enum tc_gred_command command; | |
902 | u32 handle; | |
903 | u32 parent; | |
904 | union { | |
905 | struct tc_gred_qopt_offload_params set; | |
e49efd52 | 906 | struct tc_gred_qopt_offload_stats stats; |
890d8d23 JK |
907 | }; |
908 | }; | |
909 | ||
7fdb61b4 NF |
910 | enum tc_prio_command { |
911 | TC_PRIO_REPLACE, | |
912 | TC_PRIO_DESTROY, | |
913 | TC_PRIO_STATS, | |
b9c7a7ac | 914 | TC_PRIO_GRAFT, |
7fdb61b4 NF |
915 | }; |
916 | ||
917 | struct tc_prio_qopt_offload_params { | |
918 | int bands; | |
919 | u8 priomap[TC_PRIO_MAX + 1]; | |
920 | /* In case that a prio qdisc is offloaded and now is changed to a | |
921 | * non-offloadedable config, it needs to update the backlog & qlen | |
922 | * values to negate the HW backlog & qlen values (and only them). | |
923 | */ | |
924 | struct gnet_stats_queue *qstats; | |
925 | }; | |
926 | ||
b9c7a7ac NF |
927 | struct tc_prio_qopt_offload_graft_params { |
928 | u8 band; | |
929 | u32 child_handle; | |
930 | }; | |
931 | ||
7fdb61b4 NF |
932 | struct tc_prio_qopt_offload { |
933 | enum tc_prio_command command; | |
934 | u32 handle; | |
935 | u32 parent; | |
936 | union { | |
937 | struct tc_prio_qopt_offload_params replace_params; | |
938 | struct tc_qopt_offload_stats stats; | |
b9c7a7ac | 939 | struct tc_prio_qopt_offload_graft_params graft_params; |
7fdb61b4 NF |
940 | }; |
941 | }; | |
b9c7a7ac | 942 | |
98b0e5f6 JK |
943 | enum tc_root_command { |
944 | TC_ROOT_GRAFT, | |
945 | }; | |
946 | ||
947 | struct tc_root_qopt_offload { | |
948 | enum tc_root_command command; | |
949 | u32 handle; | |
950 | bool ingress; | |
951 | }; | |
952 | ||
1da177e4 | 953 | #endif |