blob: 5cb694469b517626667e9a9419a7ff116d0024ee [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jiri Pirko77b99002015-05-12 14:56:21 +02002/*
3 * net/sched/cls_flower.c Flower classifier
4 *
5 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
Jiri Pirko77b99002015-05-12 14:56:21 +02006 */
7
8#include <linux/kernel.h>
9#include <linux/init.h>
10#include <linux/module.h>
11#include <linux/rhashtable.h>
Daniel Borkmannd9363772016-11-27 01:18:01 +010012#include <linux/workqueue.h>
Vlad Buslov06177552019-03-21 15:17:35 +020013#include <linux/refcount.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020014
15#include <linux/if_ether.h>
16#include <linux/in6.h>
17#include <linux/ip.h>
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -040018#include <linux/mpls.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020019
20#include <net/sch_generic.h>
21#include <net/pkt_cls.h>
22#include <net/ip.h>
23#include <net/flow_dissector.h>
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +020024#include <net/geneve.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020025
Amir Vadaibc3103f2016-09-08 16:23:47 +030026#include <net/dst.h>
27#include <net/dst_metadata.h>
28
Paul Blakeye0ace682019-07-09 10:30:50 +030029#include <uapi/linux/netfilter/nf_conntrack_common.h>
30
Jiri Pirko77b99002015-05-12 14:56:21 +020031struct fl_flow_key {
Jiri Pirko8212ed72019-06-19 09:41:03 +030032 struct flow_dissector_key_meta meta;
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;
Jianbo Liud64efd02018-07-06 05:38:16 +000038 struct flow_dissector_key_vlan cvlan;
Jiri Pirko77b99002015-05-12 14:56:21 +020039 union {
Tom Herbertc3f83242015-06-04 09:16:40 -070040 struct flow_dissector_key_ipv4_addrs ipv4;
Jiri Pirko77b99002015-05-12 14:56:21 +020041 struct flow_dissector_key_ipv6_addrs ipv6;
42 };
43 struct flow_dissector_key_ports tp;
Simon Horman7b684882016-12-07 13:48:28 +010044 struct flow_dissector_key_icmp icmp;
Simon Horman99d31322017-01-11 14:05:43 +010045 struct flow_dissector_key_arp arp;
Amir Vadaibc3103f2016-09-08 16:23:47 +030046 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 };
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +020051 struct flow_dissector_key_ports enc_tp;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -040052 struct flow_dissector_key_mpls mpls;
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +020053 struct flow_dissector_key_tcp tcp;
Or Gerlitz4d80cc02017-06-01 21:37:38 +030054 struct flow_dissector_key_ip ip;
Or Gerlitz0e2c17b2018-07-17 19:27:18 +030055 struct flow_dissector_key_ip enc_ip;
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +020056 struct flow_dissector_key_enc_opts enc_opts;
Amritha Nambiar5c722992018-11-12 16:15:55 -080057 struct flow_dissector_key_ports tp_min;
58 struct flow_dissector_key_ports tp_max;
Paul Blakeye0ace682019-07-09 10:30:50 +030059 struct flow_dissector_key_ct ct;
Jiri Pirko77b99002015-05-12 14:56:21 +020060} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
61
62struct fl_flow_mask_range {
63 unsigned short int start;
64 unsigned short int end;
65};
66
67struct fl_flow_mask {
68 struct fl_flow_key key;
69 struct fl_flow_mask_range range;
Amritha Nambiar5c722992018-11-12 16:15:55 -080070 u32 flags;
Paul Blakey05cd2712018-04-30 14:28:30 +030071 struct rhash_head ht_node;
72 struct rhashtable ht;
73 struct rhashtable_params filter_ht_params;
74 struct flow_dissector dissector;
75 struct list_head filters;
Paolo Abeni44a5cd42018-06-21 20:02:16 +020076 struct rcu_work rwork;
Paul Blakey05cd2712018-04-30 14:28:30 +030077 struct list_head list;
Vlad Buslovf48ef4d2019-03-21 15:17:37 +020078 refcount_t refcnt;
Jiri Pirko77b99002015-05-12 14:56:21 +020079};
80
Jiri Pirkob95ec7e2018-07-23 09:23:10 +020081struct fl_flow_tmplt {
82 struct fl_flow_key dummy_key;
83 struct fl_flow_key mask;
84 struct flow_dissector dissector;
85 struct tcf_chain *chain;
86};
87
Jiri Pirko77b99002015-05-12 14:56:21 +020088struct cls_fl_head {
89 struct rhashtable ht;
Vlad Buslov259e60f2019-03-21 15:17:39 +020090 spinlock_t masks_lock; /* Protect masks list */
Paul Blakey05cd2712018-04-30 14:28:30 +030091 struct list_head masks;
Vlad Buslovc049d562019-04-24 09:53:31 +030092 struct list_head hw_filters;
Cong Wangaaa908f2018-05-23 15:26:53 -070093 struct rcu_work rwork;
Chris Mic15ab232017-08-30 02:31:58 -040094 struct idr handle_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +020095};
96
97struct cls_fl_filter {
Paul Blakey05cd2712018-04-30 14:28:30 +030098 struct fl_flow_mask *mask;
Jiri Pirko77b99002015-05-12 14:56:21 +020099 struct rhash_head ht_node;
100 struct fl_flow_key mkey;
101 struct tcf_exts exts;
102 struct tcf_result res;
103 struct fl_flow_key key;
104 struct list_head list;
Vlad Buslovc049d562019-04-24 09:53:31 +0300105 struct list_head hw_list;
Jiri Pirko77b99002015-05-12 14:56:21 +0200106 u32 handle;
Amir Vadaie69985c2016-06-05 17:11:18 +0300107 u32 flags;
Vlad Buslov86c55362018-09-07 17:22:21 +0300108 u32 in_hw_count;
Cong Wangaaa908f2018-05-23 15:26:53 -0700109 struct rcu_work rwork;
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200110 struct net_device *hw_dev;
Vlad Buslov06177552019-03-21 15:17:35 +0200111 /* Flower classifier is unlocked, which means that its reference counter
112 * can be changed concurrently without any kind of external
113 * synchronization. Use atomic reference counter to be concurrency-safe.
114 */
115 refcount_t refcnt;
Vlad Buslovb2552b82019-03-21 15:17:36 +0200116 bool deleted;
Jiri Pirko77b99002015-05-12 14:56:21 +0200117};
118
Paul Blakey05cd2712018-04-30 14:28:30 +0300119static const struct rhashtable_params mask_ht_params = {
120 .key_offset = offsetof(struct fl_flow_mask, key),
121 .key_len = sizeof(struct fl_flow_key),
122 .head_offset = offsetof(struct fl_flow_mask, ht_node),
123 .automatic_shrinking = true,
124};
125
Jiri Pirko77b99002015-05-12 14:56:21 +0200126static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
127{
128 return mask->range.end - mask->range.start;
129}
130
131static void fl_mask_update_range(struct fl_flow_mask *mask)
132{
133 const u8 *bytes = (const u8 *) &mask->key;
134 size_t size = sizeof(mask->key);
Paul Blakey05cd2712018-04-30 14:28:30 +0300135 size_t i, first = 0, last;
Jiri Pirko77b99002015-05-12 14:56:21 +0200136
Paul Blakey05cd2712018-04-30 14:28:30 +0300137 for (i = 0; i < size; i++) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200138 if (bytes[i]) {
Paul Blakey05cd2712018-04-30 14:28:30 +0300139 first = i;
140 break;
141 }
142 }
143 last = first;
144 for (i = size - 1; i != first; i--) {
145 if (bytes[i]) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200146 last = i;
Paul Blakey05cd2712018-04-30 14:28:30 +0300147 break;
Jiri Pirko77b99002015-05-12 14:56:21 +0200148 }
149 }
150 mask->range.start = rounddown(first, sizeof(long));
151 mask->range.end = roundup(last + 1, sizeof(long));
152}
153
154static void *fl_key_get_start(struct fl_flow_key *key,
155 const struct fl_flow_mask *mask)
156{
157 return (u8 *) key + mask->range.start;
158}
159
160static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
161 struct fl_flow_mask *mask)
162{
163 const long *lkey = fl_key_get_start(key, mask);
164 const long *lmask = fl_key_get_start(&mask->key, mask);
165 long *lmkey = fl_key_get_start(mkey, mask);
166 int i;
167
168 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
169 *lmkey++ = *lkey++ & *lmask++;
170}
171
Jiri Pirkob95ec7e2018-07-23 09:23:10 +0200172static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
173 struct fl_flow_mask *mask)
174{
175 const long *lmask = fl_key_get_start(&mask->key, mask);
176 const long *ltmplt;
177 int i;
178
179 if (!tmplt)
180 return true;
181 ltmplt = fl_key_get_start(&tmplt->mask, mask);
182 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
183 if (~*ltmplt++ & *lmask++)
184 return false;
185 }
186 return true;
187}
188
Jiri Pirko77b99002015-05-12 14:56:21 +0200189static void fl_clear_masked_range(struct fl_flow_key *key,
190 struct fl_flow_mask *mask)
191{
192 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
193}
194
Amritha Nambiar5c722992018-11-12 16:15:55 -0800195static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter,
196 struct fl_flow_key *key,
197 struct fl_flow_key *mkey)
198{
199 __be16 min_mask, max_mask, min_val, max_val;
200
201 min_mask = htons(filter->mask->key.tp_min.dst);
202 max_mask = htons(filter->mask->key.tp_max.dst);
203 min_val = htons(filter->key.tp_min.dst);
204 max_val = htons(filter->key.tp_max.dst);
205
206 if (min_mask && max_mask) {
207 if (htons(key->tp.dst) < min_val ||
208 htons(key->tp.dst) > max_val)
209 return false;
210
211 /* skb does not have min and max values */
212 mkey->tp_min.dst = filter->mkey.tp_min.dst;
213 mkey->tp_max.dst = filter->mkey.tp_max.dst;
214 }
215 return true;
216}
217
218static bool fl_range_port_src_cmp(struct cls_fl_filter *filter,
219 struct fl_flow_key *key,
220 struct fl_flow_key *mkey)
221{
222 __be16 min_mask, max_mask, min_val, max_val;
223
224 min_mask = htons(filter->mask->key.tp_min.src);
225 max_mask = htons(filter->mask->key.tp_max.src);
226 min_val = htons(filter->key.tp_min.src);
227 max_val = htons(filter->key.tp_max.src);
228
229 if (min_mask && max_mask) {
230 if (htons(key->tp.src) < min_val ||
231 htons(key->tp.src) > max_val)
232 return false;
233
234 /* skb does not have min and max values */
235 mkey->tp_min.src = filter->mkey.tp_min.src;
236 mkey->tp_max.src = filter->mkey.tp_max.src;
237 }
238 return true;
239}
240
241static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask,
242 struct fl_flow_key *mkey)
Paul Blakeya3308d82017-01-16 10:45:13 +0200243{
Paul Blakey05cd2712018-04-30 14:28:30 +0300244 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
245 mask->filter_ht_params);
Paul Blakeya3308d82017-01-16 10:45:13 +0200246}
247
Amritha Nambiar5c722992018-11-12 16:15:55 -0800248static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask,
249 struct fl_flow_key *mkey,
250 struct fl_flow_key *key)
251{
252 struct cls_fl_filter *filter, *f;
253
254 list_for_each_entry_rcu(filter, &mask->filters, list) {
255 if (!fl_range_port_dst_cmp(filter, key, mkey))
256 continue;
257
258 if (!fl_range_port_src_cmp(filter, key, mkey))
259 continue;
260
261 f = __fl_lookup(mask, mkey);
262 if (f)
263 return f;
264 }
265 return NULL;
266}
267
268static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
269 struct fl_flow_key *mkey,
270 struct fl_flow_key *key)
271{
272 if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE))
273 return fl_lookup_range(mask, mkey, key);
274
275 return __fl_lookup(mask, mkey);
276}
277
Paul Blakeye0ace682019-07-09 10:30:50 +0300278static u16 fl_ct_info_to_flower_map[] = {
279 [IP_CT_ESTABLISHED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
280 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
281 [IP_CT_RELATED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
282 TCA_FLOWER_KEY_CT_FLAGS_RELATED,
283 [IP_CT_ESTABLISHED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
284 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
285 [IP_CT_RELATED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
286 TCA_FLOWER_KEY_CT_FLAGS_RELATED,
287 [IP_CT_NEW] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
288 TCA_FLOWER_KEY_CT_FLAGS_NEW,
289};
290
Jiri Pirko77b99002015-05-12 14:56:21 +0200291static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
292 struct tcf_result *res)
293{
294 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
Jiri Pirko77b99002015-05-12 14:56:21 +0200295 struct fl_flow_key skb_mkey;
Paul Blakeye0ace682019-07-09 10:30:50 +0300296 struct fl_flow_key skb_key;
297 struct fl_flow_mask *mask;
298 struct cls_fl_filter *f;
Jiri Pirko77b99002015-05-12 14:56:21 +0200299
Paul Blakey05cd2712018-04-30 14:28:30 +0300300 list_for_each_entry_rcu(mask, &head->masks, list) {
301 fl_clear_masked_range(&skb_key, mask);
Amir Vadaie69985c2016-06-05 17:11:18 +0300302
Jiri Pirko8212ed72019-06-19 09:41:03 +0300303 skb_flow_dissect_meta(skb, &mask->dissector, &skb_key);
Paul Blakey05cd2712018-04-30 14:28:30 +0300304 /* skb_flow_dissect() does not set n_proto in case an unknown
305 * protocol, so do it rather here.
306 */
307 skb_key.basic.n_proto = skb->protocol;
308 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
Paul Blakeye0ace682019-07-09 10:30:50 +0300309 skb_flow_dissect_ct(skb, &mask->dissector, &skb_key,
310 fl_ct_info_to_flower_map,
311 ARRAY_SIZE(fl_ct_info_to_flower_map));
Paul Blakey05cd2712018-04-30 14:28:30 +0300312 skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
Amir Vadaibc3103f2016-09-08 16:23:47 +0300313
Paul Blakey05cd2712018-04-30 14:28:30 +0300314 fl_set_masked_key(&skb_mkey, &skb_key, mask);
Jiri Pirko77b99002015-05-12 14:56:21 +0200315
Amritha Nambiar5c722992018-11-12 16:15:55 -0800316 f = fl_lookup(mask, &skb_mkey, &skb_key);
Paul Blakey05cd2712018-04-30 14:28:30 +0300317 if (f && !tc_skip_sw(f->flags)) {
318 *res = f->res;
319 return tcf_exts_exec(skb, &f->exts, res);
320 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200321 }
322 return -1;
323}
324
325static int fl_init(struct tcf_proto *tp)
326{
327 struct cls_fl_head *head;
328
329 head = kzalloc(sizeof(*head), GFP_KERNEL);
330 if (!head)
331 return -ENOBUFS;
332
Vlad Buslov259e60f2019-03-21 15:17:39 +0200333 spin_lock_init(&head->masks_lock);
Paul Blakey05cd2712018-04-30 14:28:30 +0300334 INIT_LIST_HEAD_RCU(&head->masks);
Vlad Buslovc049d562019-04-24 09:53:31 +0300335 INIT_LIST_HEAD(&head->hw_filters);
Jiri Pirko77b99002015-05-12 14:56:21 +0200336 rcu_assign_pointer(tp->root, head);
Chris Mic15ab232017-08-30 02:31:58 -0400337 idr_init(&head->handle_idr);
Jiri Pirko77b99002015-05-12 14:56:21 +0200338
Paul Blakey05cd2712018-04-30 14:28:30 +0300339 return rhashtable_init(&head->ht, &mask_ht_params);
340}
341
Vlad Buslov99815f52019-06-13 17:54:04 +0300342static void fl_mask_free(struct fl_flow_mask *mask, bool mask_init_done)
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200343{
Vlad Buslov99815f52019-06-13 17:54:04 +0300344 /* temporary masks don't have their filters list and ht initialized */
345 if (mask_init_done) {
346 WARN_ON(!list_empty(&mask->filters));
347 rhashtable_destroy(&mask->ht);
348 }
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200349 kfree(mask);
350}
351
352static void fl_mask_free_work(struct work_struct *work)
353{
354 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
355 struct fl_flow_mask, rwork);
356
Vlad Buslov99815f52019-06-13 17:54:04 +0300357 fl_mask_free(mask, true);
358}
359
360static void fl_uninit_mask_free_work(struct work_struct *work)
361{
362 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
363 struct fl_flow_mask, rwork);
364
365 fl_mask_free(mask, false);
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200366}
367
Vlad Buslov99946772019-04-12 00:54:19 +0300368static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask)
Paul Blakey05cd2712018-04-30 14:28:30 +0300369{
Vlad Buslovf48ef4d2019-03-21 15:17:37 +0200370 if (!refcount_dec_and_test(&mask->refcnt))
Paul Blakey05cd2712018-04-30 14:28:30 +0300371 return false;
372
373 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
Vlad Buslov259e60f2019-03-21 15:17:39 +0200374
375 spin_lock(&head->masks_lock);
Paul Blakey05cd2712018-04-30 14:28:30 +0300376 list_del_rcu(&mask->list);
Vlad Buslov259e60f2019-03-21 15:17:39 +0200377 spin_unlock(&head->masks_lock);
378
Vlad Buslov99946772019-04-12 00:54:19 +0300379 tcf_queue_work(&mask->rwork, fl_mask_free_work);
Paul Blakey05cd2712018-04-30 14:28:30 +0300380
381 return true;
Jiri Pirko77b99002015-05-12 14:56:21 +0200382}
383
Vlad Buslovc049d562019-04-24 09:53:31 +0300384static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp)
385{
386 /* Flower classifier only changes root pointer during init and destroy.
387 * Users must obtain reference to tcf_proto instance before calling its
388 * API, so tp->root pointer is protected from concurrent call to
389 * fl_destroy() by reference counting.
390 */
391 return rcu_dereference_raw(tp->root);
392}
393
Cong Wang0dadc112017-11-06 13:47:24 -0800394static void __fl_destroy_filter(struct cls_fl_filter *f)
395{
396 tcf_exts_destroy(&f->exts);
397 tcf_exts_put_net(&f->exts);
398 kfree(f);
399}
400
Cong Wang0552c8a2017-10-26 18:24:33 -0700401static void fl_destroy_filter_work(struct work_struct *work)
402{
Cong Wangaaa908f2018-05-23 15:26:53 -0700403 struct cls_fl_filter *f = container_of(to_rcu_work(work),
404 struct cls_fl_filter, rwork);
Cong Wang0552c8a2017-10-26 18:24:33 -0700405
Cong Wang0dadc112017-11-06 13:47:24 -0800406 __fl_destroy_filter(f);
Cong Wang0552c8a2017-10-26 18:24:33 -0700407}
408
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800409static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200410 bool rtnl_held, struct netlink_ext_ack *extack)
Amir Vadai5b33f482016-03-08 12:42:29 +0200411{
Jiri Pirko208c0f42017-10-19 15:50:32 +0200412 struct tcf_block *block = tp->chain->block;
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200413 struct flow_cls_offload cls_flower = {};
Amir Vadai5b33f482016-03-08 12:42:29 +0200414
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200415 if (!rtnl_held)
416 rtnl_lock();
417
Pieter Jansen van Vuurend6787142019-05-06 17:24:21 -0700418 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200419 cls_flower.command = FLOW_CLS_DESTROY;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200420 cls_flower.cookie = (unsigned long) f;
Amir Vadai5b33f482016-03-08 12:42:29 +0200421
Vlad Buslov40119212019-08-26 16:44:59 +0300422 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, false,
423 &f->flags, &f->in_hw_count, true);
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200424
425 if (!rtnl_held)
426 rtnl_unlock();
Amir Vadai5b33f482016-03-08 12:42:29 +0200427}
428
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300429static int fl_hw_replace_filter(struct tcf_proto *tp,
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200430 struct cls_fl_filter *f, bool rtnl_held,
Quentin Monnet41002032018-01-19 17:44:43 -0800431 struct netlink_ext_ack *extack)
Amir Vadai5b33f482016-03-08 12:42:29 +0200432{
Jiri Pirko208c0f42017-10-19 15:50:32 +0200433 struct tcf_block *block = tp->chain->block;
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200434 struct flow_cls_offload cls_flower = {};
Jiri Pirko717503b2017-10-11 09:41:09 +0200435 bool skip_sw = tc_skip_sw(f->flags);
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200436 int err = 0;
437
438 if (!rtnl_held)
439 rtnl_lock();
Amir Vadai5b33f482016-03-08 12:42:29 +0200440
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100441 cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts));
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200442 if (!cls_flower.rule) {
443 err = -ENOMEM;
444 goto errout;
445 }
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +0100446
Pieter Jansen van Vuurend6787142019-05-06 17:24:21 -0700447 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200448 cls_flower.command = FLOW_CLS_REPLACE;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200449 cls_flower.cookie = (unsigned long) f;
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +0100450 cls_flower.rule->match.dissector = &f->mask->dissector;
451 cls_flower.rule->match.mask = &f->mask->key;
452 cls_flower.rule->match.key = &f->mkey;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700453 cls_flower.classid = f->res.classid;
Amir Vadai5b33f482016-03-08 12:42:29 +0200454
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +0100455 err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
456 if (err) {
457 kfree(cls_flower.rule);
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200458 if (skip_sw)
Vlad Buslov1f15bb42019-02-12 23:39:06 +0200459 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200460 else
461 err = 0;
462 goto errout;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +0100463 }
464
Vlad Buslov40119212019-08-26 16:44:59 +0300465 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSFLOWER, &cls_flower,
466 skip_sw, &f->flags, &f->in_hw_count, true);
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +0100467 kfree(cls_flower.rule);
468
Vlad Buslov40119212019-08-26 16:44:59 +0300469 if (err) {
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200470 fl_hw_destroy_filter(tp, f, true, NULL);
471 goto errout;
Jiri Pirko717503b2017-10-11 09:41:09 +0200472 }
473
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200474 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW)) {
475 err = -EINVAL;
476 goto errout;
477 }
Jiri Pirko717503b2017-10-11 09:41:09 +0200478
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200479errout:
480 if (!rtnl_held)
481 rtnl_unlock();
482
483 return err;
Amir Vadai5b33f482016-03-08 12:42:29 +0200484}
485
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200486static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f,
487 bool rtnl_held)
Amir Vadai10cbc682016-05-13 12:55:37 +0000488{
Jiri Pirko208c0f42017-10-19 15:50:32 +0200489 struct tcf_block *block = tp->chain->block;
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200490 struct flow_cls_offload cls_flower = {};
Amir Vadai10cbc682016-05-13 12:55:37 +0000491
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200492 if (!rtnl_held)
493 rtnl_lock();
494
Pieter Jansen van Vuurend6787142019-05-06 17:24:21 -0700495 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200496 cls_flower.command = FLOW_CLS_STATS;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200497 cls_flower.cookie = (unsigned long) f;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700498 cls_flower.classid = f->res.classid;
Amir Vadai10cbc682016-05-13 12:55:37 +0000499
Vlad Buslov40119212019-08-26 16:44:59 +0300500 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
Pablo Neira Ayuso3b1903e2019-02-02 12:50:47 +0100501
502 tcf_exts_stats_update(&f->exts, cls_flower.stats.bytes,
503 cls_flower.stats.pkts,
504 cls_flower.stats.lastused);
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200505
506 if (!rtnl_held)
507 rtnl_unlock();
Amir Vadai10cbc682016-05-13 12:55:37 +0000508}
509
Vlad Buslov06177552019-03-21 15:17:35 +0200510static void __fl_put(struct cls_fl_filter *f)
511{
512 if (!refcount_dec_and_test(&f->refcnt))
513 return;
514
515 if (tcf_exts_get_net(&f->exts))
516 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
517 else
518 __fl_destroy_filter(f);
519}
520
521static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle)
522{
523 struct cls_fl_filter *f;
524
525 rcu_read_lock();
526 f = idr_find(&head->handle_idr, handle);
527 if (f && !refcount_inc_not_zero(&f->refcnt))
528 f = NULL;
529 rcu_read_unlock();
530
531 return f;
532}
533
Vlad Buslovb2552b82019-03-21 15:17:36 +0200534static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200535 bool *last, bool rtnl_held,
536 struct netlink_ext_ack *extack)
Roi Dayan13fa8762016-11-01 16:08:29 +0200537{
Vlad Buslove4746192019-03-21 15:17:33 +0200538 struct cls_fl_head *head = fl_head_dereference(tp);
Chris Mic15ab232017-08-30 02:31:58 -0400539
Vlad Buslovb2552b82019-03-21 15:17:36 +0200540 *last = false;
541
Vlad Buslov3d81e712019-03-21 15:17:42 +0200542 spin_lock(&tp->lock);
543 if (f->deleted) {
544 spin_unlock(&tp->lock);
Vlad Buslovb2552b82019-03-21 15:17:36 +0200545 return -ENOENT;
Vlad Buslov3d81e712019-03-21 15:17:42 +0200546 }
Vlad Buslovb2552b82019-03-21 15:17:36 +0200547
548 f->deleted = true;
549 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
550 f->mask->filter_ht_params);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500551 idr_remove(&head->handle_idr, f->handle);
Roi Dayan13fa8762016-11-01 16:08:29 +0200552 list_del_rcu(&f->list);
Vlad Buslov3d81e712019-03-21 15:17:42 +0200553 spin_unlock(&tp->lock);
554
Vlad Buslov99946772019-04-12 00:54:19 +0300555 *last = fl_mask_put(head, f->mask);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200556 if (!tc_skip_hw(f->flags))
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200557 fl_hw_destroy_filter(tp, f, rtnl_held, extack);
Roi Dayan13fa8762016-11-01 16:08:29 +0200558 tcf_unbind_filter(tp, &f->res);
Vlad Buslov06177552019-03-21 15:17:35 +0200559 __fl_put(f);
Paul Blakey05cd2712018-04-30 14:28:30 +0300560
Vlad Buslovb2552b82019-03-21 15:17:36 +0200561 return 0;
Roi Dayan13fa8762016-11-01 16:08:29 +0200562}
563
Daniel Borkmannd9363772016-11-27 01:18:01 +0100564static void fl_destroy_sleepable(struct work_struct *work)
565{
Cong Wangaaa908f2018-05-23 15:26:53 -0700566 struct cls_fl_head *head = container_of(to_rcu_work(work),
567 struct cls_fl_head,
568 rwork);
Paul Blakeyde9dc652018-06-03 10:06:13 +0300569
570 rhashtable_destroy(&head->ht);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100571 kfree(head);
572 module_put(THIS_MODULE);
573}
574
Vlad Buslov12db03b2019-02-11 10:55:45 +0200575static void fl_destroy(struct tcf_proto *tp, bool rtnl_held,
576 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200577{
Vlad Buslove4746192019-03-21 15:17:33 +0200578 struct cls_fl_head *head = fl_head_dereference(tp);
Paul Blakey05cd2712018-04-30 14:28:30 +0300579 struct fl_flow_mask *mask, *next_mask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200580 struct cls_fl_filter *f, *next;
Vlad Buslovb2552b82019-03-21 15:17:36 +0200581 bool last;
Jiri Pirko77b99002015-05-12 14:56:21 +0200582
Paul Blakey05cd2712018-04-30 14:28:30 +0300583 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
584 list_for_each_entry_safe(f, next, &mask->filters, list) {
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200585 __fl_delete(tp, f, &last, rtnl_held, extack);
Vlad Buslovb2552b82019-03-21 15:17:36 +0200586 if (last)
Paul Blakey05cd2712018-04-30 14:28:30 +0300587 break;
588 }
589 }
Chris Mic15ab232017-08-30 02:31:58 -0400590 idr_destroy(&head->handle_idr);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100591
592 __module_get(THIS_MODULE);
Cong Wangaaa908f2018-05-23 15:26:53 -0700593 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
Jiri Pirko77b99002015-05-12 14:56:21 +0200594}
595
Vlad Buslov06177552019-03-21 15:17:35 +0200596static void fl_put(struct tcf_proto *tp, void *arg)
597{
598 struct cls_fl_filter *f = arg;
599
600 __fl_put(f);
601}
602
WANG Cong8113c092017-08-04 21:31:43 -0700603static void *fl_get(struct tcf_proto *tp, u32 handle)
Jiri Pirko77b99002015-05-12 14:56:21 +0200604{
Vlad Buslove4746192019-03-21 15:17:33 +0200605 struct cls_fl_head *head = fl_head_dereference(tp);
Jiri Pirko77b99002015-05-12 14:56:21 +0200606
Vlad Buslov06177552019-03-21 15:17:35 +0200607 return __fl_get(head, handle);
Jiri Pirko77b99002015-05-12 14:56:21 +0200608}
609
610static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
611 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
612 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
613 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
614 .len = IFNAMSIZ },
615 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
616 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
617 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
618 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
619 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
620 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
621 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
622 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
623 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
624 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
625 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
626 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
627 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
628 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
629 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
630 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
Jamal Hadi Salimb175c3a2015-06-25 06:55:27 -0400631 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
632 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300633 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
634 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
635 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
Amir Vadaibc3103f2016-09-08 16:23:47 +0300636 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
637 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
638 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
639 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
640 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
641 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
642 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
643 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
644 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
Or Gerlitzaa72d702016-09-15 15:28:22 +0300645 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
646 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
647 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
648 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
Simon Horman5976c5f2016-11-03 13:24:21 +0100649 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
650 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
651 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
652 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200653 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
654 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
655 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
656 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200657 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
658 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
Simon Horman7b684882016-12-07 13:48:28 +0100659 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
660 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
661 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
662 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
663 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
664 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
665 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
666 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
Simon Horman99d31322017-01-11 14:05:43 +0100667 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
668 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
669 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
670 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
671 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
672 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
673 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
674 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
675 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
676 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400677 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
678 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
679 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
680 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200681 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
682 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300683 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
684 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
685 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
686 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
Jianbo Liud64efd02018-07-06 05:38:16 +0000687 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
688 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
689 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300690 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
691 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
692 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
693 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200694 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
695 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
Paul Blakeye0ace682019-07-09 10:30:50 +0300696 [TCA_FLOWER_KEY_CT_STATE] = { .type = NLA_U16 },
697 [TCA_FLOWER_KEY_CT_STATE_MASK] = { .type = NLA_U16 },
698 [TCA_FLOWER_KEY_CT_ZONE] = { .type = NLA_U16 },
699 [TCA_FLOWER_KEY_CT_ZONE_MASK] = { .type = NLA_U16 },
700 [TCA_FLOWER_KEY_CT_MARK] = { .type = NLA_U32 },
701 [TCA_FLOWER_KEY_CT_MARK_MASK] = { .type = NLA_U32 },
702 [TCA_FLOWER_KEY_CT_LABELS] = { .type = NLA_BINARY,
703 .len = 128 / BITS_PER_BYTE },
704 [TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NLA_BINARY,
705 .len = 128 / BITS_PER_BYTE },
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200706};
707
708static const struct nla_policy
709enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
710 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
711};
712
713static const struct nla_policy
714geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
715 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
716 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
717 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
718 .len = 128 },
Jiri Pirko77b99002015-05-12 14:56:21 +0200719};
720
721static void fl_set_key_val(struct nlattr **tb,
722 void *val, int val_type,
723 void *mask, int mask_type, int len)
724{
725 if (!tb[val_type])
726 return;
Paul Blakeye0ace682019-07-09 10:30:50 +0300727 nla_memcpy(val, tb[val_type], len);
Jiri Pirko77b99002015-05-12 14:56:21 +0200728 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
729 memset(mask, 0xff, len);
730 else
Paul Blakeye0ace682019-07-09 10:30:50 +0300731 nla_memcpy(mask, tb[mask_type], len);
Jiri Pirko77b99002015-05-12 14:56:21 +0200732}
733
Amritha Nambiar5c722992018-11-12 16:15:55 -0800734static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
735 struct fl_flow_key *mask)
736{
737 fl_set_key_val(tb, &key->tp_min.dst,
738 TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_min.dst,
739 TCA_FLOWER_UNSPEC, sizeof(key->tp_min.dst));
740 fl_set_key_val(tb, &key->tp_max.dst,
741 TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_max.dst,
742 TCA_FLOWER_UNSPEC, sizeof(key->tp_max.dst));
743 fl_set_key_val(tb, &key->tp_min.src,
744 TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_min.src,
745 TCA_FLOWER_UNSPEC, sizeof(key->tp_min.src));
746 fl_set_key_val(tb, &key->tp_max.src,
747 TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_max.src,
748 TCA_FLOWER_UNSPEC, sizeof(key->tp_max.src));
749
750 if ((mask->tp_min.dst && mask->tp_max.dst &&
751 htons(key->tp_max.dst) <= htons(key->tp_min.dst)) ||
752 (mask->tp_min.src && mask->tp_max.src &&
753 htons(key->tp_max.src) <= htons(key->tp_min.src)))
754 return -EINVAL;
755
756 return 0;
757}
758
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400759static int fl_set_key_mpls(struct nlattr **tb,
760 struct flow_dissector_key_mpls *key_val,
761 struct flow_dissector_key_mpls *key_mask)
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400762{
763 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
764 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
765 key_mask->mpls_ttl = MPLS_TTL_MASK;
766 }
767 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400768 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
769
770 if (bos & ~MPLS_BOS_MASK)
771 return -EINVAL;
772 key_val->mpls_bos = bos;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400773 key_mask->mpls_bos = MPLS_BOS_MASK;
774 }
775 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400776 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
777
778 if (tc & ~MPLS_TC_MASK)
779 return -EINVAL;
780 key_val->mpls_tc = tc;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400781 key_mask->mpls_tc = MPLS_TC_MASK;
782 }
783 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400784 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
785
786 if (label & ~MPLS_LABEL_MASK)
787 return -EINVAL;
788 key_val->mpls_label = label;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400789 key_mask->mpls_label = MPLS_LABEL_MASK;
790 }
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400791 return 0;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400792}
793
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300794static void fl_set_key_vlan(struct nlattr **tb,
Jianbo Liuaaab0832018-07-06 05:38:13 +0000795 __be16 ethertype,
Jianbo Liud64efd02018-07-06 05:38:16 +0000796 int vlan_id_key, int vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300797 struct flow_dissector_key_vlan *key_val,
798 struct flow_dissector_key_vlan *key_mask)
799{
800#define VLAN_PRIORITY_MASK 0x7
801
Jianbo Liud64efd02018-07-06 05:38:16 +0000802 if (tb[vlan_id_key]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300803 key_val->vlan_id =
Jianbo Liud64efd02018-07-06 05:38:16 +0000804 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300805 key_mask->vlan_id = VLAN_VID_MASK;
806 }
Jianbo Liud64efd02018-07-06 05:38:16 +0000807 if (tb[vlan_prio_key]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300808 key_val->vlan_priority =
Jianbo Liud64efd02018-07-06 05:38:16 +0000809 nla_get_u8(tb[vlan_prio_key]) &
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300810 VLAN_PRIORITY_MASK;
811 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
812 }
Jianbo Liuaaab0832018-07-06 05:38:13 +0000813 key_val->vlan_tpid = ethertype;
814 key_mask->vlan_tpid = cpu_to_be16(~0);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300815}
816
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200817static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
818 u32 *dissector_key, u32 *dissector_mask,
819 u32 flower_flag_bit, u32 dissector_flag_bit)
820{
821 if (flower_mask & flower_flag_bit) {
822 *dissector_mask |= dissector_flag_bit;
823 if (flower_key & flower_flag_bit)
824 *dissector_key |= dissector_flag_bit;
825 }
826}
827
Or Gerlitzd9724772016-12-22 14:28:15 +0200828static int fl_set_key_flags(struct nlattr **tb,
829 u32 *flags_key, u32 *flags_mask)
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200830{
831 u32 key, mask;
832
Or Gerlitzd9724772016-12-22 14:28:15 +0200833 /* mask is mandatory for flags */
834 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
835 return -EINVAL;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200836
837 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
Or Gerlitzd9724772016-12-22 14:28:15 +0200838 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200839
840 *flags_key = 0;
841 *flags_mask = 0;
842
843 fl_set_key_flag(key, mask, flags_key, flags_mask,
844 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +0100845 fl_set_key_flag(key, mask, flags_key, flags_mask,
846 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
847 FLOW_DIS_FIRST_FRAG);
Or Gerlitzd9724772016-12-22 14:28:15 +0200848
849 return 0;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200850}
851
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300852static void fl_set_key_ip(struct nlattr **tb, bool encap,
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300853 struct flow_dissector_key_ip *key,
854 struct flow_dissector_key_ip *mask)
855{
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300856 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
857 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
858 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
859 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300860
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300861 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
862 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300863}
864
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200865static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
866 int depth, int option_len,
867 struct netlink_ext_ack *extack)
868{
869 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
870 struct nlattr *class = NULL, *type = NULL, *data = NULL;
871 struct geneve_opt *opt;
872 int err, data_len = 0;
873
874 if (option_len > sizeof(struct geneve_opt))
875 data_len = option_len - sizeof(struct geneve_opt);
876
877 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
878 memset(opt, 0xff, option_len);
879 opt->length = data_len / 4;
880 opt->r1 = 0;
881 opt->r2 = 0;
882 opt->r3 = 0;
883
884 /* If no mask has been prodived we assume an exact match. */
885 if (!depth)
886 return sizeof(struct geneve_opt) + data_len;
887
888 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
889 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
890 return -EINVAL;
891 }
892
Johannes Berg8cb08172019-04-26 14:07:28 +0200893 err = nla_parse_nested_deprecated(tb,
894 TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
895 nla, geneve_opt_policy, extack);
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200896 if (err < 0)
897 return err;
898
899 /* We are not allowed to omit any of CLASS, TYPE or DATA
900 * fields from the key.
901 */
902 if (!option_len &&
903 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
904 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
905 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
906 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
907 return -EINVAL;
908 }
909
910 /* Omitting any of CLASS, TYPE or DATA fields is allowed
911 * for the mask.
912 */
913 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
914 int new_len = key->enc_opts.len;
915
916 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
917 data_len = nla_len(data);
918 if (data_len < 4) {
919 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
920 return -ERANGE;
921 }
922 if (data_len % 4) {
923 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
924 return -ERANGE;
925 }
926
927 new_len += sizeof(struct geneve_opt) + data_len;
928 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
929 if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
930 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
931 return -ERANGE;
932 }
933 opt->length = data_len / 4;
934 memcpy(opt->opt_data, nla_data(data), data_len);
935 }
936
937 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
938 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
939 opt->opt_class = nla_get_be16(class);
940 }
941
942 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
943 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
944 opt->type = nla_get_u8(type);
945 }
946
947 return sizeof(struct geneve_opt) + data_len;
948}
949
950static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
951 struct fl_flow_key *mask,
952 struct netlink_ext_ack *extack)
953{
954 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
Jakub Kicinski63c82992018-11-09 21:06:26 -0800955 int err, option_len, key_depth, msk_depth = 0;
956
Johannes Berg8cb08172019-04-26 14:07:28 +0200957 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS],
958 TCA_FLOWER_KEY_ENC_OPTS_MAX,
959 enc_opts_policy, extack);
Jakub Kicinski63c82992018-11-09 21:06:26 -0800960 if (err)
961 return err;
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200962
963 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
964
965 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
Johannes Berg8cb08172019-04-26 14:07:28 +0200966 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
967 TCA_FLOWER_KEY_ENC_OPTS_MAX,
968 enc_opts_policy, extack);
Jakub Kicinski63c82992018-11-09 21:06:26 -0800969 if (err)
970 return err;
971
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200972 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
973 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
974 }
975
976 nla_for_each_attr(nla_opt_key, nla_enc_key,
977 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
978 switch (nla_type(nla_opt_key)) {
979 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
980 option_len = 0;
981 key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
982 option_len = fl_set_geneve_opt(nla_opt_key, key,
983 key_depth, option_len,
984 extack);
985 if (option_len < 0)
986 return option_len;
987
988 key->enc_opts.len += option_len;
989 /* At the same time we need to parse through the mask
990 * in order to verify exact and mask attribute lengths.
991 */
992 mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
993 option_len = fl_set_geneve_opt(nla_opt_msk, mask,
994 msk_depth, option_len,
995 extack);
996 if (option_len < 0)
997 return option_len;
998
999 mask->enc_opts.len += option_len;
1000 if (key->enc_opts.len != mask->enc_opts.len) {
1001 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1002 return -EINVAL;
1003 }
1004
1005 if (msk_depth)
1006 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
1007 break;
1008 default:
1009 NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
1010 return -EINVAL;
1011 }
1012 }
1013
1014 return 0;
1015}
1016
Paul Blakeye0ace682019-07-09 10:30:50 +03001017static int fl_set_key_ct(struct nlattr **tb,
1018 struct flow_dissector_key_ct *key,
1019 struct flow_dissector_key_ct *mask,
1020 struct netlink_ext_ack *extack)
1021{
1022 if (tb[TCA_FLOWER_KEY_CT_STATE]) {
1023 if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) {
1024 NL_SET_ERR_MSG(extack, "Conntrack isn't enabled");
1025 return -EOPNOTSUPP;
1026 }
1027 fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
1028 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
1029 sizeof(key->ct_state));
1030 }
1031 if (tb[TCA_FLOWER_KEY_CT_ZONE]) {
1032 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1033 NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled");
1034 return -EOPNOTSUPP;
1035 }
1036 fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
1037 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
1038 sizeof(key->ct_zone));
1039 }
1040 if (tb[TCA_FLOWER_KEY_CT_MARK]) {
1041 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1042 NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled");
1043 return -EOPNOTSUPP;
1044 }
1045 fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
1046 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
1047 sizeof(key->ct_mark));
1048 }
1049 if (tb[TCA_FLOWER_KEY_CT_LABELS]) {
1050 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1051 NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled");
1052 return -EOPNOTSUPP;
1053 }
1054 fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
1055 mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
1056 sizeof(key->ct_labels));
1057 }
1058
1059 return 0;
1060}
1061
Jiri Pirko77b99002015-05-12 14:56:21 +02001062static int fl_set_key(struct net *net, struct nlattr **tb,
Alexander Aring1057c552018-01-18 11:20:54 -05001063 struct fl_flow_key *key, struct fl_flow_key *mask,
1064 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001065{
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001066 __be16 ethertype;
Or Gerlitzd9724772016-12-22 14:28:15 +02001067 int ret = 0;
Jiri Pirkoa5148622019-06-15 11:03:49 +02001068
Jiri Pirko77b99002015-05-12 14:56:21 +02001069 if (tb[TCA_FLOWER_INDEV]) {
Alexander Aring1057c552018-01-18 11:20:54 -05001070 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
Jiri Pirko77b99002015-05-12 14:56:21 +02001071 if (err < 0)
1072 return err;
Jiri Pirko8212ed72019-06-19 09:41:03 +03001073 key->meta.ingress_ifindex = err;
1074 mask->meta.ingress_ifindex = 0xffffffff;
Jiri Pirko77b99002015-05-12 14:56:21 +02001075 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001076
1077 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1078 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1079 sizeof(key->eth.dst));
1080 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1081 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1082 sizeof(key->eth.src));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -05001083
Arnd Bergmann0b498a52016-08-26 17:25:45 +02001084 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001085 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
1086
Jianbo Liuaaab0832018-07-06 05:38:13 +00001087 if (eth_type_vlan(ethertype)) {
Jianbo Liud64efd02018-07-06 05:38:16 +00001088 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
1089 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan,
1090 &mask->vlan);
1091
Jianbo Liu5e9a0fe2018-07-09 02:26:20 +00001092 if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
1093 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
1094 if (eth_type_vlan(ethertype)) {
1095 fl_set_key_vlan(tb, ethertype,
1096 TCA_FLOWER_KEY_CVLAN_ID,
1097 TCA_FLOWER_KEY_CVLAN_PRIO,
1098 &key->cvlan, &mask->cvlan);
1099 fl_set_key_val(tb, &key->basic.n_proto,
1100 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1101 &mask->basic.n_proto,
1102 TCA_FLOWER_UNSPEC,
1103 sizeof(key->basic.n_proto));
1104 } else {
1105 key->basic.n_proto = ethertype;
1106 mask->basic.n_proto = cpu_to_be16(~0);
1107 }
Jianbo Liud64efd02018-07-06 05:38:16 +00001108 }
Arnd Bergmann0b498a52016-08-26 17:25:45 +02001109 } else {
1110 key->basic.n_proto = ethertype;
1111 mask->basic.n_proto = cpu_to_be16(~0);
1112 }
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001113 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -05001114
Jiri Pirko77b99002015-05-12 14:56:21 +02001115 if (key->basic.n_proto == htons(ETH_P_IP) ||
1116 key->basic.n_proto == htons(ETH_P_IPV6)) {
1117 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1118 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1119 sizeof(key->basic.ip_proto));
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001120 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
Jiri Pirko77b99002015-05-12 14:56:21 +02001121 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -05001122
1123 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
1124 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +02001125 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +02001126 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1127 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1128 sizeof(key->ipv4.src));
1129 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1130 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1131 sizeof(key->ipv4.dst));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -05001132 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
1133 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +02001134 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +02001135 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1136 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1137 sizeof(key->ipv6.src));
1138 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1139 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1140 sizeof(key->ipv6.dst));
1141 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -05001142
Jiri Pirko77b99002015-05-12 14:56:21 +02001143 if (key->basic.ip_proto == IPPROTO_TCP) {
1144 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001145 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001146 sizeof(key->tp.src));
1147 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001148 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001149 sizeof(key->tp.dst));
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001150 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1151 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1152 sizeof(key->tcp.flags));
Jiri Pirko77b99002015-05-12 14:56:21 +02001153 } else if (key->basic.ip_proto == IPPROTO_UDP) {
1154 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001155 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001156 sizeof(key->tp.src));
1157 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001158 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001159 sizeof(key->tp.dst));
Simon Horman5976c5f2016-11-03 13:24:21 +01001160 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
1161 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1162 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1163 sizeof(key->tp.src));
1164 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1165 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1166 sizeof(key->tp.dst));
Simon Horman7b684882016-12-07 13:48:28 +01001167 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
1168 key->basic.ip_proto == IPPROTO_ICMP) {
1169 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
1170 &mask->icmp.type,
1171 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1172 sizeof(key->icmp.type));
1173 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
1174 &mask->icmp.code,
1175 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1176 sizeof(key->icmp.code));
1177 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1178 key->basic.ip_proto == IPPROTO_ICMPV6) {
1179 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
1180 &mask->icmp.type,
1181 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1182 sizeof(key->icmp.type));
Simon Horman040587a2017-01-30 16:19:02 +01001183 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
Simon Horman7b684882016-12-07 13:48:28 +01001184 &mask->icmp.code,
Simon Horman040587a2017-01-30 16:19:02 +01001185 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
Simon Horman7b684882016-12-07 13:48:28 +01001186 sizeof(key->icmp.code));
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001187 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
1188 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -04001189 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
1190 if (ret)
1191 return ret;
Simon Horman99d31322017-01-11 14:05:43 +01001192 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
1193 key->basic.n_proto == htons(ETH_P_RARP)) {
1194 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
1195 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
1196 sizeof(key->arp.sip));
1197 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
1198 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
1199 sizeof(key->arp.tip));
1200 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
1201 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
1202 sizeof(key->arp.op));
1203 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1204 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1205 sizeof(key->arp.sha));
1206 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1207 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1208 sizeof(key->arp.tha));
Jiri Pirko77b99002015-05-12 14:56:21 +02001209 }
1210
Amritha Nambiar5c722992018-11-12 16:15:55 -08001211 if (key->basic.ip_proto == IPPROTO_TCP ||
1212 key->basic.ip_proto == IPPROTO_UDP ||
1213 key->basic.ip_proto == IPPROTO_SCTP) {
1214 ret = fl_set_key_port_range(tb, key, mask);
1215 if (ret)
1216 return ret;
1217 }
1218
Amir Vadaibc3103f2016-09-08 16:23:47 +03001219 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
1220 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
1221 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +02001222 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +03001223 fl_set_key_val(tb, &key->enc_ipv4.src,
1224 TCA_FLOWER_KEY_ENC_IPV4_SRC,
1225 &mask->enc_ipv4.src,
1226 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1227 sizeof(key->enc_ipv4.src));
1228 fl_set_key_val(tb, &key->enc_ipv4.dst,
1229 TCA_FLOWER_KEY_ENC_IPV4_DST,
1230 &mask->enc_ipv4.dst,
1231 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1232 sizeof(key->enc_ipv4.dst));
1233 }
1234
1235 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
1236 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
1237 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +02001238 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +03001239 fl_set_key_val(tb, &key->enc_ipv6.src,
1240 TCA_FLOWER_KEY_ENC_IPV6_SRC,
1241 &mask->enc_ipv6.src,
1242 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1243 sizeof(key->enc_ipv6.src));
1244 fl_set_key_val(tb, &key->enc_ipv6.dst,
1245 TCA_FLOWER_KEY_ENC_IPV6_DST,
1246 &mask->enc_ipv6.dst,
1247 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1248 sizeof(key->enc_ipv6.dst));
1249 }
1250
1251 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001252 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
Amir Vadaibc3103f2016-09-08 16:23:47 +03001253 sizeof(key->enc_key_id.keyid));
1254
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001255 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1256 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1257 sizeof(key->enc_tp.src));
1258
1259 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1260 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1261 sizeof(key->enc_tp.dst));
1262
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001263 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
1264
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001265 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
1266 ret = fl_set_enc_opt(tb, key, mask, extack);
1267 if (ret)
1268 return ret;
1269 }
1270
Paul Blakeye0ace682019-07-09 10:30:50 +03001271 ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack);
1272 if (ret)
1273 return ret;
1274
Or Gerlitzd9724772016-12-22 14:28:15 +02001275 if (tb[TCA_FLOWER_KEY_FLAGS])
1276 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001277
Or Gerlitzd9724772016-12-22 14:28:15 +02001278 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +02001279}
1280
Paul Blakey05cd2712018-04-30 14:28:30 +03001281static void fl_mask_copy(struct fl_flow_mask *dst,
1282 struct fl_flow_mask *src)
Jiri Pirko77b99002015-05-12 14:56:21 +02001283{
Paul Blakey05cd2712018-04-30 14:28:30 +03001284 const void *psrc = fl_key_get_start(&src->key, src);
1285 void *pdst = fl_key_get_start(&dst->key, src);
Jiri Pirko77b99002015-05-12 14:56:21 +02001286
Paul Blakey05cd2712018-04-30 14:28:30 +03001287 memcpy(pdst, psrc, fl_mask_range(src));
1288 dst->range = src->range;
Jiri Pirko77b99002015-05-12 14:56:21 +02001289}
1290
1291static const struct rhashtable_params fl_ht_params = {
1292 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
1293 .head_offset = offsetof(struct cls_fl_filter, ht_node),
1294 .automatic_shrinking = true,
1295};
1296
Paul Blakey05cd2712018-04-30 14:28:30 +03001297static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +02001298{
Paul Blakey05cd2712018-04-30 14:28:30 +03001299 mask->filter_ht_params = fl_ht_params;
1300 mask->filter_ht_params.key_len = fl_mask_range(mask);
1301 mask->filter_ht_params.key_offset += mask->range.start;
Jiri Pirko77b99002015-05-12 14:56:21 +02001302
Paul Blakey05cd2712018-04-30 14:28:30 +03001303 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
Jiri Pirko77b99002015-05-12 14:56:21 +02001304}
1305
1306#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
zhong jiangcb205a82018-09-19 19:32:11 +08001307#define FL_KEY_MEMBER_SIZE(member) FIELD_SIZEOF(struct fl_flow_key, member)
Jiri Pirko77b99002015-05-12 14:56:21 +02001308
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001309#define FL_KEY_IS_MASKED(mask, member) \
1310 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
1311 0, FL_KEY_MEMBER_SIZE(member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +02001312
1313#define FL_KEY_SET(keys, cnt, id, member) \
1314 do { \
1315 keys[cnt].key_id = id; \
1316 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
1317 cnt++; \
1318 } while(0);
1319
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001320#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
Jiri Pirko77b99002015-05-12 14:56:21 +02001321 do { \
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001322 if (FL_KEY_IS_MASKED(mask, member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +02001323 FL_KEY_SET(keys, cnt, id, member); \
1324 } while(0);
1325
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001326static void fl_init_dissector(struct flow_dissector *dissector,
1327 struct fl_flow_key *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +02001328{
1329 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
1330 size_t cnt = 0;
1331
Jiri Pirko8212ed72019-06-19 09:41:03 +03001332 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1333 FLOW_DISSECTOR_KEY_META, meta);
Tom Herbert42aecaa2015-06-04 09:16:39 -07001334 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
Jiri Pirko77b99002015-05-12 14:56:21 +02001335 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001336 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001337 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001338 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001339 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001340 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001341 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
Amritha Nambiar5c722992018-11-12 16:15:55 -08001342 if (FL_KEY_IS_MASKED(mask, tp) ||
1343 FL_KEY_IS_MASKED(mask, tp_min) || FL_KEY_IS_MASKED(mask, tp_max))
1344 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_PORTS, tp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001345 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001346 FLOW_DISSECTOR_KEY_IP, ip);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001347 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001348 FLOW_DISSECTOR_KEY_TCP, tcp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001349 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Simon Horman7b684882016-12-07 13:48:28 +01001350 FLOW_DISSECTOR_KEY_ICMP, icmp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001351 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Simon Horman99d31322017-01-11 14:05:43 +01001352 FLOW_DISSECTOR_KEY_ARP, arp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001353 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001354 FLOW_DISSECTOR_KEY_MPLS, mpls);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001355 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001356 FLOW_DISSECTOR_KEY_VLAN, vlan);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001357 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Jianbo Liud64efd02018-07-06 05:38:16 +00001358 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001359 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +02001360 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001361 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +02001362 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001363 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +02001364 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001365 if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
1366 FL_KEY_IS_MASKED(mask, enc_ipv6))
Hadar Hen Zion519d1052016-11-07 15:14:38 +02001367 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
1368 enc_control);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001369 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001370 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001371 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001372 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001373 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1374 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
Paul Blakeye0ace682019-07-09 10:30:50 +03001375 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1376 FLOW_DISSECTOR_KEY_CT, ct);
Jiri Pirko77b99002015-05-12 14:56:21 +02001377
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001378 skb_flow_dissector_init(dissector, keys, cnt);
Paul Blakey05cd2712018-04-30 14:28:30 +03001379}
1380
1381static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
1382 struct fl_flow_mask *mask)
1383{
1384 struct fl_flow_mask *newmask;
1385 int err;
1386
1387 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
1388 if (!newmask)
1389 return ERR_PTR(-ENOMEM);
1390
1391 fl_mask_copy(newmask, mask);
1392
Amritha Nambiar5c722992018-11-12 16:15:55 -08001393 if ((newmask->key.tp_min.dst && newmask->key.tp_max.dst) ||
1394 (newmask->key.tp_min.src && newmask->key.tp_max.src))
1395 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
1396
Paul Blakey05cd2712018-04-30 14:28:30 +03001397 err = fl_init_mask_hashtable(newmask);
1398 if (err)
1399 goto errout_free;
1400
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001401 fl_init_dissector(&newmask->dissector, &newmask->key);
Paul Blakey05cd2712018-04-30 14:28:30 +03001402
1403 INIT_LIST_HEAD_RCU(&newmask->filters);
1404
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001405 refcount_set(&newmask->refcnt, 1);
Vlad Buslov195c2342019-03-21 15:17:38 +02001406 err = rhashtable_replace_fast(&head->ht, &mask->ht_node,
1407 &newmask->ht_node, mask_ht_params);
Paul Blakey05cd2712018-04-30 14:28:30 +03001408 if (err)
1409 goto errout_destroy;
1410
Vlad Buslov259e60f2019-03-21 15:17:39 +02001411 spin_lock(&head->masks_lock);
Paul Blakey05cd2712018-04-30 14:28:30 +03001412 list_add_tail_rcu(&newmask->list, &head->masks);
Vlad Buslov259e60f2019-03-21 15:17:39 +02001413 spin_unlock(&head->masks_lock);
Paul Blakey05cd2712018-04-30 14:28:30 +03001414
1415 return newmask;
1416
1417errout_destroy:
1418 rhashtable_destroy(&newmask->ht);
1419errout_free:
1420 kfree(newmask);
1421
1422 return ERR_PTR(err);
Jiri Pirko77b99002015-05-12 14:56:21 +02001423}
1424
1425static int fl_check_assign_mask(struct cls_fl_head *head,
Paul Blakey05cd2712018-04-30 14:28:30 +03001426 struct cls_fl_filter *fnew,
1427 struct cls_fl_filter *fold,
Jiri Pirko77b99002015-05-12 14:56:21 +02001428 struct fl_flow_mask *mask)
1429{
Paul Blakey05cd2712018-04-30 14:28:30 +03001430 struct fl_flow_mask *newmask;
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001431 int ret = 0;
Jiri Pirko77b99002015-05-12 14:56:21 +02001432
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001433 rcu_read_lock();
Vlad Buslov195c2342019-03-21 15:17:38 +02001434
1435 /* Insert mask as temporary node to prevent concurrent creation of mask
1436 * with same key. Any concurrent lookups with same key will return
Vlad Buslov99815f52019-06-13 17:54:04 +03001437 * -EAGAIN because mask's refcnt is zero.
Vlad Buslov195c2342019-03-21 15:17:38 +02001438 */
1439 fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht,
1440 &mask->ht_node,
1441 mask_ht_params);
Paul Blakey05cd2712018-04-30 14:28:30 +03001442 if (!fnew->mask) {
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001443 rcu_read_unlock();
1444
Vlad Buslov195c2342019-03-21 15:17:38 +02001445 if (fold) {
1446 ret = -EINVAL;
1447 goto errout_cleanup;
1448 }
Paul Blakey05cd2712018-04-30 14:28:30 +03001449
1450 newmask = fl_create_new_mask(head, mask);
Vlad Buslov195c2342019-03-21 15:17:38 +02001451 if (IS_ERR(newmask)) {
1452 ret = PTR_ERR(newmask);
1453 goto errout_cleanup;
1454 }
Paul Blakey05cd2712018-04-30 14:28:30 +03001455
1456 fnew->mask = newmask;
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001457 return 0;
Vlad Buslov195c2342019-03-21 15:17:38 +02001458 } else if (IS_ERR(fnew->mask)) {
1459 ret = PTR_ERR(fnew->mask);
Paul Blakeyf6521c52018-06-03 10:06:14 +03001460 } else if (fold && fold->mask != fnew->mask) {
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001461 ret = -EINVAL;
1462 } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) {
1463 /* Mask was deleted concurrently, try again */
1464 ret = -EAGAIN;
Jiri Pirko77b99002015-05-12 14:56:21 +02001465 }
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001466 rcu_read_unlock();
1467 return ret;
Vlad Buslov195c2342019-03-21 15:17:38 +02001468
1469errout_cleanup:
1470 rhashtable_remove_fast(&head->ht, &mask->ht_node,
1471 mask_ht_params);
Vlad Buslov195c2342019-03-21 15:17:38 +02001472 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +02001473}
1474
1475static int fl_set_parms(struct net *net, struct tcf_proto *tp,
1476 struct cls_fl_filter *f, struct fl_flow_mask *mask,
1477 unsigned long base, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05001478 struct nlattr *est, bool ovr,
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001479 struct fl_flow_tmplt *tmplt, bool rtnl_held,
Alexander Aring50a56192018-01-18 11:20:52 -05001480 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001481{
Jiri Pirko77b99002015-05-12 14:56:21 +02001482 int err;
1483
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001484 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, rtnl_held,
Vlad Buslovec6743a2019-02-11 10:55:43 +02001485 extack);
Jiri Pirko77b99002015-05-12 14:56:21 +02001486 if (err < 0)
1487 return err;
1488
1489 if (tb[TCA_FLOWER_CLASSID]) {
1490 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001491 if (!rtnl_held)
1492 rtnl_lock();
Jiri Pirko77b99002015-05-12 14:56:21 +02001493 tcf_bind_filter(tp, &f->res, base);
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001494 if (!rtnl_held)
1495 rtnl_unlock();
Jiri Pirko77b99002015-05-12 14:56:21 +02001496 }
1497
Alexander Aring1057c552018-01-18 11:20:54 -05001498 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +02001499 if (err)
Jiri Pirko45507522017-08-04 14:29:06 +02001500 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +02001501
1502 fl_mask_update_range(mask);
1503 fl_set_masked_key(&f->mkey, &f->key, mask);
1504
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001505 if (!fl_mask_fits_tmplt(tmplt, mask)) {
1506 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
1507 return -EINVAL;
1508 }
1509
Jiri Pirko77b99002015-05-12 14:56:21 +02001510 return 0;
Jiri Pirko77b99002015-05-12 14:56:21 +02001511}
1512
Vlad Buslov1f17f772019-04-05 20:56:26 +03001513static int fl_ht_insert_unique(struct cls_fl_filter *fnew,
1514 struct cls_fl_filter *fold,
1515 bool *in_ht)
1516{
1517 struct fl_flow_mask *mask = fnew->mask;
1518 int err;
1519
Vlad Buslov9e355522019-04-11 19:12:20 +03001520 err = rhashtable_lookup_insert_fast(&mask->ht,
1521 &fnew->ht_node,
1522 mask->filter_ht_params);
Vlad Buslov1f17f772019-04-05 20:56:26 +03001523 if (err) {
1524 *in_ht = false;
1525 /* It is okay if filter with same key exists when
1526 * overwriting.
1527 */
1528 return fold && err == -EEXIST ? 0 : err;
1529 }
1530
1531 *in_ht = true;
1532 return 0;
1533}
1534
Jiri Pirko77b99002015-05-12 14:56:21 +02001535static int fl_change(struct net *net, struct sk_buff *in_skb,
1536 struct tcf_proto *tp, unsigned long base,
1537 u32 handle, struct nlattr **tca,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001538 void **arg, bool ovr, bool rtnl_held,
1539 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001540{
Vlad Buslove4746192019-03-21 15:17:33 +02001541 struct cls_fl_head *head = fl_head_dereference(tp);
WANG Cong8113c092017-08-04 21:31:43 -07001542 struct cls_fl_filter *fold = *arg;
Jiri Pirko77b99002015-05-12 14:56:21 +02001543 struct cls_fl_filter *fnew;
Ivan Vecera2cddd202019-01-16 16:53:52 +01001544 struct fl_flow_mask *mask;
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001545 struct nlattr **tb;
Vlad Buslov1f17f772019-04-05 20:56:26 +03001546 bool in_ht;
Jiri Pirko77b99002015-05-12 14:56:21 +02001547 int err;
1548
Vlad Buslov06177552019-03-21 15:17:35 +02001549 if (!tca[TCA_OPTIONS]) {
1550 err = -EINVAL;
1551 goto errout_fold;
1552 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001553
Ivan Vecera2cddd202019-01-16 16:53:52 +01001554 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
Vlad Buslov06177552019-03-21 15:17:35 +02001555 if (!mask) {
1556 err = -ENOBUFS;
1557 goto errout_fold;
1558 }
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001559
Ivan Vecera2cddd202019-01-16 16:53:52 +01001560 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1561 if (!tb) {
1562 err = -ENOBUFS;
1563 goto errout_mask_alloc;
1564 }
1565
Johannes Berg8cb08172019-04-26 14:07:28 +02001566 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
1567 tca[TCA_OPTIONS], fl_policy, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +02001568 if (err < 0)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001569 goto errout_tb;
Jiri Pirko77b99002015-05-12 14:56:21 +02001570
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001571 if (fold && handle && fold->handle != handle) {
1572 err = -EINVAL;
1573 goto errout_tb;
1574 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001575
1576 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001577 if (!fnew) {
1578 err = -ENOBUFS;
1579 goto errout_tb;
1580 }
Vlad Buslovc049d562019-04-24 09:53:31 +03001581 INIT_LIST_HEAD(&fnew->hw_list);
Vlad Buslov06177552019-03-21 15:17:35 +02001582 refcount_set(&fnew->refcnt, 1);
Jiri Pirko77b99002015-05-12 14:56:21 +02001583
Cong Wang14215102019-02-20 21:37:42 -08001584 err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0);
WANG Congb9a24bb2016-08-19 12:36:54 -07001585 if (err < 0)
1586 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +02001587
Vlad Buslovecb3dea2019-03-06 16:22:12 +02001588 if (tb[TCA_FLOWER_FLAGS]) {
1589 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
1590
1591 if (!tc_flags_valid(fnew->flags)) {
1592 err = -EINVAL;
1593 goto errout;
1594 }
1595 }
1596
1597 err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr,
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001598 tp->chain->tmplt_priv, rtnl_held, extack);
Vlad Buslovecb3dea2019-03-06 16:22:12 +02001599 if (err)
1600 goto errout;
1601
1602 err = fl_check_assign_mask(head, fnew, fold, mask);
1603 if (err)
1604 goto errout;
1605
Vlad Buslov1f17f772019-04-05 20:56:26 +03001606 err = fl_ht_insert_unique(fnew, fold, &in_ht);
1607 if (err)
1608 goto errout_mask;
1609
Hadar Hen Zion79685212016-12-01 14:06:34 +02001610 if (!tc_skip_hw(fnew->flags)) {
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001611 err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack);
Hadar Hen Zion79685212016-12-01 14:06:34 +02001612 if (err)
Vlad Buslov1f17f772019-04-05 20:56:26 +03001613 goto errout_ht;
Hadar Hen Zion79685212016-12-01 14:06:34 +02001614 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001615
Or Gerlitz55593962017-02-16 10:31:13 +02001616 if (!tc_in_hw(fnew->flags))
1617 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1618
Vlad Buslov3d81e712019-03-21 15:17:42 +02001619 spin_lock(&tp->lock);
1620
Vlad Buslov272ffaa2019-03-21 15:17:41 +02001621 /* tp was deleted concurrently. -EAGAIN will cause caller to lookup
1622 * proto again or create new one, if necessary.
1623 */
1624 if (tp->deleting) {
1625 err = -EAGAIN;
1626 goto errout_hw;
1627 }
1628
Amir Vadai5b33f482016-03-08 12:42:29 +02001629 if (fold) {
Vlad Buslovb2552b82019-03-21 15:17:36 +02001630 /* Fold filter was deleted concurrently. Retry lookup. */
1631 if (fold->deleted) {
1632 err = -EAGAIN;
1633 goto errout_hw;
1634 }
1635
Vlad Buslov620da482019-03-21 15:17:34 +02001636 fnew->handle = handle;
1637
Vlad Buslov1f17f772019-04-05 20:56:26 +03001638 if (!in_ht) {
1639 struct rhashtable_params params =
1640 fnew->mask->filter_ht_params;
1641
1642 err = rhashtable_insert_fast(&fnew->mask->ht,
1643 &fnew->ht_node,
1644 params);
1645 if (err)
1646 goto errout_hw;
1647 in_ht = true;
1648 }
Vlad Buslov620da482019-03-21 15:17:34 +02001649
Vlad Buslovc049d562019-04-24 09:53:31 +03001650 refcount_inc(&fnew->refcnt);
Roi Dayan599d2572018-12-19 18:07:56 +02001651 rhashtable_remove_fast(&fold->mask->ht,
1652 &fold->ht_node,
1653 fold->mask->filter_ht_params);
Matthew Wilcox234a4622017-11-28 09:56:36 -05001654 idr_replace(&head->handle_idr, fnew, fnew->handle);
Daniel Borkmannff3532f2015-07-17 22:38:44 +02001655 list_replace_rcu(&fold->list, &fnew->list);
Vlad Buslovb2552b82019-03-21 15:17:36 +02001656 fold->deleted = true;
Vlad Buslov620da482019-03-21 15:17:34 +02001657
Vlad Buslov3d81e712019-03-21 15:17:42 +02001658 spin_unlock(&tp->lock);
1659
Vlad Buslov99946772019-04-12 00:54:19 +03001660 fl_mask_put(head, fold->mask);
Vlad Buslov620da482019-03-21 15:17:34 +02001661 if (!tc_skip_hw(fold->flags))
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001662 fl_hw_destroy_filter(tp, fold, rtnl_held, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +02001663 tcf_unbind_filter(tp, &fold->res);
Vlad Buslov06177552019-03-21 15:17:35 +02001664 /* Caller holds reference to fold, so refcnt is always > 0
1665 * after this.
1666 */
1667 refcount_dec(&fold->refcnt);
1668 __fl_put(fold);
Jiri Pirko77b99002015-05-12 14:56:21 +02001669 } else {
Vlad Buslov620da482019-03-21 15:17:34 +02001670 if (handle) {
1671 /* user specifies a handle and it doesn't exist */
1672 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1673 handle, GFP_ATOMIC);
Vlad Buslov9a2d9382019-03-21 15:17:40 +02001674
1675 /* Filter with specified handle was concurrently
1676 * inserted after initial check in cls_api. This is not
1677 * necessarily an error if NLM_F_EXCL is not set in
1678 * message flags. Returning EAGAIN will cause cls_api to
1679 * try to update concurrently inserted rule.
1680 */
1681 if (err == -ENOSPC)
1682 err = -EAGAIN;
Vlad Buslov620da482019-03-21 15:17:34 +02001683 } else {
1684 handle = 1;
1685 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1686 INT_MAX, GFP_ATOMIC);
1687 }
1688 if (err)
1689 goto errout_hw;
1690
Vlad Buslovc049d562019-04-24 09:53:31 +03001691 refcount_inc(&fnew->refcnt);
Vlad Buslov620da482019-03-21 15:17:34 +02001692 fnew->handle = handle;
Paul Blakey05cd2712018-04-30 14:28:30 +03001693 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
Vlad Buslov3d81e712019-03-21 15:17:42 +02001694 spin_unlock(&tp->lock);
Jiri Pirko77b99002015-05-12 14:56:21 +02001695 }
1696
Vlad Buslov620da482019-03-21 15:17:34 +02001697 *arg = fnew;
1698
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001699 kfree(tb);
Vlad Buslov99815f52019-06-13 17:54:04 +03001700 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
Jiri Pirko77b99002015-05-12 14:56:21 +02001701 return 0;
1702
Vlad Buslovc049d562019-04-24 09:53:31 +03001703errout_ht:
1704 spin_lock(&tp->lock);
Vlad Buslov620da482019-03-21 15:17:34 +02001705errout_hw:
Vlad Buslovc049d562019-04-24 09:53:31 +03001706 fnew->deleted = true;
Vlad Buslov3d81e712019-03-21 15:17:42 +02001707 spin_unlock(&tp->lock);
Vlad Buslov620da482019-03-21 15:17:34 +02001708 if (!tc_skip_hw(fnew->flags))
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001709 fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL);
Vlad Buslov1f17f772019-04-05 20:56:26 +03001710 if (in_ht)
1711 rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node,
1712 fnew->mask->filter_ht_params);
Vlad Buslovecb3dea2019-03-06 16:22:12 +02001713errout_mask:
Vlad Buslov99946772019-04-12 00:54:19 +03001714 fl_mask_put(head, fnew->mask);
Jiri Pirko77b99002015-05-12 14:56:21 +02001715errout:
Vlad Buslovc049d562019-04-24 09:53:31 +03001716 __fl_put(fnew);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001717errout_tb:
1718 kfree(tb);
Ivan Vecera2cddd202019-01-16 16:53:52 +01001719errout_mask_alloc:
Vlad Buslov99815f52019-06-13 17:54:04 +03001720 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
Vlad Buslov06177552019-03-21 15:17:35 +02001721errout_fold:
1722 if (fold)
1723 __fl_put(fold);
Jiri Pirko77b99002015-05-12 14:56:21 +02001724 return err;
1725}
1726
Alexander Aring571acf22018-01-18 11:20:53 -05001727static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001728 bool rtnl_held, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001729{
Vlad Buslove4746192019-03-21 15:17:33 +02001730 struct cls_fl_head *head = fl_head_dereference(tp);
WANG Cong8113c092017-08-04 21:31:43 -07001731 struct cls_fl_filter *f = arg;
Vlad Buslovb2552b82019-03-21 15:17:36 +02001732 bool last_on_mask;
1733 int err = 0;
Jiri Pirko77b99002015-05-12 14:56:21 +02001734
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001735 err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack);
Paul Blakey05cd2712018-04-30 14:28:30 +03001736 *last = list_empty(&head->masks);
Vlad Buslov06177552019-03-21 15:17:35 +02001737 __fl_put(f);
1738
Vlad Buslovb2552b82019-03-21 15:17:36 +02001739 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +02001740}
1741
Vlad Buslov12db03b2019-02-11 10:55:45 +02001742static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg,
1743 bool rtnl_held)
Jiri Pirko77b99002015-05-12 14:56:21 +02001744{
Cong Wangd39d7142019-06-28 11:03:42 -07001745 struct cls_fl_head *head = fl_head_dereference(tp);
1746 unsigned long id = arg->cookie, tmp;
Jiri Pirko77b99002015-05-12 14:56:21 +02001747 struct cls_fl_filter *f;
1748
Vlad Buslov01683a12018-07-09 13:29:11 +03001749 arg->count = arg->skip;
1750
Cong Wangd39d7142019-06-28 11:03:42 -07001751 idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) {
1752 /* don't return filters that are being deleted */
1753 if (!refcount_inc_not_zero(&f->refcnt))
1754 continue;
Vlad Buslov01683a12018-07-09 13:29:11 +03001755 if (arg->fn(tp, f, arg) < 0) {
Vlad Buslov06177552019-03-21 15:17:35 +02001756 __fl_put(f);
Vlad Buslov01683a12018-07-09 13:29:11 +03001757 arg->stop = 1;
1758 break;
Paul Blakey05cd2712018-04-30 14:28:30 +03001759 }
Vlad Buslov06177552019-03-21 15:17:35 +02001760 __fl_put(f);
Vlad Buslov01683a12018-07-09 13:29:11 +03001761 arg->count++;
Jiri Pirko77b99002015-05-12 14:56:21 +02001762 }
Cong Wangd39d7142019-06-28 11:03:42 -07001763 arg->cookie = id;
Jiri Pirko77b99002015-05-12 14:56:21 +02001764}
1765
Vlad Buslovc049d562019-04-24 09:53:31 +03001766static struct cls_fl_filter *
1767fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add)
1768{
1769 struct cls_fl_head *head = fl_head_dereference(tp);
1770
1771 spin_lock(&tp->lock);
1772 if (list_empty(&head->hw_filters)) {
1773 spin_unlock(&tp->lock);
1774 return NULL;
1775 }
1776
1777 if (!f)
1778 f = list_entry(&head->hw_filters, struct cls_fl_filter,
1779 hw_list);
1780 list_for_each_entry_continue(f, &head->hw_filters, hw_list) {
1781 if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) {
1782 spin_unlock(&tp->lock);
1783 return f;
1784 }
1785 }
1786
1787 spin_unlock(&tp->lock);
1788 return NULL;
1789}
1790
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +02001791static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
John Hurley31533cb2018-06-25 14:30:06 -07001792 void *cb_priv, struct netlink_ext_ack *extack)
1793{
John Hurley31533cb2018-06-25 14:30:06 -07001794 struct tcf_block *block = tp->chain->block;
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +02001795 struct flow_cls_offload cls_flower = {};
Vlad Buslovc049d562019-04-24 09:53:31 +03001796 struct cls_fl_filter *f = NULL;
John Hurley31533cb2018-06-25 14:30:06 -07001797 int err;
1798
Vlad Buslovc049d562019-04-24 09:53:31 +03001799 /* hw_filters list can only be changed by hw offload functions after
1800 * obtaining rtnl lock. Make sure it is not changed while reoffload is
1801 * iterating it.
1802 */
1803 ASSERT_RTNL();
John Hurley31533cb2018-06-25 14:30:06 -07001804
Vlad Buslovc049d562019-04-24 09:53:31 +03001805 while ((f = fl_get_next_hw_filter(tp, f, add))) {
John Hurley95e27a42019-04-02 23:53:20 +01001806 cls_flower.rule =
1807 flow_rule_alloc(tcf_exts_num_actions(&f->exts));
1808 if (!cls_flower.rule) {
1809 __fl_put(f);
1810 return -ENOMEM;
John Hurley31533cb2018-06-25 14:30:06 -07001811 }
John Hurley95e27a42019-04-02 23:53:20 +01001812
1813 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags,
Pieter Jansen van Vuurend6787142019-05-06 17:24:21 -07001814 extack);
John Hurley95e27a42019-04-02 23:53:20 +01001815 cls_flower.command = add ?
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +02001816 FLOW_CLS_REPLACE : FLOW_CLS_DESTROY;
John Hurley95e27a42019-04-02 23:53:20 +01001817 cls_flower.cookie = (unsigned long)f;
1818 cls_flower.rule->match.dissector = &f->mask->dissector;
1819 cls_flower.rule->match.mask = &f->mask->key;
1820 cls_flower.rule->match.key = &f->mkey;
1821
1822 err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
1823 if (err) {
1824 kfree(cls_flower.rule);
1825 if (tc_skip_sw(f->flags)) {
1826 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
1827 __fl_put(f);
1828 return err;
1829 }
1830 goto next_flow;
1831 }
1832
1833 cls_flower.classid = f->res.classid;
1834
Vlad Buslov40119212019-08-26 16:44:59 +03001835 err = tc_setup_cb_reoffload(block, tp, add, cb,
1836 TC_SETUP_CLSFLOWER, &cls_flower,
1837 cb_priv, &f->flags,
1838 &f->in_hw_count);
John Hurley95e27a42019-04-02 23:53:20 +01001839 kfree(cls_flower.rule);
1840
1841 if (err) {
Vlad Buslov40119212019-08-26 16:44:59 +03001842 __fl_put(f);
1843 return err;
John Hurley95e27a42019-04-02 23:53:20 +01001844 }
John Hurley95e27a42019-04-02 23:53:20 +01001845next_flow:
John Hurley95e27a42019-04-02 23:53:20 +01001846 __fl_put(f);
John Hurley31533cb2018-06-25 14:30:06 -07001847 }
1848
1849 return 0;
1850}
1851
Vlad Buslova449a3e2019-08-26 16:45:00 +03001852static void fl_hw_add(struct tcf_proto *tp, void *type_data)
1853{
1854 struct flow_cls_offload *cls_flower = type_data;
1855 struct cls_fl_filter *f =
1856 (struct cls_fl_filter *) cls_flower->cookie;
1857 struct cls_fl_head *head = fl_head_dereference(tp);
1858
1859 spin_lock(&tp->lock);
1860 list_add(&f->hw_list, &head->hw_filters);
1861 spin_unlock(&tp->lock);
1862}
1863
1864static void fl_hw_del(struct tcf_proto *tp, void *type_data)
1865{
1866 struct flow_cls_offload *cls_flower = type_data;
1867 struct cls_fl_filter *f =
1868 (struct cls_fl_filter *) cls_flower->cookie;
1869
1870 spin_lock(&tp->lock);
1871 if (!list_empty(&f->hw_list))
1872 list_del_init(&f->hw_list);
1873 spin_unlock(&tp->lock);
1874}
1875
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01001876static int fl_hw_create_tmplt(struct tcf_chain *chain,
1877 struct fl_flow_tmplt *tmplt)
Jiri Pirko34738452018-07-23 09:23:11 +02001878{
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +02001879 struct flow_cls_offload cls_flower = {};
Jiri Pirko34738452018-07-23 09:23:11 +02001880 struct tcf_block *block = chain->block;
Jiri Pirko34738452018-07-23 09:23:11 +02001881
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +01001882 cls_flower.rule = flow_rule_alloc(0);
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01001883 if (!cls_flower.rule)
1884 return -ENOMEM;
1885
Jiri Pirko34738452018-07-23 09:23:11 +02001886 cls_flower.common.chain_index = chain->index;
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +02001887 cls_flower.command = FLOW_CLS_TMPLT_CREATE;
Jiri Pirko34738452018-07-23 09:23:11 +02001888 cls_flower.cookie = (unsigned long) tmplt;
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01001889 cls_flower.rule->match.dissector = &tmplt->dissector;
1890 cls_flower.rule->match.mask = &tmplt->mask;
1891 cls_flower.rule->match.key = &tmplt->dummy_key;
Jiri Pirko34738452018-07-23 09:23:11 +02001892
1893 /* We don't care if driver (any of them) fails to handle this
1894 * call. It serves just as a hint for it.
1895 */
Vlad Buslov40119212019-08-26 16:44:59 +03001896 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01001897 kfree(cls_flower.rule);
1898
1899 return 0;
Jiri Pirko34738452018-07-23 09:23:11 +02001900}
1901
1902static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
1903 struct fl_flow_tmplt *tmplt)
1904{
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +02001905 struct flow_cls_offload cls_flower = {};
Jiri Pirko34738452018-07-23 09:23:11 +02001906 struct tcf_block *block = chain->block;
1907
1908 cls_flower.common.chain_index = chain->index;
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +02001909 cls_flower.command = FLOW_CLS_TMPLT_DESTROY;
Jiri Pirko34738452018-07-23 09:23:11 +02001910 cls_flower.cookie = (unsigned long) tmplt;
1911
Vlad Buslov40119212019-08-26 16:44:59 +03001912 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
Jiri Pirko34738452018-07-23 09:23:11 +02001913}
1914
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001915static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
1916 struct nlattr **tca,
1917 struct netlink_ext_ack *extack)
1918{
1919 struct fl_flow_tmplt *tmplt;
1920 struct nlattr **tb;
1921 int err;
1922
1923 if (!tca[TCA_OPTIONS])
1924 return ERR_PTR(-EINVAL);
1925
1926 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1927 if (!tb)
1928 return ERR_PTR(-ENOBUFS);
Johannes Berg8cb08172019-04-26 14:07:28 +02001929 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
1930 tca[TCA_OPTIONS], fl_policy, NULL);
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001931 if (err)
1932 goto errout_tb;
1933
1934 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
Dan Carpenter1cbc36a52018-08-03 22:27:55 +03001935 if (!tmplt) {
1936 err = -ENOMEM;
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001937 goto errout_tb;
Dan Carpenter1cbc36a52018-08-03 22:27:55 +03001938 }
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001939 tmplt->chain = chain;
1940 err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
1941 if (err)
1942 goto errout_tmplt;
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001943
1944 fl_init_dissector(&tmplt->dissector, &tmplt->mask);
1945
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01001946 err = fl_hw_create_tmplt(chain, tmplt);
1947 if (err)
1948 goto errout_tmplt;
Jiri Pirko34738452018-07-23 09:23:11 +02001949
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01001950 kfree(tb);
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001951 return tmplt;
1952
1953errout_tmplt:
1954 kfree(tmplt);
1955errout_tb:
1956 kfree(tb);
1957 return ERR_PTR(err);
1958}
1959
1960static void fl_tmplt_destroy(void *tmplt_priv)
1961{
1962 struct fl_flow_tmplt *tmplt = tmplt_priv;
1963
Cong Wang95278dd2018-10-02 12:50:19 -07001964 fl_hw_destroy_tmplt(tmplt->chain, tmplt);
1965 kfree(tmplt);
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001966}
1967
Jiri Pirko77b99002015-05-12 14:56:21 +02001968static int fl_dump_key_val(struct sk_buff *skb,
1969 void *val, int val_type,
1970 void *mask, int mask_type, int len)
1971{
1972 int err;
1973
1974 if (!memchr_inv(mask, 0, len))
1975 return 0;
1976 err = nla_put(skb, val_type, len, val);
1977 if (err)
1978 return err;
1979 if (mask_type != TCA_FLOWER_UNSPEC) {
1980 err = nla_put(skb, mask_type, len, mask);
1981 if (err)
1982 return err;
1983 }
1984 return 0;
1985}
1986
Amritha Nambiar5c722992018-11-12 16:15:55 -08001987static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
1988 struct fl_flow_key *mask)
1989{
1990 if (fl_dump_key_val(skb, &key->tp_min.dst, TCA_FLOWER_KEY_PORT_DST_MIN,
1991 &mask->tp_min.dst, TCA_FLOWER_UNSPEC,
1992 sizeof(key->tp_min.dst)) ||
1993 fl_dump_key_val(skb, &key->tp_max.dst, TCA_FLOWER_KEY_PORT_DST_MAX,
1994 &mask->tp_max.dst, TCA_FLOWER_UNSPEC,
1995 sizeof(key->tp_max.dst)) ||
1996 fl_dump_key_val(skb, &key->tp_min.src, TCA_FLOWER_KEY_PORT_SRC_MIN,
1997 &mask->tp_min.src, TCA_FLOWER_UNSPEC,
1998 sizeof(key->tp_min.src)) ||
1999 fl_dump_key_val(skb, &key->tp_max.src, TCA_FLOWER_KEY_PORT_SRC_MAX,
2000 &mask->tp_max.src, TCA_FLOWER_UNSPEC,
2001 sizeof(key->tp_max.src)))
2002 return -1;
2003
2004 return 0;
2005}
2006
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002007static int fl_dump_key_mpls(struct sk_buff *skb,
2008 struct flow_dissector_key_mpls *mpls_key,
2009 struct flow_dissector_key_mpls *mpls_mask)
2010{
2011 int err;
2012
2013 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
2014 return 0;
2015 if (mpls_mask->mpls_ttl) {
2016 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
2017 mpls_key->mpls_ttl);
2018 if (err)
2019 return err;
2020 }
2021 if (mpls_mask->mpls_tc) {
2022 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
2023 mpls_key->mpls_tc);
2024 if (err)
2025 return err;
2026 }
2027 if (mpls_mask->mpls_label) {
2028 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
2029 mpls_key->mpls_label);
2030 if (err)
2031 return err;
2032 }
2033 if (mpls_mask->mpls_bos) {
2034 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
2035 mpls_key->mpls_bos);
2036 if (err)
2037 return err;
2038 }
2039 return 0;
2040}
2041
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03002042static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03002043 struct flow_dissector_key_ip *key,
2044 struct flow_dissector_key_ip *mask)
2045{
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03002046 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
2047 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
2048 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
2049 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
2050
2051 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
2052 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
Or Gerlitz4d80cc02017-06-01 21:37:38 +03002053 return -1;
2054
2055 return 0;
2056}
2057
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03002058static int fl_dump_key_vlan(struct sk_buff *skb,
Jianbo Liud64efd02018-07-06 05:38:16 +00002059 int vlan_id_key, int vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03002060 struct flow_dissector_key_vlan *vlan_key,
2061 struct flow_dissector_key_vlan *vlan_mask)
2062{
2063 int err;
2064
2065 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
2066 return 0;
2067 if (vlan_mask->vlan_id) {
Jianbo Liud64efd02018-07-06 05:38:16 +00002068 err = nla_put_u16(skb, vlan_id_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03002069 vlan_key->vlan_id);
2070 if (err)
2071 return err;
2072 }
2073 if (vlan_mask->vlan_priority) {
Jianbo Liud64efd02018-07-06 05:38:16 +00002074 err = nla_put_u8(skb, vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03002075 vlan_key->vlan_priority);
2076 if (err)
2077 return err;
2078 }
2079 return 0;
2080}
2081
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02002082static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
2083 u32 *flower_key, u32 *flower_mask,
2084 u32 flower_flag_bit, u32 dissector_flag_bit)
2085{
2086 if (dissector_mask & dissector_flag_bit) {
2087 *flower_mask |= flower_flag_bit;
2088 if (dissector_key & dissector_flag_bit)
2089 *flower_key |= flower_flag_bit;
2090 }
2091}
2092
2093static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
2094{
2095 u32 key, mask;
2096 __be32 _key, _mask;
2097 int err;
2098
2099 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
2100 return 0;
2101
2102 key = 0;
2103 mask = 0;
2104
2105 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2106 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +01002107 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2108 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
2109 FLOW_DIS_FIRST_FRAG);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02002110
2111 _key = cpu_to_be32(key);
2112 _mask = cpu_to_be32(mask);
2113
2114 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
2115 if (err)
2116 return err;
2117
2118 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
2119}
2120
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02002121static int fl_dump_key_geneve_opt(struct sk_buff *skb,
2122 struct flow_dissector_key_enc_opts *enc_opts)
2123{
2124 struct geneve_opt *opt;
2125 struct nlattr *nest;
2126 int opt_off = 0;
2127
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002128 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02002129 if (!nest)
2130 goto nla_put_failure;
2131
2132 while (enc_opts->len > opt_off) {
2133 opt = (struct geneve_opt *)&enc_opts->data[opt_off];
2134
2135 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
2136 opt->opt_class))
2137 goto nla_put_failure;
2138 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
2139 opt->type))
2140 goto nla_put_failure;
2141 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
2142 opt->length * 4, opt->opt_data))
2143 goto nla_put_failure;
2144
2145 opt_off += sizeof(struct geneve_opt) + opt->length * 4;
2146 }
2147 nla_nest_end(skb, nest);
2148 return 0;
2149
2150nla_put_failure:
2151 nla_nest_cancel(skb, nest);
2152 return -EMSGSIZE;
2153}
2154
Paul Blakeye0ace682019-07-09 10:30:50 +03002155static int fl_dump_key_ct(struct sk_buff *skb,
2156 struct flow_dissector_key_ct *key,
2157 struct flow_dissector_key_ct *mask)
2158{
2159 if (IS_ENABLED(CONFIG_NF_CONNTRACK) &&
2160 fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
2161 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
2162 sizeof(key->ct_state)))
2163 goto nla_put_failure;
2164
2165 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
2166 fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
2167 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
2168 sizeof(key->ct_zone)))
2169 goto nla_put_failure;
2170
2171 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
2172 fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
2173 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
2174 sizeof(key->ct_mark)))
2175 goto nla_put_failure;
2176
2177 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
2178 fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
2179 &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
2180 sizeof(key->ct_labels)))
2181 goto nla_put_failure;
2182
2183 return 0;
2184
2185nla_put_failure:
2186 return -EMSGSIZE;
2187}
2188
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02002189static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
2190 struct flow_dissector_key_enc_opts *enc_opts)
2191{
2192 struct nlattr *nest;
2193 int err;
2194
2195 if (!enc_opts->len)
2196 return 0;
2197
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002198 nest = nla_nest_start_noflag(skb, enc_opt_type);
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02002199 if (!nest)
2200 goto nla_put_failure;
2201
2202 switch (enc_opts->dst_opt_type) {
2203 case TUNNEL_GENEVE_OPT:
2204 err = fl_dump_key_geneve_opt(skb, enc_opts);
2205 if (err)
2206 goto nla_put_failure;
2207 break;
2208 default:
2209 goto nla_put_failure;
2210 }
2211 nla_nest_end(skb, nest);
2212 return 0;
2213
2214nla_put_failure:
2215 nla_nest_cancel(skb, nest);
2216 return -EMSGSIZE;
2217}
2218
2219static int fl_dump_key_enc_opt(struct sk_buff *skb,
2220 struct flow_dissector_key_enc_opts *key_opts,
2221 struct flow_dissector_key_enc_opts *msk_opts)
2222{
2223 int err;
2224
2225 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
2226 if (err)
2227 return err;
2228
2229 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
2230}
2231
Jiri Pirkof5749082018-07-23 09:23:08 +02002232static int fl_dump_key(struct sk_buff *skb, struct net *net,
2233 struct fl_flow_key *key, struct fl_flow_key *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +02002234{
Jiri Pirko8212ed72019-06-19 09:41:03 +03002235 if (mask->meta.ingress_ifindex) {
Jiri Pirko77b99002015-05-12 14:56:21 +02002236 struct net_device *dev;
2237
Jiri Pirko8212ed72019-06-19 09:41:03 +03002238 dev = __dev_get_by_index(net, key->meta.ingress_ifindex);
Jiri Pirko77b99002015-05-12 14:56:21 +02002239 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
2240 goto nla_put_failure;
2241 }
2242
2243 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
2244 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
2245 sizeof(key->eth.dst)) ||
2246 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
2247 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
2248 sizeof(key->eth.src)) ||
2249 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
2250 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
2251 sizeof(key->basic.n_proto)))
2252 goto nla_put_failure;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03002253
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002254 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
2255 goto nla_put_failure;
2256
Jianbo Liud64efd02018-07-06 05:38:16 +00002257 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
2258 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03002259 goto nla_put_failure;
2260
Jianbo Liud64efd02018-07-06 05:38:16 +00002261 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
2262 TCA_FLOWER_KEY_CVLAN_PRIO,
2263 &key->cvlan, &mask->cvlan) ||
2264 (mask->cvlan.vlan_tpid &&
Jianbo Liu158abbf2018-07-25 02:31:25 +00002265 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
2266 key->cvlan.vlan_tpid)))
Jianbo Liud3069512018-07-06 05:38:15 +00002267 goto nla_put_failure;
2268
Jianbo Liu5e9a0fe2018-07-09 02:26:20 +00002269 if (mask->basic.n_proto) {
2270 if (mask->cvlan.vlan_tpid) {
2271 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
2272 key->basic.n_proto))
2273 goto nla_put_failure;
2274 } else if (mask->vlan.vlan_tpid) {
2275 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
2276 key->basic.n_proto))
2277 goto nla_put_failure;
2278 }
Jianbo Liud64efd02018-07-06 05:38:16 +00002279 }
2280
Jiri Pirko77b99002015-05-12 14:56:21 +02002281 if ((key->basic.n_proto == htons(ETH_P_IP) ||
2282 key->basic.n_proto == htons(ETH_P_IPV6)) &&
Or Gerlitz4d80cc02017-06-01 21:37:38 +03002283 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
Jiri Pirko77b99002015-05-12 14:56:21 +02002284 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03002285 sizeof(key->basic.ip_proto)) ||
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03002286 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
Jiri Pirko77b99002015-05-12 14:56:21 +02002287 goto nla_put_failure;
2288
Tom Herbertc3f83242015-06-04 09:16:40 -07002289 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02002290 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
2291 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
2292 sizeof(key->ipv4.src)) ||
2293 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
2294 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
2295 sizeof(key->ipv4.dst))))
2296 goto nla_put_failure;
Tom Herbertc3f83242015-06-04 09:16:40 -07002297 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02002298 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
2299 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
2300 sizeof(key->ipv6.src)) ||
2301 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
2302 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
2303 sizeof(key->ipv6.dst))))
2304 goto nla_put_failure;
2305
2306 if (key->basic.ip_proto == IPPROTO_TCP &&
2307 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03002308 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02002309 sizeof(key->tp.src)) ||
2310 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03002311 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02002312 sizeof(key->tp.dst)) ||
2313 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
2314 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
2315 sizeof(key->tcp.flags))))
Jiri Pirko77b99002015-05-12 14:56:21 +02002316 goto nla_put_failure;
2317 else if (key->basic.ip_proto == IPPROTO_UDP &&
2318 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03002319 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02002320 sizeof(key->tp.src)) ||
2321 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03002322 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02002323 sizeof(key->tp.dst))))
2324 goto nla_put_failure;
Simon Horman5976c5f2016-11-03 13:24:21 +01002325 else if (key->basic.ip_proto == IPPROTO_SCTP &&
2326 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
2327 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
2328 sizeof(key->tp.src)) ||
2329 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
2330 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
2331 sizeof(key->tp.dst))))
2332 goto nla_put_failure;
Simon Horman7b684882016-12-07 13:48:28 +01002333 else if (key->basic.n_proto == htons(ETH_P_IP) &&
2334 key->basic.ip_proto == IPPROTO_ICMP &&
2335 (fl_dump_key_val(skb, &key->icmp.type,
2336 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
2337 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
2338 sizeof(key->icmp.type)) ||
2339 fl_dump_key_val(skb, &key->icmp.code,
2340 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
2341 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
2342 sizeof(key->icmp.code))))
2343 goto nla_put_failure;
2344 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
2345 key->basic.ip_proto == IPPROTO_ICMPV6 &&
2346 (fl_dump_key_val(skb, &key->icmp.type,
2347 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
2348 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
2349 sizeof(key->icmp.type)) ||
2350 fl_dump_key_val(skb, &key->icmp.code,
2351 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
2352 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
2353 sizeof(key->icmp.code))))
2354 goto nla_put_failure;
Simon Horman99d31322017-01-11 14:05:43 +01002355 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
2356 key->basic.n_proto == htons(ETH_P_RARP)) &&
2357 (fl_dump_key_val(skb, &key->arp.sip,
2358 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
2359 TCA_FLOWER_KEY_ARP_SIP_MASK,
2360 sizeof(key->arp.sip)) ||
2361 fl_dump_key_val(skb, &key->arp.tip,
2362 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
2363 TCA_FLOWER_KEY_ARP_TIP_MASK,
2364 sizeof(key->arp.tip)) ||
2365 fl_dump_key_val(skb, &key->arp.op,
2366 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
2367 TCA_FLOWER_KEY_ARP_OP_MASK,
2368 sizeof(key->arp.op)) ||
2369 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
2370 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
2371 sizeof(key->arp.sha)) ||
2372 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
2373 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
2374 sizeof(key->arp.tha))))
2375 goto nla_put_failure;
Jiri Pirko77b99002015-05-12 14:56:21 +02002376
Amritha Nambiar5c722992018-11-12 16:15:55 -08002377 if ((key->basic.ip_proto == IPPROTO_TCP ||
2378 key->basic.ip_proto == IPPROTO_UDP ||
2379 key->basic.ip_proto == IPPROTO_SCTP) &&
2380 fl_dump_key_port_range(skb, key, mask))
2381 goto nla_put_failure;
2382
Amir Vadaibc3103f2016-09-08 16:23:47 +03002383 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
2384 (fl_dump_key_val(skb, &key->enc_ipv4.src,
2385 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
2386 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
2387 sizeof(key->enc_ipv4.src)) ||
2388 fl_dump_key_val(skb, &key->enc_ipv4.dst,
2389 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
2390 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
2391 sizeof(key->enc_ipv4.dst))))
2392 goto nla_put_failure;
2393 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
2394 (fl_dump_key_val(skb, &key->enc_ipv6.src,
2395 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
2396 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
2397 sizeof(key->enc_ipv6.src)) ||
2398 fl_dump_key_val(skb, &key->enc_ipv6.dst,
2399 TCA_FLOWER_KEY_ENC_IPV6_DST,
2400 &mask->enc_ipv6.dst,
2401 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
2402 sizeof(key->enc_ipv6.dst))))
2403 goto nla_put_failure;
2404
2405 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03002406 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02002407 sizeof(key->enc_key_id)) ||
2408 fl_dump_key_val(skb, &key->enc_tp.src,
2409 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
2410 &mask->enc_tp.src,
2411 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
2412 sizeof(key->enc_tp.src)) ||
2413 fl_dump_key_val(skb, &key->enc_tp.dst,
2414 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
2415 &mask->enc_tp.dst,
2416 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03002417 sizeof(key->enc_tp.dst)) ||
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02002418 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
2419 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
Amir Vadaibc3103f2016-09-08 16:23:47 +03002420 goto nla_put_failure;
2421
Paul Blakeye0ace682019-07-09 10:30:50 +03002422 if (fl_dump_key_ct(skb, &key->ct, &mask->ct))
2423 goto nla_put_failure;
2424
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02002425 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
2426 goto nla_put_failure;
2427
Jiri Pirkof5749082018-07-23 09:23:08 +02002428 return 0;
2429
2430nla_put_failure:
2431 return -EMSGSIZE;
2432}
2433
2434static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002435 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
Jiri Pirkof5749082018-07-23 09:23:08 +02002436{
2437 struct cls_fl_filter *f = fh;
2438 struct nlattr *nest;
2439 struct fl_flow_key *key, *mask;
Vlad Buslov3d81e712019-03-21 15:17:42 +02002440 bool skip_hw;
Jiri Pirkof5749082018-07-23 09:23:08 +02002441
2442 if (!f)
2443 return skb->len;
2444
2445 t->tcm_handle = f->handle;
2446
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002447 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Jiri Pirkof5749082018-07-23 09:23:08 +02002448 if (!nest)
2449 goto nla_put_failure;
2450
Vlad Buslov3d81e712019-03-21 15:17:42 +02002451 spin_lock(&tp->lock);
2452
Jiri Pirkof5749082018-07-23 09:23:08 +02002453 if (f->res.classid &&
2454 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
Vlad Buslov3d81e712019-03-21 15:17:42 +02002455 goto nla_put_failure_locked;
Jiri Pirkof5749082018-07-23 09:23:08 +02002456
2457 key = &f->key;
2458 mask = &f->mask->key;
Vlad Buslov3d81e712019-03-21 15:17:42 +02002459 skip_hw = tc_skip_hw(f->flags);
Jiri Pirkof5749082018-07-23 09:23:08 +02002460
2461 if (fl_dump_key(skb, net, key, mask))
Vlad Buslov3d81e712019-03-21 15:17:42 +02002462 goto nla_put_failure_locked;
Jiri Pirkof5749082018-07-23 09:23:08 +02002463
Or Gerlitz749e6722017-02-16 10:31:10 +02002464 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
Vlad Buslov3d81e712019-03-21 15:17:42 +02002465 goto nla_put_failure_locked;
2466
2467 spin_unlock(&tp->lock);
2468
2469 if (!skip_hw)
Vlad Buslovc24e43d82019-03-21 15:17:43 +02002470 fl_hw_update_stats(tp, f, rtnl_held);
Amir Vadaie69985c2016-06-05 17:11:18 +03002471
Vlad Buslov86c55362018-09-07 17:22:21 +03002472 if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
2473 goto nla_put_failure;
2474
Jiri Pirko77b99002015-05-12 14:56:21 +02002475 if (tcf_exts_dump(skb, &f->exts))
2476 goto nla_put_failure;
2477
2478 nla_nest_end(skb, nest);
2479
2480 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
2481 goto nla_put_failure;
2482
2483 return skb->len;
2484
Vlad Buslov3d81e712019-03-21 15:17:42 +02002485nla_put_failure_locked:
2486 spin_unlock(&tp->lock);
Jiri Pirko77b99002015-05-12 14:56:21 +02002487nla_put_failure:
2488 nla_nest_cancel(skb, nest);
2489 return -1;
2490}
2491
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02002492static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
2493{
2494 struct fl_flow_tmplt *tmplt = tmplt_priv;
2495 struct fl_flow_key *key, *mask;
2496 struct nlattr *nest;
2497
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002498 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02002499 if (!nest)
2500 goto nla_put_failure;
2501
2502 key = &tmplt->dummy_key;
2503 mask = &tmplt->mask;
2504
2505 if (fl_dump_key(skb, net, key, mask))
2506 goto nla_put_failure;
2507
2508 nla_nest_end(skb, nest);
2509
2510 return skb->len;
2511
2512nla_put_failure:
2513 nla_nest_cancel(skb, nest);
2514 return -EMSGSIZE;
2515}
2516
Cong Wang07d79fc2017-08-30 14:30:36 -07002517static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
2518{
2519 struct cls_fl_filter *f = fh;
2520
2521 if (f && f->res.classid == classid)
2522 f->res.class = cl;
2523}
2524
Jiri Pirko77b99002015-05-12 14:56:21 +02002525static struct tcf_proto_ops cls_fl_ops __read_mostly = {
2526 .kind = "flower",
2527 .classify = fl_classify,
2528 .init = fl_init,
2529 .destroy = fl_destroy,
2530 .get = fl_get,
Vlad Buslov06177552019-03-21 15:17:35 +02002531 .put = fl_put,
Jiri Pirko77b99002015-05-12 14:56:21 +02002532 .change = fl_change,
2533 .delete = fl_delete,
2534 .walk = fl_walk,
John Hurley31533cb2018-06-25 14:30:06 -07002535 .reoffload = fl_reoffload,
Vlad Buslova449a3e2019-08-26 16:45:00 +03002536 .hw_add = fl_hw_add,
2537 .hw_del = fl_hw_del,
Jiri Pirko77b99002015-05-12 14:56:21 +02002538 .dump = fl_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07002539 .bind_class = fl_bind_class,
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02002540 .tmplt_create = fl_tmplt_create,
2541 .tmplt_destroy = fl_tmplt_destroy,
2542 .tmplt_dump = fl_tmplt_dump,
Jiri Pirko77b99002015-05-12 14:56:21 +02002543 .owner = THIS_MODULE,
Vlad Buslov92149192019-03-21 15:17:44 +02002544 .flags = TCF_PROTO_OPS_DOIT_UNLOCKED,
Jiri Pirko77b99002015-05-12 14:56:21 +02002545};
2546
2547static int __init cls_fl_init(void)
2548{
2549 return register_tcf_proto_ops(&cls_fl_ops);
2550}
2551
2552static void __exit cls_fl_exit(void)
2553{
2554 unregister_tcf_proto_ops(&cls_fl_ops);
2555}
2556
2557module_init(cls_fl_init);
2558module_exit(cls_fl_exit);
2559
2560MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
2561MODULE_DESCRIPTION("Flower classifier");
2562MODULE_LICENSE("GPL v2");