blob: fb74a47830f4a623bd094668197fa2949337e65b [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;
Jiri Pirko77b99002015-05-12 14:56:21 +020053} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
54
55struct fl_flow_mask_range {
56 unsigned short int start;
57 unsigned short int end;
58};
59
60struct fl_flow_mask {
61 struct fl_flow_key key;
62 struct fl_flow_mask_range range;
63 struct rcu_head rcu;
64};
65
66struct cls_fl_head {
67 struct rhashtable ht;
68 struct fl_flow_mask mask;
69 struct flow_dissector dissector;
70 u32 hgen;
71 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 };
Jiri Pirko77b99002015-05-12 14:56:21 +020078};
79
80struct cls_fl_filter {
81 struct rhash_head ht_node;
82 struct fl_flow_key mkey;
83 struct tcf_exts exts;
84 struct tcf_result res;
85 struct fl_flow_key key;
86 struct list_head list;
87 u32 handle;
Amir Vadaie69985c2016-06-05 17:11:18 +030088 u32 flags;
Jiri Pirko77b99002015-05-12 14:56:21 +020089 struct rcu_head rcu;
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +020090 struct tc_to_netdev tc;
91 struct net_device *hw_dev;
Jiri Pirko77b99002015-05-12 14:56:21 +020092};
93
94static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
95{
96 return mask->range.end - mask->range.start;
97}
98
99static void fl_mask_update_range(struct fl_flow_mask *mask)
100{
101 const u8 *bytes = (const u8 *) &mask->key;
102 size_t size = sizeof(mask->key);
103 size_t i, first = 0, last = size - 1;
104
105 for (i = 0; i < sizeof(mask->key); i++) {
106 if (bytes[i]) {
107 if (!first && i)
108 first = i;
109 last = i;
110 }
111 }
112 mask->range.start = rounddown(first, sizeof(long));
113 mask->range.end = roundup(last + 1, sizeof(long));
114}
115
116static void *fl_key_get_start(struct fl_flow_key *key,
117 const struct fl_flow_mask *mask)
118{
119 return (u8 *) key + mask->range.start;
120}
121
122static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
123 struct fl_flow_mask *mask)
124{
125 const long *lkey = fl_key_get_start(key, mask);
126 const long *lmask = fl_key_get_start(&mask->key, mask);
127 long *lmkey = fl_key_get_start(mkey, mask);
128 int i;
129
130 for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
131 *lmkey++ = *lkey++ & *lmask++;
132}
133
134static void fl_clear_masked_range(struct fl_flow_key *key,
135 struct fl_flow_mask *mask)
136{
137 memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
138}
139
Paul Blakeya3308d82017-01-16 10:45:13 +0200140static struct cls_fl_filter *fl_lookup(struct cls_fl_head *head,
141 struct fl_flow_key *mkey)
142{
143 return rhashtable_lookup_fast(&head->ht,
144 fl_key_get_start(mkey, &head->mask),
145 head->ht_params);
146}
147
Jiri Pirko77b99002015-05-12 14:56:21 +0200148static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
149 struct tcf_result *res)
150{
151 struct cls_fl_head *head = rcu_dereference_bh(tp->root);
152 struct cls_fl_filter *f;
153 struct fl_flow_key skb_key;
154 struct fl_flow_key skb_mkey;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300155 struct ip_tunnel_info *info;
Jiri Pirko77b99002015-05-12 14:56:21 +0200156
Amir Vadaie69985c2016-06-05 17:11:18 +0300157 if (!atomic_read(&head->ht.nelems))
158 return -1;
159
Jiri Pirko77b99002015-05-12 14:56:21 +0200160 fl_clear_masked_range(&skb_key, &head->mask);
Amir Vadaibc3103f2016-09-08 16:23:47 +0300161
162 info = skb_tunnel_info(skb);
163 if (info) {
164 struct ip_tunnel_key *key = &info->key;
165
166 switch (ip_tunnel_info_af(info)) {
167 case AF_INET:
Paul Blakey0df0f202016-12-28 14:54:47 +0200168 skb_key.enc_control.addr_type =
169 FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300170 skb_key.enc_ipv4.src = key->u.ipv4.src;
171 skb_key.enc_ipv4.dst = key->u.ipv4.dst;
172 break;
173 case AF_INET6:
Paul Blakey0df0f202016-12-28 14:54:47 +0200174 skb_key.enc_control.addr_type =
175 FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300176 skb_key.enc_ipv6.src = key->u.ipv6.src;
177 skb_key.enc_ipv6.dst = key->u.ipv6.dst;
178 break;
179 }
180
181 skb_key.enc_key_id.keyid = tunnel_id_to_key32(key->tun_id);
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200182 skb_key.enc_tp.src = key->tp_src;
183 skb_key.enc_tp.dst = key->tp_dst;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300184 }
185
Jiri Pirko77b99002015-05-12 14:56:21 +0200186 skb_key.indev_ifindex = skb->skb_iif;
187 /* skb_flow_dissect() does not set n_proto in case an unknown protocol,
188 * so do it rather here.
189 */
190 skb_key.basic.n_proto = skb->protocol;
Tom Herbertcd79a232015-09-01 09:24:27 -0700191 skb_flow_dissect(skb, &head->dissector, &skb_key, 0);
Jiri Pirko77b99002015-05-12 14:56:21 +0200192
193 fl_set_masked_key(&skb_mkey, &skb_key, &head->mask);
194
Paul Blakeya3308d82017-01-16 10:45:13 +0200195 f = fl_lookup(head, &skb_mkey);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300196 if (f && !tc_skip_sw(f->flags)) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200197 *res = f->res;
198 return tcf_exts_exec(skb, &f->exts, res);
199 }
200 return -1;
201}
202
203static int fl_init(struct tcf_proto *tp)
204{
205 struct cls_fl_head *head;
206
207 head = kzalloc(sizeof(*head), GFP_KERNEL);
208 if (!head)
209 return -ENOBUFS;
210
211 INIT_LIST_HEAD_RCU(&head->filters);
212 rcu_assign_pointer(tp->root, head);
213
214 return 0;
215}
216
217static void fl_destroy_filter(struct rcu_head *head)
218{
219 struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);
220
221 tcf_exts_destroy(&f->exts);
222 kfree(f);
223}
224
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200225static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f)
Amir Vadai5b33f482016-03-08 12:42:29 +0200226{
Amir Vadai5b33f482016-03-08 12:42:29 +0200227 struct tc_cls_flower_offload offload = {0};
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200228 struct net_device *dev = f->hw_dev;
229 struct tc_to_netdev *tc = &f->tc;
Amir Vadai5b33f482016-03-08 12:42:29 +0200230
Hadar Hen Zion79685212016-12-01 14:06:34 +0200231 if (!tc_can_offload(dev, tp))
Amir Vadai5b33f482016-03-08 12:42:29 +0200232 return;
233
234 offload.command = TC_CLSFLOWER_DESTROY;
Jiri Pirko69ca05c2017-02-03 10:29:08 +0100235 offload.prio = tp->prio;
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200236 offload.cookie = (unsigned long)f;
Amir Vadai5b33f482016-03-08 12:42:29 +0200237
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200238 tc->type = TC_SETUP_CLSFLOWER;
239 tc->cls_flower = &offload;
Amir Vadai5b33f482016-03-08 12:42:29 +0200240
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200241 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, tc);
Amir Vadai5b33f482016-03-08 12:42:29 +0200242}
243
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300244static int fl_hw_replace_filter(struct tcf_proto *tp,
245 struct flow_dissector *dissector,
246 struct fl_flow_key *mask,
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200247 struct cls_fl_filter *f)
Amir Vadai5b33f482016-03-08 12:42:29 +0200248{
249 struct net_device *dev = tp->q->dev_queue->dev;
250 struct tc_cls_flower_offload offload = {0};
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200251 struct tc_to_netdev *tc = &f->tc;
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300252 int err;
Amir Vadai5b33f482016-03-08 12:42:29 +0200253
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200254 if (!tc_can_offload(dev, tp)) {
Hadar Hen Ziona6e16932016-12-04 15:25:19 +0200255 if (tcf_exts_get_dev(dev, &f->exts, &f->hw_dev) ||
256 (f->hw_dev && !tc_can_offload(f->hw_dev, tp))) {
257 f->hw_dev = dev;
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200258 return tc_skip_sw(f->flags) ? -EINVAL : 0;
Hadar Hen Ziona6e16932016-12-04 15:25:19 +0200259 }
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200260 dev = f->hw_dev;
261 tc->egress_dev = true;
262 } else {
263 f->hw_dev = dev;
264 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200265
266 offload.command = TC_CLSFLOWER_REPLACE;
Jiri Pirko69ca05c2017-02-03 10:29:08 +0100267 offload.prio = tp->prio;
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200268 offload.cookie = (unsigned long)f;
Amir Vadai5b33f482016-03-08 12:42:29 +0200269 offload.dissector = dissector;
270 offload.mask = mask;
Paul Blakeyf93bd172016-12-14 19:00:58 +0200271 offload.key = &f->mkey;
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200272 offload.exts = &f->exts;
Amir Vadai5b33f482016-03-08 12:42:29 +0200273
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200274 tc->type = TC_SETUP_CLSFLOWER;
275 tc->cls_flower = &offload;
Amir Vadai5b33f482016-03-08 12:42:29 +0200276
Jamal Hadi Salim5a7a5552016-09-18 08:45:33 -0400277 err = dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol,
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200278 tc);
Or Gerlitz55593962017-02-16 10:31:13 +0200279 if (!err)
280 f->flags |= TCA_CLS_FLAGS_IN_HW;
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300281
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200282 if (tc_skip_sw(f->flags))
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300283 return err;
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300284 return 0;
Amir Vadai5b33f482016-03-08 12:42:29 +0200285}
286
Amir Vadai10cbc682016-05-13 12:55:37 +0000287static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
288{
Amir Vadai10cbc682016-05-13 12:55:37 +0000289 struct tc_cls_flower_offload offload = {0};
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200290 struct net_device *dev = f->hw_dev;
291 struct tc_to_netdev *tc = &f->tc;
Amir Vadai10cbc682016-05-13 12:55:37 +0000292
Hadar Hen Zion79685212016-12-01 14:06:34 +0200293 if (!tc_can_offload(dev, tp))
Amir Vadai10cbc682016-05-13 12:55:37 +0000294 return;
295
296 offload.command = TC_CLSFLOWER_STATS;
Jiri Pirko69ca05c2017-02-03 10:29:08 +0100297 offload.prio = tp->prio;
Amir Vadai10cbc682016-05-13 12:55:37 +0000298 offload.cookie = (unsigned long)f;
299 offload.exts = &f->exts;
300
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200301 tc->type = TC_SETUP_CLSFLOWER;
302 tc->cls_flower = &offload;
Amir Vadai10cbc682016-05-13 12:55:37 +0000303
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200304 dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, tp->protocol, tc);
Amir Vadai10cbc682016-05-13 12:55:37 +0000305}
306
Roi Dayan13fa8762016-11-01 16:08:29 +0200307static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f)
308{
309 list_del_rcu(&f->list);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200310 if (!tc_skip_hw(f->flags))
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200311 fl_hw_destroy_filter(tp, f);
Roi Dayan13fa8762016-11-01 16:08:29 +0200312 tcf_unbind_filter(tp, &f->res);
313 call_rcu(&f->rcu, fl_destroy_filter);
314}
315
Daniel Borkmannd9363772016-11-27 01:18:01 +0100316static void fl_destroy_sleepable(struct work_struct *work)
317{
318 struct cls_fl_head *head = container_of(work, struct cls_fl_head,
319 work);
320 if (head->mask_assigned)
321 rhashtable_destroy(&head->ht);
322 kfree(head);
323 module_put(THIS_MODULE);
324}
325
326static void fl_destroy_rcu(struct rcu_head *rcu)
327{
328 struct cls_fl_head *head = container_of(rcu, struct cls_fl_head, rcu);
329
330 INIT_WORK(&head->work, fl_destroy_sleepable);
331 schedule_work(&head->work);
332}
333
WANG Cong763dbf62017-04-19 14:21:21 -0700334static void fl_destroy(struct tcf_proto *tp)
Jiri Pirko77b99002015-05-12 14:56:21 +0200335{
336 struct cls_fl_head *head = rtnl_dereference(tp->root);
337 struct cls_fl_filter *f, *next;
338
Roi Dayan13fa8762016-11-01 16:08:29 +0200339 list_for_each_entry_safe(f, next, &head->filters, list)
340 __fl_delete(tp, f);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100341
342 __module_get(THIS_MODULE);
343 call_rcu(&head->rcu, fl_destroy_rcu);
Jiri Pirko77b99002015-05-12 14:56:21 +0200344}
345
346static unsigned long fl_get(struct tcf_proto *tp, u32 handle)
347{
348 struct cls_fl_head *head = rtnl_dereference(tp->root);
349 struct cls_fl_filter *f;
350
351 list_for_each_entry(f, &head->filters, list)
352 if (f->handle == handle)
353 return (unsigned long) f;
354 return 0;
355}
356
357static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
358 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
359 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
360 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
361 .len = IFNAMSIZ },
362 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
363 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
364 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
365 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
366 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
367 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
368 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
369 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
370 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
371 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
372 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
373 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
374 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
375 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
376 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
377 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
Jamal Hadi Salimb175c3a2015-06-25 06:55:27 -0400378 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
379 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300380 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
381 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
382 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
Amir Vadaibc3103f2016-09-08 16:23:47 +0300383 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
384 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
385 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
386 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
387 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
388 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
389 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
390 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
391 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
Or Gerlitzaa72d702016-09-15 15:28:22 +0300392 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
393 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
394 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
395 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
Simon Horman5976c5f2016-11-03 13:24:21 +0100396 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
397 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
398 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
399 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200400 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
401 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
402 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
403 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200404 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
405 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
Simon Horman7b684882016-12-07 13:48:28 +0100406 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
407 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
408 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
409 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
410 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
411 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
412 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
413 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
Simon Horman99d31322017-01-11 14:05:43 +0100414 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
415 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
416 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
417 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
418 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
419 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
420 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
421 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
422 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
423 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400424 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
425 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
426 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
427 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200428 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
429 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
Jiri Pirko77b99002015-05-12 14:56:21 +0200430};
431
432static void fl_set_key_val(struct nlattr **tb,
433 void *val, int val_type,
434 void *mask, int mask_type, int len)
435{
436 if (!tb[val_type])
437 return;
438 memcpy(val, nla_data(tb[val_type]), len);
439 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
440 memset(mask, 0xff, len);
441 else
442 memcpy(mask, nla_data(tb[mask_type]), len);
443}
444
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400445static int fl_set_key_mpls(struct nlattr **tb,
446 struct flow_dissector_key_mpls *key_val,
447 struct flow_dissector_key_mpls *key_mask)
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400448{
449 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
450 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
451 key_mask->mpls_ttl = MPLS_TTL_MASK;
452 }
453 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400454 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
455
456 if (bos & ~MPLS_BOS_MASK)
457 return -EINVAL;
458 key_val->mpls_bos = bos;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400459 key_mask->mpls_bos = MPLS_BOS_MASK;
460 }
461 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400462 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
463
464 if (tc & ~MPLS_TC_MASK)
465 return -EINVAL;
466 key_val->mpls_tc = tc;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400467 key_mask->mpls_tc = MPLS_TC_MASK;
468 }
469 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400470 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
471
472 if (label & ~MPLS_LABEL_MASK)
473 return -EINVAL;
474 key_val->mpls_label = label;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400475 key_mask->mpls_label = MPLS_LABEL_MASK;
476 }
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400477 return 0;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400478}
479
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300480static void fl_set_key_vlan(struct nlattr **tb,
481 struct flow_dissector_key_vlan *key_val,
482 struct flow_dissector_key_vlan *key_mask)
483{
484#define VLAN_PRIORITY_MASK 0x7
485
486 if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
487 key_val->vlan_id =
488 nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
489 key_mask->vlan_id = VLAN_VID_MASK;
490 }
491 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
492 key_val->vlan_priority =
493 nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
494 VLAN_PRIORITY_MASK;
495 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
496 }
497}
498
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200499static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
500 u32 *dissector_key, u32 *dissector_mask,
501 u32 flower_flag_bit, u32 dissector_flag_bit)
502{
503 if (flower_mask & flower_flag_bit) {
504 *dissector_mask |= dissector_flag_bit;
505 if (flower_key & flower_flag_bit)
506 *dissector_key |= dissector_flag_bit;
507 }
508}
509
Or Gerlitzd9724772016-12-22 14:28:15 +0200510static int fl_set_key_flags(struct nlattr **tb,
511 u32 *flags_key, u32 *flags_mask)
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200512{
513 u32 key, mask;
514
Or Gerlitzd9724772016-12-22 14:28:15 +0200515 /* mask is mandatory for flags */
516 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
517 return -EINVAL;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200518
519 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
Or Gerlitzd9724772016-12-22 14:28:15 +0200520 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200521
522 *flags_key = 0;
523 *flags_mask = 0;
524
525 fl_set_key_flag(key, mask, flags_key, flags_mask,
526 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Or Gerlitzd9724772016-12-22 14:28:15 +0200527
528 return 0;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200529}
530
Jiri Pirko77b99002015-05-12 14:56:21 +0200531static int fl_set_key(struct net *net, struct nlattr **tb,
532 struct fl_flow_key *key, struct fl_flow_key *mask)
533{
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300534 __be16 ethertype;
Or Gerlitzd9724772016-12-22 14:28:15 +0200535 int ret = 0;
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400536#ifdef CONFIG_NET_CLS_IND
Jiri Pirko77b99002015-05-12 14:56:21 +0200537 if (tb[TCA_FLOWER_INDEV]) {
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400538 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV]);
Jiri Pirko77b99002015-05-12 14:56:21 +0200539 if (err < 0)
540 return err;
541 key->indev_ifindex = err;
542 mask->indev_ifindex = 0xffffffff;
543 }
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400544#endif
Jiri Pirko77b99002015-05-12 14:56:21 +0200545
546 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
547 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
548 sizeof(key->eth.dst));
549 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
550 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
551 sizeof(key->eth.src));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500552
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200553 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300554 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
555
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200556 if (ethertype == htons(ETH_P_8021Q)) {
557 fl_set_key_vlan(tb, &key->vlan, &mask->vlan);
558 fl_set_key_val(tb, &key->basic.n_proto,
559 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
560 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
561 sizeof(key->basic.n_proto));
562 } else {
563 key->basic.n_proto = ethertype;
564 mask->basic.n_proto = cpu_to_be16(~0);
565 }
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300566 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500567
Jiri Pirko77b99002015-05-12 14:56:21 +0200568 if (key->basic.n_proto == htons(ETH_P_IP) ||
569 key->basic.n_proto == htons(ETH_P_IPV6)) {
570 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
571 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
572 sizeof(key->basic.ip_proto));
573 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500574
575 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
576 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200577 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200578 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
579 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
580 sizeof(key->ipv4.src));
581 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
582 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
583 sizeof(key->ipv4.dst));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500584 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
585 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200586 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200587 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
588 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
589 sizeof(key->ipv6.src));
590 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
591 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
592 sizeof(key->ipv6.dst));
593 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500594
Jiri Pirko77b99002015-05-12 14:56:21 +0200595 if (key->basic.ip_proto == IPPROTO_TCP) {
596 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300597 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200598 sizeof(key->tp.src));
599 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300600 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200601 sizeof(key->tp.dst));
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200602 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
603 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
604 sizeof(key->tcp.flags));
Jiri Pirko77b99002015-05-12 14:56:21 +0200605 } else if (key->basic.ip_proto == IPPROTO_UDP) {
606 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300607 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200608 sizeof(key->tp.src));
609 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300610 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200611 sizeof(key->tp.dst));
Simon Horman5976c5f2016-11-03 13:24:21 +0100612 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
613 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
614 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
615 sizeof(key->tp.src));
616 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
617 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
618 sizeof(key->tp.dst));
Simon Horman7b684882016-12-07 13:48:28 +0100619 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
620 key->basic.ip_proto == IPPROTO_ICMP) {
621 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
622 &mask->icmp.type,
623 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
624 sizeof(key->icmp.type));
625 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
626 &mask->icmp.code,
627 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
628 sizeof(key->icmp.code));
629 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
630 key->basic.ip_proto == IPPROTO_ICMPV6) {
631 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
632 &mask->icmp.type,
633 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
634 sizeof(key->icmp.type));
Simon Horman040587a2017-01-30 16:19:02 +0100635 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
Simon Horman7b684882016-12-07 13:48:28 +0100636 &mask->icmp.code,
Simon Horman040587a2017-01-30 16:19:02 +0100637 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
Simon Horman7b684882016-12-07 13:48:28 +0100638 sizeof(key->icmp.code));
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400639 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
640 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400641 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
642 if (ret)
643 return ret;
Simon Horman99d31322017-01-11 14:05:43 +0100644 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
645 key->basic.n_proto == htons(ETH_P_RARP)) {
646 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
647 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
648 sizeof(key->arp.sip));
649 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
650 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
651 sizeof(key->arp.tip));
652 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
653 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
654 sizeof(key->arp.op));
655 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
656 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
657 sizeof(key->arp.sha));
658 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
659 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
660 sizeof(key->arp.tha));
Jiri Pirko77b99002015-05-12 14:56:21 +0200661 }
662
Amir Vadaibc3103f2016-09-08 16:23:47 +0300663 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
664 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
665 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200666 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300667 fl_set_key_val(tb, &key->enc_ipv4.src,
668 TCA_FLOWER_KEY_ENC_IPV4_SRC,
669 &mask->enc_ipv4.src,
670 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
671 sizeof(key->enc_ipv4.src));
672 fl_set_key_val(tb, &key->enc_ipv4.dst,
673 TCA_FLOWER_KEY_ENC_IPV4_DST,
674 &mask->enc_ipv4.dst,
675 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
676 sizeof(key->enc_ipv4.dst));
677 }
678
679 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
680 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
681 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200682 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300683 fl_set_key_val(tb, &key->enc_ipv6.src,
684 TCA_FLOWER_KEY_ENC_IPV6_SRC,
685 &mask->enc_ipv6.src,
686 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
687 sizeof(key->enc_ipv6.src));
688 fl_set_key_val(tb, &key->enc_ipv6.dst,
689 TCA_FLOWER_KEY_ENC_IPV6_DST,
690 &mask->enc_ipv6.dst,
691 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
692 sizeof(key->enc_ipv6.dst));
693 }
694
695 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +0300696 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
Amir Vadaibc3103f2016-09-08 16:23:47 +0300697 sizeof(key->enc_key_id.keyid));
698
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200699 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
700 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
701 sizeof(key->enc_tp.src));
702
703 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
704 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
705 sizeof(key->enc_tp.dst));
706
Or Gerlitzd9724772016-12-22 14:28:15 +0200707 if (tb[TCA_FLOWER_KEY_FLAGS])
708 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200709
Or Gerlitzd9724772016-12-22 14:28:15 +0200710 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +0200711}
712
713static bool fl_mask_eq(struct fl_flow_mask *mask1,
714 struct fl_flow_mask *mask2)
715{
716 const long *lmask1 = fl_key_get_start(&mask1->key, mask1);
717 const long *lmask2 = fl_key_get_start(&mask2->key, mask2);
718
719 return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) &&
720 !memcmp(lmask1, lmask2, fl_mask_range(mask1));
721}
722
723static const struct rhashtable_params fl_ht_params = {
724 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
725 .head_offset = offsetof(struct cls_fl_filter, ht_node),
726 .automatic_shrinking = true,
727};
728
729static int fl_init_hashtable(struct cls_fl_head *head,
730 struct fl_flow_mask *mask)
731{
732 head->ht_params = fl_ht_params;
733 head->ht_params.key_len = fl_mask_range(mask);
734 head->ht_params.key_offset += mask->range.start;
735
736 return rhashtable_init(&head->ht, &head->ht_params);
737}
738
739#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
740#define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
Jiri Pirko77b99002015-05-12 14:56:21 +0200741
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300742#define FL_KEY_IS_MASKED(mask, member) \
743 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
744 0, FL_KEY_MEMBER_SIZE(member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200745
746#define FL_KEY_SET(keys, cnt, id, member) \
747 do { \
748 keys[cnt].key_id = id; \
749 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
750 cnt++; \
751 } while(0);
752
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300753#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200754 do { \
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300755 if (FL_KEY_IS_MASKED(mask, member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200756 FL_KEY_SET(keys, cnt, id, member); \
757 } while(0);
758
759static void fl_init_dissector(struct cls_fl_head *head,
760 struct fl_flow_mask *mask)
761{
762 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
763 size_t cnt = 0;
764
Tom Herbert42aecaa2015-06-04 09:16:39 -0700765 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
Jiri Pirko77b99002015-05-12 14:56:21 +0200766 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300767 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
768 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
769 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
770 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
771 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
772 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
773 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
774 FLOW_DISSECTOR_KEY_PORTS, tp);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300775 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,
830 struct nlattr *est, bool ovr)
831{
832 struct tcf_exts e;
833 int err;
834
WANG Congb9a24bb2016-08-19 12:36:54 -0700835 err = tcf_exts_init(&e, TCA_FLOWER_ACT, 0);
Jiri Pirko77b99002015-05-12 14:56:21 +0200836 if (err < 0)
837 return err;
WANG Congb9a24bb2016-08-19 12:36:54 -0700838 err = tcf_exts_validate(net, tp, tb, est, &e, ovr);
839 if (err < 0)
840 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +0200841
842 if (tb[TCA_FLOWER_CLASSID]) {
843 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
844 tcf_bind_filter(tp, &f->res, base);
845 }
846
847 err = fl_set_key(net, tb, &f->key, &mask->key);
848 if (err)
849 goto errout;
850
851 fl_mask_update_range(mask);
852 fl_set_masked_key(&f->mkey, &f->key, mask);
853
854 tcf_exts_change(tp, &f->exts, &e);
855
856 return 0;
857errout:
858 tcf_exts_destroy(&e);
859 return err;
860}
861
862static u32 fl_grab_new_handle(struct tcf_proto *tp,
863 struct cls_fl_head *head)
864{
865 unsigned int i = 0x80000000;
866 u32 handle;
867
868 do {
869 if (++head->hgen == 0x7FFFFFFF)
870 head->hgen = 1;
871 } while (--i > 0 && fl_get(tp, head->hgen));
872
873 if (unlikely(i == 0)) {
874 pr_err("Insufficient number of handles\n");
875 handle = 0;
876 } else {
877 handle = head->hgen;
878 }
879
880 return handle;
881}
882
883static int fl_change(struct net *net, struct sk_buff *in_skb,
884 struct tcf_proto *tp, unsigned long base,
885 u32 handle, struct nlattr **tca,
886 unsigned long *arg, bool ovr)
887{
888 struct cls_fl_head *head = rtnl_dereference(tp->root);
889 struct cls_fl_filter *fold = (struct cls_fl_filter *) *arg;
890 struct cls_fl_filter *fnew;
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100891 struct nlattr **tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200892 struct fl_flow_mask mask = {};
893 int err;
894
895 if (!tca[TCA_OPTIONS])
896 return -EINVAL;
897
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100898 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
899 if (!tb)
900 return -ENOBUFS;
901
Johannes Bergfceb6432017-04-12 14:34:07 +0200902 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
903 fl_policy, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +0200904 if (err < 0)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100905 goto errout_tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200906
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100907 if (fold && handle && fold->handle != handle) {
908 err = -EINVAL;
909 goto errout_tb;
910 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200911
912 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100913 if (!fnew) {
914 err = -ENOBUFS;
915 goto errout_tb;
916 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200917
WANG Congb9a24bb2016-08-19 12:36:54 -0700918 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
919 if (err < 0)
920 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +0200921
922 if (!handle) {
923 handle = fl_grab_new_handle(tp, head);
924 if (!handle) {
925 err = -EINVAL;
926 goto errout;
927 }
928 }
929 fnew->handle = handle;
930
Amir Vadaie69985c2016-06-05 17:11:18 +0300931 if (tb[TCA_FLOWER_FLAGS]) {
932 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
933
934 if (!tc_flags_valid(fnew->flags)) {
935 err = -EINVAL;
936 goto errout;
937 }
938 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200939
Jiri Pirko77b99002015-05-12 14:56:21 +0200940 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr);
941 if (err)
942 goto errout;
943
944 err = fl_check_assign_mask(head, &mask);
945 if (err)
946 goto errout;
947
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300948 if (!tc_skip_sw(fnew->flags)) {
Paul Blakeya3308d82017-01-16 10:45:13 +0200949 if (!fold && fl_lookup(head, &fnew->mkey)) {
950 err = -EEXIST;
951 goto errout;
952 }
953
Amir Vadaie69985c2016-06-05 17:11:18 +0300954 err = rhashtable_insert_fast(&head->ht, &fnew->ht_node,
955 head->ht_params);
956 if (err)
957 goto errout;
958 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200959
Hadar Hen Zion79685212016-12-01 14:06:34 +0200960 if (!tc_skip_hw(fnew->flags)) {
961 err = fl_hw_replace_filter(tp,
962 &head->dissector,
963 &mask.key,
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200964 fnew);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200965 if (err)
966 goto errout;
967 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200968
Or Gerlitz55593962017-02-16 10:31:13 +0200969 if (!tc_in_hw(fnew->flags))
970 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
971
Amir Vadai5b33f482016-03-08 12:42:29 +0200972 if (fold) {
Jiri Pirko725cbb622016-11-28 15:40:13 +0100973 if (!tc_skip_sw(fold->flags))
974 rhashtable_remove_fast(&head->ht, &fold->ht_node,
975 head->ht_params);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200976 if (!tc_skip_hw(fold->flags))
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200977 fl_hw_destroy_filter(tp, fold);
Amir Vadai5b33f482016-03-08 12:42:29 +0200978 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200979
980 *arg = (unsigned long) fnew;
981
982 if (fold) {
Daniel Borkmannff3532f2015-07-17 22:38:44 +0200983 list_replace_rcu(&fold->list, &fnew->list);
Jiri Pirko77b99002015-05-12 14:56:21 +0200984 tcf_unbind_filter(tp, &fold->res);
985 call_rcu(&fold->rcu, fl_destroy_filter);
986 } else {
987 list_add_tail_rcu(&fnew->list, &head->filters);
988 }
989
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100990 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +0200991 return 0;
992
993errout:
WANG Congb9a24bb2016-08-19 12:36:54 -0700994 tcf_exts_destroy(&fnew->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +0200995 kfree(fnew);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100996errout_tb:
997 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +0200998 return err;
999}
1000
WANG Cong763dbf62017-04-19 14:21:21 -07001001static int fl_delete(struct tcf_proto *tp, unsigned long arg, bool *last)
Jiri Pirko77b99002015-05-12 14:56:21 +02001002{
1003 struct cls_fl_head *head = rtnl_dereference(tp->root);
1004 struct cls_fl_filter *f = (struct cls_fl_filter *) arg;
1005
Jiri Pirko725cbb622016-11-28 15:40:13 +01001006 if (!tc_skip_sw(f->flags))
1007 rhashtable_remove_fast(&head->ht, &f->ht_node,
1008 head->ht_params);
Roi Dayan13fa8762016-11-01 16:08:29 +02001009 __fl_delete(tp, f);
WANG Cong763dbf62017-04-19 14:21:21 -07001010 *last = list_empty(&head->filters);
Jiri Pirko77b99002015-05-12 14:56:21 +02001011 return 0;
1012}
1013
1014static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1015{
1016 struct cls_fl_head *head = rtnl_dereference(tp->root);
1017 struct cls_fl_filter *f;
1018
1019 list_for_each_entry_rcu(f, &head->filters, list) {
1020 if (arg->count < arg->skip)
1021 goto skip;
1022 if (arg->fn(tp, (unsigned long) f, arg) < 0) {
1023 arg->stop = 1;
1024 break;
1025 }
1026skip:
1027 arg->count++;
1028 }
1029}
1030
1031static int fl_dump_key_val(struct sk_buff *skb,
1032 void *val, int val_type,
1033 void *mask, int mask_type, int len)
1034{
1035 int err;
1036
1037 if (!memchr_inv(mask, 0, len))
1038 return 0;
1039 err = nla_put(skb, val_type, len, val);
1040 if (err)
1041 return err;
1042 if (mask_type != TCA_FLOWER_UNSPEC) {
1043 err = nla_put(skb, mask_type, len, mask);
1044 if (err)
1045 return err;
1046 }
1047 return 0;
1048}
1049
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001050static int fl_dump_key_mpls(struct sk_buff *skb,
1051 struct flow_dissector_key_mpls *mpls_key,
1052 struct flow_dissector_key_mpls *mpls_mask)
1053{
1054 int err;
1055
1056 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1057 return 0;
1058 if (mpls_mask->mpls_ttl) {
1059 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1060 mpls_key->mpls_ttl);
1061 if (err)
1062 return err;
1063 }
1064 if (mpls_mask->mpls_tc) {
1065 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1066 mpls_key->mpls_tc);
1067 if (err)
1068 return err;
1069 }
1070 if (mpls_mask->mpls_label) {
1071 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1072 mpls_key->mpls_label);
1073 if (err)
1074 return err;
1075 }
1076 if (mpls_mask->mpls_bos) {
1077 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1078 mpls_key->mpls_bos);
1079 if (err)
1080 return err;
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
Jiri Pirko77b99002015-05-12 14:56:21 +02001144static int fl_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
1145 struct sk_buff *skb, struct tcmsg *t)
1146{
1147 struct cls_fl_head *head = rtnl_dereference(tp->root);
1148 struct cls_fl_filter *f = (struct cls_fl_filter *) fh;
1149 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)) &&
1198 fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
1199 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
1200 sizeof(key->basic.ip_proto)))
1201 goto nla_put_failure;
1202
Tom Herbertc3f83242015-06-04 09:16:40 -07001203 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001204 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1205 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1206 sizeof(key->ipv4.src)) ||
1207 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1208 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1209 sizeof(key->ipv4.dst))))
1210 goto nla_put_failure;
Tom Herbertc3f83242015-06-04 09:16:40 -07001211 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001212 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1213 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1214 sizeof(key->ipv6.src)) ||
1215 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1216 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1217 sizeof(key->ipv6.dst))))
1218 goto nla_put_failure;
1219
1220 if (key->basic.ip_proto == IPPROTO_TCP &&
1221 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001222 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001223 sizeof(key->tp.src)) ||
1224 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001225 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001226 sizeof(key->tp.dst)) ||
1227 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1228 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1229 sizeof(key->tcp.flags))))
Jiri Pirko77b99002015-05-12 14:56:21 +02001230 goto nla_put_failure;
1231 else if (key->basic.ip_proto == IPPROTO_UDP &&
1232 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001233 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001234 sizeof(key->tp.src)) ||
1235 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001236 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001237 sizeof(key->tp.dst))))
1238 goto nla_put_failure;
Simon Horman5976c5f2016-11-03 13:24:21 +01001239 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1240 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1241 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1242 sizeof(key->tp.src)) ||
1243 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1244 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1245 sizeof(key->tp.dst))))
1246 goto nla_put_failure;
Simon Horman7b684882016-12-07 13:48:28 +01001247 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1248 key->basic.ip_proto == IPPROTO_ICMP &&
1249 (fl_dump_key_val(skb, &key->icmp.type,
1250 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1251 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1252 sizeof(key->icmp.type)) ||
1253 fl_dump_key_val(skb, &key->icmp.code,
1254 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1255 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1256 sizeof(key->icmp.code))))
1257 goto nla_put_failure;
1258 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1259 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1260 (fl_dump_key_val(skb, &key->icmp.type,
1261 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1262 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1263 sizeof(key->icmp.type)) ||
1264 fl_dump_key_val(skb, &key->icmp.code,
1265 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1266 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1267 sizeof(key->icmp.code))))
1268 goto nla_put_failure;
Simon Horman99d31322017-01-11 14:05:43 +01001269 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1270 key->basic.n_proto == htons(ETH_P_RARP)) &&
1271 (fl_dump_key_val(skb, &key->arp.sip,
1272 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1273 TCA_FLOWER_KEY_ARP_SIP_MASK,
1274 sizeof(key->arp.sip)) ||
1275 fl_dump_key_val(skb, &key->arp.tip,
1276 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1277 TCA_FLOWER_KEY_ARP_TIP_MASK,
1278 sizeof(key->arp.tip)) ||
1279 fl_dump_key_val(skb, &key->arp.op,
1280 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1281 TCA_FLOWER_KEY_ARP_OP_MASK,
1282 sizeof(key->arp.op)) ||
1283 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1284 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1285 sizeof(key->arp.sha)) ||
1286 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1287 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1288 sizeof(key->arp.tha))))
1289 goto nla_put_failure;
Jiri Pirko77b99002015-05-12 14:56:21 +02001290
Amir Vadaibc3103f2016-09-08 16:23:47 +03001291 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1292 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1293 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1294 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1295 sizeof(key->enc_ipv4.src)) ||
1296 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1297 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1298 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1299 sizeof(key->enc_ipv4.dst))))
1300 goto nla_put_failure;
1301 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1302 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1303 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1304 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1305 sizeof(key->enc_ipv6.src)) ||
1306 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1307 TCA_FLOWER_KEY_ENC_IPV6_DST,
1308 &mask->enc_ipv6.dst,
1309 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1310 sizeof(key->enc_ipv6.dst))))
1311 goto nla_put_failure;
1312
1313 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001314 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001315 sizeof(key->enc_key_id)) ||
1316 fl_dump_key_val(skb, &key->enc_tp.src,
1317 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1318 &mask->enc_tp.src,
1319 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1320 sizeof(key->enc_tp.src)) ||
1321 fl_dump_key_val(skb, &key->enc_tp.dst,
1322 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1323 &mask->enc_tp.dst,
1324 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1325 sizeof(key->enc_tp.dst)))
Amir Vadaibc3103f2016-09-08 16:23:47 +03001326 goto nla_put_failure;
1327
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001328 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1329 goto nla_put_failure;
1330
Or Gerlitz749e6722017-02-16 10:31:10 +02001331 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1332 goto nla_put_failure;
Amir Vadaie69985c2016-06-05 17:11:18 +03001333
Jiri Pirko77b99002015-05-12 14:56:21 +02001334 if (tcf_exts_dump(skb, &f->exts))
1335 goto nla_put_failure;
1336
1337 nla_nest_end(skb, nest);
1338
1339 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1340 goto nla_put_failure;
1341
1342 return skb->len;
1343
1344nla_put_failure:
1345 nla_nest_cancel(skb, nest);
1346 return -1;
1347}
1348
1349static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1350 .kind = "flower",
1351 .classify = fl_classify,
1352 .init = fl_init,
1353 .destroy = fl_destroy,
1354 .get = fl_get,
1355 .change = fl_change,
1356 .delete = fl_delete,
1357 .walk = fl_walk,
1358 .dump = fl_dump,
1359 .owner = THIS_MODULE,
1360};
1361
1362static int __init cls_fl_init(void)
1363{
1364 return register_tcf_proto_ops(&cls_fl_ops);
1365}
1366
1367static void __exit cls_fl_exit(void)
1368{
1369 unregister_tcf_proto_ops(&cls_fl_ops);
1370}
1371
1372module_init(cls_fl_init);
1373module_exit(cls_fl_exit);
1374
1375MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1376MODULE_DESCRIPTION("Flower classifier");
1377MODULE_LICENSE("GPL v2");