1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * net/sched/cls_flower.c Flower classifier
4 *
5 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
6 */
7
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/rhashtable.h>
12 #include <linux/workqueue.h>
13 #include <linux/refcount.h>
14 #include <linux/bitfield.h>
15
16 #include <linux/if_ether.h>
17 #include <linux/in6.h>
18 #include <linux/ip.h>
19 #include <linux/mpls.h>
20 #include <linux/ppp_defs.h>
21
22 #include <net/sch_generic.h>
23 #include <net/pkt_cls.h>
24 #include <net/pkt_sched.h>
25 #include <net/ip.h>
26 #include <net/flow_dissector.h>
27 #include <net/geneve.h>
28 #include <net/vxlan.h>
29 #include <net/erspan.h>
30 #include <net/gtp.h>
31 #include <net/pfcp.h>
32 #include <net/tc_wrapper.h>
33
34 #include <net/dst.h>
35 #include <net/dst_metadata.h>
36
37 #include <uapi/linux/netfilter/nf_conntrack_common.h>
38
39 #define TCA_FLOWER_KEY_CT_FLAGS_MAX \
40 ((__TCA_FLOWER_KEY_CT_FLAGS_MAX - 1) << 1)
41 #define TCA_FLOWER_KEY_CT_FLAGS_MASK \
42 (TCA_FLOWER_KEY_CT_FLAGS_MAX - 1)
43
44 #define TCA_FLOWER_KEY_FLAGS_POLICY_MASK \
45 (TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT | \
46 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST)
47
48 #define TCA_FLOWER_KEY_ENC_FLAGS_POLICY_MASK \
49 (TCA_FLOWER_KEY_FLAGS_TUNNEL_CSUM | \
50 TCA_FLOWER_KEY_FLAGS_TUNNEL_DONT_FRAGMENT | \
51 TCA_FLOWER_KEY_FLAGS_TUNNEL_OAM | \
52 TCA_FLOWER_KEY_FLAGS_TUNNEL_CRIT_OPT)
53
54 struct fl_flow_key {
55 struct flow_dissector_key_meta meta;
56 struct flow_dissector_key_control control;
57 struct flow_dissector_key_control enc_control;
58 struct flow_dissector_key_basic basic;
59 struct flow_dissector_key_eth_addrs eth;
60 struct flow_dissector_key_vlan vlan;
61 struct flow_dissector_key_vlan cvlan;
62 union {
63 struct flow_dissector_key_ipv4_addrs ipv4;
64 struct flow_dissector_key_ipv6_addrs ipv6;
65 };
66 struct flow_dissector_key_ports tp;
67 struct flow_dissector_key_icmp icmp;
68 struct flow_dissector_key_arp arp;
69 struct flow_dissector_key_keyid enc_key_id;
70 union {
71 struct flow_dissector_key_ipv4_addrs enc_ipv4;
72 struct flow_dissector_key_ipv6_addrs enc_ipv6;
73 };
74 struct flow_dissector_key_ports enc_tp;
75 struct flow_dissector_key_mpls mpls;
76 struct flow_dissector_key_tcp tcp;
77 struct flow_dissector_key_ip ip;
78 struct flow_dissector_key_ip enc_ip;
79 struct flow_dissector_key_enc_opts enc_opts;
80 struct flow_dissector_key_ports_range tp_range;
81 struct flow_dissector_key_ct ct;
82 struct flow_dissector_key_hash hash;
83 struct flow_dissector_key_num_of_vlans num_of_vlans;
84 struct flow_dissector_key_pppoe pppoe;
85 struct flow_dissector_key_l2tpv3 l2tpv3;
86 struct flow_dissector_key_ipsec ipsec;
87 struct flow_dissector_key_cfm cfm;
88 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
89
90 struct fl_flow_mask_range {
91 unsigned short int start;
92 unsigned short int end;
93 };
94
95 struct fl_flow_mask {
96 struct fl_flow_key key;
97 struct fl_flow_mask_range range;
98 u32 flags;
99 struct rhash_head ht_node;
100 struct rhashtable ht;
101 struct rhashtable_params filter_ht_params;
102 struct flow_dissector dissector;
103 struct list_head filters;
104 struct rcu_work rwork;
105 struct list_head list;
106 refcount_t refcnt;
107 };
108
109 struct fl_flow_tmplt {
110 struct fl_flow_key dummy_key;
111 struct fl_flow_key mask;
112 struct flow_dissector dissector;
113 struct tcf_chain *chain;
114 };
115
116 struct cls_fl_head {
117 struct rhashtable ht;
118 spinlock_t masks_lock; /* Protect masks list */
119 struct list_head masks;
120 struct list_head hw_filters;
121 struct rcu_work rwork;
122 struct idr handle_idr;
123 };
124
125 struct cls_fl_filter {
126 struct fl_flow_mask *mask;
127 struct rhash_head ht_node;
128 struct fl_flow_key mkey;
129 struct tcf_exts exts;
130 struct tcf_result res;
131 struct fl_flow_key key;
132 struct list_head list;
133 struct list_head hw_list;
134 u32 handle;
135 u32 flags;
136 u32 in_hw_count;
137 u8 needs_tc_skb_ext:1;
138 struct rcu_work rwork;
139 struct net_device *hw_dev;
140 /* Flower classifier is unlocked, which means that its reference counter
141 * can be changed concurrently without any kind of external
142 * synchronization. Use atomic reference counter to be concurrency-safe.
143 */
144 refcount_t refcnt;
145 bool deleted;
146 };
147
148 static const struct rhashtable_params mask_ht_params = {
149 .key_offset = offsetof(struct fl_flow_mask, key),
150 .key_len = sizeof(struct fl_flow_key),
151 .head_offset = offsetof(struct fl_flow_mask, ht_node),
152 .automatic_shrinking = true,
153 };
154
fl_mask_range(const struct fl_flow_mask * mask)155 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
156 {
157 return mask->range.end - mask->range.start;
158 }
159
fl_mask_update_range(struct fl_flow_mask * mask)160 static void fl_mask_update_range(struct fl_flow_mask *mask)
161 {
162 const u8 *bytes = (const u8 *) &mask->key;
163 size_t size = sizeof(mask->key);
164 size_t i, first = 0, last;
165
166 for (i = 0; i < size; i++) {
167 if (bytes[i]) {
168 first = i;
169 break;
170 }
171 }
172 last = first;
173 for (i = size - 1; i != first; i--) {
174 if (bytes[i]) {
175 last = i;
176 break;
177 }
178 }
179 mask->range.start = rounddown(first, sizeof(long));
180 mask->range.end = roundup(last + 1, sizeof(long));
181 }
182
fl_key_get_start(struct fl_flow_key * key,const struct fl_flow_mask * mask)183 static void *fl_key_get_start(struct fl_flow_key *key,
184 const struct fl_flow_mask *mask)
185 {
186 return (u8 *) key + mask->range.start;
187 }
188
fl_set_masked_key(struct fl_flow_key * mkey,struct fl_flow_key * key,struct fl_flow_mask * mask)189 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
190 struct fl_flow_mask *mask)
191 {
192 const long *lkey = fl_key_get_start(key, mask);
193 const long *lmask = fl_key_get_start(&mask->key, mask);
194 long *lmkey = fl_key_get_start(mkey, mask);
195 int i;
196
197 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
198 *lmkey++ = *lkey++ & *lmask++;
199 }
200
fl_mask_fits_tmplt(struct fl_flow_tmplt * tmplt,struct fl_flow_mask * mask)201 static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
202 struct fl_flow_mask *mask)
203 {
204 const long *lmask = fl_key_get_start(&mask->key, mask);
205 const long *ltmplt;
206 int i;
207
208 if (!tmplt)
209 return true;
210 ltmplt = fl_key_get_start(&tmplt->mask, mask);
211 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
212 if (~*ltmplt++ & *lmask++)
213 return false;
214 }
215 return true;
216 }
217
fl_clear_masked_range(struct fl_flow_key * key,struct fl_flow_mask * mask)218 static void fl_clear_masked_range(struct fl_flow_key *key,
219 struct fl_flow_mask *mask)
220 {
221 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
222 }
223
fl_range_port_dst_cmp(struct cls_fl_filter * filter,struct fl_flow_key * key,struct fl_flow_key * mkey)224 static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter,
225 struct fl_flow_key *key,
226 struct fl_flow_key *mkey)
227 {
228 u16 min_mask, max_mask, min_val, max_val;
229
230 min_mask = ntohs(filter->mask->key.tp_range.tp_min.dst);
231 max_mask = ntohs(filter->mask->key.tp_range.tp_max.dst);
232 min_val = ntohs(filter->key.tp_range.tp_min.dst);
233 max_val = ntohs(filter->key.tp_range.tp_max.dst);
234
235 if (min_mask && max_mask) {
236 if (ntohs(key->tp_range.tp.dst) < min_val ||
237 ntohs(key->tp_range.tp.dst) > max_val)
238 return false;
239
240 /* skb does not have min and max values */
241 mkey->tp_range.tp_min.dst = filter->mkey.tp_range.tp_min.dst;
242 mkey->tp_range.tp_max.dst = filter->mkey.tp_range.tp_max.dst;
243 }
244 return true;
245 }
246
fl_range_port_src_cmp(struct cls_fl_filter * filter,struct fl_flow_key * key,struct fl_flow_key * mkey)247 static bool fl_range_port_src_cmp(struct cls_fl_filter *filter,
248 struct fl_flow_key *key,
249 struct fl_flow_key *mkey)
250 {
251 u16 min_mask, max_mask, min_val, max_val;
252
253 min_mask = ntohs(filter->mask->key.tp_range.tp_min.src);
254 max_mask = ntohs(filter->mask->key.tp_range.tp_max.src);
255 min_val = ntohs(filter->key.tp_range.tp_min.src);
256 max_val = ntohs(filter->key.tp_range.tp_max.src);
257
258 if (min_mask && max_mask) {
259 if (ntohs(key->tp_range.tp.src) < min_val ||
260 ntohs(key->tp_range.tp.src) > max_val)
261 return false;
262
263 /* skb does not have min and max values */
264 mkey->tp_range.tp_min.src = filter->mkey.tp_range.tp_min.src;
265 mkey->tp_range.tp_max.src = filter->mkey.tp_range.tp_max.src;
266 }
267 return true;
268 }
269
__fl_lookup(struct fl_flow_mask * mask,struct fl_flow_key * mkey)270 static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask,
271 struct fl_flow_key *mkey)
272 {
273 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
274 mask->filter_ht_params);
275 }
276
fl_lookup_range(struct fl_flow_mask * mask,struct fl_flow_key * mkey,struct fl_flow_key * key)277 static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask,
278 struct fl_flow_key *mkey,
279 struct fl_flow_key *key)
280 {
281 struct cls_fl_filter *filter, *f;
282
283 list_for_each_entry_rcu(filter, &mask->filters, list) {
284 if (!fl_range_port_dst_cmp(filter, key, mkey))
285 continue;
286
287 if (!fl_range_port_src_cmp(filter, key, mkey))
288 continue;
289
290 f = __fl_lookup(mask, mkey);
291 if (f)
292 return f;
293 }
294 return NULL;
295 }
296
297 static noinline_for_stack
fl_mask_lookup(struct fl_flow_mask * mask,struct fl_flow_key * key)298 struct cls_fl_filter *fl_mask_lookup(struct fl_flow_mask *mask, struct fl_flow_key *key)
299 {
300 struct fl_flow_key mkey;
301
302 fl_set_masked_key(&mkey, key, mask);
303 if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE))
304 return fl_lookup_range(mask, &mkey, key);
305
306 return __fl_lookup(mask, &mkey);
307 }
308
309 static u16 fl_ct_info_to_flower_map[] = {
310 [IP_CT_ESTABLISHED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
311 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
312 [IP_CT_RELATED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
313 TCA_FLOWER_KEY_CT_FLAGS_RELATED,
314 [IP_CT_ESTABLISHED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
315 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED |
316 TCA_FLOWER_KEY_CT_FLAGS_REPLY,
317 [IP_CT_RELATED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
318 TCA_FLOWER_KEY_CT_FLAGS_RELATED |
319 TCA_FLOWER_KEY_CT_FLAGS_REPLY,
320 [IP_CT_NEW] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
321 TCA_FLOWER_KEY_CT_FLAGS_NEW,
322 };
323
fl_classify(struct sk_buff * skb,const struct tcf_proto * tp,struct tcf_result * res)324 TC_INDIRECT_SCOPE int fl_classify(struct sk_buff *skb,
325 const struct tcf_proto *tp,
326 struct tcf_result *res)
327 {
328 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
329 bool post_ct = tc_skb_cb(skb)->post_ct;
330 u16 zone = tc_skb_cb(skb)->zone;
331 struct fl_flow_key skb_key;
332 struct fl_flow_mask *mask;
333 struct cls_fl_filter *f;
334
335 list_for_each_entry_rcu(mask, &head->masks, list) {
336 flow_dissector_init_keys(&skb_key.control, &skb_key.basic);
337 fl_clear_masked_range(&skb_key, mask);
338
339 skb_flow_dissect_meta(skb, &mask->dissector, &skb_key);
340 /* skb_flow_dissect() does not set n_proto in case an unknown
341 * protocol, so do it rather here.
342 */
343 skb_key.basic.n_proto = skb_protocol(skb, false);
344 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
345 skb_flow_dissect_ct(skb, &mask->dissector, &skb_key,
346 fl_ct_info_to_flower_map,
347 ARRAY_SIZE(fl_ct_info_to_flower_map),
348 post_ct, zone);
349 skb_flow_dissect_hash(skb, &mask->dissector, &skb_key);
350 skb_flow_dissect(skb, &mask->dissector, &skb_key,
351 FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP);
352
353 f = fl_mask_lookup(mask, &skb_key);
354 if (f && !tc_skip_sw(f->flags)) {
355 *res = f->res;
356 return tcf_exts_exec(skb, &f->exts, res);
357 }
358 }
359 return -1;
360 }
361
fl_init(struct tcf_proto * tp)362 static int fl_init(struct tcf_proto *tp)
363 {
364 struct cls_fl_head *head;
365
366 head = kzalloc(sizeof(*head), GFP_KERNEL);
367 if (!head)
368 return -ENOBUFS;
369
370 spin_lock_init(&head->masks_lock);
371 INIT_LIST_HEAD_RCU(&head->masks);
372 INIT_LIST_HEAD(&head->hw_filters);
373 rcu_assign_pointer(tp->root, head);
374 idr_init(&head->handle_idr);
375
376 return rhashtable_init(&head->ht, &mask_ht_params);
377 }
378
fl_mask_free(struct fl_flow_mask * mask,bool mask_init_done)379 static void fl_mask_free(struct fl_flow_mask *mask, bool mask_init_done)
380 {
381 /* temporary masks don't have their filters list and ht initialized */
382 if (mask_init_done) {
383 WARN_ON(!list_empty(&mask->filters));
384 rhashtable_destroy(&mask->ht);
385 }
386 kfree(mask);
387 }
388
fl_mask_free_work(struct work_struct * work)389 static void fl_mask_free_work(struct work_struct *work)
390 {
391 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
392 struct fl_flow_mask, rwork);
393
394 fl_mask_free(mask, true);
395 }
396
fl_uninit_mask_free_work(struct work_struct * work)397 static void fl_uninit_mask_free_work(struct work_struct *work)
398 {
399 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
400 struct fl_flow_mask, rwork);
401
402 fl_mask_free(mask, false);
403 }
404
fl_mask_put(struct cls_fl_head * head,struct fl_flow_mask * mask)405 static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask)
406 {
407 if (!refcount_dec_and_test(&mask->refcnt))
408 return false;
409
410 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
411
412 spin_lock(&head->masks_lock);
413 list_del_rcu(&mask->list);
414 spin_unlock(&head->masks_lock);
415
416 tcf_queue_work(&mask->rwork, fl_mask_free_work);
417
418 return true;
419 }
420
fl_head_dereference(struct tcf_proto * tp)421 static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp)
422 {
423 /* Flower classifier only changes root pointer during init and destroy.
424 * Users must obtain reference to tcf_proto instance before calling its
425 * API, so tp->root pointer is protected from concurrent call to
426 * fl_destroy() by reference counting.
427 */
428 return rcu_dereference_raw(tp->root);
429 }
430
__fl_destroy_filter(struct cls_fl_filter * f)431 static void __fl_destroy_filter(struct cls_fl_filter *f)
432 {
433 if (f->needs_tc_skb_ext)
434 tc_skb_ext_tc_disable();
435 tcf_exts_destroy(&f->exts);
436 tcf_exts_put_net(&f->exts);
437 kfree(f);
438 }
439
fl_destroy_filter_work(struct work_struct * work)440 static void fl_destroy_filter_work(struct work_struct *work)
441 {
442 struct cls_fl_filter *f = container_of(to_rcu_work(work),
443 struct cls_fl_filter, rwork);
444
445 __fl_destroy_filter(f);
446 }
447
fl_hw_destroy_filter(struct tcf_proto * tp,struct cls_fl_filter * f,bool rtnl_held,struct netlink_ext_ack * extack)448 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
449 bool rtnl_held, struct netlink_ext_ack *extack)
450 {
451 struct tcf_block *block = tp->chain->block;
452 struct flow_cls_offload cls_flower = {};
453
454 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
455 cls_flower.command = FLOW_CLS_DESTROY;
456 cls_flower.cookie = (unsigned long) f;
457
458 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, false,
459 &f->flags, &f->in_hw_count, rtnl_held);
460
461 }
462
fl_hw_replace_filter(struct tcf_proto * tp,struct cls_fl_filter * f,bool rtnl_held,struct netlink_ext_ack * extack)463 static int fl_hw_replace_filter(struct tcf_proto *tp,
464 struct cls_fl_filter *f, bool rtnl_held,
465 struct netlink_ext_ack *extack)
466 {
467 struct tcf_block *block = tp->chain->block;
468 struct flow_cls_offload cls_flower = {};
469 bool skip_sw = tc_skip_sw(f->flags);
470 int err = 0;
471
472 cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts));
473 if (!cls_flower.rule)
474 return -ENOMEM;
475
476 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
477 cls_flower.command = FLOW_CLS_REPLACE;
478 cls_flower.cookie = (unsigned long) f;
479 cls_flower.rule->match.dissector = &f->mask->dissector;
480 cls_flower.rule->match.mask = &f->mask->key;
481 cls_flower.rule->match.key = &f->mkey;
482 cls_flower.classid = f->res.classid;
483
484 err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts,
485 cls_flower.common.extack);
486 if (err) {
487 kfree(cls_flower.rule);
488
489 return skip_sw ? err : 0;
490 }
491
492 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSFLOWER, &cls_flower,
493 skip_sw, &f->flags, &f->in_hw_count, rtnl_held);
494 tc_cleanup_offload_action(&cls_flower.rule->action);
495 kfree(cls_flower.rule);
496
497 if (err) {
498 fl_hw_destroy_filter(tp, f, rtnl_held, NULL);
499 return err;
500 }
501
502 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
503 return -EINVAL;
504
505 return 0;
506 }
507
fl_hw_update_stats(struct tcf_proto * tp,struct cls_fl_filter * f,bool rtnl_held)508 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f,
509 bool rtnl_held)
510 {
511 struct tcf_block *block = tp->chain->block;
512 struct flow_cls_offload cls_flower = {};
513
514 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
515 cls_flower.command = FLOW_CLS_STATS;
516 cls_flower.cookie = (unsigned long) f;
517 cls_flower.classid = f->res.classid;
518
519 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false,
520 rtnl_held);
521
522 tcf_exts_hw_stats_update(&f->exts, &cls_flower.stats, cls_flower.use_act_stats);
523 }
524
__fl_put(struct cls_fl_filter * f)525 static void __fl_put(struct cls_fl_filter *f)
526 {
527 if (!refcount_dec_and_test(&f->refcnt))
528 return;
529
530 if (tcf_exts_get_net(&f->exts))
531 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
532 else
533 __fl_destroy_filter(f);
534 }
535
__fl_get(struct cls_fl_head * head,u32 handle)536 static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle)
537 {
538 struct cls_fl_filter *f;
539
540 rcu_read_lock();
541 f = idr_find(&head->handle_idr, handle);
542 if (f && !refcount_inc_not_zero(&f->refcnt))
543 f = NULL;
544 rcu_read_unlock();
545
546 return f;
547 }
548
fl_get_exts(const struct tcf_proto * tp,u32 handle)549 static struct tcf_exts *fl_get_exts(const struct tcf_proto *tp, u32 handle)
550 {
551 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
552 struct cls_fl_filter *f;
553
554 f = idr_find(&head->handle_idr, handle);
555 return f ? &f->exts : NULL;
556 }
557
__fl_delete(struct tcf_proto * tp,struct cls_fl_filter * f,bool * last,bool rtnl_held,struct netlink_ext_ack * extack)558 static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
559 bool *last, bool rtnl_held,
560 struct netlink_ext_ack *extack)
561 {
562 struct cls_fl_head *head = fl_head_dereference(tp);
563
564 *last = false;
565
566 spin_lock(&tp->lock);
567 if (f->deleted) {
568 spin_unlock(&tp->lock);
569 return -ENOENT;
570 }
571
572 f->deleted = true;
573 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
574 f->mask->filter_ht_params);
575 idr_remove(&head->handle_idr, f->handle);
576 list_del_rcu(&f->list);
577 spin_unlock(&tp->lock);
578
579 *last = fl_mask_put(head, f->mask);
580 if (!tc_skip_hw(f->flags))
581 fl_hw_destroy_filter(tp, f, rtnl_held, extack);
582 tcf_unbind_filter(tp, &f->res);
583 __fl_put(f);
584
585 return 0;
586 }
587
fl_destroy_sleepable(struct work_struct * work)588 static void fl_destroy_sleepable(struct work_struct *work)
589 {
590 struct cls_fl_head *head = container_of(to_rcu_work(work),
591 struct cls_fl_head,
592 rwork);
593
594 rhashtable_destroy(&head->ht);
595 kfree(head);
596 module_put(THIS_MODULE);
597 }
598
fl_destroy(struct tcf_proto * tp,bool rtnl_held,struct netlink_ext_ack * extack)599 static void fl_destroy(struct tcf_proto *tp, bool rtnl_held,
600 struct netlink_ext_ack *extack)
601 {
602 struct cls_fl_head *head = fl_head_dereference(tp);
603 struct fl_flow_mask *mask, *next_mask;
604 struct cls_fl_filter *f, *next;
605 bool last;
606
607 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
608 list_for_each_entry_safe(f, next, &mask->filters, list) {
609 __fl_delete(tp, f, &last, rtnl_held, extack);
610 if (last)
611 break;
612 }
613 }
614 idr_destroy(&head->handle_idr);
615
616 __module_get(THIS_MODULE);
617 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
618 }
619
fl_put(struct tcf_proto * tp,void * arg)620 static void fl_put(struct tcf_proto *tp, void *arg)
621 {
622 struct cls_fl_filter *f = arg;
623
624 __fl_put(f);
625 }
626
fl_get(struct tcf_proto * tp,u32 handle)627 static void *fl_get(struct tcf_proto *tp, u32 handle)
628 {
629 struct cls_fl_head *head = fl_head_dereference(tp);
630
631 return __fl_get(head, handle);
632 }
633
634 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
635 [TCA_FLOWER_UNSPEC] = { .strict_start_type =
636 TCA_FLOWER_L2_MISS },
637 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
638 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
639 .len = IFNAMSIZ },
640 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
641 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
642 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
643 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
644 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
645 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
646 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
647 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
648 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
649 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
650 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
651 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
652 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
653 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
654 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
655 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
656 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
657 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
658 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
659 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
660 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
661 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
662 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
663 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
664 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
665 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
666 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
667 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
668 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
669 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
670 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
671 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
672 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
673 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
674 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
675 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
676 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
677 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
678 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
679 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
680 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
681 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
682 [TCA_FLOWER_KEY_FLAGS] = NLA_POLICY_MASK(NLA_BE32,
683 TCA_FLOWER_KEY_FLAGS_POLICY_MASK),
684 [TCA_FLOWER_KEY_FLAGS_MASK] = NLA_POLICY_MASK(NLA_BE32,
685 TCA_FLOWER_KEY_FLAGS_POLICY_MASK),
686 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
687 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
688 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
689 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
690 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
691 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
692 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
693 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
694 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
695 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
696 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
697 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
698 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
699 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
700 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
701 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
702 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
703 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
704 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
705 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
706 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
707 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
708 [TCA_FLOWER_KEY_MPLS_OPTS] = { .type = NLA_NESTED },
709 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
710 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
711 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
712 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
713 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
714 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
715 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
716 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
717 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
718 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
719 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
720 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
721 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
722 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
723 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
724 [TCA_FLOWER_KEY_CT_STATE] =
725 NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK),
726 [TCA_FLOWER_KEY_CT_STATE_MASK] =
727 NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK),
728 [TCA_FLOWER_KEY_CT_ZONE] = { .type = NLA_U16 },
729 [TCA_FLOWER_KEY_CT_ZONE_MASK] = { .type = NLA_U16 },
730 [TCA_FLOWER_KEY_CT_MARK] = { .type = NLA_U32 },
731 [TCA_FLOWER_KEY_CT_MARK_MASK] = { .type = NLA_U32 },
732 [TCA_FLOWER_KEY_CT_LABELS] = { .type = NLA_BINARY,
733 .len = 128 / BITS_PER_BYTE },
734 [TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NLA_BINARY,
735 .len = 128 / BITS_PER_BYTE },
736 [TCA_FLOWER_FLAGS] = { .type = NLA_U32 },
737 [TCA_FLOWER_KEY_HASH] = { .type = NLA_U32 },
738 [TCA_FLOWER_KEY_HASH_MASK] = { .type = NLA_U32 },
739 [TCA_FLOWER_KEY_NUM_OF_VLANS] = { .type = NLA_U8 },
740 [TCA_FLOWER_KEY_PPPOE_SID] = { .type = NLA_U16 },
741 [TCA_FLOWER_KEY_PPP_PROTO] = { .type = NLA_U16 },
742 [TCA_FLOWER_KEY_L2TPV3_SID] = { .type = NLA_U32 },
743 [TCA_FLOWER_KEY_SPI] = { .type = NLA_U32 },
744 [TCA_FLOWER_KEY_SPI_MASK] = { .type = NLA_U32 },
745 [TCA_FLOWER_L2_MISS] = NLA_POLICY_MAX(NLA_U8, 1),
746 [TCA_FLOWER_KEY_CFM] = { .type = NLA_NESTED },
747 [TCA_FLOWER_KEY_ENC_FLAGS] = NLA_POLICY_MASK(NLA_BE32,
748 TCA_FLOWER_KEY_ENC_FLAGS_POLICY_MASK),
749 [TCA_FLOWER_KEY_ENC_FLAGS_MASK] = NLA_POLICY_MASK(NLA_BE32,
750 TCA_FLOWER_KEY_ENC_FLAGS_POLICY_MASK),
751 };
752
753 static const struct nla_policy
754 enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
755 [TCA_FLOWER_KEY_ENC_OPTS_UNSPEC] = {
756 .strict_start_type = TCA_FLOWER_KEY_ENC_OPTS_VXLAN },
757 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
758 [TCA_FLOWER_KEY_ENC_OPTS_VXLAN] = { .type = NLA_NESTED },
759 [TCA_FLOWER_KEY_ENC_OPTS_ERSPAN] = { .type = NLA_NESTED },
760 [TCA_FLOWER_KEY_ENC_OPTS_GTP] = { .type = NLA_NESTED },
761 [TCA_FLOWER_KEY_ENC_OPTS_PFCP] = { .type = NLA_NESTED },
762 };
763
764 static const struct nla_policy
765 geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
766 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
767 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
768 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
769 .len = 128 },
770 };
771
772 static const struct nla_policy
773 vxlan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1] = {
774 [TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP] = { .type = NLA_U32 },
775 };
776
777 static const struct nla_policy
778 erspan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1] = {
779 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER] = { .type = NLA_U8 },
780 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
781 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
782 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
783 };
784
785 static const struct nla_policy
786 gtp_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GTP_MAX + 1] = {
787 [TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE] = { .type = NLA_U8 },
788 [TCA_FLOWER_KEY_ENC_OPT_GTP_QFI] = { .type = NLA_U8 },
789 };
790
791 static const struct nla_policy
792 pfcp_opt_policy[TCA_FLOWER_KEY_ENC_OPT_PFCP_MAX + 1] = {
793 [TCA_FLOWER_KEY_ENC_OPT_PFCP_TYPE] = { .type = NLA_U8 },
794 [TCA_FLOWER_KEY_ENC_OPT_PFCP_SEID] = { .type = NLA_U64 },
795 };
796
797 static const struct nla_policy
798 mpls_stack_entry_policy[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1] = {
799 [TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH] = { .type = NLA_U8 },
800 [TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL] = { .type = NLA_U8 },
801 [TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS] = { .type = NLA_U8 },
802 [TCA_FLOWER_KEY_MPLS_OPT_LSE_TC] = { .type = NLA_U8 },
803 [TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL] = { .type = NLA_U32 },
804 };
805
806 static const struct nla_policy
807 cfm_opt_policy[TCA_FLOWER_KEY_CFM_OPT_MAX + 1] = {
808 [TCA_FLOWER_KEY_CFM_MD_LEVEL] = NLA_POLICY_MAX(NLA_U8,
809 FLOW_DIS_CFM_MDL_MAX),
810 [TCA_FLOWER_KEY_CFM_OPCODE] = { .type = NLA_U8 },
811 };
812
fl_set_key_val(struct nlattr ** tb,void * val,int val_type,void * mask,int mask_type,int len)813 static void fl_set_key_val(struct nlattr **tb,
814 void *val, int val_type,
815 void *mask, int mask_type, int len)
816 {
817 if (!tb[val_type])
818 return;
819 nla_memcpy(val, tb[val_type], len);
820 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
821 memset(mask, 0xff, len);
822 else
823 nla_memcpy(mask, tb[mask_type], len);
824 }
825
fl_set_key_spi(struct nlattr ** tb,struct fl_flow_key * key,struct fl_flow_key * mask,struct netlink_ext_ack * extack)826 static int fl_set_key_spi(struct nlattr **tb, struct fl_flow_key *key,
827 struct fl_flow_key *mask,
828 struct netlink_ext_ack *extack)
829 {
830 if (key->basic.ip_proto != IPPROTO_ESP &&
831 key->basic.ip_proto != IPPROTO_AH) {
832 NL_SET_ERR_MSG(extack,
833 "Protocol must be either ESP or AH");
834 return -EINVAL;
835 }
836
837 fl_set_key_val(tb, &key->ipsec.spi,
838 TCA_FLOWER_KEY_SPI,
839 &mask->ipsec.spi, TCA_FLOWER_KEY_SPI_MASK,
840 sizeof(key->ipsec.spi));
841 return 0;
842 }
843
fl_set_key_port_range(struct nlattr ** tb,struct fl_flow_key * key,struct fl_flow_key * mask,struct netlink_ext_ack * extack)844 static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
845 struct fl_flow_key *mask,
846 struct netlink_ext_ack *extack)
847 {
848 fl_set_key_val(tb, &key->tp_range.tp_min.dst,
849 TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_range.tp_min.dst,
850 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.dst));
851 fl_set_key_val(tb, &key->tp_range.tp_max.dst,
852 TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_range.tp_max.dst,
853 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.dst));
854 fl_set_key_val(tb, &key->tp_range.tp_min.src,
855 TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_range.tp_min.src,
856 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.src));
857 fl_set_key_val(tb, &key->tp_range.tp_max.src,
858 TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_range.tp_max.src,
859 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.src));
860
861 if (mask->tp_range.tp_min.dst != mask->tp_range.tp_max.dst) {
862 NL_SET_ERR_MSG(extack,
863 "Both min and max destination ports must be specified");
864 return -EINVAL;
865 }
866 if (mask->tp_range.tp_min.src != mask->tp_range.tp_max.src) {
867 NL_SET_ERR_MSG(extack,
868 "Both min and max source ports must be specified");
869 return -EINVAL;
870 }
871 if (mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst &&
872 ntohs(key->tp_range.tp_max.dst) <=
873 ntohs(key->tp_range.tp_min.dst)) {
874 NL_SET_ERR_MSG_ATTR(extack,
875 tb[TCA_FLOWER_KEY_PORT_DST_MIN],
876 "Invalid destination port range (min must be strictly smaller than max)");
877 return -EINVAL;
878 }
879 if (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src &&
880 ntohs(key->tp_range.tp_max.src) <=
881 ntohs(key->tp_range.tp_min.src)) {
882 NL_SET_ERR_MSG_ATTR(extack,
883 tb[TCA_FLOWER_KEY_PORT_SRC_MIN],
884 "Invalid source port range (min must be strictly smaller than max)");
885 return -EINVAL;
886 }
887
888 return 0;
889 }
890
fl_set_key_mpls_lse(const struct nlattr * nla_lse,struct flow_dissector_key_mpls * key_val,struct flow_dissector_key_mpls * key_mask,struct netlink_ext_ack * extack)891 static int fl_set_key_mpls_lse(const struct nlattr *nla_lse,
892 struct flow_dissector_key_mpls *key_val,
893 struct flow_dissector_key_mpls *key_mask,
894 struct netlink_ext_ack *extack)
895 {
896 struct nlattr *tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1];
897 struct flow_dissector_mpls_lse *lse_mask;
898 struct flow_dissector_mpls_lse *lse_val;
899 u8 lse_index;
900 u8 depth;
901 int err;
902
903 err = nla_parse_nested(tb, TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX, nla_lse,
904 mpls_stack_entry_policy, extack);
905 if (err < 0)
906 return err;
907
908 if (!tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]) {
909 NL_SET_ERR_MSG(extack, "Missing MPLS option \"depth\"");
910 return -EINVAL;
911 }
912
913 depth = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]);
914
915 /* LSE depth starts at 1, for consistency with terminology used by
916 * RFC 3031 (section 3.9), where depth 0 refers to unlabeled packets.
917 */
918 if (depth < 1 || depth > FLOW_DIS_MPLS_MAX) {
919 NL_SET_ERR_MSG_ATTR(extack,
920 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH],
921 "Invalid MPLS depth");
922 return -EINVAL;
923 }
924 lse_index = depth - 1;
925
926 dissector_set_mpls_lse(key_val, lse_index);
927 dissector_set_mpls_lse(key_mask, lse_index);
928
929 lse_val = &key_val->ls[lse_index];
930 lse_mask = &key_mask->ls[lse_index];
931
932 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]) {
933 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]);
934 lse_mask->mpls_ttl = MPLS_TTL_MASK;
935 }
936 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]) {
937 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]);
938
939 if (bos & ~MPLS_BOS_MASK) {
940 NL_SET_ERR_MSG_ATTR(extack,
941 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS],
942 "Bottom Of Stack (BOS) must be 0 or 1");
943 return -EINVAL;
944 }
945 lse_val->mpls_bos = bos;
946 lse_mask->mpls_bos = MPLS_BOS_MASK;
947 }
948 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]) {
949 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]);
950
951 if (tc & ~MPLS_TC_MASK) {
952 NL_SET_ERR_MSG_ATTR(extack,
953 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC],
954 "Traffic Class (TC) must be between 0 and 7");
955 return -EINVAL;
956 }
957 lse_val->mpls_tc = tc;
958 lse_mask->mpls_tc = MPLS_TC_MASK;
959 }
960 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]) {
961 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]);
962
963 if (label & ~MPLS_LABEL_MASK) {
964 NL_SET_ERR_MSG_ATTR(extack,
965 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL],
966 "Label must be between 0 and 1048575");
967 return -EINVAL;
968 }
969 lse_val->mpls_label = label;
970 lse_mask->mpls_label = MPLS_LABEL_MASK;
971 }
972
973 return 0;
974 }
975
fl_set_key_mpls_opts(const struct nlattr * nla_mpls_opts,struct flow_dissector_key_mpls * key_val,struct flow_dissector_key_mpls * key_mask,struct netlink_ext_ack * extack)976 static int fl_set_key_mpls_opts(const struct nlattr *nla_mpls_opts,
977 struct flow_dissector_key_mpls *key_val,
978 struct flow_dissector_key_mpls *key_mask,
979 struct netlink_ext_ack *extack)
980 {
981 struct nlattr *nla_lse;
982 int rem;
983 int err;
984
985 if (!(nla_mpls_opts->nla_type & NLA_F_NESTED)) {
986 NL_SET_ERR_MSG_ATTR(extack, nla_mpls_opts,
987 "NLA_F_NESTED is missing");
988 return -EINVAL;
989 }
990
991 nla_for_each_nested(nla_lse, nla_mpls_opts, rem) {
992 if (nla_type(nla_lse) != TCA_FLOWER_KEY_MPLS_OPTS_LSE) {
993 NL_SET_ERR_MSG_ATTR(extack, nla_lse,
994 "Invalid MPLS option type");
995 return -EINVAL;
996 }
997
998 err = fl_set_key_mpls_lse(nla_lse, key_val, key_mask, extack);
999 if (err < 0)
1000 return err;
1001 }
1002 if (rem) {
1003 NL_SET_ERR_MSG(extack,
1004 "Bytes leftover after parsing MPLS options");
1005 return -EINVAL;
1006 }
1007
1008 return 0;
1009 }
1010
fl_set_key_mpls(struct nlattr ** tb,struct flow_dissector_key_mpls * key_val,struct flow_dissector_key_mpls * key_mask,struct netlink_ext_ack * extack)1011 static int fl_set_key_mpls(struct nlattr **tb,
1012 struct flow_dissector_key_mpls *key_val,
1013 struct flow_dissector_key_mpls *key_mask,
1014 struct netlink_ext_ack *extack)
1015 {
1016 struct flow_dissector_mpls_lse *lse_mask;
1017 struct flow_dissector_mpls_lse *lse_val;
1018
1019 if (tb[TCA_FLOWER_KEY_MPLS_OPTS]) {
1020 if (tb[TCA_FLOWER_KEY_MPLS_TTL] ||
1021 tb[TCA_FLOWER_KEY_MPLS_BOS] ||
1022 tb[TCA_FLOWER_KEY_MPLS_TC] ||
1023 tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
1024 NL_SET_ERR_MSG_ATTR(extack,
1025 tb[TCA_FLOWER_KEY_MPLS_OPTS],
1026 "MPLS label, Traffic Class, Bottom Of Stack and Time To Live must be encapsulated in the MPLS options attribute");
1027 return -EBADMSG;
1028 }
1029
1030 return fl_set_key_mpls_opts(tb[TCA_FLOWER_KEY_MPLS_OPTS],
1031 key_val, key_mask, extack);
1032 }
1033
1034 lse_val = &key_val->ls[0];
1035 lse_mask = &key_mask->ls[0];
1036
1037 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
1038 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
1039 lse_mask->mpls_ttl = MPLS_TTL_MASK;
1040 dissector_set_mpls_lse(key_val, 0);
1041 dissector_set_mpls_lse(key_mask, 0);
1042 }
1043 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
1044 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
1045
1046 if (bos & ~MPLS_BOS_MASK) {
1047 NL_SET_ERR_MSG_ATTR(extack,
1048 tb[TCA_FLOWER_KEY_MPLS_BOS],
1049 "Bottom Of Stack (BOS) must be 0 or 1");
1050 return -EINVAL;
1051 }
1052 lse_val->mpls_bos = bos;
1053 lse_mask->mpls_bos = MPLS_BOS_MASK;
1054 dissector_set_mpls_lse(key_val, 0);
1055 dissector_set_mpls_lse(key_mask, 0);
1056 }
1057 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
1058 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
1059
1060 if (tc & ~MPLS_TC_MASK) {
1061 NL_SET_ERR_MSG_ATTR(extack,
1062 tb[TCA_FLOWER_KEY_MPLS_TC],
1063 "Traffic Class (TC) must be between 0 and 7");
1064 return -EINVAL;
1065 }
1066 lse_val->mpls_tc = tc;
1067 lse_mask->mpls_tc = MPLS_TC_MASK;
1068 dissector_set_mpls_lse(key_val, 0);
1069 dissector_set_mpls_lse(key_mask, 0);
1070 }
1071 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
1072 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
1073
1074 if (label & ~MPLS_LABEL_MASK) {
1075 NL_SET_ERR_MSG_ATTR(extack,
1076 tb[TCA_FLOWER_KEY_MPLS_LABEL],
1077 "Label must be between 0 and 1048575");
1078 return -EINVAL;
1079 }
1080 lse_val->mpls_label = label;
1081 lse_mask->mpls_label = MPLS_LABEL_MASK;
1082 dissector_set_mpls_lse(key_val, 0);
1083 dissector_set_mpls_lse(key_mask, 0);
1084 }
1085 return 0;
1086 }
1087
fl_set_key_vlan(struct nlattr ** tb,__be16 ethertype,int vlan_id_key,int vlan_prio_key,int vlan_next_eth_type_key,struct flow_dissector_key_vlan * key_val,struct flow_dissector_key_vlan * key_mask)1088 static void fl_set_key_vlan(struct nlattr **tb,
1089 __be16 ethertype,
1090 int vlan_id_key, int vlan_prio_key,
1091 int vlan_next_eth_type_key,
1092 struct flow_dissector_key_vlan *key_val,
1093 struct flow_dissector_key_vlan *key_mask)
1094 {
1095 #define VLAN_PRIORITY_MASK 0x7
1096
1097 if (tb[vlan_id_key]) {
1098 key_val->vlan_id =
1099 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
1100 key_mask->vlan_id = VLAN_VID_MASK;
1101 }
1102 if (tb[vlan_prio_key]) {
1103 key_val->vlan_priority =
1104 nla_get_u8(tb[vlan_prio_key]) &
1105 VLAN_PRIORITY_MASK;
1106 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
1107 }
1108 if (ethertype) {
1109 key_val->vlan_tpid = ethertype;
1110 key_mask->vlan_tpid = cpu_to_be16(~0);
1111 }
1112 if (tb[vlan_next_eth_type_key]) {
1113 key_val->vlan_eth_type =
1114 nla_get_be16(tb[vlan_next_eth_type_key]);
1115 key_mask->vlan_eth_type = cpu_to_be16(~0);
1116 }
1117 }
1118
fl_set_key_pppoe(struct nlattr ** tb,struct flow_dissector_key_pppoe * key_val,struct flow_dissector_key_pppoe * key_mask,struct fl_flow_key * key,struct fl_flow_key * mask)1119 static void fl_set_key_pppoe(struct nlattr **tb,
1120 struct flow_dissector_key_pppoe *key_val,
1121 struct flow_dissector_key_pppoe *key_mask,
1122 struct fl_flow_key *key,
1123 struct fl_flow_key *mask)
1124 {
1125 /* key_val::type must be set to ETH_P_PPP_SES
1126 * because ETH_P_PPP_SES was stored in basic.n_proto
1127 * which might get overwritten by ppp_proto
1128 * or might be set to 0, the role of key_val::type
1129 * is similar to vlan_key::tpid
1130 */
1131 key_val->type = htons(ETH_P_PPP_SES);
1132 key_mask->type = cpu_to_be16(~0);
1133
1134 if (tb[TCA_FLOWER_KEY_PPPOE_SID]) {
1135 key_val->session_id =
1136 nla_get_be16(tb[TCA_FLOWER_KEY_PPPOE_SID]);
1137 key_mask->session_id = cpu_to_be16(~0);
1138 }
1139 if (tb[TCA_FLOWER_KEY_PPP_PROTO]) {
1140 key_val->ppp_proto =
1141 nla_get_be16(tb[TCA_FLOWER_KEY_PPP_PROTO]);
1142 key_mask->ppp_proto = cpu_to_be16(~0);
1143
1144 if (key_val->ppp_proto == htons(PPP_IP)) {
1145 key->basic.n_proto = htons(ETH_P_IP);
1146 mask->basic.n_proto = cpu_to_be16(~0);
1147 } else if (key_val->ppp_proto == htons(PPP_IPV6)) {
1148 key->basic.n_proto = htons(ETH_P_IPV6);
1149 mask->basic.n_proto = cpu_to_be16(~0);
1150 } else if (key_val->ppp_proto == htons(PPP_MPLS_UC)) {
1151 key->basic.n_proto = htons(ETH_P_MPLS_UC);
1152 mask->basic.n_proto = cpu_to_be16(~0);
1153 } else if (key_val->ppp_proto == htons(PPP_MPLS_MC)) {
1154 key->basic.n_proto = htons(ETH_P_MPLS_MC);
1155 mask->basic.n_proto = cpu_to_be16(~0);
1156 }
1157 } else {
1158 key->basic.n_proto = 0;
1159 mask->basic.n_proto = cpu_to_be16(0);
1160 }
1161 }
1162
fl_set_key_flag(u32 flower_key,u32 flower_mask,u32 * dissector_key,u32 * dissector_mask,u32 flower_flag_bit,u32 dissector_flag_bit)1163 static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
1164 u32 *dissector_key, u32 *dissector_mask,
1165 u32 flower_flag_bit, u32 dissector_flag_bit)
1166 {
1167 if (flower_mask & flower_flag_bit) {
1168 *dissector_mask |= dissector_flag_bit;
1169 if (flower_key & flower_flag_bit)
1170 *dissector_key |= dissector_flag_bit;
1171 }
1172 }
1173
fl_set_key_flags(struct nlattr * tca_opts,struct nlattr ** tb,bool encap,u32 * flags_key,u32 * flags_mask,struct netlink_ext_ack * extack)1174 static int fl_set_key_flags(struct nlattr *tca_opts, struct nlattr **tb,
1175 bool encap, u32 *flags_key, u32 *flags_mask,
1176 struct netlink_ext_ack *extack)
1177 {
1178 int fl_key, fl_mask;
1179 u32 key, mask;
1180
1181 if (encap) {
1182 fl_key = TCA_FLOWER_KEY_ENC_FLAGS;
1183 fl_mask = TCA_FLOWER_KEY_ENC_FLAGS_MASK;
1184 } else {
1185 fl_key = TCA_FLOWER_KEY_FLAGS;
1186 fl_mask = TCA_FLOWER_KEY_FLAGS_MASK;
1187 }
1188
1189 /* mask is mandatory for flags */
1190 if (NL_REQ_ATTR_CHECK(extack, tca_opts, tb, fl_mask)) {
1191 NL_SET_ERR_MSG(extack, "Missing flags mask");
1192 return -EINVAL;
1193 }
1194
1195 key = be32_to_cpu(nla_get_be32(tb[fl_key]));
1196 mask = be32_to_cpu(nla_get_be32(tb[fl_mask]));
1197
1198 *flags_key = 0;
1199 *flags_mask = 0;
1200
1201 fl_set_key_flag(key, mask, flags_key, flags_mask,
1202 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1203 fl_set_key_flag(key, mask, flags_key, flags_mask,
1204 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1205 FLOW_DIS_FIRST_FRAG);
1206
1207 fl_set_key_flag(key, mask, flags_key, flags_mask,
1208 TCA_FLOWER_KEY_FLAGS_TUNNEL_CSUM,
1209 FLOW_DIS_F_TUNNEL_CSUM);
1210
1211 fl_set_key_flag(key, mask, flags_key, flags_mask,
1212 TCA_FLOWER_KEY_FLAGS_TUNNEL_DONT_FRAGMENT,
1213 FLOW_DIS_F_TUNNEL_DONT_FRAGMENT);
1214
1215 fl_set_key_flag(key, mask, flags_key, flags_mask,
1216 TCA_FLOWER_KEY_FLAGS_TUNNEL_OAM, FLOW_DIS_F_TUNNEL_OAM);
1217
1218 fl_set_key_flag(key, mask, flags_key, flags_mask,
1219 TCA_FLOWER_KEY_FLAGS_TUNNEL_CRIT_OPT,
1220 FLOW_DIS_F_TUNNEL_CRIT_OPT);
1221
1222 return 0;
1223 }
1224
fl_set_key_ip(struct nlattr ** tb,bool encap,struct flow_dissector_key_ip * key,struct flow_dissector_key_ip * mask)1225 static void fl_set_key_ip(struct nlattr **tb, bool encap,
1226 struct flow_dissector_key_ip *key,
1227 struct flow_dissector_key_ip *mask)
1228 {
1229 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1230 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1231 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1232 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
1233
1234 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
1235 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
1236 }
1237
fl_set_geneve_opt(const struct nlattr * nla,struct fl_flow_key * key,int depth,int option_len,struct netlink_ext_ack * extack)1238 static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
1239 int depth, int option_len,
1240 struct netlink_ext_ack *extack)
1241 {
1242 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
1243 struct nlattr *class = NULL, *type = NULL, *data = NULL;
1244 struct geneve_opt *opt;
1245 int err, data_len = 0;
1246
1247 if (option_len > sizeof(struct geneve_opt))
1248 data_len = option_len - sizeof(struct geneve_opt);
1249
1250 if (key->enc_opts.len > FLOW_DIS_TUN_OPTS_MAX - 4)
1251 return -ERANGE;
1252
1253 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
1254 memset(opt, 0xff, option_len);
1255 opt->length = data_len / 4;
1256 opt->r1 = 0;
1257 opt->r2 = 0;
1258 opt->r3 = 0;
1259
1260 /* If no mask has been prodived we assume an exact match. */
1261 if (!depth)
1262 return sizeof(struct geneve_opt) + data_len;
1263
1264 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
1265 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
1266 return -EINVAL;
1267 }
1268
1269 err = nla_parse_nested_deprecated(tb,
1270 TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
1271 nla, geneve_opt_policy, extack);
1272 if (err < 0)
1273 return err;
1274
1275 /* We are not allowed to omit any of CLASS, TYPE or DATA
1276 * fields from the key.
1277 */
1278 if (!option_len &&
1279 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
1280 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
1281 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
1282 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
1283 return -EINVAL;
1284 }
1285
1286 /* Omitting any of CLASS, TYPE or DATA fields is allowed
1287 * for the mask.
1288 */
1289 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
1290 int new_len = key->enc_opts.len;
1291
1292 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
1293 data_len = nla_len(data);
1294 if (data_len < 4) {
1295 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
1296 return -ERANGE;
1297 }
1298 if (data_len % 4) {
1299 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
1300 return -ERANGE;
1301 }
1302
1303 new_len += sizeof(struct geneve_opt) + data_len;
1304 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
1305 if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
1306 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
1307 return -ERANGE;
1308 }
1309 opt->length = data_len / 4;
1310 memcpy(opt->opt_data, nla_data(data), data_len);
1311 }
1312
1313 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
1314 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
1315 opt->opt_class = nla_get_be16(class);
1316 }
1317
1318 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
1319 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
1320 opt->type = nla_get_u8(type);
1321 }
1322
1323 return sizeof(struct geneve_opt) + data_len;
1324 }
1325
fl_set_vxlan_opt(const struct nlattr * nla,struct fl_flow_key * key,int depth,int option_len,struct netlink_ext_ack * extack)1326 static int fl_set_vxlan_opt(const struct nlattr *nla, struct fl_flow_key *key,
1327 int depth, int option_len,
1328 struct netlink_ext_ack *extack)
1329 {
1330 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1];
1331 struct vxlan_metadata *md;
1332 int err;
1333
1334 md = (struct vxlan_metadata *)&key->enc_opts.data[key->enc_opts.len];
1335 memset(md, 0xff, sizeof(*md));
1336
1337 if (!depth)
1338 return sizeof(*md);
1339
1340 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_VXLAN) {
1341 NL_SET_ERR_MSG(extack, "Non-vxlan option type for mask");
1342 return -EINVAL;
1343 }
1344
1345 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, nla,
1346 vxlan_opt_policy, extack);
1347 if (err < 0)
1348 return err;
1349
1350 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
1351 NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp");
1352 return -EINVAL;
1353 }
1354
1355 if (tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
1356 md->gbp = nla_get_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]);
1357 md->gbp &= VXLAN_GBP_MASK;
1358 }
1359
1360 return sizeof(*md);
1361 }
1362
fl_set_erspan_opt(const struct nlattr * nla,struct fl_flow_key * key,int depth,int option_len,struct netlink_ext_ack * extack)1363 static int fl_set_erspan_opt(const struct nlattr *nla, struct fl_flow_key *key,
1364 int depth, int option_len,
1365 struct netlink_ext_ack *extack)
1366 {
1367 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1];
1368 struct erspan_metadata *md;
1369 int err;
1370
1371 md = (struct erspan_metadata *)&key->enc_opts.data[key->enc_opts.len];
1372 md->version = 1;
1373
1374 if (!depth)
1375 return sizeof(*md);
1376
1377 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_ERSPAN) {
1378 NL_SET_ERR_MSG(extack, "Non-erspan option type for mask");
1379 return -EINVAL;
1380 }
1381
1382 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX, nla,
1383 erspan_opt_policy, extack);
1384 if (err < 0)
1385 return err;
1386
1387 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) {
1388 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver");
1389 return -EINVAL;
1390 }
1391
1392 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER])
1393 md->version = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]);
1394
1395 if (md->version == 1) {
1396 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
1397 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index");
1398 return -EINVAL;
1399 }
1400 memset(&md->u.index, 0xff, sizeof(md->u.index));
1401 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
1402 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX];
1403 md->u.index = nla_get_be32(nla);
1404 }
1405 } else if (md->version == 2) {
1406 if (!option_len && (!tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] ||
1407 !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID])) {
1408 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid");
1409 return -EINVAL;
1410 }
1411 md->u.md2.dir = 1;
1412 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]) {
1413 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR];
1414 md->u.md2.dir = nla_get_u8(nla);
1415 }
1416 set_hwid(&md->u.md2, 0xff);
1417 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]) {
1418 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID];
1419 set_hwid(&md->u.md2, nla_get_u8(nla));
1420 }
1421 } else {
1422 NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect");
1423 return -EINVAL;
1424 }
1425
1426 return sizeof(*md);
1427 }
1428
fl_set_gtp_opt(const struct nlattr * nla,struct fl_flow_key * key,int depth,int option_len,struct netlink_ext_ack * extack)1429 static int fl_set_gtp_opt(const struct nlattr *nla, struct fl_flow_key *key,
1430 int depth, int option_len,
1431 struct netlink_ext_ack *extack)
1432 {
1433 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GTP_MAX + 1];
1434 struct gtp_pdu_session_info *sinfo;
1435 u8 len = key->enc_opts.len;
1436 int err;
1437
1438 sinfo = (struct gtp_pdu_session_info *)&key->enc_opts.data[len];
1439 memset(sinfo, 0xff, option_len);
1440
1441 if (!depth)
1442 return sizeof(*sinfo);
1443
1444 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GTP) {
1445 NL_SET_ERR_MSG_MOD(extack, "Non-gtp option type for mask");
1446 return -EINVAL;
1447 }
1448
1449 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GTP_MAX, nla,
1450 gtp_opt_policy, extack);
1451 if (err < 0)
1452 return err;
1453
1454 if (!option_len &&
1455 (!tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE] ||
1456 !tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI])) {
1457 NL_SET_ERR_MSG_MOD(extack,
1458 "Missing tunnel key gtp option pdu type or qfi");
1459 return -EINVAL;
1460 }
1461
1462 if (tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE])
1463 sinfo->pdu_type =
1464 nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE]);
1465
1466 if (tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI])
1467 sinfo->qfi = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_GTP_QFI]);
1468
1469 return sizeof(*sinfo);
1470 }
1471
fl_set_pfcp_opt(const struct nlattr * nla,struct fl_flow_key * key,int depth,int option_len,struct netlink_ext_ack * extack)1472 static int fl_set_pfcp_opt(const struct nlattr *nla, struct fl_flow_key *key,
1473 int depth, int option_len,
1474 struct netlink_ext_ack *extack)
1475 {
1476 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_PFCP_MAX + 1];
1477 struct pfcp_metadata *md;
1478 int err;
1479
1480 md = (struct pfcp_metadata *)&key->enc_opts.data[key->enc_opts.len];
1481 memset(md, 0xff, sizeof(*md));
1482
1483 if (!depth)
1484 return sizeof(*md);
1485
1486 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_PFCP) {
1487 NL_SET_ERR_MSG_MOD(extack, "Non-pfcp option type for mask");
1488 return -EINVAL;
1489 }
1490
1491 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_PFCP_MAX, nla,
1492 pfcp_opt_policy, extack);
1493 if (err < 0)
1494 return err;
1495
1496 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_PFCP_TYPE]) {
1497 NL_SET_ERR_MSG_MOD(extack, "Missing tunnel key pfcp option type");
1498 return -EINVAL;
1499 }
1500
1501 if (tb[TCA_FLOWER_KEY_ENC_OPT_PFCP_TYPE])
1502 md->type = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_PFCP_TYPE]);
1503
1504 if (tb[TCA_FLOWER_KEY_ENC_OPT_PFCP_SEID])
1505 md->seid = nla_get_be64(tb[TCA_FLOWER_KEY_ENC_OPT_PFCP_SEID]);
1506
1507 return sizeof(*md);
1508 }
1509
fl_set_enc_opt(struct nlattr ** tb,struct fl_flow_key * key,struct fl_flow_key * mask,struct netlink_ext_ack * extack)1510 static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
1511 struct fl_flow_key *mask,
1512 struct netlink_ext_ack *extack)
1513 {
1514 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
1515 int err, option_len, key_depth, msk_depth = 0;
1516
1517 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS],
1518 TCA_FLOWER_KEY_ENC_OPTS_MAX,
1519 enc_opts_policy, extack);
1520 if (err)
1521 return err;
1522
1523 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
1524
1525 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
1526 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
1527 TCA_FLOWER_KEY_ENC_OPTS_MAX,
1528 enc_opts_policy, extack);
1529 if (err)
1530 return err;
1531
1532 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
1533 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
1534 if (!nla_ok(nla_opt_msk, msk_depth)) {
1535 NL_SET_ERR_MSG(extack, "Invalid nested attribute for masks");
1536 return -EINVAL;
1537 }
1538 }
1539
1540 nla_for_each_attr(nla_opt_key, nla_enc_key,
1541 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
1542 switch (nla_type(nla_opt_key)) {
1543 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
1544 if (key->enc_opts.dst_opt_type &&
1545 key->enc_opts.dst_opt_type !=
1546 IP_TUNNEL_GENEVE_OPT_BIT) {
1547 NL_SET_ERR_MSG(extack, "Duplicate type for geneve options");
1548 return -EINVAL;
1549 }
1550 option_len = 0;
1551 key->enc_opts.dst_opt_type = IP_TUNNEL_GENEVE_OPT_BIT;
1552 option_len = fl_set_geneve_opt(nla_opt_key, key,
1553 key_depth, option_len,
1554 extack);
1555 if (option_len < 0)
1556 return option_len;
1557
1558 key->enc_opts.len += option_len;
1559 /* At the same time we need to parse through the mask
1560 * in order to verify exact and mask attribute lengths.
1561 */
1562 mask->enc_opts.dst_opt_type = IP_TUNNEL_GENEVE_OPT_BIT;
1563 option_len = fl_set_geneve_opt(nla_opt_msk, mask,
1564 msk_depth, option_len,
1565 extack);
1566 if (option_len < 0)
1567 return option_len;
1568
1569 mask->enc_opts.len += option_len;
1570 if (key->enc_opts.len != mask->enc_opts.len) {
1571 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1572 return -EINVAL;
1573 }
1574 break;
1575 case TCA_FLOWER_KEY_ENC_OPTS_VXLAN:
1576 if (key->enc_opts.dst_opt_type) {
1577 NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options");
1578 return -EINVAL;
1579 }
1580 option_len = 0;
1581 key->enc_opts.dst_opt_type = IP_TUNNEL_VXLAN_OPT_BIT;
1582 option_len = fl_set_vxlan_opt(nla_opt_key, key,
1583 key_depth, option_len,
1584 extack);
1585 if (option_len < 0)
1586 return option_len;
1587
1588 key->enc_opts.len += option_len;
1589 /* At the same time we need to parse through the mask
1590 * in order to verify exact and mask attribute lengths.
1591 */
1592 mask->enc_opts.dst_opt_type = IP_TUNNEL_VXLAN_OPT_BIT;
1593 option_len = fl_set_vxlan_opt(nla_opt_msk, mask,
1594 msk_depth, option_len,
1595 extack);
1596 if (option_len < 0)
1597 return option_len;
1598
1599 mask->enc_opts.len += option_len;
1600 if (key->enc_opts.len != mask->enc_opts.len) {
1601 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1602 return -EINVAL;
1603 }
1604 break;
1605 case TCA_FLOWER_KEY_ENC_OPTS_ERSPAN:
1606 if (key->enc_opts.dst_opt_type) {
1607 NL_SET_ERR_MSG(extack, "Duplicate type for erspan options");
1608 return -EINVAL;
1609 }
1610 option_len = 0;
1611 key->enc_opts.dst_opt_type = IP_TUNNEL_ERSPAN_OPT_BIT;
1612 option_len = fl_set_erspan_opt(nla_opt_key, key,
1613 key_depth, option_len,
1614 extack);
1615 if (option_len < 0)
1616 return option_len;
1617
1618 key->enc_opts.len += option_len;
1619 /* At the same time we need to parse through the mask
1620 * in order to verify exact and mask attribute lengths.
1621 */
1622 mask->enc_opts.dst_opt_type = IP_TUNNEL_ERSPAN_OPT_BIT;
1623 option_len = fl_set_erspan_opt(nla_opt_msk, mask,
1624 msk_depth, option_len,
1625 extack);
1626 if (option_len < 0)
1627 return option_len;
1628
1629 mask->enc_opts.len += option_len;
1630 if (key->enc_opts.len != mask->enc_opts.len) {
1631 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1632 return -EINVAL;
1633 }
1634 break;
1635 case TCA_FLOWER_KEY_ENC_OPTS_GTP:
1636 if (key->enc_opts.dst_opt_type) {
1637 NL_SET_ERR_MSG_MOD(extack,
1638 "Duplicate type for gtp options");
1639 return -EINVAL;
1640 }
1641 option_len = 0;
1642 key->enc_opts.dst_opt_type = IP_TUNNEL_GTP_OPT_BIT;
1643 option_len = fl_set_gtp_opt(nla_opt_key, key,
1644 key_depth, option_len,
1645 extack);
1646 if (option_len < 0)
1647 return option_len;
1648
1649 key->enc_opts.len += option_len;
1650 /* At the same time we need to parse through the mask
1651 * in order to verify exact and mask attribute lengths.
1652 */
1653 mask->enc_opts.dst_opt_type = IP_TUNNEL_GTP_OPT_BIT;
1654 option_len = fl_set_gtp_opt(nla_opt_msk, mask,
1655 msk_depth, option_len,
1656 extack);
1657 if (option_len < 0)
1658 return option_len;
1659
1660 mask->enc_opts.len += option_len;
1661 if (key->enc_opts.len != mask->enc_opts.len) {
1662 NL_SET_ERR_MSG_MOD(extack,
1663 "Key and mask miss aligned");
1664 return -EINVAL;
1665 }
1666 break;
1667 case TCA_FLOWER_KEY_ENC_OPTS_PFCP:
1668 if (key->enc_opts.dst_opt_type) {
1669 NL_SET_ERR_MSG_MOD(extack, "Duplicate type for pfcp options");
1670 return -EINVAL;
1671 }
1672 option_len = 0;
1673 key->enc_opts.dst_opt_type = IP_TUNNEL_PFCP_OPT_BIT;
1674 option_len = fl_set_pfcp_opt(nla_opt_key, key,
1675 key_depth, option_len,
1676 extack);
1677 if (option_len < 0)
1678 return option_len;
1679
1680 key->enc_opts.len += option_len;
1681 /* At the same time we need to parse through the mask
1682 * in order to verify exact and mask attribute lengths.
1683 */
1684 mask->enc_opts.dst_opt_type = IP_TUNNEL_PFCP_OPT_BIT;
1685 option_len = fl_set_pfcp_opt(nla_opt_msk, mask,
1686 msk_depth, option_len,
1687 extack);
1688 if (option_len < 0)
1689 return option_len;
1690
1691 mask->enc_opts.len += option_len;
1692 if (key->enc_opts.len != mask->enc_opts.len) {
1693 NL_SET_ERR_MSG_MOD(extack, "Key and mask miss aligned");
1694 return -EINVAL;
1695 }
1696 break;
1697 default:
1698 NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
1699 return -EINVAL;
1700 }
1701
1702 if (!msk_depth)
1703 continue;
1704
1705 if (!nla_ok(nla_opt_msk, msk_depth)) {
1706 NL_SET_ERR_MSG(extack, "A mask attribute is invalid");
1707 return -EINVAL;
1708 }
1709 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
1710 }
1711
1712 return 0;
1713 }
1714
fl_validate_ct_state(u16 state,struct nlattr * tb,struct netlink_ext_ack * extack)1715 static int fl_validate_ct_state(u16 state, struct nlattr *tb,
1716 struct netlink_ext_ack *extack)
1717 {
1718 if (state && !(state & TCA_FLOWER_KEY_CT_FLAGS_TRACKED)) {
1719 NL_SET_ERR_MSG_ATTR(extack, tb,
1720 "no trk, so no other flag can be set");
1721 return -EINVAL;
1722 }
1723
1724 if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW &&
1725 state & TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED) {
1726 NL_SET_ERR_MSG_ATTR(extack, tb,
1727 "new and est are mutually exclusive");
1728 return -EINVAL;
1729 }
1730
1731 if (state & TCA_FLOWER_KEY_CT_FLAGS_INVALID &&
1732 state & ~(TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
1733 TCA_FLOWER_KEY_CT_FLAGS_INVALID)) {
1734 NL_SET_ERR_MSG_ATTR(extack, tb,
1735 "when inv is set, only trk may be set");
1736 return -EINVAL;
1737 }
1738
1739 if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW &&
1740 state & TCA_FLOWER_KEY_CT_FLAGS_REPLY) {
1741 NL_SET_ERR_MSG_ATTR(extack, tb,
1742 "new and rpl are mutually exclusive");
1743 return -EINVAL;
1744 }
1745
1746 return 0;
1747 }
1748
fl_set_key_ct(struct nlattr ** tb,struct flow_dissector_key_ct * key,struct flow_dissector_key_ct * mask,struct netlink_ext_ack * extack)1749 static int fl_set_key_ct(struct nlattr **tb,
1750 struct flow_dissector_key_ct *key,
1751 struct flow_dissector_key_ct *mask,
1752 struct netlink_ext_ack *extack)
1753 {
1754 if (tb[TCA_FLOWER_KEY_CT_STATE]) {
1755 int err;
1756
1757 if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) {
1758 NL_SET_ERR_MSG(extack, "Conntrack isn't enabled");
1759 return -EOPNOTSUPP;
1760 }
1761 fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
1762 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
1763 sizeof(key->ct_state));
1764
1765 err = fl_validate_ct_state(key->ct_state & mask->ct_state,
1766 tb[TCA_FLOWER_KEY_CT_STATE_MASK],
1767 extack);
1768 if (err)
1769 return err;
1770
1771 }
1772 if (tb[TCA_FLOWER_KEY_CT_ZONE]) {
1773 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1774 NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled");
1775 return -EOPNOTSUPP;
1776 }
1777 fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
1778 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
1779 sizeof(key->ct_zone));
1780 }
1781 if (tb[TCA_FLOWER_KEY_CT_MARK]) {
1782 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1783 NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled");
1784 return -EOPNOTSUPP;
1785 }
1786 fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
1787 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
1788 sizeof(key->ct_mark));
1789 }
1790 if (tb[TCA_FLOWER_KEY_CT_LABELS]) {
1791 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1792 NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled");
1793 return -EOPNOTSUPP;
1794 }
1795 fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
1796 mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
1797 sizeof(key->ct_labels));
1798 }
1799
1800 return 0;
1801 }
1802
is_vlan_key(struct nlattr * tb,__be16 * ethertype,struct fl_flow_key * key,struct fl_flow_key * mask,int vthresh)1803 static bool is_vlan_key(struct nlattr *tb, __be16 *ethertype,
1804 struct fl_flow_key *key, struct fl_flow_key *mask,
1805 int vthresh)
1806 {
1807 const bool good_num_of_vlans = key->num_of_vlans.num_of_vlans > vthresh;
1808
1809 if (!tb) {
1810 *ethertype = 0;
1811 return good_num_of_vlans;
1812 }
1813
1814 *ethertype = nla_get_be16(tb);
1815 if (good_num_of_vlans || eth_type_vlan(*ethertype))
1816 return true;
1817
1818 key->basic.n_proto = *ethertype;
1819 mask->basic.n_proto = cpu_to_be16(~0);
1820 return false;
1821 }
1822
fl_set_key_cfm_md_level(struct nlattr ** tb,struct fl_flow_key * key,struct fl_flow_key * mask,struct netlink_ext_ack * extack)1823 static void fl_set_key_cfm_md_level(struct nlattr **tb,
1824 struct fl_flow_key *key,
1825 struct fl_flow_key *mask,
1826 struct netlink_ext_ack *extack)
1827 {
1828 u8 level;
1829
1830 if (!tb[TCA_FLOWER_KEY_CFM_MD_LEVEL])
1831 return;
1832
1833 level = nla_get_u8(tb[TCA_FLOWER_KEY_CFM_MD_LEVEL]);
1834 key->cfm.mdl_ver = FIELD_PREP(FLOW_DIS_CFM_MDL_MASK, level);
1835 mask->cfm.mdl_ver = FLOW_DIS_CFM_MDL_MASK;
1836 }
1837
fl_set_key_cfm_opcode(struct nlattr ** tb,struct fl_flow_key * key,struct fl_flow_key * mask,struct netlink_ext_ack * extack)1838 static void fl_set_key_cfm_opcode(struct nlattr **tb,
1839 struct fl_flow_key *key,
1840 struct fl_flow_key *mask,
1841 struct netlink_ext_ack *extack)
1842 {
1843 fl_set_key_val(tb, &key->cfm.opcode, TCA_FLOWER_KEY_CFM_OPCODE,
1844 &mask->cfm.opcode, TCA_FLOWER_UNSPEC,
1845 sizeof(key->cfm.opcode));
1846 }
1847
fl_set_key_cfm(struct nlattr ** tb,struct fl_flow_key * key,struct fl_flow_key * mask,struct netlink_ext_ack * extack)1848 static int fl_set_key_cfm(struct nlattr **tb,
1849 struct fl_flow_key *key,
1850 struct fl_flow_key *mask,
1851 struct netlink_ext_ack *extack)
1852 {
1853 struct nlattr *nla_cfm_opt[TCA_FLOWER_KEY_CFM_OPT_MAX + 1];
1854 int err;
1855
1856 if (!tb[TCA_FLOWER_KEY_CFM])
1857 return 0;
1858
1859 err = nla_parse_nested(nla_cfm_opt, TCA_FLOWER_KEY_CFM_OPT_MAX,
1860 tb[TCA_FLOWER_KEY_CFM], cfm_opt_policy, extack);
1861 if (err < 0)
1862 return err;
1863
1864 fl_set_key_cfm_opcode(nla_cfm_opt, key, mask, extack);
1865 fl_set_key_cfm_md_level(nla_cfm_opt, key, mask, extack);
1866
1867 return 0;
1868 }
1869
fl_set_key(struct net * net,struct nlattr * tca_opts,struct nlattr ** tb,struct fl_flow_key * key,struct fl_flow_key * mask,struct netlink_ext_ack * extack)1870 static int fl_set_key(struct net *net, struct nlattr *tca_opts,
1871 struct nlattr **tb, struct fl_flow_key *key,
1872 struct fl_flow_key *mask, struct netlink_ext_ack *extack)
1873 {
1874 __be16 ethertype;
1875 int ret = 0;
1876
1877 if (tb[TCA_FLOWER_INDEV]) {
1878 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
1879 if (err < 0)
1880 return err;
1881 key->meta.ingress_ifindex = err;
1882 mask->meta.ingress_ifindex = 0xffffffff;
1883 }
1884
1885 fl_set_key_val(tb, &key->meta.l2_miss, TCA_FLOWER_L2_MISS,
1886 &mask->meta.l2_miss, TCA_FLOWER_UNSPEC,
1887 sizeof(key->meta.l2_miss));
1888
1889 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1890 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1891 sizeof(key->eth.dst));
1892 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1893 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1894 sizeof(key->eth.src));
1895 fl_set_key_val(tb, &key->num_of_vlans,
1896 TCA_FLOWER_KEY_NUM_OF_VLANS,
1897 &mask->num_of_vlans,
1898 TCA_FLOWER_UNSPEC,
1899 sizeof(key->num_of_vlans));
1900
1901 if (is_vlan_key(tb[TCA_FLOWER_KEY_ETH_TYPE], ðertype, key, mask, 0)) {
1902 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
1903 TCA_FLOWER_KEY_VLAN_PRIO,
1904 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1905 &key->vlan, &mask->vlan);
1906
1907 if (is_vlan_key(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE],
1908 ðertype, key, mask, 1)) {
1909 fl_set_key_vlan(tb, ethertype,
1910 TCA_FLOWER_KEY_CVLAN_ID,
1911 TCA_FLOWER_KEY_CVLAN_PRIO,
1912 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1913 &key->cvlan, &mask->cvlan);
1914 fl_set_key_val(tb, &key->basic.n_proto,
1915 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1916 &mask->basic.n_proto,
1917 TCA_FLOWER_UNSPEC,
1918 sizeof(key->basic.n_proto));
1919 }
1920 }
1921
1922 if (key->basic.n_proto == htons(ETH_P_PPP_SES))
1923 fl_set_key_pppoe(tb, &key->pppoe, &mask->pppoe, key, mask);
1924
1925 if (key->basic.n_proto == htons(ETH_P_IP) ||
1926 key->basic.n_proto == htons(ETH_P_IPV6)) {
1927 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1928 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1929 sizeof(key->basic.ip_proto));
1930 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
1931 }
1932
1933 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
1934 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1935 mask->control.addr_type = ~0;
1936 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1937 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1938 sizeof(key->ipv4.src));
1939 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1940 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1941 sizeof(key->ipv4.dst));
1942 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
1943 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1944 mask->control.addr_type = ~0;
1945 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1946 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1947 sizeof(key->ipv6.src));
1948 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1949 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1950 sizeof(key->ipv6.dst));
1951 }
1952
1953 if (key->basic.ip_proto == IPPROTO_TCP) {
1954 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1955 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1956 sizeof(key->tp.src));
1957 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1958 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1959 sizeof(key->tp.dst));
1960 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1961 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1962 sizeof(key->tcp.flags));
1963 } else if (key->basic.ip_proto == IPPROTO_UDP) {
1964 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1965 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1966 sizeof(key->tp.src));
1967 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1968 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1969 sizeof(key->tp.dst));
1970 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
1971 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1972 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1973 sizeof(key->tp.src));
1974 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1975 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1976 sizeof(key->tp.dst));
1977 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
1978 key->basic.ip_proto == IPPROTO_ICMP) {
1979 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
1980 &mask->icmp.type,
1981 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1982 sizeof(key->icmp.type));
1983 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
1984 &mask->icmp.code,
1985 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1986 sizeof(key->icmp.code));
1987 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1988 key->basic.ip_proto == IPPROTO_ICMPV6) {
1989 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
1990 &mask->icmp.type,
1991 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1992 sizeof(key->icmp.type));
1993 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
1994 &mask->icmp.code,
1995 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1996 sizeof(key->icmp.code));
1997 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
1998 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
1999 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls, extack);
2000 if (ret)
2001 return ret;
2002 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
2003 key->basic.n_proto == htons(ETH_P_RARP)) {
2004 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
2005 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
2006 sizeof(key->arp.sip));
2007 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
2008 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
2009 sizeof(key->arp.tip));
2010 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
2011 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
2012 sizeof(key->arp.op));
2013 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
2014 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
2015 sizeof(key->arp.sha));
2016 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
2017 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
2018 sizeof(key->arp.tha));
2019 } else if (key->basic.ip_proto == IPPROTO_L2TP) {
2020 fl_set_key_val(tb, &key->l2tpv3.session_id,
2021 TCA_FLOWER_KEY_L2TPV3_SID,
2022 &mask->l2tpv3.session_id, TCA_FLOWER_UNSPEC,
2023 sizeof(key->l2tpv3.session_id));
2024 } else if (key->basic.n_proto == htons(ETH_P_CFM)) {
2025 ret = fl_set_key_cfm(tb, key, mask, extack);
2026 if (ret)
2027 return ret;
2028 }
2029
2030 if (key->basic.ip_proto == IPPROTO_TCP ||
2031 key->basic.ip_proto == IPPROTO_UDP ||
2032 key->basic.ip_proto == IPPROTO_SCTP) {
2033 ret = fl_set_key_port_range(tb, key, mask, extack);
2034 if (ret)
2035 return ret;
2036 }
2037
2038 if (tb[TCA_FLOWER_KEY_SPI]) {
2039 ret = fl_set_key_spi(tb, key, mask, extack);
2040 if (ret)
2041 return ret;
2042 }
2043
2044 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
2045 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
2046 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
2047 mask->enc_control.addr_type = ~0;
2048 fl_set_key_val(tb, &key->enc_ipv4.src,
2049 TCA_FLOWER_KEY_ENC_IPV4_SRC,
2050 &mask->enc_ipv4.src,
2051 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
2052 sizeof(key->enc_ipv4.src));
2053 fl_set_key_val(tb, &key->enc_ipv4.dst,
2054 TCA_FLOWER_KEY_ENC_IPV4_DST,
2055 &mask->enc_ipv4.dst,
2056 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
2057 sizeof(key->enc_ipv4.dst));
2058 }
2059
2060 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
2061 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
2062 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
2063 mask->enc_control.addr_type = ~0;
2064 fl_set_key_val(tb, &key->enc_ipv6.src,
2065 TCA_FLOWER_KEY_ENC_IPV6_SRC,
2066 &mask->enc_ipv6.src,
2067 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
2068 sizeof(key->enc_ipv6.src));
2069 fl_set_key_val(tb, &key->enc_ipv6.dst,
2070 TCA_FLOWER_KEY_ENC_IPV6_DST,
2071 &mask->enc_ipv6.dst,
2072 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
2073 sizeof(key->enc_ipv6.dst));
2074 }
2075
2076 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
2077 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
2078 sizeof(key->enc_key_id.keyid));
2079
2080 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
2081 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
2082 sizeof(key->enc_tp.src));
2083
2084 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
2085 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
2086 sizeof(key->enc_tp.dst));
2087
2088 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
2089
2090 fl_set_key_val(tb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
2091 &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
2092 sizeof(key->hash.hash));
2093
2094 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
2095 ret = fl_set_enc_opt(tb, key, mask, extack);
2096 if (ret)
2097 return ret;
2098 }
2099
2100 ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack);
2101 if (ret)
2102 return ret;
2103
2104 if (tb[TCA_FLOWER_KEY_FLAGS]) {
2105 ret = fl_set_key_flags(tca_opts, tb, false,
2106 &key->control.flags,
2107 &mask->control.flags, extack);
2108 if (ret)
2109 return ret;
2110 }
2111
2112 if (tb[TCA_FLOWER_KEY_ENC_FLAGS])
2113 ret = fl_set_key_flags(tca_opts, tb, true,
2114 &key->enc_control.flags,
2115 &mask->enc_control.flags, extack);
2116
2117 return ret;
2118 }
2119
fl_mask_copy(struct fl_flow_mask * dst,struct fl_flow_mask * src)2120 static void fl_mask_copy(struct fl_flow_mask *dst,
2121 struct fl_flow_mask *src)
2122 {
2123 const void *psrc = fl_key_get_start(&src->key, src);
2124 void *pdst = fl_key_get_start(&dst->key, src);
2125
2126 memcpy(pdst, psrc, fl_mask_range(src));
2127 dst->range = src->range;
2128 }
2129
2130 static const struct rhashtable_params fl_ht_params = {
2131 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
2132 .head_offset = offsetof(struct cls_fl_filter, ht_node),
2133 .automatic_shrinking = true,
2134 };
2135
fl_init_mask_hashtable(struct fl_flow_mask * mask)2136 static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
2137 {
2138 mask->filter_ht_params = fl_ht_params;
2139 mask->filter_ht_params.key_len = fl_mask_range(mask);
2140 mask->filter_ht_params.key_offset += mask->range.start;
2141
2142 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
2143 }
2144
2145 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
2146 #define FL_KEY_MEMBER_SIZE(member) sizeof_field(struct fl_flow_key, member)
2147
2148 #define FL_KEY_IS_MASKED(mask, member) \
2149 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
2150 0, FL_KEY_MEMBER_SIZE(member)) \
2151
2152 #define FL_KEY_SET(keys, cnt, id, member) \
2153 do { \
2154 keys[cnt].key_id = id; \
2155 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
2156 cnt++; \
2157 } while(0);
2158
2159 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
2160 do { \
2161 if (FL_KEY_IS_MASKED(mask, member)) \
2162 FL_KEY_SET(keys, cnt, id, member); \
2163 } while(0);
2164
fl_init_dissector(struct flow_dissector * dissector,struct fl_flow_key * mask)2165 static void fl_init_dissector(struct flow_dissector *dissector,
2166 struct fl_flow_key *mask)
2167 {
2168 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
2169 size_t cnt = 0;
2170
2171 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2172 FLOW_DISSECTOR_KEY_META, meta);
2173 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
2174 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
2175 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2176 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
2177 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2178 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
2179 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2180 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
2181 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2182 FLOW_DISSECTOR_KEY_PORTS, tp);
2183 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2184 FLOW_DISSECTOR_KEY_PORTS_RANGE, tp_range);
2185 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2186 FLOW_DISSECTOR_KEY_IP, ip);
2187 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2188 FLOW_DISSECTOR_KEY_TCP, tcp);
2189 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2190 FLOW_DISSECTOR_KEY_ICMP, icmp);
2191 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2192 FLOW_DISSECTOR_KEY_ARP, arp);
2193 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2194 FLOW_DISSECTOR_KEY_MPLS, mpls);
2195 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2196 FLOW_DISSECTOR_KEY_VLAN, vlan);
2197 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2198 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
2199 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2200 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
2201 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2202 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
2203 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2204 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
2205 if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
2206 FL_KEY_IS_MASKED(mask, enc_ipv6) ||
2207 FL_KEY_IS_MASKED(mask, enc_control))
2208 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
2209 enc_control);
2210 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2211 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
2212 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2213 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
2214 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2215 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
2216 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2217 FLOW_DISSECTOR_KEY_CT, ct);
2218 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2219 FLOW_DISSECTOR_KEY_HASH, hash);
2220 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2221 FLOW_DISSECTOR_KEY_NUM_OF_VLANS, num_of_vlans);
2222 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2223 FLOW_DISSECTOR_KEY_PPPOE, pppoe);
2224 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2225 FLOW_DISSECTOR_KEY_L2TPV3, l2tpv3);
2226 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2227 FLOW_DISSECTOR_KEY_IPSEC, ipsec);
2228 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
2229 FLOW_DISSECTOR_KEY_CFM, cfm);
2230
2231 skb_flow_dissector_init(dissector, keys, cnt);
2232 }
2233
fl_create_new_mask(struct cls_fl_head * head,struct fl_flow_mask * mask)2234 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
2235 struct fl_flow_mask *mask)
2236 {
2237 struct fl_flow_mask *newmask;
2238 int err;
2239
2240 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
2241 if (!newmask)
2242 return ERR_PTR(-ENOMEM);
2243
2244 fl_mask_copy(newmask, mask);
2245
2246 if ((newmask->key.tp_range.tp_min.dst &&
2247 newmask->key.tp_range.tp_max.dst) ||
2248 (newmask->key.tp_range.tp_min.src &&
2249 newmask->key.tp_range.tp_max.src))
2250 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
2251
2252 err = fl_init_mask_hashtable(newmask);
2253 if (err)
2254 goto errout_free;
2255
2256 fl_init_dissector(&newmask->dissector, &newmask->key);
2257
2258 INIT_LIST_HEAD_RCU(&newmask->filters);
2259
2260 refcount_set(&newmask->refcnt, 1);
2261 err = rhashtable_replace_fast(&head->ht, &mask->ht_node,
2262 &newmask->ht_node, mask_ht_params);
2263 if (err)
2264 goto errout_destroy;
2265
2266 spin_lock(&head->masks_lock);
2267 list_add_tail_rcu(&newmask->list, &head->masks);
2268 spin_unlock(&head->masks_lock);
2269
2270 return newmask;
2271
2272 errout_destroy:
2273 rhashtable_destroy(&newmask->ht);
2274 errout_free:
2275 kfree(newmask);
2276
2277 return ERR_PTR(err);
2278 }
2279
fl_check_assign_mask(struct cls_fl_head * head,struct cls_fl_filter * fnew,struct cls_fl_filter * fold,struct fl_flow_mask * mask)2280 static int fl_check_assign_mask(struct cls_fl_head *head,
2281 struct cls_fl_filter *fnew,
2282 struct cls_fl_filter *fold,
2283 struct fl_flow_mask *mask)
2284 {
2285 struct fl_flow_mask *newmask;
2286 int ret = 0;
2287
2288 rcu_read_lock();
2289
2290 /* Insert mask as temporary node to prevent concurrent creation of mask
2291 * with same key. Any concurrent lookups with same key will return
2292 * -EAGAIN because mask's refcnt is zero.
2293 */
2294 fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht,
2295 &mask->ht_node,
2296 mask_ht_params);
2297 if (!fnew->mask) {
2298 rcu_read_unlock();
2299
2300 if (fold) {
2301 ret = -EINVAL;
2302 goto errout_cleanup;
2303 }
2304
2305 newmask = fl_create_new_mask(head, mask);
2306 if (IS_ERR(newmask)) {
2307 ret = PTR_ERR(newmask);
2308 goto errout_cleanup;
2309 }
2310
2311 fnew->mask = newmask;
2312 return 0;
2313 } else if (IS_ERR(fnew->mask)) {
2314 ret = PTR_ERR(fnew->mask);
2315 } else if (fold && fold->mask != fnew->mask) {
2316 ret = -EINVAL;
2317 } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) {
2318 /* Mask was deleted concurrently, try again */
2319 ret = -EAGAIN;
2320 }
2321 rcu_read_unlock();
2322 return ret;
2323
2324 errout_cleanup:
2325 rhashtable_remove_fast(&head->ht, &mask->ht_node,
2326 mask_ht_params);
2327 return ret;
2328 }
2329
fl_needs_tc_skb_ext(const struct fl_flow_key * mask)2330 static bool fl_needs_tc_skb_ext(const struct fl_flow_key *mask)
2331 {
2332 return mask->meta.l2_miss;
2333 }
2334
fl_ht_insert_unique(struct cls_fl_filter * fnew,struct cls_fl_filter * fold,bool * in_ht)2335 static int fl_ht_insert_unique(struct cls_fl_filter *fnew,
2336 struct cls_fl_filter *fold,
2337 bool *in_ht)
2338 {
2339 struct fl_flow_mask *mask = fnew->mask;
2340 int err;
2341
2342 err = rhashtable_lookup_insert_fast(&mask->ht,
2343 &fnew->ht_node,
2344 mask->filter_ht_params);
2345 if (err) {
2346 *in_ht = false;
2347 /* It is okay if filter with same key exists when
2348 * overwriting.
2349 */
2350 return fold && err == -EEXIST ? 0 : err;
2351 }
2352
2353 *in_ht = true;
2354 return 0;
2355 }
2356
fl_change(struct net * net,struct sk_buff * in_skb,struct tcf_proto * tp,unsigned long base,u32 handle,struct nlattr ** tca,void ** arg,u32 flags,struct netlink_ext_ack * extack)2357 static int fl_change(struct net *net, struct sk_buff *in_skb,
2358 struct tcf_proto *tp, unsigned long base,
2359 u32 handle, struct nlattr **tca,
2360 void **arg, u32 flags,
2361 struct netlink_ext_ack *extack)
2362 {
2363 struct cls_fl_head *head = fl_head_dereference(tp);
2364 bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL);
2365 struct nlattr *tca_opts = tca[TCA_OPTIONS];
2366 struct cls_fl_filter *fold = *arg;
2367 bool bound_to_filter = false;
2368 struct cls_fl_filter *fnew;
2369 struct fl_flow_mask *mask;
2370 struct nlattr **tb;
2371 bool in_ht;
2372 int err;
2373
2374 if (!tca_opts) {
2375 err = -EINVAL;
2376 goto errout_fold;
2377 }
2378
2379 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
2380 if (!mask) {
2381 err = -ENOBUFS;
2382 goto errout_fold;
2383 }
2384
2385 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
2386 if (!tb) {
2387 err = -ENOBUFS;
2388 goto errout_mask_alloc;
2389 }
2390
2391 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
2392 tca_opts, fl_policy, NULL);
2393 if (err < 0)
2394 goto errout_tb;
2395
2396 if (fold && handle && fold->handle != handle) {
2397 err = -EINVAL;
2398 goto errout_tb;
2399 }
2400
2401 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
2402 if (!fnew) {
2403 err = -ENOBUFS;
2404 goto errout_tb;
2405 }
2406 INIT_LIST_HEAD(&fnew->hw_list);
2407 refcount_set(&fnew->refcnt, 1);
2408
2409 if (tb[TCA_FLOWER_FLAGS]) {
2410 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
2411
2412 if (!tc_flags_valid(fnew->flags)) {
2413 kfree(fnew);
2414 err = -EINVAL;
2415 goto errout_tb;
2416 }
2417 }
2418
2419 if (!fold) {
2420 spin_lock(&tp->lock);
2421 if (!handle) {
2422 handle = 1;
2423 err = idr_alloc_u32(&head->handle_idr, NULL, &handle,
2424 INT_MAX, GFP_ATOMIC);
2425 } else {
2426 err = idr_alloc_u32(&head->handle_idr, NULL, &handle,
2427 handle, GFP_ATOMIC);
2428
2429 /* Filter with specified handle was concurrently
2430 * inserted after initial check in cls_api. This is not
2431 * necessarily an error if NLM_F_EXCL is not set in
2432 * message flags. Returning EAGAIN will cause cls_api to
2433 * try to update concurrently inserted rule.
2434 */
2435 if (err == -ENOSPC)
2436 err = -EAGAIN;
2437 }
2438 spin_unlock(&tp->lock);
2439
2440 if (err) {
2441 kfree(fnew);
2442 goto errout_tb;
2443 }
2444 }
2445 fnew->handle = handle;
2446
2447 err = tcf_exts_init_ex(&fnew->exts, net, TCA_FLOWER_ACT, 0, tp, handle,
2448 !tc_skip_hw(fnew->flags));
2449 if (err < 0)
2450 goto errout_idr;
2451
2452 err = tcf_exts_validate_ex(net, tp, tb, tca[TCA_RATE],
2453 &fnew->exts, flags, fnew->flags,
2454 extack);
2455 if (err < 0)
2456 goto errout_idr;
2457
2458 if (tb[TCA_FLOWER_CLASSID]) {
2459 fnew->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
2460 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2461 rtnl_lock();
2462 tcf_bind_filter(tp, &fnew->res, base);
2463 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2464 rtnl_unlock();
2465 bound_to_filter = true;
2466 }
2467
2468 err = fl_set_key(net, tca_opts, tb, &fnew->key, &mask->key, extack);
2469 if (err)
2470 goto unbind_filter;
2471
2472 fl_mask_update_range(mask);
2473 fl_set_masked_key(&fnew->mkey, &fnew->key, mask);
2474
2475 if (!fl_mask_fits_tmplt(tp->chain->tmplt_priv, mask)) {
2476 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
2477 err = -EINVAL;
2478 goto unbind_filter;
2479 }
2480
2481 /* Enable tc skb extension if filter matches on data extracted from
2482 * this extension.
2483 */
2484 if (fl_needs_tc_skb_ext(&mask->key)) {
2485 fnew->needs_tc_skb_ext = 1;
2486 tc_skb_ext_tc_enable();
2487 }
2488
2489 err = fl_check_assign_mask(head, fnew, fold, mask);
2490 if (err)
2491 goto unbind_filter;
2492
2493 err = fl_ht_insert_unique(fnew, fold, &in_ht);
2494 if (err)
2495 goto errout_mask;
2496
2497 if (!tc_skip_hw(fnew->flags)) {
2498 err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack);
2499 if (err)
2500 goto errout_ht;
2501 }
2502
2503 if (!tc_in_hw(fnew->flags))
2504 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
2505
2506 spin_lock(&tp->lock);
2507
2508 /* tp was deleted concurrently. -EAGAIN will cause caller to lookup
2509 * proto again or create new one, if necessary.
2510 */
2511 if (tp->deleting) {
2512 err = -EAGAIN;
2513 goto errout_hw;
2514 }
2515
2516 if (fold) {
2517 /* Fold filter was deleted concurrently. Retry lookup. */
2518 if (fold->deleted) {
2519 err = -EAGAIN;
2520 goto errout_hw;
2521 }
2522
2523 fnew->handle = handle;
2524
2525 if (!in_ht) {
2526 struct rhashtable_params params =
2527 fnew->mask->filter_ht_params;
2528
2529 err = rhashtable_insert_fast(&fnew->mask->ht,
2530 &fnew->ht_node,
2531 params);
2532 if (err)
2533 goto errout_hw;
2534 in_ht = true;
2535 }
2536
2537 refcount_inc(&fnew->refcnt);
2538 rhashtable_remove_fast(&fold->mask->ht,
2539 &fold->ht_node,
2540 fold->mask->filter_ht_params);
2541 idr_replace(&head->handle_idr, fnew, fnew->handle);
2542 list_replace_rcu(&fold->list, &fnew->list);
2543 fold->deleted = true;
2544
2545 spin_unlock(&tp->lock);
2546
2547 fl_mask_put(head, fold->mask);
2548 if (!tc_skip_hw(fold->flags))
2549 fl_hw_destroy_filter(tp, fold, rtnl_held, NULL);
2550 tcf_unbind_filter(tp, &fold->res);
2551 /* Caller holds reference to fold, so refcnt is always > 0
2552 * after this.
2553 */
2554 refcount_dec(&fold->refcnt);
2555 __fl_put(fold);
2556 } else {
2557 idr_replace(&head->handle_idr, fnew, fnew->handle);
2558
2559 refcount_inc(&fnew->refcnt);
2560 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
2561 spin_unlock(&tp->lock);
2562 }
2563
2564 *arg = fnew;
2565
2566 kfree(tb);
2567 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
2568 return 0;
2569
2570 errout_ht:
2571 spin_lock(&tp->lock);
2572 errout_hw:
2573 fnew->deleted = true;
2574 spin_unlock(&tp->lock);
2575 if (!tc_skip_hw(fnew->flags))
2576 fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL);
2577 if (in_ht)
2578 rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node,
2579 fnew->mask->filter_ht_params);
2580 errout_mask:
2581 fl_mask_put(head, fnew->mask);
2582
2583 unbind_filter:
2584 if (bound_to_filter) {
2585 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2586 rtnl_lock();
2587 tcf_unbind_filter(tp, &fnew->res);
2588 if (flags & TCA_ACT_FLAGS_NO_RTNL)
2589 rtnl_unlock();
2590 }
2591
2592 errout_idr:
2593 if (!fold) {
2594 spin_lock(&tp->lock);
2595 idr_remove(&head->handle_idr, fnew->handle);
2596 spin_unlock(&tp->lock);
2597 }
2598 __fl_put(fnew);
2599 errout_tb:
2600 kfree(tb);
2601 errout_mask_alloc:
2602 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
2603 errout_fold:
2604 if (fold)
2605 __fl_put(fold);
2606 return err;
2607 }
2608
fl_delete(struct tcf_proto * tp,void * arg,bool * last,bool rtnl_held,struct netlink_ext_ack * extack)2609 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
2610 bool rtnl_held, struct netlink_ext_ack *extack)
2611 {
2612 struct cls_fl_head *head = fl_head_dereference(tp);
2613 struct cls_fl_filter *f = arg;
2614 bool last_on_mask;
2615 int err = 0;
2616
2617 err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack);
2618 *last = list_empty(&head->masks);
2619 __fl_put(f);
2620
2621 return err;
2622 }
2623
fl_walk(struct tcf_proto * tp,struct tcf_walker * arg,bool rtnl_held)2624 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg,
2625 bool rtnl_held)
2626 {
2627 struct cls_fl_head *head = fl_head_dereference(tp);
2628 unsigned long id = arg->cookie, tmp;
2629 struct cls_fl_filter *f;
2630
2631 arg->count = arg->skip;
2632
2633 rcu_read_lock();
2634 idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) {
2635 /* don't return filters that are being deleted */
2636 if (!f || !refcount_inc_not_zero(&f->refcnt))
2637 continue;
2638 rcu_read_unlock();
2639
2640 if (arg->fn(tp, f, arg) < 0) {
2641 __fl_put(f);
2642 arg->stop = 1;
2643 rcu_read_lock();
2644 break;
2645 }
2646 __fl_put(f);
2647 arg->count++;
2648 rcu_read_lock();
2649 }
2650 rcu_read_unlock();
2651 arg->cookie = id;
2652 }
2653
2654 static struct cls_fl_filter *
fl_get_next_hw_filter(struct tcf_proto * tp,struct cls_fl_filter * f,bool add)2655 fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add)
2656 {
2657 struct cls_fl_head *head = fl_head_dereference(tp);
2658
2659 spin_lock(&tp->lock);
2660 if (list_empty(&head->hw_filters)) {
2661 spin_unlock(&tp->lock);
2662 return NULL;
2663 }
2664
2665 if (!f)
2666 f = list_entry(&head->hw_filters, struct cls_fl_filter,
2667 hw_list);
2668 list_for_each_entry_continue(f, &head->hw_filters, hw_list) {
2669 if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) {
2670 spin_unlock(&tp->lock);
2671 return f;
2672 }
2673 }
2674
2675 spin_unlock(&tp->lock);
2676 return NULL;
2677 }
2678
fl_reoffload(struct tcf_proto * tp,bool add,flow_setup_cb_t * cb,void * cb_priv,struct netlink_ext_ack * extack)2679 static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
2680 void *cb_priv, struct netlink_ext_ack *extack)
2681 {
2682 struct tcf_block *block = tp->chain->block;
2683 struct flow_cls_offload cls_flower = {};
2684 struct cls_fl_filter *f = NULL;
2685 int err;
2686
2687 /* hw_filters list can only be changed by hw offload functions after
2688 * obtaining rtnl lock. Make sure it is not changed while reoffload is
2689 * iterating it.
2690 */
2691 ASSERT_RTNL();
2692
2693 while ((f = fl_get_next_hw_filter(tp, f, add))) {
2694 cls_flower.rule =
2695 flow_rule_alloc(tcf_exts_num_actions(&f->exts));
2696 if (!cls_flower.rule) {
2697 __fl_put(f);
2698 return -ENOMEM;
2699 }
2700
2701 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags,
2702 extack);
2703 cls_flower.command = add ?
2704 FLOW_CLS_REPLACE : FLOW_CLS_DESTROY;
2705 cls_flower.cookie = (unsigned long)f;
2706 cls_flower.rule->match.dissector = &f->mask->dissector;
2707 cls_flower.rule->match.mask = &f->mask->key;
2708 cls_flower.rule->match.key = &f->mkey;
2709
2710 err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts,
2711 cls_flower.common.extack);
2712 if (err) {
2713 kfree(cls_flower.rule);
2714 if (tc_skip_sw(f->flags)) {
2715 __fl_put(f);
2716 return err;
2717 }
2718 goto next_flow;
2719 }
2720
2721 cls_flower.classid = f->res.classid;
2722
2723 err = tc_setup_cb_reoffload(block, tp, add, cb,
2724 TC_SETUP_CLSFLOWER, &cls_flower,
2725 cb_priv, &f->flags,
2726 &f->in_hw_count);
2727 tc_cleanup_offload_action(&cls_flower.rule->action);
2728 kfree(cls_flower.rule);
2729
2730 if (err) {
2731 __fl_put(f);
2732 return err;
2733 }
2734 next_flow:
2735 __fl_put(f);
2736 }
2737
2738 return 0;
2739 }
2740
fl_hw_add(struct tcf_proto * tp,void * type_data)2741 static void fl_hw_add(struct tcf_proto *tp, void *type_data)
2742 {
2743 struct flow_cls_offload *cls_flower = type_data;
2744 struct cls_fl_filter *f =
2745 (struct cls_fl_filter *) cls_flower->cookie;
2746 struct cls_fl_head *head = fl_head_dereference(tp);
2747
2748 spin_lock(&tp->lock);
2749 list_add(&f->hw_list, &head->hw_filters);
2750 spin_unlock(&tp->lock);
2751 }
2752
fl_hw_del(struct tcf_proto * tp,void * type_data)2753 static void fl_hw_del(struct tcf_proto *tp, void *type_data)
2754 {
2755 struct flow_cls_offload *cls_flower = type_data;
2756 struct cls_fl_filter *f =
2757 (struct cls_fl_filter *) cls_flower->cookie;
2758
2759 spin_lock(&tp->lock);
2760 if (!list_empty(&f->hw_list))
2761 list_del_init(&f->hw_list);
2762 spin_unlock(&tp->lock);
2763 }
2764
fl_hw_create_tmplt(struct tcf_chain * chain,struct fl_flow_tmplt * tmplt)2765 static int fl_hw_create_tmplt(struct tcf_chain *chain,
2766 struct fl_flow_tmplt *tmplt)
2767 {
2768 struct flow_cls_offload cls_flower = {};
2769 struct tcf_block *block = chain->block;
2770
2771 cls_flower.rule = flow_rule_alloc(0);
2772 if (!cls_flower.rule)
2773 return -ENOMEM;
2774
2775 cls_flower.common.chain_index = chain->index;
2776 cls_flower.command = FLOW_CLS_TMPLT_CREATE;
2777 cls_flower.cookie = (unsigned long) tmplt;
2778 cls_flower.rule->match.dissector = &tmplt->dissector;
2779 cls_flower.rule->match.mask = &tmplt->mask;
2780 cls_flower.rule->match.key = &tmplt->dummy_key;
2781
2782 /* We don't care if driver (any of them) fails to handle this
2783 * call. It serves just as a hint for it.
2784 */
2785 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
2786 kfree(cls_flower.rule);
2787
2788 return 0;
2789 }
2790
fl_hw_destroy_tmplt(struct tcf_chain * chain,struct fl_flow_tmplt * tmplt)2791 static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
2792 struct fl_flow_tmplt *tmplt)
2793 {
2794 struct flow_cls_offload cls_flower = {};
2795 struct tcf_block *block = chain->block;
2796
2797 cls_flower.common.chain_index = chain->index;
2798 cls_flower.command = FLOW_CLS_TMPLT_DESTROY;
2799 cls_flower.cookie = (unsigned long) tmplt;
2800
2801 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
2802 }
2803
fl_tmplt_create(struct net * net,struct tcf_chain * chain,struct nlattr ** tca,struct netlink_ext_ack * extack)2804 static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
2805 struct nlattr **tca,
2806 struct netlink_ext_ack *extack)
2807 {
2808 struct nlattr *tca_opts = tca[TCA_OPTIONS];
2809 struct fl_flow_tmplt *tmplt;
2810 struct nlattr **tb;
2811 int err;
2812
2813 if (!tca_opts)
2814 return ERR_PTR(-EINVAL);
2815
2816 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
2817 if (!tb)
2818 return ERR_PTR(-ENOBUFS);
2819 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
2820 tca_opts, fl_policy, NULL);
2821 if (err)
2822 goto errout_tb;
2823
2824 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
2825 if (!tmplt) {
2826 err = -ENOMEM;
2827 goto errout_tb;
2828 }
2829 tmplt->chain = chain;
2830 err = fl_set_key(net, tca_opts, tb, &tmplt->dummy_key,
2831 &tmplt->mask, extack);
2832 if (err)
2833 goto errout_tmplt;
2834
2835 fl_init_dissector(&tmplt->dissector, &tmplt->mask);
2836
2837 err = fl_hw_create_tmplt(chain, tmplt);
2838 if (err)
2839 goto errout_tmplt;
2840
2841 kfree(tb);
2842 return tmplt;
2843
2844 errout_tmplt:
2845 kfree(tmplt);
2846 errout_tb:
2847 kfree(tb);
2848 return ERR_PTR(err);
2849 }
2850
fl_tmplt_destroy(void * tmplt_priv)2851 static void fl_tmplt_destroy(void *tmplt_priv)
2852 {
2853 struct fl_flow_tmplt *tmplt = tmplt_priv;
2854
2855 fl_hw_destroy_tmplt(tmplt->chain, tmplt);
2856 kfree(tmplt);
2857 }
2858
fl_tmplt_reoffload(struct tcf_chain * chain,bool add,flow_setup_cb_t * cb,void * cb_priv)2859 static void fl_tmplt_reoffload(struct tcf_chain *chain, bool add,
2860 flow_setup_cb_t *cb, void *cb_priv)
2861 {
2862 struct fl_flow_tmplt *tmplt = chain->tmplt_priv;
2863 struct flow_cls_offload cls_flower = {};
2864
2865 cls_flower.rule = flow_rule_alloc(0);
2866 if (!cls_flower.rule)
2867 return;
2868
2869 cls_flower.common.chain_index = chain->index;
2870 cls_flower.command = add ? FLOW_CLS_TMPLT_CREATE :
2871 FLOW_CLS_TMPLT_DESTROY;
2872 cls_flower.cookie = (unsigned long) tmplt;
2873 cls_flower.rule->match.dissector = &tmplt->dissector;
2874 cls_flower.rule->match.mask = &tmplt->mask;
2875 cls_flower.rule->match.key = &tmplt->dummy_key;
2876
2877 cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
2878 kfree(cls_flower.rule);
2879 }
2880
fl_dump_key_val(struct sk_buff * skb,void * val,int val_type,void * mask,int mask_type,int len)2881 static int fl_dump_key_val(struct sk_buff *skb,
2882 void *val, int val_type,
2883 void *mask, int mask_type, int len)
2884 {
2885 int err;
2886
2887 if (!memchr_inv(mask, 0, len))
2888 return 0;
2889 err = nla_put(skb, val_type, len, val);
2890 if (err)
2891 return err;
2892 if (mask_type != TCA_FLOWER_UNSPEC) {
2893 err = nla_put(skb, mask_type, len, mask);
2894 if (err)
2895 return err;
2896 }
2897 return 0;
2898 }
2899
fl_dump_key_port_range(struct sk_buff * skb,struct fl_flow_key * key,struct fl_flow_key * mask)2900 static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
2901 struct fl_flow_key *mask)
2902 {
2903 if (fl_dump_key_val(skb, &key->tp_range.tp_min.dst,
2904 TCA_FLOWER_KEY_PORT_DST_MIN,
2905 &mask->tp_range.tp_min.dst, TCA_FLOWER_UNSPEC,
2906 sizeof(key->tp_range.tp_min.dst)) ||
2907 fl_dump_key_val(skb, &key->tp_range.tp_max.dst,
2908 TCA_FLOWER_KEY_PORT_DST_MAX,
2909 &mask->tp_range.tp_max.dst, TCA_FLOWER_UNSPEC,
2910 sizeof(key->tp_range.tp_max.dst)) ||
2911 fl_dump_key_val(skb, &key->tp_range.tp_min.src,
2912 TCA_FLOWER_KEY_PORT_SRC_MIN,
2913 &mask->tp_range.tp_min.src, TCA_FLOWER_UNSPEC,
2914 sizeof(key->tp_range.tp_min.src)) ||
2915 fl_dump_key_val(skb, &key->tp_range.tp_max.src,
2916 TCA_FLOWER_KEY_PORT_SRC_MAX,
2917 &mask->tp_range.tp_max.src, TCA_FLOWER_UNSPEC,
2918 sizeof(key->tp_range.tp_max.src)))
2919 return -1;
2920
2921 return 0;
2922 }
2923
fl_dump_key_mpls_opt_lse(struct sk_buff * skb,struct flow_dissector_key_mpls * mpls_key,struct flow_dissector_key_mpls * mpls_mask,u8 lse_index)2924 static int fl_dump_key_mpls_opt_lse(struct sk_buff *skb,
2925 struct flow_dissector_key_mpls *mpls_key,
2926 struct flow_dissector_key_mpls *mpls_mask,
2927 u8 lse_index)
2928 {
2929 struct flow_dissector_mpls_lse *lse_mask = &mpls_mask->ls[lse_index];
2930 struct flow_dissector_mpls_lse *lse_key = &mpls_key->ls[lse_index];
2931 int err;
2932
2933 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH,
2934 lse_index + 1);
2935 if (err)
2936 return err;
2937
2938 if (lse_mask->mpls_ttl) {
2939 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL,
2940 lse_key->mpls_ttl);
2941 if (err)
2942 return err;
2943 }
2944 if (lse_mask->mpls_bos) {
2945 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS,
2946 lse_key->mpls_bos);
2947 if (err)
2948 return err;
2949 }
2950 if (lse_mask->mpls_tc) {
2951 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TC,
2952 lse_key->mpls_tc);
2953 if (err)
2954 return err;
2955 }
2956 if (lse_mask->mpls_label) {
2957 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL,
2958 lse_key->mpls_label);
2959 if (err)
2960 return err;
2961 }
2962
2963 return 0;
2964 }
2965
fl_dump_key_mpls_opts(struct sk_buff * skb,struct flow_dissector_key_mpls * mpls_key,struct flow_dissector_key_mpls * mpls_mask)2966 static int fl_dump_key_mpls_opts(struct sk_buff *skb,
2967 struct flow_dissector_key_mpls *mpls_key,
2968 struct flow_dissector_key_mpls *mpls_mask)
2969 {
2970 struct nlattr *opts;
2971 struct nlattr *lse;
2972 u8 lse_index;
2973 int err;
2974
2975 opts = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS);
2976 if (!opts)
2977 return -EMSGSIZE;
2978
2979 for (lse_index = 0; lse_index < FLOW_DIS_MPLS_MAX; lse_index++) {
2980 if (!(mpls_mask->used_lses & 1 << lse_index))
2981 continue;
2982
2983 lse = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS_LSE);
2984 if (!lse) {
2985 err = -EMSGSIZE;
2986 goto err_opts;
2987 }
2988
2989 err = fl_dump_key_mpls_opt_lse(skb, mpls_key, mpls_mask,
2990 lse_index);
2991 if (err)
2992 goto err_opts_lse;
2993 nla_nest_end(skb, lse);
2994 }
2995 nla_nest_end(skb, opts);
2996
2997 return 0;
2998
2999 err_opts_lse:
3000 nla_nest_cancel(skb, lse);
3001 err_opts:
3002 nla_nest_cancel(skb, opts);
3003
3004 return err;
3005 }
3006
fl_dump_key_mpls(struct sk_buff * skb,struct flow_dissector_key_mpls * mpls_key,struct flow_dissector_key_mpls * mpls_mask)3007 static int fl_dump_key_mpls(struct sk_buff *skb,
3008 struct flow_dissector_key_mpls *mpls_key,
3009 struct flow_dissector_key_mpls *mpls_mask)
3010 {
3011 struct flow_dissector_mpls_lse *lse_mask;
3012 struct flow_dissector_mpls_lse *lse_key;
3013 int err;
3014
3015 if (!mpls_mask->used_lses)
3016 return 0;
3017
3018 lse_mask = &mpls_mask->ls[0];
3019 lse_key = &mpls_key->ls[0];
3020
3021 /* For backward compatibility, don't use the MPLS nested attributes if
3022 * the rule can be expressed using the old attributes.
3023 */
3024 if (mpls_mask->used_lses & ~1 ||
3025 (!lse_mask->mpls_ttl && !lse_mask->mpls_bos &&
3026 !lse_mask->mpls_tc && !lse_mask->mpls_label))
3027 return fl_dump_key_mpls_opts(skb, mpls_key, mpls_mask);
3028
3029 if (lse_mask->mpls_ttl) {
3030 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
3031 lse_key->mpls_ttl);
3032 if (err)
3033 return err;
3034 }
3035 if (lse_mask->mpls_tc) {
3036 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
3037 lse_key->mpls_tc);
3038 if (err)
3039 return err;
3040 }
3041 if (lse_mask->mpls_label) {
3042 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
3043 lse_key->mpls_label);
3044 if (err)
3045 return err;
3046 }
3047 if (lse_mask->mpls_bos) {
3048 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
3049 lse_key->mpls_bos);
3050 if (err)
3051 return err;
3052 }
3053 return 0;
3054 }
3055
fl_dump_key_ip(struct sk_buff * skb,bool encap,struct flow_dissector_key_ip * key,struct flow_dissector_key_ip * mask)3056 static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
3057 struct flow_dissector_key_ip *key,
3058 struct flow_dissector_key_ip *mask)
3059 {
3060 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
3061 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
3062 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
3063 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
3064
3065 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
3066 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
3067 return -1;
3068
3069 return 0;
3070 }
3071
fl_dump_key_vlan(struct sk_buff * skb,int vlan_id_key,int vlan_prio_key,struct flow_dissector_key_vlan * vlan_key,struct flow_dissector_key_vlan * vlan_mask)3072 static int fl_dump_key_vlan(struct sk_buff *skb,
3073 int vlan_id_key, int vlan_prio_key,
3074 struct flow_dissector_key_vlan *vlan_key,
3075 struct flow_dissector_key_vlan *vlan_mask)
3076 {
3077 int err;
3078
3079 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
3080 return 0;
3081 if (vlan_mask->vlan_id) {
3082 err = nla_put_u16(skb, vlan_id_key,
3083 vlan_key->vlan_id);
3084 if (err)
3085 return err;
3086 }
3087 if (vlan_mask->vlan_priority) {
3088 err = nla_put_u8(skb, vlan_prio_key,
3089 vlan_key->vlan_priority);
3090 if (err)
3091 return err;
3092 }
3093 return 0;
3094 }
3095
fl_get_key_flag(u32 dissector_key,u32 dissector_mask,u32 * flower_key,u32 * flower_mask,u32 flower_flag_bit,u32 dissector_flag_bit)3096 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
3097 u32 *flower_key, u32 *flower_mask,
3098 u32 flower_flag_bit, u32 dissector_flag_bit)
3099 {
3100 if (dissector_mask & dissector_flag_bit) {
3101 *flower_mask |= flower_flag_bit;
3102 if (dissector_key & dissector_flag_bit)
3103 *flower_key |= flower_flag_bit;
3104 }
3105 }
3106
fl_dump_key_flags(struct sk_buff * skb,bool encap,u32 flags_key,u32 flags_mask)3107 static int fl_dump_key_flags(struct sk_buff *skb, bool encap,
3108 u32 flags_key, u32 flags_mask)
3109 {
3110 int fl_key, fl_mask;
3111 __be32 _key, _mask;
3112 u32 key, mask;
3113 int err;
3114
3115 if (encap) {
3116 fl_key = TCA_FLOWER_KEY_ENC_FLAGS;
3117 fl_mask = TCA_FLOWER_KEY_ENC_FLAGS_MASK;
3118 } else {
3119 fl_key = TCA_FLOWER_KEY_FLAGS;
3120 fl_mask = TCA_FLOWER_KEY_FLAGS_MASK;
3121 }
3122
3123 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
3124 return 0;
3125
3126 key = 0;
3127 mask = 0;
3128
3129 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
3130 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
3131 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
3132 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
3133 FLOW_DIS_FIRST_FRAG);
3134
3135 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
3136 TCA_FLOWER_KEY_FLAGS_TUNNEL_CSUM,
3137 FLOW_DIS_F_TUNNEL_CSUM);
3138
3139 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
3140 TCA_FLOWER_KEY_FLAGS_TUNNEL_DONT_FRAGMENT,
3141 FLOW_DIS_F_TUNNEL_DONT_FRAGMENT);
3142
3143 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
3144 TCA_FLOWER_KEY_FLAGS_TUNNEL_OAM, FLOW_DIS_F_TUNNEL_OAM);
3145
3146 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
3147 TCA_FLOWER_KEY_FLAGS_TUNNEL_CRIT_OPT,
3148 FLOW_DIS_F_TUNNEL_CRIT_OPT);
3149
3150 _key = cpu_to_be32(key);
3151 _mask = cpu_to_be32(mask);
3152
3153 err = nla_put(skb, fl_key, 4, &_key);
3154 if (err)
3155 return err;
3156
3157 return nla_put(skb, fl_mask, 4, &_mask);
3158 }
3159
fl_dump_key_geneve_opt(struct sk_buff * skb,struct flow_dissector_key_enc_opts * enc_opts)3160 static int fl_dump_key_geneve_opt(struct sk_buff *skb,
3161 struct flow_dissector_key_enc_opts *enc_opts)
3162 {
3163 struct geneve_opt *opt;
3164 struct nlattr *nest;
3165 int opt_off = 0;
3166
3167 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
3168 if (!nest)
3169 goto nla_put_failure;
3170
3171 while (enc_opts->len > opt_off) {
3172 opt = (struct geneve_opt *)&enc_opts->data[opt_off];
3173
3174 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
3175 opt->opt_class))
3176 goto nla_put_failure;
3177 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
3178 opt->type))
3179 goto nla_put_failure;
3180 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
3181 opt->length * 4, opt->opt_data))
3182 goto nla_put_failure;
3183
3184 opt_off += sizeof(struct geneve_opt) + opt->length * 4;
3185 }
3186 nla_nest_end(skb, nest);
3187 return 0;
3188
3189 nla_put_failure:
3190 nla_nest_cancel(skb, nest);
3191 return -EMSGSIZE;
3192 }
3193
fl_dump_key_vxlan_opt(struct sk_buff * skb,struct flow_dissector_key_enc_opts * enc_opts)3194 static int fl_dump_key_vxlan_opt(struct sk_buff *skb,
3195 struct flow_dissector_key_enc_opts *enc_opts)
3196 {
3197 struct vxlan_metadata *md;
3198 struct nlattr *nest;
3199
3200 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_VXLAN);
3201 if (!nest)
3202 goto nla_put_failure;
3203
3204 md = (struct vxlan_metadata *)&enc_opts->data[0];
3205 if (nla_put_u32(skb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, md->gbp))
3206 goto nla_put_failure;
3207
3208 nla_nest_end(skb, nest);
3209 return 0;
3210
3211 nla_put_failure:
3212 nla_nest_cancel(skb, nest);
3213 return -EMSGSIZE;
3214 }
3215
fl_dump_key_erspan_opt(struct sk_buff * skb,struct flow_dissector_key_enc_opts * enc_opts)3216 static int fl_dump_key_erspan_opt(struct sk_buff *skb,
3217 struct flow_dissector_key_enc_opts *enc_opts)
3218 {
3219 struct erspan_metadata *md;
3220 struct nlattr *nest;
3221
3222 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_ERSPAN);
3223 if (!nest)
3224 goto nla_put_failure;
3225
3226 md = (struct erspan_metadata *)&enc_opts->data[0];
3227 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER, md->version))
3228 goto nla_put_failure;
3229
3230 if (md->version == 1 &&
3231 nla_put_be32(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index))
3232 goto nla_put_failure;
3233
3234 if (md->version == 2 &&
3235 (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR,
3236 md->u.md2.dir) ||
3237 nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID,
3238 get_hwid(&md->u.md2))))
3239 goto nla_put_failure;
3240
3241 nla_nest_end(skb, nest);
3242 return 0;
3243
3244 nla_put_failure:
3245 nla_nest_cancel(skb, nest);
3246 return -EMSGSIZE;
3247 }
3248
fl_dump_key_gtp_opt(struct sk_buff * skb,struct flow_dissector_key_enc_opts * enc_opts)3249 static int fl_dump_key_gtp_opt(struct sk_buff *skb,
3250 struct flow_dissector_key_enc_opts *enc_opts)
3251
3252 {
3253 struct gtp_pdu_session_info *session_info;
3254 struct nlattr *nest;
3255
3256 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GTP);
3257 if (!nest)
3258 goto nla_put_failure;
3259
3260 session_info = (struct gtp_pdu_session_info *)&enc_opts->data[0];
3261
3262 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GTP_PDU_TYPE,
3263 session_info->pdu_type))
3264 goto nla_put_failure;
3265
3266 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GTP_QFI, session_info->qfi))
3267 goto nla_put_failure;
3268
3269 nla_nest_end(skb, nest);
3270 return 0;
3271
3272 nla_put_failure:
3273 nla_nest_cancel(skb, nest);
3274 return -EMSGSIZE;
3275 }
3276
fl_dump_key_pfcp_opt(struct sk_buff * skb,struct flow_dissector_key_enc_opts * enc_opts)3277 static int fl_dump_key_pfcp_opt(struct sk_buff *skb,
3278 struct flow_dissector_key_enc_opts *enc_opts)
3279 {
3280 struct pfcp_metadata *md;
3281 struct nlattr *nest;
3282
3283 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_PFCP);
3284 if (!nest)
3285 goto nla_put_failure;
3286
3287 md = (struct pfcp_metadata *)&enc_opts->data[0];
3288 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_PFCP_TYPE, md->type))
3289 goto nla_put_failure;
3290
3291 if (nla_put_be64(skb, TCA_FLOWER_KEY_ENC_OPT_PFCP_SEID,
3292 md->seid, 0))
3293 goto nla_put_failure;
3294
3295 nla_nest_end(skb, nest);
3296 return 0;
3297
3298 nla_put_failure:
3299 nla_nest_cancel(skb, nest);
3300 return -EMSGSIZE;
3301 }
3302
fl_dump_key_ct(struct sk_buff * skb,struct flow_dissector_key_ct * key,struct flow_dissector_key_ct * mask)3303 static int fl_dump_key_ct(struct sk_buff *skb,
3304 struct flow_dissector_key_ct *key,
3305 struct flow_dissector_key_ct *mask)
3306 {
3307 if (IS_ENABLED(CONFIG_NF_CONNTRACK) &&
3308 fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
3309 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
3310 sizeof(key->ct_state)))
3311 goto nla_put_failure;
3312
3313 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
3314 fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
3315 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
3316 sizeof(key->ct_zone)))
3317 goto nla_put_failure;
3318
3319 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
3320 fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
3321 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
3322 sizeof(key->ct_mark)))
3323 goto nla_put_failure;
3324
3325 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
3326 fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
3327 &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
3328 sizeof(key->ct_labels)))
3329 goto nla_put_failure;
3330
3331 return 0;
3332
3333 nla_put_failure:
3334 return -EMSGSIZE;
3335 }
3336
fl_dump_key_cfm(struct sk_buff * skb,struct flow_dissector_key_cfm * key,struct flow_dissector_key_cfm * mask)3337 static int fl_dump_key_cfm(struct sk_buff *skb,
3338 struct flow_dissector_key_cfm *key,
3339 struct flow_dissector_key_cfm *mask)
3340 {
3341 struct nlattr *opts;
3342 int err;
3343 u8 mdl;
3344
3345 if (!memchr_inv(mask, 0, sizeof(*mask)))
3346 return 0;
3347
3348 opts = nla_nest_start(skb, TCA_FLOWER_KEY_CFM);
3349 if (!opts)
3350 return -EMSGSIZE;
3351
3352 if (FIELD_GET(FLOW_DIS_CFM_MDL_MASK, mask->mdl_ver)) {
3353 mdl = FIELD_GET(FLOW_DIS_CFM_MDL_MASK, key->mdl_ver);
3354 err = nla_put_u8(skb, TCA_FLOWER_KEY_CFM_MD_LEVEL, mdl);
3355 if (err)
3356 goto err_cfm_opts;
3357 }
3358
3359 if (mask->opcode) {
3360 err = nla_put_u8(skb, TCA_FLOWER_KEY_CFM_OPCODE, key->opcode);
3361 if (err)
3362 goto err_cfm_opts;
3363 }
3364
3365 nla_nest_end(skb, opts);
3366
3367 return 0;
3368
3369 err_cfm_opts:
3370 nla_nest_cancel(skb, opts);
3371 return err;
3372 }
3373
fl_dump_key_options(struct sk_buff * skb,int enc_opt_type,struct flow_dissector_key_enc_opts * enc_opts)3374 static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
3375 struct flow_dissector_key_enc_opts *enc_opts)
3376 {
3377 struct nlattr *nest;
3378 int err;
3379
3380 if (!enc_opts->len)
3381 return 0;
3382
3383 nest = nla_nest_start_noflag(skb, enc_opt_type);
3384 if (!nest)
3385 goto nla_put_failure;
3386
3387 switch (enc_opts->dst_opt_type) {
3388 case IP_TUNNEL_GENEVE_OPT_BIT:
3389 err = fl_dump_key_geneve_opt(skb, enc_opts);
3390 if (err)
3391 goto nla_put_failure;
3392 break;
3393 case IP_TUNNEL_VXLAN_OPT_BIT:
3394 err = fl_dump_key_vxlan_opt(skb, enc_opts);
3395 if (err)
3396 goto nla_put_failure;
3397 break;
3398 case IP_TUNNEL_ERSPAN_OPT_BIT:
3399 err = fl_dump_key_erspan_opt(skb, enc_opts);
3400 if (err)
3401 goto nla_put_failure;
3402 break;
3403 case IP_TUNNEL_GTP_OPT_BIT:
3404 err = fl_dump_key_gtp_opt(skb, enc_opts);
3405 if (err)
3406 goto nla_put_failure;
3407 break;
3408 case IP_TUNNEL_PFCP_OPT_BIT:
3409 err = fl_dump_key_pfcp_opt(skb, enc_opts);
3410 if (err)
3411 goto nla_put_failure;
3412 break;
3413 default:
3414 goto nla_put_failure;
3415 }
3416 nla_nest_end(skb, nest);
3417 return 0;
3418
3419 nla_put_failure:
3420 nla_nest_cancel(skb, nest);
3421 return -EMSGSIZE;
3422 }
3423
fl_dump_key_enc_opt(struct sk_buff * skb,struct flow_dissector_key_enc_opts * key_opts,struct flow_dissector_key_enc_opts * msk_opts)3424 static int fl_dump_key_enc_opt(struct sk_buff *skb,
3425 struct flow_dissector_key_enc_opts *key_opts,
3426 struct flow_dissector_key_enc_opts *msk_opts)
3427 {
3428 int err;
3429
3430 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
3431 if (err)
3432 return err;
3433
3434 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
3435 }
3436
fl_dump_key(struct sk_buff * skb,struct net * net,struct fl_flow_key * key,struct fl_flow_key * mask)3437 static int fl_dump_key(struct sk_buff *skb, struct net *net,
3438 struct fl_flow_key *key, struct fl_flow_key *mask)
3439 {
3440 if (mask->meta.ingress_ifindex) {
3441 struct net_device *dev;
3442
3443 dev = __dev_get_by_index(net, key->meta.ingress_ifindex);
3444 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
3445 goto nla_put_failure;
3446 }
3447
3448 if (fl_dump_key_val(skb, &key->meta.l2_miss,
3449 TCA_FLOWER_L2_MISS, &mask->meta.l2_miss,
3450 TCA_FLOWER_UNSPEC, sizeof(key->meta.l2_miss)))
3451 goto nla_put_failure;
3452
3453 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
3454 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
3455 sizeof(key->eth.dst)) ||
3456 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
3457 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
3458 sizeof(key->eth.src)) ||
3459 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
3460 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
3461 sizeof(key->basic.n_proto)))
3462 goto nla_put_failure;
3463
3464 if (mask->num_of_vlans.num_of_vlans) {
3465 if (nla_put_u8(skb, TCA_FLOWER_KEY_NUM_OF_VLANS, key->num_of_vlans.num_of_vlans))
3466 goto nla_put_failure;
3467 }
3468
3469 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
3470 goto nla_put_failure;
3471
3472 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
3473 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
3474 goto nla_put_failure;
3475
3476 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
3477 TCA_FLOWER_KEY_CVLAN_PRIO,
3478 &key->cvlan, &mask->cvlan) ||
3479 (mask->cvlan.vlan_tpid &&
3480 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
3481 key->cvlan.vlan_tpid)))
3482 goto nla_put_failure;
3483
3484 if (mask->basic.n_proto) {
3485 if (mask->cvlan.vlan_eth_type) {
3486 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
3487 key->basic.n_proto))
3488 goto nla_put_failure;
3489 } else if (mask->vlan.vlan_eth_type) {
3490 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
3491 key->vlan.vlan_eth_type))
3492 goto nla_put_failure;
3493 }
3494 }
3495
3496 if ((key->basic.n_proto == htons(ETH_P_IP) ||
3497 key->basic.n_proto == htons(ETH_P_IPV6)) &&
3498 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
3499 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
3500 sizeof(key->basic.ip_proto)) ||
3501 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
3502 goto nla_put_failure;
3503
3504 if (mask->pppoe.session_id) {
3505 if (nla_put_be16(skb, TCA_FLOWER_KEY_PPPOE_SID,
3506 key->pppoe.session_id))
3507 goto nla_put_failure;
3508 }
3509 if (mask->basic.n_proto && mask->pppoe.ppp_proto) {
3510 if (nla_put_be16(skb, TCA_FLOWER_KEY_PPP_PROTO,
3511 key->pppoe.ppp_proto))
3512 goto nla_put_failure;
3513 }
3514
3515 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
3516 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
3517 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
3518 sizeof(key->ipv4.src)) ||
3519 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
3520 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
3521 sizeof(key->ipv4.dst))))
3522 goto nla_put_failure;
3523 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
3524 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
3525 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
3526 sizeof(key->ipv6.src)) ||
3527 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
3528 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
3529 sizeof(key->ipv6.dst))))
3530 goto nla_put_failure;
3531
3532 if (key->basic.ip_proto == IPPROTO_TCP &&
3533 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
3534 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
3535 sizeof(key->tp.src)) ||
3536 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
3537 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
3538 sizeof(key->tp.dst)) ||
3539 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
3540 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
3541 sizeof(key->tcp.flags))))
3542 goto nla_put_failure;
3543 else if (key->basic.ip_proto == IPPROTO_UDP &&
3544 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
3545 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
3546 sizeof(key->tp.src)) ||
3547 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
3548 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
3549 sizeof(key->tp.dst))))
3550 goto nla_put_failure;
3551 else if (key->basic.ip_proto == IPPROTO_SCTP &&
3552 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
3553 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
3554 sizeof(key->tp.src)) ||
3555 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
3556 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
3557 sizeof(key->tp.dst))))
3558 goto nla_put_failure;
3559 else if (key->basic.n_proto == htons(ETH_P_IP) &&
3560 key->basic.ip_proto == IPPROTO_ICMP &&
3561 (fl_dump_key_val(skb, &key->icmp.type,
3562 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
3563 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
3564 sizeof(key->icmp.type)) ||
3565 fl_dump_key_val(skb, &key->icmp.code,
3566 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
3567 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
3568 sizeof(key->icmp.code))))
3569 goto nla_put_failure;
3570 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
3571 key->basic.ip_proto == IPPROTO_ICMPV6 &&
3572 (fl_dump_key_val(skb, &key->icmp.type,
3573 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
3574 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
3575 sizeof(key->icmp.type)) ||
3576 fl_dump_key_val(skb, &key->icmp.code,
3577 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
3578 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
3579 sizeof(key->icmp.code))))
3580 goto nla_put_failure;
3581 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
3582 key->basic.n_proto == htons(ETH_P_RARP)) &&
3583 (fl_dump_key_val(skb, &key->arp.sip,
3584 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
3585 TCA_FLOWER_KEY_ARP_SIP_MASK,
3586 sizeof(key->arp.sip)) ||
3587 fl_dump_key_val(skb, &key->arp.tip,
3588 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
3589 TCA_FLOWER_KEY_ARP_TIP_MASK,
3590 sizeof(key->arp.tip)) ||
3591 fl_dump_key_val(skb, &key->arp.op,
3592 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
3593 TCA_FLOWER_KEY_ARP_OP_MASK,
3594 sizeof(key->arp.op)) ||
3595 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
3596 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
3597 sizeof(key->arp.sha)) ||
3598 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
3599 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
3600 sizeof(key->arp.tha))))
3601 goto nla_put_failure;
3602 else if (key->basic.ip_proto == IPPROTO_L2TP &&
3603 fl_dump_key_val(skb, &key->l2tpv3.session_id,
3604 TCA_FLOWER_KEY_L2TPV3_SID,
3605 &mask->l2tpv3.session_id,
3606 TCA_FLOWER_UNSPEC,
3607 sizeof(key->l2tpv3.session_id)))
3608 goto nla_put_failure;
3609
3610 if (key->ipsec.spi &&
3611 fl_dump_key_val(skb, &key->ipsec.spi, TCA_FLOWER_KEY_SPI,
3612 &mask->ipsec.spi, TCA_FLOWER_KEY_SPI_MASK,
3613 sizeof(key->ipsec.spi)))
3614 goto nla_put_failure;
3615
3616 if ((key->basic.ip_proto == IPPROTO_TCP ||
3617 key->basic.ip_proto == IPPROTO_UDP ||
3618 key->basic.ip_proto == IPPROTO_SCTP) &&
3619 fl_dump_key_port_range(skb, key, mask))
3620 goto nla_put_failure;
3621
3622 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
3623 (fl_dump_key_val(skb, &key->enc_ipv4.src,
3624 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
3625 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
3626 sizeof(key->enc_ipv4.src)) ||
3627 fl_dump_key_val(skb, &key->enc_ipv4.dst,
3628 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
3629 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
3630 sizeof(key->enc_ipv4.dst))))
3631 goto nla_put_failure;
3632 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
3633 (fl_dump_key_val(skb, &key->enc_ipv6.src,
3634 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
3635 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
3636 sizeof(key->enc_ipv6.src)) ||
3637 fl_dump_key_val(skb, &key->enc_ipv6.dst,
3638 TCA_FLOWER_KEY_ENC_IPV6_DST,
3639 &mask->enc_ipv6.dst,
3640 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
3641 sizeof(key->enc_ipv6.dst))))
3642 goto nla_put_failure;
3643
3644 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
3645 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
3646 sizeof(key->enc_key_id)) ||
3647 fl_dump_key_val(skb, &key->enc_tp.src,
3648 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
3649 &mask->enc_tp.src,
3650 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
3651 sizeof(key->enc_tp.src)) ||
3652 fl_dump_key_val(skb, &key->enc_tp.dst,
3653 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
3654 &mask->enc_tp.dst,
3655 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
3656 sizeof(key->enc_tp.dst)) ||
3657 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
3658 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
3659 goto nla_put_failure;
3660
3661 if (fl_dump_key_ct(skb, &key->ct, &mask->ct))
3662 goto nla_put_failure;
3663
3664 if (fl_dump_key_flags(skb, false, key->control.flags,
3665 mask->control.flags))
3666 goto nla_put_failure;
3667
3668 if (fl_dump_key_val(skb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
3669 &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
3670 sizeof(key->hash.hash)))
3671 goto nla_put_failure;
3672
3673 if (fl_dump_key_cfm(skb, &key->cfm, &mask->cfm))
3674 goto nla_put_failure;
3675
3676 if (fl_dump_key_flags(skb, true, key->enc_control.flags,
3677 mask->enc_control.flags))
3678 goto nla_put_failure;
3679
3680 return 0;
3681
3682 nla_put_failure:
3683 return -EMSGSIZE;
3684 }
3685
fl_dump(struct net * net,struct tcf_proto * tp,void * fh,struct sk_buff * skb,struct tcmsg * t,bool rtnl_held)3686 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
3687 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
3688 {
3689 struct cls_fl_filter *f = fh;
3690 struct nlattr *nest;
3691 struct fl_flow_key *key, *mask;
3692 bool skip_hw;
3693
3694 if (!f)
3695 return skb->len;
3696
3697 t->tcm_handle = f->handle;
3698
3699 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3700 if (!nest)
3701 goto nla_put_failure;
3702
3703 spin_lock(&tp->lock);
3704
3705 if (f->res.classid &&
3706 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
3707 goto nla_put_failure_locked;
3708
3709 key = &f->key;
3710 mask = &f->mask->key;
3711 skip_hw = tc_skip_hw(f->flags);
3712
3713 if (fl_dump_key(skb, net, key, mask))
3714 goto nla_put_failure_locked;
3715
3716 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
3717 goto nla_put_failure_locked;
3718
3719 spin_unlock(&tp->lock);
3720
3721 if (!skip_hw)
3722 fl_hw_update_stats(tp, f, rtnl_held);
3723
3724 if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
3725 goto nla_put_failure;
3726
3727 if (tcf_exts_dump(skb, &f->exts))
3728 goto nla_put_failure;
3729
3730 nla_nest_end(skb, nest);
3731
3732 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
3733 goto nla_put_failure;
3734
3735 return skb->len;
3736
3737 nla_put_failure_locked:
3738 spin_unlock(&tp->lock);
3739 nla_put_failure:
3740 nla_nest_cancel(skb, nest);
3741 return -1;
3742 }
3743
fl_terse_dump(struct net * net,struct tcf_proto * tp,void * fh,struct sk_buff * skb,struct tcmsg * t,bool rtnl_held)3744 static int fl_terse_dump(struct net *net, struct tcf_proto *tp, void *fh,
3745 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
3746 {
3747 struct cls_fl_filter *f = fh;
3748 struct nlattr *nest;
3749 bool skip_hw;
3750
3751 if (!f)
3752 return skb->len;
3753
3754 t->tcm_handle = f->handle;
3755
3756 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3757 if (!nest)
3758 goto nla_put_failure;
3759
3760 spin_lock(&tp->lock);
3761
3762 skip_hw = tc_skip_hw(f->flags);
3763
3764 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
3765 goto nla_put_failure_locked;
3766
3767 spin_unlock(&tp->lock);
3768
3769 if (!skip_hw)
3770 fl_hw_update_stats(tp, f, rtnl_held);
3771
3772 if (tcf_exts_terse_dump(skb, &f->exts))
3773 goto nla_put_failure;
3774
3775 nla_nest_end(skb, nest);
3776
3777 return skb->len;
3778
3779 nla_put_failure_locked:
3780 spin_unlock(&tp->lock);
3781 nla_put_failure:
3782 nla_nest_cancel(skb, nest);
3783 return -1;
3784 }
3785
fl_tmplt_dump(struct sk_buff * skb,struct net * net,void * tmplt_priv)3786 static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
3787 {
3788 struct fl_flow_tmplt *tmplt = tmplt_priv;
3789 struct fl_flow_key *key, *mask;
3790 struct nlattr *nest;
3791
3792 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3793 if (!nest)
3794 goto nla_put_failure;
3795
3796 key = &tmplt->dummy_key;
3797 mask = &tmplt->mask;
3798
3799 if (fl_dump_key(skb, net, key, mask))
3800 goto nla_put_failure;
3801
3802 nla_nest_end(skb, nest);
3803
3804 return skb->len;
3805
3806 nla_put_failure:
3807 nla_nest_cancel(skb, nest);
3808 return -EMSGSIZE;
3809 }
3810
fl_bind_class(void * fh,u32 classid,unsigned long cl,void * q,unsigned long base)3811 static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
3812 unsigned long base)
3813 {
3814 struct cls_fl_filter *f = fh;
3815
3816 tc_cls_bind_class(classid, cl, q, &f->res, base);
3817 }
3818
fl_delete_empty(struct tcf_proto * tp)3819 static bool fl_delete_empty(struct tcf_proto *tp)
3820 {
3821 struct cls_fl_head *head = fl_head_dereference(tp);
3822
3823 spin_lock(&tp->lock);
3824 tp->deleting = idr_is_empty(&head->handle_idr);
3825 spin_unlock(&tp->lock);
3826
3827 return tp->deleting;
3828 }
3829
3830 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
3831 .kind = "flower",
3832 .classify = fl_classify,
3833 .init = fl_init,
3834 .destroy = fl_destroy,
3835 .get = fl_get,
3836 .put = fl_put,
3837 .change = fl_change,
3838 .delete = fl_delete,
3839 .delete_empty = fl_delete_empty,
3840 .walk = fl_walk,
3841 .reoffload = fl_reoffload,
3842 .hw_add = fl_hw_add,
3843 .hw_del = fl_hw_del,
3844 .dump = fl_dump,
3845 .terse_dump = fl_terse_dump,
3846 .bind_class = fl_bind_class,
3847 .tmplt_create = fl_tmplt_create,
3848 .tmplt_destroy = fl_tmplt_destroy,
3849 .tmplt_reoffload = fl_tmplt_reoffload,
3850 .tmplt_dump = fl_tmplt_dump,
3851 .get_exts = fl_get_exts,
3852 .owner = THIS_MODULE,
3853 .flags = TCF_PROTO_OPS_DOIT_UNLOCKED,
3854 };
3855 MODULE_ALIAS_NET_CLS("flower");
3856
cls_fl_init(void)3857 static int __init cls_fl_init(void)
3858 {
3859 return register_tcf_proto_ops(&cls_fl_ops);
3860 }
3861
cls_fl_exit(void)3862 static void __exit cls_fl_exit(void)
3863 {
3864 unregister_tcf_proto_ops(&cls_fl_ops);
3865 }
3866
3867 module_init(cls_fl_init);
3868 module_exit(cls_fl_exit);
3869
3870 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
3871 MODULE_DESCRIPTION("Flower classifier");
3872 MODULE_LICENSE("GPL v2");
3873