Blame view

net/sched/cls_flow.c 15.9 KB
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  /*
   * net/sched/cls_flow.c		Generic flow classifier
   *
   * Copyright (c) 2007, 2008 Patrick McHardy <kaber@trash.net>
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   */
  
  #include <linux/kernel.h>
  #include <linux/init.h>
  #include <linux/list.h>
  #include <linux/jhash.h>
  #include <linux/random.h>
  #include <linux/pkt_cls.h>
  #include <linux/skbuff.h>
  #include <linux/in.h>
  #include <linux/ip.h>
  #include <linux/ipv6.h>
9ec138101   Patrick McHardy   [NET_SCHED]: cls_...
22
  #include <linux/if_vlan.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
23
  #include <linux/slab.h>
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  
  #include <net/pkt_cls.h>
  #include <net/ip.h>
  #include <net/route.h>
  #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  #include <net/netfilter/nf_conntrack.h>
  #endif
  
  struct flow_head {
  	struct list_head	filters;
  };
  
  struct flow_filter {
  	struct list_head	list;
  	struct tcf_exts		exts;
  	struct tcf_ematch_tree	ematches;
72d9794f4   Patrick McHardy   net-sched: cls_fl...
40
41
  	struct timer_list	perturb_timer;
  	u32			perturb_period;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
42
43
44
45
46
47
48
49
50
51
52
  	u32			handle;
  
  	u32			nkeys;
  	u32			keymask;
  	u32			mode;
  	u32			mask;
  	u32			xor;
  	u32			rshift;
  	u32			addend;
  	u32			divisor;
  	u32			baseclass;
72d9794f4   Patrick McHardy   net-sched: cls_fl...
53
  	u32			hashrnd;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
54
  };
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
55
56
57
58
59
60
61
62
63
64
65
  static const struct tcf_ext_map flow_ext_map = {
  	.action	= TCA_FLOW_ACT,
  	.police	= TCA_FLOW_POLICE,
  };
  
  static inline u32 addr_fold(void *addr)
  {
  	unsigned long a = (unsigned long)addr;
  
  	return (a & 0xFFFFFFFF) ^ (BITS_PER_LONG > 32 ? a >> 32 : 0);
  }
4b95c3d40   Changli Gao   cls_flow: add san...
66
  static u32 flow_get_src(struct sk_buff *skb)
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
67
68
  {
  	switch (skb->protocol) {
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
69
  	case htons(ETH_P_IP):
4b95c3d40   Changli Gao   cls_flow: add san...
70
71
72
  		if (pskb_network_may_pull(skb, sizeof(struct iphdr)))
  			return ntohl(ip_hdr(skb)->saddr);
  		break;
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
73
  	case htons(ETH_P_IPV6):
4b95c3d40   Changli Gao   cls_flow: add san...
74
75
76
  		if (pskb_network_may_pull(skb, sizeof(struct ipv6hdr)))
  			return ntohl(ipv6_hdr(skb)->saddr.s6_addr32[3]);
  		break;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
77
  	}
4b95c3d40   Changli Gao   cls_flow: add san...
78
79
  
  	return addr_fold(skb->sk);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
80
  }
4b95c3d40   Changli Gao   cls_flow: add san...
81
  static u32 flow_get_dst(struct sk_buff *skb)
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
82
83
  {
  	switch (skb->protocol) {
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
84
  	case htons(ETH_P_IP):
4b95c3d40   Changli Gao   cls_flow: add san...
85
86
87
  		if (pskb_network_may_pull(skb, sizeof(struct iphdr)))
  			return ntohl(ip_hdr(skb)->daddr);
  		break;
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
88
  	case htons(ETH_P_IPV6):
4b95c3d40   Changli Gao   cls_flow: add san...
89
90
91
  		if (pskb_network_may_pull(skb, sizeof(struct ipv6hdr)))
  			return ntohl(ipv6_hdr(skb)->daddr.s6_addr32[3]);
  		break;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
92
  	}
4b95c3d40   Changli Gao   cls_flow: add san...
93
94
  
  	return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
95
  }
