blob: d15c40c7bde733c3b6d357f4015b1e9c57c4f75a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __NET_PKT_CLS_H
2#define __NET_PKT_CLS_H
3
4#include <linux/pkt_cls.h>
Cong Wang7aa00452017-10-26 18:24:28 -07005#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <net/sch_generic.h>
7#include <net/act_api.h>
8
9/* Basic packet classifier frontend definitions. */
10
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +000011struct tcf_walker {
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 int stop;
13 int skip;
14 int count;
WANG Cong8113c092017-08-04 21:31:43 -070015 int (*fn)(struct tcf_proto *, void *node, struct tcf_walker *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016};
17
Joe Perches5c152572013-07-30 22:47:13 -070018int register_tcf_proto_ops(struct tcf_proto_ops *ops);
19int unregister_tcf_proto_ops(struct tcf_proto_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Jiri Pirko8c4083b2017-10-19 15:50:29 +020021enum tcf_block_binder_type {
22 TCF_BLOCK_BINDER_TYPE_UNSPEC,
Jiri Pirko6e40cf22017-10-19 15:50:30 +020023 TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS,
24 TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS,
Jiri Pirko8c4083b2017-10-19 15:50:29 +020025};
26
27struct tcf_block_ext_info {
28 enum tcf_block_binder_type binder_type;
29};
30
Jiri Pirkoacb67442017-10-19 15:50:31 +020031struct tcf_block_cb;
Cong Wang7aa00452017-10-26 18:24:28 -070032bool tcf_queue_work(struct work_struct *work);
Jiri Pirkoacb67442017-10-19 15:50:31 +020033
Jiri Pirko8ae70032017-02-15 11:57:50 +010034#ifdef CONFIG_NET_CLS
WANG Cong367a8ce2017-05-23 09:42:37 -070035struct tcf_chain *tcf_chain_get(struct tcf_block *block, u32 chain_index,
36 bool create);
Jiri Pirko5bc17012017-05-17 11:08:01 +020037void tcf_chain_put(struct tcf_chain *chain);
Jiri Pirko6529eab2017-05-17 11:07:55 +020038int tcf_block_get(struct tcf_block **p_block,
Jiri Pirko69d78ef2017-10-13 14:00:57 +020039 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q);
Jiri Pirko8c4083b2017-10-19 15:50:29 +020040int tcf_block_get_ext(struct tcf_block **p_block,
41 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
42 struct tcf_block_ext_info *ei);
Jiri Pirko6529eab2017-05-17 11:07:55 +020043void tcf_block_put(struct tcf_block *block);
Jiri Pirko8c4083b2017-10-19 15:50:29 +020044void tcf_block_put_ext(struct tcf_block *block,
45 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
46 struct tcf_block_ext_info *ei);
Jiri Pirko44186462017-10-13 14:00:59 +020047
48static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
49{
50 return block->q;
51}
52
53static inline struct net_device *tcf_block_dev(struct tcf_block *block)
54{
55 return tcf_block_q(block)->dev_queue->dev;
56}
57
Jiri Pirkoacb67442017-10-19 15:50:31 +020058void *tcf_block_cb_priv(struct tcf_block_cb *block_cb);
59struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
60 tc_setup_cb_t *cb, void *cb_ident);
61void tcf_block_cb_incref(struct tcf_block_cb *block_cb);
62unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb);
63struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
64 tc_setup_cb_t *cb, void *cb_ident,
65 void *cb_priv);
66int tcf_block_cb_register(struct tcf_block *block,
67 tc_setup_cb_t *cb, void *cb_ident,
68 void *cb_priv);
69void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb);
70void tcf_block_cb_unregister(struct tcf_block *block,
71 tc_setup_cb_t *cb, void *cb_ident);
72
Jiri Pirko87d83092017-05-17 11:07:54 +020073int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
74 struct tcf_result *res, bool compat_mode);
75
Jiri Pirko8ae70032017-02-15 11:57:50 +010076#else
Jiri Pirko6529eab2017-05-17 11:07:55 +020077static inline
78int tcf_block_get(struct tcf_block **p_block,
Jiri Pirko69d78ef2017-10-13 14:00:57 +020079 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q)
Jiri Pirko6529eab2017-05-17 11:07:55 +020080{
81 return 0;
82}
83
Jiri Pirko8c4083b2017-10-19 15:50:29 +020084static inline
85int tcf_block_get_ext(struct tcf_block **p_block,
86 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
87 struct tcf_block_ext_info *ei)
88{
89 return 0;
90}
91
Jiri Pirko6529eab2017-05-17 11:07:55 +020092static inline void tcf_block_put(struct tcf_block *block)
Jiri Pirko8ae70032017-02-15 11:57:50 +010093{
94}
Jiri Pirko87d83092017-05-17 11:07:54 +020095
Jiri Pirko8c4083b2017-10-19 15:50:29 +020096static inline
97void tcf_block_put_ext(struct tcf_block *block,
98 struct tcf_proto __rcu **p_filter_chain, struct Qdisc *q,
99 struct tcf_block_ext_info *ei)
100{
101}
102
Jiri Pirko44186462017-10-13 14:00:59 +0200103static inline struct Qdisc *tcf_block_q(struct tcf_block *block)
104{
105 return NULL;
106}
107
108static inline struct net_device *tcf_block_dev(struct tcf_block *block)
109{
110 return NULL;
111}
112
Jiri Pirkoacb67442017-10-19 15:50:31 +0200113static inline
114int tc_setup_cb_block_register(struct tcf_block *block, tc_setup_cb_t *cb,
115 void *cb_priv)
116{
117 return 0;
118}
119
120static inline
121void tc_setup_cb_block_unregister(struct tcf_block *block, tc_setup_cb_t *cb,
122 void *cb_priv)
123{
124}
125
126static inline
127void *tcf_block_cb_priv(struct tcf_block_cb *block_cb)
128{
129 return NULL;
130}
131
132static inline
133struct tcf_block_cb *tcf_block_cb_lookup(struct tcf_block *block,
134 tc_setup_cb_t *cb, void *cb_ident)
135{
136 return NULL;
137}
138
139static inline
140void tcf_block_cb_incref(struct tcf_block_cb *block_cb)
141{
142}
143
144static inline
145unsigned int tcf_block_cb_decref(struct tcf_block_cb *block_cb)
146{
147 return 0;
148}
149
150static inline
151struct tcf_block_cb *__tcf_block_cb_register(struct tcf_block *block,
152 tc_setup_cb_t *cb, void *cb_ident,
153 void *cb_priv)
154{
155 return NULL;
156}
157
158static inline
159int tcf_block_cb_register(struct tcf_block *block,
160 tc_setup_cb_t *cb, void *cb_ident,
161 void *cb_priv)
162{
163 return 0;
164}
165
166static inline
167void __tcf_block_cb_unregister(struct tcf_block_cb *block_cb)
168{
169}
170
171static inline
172void tcf_block_cb_unregister(struct tcf_block *block,
173 tc_setup_cb_t *cb, void *cb_ident)
174{
175}
176
Jiri Pirko87d83092017-05-17 11:07:54 +0200177static inline int tcf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
178 struct tcf_result *res, bool compat_mode)
179{
180 return TC_ACT_UNSPEC;
181}
Jiri Pirko8ae70032017-02-15 11:57:50 +0100182#endif
Jiri Pirkocf1facd2017-02-09 14:38:56 +0100183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static inline unsigned long
185__cls_set_class(unsigned long *clp, unsigned long cl)
186{
WANG Conga0efb802014-09-30 16:07:24 -0700187 return xchg(clp, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
190static inline unsigned long
Jiri Pirko34e37592017-10-13 14:01:00 +0200191cls_set_class(struct Qdisc *q, unsigned long *clp, unsigned long cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 unsigned long old_cl;
Jiri Pirko34e37592017-10-13 14:01:00 +0200194
195 sch_tree_lock(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 old_cl = __cls_set_class(clp, cl);
Jiri Pirko34e37592017-10-13 14:01:00 +0200197 sch_tree_unlock(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return old_cl;
199}
200
201static inline void
202tcf_bind_filter(struct tcf_proto *tp, struct tcf_result *r, unsigned long base)
203{
Jiri Pirko34e37592017-10-13 14:01:00 +0200204 struct Qdisc *q = tp->chain->block->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 unsigned long cl;
206
Jiri Pirko34e37592017-10-13 14:01:00 +0200207 /* Check q as it is not set for shared blocks. In that case,
208 * setting class is not supported.
209 */
210 if (!q)
211 return;
212 cl = q->ops->cl_ops->bind_tcf(q, base, r->classid);
213 cl = cls_set_class(q, &r->class, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (cl)
Jiri Pirko34e37592017-10-13 14:01:00 +0200215 q->ops->cl_ops->unbind_tcf(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218static inline void
219tcf_unbind_filter(struct tcf_proto *tp, struct tcf_result *r)
220{
Jiri Pirko34e37592017-10-13 14:01:00 +0200221 struct Qdisc *q = tp->chain->block->q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 unsigned long cl;
223
Jiri Pirko34e37592017-10-13 14:01:00 +0200224 if (!q)
225 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if ((cl = __cls_set_class(&r->class, 0)) != 0)
Jiri Pirko34e37592017-10-13 14:01:00 +0200227 q->ops->cl_ops->unbind_tcf(q, cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000230struct tcf_exts {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#ifdef CONFIG_NET_CLS_ACT
WANG Cong33be6272013-12-15 20:15:05 -0800232 __u32 type; /* for backward compat(TCA_OLD_COMPAT) */
WANG Cong22dc13c2016-08-13 22:35:00 -0700233 int nr_actions;
234 struct tc_action **actions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#endif
WANG Cong5da57f42013-12-15 20:15:07 -0800236 /* Map to export classifier specific extension TLV types to the
237 * generic extensions API. Unsupported extensions must be set to 0.
238 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int action;
240 int police;
241};
242
WANG Congb9a24bb2016-08-19 12:36:54 -0700243static inline int tcf_exts_init(struct tcf_exts *exts, int action, int police)
WANG Cong33be6272013-12-15 20:15:05 -0800244{
245#ifdef CONFIG_NET_CLS_ACT
WANG Cong5da57f42013-12-15 20:15:07 -0800246 exts->type = 0;
WANG Cong22dc13c2016-08-13 22:35:00 -0700247 exts->nr_actions = 0;
248 exts->actions = kcalloc(TCA_ACT_MAX_PRIO, sizeof(struct tc_action *),
249 GFP_KERNEL);
WANG Congb9a24bb2016-08-19 12:36:54 -0700250 if (!exts->actions)
251 return -ENOMEM;
WANG Cong33be6272013-12-15 20:15:05 -0800252#endif
WANG Cong5da57f42013-12-15 20:15:07 -0800253 exts->action = action;
254 exts->police = police;
WANG Congb9a24bb2016-08-19 12:36:54 -0700255 return 0;
WANG Cong33be6272013-12-15 20:15:05 -0800256}
257
WANG Cong22dc13c2016-08-13 22:35:00 -0700258static inline void tcf_exts_to_list(const struct tcf_exts *exts,
259 struct list_head *actions)
260{
261#ifdef CONFIG_NET_CLS_ACT
262 int i;
263
264 for (i = 0; i < exts->nr_actions; i++) {
265 struct tc_action *a = exts->actions[i];
266
Hadar Hen Zionfa5effe2016-09-27 11:09:51 +0300267 list_add_tail(&a->list, actions);
WANG Cong22dc13c2016-08-13 22:35:00 -0700268 }
269#endif
270}
271
Jakub Kicinskid897a632017-05-31 08:06:43 -0700272static inline void
273tcf_exts_stats_update(const struct tcf_exts *exts,
274 u64 bytes, u64 packets, u64 lastuse)
275{
276#ifdef CONFIG_NET_CLS_ACT
277 int i;
278
279 preempt_disable();
280
281 for (i = 0; i < exts->nr_actions; i++) {
282 struct tc_action *a = exts->actions[i];
283
284 tcf_action_stats_update(a, bytes, packets, lastuse);
285 }
286
287 preempt_enable();
288#endif
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291/**
Jiri Pirko3bcc0ce2017-08-04 14:28:58 +0200292 * tcf_exts_has_actions - check if at least one action is present
293 * @exts: tc filter extensions handle
294 *
295 * Returns true if at least one action is present.
296 */
297static inline bool tcf_exts_has_actions(struct tcf_exts *exts)
298{
WANG Cong2734437e2016-08-13 22:34:59 -0700299#ifdef CONFIG_NET_CLS_ACT
Jiri Pirko3bcc0ce2017-08-04 14:28:58 +0200300 return exts->nr_actions;
301#else
302 return false;
303#endif
304}
WANG Cong2734437e2016-08-13 22:34:59 -0700305
Jiri Pirko3bcc0ce2017-08-04 14:28:58 +0200306/**
307 * tcf_exts_has_one_action - check if exactly one action is present
308 * @exts: tc filter extensions handle
309 *
310 * Returns true if exactly one action is present.
311 */
312static inline bool tcf_exts_has_one_action(struct tcf_exts *exts)
313{
314#ifdef CONFIG_NET_CLS_ACT
315 return exts->nr_actions == 1;
316#else
317 return false;
318#endif
319}
WANG Cong2734437e2016-08-13 22:34:59 -0700320
Jiri Pirkoaf69afc2017-08-04 14:28:59 +0200321/**
322 * tcf_exts_exec - execute tc filter extensions
323 * @skb: socket buffer
324 * @exts: tc filter extensions handle
325 * @res: desired result
326 *
Jiri Pirkoaf089e72017-08-04 14:29:01 +0200327 * Executes all configured extensions. Returns TC_ACT_OK on a normal execution,
Jiri Pirkoaf69afc2017-08-04 14:28:59 +0200328 * a negative number if the filter must be considered unmatched or
329 * a positive action code (TC_ACT_*) which must be returned to the
330 * underlying layer.
331 */
332static inline int
333tcf_exts_exec(struct sk_buff *skb, struct tcf_exts *exts,
334 struct tcf_result *res)
335{
336#ifdef CONFIG_NET_CLS_ACT
Jiri Pirkoec1a9cc2017-08-04 14:29:02 +0200337 return tcf_action_exec(skb, exts->actions, exts->nr_actions, res);
Jiri Pirkoaf69afc2017-08-04 14:28:59 +0200338#endif
Jiri Pirkoaf089e72017-08-04 14:29:01 +0200339 return TC_ACT_OK;
Jiri Pirkoaf69afc2017-08-04 14:28:59 +0200340}
341
Joe Perches5c152572013-07-30 22:47:13 -0700342int tcf_exts_validate(struct net *net, struct tcf_proto *tp,
343 struct nlattr **tb, struct nlattr *rate_tlv,
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700344 struct tcf_exts *exts, bool ovr);
WANG Cong18d02642014-09-25 10:26:37 -0700345void tcf_exts_destroy(struct tcf_exts *exts);
Jiri Pirko9b0d4442017-08-04 14:29:15 +0200346void tcf_exts_change(struct tcf_exts *dst, struct tcf_exts *src);
WANG Cong5da57f42013-12-15 20:15:07 -0800347int tcf_exts_dump(struct sk_buff *skb, struct tcf_exts *exts);
348int tcf_exts_dump_stats(struct sk_buff *skb, struct tcf_exts *exts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350/**
351 * struct tcf_pkt_info - packet information
352 */
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000353struct tcf_pkt_info {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 unsigned char * ptr;
355 int nexthdr;
356};
357
358#ifdef CONFIG_NET_EMATCH
359
360struct tcf_ematch_ops;
361
362/**
363 * struct tcf_ematch - extended match (ematch)
364 *
365 * @matchid: identifier to allow userspace to reidentify a match
366 * @flags: flags specifying attributes and the relation to other matches
367 * @ops: the operations lookup table of the corresponding ematch module
368 * @datalen: length of the ematch specific configuration data
369 * @data: ematch specific data
370 */
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000371struct tcf_ematch {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct tcf_ematch_ops * ops;
373 unsigned long data;
374 unsigned int datalen;
375 u16 matchid;
376 u16 flags;
John Fastabend82a470f2014-10-05 21:27:53 -0700377 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};
379
380static inline int tcf_em_is_container(struct tcf_ematch *em)
381{
382 return !em->ops;
383}
384
385static inline int tcf_em_is_simple(struct tcf_ematch *em)
386{
387 return em->flags & TCF_EM_SIMPLE;
388}
389
390static inline int tcf_em_is_inverted(struct tcf_ematch *em)
391{
392 return em->flags & TCF_EM_INVERT;
393}
394
395static inline int tcf_em_last_match(struct tcf_ematch *em)
396{
397 return (em->flags & TCF_EM_REL_MASK) == TCF_EM_REL_END;
398}
399
400static inline int tcf_em_early_end(struct tcf_ematch *em, int result)
401{
402 if (tcf_em_last_match(em))
403 return 1;
404
405 if (result == 0 && em->flags & TCF_EM_REL_AND)
406 return 1;
407
408 if (result != 0 && em->flags & TCF_EM_REL_OR)
409 return 1;
410
411 return 0;
412}
413
414/**
415 * struct tcf_ematch_tree - ematch tree handle
416 *
417 * @hdr: ematch tree header supplied by userspace
418 * @matches: array of ematches
419 */
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000420struct tcf_ematch_tree {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct tcf_ematch_tree_hdr hdr;
422 struct tcf_ematch * matches;
423
424};
425
426/**
427 * struct tcf_ematch_ops - ematch module operations
428 *
429 * @kind: identifier (kind) of this ematch module
430 * @datalen: length of expected configuration data (optional)
431 * @change: called during validation (optional)
432 * @match: called during ematch tree evaluation, must return 1/0
433 * @destroy: called during destroyage (optional)
434 * @dump: called during dumping process (optional)
435 * @owner: owner, must be set to THIS_MODULE
436 * @link: link to previous/next ematch module (internal use)
437 */
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000438struct tcf_ematch_ops {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int kind;
440 int datalen;
John Fastabend82a470f2014-10-05 21:27:53 -0700441 int (*change)(struct net *net, void *,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int, struct tcf_ematch *);
443 int (*match)(struct sk_buff *, struct tcf_ematch *,
444 struct tcf_pkt_info *);
John Fastabend82a470f2014-10-05 21:27:53 -0700445 void (*destroy)(struct tcf_ematch *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int (*dump)(struct sk_buff *, struct tcf_ematch *);
447 struct module *owner;
448 struct list_head link;
449};
450
Joe Perches5c152572013-07-30 22:47:13 -0700451int tcf_em_register(struct tcf_ematch_ops *);
452void tcf_em_unregister(struct tcf_ematch_ops *);
453int tcf_em_tree_validate(struct tcf_proto *, struct nlattr *,
454 struct tcf_ematch_tree *);
John Fastabend82a470f2014-10-05 21:27:53 -0700455void tcf_em_tree_destroy(struct tcf_ematch_tree *);
Joe Perches5c152572013-07-30 22:47:13 -0700456int tcf_em_tree_dump(struct sk_buff *, struct tcf_ematch_tree *, int);
457int __tcf_em_tree_match(struct sk_buff *, struct tcf_ematch_tree *,
458 struct tcf_pkt_info *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * tcf_em_tree_match - evaulate an ematch tree
462 *
463 * @skb: socket buffer of the packet in question
464 * @tree: ematch tree to be used for evaluation
465 * @info: packet information examined by classifier
466 *
467 * This function matches @skb against the ematch tree in @tree by going
468 * through all ematches respecting their logic relations returning
469 * as soon as the result is obvious.
470 *
471 * Returns 1 if the ematch tree as-one matches, no ematches are configured
472 * or ematch is not enabled in the kernel, otherwise 0 is returned.
473 */
474static inline int tcf_em_tree_match(struct sk_buff *skb,
475 struct tcf_ematch_tree *tree,
476 struct tcf_pkt_info *info)
477{
478 if (tree->hdr.nmatches)
479 return __tcf_em_tree_match(skb, tree, info);
480 else
481 return 1;
482}
483
Patrick McHardydb3d99c2007-07-11 19:46:26 -0700484#define MODULE_ALIAS_TCF_EMATCH(kind) MODULE_ALIAS("ematch-kind-" __stringify(kind))
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486#else /* CONFIG_NET_EMATCH */
487
Eric Dumazetfd2c3ef2009-11-03 03:26:03 +0000488struct tcf_ematch_tree {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489};
490
491#define tcf_em_tree_validate(tp, tb, t) ((void)(t), 0)
John Fastabend82a470f2014-10-05 21:27:53 -0700492#define tcf_em_tree_destroy(t) do { (void)(t); } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493#define tcf_em_tree_dump(skb, t, tlv) (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494#define tcf_em_tree_match(skb, t, info) ((void)(info), 1)
495
496#endif /* CONFIG_NET_EMATCH */
497
498static inline unsigned char * tcf_get_base_ptr(struct sk_buff *skb, int layer)
499{
500 switch (layer) {
501 case TCF_LAYER_LINK:
502 return skb->data;
503 case TCF_LAYER_NETWORK:
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700504 return skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 case TCF_LAYER_TRANSPORT:
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -0700506 return skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
509 return NULL;
510}
511
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700512static inline int tcf_valid_offset(const struct sk_buff *skb,
513 const unsigned char *ptr, const int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
David S. Millerda521b22010-12-21 12:43:16 -0800515 return likely((ptr + len) <= skb_tail_pointer(skb) &&
516 ptr >= skb->head &&
517 (ptr <= (ptr + len)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520#ifdef CONFIG_NET_CLS_IND
Denis V. Lunev0eeb8ff2007-12-04 01:15:45 -0800521#include <net/net_namespace.h>
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523static inline int
WANG Cong2519a602014-01-09 16:14:02 -0800524tcf_change_indev(struct net *net, struct nlattr *indev_tlv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
WANG Cong2519a602014-01-09 16:14:02 -0800526 char indev[IFNAMSIZ];
Patrick McHardyc01003c2007-03-29 11:46:52 -0700527 struct net_device *dev;
528
WANG Cong2519a602014-01-09 16:14:02 -0800529 if (nla_strlcpy(indev, indev_tlv, IFNAMSIZ) >= IFNAMSIZ)
530 return -EINVAL;
531 dev = __dev_get_by_name(net, indev);
532 if (!dev)
533 return -ENODEV;
534 return dev->ifindex;
535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
WANG Cong2519a602014-01-09 16:14:02 -0800537static inline bool
538tcf_match_indev(struct sk_buff *skb, int ifindex)
539{
540 if (!ifindex)
541 return true;
542 if (!skb->skb_iif)
543 return false;
544 return ifindex == skb->skb_iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546#endif /* CONFIG_NET_CLS_IND */
547
Jiri Pirko208c0f42017-10-19 15:50:32 +0200548int tc_setup_cb_call(struct tcf_block *block, struct tcf_exts *exts,
549 enum tc_setup_type type, void *type_data, bool err_stop);
Jiri Pirko717503b2017-10-11 09:41:09 +0200550
Jiri Pirko8c4083b2017-10-19 15:50:29 +0200551enum tc_block_command {
552 TC_BLOCK_BIND,
553 TC_BLOCK_UNBIND,
554};
555
556struct tc_block_offload {
557 enum tc_block_command command;
558 enum tcf_block_binder_type binder_type;
559 struct tcf_block *block;
560};
561
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200562struct tc_cls_common_offload {
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200563 u32 chain_index;
564 __be16 protocol;
Jiri Pirkod7c1c8d2017-08-07 10:15:30 +0200565 u32 prio;
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200566};
567
568static inline void
569tc_cls_common_offload_init(struct tc_cls_common_offload *cls_common,
570 const struct tcf_proto *tp)
571{
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200572 cls_common->chain_index = tp->chain->index;
573 cls_common->protocol = tp->protocol;
Jiri Pirkod7c1c8d2017-08-07 10:15:30 +0200574 cls_common->prio = tp->prio;
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200575}
576
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800577struct tc_cls_u32_knode {
578 struct tcf_exts *exts;
John Fastabende0148602016-02-17 14:59:30 -0800579 struct tc_u32_sel *sel;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800580 u32 handle;
581 u32 val;
582 u32 mask;
583 u32 link_handle;
John Fastabende0148602016-02-17 14:59:30 -0800584 u8 fshift;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800585};
586
587struct tc_cls_u32_hnode {
588 u32 handle;
589 u32 prio;
590 unsigned int divisor;
591};
592
593enum tc_clsu32_command {
594 TC_CLSU32_NEW_KNODE,
595 TC_CLSU32_REPLACE_KNODE,
596 TC_CLSU32_DELETE_KNODE,
597 TC_CLSU32_NEW_HNODE,
598 TC_CLSU32_REPLACE_HNODE,
599 TC_CLSU32_DELETE_HNODE,
600};
601
602struct tc_cls_u32_offload {
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200603 struct tc_cls_common_offload common;
John Fastabenda1b7c5f2016-02-16 21:17:09 -0800604 /* knode values */
605 enum tc_clsu32_command command;
606 union {
607 struct tc_cls_u32_knode knode;
608 struct tc_cls_u32_hnode hnode;
609 };
610};
611
Jiri Pirko7b06e8a2017-08-09 14:30:35 +0200612static inline bool tc_can_offload(const struct net_device *dev)
John Fastabend6843e7a2016-02-26 07:53:49 -0800613{
Jiri Pirko70b5aee2017-11-01 11:47:41 +0100614 return dev->features & NETIF_F_HW_TC;
John Fastabend6843e7a2016-02-26 07:53:49 -0800615}
616
Hadar Hen Zion55330f02016-12-01 14:06:33 +0200617static inline bool tc_skip_hw(u32 flags)
618{
619 return (flags & TCA_CLS_FLAGS_SKIP_HW) ? true : false;
620}
621
Samudrala, Sridhard34e3e12016-05-12 17:08:23 -0700622static inline bool tc_skip_sw(u32 flags)
623{
624 return (flags & TCA_CLS_FLAGS_SKIP_SW) ? true : false;
625}
626
627/* SKIP_HW and SKIP_SW are mutually exclusive flags. */
628static inline bool tc_flags_valid(u32 flags)
629{
630 if (flags & ~(TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW))
631 return false;
632
633 if (!(flags ^ (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)))
634 return false;
635
636 return true;
637}
638
Or Gerlitze6960282017-02-16 10:31:12 +0200639static inline bool tc_in_hw(u32 flags)
640{
641 return (flags & TCA_CLS_FLAGS_IN_HW) ? true : false;
642}
643
Amir Vadai5b33f482016-03-08 12:42:29 +0200644enum tc_fl_command {
645 TC_CLSFLOWER_REPLACE,
646 TC_CLSFLOWER_DESTROY,
Amir Vadai10cbc682016-05-13 12:55:37 +0000647 TC_CLSFLOWER_STATS,
Amir Vadai5b33f482016-03-08 12:42:29 +0200648};
649
650struct tc_cls_flower_offload {
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200651 struct tc_cls_common_offload common;
Amir Vadai5b33f482016-03-08 12:42:29 +0200652 enum tc_fl_command command;
Amir Vadai8208d212016-03-11 11:08:45 +0200653 unsigned long cookie;
Amir Vadai5b33f482016-03-08 12:42:29 +0200654 struct flow_dissector *dissector;
655 struct fl_flow_key *mask;
656 struct fl_flow_key *key;
657 struct tcf_exts *exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700658 u32 classid;
Amir Vadai5b33f482016-03-08 12:42:29 +0200659};
660
Yotam Gigib87f7932016-07-21 12:03:12 +0200661enum tc_matchall_command {
662 TC_CLSMATCHALL_REPLACE,
663 TC_CLSMATCHALL_DESTROY,
664};
665
666struct tc_cls_matchall_offload {
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200667 struct tc_cls_common_offload common;
Yotam Gigib87f7932016-07-21 12:03:12 +0200668 enum tc_matchall_command command;
669 struct tcf_exts *exts;
670 unsigned long cookie;
671};
672
Jakub Kicinski332ae8e2016-09-21 11:43:53 +0100673enum tc_clsbpf_command {
674 TC_CLSBPF_ADD,
675 TC_CLSBPF_REPLACE,
676 TC_CLSBPF_DESTROY,
Jakub Kicinski68d64062016-09-21 11:44:02 +0100677 TC_CLSBPF_STATS,
Jakub Kicinski332ae8e2016-09-21 11:43:53 +0100678};
679
680struct tc_cls_bpf_offload {
Jiri Pirko5fd9fc42017-08-07 10:15:29 +0200681 struct tc_cls_common_offload common;
Jakub Kicinski332ae8e2016-09-21 11:43:53 +0100682 enum tc_clsbpf_command command;
683 struct tcf_exts *exts;
684 struct bpf_prog *prog;
685 const char *name;
686 bool exts_integrated;
Jakub Kicinski0d01d452016-09-21 11:43:54 +0100687 u32 gen_flags;
Jakub Kicinski332ae8e2016-09-21 11:43:53 +0100688};
689
Amritha Nambiar4e8b86c2017-09-07 04:00:06 -0700690struct tc_mqprio_qopt_offload {
691 /* struct tc_mqprio_qopt must always be the first element */
692 struct tc_mqprio_qopt qopt;
693 u16 mode;
694 u16 shaper;
695 u32 flags;
696 u64 min_rate[TC_QOPT_MAX_QUEUE];
697 u64 max_rate[TC_QOPT_MAX_QUEUE];
698};
Jamal Hadi Salim1045ba72017-01-24 07:02:41 -0500699
700/* This structure holds cookie structure that is passed from user
701 * to the kernel for actions and classifiers
702 */
703struct tc_cookie {
704 u8 *data;
705 u32 len;
706};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707#endif