xref: /openbmc/linux/net/sched/cls_flower.c (revision a0ae2562c6c4b2721d9fddba63b7286c13517d9f)
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 
28 #include <net/dst.h>
29 #include <net/dst_metadata.h>
30 
31 struct fl_flow_key {
32 	int	indev_ifindex;
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 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
56 
57 struct fl_flow_mask_range {
58 	unsigned short int start;
59 	unsigned short int end;
60 };
61 
62 struct fl_flow_mask {
63 	struct fl_flow_key key;
64 	struct fl_flow_mask_range range;
65 	struct rhash_head ht_node;
66 	struct rhashtable ht;
67 	struct rhashtable_params filter_ht_params;
68 	struct flow_dissector dissector;
69 	struct list_head filters;
70 	struct rcu_work rwork;
71 	struct list_head list;
72 };
73 
74 struct cls_fl_head {
75 	struct rhashtable ht;
76 	struct list_head masks;
77 	struct rcu_work rwork;
78 	struct idr handle_idr;
79 };
80 
81 struct cls_fl_filter {
82 	struct fl_flow_mask *mask;
83 	struct rhash_head ht_node;
84 	struct fl_flow_key mkey;
85 	struct tcf_exts exts;
86 	struct tcf_result res;
87 	struct fl_flow_key key;
88 	struct list_head list;
89 	u32 handle;
90 	u32 flags;
91 	unsigned int in_hw_count;
92 	struct rcu_work rwork;
93 	struct net_device *hw_dev;
94 };
95 
96 static const struct rhashtable_params mask_ht_params = {
97 	.key_offset = offsetof(struct fl_flow_mask, key),
98 	.key_len = sizeof(struct fl_flow_key),
99 	.head_offset = offsetof(struct fl_flow_mask, ht_node),
100 	.automatic_shrinking = true,
101 };
102 
103 static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
104 {
105 	return mask->range.end - mask->range.start;
106 }
107 
108 static void fl_mask_update_range(struct fl_flow_mask *mask)
109 {
110 	const u8 *bytes = (const u8 *) &mask->key;
111 	size_t size = sizeof(mask->key);
112 	size_t i, first = 0, last;
113 
114 	for (i = 0; i < size; i++) {
115 		if (bytes[i]) {
116 			first = i;
117 			break;
118 		}
119 	}
120 	last = first;
121 	for (i = size - 1; i != first; i--) {
122 		if (bytes[i]) {
123 			last = i;
124 			break;
125 		}
126 	}
127 	mask->range.start = rounddown(first, sizeof(long));
128 	mask->range.end = roundup(last + 1, sizeof(long));
129 }
130 
131 static void *fl_key_get_start(struct fl_flow_key *key,
132 			      const struct fl_flow_mask *mask)
133 {
134 	return (u8 *) key + mask->range.start;
135 }
136 
137 static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
138 			      struct fl_flow_mask *mask)
139 {
140 	const long *lkey = fl_key_get_start(key, mask);
141 	const long *lmask = fl_key_get_start(&mask->key, mask);
142 	long *lmkey = fl_key_get_start(mkey, mask);
143 	int i;
144 
145 	for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
146 		*lmkey++ = *lkey++ & *lmask++;
147 }
148 
149 static void fl_clear_masked_range(struct fl_flow_key *key,
150 				  struct fl_flow_mask *mask)
151 {
152 	memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
153 }
154 
155 static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
156 				       struct fl_flow_key *mkey)
157 {
158 	return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
159 				      mask->filter_ht_params);
160 }
161 
162 static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
163 		       struct tcf_result *res)
164 {
165 	struct cls_fl_head *head = rcu_dereference_bh(tp->root);
166 	struct cls_fl_filter *f;
167 	struct fl_flow_mask *mask;
168 	struct fl_flow_key skb_key;
169 	struct fl_flow_key skb_mkey;
170 
171 	list_for_each_entry_rcu(mask, &head->masks, list) {
172 		fl_clear_masked_range(&skb_key, mask);
173 
174 		skb_key.indev_ifindex = skb->skb_iif;
175 		/* skb_flow_dissect() does not set n_proto in case an unknown
176 		 * protocol, so do it rather here.
177 		 */
178 		skb_key.basic.n_proto = skb->protocol;
179 		skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
180 		skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
181 
182 		fl_set_masked_key(&skb_mkey, &skb_key, mask);
183 
184 		f = fl_lookup(mask, &skb_mkey);
185 		if (f && !tc_skip_sw(f->flags)) {
186 			*res = f->res;
187 			return tcf_exts_exec(skb, &f->exts, res);
188 		}
189 	}
190 	return -1;
191 }
192 
193 static int fl_init(struct tcf_proto *tp)
194 {
195 	struct cls_fl_head *head;
196 
197 	head = kzalloc(sizeof(*head), GFP_KERNEL);
198 	if (!head)
199 		return -ENOBUFS;
200 
201 	INIT_LIST_HEAD_RCU(&head->masks);
202 	rcu_assign_pointer(tp->root, head);
203 	idr_init(&head->handle_idr);
204 
205 	return rhashtable_init(&head->ht, &mask_ht_params);
206 }
207 
208 static void fl_mask_free(struct fl_flow_mask *mask)
209 {
210 	rhashtable_destroy(&mask->ht);
211 	kfree(mask);
212 }
213 
214 static void fl_mask_free_work(struct work_struct *work)
215 {
216 	struct fl_flow_mask *mask = container_of(to_rcu_work(work),
217 						 struct fl_flow_mask, rwork);
218 
219 	fl_mask_free(mask);
220 }
221 
222 static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask,
223 			bool async)
224 {
225 	if (!list_empty(&mask->filters))
226 		return false;
227 
228 	rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
229 	list_del_rcu(&mask->list);
230 	if (async)
231 		tcf_queue_work(&mask->rwork, fl_mask_free_work);
232 	else
233 		fl_mask_free(mask);
234 
235 	return true;
236 }
237 
238 static void __fl_destroy_filter(struct cls_fl_filter *f)
239 {
240 	tcf_exts_destroy(&f->exts);
241 	tcf_exts_put_net(&f->exts);
242 	kfree(f);
243 }
244 
245 static void fl_destroy_filter_work(struct work_struct *work)
246 {
247 	struct cls_fl_filter *f = container_of(to_rcu_work(work),
248 					struct cls_fl_filter, rwork);
249 
250 	rtnl_lock();
251 	__fl_destroy_filter(f);
252 	rtnl_unlock();
253 }
254 
255 static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
256 				 struct netlink_ext_ack *extack)
257 {
258 	struct tc_cls_flower_offload cls_flower = {};
259 	struct tcf_block *block = tp->chain->block;
260 
261 	tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
262 	cls_flower.command = TC_CLSFLOWER_DESTROY;
263 	cls_flower.cookie = (unsigned long) f;
264 
265 	tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
266 			 &cls_flower, false);
267 	tcf_block_offload_dec(block, &f->flags);
268 }
269 
270 static int fl_hw_replace_filter(struct tcf_proto *tp,
271 				struct cls_fl_filter *f,
272 				struct netlink_ext_ack *extack)
273 {
274 	struct tc_cls_flower_offload cls_flower = {};
275 	struct tcf_block *block = tp->chain->block;
276 	bool skip_sw = tc_skip_sw(f->flags);
277 	int err;
278 
279 	tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
280 	cls_flower.command = TC_CLSFLOWER_REPLACE;
281 	cls_flower.cookie = (unsigned long) f;
282 	cls_flower.dissector = &f->mask->dissector;
283 	cls_flower.mask = &f->mask->key;
284 	cls_flower.key = &f->mkey;
285 	cls_flower.exts = &f->exts;
286 	cls_flower.classid = f->res.classid;
287 
288 	err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
289 			       &cls_flower, skip_sw);
290 	if (err < 0) {
291 		fl_hw_destroy_filter(tp, f, NULL);
292 		return err;
293 	} else if (err > 0) {
294 		f->in_hw_count = err;
295 		tcf_block_offload_inc(block, &f->flags);
296 	}
297 
298 	if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
299 		return -EINVAL;
300 
301 	return 0;
302 }
303 
304 static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
305 {
306 	struct tc_cls_flower_offload cls_flower = {};
307 	struct tcf_block *block = tp->chain->block;
308 
309 	tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
310 	cls_flower.command = TC_CLSFLOWER_STATS;
311 	cls_flower.cookie = (unsigned long) f;
312 	cls_flower.exts = &f->exts;
313 	cls_flower.classid = f->res.classid;
314 
315 	tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
316 			 &cls_flower, false);
317 }
318 
319 static bool __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
320 			struct netlink_ext_ack *extack)
321 {
322 	struct cls_fl_head *head = rtnl_dereference(tp->root);
323 	bool async = tcf_exts_get_net(&f->exts);
324 	bool last;
325 
326 	idr_remove(&head->handle_idr, f->handle);
327 	list_del_rcu(&f->list);
328 	last = fl_mask_put(head, f->mask, async);
329 	if (!tc_skip_hw(f->flags))
330 		fl_hw_destroy_filter(tp, f, extack);
331 	tcf_unbind_filter(tp, &f->res);
332 	if (async)
333 		tcf_queue_work(&f->rwork, fl_destroy_filter_work);
334 	else
335 		__fl_destroy_filter(f);
336 
337 	return last;
338 }
339 
340 static void fl_destroy_sleepable(struct work_struct *work)
341 {
342 	struct cls_fl_head *head = container_of(to_rcu_work(work),
343 						struct cls_fl_head,
344 						rwork);
345 
346 	rhashtable_destroy(&head->ht);
347 	kfree(head);
348 	module_put(THIS_MODULE);
349 }
350 
351 static void fl_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
352 {
353 	struct cls_fl_head *head = rtnl_dereference(tp->root);
354 	struct fl_flow_mask *mask, *next_mask;
355 	struct cls_fl_filter *f, *next;
356 
357 	list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
358 		list_for_each_entry_safe(f, next, &mask->filters, list) {
359 			if (__fl_delete(tp, f, extack))
360 				break;
361 		}
362 	}
363 	idr_destroy(&head->handle_idr);
364 
365 	__module_get(THIS_MODULE);
366 	tcf_queue_work(&head->rwork, fl_destroy_sleepable);
367 }
368 
369 static void *fl_get(struct tcf_proto *tp, u32 handle)
370 {
371 	struct cls_fl_head *head = rtnl_dereference(tp->root);
372 
373 	return idr_find(&head->handle_idr, handle);
374 }
375 
376 static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
377 	[TCA_FLOWER_UNSPEC]		= { .type = NLA_UNSPEC },
378 	[TCA_FLOWER_CLASSID]		= { .type = NLA_U32 },
379 	[TCA_FLOWER_INDEV]		= { .type = NLA_STRING,
380 					    .len = IFNAMSIZ },
381 	[TCA_FLOWER_KEY_ETH_DST]	= { .len = ETH_ALEN },
382 	[TCA_FLOWER_KEY_ETH_DST_MASK]	= { .len = ETH_ALEN },
383 	[TCA_FLOWER_KEY_ETH_SRC]	= { .len = ETH_ALEN },
384 	[TCA_FLOWER_KEY_ETH_SRC_MASK]	= { .len = ETH_ALEN },
385 	[TCA_FLOWER_KEY_ETH_TYPE]	= { .type = NLA_U16 },
386 	[TCA_FLOWER_KEY_IP_PROTO]	= { .type = NLA_U8 },
387 	[TCA_FLOWER_KEY_IPV4_SRC]	= { .type = NLA_U32 },
388 	[TCA_FLOWER_KEY_IPV4_SRC_MASK]	= { .type = NLA_U32 },
389 	[TCA_FLOWER_KEY_IPV4_DST]	= { .type = NLA_U32 },
390 	[TCA_FLOWER_KEY_IPV4_DST_MASK]	= { .type = NLA_U32 },
391 	[TCA_FLOWER_KEY_IPV6_SRC]	= { .len = sizeof(struct in6_addr) },
392 	[TCA_FLOWER_KEY_IPV6_SRC_MASK]	= { .len = sizeof(struct in6_addr) },
393 	[TCA_FLOWER_KEY_IPV6_DST]	= { .len = sizeof(struct in6_addr) },
394 	[TCA_FLOWER_KEY_IPV6_DST_MASK]	= { .len = sizeof(struct in6_addr) },
395 	[TCA_FLOWER_KEY_TCP_SRC]	= { .type = NLA_U16 },
396 	[TCA_FLOWER_KEY_TCP_DST]	= { .type = NLA_U16 },
397 	[TCA_FLOWER_KEY_UDP_SRC]	= { .type = NLA_U16 },
398 	[TCA_FLOWER_KEY_UDP_DST]	= { .type = NLA_U16 },
399 	[TCA_FLOWER_KEY_VLAN_ID]	= { .type = NLA_U16 },
400 	[TCA_FLOWER_KEY_VLAN_PRIO]	= { .type = NLA_U8 },
401 	[TCA_FLOWER_KEY_VLAN_ETH_TYPE]	= { .type = NLA_U16 },
402 	[TCA_FLOWER_KEY_ENC_KEY_ID]	= { .type = NLA_U32 },
403 	[TCA_FLOWER_KEY_ENC_IPV4_SRC]	= { .type = NLA_U32 },
404 	[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
405 	[TCA_FLOWER_KEY_ENC_IPV4_DST]	= { .type = NLA_U32 },
406 	[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
407 	[TCA_FLOWER_KEY_ENC_IPV6_SRC]	= { .len = sizeof(struct in6_addr) },
408 	[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
409 	[TCA_FLOWER_KEY_ENC_IPV6_DST]	= { .len = sizeof(struct in6_addr) },
410 	[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
411 	[TCA_FLOWER_KEY_TCP_SRC_MASK]	= { .type = NLA_U16 },
412 	[TCA_FLOWER_KEY_TCP_DST_MASK]	= { .type = NLA_U16 },
413 	[TCA_FLOWER_KEY_UDP_SRC_MASK]	= { .type = NLA_U16 },
414 	[TCA_FLOWER_KEY_UDP_DST_MASK]	= { .type = NLA_U16 },
415 	[TCA_FLOWER_KEY_SCTP_SRC_MASK]	= { .type = NLA_U16 },
416 	[TCA_FLOWER_KEY_SCTP_DST_MASK]	= { .type = NLA_U16 },
417 	[TCA_FLOWER_KEY_SCTP_SRC]	= { .type = NLA_U16 },
418 	[TCA_FLOWER_KEY_SCTP_DST]	= { .type = NLA_U16 },
419 	[TCA_FLOWER_KEY_ENC_UDP_SRC_PORT]	= { .type = NLA_U16 },
420 	[TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK]	= { .type = NLA_U16 },
421 	[TCA_FLOWER_KEY_ENC_UDP_DST_PORT]	= { .type = NLA_U16 },
422 	[TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK]	= { .type = NLA_U16 },
423 	[TCA_FLOWER_KEY_FLAGS]		= { .type = NLA_U32 },
424 	[TCA_FLOWER_KEY_FLAGS_MASK]	= { .type = NLA_U32 },
425 	[TCA_FLOWER_KEY_ICMPV4_TYPE]	= { .type = NLA_U8 },
426 	[TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
427 	[TCA_FLOWER_KEY_ICMPV4_CODE]	= { .type = NLA_U8 },
428 	[TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
429 	[TCA_FLOWER_KEY_ICMPV6_TYPE]	= { .type = NLA_U8 },
430 	[TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
431 	[TCA_FLOWER_KEY_ICMPV6_CODE]	= { .type = NLA_U8 },
432 	[TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
433 	[TCA_FLOWER_KEY_ARP_SIP]	= { .type = NLA_U32 },
434 	[TCA_FLOWER_KEY_ARP_SIP_MASK]	= { .type = NLA_U32 },
435 	[TCA_FLOWER_KEY_ARP_TIP]	= { .type = NLA_U32 },
436 	[TCA_FLOWER_KEY_ARP_TIP_MASK]	= { .type = NLA_U32 },
437 	[TCA_FLOWER_KEY_ARP_OP]		= { .type = NLA_U8 },
438 	[TCA_FLOWER_KEY_ARP_OP_MASK]	= { .type = NLA_U8 },
439 	[TCA_FLOWER_KEY_ARP_SHA]	= { .len = ETH_ALEN },
440 	[TCA_FLOWER_KEY_ARP_SHA_MASK]	= { .len = ETH_ALEN },
441 	[TCA_FLOWER_KEY_ARP_THA]	= { .len = ETH_ALEN },
442 	[TCA_FLOWER_KEY_ARP_THA_MASK]	= { .len = ETH_ALEN },
443 	[TCA_FLOWER_KEY_MPLS_TTL]	= { .type = NLA_U8 },
444 	[TCA_FLOWER_KEY_MPLS_BOS]	= { .type = NLA_U8 },
445 	[TCA_FLOWER_KEY_MPLS_TC]	= { .type = NLA_U8 },
446 	[TCA_FLOWER_KEY_MPLS_LABEL]	= { .type = NLA_U32 },
447 	[TCA_FLOWER_KEY_TCP_FLAGS]	= { .type = NLA_U16 },
448 	[TCA_FLOWER_KEY_TCP_FLAGS_MASK]	= { .type = NLA_U16 },
449 	[TCA_FLOWER_KEY_IP_TOS]		= { .type = NLA_U8 },
450 	[TCA_FLOWER_KEY_IP_TOS_MASK]	= { .type = NLA_U8 },
451 	[TCA_FLOWER_KEY_IP_TTL]		= { .type = NLA_U8 },
452 	[TCA_FLOWER_KEY_IP_TTL_MASK]	= { .type = NLA_U8 },
453 	[TCA_FLOWER_KEY_CVLAN_ID]	= { .type = NLA_U16 },
454 	[TCA_FLOWER_KEY_CVLAN_PRIO]	= { .type = NLA_U8 },
455 	[TCA_FLOWER_KEY_CVLAN_ETH_TYPE]	= { .type = NLA_U16 },
456 };
457 
458 static void fl_set_key_val(struct nlattr **tb,
459 			   void *val, int val_type,
460 			   void *mask, int mask_type, int len)
461 {
462 	if (!tb[val_type])
463 		return;
464 	memcpy(val, nla_data(tb[val_type]), len);
465 	if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
466 		memset(mask, 0xff, len);
467 	else
468 		memcpy(mask, nla_data(tb[mask_type]), len);
469 }
470 
471 static int fl_set_key_mpls(struct nlattr **tb,
472 			   struct flow_dissector_key_mpls *key_val,
473 			   struct flow_dissector_key_mpls *key_mask)
474 {
475 	if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
476 		key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
477 		key_mask->mpls_ttl = MPLS_TTL_MASK;
478 	}
479 	if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
480 		u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
481 
482 		if (bos & ~MPLS_BOS_MASK)
483 			return -EINVAL;
484 		key_val->mpls_bos = bos;
485 		key_mask->mpls_bos = MPLS_BOS_MASK;
486 	}
487 	if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
488 		u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
489 
490 		if (tc & ~MPLS_TC_MASK)
491 			return -EINVAL;
492 		key_val->mpls_tc = tc;
493 		key_mask->mpls_tc = MPLS_TC_MASK;
494 	}
495 	if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
496 		u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
497 
498 		if (label & ~MPLS_LABEL_MASK)
499 			return -EINVAL;
500 		key_val->mpls_label = label;
501 		key_mask->mpls_label = MPLS_LABEL_MASK;
502 	}
503 	return 0;
504 }
505 
506 static void fl_set_key_vlan(struct nlattr **tb,
507 			    __be16 ethertype,
508 			    int vlan_id_key, int vlan_prio_key,
509 			    struct flow_dissector_key_vlan *key_val,
510 			    struct flow_dissector_key_vlan *key_mask)
511 {
512 #define VLAN_PRIORITY_MASK	0x7
513 
514 	if (tb[vlan_id_key]) {
515 		key_val->vlan_id =
516 			nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
517 		key_mask->vlan_id = VLAN_VID_MASK;
518 	}
519 	if (tb[vlan_prio_key]) {
520 		key_val->vlan_priority =
521 			nla_get_u8(tb[vlan_prio_key]) &
522 			VLAN_PRIORITY_MASK;
523 		key_mask->vlan_priority = VLAN_PRIORITY_MASK;
524 	}
525 	key_val->vlan_tpid = ethertype;
526 	key_mask->vlan_tpid = cpu_to_be16(~0);
527 }
528 
529 static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
530 			    u32 *dissector_key, u32 *dissector_mask,
531 			    u32 flower_flag_bit, u32 dissector_flag_bit)
532 {
533 	if (flower_mask & flower_flag_bit) {
534 		*dissector_mask |= dissector_flag_bit;
535 		if (flower_key & flower_flag_bit)
536 			*dissector_key |= dissector_flag_bit;
537 	}
538 }
539 
540 static int fl_set_key_flags(struct nlattr **tb,
541 			    u32 *flags_key, u32 *flags_mask)
542 {
543 	u32 key, mask;
544 
545 	/* mask is mandatory for flags */
546 	if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
547 		return -EINVAL;
548 
549 	key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
550 	mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
551 
552 	*flags_key  = 0;
553 	*flags_mask = 0;
554 
555 	fl_set_key_flag(key, mask, flags_key, flags_mask,
556 			TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
557 	fl_set_key_flag(key, mask, flags_key, flags_mask,
558 			TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
559 			FLOW_DIS_FIRST_FRAG);
560 
561 	return 0;
562 }
563 
564 static void fl_set_key_ip(struct nlattr **tb,
565 			  struct flow_dissector_key_ip *key,
566 			  struct flow_dissector_key_ip *mask)
567 {
568 		fl_set_key_val(tb, &key->tos, TCA_FLOWER_KEY_IP_TOS,
569 			       &mask->tos, TCA_FLOWER_KEY_IP_TOS_MASK,
570 			       sizeof(key->tos));
571 
572 		fl_set_key_val(tb, &key->ttl, TCA_FLOWER_KEY_IP_TTL,
573 			       &mask->ttl, TCA_FLOWER_KEY_IP_TTL_MASK,
574 			       sizeof(key->ttl));
575 }
576 
577 static int fl_set_key(struct net *net, struct nlattr **tb,
578 		      struct fl_flow_key *key, struct fl_flow_key *mask,
579 		      struct netlink_ext_ack *extack)
580 {
581 	__be16 ethertype;
582 	int ret = 0;
583 #ifdef CONFIG_NET_CLS_IND
584 	if (tb[TCA_FLOWER_INDEV]) {
585 		int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
586 		if (err < 0)
587 			return err;
588 		key->indev_ifindex = err;
589 		mask->indev_ifindex = 0xffffffff;
590 	}
591 #endif
592 
593 	fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
594 		       mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
595 		       sizeof(key->eth.dst));
596 	fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
597 		       mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
598 		       sizeof(key->eth.src));
599 
600 	if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
601 		ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
602 
603 		if (eth_type_vlan(ethertype)) {
604 			fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
605 					TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan,
606 					&mask->vlan);
607 
608 			if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
609 				ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
610 				if (eth_type_vlan(ethertype)) {
611 					fl_set_key_vlan(tb, ethertype,
612 							TCA_FLOWER_KEY_CVLAN_ID,
613 							TCA_FLOWER_KEY_CVLAN_PRIO,
614 							&key->cvlan, &mask->cvlan);
615 					fl_set_key_val(tb, &key->basic.n_proto,
616 						       TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
617 						       &mask->basic.n_proto,
618 						       TCA_FLOWER_UNSPEC,
619 						       sizeof(key->basic.n_proto));
620 				} else {
621 					key->basic.n_proto = ethertype;
622 					mask->basic.n_proto = cpu_to_be16(~0);
623 				}
624 			}
625 		} else {
626 			key->basic.n_proto = ethertype;
627 			mask->basic.n_proto = cpu_to_be16(~0);
628 		}
629 	}
630 
631 	if (key->basic.n_proto == htons(ETH_P_IP) ||
632 	    key->basic.n_proto == htons(ETH_P_IPV6)) {
633 		fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
634 			       &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
635 			       sizeof(key->basic.ip_proto));
636 		fl_set_key_ip(tb, &key->ip, &mask->ip);
637 	}
638 
639 	if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
640 		key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
641 		mask->control.addr_type = ~0;
642 		fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
643 			       &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
644 			       sizeof(key->ipv4.src));
645 		fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
646 			       &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
647 			       sizeof(key->ipv4.dst));
648 	} else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
649 		key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
650 		mask->control.addr_type = ~0;
651 		fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
652 			       &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
653 			       sizeof(key->ipv6.src));
654 		fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
655 			       &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
656 			       sizeof(key->ipv6.dst));
657 	}
658 
659 	if (key->basic.ip_proto == IPPROTO_TCP) {
660 		fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
661 			       &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
662 			       sizeof(key->tp.src));
663 		fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
664 			       &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
665 			       sizeof(key->tp.dst));
666 		fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
667 			       &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
668 			       sizeof(key->tcp.flags));
669 	} else if (key->basic.ip_proto == IPPROTO_UDP) {
670 		fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
671 			       &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
672 			       sizeof(key->tp.src));
673 		fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
674 			       &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
675 			       sizeof(key->tp.dst));
676 	} else if (key->basic.ip_proto == IPPROTO_SCTP) {
677 		fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
678 			       &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
679 			       sizeof(key->tp.src));
680 		fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
681 			       &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
682 			       sizeof(key->tp.dst));
683 	} else if (key->basic.n_proto == htons(ETH_P_IP) &&
684 		   key->basic.ip_proto == IPPROTO_ICMP) {
685 		fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
686 			       &mask->icmp.type,
687 			       TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
688 			       sizeof(key->icmp.type));
689 		fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
690 			       &mask->icmp.code,
691 			       TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
692 			       sizeof(key->icmp.code));
693 	} else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
694 		   key->basic.ip_proto == IPPROTO_ICMPV6) {
695 		fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
696 			       &mask->icmp.type,
697 			       TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
698 			       sizeof(key->icmp.type));
699 		fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
700 			       &mask->icmp.code,
701 			       TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
702 			       sizeof(key->icmp.code));
703 	} else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
704 		   key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
705 		ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
706 		if (ret)
707 			return ret;
708 	} else if (key->basic.n_proto == htons(ETH_P_ARP) ||
709 		   key->basic.n_proto == htons(ETH_P_RARP)) {
710 		fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
711 			       &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
712 			       sizeof(key->arp.sip));
713 		fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
714 			       &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
715 			       sizeof(key->arp.tip));
716 		fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
717 			       &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
718 			       sizeof(key->arp.op));
719 		fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
720 			       mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
721 			       sizeof(key->arp.sha));
722 		fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
723 			       mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
724 			       sizeof(key->arp.tha));
725 	}
726 
727 	if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
728 	    tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
729 		key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
730 		mask->enc_control.addr_type = ~0;
731 		fl_set_key_val(tb, &key->enc_ipv4.src,
732 			       TCA_FLOWER_KEY_ENC_IPV4_SRC,
733 			       &mask->enc_ipv4.src,
734 			       TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
735 			       sizeof(key->enc_ipv4.src));
736 		fl_set_key_val(tb, &key->enc_ipv4.dst,
737 			       TCA_FLOWER_KEY_ENC_IPV4_DST,
738 			       &mask->enc_ipv4.dst,
739 			       TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
740 			       sizeof(key->enc_ipv4.dst));
741 	}
742 
743 	if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
744 	    tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
745 		key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
746 		mask->enc_control.addr_type = ~0;
747 		fl_set_key_val(tb, &key->enc_ipv6.src,
748 			       TCA_FLOWER_KEY_ENC_IPV6_SRC,
749 			       &mask->enc_ipv6.src,
750 			       TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
751 			       sizeof(key->enc_ipv6.src));
752 		fl_set_key_val(tb, &key->enc_ipv6.dst,
753 			       TCA_FLOWER_KEY_ENC_IPV6_DST,
754 			       &mask->enc_ipv6.dst,
755 			       TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
756 			       sizeof(key->enc_ipv6.dst));
757 	}
758 
759 	fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
760 		       &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
761 		       sizeof(key->enc_key_id.keyid));
762 
763 	fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
764 		       &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
765 		       sizeof(key->enc_tp.src));
766 
767 	fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
768 		       &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
769 		       sizeof(key->enc_tp.dst));
770 
771 	if (tb[TCA_FLOWER_KEY_FLAGS])
772 		ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
773 
774 	return ret;
775 }
776 
777 static void fl_mask_copy(struct fl_flow_mask *dst,
778 			 struct fl_flow_mask *src)
779 {
780 	const void *psrc = fl_key_get_start(&src->key, src);
781 	void *pdst = fl_key_get_start(&dst->key, src);
782 
783 	memcpy(pdst, psrc, fl_mask_range(src));
784 	dst->range = src->range;
785 }
786 
787 static const struct rhashtable_params fl_ht_params = {
788 	.key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
789 	.head_offset = offsetof(struct cls_fl_filter, ht_node),
790 	.automatic_shrinking = true,
791 };
792 
793 static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
794 {
795 	mask->filter_ht_params = fl_ht_params;
796 	mask->filter_ht_params.key_len = fl_mask_range(mask);
797 	mask->filter_ht_params.key_offset += mask->range.start;
798 
799 	return rhashtable_init(&mask->ht, &mask->filter_ht_params);
800 }
801 
802 #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
803 #define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
804 
805 #define FL_KEY_IS_MASKED(mask, member)						\
806 	memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member),		\
807 		   0, FL_KEY_MEMBER_SIZE(member))				\
808 
809 #define FL_KEY_SET(keys, cnt, id, member)					\
810 	do {									\
811 		keys[cnt].key_id = id;						\
812 		keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member);		\
813 		cnt++;								\
814 	} while(0);
815 
816 #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member)			\
817 	do {									\
818 		if (FL_KEY_IS_MASKED(mask, member))				\
819 			FL_KEY_SET(keys, cnt, id, member);			\
820 	} while(0);
821 
822 static void fl_init_dissector(struct fl_flow_mask *mask)
823 {
824 	struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
825 	size_t cnt = 0;
826 
827 	FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
828 	FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
829 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
830 			     FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
831 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
832 			     FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
833 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
834 			     FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
835 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
836 			     FLOW_DISSECTOR_KEY_PORTS, tp);
837 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
838 			     FLOW_DISSECTOR_KEY_IP, ip);
839 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
840 			     FLOW_DISSECTOR_KEY_TCP, tcp);
841 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
842 			     FLOW_DISSECTOR_KEY_ICMP, icmp);
843 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
844 			     FLOW_DISSECTOR_KEY_ARP, arp);
845 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
846 			     FLOW_DISSECTOR_KEY_MPLS, mpls);
847 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
848 			     FLOW_DISSECTOR_KEY_VLAN, vlan);
849 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
850 			     FLOW_DISSECTOR_KEY_CVLAN, cvlan);
851 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
852 			     FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
853 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
854 			     FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
855 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
856 			     FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
857 	if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
858 	    FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
859 		FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
860 			   enc_control);
861 	FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
862 			     FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
863 
864 	skb_flow_dissector_init(&mask->dissector, keys, cnt);
865 }
866 
867 static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
868 					       struct fl_flow_mask *mask)
869 {
870 	struct fl_flow_mask *newmask;
871 	int err;
872 
873 	newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
874 	if (!newmask)
875 		return ERR_PTR(-ENOMEM);
876 
877 	fl_mask_copy(newmask, mask);
878 
879 	err = fl_init_mask_hashtable(newmask);
880 	if (err)
881 		goto errout_free;
882 
883 	fl_init_dissector(newmask);
884 
885 	INIT_LIST_HEAD_RCU(&newmask->filters);
886 
887 	err = rhashtable_insert_fast(&head->ht, &newmask->ht_node,
888 				     mask_ht_params);
889 	if (err)
890 		goto errout_destroy;
891 
892 	list_add_tail_rcu(&newmask->list, &head->masks);
893 
894 	return newmask;
895 
896 errout_destroy:
897 	rhashtable_destroy(&newmask->ht);
898 errout_free:
899 	kfree(newmask);
900 
901 	return ERR_PTR(err);
902 }
903 
904 static int fl_check_assign_mask(struct cls_fl_head *head,
905 				struct cls_fl_filter *fnew,
906 				struct cls_fl_filter *fold,
907 				struct fl_flow_mask *mask)
908 {
909 	struct fl_flow_mask *newmask;
910 
911 	fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params);
912 	if (!fnew->mask) {
913 		if (fold)
914 			return -EINVAL;
915 
916 		newmask = fl_create_new_mask(head, mask);
917 		if (IS_ERR(newmask))
918 			return PTR_ERR(newmask);
919 
920 		fnew->mask = newmask;
921 	} else if (fold && fold->mask != fnew->mask) {
922 		return -EINVAL;
923 	}
924 
925 	return 0;
926 }
927 
928 static int fl_set_parms(struct net *net, struct tcf_proto *tp,
929 			struct cls_fl_filter *f, struct fl_flow_mask *mask,
930 			unsigned long base, struct nlattr **tb,
931 			struct nlattr *est, bool ovr,
932 			struct netlink_ext_ack *extack)
933 {
934 	int err;
935 
936 	err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
937 	if (err < 0)
938 		return err;
939 
940 	if (tb[TCA_FLOWER_CLASSID]) {
941 		f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
942 		tcf_bind_filter(tp, &f->res, base);
943 	}
944 
945 	err = fl_set_key(net, tb, &f->key, &mask->key, extack);
946 	if (err)
947 		return err;
948 
949 	fl_mask_update_range(mask);
950 	fl_set_masked_key(&f->mkey, &f->key, mask);
951 
952 	return 0;
953 }
954 
955 static int fl_change(struct net *net, struct sk_buff *in_skb,
956 		     struct tcf_proto *tp, unsigned long base,
957 		     u32 handle, struct nlattr **tca,
958 		     void **arg, bool ovr, struct netlink_ext_ack *extack)
959 {
960 	struct cls_fl_head *head = rtnl_dereference(tp->root);
961 	struct cls_fl_filter *fold = *arg;
962 	struct cls_fl_filter *fnew;
963 	struct nlattr **tb;
964 	struct fl_flow_mask mask = {};
965 	int err;
966 
967 	if (!tca[TCA_OPTIONS])
968 		return -EINVAL;
969 
970 	tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
971 	if (!tb)
972 		return -ENOBUFS;
973 
974 	err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
975 			       fl_policy, NULL);
976 	if (err < 0)
977 		goto errout_tb;
978 
979 	if (fold && handle && fold->handle != handle) {
980 		err = -EINVAL;
981 		goto errout_tb;
982 	}
983 
984 	fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
985 	if (!fnew) {
986 		err = -ENOBUFS;
987 		goto errout_tb;
988 	}
989 
990 	err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
991 	if (err < 0)
992 		goto errout;
993 
994 	if (!handle) {
995 		handle = 1;
996 		err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
997 				    INT_MAX, GFP_KERNEL);
998 	} else if (!fold) {
999 		/* user specifies a handle and it doesn't exist */
1000 		err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1001 				    handle, GFP_KERNEL);
1002 	}
1003 	if (err)
1004 		goto errout;
1005 	fnew->handle = handle;
1006 
1007 	if (tb[TCA_FLOWER_FLAGS]) {
1008 		fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
1009 
1010 		if (!tc_flags_valid(fnew->flags)) {
1011 			err = -EINVAL;
1012 			goto errout_idr;
1013 		}
1014 	}
1015 
1016 	err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr,
1017 			   extack);
1018 	if (err)
1019 		goto errout_idr;
1020 
1021 	err = fl_check_assign_mask(head, fnew, fold, &mask);
1022 	if (err)
1023 		goto errout_idr;
1024 
1025 	if (!tc_skip_sw(fnew->flags)) {
1026 		if (!fold && fl_lookup(fnew->mask, &fnew->mkey)) {
1027 			err = -EEXIST;
1028 			goto errout_mask;
1029 		}
1030 
1031 		err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node,
1032 					     fnew->mask->filter_ht_params);
1033 		if (err)
1034 			goto errout_mask;
1035 	}
1036 
1037 	if (!tc_skip_hw(fnew->flags)) {
1038 		err = fl_hw_replace_filter(tp, fnew, extack);
1039 		if (err)
1040 			goto errout_mask;
1041 	}
1042 
1043 	if (!tc_in_hw(fnew->flags))
1044 		fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1045 
1046 	if (fold) {
1047 		if (!tc_skip_sw(fold->flags))
1048 			rhashtable_remove_fast(&fold->mask->ht,
1049 					       &fold->ht_node,
1050 					       fold->mask->filter_ht_params);
1051 		if (!tc_skip_hw(fold->flags))
1052 			fl_hw_destroy_filter(tp, fold, NULL);
1053 	}
1054 
1055 	*arg = fnew;
1056 
1057 	if (fold) {
1058 		idr_replace(&head->handle_idr, fnew, fnew->handle);
1059 		list_replace_rcu(&fold->list, &fnew->list);
1060 		tcf_unbind_filter(tp, &fold->res);
1061 		tcf_exts_get_net(&fold->exts);
1062 		tcf_queue_work(&fold->rwork, fl_destroy_filter_work);
1063 	} else {
1064 		list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
1065 	}
1066 
1067 	kfree(tb);
1068 	return 0;
1069 
1070 errout_mask:
1071 	fl_mask_put(head, fnew->mask, false);
1072 
1073 errout_idr:
1074 	if (!fold)
1075 		idr_remove(&head->handle_idr, fnew->handle);
1076 errout:
1077 	tcf_exts_destroy(&fnew->exts);
1078 	kfree(fnew);
1079 errout_tb:
1080 	kfree(tb);
1081 	return err;
1082 }
1083 
1084 static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
1085 		     struct netlink_ext_ack *extack)
1086 {
1087 	struct cls_fl_head *head = rtnl_dereference(tp->root);
1088 	struct cls_fl_filter *f = arg;
1089 
1090 	if (!tc_skip_sw(f->flags))
1091 		rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
1092 				       f->mask->filter_ht_params);
1093 	__fl_delete(tp, f, extack);
1094 	*last = list_empty(&head->masks);
1095 	return 0;
1096 }
1097 
1098 static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1099 {
1100 	struct cls_fl_head *head = rtnl_dereference(tp->root);
1101 	struct cls_fl_filter *f;
1102 
1103 	arg->count = arg->skip;
1104 
1105 	while ((f = idr_get_next_ul(&head->handle_idr,
1106 				    &arg->cookie)) != NULL) {
1107 		if (arg->fn(tp, f, arg) < 0) {
1108 			arg->stop = 1;
1109 			break;
1110 		}
1111 		arg->cookie = f->handle + 1;
1112 		arg->count++;
1113 	}
1114 }
1115 
1116 static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1117 			void *cb_priv, struct netlink_ext_ack *extack)
1118 {
1119 	struct cls_fl_head *head = rtnl_dereference(tp->root);
1120 	struct tc_cls_flower_offload cls_flower = {};
1121 	struct tcf_block *block = tp->chain->block;
1122 	struct fl_flow_mask *mask;
1123 	struct cls_fl_filter *f;
1124 	int err;
1125 
1126 	list_for_each_entry(mask, &head->masks, list) {
1127 		list_for_each_entry(f, &mask->filters, list) {
1128 			if (tc_skip_hw(f->flags))
1129 				continue;
1130 
1131 			tc_cls_common_offload_init(&cls_flower.common, tp,
1132 						   f->flags, extack);
1133 			cls_flower.command = add ?
1134 				TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY;
1135 			cls_flower.cookie = (unsigned long)f;
1136 			cls_flower.dissector = &mask->dissector;
1137 			cls_flower.mask = &f->mkey;
1138 			cls_flower.key = &f->key;
1139 			cls_flower.exts = &f->exts;
1140 			cls_flower.classid = f->res.classid;
1141 
1142 			err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
1143 			if (err) {
1144 				if (add && tc_skip_sw(f->flags))
1145 					return err;
1146 				continue;
1147 			}
1148 
1149 			tc_cls_offload_cnt_update(block, &f->in_hw_count,
1150 						  &f->flags, add);
1151 		}
1152 	}
1153 
1154 	return 0;
1155 }
1156 
1157 static int fl_dump_key_val(struct sk_buff *skb,
1158 			   void *val, int val_type,
1159 			   void *mask, int mask_type, int len)
1160 {
1161 	int err;
1162 
1163 	if (!memchr_inv(mask, 0, len))
1164 		return 0;
1165 	err = nla_put(skb, val_type, len, val);
1166 	if (err)
1167 		return err;
1168 	if (mask_type != TCA_FLOWER_UNSPEC) {
1169 		err = nla_put(skb, mask_type, len, mask);
1170 		if (err)
1171 			return err;
1172 	}
1173 	return 0;
1174 }
1175 
1176 static int fl_dump_key_mpls(struct sk_buff *skb,
1177 			    struct flow_dissector_key_mpls *mpls_key,
1178 			    struct flow_dissector_key_mpls *mpls_mask)
1179 {
1180 	int err;
1181 
1182 	if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1183 		return 0;
1184 	if (mpls_mask->mpls_ttl) {
1185 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1186 				 mpls_key->mpls_ttl);
1187 		if (err)
1188 			return err;
1189 	}
1190 	if (mpls_mask->mpls_tc) {
1191 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1192 				 mpls_key->mpls_tc);
1193 		if (err)
1194 			return err;
1195 	}
1196 	if (mpls_mask->mpls_label) {
1197 		err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1198 				  mpls_key->mpls_label);
1199 		if (err)
1200 			return err;
1201 	}
1202 	if (mpls_mask->mpls_bos) {
1203 		err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1204 				 mpls_key->mpls_bos);
1205 		if (err)
1206 			return err;
1207 	}
1208 	return 0;
1209 }
1210 
1211 static int fl_dump_key_ip(struct sk_buff *skb,
1212 			  struct flow_dissector_key_ip *key,
1213 			  struct flow_dissector_key_ip *mask)
1214 {
1215 	if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos,
1216 			    TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) ||
1217 	    fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl,
1218 			    TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl)))
1219 		return -1;
1220 
1221 	return 0;
1222 }
1223 
1224 static int fl_dump_key_vlan(struct sk_buff *skb,
1225 			    int vlan_id_key, int vlan_prio_key,
1226 			    struct flow_dissector_key_vlan *vlan_key,
1227 			    struct flow_dissector_key_vlan *vlan_mask)
1228 {
1229 	int err;
1230 
1231 	if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1232 		return 0;
1233 	if (vlan_mask->vlan_id) {
1234 		err = nla_put_u16(skb, vlan_id_key,
1235 				  vlan_key->vlan_id);
1236 		if (err)
1237 			return err;
1238 	}
1239 	if (vlan_mask->vlan_priority) {
1240 		err = nla_put_u8(skb, vlan_prio_key,
1241 				 vlan_key->vlan_priority);
1242 		if (err)
1243 			return err;
1244 	}
1245 	return 0;
1246 }
1247 
1248 static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1249 			    u32 *flower_key, u32 *flower_mask,
1250 			    u32 flower_flag_bit, u32 dissector_flag_bit)
1251 {
1252 	if (dissector_mask & dissector_flag_bit) {
1253 		*flower_mask |= flower_flag_bit;
1254 		if (dissector_key & dissector_flag_bit)
1255 			*flower_key |= flower_flag_bit;
1256 	}
1257 }
1258 
1259 static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1260 {
1261 	u32 key, mask;
1262 	__be32 _key, _mask;
1263 	int err;
1264 
1265 	if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1266 		return 0;
1267 
1268 	key = 0;
1269 	mask = 0;
1270 
1271 	fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1272 			TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1273 	fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1274 			TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1275 			FLOW_DIS_FIRST_FRAG);
1276 
1277 	_key = cpu_to_be32(key);
1278 	_mask = cpu_to_be32(mask);
1279 
1280 	err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1281 	if (err)
1282 		return err;
1283 
1284 	return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1285 }
1286 
1287 static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
1288 		   struct sk_buff *skb, struct tcmsg *t)
1289 {
1290 	struct cls_fl_filter *f = fh;
1291 	struct nlattr *nest;
1292 	struct fl_flow_key *key, *mask;
1293 
1294 	if (!f)
1295 		return skb->len;
1296 
1297 	t->tcm_handle = f->handle;
1298 
1299 	nest = nla_nest_start(skb, TCA_OPTIONS);
1300 	if (!nest)
1301 		goto nla_put_failure;
1302 
1303 	if (f->res.classid &&
1304 	    nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1305 		goto nla_put_failure;
1306 
1307 	key = &f->key;
1308 	mask = &f->mask->key;
1309 
1310 	if (mask->indev_ifindex) {
1311 		struct net_device *dev;
1312 
1313 		dev = __dev_get_by_index(net, key->indev_ifindex);
1314 		if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1315 			goto nla_put_failure;
1316 	}
1317 
1318 	if (!tc_skip_hw(f->flags))
1319 		fl_hw_update_stats(tp, f);
1320 
1321 	if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1322 			    mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1323 			    sizeof(key->eth.dst)) ||
1324 	    fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1325 			    mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1326 			    sizeof(key->eth.src)) ||
1327 	    fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1328 			    &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1329 			    sizeof(key->basic.n_proto)))
1330 		goto nla_put_failure;
1331 
1332 	if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1333 		goto nla_put_failure;
1334 
1335 	if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
1336 			     TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
1337 		goto nla_put_failure;
1338 
1339 	if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
1340 			     TCA_FLOWER_KEY_CVLAN_PRIO,
1341 			     &key->cvlan, &mask->cvlan) ||
1342 	    (mask->cvlan.vlan_tpid &&
1343 	     nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1344 			 key->cvlan.vlan_tpid)))
1345 		goto nla_put_failure;
1346 
1347 	if (mask->basic.n_proto) {
1348 		if (mask->cvlan.vlan_tpid) {
1349 			if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1350 					 key->basic.n_proto))
1351 				goto nla_put_failure;
1352 		} else if (mask->vlan.vlan_tpid) {
1353 			if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1354 					 key->basic.n_proto))
1355 				goto nla_put_failure;
1356 		}
1357 	}
1358 
1359 	if ((key->basic.n_proto == htons(ETH_P_IP) ||
1360 	     key->basic.n_proto == htons(ETH_P_IPV6)) &&
1361 	    (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1362 			    &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1363 			    sizeof(key->basic.ip_proto)) ||
1364 	    fl_dump_key_ip(skb, &key->ip, &mask->ip)))
1365 		goto nla_put_failure;
1366 
1367 	if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1368 	    (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1369 			     &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1370 			     sizeof(key->ipv4.src)) ||
1371 	     fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1372 			     &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1373 			     sizeof(key->ipv4.dst))))
1374 		goto nla_put_failure;
1375 	else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1376 		 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1377 				  &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1378 				  sizeof(key->ipv6.src)) ||
1379 		  fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1380 				  &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1381 				  sizeof(key->ipv6.dst))))
1382 		goto nla_put_failure;
1383 
1384 	if (key->basic.ip_proto == IPPROTO_TCP &&
1385 	    (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
1386 			     &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
1387 			     sizeof(key->tp.src)) ||
1388 	     fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
1389 			     &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
1390 			     sizeof(key->tp.dst)) ||
1391 	     fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1392 			     &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1393 			     sizeof(key->tcp.flags))))
1394 		goto nla_put_failure;
1395 	else if (key->basic.ip_proto == IPPROTO_UDP &&
1396 		 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
1397 				  &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
1398 				  sizeof(key->tp.src)) ||
1399 		  fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
1400 				  &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
1401 				  sizeof(key->tp.dst))))
1402 		goto nla_put_failure;
1403 	else if (key->basic.ip_proto == IPPROTO_SCTP &&
1404 		 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1405 				  &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1406 				  sizeof(key->tp.src)) ||
1407 		  fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1408 				  &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1409 				  sizeof(key->tp.dst))))
1410 		goto nla_put_failure;
1411 	else if (key->basic.n_proto == htons(ETH_P_IP) &&
1412 		 key->basic.ip_proto == IPPROTO_ICMP &&
1413 		 (fl_dump_key_val(skb, &key->icmp.type,
1414 				  TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1415 				  TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1416 				  sizeof(key->icmp.type)) ||
1417 		  fl_dump_key_val(skb, &key->icmp.code,
1418 				  TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1419 				  TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1420 				  sizeof(key->icmp.code))))
1421 		goto nla_put_failure;
1422 	else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1423 		 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1424 		 (fl_dump_key_val(skb, &key->icmp.type,
1425 				  TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1426 				  TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1427 				  sizeof(key->icmp.type)) ||
1428 		  fl_dump_key_val(skb, &key->icmp.code,
1429 				  TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1430 				  TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1431 				  sizeof(key->icmp.code))))
1432 		goto nla_put_failure;
1433 	else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1434 		  key->basic.n_proto == htons(ETH_P_RARP)) &&
1435 		 (fl_dump_key_val(skb, &key->arp.sip,
1436 				  TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1437 				  TCA_FLOWER_KEY_ARP_SIP_MASK,
1438 				  sizeof(key->arp.sip)) ||
1439 		  fl_dump_key_val(skb, &key->arp.tip,
1440 				  TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1441 				  TCA_FLOWER_KEY_ARP_TIP_MASK,
1442 				  sizeof(key->arp.tip)) ||
1443 		  fl_dump_key_val(skb, &key->arp.op,
1444 				  TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1445 				  TCA_FLOWER_KEY_ARP_OP_MASK,
1446 				  sizeof(key->arp.op)) ||
1447 		  fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1448 				  mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1449 				  sizeof(key->arp.sha)) ||
1450 		  fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1451 				  mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1452 				  sizeof(key->arp.tha))))
1453 		goto nla_put_failure;
1454 
1455 	if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1456 	    (fl_dump_key_val(skb, &key->enc_ipv4.src,
1457 			    TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1458 			    TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1459 			    sizeof(key->enc_ipv4.src)) ||
1460 	     fl_dump_key_val(skb, &key->enc_ipv4.dst,
1461 			     TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1462 			     TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1463 			     sizeof(key->enc_ipv4.dst))))
1464 		goto nla_put_failure;
1465 	else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1466 		 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1467 			    TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1468 			    TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1469 			    sizeof(key->enc_ipv6.src)) ||
1470 		 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1471 				 TCA_FLOWER_KEY_ENC_IPV6_DST,
1472 				 &mask->enc_ipv6.dst,
1473 				 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1474 			    sizeof(key->enc_ipv6.dst))))
1475 		goto nla_put_failure;
1476 
1477 	if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
1478 			    &mask->enc_key_id, TCA_FLOWER_UNSPEC,
1479 			    sizeof(key->enc_key_id)) ||
1480 	    fl_dump_key_val(skb, &key->enc_tp.src,
1481 			    TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1482 			    &mask->enc_tp.src,
1483 			    TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1484 			    sizeof(key->enc_tp.src)) ||
1485 	    fl_dump_key_val(skb, &key->enc_tp.dst,
1486 			    TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1487 			    &mask->enc_tp.dst,
1488 			    TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1489 			    sizeof(key->enc_tp.dst)))
1490 		goto nla_put_failure;
1491 
1492 	if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1493 		goto nla_put_failure;
1494 
1495 	if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1496 		goto nla_put_failure;
1497 
1498 	if (tcf_exts_dump(skb, &f->exts))
1499 		goto nla_put_failure;
1500 
1501 	nla_nest_end(skb, nest);
1502 
1503 	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1504 		goto nla_put_failure;
1505 
1506 	return skb->len;
1507 
1508 nla_put_failure:
1509 	nla_nest_cancel(skb, nest);
1510 	return -1;
1511 }
1512 
1513 static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
1514 {
1515 	struct cls_fl_filter *f = fh;
1516 
1517 	if (f && f->res.classid == classid)
1518 		f->res.class = cl;
1519 }
1520 
1521 static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1522 	.kind		= "flower",
1523 	.classify	= fl_classify,
1524 	.init		= fl_init,
1525 	.destroy	= fl_destroy,
1526 	.get		= fl_get,
1527 	.change		= fl_change,
1528 	.delete		= fl_delete,
1529 	.walk		= fl_walk,
1530 	.reoffload	= fl_reoffload,
1531 	.dump		= fl_dump,
1532 	.bind_class	= fl_bind_class,
1533 	.owner		= THIS_MODULE,
1534 };
1535 
1536 static int __init cls_fl_init(void)
1537 {
1538 	return register_tcf_proto_ops(&cls_fl_ops);
1539 }
1540 
1541 static void __exit cls_fl_exit(void)
1542 {
1543 	unregister_tcf_proto_ops(&cls_fl_ops);
1544 }
1545 
1546 module_init(cls_fl_init);
1547 module_exit(cls_fl_exit);
1548 
1549 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1550 MODULE_DESCRIPTION("Flower classifier");
1551 MODULE_LICENSE("GPL v2");
1552