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