blob: 7d0ce2c40f9333aed8ef6bb4270fa8a8056330a9 [file] [log] [blame]
Jiri Pirko77b99002015-05-12 14:56:21 +02001/*
2 * net/sched/cls_flower.c Flower classifier
3 *
4 * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/rhashtable.h>
Daniel Borkmannd9363772016-11-27 01:18:01 +010016#include <linux/workqueue.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020017
18#include <linux/if_ether.h>
19#include <linux/in6.h>
20#include <linux/ip.h>
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -040021#include <linux/mpls.h>
Jiri Pirko77b99002015-05-12 14:56:21 +020022
23#include <net/sch_generic.h>
24#include <net/pkt_cls.h>
25#include <net/ip.h>
26#include <net/flow_dissector.h>
27
Amir Vadaibc3103f2016-09-08 16:23:47 +030028#include <net/dst.h>
29#include <net/dst_metadata.h>
30
Jiri Pirko77b99002015-05-12 14:56:21 +020031struct fl_flow_key {
32 int indev_ifindex;
Tom Herbert42aecaa2015-06-04 09:16:39 -070033 struct flow_dissector_key_control control;
Amir Vadaibc3103f2016-09-08 16:23:47 +030034 struct flow_dissector_key_control enc_control;
Jiri Pirko77b99002015-05-12 14:56:21 +020035 struct flow_dissector_key_basic basic;
36 struct flow_dissector_key_eth_addrs eth;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +030037 struct flow_dissector_key_vlan vlan;
Jiri Pirko77b99002015-05-12 14:56:21 +020038 union {
Tom Herbertc3f83242015-06-04 09:16:40 -070039 struct flow_dissector_key_ipv4_addrs ipv4;
Jiri Pirko77b99002015-05-12 14:56:21 +020040 struct flow_dissector_key_ipv6_addrs ipv6;
41 };
42 struct flow_dissector_key_ports tp;
Simon Horman7b684882016-12-07 13:48:28 +010043 struct flow_dissector_key_icmp icmp;
Simon Horman99d31322017-01-11 14:05:43 +010044 struct flow_dissector_key_arp arp;
Amir Vadaibc3103f2016-09-08 16:23:47 +030045 struct flow_dissector_key_keyid enc_key_id;
46 union {
47 struct flow_dissector_key_ipv4_addrs enc_ipv4;
48 struct flow_dissector_key_ipv6_addrs enc_ipv6;
49 };
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +020050 struct flow_dissector_key_ports enc_tp;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -040051 struct flow_dissector_key_mpls mpls;
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +020052 struct flow_dissector_key_tcp tcp;
Or Gerlitz4d80cc02017-06-01 21:37:38 +030053 struct flow_dissector_key_ip ip;
Jiri Pirko77b99002015-05-12 14:56:21 +020054} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
55
56struct fl_flow_mask_range {
57 unsigned short int start;
58 unsigned short int end;
59};
60
61struct fl_flow_mask {
62 struct fl_flow_key key;
63 struct fl_flow_mask_range range;
64 struct rcu_head rcu;
65};
66
67struct cls_fl_head {
68 struct rhashtable ht;
69 struct fl_flow_mask mask;
70 struct flow_dissector dissector;
Jiri Pirko77b99002015-05-12 14:56:21 +020071 bool mask_assigned;
72 struct list_head filters;
73 struct rhashtable_params ht_params;
Daniel Borkmannd9363772016-11-27 01:18:01 +010074 union {
75 struct work_struct work;
76 struct rcu_head rcu;
77 };
Chris Mic15ab232017-08-30 02:31:58 -040078 struct idr handle_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +020079};
80
81struct cls_fl_filter {
82 struct rhash_head ht_node;
83 struct fl_flow_key mkey;
84 struct tcf_exts exts;
85 struct tcf_result res;
86 struct fl_flow_key key;
87 struct list_head list;
88 u32 handle;
Amir Vadaie69985c2016-06-05 17:11:18 +030089 u32 flags;
Cong Wang0552c8a2017-10-26 18:24:33 -070090 union {
91 struct work_struct work;
92 struct rcu_head rcu;
93 };
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +020094 struct net_device *hw_dev;
Jiri Pirko77b99002015-05-12 14:56:21 +020095};
96
97static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
98{
99 return mask->range.end - mask->range.start;
100}
101
102static void fl_mask_update_range(struct fl_flow_mask *mask)
103{
104 const u8 *bytes = (const u8 *) &mask->key;
105 size_t size = sizeof(mask->key);
106 size_t i, first = 0, last = size - 1;
107
108 for (i = 0; i < sizeof(mask->key); i++) {
109 if (bytes[i]) {
110 if (!first && i)
111 first = i;
112 last = i;
113 }
114 }
115 mask->range.start = rounddown(first, sizeof(long));
116 mask->range.end = roundup(last + 1, sizeof(long));
117}
118
119static void *fl_key_get_start(struct fl_flow_key *key,
120 const struct fl_flow_mask *mask)
121{
122 return (u8 *) key + mask->range.start;
123}
124
125static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
126 struct fl_flow_mask *mask)
127{
128 const long *lkey = fl_key_get_start(key, mask);
129 const long *lmask = fl_key_get_start(&mask->key, mask);
130 long *lmkey = fl_key_get_start(mkey, mask);
131 int i;
132
133 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
134 *lmkey++ = *lkey++ & *lmask++;
135}
136
137static void fl_clear_masked_range(struct fl_flow_key *key,
138 struct fl_flow_mask *mask)
139{
140 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
141}
142
Paul Blakeya3308d82017-01-16 10:45:13 +0200143static struct cls_fl_filter *fl_lookup(struct cls_fl_head *head,
144 struct fl_flow_key *mkey)
145{
146 return rhashtable_lookup_fast(&head->ht,
147 fl_key_get_start(mkey, &head->mask),
148 head->ht_params);
149}
150
Jiri Pirko77b99002015-05-12 14:56:21 +0200151static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
152 struct tcf_result *res)
153{
154 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
155 struct cls_fl_filter *f;
156 struct fl_flow_key skb_key;
157 struct fl_flow_key skb_mkey;
158
Amir Vadaie69985c2016-06-05 17:11:18 +0300159 if (!atomic_read(&head->ht.nelems))
160 return -1;
161
Jiri Pirko77b99002015-05-12 14:56:21 +0200162 fl_clear_masked_range(&skb_key, &head->mask);
Amir Vadaibc3103f2016-09-08 16:23:47 +0300163
Jiri Pirko77b99002015-05-12 14:56:21 +0200164 skb_key.indev_ifindex = skb->skb_iif;
165 /* skb_flow_dissect() does not set n_proto in case an unknown protocol,
166 * so do it rather here.
167 */
168 skb_key.basic.n_proto = skb->protocol;
Simon Horman62b32372017-12-04 11:31:48 +0100169 skb_flow_dissect_tunnel_info(skb, &head->dissector, &skb_key);
Tom Herbertcd79a232015-09-01 09:24:27 -0700170 skb_flow_dissect(skb, &head->dissector, &skb_key, 0);
Jiri Pirko77b99002015-05-12 14:56:21 +0200171
172 fl_set_masked_key(&skb_mkey, &skb_key, &head->mask);
173
Paul Blakeya3308d82017-01-16 10:45:13 +0200174 f = fl_lookup(head, &skb_mkey);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300175 if (f && !tc_skip_sw(f->flags)) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200176 *res = f->res;
177 return tcf_exts_exec(skb, &f->exts, res);
178 }
179 return -1;
180}
181
182static int fl_init(struct tcf_proto *tp)
183{
184 struct cls_fl_head *head;
185
186 head = kzalloc(sizeof(*head), GFP_KERNEL);
187 if (!head)
188 return -ENOBUFS;
189
190 INIT_LIST_HEAD_RCU(&head->filters);
191 rcu_assign_pointer(tp->root, head);
Chris Mic15ab232017-08-30 02:31:58 -0400192 idr_init(&head->handle_idr);
Jiri Pirko77b99002015-05-12 14:56:21 +0200193
194 return 0;
195}
196
Cong Wang0dadc112017-11-06 13:47:24 -0800197static void __fl_destroy_filter(struct cls_fl_filter *f)
198{
199 tcf_exts_destroy(&f->exts);
200 tcf_exts_put_net(&f->exts);
201 kfree(f);
202}
203
Cong Wang0552c8a2017-10-26 18:24:33 -0700204static void fl_destroy_filter_work(struct work_struct *work)
205{
206 struct cls_fl_filter *f = container_of(work, struct cls_fl_filter, work);
207
208 rtnl_lock();
Cong Wang0dadc112017-11-06 13:47:24 -0800209 __fl_destroy_filter(f);
Cong Wang0552c8a2017-10-26 18:24:33 -0700210 rtnl_unlock();
211}
212
Jiri Pirko77b99002015-05-12 14:56:21 +0200213static void fl_destroy_filter(struct rcu_head *head)
214{
215 struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);
216
Cong Wang0552c8a2017-10-26 18:24:33 -0700217 INIT_WORK(&f->work, fl_destroy_filter_work);
218 tcf_queue_work(&f->work);
Jiri Pirko77b99002015-05-12 14:56:21 +0200219}
220
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800221static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
222 struct netlink_ext_ack *extack)
Amir Vadai5b33f482016-03-08 12:42:29 +0200223{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200224 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200225 struct tcf_block *block = tp->chain->block;
Amir Vadai5b33f482016-03-08 12:42:29 +0200226
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800227 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200228 cls_flower.command = TC_CLSFLOWER_DESTROY;
229 cls_flower.cookie = (unsigned long) f;
Amir Vadai5b33f482016-03-08 12:42:29 +0200230
Jiri Pirko208c0f42017-10-19 15:50:32 +0200231 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200232 &cls_flower, false);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100233 tcf_block_offload_dec(block, &f->flags);
Amir Vadai5b33f482016-03-08 12:42:29 +0200234}
235
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300236static int fl_hw_replace_filter(struct tcf_proto *tp,
237 struct flow_dissector *dissector,
238 struct fl_flow_key *mask,
Quentin Monnet41002032018-01-19 17:44:43 -0800239 struct cls_fl_filter *f,
240 struct netlink_ext_ack *extack)
Amir Vadai5b33f482016-03-08 12:42:29 +0200241{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200242 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200243 struct tcf_block *block = tp->chain->block;
Jiri Pirko717503b2017-10-11 09:41:09 +0200244 bool skip_sw = tc_skip_sw(f->flags);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300245 int err;
Amir Vadai5b33f482016-03-08 12:42:29 +0200246
Jakub Kicinskiea205942018-01-24 12:54:20 -0800247 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200248 cls_flower.command = TC_CLSFLOWER_REPLACE;
249 cls_flower.cookie = (unsigned long) f;
250 cls_flower.dissector = dissector;
251 cls_flower.mask = mask;
252 cls_flower.key = &f->mkey;
253 cls_flower.exts = &f->exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700254 cls_flower.classid = f->res.classid;
Amir Vadai5b33f482016-03-08 12:42:29 +0200255
Jiri Pirko208c0f42017-10-19 15:50:32 +0200256 err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200257 &cls_flower, skip_sw);
258 if (err < 0) {
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800259 fl_hw_destroy_filter(tp, f, NULL);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300260 return err;
Jiri Pirko717503b2017-10-11 09:41:09 +0200261 } else if (err > 0) {
Jiri Pirkocaa72602018-01-17 11:46:50 +0100262 tcf_block_offload_inc(block, &f->flags);
Jiri Pirko717503b2017-10-11 09:41:09 +0200263 }
264
265 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
266 return -EINVAL;
267
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300268 return 0;
Amir Vadai5b33f482016-03-08 12:42:29 +0200269}
270
Amir Vadai10cbc682016-05-13 12:55:37 +0000271static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
272{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200273 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200274 struct tcf_block *block = tp->chain->block;
Amir Vadai10cbc682016-05-13 12:55:37 +0000275
Jakub Kicinskiea205942018-01-24 12:54:20 -0800276 tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
Jiri Pirkode4784c2017-08-07 10:15:32 +0200277 cls_flower.command = TC_CLSFLOWER_STATS;
278 cls_flower.cookie = (unsigned long) f;
279 cls_flower.exts = &f->exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700280 cls_flower.classid = f->res.classid;
Amir Vadai10cbc682016-05-13 12:55:37 +0000281
Jiri Pirko208c0f42017-10-19 15:50:32 +0200282 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200283 &cls_flower, false);
Amir Vadai10cbc682016-05-13 12:55:37 +0000284}
285
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800286static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
287 struct netlink_ext_ack *extack)
Roi Dayan13fa8762016-11-01 16:08:29 +0200288{
Chris Mic15ab232017-08-30 02:31:58 -0400289 struct cls_fl_head *head = rtnl_dereference(tp->root);
290
Matthew Wilcox9c160942017-11-28 09:48:43 -0500291 idr_remove(&head->handle_idr, f->handle);
Roi Dayan13fa8762016-11-01 16:08:29 +0200292 list_del_rcu(&f->list);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200293 if (!tc_skip_hw(f->flags))
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800294 fl_hw_destroy_filter(tp, f, extack);
Roi Dayan13fa8762016-11-01 16:08:29 +0200295 tcf_unbind_filter(tp, &f->res);
Cong Wang0dadc112017-11-06 13:47:24 -0800296 if (tcf_exts_get_net(&f->exts))
297 call_rcu(&f->rcu, fl_destroy_filter);
298 else
299 __fl_destroy_filter(f);
Roi Dayan13fa8762016-11-01 16:08:29 +0200300}
301
Daniel Borkmannd9363772016-11-27 01:18:01 +0100302static void fl_destroy_sleepable(struct work_struct *work)
303{
304 struct cls_fl_head *head = container_of(work, struct cls_fl_head,
305 work);
306 if (head->mask_assigned)
307 rhashtable_destroy(&head->ht);
308 kfree(head);
309 module_put(THIS_MODULE);
310}
311
312static void fl_destroy_rcu(struct rcu_head *rcu)
313{
314 struct cls_fl_head *head = container_of(rcu, struct cls_fl_head, rcu);
315
316 INIT_WORK(&head->work, fl_destroy_sleepable);
317 schedule_work(&head->work);
318}
319
Jakub Kicinski715df5e2018-01-24 12:54:13 -0800320static void fl_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200321{
322 struct cls_fl_head *head = rtnl_dereference(tp->root);
323 struct cls_fl_filter *f, *next;
324
Roi Dayan13fa8762016-11-01 16:08:29 +0200325 list_for_each_entry_safe(f, next, &head->filters, list)
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800326 __fl_delete(tp, f, extack);
Chris Mic15ab232017-08-30 02:31:58 -0400327 idr_destroy(&head->handle_idr);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100328
329 __module_get(THIS_MODULE);
330 call_rcu(&head->rcu, fl_destroy_rcu);
Jiri Pirko77b99002015-05-12 14:56:21 +0200331}
332
WANG Cong8113c092017-08-04 21:31:43 -0700333static void *fl_get(struct tcf_proto *tp, u32 handle)
Jiri Pirko77b99002015-05-12 14:56:21 +0200334{
335 struct cls_fl_head *head = rtnl_dereference(tp->root);
Jiri Pirko77b99002015-05-12 14:56:21 +0200336
Matthew Wilcox322d8842017-11-28 10:01:24 -0500337 return idr_find(&head->handle_idr, handle);
Jiri Pirko77b99002015-05-12 14:56:21 +0200338}
339
340static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
341 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
342 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
343 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
344 .len = IFNAMSIZ },
345 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
346 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
347 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
348 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
349 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
350 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
351 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
352 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
353 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
354 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
355 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
356 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
357 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
358 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
359 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
360 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
Jamal Hadi Salimb175c3a2015-06-25 06:55:27 -0400361 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
362 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300363 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
364 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
365 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
Amir Vadaibc3103f2016-09-08 16:23:47 +0300366 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
367 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
368 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
369 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
370 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
371 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
372 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
373 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
374 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
Or Gerlitzaa72d702016-09-15 15:28:22 +0300375 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
376 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
377 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
378 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
Simon Horman5976c5f2016-11-03 13:24:21 +0100379 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
380 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
381 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
382 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200383 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
384 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
385 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
386 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200387 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
388 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
Simon Horman7b684882016-12-07 13:48:28 +0100389 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
390 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
391 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
392 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
393 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
394 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
395 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
396 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
Simon Horman99d31322017-01-11 14:05:43 +0100397 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
398 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
399 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
400 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
401 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
402 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
403 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
404 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
405 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
406 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400407 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
408 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
409 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
410 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200411 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
412 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300413 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
414 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
415 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
416 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
Jiri Pirko77b99002015-05-12 14:56:21 +0200417};
418
419static void fl_set_key_val(struct nlattr **tb,
420 void *val, int val_type,
421 void *mask, int mask_type, int len)
422{
423 if (!tb[val_type])
424 return;
425 memcpy(val, nla_data(tb[val_type]), len);
426 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
427 memset(mask, 0xff, len);
428 else
429 memcpy(mask, nla_data(tb[mask_type]), len);
430}
431
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400432static int fl_set_key_mpls(struct nlattr **tb,
433 struct flow_dissector_key_mpls *key_val,
434 struct flow_dissector_key_mpls *key_mask)
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400435{
436 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
437 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
438 key_mask->mpls_ttl = MPLS_TTL_MASK;
439 }
440 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400441 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
442
443 if (bos & ~MPLS_BOS_MASK)
444 return -EINVAL;
445 key_val->mpls_bos = bos;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400446 key_mask->mpls_bos = MPLS_BOS_MASK;
447 }
448 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400449 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
450
451 if (tc & ~MPLS_TC_MASK)
452 return -EINVAL;
453 key_val->mpls_tc = tc;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400454 key_mask->mpls_tc = MPLS_TC_MASK;
455 }
456 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400457 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
458
459 if (label & ~MPLS_LABEL_MASK)
460 return -EINVAL;
461 key_val->mpls_label = label;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400462 key_mask->mpls_label = MPLS_LABEL_MASK;
463 }
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400464 return 0;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400465}
466
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300467static void fl_set_key_vlan(struct nlattr **tb,
468 struct flow_dissector_key_vlan *key_val,
469 struct flow_dissector_key_vlan *key_mask)
470{
471#define VLAN_PRIORITY_MASK 0x7
472
473 if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
474 key_val->vlan_id =
475 nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
476 key_mask->vlan_id = VLAN_VID_MASK;
477 }
478 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
479 key_val->vlan_priority =
480 nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
481 VLAN_PRIORITY_MASK;
482 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
483 }
484}
485
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200486static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
487 u32 *dissector_key, u32 *dissector_mask,
488 u32 flower_flag_bit, u32 dissector_flag_bit)
489{
490 if (flower_mask & flower_flag_bit) {
491 *dissector_mask |= dissector_flag_bit;
492 if (flower_key & flower_flag_bit)
493 *dissector_key |= dissector_flag_bit;
494 }
495}
496
Or Gerlitzd9724772016-12-22 14:28:15 +0200497static int fl_set_key_flags(struct nlattr **tb,
498 u32 *flags_key, u32 *flags_mask)
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200499{
500 u32 key, mask;
501
Or Gerlitzd9724772016-12-22 14:28:15 +0200502 /* mask is mandatory for flags */
503 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
504 return -EINVAL;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200505
506 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
Or Gerlitzd9724772016-12-22 14:28:15 +0200507 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200508
509 *flags_key = 0;
510 *flags_mask = 0;
511
512 fl_set_key_flag(key, mask, flags_key, flags_mask,
513 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Or Gerlitzd9724772016-12-22 14:28:15 +0200514
515 return 0;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200516}
517
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300518static void fl_set_key_ip(struct nlattr **tb,
519 struct flow_dissector_key_ip *key,
520 struct flow_dissector_key_ip *mask)
521{
522 fl_set_key_val(tb, &key->tos, TCA_FLOWER_KEY_IP_TOS,
523 &mask->tos, TCA_FLOWER_KEY_IP_TOS_MASK,
524 sizeof(key->tos));
525
526 fl_set_key_val(tb, &key->ttl, TCA_FLOWER_KEY_IP_TTL,
527 &mask->ttl, TCA_FLOWER_KEY_IP_TTL_MASK,
528 sizeof(key->ttl));
529}
530
Jiri Pirko77b99002015-05-12 14:56:21 +0200531static int fl_set_key(struct net *net, struct nlattr **tb,
Alexander Aring1057c552018-01-18 11:20:54 -0500532 struct fl_flow_key *key, struct fl_flow_key *mask,
533 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200534{
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300535 __be16 ethertype;
Or Gerlitzd9724772016-12-22 14:28:15 +0200536 int ret = 0;
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400537#ifdef CONFIG_NET_CLS_IND
Jiri Pirko77b99002015-05-12 14:56:21 +0200538 if (tb[TCA_FLOWER_INDEV]) {
Alexander Aring1057c552018-01-18 11:20:54 -0500539 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200540 if (err < 0)
541 return err;
542 key->indev_ifindex = err;
543 mask->indev_ifindex = 0xffffffff;
544 }
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400545#endif
Jiri Pirko77b99002015-05-12 14:56:21 +0200546
547 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
548 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
549 sizeof(key->eth.dst));
550 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
551 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
552 sizeof(key->eth.src));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500553
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200554 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300555 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
556
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200557 if (ethertype == htons(ETH_P_8021Q)) {
558 fl_set_key_vlan(tb, &key->vlan, &mask->vlan);
559 fl_set_key_val(tb, &key->basic.n_proto,
560 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
561 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
562 sizeof(key->basic.n_proto));
563 } else {
564 key->basic.n_proto = ethertype;
565 mask->basic.n_proto = cpu_to_be16(~0);
566 }
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300567 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500568
Jiri Pirko77b99002015-05-12 14:56:21 +0200569 if (key->basic.n_proto == htons(ETH_P_IP) ||
570 key->basic.n_proto == htons(ETH_P_IPV6)) {
571 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
572 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
573 sizeof(key->basic.ip_proto));
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300574 fl_set_key_ip(tb, &key->ip, &mask->ip);
Jiri Pirko77b99002015-05-12 14:56:21 +0200575 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500576
577 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
578 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200579 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200580 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
581 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
582 sizeof(key->ipv4.src));
583 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
584 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
585 sizeof(key->ipv4.dst));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500586 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
587 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200588 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200589 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
590 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
591 sizeof(key->ipv6.src));
592 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
593 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
594 sizeof(key->ipv6.dst));
595 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500596
Jiri Pirko77b99002015-05-12 14:56:21 +0200597 if (key->basic.ip_proto == IPPROTO_TCP) {
598 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300599 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200600 sizeof(key->tp.src));
601 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300602 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200603 sizeof(key->tp.dst));
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200604 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
605 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
606 sizeof(key->tcp.flags));
Jiri Pirko77b99002015-05-12 14:56:21 +0200607 } else if (key->basic.ip_proto == IPPROTO_UDP) {
608 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300609 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200610 sizeof(key->tp.src));
611 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300612 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200613 sizeof(key->tp.dst));
Simon Horman5976c5f2016-11-03 13:24:21 +0100614 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
615 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
616 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
617 sizeof(key->tp.src));
618 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
619 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
620 sizeof(key->tp.dst));
Simon Horman7b684882016-12-07 13:48:28 +0100621 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
622 key->basic.ip_proto == IPPROTO_ICMP) {
623 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
624 &mask->icmp.type,
625 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
626 sizeof(key->icmp.type));
627 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
628 &mask->icmp.code,
629 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
630 sizeof(key->icmp.code));
631 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
632 key->basic.ip_proto == IPPROTO_ICMPV6) {
633 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
634 &mask->icmp.type,
635 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
636 sizeof(key->icmp.type));
Simon Horman040587a2017-01-30 16:19:02 +0100637 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
Simon Horman7b684882016-12-07 13:48:28 +0100638 &mask->icmp.code,
Simon Horman040587a2017-01-30 16:19:02 +0100639 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
Simon Horman7b684882016-12-07 13:48:28 +0100640 sizeof(key->icmp.code));
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400641 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
642 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400643 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
644 if (ret)
645 return ret;
Simon Horman99d31322017-01-11 14:05:43 +0100646 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
647 key->basic.n_proto == htons(ETH_P_RARP)) {
648 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
649 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
650 sizeof(key->arp.sip));
651 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
652 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
653 sizeof(key->arp.tip));
654 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
655 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
656 sizeof(key->arp.op));
657 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
658 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
659 sizeof(key->arp.sha));
660 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
661 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
662 sizeof(key->arp.tha));
Jiri Pirko77b99002015-05-12 14:56:21 +0200663 }
664
Amir Vadaibc3103f2016-09-08 16:23:47 +0300665 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
666 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
667 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200668 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300669 fl_set_key_val(tb, &key->enc_ipv4.src,
670 TCA_FLOWER_KEY_ENC_IPV4_SRC,
671 &mask->enc_ipv4.src,
672 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
673 sizeof(key->enc_ipv4.src));
674 fl_set_key_val(tb, &key->enc_ipv4.dst,
675 TCA_FLOWER_KEY_ENC_IPV4_DST,
676 &mask->enc_ipv4.dst,
677 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
678 sizeof(key->enc_ipv4.dst));
679 }
680
681 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
682 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
683 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200684 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300685 fl_set_key_val(tb, &key->enc_ipv6.src,
686 TCA_FLOWER_KEY_ENC_IPV6_SRC,
687 &mask->enc_ipv6.src,
688 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
689 sizeof(key->enc_ipv6.src));
690 fl_set_key_val(tb, &key->enc_ipv6.dst,
691 TCA_FLOWER_KEY_ENC_IPV6_DST,
692 &mask->enc_ipv6.dst,
693 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
694 sizeof(key->enc_ipv6.dst));
695 }
696
697 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +0300698 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
Amir Vadaibc3103f2016-09-08 16:23:47 +0300699 sizeof(key->enc_key_id.keyid));
700
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200701 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
702 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
703 sizeof(key->enc_tp.src));
704
705 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
706 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
707 sizeof(key->enc_tp.dst));
708
Or Gerlitzd9724772016-12-22 14:28:15 +0200709 if (tb[TCA_FLOWER_KEY_FLAGS])
710 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200711
Or Gerlitzd9724772016-12-22 14:28:15 +0200712 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +0200713}
714
715static bool fl_mask_eq(struct fl_flow_mask *mask1,
716 struct fl_flow_mask *mask2)
717{
718 const long *lmask1 = fl_key_get_start(&mask1->key, mask1);
719 const long *lmask2 = fl_key_get_start(&mask2->key, mask2);
720
721 return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) &&
722 !memcmp(lmask1, lmask2, fl_mask_range(mask1));
723}
724
725static const struct rhashtable_params fl_ht_params = {
726 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
727 .head_offset = offsetof(struct cls_fl_filter, ht_node),
728 .automatic_shrinking = true,
729};
730
731static int fl_init_hashtable(struct cls_fl_head *head,
732 struct fl_flow_mask *mask)
733{
734 head->ht_params = fl_ht_params;
735 head->ht_params.key_len = fl_mask_range(mask);
736 head->ht_params.key_offset += mask->range.start;
737
738 return rhashtable_init(&head->ht, &head->ht_params);
739}
740
741#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
742#define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
Jiri Pirko77b99002015-05-12 14:56:21 +0200743
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300744#define FL_KEY_IS_MASKED(mask, member) \
745 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
746 0, FL_KEY_MEMBER_SIZE(member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200747
748#define FL_KEY_SET(keys, cnt, id, member) \
749 do { \
750 keys[cnt].key_id = id; \
751 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
752 cnt++; \
753 } while(0);
754
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300755#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200756 do { \
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300757 if (FL_KEY_IS_MASKED(mask, member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200758 FL_KEY_SET(keys, cnt, id, member); \
759 } while(0);
760
761static void fl_init_dissector(struct cls_fl_head *head,
762 struct fl_flow_mask *mask)
763{
764 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
765 size_t cnt = 0;
766
Tom Herbert42aecaa2015-06-04 09:16:39 -0700767 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
Jiri Pirko77b99002015-05-12 14:56:21 +0200768 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300769 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
770 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
771 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
772 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
773 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
774 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
775 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
776 FLOW_DISSECTOR_KEY_PORTS, tp);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300777 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300778 FLOW_DISSECTOR_KEY_IP, ip);
779 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200780 FLOW_DISSECTOR_KEY_TCP, tcp);
781 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman7b684882016-12-07 13:48:28 +0100782 FLOW_DISSECTOR_KEY_ICMP, icmp);
783 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman99d31322017-01-11 14:05:43 +0100784 FLOW_DISSECTOR_KEY_ARP, arp);
785 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400786 FLOW_DISSECTOR_KEY_MPLS, mpls);
787 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300788 FLOW_DISSECTOR_KEY_VLAN, vlan);
Hadar Hen Zion519d1052016-11-07 15:14:38 +0200789 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
790 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
791 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
792 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
793 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
794 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
795 if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
796 FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
797 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
798 enc_control);
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200799 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
800 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
Jiri Pirko77b99002015-05-12 14:56:21 +0200801
802 skb_flow_dissector_init(&head->dissector, keys, cnt);
803}
804
805static int fl_check_assign_mask(struct cls_fl_head *head,
806 struct fl_flow_mask *mask)
807{
808 int err;
809
810 if (head->mask_assigned) {
811 if (!fl_mask_eq(&head->mask, mask))
812 return -EINVAL;
813 else
814 return 0;
815 }
816
817 /* Mask is not assigned yet. So assign it and init hashtable
818 * according to that.
819 */
820 err = fl_init_hashtable(head, mask);
821 if (err)
822 return err;
823 memcpy(&head->mask, mask, sizeof(head->mask));
824 head->mask_assigned = true;
825
826 fl_init_dissector(head, mask);
827
828 return 0;
829}
830
831static int fl_set_parms(struct net *net, struct tcf_proto *tp,
832 struct cls_fl_filter *f, struct fl_flow_mask *mask,
833 unsigned long base, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500834 struct nlattr *est, bool ovr,
835 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200836{
Jiri Pirko77b99002015-05-12 14:56:21 +0200837 int err;
838
Alexander Aring50a56192018-01-18 11:20:52 -0500839 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200840 if (err < 0)
841 return err;
842
843 if (tb[TCA_FLOWER_CLASSID]) {
844 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
845 tcf_bind_filter(tp, &f->res, base);
846 }
847
Alexander Aring1057c552018-01-18 11:20:54 -0500848 err = fl_set_key(net, tb, &f->key, &mask->key, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200849 if (err)
Jiri Pirko45507522017-08-04 14:29:06 +0200850 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +0200851
852 fl_mask_update_range(mask);
853 fl_set_masked_key(&f->mkey, &f->key, mask);
854
Jiri Pirko77b99002015-05-12 14:56:21 +0200855 return 0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200856}
857
Jiri Pirko77b99002015-05-12 14:56:21 +0200858static int fl_change(struct net *net, struct sk_buff *in_skb,
859 struct tcf_proto *tp, unsigned long base,
860 u32 handle, struct nlattr **tca,
Alexander Aring7306db32018-01-18 11:20:51 -0500861 void **arg, bool ovr, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200862{
863 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700864 struct cls_fl_filter *fold = *arg;
Jiri Pirko77b99002015-05-12 14:56:21 +0200865 struct cls_fl_filter *fnew;
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100866 struct nlattr **tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200867 struct fl_flow_mask mask = {};
868 int err;
869
870 if (!tca[TCA_OPTIONS])
871 return -EINVAL;
872
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100873 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
874 if (!tb)
875 return -ENOBUFS;
876
Johannes Bergfceb6432017-04-12 14:34:07 +0200877 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
878 fl_policy, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +0200879 if (err < 0)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100880 goto errout_tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200881
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100882 if (fold && handle && fold->handle != handle) {
883 err = -EINVAL;
884 goto errout_tb;
885 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200886
887 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100888 if (!fnew) {
889 err = -ENOBUFS;
890 goto errout_tb;
891 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200892
WANG Congb9a24bb2016-08-19 12:36:54 -0700893 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
894 if (err < 0)
895 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +0200896
897 if (!handle) {
Matthew Wilcox85bd0432017-11-28 10:53:03 -0500898 handle = 1;
899 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
900 INT_MAX, GFP_KERNEL);
901 } else if (!fold) {
902 /* user specifies a handle and it doesn't exist */
903 err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
904 handle, GFP_KERNEL);
Jiri Pirko77b99002015-05-12 14:56:21 +0200905 }
Matthew Wilcox85bd0432017-11-28 10:53:03 -0500906 if (err)
907 goto errout;
908 fnew->handle = handle;
Jiri Pirko77b99002015-05-12 14:56:21 +0200909
Amir Vadaie69985c2016-06-05 17:11:18 +0300910 if (tb[TCA_FLOWER_FLAGS]) {
911 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
912
913 if (!tc_flags_valid(fnew->flags)) {
914 err = -EINVAL;
Cong Wangfe2502e2017-09-20 09:18:45 -0700915 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +0300916 }
917 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200918
Alexander Aring50a56192018-01-18 11:20:52 -0500919 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr,
920 extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200921 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700922 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +0200923
924 err = fl_check_assign_mask(head, &mask);
925 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700926 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +0200927
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300928 if (!tc_skip_sw(fnew->flags)) {
Paul Blakeya3308d82017-01-16 10:45:13 +0200929 if (!fold && fl_lookup(head, &fnew->mkey)) {
930 err = -EEXIST;
Cong Wangfe2502e2017-09-20 09:18:45 -0700931 goto errout_idr;
Paul Blakeya3308d82017-01-16 10:45:13 +0200932 }
933
Amir Vadaie69985c2016-06-05 17:11:18 +0300934 err = rhashtable_insert_fast(&head->ht, &fnew->ht_node,
935 head->ht_params);
936 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700937 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +0300938 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200939
Hadar Hen Zion79685212016-12-01 14:06:34 +0200940 if (!tc_skip_hw(fnew->flags)) {
941 err = fl_hw_replace_filter(tp,
942 &head->dissector,
943 &mask.key,
Quentin Monnet41002032018-01-19 17:44:43 -0800944 fnew,
945 extack);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200946 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700947 goto errout_idr;
Hadar Hen Zion79685212016-12-01 14:06:34 +0200948 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200949
Or Gerlitz55593962017-02-16 10:31:13 +0200950 if (!tc_in_hw(fnew->flags))
951 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
952
Amir Vadai5b33f482016-03-08 12:42:29 +0200953 if (fold) {
Jiri Pirko725cbb622016-11-28 15:40:13 +0100954 if (!tc_skip_sw(fold->flags))
955 rhashtable_remove_fast(&head->ht, &fold->ht_node,
956 head->ht_params);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200957 if (!tc_skip_hw(fold->flags))
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800958 fl_hw_destroy_filter(tp, fold, NULL);
Amir Vadai5b33f482016-03-08 12:42:29 +0200959 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200960
WANG Cong8113c092017-08-04 21:31:43 -0700961 *arg = fnew;
Jiri Pirko77b99002015-05-12 14:56:21 +0200962
963 if (fold) {
Matthew Wilcox234a4622017-11-28 09:56:36 -0500964 idr_replace(&head->handle_idr, fnew, fnew->handle);
Daniel Borkmannff3532f2015-07-17 22:38:44 +0200965 list_replace_rcu(&fold->list, &fnew->list);
Jiri Pirko77b99002015-05-12 14:56:21 +0200966 tcf_unbind_filter(tp, &fold->res);
Cong Wang0dadc112017-11-06 13:47:24 -0800967 tcf_exts_get_net(&fold->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +0200968 call_rcu(&fold->rcu, fl_destroy_filter);
969 } else {
970 list_add_tail_rcu(&fnew->list, &head->filters);
971 }
972
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100973 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +0200974 return 0;
975
Cong Wangfe2502e2017-09-20 09:18:45 -0700976errout_idr:
977 if (fnew->handle)
Matthew Wilcox9c160942017-11-28 09:48:43 -0500978 idr_remove(&head->handle_idr, fnew->handle);
Jiri Pirko77b99002015-05-12 14:56:21 +0200979errout:
WANG Congb9a24bb2016-08-19 12:36:54 -0700980 tcf_exts_destroy(&fnew->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +0200981 kfree(fnew);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100982errout_tb:
983 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +0200984 return err;
985}
986
Alexander Aring571acf22018-01-18 11:20:53 -0500987static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
988 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200989{
990 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700991 struct cls_fl_filter *f = arg;
Jiri Pirko77b99002015-05-12 14:56:21 +0200992
Jiri Pirko725cbb622016-11-28 15:40:13 +0100993 if (!tc_skip_sw(f->flags))
994 rhashtable_remove_fast(&head->ht, &f->ht_node,
995 head->ht_params);
Jakub Kicinski1b0f8032018-01-24 12:54:21 -0800996 __fl_delete(tp, f, extack);
WANG Cong763dbf62017-04-19 14:21:21 -0700997 *last = list_empty(&head->filters);
Jiri Pirko77b99002015-05-12 14:56:21 +0200998 return 0;
999}
1000
1001static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1002{
1003 struct cls_fl_head *head = rtnl_dereference(tp->root);
1004 struct cls_fl_filter *f;
1005
1006 list_for_each_entry_rcu(f, &head->filters, list) {
1007 if (arg->count < arg->skip)
1008 goto skip;
WANG Cong8113c092017-08-04 21:31:43 -07001009 if (arg->fn(tp, f, arg) < 0) {
Jiri Pirko77b99002015-05-12 14:56:21 +02001010 arg->stop = 1;
1011 break;
1012 }
1013skip:
1014 arg->count++;
1015 }
1016}
1017
1018static int fl_dump_key_val(struct sk_buff *skb,
1019 void *val, int val_type,
1020 void *mask, int mask_type, int len)
1021{
1022 int err;
1023
1024 if (!memchr_inv(mask, 0, len))
1025 return 0;
1026 err = nla_put(skb, val_type, len, val);
1027 if (err)
1028 return err;
1029 if (mask_type != TCA_FLOWER_UNSPEC) {
1030 err = nla_put(skb, mask_type, len, mask);
1031 if (err)
1032 return err;
1033 }
1034 return 0;
1035}
1036
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001037static int fl_dump_key_mpls(struct sk_buff *skb,
1038 struct flow_dissector_key_mpls *mpls_key,
1039 struct flow_dissector_key_mpls *mpls_mask)
1040{
1041 int err;
1042
1043 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1044 return 0;
1045 if (mpls_mask->mpls_ttl) {
1046 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1047 mpls_key->mpls_ttl);
1048 if (err)
1049 return err;
1050 }
1051 if (mpls_mask->mpls_tc) {
1052 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1053 mpls_key->mpls_tc);
1054 if (err)
1055 return err;
1056 }
1057 if (mpls_mask->mpls_label) {
1058 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1059 mpls_key->mpls_label);
1060 if (err)
1061 return err;
1062 }
1063 if (mpls_mask->mpls_bos) {
1064 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1065 mpls_key->mpls_bos);
1066 if (err)
1067 return err;
1068 }
1069 return 0;
1070}
1071
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001072static int fl_dump_key_ip(struct sk_buff *skb,
1073 struct flow_dissector_key_ip *key,
1074 struct flow_dissector_key_ip *mask)
1075{
1076 if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos,
1077 TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) ||
1078 fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl,
1079 TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl)))
1080 return -1;
1081
1082 return 0;
1083}
1084
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001085static int fl_dump_key_vlan(struct sk_buff *skb,
1086 struct flow_dissector_key_vlan *vlan_key,
1087 struct flow_dissector_key_vlan *vlan_mask)
1088{
1089 int err;
1090
1091 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1092 return 0;
1093 if (vlan_mask->vlan_id) {
1094 err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID,
1095 vlan_key->vlan_id);
1096 if (err)
1097 return err;
1098 }
1099 if (vlan_mask->vlan_priority) {
1100 err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO,
1101 vlan_key->vlan_priority);
1102 if (err)
1103 return err;
1104 }
1105 return 0;
1106}
1107
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001108static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1109 u32 *flower_key, u32 *flower_mask,
1110 u32 flower_flag_bit, u32 dissector_flag_bit)
1111{
1112 if (dissector_mask & dissector_flag_bit) {
1113 *flower_mask |= flower_flag_bit;
1114 if (dissector_key & dissector_flag_bit)
1115 *flower_key |= flower_flag_bit;
1116 }
1117}
1118
1119static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1120{
1121 u32 key, mask;
1122 __be32 _key, _mask;
1123 int err;
1124
1125 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1126 return 0;
1127
1128 key = 0;
1129 mask = 0;
1130
1131 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1132 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1133
1134 _key = cpu_to_be32(key);
1135 _mask = cpu_to_be32(mask);
1136
1137 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1138 if (err)
1139 return err;
1140
1141 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1142}
1143
WANG Cong8113c092017-08-04 21:31:43 -07001144static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jiri Pirko77b99002015-05-12 14:56:21 +02001145 struct sk_buff *skb, struct tcmsg *t)
1146{
1147 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -07001148 struct cls_fl_filter *f = fh;
Jiri Pirko77b99002015-05-12 14:56:21 +02001149 struct nlattr *nest;
1150 struct fl_flow_key *key, *mask;
1151
1152 if (!f)
1153 return skb->len;
1154
1155 t->tcm_handle = f->handle;
1156
1157 nest = nla_nest_start(skb, TCA_OPTIONS);
1158 if (!nest)
1159 goto nla_put_failure;
1160
1161 if (f->res.classid &&
1162 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1163 goto nla_put_failure;
1164
1165 key = &f->key;
1166 mask = &head->mask.key;
1167
1168 if (mask->indev_ifindex) {
1169 struct net_device *dev;
1170
1171 dev = __dev_get_by_index(net, key->indev_ifindex);
1172 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1173 goto nla_put_failure;
1174 }
1175
Hadar Hen Zion79685212016-12-01 14:06:34 +02001176 if (!tc_skip_hw(f->flags))
1177 fl_hw_update_stats(tp, f);
Amir Vadai10cbc682016-05-13 12:55:37 +00001178
Jiri Pirko77b99002015-05-12 14:56:21 +02001179 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1180 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1181 sizeof(key->eth.dst)) ||
1182 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1183 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1184 sizeof(key->eth.src)) ||
1185 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1186 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1187 sizeof(key->basic.n_proto)))
1188 goto nla_put_failure;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001189
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001190 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1191 goto nla_put_failure;
1192
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001193 if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan))
1194 goto nla_put_failure;
1195
Jiri Pirko77b99002015-05-12 14:56:21 +02001196 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1197 key->basic.n_proto == htons(ETH_P_IPV6)) &&
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001198 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
Jiri Pirko77b99002015-05-12 14:56:21 +02001199 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001200 sizeof(key->basic.ip_proto)) ||
1201 fl_dump_key_ip(skb, &key->ip, &mask->ip)))
Jiri Pirko77b99002015-05-12 14:56:21 +02001202 goto nla_put_failure;
1203
Tom Herbertc3f83242015-06-04 09:16:40 -07001204 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001205 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1206 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1207 sizeof(key->ipv4.src)) ||
1208 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1209 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1210 sizeof(key->ipv4.dst))))
1211 goto nla_put_failure;
Tom Herbertc3f83242015-06-04 09:16:40 -07001212 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001213 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1214 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1215 sizeof(key->ipv6.src)) ||
1216 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1217 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1218 sizeof(key->ipv6.dst))))
1219 goto nla_put_failure;
1220
1221 if (key->basic.ip_proto == IPPROTO_TCP &&
1222 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001223 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001224 sizeof(key->tp.src)) ||
1225 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001226 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001227 sizeof(key->tp.dst)) ||
1228 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1229 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1230 sizeof(key->tcp.flags))))
Jiri Pirko77b99002015-05-12 14:56:21 +02001231 goto nla_put_failure;
1232 else if (key->basic.ip_proto == IPPROTO_UDP &&
1233 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001234 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001235 sizeof(key->tp.src)) ||
1236 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001237 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001238 sizeof(key->tp.dst))))
1239 goto nla_put_failure;
Simon Horman5976c5f2016-11-03 13:24:21 +01001240 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1241 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1242 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1243 sizeof(key->tp.src)) ||
1244 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1245 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1246 sizeof(key->tp.dst))))
1247 goto nla_put_failure;
Simon Horman7b684882016-12-07 13:48:28 +01001248 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1249 key->basic.ip_proto == IPPROTO_ICMP &&
1250 (fl_dump_key_val(skb, &key->icmp.type,
1251 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1252 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1253 sizeof(key->icmp.type)) ||
1254 fl_dump_key_val(skb, &key->icmp.code,
1255 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1256 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1257 sizeof(key->icmp.code))))
1258 goto nla_put_failure;
1259 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1260 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1261 (fl_dump_key_val(skb, &key->icmp.type,
1262 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1263 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1264 sizeof(key->icmp.type)) ||
1265 fl_dump_key_val(skb, &key->icmp.code,
1266 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1267 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1268 sizeof(key->icmp.code))))
1269 goto nla_put_failure;
Simon Horman99d31322017-01-11 14:05:43 +01001270 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1271 key->basic.n_proto == htons(ETH_P_RARP)) &&
1272 (fl_dump_key_val(skb, &key->arp.sip,
1273 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1274 TCA_FLOWER_KEY_ARP_SIP_MASK,
1275 sizeof(key->arp.sip)) ||
1276 fl_dump_key_val(skb, &key->arp.tip,
1277 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1278 TCA_FLOWER_KEY_ARP_TIP_MASK,
1279 sizeof(key->arp.tip)) ||
1280 fl_dump_key_val(skb, &key->arp.op,
1281 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1282 TCA_FLOWER_KEY_ARP_OP_MASK,
1283 sizeof(key->arp.op)) ||
1284 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1285 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1286 sizeof(key->arp.sha)) ||
1287 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1288 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1289 sizeof(key->arp.tha))))
1290 goto nla_put_failure;
Jiri Pirko77b99002015-05-12 14:56:21 +02001291
Amir Vadaibc3103f2016-09-08 16:23:47 +03001292 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1293 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1294 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1295 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1296 sizeof(key->enc_ipv4.src)) ||
1297 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1298 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1299 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1300 sizeof(key->enc_ipv4.dst))))
1301 goto nla_put_failure;
1302 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1303 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1304 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1305 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1306 sizeof(key->enc_ipv6.src)) ||
1307 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1308 TCA_FLOWER_KEY_ENC_IPV6_DST,
1309 &mask->enc_ipv6.dst,
1310 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1311 sizeof(key->enc_ipv6.dst))))
1312 goto nla_put_failure;
1313
1314 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001315 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001316 sizeof(key->enc_key_id)) ||
1317 fl_dump_key_val(skb, &key->enc_tp.src,
1318 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1319 &mask->enc_tp.src,
1320 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1321 sizeof(key->enc_tp.src)) ||
1322 fl_dump_key_val(skb, &key->enc_tp.dst,
1323 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1324 &mask->enc_tp.dst,
1325 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1326 sizeof(key->enc_tp.dst)))
Amir Vadaibc3103f2016-09-08 16:23:47 +03001327 goto nla_put_failure;
1328
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001329 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1330 goto nla_put_failure;
1331
Or Gerlitz749e6722017-02-16 10:31:10 +02001332 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1333 goto nla_put_failure;
Amir Vadaie69985c2016-06-05 17:11:18 +03001334
Jiri Pirko77b99002015-05-12 14:56:21 +02001335 if (tcf_exts_dump(skb, &f->exts))
1336 goto nla_put_failure;
1337
1338 nla_nest_end(skb, nest);
1339
1340 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1341 goto nla_put_failure;
1342
1343 return skb->len;
1344
1345nla_put_failure:
1346 nla_nest_cancel(skb, nest);
1347 return -1;
1348}
1349
Cong Wang07d79fc2017-08-30 14:30:36 -07001350static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
1351{
1352 struct cls_fl_filter *f = fh;
1353
1354 if (f && f->res.classid == classid)
1355 f->res.class = cl;
1356}
1357
Jiri Pirko77b99002015-05-12 14:56:21 +02001358static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1359 .kind = "flower",
1360 .classify = fl_classify,
1361 .init = fl_init,
1362 .destroy = fl_destroy,
1363 .get = fl_get,
1364 .change = fl_change,
1365 .delete = fl_delete,
1366 .walk = fl_walk,
1367 .dump = fl_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07001368 .bind_class = fl_bind_class,
Jiri Pirko77b99002015-05-12 14:56:21 +02001369 .owner = THIS_MODULE,
1370};
1371
1372static int __init cls_fl_init(void)
1373{
1374 return register_tcf_proto_ops(&cls_fl_ops);
1375}
1376
1377static void __exit cls_fl_exit(void)
1378{
1379 unregister_tcf_proto_ops(&cls_fl_ops);
1380}
1381
1382module_init(cls_fl_init);
1383module_exit(cls_fl_exit);
1384
1385MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1386MODULE_DESCRIPTION("Flower classifier");
1387MODULE_LICENSE("GPL v2");