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