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