blob: 543a3e875d05b17814d8cffa3276d0809b2da1b2 [file] [log] [blame]
Jiri Pirko77b99002015-05-12 14:56:21 +02001/*
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>
Daniel Borkmannd9363772016-11-27 01:18:01 +010016#include <linux/workqueue.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020017
18#include <linux/if_ether.h>
19#include <linux/in6.h>
20#include <linux/ip.h>
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -040021#include <linux/mpls.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020022
23#include <net/sch_generic.h>
24#include <net/pkt_cls.h>
25#include <net/ip.h>
26#include <net/flow_dissector.h>
27
Amir Vadaibc3103f2016-09-08 16:23:47 +030028#include <net/dst.h>
29#include <net/dst_metadata.h>
30
Jiri Pirko77b99002015-05-12 14:56:21 +020031struct fl_flow_key {
32 int indev_ifindex;
Tom Herbert42aecaa2015-06-04 09:16:39 -070033 struct flow_dissector_key_control control;
Amir Vadaibc3103f2016-09-08 16:23:47 +030034 struct flow_dissector_key_control enc_control;
Jiri Pirko77b99002015-05-12 14:56:21 +020035 struct flow_dissector_key_basic basic;
36 struct flow_dissector_key_eth_addrs eth;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +030037 struct flow_dissector_key_vlan vlan;
Jiri Pirko77b99002015-05-12 14:56:21 +020038 union {
Tom Herbertc3f83242015-06-04 09:16:40 -070039 struct flow_dissector_key_ipv4_addrs ipv4;
Jiri Pirko77b99002015-05-12 14:56:21 +020040 struct flow_dissector_key_ipv6_addrs ipv6;
41 };
42 struct flow_dissector_key_ports tp;
Simon Horman7b684882016-12-07 13:48:28 +010043 struct flow_dissector_key_icmp icmp;
Simon Horman99d31322017-01-11 14:05:43 +010044 struct flow_dissector_key_arp arp;
Amir Vadaibc3103f2016-09-08 16:23:47 +030045 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 };
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +020050 struct flow_dissector_key_ports enc_tp;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -040051 struct flow_dissector_key_mpls mpls;
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +020052 struct flow_dissector_key_tcp tcp;
Or Gerlitz4d80cc02017-06-01 21:37:38 +030053 struct flow_dissector_key_ip ip;
Jiri Pirko77b99002015-05-12 14:56:21 +020054} __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;
64 struct rcu_head rcu;
65};
66
67struct cls_fl_head {
68 struct rhashtable ht;
69 struct fl_flow_mask mask;
70 struct flow_dissector dissector;
Jiri Pirko77b99002015-05-12 14:56:21 +020071 bool mask_assigned;
72 struct list_head filters;
73 struct rhashtable_params ht_params;
Daniel Borkmannd9363772016-11-27 01:18:01 +010074 union {
75 struct work_struct work;
76 struct rcu_head rcu;
77 };
Chris Mic15ab232017-08-30 02:31:58 -040078 struct idr handle_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +020079};
80
81struct cls_fl_filter {
82 struct rhash_head ht_node;
83 struct fl_flow_key mkey;
84 struct tcf_exts exts;
85 struct tcf_result res;
86 struct fl_flow_key key;
87 struct list_head list;
88 u32 handle;
Amir Vadaie69985c2016-06-05 17:11:18 +030089 u32 flags;
Cong Wang0552c8a2017-10-26 18:24:33 -070090 union {
91 struct work_struct work;
92 struct rcu_head rcu;
93 };
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +020094 struct net_device *hw_dev;
Jiri Pirko77b99002015-05-12 14:56:21 +020095};
96
97static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
98{
99 return mask->range.end - mask->range.start;
100}
101
102static void fl_mask_update_range(struct fl_flow_mask *mask)
103{
104 const u8 *bytes = (const u8 *) &mask->key;
105 size_t size = sizeof(mask->key);
106 size_t i, first = 0, last = size - 1;
107
108 for (i = 0; i < sizeof(mask->key); i++) {
109 if (bytes[i]) {
110 if (!first && i)
111 first = i;
112 last = i;
113 }
114 }
115 mask->range.start = rounddown(first, sizeof(long));
116 mask->range.end = roundup(last + 1, sizeof(long));
117}
118
119static void *fl_key_get_start(struct fl_flow_key *key,
120 const struct fl_flow_mask *mask)
121{
122 return (u8 *) key + mask->range.start;
123}
124
125static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
126 struct fl_flow_mask *mask)
127{
128 const long *lkey = fl_key_get_start(key, mask);
129 const long *lmask = fl_key_get_start(&mask->key, mask);
130 long *lmkey = fl_key_get_start(mkey, mask);
131 int i;
132
133 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
134 *lmkey++ = *lkey++ & *lmask++;
135}
136
137static void fl_clear_masked_range(struct fl_flow_key *key,
138 struct fl_flow_mask *mask)
139{
140 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
141}
142
Paul Blakeya3308d82017-01-16 10:45:13 +0200143static struct cls_fl_filter *fl_lookup(struct cls_fl_head *head,
144 struct fl_flow_key *mkey)
145{
146 return rhashtable_lookup_fast(&head->ht,
147 fl_key_get_start(mkey, &head->mask),
148 head->ht_params);
149}
150
Jiri Pirko77b99002015-05-12 14:56:21 +0200151static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
152 struct tcf_result *res)
153{
154 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
155 struct cls_fl_filter *f;
156 struct fl_flow_key skb_key;
157 struct fl_flow_key skb_mkey;
158
Amir Vadaie69985c2016-06-05 17:11:18 +0300159 if (!atomic_read(&head->ht.nelems))
160 return -1;
161
Jiri Pirko77b99002015-05-12 14:56:21 +0200162 fl_clear_masked_range(&skb_key, &head->mask);
Amir Vadaibc3103f2016-09-08 16:23:47 +0300163
Jiri Pirko77b99002015-05-12 14:56:21 +0200164 skb_key.indev_ifindex = skb->skb_iif;
165 /* skb_flow_dissect() does not set n_proto in case an unknown protocol,
166 * so do it rather here.
167 */
168 skb_key.basic.n_proto = skb->protocol;
Tom Herbertcd79a232015-09-01 09:24:27 -0700169 skb_flow_dissect(skb, &head->dissector, &skb_key, 0);
Jiri Pirko77b99002015-05-12 14:56:21 +0200170
171 fl_set_masked_key(&skb_mkey, &skb_key, &head->mask);
172
Paul Blakeya3308d82017-01-16 10:45:13 +0200173 f = fl_lookup(head, &skb_mkey);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300174 if (f && !tc_skip_sw(f->flags)) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200175 *res = f->res;
176 return tcf_exts_exec(skb, &f->exts, res);
177 }
178 return -1;
179}
180
181static int fl_init(struct tcf_proto *tp)
182{
183 struct cls_fl_head *head;
184
185 head = kzalloc(sizeof(*head), GFP_KERNEL);
186 if (!head)
187 return -ENOBUFS;
188
189 INIT_LIST_HEAD_RCU(&head->filters);
190 rcu_assign_pointer(tp->root, head);
Chris Mic15ab232017-08-30 02:31:58 -0400191 idr_init(&head->handle_idr);
Jiri Pirko77b99002015-05-12 14:56:21 +0200192
193 return 0;
194}
195
Cong Wang0dadc112017-11-06 13:47:24 -0800196static void __fl_destroy_filter(struct cls_fl_filter *f)
197{
198 tcf_exts_destroy(&f->exts);
199 tcf_exts_put_net(&f->exts);
200 kfree(f);
201}
202
Cong Wang0552c8a2017-10-26 18:24:33 -0700203static void fl_destroy_filter_work(struct work_struct *work)
204{
205 struct cls_fl_filter *f = container_of(work, struct cls_fl_filter, work);
206
207 rtnl_lock();
Cong Wang0dadc112017-11-06 13:47:24 -0800208 __fl_destroy_filter(f);
Cong Wang0552c8a2017-10-26 18:24:33 -0700209 rtnl_unlock();
210}
211
Jiri Pirko77b99002015-05-12 14:56:21 +0200212static void fl_destroy_filter(struct rcu_head *head)
213{
214 struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);
215
Cong Wang0552c8a2017-10-26 18:24:33 -0700216 INIT_WORK(&f->work, fl_destroy_filter_work);
217 tcf_queue_work(&f->work);
Jiri Pirko77b99002015-05-12 14:56:21 +0200218}
219
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200220static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f)
Amir Vadai5b33f482016-03-08 12:42:29 +0200221{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200222 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200223 struct tcf_block *block = tp->chain->block;
Amir Vadai5b33f482016-03-08 12:42:29 +0200224
Jiri Pirkode4784c2017-08-07 10:15:32 +0200225 tc_cls_common_offload_init(&cls_flower.common, tp);
226 cls_flower.command = TC_CLSFLOWER_DESTROY;
227 cls_flower.cookie = (unsigned long) f;
Amir Vadai5b33f482016-03-08 12:42:29 +0200228
Jiri Pirko208c0f42017-10-19 15:50:32 +0200229 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200230 &cls_flower, false);
Amir Vadai5b33f482016-03-08 12:42:29 +0200231}
232
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300233static int fl_hw_replace_filter(struct tcf_proto *tp,
234 struct flow_dissector *dissector,
235 struct fl_flow_key *mask,
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200236 struct cls_fl_filter *f)
Amir Vadai5b33f482016-03-08 12:42:29 +0200237{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200238 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200239 struct tcf_block *block = tp->chain->block;
Jiri Pirko717503b2017-10-11 09:41:09 +0200240 bool skip_sw = tc_skip_sw(f->flags);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300241 int err;
Amir Vadai5b33f482016-03-08 12:42:29 +0200242
Jiri Pirkode4784c2017-08-07 10:15:32 +0200243 tc_cls_common_offload_init(&cls_flower.common, tp);
244 cls_flower.command = TC_CLSFLOWER_REPLACE;
245 cls_flower.cookie = (unsigned long) f;
246 cls_flower.dissector = dissector;
247 cls_flower.mask = mask;
248 cls_flower.key = &f->mkey;
249 cls_flower.exts = &f->exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700250 cls_flower.classid = f->res.classid;
Amir Vadai5b33f482016-03-08 12:42:29 +0200251
Jiri Pirko208c0f42017-10-19 15:50:32 +0200252 err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200253 &cls_flower, skip_sw);
254 if (err < 0) {
255 fl_hw_destroy_filter(tp, f);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300256 return err;
Jiri Pirko717503b2017-10-11 09:41:09 +0200257 } else if (err > 0) {
258 f->flags |= TCA_CLS_FLAGS_IN_HW;
259 }
260
261 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
262 return -EINVAL;
263
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300264 return 0;
Amir Vadai5b33f482016-03-08 12:42:29 +0200265}
266
Amir Vadai10cbc682016-05-13 12:55:37 +0000267static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
268{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200269 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200270 struct tcf_block *block = tp->chain->block;
Amir Vadai10cbc682016-05-13 12:55:37 +0000271
Jiri Pirkode4784c2017-08-07 10:15:32 +0200272 tc_cls_common_offload_init(&cls_flower.common, tp);
273 cls_flower.command = TC_CLSFLOWER_STATS;
274 cls_flower.cookie = (unsigned long) f;
275 cls_flower.exts = &f->exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700276 cls_flower.classid = f->res.classid;
Amir Vadai10cbc682016-05-13 12:55:37 +0000277
Jiri Pirko208c0f42017-10-19 15:50:32 +0200278 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200279 &cls_flower, false);
Amir Vadai10cbc682016-05-13 12:55:37 +0000280}
281
Roi Dayan13fa8762016-11-01 16:08:29 +0200282static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f)
283{
Chris Mic15ab232017-08-30 02:31:58 -0400284 struct cls_fl_head *head = rtnl_dereference(tp->root);
285
286 idr_remove_ext(&head->handle_idr, f->handle);
Roi Dayan13fa8762016-11-01 16:08:29 +0200287 list_del_rcu(&f->list);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200288 if (!tc_skip_hw(f->flags))
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200289 fl_hw_destroy_filter(tp, f);
Roi Dayan13fa8762016-11-01 16:08:29 +0200290 tcf_unbind_filter(tp, &f->res);
Cong Wang0dadc112017-11-06 13:47:24 -0800291 if (tcf_exts_get_net(&f->exts))
292 call_rcu(&f->rcu, fl_destroy_filter);
293 else
294 __fl_destroy_filter(f);
Roi Dayan13fa8762016-11-01 16:08:29 +0200295}
296
Daniel Borkmannd9363772016-11-27 01:18:01 +0100297static void fl_destroy_sleepable(struct work_struct *work)
298{
299 struct cls_fl_head *head = container_of(work, struct cls_fl_head,
300 work);
301 if (head->mask_assigned)
302 rhashtable_destroy(&head->ht);
303 kfree(head);
304 module_put(THIS_MODULE);
305}
306
307static void fl_destroy_rcu(struct rcu_head *rcu)
308{
309 struct cls_fl_head *head = container_of(rcu, struct cls_fl_head, rcu);
310
311 INIT_WORK(&head->work, fl_destroy_sleepable);
312 schedule_work(&head->work);
313}
314
WANG Cong763dbf62017-04-19 14:21:21 -0700315static void fl_destroy(struct tcf_proto *tp)
Jiri Pirko77b99002015-05-12 14:56:21 +0200316{
317 struct cls_fl_head *head = rtnl_dereference(tp->root);
318 struct cls_fl_filter *f, *next;
319
Roi Dayan13fa8762016-11-01 16:08:29 +0200320 list_for_each_entry_safe(f, next, &head->filters, list)
321 __fl_delete(tp, f);
Chris Mic15ab232017-08-30 02:31:58 -0400322 idr_destroy(&head->handle_idr);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100323
324 __module_get(THIS_MODULE);
325 call_rcu(&head->rcu, fl_destroy_rcu);
Jiri Pirko77b99002015-05-12 14:56:21 +0200326}
327
WANG Cong8113c092017-08-04 21:31:43 -0700328static void *fl_get(struct tcf_proto *tp, u32 handle)
Jiri Pirko77b99002015-05-12 14:56:21 +0200329{
330 struct cls_fl_head *head = rtnl_dereference(tp->root);
Jiri Pirko77b99002015-05-12 14:56:21 +0200331
Chris Mic15ab232017-08-30 02:31:58 -0400332 return idr_find_ext(&head->handle_idr, handle);
Jiri Pirko77b99002015-05-12 14:56:21 +0200333}
334
335static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
336 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
337 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
338 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
339 .len = IFNAMSIZ },
340 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
341 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
342 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
343 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
344 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
345 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
346 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
347 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
348 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
349 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
350 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
351 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
352 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
353 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
354 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
355 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
Jamal Hadi Salimb175c3a2015-06-25 06:55:27 -0400356 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
357 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300358 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
359 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
360 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
Amir Vadaibc3103f2016-09-08 16:23:47 +0300361 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
362 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
363 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
364 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
365 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
366 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
367 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
368 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
369 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
Or Gerlitzaa72d702016-09-15 15:28:22 +0300370 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
371 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
372 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
373 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
Simon Horman5976c5f2016-11-03 13:24:21 +0100374 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
375 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
376 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
377 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200378 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
379 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
380 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
381 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200382 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
383 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
Simon Horman7b684882016-12-07 13:48:28 +0100384 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
385 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
386 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
387 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
388 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
389 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
390 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
391 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
Simon Horman99d31322017-01-11 14:05:43 +0100392 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
393 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
394 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
395 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
396 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
397 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
398 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
399 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
400 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
401 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400402 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
403 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
404 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
405 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200406 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
407 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300408 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
409 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
410 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
411 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
Jiri Pirko77b99002015-05-12 14:56:21 +0200412};
413
414static void fl_set_key_val(struct nlattr **tb,
415 void *val, int val_type,
416 void *mask, int mask_type, int len)
417{
418 if (!tb[val_type])
419 return;
420 memcpy(val, nla_data(tb[val_type]), len);
421 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
422 memset(mask, 0xff, len);
423 else
424 memcpy(mask, nla_data(tb[mask_type]), len);
425}
426
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400427static int fl_set_key_mpls(struct nlattr **tb,
428 struct flow_dissector_key_mpls *key_val,
429 struct flow_dissector_key_mpls *key_mask)
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400430{
431 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
432 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
433 key_mask->mpls_ttl = MPLS_TTL_MASK;
434 }
435 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400436 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
437
438 if (bos & ~MPLS_BOS_MASK)
439 return -EINVAL;
440 key_val->mpls_bos = bos;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400441 key_mask->mpls_bos = MPLS_BOS_MASK;
442 }
443 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400444 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
445
446 if (tc & ~MPLS_TC_MASK)
447 return -EINVAL;
448 key_val->mpls_tc = tc;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400449 key_mask->mpls_tc = MPLS_TC_MASK;
450 }
451 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400452 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
453
454 if (label & ~MPLS_LABEL_MASK)
455 return -EINVAL;
456 key_val->mpls_label = label;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400457 key_mask->mpls_label = MPLS_LABEL_MASK;
458 }
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400459 return 0;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400460}
461
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300462static void fl_set_key_vlan(struct nlattr **tb,
463 struct flow_dissector_key_vlan *key_val,
464 struct flow_dissector_key_vlan *key_mask)
465{
466#define VLAN_PRIORITY_MASK 0x7
467
468 if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
469 key_val->vlan_id =
470 nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
471 key_mask->vlan_id = VLAN_VID_MASK;
472 }
473 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
474 key_val->vlan_priority =
475 nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
476 VLAN_PRIORITY_MASK;
477 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
478 }
479}
480
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200481static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
482 u32 *dissector_key, u32 *dissector_mask,
483 u32 flower_flag_bit, u32 dissector_flag_bit)
484{
485 if (flower_mask & flower_flag_bit) {
486 *dissector_mask |= dissector_flag_bit;
487 if (flower_key & flower_flag_bit)
488 *dissector_key |= dissector_flag_bit;
489 }
490}
491
Or Gerlitzd9724772016-12-22 14:28:15 +0200492static int fl_set_key_flags(struct nlattr **tb,
493 u32 *flags_key, u32 *flags_mask)
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200494{
495 u32 key, mask;
496
Or Gerlitzd9724772016-12-22 14:28:15 +0200497 /* mask is mandatory for flags */
498 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
499 return -EINVAL;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200500
501 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
Or Gerlitzd9724772016-12-22 14:28:15 +0200502 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200503
504 *flags_key = 0;
505 *flags_mask = 0;
506
507 fl_set_key_flag(key, mask, flags_key, flags_mask,
508 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Or Gerlitzd9724772016-12-22 14:28:15 +0200509
510 return 0;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200511}
512
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300513static void fl_set_key_ip(struct nlattr **tb,
514 struct flow_dissector_key_ip *key,
515 struct flow_dissector_key_ip *mask)
516{
517 fl_set_key_val(tb, &key->tos, TCA_FLOWER_KEY_IP_TOS,
518 &mask->tos, TCA_FLOWER_KEY_IP_TOS_MASK,
519 sizeof(key->tos));
520
521 fl_set_key_val(tb, &key->ttl, TCA_FLOWER_KEY_IP_TTL,
522 &mask->ttl, TCA_FLOWER_KEY_IP_TTL_MASK,
523 sizeof(key->ttl));
524}
525
Jiri Pirko77b99002015-05-12 14:56:21 +0200526static int fl_set_key(struct net *net, struct nlattr **tb,
527 struct fl_flow_key *key, struct fl_flow_key *mask)
528{
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300529 __be16 ethertype;
Or Gerlitzd9724772016-12-22 14:28:15 +0200530 int ret = 0;
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400531#ifdef CONFIG_NET_CLS_IND
Jiri Pirko77b99002015-05-12 14:56:21 +0200532 if (tb[TCA_FLOWER_INDEV]) {
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400533 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV]);
Jiri Pirko77b99002015-05-12 14:56:21 +0200534 if (err < 0)
535 return err;
536 key->indev_ifindex = err;
537 mask->indev_ifindex = 0xffffffff;
538 }
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400539#endif
Jiri Pirko77b99002015-05-12 14:56:21 +0200540
541 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
542 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
543 sizeof(key->eth.dst));
544 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
545 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
546 sizeof(key->eth.src));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500547
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200548 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300549 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
550
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200551 if (ethertype == htons(ETH_P_8021Q)) {
552 fl_set_key_vlan(tb, &key->vlan, &mask->vlan);
553 fl_set_key_val(tb, &key->basic.n_proto,
554 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
555 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
556 sizeof(key->basic.n_proto));
557 } else {
558 key->basic.n_proto = ethertype;
559 mask->basic.n_proto = cpu_to_be16(~0);
560 }
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300561 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500562
Jiri Pirko77b99002015-05-12 14:56:21 +0200563 if (key->basic.n_proto == htons(ETH_P_IP) ||
564 key->basic.n_proto == htons(ETH_P_IPV6)) {
565 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
566 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
567 sizeof(key->basic.ip_proto));
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300568 fl_set_key_ip(tb, &key->ip, &mask->ip);
Jiri Pirko77b99002015-05-12 14:56:21 +0200569 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500570
571 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
572 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200573 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200574 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
575 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
576 sizeof(key->ipv4.src));
577 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
578 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
579 sizeof(key->ipv4.dst));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500580 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
581 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200582 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200583 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
584 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
585 sizeof(key->ipv6.src));
586 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
587 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
588 sizeof(key->ipv6.dst));
589 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500590
Jiri Pirko77b99002015-05-12 14:56:21 +0200591 if (key->basic.ip_proto == IPPROTO_TCP) {
592 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300593 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200594 sizeof(key->tp.src));
595 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300596 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200597 sizeof(key->tp.dst));
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200598 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
599 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
600 sizeof(key->tcp.flags));
Jiri Pirko77b99002015-05-12 14:56:21 +0200601 } else if (key->basic.ip_proto == IPPROTO_UDP) {
602 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300603 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200604 sizeof(key->tp.src));
605 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300606 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200607 sizeof(key->tp.dst));
Simon Horman5976c5f2016-11-03 13:24:21 +0100608 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
609 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
610 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
611 sizeof(key->tp.src));
612 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
613 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
614 sizeof(key->tp.dst));
Simon Horman7b684882016-12-07 13:48:28 +0100615 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
616 key->basic.ip_proto == IPPROTO_ICMP) {
617 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
618 &mask->icmp.type,
619 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
620 sizeof(key->icmp.type));
621 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
622 &mask->icmp.code,
623 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
624 sizeof(key->icmp.code));
625 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
626 key->basic.ip_proto == IPPROTO_ICMPV6) {
627 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
628 &mask->icmp.type,
629 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
630 sizeof(key->icmp.type));
Simon Horman040587a2017-01-30 16:19:02 +0100631 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
Simon Horman7b684882016-12-07 13:48:28 +0100632 &mask->icmp.code,
Simon Horman040587a2017-01-30 16:19:02 +0100633 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
Simon Horman7b684882016-12-07 13:48:28 +0100634 sizeof(key->icmp.code));
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400635 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
636 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400637 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
638 if (ret)
639 return ret;
Simon Horman99d31322017-01-11 14:05:43 +0100640 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
641 key->basic.n_proto == htons(ETH_P_RARP)) {
642 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
643 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
644 sizeof(key->arp.sip));
645 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
646 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
647 sizeof(key->arp.tip));
648 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
649 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
650 sizeof(key->arp.op));
651 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
652 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
653 sizeof(key->arp.sha));
654 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
655 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
656 sizeof(key->arp.tha));
Jiri Pirko77b99002015-05-12 14:56:21 +0200657 }
658
Amir Vadaibc3103f2016-09-08 16:23:47 +0300659 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
660 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
661 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200662 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300663 fl_set_key_val(tb, &key->enc_ipv4.src,
664 TCA_FLOWER_KEY_ENC_IPV4_SRC,
665 &mask->enc_ipv4.src,
666 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
667 sizeof(key->enc_ipv4.src));
668 fl_set_key_val(tb, &key->enc_ipv4.dst,
669 TCA_FLOWER_KEY_ENC_IPV4_DST,
670 &mask->enc_ipv4.dst,
671 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
672 sizeof(key->enc_ipv4.dst));
673 }
674
675 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
676 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
677 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200678 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300679 fl_set_key_val(tb, &key->enc_ipv6.src,
680 TCA_FLOWER_KEY_ENC_IPV6_SRC,
681 &mask->enc_ipv6.src,
682 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
683 sizeof(key->enc_ipv6.src));
684 fl_set_key_val(tb, &key->enc_ipv6.dst,
685 TCA_FLOWER_KEY_ENC_IPV6_DST,
686 &mask->enc_ipv6.dst,
687 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
688 sizeof(key->enc_ipv6.dst));
689 }
690
691 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +0300692 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
Amir Vadaibc3103f2016-09-08 16:23:47 +0300693 sizeof(key->enc_key_id.keyid));
694
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200695 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
696 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
697 sizeof(key->enc_tp.src));
698
699 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
700 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
701 sizeof(key->enc_tp.dst));
702
Or Gerlitzd9724772016-12-22 14:28:15 +0200703 if (tb[TCA_FLOWER_KEY_FLAGS])
704 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200705
Or Gerlitzd9724772016-12-22 14:28:15 +0200706 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +0200707}
708
709static bool fl_mask_eq(struct fl_flow_mask *mask1,
710 struct fl_flow_mask *mask2)
711{
712 const long *lmask1 = fl_key_get_start(&mask1->key, mask1);
713 const long *lmask2 = fl_key_get_start(&mask2->key, mask2);
714
715 return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) &&
716 !memcmp(lmask1, lmask2, fl_mask_range(mask1));
717}
718
719static const struct rhashtable_params fl_ht_params = {
720 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
721 .head_offset = offsetof(struct cls_fl_filter, ht_node),
722 .automatic_shrinking = true,
723};
724
725static int fl_init_hashtable(struct cls_fl_head *head,
726 struct fl_flow_mask *mask)
727{
728 head->ht_params = fl_ht_params;
729 head->ht_params.key_len = fl_mask_range(mask);
730 head->ht_params.key_offset += mask->range.start;
731
732 return rhashtable_init(&head->ht, &head->ht_params);
733}
734
735#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
736#define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
Jiri Pirko77b99002015-05-12 14:56:21 +0200737
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300738#define FL_KEY_IS_MASKED(mask, member) \
739 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
740 0, FL_KEY_MEMBER_SIZE(member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200741
742#define FL_KEY_SET(keys, cnt, id, member) \
743 do { \
744 keys[cnt].key_id = id; \
745 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
746 cnt++; \
747 } while(0);
748
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300749#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200750 do { \
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300751 if (FL_KEY_IS_MASKED(mask, member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200752 FL_KEY_SET(keys, cnt, id, member); \
753 } while(0);
754
755static void fl_init_dissector(struct cls_fl_head *head,
756 struct fl_flow_mask *mask)
757{
758 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
759 size_t cnt = 0;
760
Tom Herbert42aecaa2015-06-04 09:16:39 -0700761 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
Jiri Pirko77b99002015-05-12 14:56:21 +0200762 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300763 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
764 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
765 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
766 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
767 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
768 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
769 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
770 FLOW_DISSECTOR_KEY_PORTS, tp);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300771 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300772 FLOW_DISSECTOR_KEY_IP, ip);
773 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200774 FLOW_DISSECTOR_KEY_TCP, tcp);
775 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman7b684882016-12-07 13:48:28 +0100776 FLOW_DISSECTOR_KEY_ICMP, icmp);
777 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman99d31322017-01-11 14:05:43 +0100778 FLOW_DISSECTOR_KEY_ARP, arp);
779 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400780 FLOW_DISSECTOR_KEY_MPLS, mpls);
781 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300782 FLOW_DISSECTOR_KEY_VLAN, vlan);
Hadar Hen Zion519d1052016-11-07 15:14:38 +0200783 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
784 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
785 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
786 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
787 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
788 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
789 if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
790 FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
791 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
792 enc_control);
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200793 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
794 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
Jiri Pirko77b99002015-05-12 14:56:21 +0200795
796 skb_flow_dissector_init(&head->dissector, keys, cnt);
797}
798
799static int fl_check_assign_mask(struct cls_fl_head *head,
800 struct fl_flow_mask *mask)
801{
802 int err;
803
804 if (head->mask_assigned) {
805 if (!fl_mask_eq(&head->mask, mask))
806 return -EINVAL;
807 else
808 return 0;
809 }
810
811 /* Mask is not assigned yet. So assign it and init hashtable
812 * according to that.
813 */
814 err = fl_init_hashtable(head, mask);
815 if (err)
816 return err;
817 memcpy(&head->mask, mask, sizeof(head->mask));
818 head->mask_assigned = true;
819
820 fl_init_dissector(head, mask);
821
822 return 0;
823}
824
825static int fl_set_parms(struct net *net, struct tcf_proto *tp,
826 struct cls_fl_filter *f, struct fl_flow_mask *mask,
827 unsigned long base, struct nlattr **tb,
828 struct nlattr *est, bool ovr)
829{
Jiri Pirko77b99002015-05-12 14:56:21 +0200830 int err;
831
Jiri Pirko45507522017-08-04 14:29:06 +0200832 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr);
Jiri Pirko77b99002015-05-12 14:56:21 +0200833 if (err < 0)
834 return err;
835
836 if (tb[TCA_FLOWER_CLASSID]) {
837 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
838 tcf_bind_filter(tp, &f->res, base);
839 }
840
841 err = fl_set_key(net, tb, &f->key, &mask->key);
842 if (err)
Jiri Pirko45507522017-08-04 14:29:06 +0200843 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +0200844
845 fl_mask_update_range(mask);
846 fl_set_masked_key(&f->mkey, &f->key, mask);
847
Jiri Pirko77b99002015-05-12 14:56:21 +0200848 return 0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200849}
850
Jiri Pirko77b99002015-05-12 14:56:21 +0200851static int fl_change(struct net *net, struct sk_buff *in_skb,
852 struct tcf_proto *tp, unsigned long base,
853 u32 handle, struct nlattr **tca,
WANG Cong8113c092017-08-04 21:31:43 -0700854 void **arg, bool ovr)
Jiri Pirko77b99002015-05-12 14:56:21 +0200855{
856 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700857 struct cls_fl_filter *fold = *arg;
Jiri Pirko77b99002015-05-12 14:56:21 +0200858 struct cls_fl_filter *fnew;
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100859 struct nlattr **tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200860 struct fl_flow_mask mask = {};
Chris Mic15ab232017-08-30 02:31:58 -0400861 unsigned long idr_index;
Jiri Pirko77b99002015-05-12 14:56:21 +0200862 int err;
863
864 if (!tca[TCA_OPTIONS])
865 return -EINVAL;
866
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100867 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
868 if (!tb)
869 return -ENOBUFS;
870
Johannes Bergfceb6432017-04-12 14:34:07 +0200871 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
872 fl_policy, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +0200873 if (err < 0)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100874 goto errout_tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200875
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100876 if (fold && handle && fold->handle != handle) {
877 err = -EINVAL;
878 goto errout_tb;
879 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200880
881 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100882 if (!fnew) {
883 err = -ENOBUFS;
884 goto errout_tb;
885 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200886
WANG Congb9a24bb2016-08-19 12:36:54 -0700887 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
888 if (err < 0)
889 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +0200890
891 if (!handle) {
Chris Mic15ab232017-08-30 02:31:58 -0400892 err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
893 1, 0x80000000, GFP_KERNEL);
894 if (err)
Jiri Pirko77b99002015-05-12 14:56:21 +0200895 goto errout;
Chris Mic15ab232017-08-30 02:31:58 -0400896 fnew->handle = idr_index;
Jiri Pirko77b99002015-05-12 14:56:21 +0200897 }
Chris Mic15ab232017-08-30 02:31:58 -0400898
899 /* user specifies a handle and it doesn't exist */
900 if (handle && !fold) {
901 err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
902 handle, handle + 1, GFP_KERNEL);
903 if (err)
904 goto errout;
905 fnew->handle = idr_index;
906 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200907
Amir Vadaie69985c2016-06-05 17:11:18 +0300908 if (tb[TCA_FLOWER_FLAGS]) {
909 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
910
911 if (!tc_flags_valid(fnew->flags)) {
912 err = -EINVAL;
Cong Wangfe2502e2017-09-20 09:18:45 -0700913 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +0300914 }
915 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200916
Jiri Pirko77b99002015-05-12 14:56:21 +0200917 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr);
918 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700919 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +0200920
921 err = fl_check_assign_mask(head, &mask);
922 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700923 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +0200924
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300925 if (!tc_skip_sw(fnew->flags)) {
Paul Blakeya3308d82017-01-16 10:45:13 +0200926 if (!fold && fl_lookup(head, &fnew->mkey)) {
927 err = -EEXIST;
Cong Wangfe2502e2017-09-20 09:18:45 -0700928 goto errout_idr;
Paul Blakeya3308d82017-01-16 10:45:13 +0200929 }
930
Amir Vadaie69985c2016-06-05 17:11:18 +0300931 err = rhashtable_insert_fast(&head->ht, &fnew->ht_node,
932 head->ht_params);
933 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700934 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +0300935 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200936
Hadar Hen Zion79685212016-12-01 14:06:34 +0200937 if (!tc_skip_hw(fnew->flags)) {
938 err = fl_hw_replace_filter(tp,
939 &head->dissector,
940 &mask.key,
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200941 fnew);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200942 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700943 goto errout_idr;
Hadar Hen Zion79685212016-12-01 14:06:34 +0200944 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200945
Or Gerlitz55593962017-02-16 10:31:13 +0200946 if (!tc_in_hw(fnew->flags))
947 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
948
Amir Vadai5b33f482016-03-08 12:42:29 +0200949 if (fold) {
Jiri Pirko725cbb622016-11-28 15:40:13 +0100950 if (!tc_skip_sw(fold->flags))
951 rhashtable_remove_fast(&head->ht, &fold->ht_node,
952 head->ht_params);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200953 if (!tc_skip_hw(fold->flags))
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200954 fl_hw_destroy_filter(tp, fold);
Amir Vadai5b33f482016-03-08 12:42:29 +0200955 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200956
WANG Cong8113c092017-08-04 21:31:43 -0700957 *arg = fnew;
Jiri Pirko77b99002015-05-12 14:56:21 +0200958
959 if (fold) {
Chris Mic15ab232017-08-30 02:31:58 -0400960 fnew->handle = handle;
961 idr_replace_ext(&head->handle_idr, fnew, fnew->handle);
Daniel Borkmannff3532f2015-07-17 22:38:44 +0200962 list_replace_rcu(&fold->list, &fnew->list);
Jiri Pirko77b99002015-05-12 14:56:21 +0200963 tcf_unbind_filter(tp, &fold->res);
Cong Wang0dadc112017-11-06 13:47:24 -0800964 tcf_exts_get_net(&fold->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +0200965 call_rcu(&fold->rcu, fl_destroy_filter);
966 } else {
967 list_add_tail_rcu(&fnew->list, &head->filters);
968 }
969
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100970 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +0200971 return 0;
972
Cong Wangfe2502e2017-09-20 09:18:45 -0700973errout_idr:
974 if (fnew->handle)
975 idr_remove_ext(&head->handle_idr, fnew->handle);
Jiri Pirko77b99002015-05-12 14:56:21 +0200976errout:
WANG Congb9a24bb2016-08-19 12:36:54 -0700977 tcf_exts_destroy(&fnew->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +0200978 kfree(fnew);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100979errout_tb:
980 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +0200981 return err;
982}
983
WANG Cong8113c092017-08-04 21:31:43 -0700984static int fl_delete(struct tcf_proto *tp, void *arg, bool *last)
Jiri Pirko77b99002015-05-12 14:56:21 +0200985{
986 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700987 struct cls_fl_filter *f = arg;
Jiri Pirko77b99002015-05-12 14:56:21 +0200988
Jiri Pirko725cbb622016-11-28 15:40:13 +0100989 if (!tc_skip_sw(f->flags))
990 rhashtable_remove_fast(&head->ht, &f->ht_node,
991 head->ht_params);
Roi Dayan13fa8762016-11-01 16:08:29 +0200992 __fl_delete(tp, f);
WANG Cong763dbf62017-04-19 14:21:21 -0700993 *last = list_empty(&head->filters);
Jiri Pirko77b99002015-05-12 14:56:21 +0200994 return 0;
995}
996
997static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
998{
999 struct cls_fl_head *head = rtnl_dereference(tp->root);
1000 struct cls_fl_filter *f;
1001
1002 list_for_each_entry_rcu(f, &head->filters, list) {
1003 if (arg->count < arg->skip)
1004 goto skip;
WANG Cong8113c092017-08-04 21:31:43 -07001005 if (arg->fn(tp, f, arg) < 0) {
Jiri Pirko77b99002015-05-12 14:56:21 +02001006 arg->stop = 1;
1007 break;
1008 }
1009skip:
1010 arg->count++;
1011 }
1012}
1013
1014static int fl_dump_key_val(struct sk_buff *skb,
1015 void *val, int val_type,
1016 void *mask, int mask_type, int len)
1017{
1018 int err;
1019
1020 if (!memchr_inv(mask, 0, len))
1021 return 0;
1022 err = nla_put(skb, val_type, len, val);
1023 if (err)
1024 return err;
1025 if (mask_type != TCA_FLOWER_UNSPEC) {
1026 err = nla_put(skb, mask_type, len, mask);
1027 if (err)
1028 return err;
1029 }
1030 return 0;
1031}
1032
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001033static int fl_dump_key_mpls(struct sk_buff *skb,
1034 struct flow_dissector_key_mpls *mpls_key,
1035 struct flow_dissector_key_mpls *mpls_mask)
1036{
1037 int err;
1038
1039 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1040 return 0;
1041 if (mpls_mask->mpls_ttl) {
1042 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1043 mpls_key->mpls_ttl);
1044 if (err)
1045 return err;
1046 }
1047 if (mpls_mask->mpls_tc) {
1048 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1049 mpls_key->mpls_tc);
1050 if (err)
1051 return err;
1052 }
1053 if (mpls_mask->mpls_label) {
1054 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1055 mpls_key->mpls_label);
1056 if (err)
1057 return err;
1058 }
1059 if (mpls_mask->mpls_bos) {
1060 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1061 mpls_key->mpls_bos);
1062 if (err)
1063 return err;
1064 }
1065 return 0;
1066}
1067
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001068static int fl_dump_key_ip(struct sk_buff *skb,
1069 struct flow_dissector_key_ip *key,
1070 struct flow_dissector_key_ip *mask)
1071{
1072 if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos,
1073 TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) ||
1074 fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl,
1075 TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl)))
1076 return -1;
1077
1078 return 0;
1079}
1080
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001081static int fl_dump_key_vlan(struct sk_buff *skb,
1082 struct flow_dissector_key_vlan *vlan_key,
1083 struct flow_dissector_key_vlan *vlan_mask)
1084{
1085 int err;
1086
1087 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1088 return 0;
1089 if (vlan_mask->vlan_id) {
1090 err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID,
1091 vlan_key->vlan_id);
1092 if (err)
1093 return err;
1094 }
1095 if (vlan_mask->vlan_priority) {
1096 err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO,
1097 vlan_key->vlan_priority);
1098 if (err)
1099 return err;
1100 }
1101 return 0;
1102}
1103
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001104static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1105 u32 *flower_key, u32 *flower_mask,
1106 u32 flower_flag_bit, u32 dissector_flag_bit)
1107{
1108 if (dissector_mask & dissector_flag_bit) {
1109 *flower_mask |= flower_flag_bit;
1110 if (dissector_key & dissector_flag_bit)
1111 *flower_key |= flower_flag_bit;
1112 }
1113}
1114
1115static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1116{
1117 u32 key, mask;
1118 __be32 _key, _mask;
1119 int err;
1120
1121 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1122 return 0;
1123
1124 key = 0;
1125 mask = 0;
1126
1127 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1128 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1129
1130 _key = cpu_to_be32(key);
1131 _mask = cpu_to_be32(mask);
1132
1133 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1134 if (err)
1135 return err;
1136
1137 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1138}
1139
WANG Cong8113c092017-08-04 21:31:43 -07001140static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jiri Pirko77b99002015-05-12 14:56:21 +02001141 struct sk_buff *skb, struct tcmsg *t)
1142{
1143 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -07001144 struct cls_fl_filter *f = fh;
Jiri Pirko77b99002015-05-12 14:56:21 +02001145 struct nlattr *nest;
1146 struct fl_flow_key *key, *mask;
1147
1148 if (!f)
1149 return skb->len;
1150
1151 t->tcm_handle = f->handle;
1152
1153 nest = nla_nest_start(skb, TCA_OPTIONS);
1154 if (!nest)
1155 goto nla_put_failure;
1156
1157 if (f->res.classid &&
1158 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1159 goto nla_put_failure;
1160
1161 key = &f->key;
1162 mask = &head->mask.key;
1163
1164 if (mask->indev_ifindex) {
1165 struct net_device *dev;
1166
1167 dev = __dev_get_by_index(net, key->indev_ifindex);
1168 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1169 goto nla_put_failure;
1170 }
1171
Hadar Hen Zion79685212016-12-01 14:06:34 +02001172 if (!tc_skip_hw(f->flags))
1173 fl_hw_update_stats(tp, f);
Amir Vadai10cbc682016-05-13 12:55:37 +00001174
Jiri Pirko77b99002015-05-12 14:56:21 +02001175 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1176 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1177 sizeof(key->eth.dst)) ||
1178 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1179 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1180 sizeof(key->eth.src)) ||
1181 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1182 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1183 sizeof(key->basic.n_proto)))
1184 goto nla_put_failure;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001185
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001186 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1187 goto nla_put_failure;
1188
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001189 if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan))
1190 goto nla_put_failure;
1191
Jiri Pirko77b99002015-05-12 14:56:21 +02001192 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1193 key->basic.n_proto == htons(ETH_P_IPV6)) &&
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001194 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
Jiri Pirko77b99002015-05-12 14:56:21 +02001195 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001196 sizeof(key->basic.ip_proto)) ||
1197 fl_dump_key_ip(skb, &key->ip, &mask->ip)))
Jiri Pirko77b99002015-05-12 14:56:21 +02001198 goto nla_put_failure;
1199
Tom Herbertc3f83242015-06-04 09:16:40 -07001200 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001201 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1202 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1203 sizeof(key->ipv4.src)) ||
1204 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1205 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1206 sizeof(key->ipv4.dst))))
1207 goto nla_put_failure;
Tom Herbertc3f83242015-06-04 09:16:40 -07001208 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001209 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1210 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1211 sizeof(key->ipv6.src)) ||
1212 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1213 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1214 sizeof(key->ipv6.dst))))
1215 goto nla_put_failure;
1216
1217 if (key->basic.ip_proto == IPPROTO_TCP &&
1218 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001219 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001220 sizeof(key->tp.src)) ||
1221 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001222 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001223 sizeof(key->tp.dst)) ||
1224 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1225 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1226 sizeof(key->tcp.flags))))
Jiri Pirko77b99002015-05-12 14:56:21 +02001227 goto nla_put_failure;
1228 else if (key->basic.ip_proto == IPPROTO_UDP &&
1229 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001230 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001231 sizeof(key->tp.src)) ||
1232 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001233 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001234 sizeof(key->tp.dst))))
1235 goto nla_put_failure;
Simon Horman5976c5f2016-11-03 13:24:21 +01001236 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1237 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1238 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1239 sizeof(key->tp.src)) ||
1240 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1241 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1242 sizeof(key->tp.dst))))
1243 goto nla_put_failure;
Simon Horman7b684882016-12-07 13:48:28 +01001244 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1245 key->basic.ip_proto == IPPROTO_ICMP &&
1246 (fl_dump_key_val(skb, &key->icmp.type,
1247 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1248 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1249 sizeof(key->icmp.type)) ||
1250 fl_dump_key_val(skb, &key->icmp.code,
1251 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1252 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1253 sizeof(key->icmp.code))))
1254 goto nla_put_failure;
1255 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1256 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1257 (fl_dump_key_val(skb, &key->icmp.type,
1258 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1259 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1260 sizeof(key->icmp.type)) ||
1261 fl_dump_key_val(skb, &key->icmp.code,
1262 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1263 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1264 sizeof(key->icmp.code))))
1265 goto nla_put_failure;
Simon Horman99d31322017-01-11 14:05:43 +01001266 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1267 key->basic.n_proto == htons(ETH_P_RARP)) &&
1268 (fl_dump_key_val(skb, &key->arp.sip,
1269 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1270 TCA_FLOWER_KEY_ARP_SIP_MASK,
1271 sizeof(key->arp.sip)) ||
1272 fl_dump_key_val(skb, &key->arp.tip,
1273 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1274 TCA_FLOWER_KEY_ARP_TIP_MASK,
1275 sizeof(key->arp.tip)) ||
1276 fl_dump_key_val(skb, &key->arp.op,
1277 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1278 TCA_FLOWER_KEY_ARP_OP_MASK,
1279 sizeof(key->arp.op)) ||
1280 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1281 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1282 sizeof(key->arp.sha)) ||
1283 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1284 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1285 sizeof(key->arp.tha))))
1286 goto nla_put_failure;
Jiri Pirko77b99002015-05-12 14:56:21 +02001287
Amir Vadaibc3103f2016-09-08 16:23:47 +03001288 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1289 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1290 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1291 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1292 sizeof(key->enc_ipv4.src)) ||
1293 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1294 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1295 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1296 sizeof(key->enc_ipv4.dst))))
1297 goto nla_put_failure;
1298 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1299 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1300 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1301 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1302 sizeof(key->enc_ipv6.src)) ||
1303 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1304 TCA_FLOWER_KEY_ENC_IPV6_DST,
1305 &mask->enc_ipv6.dst,
1306 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1307 sizeof(key->enc_ipv6.dst))))
1308 goto nla_put_failure;
1309
1310 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001311 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001312 sizeof(key->enc_key_id)) ||
1313 fl_dump_key_val(skb, &key->enc_tp.src,
1314 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1315 &mask->enc_tp.src,
1316 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1317 sizeof(key->enc_tp.src)) ||
1318 fl_dump_key_val(skb, &key->enc_tp.dst,
1319 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1320 &mask->enc_tp.dst,
1321 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1322 sizeof(key->enc_tp.dst)))
Amir Vadaibc3103f2016-09-08 16:23:47 +03001323 goto nla_put_failure;
1324
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001325 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1326 goto nla_put_failure;
1327
Or Gerlitz749e6722017-02-16 10:31:10 +02001328 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1329 goto nla_put_failure;
Amir Vadaie69985c2016-06-05 17:11:18 +03001330
Jiri Pirko77b99002015-05-12 14:56:21 +02001331 if (tcf_exts_dump(skb, &f->exts))
1332 goto nla_put_failure;
1333
1334 nla_nest_end(skb, nest);
1335
1336 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1337 goto nla_put_failure;
1338
1339 return skb->len;
1340
1341nla_put_failure:
1342 nla_nest_cancel(skb, nest);
1343 return -1;
1344}
1345
Cong Wang07d79fc2017-08-30 14:30:36 -07001346static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
1347{
1348 struct cls_fl_filter *f = fh;
1349
1350 if (f && f->res.classid == classid)
1351 f->res.class = cl;
1352}
1353
Jiri Pirko77b99002015-05-12 14:56:21 +02001354static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1355 .kind = "flower",
1356 .classify = fl_classify,
1357 .init = fl_init,
1358 .destroy = fl_destroy,
1359 .get = fl_get,
1360 .change = fl_change,
1361 .delete = fl_delete,
1362 .walk = fl_walk,
1363 .dump = fl_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07001364 .bind_class = fl_bind_class,
Jiri Pirko77b99002015-05-12 14:56:21 +02001365 .owner = THIS_MODULE,
1366};
1367
1368static int __init cls_fl_init(void)
1369{
1370 return register_tcf_proto_ops(&cls_fl_ops);
1371}
1372
1373static void __exit cls_fl_exit(void)
1374{
1375 unregister_tcf_proto_ops(&cls_fl_ops);
1376}
1377
1378module_init(cls_fl_init);
1379module_exit(cls_fl_exit);
1380
1381MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1382MODULE_DESCRIPTION("Flower classifier");
1383MODULE_LICENSE("GPL v2");