4b95c3d40   Changli Gao   cls_flow: add san...
96
  static u32 flow_get_proto(struct sk_buff *skb)
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
97
98
  {
  	switch (skb->protocol) {
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
99
  	case htons(ETH_P_IP):
4b95c3d40   Changli Gao   cls_flow: add san...
100
101
  		return pskb_network_may_pull(skb, sizeof(struct iphdr)) ?
  		       ip_hdr(skb)->protocol : 0;
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
102
  	case htons(ETH_P_IPV6):
4b95c3d40   Changli Gao   cls_flow: add san...
103
104
  		return pskb_network_may_pull(skb, sizeof(struct ipv6hdr)) ?
  		       ipv6_hdr(skb)->nexthdr : 0;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  	default:
  		return 0;
  	}
  }
  
  static int has_ports(u8 protocol)
  {
  	switch (protocol) {
  	case IPPROTO_TCP:
  	case IPPROTO_UDP:
  	case IPPROTO_UDPLITE:
  	case IPPROTO_SCTP:
  	case IPPROTO_DCCP:
  	case IPPROTO_ESP:
  		return 1;
  	default:
  		return 0;
  	}
  }
4b95c3d40   Changli Gao   cls_flow: add san...
124
  static u32 flow_get_proto_src(struct sk_buff *skb)
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
125
  {
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
126
  	switch (skb->protocol) {
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
127
  	case htons(ETH_P_IP): {
4b95c3d40   Changli Gao   cls_flow: add san...
128
  		struct iphdr *iph;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
129

4b95c3d40   Changli Gao   cls_flow: add san...
130
131
132
  		if (!pskb_network_may_pull(skb, sizeof(*iph)))
  			break;
  		iph = ip_hdr(skb);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
133
  		if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
4b95c3d40   Changli Gao   cls_flow: add san...
134
135
136
  		    has_ports(iph->protocol) &&
  		    pskb_network_may_pull(skb, iph->ihl * 4 + 2))
  			return ntohs(*(__be16 *)((void *)iph + iph->ihl * 4));
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
137
138
  		break;
  	}
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
139
  	case htons(ETH_P_IPV6): {
4b95c3d40   Changli Gao   cls_flow: add san...
140
  		struct ipv6hdr *iph;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
141

4b95c3d40   Changli Gao   cls_flow: add san...
142
143
144
  		if (!pskb_network_may_pull(skb, sizeof(*iph) + 2))
  			break;
  		iph = ipv6_hdr(skb);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
145
  		if (has_ports(iph->nexthdr))
4b95c3d40   Changli Gao   cls_flow: add san...
146
  			return ntohs(*(__be16 *)&iph[1]);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
147
148
  		break;
  	}
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
149
  	}
4b95c3d40   Changli Gao   cls_flow: add san...
150
  	return addr_fold(skb->sk);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
151
  }
4b95c3d40   Changli Gao   cls_flow: add san...
152
  static u32 flow_get_proto_dst(struct sk_buff *skb)
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
153
  {
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
154
  	switch (skb->protocol) {
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
155
  	case htons(ETH_P_IP): {
4b95c3d40   Changli Gao   cls_flow: add san...
156
  		struct iphdr *iph;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
157

4b95c3d40   Changli Gao   cls_flow: add san...
158
159
160
  		if (!pskb_network_may_pull(skb, sizeof(*iph)))
  			break;
  		iph = ip_hdr(skb);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
161
  		if (!(iph->frag_off&htons(IP_MF|IP_OFFSET)) &&
4b95c3d40   Changli Gao   cls_flow: add san...
162
163
164
  		    has_ports(iph->protocol) &&
  		    pskb_network_may_pull(skb, iph->ihl * 4 + 4))
  			return ntohs(*(__be16 *)((void *)iph + iph->ihl * 4 + 2));
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
165
166
  		break;
  	}
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
167
  	case htons(ETH_P_IPV6): {
4b95c3d40   Changli Gao   cls_flow: add san...
168
  		struct ipv6hdr *iph;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
169

4b95c3d40   Changli Gao   cls_flow: add san...
170
171
172
  		if (!pskb_network_may_pull(skb, sizeof(*iph) + 4))
  			break;
  		iph = ipv6_hdr(skb);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
173
  		if (has_ports(iph->nexthdr))
4b95c3d40   Changli Gao   cls_flow: add san...
174
  			return ntohs(*(__be16 *)((void *)&iph[1] + 2));
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
175
176
  		break;
  	}
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
177
  	}
4b95c3d40   Changli Gao   cls_flow: add san...
178
  	return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
179
180
181
182
  }
  
  static u32 flow_get_iif(const struct sk_buff *skb)
  {
8964be4a9   Eric Dumazet   net: rename skb->...
183
  	return skb->skb_iif;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  }
  
  static u32 flow_get_priority(const struct sk_buff *skb)
  {
  	return skb->priority;
  }
  
  static u32 flow_get_mark(const struct sk_buff *skb)
  {
  	return skb->mark;
  }
  
  static u32 flow_get_nfct(const struct sk_buff *skb)
  {
  #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  	return addr_fold(skb->nfct);
  #else
  	return 0;
  #endif
  }
  
  #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  #define CTTUPLE(skb, member)						\
  ({									\
  	enum ip_conntrack_info ctinfo;					\
  	struct nf_conn *ct = nf_ct_get(skb, &ctinfo);			\
  	if (ct == NULL)							\
  		goto fallback;						\
  	ct->tuplehash[CTINFO2DIR(ctinfo)].tuple.member;			\
  })
  #else
  #define CTTUPLE(skb, member)						\
  ({									\
  	goto fallback;							\
  	0;								\
  })
  #endif
