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