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> | |
ec624fe7 | 22 | #include <net/pkt_sched.h> |
77b9900e JP |
23 | #include <net/ip.h> |
24 | #include <net/flow_dissector.h> | |
0a6e7778 | 25 | #include <net/geneve.h> |
d8f9dfae | 26 | #include <net/vxlan.h> |
79b1011c | 27 | #include <net/erspan.h> |
77b9900e | 28 | |
bc3103f1 AV |
29 | #include <net/dst.h> |
30 | #include <net/dst_metadata.h> | |
31 | ||
e0ace68a PB |
32 | #include <uapi/linux/netfilter/nf_conntrack_common.h> |
33 | ||
1bcc51ac | 34 | #define TCA_FLOWER_KEY_CT_FLAGS_MAX \ |
35 | ((__TCA_FLOWER_KEY_CT_FLAGS_MAX - 1) << 1) | |
36 | #define TCA_FLOWER_KEY_CT_FLAGS_MASK \ | |
37 | (TCA_FLOWER_KEY_CT_FLAGS_MAX - 1) | |
38 | ||
77b9900e | 39 | struct fl_flow_key { |
8212ed77 | 40 | struct flow_dissector_key_meta meta; |
42aecaa9 | 41 | struct flow_dissector_key_control control; |
bc3103f1 | 42 | struct flow_dissector_key_control enc_control; |
77b9900e JP |
43 | struct flow_dissector_key_basic basic; |
44 | struct flow_dissector_key_eth_addrs eth; | |
9399ae9a | 45 | struct flow_dissector_key_vlan vlan; |
d64efd09 | 46 | struct flow_dissector_key_vlan cvlan; |
77b9900e | 47 | union { |
c3f83241 | 48 | struct flow_dissector_key_ipv4_addrs ipv4; |
77b9900e JP |
49 | struct flow_dissector_key_ipv6_addrs ipv6; |
50 | }; | |
51 | struct flow_dissector_key_ports tp; | |
7b684884 | 52 | struct flow_dissector_key_icmp icmp; |
99d31326 | 53 | struct flow_dissector_key_arp arp; |
bc3103f1 AV |
54 | struct flow_dissector_key_keyid enc_key_id; |
55 | union { | |
56 | struct flow_dissector_key_ipv4_addrs enc_ipv4; | |
57 | struct flow_dissector_key_ipv6_addrs enc_ipv6; | |
58 | }; | |
f4d997fd | 59 | struct flow_dissector_key_ports enc_tp; |
a577d8f7 | 60 | struct flow_dissector_key_mpls mpls; |
fdfc7dd6 | 61 | struct flow_dissector_key_tcp tcp; |
4d80cc0a | 62 | struct flow_dissector_key_ip ip; |
0e2c17b6 | 63 | struct flow_dissector_key_ip enc_ip; |
0a6e7778 | 64 | struct flow_dissector_key_enc_opts enc_opts; |
8ffb055b YK |
65 | union { |
66 | struct flow_dissector_key_ports tp; | |
67 | struct { | |
68 | struct flow_dissector_key_ports tp_min; | |
69 | struct flow_dissector_key_ports tp_max; | |
70 | }; | |
71 | } tp_range; | |
e0ace68a | 72 | struct flow_dissector_key_ct ct; |
5923b8f7 | 73 | struct flow_dissector_key_hash hash; |
77b9900e JP |
74 | } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ |
75 | ||
76 | struct fl_flow_mask_range { | |
77 | unsigned short int start; | |
78 | unsigned short int end; | |
79 | }; | |
80 | ||
81 | struct fl_flow_mask { | |
82 | struct fl_flow_key key; | |
83 | struct fl_flow_mask_range range; | |
5c72299f | 84 | u32 flags; |
05cd271f PB |
85 | struct rhash_head ht_node; |
86 | struct rhashtable ht; | |
87 | struct rhashtable_params filter_ht_params; | |
88 | struct flow_dissector dissector; | |
89 | struct list_head filters; | |
44a5cd43 | 90 | struct rcu_work rwork; |
05cd271f | 91 | struct list_head list; |
f48ef4d5 | 92 | refcount_t refcnt; |
77b9900e JP |
93 | }; |
94 | ||
b95ec7eb JP |
95 | struct fl_flow_tmplt { |
96 | struct fl_flow_key dummy_key; | |
97 | struct fl_flow_key mask; | |
98 | struct flow_dissector dissector; | |
99 | struct tcf_chain *chain; | |
100 | }; | |
101 | ||
77b9900e JP |
102 | struct cls_fl_head { |
103 | struct rhashtable ht; | |
259e60f9 | 104 | spinlock_t masks_lock; /* Protect masks list */ |
05cd271f | 105 | struct list_head masks; |
c049d56e | 106 | struct list_head hw_filters; |
aaa908ff | 107 | struct rcu_work rwork; |
c15ab236 | 108 | struct idr handle_idr; |
77b9900e JP |
109 | }; |
110 | ||
111 | struct cls_fl_filter { | |
05cd271f | 112 | struct fl_flow_mask *mask; |
77b9900e JP |
113 | struct rhash_head ht_node; |
114 | struct fl_flow_key mkey; | |
115 | struct tcf_exts exts; | |
116 | struct tcf_result res; | |
117 | struct fl_flow_key key; | |
118 | struct list_head list; | |
c049d56e | 119 | struct list_head hw_list; |
77b9900e | 120 | u32 handle; |
e69985c6 | 121 | u32 flags; |
86c55361 | 122 | u32 in_hw_count; |
aaa908ff | 123 | struct rcu_work rwork; |
7091d8c7 | 124 | struct net_device *hw_dev; |
06177558 VB |
125 | /* Flower classifier is unlocked, which means that its reference counter |
126 | * can be changed concurrently without any kind of external | |
127 | * synchronization. Use atomic reference counter to be concurrency-safe. | |
128 | */ | |
129 | refcount_t refcnt; | |
b2552b8c | 130 | bool deleted; |
77b9900e JP |
131 | }; |
132 | ||
05cd271f PB |
133 | static const struct rhashtable_params mask_ht_params = { |
134 | .key_offset = offsetof(struct fl_flow_mask, key), | |
135 | .key_len = sizeof(struct fl_flow_key), | |
136 | .head_offset = offsetof(struct fl_flow_mask, ht_node), | |
137 | .automatic_shrinking = true, | |
138 | }; | |
139 | ||
77b9900e JP |
140 | static unsigned short int fl_mask_range(const struct fl_flow_mask *mask) |
141 | { | |
142 | return mask->range.end - mask->range.start; | |
143 | } | |
144 | ||
145 | static void fl_mask_update_range(struct fl_flow_mask *mask) | |
146 | { | |
147 | const u8 *bytes = (const u8 *) &mask->key; | |
148 | size_t size = sizeof(mask->key); | |
05cd271f | 149 | size_t i, first = 0, last; |
77b9900e | 150 | |
05cd271f PB |
151 | for (i = 0; i < size; i++) { |
152 | if (bytes[i]) { | |
153 | first = i; | |
154 | break; | |
155 | } | |
156 | } | |
157 | last = first; | |
158 | for (i = size - 1; i != first; i--) { | |
77b9900e | 159 | if (bytes[i]) { |
77b9900e | 160 | last = i; |
05cd271f | 161 | break; |
77b9900e JP |
162 | } |
163 | } | |
164 | mask->range.start = rounddown(first, sizeof(long)); | |
165 | mask->range.end = roundup(last + 1, sizeof(long)); | |
166 | } | |
167 | ||
168 | static void *fl_key_get_start(struct fl_flow_key *key, | |
169 | const struct fl_flow_mask *mask) | |
170 | { | |
171 | return (u8 *) key + mask->range.start; | |
172 | } | |
173 | ||
174 | static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key, | |
175 | struct fl_flow_mask *mask) | |
176 | { | |
177 | const long *lkey = fl_key_get_start(key, mask); | |
178 | const long *lmask = fl_key_get_start(&mask->key, mask); | |
179 | long *lmkey = fl_key_get_start(mkey, mask); | |
180 | int i; | |
181 | ||
182 | for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) | |
183 | *lmkey++ = *lkey++ & *lmask++; | |
184 | } | |
185 | ||
b95ec7eb JP |
186 | static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt, |
187 | struct fl_flow_mask *mask) | |
188 | { | |
189 | const long *lmask = fl_key_get_start(&mask->key, mask); | |
190 | const long *ltmplt; | |
191 | int i; | |
192 | ||
193 | if (!tmplt) | |
194 | return true; | |
195 | ltmplt = fl_key_get_start(&tmplt->mask, mask); | |
196 | for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) { | |
197 | if (~*ltmplt++ & *lmask++) | |
198 | return false; | |
199 | } | |
200 | return true; | |
201 | } | |
202 | ||
77b9900e JP |
203 | static void fl_clear_masked_range(struct fl_flow_key *key, |
204 | struct fl_flow_mask *mask) | |
205 | { | |
206 | memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask)); | |
207 | } | |
208 | ||
5c72299f AN |
209 | static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter, |
210 | struct fl_flow_key *key, | |
211 | struct fl_flow_key *mkey) | |
212 | { | |
6215afcb | 213 | u16 min_mask, max_mask, min_val, max_val; |
5c72299f | 214 | |
6215afcb VO |
215 | min_mask = ntohs(filter->mask->key.tp_range.tp_min.dst); |
216 | max_mask = ntohs(filter->mask->key.tp_range.tp_max.dst); | |
217 | min_val = ntohs(filter->key.tp_range.tp_min.dst); | |
218 | max_val = ntohs(filter->key.tp_range.tp_max.dst); | |
5c72299f AN |
219 | |
220 | if (min_mask && max_mask) { | |
6215afcb VO |
221 | if (ntohs(key->tp_range.tp.dst) < min_val || |
222 | ntohs(key->tp_range.tp.dst) > max_val) | |
5c72299f AN |
223 | return false; |
224 | ||
225 | /* skb does not have min and max values */ | |
8ffb055b YK |
226 | mkey->tp_range.tp_min.dst = filter->mkey.tp_range.tp_min.dst; |
227 | mkey->tp_range.tp_max.dst = filter->mkey.tp_range.tp_max.dst; | |
5c72299f AN |
228 | } |
229 | return true; | |
230 | } | |
231 | ||
232 | static bool fl_range_port_src_cmp(struct cls_fl_filter *filter, | |
233 | struct fl_flow_key *key, | |
234 | struct fl_flow_key *mkey) | |
235 | { | |
6215afcb | 236 | u16 min_mask, max_mask, min_val, max_val; |
5c72299f | 237 | |
6215afcb VO |
238 | min_mask = ntohs(filter->mask->key.tp_range.tp_min.src); |
239 | max_mask = ntohs(filter->mask->key.tp_range.tp_max.src); | |
240 | min_val = ntohs(filter->key.tp_range.tp_min.src); | |
241 | max_val = ntohs(filter->key.tp_range.tp_max.src); | |
5c72299f AN |
242 | |
243 | if (min_mask && max_mask) { | |
6215afcb VO |
244 | if (ntohs(key->tp_range.tp.src) < min_val || |
245 | ntohs(key->tp_range.tp.src) > max_val) | |
5c72299f AN |
246 | return false; |
247 | ||
248 | /* skb does not have min and max values */ | |
8ffb055b YK |
249 | mkey->tp_range.tp_min.src = filter->mkey.tp_range.tp_min.src; |
250 | mkey->tp_range.tp_max.src = filter->mkey.tp_range.tp_max.src; | |
5c72299f AN |
251 | } |
252 | return true; | |
253 | } | |
254 | ||
255 | static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask, | |
256 | struct fl_flow_key *mkey) | |
a3308d8f | 257 | { |
05cd271f PB |
258 | return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask), |
259 | mask->filter_ht_params); | |
a3308d8f PB |
260 | } |
261 | ||
5c72299f AN |
262 | static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask, |
263 | struct fl_flow_key *mkey, | |
264 | struct fl_flow_key *key) | |
265 | { | |
266 | struct cls_fl_filter *filter, *f; | |
267 | ||
268 | list_for_each_entry_rcu(filter, &mask->filters, list) { | |
269 | if (!fl_range_port_dst_cmp(filter, key, mkey)) | |
270 | continue; | |
271 | ||
272 | if (!fl_range_port_src_cmp(filter, key, mkey)) | |
273 | continue; | |
274 | ||
275 | f = __fl_lookup(mask, mkey); | |
276 | if (f) | |
277 | return f; | |
278 | } | |
279 | return NULL; | |
280 | } | |
281 | ||
0af413bd AB |
282 | static noinline_for_stack |
283 | struct cls_fl_filter *fl_mask_lookup(struct fl_flow_mask *mask, struct fl_flow_key *key) | |
5c72299f | 284 | { |
0af413bd AB |
285 | struct fl_flow_key mkey; |
286 | ||
287 | fl_set_masked_key(&mkey, key, mask); | |
5c72299f | 288 | if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE)) |
0af413bd | 289 | return fl_lookup_range(mask, &mkey, key); |
5c72299f | 290 | |
0af413bd | 291 | return __fl_lookup(mask, &mkey); |
5c72299f AN |
292 | } |
293 | ||
e0ace68a PB |
294 | static u16 fl_ct_info_to_flower_map[] = { |
295 | [IP_CT_ESTABLISHED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | | |
296 | TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED, | |
297 | [IP_CT_RELATED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | | |
298 | TCA_FLOWER_KEY_CT_FLAGS_RELATED, | |
299 | [IP_CT_ESTABLISHED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | | |
8c85d18c PB |
300 | TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED | |
301 | TCA_FLOWER_KEY_CT_FLAGS_REPLY, | |
e0ace68a | 302 | [IP_CT_RELATED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | |
8c85d18c PB |
303 | TCA_FLOWER_KEY_CT_FLAGS_RELATED | |
304 | TCA_FLOWER_KEY_CT_FLAGS_REPLY, | |
e0ace68a PB |
305 | [IP_CT_NEW] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | |
306 | TCA_FLOWER_KEY_CT_FLAGS_NEW, | |
307 | }; | |
308 | ||
77b9900e JP |
309 | static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
310 | struct tcf_result *res) | |
311 | { | |
312 | struct cls_fl_head *head = rcu_dereference_bh(tp->root); | |
ec624fe7 | 313 | bool post_ct = tc_skb_cb(skb)->post_ct; |
38495958 | 314 | u16 zone = tc_skb_cb(skb)->zone; |
e0ace68a PB |
315 | struct fl_flow_key skb_key; |
316 | struct fl_flow_mask *mask; | |
317 | struct cls_fl_filter *f; | |
77b9900e | 318 | |
05cd271f | 319 | list_for_each_entry_rcu(mask, &head->masks, list) { |
8a9093c7 | 320 | flow_dissector_init_keys(&skb_key.control, &skb_key.basic); |
05cd271f | 321 | fl_clear_masked_range(&skb_key, mask); |
bc3103f1 | 322 | |
8212ed77 | 323 | skb_flow_dissect_meta(skb, &mask->dissector, &skb_key); |
05cd271f PB |
324 | /* skb_flow_dissect() does not set n_proto in case an unknown |
325 | * protocol, so do it rather here. | |
326 | */ | |
d7bf2ebe | 327 | skb_key.basic.n_proto = skb_protocol(skb, false); |
05cd271f | 328 | skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key); |
e0ace68a PB |
329 | skb_flow_dissect_ct(skb, &mask->dissector, &skb_key, |
330 | fl_ct_info_to_flower_map, | |
7baf2429 | 331 | ARRAY_SIZE(fl_ct_info_to_flower_map), |
38495958 | 332 | post_ct, zone); |
5923b8f7 | 333 | skb_flow_dissect_hash(skb, &mask->dissector, &skb_key); |
6de6e46d YK |
334 | skb_flow_dissect(skb, &mask->dissector, &skb_key, |
335 | FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP); | |
77b9900e | 336 | |
0af413bd | 337 | f = fl_mask_lookup(mask, &skb_key); |
05cd271f PB |
338 | if (f && !tc_skip_sw(f->flags)) { |
339 | *res = f->res; | |
340 | return tcf_exts_exec(skb, &f->exts, res); | |
341 | } | |
77b9900e JP |
342 | } |
343 | return -1; | |
344 | } | |
345 | ||
346 | static int fl_init(struct tcf_proto *tp) | |
347 | { | |
348 | struct cls_fl_head *head; | |
349 | ||
350 | head = kzalloc(sizeof(*head), GFP_KERNEL); | |
351 | if (!head) | |
352 | return -ENOBUFS; | |
353 | ||
259e60f9 | 354 | spin_lock_init(&head->masks_lock); |
05cd271f | 355 | INIT_LIST_HEAD_RCU(&head->masks); |
c049d56e | 356 | INIT_LIST_HEAD(&head->hw_filters); |
77b9900e | 357 | rcu_assign_pointer(tp->root, head); |
c15ab236 | 358 | idr_init(&head->handle_idr); |
77b9900e | 359 | |
05cd271f PB |
360 | return rhashtable_init(&head->ht, &mask_ht_params); |
361 | } | |
362 | ||
99815f50 | 363 | static void fl_mask_free(struct fl_flow_mask *mask, bool mask_init_done) |
44a5cd43 | 364 | { |
99815f50 VB |
365 | /* temporary masks don't have their filters list and ht initialized */ |
366 | if (mask_init_done) { | |
367 | WARN_ON(!list_empty(&mask->filters)); | |
368 | rhashtable_destroy(&mask->ht); | |
369 | } | |
44a5cd43 PA |
370 | kfree(mask); |
371 | } | |
372 | ||
373 | static void fl_mask_free_work(struct work_struct *work) | |
374 | { | |
375 | struct fl_flow_mask *mask = container_of(to_rcu_work(work), | |
376 | struct fl_flow_mask, rwork); | |
377 | ||
99815f50 VB |
378 | fl_mask_free(mask, true); |
379 | } | |
380 | ||
381 | static void fl_uninit_mask_free_work(struct work_struct *work) | |
382 | { | |
383 | struct fl_flow_mask *mask = container_of(to_rcu_work(work), | |
384 | struct fl_flow_mask, rwork); | |
385 | ||
386 | fl_mask_free(mask, false); | |
44a5cd43 PA |
387 | } |
388 | ||
9994677c | 389 | static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask) |
05cd271f | 390 | { |
f48ef4d5 | 391 | if (!refcount_dec_and_test(&mask->refcnt)) |
05cd271f PB |
392 | return false; |
393 | ||
394 | rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params); | |
259e60f9 VB |
395 | |
396 | spin_lock(&head->masks_lock); | |
05cd271f | 397 | list_del_rcu(&mask->list); |
259e60f9 VB |
398 | spin_unlock(&head->masks_lock); |
399 | ||
9994677c | 400 | tcf_queue_work(&mask->rwork, fl_mask_free_work); |
05cd271f PB |
401 | |
402 | return true; | |
77b9900e JP |
403 | } |
404 | ||
c049d56e VB |
405 | static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp) |
406 | { | |
407 | /* Flower classifier only changes root pointer during init and destroy. | |
408 | * Users must obtain reference to tcf_proto instance before calling its | |
409 | * API, so tp->root pointer is protected from concurrent call to | |
410 | * fl_destroy() by reference counting. | |
411 | */ | |
412 | return rcu_dereference_raw(tp->root); | |
413 | } | |
414 | ||
0dadc117 CW |
415 | static void __fl_destroy_filter(struct cls_fl_filter *f) |
416 | { | |
417 | tcf_exts_destroy(&f->exts); | |
418 | tcf_exts_put_net(&f->exts); | |
419 | kfree(f); | |
420 | } | |
421 | ||
0552c8af | 422 | static void fl_destroy_filter_work(struct work_struct *work) |
77b9900e | 423 | { |
aaa908ff CW |
424 | struct cls_fl_filter *f = container_of(to_rcu_work(work), |
425 | struct cls_fl_filter, rwork); | |
77b9900e | 426 | |
0dadc117 | 427 | __fl_destroy_filter(f); |
0552c8af CW |
428 | } |
429 | ||
1b0f8037 | 430 | static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f, |
c24e43d8 | 431 | bool rtnl_held, struct netlink_ext_ack *extack) |
5b33f488 | 432 | { |
208c0f4b | 433 | struct tcf_block *block = tp->chain->block; |
f9e30088 | 434 | struct flow_cls_offload cls_flower = {}; |
5b33f488 | 435 | |
d6787147 | 436 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); |
f9e30088 | 437 | cls_flower.command = FLOW_CLS_DESTROY; |
de4784ca | 438 | cls_flower.cookie = (unsigned long) f; |
5b33f488 | 439 | |
40119211 | 440 | tc_setup_cb_destroy(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, false, |
918190f5 | 441 | &f->flags, &f->in_hw_count, rtnl_held); |
c24e43d8 | 442 | |
5b33f488 AV |
443 | } |
444 | ||
e8eb36cd | 445 | static int fl_hw_replace_filter(struct tcf_proto *tp, |
c24e43d8 | 446 | struct cls_fl_filter *f, bool rtnl_held, |
41002038 | 447 | struct netlink_ext_ack *extack) |
5b33f488 | 448 | { |
208c0f4b | 449 | struct tcf_block *block = tp->chain->block; |
f9e30088 | 450 | struct flow_cls_offload cls_flower = {}; |
717503b9 | 451 | bool skip_sw = tc_skip_sw(f->flags); |
c24e43d8 VB |
452 | int err = 0; |
453 | ||
e3ab786b | 454 | cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts)); |
918190f5 VB |
455 | if (!cls_flower.rule) |
456 | return -ENOMEM; | |
8f256622 | 457 | |
d6787147 | 458 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); |
f9e30088 | 459 | cls_flower.command = FLOW_CLS_REPLACE; |
de4784ca | 460 | cls_flower.cookie = (unsigned long) f; |
8f256622 PNA |
461 | cls_flower.rule->match.dissector = &f->mask->dissector; |
462 | cls_flower.rule->match.mask = &f->mask->key; | |
463 | cls_flower.rule->match.key = &f->mkey; | |
384c181e | 464 | cls_flower.classid = f->res.classid; |
5b33f488 | 465 | |
9c1c0e12 | 466 | err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts); |
3a7b6861 PNA |
467 | if (err) { |
468 | kfree(cls_flower.rule); | |
918190f5 | 469 | if (skip_sw) { |
1f15bb4f | 470 | NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); |
918190f5 VB |
471 | return err; |
472 | } | |
473 | return 0; | |
3a7b6861 PNA |
474 | } |
475 | ||
40119211 | 476 | err = tc_setup_cb_add(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, |
918190f5 | 477 | skip_sw, &f->flags, &f->in_hw_count, rtnl_held); |
9c1c0e12 | 478 | tc_cleanup_offload_action(&cls_flower.rule->action); |
8f256622 PNA |
479 | kfree(cls_flower.rule); |
480 | ||
40119211 | 481 | if (err) { |
918190f5 VB |
482 | fl_hw_destroy_filter(tp, f, rtnl_held, NULL); |
483 | return err; | |
717503b9 JP |
484 | } |
485 | ||
918190f5 VB |
486 | if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW)) |
487 | return -EINVAL; | |
c24e43d8 | 488 | |
918190f5 | 489 | return 0; |
5b33f488 AV |
490 | } |
491 | ||
c24e43d8 VB |
492 | static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f, |
493 | bool rtnl_held) | |
10cbc684 | 494 | { |
208c0f4b | 495 | struct tcf_block *block = tp->chain->block; |
f9e30088 | 496 | struct flow_cls_offload cls_flower = {}; |
10cbc684 | 497 | |
d6787147 | 498 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL); |
f9e30088 | 499 | cls_flower.command = FLOW_CLS_STATS; |
de4784ca | 500 | cls_flower.cookie = (unsigned long) f; |
384c181e | 501 | cls_flower.classid = f->res.classid; |
10cbc684 | 502 | |
918190f5 VB |
503 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, |
504 | rtnl_held); | |
3b1903ef | 505 | |
bcd64368 BZ |
506 | tcf_exts_hw_stats_update(&f->exts, cls_flower.stats.bytes, |
507 | cls_flower.stats.pkts, | |
508 | cls_flower.stats.drops, | |
509 | cls_flower.stats.lastused, | |
510 | cls_flower.stats.used_hw_stats, | |
511 | cls_flower.stats.used_hw_stats_valid); | |
10cbc684 AV |
512 | } |
513 | ||
06177558 VB |
514 | static void __fl_put(struct cls_fl_filter *f) |
515 | { | |
516 | if (!refcount_dec_and_test(&f->refcnt)) | |
517 | return; | |
518 | ||
519 | if (tcf_exts_get_net(&f->exts)) | |
520 | tcf_queue_work(&f->rwork, fl_destroy_filter_work); | |
521 | else | |
522 | __fl_destroy_filter(f); | |
523 | } | |
524 | ||
525 | static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle) | |
526 | { | |
527 | struct cls_fl_filter *f; | |
528 | ||
529 | rcu_read_lock(); | |
530 | f = idr_find(&head->handle_idr, handle); | |
531 | if (f && !refcount_inc_not_zero(&f->refcnt)) | |
532 | f = NULL; | |
533 | rcu_read_unlock(); | |
534 | ||
535 | return f; | |
536 | } | |
537 | ||
b2552b8c | 538 | static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f, |
c24e43d8 VB |
539 | bool *last, bool rtnl_held, |
540 | struct netlink_ext_ack *extack) | |
13fa876e | 541 | { |
e474619a | 542 | struct cls_fl_head *head = fl_head_dereference(tp); |
c15ab236 | 543 | |
b2552b8c VB |
544 | *last = false; |
545 | ||
3d81e711 VB |
546 | spin_lock(&tp->lock); |
547 | if (f->deleted) { | |
548 | spin_unlock(&tp->lock); | |
b2552b8c | 549 | return -ENOENT; |
3d81e711 | 550 | } |
b2552b8c VB |
551 | |
552 | f->deleted = true; | |
553 | rhashtable_remove_fast(&f->mask->ht, &f->ht_node, | |
554 | f->mask->filter_ht_params); | |
9c160941 | 555 | idr_remove(&head->handle_idr, f->handle); |
13fa876e | 556 | list_del_rcu(&f->list); |
3d81e711 VB |
557 | spin_unlock(&tp->lock); |
558 | ||
9994677c | 559 | *last = fl_mask_put(head, f->mask); |
79685219 | 560 | if (!tc_skip_hw(f->flags)) |
c24e43d8 | 561 | fl_hw_destroy_filter(tp, f, rtnl_held, extack); |
13fa876e | 562 | tcf_unbind_filter(tp, &f->res); |
06177558 | 563 | __fl_put(f); |
05cd271f | 564 | |
b2552b8c | 565 | return 0; |
13fa876e RD |
566 | } |
567 | ||
d9363774 DB |
568 | static void fl_destroy_sleepable(struct work_struct *work) |
569 | { | |
aaa908ff CW |
570 | struct cls_fl_head *head = container_of(to_rcu_work(work), |
571 | struct cls_fl_head, | |
572 | rwork); | |
de9dc650 PB |
573 | |
574 | rhashtable_destroy(&head->ht); | |
d9363774 DB |
575 | kfree(head); |
576 | module_put(THIS_MODULE); | |
577 | } | |
578 | ||
12db03b6 VB |
579 | static void fl_destroy(struct tcf_proto *tp, bool rtnl_held, |
580 | struct netlink_ext_ack *extack) | |
77b9900e | 581 | { |
e474619a | 582 | struct cls_fl_head *head = fl_head_dereference(tp); |
05cd271f | 583 | struct fl_flow_mask *mask, *next_mask; |
77b9900e | 584 | struct cls_fl_filter *f, *next; |
b2552b8c | 585 | bool last; |
77b9900e | 586 | |
05cd271f PB |
587 | list_for_each_entry_safe(mask, next_mask, &head->masks, list) { |
588 | list_for_each_entry_safe(f, next, &mask->filters, list) { | |
c24e43d8 | 589 | __fl_delete(tp, f, &last, rtnl_held, extack); |
b2552b8c | 590 | if (last) |
05cd271f PB |
591 | break; |
592 | } | |
593 | } | |
c15ab236 | 594 | idr_destroy(&head->handle_idr); |
d9363774 DB |
595 | |
596 | __module_get(THIS_MODULE); | |
aaa908ff | 597 | tcf_queue_work(&head->rwork, fl_destroy_sleepable); |
77b9900e JP |
598 | } |
599 | ||
06177558 VB |
600 | static void fl_put(struct tcf_proto *tp, void *arg) |
601 | { | |
602 | struct cls_fl_filter *f = arg; | |
603 | ||
604 | __fl_put(f); | |
605 | } | |
606 | ||
8113c095 | 607 | static void *fl_get(struct tcf_proto *tp, u32 handle) |
77b9900e | 608 | { |
e474619a | 609 | struct cls_fl_head *head = fl_head_dereference(tp); |
77b9900e | 610 | |
06177558 | 611 | return __fl_get(head, handle); |
77b9900e JP |
612 | } |
613 | ||
614 | static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = { | |
615 | [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC }, | |
616 | [TCA_FLOWER_CLASSID] = { .type = NLA_U32 }, | |
617 | [TCA_FLOWER_INDEV] = { .type = NLA_STRING, | |
618 | .len = IFNAMSIZ }, | |
619 | [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN }, | |
620 | [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN }, | |
621 | [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN }, | |
622 | [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN }, | |
623 | [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 }, | |
624 | [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 }, | |
625 | [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 }, | |
626 | [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 }, | |
627 | [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 }, | |
628 | [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 }, | |
629 | [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, | |
630 | [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, | |
631 | [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) }, | |
632 | [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, | |
633 | [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 }, | |
634 | [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 }, | |
b175c3a4 JHS |
635 | [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 }, |
636 | [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 }, | |
9399ae9a HHZ |
637 | [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 }, |
638 | [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 }, | |
639 | [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 }, | |
bc3103f1 AV |
640 | [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, |
641 | [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, | |
642 | [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 }, | |
643 | [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, | |
644 | [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 }, | |
645 | [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, | |
646 | [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, | |
647 | [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, | |
648 | [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, | |
aa72d708 OG |
649 | [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 }, |
650 | [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 }, | |
651 | [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 }, | |
652 | [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 }, | |
5976c5f4 SH |
653 | [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 }, |
654 | [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 }, | |
655 | [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 }, | |
656 | [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 }, | |
f4d997fd HHZ |
657 | [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 }, |
658 | [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 }, | |
659 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 }, | |
660 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 }, | |
faa3ffce OG |
661 | [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 }, |
662 | [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 }, | |
7b684884 SH |
663 | [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 }, |
664 | [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 }, | |
665 | [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 }, | |
666 | [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 }, | |
667 | [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 }, | |
668 | [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 }, | |
669 | [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 }, | |
670 | [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 }, | |
99d31326 SH |
671 | [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 }, |
672 | [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 }, | |
673 | [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 }, | |
674 | [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 }, | |
675 | [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 }, | |
676 | [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 }, | |
677 | [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN }, | |
678 | [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN }, | |
679 | [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN }, | |
680 | [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN }, | |
a577d8f7 BL |
681 | [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 }, |
682 | [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 }, | |
683 | [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 }, | |
684 | [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 }, | |
61aec25a | 685 | [TCA_FLOWER_KEY_MPLS_OPTS] = { .type = NLA_NESTED }, |
fdfc7dd6 JP |
686 | [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 }, |
687 | [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 }, | |
4d80cc0a OG |
688 | [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 }, |
689 | [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 }, | |
690 | [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 }, | |
691 | [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 }, | |
d64efd09 JL |
692 | [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 }, |
693 | [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 }, | |
694 | [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 }, | |
0e2c17b6 OG |
695 | [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 }, |
696 | [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 }, | |
697 | [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 }, | |
698 | [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 }, | |
0a6e7778 PJV |
699 | [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED }, |
700 | [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED }, | |
1bcc51ac | 701 | [TCA_FLOWER_KEY_CT_STATE] = |
702 | NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK), | |
703 | [TCA_FLOWER_KEY_CT_STATE_MASK] = | |
704 | NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK), | |
e0ace68a PB |
705 | [TCA_FLOWER_KEY_CT_ZONE] = { .type = NLA_U16 }, |
706 | [TCA_FLOWER_KEY_CT_ZONE_MASK] = { .type = NLA_U16 }, | |
707 | [TCA_FLOWER_KEY_CT_MARK] = { .type = NLA_U32 }, | |
708 | [TCA_FLOWER_KEY_CT_MARK_MASK] = { .type = NLA_U32 }, | |
709 | [TCA_FLOWER_KEY_CT_LABELS] = { .type = NLA_BINARY, | |
710 | .len = 128 / BITS_PER_BYTE }, | |
711 | [TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NLA_BINARY, | |
712 | .len = 128 / BITS_PER_BYTE }, | |
e2debf08 | 713 | [TCA_FLOWER_FLAGS] = { .type = NLA_U32 }, |
5923b8f7 AL |
714 | [TCA_FLOWER_KEY_HASH] = { .type = NLA_U32 }, |
715 | [TCA_FLOWER_KEY_HASH_MASK] = { .type = NLA_U32 }, | |
716 | ||
0a6e7778 PJV |
717 | }; |
718 | ||
719 | static const struct nla_policy | |
720 | enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = { | |
d8f9dfae XL |
721 | [TCA_FLOWER_KEY_ENC_OPTS_UNSPEC] = { |
722 | .strict_start_type = TCA_FLOWER_KEY_ENC_OPTS_VXLAN }, | |
0a6e7778 | 723 | [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED }, |
d8f9dfae | 724 | [TCA_FLOWER_KEY_ENC_OPTS_VXLAN] = { .type = NLA_NESTED }, |
79b1011c | 725 | [TCA_FLOWER_KEY_ENC_OPTS_ERSPAN] = { .type = NLA_NESTED }, |
0a6e7778 PJV |
726 | }; |
727 | ||
728 | static const struct nla_policy | |
729 | geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = { | |
730 | [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 }, | |
731 | [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 }, | |
732 | [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY, | |
733 | .len = 128 }, | |
77b9900e JP |
734 | }; |
735 | ||
d8f9dfae XL |
736 | static const struct nla_policy |
737 | vxlan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1] = { | |
738 | [TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP] = { .type = NLA_U32 }, | |
739 | }; | |
740 | ||
79b1011c XL |
741 | static const struct nla_policy |
742 | erspan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1] = { | |
743 | [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER] = { .type = NLA_U8 }, | |
744 | [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX] = { .type = NLA_U32 }, | |
745 | [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] = { .type = NLA_U8 }, | |
746 | [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID] = { .type = NLA_U8 }, | |
747 | }; | |
748 | ||
61aec25a GN |
749 | static const struct nla_policy |
750 | mpls_stack_entry_policy[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1] = { | |
751 | [TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH] = { .type = NLA_U8 }, | |
752 | [TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL] = { .type = NLA_U8 }, | |
753 | [TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS] = { .type = NLA_U8 }, | |
754 | [TCA_FLOWER_KEY_MPLS_OPT_LSE_TC] = { .type = NLA_U8 }, | |
755 | [TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL] = { .type = NLA_U32 }, | |
756 | }; | |
757 | ||
77b9900e JP |
758 | static void fl_set_key_val(struct nlattr **tb, |
759 | void *val, int val_type, | |
760 | void *mask, int mask_type, int len) | |
761 | { | |
762 | if (!tb[val_type]) | |
763 | return; | |
e0ace68a | 764 | nla_memcpy(val, tb[val_type], len); |
77b9900e JP |
765 | if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type]) |
766 | memset(mask, 0xff, len); | |
767 | else | |
e0ace68a | 768 | nla_memcpy(mask, tb[mask_type], len); |
77b9900e JP |
769 | } |
770 | ||
5c72299f | 771 | static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key, |
bd7d4c12 GN |
772 | struct fl_flow_key *mask, |
773 | struct netlink_ext_ack *extack) | |
5c72299f | 774 | { |
8ffb055b YK |
775 | fl_set_key_val(tb, &key->tp_range.tp_min.dst, |
776 | TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_range.tp_min.dst, | |
777 | TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.dst)); | |
778 | fl_set_key_val(tb, &key->tp_range.tp_max.dst, | |
779 | TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_range.tp_max.dst, | |
780 | TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.dst)); | |
781 | fl_set_key_val(tb, &key->tp_range.tp_min.src, | |
782 | TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_range.tp_min.src, | |
783 | TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.src)); | |
784 | fl_set_key_val(tb, &key->tp_range.tp_max.src, | |
785 | TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_range.tp_max.src, | |
786 | TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.src)); | |
787 | ||
bd7d4c12 | 788 | if (mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst && |
6215afcb VO |
789 | ntohs(key->tp_range.tp_max.dst) <= |
790 | ntohs(key->tp_range.tp_min.dst)) { | |
bd7d4c12 GN |
791 | NL_SET_ERR_MSG_ATTR(extack, |
792 | tb[TCA_FLOWER_KEY_PORT_DST_MIN], | |
793 | "Invalid destination port range (min must be strictly smaller than max)"); | |
5c72299f | 794 | return -EINVAL; |
bd7d4c12 GN |
795 | } |
796 | if (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src && | |
6215afcb VO |
797 | ntohs(key->tp_range.tp_max.src) <= |
798 | ntohs(key->tp_range.tp_min.src)) { | |
bd7d4c12 GN |
799 | NL_SET_ERR_MSG_ATTR(extack, |
800 | tb[TCA_FLOWER_KEY_PORT_SRC_MIN], | |
801 | "Invalid source port range (min must be strictly smaller than max)"); | |
802 | return -EINVAL; | |
803 | } | |
5c72299f AN |
804 | |
805 | return 0; | |
806 | } | |
807 | ||
61aec25a GN |
808 | static int fl_set_key_mpls_lse(const struct nlattr *nla_lse, |
809 | struct flow_dissector_key_mpls *key_val, | |
810 | struct flow_dissector_key_mpls *key_mask, | |
811 | struct netlink_ext_ack *extack) | |
812 | { | |
813 | struct nlattr *tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1]; | |
814 | struct flow_dissector_mpls_lse *lse_mask; | |
815 | struct flow_dissector_mpls_lse *lse_val; | |
816 | u8 lse_index; | |
817 | u8 depth; | |
818 | int err; | |
819 | ||
820 | err = nla_parse_nested(tb, TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX, nla_lse, | |
821 | mpls_stack_entry_policy, extack); | |
822 | if (err < 0) | |
823 | return err; | |
824 | ||
825 | if (!tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]) { | |
826 | NL_SET_ERR_MSG(extack, "Missing MPLS option \"depth\""); | |
827 | return -EINVAL; | |
828 | } | |
829 | ||
830 | depth = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]); | |
831 | ||
832 | /* LSE depth starts at 1, for consistency with terminology used by | |
833 | * RFC 3031 (section 3.9), where depth 0 refers to unlabeled packets. | |
834 | */ | |
835 | if (depth < 1 || depth > FLOW_DIS_MPLS_MAX) { | |
836 | NL_SET_ERR_MSG_ATTR(extack, | |
837 | tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH], | |
838 | "Invalid MPLS depth"); | |
839 | return -EINVAL; | |
840 | } | |
841 | lse_index = depth - 1; | |
842 | ||
843 | dissector_set_mpls_lse(key_val, lse_index); | |
844 | dissector_set_mpls_lse(key_mask, lse_index); | |
845 | ||
846 | lse_val = &key_val->ls[lse_index]; | |
847 | lse_mask = &key_mask->ls[lse_index]; | |
848 | ||
849 | if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]) { | |
850 | lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]); | |
851 | lse_mask->mpls_ttl = MPLS_TTL_MASK; | |
852 | } | |
853 | if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]) { | |
854 | u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]); | |
855 | ||
856 | if (bos & ~MPLS_BOS_MASK) { | |
857 | NL_SET_ERR_MSG_ATTR(extack, | |
858 | tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS], | |
859 | "Bottom Of Stack (BOS) must be 0 or 1"); | |
860 | return -EINVAL; | |
861 | } | |
862 | lse_val->mpls_bos = bos; | |
863 | lse_mask->mpls_bos = MPLS_BOS_MASK; | |
864 | } | |
865 | if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]) { | |
866 | u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]); | |
867 | ||
868 | if (tc & ~MPLS_TC_MASK) { | |
869 | NL_SET_ERR_MSG_ATTR(extack, | |
870 | tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC], | |
871 | "Traffic Class (TC) must be between 0 and 7"); | |
872 | return -EINVAL; | |
873 | } | |
874 | lse_val->mpls_tc = tc; | |
875 | lse_mask->mpls_tc = MPLS_TC_MASK; | |
876 | } | |
877 | if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]) { | |
878 | u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]); | |
879 | ||
880 | if (label & ~MPLS_LABEL_MASK) { | |
881 | NL_SET_ERR_MSG_ATTR(extack, | |
882 | tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL], | |
883 | "Label must be between 0 and 1048575"); | |
884 | return -EINVAL; | |
885 | } | |
886 | lse_val->mpls_label = label; | |
887 | lse_mask->mpls_label = MPLS_LABEL_MASK; | |
888 | } | |
889 | ||
890 | return 0; | |
891 | } | |
892 | ||
893 | static int fl_set_key_mpls_opts(const struct nlattr *nla_mpls_opts, | |
894 | struct flow_dissector_key_mpls *key_val, | |
895 | struct flow_dissector_key_mpls *key_mask, | |
896 | struct netlink_ext_ack *extack) | |
897 | { | |
898 | struct nlattr *nla_lse; | |
899 | int rem; | |
900 | int err; | |
901 | ||
902 | if (!(nla_mpls_opts->nla_type & NLA_F_NESTED)) { | |
903 | NL_SET_ERR_MSG_ATTR(extack, nla_mpls_opts, | |
904 | "NLA_F_NESTED is missing"); | |
905 | return -EINVAL; | |
906 | } | |
907 | ||
908 | nla_for_each_nested(nla_lse, nla_mpls_opts, rem) { | |
909 | if (nla_type(nla_lse) != TCA_FLOWER_KEY_MPLS_OPTS_LSE) { | |
910 | NL_SET_ERR_MSG_ATTR(extack, nla_lse, | |
911 | "Invalid MPLS option type"); | |
912 | return -EINVAL; | |
913 | } | |
914 | ||
915 | err = fl_set_key_mpls_lse(nla_lse, key_val, key_mask, extack); | |
916 | if (err < 0) | |
917 | return err; | |
918 | } | |
919 | if (rem) { | |
920 | NL_SET_ERR_MSG(extack, | |
921 | "Bytes leftover after parsing MPLS options"); | |
922 | return -EINVAL; | |
923 | } | |
924 | ||
925 | return 0; | |
926 | } | |
927 | ||
1a7fca63 BL |
928 | static int fl_set_key_mpls(struct nlattr **tb, |
929 | struct flow_dissector_key_mpls *key_val, | |
442f730e GN |
930 | struct flow_dissector_key_mpls *key_mask, |
931 | struct netlink_ext_ack *extack) | |
a577d8f7 | 932 | { |
58cff782 GN |
933 | struct flow_dissector_mpls_lse *lse_mask; |
934 | struct flow_dissector_mpls_lse *lse_val; | |
935 | ||
61aec25a GN |
936 | if (tb[TCA_FLOWER_KEY_MPLS_OPTS]) { |
937 | if (tb[TCA_FLOWER_KEY_MPLS_TTL] || | |
938 | tb[TCA_FLOWER_KEY_MPLS_BOS] || | |
939 | tb[TCA_FLOWER_KEY_MPLS_TC] || | |
940 | tb[TCA_FLOWER_KEY_MPLS_LABEL]) { | |
941 | NL_SET_ERR_MSG_ATTR(extack, | |
942 | tb[TCA_FLOWER_KEY_MPLS_OPTS], | |
943 | "MPLS label, Traffic Class, Bottom Of Stack and Time To Live must be encapsulated in the MPLS options attribute"); | |
944 | return -EBADMSG; | |
945 | } | |
946 | ||
947 | return fl_set_key_mpls_opts(tb[TCA_FLOWER_KEY_MPLS_OPTS], | |
948 | key_val, key_mask, extack); | |
949 | } | |
950 | ||
58cff782 GN |
951 | lse_val = &key_val->ls[0]; |
952 | lse_mask = &key_mask->ls[0]; | |
953 | ||
a577d8f7 | 954 | if (tb[TCA_FLOWER_KEY_MPLS_TTL]) { |
58cff782 GN |
955 | lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]); |
956 | lse_mask->mpls_ttl = MPLS_TTL_MASK; | |
957 | dissector_set_mpls_lse(key_val, 0); | |
958 | dissector_set_mpls_lse(key_mask, 0); | |
a577d8f7 BL |
959 | } |
960 | if (tb[TCA_FLOWER_KEY_MPLS_BOS]) { | |
1a7fca63 BL |
961 | u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]); |
962 | ||
442f730e GN |
963 | if (bos & ~MPLS_BOS_MASK) { |
964 | NL_SET_ERR_MSG_ATTR(extack, | |
965 | tb[TCA_FLOWER_KEY_MPLS_BOS], | |
966 | "Bottom Of Stack (BOS) must be 0 or 1"); | |
1a7fca63 | 967 | return -EINVAL; |
442f730e | 968 | } |
58cff782 GN |
969 | lse_val->mpls_bos = bos; |
970 | lse_mask->mpls_bos = MPLS_BOS_MASK; | |
971 | dissector_set_mpls_lse(key_val, 0); | |
972 | dissector_set_mpls_lse(key_mask, 0); | |
a577d8f7 BL |
973 | } |
974 | if (tb[TCA_FLOWER_KEY_MPLS_TC]) { | |
1a7fca63 BL |
975 | u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]); |
976 | ||
442f730e GN |
977 | if (tc & ~MPLS_TC_MASK) { |
978 | NL_SET_ERR_MSG_ATTR(extack, | |
979 | tb[TCA_FLOWER_KEY_MPLS_TC], | |
980 | "Traffic Class (TC) must be between 0 and 7"); | |
1a7fca63 | 981 | return -EINVAL; |
442f730e | 982 | } |
58cff782 GN |
983 | lse_val->mpls_tc = tc; |
984 | lse_mask->mpls_tc = MPLS_TC_MASK; | |
985 | dissector_set_mpls_lse(key_val, 0); | |
986 | dissector_set_mpls_lse(key_mask, 0); | |
a577d8f7 BL |
987 | } |
988 | if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) { | |
1a7fca63 BL |
989 | u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]); |
990 | ||
442f730e GN |
991 | if (label & ~MPLS_LABEL_MASK) { |
992 | NL_SET_ERR_MSG_ATTR(extack, | |
993 | tb[TCA_FLOWER_KEY_MPLS_LABEL], | |
994 | "Label must be between 0 and 1048575"); | |
1a7fca63 | 995 | return -EINVAL; |
442f730e | 996 | } |
58cff782 GN |
997 | lse_val->mpls_label = label; |
998 | lse_mask->mpls_label = MPLS_LABEL_MASK; | |
999 | dissector_set_mpls_lse(key_val, 0); | |
1000 | dissector_set_mpls_lse(key_mask, 0); | |
a577d8f7 | 1001 | } |
1a7fca63 | 1002 | return 0; |
a577d8f7 BL |
1003 | } |
1004 | ||
9399ae9a | 1005 | static void fl_set_key_vlan(struct nlattr **tb, |
aaab0834 | 1006 | __be16 ethertype, |
d64efd09 | 1007 | int vlan_id_key, int vlan_prio_key, |
9399ae9a HHZ |
1008 | struct flow_dissector_key_vlan *key_val, |
1009 | struct flow_dissector_key_vlan *key_mask) | |
1010 | { | |
1011 | #define VLAN_PRIORITY_MASK 0x7 | |
1012 | ||
d64efd09 | 1013 | if (tb[vlan_id_key]) { |
9399ae9a | 1014 | key_val->vlan_id = |
d64efd09 | 1015 | nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK; |
9399ae9a HHZ |
1016 | key_mask->vlan_id = VLAN_VID_MASK; |
1017 | } | |
d64efd09 | 1018 | if (tb[vlan_prio_key]) { |
9399ae9a | 1019 | key_val->vlan_priority = |
d64efd09 | 1020 | nla_get_u8(tb[vlan_prio_key]) & |
9399ae9a HHZ |
1021 | VLAN_PRIORITY_MASK; |
1022 | key_mask->vlan_priority = VLAN_PRIORITY_MASK; | |
1023 | } | |
aaab0834 JL |
1024 | key_val->vlan_tpid = ethertype; |
1025 | key_mask->vlan_tpid = cpu_to_be16(~0); | |
9399ae9a HHZ |
1026 | } |
1027 | ||
faa3ffce OG |
1028 | static void fl_set_key_flag(u32 flower_key, u32 flower_mask, |
1029 | u32 *dissector_key, u32 *dissector_mask, | |
1030 | u32 flower_flag_bit, u32 dissector_flag_bit) | |
1031 | { | |
1032 | if (flower_mask & flower_flag_bit) { | |
1033 | *dissector_mask |= dissector_flag_bit; | |
1034 | if (flower_key & flower_flag_bit) | |
1035 | *dissector_key |= dissector_flag_bit; | |
1036 | } | |
1037 | } | |
1038 | ||
e304e21a GN |
1039 | static int fl_set_key_flags(struct nlattr **tb, u32 *flags_key, |
1040 | u32 *flags_mask, struct netlink_ext_ack *extack) | |
faa3ffce OG |
1041 | { |
1042 | u32 key, mask; | |
1043 | ||
d9724772 | 1044 | /* mask is mandatory for flags */ |
e304e21a GN |
1045 | if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) { |
1046 | NL_SET_ERR_MSG(extack, "Missing flags mask"); | |
d9724772 | 1047 | return -EINVAL; |
e304e21a | 1048 | } |
faa3ffce | 1049 | |
abee13f5 VO |
1050 | key = be32_to_cpu(nla_get_be32(tb[TCA_FLOWER_KEY_FLAGS])); |
1051 | mask = be32_to_cpu(nla_get_be32(tb[TCA_FLOWER_KEY_FLAGS_MASK])); | |
faa3ffce OG |
1052 | |
1053 | *flags_key = 0; | |
1054 | *flags_mask = 0; | |
1055 | ||
1056 | fl_set_key_flag(key, mask, flags_key, flags_mask, | |
1057 | TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); | |
459d153d PJV |
1058 | fl_set_key_flag(key, mask, flags_key, flags_mask, |
1059 | TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, | |
1060 | FLOW_DIS_FIRST_FRAG); | |
d9724772 OG |
1061 | |
1062 | return 0; | |
faa3ffce OG |
1063 | } |
1064 | ||
0e2c17b6 | 1065 | static void fl_set_key_ip(struct nlattr **tb, bool encap, |
4d80cc0a OG |
1066 | struct flow_dissector_key_ip *key, |
1067 | struct flow_dissector_key_ip *mask) | |
1068 | { | |
0e2c17b6 OG |
1069 | int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS; |
1070 | int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL; | |
1071 | int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK; | |
1072 | int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK; | |
4d80cc0a | 1073 | |
0e2c17b6 OG |
1074 | fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)); |
1075 | fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)); | |
4d80cc0a OG |
1076 | } |
1077 | ||
0a6e7778 PJV |
1078 | static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key, |
1079 | int depth, int option_len, | |
1080 | struct netlink_ext_ack *extack) | |
1081 | { | |
1082 | struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1]; | |
1083 | struct nlattr *class = NULL, *type = NULL, *data = NULL; | |
1084 | struct geneve_opt *opt; | |
1085 | int err, data_len = 0; | |
1086 | ||
1087 | if (option_len > sizeof(struct geneve_opt)) | |
1088 | data_len = option_len - sizeof(struct geneve_opt); | |
1089 | ||
1090 | opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len]; | |
1091 | memset(opt, 0xff, option_len); | |
1092 | opt->length = data_len / 4; | |
1093 | opt->r1 = 0; | |
1094 | opt->r2 = 0; | |
1095 | opt->r3 = 0; | |
1096 | ||
1097 | /* If no mask has been prodived we assume an exact match. */ | |
1098 | if (!depth) | |
1099 | return sizeof(struct geneve_opt) + data_len; | |
1100 | ||
1101 | if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) { | |
1102 | NL_SET_ERR_MSG(extack, "Non-geneve option type for mask"); | |
1103 | return -EINVAL; | |
1104 | } | |
1105 | ||
8cb08174 JB |
1106 | err = nla_parse_nested_deprecated(tb, |
1107 | TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX, | |
1108 | nla, geneve_opt_policy, extack); | |
0a6e7778 PJV |
1109 | if (err < 0) |
1110 | return err; | |
1111 | ||
1112 | /* We are not allowed to omit any of CLASS, TYPE or DATA | |
1113 | * fields from the key. | |
1114 | */ | |
1115 | if (!option_len && | |
1116 | (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] || | |
1117 | !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] || | |
1118 | !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) { | |
1119 | NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data"); | |
1120 | return -EINVAL; | |
1121 | } | |
1122 | ||
1123 | /* Omitting any of CLASS, TYPE or DATA fields is allowed | |
1124 | * for the mask. | |
1125 | */ | |
1126 | if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) { | |
1127 | int new_len = key->enc_opts.len; | |
1128 | ||
1129 | data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]; | |
1130 | data_len = nla_len(data); | |
1131 | if (data_len < 4) { | |
1132 | NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long"); | |
1133 | return -ERANGE; | |
1134 | } | |
1135 | if (data_len % 4) { | |
1136 | NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long"); | |
1137 | return -ERANGE; | |
1138 | } | |
1139 | ||
1140 | new_len += sizeof(struct geneve_opt) + data_len; | |
1141 | BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX); | |
1142 | if (new_len > FLOW_DIS_TUN_OPTS_MAX) { | |
1143 | NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size"); | |
1144 | return -ERANGE; | |
1145 | } | |
1146 | opt->length = data_len / 4; | |
1147 | memcpy(opt->opt_data, nla_data(data), data_len); | |
1148 | } | |
1149 | ||
1150 | if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) { | |
1151 | class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]; | |
1152 | opt->opt_class = nla_get_be16(class); | |
1153 | } | |
1154 | ||
1155 | if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) { | |
1156 | type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]; | |
1157 | opt->type = nla_get_u8(type); | |
1158 | } | |
1159 | ||
1160 | return sizeof(struct geneve_opt) + data_len; | |
1161 | } | |
1162 | ||
d8f9dfae XL |
1163 | static int fl_set_vxlan_opt(const struct nlattr *nla, struct fl_flow_key *key, |
1164 | int depth, int option_len, | |
1165 | struct netlink_ext_ack *extack) | |
1166 | { | |
1167 | struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1]; | |
1168 | struct vxlan_metadata *md; | |
1169 | int err; | |
1170 | ||
1171 | md = (struct vxlan_metadata *)&key->enc_opts.data[key->enc_opts.len]; | |
1172 | memset(md, 0xff, sizeof(*md)); | |
1173 | ||
1174 | if (!depth) | |
1175 | return sizeof(*md); | |
1176 | ||
1177 | if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_VXLAN) { | |
1178 | NL_SET_ERR_MSG(extack, "Non-vxlan option type for mask"); | |
1179 | return -EINVAL; | |
1180 | } | |
1181 | ||
1182 | err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, nla, | |
1183 | vxlan_opt_policy, extack); | |
1184 | if (err < 0) | |
1185 | return err; | |
1186 | ||
1187 | if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) { | |
1188 | NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp"); | |
1189 | return -EINVAL; | |
1190 | } | |
1191 | ||
13e6ce98 | 1192 | if (tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) { |
d8f9dfae | 1193 | md->gbp = nla_get_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]); |
13e6ce98 XL |
1194 | md->gbp &= VXLAN_GBP_MASK; |
1195 | } | |
d8f9dfae XL |
1196 | |
1197 | return sizeof(*md); | |
1198 | } | |
1199 | ||
79b1011c XL |
1200 | static int fl_set_erspan_opt(const struct nlattr *nla, struct fl_flow_key *key, |
1201 | int depth, int option_len, | |
1202 | struct netlink_ext_ack *extack) | |
1203 | { | |
1204 | struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1]; | |
1205 | struct erspan_metadata *md; | |
1206 | int err; | |
1207 | ||
1208 | md = (struct erspan_metadata *)&key->enc_opts.data[key->enc_opts.len]; | |
1209 | memset(md, 0xff, sizeof(*md)); | |
1210 | md->version = 1; | |
1211 | ||
1212 | if (!depth) | |
1213 | return sizeof(*md); | |
1214 | ||
1215 | if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_ERSPAN) { | |
1216 | NL_SET_ERR_MSG(extack, "Non-erspan option type for mask"); | |
1217 | return -EINVAL; | |
1218 | } | |
1219 | ||
1220 | err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX, nla, | |
1221 | erspan_opt_policy, extack); | |
1222 | if (err < 0) | |
1223 | return err; | |
1224 | ||
1225 | if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) { | |
1226 | NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver"); | |
1227 | return -EINVAL; | |
1228 | } | |
1229 | ||
1230 | if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) | |
1231 | md->version = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]); | |
1232 | ||
1233 | if (md->version == 1) { | |
1234 | if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) { | |
1235 | NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index"); | |
1236 | return -EINVAL; | |
1237 | } | |
1238 | if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) { | |
1239 | nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]; | |
8e1b3ac4 | 1240 | memset(&md->u, 0x00, sizeof(md->u)); |
79b1011c XL |
1241 | md->u.index = nla_get_be32(nla); |
1242 | } | |
1243 | } else if (md->version == 2) { | |
1244 | if (!option_len && (!tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] || | |
1245 | !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID])) { | |
1246 | NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid"); | |
1247 | return -EINVAL; | |
1248 | } | |
1249 | if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]) { | |
1250 | nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]; | |
1251 | md->u.md2.dir = nla_get_u8(nla); | |
1252 | } | |
1253 | if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]) { | |
1254 | nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]; | |
1255 | set_hwid(&md->u.md2, nla_get_u8(nla)); | |
1256 | } | |
1257 | } else { | |
1258 | NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect"); | |
1259 | return -EINVAL; | |
1260 | } | |
1261 | ||
1262 | return sizeof(*md); | |
1263 | } | |
1264 | ||
0a6e7778 PJV |
1265 | static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key, |
1266 | struct fl_flow_key *mask, | |
1267 | struct netlink_ext_ack *extack) | |
1268 | { | |
1269 | const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL; | |
63c82997 JK |
1270 | int err, option_len, key_depth, msk_depth = 0; |
1271 | ||
8cb08174 JB |
1272 | err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS], |
1273 | TCA_FLOWER_KEY_ENC_OPTS_MAX, | |
1274 | enc_opts_policy, extack); | |
63c82997 JK |
1275 | if (err) |
1276 | return err; | |
0a6e7778 PJV |
1277 | |
1278 | nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]); | |
1279 | ||
1280 | if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) { | |
8cb08174 JB |
1281 | err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK], |
1282 | TCA_FLOWER_KEY_ENC_OPTS_MAX, | |
1283 | enc_opts_policy, extack); | |
63c82997 JK |
1284 | if (err) |
1285 | return err; | |
1286 | ||
0a6e7778 PJV |
1287 | nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); |
1288 | msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); | |
c96adff9 CW |
1289 | if (!nla_ok(nla_opt_msk, msk_depth)) { |
1290 | NL_SET_ERR_MSG(extack, "Invalid nested attribute for masks"); | |
1291 | return -EINVAL; | |
1292 | } | |
0a6e7778 PJV |
1293 | } |
1294 | ||
1295 | nla_for_each_attr(nla_opt_key, nla_enc_key, | |
1296 | nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) { | |
1297 | switch (nla_type(nla_opt_key)) { | |
1298 | case TCA_FLOWER_KEY_ENC_OPTS_GENEVE: | |
d8f9dfae XL |
1299 | if (key->enc_opts.dst_opt_type && |
1300 | key->enc_opts.dst_opt_type != TUNNEL_GENEVE_OPT) { | |
1301 | NL_SET_ERR_MSG(extack, "Duplicate type for geneve options"); | |
1302 | return -EINVAL; | |
1303 | } | |
0a6e7778 PJV |
1304 | option_len = 0; |
1305 | key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT; | |
1306 | option_len = fl_set_geneve_opt(nla_opt_key, key, | |
1307 | key_depth, option_len, | |
1308 | extack); | |
1309 | if (option_len < 0) | |
1310 | return option_len; | |
1311 | ||
1312 | key->enc_opts.len += option_len; | |
1313 | /* At the same time we need to parse through the mask | |
1314 | * in order to verify exact and mask attribute lengths. | |
1315 | */ | |
1316 | mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT; | |
1317 | option_len = fl_set_geneve_opt(nla_opt_msk, mask, | |
1318 | msk_depth, option_len, | |
1319 | extack); | |
1320 | if (option_len < 0) | |
1321 | return option_len; | |
1322 | ||
1323 | mask->enc_opts.len += option_len; | |
1324 | if (key->enc_opts.len != mask->enc_opts.len) { | |
1325 | NL_SET_ERR_MSG(extack, "Key and mask miss aligned"); | |
1326 | return -EINVAL; | |
1327 | } | |
d8f9dfae XL |
1328 | break; |
1329 | case TCA_FLOWER_KEY_ENC_OPTS_VXLAN: | |
1330 | if (key->enc_opts.dst_opt_type) { | |
1331 | NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options"); | |
1332 | return -EINVAL; | |
1333 | } | |
1334 | option_len = 0; | |
1335 | key->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT; | |
1336 | option_len = fl_set_vxlan_opt(nla_opt_key, key, | |
1337 | key_depth, option_len, | |
1338 | extack); | |
1339 | if (option_len < 0) | |
1340 | return option_len; | |
1341 | ||
1342 | key->enc_opts.len += option_len; | |
1343 | /* At the same time we need to parse through the mask | |
1344 | * in order to verify exact and mask attribute lengths. | |
1345 | */ | |
1346 | mask->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT; | |
1347 | option_len = fl_set_vxlan_opt(nla_opt_msk, mask, | |
1348 | msk_depth, option_len, | |
1349 | extack); | |
1350 | if (option_len < 0) | |
1351 | return option_len; | |
1352 | ||
1353 | mask->enc_opts.len += option_len; | |
1354 | if (key->enc_opts.len != mask->enc_opts.len) { | |
1355 | NL_SET_ERR_MSG(extack, "Key and mask miss aligned"); | |
1356 | return -EINVAL; | |
1357 | } | |
79b1011c XL |
1358 | break; |
1359 | case TCA_FLOWER_KEY_ENC_OPTS_ERSPAN: | |
1360 | if (key->enc_opts.dst_opt_type) { | |
1361 | NL_SET_ERR_MSG(extack, "Duplicate type for erspan options"); | |
1362 | return -EINVAL; | |
1363 | } | |
1364 | option_len = 0; | |
1365 | key->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT; | |
1366 | option_len = fl_set_erspan_opt(nla_opt_key, key, | |
1367 | key_depth, option_len, | |
1368 | extack); | |
1369 | if (option_len < 0) | |
1370 | return option_len; | |
1371 | ||
1372 | key->enc_opts.len += option_len; | |
1373 | /* At the same time we need to parse through the mask | |
1374 | * in order to verify exact and mask attribute lengths. | |
1375 | */ | |
1376 | mask->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT; | |
1377 | option_len = fl_set_erspan_opt(nla_opt_msk, mask, | |
1378 | msk_depth, option_len, | |
1379 | extack); | |
1380 | if (option_len < 0) | |
1381 | return option_len; | |
1382 | ||
1383 | mask->enc_opts.len += option_len; | |
1384 | if (key->enc_opts.len != mask->enc_opts.len) { | |
1385 | NL_SET_ERR_MSG(extack, "Key and mask miss aligned"); | |
1386 | return -EINVAL; | |
1387 | } | |
0a6e7778 PJV |
1388 | break; |
1389 | default: | |
1390 | NL_SET_ERR_MSG(extack, "Unknown tunnel option type"); | |
1391 | return -EINVAL; | |
1392 | } | |
c96adff9 CW |
1393 | |
1394 | if (!msk_depth) | |
1395 | continue; | |
1396 | ||
1397 | if (!nla_ok(nla_opt_msk, msk_depth)) { | |
1398 | NL_SET_ERR_MSG(extack, "A mask attribute is invalid"); | |
1399 | return -EINVAL; | |
1400 | } | |
1401 | nla_opt_msk = nla_next(nla_opt_msk, &msk_depth); | |
0a6e7778 PJV |
1402 | } |
1403 | ||
1404 | return 0; | |
1405 | } | |
1406 | ||
1bcc51ac | 1407 | static int fl_validate_ct_state(u16 state, struct nlattr *tb, |
1408 | struct netlink_ext_ack *extack) | |
1409 | { | |
1410 | if (state && !(state & TCA_FLOWER_KEY_CT_FLAGS_TRACKED)) { | |
1411 | NL_SET_ERR_MSG_ATTR(extack, tb, | |
1412 | "no trk, so no other flag can be set"); | |
1413 | return -EINVAL; | |
1414 | } | |
1415 | ||
1416 | if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW && | |
1417 | state & TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED) { | |
1418 | NL_SET_ERR_MSG_ATTR(extack, tb, | |
1419 | "new and est are mutually exclusive"); | |
1420 | return -EINVAL; | |
1421 | } | |
1422 | ||
3aed8b63 | 1423 | if (state & TCA_FLOWER_KEY_CT_FLAGS_INVALID && |
1424 | state & ~(TCA_FLOWER_KEY_CT_FLAGS_TRACKED | | |
1425 | TCA_FLOWER_KEY_CT_FLAGS_INVALID)) { | |
1426 | NL_SET_ERR_MSG_ATTR(extack, tb, | |
1427 | "when inv is set, only trk may be set"); | |
1428 | return -EINVAL; | |
1429 | } | |
1430 | ||
1431 | if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW && | |
1432 | state & TCA_FLOWER_KEY_CT_FLAGS_REPLY) { | |
1433 | NL_SET_ERR_MSG_ATTR(extack, tb, | |
1434 | "new and rpl are mutually exclusive"); | |
1435 | return -EINVAL; | |
1436 | } | |
1437 | ||
1bcc51ac | 1438 | return 0; |
1439 | } | |
1440 | ||
e0ace68a PB |
1441 | static int fl_set_key_ct(struct nlattr **tb, |
1442 | struct flow_dissector_key_ct *key, | |
1443 | struct flow_dissector_key_ct *mask, | |
1444 | struct netlink_ext_ack *extack) | |
1445 | { | |
1446 | if (tb[TCA_FLOWER_KEY_CT_STATE]) { | |
1bcc51ac | 1447 | int err; |
1448 | ||
e0ace68a PB |
1449 | if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) { |
1450 | NL_SET_ERR_MSG(extack, "Conntrack isn't enabled"); | |
1451 | return -EOPNOTSUPP; | |
1452 | } | |
1453 | fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE, | |
1454 | &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK, | |
1455 | sizeof(key->ct_state)); | |
1bcc51ac | 1456 | |
afa536d8 | 1457 | err = fl_validate_ct_state(key->ct_state & mask->ct_state, |
1bcc51ac | 1458 | tb[TCA_FLOWER_KEY_CT_STATE_MASK], |
1459 | extack); | |
1460 | if (err) | |
1461 | return err; | |
1462 | ||
e0ace68a PB |
1463 | } |
1464 | if (tb[TCA_FLOWER_KEY_CT_ZONE]) { | |
1465 | if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) { | |
1466 | NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled"); | |
1467 | return -EOPNOTSUPP; | |
1468 | } | |
1469 | fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE, | |
1470 | &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK, | |
1471 | sizeof(key->ct_zone)); | |
1472 | } | |
1473 | if (tb[TCA_FLOWER_KEY_CT_MARK]) { | |
1474 | if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) { | |
1475 | NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled"); | |
1476 | return -EOPNOTSUPP; | |
1477 | } | |
1478 | fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK, | |
1479 | &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK, | |
1480 | sizeof(key->ct_mark)); | |
1481 | } | |
1482 | if (tb[TCA_FLOWER_KEY_CT_LABELS]) { | |
1483 | if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) { | |
1484 | NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled"); | |
1485 | return -EOPNOTSUPP; | |
1486 | } | |
1487 | fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS, | |
1488 | mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK, | |
1489 | sizeof(key->ct_labels)); | |
1490 | } | |
1491 | ||
1492 | return 0; | |
1493 | } | |
1494 | ||
77b9900e | 1495 | static int fl_set_key(struct net *net, struct nlattr **tb, |
1057c55f AA |
1496 | struct fl_flow_key *key, struct fl_flow_key *mask, |
1497 | struct netlink_ext_ack *extack) | |
77b9900e | 1498 | { |
9399ae9a | 1499 | __be16 ethertype; |
d9724772 | 1500 | int ret = 0; |
a5148626 | 1501 | |
77b9900e | 1502 | if (tb[TCA_FLOWER_INDEV]) { |
1057c55f | 1503 | int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack); |
77b9900e JP |
1504 | if (err < 0) |
1505 | return err; | |
8212ed77 JP |
1506 | key->meta.ingress_ifindex = err; |
1507 | mask->meta.ingress_ifindex = 0xffffffff; | |
77b9900e JP |
1508 | } |
1509 | ||
1510 | fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, | |
1511 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, | |
1512 | sizeof(key->eth.dst)); | |
1513 | fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, | |
1514 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, | |
1515 | sizeof(key->eth.src)); | |
66530bdf | 1516 | |
0b498a52 | 1517 | if (tb[TCA_FLOWER_KEY_ETH_TYPE]) { |
9399ae9a HHZ |
1518 | ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]); |
1519 | ||
aaab0834 | 1520 | if (eth_type_vlan(ethertype)) { |
d64efd09 JL |
1521 | fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID, |
1522 | TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, | |
1523 | &mask->vlan); | |
1524 | ||
5e9a0fe4 JL |
1525 | if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) { |
1526 | ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]); | |
1527 | if (eth_type_vlan(ethertype)) { | |
1528 | fl_set_key_vlan(tb, ethertype, | |
1529 | TCA_FLOWER_KEY_CVLAN_ID, | |
1530 | TCA_FLOWER_KEY_CVLAN_PRIO, | |
1531 | &key->cvlan, &mask->cvlan); | |
1532 | fl_set_key_val(tb, &key->basic.n_proto, | |
1533 | TCA_FLOWER_KEY_CVLAN_ETH_TYPE, | |
1534 | &mask->basic.n_proto, | |
1535 | TCA_FLOWER_UNSPEC, | |
1536 | sizeof(key->basic.n_proto)); | |
1537 | } else { | |
1538 | key->basic.n_proto = ethertype; | |
1539 | mask->basic.n_proto = cpu_to_be16(~0); | |
1540 | } | |
d64efd09 | 1541 | } |
0b498a52 AB |
1542 | } else { |
1543 | key->basic.n_proto = ethertype; | |
1544 | mask->basic.n_proto = cpu_to_be16(~0); | |
1545 | } | |
9399ae9a | 1546 | } |
66530bdf | 1547 | |
77b9900e JP |
1548 | if (key->basic.n_proto == htons(ETH_P_IP) || |
1549 | key->basic.n_proto == htons(ETH_P_IPV6)) { | |
1550 | fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, | |
1551 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, | |
1552 | sizeof(key->basic.ip_proto)); | |
0e2c17b6 | 1553 | fl_set_key_ip(tb, false, &key->ip, &mask->ip); |
77b9900e | 1554 | } |
66530bdf JHS |
1555 | |
1556 | if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) { | |
1557 | key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; | |
970bfcd0 | 1558 | mask->control.addr_type = ~0; |
77b9900e JP |
1559 | fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
1560 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, | |
1561 | sizeof(key->ipv4.src)); | |
1562 | fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, | |
1563 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, | |
1564 | sizeof(key->ipv4.dst)); | |
66530bdf JHS |
1565 | } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) { |
1566 | key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
970bfcd0 | 1567 | mask->control.addr_type = ~0; |
77b9900e JP |
1568 | fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
1569 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, | |
1570 | sizeof(key->ipv6.src)); | |
1571 | fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, | |
1572 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, | |
1573 | sizeof(key->ipv6.dst)); | |
1574 | } | |
66530bdf | 1575 | |
77b9900e JP |
1576 | if (key->basic.ip_proto == IPPROTO_TCP) { |
1577 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, | |
aa72d708 | 1578 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
77b9900e JP |
1579 | sizeof(key->tp.src)); |
1580 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, | |
aa72d708 | 1581 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
77b9900e | 1582 | sizeof(key->tp.dst)); |
fdfc7dd6 JP |
1583 | fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, |
1584 | &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, | |
1585 | sizeof(key->tcp.flags)); | |
77b9900e JP |
1586 | } else if (key->basic.ip_proto == IPPROTO_UDP) { |
1587 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, | |
aa72d708 | 1588 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
77b9900e JP |
1589 | sizeof(key->tp.src)); |
1590 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, | |
aa72d708 | 1591 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
77b9900e | 1592 | sizeof(key->tp.dst)); |
5976c5f4 SH |
1593 | } else if (key->basic.ip_proto == IPPROTO_SCTP) { |
1594 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, | |
1595 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, | |
1596 | sizeof(key->tp.src)); | |
1597 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, | |
1598 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, | |
1599 | sizeof(key->tp.dst)); | |
7b684884 SH |
1600 | } else if (key->basic.n_proto == htons(ETH_P_IP) && |
1601 | key->basic.ip_proto == IPPROTO_ICMP) { | |
1602 | fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE, | |
1603 | &mask->icmp.type, | |
1604 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, | |
1605 | sizeof(key->icmp.type)); | |
1606 | fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE, | |
1607 | &mask->icmp.code, | |
1608 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK, | |
1609 | sizeof(key->icmp.code)); | |
1610 | } else if (key->basic.n_proto == htons(ETH_P_IPV6) && | |
1611 | key->basic.ip_proto == IPPROTO_ICMPV6) { | |
1612 | fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE, | |
1613 | &mask->icmp.type, | |
1614 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, | |
1615 | sizeof(key->icmp.type)); | |
040587af | 1616 | fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE, |
7b684884 | 1617 | &mask->icmp.code, |
040587af | 1618 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK, |
7b684884 | 1619 | sizeof(key->icmp.code)); |
a577d8f7 BL |
1620 | } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) || |
1621 | key->basic.n_proto == htons(ETH_P_MPLS_MC)) { | |
442f730e | 1622 | ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls, extack); |
1a7fca63 BL |
1623 | if (ret) |
1624 | return ret; | |
99d31326 SH |
1625 | } else if (key->basic.n_proto == htons(ETH_P_ARP) || |
1626 | key->basic.n_proto == htons(ETH_P_RARP)) { | |
1627 | fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP, | |
1628 | &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK, | |
1629 | sizeof(key->arp.sip)); | |
1630 | fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP, | |
1631 | &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK, | |
1632 | sizeof(key->arp.tip)); | |
1633 | fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP, | |
1634 | &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK, | |
1635 | sizeof(key->arp.op)); | |
1636 | fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, | |
1637 | mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, | |
1638 | sizeof(key->arp.sha)); | |
1639 | fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, | |
1640 | mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, | |
1641 | sizeof(key->arp.tha)); | |
77b9900e JP |
1642 | } |
1643 | ||
5c72299f AN |
1644 | if (key->basic.ip_proto == IPPROTO_TCP || |
1645 | key->basic.ip_proto == IPPROTO_UDP || | |
1646 | key->basic.ip_proto == IPPROTO_SCTP) { | |
bd7d4c12 | 1647 | ret = fl_set_key_port_range(tb, key, mask, extack); |
5c72299f AN |
1648 | if (ret) |
1649 | return ret; | |
1650 | } | |
1651 | ||
bc3103f1 AV |
1652 | if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] || |
1653 | tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) { | |
1654 | key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; | |
970bfcd0 | 1655 | mask->enc_control.addr_type = ~0; |
bc3103f1 AV |
1656 | fl_set_key_val(tb, &key->enc_ipv4.src, |
1657 | TCA_FLOWER_KEY_ENC_IPV4_SRC, | |
1658 | &mask->enc_ipv4.src, | |
1659 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, | |
1660 | sizeof(key->enc_ipv4.src)); | |
1661 | fl_set_key_val(tb, &key->enc_ipv4.dst, | |
1662 | TCA_FLOWER_KEY_ENC_IPV4_DST, | |
1663 | &mask->enc_ipv4.dst, | |
1664 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, | |
1665 | sizeof(key->enc_ipv4.dst)); | |
1666 | } | |
1667 | ||
1668 | if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] || | |
1669 | tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) { | |
1670 | key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; | |
970bfcd0 | 1671 | mask->enc_control.addr_type = ~0; |
bc3103f1 AV |
1672 | fl_set_key_val(tb, &key->enc_ipv6.src, |
1673 | TCA_FLOWER_KEY_ENC_IPV6_SRC, | |
1674 | &mask->enc_ipv6.src, | |
1675 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, | |
1676 | sizeof(key->enc_ipv6.src)); | |
1677 | fl_set_key_val(tb, &key->enc_ipv6.dst, | |
1678 | TCA_FLOWER_KEY_ENC_IPV6_DST, | |
1679 | &mask->enc_ipv6.dst, | |
1680 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, | |
1681 | sizeof(key->enc_ipv6.dst)); | |
1682 | } | |
1683 | ||
1684 | fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID, | |
eb523f42 | 1685 | &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC, |
bc3103f1 AV |
1686 | sizeof(key->enc_key_id.keyid)); |
1687 | ||
f4d997fd HHZ |
1688 | fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, |
1689 | &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, | |
1690 | sizeof(key->enc_tp.src)); | |
1691 | ||
1692 | fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT, | |
1693 | &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, | |
1694 | sizeof(key->enc_tp.dst)); | |
1695 | ||
0e2c17b6 OG |
1696 | fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip); |
1697 | ||
5923b8f7 AL |
1698 | fl_set_key_val(tb, &key->hash.hash, TCA_FLOWER_KEY_HASH, |
1699 | &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK, | |
1700 | sizeof(key->hash.hash)); | |
1701 | ||
0a6e7778 PJV |
1702 | if (tb[TCA_FLOWER_KEY_ENC_OPTS]) { |
1703 | ret = fl_set_enc_opt(tb, key, mask, extack); | |
1704 | if (ret) | |
1705 | return ret; | |
1706 | } | |
1707 | ||
e0ace68a PB |
1708 | ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack); |
1709 | if (ret) | |
1710 | return ret; | |
1711 | ||
d9724772 | 1712 | if (tb[TCA_FLOWER_KEY_FLAGS]) |
e304e21a GN |
1713 | ret = fl_set_key_flags(tb, &key->control.flags, |
1714 | &mask->control.flags, extack); | |
faa3ffce | 1715 | |
d9724772 | 1716 | return ret; |
77b9900e JP |
1717 | } |
1718 | ||
05cd271f PB |
1719 | static void fl_mask_copy(struct fl_flow_mask *dst, |
1720 | struct fl_flow_mask *src) | |
77b9900e | 1721 | { |
05cd271f PB |
1722 | const void *psrc = fl_key_get_start(&src->key, src); |
1723 | void *pdst = fl_key_get_start(&dst->key, src); | |
77b9900e | 1724 | |
05cd271f PB |
1725 | memcpy(pdst, psrc, fl_mask_range(src)); |
1726 | dst->range = src->range; | |
77b9900e JP |
1727 | } |
1728 | ||
1729 | static const struct rhashtable_params fl_ht_params = { | |
1730 | .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */ | |
1731 | .head_offset = offsetof(struct cls_fl_filter, ht_node), | |
1732 | .automatic_shrinking = true, | |
1733 | }; | |
1734 | ||
05cd271f | 1735 | static int fl_init_mask_hashtable(struct fl_flow_mask *mask) |
77b9900e | 1736 | { |
05cd271f PB |
1737 | mask->filter_ht_params = fl_ht_params; |
1738 | mask->filter_ht_params.key_len = fl_mask_range(mask); | |
1739 | mask->filter_ht_params.key_offset += mask->range.start; | |
77b9900e | 1740 | |
05cd271f | 1741 | return rhashtable_init(&mask->ht, &mask->filter_ht_params); |
77b9900e JP |
1742 | } |
1743 | ||
1744 | #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member) | |
c593642c | 1745 | #define FL_KEY_MEMBER_SIZE(member) sizeof_field(struct fl_flow_key, member) |
77b9900e | 1746 | |
339ba878 HHZ |
1747 | #define FL_KEY_IS_MASKED(mask, member) \ |
1748 | memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \ | |
1749 | 0, FL_KEY_MEMBER_SIZE(member)) \ | |
77b9900e JP |
1750 | |
1751 | #define FL_KEY_SET(keys, cnt, id, member) \ | |
1752 | do { \ | |
1753 | keys[cnt].key_id = id; \ | |
1754 | keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \ | |
1755 | cnt++; \ | |
1756 | } while(0); | |
1757 | ||
339ba878 | 1758 | #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \ |
77b9900e | 1759 | do { \ |
339ba878 | 1760 | if (FL_KEY_IS_MASKED(mask, member)) \ |
77b9900e JP |
1761 | FL_KEY_SET(keys, cnt, id, member); \ |
1762 | } while(0); | |
1763 | ||
33fb5cba JP |
1764 | static void fl_init_dissector(struct flow_dissector *dissector, |
1765 | struct fl_flow_key *mask) | |
77b9900e JP |
1766 | { |
1767 | struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX]; | |
1768 | size_t cnt = 0; | |
1769 | ||
8212ed77 JP |
1770 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
1771 | FLOW_DISSECTOR_KEY_META, meta); | |
42aecaa9 | 1772 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control); |
77b9900e | 1773 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic); |
33fb5cba | 1774 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
339ba878 | 1775 | FLOW_DISSECTOR_KEY_ETH_ADDRS, eth); |
33fb5cba | 1776 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
339ba878 | 1777 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4); |
33fb5cba | 1778 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
339ba878 | 1779 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6); |
8ffb055b YK |
1780 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
1781 | FLOW_DISSECTOR_KEY_PORTS, tp); | |
1782 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, | |
1783 | FLOW_DISSECTOR_KEY_PORTS_RANGE, tp_range); | |
33fb5cba | 1784 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
4d80cc0a | 1785 | FLOW_DISSECTOR_KEY_IP, ip); |
33fb5cba | 1786 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
fdfc7dd6 | 1787 | FLOW_DISSECTOR_KEY_TCP, tcp); |
33fb5cba | 1788 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
7b684884 | 1789 | FLOW_DISSECTOR_KEY_ICMP, icmp); |
33fb5cba | 1790 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
99d31326 | 1791 | FLOW_DISSECTOR_KEY_ARP, arp); |
33fb5cba | 1792 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
a577d8f7 | 1793 | FLOW_DISSECTOR_KEY_MPLS, mpls); |
33fb5cba | 1794 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
9399ae9a | 1795 | FLOW_DISSECTOR_KEY_VLAN, vlan); |
33fb5cba | 1796 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
d64efd09 | 1797 | FLOW_DISSECTOR_KEY_CVLAN, cvlan); |
33fb5cba | 1798 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
519d1052 | 1799 | FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id); |
33fb5cba | 1800 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
519d1052 | 1801 | FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4); |
33fb5cba | 1802 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
519d1052 | 1803 | FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6); |
33fb5cba JP |
1804 | if (FL_KEY_IS_MASKED(mask, enc_ipv4) || |
1805 | FL_KEY_IS_MASKED(mask, enc_ipv6)) | |
519d1052 HHZ |
1806 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL, |
1807 | enc_control); | |
33fb5cba | 1808 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
f4d997fd | 1809 | FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp); |
33fb5cba | 1810 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
0e2c17b6 | 1811 | FLOW_DISSECTOR_KEY_ENC_IP, enc_ip); |
0a6e7778 PJV |
1812 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
1813 | FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts); | |
e0ace68a PB |
1814 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
1815 | FLOW_DISSECTOR_KEY_CT, ct); | |
5923b8f7 AL |
1816 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
1817 | FLOW_DISSECTOR_KEY_HASH, hash); | |
77b9900e | 1818 | |
33fb5cba | 1819 | skb_flow_dissector_init(dissector, keys, cnt); |
05cd271f PB |
1820 | } |
1821 | ||
1822 | static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head, | |
1823 | struct fl_flow_mask *mask) | |
1824 | { | |
1825 | struct fl_flow_mask *newmask; | |
1826 | int err; | |
1827 | ||
1828 | newmask = kzalloc(sizeof(*newmask), GFP_KERNEL); | |
1829 | if (!newmask) | |
1830 | return ERR_PTR(-ENOMEM); | |
1831 | ||
1832 | fl_mask_copy(newmask, mask); | |
1833 | ||
8ffb055b YK |
1834 | if ((newmask->key.tp_range.tp_min.dst && |
1835 | newmask->key.tp_range.tp_max.dst) || | |
1836 | (newmask->key.tp_range.tp_min.src && | |
1837 | newmask->key.tp_range.tp_max.src)) | |
5c72299f AN |
1838 | newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE; |
1839 | ||
05cd271f PB |
1840 | err = fl_init_mask_hashtable(newmask); |
1841 | if (err) | |
1842 | goto errout_free; | |
1843 | ||
33fb5cba | 1844 | fl_init_dissector(&newmask->dissector, &newmask->key); |
05cd271f PB |
1845 | |
1846 | INIT_LIST_HEAD_RCU(&newmask->filters); | |
1847 | ||
f48ef4d5 | 1848 | refcount_set(&newmask->refcnt, 1); |
195c234d VB |
1849 | err = rhashtable_replace_fast(&head->ht, &mask->ht_node, |
1850 | &newmask->ht_node, mask_ht_params); | |
05cd271f PB |
1851 | if (err) |
1852 | goto errout_destroy; | |
1853 | ||
259e60f9 | 1854 | spin_lock(&head->masks_lock); |
05cd271f | 1855 | list_add_tail_rcu(&newmask->list, &head->masks); |
259e60f9 | 1856 | spin_unlock(&head->masks_lock); |
05cd271f PB |
1857 | |
1858 | return newmask; | |
1859 | ||
1860 | errout_destroy: | |
1861 | rhashtable_destroy(&newmask->ht); | |
1862 | errout_free: | |
1863 | kfree(newmask); | |
1864 | ||
1865 | return ERR_PTR(err); | |
77b9900e JP |
1866 | } |
1867 | ||
1868 | static int fl_check_assign_mask(struct cls_fl_head *head, | |
05cd271f PB |
1869 | struct cls_fl_filter *fnew, |
1870 | struct cls_fl_filter *fold, | |
77b9900e JP |
1871 | struct fl_flow_mask *mask) |
1872 | { | |
05cd271f | 1873 | struct fl_flow_mask *newmask; |
f48ef4d5 | 1874 | int ret = 0; |
77b9900e | 1875 | |
f48ef4d5 | 1876 | rcu_read_lock(); |
195c234d VB |
1877 | |
1878 | /* Insert mask as temporary node to prevent concurrent creation of mask | |
1879 | * with same key. Any concurrent lookups with same key will return | |
99815f50 | 1880 | * -EAGAIN because mask's refcnt is zero. |
195c234d VB |
1881 | */ |
1882 | fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht, | |
1883 | &mask->ht_node, | |
1884 | mask_ht_params); | |
05cd271f | 1885 | if (!fnew->mask) { |
f48ef4d5 VB |
1886 | rcu_read_unlock(); |
1887 | ||
195c234d VB |
1888 | if (fold) { |
1889 | ret = -EINVAL; | |
1890 | goto errout_cleanup; | |
1891 | } | |
77b9900e | 1892 | |
05cd271f | 1893 | newmask = fl_create_new_mask(head, mask); |
195c234d VB |
1894 | if (IS_ERR(newmask)) { |
1895 | ret = PTR_ERR(newmask); | |
1896 | goto errout_cleanup; | |
1897 | } | |
77b9900e | 1898 | |
05cd271f | 1899 | fnew->mask = newmask; |
f48ef4d5 | 1900 | return 0; |
195c234d VB |
1901 | } else if (IS_ERR(fnew->mask)) { |
1902 | ret = PTR_ERR(fnew->mask); | |
f6521c58 | 1903 | } else if (fold && fold->mask != fnew->mask) { |
f48ef4d5 VB |
1904 | ret = -EINVAL; |
1905 | } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) { | |
1906 | /* Mask was deleted concurrently, try again */ | |
1907 | ret = -EAGAIN; | |
05cd271f | 1908 | } |
f48ef4d5 VB |
1909 | rcu_read_unlock(); |
1910 | return ret; | |
195c234d VB |
1911 | |
1912 | errout_cleanup: | |
1913 | rhashtable_remove_fast(&head->ht, &mask->ht_node, | |
1914 | mask_ht_params); | |
195c234d | 1915 | return ret; |
77b9900e JP |
1916 | } |
1917 | ||
1918 | static int fl_set_parms(struct net *net, struct tcf_proto *tp, | |
1919 | struct cls_fl_filter *f, struct fl_flow_mask *mask, | |
1920 | unsigned long base, struct nlattr **tb, | |
695176bf | 1921 | struct nlattr *est, |
c86e0209 BZ |
1922 | struct fl_flow_tmplt *tmplt, |
1923 | u32 flags, u32 fl_flags, | |
50a56190 | 1924 | struct netlink_ext_ack *extack) |
77b9900e | 1925 | { |
77b9900e JP |
1926 | int err; |
1927 | ||
c86e0209 BZ |
1928 | err = tcf_exts_validate_ex(net, tp, tb, est, &f->exts, flags, |
1929 | fl_flags, extack); | |
77b9900e JP |
1930 | if (err < 0) |
1931 | return err; | |
1932 | ||
1933 | if (tb[TCA_FLOWER_CLASSID]) { | |
1934 | f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]); | |
695176bf | 1935 | if (flags & TCA_ACT_FLAGS_NO_RTNL) |
c24e43d8 | 1936 | rtnl_lock(); |
77b9900e | 1937 | tcf_bind_filter(tp, &f->res, base); |
695176bf | 1938 | if (flags & TCA_ACT_FLAGS_NO_RTNL) |
c24e43d8 | 1939 | rtnl_unlock(); |
77b9900e JP |
1940 | } |
1941 | ||
1057c55f | 1942 | err = fl_set_key(net, tb, &f->key, &mask->key, extack); |
77b9900e | 1943 | if (err) |
45507529 | 1944 | return err; |
77b9900e JP |
1945 | |
1946 | fl_mask_update_range(mask); | |
1947 | fl_set_masked_key(&f->mkey, &f->key, mask); | |
1948 | ||
b95ec7eb JP |
1949 | if (!fl_mask_fits_tmplt(tmplt, mask)) { |
1950 | NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template"); | |
1951 | return -EINVAL; | |
1952 | } | |
1953 | ||
77b9900e | 1954 | return 0; |
77b9900e JP |
1955 | } |
1956 | ||
1f17f774 VB |
1957 | static int fl_ht_insert_unique(struct cls_fl_filter *fnew, |
1958 | struct cls_fl_filter *fold, | |
1959 | bool *in_ht) | |
1960 | { | |
1961 | struct fl_flow_mask *mask = fnew->mask; | |
1962 | int err; | |
1963 | ||
9e35552a VB |
1964 | err = rhashtable_lookup_insert_fast(&mask->ht, |
1965 | &fnew->ht_node, | |
1966 | mask->filter_ht_params); | |
1f17f774 VB |
1967 | if (err) { |
1968 | *in_ht = false; | |
1969 | /* It is okay if filter with same key exists when | |
1970 | * overwriting. | |
1971 | */ | |
1972 | return fold && err == -EEXIST ? 0 : err; | |
1973 | } | |
1974 | ||
1975 | *in_ht = true; | |
1976 | return 0; | |
1977 | } | |
1978 | ||
77b9900e JP |
1979 | static int fl_change(struct net *net, struct sk_buff *in_skb, |
1980 | struct tcf_proto *tp, unsigned long base, | |
1981 | u32 handle, struct nlattr **tca, | |
695176bf | 1982 | void **arg, u32 flags, |
12db03b6 | 1983 | struct netlink_ext_ack *extack) |
77b9900e | 1984 | { |
e474619a | 1985 | struct cls_fl_head *head = fl_head_dereference(tp); |
695176bf | 1986 | bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL); |
8113c095 | 1987 | struct cls_fl_filter *fold = *arg; |
77b9900e | 1988 | struct cls_fl_filter *fnew; |
2cddd201 | 1989 | struct fl_flow_mask *mask; |
39b7b6a6 | 1990 | struct nlattr **tb; |
1f17f774 | 1991 | bool in_ht; |
77b9900e JP |
1992 | int err; |
1993 | ||
06177558 VB |
1994 | if (!tca[TCA_OPTIONS]) { |
1995 | err = -EINVAL; | |
1996 | goto errout_fold; | |
1997 | } | |
77b9900e | 1998 | |
2cddd201 | 1999 | mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL); |
06177558 VB |
2000 | if (!mask) { |
2001 | err = -ENOBUFS; | |
2002 | goto errout_fold; | |
2003 | } | |
39b7b6a6 | 2004 | |
2cddd201 IV |
2005 | tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); |
2006 | if (!tb) { | |
2007 | err = -ENOBUFS; | |
2008 | goto errout_mask_alloc; | |
2009 | } | |
2010 | ||
8cb08174 JB |
2011 | err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX, |
2012 | tca[TCA_OPTIONS], fl_policy, NULL); | |
77b9900e | 2013 | if (err < 0) |
39b7b6a6 | 2014 | goto errout_tb; |
77b9900e | 2015 | |
39b7b6a6 AB |
2016 | if (fold && handle && fold->handle != handle) { |
2017 | err = -EINVAL; | |
2018 | goto errout_tb; | |
2019 | } | |
77b9900e JP |
2020 | |
2021 | fnew = kzalloc(sizeof(*fnew), GFP_KERNEL); | |
39b7b6a6 AB |
2022 | if (!fnew) { |
2023 | err = -ENOBUFS; | |
2024 | goto errout_tb; | |
2025 | } | |
c049d56e | 2026 | INIT_LIST_HEAD(&fnew->hw_list); |
06177558 | 2027 | refcount_set(&fnew->refcnt, 1); |
77b9900e | 2028 | |
14215108 | 2029 | err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0); |
b9a24bb7 WC |
2030 | if (err < 0) |
2031 | goto errout; | |
77b9900e | 2032 | |
e69985c6 AV |
2033 | if (tb[TCA_FLOWER_FLAGS]) { |
2034 | fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]); | |
2035 | ||
2036 | if (!tc_flags_valid(fnew->flags)) { | |
2037 | err = -EINVAL; | |
ecb3dea4 | 2038 | goto errout; |
e69985c6 AV |
2039 | } |
2040 | } | |
5b33f488 | 2041 | |
695176bf | 2042 | err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], |
c86e0209 BZ |
2043 | tp->chain->tmplt_priv, flags, fnew->flags, |
2044 | extack); | |
77b9900e | 2045 | if (err) |
ecb3dea4 | 2046 | goto errout; |
77b9900e | 2047 | |
2cddd201 | 2048 | err = fl_check_assign_mask(head, fnew, fold, mask); |
77b9900e | 2049 | if (err) |
ecb3dea4 VB |
2050 | goto errout; |
2051 | ||
1f17f774 VB |
2052 | err = fl_ht_insert_unique(fnew, fold, &in_ht); |
2053 | if (err) | |
2054 | goto errout_mask; | |
2055 | ||
79685219 | 2056 | if (!tc_skip_hw(fnew->flags)) { |
c24e43d8 | 2057 | err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack); |
79685219 | 2058 | if (err) |
1f17f774 | 2059 | goto errout_ht; |
79685219 | 2060 | } |
5b33f488 | 2061 | |
55593960 OG |
2062 | if (!tc_in_hw(fnew->flags)) |
2063 | fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW; | |
2064 | ||
3d81e711 VB |
2065 | spin_lock(&tp->lock); |
2066 | ||
272ffaad VB |
2067 | /* tp was deleted concurrently. -EAGAIN will cause caller to lookup |
2068 | * proto again or create new one, if necessary. | |
2069 | */ | |
2070 | if (tp->deleting) { | |
2071 | err = -EAGAIN; | |
2072 | goto errout_hw; | |
2073 | } | |
2074 | ||
5b33f488 | 2075 | if (fold) { |
b2552b8c VB |
2076 | /* Fold filter was deleted concurrently. Retry lookup. */ |
2077 | if (fold->deleted) { | |
2078 | err = -EAGAIN; | |
2079 | goto errout_hw; | |
2080 | } | |
2081 | ||
620da486 VB |
2082 | fnew->handle = handle; |
2083 | ||
1f17f774 VB |
2084 | if (!in_ht) { |
2085 | struct rhashtable_params params = | |
2086 | fnew->mask->filter_ht_params; | |
2087 | ||
2088 | err = rhashtable_insert_fast(&fnew->mask->ht, | |
2089 | &fnew->ht_node, | |
2090 | params); | |
2091 | if (err) | |
2092 | goto errout_hw; | |
2093 | in_ht = true; | |
2094 | } | |
620da486 | 2095 | |
c049d56e | 2096 | refcount_inc(&fnew->refcnt); |
599d2570 RD |
2097 | rhashtable_remove_fast(&fold->mask->ht, |
2098 | &fold->ht_node, | |
2099 | fold->mask->filter_ht_params); | |
234a4624 | 2100 | idr_replace(&head->handle_idr, fnew, fnew->handle); |
ff3532f2 | 2101 | list_replace_rcu(&fold->list, &fnew->list); |
b2552b8c | 2102 | fold->deleted = true; |
620da486 | 2103 | |
3d81e711 VB |
2104 | spin_unlock(&tp->lock); |
2105 | ||
9994677c | 2106 | fl_mask_put(head, fold->mask); |
620da486 | 2107 | if (!tc_skip_hw(fold->flags)) |
c24e43d8 | 2108 | fl_hw_destroy_filter(tp, fold, rtnl_held, NULL); |
77b9900e | 2109 | tcf_unbind_filter(tp, &fold->res); |
06177558 VB |
2110 | /* Caller holds reference to fold, so refcnt is always > 0 |
2111 | * after this. | |
2112 | */ | |
2113 | refcount_dec(&fold->refcnt); | |
2114 | __fl_put(fold); | |
77b9900e | 2115 | } else { |
620da486 VB |
2116 | if (handle) { |
2117 | /* user specifies a handle and it doesn't exist */ | |
2118 | err = idr_alloc_u32(&head->handle_idr, fnew, &handle, | |
2119 | handle, GFP_ATOMIC); | |
9a2d9389 VB |
2120 | |
2121 | /* Filter with specified handle was concurrently | |
2122 | * inserted after initial check in cls_api. This is not | |
2123 | * necessarily an error if NLM_F_EXCL is not set in | |
2124 | * message flags. Returning EAGAIN will cause cls_api to | |
2125 | * try to update concurrently inserted rule. | |
2126 | */ | |
2127 | if (err == -ENOSPC) | |
2128 | err = -EAGAIN; | |
620da486 VB |
2129 | } else { |
2130 | handle = 1; | |
2131 | err = idr_alloc_u32(&head->handle_idr, fnew, &handle, | |
2132 | INT_MAX, GFP_ATOMIC); | |
2133 | } | |
2134 | if (err) | |
2135 | goto errout_hw; | |
2136 | ||
c049d56e | 2137 | refcount_inc(&fnew->refcnt); |
620da486 | 2138 | fnew->handle = handle; |
05cd271f | 2139 | list_add_tail_rcu(&fnew->list, &fnew->mask->filters); |
3d81e711 | 2140 | spin_unlock(&tp->lock); |
77b9900e JP |
2141 | } |
2142 | ||
620da486 VB |
2143 | *arg = fnew; |
2144 | ||
39b7b6a6 | 2145 | kfree(tb); |
99815f50 | 2146 | tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work); |
77b9900e JP |
2147 | return 0; |
2148 | ||
c049d56e VB |
2149 | errout_ht: |
2150 | spin_lock(&tp->lock); | |
620da486 | 2151 | errout_hw: |
c049d56e | 2152 | fnew->deleted = true; |
3d81e711 | 2153 | spin_unlock(&tp->lock); |
620da486 | 2154 | if (!tc_skip_hw(fnew->flags)) |
c24e43d8 | 2155 | fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL); |
1f17f774 VB |
2156 | if (in_ht) |
2157 | rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node, | |
2158 | fnew->mask->filter_ht_params); | |
ecb3dea4 | 2159 | errout_mask: |
9994677c | 2160 | fl_mask_put(head, fnew->mask); |
77b9900e | 2161 | errout: |
c049d56e | 2162 | __fl_put(fnew); |
39b7b6a6 AB |
2163 | errout_tb: |
2164 | kfree(tb); | |
2cddd201 | 2165 | errout_mask_alloc: |
99815f50 | 2166 | tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work); |
06177558 VB |
2167 | errout_fold: |
2168 | if (fold) | |
2169 | __fl_put(fold); | |
77b9900e JP |
2170 | return err; |
2171 | } | |
2172 | ||
571acf21 | 2173 | static int fl_delete(struct tcf_proto *tp, void *arg, bool *last, |
12db03b6 | 2174 | bool rtnl_held, struct netlink_ext_ack *extack) |
77b9900e | 2175 | { |
e474619a | 2176 | struct cls_fl_head *head = fl_head_dereference(tp); |
8113c095 | 2177 | struct cls_fl_filter *f = arg; |
b2552b8c VB |
2178 | bool last_on_mask; |
2179 | int err = 0; | |
77b9900e | 2180 | |
c24e43d8 | 2181 | err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack); |
05cd271f | 2182 | *last = list_empty(&head->masks); |
06177558 VB |
2183 | __fl_put(f); |
2184 | ||
b2552b8c | 2185 | return err; |
77b9900e JP |
2186 | } |
2187 | ||
12db03b6 VB |
2188 | static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
2189 | bool rtnl_held) | |
77b9900e | 2190 | { |
d39d7149 CW |
2191 | struct cls_fl_head *head = fl_head_dereference(tp); |
2192 | unsigned long id = arg->cookie, tmp; | |
77b9900e | 2193 | struct cls_fl_filter *f; |
05cd271f | 2194 | |
01683a14 VB |
2195 | arg->count = arg->skip; |
2196 | ||
d5ef1906 | 2197 | rcu_read_lock(); |
d39d7149 CW |
2198 | idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) { |
2199 | /* don't return filters that are being deleted */ | |
2200 | if (!refcount_inc_not_zero(&f->refcnt)) | |
2201 | continue; | |
d5ef1906 VB |
2202 | rcu_read_unlock(); |
2203 | ||
01683a14 | 2204 | if (arg->fn(tp, f, arg) < 0) { |
06177558 | 2205 | __fl_put(f); |
01683a14 | 2206 | arg->stop = 1; |
d5ef1906 | 2207 | rcu_read_lock(); |
01683a14 | 2208 | break; |
05cd271f | 2209 | } |
06177558 | 2210 | __fl_put(f); |
01683a14 | 2211 | arg->count++; |
d5ef1906 | 2212 | rcu_read_lock(); |
77b9900e | 2213 | } |
d5ef1906 | 2214 | rcu_read_unlock(); |
d39d7149 | 2215 | arg->cookie = id; |
77b9900e JP |
2216 | } |
2217 | ||
c049d56e VB |
2218 | static struct cls_fl_filter * |
2219 | fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add) | |
2220 | { | |
2221 | struct cls_fl_head *head = fl_head_dereference(tp); | |
2222 | ||
2223 | spin_lock(&tp->lock); | |
2224 | if (list_empty(&head->hw_filters)) { | |
2225 | spin_unlock(&tp->lock); | |
2226 | return NULL; | |
2227 | } | |
2228 | ||
2229 | if (!f) | |
2230 | f = list_entry(&head->hw_filters, struct cls_fl_filter, | |
2231 | hw_list); | |
2232 | list_for_each_entry_continue(f, &head->hw_filters, hw_list) { | |
2233 | if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) { | |
2234 | spin_unlock(&tp->lock); | |
2235 | return f; | |
2236 | } | |
2237 | } | |
2238 | ||
2239 | spin_unlock(&tp->lock); | |
2240 | return NULL; | |
2241 | } | |
2242 | ||
a7323311 | 2243 | static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb, |
31533cba JH |
2244 | void *cb_priv, struct netlink_ext_ack *extack) |
2245 | { | |
31533cba | 2246 | struct tcf_block *block = tp->chain->block; |
f9e30088 | 2247 | struct flow_cls_offload cls_flower = {}; |
c049d56e | 2248 | struct cls_fl_filter *f = NULL; |
31533cba JH |
2249 | int err; |
2250 | ||
c049d56e VB |
2251 | /* hw_filters list can only be changed by hw offload functions after |
2252 | * obtaining rtnl lock. Make sure it is not changed while reoffload is | |
2253 | * iterating it. | |
2254 | */ | |
2255 | ASSERT_RTNL(); | |
3a7b6861 | 2256 | |
c049d56e | 2257 | while ((f = fl_get_next_hw_filter(tp, f, add))) { |
95e27a4d JH |
2258 | cls_flower.rule = |
2259 | flow_rule_alloc(tcf_exts_num_actions(&f->exts)); | |
2260 | if (!cls_flower.rule) { | |
2261 | __fl_put(f); | |
2262 | return -ENOMEM; | |
2263 | } | |
31533cba | 2264 | |
95e27a4d | 2265 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, |
d6787147 | 2266 | extack); |
95e27a4d | 2267 | cls_flower.command = add ? |
f9e30088 | 2268 | FLOW_CLS_REPLACE : FLOW_CLS_DESTROY; |
95e27a4d JH |
2269 | cls_flower.cookie = (unsigned long)f; |
2270 | cls_flower.rule->match.dissector = &f->mask->dissector; | |
2271 | cls_flower.rule->match.mask = &f->mask->key; | |
2272 | cls_flower.rule->match.key = &f->mkey; | |
2273 | ||
9c1c0e12 | 2274 | err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts); |
95e27a4d | 2275 | if (err) { |
8f256622 | 2276 | kfree(cls_flower.rule); |
95e27a4d JH |
2277 | if (tc_skip_sw(f->flags)) { |
2278 | NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); | |
2279 | __fl_put(f); | |
2280 | return err; | |
31533cba | 2281 | } |
95e27a4d JH |
2282 | goto next_flow; |
2283 | } | |
31533cba | 2284 | |
95e27a4d JH |
2285 | cls_flower.classid = f->res.classid; |
2286 | ||
40119211 VB |
2287 | err = tc_setup_cb_reoffload(block, tp, add, cb, |
2288 | TC_SETUP_CLSFLOWER, &cls_flower, | |
2289 | cb_priv, &f->flags, | |
2290 | &f->in_hw_count); | |
9c1c0e12 | 2291 | tc_cleanup_offload_action(&cls_flower.rule->action); |
95e27a4d JH |
2292 | kfree(cls_flower.rule); |
2293 | ||
2294 | if (err) { | |
40119211 VB |
2295 | __fl_put(f); |
2296 | return err; | |
31533cba | 2297 | } |
95e27a4d | 2298 | next_flow: |
95e27a4d | 2299 | __fl_put(f); |
31533cba JH |
2300 | } |
2301 | ||
2302 | return 0; | |
2303 | } | |
2304 | ||
a449a3e7 VB |
2305 | static void fl_hw_add(struct tcf_proto *tp, void *type_data) |
2306 | { | |
2307 | struct flow_cls_offload *cls_flower = type_data; | |
2308 | struct cls_fl_filter *f = | |
2309 | (struct cls_fl_filter *) cls_flower->cookie; | |
2310 | struct cls_fl_head *head = fl_head_dereference(tp); | |
2311 | ||
2312 | spin_lock(&tp->lock); | |
2313 | list_add(&f->hw_list, &head->hw_filters); | |
2314 | spin_unlock(&tp->lock); | |
2315 | } | |
2316 | ||
2317 | static void fl_hw_del(struct tcf_proto *tp, void *type_data) | |
2318 | { | |
2319 | struct flow_cls_offload *cls_flower = type_data; | |
2320 | struct cls_fl_filter *f = | |
2321 | (struct cls_fl_filter *) cls_flower->cookie; | |
2322 | ||
2323 | spin_lock(&tp->lock); | |
2324 | if (!list_empty(&f->hw_list)) | |
2325 | list_del_init(&f->hw_list); | |
2326 | spin_unlock(&tp->lock); | |
2327 | } | |
2328 | ||
8f256622 PNA |
2329 | static int fl_hw_create_tmplt(struct tcf_chain *chain, |
2330 | struct fl_flow_tmplt *tmplt) | |
34738452 | 2331 | { |
f9e30088 | 2332 | struct flow_cls_offload cls_flower = {}; |
34738452 | 2333 | struct tcf_block *block = chain->block; |
34738452 | 2334 | |
e3ab786b | 2335 | cls_flower.rule = flow_rule_alloc(0); |
8f256622 PNA |
2336 | if (!cls_flower.rule) |
2337 | return -ENOMEM; | |
2338 | ||
34738452 | 2339 | cls_flower.common.chain_index = chain->index; |
f9e30088 | 2340 | cls_flower.command = FLOW_CLS_TMPLT_CREATE; |
34738452 | 2341 | cls_flower.cookie = (unsigned long) tmplt; |
8f256622 PNA |
2342 | cls_flower.rule->match.dissector = &tmplt->dissector; |
2343 | cls_flower.rule->match.mask = &tmplt->mask; | |
2344 | cls_flower.rule->match.key = &tmplt->dummy_key; | |
34738452 JP |
2345 | |
2346 | /* We don't care if driver (any of them) fails to handle this | |
2347 | * call. It serves just as a hint for it. | |
2348 | */ | |
40119211 | 2349 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true); |
8f256622 PNA |
2350 | kfree(cls_flower.rule); |
2351 | ||
2352 | return 0; | |
34738452 JP |
2353 | } |
2354 | ||
2355 | static void fl_hw_destroy_tmplt(struct tcf_chain *chain, | |
2356 | struct fl_flow_tmplt *tmplt) | |
2357 | { | |
f9e30088 | 2358 | struct flow_cls_offload cls_flower = {}; |
34738452 JP |
2359 | struct tcf_block *block = chain->block; |
2360 | ||
2361 | cls_flower.common.chain_index = chain->index; | |
f9e30088 | 2362 | cls_flower.command = FLOW_CLS_TMPLT_DESTROY; |
34738452 JP |
2363 | cls_flower.cookie = (unsigned long) tmplt; |
2364 | ||
40119211 | 2365 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true); |
34738452 JP |
2366 | } |
2367 | ||
b95ec7eb JP |
2368 | static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain, |
2369 | struct nlattr **tca, | |
2370 | struct netlink_ext_ack *extack) | |
2371 | { | |
2372 | struct fl_flow_tmplt *tmplt; | |
2373 | struct nlattr **tb; | |
2374 | int err; | |
2375 | ||
2376 | if (!tca[TCA_OPTIONS]) | |
2377 | return ERR_PTR(-EINVAL); | |
2378 | ||
2379 | tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); | |
2380 | if (!tb) | |
2381 | return ERR_PTR(-ENOBUFS); | |
8cb08174 JB |
2382 | err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX, |
2383 | tca[TCA_OPTIONS], fl_policy, NULL); | |
b95ec7eb JP |
2384 | if (err) |
2385 | goto errout_tb; | |
2386 | ||
2387 | tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL); | |
1cbc36a5 DC |
2388 | if (!tmplt) { |
2389 | err = -ENOMEM; | |
b95ec7eb | 2390 | goto errout_tb; |
1cbc36a5 | 2391 | } |
b95ec7eb JP |
2392 | tmplt->chain = chain; |
2393 | err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack); | |
2394 | if (err) | |
2395 | goto errout_tmplt; | |
b95ec7eb JP |
2396 | |
2397 | fl_init_dissector(&tmplt->dissector, &tmplt->mask); | |
2398 | ||
8f256622 PNA |
2399 | err = fl_hw_create_tmplt(chain, tmplt); |
2400 | if (err) | |
2401 | goto errout_tmplt; | |
34738452 | 2402 | |
8f256622 | 2403 | kfree(tb); |
b95ec7eb JP |
2404 | return tmplt; |
2405 | ||
2406 | errout_tmplt: | |
2407 | kfree(tmplt); | |
2408 | errout_tb: | |
2409 | kfree(tb); | |
2410 | return ERR_PTR(err); | |
2411 | } | |
2412 | ||
ec3ed293 VB |
2413 | static void fl_tmplt_destroy(void *tmplt_priv) |
2414 | { | |
2415 | struct fl_flow_tmplt *tmplt = tmplt_priv; | |
2416 | ||
95278dda CW |
2417 | fl_hw_destroy_tmplt(tmplt->chain, tmplt); |
2418 | kfree(tmplt); | |
ec3ed293 VB |
2419 | } |
2420 | ||
77b9900e JP |
2421 | static int fl_dump_key_val(struct sk_buff *skb, |
2422 | void *val, int val_type, | |
2423 | void *mask, int mask_type, int len) | |
2424 | { | |
2425 | int err; | |
2426 | ||
2427 | if (!memchr_inv(mask, 0, len)) | |
2428 | return 0; | |
2429 | err = nla_put(skb, val_type, len, val); | |
2430 | if (err) | |
2431 | return err; | |
2432 | if (mask_type != TCA_FLOWER_UNSPEC) { | |
2433 | err = nla_put(skb, mask_type, len, mask); | |
2434 | if (err) | |
2435 | return err; | |
2436 | } | |
2437 | return 0; | |
2438 | } | |
2439 | ||
5c72299f AN |
2440 | static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key, |
2441 | struct fl_flow_key *mask) | |
2442 | { | |
8ffb055b YK |
2443 | if (fl_dump_key_val(skb, &key->tp_range.tp_min.dst, |
2444 | TCA_FLOWER_KEY_PORT_DST_MIN, | |
2445 | &mask->tp_range.tp_min.dst, TCA_FLOWER_UNSPEC, | |
2446 | sizeof(key->tp_range.tp_min.dst)) || | |
2447 | fl_dump_key_val(skb, &key->tp_range.tp_max.dst, | |
2448 | TCA_FLOWER_KEY_PORT_DST_MAX, | |
2449 | &mask->tp_range.tp_max.dst, TCA_FLOWER_UNSPEC, | |
2450 | sizeof(key->tp_range.tp_max.dst)) || | |
2451 | fl_dump_key_val(skb, &key->tp_range.tp_min.src, | |
2452 | TCA_FLOWER_KEY_PORT_SRC_MIN, | |
2453 | &mask->tp_range.tp_min.src, TCA_FLOWER_UNSPEC, | |
2454 | sizeof(key->tp_range.tp_min.src)) || | |
2455 | fl_dump_key_val(skb, &key->tp_range.tp_max.src, | |
2456 | TCA_FLOWER_KEY_PORT_SRC_MAX, | |
2457 | &mask->tp_range.tp_max.src, TCA_FLOWER_UNSPEC, | |
2458 | sizeof(key->tp_range.tp_max.src))) | |
5c72299f AN |
2459 | return -1; |
2460 | ||
2461 | return 0; | |
2462 | } | |
2463 | ||
61aec25a GN |
2464 | static int fl_dump_key_mpls_opt_lse(struct sk_buff *skb, |
2465 | struct flow_dissector_key_mpls *mpls_key, | |
2466 | struct flow_dissector_key_mpls *mpls_mask, | |
2467 | u8 lse_index) | |
2468 | { | |
2469 | struct flow_dissector_mpls_lse *lse_mask = &mpls_mask->ls[lse_index]; | |
2470 | struct flow_dissector_mpls_lse *lse_key = &mpls_key->ls[lse_index]; | |
2471 | int err; | |
2472 | ||
2473 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH, | |
2474 | lse_index + 1); | |
2475 | if (err) | |
2476 | return err; | |
2477 | ||
2478 | if (lse_mask->mpls_ttl) { | |
2479 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL, | |
2480 | lse_key->mpls_ttl); | |
2481 | if (err) | |
2482 | return err; | |
2483 | } | |
2484 | if (lse_mask->mpls_bos) { | |
2485 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS, | |
2486 | lse_key->mpls_bos); | |
2487 | if (err) | |
2488 | return err; | |
2489 | } | |
2490 | if (lse_mask->mpls_tc) { | |
2491 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TC, | |
2492 | lse_key->mpls_tc); | |
2493 | if (err) | |
2494 | return err; | |
2495 | } | |
2496 | if (lse_mask->mpls_label) { | |
7fdd375e GN |
2497 | err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL, |
2498 | lse_key->mpls_label); | |
61aec25a GN |
2499 | if (err) |
2500 | return err; | |
2501 | } | |
2502 | ||
2503 | return 0; | |
2504 | } | |
2505 | ||
2506 | static int fl_dump_key_mpls_opts(struct sk_buff *skb, | |
2507 | struct flow_dissector_key_mpls *mpls_key, | |
2508 | struct flow_dissector_key_mpls *mpls_mask) | |
2509 | { | |
2510 | struct nlattr *opts; | |
2511 | struct nlattr *lse; | |
2512 | u8 lse_index; | |
2513 | int err; | |
2514 | ||
2515 | opts = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS); | |
2516 | if (!opts) | |
2517 | return -EMSGSIZE; | |
2518 | ||
2519 | for (lse_index = 0; lse_index < FLOW_DIS_MPLS_MAX; lse_index++) { | |
2520 | if (!(mpls_mask->used_lses & 1 << lse_index)) | |
2521 | continue; | |
2522 | ||
2523 | lse = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS_LSE); | |
2524 | if (!lse) { | |
2525 | err = -EMSGSIZE; | |
2526 | goto err_opts; | |
2527 | } | |
2528 | ||
2529 | err = fl_dump_key_mpls_opt_lse(skb, mpls_key, mpls_mask, | |
2530 | lse_index); | |
2531 | if (err) | |
2532 | goto err_opts_lse; | |
2533 | nla_nest_end(skb, lse); | |
2534 | } | |
2535 | nla_nest_end(skb, opts); | |
2536 | ||
2537 | return 0; | |
2538 | ||
2539 | err_opts_lse: | |
2540 | nla_nest_cancel(skb, lse); | |
2541 | err_opts: | |
2542 | nla_nest_cancel(skb, opts); | |
2543 | ||
2544 | return err; | |
2545 | } | |
2546 | ||
a577d8f7 BL |
2547 | static int fl_dump_key_mpls(struct sk_buff *skb, |
2548 | struct flow_dissector_key_mpls *mpls_key, | |
2549 | struct flow_dissector_key_mpls *mpls_mask) | |
2550 | { | |
58cff782 GN |
2551 | struct flow_dissector_mpls_lse *lse_mask; |
2552 | struct flow_dissector_mpls_lse *lse_key; | |
a577d8f7 BL |
2553 | int err; |
2554 | ||
61aec25a | 2555 | if (!mpls_mask->used_lses) |
a577d8f7 | 2556 | return 0; |
58cff782 GN |
2557 | |
2558 | lse_mask = &mpls_mask->ls[0]; | |
2559 | lse_key = &mpls_key->ls[0]; | |
2560 | ||
61aec25a GN |
2561 | /* For backward compatibility, don't use the MPLS nested attributes if |
2562 | * the rule can be expressed using the old attributes. | |
2563 | */ | |
2564 | if (mpls_mask->used_lses & ~1 || | |
2565 | (!lse_mask->mpls_ttl && !lse_mask->mpls_bos && | |
2566 | !lse_mask->mpls_tc && !lse_mask->mpls_label)) | |
2567 | return fl_dump_key_mpls_opts(skb, mpls_key, mpls_mask); | |
2568 | ||
58cff782 | 2569 | if (lse_mask->mpls_ttl) { |
a577d8f7 | 2570 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL, |
58cff782 | 2571 | lse_key->mpls_ttl); |
a577d8f7 BL |
2572 | if (err) |
2573 | return err; | |
2574 | } | |
58cff782 | 2575 | if (lse_mask->mpls_tc) { |
a577d8f7 | 2576 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC, |
58cff782 | 2577 | lse_key->mpls_tc); |
a577d8f7 BL |
2578 | if (err) |
2579 | return err; | |
2580 | } | |
58cff782 | 2581 | if (lse_mask->mpls_label) { |
a577d8f7 | 2582 | err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL, |
58cff782 | 2583 | lse_key->mpls_label); |
a577d8f7 BL |
2584 | if (err) |
2585 | return err; | |
2586 | } | |
58cff782 | 2587 | if (lse_mask->mpls_bos) { |
a577d8f7 | 2588 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS, |
58cff782 | 2589 | lse_key->mpls_bos); |
a577d8f7 BL |
2590 | if (err) |
2591 | return err; | |
2592 | } | |
2593 | return 0; | |
2594 | } | |
2595 | ||
0e2c17b6 | 2596 | static int fl_dump_key_ip(struct sk_buff *skb, bool encap, |
4d80cc0a OG |
2597 | struct flow_dissector_key_ip *key, |
2598 | struct flow_dissector_key_ip *mask) | |
2599 | { | |
0e2c17b6 OG |
2600 | int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS; |
2601 | int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL; | |
2602 | int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK; | |
2603 | int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK; | |
2604 | ||
2605 | if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) || | |
2606 | fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl))) | |
4d80cc0a OG |
2607 | return -1; |
2608 | ||
2609 | return 0; | |
2610 | } | |
2611 | ||
9399ae9a | 2612 | static int fl_dump_key_vlan(struct sk_buff *skb, |
d64efd09 | 2613 | int vlan_id_key, int vlan_prio_key, |
9399ae9a HHZ |
2614 | struct flow_dissector_key_vlan *vlan_key, |
2615 | struct flow_dissector_key_vlan *vlan_mask) | |
2616 | { | |
2617 | int err; | |
2618 | ||
2619 | if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask))) | |
2620 | return 0; | |
2621 | if (vlan_mask->vlan_id) { | |
d64efd09 | 2622 | err = nla_put_u16(skb, vlan_id_key, |
9399ae9a HHZ |
2623 | vlan_key->vlan_id); |
2624 | if (err) | |
2625 | return err; | |
2626 | } | |
2627 | if (vlan_mask->vlan_priority) { | |
d64efd09 | 2628 | err = nla_put_u8(skb, vlan_prio_key, |
9399ae9a HHZ |
2629 | vlan_key->vlan_priority); |
2630 | if (err) | |
2631 | return err; | |
2632 | } | |
2633 | return 0; | |
2634 | } | |
2635 | ||
faa3ffce OG |
2636 | static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask, |
2637 | u32 *flower_key, u32 *flower_mask, | |
2638 | u32 flower_flag_bit, u32 dissector_flag_bit) | |
2639 | { | |
2640 | if (dissector_mask & dissector_flag_bit) { | |
2641 | *flower_mask |= flower_flag_bit; | |
2642 | if (dissector_key & dissector_flag_bit) | |
2643 | *flower_key |= flower_flag_bit; | |
2644 | } | |
2645 | } | |
2646 | ||
2647 | static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask) | |
2648 | { | |
2649 | u32 key, mask; | |
2650 | __be32 _key, _mask; | |
2651 | int err; | |
2652 | ||
2653 | if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask))) | |
2654 | return 0; | |
2655 | ||
2656 | key = 0; | |
2657 | mask = 0; | |
2658 | ||
2659 | fl_get_key_flag(flags_key, flags_mask, &key, &mask, | |
2660 | TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); | |
459d153d PJV |
2661 | fl_get_key_flag(flags_key, flags_mask, &key, &mask, |
2662 | TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, | |
2663 | FLOW_DIS_FIRST_FRAG); | |
faa3ffce OG |
2664 | |
2665 | _key = cpu_to_be32(key); | |
2666 | _mask = cpu_to_be32(mask); | |
2667 | ||
2668 | err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key); | |
2669 | if (err) | |
2670 | return err; | |
2671 | ||
2672 | return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask); | |
2673 | } | |
2674 | ||
0a6e7778 PJV |
2675 | static int fl_dump_key_geneve_opt(struct sk_buff *skb, |
2676 | struct flow_dissector_key_enc_opts *enc_opts) | |
2677 | { | |
2678 | struct geneve_opt *opt; | |
2679 | struct nlattr *nest; | |
2680 | int opt_off = 0; | |
2681 | ||
ae0be8de | 2682 | nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE); |
0a6e7778 PJV |
2683 | if (!nest) |
2684 | goto nla_put_failure; | |
2685 | ||
2686 | while (enc_opts->len > opt_off) { | |
2687 | opt = (struct geneve_opt *)&enc_opts->data[opt_off]; | |
2688 | ||
2689 | if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS, | |
2690 | opt->opt_class)) | |
2691 | goto nla_put_failure; | |
2692 | if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE, | |
2693 | opt->type)) | |
2694 | goto nla_put_failure; | |
2695 | if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA, | |
2696 | opt->length * 4, opt->opt_data)) | |
2697 | goto nla_put_failure; | |
2698 | ||
2699 | opt_off += sizeof(struct geneve_opt) + opt->length * 4; | |
2700 | } | |
2701 | nla_nest_end(skb, nest); | |
2702 | return 0; | |
2703 | ||
2704 | nla_put_failure: | |
2705 | nla_nest_cancel(skb, nest); | |
2706 | return -EMSGSIZE; | |
2707 | } | |
2708 | ||
d8f9dfae XL |
2709 | static int fl_dump_key_vxlan_opt(struct sk_buff *skb, |
2710 | struct flow_dissector_key_enc_opts *enc_opts) | |
2711 | { | |
2712 | struct vxlan_metadata *md; | |
2713 | struct nlattr *nest; | |
2714 | ||
2715 | nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_VXLAN); | |
2716 | if (!nest) | |
2717 | goto nla_put_failure; | |
2718 | ||
2719 | md = (struct vxlan_metadata *)&enc_opts->data[0]; | |
2720 | if (nla_put_u32(skb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, md->gbp)) | |
2721 | goto nla_put_failure; | |
2722 | ||
2723 | nla_nest_end(skb, nest); | |
2724 | return 0; | |
2725 | ||
2726 | nla_put_failure: | |
2727 | nla_nest_cancel(skb, nest); | |
2728 | return -EMSGSIZE; | |
2729 | } | |
2730 | ||
79b1011c XL |
2731 | static int fl_dump_key_erspan_opt(struct sk_buff *skb, |
2732 | struct flow_dissector_key_enc_opts *enc_opts) | |
2733 | { | |
2734 | struct erspan_metadata *md; | |
2735 | struct nlattr *nest; | |
2736 | ||
2737 | nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_ERSPAN); | |
2738 | if (!nest) | |
2739 | goto nla_put_failure; | |
2740 | ||
2741 | md = (struct erspan_metadata *)&enc_opts->data[0]; | |
2742 | if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER, md->version)) | |
2743 | goto nla_put_failure; | |
2744 | ||
2745 | if (md->version == 1 && | |
2746 | nla_put_be32(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index)) | |
2747 | goto nla_put_failure; | |
2748 | ||
2749 | if (md->version == 2 && | |
2750 | (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR, | |
2751 | md->u.md2.dir) || | |
2752 | nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID, | |
2753 | get_hwid(&md->u.md2)))) | |
2754 | goto nla_put_failure; | |
2755 | ||
2756 | nla_nest_end(skb, nest); | |
2757 | return 0; | |
2758 | ||
2759 | nla_put_failure: | |
2760 | nla_nest_cancel(skb, nest); | |
2761 | return -EMSGSIZE; | |
2762 | } | |
2763 | ||
e0ace68a PB |
2764 | static int fl_dump_key_ct(struct sk_buff *skb, |
2765 | struct flow_dissector_key_ct *key, | |
2766 | struct flow_dissector_key_ct *mask) | |
2767 | { | |
2768 | if (IS_ENABLED(CONFIG_NF_CONNTRACK) && | |
2769 | fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE, | |
2770 | &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK, | |
2771 | sizeof(key->ct_state))) | |
2772 | goto nla_put_failure; | |
2773 | ||
2774 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && | |
2775 | fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE, | |
2776 | &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK, | |
2777 | sizeof(key->ct_zone))) | |
2778 | goto nla_put_failure; | |
2779 | ||
2780 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && | |
2781 | fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK, | |
2782 | &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK, | |
2783 | sizeof(key->ct_mark))) | |
2784 | goto nla_put_failure; | |
2785 | ||
2786 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && | |
2787 | fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS, | |
2788 | &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK, | |
2789 | sizeof(key->ct_labels))) | |
2790 | goto nla_put_failure; | |
2791 | ||
2792 | return 0; | |
2793 | ||
2794 | nla_put_failure: | |
2795 | return -EMSGSIZE; | |
2796 | } | |
2797 | ||
0a6e7778 PJV |
2798 | static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type, |
2799 | struct flow_dissector_key_enc_opts *enc_opts) | |
2800 | { | |
2801 | struct nlattr *nest; | |
2802 | int err; | |
2803 | ||
2804 | if (!enc_opts->len) | |
2805 | return 0; | |
2806 | ||
ae0be8de | 2807 | nest = nla_nest_start_noflag(skb, enc_opt_type); |
0a6e7778 PJV |
2808 | if (!nest) |
2809 | goto nla_put_failure; | |
2810 | ||
2811 | switch (enc_opts->dst_opt_type) { | |
2812 | case TUNNEL_GENEVE_OPT: | |
2813 | err = fl_dump_key_geneve_opt(skb, enc_opts); | |
2814 | if (err) | |
2815 | goto nla_put_failure; | |
2816 | break; | |
d8f9dfae XL |
2817 | case TUNNEL_VXLAN_OPT: |
2818 | err = fl_dump_key_vxlan_opt(skb, enc_opts); | |
2819 | if (err) | |
2820 | goto nla_put_failure; | |
2821 | break; | |
79b1011c XL |
2822 | case TUNNEL_ERSPAN_OPT: |
2823 | err = fl_dump_key_erspan_opt(skb, enc_opts); | |
2824 | if (err) | |
2825 | goto nla_put_failure; | |
2826 | break; | |
0a6e7778 PJV |
2827 | default: |
2828 | goto nla_put_failure; | |
2829 | } | |
2830 | nla_nest_end(skb, nest); | |
2831 | return 0; | |
2832 | ||
2833 | nla_put_failure: | |
2834 | nla_nest_cancel(skb, nest); | |
2835 | return -EMSGSIZE; | |
2836 | } | |
2837 | ||
2838 | static int fl_dump_key_enc_opt(struct sk_buff *skb, | |
2839 | struct flow_dissector_key_enc_opts *key_opts, | |
2840 | struct flow_dissector_key_enc_opts *msk_opts) | |
2841 | { | |
2842 | int err; | |
2843 | ||
2844 | err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts); | |
2845 | if (err) | |
2846 | return err; | |
2847 | ||
2848 | return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts); | |
2849 | } | |
2850 | ||
f5749081 JP |
2851 | static int fl_dump_key(struct sk_buff *skb, struct net *net, |
2852 | struct fl_flow_key *key, struct fl_flow_key *mask) | |
77b9900e | 2853 | { |
8212ed77 | 2854 | if (mask->meta.ingress_ifindex) { |
77b9900e JP |
2855 | struct net_device *dev; |
2856 | ||
8212ed77 | 2857 | dev = __dev_get_by_index(net, key->meta.ingress_ifindex); |
77b9900e JP |
2858 | if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name)) |
2859 | goto nla_put_failure; | |
2860 | } | |
2861 | ||
2862 | if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, | |
2863 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, | |
2864 | sizeof(key->eth.dst)) || | |
2865 | fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, | |
2866 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, | |
2867 | sizeof(key->eth.src)) || | |
2868 | fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE, | |
2869 | &mask->basic.n_proto, TCA_FLOWER_UNSPEC, | |
2870 | sizeof(key->basic.n_proto))) | |
2871 | goto nla_put_failure; | |
9399ae9a | 2872 | |
a577d8f7 BL |
2873 | if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls)) |
2874 | goto nla_put_failure; | |
2875 | ||
d64efd09 JL |
2876 | if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID, |
2877 | TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan)) | |
9399ae9a HHZ |
2878 | goto nla_put_failure; |
2879 | ||
d64efd09 JL |
2880 | if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID, |
2881 | TCA_FLOWER_KEY_CVLAN_PRIO, | |
2882 | &key->cvlan, &mask->cvlan) || | |
2883 | (mask->cvlan.vlan_tpid && | |
158abbf1 JL |
2884 | nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, |
2885 | key->cvlan.vlan_tpid))) | |
d3069512 JL |
2886 | goto nla_put_failure; |
2887 | ||
5e9a0fe4 JL |
2888 | if (mask->basic.n_proto) { |
2889 | if (mask->cvlan.vlan_tpid) { | |
2890 | if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE, | |
2891 | key->basic.n_proto)) | |
2892 | goto nla_put_failure; | |
2893 | } else if (mask->vlan.vlan_tpid) { | |
2894 | if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, | |
2895 | key->basic.n_proto)) | |
2896 | goto nla_put_failure; | |
2897 | } | |
d64efd09 JL |
2898 | } |
2899 | ||
77b9900e JP |
2900 | if ((key->basic.n_proto == htons(ETH_P_IP) || |
2901 | key->basic.n_proto == htons(ETH_P_IPV6)) && | |
4d80cc0a | 2902 | (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, |
77b9900e | 2903 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, |
4d80cc0a | 2904 | sizeof(key->basic.ip_proto)) || |
0e2c17b6 | 2905 | fl_dump_key_ip(skb, false, &key->ip, &mask->ip))) |
77b9900e JP |
2906 | goto nla_put_failure; |
2907 | ||
c3f83241 | 2908 | if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
77b9900e JP |
2909 | (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
2910 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, | |
2911 | sizeof(key->ipv4.src)) || | |
2912 | fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, | |
2913 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, | |
2914 | sizeof(key->ipv4.dst)))) | |
2915 | goto nla_put_failure; | |
c3f83241 | 2916 | else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && |
77b9900e JP |
2917 | (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
2918 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, | |
2919 | sizeof(key->ipv6.src)) || | |
2920 | fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, | |
2921 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, | |
2922 | sizeof(key->ipv6.dst)))) | |
2923 | goto nla_put_failure; | |
2924 | ||
2925 | if (key->basic.ip_proto == IPPROTO_TCP && | |
2926 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, | |
aa72d708 | 2927 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
77b9900e JP |
2928 | sizeof(key->tp.src)) || |
2929 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, | |
aa72d708 | 2930 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
fdfc7dd6 JP |
2931 | sizeof(key->tp.dst)) || |
2932 | fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, | |
2933 | &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, | |
2934 | sizeof(key->tcp.flags)))) | |
77b9900e JP |
2935 | goto nla_put_failure; |
2936 | else if (key->basic.ip_proto == IPPROTO_UDP && | |
2937 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, | |
aa72d708 | 2938 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
77b9900e JP |
2939 | sizeof(key->tp.src)) || |
2940 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, | |
aa72d708 | 2941 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
5976c5f4 SH |
2942 | sizeof(key->tp.dst)))) |
2943 | goto nla_put_failure; | |
2944 | else if (key->basic.ip_proto == IPPROTO_SCTP && | |
2945 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, | |
2946 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, | |
2947 | sizeof(key->tp.src)) || | |
2948 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, | |
2949 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, | |
77b9900e JP |
2950 | sizeof(key->tp.dst)))) |
2951 | goto nla_put_failure; | |
7b684884 SH |
2952 | else if (key->basic.n_proto == htons(ETH_P_IP) && |
2953 | key->basic.ip_proto == IPPROTO_ICMP && | |
2954 | (fl_dump_key_val(skb, &key->icmp.type, | |
2955 | TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type, | |
2956 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, | |
2957 | sizeof(key->icmp.type)) || | |
2958 | fl_dump_key_val(skb, &key->icmp.code, | |
2959 | TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code, | |
2960 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK, | |
2961 | sizeof(key->icmp.code)))) | |
2962 | goto nla_put_failure; | |
2963 | else if (key->basic.n_proto == htons(ETH_P_IPV6) && | |
2964 | key->basic.ip_proto == IPPROTO_ICMPV6 && | |
2965 | (fl_dump_key_val(skb, &key->icmp.type, | |
2966 | TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type, | |
2967 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, | |
2968 | sizeof(key->icmp.type)) || | |
2969 | fl_dump_key_val(skb, &key->icmp.code, | |
2970 | TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code, | |
2971 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK, | |
2972 | sizeof(key->icmp.code)))) | |
2973 | goto nla_put_failure; | |
99d31326 SH |
2974 | else if ((key->basic.n_proto == htons(ETH_P_ARP) || |
2975 | key->basic.n_proto == htons(ETH_P_RARP)) && | |
2976 | (fl_dump_key_val(skb, &key->arp.sip, | |
2977 | TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip, | |
2978 | TCA_FLOWER_KEY_ARP_SIP_MASK, | |
2979 | sizeof(key->arp.sip)) || | |
2980 | fl_dump_key_val(skb, &key->arp.tip, | |
2981 | TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip, | |
2982 | TCA_FLOWER_KEY_ARP_TIP_MASK, | |
2983 | sizeof(key->arp.tip)) || | |
2984 | fl_dump_key_val(skb, &key->arp.op, | |
2985 | TCA_FLOWER_KEY_ARP_OP, &mask->arp.op, | |
2986 | TCA_FLOWER_KEY_ARP_OP_MASK, | |
2987 | sizeof(key->arp.op)) || | |
2988 | fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, | |
2989 | mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, | |
2990 | sizeof(key->arp.sha)) || | |
2991 | fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, | |
2992 | mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, | |
2993 | sizeof(key->arp.tha)))) | |
2994 | goto nla_put_failure; | |
77b9900e | 2995 | |
5c72299f AN |
2996 | if ((key->basic.ip_proto == IPPROTO_TCP || |
2997 | key->basic.ip_proto == IPPROTO_UDP || | |
2998 | key->basic.ip_proto == IPPROTO_SCTP) && | |
2999 | fl_dump_key_port_range(skb, key, mask)) | |
3000 | goto nla_put_failure; | |
3001 | ||
bc3103f1 AV |
3002 | if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
3003 | (fl_dump_key_val(skb, &key->enc_ipv4.src, | |
3004 | TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src, | |
3005 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, | |
3006 | sizeof(key->enc_ipv4.src)) || | |
3007 | fl_dump_key_val(skb, &key->enc_ipv4.dst, | |
3008 | TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst, | |
3009 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, | |
3010 | sizeof(key->enc_ipv4.dst)))) | |
3011 | goto nla_put_failure; | |
3012 | else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && | |
3013 | (fl_dump_key_val(skb, &key->enc_ipv6.src, | |
3014 | TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src, | |
3015 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, | |
3016 | sizeof(key->enc_ipv6.src)) || | |
3017 | fl_dump_key_val(skb, &key->enc_ipv6.dst, | |
3018 | TCA_FLOWER_KEY_ENC_IPV6_DST, | |
3019 | &mask->enc_ipv6.dst, | |
3020 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, | |
3021 | sizeof(key->enc_ipv6.dst)))) | |
3022 | goto nla_put_failure; | |
3023 | ||
3024 | if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID, | |
eb523f42 | 3025 | &mask->enc_key_id, TCA_FLOWER_UNSPEC, |
f4d997fd HHZ |
3026 | sizeof(key->enc_key_id)) || |
3027 | fl_dump_key_val(skb, &key->enc_tp.src, | |
3028 | TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, | |
3029 | &mask->enc_tp.src, | |
3030 | TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, | |
3031 | sizeof(key->enc_tp.src)) || | |
3032 | fl_dump_key_val(skb, &key->enc_tp.dst, | |
3033 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT, | |
3034 | &mask->enc_tp.dst, | |
3035 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, | |
0e2c17b6 | 3036 | sizeof(key->enc_tp.dst)) || |
0a6e7778 PJV |
3037 | fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) || |
3038 | fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts)) | |
bc3103f1 AV |
3039 | goto nla_put_failure; |
3040 | ||
e0ace68a PB |
3041 | if (fl_dump_key_ct(skb, &key->ct, &mask->ct)) |
3042 | goto nla_put_failure; | |
3043 | ||
faa3ffce OG |
3044 | if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags)) |
3045 | goto nla_put_failure; | |
3046 | ||
5923b8f7 AL |
3047 | if (fl_dump_key_val(skb, &key->hash.hash, TCA_FLOWER_KEY_HASH, |
3048 | &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK, | |
3049 | sizeof(key->hash.hash))) | |
3050 | goto nla_put_failure; | |
3051 | ||
f5749081 JP |
3052 | return 0; |
3053 | ||
3054 | nla_put_failure: | |
3055 | return -EMSGSIZE; | |
3056 | } | |
3057 | ||
3058 | static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh, | |
12db03b6 | 3059 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
f5749081 JP |
3060 | { |
3061 | struct cls_fl_filter *f = fh; | |
3062 | struct nlattr *nest; | |
3063 | struct fl_flow_key *key, *mask; | |
3d81e711 | 3064 | bool skip_hw; |
f5749081 JP |
3065 | |
3066 | if (!f) | |
3067 | return skb->len; | |
3068 | ||
3069 | t->tcm_handle = f->handle; | |
3070 | ||
ae0be8de | 3071 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
f5749081 JP |
3072 | if (!nest) |
3073 | goto nla_put_failure; | |
3074 | ||
3d81e711 VB |
3075 | spin_lock(&tp->lock); |
3076 | ||
f5749081 JP |
3077 | if (f->res.classid && |
3078 | nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid)) | |
3d81e711 | 3079 | goto nla_put_failure_locked; |
f5749081 JP |
3080 | |
3081 | key = &f->key; | |
3082 | mask = &f->mask->key; | |
3d81e711 | 3083 | skip_hw = tc_skip_hw(f->flags); |
f5749081 JP |
3084 | |
3085 | if (fl_dump_key(skb, net, key, mask)) | |
3d81e711 | 3086 | goto nla_put_failure_locked; |
f5749081 | 3087 | |
749e6720 | 3088 | if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags)) |
3d81e711 VB |
3089 | goto nla_put_failure_locked; |
3090 | ||
3091 | spin_unlock(&tp->lock); | |
3092 | ||
3093 | if (!skip_hw) | |
c24e43d8 | 3094 | fl_hw_update_stats(tp, f, rtnl_held); |
e69985c6 | 3095 | |
86c55361 VB |
3096 | if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count)) |
3097 | goto nla_put_failure; | |
3098 | ||
77b9900e JP |
3099 | if (tcf_exts_dump(skb, &f->exts)) |
3100 | goto nla_put_failure; | |
3101 | ||
3102 | nla_nest_end(skb, nest); | |
3103 | ||
3104 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) | |
3105 | goto nla_put_failure; | |
3106 | ||
3107 | return skb->len; | |
3108 | ||
3d81e711 VB |
3109 | nla_put_failure_locked: |
3110 | spin_unlock(&tp->lock); | |
77b9900e JP |
3111 | nla_put_failure: |
3112 | nla_nest_cancel(skb, nest); | |
3113 | return -1; | |
3114 | } | |
3115 | ||
0348451d VB |
3116 | static int fl_terse_dump(struct net *net, struct tcf_proto *tp, void *fh, |
3117 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) | |
3118 | { | |
3119 | struct cls_fl_filter *f = fh; | |
3120 | struct nlattr *nest; | |
3121 | bool skip_hw; | |
3122 | ||
3123 | if (!f) | |
3124 | return skb->len; | |
3125 | ||
3126 | t->tcm_handle = f->handle; | |
3127 | ||
3128 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); | |
3129 | if (!nest) | |
3130 | goto nla_put_failure; | |
3131 | ||
3132 | spin_lock(&tp->lock); | |
3133 | ||
3134 | skip_hw = tc_skip_hw(f->flags); | |
3135 | ||
3136 | if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags)) | |
3137 | goto nla_put_failure_locked; | |
3138 | ||
3139 | spin_unlock(&tp->lock); | |
3140 | ||
3141 | if (!skip_hw) | |
3142 | fl_hw_update_stats(tp, f, rtnl_held); | |
3143 | ||
3144 | if (tcf_exts_terse_dump(skb, &f->exts)) | |
3145 | goto nla_put_failure; | |
3146 | ||
3147 | nla_nest_end(skb, nest); | |
3148 | ||
3149 | return skb->len; | |
3150 | ||
3151 | nla_put_failure_locked: | |
3152 | spin_unlock(&tp->lock); | |
3153 | nla_put_failure: | |
3154 | nla_nest_cancel(skb, nest); | |
3155 | return -1; | |
3156 | } | |
3157 | ||
b95ec7eb JP |
3158 | static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv) |
3159 | { | |
3160 | struct fl_flow_tmplt *tmplt = tmplt_priv; | |
3161 | struct fl_flow_key *key, *mask; | |
3162 | struct nlattr *nest; | |
3163 | ||
ae0be8de | 3164 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
b95ec7eb JP |
3165 | if (!nest) |
3166 | goto nla_put_failure; | |
3167 | ||
3168 | key = &tmplt->dummy_key; | |
3169 | mask = &tmplt->mask; | |
3170 | ||
3171 | if (fl_dump_key(skb, net, key, mask)) | |
3172 | goto nla_put_failure; | |
3173 | ||
3174 | nla_nest_end(skb, nest); | |
3175 | ||
3176 | return skb->len; | |
3177 | ||
3178 | nla_put_failure: | |
3179 | nla_nest_cancel(skb, nest); | |
3180 | return -EMSGSIZE; | |
3181 | } | |
3182 | ||
2e24cd75 CW |
3183 | static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q, |
3184 | unsigned long base) | |
07d79fc7 CW |
3185 | { |
3186 | struct cls_fl_filter *f = fh; | |
3187 | ||
2e24cd75 CW |
3188 | if (f && f->res.classid == classid) { |
3189 | if (cl) | |
3190 | __tcf_bind_filter(q, &f->res, base); | |
3191 | else | |
3192 | __tcf_unbind_filter(q, &f->res); | |
3193 | } | |
07d79fc7 CW |
3194 | } |
3195 | ||
a5b72a08 DC |
3196 | static bool fl_delete_empty(struct tcf_proto *tp) |
3197 | { | |
3198 | struct cls_fl_head *head = fl_head_dereference(tp); | |
3199 | ||
3200 | spin_lock(&tp->lock); | |
3201 | tp->deleting = idr_is_empty(&head->handle_idr); | |
3202 | spin_unlock(&tp->lock); | |
3203 | ||
3204 | return tp->deleting; | |
3205 | } | |
3206 | ||
77b9900e JP |
3207 | static struct tcf_proto_ops cls_fl_ops __read_mostly = { |
3208 | .kind = "flower", | |
3209 | .classify = fl_classify, | |
3210 | .init = fl_init, | |
3211 | .destroy = fl_destroy, | |
3212 | .get = fl_get, | |
06177558 | 3213 | .put = fl_put, |
77b9900e JP |
3214 | .change = fl_change, |
3215 | .delete = fl_delete, | |
a5b72a08 | 3216 | .delete_empty = fl_delete_empty, |
77b9900e | 3217 | .walk = fl_walk, |
31533cba | 3218 | .reoffload = fl_reoffload, |
a449a3e7 VB |
3219 | .hw_add = fl_hw_add, |
3220 | .hw_del = fl_hw_del, | |
77b9900e | 3221 | .dump = fl_dump, |
0348451d | 3222 | .terse_dump = fl_terse_dump, |
07d79fc7 | 3223 | .bind_class = fl_bind_class, |
b95ec7eb JP |
3224 | .tmplt_create = fl_tmplt_create, |
3225 | .tmplt_destroy = fl_tmplt_destroy, | |
3226 | .tmplt_dump = fl_tmplt_dump, | |
77b9900e | 3227 | .owner = THIS_MODULE, |
92149190 | 3228 | .flags = TCF_PROTO_OPS_DOIT_UNLOCKED, |
77b9900e JP |
3229 | }; |
3230 | ||
3231 | static int __init cls_fl_init(void) | |
3232 | { | |
3233 | return register_tcf_proto_ops(&cls_fl_ops); | |
3234 | } | |
3235 | ||
3236 | static void __exit cls_fl_exit(void) | |
3237 | { | |
3238 | unregister_tcf_proto_ops(&cls_fl_ops); | |
3239 | } | |
3240 | ||
3241 | module_init(cls_fl_init); | |
3242 | module_exit(cls_fl_exit); | |
3243 | ||
3244 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); | |
3245 | MODULE_DESCRIPTION("Flower classifier"); | |
3246 | MODULE_LICENSE("GPL v2"); |