blob: 352876bb901b7af5076bf3a5930dde979fc0a861 [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,
503 struct flow_dissector_key_vlan *key_val,
504 struct flow_dissector_key_vlan *key_mask)
505{
506#define VLAN_PRIORITY_MASK 0x7
507
508 if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
509 key_val->vlan_id =
510 nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
511 key_mask->vlan_id = VLAN_VID_MASK;
512 }
513 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
514 key_val->vlan_priority =
515 nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
516 VLAN_PRIORITY_MASK;
517 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
518 }
519}
520
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200521static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
522 u32 *dissector_key, u32 *dissector_mask,
523 u32 flower_flag_bit, u32 dissector_flag_bit)
524{
525 if (flower_mask & flower_flag_bit) {
526 *dissector_mask |= dissector_flag_bit;
527 if (flower_key & flower_flag_bit)
528 *dissector_key |= dissector_flag_bit;
529 }
530}
531
Or Gerlitzd9724772016-12-22 14:28:15 +0200532static int fl_set_key_flags(struct nlattr **tb,
533 u32 *flags_key, u32 *flags_mask)
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200534{
535 u32 key, mask;
536
Or Gerlitzd9724772016-12-22 14:28:15 +0200537 /* mask is mandatory for flags */
538 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
539 return -EINVAL;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200540
541 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
Or Gerlitzd9724772016-12-22 14:28:15 +0200542 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200543
544 *flags_key = 0;
545 *flags_mask = 0;
546
547 fl_set_key_flag(key, mask, flags_key, flags_mask,
548 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +0100549 fl_set_key_flag(key, mask, flags_key, flags_mask,
550 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
551 FLOW_DIS_FIRST_FRAG);
Or Gerlitzd9724772016-12-22 14:28:15 +0200552
553 return 0;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200554}
555
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300556static void fl_set_key_ip(struct nlattr **tb,
557 struct flow_dissector_key_ip *key,
558 struct flow_dissector_key_ip *mask)
559{
560 fl_set_key_val(tb, &key->tos, TCA_FLOWER_KEY_IP_TOS,
561 &mask->tos, TCA_FLOWER_KEY_IP_TOS_MASK,
562 sizeof(key->tos));
563
564 fl_set_key_val(tb, &key->ttl, TCA_FLOWER_KEY_IP_TTL,
565 &mask->ttl, TCA_FLOWER_KEY_IP_TTL_MASK,
566 sizeof(key->ttl));
567}
568
Jiri Pirko77b99002015-05-12 14:56:21 +0200569static int fl_set_key(struct net *net, struct nlattr **tb,
Alexander Aring1057c552018-01-18 11:20:54 -0500570 struct fl_flow_key *key, struct fl_flow_key *mask,
571 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200572{
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300573 __be16 ethertype;
Or Gerlitzd9724772016-12-22 14:28:15 +0200574 int ret = 0;
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400575#ifdef CONFIG_NET_CLS_IND
Jiri Pirko77b99002015-05-12 14:56:21 +0200576 if (tb[TCA_FLOWER_INDEV]) {
Alexander Aring1057c552018-01-18 11:20:54 -0500577 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200578 if (err < 0)
579 return err;
580 key->indev_ifindex = err;
581 mask->indev_ifindex = 0xffffffff;
582 }
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400583#endif
Jiri Pirko77b99002015-05-12 14:56:21 +0200584
585 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
586 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
587 sizeof(key->eth.dst));
588 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
589 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
590 sizeof(key->eth.src));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500591
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200592 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300593 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
594
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200595 if (ethertype == htons(ETH_P_8021Q)) {
596 fl_set_key_vlan(tb, &key->vlan, &mask->vlan);
597 fl_set_key_val(tb, &key->basic.n_proto,
598 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
599 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
600 sizeof(key->basic.n_proto));
601 } else {
602 key->basic.n_proto = ethertype;
603 mask->basic.n_proto = cpu_to_be16(~0);
604 }
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300605 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500606
Jiri Pirko77b99002015-05-12 14:56:21 +0200607 if (key->basic.n_proto == htons(ETH_P_IP) ||
608 key->basic.n_proto == htons(ETH_P_IPV6)) {
609 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
610 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
611 sizeof(key->basic.ip_proto));
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300612 fl_set_key_ip(tb, &key->ip, &mask->ip);
Jiri Pirko77b99002015-05-12 14:56:21 +0200613 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500614
615 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
616 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200617 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200618 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
619 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
620 sizeof(key->ipv4.src));
621 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
622 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
623 sizeof(key->ipv4.dst));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500624 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
625 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200626 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200627 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
628 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
629 sizeof(key->ipv6.src));
630 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
631 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
632 sizeof(key->ipv6.dst));
633 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500634
Jiri Pirko77b99002015-05-12 14:56:21 +0200635 if (key->basic.ip_proto == IPPROTO_TCP) {
636 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300637 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200638 sizeof(key->tp.src));
639 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300640 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200641 sizeof(key->tp.dst));
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200642 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
643 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
644 sizeof(key->tcp.flags));
Jiri Pirko77b99002015-05-12 14:56:21 +0200645 } else if (key->basic.ip_proto == IPPROTO_UDP) {
646 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300647 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200648 sizeof(key->tp.src));
649 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300650 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200651 sizeof(key->tp.dst));
Simon Horman5976c5f2016-11-03 13:24:21 +0100652 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
653 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
654 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
655 sizeof(key->tp.src));
656 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
657 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
658 sizeof(key->tp.dst));
Simon Horman7b684882016-12-07 13:48:28 +0100659 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
660 key->basic.ip_proto == IPPROTO_ICMP) {
661 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
662 &mask->icmp.type,
663 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
664 sizeof(key->icmp.type));
665 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
666 &mask->icmp.code,
667 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
668 sizeof(key->icmp.code));
669 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
670 key->basic.ip_proto == IPPROTO_ICMPV6) {
671 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
672 &mask->icmp.type,
673 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
674 sizeof(key->icmp.type));
Simon Horman040587a2017-01-30 16:19:02 +0100675 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
Simon Horman7b684882016-12-07 13:48:28 +0100676 &mask->icmp.code,
Simon Horman040587a2017-01-30 16:19:02 +0100677 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
Simon Horman7b684882016-12-07 13:48:28 +0100678 sizeof(key->icmp.code));
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400679 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
680 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400681 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
682 if (ret)
683 return ret;
Simon Horman99d31322017-01-11 14:05:43 +0100684 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
685 key->basic.n_proto == htons(ETH_P_RARP)) {
686 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
687 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
688 sizeof(key->arp.sip));
689 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
690 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
691 sizeof(key->arp.tip));
692 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
693 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
694 sizeof(key->arp.op));
695 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
696 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
697 sizeof(key->arp.sha));
698 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
699 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
700 sizeof(key->arp.tha));
Jiri Pirko77b99002015-05-12 14:56:21 +0200701 }
702
Amir Vadaibc3103f2016-09-08 16:23:47 +0300703 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
704 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
705 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200706 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300707 fl_set_key_val(tb, &key->enc_ipv4.src,
708 TCA_FLOWER_KEY_ENC_IPV4_SRC,
709 &mask->enc_ipv4.src,
710 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
711 sizeof(key->enc_ipv4.src));
712 fl_set_key_val(tb, &key->enc_ipv4.dst,
713 TCA_FLOWER_KEY_ENC_IPV4_DST,
714 &mask->enc_ipv4.dst,
715 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
716 sizeof(key->enc_ipv4.dst));
717 }
718
719 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
720 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
721 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200722 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300723 fl_set_key_val(tb, &key->enc_ipv6.src,
724 TCA_FLOWER_KEY_ENC_IPV6_SRC,
725 &mask->enc_ipv6.src,
726 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
727 sizeof(key->enc_ipv6.src));
728 fl_set_key_val(tb, &key->enc_ipv6.dst,
729 TCA_FLOWER_KEY_ENC_IPV6_DST,
730 &mask->enc_ipv6.dst,
731 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
732 sizeof(key->enc_ipv6.dst));
733 }
734
735 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +0300736 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
Amir Vadaibc3103f2016-09-08 16:23:47 +0300737 sizeof(key->enc_key_id.keyid));
738
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200739 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
740 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
741 sizeof(key->enc_tp.src));
742
743 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
744 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
745 sizeof(key->enc_tp.dst));
746
Or Gerlitzd9724772016-12-22 14:28:15 +0200747 if (tb[TCA_FLOWER_KEY_FLAGS])
748 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200749
Or Gerlitzd9724772016-12-22 14:28:15 +0200750 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +0200751}
752
Paul Blakey05cd2712018-04-30 14:28:30 +0300753static void fl_mask_copy(struct fl_flow_mask *dst,
754 struct fl_flow_mask *src)
Jiri Pirko77b99002015-05-12 14:56:21 +0200755{
Paul Blakey05cd2712018-04-30 14:28:30 +0300756 const void *psrc = fl_key_get_start(&src->key, src);
757 void *pdst = fl_key_get_start(&dst->key, src);
Jiri Pirko77b99002015-05-12 14:56:21 +0200758
Paul Blakey05cd2712018-04-30 14:28:30 +0300759 memcpy(pdst, psrc, fl_mask_range(src));
760 dst->range = src->range;
Jiri Pirko77b99002015-05-12 14:56:21 +0200761}
762
763static const struct rhashtable_params fl_ht_params = {
764 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
765 .head_offset = offsetof(struct cls_fl_filter, ht_node),
766 .automatic_shrinking = true,
767};
768
Paul Blakey05cd2712018-04-30 14:28:30 +0300769static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +0200770{
Paul Blakey05cd2712018-04-30 14:28:30 +0300771 mask->filter_ht_params = fl_ht_params;
772 mask->filter_ht_params.key_len = fl_mask_range(mask);
773 mask->filter_ht_params.key_offset += mask->range.start;
Jiri Pirko77b99002015-05-12 14:56:21 +0200774
Paul Blakey05cd2712018-04-30 14:28:30 +0300775 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
Jiri Pirko77b99002015-05-12 14:56:21 +0200776}
777
778#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
779#define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
Jiri Pirko77b99002015-05-12 14:56:21 +0200780
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300781#define FL_KEY_IS_MASKED(mask, member) \
782 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
783 0, FL_KEY_MEMBER_SIZE(member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200784
785#define FL_KEY_SET(keys, cnt, id, member) \
786 do { \
787 keys[cnt].key_id = id; \
788 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
789 cnt++; \
790 } while(0);
791
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300792#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200793 do { \
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300794 if (FL_KEY_IS_MASKED(mask, member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200795 FL_KEY_SET(keys, cnt, id, member); \
796 } while(0);
797
Paul Blakey05cd2712018-04-30 14:28:30 +0300798static void fl_init_dissector(struct fl_flow_mask *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +0200799{
800 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
801 size_t cnt = 0;
802
Tom Herbert42aecaa2015-06-04 09:16:39 -0700803 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
Jiri Pirko77b99002015-05-12 14:56:21 +0200804 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300805 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
806 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
807 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
808 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
809 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
810 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
811 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
812 FLOW_DISSECTOR_KEY_PORTS, tp);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300813 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300814 FLOW_DISSECTOR_KEY_IP, ip);
815 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200816 FLOW_DISSECTOR_KEY_TCP, tcp);
817 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman7b684882016-12-07 13:48:28 +0100818 FLOW_DISSECTOR_KEY_ICMP, icmp);
819 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman99d31322017-01-11 14:05:43 +0100820 FLOW_DISSECTOR_KEY_ARP, arp);
821 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400822 FLOW_DISSECTOR_KEY_MPLS, mpls);
823 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300824 FLOW_DISSECTOR_KEY_VLAN, vlan);
Hadar Hen Zion519d1052016-11-07 15:14:38 +0200825 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
826 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
827 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
828 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
829 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
830 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
831 if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
832 FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
833 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
834 enc_control);
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200835 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
836 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
Jiri Pirko77b99002015-05-12 14:56:21 +0200837
Paul Blakey05cd2712018-04-30 14:28:30 +0300838 skb_flow_dissector_init(&mask->dissector, keys, cnt);
839}
840
841static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
842 struct fl_flow_mask *mask)
843{
844 struct fl_flow_mask *newmask;
845 int err;
846
847 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
848 if (!newmask)
849 return ERR_PTR(-ENOMEM);
850
851 fl_mask_copy(newmask, mask);
852
853 err = fl_init_mask_hashtable(newmask);
854 if (err)
855 goto errout_free;
856
857 fl_init_dissector(newmask);
858
859 INIT_LIST_HEAD_RCU(&newmask->filters);
860
861 err = rhashtable_insert_fast(&head->ht, &newmask->ht_node,
862 mask_ht_params);
863 if (err)
864 goto errout_destroy;
865
866 list_add_tail_rcu(&newmask->list, &head->masks);
867
868 return newmask;
869
870errout_destroy:
871 rhashtable_destroy(&newmask->ht);
872errout_free:
873 kfree(newmask);
874
875 return ERR_PTR(err);
Jiri Pirko77b99002015-05-12 14:56:21 +0200876}
877
878static int fl_check_assign_mask(struct cls_fl_head *head,
Paul Blakey05cd2712018-04-30 14:28:30 +0300879 struct cls_fl_filter *fnew,
880 struct cls_fl_filter *fold,
Jiri Pirko77b99002015-05-12 14:56:21 +0200881 struct fl_flow_mask *mask)
882{
Paul Blakey05cd2712018-04-30 14:28:30 +0300883 struct fl_flow_mask *newmask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200884
Paul Blakey05cd2712018-04-30 14:28:30 +0300885 fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params);
886 if (!fnew->mask) {
887 if (fold)
Jiri Pirko77b99002015-05-12 14:56:21 +0200888 return -EINVAL;
Paul Blakey05cd2712018-04-30 14:28:30 +0300889
890 newmask = fl_create_new_mask(head, mask);
891 if (IS_ERR(newmask))
892 return PTR_ERR(newmask);
893
894 fnew->mask = newmask;
Paul Blakeyf6521c52018-06-03 10:06:14 +0300895 } else if (fold && fold->mask != fnew->mask) {
Paul Blakey05cd2712018-04-30 14:28:30 +0300896 return -EINVAL;
Jiri Pirko77b99002015-05-12 14:56:21 +0200897 }
898
Jiri Pirko77b99002015-05-12 14:56:21 +0200899 return 0;
900}
901
902static int fl_set_parms(struct net *net, struct tcf_proto *tp,
903 struct cls_fl_filter *f, struct fl_flow_mask *mask,
904 unsigned long base, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500905 struct nlattr *est, bool ovr,
906 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200907{
Jiri Pirko77b99002015-05-12 14:56:21 +0200908 int err;
909
Alexander Aring50a56192018-01-18 11:20:52 -0500910 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200911 if (err < 0)
912 return err;
913
914 if (tb[TCA_FLOWER_CLASSID]) {
915 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
916 tcf_bind_filter(tp, &f->res, base);
917 }
918
Alexander Aring1057c552018-01-18 11:20:54 -0500919 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200920 if (err)
Jiri Pirko45507522017-08-04 14:29:06 +0200921 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +0200922
923 fl_mask_update_range(mask);
924 fl_set_masked_key(&f->mkey, &f->key, mask);
925
Jiri Pirko77b99002015-05-12 14:56:21 +0200926 return 0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200927}
928
Jiri Pirko77b99002015-05-12 14:56:21 +0200929static int fl_change(struct net *net, struct sk_buff *in_skb,
930 struct tcf_proto *tp, unsigned long base,
931 u32 handle, struct nlattr **tca,
Alexander Aring7306db32018-01-18 11:20:51 -0500932 void **arg, bool ovr, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200933{
934 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700935 struct cls_fl_filter *fold = *arg;
Jiri Pirko77b99002015-05-12 14:56:21 +0200936 struct cls_fl_filter *fnew;
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100937 struct nlattr **tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200938 struct fl_flow_mask mask = {};
939 int err;
940
941 if (!tca[TCA_OPTIONS])
942 return -EINVAL;
943
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100944 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
945 if (!tb)
946 return -ENOBUFS;
947
Johannes Bergfceb6432017-04-12 14:34:07 +0200948 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
949 fl_policy, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +0200950 if (err < 0)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100951 goto errout_tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200952
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100953 if (fold && handle && fold->handle != handle) {
954 err = -EINVAL;
955 goto errout_tb;
956 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200957
958 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100959 if (!fnew) {
960 err = -ENOBUFS;
961 goto errout_tb;
962 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200963
WANG Congb9a24bb2016-08-19 12:36:54 -0700964 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
965 if (err < 0)
966 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +0200967
968 if (!handle) {
Matthew Wilcox85bd0432017-11-28 10:53:03 -0500969 handle = 1;
970 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
971 INT_MAX, GFP_KERNEL);
972 } else if (!fold) {
973 /* user specifies a handle and it doesn't exist */
974 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
975 handle, GFP_KERNEL);
Jiri Pirko77b99002015-05-12 14:56:21 +0200976 }
Matthew Wilcox85bd0432017-11-28 10:53:03 -0500977 if (err)
978 goto errout;
979 fnew->handle = handle;
Jiri Pirko77b99002015-05-12 14:56:21 +0200980
Amir Vadaie69985c2016-06-05 17:11:18 +0300981 if (tb[TCA_FLOWER_FLAGS]) {
982 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
983
984 if (!tc_flags_valid(fnew->flags)) {
985 err = -EINVAL;
Cong Wangfe2502e2017-09-20 09:18:45 -0700986 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +0300987 }
988 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200989
Alexander Aring50a56192018-01-18 11:20:52 -0500990 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr,
991 extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200992 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700993 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +0200994
Paul Blakey05cd2712018-04-30 14:28:30 +0300995 err = fl_check_assign_mask(head, fnew, fold, &mask);
Jiri Pirko77b99002015-05-12 14:56:21 +0200996 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700997 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +0200998
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300999 if (!tc_skip_sw(fnew->flags)) {
Paul Blakey05cd2712018-04-30 14:28:30 +03001000 if (!fold && fl_lookup(fnew->mask, &fnew->mkey)) {
Paul Blakeya3308d82017-01-16 10:45:13 +02001001 err = -EEXIST;
Paul Blakey05cd2712018-04-30 14:28:30 +03001002 goto errout_mask;
Paul Blakeya3308d82017-01-16 10:45:13 +02001003 }
1004
Paul Blakey05cd2712018-04-30 14:28:30 +03001005 err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node,
1006 fnew->mask->filter_ht_params);
Amir Vadaie69985c2016-06-05 17:11:18 +03001007 if (err)
Paul Blakey05cd2712018-04-30 14:28:30 +03001008 goto errout_mask;
Amir Vadaie69985c2016-06-05 17:11:18 +03001009 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001010
Hadar Hen Zion79685212016-12-01 14:06:34 +02001011 if (!tc_skip_hw(fnew->flags)) {
Paul Blakey05cd2712018-04-30 14:28:30 +03001012 err = fl_hw_replace_filter(tp, fnew, extack);
Hadar Hen Zion79685212016-12-01 14:06:34 +02001013 if (err)
Paul Blakey05cd2712018-04-30 14:28:30 +03001014 goto errout_mask;
Hadar Hen Zion79685212016-12-01 14:06:34 +02001015 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001016
Or Gerlitz55593962017-02-16 10:31:13 +02001017 if (!tc_in_hw(fnew->flags))
1018 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1019
Amir Vadai5b33f482016-03-08 12:42:29 +02001020 if (fold) {
Jiri Pirko725cbb622016-11-28 15:40:13 +01001021 if (!tc_skip_sw(fold->flags))
Paul Blakey05cd2712018-04-30 14:28:30 +03001022 rhashtable_remove_fast(&fold->mask->ht,
1023 &fold->ht_node,
1024 fold->mask->filter_ht_params);
Hadar Hen Zion79685212016-12-01 14:06:34 +02001025 if (!tc_skip_hw(fold->flags))
Jakub Kicinski1b0f8032018-01-24 12:54:21 -08001026 fl_hw_destroy_filter(tp, fold, NULL);
Amir Vadai5b33f482016-03-08 12:42:29 +02001027 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001028
WANG Cong8113c092017-08-04 21:31:43 -07001029 *arg = fnew;
Jiri Pirko77b99002015-05-12 14:56:21 +02001030
1031 if (fold) {
Matthew Wilcox234a4622017-11-28 09:56:36 -05001032 idr_replace(&head->handle_idr, fnew, fnew->handle);
Daniel Borkmannff3532f2015-07-17 22:38:44 +02001033 list_replace_rcu(&fold->list, &fnew->list);
Jiri Pirko77b99002015-05-12 14:56:21 +02001034 tcf_unbind_filter(tp, &fold->res);
Cong Wang0dadc112017-11-06 13:47:24 -08001035 tcf_exts_get_net(&fold->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -07001036 tcf_queue_work(&fold->rwork, fl_destroy_filter_work);
Jiri Pirko77b99002015-05-12 14:56:21 +02001037 } else {
Paul Blakey05cd2712018-04-30 14:28:30 +03001038 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
Jiri Pirko77b99002015-05-12 14:56:21 +02001039 }
1040
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001041 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +02001042 return 0;
1043
Paul Blakey05cd2712018-04-30 14:28:30 +03001044errout_mask:
1045 fl_mask_put(head, fnew->mask, false);
1046
Cong Wangfe2502e2017-09-20 09:18:45 -07001047errout_idr:
Paul Blakey8258d2d2018-05-30 11:29:15 +03001048 if (!fold)
Matthew Wilcox9c160942017-11-28 09:48:43 -05001049 idr_remove(&head->handle_idr, fnew->handle);
Jiri Pirko77b99002015-05-12 14:56:21 +02001050errout:
WANG Congb9a24bb2016-08-19 12:36:54 -07001051 tcf_exts_destroy(&fnew->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +02001052 kfree(fnew);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001053errout_tb:
1054 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +02001055 return err;
1056}
1057
Alexander Aring571acf22018-01-18 11:20:53 -05001058static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
1059 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001060{
1061 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -07001062 struct cls_fl_filter *f = arg;
Jiri Pirko77b99002015-05-12 14:56:21 +02001063
Jiri Pirko725cbb622016-11-28 15:40:13 +01001064 if (!tc_skip_sw(f->flags))
Paul Blakey05cd2712018-04-30 14:28:30 +03001065 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
1066 f->mask->filter_ht_params);
Jakub Kicinski1b0f8032018-01-24 12:54:21 -08001067 __fl_delete(tp, f, extack);
Paul Blakey05cd2712018-04-30 14:28:30 +03001068 *last = list_empty(&head->masks);
Jiri Pirko77b99002015-05-12 14:56:21 +02001069 return 0;
1070}
1071
1072static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1073{
1074 struct cls_fl_head *head = rtnl_dereference(tp->root);
1075 struct cls_fl_filter *f;
Paul Blakey05cd2712018-04-30 14:28:30 +03001076 struct fl_flow_mask *mask;
Jiri Pirko77b99002015-05-12 14:56:21 +02001077
Paul Blakey05cd2712018-04-30 14:28:30 +03001078 list_for_each_entry_rcu(mask, &head->masks, list) {
1079 list_for_each_entry_rcu(f, &mask->filters, list) {
1080 if (arg->count < arg->skip)
1081 goto skip;
1082 if (arg->fn(tp, f, arg) < 0) {
1083 arg->stop = 1;
1084 break;
1085 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001086skip:
Paul Blakey05cd2712018-04-30 14:28:30 +03001087 arg->count++;
1088 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001089 }
1090}
1091
John Hurley31533cb2018-06-25 14:30:06 -07001092static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1093 void *cb_priv, struct netlink_ext_ack *extack)
1094{
1095 struct cls_fl_head *head = rtnl_dereference(tp->root);
1096 struct tc_cls_flower_offload cls_flower = {};
1097 struct tcf_block *block = tp->chain->block;
1098 struct fl_flow_mask *mask;
1099 struct cls_fl_filter *f;
1100 int err;
1101
1102 list_for_each_entry(mask, &head->masks, list) {
1103 list_for_each_entry(f, &mask->filters, list) {
1104 if (tc_skip_hw(f->flags))
1105 continue;
1106
1107 tc_cls_common_offload_init(&cls_flower.common, tp,
1108 f->flags, extack);
1109 cls_flower.command = add ?
1110 TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY;
1111 cls_flower.cookie = (unsigned long)f;
1112 cls_flower.dissector = &mask->dissector;
1113 cls_flower.mask = &f->mkey;
1114 cls_flower.key = &f->key;
1115 cls_flower.exts = &f->exts;
1116 cls_flower.classid = f->res.classid;
1117
1118 err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
1119 if (err) {
1120 if (add && tc_skip_sw(f->flags))
1121 return err;
1122 continue;
1123 }
1124
1125 tc_cls_offload_cnt_update(block, &f->in_hw_count,
1126 &f->flags, add);
1127 }
1128 }
1129
1130 return 0;
1131}
1132
Jiri Pirko77b99002015-05-12 14:56:21 +02001133static int fl_dump_key_val(struct sk_buff *skb,
1134 void *val, int val_type,
1135 void *mask, int mask_type, int len)
1136{
1137 int err;
1138
1139 if (!memchr_inv(mask, 0, len))
1140 return 0;
1141 err = nla_put(skb, val_type, len, val);
1142 if (err)
1143 return err;
1144 if (mask_type != TCA_FLOWER_UNSPEC) {
1145 err = nla_put(skb, mask_type, len, mask);
1146 if (err)
1147 return err;
1148 }
1149 return 0;
1150}
1151
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001152static int fl_dump_key_mpls(struct sk_buff *skb,
1153 struct flow_dissector_key_mpls *mpls_key,
1154 struct flow_dissector_key_mpls *mpls_mask)
1155{
1156 int err;
1157
1158 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1159 return 0;
1160 if (mpls_mask->mpls_ttl) {
1161 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1162 mpls_key->mpls_ttl);
1163 if (err)
1164 return err;
1165 }
1166 if (mpls_mask->mpls_tc) {
1167 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1168 mpls_key->mpls_tc);
1169 if (err)
1170 return err;
1171 }
1172 if (mpls_mask->mpls_label) {
1173 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1174 mpls_key->mpls_label);
1175 if (err)
1176 return err;
1177 }
1178 if (mpls_mask->mpls_bos) {
1179 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1180 mpls_key->mpls_bos);
1181 if (err)
1182 return err;
1183 }
1184 return 0;
1185}
1186
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001187static int fl_dump_key_ip(struct sk_buff *skb,
1188 struct flow_dissector_key_ip *key,
1189 struct flow_dissector_key_ip *mask)
1190{
1191 if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos,
1192 TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) ||
1193 fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl,
1194 TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl)))
1195 return -1;
1196
1197 return 0;
1198}
1199
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001200static int fl_dump_key_vlan(struct sk_buff *skb,
1201 struct flow_dissector_key_vlan *vlan_key,
1202 struct flow_dissector_key_vlan *vlan_mask)
1203{
1204 int err;
1205
1206 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1207 return 0;
1208 if (vlan_mask->vlan_id) {
1209 err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID,
1210 vlan_key->vlan_id);
1211 if (err)
1212 return err;
1213 }
1214 if (vlan_mask->vlan_priority) {
1215 err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO,
1216 vlan_key->vlan_priority);
1217 if (err)
1218 return err;
1219 }
1220 return 0;
1221}
1222
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001223static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1224 u32 *flower_key, u32 *flower_mask,
1225 u32 flower_flag_bit, u32 dissector_flag_bit)
1226{
1227 if (dissector_mask & dissector_flag_bit) {
1228 *flower_mask |= flower_flag_bit;
1229 if (dissector_key & dissector_flag_bit)
1230 *flower_key |= flower_flag_bit;
1231 }
1232}
1233
1234static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1235{
1236 u32 key, mask;
1237 __be32 _key, _mask;
1238 int err;
1239
1240 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1241 return 0;
1242
1243 key = 0;
1244 mask = 0;
1245
1246 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1247 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +01001248 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1249 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1250 FLOW_DIS_FIRST_FRAG);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001251
1252 _key = cpu_to_be32(key);
1253 _mask = cpu_to_be32(mask);
1254
1255 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1256 if (err)
1257 return err;
1258
1259 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1260}
1261
WANG Cong8113c092017-08-04 21:31:43 -07001262static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jiri Pirko77b99002015-05-12 14:56:21 +02001263 struct sk_buff *skb, struct tcmsg *t)
1264{
WANG Cong8113c092017-08-04 21:31:43 -07001265 struct cls_fl_filter *f = fh;
Jiri Pirko77b99002015-05-12 14:56:21 +02001266 struct nlattr *nest;
1267 struct fl_flow_key *key, *mask;
1268
1269 if (!f)
1270 return skb->len;
1271
1272 t->tcm_handle = f->handle;
1273
1274 nest = nla_nest_start(skb, TCA_OPTIONS);
1275 if (!nest)
1276 goto nla_put_failure;
1277
1278 if (f->res.classid &&
1279 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1280 goto nla_put_failure;
1281
1282 key = &f->key;
Paul Blakey05cd2712018-04-30 14:28:30 +03001283 mask = &f->mask->key;
Jiri Pirko77b99002015-05-12 14:56:21 +02001284
1285 if (mask->indev_ifindex) {
1286 struct net_device *dev;
1287
1288 dev = __dev_get_by_index(net, key->indev_ifindex);
1289 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1290 goto nla_put_failure;
1291 }
1292
Hadar Hen Zion79685212016-12-01 14:06:34 +02001293 if (!tc_skip_hw(f->flags))
1294 fl_hw_update_stats(tp, f);
Amir Vadai10cbc682016-05-13 12:55:37 +00001295
Jiri Pirko77b99002015-05-12 14:56:21 +02001296 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1297 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1298 sizeof(key->eth.dst)) ||
1299 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1300 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1301 sizeof(key->eth.src)) ||
1302 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1303 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1304 sizeof(key->basic.n_proto)))
1305 goto nla_put_failure;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001306
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001307 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1308 goto nla_put_failure;
1309
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001310 if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan))
1311 goto nla_put_failure;
1312
Jiri Pirko77b99002015-05-12 14:56:21 +02001313 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1314 key->basic.n_proto == htons(ETH_P_IPV6)) &&
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001315 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
Jiri Pirko77b99002015-05-12 14:56:21 +02001316 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001317 sizeof(key->basic.ip_proto)) ||
1318 fl_dump_key_ip(skb, &key->ip, &mask->ip)))
Jiri Pirko77b99002015-05-12 14:56:21 +02001319 goto nla_put_failure;
1320
Tom Herbertc3f83242015-06-04 09:16:40 -07001321 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001322 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1323 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1324 sizeof(key->ipv4.src)) ||
1325 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1326 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1327 sizeof(key->ipv4.dst))))
1328 goto nla_put_failure;
Tom Herbertc3f83242015-06-04 09:16:40 -07001329 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001330 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1331 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1332 sizeof(key->ipv6.src)) ||
1333 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1334 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1335 sizeof(key->ipv6.dst))))
1336 goto nla_put_failure;
1337
1338 if (key->basic.ip_proto == IPPROTO_TCP &&
1339 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001340 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001341 sizeof(key->tp.src)) ||
1342 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001343 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001344 sizeof(key->tp.dst)) ||
1345 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1346 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1347 sizeof(key->tcp.flags))))
Jiri Pirko77b99002015-05-12 14:56:21 +02001348 goto nla_put_failure;
1349 else if (key->basic.ip_proto == IPPROTO_UDP &&
1350 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001351 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001352 sizeof(key->tp.src)) ||
1353 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001354 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001355 sizeof(key->tp.dst))))
1356 goto nla_put_failure;
Simon Horman5976c5f2016-11-03 13:24:21 +01001357 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1358 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1359 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1360 sizeof(key->tp.src)) ||
1361 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1362 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1363 sizeof(key->tp.dst))))
1364 goto nla_put_failure;
Simon Horman7b684882016-12-07 13:48:28 +01001365 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1366 key->basic.ip_proto == IPPROTO_ICMP &&
1367 (fl_dump_key_val(skb, &key->icmp.type,
1368 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1369 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1370 sizeof(key->icmp.type)) ||
1371 fl_dump_key_val(skb, &key->icmp.code,
1372 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1373 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1374 sizeof(key->icmp.code))))
1375 goto nla_put_failure;
1376 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1377 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1378 (fl_dump_key_val(skb, &key->icmp.type,
1379 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1380 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1381 sizeof(key->icmp.type)) ||
1382 fl_dump_key_val(skb, &key->icmp.code,
1383 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1384 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1385 sizeof(key->icmp.code))))
1386 goto nla_put_failure;
Simon Horman99d31322017-01-11 14:05:43 +01001387 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1388 key->basic.n_proto == htons(ETH_P_RARP)) &&
1389 (fl_dump_key_val(skb, &key->arp.sip,
1390 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1391 TCA_FLOWER_KEY_ARP_SIP_MASK,
1392 sizeof(key->arp.sip)) ||
1393 fl_dump_key_val(skb, &key->arp.tip,
1394 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1395 TCA_FLOWER_KEY_ARP_TIP_MASK,
1396 sizeof(key->arp.tip)) ||
1397 fl_dump_key_val(skb, &key->arp.op,
1398 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1399 TCA_FLOWER_KEY_ARP_OP_MASK,
1400 sizeof(key->arp.op)) ||
1401 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1402 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1403 sizeof(key->arp.sha)) ||
1404 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1405 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1406 sizeof(key->arp.tha))))
1407 goto nla_put_failure;
Jiri Pirko77b99002015-05-12 14:56:21 +02001408
Amir Vadaibc3103f2016-09-08 16:23:47 +03001409 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1410 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1411 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1412 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1413 sizeof(key->enc_ipv4.src)) ||
1414 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1415 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1416 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1417 sizeof(key->enc_ipv4.dst))))
1418 goto nla_put_failure;
1419 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1420 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1421 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1422 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1423 sizeof(key->enc_ipv6.src)) ||
1424 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1425 TCA_FLOWER_KEY_ENC_IPV6_DST,
1426 &mask->enc_ipv6.dst,
1427 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1428 sizeof(key->enc_ipv6.dst))))
1429 goto nla_put_failure;
1430
1431 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001432 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001433 sizeof(key->enc_key_id)) ||
1434 fl_dump_key_val(skb, &key->enc_tp.src,
1435 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1436 &mask->enc_tp.src,
1437 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1438 sizeof(key->enc_tp.src)) ||
1439 fl_dump_key_val(skb, &key->enc_tp.dst,
1440 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1441 &mask->enc_tp.dst,
1442 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1443 sizeof(key->enc_tp.dst)))
Amir Vadaibc3103f2016-09-08 16:23:47 +03001444 goto nla_put_failure;
1445
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001446 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1447 goto nla_put_failure;
1448
Or Gerlitz749e6722017-02-16 10:31:10 +02001449 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1450 goto nla_put_failure;
Amir Vadaie69985c2016-06-05 17:11:18 +03001451
Jiri Pirko77b99002015-05-12 14:56:21 +02001452 if (tcf_exts_dump(skb, &f->exts))
1453 goto nla_put_failure;
1454
1455 nla_nest_end(skb, nest);
1456
1457 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1458 goto nla_put_failure;
1459
1460 return skb->len;
1461
1462nla_put_failure:
1463 nla_nest_cancel(skb, nest);
1464 return -1;
1465}
1466
Cong Wang07d79fc2017-08-30 14:30:36 -07001467static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
1468{
1469 struct cls_fl_filter *f = fh;
1470
1471 if (f && f->res.classid == classid)
1472 f->res.class = cl;
1473}
1474
Jiri Pirko77b99002015-05-12 14:56:21 +02001475static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1476 .kind = "flower",
1477 .classify = fl_classify,
1478 .init = fl_init,
1479 .destroy = fl_destroy,
1480 .get = fl_get,
1481 .change = fl_change,
1482 .delete = fl_delete,
1483 .walk = fl_walk,
John Hurley31533cb2018-06-25 14:30:06 -07001484 .reoffload = fl_reoffload,
Jiri Pirko77b99002015-05-12 14:56:21 +02001485 .dump = fl_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07001486 .bind_class = fl_bind_class,
Jiri Pirko77b99002015-05-12 14:56:21 +02001487 .owner = THIS_MODULE,
1488};
1489
1490static int __init cls_fl_init(void)
1491{
1492 return register_tcf_proto_ops(&cls_fl_ops);
1493}
1494
1495static void __exit cls_fl_exit(void)
1496{
1497 unregister_tcf_proto_ops(&cls_fl_ops);
1498}
1499
1500module_init(cls_fl_init);
1501module_exit(cls_fl_exit);
1502
1503MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1504MODULE_DESCRIPTION("Flower classifier");
1505MODULE_LICENSE("GPL v2");