blob: 7a838d1c1c0059bddb3da24a59ad73689fec1877 [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;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300158 struct ip_tunnel_info *info;
Jiri Pirko77b99002015-05-12 14:56:21 +0200159
Amir Vadaie69985c2016-06-05 17:11:18 +0300160 if (!atomic_read(&head->ht.nelems))
161 return -1;
162
Jiri Pirko77b99002015-05-12 14:56:21 +0200163 fl_clear_masked_range(&skb_key, &head->mask);
Amir Vadaibc3103f2016-09-08 16:23:47 +0300164
165 info = skb_tunnel_info(skb);
166 if (info) {
167 struct ip_tunnel_key *key = &info->key;
168
169 switch (ip_tunnel_info_af(info)) {
170 case AF_INET:
Paul Blakey0df0f202016-12-28 14:54:47 +0200171 skb_key.enc_control.addr_type =
172 FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300173 skb_key.enc_ipv4.src = key->u.ipv4.src;
174 skb_key.enc_ipv4.dst = key->u.ipv4.dst;
175 break;
176 case AF_INET6:
Paul Blakey0df0f202016-12-28 14:54:47 +0200177 skb_key.enc_control.addr_type =
178 FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300179 skb_key.enc_ipv6.src = key->u.ipv6.src;
180 skb_key.enc_ipv6.dst = key->u.ipv6.dst;
181 break;
182 }
183
184 skb_key.enc_key_id.keyid = tunnel_id_to_key32(key->tun_id);
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200185 skb_key.enc_tp.src = key->tp_src;
186 skb_key.enc_tp.dst = key->tp_dst;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300187 }
188
Jiri Pirko77b99002015-05-12 14:56:21 +0200189 skb_key.indev_ifindex = skb->skb_iif;
190 /* skb_flow_dissect() does not set n_proto in case an unknown protocol,
191 * so do it rather here.
192 */
193 skb_key.basic.n_proto = skb->protocol;
Tom Herbertcd79a232015-09-01 09:24:27 -0700194 skb_flow_dissect(skb, &head->dissector, &skb_key, 0);
Jiri Pirko77b99002015-05-12 14:56:21 +0200195
196 fl_set_masked_key(&skb_mkey, &skb_key, &head->mask);
197
Paul Blakeya3308d82017-01-16 10:45:13 +0200198 f = fl_lookup(head, &skb_mkey);
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300199 if (f && !tc_skip_sw(f->flags)) {
Jiri Pirko77b99002015-05-12 14:56:21 +0200200 *res = f->res;
201 return tcf_exts_exec(skb, &f->exts, res);
202 }
203 return -1;
204}
205
206static int fl_init(struct tcf_proto *tp)
207{
208 struct cls_fl_head *head;
209
210 head = kzalloc(sizeof(*head), GFP_KERNEL);
211 if (!head)
212 return -ENOBUFS;
213
214 INIT_LIST_HEAD_RCU(&head->filters);
215 rcu_assign_pointer(tp->root, head);
Chris Mic15ab232017-08-30 02:31:58 -0400216 idr_init(&head->handle_idr);
Jiri Pirko77b99002015-05-12 14:56:21 +0200217
218 return 0;
219}
220
Cong Wang0dadc112017-11-06 13:47:24 -0800221static void __fl_destroy_filter(struct cls_fl_filter *f)
222{
223 tcf_exts_destroy(&f->exts);
224 tcf_exts_put_net(&f->exts);
225 kfree(f);
226}
227
Cong Wang0552c8a2017-10-26 18:24:33 -0700228static void fl_destroy_filter_work(struct work_struct *work)
229{
230 struct cls_fl_filter *f = container_of(work, struct cls_fl_filter, work);
231
232 rtnl_lock();
Cong Wang0dadc112017-11-06 13:47:24 -0800233 __fl_destroy_filter(f);
Cong Wang0552c8a2017-10-26 18:24:33 -0700234 rtnl_unlock();
235}
236
Jiri Pirko77b99002015-05-12 14:56:21 +0200237static void fl_destroy_filter(struct rcu_head *head)
238{
239 struct cls_fl_filter *f = container_of(head, struct cls_fl_filter, rcu);
240
Cong Wang0552c8a2017-10-26 18:24:33 -0700241 INIT_WORK(&f->work, fl_destroy_filter_work);
242 tcf_queue_work(&f->work);
Jiri Pirko77b99002015-05-12 14:56:21 +0200243}
244
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200245static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f)
Amir Vadai5b33f482016-03-08 12:42:29 +0200246{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200247 struct tc_cls_flower_offload cls_flower = {};
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200248 struct net_device *dev = f->hw_dev;
Amir Vadai5b33f482016-03-08 12:42:29 +0200249
Jiri Pirko7b06e8a2017-08-09 14:30:35 +0200250 if (!tc_can_offload(dev))
Amir Vadai5b33f482016-03-08 12:42:29 +0200251 return;
252
Jiri Pirkode4784c2017-08-07 10:15:32 +0200253 tc_cls_common_offload_init(&cls_flower.common, tp);
254 cls_flower.command = TC_CLSFLOWER_DESTROY;
255 cls_flower.cookie = (unsigned long) f;
Or Gerlitzc019b512017-10-16 12:19:48 +0300256 cls_flower.egress_dev = f->hw_dev != tp->q->dev_queue->dev;
Amir Vadai5b33f482016-03-08 12:42:29 +0200257
Jiri Pirkode4784c2017-08-07 10:15:32 +0200258 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSFLOWER, &cls_flower);
Amir Vadai5b33f482016-03-08 12:42:29 +0200259}
260
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300261static int fl_hw_replace_filter(struct tcf_proto *tp,
262 struct flow_dissector *dissector,
263 struct fl_flow_key *mask,
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200264 struct cls_fl_filter *f)
Amir Vadai5b33f482016-03-08 12:42:29 +0200265{
266 struct net_device *dev = tp->q->dev_queue->dev;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200267 struct tc_cls_flower_offload cls_flower = {};
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300268 int err;
Amir Vadai5b33f482016-03-08 12:42:29 +0200269
Jiri Pirko7b06e8a2017-08-09 14:30:35 +0200270 if (!tc_can_offload(dev)) {
Hadar Hen Ziona6e16932016-12-04 15:25:19 +0200271 if (tcf_exts_get_dev(dev, &f->exts, &f->hw_dev) ||
Jiri Pirko7b06e8a2017-08-09 14:30:35 +0200272 (f->hw_dev && !tc_can_offload(f->hw_dev))) {
Hadar Hen Ziona6e16932016-12-04 15:25:19 +0200273 f->hw_dev = dev;
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200274 return tc_skip_sw(f->flags) ? -EINVAL : 0;
Hadar Hen Ziona6e16932016-12-04 15:25:19 +0200275 }
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200276 dev = f->hw_dev;
Jiri Pirkode4784c2017-08-07 10:15:32 +0200277 cls_flower.egress_dev = true;
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200278 } else {
279 f->hw_dev = dev;
280 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200281
Jiri Pirkode4784c2017-08-07 10:15:32 +0200282 tc_cls_common_offload_init(&cls_flower.common, tp);
283 cls_flower.command = TC_CLSFLOWER_REPLACE;
284 cls_flower.cookie = (unsigned long) f;
285 cls_flower.dissector = dissector;
286 cls_flower.mask = mask;
287 cls_flower.key = &f->mkey;
288 cls_flower.exts = &f->exts;
Amir Vadai5b33f482016-03-08 12:42:29 +0200289
Jiri Pirkode4784c2017-08-07 10:15:32 +0200290 err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSFLOWER,
291 &cls_flower);
Or Gerlitz55593962017-02-16 10:31:13 +0200292 if (!err)
293 f->flags |= TCA_CLS_FLAGS_IN_HW;
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300294
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200295 if (tc_skip_sw(f->flags))
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300296 return err;
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300297 return 0;
Amir Vadai5b33f482016-03-08 12:42:29 +0200298}
299
Amir Vadai10cbc682016-05-13 12:55:37 +0000300static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
301{
Jiri Pirkode4784c2017-08-07 10:15:32 +0200302 struct tc_cls_flower_offload cls_flower = {};
Hadar Hen Zion7091d8c2016-12-01 14:06:37 +0200303 struct net_device *dev = f->hw_dev;
Amir Vadai10cbc682016-05-13 12:55:37 +0000304
Jiri Pirko7b06e8a2017-08-09 14:30:35 +0200305 if (!tc_can_offload(dev))
Amir Vadai10cbc682016-05-13 12:55:37 +0000306 return;
307
Jiri Pirkode4784c2017-08-07 10:15:32 +0200308 tc_cls_common_offload_init(&cls_flower.common, tp);
309 cls_flower.command = TC_CLSFLOWER_STATS;
310 cls_flower.cookie = (unsigned long) f;
311 cls_flower.exts = &f->exts;
Or Gerlitzc019b512017-10-16 12:19:48 +0300312 cls_flower.egress_dev = f->hw_dev != tp->q->dev_queue->dev;
Amir Vadai10cbc682016-05-13 12:55:37 +0000313
Jiri Pirkod978db82017-08-16 17:15:18 +0200314 dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_CLSFLOWER,
Jiri Pirkode4784c2017-08-07 10:15:32 +0200315 &cls_flower);
Amir Vadai10cbc682016-05-13 12:55:37 +0000316}
317
Roi Dayan13fa8762016-11-01 16:08:29 +0200318static void __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f)
319{
Chris Mic15ab232017-08-30 02:31:58 -0400320 struct cls_fl_head *head = rtnl_dereference(tp->root);
321
322 idr_remove_ext(&head->handle_idr, f->handle);
Roi Dayan13fa8762016-11-01 16:08:29 +0200323 list_del_rcu(&f->list);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200324 if (!tc_skip_hw(f->flags))
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200325 fl_hw_destroy_filter(tp, f);
Roi Dayan13fa8762016-11-01 16:08:29 +0200326 tcf_unbind_filter(tp, &f->res);
Cong Wang0dadc112017-11-06 13:47:24 -0800327 if (tcf_exts_get_net(&f->exts))
328 call_rcu(&f->rcu, fl_destroy_filter);
329 else
330 __fl_destroy_filter(f);
Roi Dayan13fa8762016-11-01 16:08:29 +0200331}
332
Daniel Borkmannd9363772016-11-27 01:18:01 +0100333static void fl_destroy_sleepable(struct work_struct *work)
334{
335 struct cls_fl_head *head = container_of(work, struct cls_fl_head,
336 work);
337 if (head->mask_assigned)
338 rhashtable_destroy(&head->ht);
339 kfree(head);
340 module_put(THIS_MODULE);
341}
342
343static void fl_destroy_rcu(struct rcu_head *rcu)
344{
345 struct cls_fl_head *head = container_of(rcu, struct cls_fl_head, rcu);
346
347 INIT_WORK(&head->work, fl_destroy_sleepable);
348 schedule_work(&head->work);
349}
350
WANG Cong763dbf62017-04-19 14:21:21 -0700351static void fl_destroy(struct tcf_proto *tp)
Jiri Pirko77b99002015-05-12 14:56:21 +0200352{
353 struct cls_fl_head *head = rtnl_dereference(tp->root);
354 struct cls_fl_filter *f, *next;
355
Roi Dayan13fa8762016-11-01 16:08:29 +0200356 list_for_each_entry_safe(f, next, &head->filters, list)
357 __fl_delete(tp, f);
Chris Mic15ab232017-08-30 02:31:58 -0400358 idr_destroy(&head->handle_idr);
Daniel Borkmannd9363772016-11-27 01:18:01 +0100359
360 __module_get(THIS_MODULE);
361 call_rcu(&head->rcu, fl_destroy_rcu);
Jiri Pirko77b99002015-05-12 14:56:21 +0200362}
363
WANG Cong8113c092017-08-04 21:31:43 -0700364static void *fl_get(struct tcf_proto *tp, u32 handle)
Jiri Pirko77b99002015-05-12 14:56:21 +0200365{
366 struct cls_fl_head *head = rtnl_dereference(tp->root);
Jiri Pirko77b99002015-05-12 14:56:21 +0200367
Chris Mic15ab232017-08-30 02:31:58 -0400368 return idr_find_ext(&head->handle_idr, handle);
Jiri Pirko77b99002015-05-12 14:56:21 +0200369}
370
371static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
372 [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
373 [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
374 [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
375 .len = IFNAMSIZ },
376 [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
377 [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
378 [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
379 [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
380 [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
381 [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
382 [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
383 [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
384 [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
385 [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
386 [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
387 [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
388 [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
389 [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
390 [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
391 [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
Jamal Hadi Salimb175c3a2015-06-25 06:55:27 -0400392 [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
393 [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300394 [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
395 [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
396 [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
Amir Vadaibc3103f2016-09-08 16:23:47 +0300397 [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
398 [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
399 [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
400 [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
401 [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
402 [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
403 [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
404 [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
405 [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
Or Gerlitzaa72d702016-09-15 15:28:22 +0300406 [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
407 [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
408 [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
409 [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
Simon Horman5976c5f2016-11-03 13:24:21 +0100410 [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
411 [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
412 [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
413 [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200414 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
415 [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
416 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
417 [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200418 [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
419 [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
Simon Horman7b684882016-12-07 13:48:28 +0100420 [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
421 [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
422 [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
423 [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
424 [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
425 [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
426 [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
427 [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
Simon Horman99d31322017-01-11 14:05:43 +0100428 [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
429 [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
430 [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
431 [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
432 [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
433 [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
434 [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
435 [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
436 [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
437 [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400438 [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
439 [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
440 [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
441 [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200442 [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
443 [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300444 [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
445 [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
446 [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
447 [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
Jiri Pirko77b99002015-05-12 14:56:21 +0200448};
449
450static void fl_set_key_val(struct nlattr **tb,
451 void *val, int val_type,
452 void *mask, int mask_type, int len)
453{
454 if (!tb[val_type])
455 return;
456 memcpy(val, nla_data(tb[val_type]), len);
457 if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
458 memset(mask, 0xff, len);
459 else
460 memcpy(mask, nla_data(tb[mask_type]), len);
461}
462
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400463static int fl_set_key_mpls(struct nlattr **tb,
464 struct flow_dissector_key_mpls *key_val,
465 struct flow_dissector_key_mpls *key_mask)
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400466{
467 if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
468 key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
469 key_mask->mpls_ttl = MPLS_TTL_MASK;
470 }
471 if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400472 u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
473
474 if (bos & ~MPLS_BOS_MASK)
475 return -EINVAL;
476 key_val->mpls_bos = bos;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400477 key_mask->mpls_bos = MPLS_BOS_MASK;
478 }
479 if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400480 u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
481
482 if (tc & ~MPLS_TC_MASK)
483 return -EINVAL;
484 key_val->mpls_tc = tc;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400485 key_mask->mpls_tc = MPLS_TC_MASK;
486 }
487 if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400488 u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
489
490 if (label & ~MPLS_LABEL_MASK)
491 return -EINVAL;
492 key_val->mpls_label = label;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400493 key_mask->mpls_label = MPLS_LABEL_MASK;
494 }
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400495 return 0;
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400496}
497
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300498static void fl_set_key_vlan(struct nlattr **tb,
499 struct flow_dissector_key_vlan *key_val,
500 struct flow_dissector_key_vlan *key_mask)
501{
502#define VLAN_PRIORITY_MASK 0x7
503
504 if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
505 key_val->vlan_id =
506 nla_get_u16(tb[TCA_FLOWER_KEY_VLAN_ID]) & VLAN_VID_MASK;
507 key_mask->vlan_id = VLAN_VID_MASK;
508 }
509 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
510 key_val->vlan_priority =
511 nla_get_u8(tb[TCA_FLOWER_KEY_VLAN_PRIO]) &
512 VLAN_PRIORITY_MASK;
513 key_mask->vlan_priority = VLAN_PRIORITY_MASK;
514 }
515}
516
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200517static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
518 u32 *dissector_key, u32 *dissector_mask,
519 u32 flower_flag_bit, u32 dissector_flag_bit)
520{
521 if (flower_mask & flower_flag_bit) {
522 *dissector_mask |= dissector_flag_bit;
523 if (flower_key & flower_flag_bit)
524 *dissector_key |= dissector_flag_bit;
525 }
526}
527
Or Gerlitzd9724772016-12-22 14:28:15 +0200528static int fl_set_key_flags(struct nlattr **tb,
529 u32 *flags_key, u32 *flags_mask)
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200530{
531 u32 key, mask;
532
Or Gerlitzd9724772016-12-22 14:28:15 +0200533 /* mask is mandatory for flags */
534 if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
535 return -EINVAL;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200536
537 key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
Or Gerlitzd9724772016-12-22 14:28:15 +0200538 mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200539
540 *flags_key = 0;
541 *flags_mask = 0;
542
543 fl_set_key_flag(key, mask, flags_key, flags_mask,
544 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
Or Gerlitzd9724772016-12-22 14:28:15 +0200545
546 return 0;
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200547}
548
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300549static void fl_set_key_ip(struct nlattr **tb,
550 struct flow_dissector_key_ip *key,
551 struct flow_dissector_key_ip *mask)
552{
553 fl_set_key_val(tb, &key->tos, TCA_FLOWER_KEY_IP_TOS,
554 &mask->tos, TCA_FLOWER_KEY_IP_TOS_MASK,
555 sizeof(key->tos));
556
557 fl_set_key_val(tb, &key->ttl, TCA_FLOWER_KEY_IP_TTL,
558 &mask->ttl, TCA_FLOWER_KEY_IP_TTL_MASK,
559 sizeof(key->ttl));
560}
561
Jiri Pirko77b99002015-05-12 14:56:21 +0200562static int fl_set_key(struct net *net, struct nlattr **tb,
563 struct fl_flow_key *key, struct fl_flow_key *mask)
564{
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300565 __be16 ethertype;
Or Gerlitzd9724772016-12-22 14:28:15 +0200566 int ret = 0;
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400567#ifdef CONFIG_NET_CLS_IND
Jiri Pirko77b99002015-05-12 14:56:21 +0200568 if (tb[TCA_FLOWER_INDEV]) {
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400569 int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV]);
Jiri Pirko77b99002015-05-12 14:56:21 +0200570 if (err < 0)
571 return err;
572 key->indev_ifindex = err;
573 mask->indev_ifindex = 0xffffffff;
574 }
Brian Haleydd3aa3b2015-05-14 13:20:15 -0400575#endif
Jiri Pirko77b99002015-05-12 14:56:21 +0200576
577 fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
578 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
579 sizeof(key->eth.dst));
580 fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
581 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
582 sizeof(key->eth.src));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500583
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200584 if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300585 ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
586
Arnd Bergmann0b498a52016-08-26 17:25:45 +0200587 if (ethertype == htons(ETH_P_8021Q)) {
588 fl_set_key_vlan(tb, &key->vlan, &mask->vlan);
589 fl_set_key_val(tb, &key->basic.n_proto,
590 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
591 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
592 sizeof(key->basic.n_proto));
593 } else {
594 key->basic.n_proto = ethertype;
595 mask->basic.n_proto = cpu_to_be16(~0);
596 }
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300597 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500598
Jiri Pirko77b99002015-05-12 14:56:21 +0200599 if (key->basic.n_proto == htons(ETH_P_IP) ||
600 key->basic.n_proto == htons(ETH_P_IPV6)) {
601 fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
602 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
603 sizeof(key->basic.ip_proto));
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300604 fl_set_key_ip(tb, &key->ip, &mask->ip);
Jiri Pirko77b99002015-05-12 14:56:21 +0200605 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500606
607 if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
608 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200609 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200610 fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
611 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
612 sizeof(key->ipv4.src));
613 fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
614 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
615 sizeof(key->ipv4.dst));
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500616 } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
617 key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200618 mask->control.addr_type = ~0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200619 fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
620 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
621 sizeof(key->ipv6.src));
622 fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
623 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
624 sizeof(key->ipv6.dst));
625 }
Jamal Hadi Salim66530bd2016-01-10 11:47:01 -0500626
Jiri Pirko77b99002015-05-12 14:56:21 +0200627 if (key->basic.ip_proto == IPPROTO_TCP) {
628 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300629 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200630 sizeof(key->tp.src));
631 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300632 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200633 sizeof(key->tp.dst));
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200634 fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
635 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
636 sizeof(key->tcp.flags));
Jiri Pirko77b99002015-05-12 14:56:21 +0200637 } else if (key->basic.ip_proto == IPPROTO_UDP) {
638 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300639 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200640 sizeof(key->tp.src));
641 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +0300642 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +0200643 sizeof(key->tp.dst));
Simon Horman5976c5f2016-11-03 13:24:21 +0100644 } else if (key->basic.ip_proto == IPPROTO_SCTP) {
645 fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
646 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
647 sizeof(key->tp.src));
648 fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
649 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
650 sizeof(key->tp.dst));
Simon Horman7b684882016-12-07 13:48:28 +0100651 } else if (key->basic.n_proto == htons(ETH_P_IP) &&
652 key->basic.ip_proto == IPPROTO_ICMP) {
653 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
654 &mask->icmp.type,
655 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
656 sizeof(key->icmp.type));
657 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
658 &mask->icmp.code,
659 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
660 sizeof(key->icmp.code));
661 } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
662 key->basic.ip_proto == IPPROTO_ICMPV6) {
663 fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
664 &mask->icmp.type,
665 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
666 sizeof(key->icmp.type));
Simon Horman040587a2017-01-30 16:19:02 +0100667 fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
Simon Horman7b684882016-12-07 13:48:28 +0100668 &mask->icmp.code,
Simon Horman040587a2017-01-30 16:19:02 +0100669 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
Simon Horman7b684882016-12-07 13:48:28 +0100670 sizeof(key->icmp.code));
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400671 } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
672 key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
Benjamin LaHaise1a7fca62017-05-01 09:58:40 -0400673 ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
674 if (ret)
675 return ret;
Simon Horman99d31322017-01-11 14:05:43 +0100676 } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
677 key->basic.n_proto == htons(ETH_P_RARP)) {
678 fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
679 &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
680 sizeof(key->arp.sip));
681 fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
682 &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
683 sizeof(key->arp.tip));
684 fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
685 &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
686 sizeof(key->arp.op));
687 fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
688 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
689 sizeof(key->arp.sha));
690 fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
691 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
692 sizeof(key->arp.tha));
Jiri Pirko77b99002015-05-12 14:56:21 +0200693 }
694
Amir Vadaibc3103f2016-09-08 16:23:47 +0300695 if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
696 tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
697 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200698 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300699 fl_set_key_val(tb, &key->enc_ipv4.src,
700 TCA_FLOWER_KEY_ENC_IPV4_SRC,
701 &mask->enc_ipv4.src,
702 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
703 sizeof(key->enc_ipv4.src));
704 fl_set_key_val(tb, &key->enc_ipv4.dst,
705 TCA_FLOWER_KEY_ENC_IPV4_DST,
706 &mask->enc_ipv4.dst,
707 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
708 sizeof(key->enc_ipv4.dst));
709 }
710
711 if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
712 tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
713 key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
Paul Blakey970bfcd2016-12-14 19:00:57 +0200714 mask->enc_control.addr_type = ~0;
Amir Vadaibc3103f2016-09-08 16:23:47 +0300715 fl_set_key_val(tb, &key->enc_ipv6.src,
716 TCA_FLOWER_KEY_ENC_IPV6_SRC,
717 &mask->enc_ipv6.src,
718 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
719 sizeof(key->enc_ipv6.src));
720 fl_set_key_val(tb, &key->enc_ipv6.dst,
721 TCA_FLOWER_KEY_ENC_IPV6_DST,
722 &mask->enc_ipv6.dst,
723 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
724 sizeof(key->enc_ipv6.dst));
725 }
726
727 fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +0300728 &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
Amir Vadaibc3103f2016-09-08 16:23:47 +0300729 sizeof(key->enc_key_id.keyid));
730
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200731 fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
732 &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
733 sizeof(key->enc_tp.src));
734
735 fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
736 &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
737 sizeof(key->enc_tp.dst));
738
Or Gerlitzd9724772016-12-22 14:28:15 +0200739 if (tb[TCA_FLOWER_KEY_FLAGS])
740 ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +0200741
Or Gerlitzd9724772016-12-22 14:28:15 +0200742 return ret;
Jiri Pirko77b99002015-05-12 14:56:21 +0200743}
744
745static bool fl_mask_eq(struct fl_flow_mask *mask1,
746 struct fl_flow_mask *mask2)
747{
748 const long *lmask1 = fl_key_get_start(&mask1->key, mask1);
749 const long *lmask2 = fl_key_get_start(&mask2->key, mask2);
750
751 return !memcmp(&mask1->range, &mask2->range, sizeof(mask1->range)) &&
752 !memcmp(lmask1, lmask2, fl_mask_range(mask1));
753}
754
755static const struct rhashtable_params fl_ht_params = {
756 .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
757 .head_offset = offsetof(struct cls_fl_filter, ht_node),
758 .automatic_shrinking = true,
759};
760
761static int fl_init_hashtable(struct cls_fl_head *head,
762 struct fl_flow_mask *mask)
763{
764 head->ht_params = fl_ht_params;
765 head->ht_params.key_len = fl_mask_range(mask);
766 head->ht_params.key_offset += mask->range.start;
767
768 return rhashtable_init(&head->ht, &head->ht_params);
769}
770
771#define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
772#define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
Jiri Pirko77b99002015-05-12 14:56:21 +0200773
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300774#define FL_KEY_IS_MASKED(mask, member) \
775 memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
776 0, FL_KEY_MEMBER_SIZE(member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200777
778#define FL_KEY_SET(keys, cnt, id, member) \
779 do { \
780 keys[cnt].key_id = id; \
781 keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
782 cnt++; \
783 } while(0);
784
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300785#define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200786 do { \
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300787 if (FL_KEY_IS_MASKED(mask, member)) \
Jiri Pirko77b99002015-05-12 14:56:21 +0200788 FL_KEY_SET(keys, cnt, id, member); \
789 } while(0);
790
791static void fl_init_dissector(struct cls_fl_head *head,
792 struct fl_flow_mask *mask)
793{
794 struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
795 size_t cnt = 0;
796
Tom Herbert42aecaa2015-06-04 09:16:39 -0700797 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
Jiri Pirko77b99002015-05-12 14:56:21 +0200798 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
Hadar Hen Zion339ba872016-08-17 13:36:12 +0300799 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
800 FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
801 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
802 FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
803 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
804 FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
805 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
806 FLOW_DISSECTOR_KEY_PORTS, tp);
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300807 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Or Gerlitz4d80cc02017-06-01 21:37:38 +0300808 FLOW_DISSECTOR_KEY_IP, ip);
809 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +0200810 FLOW_DISSECTOR_KEY_TCP, tcp);
811 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman7b684882016-12-07 13:48:28 +0100812 FLOW_DISSECTOR_KEY_ICMP, icmp);
813 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Simon Horman99d31322017-01-11 14:05:43 +0100814 FLOW_DISSECTOR_KEY_ARP, arp);
815 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -0400816 FLOW_DISSECTOR_KEY_MPLS, mpls);
817 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
Hadar Hen Zion9399ae92016-08-17 13:36:13 +0300818 FLOW_DISSECTOR_KEY_VLAN, vlan);
Hadar Hen Zion519d1052016-11-07 15:14:38 +0200819 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
820 FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
821 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
822 FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
823 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
824 FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
825 if (FL_KEY_IS_MASKED(&mask->key, enc_ipv4) ||
826 FL_KEY_IS_MASKED(&mask->key, enc_ipv6))
827 FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
828 enc_control);
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +0200829 FL_KEY_SET_IF_MASKED(&mask->key, keys, cnt,
830 FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
Jiri Pirko77b99002015-05-12 14:56:21 +0200831
832 skb_flow_dissector_init(&head->dissector, keys, cnt);
833}
834
835static int fl_check_assign_mask(struct cls_fl_head *head,
836 struct fl_flow_mask *mask)
837{
838 int err;
839
840 if (head->mask_assigned) {
841 if (!fl_mask_eq(&head->mask, mask))
842 return -EINVAL;
843 else
844 return 0;
845 }
846
847 /* Mask is not assigned yet. So assign it and init hashtable
848 * according to that.
849 */
850 err = fl_init_hashtable(head, mask);
851 if (err)
852 return err;
853 memcpy(&head->mask, mask, sizeof(head->mask));
854 head->mask_assigned = true;
855
856 fl_init_dissector(head, mask);
857
858 return 0;
859}
860
861static int fl_set_parms(struct net *net, struct tcf_proto *tp,
862 struct cls_fl_filter *f, struct fl_flow_mask *mask,
863 unsigned long base, struct nlattr **tb,
864 struct nlattr *est, bool ovr)
865{
Jiri Pirko77b99002015-05-12 14:56:21 +0200866 int err;
867
Jiri Pirko45507522017-08-04 14:29:06 +0200868 err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr);
Jiri Pirko77b99002015-05-12 14:56:21 +0200869 if (err < 0)
870 return err;
871
872 if (tb[TCA_FLOWER_CLASSID]) {
873 f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
874 tcf_bind_filter(tp, &f->res, base);
875 }
876
877 err = fl_set_key(net, tb, &f->key, &mask->key);
878 if (err)
Jiri Pirko45507522017-08-04 14:29:06 +0200879 return err;
Jiri Pirko77b99002015-05-12 14:56:21 +0200880
881 fl_mask_update_range(mask);
882 fl_set_masked_key(&f->mkey, &f->key, mask);
883
Jiri Pirko77b99002015-05-12 14:56:21 +0200884 return 0;
Jiri Pirko77b99002015-05-12 14:56:21 +0200885}
886
Jiri Pirko77b99002015-05-12 14:56:21 +0200887static int fl_change(struct net *net, struct sk_buff *in_skb,
888 struct tcf_proto *tp, unsigned long base,
889 u32 handle, struct nlattr **tca,
WANG Cong8113c092017-08-04 21:31:43 -0700890 void **arg, bool ovr)
Jiri Pirko77b99002015-05-12 14:56:21 +0200891{
892 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -0700893 struct cls_fl_filter *fold = *arg;
Jiri Pirko77b99002015-05-12 14:56:21 +0200894 struct cls_fl_filter *fnew;
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100895 struct nlattr **tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200896 struct fl_flow_mask mask = {};
Chris Mic15ab232017-08-30 02:31:58 -0400897 unsigned long idr_index;
Jiri Pirko77b99002015-05-12 14:56:21 +0200898 int err;
899
900 if (!tca[TCA_OPTIONS])
901 return -EINVAL;
902
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100903 tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
904 if (!tb)
905 return -ENOBUFS;
906
Johannes Bergfceb6432017-04-12 14:34:07 +0200907 err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
908 fl_policy, NULL);
Jiri Pirko77b99002015-05-12 14:56:21 +0200909 if (err < 0)
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100910 goto errout_tb;
Jiri Pirko77b99002015-05-12 14:56:21 +0200911
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100912 if (fold && handle && fold->handle != handle) {
913 err = -EINVAL;
914 goto errout_tb;
915 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200916
917 fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +0100918 if (!fnew) {
919 err = -ENOBUFS;
920 goto errout_tb;
921 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200922
WANG Congb9a24bb2016-08-19 12:36:54 -0700923 err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
924 if (err < 0)
925 goto errout;
Jiri Pirko77b99002015-05-12 14:56:21 +0200926
927 if (!handle) {
Chris Mic15ab232017-08-30 02:31:58 -0400928 err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
929 1, 0x80000000, GFP_KERNEL);
930 if (err)
Jiri Pirko77b99002015-05-12 14:56:21 +0200931 goto errout;
Chris Mic15ab232017-08-30 02:31:58 -0400932 fnew->handle = idr_index;
Jiri Pirko77b99002015-05-12 14:56:21 +0200933 }
Chris Mic15ab232017-08-30 02:31:58 -0400934
935 /* user specifies a handle and it doesn't exist */
936 if (handle && !fold) {
937 err = idr_alloc_ext(&head->handle_idr, fnew, &idr_index,
938 handle, handle + 1, GFP_KERNEL);
939 if (err)
940 goto errout;
941 fnew->handle = idr_index;
942 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200943
Amir Vadaie69985c2016-06-05 17:11:18 +0300944 if (tb[TCA_FLOWER_FLAGS]) {
945 fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
946
947 if (!tc_flags_valid(fnew->flags)) {
948 err = -EINVAL;
Cong Wangfe2502e2017-09-20 09:18:45 -0700949 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +0300950 }
951 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200952
Jiri Pirko77b99002015-05-12 14:56:21 +0200953 err = fl_set_parms(net, tp, fnew, &mask, base, tb, tca[TCA_RATE], ovr);
954 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700955 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +0200956
957 err = fl_check_assign_mask(head, &mask);
958 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700959 goto errout_idr;
Jiri Pirko77b99002015-05-12 14:56:21 +0200960
Amir Vadaie8eb36c2016-06-13 12:06:39 +0300961 if (!tc_skip_sw(fnew->flags)) {
Paul Blakeya3308d82017-01-16 10:45:13 +0200962 if (!fold && fl_lookup(head, &fnew->mkey)) {
963 err = -EEXIST;
Cong Wangfe2502e2017-09-20 09:18:45 -0700964 goto errout_idr;
Paul Blakeya3308d82017-01-16 10:45:13 +0200965 }
966
Amir Vadaie69985c2016-06-05 17:11:18 +0300967 err = rhashtable_insert_fast(&head->ht, &fnew->ht_node,
968 head->ht_params);
969 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700970 goto errout_idr;
Amir Vadaie69985c2016-06-05 17:11:18 +0300971 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200972
Hadar Hen Zion79685212016-12-01 14:06:34 +0200973 if (!tc_skip_hw(fnew->flags)) {
974 err = fl_hw_replace_filter(tp,
975 &head->dissector,
976 &mask.key,
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200977 fnew);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200978 if (err)
Cong Wangfe2502e2017-09-20 09:18:45 -0700979 goto errout_idr;
Hadar Hen Zion79685212016-12-01 14:06:34 +0200980 }
Amir Vadai5b33f482016-03-08 12:42:29 +0200981
Or Gerlitz55593962017-02-16 10:31:13 +0200982 if (!tc_in_hw(fnew->flags))
983 fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
984
Amir Vadai5b33f482016-03-08 12:42:29 +0200985 if (fold) {
Jiri Pirko725cbb622016-11-28 15:40:13 +0100986 if (!tc_skip_sw(fold->flags))
987 rhashtable_remove_fast(&head->ht, &fold->ht_node,
988 head->ht_params);
Hadar Hen Zion79685212016-12-01 14:06:34 +0200989 if (!tc_skip_hw(fold->flags))
Hadar Hen Zion3036dab2016-12-01 14:06:35 +0200990 fl_hw_destroy_filter(tp, fold);
Amir Vadai5b33f482016-03-08 12:42:29 +0200991 }
Jiri Pirko77b99002015-05-12 14:56:21 +0200992
WANG Cong8113c092017-08-04 21:31:43 -0700993 *arg = fnew;
Jiri Pirko77b99002015-05-12 14:56:21 +0200994
995 if (fold) {
Chris Mic15ab232017-08-30 02:31:58 -0400996 fnew->handle = handle;
997 idr_replace_ext(&head->handle_idr, fnew, fnew->handle);
Daniel Borkmannff3532f2015-07-17 22:38:44 +0200998 list_replace_rcu(&fold->list, &fnew->list);
Jiri Pirko77b99002015-05-12 14:56:21 +0200999 tcf_unbind_filter(tp, &fold->res);
Cong Wang0dadc112017-11-06 13:47:24 -08001000 tcf_exts_get_net(&fold->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +02001001 call_rcu(&fold->rcu, fl_destroy_filter);
1002 } else {
1003 list_add_tail_rcu(&fnew->list, &head->filters);
1004 }
1005
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001006 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +02001007 return 0;
1008
Cong Wangfe2502e2017-09-20 09:18:45 -07001009errout_idr:
1010 if (fnew->handle)
1011 idr_remove_ext(&head->handle_idr, fnew->handle);
Jiri Pirko77b99002015-05-12 14:56:21 +02001012errout:
WANG Congb9a24bb2016-08-19 12:36:54 -07001013 tcf_exts_destroy(&fnew->exts);
Jiri Pirko77b99002015-05-12 14:56:21 +02001014 kfree(fnew);
Arnd Bergmann39b7b6a2017-01-19 10:45:31 +01001015errout_tb:
1016 kfree(tb);
Jiri Pirko77b99002015-05-12 14:56:21 +02001017 return err;
1018}
1019
WANG Cong8113c092017-08-04 21:31:43 -07001020static int fl_delete(struct tcf_proto *tp, void *arg, bool *last)
Jiri Pirko77b99002015-05-12 14:56:21 +02001021{
1022 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -07001023 struct cls_fl_filter *f = arg;
Jiri Pirko77b99002015-05-12 14:56:21 +02001024
Jiri Pirko725cbb622016-11-28 15:40:13 +01001025 if (!tc_skip_sw(f->flags))
1026 rhashtable_remove_fast(&head->ht, &f->ht_node,
1027 head->ht_params);
Roi Dayan13fa8762016-11-01 16:08:29 +02001028 __fl_delete(tp, f);
WANG Cong763dbf62017-04-19 14:21:21 -07001029 *last = list_empty(&head->filters);
Jiri Pirko77b99002015-05-12 14:56:21 +02001030 return 0;
1031}
1032
1033static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
1034{
1035 struct cls_fl_head *head = rtnl_dereference(tp->root);
1036 struct cls_fl_filter *f;
1037
1038 list_for_each_entry_rcu(f, &head->filters, list) {
1039 if (arg->count < arg->skip)
1040 goto skip;
WANG Cong8113c092017-08-04 21:31:43 -07001041 if (arg->fn(tp, f, arg) < 0) {
Jiri Pirko77b99002015-05-12 14:56:21 +02001042 arg->stop = 1;
1043 break;
1044 }
1045skip:
1046 arg->count++;
1047 }
1048}
1049
1050static int fl_dump_key_val(struct sk_buff *skb,
1051 void *val, int val_type,
1052 void *mask, int mask_type, int len)
1053{
1054 int err;
1055
1056 if (!memchr_inv(mask, 0, len))
1057 return 0;
1058 err = nla_put(skb, val_type, len, val);
1059 if (err)
1060 return err;
1061 if (mask_type != TCA_FLOWER_UNSPEC) {
1062 err = nla_put(skb, mask_type, len, mask);
1063 if (err)
1064 return err;
1065 }
1066 return 0;
1067}
1068
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001069static int fl_dump_key_mpls(struct sk_buff *skb,
1070 struct flow_dissector_key_mpls *mpls_key,
1071 struct flow_dissector_key_mpls *mpls_mask)
1072{
1073 int err;
1074
1075 if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
1076 return 0;
1077 if (mpls_mask->mpls_ttl) {
1078 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
1079 mpls_key->mpls_ttl);
1080 if (err)
1081 return err;
1082 }
1083 if (mpls_mask->mpls_tc) {
1084 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
1085 mpls_key->mpls_tc);
1086 if (err)
1087 return err;
1088 }
1089 if (mpls_mask->mpls_label) {
1090 err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
1091 mpls_key->mpls_label);
1092 if (err)
1093 return err;
1094 }
1095 if (mpls_mask->mpls_bos) {
1096 err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
1097 mpls_key->mpls_bos);
1098 if (err)
1099 return err;
1100 }
1101 return 0;
1102}
1103
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001104static int fl_dump_key_ip(struct sk_buff *skb,
1105 struct flow_dissector_key_ip *key,
1106 struct flow_dissector_key_ip *mask)
1107{
1108 if (fl_dump_key_val(skb, &key->tos, TCA_FLOWER_KEY_IP_TOS, &mask->tos,
1109 TCA_FLOWER_KEY_IP_TOS_MASK, sizeof(key->tos)) ||
1110 fl_dump_key_val(skb, &key->ttl, TCA_FLOWER_KEY_IP_TTL, &mask->ttl,
1111 TCA_FLOWER_KEY_IP_TTL_MASK, sizeof(key->ttl)))
1112 return -1;
1113
1114 return 0;
1115}
1116
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001117static int fl_dump_key_vlan(struct sk_buff *skb,
1118 struct flow_dissector_key_vlan *vlan_key,
1119 struct flow_dissector_key_vlan *vlan_mask)
1120{
1121 int err;
1122
1123 if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
1124 return 0;
1125 if (vlan_mask->vlan_id) {
1126 err = nla_put_u16(skb, TCA_FLOWER_KEY_VLAN_ID,
1127 vlan_key->vlan_id);
1128 if (err)
1129 return err;
1130 }
1131 if (vlan_mask->vlan_priority) {
1132 err = nla_put_u8(skb, TCA_FLOWER_KEY_VLAN_PRIO,
1133 vlan_key->vlan_priority);
1134 if (err)
1135 return err;
1136 }
1137 return 0;
1138}
1139
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001140static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
1141 u32 *flower_key, u32 *flower_mask,
1142 u32 flower_flag_bit, u32 dissector_flag_bit)
1143{
1144 if (dissector_mask & dissector_flag_bit) {
1145 *flower_mask |= flower_flag_bit;
1146 if (dissector_key & dissector_flag_bit)
1147 *flower_key |= flower_flag_bit;
1148 }
1149}
1150
1151static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
1152{
1153 u32 key, mask;
1154 __be32 _key, _mask;
1155 int err;
1156
1157 if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
1158 return 0;
1159
1160 key = 0;
1161 mask = 0;
1162
1163 fl_get_key_flag(flags_key, flags_mask, &key, &mask,
1164 TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
1165
1166 _key = cpu_to_be32(key);
1167 _mask = cpu_to_be32(mask);
1168
1169 err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
1170 if (err)
1171 return err;
1172
1173 return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
1174}
1175
WANG Cong8113c092017-08-04 21:31:43 -07001176static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
Jiri Pirko77b99002015-05-12 14:56:21 +02001177 struct sk_buff *skb, struct tcmsg *t)
1178{
1179 struct cls_fl_head *head = rtnl_dereference(tp->root);
WANG Cong8113c092017-08-04 21:31:43 -07001180 struct cls_fl_filter *f = fh;
Jiri Pirko77b99002015-05-12 14:56:21 +02001181 struct nlattr *nest;
1182 struct fl_flow_key *key, *mask;
1183
1184 if (!f)
1185 return skb->len;
1186
1187 t->tcm_handle = f->handle;
1188
1189 nest = nla_nest_start(skb, TCA_OPTIONS);
1190 if (!nest)
1191 goto nla_put_failure;
1192
1193 if (f->res.classid &&
1194 nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
1195 goto nla_put_failure;
1196
1197 key = &f->key;
1198 mask = &head->mask.key;
1199
1200 if (mask->indev_ifindex) {
1201 struct net_device *dev;
1202
1203 dev = __dev_get_by_index(net, key->indev_ifindex);
1204 if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
1205 goto nla_put_failure;
1206 }
1207
Hadar Hen Zion79685212016-12-01 14:06:34 +02001208 if (!tc_skip_hw(f->flags))
1209 fl_hw_update_stats(tp, f);
Amir Vadai10cbc682016-05-13 12:55:37 +00001210
Jiri Pirko77b99002015-05-12 14:56:21 +02001211 if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
1212 mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
1213 sizeof(key->eth.dst)) ||
1214 fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
1215 mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
1216 sizeof(key->eth.src)) ||
1217 fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
1218 &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
1219 sizeof(key->basic.n_proto)))
1220 goto nla_put_failure;
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001221
Benjamin LaHaisea577d8f2017-04-22 16:52:47 -04001222 if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
1223 goto nla_put_failure;
1224
Hadar Hen Zion9399ae92016-08-17 13:36:13 +03001225 if (fl_dump_key_vlan(skb, &key->vlan, &mask->vlan))
1226 goto nla_put_failure;
1227
Jiri Pirko77b99002015-05-12 14:56:21 +02001228 if ((key->basic.n_proto == htons(ETH_P_IP) ||
1229 key->basic.n_proto == htons(ETH_P_IPV6)) &&
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001230 (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
Jiri Pirko77b99002015-05-12 14:56:21 +02001231 &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
Or Gerlitz4d80cc02017-06-01 21:37:38 +03001232 sizeof(key->basic.ip_proto)) ||
1233 fl_dump_key_ip(skb, &key->ip, &mask->ip)))
Jiri Pirko77b99002015-05-12 14:56:21 +02001234 goto nla_put_failure;
1235
Tom Herbertc3f83242015-06-04 09:16:40 -07001236 if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001237 (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
1238 &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
1239 sizeof(key->ipv4.src)) ||
1240 fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
1241 &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
1242 sizeof(key->ipv4.dst))))
1243 goto nla_put_failure;
Tom Herbertc3f83242015-06-04 09:16:40 -07001244 else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
Jiri Pirko77b99002015-05-12 14:56:21 +02001245 (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
1246 &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
1247 sizeof(key->ipv6.src)) ||
1248 fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
1249 &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
1250 sizeof(key->ipv6.dst))))
1251 goto nla_put_failure;
1252
1253 if (key->basic.ip_proto == IPPROTO_TCP &&
1254 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001255 &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001256 sizeof(key->tp.src)) ||
1257 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001258 &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
Jiri Pirkofdfc7dd2017-05-23 18:40:45 +02001259 sizeof(key->tp.dst)) ||
1260 fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
1261 &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
1262 sizeof(key->tcp.flags))))
Jiri Pirko77b99002015-05-12 14:56:21 +02001263 goto nla_put_failure;
1264 else if (key->basic.ip_proto == IPPROTO_UDP &&
1265 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001266 &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001267 sizeof(key->tp.src)) ||
1268 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
Or Gerlitzaa72d702016-09-15 15:28:22 +03001269 &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
Jiri Pirko77b99002015-05-12 14:56:21 +02001270 sizeof(key->tp.dst))))
1271 goto nla_put_failure;
Simon Horman5976c5f2016-11-03 13:24:21 +01001272 else if (key->basic.ip_proto == IPPROTO_SCTP &&
1273 (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
1274 &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
1275 sizeof(key->tp.src)) ||
1276 fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
1277 &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
1278 sizeof(key->tp.dst))))
1279 goto nla_put_failure;
Simon Horman7b684882016-12-07 13:48:28 +01001280 else if (key->basic.n_proto == htons(ETH_P_IP) &&
1281 key->basic.ip_proto == IPPROTO_ICMP &&
1282 (fl_dump_key_val(skb, &key->icmp.type,
1283 TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
1284 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
1285 sizeof(key->icmp.type)) ||
1286 fl_dump_key_val(skb, &key->icmp.code,
1287 TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
1288 TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
1289 sizeof(key->icmp.code))))
1290 goto nla_put_failure;
1291 else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
1292 key->basic.ip_proto == IPPROTO_ICMPV6 &&
1293 (fl_dump_key_val(skb, &key->icmp.type,
1294 TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
1295 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
1296 sizeof(key->icmp.type)) ||
1297 fl_dump_key_val(skb, &key->icmp.code,
1298 TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
1299 TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
1300 sizeof(key->icmp.code))))
1301 goto nla_put_failure;
Simon Horman99d31322017-01-11 14:05:43 +01001302 else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
1303 key->basic.n_proto == htons(ETH_P_RARP)) &&
1304 (fl_dump_key_val(skb, &key->arp.sip,
1305 TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
1306 TCA_FLOWER_KEY_ARP_SIP_MASK,
1307 sizeof(key->arp.sip)) ||
1308 fl_dump_key_val(skb, &key->arp.tip,
1309 TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
1310 TCA_FLOWER_KEY_ARP_TIP_MASK,
1311 sizeof(key->arp.tip)) ||
1312 fl_dump_key_val(skb, &key->arp.op,
1313 TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
1314 TCA_FLOWER_KEY_ARP_OP_MASK,
1315 sizeof(key->arp.op)) ||
1316 fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
1317 mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
1318 sizeof(key->arp.sha)) ||
1319 fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
1320 mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
1321 sizeof(key->arp.tha))))
1322 goto nla_put_failure;
Jiri Pirko77b99002015-05-12 14:56:21 +02001323
Amir Vadaibc3103f2016-09-08 16:23:47 +03001324 if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
1325 (fl_dump_key_val(skb, &key->enc_ipv4.src,
1326 TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
1327 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
1328 sizeof(key->enc_ipv4.src)) ||
1329 fl_dump_key_val(skb, &key->enc_ipv4.dst,
1330 TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
1331 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
1332 sizeof(key->enc_ipv4.dst))))
1333 goto nla_put_failure;
1334 else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
1335 (fl_dump_key_val(skb, &key->enc_ipv6.src,
1336 TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
1337 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
1338 sizeof(key->enc_ipv6.src)) ||
1339 fl_dump_key_val(skb, &key->enc_ipv6.dst,
1340 TCA_FLOWER_KEY_ENC_IPV6_DST,
1341 &mask->enc_ipv6.dst,
1342 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
1343 sizeof(key->enc_ipv6.dst))))
1344 goto nla_put_failure;
1345
1346 if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
Hadar Hen Zioneb523f42016-09-27 11:21:18 +03001347 &mask->enc_key_id, TCA_FLOWER_UNSPEC,
Hadar Hen Zionf4d997f2016-11-07 15:14:39 +02001348 sizeof(key->enc_key_id)) ||
1349 fl_dump_key_val(skb, &key->enc_tp.src,
1350 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
1351 &mask->enc_tp.src,
1352 TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
1353 sizeof(key->enc_tp.src)) ||
1354 fl_dump_key_val(skb, &key->enc_tp.dst,
1355 TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
1356 &mask->enc_tp.dst,
1357 TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
1358 sizeof(key->enc_tp.dst)))
Amir Vadaibc3103f2016-09-08 16:23:47 +03001359 goto nla_put_failure;
1360
Or Gerlitzfaa3ffc2016-12-07 14:03:10 +02001361 if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
1362 goto nla_put_failure;
1363
Or Gerlitz749e6722017-02-16 10:31:10 +02001364 if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
1365 goto nla_put_failure;
Amir Vadaie69985c2016-06-05 17:11:18 +03001366
Jiri Pirko77b99002015-05-12 14:56:21 +02001367 if (tcf_exts_dump(skb, &f->exts))
1368 goto nla_put_failure;
1369
1370 nla_nest_end(skb, nest);
1371
1372 if (tcf_exts_dump_stats(skb, &f->exts) < 0)
1373 goto nla_put_failure;
1374
1375 return skb->len;
1376
1377nla_put_failure:
1378 nla_nest_cancel(skb, nest);
1379 return -1;
1380}
1381
Cong Wang07d79fc2017-08-30 14:30:36 -07001382static void fl_bind_class(void *fh, u32 classid, unsigned long cl)
1383{
1384 struct cls_fl_filter *f = fh;
1385
1386 if (f && f->res.classid == classid)
1387 f->res.class = cl;
1388}
1389
Jiri Pirko77b99002015-05-12 14:56:21 +02001390static struct tcf_proto_ops cls_fl_ops __read_mostly = {
1391 .kind = "flower",
1392 .classify = fl_classify,
1393 .init = fl_init,
1394 .destroy = fl_destroy,
1395 .get = fl_get,
1396 .change = fl_change,
1397 .delete = fl_delete,
1398 .walk = fl_walk,
1399 .dump = fl_dump,
Cong Wang07d79fc2017-08-30 14:30:36 -07001400 .bind_class = fl_bind_class,
Jiri Pirko77b99002015-05-12 14:56:21 +02001401 .owner = THIS_MODULE,
1402};
1403
1404static int __init cls_fl_init(void)
1405{
1406 return register_tcf_proto_ops(&cls_fl_ops);
1407}
1408
1409static void __exit cls_fl_exit(void)
1410{
1411 unregister_tcf_proto_ops(&cls_fl_ops);
1412}
1413
1414module_init(cls_fl_init);
1415module_exit(cls_fl_exit);
1416
1417MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
1418MODULE_DESCRIPTION("Flower classifier");
1419MODULE_LICENSE("GPL v2");