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> |
06177558 | 17 | #include <linux/refcount.h> |
77b9900e JP |
18 | |
19 | #include <linux/if_ether.h> | |
20 | #include <linux/in6.h> | |
21 | #include <linux/ip.h> | |
a577d8f7 | 22 | #include <linux/mpls.h> |
77b9900e JP |
23 | |
24 | #include <net/sch_generic.h> | |
25 | #include <net/pkt_cls.h> | |
26 | #include <net/ip.h> | |
27 | #include <net/flow_dissector.h> | |
0a6e7778 | 28 | #include <net/geneve.h> |
77b9900e | 29 | |
bc3103f1 AV |
30 | #include <net/dst.h> |
31 | #include <net/dst_metadata.h> | |
32 | ||
77b9900e JP |
33 | struct fl_flow_key { |
34 | int indev_ifindex; | |
42aecaa9 | 35 | struct flow_dissector_key_control control; |
bc3103f1 | 36 | struct flow_dissector_key_control enc_control; |
77b9900e JP |
37 | struct flow_dissector_key_basic basic; |
38 | struct flow_dissector_key_eth_addrs eth; | |
9399ae9a | 39 | struct flow_dissector_key_vlan vlan; |
d64efd09 | 40 | struct flow_dissector_key_vlan cvlan; |
77b9900e | 41 | union { |
c3f83241 | 42 | struct flow_dissector_key_ipv4_addrs ipv4; |
77b9900e JP |
43 | struct flow_dissector_key_ipv6_addrs ipv6; |
44 | }; | |
45 | struct flow_dissector_key_ports tp; | |
7b684884 | 46 | struct flow_dissector_key_icmp icmp; |
99d31326 | 47 | struct flow_dissector_key_arp arp; |
bc3103f1 AV |
48 | struct flow_dissector_key_keyid enc_key_id; |
49 | union { | |
50 | struct flow_dissector_key_ipv4_addrs enc_ipv4; | |
51 | struct flow_dissector_key_ipv6_addrs enc_ipv6; | |
52 | }; | |
f4d997fd | 53 | struct flow_dissector_key_ports enc_tp; |
a577d8f7 | 54 | struct flow_dissector_key_mpls mpls; |
fdfc7dd6 | 55 | struct flow_dissector_key_tcp tcp; |
4d80cc0a | 56 | struct flow_dissector_key_ip ip; |
0e2c17b6 | 57 | struct flow_dissector_key_ip enc_ip; |
0a6e7778 | 58 | struct flow_dissector_key_enc_opts enc_opts; |
5c72299f AN |
59 | struct flow_dissector_key_ports tp_min; |
60 | struct flow_dissector_key_ports tp_max; | |
77b9900e JP |
61 | } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ |
62 | ||
63 | struct fl_flow_mask_range { | |
64 | unsigned short int start; | |
65 | unsigned short int end; | |
66 | }; | |
67 | ||
68 | struct fl_flow_mask { | |
69 | struct fl_flow_key key; | |
70 | struct fl_flow_mask_range range; | |
5c72299f | 71 | u32 flags; |
05cd271f PB |
72 | struct rhash_head ht_node; |
73 | struct rhashtable ht; | |
74 | struct rhashtable_params filter_ht_params; | |
75 | struct flow_dissector dissector; | |
76 | struct list_head filters; | |
44a5cd43 | 77 | struct rcu_work rwork; |
05cd271f | 78 | struct list_head list; |
f48ef4d5 | 79 | refcount_t refcnt; |
77b9900e JP |
80 | }; |
81 | ||
b95ec7eb JP |
82 | struct fl_flow_tmplt { |
83 | struct fl_flow_key dummy_key; | |
84 | struct fl_flow_key mask; | |
85 | struct flow_dissector dissector; | |
86 | struct tcf_chain *chain; | |
87 | }; | |
88 | ||
77b9900e JP |
89 | struct cls_fl_head { |
90 | struct rhashtable ht; | |
05cd271f | 91 | struct list_head masks; |
aaa908ff | 92 | struct rcu_work rwork; |
c15ab236 | 93 | struct idr handle_idr; |
77b9900e JP |
94 | }; |
95 | ||
96 | struct cls_fl_filter { | |
05cd271f | 97 | struct fl_flow_mask *mask; |
77b9900e JP |
98 | struct rhash_head ht_node; |
99 | struct fl_flow_key mkey; | |
100 | struct tcf_exts exts; | |
101 | struct tcf_result res; | |
102 | struct fl_flow_key key; | |
103 | struct list_head list; | |
104 | u32 handle; | |
e69985c6 | 105 | u32 flags; |
86c55361 | 106 | u32 in_hw_count; |
aaa908ff | 107 | struct rcu_work rwork; |
7091d8c7 | 108 | struct net_device *hw_dev; |
06177558 VB |
109 | /* Flower classifier is unlocked, which means that its reference counter |
110 | * can be changed concurrently without any kind of external | |
111 | * synchronization. Use atomic reference counter to be concurrency-safe. | |
112 | */ | |
113 | refcount_t refcnt; | |
b2552b8c | 114 | bool deleted; |
77b9900e JP |
115 | }; |
116 | ||
05cd271f PB |
117 | static const struct rhashtable_params mask_ht_params = { |
118 | .key_offset = offsetof(struct fl_flow_mask, key), | |
119 | .key_len = sizeof(struct fl_flow_key), | |
120 | .head_offset = offsetof(struct fl_flow_mask, ht_node), | |
121 | .automatic_shrinking = true, | |
122 | }; | |
123 | ||
77b9900e JP |
124 | static unsigned short int fl_mask_range(const struct fl_flow_mask *mask) |
125 | { | |
126 | return mask->range.end - mask->range.start; | |
127 | } | |
128 | ||
129 | static void fl_mask_update_range(struct fl_flow_mask *mask) | |
130 | { | |
131 | const u8 *bytes = (const u8 *) &mask->key; | |
132 | size_t size = sizeof(mask->key); | |
05cd271f | 133 | size_t i, first = 0, last; |
77b9900e | 134 | |
05cd271f PB |
135 | for (i = 0; i < size; i++) { |
136 | if (bytes[i]) { | |
137 | first = i; | |
138 | break; | |
139 | } | |
140 | } | |
141 | last = first; | |
142 | for (i = size - 1; i != first; i--) { | |
77b9900e | 143 | if (bytes[i]) { |
77b9900e | 144 | last = i; |
05cd271f | 145 | break; |
77b9900e JP |
146 | } |
147 | } | |
148 | mask->range.start = rounddown(first, sizeof(long)); | |
149 | mask->range.end = roundup(last + 1, sizeof(long)); | |
150 | } | |
151 | ||
152 | static void *fl_key_get_start(struct fl_flow_key *key, | |
153 | const struct fl_flow_mask *mask) | |
154 | { | |
155 | return (u8 *) key + mask->range.start; | |
156 | } | |
157 | ||
158 | static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key, | |
159 | struct fl_flow_mask *mask) | |
160 | { | |
161 | const long *lkey = fl_key_get_start(key, mask); | |
162 | const long *lmask = fl_key_get_start(&mask->key, mask); | |
163 | long *lmkey = fl_key_get_start(mkey, mask); | |
164 | int i; | |
165 | ||
166 | for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) | |
167 | *lmkey++ = *lkey++ & *lmask++; | |
168 | } | |
169 | ||
b95ec7eb JP |
170 | static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt, |
171 | struct fl_flow_mask *mask) | |
172 | { | |
173 | const long *lmask = fl_key_get_start(&mask->key, mask); | |
174 | const long *ltmplt; | |
175 | int i; | |
176 | ||
177 | if (!tmplt) | |
178 | return true; | |
179 | ltmplt = fl_key_get_start(&tmplt->mask, mask); | |
180 | for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) { | |
181 | if (~*ltmplt++ & *lmask++) | |
182 | return false; | |
183 | } | |
184 | return true; | |
185 | } | |
186 | ||
77b9900e JP |
187 | static void fl_clear_masked_range(struct fl_flow_key *key, |
188 | struct fl_flow_mask *mask) | |
189 | { | |
190 | memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask)); | |
191 | } | |
192 | ||
5c72299f AN |
193 | static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter, |
194 | struct fl_flow_key *key, | |
195 | struct fl_flow_key *mkey) | |
196 | { | |
197 | __be16 min_mask, max_mask, min_val, max_val; | |
198 | ||
199 | min_mask = htons(filter->mask->key.tp_min.dst); | |
200 | max_mask = htons(filter->mask->key.tp_max.dst); | |
201 | min_val = htons(filter->key.tp_min.dst); | |
202 | max_val = htons(filter->key.tp_max.dst); | |
203 | ||
204 | if (min_mask && max_mask) { | |
205 | if (htons(key->tp.dst) < min_val || | |
206 | htons(key->tp.dst) > max_val) | |
207 | return false; | |
208 | ||
209 | /* skb does not have min and max values */ | |
210 | mkey->tp_min.dst = filter->mkey.tp_min.dst; | |
211 | mkey->tp_max.dst = filter->mkey.tp_max.dst; | |
212 | } | |
213 | return true; | |
214 | } | |
215 | ||
216 | static bool fl_range_port_src_cmp(struct cls_fl_filter *filter, | |
217 | struct fl_flow_key *key, | |
218 | struct fl_flow_key *mkey) | |
219 | { | |
220 | __be16 min_mask, max_mask, min_val, max_val; | |
221 | ||
222 | min_mask = htons(filter->mask->key.tp_min.src); | |
223 | max_mask = htons(filter->mask->key.tp_max.src); | |
224 | min_val = htons(filter->key.tp_min.src); | |
225 | max_val = htons(filter->key.tp_max.src); | |
226 | ||
227 | if (min_mask && max_mask) { | |
228 | if (htons(key->tp.src) < min_val || | |
229 | htons(key->tp.src) > max_val) | |
230 | return false; | |
231 | ||
232 | /* skb does not have min and max values */ | |
233 | mkey->tp_min.src = filter->mkey.tp_min.src; | |
234 | mkey->tp_max.src = filter->mkey.tp_max.src; | |
235 | } | |
236 | return true; | |
237 | } | |
238 | ||
239 | static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask, | |
240 | struct fl_flow_key *mkey) | |
a3308d8f | 241 | { |
05cd271f PB |
242 | return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask), |
243 | mask->filter_ht_params); | |
a3308d8f PB |
244 | } |
245 | ||
5c72299f AN |
246 | static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask, |
247 | struct fl_flow_key *mkey, | |
248 | struct fl_flow_key *key) | |
249 | { | |
250 | struct cls_fl_filter *filter, *f; | |
251 | ||
252 | list_for_each_entry_rcu(filter, &mask->filters, list) { | |
253 | if (!fl_range_port_dst_cmp(filter, key, mkey)) | |
254 | continue; | |
255 | ||
256 | if (!fl_range_port_src_cmp(filter, key, mkey)) | |
257 | continue; | |
258 | ||
259 | f = __fl_lookup(mask, mkey); | |
260 | if (f) | |
261 | return f; | |
262 | } | |
263 | return NULL; | |
264 | } | |
265 | ||
266 | static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask, | |
267 | struct fl_flow_key *mkey, | |
268 | struct fl_flow_key *key) | |
269 | { | |
270 | if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE)) | |
271 | return fl_lookup_range(mask, mkey, key); | |
272 | ||
273 | return __fl_lookup(mask, mkey); | |
274 | } | |
275 | ||
77b9900e JP |
276 | static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
277 | struct tcf_result *res) | |
278 | { | |
279 | struct cls_fl_head *head = rcu_dereference_bh(tp->root); | |
280 | struct cls_fl_filter *f; | |
05cd271f | 281 | struct fl_flow_mask *mask; |
77b9900e JP |
282 | struct fl_flow_key skb_key; |
283 | struct fl_flow_key skb_mkey; | |
284 | ||
05cd271f PB |
285 | list_for_each_entry_rcu(mask, &head->masks, list) { |
286 | fl_clear_masked_range(&skb_key, mask); | |
bc3103f1 | 287 | |
05cd271f PB |
288 | skb_key.indev_ifindex = skb->skb_iif; |
289 | /* skb_flow_dissect() does not set n_proto in case an unknown | |
290 | * protocol, so do it rather here. | |
291 | */ | |
292 | skb_key.basic.n_proto = skb->protocol; | |
293 | skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key); | |
294 | skb_flow_dissect(skb, &mask->dissector, &skb_key, 0); | |
77b9900e | 295 | |
05cd271f | 296 | fl_set_masked_key(&skb_mkey, &skb_key, mask); |
77b9900e | 297 | |
5c72299f | 298 | f = fl_lookup(mask, &skb_mkey, &skb_key); |
05cd271f PB |
299 | if (f && !tc_skip_sw(f->flags)) { |
300 | *res = f->res; | |
301 | return tcf_exts_exec(skb, &f->exts, res); | |
302 | } | |
77b9900e JP |
303 | } |
304 | return -1; | |
305 | } | |
306 | ||
307 | static int fl_init(struct tcf_proto *tp) | |
308 | { | |
309 | struct cls_fl_head *head; | |
310 | ||
311 | head = kzalloc(sizeof(*head), GFP_KERNEL); | |
312 | if (!head) | |
313 | return -ENOBUFS; | |
314 | ||
05cd271f | 315 | INIT_LIST_HEAD_RCU(&head->masks); |
77b9900e | 316 | rcu_assign_pointer(tp->root, head); |
c15ab236 | 317 | idr_init(&head->handle_idr); |
77b9900e | 318 | |
05cd271f PB |
319 | return rhashtable_init(&head->ht, &mask_ht_params); |
320 | } | |
321 | ||
44a5cd43 PA |
322 | static void fl_mask_free(struct fl_flow_mask *mask) |
323 | { | |
f48ef4d5 | 324 | WARN_ON(!list_empty(&mask->filters)); |
44a5cd43 PA |
325 | rhashtable_destroy(&mask->ht); |
326 | kfree(mask); | |
327 | } | |
328 | ||
329 | static void fl_mask_free_work(struct work_struct *work) | |
330 | { | |
331 | struct fl_flow_mask *mask = container_of(to_rcu_work(work), | |
332 | struct fl_flow_mask, rwork); | |
333 | ||
334 | fl_mask_free(mask); | |
335 | } | |
336 | ||
05cd271f PB |
337 | static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask, |
338 | bool async) | |
339 | { | |
f48ef4d5 | 340 | if (!refcount_dec_and_test(&mask->refcnt)) |
05cd271f PB |
341 | return false; |
342 | ||
343 | rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params); | |
05cd271f PB |
344 | list_del_rcu(&mask->list); |
345 | if (async) | |
44a5cd43 | 346 | tcf_queue_work(&mask->rwork, fl_mask_free_work); |
05cd271f | 347 | else |
44a5cd43 | 348 | fl_mask_free(mask); |
05cd271f PB |
349 | |
350 | return true; | |
77b9900e JP |
351 | } |
352 | ||
0dadc117 CW |
353 | static void __fl_destroy_filter(struct cls_fl_filter *f) |
354 | { | |
355 | tcf_exts_destroy(&f->exts); | |
356 | tcf_exts_put_net(&f->exts); | |
357 | kfree(f); | |
358 | } | |
359 | ||
0552c8af | 360 | static void fl_destroy_filter_work(struct work_struct *work) |
77b9900e | 361 | { |
aaa908ff CW |
362 | struct cls_fl_filter *f = container_of(to_rcu_work(work), |
363 | struct cls_fl_filter, rwork); | |
77b9900e | 364 | |
0552c8af | 365 | rtnl_lock(); |
0dadc117 | 366 | __fl_destroy_filter(f); |
0552c8af CW |
367 | rtnl_unlock(); |
368 | } | |
369 | ||
1b0f8037 JK |
370 | static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f, |
371 | struct netlink_ext_ack *extack) | |
5b33f488 | 372 | { |
de4784ca | 373 | struct tc_cls_flower_offload cls_flower = {}; |
208c0f4b | 374 | struct tcf_block *block = tp->chain->block; |
5b33f488 | 375 | |
1b0f8037 | 376 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); |
de4784ca JP |
377 | cls_flower.command = TC_CLSFLOWER_DESTROY; |
378 | cls_flower.cookie = (unsigned long) f; | |
5b33f488 | 379 | |
aeb3fecd | 380 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false); |
caa72601 | 381 | tcf_block_offload_dec(block, &f->flags); |
5b33f488 AV |
382 | } |
383 | ||
e8eb36cd | 384 | static int fl_hw_replace_filter(struct tcf_proto *tp, |
41002038 QM |
385 | struct cls_fl_filter *f, |
386 | struct netlink_ext_ack *extack) | |
5b33f488 | 387 | { |
de4784ca | 388 | struct tc_cls_flower_offload cls_flower = {}; |
208c0f4b | 389 | struct tcf_block *block = tp->chain->block; |
717503b9 | 390 | bool skip_sw = tc_skip_sw(f->flags); |
e8eb36cd | 391 | int err; |
5b33f488 | 392 | |
e3ab786b | 393 | cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts)); |
8f256622 PNA |
394 | if (!cls_flower.rule) |
395 | return -ENOMEM; | |
396 | ||
ea205940 | 397 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); |
de4784ca JP |
398 | cls_flower.command = TC_CLSFLOWER_REPLACE; |
399 | cls_flower.cookie = (unsigned long) f; | |
8f256622 PNA |
400 | cls_flower.rule->match.dissector = &f->mask->dissector; |
401 | cls_flower.rule->match.mask = &f->mask->key; | |
402 | cls_flower.rule->match.key = &f->mkey; | |
384c181e | 403 | cls_flower.classid = f->res.classid; |
5b33f488 | 404 | |
3a7b6861 PNA |
405 | err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts); |
406 | if (err) { | |
407 | kfree(cls_flower.rule); | |
1f15bb4f VB |
408 | if (skip_sw) { |
409 | NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); | |
410 | return err; | |
411 | } | |
412 | return 0; | |
3a7b6861 PNA |
413 | } |
414 | ||
aeb3fecd | 415 | err = tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, skip_sw); |
8f256622 PNA |
416 | kfree(cls_flower.rule); |
417 | ||
717503b9 | 418 | if (err < 0) { |
1b0f8037 | 419 | fl_hw_destroy_filter(tp, f, NULL); |
e8eb36cd | 420 | return err; |
717503b9 | 421 | } else if (err > 0) { |
31533cba | 422 | f->in_hw_count = err; |
caa72601 | 423 | tcf_block_offload_inc(block, &f->flags); |
717503b9 JP |
424 | } |
425 | ||
426 | if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW)) | |
427 | return -EINVAL; | |
428 | ||
e8eb36cd | 429 | return 0; |
5b33f488 AV |
430 | } |
431 | ||
10cbc684 AV |
432 | static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f) |
433 | { | |
de4784ca | 434 | struct tc_cls_flower_offload cls_flower = {}; |
208c0f4b | 435 | struct tcf_block *block = tp->chain->block; |
10cbc684 | 436 | |
ea205940 | 437 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL); |
de4784ca JP |
438 | cls_flower.command = TC_CLSFLOWER_STATS; |
439 | cls_flower.cookie = (unsigned long) f; | |
384c181e | 440 | cls_flower.classid = f->res.classid; |
10cbc684 | 441 | |
aeb3fecd | 442 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false); |
3b1903ef PNA |
443 | |
444 | tcf_exts_stats_update(&f->exts, cls_flower.stats.bytes, | |
445 | cls_flower.stats.pkts, | |
446 | cls_flower.stats.lastused); | |
10cbc684 AV |
447 | } |
448 | ||
e474619a VB |
449 | static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp) |
450 | { | |
451 | /* Flower classifier only changes root pointer during init and destroy. | |
452 | * Users must obtain reference to tcf_proto instance before calling its | |
453 | * API, so tp->root pointer is protected from concurrent call to | |
454 | * fl_destroy() by reference counting. | |
455 | */ | |
456 | return rcu_dereference_raw(tp->root); | |
457 | } | |
458 | ||
06177558 VB |
459 | static void __fl_put(struct cls_fl_filter *f) |
460 | { | |
461 | if (!refcount_dec_and_test(&f->refcnt)) | |
462 | return; | |
463 | ||
b2552b8c VB |
464 | WARN_ON(!f->deleted); |
465 | ||
06177558 VB |
466 | if (tcf_exts_get_net(&f->exts)) |
467 | tcf_queue_work(&f->rwork, fl_destroy_filter_work); | |
468 | else | |
469 | __fl_destroy_filter(f); | |
470 | } | |
471 | ||
472 | static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle) | |
473 | { | |
474 | struct cls_fl_filter *f; | |
475 | ||
476 | rcu_read_lock(); | |
477 | f = idr_find(&head->handle_idr, handle); | |
478 | if (f && !refcount_inc_not_zero(&f->refcnt)) | |
479 | f = NULL; | |
480 | rcu_read_unlock(); | |
481 | ||
482 | return f; | |
483 | } | |
484 | ||
485 | static struct cls_fl_filter *fl_get_next_filter(struct tcf_proto *tp, | |
486 | unsigned long *handle) | |
487 | { | |
488 | struct cls_fl_head *head = fl_head_dereference(tp); | |
489 | struct cls_fl_filter *f; | |
490 | ||
491 | rcu_read_lock(); | |
492 | while ((f = idr_get_next_ul(&head->handle_idr, handle))) { | |
493 | /* don't return filters that are being deleted */ | |
494 | if (refcount_inc_not_zero(&f->refcnt)) | |
495 | break; | |
496 | ++(*handle); | |
497 | } | |
498 | rcu_read_unlock(); | |
499 | ||
500 | return f; | |
501 | } | |
502 | ||
b2552b8c VB |
503 | static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f, |
504 | bool *last, struct netlink_ext_ack *extack) | |
13fa876e | 505 | { |
e474619a | 506 | struct cls_fl_head *head = fl_head_dereference(tp); |
05cd271f | 507 | bool async = tcf_exts_get_net(&f->exts); |
c15ab236 | 508 | |
b2552b8c VB |
509 | *last = false; |
510 | ||
511 | if (f->deleted) | |
512 | return -ENOENT; | |
513 | ||
514 | f->deleted = true; | |
515 | rhashtable_remove_fast(&f->mask->ht, &f->ht_node, | |
516 | f->mask->filter_ht_params); | |
9c160941 | 517 | idr_remove(&head->handle_idr, f->handle); |
13fa876e | 518 | list_del_rcu(&f->list); |
b2552b8c | 519 | *last = fl_mask_put(head, f->mask, async); |
79685219 | 520 | if (!tc_skip_hw(f->flags)) |
1b0f8037 | 521 | fl_hw_destroy_filter(tp, f, extack); |
13fa876e | 522 | tcf_unbind_filter(tp, &f->res); |
06177558 | 523 | __fl_put(f); |
05cd271f | 524 | |
b2552b8c | 525 | return 0; |
13fa876e RD |
526 | } |
527 | ||
d9363774 DB |
528 | static void fl_destroy_sleepable(struct work_struct *work) |
529 | { | |
aaa908ff CW |
530 | struct cls_fl_head *head = container_of(to_rcu_work(work), |
531 | struct cls_fl_head, | |
532 | rwork); | |
de9dc650 PB |
533 | |
534 | rhashtable_destroy(&head->ht); | |
d9363774 DB |
535 | kfree(head); |
536 | module_put(THIS_MODULE); | |
537 | } | |
538 | ||
12db03b6 VB |
539 | static void fl_destroy(struct tcf_proto *tp, bool rtnl_held, |
540 | struct netlink_ext_ack *extack) | |
77b9900e | 541 | { |
e474619a | 542 | struct cls_fl_head *head = fl_head_dereference(tp); |
05cd271f | 543 | struct fl_flow_mask *mask, *next_mask; |
77b9900e | 544 | struct cls_fl_filter *f, *next; |
b2552b8c | 545 | bool last; |
77b9900e | 546 | |
05cd271f PB |
547 | list_for_each_entry_safe(mask, next_mask, &head->masks, list) { |
548 | list_for_each_entry_safe(f, next, &mask->filters, list) { | |
b2552b8c VB |
549 | __fl_delete(tp, f, &last, extack); |
550 | if (last) | |
05cd271f PB |
551 | break; |
552 | } | |
553 | } | |
c15ab236 | 554 | idr_destroy(&head->handle_idr); |
d9363774 DB |
555 | |
556 | __module_get(THIS_MODULE); | |
aaa908ff | 557 | tcf_queue_work(&head->rwork, fl_destroy_sleepable); |
77b9900e JP |
558 | } |
559 | ||
06177558 VB |
560 | static void fl_put(struct tcf_proto *tp, void *arg) |
561 | { | |
562 | struct cls_fl_filter *f = arg; | |
563 | ||
564 | __fl_put(f); | |
565 | } | |
566 | ||
8113c095 | 567 | static void *fl_get(struct tcf_proto *tp, u32 handle) |
77b9900e | 568 | { |
e474619a | 569 | struct cls_fl_head *head = fl_head_dereference(tp); |
77b9900e | 570 | |
06177558 | 571 | return __fl_get(head, handle); |
77b9900e JP |
572 | } |
573 | ||
574 | static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = { | |
575 | [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC }, | |
576 | [TCA_FLOWER_CLASSID] = { .type = NLA_U32 }, | |
577 | [TCA_FLOWER_INDEV] = { .type = NLA_STRING, | |
578 | .len = IFNAMSIZ }, | |
579 | [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN }, | |
580 | [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN }, | |
581 | [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN }, | |
582 | [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN }, | |
583 | [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 }, | |
584 | [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 }, | |
585 | [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 }, | |
586 | [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 }, | |
587 | [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 }, | |
588 | [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 }, | |
589 | [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, | |
590 | [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, | |
591 | [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) }, | |
592 | [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, | |
593 | [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 }, | |
594 | [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 }, | |
b175c3a4 JHS |
595 | [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 }, |
596 | [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 }, | |
9399ae9a HHZ |
597 | [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 }, |
598 | [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 }, | |
599 | [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 }, | |
bc3103f1 AV |
600 | [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, |
601 | [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, | |
602 | [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 }, | |
603 | [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, | |
604 | [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 }, | |
605 | [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, | |
606 | [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, | |
607 | [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, | |
608 | [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, | |
aa72d708 OG |
609 | [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 }, |
610 | [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 }, | |
611 | [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 }, | |
612 | [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 }, | |
5976c5f4 SH |
613 | [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 }, |
614 | [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 }, | |
615 | [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 }, | |
616 | [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 }, | |
f4d997fd HHZ |
617 | [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 }, |
618 | [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 }, | |
619 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 }, | |
620 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 }, | |
faa3ffce OG |
621 | [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 }, |
622 | [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 }, | |
7b684884 SH |
623 | [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 }, |
624 | [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 }, | |
625 | [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 }, | |
626 | [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 }, | |
627 | [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 }, | |
628 | [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 }, | |
629 | [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 }, | |
630 | [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 }, | |
99d31326 SH |
631 | [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 }, |
632 | [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 }, | |
633 | [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 }, | |
634 | [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 }, | |
635 | [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 }, | |
636 | [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 }, | |
637 | [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN }, | |
638 | [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN }, | |
639 | [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN }, | |
640 | [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN }, | |
a577d8f7 BL |
641 | [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 }, |
642 | [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 }, | |
643 | [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 }, | |
644 | [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 }, | |
fdfc7dd6 JP |
645 | [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 }, |
646 | [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 }, | |
4d80cc0a OG |
647 | [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 }, |
648 | [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 }, | |
649 | [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 }, | |
650 | [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 }, | |
d64efd09 JL |
651 | [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 }, |
652 | [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 }, | |
653 | [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 }, | |
0e2c17b6 OG |
654 | [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 }, |
655 | [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 }, | |
656 | [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 }, | |
657 | [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 }, | |
0a6e7778 PJV |
658 | [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED }, |
659 | [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED }, | |
660 | }; | |
661 | ||
662 | static const struct nla_policy | |
663 | enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = { | |
664 | [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED }, | |
665 | }; | |
666 | ||
667 | static const struct nla_policy | |
668 | geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = { | |
669 | [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 }, | |
670 | [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 }, | |
671 | [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY, | |
672 | .len = 128 }, | |
77b9900e JP |
673 | }; |
674 | ||
675 | static void fl_set_key_val(struct nlattr **tb, | |
676 | void *val, int val_type, | |
677 | void *mask, int mask_type, int len) | |
678 | { | |
679 | if (!tb[val_type]) | |
680 | return; | |
681 | memcpy(val, nla_data(tb[val_type]), len); | |
682 | if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type]) | |
683 | memset(mask, 0xff, len); | |
684 | else | |
685 | memcpy(mask, nla_data(tb[mask_type]), len); | |
686 | } | |
687 | ||
5c72299f AN |
688 | static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key, |
689 | struct fl_flow_key *mask) | |
690 | { | |
691 | fl_set_key_val(tb, &key->tp_min.dst, | |
692 | TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_min.dst, | |
693 | TCA_FLOWER_UNSPEC, sizeof(key->tp_min.dst)); | |
694 | fl_set_key_val(tb, &key->tp_max.dst, | |
695 | TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_max.dst, | |
696 | TCA_FLOWER_UNSPEC, sizeof(key->tp_max.dst)); | |
697 | fl_set_key_val(tb, &key->tp_min.src, | |
698 | TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_min.src, | |
699 | TCA_FLOWER_UNSPEC, sizeof(key->tp_min.src)); | |
700 | fl_set_key_val(tb, &key->tp_max.src, | |
701 | TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_max.src, | |
702 | TCA_FLOWER_UNSPEC, sizeof(key->tp_max.src)); | |
703 | ||
704 | if ((mask->tp_min.dst && mask->tp_max.dst && | |
705 | htons(key->tp_max.dst) <= htons(key->tp_min.dst)) || | |
706 | (mask->tp_min.src && mask->tp_max.src && | |
707 | htons(key->tp_max.src) <= htons(key->tp_min.src))) | |
708 | return -EINVAL; | |
709 | ||
710 | return 0; | |
711 | } | |
712 | ||
1a7fca63 BL |
713 | static int fl_set_key_mpls(struct nlattr **tb, |
714 | struct flow_dissector_key_mpls *key_val, | |
715 | struct flow_dissector_key_mpls *key_mask) | |
a577d8f7 BL |
716 | { |
717 | if (tb[TCA_FLOWER_KEY_MPLS_TTL]) { | |
718 | key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]); | |
719 | key_mask->mpls_ttl = MPLS_TTL_MASK; | |
720 | } | |
721 | if (tb[TCA_FLOWER_KEY_MPLS_BOS]) { | |
1a7fca63 BL |
722 | u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]); |
723 | ||
724 | if (bos & ~MPLS_BOS_MASK) | |
725 | return -EINVAL; | |
726 | key_val->mpls_bos = bos; | |
a577d8f7 BL |
727 | key_mask->mpls_bos = MPLS_BOS_MASK; |
728 | } | |
729 | if (tb[TCA_FLOWER_KEY_MPLS_TC]) { | |
1a7fca63 BL |
730 | u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]); |
731 | ||
732 | if (tc & ~MPLS_TC_MASK) | |
733 | return -EINVAL; | |
734 | key_val->mpls_tc = tc; | |
a577d8f7 BL |
735 | key_mask->mpls_tc = MPLS_TC_MASK; |
736 | } | |
737 | if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) { | |
1a7fca63 BL |
738 | u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]); |
739 | ||
740 | if (label & ~MPLS_LABEL_MASK) | |
741 | return -EINVAL; | |
742 | key_val->mpls_label = label; | |
a577d8f7 BL |
743 | key_mask->mpls_label = MPLS_LABEL_MASK; |
744 | } | |
1a7fca63 | 745 | return 0; |
a577d8f7 BL |
746 | } |
747 | ||
9399ae9a | 748 | static void fl_set_key_vlan(struct nlattr **tb, |
aaab0834 | 749 | __be16 ethertype, |
d64efd09 | 750 | int vlan_id_key, int vlan_prio_key, |
9399ae9a HHZ |
751 | struct flow_dissector_key_vlan *key_val, |
752 | struct flow_dissector_key_vlan *key_mask) | |
753 | { | |
754 | #define VLAN_PRIORITY_MASK 0x7 | |
755 | ||
d64efd09 | 756 | if (tb[vlan_id_key]) { |
9399ae9a | 757 | key_val->vlan_id = |
d64efd09 | 758 | nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK; |
9399ae9a HHZ |
759 | key_mask->vlan_id = VLAN_VID_MASK; |
760 | } | |
d64efd09 | 761 | if (tb[vlan_prio_key]) { |
9399ae9a | 762 | key_val->vlan_priority = |
d64efd09 | 763 | nla_get_u8(tb[vlan_prio_key]) & |
9399ae9a HHZ |
764 | VLAN_PRIORITY_MASK; |
765 | key_mask->vlan_priority = VLAN_PRIORITY_MASK; | |
766 | } | |
aaab0834 JL |
767 | key_val->vlan_tpid = ethertype; |
768 | key_mask->vlan_tpid = cpu_to_be16(~0); | |
9399ae9a HHZ |
769 | } |
770 | ||
faa3ffce OG |
771 | static void fl_set_key_flag(u32 flower_key, u32 flower_mask, |
772 | u32 *dissector_key, u32 *dissector_mask, | |
773 | u32 flower_flag_bit, u32 dissector_flag_bit) | |
774 | { | |
775 | if (flower_mask & flower_flag_bit) { | |
776 | *dissector_mask |= dissector_flag_bit; | |
777 | if (flower_key & flower_flag_bit) | |
778 | *dissector_key |= dissector_flag_bit; | |
779 | } | |
780 | } | |
781 | ||
d9724772 OG |
782 | static int fl_set_key_flags(struct nlattr **tb, |
783 | u32 *flags_key, u32 *flags_mask) | |
faa3ffce OG |
784 | { |
785 | u32 key, mask; | |
786 | ||
d9724772 OG |
787 | /* mask is mandatory for flags */ |
788 | if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) | |
789 | return -EINVAL; | |
faa3ffce OG |
790 | |
791 | key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS])); | |
d9724772 | 792 | mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK])); |
faa3ffce OG |
793 | |
794 | *flags_key = 0; | |
795 | *flags_mask = 0; | |
796 | ||
797 | fl_set_key_flag(key, mask, flags_key, flags_mask, | |
798 | TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); | |
459d153d PJV |
799 | fl_set_key_flag(key, mask, flags_key, flags_mask, |
800 | TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, | |
801 | FLOW_DIS_FIRST_FRAG); | |
d9724772 OG |
802 | |
803 | return 0; | |
faa3ffce OG |
804 | } |
805 | ||
0e2c17b6 | 806 | static void fl_set_key_ip(struct nlattr **tb, bool encap, |
4d80cc0a OG |
807 | struct flow_dissector_key_ip *key, |
808 | struct flow_dissector_key_ip *mask) | |
809 | { | |
0e2c17b6 OG |
810 | int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS; |
811 | int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL; | |
812 | int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK; | |
813 | int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK; | |
4d80cc0a | 814 | |
0e2c17b6 OG |
815 | fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)); |
816 | fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)); | |
4d80cc0a OG |
817 | } |
818 | ||
0a6e7778 PJV |
819 | static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key, |
820 | int depth, int option_len, | |
821 | struct netlink_ext_ack *extack) | |
822 | { | |
823 | struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1]; | |
824 | struct nlattr *class = NULL, *type = NULL, *data = NULL; | |
825 | struct geneve_opt *opt; | |
826 | int err, data_len = 0; | |
827 | ||
828 | if (option_len > sizeof(struct geneve_opt)) | |
829 | data_len = option_len - sizeof(struct geneve_opt); | |
830 | ||
831 | opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len]; | |
832 | memset(opt, 0xff, option_len); | |
833 | opt->length = data_len / 4; | |
834 | opt->r1 = 0; | |
835 | opt->r2 = 0; | |
836 | opt->r3 = 0; | |
837 | ||
838 | /* If no mask has been prodived we assume an exact match. */ | |
839 | if (!depth) | |
840 | return sizeof(struct geneve_opt) + data_len; | |
841 | ||
842 | if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) { | |
843 | NL_SET_ERR_MSG(extack, "Non-geneve option type for mask"); | |
844 | return -EINVAL; | |
845 | } | |
846 | ||
847 | err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX, | |
848 | nla, geneve_opt_policy, extack); | |
849 | if (err < 0) | |
850 | return err; | |
851 | ||
852 | /* We are not allowed to omit any of CLASS, TYPE or DATA | |
853 | * fields from the key. | |
854 | */ | |
855 | if (!option_len && | |
856 | (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] || | |
857 | !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] || | |
858 | !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) { | |
859 | NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data"); | |
860 | return -EINVAL; | |
861 | } | |
862 | ||
863 | /* Omitting any of CLASS, TYPE or DATA fields is allowed | |
864 | * for the mask. | |
865 | */ | |
866 | if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) { | |
867 | int new_len = key->enc_opts.len; | |
868 | ||
869 | data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]; | |
870 | data_len = nla_len(data); | |
871 | if (data_len < 4) { | |
872 | NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long"); | |
873 | return -ERANGE; | |
874 | } | |
875 | if (data_len % 4) { | |
876 | NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long"); | |
877 | return -ERANGE; | |
878 | } | |
879 | ||
880 | new_len += sizeof(struct geneve_opt) + data_len; | |
881 | BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX); | |
882 | if (new_len > FLOW_DIS_TUN_OPTS_MAX) { | |
883 | NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size"); | |
884 | return -ERANGE; | |
885 | } | |
886 | opt->length = data_len / 4; | |
887 | memcpy(opt->opt_data, nla_data(data), data_len); | |
888 | } | |
889 | ||
890 | if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) { | |
891 | class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]; | |
892 | opt->opt_class = nla_get_be16(class); | |
893 | } | |
894 | ||
895 | if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) { | |
896 | type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]; | |
897 | opt->type = nla_get_u8(type); | |
898 | } | |
899 | ||
900 | return sizeof(struct geneve_opt) + data_len; | |
901 | } | |
902 | ||
903 | static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key, | |
904 | struct fl_flow_key *mask, | |
905 | struct netlink_ext_ack *extack) | |
906 | { | |
907 | const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL; | |
63c82997 JK |
908 | int err, option_len, key_depth, msk_depth = 0; |
909 | ||
910 | err = nla_validate_nested(tb[TCA_FLOWER_KEY_ENC_OPTS], | |
911 | TCA_FLOWER_KEY_ENC_OPTS_MAX, | |
912 | enc_opts_policy, extack); | |
913 | if (err) | |
914 | return err; | |
0a6e7778 PJV |
915 | |
916 | nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]); | |
917 | ||
918 | if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) { | |
63c82997 JK |
919 | err = nla_validate_nested(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK], |
920 | TCA_FLOWER_KEY_ENC_OPTS_MAX, | |
921 | enc_opts_policy, extack); | |
922 | if (err) | |
923 | return err; | |
924 | ||
0a6e7778 PJV |
925 | nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); |
926 | msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); | |
927 | } | |
928 | ||
929 | nla_for_each_attr(nla_opt_key, nla_enc_key, | |
930 | nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) { | |
931 | switch (nla_type(nla_opt_key)) { | |
932 | case TCA_FLOWER_KEY_ENC_OPTS_GENEVE: | |
933 | option_len = 0; | |
934 | key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT; | |
935 | option_len = fl_set_geneve_opt(nla_opt_key, key, | |
936 | key_depth, option_len, | |
937 | extack); | |
938 | if (option_len < 0) | |
939 | return option_len; | |
940 | ||
941 | key->enc_opts.len += option_len; | |
942 | /* At the same time we need to parse through the mask | |
943 | * in order to verify exact and mask attribute lengths. | |
944 | */ | |
945 | mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT; | |
946 | option_len = fl_set_geneve_opt(nla_opt_msk, mask, | |
947 | msk_depth, option_len, | |
948 | extack); | |
949 | if (option_len < 0) | |
950 | return option_len; | |
951 | ||
952 | mask->enc_opts.len += option_len; | |
953 | if (key->enc_opts.len != mask->enc_opts.len) { | |
954 | NL_SET_ERR_MSG(extack, "Key and mask miss aligned"); | |
955 | return -EINVAL; | |
956 | } | |
957 | ||
958 | if (msk_depth) | |
959 | nla_opt_msk = nla_next(nla_opt_msk, &msk_depth); | |
960 | break; | |
961 | default: | |
962 | NL_SET_ERR_MSG(extack, "Unknown tunnel option type"); | |
963 | return -EINVAL; | |
964 | } | |
965 | } | |
966 | ||
967 | return 0; | |
968 | } | |
969 | ||
77b9900e | 970 | static int fl_set_key(struct net *net, struct nlattr **tb, |
1057c55f AA |
971 | struct fl_flow_key *key, struct fl_flow_key *mask, |
972 | struct netlink_ext_ack *extack) | |
77b9900e | 973 | { |
9399ae9a | 974 | __be16 ethertype; |
d9724772 | 975 | int ret = 0; |
dd3aa3b5 | 976 | #ifdef CONFIG_NET_CLS_IND |
77b9900e | 977 | if (tb[TCA_FLOWER_INDEV]) { |
1057c55f | 978 | int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack); |
77b9900e JP |
979 | if (err < 0) |
980 | return err; | |
981 | key->indev_ifindex = err; | |
982 | mask->indev_ifindex = 0xffffffff; | |
983 | } | |
dd3aa3b5 | 984 | #endif |
77b9900e JP |
985 | |
986 | fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, | |
987 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, | |
988 | sizeof(key->eth.dst)); | |
989 | fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, | |
990 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, | |
991 | sizeof(key->eth.src)); | |
66530bdf | 992 | |
0b498a52 | 993 | if (tb[TCA_FLOWER_KEY_ETH_TYPE]) { |
9399ae9a HHZ |
994 | ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]); |
995 | ||
aaab0834 | 996 | if (eth_type_vlan(ethertype)) { |
d64efd09 JL |
997 | fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID, |
998 | TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, | |
999 | &mask->vlan); | |
1000 | ||
5e9a0fe4 JL |
1001 | if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) { |
1002 | ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]); | |
1003 | if (eth_type_vlan(ethertype)) { | |
1004 | fl_set_key_vlan(tb, ethertype, | |
1005 | TCA_FLOWER_KEY_CVLAN_ID, | |
1006 | TCA_FLOWER_KEY_CVLAN_PRIO, | |
1007 | &key->cvlan, &mask->cvlan); | |
1008 | fl_set_key_val(tb, &key->basic.n_proto, | |
1009 | TCA_FLOWER_KEY_CVLAN_ETH_TYPE, | |
1010 | &mask->basic.n_proto, | |
1011 | TCA_FLOWER_UNSPEC, | |
1012 | sizeof(key->basic.n_proto)); | |
1013 | } else { | |
1014 | key->basic.n_proto = ethertype; | |
1015 | mask->basic.n_proto = cpu_to_be16(~0); | |
1016 | } | |
d64efd09 | 1017 | } |
0b498a52 AB |
1018 | } else { |
1019 | key->basic.n_proto = ethertype; | |
1020 | mask->basic.n_proto = cpu_to_be16(~0); | |
1021 | } | |
9399ae9a | 1022 | } |
66530bdf | 1023 | |
77b9900e JP |
1024 | if (key->basic.n_proto == htons(ETH_P_IP) || |
1025 | key->basic.n_proto == htons(ETH_P_IPV6)) { | |
1026 | fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, | |
1027 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, | |
1028 | sizeof(key->basic.ip_proto)); | |
0e2c17b6 | 1029 | fl_set_key_ip(tb, false, &key->ip, &mask->ip); |
77b9900e | 1030 | } |
66530bdf JHS |
1031 | |
1032 | if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) { | |
1033 | key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; | |
970bfcd0 | 1034 | mask->control.addr_type = ~0; |
77b9900e JP |
1035 | fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
1036 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, | |
1037 | sizeof(key->ipv4.src)); | |
1038 | fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, | |
1039 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, | |
1040 | sizeof(key->ipv4.dst)); | |
66530bdf JHS |
1041 | } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) { |
1042 | key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
970bfcd0 | 1043 | mask->control.addr_type = ~0; |
77b9900e JP |
1044 | fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
1045 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, | |
1046 | sizeof(key->ipv6.src)); | |
1047 | fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, | |
1048 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, | |
1049 | sizeof(key->ipv6.dst)); | |
1050 | } | |
66530bdf | 1051 | |
77b9900e JP |
1052 | if (key->basic.ip_proto == IPPROTO_TCP) { |
1053 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, | |
aa72d708 | 1054 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
77b9900e JP |
1055 | sizeof(key->tp.src)); |
1056 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, | |
aa72d708 | 1057 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
77b9900e | 1058 | sizeof(key->tp.dst)); |
fdfc7dd6 JP |
1059 | fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, |
1060 | &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, | |
1061 | sizeof(key->tcp.flags)); | |
77b9900e JP |
1062 | } else if (key->basic.ip_proto == IPPROTO_UDP) { |
1063 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, | |
aa72d708 | 1064 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
77b9900e JP |
1065 | sizeof(key->tp.src)); |
1066 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, | |
aa72d708 | 1067 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
77b9900e | 1068 | sizeof(key->tp.dst)); |
5976c5f4 SH |
1069 | } else if (key->basic.ip_proto == IPPROTO_SCTP) { |
1070 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, | |
1071 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, | |
1072 | sizeof(key->tp.src)); | |
1073 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, | |
1074 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, | |
1075 | sizeof(key->tp.dst)); | |
7b684884 SH |
1076 | } else if (key->basic.n_proto == htons(ETH_P_IP) && |
1077 | key->basic.ip_proto == IPPROTO_ICMP) { | |
1078 | fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE, | |
1079 | &mask->icmp.type, | |
1080 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, | |
1081 | sizeof(key->icmp.type)); | |
1082 | fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE, | |
1083 | &mask->icmp.code, | |
1084 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK, | |
1085 | sizeof(key->icmp.code)); | |
1086 | } else if (key->basic.n_proto == htons(ETH_P_IPV6) && | |
1087 | key->basic.ip_proto == IPPROTO_ICMPV6) { | |
1088 | fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE, | |
1089 | &mask->icmp.type, | |
1090 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, | |
1091 | sizeof(key->icmp.type)); | |
040587af | 1092 | fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE, |
7b684884 | 1093 | &mask->icmp.code, |
040587af | 1094 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK, |
7b684884 | 1095 | sizeof(key->icmp.code)); |
a577d8f7 BL |
1096 | } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) || |
1097 | key->basic.n_proto == htons(ETH_P_MPLS_MC)) { | |
1a7fca63 BL |
1098 | ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls); |
1099 | if (ret) | |
1100 | return ret; | |
99d31326 SH |
1101 | } else if (key->basic.n_proto == htons(ETH_P_ARP) || |
1102 | key->basic.n_proto == htons(ETH_P_RARP)) { | |
1103 | fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP, | |
1104 | &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK, | |
1105 | sizeof(key->arp.sip)); | |
1106 | fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP, | |
1107 | &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK, | |
1108 | sizeof(key->arp.tip)); | |
1109 | fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP, | |
1110 | &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK, | |
1111 | sizeof(key->arp.op)); | |
1112 | fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, | |
1113 | mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, | |
1114 | sizeof(key->arp.sha)); | |
1115 | fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, | |
1116 | mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, | |
1117 | sizeof(key->arp.tha)); | |
77b9900e JP |
1118 | } |
1119 | ||
5c72299f AN |
1120 | if (key->basic.ip_proto == IPPROTO_TCP || |
1121 | key->basic.ip_proto == IPPROTO_UDP || | |
1122 | key->basic.ip_proto == IPPROTO_SCTP) { | |
1123 | ret = fl_set_key_port_range(tb, key, mask); | |
1124 | if (ret) | |
1125 | return ret; | |
1126 | } | |
1127 | ||
bc3103f1 AV |
1128 | if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] || |
1129 | tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) { | |
1130 | key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; | |
970bfcd0 | 1131 | mask->enc_control.addr_type = ~0; |
bc3103f1 AV |
1132 | fl_set_key_val(tb, &key->enc_ipv4.src, |
1133 | TCA_FLOWER_KEY_ENC_IPV4_SRC, | |
1134 | &mask->enc_ipv4.src, | |
1135 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, | |
1136 | sizeof(key->enc_ipv4.src)); | |
1137 | fl_set_key_val(tb, &key->enc_ipv4.dst, | |
1138 | TCA_FLOWER_KEY_ENC_IPV4_DST, | |
1139 | &mask->enc_ipv4.dst, | |
1140 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, | |
1141 | sizeof(key->enc_ipv4.dst)); | |
1142 | } | |
1143 | ||
1144 | if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] || | |
1145 | tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) { | |
1146 | key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
970bfcd0 | 1147 | mask->enc_control.addr_type = ~0; |
bc3103f1 AV |
1148 | fl_set_key_val(tb, &key->enc_ipv6.src, |
1149 | TCA_FLOWER_KEY_ENC_IPV6_SRC, | |
1150 | &mask->enc_ipv6.src, | |
1151 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, | |
1152 | sizeof(key->enc_ipv6.src)); | |
1153 | fl_set_key_val(tb, &key->enc_ipv6.dst, | |
1154 | TCA_FLOWER_KEY_ENC_IPV6_DST, | |
1155 | &mask->enc_ipv6.dst, | |
1156 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, | |
1157 | sizeof(key->enc_ipv6.dst)); | |
1158 | } | |
1159 | ||
1160 | fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID, | |
eb523f42 | 1161 | &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC, |
bc3103f1 AV |
1162 | sizeof(key->enc_key_id.keyid)); |
1163 | ||
f4d997fd HHZ |
1164 | fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, |
1165 | &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, | |
1166 | sizeof(key->enc_tp.src)); | |
1167 | ||
1168 | fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT, | |
1169 | &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, | |
1170 | sizeof(key->enc_tp.dst)); | |
1171 | ||
0e2c17b6 OG |
1172 | fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip); |
1173 | ||
0a6e7778 PJV |
1174 | if (tb[TCA_FLOWER_KEY_ENC_OPTS]) { |
1175 | ret = fl_set_enc_opt(tb, key, mask, extack); | |
1176 | if (ret) | |
1177 | return ret; | |
1178 | } | |
1179 | ||
d9724772 OG |
1180 | if (tb[TCA_FLOWER_KEY_FLAGS]) |
1181 | ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags); | |
faa3ffce | 1182 | |
d9724772 | 1183 | return ret; |
77b9900e JP |
1184 | } |
1185 | ||
05cd271f PB |
1186 | static void fl_mask_copy(struct fl_flow_mask *dst, |
1187 | struct fl_flow_mask *src) | |
77b9900e | 1188 | { |
05cd271f PB |
1189 | const void *psrc = fl_key_get_start(&src->key, src); |
1190 | void *pdst = fl_key_get_start(&dst->key, src); | |
77b9900e | 1191 | |
05cd271f PB |
1192 | memcpy(pdst, psrc, fl_mask_range(src)); |
1193 | dst->range = src->range; | |
77b9900e JP |
1194 | } |
1195 | ||
1196 | static const struct rhashtable_params fl_ht_params = { | |
1197 | .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */ | |
1198 | .head_offset = offsetof(struct cls_fl_filter, ht_node), | |
1199 | .automatic_shrinking = true, | |
1200 | }; | |
1201 | ||
05cd271f | 1202 | static int fl_init_mask_hashtable(struct fl_flow_mask *mask) |
77b9900e | 1203 | { |
05cd271f PB |
1204 | mask->filter_ht_params = fl_ht_params; |
1205 | mask->filter_ht_params.key_len = fl_mask_range(mask); | |
1206 | mask->filter_ht_params.key_offset += mask->range.start; | |
77b9900e | 1207 | |
05cd271f | 1208 | return rhashtable_init(&mask->ht, &mask->filter_ht_params); |
77b9900e JP |
1209 | } |
1210 | ||
1211 | #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member) | |
cb205a81 | 1212 | #define FL_KEY_MEMBER_SIZE(member) FIELD_SIZEOF(struct fl_flow_key, member) |
77b9900e | 1213 | |
339ba878 HHZ |
1214 | #define FL_KEY_IS_MASKED(mask, member) \ |
1215 | memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \ | |
1216 | 0, FL_KEY_MEMBER_SIZE(member)) \ | |
77b9900e JP |
1217 | |
1218 | #define FL_KEY_SET(keys, cnt, id, member) \ | |
1219 | do { \ | |
1220 | keys[cnt].key_id = id; \ | |
1221 | keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \ | |
1222 | cnt++; \ | |
1223 | } while(0); | |
1224 | ||
339ba878 | 1225 | #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \ |
77b9900e | 1226 | do { \ |
339ba878 | 1227 | if (FL_KEY_IS_MASKED(mask, member)) \ |
77b9900e JP |
1228 | FL_KEY_SET(keys, cnt, id, member); \ |
1229 | } while(0); | |
1230 | ||
33fb5cba JP |
1231 | static void fl_init_dissector(struct flow_dissector *dissector, |
1232 | struct fl_flow_key *mask) | |
77b9900e JP |
1233 | { |
1234 | struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX]; | |
1235 | size_t cnt = 0; | |
1236 | ||
42aecaa9 | 1237 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control); |
77b9900e | 1238 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic); |
33fb5cba | 1239 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
339ba878 | 1240 | FLOW_DISSECTOR_KEY_ETH_ADDRS, eth); |
33fb5cba | 1241 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
339ba878 | 1242 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4); |
33fb5cba | 1243 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
339ba878 | 1244 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6); |
5c72299f AN |
1245 | if (FL_KEY_IS_MASKED(mask, tp) || |
1246 | FL_KEY_IS_MASKED(mask, tp_min) || FL_KEY_IS_MASKED(mask, tp_max)) | |
1247 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_PORTS, tp); | |
33fb5cba | 1248 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
4d80cc0a | 1249 | FLOW_DISSECTOR_KEY_IP, ip); |
33fb5cba | 1250 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
fdfc7dd6 | 1251 | FLOW_DISSECTOR_KEY_TCP, tcp); |
33fb5cba | 1252 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
7b684884 | 1253 | FLOW_DISSECTOR_KEY_ICMP, icmp); |
33fb5cba | 1254 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
99d31326 | 1255 | FLOW_DISSECTOR_KEY_ARP, arp); |
33fb5cba | 1256 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
a577d8f7 | 1257 | FLOW_DISSECTOR_KEY_MPLS, mpls); |
33fb5cba | 1258 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
9399ae9a | 1259 | FLOW_DISSECTOR_KEY_VLAN, vlan); |
33fb5cba | 1260 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
d64efd09 | 1261 | FLOW_DISSECTOR_KEY_CVLAN, cvlan); |
33fb5cba | 1262 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
519d1052 | 1263 | FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id); |
33fb5cba | 1264 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
519d1052 | 1265 | FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4); |
33fb5cba | 1266 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
519d1052 | 1267 | FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6); |
33fb5cba JP |
1268 | if (FL_KEY_IS_MASKED(mask, enc_ipv4) || |
1269 | FL_KEY_IS_MASKED(mask, enc_ipv6)) | |
519d1052 HHZ |
1270 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL, |
1271 | enc_control); | |
33fb5cba | 1272 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
f4d997fd | 1273 | FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp); |
33fb5cba | 1274 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
0e2c17b6 | 1275 | FLOW_DISSECTOR_KEY_ENC_IP, enc_ip); |
0a6e7778 PJV |
1276 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
1277 | FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts); | |
77b9900e | 1278 | |
33fb5cba | 1279 | skb_flow_dissector_init(dissector, keys, cnt); |
05cd271f PB |
1280 | } |
1281 | ||
1282 | static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head, | |
1283 | struct fl_flow_mask *mask) | |
1284 | { | |
1285 | struct fl_flow_mask *newmask; | |
1286 | int err; | |
1287 | ||
1288 | newmask = kzalloc(sizeof(*newmask), GFP_KERNEL); | |
1289 | if (!newmask) | |
1290 | return ERR_PTR(-ENOMEM); | |
1291 | ||
1292 | fl_mask_copy(newmask, mask); | |
1293 | ||
5c72299f AN |
1294 | if ((newmask->key.tp_min.dst && newmask->key.tp_max.dst) || |
1295 | (newmask->key.tp_min.src && newmask->key.tp_max.src)) | |
1296 | newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE; | |
1297 | ||
05cd271f PB |
1298 | err = fl_init_mask_hashtable(newmask); |
1299 | if (err) | |
1300 | goto errout_free; | |
1301 | ||
33fb5cba | 1302 | fl_init_dissector(&newmask->dissector, &newmask->key); |
05cd271f PB |
1303 | |
1304 | INIT_LIST_HEAD_RCU(&newmask->filters); | |
1305 | ||
f48ef4d5 | 1306 | refcount_set(&newmask->refcnt, 1); |
195c234d VB |
1307 | err = rhashtable_replace_fast(&head->ht, &mask->ht_node, |
1308 | &newmask->ht_node, mask_ht_params); | |
05cd271f PB |
1309 | if (err) |
1310 | goto errout_destroy; | |
1311 | ||
195c234d VB |
1312 | /* Wait until any potential concurrent users of mask are finished */ |
1313 | synchronize_rcu(); | |
1314 | ||
05cd271f PB |
1315 | list_add_tail_rcu(&newmask->list, &head->masks); |
1316 | ||
1317 | return newmask; | |
1318 | ||
1319 | errout_destroy: | |
1320 | rhashtable_destroy(&newmask->ht); | |
1321 | errout_free: | |
1322 | kfree(newmask); | |
1323 | ||
1324 | return ERR_PTR(err); | |
77b9900e JP |
1325 | } |
1326 | ||
1327 | static int fl_check_assign_mask(struct cls_fl_head *head, | |
05cd271f PB |
1328 | struct cls_fl_filter *fnew, |
1329 | struct cls_fl_filter *fold, | |
77b9900e JP |
1330 | struct fl_flow_mask *mask) |
1331 | { | |
05cd271f | 1332 | struct fl_flow_mask *newmask; |
f48ef4d5 | 1333 | int ret = 0; |
77b9900e | 1334 | |
f48ef4d5 | 1335 | rcu_read_lock(); |
195c234d VB |
1336 | |
1337 | /* Insert mask as temporary node to prevent concurrent creation of mask | |
1338 | * with same key. Any concurrent lookups with same key will return | |
1339 | * -EAGAIN because mask's refcnt is zero. It is safe to insert | |
1340 | * stack-allocated 'mask' to masks hash table because we call | |
1341 | * synchronize_rcu() before returning from this function (either in case | |
1342 | * of error or after replacing it with heap-allocated mask in | |
1343 | * fl_create_new_mask()). | |
1344 | */ | |
1345 | fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht, | |
1346 | &mask->ht_node, | |
1347 | mask_ht_params); | |
05cd271f | 1348 | if (!fnew->mask) { |
f48ef4d5 VB |
1349 | rcu_read_unlock(); |
1350 | ||
195c234d VB |
1351 | if (fold) { |
1352 | ret = -EINVAL; | |
1353 | goto errout_cleanup; | |
1354 | } | |
77b9900e | 1355 | |
05cd271f | 1356 | newmask = fl_create_new_mask(head, mask); |
195c234d VB |
1357 | if (IS_ERR(newmask)) { |
1358 | ret = PTR_ERR(newmask); | |
1359 | goto errout_cleanup; | |
1360 | } | |
77b9900e | 1361 | |
05cd271f | 1362 | fnew->mask = newmask; |
f48ef4d5 | 1363 | return 0; |
195c234d VB |
1364 | } else if (IS_ERR(fnew->mask)) { |
1365 | ret = PTR_ERR(fnew->mask); | |
f6521c58 | 1366 | } else if (fold && fold->mask != fnew->mask) { |
f48ef4d5 VB |
1367 | ret = -EINVAL; |
1368 | } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) { | |
1369 | /* Mask was deleted concurrently, try again */ | |
1370 | ret = -EAGAIN; | |
05cd271f | 1371 | } |
f48ef4d5 VB |
1372 | rcu_read_unlock(); |
1373 | return ret; | |
195c234d VB |
1374 | |
1375 | errout_cleanup: | |
1376 | rhashtable_remove_fast(&head->ht, &mask->ht_node, | |
1377 | mask_ht_params); | |
1378 | /* Wait until any potential concurrent users of mask are finished */ | |
1379 | synchronize_rcu(); | |
1380 | return ret; | |
77b9900e JP |
1381 | } |
1382 | ||
1383 | static int fl_set_parms(struct net *net, struct tcf_proto *tp, | |
1384 | struct cls_fl_filter *f, struct fl_flow_mask *mask, | |
1385 | unsigned long base, struct nlattr **tb, | |
50a56190 | 1386 | struct nlattr *est, bool ovr, |
b95ec7eb | 1387 | struct fl_flow_tmplt *tmplt, |
50a56190 | 1388 | struct netlink_ext_ack *extack) |
77b9900e | 1389 | { |
77b9900e JP |
1390 | int err; |
1391 | ||
ec6743a1 VB |
1392 | err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, true, |
1393 | extack); | |
77b9900e JP |
1394 | if (err < 0) |
1395 | return err; | |
1396 | ||
1397 | if (tb[TCA_FLOWER_CLASSID]) { | |
1398 | f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]); | |
1399 | tcf_bind_filter(tp, &f->res, base); | |
1400 | } | |
1401 | ||
1057c55f | 1402 | err = fl_set_key(net, tb, &f->key, &mask->key, extack); |
77b9900e | 1403 | if (err) |
45507529 | 1404 | return err; |
77b9900e JP |
1405 | |
1406 | fl_mask_update_range(mask); | |
1407 | fl_set_masked_key(&f->mkey, &f->key, mask); | |
1408 | ||
b95ec7eb JP |
1409 | if (!fl_mask_fits_tmplt(tmplt, mask)) { |
1410 | NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template"); | |
1411 | return -EINVAL; | |
1412 | } | |
1413 | ||
77b9900e | 1414 | return 0; |
77b9900e JP |
1415 | } |
1416 | ||
77b9900e JP |
1417 | static int fl_change(struct net *net, struct sk_buff *in_skb, |
1418 | struct tcf_proto *tp, unsigned long base, | |
1419 | u32 handle, struct nlattr **tca, | |
12db03b6 VB |
1420 | void **arg, bool ovr, bool rtnl_held, |
1421 | struct netlink_ext_ack *extack) | |
77b9900e | 1422 | { |
e474619a | 1423 | struct cls_fl_head *head = fl_head_dereference(tp); |
8113c095 | 1424 | struct cls_fl_filter *fold = *arg; |
77b9900e | 1425 | struct cls_fl_filter *fnew; |
2cddd201 | 1426 | struct fl_flow_mask *mask; |
39b7b6a6 | 1427 | struct nlattr **tb; |
77b9900e JP |
1428 | int err; |
1429 | ||
06177558 VB |
1430 | if (!tca[TCA_OPTIONS]) { |
1431 | err = -EINVAL; | |
1432 | goto errout_fold; | |
1433 | } | |
77b9900e | 1434 | |
2cddd201 | 1435 | mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL); |
06177558 VB |
1436 | if (!mask) { |
1437 | err = -ENOBUFS; | |
1438 | goto errout_fold; | |
1439 | } | |
39b7b6a6 | 1440 | |
2cddd201 IV |
1441 | tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); |
1442 | if (!tb) { | |
1443 | err = -ENOBUFS; | |
1444 | goto errout_mask_alloc; | |
1445 | } | |
1446 | ||
fceb6435 JB |
1447 | err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS], |
1448 | fl_policy, NULL); | |
77b9900e | 1449 | if (err < 0) |
39b7b6a6 | 1450 | goto errout_tb; |
77b9900e | 1451 | |
39b7b6a6 AB |
1452 | if (fold && handle && fold->handle != handle) { |
1453 | err = -EINVAL; | |
1454 | goto errout_tb; | |
1455 | } | |
77b9900e JP |
1456 | |
1457 | fnew = kzalloc(sizeof(*fnew), GFP_KERNEL); | |
39b7b6a6 AB |
1458 | if (!fnew) { |
1459 | err = -ENOBUFS; | |
1460 | goto errout_tb; | |
1461 | } | |
06177558 | 1462 | refcount_set(&fnew->refcnt, 1); |
77b9900e | 1463 | |
14215108 | 1464 | err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0); |
b9a24bb7 WC |
1465 | if (err < 0) |
1466 | goto errout; | |
77b9900e | 1467 | |
e69985c6 AV |
1468 | if (tb[TCA_FLOWER_FLAGS]) { |
1469 | fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]); | |
1470 | ||
1471 | if (!tc_flags_valid(fnew->flags)) { | |
1472 | err = -EINVAL; | |
ecb3dea4 | 1473 | goto errout; |
e69985c6 AV |
1474 | } |
1475 | } | |
5b33f488 | 1476 | |
2cddd201 | 1477 | err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr, |
b95ec7eb | 1478 | tp->chain->tmplt_priv, extack); |
77b9900e | 1479 | if (err) |
ecb3dea4 | 1480 | goto errout; |
77b9900e | 1481 | |
2cddd201 | 1482 | err = fl_check_assign_mask(head, fnew, fold, mask); |
77b9900e | 1483 | if (err) |
ecb3dea4 VB |
1484 | goto errout; |
1485 | ||
79685219 | 1486 | if (!tc_skip_hw(fnew->flags)) { |
05cd271f | 1487 | err = fl_hw_replace_filter(tp, fnew, extack); |
79685219 | 1488 | if (err) |
620da486 | 1489 | goto errout_mask; |
79685219 | 1490 | } |
5b33f488 | 1491 | |
55593960 OG |
1492 | if (!tc_in_hw(fnew->flags)) |
1493 | fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW; | |
1494 | ||
06177558 | 1495 | refcount_inc(&fnew->refcnt); |
5b33f488 | 1496 | if (fold) { |
b2552b8c VB |
1497 | /* Fold filter was deleted concurrently. Retry lookup. */ |
1498 | if (fold->deleted) { | |
1499 | err = -EAGAIN; | |
1500 | goto errout_hw; | |
1501 | } | |
1502 | ||
620da486 VB |
1503 | fnew->handle = handle; |
1504 | ||
1505 | err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node, | |
1506 | fnew->mask->filter_ht_params); | |
1507 | if (err) | |
1508 | goto errout_hw; | |
1509 | ||
599d2570 RD |
1510 | rhashtable_remove_fast(&fold->mask->ht, |
1511 | &fold->ht_node, | |
1512 | fold->mask->filter_ht_params); | |
234a4624 | 1513 | idr_replace(&head->handle_idr, fnew, fnew->handle); |
ff3532f2 | 1514 | list_replace_rcu(&fold->list, &fnew->list); |
b2552b8c | 1515 | fold->deleted = true; |
620da486 | 1516 | |
f48ef4d5 | 1517 | fl_mask_put(head, fold->mask, true); |
620da486 VB |
1518 | if (!tc_skip_hw(fold->flags)) |
1519 | fl_hw_destroy_filter(tp, fold, NULL); | |
77b9900e | 1520 | tcf_unbind_filter(tp, &fold->res); |
0dadc117 | 1521 | tcf_exts_get_net(&fold->exts); |
06177558 VB |
1522 | /* Caller holds reference to fold, so refcnt is always > 0 |
1523 | * after this. | |
1524 | */ | |
1525 | refcount_dec(&fold->refcnt); | |
1526 | __fl_put(fold); | |
77b9900e | 1527 | } else { |
620da486 VB |
1528 | if (__fl_lookup(fnew->mask, &fnew->mkey)) { |
1529 | err = -EEXIST; | |
1530 | goto errout_hw; | |
1531 | } | |
1532 | ||
1533 | if (handle) { | |
1534 | /* user specifies a handle and it doesn't exist */ | |
1535 | err = idr_alloc_u32(&head->handle_idr, fnew, &handle, | |
1536 | handle, GFP_ATOMIC); | |
1537 | } else { | |
1538 | handle = 1; | |
1539 | err = idr_alloc_u32(&head->handle_idr, fnew, &handle, | |
1540 | INT_MAX, GFP_ATOMIC); | |
1541 | } | |
1542 | if (err) | |
1543 | goto errout_hw; | |
1544 | ||
1545 | fnew->handle = handle; | |
1546 | ||
1547 | err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node, | |
1548 | fnew->mask->filter_ht_params); | |
1549 | if (err) | |
1550 | goto errout_idr; | |
1551 | ||
05cd271f | 1552 | list_add_tail_rcu(&fnew->list, &fnew->mask->filters); |
77b9900e JP |
1553 | } |
1554 | ||
620da486 VB |
1555 | *arg = fnew; |
1556 | ||
39b7b6a6 | 1557 | kfree(tb); |
2cddd201 | 1558 | kfree(mask); |
77b9900e JP |
1559 | return 0; |
1560 | ||
fe2502e4 | 1561 | errout_idr: |
620da486 VB |
1562 | idr_remove(&head->handle_idr, fnew->handle); |
1563 | errout_hw: | |
1564 | if (!tc_skip_hw(fnew->flags)) | |
1565 | fl_hw_destroy_filter(tp, fnew, NULL); | |
ecb3dea4 | 1566 | errout_mask: |
f48ef4d5 | 1567 | fl_mask_put(head, fnew->mask, true); |
77b9900e | 1568 | errout: |
b9a24bb7 | 1569 | tcf_exts_destroy(&fnew->exts); |
77b9900e | 1570 | kfree(fnew); |
39b7b6a6 AB |
1571 | errout_tb: |
1572 | kfree(tb); | |
2cddd201 IV |
1573 | errout_mask_alloc: |
1574 | kfree(mask); | |
06177558 VB |
1575 | errout_fold: |
1576 | if (fold) | |
1577 | __fl_put(fold); | |
77b9900e JP |
1578 | return err; |
1579 | } | |
1580 | ||
571acf21 | 1581 | static int fl_delete(struct tcf_proto *tp, void *arg, bool *last, |
12db03b6 | 1582 | bool rtnl_held, struct netlink_ext_ack *extack) |
77b9900e | 1583 | { |
e474619a | 1584 | struct cls_fl_head *head = fl_head_dereference(tp); |
8113c095 | 1585 | struct cls_fl_filter *f = arg; |
b2552b8c VB |
1586 | bool last_on_mask; |
1587 | int err = 0; | |
77b9900e | 1588 | |
b2552b8c | 1589 | err = __fl_delete(tp, f, &last_on_mask, extack); |
05cd271f | 1590 | *last = list_empty(&head->masks); |
06177558 VB |
1591 | __fl_put(f); |
1592 | ||
b2552b8c | 1593 | return err; |
77b9900e JP |
1594 | } |
1595 | ||
12db03b6 VB |
1596 | static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
1597 | bool rtnl_held) | |
77b9900e | 1598 | { |
77b9900e | 1599 | struct cls_fl_filter *f; |
05cd271f | 1600 | |
01683a14 VB |
1601 | arg->count = arg->skip; |
1602 | ||
06177558 | 1603 | while ((f = fl_get_next_filter(tp, &arg->cookie)) != NULL) { |
01683a14 | 1604 | if (arg->fn(tp, f, arg) < 0) { |
06177558 | 1605 | __fl_put(f); |
01683a14 VB |
1606 | arg->stop = 1; |
1607 | break; | |
05cd271f | 1608 | } |
06177558 VB |
1609 | __fl_put(f); |
1610 | arg->cookie++; | |
01683a14 | 1611 | arg->count++; |
77b9900e JP |
1612 | } |
1613 | } | |
1614 | ||
31533cba JH |
1615 | static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb, |
1616 | void *cb_priv, struct netlink_ext_ack *extack) | |
1617 | { | |
e474619a | 1618 | struct cls_fl_head *head = fl_head_dereference(tp); |
31533cba JH |
1619 | struct tc_cls_flower_offload cls_flower = {}; |
1620 | struct tcf_block *block = tp->chain->block; | |
1621 | struct fl_flow_mask *mask; | |
1622 | struct cls_fl_filter *f; | |
1623 | int err; | |
1624 | ||
1625 | list_for_each_entry(mask, &head->masks, list) { | |
1626 | list_for_each_entry(f, &mask->filters, list) { | |
1627 | if (tc_skip_hw(f->flags)) | |
1628 | continue; | |
1629 | ||
e3ab786b PNA |
1630 | cls_flower.rule = |
1631 | flow_rule_alloc(tcf_exts_num_actions(&f->exts)); | |
8f256622 PNA |
1632 | if (!cls_flower.rule) |
1633 | return -ENOMEM; | |
1634 | ||
31533cba JH |
1635 | tc_cls_common_offload_init(&cls_flower.common, tp, |
1636 | f->flags, extack); | |
1637 | cls_flower.command = add ? | |
1638 | TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY; | |
1639 | cls_flower.cookie = (unsigned long)f; | |
8f256622 PNA |
1640 | cls_flower.rule->match.dissector = &mask->dissector; |
1641 | cls_flower.rule->match.mask = &mask->key; | |
1642 | cls_flower.rule->match.key = &f->mkey; | |
3a7b6861 PNA |
1643 | |
1644 | err = tc_setup_flow_action(&cls_flower.rule->action, | |
1645 | &f->exts); | |
1646 | if (err) { | |
1647 | kfree(cls_flower.rule); | |
1f15bb4f VB |
1648 | if (tc_skip_sw(f->flags)) { |
1649 | NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); | |
1650 | return err; | |
1651 | } | |
1652 | continue; | |
3a7b6861 PNA |
1653 | } |
1654 | ||
31533cba JH |
1655 | cls_flower.classid = f->res.classid; |
1656 | ||
1657 | err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv); | |
8f256622 PNA |
1658 | kfree(cls_flower.rule); |
1659 | ||
31533cba JH |
1660 | if (err) { |
1661 | if (add && tc_skip_sw(f->flags)) | |
1662 | return err; | |
1663 | continue; | |
1664 | } | |
1665 | ||
1666 | tc_cls_offload_cnt_update(block, &f->in_hw_count, | |
1667 | &f->flags, add); | |
1668 | } | |
1669 | } | |
1670 | ||
1671 | return 0; | |
1672 | } | |
1673 | ||
8f256622 PNA |
1674 | static int fl_hw_create_tmplt(struct tcf_chain *chain, |
1675 | struct fl_flow_tmplt *tmplt) | |
34738452 JP |
1676 | { |
1677 | struct tc_cls_flower_offload cls_flower = {}; | |
1678 | struct tcf_block *block = chain->block; | |
34738452 | 1679 | |
e3ab786b | 1680 | cls_flower.rule = flow_rule_alloc(0); |
8f256622 PNA |
1681 | if (!cls_flower.rule) |
1682 | return -ENOMEM; | |
1683 | ||
34738452 JP |
1684 | cls_flower.common.chain_index = chain->index; |
1685 | cls_flower.command = TC_CLSFLOWER_TMPLT_CREATE; | |
1686 | cls_flower.cookie = (unsigned long) tmplt; | |
8f256622 PNA |
1687 | cls_flower.rule->match.dissector = &tmplt->dissector; |
1688 | cls_flower.rule->match.mask = &tmplt->mask; | |
1689 | cls_flower.rule->match.key = &tmplt->dummy_key; | |
34738452 JP |
1690 | |
1691 | /* We don't care if driver (any of them) fails to handle this | |
1692 | * call. It serves just as a hint for it. | |
1693 | */ | |
aeb3fecd | 1694 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false); |
8f256622 PNA |
1695 | kfree(cls_flower.rule); |
1696 | ||
1697 | return 0; | |
34738452 JP |
1698 | } |
1699 | ||
1700 | static void fl_hw_destroy_tmplt(struct tcf_chain *chain, | |
1701 | struct fl_flow_tmplt *tmplt) | |
1702 | { | |
1703 | struct tc_cls_flower_offload cls_flower = {}; | |
1704 | struct tcf_block *block = chain->block; | |
1705 | ||
1706 | cls_flower.common.chain_index = chain->index; | |
1707 | cls_flower.command = TC_CLSFLOWER_TMPLT_DESTROY; | |
1708 | cls_flower.cookie = (unsigned long) tmplt; | |
1709 | ||
aeb3fecd | 1710 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false); |
34738452 JP |
1711 | } |
1712 | ||
b95ec7eb JP |
1713 | static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain, |
1714 | struct nlattr **tca, | |
1715 | struct netlink_ext_ack *extack) | |
1716 | { | |
1717 | struct fl_flow_tmplt *tmplt; | |
1718 | struct nlattr **tb; | |
1719 | int err; | |
1720 | ||
1721 | if (!tca[TCA_OPTIONS]) | |
1722 | return ERR_PTR(-EINVAL); | |
1723 | ||
1724 | tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); | |
1725 | if (!tb) | |
1726 | return ERR_PTR(-ENOBUFS); | |
1727 | err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS], | |
1728 | fl_policy, NULL); | |
1729 | if (err) | |
1730 | goto errout_tb; | |
1731 | ||
1732 | tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL); | |
1cbc36a5 DC |
1733 | if (!tmplt) { |
1734 | err = -ENOMEM; | |
b95ec7eb | 1735 | goto errout_tb; |
1cbc36a5 | 1736 | } |
b95ec7eb JP |
1737 | tmplt->chain = chain; |
1738 | err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack); | |
1739 | if (err) | |
1740 | goto errout_tmplt; | |
b95ec7eb JP |
1741 | |
1742 | fl_init_dissector(&tmplt->dissector, &tmplt->mask); | |
1743 | ||
8f256622 PNA |
1744 | err = fl_hw_create_tmplt(chain, tmplt); |
1745 | if (err) | |
1746 | goto errout_tmplt; | |
34738452 | 1747 | |
8f256622 | 1748 | kfree(tb); |
b95ec7eb JP |
1749 | return tmplt; |
1750 | ||
1751 | errout_tmplt: | |
1752 | kfree(tmplt); | |
1753 | errout_tb: | |
1754 | kfree(tb); | |
1755 | return ERR_PTR(err); | |
1756 | } | |
1757 | ||
ec3ed293 VB |
1758 | static void fl_tmplt_destroy(void *tmplt_priv) |
1759 | { | |
1760 | struct fl_flow_tmplt *tmplt = tmplt_priv; | |
1761 | ||
95278dda CW |
1762 | fl_hw_destroy_tmplt(tmplt->chain, tmplt); |
1763 | kfree(tmplt); | |
ec3ed293 VB |
1764 | } |
1765 | ||
77b9900e JP |
1766 | static int fl_dump_key_val(struct sk_buff *skb, |
1767 | void *val, int val_type, | |
1768 | void *mask, int mask_type, int len) | |
1769 | { | |
1770 | int err; | |
1771 | ||
1772 | if (!memchr_inv(mask, 0, len)) | |
1773 | return 0; | |
1774 | err = nla_put(skb, val_type, len, val); | |
1775 | if (err) | |
1776 | return err; | |
1777 | if (mask_type != TCA_FLOWER_UNSPEC) { | |
1778 | err = nla_put(skb, mask_type, len, mask); | |
1779 | if (err) | |
1780 | return err; | |
1781 | } | |
1782 | return 0; | |
1783 | } | |
1784 | ||
5c72299f AN |
1785 | static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key, |
1786 | struct fl_flow_key *mask) | |
1787 | { | |
1788 | if (fl_dump_key_val(skb, &key->tp_min.dst, TCA_FLOWER_KEY_PORT_DST_MIN, | |
1789 | &mask->tp_min.dst, TCA_FLOWER_UNSPEC, | |
1790 | sizeof(key->tp_min.dst)) || | |
1791 | fl_dump_key_val(skb, &key->tp_max.dst, TCA_FLOWER_KEY_PORT_DST_MAX, | |
1792 | &mask->tp_max.dst, TCA_FLOWER_UNSPEC, | |
1793 | sizeof(key->tp_max.dst)) || | |
1794 | fl_dump_key_val(skb, &key->tp_min.src, TCA_FLOWER_KEY_PORT_SRC_MIN, | |
1795 | &mask->tp_min.src, TCA_FLOWER_UNSPEC, | |
1796 | sizeof(key->tp_min.src)) || | |
1797 | fl_dump_key_val(skb, &key->tp_max.src, TCA_FLOWER_KEY_PORT_SRC_MAX, | |
1798 | &mask->tp_max.src, TCA_FLOWER_UNSPEC, | |
1799 | sizeof(key->tp_max.src))) | |
1800 | return -1; | |
1801 | ||
1802 | return 0; | |
1803 | } | |
1804 | ||
a577d8f7 BL |
1805 | static int fl_dump_key_mpls(struct sk_buff *skb, |
1806 | struct flow_dissector_key_mpls *mpls_key, | |
1807 | struct flow_dissector_key_mpls *mpls_mask) | |
1808 | { | |
1809 | int err; | |
1810 | ||
1811 | if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask))) | |
1812 | return 0; | |
1813 | if (mpls_mask->mpls_ttl) { | |
1814 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL, | |
1815 | mpls_key->mpls_ttl); | |
1816 | if (err) | |
1817 | return err; | |
1818 | } | |
1819 | if (mpls_mask->mpls_tc) { | |
1820 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC, | |
1821 | mpls_key->mpls_tc); | |
1822 | if (err) | |
1823 | return err; | |
1824 | } | |
1825 | if (mpls_mask->mpls_label) { | |
1826 | err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL, | |
1827 | mpls_key->mpls_label); | |
1828 | if (err) | |
1829 | return err; | |
1830 | } | |
1831 | if (mpls_mask->mpls_bos) { | |
1832 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS, | |
1833 | mpls_key->mpls_bos); | |
1834 | if (err) | |
1835 | return err; | |
1836 | } | |
1837 | return 0; | |
1838 | } | |
1839 | ||
0e2c17b6 | 1840 | static int fl_dump_key_ip(struct sk_buff *skb, bool encap, |
4d80cc0a OG |
1841 | struct flow_dissector_key_ip *key, |
1842 | struct flow_dissector_key_ip *mask) | |
1843 | { | |
0e2c17b6 OG |
1844 | int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS; |
1845 | int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL; | |
1846 | int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK; | |
1847 | int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK; | |
1848 | ||
1849 | if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) || | |
1850 | fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl))) | |
4d80cc0a OG |
1851 | return -1; |
1852 | ||
1853 | return 0; | |
1854 | } | |
1855 | ||
9399ae9a | 1856 | static int fl_dump_key_vlan(struct sk_buff *skb, |
d64efd09 | 1857 | int vlan_id_key, int vlan_prio_key, |
9399ae9a HHZ |
1858 | struct flow_dissector_key_vlan *vlan_key, |
1859 | struct flow_dissector_key_vlan *vlan_mask) | |
1860 | { | |
1861 | int err; | |
1862 | ||
1863 | if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask))) | |
1864 | return 0; | |
1865 | if (vlan_mask->vlan_id) { | |
d64efd09 | 1866 | err = nla_put_u16(skb, vlan_id_key, |
9399ae9a HHZ |
1867 | vlan_key->vlan_id); |
1868 | if (err) | |
1869 | return err; | |
1870 | } | |
1871 | if (vlan_mask->vlan_priority) { | |
d64efd09 | 1872 | err = nla_put_u8(skb, vlan_prio_key, |
9399ae9a HHZ |
1873 | vlan_key->vlan_priority); |
1874 | if (err) | |
1875 | return err; | |
1876 | } | |
1877 | return 0; | |
1878 | } | |
1879 | ||
faa3ffce OG |
1880 | static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask, |
1881 | u32 *flower_key, u32 *flower_mask, | |
1882 | u32 flower_flag_bit, u32 dissector_flag_bit) | |
1883 | { | |
1884 | if (dissector_mask & dissector_flag_bit) { | |
1885 | *flower_mask |= flower_flag_bit; | |
1886 | if (dissector_key & dissector_flag_bit) | |
1887 | *flower_key |= flower_flag_bit; | |
1888 | } | |
1889 | } | |
1890 | ||
1891 | static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask) | |
1892 | { | |
1893 | u32 key, mask; | |
1894 | __be32 _key, _mask; | |
1895 | int err; | |
1896 | ||
1897 | if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask))) | |
1898 | return 0; | |
1899 | ||
1900 | key = 0; | |
1901 | mask = 0; | |
1902 | ||
1903 | fl_get_key_flag(flags_key, flags_mask, &key, &mask, | |
1904 | TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); | |
459d153d PJV |
1905 | fl_get_key_flag(flags_key, flags_mask, &key, &mask, |
1906 | TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, | |
1907 | FLOW_DIS_FIRST_FRAG); | |
faa3ffce OG |
1908 | |
1909 | _key = cpu_to_be32(key); | |
1910 | _mask = cpu_to_be32(mask); | |
1911 | ||
1912 | err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key); | |
1913 | if (err) | |
1914 | return err; | |
1915 | ||
1916 | return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask); | |
1917 | } | |
1918 | ||
0a6e7778 PJV |
1919 | static int fl_dump_key_geneve_opt(struct sk_buff *skb, |
1920 | struct flow_dissector_key_enc_opts *enc_opts) | |
1921 | { | |
1922 | struct geneve_opt *opt; | |
1923 | struct nlattr *nest; | |
1924 | int opt_off = 0; | |
1925 | ||
1926 | nest = nla_nest_start(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE); | |
1927 | if (!nest) | |
1928 | goto nla_put_failure; | |
1929 | ||
1930 | while (enc_opts->len > opt_off) { | |
1931 | opt = (struct geneve_opt *)&enc_opts->data[opt_off]; | |
1932 | ||
1933 | if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS, | |
1934 | opt->opt_class)) | |
1935 | goto nla_put_failure; | |
1936 | if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE, | |
1937 | opt->type)) | |
1938 | goto nla_put_failure; | |
1939 | if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA, | |
1940 | opt->length * 4, opt->opt_data)) | |
1941 | goto nla_put_failure; | |
1942 | ||
1943 | opt_off += sizeof(struct geneve_opt) + opt->length * 4; | |
1944 | } | |
1945 | nla_nest_end(skb, nest); | |
1946 | return 0; | |
1947 | ||
1948 | nla_put_failure: | |
1949 | nla_nest_cancel(skb, nest); | |
1950 | return -EMSGSIZE; | |
1951 | } | |
1952 | ||
1953 | static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type, | |
1954 | struct flow_dissector_key_enc_opts *enc_opts) | |
1955 | { | |
1956 | struct nlattr *nest; | |
1957 | int err; | |
1958 | ||
1959 | if (!enc_opts->len) | |
1960 | return 0; | |
1961 | ||
1962 | nest = nla_nest_start(skb, enc_opt_type); | |
1963 | if (!nest) | |
1964 | goto nla_put_failure; | |
1965 | ||
1966 | switch (enc_opts->dst_opt_type) { | |
1967 | case TUNNEL_GENEVE_OPT: | |
1968 | err = fl_dump_key_geneve_opt(skb, enc_opts); | |
1969 | if (err) | |
1970 | goto nla_put_failure; | |
1971 | break; | |
1972 | default: | |
1973 | goto nla_put_failure; | |
1974 | } | |
1975 | nla_nest_end(skb, nest); | |
1976 | return 0; | |
1977 | ||
1978 | nla_put_failure: | |
1979 | nla_nest_cancel(skb, nest); | |
1980 | return -EMSGSIZE; | |
1981 | } | |
1982 | ||
1983 | static int fl_dump_key_enc_opt(struct sk_buff *skb, | |
1984 | struct flow_dissector_key_enc_opts *key_opts, | |
1985 | struct flow_dissector_key_enc_opts *msk_opts) | |
1986 | { | |
1987 | int err; | |
1988 | ||
1989 | err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts); | |
1990 | if (err) | |
1991 | return err; | |
1992 | ||
1993 | return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts); | |
1994 | } | |
1995 | ||
f5749081 JP |
1996 | static int fl_dump_key(struct sk_buff *skb, struct net *net, |
1997 | struct fl_flow_key *key, struct fl_flow_key *mask) | |
77b9900e | 1998 | { |
77b9900e JP |
1999 | if (mask->indev_ifindex) { |
2000 | struct net_device *dev; | |
2001 | ||
2002 | dev = __dev_get_by_index(net, key->indev_ifindex); | |
2003 | if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name)) | |
2004 | goto nla_put_failure; | |
2005 | } | |
2006 | ||
2007 | if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, | |
2008 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, | |
2009 | sizeof(key->eth.dst)) || | |
2010 | fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, | |
2011 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, | |
2012 | sizeof(key->eth.src)) || | |
2013 | fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE, | |
2014 | &mask->basic.n_proto, TCA_FLOWER_UNSPEC, | |
2015 | sizeof(key->basic.n_proto))) | |
2016 | goto nla_put_failure; | |
9399ae9a | 2017 | |
a577d8f7 BL |
2018 | if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls)) |
2019 | goto nla_put_failure; | |
2020 | ||
d64efd09 JL |
2021 | if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID, |
2022 | TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan)) | |
9399ae9a HHZ |
2023 | goto nla_put_failure; |
2024 | ||
d64efd09 JL |
2025 | if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID, |
2026 | TCA_FLOWER_KEY_CVLAN_PRIO, | |
2027 | &key->cvlan, &mask->cvlan) || | |
2028 | (mask->cvlan.vlan_tpid && | |
158abbf1 JL |
2029 | nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, |
2030 | key->cvlan.vlan_tpid))) | |
d3069512 JL |
2031 | goto nla_put_failure; |
2032 | ||
5e9a0fe4 JL |
2033 | if (mask->basic.n_proto) { |
2034 | if (mask->cvlan.vlan_tpid) { | |
2035 | if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE, | |
2036 | key->basic.n_proto)) | |
2037 | goto nla_put_failure; | |
2038 | } else if (mask->vlan.vlan_tpid) { | |
2039 | if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, | |
2040 | key->basic.n_proto)) | |
2041 | goto nla_put_failure; | |
2042 | } | |
d64efd09 JL |
2043 | } |
2044 | ||
77b9900e JP |
2045 | if ((key->basic.n_proto == htons(ETH_P_IP) || |
2046 | key->basic.n_proto == htons(ETH_P_IPV6)) && | |
4d80cc0a | 2047 | (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, |
77b9900e | 2048 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, |
4d80cc0a | 2049 | sizeof(key->basic.ip_proto)) || |
0e2c17b6 | 2050 | fl_dump_key_ip(skb, false, &key->ip, &mask->ip))) |
77b9900e JP |
2051 | goto nla_put_failure; |
2052 | ||
c3f83241 | 2053 | if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
77b9900e JP |
2054 | (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
2055 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, | |
2056 | sizeof(key->ipv4.src)) || | |
2057 | fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, | |
2058 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, | |
2059 | sizeof(key->ipv4.dst)))) | |
2060 | goto nla_put_failure; | |
c3f83241 | 2061 | else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && |
77b9900e JP |
2062 | (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
2063 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, | |
2064 | sizeof(key->ipv6.src)) || | |
2065 | fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, | |
2066 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, | |
2067 | sizeof(key->ipv6.dst)))) | |
2068 | goto nla_put_failure; | |
2069 | ||
2070 | if (key->basic.ip_proto == IPPROTO_TCP && | |
2071 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, | |
aa72d708 | 2072 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
77b9900e JP |
2073 | sizeof(key->tp.src)) || |
2074 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, | |
aa72d708 | 2075 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
fdfc7dd6 JP |
2076 | sizeof(key->tp.dst)) || |
2077 | fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, | |
2078 | &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, | |
2079 | sizeof(key->tcp.flags)))) | |
77b9900e JP |
2080 | goto nla_put_failure; |
2081 | else if (key->basic.ip_proto == IPPROTO_UDP && | |
2082 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, | |
aa72d708 | 2083 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
77b9900e JP |
2084 | sizeof(key->tp.src)) || |
2085 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, | |
aa72d708 | 2086 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
5976c5f4 SH |
2087 | sizeof(key->tp.dst)))) |
2088 | goto nla_put_failure; | |
2089 | else if (key->basic.ip_proto == IPPROTO_SCTP && | |
2090 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, | |
2091 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, | |
2092 | sizeof(key->tp.src)) || | |
2093 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, | |
2094 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, | |
77b9900e JP |
2095 | sizeof(key->tp.dst)))) |
2096 | goto nla_put_failure; | |
7b684884 SH |
2097 | else if (key->basic.n_proto == htons(ETH_P_IP) && |
2098 | key->basic.ip_proto == IPPROTO_ICMP && | |
2099 | (fl_dump_key_val(skb, &key->icmp.type, | |
2100 | TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type, | |
2101 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, | |
2102 | sizeof(key->icmp.type)) || | |
2103 | fl_dump_key_val(skb, &key->icmp.code, | |
2104 | TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code, | |
2105 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK, | |
2106 | sizeof(key->icmp.code)))) | |
2107 | goto nla_put_failure; | |
2108 | else if (key->basic.n_proto == htons(ETH_P_IPV6) && | |
2109 | key->basic.ip_proto == IPPROTO_ICMPV6 && | |
2110 | (fl_dump_key_val(skb, &key->icmp.type, | |
2111 | TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type, | |
2112 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, | |
2113 | sizeof(key->icmp.type)) || | |
2114 | fl_dump_key_val(skb, &key->icmp.code, | |
2115 | TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code, | |
2116 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK, | |
2117 | sizeof(key->icmp.code)))) | |
2118 | goto nla_put_failure; | |
99d31326 SH |
2119 | else if ((key->basic.n_proto == htons(ETH_P_ARP) || |
2120 | key->basic.n_proto == htons(ETH_P_RARP)) && | |
2121 | (fl_dump_key_val(skb, &key->arp.sip, | |
2122 | TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip, | |
2123 | TCA_FLOWER_KEY_ARP_SIP_MASK, | |
2124 | sizeof(key->arp.sip)) || | |
2125 | fl_dump_key_val(skb, &key->arp.tip, | |
2126 | TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip, | |
2127 | TCA_FLOWER_KEY_ARP_TIP_MASK, | |
2128 | sizeof(key->arp.tip)) || | |
2129 | fl_dump_key_val(skb, &key->arp.op, | |
2130 | TCA_FLOWER_KEY_ARP_OP, &mask->arp.op, | |
2131 | TCA_FLOWER_KEY_ARP_OP_MASK, | |
2132 | sizeof(key->arp.op)) || | |
2133 | fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, | |
2134 | mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, | |
2135 | sizeof(key->arp.sha)) || | |
2136 | fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, | |
2137 | mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, | |
2138 | sizeof(key->arp.tha)))) | |
2139 | goto nla_put_failure; | |
77b9900e | 2140 | |
5c72299f AN |
2141 | if ((key->basic.ip_proto == IPPROTO_TCP || |
2142 | key->basic.ip_proto == IPPROTO_UDP || | |
2143 | key->basic.ip_proto == IPPROTO_SCTP) && | |
2144 | fl_dump_key_port_range(skb, key, mask)) | |
2145 | goto nla_put_failure; | |
2146 | ||
bc3103f1 AV |
2147 | if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
2148 | (fl_dump_key_val(skb, &key->enc_ipv4.src, | |
2149 | TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src, | |
2150 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, | |
2151 | sizeof(key->enc_ipv4.src)) || | |
2152 | fl_dump_key_val(skb, &key->enc_ipv4.dst, | |
2153 | TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst, | |
2154 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, | |
2155 | sizeof(key->enc_ipv4.dst)))) | |
2156 | goto nla_put_failure; | |
2157 | else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && | |
2158 | (fl_dump_key_val(skb, &key->enc_ipv6.src, | |
2159 | TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src, | |
2160 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, | |
2161 | sizeof(key->enc_ipv6.src)) || | |
2162 | fl_dump_key_val(skb, &key->enc_ipv6.dst, | |
2163 | TCA_FLOWER_KEY_ENC_IPV6_DST, | |
2164 | &mask->enc_ipv6.dst, | |
2165 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, | |
2166 | sizeof(key->enc_ipv6.dst)))) | |
2167 | goto nla_put_failure; | |
2168 | ||
2169 | if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID, | |
eb523f42 | 2170 | &mask->enc_key_id, TCA_FLOWER_UNSPEC, |
f4d997fd HHZ |
2171 | sizeof(key->enc_key_id)) || |
2172 | fl_dump_key_val(skb, &key->enc_tp.src, | |
2173 | TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, | |
2174 | &mask->enc_tp.src, | |
2175 | TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, | |
2176 | sizeof(key->enc_tp.src)) || | |
2177 | fl_dump_key_val(skb, &key->enc_tp.dst, | |
2178 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT, | |
2179 | &mask->enc_tp.dst, | |
2180 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, | |
0e2c17b6 | 2181 | sizeof(key->enc_tp.dst)) || |
0a6e7778 PJV |
2182 | fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) || |
2183 | fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts)) | |
bc3103f1 AV |
2184 | goto nla_put_failure; |
2185 | ||
faa3ffce OG |
2186 | if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags)) |
2187 | goto nla_put_failure; | |
2188 | ||
f5749081 JP |
2189 | return 0; |
2190 | ||
2191 | nla_put_failure: | |
2192 | return -EMSGSIZE; | |
2193 | } | |
2194 | ||
2195 | static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh, | |
12db03b6 | 2196 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
f5749081 JP |
2197 | { |
2198 | struct cls_fl_filter *f = fh; | |
2199 | struct nlattr *nest; | |
2200 | struct fl_flow_key *key, *mask; | |
2201 | ||
2202 | if (!f) | |
2203 | return skb->len; | |
2204 | ||
2205 | t->tcm_handle = f->handle; | |
2206 | ||
2207 | nest = nla_nest_start(skb, TCA_OPTIONS); | |
2208 | if (!nest) | |
2209 | goto nla_put_failure; | |
2210 | ||
2211 | if (f->res.classid && | |
2212 | nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid)) | |
2213 | goto nla_put_failure; | |
2214 | ||
2215 | key = &f->key; | |
2216 | mask = &f->mask->key; | |
2217 | ||
2218 | if (fl_dump_key(skb, net, key, mask)) | |
2219 | goto nla_put_failure; | |
2220 | ||
2221 | if (!tc_skip_hw(f->flags)) | |
2222 | fl_hw_update_stats(tp, f); | |
2223 | ||
749e6720 OG |
2224 | if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags)) |
2225 | goto nla_put_failure; | |
e69985c6 | 2226 | |
86c55361 VB |
2227 | if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count)) |
2228 | goto nla_put_failure; | |
2229 | ||
77b9900e JP |
2230 | if (tcf_exts_dump(skb, &f->exts)) |
2231 | goto nla_put_failure; | |
2232 | ||
2233 | nla_nest_end(skb, nest); | |
2234 | ||
2235 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) | |
2236 | goto nla_put_failure; | |
2237 | ||
2238 | return skb->len; | |
2239 | ||
2240 | nla_put_failure: | |
2241 | nla_nest_cancel(skb, nest); | |
2242 | return -1; | |
2243 | } | |
2244 | ||
b95ec7eb JP |
2245 | static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv) |
2246 | { | |
2247 | struct fl_flow_tmplt *tmplt = tmplt_priv; | |
2248 | struct fl_flow_key *key, *mask; | |
2249 | struct nlattr *nest; | |
2250 | ||
2251 | nest = nla_nest_start(skb, TCA_OPTIONS); | |
2252 | if (!nest) | |
2253 | goto nla_put_failure; | |
2254 | ||
2255 | key = &tmplt->dummy_key; | |
2256 | mask = &tmplt->mask; | |
2257 | ||
2258 | if (fl_dump_key(skb, net, key, mask)) | |
2259 | goto nla_put_failure; | |
2260 | ||
2261 | nla_nest_end(skb, nest); | |
2262 | ||
2263 | return skb->len; | |
2264 | ||
2265 | nla_put_failure: | |
2266 | nla_nest_cancel(skb, nest); | |
2267 | return -EMSGSIZE; | |
2268 | } | |
2269 | ||
07d79fc7 CW |
2270 | static void fl_bind_class(void *fh, u32 classid, unsigned long cl) |
2271 | { | |
2272 | struct cls_fl_filter *f = fh; | |
2273 | ||
2274 | if (f && f->res.classid == classid) | |
2275 | f->res.class = cl; | |
2276 | } | |
2277 | ||
77b9900e JP |
2278 | static struct tcf_proto_ops cls_fl_ops __read_mostly = { |
2279 | .kind = "flower", | |
2280 | .classify = fl_classify, | |
2281 | .init = fl_init, | |
2282 | .destroy = fl_destroy, | |
2283 | .get = fl_get, | |
06177558 | 2284 | .put = fl_put, |
77b9900e JP |
2285 | .change = fl_change, |
2286 | .delete = fl_delete, | |
2287 | .walk = fl_walk, | |
31533cba | 2288 | .reoffload = fl_reoffload, |
77b9900e | 2289 | .dump = fl_dump, |
07d79fc7 | 2290 | .bind_class = fl_bind_class, |
b95ec7eb JP |
2291 | .tmplt_create = fl_tmplt_create, |
2292 | .tmplt_destroy = fl_tmplt_destroy, | |
2293 | .tmplt_dump = fl_tmplt_dump, | |
77b9900e JP |
2294 | .owner = THIS_MODULE, |
2295 | }; | |
2296 | ||
2297 | static int __init cls_fl_init(void) | |
2298 | { | |
2299 | return register_tcf_proto_ops(&cls_fl_ops); | |
2300 | } | |
2301 | ||
2302 | static void __exit cls_fl_exit(void) | |
2303 | { | |
2304 | unregister_tcf_proto_ops(&cls_fl_ops); | |
2305 | } | |
2306 | ||
2307 | module_init(cls_fl_init); | |
2308 | module_exit(cls_fl_exit); | |
2309 | ||
2310 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); | |
2311 | MODULE_DESCRIPTION("Flower classifier"); | |
2312 | MODULE_LICENSE("GPL v2"); |