blob: 76bc3a20ffdb31bb4c9b51942de74c64928c2a3a [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>
Jiri Pirko1bd758e2015-05-12 14:56:07 +020029#include <net/flow_dissector.h>
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000030
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{
Tom Herbertc3f83242015-06-04 09:16:40 -070071 __be32 src = flow_get_u32_src(flow);
72
73 if (src)
74 return ntohl(src);
75
Changli Gao4b95c3d2010-08-04 04:48:12 +000076 return addr_fold(skb->sk);
Patrick McHardye5dfb812008-01-31 18:37:42 -080077}
78
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000079static u32 flow_get_dst(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -080080{
Tom Herbertc3f83242015-06-04 09:16:40 -070081 __be32 dst = flow_get_u32_dst(flow);
82
83 if (dst)
84 return ntohl(dst);
85
Jiri Pirkod8b96052015-01-13 17:13:43 +010086 return addr_fold(skb_dst(skb)) ^ (__force u16) tc_skb_protocol(skb);
Patrick McHardye5dfb812008-01-31 18:37:42 -080087}
88
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000089static u32 flow_get_proto(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -080090{
Jiri Pirko06635a32015-05-12 14:56:16 +020091 return flow->basic.ip_proto;
Patrick McHardye5dfb812008-01-31 18:37:42 -080092}
93
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +000094static u32 flow_get_proto_src(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -080095{
Jiri Pirko06635a32015-05-12 14:56:16 +020096 if (flow->ports.ports)
Jiri Pirko59346af2015-05-12 14:56:20 +020097 return ntohs(flow->ports.src);
Eric Dumazet859c2012011-10-23 17:59:41 +000098
Changli Gao4b95c3d2010-08-04 04:48:12 +000099 return addr_fold(skb->sk);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800100}
101
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000102static u32 flow_get_proto_dst(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800103{
Jiri Pirko06635a32015-05-12 14:56:16 +0200104 if (flow->ports.ports)
Jiri Pirko59346af2015-05-12 14:56:20 +0200105 return ntohs(flow->ports.dst);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800106
Jiri Pirkod8b96052015-01-13 17:13:43 +0100107 return addr_fold(skb_dst(skb)) ^ (__force u16) tc_skb_protocol(skb);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800108}
109
110static u32 flow_get_iif(const struct sk_buff *skb)
111{
Eric Dumazet8964be42009-11-20 15:35:04 -0800112 return skb->skb_iif;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800113}
114
115static u32 flow_get_priority(const struct sk_buff *skb)
116{
117 return skb->priority;
118}
119
120static u32 flow_get_mark(const struct sk_buff *skb)
121{
122 return skb->mark;
123}
124
125static u32 flow_get_nfct(const struct sk_buff *skb)
126{
127#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
128 return addr_fold(skb->nfct);
129#else
130 return 0;
131#endif
132}
133
134#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
135#define CTTUPLE(skb, member) \
136({ \
137 enum ip_conntrack_info ctinfo; \
Eric Dumazet859c2012011-10-23 17:59:41 +0000138 const struct nf_conn *ct = nf_ct_get(skb, &ctinfo); \
Patrick McHardye5dfb812008-01-31 18:37:42 -0800139 if (ct == NULL) \
140 goto fallback; \
141 ct->tuplehash[CTINFO2DIR(ctinfo)].tuple.member; \
142})
143#else
144#define CTTUPLE(skb, member) \
145({ \
146 goto fallback; \
147 0; \
148})
149#endif
150
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000151static u32 flow_get_nfct_src(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800152{
Jiri Pirkod8b96052015-01-13 17:13:43 +0100153 switch (tc_skb_protocol(skb)) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700154 case htons(ETH_P_IP):
Patrick McHardye5dfb812008-01-31 18:37:42 -0800155 return ntohl(CTTUPLE(skb, src.u3.ip));
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700156 case htons(ETH_P_IPV6):
Patrick McHardye5dfb812008-01-31 18:37:42 -0800157 return ntohl(CTTUPLE(skb, src.u3.ip6[3]));
158 }
159fallback:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000160 return flow_get_src(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800161}
162
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000163static u32 flow_get_nfct_dst(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800164{
Jiri Pirkod8b96052015-01-13 17:13:43 +0100165 switch (tc_skb_protocol(skb)) {
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700166 case htons(ETH_P_IP):
Patrick McHardye5dfb812008-01-31 18:37:42 -0800167 return ntohl(CTTUPLE(skb, dst.u3.ip));
Arnaldo Carvalho de Melo60678042008-09-20 22:20:49 -0700168 case htons(ETH_P_IPV6):
Patrick McHardye5dfb812008-01-31 18:37:42 -0800169 return ntohl(CTTUPLE(skb, dst.u3.ip6[3]));
170 }
171fallback:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000172 return flow_get_dst(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800173}
174
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000175static u32 flow_get_nfct_proto_src(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800176{
177 return ntohs(CTTUPLE(skb, src.u.all));
178fallback:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000179 return flow_get_proto_src(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800180}
181
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000182static u32 flow_get_nfct_proto_dst(const struct sk_buff *skb, const struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800183{
184 return ntohs(CTTUPLE(skb, dst.u.all));
185fallback:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000186 return flow_get_proto_dst(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800187}
188
189static u32 flow_get_rtclassid(const struct sk_buff *skb)
190{
Patrick McHardyc7066f72011-01-14 13:36:42 +0100191#ifdef CONFIG_IP_ROUTE_CLASSID
Eric Dumazetadf30902009-06-02 05:19:30 +0000192 if (skb_dst(skb))
193 return skb_dst(skb)->tclassid;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800194#endif
195 return 0;
196}
197
198static u32 flow_get_skuid(const struct sk_buff *skb)
199{
Eric W. Biedermana6c67962012-05-25 13:49:36 -0600200 if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file) {
201 kuid_t skuid = skb->sk->sk_socket->file->f_cred->fsuid;
202 return from_kuid(&init_user_ns, skuid);
203 }
Patrick McHardye5dfb812008-01-31 18:37:42 -0800204 return 0;
205}
206
207static u32 flow_get_skgid(const struct sk_buff *skb)
208{
Eric W. Biedermana6c67962012-05-25 13:49:36 -0600209 if (skb->sk && skb->sk->sk_socket && skb->sk->sk_socket->file) {
210 kgid_t skgid = skb->sk->sk_socket->file->f_cred->fsgid;
211 return from_kgid(&init_user_ns, skgid);
212 }
Patrick McHardye5dfb812008-01-31 18:37:42 -0800213 return 0;
214}
215
Patrick McHardy9ec13812008-02-05 16:21:04 -0800216static u32 flow_get_vlan_tag(const struct sk_buff *skb)
217{
218 u16 uninitialized_var(tag);
219
220 if (vlan_get_tag(skb, &tag) < 0)
221 return 0;
222 return tag & VLAN_VID_MASK;
223}
224
Changli Gao739a91e2010-08-21 06:23:15 +0000225static u32 flow_get_rxhash(struct sk_buff *skb)
226{
Tom Herbert3958afa1b2013-12-15 22:12:06 -0800227 return skb_get_hash(skb);
Changli Gao739a91e2010-08-21 06:23:15 +0000228}
229
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000230static u32 flow_key_get(struct sk_buff *skb, int key, struct flow_keys *flow)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800231{
232 switch (key) {
233 case FLOW_KEY_SRC:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000234 return flow_get_src(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800235 case FLOW_KEY_DST:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000236 return flow_get_dst(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800237 case FLOW_KEY_PROTO:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000238 return flow_get_proto(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800239 case FLOW_KEY_PROTO_SRC:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000240 return flow_get_proto_src(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800241 case FLOW_KEY_PROTO_DST:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000242 return flow_get_proto_dst(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800243 case FLOW_KEY_IIF:
244 return flow_get_iif(skb);
245 case FLOW_KEY_PRIORITY:
246 return flow_get_priority(skb);
247 case FLOW_KEY_MARK:
248 return flow_get_mark(skb);
249 case FLOW_KEY_NFCT:
250 return flow_get_nfct(skb);
251 case FLOW_KEY_NFCT_SRC:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000252 return flow_get_nfct_src(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800253 case FLOW_KEY_NFCT_DST:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000254 return flow_get_nfct_dst(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800255 case FLOW_KEY_NFCT_PROTO_SRC:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000256 return flow_get_nfct_proto_src(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800257 case FLOW_KEY_NFCT_PROTO_DST:
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000258 return flow_get_nfct_proto_dst(skb, flow);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800259 case FLOW_KEY_RTCLASSID:
260 return flow_get_rtclassid(skb);
261 case FLOW_KEY_SKUID:
262 return flow_get_skuid(skb);
263 case FLOW_KEY_SKGID:
264 return flow_get_skgid(skb);
Patrick McHardy9ec13812008-02-05 16:21:04 -0800265 case FLOW_KEY_VLAN_TAG:
266 return flow_get_vlan_tag(skb);
Changli Gao739a91e2010-08-21 06:23:15 +0000267 case FLOW_KEY_RXHASH:
268 return flow_get_rxhash(skb);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800269 default:
270 WARN_ON(1);
271 return 0;
272 }
273}
274
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000275#define FLOW_KEYS_NEEDED ((1 << FLOW_KEY_SRC) | \
276 (1 << FLOW_KEY_DST) | \
277 (1 << FLOW_KEY_PROTO) | \
278 (1 << FLOW_KEY_PROTO_SRC) | \
279 (1 << FLOW_KEY_PROTO_DST) | \
280 (1 << FLOW_KEY_NFCT_SRC) | \
281 (1 << FLOW_KEY_NFCT_DST) | \
282 (1 << FLOW_KEY_NFCT_PROTO_SRC) | \
283 (1 << FLOW_KEY_NFCT_PROTO_DST))
284
Eric Dumazetdc7f9f62011-07-05 23:25:42 +0000285static int flow_classify(struct sk_buff *skb, const struct tcf_proto *tp,
Patrick McHardye5dfb812008-01-31 18:37:42 -0800286 struct tcf_result *res)
287{
John Fastabend70da9f02014-09-12 20:06:55 -0700288 struct flow_head *head = rcu_dereference_bh(tp->root);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800289 struct flow_filter *f;
290 u32 keymask;
291 u32 classid;
292 unsigned int n, key;
293 int r;
294
John Fastabend70da9f02014-09-12 20:06:55 -0700295 list_for_each_entry_rcu(f, &head->filters, list) {
Eric Dumazet3a539432011-12-14 02:30:00 +0000296 u32 keys[FLOW_KEY_MAX + 1];
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000297 struct flow_keys flow_keys;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800298
299 if (!tcf_em_tree_match(skb, &f->ematches, NULL))
300 continue;
301
302 keymask = f->keymask;
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000303 if (keymask & FLOW_KEYS_NEEDED)
Jiri Pirko06635a32015-05-12 14:56:16 +0200304 skb_flow_dissect_flow_keys(skb, &flow_keys);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800305
306 for (n = 0; n < f->nkeys; n++) {
307 key = ffs(keymask) - 1;
308 keymask &= ~(1 << key);
Eric Dumazet6bd2a9a2011-11-28 05:24:18 +0000309 keys[n] = flow_key_get(skb, key, &flow_keys);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800310 }
311
312 if (f->mode == FLOW_MODE_HASH)
Patrick McHardy72d97942008-07-14 20:36:32 -0700313 classid = jhash2(keys, f->nkeys, f->hashrnd);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800314 else {
315 classid = keys[0];
316 classid = (classid & f->mask) ^ f->xor;
317 classid = (classid >> f->rshift) + f->addend;
318 }
319
320 if (f->divisor)
321 classid %= f->divisor;
322
323 res->class = 0;
324 res->classid = TC_H_MAKE(f->baseclass, f->baseclass + classid);
325
326 r = tcf_exts_exec(skb, &f->exts, res);
327 if (r < 0)
328 continue;
329 return r;
330 }
331 return -1;
332}
333
Patrick McHardy72d97942008-07-14 20:36:32 -0700334static void flow_perturbation(unsigned long arg)
335{
336 struct flow_filter *f = (struct flow_filter *)arg;
337
338 get_random_bytes(&f->hashrnd, 4);
339 if (f->perturb_period)
340 mod_timer(&f->perturb_timer, jiffies + f->perturb_period);
341}
342
Patrick McHardye5dfb812008-01-31 18:37:42 -0800343static const struct nla_policy flow_policy[TCA_FLOW_MAX + 1] = {
344 [TCA_FLOW_KEYS] = { .type = NLA_U32 },
345 [TCA_FLOW_MODE] = { .type = NLA_U32 },
346 [TCA_FLOW_BASECLASS] = { .type = NLA_U32 },
347 [TCA_FLOW_RSHIFT] = { .type = NLA_U32 },
348 [TCA_FLOW_ADDEND] = { .type = NLA_U32 },
349 [TCA_FLOW_MASK] = { .type = NLA_U32 },
350 [TCA_FLOW_XOR] = { .type = NLA_U32 },
351 [TCA_FLOW_DIVISOR] = { .type = NLA_U32 },
352 [TCA_FLOW_ACT] = { .type = NLA_NESTED },
353 [TCA_FLOW_POLICE] = { .type = NLA_NESTED },
354 [TCA_FLOW_EMATCHES] = { .type = NLA_NESTED },
Patrick McHardy72d97942008-07-14 20:36:32 -0700355 [TCA_FLOW_PERTURB] = { .type = NLA_U32 },
Patrick McHardye5dfb812008-01-31 18:37:42 -0800356};
357
John Fastabend70da9f02014-09-12 20:06:55 -0700358static void flow_destroy_filter(struct rcu_head *head)
359{
360 struct flow_filter *f = container_of(head, struct flow_filter, rcu);
361
362 del_timer_sync(&f->perturb_timer);
WANG Cong18d02642014-09-25 10:26:37 -0700363 tcf_exts_destroy(&f->exts);
John Fastabend82a470f2014-10-05 21:27:53 -0700364 tcf_em_tree_destroy(&f->ematches);
John Fastabend70da9f02014-09-12 20:06:55 -0700365 kfree(f);
366}
367
Benjamin LaHaisec1b52732013-01-14 05:15:39 +0000368static int flow_change(struct net *net, struct sk_buff *in_skb,
Eric W. Biedermanaf4c6642012-05-25 13:42:45 -0600369 struct tcf_proto *tp, unsigned long base,
Patrick McHardye5dfb812008-01-31 18:37:42 -0800370 u32 handle, struct nlattr **tca,
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700371 unsigned long *arg, bool ovr)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800372{
John Fastabend70da9f02014-09-12 20:06:55 -0700373 struct flow_head *head = rtnl_dereference(tp->root);
374 struct flow_filter *fold, *fnew;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800375 struct nlattr *opt = tca[TCA_OPTIONS];
376 struct nlattr *tb[TCA_FLOW_MAX + 1];
377 struct tcf_exts e;
378 struct tcf_ematch_tree t;
379 unsigned int nkeys = 0;
Patrick McHardy72d97942008-07-14 20:36:32 -0700380 unsigned int perturb_period = 0;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800381 u32 baseclass = 0;
382 u32 keymask = 0;
383 u32 mode;
384 int err;
385
386 if (opt == NULL)
387 return -EINVAL;
388
389 err = nla_parse_nested(tb, TCA_FLOW_MAX, opt, flow_policy);
390 if (err < 0)
391 return err;
392
393 if (tb[TCA_FLOW_BASECLASS]) {
394 baseclass = nla_get_u32(tb[TCA_FLOW_BASECLASS]);
395 if (TC_H_MIN(baseclass) == 0)
396 return -EINVAL;
397 }
398
399 if (tb[TCA_FLOW_KEYS]) {
400 keymask = nla_get_u32(tb[TCA_FLOW_KEYS]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800401
402 nkeys = hweight32(keymask);
403 if (nkeys == 0)
404 return -EINVAL;
Patrick McHardy4f250492008-02-05 16:19:59 -0800405
406 if (fls(keymask) - 1 > FLOW_KEY_MAX)
407 return -EOPNOTSUPP;
Eric W. Biedermana6c67962012-05-25 13:49:36 -0600408
409 if ((keymask & (FLOW_KEY_SKUID|FLOW_KEY_SKGID)) &&
Patrick McHardye32123e2013-04-17 06:46:57 +0000410 sk_user_ns(NETLINK_CB(in_skb).sk) != &init_user_ns)
Eric W. Biedermana6c67962012-05-25 13:49:36 -0600411 return -EOPNOTSUPP;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800412 }
413
WANG Cong5da57f42013-12-15 20:15:07 -0800414 tcf_exts_init(&e, TCA_FLOW_ACT, TCA_FLOW_POLICE);
Cong Wang2f7ef2f2014-04-25 13:54:06 -0700415 err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e, ovr);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800416 if (err < 0)
417 return err;
418
419 err = tcf_em_tree_validate(tp, tb[TCA_FLOW_EMATCHES], &t);
420 if (err < 0)
421 goto err1;
422
John Fastabend70da9f02014-09-12 20:06:55 -0700423 err = -ENOBUFS;
424 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
425 if (!fnew)
426 goto err2;
427
428 fold = (struct flow_filter *)*arg;
429 if (fold) {
Patrick McHardye5dfb812008-01-31 18:37:42 -0800430 err = -EINVAL;
John Fastabend70da9f02014-09-12 20:06:55 -0700431 if (fold->handle != handle && handle)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800432 goto err2;
433
John Fastabend70da9f02014-09-12 20:06:55 -0700434 /* Copy fold into fnew */
John Fastabend70da9f02014-09-12 20:06:55 -0700435 fnew->tp = fold->tp;
John Fastabend70da9f02014-09-12 20:06:55 -0700436 fnew->handle = fold->handle;
437 fnew->nkeys = fold->nkeys;
438 fnew->keymask = fold->keymask;
439 fnew->mode = fold->mode;
440 fnew->mask = fold->mask;
441 fnew->xor = fold->xor;
442 fnew->rshift = fold->rshift;
443 fnew->addend = fold->addend;
444 fnew->divisor = fold->divisor;
445 fnew->baseclass = fold->baseclass;
446 fnew->hashrnd = fold->hashrnd;
447
448 mode = fold->mode;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800449 if (tb[TCA_FLOW_MODE])
450 mode = nla_get_u32(tb[TCA_FLOW_MODE]);
451 if (mode != FLOW_MODE_HASH && nkeys > 1)
452 goto err2;
Patrick McHardy72d97942008-07-14 20:36:32 -0700453
454 if (mode == FLOW_MODE_HASH)
John Fastabend70da9f02014-09-12 20:06:55 -0700455 perturb_period = fold->perturb_period;
Patrick McHardy72d97942008-07-14 20:36:32 -0700456 if (tb[TCA_FLOW_PERTURB]) {
457 if (mode != FLOW_MODE_HASH)
458 goto err2;
459 perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
460 }
Patrick McHardye5dfb812008-01-31 18:37:42 -0800461 } else {
462 err = -EINVAL;
463 if (!handle)
464 goto err2;
465 if (!tb[TCA_FLOW_KEYS])
466 goto err2;
467
468 mode = FLOW_MODE_MAP;
469 if (tb[TCA_FLOW_MODE])
470 mode = nla_get_u32(tb[TCA_FLOW_MODE]);
471 if (mode != FLOW_MODE_HASH && nkeys > 1)
472 goto err2;
473
Patrick McHardy72d97942008-07-14 20:36:32 -0700474 if (tb[TCA_FLOW_PERTURB]) {
475 if (mode != FLOW_MODE_HASH)
476 goto err2;
477 perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ;
478 }
479
Patrick McHardye5dfb812008-01-31 18:37:42 -0800480 if (TC_H_MAJ(baseclass) == 0)
481 baseclass = TC_H_MAKE(tp->q->handle, baseclass);
482 if (TC_H_MIN(baseclass) == 0)
483 baseclass = TC_H_MAKE(baseclass, 1);
484
John Fastabend70da9f02014-09-12 20:06:55 -0700485 fnew->handle = handle;
486 fnew->mask = ~0U;
487 fnew->tp = tp;
488 get_random_bytes(&fnew->hashrnd, 4);
489 tcf_exts_init(&fnew->exts, TCA_FLOW_ACT, TCA_FLOW_POLICE);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800490 }
491
John Fastabend70da9f02014-09-12 20:06:55 -0700492 fnew->perturb_timer.function = flow_perturbation;
493 fnew->perturb_timer.data = (unsigned long)fnew;
494 init_timer_deferrable(&fnew->perturb_timer);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800495
John Fastabend70da9f02014-09-12 20:06:55 -0700496 tcf_exts_change(tp, &fnew->exts, &e);
497 tcf_em_tree_change(tp, &fnew->ematches, &t);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800498
Eric Dumazet02875872014-10-05 18:38:35 -0700499 netif_keep_dst(qdisc_dev(tp->q));
500
Patrick McHardye5dfb812008-01-31 18:37:42 -0800501 if (tb[TCA_FLOW_KEYS]) {
John Fastabend70da9f02014-09-12 20:06:55 -0700502 fnew->keymask = keymask;
503 fnew->nkeys = nkeys;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800504 }
505
John Fastabend70da9f02014-09-12 20:06:55 -0700506 fnew->mode = mode;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800507
508 if (tb[TCA_FLOW_MASK])
John Fastabend70da9f02014-09-12 20:06:55 -0700509 fnew->mask = nla_get_u32(tb[TCA_FLOW_MASK]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800510 if (tb[TCA_FLOW_XOR])
John Fastabend70da9f02014-09-12 20:06:55 -0700511 fnew->xor = nla_get_u32(tb[TCA_FLOW_XOR]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800512 if (tb[TCA_FLOW_RSHIFT])
John Fastabend70da9f02014-09-12 20:06:55 -0700513 fnew->rshift = nla_get_u32(tb[TCA_FLOW_RSHIFT]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800514 if (tb[TCA_FLOW_ADDEND])
John Fastabend70da9f02014-09-12 20:06:55 -0700515 fnew->addend = nla_get_u32(tb[TCA_FLOW_ADDEND]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800516
517 if (tb[TCA_FLOW_DIVISOR])
John Fastabend70da9f02014-09-12 20:06:55 -0700518 fnew->divisor = nla_get_u32(tb[TCA_FLOW_DIVISOR]);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800519 if (baseclass)
John Fastabend70da9f02014-09-12 20:06:55 -0700520 fnew->baseclass = baseclass;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800521
John Fastabend70da9f02014-09-12 20:06:55 -0700522 fnew->perturb_period = perturb_period;
Patrick McHardy72d97942008-07-14 20:36:32 -0700523 if (perturb_period)
John Fastabend70da9f02014-09-12 20:06:55 -0700524 mod_timer(&fnew->perturb_timer, jiffies + perturb_period);
Patrick McHardy72d97942008-07-14 20:36:32 -0700525
Patrick McHardye5dfb812008-01-31 18:37:42 -0800526 if (*arg == 0)
John Fastabend70da9f02014-09-12 20:06:55 -0700527 list_add_tail_rcu(&fnew->list, &head->filters);
528 else
529 list_replace_rcu(&fnew->list, &fold->list);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800530
John Fastabend70da9f02014-09-12 20:06:55 -0700531 *arg = (unsigned long)fnew;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800532
John Fastabend70da9f02014-09-12 20:06:55 -0700533 if (fold)
534 call_rcu(&fold->rcu, flow_destroy_filter);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800535 return 0;
536
537err2:
John Fastabend82a470f2014-10-05 21:27:53 -0700538 tcf_em_tree_destroy(&t);
John Fastabend70da9f02014-09-12 20:06:55 -0700539 kfree(fnew);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800540err1:
WANG Cong18d02642014-09-25 10:26:37 -0700541 tcf_exts_destroy(&e);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800542 return err;
543}
544
Patrick McHardye5dfb812008-01-31 18:37:42 -0800545static int flow_delete(struct tcf_proto *tp, unsigned long arg)
546{
547 struct flow_filter *f = (struct flow_filter *)arg;
548
John Fastabend70da9f02014-09-12 20:06:55 -0700549 list_del_rcu(&f->list);
550 call_rcu(&f->rcu, flow_destroy_filter);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800551 return 0;
552}
553
554static int flow_init(struct tcf_proto *tp)
555{
556 struct flow_head *head;
557
Patrick McHardye5dfb812008-01-31 18:37:42 -0800558 head = kzalloc(sizeof(*head), GFP_KERNEL);
559 if (head == NULL)
560 return -ENOBUFS;
561 INIT_LIST_HEAD(&head->filters);
John Fastabend70da9f02014-09-12 20:06:55 -0700562 rcu_assign_pointer(tp->root, head);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800563 return 0;
564}
565
Cong Wang1e052be2015-03-06 11:47:59 -0800566static bool flow_destroy(struct tcf_proto *tp, bool force)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800567{
John Fastabend70da9f02014-09-12 20:06:55 -0700568 struct flow_head *head = rtnl_dereference(tp->root);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800569 struct flow_filter *f, *next;
570
Cong Wang1e052be2015-03-06 11:47:59 -0800571 if (!force && !list_empty(&head->filters))
572 return false;
573
Patrick McHardye5dfb812008-01-31 18:37:42 -0800574 list_for_each_entry_safe(f, next, &head->filters, list) {
John Fastabend70da9f02014-09-12 20:06:55 -0700575 list_del_rcu(&f->list);
576 call_rcu(&f->rcu, flow_destroy_filter);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800577 }
John Fastabend70da9f02014-09-12 20:06:55 -0700578 RCU_INIT_POINTER(tp->root, NULL);
579 kfree_rcu(head, rcu);
Cong Wang1e052be2015-03-06 11:47:59 -0800580 return true;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800581}
582
583static unsigned long flow_get(struct tcf_proto *tp, u32 handle)
584{
John Fastabend70da9f02014-09-12 20:06:55 -0700585 struct flow_head *head = rtnl_dereference(tp->root);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800586 struct flow_filter *f;
587
Jiri Pirko6a659cd2014-12-02 18:00:34 +0100588 list_for_each_entry(f, &head->filters, list)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800589 if (f->handle == handle)
590 return (unsigned long)f;
591 return 0;
592}
593
WANG Cong832d1d52014-01-09 16:14:01 -0800594static int flow_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
Patrick McHardye5dfb812008-01-31 18:37:42 -0800595 struct sk_buff *skb, struct tcmsg *t)
596{
597 struct flow_filter *f = (struct flow_filter *)fh;
598 struct nlattr *nest;
599
600 if (f == NULL)
601 return skb->len;
602
603 t->tcm_handle = f->handle;
604
605 nest = nla_nest_start(skb, TCA_OPTIONS);
606 if (nest == NULL)
607 goto nla_put_failure;
608
David S. Miller1b34ec42012-03-29 05:11:39 -0400609 if (nla_put_u32(skb, TCA_FLOW_KEYS, f->keymask) ||
610 nla_put_u32(skb, TCA_FLOW_MODE, f->mode))
611 goto nla_put_failure;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800612
613 if (f->mask != ~0 || f->xor != 0) {
David S. Miller1b34ec42012-03-29 05:11:39 -0400614 if (nla_put_u32(skb, TCA_FLOW_MASK, f->mask) ||
615 nla_put_u32(skb, TCA_FLOW_XOR, f->xor))
616 goto nla_put_failure;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800617 }
David S. Miller1b34ec42012-03-29 05:11:39 -0400618 if (f->rshift &&
619 nla_put_u32(skb, TCA_FLOW_RSHIFT, f->rshift))
620 goto nla_put_failure;
621 if (f->addend &&
622 nla_put_u32(skb, TCA_FLOW_ADDEND, f->addend))
623 goto nla_put_failure;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800624
David S. Miller1b34ec42012-03-29 05:11:39 -0400625 if (f->divisor &&
626 nla_put_u32(skb, TCA_FLOW_DIVISOR, f->divisor))
627 goto nla_put_failure;
628 if (f->baseclass &&
629 nla_put_u32(skb, TCA_FLOW_BASECLASS, f->baseclass))
630 goto nla_put_failure;
Patrick McHardye5dfb812008-01-31 18:37:42 -0800631
David S. Miller1b34ec42012-03-29 05:11:39 -0400632 if (f->perturb_period &&
633 nla_put_u32(skb, TCA_FLOW_PERTURB, f->perturb_period / HZ))
634 goto nla_put_failure;
Patrick McHardy72d97942008-07-14 20:36:32 -0700635
WANG Cong5da57f42013-12-15 20:15:07 -0800636 if (tcf_exts_dump(skb, &f->exts) < 0)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800637 goto nla_put_failure;
Rami Rosen0aead542008-02-05 02:56:48 -0800638#ifdef CONFIG_NET_EMATCH
Patrick McHardye5dfb812008-01-31 18:37:42 -0800639 if (f->ematches.hdr.nmatches &&
640 tcf_em_tree_dump(skb, &f->ematches, TCA_FLOW_EMATCHES) < 0)
641 goto nla_put_failure;
Rami Rosen0aead542008-02-05 02:56:48 -0800642#endif
Patrick McHardye5dfb812008-01-31 18:37:42 -0800643 nla_nest_end(skb, nest);
644
WANG Cong5da57f42013-12-15 20:15:07 -0800645 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
Patrick McHardye5dfb812008-01-31 18:37:42 -0800646 goto nla_put_failure;
647
648 return skb->len;
649
650nla_put_failure:
Jiri Pirko6ea3b442014-12-09 22:23:29 +0100651 nla_nest_cancel(skb, nest);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800652 return -1;
653}
654
655static void flow_walk(struct tcf_proto *tp, struct tcf_walker *arg)
656{
John Fastabend70da9f02014-09-12 20:06:55 -0700657 struct flow_head *head = rtnl_dereference(tp->root);
Patrick McHardye5dfb812008-01-31 18:37:42 -0800658 struct flow_filter *f;
659
Jiri Pirko6a659cd2014-12-02 18:00:34 +0100660 list_for_each_entry(f, &head->filters, list) {
Patrick McHardye5dfb812008-01-31 18:37:42 -0800661 if (arg->count < arg->skip)
662 goto skip;
663 if (arg->fn(tp, (unsigned long)f, arg) < 0) {
664 arg->stop = 1;
665 break;
666 }
667skip:
668 arg->count++;
669 }
670}
671
672static struct tcf_proto_ops cls_flow_ops __read_mostly = {
673 .kind = "flow",
674 .classify = flow_classify,
675 .init = flow_init,
676 .destroy = flow_destroy,
677 .change = flow_change,
678 .delete = flow_delete,
679 .get = flow_get,
Patrick McHardye5dfb812008-01-31 18:37:42 -0800680 .dump = flow_dump,
681 .walk = flow_walk,
682 .owner = THIS_MODULE,
683};
684
685static int __init cls_flow_init(void)
686{
687 return register_tcf_proto_ops(&cls_flow_ops);
688}
689
690static void __exit cls_flow_exit(void)
691{
692 unregister_tcf_proto_ops(&cls_flow_ops);
693}
694
695module_init(cls_flow_init);
696module_exit(cls_flow_exit);
697
698MODULE_LICENSE("GPL");
699MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
700MODULE_DESCRIPTION("TC flow classifier");