xref: /openbmc/linux/net/sched/cls_flower.c (revision f220d3eb)
1 /*
2  * net/sched/cls_flower.c		Flower classifier
3  *
4  * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/rhashtable.h>
16 #include <linux/workqueue.h>
17 
18 #include <linux/if_ether.h>
19 #include <linux/in6.h>
20 #include <linux/ip.h>
21 #include <linux/mpls.h>
22 
23 #include <net/sch_generic.h>
24 #include <net/pkt_cls.h>
25 #include <net/ip.h>
26 #include <net/flow_dissector.h>
27 #include <net/geneve.h>
28 
29 #include <net/dst.h>
30 #include <net/dst_metadata.h>
31 
32 struct fl_flow_key {
33 	int	indev_ifindex;
34 	struct flow_dissector_key_control control;
35 	struct flow_dissector_key_control enc_control;
36 	struct flow_dissector_key_basic basic;
37 	struct flow_dissector_key_eth_addrs eth;
38 	struct flow_dissector_key_vlan vlan;
39 	struct flow_dissector_key_vlan cvlan;
40 	union {
41 		struct flow_dissector_key_ipv4_addrs ipv4;
42 		struct flow_dissector_key_ipv6_addrs ipv6;
43 	};
44 	struct flow_dissector_key_ports tp;
45 	struct flow_dissector_key_icmp icmp;
46 	struct flow_dissector_key_arp arp;
47 	struct flow_dissector_key_keyid enc_key_id;
48 	union {
49 		struct flow_dissector_key_ipv4_addrs enc_ipv4;
50 		struct flow_dissector_key_ipv6_addrs enc_ipv6;
51 	};
52 	struct flow_dissector_key_ports enc_tp;
53 	struct flow_dissector_key_mpls mpls;
54 	struct flow_dissector_key_tcp tcp;
55 	struct flow_dissector_key_ip ip;
56 	struct flow_dissector_key_ip enc_ip;
57 	struct flow_dissector_key_enc_opts enc_opts;
58 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
59 
60 struct fl_flow_mask_range {
61 	unsigned short int start;
62 	unsigned short int end;
63 };
64 
65 struct fl_flow_mask {
66 	struct fl_flow_key key;
67 	struct fl_flow_mask_range range;
68 	struct rhash_head ht_node;
69 	struct rhashtable ht;
70 	struct rhashtable_params filter_ht_params;
71 	struct flow_dissector dissector;
72 	struct list_head filters;
73 	struct rcu_work rwork;
74 	struct list_head list;
75 };
76 
77 struct fl_flow_tmplt {
78 	struct fl_flow_key dummy_key;
79 	struct fl_flow_key mask;
80 	struct flow_dissector dissector;
81 	struct tcf_chain *chain;
82 };
83 
84 struct cls_fl_head {
85 	struct rhashtable ht;
86 	struct list_head masks;
87 	struct rcu_work rwork;
88 	struct idr handle_idr;
89 };
90 
91 struct cls_fl_filter {
92 	struct fl_flow_mask *mask;
93 	struct rhash_head ht_node;
94 	struct fl_flow_key mkey;
95 	struct tcf_exts exts;
96 	struct tcf_result res;
97 	struct fl_flow_key key;
98 	struct list_head list;
99 	u32 handle;
100 	u32 flags;
101 	unsigned int in_hw_count;
102 	struct rcu_work rwork;
103 	struct net_device *hw_dev;
104 };
105 
106 static const struct rhashtable_params mask_ht_params = {
107 	.key_offset = offsetof(struct fl_flow_mask, key),
108 	.key_len = sizeof(struct fl_flow_key),
109 	.head_offset = offsetof(struct fl_flow_mask, ht_node),
110 	.automatic_shrinking = true,
111 };
112 
113 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
114 {
115 	return mask->range.end - mask->range.start;
116 }
117 
118 static void fl_mask_update_range(struct fl_flow_mask *mask)
119 {
120 	const u8 *bytes = (const u8 *) &mask->key;
121 	size_t size = sizeof(mask->key);
122 	size_t i, first = 0, last;
123 
124 	for (i = 0; i < size; i++) {
125 		if (bytes[i]) {
126 			first = i;
127 			break;
128 		}
129 	}
130 	last = first;
131 	for (i = size - 1; i != first; i--) {
132 		if (bytes[i]) {
133 			last = i;
134 			break;
135 		}
136 	}
137 	mask->range.start = rounddown(first, sizeof(long));
138 	mask->range.end = roundup(last + 1, sizeof(long));
139 }
140 
141 static void *fl_key_get_start(struct fl_flow_key *key,
142 			      const struct fl_flow_mask *mask)
143 {
144 	return (u8 *) key + mask->range.start;
145 }
146 
147 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
148 			      struct fl_flow_mask *mask)
149 {
150 	const long *lkey = fl_key_get_start(key, mask);
151 	const long *lmask = fl_key_get_start(&mask->key, mask);
152 	long *lmkey = fl_key_get_start(mkey, mask);
153 	int i;
154 
155 	for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
156 		*lmkey++ = *lkey++ & *lmask++;
157 }
158 
159 static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
160 			       struct fl_flow_mask *mask)
161 {
162 	const long *lmask = fl_key_get_start(&mask->key, mask);
163 	const long *ltmplt;
164 	int i;
165 
166 	if (!tmplt)
167 		return true;
168 	ltmplt = fl_key_get_start(&tmplt->mask, mask);
169 	for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
170 		if (~*ltmplt++ & *lmask++)
171 			return false;
172 	}
173 	return true;
174 }
175 
176 static void fl_clear_masked_range(struct fl_flow_key *key,
177 				  struct fl_flow_mask *mask)
178 {
179 	memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
180 }
181 
182 static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
183 				       struct fl_flow_key *mkey)
184 {
185 	return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
186 				      mask->filter_ht_params);
187 }
188 
189 static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
190 		       struct tcf_result *res)
191 {
192 	struct cls_fl_head *head = rcu_dereference_bh(tp->root);
193 	struct cls_fl_filter *f;
194 	struct fl_flow_mask *mask;
195 	struct fl_flow_key skb_key;
196 	struct fl_flow_key skb_mkey;
197 
198 	list_for_each_entry_rcu(mask, &head->masks, list) {
199 		fl_clear_masked_range(&skb_key, mask);
200 
201 		skb_key.indev_ifindex = skb->skb_iif;
202 		/* skb_flow_dissect() does not set n_proto in case an unknown
203 		 * protocol, so do it rather here.
204 		 */
205 		skb_key.basic.n_proto = skb->protocol;
206 		skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
207 		skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
208 
209 		fl_set_masked_key(&skb_mkey, &skb_key, mask);
210 
211 		f = fl_lookup(mask, &skb_mkey);
212 		if (f && !tc_skip_sw(f->flags)) {
213 			*res = f->res;
214 			return tcf_exts_exec(skb, &f->exts, res);
215 		}
216 	}
217 	return -1;
218 }
219 
220 static int fl_init(struct tcf_proto *tp)
221 {
222 	struct cls_fl_head *head;
223 
224 	head = kzalloc(sizeof(*head), GFP_KERNEL);
225 	if (!head)
226 		return -ENOBUFS;
227 
228 	INIT_LIST_HEAD_RCU(&head->masks);
229 	rcu_assign_pointer(tp->root, head);
230 	idr_init(&head->handle_idr);
231 
232 	return rhashtable_init(&head->ht, &mask_ht_params);
233 }
234 
235 static void fl_mask_free(struct fl_flow_mask *mask)
236 {
237 	rhashtable_destroy(&mask->ht);
238 	kfree(mask);
239 }
240 
241 static void fl_mask_free_work(struct work_struct *work)
242 {
243 	struct fl_flow_mask *mask = container_of(to_rcu_work(work),
244 						 struct fl_flow_mask, rwork);
245 
246 	fl_mask_free(mask);
247 }
248 
249 static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask,
250 			bool async)
251 {
252 	if (!list_empty(&mask->filters))
253 		return false;
254 
255 	rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
256 	list_del_rcu(&mask->list);
257 	if (async)
258 		tcf_queue_work(&mask->rwork, fl_mask_free_work);
259 	else
260 		fl_mask_free(mask);
261 
262 	return true;
263 }
264 
265 static void __fl_destroy_filter(struct cls_fl_filter *f)
266 {
267 	tcf_exts_destroy(&f->exts);
268 	tcf_exts_put_net(&f->exts);
269 	kfree(f);
270 }
271 
272 static void fl_destroy_filter_work(struct work_struct *work)
273 {
274 	struct cls_fl_filter *f = container_of(to_rcu_work(work),
275 					struct cls_fl_filter, rwork);
276 
277 	rtnl_lock();
278 	__fl_destroy_filter(f);
279 	rtnl_unlock();
280 }
281 
282 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
283 				 struct netlink_ext_ack *extack)
284 {
285 	struct tc_cls_flower_offload cls_flower = {};
286 	struct tcf_block *block = tp->chain->block;
287 
288 	tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
289 	cls_flower.command = TC_CLSFLOWER_DESTROY;
290 	cls_flower.cookie = (unsigned long) f;
291 
292 	tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
293 			 &cls_flower, false);
294 	tcf_block_offload_dec(block, &f->flags);
295 }
296 
297 static int fl_hw_replace_filter(struct tcf_proto *tp,
298 				struct cls_fl_filter *f,
299 				struct netlink_ext_ack *extack)
300 {
301 	struct tc_cls_flower_offload cls_flower = {};
302 	struct tcf_block *block = tp->chain->block;
303 	bool skip_sw = tc_skip_sw(f->flags);
304 	int err;
305 
306 	tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
307 	cls_flower.command = TC_CLSFLOWER_REPLACE;
308 	cls_flower.cookie = (unsigned long) f;
309 	cls_flower.dissector = &f->mask->dissector;
310 	cls_flower.mask = &f->mask->key;
311 	cls_flower.key = &f->mkey;
312 	cls_flower.exts = &f->exts;
313 	cls_flower.classid = f->res.classid;
314 
315 	err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
316 			       &cls_flower, skip_sw);
317 	if (err < 0) {
318 		fl_hw_destroy_filter(tp, f, NULL);
319 		return err;
320 	} else if (err > 0) {
321 		f->in_hw_count = err;
322 		tcf_block_offload_inc(block, &f->flags);
323 	}
324 
325 	if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
326 		return -EINVAL;
327 
328 	return 0;
329 }
330 
331 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
332 {
333 	struct tc_cls_flower_offload cls_flower = {};
334 	struct tcf_block *block = tp->chain->block;
335 
336 	tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
337 	cls_flower.command = TC_CLSFLOWER_STATS;
338 	cls_flower.cookie = (unsigned long) f;
339 	cls_flower.exts = &f->exts;
340 	cls_flower.classid = f->res.classid;
341 
342 	tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
343 			 &cls_flower, false);
344 }
345 
346 static bool __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
347 			struct netlink_ext_ack *extack)
348 {
349 	struct cls_fl_head *head = rtnl_dereference(tp->root);
350 	bool async = tcf_exts_get_net(&f->exts);
351 	bool last;
352 
353 	idr_remove(&head->handle_idr, f->handle);
354 	list_del_rcu(&f->list);
355 	last = fl_mask_put(head, f->mask, async);
356 	if (!tc_skip_hw(f->flags))
357 		fl_hw_destroy_filter(tp, f, extack);
358 	tcf_unbind_filter(tp, &f->res);
359 	if (async)
360 		tcf_queue_work(&f->rwork, fl_destroy_filter_work);
361 	else
362 		__fl_destroy_filter(f);
363 
364 	return last;
365 }
366 
367 static void fl_destroy_sleepable(struct work_struct *work)
368 {
369 	struct cls_fl_head *head = container_of(to_rcu_work(work),
370 						struct cls_fl_head,
371 						rwork);
372 
373 	rhashtable_destroy(&head->ht);
374 	kfree(head);
375 	module_put(THIS_MODULE);
376 }
377 
378 static void fl_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
379 {
380 	struct cls_fl_head *head = rtnl_dereference(tp->root);
381 	struct fl_flow_mask *mask, *next_mask;
382 	struct cls_fl_filter *f, *next;
383 
384 	list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
385 		list_for_each_entry_safe(f, next, &mask->filters, list) {
386 			if (__fl_delete(tp, f, extack))
387 				break;
388 		}
389 	}
390 	idr_destroy(&head->handle_idr);
391 
392 	__module_get(THIS_MODULE);
393 	tcf_queue_work(&head->rwork, fl_destroy_sleepable);
394 }
395 
396 static void *fl_get(struct tcf_proto *tp, u32 handle)
397 {
398 	struct cls_fl_head *head = rtnl_dereference(tp->root);
399 
400 	return idr_find(&head->handle_idr, handle);
401 }
402 
403 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
404 	[TCA_FLOWER_UNSPEC]		= { .type = NLA_UNSPEC },
405 	[TCA_FLOWER_CLASSID]		= { .type = NLA_U32 },
406 	[TCA_FLOWER_INDEV]		= { .type = NLA_STRING,
407 					    .len = IFNAMSIZ },
408 	[TCA_FLOWER_KEY_ETH_DST]	= { .len = ETH_ALEN },
409 	[TCA_FLOWER_KEY_ETH_DST_MASK]	= { .len = ETH_ALEN },
410 	[TCA_FLOWER_KEY_ETH_SRC]	= { .len = ETH_ALEN },
411 	[TCA_FLOWER_KEY_ETH_SRC_MASK]	= { .len = ETH_ALEN },
412 	[TCA_FLOWER_KEY_ETH_TYPE]	= { .type = NLA_U16 },
413 	[TCA_FLOWER_KEY_IP_PROTO]	= { .type = NLA_U8 },
414 	[TCA_FLOWER_KEY_IPV4_SRC]	= { .type = NLA_U32 },
415 	[TCA_FLOWER_KEY_IPV4_SRC_MASK]	= { .type = NLA_U32 },
416 	[TCA_FLOWER_KEY_IPV4_DST]	= { .type = NLA_U32 },
417 	[TCA_FLOWER_KEY_IPV4_DST_MASK]	= { .type = NLA_U32 },
418 	[TCA_FLOWER_KEY_IPV6_SRC]	= { .len = sizeof(struct in6_addr) },
419 	[TCA_FLOWER_KEY_IPV6_SRC_MASK]	= { .len = sizeof(struct in6_addr) },
420 	[TCA_FLOWER_KEY_IPV6_DST]	= { .len = sizeof(struct in6_addr) },
421 	[TCA_FLOWER_KEY_IPV6_DST_MASK]	= { .len = sizeof(struct in6_addr) },
422 	[TCA_FLOWER_KEY_TCP_SRC]	= { .type = NLA_U16 },
423 	[TCA_FLOWER_KEY_TCP_DST]	= { .type = NLA_U16 },
424 	[TCA_FLOWER_KEY_UDP_SRC]	= { .type = NLA_U16 },
425 	[TCA_FLOWER_KEY_UDP_DST]	= { .type = NLA_U16 },
426 	[TCA_FLOWER_KEY_VLAN_ID]	= { .type = NLA_U16 },
427 	[TCA_FLOWER_KEY_VLAN_PRIO]	= { .type = NLA_U8 },
428 	[TCA_FLOWER_KEY_VLAN_ETH_TYPE]	= { .type = NLA_U16 },
429 	[TCA_FLOWER_KEY_ENC_KEY_ID]	= { .type = NLA_U32 },
430 	[TCA_FLOWER_KEY_ENC_IPV4_SRC]	= { .type = NLA_U32 },
431 	[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
432 	[TCA_FLOWER_KEY_ENC_IPV4_DST]	= { .type = NLA_U32 },
433 	[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
434 	[TCA_FLOWER_KEY_ENC_IPV6_SRC]	= { .len = sizeof(struct in6_addr) },
435 	[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
436 	[TCA_FLOWER_KEY_ENC_IPV6_DST]	= { .len = sizeof(struct in6_addr) },
437 	[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
438 	[TCA_FLOWER_KEY_TCP_SRC_MASK]	= { .type = NLA_U16 },
439 	[TCA_FLOWER_KEY_TCP_DST_MASK]	= { .type = NLA_U16 },
440 	[TCA_FLOWER_KEY_UDP_SRC_MASK]	= { .type = NLA_U16 },
441 	[TCA_FLOWER_KEY_UDP_DST_MASK]	= { .type = NLA_U16 },
442 	[TCA_FLOWER_KEY_SCTP_SRC_MASK]	= { .type = NLA_U16 },
443 	[TCA_FLOWER_KEY_SCTP_DST_MASK]	= { .type = NLA_U16 },
444 	[TCA_FLOWER_KEY_SCTP_SRC]	= { .type = NLA_U16 },
445 	[TCA_FLOWER_KEY_SCTP_DST]	= { .type = NLA_U16 },
446 	[TCA_FLOWER_KEY_ENC_UDP_SRC_PORT]	= { .type = NLA_U16 },
447 	[TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK]	= { .type = NLA_U16 },
448 	[TCA_FLOWER_KEY_ENC_UDP_DST_PORT]	= { .type = NLA_U16 },
449 	[TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK]	= { .type = NLA_U16 },
450 	[TCA_FLOWER_KEY_FLAGS]		= { .type = NLA_U32 },
451 	[TCA_FLOWER_KEY_FLAGS_MASK]	= { .type = NLA_U32 },
452 	[TCA_FLOWER_KEY_ICMPV4_TYPE]	= { .type = NLA_U8 },
453 	[TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
454 	[TCA_FLOWER_KEY_ICMPV4_CODE]	= { .type = NLA_U8 },
455 	[TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
456 	[TCA_FLOWER_KEY_ICMPV6_TYPE]	= { .type = NLA_U8 },
457 	[TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
458 	[TCA_FLOWER_KEY_ICMPV6_CODE]	= { .type = NLA_U8 },
459 	[TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
460 	[TCA_FLOWER_KEY_ARP_SIP]	= { .type = NLA_U32 },
461 	[TCA_FLOWER_KEY_ARP_SIP_MASK]	= { .type = NLA_U32 },
462 	[TCA_FLOWER_KEY_ARP_TIP]	= { .type = NLA_U32 },
463 	[TCA_FLOWER_KEY_ARP_TIP_MASK]	= { .type = NLA_U32 },
464 	[TCA_FLOWER_KEY_ARP_OP]		= { .type = NLA_U8 },
465 	[TCA_FLOWER_KEY_ARP_OP_MASK]	= { .type = NLA_U8 },
466 	[TCA_FLOWER_KEY_ARP_SHA]	= { .len = ETH_ALEN },
467 	[TCA_FLOWER_KEY_ARP_SHA_MASK]	= { .len = ETH_ALEN },
468 	[TCA_FLOWER_KEY_ARP_THA]	= { .len = ETH_ALEN },
469 	[TCA_FLOWER_KEY_ARP_THA_MASK]	= { .len = ETH_ALEN },
470 	[TCA_FLOWER_KEY_MPLS_TTL]	= { .type = NLA_U8 },
471 	[TCA_FLOWER_KEY_MPLS_BOS]	= { .type = NLA_U8 },
472 	[TCA_FLOWER_KEY_MPLS_TC]	= { .type = NLA_U8 },
473 	[TCA_FLOWER_KEY_MPLS_LABEL]	= { .type = NLA_U32 },
474 	[TCA_FLOWER_KEY_TCP_FLAGS]	= { .type = NLA_U16 },
475 	[TCA_FLOWER_KEY_TCP_FLAGS_MASK]	= { .type = NLA_U16 },
476 	[TCA_FLOWER_KEY_IP_TOS]		= { .type = NLA_U8 },
477 	[TCA_FLOWER_KEY_IP_TOS_MASK]	= { .type = NLA_U8 },
478 	[TCA_FLOWER_KEY_IP_TTL]		= { .type = NLA_U8 },
479 	[TCA_FLOWER_KEY_IP_TTL_MASK]	= { .type = NLA_U8 },
480 	[TCA_FLOWER_KEY_CVLAN_ID]	= { .type = NLA_U16 },
481 	[TCA_FLOWER_KEY_CVLAN_PRIO]	= { .type = NLA_U8 },
482 	[TCA_FLOWER_KEY_CVLAN_ETH_TYPE]	= { .type = NLA_U16 },
483 	[TCA_FLOWER_KEY_ENC_IP_TOS]	= { .type = NLA_U8 },
484 	[TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
485 	[TCA_FLOWER_KEY_ENC_IP_TTL]	 = { .type = NLA_U8 },
486 	[TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
487 	[TCA_FLOWER_KEY_ENC_OPTS]	= { .type = NLA_NESTED },
488 	[TCA_FLOWER_KEY_ENC_OPTS_MASK]	= { .type = NLA_NESTED },
489 };
490 
491 static const struct nla_policy
492 enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
493 	[TCA_FLOWER_KEY_ENC_OPTS_GENEVE]        = { .type = NLA_NESTED },
494 };
495 
496 static const struct nla_policy
497 geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
498 	[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]      = { .type = NLA_U16 },
499 	[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]       = { .type = NLA_U8 },
500 	[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]       = { .type = NLA_BINARY,
501 						       .len = 128 },
502 };
503 
504 static void fl_set_key_val(struct nlattr **tb,
505 			   void *val, int val_type,
506 			   void *mask, int mask_type, int len)
507 {
508 	if (!tb[val_type])
509 		return;
510 	memcpy(val, nla_data(tb[val_type]), len);
511 	if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
512 		memset(mask, 0xff, len);
513 	else
514 		memcpy(mask, nla_data(tb[mask_type]), len);
515 }
516 
517 static int fl_set_key_mpls(struct nlattr **tb,
518 			   struct flow_dissector_key_mpls *key_val,
519 			   struct flow_dissector_key_mpls *key_mask)
520 {
521 	if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
522 		key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
523 		key_mask->mpls_ttl = MPLS_TTL_MASK;
524 	}
525 	if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
526 		u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
527 
528 		if (bos & ~MPLS_BOS_MASK)
529 			return -EINVAL;
530 		key_val->mpls_bos = bos;
531 		key_mask->mpls_bos = MPLS_BOS_MASK;
532 	}
533 	if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
534 		u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
535 
536 		if (tc & ~MPLS_TC_MASK)
537 			return -EINVAL;
538 		key_val->mpls_tc = tc;
539 		key_mask->mpls_tc = MPLS_TC_MASK;
540 	}
541 	if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
542 		u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
543 
544 		if (label & ~MPLS_LABEL_MASK)
545 			return -EINVAL;
546 		key_val->mpls_label = label;
547 		key_mask->mpls_label = MPLS_LABEL_MASK;
548 	}
549 	return 0;
550 }
551 
552 static void fl_set_key_vlan(struct nlattr **tb,
553 			    __be16 ethertype,
554 			    int vlan_id_key, int vlan_prio_key,
555 			    struct flow_dissector_key_vlan *key_val,
556 			    struct flow_dissector_key_vlan *key_mask)
557 {
558 #define VLAN_PRIORITY_MASK	0x7
559 
560 	if (tb[vlan_id_key]) {
561 		key_val->vlan_id =
562 			nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
563 		key_mask->vlan_id = VLAN_VID_MASK;
564 	}
565 	if (tb[vlan_prio_key]) {
566 		key_val->vlan_priority =
567 			nla_get_u8(tb[vlan_prio_key]) &
568 			VLAN_PRIORITY_MASK;
569 		key_mask->vlan_priority = VLAN_PRIORITY_MASK;
570 	}
571 	key_val->vlan_tpid = ethertype;
572 	key_mask->vlan_tpid = cpu_to_be16(~0);
573 }
574 
575 static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
576 			    u32 *dissector_key, u32 *dissector_mask,
577 			    u32 flower_flag_bit, u32 dissector_flag_bit)
578 {
579 	if (flower_mask & flower_flag_bit) {
580 		*dissector_mask |= dissector_flag_bit;
581 		if (flower_key & flower_flag_bit)
582 			*dissector_key |= dissector_flag_bit;
583 	}
584 }
585 
586 static int fl_set_key_flags(struct nlattr **tb,
587 			    u32 *flags_key, u32 *flags_mask)
588 {
589 	u32 key, mask;
590 
591 	/* mask is mandatory for flags */
592 	if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
593 		return -EINVAL;
594 
595 	key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
596 	mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
597 
598 	*flags_key  = 0;
599 	*flags_mask = 0;
600 
601 	fl_set_key_flag(key, mask, flags_key, flags_mask,
602 			TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
603 	fl_set_key_flag(key, mask, flags_key, flags_mask,
604 			TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
605 			FLOW_DIS_FIRST_FRAG);
606 
607 	return 0;
608 }
609 
610 static void fl_set_key_ip(struct nlattr **tb, bool encap,
611 			  struct flow_dissector_key_ip *key,
612 			  struct flow_dissector_key_ip *mask)
613 {
614 	int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
615 	int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
616 	int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
617 	int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
618 
619 	fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
620 	fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
621 }
622 
623 static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
624 			     int depth, int option_len,
625 			     struct netlink_ext_ack *extack)
626 {
627 	struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
628 	struct nlattr *class = NULL, *type = NULL, *data = NULL;
629 	struct geneve_opt *opt;
630 	int err, data_len = 0;
631 
632 	if (option_len > sizeof(struct geneve_opt))
633 		data_len = option_len - sizeof(struct geneve_opt);
634 
635 	opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
636 	memset(opt, 0xff, option_len);
637 	opt->length = data_len / 4;
638 	opt->r1 = 0;
639 	opt->r2 = 0;
640 	opt->r3 = 0;
641 
642 	/* If no mask has been prodived we assume an exact match. */
643 	if (!depth)
644 		return sizeof(struct geneve_opt) + data_len;
645 
646 	if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
647 		NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
648 		return -EINVAL;
649 	}
650 
651 	err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
652 			       nla, geneve_opt_policy, extack);
653 	if (err < 0)
654 		return err;
655 
656 	/* We are not allowed to omit any of CLASS, TYPE or DATA
657 	 * fields from the key.
658 	 */
659 	if (!option_len &&
660 	    (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
661 	     !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
662 	     !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
663 		NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
664 		return -EINVAL;
665 	}
666 
667 	/* Omitting any of CLASS, TYPE or DATA fields is allowed
668 	 * for the mask.
669 	 */
670 	if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
671 		int new_len = key->enc_opts.len;
672 
673 		data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
674 		data_len = nla_len(data);
675 		if (data_len < 4) {
676 			NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
677 			return -ERANGE;
678 		}
679 		if (data_len % 4) {
680 			NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
681 			return -ERANGE;
682 		}
683 
684 		new_len += sizeof(struct geneve_opt) + data_len;
685 		BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
686 		if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
687 			NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
688 			return -ERANGE;
689 		}
690 		opt->length = data_len / 4;
691 		memcpy(opt->opt_data, nla_data(data), data_len);
692 	}
693 
694 	if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
695 		class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
696 		opt->opt_class = nla_get_be16(class);
697 	}
698 
699 	if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
700 		type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
701 		opt->type = nla_get_u8(type);
702 	}
703 
704 	return sizeof(struct geneve_opt) + data_len;
705 }
706 
707 static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
708 			  struct fl_flow_key *mask,
709 			  struct netlink_ext_ack *extack)
710 {
711 	const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
712 	int option_len, key_depth, msk_depth = 0;
713 
714 	nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
715 
716 	if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
717 		nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
718 		msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
719 	}
720 
721 	nla_for_each_attr(nla_opt_key, nla_enc_key,
722 			  nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
723 		switch (nla_type(nla_opt_key)) {
724 		case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
725 			option_len = 0;
726 			key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
727 			option_len = fl_set_geneve_opt(nla_opt_key, key,
728 						       key_depth, option_len,
729 						       extack);
730 			if (option_len < 0)
731 				return option_len;
732 
733 			key->enc_opts.len += option_len;
734 			/* At the same time we need to parse through the mask
735 			 * in order to verify exact and mask attribute lengths.
736 			 */
737 			mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
738 			option_len = fl_set_geneve_opt(nla_opt_msk, mask,
739 						       msk_depth, option_len,
740 						       extack);
741 			if (option_len < 0)
742 				return option_len;
743 
744 			mask->enc_opts.len += option_len;
745 			if (key->enc_opts.len != mask->enc_opts.len) {
746 				NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
747 				return -EINVAL;
748 			}
749 
750 			if (msk_depth)
751 				nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
752 			break;
753 		default:
754 			NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
755 			return -EINVAL;
756 		}
757 	}
758 
759 	return 0;
760 }
761 
762 static int fl_set_key(struct net *net, struct nlattr **tb,
763 		      struct fl_flow_key *key, struct fl_flow_key *mask,
764 		      struct netlink_ext_ack *extack)
765 {
766 	__be16 ethertype;
767 	int ret = 0;
768 #ifdef CONFIG_NET_CLS_IND
769 	if (tb[TCA_FLOWER_INDEV]) {
770 		int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
771 		if (err < 0)
772 			return err;
773 		key->indev_ifindex = err;
774 		mask->indev_ifindex = 0xffffffff;
775 	}
776 #endif
777 
778 	fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
779 		       mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
780 		       sizeof(key->eth.dst));
781 	fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
782 		       mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
783 		       sizeof(key->eth.src));
784 
785 	if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
786 		ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
787 
788 		if (eth_type_vlan(ethertype)) {
789 			fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
790 					TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan,
791 					&mask->vlan);
792 
793 			if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
794 				ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
795 				if (eth_type_vlan(ethertype)) {
796 					fl_set_key_vlan(tb, ethertype,
797 							TCA_FLOWER_KEY_CVLAN_ID,
798 							TCA_FLOWER_KEY_CVLAN_PRIO,
799 							&key->cvlan, &mask->cvlan);
800 					fl_set_key_val(tb, &key->basic.n_proto,
801 						       TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
802 						       &mask->basic.n_proto,
803 						       TCA_FLOWER_UNSPEC,
804 						       sizeof(key->basic.n_proto));
805 				} else {
806 					key->basic.n_proto = ethertype;
807 					mask->basic.n_proto = cpu_to_be16(~0);
808 				}
809 			}
810 		} else {
811 			key->basic.n_proto = ethertype;
812 			mask->basic.n_proto = cpu_to_be16(~0);
813 		}
814 	}
815 
816 	if (key->basic.n_proto == htons(ETH_P_IP) ||
817 	    key->basic.n_proto == htons(ETH_P_IPV6)) {
818 		fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
819 			       &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
820 			       sizeof(key->basic.ip_proto));
821 		fl_set_key_ip(tb, false, &key->ip, &mask->ip);
822 	}
823 
824 	if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
825 		key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
826 		mask->control.addr_type = ~0;
827 		fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
828 			       &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
829 			       sizeof(key->ipv4.src));
830 		fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
831 			       &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
832 			       sizeof(key->ipv4.dst));
833 	} else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
834 		key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
835 		mask->control.addr_type = ~0;
836 		fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
837 			       &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
838 			       sizeof(key->ipv6.src));
839 		fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
840 			       &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
841 			       sizeof(key->ipv6.dst));
842 	}
843 
844 	if (key->basic.ip_proto == IPPROTO_TCP) {
845 		fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
846 			       &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
847 			       sizeof(key->tp.src));
848 		fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
849 			       &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
850 			       sizeof(key->tp.dst));
851 		fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
852 			       &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
853 			       sizeof(key->tcp.flags));
854 	} else if (key->basic.ip_proto == IPPROTO_UDP) {
855 		fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
856 			       &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
857 			       sizeof(key->tp.src));
858 		fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
859 			       &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
860 			       sizeof(key->tp.dst));
861 	} else if (key->basic.ip_proto == IPPROTO_SCTP) {
862 		fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
863 			       &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
864 			       sizeof(key->tp.src));
865 		fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
866 			       &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
867 			       sizeof(key->tp.dst));
868 	} else if (key->basic.n_proto == htons(ETH_P_IP) &&
869 		   key->basic.ip_proto == IPPROTO_ICMP) {
870 		fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
871 			       &mask->icmp.type,
872 			       TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
873 			       sizeof(key->icmp.type));
874 		fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
875 			       &mask->icmp.code,
876 			       TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
877 			       sizeof(key->icmp.code));
878 	} else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
879 		   key->basic.ip_proto == IPPROTO_ICMPV6) {
880 		fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
881 			       &mask->icmp.type,
882 			       TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
883 			       sizeof(key->icmp.type));
884 		fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
885 			       &mask->icmp.code,
886 			       TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
887 			       sizeof(key->icmp.code));
888 	} else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
889 		   key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
890 		ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
891 		if (ret)
892 			return ret;
893 	} else if (key->basic.n_proto == htons(ETH_P_ARP) ||
894 		   key->basic.n_proto == htons(ETH_P_RARP)) {
895 		fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
896 			       &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
897 			       sizeof(key->arp.sip));
898 		fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
899 			       &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
900 			       sizeof(key->arp.tip));
901 		fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
902 			       &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
903 			       sizeof(key->arp.op));
904 		fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
905 			       mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
906 			       sizeof(key->arp.sha));
907 		fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
908 			       mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
909 			       sizeof(key->arp.tha));
910 	}
911 
912 	if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
913 	    tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
914 		key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
915 		mask->enc_control.addr_type = ~0;
916 		fl_set_key_val(tb, &key->enc_ipv4.src,
917 			       TCA_FLOWER_KEY_ENC_IPV4_SRC,
918 			       &mask->enc_ipv4.src,
919 			       TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
920 			       sizeof(key->enc_ipv4.src));
921 		fl_set_key_val(tb, &key->enc_ipv4.dst,
922 			       TCA_FLOWER_KEY_ENC_IPV4_DST,
923 			       &mask->enc_ipv4.dst,
924 			       TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
925 			       sizeof(key->enc_ipv4.dst));
926 	}
927 
928 	if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
929 	    tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
930 		key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
931 		mask->enc_control.addr_type = ~0;
932 		fl_set_key_val(tb, &key->enc_ipv6.src,
933 			       TCA_FLOWER_KEY_ENC_IPV6_SRC,
934 			       &mask->enc_ipv6.src,
935 			       TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
936 			       sizeof(key->enc_ipv6.src));
937 		fl_set_key_val(tb, &key->enc_ipv6.dst,
938 			       TCA_FLOWER_KEY_ENC_IPV6_DST,
939 			       &mask->enc_ipv6.dst,
940 			       TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
941 			       sizeof(key->enc_ipv6.dst));
942 	}
943 
944 	fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
945 		       &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
946 		       sizeof(key->enc_key_id.keyid));
947 
948 	fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
949 		       &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
950 		       sizeof(key->enc_tp.src));
951 
952 	fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
953 		       &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
954 		       sizeof(key->enc_tp.dst));
955 
956 	fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
957 
958 	if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
959 		ret = fl_set_enc_opt(tb, key, mask, extack);
960 		if (ret)
961 			return ret;
962 	}
963 
964 	if (tb[TCA_FLOWER_KEY_FLAGS])
965 		ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
966 
967 	return ret;
968 }
969 
970 static void fl_mask_copy(struct fl_flow_mask *dst,
971 			 struct fl_flow_mask *src)
972 {
973 	const void *psrc = fl_key_get_start(&src->key, src);
974 	void *pdst = fl_key_get_start(&dst->key, src);
975 
976 	memcpy(pdst, psrc, fl_mask_range(src));
977 	dst->range = src->range;
978 }
979 
980 static const struct rhashtable_params fl_ht_params = {
981 	.key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
982 	.head_offset = offsetof(struct cls_fl_filter, ht_node),
983 	.automatic_shrinking = true,
984 };
985 
986 static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
987 {
988 	mask->filter_ht_params = fl_ht_params;
989 	mask->filter_ht_params.key_len = fl_mask_range(mask);
990 	mask->filter_ht_params.key_offset += mask->range.start;
991 
992 	return rhashtable_init(&mask->ht, &mask->filter_ht_params);
993 }
994 
995 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
996 #define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
997 
998 #define FL_KEY_IS_MASKED(mask, member)						\
999 	memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member),		\
1000 		   0, FL_KEY_MEMBER_SIZE(member))				\
1001 
1002 #define FL_KEY_SET(keys, cnt, id, member)					\
1003 	do {									\
1004 		keys[cnt].key_id = id;						\
1005 		keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member);		\
1006 		cnt++;								\
1007 	} while(0);
1008 
1009 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member)			\
1010 	do {									\
1011 		if (FL_KEY_IS_MASKED(mask, member))				\
1012 			FL_KEY_SET(keys, cnt, id, member);			\
1013 	} while(0);
1014 
1015 static void fl_init_dissector(struct flow_dissector *dissector,
1016 			      struct fl_flow_key *mask)
1017 {
1018 	struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
1019 	size_t cnt = 0;
1020 
1021 	FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
1022 	FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
1023 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1024 			     FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
1025 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1026 			     FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
1027 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1028 			     FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
1029 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1030 			     FLOW_DISSECTOR_KEY_PORTS, tp);
1031 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1032 			     FLOW_DISSECTOR_KEY_IP, ip);
1033 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1034 			     FLOW_DISSECTOR_KEY_TCP, tcp);
1035 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1036 			     FLOW_DISSECTOR_KEY_ICMP, icmp);
1037 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1038 			     FLOW_DISSECTOR_KEY_ARP, arp);
1039 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1040 			     FLOW_DISSECTOR_KEY_MPLS, mpls);
1041 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1042 			     FLOW_DISSECTOR_KEY_VLAN, vlan);
1043 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1044 			     FLOW_DISSECTOR_KEY_CVLAN, cvlan);
1045 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1046 			     FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
1047 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1048 			     FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
1049 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1050 			     FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
1051 	if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
1052 	    FL_KEY_IS_MASKED(mask, enc_ipv6))
1053 		FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
1054 			   enc_control);
1055 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1056 			     FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
1057 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1058 			     FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
1059 	FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1060 			     FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
1061 
1062 	skb_flow_dissector_init(dissector, keys, cnt);
1063 }
1064 
1065 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
1066 					       struct fl_flow_mask *mask)
1067 {
1068 	struct fl_flow_mask *newmask;
1069 	int err;
1070 
1071 	newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
1072 	if (!newmask)
1073 		return ERR_PTR(-ENOMEM);
1074 
1075 	fl_mask_copy(newmask, mask);
1076 
1077 	err = fl_init_mask_hashtable(newmask);
1078 	if (err)
1079 		goto errout_free;
1080 
1081 	fl_init_dissector(&newmask->dissector, &newmask->key);
1082 
1083 	INIT_LIST_HEAD_RCU(&newmask->filters);
1084 
1085 	err = rhashtable_insert_fast(&head->ht, &newmask->ht_node,
1086 				     mask_ht_params);
1087 	if (err)
1088 		goto errout_destroy;
1089 
1090 	list_add_tail_rcu(&newmask->list, &head->masks);
1091 
1092 	return newmask;
1093 
1094 errout_destroy:
1095 	rhashtable_destroy(&newmask->ht);
1096 errout_free:
1097 	kfree(newmask);
1098 
1099 	return ERR_PTR(err);
1100 }
1101 
1102 static int fl_check_assign_mask(struct cls_fl_head *head,
1103 				struct cls_fl_filter *fnew,
1104 				struct cls_fl_filter *fold,
1105 				struct fl_flow_mask *mask)
1106 {
1107 	struct fl_flow_mask *newmask;
1108 
1109 	fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params);
1110 	if (!fnew->mask) {
1111 		if (fold)
1112 			return -EINVAL;
1113 
1114 		newmask = fl_create_new_mask(head, mask);
1115 		if (IS_ERR(newmask))
1116 			return PTR_ERR(newmask);
1117 
1118 		fnew->mask = newmask;
1119 	} else if (fold && fold->mask != fnew->mask) {
1120 		return -EINVAL;
1121 	}
1122 
1123 	return 0;
1124 }
1125 
1126 static int fl_set_parms(struct net *net, struct tcf_proto *tp,
1127 			struct cls_fl_filter *f, struct fl_flow_mask *mask,
1128 			unsigned long base, struct nlattr **tb,
1129 			struct nlattr *est, bool ovr,
1130 			struct fl_flow_tmplt *tmplt,
1131 			struct netlink_ext_ack *extack)
1132 {
1133 	int err;
1134 
1135 	err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
1136 	if (err < 0)
1137 		return err;
1138 
1139 	if (tb[TCA_FLOWER_CLASSID]) {
1140 		f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
1141 		tcf_bind_filter(tp, &f->res, base);
1142 	}
1143 
1144 	err = fl_set_key(net, tb, &f->key, &mask->key, extack);
1145 	if (err)
1146 		return err;
1147 
1148 	fl_mask_update_range(mask);
1149 	fl_set_masked_key(&f->mkey, &f->key, mask);
1150 
1151 	if (!fl_mask_fits_tmplt(tmplt, mask)) {
1152 		NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
1153 		return -EINVAL;
1154 	}
1155 
1156 	return 0;
1157 }
1158 
1159 static int fl_change(struct net *net, struct sk_buff *in_skb,
1160 		     struct tcf_proto *tp, unsigned long base,
1161 		     u32 handle, struct nlattr **tca,
1162 		     void **arg, bool ovr, struct netlink_ext_ack *extack)
1163 {
1164 	struct cls_fl_head *head = rtnl_dereference(tp->root);
1165 	struct cls_fl_filter *fold = *arg;
1166 	struct cls_fl_filter *fnew;
1167 	struct nlattr **tb;
1168 	struct fl_flow_mask mask = {};
1169 	int err;
1170 
1171 	if (!tca[TCA_OPTIONS])
1172 		return -EINVAL;
1173 
1174 	tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1175 	if (!tb)
1176 		return -ENOBUFS;
1177 
1178 	err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
1179 			       fl_policy, NULL);
1180 	if (err < 0)
1181 		goto errout_tb;
1182 
1183 	if (fold && handle && fold->handle != handle) {
1184 		err = -EINVAL;
1185 		goto errout_tb;
1186 	}
1187 
1188 	fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
1189 	if (!fnew) {
1190 		err = -ENOBUFS;
1191 		goto errout_tb;
1192 	}
1193 
1194 	err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
1195 	if (err < 0)
1196 		goto errout;
1197 
1198 	if (!handle) {
1199 		handle = 1;
1200 		err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1201 				    INT_MAX, GFP_KERNEL);
1202 	} else if (!fold) {
1203 		/* user specifies a handle and it doesn't exist */
1204 		err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1205 				    handle, GFP_KERNEL);
1206 	}
1207 	if (err)
1208 		goto errout;
1209 	fnew->handle = handle;
1210 
1211 	if (tb[TCA_FLOWER_FLAGS]) {
1212 		fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
1213 
1214 		if (!tc_flags_valid(fnew->flags)) {
1215 			err = -EINVAL;
1216 			goto errout_idr;
1217 		}
1218 	}
1219 
1220 	err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr,
1221 			   tp->chain->tmplt_priv, extack);
1222 	if (err)
1223 		goto errout_idr;
1224 
1225 	err = fl_check_assign_mask(head, fnew, fold, &mask);
1226 	if (err)
1227 		goto errout_idr;
1228 
1229 	if (!tc_skip_sw(fnew->flags)) {
1230 		if (!fold && fl_lookup(fnew->mask, &fnew->mkey)) {
1231 			err = -EEXIST;
1232 			goto errout_mask;
1233 		}
1234 
1235 		err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node,
1236 					     fnew->mask->filter_ht_params);
1237 		if (err)
1238 			goto errout_mask;
1239 	}
1240 
1241 	if (!tc_skip_hw(fnew->flags)) {
1242 		err = fl_hw_replace_filter(tp, fnew, extack);
1243 		if (err)
1244 			goto errout_mask;
1245 	}
1246 
1247 	if (!tc_in_hw(fnew->flags))
1248 		fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1249 
1250 	if (fold) {
1251 		if (!tc_skip_sw(fold->flags))
1252 			rhashtable_remove_fast(&fold->mask->ht,
1253 					       &fold->ht_node,
1254 					       fold->mask->filter_ht_params);
1255 		if (!tc_skip_hw(fold->flags))
1256 			fl_hw_destroy_filter(tp, fold, NULL);
1257 	}
1258 
1259 	*arg = fnew;
1260 
1261 	if (fold) {
1262 		idr_replace(&head->handle_idr, fnew, fnew->handle);
1263 		list_replace_rcu(&fold->list, &fnew->list);
1264 		tcf_unbind_filter(tp, &fold->res);
1265 		tcf_exts_get_net(&fold->exts);
1266 		tcf_queue_work(&fold->rwork, fl_destroy_filter_work);
1267 	} else {
1268 		list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
1269 	}
1270 
1271 	kfree(tb);
1272 	return 0;
1273 
1274 errout_mask:
1275 	fl_mask_put(head, fnew->mask, false);
1276 
1277 errout_idr:
1278 	if (!fold)
1279 		idr_remove(&head->handle_idr, fnew->handle);
1280 errout:
1281 	tcf_exts_destroy(&fnew->exts);
1282 	kfree(fnew);
1283 errout_tb:
1284 	kfree(tb);
1285 	return err;
1286 }
1287 
1288 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
1289 		     struct netlink_ext_ack *extack)
1290 {
1291 	struct cls_fl_head *head = rtnl_dereference(tp->root);
1292 	struct cls_fl_filter *f = arg;
1293 
1294 	if (!tc_skip_sw(f->flags))
1295 		rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
1296 				       f->mask->filter_ht_params);
1297 	__fl_delete(tp, f, extack);
1298 	*last = list_empty(&head->masks);
1299 	return 0;
1300 }
1301 
1302 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1303 {
1304 	struct cls_fl_head *head = rtnl_dereference(tp->root);
1305 	struct cls_fl_filter *f;
1306 
1307 	arg->count = arg->skip;
1308 
1309 	while ((f = idr_get_next_ul(&head->handle_idr,
1310 				    &arg->cookie)) != NULL) {
1311 		if (arg->fn(tp, f, arg) < 0) {
1312 			arg->stop = 1;
1313 			break;
1314 		}
1315 		arg->cookie = f->handle + 1;
1316 		arg->count++;
1317 	}
1318 }
1319 
1320 static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1321 			void *cb_priv, struct netlink_ext_ack *extack)
1322 {
1323 	struct cls_fl_head *head = rtnl_dereference(tp->root);
1324 	struct tc_cls_flower_offload cls_flower = {};
1325 	struct tcf_block *block = tp->chain->block;
1326 	struct fl_flow_mask *mask;
1327 	struct cls_fl_filter *f;
1328 	int err;
1329 
1330 	list_for_each_entry(mask, &head->masks, list) {
1331 		list_for_each_entry(f, &mask->filters, list) {
1332 			if (tc_skip_hw(f->flags))
1333 				continue;
1334 
1335 			tc_cls_common_offload_init(&cls_flower.common, tp,
1336 						   f->flags, extack);
1337 			cls_flower.command = add ?
1338 				TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY;
1339 			cls_flower.cookie = (unsigned long)f;
1340 			cls_flower.dissector = &mask->dissector;
1341 			cls_flower.mask = &mask->key;
1342 			cls_flower.key = &f->mkey;
1343 			cls_flower.exts = &f->exts;
1344 			cls_flower.classid = f->res.classid;
1345 
1346 			err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
1347 			if (err) {
1348 				if (add && tc_skip_sw(f->flags))
1349 					return err;
1350 				continue;
1351 			}
1352 
1353 			tc_cls_offload_cnt_update(block, &f->in_hw_count,
1354 						  &f->flags, add);
1355 		}
1356 	}
1357 
1358 	return 0;
1359 }
1360 
1361 static void fl_hw_create_tmplt(struct tcf_chain *chain,
1362 			       struct fl_flow_tmplt *tmplt)
1363 {
1364 	struct tc_cls_flower_offload cls_flower = {};
1365 	struct tcf_block *block = chain->block;
1366 	struct tcf_exts dummy_exts = { 0, };
1367 
1368 	cls_flower.common.chain_index = chain->index;
1369 	cls_flower.command = TC_CLSFLOWER_TMPLT_CREATE;
1370 	cls_flower.cookie = (unsigned long) tmplt;
1371 	cls_flower.dissector = &tmplt->dissector;
1372 	cls_flower.mask = &tmplt->mask;
1373 	cls_flower.key = &tmplt->dummy_key;
1374 	cls_flower.exts = &dummy_exts;
1375 
1376 	/* We don't care if driver (any of them) fails to handle this
1377 	 * call. It serves just as a hint for it.
1378 	 */
1379 	tc_setup_cb_call(block, NULL, TC_SETUP_CLSFLOWER,
1380 			 &cls_flower, false);
1381 }
1382 
1383 static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
1384 				struct fl_flow_tmplt *tmplt)
1385 {
1386 	struct tc_cls_flower_offload cls_flower = {};
1387 	struct tcf_block *block = chain->block;
1388 
1389 	cls_flower.common.chain_index = chain->index;
1390 	cls_flower.command = TC_CLSFLOWER_TMPLT_DESTROY;
1391 	cls_flower.cookie = (unsigned long) tmplt;
1392 
1393 	tc_setup_cb_call(block, NULL, TC_SETUP_CLSFLOWER,
1394 			 &cls_flower, false);
1395 }
1396 
1397 static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
1398 			     struct nlattr **tca,
1399 			     struct netlink_ext_ack *extack)
1400 {
1401 	struct fl_flow_tmplt *tmplt;
1402 	struct nlattr **tb;
1403 	int err;
1404 
1405 	if (!tca[TCA_OPTIONS])
1406 		return ERR_PTR(-EINVAL);
1407 
1408 	tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1409 	if (!tb)
1410 		return ERR_PTR(-ENOBUFS);
1411 	err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
1412 			       fl_policy, NULL);
1413 	if (err)
1414 		goto errout_tb;
1415 
1416 	tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
1417 	if (!tmplt) {
1418 		err = -ENOMEM;
1419 		goto errout_tb;
1420 	}
1421 	tmplt->chain = chain;
1422 	err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
1423 	if (err)
1424 		goto errout_tmplt;
1425 	kfree(tb);
1426 
1427 	fl_init_dissector(&tmplt->dissector, &tmplt->mask);
1428 
1429 	fl_hw_create_tmplt(chain, tmplt);
1430 
1431 	return tmplt;
1432 
1433 errout_tmplt:
1434 	kfree(tmplt);
1435 errout_tb:
1436 	kfree(tb);
1437 	return ERR_PTR(err);
1438 }
1439 
1440 static void fl_tmplt_destroy(void *tmplt_priv)
1441 {
1442 	struct fl_flow_tmplt *tmplt = tmplt_priv;
1443 
1444 	fl_hw_destroy_tmplt(tmplt->chain, tmplt);
1445 	kfree(tmplt);
1446 }
1447 
1448 static int fl_dump_key_val(struct sk_buff *skb,
1449 			   void *val, int val_type,
1450 			   void *mask, int mask_type, int len)
1451 {
1452 	int err;
1453 
1454 	if (!memchr_inv(mask, 0, len))
1455 		return 0;
1456 	err = nla_put(skb, val_type, len, val);
1457 	if (err)
1458 		return err;
1459 	if (mask_type != TCA_FLOWER_UNSPEC) {
1460 		err = nla_put(skb, mask_type, len, mask);
1461 		if (err)
1462 			return err;
1463 	}
1464 	return 0;
1465 }
1466 
1467 static int fl_dump_key_mpls(struct sk_buff *skb,
1468 			    struct flow_dissector_key_mpls *mpls_key,
1469 			    struct flow_dissector_key_mpls *mpls_mask)
1470 {
1471 	int err;
1472 
1473 	if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1474 		return 0;
1475 	if (mpls_mask->mpls_ttl) {
1476 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1477 				 mpls_key->mpls_ttl);
1478 		if (err)
1479 			return err;
1480 	}
1481 	if (mpls_mask->mpls_tc) {
1482 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1483 				 mpls_key->mpls_tc);
1484 		if (err)
1485 			return err;
1486 	}
1487 	if (mpls_mask->mpls_label) {
1488 		err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1489 				  mpls_key->mpls_label);
1490 		if (err)
1491 			return err;
1492 	}
1493 	if (mpls_mask->mpls_bos) {
1494 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1495 				 mpls_key->mpls_bos);
1496 		if (err)
1497 			return err;
1498 	}
1499 	return 0;
1500 }
1501 
1502 static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
1503 			  struct flow_dissector_key_ip *key,
1504 			  struct flow_dissector_key_ip *mask)
1505 {
1506 	int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1507 	int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1508 	int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1509 	int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
1510 
1511 	if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
1512 	    fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
1513 		return -1;
1514 
1515 	return 0;
1516 }
1517 
1518 static int fl_dump_key_vlan(struct sk_buff *skb,
1519 			    int vlan_id_key, int vlan_prio_key,
1520 			    struct flow_dissector_key_vlan *vlan_key,
1521 			    struct flow_dissector_key_vlan *vlan_mask)
1522 {
1523 	int err;
1524 
1525 	if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1526 		return 0;
1527 	if (vlan_mask->vlan_id) {
1528 		err = nla_put_u16(skb, vlan_id_key,
1529 				  vlan_key->vlan_id);
1530 		if (err)
1531 			return err;
1532 	}
1533 	if (vlan_mask->vlan_priority) {
1534 		err = nla_put_u8(skb, vlan_prio_key,
1535 				 vlan_key->vlan_priority);
1536 		if (err)
1537 			return err;
1538 	}
1539 	return 0;
1540 }
1541 
1542 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1543 			    u32 *flower_key, u32 *flower_mask,
1544 			    u32 flower_flag_bit, u32 dissector_flag_bit)
1545 {
1546 	if (dissector_mask & dissector_flag_bit) {
1547 		*flower_mask |= flower_flag_bit;
1548 		if (dissector_key & dissector_flag_bit)
1549 			*flower_key |= flower_flag_bit;
1550 	}
1551 }
1552 
1553 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1554 {
1555 	u32 key, mask;
1556 	__be32 _key, _mask;
1557 	int err;
1558 
1559 	if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1560 		return 0;
1561 
1562 	key = 0;
1563 	mask = 0;
1564 
1565 	fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1566 			TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1567 	fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1568 			TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1569 			FLOW_DIS_FIRST_FRAG);
1570 
1571 	_key = cpu_to_be32(key);
1572 	_mask = cpu_to_be32(mask);
1573 
1574 	err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1575 	if (err)
1576 		return err;
1577 
1578 	return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1579 }
1580 
1581 static int fl_dump_key_geneve_opt(struct sk_buff *skb,
1582 				  struct flow_dissector_key_enc_opts *enc_opts)
1583 {
1584 	struct geneve_opt *opt;
1585 	struct nlattr *nest;
1586 	int opt_off = 0;
1587 
1588 	nest = nla_nest_start(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
1589 	if (!nest)
1590 		goto nla_put_failure;
1591 
1592 	while (enc_opts->len > opt_off) {
1593 		opt = (struct geneve_opt *)&enc_opts->data[opt_off];
1594 
1595 		if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
1596 				 opt->opt_class))
1597 			goto nla_put_failure;
1598 		if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
1599 			       opt->type))
1600 			goto nla_put_failure;
1601 		if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
1602 			    opt->length * 4, opt->opt_data))
1603 			goto nla_put_failure;
1604 
1605 		opt_off += sizeof(struct geneve_opt) + opt->length * 4;
1606 	}
1607 	nla_nest_end(skb, nest);
1608 	return 0;
1609 
1610 nla_put_failure:
1611 	nla_nest_cancel(skb, nest);
1612 	return -EMSGSIZE;
1613 }
1614 
1615 static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
1616 			       struct flow_dissector_key_enc_opts *enc_opts)
1617 {
1618 	struct nlattr *nest;
1619 	int err;
1620 
1621 	if (!enc_opts->len)
1622 		return 0;
1623 
1624 	nest = nla_nest_start(skb, enc_opt_type);
1625 	if (!nest)
1626 		goto nla_put_failure;
1627 
1628 	switch (enc_opts->dst_opt_type) {
1629 	case TUNNEL_GENEVE_OPT:
1630 		err = fl_dump_key_geneve_opt(skb, enc_opts);
1631 		if (err)
1632 			goto nla_put_failure;
1633 		break;
1634 	default:
1635 		goto nla_put_failure;
1636 	}
1637 	nla_nest_end(skb, nest);
1638 	return 0;
1639 
1640 nla_put_failure:
1641 	nla_nest_cancel(skb, nest);
1642 	return -EMSGSIZE;
1643 }
1644 
1645 static int fl_dump_key_enc_opt(struct sk_buff *skb,
1646 			       struct flow_dissector_key_enc_opts *key_opts,
1647 			       struct flow_dissector_key_enc_opts *msk_opts)
1648 {
1649 	int err;
1650 
1651 	err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
1652 	if (err)
1653 		return err;
1654 
1655 	return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
1656 }
1657 
1658 static int fl_dump_key(struct sk_buff *skb, struct net *net,
1659 		       struct fl_flow_key *key, struct fl_flow_key *mask)
1660 {
1661 	if (mask->indev_ifindex) {
1662 		struct net_device *dev;
1663 
1664 		dev = __dev_get_by_index(net, key->indev_ifindex);
1665 		if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1666 			goto nla_put_failure;
1667 	}
1668 
1669 	if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1670 			    mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1671 			    sizeof(key->eth.dst)) ||
1672 	    fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1673 			    mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1674 			    sizeof(key->eth.src)) ||
1675 	    fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1676 			    &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1677 			    sizeof(key->basic.n_proto)))
1678 		goto nla_put_failure;
1679 
1680 	if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1681 		goto nla_put_failure;
1682 
1683 	if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
1684 			     TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
1685 		goto nla_put_failure;
1686 
1687 	if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
1688 			     TCA_FLOWER_KEY_CVLAN_PRIO,
1689 			     &key->cvlan, &mask->cvlan) ||
1690 	    (mask->cvlan.vlan_tpid &&
1691 	     nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1692 			  key->cvlan.vlan_tpid)))
1693 		goto nla_put_failure;
1694 
1695 	if (mask->basic.n_proto) {
1696 		if (mask->cvlan.vlan_tpid) {
1697 			if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1698 					 key->basic.n_proto))
1699 				goto nla_put_failure;
1700 		} else if (mask->vlan.vlan_tpid) {
1701 			if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1702 					 key->basic.n_proto))
1703 				goto nla_put_failure;
1704 		}
1705 	}
1706 
1707 	if ((key->basic.n_proto == htons(ETH_P_IP) ||
1708 	     key->basic.n_proto == htons(ETH_P_IPV6)) &&
1709 	    (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1710 			    &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1711 			    sizeof(key->basic.ip_proto)) ||
1712 	    fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
1713 		goto nla_put_failure;
1714 
1715 	if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1716 	    (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1717 			     &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1718 			     sizeof(key->ipv4.src)) ||
1719 	     fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1720 			     &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1721 			     sizeof(key->ipv4.dst))))
1722 		goto nla_put_failure;
1723 	else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1724 		 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1725 				  &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1726 				  sizeof(key->ipv6.src)) ||
1727 		  fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1728 				  &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1729 				  sizeof(key->ipv6.dst))))
1730 		goto nla_put_failure;
1731 
1732 	if (key->basic.ip_proto == IPPROTO_TCP &&
1733 	    (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1734 			     &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1735 			     sizeof(key->tp.src)) ||
1736 	     fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1737 			     &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1738 			     sizeof(key->tp.dst)) ||
1739 	     fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1740 			     &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1741 			     sizeof(key->tcp.flags))))
1742 		goto nla_put_failure;
1743 	else if (key->basic.ip_proto == IPPROTO_UDP &&
1744 		 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1745 				  &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1746 				  sizeof(key->tp.src)) ||
1747 		  fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1748 				  &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1749 				  sizeof(key->tp.dst))))
1750 		goto nla_put_failure;
1751 	else if (key->basic.ip_proto == IPPROTO_SCTP &&
1752 		 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1753 				  &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1754 				  sizeof(key->tp.src)) ||
1755 		  fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1756 				  &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1757 				  sizeof(key->tp.dst))))
1758 		goto nla_put_failure;
1759 	else if (key->basic.n_proto == htons(ETH_P_IP) &&
1760 		 key->basic.ip_proto == IPPROTO_ICMP &&
1761 		 (fl_dump_key_val(skb, &key->icmp.type,
1762 				  TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1763 				  TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1764 				  sizeof(key->icmp.type)) ||
1765 		  fl_dump_key_val(skb, &key->icmp.code,
1766 				  TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1767 				  TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1768 				  sizeof(key->icmp.code))))
1769 		goto nla_put_failure;
1770 	else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1771 		 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1772 		 (fl_dump_key_val(skb, &key->icmp.type,
1773 				  TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1774 				  TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1775 				  sizeof(key->icmp.type)) ||
1776 		  fl_dump_key_val(skb, &key->icmp.code,
1777 				  TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1778 				  TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1779 				  sizeof(key->icmp.code))))
1780 		goto nla_put_failure;
1781 	else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1782 		  key->basic.n_proto == htons(ETH_P_RARP)) &&
1783 		 (fl_dump_key_val(skb, &key->arp.sip,
1784 				  TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1785 				  TCA_FLOWER_KEY_ARP_SIP_MASK,
1786 				  sizeof(key->arp.sip)) ||
1787 		  fl_dump_key_val(skb, &key->arp.tip,
1788 				  TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1789 				  TCA_FLOWER_KEY_ARP_TIP_MASK,
1790 				  sizeof(key->arp.tip)) ||
1791 		  fl_dump_key_val(skb, &key->arp.op,
1792 				  TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1793 				  TCA_FLOWER_KEY_ARP_OP_MASK,
1794 				  sizeof(key->arp.op)) ||
1795 		  fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1796 				  mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1797 				  sizeof(key->arp.sha)) ||
1798 		  fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1799 				  mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1800 				  sizeof(key->arp.tha))))
1801 		goto nla_put_failure;
1802 
1803 	if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1804 	    (fl_dump_key_val(skb, &key->enc_ipv4.src,
1805 			    TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1806 			    TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1807 			    sizeof(key->enc_ipv4.src)) ||
1808 	     fl_dump_key_val(skb, &key->enc_ipv4.dst,
1809 			     TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1810 			     TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1811 			     sizeof(key->enc_ipv4.dst))))
1812 		goto nla_put_failure;
1813 	else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1814 		 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1815 			    TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1816 			    TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1817 			    sizeof(key->enc_ipv6.src)) ||
1818 		 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1819 				 TCA_FLOWER_KEY_ENC_IPV6_DST,
1820 				 &mask->enc_ipv6.dst,
1821 				 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1822 			    sizeof(key->enc_ipv6.dst))))
1823 		goto nla_put_failure;
1824 
1825 	if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
1826 			    &mask->enc_key_id, TCA_FLOWER_UNSPEC,
1827 			    sizeof(key->enc_key_id)) ||
1828 	    fl_dump_key_val(skb, &key->enc_tp.src,
1829 			    TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1830 			    &mask->enc_tp.src,
1831 			    TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1832 			    sizeof(key->enc_tp.src)) ||
1833 	    fl_dump_key_val(skb, &key->enc_tp.dst,
1834 			    TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1835 			    &mask->enc_tp.dst,
1836 			    TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1837 			    sizeof(key->enc_tp.dst)) ||
1838 	    fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
1839 	    fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
1840 		goto nla_put_failure;
1841 
1842 	if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1843 		goto nla_put_failure;
1844 
1845 	return 0;
1846 
1847 nla_put_failure:
1848 	return -EMSGSIZE;
1849 }
1850 
1851 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
1852 		   struct sk_buff *skb, struct tcmsg *t)
1853 {
1854 	struct cls_fl_filter *f = fh;
1855 	struct nlattr *nest;
1856 	struct fl_flow_key *key, *mask;
1857 
1858 	if (!f)
1859 		return skb->len;
1860 
1861 	t->tcm_handle = f->handle;
1862 
1863 	nest = nla_nest_start(skb, TCA_OPTIONS);
1864 	if (!nest)
1865 		goto nla_put_failure;
1866 
1867 	if (f->res.classid &&
1868 	    nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1869 		goto nla_put_failure;
1870 
1871 	key = &f->key;
1872 	mask = &f->mask->key;
1873 
1874 	if (fl_dump_key(skb, net, key, mask))
1875 		goto nla_put_failure;
1876 
1877 	if (!tc_skip_hw(f->flags))
1878 		fl_hw_update_stats(tp, f);
1879 
1880 	if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1881 		goto nla_put_failure;
1882 
1883 	if (tcf_exts_dump(skb, &f->exts))
1884 		goto nla_put_failure;
1885 
1886 	nla_nest_end(skb, nest);
1887 
1888 	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1889 		goto nla_put_failure;
1890 
1891 	return skb->len;
1892 
1893 nla_put_failure:
1894 	nla_nest_cancel(skb, nest);
1895 	return -1;
1896 }
1897 
1898 static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
1899 {
1900 	struct fl_flow_tmplt *tmplt = tmplt_priv;
1901 	struct fl_flow_key *key, *mask;
1902 	struct nlattr *nest;
1903 
1904 	nest = nla_nest_start(skb, TCA_OPTIONS);
1905 	if (!nest)
1906 		goto nla_put_failure;
1907 
1908 	key = &tmplt->dummy_key;
1909 	mask = &tmplt->mask;
1910 
1911 	if (fl_dump_key(skb, net, key, mask))
1912 		goto nla_put_failure;
1913 
1914 	nla_nest_end(skb, nest);
1915 
1916 	return skb->len;
1917 
1918 nla_put_failure:
1919 	nla_nest_cancel(skb, nest);
1920 	return -EMSGSIZE;
1921 }
1922 
1923 static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
1924 {
1925 	struct cls_fl_filter *f = fh;
1926 
1927 	if (f && f->res.classid == classid)
1928 		f->res.class = cl;
1929 }
1930 
1931 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1932 	.kind		= "flower",
1933 	.classify	= fl_classify,
1934 	.init		= fl_init,
1935 	.destroy	= fl_destroy,
1936 	.get		= fl_get,
1937 	.change		= fl_change,
1938 	.delete		= fl_delete,
1939 	.walk		= fl_walk,
1940 	.reoffload	= fl_reoffload,
1941 	.dump		= fl_dump,
1942 	.bind_class	= fl_bind_class,
1943 	.tmplt_create	= fl_tmplt_create,
1944 	.tmplt_destroy	= fl_tmplt_destroy,
1945 	.tmplt_dump	= fl_tmplt_dump,
1946 	.owner		= THIS_MODULE,
1947 };
1948 
1949 static int __init cls_fl_init(void)
1950 {
1951 	return register_tcf_proto_ops(&cls_fl_ops);
1952 }
1953 
1954 static void __exit cls_fl_exit(void)
1955 {
1956 	unregister_tcf_proto_ops(&cls_fl_ops);
1957 }
1958 
1959 module_init(cls_fl_init);
1960 module_exit(cls_fl_exit);
1961 
1962 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1963 MODULE_DESCRIPTION("Flower classifier");
1964 MODULE_LICENSE("GPL v2");
1965