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