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