blob: 12ca9d13db83320b6fe3381b1874206dec433a74 [file] [log] [blame]
Jiri Pirko77b99002015-05-12 14:56:21 +02001/*
2 * net/sched/cls_flower.c Flower classifier
3 *
4 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/rhashtable.h>
Daniel Borkmannd9363772016-11-27 01:18:01 +010016#include <linux/workqueue.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020017
18#include <linux/if_ether.h>
19#include <linux/in6.h>
20#include <linux/ip.h>
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -040021#include <linux/mpls.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020022
23#include <net/sch_generic.h>
24#include <net/pkt_cls.h>
25#include <net/ip.h>
26#include <net/flow_dissector.h>
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +020027#include <net/geneve.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020028
Amir Vadaibc3103f2016-09-08 16:23:47 +030029#include <net/dst.h>
30#include <net/dst_metadata.h>
31
Jiri Pirko77b99002015-05-12 14:56:21 +020032struct fl_flow_key {
33 int indev_ifindex;
Tom Herbert42aecaa2015-06-04 09:16:39 -070034 struct flow_dissector_key_control control;
Amir Vadaibc3103f2016-09-08 16:23:47 +030035 struct flow_dissector_key_control enc_control;
Jiri Pirko77b99002015-05-12 14:56:21 +020036 struct flow_dissector_key_basic basic;
37 struct flow_dissector_key_eth_addrs eth;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +030038 struct flow_dissector_key_vlan vlan;
Jianbo Liud64efd02018-07-06 05:38:16 +000039 struct flow_dissector_key_vlan cvlan;
Jiri Pirko77b99002015-05-12 14:56:21 +020040 union {
Tom Herbertc3f83242015-06-04 09:16:40 -070041 struct flow_dissector_key_ipv4_addrs ipv4;
Jiri Pirko77b99002015-05-12 14:56:21 +020042 struct flow_dissector_key_ipv6_addrs ipv6;
43 };
44 struct flow_dissector_key_ports tp;
Simon Horman7b684882016-12-07 13:48:28 +010045 struct flow_dissector_key_icmp icmp;
Simon Horman99d31322017-01-11 14:05:43 +010046 struct flow_dissector_key_arp arp;
Amir Vadaibc3103f2016-09-08 16:23:47 +030047 struct flow_dissector_key_keyid enc_key_id;
48 union {
49 struct flow_dissector_key_ipv4_addrs enc_ipv4;
50 struct flow_dissector_key_ipv6_addrs enc_ipv6;
51 };
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +020052 struct flow_dissector_key_ports enc_tp;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -040053 struct flow_dissector_key_mpls mpls;
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +020054 struct flow_dissector_key_tcp tcp;
Or Gerlitz4d80cc02017-06-01 21:37:38 +030055 struct flow_dissector_key_ip ip;
Or Gerlitz0e2c17b2018-07-17 19:27:18 +030056 struct flow_dissector_key_ip enc_ip;
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +020057 struct flow_dissector_key_enc_opts enc_opts;
Amritha Nambiar5c722992018-11-12 16:15:55 -080058 struct flow_dissector_key_ports tp_min;
59 struct flow_dissector_key_ports tp_max;
Jiri Pirko77b99002015-05-12 14:56:21 +020060} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
61
62struct fl_flow_mask_range {
63 unsigned short int start;
64 unsigned short int end;
65};
66
67struct fl_flow_mask {
68 struct fl_flow_key key;
69 struct fl_flow_mask_range range;
Amritha Nambiar5c722992018-11-12 16:15:55 -080070 u32 flags;
Paul Blakey05cd2712018-04-30 14:28:30 +030071 struct rhash_head ht_node;
72 struct rhashtable ht;
73 struct rhashtable_params filter_ht_params;
74 struct flow_dissector dissector;
75 struct list_head filters;
Paolo Abeni44a5cd42018-06-21 20:02:16 +020076 struct rcu_work rwork;
Paul Blakey05cd2712018-04-30 14:28:30 +030077 struct list_head list;
Jiri Pirko77b99002015-05-12 14:56:21 +020078};
79
Jiri Pirkob95ec7e2018-07-23 09:23:10 +020080struct fl_flow_tmplt {
81 struct fl_flow_key dummy_key;
82 struct fl_flow_key mask;
83 struct flow_dissector dissector;
84 struct tcf_chain *chain;
85};
86
Jiri Pirko77b99002015-05-12 14:56:21 +020087struct cls_fl_head {
88 struct rhashtable ht;
Paul Blakey05cd2712018-04-30 14:28:30 +030089 struct list_head masks;
Cong Wangaaa908f2018-05-23 15:26:53 -070090 struct rcu_work rwork;
Chris Mic15ab232017-08-30 02:31:58 -040091 struct idr handle_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +020092};
93
94struct cls_fl_filter {
Paul Blakey05cd2712018-04-30 14:28:30 +030095 struct fl_flow_mask *mask;
Jiri Pirko77b99002015-05-12 14:56:21 +020096 struct rhash_head ht_node;
97 struct fl_flow_key mkey;
98 struct tcf_exts exts;
99 struct tcf_result res;
100 struct fl_flow_key key;
101 struct list_head list;
102 u32 handle;
Amir Vadaie69985c2016-06-05 17:11:18 +0300103 u32 flags;
Vlad Buslov86c55362018-09-07 17:22:21 +0300104 u32 in_hw_count;
Cong Wangaaa908f2018-05-23 15:26:53 -0700105 struct rcu_work rwork;
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200106 struct net_device *hw_dev;
Jiri Pirko77b99002015-05-12 14:56:21 +0200107};
108
Paul Blakey05cd2712018-04-30 14:28:30 +0300109static const struct rhashtable_params mask_ht_params = {
110 .key_offset = offsetof(struct fl_flow_mask, key),
111 .key_len = sizeof(struct fl_flow_key),
112 .head_offset = offsetof(struct fl_flow_mask, ht_node),
113 .automatic_shrinking = true,
114};
115
Jiri Pirko77b99002015-05-12 14:56:21 +0200116static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
117{
118 return mask->range.end - mask->range.start;
119}
120
121static void fl_mask_update_range(struct fl_flow_mask *mask)
122{
123 const u8 *bytes = (const u8 *) &mask->key;
124 size_t size = sizeof(mask->key);
Paul Blakey05cd2712018-04-30 14:28:30 +0300125 size_t i, first = 0, last;
Jiri Pirko77b99002015-05-12 14:56:21 +0200126
Paul Blakey05cd2712018-04-30 14:28:30 +0300127 for (i = 0; i < size; i++) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200128 if (bytes[i]) {
Paul Blakey05cd2712018-04-30 14:28:30 +0300129 first = i;
130 break;
131 }
132 }
133 last = first;
134 for (i = size - 1; i != first; i--) {
135 if (bytes[i]) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200136 last = i;
Paul Blakey05cd2712018-04-30 14:28:30 +0300137 break;
Jiri Pirko77b99002015-05-12 14:56:21 +0200138 }
139 }
140 mask->range.start = rounddown(first, sizeof(long));
141 mask->range.end = roundup(last + 1, sizeof(long));
142}
143
144static void *fl_key_get_start(struct fl_flow_key *key,
145 const struct fl_flow_mask *mask)
146{
147 return (u8 *) key + mask->range.start;
148}
149
150static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
151 struct fl_flow_mask *mask)
152{
153 const long *lkey = fl_key_get_start(key, mask);
154 const long *lmask = fl_key_get_start(&mask->key, mask);
155 long *lmkey = fl_key_get_start(mkey, mask);
156 int i;
157
158 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
159 *lmkey++ = *lkey++ & *lmask++;
160}
161
Jiri Pirkob95ec7e2018-07-23 09:23:10 +0200162static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
163 struct fl_flow_mask *mask)
164{
165 const long *lmask = fl_key_get_start(&mask->key, mask);
166 const long *ltmplt;
167 int i;
168
169 if (!tmplt)
170 return true;
171 ltmplt = fl_key_get_start(&tmplt->mask, mask);
172 for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
173 if (~*ltmplt++ & *lmask++)
174 return false;
175 }
176 return true;
177}
178
Jiri Pirko77b99002015-05-12 14:56:21 +0200179static void fl_clear_masked_range(struct fl_flow_key *key,
180 struct fl_flow_mask *mask)
181{
182 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
183}
184
Amritha Nambiar5c722992018-11-12 16:15:55 -0800185static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter,
186 struct fl_flow_key *key,
187 struct fl_flow_key *mkey)
188{
189 __be16 min_mask, max_mask, min_val, max_val;
190
191 min_mask = htons(filter->mask->key.tp_min.dst);
192 max_mask = htons(filter->mask->key.tp_max.dst);
193 min_val = htons(filter->key.tp_min.dst);
194 max_val = htons(filter->key.tp_max.dst);
195
196 if (min_mask && max_mask) {
197 if (htons(key->tp.dst) < min_val ||
198 htons(key->tp.dst) > max_val)
199 return false;
200
201 /* skb does not have min and max values */
202 mkey->tp_min.dst = filter->mkey.tp_min.dst;
203 mkey->tp_max.dst = filter->mkey.tp_max.dst;
204 }
205 return true;
206}
207
208static bool fl_range_port_src_cmp(struct cls_fl_filter *filter,
209 struct fl_flow_key *key,
210 struct fl_flow_key *mkey)
211{
212 __be16 min_mask, max_mask, min_val, max_val;
213
214 min_mask = htons(filter->mask->key.tp_min.src);
215 max_mask = htons(filter->mask->key.tp_max.src);
216 min_val = htons(filter->key.tp_min.src);
217 max_val = htons(filter->key.tp_max.src);
218
219 if (min_mask && max_mask) {
220 if (htons(key->tp.src) < min_val ||
221 htons(key->tp.src) > max_val)
222 return false;
223
224 /* skb does not have min and max values */
225 mkey->tp_min.src = filter->mkey.tp_min.src;
226 mkey->tp_max.src = filter->mkey.tp_max.src;
227 }
228 return true;
229}
230
231static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask,
232 struct fl_flow_key *mkey)
Paul Blakeya3308d82017-01-16 10:45:13 +0200233{
Paul Blakey05cd2712018-04-30 14:28:30 +0300234 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
235 mask->filter_ht_params);
Paul Blakeya3308d82017-01-16 10:45:13 +0200236}
237
Amritha Nambiar5c722992018-11-12 16:15:55 -0800238static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask,
239 struct fl_flow_key *mkey,
240 struct fl_flow_key *key)
241{
242 struct cls_fl_filter *filter, *f;
243
244 list_for_each_entry_rcu(filter, &mask->filters, list) {
245 if (!fl_range_port_dst_cmp(filter, key, mkey))
246 continue;
247
248 if (!fl_range_port_src_cmp(filter, key, mkey))
249 continue;
250
251 f = __fl_lookup(mask, mkey);
252 if (f)
253 return f;
254 }
255 return NULL;
256}
257
258static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
259 struct fl_flow_key *mkey,
260 struct fl_flow_key *key)
261{
262 if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE))
263 return fl_lookup_range(mask, mkey, key);
264
265 return __fl_lookup(mask, mkey);
266}
267
Jiri Pirko77b99002015-05-12 14:56:21 +0200268static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
269 struct tcf_result *res)
270{
271 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
272 struct cls_fl_filter *f;
Paul Blakey05cd2712018-04-30 14:28:30 +0300273 struct fl_flow_mask *mask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200274 struct fl_flow_key skb_key;
275 struct fl_flow_key skb_mkey;
276
Paul Blakey05cd2712018-04-30 14:28:30 +0300277 list_for_each_entry_rcu(mask, &head->masks, list) {
278 fl_clear_masked_range(&skb_key, mask);
Amir Vadaie69985c2016-06-05 17:11:18 +0300279
Paul Blakey05cd2712018-04-30 14:28:30 +0300280 skb_key.indev_ifindex = skb->skb_iif;
281 /* skb_flow_dissect() does not set n_proto in case an unknown
282 * protocol, so do it rather here.
283 */
284 skb_key.basic.n_proto = skb->protocol;
285 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
286 skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
Amir Vadaibc3103f2016-09-08 16:23:47 +0300287
Paul Blakey05cd2712018-04-30 14:28:30 +0300288 fl_set_masked_key(&skb_mkey, &skb_key, mask);
Jiri Pirko77b99002015-05-12 14:56:21 +0200289
Amritha Nambiar5c722992018-11-12 16:15:55 -0800290 f = fl_lookup(mask, &skb_mkey, &skb_key);
Paul Blakey05cd2712018-04-30 14:28:30 +0300291 if (f && !tc_skip_sw(f->flags)) {
292 *res = f->res;
293 return tcf_exts_exec(skb, &f->exts, res);
294 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200295 }
296 return -1;
297}
298
299static int fl_init(struct tcf_proto *tp)
300{
301 struct cls_fl_head *head;
302
303 head = kzalloc(sizeof(*head), GFP_KERNEL);
304 if (!head)
305 return -ENOBUFS;
306
Paul Blakey05cd2712018-04-30 14:28:30 +0300307 INIT_LIST_HEAD_RCU(&head->masks);
Jiri Pirko77b99002015-05-12 14:56:21 +0200308 rcu_assign_pointer(tp->root, head);
Chris Mic15ab232017-08-30 02:31:58 -0400309 idr_init(&head->handle_idr);
Jiri Pirko77b99002015-05-12 14:56:21 +0200310
Paul Blakey05cd2712018-04-30 14:28:30 +0300311 return rhashtable_init(&head->ht, &mask_ht_params);
312}
313
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200314static void fl_mask_free(struct fl_flow_mask *mask)
315{
316 rhashtable_destroy(&mask->ht);
317 kfree(mask);
318}
319
320static void fl_mask_free_work(struct work_struct *work)
321{
322 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
323 struct fl_flow_mask, rwork);
324
325 fl_mask_free(mask);
326}
327
Paul Blakey05cd2712018-04-30 14:28:30 +0300328static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask,
329 bool async)
330{
331 if (!list_empty(&mask->filters))
332 return false;
333
334 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
Paul Blakey05cd2712018-04-30 14:28:30 +0300335 list_del_rcu(&mask->list);
336 if (async)
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200337 tcf_queue_work(&mask->rwork, fl_mask_free_work);
Paul Blakey05cd2712018-04-30 14:28:30 +0300338 else
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200339 fl_mask_free(mask);
Paul Blakey05cd2712018-04-30 14:28:30 +0300340
341 return true;
Jiri Pirko77b99002015-05-12 14:56:21 +0200342}
343
Cong Wang0dadc112017-11-06 13:47:24 -0800344static void __fl_destroy_filter(struct cls_fl_filter *f)
345{
346 tcf_exts_destroy(&f->exts);
347 tcf_exts_put_net(&f->exts);
348 kfree(f);
349}
350
Cong Wang0552c8a2017-10-26 18:24:33 -0700351static void fl_destroy_filter_work(struct work_struct *work)
352{
Cong Wangaaa908f2018-05-23 15:26:53 -0700353 struct cls_fl_filter *f = container_of(to_rcu_work(work),
354 struct cls_fl_filter, rwork);
Cong Wang0552c8a2017-10-26 18:24:33 -0700355
356 rtnl_lock();
Cong Wang0dadc112017-11-06 13:47:24 -0800357 __fl_destroy_filter(f);
Cong Wang0552c8a2017-10-26 18:24:33 -0700358 rtnl_unlock();
359}
360
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800361static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
362 struct netlink_ext_ack *extack)
Amir Vadai5b33f482016-03-08 12:42:29 +0200363{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200364 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200365 struct tcf_block *block = tp->chain->block;
Amir Vadai5b33f482016-03-08 12:42:29 +0200366
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800367 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200368 cls_flower.command = TC_CLSFLOWER_DESTROY;
369 cls_flower.cookie = (unsigned long) f;
Amir Vadai5b33f482016-03-08 12:42:29 +0200370
Cong Wangaeb3fec2018-12-11 11:15:46 -0800371 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100372 tcf_block_offload_dec(block, &f->flags);
Amir Vadai5b33f482016-03-08 12:42:29 +0200373}
374
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300375static int fl_hw_replace_filter(struct tcf_proto *tp,
Quentin Monnet41002032018-01-19 17:44:43 -0800376 struct cls_fl_filter *f,
377 struct netlink_ext_ack *extack)
Amir Vadai5b33f482016-03-08 12:42:29 +0200378{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200379 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200380 struct tcf_block *block = tp->chain->block;
Jiri Pirko717503b2017-10-11 09:41:09 +0200381 bool skip_sw = tc_skip_sw(f->flags);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300382 int err;
Amir Vadai5b33f482016-03-08 12:42:29 +0200383
Jakub Kicinskiea205942018-01-24 12:54:20 -0800384 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200385 cls_flower.command = TC_CLSFLOWER_REPLACE;
386 cls_flower.cookie = (unsigned long) f;
Paul Blakey05cd2712018-04-30 14:28:30 +0300387 cls_flower.dissector = &f->mask->dissector;
388 cls_flower.mask = &f->mask->key;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200389 cls_flower.key = &f->mkey;
390 cls_flower.exts = &f->exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700391 cls_flower.classid = f->res.classid;
Amir Vadai5b33f482016-03-08 12:42:29 +0200392
Cong Wangaeb3fec2018-12-11 11:15:46 -0800393 err = tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, skip_sw);
Jiri Pirko717503b2017-10-11 09:41:09 +0200394 if (err < 0) {
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800395 fl_hw_destroy_filter(tp, f, NULL);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300396 return err;
Jiri Pirko717503b2017-10-11 09:41:09 +0200397 } else if (err > 0) {
John Hurley31533cb2018-06-25 14:30:06 -0700398 f->in_hw_count = err;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100399 tcf_block_offload_inc(block, &f->flags);
Jiri Pirko717503b2017-10-11 09:41:09 +0200400 }
401
402 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
403 return -EINVAL;
404
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300405 return 0;
Amir Vadai5b33f482016-03-08 12:42:29 +0200406}
407
Amir Vadai10cbc682016-05-13 12:55:37 +0000408static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
409{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200410 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200411 struct tcf_block *block = tp->chain->block;
Amir Vadai10cbc682016-05-13 12:55:37 +0000412
Jakub Kicinskiea205942018-01-24 12:54:20 -0800413 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200414 cls_flower.command = TC_CLSFLOWER_STATS;
415 cls_flower.cookie = (unsigned long) f;
416 cls_flower.exts = &f->exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700417 cls_flower.classid = f->res.classid;
Amir Vadai10cbc682016-05-13 12:55:37 +0000418
Cong Wangaeb3fec2018-12-11 11:15:46 -0800419 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
Amir Vadai10cbc682016-05-13 12:55:37 +0000420}
421
Paul Blakey05cd2712018-04-30 14:28:30 +0300422static bool __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800423 struct netlink_ext_ack *extack)
Roi Dayan13fa8762016-11-01 16:08:29 +0200424{
Chris Mic15ab232017-08-30 02:31:58 -0400425 struct cls_fl_head *head = rtnl_dereference(tp->root);
Paul Blakey05cd2712018-04-30 14:28:30 +0300426 bool async = tcf_exts_get_net(&f->exts);
427 bool last;
Chris Mic15ab232017-08-30 02:31:58 -0400428
Matthew Wilcox9c160942017-11-28 09:48:43 -0500429 idr_remove(&head->handle_idr, f->handle);
Roi Dayan13fa8762016-11-01 16:08:29 +0200430 list_del_rcu(&f->list);
Paul Blakey05cd2712018-04-30 14:28:30 +0300431 last = fl_mask_put(head, f->mask, async);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200432 if (!tc_skip_hw(f->flags))
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800433 fl_hw_destroy_filter(tp, f, extack);
Roi Dayan13fa8762016-11-01 16:08:29 +0200434 tcf_unbind_filter(tp, &f->res);
Paul Blakey05cd2712018-04-30 14:28:30 +0300435 if (async)
Cong Wangaaa908f2018-05-23 15:26:53 -0700436 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
Cong Wang0dadc112017-11-06 13:47:24 -0800437 else
438 __fl_destroy_filter(f);
Paul Blakey05cd2712018-04-30 14:28:30 +0300439
440 return last;
Roi Dayan13fa8762016-11-01 16:08:29 +0200441}
442
Daniel Borkmannd9363772016-11-27 01:18:01 +0100443static void fl_destroy_sleepable(struct work_struct *work)
444{
Cong Wangaaa908f2018-05-23 15:26:53 -0700445 struct cls_fl_head *head = container_of(to_rcu_work(work),
446 struct cls_fl_head,
447 rwork);
Paul Blakeyde9dc652018-06-03 10:06:13 +0300448
449 rhashtable_destroy(&head->ht);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100450 kfree(head);
451 module_put(THIS_MODULE);
452}
453
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800454static void fl_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200455{
456 struct cls_fl_head *head = rtnl_dereference(tp->root);
Paul Blakey05cd2712018-04-30 14:28:30 +0300457 struct fl_flow_mask *mask, *next_mask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200458 struct cls_fl_filter *f, *next;
459
Paul Blakey05cd2712018-04-30 14:28:30 +0300460 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
461 list_for_each_entry_safe(f, next, &mask->filters, list) {
462 if (__fl_delete(tp, f, extack))
463 break;
464 }
465 }
Chris Mic15ab232017-08-30 02:31:58 -0400466 idr_destroy(&head->handle_idr);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100467
468 __module_get(THIS_MODULE);
Cong Wangaaa908f2018-05-23 15:26:53 -0700469 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
Jiri Pirko77b99002015-05-12 14:56:21 +0200470}
471
WANG Cong8113c092017-08-04 21:31:43 -0700472static void *fl_get(struct tcf_proto *tp, u32 handle)
Jiri Pirko77b99002015-05-12 14:56:21 +0200473{
474 struct cls_fl_head *head = rtnl_dereference(tp->root);
Jiri Pirko77b99002015-05-12 14:56:21 +0200475
Matthew Wilcox322d8842017-11-28 10:01:24 -0500476 return idr_find(&head->handle_idr, handle);
Jiri Pirko77b99002015-05-12 14:56:21 +0200477}
478
479static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
480 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
481 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
482 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
483 .len = IFNAMSIZ },
484 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
485 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
486 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
487 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
488 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
489 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
490 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
491 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
492 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
493 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
494 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
495 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
496 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
497 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
498 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
499 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
Jamal Hadi Salimb175c3a2015-06-25 06:55:27 -0400500 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
501 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300502 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
503 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
504 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
Amir Vadaibc3103f2016-09-08 16:23:47 +0300505 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
506 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
507 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
508 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
509 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
510 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
511 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
512 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
513 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
Or Gerlitzaa72d702016-09-15 15:28:22 +0300514 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
515 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
516 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
517 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
Simon Horman5976c5f2016-11-03 13:24:21 +0100518 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
519 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
520 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
521 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200522 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
523 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
524 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
525 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200526 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
527 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
Simon Horman7b684882016-12-07 13:48:28 +0100528 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
529 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
530 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
531 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
532 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
533 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
534 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
535 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
Simon Horman99d31322017-01-11 14:05:43 +0100536 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
537 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
538 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
539 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
540 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
541 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
542 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
543 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
544 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
545 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400546 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
547 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
548 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
549 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200550 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
551 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300552 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
553 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
554 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
555 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
Jianbo Liud64efd02018-07-06 05:38:16 +0000556 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
557 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
558 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300559 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
560 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
561 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
562 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200563 [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
564 [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
565};
566
567static const struct nla_policy
568enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
569 [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
570};
571
572static const struct nla_policy
573geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
574 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
575 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
576 [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
577 .len = 128 },
Jiri Pirko77b99002015-05-12 14:56:21 +0200578};
579
580static void fl_set_key_val(struct nlattr **tb,
581 void *val, int val_type,
582 void *mask, int mask_type, int len)
583{
584 if (!tb[val_type])
585 return;
586 memcpy(val, nla_data(tb[val_type]), len);
587 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
588 memset(mask, 0xff, len);
589 else
590 memcpy(mask, nla_data(tb[mask_type]), len);
591}
592
Amritha Nambiar5c722992018-11-12 16:15:55 -0800593static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key,
594 struct fl_flow_key *mask)
595{
596 fl_set_key_val(tb, &key->tp_min.dst,
597 TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_min.dst,
598 TCA_FLOWER_UNSPEC, sizeof(key->tp_min.dst));
599 fl_set_key_val(tb, &key->tp_max.dst,
600 TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_max.dst,
601 TCA_FLOWER_UNSPEC, sizeof(key->tp_max.dst));
602 fl_set_key_val(tb, &key->tp_min.src,
603 TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_min.src,
604 TCA_FLOWER_UNSPEC, sizeof(key->tp_min.src));
605 fl_set_key_val(tb, &key->tp_max.src,
606 TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_max.src,
607 TCA_FLOWER_UNSPEC, sizeof(key->tp_max.src));
608
609 if ((mask->tp_min.dst && mask->tp_max.dst &&
610 htons(key->tp_max.dst) <= htons(key->tp_min.dst)) ||
611 (mask->tp_min.src && mask->tp_max.src &&
612 htons(key->tp_max.src) <= htons(key->tp_min.src)))
613 return -EINVAL;
614
615 return 0;
616}
617
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400618static int fl_set_key_mpls(struct nlattr **tb,
619 struct flow_dissector_key_mpls *key_val,
620 struct flow_dissector_key_mpls *key_mask)
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400621{
622 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
623 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
624 key_mask->mpls_ttl = MPLS_TTL_MASK;
625 }
626 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400627 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
628
629 if (bos & ~MPLS_BOS_MASK)
630 return -EINVAL;
631 key_val->mpls_bos = bos;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400632 key_mask->mpls_bos = MPLS_BOS_MASK;
633 }
634 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400635 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
636
637 if (tc & ~MPLS_TC_MASK)
638 return -EINVAL;
639 key_val->mpls_tc = tc;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400640 key_mask->mpls_tc = MPLS_TC_MASK;
641 }
642 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400643 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
644
645 if (label & ~MPLS_LABEL_MASK)
646 return -EINVAL;
647 key_val->mpls_label = label;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400648 key_mask->mpls_label = MPLS_LABEL_MASK;
649 }
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400650 return 0;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400651}
652
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300653static void fl_set_key_vlan(struct nlattr **tb,
Jianbo Liuaaab0832018-07-06 05:38:13 +0000654 __be16 ethertype,
Jianbo Liud64efd02018-07-06 05:38:16 +0000655 int vlan_id_key, int vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300656 struct flow_dissector_key_vlan *key_val,
657 struct flow_dissector_key_vlan *key_mask)
658{
659#define VLAN_PRIORITY_MASK 0x7
660
Jianbo Liud64efd02018-07-06 05:38:16 +0000661 if (tb[vlan_id_key]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300662 key_val->vlan_id =
Jianbo Liud64efd02018-07-06 05:38:16 +0000663 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300664 key_mask->vlan_id = VLAN_VID_MASK;
665 }
Jianbo Liud64efd02018-07-06 05:38:16 +0000666 if (tb[vlan_prio_key]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300667 key_val->vlan_priority =
Jianbo Liud64efd02018-07-06 05:38:16 +0000668 nla_get_u8(tb[vlan_prio_key]) &
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300669 VLAN_PRIORITY_MASK;
670 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
671 }
Jianbo Liuaaab0832018-07-06 05:38:13 +0000672 key_val->vlan_tpid = ethertype;
673 key_mask->vlan_tpid = cpu_to_be16(~0);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300674}
675
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200676static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
677 u32 *dissector_key, u32 *dissector_mask,
678 u32 flower_flag_bit, u32 dissector_flag_bit)
679{
680 if (flower_mask & flower_flag_bit) {
681 *dissector_mask |= dissector_flag_bit;
682 if (flower_key & flower_flag_bit)
683 *dissector_key |= dissector_flag_bit;
684 }
685}
686
Or Gerlitzd9724772016-12-22 14:28:15 +0200687static int fl_set_key_flags(struct nlattr **tb,
688 u32 *flags_key, u32 *flags_mask)
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200689{
690 u32 key, mask;
691
Or Gerlitzd9724772016-12-22 14:28:15 +0200692 /* mask is mandatory for flags */
693 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
694 return -EINVAL;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200695
696 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
Or Gerlitzd9724772016-12-22 14:28:15 +0200697 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200698
699 *flags_key = 0;
700 *flags_mask = 0;
701
702 fl_set_key_flag(key, mask, flags_key, flags_mask,
703 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +0100704 fl_set_key_flag(key, mask, flags_key, flags_mask,
705 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
706 FLOW_DIS_FIRST_FRAG);
Or Gerlitzd9724772016-12-22 14:28:15 +0200707
708 return 0;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200709}
710
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300711static void fl_set_key_ip(struct nlattr **tb, bool encap,
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300712 struct flow_dissector_key_ip *key,
713 struct flow_dissector_key_ip *mask)
714{
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300715 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
716 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
717 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
718 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300719
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300720 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
721 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300722}
723
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200724static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
725 int depth, int option_len,
726 struct netlink_ext_ack *extack)
727{
728 struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
729 struct nlattr *class = NULL, *type = NULL, *data = NULL;
730 struct geneve_opt *opt;
731 int err, data_len = 0;
732
733 if (option_len > sizeof(struct geneve_opt))
734 data_len = option_len - sizeof(struct geneve_opt);
735
736 opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
737 memset(opt, 0xff, option_len);
738 opt->length = data_len / 4;
739 opt->r1 = 0;
740 opt->r2 = 0;
741 opt->r3 = 0;
742
743 /* If no mask has been prodived we assume an exact match. */
744 if (!depth)
745 return sizeof(struct geneve_opt) + data_len;
746
747 if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
748 NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
749 return -EINVAL;
750 }
751
752 err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
753 nla, geneve_opt_policy, extack);
754 if (err < 0)
755 return err;
756
757 /* We are not allowed to omit any of CLASS, TYPE or DATA
758 * fields from the key.
759 */
760 if (!option_len &&
761 (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
762 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
763 !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
764 NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
765 return -EINVAL;
766 }
767
768 /* Omitting any of CLASS, TYPE or DATA fields is allowed
769 * for the mask.
770 */
771 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
772 int new_len = key->enc_opts.len;
773
774 data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
775 data_len = nla_len(data);
776 if (data_len < 4) {
777 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
778 return -ERANGE;
779 }
780 if (data_len % 4) {
781 NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
782 return -ERANGE;
783 }
784
785 new_len += sizeof(struct geneve_opt) + data_len;
786 BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
787 if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
788 NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
789 return -ERANGE;
790 }
791 opt->length = data_len / 4;
792 memcpy(opt->opt_data, nla_data(data), data_len);
793 }
794
795 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
796 class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
797 opt->opt_class = nla_get_be16(class);
798 }
799
800 if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
801 type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
802 opt->type = nla_get_u8(type);
803 }
804
805 return sizeof(struct geneve_opt) + data_len;
806}
807
808static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
809 struct fl_flow_key *mask,
810 struct netlink_ext_ack *extack)
811{
812 const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
Jakub Kicinski63c82992018-11-09 21:06:26 -0800813 int err, option_len, key_depth, msk_depth = 0;
814
815 err = nla_validate_nested(tb[TCA_FLOWER_KEY_ENC_OPTS],
816 TCA_FLOWER_KEY_ENC_OPTS_MAX,
817 enc_opts_policy, extack);
818 if (err)
819 return err;
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200820
821 nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
822
823 if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
Jakub Kicinski63c82992018-11-09 21:06:26 -0800824 err = nla_validate_nested(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
825 TCA_FLOWER_KEY_ENC_OPTS_MAX,
826 enc_opts_policy, extack);
827 if (err)
828 return err;
829
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +0200830 nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
831 msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
832 }
833
834 nla_for_each_attr(nla_opt_key, nla_enc_key,
835 nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
836 switch (nla_type(nla_opt_key)) {
837 case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
838 option_len = 0;
839 key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
840 option_len = fl_set_geneve_opt(nla_opt_key, key,
841 key_depth, option_len,
842 extack);
843 if (option_len < 0)
844 return option_len;
845
846 key->enc_opts.len += option_len;
847 /* At the same time we need to parse through the mask
848 * in order to verify exact and mask attribute lengths.
849 */
850 mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
851 option_len = fl_set_geneve_opt(nla_opt_msk, mask,
852 msk_depth, option_len,
853 extack);
854 if (option_len < 0)
855 return option_len;
856
857 mask->enc_opts.len += option_len;
858 if (key->enc_opts.len != mask->enc_opts.len) {
859 NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
860 return -EINVAL;
861 }
862
863 if (msk_depth)
864 nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
865 break;
866 default:
867 NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
868 return -EINVAL;
869 }
870 }
871
872 return 0;
873}
874
Jiri Pirko77b99002015-05-12 14:56:21 +0200875static int fl_set_key(struct net *net, struct nlattr **tb,
Alexander Aring1057c552018-01-18 11:20:54 -0500876 struct fl_flow_key *key, struct fl_flow_key *mask,
877 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200878{
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300879 __be16 ethertype;
Or Gerlitzd9724772016-12-22 14:28:15 +0200880 int ret = 0;
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400881#ifdef CONFIG_NET_CLS_IND
Jiri Pirko77b99002015-05-12 14:56:21 +0200882 if (tb[TCA_FLOWER_INDEV]) {
Alexander Aring1057c552018-01-18 11:20:54 -0500883 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200884 if (err < 0)
885 return err;
886 key->indev_ifindex = err;
887 mask->indev_ifindex = 0xffffffff;
888 }
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400889#endif
Jiri Pirko77b99002015-05-12 14:56:21 +0200890
891 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
892 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
893 sizeof(key->eth.dst));
894 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
895 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
896 sizeof(key->eth.src));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500897
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200898 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300899 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
900
Jianbo Liuaaab0832018-07-06 05:38:13 +0000901 if (eth_type_vlan(ethertype)) {
Jianbo Liud64efd02018-07-06 05:38:16 +0000902 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
903 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan,
904 &mask->vlan);
905
Jianbo Liu5e9a0fe2018-07-09 02:26:20 +0000906 if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
907 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
908 if (eth_type_vlan(ethertype)) {
909 fl_set_key_vlan(tb, ethertype,
910 TCA_FLOWER_KEY_CVLAN_ID,
911 TCA_FLOWER_KEY_CVLAN_PRIO,
912 &key->cvlan, &mask->cvlan);
913 fl_set_key_val(tb, &key->basic.n_proto,
914 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
915 &mask->basic.n_proto,
916 TCA_FLOWER_UNSPEC,
917 sizeof(key->basic.n_proto));
918 } else {
919 key->basic.n_proto = ethertype;
920 mask->basic.n_proto = cpu_to_be16(~0);
921 }
Jianbo Liud64efd02018-07-06 05:38:16 +0000922 }
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200923 } else {
924 key->basic.n_proto = ethertype;
925 mask->basic.n_proto = cpu_to_be16(~0);
926 }
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300927 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500928
Jiri Pirko77b99002015-05-12 14:56:21 +0200929 if (key->basic.n_proto == htons(ETH_P_IP) ||
930 key->basic.n_proto == htons(ETH_P_IPV6)) {
931 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
932 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
933 sizeof(key->basic.ip_proto));
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300934 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
Jiri Pirko77b99002015-05-12 14:56:21 +0200935 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500936
937 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
938 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200939 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200940 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
941 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
942 sizeof(key->ipv4.src));
943 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
944 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
945 sizeof(key->ipv4.dst));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500946 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
947 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200948 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200949 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
950 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
951 sizeof(key->ipv6.src));
952 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
953 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
954 sizeof(key->ipv6.dst));
955 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500956
Jiri Pirko77b99002015-05-12 14:56:21 +0200957 if (key->basic.ip_proto == IPPROTO_TCP) {
958 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300959 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200960 sizeof(key->tp.src));
961 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300962 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200963 sizeof(key->tp.dst));
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200964 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
965 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
966 sizeof(key->tcp.flags));
Jiri Pirko77b99002015-05-12 14:56:21 +0200967 } else if (key->basic.ip_proto == IPPROTO_UDP) {
968 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300969 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200970 sizeof(key->tp.src));
971 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300972 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200973 sizeof(key->tp.dst));
Simon Horman5976c5f2016-11-03 13:24:21 +0100974 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
975 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
976 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
977 sizeof(key->tp.src));
978 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
979 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
980 sizeof(key->tp.dst));
Simon Horman7b684882016-12-07 13:48:28 +0100981 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
982 key->basic.ip_proto == IPPROTO_ICMP) {
983 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
984 &mask->icmp.type,
985 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
986 sizeof(key->icmp.type));
987 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
988 &mask->icmp.code,
989 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
990 sizeof(key->icmp.code));
991 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
992 key->basic.ip_proto == IPPROTO_ICMPV6) {
993 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
994 &mask->icmp.type,
995 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
996 sizeof(key->icmp.type));
Simon Horman040587a2017-01-30 16:19:02 +0100997 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
Simon Horman7b684882016-12-07 13:48:28 +0100998 &mask->icmp.code,
Simon Horman040587a2017-01-30 16:19:02 +0100999 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
Simon Horman7b684882016-12-07 13:48:28 +01001000 sizeof(key->icmp.code));
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001001 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
1002 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -04001003 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
1004 if (ret)
1005 return ret;
Simon Horman99d31322017-01-11 14:05:43 +01001006 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
1007 key->basic.n_proto == htons(ETH_P_RARP)) {
1008 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
1009 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
1010 sizeof(key->arp.sip));
1011 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
1012 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
1013 sizeof(key->arp.tip));
1014 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
1015 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
1016 sizeof(key->arp.op));
1017 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1018 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1019 sizeof(key->arp.sha));
1020 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1021 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1022 sizeof(key->arp.tha));
Jiri Pirko77b99002015-05-12 14:56:21 +02001023 }
1024
Amritha Nambiar5c722992018-11-12 16:15:55 -08001025 if (key->basic.ip_proto == IPPROTO_TCP ||
1026 key->basic.ip_proto == IPPROTO_UDP ||
1027 key->basic.ip_proto == IPPROTO_SCTP) {
1028 ret = fl_set_key_port_range(tb, key, mask);
1029 if (ret)
1030 return ret;
1031 }
1032
Amir Vadaibc3103f2016-09-08 16:23:47 +03001033 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
1034 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
1035 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +02001036 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +03001037 fl_set_key_val(tb, &key->enc_ipv4.src,
1038 TCA_FLOWER_KEY_ENC_IPV4_SRC,
1039 &mask->enc_ipv4.src,
1040 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1041 sizeof(key->enc_ipv4.src));
1042 fl_set_key_val(tb, &key->enc_ipv4.dst,
1043 TCA_FLOWER_KEY_ENC_IPV4_DST,
1044 &mask->enc_ipv4.dst,
1045 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1046 sizeof(key->enc_ipv4.dst));
1047 }
1048
1049 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
1050 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
1051 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +02001052 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +03001053 fl_set_key_val(tb, &key->enc_ipv6.src,
1054 TCA_FLOWER_KEY_ENC_IPV6_SRC,
1055 &mask->enc_ipv6.src,
1056 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1057 sizeof(key->enc_ipv6.src));
1058 fl_set_key_val(tb, &key->enc_ipv6.dst,
1059 TCA_FLOWER_KEY_ENC_IPV6_DST,
1060 &mask->enc_ipv6.dst,
1061 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1062 sizeof(key->enc_ipv6.dst));
1063 }
1064
1065 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001066 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
Amir Vadaibc3103f2016-09-08 16:23:47 +03001067 sizeof(key->enc_key_id.keyid));
1068
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001069 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1070 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1071 sizeof(key->enc_tp.src));
1072
1073 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1074 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1075 sizeof(key->enc_tp.dst));
1076
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001077 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
1078
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001079 if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
1080 ret = fl_set_enc_opt(tb, key, mask, extack);
1081 if (ret)
1082 return ret;
1083 }
1084
Or Gerlitzd9724772016-12-22 14:28:15 +02001085 if (tb[TCA_FLOWER_KEY_FLAGS])
1086 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001087
Or Gerlitzd9724772016-12-22 14:28:15 +02001088 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +02001089}
1090
Paul Blakey05cd2712018-04-30 14:28:30 +03001091static void fl_mask_copy(struct fl_flow_mask *dst,
1092 struct fl_flow_mask *src)
Jiri Pirko77b99002015-05-12 14:56:21 +02001093{
Paul Blakey05cd2712018-04-30 14:28:30 +03001094 const void *psrc = fl_key_get_start(&src->key, src);
1095 void *pdst = fl_key_get_start(&dst->key, src);
Jiri Pirko77b99002015-05-12 14:56:21 +02001096
Paul Blakey05cd2712018-04-30 14:28:30 +03001097 memcpy(pdst, psrc, fl_mask_range(src));
1098 dst->range = src->range;
Jiri Pirko77b99002015-05-12 14:56:21 +02001099}
1100
1101static const struct rhashtable_params fl_ht_params = {
1102 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
1103 .head_offset = offsetof(struct cls_fl_filter, ht_node),
1104 .automatic_shrinking = true,
1105};
1106
Paul Blakey05cd2712018-04-30 14:28:30 +03001107static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +02001108{
Paul Blakey05cd2712018-04-30 14:28:30 +03001109 mask->filter_ht_params = fl_ht_params;
1110 mask->filter_ht_params.key_len = fl_mask_range(mask);
1111 mask->filter_ht_params.key_offset += mask->range.start;
Jiri Pirko77b99002015-05-12 14:56:21 +02001112
Paul Blakey05cd2712018-04-30 14:28:30 +03001113 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
Jiri Pirko77b99002015-05-12 14:56:21 +02001114}
1115
1116#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
zhong jiangcb205a82018-09-19 19:32:11 +08001117#define FL_KEY_MEMBER_SIZE(member) FIELD_SIZEOF(struct fl_flow_key, member)
Jiri Pirko77b99002015-05-12 14:56:21 +02001118
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001119#define FL_KEY_IS_MASKED(mask, member) \
1120 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
1121 0, FL_KEY_MEMBER_SIZE(member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +02001122
1123#define FL_KEY_SET(keys, cnt, id, member) \
1124 do { \
1125 keys[cnt].key_id = id; \
1126 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
1127 cnt++; \
1128 } while(0);
1129
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001130#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
Jiri Pirko77b99002015-05-12 14:56:21 +02001131 do { \
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001132 if (FL_KEY_IS_MASKED(mask, member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +02001133 FL_KEY_SET(keys, cnt, id, member); \
1134 } while(0);
1135
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001136static void fl_init_dissector(struct flow_dissector *dissector,
1137 struct fl_flow_key *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +02001138{
1139 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
1140 size_t cnt = 0;
1141
Tom Herbert42aecaa2015-06-04 09:16:39 -07001142 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
Jiri Pirko77b99002015-05-12 14:56:21 +02001143 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001144 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001145 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001146 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001147 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001148 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +03001149 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
Amritha Nambiar5c722992018-11-12 16:15:55 -08001150 if (FL_KEY_IS_MASKED(mask, tp) ||
1151 FL_KEY_IS_MASKED(mask, tp_min) || FL_KEY_IS_MASKED(mask, tp_max))
1152 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_PORTS, tp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001153 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001154 FLOW_DISSECTOR_KEY_IP, ip);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001155 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001156 FLOW_DISSECTOR_KEY_TCP, tcp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001157 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Simon Horman7b684882016-12-07 13:48:28 +01001158 FLOW_DISSECTOR_KEY_ICMP, icmp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001159 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Simon Horman99d31322017-01-11 14:05:43 +01001160 FLOW_DISSECTOR_KEY_ARP, arp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001161 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001162 FLOW_DISSECTOR_KEY_MPLS, mpls);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001163 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001164 FLOW_DISSECTOR_KEY_VLAN, vlan);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001165 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Jianbo Liud64efd02018-07-06 05:38:16 +00001166 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001167 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +02001168 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001169 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +02001170 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001171 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +02001172 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001173 if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
1174 FL_KEY_IS_MASKED(mask, enc_ipv6))
Hadar Hen Zion519d1052016-11-07 15:14:38 +02001175 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
1176 enc_control);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001177 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001178 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001179 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001180 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001181 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
1182 FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
Jiri Pirko77b99002015-05-12 14:56:21 +02001183
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001184 skb_flow_dissector_init(dissector, keys, cnt);
Paul Blakey05cd2712018-04-30 14:28:30 +03001185}
1186
1187static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
1188 struct fl_flow_mask *mask)
1189{
1190 struct fl_flow_mask *newmask;
1191 int err;
1192
1193 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
1194 if (!newmask)
1195 return ERR_PTR(-ENOMEM);
1196
1197 fl_mask_copy(newmask, mask);
1198
Amritha Nambiar5c722992018-11-12 16:15:55 -08001199 if ((newmask->key.tp_min.dst && newmask->key.tp_max.dst) ||
1200 (newmask->key.tp_min.src && newmask->key.tp_max.src))
1201 newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE;
1202
Paul Blakey05cd2712018-04-30 14:28:30 +03001203 err = fl_init_mask_hashtable(newmask);
1204 if (err)
1205 goto errout_free;
1206
Jiri Pirko33fb5cb2018-07-23 09:23:09 +02001207 fl_init_dissector(&newmask->dissector, &newmask->key);
Paul Blakey05cd2712018-04-30 14:28:30 +03001208
1209 INIT_LIST_HEAD_RCU(&newmask->filters);
1210
1211 err = rhashtable_insert_fast(&head->ht, &newmask->ht_node,
1212 mask_ht_params);
1213 if (err)
1214 goto errout_destroy;
1215
1216 list_add_tail_rcu(&newmask->list, &head->masks);
1217
1218 return newmask;
1219
1220errout_destroy:
1221 rhashtable_destroy(&newmask->ht);
1222errout_free:
1223 kfree(newmask);
1224
1225 return ERR_PTR(err);
Jiri Pirko77b99002015-05-12 14:56:21 +02001226}
1227
1228static int fl_check_assign_mask(struct cls_fl_head *head,
Paul Blakey05cd2712018-04-30 14:28:30 +03001229 struct cls_fl_filter *fnew,
1230 struct cls_fl_filter *fold,
Jiri Pirko77b99002015-05-12 14:56:21 +02001231 struct fl_flow_mask *mask)
1232{
Paul Blakey05cd2712018-04-30 14:28:30 +03001233 struct fl_flow_mask *newmask;
Jiri Pirko77b99002015-05-12 14:56:21 +02001234
Paul Blakey05cd2712018-04-30 14:28:30 +03001235 fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params);
1236 if (!fnew->mask) {
1237 if (fold)
Jiri Pirko77b99002015-05-12 14:56:21 +02001238 return -EINVAL;
Paul Blakey05cd2712018-04-30 14:28:30 +03001239
1240 newmask = fl_create_new_mask(head, mask);
1241 if (IS_ERR(newmask))
1242 return PTR_ERR(newmask);
1243
1244 fnew->mask = newmask;
Paul Blakeyf6521c52018-06-03 10:06:14 +03001245 } else if (fold && fold->mask != fnew->mask) {
Paul Blakey05cd2712018-04-30 14:28:30 +03001246 return -EINVAL;
Jiri Pirko77b99002015-05-12 14:56:21 +02001247 }
1248
Jiri Pirko77b99002015-05-12 14:56:21 +02001249 return 0;
1250}
1251
1252static int fl_set_parms(struct net *net, struct tcf_proto *tp,
1253 struct cls_fl_filter *f, struct fl_flow_mask *mask,
1254 unsigned long base, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -05001255 struct nlattr *est, bool ovr,
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001256 struct fl_flow_tmplt *tmplt,
Alexander Aring50a56192018-01-18 11:20:52 -05001257 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001258{
Jiri Pirko77b99002015-05-12 14:56:21 +02001259 int err;
1260
Alexander Aring50a56192018-01-18 11:20:52 -05001261 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +02001262 if (err < 0)
1263 return err;
1264
1265 if (tb[TCA_FLOWER_CLASSID]) {
1266 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
1267 tcf_bind_filter(tp, &f->res, base);
1268 }
1269
Alexander Aring1057c552018-01-18 11:20:54 -05001270 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +02001271 if (err)
Jiri Pirko45507522017-08-04 14:29:06 +02001272 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +02001273
1274 fl_mask_update_range(mask);
1275 fl_set_masked_key(&f->mkey, &f->key, mask);
1276
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001277 if (!fl_mask_fits_tmplt(tmplt, mask)) {
1278 NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
1279 return -EINVAL;
1280 }
1281
Jiri Pirko77b99002015-05-12 14:56:21 +02001282 return 0;
Jiri Pirko77b99002015-05-12 14:56:21 +02001283}
1284
Jiri Pirko77b99002015-05-12 14:56:21 +02001285static int fl_change(struct net *net, struct sk_buff *in_skb,
1286 struct tcf_proto *tp, unsigned long base,
1287 u32 handle, struct nlattr **tca,
Alexander Aring7306db32018-01-18 11:20:51 -05001288 void **arg, bool ovr, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001289{
1290 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -07001291 struct cls_fl_filter *fold = *arg;
Jiri Pirko77b99002015-05-12 14:56:21 +02001292 struct cls_fl_filter *fnew;
Ivan Vecera2cddd202019-01-16 16:53:52 +01001293 struct fl_flow_mask *mask;
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001294 struct nlattr **tb;
Jiri Pirko77b99002015-05-12 14:56:21 +02001295 int err;
1296
1297 if (!tca[TCA_OPTIONS])
1298 return -EINVAL;
1299
Ivan Vecera2cddd202019-01-16 16:53:52 +01001300 mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
1301 if (!mask)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001302 return -ENOBUFS;
1303
Ivan Vecera2cddd202019-01-16 16:53:52 +01001304 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1305 if (!tb) {
1306 err = -ENOBUFS;
1307 goto errout_mask_alloc;
1308 }
1309
Johannes Bergfceb6432017-04-12 14:34:07 +02001310 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
1311 fl_policy, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +02001312 if (err < 0)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001313 goto errout_tb;
Jiri Pirko77b99002015-05-12 14:56:21 +02001314
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001315 if (fold && handle && fold->handle != handle) {
1316 err = -EINVAL;
1317 goto errout_tb;
1318 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001319
1320 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001321 if (!fnew) {
1322 err = -ENOBUFS;
1323 goto errout_tb;
1324 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001325
WANG Congb9a24bb2016-08-19 12:36:54 -07001326 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
1327 if (err < 0)
1328 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +02001329
1330 if (!handle) {
Matthew Wilcox85bd0432017-11-28 10:53:03 -05001331 handle = 1;
1332 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1333 INT_MAX, GFP_KERNEL);
1334 } else if (!fold) {
1335 /* user specifies a handle and it doesn't exist */
1336 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1337 handle, GFP_KERNEL);
Jiri Pirko77b99002015-05-12 14:56:21 +02001338 }
Matthew Wilcox85bd0432017-11-28 10:53:03 -05001339 if (err)
1340 goto errout;
1341 fnew->handle = handle;
Jiri Pirko77b99002015-05-12 14:56:21 +02001342
Amir Vadaie69985c2016-06-05 17:11:18 +03001343 if (tb[TCA_FLOWER_FLAGS]) {
1344 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
1345
1346 if (!tc_flags_valid(fnew->flags)) {
1347 err = -EINVAL;
Cong Wangfe2502e2017-09-20 09:18:45 -07001348 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +03001349 }
1350 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001351
Ivan Vecera2cddd202019-01-16 16:53:52 +01001352 err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr,
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001353 tp->chain->tmplt_priv, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +02001354 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -07001355 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +02001356
Ivan Vecera2cddd202019-01-16 16:53:52 +01001357 err = fl_check_assign_mask(head, fnew, fold, mask);
Jiri Pirko77b99002015-05-12 14:56:21 +02001358 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -07001359 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +02001360
David S. Miller4cc1fee2018-12-09 21:27:48 -08001361 if (!fold && __fl_lookup(fnew->mask, &fnew->mkey)) {
Or Gerlitz35cc3cef2018-12-09 18:10:24 +02001362 err = -EEXIST;
1363 goto errout_mask;
Amir Vadaie69985c2016-06-05 17:11:18 +03001364 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001365
Or Gerlitz35cc3cef2018-12-09 18:10:24 +02001366 err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node,
1367 fnew->mask->filter_ht_params);
1368 if (err)
1369 goto errout_mask;
1370
Hadar Hen Zion79685212016-12-01 14:06:34 +02001371 if (!tc_skip_hw(fnew->flags)) {
Paul Blakey05cd2712018-04-30 14:28:30 +03001372 err = fl_hw_replace_filter(tp, fnew, extack);
Hadar Hen Zion79685212016-12-01 14:06:34 +02001373 if (err)
Petr Machatac1f7e022019-02-04 14:50:38 +00001374 goto errout_mask_ht;
Hadar Hen Zion79685212016-12-01 14:06:34 +02001375 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001376
Or Gerlitz55593962017-02-16 10:31:13 +02001377 if (!tc_in_hw(fnew->flags))
1378 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1379
Amir Vadai5b33f482016-03-08 12:42:29 +02001380 if (fold) {
Roi Dayan599d2572018-12-19 18:07:56 +02001381 rhashtable_remove_fast(&fold->mask->ht,
1382 &fold->ht_node,
1383 fold->mask->filter_ht_params);
Hadar Hen Zion79685212016-12-01 14:06:34 +02001384 if (!tc_skip_hw(fold->flags))
Jakub Kicinski1b0f8032018-01-24 12:54:21 -08001385 fl_hw_destroy_filter(tp, fold, NULL);
Amir Vadai5b33f482016-03-08 12:42:29 +02001386 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001387
WANG Cong8113c092017-08-04 21:31:43 -07001388 *arg = fnew;
Jiri Pirko77b99002015-05-12 14:56:21 +02001389
1390 if (fold) {
Matthew Wilcox234a4622017-11-28 09:56:36 -05001391 idr_replace(&head->handle_idr, fnew, fnew->handle);
Daniel Borkmannff3532f2015-07-17 22:38:44 +02001392 list_replace_rcu(&fold->list, &fnew->list);
Jiri Pirko77b99002015-05-12 14:56:21 +02001393 tcf_unbind_filter(tp, &fold->res);
Cong Wang0dadc112017-11-06 13:47:24 -08001394 tcf_exts_get_net(&fold->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -07001395 tcf_queue_work(&fold->rwork, fl_destroy_filter_work);
Jiri Pirko77b99002015-05-12 14:56:21 +02001396 } else {
Paul Blakey05cd2712018-04-30 14:28:30 +03001397 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
Jiri Pirko77b99002015-05-12 14:56:21 +02001398 }
1399
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001400 kfree(tb);
Ivan Vecera2cddd202019-01-16 16:53:52 +01001401 kfree(mask);
Jiri Pirko77b99002015-05-12 14:56:21 +02001402 return 0;
1403
Petr Machatac1f7e022019-02-04 14:50:38 +00001404errout_mask_ht:
1405 rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node,
1406 fnew->mask->filter_ht_params);
1407
Paul Blakey05cd2712018-04-30 14:28:30 +03001408errout_mask:
1409 fl_mask_put(head, fnew->mask, false);
1410
Cong Wangfe2502e2017-09-20 09:18:45 -07001411errout_idr:
Paul Blakey8258d2d2018-05-30 11:29:15 +03001412 if (!fold)
Matthew Wilcox9c160942017-11-28 09:48:43 -05001413 idr_remove(&head->handle_idr, fnew->handle);
Jiri Pirko77b99002015-05-12 14:56:21 +02001414errout:
WANG Congb9a24bb2016-08-19 12:36:54 -07001415 tcf_exts_destroy(&fnew->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +02001416 kfree(fnew);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001417errout_tb:
1418 kfree(tb);
Ivan Vecera2cddd202019-01-16 16:53:52 +01001419errout_mask_alloc:
1420 kfree(mask);
Jiri Pirko77b99002015-05-12 14:56:21 +02001421 return err;
1422}
1423
Alexander Aring571acf22018-01-18 11:20:53 -05001424static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
1425 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001426{
1427 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -07001428 struct cls_fl_filter *f = arg;
Jiri Pirko77b99002015-05-12 14:56:21 +02001429
Or Gerlitz35cc3cef2018-12-09 18:10:24 +02001430 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
1431 f->mask->filter_ht_params);
Jakub Kicinski1b0f8032018-01-24 12:54:21 -08001432 __fl_delete(tp, f, extack);
Paul Blakey05cd2712018-04-30 14:28:30 +03001433 *last = list_empty(&head->masks);
Jiri Pirko77b99002015-05-12 14:56:21 +02001434 return 0;
1435}
1436
1437static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1438{
1439 struct cls_fl_head *head = rtnl_dereference(tp->root);
1440 struct cls_fl_filter *f;
1441
Vlad Buslov01683a12018-07-09 13:29:11 +03001442 arg->count = arg->skip;
1443
1444 while ((f = idr_get_next_ul(&head->handle_idr,
1445 &arg->cookie)) != NULL) {
1446 if (arg->fn(tp, f, arg) < 0) {
1447 arg->stop = 1;
1448 break;
Paul Blakey05cd2712018-04-30 14:28:30 +03001449 }
Vlad Buslov01683a12018-07-09 13:29:11 +03001450 arg->cookie = f->handle + 1;
1451 arg->count++;
Jiri Pirko77b99002015-05-12 14:56:21 +02001452 }
1453}
1454
John Hurley31533cb2018-06-25 14:30:06 -07001455static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1456 void *cb_priv, struct netlink_ext_ack *extack)
1457{
1458 struct cls_fl_head *head = rtnl_dereference(tp->root);
1459 struct tc_cls_flower_offload cls_flower = {};
1460 struct tcf_block *block = tp->chain->block;
1461 struct fl_flow_mask *mask;
1462 struct cls_fl_filter *f;
1463 int err;
1464
1465 list_for_each_entry(mask, &head->masks, list) {
1466 list_for_each_entry(f, &mask->filters, list) {
1467 if (tc_skip_hw(f->flags))
1468 continue;
1469
1470 tc_cls_common_offload_init(&cls_flower.common, tp,
1471 f->flags, extack);
1472 cls_flower.command = add ?
1473 TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY;
1474 cls_flower.cookie = (unsigned long)f;
1475 cls_flower.dissector = &mask->dissector;
Vlad Buslov9ca61632018-08-06 11:27:10 +03001476 cls_flower.mask = &mask->key;
1477 cls_flower.key = &f->mkey;
John Hurley31533cb2018-06-25 14:30:06 -07001478 cls_flower.exts = &f->exts;
1479 cls_flower.classid = f->res.classid;
1480
1481 err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
1482 if (err) {
1483 if (add && tc_skip_sw(f->flags))
1484 return err;
1485 continue;
1486 }
1487
1488 tc_cls_offload_cnt_update(block, &f->in_hw_count,
1489 &f->flags, add);
1490 }
1491 }
1492
1493 return 0;
1494}
1495
Jiri Pirko34738452018-07-23 09:23:11 +02001496static void fl_hw_create_tmplt(struct tcf_chain *chain,
1497 struct fl_flow_tmplt *tmplt)
1498{
1499 struct tc_cls_flower_offload cls_flower = {};
1500 struct tcf_block *block = chain->block;
1501 struct tcf_exts dummy_exts = { 0, };
1502
1503 cls_flower.common.chain_index = chain->index;
1504 cls_flower.command = TC_CLSFLOWER_TMPLT_CREATE;
1505 cls_flower.cookie = (unsigned long) tmplt;
1506 cls_flower.dissector = &tmplt->dissector;
1507 cls_flower.mask = &tmplt->mask;
1508 cls_flower.key = &tmplt->dummy_key;
1509 cls_flower.exts = &dummy_exts;
1510
1511 /* We don't care if driver (any of them) fails to handle this
1512 * call. It serves just as a hint for it.
1513 */
Cong Wangaeb3fec2018-12-11 11:15:46 -08001514 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
Jiri Pirko34738452018-07-23 09:23:11 +02001515}
1516
1517static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
1518 struct fl_flow_tmplt *tmplt)
1519{
1520 struct tc_cls_flower_offload cls_flower = {};
1521 struct tcf_block *block = chain->block;
1522
1523 cls_flower.common.chain_index = chain->index;
1524 cls_flower.command = TC_CLSFLOWER_TMPLT_DESTROY;
1525 cls_flower.cookie = (unsigned long) tmplt;
1526
Cong Wangaeb3fec2018-12-11 11:15:46 -08001527 tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false);
Jiri Pirko34738452018-07-23 09:23:11 +02001528}
1529
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001530static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
1531 struct nlattr **tca,
1532 struct netlink_ext_ack *extack)
1533{
1534 struct fl_flow_tmplt *tmplt;
1535 struct nlattr **tb;
1536 int err;
1537
1538 if (!tca[TCA_OPTIONS])
1539 return ERR_PTR(-EINVAL);
1540
1541 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
1542 if (!tb)
1543 return ERR_PTR(-ENOBUFS);
1544 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
1545 fl_policy, NULL);
1546 if (err)
1547 goto errout_tb;
1548
1549 tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
Dan Carpenter1cbc36a52018-08-03 22:27:55 +03001550 if (!tmplt) {
1551 err = -ENOMEM;
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001552 goto errout_tb;
Dan Carpenter1cbc36a52018-08-03 22:27:55 +03001553 }
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001554 tmplt->chain = chain;
1555 err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
1556 if (err)
1557 goto errout_tmplt;
1558 kfree(tb);
1559
1560 fl_init_dissector(&tmplt->dissector, &tmplt->mask);
1561
Jiri Pirko34738452018-07-23 09:23:11 +02001562 fl_hw_create_tmplt(chain, tmplt);
1563
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001564 return tmplt;
1565
1566errout_tmplt:
1567 kfree(tmplt);
1568errout_tb:
1569 kfree(tb);
1570 return ERR_PTR(err);
1571}
1572
1573static void fl_tmplt_destroy(void *tmplt_priv)
1574{
1575 struct fl_flow_tmplt *tmplt = tmplt_priv;
1576
Cong Wang95278dd2018-10-02 12:50:19 -07001577 fl_hw_destroy_tmplt(tmplt->chain, tmplt);
1578 kfree(tmplt);
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02001579}
1580
Jiri Pirko77b99002015-05-12 14:56:21 +02001581static int fl_dump_key_val(struct sk_buff *skb,
1582 void *val, int val_type,
1583 void *mask, int mask_type, int len)
1584{
1585 int err;
1586
1587 if (!memchr_inv(mask, 0, len))
1588 return 0;
1589 err = nla_put(skb, val_type, len, val);
1590 if (err)
1591 return err;
1592 if (mask_type != TCA_FLOWER_UNSPEC) {
1593 err = nla_put(skb, mask_type, len, mask);
1594 if (err)
1595 return err;
1596 }
1597 return 0;
1598}
1599
Amritha Nambiar5c722992018-11-12 16:15:55 -08001600static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key,
1601 struct fl_flow_key *mask)
1602{
1603 if (fl_dump_key_val(skb, &key->tp_min.dst, TCA_FLOWER_KEY_PORT_DST_MIN,
1604 &mask->tp_min.dst, TCA_FLOWER_UNSPEC,
1605 sizeof(key->tp_min.dst)) ||
1606 fl_dump_key_val(skb, &key->tp_max.dst, TCA_FLOWER_KEY_PORT_DST_MAX,
1607 &mask->tp_max.dst, TCA_FLOWER_UNSPEC,
1608 sizeof(key->tp_max.dst)) ||
1609 fl_dump_key_val(skb, &key->tp_min.src, TCA_FLOWER_KEY_PORT_SRC_MIN,
1610 &mask->tp_min.src, TCA_FLOWER_UNSPEC,
1611 sizeof(key->tp_min.src)) ||
1612 fl_dump_key_val(skb, &key->tp_max.src, TCA_FLOWER_KEY_PORT_SRC_MAX,
1613 &mask->tp_max.src, TCA_FLOWER_UNSPEC,
1614 sizeof(key->tp_max.src)))
1615 return -1;
1616
1617 return 0;
1618}
1619
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001620static int fl_dump_key_mpls(struct sk_buff *skb,
1621 struct flow_dissector_key_mpls *mpls_key,
1622 struct flow_dissector_key_mpls *mpls_mask)
1623{
1624 int err;
1625
1626 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1627 return 0;
1628 if (mpls_mask->mpls_ttl) {
1629 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1630 mpls_key->mpls_ttl);
1631 if (err)
1632 return err;
1633 }
1634 if (mpls_mask->mpls_tc) {
1635 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1636 mpls_key->mpls_tc);
1637 if (err)
1638 return err;
1639 }
1640 if (mpls_mask->mpls_label) {
1641 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1642 mpls_key->mpls_label);
1643 if (err)
1644 return err;
1645 }
1646 if (mpls_mask->mpls_bos) {
1647 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1648 mpls_key->mpls_bos);
1649 if (err)
1650 return err;
1651 }
1652 return 0;
1653}
1654
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001655static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001656 struct flow_dissector_key_ip *key,
1657 struct flow_dissector_key_ip *mask)
1658{
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001659 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1660 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1661 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1662 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
1663
1664 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
1665 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001666 return -1;
1667
1668 return 0;
1669}
1670
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001671static int fl_dump_key_vlan(struct sk_buff *skb,
Jianbo Liud64efd02018-07-06 05:38:16 +00001672 int vlan_id_key, int vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001673 struct flow_dissector_key_vlan *vlan_key,
1674 struct flow_dissector_key_vlan *vlan_mask)
1675{
1676 int err;
1677
1678 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1679 return 0;
1680 if (vlan_mask->vlan_id) {
Jianbo Liud64efd02018-07-06 05:38:16 +00001681 err = nla_put_u16(skb, vlan_id_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001682 vlan_key->vlan_id);
1683 if (err)
1684 return err;
1685 }
1686 if (vlan_mask->vlan_priority) {
Jianbo Liud64efd02018-07-06 05:38:16 +00001687 err = nla_put_u8(skb, vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001688 vlan_key->vlan_priority);
1689 if (err)
1690 return err;
1691 }
1692 return 0;
1693}
1694
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001695static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1696 u32 *flower_key, u32 *flower_mask,
1697 u32 flower_flag_bit, u32 dissector_flag_bit)
1698{
1699 if (dissector_mask & dissector_flag_bit) {
1700 *flower_mask |= flower_flag_bit;
1701 if (dissector_key & dissector_flag_bit)
1702 *flower_key |= flower_flag_bit;
1703 }
1704}
1705
1706static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1707{
1708 u32 key, mask;
1709 __be32 _key, _mask;
1710 int err;
1711
1712 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1713 return 0;
1714
1715 key = 0;
1716 mask = 0;
1717
1718 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1719 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +01001720 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1721 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1722 FLOW_DIS_FIRST_FRAG);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001723
1724 _key = cpu_to_be32(key);
1725 _mask = cpu_to_be32(mask);
1726
1727 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1728 if (err)
1729 return err;
1730
1731 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1732}
1733
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001734static int fl_dump_key_geneve_opt(struct sk_buff *skb,
1735 struct flow_dissector_key_enc_opts *enc_opts)
1736{
1737 struct geneve_opt *opt;
1738 struct nlattr *nest;
1739 int opt_off = 0;
1740
1741 nest = nla_nest_start(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
1742 if (!nest)
1743 goto nla_put_failure;
1744
1745 while (enc_opts->len > opt_off) {
1746 opt = (struct geneve_opt *)&enc_opts->data[opt_off];
1747
1748 if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
1749 opt->opt_class))
1750 goto nla_put_failure;
1751 if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
1752 opt->type))
1753 goto nla_put_failure;
1754 if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
1755 opt->length * 4, opt->opt_data))
1756 goto nla_put_failure;
1757
1758 opt_off += sizeof(struct geneve_opt) + opt->length * 4;
1759 }
1760 nla_nest_end(skb, nest);
1761 return 0;
1762
1763nla_put_failure:
1764 nla_nest_cancel(skb, nest);
1765 return -EMSGSIZE;
1766}
1767
1768static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
1769 struct flow_dissector_key_enc_opts *enc_opts)
1770{
1771 struct nlattr *nest;
1772 int err;
1773
1774 if (!enc_opts->len)
1775 return 0;
1776
1777 nest = nla_nest_start(skb, enc_opt_type);
1778 if (!nest)
1779 goto nla_put_failure;
1780
1781 switch (enc_opts->dst_opt_type) {
1782 case TUNNEL_GENEVE_OPT:
1783 err = fl_dump_key_geneve_opt(skb, enc_opts);
1784 if (err)
1785 goto nla_put_failure;
1786 break;
1787 default:
1788 goto nla_put_failure;
1789 }
1790 nla_nest_end(skb, nest);
1791 return 0;
1792
1793nla_put_failure:
1794 nla_nest_cancel(skb, nest);
1795 return -EMSGSIZE;
1796}
1797
1798static int fl_dump_key_enc_opt(struct sk_buff *skb,
1799 struct flow_dissector_key_enc_opts *key_opts,
1800 struct flow_dissector_key_enc_opts *msk_opts)
1801{
1802 int err;
1803
1804 err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
1805 if (err)
1806 return err;
1807
1808 return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
1809}
1810
Jiri Pirkof5749082018-07-23 09:23:08 +02001811static int fl_dump_key(struct sk_buff *skb, struct net *net,
1812 struct fl_flow_key *key, struct fl_flow_key *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +02001813{
Jiri Pirko77b99002015-05-12 14:56:21 +02001814 if (mask->indev_ifindex) {
1815 struct net_device *dev;
1816
1817 dev = __dev_get_by_index(net, key->indev_ifindex);
1818 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1819 goto nla_put_failure;
1820 }
1821
1822 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1823 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1824 sizeof(key->eth.dst)) ||
1825 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1826 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1827 sizeof(key->eth.src)) ||
1828 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1829 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1830 sizeof(key->basic.n_proto)))
1831 goto nla_put_failure;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001832
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001833 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1834 goto nla_put_failure;
1835
Jianbo Liud64efd02018-07-06 05:38:16 +00001836 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
1837 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001838 goto nla_put_failure;
1839
Jianbo Liud64efd02018-07-06 05:38:16 +00001840 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
1841 TCA_FLOWER_KEY_CVLAN_PRIO,
1842 &key->cvlan, &mask->cvlan) ||
1843 (mask->cvlan.vlan_tpid &&
Jianbo Liu158abbf2018-07-25 02:31:25 +00001844 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1845 key->cvlan.vlan_tpid)))
Jianbo Liud3069512018-07-06 05:38:15 +00001846 goto nla_put_failure;
1847
Jianbo Liu5e9a0fe2018-07-09 02:26:20 +00001848 if (mask->basic.n_proto) {
1849 if (mask->cvlan.vlan_tpid) {
1850 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1851 key->basic.n_proto))
1852 goto nla_put_failure;
1853 } else if (mask->vlan.vlan_tpid) {
1854 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1855 key->basic.n_proto))
1856 goto nla_put_failure;
1857 }
Jianbo Liud64efd02018-07-06 05:38:16 +00001858 }
1859
Jiri Pirko77b99002015-05-12 14:56:21 +02001860 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1861 key->basic.n_proto == htons(ETH_P_IPV6)) &&
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001862 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
Jiri Pirko77b99002015-05-12 14:56:21 +02001863 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001864 sizeof(key->basic.ip_proto)) ||
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001865 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
Jiri Pirko77b99002015-05-12 14:56:21 +02001866 goto nla_put_failure;
1867
Tom Herbertc3f83242015-06-04 09:16:40 -07001868 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001869 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1870 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1871 sizeof(key->ipv4.src)) ||
1872 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1873 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1874 sizeof(key->ipv4.dst))))
1875 goto nla_put_failure;
Tom Herbertc3f83242015-06-04 09:16:40 -07001876 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001877 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1878 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1879 sizeof(key->ipv6.src)) ||
1880 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1881 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1882 sizeof(key->ipv6.dst))))
1883 goto nla_put_failure;
1884
1885 if (key->basic.ip_proto == IPPROTO_TCP &&
1886 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001887 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001888 sizeof(key->tp.src)) ||
1889 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001890 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001891 sizeof(key->tp.dst)) ||
1892 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1893 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1894 sizeof(key->tcp.flags))))
Jiri Pirko77b99002015-05-12 14:56:21 +02001895 goto nla_put_failure;
1896 else if (key->basic.ip_proto == IPPROTO_UDP &&
1897 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001898 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001899 sizeof(key->tp.src)) ||
1900 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001901 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001902 sizeof(key->tp.dst))))
1903 goto nla_put_failure;
Simon Horman5976c5f2016-11-03 13:24:21 +01001904 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1905 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1906 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1907 sizeof(key->tp.src)) ||
1908 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1909 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1910 sizeof(key->tp.dst))))
1911 goto nla_put_failure;
Simon Horman7b684882016-12-07 13:48:28 +01001912 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1913 key->basic.ip_proto == IPPROTO_ICMP &&
1914 (fl_dump_key_val(skb, &key->icmp.type,
1915 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1916 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1917 sizeof(key->icmp.type)) ||
1918 fl_dump_key_val(skb, &key->icmp.code,
1919 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1920 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1921 sizeof(key->icmp.code))))
1922 goto nla_put_failure;
1923 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1924 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1925 (fl_dump_key_val(skb, &key->icmp.type,
1926 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1927 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1928 sizeof(key->icmp.type)) ||
1929 fl_dump_key_val(skb, &key->icmp.code,
1930 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1931 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1932 sizeof(key->icmp.code))))
1933 goto nla_put_failure;
Simon Horman99d31322017-01-11 14:05:43 +01001934 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1935 key->basic.n_proto == htons(ETH_P_RARP)) &&
1936 (fl_dump_key_val(skb, &key->arp.sip,
1937 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1938 TCA_FLOWER_KEY_ARP_SIP_MASK,
1939 sizeof(key->arp.sip)) ||
1940 fl_dump_key_val(skb, &key->arp.tip,
1941 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1942 TCA_FLOWER_KEY_ARP_TIP_MASK,
1943 sizeof(key->arp.tip)) ||
1944 fl_dump_key_val(skb, &key->arp.op,
1945 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1946 TCA_FLOWER_KEY_ARP_OP_MASK,
1947 sizeof(key->arp.op)) ||
1948 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1949 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1950 sizeof(key->arp.sha)) ||
1951 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1952 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1953 sizeof(key->arp.tha))))
1954 goto nla_put_failure;
Jiri Pirko77b99002015-05-12 14:56:21 +02001955
Amritha Nambiar5c722992018-11-12 16:15:55 -08001956 if ((key->basic.ip_proto == IPPROTO_TCP ||
1957 key->basic.ip_proto == IPPROTO_UDP ||
1958 key->basic.ip_proto == IPPROTO_SCTP) &&
1959 fl_dump_key_port_range(skb, key, mask))
1960 goto nla_put_failure;
1961
Amir Vadaibc3103f2016-09-08 16:23:47 +03001962 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1963 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1964 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1965 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1966 sizeof(key->enc_ipv4.src)) ||
1967 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1968 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1969 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1970 sizeof(key->enc_ipv4.dst))))
1971 goto nla_put_failure;
1972 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1973 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1974 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1975 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1976 sizeof(key->enc_ipv6.src)) ||
1977 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1978 TCA_FLOWER_KEY_ENC_IPV6_DST,
1979 &mask->enc_ipv6.dst,
1980 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1981 sizeof(key->enc_ipv6.dst))))
1982 goto nla_put_failure;
1983
1984 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001985 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001986 sizeof(key->enc_key_id)) ||
1987 fl_dump_key_val(skb, &key->enc_tp.src,
1988 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1989 &mask->enc_tp.src,
1990 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1991 sizeof(key->enc_tp.src)) ||
1992 fl_dump_key_val(skb, &key->enc_tp.dst,
1993 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1994 &mask->enc_tp.dst,
1995 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001996 sizeof(key->enc_tp.dst)) ||
Pieter Jansen van Vuuren0a6e7772018-08-07 17:36:01 +02001997 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
1998 fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
Amir Vadaibc3103f2016-09-08 16:23:47 +03001999 goto nla_put_failure;
2000
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02002001 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
2002 goto nla_put_failure;
2003
Jiri Pirkof5749082018-07-23 09:23:08 +02002004 return 0;
2005
2006nla_put_failure:
2007 return -EMSGSIZE;
2008}
2009
2010static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
2011 struct sk_buff *skb, struct tcmsg *t)
2012{
2013 struct cls_fl_filter *f = fh;
2014 struct nlattr *nest;
2015 struct fl_flow_key *key, *mask;
2016
2017 if (!f)
2018 return skb->len;
2019
2020 t->tcm_handle = f->handle;
2021
2022 nest = nla_nest_start(skb, TCA_OPTIONS);
2023 if (!nest)
2024 goto nla_put_failure;
2025
2026 if (f->res.classid &&
2027 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
2028 goto nla_put_failure;
2029
2030 key = &f->key;
2031 mask = &f->mask->key;
2032
2033 if (fl_dump_key(skb, net, key, mask))
2034 goto nla_put_failure;
2035
2036 if (!tc_skip_hw(f->flags))
2037 fl_hw_update_stats(tp, f);
2038
Or Gerlitz749e6722017-02-16 10:31:10 +02002039 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
2040 goto nla_put_failure;
Amir Vadaie69985c2016-06-05 17:11:18 +03002041
Vlad Buslov86c55362018-09-07 17:22:21 +03002042 if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count))
2043 goto nla_put_failure;
2044
Jiri Pirko77b99002015-05-12 14:56:21 +02002045 if (tcf_exts_dump(skb, &f->exts))
2046 goto nla_put_failure;
2047
2048 nla_nest_end(skb, nest);
2049
2050 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
2051 goto nla_put_failure;
2052
2053 return skb->len;
2054
2055nla_put_failure:
2056 nla_nest_cancel(skb, nest);
2057 return -1;
2058}
2059
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02002060static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
2061{
2062 struct fl_flow_tmplt *tmplt = tmplt_priv;
2063 struct fl_flow_key *key, *mask;
2064 struct nlattr *nest;
2065
2066 nest = nla_nest_start(skb, TCA_OPTIONS);
2067 if (!nest)
2068 goto nla_put_failure;
2069
2070 key = &tmplt->dummy_key;
2071 mask = &tmplt->mask;
2072
2073 if (fl_dump_key(skb, net, key, mask))
2074 goto nla_put_failure;
2075
2076 nla_nest_end(skb, nest);
2077
2078 return skb->len;
2079
2080nla_put_failure:
2081 nla_nest_cancel(skb, nest);
2082 return -EMSGSIZE;
2083}
2084
Cong Wang07d79fc2017-08-30 14:30:36 -07002085static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
2086{
2087 struct cls_fl_filter *f = fh;
2088
2089 if (f && f->res.classid == classid)
2090 f->res.class = cl;
2091}
2092
Jiri Pirko77b99002015-05-12 14:56:21 +02002093static struct tcf_proto_ops cls_fl_ops __read_mostly = {
2094 .kind = "flower",
2095 .classify = fl_classify,
2096 .init = fl_init,
2097 .destroy = fl_destroy,
2098 .get = fl_get,
2099 .change = fl_change,
2100 .delete = fl_delete,
2101 .walk = fl_walk,
John Hurley31533cb2018-06-25 14:30:06 -07002102 .reoffload = fl_reoffload,
Jiri Pirko77b99002015-05-12 14:56:21 +02002103 .dump = fl_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07002104 .bind_class = fl_bind_class,
Jiri Pirkob95ec7e2018-07-23 09:23:10 +02002105 .tmplt_create = fl_tmplt_create,
2106 .tmplt_destroy = fl_tmplt_destroy,
2107 .tmplt_dump = fl_tmplt_dump,
Jiri Pirko77b99002015-05-12 14:56:21 +02002108 .owner = THIS_MODULE,
2109};
2110
2111static int __init cls_fl_init(void)
2112{
2113 return register_tcf_proto_ops(&cls_fl_ops);
2114}
2115
2116static void __exit cls_fl_exit(void)
2117{
2118 unregister_tcf_proto_ops(&cls_fl_ops);
2119}
2120
2121module_init(cls_fl_init);
2122module_exit(cls_fl_exit);
2123
2124MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
2125MODULE_DESCRIPTION("Flower classifier");
2126MODULE_LICENSE("GPL v2");