xref: /linux/net/sched/cls_flow.c (revision f294fc71c4a0fa4964f6428a1b4e7929c1d83125)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/cls_flow.c		Generic flow classifier
4  *
5  * Copyright (c) 2007, 2008 Patrick McHardy <kaber@trash.net>
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/list.h>
11 #include <linux/jhash.h>
12 #include <linux/random.h>
13 #include <linux/pkt_cls.h>
14 #include <linux/skbuff.h>
15 #include <linux/in.h>
16 #include <linux/ip.h>
17 #include <linux/ipv6.h>
18 #include <linux/if_vlan.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <net/inet_sock.h>
22 
23 #include <net/pkt_cls.h>
24 #include <linux/siphash.h>
25 #include <net/ip.h>
26 #include <net/route.h>
27 #include <net/flow_dissector.h>
28 #include <net/tc_wrapper.h>
29 
30 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
31 #include <net/netfilter/nf_conntrack.h>
32 #endif
33 
34 struct flow_head {
35 	struct list_head	filters;
36 	struct rcu_head		rcu;
37 };
38 
39 struct flow_filter {
40 	struct list_head	list;
41 	struct tcf_exts		exts;
42 	struct tcf_ematch_tree	ematches;
43 	struct tcf_proto	*tp;
44 	struct timer_list	perturb_timer;
45 	u32			perturb_period;
46 	u32			handle;
47 
48 	u32			nkeys;
49 	u32			keymask;
50 	u32			mode;
51 	u32			mask;
52 	u32			xor;
53 	u32			rshift;
54 	u32			addend;
55 	u32			divisor;
56 	u32			baseclass;
57 	u32			hashrnd;
58 	struct rcu_work		rwork;
59 };
60 
61 static siphash_aligned_key_t flow_keys_secret __read_mostly;
62 
63 static inline u32 addr_fold(void *addr)
64 {
65 #ifdef CONFIG_64BIT
66 	return (u32)siphash_1u64((u64)addr, &flow_keys_secret);
67 #else
68 	return (u32)siphash_1u32((u32)addr, &flow_keys_secret);
69 #endif
70 }
71 
72 static u32 flow_get_src(const struct sk_buff *skb, const struct flow_keys *flow)
73 {
74 	__be32 src = flow_get_u32_src(flow);
75 
76 	if (src)
77 		return ntohl(src);
78 
79 	return addr_fold(skb->sk);
80 }
81 
82 static u32 flow_get_dst(const struct sk_buff *skb, const struct flow_keys *flow)
83 {
84 	__be32 dst = flow_get_u32_dst(flow);
85 
86 	if (dst)
87 		return ntohl(dst);
88 
89 	return addr_fold(skb_dst(skb)) ^ (__force u16)skb_protocol(skb, true);
90 }
91 
92 static u32 flow_get_proto(const struct sk_buff *skb,
93 			  const struct flow_keys *flow)
94 {
95 	return flow->basic.ip_proto;
96 }
97 
98 static u32 flow_get_proto_src(const struct sk_buff *skb,
99 			      const struct flow_keys *flow)
100 {
101 	if (flow->ports.ports)
102 		return ntohs(flow->ports.src);
103 
104 	return addr_fold(skb->sk);
105 }
106 
107 static u32 flow_get_proto_dst(const struct sk_buff *skb,
108 			      const struct flow_keys *flow)
109 {
110 	if (flow->ports.ports)
111 		return ntohs(flow->ports.dst);
112 
113 	return addr_fold(skb_dst(skb)) ^ (__force u16)skb_protocol(skb, true);
114 }
115 
116 static u32 flow_get_iif(const struct sk_buff *skb)
117 {
118 	return skb->skb_iif;
119 }
120 
121 static u32 flow_get_priority(const struct sk_buff *skb)
122 {
123 	return skb->priority;
124 }
125 
126 static u32 flow_get_mark(const struct sk_buff *skb)
127 {
128 	return skb->mark;
129 }
130 
131 static u32 flow_get_nfct(const struct sk_buff *skb)
132 {
133 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
134 	return addr_fold(skb_nfct(skb));
135 #else
136 	return 0;
137 #endif
138 }
139 
140 #if IS_ENABLED(CONFIG_NF_CONNTRACK)
141 #define CTTUPLE(skb, member)						\
142 ({									\
143 	enum ip_conntrack_info ctinfo;					\
144 	const struct nf_conn *ct = nf_ct_get(skb, &ctinfo);		\
145 	if (ct == NULL)							\
146 		goto fallback;						\
147 	ct->tuplehash[CTINFO2DIR(ctinfo)].tuple.member;			\
148 })
149 #else
150 #define CTTUPLE(skb, member)						\
151 ({									\
152 	goto fallback;							\
153 	0;								\
154 })
155 #endif
156 
157 static u32 flow_get_nfct_src(const struct sk_buff *skb,
158 			     const struct flow_keys *flow)
159 {
160 	switch (skb_protocol(skb, true)) {
161 	case htons(ETH_P_IP):
162 		return ntohl(CTTUPLE(skb, src.u3.ip));
163 	case htons(ETH_P_IPV6):
164 		return ntohl(CTTUPLE(skb, src.u3.ip6[3]));
165 	}
166 fallback:
167 	return flow_get_src(skb, flow);
168 }
169 
170 static u32 flow_get_nfct_dst(const struct sk_buff *skb,
171 			     const struct flow_keys *flow)
172 {
173 	switch (skb_protocol(skb, true)) {
174 	case htons(ETH_P_IP):
175 		return ntohl(CTTUPLE(skb, dst.u3.ip));
176 	case htons(ETH_P_IPV6):
177 		return ntohl(CTTUPLE(skb, dst.u3.ip6[3]));
178 	}
179 fallback:
180 	return flow_get_dst(skb, flow);
181 }
182 
183 static u32 flow_get_nfct_proto_src(const struct sk_buff *skb,
184 				   const struct flow_keys *flow)
185 {
186 	return ntohs(CTTUPLE(skb, src.u.all));
187 fallback:
188 	return flow_get_proto_src(skb, flow);
189 }
190 
191 static u32 flow_get_nfct_proto_dst(const struct sk_buff *skb,
192 				   const struct flow_keys *flow)
193 {
194 	return ntohs(CTTUPLE(skb, dst.u.all));
195 fallback:
196 	return flow_get_proto_dst(skb, flow);
197 }
198 
199 static u32 flow_get_rtclassid(const struct sk_buff *skb)
200 {
201 #ifdef CONFIG_IP_ROUTE_CLASSID
202 	if (skb_dst(skb))
203 		return skb_dst(skb)->tclassid;
204 #endif
205 	return 0;
206 }
207 
208 static u32 flow_get_skuid(const struct sk_buff *skb)
209 {
210 	struct sock *sk = skb_to_full_sk(skb);
211 
212 	if (sk && sk->sk_socket && sk->sk_socket->file) {
213 		kuid_t skuid = sk->sk_socket->file->f_cred->fsuid;
214 
215 		return from_kuid(&init_user_ns, skuid);
216 	}
217 	return 0;
218 }
219 
220 static u32 flow_get_skgid(const struct sk_buff *skb)
221 {
222 	struct sock *sk = skb_to_full_sk(skb);
223 
224 	if (sk && sk->sk_socket && sk->sk_socket->file) {
225 		kgid_t skgid = sk->sk_socket->file->f_cred->fsgid;
226 
227 		return from_kgid(&init_user_ns, skgid);
228 	}
229 	return 0;
230 }
231 
232 static u32 flow_get_vlan_tag(const struct sk_buff *skb)
233 {
234 	u16 tag;
235 
236 	if (vlan_get_tag(skb, &tag) < 0)
237 		return 0;
238 	return tag & VLAN_VID_MASK;
239 }
240 
241 static u32 flow_get_rxhash(struct sk_buff *skb)
242 {
243 	return skb_get_hash(skb);
244 }
245 
246 static u32 flow_key_get(struct sk_buff *skb, int key, struct flow_keys *flow)
247 {
248 	switch (key) {
249 	case FLOW_KEY_SRC:
250 		return flow_get_src(skb, flow);
251 	case FLOW_KEY_DST:
252 		return flow_get_dst(skb, flow);
253 	case FLOW_KEY_PROTO:
254 		return flow_get_proto(skb, flow);
255 	case FLOW_KEY_PROTO_SRC:
256 		return flow_get_proto_src(skb, flow);
257 	case FLOW_KEY_PROTO_DST:
258 		return flow_get_proto_dst(skb, flow);
259 	case FLOW_KEY_IIF:
260 		return flow_get_iif(skb);
261 	case FLOW_KEY_PRIORITY:
262 		return flow_get_priority(skb);
263 	case FLOW_KEY_MARK:
264 		return flow_get_mark(skb);
265 	case FLOW_KEY_NFCT:
266 		return flow_get_nfct(skb);
267 	case FLOW_KEY_NFCT_SRC:
268 		return flow_get_nfct_src(skb, flow);
269 	case FLOW_KEY_NFCT_DST:
270 		return flow_get_nfct_dst(skb, flow);
271 	case FLOW_KEY_NFCT_PROTO_SRC:
272 		return flow_get_nfct_proto_src(skb, flow);
273 	case FLOW_KEY_NFCT_PROTO_DST:
274 		return flow_get_nfct_proto_dst(skb, flow);
275 	case FLOW_KEY_RTCLASSID:
276 		return flow_get_rtclassid(skb);
277 	case FLOW_KEY_SKUID:
278 		return flow_get_skuid(skb);
279 	case FLOW_KEY_SKGID:
280 		return flow_get_skgid(skb);
281 	case FLOW_KEY_VLAN_TAG:
282 		return flow_get_vlan_tag(skb);
283 	case FLOW_KEY_RXHASH:
284 		return flow_get_rxhash(skb);
285 	default:
286 		WARN_ON(1);
287 		return 0;
288 	}
289 }
290 
291 #define FLOW_KEYS_NEEDED ((1 << FLOW_KEY_SRC) | 		\
292 			  (1 << FLOW_KEY_DST) |			\
293 			  (1 << FLOW_KEY_PROTO) |		\
294 			  (1 << FLOW_KEY_PROTO_SRC) |		\
295 			  (1 << FLOW_KEY_PROTO_DST) | 		\
296 			  (1 << FLOW_KEY_NFCT_SRC) |		\
297 			  (1 << FLOW_KEY_NFCT_DST) |		\
298 			  (1 << FLOW_KEY_NFCT_PROTO_SRC) |	\
299 			  (1 << FLOW_KEY_NFCT_PROTO_DST))
300 
301 TC_INDIRECT_SCOPE int flow_classify(struct sk_buff *skb,
302 				    const struct tcf_proto *tp,
303 				    struct tcf_result *res)
304 {
305 	struct flow_head *head = rcu_dereference_bh(tp->root);
306 	struct flow_filter *f;
307 	u32 keymask;
308 	u32 classid;
309 	unsigned int n, key;
310 	int r;
311 
312 	list_for_each_entry_rcu(f, &head->filters, list) {
313 		u32 keys[FLOW_KEY_MAX + 1];
314 		struct flow_keys flow_keys;
315 
316 		if (!tcf_em_tree_match(skb, &f->ematches, NULL))
317 			continue;
318 
319 		keymask = f->keymask;
320 		if (keymask & FLOW_KEYS_NEEDED)
321 			skb_flow_dissect_flow_keys(skb, &flow_keys, 0);
322 
323 		for (n = 0; n < f->nkeys; n++) {
324 			key = ffs(keymask) - 1;
325 			keymask &= ~(1 << key);
326 			keys[n] = flow_key_get(skb, key, &flow_keys);
327 		}
328 
329 		if (f->mode == FLOW_MODE_HASH)
330 			classid = jhash2(keys, f->nkeys, f->hashrnd);
331 		else {
332 			classid = keys[0];
333 			classid = (classid & f->mask) ^ f->xor;
334 			classid = (classid >> f->rshift) + f->addend;
335 		}
336 
337 		if (f->divisor)
338 			classid %= f->divisor;
339 
340 		res->class   = 0;
341 		res->classid = TC_H_MAKE(f->baseclass, f->baseclass + classid);
342 
343 		r = tcf_exts_exec(skb, &f->exts, res);
344 		if (r < 0)
345 			continue;
346 		return r;
347 	}
348 	return -1;
349 }
350 
351 static void flow_perturbation(struct timer_list *t)
352 {
353 	struct flow_filter *f = timer_container_of(f, t, perturb_timer);
354 
355 	get_random_bytes(&f->hashrnd, 4);
356 	if (f->perturb_period)
357 		mod_timer(&f->perturb_timer, jiffies + f->perturb_period);
358 }
359 
360 static const struct nla_policy flow_policy[TCA_FLOW_MAX + 1] = {
361 	[TCA_FLOW_KEYS]		= { .type = NLA_U32 },
362 	[TCA_FLOW_MODE]		= { .type = NLA_U32 },
363 	[TCA_FLOW_BASECLASS]	= { .type = NLA_U32 },
364 	[TCA_FLOW_RSHIFT]	= NLA_POLICY_MAX(NLA_U32,
365 						 31 /* BITS_PER_U32 - 1 */),
366 	[TCA_FLOW_ADDEND]	= { .type = NLA_U32 },
367 	[TCA_FLOW_MASK]		= { .type = NLA_U32 },
368 	[TCA_FLOW_XOR]		= { .type = NLA_U32 },
369 	[TCA_FLOW_DIVISOR]	= { .type = NLA_U32 },
370 	[TCA_FLOW_ACT]		= { .type = NLA_NESTED },
371 	[TCA_FLOW_POLICE]	= { .type = NLA_NESTED },
372 	[TCA_FLOW_EMATCHES]	= { .type = NLA_NESTED },
373 	[TCA_FLOW_PERTURB]	= { .type = NLA_U32 },
374 };
375 
376 static void __flow_destroy_filter(struct flow_filter *f)
377 {
378 	timer_shutdown_sync(&f->perturb_timer);
379 	tcf_exts_destroy(&f->exts);
380 	tcf_em_tree_destroy(&f->ematches);
381 	tcf_exts_put_net(&f->exts);
382 	kfree(f);
383 }
384 
385 static void flow_destroy_filter_work(struct work_struct *work)
386 {
387 	struct flow_filter *f = container_of(to_rcu_work(work),
388 					     struct flow_filter,
389 					     rwork);
390 	rtnl_lock();
391 	__flow_destroy_filter(f);
392 	rtnl_unlock();
393 }
394 
395 static int flow_change(struct net *net, struct sk_buff *in_skb,
396 		       struct tcf_proto *tp, unsigned long base,
397 		       u32 handle, struct nlattr **tca,
398 		       void **arg, u32 flags,
399 		       struct netlink_ext_ack *extack)
400 {
401 	struct flow_head *head = rtnl_dereference(tp->root);
402 	struct flow_filter *fold, *fnew;
403 	struct nlattr *opt = tca[TCA_OPTIONS];
404 	struct nlattr *tb[TCA_FLOW_MAX + 1];
405 	unsigned int nkeys = 0;
406 	unsigned int perturb_period = 0;
407 	u32 baseclass = 0;
408 	u32 keymask = 0;
409 	u32 mode;
410 	int err;
411 
412 	if (opt == NULL)
413 		return -EINVAL;
414 
415 	err = nla_parse_nested_deprecated(tb, TCA_FLOW_MAX, opt, flow_policy,
416 					  NULL);
417 	if (err < 0)
418 		return err;
419 
420 	if (tb[TCA_FLOW_BASECLASS]) {
421 		baseclass = nla_get_u32(tb[TCA_FLOW_BASECLASS]);
422 		if (TC_H_MIN(baseclass) == 0)
423 			return -EINVAL;
424 	}
425 
426 	if (tb[TCA_FLOW_KEYS]) {
427 		keymask = nla_get_u32(tb[TCA_FLOW_KEYS]);
428 
429 		nkeys = hweight32(keymask);
430 		if (nkeys == 0)
431 			return -EINVAL;
432 
433 		if (fls(keymask) - 1 > FLOW_KEY_MAX)
434 			return -EOPNOTSUPP;
435 
436 		if ((keymask & (FLOW_KEY_SKUID|FLOW_KEY_SKGID)) &&
437 		    sk_user_ns(NETLINK_CB(in_skb).sk) != &init_user_ns)
438 			return -EOPNOTSUPP;
439 	}
440 
441 	fnew = kzalloc_obj(*fnew);
442 	if (!fnew)
443 		return -ENOBUFS;
444 
445 	err = tcf_em_tree_validate(tp, tb[TCA_FLOW_EMATCHES], &fnew->ematches);
446 	if (err < 0)
447 		goto err1;
448 
449 	err = tcf_exts_init(&fnew->exts, net, TCA_FLOW_ACT, TCA_FLOW_POLICE);
450 	if (err < 0)
451 		goto err2;
452 
453 	err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &fnew->exts, flags,
454 				extack);
455 	if (err < 0)
456 		goto err2;
457 
458 	fold = *arg;
459 	if (fold) {
460 		err = -EINVAL;
461 		if (fold->handle != handle && handle)
462 			goto err2;
463 
464 		/* Copy fold into fnew */
465 		fnew->tp = fold->tp;
466 		fnew->handle = fold->handle;
467 		fnew->nkeys = fold->nkeys;
468 		fnew->keymask = fold->keymask;
469 		fnew->mode = fold->mode;
470 		fnew->mask = fold->mask;
471 		fnew->xor = fold->xor;
472 		fnew->rshift = fold->rshift;
473 		fnew->addend = fold->addend;
474 		fnew->divisor = fold->divisor;
475 		fnew->baseclass = fold->baseclass;
476 		fnew->hashrnd = fold->hashrnd;
477 
478 		mode = fold->mode;
479 		if (tb[TCA_FLOW_MODE])
480 			mode = nla_get_u32(tb[TCA_FLOW_MODE]);
481 		if (mode != FLOW_MODE_HASH && nkeys > 1)
482 			goto err2;
483 
484 		if (mode == FLOW_MODE_HASH)
485 			perturb_period = fold->perturb_period;
486 		if (tb[TCA_FLOW_PERTURB]) {
487 			if (mode != FLOW_MODE_HASH)
488 				goto err2;
489 			perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
490 		}
491 	} else {
492 		err = -EINVAL;
493 		if (!handle)
494 			goto err2;
495 		if (!tb[TCA_FLOW_KEYS])
496 			goto err2;
497 
498 		mode = FLOW_MODE_MAP;
499 		if (tb[TCA_FLOW_MODE])
500 			mode = nla_get_u32(tb[TCA_FLOW_MODE]);
501 		if (mode != FLOW_MODE_HASH && nkeys > 1)
502 			goto err2;
503 
504 		if (tb[TCA_FLOW_PERTURB]) {
505 			if (mode != FLOW_MODE_HASH)
506 				goto err2;
507 			perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
508 		}
509 
510 		if (TC_H_MAJ(baseclass) == 0) {
511 			struct tcf_block *block = tp->chain->block;
512 			struct Qdisc *q;
513 
514 			if (tcf_block_shared(block)) {
515 				NL_SET_ERR_MSG(extack,
516 					       "Must specify baseclass when attaching flow filter to block");
517 				goto err2;
518 			}
519 
520 			q = tcf_block_q(block);
521 			baseclass = TC_H_MAKE(q->handle, baseclass);
522 		}
523 		if (TC_H_MIN(baseclass) == 0)
524 			baseclass = TC_H_MAKE(baseclass, 1);
525 
526 		fnew->handle = handle;
527 		fnew->mask  = ~0U;
528 		fnew->tp = tp;
529 		get_random_bytes(&fnew->hashrnd, 4);
530 	}
531 
532 	timer_setup(&fnew->perturb_timer, flow_perturbation, TIMER_DEFERRABLE);
533 
534 	tcf_block_netif_keep_dst(tp->chain->block);
535 
536 	if (tb[TCA_FLOW_KEYS]) {
537 		fnew->keymask = keymask;
538 		fnew->nkeys   = nkeys;
539 	}
540 
541 	fnew->mode = mode;
542 
543 	if (tb[TCA_FLOW_MASK])
544 		fnew->mask = nla_get_u32(tb[TCA_FLOW_MASK]);
545 	if (tb[TCA_FLOW_XOR])
546 		fnew->xor = nla_get_u32(tb[TCA_FLOW_XOR]);
547 	if (tb[TCA_FLOW_RSHIFT])
548 		fnew->rshift = nla_get_u32(tb[TCA_FLOW_RSHIFT]);
549 	if (tb[TCA_FLOW_ADDEND])
550 		fnew->addend = nla_get_u32(tb[TCA_FLOW_ADDEND]);
551 
552 	if (tb[TCA_FLOW_DIVISOR])
553 		fnew->divisor = nla_get_u32(tb[TCA_FLOW_DIVISOR]);
554 	if (baseclass)
555 		fnew->baseclass = baseclass;
556 
557 	fnew->perturb_period = perturb_period;
558 	if (perturb_period)
559 		mod_timer(&fnew->perturb_timer, jiffies + perturb_period);
560 
561 	if (!*arg)
562 		list_add_tail_rcu(&fnew->list, &head->filters);
563 	else
564 		list_replace_rcu(&fold->list, &fnew->list);
565 
566 	*arg = fnew;
567 
568 	if (fold) {
569 		tcf_exts_get_net(&fold->exts);
570 		tcf_queue_work(&fold->rwork, flow_destroy_filter_work);
571 	}
572 	return 0;
573 
574 err2:
575 	tcf_exts_destroy(&fnew->exts);
576 	tcf_em_tree_destroy(&fnew->ematches);
577 err1:
578 	kfree(fnew);
579 	return err;
580 }
581 
582 static int flow_delete(struct tcf_proto *tp, void *arg, bool *last,
583 		       bool rtnl_held, struct netlink_ext_ack *extack)
584 {
585 	struct flow_head *head = rtnl_dereference(tp->root);
586 	struct flow_filter *f = arg;
587 
588 	list_del_rcu(&f->list);
589 	tcf_exts_get_net(&f->exts);
590 	tcf_queue_work(&f->rwork, flow_destroy_filter_work);
591 	*last = list_empty(&head->filters);
592 	return 0;
593 }
594 
595 static int flow_init(struct tcf_proto *tp)
596 {
597 	struct flow_head *head;
598 
599 	head = kzalloc_obj(*head);
600 	if (head == NULL)
601 		return -ENOBUFS;
602 	INIT_LIST_HEAD(&head->filters);
603 	rcu_assign_pointer(tp->root, head);
604 	net_get_random_once(&flow_keys_secret, sizeof(flow_keys_secret));
605 	return 0;
606 }
607 
608 static void flow_destroy(struct tcf_proto *tp, bool rtnl_held,
609 			 struct netlink_ext_ack *extack)
610 {
611 	struct flow_head *head = rtnl_dereference(tp->root);
612 	struct flow_filter *f, *next;
613 
614 	list_for_each_entry_safe(f, next, &head->filters, list) {
615 		list_del_rcu(&f->list);
616 		if (tcf_exts_get_net(&f->exts))
617 			tcf_queue_work(&f->rwork, flow_destroy_filter_work);
618 		else
619 			__flow_destroy_filter(f);
620 	}
621 	kfree_rcu(head, rcu);
622 }
623 
624 static void *flow_get(struct tcf_proto *tp, u32 handle)
625 {
626 	struct flow_head *head = rtnl_dereference(tp->root);
627 	struct flow_filter *f;
628 
629 	list_for_each_entry(f, &head->filters, list)
630 		if (f->handle == handle)
631 			return f;
632 	return NULL;
633 }
634 
635 static int flow_dump(struct net *net, struct tcf_proto *tp, void *fh,
636 		     struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
637 {
638 	struct flow_filter *f = fh;
639 	struct nlattr *nest;
640 
641 	if (f == NULL)
642 		return skb->len;
643 
644 	t->tcm_handle = f->handle;
645 
646 	nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
647 	if (nest == NULL)
648 		goto nla_put_failure;
649 
650 	if (nla_put_u32(skb, TCA_FLOW_KEYS, f->keymask) ||
651 	    nla_put_u32(skb, TCA_FLOW_MODE, f->mode))
652 		goto nla_put_failure;
653 
654 	if (f->mask != ~0 || f->xor != 0) {
655 		if (nla_put_u32(skb, TCA_FLOW_MASK, f->mask) ||
656 		    nla_put_u32(skb, TCA_FLOW_XOR, f->xor))
657 			goto nla_put_failure;
658 	}
659 	if (f->rshift &&
660 	    nla_put_u32(skb, TCA_FLOW_RSHIFT, f->rshift))
661 		goto nla_put_failure;
662 	if (f->addend &&
663 	    nla_put_u32(skb, TCA_FLOW_ADDEND, f->addend))
664 		goto nla_put_failure;
665 
666 	if (f->divisor &&
667 	    nla_put_u32(skb, TCA_FLOW_DIVISOR, f->divisor))
668 		goto nla_put_failure;
669 	if (f->baseclass &&
670 	    nla_put_u32(skb, TCA_FLOW_BASECLASS, f->baseclass))
671 		goto nla_put_failure;
672 
673 	if (f->perturb_period &&
674 	    nla_put_u32(skb, TCA_FLOW_PERTURB, f->perturb_period / HZ))
675 		goto nla_put_failure;
676 
677 	if (tcf_exts_dump(skb, &f->exts) < 0)
678 		goto nla_put_failure;
679 #ifdef CONFIG_NET_EMATCH
680 	if (f->ematches.hdr.nmatches &&
681 	    tcf_em_tree_dump(skb, &f->ematches, TCA_FLOW_EMATCHES) < 0)
682 		goto nla_put_failure;
683 #endif
684 	nla_nest_end(skb, nest);
685 
686 	if (tcf_exts_dump_stats(skb, &f->exts) < 0)
687 		goto nla_put_failure;
688 
689 	return skb->len;
690 
691 nla_put_failure:
692 	nla_nest_cancel(skb, nest);
693 	return -1;
694 }
695 
696 static void flow_walk(struct tcf_proto *tp, struct tcf_walker *arg,
697 		      bool rtnl_held)
698 {
699 	struct flow_head *head = rtnl_dereference(tp->root);
700 	struct flow_filter *f;
701 
702 	list_for_each_entry(f, &head->filters, list) {
703 		if (!tc_cls_stats_dump(tp, arg, f))
704 			break;
705 	}
706 }
707 
708 static struct tcf_proto_ops cls_flow_ops __read_mostly = {
709 	.kind		= "flow",
710 	.classify	= flow_classify,
711 	.init		= flow_init,
712 	.destroy	= flow_destroy,
713 	.change		= flow_change,
714 	.delete		= flow_delete,
715 	.get		= flow_get,
716 	.dump		= flow_dump,
717 	.walk		= flow_walk,
718 	.owner		= THIS_MODULE,
719 };
720 MODULE_ALIAS_NET_CLS("flow");
721 
722 static int __init cls_flow_init(void)
723 {
724 	return register_tcf_proto_ops(&cls_flow_ops);
725 }
726 
727 static void __exit cls_flow_exit(void)
728 {
729 	unregister_tcf_proto_ops(&cls_flow_ops);
730 }
731 
732 module_init(cls_flow_init);
733 module_exit(cls_flow_exit);
734 
735 MODULE_LICENSE("GPL");
736 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
737 MODULE_DESCRIPTION("TC flow classifier");
738