blob: 9e087d885675cc7b2a4286635d93299ed9a89c46 [file] [log] [blame]
Patrick McHardye5dfb812008-01-31 18:37:42 -08001/*
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>
Patrick McHardy9ec13812008-02-05 16:21:04 -080022#include <linux/if_vlan.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Patrick McHardye5dfb812008-01-31 18:37:42 -080024
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
32struct flow_head {
33 struct list_head filters;
34};
35
36struct flow_filter {
37 struct list_head list;
38 struct tcf_exts exts;
39 struct tcf_ematch_tree ematches;
Patrick McHardy72d97942008-07-14 20:36:32 -070040 struct timer_list perturb_timer;
41 u32 perturb_period;
Patrick McHardye5dfb812008-01-31 18:37:42 -080042 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;
Patrick McHardy72d97942008-07-14 20:36:32 -070053 u32 hashrnd;
Patrick McHardye5dfb812008-01-31 18:37:42 -080054};
55
Patrick McHardye5dfb812008-01-31 18:37:42 -080056static const struct tcf_ext_map flow_ext_map = {
57 .action = TCA_FLOW_ACT,
58 .police = TCA_FLOW_POLICE,
59};
60
61static 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
Eric Dumazet859c2012011-10-23 17:59:41 +000068static u32 flow_get_src(const struct sk_buff *skb, int nhoff)
Patrick McHardye5dfb812008-01-31 18:37:42 -080069{
Eric Dumazet859c2012011-10-23 17:59:41 +000070 __be32 *data = NULL, hdata;
71
Patrick McHardye5dfb812008-01-31 18:37:42 -080072 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -070073 case htons(ETH_P_IP):
Eric Dumazet859c2012011-10-23 17:59:41 +000074 data = skb_header_pointer(skb,
75 nhoff + offsetof(struct iphdr,
76 saddr),
77 4, &hdata);
Changli Gao4b95c3d2010-08-04 04:48:12 +000078 break;
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -070079 case htons(ETH_P_IPV6):
Eric Dumazet859c2012011-10-23 17:59:41 +000080 data = skb_header_pointer(skb,
81 nhoff + offsetof(struct ipv6hdr,
82 saddr.s6_addr32[3]),
83 4, &hdata);
Changli Gao4b95c3d2010-08-04 04:48:12 +000084 break;
Patrick McHardye5dfb812008-01-31 18:37:42 -080085 }
Changli Gao4b95c3d2010-08-04 04:48:12 +000086
Eric Dumazet859c2012011-10-23 17:59:41 +000087 if (data)
88 return ntohl(*data);
Changli Gao4b95c3d2010-08-04 04:48:12 +000089 return addr_fold(skb->sk);
Patrick McHardye5dfb812008-01-31 18:37:42 -080090}
91
Eric Dumazet859c2012011-10-23 17:59:41 +000092static u32 flow_get_dst(const struct sk_buff *skb, int nhoff)
Patrick McHardye5dfb812008-01-31 18:37:42 -080093{
Eric Dumazet859c2012011-10-23 17:59:41 +000094 __be32 *data = NULL, hdata;
95
Patrick McHardye5dfb812008-01-31 18:37:42 -080096 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -070097 case htons(ETH_P_IP):
Eric Dumazet859c2012011-10-23 17:59:41 +000098 data = skb_header_pointer(skb,
99 nhoff + offsetof(struct iphdr,
100 daddr),
101 4, &hdata);
Changli Gao4b95c3d2010-08-04 04:48:12 +0000102 break;
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700103 case htons(ETH_P_IPV6):
Eric Dumazet859c2012011-10-23 17:59:41 +0000104 data = skb_header_pointer(skb,
105 nhoff + offsetof(struct ipv6hdr,
106 daddr.s6_addr32[3]),
107 4, &hdata);
Changli Gao4b95c3d2010-08-04 04:48:12 +0000108 break;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800109 }
Changli Gao4b95c3d2010-08-04 04:48:12 +0000110
Eric Dumazet859c2012011-10-23 17:59:41 +0000111 if (data)
112 return ntohl(*data);
Changli Gao4b95c3d2010-08-04 04:48:12 +0000113 return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800114}
115
Eric Dumazet859c2012011-10-23 17:59:41 +0000116static u32 flow_get_proto(const struct sk_buff *skb, int nhoff)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800117{
Eric Dumazet859c2012011-10-23 17:59:41 +0000118 __u8 *data = NULL, hdata;
119
Patrick McHardye5dfb812008-01-31 18:37:42 -0800120 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700121 case htons(ETH_P_IP):
Eric Dumazet859c2012011-10-23 17:59:41 +0000122 data = skb_header_pointer(skb,
123 nhoff + offsetof(struct iphdr,
124 protocol),
125 1, &hdata);
126 break;
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700127 case htons(ETH_P_IPV6):
Eric Dumazet859c2012011-10-23 17:59:41 +0000128 data = skb_header_pointer(skb,
129 nhoff + offsetof(struct ipv6hdr,
130 nexthdr),
131 1, &hdata);
132 break;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800133 }
Eric Dumazet859c2012011-10-23 17:59:41 +0000134 if (data)
135 return *data;
136 return 0;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800137}
138
Eric Dumazet859c2012011-10-23 17:59:41 +0000139/* helper function to get either src or dst port */
140static __be16 *flow_get_proto_common(const struct sk_buff *skb, int nhoff,
141 __be16 *_port, int dst)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800142{
Eric Dumazet859c2012011-10-23 17:59:41 +0000143 __be16 *port = NULL;
144 int poff;
145
Patrick McHardye5dfb812008-01-31 18:37:42 -0800146 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700147 case htons(ETH_P_IP): {
Eric Dumazet859c2012011-10-23 17:59:41 +0000148 struct iphdr *iph, _iph;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800149
Eric Dumazet859c2012011-10-23 17:59:41 +0000150 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
151 if (!iph)
Changli Gao4b95c3d2010-08-04 04:48:12 +0000152 break;
Paul Gortmaker56f8a752011-06-21 20:33:34 -0700153 if (ip_is_fragment(iph))
Changli Gao78d33072010-08-17 19:05:08 +0000154 break;
155 poff = proto_ports_offset(iph->protocol);
Eric Dumazet859c2012011-10-23 17:59:41 +0000156 if (poff >= 0)
157 port = skb_header_pointer(skb,
158 nhoff + iph->ihl * 4 + poff + dst,
159 sizeof(*_port), _port);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800160 break;
161 }
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700162 case htons(ETH_P_IPV6): {
Eric Dumazet859c2012011-10-23 17:59:41 +0000163 struct ipv6hdr *iph, _iph;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800164
Eric Dumazet859c2012011-10-23 17:59:41 +0000165 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
166 if (!iph)
Changli Gao4b95c3d2010-08-04 04:48:12 +0000167 break;
Changli Gao78d33072010-08-17 19:05:08 +0000168 poff = proto_ports_offset(iph->nexthdr);
Eric Dumazet859c2012011-10-23 17:59:41 +0000169 if (poff >= 0)
170 port = skb_header_pointer(skb,
171 nhoff + sizeof(*iph) + poff + dst,
172 sizeof(*_port), _port);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800173 break;
174 }
Patrick McHardye5dfb812008-01-31 18:37:42 -0800175 }
176
Eric Dumazet859c2012011-10-23 17:59:41 +0000177 return port;
178}
179
180static u32 flow_get_proto_src(const struct sk_buff *skb, int nhoff)
181{
182 __be16 _port, *port = flow_get_proto_common(skb, nhoff, &_port, 0);
183
184 if (port)
185 return ntohs(*port);
186
Changli Gao4b95c3d2010-08-04 04:48:12 +0000187 return addr_fold(skb->sk);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800188}
189
Eric Dumazet859c2012011-10-23 17:59:41 +0000190static u32 flow_get_proto_dst(const struct sk_buff *skb, int nhoff)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800191{
Eric Dumazet859c2012011-10-23 17:59:41 +0000192 __be16 _port, *port = flow_get_proto_common(skb, nhoff, &_port, 2);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800193
Eric Dumazet859c2012011-10-23 17:59:41 +0000194 if (port)
195 return ntohs(*port);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800196
Changli Gao4b95c3d2010-08-04 04:48:12 +0000197 return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800198}
199
200static u32 flow_get_iif(const struct sk_buff *skb)
201{
Eric Dumazet8964be42009-11-20 15:35:04 -0800202 return skb->skb_iif;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800203}
204
205static u32 flow_get_priority(const struct sk_buff *skb)
206{
207 return skb->priority;
208}
209
210static u32 flow_get_mark(const struct sk_buff *skb)
211{
212 return skb->mark;
213}
214
215static 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; \
Eric Dumazet859c2012011-10-23 17:59:41 +0000228 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo); \
Patrick McHardye5dfb812008-01-31 18:37:42 -0800229 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
Eric Dumazet859c2012011-10-23 17:59:41 +0000241static u32 flow_get_nfct_src(const struct sk_buff *skb, int nhoff)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800242{
243 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700244 case htons(ETH_P_IP):
Patrick McHardye5dfb812008-01-31 18:37:42 -0800245 return ntohl(CTTUPLE(skb, src.u3.ip));
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700246 case htons(ETH_P_IPV6):
Patrick McHardye5dfb812008-01-31 18:37:42 -0800247 return ntohl(CTTUPLE(skb, src.u3.ip6[3]));
248 }
249fallback:
Eric Dumazet859c2012011-10-23 17:59:41 +0000250 return flow_get_src(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800251}
252
Eric Dumazet859c2012011-10-23 17:59:41 +0000253static u32 flow_get_nfct_dst(const struct sk_buff *skb, int nhoff)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800254{
255 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700256 case htons(ETH_P_IP):
Patrick McHardye5dfb812008-01-31 18:37:42 -0800257 return ntohl(CTTUPLE(skb, dst.u3.ip));
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700258 case htons(ETH_P_IPV6):
Patrick McHardye5dfb812008-01-31 18:37:42 -0800259 return ntohl(CTTUPLE(skb, dst.u3.ip6[3]));
260 }
261fallback:
Eric Dumazet859c2012011-10-23 17:59:41 +0000262 return flow_get_dst(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800263}
264
Eric Dumazet859c2012011-10-23 17:59:41 +0000265static u32 flow_get_nfct_proto_src(const struct sk_buff *skb, int nhoff)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800266{
267 return ntohs(CTTUPLE(skb, src.u.all));
268fallback:
Eric Dumazet859c2012011-10-23 17:59:41 +0000269 return flow_get_proto_src(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800270}
271
Eric Dumazet859c2012011-10-23 17:59:41 +0000272static u32 flow_get_nfct_proto_dst(const struct sk_buff *skb, int nhoff)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800273{
274 return ntohs(CTTUPLE(skb, dst.u.all));
275fallback:
Eric Dumazet859c2012011-10-23 17:59:41 +0000276 return flow_get_proto_dst(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800277}
278
279static u32 flow_get_rtclassid(const struct sk_buff *skb)
280{
Patrick McHardyc7066f72011-01-14 13:36:42 +0100281#ifdef CONFIG_IP_ROUTE_CLASSID
Eric Dumazetadf30902009-06-02 05:19:30 +0000282 if (skb_dst(skb))
283 return skb_dst(skb)->tclassid;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800284#endif
285 return 0;
286}
287
288static u32 flow_get_skuid(const struct sk_buff *skb)
289{
290 if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file)
David Howellsd76b0d92008-11-14 10:39:25 +1100291 return skb->sk->sk_socket->file->f_cred->fsuid;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800292 return 0;
293}
294
295static u32 flow_get_skgid(const struct sk_buff *skb)
296{
297 if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file)
David Howellsd76b0d92008-11-14 10:39:25 +1100298 return skb->sk->sk_socket->file->f_cred->fsgid;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800299 return 0;
300}
301
Patrick McHardy9ec13812008-02-05 16:21:04 -0800302static 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
Changli Gao739a91e2010-08-21 06:23:15 +0000311static u32 flow_get_rxhash(struct sk_buff *skb)
312{
313 return skb_get_rxhash(skb);
314}
315
Changli Gao4b95c3d2010-08-04 04:48:12 +0000316static u32 flow_key_get(struct sk_buff *skb, int key)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800317{
Eric Dumazet859c2012011-10-23 17:59:41 +0000318 int nhoff = skb_network_offset(skb);
319
Patrick McHardye5dfb812008-01-31 18:37:42 -0800320 switch (key) {
321 case FLOW_KEY_SRC:
Eric Dumazet859c2012011-10-23 17:59:41 +0000322 return flow_get_src(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800323 case FLOW_KEY_DST:
Eric Dumazet859c2012011-10-23 17:59:41 +0000324 return flow_get_dst(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800325 case FLOW_KEY_PROTO:
Eric Dumazet859c2012011-10-23 17:59:41 +0000326 return flow_get_proto(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800327 case FLOW_KEY_PROTO_SRC:
Eric Dumazet859c2012011-10-23 17:59:41 +0000328 return flow_get_proto_src(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800329 case FLOW_KEY_PROTO_DST:
Eric Dumazet859c2012011-10-23 17:59:41 +0000330 return flow_get_proto_dst(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800331 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:
Eric Dumazet859c2012011-10-23 17:59:41 +0000340 return flow_get_nfct_src(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800341 case FLOW_KEY_NFCT_DST:
Eric Dumazet859c2012011-10-23 17:59:41 +0000342 return flow_get_nfct_dst(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800343 case FLOW_KEY_NFCT_PROTO_SRC:
Eric Dumazet859c2012011-10-23 17:59:41 +0000344 return flow_get_nfct_proto_src(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800345 case FLOW_KEY_NFCT_PROTO_DST:
Eric Dumazet859c2012011-10-23 17:59:41 +0000346 return flow_get_nfct_proto_dst(skb, nhoff);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800347 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);
Patrick McHardy9ec13812008-02-05 16:21:04 -0800353 case FLOW_KEY_VLAN_TAG:
354 return flow_get_vlan_tag(skb);
Changli Gao739a91e2010-08-21 06:23:15 +0000355 case FLOW_KEY_RXHASH:
356 return flow_get_rxhash(skb);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800357 default:
358 WARN_ON(1);
359 return 0;
360 }
361}
362
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000363static int flow_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Patrick McHardye5dfb812008-01-31 18:37:42 -0800364 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)
Patrick McHardy72d97942008-07-14 20:36:32 -0700388 classid = jhash2(keys, f->nkeys, f->hashrnd);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800389 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
Patrick McHardy72d97942008-07-14 20:36:32 -0700409static 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
Patrick McHardye5dfb812008-01-31 18:37:42 -0800418static 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 },
Patrick McHardy72d97942008-07-14 20:36:32 -0700430 [TCA_FLOW_PERTURB] = { .type = NLA_U32 },
Patrick McHardye5dfb812008-01-31 18:37:42 -0800431};
432
433static 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;
Patrick McHardy72d97942008-07-14 20:36:32 -0700444 unsigned int perturb_period = 0;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800445 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]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800465
466 nkeys = hweight32(keymask);
467 if (nkeys == 0)
468 return -EINVAL;
Patrick McHardy4f250492008-02-05 16:19:59 -0800469
470 if (fls(keymask) - 1 > FLOW_KEY_MAX)
471 return -EOPNOTSUPP;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800472 }
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;
Patrick McHardy72d97942008-07-14 20:36:32 -0700493
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 }
Patrick McHardye5dfb812008-01-31 18:37:42 -0800501 } 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
Patrick McHardy72d97942008-07-14 20:36:32 -0700514 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
Patrick McHardye5dfb812008-01-31 18:37:42 -0800520 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;
Patrick McHardy72d97942008-07-14 20:36:32 -0700532
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);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800537 }
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
Patrick McHardy72d97942008-07-14 20:36:32 -0700565 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
Patrick McHardye5dfb812008-01-31 18:37:42 -0800570 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
578err2:
579 tcf_em_tree_destroy(tp, &t);
580err1:
581 tcf_exts_destroy(tp, &e);
582 return err;
583}
584
585static void flow_destroy_filter(struct tcf_proto *tp, struct flow_filter *f)
586{
Patrick McHardy72d97942008-07-14 20:36:32 -0700587 del_timer_sync(&f->perturb_timer);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800588 tcf_exts_destroy(tp, &f->exts);
589 tcf_em_tree_destroy(tp, &f->ematches);
590 kfree(f);
591}
592
593static 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
604static int flow_init(struct tcf_proto *tp)
605{
606 struct flow_head *head;
607
Patrick McHardye5dfb812008-01-31 18:37:42 -0800608 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
616static 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
628static 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
639static void flow_put(struct tcf_proto *tp, unsigned long f)
640{
Patrick McHardye5dfb812008-01-31 18:37:42 -0800641}
642
643static 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
Patrick McHardy72d97942008-07-14 20:36:32 -0700675 if (f->perturb_period)
676 NLA_PUT_U32(skb, TCA_FLOW_PERTURB, f->perturb_period / HZ);
677
Patrick McHardye5dfb812008-01-31 18:37:42 -0800678 if (tcf_exts_dump(skb, &f->exts, &flow_ext_map) < 0)
679 goto nla_put_failure;
Rami Rosen0aead542008-02-05 02:56:48 -0800680#ifdef CONFIG_NET_EMATCH
Patrick McHardye5dfb812008-01-31 18:37:42 -0800681 if (f->ematches.hdr.nmatches &&
682 tcf_em_tree_dump(skb, &f->ematches, TCA_FLOW_EMATCHES) < 0)
683 goto nla_put_failure;
Rami Rosen0aead542008-02-05 02:56:48 -0800684#endif
Patrick McHardye5dfb812008-01-31 18:37:42 -0800685 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
692nla_put_failure:
693 nlmsg_trim(skb, nest);
694 return -1;
695}
696
697static 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 }
709skip:
710 arg->count++;
711 }
712}
713
714static 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
728static int __init cls_flow_init(void)
729{
730 return register_tcf_proto_ops(&cls_flow_ops);
731}
732
733static void __exit cls_flow_exit(void)
734{
735 unregister_tcf_proto_ops(&cls_flow_ops);
736}
737
738module_init(cls_flow_init);
739module_exit(cls_flow_exit);
740
741MODULE_LICENSE("GPL");
742MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
743MODULE_DESCRIPTION("TC flow classifier");