blob: 50145b873ff149addcb5755a5a9412f52b6135bf [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
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200221static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f)
Amir Vadai5b33f482016-03-08 12:42:29 +0200222{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200223 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200224 struct tcf_block *block = tp->chain->block;
Amir Vadai5b33f482016-03-08 12:42:29 +0200225
Jiri Pirkode4784c2017-08-07 10:15:32 +0200226 tc_cls_common_offload_init(&cls_flower.common, tp);
227 cls_flower.command = TC_CLSFLOWER_DESTROY;
228 cls_flower.cookie = (unsigned long) f;
Amir Vadai5b33f482016-03-08 12:42:29 +0200229
Jiri Pirko208c0f42017-10-19 15:50:32 +0200230 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200231 &cls_flower, false);
Jiri Pirkocaa72602018-01-17 11:46:50 +0100232 tcf_block_offload_dec(block, &f->flags);
Amir Vadai5b33f482016-03-08 12:42:29 +0200233}
234
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300235static int fl_hw_replace_filter(struct tcf_proto *tp,
236 struct flow_dissector *dissector,
237 struct fl_flow_key *mask,
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200238 struct cls_fl_filter *f)
Amir Vadai5b33f482016-03-08 12:42:29 +0200239{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200240 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200241 struct tcf_block *block = tp->chain->block;
Jiri Pirko717503b2017-10-11 09:41:09 +0200242 bool skip_sw = tc_skip_sw(f->flags);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300243 int err;
Amir Vadai5b33f482016-03-08 12:42:29 +0200244
Jiri Pirkode4784c2017-08-07 10:15:32 +0200245 tc_cls_common_offload_init(&cls_flower.common, tp);
246 cls_flower.command = TC_CLSFLOWER_REPLACE;
247 cls_flower.cookie = (unsigned long) f;
248 cls_flower.dissector = dissector;
249 cls_flower.mask = mask;
250 cls_flower.key = &f->mkey;
251 cls_flower.exts = &f->exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700252 cls_flower.classid = f->res.classid;
Amir Vadai5b33f482016-03-08 12:42:29 +0200253
Jiri Pirko208c0f42017-10-19 15:50:32 +0200254 err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200255 &cls_flower, skip_sw);
256 if (err < 0) {
257 fl_hw_destroy_filter(tp, f);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300258 return err;
Jiri Pirko717503b2017-10-11 09:41:09 +0200259 } else if (err > 0) {
Jiri Pirkocaa72602018-01-17 11:46:50 +0100260 tcf_block_offload_inc(block, &f->flags);
Jiri Pirko717503b2017-10-11 09:41:09 +0200261 }
262
263 if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
264 return -EINVAL;
265
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300266 return 0;
Amir Vadai5b33f482016-03-08 12:42:29 +0200267}
268
Amir Vadai10cbc682016-05-13 12:55:37 +0000269static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
270{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200271 struct tc_cls_flower_offload cls_flower = {};
Jiri Pirko208c0f42017-10-19 15:50:32 +0200272 struct tcf_block *block = tp->chain->block;
Amir Vadai10cbc682016-05-13 12:55:37 +0000273
Jiri Pirkode4784c2017-08-07 10:15:32 +0200274 tc_cls_common_offload_init(&cls_flower.common, tp);
275 cls_flower.command = TC_CLSFLOWER_STATS;
276 cls_flower.cookie = (unsigned long) f;
277 cls_flower.exts = &f->exts;
Amritha Nambiar384c1812017-10-27 02:35:34 -0700278 cls_flower.classid = f->res.classid;
Amir Vadai10cbc682016-05-13 12:55:37 +0000279
Jiri Pirko208c0f42017-10-19 15:50:32 +0200280 tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
Jiri Pirko717503b2017-10-11 09:41:09 +0200281 &cls_flower, false);
Amir Vadai10cbc682016-05-13 12:55:37 +0000282}
283
Roi Dayan13fa8762016-11-01 16:08:29 +0200284static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f)
285{
Chris Mic15ab232017-08-30 02:31:58 -0400286 struct cls_fl_head *head = rtnl_dereference(tp->root);
287
288 idr_remove_ext(&head->handle_idr, f->handle);
Roi Dayan13fa8762016-11-01 16:08:29 +0200289 list_del_rcu(&f->list);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200290 if (!tc_skip_hw(f->flags))
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200291 fl_hw_destroy_filter(tp, f);
Roi Dayan13fa8762016-11-01 16:08:29 +0200292 tcf_unbind_filter(tp, &f->res);
Cong Wang0dadc112017-11-06 13:47:24 -0800293 if (tcf_exts_get_net(&f->exts))
294 call_rcu(&f->rcu, fl_destroy_filter);
295 else
296 __fl_destroy_filter(f);
Roi Dayan13fa8762016-11-01 16:08:29 +0200297}
298
Daniel Borkmannd9363772016-11-27 01:18:01 +0100299static void fl_destroy_sleepable(struct work_struct *work)
300{
301 struct cls_fl_head *head = container_of(work, struct cls_fl_head,
302 work);
303 if (head->mask_assigned)
304 rhashtable_destroy(&head->ht);
305 kfree(head);
306 module_put(THIS_MODULE);
307}
308
309static void fl_destroy_rcu(struct rcu_head *rcu)
310{
311 struct cls_fl_head *head = container_of(rcu, struct cls_fl_head, rcu);
312
313 INIT_WORK(&head->work, fl_destroy_sleepable);
314 schedule_work(&head->work);
315}
316
WANG Cong763dbf62017-04-19 14:21:21 -0700317static void fl_destroy(struct tcf_proto *tp)
Jiri Pirko77b99002015-05-12 14:56:21 +0200318{
319 struct cls_fl_head *head = rtnl_dereference(tp->root);
320 struct cls_fl_filter *f, *next;
321
Roi Dayan13fa8762016-11-01 16:08:29 +0200322 list_for_each_entry_safe(f, next, &head->filters, list)
323 __fl_delete(tp, f);
Chris Mic15ab232017-08-30 02:31:58 -0400324 idr_destroy(&head->handle_idr);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100325
326 __module_get(THIS_MODULE);
327 call_rcu(&head->rcu, fl_destroy_rcu);
Jiri Pirko77b99002015-05-12 14:56:21 +0200328}
329
WANG Cong8113c092017-08-04 21:31:43 -0700330static void *fl_get(struct tcf_proto *tp, u32 handle)
Jiri Pirko77b99002015-05-12 14:56:21 +0200331{
332 struct cls_fl_head *head = rtnl_dereference(tp->root);
Jiri Pirko77b99002015-05-12 14:56:21 +0200333
Chris Mic15ab232017-08-30 02:31:58 -0400334 return idr_find_ext(&head->handle_idr, handle);
Jiri Pirko77b99002015-05-12 14:56:21 +0200335}
336
337static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
338 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
339 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
340 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
341 .len = IFNAMSIZ },
342 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
343 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
344 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
345 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
346 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
347 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
348 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
349 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
350 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
351 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
352 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
353 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
354 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
355 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
356 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
357 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
Jamal Hadi Salimb175c3a2015-06-25 06:55:27 -0400358 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
359 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300360 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
361 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
362 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
Amir Vadaibc3103f2016-09-08 16:23:47 +0300363 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
364 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
365 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
366 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
367 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
368 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
369 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
370 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
371 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
Or Gerlitzaa72d702016-09-15 15:28:22 +0300372 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
373 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
374 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
375 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
Simon Horman5976c5f2016-11-03 13:24:21 +0100376 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
377 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
378 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
379 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200380 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
381 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
382 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
383 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200384 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
385 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
Simon Horman7b684882016-12-07 13:48:28 +0100386 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
387 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
388 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
389 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
390 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
391 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
392 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
393 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
Simon Horman99d31322017-01-11 14:05:43 +0100394 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
395 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
396 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
397 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
398 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
399 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
400 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
401 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
402 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
403 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400404 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
405 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
406 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
407 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200408 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
409 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300410 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
411 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
412 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
413 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
Jiri Pirko77b99002015-05-12 14:56:21 +0200414};
415
416static void fl_set_key_val(struct nlattr **tb,
417 void *val, int val_type,
418 void *mask, int mask_type, int len)
419{
420 if (!tb[val_type])
421 return;
422 memcpy(val, nla_data(tb[val_type]), len);
423 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
424 memset(mask, 0xff, len);
425 else
426 memcpy(mask, nla_data(tb[mask_type]), len);
427}
428
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400429static int fl_set_key_mpls(struct nlattr **tb,
430 struct flow_dissector_key_mpls *key_val,
431 struct flow_dissector_key_mpls *key_mask)
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400432{
433 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
434 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
435 key_mask->mpls_ttl = MPLS_TTL_MASK;
436 }
437 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400438 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
439
440 if (bos & ~MPLS_BOS_MASK)
441 return -EINVAL;
442 key_val->mpls_bos = bos;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400443 key_mask->mpls_bos = MPLS_BOS_MASK;
444 }
445 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400446 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
447
448 if (tc & ~MPLS_TC_MASK)
449 return -EINVAL;
450 key_val->mpls_tc = tc;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400451 key_mask->mpls_tc = MPLS_TC_MASK;
452 }
453 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400454 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
455
456 if (label & ~MPLS_LABEL_MASK)
457 return -EINVAL;
458 key_val->mpls_label = label;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400459 key_mask->mpls_label = MPLS_LABEL_MASK;
460 }
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400461 return 0;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400462}
463
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300464static void fl_set_key_vlan(struct nlattr **tb,
465 struct flow_dissector_key_vlan *key_val,
466 struct flow_dissector_key_vlan *key_mask)
467{
468#define VLAN_PRIORITY_MASK 0x7
469
470 if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
471 key_val->vlan_id =
472 nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
473 key_mask->vlan_id = VLAN_VID_MASK;
474 }
475 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
476 key_val->vlan_priority =
477 nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
478 VLAN_PRIORITY_MASK;
479 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
480 }
481}
482
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200483static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
484 u32 *dissector_key, u32 *dissector_mask,
485 u32 flower_flag_bit, u32 dissector_flag_bit)
486{
487 if (flower_mask & flower_flag_bit) {
488 *dissector_mask |= dissector_flag_bit;
489 if (flower_key & flower_flag_bit)
490 *dissector_key |= dissector_flag_bit;
491 }
492}
493
Or Gerlitzd9724772016-12-22 14:28:15 +0200494static int fl_set_key_flags(struct nlattr **tb,
495 u32 *flags_key, u32 *flags_mask)
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200496{
497 u32 key, mask;
498
Or Gerlitzd9724772016-12-22 14:28:15 +0200499 /* mask is mandatory for flags */
500 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
501 return -EINVAL;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200502
503 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
Or Gerlitzd9724772016-12-22 14:28:15 +0200504 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200505
506 *flags_key = 0;
507 *flags_mask = 0;
508
509 fl_set_key_flag(key, mask, flags_key, flags_mask,
510 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Or Gerlitzd9724772016-12-22 14:28:15 +0200511
512 return 0;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200513}
514
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300515static void fl_set_key_ip(struct nlattr **tb,
516 struct flow_dissector_key_ip *key,
517 struct flow_dissector_key_ip *mask)
518{
519 fl_set_key_val(tb, &key->tos, TCA_FLOWER_KEY_IP_TOS,
520 &mask->tos, TCA_FLOWER_KEY_IP_TOS_MASK,
521 sizeof(key->tos));
522
523 fl_set_key_val(tb, &key->ttl, TCA_FLOWER_KEY_IP_TTL,
524 &mask->ttl, TCA_FLOWER_KEY_IP_TTL_MASK,
525 sizeof(key->ttl));
526}
527
Jiri Pirko77b99002015-05-12 14:56:21 +0200528static int fl_set_key(struct net *net, struct nlattr **tb,
529 struct fl_flow_key *key, struct fl_flow_key *mask)
530{
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300531 __be16 ethertype;
Or Gerlitzd9724772016-12-22 14:28:15 +0200532 int ret = 0;
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400533#ifdef CONFIG_NET_CLS_IND
Jiri Pirko77b99002015-05-12 14:56:21 +0200534 if (tb[TCA_FLOWER_INDEV]) {
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400535 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV]);
Jiri Pirko77b99002015-05-12 14:56:21 +0200536 if (err < 0)
537 return err;
538 key->indev_ifindex = err;
539 mask->indev_ifindex = 0xffffffff;
540 }
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400541#endif
Jiri Pirko77b99002015-05-12 14:56:21 +0200542
543 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
544 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
545 sizeof(key->eth.dst));
546 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
547 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
548 sizeof(key->eth.src));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500549
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200550 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300551 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
552
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200553 if (ethertype == htons(ETH_P_8021Q)) {
554 fl_set_key_vlan(tb, &key->vlan, &mask->vlan);
555 fl_set_key_val(tb, &key->basic.n_proto,
556 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
557 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
558 sizeof(key->basic.n_proto));
559 } else {
560 key->basic.n_proto = ethertype;
561 mask->basic.n_proto = cpu_to_be16(~0);
562 }
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300563 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500564
Jiri Pirko77b99002015-05-12 14:56:21 +0200565 if (key->basic.n_proto == htons(ETH_P_IP) ||
566 key->basic.n_proto == htons(ETH_P_IPV6)) {
567 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
568 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
569 sizeof(key->basic.ip_proto));
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300570 fl_set_key_ip(tb, &key->ip, &mask->ip);
Jiri Pirko77b99002015-05-12 14:56:21 +0200571 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500572
573 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
574 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200575 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200576 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
577 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
578 sizeof(key->ipv4.src));
579 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
580 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
581 sizeof(key->ipv4.dst));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500582 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
583 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200584 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200585 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
586 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
587 sizeof(key->ipv6.src));
588 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
589 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
590 sizeof(key->ipv6.dst));
591 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500592
Jiri Pirko77b99002015-05-12 14:56:21 +0200593 if (key->basic.ip_proto == IPPROTO_TCP) {
594 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300595 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200596 sizeof(key->tp.src));
597 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300598 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200599 sizeof(key->tp.dst));
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200600 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
601 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
602 sizeof(key->tcp.flags));
Jiri Pirko77b99002015-05-12 14:56:21 +0200603 } else if (key->basic.ip_proto == IPPROTO_UDP) {
604 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300605 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200606 sizeof(key->tp.src));
607 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300608 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200609 sizeof(key->tp.dst));
Simon Horman5976c5f2016-11-03 13:24:21 +0100610 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
611 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
612 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
613 sizeof(key->tp.src));
614 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
615 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
616 sizeof(key->tp.dst));
Simon Horman7b684882016-12-07 13:48:28 +0100617 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
618 key->basic.ip_proto == IPPROTO_ICMP) {
619 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
620 &mask->icmp.type,
621 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
622 sizeof(key->icmp.type));
623 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
624 &mask->icmp.code,
625 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
626 sizeof(key->icmp.code));
627 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
628 key->basic.ip_proto == IPPROTO_ICMPV6) {
629 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
630 &mask->icmp.type,
631 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
632 sizeof(key->icmp.type));
Simon Horman040587a2017-01-30 16:19:02 +0100633 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
Simon Horman7b684882016-12-07 13:48:28 +0100634 &mask->icmp.code,
Simon Horman040587a2017-01-30 16:19:02 +0100635 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
Simon Horman7b684882016-12-07 13:48:28 +0100636 sizeof(key->icmp.code));
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400637 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
638 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400639 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
640 if (ret)
641 return ret;
Simon Horman99d31322017-01-11 14:05:43 +0100642 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
643 key->basic.n_proto == htons(ETH_P_RARP)) {
644 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
645 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
646 sizeof(key->arp.sip));
647 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
648 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
649 sizeof(key->arp.tip));
650 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
651 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
652 sizeof(key->arp.op));
653 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
654 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
655 sizeof(key->arp.sha));
656 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
657 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
658 sizeof(key->arp.tha));
Jiri Pirko77b99002015-05-12 14:56:21 +0200659 }
660
Amir Vadaibc3103f2016-09-08 16:23:47 +0300661 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
662 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
663 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200664 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300665 fl_set_key_val(tb, &key->enc_ipv4.src,
666 TCA_FLOWER_KEY_ENC_IPV4_SRC,
667 &mask->enc_ipv4.src,
668 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
669 sizeof(key->enc_ipv4.src));
670 fl_set_key_val(tb, &key->enc_ipv4.dst,
671 TCA_FLOWER_KEY_ENC_IPV4_DST,
672 &mask->enc_ipv4.dst,
673 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
674 sizeof(key->enc_ipv4.dst));
675 }
676
677 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
678 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
679 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200680 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300681 fl_set_key_val(tb, &key->enc_ipv6.src,
682 TCA_FLOWER_KEY_ENC_IPV6_SRC,
683 &mask->enc_ipv6.src,
684 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
685 sizeof(key->enc_ipv6.src));
686 fl_set_key_val(tb, &key->enc_ipv6.dst,
687 TCA_FLOWER_KEY_ENC_IPV6_DST,
688 &mask->enc_ipv6.dst,
689 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
690 sizeof(key->enc_ipv6.dst));
691 }
692
693 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +0300694 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
Amir Vadaibc3103f2016-09-08 16:23:47 +0300695 sizeof(key->enc_key_id.keyid));
696
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200697 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
698 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
699 sizeof(key->enc_tp.src));
700
701 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
702 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
703 sizeof(key->enc_tp.dst));
704
Or Gerlitzd9724772016-12-22 14:28:15 +0200705 if (tb[TCA_FLOWER_KEY_FLAGS])
706 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200707
Or Gerlitzd9724772016-12-22 14:28:15 +0200708 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +0200709}
710
711static bool fl_mask_eq(struct fl_flow_mask *mask1,
712 struct fl_flow_mask *mask2)
713{
714 const long *lmask1 = fl_key_get_start(&mask1->key, mask1);
715 const long *lmask2 = fl_key_get_start(&mask2->key, mask2);
716
717 return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) &&
718 !memcmp(lmask1, lmask2, fl_mask_range(mask1));
719}
720
721static const struct rhashtable_params fl_ht_params = {
722 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
723 .head_offset = offsetof(struct cls_fl_filter, ht_node),
724 .automatic_shrinking = true,
725};
726
727static int fl_init_hashtable(struct cls_fl_head *head,
728 struct fl_flow_mask *mask)
729{
730 head->ht_params = fl_ht_params;
731 head->ht_params.key_len = fl_mask_range(mask);
732 head->ht_params.key_offset += mask->range.start;
733
734 return rhashtable_init(&head->ht, &head->ht_params);
735}
736
737#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
738#define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
Jiri Pirko77b99002015-05-12 14:56:21 +0200739
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300740#define FL_KEY_IS_MASKED(mask, member) \
741 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
742 0, FL_KEY_MEMBER_SIZE(member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200743
744#define FL_KEY_SET(keys, cnt, id, member) \
745 do { \
746 keys[cnt].key_id = id; \
747 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
748 cnt++; \
749 } while(0);
750
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300751#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200752 do { \
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300753 if (FL_KEY_IS_MASKED(mask, member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200754 FL_KEY_SET(keys, cnt, id, member); \
755 } while(0);
756
757static void fl_init_dissector(struct cls_fl_head *head,
758 struct fl_flow_mask *mask)
759{
760 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
761 size_t cnt = 0;
762
Tom Herbert42aecaa2015-06-04 09:16:39 -0700763 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
Jiri Pirko77b99002015-05-12 14:56:21 +0200764 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300765 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
766 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
767 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
768 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
769 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
770 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
771 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
772 FLOW_DISSECTOR_KEY_PORTS, tp);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300773 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300774 FLOW_DISSECTOR_KEY_IP, ip);
775 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200776 FLOW_DISSECTOR_KEY_TCP, tcp);
777 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman7b684882016-12-07 13:48:28 +0100778 FLOW_DISSECTOR_KEY_ICMP, icmp);
779 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman99d31322017-01-11 14:05:43 +0100780 FLOW_DISSECTOR_KEY_ARP, arp);
781 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400782 FLOW_DISSECTOR_KEY_MPLS, mpls);
783 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300784 FLOW_DISSECTOR_KEY_VLAN, vlan);
Hadar Hen Zion519d1052016-11-07 15:14:38 +0200785 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
786 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
787 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
788 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
789 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
790 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
791 if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
792 FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
793 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
794 enc_control);
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200795 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
796 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
Jiri Pirko77b99002015-05-12 14:56:21 +0200797
798 skb_flow_dissector_init(&head->dissector, keys, cnt);
799}
800
801static int fl_check_assign_mask(struct cls_fl_head *head,
802 struct fl_flow_mask *mask)
803{
804 int err;
805
806 if (head->mask_assigned) {
807 if (!fl_mask_eq(&head->mask, mask))
808 return -EINVAL;
809 else
810 return 0;
811 }
812
813 /* Mask is not assigned yet. So assign it and init hashtable
814 * according to that.
815 */
816 err = fl_init_hashtable(head, mask);
817 if (err)
818 return err;
819 memcpy(&head->mask, mask, sizeof(head->mask));
820 head->mask_assigned = true;
821
822 fl_init_dissector(head, mask);
823
824 return 0;
825}
826
827static int fl_set_parms(struct net *net, struct tcf_proto *tp,
828 struct cls_fl_filter *f, struct fl_flow_mask *mask,
829 unsigned long base, struct nlattr **tb,
Alexander Aring50a56192018-01-18 11:20:52 -0500830 struct nlattr *est, bool ovr,
831 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200832{
Jiri Pirko77b99002015-05-12 14:56:21 +0200833 int err;
834
Alexander Aring50a56192018-01-18 11:20:52 -0500835 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200836 if (err < 0)
837 return err;
838
839 if (tb[TCA_FLOWER_CLASSID]) {
840 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
841 tcf_bind_filter(tp, &f->res, base);
842 }
843
844 err = fl_set_key(net, tb, &f->key, &mask->key);
845 if (err)
Jiri Pirko45507522017-08-04 14:29:06 +0200846 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +0200847
848 fl_mask_update_range(mask);
849 fl_set_masked_key(&f->mkey, &f->key, mask);
850
Jiri Pirko77b99002015-05-12 14:56:21 +0200851 return 0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200852}
853
Jiri Pirko77b99002015-05-12 14:56:21 +0200854static int fl_change(struct net *net, struct sk_buff *in_skb,
855 struct tcf_proto *tp, unsigned long base,
856 u32 handle, struct nlattr **tca,
Alexander Aring7306db32018-01-18 11:20:51 -0500857 void **arg, bool ovr, struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200858{
859 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700860 struct cls_fl_filter *fold = *arg;
Jiri Pirko77b99002015-05-12 14:56:21 +0200861 struct cls_fl_filter *fnew;
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100862 struct nlattr **tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200863 struct fl_flow_mask mask = {};
Chris Mic15ab232017-08-30 02:31:58 -0400864 unsigned long idr_index;
Jiri Pirko77b99002015-05-12 14:56:21 +0200865 int err;
866
867 if (!tca[TCA_OPTIONS])
868 return -EINVAL;
869
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100870 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
871 if (!tb)
872 return -ENOBUFS;
873
Johannes Bergfceb6432017-04-12 14:34:07 +0200874 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
875 fl_policy, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +0200876 if (err < 0)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100877 goto errout_tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200878
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100879 if (fold && handle && fold->handle != handle) {
880 err = -EINVAL;
881 goto errout_tb;
882 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200883
884 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100885 if (!fnew) {
886 err = -ENOBUFS;
887 goto errout_tb;
888 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200889
WANG Congb9a24bb2016-08-19 12:36:54 -0700890 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
891 if (err < 0)
892 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +0200893
894 if (!handle) {
Chris Mic15ab232017-08-30 02:31:58 -0400895 err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
896 1, 0x80000000, GFP_KERNEL);
897 if (err)
Jiri Pirko77b99002015-05-12 14:56:21 +0200898 goto errout;
Chris Mic15ab232017-08-30 02:31:58 -0400899 fnew->handle = idr_index;
Jiri Pirko77b99002015-05-12 14:56:21 +0200900 }
Chris Mic15ab232017-08-30 02:31:58 -0400901
902 /* user specifies a handle and it doesn't exist */
903 if (handle && !fold) {
904 err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
905 handle, handle + 1, GFP_KERNEL);
906 if (err)
907 goto errout;
908 fnew->handle = idr_index;
909 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200910
Amir Vadaie69985c2016-06-05 17:11:18 +0300911 if (tb[TCA_FLOWER_FLAGS]) {
912 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
913
914 if (!tc_flags_valid(fnew->flags)) {
915 err = -EINVAL;
Cong Wangfe2502e2017-09-20 09:18:45 -0700916 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +0300917 }
918 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200919
Alexander Aring50a56192018-01-18 11:20:52 -0500920 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr,
921 extack);
Jiri Pirko77b99002015-05-12 14:56:21 +0200922 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700923 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +0200924
925 err = fl_check_assign_mask(head, &mask);
926 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700927 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +0200928
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300929 if (!tc_skip_sw(fnew->flags)) {
Paul Blakeya3308d82017-01-16 10:45:13 +0200930 if (!fold && fl_lookup(head, &fnew->mkey)) {
931 err = -EEXIST;
Cong Wangfe2502e2017-09-20 09:18:45 -0700932 goto errout_idr;
Paul Blakeya3308d82017-01-16 10:45:13 +0200933 }
934
Amir Vadaie69985c2016-06-05 17:11:18 +0300935 err = rhashtable_insert_fast(&head->ht, &fnew->ht_node,
936 head->ht_params);
937 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700938 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +0300939 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200940
Hadar Hen Zion79685212016-12-01 14:06:34 +0200941 if (!tc_skip_hw(fnew->flags)) {
942 err = fl_hw_replace_filter(tp,
943 &head->dissector,
944 &mask.key,
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200945 fnew);
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))
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200958 fl_hw_destroy_filter(tp, fold);
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) {
Chris Mic15ab232017-08-30 02:31:58 -0400964 fnew->handle = handle;
965 idr_replace_ext(&head->handle_idr, fnew, fnew->handle);
Daniel Borkmannff3532f2015-07-17 22:38:44 +0200966 list_replace_rcu(&fold->list, &fnew->list);
Jiri Pirko77b99002015-05-12 14:56:21 +0200967 tcf_unbind_filter(tp, &fold->res);
Cong Wang0dadc112017-11-06 13:47:24 -0800968 tcf_exts_get_net(&fold->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +0200969 call_rcu(&fold->rcu, fl_destroy_filter);
970 } else {
971 list_add_tail_rcu(&fnew->list, &head->filters);
972 }
973
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100974 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +0200975 return 0;
976
Cong Wangfe2502e2017-09-20 09:18:45 -0700977errout_idr:
978 if (fnew->handle)
979 idr_remove_ext(&head->handle_idr, fnew->handle);
Jiri Pirko77b99002015-05-12 14:56:21 +0200980errout:
WANG Congb9a24bb2016-08-19 12:36:54 -0700981 tcf_exts_destroy(&fnew->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +0200982 kfree(fnew);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100983errout_tb:
984 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +0200985 return err;
986}
987
Alexander Aring571acf22018-01-18 11:20:53 -0500988static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
989 struct netlink_ext_ack *extack)
Jiri Pirko77b99002015-05-12 14:56:21 +0200990{
991 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700992 struct cls_fl_filter *f = arg;
Jiri Pirko77b99002015-05-12 14:56:21 +0200993
Jiri Pirko725cbb622016-11-28 15:40:13 +0100994 if (!tc_skip_sw(f->flags))
995 rhashtable_remove_fast(&head->ht, &f->ht_node,
996 head->ht_params);
Roi Dayan13fa8762016-11-01 16:08:29 +0200997 __fl_delete(tp, f);
WANG Cong763dbf62017-04-19 14:21:21 -0700998 *last = list_empty(&head->filters);
Jiri Pirko77b99002015-05-12 14:56:21 +0200999 return 0;
1000}
1001
1002static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1003{
1004 struct cls_fl_head *head = rtnl_dereference(tp->root);
1005 struct cls_fl_filter *f;
1006
1007 list_for_each_entry_rcu(f, &head->filters, list) {
1008 if (arg->count < arg->skip)
1009 goto skip;
WANG Cong8113c092017-08-04 21:31:43 -07001010 if (arg->fn(tp, f, arg) < 0) {
Jiri Pirko77b99002015-05-12 14:56:21 +02001011 arg->stop = 1;
1012 break;
1013 }
1014skip:
1015 arg->count++;
1016 }
1017}
1018
1019static int fl_dump_key_val(struct sk_buff *skb,
1020 void *val, int val_type,
1021 void *mask, int mask_type, int len)
1022{
1023 int err;
1024
1025 if (!memchr_inv(mask, 0, len))
1026 return 0;
1027 err = nla_put(skb, val_type, len, val);
1028 if (err)
1029 return err;
1030 if (mask_type != TCA_FLOWER_UNSPEC) {
1031 err = nla_put(skb, mask_type, len, mask);
1032 if (err)
1033 return err;
1034 }
1035 return 0;
1036}
1037
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001038static int fl_dump_key_mpls(struct sk_buff *skb,
1039 struct flow_dissector_key_mpls *mpls_key,
1040 struct flow_dissector_key_mpls *mpls_mask)
1041{
1042 int err;
1043
1044 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1045 return 0;
1046 if (mpls_mask->mpls_ttl) {
1047 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1048 mpls_key->mpls_ttl);
1049 if (err)
1050 return err;
1051 }
1052 if (mpls_mask->mpls_tc) {
1053 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1054 mpls_key->mpls_tc);
1055 if (err)
1056 return err;
1057 }
1058 if (mpls_mask->mpls_label) {
1059 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1060 mpls_key->mpls_label);
1061 if (err)
1062 return err;
1063 }
1064 if (mpls_mask->mpls_bos) {
1065 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1066 mpls_key->mpls_bos);
1067 if (err)
1068 return err;
1069 }
1070 return 0;
1071}
1072
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001073static int fl_dump_key_ip(struct sk_buff *skb,
1074 struct flow_dissector_key_ip *key,
1075 struct flow_dissector_key_ip *mask)
1076{
1077 if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos,
1078 TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) ||
1079 fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl,
1080 TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl)))
1081 return -1;
1082
1083 return 0;
1084}
1085
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001086static int fl_dump_key_vlan(struct sk_buff *skb,
1087 struct flow_dissector_key_vlan *vlan_key,
1088 struct flow_dissector_key_vlan *vlan_mask)
1089{
1090 int err;
1091
1092 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1093 return 0;
1094 if (vlan_mask->vlan_id) {
1095 err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID,
1096 vlan_key->vlan_id);
1097 if (err)
1098 return err;
1099 }
1100 if (vlan_mask->vlan_priority) {
1101 err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO,
1102 vlan_key->vlan_priority);
1103 if (err)
1104 return err;
1105 }
1106 return 0;
1107}
1108
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001109static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1110 u32 *flower_key, u32 *flower_mask,
1111 u32 flower_flag_bit, u32 dissector_flag_bit)
1112{
1113 if (dissector_mask & dissector_flag_bit) {
1114 *flower_mask |= flower_flag_bit;
1115 if (dissector_key & dissector_flag_bit)
1116 *flower_key |= flower_flag_bit;
1117 }
1118}
1119
1120static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1121{
1122 u32 key, mask;
1123 __be32 _key, _mask;
1124 int err;
1125
1126 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1127 return 0;
1128
1129 key = 0;
1130 mask = 0;
1131
1132 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1133 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1134
1135 _key = cpu_to_be32(key);
1136 _mask = cpu_to_be32(mask);
1137
1138 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1139 if (err)
1140 return err;
1141
1142 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1143}
1144
WANG Cong8113c092017-08-04 21:31:43 -07001145static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jiri Pirko77b99002015-05-12 14:56:21 +02001146 struct sk_buff *skb, struct tcmsg *t)
1147{
1148 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -07001149 struct cls_fl_filter *f = fh;
Jiri Pirko77b99002015-05-12 14:56:21 +02001150 struct nlattr *nest;
1151 struct fl_flow_key *key, *mask;
1152
1153 if (!f)
1154 return skb->len;
1155
1156 t->tcm_handle = f->handle;
1157
1158 nest = nla_nest_start(skb, TCA_OPTIONS);
1159 if (!nest)
1160 goto nla_put_failure;
1161
1162 if (f->res.classid &&
1163 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1164 goto nla_put_failure;
1165
1166 key = &f->key;
1167 mask = &head->mask.key;
1168
1169 if (mask->indev_ifindex) {
1170 struct net_device *dev;
1171
1172 dev = __dev_get_by_index(net, key->indev_ifindex);
1173 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1174 goto nla_put_failure;
1175 }
1176
Hadar Hen Zion79685212016-12-01 14:06:34 +02001177 if (!tc_skip_hw(f->flags))
1178 fl_hw_update_stats(tp, f);
Amir Vadai10cbc682016-05-13 12:55:37 +00001179
Jiri Pirko77b99002015-05-12 14:56:21 +02001180 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1181 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1182 sizeof(key->eth.dst)) ||
1183 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1184 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1185 sizeof(key->eth.src)) ||
1186 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1187 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1188 sizeof(key->basic.n_proto)))
1189 goto nla_put_failure;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001190
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001191 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1192 goto nla_put_failure;
1193
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001194 if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan))
1195 goto nla_put_failure;
1196
Jiri Pirko77b99002015-05-12 14:56:21 +02001197 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1198 key->basic.n_proto == htons(ETH_P_IPV6)) &&
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001199 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
Jiri Pirko77b99002015-05-12 14:56:21 +02001200 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001201 sizeof(key->basic.ip_proto)) ||
1202 fl_dump_key_ip(skb, &key->ip, &mask->ip)))
Jiri Pirko77b99002015-05-12 14:56:21 +02001203 goto nla_put_failure;
1204
Tom Herbertc3f83242015-06-04 09:16:40 -07001205 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001206 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1207 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1208 sizeof(key->ipv4.src)) ||
1209 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1210 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1211 sizeof(key->ipv4.dst))))
1212 goto nla_put_failure;
Tom Herbertc3f83242015-06-04 09:16:40 -07001213 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001214 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1215 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1216 sizeof(key->ipv6.src)) ||
1217 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1218 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1219 sizeof(key->ipv6.dst))))
1220 goto nla_put_failure;
1221
1222 if (key->basic.ip_proto == IPPROTO_TCP &&
1223 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001224 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001225 sizeof(key->tp.src)) ||
1226 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001227 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001228 sizeof(key->tp.dst)) ||
1229 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1230 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1231 sizeof(key->tcp.flags))))
Jiri Pirko77b99002015-05-12 14:56:21 +02001232 goto nla_put_failure;
1233 else if (key->basic.ip_proto == IPPROTO_UDP &&
1234 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001235 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001236 sizeof(key->tp.src)) ||
1237 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001238 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001239 sizeof(key->tp.dst))))
1240 goto nla_put_failure;
Simon Horman5976c5f2016-11-03 13:24:21 +01001241 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1242 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1243 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1244 sizeof(key->tp.src)) ||
1245 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1246 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1247 sizeof(key->tp.dst))))
1248 goto nla_put_failure;
Simon Horman7b684882016-12-07 13:48:28 +01001249 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1250 key->basic.ip_proto == IPPROTO_ICMP &&
1251 (fl_dump_key_val(skb, &key->icmp.type,
1252 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1253 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1254 sizeof(key->icmp.type)) ||
1255 fl_dump_key_val(skb, &key->icmp.code,
1256 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1257 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1258 sizeof(key->icmp.code))))
1259 goto nla_put_failure;
1260 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1261 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1262 (fl_dump_key_val(skb, &key->icmp.type,
1263 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1264 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1265 sizeof(key->icmp.type)) ||
1266 fl_dump_key_val(skb, &key->icmp.code,
1267 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1268 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1269 sizeof(key->icmp.code))))
1270 goto nla_put_failure;
Simon Horman99d31322017-01-11 14:05:43 +01001271 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1272 key->basic.n_proto == htons(ETH_P_RARP)) &&
1273 (fl_dump_key_val(skb, &key->arp.sip,
1274 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1275 TCA_FLOWER_KEY_ARP_SIP_MASK,
1276 sizeof(key->arp.sip)) ||
1277 fl_dump_key_val(skb, &key->arp.tip,
1278 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1279 TCA_FLOWER_KEY_ARP_TIP_MASK,
1280 sizeof(key->arp.tip)) ||
1281 fl_dump_key_val(skb, &key->arp.op,
1282 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1283 TCA_FLOWER_KEY_ARP_OP_MASK,
1284 sizeof(key->arp.op)) ||
1285 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1286 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1287 sizeof(key->arp.sha)) ||
1288 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1289 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1290 sizeof(key->arp.tha))))
1291 goto nla_put_failure;
Jiri Pirko77b99002015-05-12 14:56:21 +02001292
Amir Vadaibc3103f2016-09-08 16:23:47 +03001293 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1294 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1295 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1296 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1297 sizeof(key->enc_ipv4.src)) ||
1298 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1299 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1300 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1301 sizeof(key->enc_ipv4.dst))))
1302 goto nla_put_failure;
1303 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1304 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1305 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1306 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1307 sizeof(key->enc_ipv6.src)) ||
1308 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1309 TCA_FLOWER_KEY_ENC_IPV6_DST,
1310 &mask->enc_ipv6.dst,
1311 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1312 sizeof(key->enc_ipv6.dst))))
1313 goto nla_put_failure;
1314
1315 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001316 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001317 sizeof(key->enc_key_id)) ||
1318 fl_dump_key_val(skb, &key->enc_tp.src,
1319 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1320 &mask->enc_tp.src,
1321 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1322 sizeof(key->enc_tp.src)) ||
1323 fl_dump_key_val(skb, &key->enc_tp.dst,
1324 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1325 &mask->enc_tp.dst,
1326 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1327 sizeof(key->enc_tp.dst)))
Amir Vadaibc3103f2016-09-08 16:23:47 +03001328 goto nla_put_failure;
1329
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001330 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1331 goto nla_put_failure;
1332
Or Gerlitz749e6722017-02-16 10:31:10 +02001333 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1334 goto nla_put_failure;
Amir Vadaie69985c2016-06-05 17:11:18 +03001335
Jiri Pirko77b99002015-05-12 14:56:21 +02001336 if (tcf_exts_dump(skb, &f->exts))
1337 goto nla_put_failure;
1338
1339 nla_nest_end(skb, nest);
1340
1341 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1342 goto nla_put_failure;
1343
1344 return skb->len;
1345
1346nla_put_failure:
1347 nla_nest_cancel(skb, nest);
1348 return -1;
1349}
1350
Cong Wang07d79fc2017-08-30 14:30:36 -07001351static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
1352{
1353 struct cls_fl_filter *f = fh;
1354
1355 if (f && f->res.classid == classid)
1356 f->res.class = cl;
1357}
1358
Jiri Pirko77b99002015-05-12 14:56:21 +02001359static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1360 .kind = "flower",
1361 .classify = fl_classify,
1362 .init = fl_init,
1363 .destroy = fl_destroy,
1364 .get = fl_get,
1365 .change = fl_change,
1366 .delete = fl_delete,
1367 .walk = fl_walk,
1368 .dump = fl_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07001369 .bind_class = fl_bind_class,
Jiri Pirko77b99002015-05-12 14:56:21 +02001370 .owner = THIS_MODULE,
1371};
1372
1373static int __init cls_fl_init(void)
1374{
1375 return register_tcf_proto_ops(&cls_fl_ops);
1376}
1377
1378static void __exit cls_fl_exit(void)
1379{
1380 unregister_tcf_proto_ops(&cls_fl_ops);
1381}
1382
1383module_init(cls_fl_init);
1384module_exit(cls_fl_exit);
1385
1386MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1387MODULE_DESCRIPTION("Flower classifier");
1388MODULE_LICENSE("GPL v2");