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