blob: bb7aa1e9d281a9c93c5614b9636d02e0ee9eefdb [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
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200829static void fl_init_dissector(struct flow_dissector *dissector,
830 struct fl_flow_key *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +0200831{
832 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
833 size_t cnt = 0;
834
Tom Herbert42aecaa2015-06-04 09:16:39 -0700835 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
Jiri Pirko77b99002015-05-12 14:56:21 +0200836 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200837 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300838 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200839 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300840 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200841 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300842 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200843 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300844 FLOW_DISSECTOR_KEY_PORTS, tp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200845 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300846 FLOW_DISSECTOR_KEY_IP, ip);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200847 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200848 FLOW_DISSECTOR_KEY_TCP, tcp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200849 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Simon Horman7b684882016-12-07 13:48:28 +0100850 FLOW_DISSECTOR_KEY_ICMP, icmp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200851 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Simon Horman99d31322017-01-11 14:05:43 +0100852 FLOW_DISSECTOR_KEY_ARP, arp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200853 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400854 FLOW_DISSECTOR_KEY_MPLS, mpls);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200855 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300856 FLOW_DISSECTOR_KEY_VLAN, vlan);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200857 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Jianbo Liud64efd02018-07-06 05:38:16 +0000858 FLOW_DISSECTOR_KEY_CVLAN, cvlan);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200859 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +0200860 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200861 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +0200862 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200863 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zion519d1052016-11-07 15:14:38 +0200864 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200865 if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
866 FL_KEY_IS_MASKED(mask, enc_ipv6))
Hadar Hen Zion519d1052016-11-07 15:14:38 +0200867 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
868 enc_control);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200869 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200870 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200871 FL_KEY_SET_IF_MASKED(mask, keys, cnt,
Or Gerlitz0e2c17b2018-07-17 19:27:18 +0300872 FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
Jiri Pirko77b99002015-05-12 14:56:21 +0200873
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200874 skb_flow_dissector_init(dissector, keys, cnt);
Paul Blakey05cd2712018-04-30 14:28:30 +0300875}
876
877static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
878 struct fl_flow_mask *mask)
879{
880 struct fl_flow_mask *newmask;
881 int err;
882
883 newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
884 if (!newmask)
885 return ERR_PTR(-ENOMEM);
886
887 fl_mask_copy(newmask, mask);
888
889 err = fl_init_mask_hashtable(newmask);
890 if (err)
891 goto errout_free;
892
Jiri Pirko33fb5cb2018-07-23 09:23:09 +0200893 fl_init_dissector(&newmask->dissector, &newmask->key);
Paul Blakey05cd2712018-04-30 14:28:30 +0300894
895 INIT_LIST_HEAD_RCU(&newmask->filters);
896
897 err = rhashtable_insert_fast(&head->ht, &newmask->ht_node,
898 mask_ht_params);
899 if (err)
900 goto errout_destroy;
901
902 list_add_tail_rcu(&newmask->list, &head->masks);
903
904 return newmask;
905
906errout_destroy:
907 rhashtable_destroy(&newmask->ht);
908errout_free:
909 kfree(newmask);
910
911 return ERR_PTR(err);
Jiri Pirko77b99002015-05-12 14:56:21 +0200912}
913
914static int fl_check_assign_mask(struct cls_fl_head *head,
Paul Blakey05cd2712018-04-30 14:28:30 +0300915 struct cls_fl_filter *fnew,
916 struct cls_fl_filter *fold,
Jiri Pirko77b99002015-05-12 14:56:21 +0200917 struct fl_flow_mask *mask)
918{
Paul Blakey05cd2712018-04-30 14:28:30 +0300919 struct fl_flow_mask *newmask;
Jiri Pirko77b99002015-05-12 14:56:21 +0200920
Paul Blakey05cd2712018-04-30 14:28:30 +0300921 fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params);
922 if (!fnew->mask) {
923 if (fold)
Jiri Pirko77b99002015-05-12 14:56:21 +0200924 return -EINVAL;
Paul Blakey05cd2712018-04-30 14:28:30 +0300925
926 newmask = fl_create_new_mask(head, mask);
927 if (IS_ERR(newmask))
928 return PTR_ERR(newmask);
929
930 fnew->mask = newmask;
Paul Blakeyf6521c52018-06-03 10:06:14 +0300931 } else if (fold && fold->mask != fnew->mask) {
Paul Blakey05cd2712018-04-30 14:28:30 +0300932 return -EINVAL;
Jiri Pirko77b99002015-05-12 14:56:21 +0200933 }
934
Jiri Pirko77b99002015-05-12 14:56:21 +0200935 return 0;
936}
937
938static int fl_set_parms(struct net *net, struct tcf_proto *tp,
939 struct cls_fl_filter *f, struct fl_flow_mask *mask,
940 unsigned long base, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500941 struct nlattr *est, bool ovr,
942 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200943{
Jiri Pirko77b99002015-05-12 14:56:21 +0200944 int err;
945
Alexander Aring50a56192018-01-18 11:20:52 -0500946 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200947 if (err < 0)
948 return err;
949
950 if (tb[TCA_FLOWER_CLASSID]) {
951 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
952 tcf_bind_filter(tp, &f->res, base);
953 }
954
Alexander Aring1057c552018-01-18 11:20:54 -0500955 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200956 if (err)
Jiri Pirko45507522017-08-04 14:29:06 +0200957 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +0200958
959 fl_mask_update_range(mask);
960 fl_set_masked_key(&f->mkey, &f->key, mask);
961
Jiri Pirko77b99002015-05-12 14:56:21 +0200962 return 0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200963}
964
Jiri Pirko77b99002015-05-12 14:56:21 +0200965static int fl_change(struct net *net, struct sk_buff *in_skb,
966 struct tcf_proto *tp, unsigned long base,
967 u32 handle, struct nlattr **tca,
Alexander Aring7306db32018-01-18 11:20:51 -0500968 void **arg, bool ovr, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200969{
970 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700971 struct cls_fl_filter *fold = *arg;
Jiri Pirko77b99002015-05-12 14:56:21 +0200972 struct cls_fl_filter *fnew;
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100973 struct nlattr **tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200974 struct fl_flow_mask mask = {};
975 int err;
976
977 if (!tca[TCA_OPTIONS])
978 return -EINVAL;
979
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100980 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
981 if (!tb)
982 return -ENOBUFS;
983
Johannes Bergfceb6432017-04-12 14:34:07 +0200984 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
985 fl_policy, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +0200986 if (err < 0)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100987 goto errout_tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200988
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100989 if (fold && handle && fold->handle != handle) {
990 err = -EINVAL;
991 goto errout_tb;
992 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200993
994 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100995 if (!fnew) {
996 err = -ENOBUFS;
997 goto errout_tb;
998 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200999
WANG Congb9a24bb2016-08-19 12:36:54 -07001000 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
1001 if (err < 0)
1002 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +02001003
1004 if (!handle) {
Matthew Wilcox85bd0432017-11-28 10:53:03 -05001005 handle = 1;
1006 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1007 INT_MAX, GFP_KERNEL);
1008 } else if (!fold) {
1009 /* user specifies a handle and it doesn't exist */
1010 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
1011 handle, GFP_KERNEL);
Jiri Pirko77b99002015-05-12 14:56:21 +02001012 }
Matthew Wilcox85bd0432017-11-28 10:53:03 -05001013 if (err)
1014 goto errout;
1015 fnew->handle = handle;
Jiri Pirko77b99002015-05-12 14:56:21 +02001016
Amir Vadaie69985c2016-06-05 17:11:18 +03001017 if (tb[TCA_FLOWER_FLAGS]) {
1018 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
1019
1020 if (!tc_flags_valid(fnew->flags)) {
1021 err = -EINVAL;
Cong Wangfe2502e2017-09-20 09:18:45 -07001022 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +03001023 }
1024 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001025
Alexander Aring50a56192018-01-18 11:20:52 -05001026 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr,
1027 extack);
Jiri Pirko77b99002015-05-12 14:56:21 +02001028 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -07001029 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +02001030
Paul Blakey05cd2712018-04-30 14:28:30 +03001031 err = fl_check_assign_mask(head, fnew, fold, &mask);
Jiri Pirko77b99002015-05-12 14:56:21 +02001032 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -07001033 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +02001034
Amir Vadaie8eb36c2016-06-13 12:06:39 +03001035 if (!tc_skip_sw(fnew->flags)) {
Paul Blakey05cd2712018-04-30 14:28:30 +03001036 if (!fold && fl_lookup(fnew->mask, &fnew->mkey)) {
Paul Blakeya3308d82017-01-16 10:45:13 +02001037 err = -EEXIST;
Paul Blakey05cd2712018-04-30 14:28:30 +03001038 goto errout_mask;
Paul Blakeya3308d82017-01-16 10:45:13 +02001039 }
1040
Paul Blakey05cd2712018-04-30 14:28:30 +03001041 err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node,
1042 fnew->mask->filter_ht_params);
Amir Vadaie69985c2016-06-05 17:11:18 +03001043 if (err)
Paul Blakey05cd2712018-04-30 14:28:30 +03001044 goto errout_mask;
Amir Vadaie69985c2016-06-05 17:11:18 +03001045 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001046
Hadar Hen Zion79685212016-12-01 14:06:34 +02001047 if (!tc_skip_hw(fnew->flags)) {
Paul Blakey05cd2712018-04-30 14:28:30 +03001048 err = fl_hw_replace_filter(tp, fnew, extack);
Hadar Hen Zion79685212016-12-01 14:06:34 +02001049 if (err)
Paul Blakey05cd2712018-04-30 14:28:30 +03001050 goto errout_mask;
Hadar Hen Zion79685212016-12-01 14:06:34 +02001051 }
Amir Vadai5b33f482016-03-08 12:42:29 +02001052
Or Gerlitz55593962017-02-16 10:31:13 +02001053 if (!tc_in_hw(fnew->flags))
1054 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
1055
Amir Vadai5b33f482016-03-08 12:42:29 +02001056 if (fold) {
Jiri Pirko725cbb622016-11-28 15:40:13 +01001057 if (!tc_skip_sw(fold->flags))
Paul Blakey05cd2712018-04-30 14:28:30 +03001058 rhashtable_remove_fast(&fold->mask->ht,
1059 &fold->ht_node,
1060 fold->mask->filter_ht_params);
Hadar Hen Zion79685212016-12-01 14:06:34 +02001061 if (!tc_skip_hw(fold->flags))
Jakub Kicinski1b0f8032018-01-24 12:54:21 -08001062 fl_hw_destroy_filter(tp, fold, NULL);
Amir Vadai5b33f482016-03-08 12:42:29 +02001063 }
Jiri Pirko77b99002015-05-12 14:56:21 +02001064
WANG Cong8113c092017-08-04 21:31:43 -07001065 *arg = fnew;
Jiri Pirko77b99002015-05-12 14:56:21 +02001066
1067 if (fold) {
Matthew Wilcox234a4622017-11-28 09:56:36 -05001068 idr_replace(&head->handle_idr, fnew, fnew->handle);
Daniel Borkmannff3532f2015-07-17 22:38:44 +02001069 list_replace_rcu(&fold->list, &fnew->list);
Jiri Pirko77b99002015-05-12 14:56:21 +02001070 tcf_unbind_filter(tp, &fold->res);
Cong Wang0dadc112017-11-06 13:47:24 -08001071 tcf_exts_get_net(&fold->exts);
Cong Wangaaa908f2018-05-23 15:26:53 -07001072 tcf_queue_work(&fold->rwork, fl_destroy_filter_work);
Jiri Pirko77b99002015-05-12 14:56:21 +02001073 } else {
Paul Blakey05cd2712018-04-30 14:28:30 +03001074 list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
Jiri Pirko77b99002015-05-12 14:56:21 +02001075 }
1076
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001077 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +02001078 return 0;
1079
Paul Blakey05cd2712018-04-30 14:28:30 +03001080errout_mask:
1081 fl_mask_put(head, fnew->mask, false);
1082
Cong Wangfe2502e2017-09-20 09:18:45 -07001083errout_idr:
Paul Blakey8258d2d2018-05-30 11:29:15 +03001084 if (!fold)
Matthew Wilcox9c160942017-11-28 09:48:43 -05001085 idr_remove(&head->handle_idr, fnew->handle);
Jiri Pirko77b99002015-05-12 14:56:21 +02001086errout:
WANG Congb9a24bb2016-08-19 12:36:54 -07001087 tcf_exts_destroy(&fnew->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +02001088 kfree(fnew);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001089errout_tb:
1090 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +02001091 return err;
1092}
1093
Alexander Aring571acf22018-01-18 11:20:53 -05001094static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
1095 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +02001096{
1097 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -07001098 struct cls_fl_filter *f = arg;
Jiri Pirko77b99002015-05-12 14:56:21 +02001099
Jiri Pirko725cbb622016-11-28 15:40:13 +01001100 if (!tc_skip_sw(f->flags))
Paul Blakey05cd2712018-04-30 14:28:30 +03001101 rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
1102 f->mask->filter_ht_params);
Jakub Kicinski1b0f8032018-01-24 12:54:21 -08001103 __fl_delete(tp, f, extack);
Paul Blakey05cd2712018-04-30 14:28:30 +03001104 *last = list_empty(&head->masks);
Jiri Pirko77b99002015-05-12 14:56:21 +02001105 return 0;
1106}
1107
1108static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1109{
1110 struct cls_fl_head *head = rtnl_dereference(tp->root);
1111 struct cls_fl_filter *f;
1112
Vlad Buslov01683a12018-07-09 13:29:11 +03001113 arg->count = arg->skip;
1114
1115 while ((f = idr_get_next_ul(&head->handle_idr,
1116 &arg->cookie)) != NULL) {
1117 if (arg->fn(tp, f, arg) < 0) {
1118 arg->stop = 1;
1119 break;
Paul Blakey05cd2712018-04-30 14:28:30 +03001120 }
Vlad Buslov01683a12018-07-09 13:29:11 +03001121 arg->cookie = f->handle + 1;
1122 arg->count++;
Jiri Pirko77b99002015-05-12 14:56:21 +02001123 }
1124}
1125
John Hurley31533cb2018-06-25 14:30:06 -07001126static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
1127 void *cb_priv, struct netlink_ext_ack *extack)
1128{
1129 struct cls_fl_head *head = rtnl_dereference(tp->root);
1130 struct tc_cls_flower_offload cls_flower = {};
1131 struct tcf_block *block = tp->chain->block;
1132 struct fl_flow_mask *mask;
1133 struct cls_fl_filter *f;
1134 int err;
1135
1136 list_for_each_entry(mask, &head->masks, list) {
1137 list_for_each_entry(f, &mask->filters, list) {
1138 if (tc_skip_hw(f->flags))
1139 continue;
1140
1141 tc_cls_common_offload_init(&cls_flower.common, tp,
1142 f->flags, extack);
1143 cls_flower.command = add ?
1144 TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY;
1145 cls_flower.cookie = (unsigned long)f;
1146 cls_flower.dissector = &mask->dissector;
1147 cls_flower.mask = &f->mkey;
1148 cls_flower.key = &f->key;
1149 cls_flower.exts = &f->exts;
1150 cls_flower.classid = f->res.classid;
1151
1152 err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
1153 if (err) {
1154 if (add && tc_skip_sw(f->flags))
1155 return err;
1156 continue;
1157 }
1158
1159 tc_cls_offload_cnt_update(block, &f->in_hw_count,
1160 &f->flags, add);
1161 }
1162 }
1163
1164 return 0;
1165}
1166
Jiri Pirko77b99002015-05-12 14:56:21 +02001167static int fl_dump_key_val(struct sk_buff *skb,
1168 void *val, int val_type,
1169 void *mask, int mask_type, int len)
1170{
1171 int err;
1172
1173 if (!memchr_inv(mask, 0, len))
1174 return 0;
1175 err = nla_put(skb, val_type, len, val);
1176 if (err)
1177 return err;
1178 if (mask_type != TCA_FLOWER_UNSPEC) {
1179 err = nla_put(skb, mask_type, len, mask);
1180 if (err)
1181 return err;
1182 }
1183 return 0;
1184}
1185
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001186static int fl_dump_key_mpls(struct sk_buff *skb,
1187 struct flow_dissector_key_mpls *mpls_key,
1188 struct flow_dissector_key_mpls *mpls_mask)
1189{
1190 int err;
1191
1192 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1193 return 0;
1194 if (mpls_mask->mpls_ttl) {
1195 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1196 mpls_key->mpls_ttl);
1197 if (err)
1198 return err;
1199 }
1200 if (mpls_mask->mpls_tc) {
1201 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1202 mpls_key->mpls_tc);
1203 if (err)
1204 return err;
1205 }
1206 if (mpls_mask->mpls_label) {
1207 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1208 mpls_key->mpls_label);
1209 if (err)
1210 return err;
1211 }
1212 if (mpls_mask->mpls_bos) {
1213 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1214 mpls_key->mpls_bos);
1215 if (err)
1216 return err;
1217 }
1218 return 0;
1219}
1220
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001221static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001222 struct flow_dissector_key_ip *key,
1223 struct flow_dissector_key_ip *mask)
1224{
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001225 int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
1226 int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
1227 int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
1228 int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
1229
1230 if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
1231 fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001232 return -1;
1233
1234 return 0;
1235}
1236
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001237static int fl_dump_key_vlan(struct sk_buff *skb,
Jianbo Liud64efd02018-07-06 05:38:16 +00001238 int vlan_id_key, int vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001239 struct flow_dissector_key_vlan *vlan_key,
1240 struct flow_dissector_key_vlan *vlan_mask)
1241{
1242 int err;
1243
1244 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1245 return 0;
1246 if (vlan_mask->vlan_id) {
Jianbo Liud64efd02018-07-06 05:38:16 +00001247 err = nla_put_u16(skb, vlan_id_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001248 vlan_key->vlan_id);
1249 if (err)
1250 return err;
1251 }
1252 if (vlan_mask->vlan_priority) {
Jianbo Liud64efd02018-07-06 05:38:16 +00001253 err = nla_put_u8(skb, vlan_prio_key,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001254 vlan_key->vlan_priority);
1255 if (err)
1256 return err;
1257 }
1258 return 0;
1259}
1260
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001261static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1262 u32 *flower_key, u32 *flower_mask,
1263 u32 flower_flag_bit, u32 dissector_flag_bit)
1264{
1265 if (dissector_mask & dissector_flag_bit) {
1266 *flower_mask |= flower_flag_bit;
1267 if (dissector_key & dissector_flag_bit)
1268 *flower_key |= flower_flag_bit;
1269 }
1270}
1271
1272static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1273{
1274 u32 key, mask;
1275 __be32 _key, _mask;
1276 int err;
1277
1278 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1279 return 0;
1280
1281 key = 0;
1282 mask = 0;
1283
1284 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1285 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Pieter Jansen van Vuuren459d1532018-03-06 18:11:14 +01001286 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1287 TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
1288 FLOW_DIS_FIRST_FRAG);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001289
1290 _key = cpu_to_be32(key);
1291 _mask = cpu_to_be32(mask);
1292
1293 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1294 if (err)
1295 return err;
1296
1297 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1298}
1299
Jiri Pirkof5749082018-07-23 09:23:08 +02001300static int fl_dump_key(struct sk_buff *skb, struct net *net,
1301 struct fl_flow_key *key, struct fl_flow_key *mask)
Jiri Pirko77b99002015-05-12 14:56:21 +02001302{
Jiri Pirko77b99002015-05-12 14:56:21 +02001303 if (mask->indev_ifindex) {
1304 struct net_device *dev;
1305
1306 dev = __dev_get_by_index(net, key->indev_ifindex);
1307 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1308 goto nla_put_failure;
1309 }
1310
1311 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1312 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1313 sizeof(key->eth.dst)) ||
1314 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1315 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1316 sizeof(key->eth.src)) ||
1317 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1318 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1319 sizeof(key->basic.n_proto)))
1320 goto nla_put_failure;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001321
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001322 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1323 goto nla_put_failure;
1324
Jianbo Liud64efd02018-07-06 05:38:16 +00001325 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
1326 TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001327 goto nla_put_failure;
1328
Jianbo Liud64efd02018-07-06 05:38:16 +00001329 if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
1330 TCA_FLOWER_KEY_CVLAN_PRIO,
1331 &key->cvlan, &mask->cvlan) ||
1332 (mask->cvlan.vlan_tpid &&
1333 nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1334 key->cvlan.vlan_tpid)))
Jianbo Liud3069512018-07-06 05:38:15 +00001335 goto nla_put_failure;
1336
Jianbo Liu5e9a0fe2018-07-09 02:26:20 +00001337 if (mask->basic.n_proto) {
1338 if (mask->cvlan.vlan_tpid) {
1339 if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
1340 key->basic.n_proto))
1341 goto nla_put_failure;
1342 } else if (mask->vlan.vlan_tpid) {
1343 if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
1344 key->basic.n_proto))
1345 goto nla_put_failure;
1346 }
Jianbo Liud64efd02018-07-06 05:38:16 +00001347 }
1348
Jiri Pirko77b99002015-05-12 14:56:21 +02001349 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1350 key->basic.n_proto == htons(ETH_P_IPV6)) &&
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001351 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
Jiri Pirko77b99002015-05-12 14:56:21 +02001352 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001353 sizeof(key->basic.ip_proto)) ||
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001354 fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
Jiri Pirko77b99002015-05-12 14:56:21 +02001355 goto nla_put_failure;
1356
Tom Herbertc3f83242015-06-04 09:16:40 -07001357 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001358 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1359 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1360 sizeof(key->ipv4.src)) ||
1361 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1362 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1363 sizeof(key->ipv4.dst))))
1364 goto nla_put_failure;
Tom Herbertc3f83242015-06-04 09:16:40 -07001365 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001366 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1367 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1368 sizeof(key->ipv6.src)) ||
1369 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1370 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1371 sizeof(key->ipv6.dst))))
1372 goto nla_put_failure;
1373
1374 if (key->basic.ip_proto == IPPROTO_TCP &&
1375 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001376 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001377 sizeof(key->tp.src)) ||
1378 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001379 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001380 sizeof(key->tp.dst)) ||
1381 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1382 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1383 sizeof(key->tcp.flags))))
Jiri Pirko77b99002015-05-12 14:56:21 +02001384 goto nla_put_failure;
1385 else if (key->basic.ip_proto == IPPROTO_UDP &&
1386 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001387 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001388 sizeof(key->tp.src)) ||
1389 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001390 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001391 sizeof(key->tp.dst))))
1392 goto nla_put_failure;
Simon Horman5976c5f2016-11-03 13:24:21 +01001393 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1394 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1395 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1396 sizeof(key->tp.src)) ||
1397 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1398 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1399 sizeof(key->tp.dst))))
1400 goto nla_put_failure;
Simon Horman7b684882016-12-07 13:48:28 +01001401 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1402 key->basic.ip_proto == IPPROTO_ICMP &&
1403 (fl_dump_key_val(skb, &key->icmp.type,
1404 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1405 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1406 sizeof(key->icmp.type)) ||
1407 fl_dump_key_val(skb, &key->icmp.code,
1408 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1409 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1410 sizeof(key->icmp.code))))
1411 goto nla_put_failure;
1412 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1413 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1414 (fl_dump_key_val(skb, &key->icmp.type,
1415 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1416 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1417 sizeof(key->icmp.type)) ||
1418 fl_dump_key_val(skb, &key->icmp.code,
1419 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1420 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1421 sizeof(key->icmp.code))))
1422 goto nla_put_failure;
Simon Horman99d31322017-01-11 14:05:43 +01001423 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1424 key->basic.n_proto == htons(ETH_P_RARP)) &&
1425 (fl_dump_key_val(skb, &key->arp.sip,
1426 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1427 TCA_FLOWER_KEY_ARP_SIP_MASK,
1428 sizeof(key->arp.sip)) ||
1429 fl_dump_key_val(skb, &key->arp.tip,
1430 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1431 TCA_FLOWER_KEY_ARP_TIP_MASK,
1432 sizeof(key->arp.tip)) ||
1433 fl_dump_key_val(skb, &key->arp.op,
1434 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1435 TCA_FLOWER_KEY_ARP_OP_MASK,
1436 sizeof(key->arp.op)) ||
1437 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1438 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1439 sizeof(key->arp.sha)) ||
1440 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1441 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1442 sizeof(key->arp.tha))))
1443 goto nla_put_failure;
Jiri Pirko77b99002015-05-12 14:56:21 +02001444
Amir Vadaibc3103f2016-09-08 16:23:47 +03001445 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1446 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1447 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1448 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1449 sizeof(key->enc_ipv4.src)) ||
1450 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1451 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1452 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1453 sizeof(key->enc_ipv4.dst))))
1454 goto nla_put_failure;
1455 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1456 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1457 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1458 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1459 sizeof(key->enc_ipv6.src)) ||
1460 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1461 TCA_FLOWER_KEY_ENC_IPV6_DST,
1462 &mask->enc_ipv6.dst,
1463 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1464 sizeof(key->enc_ipv6.dst))))
1465 goto nla_put_failure;
1466
1467 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001468 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001469 sizeof(key->enc_key_id)) ||
1470 fl_dump_key_val(skb, &key->enc_tp.src,
1471 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1472 &mask->enc_tp.src,
1473 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1474 sizeof(key->enc_tp.src)) ||
1475 fl_dump_key_val(skb, &key->enc_tp.dst,
1476 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1477 &mask->enc_tp.dst,
1478 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
Or Gerlitz0e2c17b2018-07-17 19:27:18 +03001479 sizeof(key->enc_tp.dst)) ||
1480 fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip))
Amir Vadaibc3103f2016-09-08 16:23:47 +03001481 goto nla_put_failure;
1482
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001483 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1484 goto nla_put_failure;
1485
Jiri Pirkof5749082018-07-23 09:23:08 +02001486 return 0;
1487
1488nla_put_failure:
1489 return -EMSGSIZE;
1490}
1491
1492static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
1493 struct sk_buff *skb, struct tcmsg *t)
1494{
1495 struct cls_fl_filter *f = fh;
1496 struct nlattr *nest;
1497 struct fl_flow_key *key, *mask;
1498
1499 if (!f)
1500 return skb->len;
1501
1502 t->tcm_handle = f->handle;
1503
1504 nest = nla_nest_start(skb, TCA_OPTIONS);
1505 if (!nest)
1506 goto nla_put_failure;
1507
1508 if (f->res.classid &&
1509 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1510 goto nla_put_failure;
1511
1512 key = &f->key;
1513 mask = &f->mask->key;
1514
1515 if (fl_dump_key(skb, net, key, mask))
1516 goto nla_put_failure;
1517
1518 if (!tc_skip_hw(f->flags))
1519 fl_hw_update_stats(tp, f);
1520
Or Gerlitz749e6722017-02-16 10:31:10 +02001521 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1522 goto nla_put_failure;
Amir Vadaie69985c2016-06-05 17:11:18 +03001523
Jiri Pirko77b99002015-05-12 14:56:21 +02001524 if (tcf_exts_dump(skb, &f->exts))
1525 goto nla_put_failure;
1526
1527 nla_nest_end(skb, nest);
1528
1529 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1530 goto nla_put_failure;
1531
1532 return skb->len;
1533
1534nla_put_failure:
1535 nla_nest_cancel(skb, nest);
1536 return -1;
1537}
1538
Cong Wang07d79fc2017-08-30 14:30:36 -07001539static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
1540{
1541 struct cls_fl_filter *f = fh;
1542
1543 if (f && f->res.classid == classid)
1544 f->res.class = cl;
1545}
1546
Jiri Pirko77b99002015-05-12 14:56:21 +02001547static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1548 .kind = "flower",
1549 .classify = fl_classify,
1550 .init = fl_init,
1551 .destroy = fl_destroy,
1552 .get = fl_get,
1553 .change = fl_change,
1554 .delete = fl_delete,
1555 .walk = fl_walk,
John Hurley31533cb2018-06-25 14:30:06 -07001556 .reoffload = fl_reoffload,
Jiri Pirko77b99002015-05-12 14:56:21 +02001557 .dump = fl_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07001558 .bind_class = fl_bind_class,
Jiri Pirko77b99002015-05-12 14:56:21 +02001559 .owner = THIS_MODULE,
1560};
1561
1562static int __init cls_fl_init(void)
1563{
1564 return register_tcf_proto_ops(&cls_fl_ops);
1565}
1566
1567static void __exit cls_fl_exit(void)
1568{
1569 unregister_tcf_proto_ops(&cls_fl_ops);
1570}
1571
1572module_init(cls_fl_init);
1573module_exit(cls_fl_exit);
1574
1575MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1576MODULE_DESCRIPTION("Flower classifier");
1577MODULE_LICENSE("GPL v2");