xref: /linux/net/sched/cls_u32.c (revision ecdf06e1ea5376bba03c155751f6869d3dfaa210)
1 /*
2  * net/sched/cls_u32.c	Ugly (or Universal) 32bit key Packet Classifier.
3  *
4  *		This program is free software; you can redistribute it and/or
5  *		modify it under the terms of the GNU General Public License
6  *		as published by the Free Software Foundation; either version
7  *		2 of the License, or (at your option) any later version.
8  *
9  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  *	The filters are packed to hash tables of key nodes
12  *	with a set of 32bit key/mask pairs at every node.
13  *	Nodes reference next level hash tables etc.
14  *
15  *	This scheme is the best universal classifier I managed to
16  *	invent; it is not super-fast, but it is not slow (provided you
17  *	program it correctly), and general enough.  And its relative
18  *	speed grows as the number of rules becomes larger.
19  *
20  *	It seems that it represents the best middle point between
21  *	speed and manageability both by human and by machine.
22  *
23  *	It is especially useful for link sharing combined with QoS;
24  *	pure RSVP doesn't need such a general approach and can use
25  *	much simpler (and faster) schemes, sort of cls_rsvp.c.
26  *
27  *	JHS: We should remove the CONFIG_NET_CLS_IND from here
28  *	eventually when the meta match extension is made available
29  *
30  *	nfmark match added by Catalin(ux aka Dino) BOIE <catab at umbrella.ro>
31  */
32 
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/types.h>
36 #include <linux/kernel.h>
37 #include <linux/string.h>
38 #include <linux/errno.h>
39 #include <linux/percpu.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/skbuff.h>
42 #include <linux/bitmap.h>
43 #include <linux/netdevice.h>
44 #include <linux/hash.h>
45 #include <net/netlink.h>
46 #include <net/act_api.h>
47 #include <net/pkt_cls.h>
48 #include <linux/idr.h>
49 
50 struct tc_u_knode {
51 	struct tc_u_knode __rcu	*next;
52 	u32			handle;
53 	struct tc_u_hnode __rcu	*ht_up;
54 	struct tcf_exts		exts;
55 #ifdef CONFIG_NET_CLS_IND
56 	int			ifindex;
57 #endif
58 	u8			fshift;
59 	struct tcf_result	res;
60 	struct tc_u_hnode __rcu	*ht_down;
61 #ifdef CONFIG_CLS_U32_PERF
62 	struct tc_u32_pcnt __percpu *pf;
63 #endif
64 	u32			flags;
65 #ifdef CONFIG_CLS_U32_MARK
66 	u32			val;
67 	u32			mask;
68 	u32 __percpu		*pcpu_success;
69 #endif
70 	struct tcf_proto	*tp;
71 	union {
72 		struct work_struct	work;
73 		struct rcu_head		rcu;
74 	};
75 	/* The 'sel' field MUST be the last field in structure to allow for
76 	 * tc_u32_keys allocated at end of structure.
77 	 */
78 	struct tc_u32_sel	sel;
79 };
80 
81 struct tc_u_hnode {
82 	struct tc_u_hnode __rcu	*next;
83 	u32			handle;
84 	u32			prio;
85 	struct tc_u_common	*tp_c;
86 	int			refcnt;
87 	unsigned int		divisor;
88 	struct idr		handle_idr;
89 	struct rcu_head		rcu;
90 	u32			flags;
91 	/* The 'ht' field MUST be the last field in structure to allow for
92 	 * more entries allocated at end of structure.
93 	 */
94 	struct tc_u_knode __rcu	*ht[1];
95 };
96 
97 struct tc_u_common {
98 	struct tc_u_hnode __rcu	*hlist;
99 	struct tcf_block	*block;
100 	int			refcnt;
101 	struct idr		handle_idr;
102 	struct hlist_node	hnode;
103 	struct rcu_head		rcu;
104 };
105 
106 static inline unsigned int u32_hash_fold(__be32 key,
107 					 const struct tc_u32_sel *sel,
108 					 u8 fshift)
109 {
110 	unsigned int h = ntohl(key & sel->hmask) >> fshift;
111 
112 	return h;
113 }
114 
115 static int u32_classify(struct sk_buff *skb, const struct tcf_proto *tp,
116 			struct tcf_result *res)
117 {
118 	struct {
119 		struct tc_u_knode *knode;
120 		unsigned int	  off;
121 	} stack[TC_U32_MAXDEPTH];
122 
123 	struct tc_u_hnode *ht = rcu_dereference_bh(tp->root);
124 	unsigned int off = skb_network_offset(skb);
125 	struct tc_u_knode *n;
126 	int sdepth = 0;
127 	int off2 = 0;
128 	int sel = 0;
129 #ifdef CONFIG_CLS_U32_PERF
130 	int j;
131 #endif
132 	int i, r;
133 
134 next_ht:
135 	n = rcu_dereference_bh(ht->ht[sel]);
136 
137 next_knode:
138 	if (n) {
139 		struct tc_u32_key *key = n->sel.keys;
140 
141 #ifdef CONFIG_CLS_U32_PERF
142 		__this_cpu_inc(n->pf->rcnt);
143 		j = 0;
144 #endif
145 
146 		if (tc_skip_sw(n->flags)) {
147 			n = rcu_dereference_bh(n->next);
148 			goto next_knode;
149 		}
150 
151 #ifdef CONFIG_CLS_U32_MARK
152 		if ((skb->mark & n->mask) != n->val) {
153 			n = rcu_dereference_bh(n->next);
154 			goto next_knode;
155 		} else {
156 			__this_cpu_inc(*n->pcpu_success);
157 		}
158 #endif
159 
160 		for (i = n->sel.nkeys; i > 0; i--, key++) {
161 			int toff = off + key->off + (off2 & key->offmask);
162 			__be32 *data, hdata;
163 
164 			if (skb_headroom(skb) + toff > INT_MAX)
165 				goto out;
166 
167 			data = skb_header_pointer(skb, toff, 4, &hdata);
168 			if (!data)
169 				goto out;
170 			if ((*data ^ key->val) & key->mask) {
171 				n = rcu_dereference_bh(n->next);
172 				goto next_knode;
173 			}
174 #ifdef CONFIG_CLS_U32_PERF
175 			__this_cpu_inc(n->pf->kcnts[j]);
176 			j++;
177 #endif
178 		}
179 
180 		ht = rcu_dereference_bh(n->ht_down);
181 		if (!ht) {
182 check_terminal:
183 			if (n->sel.flags & TC_U32_TERMINAL) {
184 
185 				*res = n->res;
186 #ifdef CONFIG_NET_CLS_IND
187 				if (!tcf_match_indev(skb, n->ifindex)) {
188 					n = rcu_dereference_bh(n->next);
189 					goto next_knode;
190 				}
191 #endif
192 #ifdef CONFIG_CLS_U32_PERF
193 				__this_cpu_inc(n->pf->rhit);
194 #endif
195 				r = tcf_exts_exec(skb, &n->exts, res);
196 				if (r < 0) {
197 					n = rcu_dereference_bh(n->next);
198 					goto next_knode;
199 				}
200 
201 				return r;
202 			}
203 			n = rcu_dereference_bh(n->next);
204 			goto next_knode;
205 		}
206 
207 		/* PUSH */
208 		if (sdepth >= TC_U32_MAXDEPTH)
209 			goto deadloop;
210 		stack[sdepth].knode = n;
211 		stack[sdepth].off = off;
212 		sdepth++;
213 
214 		ht = rcu_dereference_bh(n->ht_down);
215 		sel = 0;
216 		if (ht->divisor) {
217 			__be32 *data, hdata;
218 
219 			data = skb_header_pointer(skb, off + n->sel.hoff, 4,
220 						  &hdata);
221 			if (!data)
222 				goto out;
223 			sel = ht->divisor & u32_hash_fold(*data, &n->sel,
224 							  n->fshift);
225 		}
226 		if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
227 			goto next_ht;
228 
229 		if (n->sel.flags & (TC_U32_OFFSET | TC_U32_VAROFFSET)) {
230 			off2 = n->sel.off + 3;
231 			if (n->sel.flags & TC_U32_VAROFFSET) {
232 				__be16 *data, hdata;
233 
234 				data = skb_header_pointer(skb,
235 							  off + n->sel.offoff,
236 							  2, &hdata);
237 				if (!data)
238 					goto out;
239 				off2 += ntohs(n->sel.offmask & *data) >>
240 					n->sel.offshift;
241 			}
242 			off2 &= ~3;
243 		}
244 		if (n->sel.flags & TC_U32_EAT) {
245 			off += off2;
246 			off2 = 0;
247 		}
248 
249 		if (off < skb->len)
250 			goto next_ht;
251 	}
252 
253 	/* POP */
254 	if (sdepth--) {
255 		n = stack[sdepth].knode;
256 		ht = rcu_dereference_bh(n->ht_up);
257 		off = stack[sdepth].off;
258 		goto check_terminal;
259 	}
260 out:
261 	return -1;
262 
263 deadloop:
264 	net_warn_ratelimited("cls_u32: dead loop\n");
265 	return -1;
266 }
267 
268 static struct tc_u_hnode *u32_lookup_ht(struct tc_u_common *tp_c, u32 handle)
269 {
270 	struct tc_u_hnode *ht;
271 
272 	for (ht = rtnl_dereference(tp_c->hlist);
273 	     ht;
274 	     ht = rtnl_dereference(ht->next))
275 		if (ht->handle == handle)
276 			break;
277 
278 	return ht;
279 }
280 
281 static struct tc_u_knode *u32_lookup_key(struct tc_u_hnode *ht, u32 handle)
282 {
283 	unsigned int sel;
284 	struct tc_u_knode *n = NULL;
285 
286 	sel = TC_U32_HASH(handle);
287 	if (sel > ht->divisor)
288 		goto out;
289 
290 	for (n = rtnl_dereference(ht->ht[sel]);
291 	     n;
292 	     n = rtnl_dereference(n->next))
293 		if (n->handle == handle)
294 			break;
295 out:
296 	return n;
297 }
298 
299 
300 static void *u32_get(struct tcf_proto *tp, u32 handle)
301 {
302 	struct tc_u_hnode *ht;
303 	struct tc_u_common *tp_c = tp->data;
304 
305 	if (TC_U32_HTID(handle) == TC_U32_ROOT)
306 		ht = rtnl_dereference(tp->root);
307 	else
308 		ht = u32_lookup_ht(tp_c, TC_U32_HTID(handle));
309 
310 	if (!ht)
311 		return NULL;
312 
313 	if (TC_U32_KEY(handle) == 0)
314 		return ht;
315 
316 	return u32_lookup_key(ht, handle);
317 }
318 
319 static u32 gen_new_htid(struct tc_u_common *tp_c, struct tc_u_hnode *ptr)
320 {
321 	unsigned long idr_index;
322 	int err;
323 
324 	/* This is only used inside rtnl lock it is safe to increment
325 	 * without read _copy_ update semantics
326 	 */
327 	err = idr_alloc_ext(&tp_c->handle_idr, ptr, &idr_index,
328 			    1, 0x7FF, GFP_KERNEL);
329 	if (err)
330 		return 0;
331 	return (u32)(idr_index | 0x800) << 20;
332 }
333 
334 static struct hlist_head *tc_u_common_hash;
335 
336 #define U32_HASH_SHIFT 10
337 #define U32_HASH_SIZE (1 << U32_HASH_SHIFT)
338 
339 static unsigned int tc_u_hash(const struct tcf_proto *tp)
340 {
341 	return hash_ptr(tp->chain->block, U32_HASH_SHIFT);
342 }
343 
344 static struct tc_u_common *tc_u_common_find(const struct tcf_proto *tp)
345 {
346 	struct tc_u_common *tc;
347 	unsigned int h;
348 
349 	h = tc_u_hash(tp);
350 	hlist_for_each_entry(tc, &tc_u_common_hash[h], hnode) {
351 		if (tc->block == tp->chain->block)
352 			return tc;
353 	}
354 	return NULL;
355 }
356 
357 static int u32_init(struct tcf_proto *tp)
358 {
359 	struct tc_u_hnode *root_ht;
360 	struct tc_u_common *tp_c;
361 	unsigned int h;
362 
363 	tp_c = tc_u_common_find(tp);
364 
365 	root_ht = kzalloc(sizeof(*root_ht), GFP_KERNEL);
366 	if (root_ht == NULL)
367 		return -ENOBUFS;
368 
369 	root_ht->refcnt++;
370 	root_ht->handle = tp_c ? gen_new_htid(tp_c, root_ht) : 0x80000000;
371 	root_ht->prio = tp->prio;
372 	idr_init(&root_ht->handle_idr);
373 
374 	if (tp_c == NULL) {
375 		tp_c = kzalloc(sizeof(*tp_c), GFP_KERNEL);
376 		if (tp_c == NULL) {
377 			kfree(root_ht);
378 			return -ENOBUFS;
379 		}
380 		tp_c->block = tp->chain->block;
381 		INIT_HLIST_NODE(&tp_c->hnode);
382 		idr_init(&tp_c->handle_idr);
383 
384 		h = tc_u_hash(tp);
385 		hlist_add_head(&tp_c->hnode, &tc_u_common_hash[h]);
386 	}
387 
388 	tp_c->refcnt++;
389 	RCU_INIT_POINTER(root_ht->next, tp_c->hlist);
390 	rcu_assign_pointer(tp_c->hlist, root_ht);
391 	root_ht->tp_c = tp_c;
392 
393 	rcu_assign_pointer(tp->root, root_ht);
394 	tp->data = tp_c;
395 	return 0;
396 }
397 
398 static int u32_destroy_key(struct tcf_proto *tp, struct tc_u_knode *n,
399 			   bool free_pf)
400 {
401 	struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
402 
403 	tcf_exts_destroy(&n->exts);
404 	tcf_exts_put_net(&n->exts);
405 	if (ht && --ht->refcnt == 0)
406 		kfree(ht);
407 #ifdef CONFIG_CLS_U32_PERF
408 	if (free_pf)
409 		free_percpu(n->pf);
410 #endif
411 #ifdef CONFIG_CLS_U32_MARK
412 	if (free_pf)
413 		free_percpu(n->pcpu_success);
414 #endif
415 	kfree(n);
416 	return 0;
417 }
418 
419 /* u32_delete_key_rcu should be called when free'ing a copied
420  * version of a tc_u_knode obtained from u32_init_knode(). When
421  * copies are obtained from u32_init_knode() the statistics are
422  * shared between the old and new copies to allow readers to
423  * continue to update the statistics during the copy. To support
424  * this the u32_delete_key_rcu variant does not free the percpu
425  * statistics.
426  */
427 static void u32_delete_key_work(struct work_struct *work)
428 {
429 	struct tc_u_knode *key = container_of(work, struct tc_u_knode, work);
430 
431 	rtnl_lock();
432 	u32_destroy_key(key->tp, key, false);
433 	rtnl_unlock();
434 }
435 
436 static void u32_delete_key_rcu(struct rcu_head *rcu)
437 {
438 	struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
439 
440 	INIT_WORK(&key->work, u32_delete_key_work);
441 	tcf_queue_work(&key->work);
442 }
443 
444 /* u32_delete_key_freepf_rcu is the rcu callback variant
445  * that free's the entire structure including the statistics
446  * percpu variables. Only use this if the key is not a copy
447  * returned by u32_init_knode(). See u32_delete_key_rcu()
448  * for the variant that should be used with keys return from
449  * u32_init_knode()
450  */
451 static void u32_delete_key_freepf_work(struct work_struct *work)
452 {
453 	struct tc_u_knode *key = container_of(work, struct tc_u_knode, work);
454 
455 	rtnl_lock();
456 	u32_destroy_key(key->tp, key, true);
457 	rtnl_unlock();
458 }
459 
460 static void u32_delete_key_freepf_rcu(struct rcu_head *rcu)
461 {
462 	struct tc_u_knode *key = container_of(rcu, struct tc_u_knode, rcu);
463 
464 	INIT_WORK(&key->work, u32_delete_key_freepf_work);
465 	tcf_queue_work(&key->work);
466 }
467 
468 static int u32_delete_key(struct tcf_proto *tp, struct tc_u_knode *key)
469 {
470 	struct tc_u_knode __rcu **kp;
471 	struct tc_u_knode *pkp;
472 	struct tc_u_hnode *ht = rtnl_dereference(key->ht_up);
473 
474 	if (ht) {
475 		kp = &ht->ht[TC_U32_HASH(key->handle)];
476 		for (pkp = rtnl_dereference(*kp); pkp;
477 		     kp = &pkp->next, pkp = rtnl_dereference(*kp)) {
478 			if (pkp == key) {
479 				RCU_INIT_POINTER(*kp, key->next);
480 
481 				tcf_unbind_filter(tp, &key->res);
482 				tcf_exts_get_net(&key->exts);
483 				call_rcu(&key->rcu, u32_delete_key_freepf_rcu);
484 				return 0;
485 			}
486 		}
487 	}
488 	WARN_ON(1);
489 	return 0;
490 }
491 
492 static void u32_clear_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
493 			       struct netlink_ext_ack *extack)
494 {
495 	struct tcf_block *block = tp->chain->block;
496 	struct tc_cls_u32_offload cls_u32 = {};
497 
498 	tc_cls_common_offload_init(&cls_u32.common, tp, h->flags, extack);
499 	cls_u32.command = TC_CLSU32_DELETE_HNODE;
500 	cls_u32.hnode.divisor = h->divisor;
501 	cls_u32.hnode.handle = h->handle;
502 	cls_u32.hnode.prio = h->prio;
503 
504 	tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
505 }
506 
507 static int u32_replace_hw_hnode(struct tcf_proto *tp, struct tc_u_hnode *h,
508 				u32 flags, struct netlink_ext_ack *extack)
509 {
510 	struct tcf_block *block = tp->chain->block;
511 	struct tc_cls_u32_offload cls_u32 = {};
512 	bool skip_sw = tc_skip_sw(flags);
513 	bool offloaded = false;
514 	int err;
515 
516 	tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
517 	cls_u32.command = TC_CLSU32_NEW_HNODE;
518 	cls_u32.hnode.divisor = h->divisor;
519 	cls_u32.hnode.handle = h->handle;
520 	cls_u32.hnode.prio = h->prio;
521 
522 	err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
523 	if (err < 0) {
524 		u32_clear_hw_hnode(tp, h, NULL);
525 		return err;
526 	} else if (err > 0) {
527 		offloaded = true;
528 	}
529 
530 	if (skip_sw && !offloaded)
531 		return -EINVAL;
532 
533 	return 0;
534 }
535 
536 static void u32_remove_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
537 				struct netlink_ext_ack *extack)
538 {
539 	struct tcf_block *block = tp->chain->block;
540 	struct tc_cls_u32_offload cls_u32 = {};
541 
542 	tc_cls_common_offload_init(&cls_u32.common, tp, n->flags, extack);
543 	cls_u32.command = TC_CLSU32_DELETE_KNODE;
544 	cls_u32.knode.handle = n->handle;
545 
546 	tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, false);
547 	tcf_block_offload_dec(block, &n->flags);
548 }
549 
550 static int u32_replace_hw_knode(struct tcf_proto *tp, struct tc_u_knode *n,
551 				u32 flags, struct netlink_ext_ack *extack)
552 {
553 	struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
554 	struct tcf_block *block = tp->chain->block;
555 	struct tc_cls_u32_offload cls_u32 = {};
556 	bool skip_sw = tc_skip_sw(flags);
557 	int err;
558 
559 	tc_cls_common_offload_init(&cls_u32.common, tp, flags, extack);
560 	cls_u32.command = TC_CLSU32_REPLACE_KNODE;
561 	cls_u32.knode.handle = n->handle;
562 	cls_u32.knode.fshift = n->fshift;
563 #ifdef CONFIG_CLS_U32_MARK
564 	cls_u32.knode.val = n->val;
565 	cls_u32.knode.mask = n->mask;
566 #else
567 	cls_u32.knode.val = 0;
568 	cls_u32.knode.mask = 0;
569 #endif
570 	cls_u32.knode.sel = &n->sel;
571 	cls_u32.knode.exts = &n->exts;
572 	if (n->ht_down)
573 		cls_u32.knode.link_handle = ht->handle;
574 
575 	err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSU32, &cls_u32, skip_sw);
576 	if (err < 0) {
577 		u32_remove_hw_knode(tp, n, NULL);
578 		return err;
579 	} else if (err > 0) {
580 		tcf_block_offload_inc(block, &n->flags);
581 	}
582 
583 	if (skip_sw && !(n->flags & TCA_CLS_FLAGS_IN_HW))
584 		return -EINVAL;
585 
586 	return 0;
587 }
588 
589 static void u32_clear_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
590 			    struct netlink_ext_ack *extack)
591 {
592 	struct tc_u_knode *n;
593 	unsigned int h;
594 
595 	for (h = 0; h <= ht->divisor; h++) {
596 		while ((n = rtnl_dereference(ht->ht[h])) != NULL) {
597 			RCU_INIT_POINTER(ht->ht[h],
598 					 rtnl_dereference(n->next));
599 			tcf_unbind_filter(tp, &n->res);
600 			u32_remove_hw_knode(tp, n, extack);
601 			idr_remove_ext(&ht->handle_idr, n->handle);
602 			if (tcf_exts_get_net(&n->exts))
603 				call_rcu(&n->rcu, u32_delete_key_freepf_rcu);
604 			else
605 				u32_destroy_key(n->tp, n, true);
606 		}
607 	}
608 }
609 
610 static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht,
611 			     struct netlink_ext_ack *extack)
612 {
613 	struct tc_u_common *tp_c = tp->data;
614 	struct tc_u_hnode __rcu **hn;
615 	struct tc_u_hnode *phn;
616 
617 	WARN_ON(ht->refcnt);
618 
619 	u32_clear_hnode(tp, ht, extack);
620 
621 	hn = &tp_c->hlist;
622 	for (phn = rtnl_dereference(*hn);
623 	     phn;
624 	     hn = &phn->next, phn = rtnl_dereference(*hn)) {
625 		if (phn == ht) {
626 			u32_clear_hw_hnode(tp, ht, extack);
627 			idr_destroy(&ht->handle_idr);
628 			idr_remove_ext(&tp_c->handle_idr, ht->handle);
629 			RCU_INIT_POINTER(*hn, ht->next);
630 			kfree_rcu(ht, rcu);
631 			return 0;
632 		}
633 	}
634 
635 	return -ENOENT;
636 }
637 
638 static bool ht_empty(struct tc_u_hnode *ht)
639 {
640 	unsigned int h;
641 
642 	for (h = 0; h <= ht->divisor; h++)
643 		if (rcu_access_pointer(ht->ht[h]))
644 			return false;
645 
646 	return true;
647 }
648 
649 static void u32_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
650 {
651 	struct tc_u_common *tp_c = tp->data;
652 	struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
653 
654 	WARN_ON(root_ht == NULL);
655 
656 	if (root_ht && --root_ht->refcnt == 0)
657 		u32_destroy_hnode(tp, root_ht, extack);
658 
659 	if (--tp_c->refcnt == 0) {
660 		struct tc_u_hnode *ht;
661 
662 		hlist_del(&tp_c->hnode);
663 
664 		while ((ht = rtnl_dereference(tp_c->hlist)) != NULL) {
665 			u32_clear_hnode(tp, ht, extack);
666 			RCU_INIT_POINTER(tp_c->hlist, ht->next);
667 
668 			/* u32_destroy_key() will later free ht for us, if it's
669 			 * still referenced by some knode
670 			 */
671 			if (--ht->refcnt == 0)
672 				kfree_rcu(ht, rcu);
673 		}
674 
675 		idr_destroy(&tp_c->handle_idr);
676 		kfree(tp_c);
677 	}
678 
679 	tp->data = NULL;
680 }
681 
682 static int u32_delete(struct tcf_proto *tp, void *arg, bool *last,
683 		      struct netlink_ext_ack *extack)
684 {
685 	struct tc_u_hnode *ht = arg;
686 	struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
687 	struct tc_u_common *tp_c = tp->data;
688 	int ret = 0;
689 
690 	if (ht == NULL)
691 		goto out;
692 
693 	if (TC_U32_KEY(ht->handle)) {
694 		u32_remove_hw_knode(tp, (struct tc_u_knode *)ht, extack);
695 		ret = u32_delete_key(tp, (struct tc_u_knode *)ht);
696 		goto out;
697 	}
698 
699 	if (root_ht == ht) {
700 		NL_SET_ERR_MSG_MOD(extack, "Not allowed to delete root node");
701 		return -EINVAL;
702 	}
703 
704 	if (ht->refcnt == 1) {
705 		ht->refcnt--;
706 		u32_destroy_hnode(tp, ht, extack);
707 	} else {
708 		NL_SET_ERR_MSG_MOD(extack, "Can not delete in-use filter");
709 		return -EBUSY;
710 	}
711 
712 out:
713 	*last = true;
714 	if (root_ht) {
715 		if (root_ht->refcnt > 1) {
716 			*last = false;
717 			goto ret;
718 		}
719 		if (root_ht->refcnt == 1) {
720 			if (!ht_empty(root_ht)) {
721 				*last = false;
722 				goto ret;
723 			}
724 		}
725 	}
726 
727 	if (tp_c->refcnt > 1) {
728 		*last = false;
729 		goto ret;
730 	}
731 
732 	if (tp_c->refcnt == 1) {
733 		struct tc_u_hnode *ht;
734 
735 		for (ht = rtnl_dereference(tp_c->hlist);
736 		     ht;
737 		     ht = rtnl_dereference(ht->next))
738 			if (!ht_empty(ht)) {
739 				*last = false;
740 				break;
741 			}
742 	}
743 
744 ret:
745 	return ret;
746 }
747 
748 static u32 gen_new_kid(struct tc_u_hnode *ht, u32 htid)
749 {
750 	unsigned long idr_index;
751 	u32 start = htid | 0x800;
752 	u32 max = htid | 0xFFF;
753 	u32 min = htid;
754 
755 	if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
756 			  start, max + 1, GFP_KERNEL)) {
757 		if (idr_alloc_ext(&ht->handle_idr, NULL, &idr_index,
758 				  min + 1, max + 1, GFP_KERNEL))
759 			return max;
760 	}
761 
762 	return (u32)idr_index;
763 }
764 
765 static const struct nla_policy u32_policy[TCA_U32_MAX + 1] = {
766 	[TCA_U32_CLASSID]	= { .type = NLA_U32 },
767 	[TCA_U32_HASH]		= { .type = NLA_U32 },
768 	[TCA_U32_LINK]		= { .type = NLA_U32 },
769 	[TCA_U32_DIVISOR]	= { .type = NLA_U32 },
770 	[TCA_U32_SEL]		= { .len = sizeof(struct tc_u32_sel) },
771 	[TCA_U32_INDEV]		= { .type = NLA_STRING, .len = IFNAMSIZ },
772 	[TCA_U32_MARK]		= { .len = sizeof(struct tc_u32_mark) },
773 	[TCA_U32_FLAGS]		= { .type = NLA_U32 },
774 };
775 
776 static int u32_set_parms(struct net *net, struct tcf_proto *tp,
777 			 unsigned long base, struct tc_u_hnode *ht,
778 			 struct tc_u_knode *n, struct nlattr **tb,
779 			 struct nlattr *est, bool ovr,
780 			 struct netlink_ext_ack *extack)
781 {
782 	int err;
783 
784 	err = tcf_exts_validate(net, tp, tb, est, &n->exts, ovr, extack);
785 	if (err < 0)
786 		return err;
787 
788 	if (tb[TCA_U32_LINK]) {
789 		u32 handle = nla_get_u32(tb[TCA_U32_LINK]);
790 		struct tc_u_hnode *ht_down = NULL, *ht_old;
791 
792 		if (TC_U32_KEY(handle)) {
793 			NL_SET_ERR_MSG_MOD(extack, "u32 Link handle must be a hash table");
794 			return -EINVAL;
795 		}
796 
797 		if (handle) {
798 			ht_down = u32_lookup_ht(ht->tp_c, handle);
799 
800 			if (!ht_down) {
801 				NL_SET_ERR_MSG_MOD(extack, "Link hash table not found");
802 				return -EINVAL;
803 			}
804 			ht_down->refcnt++;
805 		}
806 
807 		ht_old = rtnl_dereference(n->ht_down);
808 		rcu_assign_pointer(n->ht_down, ht_down);
809 
810 		if (ht_old)
811 			ht_old->refcnt--;
812 	}
813 	if (tb[TCA_U32_CLASSID]) {
814 		n->res.classid = nla_get_u32(tb[TCA_U32_CLASSID]);
815 		tcf_bind_filter(tp, &n->res, base);
816 	}
817 
818 #ifdef CONFIG_NET_CLS_IND
819 	if (tb[TCA_U32_INDEV]) {
820 		int ret;
821 		ret = tcf_change_indev(net, tb[TCA_U32_INDEV], extack);
822 		if (ret < 0)
823 			return -EINVAL;
824 		n->ifindex = ret;
825 	}
826 #endif
827 	return 0;
828 }
829 
830 static void u32_replace_knode(struct tcf_proto *tp, struct tc_u_common *tp_c,
831 			      struct tc_u_knode *n)
832 {
833 	struct tc_u_knode __rcu **ins;
834 	struct tc_u_knode *pins;
835 	struct tc_u_hnode *ht;
836 
837 	if (TC_U32_HTID(n->handle) == TC_U32_ROOT)
838 		ht = rtnl_dereference(tp->root);
839 	else
840 		ht = u32_lookup_ht(tp_c, TC_U32_HTID(n->handle));
841 
842 	ins = &ht->ht[TC_U32_HASH(n->handle)];
843 
844 	/* The node must always exist for it to be replaced if this is not the
845 	 * case then something went very wrong elsewhere.
846 	 */
847 	for (pins = rtnl_dereference(*ins); ;
848 	     ins = &pins->next, pins = rtnl_dereference(*ins))
849 		if (pins->handle == n->handle)
850 			break;
851 
852 	idr_replace_ext(&ht->handle_idr, n, n->handle);
853 	RCU_INIT_POINTER(n->next, pins->next);
854 	rcu_assign_pointer(*ins, n);
855 }
856 
857 static struct tc_u_knode *u32_init_knode(struct tcf_proto *tp,
858 					 struct tc_u_knode *n)
859 {
860 	struct tc_u_hnode *ht = rtnl_dereference(n->ht_down);
861 	struct tc_u32_sel *s = &n->sel;
862 	struct tc_u_knode *new;
863 
864 	new = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key),
865 		      GFP_KERNEL);
866 
867 	if (!new)
868 		return NULL;
869 
870 	RCU_INIT_POINTER(new->next, n->next);
871 	new->handle = n->handle;
872 	RCU_INIT_POINTER(new->ht_up, n->ht_up);
873 
874 #ifdef CONFIG_NET_CLS_IND
875 	new->ifindex = n->ifindex;
876 #endif
877 	new->fshift = n->fshift;
878 	new->res = n->res;
879 	new->flags = n->flags;
880 	RCU_INIT_POINTER(new->ht_down, ht);
881 
882 	/* bump reference count as long as we hold pointer to structure */
883 	if (ht)
884 		ht->refcnt++;
885 
886 #ifdef CONFIG_CLS_U32_PERF
887 	/* Statistics may be incremented by readers during update
888 	 * so we must keep them in tact. When the node is later destroyed
889 	 * a special destroy call must be made to not free the pf memory.
890 	 */
891 	new->pf = n->pf;
892 #endif
893 
894 #ifdef CONFIG_CLS_U32_MARK
895 	new->val = n->val;
896 	new->mask = n->mask;
897 	/* Similarly success statistics must be moved as pointers */
898 	new->pcpu_success = n->pcpu_success;
899 #endif
900 	new->tp = tp;
901 	memcpy(&new->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
902 
903 	if (tcf_exts_init(&new->exts, TCA_U32_ACT, TCA_U32_POLICE)) {
904 		kfree(new);
905 		return NULL;
906 	}
907 
908 	return new;
909 }
910 
911 static int u32_change(struct net *net, struct sk_buff *in_skb,
912 		      struct tcf_proto *tp, unsigned long base, u32 handle,
913 		      struct nlattr **tca, void **arg, bool ovr,
914 		      struct netlink_ext_ack *extack)
915 {
916 	struct tc_u_common *tp_c = tp->data;
917 	struct tc_u_hnode *ht;
918 	struct tc_u_knode *n;
919 	struct tc_u32_sel *s;
920 	struct nlattr *opt = tca[TCA_OPTIONS];
921 	struct nlattr *tb[TCA_U32_MAX + 1];
922 	u32 htid, flags = 0;
923 	int err;
924 #ifdef CONFIG_CLS_U32_PERF
925 	size_t size;
926 #endif
927 
928 	if (!opt) {
929 		if (handle) {
930 			NL_SET_ERR_MSG_MOD(extack, "Filter handle requires options");
931 			return -EINVAL;
932 		} else {
933 			return 0;
934 		}
935 	}
936 
937 	err = nla_parse_nested(tb, TCA_U32_MAX, opt, u32_policy, extack);
938 	if (err < 0)
939 		return err;
940 
941 	if (tb[TCA_U32_FLAGS]) {
942 		flags = nla_get_u32(tb[TCA_U32_FLAGS]);
943 		if (!tc_flags_valid(flags)) {
944 			NL_SET_ERR_MSG_MOD(extack, "Invalid filter flags");
945 			return -EINVAL;
946 		}
947 	}
948 
949 	n = *arg;
950 	if (n) {
951 		struct tc_u_knode *new;
952 
953 		if (TC_U32_KEY(n->handle) == 0) {
954 			NL_SET_ERR_MSG_MOD(extack, "Key node id cannot be zero");
955 			return -EINVAL;
956 		}
957 
958 		if (n->flags != flags) {
959 			NL_SET_ERR_MSG_MOD(extack, "Key node flags do not match passed flags");
960 			return -EINVAL;
961 		}
962 
963 		new = u32_init_knode(tp, n);
964 		if (!new)
965 			return -ENOMEM;
966 
967 		err = u32_set_parms(net, tp, base,
968 				    rtnl_dereference(n->ht_up), new, tb,
969 				    tca[TCA_RATE], ovr, extack);
970 
971 		if (err) {
972 			u32_destroy_key(tp, new, false);
973 			return err;
974 		}
975 
976 		err = u32_replace_hw_knode(tp, new, flags, extack);
977 		if (err) {
978 			u32_destroy_key(tp, new, false);
979 			return err;
980 		}
981 
982 		if (!tc_in_hw(new->flags))
983 			new->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
984 
985 		u32_replace_knode(tp, tp_c, new);
986 		tcf_unbind_filter(tp, &n->res);
987 		tcf_exts_get_net(&n->exts);
988 		call_rcu(&n->rcu, u32_delete_key_rcu);
989 		return 0;
990 	}
991 
992 	if (tb[TCA_U32_DIVISOR]) {
993 		unsigned int divisor = nla_get_u32(tb[TCA_U32_DIVISOR]);
994 
995 		if (--divisor > 0x100) {
996 			NL_SET_ERR_MSG_MOD(extack, "Exceeded maximum 256 hash buckets");
997 			return -EINVAL;
998 		}
999 		if (TC_U32_KEY(handle)) {
1000 			NL_SET_ERR_MSG_MOD(extack, "Divisor can only be used on a hash table");
1001 			return -EINVAL;
1002 		}
1003 		ht = kzalloc(sizeof(*ht) + divisor*sizeof(void *), GFP_KERNEL);
1004 		if (ht == NULL)
1005 			return -ENOBUFS;
1006 		if (handle == 0) {
1007 			handle = gen_new_htid(tp->data, ht);
1008 			if (handle == 0) {
1009 				kfree(ht);
1010 				return -ENOMEM;
1011 			}
1012 		} else {
1013 			err = idr_alloc_ext(&tp_c->handle_idr, ht, NULL,
1014 					    handle, handle + 1, GFP_KERNEL);
1015 			if (err) {
1016 				kfree(ht);
1017 				return err;
1018 			}
1019 		}
1020 		ht->tp_c = tp_c;
1021 		ht->refcnt = 1;
1022 		ht->divisor = divisor;
1023 		ht->handle = handle;
1024 		ht->prio = tp->prio;
1025 		idr_init(&ht->handle_idr);
1026 		ht->flags = flags;
1027 
1028 		err = u32_replace_hw_hnode(tp, ht, flags, extack);
1029 		if (err) {
1030 			idr_remove_ext(&tp_c->handle_idr, handle);
1031 			kfree(ht);
1032 			return err;
1033 		}
1034 
1035 		RCU_INIT_POINTER(ht->next, tp_c->hlist);
1036 		rcu_assign_pointer(tp_c->hlist, ht);
1037 		*arg = ht;
1038 
1039 		return 0;
1040 	}
1041 
1042 	if (tb[TCA_U32_HASH]) {
1043 		htid = nla_get_u32(tb[TCA_U32_HASH]);
1044 		if (TC_U32_HTID(htid) == TC_U32_ROOT) {
1045 			ht = rtnl_dereference(tp->root);
1046 			htid = ht->handle;
1047 		} else {
1048 			ht = u32_lookup_ht(tp->data, TC_U32_HTID(htid));
1049 			if (!ht) {
1050 				NL_SET_ERR_MSG_MOD(extack, "Specified hash table not found");
1051 				return -EINVAL;
1052 			}
1053 		}
1054 	} else {
1055 		ht = rtnl_dereference(tp->root);
1056 		htid = ht->handle;
1057 	}
1058 
1059 	if (ht->divisor < TC_U32_HASH(htid)) {
1060 		NL_SET_ERR_MSG_MOD(extack, "Specified hash table buckets exceed configured value");
1061 		return -EINVAL;
1062 	}
1063 
1064 	if (handle) {
1065 		if (TC_U32_HTID(handle) && TC_U32_HTID(handle ^ htid)) {
1066 			NL_SET_ERR_MSG_MOD(extack, "Handle specified hash table address mismatch");
1067 			return -EINVAL;
1068 		}
1069 		handle = htid | TC_U32_NODE(handle);
1070 		err = idr_alloc_ext(&ht->handle_idr, NULL, NULL,
1071 				    handle, handle + 1,
1072 				    GFP_KERNEL);
1073 		if (err)
1074 			return err;
1075 	} else
1076 		handle = gen_new_kid(ht, htid);
1077 
1078 	if (tb[TCA_U32_SEL] == NULL) {
1079 		NL_SET_ERR_MSG_MOD(extack, "Selector not specified");
1080 		err = -EINVAL;
1081 		goto erridr;
1082 	}
1083 
1084 	s = nla_data(tb[TCA_U32_SEL]);
1085 
1086 	n = kzalloc(sizeof(*n) + s->nkeys*sizeof(struct tc_u32_key), GFP_KERNEL);
1087 	if (n == NULL) {
1088 		err = -ENOBUFS;
1089 		goto erridr;
1090 	}
1091 
1092 #ifdef CONFIG_CLS_U32_PERF
1093 	size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
1094 	n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
1095 	if (!n->pf) {
1096 		err = -ENOBUFS;
1097 		goto errfree;
1098 	}
1099 #endif
1100 
1101 	memcpy(&n->sel, s, sizeof(*s) + s->nkeys*sizeof(struct tc_u32_key));
1102 	RCU_INIT_POINTER(n->ht_up, ht);
1103 	n->handle = handle;
1104 	n->fshift = s->hmask ? ffs(ntohl(s->hmask)) - 1 : 0;
1105 	n->flags = flags;
1106 	n->tp = tp;
1107 
1108 	err = tcf_exts_init(&n->exts, TCA_U32_ACT, TCA_U32_POLICE);
1109 	if (err < 0)
1110 		goto errout;
1111 
1112 #ifdef CONFIG_CLS_U32_MARK
1113 	n->pcpu_success = alloc_percpu(u32);
1114 	if (!n->pcpu_success) {
1115 		err = -ENOMEM;
1116 		goto errout;
1117 	}
1118 
1119 	if (tb[TCA_U32_MARK]) {
1120 		struct tc_u32_mark *mark;
1121 
1122 		mark = nla_data(tb[TCA_U32_MARK]);
1123 		n->val = mark->val;
1124 		n->mask = mark->mask;
1125 	}
1126 #endif
1127 
1128 	err = u32_set_parms(net, tp, base, ht, n, tb, tca[TCA_RATE], ovr,
1129 			    extack);
1130 	if (err == 0) {
1131 		struct tc_u_knode __rcu **ins;
1132 		struct tc_u_knode *pins;
1133 
1134 		err = u32_replace_hw_knode(tp, n, flags, extack);
1135 		if (err)
1136 			goto errhw;
1137 
1138 		if (!tc_in_hw(n->flags))
1139 			n->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1140 
1141 		ins = &ht->ht[TC_U32_HASH(handle)];
1142 		for (pins = rtnl_dereference(*ins); pins;
1143 		     ins = &pins->next, pins = rtnl_dereference(*ins))
1144 			if (TC_U32_NODE(handle) < TC_U32_NODE(pins->handle))
1145 				break;
1146 
1147 		RCU_INIT_POINTER(n->next, pins);
1148 		rcu_assign_pointer(*ins, n);
1149 		*arg = n;
1150 		return 0;
1151 	}
1152 
1153 errhw:
1154 #ifdef CONFIG_CLS_U32_MARK
1155 	free_percpu(n->pcpu_success);
1156 #endif
1157 
1158 errout:
1159 	tcf_exts_destroy(&n->exts);
1160 #ifdef CONFIG_CLS_U32_PERF
1161 errfree:
1162 	free_percpu(n->pf);
1163 #endif
1164 	kfree(n);
1165 erridr:
1166 	idr_remove_ext(&ht->handle_idr, handle);
1167 	return err;
1168 }
1169 
1170 static void u32_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1171 {
1172 	struct tc_u_common *tp_c = tp->data;
1173 	struct tc_u_hnode *ht;
1174 	struct tc_u_knode *n;
1175 	unsigned int h;
1176 
1177 	if (arg->stop)
1178 		return;
1179 
1180 	for (ht = rtnl_dereference(tp_c->hlist);
1181 	     ht;
1182 	     ht = rtnl_dereference(ht->next)) {
1183 		if (ht->prio != tp->prio)
1184 			continue;
1185 		if (arg->count >= arg->skip) {
1186 			if (arg->fn(tp, ht, arg) < 0) {
1187 				arg->stop = 1;
1188 				return;
1189 			}
1190 		}
1191 		arg->count++;
1192 		for (h = 0; h <= ht->divisor; h++) {
1193 			for (n = rtnl_dereference(ht->ht[h]);
1194 			     n;
1195 			     n = rtnl_dereference(n->next)) {
1196 				if (arg->count < arg->skip) {
1197 					arg->count++;
1198 					continue;
1199 				}
1200 				if (arg->fn(tp, n, arg) < 0) {
1201 					arg->stop = 1;
1202 					return;
1203 				}
1204 				arg->count++;
1205 			}
1206 		}
1207 	}
1208 }
1209 
1210 static void u32_bind_class(void *fh, u32 classid, unsigned long cl)
1211 {
1212 	struct tc_u_knode *n = fh;
1213 
1214 	if (n && n->res.classid == classid)
1215 		n->res.class = cl;
1216 }
1217 
1218 static int u32_dump(struct net *net, struct tcf_proto *tp, void *fh,
1219 		    struct sk_buff *skb, struct tcmsg *t)
1220 {
1221 	struct tc_u_knode *n = fh;
1222 	struct tc_u_hnode *ht_up, *ht_down;
1223 	struct nlattr *nest;
1224 
1225 	if (n == NULL)
1226 		return skb->len;
1227 
1228 	t->tcm_handle = n->handle;
1229 
1230 	nest = nla_nest_start(skb, TCA_OPTIONS);
1231 	if (nest == NULL)
1232 		goto nla_put_failure;
1233 
1234 	if (TC_U32_KEY(n->handle) == 0) {
1235 		struct tc_u_hnode *ht = fh;
1236 		u32 divisor = ht->divisor + 1;
1237 
1238 		if (nla_put_u32(skb, TCA_U32_DIVISOR, divisor))
1239 			goto nla_put_failure;
1240 	} else {
1241 #ifdef CONFIG_CLS_U32_PERF
1242 		struct tc_u32_pcnt *gpf;
1243 		int cpu;
1244 #endif
1245 
1246 		if (nla_put(skb, TCA_U32_SEL,
1247 			    sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
1248 			    &n->sel))
1249 			goto nla_put_failure;
1250 
1251 		ht_up = rtnl_dereference(n->ht_up);
1252 		if (ht_up) {
1253 			u32 htid = n->handle & 0xFFFFF000;
1254 			if (nla_put_u32(skb, TCA_U32_HASH, htid))
1255 				goto nla_put_failure;
1256 		}
1257 		if (n->res.classid &&
1258 		    nla_put_u32(skb, TCA_U32_CLASSID, n->res.classid))
1259 			goto nla_put_failure;
1260 
1261 		ht_down = rtnl_dereference(n->ht_down);
1262 		if (ht_down &&
1263 		    nla_put_u32(skb, TCA_U32_LINK, ht_down->handle))
1264 			goto nla_put_failure;
1265 
1266 		if (n->flags && nla_put_u32(skb, TCA_U32_FLAGS, n->flags))
1267 			goto nla_put_failure;
1268 
1269 #ifdef CONFIG_CLS_U32_MARK
1270 		if ((n->val || n->mask)) {
1271 			struct tc_u32_mark mark = {.val = n->val,
1272 						   .mask = n->mask,
1273 						   .success = 0};
1274 			int cpum;
1275 
1276 			for_each_possible_cpu(cpum) {
1277 				__u32 cnt = *per_cpu_ptr(n->pcpu_success, cpum);
1278 
1279 				mark.success += cnt;
1280 			}
1281 
1282 			if (nla_put(skb, TCA_U32_MARK, sizeof(mark), &mark))
1283 				goto nla_put_failure;
1284 		}
1285 #endif
1286 
1287 		if (tcf_exts_dump(skb, &n->exts) < 0)
1288 			goto nla_put_failure;
1289 
1290 #ifdef CONFIG_NET_CLS_IND
1291 		if (n->ifindex) {
1292 			struct net_device *dev;
1293 			dev = __dev_get_by_index(net, n->ifindex);
1294 			if (dev && nla_put_string(skb, TCA_U32_INDEV, dev->name))
1295 				goto nla_put_failure;
1296 		}
1297 #endif
1298 #ifdef CONFIG_CLS_U32_PERF
1299 		gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
1300 			      n->sel.nkeys * sizeof(u64),
1301 			      GFP_KERNEL);
1302 		if (!gpf)
1303 			goto nla_put_failure;
1304 
1305 		for_each_possible_cpu(cpu) {
1306 			int i;
1307 			struct tc_u32_pcnt *pf = per_cpu_ptr(n->pf, cpu);
1308 
1309 			gpf->rcnt += pf->rcnt;
1310 			gpf->rhit += pf->rhit;
1311 			for (i = 0; i < n->sel.nkeys; i++)
1312 				gpf->kcnts[i] += pf->kcnts[i];
1313 		}
1314 
1315 		if (nla_put_64bit(skb, TCA_U32_PCNT,
1316 				  sizeof(struct tc_u32_pcnt) +
1317 				  n->sel.nkeys * sizeof(u64),
1318 				  gpf, TCA_U32_PAD)) {
1319 			kfree(gpf);
1320 			goto nla_put_failure;
1321 		}
1322 		kfree(gpf);
1323 #endif
1324 	}
1325 
1326 	nla_nest_end(skb, nest);
1327 
1328 	if (TC_U32_KEY(n->handle))
1329 		if (tcf_exts_dump_stats(skb, &n->exts) < 0)
1330 			goto nla_put_failure;
1331 	return skb->len;
1332 
1333 nla_put_failure:
1334 	nla_nest_cancel(skb, nest);
1335 	return -1;
1336 }
1337 
1338 static struct tcf_proto_ops cls_u32_ops __read_mostly = {
1339 	.kind		=	"u32",
1340 	.classify	=	u32_classify,
1341 	.init		=	u32_init,
1342 	.destroy	=	u32_destroy,
1343 	.get		=	u32_get,
1344 	.change		=	u32_change,
1345 	.delete		=	u32_delete,
1346 	.walk		=	u32_walk,
1347 	.dump		=	u32_dump,
1348 	.bind_class	=	u32_bind_class,
1349 	.owner		=	THIS_MODULE,
1350 };
1351 
1352 static int __init init_u32(void)
1353 {
1354 	int i, ret;
1355 
1356 	pr_info("u32 classifier\n");
1357 #ifdef CONFIG_CLS_U32_PERF
1358 	pr_info("    Performance counters on\n");
1359 #endif
1360 #ifdef CONFIG_NET_CLS_IND
1361 	pr_info("    input device check on\n");
1362 #endif
1363 #ifdef CONFIG_NET_CLS_ACT
1364 	pr_info("    Actions configured\n");
1365 #endif
1366 	tc_u_common_hash = kvmalloc_array(U32_HASH_SIZE,
1367 					  sizeof(struct hlist_head),
1368 					  GFP_KERNEL);
1369 	if (!tc_u_common_hash)
1370 		return -ENOMEM;
1371 
1372 	for (i = 0; i < U32_HASH_SIZE; i++)
1373 		INIT_HLIST_HEAD(&tc_u_common_hash[i]);
1374 
1375 	ret = register_tcf_proto_ops(&cls_u32_ops);
1376 	if (ret)
1377 		kvfree(tc_u_common_hash);
1378 	return ret;
1379 }
1380 
1381 static void __exit exit_u32(void)
1382 {
1383 	unregister_tcf_proto_ops(&cls_u32_ops);
1384 	kvfree(tc_u_common_hash);
1385 }
1386 
1387 module_init(init_u32)
1388 module_exit(exit_u32)
1389 MODULE_LICENSE("GPL");
1390