Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 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> |
Patrick McHardy | 9ec1381 | 2008-02-05 16:21:04 -0800 | [diff] [blame] | 22 | #include <linux/if_vlan.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 25 | |
| 26 | #include <net/pkt_cls.h> |
| 27 | #include <net/ip.h> |
| 28 | #include <net/route.h> |
Jiri Pirko | 1bd758e | 2015-05-12 14:56:07 +0200 | [diff] [blame] | 29 | #include <net/flow_dissector.h> |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 30 | |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 31 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) |
| 32 | #include <net/netfilter/nf_conntrack.h> |
| 33 | #endif |
| 34 | |
| 35 | struct flow_head { |
| 36 | struct list_head filters; |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 37 | struct rcu_head rcu; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | struct flow_filter { |
| 41 | struct list_head list; |
| 42 | struct tcf_exts exts; |
| 43 | struct tcf_ematch_tree ematches; |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 44 | struct tcf_proto *tp; |
Patrick McHardy | 72d9794 | 2008-07-14 20:36:32 -0700 | [diff] [blame] | 45 | struct timer_list perturb_timer; |
| 46 | u32 perturb_period; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 47 | 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 McHardy | 72d9794 | 2008-07-14 20:36:32 -0700 | [diff] [blame] | 58 | u32 hashrnd; |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 59 | struct rcu_head rcu; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 60 | }; |
| 61 | |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 62 | static 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 Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 69 | static u32 flow_get_src(const struct sk_buff *skb, const struct flow_keys *flow) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 70 | { |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 71 | __be32 src = flow_get_u32_src(flow); |
| 72 | |
| 73 | if (src) |
| 74 | return ntohl(src); |
| 75 | |
Changli Gao | 4b95c3d | 2010-08-04 04:48:12 +0000 | [diff] [blame] | 76 | return addr_fold(skb->sk); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 79 | static u32 flow_get_dst(const struct sk_buff *skb, const struct flow_keys *flow) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 80 | { |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 81 | __be32 dst = flow_get_u32_dst(flow); |
| 82 | |
| 83 | if (dst) |
| 84 | return ntohl(dst); |
| 85 | |
Jiri Pirko | d8b9605 | 2015-01-13 17:13:43 +0100 | [diff] [blame] | 86 | return addr_fold(skb_dst(skb)) ^ (__force u16) tc_skb_protocol(skb); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 89 | static u32 flow_get_proto(const struct sk_buff *skb, const struct flow_keys *flow) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 90 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 91 | return flow->basic.ip_proto; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 94 | static u32 flow_get_proto_src(const struct sk_buff *skb, const struct flow_keys *flow) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 95 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 96 | if (flow->ports.ports) |
Jiri Pirko | 59346af | 2015-05-12 14:56:20 +0200 | [diff] [blame] | 97 | return ntohs(flow->ports.src); |
Eric Dumazet | 859c201 | 2011-10-23 17:59:41 +0000 | [diff] [blame] | 98 | |
Changli Gao | 4b95c3d | 2010-08-04 04:48:12 +0000 | [diff] [blame] | 99 | return addr_fold(skb->sk); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 102 | static u32 flow_get_proto_dst(const struct sk_buff *skb, const struct flow_keys *flow) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 103 | { |
Jiri Pirko | 06635a3 | 2015-05-12 14:56:16 +0200 | [diff] [blame] | 104 | if (flow->ports.ports) |
Jiri Pirko | 59346af | 2015-05-12 14:56:20 +0200 | [diff] [blame] | 105 | return ntohs(flow->ports.dst); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 106 | |
Jiri Pirko | d8b9605 | 2015-01-13 17:13:43 +0100 | [diff] [blame] | 107 | return addr_fold(skb_dst(skb)) ^ (__force u16) tc_skb_protocol(skb); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static u32 flow_get_iif(const struct sk_buff *skb) |
| 111 | { |
Eric Dumazet | 8964be4 | 2009-11-20 15:35:04 -0800 | [diff] [blame] | 112 | return skb->skb_iif; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static u32 flow_get_priority(const struct sk_buff *skb) |
| 116 | { |
| 117 | return skb->priority; |
| 118 | } |
| 119 | |
| 120 | static u32 flow_get_mark(const struct sk_buff *skb) |
| 121 | { |
| 122 | return skb->mark; |
| 123 | } |
| 124 | |
| 125 | static 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 Dumazet | 859c201 | 2011-10-23 17:59:41 +0000 | [diff] [blame] | 138 | const struct nf_conn *ct = nf_ct_get(skb, &ctinfo); \ |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 139 | 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 Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 151 | static u32 flow_get_nfct_src(const struct sk_buff *skb, const struct flow_keys *flow) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 152 | { |
Jiri Pirko | d8b9605 | 2015-01-13 17:13:43 +0100 | [diff] [blame] | 153 | switch (tc_skb_protocol(skb)) { |
Arnaldo Carvalho de Melo | 6067804 | 2008-09-20 22:20:49 -0700 | [diff] [blame] | 154 | case htons(ETH_P_IP): |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 155 | return ntohl(CTTUPLE(skb, src.u3.ip)); |
Arnaldo Carvalho de Melo | 6067804 | 2008-09-20 22:20:49 -0700 | [diff] [blame] | 156 | case htons(ETH_P_IPV6): |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 157 | return ntohl(CTTUPLE(skb, src.u3.ip6[3])); |
| 158 | } |
| 159 | fallback: |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 160 | return flow_get_src(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 163 | static u32 flow_get_nfct_dst(const struct sk_buff *skb, const struct flow_keys *flow) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 164 | { |
Jiri Pirko | d8b9605 | 2015-01-13 17:13:43 +0100 | [diff] [blame] | 165 | switch (tc_skb_protocol(skb)) { |
Arnaldo Carvalho de Melo | 6067804 | 2008-09-20 22:20:49 -0700 | [diff] [blame] | 166 | case htons(ETH_P_IP): |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 167 | return ntohl(CTTUPLE(skb, dst.u3.ip)); |
Arnaldo Carvalho de Melo | 6067804 | 2008-09-20 22:20:49 -0700 | [diff] [blame] | 168 | case htons(ETH_P_IPV6): |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 169 | return ntohl(CTTUPLE(skb, dst.u3.ip6[3])); |
| 170 | } |
| 171 | fallback: |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 172 | return flow_get_dst(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 175 | static u32 flow_get_nfct_proto_src(const struct sk_buff *skb, const struct flow_keys *flow) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 176 | { |
| 177 | return ntohs(CTTUPLE(skb, src.u.all)); |
| 178 | fallback: |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 179 | return flow_get_proto_src(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 182 | static u32 flow_get_nfct_proto_dst(const struct sk_buff *skb, const struct flow_keys *flow) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 183 | { |
| 184 | return ntohs(CTTUPLE(skb, dst.u.all)); |
| 185 | fallback: |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 186 | return flow_get_proto_dst(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static u32 flow_get_rtclassid(const struct sk_buff *skb) |
| 190 | { |
Patrick McHardy | c7066f7 | 2011-01-14 13:36:42 +0100 | [diff] [blame] | 191 | #ifdef CONFIG_IP_ROUTE_CLASSID |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 192 | if (skb_dst(skb)) |
| 193 | return skb_dst(skb)->tclassid; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 194 | #endif |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static u32 flow_get_skuid(const struct sk_buff *skb) |
| 199 | { |
Eric W. Biederman | a6c6796 | 2012-05-25 13:49:36 -0600 | [diff] [blame] | 200 | 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 McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static u32 flow_get_skgid(const struct sk_buff *skb) |
| 208 | { |
Eric W. Biederman | a6c6796 | 2012-05-25 13:49:36 -0600 | [diff] [blame] | 209 | 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 McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 213 | return 0; |
| 214 | } |
| 215 | |
Patrick McHardy | 9ec1381 | 2008-02-05 16:21:04 -0800 | [diff] [blame] | 216 | static 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 Gao | 739a91e | 2010-08-21 06:23:15 +0000 | [diff] [blame] | 225 | static u32 flow_get_rxhash(struct sk_buff *skb) |
| 226 | { |
Tom Herbert | 3958afa1b | 2013-12-15 22:12:06 -0800 | [diff] [blame] | 227 | return skb_get_hash(skb); |
Changli Gao | 739a91e | 2010-08-21 06:23:15 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 230 | static u32 flow_key_get(struct sk_buff *skb, int key, struct flow_keys *flow) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 231 | { |
| 232 | switch (key) { |
| 233 | case FLOW_KEY_SRC: |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 234 | return flow_get_src(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 235 | case FLOW_KEY_DST: |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 236 | return flow_get_dst(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 237 | case FLOW_KEY_PROTO: |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 238 | return flow_get_proto(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 239 | case FLOW_KEY_PROTO_SRC: |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 240 | return flow_get_proto_src(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 241 | case FLOW_KEY_PROTO_DST: |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 242 | return flow_get_proto_dst(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 243 | 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 Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 252 | return flow_get_nfct_src(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 253 | case FLOW_KEY_NFCT_DST: |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 254 | return flow_get_nfct_dst(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 255 | case FLOW_KEY_NFCT_PROTO_SRC: |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 256 | return flow_get_nfct_proto_src(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 257 | case FLOW_KEY_NFCT_PROTO_DST: |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 258 | return flow_get_nfct_proto_dst(skb, flow); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 259 | 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 McHardy | 9ec1381 | 2008-02-05 16:21:04 -0800 | [diff] [blame] | 265 | case FLOW_KEY_VLAN_TAG: |
| 266 | return flow_get_vlan_tag(skb); |
Changli Gao | 739a91e | 2010-08-21 06:23:15 +0000 | [diff] [blame] | 267 | case FLOW_KEY_RXHASH: |
| 268 | return flow_get_rxhash(skb); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 269 | default: |
| 270 | WARN_ON(1); |
| 271 | return 0; |
| 272 | } |
| 273 | } |
| 274 | |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 275 | #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 Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 285 | static int flow_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 286 | struct tcf_result *res) |
| 287 | { |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 288 | struct flow_head *head = rcu_dereference_bh(tp->root); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 289 | struct flow_filter *f; |
| 290 | u32 keymask; |
| 291 | u32 classid; |
| 292 | unsigned int n, key; |
| 293 | int r; |
| 294 | |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 295 | list_for_each_entry_rcu(f, &head->filters, list) { |
Eric Dumazet | 3a53943 | 2011-12-14 02:30:00 +0000 | [diff] [blame] | 296 | u32 keys[FLOW_KEY_MAX + 1]; |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 297 | struct flow_keys flow_keys; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 298 | |
| 299 | if (!tcf_em_tree_match(skb, &f->ematches, NULL)) |
| 300 | continue; |
| 301 | |
| 302 | keymask = f->keymask; |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 303 | if (keymask & FLOW_KEYS_NEEDED) |
Tom Herbert | cd79a23 | 2015-09-01 09:24:27 -0700 | [diff] [blame] | 304 | skb_flow_dissect_flow_keys(skb, &flow_keys, 0); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 305 | |
| 306 | for (n = 0; n < f->nkeys; n++) { |
| 307 | key = ffs(keymask) - 1; |
| 308 | keymask &= ~(1 << key); |
Eric Dumazet | 6bd2a9a | 2011-11-28 05:24:18 +0000 | [diff] [blame] | 309 | keys[n] = flow_key_get(skb, key, &flow_keys); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (f->mode == FLOW_MODE_HASH) |
Patrick McHardy | 72d9794 | 2008-07-14 20:36:32 -0700 | [diff] [blame] | 313 | classid = jhash2(keys, f->nkeys, f->hashrnd); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 314 | 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 McHardy | 72d9794 | 2008-07-14 20:36:32 -0700 | [diff] [blame] | 334 | static 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 McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 343 | static 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 McHardy | 72d9794 | 2008-07-14 20:36:32 -0700 | [diff] [blame] | 355 | [TCA_FLOW_PERTURB] = { .type = NLA_U32 }, |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 356 | }; |
| 357 | |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 358 | static 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 Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 363 | tcf_exts_destroy(&f->exts); |
John Fastabend | 82a470f | 2014-10-05 21:27:53 -0700 | [diff] [blame] | 364 | tcf_em_tree_destroy(&f->ematches); |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 365 | kfree(f); |
| 366 | } |
| 367 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 368 | static int flow_change(struct net *net, struct sk_buff *in_skb, |
Eric W. Biederman | af4c664 | 2012-05-25 13:42:45 -0600 | [diff] [blame] | 369 | struct tcf_proto *tp, unsigned long base, |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 370 | u32 handle, struct nlattr **tca, |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 371 | unsigned long *arg, bool ovr) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 372 | { |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 373 | struct flow_head *head = rtnl_dereference(tp->root); |
| 374 | struct flow_filter *fold, *fnew; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 375 | 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 McHardy | 72d9794 | 2008-07-14 20:36:32 -0700 | [diff] [blame] | 380 | unsigned int perturb_period = 0; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 381 | 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 McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 401 | |
| 402 | nkeys = hweight32(keymask); |
| 403 | if (nkeys == 0) |
| 404 | return -EINVAL; |
Patrick McHardy | 4f25049 | 2008-02-05 16:19:59 -0800 | [diff] [blame] | 405 | |
| 406 | if (fls(keymask) - 1 > FLOW_KEY_MAX) |
| 407 | return -EOPNOTSUPP; |
Eric W. Biederman | a6c6796 | 2012-05-25 13:49:36 -0600 | [diff] [blame] | 408 | |
| 409 | if ((keymask & (FLOW_KEY_SKUID|FLOW_KEY_SKGID)) && |
Patrick McHardy | e32123e | 2013-04-17 06:46:57 +0000 | [diff] [blame] | 410 | sk_user_ns(NETLINK_CB(in_skb).sk) != &init_user_ns) |
Eric W. Biederman | a6c6796 | 2012-05-25 13:49:36 -0600 | [diff] [blame] | 411 | return -EOPNOTSUPP; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 412 | } |
| 413 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 414 | tcf_exts_init(&e, TCA_FLOW_ACT, TCA_FLOW_POLICE); |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 415 | err = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &e, ovr); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 416 | 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 Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 423 | err = -ENOBUFS; |
| 424 | fnew = kzalloc(sizeof(*fnew), GFP_KERNEL); |
| 425 | if (!fnew) |
| 426 | goto err2; |
| 427 | |
Daniel Borkmann | 32b2f4b | 2015-07-17 22:38:45 +0200 | [diff] [blame] | 428 | tcf_exts_init(&fnew->exts, TCA_FLOW_ACT, TCA_FLOW_POLICE); |
| 429 | |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 430 | fold = (struct flow_filter *)*arg; |
| 431 | if (fold) { |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 432 | err = -EINVAL; |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 433 | if (fold->handle != handle && handle) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 434 | goto err2; |
| 435 | |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 436 | /* Copy fold into fnew */ |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 437 | fnew->tp = fold->tp; |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 438 | fnew->handle = fold->handle; |
| 439 | fnew->nkeys = fold->nkeys; |
| 440 | fnew->keymask = fold->keymask; |
| 441 | fnew->mode = fold->mode; |
| 442 | fnew->mask = fold->mask; |
| 443 | fnew->xor = fold->xor; |
| 444 | fnew->rshift = fold->rshift; |
| 445 | fnew->addend = fold->addend; |
| 446 | fnew->divisor = fold->divisor; |
| 447 | fnew->baseclass = fold->baseclass; |
| 448 | fnew->hashrnd = fold->hashrnd; |
| 449 | |
| 450 | mode = fold->mode; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 451 | if (tb[TCA_FLOW_MODE]) |
| 452 | mode = nla_get_u32(tb[TCA_FLOW_MODE]); |
| 453 | if (mode != FLOW_MODE_HASH && nkeys > 1) |
| 454 | goto err2; |
Patrick McHardy | 72d9794 | 2008-07-14 20:36:32 -0700 | [diff] [blame] | 455 | |
| 456 | if (mode == FLOW_MODE_HASH) |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 457 | perturb_period = fold->perturb_period; |
Patrick McHardy | 72d9794 | 2008-07-14 20:36:32 -0700 | [diff] [blame] | 458 | if (tb[TCA_FLOW_PERTURB]) { |
| 459 | if (mode != FLOW_MODE_HASH) |
| 460 | goto err2; |
| 461 | perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ; |
| 462 | } |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 463 | } else { |
| 464 | err = -EINVAL; |
| 465 | if (!handle) |
| 466 | goto err2; |
| 467 | if (!tb[TCA_FLOW_KEYS]) |
| 468 | goto err2; |
| 469 | |
| 470 | mode = FLOW_MODE_MAP; |
| 471 | if (tb[TCA_FLOW_MODE]) |
| 472 | mode = nla_get_u32(tb[TCA_FLOW_MODE]); |
| 473 | if (mode != FLOW_MODE_HASH && nkeys > 1) |
| 474 | goto err2; |
| 475 | |
Patrick McHardy | 72d9794 | 2008-07-14 20:36:32 -0700 | [diff] [blame] | 476 | if (tb[TCA_FLOW_PERTURB]) { |
| 477 | if (mode != FLOW_MODE_HASH) |
| 478 | goto err2; |
| 479 | perturb_period = nla_get_u32(tb[TCA_FLOW_PERTURB]) * HZ; |
| 480 | } |
| 481 | |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 482 | if (TC_H_MAJ(baseclass) == 0) |
| 483 | baseclass = TC_H_MAKE(tp->q->handle, baseclass); |
| 484 | if (TC_H_MIN(baseclass) == 0) |
| 485 | baseclass = TC_H_MAKE(baseclass, 1); |
| 486 | |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 487 | fnew->handle = handle; |
| 488 | fnew->mask = ~0U; |
| 489 | fnew->tp = tp; |
| 490 | get_random_bytes(&fnew->hashrnd, 4); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 491 | } |
| 492 | |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 493 | fnew->perturb_timer.function = flow_perturbation; |
| 494 | fnew->perturb_timer.data = (unsigned long)fnew; |
| 495 | init_timer_deferrable(&fnew->perturb_timer); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 496 | |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 497 | tcf_exts_change(tp, &fnew->exts, &e); |
| 498 | tcf_em_tree_change(tp, &fnew->ematches, &t); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 499 | |
Eric Dumazet | 0287587 | 2014-10-05 18:38:35 -0700 | [diff] [blame] | 500 | netif_keep_dst(qdisc_dev(tp->q)); |
| 501 | |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 502 | if (tb[TCA_FLOW_KEYS]) { |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 503 | fnew->keymask = keymask; |
| 504 | fnew->nkeys = nkeys; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 505 | } |
| 506 | |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 507 | fnew->mode = mode; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 508 | |
| 509 | if (tb[TCA_FLOW_MASK]) |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 510 | fnew->mask = nla_get_u32(tb[TCA_FLOW_MASK]); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 511 | if (tb[TCA_FLOW_XOR]) |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 512 | fnew->xor = nla_get_u32(tb[TCA_FLOW_XOR]); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 513 | if (tb[TCA_FLOW_RSHIFT]) |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 514 | fnew->rshift = nla_get_u32(tb[TCA_FLOW_RSHIFT]); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 515 | if (tb[TCA_FLOW_ADDEND]) |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 516 | fnew->addend = nla_get_u32(tb[TCA_FLOW_ADDEND]); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 517 | |
| 518 | if (tb[TCA_FLOW_DIVISOR]) |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 519 | fnew->divisor = nla_get_u32(tb[TCA_FLOW_DIVISOR]); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 520 | if (baseclass) |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 521 | fnew->baseclass = baseclass; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 522 | |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 523 | fnew->perturb_period = perturb_period; |
Patrick McHardy | 72d9794 | 2008-07-14 20:36:32 -0700 | [diff] [blame] | 524 | if (perturb_period) |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 525 | mod_timer(&fnew->perturb_timer, jiffies + perturb_period); |
Patrick McHardy | 72d9794 | 2008-07-14 20:36:32 -0700 | [diff] [blame] | 526 | |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 527 | if (*arg == 0) |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 528 | list_add_tail_rcu(&fnew->list, &head->filters); |
| 529 | else |
Daniel Borkmann | 32b2f4b | 2015-07-17 22:38:45 +0200 | [diff] [blame] | 530 | list_replace_rcu(&fold->list, &fnew->list); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 531 | |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 532 | *arg = (unsigned long)fnew; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 533 | |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 534 | if (fold) |
| 535 | call_rcu(&fold->rcu, flow_destroy_filter); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 536 | return 0; |
| 537 | |
| 538 | err2: |
John Fastabend | 82a470f | 2014-10-05 21:27:53 -0700 | [diff] [blame] | 539 | tcf_em_tree_destroy(&t); |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 540 | kfree(fnew); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 541 | err1: |
WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 542 | tcf_exts_destroy(&e); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 543 | return err; |
| 544 | } |
| 545 | |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 546 | static int flow_delete(struct tcf_proto *tp, unsigned long arg) |
| 547 | { |
| 548 | struct flow_filter *f = (struct flow_filter *)arg; |
| 549 | |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 550 | list_del_rcu(&f->list); |
| 551 | call_rcu(&f->rcu, flow_destroy_filter); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | static int flow_init(struct tcf_proto *tp) |
| 556 | { |
| 557 | struct flow_head *head; |
| 558 | |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 559 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 560 | if (head == NULL) |
| 561 | return -ENOBUFS; |
| 562 | INIT_LIST_HEAD(&head->filters); |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 563 | rcu_assign_pointer(tp->root, head); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | |
Cong Wang | 1e052be | 2015-03-06 11:47:59 -0800 | [diff] [blame] | 567 | static bool flow_destroy(struct tcf_proto *tp, bool force) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 568 | { |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 569 | struct flow_head *head = rtnl_dereference(tp->root); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 570 | struct flow_filter *f, *next; |
| 571 | |
Cong Wang | 1e052be | 2015-03-06 11:47:59 -0800 | [diff] [blame] | 572 | if (!force && !list_empty(&head->filters)) |
| 573 | return false; |
| 574 | |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 575 | list_for_each_entry_safe(f, next, &head->filters, list) { |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 576 | list_del_rcu(&f->list); |
| 577 | call_rcu(&f->rcu, flow_destroy_filter); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 578 | } |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 579 | RCU_INIT_POINTER(tp->root, NULL); |
| 580 | kfree_rcu(head, rcu); |
Cong Wang | 1e052be | 2015-03-06 11:47:59 -0800 | [diff] [blame] | 581 | return true; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static unsigned long flow_get(struct tcf_proto *tp, u32 handle) |
| 585 | { |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 586 | struct flow_head *head = rtnl_dereference(tp->root); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 587 | struct flow_filter *f; |
| 588 | |
Jiri Pirko | 6a659cd | 2014-12-02 18:00:34 +0100 | [diff] [blame] | 589 | list_for_each_entry(f, &head->filters, list) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 590 | if (f->handle == handle) |
| 591 | return (unsigned long)f; |
| 592 | return 0; |
| 593 | } |
| 594 | |
WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 595 | static int flow_dump(struct net *net, struct tcf_proto *tp, unsigned long fh, |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 596 | struct sk_buff *skb, struct tcmsg *t) |
| 597 | { |
| 598 | struct flow_filter *f = (struct flow_filter *)fh; |
| 599 | struct nlattr *nest; |
| 600 | |
| 601 | if (f == NULL) |
| 602 | return skb->len; |
| 603 | |
| 604 | t->tcm_handle = f->handle; |
| 605 | |
| 606 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 607 | if (nest == NULL) |
| 608 | goto nla_put_failure; |
| 609 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 610 | if (nla_put_u32(skb, TCA_FLOW_KEYS, f->keymask) || |
| 611 | nla_put_u32(skb, TCA_FLOW_MODE, f->mode)) |
| 612 | goto nla_put_failure; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 613 | |
| 614 | if (f->mask != ~0 || f->xor != 0) { |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 615 | if (nla_put_u32(skb, TCA_FLOW_MASK, f->mask) || |
| 616 | nla_put_u32(skb, TCA_FLOW_XOR, f->xor)) |
| 617 | goto nla_put_failure; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 618 | } |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 619 | if (f->rshift && |
| 620 | nla_put_u32(skb, TCA_FLOW_RSHIFT, f->rshift)) |
| 621 | goto nla_put_failure; |
| 622 | if (f->addend && |
| 623 | nla_put_u32(skb, TCA_FLOW_ADDEND, f->addend)) |
| 624 | goto nla_put_failure; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 625 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 626 | if (f->divisor && |
| 627 | nla_put_u32(skb, TCA_FLOW_DIVISOR, f->divisor)) |
| 628 | goto nla_put_failure; |
| 629 | if (f->baseclass && |
| 630 | nla_put_u32(skb, TCA_FLOW_BASECLASS, f->baseclass)) |
| 631 | goto nla_put_failure; |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 632 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 633 | if (f->perturb_period && |
| 634 | nla_put_u32(skb, TCA_FLOW_PERTURB, f->perturb_period / HZ)) |
| 635 | goto nla_put_failure; |
Patrick McHardy | 72d9794 | 2008-07-14 20:36:32 -0700 | [diff] [blame] | 636 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 637 | if (tcf_exts_dump(skb, &f->exts) < 0) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 638 | goto nla_put_failure; |
Rami Rosen | 0aead54 | 2008-02-05 02:56:48 -0800 | [diff] [blame] | 639 | #ifdef CONFIG_NET_EMATCH |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 640 | if (f->ematches.hdr.nmatches && |
| 641 | tcf_em_tree_dump(skb, &f->ematches, TCA_FLOW_EMATCHES) < 0) |
| 642 | goto nla_put_failure; |
Rami Rosen | 0aead54 | 2008-02-05 02:56:48 -0800 | [diff] [blame] | 643 | #endif |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 644 | nla_nest_end(skb, nest); |
| 645 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 646 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 647 | goto nla_put_failure; |
| 648 | |
| 649 | return skb->len; |
| 650 | |
| 651 | nla_put_failure: |
Jiri Pirko | 6ea3b44 | 2014-12-09 22:23:29 +0100 | [diff] [blame] | 652 | nla_nest_cancel(skb, nest); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 653 | return -1; |
| 654 | } |
| 655 | |
| 656 | static void flow_walk(struct tcf_proto *tp, struct tcf_walker *arg) |
| 657 | { |
John Fastabend | 70da9f0 | 2014-09-12 20:06:55 -0700 | [diff] [blame] | 658 | struct flow_head *head = rtnl_dereference(tp->root); |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 659 | struct flow_filter *f; |
| 660 | |
Jiri Pirko | 6a659cd | 2014-12-02 18:00:34 +0100 | [diff] [blame] | 661 | list_for_each_entry(f, &head->filters, list) { |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 662 | if (arg->count < arg->skip) |
| 663 | goto skip; |
| 664 | if (arg->fn(tp, (unsigned long)f, arg) < 0) { |
| 665 | arg->stop = 1; |
| 666 | break; |
| 667 | } |
| 668 | skip: |
| 669 | arg->count++; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | static struct tcf_proto_ops cls_flow_ops __read_mostly = { |
| 674 | .kind = "flow", |
| 675 | .classify = flow_classify, |
| 676 | .init = flow_init, |
| 677 | .destroy = flow_destroy, |
| 678 | .change = flow_change, |
| 679 | .delete = flow_delete, |
| 680 | .get = flow_get, |
Patrick McHardy | e5dfb81 | 2008-01-31 18:37:42 -0800 | [diff] [blame] | 681 | .dump = flow_dump, |
| 682 | .walk = flow_walk, |
| 683 | .owner = THIS_MODULE, |
| 684 | }; |
| 685 | |
| 686 | static int __init cls_flow_init(void) |
| 687 | { |
| 688 | return register_tcf_proto_ops(&cls_flow_ops); |
| 689 | } |
| 690 | |
| 691 | static void __exit cls_flow_exit(void) |
| 692 | { |
| 693 | unregister_tcf_proto_ops(&cls_flow_ops); |
| 694 | } |
| 695 | |
| 696 | module_init(cls_flow_init); |
| 697 | module_exit(cls_flow_exit); |
| 698 | |
| 699 | MODULE_LICENSE("GPL"); |
| 700 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 701 | MODULE_DESCRIPTION("TC flow classifier"); |