4b95c3d40   Changli Gao   cls_flow: add san...
221
  static u32 flow_get_nfct_src(struct sk_buff *skb)
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
222
223
  {
  	switch (skb->protocol) {
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
224
  	case htons(ETH_P_IP):
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
225
  		return ntohl(CTTUPLE(skb, src.u3.ip));
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
226
  	case htons(ETH_P_IPV6):
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
227
228
229
230
231
  		return ntohl(CTTUPLE(skb, src.u3.ip6[3]));
  	}
  fallback:
  	return flow_get_src(skb);
  }
4b95c3d40   Changli Gao   cls_flow: add san...
232
  static u32 flow_get_nfct_dst(struct sk_buff *skb)
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
233
234
  {
  	switch (skb->protocol) {
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
235
  	case htons(ETH_P_IP):
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
236
  		return ntohl(CTTUPLE(skb, dst.u3.ip));
606780404   Arnaldo Carvalho de Melo   net: Use hton[sl]...
237
  	case htons(ETH_P_IPV6):
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
238
239
240
241
242
  		return ntohl(CTTUPLE(skb, dst.u3.ip6[3]));
  	}
  fallback:
  	return flow_get_dst(skb);
  }
4b95c3d40   Changli Gao   cls_flow: add san...
243
  static u32 flow_get_nfct_proto_src(struct sk_buff *skb)
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
244
245
246
247
248
  {
  	return ntohs(CTTUPLE(skb, src.u.all));
  fallback:
  	return flow_get_proto_src(skb);
  }
4b95c3d40   Changli Gao   cls_flow: add san...
249
  static u32 flow_get_nfct_proto_dst(struct sk_buff *skb)
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
250
251
252
253
254
255
256
257
258
  {
  	return ntohs(CTTUPLE(skb, dst.u.all));
  fallback:
  	return flow_get_proto_dst(skb);
  }
  
  static u32 flow_get_rtclassid(const struct sk_buff *skb)
  {
  #ifdef CONFIG_NET_CLS_ROUTE
adf30907d   Eric Dumazet   net: skb->dst acc...
259
260
  	if (skb_dst(skb))
  		return skb_dst(skb)->tclassid;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
261
262
263
264
265
266
267
  #endif
  	return 0;
  }
  
  static u32 flow_get_skuid(const struct sk_buff *skb)
  {
  	if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file)
d76b0d9b2   David Howells   CRED: Use creds i...
268
  		return skb->sk->sk_socket->file->f_cred->fsuid;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
269
270
271
272
273
274
  	return 0;
  }
  
  static u32 flow_get_skgid(const struct sk_buff *skb)
  {
  	if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file)
d76b0d9b2   David Howells   CRED: Use creds i...
275
  		return skb->sk->sk_socket->file->f_cred->fsgid;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
276
277
  	return 0;
  }
9ec138101   Patrick McHardy   [NET_SCHED]: cls_...
278
279
280
281
282
283
284
285
  static u32 flow_get_vlan_tag(const struct sk_buff *skb)
  {
  	u16 uninitialized_var(tag);
  
  	if (vlan_get_tag(skb, &tag) < 0)
  		return 0;
  	return tag & VLAN_VID_MASK;
  }
