xref: /linux/net/sched/cls_flower.c (revision 4201c9260a8d3c4ef238e51692a7e9b4e1e29efe)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/cls_flower.c		Flower classifier
4  *
5  * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/rhashtable.h>
12 #include <linux/workqueue.h>
13 #include <linux/refcount.h>
14 
15 #include <linux/if_ether.h>
16 #include <linux/in6.h>
17 #include <linux/ip.h>
18 #include <linux/mpls.h>
19 
20 #include <net/sch_generic.h>
21 #include <net/pkt_cls.h>
22 #include <net/ip.h>
23 #include <net/flow_dissector.h>
24 #include <net/geneve.h>
25 
26 #include <net/dst.h>
27 #include <net/dst_metadata.h>
28 
29 struct fl_flow_key {
30 	int	indev_ifindex;
31 	struct flow_dissector_key_control control;
32 	struct flow_dissector_key_control enc_control;
33 	struct flow_dissector_key_basic basic;
34 	struct flow_dissector_key_eth_addrs eth;
35 	struct flow_dissector_key_vlan vlan;
36 	struct flow_dissector_key_vlan cvlan;
37 	union {
38 		struct flow_dissector_key_ipv4_addrs ipv4;
39 		struct flow_dissector_key_ipv6_addrs ipv6;
40 	};
41 	struct flow_dissector_key_ports tp;
42 	struct flow_dissector_key_icmp icmp;
43 	struct flow_dissector_key_arp arp;
44 	struct flow_dissector_key_keyid enc_key_id;
45 	union {
46 		struct flow_dissector_key_ipv4_addrs enc_ipv4;
47 		struct flow_dissector_key_ipv6_addrs enc_ipv6;
48 	};
49 	struct flow_dissector_key_ports enc_tp;
50 	struct flow_dissector_key_mpls mpls;
51 	struct flow_dissector_key_tcp tcp;
52 	struct flow_dissector_key_ip ip;
53 	struct flow_dissector_key_ip enc_ip;
54 	struct flow_dissector_key_enc_opts enc_opts;
55 	struct flow_dissector_key_ports tp_min;
56 	struct flow_dissector_key_ports tp_max;
57 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
58 
59 struct fl_flow_mask_range {
60 	unsigned short int start;
61 	unsigned short int end;
62 };
63 
64 struct fl_flow_mask {
65 	struct fl_flow_key key;
66 	struct fl_flow_mask_range range;
67 	u32 flags;
68 	struct rhash_head ht_node;
69 	struct rhashtable ht;
70 	struct rhashtable_params filter_ht_params;
71 	struct flow_dissector dissector;
72 	struct list_head filters;
73 	struct rcu_work rwork;
74 	struct list_head list;
75 	refcount_t refcnt;
76 };
77 
78 struct fl_flow_tmplt {
79 	struct fl_flow_key dummy_key;
80 	struct fl_flow_key mask;
81 	struct flow_dissector dissector;
82 	struct tcf_chain *chain;
83 };
84 
85 struct cls_fl_head {
86 	struct rhashtable ht;
87 	spinlock_t masks_lock; /* Protect masks list */
88 	struct list_head masks;
89 	struct list_head hw_filters;
90 	struct rcu_work rwork;
91 	struct idr handle_idr;
92 };
93 
94 struct cls_fl_filter {
95 	struct fl_flow_mask *mask;
96 	struct rhash_head ht_node;
97 	struct fl_flow_key mkey;
98 	struct tcf_exts exts;
99 	struct tcf_result res;
100 	struct fl_flow_key key;
101 	struct list_head list;
102 	struct list_head hw_list;
103 	u32 handle;
104 	u32 flags;
105 	u32 in_hw_count;
106 	struct rcu_work rwork;
107 	struct net_device *hw_dev;
108 	/* Flower classifier is unlocked, which means that its reference counter
109 	 * can be changed concurrently without any kind of external
110 	 * synchronization. Use atomic reference counter to be concurrency-safe.
111 	 */
112 	refcount_t refcnt;
113 	bool deleted;
114 };
115 
116 static const struct rhashtable_params mask_ht_params = {
117 	.key_offset = offsetof(struct fl_flow_mask, key),
118 	.key_len = sizeof(struct fl_flow_key),
119 	.head_offset = offsetof(struct fl_flow_mask, ht_node),
120 	.automatic_shrinking = true,
121 };
122 
123 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
124 {
125 	return mask->range.end - mask->range.start;
126 }
127 
128 static void fl_mask_update_range(struct fl_flow_mask *mask)
129 {
130 	const u8 *bytes = (const u8 *) &mask->key;
131 	size_t size = sizeof(mask->key);
132 	size_t i, first = 0, last;
133 
134 	for (i = 0; i < size; i++) {
135 		if (bytes[i]) {
136 			first = i;
137 			break;
138 		}
139 	}
140 	last = first;
141 	for (i = size - 1; i != first; i--) {
142 		if (bytes[i]) {
143 			last = i;
144 			break;
145 		}
146 	}
147 	mask->range.start = rounddown(first, sizeof(long));
148 	mask->range.end = roundup(last + 1, sizeof(long));
149 }
150 
151 static void *fl_key_get_start(struct fl_flow_key *key,
152 			      const struct fl_flow_mask *mask)
153 {
154 	return (u8 *) key + mask->range.start;
155 }
156 
157 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
158 			      struct fl_flow_mask *mask)
159 {
160 	const long *lkey = fl_key_get_start(key, mask);
161 	const long *lmask = fl_key_get_start(&mask->key, mask);
162 	long *lmkey = fl_key_get_start(mkey, mask);
163 	int i;
164 
165 	for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
166 		*lmkey++ = *lkey++ & *lmask++;
167 }
168 
169 static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
170 			       struct fl_flow_mask *mask)
171 {
172 	const long *lmask = fl_key_get_start(&mask->key, mask);
173 	const long *ltmplt;
174 	int i;
175 
176 	if (!tmplt)
177 		return true;
178 	ltmplt = fl_key_get_start(&tmplt->mask, mask);
179 	for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
180 		if (~*ltmplt++ & *lmask++)
181 			return false;
182 	}
183 	return true;
184 }
185 
186 static void fl_clear_masked_range(struct fl_flow_key *key,
187 				  struct fl_flow_mask *mask)
188 {
189 	memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
190 }
191 
192 static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter,
193 				  struct fl_flow_key *key,
194 				  struct fl_flow_key *mkey)
195 {
196 	__be16 min_mask, max_mask, min_val, max_val;
197 
198 	min_mask = htons(filter->mask->key.tp_min.dst);
199 	max_mask = htons(filter->mask->key.tp_max.dst);
200 	min_val = htons(filter->key.tp_min.dst);
201 	max_val = htons(filter->key.tp_max.dst);
202 
203 	if (min_mask && max_mask) {
204 		if (htons(key->tp.dst) < min_val ||
205 		    htons(key->tp.dst) > max_val)
206 			return false;
207 
208 		/* skb does not have min and max values */
209 		mkey->tp_min.dst = filter->mkey.tp_min.dst;
210 		mkey->tp_max.dst = filter->mkey.tp_max.dst;
211 	}
212 	return true;
213 }
214 
215 static bool fl_range_port_src_cmp(struct cls_fl_filter *filter,
216 				  struct fl_flow_key *key,
217 				  struct fl_flow_key *mkey)
218 {
219 	__be16 min_mask, max_mask, min_val, max_val;
220 
221 	min_mask = htons(filter->mask->key.tp_min.src);
222 	max_mask = htons(filter->mask->key.tp_max.src);
223 	min_val = htons(filter->key.tp_min.src);
224 	max_val = htons(filter->key.tp_max.src);
225 
226 	if (min_mask && max_mask) {
227 		if (htons(key->tp.src) < min_val ||
228 		    htons(key->tp.src) > max_val)
229 			return false;
230 
231 		/* skb does not have min and max values */
232 		mkey->tp_min.src = filter->mkey.tp_min.src;
233 		mkey->tp_max.src = filter->mkey.tp_max.src;
234 	}
235 	return true;
236 }
237 
238 static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask,
239 					 struct fl_flow_key *mkey)
240 {
241 	return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
242 				      mask->filter_ht_params);
243 }
244 
245 static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask,
246 					     struct fl_flow_key *mkey,
247 					     struct fl_flow_key *key)
248 {
249 	struct cls_fl_filter *filter, *f;
250 
251 	list_for_each_entry_rcu(filter, &mask->filters, list) {
252 		if (!fl_range_port_dst_cmp(filter, key, mkey))
253 			continue;
254 
255 		if (!fl_range_port_src_cmp(filter, key, mkey))
256 			continue;
257 
258 		f = __fl_lookup(mask, mkey);
259 		if (f)
260 			return f;
261 	}
262 	return NULL;
263 }
264 
265 static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
266 				       struct fl_flow_key *mkey,
267 				       struct fl_flow_key *key)
268 {
269 	if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE))
270 		return fl_lookup_range(mask, mkey, key);
271 
272 	return __fl_lookup(mask, mkey);
273 }
274 
275 static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
276 		       struct tcf_result *res)
277 {
278 	struct cls_fl_head *head = rcu_dereference_bh(tp->root);
279 	struct cls_fl_filter *f;
280 	struct fl_flow_mask *mask;
281 	struct fl_flow_key skb_key;
282 	struct fl_flow_key skb_mkey;
283 
284 	list_for_each_entry_rcu(mask, &head->masks, list) {
285 		fl_clear_masked_range(&skb_key, mask);
286 
287 		skb_key.indev_ifindex = skb->skb_iif;
288 		/* skb_flow_dissect() does not set n_proto in case an unknown
289 		 * protocol, so do it rather here.
290 		 */
291 		skb_key.basic.n_proto = skb->protocol;
292 		skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
293 		skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
294 
295 		fl_set_masked_key(&skb_mkey, &skb_key, mask);
296 
297 		f = fl_lookup(mask, &skb_mkey, &skb_key);
298 		if (f && !tc_skip_sw(f->flags)) {
299 			*res = f->res;
300 			return tcf_exts_exec(skb, &f->exts, res);
301 		}
302 	}
303 	return -1;
304 }
305 
306 static int fl_init(struct tcf_proto *tp)
307 {
308 	struct cls_fl_head *head;
309 
310 	head = kzalloc(sizeof(*head), GFP_KERNEL);
311 	if (!head)
312 		return -ENOBUFS;
313 
314 	spin_lock_init(&head->masks_lock);
315 	INIT_LIST_HEAD_RCU(&head->masks);
316 	INIT_LIST_HEAD(&head->hw_filters);
317 	rcu_assign_pointer(tp->root, head);
318 	idr_init(&head->handle_idr);
319 
320 	return rhashtable_init(&head->ht, &mask_ht_params);
321 }
322 
323 static void fl_mask_free(struct fl_flow_mask *mask)
324 {
325 	WARN_ON(!list_empty(&mask->filters));
326 	rhashtable_destroy(&mask->ht);
327 	kfree(mask);
328 }
329 
330 static void fl_mask_free_work(struct work_struct *work)
331 {
332 	struct fl_flow_mask *mask = container_of(to_rcu_work(work),
333 						 struct fl_flow_mask, rwork);
334 
335 	fl_mask_free(mask);
336 }
337 
338 static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask)
339 {
340 	if (!refcount_dec_and_test(&mask->refcnt))
341 		return false;
342 
343 	rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
344 
345 	spin_lock(&head->masks_lock);
346 	list_del_rcu(&mask->list);
347 	spin_unlock(&head->masks_lock);
348 
349 	tcf_queue_work(&mask->rwork, fl_mask_free_work);
350 
351 	return true;
352 }
353 
354 static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp)
355 {
356 	/* Flower classifier only changes root pointer during init and destroy.
357 	 * Users must obtain reference to tcf_proto instance before calling its
358 	 * API, so tp->root pointer is protected from concurrent call to
359 	 * fl_destroy() by reference counting.
360 	 */
361 	return rcu_dereference_raw(tp->root);
362 }
363 
364 static void __fl_destroy_filter(struct cls_fl_filter *f)
365 {
366 	tcf_exts_destroy(&f->exts);
367 	tcf_exts_put_net(&f->exts);
368 	kfree(f);
369 }
370 
371 static void fl_destroy_filter_work(struct work_struct *work)
372 {
373 	struct cls_fl_filter *f = container_of(to_rcu_work(work),
374 					struct cls_fl_filter, rwork);
375 
376 	__fl_destroy_filter(f);
377 }
378 
379 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
380 				 bool rtnl_held, struct netlink_ext_ack *extack)
381 {
382 	struct tc_cls_flower_offload cls_flower = {};
383 	struct tcf_block *block = tp->chain->block;
384 
385 	if (!rtnl_held)
386 		rtnl_lock();
387 
388 	tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
389 	cls_flower.command = TC_CLSFLOWER_DESTROY;
390 	cls_flower.cookie = (unsigned long) f;
391 
392 	tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
393 	spin_lock(&tp->lock);
394 	list_del_init(&f->hw_list);
395 	tcf_block_offload_dec(block, &f->flags);
396 	spin_unlock(&tp->lock);
397 
398 	if (!rtnl_held)
399 		rtnl_unlock();
400 }
401 
402 static int fl_hw_replace_filter(struct tcf_proto *tp,
403 				struct cls_fl_filter *f, bool rtnl_held,
404 				struct netlink_ext_ack *extack)
405 {
406 	struct cls_fl_head *head = fl_head_dereference(tp);
407 	struct tc_cls_flower_offload cls_flower = {};
408 	struct tcf_block *block = tp->chain->block;
409 	bool skip_sw = tc_skip_sw(f->flags);
410 	int err = 0;
411 
412 	if (!rtnl_held)
413 		rtnl_lock();
414 
415 	cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts));
416 	if (!cls_flower.rule) {
417 		err = -ENOMEM;
418 		goto errout;
419 	}
420 
421 	tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
422 	cls_flower.command = TC_CLSFLOWER_REPLACE;
423 	cls_flower.cookie = (unsigned long) f;
424 	cls_flower.rule->match.dissector = &f->mask->dissector;
425 	cls_flower.rule->match.mask = &f->mask->key;
426 	cls_flower.rule->match.key = &f->mkey;
427 	cls_flower.classid = f->res.classid;
428 
429 	err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
430 	if (err) {
431 		kfree(cls_flower.rule);
432 		if (skip_sw)
433 			NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
434 		else
435 			err = 0;
436 		goto errout;
437 	}
438 
439 	err = tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, skip_sw);
440 	kfree(cls_flower.rule);
441 
442 	if (err < 0) {
443 		fl_hw_destroy_filter(tp, f, true, NULL);
444 		goto errout;
445 	} else if (err > 0) {
446 		f->in_hw_count = err;
447 		err = 0;
448 		spin_lock(&tp->lock);
449 		tcf_block_offload_inc(block, &f->flags);
450 		spin_unlock(&tp->lock);
451 	}
452 
453 	if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW)) {
454 		err = -EINVAL;
455 		goto errout;
456 	}
457 
458 	spin_lock(&tp->lock);
459 	list_add(&f->hw_list, &head->hw_filters);
460 	spin_unlock(&tp->lock);
461 errout:
462 	if (!rtnl_held)
463 		rtnl_unlock();
464 
465 	return err;
466 }
467 
468 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f,
469 			       bool rtnl_held)
470 {
471 	struct tc_cls_flower_offload cls_flower = {};
472 	struct tcf_block *block = tp->chain->block;
473 
474 	if (!rtnl_held)
475 		rtnl_lock();
476 
477 	tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
478 	cls_flower.command = TC_CLSFLOWER_STATS;
479 	cls_flower.cookie = (unsigned long) f;
480 	cls_flower.classid = f->res.classid;
481 
482 	tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
483 
484 	tcf_exts_stats_update(&f->exts, cls_flower.stats.bytes,
485 			      cls_flower.stats.pkts,
486 			      cls_flower.stats.lastused);
487 
488 	if (!rtnl_held)
489 		rtnl_unlock();
490 }
491 
492 static void __fl_put(struct cls_fl_filter *f)
493 {
494 	if (!refcount_dec_and_test(&f->refcnt))
495 		return;
496 
497 	if (tcf_exts_get_net(&f->exts))
498 		tcf_queue_work(&f->rwork, fl_destroy_filter_work);
499 	else
500 		__fl_destroy_filter(f);
501 }
502 
503 static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle)
504 {
505 	struct cls_fl_filter *f;
506 
507 	rcu_read_lock();
508 	f = idr_find(&head->handle_idr, handle);
509 	if (f && !refcount_inc_not_zero(&f->refcnt))
510 		f = NULL;
511 	rcu_read_unlock();
512 
513 	return f;
514 }
515 
516 static struct cls_fl_filter *fl_get_next_filter(struct tcf_proto *tp,
517 						unsigned long *handle)
518 {
519 	struct cls_fl_head *head = fl_head_dereference(tp);
520 	struct cls_fl_filter *f;
521 
522 	rcu_read_lock();
523 	while ((f = idr_get_next_ul(&head->handle_idr, handle))) {
524 		/* don't return filters that are being deleted */
525 		if (refcount_inc_not_zero(&f->refcnt))
526 			break;
527 		++(*handle);
528 	}
529 	rcu_read_unlock();
530 
531 	return f;
532 }
533 
534 static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
535 		       bool *last, bool rtnl_held,
536 		       struct netlink_ext_ack *extack)
537 {
538 	struct cls_fl_head *head = fl_head_dereference(tp);
539 
540 	*last = false;
541 
542 	spin_lock(&tp->lock);
543 	if (f->deleted) {
544 		spin_unlock(&tp->lock);
545 		return -ENOENT;
546 	}
547 
548 	f->deleted = true;
549 	rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
550 			       f->mask->filter_ht_params);
551 	idr_remove(&head->handle_idr, f->handle);
552 	list_del_rcu(&f->list);
553 	spin_unlock(&tp->lock);
554 
555 	*last = fl_mask_put(head, f->mask);
556 	if (!tc_skip_hw(f->flags))
557 		fl_hw_destroy_filter(tp, f, rtnl_held, extack);
558 	tcf_unbind_filter(tp, &f->res);
559 	__fl_put(f);
560 
561 	return 0;
562 }
563 
564 static void fl_destroy_sleepable(struct work_struct *work)
565 {
566 	struct cls_fl_head *head = container_of(to_rcu_work(work),
567 						struct cls_fl_head,
568 						rwork);
569 
570 	rhashtable_destroy(&head->ht);
571 	kfree(head);
572 	module_put(THIS_MODULE);
573 }
574 
575 static void fl_destroy(struct tcf_proto *tp, bool rtnl_held,
576 		       struct netlink_ext_ack *extack)
577 {
578 	struct cls_fl_head *head = fl_head_dereference(tp);
579 	struct fl_flow_mask *mask, *next_mask;
580 	struct cls_fl_filter *f, *next;
581 	bool last;
582 
583 	list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
584 		list_for_each_entry_safe(f, next, &mask->filters, list) {
585 			__fl_delete(tp, f, &last, rtnl_held, extack);
586 			if (last)
587 				break;
588 		}
589 	}
590 	idr_destroy(&head->handle_idr);
591 
592 	__module_get(THIS_MODULE);
593 	tcf_queue_work(&head->rwork, fl_destroy_sleepable);
594 }
595 
596 static void fl_put(struct tcf_proto *tp, void *arg)
597 {
598 	struct cls_fl_filter *f = arg;
599 
600 	__fl_put(f);
601 }
602 
603 static void *fl_get(struct tcf_proto *tp, u32 handle)
604 {
605 	struct cls_fl_head *head = fl_head_dereference(tp);
606 
607 	return __fl_get(head, handle);
608 }
609 
610 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
611 	[TCA_FLOWER_UNSPEC]		= { .type = NLA_UNSPEC },
612 	[TCA_FLOWER_CLASSID]		= { .type = NLA_U32 },
613 	[TCA_FLOWER_INDEV]		= { .type = NLA_STRING,
614 					    .len = IFNAMSIZ },
615 	[TCA_FLOWER_KEY_ETH_DST]	= { .len = ETH_ALEN },
616 	[TCA_FLOWER_KEY_ETH_DST_MASK]	= { .len = ETH_ALEN },
617 	[TCA_FLOWER_KEY_ETH_SRC]	= { .len = ETH_ALEN },
618 	[TCA_FLOWER_KEY_ETH_SRC_MASK]	= { .len = ETH_ALEN },
619 	[TCA_FLOWER_KEY_ETH_TYPE]	= { .type = NLA_U16 },
620 	[TCA_FLOWER_KEY_IP_PROTO]	= { .type = NLA_U8 },
621 	[TCA_FLOWER_KEY_IPV4_SRC]	= { .type = NLA_U32 },
622 	[TCA_FLOWER_KEY_IPV4_SRC_MASK]	= { .type = NLA_U32 },
623 	[TCA_FLOWER_KEY_IPV4_DST]	= { .type = NLA_U32 },
624 	[TCA_FLOWER_KEY_IPV4_DST_MASK]	= { .type = NLA_U32 },
625 	[TCA_FLOWER_KEY_IPV6_SRC]	= { .len = sizeof(struct in6_addr) },
626 	[TCA_FLOWER_KEY_IPV6_SRC_MASK]	= { .len = sizeof(struct in6_addr) },
627 	[TCA_FLOWER_KEY_IPV6_DST]	= { .len = sizeof(struct in6_addr) },
628 	[TCA_FLOWER_KEY_IPV6_DST_MASK]	= { .len = sizeof(struct in6_addr) },
629 	[TCA_FLOWER_KEY_TCP_SRC]	= { .type = NLA_U16 },
630 	[TCA_FLOWER_KEY_TCP_DST]	= { .type = NLA_U16 },
631 	[TCA_FLOWER_KEY_UDP_SRC]	= { .type = NLA_U16 },
632 	[TCA_FLOWER_KEY_UDP_DST]	= { .type = NLA_U16 },
633 	[TCA_FLOWER_KEY_VLAN_ID]	= { .type = NLA_U16 },
634 	[TCA_FLOWER_KEY_VLAN_PRIO]	= { .type = NLA_U8 },
635 	[TCA_FLOWER_KEY_VLAN_ETH_TYPE]	= { .type = NLA_U16 },
636 	[TCA_FLOWER_KEY_ENC_KEY_ID]	= { .type = NLA_U32 },
637 	[TCA_FLOWER_KEY_ENC_IPV4_SRC]	= { .type = NLA_U32 },
638 	[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
639 	[TCA_FLOWER_KEY_ENC_IPV4_DST]	= { .type = NLA_U32 },
640 	[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
641 	[TCA_FLOWER_KEY_ENC_IPV6_SRC]	= { .len = sizeof(struct in6_addr) },
642 	[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
643 	[TCA_FLOWER_KEY_ENC_IPV6_DST]	= { .len = sizeof(struct in6_addr) },
644 	[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
645 	[TCA_FLOWER_KEY_TCP_SRC_MASK]	= { .type = NLA_U16 },
646 	[TCA_FLOWER_KEY_TCP_DST_MASK]	= { .type = NLA_U16 },
647 	[TCA_FLOWER_KEY_UDP_SRC_MASK]	= { .type = NLA_U16 },
648 	[TCA_FLOWER_KEY_UDP_DST_MASK]	= { .type = NLA_U16 },
649 	[TCA_FLOWER_KEY_SCTP_SRC_MASK]	= { .type = NLA_U16 },
650 	[TCA_FLOWER_KEY_SCTP_DST_MASK]	= { .type = NLA_U16 },
651 	[TCA_FLOWER_KEY_SCTP_SRC]	= { .type = NLA_U16 },
652 	[TCA_FLOWER_KEY_SCTP_DST]	= { .type = NLA_U16 },
653 	[TCA_FLOWER_KEY_ENC_UDP_SRC_PORT]	= { .type = NLA_U16 },
654 	[TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK]	= { .type = NLA_U16 },
655 	[TCA_FLOWER_KEY_ENC_UDP_DST_PORT]	= { .type = NLA_U16 },
656 	[TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK]	= { .type = NLA_U16 },
657 	[TCA_FLOWER_KEY_FLAGS]		= { .type = NLA_U32 },
658 	[TCA_FLOWER_KEY_FLAGS_MASK]	= { .type = NLA_U32 },
659 	[TCA_FLOWER_KEY_ICMPV4_TYPE]	= { .type = NLA_U8 },
660 	[TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
661 	[TCA_FLOWER_KEY_ICMPV4_CODE]	= { .type = NLA_U8 },
662 	[TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
663 	[TCA_FLOWER_KEY_ICMPV6_TYPE]	= { .type = NLA_U8 },
664 	[TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
665 	[TCA_FLOWER_KEY_ICMPV6_CODE]	= { .type = NLA_U8 },
666 	[TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
667 	[TCA_FLOWER_KEY_ARP_SIP]	= { .type = NLA_U32 },
668 	[TCA_FLOWER_KEY_ARP_SIP_MASK]	= { .type = NLA_U32 },
669 	[TCA_FLOWER_KEY_ARP_TIP]	= { .type = NLA_U32 },
670 	[TCA_FLOWER_KEY_ARP_TIP_MASK]	= { .type = NLA_U32 },
671 	[TCA_FLOWER_KEY_ARP_OP]		= { .type = NLA_U8 },
672 	[TCA_FLOWER_KEY_ARP_OP_MASK]	= { .type = NLA_U8 },
673 	[TCA_FLOWER_KEY_ARP_SHA]	= { .len = ETH_ALEN },
674 	[TCA_FLOWER_KEY_ARP_SHA_MASK]	= { .len = ETH_ALEN },
675 	[TCA_FLOWER_KEY_ARP_THA]	= { .len = ETH_ALEN },
676 	[TCA_FLOWER_KEY_ARP_THA_MASK]	= { .len = ETH_ALEN },
677 	[TCA_FLOWER_KEY_MPLS_TTL]	= { .type = NLA_U8 },
678 	[TCA_FLOWER_KEY_MPLS_BOS]	= { .type = NLA_U8 },
679 	[TCA_FLOWER_KEY_MPLS_TC]	= { .type = NLA_U8 },
680 	[TCA_FLOWER_KEY_MPLS_LABEL]	= { .type = NLA_U32 },
681 	[TCA_FLOWER_KEY_TCP_FLAGS]	= { .type = NLA_U16 },
682 	[TCA_FLOWER_KEY_TCP_FLAGS_MASK]	= { .type = NLA_U16 },
683 	[TCA_FLOWER_KEY_IP_TOS]		= { .type = NLA_U8 },
684 	[TCA_FLOWER_KEY_IP_TOS_MASK]	= { .type = NLA_U8 },
685 	[TCA_FLOWER_KEY_IP_TTL]		= { .type = NLA_U8 },
686 	[TCA_FLOWER_KEY_IP_TTL_MASK]	= { .type = NLA_U8 },
687 	[TCA_FLOWER_KEY_CVLAN_ID]	= { .type = NLA_U16 },
688 	[TCA_FLOWER_KEY_CVLAN_PRIO]	= { .type = NLA_U8 },
689 	[TCA_FLOWER_KEY_CVLAN_ETH_TYPE]	= { .type = NLA_U16 },
690 	[TCA_FLOWER_KEY_ENC_IP_TOS]	= { .type = NLA_U8 },
691 	[TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
692 	[TCA_FLOWER_KEY_ENC_IP_TTL]	 = { .type = NLA_U8 },
693 	[TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
694 	[TCA_FLOWER_KEY_ENC_OPTS]	= { .type = NLA_NESTED },
695 	[TCA_FLOWER_KEY_ENC_OPTS_MASK]	= { .type = NLA_NESTED },
696 };
697 
698 static const struct nla_policy
699 enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
700 	[TCA_FLOWER_KEY_ENC_OPTS_GENEVE]        = { .type = NLA_NESTED },
701 };
702 
703 static const struct nla_policy
704 geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
705 	[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]      = { .type = NLA_U16 },
706 	[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]       = { .type = NLA_U8 },
707 	[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]       = { .type = NLA_BINARY,
708 						       .len = 128 },
709 };
710 
711 static void fl_set_key_val(struct nlattr **tb,
712 			   void *val, int val_type,
713 			   void *mask, int mask_type, int len)
714 {
715 	if (!tb[val_type])
716 		return;
717 	memcpy(val, nla_data(tb[val_type]), len);
718 	if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
719 		memset(mask, 0xff, len);
720 	else
721 		memcpy(mask, nla_data(tb[mask_type]), len);
722 }
723 
724 static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
725 				 struct fl_flow_key *mask)
726 {
727 	fl_set_key_val(tb, &key->tp_min.dst,
728 		       TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_min.dst,
729 		       TCA_FLOWER_UNSPEC, sizeof(key->tp_min.dst));
730 	fl_set_key_val(tb, &key->tp_max.dst,
731 		       TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_max.dst,
732 		       TCA_FLOWER_UNSPEC, sizeof(key->tp_max.dst));
733 	fl_set_key_val(tb, &key->tp_min.src,
734 		       TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_min.src,
735 		       TCA_FLOWER_UNSPEC, sizeof(key->tp_min.src));
736 	fl_set_key_val(tb, &key->tp_max.src,
737 		       TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_max.src,
738 		       TCA_FLOWER_UNSPEC, sizeof(key->tp_max.src));
739 
740 	if ((mask->tp_min.dst && mask->tp_max.dst &&
741 	     htons(key->tp_max.dst) <= htons(key->tp_min.dst)) ||
742 	     (mask->tp_min.src && mask->tp_max.src &&
743 	      htons(key->tp_max.src) <= htons(key->tp_min.src)))
744 		return -EINVAL;
745 
746 	return 0;
747 }
748 
749 static int fl_set_key_mpls(struct nlattr **tb,
750 			   struct flow_dissector_key_mpls *key_val,
751 			   struct flow_dissector_key_mpls *key_mask)
752 {
753 	if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
754 		key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
755 		key_mask->mpls_ttl = MPLS_TTL_MASK;
756 	}
757 	if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
758 		u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
759 
760 		if (bos & ~MPLS_BOS_MASK)
761 			return -EINVAL;
762 		key_val->mpls_bos = bos;
763 		key_mask->mpls_bos = MPLS_BOS_MASK;
764 	}
765 	if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
766 		u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
767 
768 		if (tc & ~MPLS_TC_MASK)
769 			return -EINVAL;
770 		key_val->mpls_tc = tc;
771 		key_mask->mpls_tc = MPLS_TC_MASK;
772 	}
773 	if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
774 		u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
775 
776 		if (label & ~MPLS_LABEL_MASK)
777 			return -EINVAL;
778 		key_val->mpls_label = label;
779 		key_mask->mpls_label = MPLS_LABEL_MASK;
780 	}
781 	return 0;
782 }
783 
784 static void fl_set_key_vlan(struct nlattr **tb,
785 			    __be16 ethertype,
786 			    int vlan_id_key, int vlan_prio_key,
787 			    struct flow_dissector_key_vlan *key_val,
788 			    struct flow_dissector_key_vlan *key_mask)
789 {
790 #define VLAN_PRIORITY_MASK	0x7
791 
792 	if (tb[vlan_id_key]) {
793 		key_val->vlan_id =
794 			nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
795 		key_mask->vlan_id = VLAN_VID_MASK;
796 	}
797 	if (tb[vlan_prio_key]) {
798 		key_val->vlan_priority =
799 			nla_get_u8(tb[vlan_prio_key]) &
800 			VLAN_PRIORITY_MASK;
801 		key_mask->vlan_priority = VLAN_PRIORITY_MASK;
802 	}
803 	key_val->vlan_tpid = ethertype;
804 	key_mask->vlan_tpid = cpu_to_be16(~0);
805 }
806 
807 static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
808 			    u32 *dissector_key, u32 *dissector_mask,
809 			    u32 flower_flag_bit, u32 dissector_flag_bit)
810 {
811 	if (flower_mask & flower_flag_bit) {
812 		*dissector_mask |= dissector_flag_bit;
813 		if (flower_key & flower_flag_bit)
814 			*dissector_key |= dissector_flag_bit;
815 	}
816 }
817 
818 static int fl_set_key_flags(struct nlattr **tb,
819 			    u32 *flags_key, u32 *flags_mask)
820 {
821 	u32 key, mask;
822 
823 	/* mask is mandatory for flags */
824 	if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
825 		return -EINVAL;
826 
827 	key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
828 	mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
829 
830 	*flags_key  = 0;
831 	*flags_mask = 0;
832 
833 	fl_set_key_flag(key, mask, flags_key, flags_mask,
834 			TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
835 	fl_set_key_flag(key, mask, flags_key, flags_mask,
836 			TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
837 			FLOW_DIS_FIRST_FRAG);
838 
839 	return 0;
840 }
841 
842 static void fl_set_key_ip(struct nlattr **tb, bool encap,
843 			  struct flow_dissector_key_ip *key,
844 			  struct flow_dissector_key_ip *mask)
845 {
846 	int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
847 	int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
848 	int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
849 	int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
850 
851 	fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
852 	fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
853 }
854 
855 static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
856 			     int depth, int option_len,
857 			     struct netlink_ext_ack *extack)
858 {
859 	struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
860 	struct nlattr *class = NULL, *type = NULL, *data = NULL;
861 	struct geneve_opt *opt;
862 	int err, data_len = 0;
863 
864 	if (option_len > sizeof(struct geneve_opt))
865 		data_len = option_len - sizeof(struct geneve_opt);
866 
867 	opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
868 	memset(opt, 0xff, option_len);
869 	opt->length = data_len / 4;
870 	opt->r1 = 0;
871 	opt->r2 = 0;
872 	opt->r3 = 0;
873 
874 	/* If no mask has been prodived we assume an exact match. */
875 	if (!depth)
876 		return sizeof(struct geneve_opt) + data_len;
877 
878 	if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
879 		NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
880 		return -EINVAL;
881 	}
882 
883 	err = nla_parse_nested_deprecated(tb,
884 					  TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
885 					  nla, geneve_opt_policy, extack);
886 	if (err < 0)
887 		return err;
888 
889 	/* We are not allowed to omit any of CLASS, TYPE or DATA
890 	 * fields from the key.
891 	 */
892 	if (!option_len &&
893 	    (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
894 	     !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
895 	     !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
896 		NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
897 		return -EINVAL;
898 	}
899 
900 	/* Omitting any of CLASS, TYPE or DATA fields is allowed
901 	 * for the mask.
902 	 */
903 	if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
904 		int new_len = key->enc_opts.len;
905 
906 		data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
907 		data_len = nla_len(data);
908 		if (data_len < 4) {
909 			NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
910 			return -ERANGE;
911 		}
912 		if (data_len % 4) {
913 			NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
914 			return -ERANGE;
915 		}
916 
917 		new_len += sizeof(struct geneve_opt) + data_len;
918 		BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
919 		if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
920 			NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
921 			return -ERANGE;
922 		}
923 		opt->length = data_len / 4;
924 		memcpy(opt->opt_data, nla_data(data), data_len);
925 	}
926 
927 	if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
928 		class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
929 		opt->opt_class = nla_get_be16(class);
930 	}
931 
932 	if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
933 		type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
934 		opt->type = nla_get_u8(type);
935 	}
936 
937 	return sizeof(struct geneve_opt) + data_len;
938 }
939 
940 static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
941 			  struct fl_flow_key *mask,
942 			  struct netlink_ext_ack *extack)
943 {
944 	const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
945 	int err, option_len, key_depth, msk_depth = 0;
946 
947 	err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS],
948 					     TCA_FLOWER_KEY_ENC_OPTS_MAX,
949 					     enc_opts_policy, extack);
950 	if (err)
951 		return err;
952 
953 	nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
954 
955 	if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
956 		err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
957 						     TCA_FLOWER_KEY_ENC_OPTS_MAX,
958 						     enc_opts_policy, extack);
959 		if (err)
960 			return err;
961 
962 		nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
963 		msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
964 	}
965 
966 	nla_for_each_attr(nla_opt_key, nla_enc_key,
967 			  nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
968 		switch (nla_type(nla_opt_key)) {
969 		case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
970 			option_len = 0;
971 			key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
972 			option_len = fl_set_geneve_opt(nla_opt_key, key,
973 						       key_depth, option_len,
974 						       extack);
975 			if (option_len < 0)
976 				return option_len;
977 
978 			key->enc_opts.len += option_len;
979 			/* At the same time we need to parse through the mask
980 			 * in order to verify exact and mask attribute lengths.
981 			 */
982 			mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
983 			option_len = fl_set_geneve_opt(nla_opt_msk, mask,
984 						       msk_depth, option_len,
985 						       extack);
986 			if (option_len < 0)
987 				return option_len;
988 
989 			mask->enc_opts.len += option_len;
990 			if (key->enc_opts.len != mask->enc_opts.len) {
991 				NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
992 				return -EINVAL;
993 			}
994 
995 			if (msk_depth)
996 				nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
997 			break;
998 		default:
999 			NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
1000 			return -EINVAL;
1001 		}
1002 	}
1003 
1004 	return 0;
1005 }
1006 
1007 static int fl_set_key(struct net *net, struct nlattr **tb,
1008 		      struct fl_flow_key *key, struct fl_flow_key *mask,
1009 		      struct netlink_ext_ack *extack)
1010 {
1011 	__be16 ethertype;
1012 	int ret = 0;
1013 
1014 	if (tb[TCA_FLOWER_INDEV]) {
1015 		int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
1016 		if (err < 0)
1017 			return err;
1018 		key->indev_ifindex = err;
1019 		mask->indev_ifindex = 0xffffffff;
1020 	}
1021 
1022 	fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1023 		       mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1024 		       sizeof(key->eth.dst));
1025 	fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1026 		       mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1027 		       sizeof(key->eth.src));
1028 
1029 	if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
1030 		ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
1031 
1032 		if (eth_type_vlan(ethertype)) {
1033 			fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
1034 					TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan,
1035 					&mask->vlan);
1036 
1037 			if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
1038 				ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
1039 				if (eth_type_vlan(ethertype)) {
1040 					fl_set_key_vlan(tb, ethertype,
1041 							TCA_FLOWER_KEY_CVLAN_ID,
1042 							TCA_FLOWER_KEY_CVLAN_PRIO,
1043 							&key->cvlan, &mask->cvlan);
1044 					fl_set_key_val(tb, &key->basic.n_proto,
1045 						       TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1046 						       &mask->basic.n_proto,
1047 						       TCA_FLOWER_UNSPEC,
1048 						       sizeof(key->basic.n_proto));
1049 				} else {
1050 					key->basic.n_proto = ethertype;
1051 					mask->basic.n_proto = cpu_to_be16(~0);
1052 				}
1053 			}
1054 		} else {
1055 			key->basic.n_proto = ethertype;
1056 			mask->basic.n_proto = cpu_to_be16(~0);
1057 		}
1058 	}
1059 
1060 	if (key->basic.n_proto == htons(ETH_P_IP) ||
1061 	    key->basic.n_proto == htons(ETH_P_IPV6)) {
1062 		fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1063 			       &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1064 			       sizeof(key->basic.ip_proto));
1065 		fl_set_key_ip(tb, false, &key->ip, &mask->ip);
1066 	}
1067 
1068 	if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
1069 		key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1070 		mask->control.addr_type = ~0;
1071 		fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1072 			       &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1073 			       sizeof(key->ipv4.src));
1074 		fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1075 			       &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1076 			       sizeof(key->ipv4.dst));
1077 	} else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
1078 		key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1079 		mask->control.addr_type = ~0;
1080 		fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1081 			       &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1082 			       sizeof(key->ipv6.src));
1083 		fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1084 			       &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1085 			       sizeof(key->ipv6.dst));
1086 	}
1087 
1088 	if (key->basic.ip_proto == IPPROTO_TCP) {
1089 		fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1090 			       &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1091 			       sizeof(key->tp.src));
1092 		fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1093 			       &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1094 			       sizeof(key->tp.dst));
1095 		fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1096 			       &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1097 			       sizeof(key->tcp.flags));
1098 	} else if (key->basic.ip_proto == IPPROTO_UDP) {
1099 		fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1100 			       &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1101 			       sizeof(key->tp.src));
1102 		fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1103 			       &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1104 			       sizeof(key->tp.dst));
1105 	} else if (key->basic.ip_proto == IPPROTO_SCTP) {
1106 		fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1107 			       &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1108 			       sizeof(key->tp.src));
1109 		fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1110 			       &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1111 			       sizeof(key->tp.dst));
1112 	} else if (key->basic.n_proto == htons(ETH_P_IP) &&
1113 		   key->basic.ip_proto == IPPROTO_ICMP) {
1114 		fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
1115 			       &mask->icmp.type,
1116 			       TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1117 			       sizeof(key->icmp.type));
1118 		fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
1119 			       &mask->icmp.code,
1120 			       TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1121 			       sizeof(key->icmp.code));
1122 	} else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1123 		   key->basic.ip_proto == IPPROTO_ICMPV6) {
1124 		fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
1125 			       &mask->icmp.type,
1126 			       TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1127 			       sizeof(key->icmp.type));
1128 		fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
1129 			       &mask->icmp.code,
1130 			       TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1131 			       sizeof(key->icmp.code));
1132 	} else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
1133 		   key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
1134 		ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
1135 		if (ret)
1136 			return ret;
1137 	} else if (key->basic.n_proto == htons(ETH_P_ARP) ||
1138 		   key->basic.n_proto == htons(ETH_P_RARP)) {
1139 		fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
1140 			       &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
1141 			       sizeof(key->arp.sip));
1142 		fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
1143 			       &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
1144 			       sizeof(key->arp.tip));
1145 		fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
1146 			       &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
1147 			       sizeof(key->arp.op));
1148 		fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1149 			       mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1150 			       sizeof(key->arp.sha));
1151 		fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1152 			       mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1153 			       sizeof(key->arp.tha));
1154 	}
1155 
1156 	if (key->basic.ip_proto == IPPROTO_TCP ||
1157 	    key->basic.ip_proto == IPPROTO_UDP ||
1158 	    key->basic.ip_proto == IPPROTO_SCTP) {
1159 		ret = fl_set_key_port_range(tb, key, mask);
1160 		if (ret)
1161 			return ret;
1162 	}
1163 
1164 	if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
1165 	    tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
1166 		key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
1167 		mask->enc_control.addr_type = ~0;
1168 		fl_set_key_val(tb, &key->enc_ipv4.src,
1169 			       TCA_FLOWER_KEY_ENC_IPV4_SRC,
1170 			       &mask->enc_ipv4.src,
1171 			       TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1172 			       sizeof(key->enc_ipv4.src));
1173 		fl_set_key_val(tb, &key->enc_ipv4.dst,
1174 			       TCA_FLOWER_KEY_ENC_IPV4_DST,
1175 			       &mask->enc_ipv4.dst,
1176 			       TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1177 			       sizeof(key->enc_ipv4.dst));
1178 	}
1179 
1180 	if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
1181 	    tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
1182 		key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
1183 		mask->enc_control.addr_type = ~0;
1184 		fl_set_key_val(tb, &key->enc_ipv6.src,
1185 			       TCA_FLOWER_KEY_ENC_IPV6_SRC,
1186 			       &mask->enc_ipv6.src,
1187 			       TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1188 			       sizeof(key->enc_ipv6.src));
1189 		fl_set_key_val(tb, &key->enc_ipv6.dst,
1190 			       TCA_FLOWER_KEY_ENC_IPV6_DST,
1191 			       &mask->enc_ipv6.dst,
1192 			       TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1193 			       sizeof(key->enc_ipv6.dst));
1194 	}
1195 
1196 	fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
1197 		       &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
1198 		       sizeof(key->enc_key_id.keyid));
1199 
1200 	fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1201 		       &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1202 		       sizeof(key->enc_tp.src));
1203 
1204 	fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1205 		       &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1206 		       sizeof(key->enc_tp.dst));
1207 
1208 	fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
1209 
1210 	if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
1211 		ret = fl_set_enc_opt(tb, key, mask, extack);
1212 		if (ret)
1213 			return ret;
1214 	}
1215 
1216 	if (tb[TCA_FLOWER_KEY_FLAGS])
1217 		ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
1218 
1219 	return ret;
1220 }
1221 
1222 static void fl_mask_copy(struct fl_flow_mask *dst,
1223 			 struct fl_flow_mask *src)
1224 {
1225 	const void *psrc = fl_key_get_start(&src->key, src);
1226 	void *pdst = fl_key_get_start(&dst->key, src);
1227 
1228 	memcpy(pdst, psrc, fl_mask_range(src));
1229 	dst->range = src->range;
1230 }
1231 
1232 static const struct rhashtable_params fl_ht_params = {
1233 	.key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
1234 	.head_offset = offsetof(struct cls_fl_filter, ht_node),
1235 	.automatic_shrinking = true,
1236 };
1237 
1238 static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
1239 {
1240 	mask->filter_ht_params = fl_ht_params;
1241 	mask->filter_ht_params.key_len = fl_mask_range(mask);
1242 	mask->filter_ht_params.key_offset += mask->range.start;
1243 
1244 	return rhashtable_init(&mask->ht, &mask->filter_ht_params);
1245 }
1246 
1247 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
1248 #define FL_KEY_MEMBER_SIZE(member) FIELD_SIZEOF(struct fl_flow_key, member)
1249 
1250 #define FL_KEY_IS_MASKED(mask, member)						\
1251 	memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member),		\
1252 		   0, FL_KEY_MEMBER_SIZE(member))				\
1253 
1254 #define FL_KEY_SET(keys, cnt, id, member)					\
1255 	do {									\
1256 		keys[cnt].key_id = id;						\
1257 		keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member);		\
1258 		cnt++;								\
1259 	} while(0);
1260 
1261 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member)			\
1262 	do {									\
1263 		if (FL_KEY_IS_MASKED(mask, member))				\
1264 			FL_KEY_SET(keys, cnt, id, member);			\
1265 	} while(0);
1266 
1267 static void fl_init_dissector(struct flow_dissector *dissector,
1268 			      struct fl_flow_key *mask)
1269 {
1270 	struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
1271 	size_t cnt = 0;
1272 
1273 	FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
1274 	FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
1275 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1276 			     FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
1277 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1278 			     FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
1279 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1280 			     FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
1281 	if (FL_KEY_IS_MASKED(mask, tp) ||
1282 	    FL_KEY_IS_MASKED(mask, tp_min) || FL_KEY_IS_MASKED(mask, tp_max))
1283 		FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_PORTS, tp);
1284 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1285 			     FLOW_DISSECTOR_KEY_IP, ip);
1286 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1287 			     FLOW_DISSECTOR_KEY_TCP, tcp);
1288 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1289 			     FLOW_DISSECTOR_KEY_ICMP, icmp);
1290 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1291 			     FLOW_DISSECTOR_KEY_ARP, arp);
1292 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1293 			     FLOW_DISSECTOR_KEY_MPLS, mpls);
1294 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1295 			     FLOW_DISSECTOR_KEY_VLAN, vlan);
1296 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1297 			     FLOW_DISSECTOR_KEY_CVLAN, cvlan);
1298 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1299 			     FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
1300 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1301 			     FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
1302 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1303 			     FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
1304 	if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
1305 	    FL_KEY_IS_MASKED(mask, enc_ipv6))
1306 		FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
1307 			   enc_control);
1308 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1309 			     FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
1310 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1311 			     FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
1312 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1313 			     FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
1314 
1315 	skb_flow_dissector_init(dissector, keys, cnt);
1316 }
1317 
1318 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
1319 					       struct fl_flow_mask *mask)
1320 {
1321 	struct fl_flow_mask *newmask;
1322 	int err;
1323 
1324 	newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
1325 	if (!newmask)
1326 		return ERR_PTR(-ENOMEM);
1327 
1328 	fl_mask_copy(newmask, mask);
1329 
1330 	if ((newmask->key.tp_min.dst && newmask->key.tp_max.dst) ||
1331 	    (newmask->key.tp_min.src && newmask->key.tp_max.src))
1332 		newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
1333 
1334 	err = fl_init_mask_hashtable(newmask);
1335 	if (err)
1336 		goto errout_free;
1337 
1338 	fl_init_dissector(&newmask->dissector, &newmask->key);
1339 
1340 	INIT_LIST_HEAD_RCU(&newmask->filters);
1341 
1342 	refcount_set(&newmask->refcnt, 1);
1343 	err = rhashtable_replace_fast(&head->ht, &mask->ht_node,
1344 				      &newmask->ht_node, mask_ht_params);
1345 	if (err)
1346 		goto errout_destroy;
1347 
1348 	/* Wait until any potential concurrent users of mask are finished */
1349 	synchronize_rcu();
1350 
1351 	spin_lock(&head->masks_lock);
1352 	list_add_tail_rcu(&newmask->list, &head->masks);
1353 	spin_unlock(&head->masks_lock);
1354 
1355 	return newmask;
1356 
1357 errout_destroy:
1358 	rhashtable_destroy(&newmask->ht);
1359 errout_free:
1360 	kfree(newmask);
1361 
1362 	return ERR_PTR(err);
1363 }
1364 
1365 static int fl_check_assign_mask(struct cls_fl_head *head,
1366 				struct cls_fl_filter *fnew,
1367 				struct cls_fl_filter *fold,
1368 				struct fl_flow_mask *mask)
1369 {
1370 	struct fl_flow_mask *newmask;
1371 	int ret = 0;
1372 
1373 	rcu_read_lock();
1374 
1375 	/* Insert mask as temporary node to prevent concurrent creation of mask
1376 	 * with same key. Any concurrent lookups with same key will return
1377 	 * -EAGAIN because mask's refcnt is zero. It is safe to insert
1378 	 * stack-allocated 'mask' to masks hash table because we call
1379 	 * synchronize_rcu() before returning from this function (either in case
1380 	 * of error or after replacing it with heap-allocated mask in
1381 	 * fl_create_new_mask()).
1382 	 */
1383 	fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht,
1384 						       &mask->ht_node,
1385 						       mask_ht_params);
1386 	if (!fnew->mask) {
1387 		rcu_read_unlock();
1388 
1389 		if (fold) {
1390 			ret = -EINVAL;
1391 			goto errout_cleanup;
1392 		}
1393 
1394 		newmask = fl_create_new_mask(head, mask);
1395 		if (IS_ERR(newmask)) {
1396 			ret = PTR_ERR(newmask);
1397 			goto errout_cleanup;
1398 		}
1399 
1400 		fnew->mask = newmask;
1401 		return 0;
1402 	} else if (IS_ERR(fnew->mask)) {
1403 		ret = PTR_ERR(fnew->mask);
1404 	} else if (fold && fold->mask != fnew->mask) {
1405 		ret = -EINVAL;
1406 	} else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) {
1407 		/* Mask was deleted concurrently, try again */
1408 		ret = -EAGAIN;
1409 	}
1410 	rcu_read_unlock();
1411 	return ret;
1412 
1413 errout_cleanup:
1414 	rhashtable_remove_fast(&head->ht, &mask->ht_node,
1415 			       mask_ht_params);
1416 	/* Wait until any potential concurrent users of mask are finished */
1417 	synchronize_rcu();
1418 	return ret;
1419 }
1420 
1421 static int fl_set_parms(struct net *net, struct tcf_proto *tp,
1422 			struct cls_fl_filter *f, struct fl_flow_mask *mask,
1423 			unsigned long base, struct nlattr **tb,
1424 			struct nlattr *est, bool ovr,
1425 			struct fl_flow_tmplt *tmplt, bool rtnl_held,
1426 			struct netlink_ext_ack *extack)
1427 {
1428 	int err;
1429 
1430 	err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, rtnl_held,
1431 				extack);
1432 	if (err < 0)
1433 		return err;
1434 
1435 	if (tb[TCA_FLOWER_CLASSID]) {
1436 		f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
1437 		if (!rtnl_held)
1438 			rtnl_lock();
1439 		tcf_bind_filter(tp, &f->res, base);
1440 		if (!rtnl_held)
1441 			rtnl_unlock();
1442 	}
1443 
1444 	err = fl_set_key(net, tb, &f->key, &mask->key, extack);
1445 	if (err)
1446 		return err;
1447 
1448 	fl_mask_update_range(mask);
1449 	fl_set_masked_key(&f->mkey, &f->key, mask);
1450 
1451 	if (!fl_mask_fits_tmplt(tmplt, mask)) {
1452 		NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
1453 		return -EINVAL;
1454 	}
1455 
1456 	return 0;
1457 }
1458 
1459 static int fl_ht_insert_unique(struct cls_fl_filter *fnew,
1460 			       struct cls_fl_filter *fold,
1461 			       bool *in_ht)
1462 {
1463 	struct fl_flow_mask *mask = fnew->mask;
1464 	int err;
1465 
1466 	err = rhashtable_lookup_insert_fast(&mask->ht,
1467 					    &fnew->ht_node,
1468 					    mask->filter_ht_params);
1469 	if (err) {
1470 		*in_ht = false;
1471 		/* It is okay if filter with same key exists when
1472 		 * overwriting.
1473 		 */
1474 		return fold && err == -EEXIST ? 0 : err;
1475 	}
1476 
1477 	*in_ht = true;
1478 	return 0;
1479 }
1480 
1481 static int fl_change(struct net *net, struct sk_buff *in_skb,
1482 		     struct tcf_proto *tp, unsigned long base,
1483 		     u32 handle, struct nlattr **tca,
1484 		     void **arg, bool ovr, bool rtnl_held,
1485 		     struct netlink_ext_ack *extack)
1486 {
1487 	struct cls_fl_head *head = fl_head_dereference(tp);
1488 	struct cls_fl_filter *fold = *arg;
1489 	struct cls_fl_filter *fnew;
1490 	struct fl_flow_mask *mask;
1491 	struct nlattr **tb;
1492 	bool in_ht;
1493 	int err;
1494 
1495 	if (!tca[TCA_OPTIONS]) {
1496 		err = -EINVAL;
1497 		goto errout_fold;
1498 	}
1499 
1500 	mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
1501 	if (!mask) {
1502 		err = -ENOBUFS;
1503 		goto errout_fold;
1504 	}
1505 
1506 	tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1507 	if (!tb) {
1508 		err = -ENOBUFS;
1509 		goto errout_mask_alloc;
1510 	}
1511 
1512 	err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
1513 					  tca[TCA_OPTIONS], fl_policy, NULL);
1514 	if (err < 0)
1515 		goto errout_tb;
1516 
1517 	if (fold && handle && fold->handle != handle) {
1518 		err = -EINVAL;
1519 		goto errout_tb;
1520 	}
1521 
1522 	fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
1523 	if (!fnew) {
1524 		err = -ENOBUFS;
1525 		goto errout_tb;
1526 	}
1527 	INIT_LIST_HEAD(&fnew->hw_list);
1528 	refcount_set(&fnew->refcnt, 1);
1529 
1530 	err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0);
1531 	if (err < 0)
1532 		goto errout;
1533 
1534 	if (tb[TCA_FLOWER_FLAGS]) {
1535 		fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
1536 
1537 		if (!tc_flags_valid(fnew->flags)) {
1538 			err = -EINVAL;
1539 			goto errout;
1540 		}
1541 	}
1542 
1543 	err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr,
1544 			   tp->chain->tmplt_priv, rtnl_held, extack);
1545 	if (err)
1546 		goto errout;
1547 
1548 	err = fl_check_assign_mask(head, fnew, fold, mask);
1549 	if (err)
1550 		goto errout;
1551 
1552 	err = fl_ht_insert_unique(fnew, fold, &in_ht);
1553 	if (err)
1554 		goto errout_mask;
1555 
1556 	if (!tc_skip_hw(fnew->flags)) {
1557 		err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack);
1558 		if (err)
1559 			goto errout_ht;
1560 	}
1561 
1562 	if (!tc_in_hw(fnew->flags))
1563 		fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1564 
1565 	spin_lock(&tp->lock);
1566 
1567 	/* tp was deleted concurrently. -EAGAIN will cause caller to lookup
1568 	 * proto again or create new one, if necessary.
1569 	 */
1570 	if (tp->deleting) {
1571 		err = -EAGAIN;
1572 		goto errout_hw;
1573 	}
1574 
1575 	if (fold) {
1576 		/* Fold filter was deleted concurrently. Retry lookup. */
1577 		if (fold->deleted) {
1578 			err = -EAGAIN;
1579 			goto errout_hw;
1580 		}
1581 
1582 		fnew->handle = handle;
1583 
1584 		if (!in_ht) {
1585 			struct rhashtable_params params =
1586 				fnew->mask->filter_ht_params;
1587 
1588 			err = rhashtable_insert_fast(&fnew->mask->ht,
1589 						     &fnew->ht_node,
1590 						     params);
1591 			if (err)
1592 				goto errout_hw;
1593 			in_ht = true;
1594 		}
1595 
1596 		refcount_inc(&fnew->refcnt);
1597 		rhashtable_remove_fast(&fold->mask->ht,
1598 				       &fold->ht_node,
1599 				       fold->mask->filter_ht_params);
1600 		idr_replace(&head->handle_idr, fnew, fnew->handle);
1601 		list_replace_rcu(&fold->list, &fnew->list);
1602 		fold->deleted = true;
1603 
1604 		spin_unlock(&tp->lock);
1605 
1606 		fl_mask_put(head, fold->mask);
1607 		if (!tc_skip_hw(fold->flags))
1608 			fl_hw_destroy_filter(tp, fold, rtnl_held, NULL);
1609 		tcf_unbind_filter(tp, &fold->res);
1610 		/* Caller holds reference to fold, so refcnt is always > 0
1611 		 * after this.
1612 		 */
1613 		refcount_dec(&fold->refcnt);
1614 		__fl_put(fold);
1615 	} else {
1616 		if (handle) {
1617 			/* user specifies a handle and it doesn't exist */
1618 			err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1619 					    handle, GFP_ATOMIC);
1620 
1621 			/* Filter with specified handle was concurrently
1622 			 * inserted after initial check in cls_api. This is not
1623 			 * necessarily an error if NLM_F_EXCL is not set in
1624 			 * message flags. Returning EAGAIN will cause cls_api to
1625 			 * try to update concurrently inserted rule.
1626 			 */
1627 			if (err == -ENOSPC)
1628 				err = -EAGAIN;
1629 		} else {
1630 			handle = 1;
1631 			err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1632 					    INT_MAX, GFP_ATOMIC);
1633 		}
1634 		if (err)
1635 			goto errout_hw;
1636 
1637 		refcount_inc(&fnew->refcnt);
1638 		fnew->handle = handle;
1639 		list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
1640 		spin_unlock(&tp->lock);
1641 	}
1642 
1643 	*arg = fnew;
1644 
1645 	kfree(tb);
1646 	kfree(mask);
1647 	return 0;
1648 
1649 errout_ht:
1650 	spin_lock(&tp->lock);
1651 errout_hw:
1652 	fnew->deleted = true;
1653 	spin_unlock(&tp->lock);
1654 	if (!tc_skip_hw(fnew->flags))
1655 		fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL);
1656 	if (in_ht)
1657 		rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node,
1658 				       fnew->mask->filter_ht_params);
1659 errout_mask:
1660 	fl_mask_put(head, fnew->mask);
1661 errout:
1662 	__fl_put(fnew);
1663 errout_tb:
1664 	kfree(tb);
1665 errout_mask_alloc:
1666 	kfree(mask);
1667 errout_fold:
1668 	if (fold)
1669 		__fl_put(fold);
1670 	return err;
1671 }
1672 
1673 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
1674 		     bool rtnl_held, struct netlink_ext_ack *extack)
1675 {
1676 	struct cls_fl_head *head = fl_head_dereference(tp);
1677 	struct cls_fl_filter *f = arg;
1678 	bool last_on_mask;
1679 	int err = 0;
1680 
1681 	err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack);
1682 	*last = list_empty(&head->masks);
1683 	__fl_put(f);
1684 
1685 	return err;
1686 }
1687 
1688 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg,
1689 		    bool rtnl_held)
1690 {
1691 	struct cls_fl_filter *f;
1692 
1693 	arg->count = arg->skip;
1694 
1695 	while ((f = fl_get_next_filter(tp, &arg->cookie)) != NULL) {
1696 		if (arg->fn(tp, f, arg) < 0) {
1697 			__fl_put(f);
1698 			arg->stop = 1;
1699 			break;
1700 		}
1701 		__fl_put(f);
1702 		arg->cookie++;
1703 		arg->count++;
1704 	}
1705 }
1706 
1707 static struct cls_fl_filter *
1708 fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add)
1709 {
1710 	struct cls_fl_head *head = fl_head_dereference(tp);
1711 
1712 	spin_lock(&tp->lock);
1713 	if (list_empty(&head->hw_filters)) {
1714 		spin_unlock(&tp->lock);
1715 		return NULL;
1716 	}
1717 
1718 	if (!f)
1719 		f = list_entry(&head->hw_filters, struct cls_fl_filter,
1720 			       hw_list);
1721 	list_for_each_entry_continue(f, &head->hw_filters, hw_list) {
1722 		if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) {
1723 			spin_unlock(&tp->lock);
1724 			return f;
1725 		}
1726 	}
1727 
1728 	spin_unlock(&tp->lock);
1729 	return NULL;
1730 }
1731 
1732 static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1733 			void *cb_priv, struct netlink_ext_ack *extack)
1734 {
1735 	struct tc_cls_flower_offload cls_flower = {};
1736 	struct tcf_block *block = tp->chain->block;
1737 	struct cls_fl_filter *f = NULL;
1738 	int err;
1739 
1740 	/* hw_filters list can only be changed by hw offload functions after
1741 	 * obtaining rtnl lock. Make sure it is not changed while reoffload is
1742 	 * iterating it.
1743 	 */
1744 	ASSERT_RTNL();
1745 
1746 	while ((f = fl_get_next_hw_filter(tp, f, add))) {
1747 		cls_flower.rule =
1748 			flow_rule_alloc(tcf_exts_num_actions(&f->exts));
1749 		if (!cls_flower.rule) {
1750 			__fl_put(f);
1751 			return -ENOMEM;
1752 		}
1753 
1754 		tc_cls_common_offload_init(&cls_flower.common, tp, f->flags,
1755 					   extack);
1756 		cls_flower.command = add ?
1757 			TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY;
1758 		cls_flower.cookie = (unsigned long)f;
1759 		cls_flower.rule->match.dissector = &f->mask->dissector;
1760 		cls_flower.rule->match.mask = &f->mask->key;
1761 		cls_flower.rule->match.key = &f->mkey;
1762 
1763 		err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
1764 		if (err) {
1765 			kfree(cls_flower.rule);
1766 			if (tc_skip_sw(f->flags)) {
1767 				NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
1768 				__fl_put(f);
1769 				return err;
1770 			}
1771 			goto next_flow;
1772 		}
1773 
1774 		cls_flower.classid = f->res.classid;
1775 
1776 		err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
1777 		kfree(cls_flower.rule);
1778 
1779 		if (err) {
1780 			if (add && tc_skip_sw(f->flags)) {
1781 				__fl_put(f);
1782 				return err;
1783 			}
1784 			goto next_flow;
1785 		}
1786 
1787 		spin_lock(&tp->lock);
1788 		tc_cls_offload_cnt_update(block, &f->in_hw_count, &f->flags,
1789 					  add);
1790 		spin_unlock(&tp->lock);
1791 next_flow:
1792 		__fl_put(f);
1793 	}
1794 
1795 	return 0;
1796 }
1797 
1798 static int fl_hw_create_tmplt(struct tcf_chain *chain,
1799 			      struct fl_flow_tmplt *tmplt)
1800 {
1801 	struct tc_cls_flower_offload cls_flower = {};
1802 	struct tcf_block *block = chain->block;
1803 
1804 	cls_flower.rule = flow_rule_alloc(0);
1805 	if (!cls_flower.rule)
1806 		return -ENOMEM;
1807 
1808 	cls_flower.common.chain_index = chain->index;
1809 	cls_flower.command = TC_CLSFLOWER_TMPLT_CREATE;
1810 	cls_flower.cookie = (unsigned long) tmplt;
1811 	cls_flower.rule->match.dissector = &tmplt->dissector;
1812 	cls_flower.rule->match.mask = &tmplt->mask;
1813 	cls_flower.rule->match.key = &tmplt->dummy_key;
1814 
1815 	/* We don't care if driver (any of them) fails to handle this
1816 	 * call. It serves just as a hint for it.
1817 	 */
1818 	tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
1819 	kfree(cls_flower.rule);
1820 
1821 	return 0;
1822 }
1823 
1824 static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
1825 				struct fl_flow_tmplt *tmplt)
1826 {
1827 	struct tc_cls_flower_offload cls_flower = {};
1828 	struct tcf_block *block = chain->block;
1829 
1830 	cls_flower.common.chain_index = chain->index;
1831 	cls_flower.command = TC_CLSFLOWER_TMPLT_DESTROY;
1832 	cls_flower.cookie = (unsigned long) tmplt;
1833 
1834 	tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
1835 }
1836 
1837 static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
1838 			     struct nlattr **tca,
1839 			     struct netlink_ext_ack *extack)
1840 {
1841 	struct fl_flow_tmplt *tmplt;
1842 	struct nlattr **tb;
1843 	int err;
1844 
1845 	if (!tca[TCA_OPTIONS])
1846 		return ERR_PTR(-EINVAL);
1847 
1848 	tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1849 	if (!tb)
1850 		return ERR_PTR(-ENOBUFS);
1851 	err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
1852 					  tca[TCA_OPTIONS], fl_policy, NULL);
1853 	if (err)
1854 		goto errout_tb;
1855 
1856 	tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
1857 	if (!tmplt) {
1858 		err = -ENOMEM;
1859 		goto errout_tb;
1860 	}
1861 	tmplt->chain = chain;
1862 	err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
1863 	if (err)
1864 		goto errout_tmplt;
1865 
1866 	fl_init_dissector(&tmplt->dissector, &tmplt->mask);
1867 
1868 	err = fl_hw_create_tmplt(chain, tmplt);
1869 	if (err)
1870 		goto errout_tmplt;
1871 
1872 	kfree(tb);
1873 	return tmplt;
1874 
1875 errout_tmplt:
1876 	kfree(tmplt);
1877 errout_tb:
1878 	kfree(tb);
1879 	return ERR_PTR(err);
1880 }
1881 
1882 static void fl_tmplt_destroy(void *tmplt_priv)
1883 {
1884 	struct fl_flow_tmplt *tmplt = tmplt_priv;
1885 
1886 	fl_hw_destroy_tmplt(tmplt->chain, tmplt);
1887 	kfree(tmplt);
1888 }
1889 
1890 static int fl_dump_key_val(struct sk_buff *skb,
1891 			   void *val, int val_type,
1892 			   void *mask, int mask_type, int len)
1893 {
1894 	int err;
1895 
1896 	if (!memchr_inv(mask, 0, len))
1897 		return 0;
1898 	err = nla_put(skb, val_type, len, val);
1899 	if (err)
1900 		return err;
1901 	if (mask_type != TCA_FLOWER_UNSPEC) {
1902 		err = nla_put(skb, mask_type, len, mask);
1903 		if (err)
1904 			return err;
1905 	}
1906 	return 0;
1907 }
1908 
1909 static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
1910 				  struct fl_flow_key *mask)
1911 {
1912 	if (fl_dump_key_val(skb, &key->tp_min.dst, TCA_FLOWER_KEY_PORT_DST_MIN,
1913 			    &mask->tp_min.dst, TCA_FLOWER_UNSPEC,
1914 			    sizeof(key->tp_min.dst)) ||
1915 	    fl_dump_key_val(skb, &key->tp_max.dst, TCA_FLOWER_KEY_PORT_DST_MAX,
1916 			    &mask->tp_max.dst, TCA_FLOWER_UNSPEC,
1917 			    sizeof(key->tp_max.dst)) ||
1918 	    fl_dump_key_val(skb, &key->tp_min.src, TCA_FLOWER_KEY_PORT_SRC_MIN,
1919 			    &mask->tp_min.src, TCA_FLOWER_UNSPEC,
1920 			    sizeof(key->tp_min.src)) ||
1921 	    fl_dump_key_val(skb, &key->tp_max.src, TCA_FLOWER_KEY_PORT_SRC_MAX,
1922 			    &mask->tp_max.src, TCA_FLOWER_UNSPEC,
1923 			    sizeof(key->tp_max.src)))
1924 		return -1;
1925 
1926 	return 0;
1927 }
1928 
1929 static int fl_dump_key_mpls(struct sk_buff *skb,
1930 			    struct flow_dissector_key_mpls *mpls_key,
1931 			    struct flow_dissector_key_mpls *mpls_mask)
1932 {
1933 	int err;
1934 
1935 	if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1936 		return 0;
1937 	if (mpls_mask->mpls_ttl) {
1938 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1939 				 mpls_key->mpls_ttl);
1940 		if (err)
1941 			return err;
1942 	}
1943 	if (mpls_mask->mpls_tc) {
1944 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1945 				 mpls_key->mpls_tc);
1946 		if (err)
1947 			return err;
1948 	}
1949 	if (mpls_mask->mpls_label) {
1950 		err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1951 				  mpls_key->mpls_label);
1952 		if (err)
1953 			return err;
1954 	}
1955 	if (mpls_mask->mpls_bos) {
1956 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1957 				 mpls_key->mpls_bos);
1958 		if (err)
1959 			return err;
1960 	}
1961 	return 0;
1962 }
1963 
1964 static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
1965 			  struct flow_dissector_key_ip *key,
1966 			  struct flow_dissector_key_ip *mask)
1967 {
1968 	int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1969 	int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1970 	int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1971 	int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
1972 
1973 	if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
1974 	    fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
1975 		return -1;
1976 
1977 	return 0;
1978 }
1979 
1980 static int fl_dump_key_vlan(struct sk_buff *skb,
1981 			    int vlan_id_key, int vlan_prio_key,
1982 			    struct flow_dissector_key_vlan *vlan_key,
1983 			    struct flow_dissector_key_vlan *vlan_mask)
1984 {
1985 	int err;
1986 
1987 	if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1988 		return 0;
1989 	if (vlan_mask->vlan_id) {
1990 		err = nla_put_u16(skb, vlan_id_key,
1991 				  vlan_key->vlan_id);
1992 		if (err)
1993 			return err;
1994 	}
1995 	if (vlan_mask->vlan_priority) {
1996 		err = nla_put_u8(skb, vlan_prio_key,
1997 				 vlan_key->vlan_priority);
1998 		if (err)
1999 			return err;
2000 	}
2001 	return 0;
2002 }
2003 
2004 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
2005 			    u32 *flower_key, u32 *flower_mask,
2006 			    u32 flower_flag_bit, u32 dissector_flag_bit)
2007 {
2008 	if (dissector_mask & dissector_flag_bit) {
2009 		*flower_mask |= flower_flag_bit;
2010 		if (dissector_key & dissector_flag_bit)
2011 			*flower_key |= flower_flag_bit;
2012 	}
2013 }
2014 
2015 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
2016 {
2017 	u32 key, mask;
2018 	__be32 _key, _mask;
2019 	int err;
2020 
2021 	if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
2022 		return 0;
2023 
2024 	key = 0;
2025 	mask = 0;
2026 
2027 	fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2028 			TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
2029 	fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2030 			TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
2031 			FLOW_DIS_FIRST_FRAG);
2032 
2033 	_key = cpu_to_be32(key);
2034 	_mask = cpu_to_be32(mask);
2035 
2036 	err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
2037 	if (err)
2038 		return err;
2039 
2040 	return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
2041 }
2042 
2043 static int fl_dump_key_geneve_opt(struct sk_buff *skb,
2044 				  struct flow_dissector_key_enc_opts *enc_opts)
2045 {
2046 	struct geneve_opt *opt;
2047 	struct nlattr *nest;
2048 	int opt_off = 0;
2049 
2050 	nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
2051 	if (!nest)
2052 		goto nla_put_failure;
2053 
2054 	while (enc_opts->len > opt_off) {
2055 		opt = (struct geneve_opt *)&enc_opts->data[opt_off];
2056 
2057 		if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
2058 				 opt->opt_class))
2059 			goto nla_put_failure;
2060 		if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
2061 			       opt->type))
2062 			goto nla_put_failure;
2063 		if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
2064 			    opt->length * 4, opt->opt_data))
2065 			goto nla_put_failure;
2066 
2067 		opt_off += sizeof(struct geneve_opt) + opt->length * 4;
2068 	}
2069 	nla_nest_end(skb, nest);
2070 	return 0;
2071 
2072 nla_put_failure:
2073 	nla_nest_cancel(skb, nest);
2074 	return -EMSGSIZE;
2075 }
2076 
2077 static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
2078 			       struct flow_dissector_key_enc_opts *enc_opts)
2079 {
2080 	struct nlattr *nest;
2081 	int err;
2082 
2083 	if (!enc_opts->len)
2084 		return 0;
2085 
2086 	nest = nla_nest_start_noflag(skb, enc_opt_type);
2087 	if (!nest)
2088 		goto nla_put_failure;
2089 
2090 	switch (enc_opts->dst_opt_type) {
2091 	case TUNNEL_GENEVE_OPT:
2092 		err = fl_dump_key_geneve_opt(skb, enc_opts);
2093 		if (err)
2094 			goto nla_put_failure;
2095 		break;
2096 	default:
2097 		goto nla_put_failure;
2098 	}
2099 	nla_nest_end(skb, nest);
2100 	return 0;
2101 
2102 nla_put_failure:
2103 	nla_nest_cancel(skb, nest);
2104 	return -EMSGSIZE;
2105 }
2106 
2107 static int fl_dump_key_enc_opt(struct sk_buff *skb,
2108 			       struct flow_dissector_key_enc_opts *key_opts,
2109 			       struct flow_dissector_key_enc_opts *msk_opts)
2110 {
2111 	int err;
2112 
2113 	err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
2114 	if (err)
2115 		return err;
2116 
2117 	return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
2118 }
2119 
2120 static int fl_dump_key(struct sk_buff *skb, struct net *net,
2121 		       struct fl_flow_key *key, struct fl_flow_key *mask)
2122 {
2123 	if (mask->indev_ifindex) {
2124 		struct net_device *dev;
2125 
2126 		dev = __dev_get_by_index(net, key->indev_ifindex);
2127 		if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
2128 			goto nla_put_failure;
2129 	}
2130 
2131 	if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
2132 			    mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
2133 			    sizeof(key->eth.dst)) ||
2134 	    fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
2135 			    mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
2136 			    sizeof(key->eth.src)) ||
2137 	    fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
2138 			    &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
2139 			    sizeof(key->basic.n_proto)))
2140 		goto nla_put_failure;
2141 
2142 	if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
2143 		goto nla_put_failure;
2144 
2145 	if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
2146 			     TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
2147 		goto nla_put_failure;
2148 
2149 	if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
2150 			     TCA_FLOWER_KEY_CVLAN_PRIO,
2151 			     &key->cvlan, &mask->cvlan) ||
2152 	    (mask->cvlan.vlan_tpid &&
2153 	     nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
2154 			  key->cvlan.vlan_tpid)))
2155 		goto nla_put_failure;
2156 
2157 	if (mask->basic.n_proto) {
2158 		if (mask->cvlan.vlan_tpid) {
2159 			if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
2160 					 key->basic.n_proto))
2161 				goto nla_put_failure;
2162 		} else if (mask->vlan.vlan_tpid) {
2163 			if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
2164 					 key->basic.n_proto))
2165 				goto nla_put_failure;
2166 		}
2167 	}
2168 
2169 	if ((key->basic.n_proto == htons(ETH_P_IP) ||
2170 	     key->basic.n_proto == htons(ETH_P_IPV6)) &&
2171 	    (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
2172 			    &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
2173 			    sizeof(key->basic.ip_proto)) ||
2174 	    fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
2175 		goto nla_put_failure;
2176 
2177 	if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
2178 	    (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
2179 			     &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
2180 			     sizeof(key->ipv4.src)) ||
2181 	     fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
2182 			     &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
2183 			     sizeof(key->ipv4.dst))))
2184 		goto nla_put_failure;
2185 	else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
2186 		 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
2187 				  &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
2188 				  sizeof(key->ipv6.src)) ||
2189 		  fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
2190 				  &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
2191 				  sizeof(key->ipv6.dst))))
2192 		goto nla_put_failure;
2193 
2194 	if (key->basic.ip_proto == IPPROTO_TCP &&
2195 	    (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
2196 			     &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
2197 			     sizeof(key->tp.src)) ||
2198 	     fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
2199 			     &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
2200 			     sizeof(key->tp.dst)) ||
2201 	     fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
2202 			     &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
2203 			     sizeof(key->tcp.flags))))
2204 		goto nla_put_failure;
2205 	else if (key->basic.ip_proto == IPPROTO_UDP &&
2206 		 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
2207 				  &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
2208 				  sizeof(key->tp.src)) ||
2209 		  fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
2210 				  &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
2211 				  sizeof(key->tp.dst))))
2212 		goto nla_put_failure;
2213 	else if (key->basic.ip_proto == IPPROTO_SCTP &&
2214 		 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
2215 				  &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
2216 				  sizeof(key->tp.src)) ||
2217 		  fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
2218 				  &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
2219 				  sizeof(key->tp.dst))))
2220 		goto nla_put_failure;
2221 	else if (key->basic.n_proto == htons(ETH_P_IP) &&
2222 		 key->basic.ip_proto == IPPROTO_ICMP &&
2223 		 (fl_dump_key_val(skb, &key->icmp.type,
2224 				  TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
2225 				  TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
2226 				  sizeof(key->icmp.type)) ||
2227 		  fl_dump_key_val(skb, &key->icmp.code,
2228 				  TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
2229 				  TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
2230 				  sizeof(key->icmp.code))))
2231 		goto nla_put_failure;
2232 	else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
2233 		 key->basic.ip_proto == IPPROTO_ICMPV6 &&
2234 		 (fl_dump_key_val(skb, &key->icmp.type,
2235 				  TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
2236 				  TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
2237 				  sizeof(key->icmp.type)) ||
2238 		  fl_dump_key_val(skb, &key->icmp.code,
2239 				  TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
2240 				  TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
2241 				  sizeof(key->icmp.code))))
2242 		goto nla_put_failure;
2243 	else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
2244 		  key->basic.n_proto == htons(ETH_P_RARP)) &&
2245 		 (fl_dump_key_val(skb, &key->arp.sip,
2246 				  TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
2247 				  TCA_FLOWER_KEY_ARP_SIP_MASK,
2248 				  sizeof(key->arp.sip)) ||
2249 		  fl_dump_key_val(skb, &key->arp.tip,
2250 				  TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
2251 				  TCA_FLOWER_KEY_ARP_TIP_MASK,
2252 				  sizeof(key->arp.tip)) ||
2253 		  fl_dump_key_val(skb, &key->arp.op,
2254 				  TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
2255 				  TCA_FLOWER_KEY_ARP_OP_MASK,
2256 				  sizeof(key->arp.op)) ||
2257 		  fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
2258 				  mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
2259 				  sizeof(key->arp.sha)) ||
2260 		  fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
2261 				  mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
2262 				  sizeof(key->arp.tha))))
2263 		goto nla_put_failure;
2264 
2265 	if ((key->basic.ip_proto == IPPROTO_TCP ||
2266 	     key->basic.ip_proto == IPPROTO_UDP ||
2267 	     key->basic.ip_proto == IPPROTO_SCTP) &&
2268 	     fl_dump_key_port_range(skb, key, mask))
2269 		goto nla_put_failure;
2270 
2271 	if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
2272 	    (fl_dump_key_val(skb, &key->enc_ipv4.src,
2273 			    TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
2274 			    TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
2275 			    sizeof(key->enc_ipv4.src)) ||
2276 	     fl_dump_key_val(skb, &key->enc_ipv4.dst,
2277 			     TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
2278 			     TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
2279 			     sizeof(key->enc_ipv4.dst))))
2280 		goto nla_put_failure;
2281 	else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
2282 		 (fl_dump_key_val(skb, &key->enc_ipv6.src,
2283 			    TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
2284 			    TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
2285 			    sizeof(key->enc_ipv6.src)) ||
2286 		 fl_dump_key_val(skb, &key->enc_ipv6.dst,
2287 				 TCA_FLOWER_KEY_ENC_IPV6_DST,
2288 				 &mask->enc_ipv6.dst,
2289 				 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
2290 			    sizeof(key->enc_ipv6.dst))))
2291 		goto nla_put_failure;
2292 
2293 	if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
2294 			    &mask->enc_key_id, TCA_FLOWER_UNSPEC,
2295 			    sizeof(key->enc_key_id)) ||
2296 	    fl_dump_key_val(skb, &key->enc_tp.src,
2297 			    TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
2298 			    &mask->enc_tp.src,
2299 			    TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
2300 			    sizeof(key->enc_tp.src)) ||
2301 	    fl_dump_key_val(skb, &key->enc_tp.dst,
2302 			    TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
2303 			    &mask->enc_tp.dst,
2304 			    TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
2305 			    sizeof(key->enc_tp.dst)) ||
2306 	    fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
2307 	    fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
2308 		goto nla_put_failure;
2309 
2310 	if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
2311 		goto nla_put_failure;
2312 
2313 	return 0;
2314 
2315 nla_put_failure:
2316 	return -EMSGSIZE;
2317 }
2318 
2319 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
2320 		   struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
2321 {
2322 	struct cls_fl_filter *f = fh;
2323 	struct nlattr *nest;
2324 	struct fl_flow_key *key, *mask;
2325 	bool skip_hw;
2326 
2327 	if (!f)
2328 		return skb->len;
2329 
2330 	t->tcm_handle = f->handle;
2331 
2332 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
2333 	if (!nest)
2334 		goto nla_put_failure;
2335 
2336 	spin_lock(&tp->lock);
2337 
2338 	if (f->res.classid &&
2339 	    nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
2340 		goto nla_put_failure_locked;
2341 
2342 	key = &f->key;
2343 	mask = &f->mask->key;
2344 	skip_hw = tc_skip_hw(f->flags);
2345 
2346 	if (fl_dump_key(skb, net, key, mask))
2347 		goto nla_put_failure_locked;
2348 
2349 	if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
2350 		goto nla_put_failure_locked;
2351 
2352 	spin_unlock(&tp->lock);
2353 
2354 	if (!skip_hw)
2355 		fl_hw_update_stats(tp, f, rtnl_held);
2356 
2357 	if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
2358 		goto nla_put_failure;
2359 
2360 	if (tcf_exts_dump(skb, &f->exts))
2361 		goto nla_put_failure;
2362 
2363 	nla_nest_end(skb, nest);
2364 
2365 	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
2366 		goto nla_put_failure;
2367 
2368 	return skb->len;
2369 
2370 nla_put_failure_locked:
2371 	spin_unlock(&tp->lock);
2372 nla_put_failure:
2373 	nla_nest_cancel(skb, nest);
2374 	return -1;
2375 }
2376 
2377 static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
2378 {
2379 	struct fl_flow_tmplt *tmplt = tmplt_priv;
2380 	struct fl_flow_key *key, *mask;
2381 	struct nlattr *nest;
2382 
2383 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
2384 	if (!nest)
2385 		goto nla_put_failure;
2386 
2387 	key = &tmplt->dummy_key;
2388 	mask = &tmplt->mask;
2389 
2390 	if (fl_dump_key(skb, net, key, mask))
2391 		goto nla_put_failure;
2392 
2393 	nla_nest_end(skb, nest);
2394 
2395 	return skb->len;
2396 
2397 nla_put_failure:
2398 	nla_nest_cancel(skb, nest);
2399 	return -EMSGSIZE;
2400 }
2401 
2402 static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
2403 {
2404 	struct cls_fl_filter *f = fh;
2405 
2406 	if (f && f->res.classid == classid)
2407 		f->res.class = cl;
2408 }
2409 
2410 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
2411 	.kind		= "flower",
2412 	.classify	= fl_classify,
2413 	.init		= fl_init,
2414 	.destroy	= fl_destroy,
2415 	.get		= fl_get,
2416 	.put		= fl_put,
2417 	.change		= fl_change,
2418 	.delete		= fl_delete,
2419 	.walk		= fl_walk,
2420 	.reoffload	= fl_reoffload,
2421 	.dump		= fl_dump,
2422 	.bind_class	= fl_bind_class,
2423 	.tmplt_create	= fl_tmplt_create,
2424 	.tmplt_destroy	= fl_tmplt_destroy,
2425 	.tmplt_dump	= fl_tmplt_dump,
2426 	.owner		= THIS_MODULE,
2427 	.flags		= TCF_PROTO_OPS_DOIT_UNLOCKED,
2428 };
2429 
2430 static int __init cls_fl_init(void)
2431 {
2432 	return register_tcf_proto_ops(&cls_fl_ops);
2433 }
2434 
2435 static void __exit cls_fl_exit(void)
2436 {
2437 	unregister_tcf_proto_ops(&cls_fl_ops);
2438 }
2439 
2440 module_init(cls_fl_init);
2441 module_exit(cls_fl_exit);
2442 
2443 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
2444 MODULE_DESCRIPTION("Flower classifier");
2445 MODULE_LICENSE("GPL v2");
2446