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