4b95c3d40   Changli Gao   cls_flow: add san...
286
  static u32 flow_key_get(struct sk_buff *skb, int key)
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
  {
  	switch (key) {
  	case FLOW_KEY_SRC:
  		return flow_get_src(skb);
  	case FLOW_KEY_DST:
  		return flow_get_dst(skb);
  	case FLOW_KEY_PROTO:
  		return flow_get_proto(skb);
  	case FLOW_KEY_PROTO_SRC:
  		return flow_get_proto_src(skb);
  	case FLOW_KEY_PROTO_DST:
  		return flow_get_proto_dst(skb);
  	case FLOW_KEY_IIF:
  		return flow_get_iif(skb);
  	case FLOW_KEY_PRIORITY:
  		return flow_get_priority(skb);
  	case FLOW_KEY_MARK:
  		return flow_get_mark(skb);
  	case FLOW_KEY_NFCT:
  		return flow_get_nfct(skb);
  	case FLOW_KEY_NFCT_SRC:
  		return flow_get_nfct_src(skb);
  	case FLOW_KEY_NFCT_DST:
  		return flow_get_nfct_dst(skb);
  	case FLOW_KEY_NFCT_PROTO_SRC:
  		return flow_get_nfct_proto_src(skb);
  	case FLOW_KEY_NFCT_PROTO_DST:
  		return flow_get_nfct_proto_dst(skb);
  	case FLOW_KEY_RTCLASSID:
  		return flow_get_rtclassid(skb);
  	case FLOW_KEY_SKUID:
  		return flow_get_skuid(skb);
  	case FLOW_KEY_SKGID:
  		return flow_get_skgid(skb);
9ec138101   Patrick McHardy   [NET_SCHED]: cls_...
321
322
  	case FLOW_KEY_VLAN_TAG:
  		return flow_get_vlan_tag(skb);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  	default:
  		WARN_ON(1);
  		return 0;
  	}
  }
  
  static int flow_classify(struct sk_buff *skb, struct tcf_proto *tp,
  			 struct tcf_result *res)
  {
  	struct flow_head *head = tp->root;
  	struct flow_filter *f;
  	u32 keymask;
  	u32 classid;
  	unsigned int n, key;
  	int r;
  
  	list_for_each_entry(f, &head->filters, list) {
  		u32 keys[f->nkeys];
  
  		if (!tcf_em_tree_match(skb, &f->ematches, NULL))
  			continue;
  
  		keymask = f->keymask;
  
  		for (n = 0; n < f->nkeys; n++) {
  			key = ffs(keymask) - 1;
  			keymask &= ~(1 << key);
  			keys[n] = flow_key_get(skb, key);
  		}
  
  		if (f->mode == FLOW_MODE_HASH)
72d9794f4   Patrick McHardy   net-sched: cls_fl...
354
  			classid = jhash2(keys, f->nkeys, f->hashrnd);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
  		else {
  			classid = keys[0];
  			classid = (classid & f->mask) ^ f->xor;
  			classid = (classid >> f->rshift) + f->addend;
  		}
  
  		if (f->divisor)
  			classid %= f->divisor;
  
  		res->class   = 0;
  		res->classid = TC_H_MAKE(f->baseclass, f->baseclass + classid);
  
  		r = tcf_exts_exec(skb, &f->exts, res);
  		if (r < 0)
  			continue;
  		return r;
  	}
  	return -1;
  }
