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