]>
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> | |
d9363774 | 16 | #include <linux/workqueue.h> |
77b9900e JP |
17 | |
18 | #include <linux/if_ether.h> | |
19 | #include <linux/in6.h> | |
20 | #include <linux/ip.h> | |
a577d8f7 | 21 | #include <linux/mpls.h> |
77b9900e JP |
22 | |
23 | #include <net/sch_generic.h> | |
24 | #include <net/pkt_cls.h> | |
25 | #include <net/ip.h> | |
26 | #include <net/flow_dissector.h> | |
27 | ||
bc3103f1 AV |
28 | #include <net/dst.h> |
29 | #include <net/dst_metadata.h> | |
30 | ||
77b9900e JP |
31 | struct fl_flow_key { |
32 | int indev_ifindex; | |
42aecaa9 | 33 | struct flow_dissector_key_control control; |
bc3103f1 | 34 | struct flow_dissector_key_control enc_control; |
77b9900e JP |
35 | struct flow_dissector_key_basic basic; |
36 | struct flow_dissector_key_eth_addrs eth; | |
9399ae9a | 37 | struct flow_dissector_key_vlan vlan; |
77b9900e | 38 | union { |
c3f83241 | 39 | struct flow_dissector_key_ipv4_addrs ipv4; |
77b9900e JP |
40 | struct flow_dissector_key_ipv6_addrs ipv6; |
41 | }; | |
42 | struct flow_dissector_key_ports tp; | |
7b684884 | 43 | struct flow_dissector_key_icmp icmp; |
99d31326 | 44 | struct flow_dissector_key_arp arp; |
bc3103f1 AV |
45 | struct flow_dissector_key_keyid enc_key_id; |
46 | union { | |
47 | struct flow_dissector_key_ipv4_addrs enc_ipv4; | |
48 | struct flow_dissector_key_ipv6_addrs enc_ipv6; | |
49 | }; | |
f4d997fd | 50 | struct flow_dissector_key_ports enc_tp; |
a577d8f7 | 51 | struct flow_dissector_key_mpls mpls; |
fdfc7dd6 | 52 | struct flow_dissector_key_tcp tcp; |
4d80cc0a | 53 | struct flow_dissector_key_ip ip; |
77b9900e JP |
54 | } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ |
55 | ||
56 | struct fl_flow_mask_range { | |
57 | unsigned short int start; | |
58 | unsigned short int end; | |
59 | }; | |
60 | ||
61 | struct fl_flow_mask { | |
62 | struct fl_flow_key key; | |
63 | struct fl_flow_mask_range range; | |
64 | struct rcu_head rcu; | |
65 | }; | |
66 | ||
67 | struct cls_fl_head { | |
68 | struct rhashtable ht; | |
69 | struct fl_flow_mask mask; | |
70 | struct flow_dissector dissector; | |
77b9900e JP |
71 | bool mask_assigned; |
72 | struct list_head filters; | |
73 | struct rhashtable_params ht_params; | |
d9363774 DB |
74 | union { |
75 | struct work_struct work; | |
76 | struct rcu_head rcu; | |
77 | }; | |
c15ab236 | 78 | struct idr handle_idr; |
77b9900e JP |
79 | }; |
80 | ||
81 | struct cls_fl_filter { | |
82 | struct rhash_head ht_node; | |
83 | struct fl_flow_key mkey; | |
84 | struct tcf_exts exts; | |
85 | struct tcf_result res; | |
86 | struct fl_flow_key key; | |
87 | struct list_head list; | |
88 | u32 handle; | |
e69985c6 | 89 | u32 flags; |
0552c8af CW |
90 | union { |
91 | struct work_struct work; | |
92 | struct rcu_head rcu; | |
93 | }; | |
7091d8c7 | 94 | struct net_device *hw_dev; |
77b9900e JP |
95 | }; |
96 | ||
97 | static unsigned short int fl_mask_range(const struct fl_flow_mask *mask) | |
98 | { | |
99 | return mask->range.end - mask->range.start; | |
100 | } | |
101 | ||
102 | static void fl_mask_update_range(struct fl_flow_mask *mask) | |
103 | { | |
104 | const u8 *bytes = (const u8 *) &mask->key; | |
105 | size_t size = sizeof(mask->key); | |
106 | size_t i, first = 0, last = size - 1; | |
107 | ||
108 | for (i = 0; i < sizeof(mask->key); i++) { | |
109 | if (bytes[i]) { | |
110 | if (!first && i) | |
111 | first = i; | |
112 | last = i; | |
113 | } | |
114 | } | |
115 | mask->range.start = rounddown(first, sizeof(long)); | |
116 | mask->range.end = roundup(last + 1, sizeof(long)); | |
117 | } | |
118 | ||
119 | static void *fl_key_get_start(struct fl_flow_key *key, | |
120 | const struct fl_flow_mask *mask) | |
121 | { | |
122 | return (u8 *) key + mask->range.start; | |
123 | } | |
124 | ||
125 | static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key, | |
126 | struct fl_flow_mask *mask) | |
127 | { | |
128 | const long *lkey = fl_key_get_start(key, mask); | |
129 | const long *lmask = fl_key_get_start(&mask->key, mask); | |
130 | long *lmkey = fl_key_get_start(mkey, mask); | |
131 | int i; | |
132 | ||
133 | for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) | |
134 | *lmkey++ = *lkey++ & *lmask++; | |
135 | } | |
136 | ||
137 | static void fl_clear_masked_range(struct fl_flow_key *key, | |
138 | struct fl_flow_mask *mask) | |
139 | { | |
140 | memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask)); | |
141 | } | |
142 | ||
a3308d8f PB |
143 | static struct cls_fl_filter *fl_lookup(struct cls_fl_head *head, |
144 | struct fl_flow_key *mkey) | |
145 | { | |
146 | return rhashtable_lookup_fast(&head->ht, | |
147 | fl_key_get_start(mkey, &head->mask), | |
148 | head->ht_params); | |
149 | } | |
150 | ||
77b9900e JP |
151 | static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
152 | struct tcf_result *res) | |
153 | { | |
154 | struct cls_fl_head *head = rcu_dereference_bh(tp->root); | |
155 | struct cls_fl_filter *f; | |
156 | struct fl_flow_key skb_key; | |
157 | struct fl_flow_key skb_mkey; | |
158 | ||
e69985c6 AV |
159 | if (!atomic_read(&head->ht.nelems)) |
160 | return -1; | |
161 | ||
77b9900e | 162 | fl_clear_masked_range(&skb_key, &head->mask); |
bc3103f1 | 163 | |
77b9900e JP |
164 | skb_key.indev_ifindex = skb->skb_iif; |
165 | /* skb_flow_dissect() does not set n_proto in case an unknown protocol, | |
166 | * so do it rather here. | |
167 | */ | |
168 | skb_key.basic.n_proto = skb->protocol; | |
62b32379 | 169 | skb_flow_dissect_tunnel_info(skb, &head->dissector, &skb_key); |
cd79a238 | 170 | skb_flow_dissect(skb, &head->dissector, &skb_key, 0); |
77b9900e JP |
171 | |
172 | fl_set_masked_key(&skb_mkey, &skb_key, &head->mask); | |
173 | ||
a3308d8f | 174 | f = fl_lookup(head, &skb_mkey); |
e8eb36cd | 175 | if (f && !tc_skip_sw(f->flags)) { |
77b9900e JP |
176 | *res = f->res; |
177 | return tcf_exts_exec(skb, &f->exts, res); | |
178 | } | |
179 | return -1; | |
180 | } | |
181 | ||
182 | static int fl_init(struct tcf_proto *tp) | |
183 | { | |
184 | struct cls_fl_head *head; | |
185 | ||
186 | head = kzalloc(sizeof(*head), GFP_KERNEL); | |
187 | if (!head) | |
188 | return -ENOBUFS; | |
189 | ||
190 | INIT_LIST_HEAD_RCU(&head->filters); | |
191 | rcu_assign_pointer(tp->root, head); | |
c15ab236 | 192 | idr_init(&head->handle_idr); |
77b9900e JP |
193 | |
194 | return 0; | |
195 | } | |
196 | ||
0dadc117 CW |
197 | static void __fl_destroy_filter(struct cls_fl_filter *f) |
198 | { | |
199 | tcf_exts_destroy(&f->exts); | |
200 | tcf_exts_put_net(&f->exts); | |
201 | kfree(f); | |
202 | } | |
203 | ||
0552c8af | 204 | static void fl_destroy_filter_work(struct work_struct *work) |
77b9900e | 205 | { |
0552c8af | 206 | struct cls_fl_filter *f = container_of(work, struct cls_fl_filter, work); |
77b9900e | 207 | |
0552c8af | 208 | rtnl_lock(); |
0dadc117 | 209 | __fl_destroy_filter(f); |
0552c8af CW |
210 | rtnl_unlock(); |
211 | } | |
212 | ||
213 | static void fl_destroy_filter(struct rcu_head *head) | |
214 | { | |
215 | struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu); | |
216 | ||
217 | INIT_WORK(&f->work, fl_destroy_filter_work); | |
218 | tcf_queue_work(&f->work); | |
77b9900e JP |
219 | } |
220 | ||
3036dab6 | 221 | static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f) |
5b33f488 | 222 | { |
de4784ca | 223 | struct tc_cls_flower_offload cls_flower = {}; |
208c0f4b | 224 | struct tcf_block *block = tp->chain->block; |
5b33f488 | 225 | |
de4784ca JP |
226 | tc_cls_common_offload_init(&cls_flower.common, tp); |
227 | cls_flower.command = TC_CLSFLOWER_DESTROY; | |
228 | cls_flower.cookie = (unsigned long) f; | |
5b33f488 | 229 | |
208c0f4b | 230 | tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER, |
717503b9 | 231 | &cls_flower, false); |
5b33f488 AV |
232 | } |
233 | ||
e8eb36cd AV |
234 | static int fl_hw_replace_filter(struct tcf_proto *tp, |
235 | struct flow_dissector *dissector, | |
236 | struct fl_flow_key *mask, | |
3036dab6 | 237 | struct cls_fl_filter *f) |
5b33f488 | 238 | { |
de4784ca | 239 | struct tc_cls_flower_offload cls_flower = {}; |
208c0f4b | 240 | struct tcf_block *block = tp->chain->block; |
717503b9 | 241 | bool skip_sw = tc_skip_sw(f->flags); |
e8eb36cd | 242 | int err; |
5b33f488 | 243 | |
de4784ca JP |
244 | tc_cls_common_offload_init(&cls_flower.common, tp); |
245 | cls_flower.command = TC_CLSFLOWER_REPLACE; | |
246 | cls_flower.cookie = (unsigned long) f; | |
247 | cls_flower.dissector = dissector; | |
248 | cls_flower.mask = mask; | |
249 | cls_flower.key = &f->mkey; | |
250 | cls_flower.exts = &f->exts; | |
384c181e | 251 | cls_flower.classid = f->res.classid; |
5b33f488 | 252 | |
208c0f4b | 253 | err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER, |
717503b9 JP |
254 | &cls_flower, skip_sw); |
255 | if (err < 0) { | |
256 | fl_hw_destroy_filter(tp, f); | |
e8eb36cd | 257 | return err; |
717503b9 JP |
258 | } else if (err > 0) { |
259 | f->flags |= TCA_CLS_FLAGS_IN_HW; | |
260 | } | |
261 | ||
262 | if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW)) | |
263 | return -EINVAL; | |
264 | ||
e8eb36cd | 265 | return 0; |
5b33f488 AV |
266 | } |
267 | ||
10cbc684 AV |
268 | static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f) |
269 | { | |
de4784ca | 270 | struct tc_cls_flower_offload cls_flower = {}; |
208c0f4b | 271 | struct tcf_block *block = tp->chain->block; |
10cbc684 | 272 | |
de4784ca JP |
273 | tc_cls_common_offload_init(&cls_flower.common, tp); |
274 | cls_flower.command = TC_CLSFLOWER_STATS; | |
275 | cls_flower.cookie = (unsigned long) f; | |
276 | cls_flower.exts = &f->exts; | |
384c181e | 277 | cls_flower.classid = f->res.classid; |
10cbc684 | 278 | |
208c0f4b | 279 | tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER, |
717503b9 | 280 | &cls_flower, false); |
10cbc684 AV |
281 | } |
282 | ||
13fa876e RD |
283 | static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f) |
284 | { | |
c15ab236 CM |
285 | struct cls_fl_head *head = rtnl_dereference(tp->root); |
286 | ||
287 | idr_remove_ext(&head->handle_idr, f->handle); | |
13fa876e | 288 | list_del_rcu(&f->list); |
79685219 | 289 | if (!tc_skip_hw(f->flags)) |
3036dab6 | 290 | fl_hw_destroy_filter(tp, f); |
13fa876e | 291 | tcf_unbind_filter(tp, &f->res); |
0dadc117 CW |
292 | if (tcf_exts_get_net(&f->exts)) |
293 | call_rcu(&f->rcu, fl_destroy_filter); | |
294 | else | |
295 | __fl_destroy_filter(f); | |
13fa876e RD |
296 | } |
297 | ||
d9363774 DB |
298 | static void fl_destroy_sleepable(struct work_struct *work) |
299 | { | |
300 | struct cls_fl_head *head = container_of(work, struct cls_fl_head, | |
301 | work); | |
302 | if (head->mask_assigned) | |
303 | rhashtable_destroy(&head->ht); | |
304 | kfree(head); | |
305 | module_put(THIS_MODULE); | |
306 | } | |
307 | ||
308 | static void fl_destroy_rcu(struct rcu_head *rcu) | |
309 | { | |
310 | struct cls_fl_head *head = container_of(rcu, struct cls_fl_head, rcu); | |
311 | ||
312 | INIT_WORK(&head->work, fl_destroy_sleepable); | |
313 | schedule_work(&head->work); | |
314 | } | |
315 | ||
763dbf63 | 316 | static void fl_destroy(struct tcf_proto *tp) |
77b9900e JP |
317 | { |
318 | struct cls_fl_head *head = rtnl_dereference(tp->root); | |
319 | struct cls_fl_filter *f, *next; | |
320 | ||
13fa876e RD |
321 | list_for_each_entry_safe(f, next, &head->filters, list) |
322 | __fl_delete(tp, f); | |
c15ab236 | 323 | idr_destroy(&head->handle_idr); |
d9363774 DB |
324 | |
325 | __module_get(THIS_MODULE); | |
326 | call_rcu(&head->rcu, fl_destroy_rcu); | |
77b9900e JP |
327 | } |
328 | ||
8113c095 | 329 | static void *fl_get(struct tcf_proto *tp, u32 handle) |
77b9900e JP |
330 | { |
331 | struct cls_fl_head *head = rtnl_dereference(tp->root); | |
77b9900e | 332 | |
c15ab236 | 333 | return idr_find_ext(&head->handle_idr, handle); |
77b9900e JP |
334 | } |
335 | ||
336 | static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = { | |
337 | [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC }, | |
338 | [TCA_FLOWER_CLASSID] = { .type = NLA_U32 }, | |
339 | [TCA_FLOWER_INDEV] = { .type = NLA_STRING, | |
340 | .len = IFNAMSIZ }, | |
341 | [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN }, | |
342 | [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN }, | |
343 | [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN }, | |
344 | [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN }, | |
345 | [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 }, | |
346 | [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 }, | |
347 | [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 }, | |
348 | [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 }, | |
349 | [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 }, | |
350 | [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 }, | |
351 | [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, | |
352 | [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, | |
353 | [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) }, | |
354 | [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, | |
355 | [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 }, | |
356 | [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 }, | |
b175c3a4 JHS |
357 | [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 }, |
358 | [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 }, | |
9399ae9a HHZ |
359 | [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 }, |
360 | [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 }, | |
361 | [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 }, | |
bc3103f1 AV |
362 | [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, |
363 | [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, | |
364 | [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 }, | |
365 | [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, | |
366 | [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 }, | |
367 | [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, | |
368 | [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, | |
369 | [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, | |
370 | [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, | |
aa72d708 OG |
371 | [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 }, |
372 | [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 }, | |
373 | [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 }, | |
374 | [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 }, | |
5976c5f4 SH |
375 | [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 }, |
376 | [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 }, | |
377 | [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 }, | |
378 | [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 }, | |
f4d997fd HHZ |
379 | [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 }, |
380 | [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 }, | |
381 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 }, | |
382 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 }, | |
faa3ffce OG |
383 | [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 }, |
384 | [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 }, | |
7b684884 SH |
385 | [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 }, |
386 | [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 }, | |
387 | [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 }, | |
388 | [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 }, | |
389 | [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 }, | |
390 | [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 }, | |
391 | [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 }, | |
392 | [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 }, | |
99d31326 SH |
393 | [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 }, |
394 | [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 }, | |
395 | [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 }, | |
396 | [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 }, | |
397 | [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 }, | |
398 | [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 }, | |
399 | [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN }, | |
400 | [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN }, | |
401 | [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN }, | |
402 | [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN }, | |
a577d8f7 BL |
403 | [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 }, |
404 | [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 }, | |
405 | [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 }, | |
406 | [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 }, | |
fdfc7dd6 JP |
407 | [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 }, |
408 | [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 }, | |
4d80cc0a OG |
409 | [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 }, |
410 | [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 }, | |
411 | [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 }, | |
412 | [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 }, | |
77b9900e JP |
413 | }; |
414 | ||
415 | static void fl_set_key_val(struct nlattr **tb, | |
416 | void *val, int val_type, | |
417 | void *mask, int mask_type, int len) | |
418 | { | |
419 | if (!tb[val_type]) | |
420 | return; | |
421 | memcpy(val, nla_data(tb[val_type]), len); | |
422 | if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type]) | |
423 | memset(mask, 0xff, len); | |
424 | else | |
425 | memcpy(mask, nla_data(tb[mask_type]), len); | |
426 | } | |
427 | ||
1a7fca63 BL |
428 | static int fl_set_key_mpls(struct nlattr **tb, |
429 | struct flow_dissector_key_mpls *key_val, | |
430 | struct flow_dissector_key_mpls *key_mask) | |
a577d8f7 BL |
431 | { |
432 | if (tb[TCA_FLOWER_KEY_MPLS_TTL]) { | |
433 | key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]); | |
434 | key_mask->mpls_ttl = MPLS_TTL_MASK; | |
435 | } | |
436 | if (tb[TCA_FLOWER_KEY_MPLS_BOS]) { | |
1a7fca63 BL |
437 | u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]); |
438 | ||
439 | if (bos & ~MPLS_BOS_MASK) | |
440 | return -EINVAL; | |
441 | key_val->mpls_bos = bos; | |
a577d8f7 BL |
442 | key_mask->mpls_bos = MPLS_BOS_MASK; |
443 | } | |
444 | if (tb[TCA_FLOWER_KEY_MPLS_TC]) { | |
1a7fca63 BL |
445 | u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]); |
446 | ||
447 | if (tc & ~MPLS_TC_MASK) | |
448 | return -EINVAL; | |
449 | key_val->mpls_tc = tc; | |
a577d8f7 BL |
450 | key_mask->mpls_tc = MPLS_TC_MASK; |
451 | } | |
452 | if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) { | |
1a7fca63 BL |
453 | u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]); |
454 | ||
455 | if (label & ~MPLS_LABEL_MASK) | |
456 | return -EINVAL; | |
457 | key_val->mpls_label = label; | |
a577d8f7 BL |
458 | key_mask->mpls_label = MPLS_LABEL_MASK; |
459 | } | |
1a7fca63 | 460 | return 0; |
a577d8f7 BL |
461 | } |
462 | ||
9399ae9a HHZ |
463 | static void fl_set_key_vlan(struct nlattr **tb, |
464 | struct flow_dissector_key_vlan *key_val, | |
465 | struct flow_dissector_key_vlan *key_mask) | |
466 | { | |
467 | #define VLAN_PRIORITY_MASK 0x7 | |
468 | ||
469 | if (tb[TCA_FLOWER_KEY_VLAN_ID]) { | |
470 | key_val->vlan_id = | |
471 | nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK; | |
472 | key_mask->vlan_id = VLAN_VID_MASK; | |
473 | } | |
474 | if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) { | |
475 | key_val->vlan_priority = | |
476 | nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) & | |
477 | VLAN_PRIORITY_MASK; | |
478 | key_mask->vlan_priority = VLAN_PRIORITY_MASK; | |
479 | } | |
480 | } | |
481 | ||
faa3ffce OG |
482 | static void fl_set_key_flag(u32 flower_key, u32 flower_mask, |
483 | u32 *dissector_key, u32 *dissector_mask, | |
484 | u32 flower_flag_bit, u32 dissector_flag_bit) | |
485 | { | |
486 | if (flower_mask & flower_flag_bit) { | |
487 | *dissector_mask |= dissector_flag_bit; | |
488 | if (flower_key & flower_flag_bit) | |
489 | *dissector_key |= dissector_flag_bit; | |
490 | } | |
491 | } | |
492 | ||
d9724772 OG |
493 | static int fl_set_key_flags(struct nlattr **tb, |
494 | u32 *flags_key, u32 *flags_mask) | |
faa3ffce OG |
495 | { |
496 | u32 key, mask; | |
497 | ||
d9724772 OG |
498 | /* mask is mandatory for flags */ |
499 | if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) | |
500 | return -EINVAL; | |
faa3ffce OG |
501 | |
502 | key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS])); | |
d9724772 | 503 | mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK])); |
faa3ffce OG |
504 | |
505 | *flags_key = 0; | |
506 | *flags_mask = 0; | |
507 | ||
508 | fl_set_key_flag(key, mask, flags_key, flags_mask, | |
509 | TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); | |
d9724772 OG |
510 | |
511 | return 0; | |
faa3ffce OG |
512 | } |
513 | ||
4d80cc0a OG |
514 | static void fl_set_key_ip(struct nlattr **tb, |
515 | struct flow_dissector_key_ip *key, | |
516 | struct flow_dissector_key_ip *mask) | |
517 | { | |
518 | fl_set_key_val(tb, &key->tos, TCA_FLOWER_KEY_IP_TOS, | |
519 | &mask->tos, TCA_FLOWER_KEY_IP_TOS_MASK, | |
520 | sizeof(key->tos)); | |
521 | ||
522 | fl_set_key_val(tb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, | |
523 | &mask->ttl, TCA_FLOWER_KEY_IP_TTL_MASK, | |
524 | sizeof(key->ttl)); | |
525 | } | |
526 | ||
77b9900e JP |
527 | static int fl_set_key(struct net *net, struct nlattr **tb, |
528 | struct fl_flow_key *key, struct fl_flow_key *mask) | |
529 | { | |
9399ae9a | 530 | __be16 ethertype; |
d9724772 | 531 | int ret = 0; |
dd3aa3b5 | 532 | #ifdef CONFIG_NET_CLS_IND |
77b9900e | 533 | if (tb[TCA_FLOWER_INDEV]) { |
dd3aa3b5 | 534 | int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV]); |
77b9900e JP |
535 | if (err < 0) |
536 | return err; | |
537 | key->indev_ifindex = err; | |
538 | mask->indev_ifindex = 0xffffffff; | |
539 | } | |
dd3aa3b5 | 540 | #endif |
77b9900e JP |
541 | |
542 | fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, | |
543 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, | |
544 | sizeof(key->eth.dst)); | |
545 | fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, | |
546 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, | |
547 | sizeof(key->eth.src)); | |
66530bdf | 548 | |
0b498a52 | 549 | if (tb[TCA_FLOWER_KEY_ETH_TYPE]) { |
9399ae9a HHZ |
550 | ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]); |
551 | ||
0b498a52 AB |
552 | if (ethertype == htons(ETH_P_8021Q)) { |
553 | fl_set_key_vlan(tb, &key->vlan, &mask->vlan); | |
554 | fl_set_key_val(tb, &key->basic.n_proto, | |
555 | TCA_FLOWER_KEY_VLAN_ETH_TYPE, | |
556 | &mask->basic.n_proto, TCA_FLOWER_UNSPEC, | |
557 | sizeof(key->basic.n_proto)); | |
558 | } else { | |
559 | key->basic.n_proto = ethertype; | |
560 | mask->basic.n_proto = cpu_to_be16(~0); | |
561 | } | |
9399ae9a | 562 | } |
66530bdf | 563 | |
77b9900e JP |
564 | if (key->basic.n_proto == htons(ETH_P_IP) || |
565 | key->basic.n_proto == htons(ETH_P_IPV6)) { | |
566 | fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, | |
567 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, | |
568 | sizeof(key->basic.ip_proto)); | |
4d80cc0a | 569 | fl_set_key_ip(tb, &key->ip, &mask->ip); |
77b9900e | 570 | } |
66530bdf JHS |
571 | |
572 | if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) { | |
573 | key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; | |
970bfcd0 | 574 | mask->control.addr_type = ~0; |
77b9900e JP |
575 | fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
576 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, | |
577 | sizeof(key->ipv4.src)); | |
578 | fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, | |
579 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, | |
580 | sizeof(key->ipv4.dst)); | |
66530bdf JHS |
581 | } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) { |
582 | key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
970bfcd0 | 583 | mask->control.addr_type = ~0; |
77b9900e JP |
584 | fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
585 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, | |
586 | sizeof(key->ipv6.src)); | |
587 | fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, | |
588 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, | |
589 | sizeof(key->ipv6.dst)); | |
590 | } | |
66530bdf | 591 | |
77b9900e JP |
592 | if (key->basic.ip_proto == IPPROTO_TCP) { |
593 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, | |
aa72d708 | 594 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
77b9900e JP |
595 | sizeof(key->tp.src)); |
596 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, | |
aa72d708 | 597 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
77b9900e | 598 | sizeof(key->tp.dst)); |
fdfc7dd6 JP |
599 | fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, |
600 | &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, | |
601 | sizeof(key->tcp.flags)); | |
77b9900e JP |
602 | } else if (key->basic.ip_proto == IPPROTO_UDP) { |
603 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, | |
aa72d708 | 604 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
77b9900e JP |
605 | sizeof(key->tp.src)); |
606 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, | |
aa72d708 | 607 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
77b9900e | 608 | sizeof(key->tp.dst)); |
5976c5f4 SH |
609 | } else if (key->basic.ip_proto == IPPROTO_SCTP) { |
610 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, | |
611 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, | |
612 | sizeof(key->tp.src)); | |
613 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, | |
614 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, | |
615 | sizeof(key->tp.dst)); | |
7b684884 SH |
616 | } else if (key->basic.n_proto == htons(ETH_P_IP) && |
617 | key->basic.ip_proto == IPPROTO_ICMP) { | |
618 | fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE, | |
619 | &mask->icmp.type, | |
620 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, | |
621 | sizeof(key->icmp.type)); | |
622 | fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE, | |
623 | &mask->icmp.code, | |
624 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK, | |
625 | sizeof(key->icmp.code)); | |
626 | } else if (key->basic.n_proto == htons(ETH_P_IPV6) && | |
627 | key->basic.ip_proto == IPPROTO_ICMPV6) { | |
628 | fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE, | |
629 | &mask->icmp.type, | |
630 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, | |
631 | sizeof(key->icmp.type)); | |
040587af | 632 | fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE, |
7b684884 | 633 | &mask->icmp.code, |
040587af | 634 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK, |
7b684884 | 635 | sizeof(key->icmp.code)); |
a577d8f7 BL |
636 | } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) || |
637 | key->basic.n_proto == htons(ETH_P_MPLS_MC)) { | |
1a7fca63 BL |
638 | ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls); |
639 | if (ret) | |
640 | return ret; | |
99d31326 SH |
641 | } else if (key->basic.n_proto == htons(ETH_P_ARP) || |
642 | key->basic.n_proto == htons(ETH_P_RARP)) { | |
643 | fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP, | |
644 | &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK, | |
645 | sizeof(key->arp.sip)); | |
646 | fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP, | |
647 | &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK, | |
648 | sizeof(key->arp.tip)); | |
649 | fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP, | |
650 | &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK, | |
651 | sizeof(key->arp.op)); | |
652 | fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, | |
653 | mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, | |
654 | sizeof(key->arp.sha)); | |
655 | fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, | |
656 | mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, | |
657 | sizeof(key->arp.tha)); | |
77b9900e JP |
658 | } |
659 | ||
bc3103f1 AV |
660 | if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] || |
661 | tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) { | |
662 | key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; | |
970bfcd0 | 663 | mask->enc_control.addr_type = ~0; |
bc3103f1 AV |
664 | fl_set_key_val(tb, &key->enc_ipv4.src, |
665 | TCA_FLOWER_KEY_ENC_IPV4_SRC, | |
666 | &mask->enc_ipv4.src, | |
667 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, | |
668 | sizeof(key->enc_ipv4.src)); | |
669 | fl_set_key_val(tb, &key->enc_ipv4.dst, | |
670 | TCA_FLOWER_KEY_ENC_IPV4_DST, | |
671 | &mask->enc_ipv4.dst, | |
672 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, | |
673 | sizeof(key->enc_ipv4.dst)); | |
674 | } | |
675 | ||
676 | if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] || | |
677 | tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) { | |
678 | key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
970bfcd0 | 679 | mask->enc_control.addr_type = ~0; |
bc3103f1 AV |
680 | fl_set_key_val(tb, &key->enc_ipv6.src, |
681 | TCA_FLOWER_KEY_ENC_IPV6_SRC, | |
682 | &mask->enc_ipv6.src, | |
683 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, | |
684 | sizeof(key->enc_ipv6.src)); | |
685 | fl_set_key_val(tb, &key->enc_ipv6.dst, | |
686 | TCA_FLOWER_KEY_ENC_IPV6_DST, | |
687 | &mask->enc_ipv6.dst, | |
688 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, | |
689 | sizeof(key->enc_ipv6.dst)); | |
690 | } | |
691 | ||
692 | fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID, | |
eb523f42 | 693 | &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC, |
bc3103f1 AV |
694 | sizeof(key->enc_key_id.keyid)); |
695 | ||
f4d997fd HHZ |
696 | fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, |
697 | &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, | |
698 | sizeof(key->enc_tp.src)); | |
699 | ||
700 | fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT, | |
701 | &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, | |
702 | sizeof(key->enc_tp.dst)); | |
703 | ||
d9724772 OG |
704 | if (tb[TCA_FLOWER_KEY_FLAGS]) |
705 | ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags); | |
faa3ffce | 706 | |
d9724772 | 707 | return ret; |
77b9900e JP |
708 | } |
709 | ||
710 | static bool fl_mask_eq(struct fl_flow_mask *mask1, | |
711 | struct fl_flow_mask *mask2) | |
712 | { | |
713 | const long *lmask1 = fl_key_get_start(&mask1->key, mask1); | |
714 | const long *lmask2 = fl_key_get_start(&mask2->key, mask2); | |
715 | ||
716 | return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) && | |
717 | !memcmp(lmask1, lmask2, fl_mask_range(mask1)); | |
718 | } | |
719 | ||
720 | static const struct rhashtable_params fl_ht_params = { | |
721 | .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */ | |
722 | .head_offset = offsetof(struct cls_fl_filter, ht_node), | |
723 | .automatic_shrinking = true, | |
724 | }; | |
725 | ||
726 | static int fl_init_hashtable(struct cls_fl_head *head, | |
727 | struct fl_flow_mask *mask) | |
728 | { | |
729 | head->ht_params = fl_ht_params; | |
730 | head->ht_params.key_len = fl_mask_range(mask); | |
731 | head->ht_params.key_offset += mask->range.start; | |
732 | ||
733 | return rhashtable_init(&head->ht, &head->ht_params); | |
734 | } | |
735 | ||
736 | #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member) | |
737 | #define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member)) | |
77b9900e | 738 | |
339ba878 HHZ |
739 | #define FL_KEY_IS_MASKED(mask, member) \ |
740 | memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \ | |
741 | 0, FL_KEY_MEMBER_SIZE(member)) \ | |
77b9900e JP |
742 | |
743 | #define FL_KEY_SET(keys, cnt, id, member) \ | |
744 | do { \ | |
745 | keys[cnt].key_id = id; \ | |
746 | keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \ | |
747 | cnt++; \ | |
748 | } while(0); | |
749 | ||
339ba878 | 750 | #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \ |
77b9900e | 751 | do { \ |
339ba878 | 752 | if (FL_KEY_IS_MASKED(mask, member)) \ |
77b9900e JP |
753 | FL_KEY_SET(keys, cnt, id, member); \ |
754 | } while(0); | |
755 | ||
756 | static void fl_init_dissector(struct cls_fl_head *head, | |
757 | struct fl_flow_mask *mask) | |
758 | { | |
759 | struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX]; | |
760 | size_t cnt = 0; | |
761 | ||
42aecaa9 | 762 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control); |
77b9900e | 763 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic); |
339ba878 HHZ |
764 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, |
765 | FLOW_DISSECTOR_KEY_ETH_ADDRS, eth); | |
766 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, | |
767 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4); | |
768 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, | |
769 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6); | |
770 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, | |
771 | FLOW_DISSECTOR_KEY_PORTS, tp); | |
4d80cc0a OG |
772 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, |
773 | FLOW_DISSECTOR_KEY_IP, ip); | |
fdfc7dd6 JP |
774 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, |
775 | FLOW_DISSECTOR_KEY_TCP, tcp); | |
7b684884 SH |
776 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, |
777 | FLOW_DISSECTOR_KEY_ICMP, icmp); | |
99d31326 SH |
778 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, |
779 | FLOW_DISSECTOR_KEY_ARP, arp); | |
a577d8f7 BL |
780 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, |
781 | FLOW_DISSECTOR_KEY_MPLS, mpls); | |
9399ae9a HHZ |
782 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, |
783 | FLOW_DISSECTOR_KEY_VLAN, vlan); | |
519d1052 HHZ |
784 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, |
785 | FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id); | |
786 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, | |
787 | FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4); | |
788 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, | |
789 | FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6); | |
790 | if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) || | |
791 | FL_KEY_IS_MASKED(&mask->key, enc_ipv6)) | |
792 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL, | |
793 | enc_control); | |
f4d997fd HHZ |
794 | FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt, |
795 | FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp); | |
77b9900e JP |
796 | |
797 | skb_flow_dissector_init(&head->dissector, keys, cnt); | |
798 | } | |
799 | ||
800 | static int fl_check_assign_mask(struct cls_fl_head *head, | |
801 | struct fl_flow_mask *mask) | |
802 | { | |
803 | int err; | |
804 | ||
805 | if (head->mask_assigned) { | |
806 | if (!fl_mask_eq(&head->mask, mask)) | |
807 | return -EINVAL; | |
808 | else | |
809 | return 0; | |
810 | } | |
811 | ||
812 | /* Mask is not assigned yet. So assign it and init hashtable | |
813 | * according to that. | |
814 | */ | |
815 | err = fl_init_hashtable(head, mask); | |
816 | if (err) | |
817 | return err; | |
818 | memcpy(&head->mask, mask, sizeof(head->mask)); | |
819 | head->mask_assigned = true; | |
820 | ||
821 | fl_init_dissector(head, mask); | |
822 | ||
823 | return 0; | |
824 | } | |
825 | ||
826 | static int fl_set_parms(struct net *net, struct tcf_proto *tp, | |
827 | struct cls_fl_filter *f, struct fl_flow_mask *mask, | |
828 | unsigned long base, struct nlattr **tb, | |
829 | struct nlattr *est, bool ovr) | |
830 | { | |
77b9900e JP |
831 | int err; |
832 | ||
45507529 | 833 | err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr); |
77b9900e JP |
834 | if (err < 0) |
835 | return err; | |
836 | ||
837 | if (tb[TCA_FLOWER_CLASSID]) { | |
838 | f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]); | |
839 | tcf_bind_filter(tp, &f->res, base); | |
840 | } | |
841 | ||
842 | err = fl_set_key(net, tb, &f->key, &mask->key); | |
843 | if (err) | |
45507529 | 844 | return err; |
77b9900e JP |
845 | |
846 | fl_mask_update_range(mask); | |
847 | fl_set_masked_key(&f->mkey, &f->key, mask); | |
848 | ||
77b9900e | 849 | return 0; |
77b9900e JP |
850 | } |
851 | ||
77b9900e JP |
852 | static int fl_change(struct net *net, struct sk_buff *in_skb, |
853 | struct tcf_proto *tp, unsigned long base, | |
854 | u32 handle, struct nlattr **tca, | |
8113c095 | 855 | void **arg, bool ovr) |
77b9900e JP |
856 | { |
857 | struct cls_fl_head *head = rtnl_dereference(tp->root); | |
8113c095 | 858 | struct cls_fl_filter *fold = *arg; |
77b9900e | 859 | struct cls_fl_filter *fnew; |
39b7b6a6 | 860 | struct nlattr **tb; |
77b9900e | 861 | struct fl_flow_mask mask = {}; |
c15ab236 | 862 | unsigned long idr_index; |
77b9900e JP |
863 | int err; |
864 | ||
865 | if (!tca[TCA_OPTIONS]) | |
866 | return -EINVAL; | |
867 | ||
39b7b6a6 AB |
868 | tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); |
869 | if (!tb) | |
870 | return -ENOBUFS; | |
871 | ||
fceb6435 JB |
872 | err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS], |
873 | fl_policy, NULL); | |
77b9900e | 874 | if (err < 0) |
39b7b6a6 | 875 | goto errout_tb; |
77b9900e | 876 | |
39b7b6a6 AB |
877 | if (fold && handle && fold->handle != handle) { |
878 | err = -EINVAL; | |
879 | goto errout_tb; | |
880 | } | |
77b9900e JP |
881 | |
882 | fnew = kzalloc(sizeof(*fnew), GFP_KERNEL); | |
39b7b6a6 AB |
883 | if (!fnew) { |
884 | err = -ENOBUFS; | |
885 | goto errout_tb; | |
886 | } | |
77b9900e | 887 | |
b9a24bb7 WC |
888 | err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0); |
889 | if (err < 0) | |
890 | goto errout; | |
77b9900e JP |
891 | |
892 | if (!handle) { | |
c15ab236 CM |
893 | err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index, |
894 | 1, 0x80000000, GFP_KERNEL); | |
895 | if (err) | |
77b9900e | 896 | goto errout; |
c15ab236 CM |
897 | fnew->handle = idr_index; |
898 | } | |
899 | ||
900 | /* user specifies a handle and it doesn't exist */ | |
901 | if (handle && !fold) { | |
902 | err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index, | |
903 | handle, handle + 1, GFP_KERNEL); | |
904 | if (err) | |
905 | goto errout; | |
906 | fnew->handle = idr_index; | |
77b9900e | 907 | } |
77b9900e | 908 | |
e69985c6 AV |
909 | if (tb[TCA_FLOWER_FLAGS]) { |
910 | fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]); | |
911 | ||
912 | if (!tc_flags_valid(fnew->flags)) { | |
913 | err = -EINVAL; | |
fe2502e4 | 914 | goto errout_idr; |
e69985c6 AV |
915 | } |
916 | } | |
5b33f488 | 917 | |
77b9900e JP |
918 | err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr); |
919 | if (err) | |
fe2502e4 | 920 | goto errout_idr; |
77b9900e JP |
921 | |
922 | err = fl_check_assign_mask(head, &mask); | |
923 | if (err) | |
fe2502e4 | 924 | goto errout_idr; |
77b9900e | 925 | |
e8eb36cd | 926 | if (!tc_skip_sw(fnew->flags)) { |
a3308d8f PB |
927 | if (!fold && fl_lookup(head, &fnew->mkey)) { |
928 | err = -EEXIST; | |
fe2502e4 | 929 | goto errout_idr; |
a3308d8f PB |
930 | } |
931 | ||
e69985c6 AV |
932 | err = rhashtable_insert_fast(&head->ht, &fnew->ht_node, |
933 | head->ht_params); | |
934 | if (err) | |
fe2502e4 | 935 | goto errout_idr; |
e69985c6 | 936 | } |
5b33f488 | 937 | |
79685219 HHZ |
938 | if (!tc_skip_hw(fnew->flags)) { |
939 | err = fl_hw_replace_filter(tp, | |
940 | &head->dissector, | |
941 | &mask.key, | |
3036dab6 | 942 | fnew); |
79685219 | 943 | if (err) |
fe2502e4 | 944 | goto errout_idr; |
79685219 | 945 | } |
5b33f488 | 946 | |
55593960 OG |
947 | if (!tc_in_hw(fnew->flags)) |
948 | fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW; | |
949 | ||
5b33f488 | 950 | if (fold) { |
725cbb62 JP |
951 | if (!tc_skip_sw(fold->flags)) |
952 | rhashtable_remove_fast(&head->ht, &fold->ht_node, | |
953 | head->ht_params); | |
79685219 | 954 | if (!tc_skip_hw(fold->flags)) |
3036dab6 | 955 | fl_hw_destroy_filter(tp, fold); |
5b33f488 | 956 | } |
77b9900e | 957 | |
8113c095 | 958 | *arg = fnew; |
77b9900e JP |
959 | |
960 | if (fold) { | |
c15ab236 CM |
961 | fnew->handle = handle; |
962 | idr_replace_ext(&head->handle_idr, fnew, fnew->handle); | |
ff3532f2 | 963 | list_replace_rcu(&fold->list, &fnew->list); |
77b9900e | 964 | tcf_unbind_filter(tp, &fold->res); |
0dadc117 | 965 | tcf_exts_get_net(&fold->exts); |
77b9900e JP |
966 | call_rcu(&fold->rcu, fl_destroy_filter); |
967 | } else { | |
968 | list_add_tail_rcu(&fnew->list, &head->filters); | |
969 | } | |
970 | ||
39b7b6a6 | 971 | kfree(tb); |
77b9900e JP |
972 | return 0; |
973 | ||
fe2502e4 CW |
974 | errout_idr: |
975 | if (fnew->handle) | |
976 | idr_remove_ext(&head->handle_idr, fnew->handle); | |
77b9900e | 977 | errout: |
b9a24bb7 | 978 | tcf_exts_destroy(&fnew->exts); |
77b9900e | 979 | kfree(fnew); |
39b7b6a6 AB |
980 | errout_tb: |
981 | kfree(tb); | |
77b9900e JP |
982 | return err; |
983 | } | |
984 | ||
8113c095 | 985 | static int fl_delete(struct tcf_proto *tp, void *arg, bool *last) |
77b9900e JP |
986 | { |
987 | struct cls_fl_head *head = rtnl_dereference(tp->root); | |
8113c095 | 988 | struct cls_fl_filter *f = arg; |
77b9900e | 989 | |
725cbb62 JP |
990 | if (!tc_skip_sw(f->flags)) |
991 | rhashtable_remove_fast(&head->ht, &f->ht_node, | |
992 | head->ht_params); | |
13fa876e | 993 | __fl_delete(tp, f); |
763dbf63 | 994 | *last = list_empty(&head->filters); |
77b9900e JP |
995 | return 0; |
996 | } | |
997 | ||
998 | static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg) | |
999 | { | |
1000 | struct cls_fl_head *head = rtnl_dereference(tp->root); | |
1001 | struct cls_fl_filter *f; | |
1002 | ||
1003 | list_for_each_entry_rcu(f, &head->filters, list) { | |
1004 | if (arg->count < arg->skip) | |
1005 | goto skip; | |
8113c095 | 1006 | if (arg->fn(tp, f, arg) < 0) { |
77b9900e JP |
1007 | arg->stop = 1; |
1008 | break; | |
1009 | } | |
1010 | skip: | |
1011 | arg->count++; | |
1012 | } | |
1013 | } | |
1014 | ||
1015 | static int fl_dump_key_val(struct sk_buff *skb, | |
1016 | void *val, int val_type, | |
1017 | void *mask, int mask_type, int len) | |
1018 | { | |
1019 | int err; | |
1020 | ||
1021 | if (!memchr_inv(mask, 0, len)) | |
1022 | return 0; | |
1023 | err = nla_put(skb, val_type, len, val); | |
1024 | if (err) | |
1025 | return err; | |
1026 | if (mask_type != TCA_FLOWER_UNSPEC) { | |
1027 | err = nla_put(skb, mask_type, len, mask); | |
1028 | if (err) | |
1029 | return err; | |
1030 | } | |
1031 | return 0; | |
1032 | } | |
1033 | ||
a577d8f7 BL |
1034 | static int fl_dump_key_mpls(struct sk_buff *skb, |
1035 | struct flow_dissector_key_mpls *mpls_key, | |
1036 | struct flow_dissector_key_mpls *mpls_mask) | |
1037 | { | |
1038 | int err; | |
1039 | ||
1040 | if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask))) | |
1041 | return 0; | |
1042 | if (mpls_mask->mpls_ttl) { | |
1043 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL, | |
1044 | mpls_key->mpls_ttl); | |
1045 | if (err) | |
1046 | return err; | |
1047 | } | |
1048 | if (mpls_mask->mpls_tc) { | |
1049 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC, | |
1050 | mpls_key->mpls_tc); | |
1051 | if (err) | |
1052 | return err; | |
1053 | } | |
1054 | if (mpls_mask->mpls_label) { | |
1055 | err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL, | |
1056 | mpls_key->mpls_label); | |
1057 | if (err) | |
1058 | return err; | |
1059 | } | |
1060 | if (mpls_mask->mpls_bos) { | |
1061 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS, | |
1062 | mpls_key->mpls_bos); | |
1063 | if (err) | |
1064 | return err; | |
1065 | } | |
1066 | return 0; | |
1067 | } | |
1068 | ||
4d80cc0a OG |
1069 | static int fl_dump_key_ip(struct sk_buff *skb, |
1070 | struct flow_dissector_key_ip *key, | |
1071 | struct flow_dissector_key_ip *mask) | |
1072 | { | |
1073 | if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos, | |
1074 | TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) || | |
1075 | fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl, | |
1076 | TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl))) | |
1077 | return -1; | |
1078 | ||
1079 | return 0; | |
1080 | } | |
1081 | ||
9399ae9a HHZ |
1082 | static int fl_dump_key_vlan(struct sk_buff *skb, |
1083 | struct flow_dissector_key_vlan *vlan_key, | |
1084 | struct flow_dissector_key_vlan *vlan_mask) | |
1085 | { | |
1086 | int err; | |
1087 | ||
1088 | if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask))) | |
1089 | return 0; | |
1090 | if (vlan_mask->vlan_id) { | |
1091 | err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID, | |
1092 | vlan_key->vlan_id); | |
1093 | if (err) | |
1094 | return err; | |
1095 | } | |
1096 | if (vlan_mask->vlan_priority) { | |
1097 | err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO, | |
1098 | vlan_key->vlan_priority); | |
1099 | if (err) | |
1100 | return err; | |
1101 | } | |
1102 | return 0; | |
1103 | } | |
1104 | ||
faa3ffce OG |
1105 | static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask, |
1106 | u32 *flower_key, u32 *flower_mask, | |
1107 | u32 flower_flag_bit, u32 dissector_flag_bit) | |
1108 | { | |
1109 | if (dissector_mask & dissector_flag_bit) { | |
1110 | *flower_mask |= flower_flag_bit; | |
1111 | if (dissector_key & dissector_flag_bit) | |
1112 | *flower_key |= flower_flag_bit; | |
1113 | } | |
1114 | } | |
1115 | ||
1116 | static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask) | |
1117 | { | |
1118 | u32 key, mask; | |
1119 | __be32 _key, _mask; | |
1120 | int err; | |
1121 | ||
1122 | if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask))) | |
1123 | return 0; | |
1124 | ||
1125 | key = 0; | |
1126 | mask = 0; | |
1127 | ||
1128 | fl_get_key_flag(flags_key, flags_mask, &key, &mask, | |
1129 | TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); | |
1130 | ||
1131 | _key = cpu_to_be32(key); | |
1132 | _mask = cpu_to_be32(mask); | |
1133 | ||
1134 | err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key); | |
1135 | if (err) | |
1136 | return err; | |
1137 | ||
1138 | return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask); | |
1139 | } | |
1140 | ||
8113c095 | 1141 | static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh, |
77b9900e JP |
1142 | struct sk_buff *skb, struct tcmsg *t) |
1143 | { | |
1144 | struct cls_fl_head *head = rtnl_dereference(tp->root); | |
8113c095 | 1145 | struct cls_fl_filter *f = fh; |
77b9900e JP |
1146 | struct nlattr *nest; |
1147 | struct fl_flow_key *key, *mask; | |
1148 | ||
1149 | if (!f) | |
1150 | return skb->len; | |
1151 | ||
1152 | t->tcm_handle = f->handle; | |
1153 | ||
1154 | nest = nla_nest_start(skb, TCA_OPTIONS); | |
1155 | if (!nest) | |
1156 | goto nla_put_failure; | |
1157 | ||
1158 | if (f->res.classid && | |
1159 | nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid)) | |
1160 | goto nla_put_failure; | |
1161 | ||
1162 | key = &f->key; | |
1163 | mask = &head->mask.key; | |
1164 | ||
1165 | if (mask->indev_ifindex) { | |
1166 | struct net_device *dev; | |
1167 | ||
1168 | dev = __dev_get_by_index(net, key->indev_ifindex); | |
1169 | if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name)) | |
1170 | goto nla_put_failure; | |
1171 | } | |
1172 | ||
79685219 HHZ |
1173 | if (!tc_skip_hw(f->flags)) |
1174 | fl_hw_update_stats(tp, f); | |
10cbc684 | 1175 | |
77b9900e JP |
1176 | if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, |
1177 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, | |
1178 | sizeof(key->eth.dst)) || | |
1179 | fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, | |
1180 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, | |
1181 | sizeof(key->eth.src)) || | |
1182 | fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE, | |
1183 | &mask->basic.n_proto, TCA_FLOWER_UNSPEC, | |
1184 | sizeof(key->basic.n_proto))) | |
1185 | goto nla_put_failure; | |
9399ae9a | 1186 | |
a577d8f7 BL |
1187 | if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls)) |
1188 | goto nla_put_failure; | |
1189 | ||
9399ae9a HHZ |
1190 | if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan)) |
1191 | goto nla_put_failure; | |
1192 | ||
77b9900e JP |
1193 | if ((key->basic.n_proto == htons(ETH_P_IP) || |
1194 | key->basic.n_proto == htons(ETH_P_IPV6)) && | |
4d80cc0a | 1195 | (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, |
77b9900e | 1196 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, |
4d80cc0a OG |
1197 | sizeof(key->basic.ip_proto)) || |
1198 | fl_dump_key_ip(skb, &key->ip, &mask->ip))) | |
77b9900e JP |
1199 | goto nla_put_failure; |
1200 | ||
c3f83241 | 1201 | if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
77b9900e JP |
1202 | (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
1203 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, | |
1204 | sizeof(key->ipv4.src)) || | |
1205 | fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, | |
1206 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, | |
1207 | sizeof(key->ipv4.dst)))) | |
1208 | goto nla_put_failure; | |
c3f83241 | 1209 | else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && |
77b9900e JP |
1210 | (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
1211 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, | |
1212 | sizeof(key->ipv6.src)) || | |
1213 | fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, | |
1214 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, | |
1215 | sizeof(key->ipv6.dst)))) | |
1216 | goto nla_put_failure; | |
1217 | ||
1218 | if (key->basic.ip_proto == IPPROTO_TCP && | |
1219 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, | |
aa72d708 | 1220 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
77b9900e JP |
1221 | sizeof(key->tp.src)) || |
1222 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, | |
aa72d708 | 1223 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
fdfc7dd6 JP |
1224 | sizeof(key->tp.dst)) || |
1225 | fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, | |
1226 | &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, | |
1227 | sizeof(key->tcp.flags)))) | |
77b9900e JP |
1228 | goto nla_put_failure; |
1229 | else if (key->basic.ip_proto == IPPROTO_UDP && | |
1230 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, | |
aa72d708 | 1231 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
77b9900e JP |
1232 | sizeof(key->tp.src)) || |
1233 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, | |
aa72d708 | 1234 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
5976c5f4 SH |
1235 | sizeof(key->tp.dst)))) |
1236 | goto nla_put_failure; | |
1237 | else if (key->basic.ip_proto == IPPROTO_SCTP && | |
1238 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, | |
1239 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, | |
1240 | sizeof(key->tp.src)) || | |
1241 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, | |
1242 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, | |
77b9900e JP |
1243 | sizeof(key->tp.dst)))) |
1244 | goto nla_put_failure; | |
7b684884 SH |
1245 | else if (key->basic.n_proto == htons(ETH_P_IP) && |
1246 | key->basic.ip_proto == IPPROTO_ICMP && | |
1247 | (fl_dump_key_val(skb, &key->icmp.type, | |
1248 | TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type, | |
1249 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, | |
1250 | sizeof(key->icmp.type)) || | |
1251 | fl_dump_key_val(skb, &key->icmp.code, | |
1252 | TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code, | |
1253 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK, | |
1254 | sizeof(key->icmp.code)))) | |
1255 | goto nla_put_failure; | |
1256 | else if (key->basic.n_proto == htons(ETH_P_IPV6) && | |
1257 | key->basic.ip_proto == IPPROTO_ICMPV6 && | |
1258 | (fl_dump_key_val(skb, &key->icmp.type, | |
1259 | TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type, | |
1260 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, | |
1261 | sizeof(key->icmp.type)) || | |
1262 | fl_dump_key_val(skb, &key->icmp.code, | |
1263 | TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code, | |
1264 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK, | |
1265 | sizeof(key->icmp.code)))) | |
1266 | goto nla_put_failure; | |
99d31326 SH |
1267 | else if ((key->basic.n_proto == htons(ETH_P_ARP) || |
1268 | key->basic.n_proto == htons(ETH_P_RARP)) && | |
1269 | (fl_dump_key_val(skb, &key->arp.sip, | |
1270 | TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip, | |
1271 | TCA_FLOWER_KEY_ARP_SIP_MASK, | |
1272 | sizeof(key->arp.sip)) || | |
1273 | fl_dump_key_val(skb, &key->arp.tip, | |
1274 | TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip, | |
1275 | TCA_FLOWER_KEY_ARP_TIP_MASK, | |
1276 | sizeof(key->arp.tip)) || | |
1277 | fl_dump_key_val(skb, &key->arp.op, | |
1278 | TCA_FLOWER_KEY_ARP_OP, &mask->arp.op, | |
1279 | TCA_FLOWER_KEY_ARP_OP_MASK, | |
1280 | sizeof(key->arp.op)) || | |
1281 | fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, | |
1282 | mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, | |
1283 | sizeof(key->arp.sha)) || | |
1284 | fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, | |
1285 | mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, | |
1286 | sizeof(key->arp.tha)))) | |
1287 | goto nla_put_failure; | |
77b9900e | 1288 | |
bc3103f1 AV |
1289 | if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
1290 | (fl_dump_key_val(skb, &key->enc_ipv4.src, | |
1291 | TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src, | |
1292 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, | |
1293 | sizeof(key->enc_ipv4.src)) || | |
1294 | fl_dump_key_val(skb, &key->enc_ipv4.dst, | |
1295 | TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst, | |
1296 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, | |
1297 | sizeof(key->enc_ipv4.dst)))) | |
1298 | goto nla_put_failure; | |
1299 | else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && | |
1300 | (fl_dump_key_val(skb, &key->enc_ipv6.src, | |
1301 | TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src, | |
1302 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, | |
1303 | sizeof(key->enc_ipv6.src)) || | |
1304 | fl_dump_key_val(skb, &key->enc_ipv6.dst, | |
1305 | TCA_FLOWER_KEY_ENC_IPV6_DST, | |
1306 | &mask->enc_ipv6.dst, | |
1307 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, | |
1308 | sizeof(key->enc_ipv6.dst)))) | |
1309 | goto nla_put_failure; | |
1310 | ||
1311 | if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID, | |
eb523f42 | 1312 | &mask->enc_key_id, TCA_FLOWER_UNSPEC, |
f4d997fd HHZ |
1313 | sizeof(key->enc_key_id)) || |
1314 | fl_dump_key_val(skb, &key->enc_tp.src, | |
1315 | TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, | |
1316 | &mask->enc_tp.src, | |
1317 | TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, | |
1318 | sizeof(key->enc_tp.src)) || | |
1319 | fl_dump_key_val(skb, &key->enc_tp.dst, | |
1320 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT, | |
1321 | &mask->enc_tp.dst, | |
1322 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, | |
1323 | sizeof(key->enc_tp.dst))) | |
bc3103f1 AV |
1324 | goto nla_put_failure; |
1325 | ||
faa3ffce OG |
1326 | if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags)) |
1327 | goto nla_put_failure; | |
1328 | ||
749e6720 OG |
1329 | if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags)) |
1330 | goto nla_put_failure; | |
e69985c6 | 1331 | |
77b9900e JP |
1332 | if (tcf_exts_dump(skb, &f->exts)) |
1333 | goto nla_put_failure; | |
1334 | ||
1335 | nla_nest_end(skb, nest); | |
1336 | ||
1337 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) | |
1338 | goto nla_put_failure; | |
1339 | ||
1340 | return skb->len; | |
1341 | ||
1342 | nla_put_failure: | |
1343 | nla_nest_cancel(skb, nest); | |
1344 | return -1; | |
1345 | } | |
1346 | ||
07d79fc7 CW |
1347 | static void fl_bind_class(void *fh, u32 classid, unsigned long cl) |
1348 | { | |
1349 | struct cls_fl_filter *f = fh; | |
1350 | ||
1351 | if (f && f->res.classid == classid) | |
1352 | f->res.class = cl; | |
1353 | } | |
1354 | ||
77b9900e JP |
1355 | static struct tcf_proto_ops cls_fl_ops __read_mostly = { |
1356 | .kind = "flower", | |
1357 | .classify = fl_classify, | |
1358 | .init = fl_init, | |
1359 | .destroy = fl_destroy, | |
1360 | .get = fl_get, | |
1361 | .change = fl_change, | |
1362 | .delete = fl_delete, | |
1363 | .walk = fl_walk, | |
1364 | .dump = fl_dump, | |
07d79fc7 | 1365 | .bind_class = fl_bind_class, |
77b9900e JP |
1366 | .owner = THIS_MODULE, |
1367 | }; | |
1368 | ||
1369 | static int __init cls_fl_init(void) | |
1370 | { | |
1371 | return register_tcf_proto_ops(&cls_fl_ops); | |
1372 | } | |
1373 | ||
1374 | static void __exit cls_fl_exit(void) | |
1375 | { | |
1376 | unregister_tcf_proto_ops(&cls_fl_ops); | |
1377 | } | |
1378 | ||
1379 | module_init(cls_fl_init); | |
1380 | module_exit(cls_fl_exit); | |
1381 | ||
1382 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); | |
1383 | MODULE_DESCRIPTION("Flower classifier"); | |
1384 | MODULE_LICENSE("GPL v2"); |