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