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