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