blob: e93b13d2cb81de277a5e4fa6d7653457e448471b [file] [log] [blame]
Jiri Pirko77b99002015-05-12 14:56:21 +02001/*
2 * net/sched/cls_flower.c Flower classifier
3 *
4 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/rhashtable.h>
Daniel Borkmannd9363772016-11-27 01:18:01 +010016#include <linux/workqueue.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020017
18#include <linux/if_ether.h>
19#include <linux/in6.h>
20#include <linux/ip.h>
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -040021#include <linux/mpls.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020022
23#include <net/sch_generic.h>
24#include <net/pkt_cls.h>
25#include <net/ip.h>
26#include <net/flow_dissector.h>
27
Amir Vadaibc3103f2016-09-08 16:23:47 +030028#include <net/dst.h>
29#include <net/dst_metadata.h>
30
Jiri Pirko77b99002015-05-12 14:56:21 +020031struct fl_flow_key {
32 int indev_ifindex;
Tom Herbert42aecaa2015-06-04 09:16:39 -070033 struct flow_dissector_key_control control;
Amir Vadaibc3103f2016-09-08 16:23:47 +030034 struct flow_dissector_key_control enc_control;
Jiri Pirko77b99002015-05-12 14:56:21 +020035 struct flow_dissector_key_basic basic;
36 struct flow_dissector_key_eth_addrs eth;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +030037 struct flow_dissector_key_vlan vlan;
Jiri Pirko77b99002015-05-12 14:56:21 +020038 union {
Tom Herbertc3f83242015-06-04 09:16:40 -070039 struct flow_dissector_key_ipv4_addrs ipv4;
Jiri Pirko77b99002015-05-12 14:56:21 +020040 struct flow_dissector_key_ipv6_addrs ipv6;
41 };
42 struct flow_dissector_key_ports tp;
Simon Horman7b684882016-12-07 13:48:28 +010043 struct flow_dissector_key_icmp icmp;
Simon Horman99d31322017-01-11 14:05:43 +010044 struct flow_dissector_key_arp arp;
Amir Vadaibc3103f2016-09-08 16:23:47 +030045 struct flow_dissector_key_keyid enc_key_id;
46 union {
47 struct flow_dissector_key_ipv4_addrs enc_ipv4;
48 struct flow_dissector_key_ipv6_addrs enc_ipv6;
49 };
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +020050 struct flow_dissector_key_ports enc_tp;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -040051 struct flow_dissector_key_mpls mpls;
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +020052 struct flow_dissector_key_tcp tcp;
Or Gerlitz4d80cc02017-06-01 21:37:38 +030053 struct flow_dissector_key_ip ip;
Jiri Pirko77b99002015-05-12 14:56:21 +020054} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
55
56struct fl_flow_mask_range {
57 unsigned short int start;
58 unsigned short int end;
59};
60
61struct fl_flow_mask {
62 struct fl_flow_key key;
63 struct fl_flow_mask_range range;
Paul Blakey05cd2712018-04-30 14:28:30 +030064 struct rhash_head ht_node;
65 struct rhashtable ht;
66 struct rhashtable_params filter_ht_params;
67 struct flow_dissector dissector;
68 struct list_head filters;
Paolo Abeni44a5cd42018-06-21 20:02:16 +020069 struct rcu_work rwork;
Paul Blakey05cd2712018-04-30 14:28:30 +030070 struct list_head list;
Jiri Pirko77b99002015-05-12 14:56:21 +020071};
72
73struct cls_fl_head {
74 struct rhashtable ht;
Paul Blakey05cd2712018-04-30 14:28:30 +030075 struct list_head masks;
Cong Wangaaa908f2018-05-23 15:26:53 -070076 struct rcu_work rwork;
Chris Mic15ab232017-08-30 02:31:58 -040077 struct idr handle_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +020078};
79
80struct cls_fl_filter {
Paul Blakey05cd2712018-04-30 14:28:30 +030081 struct fl_flow_mask *mask;
Jiri Pirko77b99002015-05-12 14:56:21 +020082 struct rhash_head ht_node;
83 struct fl_flow_key mkey;
84 struct tcf_exts exts;
85 struct tcf_result res;
86 struct fl_flow_key key;
87 struct list_head list;
88 u32 handle;
Amir Vadaie69985c2016-06-05 17:11:18 +030089 u32 flags;
John Hurley31533cb2018-06-25 14:30:06 -070090 unsigned int in_hw_count;
Cong Wangaaa908f2018-05-23 15:26:53 -070091 struct rcu_work rwork;
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +020092 struct net_device *hw_dev;
Jiri Pirko77b99002015-05-12 14:56:21 +020093};
94
Paul Blakey05cd2712018-04-30 14:28:30 +030095static const struct rhashtable_params mask_ht_params = {
96 .key_offset = offsetof(struct fl_flow_mask, key),
97 .key_len = sizeof(struct fl_flow_key),
98 .head_offset = offsetof(struct fl_flow_mask, ht_node),
99 .automatic_shrinking = true,
100};
101
Jiri Pirko77b99002015-05-12 14:56:21 +0200102static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
103{
104 return mask->range.end - mask->range.start;
105}
106
107static void fl_mask_update_range(struct fl_flow_mask *mask)
108{
109 const u8 *bytes = (const u8 *) &mask->key;
110 size_t size = sizeof(mask->key);
Paul Blakey05cd2712018-04-30 14:28:30 +0300111 size_t i, first = 0, last;
Jiri Pirko77b99002015-05-12 14:56:21 +0200112
Paul Blakey05cd2712018-04-30 14:28:30 +0300113 for (i = 0; i < size; i++) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200114 if (bytes[i]) {
Paul Blakey05cd2712018-04-30 14:28:30 +0300115 first = i;
116 break;
117 }
118 }
119 last = first;
120 for (i = size - 1; i != first; i--) {
121 if (bytes[i]) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200122 last = i;
Paul Blakey05cd2712018-04-30 14:28:30 +0300123 break;
Jiri Pirko77b99002015-05-12 14:56:21 +0200124 }
125 }
126 mask->range.start = rounddown(first, sizeof(long));
127 mask->range.end = roundup(last + 1, sizeof(long));
128}
129
130static void *fl_key_get_start(struct fl_flow_key *key,
131 const struct fl_flow_mask *mask)
132{
133 return (u8 *) key + mask->range.start;
134}
135
136static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
137 struct fl_flow_mask *mask)
138{
139 const long *lkey = fl_key_get_start(key, mask);
140 const long *lmask = fl_key_get_start(&mask->key, mask);
141 long *lmkey = fl_key_get_start(mkey, mask);
142 int i;
143
144 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
145 *lmkey++ = *lkey++ & *lmask++;
146}
147
148static void fl_clear_masked_range(struct fl_flow_key *key,
149 struct fl_flow_mask *mask)
150{
151 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
152}
153
Paul Blakey05cd2712018-04-30 14:28:30 +0300154static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
Paul Blakeya3308d82017-01-16 10:45:13 +0200155 struct fl_flow_key *mkey)
156{
Paul Blakey05cd2712018-04-30 14:28:30 +0300157 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
158 mask->filter_ht_params);
Paul Blakeya3308d82017-01-16 10:45:13 +0200159}
160
Jiri Pirko77b99002015-05-12 14:56:21 +0200161static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
162 struct tcf_result *res)
163{
164 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
165 struct cls_fl_filter *f;
Paul Blakey05cd2712018-04-30 14:28:30 +0300166 struct fl_flow_mask *mask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200167 struct fl_flow_key skb_key;
168 struct fl_flow_key skb_mkey;
169
Paul Blakey05cd2712018-04-30 14:28:30 +0300170 list_for_each_entry_rcu(mask, &head->masks, list) {
171 fl_clear_masked_range(&skb_key, mask);
Amir Vadaie69985c2016-06-05 17:11:18 +0300172
Paul Blakey05cd2712018-04-30 14:28:30 +0300173 skb_key.indev_ifindex = skb->skb_iif;
174 /* skb_flow_dissect() does not set n_proto in case an unknown
175 * protocol, so do it rather here.
176 */
177 skb_key.basic.n_proto = skb->protocol;
178 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
179 skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
Amir Vadaibc3103f2016-09-08 16:23:47 +0300180
Paul Blakey05cd2712018-04-30 14:28:30 +0300181 fl_set_masked_key(&skb_mkey, &skb_key, mask);
Jiri Pirko77b99002015-05-12 14:56:21 +0200182
Paul Blakey05cd2712018-04-30 14:28:30 +0300183 f = fl_lookup(mask, &skb_mkey);
184 if (f && !tc_skip_sw(f->flags)) {
185 *res = f->res;
186 return tcf_exts_exec(skb, &f->exts, res);
187 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200188 }
189 return -1;
190}
191
192static int fl_init(struct tcf_proto *tp)
193{
194 struct cls_fl_head *head;
195
196 head = kzalloc(sizeof(*head), GFP_KERNEL);
197 if (!head)
198 return -ENOBUFS;
199
Paul Blakey05cd2712018-04-30 14:28:30 +0300200 INIT_LIST_HEAD_RCU(&head->masks);
Jiri Pirko77b99002015-05-12 14:56:21 +0200201 rcu_assign_pointer(tp->root, head);
Chris Mic15ab232017-08-30 02:31:58 -0400202 idr_init(&head->handle_idr);
Jiri Pirko77b99002015-05-12 14:56:21 +0200203
Paul Blakey05cd2712018-04-30 14:28:30 +0300204 return rhashtable_init(&head->ht, &mask_ht_params);
205}
206
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200207static void fl_mask_free(struct fl_flow_mask *mask)
208{
209 rhashtable_destroy(&mask->ht);
210 kfree(mask);
211}
212
213static void fl_mask_free_work(struct work_struct *work)
214{
215 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
216 struct fl_flow_mask, rwork);
217
218 fl_mask_free(mask);
219}
220
Paul Blakey05cd2712018-04-30 14:28:30 +0300221static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask,
222 bool async)
223{
224 if (!list_empty(&mask->filters))
225 return false;
226
227 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
Paul Blakey05cd2712018-04-30 14:28:30 +0300228 list_del_rcu(&mask->list);
229 if (async)
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200230 tcf_queue_work(&mask->rwork, fl_mask_free_work);
Paul Blakey05cd2712018-04-30 14:28:30 +0300231 else
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200232 fl_mask_free(mask);
Paul Blakey05cd2712018-04-30 14:28:30 +0300233
234 return true;
Jiri Pirko77b99002015-05-12 14:56:21 +0200235}
236
Cong Wang0dadc112017-11-06 13:47:24 -0800237static void __fl_destroy_filter(struct cls_fl_filter *f)
238{
239 tcf_exts_destroy(&f->exts);
240 tcf_exts_put_net(&f->exts);
241 kfree(f);
242}
243
Cong Wang0552c8a2017-10-26 18:24:33 -0700244static void fl_destroy_filter_work(struct work_struct *work)
245{
Cong Wangaaa908f2018-05-23 15:26:53 -0700246 struct cls_fl_filter *f = container_of(to_rcu_work(work),
247 struct cls_fl_filter, rwork);
Cong Wang0552c8a2017-10-26 18:24:33 -0700248
249 rtnl_lock();
Cong Wang0dadc112017-11-06 13:47:24 -0800250 __fl_destroy_filter(f);
Cong Wang0552c8a2017-10-26 18:24:33 -0700251 rtnl_unlock();
252}
253
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800254static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
255 struct netlink_ext_ack *extack)
Amir Vadai5b33f482016-03-08 12:42:29 +0200256{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200257 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200258 struct tcf_block *block = tp->chain->block;
Amir Vadai5b33f482016-03-08 12:42:29 +0200259
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800260 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200261 cls_flower.command = TC_CLSFLOWER_DESTROY;
262 cls_flower.cookie = (unsigned long) f;
Amir Vadai5b33f482016-03-08 12:42:29 +0200263
Jiri Pirko208c0f42017-10-19 15:50:32 +0200264 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200265 &cls_flower, false);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100266 tcf_block_offload_dec(block, &f->flags);
Amir Vadai5b33f482016-03-08 12:42:29 +0200267}
268
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300269static int fl_hw_replace_filter(struct tcf_proto *tp,
Quentin Monnet41002032018-01-19 17:44:43 -0800270 struct cls_fl_filter *f,
271 struct netlink_ext_ack *extack)
Amir Vadai5b33f482016-03-08 12:42:29 +0200272{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200273 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200274 struct tcf_block *block = tp->chain->block;
Jiri Pirko717503b2017-10-11 09:41:09 +0200275 bool skip_sw = tc_skip_sw(f->flags);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300276 int err;
Amir Vadai5b33f482016-03-08 12:42:29 +0200277
Jakub Kicinskiea205942018-01-24 12:54:20 -0800278 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200279 cls_flower.command = TC_CLSFLOWER_REPLACE;
280 cls_flower.cookie = (unsigned long) f;
Paul Blakey05cd2712018-04-30 14:28:30 +0300281 cls_flower.dissector = &f->mask->dissector;
282 cls_flower.mask = &f->mask->key;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200283 cls_flower.key = &f->mkey;
284 cls_flower.exts = &f->exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700285 cls_flower.classid = f->res.classid;
Amir Vadai5b33f482016-03-08 12:42:29 +0200286
Jiri Pirko208c0f42017-10-19 15:50:32 +0200287 err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200288 &cls_flower, skip_sw);
289 if (err < 0) {
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800290 fl_hw_destroy_filter(tp, f, NULL);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300291 return err;
Jiri Pirko717503b2017-10-11 09:41:09 +0200292 } else if (err > 0) {
John Hurley31533cb2018-06-25 14:30:06 -0700293 f->in_hw_count = err;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100294 tcf_block_offload_inc(block, &f->flags);
Jiri Pirko717503b2017-10-11 09:41:09 +0200295 }
296
297 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
298 return -EINVAL;
299
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300300 return 0;
Amir Vadai5b33f482016-03-08 12:42:29 +0200301}
302
Amir Vadai10cbc682016-05-13 12:55:37 +0000303static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
304{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200305 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200306 struct tcf_block *block = tp->chain->block;
Amir Vadai10cbc682016-05-13 12:55:37 +0000307
Jakub Kicinskiea205942018-01-24 12:54:20 -0800308 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200309 cls_flower.command = TC_CLSFLOWER_STATS;
310 cls_flower.cookie = (unsigned long) f;
311 cls_flower.exts = &f->exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700312 cls_flower.classid = f->res.classid;
Amir Vadai10cbc682016-05-13 12:55:37 +0000313
Jiri Pirko208c0f42017-10-19 15:50:32 +0200314 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200315 &cls_flower, false);
Amir Vadai10cbc682016-05-13 12:55:37 +0000316}
317
Paul Blakey05cd2712018-04-30 14:28:30 +0300318static bool __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800319 struct netlink_ext_ack *extack)
Roi Dayan13fa8762016-11-01 16:08:29 +0200320{
Chris Mic15ab232017-08-30 02:31:58 -0400321 struct cls_fl_head *head = rtnl_dereference(tp->root);
Paul Blakey05cd2712018-04-30 14:28:30 +0300322 bool async = tcf_exts_get_net(&f->exts);
323 bool last;
Chris Mic15ab232017-08-30 02:31:58 -0400324
Matthew Wilcox9c160942017-11-28 09:48:43 -0500325 idr_remove(&head->handle_idr, f->handle);
Roi Dayan13fa8762016-11-01 16:08:29 +0200326 list_del_rcu(&f->list);
Paul Blakey05cd2712018-04-30 14:28:30 +0300327 last = fl_mask_put(head, f->mask, async);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200328 if (!tc_skip_hw(f->flags))
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800329 fl_hw_destroy_filter(tp, f, extack);
Roi Dayan13fa8762016-11-01 16:08:29 +0200330 tcf_unbind_filter(tp, &f->res);
Paul Blakey05cd2712018-04-30 14:28:30 +0300331 if (async)
Cong Wangaaa908f2018-05-23 15:26:53 -0700332 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
Cong Wang0dadc112017-11-06 13:47:24 -0800333 else
334 __fl_destroy_filter(f);
Paul Blakey05cd2712018-04-30 14:28:30 +0300335
336 return last;
Roi Dayan13fa8762016-11-01 16:08:29 +0200337}
338
Daniel Borkmannd9363772016-11-27 01:18:01 +0100339static void fl_destroy_sleepable(struct work_struct *work)
340{
Cong Wangaaa908f2018-05-23 15:26:53 -0700341 struct cls_fl_head *head = container_of(to_rcu_work(work),
342 struct cls_fl_head,
343 rwork);
Paul Blakeyde9dc652018-06-03 10:06:13 +0300344
345 rhashtable_destroy(&head->ht);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100346 kfree(head);
347 module_put(THIS_MODULE);
348}
349
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800350static void fl_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200351{
352 struct cls_fl_head *head = rtnl_dereference(tp->root);
Paul Blakey05cd2712018-04-30 14:28:30 +0300353 struct fl_flow_mask *mask, *next_mask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200354 struct cls_fl_filter *f, *next;
355
Paul Blakey05cd2712018-04-30 14:28:30 +0300356 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
357 list_for_each_entry_safe(f, next, &mask->filters, list) {
358 if (__fl_delete(tp, f, extack))
359 break;
360 }
361 }
Chris Mic15ab232017-08-30 02:31:58 -0400362 idr_destroy(&head->handle_idr);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100363
364 __module_get(THIS_MODULE);
Cong Wangaaa908f2018-05-23 15:26:53 -0700365 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
Jiri Pirko77b99002015-05-12 14:56:21 +0200366}
367
WANG Cong8113c092017-08-04 21:31:43 -0700368static void *fl_get(struct tcf_proto *tp, u32 handle)
Jiri Pirko77b99002015-05-12 14:56:21 +0200369{
370 struct cls_fl_head *head = rtnl_dereference(tp->root);
Jiri Pirko77b99002015-05-12 14:56:21 +0200371
Matthew Wilcox322d8842017-11-28 10:01:24 -0500372 return idr_find(&head->handle_idr, handle);
Jiri Pirko77b99002015-05-12 14:56:21 +0200373}
374
375static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
376 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
377 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
378 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
379 .len = IFNAMSIZ },
380 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
381 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
382 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
383 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
384 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
385 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
386 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
387 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
388 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
389 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
390 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
391 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
392 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
393 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
394 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
395 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
Jamal Hadi Salimb175c3a2015-06-25 06:55:27 -0400396 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
397 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300398 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
399 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
400 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
Amir Vadaibc3103f2016-09-08 16:23:47 +0300401 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
402 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
403 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
404 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
405 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
406 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
407 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
408 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
409 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
Or Gerlitzaa72d702016-09-15 15:28:22 +0300410 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
411 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
412 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
413 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
Simon Horman5976c5f2016-11-03 13:24:21 +0100414 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
415 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
416 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
417 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200418 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
419 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
420 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
421 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200422 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
423 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
Simon Horman7b684882016-12-07 13:48:28 +0100424 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
425 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
426 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
427 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
428 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
429 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
430 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
431 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
Simon Horman99d31322017-01-11 14:05:43 +0100432 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
433 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
434 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
435 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
436 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
437 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
438 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
439 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
440 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
441 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400442 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
443 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
444 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
445 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200446 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
447 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300448 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
449 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
450 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
451 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
Jiri Pirko77b99002015-05-12 14:56:21 +0200452};
453
454static void fl_set_key_val(struct nlattr **tb,
455 void *val, int val_type,
456 void *mask, int mask_type, int len)
457{
458 if (!tb[val_type])
459 return;
460 memcpy(val, nla_data(tb[val_type]), len);
461 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
462 memset(mask, 0xff, len);
463 else
464 memcpy(mask, nla_data(tb[mask_type]), len);
465}
466
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400467static int fl_set_key_mpls(struct nlattr **tb,
468 struct flow_dissector_key_mpls *key_val,
469 struct flow_dissector_key_mpls *key_mask)
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400470{
471 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
472 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
473 key_mask->mpls_ttl = MPLS_TTL_MASK;
474 }
475 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400476 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
477
478 if (bos & ~MPLS_BOS_MASK)
479 return -EINVAL;
480 key_val->mpls_bos = bos;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400481 key_mask->mpls_bos = MPLS_BOS_MASK;
482 }
483 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400484 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
485
486 if (tc & ~MPLS_TC_MASK)
487 return -EINVAL;
488 key_val->mpls_tc = tc;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400489 key_mask->mpls_tc = MPLS_TC_MASK;
490 }
491 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400492 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
493
494 if (label & ~MPLS_LABEL_MASK)
495 return -EINVAL;
496 key_val->mpls_label = label;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400497 key_mask->mpls_label = MPLS_LABEL_MASK;
498 }
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400499 return 0;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400500}
501
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300502static void fl_set_key_vlan(struct nlattr **tb,
Jianbo Liuaaab0832018-07-06 05:38:13 +0000503 __be16 ethertype,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300504 struct flow_dissector_key_vlan *key_val,
505 struct flow_dissector_key_vlan *key_mask)
506{
507#define VLAN_PRIORITY_MASK 0x7
508
509 if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
510 key_val->vlan_id =
511 nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
512 key_mask->vlan_id = VLAN_VID_MASK;
513 }
514 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
515 key_val->vlan_priority =
516 nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
517 VLAN_PRIORITY_MASK;
518 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
519 }
Jianbo Liuaaab0832018-07-06 05:38:13 +0000520 key_val->vlan_tpid = ethertype;
521 key_mask->vlan_tpid = cpu_to_be16(~0);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300522}
523
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200524static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
525 u32 *dissector_key, u32 *dissector_mask,
526 u32 flower_flag_bit, u32 dissector_flag_bit)
527{
528 if (flower_mask & flower_flag_bit) {
529 *dissector_mask |= dissector_flag_bit;
530 if (flower_key & flower_flag_bit)
531 *dissector_key |= dissector_flag_bit;
532 }
533}
534
Or Gerlitzd9724772016-12-22 14:28:15 +0200535static int fl_set_key_flags(struct nlattr **tb,
536 u32 *flags_key, u32 *flags_mask)
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200537{
538 u32 key, mask;
539
Or Gerlitzd9724772016-12-22 14:28:15 +0200540 /* mask is mandatory for flags */
541 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
542 return -EINVAL;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200543
544 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
Or Gerlitzd9724772016-12-22 14:28:15 +0200545 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200546
547 *flags_key = 0;
548 *flags_mask = 0;
549
550 fl_set_key_flag(key, mask, flags_key, flags_mask,
551 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +0100552 fl_set_key_flag(key, mask, flags_key, flags_mask,
553 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
554 FLOW_DIS_FIRST_FRAG);
Or Gerlitzd9724772016-12-22 14:28:15 +0200555
556 return 0;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200557}
558
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300559static void fl_set_key_ip(struct nlattr **tb,
560 struct flow_dissector_key_ip *key,
561 struct flow_dissector_key_ip *mask)
562{
563 fl_set_key_val(tb, &key->tos, TCA_FLOWER_KEY_IP_TOS,
564 &mask->tos, TCA_FLOWER_KEY_IP_TOS_MASK,
565 sizeof(key->tos));
566
567 fl_set_key_val(tb, &key->ttl, TCA_FLOWER_KEY_IP_TTL,
568 &mask->ttl, TCA_FLOWER_KEY_IP_TTL_MASK,
569 sizeof(key->ttl));
570}
571
Jiri Pirko77b99002015-05-12 14:56:21 +0200572static int fl_set_key(struct net *net, struct nlattr **tb,
Alexander Aring1057c552018-01-18 11:20:54 -0500573 struct fl_flow_key *key, struct fl_flow_key *mask,
574 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200575{
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300576 __be16 ethertype;
Or Gerlitzd9724772016-12-22 14:28:15 +0200577 int ret = 0;
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400578#ifdef CONFIG_NET_CLS_IND
Jiri Pirko77b99002015-05-12 14:56:21 +0200579 if (tb[TCA_FLOWER_INDEV]) {
Alexander Aring1057c552018-01-18 11:20:54 -0500580 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200581 if (err < 0)
582 return err;
583 key->indev_ifindex = err;
584 mask->indev_ifindex = 0xffffffff;
585 }
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400586#endif
Jiri Pirko77b99002015-05-12 14:56:21 +0200587
588 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
589 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
590 sizeof(key->eth.dst));
591 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
592 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
593 sizeof(key->eth.src));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500594
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200595 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300596 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
597
Jianbo Liuaaab0832018-07-06 05:38:13 +0000598 if (eth_type_vlan(ethertype)) {
599 fl_set_key_vlan(tb, ethertype, &key->vlan, &mask->vlan);
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200600 fl_set_key_val(tb, &key->basic.n_proto,
601 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
602 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
603 sizeof(key->basic.n_proto));
604 } else {
605 key->basic.n_proto = ethertype;
606 mask->basic.n_proto = cpu_to_be16(~0);
607 }
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300608 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500609
Jiri Pirko77b99002015-05-12 14:56:21 +0200610 if (key->basic.n_proto == htons(ETH_P_IP) ||
611 key->basic.n_proto == htons(ETH_P_IPV6)) {
612 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
613 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
614 sizeof(key->basic.ip_proto));
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300615 fl_set_key_ip(tb, &key->ip, &mask->ip);
Jiri Pirko77b99002015-05-12 14:56:21 +0200616 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500617
618 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
619 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200620 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200621 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
622 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
623 sizeof(key->ipv4.src));
624 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
625 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
626 sizeof(key->ipv4.dst));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500627 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
628 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200629 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200630 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
631 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
632 sizeof(key->ipv6.src));
633 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
634 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
635 sizeof(key->ipv6.dst));
636 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500637
Jiri Pirko77b99002015-05-12 14:56:21 +0200638 if (key->basic.ip_proto == IPPROTO_TCP) {
639 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300640 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200641 sizeof(key->tp.src));
642 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300643 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200644 sizeof(key->tp.dst));
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200645 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
646 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
647 sizeof(key->tcp.flags));
Jiri Pirko77b99002015-05-12 14:56:21 +0200648 } else if (key->basic.ip_proto == IPPROTO_UDP) {
649 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300650 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200651 sizeof(key->tp.src));
652 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300653 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200654 sizeof(key->tp.dst));
Simon Horman5976c5f2016-11-03 13:24:21 +0100655 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
656 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
657 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
658 sizeof(key->tp.src));
659 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
660 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
661 sizeof(key->tp.dst));
Simon Horman7b684882016-12-07 13:48:28 +0100662 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
663 key->basic.ip_proto == IPPROTO_ICMP) {
664 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
665 &mask->icmp.type,
666 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
667 sizeof(key->icmp.type));
668 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
669 &mask->icmp.code,
670 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
671 sizeof(key->icmp.code));
672 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
673 key->basic.ip_proto == IPPROTO_ICMPV6) {
674 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
675 &mask->icmp.type,
676 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
677 sizeof(key->icmp.type));
Simon Horman040587a2017-01-30 16:19:02 +0100678 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
Simon Horman7b684882016-12-07 13:48:28 +0100679 &mask->icmp.code,
Simon Horman040587a2017-01-30 16:19:02 +0100680 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
Simon Horman7b684882016-12-07 13:48:28 +0100681 sizeof(key->icmp.code));
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400682 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
683 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400684 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
685 if (ret)
686 return ret;
Simon Horman99d31322017-01-11 14:05:43 +0100687 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
688 key->basic.n_proto == htons(ETH_P_RARP)) {
689 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
690 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
691 sizeof(key->arp.sip));
692 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
693 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
694 sizeof(key->arp.tip));
695 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
696 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
697 sizeof(key->arp.op));
698 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
699 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
700 sizeof(key->arp.sha));
701 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
702 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
703 sizeof(key->arp.tha));
Jiri Pirko77b99002015-05-12 14:56:21 +0200704 }
705
Amir Vadaibc3103f2016-09-08 16:23:47 +0300706 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
707 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
708 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200709 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300710 fl_set_key_val(tb, &key->enc_ipv4.src,
711 TCA_FLOWER_KEY_ENC_IPV4_SRC,
712 &mask->enc_ipv4.src,
713 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
714 sizeof(key->enc_ipv4.src));
715 fl_set_key_val(tb, &key->enc_ipv4.dst,
716 TCA_FLOWER_KEY_ENC_IPV4_DST,
717 &mask->enc_ipv4.dst,
718 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
719 sizeof(key->enc_ipv4.dst));
720 }
721
722 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
723 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
724 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200725 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300726 fl_set_key_val(tb, &key->enc_ipv6.src,
727 TCA_FLOWER_KEY_ENC_IPV6_SRC,
728 &mask->enc_ipv6.src,
729 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
730 sizeof(key->enc_ipv6.src));
731 fl_set_key_val(tb, &key->enc_ipv6.dst,
732 TCA_FLOWER_KEY_ENC_IPV6_DST,
733 &mask->enc_ipv6.dst,
734 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
735 sizeof(key->enc_ipv6.dst));
736 }
737
738 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +0300739 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
Amir Vadaibc3103f2016-09-08 16:23:47 +0300740 sizeof(key->enc_key_id.keyid));
741
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200742 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
743 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
744 sizeof(key->enc_tp.src));
745
746 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
747 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
748 sizeof(key->enc_tp.dst));
749
Or Gerlitzd9724772016-12-22 14:28:15 +0200750 if (tb[TCA_FLOWER_KEY_FLAGS])
751 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200752
Or Gerlitzd9724772016-12-22 14:28:15 +0200753 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +0200754}
755
Paul Blakey05cd2712018-04-30 14:28:30 +0300756static void fl_mask_copy(struct fl_flow_mask *dst,
757 struct fl_flow_mask *src)
Jiri Pirko77b99002015-05-12 14:56:21 +0200758{
Paul Blakey05cd2712018-04-30 14:28:30 +0300759 const void *psrc = fl_key_get_start(&src->key, src);
760 void *pdst = fl_key_get_start(&dst->key, src);
Jiri Pirko77b99002015-05-12 14:56:21 +0200761
Paul Blakey05cd2712018-04-30 14:28:30 +0300762 memcpy(pdst, psrc, fl_mask_range(src));
763 dst->range = src->range;
Jiri Pirko77b99002015-05-12 14:56:21 +0200764}
765
766static const struct rhashtable_params fl_ht_params = {
767 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
768 .head_offset = offsetof(struct cls_fl_filter, ht_node),
769 .automatic_shrinking = true,
770};
771
Paul Blakey05cd2712018-04-30 14:28:30 +0300772static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +0200773{
Paul Blakey05cd2712018-04-30 14:28:30 +0300774 mask->filter_ht_params = fl_ht_params;
775 mask->filter_ht_params.key_len = fl_mask_range(mask);
776 mask->filter_ht_params.key_offset += mask->range.start;
Jiri Pirko77b99002015-05-12 14:56:21 +0200777
Paul Blakey05cd2712018-04-30 14:28:30 +0300778 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
Jiri Pirko77b99002015-05-12 14:56:21 +0200779}
780
781#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
782#define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
Jiri Pirko77b99002015-05-12 14:56:21 +0200783
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300784#define FL_KEY_IS_MASKED(mask, member) \
785 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
786 0, FL_KEY_MEMBER_SIZE(member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200787
788#define FL_KEY_SET(keys, cnt, id, member) \
789 do { \
790 keys[cnt].key_id = id; \
791 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
792 cnt++; \
793 } while(0);
794
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300795#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200796 do { \
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300797 if (FL_KEY_IS_MASKED(mask, member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200798 FL_KEY_SET(keys, cnt, id, member); \
799 } while(0);
800
Paul Blakey05cd2712018-04-30 14:28:30 +0300801static void fl_init_dissector(struct fl_flow_mask *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +0200802{
803 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
804 size_t cnt = 0;
805
Tom Herbert42aecaa2015-06-04 09:16:39 -0700806 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
Jiri Pirko77b99002015-05-12 14:56:21 +0200807 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300808 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
809 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
810 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
811 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
812 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
813 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
814 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
815 FLOW_DISSECTOR_KEY_PORTS, tp);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300816 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300817 FLOW_DISSECTOR_KEY_IP, ip);
818 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200819 FLOW_DISSECTOR_KEY_TCP, tcp);
820 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman7b684882016-12-07 13:48:28 +0100821 FLOW_DISSECTOR_KEY_ICMP, icmp);
822 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman99d31322017-01-11 14:05:43 +0100823 FLOW_DISSECTOR_KEY_ARP, arp);
824 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400825 FLOW_DISSECTOR_KEY_MPLS, mpls);
826 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300827 FLOW_DISSECTOR_KEY_VLAN, vlan);
Hadar Hen Zion519d1052016-11-07 15:14:38 +0200828 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
829 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
830 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
831 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
832 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
833 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
834 if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
835 FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
836 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
837 enc_control);
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200838 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
839 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
Jiri Pirko77b99002015-05-12 14:56:21 +0200840
Paul Blakey05cd2712018-04-30 14:28:30 +0300841 skb_flow_dissector_init(&mask->dissector, keys, cnt);
842}
843
844static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
845 struct fl_flow_mask *mask)
846{
847 struct fl_flow_mask *newmask;
848 int err;
849
850 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
851 if (!newmask)
852 return ERR_PTR(-ENOMEM);
853
854 fl_mask_copy(newmask, mask);
855
856 err = fl_init_mask_hashtable(newmask);
857 if (err)
858 goto errout_free;
859
860 fl_init_dissector(newmask);
861
862 INIT_LIST_HEAD_RCU(&newmask->filters);
863
864 err = rhashtable_insert_fast(&head->ht, &newmask->ht_node,
865 mask_ht_params);
866 if (err)
867 goto errout_destroy;
868
869 list_add_tail_rcu(&newmask->list, &head->masks);
870
871 return newmask;
872
873errout_destroy:
874 rhashtable_destroy(&newmask->ht);
875errout_free:
876 kfree(newmask);
877
878 return ERR_PTR(err);
Jiri Pirko77b99002015-05-12 14:56:21 +0200879}
880
881static int fl_check_assign_mask(struct cls_fl_head *head,
Paul Blakey05cd2712018-04-30 14:28:30 +0300882 struct cls_fl_filter *fnew,
883 struct cls_fl_filter *fold,
Jiri Pirko77b99002015-05-12 14:56:21 +0200884 struct fl_flow_mask *mask)
885{
Paul Blakey05cd2712018-04-30 14:28:30 +0300886 struct fl_flow_mask *newmask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200887
Paul Blakey05cd2712018-04-30 14:28:30 +0300888 fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params);
889 if (!fnew->mask) {
890 if (fold)
Jiri Pirko77b99002015-05-12 14:56:21 +0200891 return -EINVAL;
Paul Blakey05cd2712018-04-30 14:28:30 +0300892
893 newmask = fl_create_new_mask(head, mask);
894 if (IS_ERR(newmask))
895 return PTR_ERR(newmask);
896
897 fnew->mask = newmask;
Paul Blakeyf6521c52018-06-03 10:06:14 +0300898 } else if (fold && fold->mask != fnew->mask) {
Paul Blakey05cd2712018-04-30 14:28:30 +0300899 return -EINVAL;
Jiri Pirko77b99002015-05-12 14:56:21 +0200900 }
901
Jiri Pirko77b99002015-05-12 14:56:21 +0200902 return 0;
903}
904
905static int fl_set_parms(struct net *net, struct tcf_proto *tp,
906 struct cls_fl_filter *f, struct fl_flow_mask *mask,
907 unsigned long base, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500908 struct nlattr *est, bool ovr,
909 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200910{
Jiri Pirko77b99002015-05-12 14:56:21 +0200911 int err;
912
Alexander Aring50a56192018-01-18 11:20:52 -0500913 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200914 if (err < 0)
915 return err;
916
917 if (tb[TCA_FLOWER_CLASSID]) {
918 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
919 tcf_bind_filter(tp, &f->res, base);
920 }
921
Alexander Aring1057c552018-01-18 11:20:54 -0500922 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200923 if (err)
Jiri Pirko45507522017-08-04 14:29:06 +0200924 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +0200925
926 fl_mask_update_range(mask);
927 fl_set_masked_key(&f->mkey, &f->key, mask);
928
Jiri Pirko77b99002015-05-12 14:56:21 +0200929 return 0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200930}
931
Jiri Pirko77b99002015-05-12 14:56:21 +0200932static int fl_change(struct net *net, struct sk_buff *in_skb,
933 struct tcf_proto *tp, unsigned long base,
934 u32 handle, struct nlattr **tca,
Alexander Aring7306db32018-01-18 11:20:51 -0500935 void **arg, bool ovr, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200936{
937 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700938 struct cls_fl_filter *fold = *arg;
Jiri Pirko77b99002015-05-12 14:56:21 +0200939 struct cls_fl_filter *fnew;
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100940 struct nlattr **tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200941 struct fl_flow_mask mask = {};
942 int err;
943
944 if (!tca[TCA_OPTIONS])
945 return -EINVAL;
946
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100947 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
948 if (!tb)
949 return -ENOBUFS;
950
Johannes Bergfceb6432017-04-12 14:34:07 +0200951 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
952 fl_policy, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +0200953 if (err < 0)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100954 goto errout_tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200955
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100956 if (fold && handle && fold->handle != handle) {
957 err = -EINVAL;
958 goto errout_tb;
959 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200960
961 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100962 if (!fnew) {
963 err = -ENOBUFS;
964 goto errout_tb;
965 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200966
WANG Congb9a24bb2016-08-19 12:36:54 -0700967 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
968 if (err < 0)
969 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +0200970
971 if (!handle) {
Matthew Wilcox85bd0432017-11-28 10:53:03 -0500972 handle = 1;
973 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
974 INT_MAX, GFP_KERNEL);
975 } else if (!fold) {
976 /* user specifies a handle and it doesn't exist */
977 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
978 handle, GFP_KERNEL);
Jiri Pirko77b99002015-05-12 14:56:21 +0200979 }
Matthew Wilcox85bd0432017-11-28 10:53:03 -0500980 if (err)
981 goto errout;
982 fnew->handle = handle;
Jiri Pirko77b99002015-05-12 14:56:21 +0200983
Amir Vadaie69985c2016-06-05 17:11:18 +0300984 if (tb[TCA_FLOWER_FLAGS]) {
985 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
986
987 if (!tc_flags_valid(fnew->flags)) {
988 err = -EINVAL;
Cong Wangfe2502e2017-09-20 09:18:45 -0700989 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +0300990 }
991 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200992
Alexander Aring50a56192018-01-18 11:20:52 -0500993 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr,
994 extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200995 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700996 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +0200997
Paul Blakey05cd2712018-04-30 14:28:30 +0300998 err = fl_check_assign_mask(head, fnew, fold, &mask);
Jiri Pirko77b99002015-05-12 14:56:21 +0200999 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -07001000 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +02001001
Amir Vadaie8eb36c2016-06-13 12:06:39 +03001002 if (!tc_skip_sw(fnew->flags)) {
Paul Blakey05cd2712018-04-30 14:28:30 +03001003 if (!fold && fl_lookup(fnew->mask, &fnew->mkey)) {
Paul Blakeya3308d82017-01-16 10:45:13 +02001004 err = -EEXIST;
Paul Blakey05cd2712018-04-30 14:28:30 +03001005 goto errout_mask;
Paul Blakeya3308d82017-01-16 10:45:13 +02001006 }
1007
Paul Blakey05cd2712018-04-30 14:28:30 +03001008 err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node,
1009 fnew->mask->filter_ht_params);
Amir Vadaie69985c2016-06-05 17:11:18 +03001010 if (err)
Paul Blakey05cd2712018-04-30 14:28:30 +03001011 goto errout_mask;
Amir Vadaie69985c2016-06-05 17:11:18 +03001012 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001013
Hadar Hen Zion79685212016-12-01 14:06:34 +02001014 if (!tc_skip_hw(fnew->flags)) {
Paul Blakey05cd2712018-04-30 14:28:30 +03001015 err = fl_hw_replace_filter(tp, fnew, extack);
Hadar Hen Zion79685212016-12-01 14:06:34 +02001016 if (err)
Paul Blakey05cd2712018-04-30 14:28:30 +03001017 goto errout_mask;
Hadar Hen Zion79685212016-12-01 14:06:34 +02001018 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001019
Or Gerlitz55593962017-02-16 10:31:13 +02001020 if (!tc_in_hw(fnew->flags))
1021 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1022
Amir Vadai5b33f482016-03-08 12:42:29 +02001023 if (fold) {
Jiri Pirko725cbb622016-11-28 15:40:13 +01001024 if (!tc_skip_sw(fold->flags))
Paul Blakey05cd2712018-04-30 14:28:30 +03001025 rhashtable_remove_fast(&fold->mask->ht,
1026 &fold->ht_node,
1027 fold->mask->filter_ht_params);
Hadar Hen Zion79685212016-12-01 14:06:34 +02001028 if (!tc_skip_hw(fold->flags))
Jakub Kicinski1b0f8032018-01-24 12:54:21 -08001029 fl_hw_destroy_filter(tp, fold, NULL);
Amir Vadai5b33f482016-03-08 12:42:29 +02001030 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001031
WANG Cong8113c092017-08-04 21:31:43 -07001032 *arg = fnew;
Jiri Pirko77b99002015-05-12 14:56:21 +02001033
1034 if (fold) {
Matthew Wilcox234a4622017-11-28 09:56:36 -05001035 idr_replace(&head->handle_idr, fnew, fnew->handle);
Daniel Borkmannff3532f2015-07-17 22:38:44 +02001036 list_replace_rcu(&fold->list, &fnew->list);
Jiri Pirko77b99002015-05-12 14:56:21 +02001037 tcf_unbind_filter(tp, &fold->res);
Cong Wang0dadc112017-11-06 13:47:24 -08001038 tcf_exts_get_net(&fold->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -07001039 tcf_queue_work(&fold->rwork, fl_destroy_filter_work);
Jiri Pirko77b99002015-05-12 14:56:21 +02001040 } else {
Paul Blakey05cd2712018-04-30 14:28:30 +03001041 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
Jiri Pirko77b99002015-05-12 14:56:21 +02001042 }
1043
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001044 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +02001045 return 0;
1046
Paul Blakey05cd2712018-04-30 14:28:30 +03001047errout_mask:
1048 fl_mask_put(head, fnew->mask, false);
1049
Cong Wangfe2502e2017-09-20 09:18:45 -07001050errout_idr:
Paul Blakey8258d2d2018-05-30 11:29:15 +03001051 if (!fold)
Matthew Wilcox9c160942017-11-28 09:48:43 -05001052 idr_remove(&head->handle_idr, fnew->handle);
Jiri Pirko77b99002015-05-12 14:56:21 +02001053errout:
WANG Congb9a24bb2016-08-19 12:36:54 -07001054 tcf_exts_destroy(&fnew->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +02001055 kfree(fnew);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001056errout_tb:
1057 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +02001058 return err;
1059}
1060
Alexander Aring571acf22018-01-18 11:20:53 -05001061static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
1062 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001063{
1064 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -07001065 struct cls_fl_filter *f = arg;
Jiri Pirko77b99002015-05-12 14:56:21 +02001066
Jiri Pirko725cbb622016-11-28 15:40:13 +01001067 if (!tc_skip_sw(f->flags))
Paul Blakey05cd2712018-04-30 14:28:30 +03001068 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
1069 f->mask->filter_ht_params);
Jakub Kicinski1b0f8032018-01-24 12:54:21 -08001070 __fl_delete(tp, f, extack);
Paul Blakey05cd2712018-04-30 14:28:30 +03001071 *last = list_empty(&head->masks);
Jiri Pirko77b99002015-05-12 14:56:21 +02001072 return 0;
1073}
1074
1075static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1076{
1077 struct cls_fl_head *head = rtnl_dereference(tp->root);
1078 struct cls_fl_filter *f;
Paul Blakey05cd2712018-04-30 14:28:30 +03001079 struct fl_flow_mask *mask;
Jiri Pirko77b99002015-05-12 14:56:21 +02001080
Paul Blakey05cd2712018-04-30 14:28:30 +03001081 list_for_each_entry_rcu(mask, &head->masks, list) {
1082 list_for_each_entry_rcu(f, &mask->filters, list) {
1083 if (arg->count < arg->skip)
1084 goto skip;
1085 if (arg->fn(tp, f, arg) < 0) {
1086 arg->stop = 1;
1087 break;
1088 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001089skip:
Paul Blakey05cd2712018-04-30 14:28:30 +03001090 arg->count++;
1091 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001092 }
1093}
1094
John Hurley31533cb2018-06-25 14:30:06 -07001095static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1096 void *cb_priv, struct netlink_ext_ack *extack)
1097{
1098 struct cls_fl_head *head = rtnl_dereference(tp->root);
1099 struct tc_cls_flower_offload cls_flower = {};
1100 struct tcf_block *block = tp->chain->block;
1101 struct fl_flow_mask *mask;
1102 struct cls_fl_filter *f;
1103 int err;
1104
1105 list_for_each_entry(mask, &head->masks, list) {
1106 list_for_each_entry(f, &mask->filters, list) {
1107 if (tc_skip_hw(f->flags))
1108 continue;
1109
1110 tc_cls_common_offload_init(&cls_flower.common, tp,
1111 f->flags, extack);
1112 cls_flower.command = add ?
1113 TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY;
1114 cls_flower.cookie = (unsigned long)f;
1115 cls_flower.dissector = &mask->dissector;
1116 cls_flower.mask = &f->mkey;
1117 cls_flower.key = &f->key;
1118 cls_flower.exts = &f->exts;
1119 cls_flower.classid = f->res.classid;
1120
1121 err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
1122 if (err) {
1123 if (add && tc_skip_sw(f->flags))
1124 return err;
1125 continue;
1126 }
1127
1128 tc_cls_offload_cnt_update(block, &f->in_hw_count,
1129 &f->flags, add);
1130 }
1131 }
1132
1133 return 0;
1134}
1135
Jiri Pirko77b99002015-05-12 14:56:21 +02001136static int fl_dump_key_val(struct sk_buff *skb,
1137 void *val, int val_type,
1138 void *mask, int mask_type, int len)
1139{
1140 int err;
1141
1142 if (!memchr_inv(mask, 0, len))
1143 return 0;
1144 err = nla_put(skb, val_type, len, val);
1145 if (err)
1146 return err;
1147 if (mask_type != TCA_FLOWER_UNSPEC) {
1148 err = nla_put(skb, mask_type, len, mask);
1149 if (err)
1150 return err;
1151 }
1152 return 0;
1153}
1154
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001155static int fl_dump_key_mpls(struct sk_buff *skb,
1156 struct flow_dissector_key_mpls *mpls_key,
1157 struct flow_dissector_key_mpls *mpls_mask)
1158{
1159 int err;
1160
1161 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1162 return 0;
1163 if (mpls_mask->mpls_ttl) {
1164 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1165 mpls_key->mpls_ttl);
1166 if (err)
1167 return err;
1168 }
1169 if (mpls_mask->mpls_tc) {
1170 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1171 mpls_key->mpls_tc);
1172 if (err)
1173 return err;
1174 }
1175 if (mpls_mask->mpls_label) {
1176 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1177 mpls_key->mpls_label);
1178 if (err)
1179 return err;
1180 }
1181 if (mpls_mask->mpls_bos) {
1182 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1183 mpls_key->mpls_bos);
1184 if (err)
1185 return err;
1186 }
1187 return 0;
1188}
1189
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001190static int fl_dump_key_ip(struct sk_buff *skb,
1191 struct flow_dissector_key_ip *key,
1192 struct flow_dissector_key_ip *mask)
1193{
1194 if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos,
1195 TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) ||
1196 fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl,
1197 TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl)))
1198 return -1;
1199
1200 return 0;
1201}
1202
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001203static int fl_dump_key_vlan(struct sk_buff *skb,
1204 struct flow_dissector_key_vlan *vlan_key,
1205 struct flow_dissector_key_vlan *vlan_mask)
1206{
1207 int err;
1208
1209 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1210 return 0;
1211 if (vlan_mask->vlan_id) {
1212 err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID,
1213 vlan_key->vlan_id);
1214 if (err)
1215 return err;
1216 }
1217 if (vlan_mask->vlan_priority) {
1218 err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO,
1219 vlan_key->vlan_priority);
1220 if (err)
1221 return err;
1222 }
1223 return 0;
1224}
1225
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001226static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1227 u32 *flower_key, u32 *flower_mask,
1228 u32 flower_flag_bit, u32 dissector_flag_bit)
1229{
1230 if (dissector_mask & dissector_flag_bit) {
1231 *flower_mask |= flower_flag_bit;
1232 if (dissector_key & dissector_flag_bit)
1233 *flower_key |= flower_flag_bit;
1234 }
1235}
1236
1237static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1238{
1239 u32 key, mask;
1240 __be32 _key, _mask;
1241 int err;
1242
1243 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1244 return 0;
1245
1246 key = 0;
1247 mask = 0;
1248
1249 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1250 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +01001251 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1252 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1253 FLOW_DIS_FIRST_FRAG);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001254
1255 _key = cpu_to_be32(key);
1256 _mask = cpu_to_be32(mask);
1257
1258 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1259 if (err)
1260 return err;
1261
1262 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1263}
1264
WANG Cong8113c092017-08-04 21:31:43 -07001265static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jiri Pirko77b99002015-05-12 14:56:21 +02001266 struct sk_buff *skb, struct tcmsg *t)
1267{
WANG Cong8113c092017-08-04 21:31:43 -07001268 struct cls_fl_filter *f = fh;
Jiri Pirko77b99002015-05-12 14:56:21 +02001269 struct nlattr *nest;
1270 struct fl_flow_key *key, *mask;
1271
1272 if (!f)
1273 return skb->len;
1274
1275 t->tcm_handle = f->handle;
1276
1277 nest = nla_nest_start(skb, TCA_OPTIONS);
1278 if (!nest)
1279 goto nla_put_failure;
1280
1281 if (f->res.classid &&
1282 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1283 goto nla_put_failure;
1284
1285 key = &f->key;
Paul Blakey05cd2712018-04-30 14:28:30 +03001286 mask = &f->mask->key;
Jiri Pirko77b99002015-05-12 14:56:21 +02001287
1288 if (mask->indev_ifindex) {
1289 struct net_device *dev;
1290
1291 dev = __dev_get_by_index(net, key->indev_ifindex);
1292 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1293 goto nla_put_failure;
1294 }
1295
Hadar Hen Zion79685212016-12-01 14:06:34 +02001296 if (!tc_skip_hw(f->flags))
1297 fl_hw_update_stats(tp, f);
Amir Vadai10cbc682016-05-13 12:55:37 +00001298
Jiri Pirko77b99002015-05-12 14:56:21 +02001299 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1300 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1301 sizeof(key->eth.dst)) ||
1302 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1303 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1304 sizeof(key->eth.src)) ||
1305 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1306 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1307 sizeof(key->basic.n_proto)))
1308 goto nla_put_failure;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001309
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001310 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1311 goto nla_put_failure;
1312
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001313 if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan))
1314 goto nla_put_failure;
1315
Jianbo Liud3069512018-07-06 05:38:15 +00001316 if (mask->vlan.vlan_tpid &&
1317 nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, key->basic.n_proto))
1318 goto nla_put_failure;
1319
Jiri Pirko77b99002015-05-12 14:56:21 +02001320 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1321 key->basic.n_proto == htons(ETH_P_IPV6)) &&
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001322 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
Jiri Pirko77b99002015-05-12 14:56:21 +02001323 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001324 sizeof(key->basic.ip_proto)) ||
1325 fl_dump_key_ip(skb, &key->ip, &mask->ip)))
Jiri Pirko77b99002015-05-12 14:56:21 +02001326 goto nla_put_failure;
1327
Tom Herbertc3f83242015-06-04 09:16:40 -07001328 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001329 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1330 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1331 sizeof(key->ipv4.src)) ||
1332 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1333 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1334 sizeof(key->ipv4.dst))))
1335 goto nla_put_failure;
Tom Herbertc3f83242015-06-04 09:16:40 -07001336 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001337 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1338 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1339 sizeof(key->ipv6.src)) ||
1340 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1341 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1342 sizeof(key->ipv6.dst))))
1343 goto nla_put_failure;
1344
1345 if (key->basic.ip_proto == IPPROTO_TCP &&
1346 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001347 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001348 sizeof(key->tp.src)) ||
1349 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001350 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001351 sizeof(key->tp.dst)) ||
1352 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1353 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1354 sizeof(key->tcp.flags))))
Jiri Pirko77b99002015-05-12 14:56:21 +02001355 goto nla_put_failure;
1356 else if (key->basic.ip_proto == IPPROTO_UDP &&
1357 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001358 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001359 sizeof(key->tp.src)) ||
1360 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001361 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001362 sizeof(key->tp.dst))))
1363 goto nla_put_failure;
Simon Horman5976c5f2016-11-03 13:24:21 +01001364 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1365 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1366 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1367 sizeof(key->tp.src)) ||
1368 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1369 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1370 sizeof(key->tp.dst))))
1371 goto nla_put_failure;
Simon Horman7b684882016-12-07 13:48:28 +01001372 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1373 key->basic.ip_proto == IPPROTO_ICMP &&
1374 (fl_dump_key_val(skb, &key->icmp.type,
1375 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1376 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1377 sizeof(key->icmp.type)) ||
1378 fl_dump_key_val(skb, &key->icmp.code,
1379 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1380 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1381 sizeof(key->icmp.code))))
1382 goto nla_put_failure;
1383 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1384 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1385 (fl_dump_key_val(skb, &key->icmp.type,
1386 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1387 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1388 sizeof(key->icmp.type)) ||
1389 fl_dump_key_val(skb, &key->icmp.code,
1390 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1391 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1392 sizeof(key->icmp.code))))
1393 goto nla_put_failure;
Simon Horman99d31322017-01-11 14:05:43 +01001394 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1395 key->basic.n_proto == htons(ETH_P_RARP)) &&
1396 (fl_dump_key_val(skb, &key->arp.sip,
1397 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1398 TCA_FLOWER_KEY_ARP_SIP_MASK,
1399 sizeof(key->arp.sip)) ||
1400 fl_dump_key_val(skb, &key->arp.tip,
1401 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1402 TCA_FLOWER_KEY_ARP_TIP_MASK,
1403 sizeof(key->arp.tip)) ||
1404 fl_dump_key_val(skb, &key->arp.op,
1405 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1406 TCA_FLOWER_KEY_ARP_OP_MASK,
1407 sizeof(key->arp.op)) ||
1408 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1409 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1410 sizeof(key->arp.sha)) ||
1411 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1412 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1413 sizeof(key->arp.tha))))
1414 goto nla_put_failure;
Jiri Pirko77b99002015-05-12 14:56:21 +02001415
Amir Vadaibc3103f2016-09-08 16:23:47 +03001416 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1417 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1418 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1419 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1420 sizeof(key->enc_ipv4.src)) ||
1421 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1422 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1423 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1424 sizeof(key->enc_ipv4.dst))))
1425 goto nla_put_failure;
1426 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1427 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1428 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1429 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1430 sizeof(key->enc_ipv6.src)) ||
1431 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1432 TCA_FLOWER_KEY_ENC_IPV6_DST,
1433 &mask->enc_ipv6.dst,
1434 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1435 sizeof(key->enc_ipv6.dst))))
1436 goto nla_put_failure;
1437
1438 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001439 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001440 sizeof(key->enc_key_id)) ||
1441 fl_dump_key_val(skb, &key->enc_tp.src,
1442 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1443 &mask->enc_tp.src,
1444 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1445 sizeof(key->enc_tp.src)) ||
1446 fl_dump_key_val(skb, &key->enc_tp.dst,
1447 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1448 &mask->enc_tp.dst,
1449 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1450 sizeof(key->enc_tp.dst)))
Amir Vadaibc3103f2016-09-08 16:23:47 +03001451 goto nla_put_failure;
1452
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001453 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1454 goto nla_put_failure;
1455
Or Gerlitz749e6722017-02-16 10:31:10 +02001456 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1457 goto nla_put_failure;
Amir Vadaie69985c2016-06-05 17:11:18 +03001458
Jiri Pirko77b99002015-05-12 14:56:21 +02001459 if (tcf_exts_dump(skb, &f->exts))
1460 goto nla_put_failure;
1461
1462 nla_nest_end(skb, nest);
1463
1464 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1465 goto nla_put_failure;
1466
1467 return skb->len;
1468
1469nla_put_failure:
1470 nla_nest_cancel(skb, nest);
1471 return -1;
1472}
1473
Cong Wang07d79fc2017-08-30 14:30:36 -07001474static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
1475{
1476 struct cls_fl_filter *f = fh;
1477
1478 if (f && f->res.classid == classid)
1479 f->res.class = cl;
1480}
1481
Jiri Pirko77b99002015-05-12 14:56:21 +02001482static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1483 .kind = "flower",
1484 .classify = fl_classify,
1485 .init = fl_init,
1486 .destroy = fl_destroy,
1487 .get = fl_get,
1488 .change = fl_change,
1489 .delete = fl_delete,
1490 .walk = fl_walk,
John Hurley31533cb2018-06-25 14:30:06 -07001491 .reoffload = fl_reoffload,
Jiri Pirko77b99002015-05-12 14:56:21 +02001492 .dump = fl_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07001493 .bind_class = fl_bind_class,
Jiri Pirko77b99002015-05-12 14:56:21 +02001494 .owner = THIS_MODULE,
1495};
1496
1497static int __init cls_fl_init(void)
1498{
1499 return register_tcf_proto_ops(&cls_fl_ops);
1500}
1501
1502static void __exit cls_fl_exit(void)
1503{
1504 unregister_tcf_proto_ops(&cls_fl_ops);
1505}
1506
1507module_init(cls_fl_init);
1508module_exit(cls_fl_exit);
1509
1510MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1511MODULE_DESCRIPTION("Flower classifier");
1512MODULE_LICENSE("GPL v2");