cls_flower: Support multiple masks per priority
[linux-2.6-block.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>
27
bc3103f1
AV
28#include <net/dst.h>
29#include <net/dst_metadata.h>
30
77b9900e
JP
31struct fl_flow_key {
32 int indev_ifindex;
42aecaa9 33 struct flow_dissector_key_control control;
bc3103f1 34 struct flow_dissector_key_control enc_control;
77b9900e
JP
35 struct flow_dissector_key_basic basic;
36 struct flow_dissector_key_eth_addrs eth;
9399ae9a 37 struct flow_dissector_key_vlan vlan;
77b9900e 38 union {
c3f83241 39 struct flow_dissector_key_ipv4_addrs ipv4;
77b9900e
JP
40 struct flow_dissector_key_ipv6_addrs ipv6;
41 };
42 struct flow_dissector_key_ports tp;
7b684884 43 struct flow_dissector_key_icmp icmp;
99d31326 44 struct flow_dissector_key_arp arp;
bc3103f1
AV
45 struct flow_dissector_key_keyid enc_key_id;
46 union {
47 struct flow_dissector_key_ipv4_addrs enc_ipv4;
48 struct flow_dissector_key_ipv6_addrs enc_ipv6;
49 };
f4d997fd 50 struct flow_dissector_key_ports enc_tp;
a577d8f7 51 struct flow_dissector_key_mpls mpls;
fdfc7dd6 52 struct flow_dissector_key_tcp tcp;
4d80cc0a 53 struct flow_dissector_key_ip ip;
77b9900e
JP
54} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
55
56struct fl_flow_mask_range {
57 unsigned short int start;
58 unsigned short int end;
59};
60
61struct fl_flow_mask {
62 struct fl_flow_key key;
63 struct fl_flow_mask_range range;
05cd271f
PB
64 struct rhash_head ht_node;
65 struct rhashtable ht;
66 struct rhashtable_params filter_ht_params;
67 struct flow_dissector dissector;
68 struct list_head filters;
69 struct rcu_head rcu;
70 struct list_head list;
77b9900e
JP
71};
72
73struct cls_fl_head {
74 struct rhashtable ht;
05cd271f 75 struct list_head masks;
d9363774
DB
76 union {
77 struct work_struct work;
78 struct rcu_head rcu;
79 };
c15ab236 80 struct idr handle_idr;
77b9900e
JP
81};
82
83struct cls_fl_filter {
05cd271f 84 struct fl_flow_mask *mask;
77b9900e
JP
85 struct rhash_head ht_node;
86 struct fl_flow_key mkey;
87 struct tcf_exts exts;
88 struct tcf_result res;
89 struct fl_flow_key key;
90 struct list_head list;
91 u32 handle;
e69985c6 92 u32 flags;
0552c8af
CW
93 union {
94 struct work_struct work;
95 struct rcu_head rcu;
96 };
7091d8c7 97 struct net_device *hw_dev;
77b9900e
JP
98};
99
05cd271f
PB
100static const struct rhashtable_params mask_ht_params = {
101 .key_offset = offsetof(struct fl_flow_mask, key),
102 .key_len = sizeof(struct fl_flow_key),
103 .head_offset = offsetof(struct fl_flow_mask, ht_node),
104 .automatic_shrinking = true,
105};
106
77b9900e
JP
107static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
108{
109 return mask->range.end - mask->range.start;
110}
111
112static void fl_mask_update_range(struct fl_flow_mask *mask)
113{
114 const u8 *bytes = (const u8 *) &mask->key;
115 size_t size = sizeof(mask->key);
05cd271f 116 size_t i, first = 0, last;
77b9900e 117
05cd271f
PB
118 for (i = 0; i < size; i++) {
119 if (bytes[i]) {
120 first = i;
121 break;
122 }
123 }
124 last = first;
125 for (i = size - 1; i != first; i--) {
77b9900e 126 if (bytes[i]) {
77b9900e 127 last = i;
05cd271f 128 break;
77b9900e
JP
129 }
130 }
131 mask->range.start = rounddown(first, sizeof(long));
132 mask->range.end = roundup(last + 1, sizeof(long));
133}
134
135static void *fl_key_get_start(struct fl_flow_key *key,
136 const struct fl_flow_mask *mask)
137{
138 return (u8 *) key + mask->range.start;
139}
140
141static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
142 struct fl_flow_mask *mask)
143{
144 const long *lkey = fl_key_get_start(key, mask);
145 const long *lmask = fl_key_get_start(&mask->key, mask);
146 long *lmkey = fl_key_get_start(mkey, mask);
147 int i;
148
149 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
150 *lmkey++ = *lkey++ & *lmask++;
151}
152
153static void fl_clear_masked_range(struct fl_flow_key *key,
154 struct fl_flow_mask *mask)
155{
156 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
157}
158
05cd271f 159static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
a3308d8f
PB
160 struct fl_flow_key *mkey)
161{
05cd271f
PB
162 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
163 mask->filter_ht_params);
a3308d8f
PB
164}
165
77b9900e
JP
166static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
167 struct tcf_result *res)
168{
169 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
170 struct cls_fl_filter *f;
05cd271f 171 struct fl_flow_mask *mask;
77b9900e
JP
172 struct fl_flow_key skb_key;
173 struct fl_flow_key skb_mkey;
174
05cd271f
PB
175 list_for_each_entry_rcu(mask, &head->masks, list) {
176 fl_clear_masked_range(&skb_key, mask);
bc3103f1 177
05cd271f
PB
178 skb_key.indev_ifindex = skb->skb_iif;
179 /* skb_flow_dissect() does not set n_proto in case an unknown
180 * protocol, so do it rather here.
181 */
182 skb_key.basic.n_proto = skb->protocol;
183 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
184 skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
77b9900e 185
05cd271f 186 fl_set_masked_key(&skb_mkey, &skb_key, mask);
77b9900e 187
05cd271f
PB
188 f = fl_lookup(mask, &skb_mkey);
189 if (f && !tc_skip_sw(f->flags)) {
190 *res = f->res;
191 return tcf_exts_exec(skb, &f->exts, res);
192 }
77b9900e
JP
193 }
194 return -1;
195}
196
197static int fl_init(struct tcf_proto *tp)
198{
199 struct cls_fl_head *head;
200
201 head = kzalloc(sizeof(*head), GFP_KERNEL);
202 if (!head)
203 return -ENOBUFS;
204
05cd271f 205 INIT_LIST_HEAD_RCU(&head->masks);
77b9900e 206 rcu_assign_pointer(tp->root, head);
c15ab236 207 idr_init(&head->handle_idr);
77b9900e 208
05cd271f
PB
209 return rhashtable_init(&head->ht, &mask_ht_params);
210}
211
212static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask,
213 bool async)
214{
215 if (!list_empty(&mask->filters))
216 return false;
217
218 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
219 rhashtable_destroy(&mask->ht);
220 list_del_rcu(&mask->list);
221 if (async)
222 kfree_rcu(mask, rcu);
223 else
224 kfree(mask);
225
226 return true;
77b9900e
JP
227}
228
0dadc117
CW
229static void __fl_destroy_filter(struct cls_fl_filter *f)
230{
231 tcf_exts_destroy(&f->exts);
232 tcf_exts_put_net(&f->exts);
233 kfree(f);
234}
235
0552c8af 236static void fl_destroy_filter_work(struct work_struct *work)
77b9900e 237{
0552c8af 238 struct cls_fl_filter *f = container_of(work, struct cls_fl_filter, work);
77b9900e 239
0552c8af 240 rtnl_lock();
0dadc117 241 __fl_destroy_filter(f);
0552c8af
CW
242 rtnl_unlock();
243}
244
245static void fl_destroy_filter(struct rcu_head *head)
246{
247 struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);
248
249 INIT_WORK(&f->work, fl_destroy_filter_work);
250 tcf_queue_work(&f->work);
77b9900e
JP
251}
252
1b0f8037
JK
253static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
254 struct netlink_ext_ack *extack)
5b33f488 255{
de4784ca 256 struct tc_cls_flower_offload cls_flower = {};
208c0f4b 257 struct tcf_block *block = tp->chain->block;
5b33f488 258
1b0f8037 259 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
de4784ca
JP
260 cls_flower.command = TC_CLSFLOWER_DESTROY;
261 cls_flower.cookie = (unsigned long) f;
5b33f488 262
208c0f4b 263 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
717503b9 264 &cls_flower, false);
caa72601 265 tcf_block_offload_dec(block, &f->flags);
5b33f488
AV
266}
267
e8eb36cd 268static int fl_hw_replace_filter(struct tcf_proto *tp,
41002038
QM
269 struct cls_fl_filter *f,
270 struct netlink_ext_ack *extack)
5b33f488 271{
de4784ca 272 struct tc_cls_flower_offload cls_flower = {};
208c0f4b 273 struct tcf_block *block = tp->chain->block;
717503b9 274 bool skip_sw = tc_skip_sw(f->flags);
e8eb36cd 275 int err;
5b33f488 276
ea205940 277 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
de4784ca
JP
278 cls_flower.command = TC_CLSFLOWER_REPLACE;
279 cls_flower.cookie = (unsigned long) f;
05cd271f
PB
280 cls_flower.dissector = &f->mask->dissector;
281 cls_flower.mask = &f->mask->key;
de4784ca
JP
282 cls_flower.key = &f->mkey;
283 cls_flower.exts = &f->exts;
384c181e 284 cls_flower.classid = f->res.classid;
5b33f488 285
208c0f4b 286 err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
717503b9
JP
287 &cls_flower, skip_sw);
288 if (err < 0) {
1b0f8037 289 fl_hw_destroy_filter(tp, f, NULL);
e8eb36cd 290 return err;
717503b9 291 } else if (err > 0) {
caa72601 292 tcf_block_offload_inc(block, &f->flags);
717503b9
JP
293 }
294
295 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
296 return -EINVAL;
297
e8eb36cd 298 return 0;
5b33f488
AV
299}
300
10cbc684
AV
301static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
302{
de4784ca 303 struct tc_cls_flower_offload cls_flower = {};
208c0f4b 304 struct tcf_block *block = tp->chain->block;
10cbc684 305
ea205940 306 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
de4784ca
JP
307 cls_flower.command = TC_CLSFLOWER_STATS;
308 cls_flower.cookie = (unsigned long) f;
309 cls_flower.exts = &f->exts;
384c181e 310 cls_flower.classid = f->res.classid;
10cbc684 311
208c0f4b 312 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
717503b9 313 &cls_flower, false);
10cbc684
AV
314}
315
05cd271f 316static bool __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
1b0f8037 317 struct netlink_ext_ack *extack)
13fa876e 318{
c15ab236 319 struct cls_fl_head *head = rtnl_dereference(tp->root);
05cd271f
PB
320 bool async = tcf_exts_get_net(&f->exts);
321 bool last;
c15ab236 322
9c160941 323 idr_remove(&head->handle_idr, f->handle);
13fa876e 324 list_del_rcu(&f->list);
05cd271f 325 last = fl_mask_put(head, f->mask, async);
79685219 326 if (!tc_skip_hw(f->flags))
1b0f8037 327 fl_hw_destroy_filter(tp, f, extack);
13fa876e 328 tcf_unbind_filter(tp, &f->res);
05cd271f 329 if (async)
0dadc117
CW
330 call_rcu(&f->rcu, fl_destroy_filter);
331 else
332 __fl_destroy_filter(f);
05cd271f
PB
333
334 return last;
13fa876e
RD
335}
336
d9363774
DB
337static void fl_destroy_sleepable(struct work_struct *work)
338{
339 struct cls_fl_head *head = container_of(work, struct cls_fl_head,
340 work);
d9363774
DB
341 kfree(head);
342 module_put(THIS_MODULE);
343}
344
345static void fl_destroy_rcu(struct rcu_head *rcu)
346{
347 struct cls_fl_head *head = container_of(rcu, struct cls_fl_head, rcu);
348
349 INIT_WORK(&head->work, fl_destroy_sleepable);
350 schedule_work(&head->work);
351}
352
715df5ec 353static void fl_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
77b9900e
JP
354{
355 struct cls_fl_head *head = rtnl_dereference(tp->root);
05cd271f 356 struct fl_flow_mask *mask, *next_mask;
77b9900e
JP
357 struct cls_fl_filter *f, *next;
358
05cd271f
PB
359 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
360 list_for_each_entry_safe(f, next, &mask->filters, list) {
361 if (__fl_delete(tp, f, extack))
362 break;
363 }
364 }
c15ab236 365 idr_destroy(&head->handle_idr);
d9363774
DB
366
367 __module_get(THIS_MODULE);
368 call_rcu(&head->rcu, fl_destroy_rcu);
77b9900e
JP
369}
370
8113c095 371static void *fl_get(struct tcf_proto *tp, u32 handle)
77b9900e
JP
372{
373 struct cls_fl_head *head = rtnl_dereference(tp->root);
77b9900e 374
322d884b 375 return idr_find(&head->handle_idr, handle);
77b9900e
JP
376}
377
378static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
379 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
380 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
381 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
382 .len = IFNAMSIZ },
383 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
384 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
385 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
386 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
387 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
388 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
389 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
390 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
391 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
392 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
393 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
394 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
395 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
396 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
397 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
398 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
b175c3a4
JHS
399 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
400 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
9399ae9a
HHZ
401 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
402 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
403 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
bc3103f1
AV
404 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
405 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
406 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
407 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
408 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
409 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
410 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
411 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
412 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
aa72d708
OG
413 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
414 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
415 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
416 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
5976c5f4
SH
417 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
418 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
419 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
420 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
f4d997fd
HHZ
421 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
422 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
423 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
424 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
faa3ffce
OG
425 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
426 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
7b684884
SH
427 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
428 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
429 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
430 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
431 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
432 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
433 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
434 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
99d31326
SH
435 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
436 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
437 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
438 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
439 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
440 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
441 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
442 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
443 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
444 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
a577d8f7
BL
445 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
446 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
447 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
448 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
fdfc7dd6
JP
449 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
450 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
4d80cc0a
OG
451 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
452 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
453 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
454 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
77b9900e
JP
455};
456
457static void fl_set_key_val(struct nlattr **tb,
458 void *val, int val_type,
459 void *mask, int mask_type, int len)
460{
461 if (!tb[val_type])
462 return;
463 memcpy(val, nla_data(tb[val_type]), len);
464 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
465 memset(mask, 0xff, len);
466 else
467 memcpy(mask, nla_data(tb[mask_type]), len);
468}
469
1a7fca63
BL
470static int fl_set_key_mpls(struct nlattr **tb,
471 struct flow_dissector_key_mpls *key_val,
472 struct flow_dissector_key_mpls *key_mask)
a577d8f7
BL
473{
474 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
475 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
476 key_mask->mpls_ttl = MPLS_TTL_MASK;
477 }
478 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
1a7fca63
BL
479 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
480
481 if (bos & ~MPLS_BOS_MASK)
482 return -EINVAL;
483 key_val->mpls_bos = bos;
a577d8f7
BL
484 key_mask->mpls_bos = MPLS_BOS_MASK;
485 }
486 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
1a7fca63
BL
487 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
488
489 if (tc & ~MPLS_TC_MASK)
490 return -EINVAL;
491 key_val->mpls_tc = tc;
a577d8f7
BL
492 key_mask->mpls_tc = MPLS_TC_MASK;
493 }
494 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
1a7fca63
BL
495 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
496
497 if (label & ~MPLS_LABEL_MASK)
498 return -EINVAL;
499 key_val->mpls_label = label;
a577d8f7
BL
500 key_mask->mpls_label = MPLS_LABEL_MASK;
501 }
1a7fca63 502 return 0;
a577d8f7
BL
503}
504
9399ae9a
HHZ
505static void fl_set_key_vlan(struct nlattr **tb,
506 struct flow_dissector_key_vlan *key_val,
507 struct flow_dissector_key_vlan *key_mask)
508{
509#define VLAN_PRIORITY_MASK 0x7
510
511 if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
512 key_val->vlan_id =
513 nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
514 key_mask->vlan_id = VLAN_VID_MASK;
515 }
516 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
517 key_val->vlan_priority =
518 nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
519 VLAN_PRIORITY_MASK;
520 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
521 }
522}
523
faa3ffce
OG
524static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
525 u32 *dissector_key, u32 *dissector_mask,
526 u32 flower_flag_bit, u32 dissector_flag_bit)
527{
528 if (flower_mask & flower_flag_bit) {
529 *dissector_mask |= dissector_flag_bit;
530 if (flower_key & flower_flag_bit)
531 *dissector_key |= dissector_flag_bit;
532 }
533}
534
d9724772
OG
535static int fl_set_key_flags(struct nlattr **tb,
536 u32 *flags_key, u32 *flags_mask)
faa3ffce
OG
537{
538 u32 key, mask;
539
d9724772
OG
540 /* mask is mandatory for flags */
541 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
542 return -EINVAL;
faa3ffce
OG
543
544 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
d9724772 545 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
faa3ffce
OG
546
547 *flags_key = 0;
548 *flags_mask = 0;
549
550 fl_set_key_flag(key, mask, flags_key, flags_mask,
551 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
459d153d
PJV
552 fl_set_key_flag(key, mask, flags_key, flags_mask,
553 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
554 FLOW_DIS_FIRST_FRAG);
d9724772
OG
555
556 return 0;
faa3ffce
OG
557}
558
4d80cc0a
OG
559static void fl_set_key_ip(struct nlattr **tb,
560 struct flow_dissector_key_ip *key,
561 struct flow_dissector_key_ip *mask)
562{
563 fl_set_key_val(tb, &key->tos, TCA_FLOWER_KEY_IP_TOS,
564 &mask->tos, TCA_FLOWER_KEY_IP_TOS_MASK,
565 sizeof(key->tos));
566
567 fl_set_key_val(tb, &key->ttl, TCA_FLOWER_KEY_IP_TTL,
568 &mask->ttl, TCA_FLOWER_KEY_IP_TTL_MASK,
569 sizeof(key->ttl));
570}
571
77b9900e 572static int fl_set_key(struct net *net, struct nlattr **tb,
1057c55f
AA
573 struct fl_flow_key *key, struct fl_flow_key *mask,
574 struct netlink_ext_ack *extack)
77b9900e 575{
9399ae9a 576 __be16 ethertype;
d9724772 577 int ret = 0;
dd3aa3b5 578#ifdef CONFIG_NET_CLS_IND
77b9900e 579 if (tb[TCA_FLOWER_INDEV]) {
1057c55f 580 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
77b9900e
JP
581 if (err < 0)
582 return err;
583 key->indev_ifindex = err;
584 mask->indev_ifindex = 0xffffffff;
585 }
dd3aa3b5 586#endif
77b9900e
JP
587
588 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
589 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
590 sizeof(key->eth.dst));
591 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
592 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
593 sizeof(key->eth.src));
66530bdf 594
0b498a52 595 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
9399ae9a
HHZ
596 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
597
0b498a52
AB
598 if (ethertype == htons(ETH_P_8021Q)) {
599 fl_set_key_vlan(tb, &key->vlan, &mask->vlan);
600 fl_set_key_val(tb, &key->basic.n_proto,
601 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
602 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
603 sizeof(key->basic.n_proto));
604 } else {
605 key->basic.n_proto = ethertype;
606 mask->basic.n_proto = cpu_to_be16(~0);
607 }
9399ae9a 608 }
66530bdf 609
77b9900e
JP
610 if (key->basic.n_proto == htons(ETH_P_IP) ||
611 key->basic.n_proto == htons(ETH_P_IPV6)) {
612 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
613 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
614 sizeof(key->basic.ip_proto));
4d80cc0a 615 fl_set_key_ip(tb, &key->ip, &mask->ip);
77b9900e 616 }
66530bdf
JHS
617
618 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
619 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
970bfcd0 620 mask->control.addr_type = ~0;
77b9900e
JP
621 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
622 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
623 sizeof(key->ipv4.src));
624 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
625 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
626 sizeof(key->ipv4.dst));
66530bdf
JHS
627 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
628 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
970bfcd0 629 mask->control.addr_type = ~0;
77b9900e
JP
630 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
631 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
632 sizeof(key->ipv6.src));
633 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
634 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
635 sizeof(key->ipv6.dst));
636 }
66530bdf 637
77b9900e
JP
638 if (key->basic.ip_proto == IPPROTO_TCP) {
639 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
aa72d708 640 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
77b9900e
JP
641 sizeof(key->tp.src));
642 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
aa72d708 643 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
77b9900e 644 sizeof(key->tp.dst));
fdfc7dd6
JP
645 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
646 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
647 sizeof(key->tcp.flags));
77b9900e
JP
648 } else if (key->basic.ip_proto == IPPROTO_UDP) {
649 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
aa72d708 650 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
77b9900e
JP
651 sizeof(key->tp.src));
652 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
aa72d708 653 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
77b9900e 654 sizeof(key->tp.dst));
5976c5f4
SH
655 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
656 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
657 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
658 sizeof(key->tp.src));
659 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
660 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
661 sizeof(key->tp.dst));
7b684884
SH
662 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
663 key->basic.ip_proto == IPPROTO_ICMP) {
664 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
665 &mask->icmp.type,
666 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
667 sizeof(key->icmp.type));
668 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
669 &mask->icmp.code,
670 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
671 sizeof(key->icmp.code));
672 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
673 key->basic.ip_proto == IPPROTO_ICMPV6) {
674 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
675 &mask->icmp.type,
676 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
677 sizeof(key->icmp.type));
040587af 678 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
7b684884 679 &mask->icmp.code,
040587af 680 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
7b684884 681 sizeof(key->icmp.code));
a577d8f7
BL
682 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
683 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
1a7fca63
BL
684 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
685 if (ret)
686 return ret;
99d31326
SH
687 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
688 key->basic.n_proto == htons(ETH_P_RARP)) {
689 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
690 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
691 sizeof(key->arp.sip));
692 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
693 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
694 sizeof(key->arp.tip));
695 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
696 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
697 sizeof(key->arp.op));
698 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
699 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
700 sizeof(key->arp.sha));
701 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
702 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
703 sizeof(key->arp.tha));
77b9900e
JP
704 }
705
bc3103f1
AV
706 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
707 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
708 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
970bfcd0 709 mask->enc_control.addr_type = ~0;
bc3103f1
AV
710 fl_set_key_val(tb, &key->enc_ipv4.src,
711 TCA_FLOWER_KEY_ENC_IPV4_SRC,
712 &mask->enc_ipv4.src,
713 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
714 sizeof(key->enc_ipv4.src));
715 fl_set_key_val(tb, &key->enc_ipv4.dst,
716 TCA_FLOWER_KEY_ENC_IPV4_DST,
717 &mask->enc_ipv4.dst,
718 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
719 sizeof(key->enc_ipv4.dst));
720 }
721
722 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
723 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
724 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
970bfcd0 725 mask->enc_control.addr_type = ~0;
bc3103f1
AV
726 fl_set_key_val(tb, &key->enc_ipv6.src,
727 TCA_FLOWER_KEY_ENC_IPV6_SRC,
728 &mask->enc_ipv6.src,
729 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
730 sizeof(key->enc_ipv6.src));
731 fl_set_key_val(tb, &key->enc_ipv6.dst,
732 TCA_FLOWER_KEY_ENC_IPV6_DST,
733 &mask->enc_ipv6.dst,
734 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
735 sizeof(key->enc_ipv6.dst));
736 }
737
738 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
eb523f42 739 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
bc3103f1
AV
740 sizeof(key->enc_key_id.keyid));
741
f4d997fd
HHZ
742 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
743 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
744 sizeof(key->enc_tp.src));
745
746 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
747 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
748 sizeof(key->enc_tp.dst));
749
d9724772
OG
750 if (tb[TCA_FLOWER_KEY_FLAGS])
751 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
faa3ffce 752
d9724772 753 return ret;
77b9900e
JP
754}
755
05cd271f
PB
756static void fl_mask_copy(struct fl_flow_mask *dst,
757 struct fl_flow_mask *src)
77b9900e 758{
05cd271f
PB
759 const void *psrc = fl_key_get_start(&src->key, src);
760 void *pdst = fl_key_get_start(&dst->key, src);
77b9900e 761
05cd271f
PB
762 memcpy(pdst, psrc, fl_mask_range(src));
763 dst->range = src->range;
77b9900e
JP
764}
765
766static const struct rhashtable_params fl_ht_params = {
767 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
768 .head_offset = offsetof(struct cls_fl_filter, ht_node),
769 .automatic_shrinking = true,
770};
771
05cd271f 772static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
77b9900e 773{
05cd271f
PB
774 mask->filter_ht_params = fl_ht_params;
775 mask->filter_ht_params.key_len = fl_mask_range(mask);
776 mask->filter_ht_params.key_offset += mask->range.start;
77b9900e 777
05cd271f 778 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
77b9900e
JP
779}
780
781#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
782#define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
77b9900e 783
339ba878
HHZ
784#define FL_KEY_IS_MASKED(mask, member) \
785 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
786 0, FL_KEY_MEMBER_SIZE(member)) \
77b9900e
JP
787
788#define FL_KEY_SET(keys, cnt, id, member) \
789 do { \
790 keys[cnt].key_id = id; \
791 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
792 cnt++; \
793 } while(0);
794
339ba878 795#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
77b9900e 796 do { \
339ba878 797 if (FL_KEY_IS_MASKED(mask, member)) \
77b9900e
JP
798 FL_KEY_SET(keys, cnt, id, member); \
799 } while(0);
800
05cd271f 801static void fl_init_dissector(struct fl_flow_mask *mask)
77b9900e
JP
802{
803 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
804 size_t cnt = 0;
805
42aecaa9 806 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
77b9900e 807 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
339ba878
HHZ
808 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
809 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
810 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
811 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
812 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
813 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
814 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
815 FLOW_DISSECTOR_KEY_PORTS, tp);
4d80cc0a
OG
816 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
817 FLOW_DISSECTOR_KEY_IP, ip);
fdfc7dd6
JP
818 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
819 FLOW_DISSECTOR_KEY_TCP, tcp);
7b684884
SH
820 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
821 FLOW_DISSECTOR_KEY_ICMP, icmp);
99d31326
SH
822 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
823 FLOW_DISSECTOR_KEY_ARP, arp);
a577d8f7
BL
824 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
825 FLOW_DISSECTOR_KEY_MPLS, mpls);
9399ae9a
HHZ
826 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
827 FLOW_DISSECTOR_KEY_VLAN, vlan);
519d1052
HHZ
828 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
829 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
830 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
831 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
832 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
833 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
834 if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
835 FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
836 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
837 enc_control);
f4d997fd
HHZ
838 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
839 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
77b9900e 840
05cd271f
PB
841 skb_flow_dissector_init(&mask->dissector, keys, cnt);
842}
843
844static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
845 struct fl_flow_mask *mask)
846{
847 struct fl_flow_mask *newmask;
848 int err;
849
850 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
851 if (!newmask)
852 return ERR_PTR(-ENOMEM);
853
854 fl_mask_copy(newmask, mask);
855
856 err = fl_init_mask_hashtable(newmask);
857 if (err)
858 goto errout_free;
859
860 fl_init_dissector(newmask);
861
862 INIT_LIST_HEAD_RCU(&newmask->filters);
863
864 err = rhashtable_insert_fast(&head->ht, &newmask->ht_node,
865 mask_ht_params);
866 if (err)
867 goto errout_destroy;
868
869 list_add_tail_rcu(&newmask->list, &head->masks);
870
871 return newmask;
872
873errout_destroy:
874 rhashtable_destroy(&newmask->ht);
875errout_free:
876 kfree(newmask);
877
878 return ERR_PTR(err);
77b9900e
JP
879}
880
881static int fl_check_assign_mask(struct cls_fl_head *head,
05cd271f
PB
882 struct cls_fl_filter *fnew,
883 struct cls_fl_filter *fold,
77b9900e
JP
884 struct fl_flow_mask *mask)
885{
05cd271f 886 struct fl_flow_mask *newmask;
77b9900e 887
05cd271f
PB
888 fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params);
889 if (!fnew->mask) {
890 if (fold)
77b9900e 891 return -EINVAL;
77b9900e 892
05cd271f
PB
893 newmask = fl_create_new_mask(head, mask);
894 if (IS_ERR(newmask))
895 return PTR_ERR(newmask);
77b9900e 896
05cd271f
PB
897 fnew->mask = newmask;
898 } else if (fold && fold->mask == fnew->mask) {
899 return -EINVAL;
900 }
77b9900e
JP
901
902 return 0;
903}
904
905static int fl_set_parms(struct net *net, struct tcf_proto *tp,
906 struct cls_fl_filter *f, struct fl_flow_mask *mask,
907 unsigned long base, struct nlattr **tb,
50a56190
AA
908 struct nlattr *est, bool ovr,
909 struct netlink_ext_ack *extack)
77b9900e 910{
77b9900e
JP
911 int err;
912
50a56190 913 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
77b9900e
JP
914 if (err < 0)
915 return err;
916
917 if (tb[TCA_FLOWER_CLASSID]) {
918 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
919 tcf_bind_filter(tp, &f->res, base);
920 }
921
1057c55f 922 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
77b9900e 923 if (err)
45507529 924 return err;
77b9900e
JP
925
926 fl_mask_update_range(mask);
927 fl_set_masked_key(&f->mkey, &f->key, mask);
928
77b9900e 929 return 0;
77b9900e
JP
930}
931
77b9900e
JP
932static int fl_change(struct net *net, struct sk_buff *in_skb,
933 struct tcf_proto *tp, unsigned long base,
934 u32 handle, struct nlattr **tca,
7306db38 935 void **arg, bool ovr, struct netlink_ext_ack *extack)
77b9900e
JP
936{
937 struct cls_fl_head *head = rtnl_dereference(tp->root);
8113c095 938 struct cls_fl_filter *fold = *arg;
77b9900e 939 struct cls_fl_filter *fnew;
39b7b6a6 940 struct nlattr **tb;
77b9900e
JP
941 struct fl_flow_mask mask = {};
942 int err;
943
944 if (!tca[TCA_OPTIONS])
945 return -EINVAL;
946
39b7b6a6
AB
947 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
948 if (!tb)
949 return -ENOBUFS;
950
fceb6435
JB
951 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
952 fl_policy, NULL);
77b9900e 953 if (err < 0)
39b7b6a6 954 goto errout_tb;
77b9900e 955
39b7b6a6
AB
956 if (fold && handle && fold->handle != handle) {
957 err = -EINVAL;
958 goto errout_tb;
959 }
77b9900e
JP
960
961 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
39b7b6a6
AB
962 if (!fnew) {
963 err = -ENOBUFS;
964 goto errout_tb;
965 }
77b9900e 966
b9a24bb7
WC
967 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
968 if (err < 0)
969 goto errout;
77b9900e
JP
970
971 if (!handle) {
85bd0438
MW
972 handle = 1;
973 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
974 INT_MAX, GFP_KERNEL);
975 } else if (!fold) {
976 /* user specifies a handle and it doesn't exist */
977 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
978 handle, GFP_KERNEL);
77b9900e 979 }
85bd0438
MW
980 if (err)
981 goto errout;
982 fnew->handle = handle;
77b9900e 983
e69985c6
AV
984 if (tb[TCA_FLOWER_FLAGS]) {
985 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
986
987 if (!tc_flags_valid(fnew->flags)) {
988 err = -EINVAL;
fe2502e4 989 goto errout_idr;
e69985c6
AV
990 }
991 }
5b33f488 992
50a56190
AA
993 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr,
994 extack);
77b9900e 995 if (err)
fe2502e4 996 goto errout_idr;
77b9900e 997
05cd271f 998 err = fl_check_assign_mask(head, fnew, fold, &mask);
77b9900e 999 if (err)
fe2502e4 1000 goto errout_idr;
77b9900e 1001
e8eb36cd 1002 if (!tc_skip_sw(fnew->flags)) {
05cd271f 1003 if (!fold && fl_lookup(fnew->mask, &fnew->mkey)) {
a3308d8f 1004 err = -EEXIST;
05cd271f 1005 goto errout_mask;
a3308d8f
PB
1006 }
1007
05cd271f
PB
1008 err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node,
1009 fnew->mask->filter_ht_params);
e69985c6 1010 if (err)
05cd271f 1011 goto errout_mask;
e69985c6 1012 }
5b33f488 1013
79685219 1014 if (!tc_skip_hw(fnew->flags)) {
05cd271f 1015 err = fl_hw_replace_filter(tp, fnew, extack);
79685219 1016 if (err)
05cd271f 1017 goto errout_mask;
79685219 1018 }
5b33f488 1019
55593960
OG
1020 if (!tc_in_hw(fnew->flags))
1021 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1022
5b33f488 1023 if (fold) {
725cbb62 1024 if (!tc_skip_sw(fold->flags))
05cd271f
PB
1025 rhashtable_remove_fast(&fold->mask->ht,
1026 &fold->ht_node,
1027 fold->mask->filter_ht_params);
79685219 1028 if (!tc_skip_hw(fold->flags))
1b0f8037 1029 fl_hw_destroy_filter(tp, fold, NULL);
5b33f488 1030 }
77b9900e 1031
8113c095 1032 *arg = fnew;
77b9900e
JP
1033
1034 if (fold) {
234a4624 1035 idr_replace(&head->handle_idr, fnew, fnew->handle);
ff3532f2 1036 list_replace_rcu(&fold->list, &fnew->list);
77b9900e 1037 tcf_unbind_filter(tp, &fold->res);
0dadc117 1038 tcf_exts_get_net(&fold->exts);
77b9900e
JP
1039 call_rcu(&fold->rcu, fl_destroy_filter);
1040 } else {
05cd271f 1041 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
77b9900e
JP
1042 }
1043
39b7b6a6 1044 kfree(tb);
77b9900e
JP
1045 return 0;
1046
05cd271f
PB
1047errout_mask:
1048 fl_mask_put(head, fnew->mask, false);
1049
fe2502e4
CW
1050errout_idr:
1051 if (fnew->handle)
9c160941 1052 idr_remove(&head->handle_idr, fnew->handle);
77b9900e 1053errout:
b9a24bb7 1054 tcf_exts_destroy(&fnew->exts);
77b9900e 1055 kfree(fnew);
39b7b6a6
AB
1056errout_tb:
1057 kfree(tb);
77b9900e
JP
1058 return err;
1059}
1060
571acf21
AA
1061static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
1062 struct netlink_ext_ack *extack)
77b9900e
JP
1063{
1064 struct cls_fl_head *head = rtnl_dereference(tp->root);
8113c095 1065 struct cls_fl_filter *f = arg;
77b9900e 1066
725cbb62 1067 if (!tc_skip_sw(f->flags))
05cd271f
PB
1068 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
1069 f->mask->filter_ht_params);
1b0f8037 1070 __fl_delete(tp, f, extack);
05cd271f 1071 *last = list_empty(&head->masks);
77b9900e
JP
1072 return 0;
1073}
1074
1075static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1076{
1077 struct cls_fl_head *head = rtnl_dereference(tp->root);
1078 struct cls_fl_filter *f;
05cd271f
PB
1079 struct fl_flow_mask *mask;
1080
1081 list_for_each_entry_rcu(mask, &head->masks, list) {
1082 list_for_each_entry_rcu(f, &mask->filters, list) {
1083 if (arg->count < arg->skip)
1084 goto skip;
1085 if (arg->fn(tp, f, arg) < 0) {
1086 arg->stop = 1;
1087 break;
1088 }
77b9900e 1089skip:
05cd271f
PB
1090 arg->count++;
1091 }
77b9900e
JP
1092 }
1093}
1094
1095static int fl_dump_key_val(struct sk_buff *skb,
1096 void *val, int val_type,
1097 void *mask, int mask_type, int len)
1098{
1099 int err;
1100
1101 if (!memchr_inv(mask, 0, len))
1102 return 0;
1103 err = nla_put(skb, val_type, len, val);
1104 if (err)
1105 return err;
1106 if (mask_type != TCA_FLOWER_UNSPEC) {
1107 err = nla_put(skb, mask_type, len, mask);
1108 if (err)
1109 return err;
1110 }
1111 return 0;
1112}
1113
a577d8f7
BL
1114static int fl_dump_key_mpls(struct sk_buff *skb,
1115 struct flow_dissector_key_mpls *mpls_key,
1116 struct flow_dissector_key_mpls *mpls_mask)
1117{
1118 int err;
1119
1120 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1121 return 0;
1122 if (mpls_mask->mpls_ttl) {
1123 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1124 mpls_key->mpls_ttl);
1125 if (err)
1126 return err;
1127 }
1128 if (mpls_mask->mpls_tc) {
1129 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1130 mpls_key->mpls_tc);
1131 if (err)
1132 return err;
1133 }
1134 if (mpls_mask->mpls_label) {
1135 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1136 mpls_key->mpls_label);
1137 if (err)
1138 return err;
1139 }
1140 if (mpls_mask->mpls_bos) {
1141 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1142 mpls_key->mpls_bos);
1143 if (err)
1144 return err;
1145 }
1146 return 0;
1147}
1148
4d80cc0a
OG
1149static int fl_dump_key_ip(struct sk_buff *skb,
1150 struct flow_dissector_key_ip *key,
1151 struct flow_dissector_key_ip *mask)
1152{
1153 if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos,
1154 TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) ||
1155 fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl,
1156 TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl)))
1157 return -1;
1158
1159 return 0;
1160}
1161
9399ae9a
HHZ
1162static int fl_dump_key_vlan(struct sk_buff *skb,
1163 struct flow_dissector_key_vlan *vlan_key,
1164 struct flow_dissector_key_vlan *vlan_mask)
1165{
1166 int err;
1167
1168 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1169 return 0;
1170 if (vlan_mask->vlan_id) {
1171 err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID,
1172 vlan_key->vlan_id);
1173 if (err)
1174 return err;
1175 }
1176 if (vlan_mask->vlan_priority) {
1177 err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO,
1178 vlan_key->vlan_priority);
1179 if (err)
1180 return err;
1181 }
1182 return 0;
1183}
1184
faa3ffce
OG
1185static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1186 u32 *flower_key, u32 *flower_mask,
1187 u32 flower_flag_bit, u32 dissector_flag_bit)
1188{
1189 if (dissector_mask & dissector_flag_bit) {
1190 *flower_mask |= flower_flag_bit;
1191 if (dissector_key & dissector_flag_bit)
1192 *flower_key |= flower_flag_bit;
1193 }
1194}
1195
1196static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1197{
1198 u32 key, mask;
1199 __be32 _key, _mask;
1200 int err;
1201
1202 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1203 return 0;
1204
1205 key = 0;
1206 mask = 0;
1207
1208 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1209 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
459d153d
PJV
1210 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1211 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1212 FLOW_DIS_FIRST_FRAG);
faa3ffce
OG
1213
1214 _key = cpu_to_be32(key);
1215 _mask = cpu_to_be32(mask);
1216
1217 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1218 if (err)
1219 return err;
1220
1221 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1222}
1223
8113c095 1224static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
77b9900e
JP
1225 struct sk_buff *skb, struct tcmsg *t)
1226{
8113c095 1227 struct cls_fl_filter *f = fh;
77b9900e
JP
1228 struct nlattr *nest;
1229 struct fl_flow_key *key, *mask;
1230
1231 if (!f)
1232 return skb->len;
1233
1234 t->tcm_handle = f->handle;
1235
1236 nest = nla_nest_start(skb, TCA_OPTIONS);
1237 if (!nest)
1238 goto nla_put_failure;
1239
1240 if (f->res.classid &&
1241 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1242 goto nla_put_failure;
1243
1244 key = &f->key;
05cd271f 1245 mask = &f->mask->key;
77b9900e
JP
1246
1247 if (mask->indev_ifindex) {
1248 struct net_device *dev;
1249
1250 dev = __dev_get_by_index(net, key->indev_ifindex);
1251 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1252 goto nla_put_failure;
1253 }
1254
79685219
HHZ
1255 if (!tc_skip_hw(f->flags))
1256 fl_hw_update_stats(tp, f);
10cbc684 1257
77b9900e
JP
1258 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1259 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1260 sizeof(key->eth.dst)) ||
1261 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1262 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1263 sizeof(key->eth.src)) ||
1264 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1265 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1266 sizeof(key->basic.n_proto)))
1267 goto nla_put_failure;
9399ae9a 1268
a577d8f7
BL
1269 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1270 goto nla_put_failure;
1271
9399ae9a
HHZ
1272 if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan))
1273 goto nla_put_failure;
1274
77b9900e
JP
1275 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1276 key->basic.n_proto == htons(ETH_P_IPV6)) &&
4d80cc0a 1277 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
77b9900e 1278 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
4d80cc0a
OG
1279 sizeof(key->basic.ip_proto)) ||
1280 fl_dump_key_ip(skb, &key->ip, &mask->ip)))
77b9900e
JP
1281 goto nla_put_failure;
1282
c3f83241 1283 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
77b9900e
JP
1284 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1285 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1286 sizeof(key->ipv4.src)) ||
1287 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1288 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1289 sizeof(key->ipv4.dst))))
1290 goto nla_put_failure;
c3f83241 1291 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
77b9900e
JP
1292 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1293 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1294 sizeof(key->ipv6.src)) ||
1295 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1296 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1297 sizeof(key->ipv6.dst))))
1298 goto nla_put_failure;
1299
1300 if (key->basic.ip_proto == IPPROTO_TCP &&
1301 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
aa72d708 1302 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
77b9900e
JP
1303 sizeof(key->tp.src)) ||
1304 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
aa72d708 1305 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
fdfc7dd6
JP
1306 sizeof(key->tp.dst)) ||
1307 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1308 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1309 sizeof(key->tcp.flags))))
77b9900e
JP
1310 goto nla_put_failure;
1311 else if (key->basic.ip_proto == IPPROTO_UDP &&
1312 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
aa72d708 1313 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
77b9900e
JP
1314 sizeof(key->tp.src)) ||
1315 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
aa72d708 1316 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
5976c5f4
SH
1317 sizeof(key->tp.dst))))
1318 goto nla_put_failure;
1319 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1320 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1321 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1322 sizeof(key->tp.src)) ||
1323 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1324 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
77b9900e
JP
1325 sizeof(key->tp.dst))))
1326 goto nla_put_failure;
7b684884
SH
1327 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1328 key->basic.ip_proto == IPPROTO_ICMP &&
1329 (fl_dump_key_val(skb, &key->icmp.type,
1330 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1331 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1332 sizeof(key->icmp.type)) ||
1333 fl_dump_key_val(skb, &key->icmp.code,
1334 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1335 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1336 sizeof(key->icmp.code))))
1337 goto nla_put_failure;
1338 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1339 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1340 (fl_dump_key_val(skb, &key->icmp.type,
1341 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1342 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1343 sizeof(key->icmp.type)) ||
1344 fl_dump_key_val(skb, &key->icmp.code,
1345 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1346 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1347 sizeof(key->icmp.code))))
1348 goto nla_put_failure;
99d31326
SH
1349 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1350 key->basic.n_proto == htons(ETH_P_RARP)) &&
1351 (fl_dump_key_val(skb, &key->arp.sip,
1352 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1353 TCA_FLOWER_KEY_ARP_SIP_MASK,
1354 sizeof(key->arp.sip)) ||
1355 fl_dump_key_val(skb, &key->arp.tip,
1356 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1357 TCA_FLOWER_KEY_ARP_TIP_MASK,
1358 sizeof(key->arp.tip)) ||
1359 fl_dump_key_val(skb, &key->arp.op,
1360 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1361 TCA_FLOWER_KEY_ARP_OP_MASK,
1362 sizeof(key->arp.op)) ||
1363 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1364 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1365 sizeof(key->arp.sha)) ||
1366 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1367 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1368 sizeof(key->arp.tha))))
1369 goto nla_put_failure;
77b9900e 1370
bc3103f1
AV
1371 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1372 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1373 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1374 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1375 sizeof(key->enc_ipv4.src)) ||
1376 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1377 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1378 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1379 sizeof(key->enc_ipv4.dst))))
1380 goto nla_put_failure;
1381 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1382 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1383 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1384 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1385 sizeof(key->enc_ipv6.src)) ||
1386 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1387 TCA_FLOWER_KEY_ENC_IPV6_DST,
1388 &mask->enc_ipv6.dst,
1389 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1390 sizeof(key->enc_ipv6.dst))))
1391 goto nla_put_failure;
1392
1393 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
eb523f42 1394 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
f4d997fd
HHZ
1395 sizeof(key->enc_key_id)) ||
1396 fl_dump_key_val(skb, &key->enc_tp.src,
1397 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1398 &mask->enc_tp.src,
1399 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1400 sizeof(key->enc_tp.src)) ||
1401 fl_dump_key_val(skb, &key->enc_tp.dst,
1402 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1403 &mask->enc_tp.dst,
1404 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1405 sizeof(key->enc_tp.dst)))
bc3103f1
AV
1406 goto nla_put_failure;
1407
faa3ffce
OG
1408 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1409 goto nla_put_failure;
1410
749e6720
OG
1411 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1412 goto nla_put_failure;
e69985c6 1413
77b9900e
JP
1414 if (tcf_exts_dump(skb, &f->exts))
1415 goto nla_put_failure;
1416
1417 nla_nest_end(skb, nest);
1418
1419 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1420 goto nla_put_failure;
1421
1422 return skb->len;
1423
1424nla_put_failure:
1425 nla_nest_cancel(skb, nest);
1426 return -1;
1427}
1428
07d79fc7
CW
1429static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
1430{
1431 struct cls_fl_filter *f = fh;
1432
1433 if (f && f->res.classid == classid)
1434 f->res.class = cl;
1435}
1436
77b9900e
JP
1437static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1438 .kind = "flower",
1439 .classify = fl_classify,
1440 .init = fl_init,
1441 .destroy = fl_destroy,
1442 .get = fl_get,
1443 .change = fl_change,
1444 .delete = fl_delete,
1445 .walk = fl_walk,
1446 .dump = fl_dump,
07d79fc7 1447 .bind_class = fl_bind_class,
77b9900e
JP
1448 .owner = THIS_MODULE,
1449};
1450
1451static int __init cls_fl_init(void)
1452{
1453 return register_tcf_proto_ops(&cls_fl_ops);
1454}
1455
1456static void __exit cls_fl_exit(void)
1457{
1458 unregister_tcf_proto_ops(&cls_fl_ops);
1459}
1460
1461module_init(cls_fl_init);
1462module_exit(cls_fl_exit);
1463
1464MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1465MODULE_DESCRIPTION("Flower classifier");
1466MODULE_LICENSE("GPL v2");