blob: 84f932532db7dc39e23d946cd73e97ba042795a4 [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>
Xin Longd8f9dfa2019-11-21 18:03:28 +080025#include <net/vxlan.h>
Xin Long79b10112019-11-21 18:03:29 +080026#include <net/erspan.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020027
Amir Vadaibc3103f2016-09-08 16:23:47 +030028#include <net/dst.h>
29#include <net/dst_metadata.h>
30
Paul Blakeye0ace682019-07-09 10:30:50 +030031#include <uapi/linux/netfilter/nf_conntrack_common.h>
32
Jiri Pirko77b99002015-05-12 14:56:21 +020033struct fl_flow_key {
Jiri Pirko8212ed72019-06-19 09:41:03 +030034 struct flow_dissector_key_meta meta;
Tom Herbert42aecaa2015-06-04 09:16:39 -070035 struct flow_dissector_key_control control;
Amir Vadaibc3103f2016-09-08 16:23:47 +030036 struct flow_dissector_key_control enc_control;
Jiri Pirko77b99002015-05-12 14:56:21 +020037 struct flow_dissector_key_basic basic;
38 struct flow_dissector_key_eth_addrs eth;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +030039 struct flow_dissector_key_vlan vlan;
Jianbo Liud64efd02018-07-06 05:38:16 +000040 struct flow_dissector_key_vlan cvlan;
Jiri Pirko77b99002015-05-12 14:56:21 +020041 union {
Tom Herbertc3f83242015-06-04 09:16:40 -070042 struct flow_dissector_key_ipv4_addrs ipv4;
Jiri Pirko77b99002015-05-12 14:56:21 +020043 struct flow_dissector_key_ipv6_addrs ipv6;
44 };
45 struct flow_dissector_key_ports tp;
Simon Horman7b684882016-12-07 13:48:28 +010046 struct flow_dissector_key_icmp icmp;
Simon Horman99d31322017-01-11 14:05:43 +010047 struct flow_dissector_key_arp arp;
Amir Vadaibc3103f2016-09-08 16:23:47 +030048 struct flow_dissector_key_keyid enc_key_id;
49 union {
50 struct flow_dissector_key_ipv4_addrs enc_ipv4;
51 struct flow_dissector_key_ipv6_addrs enc_ipv6;
52 };
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +020053 struct flow_dissector_key_ports enc_tp;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -040054 struct flow_dissector_key_mpls mpls;
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +020055 struct flow_dissector_key_tcp tcp;
Or Gerlitz4d80cc02017-06-01 21:37:38 +030056 struct flow_dissector_key_ip ip;
Or Gerlitz0e2c17b2018-07-17 19:27:18 +030057 struct flow_dissector_key_ip enc_ip;
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +020058 struct flow_dissector_key_enc_opts enc_opts;
Yoshiki Komachi8ffb0552019-12-03 19:40:12 +090059 union {
60 struct flow_dissector_key_ports tp;
61 struct {
62 struct flow_dissector_key_ports tp_min;
63 struct flow_dissector_key_ports tp_max;
64 };
65 } tp_range;
Paul Blakeye0ace682019-07-09 10:30:50 +030066 struct flow_dissector_key_ct ct;
Ariel Levkovich5923b8f2020-07-23 01:03:01 +030067 struct flow_dissector_key_hash hash;
Jiri Pirko77b99002015-05-12 14:56:21 +020068} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
69
70struct fl_flow_mask_range {
71 unsigned short int start;
72 unsigned short int end;
73};
74
75struct fl_flow_mask {
76 struct fl_flow_key key;
77 struct fl_flow_mask_range range;
Amritha Nambiar5c722992018-11-12 16:15:55 -080078 u32 flags;
Paul Blakey05cd2712018-04-30 14:28:30 +030079 struct rhash_head ht_node;
80 struct rhashtable ht;
81 struct rhashtable_params filter_ht_params;
82 struct flow_dissector dissector;
83 struct list_head filters;
Paolo Abeni44a5cd42018-06-21 20:02:16 +020084 struct rcu_work rwork;
Paul Blakey05cd2712018-04-30 14:28:30 +030085 struct list_head list;
Vlad Buslovf48ef4d2019-03-21 15:17:37 +020086 refcount_t refcnt;
Jiri Pirko77b99002015-05-12 14:56:21 +020087};
88
Jiri Pirkob95ec7e2018-07-23 09:23:10 +020089struct fl_flow_tmplt {
90 struct fl_flow_key dummy_key;
91 struct fl_flow_key mask;
92 struct flow_dissector dissector;
93 struct tcf_chain *chain;
94};
95
Jiri Pirko77b99002015-05-12 14:56:21 +020096struct cls_fl_head {
97 struct rhashtable ht;
Vlad Buslov259e60f2019-03-21 15:17:39 +020098 spinlock_t masks_lock; /* Protect masks list */
Paul Blakey05cd2712018-04-30 14:28:30 +030099 struct list_head masks;
Vlad Buslovc049d562019-04-24 09:53:31 +0300100 struct list_head hw_filters;
Cong Wangaaa908f2018-05-23 15:26:53 -0700101 struct rcu_work rwork;
Chris Mic15ab232017-08-30 02:31:58 -0400102 struct idr handle_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +0200103};
104
105struct cls_fl_filter {
Paul Blakey05cd2712018-04-30 14:28:30 +0300106 struct fl_flow_mask *mask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200107 struct rhash_head ht_node;
108 struct fl_flow_key mkey;
109 struct tcf_exts exts;
110 struct tcf_result res;
111 struct fl_flow_key key;
112 struct list_head list;
Vlad Buslovc049d562019-04-24 09:53:31 +0300113 struct list_head hw_list;
Jiri Pirko77b99002015-05-12 14:56:21 +0200114 u32 handle;
Amir Vadaie69985c2016-06-05 17:11:18 +0300115 u32 flags;
Vlad Buslov86c55362018-09-07 17:22:21 +0300116 u32 in_hw_count;
Cong Wangaaa908f2018-05-23 15:26:53 -0700117 struct rcu_work rwork;
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200118 struct net_device *hw_dev;
Vlad Buslov06177552019-03-21 15:17:35 +0200119 /* Flower classifier is unlocked, which means that its reference counter
120 * can be changed concurrently without any kind of external
121 * synchronization. Use atomic reference counter to be concurrency-safe.
122 */
123 refcount_t refcnt;
Vlad Buslovb2552b82019-03-21 15:17:36 +0200124 bool deleted;
Jiri Pirko77b99002015-05-12 14:56:21 +0200125};
126
Paul Blakey05cd2712018-04-30 14:28:30 +0300127static const struct rhashtable_params mask_ht_params = {
128 .key_offset = offsetof(struct fl_flow_mask, key),
129 .key_len = sizeof(struct fl_flow_key),
130 .head_offset = offsetof(struct fl_flow_mask, ht_node),
131 .automatic_shrinking = true,
132};
133
Jiri Pirko77b99002015-05-12 14:56:21 +0200134static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
135{
136 return mask->range.end - mask->range.start;
137}
138
139static void fl_mask_update_range(struct fl_flow_mask *mask)
140{
141 const u8 *bytes = (const u8 *) &mask->key;
142 size_t size = sizeof(mask->key);
Paul Blakey05cd2712018-04-30 14:28:30 +0300143 size_t i, first = 0, last;
Jiri Pirko77b99002015-05-12 14:56:21 +0200144
Paul Blakey05cd2712018-04-30 14:28:30 +0300145 for (i = 0; i < size; i++) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200146 if (bytes[i]) {
Paul Blakey05cd2712018-04-30 14:28:30 +0300147 first = i;
148 break;
149 }
150 }
151 last = first;
152 for (i = size - 1; i != first; i--) {
153 if (bytes[i]) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200154 last = i;
Paul Blakey05cd2712018-04-30 14:28:30 +0300155 break;
Jiri Pirko77b99002015-05-12 14:56:21 +0200156 }
157 }
158 mask->range.start = rounddown(first, sizeof(long));
159 mask->range.end = roundup(last + 1, sizeof(long));
160}
161
162static void *fl_key_get_start(struct fl_flow_key *key,
163 const struct fl_flow_mask *mask)
164{
165 return (u8 *) key + mask->range.start;
166}
167
168static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
169 struct fl_flow_mask *mask)
170{
171 const long *lkey = fl_key_get_start(key, mask);
172 const long *lmask = fl_key_get_start(&mask->key, mask);
173 long *lmkey = fl_key_get_start(mkey, mask);
174 int i;
175
176 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
177 *lmkey++ = *lkey++ & *lmask++;
178}
179
Jiri Pirkob95ec7e2018-07-23 09:23:10 +0200180static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
181 struct fl_flow_mask *mask)
182{
183 const long *lmask = fl_key_get_start(&mask->key, mask);
184 const long *ltmplt;
185 int i;
186
187 if (!tmplt)
188 return true;
189 ltmplt = fl_key_get_start(&tmplt->mask, mask);
190 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
191 if (~*ltmplt++ & *lmask++)
192 return false;
193 }
194 return true;
195}
196
Jiri Pirko77b99002015-05-12 14:56:21 +0200197static void fl_clear_masked_range(struct fl_flow_key *key,
198 struct fl_flow_mask *mask)
199{
200 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
201}
202
Amritha Nambiar5c722992018-11-12 16:15:55 -0800203static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter,
204 struct fl_flow_key *key,
205 struct fl_flow_key *mkey)
206{
207 __be16 min_mask, max_mask, min_val, max_val;
208
Yoshiki Komachi8ffb0552019-12-03 19:40:12 +0900209 min_mask = htons(filter->mask->key.tp_range.tp_min.dst);
210 max_mask = htons(filter->mask->key.tp_range.tp_max.dst);
211 min_val = htons(filter->key.tp_range.tp_min.dst);
212 max_val = htons(filter->key.tp_range.tp_max.dst);
Amritha Nambiar5c722992018-11-12 16:15:55 -0800213
214 if (min_mask && max_mask) {
Yoshiki Komachi8ffb0552019-12-03 19:40:12 +0900215 if (htons(key->tp_range.tp.dst) < min_val ||
216 htons(key->tp_range.tp.dst) > max_val)
Amritha Nambiar5c722992018-11-12 16:15:55 -0800217 return false;
218
219 /* skb does not have min and max values */
Yoshiki Komachi8ffb0552019-12-03 19:40:12 +0900220 mkey->tp_range.tp_min.dst = filter->mkey.tp_range.tp_min.dst;
221 mkey->tp_range.tp_max.dst = filter->mkey.tp_range.tp_max.dst;
Amritha Nambiar5c722992018-11-12 16:15:55 -0800222 }
223 return true;
224}
225
226static bool fl_range_port_src_cmp(struct cls_fl_filter *filter,
227 struct fl_flow_key *key,
228 struct fl_flow_key *mkey)
229{
230 __be16 min_mask, max_mask, min_val, max_val;
231
Yoshiki Komachi8ffb0552019-12-03 19:40:12 +0900232 min_mask = htons(filter->mask->key.tp_range.tp_min.src);
233 max_mask = htons(filter->mask->key.tp_range.tp_max.src);
234 min_val = htons(filter->key.tp_range.tp_min.src);
235 max_val = htons(filter->key.tp_range.tp_max.src);
Amritha Nambiar5c722992018-11-12 16:15:55 -0800236
237 if (min_mask && max_mask) {
Yoshiki Komachi8ffb0552019-12-03 19:40:12 +0900238 if (htons(key->tp_range.tp.src) < min_val ||
239 htons(key->tp_range.tp.src) > max_val)
Amritha Nambiar5c722992018-11-12 16:15:55 -0800240 return false;
241
242 /* skb does not have min and max values */
Yoshiki Komachi8ffb0552019-12-03 19:40:12 +0900243 mkey->tp_range.tp_min.src = filter->mkey.tp_range.tp_min.src;
244 mkey->tp_range.tp_max.src = filter->mkey.tp_range.tp_max.src;
Amritha Nambiar5c722992018-11-12 16:15:55 -0800245 }
246 return true;
247}
248
249static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask,
250 struct fl_flow_key *mkey)
Paul Blakeya3308d82017-01-16 10:45:13 +0200251{
Paul Blakey05cd2712018-04-30 14:28:30 +0300252 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
253 mask->filter_ht_params);
Paul Blakeya3308d82017-01-16 10:45:13 +0200254}
255
Amritha Nambiar5c722992018-11-12 16:15:55 -0800256static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask,
257 struct fl_flow_key *mkey,
258 struct fl_flow_key *key)
259{
260 struct cls_fl_filter *filter, *f;
261
262 list_for_each_entry_rcu(filter, &mask->filters, list) {
263 if (!fl_range_port_dst_cmp(filter, key, mkey))
264 continue;
265
266 if (!fl_range_port_src_cmp(filter, key, mkey))
267 continue;
268
269 f = __fl_lookup(mask, mkey);
270 if (f)
271 return f;
272 }
273 return NULL;
274}
275
Arnd Bergmann0af413b2020-05-29 22:13:58 +0200276static noinline_for_stack
277struct cls_fl_filter *fl_mask_lookup(struct fl_flow_mask *mask, struct fl_flow_key *key)
Amritha Nambiar5c722992018-11-12 16:15:55 -0800278{
Arnd Bergmann0af413b2020-05-29 22:13:58 +0200279 struct fl_flow_key mkey;
Amritha Nambiar5c722992018-11-12 16:15:55 -0800280
Arnd Bergmann0af413b2020-05-29 22:13:58 +0200281 fl_set_masked_key(&mkey, key, mask);
282 if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE))
283 return fl_lookup_range(mask, &mkey, key);
284
285 return __fl_lookup(mask, &mkey);
Amritha Nambiar5c722992018-11-12 16:15:55 -0800286}
287
Paul Blakeye0ace682019-07-09 10:30:50 +0300288static u16 fl_ct_info_to_flower_map[] = {
289 [IP_CT_ESTABLISHED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
290 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
291 [IP_CT_RELATED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
292 TCA_FLOWER_KEY_CT_FLAGS_RELATED,
293 [IP_CT_ESTABLISHED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
294 TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED,
295 [IP_CT_RELATED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
296 TCA_FLOWER_KEY_CT_FLAGS_RELATED,
297 [IP_CT_NEW] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED |
298 TCA_FLOWER_KEY_CT_FLAGS_NEW,
299};
300
Jiri Pirko77b99002015-05-12 14:56:21 +0200301static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
302 struct tcf_result *res)
303{
304 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
Paul Blakeye0ace682019-07-09 10:30:50 +0300305 struct fl_flow_key skb_key;
306 struct fl_flow_mask *mask;
307 struct cls_fl_filter *f;
Jiri Pirko77b99002015-05-12 14:56:21 +0200308
Paul Blakey05cd2712018-04-30 14:28:30 +0300309 list_for_each_entry_rcu(mask, &head->masks, list) {
Jason Baron8a9093c2020-02-17 15:38:09 -0500310 flow_dissector_init_keys(&skb_key.control, &skb_key.basic);
Paul Blakey05cd2712018-04-30 14:28:30 +0300311 fl_clear_masked_range(&skb_key, mask);
Amir Vadaie69985c2016-06-05 17:11:18 +0300312
Jiri Pirko8212ed72019-06-19 09:41:03 +0300313 skb_flow_dissect_meta(skb, &mask->dissector, &skb_key);
Paul Blakey05cd2712018-04-30 14:28:30 +0300314 /* skb_flow_dissect() does not set n_proto in case an unknown
315 * protocol, so do it rather here.
316 */
Toke Høiland-Jørgensend7bf2eb2020-07-03 22:26:43 +0200317 skb_key.basic.n_proto = skb_protocol(skb, false);
Paul Blakey05cd2712018-04-30 14:28:30 +0300318 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
Paul Blakeye0ace682019-07-09 10:30:50 +0300319 skb_flow_dissect_ct(skb, &mask->dissector, &skb_key,
320 fl_ct_info_to_flower_map,
321 ARRAY_SIZE(fl_ct_info_to_flower_map));
Ariel Levkovich5923b8f2020-07-23 01:03:01 +0300322 skb_flow_dissect_hash(skb, &mask->dissector, &skb_key);
Paul Blakey05cd2712018-04-30 14:28:30 +0300323 skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
Amir Vadaibc3103f2016-09-08 16:23:47 +0300324
Arnd Bergmann0af413b2020-05-29 22:13:58 +0200325 f = fl_mask_lookup(mask, &skb_key);
Paul Blakey05cd2712018-04-30 14:28:30 +0300326 if (f && !tc_skip_sw(f->flags)) {
327 *res = f->res;
328 return tcf_exts_exec(skb, &f->exts, res);
329 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200330 }
331 return -1;
332}
333
334static int fl_init(struct tcf_proto *tp)
335{
336 struct cls_fl_head *head;
337
338 head = kzalloc(sizeof(*head), GFP_KERNEL);
339 if (!head)
340 return -ENOBUFS;
341
Vlad Buslov259e60f2019-03-21 15:17:39 +0200342 spin_lock_init(&head->masks_lock);
Paul Blakey05cd2712018-04-30 14:28:30 +0300343 INIT_LIST_HEAD_RCU(&head->masks);
Vlad Buslovc049d562019-04-24 09:53:31 +0300344 INIT_LIST_HEAD(&head->hw_filters);
Jiri Pirko77b99002015-05-12 14:56:21 +0200345 rcu_assign_pointer(tp->root, head);
Chris Mic15ab232017-08-30 02:31:58 -0400346 idr_init(&head->handle_idr);
Jiri Pirko77b99002015-05-12 14:56:21 +0200347
Paul Blakey05cd2712018-04-30 14:28:30 +0300348 return rhashtable_init(&head->ht, &mask_ht_params);
349}
350
Vlad Buslov99815f52019-06-13 17:54:04 +0300351static void fl_mask_free(struct fl_flow_mask *mask, bool mask_init_done)
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200352{
Vlad Buslov99815f52019-06-13 17:54:04 +0300353 /* temporary masks don't have their filters list and ht initialized */
354 if (mask_init_done) {
355 WARN_ON(!list_empty(&mask->filters));
356 rhashtable_destroy(&mask->ht);
357 }
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200358 kfree(mask);
359}
360
361static void fl_mask_free_work(struct work_struct *work)
362{
363 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
364 struct fl_flow_mask, rwork);
365
Vlad Buslov99815f52019-06-13 17:54:04 +0300366 fl_mask_free(mask, true);
367}
368
369static void fl_uninit_mask_free_work(struct work_struct *work)
370{
371 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
372 struct fl_flow_mask, rwork);
373
374 fl_mask_free(mask, false);
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200375}
376
Vlad Buslov99946772019-04-12 00:54:19 +0300377static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask)
Paul Blakey05cd2712018-04-30 14:28:30 +0300378{
Vlad Buslovf48ef4d2019-03-21 15:17:37 +0200379 if (!refcount_dec_and_test(&mask->refcnt))
Paul Blakey05cd2712018-04-30 14:28:30 +0300380 return false;
381
382 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
Vlad Buslov259e60f2019-03-21 15:17:39 +0200383
384 spin_lock(&head->masks_lock);
Paul Blakey05cd2712018-04-30 14:28:30 +0300385 list_del_rcu(&mask->list);
Vlad Buslov259e60f2019-03-21 15:17:39 +0200386 spin_unlock(&head->masks_lock);
387
Vlad Buslov99946772019-04-12 00:54:19 +0300388 tcf_queue_work(&mask->rwork, fl_mask_free_work);
Paul Blakey05cd2712018-04-30 14:28:30 +0300389
390 return true;
Jiri Pirko77b99002015-05-12 14:56:21 +0200391}
392
Vlad Buslovc049d562019-04-24 09:53:31 +0300393static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp)
394{
395 /* Flower classifier only changes root pointer during init and destroy.
396 * Users must obtain reference to tcf_proto instance before calling its
397 * API, so tp->root pointer is protected from concurrent call to
398 * fl_destroy() by reference counting.
399 */
400 return rcu_dereference_raw(tp->root);
401}
402
Cong Wang0dadc112017-11-06 13:47:24 -0800403static void __fl_destroy_filter(struct cls_fl_filter *f)
404{
405 tcf_exts_destroy(&f->exts);
406 tcf_exts_put_net(&f->exts);
407 kfree(f);
408}
409
Cong Wang0552c8a2017-10-26 18:24:33 -0700410static void fl_destroy_filter_work(struct work_struct *work)
411{
Cong Wangaaa908f2018-05-23 15:26:53 -0700412 struct cls_fl_filter *f = container_of(to_rcu_work(work),
413 struct cls_fl_filter, rwork);
Cong Wang0552c8a2017-10-26 18:24:33 -0700414
Cong Wang0dadc112017-11-06 13:47:24 -0800415 __fl_destroy_filter(f);
Cong Wang0552c8a2017-10-26 18:24:33 -0700416}
417
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800418static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200419 bool rtnl_held, struct netlink_ext_ack *extack)
Amir Vadai5b33f482016-03-08 12:42:29 +0200420{
Jiri Pirko208c0f42017-10-19 15:50:32 +0200421 struct tcf_block *block = tp->chain->block;
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200422 struct flow_cls_offload cls_flower = {};
Amir Vadai5b33f482016-03-08 12:42:29 +0200423
Pieter Jansen van Vuurend6787142019-05-06 17:24:21 -0700424 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200425 cls_flower.command = FLOW_CLS_DESTROY;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200426 cls_flower.cookie = (unsigned long) f;
Amir Vadai5b33f482016-03-08 12:42:29 +0200427
Vlad Buslov40119212019-08-26 16:44:59 +0300428 tc_setup_cb_destroy(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, false,
Vlad Buslov918190f2019-08-26 16:45:06 +0300429 &f->flags, &f->in_hw_count, rtnl_held);
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200430
Amir Vadai5b33f482016-03-08 12:42:29 +0200431}
432
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300433static int fl_hw_replace_filter(struct tcf_proto *tp,
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200434 struct cls_fl_filter *f, bool rtnl_held,
Quentin Monnet41002032018-01-19 17:44:43 -0800435 struct netlink_ext_ack *extack)
Amir Vadai5b33f482016-03-08 12:42:29 +0200436{
Jiri Pirko208c0f42017-10-19 15:50:32 +0200437 struct tcf_block *block = tp->chain->block;
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200438 struct flow_cls_offload cls_flower = {};
Jiri Pirko717503b2017-10-11 09:41:09 +0200439 bool skip_sw = tc_skip_sw(f->flags);
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200440 int err = 0;
441
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +0100442 cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts));
Vlad Buslov918190f2019-08-26 16:45:06 +0300443 if (!cls_flower.rule)
444 return -ENOMEM;
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +0100445
Pieter Jansen van Vuurend6787142019-05-06 17:24:21 -0700446 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200447 cls_flower.command = FLOW_CLS_REPLACE;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200448 cls_flower.cookie = (unsigned long) f;
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +0100449 cls_flower.rule->match.dissector = &f->mask->dissector;
450 cls_flower.rule->match.mask = &f->mask->key;
451 cls_flower.rule->match.key = &f->mkey;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700452 cls_flower.classid = f->res.classid;
Amir Vadai5b33f482016-03-08 12:42:29 +0200453
Vlad Buslovb15e7a62020-02-17 12:12:12 +0200454 err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +0100455 if (err) {
456 kfree(cls_flower.rule);
Vlad Buslov918190f2019-08-26 16:45:06 +0300457 if (skip_sw) {
Vlad Buslov1f15bb42019-02-12 23:39:06 +0200458 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
Vlad Buslov918190f2019-08-26 16:45:06 +0300459 return err;
460 }
461 return 0;
Pablo Neira Ayuso3a7b6862019-02-02 12:50:46 +0100462 }
463
Vlad Buslov40119212019-08-26 16:44:59 +0300464 err = tc_setup_cb_add(block, tp, TC_SETUP_CLSFLOWER, &cls_flower,
Vlad Buslov918190f2019-08-26 16:45:06 +0300465 skip_sw, &f->flags, &f->in_hw_count, rtnl_held);
Vlad Buslov5a6ff4b2019-08-26 16:45:04 +0300466 tc_cleanup_flow_action(&cls_flower.rule->action);
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 Buslov918190f2019-08-26 16:45:06 +0300470 fl_hw_destroy_filter(tp, f, rtnl_held, NULL);
471 return err;
Jiri Pirko717503b2017-10-11 09:41:09 +0200472 }
473
Vlad Buslov918190f2019-08-26 16:45:06 +0300474 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
475 return -EINVAL;
Jiri Pirko717503b2017-10-11 09:41:09 +0200476
Vlad Buslov918190f2019-08-26 16:45:06 +0300477 return 0;
Amir Vadai5b33f482016-03-08 12:42:29 +0200478}
479
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200480static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f,
481 bool rtnl_held)
Amir Vadai10cbc682016-05-13 12:55:37 +0000482{
Jiri Pirko208c0f42017-10-19 15:50:32 +0200483 struct tcf_block *block = tp->chain->block;
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200484 struct flow_cls_offload cls_flower = {};
Amir Vadai10cbc682016-05-13 12:55:37 +0000485
Pieter Jansen van Vuurend6787142019-05-06 17:24:21 -0700486 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +0200487 cls_flower.command = FLOW_CLS_STATS;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200488 cls_flower.cookie = (unsigned long) f;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700489 cls_flower.classid = f->res.classid;
Amir Vadai10cbc682016-05-13 12:55:37 +0000490
Vlad Buslov918190f2019-08-26 16:45:06 +0300491 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false,
492 rtnl_held);
Pablo Neira Ayuso3b1903e2019-02-02 12:50:47 +0100493
494 tcf_exts_stats_update(&f->exts, cls_flower.stats.bytes,
495 cls_flower.stats.pkts,
Po Liu4b61d3e2020-06-19 14:01:07 +0800496 cls_flower.stats.drops,
Jiri Pirko93a129e2020-03-28 16:37:43 +0100497 cls_flower.stats.lastused,
498 cls_flower.stats.used_hw_stats,
499 cls_flower.stats.used_hw_stats_valid);
Amir Vadai10cbc682016-05-13 12:55:37 +0000500}
501
Vlad Buslov06177552019-03-21 15:17:35 +0200502static void __fl_put(struct cls_fl_filter *f)
503{
504 if (!refcount_dec_and_test(&f->refcnt))
505 return;
506
507 if (tcf_exts_get_net(&f->exts))
508 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
509 else
510 __fl_destroy_filter(f);
511}
512
513static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle)
514{
515 struct cls_fl_filter *f;
516
517 rcu_read_lock();
518 f = idr_find(&head->handle_idr, handle);
519 if (f && !refcount_inc_not_zero(&f->refcnt))
520 f = NULL;
521 rcu_read_unlock();
522
523 return f;
524}
525
Vlad Buslovb2552b82019-03-21 15:17:36 +0200526static int __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200527 bool *last, bool rtnl_held,
528 struct netlink_ext_ack *extack)
Roi Dayan13fa8762016-11-01 16:08:29 +0200529{
Vlad Buslove4746192019-03-21 15:17:33 +0200530 struct cls_fl_head *head = fl_head_dereference(tp);
Chris Mic15ab232017-08-30 02:31:58 -0400531
Vlad Buslovb2552b82019-03-21 15:17:36 +0200532 *last = false;
533
Vlad Buslov3d81e712019-03-21 15:17:42 +0200534 spin_lock(&tp->lock);
535 if (f->deleted) {
536 spin_unlock(&tp->lock);
Vlad Buslovb2552b82019-03-21 15:17:36 +0200537 return -ENOENT;
Vlad Buslov3d81e712019-03-21 15:17:42 +0200538 }
Vlad Buslovb2552b82019-03-21 15:17:36 +0200539
540 f->deleted = true;
541 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
542 f->mask->filter_ht_params);
Matthew Wilcox9c160942017-11-28 09:48:43 -0500543 idr_remove(&head->handle_idr, f->handle);
Roi Dayan13fa8762016-11-01 16:08:29 +0200544 list_del_rcu(&f->list);
Vlad Buslov3d81e712019-03-21 15:17:42 +0200545 spin_unlock(&tp->lock);
546
Vlad Buslov99946772019-04-12 00:54:19 +0300547 *last = fl_mask_put(head, f->mask);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200548 if (!tc_skip_hw(f->flags))
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200549 fl_hw_destroy_filter(tp, f, rtnl_held, extack);
Roi Dayan13fa8762016-11-01 16:08:29 +0200550 tcf_unbind_filter(tp, &f->res);
Vlad Buslov06177552019-03-21 15:17:35 +0200551 __fl_put(f);
Paul Blakey05cd2712018-04-30 14:28:30 +0300552
Vlad Buslovb2552b82019-03-21 15:17:36 +0200553 return 0;
Roi Dayan13fa8762016-11-01 16:08:29 +0200554}
555
Daniel Borkmannd9363772016-11-27 01:18:01 +0100556static void fl_destroy_sleepable(struct work_struct *work)
557{
Cong Wangaaa908f2018-05-23 15:26:53 -0700558 struct cls_fl_head *head = container_of(to_rcu_work(work),
559 struct cls_fl_head,
560 rwork);
Paul Blakeyde9dc652018-06-03 10:06:13 +0300561
562 rhashtable_destroy(&head->ht);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100563 kfree(head);
564 module_put(THIS_MODULE);
565}
566
Vlad Buslov12db03b2019-02-11 10:55:45 +0200567static void fl_destroy(struct tcf_proto *tp, bool rtnl_held,
568 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200569{
Vlad Buslove4746192019-03-21 15:17:33 +0200570 struct cls_fl_head *head = fl_head_dereference(tp);
Paul Blakey05cd2712018-04-30 14:28:30 +0300571 struct fl_flow_mask *mask, *next_mask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200572 struct cls_fl_filter *f, *next;
Vlad Buslovb2552b82019-03-21 15:17:36 +0200573 bool last;
Jiri Pirko77b99002015-05-12 14:56:21 +0200574
Paul Blakey05cd2712018-04-30 14:28:30 +0300575 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
576 list_for_each_entry_safe(f, next, &mask->filters, list) {
Vlad Buslovc24e43d82019-03-21 15:17:43 +0200577 __fl_delete(tp, f, &last, rtnl_held, extack);
Vlad Buslovb2552b82019-03-21 15:17:36 +0200578 if (last)
Paul Blakey05cd2712018-04-30 14:28:30 +0300579 break;
580 }
581 }
Chris Mic15ab232017-08-30 02:31:58 -0400582 idr_destroy(&head->handle_idr);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100583
584 __module_get(THIS_MODULE);
Cong Wangaaa908f2018-05-23 15:26:53 -0700585 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
Jiri Pirko77b99002015-05-12 14:56:21 +0200586}
587
Vlad Buslov06177552019-03-21 15:17:35 +0200588static void fl_put(struct tcf_proto *tp, void *arg)
589{
590 struct cls_fl_filter *f = arg;
591
592 __fl_put(f);
593}
594
WANG Cong8113c092017-08-04 21:31:43 -0700595static void *fl_get(struct tcf_proto *tp, u32 handle)
Jiri Pirko77b99002015-05-12 14:56:21 +0200596{
Vlad Buslove4746192019-03-21 15:17:33 +0200597 struct cls_fl_head *head = fl_head_dereference(tp);
Jiri Pirko77b99002015-05-12 14:56:21 +0200598
Vlad Buslov06177552019-03-21 15:17:35 +0200599 return __fl_get(head, handle);
Jiri Pirko77b99002015-05-12 14:56:21 +0200600}
601
602static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
603 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
604 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
605 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
606 .len = IFNAMSIZ },
607 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
608 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
609 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
610 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
611 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
612 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
613 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
614 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
615 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
616 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
617 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
618 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
619 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
620 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
621 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
622 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
Jamal Hadi Salimb175c3a2015-06-25 06:55:27 -0400623 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
624 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300625 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
626 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
627 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
Amir Vadaibc3103f2016-09-08 16:23:47 +0300628 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
629 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
630 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
631 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
632 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
633 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
634 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
635 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
636 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
Or Gerlitzaa72d702016-09-15 15:28:22 +0300637 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
638 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
639 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
640 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
Simon Horman5976c5f2016-11-03 13:24:21 +0100641 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
642 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
643 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
644 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200645 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
646 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
647 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
648 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200649 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
650 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
Simon Horman7b684882016-12-07 13:48:28 +0100651 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
652 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
653 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
654 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
655 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
656 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
657 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
658 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
Simon Horman99d31322017-01-11 14:05:43 +0100659 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
660 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
661 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
662 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
663 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
664 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
665 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
666 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
667 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
668 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400669 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
670 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
671 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
672 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
Guillaume Nault61aec252020-05-26 14:29:04 +0200673 [TCA_FLOWER_KEY_MPLS_OPTS] = { .type = NLA_NESTED },
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200674 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
675 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300676 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
677 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
678 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
679 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
Jianbo Liud64efd02018-07-06 05:38:16 +0000680 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
681 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
682 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300683 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
684 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
685 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
686 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200687 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
688 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
Paul Blakeye0ace682019-07-09 10:30:50 +0300689 [TCA_FLOWER_KEY_CT_STATE] = { .type = NLA_U16 },
690 [TCA_FLOWER_KEY_CT_STATE_MASK] = { .type = NLA_U16 },
691 [TCA_FLOWER_KEY_CT_ZONE] = { .type = NLA_U16 },
692 [TCA_FLOWER_KEY_CT_ZONE_MASK] = { .type = NLA_U16 },
693 [TCA_FLOWER_KEY_CT_MARK] = { .type = NLA_U32 },
694 [TCA_FLOWER_KEY_CT_MARK_MASK] = { .type = NLA_U32 },
695 [TCA_FLOWER_KEY_CT_LABELS] = { .type = NLA_BINARY,
696 .len = 128 / BITS_PER_BYTE },
697 [TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NLA_BINARY,
698 .len = 128 / BITS_PER_BYTE },
Davide Carattie2debf02020-02-11 19:33:40 +0100699 [TCA_FLOWER_FLAGS] = { .type = NLA_U32 },
Ariel Levkovich5923b8f2020-07-23 01:03:01 +0300700 [TCA_FLOWER_KEY_HASH] = { .type = NLA_U32 },
701 [TCA_FLOWER_KEY_HASH_MASK] = { .type = NLA_U32 },
702
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200703};
704
705static const struct nla_policy
706enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
Xin Longd8f9dfa2019-11-21 18:03:28 +0800707 [TCA_FLOWER_KEY_ENC_OPTS_UNSPEC] = {
708 .strict_start_type = TCA_FLOWER_KEY_ENC_OPTS_VXLAN },
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200709 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
Xin Longd8f9dfa2019-11-21 18:03:28 +0800710 [TCA_FLOWER_KEY_ENC_OPTS_VXLAN] = { .type = NLA_NESTED },
Xin Long79b10112019-11-21 18:03:29 +0800711 [TCA_FLOWER_KEY_ENC_OPTS_ERSPAN] = { .type = NLA_NESTED },
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200712};
713
714static const struct nla_policy
715geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
716 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
717 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
718 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
719 .len = 128 },
Jiri Pirko77b99002015-05-12 14:56:21 +0200720};
721
Xin Longd8f9dfa2019-11-21 18:03:28 +0800722static const struct nla_policy
723vxlan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1] = {
724 [TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP] = { .type = NLA_U32 },
725};
726
Xin Long79b10112019-11-21 18:03:29 +0800727static const struct nla_policy
728erspan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1] = {
729 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER] = { .type = NLA_U8 },
730 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX] = { .type = NLA_U32 },
731 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] = { .type = NLA_U8 },
732 [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID] = { .type = NLA_U8 },
733};
734
Guillaume Nault61aec252020-05-26 14:29:04 +0200735static const struct nla_policy
Guillaume Nault61aec252020-05-26 14:29:04 +0200736mpls_stack_entry_policy[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1] = {
737 [TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH] = { .type = NLA_U8 },
738 [TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL] = { .type = NLA_U8 },
739 [TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS] = { .type = NLA_U8 },
740 [TCA_FLOWER_KEY_MPLS_OPT_LSE_TC] = { .type = NLA_U8 },
741 [TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL] = { .type = NLA_U32 },
742};
743
Jiri Pirko77b99002015-05-12 14:56:21 +0200744static void fl_set_key_val(struct nlattr **tb,
745 void *val, int val_type,
746 void *mask, int mask_type, int len)
747{
748 if (!tb[val_type])
749 return;
Paul Blakeye0ace682019-07-09 10:30:50 +0300750 nla_memcpy(val, tb[val_type], len);
Jiri Pirko77b99002015-05-12 14:56:21 +0200751 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
752 memset(mask, 0xff, len);
753 else
Paul Blakeye0ace682019-07-09 10:30:50 +0300754 nla_memcpy(mask, tb[mask_type], len);
Jiri Pirko77b99002015-05-12 14:56:21 +0200755}
756
Amritha Nambiar5c722992018-11-12 16:15:55 -0800757static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
Guillaume Naultbd7d4c12020-03-23 21:48:51 +0100758 struct fl_flow_key *mask,
759 struct netlink_ext_ack *extack)
Amritha Nambiar5c722992018-11-12 16:15:55 -0800760{
Yoshiki Komachi8ffb0552019-12-03 19:40:12 +0900761 fl_set_key_val(tb, &key->tp_range.tp_min.dst,
762 TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_range.tp_min.dst,
763 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.dst));
764 fl_set_key_val(tb, &key->tp_range.tp_max.dst,
765 TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_range.tp_max.dst,
766 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.dst));
767 fl_set_key_val(tb, &key->tp_range.tp_min.src,
768 TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_range.tp_min.src,
769 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.src));
770 fl_set_key_val(tb, &key->tp_range.tp_max.src,
771 TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_range.tp_max.src,
772 TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.src));
Amritha Nambiar5c722992018-11-12 16:15:55 -0800773
Guillaume Naultbd7d4c12020-03-23 21:48:51 +0100774 if (mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst &&
775 htons(key->tp_range.tp_max.dst) <=
776 htons(key->tp_range.tp_min.dst)) {
777 NL_SET_ERR_MSG_ATTR(extack,
778 tb[TCA_FLOWER_KEY_PORT_DST_MIN],
779 "Invalid destination port range (min must be strictly smaller than max)");
Amritha Nambiar5c722992018-11-12 16:15:55 -0800780 return -EINVAL;
Guillaume Naultbd7d4c12020-03-23 21:48:51 +0100781 }
782 if (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src &&
783 htons(key->tp_range.tp_max.src) <=
784 htons(key->tp_range.tp_min.src)) {
785 NL_SET_ERR_MSG_ATTR(extack,
786 tb[TCA_FLOWER_KEY_PORT_SRC_MIN],
787 "Invalid source port range (min must be strictly smaller than max)");
788 return -EINVAL;
789 }
Amritha Nambiar5c722992018-11-12 16:15:55 -0800790
791 return 0;
792}
793
Guillaume Nault61aec252020-05-26 14:29:04 +0200794static int fl_set_key_mpls_lse(const struct nlattr *nla_lse,
795 struct flow_dissector_key_mpls *key_val,
796 struct flow_dissector_key_mpls *key_mask,
797 struct netlink_ext_ack *extack)
798{
799 struct nlattr *tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1];
800 struct flow_dissector_mpls_lse *lse_mask;
801 struct flow_dissector_mpls_lse *lse_val;
802 u8 lse_index;
803 u8 depth;
804 int err;
805
806 err = nla_parse_nested(tb, TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX, nla_lse,
807 mpls_stack_entry_policy, extack);
808 if (err < 0)
809 return err;
810
811 if (!tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]) {
812 NL_SET_ERR_MSG(extack, "Missing MPLS option \"depth\"");
813 return -EINVAL;
814 }
815
816 depth = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]);
817
818 /* LSE depth starts at 1, for consistency with terminology used by
819 * RFC 3031 (section 3.9), where depth 0 refers to unlabeled packets.
820 */
821 if (depth < 1 || depth > FLOW_DIS_MPLS_MAX) {
822 NL_SET_ERR_MSG_ATTR(extack,
823 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH],
824 "Invalid MPLS depth");
825 return -EINVAL;
826 }
827 lse_index = depth - 1;
828
829 dissector_set_mpls_lse(key_val, lse_index);
830 dissector_set_mpls_lse(key_mask, lse_index);
831
832 lse_val = &key_val->ls[lse_index];
833 lse_mask = &key_mask->ls[lse_index];
834
835 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]) {
836 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]);
837 lse_mask->mpls_ttl = MPLS_TTL_MASK;
838 }
839 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]) {
840 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]);
841
842 if (bos & ~MPLS_BOS_MASK) {
843 NL_SET_ERR_MSG_ATTR(extack,
844 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS],
845 "Bottom Of Stack (BOS) must be 0 or 1");
846 return -EINVAL;
847 }
848 lse_val->mpls_bos = bos;
849 lse_mask->mpls_bos = MPLS_BOS_MASK;
850 }
851 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]) {
852 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]);
853
854 if (tc & ~MPLS_TC_MASK) {
855 NL_SET_ERR_MSG_ATTR(extack,
856 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC],
857 "Traffic Class (TC) must be between 0 and 7");
858 return -EINVAL;
859 }
860 lse_val->mpls_tc = tc;
861 lse_mask->mpls_tc = MPLS_TC_MASK;
862 }
863 if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]) {
864 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]);
865
866 if (label & ~MPLS_LABEL_MASK) {
867 NL_SET_ERR_MSG_ATTR(extack,
868 tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL],
869 "Label must be between 0 and 1048575");
870 return -EINVAL;
871 }
872 lse_val->mpls_label = label;
873 lse_mask->mpls_label = MPLS_LABEL_MASK;
874 }
875
876 return 0;
877}
878
879static int fl_set_key_mpls_opts(const struct nlattr *nla_mpls_opts,
880 struct flow_dissector_key_mpls *key_val,
881 struct flow_dissector_key_mpls *key_mask,
882 struct netlink_ext_ack *extack)
883{
884 struct nlattr *nla_lse;
885 int rem;
886 int err;
887
888 if (!(nla_mpls_opts->nla_type & NLA_F_NESTED)) {
889 NL_SET_ERR_MSG_ATTR(extack, nla_mpls_opts,
890 "NLA_F_NESTED is missing");
891 return -EINVAL;
892 }
893
894 nla_for_each_nested(nla_lse, nla_mpls_opts, rem) {
895 if (nla_type(nla_lse) != TCA_FLOWER_KEY_MPLS_OPTS_LSE) {
896 NL_SET_ERR_MSG_ATTR(extack, nla_lse,
897 "Invalid MPLS option type");
898 return -EINVAL;
899 }
900
901 err = fl_set_key_mpls_lse(nla_lse, key_val, key_mask, extack);
902 if (err < 0)
903 return err;
904 }
905 if (rem) {
906 NL_SET_ERR_MSG(extack,
907 "Bytes leftover after parsing MPLS options");
908 return -EINVAL;
909 }
910
911 return 0;
912}
913
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400914static int fl_set_key_mpls(struct nlattr **tb,
915 struct flow_dissector_key_mpls *key_val,
Guillaume Nault442f7302020-03-23 21:48:49 +0100916 struct flow_dissector_key_mpls *key_mask,
917 struct netlink_ext_ack *extack)
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400918{
Guillaume Nault58cff782020-05-26 14:29:00 +0200919 struct flow_dissector_mpls_lse *lse_mask;
920 struct flow_dissector_mpls_lse *lse_val;
921
Guillaume Nault61aec252020-05-26 14:29:04 +0200922 if (tb[TCA_FLOWER_KEY_MPLS_OPTS]) {
923 if (tb[TCA_FLOWER_KEY_MPLS_TTL] ||
924 tb[TCA_FLOWER_KEY_MPLS_BOS] ||
925 tb[TCA_FLOWER_KEY_MPLS_TC] ||
926 tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
927 NL_SET_ERR_MSG_ATTR(extack,
928 tb[TCA_FLOWER_KEY_MPLS_OPTS],
929 "MPLS label, Traffic Class, Bottom Of Stack and Time To Live must be encapsulated in the MPLS options attribute");
930 return -EBADMSG;
931 }
932
933 return fl_set_key_mpls_opts(tb[TCA_FLOWER_KEY_MPLS_OPTS],
934 key_val, key_mask, extack);
935 }
936
Guillaume Nault58cff782020-05-26 14:29:00 +0200937 lse_val = &key_val->ls[0];
938 lse_mask = &key_mask->ls[0];
939
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400940 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
Guillaume Nault58cff782020-05-26 14:29:00 +0200941 lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
942 lse_mask->mpls_ttl = MPLS_TTL_MASK;
943 dissector_set_mpls_lse(key_val, 0);
944 dissector_set_mpls_lse(key_mask, 0);
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400945 }
946 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400947 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
948
Guillaume Nault442f7302020-03-23 21:48:49 +0100949 if (bos & ~MPLS_BOS_MASK) {
950 NL_SET_ERR_MSG_ATTR(extack,
951 tb[TCA_FLOWER_KEY_MPLS_BOS],
952 "Bottom Of Stack (BOS) must be 0 or 1");
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400953 return -EINVAL;
Guillaume Nault442f7302020-03-23 21:48:49 +0100954 }
Guillaume Nault58cff782020-05-26 14:29:00 +0200955 lse_val->mpls_bos = bos;
956 lse_mask->mpls_bos = MPLS_BOS_MASK;
957 dissector_set_mpls_lse(key_val, 0);
958 dissector_set_mpls_lse(key_mask, 0);
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400959 }
960 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400961 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
962
Guillaume Nault442f7302020-03-23 21:48:49 +0100963 if (tc & ~MPLS_TC_MASK) {
964 NL_SET_ERR_MSG_ATTR(extack,
965 tb[TCA_FLOWER_KEY_MPLS_TC],
966 "Traffic Class (TC) must be between 0 and 7");
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400967 return -EINVAL;
Guillaume Nault442f7302020-03-23 21:48:49 +0100968 }
Guillaume Nault58cff782020-05-26 14:29:00 +0200969 lse_val->mpls_tc = tc;
970 lse_mask->mpls_tc = MPLS_TC_MASK;
971 dissector_set_mpls_lse(key_val, 0);
972 dissector_set_mpls_lse(key_mask, 0);
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400973 }
974 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400975 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
976
Guillaume Nault442f7302020-03-23 21:48:49 +0100977 if (label & ~MPLS_LABEL_MASK) {
978 NL_SET_ERR_MSG_ATTR(extack,
979 tb[TCA_FLOWER_KEY_MPLS_LABEL],
980 "Label must be between 0 and 1048575");
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400981 return -EINVAL;
Guillaume Nault442f7302020-03-23 21:48:49 +0100982 }
Guillaume Nault58cff782020-05-26 14:29:00 +0200983 lse_val->mpls_label = label;
984 lse_mask->mpls_label = MPLS_LABEL_MASK;
985 dissector_set_mpls_lse(key_val, 0);
986 dissector_set_mpls_lse(key_mask, 0);
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400987 }
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400988 return 0;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400989}
990
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300991static void fl_set_key_vlan(struct nlattr **tb,
Jianbo Liuaaab0832018-07-06 05:38:13 +0000992 __be16 ethertype,
Jianbo Liud64efd02018-07-06 05:38:16 +0000993 int vlan_id_key, int vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300994 struct flow_dissector_key_vlan *key_val,
995 struct flow_dissector_key_vlan *key_mask)
996{
997#define VLAN_PRIORITY_MASK 0x7
998
Jianbo Liud64efd02018-07-06 05:38:16 +0000999 if (tb[vlan_id_key]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001000 key_val->vlan_id =
Jianbo Liud64efd02018-07-06 05:38:16 +00001001 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001002 key_mask->vlan_id = VLAN_VID_MASK;
1003 }
Jianbo Liud64efd02018-07-06 05:38:16 +00001004 if (tb[vlan_prio_key]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001005 key_val->vlan_priority =
Jianbo Liud64efd02018-07-06 05:38:16 +00001006 nla_get_u8(tb[vlan_prio_key]) &
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001007 VLAN_PRIORITY_MASK;
1008 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
1009 }
Jianbo Liuaaab0832018-07-06 05:38:13 +00001010 key_val->vlan_tpid = ethertype;
1011 key_mask->vlan_tpid = cpu_to_be16(~0);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001012}
1013
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001014static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
1015 u32 *dissector_key, u32 *dissector_mask,
1016 u32 flower_flag_bit, u32 dissector_flag_bit)
1017{
1018 if (flower_mask & flower_flag_bit) {
1019 *dissector_mask |= dissector_flag_bit;
1020 if (flower_key & flower_flag_bit)
1021 *dissector_key |= dissector_flag_bit;
1022 }
1023}
1024
Guillaume Naulte304e212020-03-23 21:48:53 +01001025static int fl_set_key_flags(struct nlattr **tb, u32 *flags_key,
1026 u32 *flags_mask, struct netlink_ext_ack *extack)
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001027{
1028 u32 key, mask;
1029
Or Gerlitzd9724772016-12-22 14:28:15 +02001030 /* mask is mandatory for flags */
Guillaume Naulte304e212020-03-23 21:48:53 +01001031 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) {
1032 NL_SET_ERR_MSG(extack, "Missing flags mask");
Or Gerlitzd9724772016-12-22 14:28:15 +02001033 return -EINVAL;
Guillaume Naulte304e212020-03-23 21:48:53 +01001034 }
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001035
1036 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
Or Gerlitzd9724772016-12-22 14:28:15 +02001037 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001038
1039 *flags_key = 0;
1040 *flags_mask = 0;
1041
1042 fl_set_key_flag(key, mask, flags_key, flags_mask,
1043 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +01001044 fl_set_key_flag(key, mask, flags_key, flags_mask,
1045 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1046 FLOW_DIS_FIRST_FRAG);
Or Gerlitzd9724772016-12-22 14:28:15 +02001047
1048 return 0;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001049}
1050
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001051static void fl_set_key_ip(struct nlattr **tb, bool encap,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001052 struct flow_dissector_key_ip *key,
1053 struct flow_dissector_key_ip *mask)
1054{
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001055 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1056 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1057 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1058 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001059
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001060 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
1061 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001062}
1063
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001064static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
1065 int depth, int option_len,
1066 struct netlink_ext_ack *extack)
1067{
1068 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
1069 struct nlattr *class = NULL, *type = NULL, *data = NULL;
1070 struct geneve_opt *opt;
1071 int err, data_len = 0;
1072
1073 if (option_len > sizeof(struct geneve_opt))
1074 data_len = option_len - sizeof(struct geneve_opt);
1075
1076 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
1077 memset(opt, 0xff, option_len);
1078 opt->length = data_len / 4;
1079 opt->r1 = 0;
1080 opt->r2 = 0;
1081 opt->r3 = 0;
1082
1083 /* If no mask has been prodived we assume an exact match. */
1084 if (!depth)
1085 return sizeof(struct geneve_opt) + data_len;
1086
1087 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
1088 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
1089 return -EINVAL;
1090 }
1091
Johannes Berg8cb08172019-04-26 14:07:28 +02001092 err = nla_parse_nested_deprecated(tb,
1093 TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
1094 nla, geneve_opt_policy, extack);
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001095 if (err < 0)
1096 return err;
1097
1098 /* We are not allowed to omit any of CLASS, TYPE or DATA
1099 * fields from the key.
1100 */
1101 if (!option_len &&
1102 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
1103 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
1104 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
1105 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
1106 return -EINVAL;
1107 }
1108
1109 /* Omitting any of CLASS, TYPE or DATA fields is allowed
1110 * for the mask.
1111 */
1112 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
1113 int new_len = key->enc_opts.len;
1114
1115 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
1116 data_len = nla_len(data);
1117 if (data_len < 4) {
1118 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
1119 return -ERANGE;
1120 }
1121 if (data_len % 4) {
1122 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
1123 return -ERANGE;
1124 }
1125
1126 new_len += sizeof(struct geneve_opt) + data_len;
1127 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
1128 if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
1129 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
1130 return -ERANGE;
1131 }
1132 opt->length = data_len / 4;
1133 memcpy(opt->opt_data, nla_data(data), data_len);
1134 }
1135
1136 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
1137 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
1138 opt->opt_class = nla_get_be16(class);
1139 }
1140
1141 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
1142 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
1143 opt->type = nla_get_u8(type);
1144 }
1145
1146 return sizeof(struct geneve_opt) + data_len;
1147}
1148
Xin Longd8f9dfa2019-11-21 18:03:28 +08001149static int fl_set_vxlan_opt(const struct nlattr *nla, struct fl_flow_key *key,
1150 int depth, int option_len,
1151 struct netlink_ext_ack *extack)
1152{
1153 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1];
1154 struct vxlan_metadata *md;
1155 int err;
1156
1157 md = (struct vxlan_metadata *)&key->enc_opts.data[key->enc_opts.len];
1158 memset(md, 0xff, sizeof(*md));
1159
1160 if (!depth)
1161 return sizeof(*md);
1162
1163 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_VXLAN) {
1164 NL_SET_ERR_MSG(extack, "Non-vxlan option type for mask");
1165 return -EINVAL;
1166 }
1167
1168 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, nla,
1169 vxlan_opt_policy, extack);
1170 if (err < 0)
1171 return err;
1172
1173 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
1174 NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp");
1175 return -EINVAL;
1176 }
1177
Xin Long13e6ce92020-09-13 19:51:50 +08001178 if (tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) {
Xin Longd8f9dfa2019-11-21 18:03:28 +08001179 md->gbp = nla_get_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]);
Xin Long13e6ce92020-09-13 19:51:50 +08001180 md->gbp &= VXLAN_GBP_MASK;
1181 }
Xin Longd8f9dfa2019-11-21 18:03:28 +08001182
1183 return sizeof(*md);
1184}
1185
Xin Long79b10112019-11-21 18:03:29 +08001186static int fl_set_erspan_opt(const struct nlattr *nla, struct fl_flow_key *key,
1187 int depth, int option_len,
1188 struct netlink_ext_ack *extack)
1189{
1190 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1];
1191 struct erspan_metadata *md;
1192 int err;
1193
1194 md = (struct erspan_metadata *)&key->enc_opts.data[key->enc_opts.len];
1195 memset(md, 0xff, sizeof(*md));
1196 md->version = 1;
1197
1198 if (!depth)
1199 return sizeof(*md);
1200
1201 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_ERSPAN) {
1202 NL_SET_ERR_MSG(extack, "Non-erspan option type for mask");
1203 return -EINVAL;
1204 }
1205
1206 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX, nla,
1207 erspan_opt_policy, extack);
1208 if (err < 0)
1209 return err;
1210
1211 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) {
1212 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver");
1213 return -EINVAL;
1214 }
1215
1216 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER])
1217 md->version = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]);
1218
1219 if (md->version == 1) {
1220 if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
1221 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index");
1222 return -EINVAL;
1223 }
1224 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) {
1225 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX];
Xin Long8e1b3ac2020-09-13 19:43:03 +08001226 memset(&md->u, 0x00, sizeof(md->u));
Xin Long79b10112019-11-21 18:03:29 +08001227 md->u.index = nla_get_be32(nla);
1228 }
1229 } else if (md->version == 2) {
1230 if (!option_len && (!tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] ||
1231 !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID])) {
1232 NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid");
1233 return -EINVAL;
1234 }
1235 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]) {
1236 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR];
1237 md->u.md2.dir = nla_get_u8(nla);
1238 }
1239 if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]) {
1240 nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID];
1241 set_hwid(&md->u.md2, nla_get_u8(nla));
1242 }
1243 } else {
1244 NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect");
1245 return -EINVAL;
1246 }
1247
1248 return sizeof(*md);
1249}
1250
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001251static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
1252 struct fl_flow_key *mask,
1253 struct netlink_ext_ack *extack)
1254{
1255 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
Jakub Kicinski63c82992018-11-09 21:06:26 -08001256 int err, option_len, key_depth, msk_depth = 0;
1257
Johannes Berg8cb08172019-04-26 14:07:28 +02001258 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS],
1259 TCA_FLOWER_KEY_ENC_OPTS_MAX,
1260 enc_opts_policy, extack);
Jakub Kicinski63c82992018-11-09 21:06:26 -08001261 if (err)
1262 return err;
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001263
1264 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
1265
1266 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
Johannes Berg8cb08172019-04-26 14:07:28 +02001267 err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
1268 TCA_FLOWER_KEY_ENC_OPTS_MAX,
1269 enc_opts_policy, extack);
Jakub Kicinski63c82992018-11-09 21:06:26 -08001270 if (err)
1271 return err;
1272
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001273 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
1274 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
Cong Wangc96adff2021-01-15 10:50:24 -08001275 if (!nla_ok(nla_opt_msk, msk_depth)) {
1276 NL_SET_ERR_MSG(extack, "Invalid nested attribute for masks");
1277 return -EINVAL;
1278 }
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001279 }
1280
1281 nla_for_each_attr(nla_opt_key, nla_enc_key,
1282 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
1283 switch (nla_type(nla_opt_key)) {
1284 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
Xin Longd8f9dfa2019-11-21 18:03:28 +08001285 if (key->enc_opts.dst_opt_type &&
1286 key->enc_opts.dst_opt_type != TUNNEL_GENEVE_OPT) {
1287 NL_SET_ERR_MSG(extack, "Duplicate type for geneve options");
1288 return -EINVAL;
1289 }
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001290 option_len = 0;
1291 key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
1292 option_len = fl_set_geneve_opt(nla_opt_key, key,
1293 key_depth, option_len,
1294 extack);
1295 if (option_len < 0)
1296 return option_len;
1297
1298 key->enc_opts.len += option_len;
1299 /* At the same time we need to parse through the mask
1300 * in order to verify exact and mask attribute lengths.
1301 */
1302 mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
1303 option_len = fl_set_geneve_opt(nla_opt_msk, mask,
1304 msk_depth, option_len,
1305 extack);
1306 if (option_len < 0)
1307 return option_len;
1308
1309 mask->enc_opts.len += option_len;
1310 if (key->enc_opts.len != mask->enc_opts.len) {
1311 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1312 return -EINVAL;
1313 }
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001314 break;
Xin Longd8f9dfa2019-11-21 18:03:28 +08001315 case TCA_FLOWER_KEY_ENC_OPTS_VXLAN:
1316 if (key->enc_opts.dst_opt_type) {
1317 NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options");
1318 return -EINVAL;
1319 }
1320 option_len = 0;
1321 key->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT;
1322 option_len = fl_set_vxlan_opt(nla_opt_key, key,
1323 key_depth, option_len,
1324 extack);
1325 if (option_len < 0)
1326 return option_len;
1327
1328 key->enc_opts.len += option_len;
1329 /* At the same time we need to parse through the mask
1330 * in order to verify exact and mask attribute lengths.
1331 */
1332 mask->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT;
1333 option_len = fl_set_vxlan_opt(nla_opt_msk, mask,
1334 msk_depth, option_len,
1335 extack);
1336 if (option_len < 0)
1337 return option_len;
1338
1339 mask->enc_opts.len += option_len;
1340 if (key->enc_opts.len != mask->enc_opts.len) {
1341 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1342 return -EINVAL;
1343 }
Xin Longd8f9dfa2019-11-21 18:03:28 +08001344 break;
Xin Long79b10112019-11-21 18:03:29 +08001345 case TCA_FLOWER_KEY_ENC_OPTS_ERSPAN:
1346 if (key->enc_opts.dst_opt_type) {
1347 NL_SET_ERR_MSG(extack, "Duplicate type for erspan options");
1348 return -EINVAL;
1349 }
1350 option_len = 0;
1351 key->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT;
1352 option_len = fl_set_erspan_opt(nla_opt_key, key,
1353 key_depth, option_len,
1354 extack);
1355 if (option_len < 0)
1356 return option_len;
1357
1358 key->enc_opts.len += option_len;
1359 /* At the same time we need to parse through the mask
1360 * in order to verify exact and mask attribute lengths.
1361 */
1362 mask->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT;
1363 option_len = fl_set_erspan_opt(nla_opt_msk, mask,
1364 msk_depth, option_len,
1365 extack);
1366 if (option_len < 0)
1367 return option_len;
1368
1369 mask->enc_opts.len += option_len;
1370 if (key->enc_opts.len != mask->enc_opts.len) {
1371 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
1372 return -EINVAL;
1373 }
Xin Long79b10112019-11-21 18:03:29 +08001374 break;
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001375 default:
1376 NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
1377 return -EINVAL;
1378 }
Cong Wangc96adff2021-01-15 10:50:24 -08001379
1380 if (!msk_depth)
1381 continue;
1382
1383 if (!nla_ok(nla_opt_msk, msk_depth)) {
1384 NL_SET_ERR_MSG(extack, "A mask attribute is invalid");
1385 return -EINVAL;
1386 }
1387 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001388 }
1389
1390 return 0;
1391}
1392
Paul Blakeye0ace682019-07-09 10:30:50 +03001393static int fl_set_key_ct(struct nlattr **tb,
1394 struct flow_dissector_key_ct *key,
1395 struct flow_dissector_key_ct *mask,
1396 struct netlink_ext_ack *extack)
1397{
1398 if (tb[TCA_FLOWER_KEY_CT_STATE]) {
1399 if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) {
1400 NL_SET_ERR_MSG(extack, "Conntrack isn't enabled");
1401 return -EOPNOTSUPP;
1402 }
1403 fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
1404 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
1405 sizeof(key->ct_state));
1406 }
1407 if (tb[TCA_FLOWER_KEY_CT_ZONE]) {
1408 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) {
1409 NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled");
1410 return -EOPNOTSUPP;
1411 }
1412 fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
1413 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
1414 sizeof(key->ct_zone));
1415 }
1416 if (tb[TCA_FLOWER_KEY_CT_MARK]) {
1417 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) {
1418 NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled");
1419 return -EOPNOTSUPP;
1420 }
1421 fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
1422 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
1423 sizeof(key->ct_mark));
1424 }
1425 if (tb[TCA_FLOWER_KEY_CT_LABELS]) {
1426 if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) {
1427 NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled");
1428 return -EOPNOTSUPP;
1429 }
1430 fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
1431 mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
1432 sizeof(key->ct_labels));
1433 }
1434
1435 return 0;
1436}
1437
Jiri Pirko77b99002015-05-12 14:56:21 +02001438static int fl_set_key(struct net *net, struct nlattr **tb,
Alexander Aring1057c552018-01-18 11:20:54 -05001439 struct fl_flow_key *key, struct fl_flow_key *mask,
1440 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001441{
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001442 __be16 ethertype;
Or Gerlitzd9724772016-12-22 14:28:15 +02001443 int ret = 0;
Jiri Pirkoa5148622019-06-15 11:03:49 +02001444
Jiri Pirko77b99002015-05-12 14:56:21 +02001445 if (tb[TCA_FLOWER_INDEV]) {
Alexander Aring1057c552018-01-18 11:20:54 -05001446 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
Jiri Pirko77b99002015-05-12 14:56:21 +02001447 if (err < 0)
1448 return err;
Jiri Pirko8212ed72019-06-19 09:41:03 +03001449 key->meta.ingress_ifindex = err;
1450 mask->meta.ingress_ifindex = 0xffffffff;
Jiri Pirko77b99002015-05-12 14:56:21 +02001451 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001452
1453 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1454 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1455 sizeof(key->eth.dst));
1456 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1457 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1458 sizeof(key->eth.src));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -05001459
Arnd Bergmann0b498a52016-08-26 17:25:45 +02001460 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001461 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
1462
Jianbo Liuaaab0832018-07-06 05:38:13 +00001463 if (eth_type_vlan(ethertype)) {
Jianbo Liud64efd02018-07-06 05:38:16 +00001464 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
1465 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan,
1466 &mask->vlan);
1467
Jianbo Liu5e9a0fe2018-07-09 02:26:20 +00001468 if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
1469 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
1470 if (eth_type_vlan(ethertype)) {
1471 fl_set_key_vlan(tb, ethertype,
1472 TCA_FLOWER_KEY_CVLAN_ID,
1473 TCA_FLOWER_KEY_CVLAN_PRIO,
1474 &key->cvlan, &mask->cvlan);
1475 fl_set_key_val(tb, &key->basic.n_proto,
1476 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1477 &mask->basic.n_proto,
1478 TCA_FLOWER_UNSPEC,
1479 sizeof(key->basic.n_proto));
1480 } else {
1481 key->basic.n_proto = ethertype;
1482 mask->basic.n_proto = cpu_to_be16(~0);
1483 }
Jianbo Liud64efd02018-07-06 05:38:16 +00001484 }
Arnd Bergmann0b498a52016-08-26 17:25:45 +02001485 } else {
1486 key->basic.n_proto = ethertype;
1487 mask->basic.n_proto = cpu_to_be16(~0);
1488 }
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001489 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -05001490
Jiri Pirko77b99002015-05-12 14:56:21 +02001491 if (key->basic.n_proto == htons(ETH_P_IP) ||
1492 key->basic.n_proto == htons(ETH_P_IPV6)) {
1493 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1494 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1495 sizeof(key->basic.ip_proto));
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001496 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
Jiri Pirko77b99002015-05-12 14:56:21 +02001497 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -05001498
1499 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
1500 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +02001501 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +02001502 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1503 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1504 sizeof(key->ipv4.src));
1505 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1506 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1507 sizeof(key->ipv4.dst));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -05001508 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
1509 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +02001510 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +02001511 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1512 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1513 sizeof(key->ipv6.src));
1514 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1515 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1516 sizeof(key->ipv6.dst));
1517 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -05001518
Jiri Pirko77b99002015-05-12 14:56:21 +02001519 if (key->basic.ip_proto == IPPROTO_TCP) {
1520 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001521 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001522 sizeof(key->tp.src));
1523 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001524 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001525 sizeof(key->tp.dst));
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001526 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1527 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1528 sizeof(key->tcp.flags));
Jiri Pirko77b99002015-05-12 14:56:21 +02001529 } else if (key->basic.ip_proto == IPPROTO_UDP) {
1530 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001531 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001532 sizeof(key->tp.src));
1533 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001534 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001535 sizeof(key->tp.dst));
Simon Horman5976c5f2016-11-03 13:24:21 +01001536 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
1537 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1538 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1539 sizeof(key->tp.src));
1540 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1541 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1542 sizeof(key->tp.dst));
Simon Horman7b684882016-12-07 13:48:28 +01001543 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
1544 key->basic.ip_proto == IPPROTO_ICMP) {
1545 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
1546 &mask->icmp.type,
1547 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1548 sizeof(key->icmp.type));
1549 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
1550 &mask->icmp.code,
1551 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1552 sizeof(key->icmp.code));
1553 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1554 key->basic.ip_proto == IPPROTO_ICMPV6) {
1555 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
1556 &mask->icmp.type,
1557 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1558 sizeof(key->icmp.type));
Simon Horman040587a2017-01-30 16:19:02 +01001559 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
Simon Horman7b684882016-12-07 13:48:28 +01001560 &mask->icmp.code,
Simon Horman040587a2017-01-30 16:19:02 +01001561 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
Simon Horman7b684882016-12-07 13:48:28 +01001562 sizeof(key->icmp.code));
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001563 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
1564 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
Guillaume Nault442f7302020-03-23 21:48:49 +01001565 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls, extack);
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -04001566 if (ret)
1567 return ret;
Simon Horman99d31322017-01-11 14:05:43 +01001568 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
1569 key->basic.n_proto == htons(ETH_P_RARP)) {
1570 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
1571 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
1572 sizeof(key->arp.sip));
1573 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
1574 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
1575 sizeof(key->arp.tip));
1576 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
1577 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
1578 sizeof(key->arp.op));
1579 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1580 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1581 sizeof(key->arp.sha));
1582 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1583 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1584 sizeof(key->arp.tha));
Jiri Pirko77b99002015-05-12 14:56:21 +02001585 }
1586
Amritha Nambiar5c722992018-11-12 16:15:55 -08001587 if (key->basic.ip_proto == IPPROTO_TCP ||
1588 key->basic.ip_proto == IPPROTO_UDP ||
1589 key->basic.ip_proto == IPPROTO_SCTP) {
Guillaume Naultbd7d4c12020-03-23 21:48:51 +01001590 ret = fl_set_key_port_range(tb, key, mask, extack);
Amritha Nambiar5c722992018-11-12 16:15:55 -08001591 if (ret)
1592 return ret;
1593 }
1594
Amir Vadaibc3103f2016-09-08 16:23:47 +03001595 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
1596 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
1597 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +02001598 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +03001599 fl_set_key_val(tb, &key->enc_ipv4.src,
1600 TCA_FLOWER_KEY_ENC_IPV4_SRC,
1601 &mask->enc_ipv4.src,
1602 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1603 sizeof(key->enc_ipv4.src));
1604 fl_set_key_val(tb, &key->enc_ipv4.dst,
1605 TCA_FLOWER_KEY_ENC_IPV4_DST,
1606 &mask->enc_ipv4.dst,
1607 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1608 sizeof(key->enc_ipv4.dst));
1609 }
1610
1611 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
1612 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
1613 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +02001614 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +03001615 fl_set_key_val(tb, &key->enc_ipv6.src,
1616 TCA_FLOWER_KEY_ENC_IPV6_SRC,
1617 &mask->enc_ipv6.src,
1618 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1619 sizeof(key->enc_ipv6.src));
1620 fl_set_key_val(tb, &key->enc_ipv6.dst,
1621 TCA_FLOWER_KEY_ENC_IPV6_DST,
1622 &mask->enc_ipv6.dst,
1623 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1624 sizeof(key->enc_ipv6.dst));
1625 }
1626
1627 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001628 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
Amir Vadaibc3103f2016-09-08 16:23:47 +03001629 sizeof(key->enc_key_id.keyid));
1630
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001631 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1632 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1633 sizeof(key->enc_tp.src));
1634
1635 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1636 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1637 sizeof(key->enc_tp.dst));
1638
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001639 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
1640
Ariel Levkovich5923b8f2020-07-23 01:03:01 +03001641 fl_set_key_val(tb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
1642 &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
1643 sizeof(key->hash.hash));
1644
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001645 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
1646 ret = fl_set_enc_opt(tb, key, mask, extack);
1647 if (ret)
1648 return ret;
1649 }
1650
Paul Blakeye0ace682019-07-09 10:30:50 +03001651 ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack);
1652 if (ret)
1653 return ret;
1654
Or Gerlitzd9724772016-12-22 14:28:15 +02001655 if (tb[TCA_FLOWER_KEY_FLAGS])
Guillaume Naulte304e212020-03-23 21:48:53 +01001656 ret = fl_set_key_flags(tb, &key->control.flags,
1657 &mask->control.flags, extack);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001658
Or Gerlitzd9724772016-12-22 14:28:15 +02001659 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +02001660}
1661
Paul Blakey05cd2712018-04-30 14:28:30 +03001662static void fl_mask_copy(struct fl_flow_mask *dst,
1663 struct fl_flow_mask *src)
Jiri Pirko77b99002015-05-12 14:56:21 +02001664{
Paul Blakey05cd2712018-04-30 14:28:30 +03001665 const void *psrc = fl_key_get_start(&src->key, src);
1666 void *pdst = fl_key_get_start(&dst->key, src);
Jiri Pirko77b99002015-05-12 14:56:21 +02001667
Paul Blakey05cd2712018-04-30 14:28:30 +03001668 memcpy(pdst, psrc, fl_mask_range(src));
1669 dst->range = src->range;
Jiri Pirko77b99002015-05-12 14:56:21 +02001670}
1671
1672static const struct rhashtable_params fl_ht_params = {
1673 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
1674 .head_offset = offsetof(struct cls_fl_filter, ht_node),
1675 .automatic_shrinking = true,
1676};
1677
Paul Blakey05cd2712018-04-30 14:28:30 +03001678static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +02001679{
Paul Blakey05cd2712018-04-30 14:28:30 +03001680 mask->filter_ht_params = fl_ht_params;
1681 mask->filter_ht_params.key_len = fl_mask_range(mask);
1682 mask->filter_ht_params.key_offset += mask->range.start;
Jiri Pirko77b99002015-05-12 14:56:21 +02001683
Paul Blakey05cd2712018-04-30 14:28:30 +03001684 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
Jiri Pirko77b99002015-05-12 14:56:21 +02001685}
1686
1687#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
Pankaj Bharadiyac5936422019-12-09 10:31:43 -08001688#define FL_KEY_MEMBER_SIZE(member) sizeof_field(struct fl_flow_key, member)
Jiri Pirko77b99002015-05-12 14:56:21 +02001689
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001690#define FL_KEY_IS_MASKED(mask, member) \
1691 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
1692 0, FL_KEY_MEMBER_SIZE(member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +02001693
1694#define FL_KEY_SET(keys, cnt, id, member) \
1695 do { \
1696 keys[cnt].key_id = id; \
1697 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
1698 cnt++; \
1699 } while(0);
1700
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001701#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
Jiri Pirko77b99002015-05-12 14:56:21 +02001702 do { \
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001703 if (FL_KEY_IS_MASKED(mask, member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +02001704 FL_KEY_SET(keys, cnt, id, member); \
1705 } while(0);
1706
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001707static void fl_init_dissector(struct flow_dissector *dissector,
1708 struct fl_flow_key *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +02001709{
1710 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
1711 size_t cnt = 0;
1712
Jiri Pirko8212ed72019-06-19 09:41:03 +03001713 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1714 FLOW_DISSECTOR_KEY_META, meta);
Tom Herbert42aecaa2015-06-04 09:16:39 -07001715 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
Jiri Pirko77b99002015-05-12 14:56:21 +02001716 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001717 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001718 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001719 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001720 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001721 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001722 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
Yoshiki Komachi8ffb0552019-12-03 19:40:12 +09001723 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1724 FLOW_DISSECTOR_KEY_PORTS, tp);
1725 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1726 FLOW_DISSECTOR_KEY_PORTS_RANGE, tp_range);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001727 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001728 FLOW_DISSECTOR_KEY_IP, ip);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001729 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001730 FLOW_DISSECTOR_KEY_TCP, tcp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001731 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Simon Horman7b684882016-12-07 13:48:28 +01001732 FLOW_DISSECTOR_KEY_ICMP, icmp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001733 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Simon Horman99d31322017-01-11 14:05:43 +01001734 FLOW_DISSECTOR_KEY_ARP, arp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001735 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001736 FLOW_DISSECTOR_KEY_MPLS, mpls);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001737 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001738 FLOW_DISSECTOR_KEY_VLAN, vlan);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001739 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Jianbo Liud64efd02018-07-06 05:38:16 +00001740 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001741 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +02001742 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001743 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +02001744 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001745 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +02001746 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001747 if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
1748 FL_KEY_IS_MASKED(mask, enc_ipv6))
Hadar Hen Zion519d1052016-11-07 15:14:38 +02001749 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
1750 enc_control);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001751 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001752 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001753 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001754 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001755 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1756 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
Paul Blakeye0ace682019-07-09 10:30:50 +03001757 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1758 FLOW_DISSECTOR_KEY_CT, ct);
Ariel Levkovich5923b8f2020-07-23 01:03:01 +03001759 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1760 FLOW_DISSECTOR_KEY_HASH, hash);
Jiri Pirko77b99002015-05-12 14:56:21 +02001761
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001762 skb_flow_dissector_init(dissector, keys, cnt);
Paul Blakey05cd2712018-04-30 14:28:30 +03001763}
1764
1765static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
1766 struct fl_flow_mask *mask)
1767{
1768 struct fl_flow_mask *newmask;
1769 int err;
1770
1771 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
1772 if (!newmask)
1773 return ERR_PTR(-ENOMEM);
1774
1775 fl_mask_copy(newmask, mask);
1776
Yoshiki Komachi8ffb0552019-12-03 19:40:12 +09001777 if ((newmask->key.tp_range.tp_min.dst &&
1778 newmask->key.tp_range.tp_max.dst) ||
1779 (newmask->key.tp_range.tp_min.src &&
1780 newmask->key.tp_range.tp_max.src))
Amritha Nambiar5c722992018-11-12 16:15:55 -08001781 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
1782
Paul Blakey05cd2712018-04-30 14:28:30 +03001783 err = fl_init_mask_hashtable(newmask);
1784 if (err)
1785 goto errout_free;
1786
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001787 fl_init_dissector(&newmask->dissector, &newmask->key);
Paul Blakey05cd2712018-04-30 14:28:30 +03001788
1789 INIT_LIST_HEAD_RCU(&newmask->filters);
1790
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001791 refcount_set(&newmask->refcnt, 1);
Vlad Buslov195c2342019-03-21 15:17:38 +02001792 err = rhashtable_replace_fast(&head->ht, &mask->ht_node,
1793 &newmask->ht_node, mask_ht_params);
Paul Blakey05cd2712018-04-30 14:28:30 +03001794 if (err)
1795 goto errout_destroy;
1796
Vlad Buslov259e60f2019-03-21 15:17:39 +02001797 spin_lock(&head->masks_lock);
Paul Blakey05cd2712018-04-30 14:28:30 +03001798 list_add_tail_rcu(&newmask->list, &head->masks);
Vlad Buslov259e60f2019-03-21 15:17:39 +02001799 spin_unlock(&head->masks_lock);
Paul Blakey05cd2712018-04-30 14:28:30 +03001800
1801 return newmask;
1802
1803errout_destroy:
1804 rhashtable_destroy(&newmask->ht);
1805errout_free:
1806 kfree(newmask);
1807
1808 return ERR_PTR(err);
Jiri Pirko77b99002015-05-12 14:56:21 +02001809}
1810
1811static int fl_check_assign_mask(struct cls_fl_head *head,
Paul Blakey05cd2712018-04-30 14:28:30 +03001812 struct cls_fl_filter *fnew,
1813 struct cls_fl_filter *fold,
Jiri Pirko77b99002015-05-12 14:56:21 +02001814 struct fl_flow_mask *mask)
1815{
Paul Blakey05cd2712018-04-30 14:28:30 +03001816 struct fl_flow_mask *newmask;
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001817 int ret = 0;
Jiri Pirko77b99002015-05-12 14:56:21 +02001818
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001819 rcu_read_lock();
Vlad Buslov195c2342019-03-21 15:17:38 +02001820
1821 /* Insert mask as temporary node to prevent concurrent creation of mask
1822 * with same key. Any concurrent lookups with same key will return
Vlad Buslov99815f52019-06-13 17:54:04 +03001823 * -EAGAIN because mask's refcnt is zero.
Vlad Buslov195c2342019-03-21 15:17:38 +02001824 */
1825 fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht,
1826 &mask->ht_node,
1827 mask_ht_params);
Paul Blakey05cd2712018-04-30 14:28:30 +03001828 if (!fnew->mask) {
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001829 rcu_read_unlock();
1830
Vlad Buslov195c2342019-03-21 15:17:38 +02001831 if (fold) {
1832 ret = -EINVAL;
1833 goto errout_cleanup;
1834 }
Paul Blakey05cd2712018-04-30 14:28:30 +03001835
1836 newmask = fl_create_new_mask(head, mask);
Vlad Buslov195c2342019-03-21 15:17:38 +02001837 if (IS_ERR(newmask)) {
1838 ret = PTR_ERR(newmask);
1839 goto errout_cleanup;
1840 }
Paul Blakey05cd2712018-04-30 14:28:30 +03001841
1842 fnew->mask = newmask;
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001843 return 0;
Vlad Buslov195c2342019-03-21 15:17:38 +02001844 } else if (IS_ERR(fnew->mask)) {
1845 ret = PTR_ERR(fnew->mask);
Paul Blakeyf6521c52018-06-03 10:06:14 +03001846 } else if (fold && fold->mask != fnew->mask) {
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001847 ret = -EINVAL;
1848 } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) {
1849 /* Mask was deleted concurrently, try again */
1850 ret = -EAGAIN;
Jiri Pirko77b99002015-05-12 14:56:21 +02001851 }
Vlad Buslovf48ef4d2019-03-21 15:17:37 +02001852 rcu_read_unlock();
1853 return ret;
Vlad Buslov195c2342019-03-21 15:17:38 +02001854
1855errout_cleanup:
1856 rhashtable_remove_fast(&head->ht, &mask->ht_node,
1857 mask_ht_params);
Vlad Buslov195c2342019-03-21 15:17:38 +02001858 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +02001859}
1860
1861static int fl_set_parms(struct net *net, struct tcf_proto *tp,
1862 struct cls_fl_filter *f, struct fl_flow_mask *mask,
1863 unsigned long base, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05001864 struct nlattr *est, bool ovr,
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001865 struct fl_flow_tmplt *tmplt, bool rtnl_held,
Alexander Aring50a56192018-01-18 11:20:52 -05001866 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001867{
Jiri Pirko77b99002015-05-12 14:56:21 +02001868 int err;
1869
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001870 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, rtnl_held,
Vlad Buslovec6743a2019-02-11 10:55:43 +02001871 extack);
Jiri Pirko77b99002015-05-12 14:56:21 +02001872 if (err < 0)
1873 return err;
1874
1875 if (tb[TCA_FLOWER_CLASSID]) {
1876 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001877 if (!rtnl_held)
1878 rtnl_lock();
Jiri Pirko77b99002015-05-12 14:56:21 +02001879 tcf_bind_filter(tp, &f->res, base);
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001880 if (!rtnl_held)
1881 rtnl_unlock();
Jiri Pirko77b99002015-05-12 14:56:21 +02001882 }
1883
Alexander Aring1057c552018-01-18 11:20:54 -05001884 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +02001885 if (err)
Jiri Pirko45507522017-08-04 14:29:06 +02001886 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +02001887
1888 fl_mask_update_range(mask);
1889 fl_set_masked_key(&f->mkey, &f->key, mask);
1890
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001891 if (!fl_mask_fits_tmplt(tmplt, mask)) {
1892 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
1893 return -EINVAL;
1894 }
1895
Jiri Pirko77b99002015-05-12 14:56:21 +02001896 return 0;
Jiri Pirko77b99002015-05-12 14:56:21 +02001897}
1898
Vlad Buslov1f17f772019-04-05 20:56:26 +03001899static int fl_ht_insert_unique(struct cls_fl_filter *fnew,
1900 struct cls_fl_filter *fold,
1901 bool *in_ht)
1902{
1903 struct fl_flow_mask *mask = fnew->mask;
1904 int err;
1905
Vlad Buslov9e355522019-04-11 19:12:20 +03001906 err = rhashtable_lookup_insert_fast(&mask->ht,
1907 &fnew->ht_node,
1908 mask->filter_ht_params);
Vlad Buslov1f17f772019-04-05 20:56:26 +03001909 if (err) {
1910 *in_ht = false;
1911 /* It is okay if filter with same key exists when
1912 * overwriting.
1913 */
1914 return fold && err == -EEXIST ? 0 : err;
1915 }
1916
1917 *in_ht = true;
1918 return 0;
1919}
1920
Jiri Pirko77b99002015-05-12 14:56:21 +02001921static int fl_change(struct net *net, struct sk_buff *in_skb,
1922 struct tcf_proto *tp, unsigned long base,
1923 u32 handle, struct nlattr **tca,
Vlad Buslov12db03b2019-02-11 10:55:45 +02001924 void **arg, bool ovr, bool rtnl_held,
1925 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001926{
Vlad Buslove4746192019-03-21 15:17:33 +02001927 struct cls_fl_head *head = fl_head_dereference(tp);
WANG Cong8113c092017-08-04 21:31:43 -07001928 struct cls_fl_filter *fold = *arg;
Jiri Pirko77b99002015-05-12 14:56:21 +02001929 struct cls_fl_filter *fnew;
Ivan Vecera2cddd202019-01-16 16:53:52 +01001930 struct fl_flow_mask *mask;
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001931 struct nlattr **tb;
Vlad Buslov1f17f772019-04-05 20:56:26 +03001932 bool in_ht;
Jiri Pirko77b99002015-05-12 14:56:21 +02001933 int err;
1934
Vlad Buslov06177552019-03-21 15:17:35 +02001935 if (!tca[TCA_OPTIONS]) {
1936 err = -EINVAL;
1937 goto errout_fold;
1938 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001939
Ivan Vecera2cddd202019-01-16 16:53:52 +01001940 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
Vlad Buslov06177552019-03-21 15:17:35 +02001941 if (!mask) {
1942 err = -ENOBUFS;
1943 goto errout_fold;
1944 }
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001945
Ivan Vecera2cddd202019-01-16 16:53:52 +01001946 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1947 if (!tb) {
1948 err = -ENOBUFS;
1949 goto errout_mask_alloc;
1950 }
1951
Johannes Berg8cb08172019-04-26 14:07:28 +02001952 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
1953 tca[TCA_OPTIONS], fl_policy, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +02001954 if (err < 0)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001955 goto errout_tb;
Jiri Pirko77b99002015-05-12 14:56:21 +02001956
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001957 if (fold && handle && fold->handle != handle) {
1958 err = -EINVAL;
1959 goto errout_tb;
1960 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001961
1962 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001963 if (!fnew) {
1964 err = -ENOBUFS;
1965 goto errout_tb;
1966 }
Vlad Buslovc049d562019-04-24 09:53:31 +03001967 INIT_LIST_HEAD(&fnew->hw_list);
Vlad Buslov06177552019-03-21 15:17:35 +02001968 refcount_set(&fnew->refcnt, 1);
Jiri Pirko77b99002015-05-12 14:56:21 +02001969
Cong Wang14215102019-02-20 21:37:42 -08001970 err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0);
WANG Congb9a24bb2016-08-19 12:36:54 -07001971 if (err < 0)
1972 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +02001973
Vlad Buslovecb3dea2019-03-06 16:22:12 +02001974 if (tb[TCA_FLOWER_FLAGS]) {
1975 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
1976
1977 if (!tc_flags_valid(fnew->flags)) {
1978 err = -EINVAL;
1979 goto errout;
1980 }
1981 }
1982
1983 err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr,
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001984 tp->chain->tmplt_priv, rtnl_held, extack);
Vlad Buslovecb3dea2019-03-06 16:22:12 +02001985 if (err)
1986 goto errout;
1987
1988 err = fl_check_assign_mask(head, fnew, fold, mask);
1989 if (err)
1990 goto errout;
1991
Vlad Buslov1f17f772019-04-05 20:56:26 +03001992 err = fl_ht_insert_unique(fnew, fold, &in_ht);
1993 if (err)
1994 goto errout_mask;
1995
Hadar Hen Zion79685212016-12-01 14:06:34 +02001996 if (!tc_skip_hw(fnew->flags)) {
Vlad Buslovc24e43d82019-03-21 15:17:43 +02001997 err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack);
Hadar Hen Zion79685212016-12-01 14:06:34 +02001998 if (err)
Vlad Buslov1f17f772019-04-05 20:56:26 +03001999 goto errout_ht;
Hadar Hen Zion79685212016-12-01 14:06:34 +02002000 }
Amir Vadai5b33f482016-03-08 12:42:29 +02002001
Or Gerlitz55593962017-02-16 10:31:13 +02002002 if (!tc_in_hw(fnew->flags))
2003 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
2004
Vlad Buslov3d81e712019-03-21 15:17:42 +02002005 spin_lock(&tp->lock);
2006
Vlad Buslov272ffaa2019-03-21 15:17:41 +02002007 /* tp was deleted concurrently. -EAGAIN will cause caller to lookup
2008 * proto again or create new one, if necessary.
2009 */
2010 if (tp->deleting) {
2011 err = -EAGAIN;
2012 goto errout_hw;
2013 }
2014
Amir Vadai5b33f482016-03-08 12:42:29 +02002015 if (fold) {
Vlad Buslovb2552b82019-03-21 15:17:36 +02002016 /* Fold filter was deleted concurrently. Retry lookup. */
2017 if (fold->deleted) {
2018 err = -EAGAIN;
2019 goto errout_hw;
2020 }
2021
Vlad Buslov620da482019-03-21 15:17:34 +02002022 fnew->handle = handle;
2023
Vlad Buslov1f17f772019-04-05 20:56:26 +03002024 if (!in_ht) {
2025 struct rhashtable_params params =
2026 fnew->mask->filter_ht_params;
2027
2028 err = rhashtable_insert_fast(&fnew->mask->ht,
2029 &fnew->ht_node,
2030 params);
2031 if (err)
2032 goto errout_hw;
2033 in_ht = true;
2034 }
Vlad Buslov620da482019-03-21 15:17:34 +02002035
Vlad Buslovc049d562019-04-24 09:53:31 +03002036 refcount_inc(&fnew->refcnt);
Roi Dayan599d2572018-12-19 18:07:56 +02002037 rhashtable_remove_fast(&fold->mask->ht,
2038 &fold->ht_node,
2039 fold->mask->filter_ht_params);
Matthew Wilcox234a4622017-11-28 09:56:36 -05002040 idr_replace(&head->handle_idr, fnew, fnew->handle);
Daniel Borkmannff3532f2015-07-17 22:38:44 +02002041 list_replace_rcu(&fold->list, &fnew->list);
Vlad Buslovb2552b82019-03-21 15:17:36 +02002042 fold->deleted = true;
Vlad Buslov620da482019-03-21 15:17:34 +02002043
Vlad Buslov3d81e712019-03-21 15:17:42 +02002044 spin_unlock(&tp->lock);
2045
Vlad Buslov99946772019-04-12 00:54:19 +03002046 fl_mask_put(head, fold->mask);
Vlad Buslov620da482019-03-21 15:17:34 +02002047 if (!tc_skip_hw(fold->flags))
Vlad Buslovc24e43d82019-03-21 15:17:43 +02002048 fl_hw_destroy_filter(tp, fold, rtnl_held, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +02002049 tcf_unbind_filter(tp, &fold->res);
Vlad Buslov06177552019-03-21 15:17:35 +02002050 /* Caller holds reference to fold, so refcnt is always > 0
2051 * after this.
2052 */
2053 refcount_dec(&fold->refcnt);
2054 __fl_put(fold);
Jiri Pirko77b99002015-05-12 14:56:21 +02002055 } else {
Vlad Buslov620da482019-03-21 15:17:34 +02002056 if (handle) {
2057 /* user specifies a handle and it doesn't exist */
2058 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
2059 handle, GFP_ATOMIC);
Vlad Buslov9a2d9382019-03-21 15:17:40 +02002060
2061 /* Filter with specified handle was concurrently
2062 * inserted after initial check in cls_api. This is not
2063 * necessarily an error if NLM_F_EXCL is not set in
2064 * message flags. Returning EAGAIN will cause cls_api to
2065 * try to update concurrently inserted rule.
2066 */
2067 if (err == -ENOSPC)
2068 err = -EAGAIN;
Vlad Buslov620da482019-03-21 15:17:34 +02002069 } else {
2070 handle = 1;
2071 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
2072 INT_MAX, GFP_ATOMIC);
2073 }
2074 if (err)
2075 goto errout_hw;
2076
Vlad Buslovc049d562019-04-24 09:53:31 +03002077 refcount_inc(&fnew->refcnt);
Vlad Buslov620da482019-03-21 15:17:34 +02002078 fnew->handle = handle;
Paul Blakey05cd2712018-04-30 14:28:30 +03002079 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
Vlad Buslov3d81e712019-03-21 15:17:42 +02002080 spin_unlock(&tp->lock);
Jiri Pirko77b99002015-05-12 14:56:21 +02002081 }
2082
Vlad Buslov620da482019-03-21 15:17:34 +02002083 *arg = fnew;
2084
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01002085 kfree(tb);
Vlad Buslov99815f52019-06-13 17:54:04 +03002086 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
Jiri Pirko77b99002015-05-12 14:56:21 +02002087 return 0;
2088
Vlad Buslovc049d562019-04-24 09:53:31 +03002089errout_ht:
2090 spin_lock(&tp->lock);
Vlad Buslov620da482019-03-21 15:17:34 +02002091errout_hw:
Vlad Buslovc049d562019-04-24 09:53:31 +03002092 fnew->deleted = true;
Vlad Buslov3d81e712019-03-21 15:17:42 +02002093 spin_unlock(&tp->lock);
Vlad Buslov620da482019-03-21 15:17:34 +02002094 if (!tc_skip_hw(fnew->flags))
Vlad Buslovc24e43d82019-03-21 15:17:43 +02002095 fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL);
Vlad Buslov1f17f772019-04-05 20:56:26 +03002096 if (in_ht)
2097 rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node,
2098 fnew->mask->filter_ht_params);
Vlad Buslovecb3dea2019-03-06 16:22:12 +02002099errout_mask:
Vlad Buslov99946772019-04-12 00:54:19 +03002100 fl_mask_put(head, fnew->mask);
Jiri Pirko77b99002015-05-12 14:56:21 +02002101errout:
Vlad Buslovc049d562019-04-24 09:53:31 +03002102 __fl_put(fnew);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01002103errout_tb:
2104 kfree(tb);
Ivan Vecera2cddd202019-01-16 16:53:52 +01002105errout_mask_alloc:
Vlad Buslov99815f52019-06-13 17:54:04 +03002106 tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work);
Vlad Buslov06177552019-03-21 15:17:35 +02002107errout_fold:
2108 if (fold)
2109 __fl_put(fold);
Jiri Pirko77b99002015-05-12 14:56:21 +02002110 return err;
2111}
2112
Alexander Aring571acf22018-01-18 11:20:53 -05002113static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002114 bool rtnl_held, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02002115{
Vlad Buslove4746192019-03-21 15:17:33 +02002116 struct cls_fl_head *head = fl_head_dereference(tp);
WANG Cong8113c092017-08-04 21:31:43 -07002117 struct cls_fl_filter *f = arg;
Vlad Buslovb2552b82019-03-21 15:17:36 +02002118 bool last_on_mask;
2119 int err = 0;
Jiri Pirko77b99002015-05-12 14:56:21 +02002120
Vlad Buslovc24e43d82019-03-21 15:17:43 +02002121 err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack);
Paul Blakey05cd2712018-04-30 14:28:30 +03002122 *last = list_empty(&head->masks);
Vlad Buslov06177552019-03-21 15:17:35 +02002123 __fl_put(f);
2124
Vlad Buslovb2552b82019-03-21 15:17:36 +02002125 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +02002126}
2127
Vlad Buslov12db03b2019-02-11 10:55:45 +02002128static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg,
2129 bool rtnl_held)
Jiri Pirko77b99002015-05-12 14:56:21 +02002130{
Cong Wangd39d7142019-06-28 11:03:42 -07002131 struct cls_fl_head *head = fl_head_dereference(tp);
2132 unsigned long id = arg->cookie, tmp;
Jiri Pirko77b99002015-05-12 14:56:21 +02002133 struct cls_fl_filter *f;
2134
Vlad Buslov01683a12018-07-09 13:29:11 +03002135 arg->count = arg->skip;
2136
Cong Wangd39d7142019-06-28 11:03:42 -07002137 idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) {
2138 /* don't return filters that are being deleted */
2139 if (!refcount_inc_not_zero(&f->refcnt))
2140 continue;
Vlad Buslov01683a12018-07-09 13:29:11 +03002141 if (arg->fn(tp, f, arg) < 0) {
Vlad Buslov06177552019-03-21 15:17:35 +02002142 __fl_put(f);
Vlad Buslov01683a12018-07-09 13:29:11 +03002143 arg->stop = 1;
2144 break;
Paul Blakey05cd2712018-04-30 14:28:30 +03002145 }
Vlad Buslov06177552019-03-21 15:17:35 +02002146 __fl_put(f);
Vlad Buslov01683a12018-07-09 13:29:11 +03002147 arg->count++;
Jiri Pirko77b99002015-05-12 14:56:21 +02002148 }
Cong Wangd39d7142019-06-28 11:03:42 -07002149 arg->cookie = id;
Jiri Pirko77b99002015-05-12 14:56:21 +02002150}
2151
Vlad Buslovc049d562019-04-24 09:53:31 +03002152static struct cls_fl_filter *
2153fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add)
2154{
2155 struct cls_fl_head *head = fl_head_dereference(tp);
2156
2157 spin_lock(&tp->lock);
2158 if (list_empty(&head->hw_filters)) {
2159 spin_unlock(&tp->lock);
2160 return NULL;
2161 }
2162
2163 if (!f)
2164 f = list_entry(&head->hw_filters, struct cls_fl_filter,
2165 hw_list);
2166 list_for_each_entry_continue(f, &head->hw_filters, hw_list) {
2167 if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) {
2168 spin_unlock(&tp->lock);
2169 return f;
2170 }
2171 }
2172
2173 spin_unlock(&tp->lock);
2174 return NULL;
2175}
2176
Pablo Neira Ayusoa7323312019-07-19 18:20:15 +02002177static int fl_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
John Hurley31533cb2018-06-25 14:30:06 -07002178 void *cb_priv, struct netlink_ext_ack *extack)
2179{
John Hurley31533cb2018-06-25 14:30:06 -07002180 struct tcf_block *block = tp->chain->block;
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +02002181 struct flow_cls_offload cls_flower = {};
Vlad Buslovc049d562019-04-24 09:53:31 +03002182 struct cls_fl_filter *f = NULL;
John Hurley31533cb2018-06-25 14:30:06 -07002183 int err;
2184
Vlad Buslovc049d562019-04-24 09:53:31 +03002185 /* hw_filters list can only be changed by hw offload functions after
2186 * obtaining rtnl lock. Make sure it is not changed while reoffload is
2187 * iterating it.
2188 */
2189 ASSERT_RTNL();
John Hurley31533cb2018-06-25 14:30:06 -07002190
Vlad Buslovc049d562019-04-24 09:53:31 +03002191 while ((f = fl_get_next_hw_filter(tp, f, add))) {
John Hurley95e27a42019-04-02 23:53:20 +01002192 cls_flower.rule =
2193 flow_rule_alloc(tcf_exts_num_actions(&f->exts));
2194 if (!cls_flower.rule) {
2195 __fl_put(f);
2196 return -ENOMEM;
John Hurley31533cb2018-06-25 14:30:06 -07002197 }
John Hurley95e27a42019-04-02 23:53:20 +01002198
2199 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags,
Pieter Jansen van Vuurend6787142019-05-06 17:24:21 -07002200 extack);
John Hurley95e27a42019-04-02 23:53:20 +01002201 cls_flower.command = add ?
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +02002202 FLOW_CLS_REPLACE : FLOW_CLS_DESTROY;
John Hurley95e27a42019-04-02 23:53:20 +01002203 cls_flower.cookie = (unsigned long)f;
2204 cls_flower.rule->match.dissector = &f->mask->dissector;
2205 cls_flower.rule->match.mask = &f->mask->key;
2206 cls_flower.rule->match.key = &f->mkey;
2207
Vlad Buslovb15e7a62020-02-17 12:12:12 +02002208 err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts);
John Hurley95e27a42019-04-02 23:53:20 +01002209 if (err) {
2210 kfree(cls_flower.rule);
2211 if (tc_skip_sw(f->flags)) {
2212 NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action");
2213 __fl_put(f);
2214 return err;
2215 }
2216 goto next_flow;
2217 }
2218
2219 cls_flower.classid = f->res.classid;
2220
Vlad Buslov40119212019-08-26 16:44:59 +03002221 err = tc_setup_cb_reoffload(block, tp, add, cb,
2222 TC_SETUP_CLSFLOWER, &cls_flower,
2223 cb_priv, &f->flags,
2224 &f->in_hw_count);
Vlad Buslov5a6ff4b2019-08-26 16:45:04 +03002225 tc_cleanup_flow_action(&cls_flower.rule->action);
John Hurley95e27a42019-04-02 23:53:20 +01002226 kfree(cls_flower.rule);
2227
2228 if (err) {
Vlad Buslov40119212019-08-26 16:44:59 +03002229 __fl_put(f);
2230 return err;
John Hurley95e27a42019-04-02 23:53:20 +01002231 }
John Hurley95e27a42019-04-02 23:53:20 +01002232next_flow:
John Hurley95e27a42019-04-02 23:53:20 +01002233 __fl_put(f);
John Hurley31533cb2018-06-25 14:30:06 -07002234 }
2235
2236 return 0;
2237}
2238
Vlad Buslova449a3e2019-08-26 16:45:00 +03002239static void fl_hw_add(struct tcf_proto *tp, void *type_data)
2240{
2241 struct flow_cls_offload *cls_flower = type_data;
2242 struct cls_fl_filter *f =
2243 (struct cls_fl_filter *) cls_flower->cookie;
2244 struct cls_fl_head *head = fl_head_dereference(tp);
2245
2246 spin_lock(&tp->lock);
2247 list_add(&f->hw_list, &head->hw_filters);
2248 spin_unlock(&tp->lock);
2249}
2250
2251static void fl_hw_del(struct tcf_proto *tp, void *type_data)
2252{
2253 struct flow_cls_offload *cls_flower = type_data;
2254 struct cls_fl_filter *f =
2255 (struct cls_fl_filter *) cls_flower->cookie;
2256
2257 spin_lock(&tp->lock);
2258 if (!list_empty(&f->hw_list))
2259 list_del_init(&f->hw_list);
2260 spin_unlock(&tp->lock);
2261}
2262
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01002263static int fl_hw_create_tmplt(struct tcf_chain *chain,
2264 struct fl_flow_tmplt *tmplt)
Jiri Pirko34738452018-07-23 09:23:11 +02002265{
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +02002266 struct flow_cls_offload cls_flower = {};
Jiri Pirko34738452018-07-23 09:23:11 +02002267 struct tcf_block *block = chain->block;
Jiri Pirko34738452018-07-23 09:23:11 +02002268
Pablo Neira Ayusoe3ab7862019-02-02 12:50:45 +01002269 cls_flower.rule = flow_rule_alloc(0);
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01002270 if (!cls_flower.rule)
2271 return -ENOMEM;
2272
Jiri Pirko34738452018-07-23 09:23:11 +02002273 cls_flower.common.chain_index = chain->index;
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +02002274 cls_flower.command = FLOW_CLS_TMPLT_CREATE;
Jiri Pirko34738452018-07-23 09:23:11 +02002275 cls_flower.cookie = (unsigned long) tmplt;
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01002276 cls_flower.rule->match.dissector = &tmplt->dissector;
2277 cls_flower.rule->match.mask = &tmplt->mask;
2278 cls_flower.rule->match.key = &tmplt->dummy_key;
Jiri Pirko34738452018-07-23 09:23:11 +02002279
2280 /* We don't care if driver (any of them) fails to handle this
2281 * call. It serves just as a hint for it.
2282 */
Vlad Buslov40119212019-08-26 16:44:59 +03002283 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01002284 kfree(cls_flower.rule);
2285
2286 return 0;
Jiri Pirko34738452018-07-23 09:23:11 +02002287}
2288
2289static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
2290 struct fl_flow_tmplt *tmplt)
2291{
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +02002292 struct flow_cls_offload cls_flower = {};
Jiri Pirko34738452018-07-23 09:23:11 +02002293 struct tcf_block *block = chain->block;
2294
2295 cls_flower.common.chain_index = chain->index;
Pablo Neira Ayusof9e30082019-07-09 22:55:49 +02002296 cls_flower.command = FLOW_CLS_TMPLT_DESTROY;
Jiri Pirko34738452018-07-23 09:23:11 +02002297 cls_flower.cookie = (unsigned long) tmplt;
2298
Vlad Buslov40119212019-08-26 16:44:59 +03002299 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true);
Jiri Pirko34738452018-07-23 09:23:11 +02002300}
2301
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02002302static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
2303 struct nlattr **tca,
2304 struct netlink_ext_ack *extack)
2305{
2306 struct fl_flow_tmplt *tmplt;
2307 struct nlattr **tb;
2308 int err;
2309
2310 if (!tca[TCA_OPTIONS])
2311 return ERR_PTR(-EINVAL);
2312
2313 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
2314 if (!tb)
2315 return ERR_PTR(-ENOBUFS);
Johannes Berg8cb08172019-04-26 14:07:28 +02002316 err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX,
2317 tca[TCA_OPTIONS], fl_policy, NULL);
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02002318 if (err)
2319 goto errout_tb;
2320
2321 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
Dan Carpenter1cbc36a52018-08-03 22:27:55 +03002322 if (!tmplt) {
2323 err = -ENOMEM;
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02002324 goto errout_tb;
Dan Carpenter1cbc36a52018-08-03 22:27:55 +03002325 }
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02002326 tmplt->chain = chain;
2327 err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
2328 if (err)
2329 goto errout_tmplt;
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02002330
2331 fl_init_dissector(&tmplt->dissector, &tmplt->mask);
2332
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01002333 err = fl_hw_create_tmplt(chain, tmplt);
2334 if (err)
2335 goto errout_tmplt;
Jiri Pirko34738452018-07-23 09:23:11 +02002336
Pablo Neira Ayuso8f256622019-02-02 12:50:43 +01002337 kfree(tb);
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02002338 return tmplt;
2339
2340errout_tmplt:
2341 kfree(tmplt);
2342errout_tb:
2343 kfree(tb);
2344 return ERR_PTR(err);
2345}
2346
2347static void fl_tmplt_destroy(void *tmplt_priv)
2348{
2349 struct fl_flow_tmplt *tmplt = tmplt_priv;
2350
Cong Wang95278dd2018-10-02 12:50:19 -07002351 fl_hw_destroy_tmplt(tmplt->chain, tmplt);
2352 kfree(tmplt);
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02002353}
2354
Jiri Pirko77b99002015-05-12 14:56:21 +02002355static int fl_dump_key_val(struct sk_buff *skb,
2356 void *val, int val_type,
2357 void *mask, int mask_type, int len)
2358{
2359 int err;
2360
2361 if (!memchr_inv(mask, 0, len))
2362 return 0;
2363 err = nla_put(skb, val_type, len, val);
2364 if (err)
2365 return err;
2366 if (mask_type != TCA_FLOWER_UNSPEC) {
2367 err = nla_put(skb, mask_type, len, mask);
2368 if (err)
2369 return err;
2370 }
2371 return 0;
2372}
2373
Amritha Nambiar5c722992018-11-12 16:15:55 -08002374static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
2375 struct fl_flow_key *mask)
2376{
Yoshiki Komachi8ffb0552019-12-03 19:40:12 +09002377 if (fl_dump_key_val(skb, &key->tp_range.tp_min.dst,
2378 TCA_FLOWER_KEY_PORT_DST_MIN,
2379 &mask->tp_range.tp_min.dst, TCA_FLOWER_UNSPEC,
2380 sizeof(key->tp_range.tp_min.dst)) ||
2381 fl_dump_key_val(skb, &key->tp_range.tp_max.dst,
2382 TCA_FLOWER_KEY_PORT_DST_MAX,
2383 &mask->tp_range.tp_max.dst, TCA_FLOWER_UNSPEC,
2384 sizeof(key->tp_range.tp_max.dst)) ||
2385 fl_dump_key_val(skb, &key->tp_range.tp_min.src,
2386 TCA_FLOWER_KEY_PORT_SRC_MIN,
2387 &mask->tp_range.tp_min.src, TCA_FLOWER_UNSPEC,
2388 sizeof(key->tp_range.tp_min.src)) ||
2389 fl_dump_key_val(skb, &key->tp_range.tp_max.src,
2390 TCA_FLOWER_KEY_PORT_SRC_MAX,
2391 &mask->tp_range.tp_max.src, TCA_FLOWER_UNSPEC,
2392 sizeof(key->tp_range.tp_max.src)))
Amritha Nambiar5c722992018-11-12 16:15:55 -08002393 return -1;
2394
2395 return 0;
2396}
2397
Guillaume Nault61aec252020-05-26 14:29:04 +02002398static int fl_dump_key_mpls_opt_lse(struct sk_buff *skb,
2399 struct flow_dissector_key_mpls *mpls_key,
2400 struct flow_dissector_key_mpls *mpls_mask,
2401 u8 lse_index)
2402{
2403 struct flow_dissector_mpls_lse *lse_mask = &mpls_mask->ls[lse_index];
2404 struct flow_dissector_mpls_lse *lse_key = &mpls_key->ls[lse_index];
2405 int err;
2406
2407 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH,
2408 lse_index + 1);
2409 if (err)
2410 return err;
2411
2412 if (lse_mask->mpls_ttl) {
2413 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL,
2414 lse_key->mpls_ttl);
2415 if (err)
2416 return err;
2417 }
2418 if (lse_mask->mpls_bos) {
2419 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS,
2420 lse_key->mpls_bos);
2421 if (err)
2422 return err;
2423 }
2424 if (lse_mask->mpls_tc) {
2425 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TC,
2426 lse_key->mpls_tc);
2427 if (err)
2428 return err;
2429 }
2430 if (lse_mask->mpls_label) {
Guillaume Nault7fdd3752020-12-09 16:48:41 +01002431 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL,
2432 lse_key->mpls_label);
Guillaume Nault61aec252020-05-26 14:29:04 +02002433 if (err)
2434 return err;
2435 }
2436
2437 return 0;
2438}
2439
2440static int fl_dump_key_mpls_opts(struct sk_buff *skb,
2441 struct flow_dissector_key_mpls *mpls_key,
2442 struct flow_dissector_key_mpls *mpls_mask)
2443{
2444 struct nlattr *opts;
2445 struct nlattr *lse;
2446 u8 lse_index;
2447 int err;
2448
2449 opts = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS);
2450 if (!opts)
2451 return -EMSGSIZE;
2452
2453 for (lse_index = 0; lse_index < FLOW_DIS_MPLS_MAX; lse_index++) {
2454 if (!(mpls_mask->used_lses & 1 << lse_index))
2455 continue;
2456
2457 lse = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS_LSE);
2458 if (!lse) {
2459 err = -EMSGSIZE;
2460 goto err_opts;
2461 }
2462
2463 err = fl_dump_key_mpls_opt_lse(skb, mpls_key, mpls_mask,
2464 lse_index);
2465 if (err)
2466 goto err_opts_lse;
2467 nla_nest_end(skb, lse);
2468 }
2469 nla_nest_end(skb, opts);
2470
2471 return 0;
2472
2473err_opts_lse:
2474 nla_nest_cancel(skb, lse);
2475err_opts:
2476 nla_nest_cancel(skb, opts);
2477
2478 return err;
2479}
2480
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002481static int fl_dump_key_mpls(struct sk_buff *skb,
2482 struct flow_dissector_key_mpls *mpls_key,
2483 struct flow_dissector_key_mpls *mpls_mask)
2484{
Guillaume Nault58cff782020-05-26 14:29:00 +02002485 struct flow_dissector_mpls_lse *lse_mask;
2486 struct flow_dissector_mpls_lse *lse_key;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002487 int err;
2488
Guillaume Nault61aec252020-05-26 14:29:04 +02002489 if (!mpls_mask->used_lses)
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002490 return 0;
Guillaume Nault58cff782020-05-26 14:29:00 +02002491
2492 lse_mask = &mpls_mask->ls[0];
2493 lse_key = &mpls_key->ls[0];
2494
Guillaume Nault61aec252020-05-26 14:29:04 +02002495 /* For backward compatibility, don't use the MPLS nested attributes if
2496 * the rule can be expressed using the old attributes.
2497 */
2498 if (mpls_mask->used_lses & ~1 ||
2499 (!lse_mask->mpls_ttl && !lse_mask->mpls_bos &&
2500 !lse_mask->mpls_tc && !lse_mask->mpls_label))
2501 return fl_dump_key_mpls_opts(skb, mpls_key, mpls_mask);
2502
Guillaume Nault58cff782020-05-26 14:29:00 +02002503 if (lse_mask->mpls_ttl) {
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002504 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
Guillaume Nault58cff782020-05-26 14:29:00 +02002505 lse_key->mpls_ttl);
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002506 if (err)
2507 return err;
2508 }
Guillaume Nault58cff782020-05-26 14:29:00 +02002509 if (lse_mask->mpls_tc) {
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002510 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
Guillaume Nault58cff782020-05-26 14:29:00 +02002511 lse_key->mpls_tc);
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002512 if (err)
2513 return err;
2514 }
Guillaume Nault58cff782020-05-26 14:29:00 +02002515 if (lse_mask->mpls_label) {
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002516 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
Guillaume Nault58cff782020-05-26 14:29:00 +02002517 lse_key->mpls_label);
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002518 if (err)
2519 return err;
2520 }
Guillaume Nault58cff782020-05-26 14:29:00 +02002521 if (lse_mask->mpls_bos) {
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002522 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
Guillaume Nault58cff782020-05-26 14:29:00 +02002523 lse_key->mpls_bos);
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002524 if (err)
2525 return err;
2526 }
2527 return 0;
2528}
2529
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03002530static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03002531 struct flow_dissector_key_ip *key,
2532 struct flow_dissector_key_ip *mask)
2533{
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03002534 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
2535 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
2536 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
2537 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
2538
2539 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
2540 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
Or Gerlitz4d80cc02017-06-01 21:37:38 +03002541 return -1;
2542
2543 return 0;
2544}
2545
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03002546static int fl_dump_key_vlan(struct sk_buff *skb,
Jianbo Liud64efd02018-07-06 05:38:16 +00002547 int vlan_id_key, int vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03002548 struct flow_dissector_key_vlan *vlan_key,
2549 struct flow_dissector_key_vlan *vlan_mask)
2550{
2551 int err;
2552
2553 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
2554 return 0;
2555 if (vlan_mask->vlan_id) {
Jianbo Liud64efd02018-07-06 05:38:16 +00002556 err = nla_put_u16(skb, vlan_id_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03002557 vlan_key->vlan_id);
2558 if (err)
2559 return err;
2560 }
2561 if (vlan_mask->vlan_priority) {
Jianbo Liud64efd02018-07-06 05:38:16 +00002562 err = nla_put_u8(skb, vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03002563 vlan_key->vlan_priority);
2564 if (err)
2565 return err;
2566 }
2567 return 0;
2568}
2569
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02002570static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
2571 u32 *flower_key, u32 *flower_mask,
2572 u32 flower_flag_bit, u32 dissector_flag_bit)
2573{
2574 if (dissector_mask & dissector_flag_bit) {
2575 *flower_mask |= flower_flag_bit;
2576 if (dissector_key & dissector_flag_bit)
2577 *flower_key |= flower_flag_bit;
2578 }
2579}
2580
2581static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
2582{
2583 u32 key, mask;
2584 __be32 _key, _mask;
2585 int err;
2586
2587 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
2588 return 0;
2589
2590 key = 0;
2591 mask = 0;
2592
2593 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2594 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +01002595 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
2596 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
2597 FLOW_DIS_FIRST_FRAG);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02002598
2599 _key = cpu_to_be32(key);
2600 _mask = cpu_to_be32(mask);
2601
2602 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
2603 if (err)
2604 return err;
2605
2606 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
2607}
2608
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02002609static int fl_dump_key_geneve_opt(struct sk_buff *skb,
2610 struct flow_dissector_key_enc_opts *enc_opts)
2611{
2612 struct geneve_opt *opt;
2613 struct nlattr *nest;
2614 int opt_off = 0;
2615
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002616 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02002617 if (!nest)
2618 goto nla_put_failure;
2619
2620 while (enc_opts->len > opt_off) {
2621 opt = (struct geneve_opt *)&enc_opts->data[opt_off];
2622
2623 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
2624 opt->opt_class))
2625 goto nla_put_failure;
2626 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
2627 opt->type))
2628 goto nla_put_failure;
2629 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
2630 opt->length * 4, opt->opt_data))
2631 goto nla_put_failure;
2632
2633 opt_off += sizeof(struct geneve_opt) + opt->length * 4;
2634 }
2635 nla_nest_end(skb, nest);
2636 return 0;
2637
2638nla_put_failure:
2639 nla_nest_cancel(skb, nest);
2640 return -EMSGSIZE;
2641}
2642
Xin Longd8f9dfa2019-11-21 18:03:28 +08002643static int fl_dump_key_vxlan_opt(struct sk_buff *skb,
2644 struct flow_dissector_key_enc_opts *enc_opts)
2645{
2646 struct vxlan_metadata *md;
2647 struct nlattr *nest;
2648
2649 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_VXLAN);
2650 if (!nest)
2651 goto nla_put_failure;
2652
2653 md = (struct vxlan_metadata *)&enc_opts->data[0];
2654 if (nla_put_u32(skb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, md->gbp))
2655 goto nla_put_failure;
2656
2657 nla_nest_end(skb, nest);
2658 return 0;
2659
2660nla_put_failure:
2661 nla_nest_cancel(skb, nest);
2662 return -EMSGSIZE;
2663}
2664
Xin Long79b10112019-11-21 18:03:29 +08002665static int fl_dump_key_erspan_opt(struct sk_buff *skb,
2666 struct flow_dissector_key_enc_opts *enc_opts)
2667{
2668 struct erspan_metadata *md;
2669 struct nlattr *nest;
2670
2671 nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_ERSPAN);
2672 if (!nest)
2673 goto nla_put_failure;
2674
2675 md = (struct erspan_metadata *)&enc_opts->data[0];
2676 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER, md->version))
2677 goto nla_put_failure;
2678
2679 if (md->version == 1 &&
2680 nla_put_be32(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index))
2681 goto nla_put_failure;
2682
2683 if (md->version == 2 &&
2684 (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR,
2685 md->u.md2.dir) ||
2686 nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID,
2687 get_hwid(&md->u.md2))))
2688 goto nla_put_failure;
2689
2690 nla_nest_end(skb, nest);
2691 return 0;
2692
2693nla_put_failure:
2694 nla_nest_cancel(skb, nest);
2695 return -EMSGSIZE;
2696}
2697
Paul Blakeye0ace682019-07-09 10:30:50 +03002698static int fl_dump_key_ct(struct sk_buff *skb,
2699 struct flow_dissector_key_ct *key,
2700 struct flow_dissector_key_ct *mask)
2701{
2702 if (IS_ENABLED(CONFIG_NF_CONNTRACK) &&
2703 fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE,
2704 &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK,
2705 sizeof(key->ct_state)))
2706 goto nla_put_failure;
2707
2708 if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
2709 fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE,
2710 &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK,
2711 sizeof(key->ct_zone)))
2712 goto nla_put_failure;
2713
2714 if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
2715 fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK,
2716 &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK,
2717 sizeof(key->ct_mark)))
2718 goto nla_put_failure;
2719
2720 if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
2721 fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS,
2722 &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK,
2723 sizeof(key->ct_labels)))
2724 goto nla_put_failure;
2725
2726 return 0;
2727
2728nla_put_failure:
2729 return -EMSGSIZE;
2730}
2731
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02002732static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
2733 struct flow_dissector_key_enc_opts *enc_opts)
2734{
2735 struct nlattr *nest;
2736 int err;
2737
2738 if (!enc_opts->len)
2739 return 0;
2740
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002741 nest = nla_nest_start_noflag(skb, enc_opt_type);
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02002742 if (!nest)
2743 goto nla_put_failure;
2744
2745 switch (enc_opts->dst_opt_type) {
2746 case TUNNEL_GENEVE_OPT:
2747 err = fl_dump_key_geneve_opt(skb, enc_opts);
2748 if (err)
2749 goto nla_put_failure;
2750 break;
Xin Longd8f9dfa2019-11-21 18:03:28 +08002751 case TUNNEL_VXLAN_OPT:
2752 err = fl_dump_key_vxlan_opt(skb, enc_opts);
2753 if (err)
2754 goto nla_put_failure;
2755 break;
Xin Long79b10112019-11-21 18:03:29 +08002756 case TUNNEL_ERSPAN_OPT:
2757 err = fl_dump_key_erspan_opt(skb, enc_opts);
2758 if (err)
2759 goto nla_put_failure;
2760 break;
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02002761 default:
2762 goto nla_put_failure;
2763 }
2764 nla_nest_end(skb, nest);
2765 return 0;
2766
2767nla_put_failure:
2768 nla_nest_cancel(skb, nest);
2769 return -EMSGSIZE;
2770}
2771
2772static int fl_dump_key_enc_opt(struct sk_buff *skb,
2773 struct flow_dissector_key_enc_opts *key_opts,
2774 struct flow_dissector_key_enc_opts *msk_opts)
2775{
2776 int err;
2777
2778 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
2779 if (err)
2780 return err;
2781
2782 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
2783}
2784
Jiri Pirkof5749082018-07-23 09:23:08 +02002785static int fl_dump_key(struct sk_buff *skb, struct net *net,
2786 struct fl_flow_key *key, struct fl_flow_key *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +02002787{
Jiri Pirko8212ed72019-06-19 09:41:03 +03002788 if (mask->meta.ingress_ifindex) {
Jiri Pirko77b99002015-05-12 14:56:21 +02002789 struct net_device *dev;
2790
Jiri Pirko8212ed72019-06-19 09:41:03 +03002791 dev = __dev_get_by_index(net, key->meta.ingress_ifindex);
Jiri Pirko77b99002015-05-12 14:56:21 +02002792 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
2793 goto nla_put_failure;
2794 }
2795
2796 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
2797 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
2798 sizeof(key->eth.dst)) ||
2799 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
2800 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
2801 sizeof(key->eth.src)) ||
2802 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
2803 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
2804 sizeof(key->basic.n_proto)))
2805 goto nla_put_failure;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03002806
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04002807 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
2808 goto nla_put_failure;
2809
Jianbo Liud64efd02018-07-06 05:38:16 +00002810 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
2811 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03002812 goto nla_put_failure;
2813
Jianbo Liud64efd02018-07-06 05:38:16 +00002814 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
2815 TCA_FLOWER_KEY_CVLAN_PRIO,
2816 &key->cvlan, &mask->cvlan) ||
2817 (mask->cvlan.vlan_tpid &&
Jianbo Liu158abbf2018-07-25 02:31:25 +00002818 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
2819 key->cvlan.vlan_tpid)))
Jianbo Liud3069512018-07-06 05:38:15 +00002820 goto nla_put_failure;
2821
Jianbo Liu5e9a0fe2018-07-09 02:26:20 +00002822 if (mask->basic.n_proto) {
2823 if (mask->cvlan.vlan_tpid) {
2824 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
2825 key->basic.n_proto))
2826 goto nla_put_failure;
2827 } else if (mask->vlan.vlan_tpid) {
2828 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
2829 key->basic.n_proto))
2830 goto nla_put_failure;
2831 }
Jianbo Liud64efd02018-07-06 05:38:16 +00002832 }
2833
Jiri Pirko77b99002015-05-12 14:56:21 +02002834 if ((key->basic.n_proto == htons(ETH_P_IP) ||
2835 key->basic.n_proto == htons(ETH_P_IPV6)) &&
Or Gerlitz4d80cc02017-06-01 21:37:38 +03002836 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
Jiri Pirko77b99002015-05-12 14:56:21 +02002837 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03002838 sizeof(key->basic.ip_proto)) ||
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03002839 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
Jiri Pirko77b99002015-05-12 14:56:21 +02002840 goto nla_put_failure;
2841
Tom Herbertc3f83242015-06-04 09:16:40 -07002842 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02002843 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
2844 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
2845 sizeof(key->ipv4.src)) ||
2846 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
2847 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
2848 sizeof(key->ipv4.dst))))
2849 goto nla_put_failure;
Tom Herbertc3f83242015-06-04 09:16:40 -07002850 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02002851 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
2852 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
2853 sizeof(key->ipv6.src)) ||
2854 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
2855 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
2856 sizeof(key->ipv6.dst))))
2857 goto nla_put_failure;
2858
2859 if (key->basic.ip_proto == IPPROTO_TCP &&
2860 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03002861 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02002862 sizeof(key->tp.src)) ||
2863 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03002864 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02002865 sizeof(key->tp.dst)) ||
2866 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
2867 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
2868 sizeof(key->tcp.flags))))
Jiri Pirko77b99002015-05-12 14:56:21 +02002869 goto nla_put_failure;
2870 else if (key->basic.ip_proto == IPPROTO_UDP &&
2871 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03002872 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02002873 sizeof(key->tp.src)) ||
2874 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03002875 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02002876 sizeof(key->tp.dst))))
2877 goto nla_put_failure;
Simon Horman5976c5f2016-11-03 13:24:21 +01002878 else if (key->basic.ip_proto == IPPROTO_SCTP &&
2879 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
2880 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
2881 sizeof(key->tp.src)) ||
2882 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
2883 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
2884 sizeof(key->tp.dst))))
2885 goto nla_put_failure;
Simon Horman7b684882016-12-07 13:48:28 +01002886 else if (key->basic.n_proto == htons(ETH_P_IP) &&
2887 key->basic.ip_proto == IPPROTO_ICMP &&
2888 (fl_dump_key_val(skb, &key->icmp.type,
2889 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
2890 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
2891 sizeof(key->icmp.type)) ||
2892 fl_dump_key_val(skb, &key->icmp.code,
2893 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
2894 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
2895 sizeof(key->icmp.code))))
2896 goto nla_put_failure;
2897 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
2898 key->basic.ip_proto == IPPROTO_ICMPV6 &&
2899 (fl_dump_key_val(skb, &key->icmp.type,
2900 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
2901 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
2902 sizeof(key->icmp.type)) ||
2903 fl_dump_key_val(skb, &key->icmp.code,
2904 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
2905 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
2906 sizeof(key->icmp.code))))
2907 goto nla_put_failure;
Simon Horman99d31322017-01-11 14:05:43 +01002908 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
2909 key->basic.n_proto == htons(ETH_P_RARP)) &&
2910 (fl_dump_key_val(skb, &key->arp.sip,
2911 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
2912 TCA_FLOWER_KEY_ARP_SIP_MASK,
2913 sizeof(key->arp.sip)) ||
2914 fl_dump_key_val(skb, &key->arp.tip,
2915 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
2916 TCA_FLOWER_KEY_ARP_TIP_MASK,
2917 sizeof(key->arp.tip)) ||
2918 fl_dump_key_val(skb, &key->arp.op,
2919 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
2920 TCA_FLOWER_KEY_ARP_OP_MASK,
2921 sizeof(key->arp.op)) ||
2922 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
2923 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
2924 sizeof(key->arp.sha)) ||
2925 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
2926 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
2927 sizeof(key->arp.tha))))
2928 goto nla_put_failure;
Jiri Pirko77b99002015-05-12 14:56:21 +02002929
Amritha Nambiar5c722992018-11-12 16:15:55 -08002930 if ((key->basic.ip_proto == IPPROTO_TCP ||
2931 key->basic.ip_proto == IPPROTO_UDP ||
2932 key->basic.ip_proto == IPPROTO_SCTP) &&
2933 fl_dump_key_port_range(skb, key, mask))
2934 goto nla_put_failure;
2935
Amir Vadaibc3103f2016-09-08 16:23:47 +03002936 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
2937 (fl_dump_key_val(skb, &key->enc_ipv4.src,
2938 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
2939 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
2940 sizeof(key->enc_ipv4.src)) ||
2941 fl_dump_key_val(skb, &key->enc_ipv4.dst,
2942 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
2943 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
2944 sizeof(key->enc_ipv4.dst))))
2945 goto nla_put_failure;
2946 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
2947 (fl_dump_key_val(skb, &key->enc_ipv6.src,
2948 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
2949 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
2950 sizeof(key->enc_ipv6.src)) ||
2951 fl_dump_key_val(skb, &key->enc_ipv6.dst,
2952 TCA_FLOWER_KEY_ENC_IPV6_DST,
2953 &mask->enc_ipv6.dst,
2954 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
2955 sizeof(key->enc_ipv6.dst))))
2956 goto nla_put_failure;
2957
2958 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03002959 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02002960 sizeof(key->enc_key_id)) ||
2961 fl_dump_key_val(skb, &key->enc_tp.src,
2962 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
2963 &mask->enc_tp.src,
2964 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
2965 sizeof(key->enc_tp.src)) ||
2966 fl_dump_key_val(skb, &key->enc_tp.dst,
2967 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
2968 &mask->enc_tp.dst,
2969 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03002970 sizeof(key->enc_tp.dst)) ||
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02002971 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
2972 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
Amir Vadaibc3103f2016-09-08 16:23:47 +03002973 goto nla_put_failure;
2974
Paul Blakeye0ace682019-07-09 10:30:50 +03002975 if (fl_dump_key_ct(skb, &key->ct, &mask->ct))
2976 goto nla_put_failure;
2977
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02002978 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
2979 goto nla_put_failure;
2980
Ariel Levkovich5923b8f2020-07-23 01:03:01 +03002981 if (fl_dump_key_val(skb, &key->hash.hash, TCA_FLOWER_KEY_HASH,
2982 &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK,
2983 sizeof(key->hash.hash)))
2984 goto nla_put_failure;
2985
Jiri Pirkof5749082018-07-23 09:23:08 +02002986 return 0;
2987
2988nla_put_failure:
2989 return -EMSGSIZE;
2990}
2991
2992static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
Vlad Buslov12db03b2019-02-11 10:55:45 +02002993 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
Jiri Pirkof5749082018-07-23 09:23:08 +02002994{
2995 struct cls_fl_filter *f = fh;
2996 struct nlattr *nest;
2997 struct fl_flow_key *key, *mask;
Vlad Buslov3d81e712019-03-21 15:17:42 +02002998 bool skip_hw;
Jiri Pirkof5749082018-07-23 09:23:08 +02002999
3000 if (!f)
3001 return skb->len;
3002
3003 t->tcm_handle = f->handle;
3004
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003005 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Jiri Pirkof5749082018-07-23 09:23:08 +02003006 if (!nest)
3007 goto nla_put_failure;
3008
Vlad Buslov3d81e712019-03-21 15:17:42 +02003009 spin_lock(&tp->lock);
3010
Jiri Pirkof5749082018-07-23 09:23:08 +02003011 if (f->res.classid &&
3012 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
Vlad Buslov3d81e712019-03-21 15:17:42 +02003013 goto nla_put_failure_locked;
Jiri Pirkof5749082018-07-23 09:23:08 +02003014
3015 key = &f->key;
3016 mask = &f->mask->key;
Vlad Buslov3d81e712019-03-21 15:17:42 +02003017 skip_hw = tc_skip_hw(f->flags);
Jiri Pirkof5749082018-07-23 09:23:08 +02003018
3019 if (fl_dump_key(skb, net, key, mask))
Vlad Buslov3d81e712019-03-21 15:17:42 +02003020 goto nla_put_failure_locked;
Jiri Pirkof5749082018-07-23 09:23:08 +02003021
Or Gerlitz749e6722017-02-16 10:31:10 +02003022 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
Vlad Buslov3d81e712019-03-21 15:17:42 +02003023 goto nla_put_failure_locked;
3024
3025 spin_unlock(&tp->lock);
3026
3027 if (!skip_hw)
Vlad Buslovc24e43d82019-03-21 15:17:43 +02003028 fl_hw_update_stats(tp, f, rtnl_held);
Amir Vadaie69985c2016-06-05 17:11:18 +03003029
Vlad Buslov86c55362018-09-07 17:22:21 +03003030 if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
3031 goto nla_put_failure;
3032
Jiri Pirko77b99002015-05-12 14:56:21 +02003033 if (tcf_exts_dump(skb, &f->exts))
3034 goto nla_put_failure;
3035
3036 nla_nest_end(skb, nest);
3037
3038 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
3039 goto nla_put_failure;
3040
3041 return skb->len;
3042
Vlad Buslov3d81e712019-03-21 15:17:42 +02003043nla_put_failure_locked:
3044 spin_unlock(&tp->lock);
Jiri Pirko77b99002015-05-12 14:56:21 +02003045nla_put_failure:
3046 nla_nest_cancel(skb, nest);
3047 return -1;
3048}
3049
Vlad Buslov03484512020-05-15 14:40:13 +03003050static int fl_terse_dump(struct net *net, struct tcf_proto *tp, void *fh,
3051 struct sk_buff *skb, struct tcmsg *t, bool rtnl_held)
3052{
3053 struct cls_fl_filter *f = fh;
3054 struct nlattr *nest;
3055 bool skip_hw;
3056
3057 if (!f)
3058 return skb->len;
3059
3060 t->tcm_handle = f->handle;
3061
3062 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
3063 if (!nest)
3064 goto nla_put_failure;
3065
3066 spin_lock(&tp->lock);
3067
3068 skip_hw = tc_skip_hw(f->flags);
3069
3070 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
3071 goto nla_put_failure_locked;
3072
3073 spin_unlock(&tp->lock);
3074
3075 if (!skip_hw)
3076 fl_hw_update_stats(tp, f, rtnl_held);
3077
3078 if (tcf_exts_terse_dump(skb, &f->exts))
3079 goto nla_put_failure;
3080
3081 nla_nest_end(skb, nest);
3082
3083 return skb->len;
3084
3085nla_put_failure_locked:
3086 spin_unlock(&tp->lock);
3087nla_put_failure:
3088 nla_nest_cancel(skb, nest);
3089 return -1;
3090}
3091
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02003092static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
3093{
3094 struct fl_flow_tmplt *tmplt = tmplt_priv;
3095 struct fl_flow_key *key, *mask;
3096 struct nlattr *nest;
3097
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003098 nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02003099 if (!nest)
3100 goto nla_put_failure;
3101
3102 key = &tmplt->dummy_key;
3103 mask = &tmplt->mask;
3104
3105 if (fl_dump_key(skb, net, key, mask))
3106 goto nla_put_failure;
3107
3108 nla_nest_end(skb, nest);
3109
3110 return skb->len;
3111
3112nla_put_failure:
3113 nla_nest_cancel(skb, nest);
3114 return -EMSGSIZE;
3115}
3116
Cong Wang2e24cd72020-01-23 16:26:18 -08003117static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
3118 unsigned long base)
Cong Wang07d79fc2017-08-30 14:30:36 -07003119{
3120 struct cls_fl_filter *f = fh;
3121
Cong Wang2e24cd72020-01-23 16:26:18 -08003122 if (f && f->res.classid == classid) {
3123 if (cl)
3124 __tcf_bind_filter(q, &f->res, base);
3125 else
3126 __tcf_unbind_filter(q, &f->res);
3127 }
Cong Wang07d79fc2017-08-30 14:30:36 -07003128}
3129
Davide Carattia5b72a02019-12-28 16:36:58 +01003130static bool fl_delete_empty(struct tcf_proto *tp)
3131{
3132 struct cls_fl_head *head = fl_head_dereference(tp);
3133
3134 spin_lock(&tp->lock);
3135 tp->deleting = idr_is_empty(&head->handle_idr);
3136 spin_unlock(&tp->lock);
3137
3138 return tp->deleting;
3139}
3140
Jiri Pirko77b99002015-05-12 14:56:21 +02003141static struct tcf_proto_ops cls_fl_ops __read_mostly = {
3142 .kind = "flower",
3143 .classify = fl_classify,
3144 .init = fl_init,
3145 .destroy = fl_destroy,
3146 .get = fl_get,
Vlad Buslov06177552019-03-21 15:17:35 +02003147 .put = fl_put,
Jiri Pirko77b99002015-05-12 14:56:21 +02003148 .change = fl_change,
3149 .delete = fl_delete,
Davide Carattia5b72a02019-12-28 16:36:58 +01003150 .delete_empty = fl_delete_empty,
Jiri Pirko77b99002015-05-12 14:56:21 +02003151 .walk = fl_walk,
John Hurley31533cb2018-06-25 14:30:06 -07003152 .reoffload = fl_reoffload,
Vlad Buslova449a3e2019-08-26 16:45:00 +03003153 .hw_add = fl_hw_add,
3154 .hw_del = fl_hw_del,
Jiri Pirko77b99002015-05-12 14:56:21 +02003155 .dump = fl_dump,
Vlad Buslov03484512020-05-15 14:40:13 +03003156 .terse_dump = fl_terse_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07003157 .bind_class = fl_bind_class,
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02003158 .tmplt_create = fl_tmplt_create,
3159 .tmplt_destroy = fl_tmplt_destroy,
3160 .tmplt_dump = fl_tmplt_dump,
Jiri Pirko77b99002015-05-12 14:56:21 +02003161 .owner = THIS_MODULE,
Vlad Buslov92149192019-03-21 15:17:44 +02003162 .flags = TCF_PROTO_OPS_DOIT_UNLOCKED,
Jiri Pirko77b99002015-05-12 14:56:21 +02003163};
3164
3165static int __init cls_fl_init(void)
3166{
3167 return register_tcf_proto_ops(&cls_fl_ops);
3168}
3169
3170static void __exit cls_fl_exit(void)
3171{
3172 unregister_tcf_proto_ops(&cls_fl_ops);
3173}
3174
3175module_init(cls_fl_init);
3176module_exit(cls_fl_exit);
3177
3178MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
3179MODULE_DESCRIPTION("Flower classifier");
3180MODULE_LICENSE("GPL v2");