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