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