blob: ab10a7c883592dee8840e9a0ca384df0359a9bee [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;
Jianbo Liud64efd02018-07-06 05:38:16 +000038 struct flow_dissector_key_vlan cvlan;
Jiri Pirko77b99002015-05-12 14:56:21 +020039 union {
Tom Herbertc3f83242015-06-04 09:16:40 -070040 struct flow_dissector_key_ipv4_addrs ipv4;
Jiri Pirko77b99002015-05-12 14:56:21 +020041 struct flow_dissector_key_ipv6_addrs ipv6;
42 };
43 struct flow_dissector_key_ports tp;
Simon Horman7b684882016-12-07 13:48:28 +010044 struct flow_dissector_key_icmp icmp;
Simon Horman99d31322017-01-11 14:05:43 +010045 struct flow_dissector_key_arp arp;
Amir Vadaibc3103f2016-09-08 16:23:47 +030046 struct flow_dissector_key_keyid enc_key_id;
47 union {
48 struct flow_dissector_key_ipv4_addrs enc_ipv4;
49 struct flow_dissector_key_ipv6_addrs enc_ipv6;
50 };
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +020051 struct flow_dissector_key_ports enc_tp;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -040052 struct flow_dissector_key_mpls mpls;
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +020053 struct flow_dissector_key_tcp tcp;
Or Gerlitz4d80cc02017-06-01 21:37:38 +030054 struct flow_dissector_key_ip ip;
Or Gerlitz0e2c17b2018-07-17 19:27:18 +030055 struct flow_dissector_key_ip enc_ip;
Jiri Pirko77b99002015-05-12 14:56:21 +020056} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
57
58struct fl_flow_mask_range {
59 unsigned short int start;
60 unsigned short int end;
61};
62
63struct fl_flow_mask {
64 struct fl_flow_key key;
65 struct fl_flow_mask_range range;
Paul Blakey05cd2712018-04-30 14:28:30 +030066 struct rhash_head ht_node;
67 struct rhashtable ht;
68 struct rhashtable_params filter_ht_params;
69 struct flow_dissector dissector;
70 struct list_head filters;
Paolo Abeni44a5cd42018-06-21 20:02:16 +020071 struct rcu_work rwork;
Paul Blakey05cd2712018-04-30 14:28:30 +030072 struct list_head list;
Jiri Pirko77b99002015-05-12 14:56:21 +020073};
74
75struct cls_fl_head {
76 struct rhashtable ht;
Paul Blakey05cd2712018-04-30 14:28:30 +030077 struct list_head masks;
Cong Wangaaa908f2018-05-23 15:26:53 -070078 struct rcu_work rwork;
Chris Mic15ab232017-08-30 02:31:58 -040079 struct idr handle_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +020080};
81
82struct cls_fl_filter {
Paul Blakey05cd2712018-04-30 14:28:30 +030083 struct fl_flow_mask *mask;
Jiri Pirko77b99002015-05-12 14:56:21 +020084 struct rhash_head ht_node;
85 struct fl_flow_key mkey;
86 struct tcf_exts exts;
87 struct tcf_result res;
88 struct fl_flow_key key;
89 struct list_head list;
90 u32 handle;
Amir Vadaie69985c2016-06-05 17:11:18 +030091 u32 flags;
John Hurley31533cb2018-06-25 14:30:06 -070092 unsigned int in_hw_count;
Cong Wangaaa908f2018-05-23 15:26:53 -070093 struct rcu_work rwork;
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +020094 struct net_device *hw_dev;
Jiri Pirko77b99002015-05-12 14:56:21 +020095};
96
Paul Blakey05cd2712018-04-30 14:28:30 +030097static const struct rhashtable_params mask_ht_params = {
98 .key_offset = offsetof(struct fl_flow_mask, key),
99 .key_len = sizeof(struct fl_flow_key),
100 .head_offset = offsetof(struct fl_flow_mask, ht_node),
101 .automatic_shrinking = true,
102};
103
Jiri Pirko77b99002015-05-12 14:56:21 +0200104static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
105{
106 return mask->range.end - mask->range.start;
107}
108
109static void fl_mask_update_range(struct fl_flow_mask *mask)
110{
111 const u8 *bytes = (const u8 *) &mask->key;
112 size_t size = sizeof(mask->key);
Paul Blakey05cd2712018-04-30 14:28:30 +0300113 size_t i, first = 0, last;
Jiri Pirko77b99002015-05-12 14:56:21 +0200114
Paul Blakey05cd2712018-04-30 14:28:30 +0300115 for (i = 0; i < size; i++) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200116 if (bytes[i]) {
Paul Blakey05cd2712018-04-30 14:28:30 +0300117 first = i;
118 break;
119 }
120 }
121 last = first;
122 for (i = size - 1; i != first; i--) {
123 if (bytes[i]) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200124 last = i;
Paul Blakey05cd2712018-04-30 14:28:30 +0300125 break;
Jiri Pirko77b99002015-05-12 14:56:21 +0200126 }
127 }
128 mask->range.start = rounddown(first, sizeof(long));
129 mask->range.end = roundup(last + 1, sizeof(long));
130}
131
132static void *fl_key_get_start(struct fl_flow_key *key,
133 const struct fl_flow_mask *mask)
134{
135 return (u8 *) key + mask->range.start;
136}
137
138static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
139 struct fl_flow_mask *mask)
140{
141 const long *lkey = fl_key_get_start(key, mask);
142 const long *lmask = fl_key_get_start(&mask->key, mask);
143 long *lmkey = fl_key_get_start(mkey, mask);
144 int i;
145
146 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
147 *lmkey++ = *lkey++ & *lmask++;
148}
149
150static void fl_clear_masked_range(struct fl_flow_key *key,
151 struct fl_flow_mask *mask)
152{
153 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
154}
155
Paul Blakey05cd2712018-04-30 14:28:30 +0300156static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
Paul Blakeya3308d82017-01-16 10:45:13 +0200157 struct fl_flow_key *mkey)
158{
Paul Blakey05cd2712018-04-30 14:28:30 +0300159 return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
160 mask->filter_ht_params);
Paul Blakeya3308d82017-01-16 10:45:13 +0200161}
162
Jiri Pirko77b99002015-05-12 14:56:21 +0200163static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
164 struct tcf_result *res)
165{
166 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
167 struct cls_fl_filter *f;
Paul Blakey05cd2712018-04-30 14:28:30 +0300168 struct fl_flow_mask *mask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200169 struct fl_flow_key skb_key;
170 struct fl_flow_key skb_mkey;
171
Paul Blakey05cd2712018-04-30 14:28:30 +0300172 list_for_each_entry_rcu(mask, &head->masks, list) {
173 fl_clear_masked_range(&skb_key, mask);
Amir Vadaie69985c2016-06-05 17:11:18 +0300174
Paul Blakey05cd2712018-04-30 14:28:30 +0300175 skb_key.indev_ifindex = skb->skb_iif;
176 /* skb_flow_dissect() does not set n_proto in case an unknown
177 * protocol, so do it rather here.
178 */
179 skb_key.basic.n_proto = skb->protocol;
180 skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
181 skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
Amir Vadaibc3103f2016-09-08 16:23:47 +0300182
Paul Blakey05cd2712018-04-30 14:28:30 +0300183 fl_set_masked_key(&skb_mkey, &skb_key, mask);
Jiri Pirko77b99002015-05-12 14:56:21 +0200184
Paul Blakey05cd2712018-04-30 14:28:30 +0300185 f = fl_lookup(mask, &skb_mkey);
186 if (f && !tc_skip_sw(f->flags)) {
187 *res = f->res;
188 return tcf_exts_exec(skb, &f->exts, res);
189 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200190 }
191 return -1;
192}
193
194static int fl_init(struct tcf_proto *tp)
195{
196 struct cls_fl_head *head;
197
198 head = kzalloc(sizeof(*head), GFP_KERNEL);
199 if (!head)
200 return -ENOBUFS;
201
Paul Blakey05cd2712018-04-30 14:28:30 +0300202 INIT_LIST_HEAD_RCU(&head->masks);
Jiri Pirko77b99002015-05-12 14:56:21 +0200203 rcu_assign_pointer(tp->root, head);
Chris Mic15ab232017-08-30 02:31:58 -0400204 idr_init(&head->handle_idr);
Jiri Pirko77b99002015-05-12 14:56:21 +0200205
Paul Blakey05cd2712018-04-30 14:28:30 +0300206 return rhashtable_init(&head->ht, &mask_ht_params);
207}
208
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200209static void fl_mask_free(struct fl_flow_mask *mask)
210{
211 rhashtable_destroy(&mask->ht);
212 kfree(mask);
213}
214
215static void fl_mask_free_work(struct work_struct *work)
216{
217 struct fl_flow_mask *mask = container_of(to_rcu_work(work),
218 struct fl_flow_mask, rwork);
219
220 fl_mask_free(mask);
221}
222
Paul Blakey05cd2712018-04-30 14:28:30 +0300223static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask,
224 bool async)
225{
226 if (!list_empty(&mask->filters))
227 return false;
228
229 rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
Paul Blakey05cd2712018-04-30 14:28:30 +0300230 list_del_rcu(&mask->list);
231 if (async)
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200232 tcf_queue_work(&mask->rwork, fl_mask_free_work);
Paul Blakey05cd2712018-04-30 14:28:30 +0300233 else
Paolo Abeni44a5cd42018-06-21 20:02:16 +0200234 fl_mask_free(mask);
Paul Blakey05cd2712018-04-30 14:28:30 +0300235
236 return true;
Jiri Pirko77b99002015-05-12 14:56:21 +0200237}
238
Cong Wang0dadc112017-11-06 13:47:24 -0800239static void __fl_destroy_filter(struct cls_fl_filter *f)
240{
241 tcf_exts_destroy(&f->exts);
242 tcf_exts_put_net(&f->exts);
243 kfree(f);
244}
245
Cong Wang0552c8a2017-10-26 18:24:33 -0700246static void fl_destroy_filter_work(struct work_struct *work)
247{
Cong Wangaaa908f2018-05-23 15:26:53 -0700248 struct cls_fl_filter *f = container_of(to_rcu_work(work),
249 struct cls_fl_filter, rwork);
Cong Wang0552c8a2017-10-26 18:24:33 -0700250
251 rtnl_lock();
Cong Wang0dadc112017-11-06 13:47:24 -0800252 __fl_destroy_filter(f);
Cong Wang0552c8a2017-10-26 18:24:33 -0700253 rtnl_unlock();
254}
255
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800256static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
257 struct netlink_ext_ack *extack)
Amir Vadai5b33f482016-03-08 12:42:29 +0200258{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200259 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200260 struct tcf_block *block = tp->chain->block;
Amir Vadai5b33f482016-03-08 12:42:29 +0200261
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800262 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200263 cls_flower.command = TC_CLSFLOWER_DESTROY;
264 cls_flower.cookie = (unsigned long) f;
Amir Vadai5b33f482016-03-08 12:42:29 +0200265
Jiri Pirko208c0f42017-10-19 15:50:32 +0200266 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200267 &cls_flower, false);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100268 tcf_block_offload_dec(block, &f->flags);
Amir Vadai5b33f482016-03-08 12:42:29 +0200269}
270
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300271static int fl_hw_replace_filter(struct tcf_proto *tp,
Quentin Monnet41002032018-01-19 17:44:43 -0800272 struct cls_fl_filter *f,
273 struct netlink_ext_ack *extack)
Amir Vadai5b33f482016-03-08 12:42:29 +0200274{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200275 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200276 struct tcf_block *block = tp->chain->block;
Jiri Pirko717503b2017-10-11 09:41:09 +0200277 bool skip_sw = tc_skip_sw(f->flags);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300278 int err;
Amir Vadai5b33f482016-03-08 12:42:29 +0200279
Jakub Kicinskiea205942018-01-24 12:54:20 -0800280 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200281 cls_flower.command = TC_CLSFLOWER_REPLACE;
282 cls_flower.cookie = (unsigned long) f;
Paul Blakey05cd2712018-04-30 14:28:30 +0300283 cls_flower.dissector = &f->mask->dissector;
284 cls_flower.mask = &f->mask->key;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200285 cls_flower.key = &f->mkey;
286 cls_flower.exts = &f->exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700287 cls_flower.classid = f->res.classid;
Amir Vadai5b33f482016-03-08 12:42:29 +0200288
Jiri Pirko208c0f42017-10-19 15:50:32 +0200289 err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200290 &cls_flower, skip_sw);
291 if (err < 0) {
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800292 fl_hw_destroy_filter(tp, f, NULL);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300293 return err;
Jiri Pirko717503b2017-10-11 09:41:09 +0200294 } else if (err > 0) {
John Hurley31533cb2018-06-25 14:30:06 -0700295 f->in_hw_count = err;
Jiri Pirkocaa72602018-01-17 11:46:50 +0100296 tcf_block_offload_inc(block, &f->flags);
Jiri Pirko717503b2017-10-11 09:41:09 +0200297 }
298
299 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
300 return -EINVAL;
301
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300302 return 0;
Amir Vadai5b33f482016-03-08 12:42:29 +0200303}
304
Amir Vadai10cbc682016-05-13 12:55:37 +0000305static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
306{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200307 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200308 struct tcf_block *block = tp->chain->block;
Amir Vadai10cbc682016-05-13 12:55:37 +0000309
Jakub Kicinskiea205942018-01-24 12:54:20 -0800310 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200311 cls_flower.command = TC_CLSFLOWER_STATS;
312 cls_flower.cookie = (unsigned long) f;
313 cls_flower.exts = &f->exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700314 cls_flower.classid = f->res.classid;
Amir Vadai10cbc682016-05-13 12:55:37 +0000315
Jiri Pirko208c0f42017-10-19 15:50:32 +0200316 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200317 &cls_flower, false);
Amir Vadai10cbc682016-05-13 12:55:37 +0000318}
319
Paul Blakey05cd2712018-04-30 14:28:30 +0300320static bool __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800321 struct netlink_ext_ack *extack)
Roi Dayan13fa8762016-11-01 16:08:29 +0200322{
Chris Mic15ab232017-08-30 02:31:58 -0400323 struct cls_fl_head *head = rtnl_dereference(tp->root);
Paul Blakey05cd2712018-04-30 14:28:30 +0300324 bool async = tcf_exts_get_net(&f->exts);
325 bool last;
Chris Mic15ab232017-08-30 02:31:58 -0400326
Matthew Wilcox9c160942017-11-28 09:48:43 -0500327 idr_remove(&head->handle_idr, f->handle);
Roi Dayan13fa8762016-11-01 16:08:29 +0200328 list_del_rcu(&f->list);
Paul Blakey05cd2712018-04-30 14:28:30 +0300329 last = fl_mask_put(head, f->mask, async);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200330 if (!tc_skip_hw(f->flags))
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800331 fl_hw_destroy_filter(tp, f, extack);
Roi Dayan13fa8762016-11-01 16:08:29 +0200332 tcf_unbind_filter(tp, &f->res);
Paul Blakey05cd2712018-04-30 14:28:30 +0300333 if (async)
Cong Wangaaa908f2018-05-23 15:26:53 -0700334 tcf_queue_work(&f->rwork, fl_destroy_filter_work);
Cong Wang0dadc112017-11-06 13:47:24 -0800335 else
336 __fl_destroy_filter(f);
Paul Blakey05cd2712018-04-30 14:28:30 +0300337
338 return last;
Roi Dayan13fa8762016-11-01 16:08:29 +0200339}
340
Daniel Borkmannd9363772016-11-27 01:18:01 +0100341static void fl_destroy_sleepable(struct work_struct *work)
342{
Cong Wangaaa908f2018-05-23 15:26:53 -0700343 struct cls_fl_head *head = container_of(to_rcu_work(work),
344 struct cls_fl_head,
345 rwork);
Paul Blakeyde9dc652018-06-03 10:06:13 +0300346
347 rhashtable_destroy(&head->ht);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100348 kfree(head);
349 module_put(THIS_MODULE);
350}
351
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800352static void fl_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200353{
354 struct cls_fl_head *head = rtnl_dereference(tp->root);
Paul Blakey05cd2712018-04-30 14:28:30 +0300355 struct fl_flow_mask *mask, *next_mask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200356 struct cls_fl_filter *f, *next;
357
Paul Blakey05cd2712018-04-30 14:28:30 +0300358 list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
359 list_for_each_entry_safe(f, next, &mask->filters, list) {
360 if (__fl_delete(tp, f, extack))
361 break;
362 }
363 }
Chris Mic15ab232017-08-30 02:31:58 -0400364 idr_destroy(&head->handle_idr);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100365
366 __module_get(THIS_MODULE);
Cong Wangaaa908f2018-05-23 15:26:53 -0700367 tcf_queue_work(&head->rwork, fl_destroy_sleepable);
Jiri Pirko77b99002015-05-12 14:56:21 +0200368}
369
WANG Cong8113c092017-08-04 21:31:43 -0700370static void *fl_get(struct tcf_proto *tp, u32 handle)
Jiri Pirko77b99002015-05-12 14:56:21 +0200371{
372 struct cls_fl_head *head = rtnl_dereference(tp->root);
Jiri Pirko77b99002015-05-12 14:56:21 +0200373
Matthew Wilcox322d8842017-11-28 10:01:24 -0500374 return idr_find(&head->handle_idr, handle);
Jiri Pirko77b99002015-05-12 14:56:21 +0200375}
376
377static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
378 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
379 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
380 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
381 .len = IFNAMSIZ },
382 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
383 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
384 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
385 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
386 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
387 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
388 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
389 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
390 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
391 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
392 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
393 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
394 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
395 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
396 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
397 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
Jamal Hadi Salimb175c3a2015-06-25 06:55:27 -0400398 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
399 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300400 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
401 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
402 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
Amir Vadaibc3103f2016-09-08 16:23:47 +0300403 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
404 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
405 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
406 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
407 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
408 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
409 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
410 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
411 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
Or Gerlitzaa72d702016-09-15 15:28:22 +0300412 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
413 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
414 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
415 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
Simon Horman5976c5f2016-11-03 13:24:21 +0100416 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
417 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
418 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
419 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200420 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
421 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
422 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
423 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200424 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
425 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
Simon Horman7b684882016-12-07 13:48:28 +0100426 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
427 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
428 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
429 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
430 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
431 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
432 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
433 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
Simon Horman99d31322017-01-11 14:05:43 +0100434 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
435 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
436 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
437 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
438 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
439 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
440 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
441 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
442 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
443 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400444 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
445 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
446 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
447 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200448 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
449 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300450 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
451 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
452 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
453 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
Jianbo Liud64efd02018-07-06 05:38:16 +0000454 [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
455 [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
456 [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300457 [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
458 [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
459 [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
460 [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
Jiri Pirko77b99002015-05-12 14:56:21 +0200461};
462
463static void fl_set_key_val(struct nlattr **tb,
464 void *val, int val_type,
465 void *mask, int mask_type, int len)
466{
467 if (!tb[val_type])
468 return;
469 memcpy(val, nla_data(tb[val_type]), len);
470 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
471 memset(mask, 0xff, len);
472 else
473 memcpy(mask, nla_data(tb[mask_type]), len);
474}
475
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400476static int fl_set_key_mpls(struct nlattr **tb,
477 struct flow_dissector_key_mpls *key_val,
478 struct flow_dissector_key_mpls *key_mask)
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400479{
480 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
481 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
482 key_mask->mpls_ttl = MPLS_TTL_MASK;
483 }
484 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400485 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
486
487 if (bos & ~MPLS_BOS_MASK)
488 return -EINVAL;
489 key_val->mpls_bos = bos;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400490 key_mask->mpls_bos = MPLS_BOS_MASK;
491 }
492 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400493 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
494
495 if (tc & ~MPLS_TC_MASK)
496 return -EINVAL;
497 key_val->mpls_tc = tc;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400498 key_mask->mpls_tc = MPLS_TC_MASK;
499 }
500 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400501 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
502
503 if (label & ~MPLS_LABEL_MASK)
504 return -EINVAL;
505 key_val->mpls_label = label;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400506 key_mask->mpls_label = MPLS_LABEL_MASK;
507 }
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400508 return 0;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400509}
510
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300511static void fl_set_key_vlan(struct nlattr **tb,
Jianbo Liuaaab0832018-07-06 05:38:13 +0000512 __be16 ethertype,
Jianbo Liud64efd02018-07-06 05:38:16 +0000513 int vlan_id_key, int vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300514 struct flow_dissector_key_vlan *key_val,
515 struct flow_dissector_key_vlan *key_mask)
516{
517#define VLAN_PRIORITY_MASK 0x7
518
Jianbo Liud64efd02018-07-06 05:38:16 +0000519 if (tb[vlan_id_key]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300520 key_val->vlan_id =
Jianbo Liud64efd02018-07-06 05:38:16 +0000521 nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300522 key_mask->vlan_id = VLAN_VID_MASK;
523 }
Jianbo Liud64efd02018-07-06 05:38:16 +0000524 if (tb[vlan_prio_key]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300525 key_val->vlan_priority =
Jianbo Liud64efd02018-07-06 05:38:16 +0000526 nla_get_u8(tb[vlan_prio_key]) &
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300527 VLAN_PRIORITY_MASK;
528 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
529 }
Jianbo Liuaaab0832018-07-06 05:38:13 +0000530 key_val->vlan_tpid = ethertype;
531 key_mask->vlan_tpid = cpu_to_be16(~0);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300532}
533
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200534static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
535 u32 *dissector_key, u32 *dissector_mask,
536 u32 flower_flag_bit, u32 dissector_flag_bit)
537{
538 if (flower_mask & flower_flag_bit) {
539 *dissector_mask |= dissector_flag_bit;
540 if (flower_key & flower_flag_bit)
541 *dissector_key |= dissector_flag_bit;
542 }
543}
544
Or Gerlitzd9724772016-12-22 14:28:15 +0200545static int fl_set_key_flags(struct nlattr **tb,
546 u32 *flags_key, u32 *flags_mask)
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200547{
548 u32 key, mask;
549
Or Gerlitzd9724772016-12-22 14:28:15 +0200550 /* mask is mandatory for flags */
551 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
552 return -EINVAL;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200553
554 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
Or Gerlitzd9724772016-12-22 14:28:15 +0200555 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200556
557 *flags_key = 0;
558 *flags_mask = 0;
559
560 fl_set_key_flag(key, mask, flags_key, flags_mask,
561 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +0100562 fl_set_key_flag(key, mask, flags_key, flags_mask,
563 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
564 FLOW_DIS_FIRST_FRAG);
Or Gerlitzd9724772016-12-22 14:28:15 +0200565
566 return 0;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200567}
568
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300569static void fl_set_key_ip(struct nlattr **tb, bool encap,
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300570 struct flow_dissector_key_ip *key,
571 struct flow_dissector_key_ip *mask)
572{
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300573 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
574 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
575 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
576 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300577
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300578 fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
579 fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300580}
581
Jiri Pirko77b99002015-05-12 14:56:21 +0200582static int fl_set_key(struct net *net, struct nlattr **tb,
Alexander Aring1057c552018-01-18 11:20:54 -0500583 struct fl_flow_key *key, struct fl_flow_key *mask,
584 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200585{
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300586 __be16 ethertype;
Or Gerlitzd9724772016-12-22 14:28:15 +0200587 int ret = 0;
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400588#ifdef CONFIG_NET_CLS_IND
Jiri Pirko77b99002015-05-12 14:56:21 +0200589 if (tb[TCA_FLOWER_INDEV]) {
Alexander Aring1057c552018-01-18 11:20:54 -0500590 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200591 if (err < 0)
592 return err;
593 key->indev_ifindex = err;
594 mask->indev_ifindex = 0xffffffff;
595 }
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400596#endif
Jiri Pirko77b99002015-05-12 14:56:21 +0200597
598 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
599 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
600 sizeof(key->eth.dst));
601 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
602 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
603 sizeof(key->eth.src));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500604
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200605 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300606 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
607
Jianbo Liuaaab0832018-07-06 05:38:13 +0000608 if (eth_type_vlan(ethertype)) {
Jianbo Liud64efd02018-07-06 05:38:16 +0000609 fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
610 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan,
611 &mask->vlan);
612
Jianbo Liu5e9a0fe2018-07-09 02:26:20 +0000613 if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
614 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
615 if (eth_type_vlan(ethertype)) {
616 fl_set_key_vlan(tb, ethertype,
617 TCA_FLOWER_KEY_CVLAN_ID,
618 TCA_FLOWER_KEY_CVLAN_PRIO,
619 &key->cvlan, &mask->cvlan);
620 fl_set_key_val(tb, &key->basic.n_proto,
621 TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
622 &mask->basic.n_proto,
623 TCA_FLOWER_UNSPEC,
624 sizeof(key->basic.n_proto));
625 } else {
626 key->basic.n_proto = ethertype;
627 mask->basic.n_proto = cpu_to_be16(~0);
628 }
Jianbo Liud64efd02018-07-06 05:38:16 +0000629 }
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200630 } else {
631 key->basic.n_proto = ethertype;
632 mask->basic.n_proto = cpu_to_be16(~0);
633 }
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300634 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500635
Jiri Pirko77b99002015-05-12 14:56:21 +0200636 if (key->basic.n_proto == htons(ETH_P_IP) ||
637 key->basic.n_proto == htons(ETH_P_IPV6)) {
638 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
639 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
640 sizeof(key->basic.ip_proto));
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300641 fl_set_key_ip(tb, false, &key->ip, &mask->ip);
Jiri Pirko77b99002015-05-12 14:56:21 +0200642 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500643
644 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
645 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200646 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200647 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
648 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
649 sizeof(key->ipv4.src));
650 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
651 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
652 sizeof(key->ipv4.dst));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500653 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
654 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200655 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200656 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
657 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
658 sizeof(key->ipv6.src));
659 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
660 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
661 sizeof(key->ipv6.dst));
662 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500663
Jiri Pirko77b99002015-05-12 14:56:21 +0200664 if (key->basic.ip_proto == IPPROTO_TCP) {
665 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300666 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200667 sizeof(key->tp.src));
668 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300669 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200670 sizeof(key->tp.dst));
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200671 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
672 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
673 sizeof(key->tcp.flags));
Jiri Pirko77b99002015-05-12 14:56:21 +0200674 } else if (key->basic.ip_proto == IPPROTO_UDP) {
675 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300676 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200677 sizeof(key->tp.src));
678 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300679 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200680 sizeof(key->tp.dst));
Simon Horman5976c5f2016-11-03 13:24:21 +0100681 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
682 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
683 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
684 sizeof(key->tp.src));
685 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
686 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
687 sizeof(key->tp.dst));
Simon Horman7b684882016-12-07 13:48:28 +0100688 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
689 key->basic.ip_proto == IPPROTO_ICMP) {
690 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
691 &mask->icmp.type,
692 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
693 sizeof(key->icmp.type));
694 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
695 &mask->icmp.code,
696 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
697 sizeof(key->icmp.code));
698 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
699 key->basic.ip_proto == IPPROTO_ICMPV6) {
700 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
701 &mask->icmp.type,
702 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
703 sizeof(key->icmp.type));
Simon Horman040587a2017-01-30 16:19:02 +0100704 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
Simon Horman7b684882016-12-07 13:48:28 +0100705 &mask->icmp.code,
Simon Horman040587a2017-01-30 16:19:02 +0100706 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
Simon Horman7b684882016-12-07 13:48:28 +0100707 sizeof(key->icmp.code));
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400708 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
709 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400710 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
711 if (ret)
712 return ret;
Simon Horman99d31322017-01-11 14:05:43 +0100713 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
714 key->basic.n_proto == htons(ETH_P_RARP)) {
715 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
716 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
717 sizeof(key->arp.sip));
718 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
719 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
720 sizeof(key->arp.tip));
721 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
722 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
723 sizeof(key->arp.op));
724 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
725 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
726 sizeof(key->arp.sha));
727 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
728 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
729 sizeof(key->arp.tha));
Jiri Pirko77b99002015-05-12 14:56:21 +0200730 }
731
Amir Vadaibc3103f2016-09-08 16:23:47 +0300732 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
733 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
734 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200735 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300736 fl_set_key_val(tb, &key->enc_ipv4.src,
737 TCA_FLOWER_KEY_ENC_IPV4_SRC,
738 &mask->enc_ipv4.src,
739 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
740 sizeof(key->enc_ipv4.src));
741 fl_set_key_val(tb, &key->enc_ipv4.dst,
742 TCA_FLOWER_KEY_ENC_IPV4_DST,
743 &mask->enc_ipv4.dst,
744 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
745 sizeof(key->enc_ipv4.dst));
746 }
747
748 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
749 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
750 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200751 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300752 fl_set_key_val(tb, &key->enc_ipv6.src,
753 TCA_FLOWER_KEY_ENC_IPV6_SRC,
754 &mask->enc_ipv6.src,
755 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
756 sizeof(key->enc_ipv6.src));
757 fl_set_key_val(tb, &key->enc_ipv6.dst,
758 TCA_FLOWER_KEY_ENC_IPV6_DST,
759 &mask->enc_ipv6.dst,
760 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
761 sizeof(key->enc_ipv6.dst));
762 }
763
764 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +0300765 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
Amir Vadaibc3103f2016-09-08 16:23:47 +0300766 sizeof(key->enc_key_id.keyid));
767
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200768 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
769 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
770 sizeof(key->enc_tp.src));
771
772 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
773 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
774 sizeof(key->enc_tp.dst));
775
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300776 fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
777
Or Gerlitzd9724772016-12-22 14:28:15 +0200778 if (tb[TCA_FLOWER_KEY_FLAGS])
779 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200780
Or Gerlitzd9724772016-12-22 14:28:15 +0200781 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +0200782}
783
Paul Blakey05cd2712018-04-30 14:28:30 +0300784static void fl_mask_copy(struct fl_flow_mask *dst,
785 struct fl_flow_mask *src)
Jiri Pirko77b99002015-05-12 14:56:21 +0200786{
Paul Blakey05cd2712018-04-30 14:28:30 +0300787 const void *psrc = fl_key_get_start(&src->key, src);
788 void *pdst = fl_key_get_start(&dst->key, src);
Jiri Pirko77b99002015-05-12 14:56:21 +0200789
Paul Blakey05cd2712018-04-30 14:28:30 +0300790 memcpy(pdst, psrc, fl_mask_range(src));
791 dst->range = src->range;
Jiri Pirko77b99002015-05-12 14:56:21 +0200792}
793
794static const struct rhashtable_params fl_ht_params = {
795 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
796 .head_offset = offsetof(struct cls_fl_filter, ht_node),
797 .automatic_shrinking = true,
798};
799
Paul Blakey05cd2712018-04-30 14:28:30 +0300800static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +0200801{
Paul Blakey05cd2712018-04-30 14:28:30 +0300802 mask->filter_ht_params = fl_ht_params;
803 mask->filter_ht_params.key_len = fl_mask_range(mask);
804 mask->filter_ht_params.key_offset += mask->range.start;
Jiri Pirko77b99002015-05-12 14:56:21 +0200805
Paul Blakey05cd2712018-04-30 14:28:30 +0300806 return rhashtable_init(&mask->ht, &mask->filter_ht_params);
Jiri Pirko77b99002015-05-12 14:56:21 +0200807}
808
809#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
810#define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
Jiri Pirko77b99002015-05-12 14:56:21 +0200811
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300812#define FL_KEY_IS_MASKED(mask, member) \
813 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
814 0, FL_KEY_MEMBER_SIZE(member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200815
816#define FL_KEY_SET(keys, cnt, id, member) \
817 do { \
818 keys[cnt].key_id = id; \
819 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
820 cnt++; \
821 } while(0);
822
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300823#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200824 do { \
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300825 if (FL_KEY_IS_MASKED(mask, member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200826 FL_KEY_SET(keys, cnt, id, member); \
827 } while(0);
828
Paul Blakey05cd2712018-04-30 14:28:30 +0300829static void fl_init_dissector(struct fl_flow_mask *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +0200830{
831 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
832 size_t cnt = 0;
833
Tom Herbert42aecaa2015-06-04 09:16:39 -0700834 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
Jiri Pirko77b99002015-05-12 14:56:21 +0200835 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300836 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
837 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
838 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
839 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
840 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
841 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
842 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
843 FLOW_DISSECTOR_KEY_PORTS, tp);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300844 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300845 FLOW_DISSECTOR_KEY_IP, ip);
846 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200847 FLOW_DISSECTOR_KEY_TCP, tcp);
848 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman7b684882016-12-07 13:48:28 +0100849 FLOW_DISSECTOR_KEY_ICMP, icmp);
850 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman99d31322017-01-11 14:05:43 +0100851 FLOW_DISSECTOR_KEY_ARP, arp);
852 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400853 FLOW_DISSECTOR_KEY_MPLS, mpls);
854 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300855 FLOW_DISSECTOR_KEY_VLAN, vlan);
Hadar Hen Zion519d1052016-11-07 15:14:38 +0200856 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Jianbo Liud64efd02018-07-06 05:38:16 +0000857 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
858 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +0200859 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
860 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
861 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
862 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
863 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
864 if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
865 FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
866 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
867 enc_control);
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200868 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
869 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300870 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
871 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
Jiri Pirko77b99002015-05-12 14:56:21 +0200872
Paul Blakey05cd2712018-04-30 14:28:30 +0300873 skb_flow_dissector_init(&mask->dissector, keys, cnt);
874}
875
876static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
877 struct fl_flow_mask *mask)
878{
879 struct fl_flow_mask *newmask;
880 int err;
881
882 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
883 if (!newmask)
884 return ERR_PTR(-ENOMEM);
885
886 fl_mask_copy(newmask, mask);
887
888 err = fl_init_mask_hashtable(newmask);
889 if (err)
890 goto errout_free;
891
892 fl_init_dissector(newmask);
893
894 INIT_LIST_HEAD_RCU(&newmask->filters);
895
896 err = rhashtable_insert_fast(&head->ht, &newmask->ht_node,
897 mask_ht_params);
898 if (err)
899 goto errout_destroy;
900
901 list_add_tail_rcu(&newmask->list, &head->masks);
902
903 return newmask;
904
905errout_destroy:
906 rhashtable_destroy(&newmask->ht);
907errout_free:
908 kfree(newmask);
909
910 return ERR_PTR(err);
Jiri Pirko77b99002015-05-12 14:56:21 +0200911}
912
913static int fl_check_assign_mask(struct cls_fl_head *head,
Paul Blakey05cd2712018-04-30 14:28:30 +0300914 struct cls_fl_filter *fnew,
915 struct cls_fl_filter *fold,
Jiri Pirko77b99002015-05-12 14:56:21 +0200916 struct fl_flow_mask *mask)
917{
Paul Blakey05cd2712018-04-30 14:28:30 +0300918 struct fl_flow_mask *newmask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200919
Paul Blakey05cd2712018-04-30 14:28:30 +0300920 fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params);
921 if (!fnew->mask) {
922 if (fold)
Jiri Pirko77b99002015-05-12 14:56:21 +0200923 return -EINVAL;
Paul Blakey05cd2712018-04-30 14:28:30 +0300924
925 newmask = fl_create_new_mask(head, mask);
926 if (IS_ERR(newmask))
927 return PTR_ERR(newmask);
928
929 fnew->mask = newmask;
Paul Blakeyf6521c52018-06-03 10:06:14 +0300930 } else if (fold && fold->mask != fnew->mask) {
Paul Blakey05cd2712018-04-30 14:28:30 +0300931 return -EINVAL;
Jiri Pirko77b99002015-05-12 14:56:21 +0200932 }
933
Jiri Pirko77b99002015-05-12 14:56:21 +0200934 return 0;
935}
936
937static int fl_set_parms(struct net *net, struct tcf_proto *tp,
938 struct cls_fl_filter *f, struct fl_flow_mask *mask,
939 unsigned long base, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500940 struct nlattr *est, bool ovr,
941 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200942{
Jiri Pirko77b99002015-05-12 14:56:21 +0200943 int err;
944
Alexander Aring50a56192018-01-18 11:20:52 -0500945 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200946 if (err < 0)
947 return err;
948
949 if (tb[TCA_FLOWER_CLASSID]) {
950 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
951 tcf_bind_filter(tp, &f->res, base);
952 }
953
Alexander Aring1057c552018-01-18 11:20:54 -0500954 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200955 if (err)
Jiri Pirko45507522017-08-04 14:29:06 +0200956 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +0200957
958 fl_mask_update_range(mask);
959 fl_set_masked_key(&f->mkey, &f->key, mask);
960
Jiri Pirko77b99002015-05-12 14:56:21 +0200961 return 0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200962}
963
Jiri Pirko77b99002015-05-12 14:56:21 +0200964static int fl_change(struct net *net, struct sk_buff *in_skb,
965 struct tcf_proto *tp, unsigned long base,
966 u32 handle, struct nlattr **tca,
Alexander Aring7306db32018-01-18 11:20:51 -0500967 void **arg, bool ovr, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200968{
969 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700970 struct cls_fl_filter *fold = *arg;
Jiri Pirko77b99002015-05-12 14:56:21 +0200971 struct cls_fl_filter *fnew;
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100972 struct nlattr **tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200973 struct fl_flow_mask mask = {};
974 int err;
975
976 if (!tca[TCA_OPTIONS])
977 return -EINVAL;
978
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100979 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
980 if (!tb)
981 return -ENOBUFS;
982
Johannes Bergfceb6432017-04-12 14:34:07 +0200983 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
984 fl_policy, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +0200985 if (err < 0)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100986 goto errout_tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200987
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100988 if (fold && handle && fold->handle != handle) {
989 err = -EINVAL;
990 goto errout_tb;
991 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200992
993 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100994 if (!fnew) {
995 err = -ENOBUFS;
996 goto errout_tb;
997 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200998
WANG Congb9a24bb2016-08-19 12:36:54 -0700999 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
1000 if (err < 0)
1001 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +02001002
1003 if (!handle) {
Matthew Wilcox85bd0432017-11-28 10:53:03 -05001004 handle = 1;
1005 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1006 INT_MAX, GFP_KERNEL);
1007 } else if (!fold) {
1008 /* user specifies a handle and it doesn't exist */
1009 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1010 handle, GFP_KERNEL);
Jiri Pirko77b99002015-05-12 14:56:21 +02001011 }
Matthew Wilcox85bd0432017-11-28 10:53:03 -05001012 if (err)
1013 goto errout;
1014 fnew->handle = handle;
Jiri Pirko77b99002015-05-12 14:56:21 +02001015
Amir Vadaie69985c2016-06-05 17:11:18 +03001016 if (tb[TCA_FLOWER_FLAGS]) {
1017 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
1018
1019 if (!tc_flags_valid(fnew->flags)) {
1020 err = -EINVAL;
Cong Wangfe2502e2017-09-20 09:18:45 -07001021 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +03001022 }
1023 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001024
Alexander Aring50a56192018-01-18 11:20:52 -05001025 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr,
1026 extack);
Jiri Pirko77b99002015-05-12 14:56:21 +02001027 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -07001028 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +02001029
Paul Blakey05cd2712018-04-30 14:28:30 +03001030 err = fl_check_assign_mask(head, fnew, fold, &mask);
Jiri Pirko77b99002015-05-12 14:56:21 +02001031 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -07001032 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +02001033
Amir Vadaie8eb36c2016-06-13 12:06:39 +03001034 if (!tc_skip_sw(fnew->flags)) {
Paul Blakey05cd2712018-04-30 14:28:30 +03001035 if (!fold && fl_lookup(fnew->mask, &fnew->mkey)) {
Paul Blakeya3308d82017-01-16 10:45:13 +02001036 err = -EEXIST;
Paul Blakey05cd2712018-04-30 14:28:30 +03001037 goto errout_mask;
Paul Blakeya3308d82017-01-16 10:45:13 +02001038 }
1039
Paul Blakey05cd2712018-04-30 14:28:30 +03001040 err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node,
1041 fnew->mask->filter_ht_params);
Amir Vadaie69985c2016-06-05 17:11:18 +03001042 if (err)
Paul Blakey05cd2712018-04-30 14:28:30 +03001043 goto errout_mask;
Amir Vadaie69985c2016-06-05 17:11:18 +03001044 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001045
Hadar Hen Zion79685212016-12-01 14:06:34 +02001046 if (!tc_skip_hw(fnew->flags)) {
Paul Blakey05cd2712018-04-30 14:28:30 +03001047 err = fl_hw_replace_filter(tp, fnew, extack);
Hadar Hen Zion79685212016-12-01 14:06:34 +02001048 if (err)
Paul Blakey05cd2712018-04-30 14:28:30 +03001049 goto errout_mask;
Hadar Hen Zion79685212016-12-01 14:06:34 +02001050 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001051
Or Gerlitz55593962017-02-16 10:31:13 +02001052 if (!tc_in_hw(fnew->flags))
1053 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1054
Amir Vadai5b33f482016-03-08 12:42:29 +02001055 if (fold) {
Jiri Pirko725cbb622016-11-28 15:40:13 +01001056 if (!tc_skip_sw(fold->flags))
Paul Blakey05cd2712018-04-30 14:28:30 +03001057 rhashtable_remove_fast(&fold->mask->ht,
1058 &fold->ht_node,
1059 fold->mask->filter_ht_params);
Hadar Hen Zion79685212016-12-01 14:06:34 +02001060 if (!tc_skip_hw(fold->flags))
Jakub Kicinski1b0f8032018-01-24 12:54:21 -08001061 fl_hw_destroy_filter(tp, fold, NULL);
Amir Vadai5b33f482016-03-08 12:42:29 +02001062 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001063
WANG Cong8113c092017-08-04 21:31:43 -07001064 *arg = fnew;
Jiri Pirko77b99002015-05-12 14:56:21 +02001065
1066 if (fold) {
Matthew Wilcox234a4622017-11-28 09:56:36 -05001067 idr_replace(&head->handle_idr, fnew, fnew->handle);
Daniel Borkmannff3532f2015-07-17 22:38:44 +02001068 list_replace_rcu(&fold->list, &fnew->list);
Jiri Pirko77b99002015-05-12 14:56:21 +02001069 tcf_unbind_filter(tp, &fold->res);
Cong Wang0dadc112017-11-06 13:47:24 -08001070 tcf_exts_get_net(&fold->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -07001071 tcf_queue_work(&fold->rwork, fl_destroy_filter_work);
Jiri Pirko77b99002015-05-12 14:56:21 +02001072 } else {
Paul Blakey05cd2712018-04-30 14:28:30 +03001073 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
Jiri Pirko77b99002015-05-12 14:56:21 +02001074 }
1075
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001076 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +02001077 return 0;
1078
Paul Blakey05cd2712018-04-30 14:28:30 +03001079errout_mask:
1080 fl_mask_put(head, fnew->mask, false);
1081
Cong Wangfe2502e2017-09-20 09:18:45 -07001082errout_idr:
Paul Blakey8258d2d2018-05-30 11:29:15 +03001083 if (!fold)
Matthew Wilcox9c160942017-11-28 09:48:43 -05001084 idr_remove(&head->handle_idr, fnew->handle);
Jiri Pirko77b99002015-05-12 14:56:21 +02001085errout:
WANG Congb9a24bb2016-08-19 12:36:54 -07001086 tcf_exts_destroy(&fnew->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +02001087 kfree(fnew);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001088errout_tb:
1089 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +02001090 return err;
1091}
1092
Alexander Aring571acf22018-01-18 11:20:53 -05001093static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
1094 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001095{
1096 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -07001097 struct cls_fl_filter *f = arg;
Jiri Pirko77b99002015-05-12 14:56:21 +02001098
Jiri Pirko725cbb622016-11-28 15:40:13 +01001099 if (!tc_skip_sw(f->flags))
Paul Blakey05cd2712018-04-30 14:28:30 +03001100 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
1101 f->mask->filter_ht_params);
Jakub Kicinski1b0f8032018-01-24 12:54:21 -08001102 __fl_delete(tp, f, extack);
Paul Blakey05cd2712018-04-30 14:28:30 +03001103 *last = list_empty(&head->masks);
Jiri Pirko77b99002015-05-12 14:56:21 +02001104 return 0;
1105}
1106
1107static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1108{
1109 struct cls_fl_head *head = rtnl_dereference(tp->root);
1110 struct cls_fl_filter *f;
1111
Vlad Buslov01683a12018-07-09 13:29:11 +03001112 arg->count = arg->skip;
1113
1114 while ((f = idr_get_next_ul(&head->handle_idr,
1115 &arg->cookie)) != NULL) {
1116 if (arg->fn(tp, f, arg) < 0) {
1117 arg->stop = 1;
1118 break;
Paul Blakey05cd2712018-04-30 14:28:30 +03001119 }
Vlad Buslov01683a12018-07-09 13:29:11 +03001120 arg->cookie = f->handle + 1;
1121 arg->count++;
Jiri Pirko77b99002015-05-12 14:56:21 +02001122 }
1123}
1124
John Hurley31533cb2018-06-25 14:30:06 -07001125static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1126 void *cb_priv, struct netlink_ext_ack *extack)
1127{
1128 struct cls_fl_head *head = rtnl_dereference(tp->root);
1129 struct tc_cls_flower_offload cls_flower = {};
1130 struct tcf_block *block = tp->chain->block;
1131 struct fl_flow_mask *mask;
1132 struct cls_fl_filter *f;
1133 int err;
1134
1135 list_for_each_entry(mask, &head->masks, list) {
1136 list_for_each_entry(f, &mask->filters, list) {
1137 if (tc_skip_hw(f->flags))
1138 continue;
1139
1140 tc_cls_common_offload_init(&cls_flower.common, tp,
1141 f->flags, extack);
1142 cls_flower.command = add ?
1143 TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY;
1144 cls_flower.cookie = (unsigned long)f;
1145 cls_flower.dissector = &mask->dissector;
1146 cls_flower.mask = &f->mkey;
1147 cls_flower.key = &f->key;
1148 cls_flower.exts = &f->exts;
1149 cls_flower.classid = f->res.classid;
1150
1151 err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
1152 if (err) {
1153 if (add && tc_skip_sw(f->flags))
1154 return err;
1155 continue;
1156 }
1157
1158 tc_cls_offload_cnt_update(block, &f->in_hw_count,
1159 &f->flags, add);
1160 }
1161 }
1162
1163 return 0;
1164}
1165
Jiri Pirko77b99002015-05-12 14:56:21 +02001166static int fl_dump_key_val(struct sk_buff *skb,
1167 void *val, int val_type,
1168 void *mask, int mask_type, int len)
1169{
1170 int err;
1171
1172 if (!memchr_inv(mask, 0, len))
1173 return 0;
1174 err = nla_put(skb, val_type, len, val);
1175 if (err)
1176 return err;
1177 if (mask_type != TCA_FLOWER_UNSPEC) {
1178 err = nla_put(skb, mask_type, len, mask);
1179 if (err)
1180 return err;
1181 }
1182 return 0;
1183}
1184
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001185static int fl_dump_key_mpls(struct sk_buff *skb,
1186 struct flow_dissector_key_mpls *mpls_key,
1187 struct flow_dissector_key_mpls *mpls_mask)
1188{
1189 int err;
1190
1191 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1192 return 0;
1193 if (mpls_mask->mpls_ttl) {
1194 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1195 mpls_key->mpls_ttl);
1196 if (err)
1197 return err;
1198 }
1199 if (mpls_mask->mpls_tc) {
1200 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1201 mpls_key->mpls_tc);
1202 if (err)
1203 return err;
1204 }
1205 if (mpls_mask->mpls_label) {
1206 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1207 mpls_key->mpls_label);
1208 if (err)
1209 return err;
1210 }
1211 if (mpls_mask->mpls_bos) {
1212 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1213 mpls_key->mpls_bos);
1214 if (err)
1215 return err;
1216 }
1217 return 0;
1218}
1219
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001220static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001221 struct flow_dissector_key_ip *key,
1222 struct flow_dissector_key_ip *mask)
1223{
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001224 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1225 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1226 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1227 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
1228
1229 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
1230 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001231 return -1;
1232
1233 return 0;
1234}
1235
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001236static int fl_dump_key_vlan(struct sk_buff *skb,
Jianbo Liud64efd02018-07-06 05:38:16 +00001237 int vlan_id_key, int vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001238 struct flow_dissector_key_vlan *vlan_key,
1239 struct flow_dissector_key_vlan *vlan_mask)
1240{
1241 int err;
1242
1243 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1244 return 0;
1245 if (vlan_mask->vlan_id) {
Jianbo Liud64efd02018-07-06 05:38:16 +00001246 err = nla_put_u16(skb, vlan_id_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001247 vlan_key->vlan_id);
1248 if (err)
1249 return err;
1250 }
1251 if (vlan_mask->vlan_priority) {
Jianbo Liud64efd02018-07-06 05:38:16 +00001252 err = nla_put_u8(skb, vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001253 vlan_key->vlan_priority);
1254 if (err)
1255 return err;
1256 }
1257 return 0;
1258}
1259
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001260static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1261 u32 *flower_key, u32 *flower_mask,
1262 u32 flower_flag_bit, u32 dissector_flag_bit)
1263{
1264 if (dissector_mask & dissector_flag_bit) {
1265 *flower_mask |= flower_flag_bit;
1266 if (dissector_key & dissector_flag_bit)
1267 *flower_key |= flower_flag_bit;
1268 }
1269}
1270
1271static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1272{
1273 u32 key, mask;
1274 __be32 _key, _mask;
1275 int err;
1276
1277 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1278 return 0;
1279
1280 key = 0;
1281 mask = 0;
1282
1283 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1284 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +01001285 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1286 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1287 FLOW_DIS_FIRST_FRAG);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001288
1289 _key = cpu_to_be32(key);
1290 _mask = cpu_to_be32(mask);
1291
1292 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1293 if (err)
1294 return err;
1295
1296 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1297}
1298
Jiri Pirkof5749082018-07-23 09:23:08 +02001299static int fl_dump_key(struct sk_buff *skb, struct net *net,
1300 struct fl_flow_key *key, struct fl_flow_key *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +02001301{
Jiri Pirko77b99002015-05-12 14:56:21 +02001302 if (mask->indev_ifindex) {
1303 struct net_device *dev;
1304
1305 dev = __dev_get_by_index(net, key->indev_ifindex);
1306 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1307 goto nla_put_failure;
1308 }
1309
1310 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1311 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1312 sizeof(key->eth.dst)) ||
1313 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1314 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1315 sizeof(key->eth.src)) ||
1316 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1317 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1318 sizeof(key->basic.n_proto)))
1319 goto nla_put_failure;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001320
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001321 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1322 goto nla_put_failure;
1323
Jianbo Liud64efd02018-07-06 05:38:16 +00001324 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
1325 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001326 goto nla_put_failure;
1327
Jianbo Liud64efd02018-07-06 05:38:16 +00001328 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
1329 TCA_FLOWER_KEY_CVLAN_PRIO,
1330 &key->cvlan, &mask->cvlan) ||
1331 (mask->cvlan.vlan_tpid &&
1332 nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1333 key->cvlan.vlan_tpid)))
Jianbo Liud3069512018-07-06 05:38:15 +00001334 goto nla_put_failure;
1335
Jianbo Liu5e9a0fe2018-07-09 02:26:20 +00001336 if (mask->basic.n_proto) {
1337 if (mask->cvlan.vlan_tpid) {
1338 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1339 key->basic.n_proto))
1340 goto nla_put_failure;
1341 } else if (mask->vlan.vlan_tpid) {
1342 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1343 key->basic.n_proto))
1344 goto nla_put_failure;
1345 }
Jianbo Liud64efd02018-07-06 05:38:16 +00001346 }
1347
Jiri Pirko77b99002015-05-12 14:56:21 +02001348 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1349 key->basic.n_proto == htons(ETH_P_IPV6)) &&
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001350 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
Jiri Pirko77b99002015-05-12 14:56:21 +02001351 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001352 sizeof(key->basic.ip_proto)) ||
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001353 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
Jiri Pirko77b99002015-05-12 14:56:21 +02001354 goto nla_put_failure;
1355
Tom Herbertc3f83242015-06-04 09:16:40 -07001356 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001357 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1358 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1359 sizeof(key->ipv4.src)) ||
1360 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1361 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1362 sizeof(key->ipv4.dst))))
1363 goto nla_put_failure;
Tom Herbertc3f83242015-06-04 09:16:40 -07001364 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001365 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1366 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1367 sizeof(key->ipv6.src)) ||
1368 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1369 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1370 sizeof(key->ipv6.dst))))
1371 goto nla_put_failure;
1372
1373 if (key->basic.ip_proto == IPPROTO_TCP &&
1374 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001375 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001376 sizeof(key->tp.src)) ||
1377 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001378 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001379 sizeof(key->tp.dst)) ||
1380 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1381 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1382 sizeof(key->tcp.flags))))
Jiri Pirko77b99002015-05-12 14:56:21 +02001383 goto nla_put_failure;
1384 else if (key->basic.ip_proto == IPPROTO_UDP &&
1385 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001386 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001387 sizeof(key->tp.src)) ||
1388 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001389 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001390 sizeof(key->tp.dst))))
1391 goto nla_put_failure;
Simon Horman5976c5f2016-11-03 13:24:21 +01001392 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1393 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1394 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1395 sizeof(key->tp.src)) ||
1396 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1397 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1398 sizeof(key->tp.dst))))
1399 goto nla_put_failure;
Simon Horman7b684882016-12-07 13:48:28 +01001400 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1401 key->basic.ip_proto == IPPROTO_ICMP &&
1402 (fl_dump_key_val(skb, &key->icmp.type,
1403 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1404 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1405 sizeof(key->icmp.type)) ||
1406 fl_dump_key_val(skb, &key->icmp.code,
1407 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1408 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1409 sizeof(key->icmp.code))))
1410 goto nla_put_failure;
1411 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1412 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1413 (fl_dump_key_val(skb, &key->icmp.type,
1414 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1415 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1416 sizeof(key->icmp.type)) ||
1417 fl_dump_key_val(skb, &key->icmp.code,
1418 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1419 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1420 sizeof(key->icmp.code))))
1421 goto nla_put_failure;
Simon Horman99d31322017-01-11 14:05:43 +01001422 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1423 key->basic.n_proto == htons(ETH_P_RARP)) &&
1424 (fl_dump_key_val(skb, &key->arp.sip,
1425 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1426 TCA_FLOWER_KEY_ARP_SIP_MASK,
1427 sizeof(key->arp.sip)) ||
1428 fl_dump_key_val(skb, &key->arp.tip,
1429 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1430 TCA_FLOWER_KEY_ARP_TIP_MASK,
1431 sizeof(key->arp.tip)) ||
1432 fl_dump_key_val(skb, &key->arp.op,
1433 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1434 TCA_FLOWER_KEY_ARP_OP_MASK,
1435 sizeof(key->arp.op)) ||
1436 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1437 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1438 sizeof(key->arp.sha)) ||
1439 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1440 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1441 sizeof(key->arp.tha))))
1442 goto nla_put_failure;
Jiri Pirko77b99002015-05-12 14:56:21 +02001443
Amir Vadaibc3103f2016-09-08 16:23:47 +03001444 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1445 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1446 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1447 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1448 sizeof(key->enc_ipv4.src)) ||
1449 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1450 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1451 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1452 sizeof(key->enc_ipv4.dst))))
1453 goto nla_put_failure;
1454 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1455 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1456 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1457 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1458 sizeof(key->enc_ipv6.src)) ||
1459 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1460 TCA_FLOWER_KEY_ENC_IPV6_DST,
1461 &mask->enc_ipv6.dst,
1462 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1463 sizeof(key->enc_ipv6.dst))))
1464 goto nla_put_failure;
1465
1466 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001467 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001468 sizeof(key->enc_key_id)) ||
1469 fl_dump_key_val(skb, &key->enc_tp.src,
1470 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1471 &mask->enc_tp.src,
1472 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1473 sizeof(key->enc_tp.src)) ||
1474 fl_dump_key_val(skb, &key->enc_tp.dst,
1475 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1476 &mask->enc_tp.dst,
1477 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001478 sizeof(key->enc_tp.dst)) ||
1479 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip))
Amir Vadaibc3103f2016-09-08 16:23:47 +03001480 goto nla_put_failure;
1481
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001482 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1483 goto nla_put_failure;
1484
Jiri Pirkof5749082018-07-23 09:23:08 +02001485 return 0;
1486
1487nla_put_failure:
1488 return -EMSGSIZE;
1489}
1490
1491static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
1492 struct sk_buff *skb, struct tcmsg *t)
1493{
1494 struct cls_fl_filter *f = fh;
1495 struct nlattr *nest;
1496 struct fl_flow_key *key, *mask;
1497
1498 if (!f)
1499 return skb->len;
1500
1501 t->tcm_handle = f->handle;
1502
1503 nest = nla_nest_start(skb, TCA_OPTIONS);
1504 if (!nest)
1505 goto nla_put_failure;
1506
1507 if (f->res.classid &&
1508 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1509 goto nla_put_failure;
1510
1511 key = &f->key;
1512 mask = &f->mask->key;
1513
1514 if (fl_dump_key(skb, net, key, mask))
1515 goto nla_put_failure;
1516
1517 if (!tc_skip_hw(f->flags))
1518 fl_hw_update_stats(tp, f);
1519
Or Gerlitz749e6722017-02-16 10:31:10 +02001520 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1521 goto nla_put_failure;
Amir Vadaie69985c2016-06-05 17:11:18 +03001522
Jiri Pirko77b99002015-05-12 14:56:21 +02001523 if (tcf_exts_dump(skb, &f->exts))
1524 goto nla_put_failure;
1525
1526 nla_nest_end(skb, nest);
1527
1528 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1529 goto nla_put_failure;
1530
1531 return skb->len;
1532
1533nla_put_failure:
1534 nla_nest_cancel(skb, nest);
1535 return -1;
1536}
1537
Cong Wang07d79fc2017-08-30 14:30:36 -07001538static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
1539{
1540 struct cls_fl_filter *f = fh;
1541
1542 if (f && f->res.classid == classid)
1543 f->res.class = cl;
1544}
1545
Jiri Pirko77b99002015-05-12 14:56:21 +02001546static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1547 .kind = "flower",
1548 .classify = fl_classify,
1549 .init = fl_init,
1550 .destroy = fl_destroy,
1551 .get = fl_get,
1552 .change = fl_change,
1553 .delete = fl_delete,
1554 .walk = fl_walk,
John Hurley31533cb2018-06-25 14:30:06 -07001555 .reoffload = fl_reoffload,
Jiri Pirko77b99002015-05-12 14:56:21 +02001556 .dump = fl_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07001557 .bind_class = fl_bind_class,
Jiri Pirko77b99002015-05-12 14:56:21 +02001558 .owner = THIS_MODULE,
1559};
1560
1561static int __init cls_fl_init(void)
1562{
1563 return register_tcf_proto_ops(&cls_fl_ops);
1564}
1565
1566static void __exit cls_fl_exit(void)
1567{
1568 unregister_tcf_proto_ops(&cls_fl_ops);
1569}
1570
1571module_init(cls_fl_init);
1572module_exit(cls_fl_exit);
1573
1574MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1575MODULE_DESCRIPTION("Flower classifier");
1576MODULE_LICENSE("GPL v2");