Commit | Line | Data |
---|---|---|
77b9900e JP |
1 | /* |
2 | * net/sched/cls_flower.c Flower classifier | |
3 | * | |
4 | * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include <linux/kernel.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/rhashtable.h> | |
16 | ||
17 | #include <linux/if_ether.h> | |
18 | #include <linux/in6.h> | |
19 | #include <linux/ip.h> | |
20 | ||
21 | #include <net/sch_generic.h> | |
22 | #include <net/pkt_cls.h> | |
23 | #include <net/ip.h> | |
24 | #include <net/flow_dissector.h> | |
25 | ||
bc3103f1 AV |
26 | #include <net/dst.h> |
27 | #include <net/dst_metadata.h> | |
28 | ||
77b9900e JP |
29 | struct fl_flow_key { |
30 | int indev_ifindex; | |
42aecaa9 | 31 | struct flow_dissector_key_control control; |
bc3103f1 | 32 | struct flow_dissector_key_control enc_control; |
77b9900e JP |
33 | struct flow_dissector_key_basic basic; |
34 | struct flow_dissector_key_eth_addrs eth; | |
9399ae9a | 35 | struct flow_dissector_key_vlan vlan; |
77b9900e | 36 | union { |
c3f83241 | 37 | struct flow_dissector_key_ipv4_addrs ipv4; |
77b9900e JP |
38 | struct flow_dissector_key_ipv6_addrs ipv6; |
39 | }; | |
40 | struct flow_dissector_key_ports tp; | |
bc3103f1 AV |
41 | struct flow_dissector_key_keyid enc_key_id; |
42 | union { | |
43 | struct flow_dissector_key_ipv4_addrs enc_ipv4; | |
44 | struct flow_dissector_key_ipv6_addrs enc_ipv6; | |
45 | }; | |
77b9900e JP |
46 | } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ |
47 | ||
48 | struct fl_flow_mask_range { | |
49 | unsigned short int start; | |
50 | unsigned short int end; | |
51 | }; | |
52 | ||
53 | struct fl_flow_mask { | |
54 | struct fl_flow_key key; | |
55 | struct fl_flow_mask_range range; | |
56 | struct rcu_head rcu; | |
57 | }; | |
58 | ||
59 | struct cls_fl_head { | |
60 | struct rhashtable ht; | |
61 | struct fl_flow_mask mask; | |
62 | struct flow_dissector dissector; | |
63 | u32 hgen; | |
64 | bool mask_assigned; | |
65 | struct list_head filters; | |
66 | struct rhashtable_params ht_params; | |
67 | struct rcu_head rcu; | |
68 | }; | |
69 | ||
70 | struct cls_fl_filter { | |
71 | struct rhash_head ht_node; | |
72 | struct fl_flow_key mkey; | |
73 | struct tcf_exts exts; | |
74 | struct tcf_result res; | |
75 | struct fl_flow_key key; | |
76 | struct list_head list; | |
77 | u32 handle; | |
e69985c6 | 78 | u32 flags; |
77b9900e JP |
79 | struct rcu_head rcu; |
80 | }; | |
81 | ||
82 | static unsigned short int fl_mask_range(const struct fl_flow_mask *mask) | |
83 | { | |
84 | return mask->range.end - mask->range.start; | |
85 | } | |
86 | ||
87 | static void fl_mask_update_range(struct fl_flow_mask *mask) | |
88 | { | |
89 | const u8 *bytes = (const u8 *) &mask->key; | |
90 | size_t size = sizeof(mask->key); | |
91 | size_t i, first = 0, last = size - 1; | |
92 | ||
93 | for (i = 0; i < sizeof(mask->key); i++) { | |
94 | if (bytes[i]) { | |
95 | if (!first && i) | |
96 | first = i; | |
97 | last = i; | |
98 | } | |
99 | } | |
100 | mask->range.start = rounddown(first, sizeof(long)); | |
101 | mask->range.end = roundup(last + 1, sizeof(long)); | |
102 | } | |
103 | ||
104 | static void *fl_key_get_start(struct fl_flow_key *key, | |
105 | const struct fl_flow_mask *mask) | |
106 | { | |
107 | return (u8 *) key + mask->range.start; | |
108 | } | |
109 | ||
110 | static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key, | |
111 | struct fl_flow_mask *mask) | |
112 | { | |
113 | const long *lkey = fl_key_get_start(key, mask); | |
114 | const long *lmask = fl_key_get_start(&mask->key, mask); | |
115 | long *lmkey = fl_key_get_start(mkey, mask); | |
116 | int i; | |
117 | ||
118 | for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) | |
119 | *lmkey++ = *lkey++ & *lmask++; | |
120 | } | |
121 | ||
122 | static void fl_clear_masked_range(struct fl_flow_key *key, | |
123 | struct fl_flow_mask *mask) | |
124 | { | |
125 | memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask)); | |
126 | } | |
127 | ||
128 | static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp, | |
129 | struct tcf_result *res) | |
130 | { | |
131 | struct cls_fl_head *head = rcu_dereference_bh(tp->root); | |
132 | struct cls_fl_filter *f; | |
133 | struct fl_flow_key skb_key; | |
134 | struct fl_flow_key skb_mkey; | |
bc3103f1 | 135 | struct ip_tunnel_info *info; |
77b9900e | 136 | |
e69985c6 AV |
137 | if (!atomic_read(&head->ht.nelems)) |
138 | return -1; | |
139 | ||
77b9900e | 140 | fl_clear_masked_range(&skb_key, &head->mask); |
bc3103f1 AV |
141 | |
142 | info = skb_tunnel_info(skb); | |
143 | if (info) { | |
144 | struct ip_tunnel_key *key = &info->key; | |
145 | ||
146 | switch (ip_tunnel_info_af(info)) { | |
147 | case AF_INET: | |
148 | skb_key.enc_ipv4.src = key->u.ipv4.src; | |
149 | skb_key.enc_ipv4.dst = key->u.ipv4.dst; | |
150 | break; | |
151 | case AF_INET6: | |
152 | skb_key.enc_ipv6.src = key->u.ipv6.src; | |
153 | skb_key.enc_ipv6.dst = key->u.ipv6.dst; | |
154 | break; | |
155 | } | |
156 | ||
157 | skb_key.enc_key_id.keyid = tunnel_id_to_key32(key->tun_id); | |
158 | } | |
159 | ||
77b9900e JP |
160 | skb_key.indev_ifindex = skb->skb_iif; |
161 | /* skb_flow_dissect() does not set n_proto in case an unknown protocol, | |
162 | * so do it rather here. | |
163 | */ | |
164 | skb_key.basic.n_proto = skb->protocol; | |
cd79a238 | 165 | skb_flow_dissect(skb, &head->dissector, &skb_key, 0); |
77b9900e JP |
166 | |
167 | fl_set_masked_key(&skb_mkey, &skb_key, &head->mask); | |
168 | ||
169 | f = rhashtable_lookup_fast(&head->ht, | |
170 | fl_key_get_start(&skb_mkey, &head->mask), | |
171 | head->ht_params); | |
e8eb36cd | 172 | if (f && !tc_skip_sw(f->flags)) { |
77b9900e JP |
173 | *res = f->res; |
174 | return tcf_exts_exec(skb, &f->exts, res); | |
175 | } | |
176 | return -1; | |
177 | } | |
178 | ||
179 | static int fl_init(struct tcf_proto *tp) | |
180 | { | |
181 | struct cls_fl_head *head; | |
182 | ||
183 | head = kzalloc(sizeof(*head), GFP_KERNEL); | |
184 | if (!head) | |
185 | return -ENOBUFS; | |
186 | ||
187 | INIT_LIST_HEAD_RCU(&head->filters); | |
188 | rcu_assign_pointer(tp->root, head); | |
189 | ||
190 | return 0; | |
191 | } | |
192 | ||
193 | static void fl_destroy_filter(struct rcu_head *head) | |
194 | { | |
195 | struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu); | |
196 | ||
197 | tcf_exts_destroy(&f->exts); | |
198 | kfree(f); | |
199 | } | |
200 | ||
8208d21b | 201 | static void fl_hw_destroy_filter(struct tcf_proto *tp, unsigned long cookie) |
5b33f488 AV |
202 | { |
203 | struct net_device *dev = tp->q->dev_queue->dev; | |
204 | struct tc_cls_flower_offload offload = {0}; | |
205 | struct tc_to_netdev tc; | |
206 | ||
92c075db | 207 | if (!tc_should_offload(dev, tp, 0)) |
5b33f488 AV |
208 | return; |
209 | ||
210 | offload.command = TC_CLSFLOWER_DESTROY; | |
211 | offload.cookie = cookie; | |
212 | ||
213 | tc.type = TC_SETUP_CLSFLOWER; | |
214 | tc.cls_flower = &offload; | |
215 | ||
216 | dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, &tc); | |
217 | } | |
218 | ||
e8eb36cd AV |
219 | static int fl_hw_replace_filter(struct tcf_proto *tp, |
220 | struct flow_dissector *dissector, | |
221 | struct fl_flow_key *mask, | |
222 | struct fl_flow_key *key, | |
223 | struct tcf_exts *actions, | |
224 | unsigned long cookie, u32 flags) | |
5b33f488 AV |
225 | { |
226 | struct net_device *dev = tp->q->dev_queue->dev; | |
227 | struct tc_cls_flower_offload offload = {0}; | |
228 | struct tc_to_netdev tc; | |
e8eb36cd | 229 | int err; |
5b33f488 | 230 | |
92c075db | 231 | if (!tc_should_offload(dev, tp, flags)) |
e8eb36cd | 232 | return tc_skip_sw(flags) ? -EINVAL : 0; |
5b33f488 AV |
233 | |
234 | offload.command = TC_CLSFLOWER_REPLACE; | |
235 | offload.cookie = cookie; | |
236 | offload.dissector = dissector; | |
237 | offload.mask = mask; | |
238 | offload.key = key; | |
239 | offload.exts = actions; | |
240 | ||
241 | tc.type = TC_SETUP_CLSFLOWER; | |
242 | tc.cls_flower = &offload; | |
243 | ||
5a7a5555 JHS |
244 | err = dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, |
245 | &tc); | |
e8eb36cd AV |
246 | |
247 | if (tc_skip_sw(flags)) | |
248 | return err; | |
249 | ||
250 | return 0; | |
5b33f488 AV |
251 | } |
252 | ||
10cbc684 AV |
253 | static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f) |
254 | { | |
255 | struct net_device *dev = tp->q->dev_queue->dev; | |
256 | struct tc_cls_flower_offload offload = {0}; | |
257 | struct tc_to_netdev tc; | |
258 | ||
92c075db | 259 | if (!tc_should_offload(dev, tp, 0)) |
10cbc684 AV |
260 | return; |
261 | ||
262 | offload.command = TC_CLSFLOWER_STATS; | |
263 | offload.cookie = (unsigned long)f; | |
264 | offload.exts = &f->exts; | |
265 | ||
266 | tc.type = TC_SETUP_CLSFLOWER; | |
267 | tc.cls_flower = &offload; | |
268 | ||
269 | dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, &tc); | |
270 | } | |
271 | ||
13fa876e RD |
272 | static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f) |
273 | { | |
274 | list_del_rcu(&f->list); | |
275 | fl_hw_destroy_filter(tp, (unsigned long)f); | |
276 | tcf_unbind_filter(tp, &f->res); | |
277 | call_rcu(&f->rcu, fl_destroy_filter); | |
278 | } | |
279 | ||
77b9900e JP |
280 | static bool fl_destroy(struct tcf_proto *tp, bool force) |
281 | { | |
282 | struct cls_fl_head *head = rtnl_dereference(tp->root); | |
283 | struct cls_fl_filter *f, *next; | |
284 | ||
285 | if (!force && !list_empty(&head->filters)) | |
286 | return false; | |
287 | ||
13fa876e RD |
288 | list_for_each_entry_safe(f, next, &head->filters, list) |
289 | __fl_delete(tp, f); | |
77b9900e JP |
290 | RCU_INIT_POINTER(tp->root, NULL); |
291 | if (head->mask_assigned) | |
292 | rhashtable_destroy(&head->ht); | |
293 | kfree_rcu(head, rcu); | |
294 | return true; | |
295 | } | |
296 | ||
297 | static unsigned long fl_get(struct tcf_proto *tp, u32 handle) | |
298 | { | |
299 | struct cls_fl_head *head = rtnl_dereference(tp->root); | |
300 | struct cls_fl_filter *f; | |
301 | ||
302 | list_for_each_entry(f, &head->filters, list) | |
303 | if (f->handle == handle) | |
304 | return (unsigned long) f; | |
305 | return 0; | |
306 | } | |
307 | ||
308 | static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = { | |
309 | [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC }, | |
310 | [TCA_FLOWER_CLASSID] = { .type = NLA_U32 }, | |
311 | [TCA_FLOWER_INDEV] = { .type = NLA_STRING, | |
312 | .len = IFNAMSIZ }, | |
313 | [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN }, | |
314 | [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN }, | |
315 | [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN }, | |
316 | [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN }, | |
317 | [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 }, | |
318 | [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 }, | |
319 | [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 }, | |
320 | [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 }, | |
321 | [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 }, | |
322 | [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 }, | |
323 | [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, | |
324 | [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, | |
325 | [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) }, | |
326 | [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, | |
327 | [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 }, | |
328 | [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 }, | |
b175c3a4 JHS |
329 | [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 }, |
330 | [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 }, | |
9399ae9a HHZ |
331 | [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 }, |
332 | [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 }, | |
333 | [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 }, | |
bc3103f1 AV |
334 | [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, |
335 | [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, | |
336 | [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 }, | |
337 | [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, | |
338 | [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 }, | |
339 | [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, | |
340 | [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, | |
341 | [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, | |
342 | [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, | |
aa72d708 OG |
343 | [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 }, |
344 | [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 }, | |
345 | [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 }, | |
346 | [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 }, | |
5976c5f4 SH |
347 | [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 }, |
348 | [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 }, | |
349 | [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 }, | |
350 | [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 }, | |
77b9900e JP |
351 | }; |
352 | ||
353 | static void fl_set_key_val(struct nlattr **tb, | |
354 | void *val, int val_type, | |
355 | void *mask, int mask_type, int len) | |
356 | { | |
357 | if (!tb[val_type]) | |
358 | return; | |
359 | memcpy(val, nla_data(tb[val_type]), len); | |
360 | if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type]) | |
361 | memset(mask, 0xff, len); | |
362 | else | |
363 | memcpy(mask, nla_data(tb[mask_type]), len); | |
364 | } | |
365 | ||
9399ae9a HHZ |
366 | static void fl_set_key_vlan(struct nlattr **tb, |
367 | struct flow_dissector_key_vlan *key_val, | |
368 | struct flow_dissector_key_vlan *key_mask) | |
369 | { | |
370 | #define VLAN_PRIORITY_MASK 0x7 | |
371 | ||
372 | if (tb[TCA_FLOWER_KEY_VLAN_ID]) { | |
373 | key_val->vlan_id = | |
374 | nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK; | |
375 | key_mask->vlan_id = VLAN_VID_MASK; | |
376 | } | |
377 | if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) { | |
378 | key_val->vlan_priority = | |
379 | nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) & | |
380 | VLAN_PRIORITY_MASK; | |
381 | key_mask->vlan_priority = VLAN_PRIORITY_MASK; | |
382 | } | |
383 | } | |
384 | ||
77b9900e JP |
385 | static int fl_set_key(struct net *net, struct nlattr **tb, |
386 | struct fl_flow_key *key, struct fl_flow_key *mask) | |
387 | { | |
9399ae9a | 388 | __be16 ethertype; |
dd3aa3b5 | 389 | #ifdef CONFIG_NET_CLS_IND |
77b9900e | 390 | if (tb[TCA_FLOWER_INDEV]) { |
dd3aa3b5 | 391 | int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV]); |
77b9900e JP |
392 | if (err < 0) |
393 | return err; | |
394 | key->indev_ifindex = err; | |
395 | mask->indev_ifindex = 0xffffffff; | |
396 | } | |
dd3aa3b5 | 397 | #endif |
77b9900e JP |
398 | |
399 | fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, | |
400 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, | |
401 | sizeof(key->eth.dst)); | |
402 | fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, | |
403 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, | |
404 | sizeof(key->eth.src)); | |
66530bdf | 405 | |
0b498a52 | 406 | if (tb[TCA_FLOWER_KEY_ETH_TYPE]) { |
9399ae9a HHZ |
407 | ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]); |
408 | ||
0b498a52 AB |
409 | if (ethertype == htons(ETH_P_8021Q)) { |
410 | fl_set_key_vlan(tb, &key->vlan, &mask->vlan); | |
411 | fl_set_key_val(tb, &key->basic.n_proto, | |
412 | TCA_FLOWER_KEY_VLAN_ETH_TYPE, | |
413 | &mask->basic.n_proto, TCA_FLOWER_UNSPEC, | |
414 | sizeof(key->basic.n_proto)); | |
415 | } else { | |
416 | key->basic.n_proto = ethertype; | |
417 | mask->basic.n_proto = cpu_to_be16(~0); | |
418 | } | |
9399ae9a | 419 | } |
66530bdf | 420 | |
77b9900e JP |
421 | if (key->basic.n_proto == htons(ETH_P_IP) || |
422 | key->basic.n_proto == htons(ETH_P_IPV6)) { | |
423 | fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, | |
424 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, | |
425 | sizeof(key->basic.ip_proto)); | |
426 | } | |
66530bdf JHS |
427 | |
428 | if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) { | |
429 | key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; | |
77b9900e JP |
430 | fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
431 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, | |
432 | sizeof(key->ipv4.src)); | |
433 | fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, | |
434 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, | |
435 | sizeof(key->ipv4.dst)); | |
66530bdf JHS |
436 | } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) { |
437 | key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
77b9900e JP |
438 | fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
439 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, | |
440 | sizeof(key->ipv6.src)); | |
441 | fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, | |
442 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, | |
443 | sizeof(key->ipv6.dst)); | |
444 | } | |
66530bdf | 445 | |
77b9900e JP |
446 | if (key->basic.ip_proto == IPPROTO_TCP) { |
447 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, | |
aa72d708 | 448 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
77b9900e JP |
449 | sizeof(key->tp.src)); |
450 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, | |
aa72d708 | 451 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
77b9900e JP |
452 | sizeof(key->tp.dst)); |
453 | } else if (key->basic.ip_proto == IPPROTO_UDP) { | |
454 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, | |
aa72d708 | 455 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
77b9900e JP |
456 | sizeof(key->tp.src)); |
457 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, | |
aa72d708 | 458 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
77b9900e | 459 | sizeof(key->tp.dst)); |
5976c5f4 SH |
460 | } else if (key->basic.ip_proto == IPPROTO_SCTP) { |
461 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, | |
462 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, | |
463 | sizeof(key->tp.src)); | |
464 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, | |
465 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, | |
466 | sizeof(key->tp.dst)); | |
77b9900e JP |
467 | } |
468 | ||
bc3103f1 AV |
469 | if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] || |
470 | tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) { | |
471 | key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; | |
472 | fl_set_key_val(tb, &key->enc_ipv4.src, | |
473 | TCA_FLOWER_KEY_ENC_IPV4_SRC, | |
474 | &mask->enc_ipv4.src, | |
475 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, | |
476 | sizeof(key->enc_ipv4.src)); | |
477 | fl_set_key_val(tb, &key->enc_ipv4.dst, | |
478 | TCA_FLOWER_KEY_ENC_IPV4_DST, | |
479 | &mask->enc_ipv4.dst, | |
480 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, | |
481 | sizeof(key->enc_ipv4.dst)); | |
482 | } | |
483 | ||
484 | if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] || | |
485 | tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) { | |
486 | key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
487 | fl_set_key_val(tb, &key->enc_ipv6.src, | |
488 | TCA_FLOWER_KEY_ENC_IPV6_SRC, | |
489 | &mask->enc_ipv6.src, | |
490 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, | |
491 | sizeof(key->enc_ipv6.src)); | |
492 | fl_set_key_val(tb, &key->enc_ipv6.dst, | |
493 | TCA_FLOWER_KEY_ENC_IPV6_DST, | |
494 | &mask->enc_ipv6.dst, | |
495 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, | |
496 | sizeof(key->enc_ipv6.dst)); | |
497 | } | |
498 | ||
499 | fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID, | |
eb523f42 | 500 | &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC, |
bc3103f1 AV |
501 | sizeof(key->enc_key_id.keyid)); |
502 | ||
77b9900e JP |
503 | return 0; |
504 | } | |
505 | ||
506 | static bool fl_mask_eq(struct fl_flow_mask *mask1, | |
507 | struct fl_flow_mask *mask2) | |
508 | { | |
509 | const long *lmask1 = fl_key_get_start(&mask1->key, mask1); | |
510 | const long *lmask2 = fl_key_get_start(&mask2->key, mask2); | |
511 | ||
512 | return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) && | |
513 | !memcmp(lmask1, lmask2, fl_mask_range(mask1)); | |
514 | } | |
515 | ||
516 | static const struct rhashtable_params fl_ht_params = { | |
517 | .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */ | |
518 | .head_offset = offsetof(struct cls_fl_filter, ht_node), | |
519 | .automatic_shrinking = true, | |
520 | }; | |
521 | ||
522 | static int fl_init_hashtable(struct cls_fl_head *head, | |
523 | struct fl_flow_mask *mask) | |
524 | { | |
525 | head->ht_params = fl_ht_params; | |
526 | head->ht_params.key_len = fl_mask_range(mask); | |
527 | head->ht_params.key_offset += mask->range.start; | |
528 | ||
529 | return rhashtable_init(&head->ht, &head->ht_params); | |
530 | } | |
531 | ||
532 | #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member) | |
533 | #define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member)) | |
77b9900e | 534 | |
339ba878 HHZ |
535 | #define FL_KEY_IS_MASKED(mask, member) \ |
536 | memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \ | |
537 | 0, FL_KEY_MEMBER_SIZE(member)) \ | |
77b9900e JP |
538 | |
539 | #define FL_KEY_SET(keys, cnt, id, member) \ | |
540 | do { \ | |
541 | keys[cnt].key_id = id; \ | |
542 | keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \ | |
543 | cnt++; \ | |
544 | } while(0); | |
545 | ||
339ba878 | 546 | #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \ |
77b9900e | 547 | do { \ |
339ba878 | 548 | if (FL_KEY_IS_MASKED(mask, member)) \ |
77b9900e JP |
549 | FL_KEY_SET(keys, cnt, id, member); \ |
550 | } while(0); | |
551 | ||
552 | static void fl_init_dissector(struct cls_fl_head *head, | |
553 | struct fl_flow_mask *mask) | |
554 | { | |
555 | struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX]; | |
556 | size_t cnt = 0; | |
557 | ||
42aecaa9 | 558 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control); |
77b9900e | 559 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic); |
339ba878 HHZ |
560 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, |
561 | FLOW_DISSECTOR_KEY_ETH_ADDRS, eth); | |
562 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, | |
563 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4); | |
564 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, | |
565 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6); | |
566 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, | |
567 | FLOW_DISSECTOR_KEY_PORTS, tp); | |
9399ae9a HHZ |
568 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, |
569 | FLOW_DISSECTOR_KEY_VLAN, vlan); | |
519d1052 HHZ |
570 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, |
571 | FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id); | |
572 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, | |
573 | FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4); | |
574 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, | |
575 | FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6); | |
576 | if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) || | |
577 | FL_KEY_IS_MASKED(&mask->key, enc_ipv6)) | |
578 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL, | |
579 | enc_control); | |
77b9900e JP |
580 | |
581 | skb_flow_dissector_init(&head->dissector, keys, cnt); | |
582 | } | |
583 | ||
584 | static int fl_check_assign_mask(struct cls_fl_head *head, | |
585 | struct fl_flow_mask *mask) | |
586 | { | |
587 | int err; | |
588 | ||
589 | if (head->mask_assigned) { | |
590 | if (!fl_mask_eq(&head->mask, mask)) | |
591 | return -EINVAL; | |
592 | else | |
593 | return 0; | |
594 | } | |
595 | ||
596 | /* Mask is not assigned yet. So assign it and init hashtable | |
597 | * according to that. | |
598 | */ | |
599 | err = fl_init_hashtable(head, mask); | |
600 | if (err) | |
601 | return err; | |
602 | memcpy(&head->mask, mask, sizeof(head->mask)); | |
603 | head->mask_assigned = true; | |
604 | ||
605 | fl_init_dissector(head, mask); | |
606 | ||
607 | return 0; | |
608 | } | |
609 | ||
610 | static int fl_set_parms(struct net *net, struct tcf_proto *tp, | |
611 | struct cls_fl_filter *f, struct fl_flow_mask *mask, | |
612 | unsigned long base, struct nlattr **tb, | |
613 | struct nlattr *est, bool ovr) | |
614 | { | |
615 | struct tcf_exts e; | |
616 | int err; | |
617 | ||
b9a24bb7 | 618 | err = tcf_exts_init(&e, TCA_FLOWER_ACT, 0); |
77b9900e JP |
619 | if (err < 0) |
620 | return err; | |
b9a24bb7 WC |
621 | err = tcf_exts_validate(net, tp, tb, est, &e, ovr); |
622 | if (err < 0) | |
623 | goto errout; | |
77b9900e JP |
624 | |
625 | if (tb[TCA_FLOWER_CLASSID]) { | |
626 | f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]); | |
627 | tcf_bind_filter(tp, &f->res, base); | |
628 | } | |
629 | ||
630 | err = fl_set_key(net, tb, &f->key, &mask->key); | |
631 | if (err) | |
632 | goto errout; | |
633 | ||
634 | fl_mask_update_range(mask); | |
635 | fl_set_masked_key(&f->mkey, &f->key, mask); | |
636 | ||
637 | tcf_exts_change(tp, &f->exts, &e); | |
638 | ||
639 | return 0; | |
640 | errout: | |
641 | tcf_exts_destroy(&e); | |
642 | return err; | |
643 | } | |
644 | ||
645 | static u32 fl_grab_new_handle(struct tcf_proto *tp, | |
646 | struct cls_fl_head *head) | |
647 | { | |
648 | unsigned int i = 0x80000000; | |
649 | u32 handle; | |
650 | ||
651 | do { | |
652 | if (++head->hgen == 0x7FFFFFFF) | |
653 | head->hgen = 1; | |
654 | } while (--i > 0 && fl_get(tp, head->hgen)); | |
655 | ||
656 | if (unlikely(i == 0)) { | |
657 | pr_err("Insufficient number of handles\n"); | |
658 | handle = 0; | |
659 | } else { | |
660 | handle = head->hgen; | |
661 | } | |
662 | ||
663 | return handle; | |
664 | } | |
665 | ||
666 | static int fl_change(struct net *net, struct sk_buff *in_skb, | |
667 | struct tcf_proto *tp, unsigned long base, | |
668 | u32 handle, struct nlattr **tca, | |
669 | unsigned long *arg, bool ovr) | |
670 | { | |
671 | struct cls_fl_head *head = rtnl_dereference(tp->root); | |
672 | struct cls_fl_filter *fold = (struct cls_fl_filter *) *arg; | |
673 | struct cls_fl_filter *fnew; | |
674 | struct nlattr *tb[TCA_FLOWER_MAX + 1]; | |
675 | struct fl_flow_mask mask = {}; | |
676 | int err; | |
677 | ||
678 | if (!tca[TCA_OPTIONS]) | |
679 | return -EINVAL; | |
680 | ||
681 | err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS], fl_policy); | |
682 | if (err < 0) | |
683 | return err; | |
684 | ||
685 | if (fold && handle && fold->handle != handle) | |
686 | return -EINVAL; | |
687 | ||
688 | fnew = kzalloc(sizeof(*fnew), GFP_KERNEL); | |
689 | if (!fnew) | |
690 | return -ENOBUFS; | |
691 | ||
b9a24bb7 WC |
692 | err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0); |
693 | if (err < 0) | |
694 | goto errout; | |
77b9900e JP |
695 | |
696 | if (!handle) { | |
697 | handle = fl_grab_new_handle(tp, head); | |
698 | if (!handle) { | |
699 | err = -EINVAL; | |
700 | goto errout; | |
701 | } | |
702 | } | |
703 | fnew->handle = handle; | |
704 | ||
e69985c6 AV |
705 | if (tb[TCA_FLOWER_FLAGS]) { |
706 | fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]); | |
707 | ||
708 | if (!tc_flags_valid(fnew->flags)) { | |
709 | err = -EINVAL; | |
710 | goto errout; | |
711 | } | |
712 | } | |
5b33f488 | 713 | |
77b9900e JP |
714 | err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr); |
715 | if (err) | |
716 | goto errout; | |
717 | ||
718 | err = fl_check_assign_mask(head, &mask); | |
719 | if (err) | |
720 | goto errout; | |
721 | ||
e8eb36cd | 722 | if (!tc_skip_sw(fnew->flags)) { |
e69985c6 AV |
723 | err = rhashtable_insert_fast(&head->ht, &fnew->ht_node, |
724 | head->ht_params); | |
725 | if (err) | |
726 | goto errout; | |
727 | } | |
5b33f488 | 728 | |
e8eb36cd AV |
729 | err = fl_hw_replace_filter(tp, |
730 | &head->dissector, | |
731 | &mask.key, | |
732 | &fnew->key, | |
733 | &fnew->exts, | |
734 | (unsigned long)fnew, | |
735 | fnew->flags); | |
736 | if (err) | |
737 | goto errout; | |
5b33f488 AV |
738 | |
739 | if (fold) { | |
77b9900e JP |
740 | rhashtable_remove_fast(&head->ht, &fold->ht_node, |
741 | head->ht_params); | |
8208d21b | 742 | fl_hw_destroy_filter(tp, (unsigned long)fold); |
5b33f488 | 743 | } |
77b9900e JP |
744 | |
745 | *arg = (unsigned long) fnew; | |
746 | ||
747 | if (fold) { | |
ff3532f2 | 748 | list_replace_rcu(&fold->list, &fnew->list); |
77b9900e JP |
749 | tcf_unbind_filter(tp, &fold->res); |
750 | call_rcu(&fold->rcu, fl_destroy_filter); | |
751 | } else { | |
752 | list_add_tail_rcu(&fnew->list, &head->filters); | |
753 | } | |
754 | ||
755 | return 0; | |
756 | ||
757 | errout: | |
b9a24bb7 | 758 | tcf_exts_destroy(&fnew->exts); |
77b9900e JP |
759 | kfree(fnew); |
760 | return err; | |
761 | } | |
762 | ||
763 | static int fl_delete(struct tcf_proto *tp, unsigned long arg) | |
764 | { | |
765 | struct cls_fl_head *head = rtnl_dereference(tp->root); | |
766 | struct cls_fl_filter *f = (struct cls_fl_filter *) arg; | |
767 | ||
768 | rhashtable_remove_fast(&head->ht, &f->ht_node, | |
769 | head->ht_params); | |
13fa876e | 770 | __fl_delete(tp, f); |
77b9900e JP |
771 | return 0; |
772 | } | |
773 | ||
774 | static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg) | |
775 | { | |
776 | struct cls_fl_head *head = rtnl_dereference(tp->root); | |
777 | struct cls_fl_filter *f; | |
778 | ||
779 | list_for_each_entry_rcu(f, &head->filters, list) { | |
780 | if (arg->count < arg->skip) | |
781 | goto skip; | |
782 | if (arg->fn(tp, (unsigned long) f, arg) < 0) { | |
783 | arg->stop = 1; | |
784 | break; | |
785 | } | |
786 | skip: | |
787 | arg->count++; | |
788 | } | |
789 | } | |
790 | ||
791 | static int fl_dump_key_val(struct sk_buff *skb, | |
792 | void *val, int val_type, | |
793 | void *mask, int mask_type, int len) | |
794 | { | |
795 | int err; | |
796 | ||
797 | if (!memchr_inv(mask, 0, len)) | |
798 | return 0; | |
799 | err = nla_put(skb, val_type, len, val); | |
800 | if (err) | |
801 | return err; | |
802 | if (mask_type != TCA_FLOWER_UNSPEC) { | |
803 | err = nla_put(skb, mask_type, len, mask); | |
804 | if (err) | |
805 | return err; | |
806 | } | |
807 | return 0; | |
808 | } | |
809 | ||
9399ae9a HHZ |
810 | static int fl_dump_key_vlan(struct sk_buff *skb, |
811 | struct flow_dissector_key_vlan *vlan_key, | |
812 | struct flow_dissector_key_vlan *vlan_mask) | |
813 | { | |
814 | int err; | |
815 | ||
816 | if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask))) | |
817 | return 0; | |
818 | if (vlan_mask->vlan_id) { | |
819 | err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID, | |
820 | vlan_key->vlan_id); | |
821 | if (err) | |
822 | return err; | |
823 | } | |
824 | if (vlan_mask->vlan_priority) { | |
825 | err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO, | |
826 | vlan_key->vlan_priority); | |
827 | if (err) | |
828 | return err; | |
829 | } | |
830 | return 0; | |
831 | } | |
832 | ||
77b9900e JP |
833 | static int fl_dump(struct net *net, struct tcf_proto *tp, unsigned long fh, |
834 | struct sk_buff *skb, struct tcmsg *t) | |
835 | { | |
836 | struct cls_fl_head *head = rtnl_dereference(tp->root); | |
837 | struct cls_fl_filter *f = (struct cls_fl_filter *) fh; | |
838 | struct nlattr *nest; | |
839 | struct fl_flow_key *key, *mask; | |
840 | ||
841 | if (!f) | |
842 | return skb->len; | |
843 | ||
844 | t->tcm_handle = f->handle; | |
845 | ||
846 | nest = nla_nest_start(skb, TCA_OPTIONS); | |
847 | if (!nest) | |
848 | goto nla_put_failure; | |
849 | ||
850 | if (f->res.classid && | |
851 | nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid)) | |
852 | goto nla_put_failure; | |
853 | ||
854 | key = &f->key; | |
855 | mask = &head->mask.key; | |
856 | ||
857 | if (mask->indev_ifindex) { | |
858 | struct net_device *dev; | |
859 | ||
860 | dev = __dev_get_by_index(net, key->indev_ifindex); | |
861 | if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name)) | |
862 | goto nla_put_failure; | |
863 | } | |
864 | ||
10cbc684 AV |
865 | fl_hw_update_stats(tp, f); |
866 | ||
77b9900e JP |
867 | if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, |
868 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, | |
869 | sizeof(key->eth.dst)) || | |
870 | fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, | |
871 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, | |
872 | sizeof(key->eth.src)) || | |
873 | fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE, | |
874 | &mask->basic.n_proto, TCA_FLOWER_UNSPEC, | |
875 | sizeof(key->basic.n_proto))) | |
876 | goto nla_put_failure; | |
9399ae9a HHZ |
877 | |
878 | if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan)) | |
879 | goto nla_put_failure; | |
880 | ||
77b9900e JP |
881 | if ((key->basic.n_proto == htons(ETH_P_IP) || |
882 | key->basic.n_proto == htons(ETH_P_IPV6)) && | |
883 | fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, | |
884 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, | |
885 | sizeof(key->basic.ip_proto))) | |
886 | goto nla_put_failure; | |
887 | ||
c3f83241 | 888 | if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
77b9900e JP |
889 | (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
890 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, | |
891 | sizeof(key->ipv4.src)) || | |
892 | fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, | |
893 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, | |
894 | sizeof(key->ipv4.dst)))) | |
895 | goto nla_put_failure; | |
c3f83241 | 896 | else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && |
77b9900e JP |
897 | (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
898 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, | |
899 | sizeof(key->ipv6.src)) || | |
900 | fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, | |
901 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, | |
902 | sizeof(key->ipv6.dst)))) | |
903 | goto nla_put_failure; | |
904 | ||
905 | if (key->basic.ip_proto == IPPROTO_TCP && | |
906 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, | |
aa72d708 | 907 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
77b9900e JP |
908 | sizeof(key->tp.src)) || |
909 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, | |
aa72d708 | 910 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
77b9900e JP |
911 | sizeof(key->tp.dst)))) |
912 | goto nla_put_failure; | |
913 | else if (key->basic.ip_proto == IPPROTO_UDP && | |
914 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, | |
aa72d708 | 915 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
77b9900e JP |
916 | sizeof(key->tp.src)) || |
917 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, | |
aa72d708 | 918 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
5976c5f4 SH |
919 | sizeof(key->tp.dst)))) |
920 | goto nla_put_failure; | |
921 | else if (key->basic.ip_proto == IPPROTO_SCTP && | |
922 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, | |
923 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, | |
924 | sizeof(key->tp.src)) || | |
925 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, | |
926 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, | |
77b9900e JP |
927 | sizeof(key->tp.dst)))) |
928 | goto nla_put_failure; | |
929 | ||
bc3103f1 AV |
930 | if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
931 | (fl_dump_key_val(skb, &key->enc_ipv4.src, | |
932 | TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src, | |
933 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, | |
934 | sizeof(key->enc_ipv4.src)) || | |
935 | fl_dump_key_val(skb, &key->enc_ipv4.dst, | |
936 | TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst, | |
937 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, | |
938 | sizeof(key->enc_ipv4.dst)))) | |
939 | goto nla_put_failure; | |
940 | else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && | |
941 | (fl_dump_key_val(skb, &key->enc_ipv6.src, | |
942 | TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src, | |
943 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, | |
944 | sizeof(key->enc_ipv6.src)) || | |
945 | fl_dump_key_val(skb, &key->enc_ipv6.dst, | |
946 | TCA_FLOWER_KEY_ENC_IPV6_DST, | |
947 | &mask->enc_ipv6.dst, | |
948 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, | |
949 | sizeof(key->enc_ipv6.dst)))) | |
950 | goto nla_put_failure; | |
951 | ||
952 | if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID, | |
eb523f42 | 953 | &mask->enc_key_id, TCA_FLOWER_UNSPEC, |
bc3103f1 AV |
954 | sizeof(key->enc_key_id))) |
955 | goto nla_put_failure; | |
956 | ||
e69985c6 AV |
957 | nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags); |
958 | ||
77b9900e JP |
959 | if (tcf_exts_dump(skb, &f->exts)) |
960 | goto nla_put_failure; | |
961 | ||
962 | nla_nest_end(skb, nest); | |
963 | ||
964 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) | |
965 | goto nla_put_failure; | |
966 | ||
967 | return skb->len; | |
968 | ||
969 | nla_put_failure: | |
970 | nla_nest_cancel(skb, nest); | |
971 | return -1; | |
972 | } | |
973 | ||
974 | static struct tcf_proto_ops cls_fl_ops __read_mostly = { | |
975 | .kind = "flower", | |
976 | .classify = fl_classify, | |
977 | .init = fl_init, | |
978 | .destroy = fl_destroy, | |
979 | .get = fl_get, | |
980 | .change = fl_change, | |
981 | .delete = fl_delete, | |
982 | .walk = fl_walk, | |
983 | .dump = fl_dump, | |
984 | .owner = THIS_MODULE, | |
985 | }; | |
986 | ||
987 | static int __init cls_fl_init(void) | |
988 | { | |
989 | return register_tcf_proto_ops(&cls_fl_ops); | |
990 | } | |
991 | ||
992 | static void __exit cls_fl_exit(void) | |
993 | { | |
994 | unregister_tcf_proto_ops(&cls_fl_ops); | |
995 | } | |
996 | ||
997 | module_init(cls_fl_init); | |
998 | module_exit(cls_fl_exit); | |
999 | ||
1000 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); | |
1001 | MODULE_DESCRIPTION("Flower classifier"); | |
1002 | MODULE_LICENSE("GPL v2"); |