72d9794f4   Patrick McHardy   net-sched: cls_fl...
374
375
376
377
378
379
380
381
  static void flow_perturbation(unsigned long arg)
  {
  	struct flow_filter *f = (struct flow_filter *)arg;
  
  	get_random_bytes(&f->hashrnd, 4);
  	if (f->perturb_period)
  		mod_timer(&f->perturb_timer, jiffies + f->perturb_period);
  }
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
382
383
384
385
386
387
388
389
390
391
392
393
  static const struct nla_policy flow_policy[TCA_FLOW_MAX + 1] = {
  	[TCA_FLOW_KEYS]		= { .type = NLA_U32 },
  	[TCA_FLOW_MODE]		= { .type = NLA_U32 },
  	[TCA_FLOW_BASECLASS]	= { .type = NLA_U32 },
  	[TCA_FLOW_RSHIFT]	= { .type = NLA_U32 },
  	[TCA_FLOW_ADDEND]	= { .type = NLA_U32 },
  	[TCA_FLOW_MASK]		= { .type = NLA_U32 },
  	[TCA_FLOW_XOR]		= { .type = NLA_U32 },
  	[TCA_FLOW_DIVISOR]	= { .type = NLA_U32 },
  	[TCA_FLOW_ACT]		= { .type = NLA_NESTED },
  	[TCA_FLOW_POLICE]	= { .type = NLA_NESTED },
  	[TCA_FLOW_EMATCHES]	= { .type = NLA_NESTED },
72d9794f4   Patrick McHardy   net-sched: cls_fl...
394
  	[TCA_FLOW_PERTURB]	= { .type = NLA_U32 },
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
395
396
397
398
399
400
401
402
403
404
405
406
407
  };
  
  static int flow_change(struct tcf_proto *tp, unsigned long base,
  		       u32 handle, struct nlattr **tca,
  		       unsigned long *arg)
  {
  	struct flow_head *head = tp->root;
  	struct flow_filter *f;
  	struct nlattr *opt = tca[TCA_OPTIONS];
  	struct nlattr *tb[TCA_FLOW_MAX + 1];
  	struct tcf_exts e;
  	struct tcf_ematch_tree t;
  	unsigned int nkeys = 0;
72d9794f4   Patrick McHardy   net-sched: cls_fl...
408
  	unsigned int perturb_period = 0;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
  	u32 baseclass = 0;
  	u32 keymask = 0;
  	u32 mode;
  	int err;
  
  	if (opt == NULL)
  		return -EINVAL;
  
  	err = nla_parse_nested(tb, TCA_FLOW_MAX, opt, flow_policy);
  	if (err < 0)
  		return err;
  
  	if (tb[TCA_FLOW_BASECLASS]) {
  		baseclass = nla_get_u32(tb[TCA_FLOW_BASECLASS]);
  		if (TC_H_MIN(baseclass) == 0)
  			return -EINVAL;
  	}
  
  	if (tb[TCA_FLOW_KEYS]) {
  		keymask = nla_get_u32(tb[TCA_FLOW_KEYS]);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
429
430
431
432
  
  		nkeys = hweight32(keymask);
  		if (nkeys == 0)
  			return -EINVAL;
4f2504910   Patrick McHardy   [NET_SCHED]: cls_...
433
434
435
  
  		if (fls(keymask) - 1 > FLOW_KEY_MAX)
  			return -EOPNOTSUPP;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
  	}
  
  	err = tcf_exts_validate(tp, tb, tca[TCA_RATE], &e, &flow_ext_map);
  	if (err < 0)
  		return err;
  
  	err = tcf_em_tree_validate(tp, tb[TCA_FLOW_EMATCHES], &t);
  	if (err < 0)
  		goto err1;
  
  	f = (struct flow_filter *)*arg;
  	if (f != NULL) {
  		err = -EINVAL;
  		if (f->handle != handle && handle)
  			goto err2;
  
  		mode = f->mode;
  		if (tb[TCA_FLOW_MODE])
  			mode = nla_get_u32(tb[TCA_FLOW_MODE]);
  		if (mode != FLOW_MODE_HASH && nkeys > 1)
  			goto err2;
72d9794f4   Patrick McHardy   net-sched: cls_fl...
457
458
459
460
461
462
463
464
  
  		if (mode == FLOW_MODE_HASH)
  			perturb_period = f->perturb_period;
  		if (tb[TCA_FLOW_PERTURB]) {
  			if (mode != FLOW_MODE_HASH)
  				goto err2;
  			perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
  		}
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
465
466
467
468
469
470
471
472
473
474
475
476
  	} else {
  		err = -EINVAL;
  		if (!handle)
  			goto err2;
  		if (!tb[TCA_FLOW_KEYS])
  			goto err2;
  
  		mode = FLOW_MODE_MAP;
  		if (tb[TCA_FLOW_MODE])
  			mode = nla_get_u32(tb[TCA_FLOW_MODE]);
  		if (mode != FLOW_MODE_HASH && nkeys > 1)
  			goto err2;
72d9794f4   Patrick McHardy   net-sched: cls_fl...
477
478
479
480
481
  		if (tb[TCA_FLOW_PERTURB]) {
  			if (mode != FLOW_MODE_HASH)
  				goto err2;
  			perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
  		}
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
482
483
484
485
486
487
488
489
490
491
492
493
  		if (TC_H_MAJ(baseclass) == 0)
  			baseclass = TC_H_MAKE(tp->q->handle, baseclass);
  		if (TC_H_MIN(baseclass) == 0)
  			baseclass = TC_H_MAKE(baseclass, 1);
  
  		err = -ENOBUFS;
  		f = kzalloc(sizeof(*f), GFP_KERNEL);
  		if (f == NULL)
  			goto err2;
  
  		f->handle = handle;
  		f->mask	  = ~0U;
72d9794f4   Patrick McHardy   net-sched: cls_fl...
494
495
496
497
498
  
  		get_random_bytes(&f->hashrnd, 4);
  		f->perturb_timer.function = flow_perturbation;
  		f->perturb_timer.data = (unsigned long)f;
  		init_timer_deferrable(&f->perturb_timer);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
  	}
  
  	tcf_exts_change(tp, &f->exts, &e);
  	tcf_em_tree_change(tp, &f->ematches, &t);
  
  	tcf_tree_lock(tp);
  
  	if (tb[TCA_FLOW_KEYS]) {
  		f->keymask = keymask;
  		f->nkeys   = nkeys;
  	}
  
  	f->mode = mode;
  
  	if (tb[TCA_FLOW_MASK])
  		f->mask = nla_get_u32(tb[TCA_FLOW_MASK]);
  	if (tb[TCA_FLOW_XOR])
  		f->xor = nla_get_u32(tb[TCA_FLOW_XOR]);
  	if (tb[TCA_FLOW_RSHIFT])
  		f->rshift = nla_get_u32(tb[TCA_FLOW_RSHIFT]);
  	if (tb[TCA_FLOW_ADDEND])
  		f->addend = nla_get_u32(tb[TCA_FLOW_ADDEND]);
  
  	if (tb[TCA_FLOW_DIVISOR])
  		f->divisor = nla_get_u32(tb[TCA_FLOW_DIVISOR]);
  	if (baseclass)
  		f->baseclass = baseclass;
72d9794f4   Patrick McHardy   net-sched: cls_fl...
526
527
528
529
  	f->perturb_period = perturb_period;
  	del_timer(&f->perturb_timer);
  	if (perturb_period)
  		mod_timer(&f->perturb_timer, jiffies + perturb_period);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
  	if (*arg == 0)
  		list_add_tail(&f->list, &head->filters);
  
  	tcf_tree_unlock(tp);
  
  	*arg = (unsigned long)f;
  	return 0;
  
  err2:
  	tcf_em_tree_destroy(tp, &t);
  err1:
  	tcf_exts_destroy(tp, &e);
  	return err;
  }
  
  static void flow_destroy_filter(struct tcf_proto *tp, struct flow_filter *f)
  {
72d9794f4   Patrick McHardy   net-sched: cls_fl...
547
  	del_timer_sync(&f->perturb_timer);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
  	tcf_exts_destroy(tp, &f->exts);
  	tcf_em_tree_destroy(tp, &f->ematches);
  	kfree(f);
  }
  
  static int flow_delete(struct tcf_proto *tp, unsigned long arg)
  {
  	struct flow_filter *f = (struct flow_filter *)arg;
  
  	tcf_tree_lock(tp);
  	list_del(&f->list);
  	tcf_tree_unlock(tp);
  	flow_destroy_filter(tp, f);
  	return 0;
  }
  
  static int flow_init(struct tcf_proto *tp)
  {
  	struct flow_head *head;
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
  	head = kzalloc(sizeof(*head), GFP_KERNEL);
  	if (head == NULL)
  		return -ENOBUFS;
  	INIT_LIST_HEAD(&head->filters);
  	tp->root = head;
  	return 0;
  }
  
  static void flow_destroy(struct tcf_proto *tp)
  {
  	struct flow_head *head = tp->root;
  	struct flow_filter *f, *next;
  
  	list_for_each_entry_safe(f, next, &head->filters, list) {
  		list_del(&f->list);
  		flow_destroy_filter(tp, f);
  	}
  	kfree(head);
  }
  
  static unsigned long flow_get(struct tcf_proto *tp, u32 handle)
  {
  	struct flow_head *head = tp->root;
  	struct flow_filter *f;
  
  	list_for_each_entry(f, &head->filters, list)
  		if (f->handle == handle)
  			return (unsigned long)f;
  	return 0;
  }
  
  static void flow_put(struct tcf_proto *tp, unsigned long f)
  {
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
  }
  
  static int flow_dump(struct tcf_proto *tp, unsigned long fh,
  		     struct sk_buff *skb, struct tcmsg *t)
  {
  	struct flow_filter *f = (struct flow_filter *)fh;
  	struct nlattr *nest;
  
  	if (f == NULL)
  		return skb->len;
  
  	t->tcm_handle = f->handle;
  
  	nest = nla_nest_start(skb, TCA_OPTIONS);
  	if (nest == NULL)
  		goto nla_put_failure;
  
  	NLA_PUT_U32(skb, TCA_FLOW_KEYS, f->keymask);
  	NLA_PUT_U32(skb, TCA_FLOW_MODE, f->mode);
  
  	if (f->mask != ~0 || f->xor != 0) {
  		NLA_PUT_U32(skb, TCA_FLOW_MASK, f->mask);
  		NLA_PUT_U32(skb, TCA_FLOW_XOR, f->xor);
  	}
  	if (f->rshift)
  		NLA_PUT_U32(skb, TCA_FLOW_RSHIFT, f->rshift);
  	if (f->addend)
  		NLA_PUT_U32(skb, TCA_FLOW_ADDEND, f->addend);
  
  	if (f->divisor)
  		NLA_PUT_U32(skb, TCA_FLOW_DIVISOR, f->divisor);
  	if (f->baseclass)
  		NLA_PUT_U32(skb, TCA_FLOW_BASECLASS, f->baseclass);
72d9794f4   Patrick McHardy   net-sched: cls_fl...
633
634
  	if (f->perturb_period)
  		NLA_PUT_U32(skb, TCA_FLOW_PERTURB, f->perturb_period / HZ);
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
635
636
  	if (tcf_exts_dump(skb, &f->exts, &flow_ext_map) < 0)
  		goto nla_put_failure;
0aead5434   Rami Rosen   [NET_SCHED]: Add ...
637
  #ifdef CONFIG_NET_EMATCH
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
638
639
640
  	if (f->ematches.hdr.nmatches &&
  	    tcf_em_tree_dump(skb, &f->ematches, TCA_FLOW_EMATCHES) < 0)
  		goto nla_put_failure;
0aead5434   Rami Rosen   [NET_SCHED]: Add ...
641
  #endif
e5dfb8151   Patrick McHardy   [NET_SCHED]: Add ...
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
  	nla_nest_end(skb, nest);
  
  	if (tcf_exts_dump_stats(skb, &f->exts, &flow_ext_map) < 0)
  		goto nla_put_failure;
  
  	return skb->len;
  
  nla_put_failure:
  	nlmsg_trim(skb, nest);
  	return -1;
  }
  
  static void flow_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  {
  	struct flow_head *head = tp->root;
  	struct flow_filter *f;
  
  	list_for_each_entry(f, &head->filters, list) {
  		if (arg->count < arg->skip)
  			goto skip;
  		if (arg->fn(tp, (unsigned long)f, arg) < 0) {
  			arg->stop = 1;
  			break;
  		}
  skip:
  		arg->count++;
  	}
  }
  
  static struct tcf_proto_ops cls_flow_ops __read_mostly = {
  	.kind		= "flow",
  	.classify	= flow_classify,
  	.init		= flow_init,
  	.destroy	= flow_destroy,
  	.change		= flow_change,
  	.delete		= flow_delete,
  	.get		= flow_get,
  	.put		= flow_put,
  	.dump		= flow_dump,
  	.walk		= flow_walk,
  	.owner		= THIS_MODULE,
  };
  
  static int __init cls_flow_init(void)
  {
  	return register_tcf_proto_ops(&cls_flow_ops);
  }
  
  static void __exit cls_flow_exit(void)
  {
  	unregister_tcf_proto_ops(&cls_flow_ops);
  }
  
  module_init(cls_flow_init);
  module_exit(cls_flow_exit);
  
  MODULE_LICENSE("GPL");
  MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  MODULE_DESCRIPTION("TC flow classifier");