blob: 4ac515f2a6cecbcb56159c9c11892771e3816ddd [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>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040024#include <linux/module.h>
Patrick McHardye5dfb812008-01-31 18:37:42 -080025
26#include <net/pkt_cls.h>
27#include <net/ip.h>
28#include <net/route.h>
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000029#include <net/flow_keys.h>
30
Patrick McHardye5dfb812008-01-31 18:37:42 -080031#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
32#include <net/netfilter/nf_conntrack.h>
33#endif
34
35struct flow_head {
36 struct list_head filters;
John Fastabend70da9f02014-09-12 20:06:55 -070037 struct rcu_head rcu;
Patrick McHardye5dfb812008-01-31 18:37:42 -080038};
39
40struct flow_filter {
41 struct list_head list;
42 struct tcf_exts exts;
43 struct tcf_ematch_tree ematches;
John Fastabend70da9f02014-09-12 20:06:55 -070044 struct tcf_proto *tp;
Patrick McHardy72d97942008-07-14 20:36:32 -070045 struct timer_list perturb_timer;
46 u32 perturb_period;
Patrick McHardye5dfb812008-01-31 18:37:42 -080047 u32 handle;
48
49 u32 nkeys;
50 u32 keymask;
51 u32 mode;
52 u32 mask;
53 u32 xor;
54 u32 rshift;
55 u32 addend;
56 u32 divisor;
57 u32 baseclass;
Patrick McHardy72d97942008-07-14 20:36:32 -070058 u32 hashrnd;
John Fastabend70da9f02014-09-12 20:06:55 -070059 struct rcu_head rcu;
Patrick McHardye5dfb812008-01-31 18:37:42 -080060};
61
Patrick McHardye5dfb812008-01-31 18:37:42 -080062static inline u32 addr_fold(void *addr)
63{
64 unsigned long a = (unsigned long)addr;
65
66 return (a & 0xFFFFFFFF) ^ (BITS_PER_LONG > 32 ? a >> 32 : 0);
67}
68
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000069static u32 flow_get_src(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -080070{
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000071 if (flow->src)
72 return ntohl(flow->src);
Changli Gao4b95c3d2010-08-04 04:48:12 +000073 return addr_fold(skb->sk);
Patrick McHardye5dfb812008-01-31 18:37:42 -080074}
75
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000076static u32 flow_get_dst(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -080077{
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000078 if (flow->dst)
79 return ntohl(flow->dst);
Changli Gao4b95c3d2010-08-04 04:48:12 +000080 return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
Patrick McHardye5dfb812008-01-31 18:37:42 -080081}
82
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000083static u32 flow_get_proto(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -080084{
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000085 return flow->ip_proto;
Patrick McHardye5dfb812008-01-31 18:37:42 -080086}
87
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000088static u32 flow_get_proto_src(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -080089{
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000090 if (flow->ports)
91 return ntohs(flow->port16[0]);
Eric Dumazet859c2012011-10-23 17:59:41 +000092
Changli Gao4b95c3d2010-08-04 04:48:12 +000093 return addr_fold(skb->sk);
Patrick McHardye5dfb812008-01-31 18:37:42 -080094}
95
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000096static u32 flow_get_proto_dst(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -080097{
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000098 if (flow->ports)
99 return ntohs(flow->port16[1]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800100
Changli Gao4b95c3d2010-08-04 04:48:12 +0000101 return addr_fold(skb_dst(skb)) ^ (__force u16)skb->protocol;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800102}
103
104static u32 flow_get_iif(const struct sk_buff *skb)
105{
Eric Dumazet8964be42009-11-20 15:35:04 -0800106 return skb->skb_iif;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800107}
108
109static u32 flow_get_priority(const struct sk_buff *skb)
110{
111 return skb->priority;
112}
113
114static u32 flow_get_mark(const struct sk_buff *skb)
115{
116 return skb->mark;
117}
118
119static u32 flow_get_nfct(const struct sk_buff *skb)
120{
121#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
122 return addr_fold(skb->nfct);
123#else
124 return 0;
125#endif
126}
127
128#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
129#define CTTUPLE(skb, member) \
130({ \
131 enum ip_conntrack_info ctinfo; \
Eric Dumazet859c2012011-10-23 17:59:41 +0000132 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo); \
Patrick McHardye5dfb812008-01-31 18:37:42 -0800133 if (ct == NULL) \
134 goto fallback; \
135 ct->tuplehash[CTINFO2DIR(ctinfo)].tuple.member; \
136})
137#else
138#define CTTUPLE(skb, member) \
139({ \
140 goto fallback; \
141 0; \
142})
143#endif
144
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000145static u32 flow_get_nfct_src(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800146{
147 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700148 case htons(ETH_P_IP):
Patrick McHardye5dfb812008-01-31 18:37:42 -0800149 return ntohl(CTTUPLE(skb, src.u3.ip));
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700150 case htons(ETH_P_IPV6):
Patrick McHardye5dfb812008-01-31 18:37:42 -0800151 return ntohl(CTTUPLE(skb, src.u3.ip6[3]));
152 }
153fallback:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000154 return flow_get_src(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800155}
156
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000157static u32 flow_get_nfct_dst(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800158{
159 switch (skb->protocol) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700160 case htons(ETH_P_IP):
Patrick McHardye5dfb812008-01-31 18:37:42 -0800161 return ntohl(CTTUPLE(skb, dst.u3.ip));
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700162 case htons(ETH_P_IPV6):
Patrick McHardye5dfb812008-01-31 18:37:42 -0800163 return ntohl(CTTUPLE(skb, dst.u3.ip6[3]));
164 }
165fallback:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000166 return flow_get_dst(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800167}
168
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000169static u32 flow_get_nfct_proto_src(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800170{
171 return ntohs(CTTUPLE(skb, src.u.all));
172fallback:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000173 return flow_get_proto_src(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800174}
175
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000176static u32 flow_get_nfct_proto_dst(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800177{
178 return ntohs(CTTUPLE(skb, dst.u.all));
179fallback:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000180 return flow_get_proto_dst(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800181}
182
183static u32 flow_get_rtclassid(const struct sk_buff *skb)
184{
Patrick McHardyc7066f72011-01-14 13:36:42 +0100185#ifdef CONFIG_IP_ROUTE_CLASSID
Eric Dumazetadf30902009-06-02 05:19:30 +0000186 if (skb_dst(skb))
187 return skb_dst(skb)->tclassid;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800188#endif
189 return 0;
190}
191
192static u32 flow_get_skuid(const struct sk_buff *skb)
193{
Eric W. Biedermana6c67962012-05-25 13:49:36 -0600194 if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file) {
195 kuid_t skuid = skb->sk->sk_socket->file->f_cred->fsuid;
196 return from_kuid(&init_user_ns, skuid);
197 }
Patrick McHardye5dfb812008-01-31 18:37:42 -0800198 return 0;
199}
200
201static u32 flow_get_skgid(const struct sk_buff *skb)
202{
Eric W. Biedermana6c67962012-05-25 13:49:36 -0600203 if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file) {
204 kgid_t skgid = skb->sk->sk_socket->file->f_cred->fsgid;
205 return from_kgid(&init_user_ns, skgid);
206 }
Patrick McHardye5dfb812008-01-31 18:37:42 -0800207 return 0;
208}
209
Patrick McHardy9ec13812008-02-05 16:21:04 -0800210static u32 flow_get_vlan_tag(const struct sk_buff *skb)
211{
212 u16 uninitialized_var(tag);
213
214 if (vlan_get_tag(skb, &tag) < 0)
215 return 0;
216 return tag & VLAN_VID_MASK;
217}
218
Changli Gao739a91e2010-08-21 06:23:15 +0000219static u32 flow_get_rxhash(struct sk_buff *skb)
220{
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800221 return skb_get_hash(skb);
Changli Gao739a91e2010-08-21 06:23:15 +0000222}
223
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000224static u32 flow_key_get(struct sk_buff *skb, int key, struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800225{
226 switch (key) {
227 case FLOW_KEY_SRC:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000228 return flow_get_src(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800229 case FLOW_KEY_DST:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000230 return flow_get_dst(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800231 case FLOW_KEY_PROTO:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000232 return flow_get_proto(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800233 case FLOW_KEY_PROTO_SRC:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000234 return flow_get_proto_src(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800235 case FLOW_KEY_PROTO_DST:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000236 return flow_get_proto_dst(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800237 case FLOW_KEY_IIF:
238 return flow_get_iif(skb);
239 case FLOW_KEY_PRIORITY:
240 return flow_get_priority(skb);
241 case FLOW_KEY_MARK:
242 return flow_get_mark(skb);
243 case FLOW_KEY_NFCT:
244 return flow_get_nfct(skb);
245 case FLOW_KEY_NFCT_SRC:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000246 return flow_get_nfct_src(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800247 case FLOW_KEY_NFCT_DST:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000248 return flow_get_nfct_dst(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800249 case FLOW_KEY_NFCT_PROTO_SRC:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000250 return flow_get_nfct_proto_src(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800251 case FLOW_KEY_NFCT_PROTO_DST:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000252 return flow_get_nfct_proto_dst(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800253 case FLOW_KEY_RTCLASSID:
254 return flow_get_rtclassid(skb);
255 case FLOW_KEY_SKUID:
256 return flow_get_skuid(skb);
257 case FLOW_KEY_SKGID:
258 return flow_get_skgid(skb);
Patrick McHardy9ec13812008-02-05 16:21:04 -0800259 case FLOW_KEY_VLAN_TAG:
260 return flow_get_vlan_tag(skb);
Changli Gao739a91e2010-08-21 06:23:15 +0000261 case FLOW_KEY_RXHASH:
262 return flow_get_rxhash(skb);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800263 default:
264 WARN_ON(1);
265 return 0;
266 }
267}
268
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000269#define FLOW_KEYS_NEEDED ((1 << FLOW_KEY_SRC) | \
270 (1 << FLOW_KEY_DST) | \
271 (1 << FLOW_KEY_PROTO) | \
272 (1 << FLOW_KEY_PROTO_SRC) | \
273 (1 << FLOW_KEY_PROTO_DST) | \
274 (1 << FLOW_KEY_NFCT_SRC) | \
275 (1 << FLOW_KEY_NFCT_DST) | \
276 (1 << FLOW_KEY_NFCT_PROTO_SRC) | \
277 (1 << FLOW_KEY_NFCT_PROTO_DST))
278
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000279static int flow_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Patrick McHardye5dfb812008-01-31 18:37:42 -0800280 struct tcf_result *res)
281{
John Fastabend70da9f02014-09-12 20:06:55 -0700282 struct flow_head *head = rcu_dereference_bh(tp->root);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800283 struct flow_filter *f;
284 u32 keymask;
285 u32 classid;
286 unsigned int n, key;
287 int r;
288
John Fastabend70da9f02014-09-12 20:06:55 -0700289 list_for_each_entry_rcu(f, &head->filters, list) {
Eric Dumazet3a539432011-12-14 02:30:00 +0000290 u32 keys[FLOW_KEY_MAX + 1];
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000291 struct flow_keys flow_keys;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800292
293 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
294 continue;
295
296 keymask = f->keymask;
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000297 if (keymask & FLOW_KEYS_NEEDED)
298 skb_flow_dissect(skb, &flow_keys);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800299
300 for (n = 0; n < f->nkeys; n++) {
301 key = ffs(keymask) - 1;
302 keymask &= ~(1 << key);
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000303 keys[n] = flow_key_get(skb, key, &flow_keys);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800304 }
305
306 if (f->mode == FLOW_MODE_HASH)
Patrick McHardy72d97942008-07-14 20:36:32 -0700307 classid = jhash2(keys, f->nkeys, f->hashrnd);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800308 else {
309 classid = keys[0];
310 classid = (classid & f->mask) ^ f->xor;
311 classid = (classid >> f->rshift) + f->addend;
312 }
313
314 if (f->divisor)
315 classid %= f->divisor;
316
317 res->class = 0;
318 res->classid = TC_H_MAKE(f->baseclass, f->baseclass + classid);
319
320 r = tcf_exts_exec(skb, &f->exts, res);
321 if (r < 0)
322 continue;
323 return r;
324 }
325 return -1;
326}
327
Patrick McHardy72d97942008-07-14 20:36:32 -0700328static void flow_perturbation(unsigned long arg)
329{
330 struct flow_filter *f = (struct flow_filter *)arg;
331
332 get_random_bytes(&f->hashrnd, 4);
333 if (f->perturb_period)
334 mod_timer(&f->perturb_timer, jiffies + f->perturb_period);
335}
336
Patrick McHardye5dfb812008-01-31 18:37:42 -0800337static const struct nla_policy flow_policy[TCA_FLOW_MAX + 1] = {
338 [TCA_FLOW_KEYS] = { .type = NLA_U32 },
339 [TCA_FLOW_MODE] = { .type = NLA_U32 },
340 [TCA_FLOW_BASECLASS] = { .type = NLA_U32 },
341 [TCA_FLOW_RSHIFT] = { .type = NLA_U32 },
342 [TCA_FLOW_ADDEND] = { .type = NLA_U32 },
343 [TCA_FLOW_MASK] = { .type = NLA_U32 },
344 [TCA_FLOW_XOR] = { .type = NLA_U32 },
345 [TCA_FLOW_DIVISOR] = { .type = NLA_U32 },
346 [TCA_FLOW_ACT] = { .type = NLA_NESTED },
347 [TCA_FLOW_POLICE] = { .type = NLA_NESTED },
348 [TCA_FLOW_EMATCHES] = { .type = NLA_NESTED },
Patrick McHardy72d97942008-07-14 20:36:32 -0700349 [TCA_FLOW_PERTURB] = { .type = NLA_U32 },
Patrick McHardye5dfb812008-01-31 18:37:42 -0800350};
351
John Fastabend70da9f02014-09-12 20:06:55 -0700352static void flow_destroy_filter(struct rcu_head *head)
353{
354 struct flow_filter *f = container_of(head, struct flow_filter, rcu);
355
356 del_timer_sync(&f->perturb_timer);
WANG Cong18d02642014-09-25 10:26:37 -0700357 tcf_exts_destroy(&f->exts);
John Fastabend82a470f2014-10-05 21:27:53 -0700358 tcf_em_tree_destroy(&f->ematches);
John Fastabend70da9f02014-09-12 20:06:55 -0700359 kfree(f);
360}
361
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000362static int flow_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600363 struct tcf_proto *tp, unsigned long base,
Patrick McHardye5dfb812008-01-31 18:37:42 -0800364 u32 handle, struct nlattr **tca,
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700365 unsigned long *arg, bool ovr)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800366{
John Fastabend70da9f02014-09-12 20:06:55 -0700367 struct flow_head *head = rtnl_dereference(tp->root);
368 struct flow_filter *fold, *fnew;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800369 struct nlattr *opt = tca[TCA_OPTIONS];
370 struct nlattr *tb[TCA_FLOW_MAX + 1];
371 struct tcf_exts e;
372 struct tcf_ematch_tree t;
373 unsigned int nkeys = 0;
Patrick McHardy72d97942008-07-14 20:36:32 -0700374 unsigned int perturb_period = 0;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800375 u32 baseclass = 0;
376 u32 keymask = 0;
377 u32 mode;
378 int err;
379
380 if (opt == NULL)
381 return -EINVAL;
382
383 err = nla_parse_nested(tb, TCA_FLOW_MAX, opt, flow_policy);
384 if (err < 0)
385 return err;
386
387 if (tb[TCA_FLOW_BASECLASS]) {
388 baseclass = nla_get_u32(tb[TCA_FLOW_BASECLASS]);
389 if (TC_H_MIN(baseclass) == 0)
390 return -EINVAL;
391 }
392
393 if (tb[TCA_FLOW_KEYS]) {
394 keymask = nla_get_u32(tb[TCA_FLOW_KEYS]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800395
396 nkeys = hweight32(keymask);
397 if (nkeys == 0)
398 return -EINVAL;
Patrick McHardy4f250492008-02-05 16:19:59 -0800399
400 if (fls(keymask) - 1 > FLOW_KEY_MAX)
401 return -EOPNOTSUPP;
Eric W. Biedermana6c67962012-05-25 13:49:36 -0600402
403 if ((keymask & (FLOW_KEY_SKUID|FLOW_KEY_SKGID)) &&
Patrick McHardye32123e2013-04-17 06:46:57 +0000404 sk_user_ns(NETLINK_CB(in_skb).sk) != &init_user_ns)
Eric W. Biedermana6c67962012-05-25 13:49:36 -0600405 return -EOPNOTSUPP;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800406 }
407
WANG Cong5da57f42013-12-15 20:15:07 -0800408 tcf_exts_init(&e, TCA_FLOW_ACT, TCA_FLOW_POLICE);
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700409 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e, ovr);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800410 if (err < 0)
411 return err;
412
413 err = tcf_em_tree_validate(tp, tb[TCA_FLOW_EMATCHES], &t);
414 if (err < 0)
415 goto err1;
416
John Fastabend70da9f02014-09-12 20:06:55 -0700417 err = -ENOBUFS;
418 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
419 if (!fnew)
420 goto err2;
421
422 fold = (struct flow_filter *)*arg;
423 if (fold) {
Patrick McHardye5dfb812008-01-31 18:37:42 -0800424 err = -EINVAL;
John Fastabend70da9f02014-09-12 20:06:55 -0700425 if (fold->handle != handle && handle)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800426 goto err2;
427
John Fastabend70da9f02014-09-12 20:06:55 -0700428 /* Copy fold into fnew */
429 fnew->handle = fold->handle;
430 fnew->keymask = fold->keymask;
431 fnew->tp = fold->tp;
432
433 fnew->handle = fold->handle;
434 fnew->nkeys = fold->nkeys;
435 fnew->keymask = fold->keymask;
436 fnew->mode = fold->mode;
437 fnew->mask = fold->mask;
438 fnew->xor = fold->xor;
439 fnew->rshift = fold->rshift;
440 fnew->addend = fold->addend;
441 fnew->divisor = fold->divisor;
442 fnew->baseclass = fold->baseclass;
443 fnew->hashrnd = fold->hashrnd;
444
445 mode = fold->mode;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800446 if (tb[TCA_FLOW_MODE])
447 mode = nla_get_u32(tb[TCA_FLOW_MODE]);
448 if (mode != FLOW_MODE_HASH && nkeys > 1)
449 goto err2;
Patrick McHardy72d97942008-07-14 20:36:32 -0700450
451 if (mode == FLOW_MODE_HASH)
John Fastabend70da9f02014-09-12 20:06:55 -0700452 perturb_period = fold->perturb_period;
Patrick McHardy72d97942008-07-14 20:36:32 -0700453 if (tb[TCA_FLOW_PERTURB]) {
454 if (mode != FLOW_MODE_HASH)
455 goto err2;
456 perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
457 }
Patrick McHardye5dfb812008-01-31 18:37:42 -0800458 } else {
459 err = -EINVAL;
460 if (!handle)
461 goto err2;
462 if (!tb[TCA_FLOW_KEYS])
463 goto err2;
464
465 mode = FLOW_MODE_MAP;
466 if (tb[TCA_FLOW_MODE])
467 mode = nla_get_u32(tb[TCA_FLOW_MODE]);
468 if (mode != FLOW_MODE_HASH && nkeys > 1)
469 goto err2;
470
Patrick McHardy72d97942008-07-14 20:36:32 -0700471 if (tb[TCA_FLOW_PERTURB]) {
472 if (mode != FLOW_MODE_HASH)
473 goto err2;
474 perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
475 }
476
Patrick McHardye5dfb812008-01-31 18:37:42 -0800477 if (TC_H_MAJ(baseclass) == 0)
478 baseclass = TC_H_MAKE(tp->q->handle, baseclass);
479 if (TC_H_MIN(baseclass) == 0)
480 baseclass = TC_H_MAKE(baseclass, 1);
481
John Fastabend70da9f02014-09-12 20:06:55 -0700482 fnew->handle = handle;
483 fnew->mask = ~0U;
484 fnew->tp = tp;
485 get_random_bytes(&fnew->hashrnd, 4);
486 tcf_exts_init(&fnew->exts, TCA_FLOW_ACT, TCA_FLOW_POLICE);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800487 }
488
John Fastabend70da9f02014-09-12 20:06:55 -0700489 fnew->perturb_timer.function = flow_perturbation;
490 fnew->perturb_timer.data = (unsigned long)fnew;
491 init_timer_deferrable(&fnew->perturb_timer);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800492
John Fastabend70da9f02014-09-12 20:06:55 -0700493 tcf_exts_change(tp, &fnew->exts, &e);
494 tcf_em_tree_change(tp, &fnew->ematches, &t);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800495
Eric Dumazet02875872014-10-05 18:38:35 -0700496 netif_keep_dst(qdisc_dev(tp->q));
497
Patrick McHardye5dfb812008-01-31 18:37:42 -0800498 if (tb[TCA_FLOW_KEYS]) {
John Fastabend70da9f02014-09-12 20:06:55 -0700499 fnew->keymask = keymask;
500 fnew->nkeys = nkeys;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800501 }
502
John Fastabend70da9f02014-09-12 20:06:55 -0700503 fnew->mode = mode;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800504
505 if (tb[TCA_FLOW_MASK])
John Fastabend70da9f02014-09-12 20:06:55 -0700506 fnew->mask = nla_get_u32(tb[TCA_FLOW_MASK]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800507 if (tb[TCA_FLOW_XOR])
John Fastabend70da9f02014-09-12 20:06:55 -0700508 fnew->xor = nla_get_u32(tb[TCA_FLOW_XOR]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800509 if (tb[TCA_FLOW_RSHIFT])
John Fastabend70da9f02014-09-12 20:06:55 -0700510 fnew->rshift = nla_get_u32(tb[TCA_FLOW_RSHIFT]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800511 if (tb[TCA_FLOW_ADDEND])
John Fastabend70da9f02014-09-12 20:06:55 -0700512 fnew->addend = nla_get_u32(tb[TCA_FLOW_ADDEND]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800513
514 if (tb[TCA_FLOW_DIVISOR])
John Fastabend70da9f02014-09-12 20:06:55 -0700515 fnew->divisor = nla_get_u32(tb[TCA_FLOW_DIVISOR]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800516 if (baseclass)
John Fastabend70da9f02014-09-12 20:06:55 -0700517 fnew->baseclass = baseclass;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800518
John Fastabend70da9f02014-09-12 20:06:55 -0700519 fnew->perturb_period = perturb_period;
Patrick McHardy72d97942008-07-14 20:36:32 -0700520 if (perturb_period)
John Fastabend70da9f02014-09-12 20:06:55 -0700521 mod_timer(&fnew->perturb_timer, jiffies + perturb_period);
Patrick McHardy72d97942008-07-14 20:36:32 -0700522
Patrick McHardye5dfb812008-01-31 18:37:42 -0800523 if (*arg == 0)
John Fastabend70da9f02014-09-12 20:06:55 -0700524 list_add_tail_rcu(&fnew->list, &head->filters);
525 else
526 list_replace_rcu(&fnew->list, &fold->list);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800527
John Fastabend70da9f02014-09-12 20:06:55 -0700528 *arg = (unsigned long)fnew;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800529
John Fastabend70da9f02014-09-12 20:06:55 -0700530 if (fold)
531 call_rcu(&fold->rcu, flow_destroy_filter);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800532 return 0;
533
534err2:
John Fastabend82a470f2014-10-05 21:27:53 -0700535 tcf_em_tree_destroy(&t);
John Fastabend70da9f02014-09-12 20:06:55 -0700536 kfree(fnew);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800537err1:
WANG Cong18d02642014-09-25 10:26:37 -0700538 tcf_exts_destroy(&e);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800539 return err;
540}
541
Patrick McHardye5dfb812008-01-31 18:37:42 -0800542static int flow_delete(struct tcf_proto *tp, unsigned long arg)
543{
544 struct flow_filter *f = (struct flow_filter *)arg;
545
John Fastabend70da9f02014-09-12 20:06:55 -0700546 list_del_rcu(&f->list);
547 call_rcu(&f->rcu, flow_destroy_filter);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800548 return 0;
549}
550
551static int flow_init(struct tcf_proto *tp)
552{
553 struct flow_head *head;
554
Patrick McHardye5dfb812008-01-31 18:37:42 -0800555 head = kzalloc(sizeof(*head), GFP_KERNEL);
556 if (head == NULL)
557 return -ENOBUFS;
558 INIT_LIST_HEAD(&head->filters);
John Fastabend70da9f02014-09-12 20:06:55 -0700559 rcu_assign_pointer(tp->root, head);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800560 return 0;
561}
562
563static void flow_destroy(struct tcf_proto *tp)
564{
John Fastabend70da9f02014-09-12 20:06:55 -0700565 struct flow_head *head = rtnl_dereference(tp->root);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800566 struct flow_filter *f, *next;
567
568 list_for_each_entry_safe(f, next, &head->filters, list) {
John Fastabend70da9f02014-09-12 20:06:55 -0700569 list_del_rcu(&f->list);
570 call_rcu(&f->rcu, flow_destroy_filter);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800571 }
John Fastabend70da9f02014-09-12 20:06:55 -0700572 RCU_INIT_POINTER(tp->root, NULL);
573 kfree_rcu(head, rcu);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800574}
575
576static unsigned long flow_get(struct tcf_proto *tp, u32 handle)
577{
John Fastabend70da9f02014-09-12 20:06:55 -0700578 struct flow_head *head = rtnl_dereference(tp->root);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800579 struct flow_filter *f;
580
John Fastabend70da9f02014-09-12 20:06:55 -0700581 list_for_each_entry_rcu(f, &head->filters, list)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800582 if (f->handle == handle)
583 return (unsigned long)f;
584 return 0;
585}
586
587static void flow_put(struct tcf_proto *tp, unsigned long f)
588{
Patrick McHardye5dfb812008-01-31 18:37:42 -0800589}
590
WANG Cong832d1d52014-01-09 16:14:01 -0800591static int flow_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
Patrick McHardye5dfb812008-01-31 18:37:42 -0800592 struct sk_buff *skb, struct tcmsg *t)
593{
594 struct flow_filter *f = (struct flow_filter *)fh;
595 struct nlattr *nest;
596
597 if (f == NULL)
598 return skb->len;
599
600 t->tcm_handle = f->handle;
601
602 nest = nla_nest_start(skb, TCA_OPTIONS);
603 if (nest == NULL)
604 goto nla_put_failure;
605
David S. Miller1b34ec42012-03-29 05:11:39 -0400606 if (nla_put_u32(skb, TCA_FLOW_KEYS, f->keymask) ||
607 nla_put_u32(skb, TCA_FLOW_MODE, f->mode))
608 goto nla_put_failure;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800609
610 if (f->mask != ~0 || f->xor != 0) {
David S. Miller1b34ec42012-03-29 05:11:39 -0400611 if (nla_put_u32(skb, TCA_FLOW_MASK, f->mask) ||
612 nla_put_u32(skb, TCA_FLOW_XOR, f->xor))
613 goto nla_put_failure;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800614 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400615 if (f->rshift &&
616 nla_put_u32(skb, TCA_FLOW_RSHIFT, f->rshift))
617 goto nla_put_failure;
618 if (f->addend &&
619 nla_put_u32(skb, TCA_FLOW_ADDEND, f->addend))
620 goto nla_put_failure;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800621
David S. Miller1b34ec42012-03-29 05:11:39 -0400622 if (f->divisor &&
623 nla_put_u32(skb, TCA_FLOW_DIVISOR, f->divisor))
624 goto nla_put_failure;
625 if (f->baseclass &&
626 nla_put_u32(skb, TCA_FLOW_BASECLASS, f->baseclass))
627 goto nla_put_failure;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800628
David S. Miller1b34ec42012-03-29 05:11:39 -0400629 if (f->perturb_period &&
630 nla_put_u32(skb, TCA_FLOW_PERTURB, f->perturb_period / HZ))
631 goto nla_put_failure;
Patrick McHardy72d97942008-07-14 20:36:32 -0700632
WANG Cong5da57f42013-12-15 20:15:07 -0800633 if (tcf_exts_dump(skb, &f->exts) < 0)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800634 goto nla_put_failure;
Rami Rosen0aead542008-02-05 02:56:48 -0800635#ifdef CONFIG_NET_EMATCH
Patrick McHardye5dfb812008-01-31 18:37:42 -0800636 if (f->ematches.hdr.nmatches &&
637 tcf_em_tree_dump(skb, &f->ematches, TCA_FLOW_EMATCHES) < 0)
638 goto nla_put_failure;
Rami Rosen0aead542008-02-05 02:56:48 -0800639#endif
Patrick McHardye5dfb812008-01-31 18:37:42 -0800640 nla_nest_end(skb, nest);
641
WANG Cong5da57f42013-12-15 20:15:07 -0800642 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800643 goto nla_put_failure;
644
645 return skb->len;
646
647nla_put_failure:
648 nlmsg_trim(skb, nest);
649 return -1;
650}
651
652static void flow_walk(struct tcf_proto *tp, struct tcf_walker *arg)
653{
John Fastabend70da9f02014-09-12 20:06:55 -0700654 struct flow_head *head = rtnl_dereference(tp->root);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800655 struct flow_filter *f;
656
John Fastabend70da9f02014-09-12 20:06:55 -0700657 list_for_each_entry_rcu(f, &head->filters, list) {
Patrick McHardye5dfb812008-01-31 18:37:42 -0800658 if (arg->count < arg->skip)
659 goto skip;
660 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
661 arg->stop = 1;
662 break;
663 }
664skip:
665 arg->count++;
666 }
667}
668
669static struct tcf_proto_ops cls_flow_ops __read_mostly = {
670 .kind = "flow",
671 .classify = flow_classify,
672 .init = flow_init,
673 .destroy = flow_destroy,
674 .change = flow_change,
675 .delete = flow_delete,
676 .get = flow_get,
677 .put = flow_put,
678 .dump = flow_dump,
679 .walk = flow_walk,
680 .owner = THIS_MODULE,
681};
682
683static int __init cls_flow_init(void)
684{
685 return register_tcf_proto_ops(&cls_flow_ops);
686}
687
688static void __exit cls_flow_exit(void)
689{
690 unregister_tcf_proto_ops(&cls_flow_ops);
691}
692
693module_init(cls_flow_init);
694module_exit(cls_flow_exit);
695
696MODULE_LICENSE("GPL");
697MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
698MODULE_DESCRIPTION("TC flow classifier");