Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1 | /* |
| 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 Borkmann | d936377 | 2016-11-27 01:18:01 +0100 | [diff] [blame] | 16 | #include <linux/workqueue.h> |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 17 | |
| 18 | #include <linux/if_ether.h> |
| 19 | #include <linux/in6.h> |
| 20 | #include <linux/ip.h> |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 21 | #include <linux/mpls.h> |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 22 | |
| 23 | #include <net/sch_generic.h> |
| 24 | #include <net/pkt_cls.h> |
| 25 | #include <net/ip.h> |
| 26 | #include <net/flow_dissector.h> |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 27 | #include <net/geneve.h> |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 28 | |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 29 | #include <net/dst.h> |
| 30 | #include <net/dst_metadata.h> |
| 31 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 32 | struct fl_flow_key { |
| 33 | int indev_ifindex; |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 34 | struct flow_dissector_key_control control; |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 35 | struct flow_dissector_key_control enc_control; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 36 | struct flow_dissector_key_basic basic; |
| 37 | struct flow_dissector_key_eth_addrs eth; |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 38 | struct flow_dissector_key_vlan vlan; |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 39 | struct flow_dissector_key_vlan cvlan; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 40 | union { |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 41 | struct flow_dissector_key_ipv4_addrs ipv4; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 42 | struct flow_dissector_key_ipv6_addrs ipv6; |
| 43 | }; |
| 44 | struct flow_dissector_key_ports tp; |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 45 | struct flow_dissector_key_icmp icmp; |
Simon Horman | 99d3132 | 2017-01-11 14:05:43 +0100 | [diff] [blame] | 46 | struct flow_dissector_key_arp arp; |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 47 | struct flow_dissector_key_keyid enc_key_id; |
| 48 | union { |
| 49 | struct flow_dissector_key_ipv4_addrs enc_ipv4; |
| 50 | struct flow_dissector_key_ipv6_addrs enc_ipv6; |
| 51 | }; |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 52 | struct flow_dissector_key_ports enc_tp; |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 53 | struct flow_dissector_key_mpls mpls; |
Jiri Pirko | fdfc7dd | 2017-05-23 18:40:45 +0200 | [diff] [blame] | 54 | struct flow_dissector_key_tcp tcp; |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 55 | struct flow_dissector_key_ip ip; |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 56 | struct flow_dissector_key_ip enc_ip; |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 57 | struct flow_dissector_key_enc_opts enc_opts; |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 58 | struct flow_dissector_key_ports tp_min; |
| 59 | struct flow_dissector_key_ports tp_max; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 60 | } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ |
| 61 | |
| 62 | struct fl_flow_mask_range { |
| 63 | unsigned short int start; |
| 64 | unsigned short int end; |
| 65 | }; |
| 66 | |
| 67 | struct fl_flow_mask { |
| 68 | struct fl_flow_key key; |
| 69 | struct fl_flow_mask_range range; |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 70 | u32 flags; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 71 | struct rhash_head ht_node; |
| 72 | struct rhashtable ht; |
| 73 | struct rhashtable_params filter_ht_params; |
| 74 | struct flow_dissector dissector; |
| 75 | struct list_head filters; |
Paolo Abeni | 44a5cd4 | 2018-06-21 20:02:16 +0200 | [diff] [blame] | 76 | struct rcu_work rwork; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 77 | struct list_head list; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 78 | }; |
| 79 | |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 80 | struct fl_flow_tmplt { |
| 81 | struct fl_flow_key dummy_key; |
| 82 | struct fl_flow_key mask; |
| 83 | struct flow_dissector dissector; |
| 84 | struct tcf_chain *chain; |
| 85 | }; |
| 86 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 87 | struct cls_fl_head { |
| 88 | struct rhashtable ht; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 89 | struct list_head masks; |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 90 | struct rcu_work rwork; |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 91 | struct idr handle_idr; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | struct cls_fl_filter { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 95 | struct fl_flow_mask *mask; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 96 | struct rhash_head ht_node; |
| 97 | struct fl_flow_key mkey; |
| 98 | struct tcf_exts exts; |
| 99 | struct tcf_result res; |
| 100 | struct fl_flow_key key; |
| 101 | struct list_head list; |
| 102 | u32 handle; |
Amir Vadai | e69985c | 2016-06-05 17:11:18 +0300 | [diff] [blame] | 103 | u32 flags; |
Vlad Buslov | 86c5536 | 2018-09-07 17:22:21 +0300 | [diff] [blame] | 104 | u32 in_hw_count; |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 105 | struct rcu_work rwork; |
Hadar Hen Zion | 7091d8c | 2016-12-01 14:06:37 +0200 | [diff] [blame] | 106 | struct net_device *hw_dev; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 109 | static const struct rhashtable_params mask_ht_params = { |
| 110 | .key_offset = offsetof(struct fl_flow_mask, key), |
| 111 | .key_len = sizeof(struct fl_flow_key), |
| 112 | .head_offset = offsetof(struct fl_flow_mask, ht_node), |
| 113 | .automatic_shrinking = true, |
| 114 | }; |
| 115 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 116 | static unsigned short int fl_mask_range(const struct fl_flow_mask *mask) |
| 117 | { |
| 118 | return mask->range.end - mask->range.start; |
| 119 | } |
| 120 | |
| 121 | static void fl_mask_update_range(struct fl_flow_mask *mask) |
| 122 | { |
| 123 | const u8 *bytes = (const u8 *) &mask->key; |
| 124 | size_t size = sizeof(mask->key); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 125 | size_t i, first = 0, last; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 126 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 127 | for (i = 0; i < size; i++) { |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 128 | if (bytes[i]) { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 129 | first = i; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | last = first; |
| 134 | for (i = size - 1; i != first; i--) { |
| 135 | if (bytes[i]) { |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 136 | last = i; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 137 | break; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | mask->range.start = rounddown(first, sizeof(long)); |
| 141 | mask->range.end = roundup(last + 1, sizeof(long)); |
| 142 | } |
| 143 | |
| 144 | static void *fl_key_get_start(struct fl_flow_key *key, |
| 145 | const struct fl_flow_mask *mask) |
| 146 | { |
| 147 | return (u8 *) key + mask->range.start; |
| 148 | } |
| 149 | |
| 150 | static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key, |
| 151 | struct fl_flow_mask *mask) |
| 152 | { |
| 153 | const long *lkey = fl_key_get_start(key, mask); |
| 154 | const long *lmask = fl_key_get_start(&mask->key, mask); |
| 155 | long *lmkey = fl_key_get_start(mkey, mask); |
| 156 | int i; |
| 157 | |
| 158 | for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) |
| 159 | *lmkey++ = *lkey++ & *lmask++; |
| 160 | } |
| 161 | |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 162 | static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt, |
| 163 | struct fl_flow_mask *mask) |
| 164 | { |
| 165 | const long *lmask = fl_key_get_start(&mask->key, mask); |
| 166 | const long *ltmplt; |
| 167 | int i; |
| 168 | |
| 169 | if (!tmplt) |
| 170 | return true; |
| 171 | ltmplt = fl_key_get_start(&tmplt->mask, mask); |
| 172 | for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) { |
| 173 | if (~*ltmplt++ & *lmask++) |
| 174 | return false; |
| 175 | } |
| 176 | return true; |
| 177 | } |
| 178 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 179 | static void fl_clear_masked_range(struct fl_flow_key *key, |
| 180 | struct fl_flow_mask *mask) |
| 181 | { |
| 182 | memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask)); |
| 183 | } |
| 184 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 185 | static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter, |
| 186 | struct fl_flow_key *key, |
| 187 | struct fl_flow_key *mkey) |
| 188 | { |
| 189 | __be16 min_mask, max_mask, min_val, max_val; |
| 190 | |
| 191 | min_mask = htons(filter->mask->key.tp_min.dst); |
| 192 | max_mask = htons(filter->mask->key.tp_max.dst); |
| 193 | min_val = htons(filter->key.tp_min.dst); |
| 194 | max_val = htons(filter->key.tp_max.dst); |
| 195 | |
| 196 | if (min_mask && max_mask) { |
| 197 | if (htons(key->tp.dst) < min_val || |
| 198 | htons(key->tp.dst) > max_val) |
| 199 | return false; |
| 200 | |
| 201 | /* skb does not have min and max values */ |
| 202 | mkey->tp_min.dst = filter->mkey.tp_min.dst; |
| 203 | mkey->tp_max.dst = filter->mkey.tp_max.dst; |
| 204 | } |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | static bool fl_range_port_src_cmp(struct cls_fl_filter *filter, |
| 209 | struct fl_flow_key *key, |
| 210 | struct fl_flow_key *mkey) |
| 211 | { |
| 212 | __be16 min_mask, max_mask, min_val, max_val; |
| 213 | |
| 214 | min_mask = htons(filter->mask->key.tp_min.src); |
| 215 | max_mask = htons(filter->mask->key.tp_max.src); |
| 216 | min_val = htons(filter->key.tp_min.src); |
| 217 | max_val = htons(filter->key.tp_max.src); |
| 218 | |
| 219 | if (min_mask && max_mask) { |
| 220 | if (htons(key->tp.src) < min_val || |
| 221 | htons(key->tp.src) > max_val) |
| 222 | return false; |
| 223 | |
| 224 | /* skb does not have min and max values */ |
| 225 | mkey->tp_min.src = filter->mkey.tp_min.src; |
| 226 | mkey->tp_max.src = filter->mkey.tp_max.src; |
| 227 | } |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask, |
| 232 | struct fl_flow_key *mkey) |
Paul Blakey | a3308d8 | 2017-01-16 10:45:13 +0200 | [diff] [blame] | 233 | { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 234 | return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask), |
| 235 | mask->filter_ht_params); |
Paul Blakey | a3308d8 | 2017-01-16 10:45:13 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 238 | static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask, |
| 239 | struct fl_flow_key *mkey, |
| 240 | struct fl_flow_key *key) |
| 241 | { |
| 242 | struct cls_fl_filter *filter, *f; |
| 243 | |
| 244 | list_for_each_entry_rcu(filter, &mask->filters, list) { |
| 245 | if (!fl_range_port_dst_cmp(filter, key, mkey)) |
| 246 | continue; |
| 247 | |
| 248 | if (!fl_range_port_src_cmp(filter, key, mkey)) |
| 249 | continue; |
| 250 | |
| 251 | f = __fl_lookup(mask, mkey); |
| 252 | if (f) |
| 253 | return f; |
| 254 | } |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask, |
| 259 | struct fl_flow_key *mkey, |
| 260 | struct fl_flow_key *key) |
| 261 | { |
| 262 | if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE)) |
| 263 | return fl_lookup_range(mask, mkey, key); |
| 264 | |
| 265 | return __fl_lookup(mask, mkey); |
| 266 | } |
| 267 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 268 | static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 269 | struct tcf_result *res) |
| 270 | { |
| 271 | struct cls_fl_head *head = rcu_dereference_bh(tp->root); |
| 272 | struct cls_fl_filter *f; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 273 | struct fl_flow_mask *mask; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 274 | struct fl_flow_key skb_key; |
| 275 | struct fl_flow_key skb_mkey; |
| 276 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 277 | list_for_each_entry_rcu(mask, &head->masks, list) { |
| 278 | fl_clear_masked_range(&skb_key, mask); |
Amir Vadai | e69985c | 2016-06-05 17:11:18 +0300 | [diff] [blame] | 279 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 280 | skb_key.indev_ifindex = skb->skb_iif; |
| 281 | /* skb_flow_dissect() does not set n_proto in case an unknown |
| 282 | * protocol, so do it rather here. |
| 283 | */ |
| 284 | skb_key.basic.n_proto = skb->protocol; |
| 285 | skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key); |
| 286 | skb_flow_dissect(skb, &mask->dissector, &skb_key, 0); |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 287 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 288 | fl_set_masked_key(&skb_mkey, &skb_key, mask); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 289 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 290 | f = fl_lookup(mask, &skb_mkey, &skb_key); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 291 | if (f && !tc_skip_sw(f->flags)) { |
| 292 | *res = f->res; |
| 293 | return tcf_exts_exec(skb, &f->exts, res); |
| 294 | } |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 295 | } |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | static int fl_init(struct tcf_proto *tp) |
| 300 | { |
| 301 | struct cls_fl_head *head; |
| 302 | |
| 303 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 304 | if (!head) |
| 305 | return -ENOBUFS; |
| 306 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 307 | INIT_LIST_HEAD_RCU(&head->masks); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 308 | rcu_assign_pointer(tp->root, head); |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 309 | idr_init(&head->handle_idr); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 310 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 311 | return rhashtable_init(&head->ht, &mask_ht_params); |
| 312 | } |
| 313 | |
Paolo Abeni | 44a5cd4 | 2018-06-21 20:02:16 +0200 | [diff] [blame] | 314 | static void fl_mask_free(struct fl_flow_mask *mask) |
| 315 | { |
| 316 | rhashtable_destroy(&mask->ht); |
| 317 | kfree(mask); |
| 318 | } |
| 319 | |
| 320 | static void fl_mask_free_work(struct work_struct *work) |
| 321 | { |
| 322 | struct fl_flow_mask *mask = container_of(to_rcu_work(work), |
| 323 | struct fl_flow_mask, rwork); |
| 324 | |
| 325 | fl_mask_free(mask); |
| 326 | } |
| 327 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 328 | static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask, |
| 329 | bool async) |
| 330 | { |
| 331 | if (!list_empty(&mask->filters)) |
| 332 | return false; |
| 333 | |
| 334 | rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 335 | list_del_rcu(&mask->list); |
| 336 | if (async) |
Paolo Abeni | 44a5cd4 | 2018-06-21 20:02:16 +0200 | [diff] [blame] | 337 | tcf_queue_work(&mask->rwork, fl_mask_free_work); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 338 | else |
Paolo Abeni | 44a5cd4 | 2018-06-21 20:02:16 +0200 | [diff] [blame] | 339 | fl_mask_free(mask); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 340 | |
| 341 | return true; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Cong Wang | 0dadc11 | 2017-11-06 13:47:24 -0800 | [diff] [blame] | 344 | static void __fl_destroy_filter(struct cls_fl_filter *f) |
| 345 | { |
| 346 | tcf_exts_destroy(&f->exts); |
| 347 | tcf_exts_put_net(&f->exts); |
| 348 | kfree(f); |
| 349 | } |
| 350 | |
Cong Wang | 0552c8a | 2017-10-26 18:24:33 -0700 | [diff] [blame] | 351 | static void fl_destroy_filter_work(struct work_struct *work) |
| 352 | { |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 353 | struct cls_fl_filter *f = container_of(to_rcu_work(work), |
| 354 | struct cls_fl_filter, rwork); |
Cong Wang | 0552c8a | 2017-10-26 18:24:33 -0700 | [diff] [blame] | 355 | |
| 356 | rtnl_lock(); |
Cong Wang | 0dadc11 | 2017-11-06 13:47:24 -0800 | [diff] [blame] | 357 | __fl_destroy_filter(f); |
Cong Wang | 0552c8a | 2017-10-26 18:24:33 -0700 | [diff] [blame] | 358 | rtnl_unlock(); |
| 359 | } |
| 360 | |
Jakub Kicinski | 1b0f803 | 2018-01-24 12:54:21 -0800 | [diff] [blame] | 361 | static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f, |
| 362 | struct netlink_ext_ack *extack) |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 363 | { |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 364 | struct tc_cls_flower_offload cls_flower = {}; |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 365 | struct tcf_block *block = tp->chain->block; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 366 | |
Jakub Kicinski | 1b0f803 | 2018-01-24 12:54:21 -0800 | [diff] [blame] | 367 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 368 | cls_flower.command = TC_CLSFLOWER_DESTROY; |
| 369 | cls_flower.cookie = (unsigned long) f; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 370 | |
Cong Wang | aeb3fec | 2018-12-11 11:15:46 -0800 | [diff] [blame] | 371 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false); |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 372 | tcf_block_offload_dec(block, &f->flags); |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 373 | } |
| 374 | |
Amir Vadai | e8eb36c | 2016-06-13 12:06:39 +0300 | [diff] [blame] | 375 | static int fl_hw_replace_filter(struct tcf_proto *tp, |
Quentin Monnet | 4100203 | 2018-01-19 17:44:43 -0800 | [diff] [blame] | 376 | struct cls_fl_filter *f, |
| 377 | struct netlink_ext_ack *extack) |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 378 | { |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 379 | struct tc_cls_flower_offload cls_flower = {}; |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 380 | struct tcf_block *block = tp->chain->block; |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 381 | bool skip_sw = tc_skip_sw(f->flags); |
Amir Vadai | e8eb36c | 2016-06-13 12:06:39 +0300 | [diff] [blame] | 382 | int err; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 383 | |
Pablo Neira Ayuso | e3ab786 | 2019-02-02 12:50:45 +0100 | [diff] [blame] | 384 | cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts)); |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 385 | if (!cls_flower.rule) |
| 386 | return -ENOMEM; |
| 387 | |
Jakub Kicinski | ea20594 | 2018-01-24 12:54:20 -0800 | [diff] [blame] | 388 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack); |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 389 | cls_flower.command = TC_CLSFLOWER_REPLACE; |
| 390 | cls_flower.cookie = (unsigned long) f; |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 391 | cls_flower.rule->match.dissector = &f->mask->dissector; |
| 392 | cls_flower.rule->match.mask = &f->mask->key; |
| 393 | cls_flower.rule->match.key = &f->mkey; |
Amritha Nambiar | 384c181 | 2017-10-27 02:35:34 -0700 | [diff] [blame] | 394 | cls_flower.classid = f->res.classid; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 395 | |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 396 | err = tc_setup_flow_action(&cls_flower.rule->action, &f->exts); |
| 397 | if (err) { |
| 398 | kfree(cls_flower.rule); |
Vlad Buslov | 1f15bb4 | 2019-02-12 23:39:06 +0200 | [diff] [blame] | 399 | if (skip_sw) { |
| 400 | NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); |
| 401 | return err; |
| 402 | } |
| 403 | return 0; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Cong Wang | aeb3fec | 2018-12-11 11:15:46 -0800 | [diff] [blame] | 406 | err = tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, skip_sw); |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 407 | kfree(cls_flower.rule); |
| 408 | |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 409 | if (err < 0) { |
Jakub Kicinski | 1b0f803 | 2018-01-24 12:54:21 -0800 | [diff] [blame] | 410 | fl_hw_destroy_filter(tp, f, NULL); |
Amir Vadai | e8eb36c | 2016-06-13 12:06:39 +0300 | [diff] [blame] | 411 | return err; |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 412 | } else if (err > 0) { |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 413 | f->in_hw_count = err; |
Jiri Pirko | caa7260 | 2018-01-17 11:46:50 +0100 | [diff] [blame] | 414 | tcf_block_offload_inc(block, &f->flags); |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW)) |
| 418 | return -EINVAL; |
| 419 | |
Amir Vadai | e8eb36c | 2016-06-13 12:06:39 +0300 | [diff] [blame] | 420 | return 0; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 423 | static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f) |
| 424 | { |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 425 | struct tc_cls_flower_offload cls_flower = {}; |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 426 | struct tcf_block *block = tp->chain->block; |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 427 | |
Jakub Kicinski | ea20594 | 2018-01-24 12:54:20 -0800 | [diff] [blame] | 428 | tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL); |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 429 | cls_flower.command = TC_CLSFLOWER_STATS; |
| 430 | cls_flower.cookie = (unsigned long) f; |
Amritha Nambiar | 384c181 | 2017-10-27 02:35:34 -0700 | [diff] [blame] | 431 | cls_flower.classid = f->res.classid; |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 432 | |
Cong Wang | aeb3fec | 2018-12-11 11:15:46 -0800 | [diff] [blame] | 433 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false); |
Pablo Neira Ayuso | 3b1903e | 2019-02-02 12:50:47 +0100 | [diff] [blame] | 434 | |
| 435 | tcf_exts_stats_update(&f->exts, cls_flower.stats.bytes, |
| 436 | cls_flower.stats.pkts, |
| 437 | cls_flower.stats.lastused); |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame^] | 440 | static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp) |
| 441 | { |
| 442 | /* Flower classifier only changes root pointer during init and destroy. |
| 443 | * Users must obtain reference to tcf_proto instance before calling its |
| 444 | * API, so tp->root pointer is protected from concurrent call to |
| 445 | * fl_destroy() by reference counting. |
| 446 | */ |
| 447 | return rcu_dereference_raw(tp->root); |
| 448 | } |
| 449 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 450 | static bool __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f, |
Jakub Kicinski | 1b0f803 | 2018-01-24 12:54:21 -0800 | [diff] [blame] | 451 | struct netlink_ext_ack *extack) |
Roi Dayan | 13fa876 | 2016-11-01 16:08:29 +0200 | [diff] [blame] | 452 | { |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame^] | 453 | struct cls_fl_head *head = fl_head_dereference(tp); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 454 | bool async = tcf_exts_get_net(&f->exts); |
| 455 | bool last; |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 456 | |
Matthew Wilcox | 9c16094 | 2017-11-28 09:48:43 -0500 | [diff] [blame] | 457 | idr_remove(&head->handle_idr, f->handle); |
Roi Dayan | 13fa876 | 2016-11-01 16:08:29 +0200 | [diff] [blame] | 458 | list_del_rcu(&f->list); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 459 | last = fl_mask_put(head, f->mask, async); |
Hadar Hen Zion | 7968521 | 2016-12-01 14:06:34 +0200 | [diff] [blame] | 460 | if (!tc_skip_hw(f->flags)) |
Jakub Kicinski | 1b0f803 | 2018-01-24 12:54:21 -0800 | [diff] [blame] | 461 | fl_hw_destroy_filter(tp, f, extack); |
Roi Dayan | 13fa876 | 2016-11-01 16:08:29 +0200 | [diff] [blame] | 462 | tcf_unbind_filter(tp, &f->res); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 463 | if (async) |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 464 | tcf_queue_work(&f->rwork, fl_destroy_filter_work); |
Cong Wang | 0dadc11 | 2017-11-06 13:47:24 -0800 | [diff] [blame] | 465 | else |
| 466 | __fl_destroy_filter(f); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 467 | |
| 468 | return last; |
Roi Dayan | 13fa876 | 2016-11-01 16:08:29 +0200 | [diff] [blame] | 469 | } |
| 470 | |
Daniel Borkmann | d936377 | 2016-11-27 01:18:01 +0100 | [diff] [blame] | 471 | static void fl_destroy_sleepable(struct work_struct *work) |
| 472 | { |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 473 | struct cls_fl_head *head = container_of(to_rcu_work(work), |
| 474 | struct cls_fl_head, |
| 475 | rwork); |
Paul Blakey | de9dc65 | 2018-06-03 10:06:13 +0300 | [diff] [blame] | 476 | |
| 477 | rhashtable_destroy(&head->ht); |
Daniel Borkmann | d936377 | 2016-11-27 01:18:01 +0100 | [diff] [blame] | 478 | kfree(head); |
| 479 | module_put(THIS_MODULE); |
| 480 | } |
| 481 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 482 | static void fl_destroy(struct tcf_proto *tp, bool rtnl_held, |
| 483 | struct netlink_ext_ack *extack) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 484 | { |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame^] | 485 | struct cls_fl_head *head = fl_head_dereference(tp); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 486 | struct fl_flow_mask *mask, *next_mask; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 487 | struct cls_fl_filter *f, *next; |
| 488 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 489 | list_for_each_entry_safe(mask, next_mask, &head->masks, list) { |
| 490 | list_for_each_entry_safe(f, next, &mask->filters, list) { |
| 491 | if (__fl_delete(tp, f, extack)) |
| 492 | break; |
| 493 | } |
| 494 | } |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 495 | idr_destroy(&head->handle_idr); |
Daniel Borkmann | d936377 | 2016-11-27 01:18:01 +0100 | [diff] [blame] | 496 | |
| 497 | __module_get(THIS_MODULE); |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 498 | tcf_queue_work(&head->rwork, fl_destroy_sleepable); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 499 | } |
| 500 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 501 | static void *fl_get(struct tcf_proto *tp, u32 handle) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 502 | { |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame^] | 503 | struct cls_fl_head *head = fl_head_dereference(tp); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 504 | |
Matthew Wilcox | 322d884 | 2017-11-28 10:01:24 -0500 | [diff] [blame] | 505 | return idr_find(&head->handle_idr, handle); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = { |
| 509 | [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC }, |
| 510 | [TCA_FLOWER_CLASSID] = { .type = NLA_U32 }, |
| 511 | [TCA_FLOWER_INDEV] = { .type = NLA_STRING, |
| 512 | .len = IFNAMSIZ }, |
| 513 | [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN }, |
| 514 | [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN }, |
| 515 | [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN }, |
| 516 | [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN }, |
| 517 | [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 }, |
| 518 | [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 }, |
| 519 | [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 }, |
| 520 | [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 }, |
| 521 | [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 }, |
| 522 | [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 }, |
| 523 | [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, |
| 524 | [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, |
| 525 | [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) }, |
| 526 | [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, |
| 527 | [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 }, |
| 528 | [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 }, |
Jamal Hadi Salim | b175c3a | 2015-06-25 06:55:27 -0400 | [diff] [blame] | 529 | [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 }, |
| 530 | [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 }, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 531 | [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 }, |
| 532 | [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 }, |
| 533 | [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 }, |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 534 | [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, |
| 535 | [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, |
| 536 | [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 }, |
| 537 | [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, |
| 538 | [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 }, |
| 539 | [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, |
| 540 | [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, |
| 541 | [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, |
| 542 | [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, |
Or Gerlitz | aa72d70 | 2016-09-15 15:28:22 +0300 | [diff] [blame] | 543 | [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 }, |
| 544 | [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 }, |
| 545 | [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 }, |
| 546 | [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 }, |
Simon Horman | 5976c5f | 2016-11-03 13:24:21 +0100 | [diff] [blame] | 547 | [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 }, |
| 548 | [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 }, |
| 549 | [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 }, |
| 550 | [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 }, |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 551 | [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 }, |
| 552 | [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 }, |
| 553 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 }, |
| 554 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 }, |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 555 | [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 }, |
| 556 | [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 }, |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 557 | [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 }, |
| 558 | [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 }, |
| 559 | [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 }, |
| 560 | [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 }, |
| 561 | [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 }, |
| 562 | [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 }, |
| 563 | [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 }, |
| 564 | [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 }, |
Simon Horman | 99d3132 | 2017-01-11 14:05:43 +0100 | [diff] [blame] | 565 | [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 }, |
| 566 | [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 }, |
| 567 | [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 }, |
| 568 | [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 }, |
| 569 | [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 }, |
| 570 | [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 }, |
| 571 | [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN }, |
| 572 | [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN }, |
| 573 | [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN }, |
| 574 | [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN }, |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 575 | [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 }, |
| 576 | [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 }, |
| 577 | [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 }, |
| 578 | [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 }, |
Jiri Pirko | fdfc7dd | 2017-05-23 18:40:45 +0200 | [diff] [blame] | 579 | [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 }, |
| 580 | [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 }, |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 581 | [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 }, |
| 582 | [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 }, |
| 583 | [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 }, |
| 584 | [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 }, |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 585 | [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 }, |
| 586 | [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 }, |
| 587 | [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 }, |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 588 | [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 }, |
| 589 | [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 }, |
| 590 | [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 }, |
| 591 | [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 }, |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 592 | [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED }, |
| 593 | [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED }, |
| 594 | }; |
| 595 | |
| 596 | static const struct nla_policy |
| 597 | enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = { |
| 598 | [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED }, |
| 599 | }; |
| 600 | |
| 601 | static const struct nla_policy |
| 602 | geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = { |
| 603 | [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 }, |
| 604 | [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 }, |
| 605 | [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY, |
| 606 | .len = 128 }, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 607 | }; |
| 608 | |
| 609 | static void fl_set_key_val(struct nlattr **tb, |
| 610 | void *val, int val_type, |
| 611 | void *mask, int mask_type, int len) |
| 612 | { |
| 613 | if (!tb[val_type]) |
| 614 | return; |
| 615 | memcpy(val, nla_data(tb[val_type]), len); |
| 616 | if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type]) |
| 617 | memset(mask, 0xff, len); |
| 618 | else |
| 619 | memcpy(mask, nla_data(tb[mask_type]), len); |
| 620 | } |
| 621 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 622 | static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key, |
| 623 | struct fl_flow_key *mask) |
| 624 | { |
| 625 | fl_set_key_val(tb, &key->tp_min.dst, |
| 626 | TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_min.dst, |
| 627 | TCA_FLOWER_UNSPEC, sizeof(key->tp_min.dst)); |
| 628 | fl_set_key_val(tb, &key->tp_max.dst, |
| 629 | TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_max.dst, |
| 630 | TCA_FLOWER_UNSPEC, sizeof(key->tp_max.dst)); |
| 631 | fl_set_key_val(tb, &key->tp_min.src, |
| 632 | TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_min.src, |
| 633 | TCA_FLOWER_UNSPEC, sizeof(key->tp_min.src)); |
| 634 | fl_set_key_val(tb, &key->tp_max.src, |
| 635 | TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_max.src, |
| 636 | TCA_FLOWER_UNSPEC, sizeof(key->tp_max.src)); |
| 637 | |
| 638 | if ((mask->tp_min.dst && mask->tp_max.dst && |
| 639 | htons(key->tp_max.dst) <= htons(key->tp_min.dst)) || |
| 640 | (mask->tp_min.src && mask->tp_max.src && |
| 641 | htons(key->tp_max.src) <= htons(key->tp_min.src))) |
| 642 | return -EINVAL; |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 647 | static int fl_set_key_mpls(struct nlattr **tb, |
| 648 | struct flow_dissector_key_mpls *key_val, |
| 649 | struct flow_dissector_key_mpls *key_mask) |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 650 | { |
| 651 | if (tb[TCA_FLOWER_KEY_MPLS_TTL]) { |
| 652 | key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]); |
| 653 | key_mask->mpls_ttl = MPLS_TTL_MASK; |
| 654 | } |
| 655 | if (tb[TCA_FLOWER_KEY_MPLS_BOS]) { |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 656 | u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]); |
| 657 | |
| 658 | if (bos & ~MPLS_BOS_MASK) |
| 659 | return -EINVAL; |
| 660 | key_val->mpls_bos = bos; |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 661 | key_mask->mpls_bos = MPLS_BOS_MASK; |
| 662 | } |
| 663 | if (tb[TCA_FLOWER_KEY_MPLS_TC]) { |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 664 | u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]); |
| 665 | |
| 666 | if (tc & ~MPLS_TC_MASK) |
| 667 | return -EINVAL; |
| 668 | key_val->mpls_tc = tc; |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 669 | key_mask->mpls_tc = MPLS_TC_MASK; |
| 670 | } |
| 671 | if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) { |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 672 | u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]); |
| 673 | |
| 674 | if (label & ~MPLS_LABEL_MASK) |
| 675 | return -EINVAL; |
| 676 | key_val->mpls_label = label; |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 677 | key_mask->mpls_label = MPLS_LABEL_MASK; |
| 678 | } |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 679 | return 0; |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 680 | } |
| 681 | |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 682 | static void fl_set_key_vlan(struct nlattr **tb, |
Jianbo Liu | aaab083 | 2018-07-06 05:38:13 +0000 | [diff] [blame] | 683 | __be16 ethertype, |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 684 | int vlan_id_key, int vlan_prio_key, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 685 | struct flow_dissector_key_vlan *key_val, |
| 686 | struct flow_dissector_key_vlan *key_mask) |
| 687 | { |
| 688 | #define VLAN_PRIORITY_MASK 0x7 |
| 689 | |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 690 | if (tb[vlan_id_key]) { |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 691 | key_val->vlan_id = |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 692 | nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK; |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 693 | key_mask->vlan_id = VLAN_VID_MASK; |
| 694 | } |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 695 | if (tb[vlan_prio_key]) { |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 696 | key_val->vlan_priority = |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 697 | nla_get_u8(tb[vlan_prio_key]) & |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 698 | VLAN_PRIORITY_MASK; |
| 699 | key_mask->vlan_priority = VLAN_PRIORITY_MASK; |
| 700 | } |
Jianbo Liu | aaab083 | 2018-07-06 05:38:13 +0000 | [diff] [blame] | 701 | key_val->vlan_tpid = ethertype; |
| 702 | key_mask->vlan_tpid = cpu_to_be16(~0); |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 703 | } |
| 704 | |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 705 | static void fl_set_key_flag(u32 flower_key, u32 flower_mask, |
| 706 | u32 *dissector_key, u32 *dissector_mask, |
| 707 | u32 flower_flag_bit, u32 dissector_flag_bit) |
| 708 | { |
| 709 | if (flower_mask & flower_flag_bit) { |
| 710 | *dissector_mask |= dissector_flag_bit; |
| 711 | if (flower_key & flower_flag_bit) |
| 712 | *dissector_key |= dissector_flag_bit; |
| 713 | } |
| 714 | } |
| 715 | |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 716 | static int fl_set_key_flags(struct nlattr **tb, |
| 717 | u32 *flags_key, u32 *flags_mask) |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 718 | { |
| 719 | u32 key, mask; |
| 720 | |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 721 | /* mask is mandatory for flags */ |
| 722 | if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) |
| 723 | return -EINVAL; |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 724 | |
| 725 | key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS])); |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 726 | mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK])); |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 727 | |
| 728 | *flags_key = 0; |
| 729 | *flags_mask = 0; |
| 730 | |
| 731 | fl_set_key_flag(key, mask, flags_key, flags_mask, |
| 732 | TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); |
Pieter Jansen van Vuuren | 459d153 | 2018-03-06 18:11:14 +0100 | [diff] [blame] | 733 | fl_set_key_flag(key, mask, flags_key, flags_mask, |
| 734 | TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, |
| 735 | FLOW_DIS_FIRST_FRAG); |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 736 | |
| 737 | return 0; |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 738 | } |
| 739 | |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 740 | static void fl_set_key_ip(struct nlattr **tb, bool encap, |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 741 | struct flow_dissector_key_ip *key, |
| 742 | struct flow_dissector_key_ip *mask) |
| 743 | { |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 744 | int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS; |
| 745 | int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL; |
| 746 | int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK; |
| 747 | int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK; |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 748 | |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 749 | fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)); |
| 750 | fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)); |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 751 | } |
| 752 | |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 753 | static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key, |
| 754 | int depth, int option_len, |
| 755 | struct netlink_ext_ack *extack) |
| 756 | { |
| 757 | struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1]; |
| 758 | struct nlattr *class = NULL, *type = NULL, *data = NULL; |
| 759 | struct geneve_opt *opt; |
| 760 | int err, data_len = 0; |
| 761 | |
| 762 | if (option_len > sizeof(struct geneve_opt)) |
| 763 | data_len = option_len - sizeof(struct geneve_opt); |
| 764 | |
| 765 | opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len]; |
| 766 | memset(opt, 0xff, option_len); |
| 767 | opt->length = data_len / 4; |
| 768 | opt->r1 = 0; |
| 769 | opt->r2 = 0; |
| 770 | opt->r3 = 0; |
| 771 | |
| 772 | /* If no mask has been prodived we assume an exact match. */ |
| 773 | if (!depth) |
| 774 | return sizeof(struct geneve_opt) + data_len; |
| 775 | |
| 776 | if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) { |
| 777 | NL_SET_ERR_MSG(extack, "Non-geneve option type for mask"); |
| 778 | return -EINVAL; |
| 779 | } |
| 780 | |
| 781 | err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX, |
| 782 | nla, geneve_opt_policy, extack); |
| 783 | if (err < 0) |
| 784 | return err; |
| 785 | |
| 786 | /* We are not allowed to omit any of CLASS, TYPE or DATA |
| 787 | * fields from the key. |
| 788 | */ |
| 789 | if (!option_len && |
| 790 | (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] || |
| 791 | !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] || |
| 792 | !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) { |
| 793 | NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data"); |
| 794 | return -EINVAL; |
| 795 | } |
| 796 | |
| 797 | /* Omitting any of CLASS, TYPE or DATA fields is allowed |
| 798 | * for the mask. |
| 799 | */ |
| 800 | if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) { |
| 801 | int new_len = key->enc_opts.len; |
| 802 | |
| 803 | data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]; |
| 804 | data_len = nla_len(data); |
| 805 | if (data_len < 4) { |
| 806 | NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long"); |
| 807 | return -ERANGE; |
| 808 | } |
| 809 | if (data_len % 4) { |
| 810 | NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long"); |
| 811 | return -ERANGE; |
| 812 | } |
| 813 | |
| 814 | new_len += sizeof(struct geneve_opt) + data_len; |
| 815 | BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX); |
| 816 | if (new_len > FLOW_DIS_TUN_OPTS_MAX) { |
| 817 | NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size"); |
| 818 | return -ERANGE; |
| 819 | } |
| 820 | opt->length = data_len / 4; |
| 821 | memcpy(opt->opt_data, nla_data(data), data_len); |
| 822 | } |
| 823 | |
| 824 | if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) { |
| 825 | class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]; |
| 826 | opt->opt_class = nla_get_be16(class); |
| 827 | } |
| 828 | |
| 829 | if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) { |
| 830 | type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]; |
| 831 | opt->type = nla_get_u8(type); |
| 832 | } |
| 833 | |
| 834 | return sizeof(struct geneve_opt) + data_len; |
| 835 | } |
| 836 | |
| 837 | static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key, |
| 838 | struct fl_flow_key *mask, |
| 839 | struct netlink_ext_ack *extack) |
| 840 | { |
| 841 | const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL; |
Jakub Kicinski | 63c8299 | 2018-11-09 21:06:26 -0800 | [diff] [blame] | 842 | int err, option_len, key_depth, msk_depth = 0; |
| 843 | |
| 844 | err = nla_validate_nested(tb[TCA_FLOWER_KEY_ENC_OPTS], |
| 845 | TCA_FLOWER_KEY_ENC_OPTS_MAX, |
| 846 | enc_opts_policy, extack); |
| 847 | if (err) |
| 848 | return err; |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 849 | |
| 850 | nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]); |
| 851 | |
| 852 | if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) { |
Jakub Kicinski | 63c8299 | 2018-11-09 21:06:26 -0800 | [diff] [blame] | 853 | err = nla_validate_nested(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK], |
| 854 | TCA_FLOWER_KEY_ENC_OPTS_MAX, |
| 855 | enc_opts_policy, extack); |
| 856 | if (err) |
| 857 | return err; |
| 858 | |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 859 | nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); |
| 860 | msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); |
| 861 | } |
| 862 | |
| 863 | nla_for_each_attr(nla_opt_key, nla_enc_key, |
| 864 | nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) { |
| 865 | switch (nla_type(nla_opt_key)) { |
| 866 | case TCA_FLOWER_KEY_ENC_OPTS_GENEVE: |
| 867 | option_len = 0; |
| 868 | key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT; |
| 869 | option_len = fl_set_geneve_opt(nla_opt_key, key, |
| 870 | key_depth, option_len, |
| 871 | extack); |
| 872 | if (option_len < 0) |
| 873 | return option_len; |
| 874 | |
| 875 | key->enc_opts.len += option_len; |
| 876 | /* At the same time we need to parse through the mask |
| 877 | * in order to verify exact and mask attribute lengths. |
| 878 | */ |
| 879 | mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT; |
| 880 | option_len = fl_set_geneve_opt(nla_opt_msk, mask, |
| 881 | msk_depth, option_len, |
| 882 | extack); |
| 883 | if (option_len < 0) |
| 884 | return option_len; |
| 885 | |
| 886 | mask->enc_opts.len += option_len; |
| 887 | if (key->enc_opts.len != mask->enc_opts.len) { |
| 888 | NL_SET_ERR_MSG(extack, "Key and mask miss aligned"); |
| 889 | return -EINVAL; |
| 890 | } |
| 891 | |
| 892 | if (msk_depth) |
| 893 | nla_opt_msk = nla_next(nla_opt_msk, &msk_depth); |
| 894 | break; |
| 895 | default: |
| 896 | NL_SET_ERR_MSG(extack, "Unknown tunnel option type"); |
| 897 | return -EINVAL; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | return 0; |
| 902 | } |
| 903 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 904 | static int fl_set_key(struct net *net, struct nlattr **tb, |
Alexander Aring | 1057c55 | 2018-01-18 11:20:54 -0500 | [diff] [blame] | 905 | struct fl_flow_key *key, struct fl_flow_key *mask, |
| 906 | struct netlink_ext_ack *extack) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 907 | { |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 908 | __be16 ethertype; |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 909 | int ret = 0; |
Brian Haley | dd3aa3b | 2015-05-14 13:20:15 -0400 | [diff] [blame] | 910 | #ifdef CONFIG_NET_CLS_IND |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 911 | if (tb[TCA_FLOWER_INDEV]) { |
Alexander Aring | 1057c55 | 2018-01-18 11:20:54 -0500 | [diff] [blame] | 912 | int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 913 | if (err < 0) |
| 914 | return err; |
| 915 | key->indev_ifindex = err; |
| 916 | mask->indev_ifindex = 0xffffffff; |
| 917 | } |
Brian Haley | dd3aa3b | 2015-05-14 13:20:15 -0400 | [diff] [blame] | 918 | #endif |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 919 | |
| 920 | fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, |
| 921 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, |
| 922 | sizeof(key->eth.dst)); |
| 923 | fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, |
| 924 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, |
| 925 | sizeof(key->eth.src)); |
Jamal Hadi Salim | 66530bd | 2016-01-10 11:47:01 -0500 | [diff] [blame] | 926 | |
Arnd Bergmann | 0b498a5 | 2016-08-26 17:25:45 +0200 | [diff] [blame] | 927 | if (tb[TCA_FLOWER_KEY_ETH_TYPE]) { |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 928 | ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]); |
| 929 | |
Jianbo Liu | aaab083 | 2018-07-06 05:38:13 +0000 | [diff] [blame] | 930 | if (eth_type_vlan(ethertype)) { |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 931 | fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID, |
| 932 | TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, |
| 933 | &mask->vlan); |
| 934 | |
Jianbo Liu | 5e9a0fe | 2018-07-09 02:26:20 +0000 | [diff] [blame] | 935 | if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) { |
| 936 | ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]); |
| 937 | if (eth_type_vlan(ethertype)) { |
| 938 | fl_set_key_vlan(tb, ethertype, |
| 939 | TCA_FLOWER_KEY_CVLAN_ID, |
| 940 | TCA_FLOWER_KEY_CVLAN_PRIO, |
| 941 | &key->cvlan, &mask->cvlan); |
| 942 | fl_set_key_val(tb, &key->basic.n_proto, |
| 943 | TCA_FLOWER_KEY_CVLAN_ETH_TYPE, |
| 944 | &mask->basic.n_proto, |
| 945 | TCA_FLOWER_UNSPEC, |
| 946 | sizeof(key->basic.n_proto)); |
| 947 | } else { |
| 948 | key->basic.n_proto = ethertype; |
| 949 | mask->basic.n_proto = cpu_to_be16(~0); |
| 950 | } |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 951 | } |
Arnd Bergmann | 0b498a5 | 2016-08-26 17:25:45 +0200 | [diff] [blame] | 952 | } else { |
| 953 | key->basic.n_proto = ethertype; |
| 954 | mask->basic.n_proto = cpu_to_be16(~0); |
| 955 | } |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 956 | } |
Jamal Hadi Salim | 66530bd | 2016-01-10 11:47:01 -0500 | [diff] [blame] | 957 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 958 | if (key->basic.n_proto == htons(ETH_P_IP) || |
| 959 | key->basic.n_proto == htons(ETH_P_IPV6)) { |
| 960 | fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, |
| 961 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, |
| 962 | sizeof(key->basic.ip_proto)); |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 963 | fl_set_key_ip(tb, false, &key->ip, &mask->ip); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 964 | } |
Jamal Hadi Salim | 66530bd | 2016-01-10 11:47:01 -0500 | [diff] [blame] | 965 | |
| 966 | if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) { |
| 967 | key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
Paul Blakey | 970bfcd | 2016-12-14 19:00:57 +0200 | [diff] [blame] | 968 | mask->control.addr_type = ~0; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 969 | fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
| 970 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, |
| 971 | sizeof(key->ipv4.src)); |
| 972 | fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, |
| 973 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, |
| 974 | sizeof(key->ipv4.dst)); |
Jamal Hadi Salim | 66530bd | 2016-01-10 11:47:01 -0500 | [diff] [blame] | 975 | } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) { |
| 976 | key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
Paul Blakey | 970bfcd | 2016-12-14 19:00:57 +0200 | [diff] [blame] | 977 | mask->control.addr_type = ~0; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 978 | fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
| 979 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, |
| 980 | sizeof(key->ipv6.src)); |
| 981 | fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, |
| 982 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, |
| 983 | sizeof(key->ipv6.dst)); |
| 984 | } |
Jamal Hadi Salim | 66530bd | 2016-01-10 11:47:01 -0500 | [diff] [blame] | 985 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 986 | if (key->basic.ip_proto == IPPROTO_TCP) { |
| 987 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, |
Or Gerlitz | aa72d70 | 2016-09-15 15:28:22 +0300 | [diff] [blame] | 988 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 989 | sizeof(key->tp.src)); |
| 990 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, |
Or Gerlitz | aa72d70 | 2016-09-15 15:28:22 +0300 | [diff] [blame] | 991 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 992 | sizeof(key->tp.dst)); |
Jiri Pirko | fdfc7dd | 2017-05-23 18:40:45 +0200 | [diff] [blame] | 993 | fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, |
| 994 | &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, |
| 995 | sizeof(key->tcp.flags)); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 996 | } else if (key->basic.ip_proto == IPPROTO_UDP) { |
| 997 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, |
Or Gerlitz | aa72d70 | 2016-09-15 15:28:22 +0300 | [diff] [blame] | 998 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 999 | sizeof(key->tp.src)); |
| 1000 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, |
Or Gerlitz | aa72d70 | 2016-09-15 15:28:22 +0300 | [diff] [blame] | 1001 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1002 | sizeof(key->tp.dst)); |
Simon Horman | 5976c5f | 2016-11-03 13:24:21 +0100 | [diff] [blame] | 1003 | } else if (key->basic.ip_proto == IPPROTO_SCTP) { |
| 1004 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, |
| 1005 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, |
| 1006 | sizeof(key->tp.src)); |
| 1007 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, |
| 1008 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, |
| 1009 | sizeof(key->tp.dst)); |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 1010 | } else if (key->basic.n_proto == htons(ETH_P_IP) && |
| 1011 | key->basic.ip_proto == IPPROTO_ICMP) { |
| 1012 | fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE, |
| 1013 | &mask->icmp.type, |
| 1014 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, |
| 1015 | sizeof(key->icmp.type)); |
| 1016 | fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE, |
| 1017 | &mask->icmp.code, |
| 1018 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK, |
| 1019 | sizeof(key->icmp.code)); |
| 1020 | } else if (key->basic.n_proto == htons(ETH_P_IPV6) && |
| 1021 | key->basic.ip_proto == IPPROTO_ICMPV6) { |
| 1022 | fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE, |
| 1023 | &mask->icmp.type, |
| 1024 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, |
| 1025 | sizeof(key->icmp.type)); |
Simon Horman | 040587a | 2017-01-30 16:19:02 +0100 | [diff] [blame] | 1026 | fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE, |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 1027 | &mask->icmp.code, |
Simon Horman | 040587a | 2017-01-30 16:19:02 +0100 | [diff] [blame] | 1028 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK, |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 1029 | sizeof(key->icmp.code)); |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 1030 | } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) || |
| 1031 | key->basic.n_proto == htons(ETH_P_MPLS_MC)) { |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 1032 | ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls); |
| 1033 | if (ret) |
| 1034 | return ret; |
Simon Horman | 99d3132 | 2017-01-11 14:05:43 +0100 | [diff] [blame] | 1035 | } else if (key->basic.n_proto == htons(ETH_P_ARP) || |
| 1036 | key->basic.n_proto == htons(ETH_P_RARP)) { |
| 1037 | fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP, |
| 1038 | &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK, |
| 1039 | sizeof(key->arp.sip)); |
| 1040 | fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP, |
| 1041 | &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK, |
| 1042 | sizeof(key->arp.tip)); |
| 1043 | fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP, |
| 1044 | &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK, |
| 1045 | sizeof(key->arp.op)); |
| 1046 | fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, |
| 1047 | mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, |
| 1048 | sizeof(key->arp.sha)); |
| 1049 | fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, |
| 1050 | mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, |
| 1051 | sizeof(key->arp.tha)); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 1054 | if (key->basic.ip_proto == IPPROTO_TCP || |
| 1055 | key->basic.ip_proto == IPPROTO_UDP || |
| 1056 | key->basic.ip_proto == IPPROTO_SCTP) { |
| 1057 | ret = fl_set_key_port_range(tb, key, mask); |
| 1058 | if (ret) |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 1062 | if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] || |
| 1063 | tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) { |
| 1064 | key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
Paul Blakey | 970bfcd | 2016-12-14 19:00:57 +0200 | [diff] [blame] | 1065 | mask->enc_control.addr_type = ~0; |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 1066 | fl_set_key_val(tb, &key->enc_ipv4.src, |
| 1067 | TCA_FLOWER_KEY_ENC_IPV4_SRC, |
| 1068 | &mask->enc_ipv4.src, |
| 1069 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, |
| 1070 | sizeof(key->enc_ipv4.src)); |
| 1071 | fl_set_key_val(tb, &key->enc_ipv4.dst, |
| 1072 | TCA_FLOWER_KEY_ENC_IPV4_DST, |
| 1073 | &mask->enc_ipv4.dst, |
| 1074 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, |
| 1075 | sizeof(key->enc_ipv4.dst)); |
| 1076 | } |
| 1077 | |
| 1078 | if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] || |
| 1079 | tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) { |
| 1080 | key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
Paul Blakey | 970bfcd | 2016-12-14 19:00:57 +0200 | [diff] [blame] | 1081 | mask->enc_control.addr_type = ~0; |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 1082 | fl_set_key_val(tb, &key->enc_ipv6.src, |
| 1083 | TCA_FLOWER_KEY_ENC_IPV6_SRC, |
| 1084 | &mask->enc_ipv6.src, |
| 1085 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, |
| 1086 | sizeof(key->enc_ipv6.src)); |
| 1087 | fl_set_key_val(tb, &key->enc_ipv6.dst, |
| 1088 | TCA_FLOWER_KEY_ENC_IPV6_DST, |
| 1089 | &mask->enc_ipv6.dst, |
| 1090 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, |
| 1091 | sizeof(key->enc_ipv6.dst)); |
| 1092 | } |
| 1093 | |
| 1094 | fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID, |
Hadar Hen Zion | eb523f4 | 2016-09-27 11:21:18 +0300 | [diff] [blame] | 1095 | &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC, |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 1096 | sizeof(key->enc_key_id.keyid)); |
| 1097 | |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 1098 | fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, |
| 1099 | &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, |
| 1100 | sizeof(key->enc_tp.src)); |
| 1101 | |
| 1102 | fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT, |
| 1103 | &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, |
| 1104 | sizeof(key->enc_tp.dst)); |
| 1105 | |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 1106 | fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip); |
| 1107 | |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1108 | if (tb[TCA_FLOWER_KEY_ENC_OPTS]) { |
| 1109 | ret = fl_set_enc_opt(tb, key, mask, extack); |
| 1110 | if (ret) |
| 1111 | return ret; |
| 1112 | } |
| 1113 | |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 1114 | if (tb[TCA_FLOWER_KEY_FLAGS]) |
| 1115 | ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags); |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 1116 | |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 1117 | return ret; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1120 | static void fl_mask_copy(struct fl_flow_mask *dst, |
| 1121 | struct fl_flow_mask *src) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1122 | { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1123 | const void *psrc = fl_key_get_start(&src->key, src); |
| 1124 | void *pdst = fl_key_get_start(&dst->key, src); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1125 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1126 | memcpy(pdst, psrc, fl_mask_range(src)); |
| 1127 | dst->range = src->range; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | static const struct rhashtable_params fl_ht_params = { |
| 1131 | .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */ |
| 1132 | .head_offset = offsetof(struct cls_fl_filter, ht_node), |
| 1133 | .automatic_shrinking = true, |
| 1134 | }; |
| 1135 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1136 | static int fl_init_mask_hashtable(struct fl_flow_mask *mask) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1137 | { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1138 | mask->filter_ht_params = fl_ht_params; |
| 1139 | mask->filter_ht_params.key_len = fl_mask_range(mask); |
| 1140 | mask->filter_ht_params.key_offset += mask->range.start; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1141 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1142 | return rhashtable_init(&mask->ht, &mask->filter_ht_params); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member) |
zhong jiang | cb205a8 | 2018-09-19 19:32:11 +0800 | [diff] [blame] | 1146 | #define FL_KEY_MEMBER_SIZE(member) FIELD_SIZEOF(struct fl_flow_key, member) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1147 | |
Hadar Hen Zion | 339ba87 | 2016-08-17 13:36:12 +0300 | [diff] [blame] | 1148 | #define FL_KEY_IS_MASKED(mask, member) \ |
| 1149 | memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \ |
| 1150 | 0, FL_KEY_MEMBER_SIZE(member)) \ |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1151 | |
| 1152 | #define FL_KEY_SET(keys, cnt, id, member) \ |
| 1153 | do { \ |
| 1154 | keys[cnt].key_id = id; \ |
| 1155 | keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \ |
| 1156 | cnt++; \ |
| 1157 | } while(0); |
| 1158 | |
Hadar Hen Zion | 339ba87 | 2016-08-17 13:36:12 +0300 | [diff] [blame] | 1159 | #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \ |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1160 | do { \ |
Hadar Hen Zion | 339ba87 | 2016-08-17 13:36:12 +0300 | [diff] [blame] | 1161 | if (FL_KEY_IS_MASKED(mask, member)) \ |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1162 | FL_KEY_SET(keys, cnt, id, member); \ |
| 1163 | } while(0); |
| 1164 | |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1165 | static void fl_init_dissector(struct flow_dissector *dissector, |
| 1166 | struct fl_flow_key *mask) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1167 | { |
| 1168 | struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX]; |
| 1169 | size_t cnt = 0; |
| 1170 | |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 1171 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1172 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1173 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 339ba87 | 2016-08-17 13:36:12 +0300 | [diff] [blame] | 1174 | FLOW_DISSECTOR_KEY_ETH_ADDRS, eth); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1175 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 339ba87 | 2016-08-17 13:36:12 +0300 | [diff] [blame] | 1176 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1177 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 339ba87 | 2016-08-17 13:36:12 +0300 | [diff] [blame] | 1178 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6); |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 1179 | if (FL_KEY_IS_MASKED(mask, tp) || |
| 1180 | FL_KEY_IS_MASKED(mask, tp_min) || FL_KEY_IS_MASKED(mask, tp_max)) |
| 1181 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_PORTS, tp); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1182 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 1183 | FLOW_DISSECTOR_KEY_IP, ip); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1184 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Jiri Pirko | fdfc7dd | 2017-05-23 18:40:45 +0200 | [diff] [blame] | 1185 | FLOW_DISSECTOR_KEY_TCP, tcp); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1186 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 1187 | FLOW_DISSECTOR_KEY_ICMP, icmp); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1188 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Simon Horman | 99d3132 | 2017-01-11 14:05:43 +0100 | [diff] [blame] | 1189 | FLOW_DISSECTOR_KEY_ARP, arp); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1190 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 1191 | FLOW_DISSECTOR_KEY_MPLS, mpls); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1192 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1193 | FLOW_DISSECTOR_KEY_VLAN, vlan); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1194 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1195 | FLOW_DISSECTOR_KEY_CVLAN, cvlan); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1196 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 519d105 | 2016-11-07 15:14:38 +0200 | [diff] [blame] | 1197 | FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1198 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 519d105 | 2016-11-07 15:14:38 +0200 | [diff] [blame] | 1199 | FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1200 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 519d105 | 2016-11-07 15:14:38 +0200 | [diff] [blame] | 1201 | FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1202 | if (FL_KEY_IS_MASKED(mask, enc_ipv4) || |
| 1203 | FL_KEY_IS_MASKED(mask, enc_ipv6)) |
Hadar Hen Zion | 519d105 | 2016-11-07 15:14:38 +0200 | [diff] [blame] | 1204 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL, |
| 1205 | enc_control); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1206 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 1207 | FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1208 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 1209 | FLOW_DISSECTOR_KEY_ENC_IP, enc_ip); |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1210 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
| 1211 | FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1212 | |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1213 | skb_flow_dissector_init(dissector, keys, cnt); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head, |
| 1217 | struct fl_flow_mask *mask) |
| 1218 | { |
| 1219 | struct fl_flow_mask *newmask; |
| 1220 | int err; |
| 1221 | |
| 1222 | newmask = kzalloc(sizeof(*newmask), GFP_KERNEL); |
| 1223 | if (!newmask) |
| 1224 | return ERR_PTR(-ENOMEM); |
| 1225 | |
| 1226 | fl_mask_copy(newmask, mask); |
| 1227 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 1228 | if ((newmask->key.tp_min.dst && newmask->key.tp_max.dst) || |
| 1229 | (newmask->key.tp_min.src && newmask->key.tp_max.src)) |
| 1230 | newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE; |
| 1231 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1232 | err = fl_init_mask_hashtable(newmask); |
| 1233 | if (err) |
| 1234 | goto errout_free; |
| 1235 | |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1236 | fl_init_dissector(&newmask->dissector, &newmask->key); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1237 | |
| 1238 | INIT_LIST_HEAD_RCU(&newmask->filters); |
| 1239 | |
| 1240 | err = rhashtable_insert_fast(&head->ht, &newmask->ht_node, |
| 1241 | mask_ht_params); |
| 1242 | if (err) |
| 1243 | goto errout_destroy; |
| 1244 | |
| 1245 | list_add_tail_rcu(&newmask->list, &head->masks); |
| 1246 | |
| 1247 | return newmask; |
| 1248 | |
| 1249 | errout_destroy: |
| 1250 | rhashtable_destroy(&newmask->ht); |
| 1251 | errout_free: |
| 1252 | kfree(newmask); |
| 1253 | |
| 1254 | return ERR_PTR(err); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | static int fl_check_assign_mask(struct cls_fl_head *head, |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1258 | struct cls_fl_filter *fnew, |
| 1259 | struct cls_fl_filter *fold, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1260 | struct fl_flow_mask *mask) |
| 1261 | { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1262 | struct fl_flow_mask *newmask; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1263 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1264 | fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params); |
| 1265 | if (!fnew->mask) { |
| 1266 | if (fold) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1267 | return -EINVAL; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1268 | |
| 1269 | newmask = fl_create_new_mask(head, mask); |
| 1270 | if (IS_ERR(newmask)) |
| 1271 | return PTR_ERR(newmask); |
| 1272 | |
| 1273 | fnew->mask = newmask; |
Paul Blakey | f6521c5 | 2018-06-03 10:06:14 +0300 | [diff] [blame] | 1274 | } else if (fold && fold->mask != fnew->mask) { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1275 | return -EINVAL; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1276 | } |
| 1277 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | static int fl_set_parms(struct net *net, struct tcf_proto *tp, |
| 1282 | struct cls_fl_filter *f, struct fl_flow_mask *mask, |
| 1283 | unsigned long base, struct nlattr **tb, |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 1284 | struct nlattr *est, bool ovr, |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 1285 | struct fl_flow_tmplt *tmplt, |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 1286 | struct netlink_ext_ack *extack) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1287 | { |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1288 | int err; |
| 1289 | |
Vlad Buslov | ec6743a | 2019-02-11 10:55:43 +0200 | [diff] [blame] | 1290 | err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, true, |
| 1291 | extack); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1292 | if (err < 0) |
| 1293 | return err; |
| 1294 | |
| 1295 | if (tb[TCA_FLOWER_CLASSID]) { |
| 1296 | f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]); |
| 1297 | tcf_bind_filter(tp, &f->res, base); |
| 1298 | } |
| 1299 | |
Alexander Aring | 1057c55 | 2018-01-18 11:20:54 -0500 | [diff] [blame] | 1300 | err = fl_set_key(net, tb, &f->key, &mask->key, extack); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1301 | if (err) |
Jiri Pirko | 4550752 | 2017-08-04 14:29:06 +0200 | [diff] [blame] | 1302 | return err; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1303 | |
| 1304 | fl_mask_update_range(mask); |
| 1305 | fl_set_masked_key(&f->mkey, &f->key, mask); |
| 1306 | |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 1307 | if (!fl_mask_fits_tmplt(tmplt, mask)) { |
| 1308 | NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template"); |
| 1309 | return -EINVAL; |
| 1310 | } |
| 1311 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1312 | return 0; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1313 | } |
| 1314 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1315 | static int fl_change(struct net *net, struct sk_buff *in_skb, |
| 1316 | struct tcf_proto *tp, unsigned long base, |
| 1317 | u32 handle, struct nlattr **tca, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1318 | void **arg, bool ovr, bool rtnl_held, |
| 1319 | struct netlink_ext_ack *extack) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1320 | { |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame^] | 1321 | struct cls_fl_head *head = fl_head_dereference(tp); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1322 | struct cls_fl_filter *fold = *arg; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1323 | struct cls_fl_filter *fnew; |
Ivan Vecera | 2cddd20 | 2019-01-16 16:53:52 +0100 | [diff] [blame] | 1324 | struct fl_flow_mask *mask; |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 1325 | struct nlattr **tb; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1326 | int err; |
| 1327 | |
| 1328 | if (!tca[TCA_OPTIONS]) |
| 1329 | return -EINVAL; |
| 1330 | |
Ivan Vecera | 2cddd20 | 2019-01-16 16:53:52 +0100 | [diff] [blame] | 1331 | mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL); |
| 1332 | if (!mask) |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 1333 | return -ENOBUFS; |
| 1334 | |
Ivan Vecera | 2cddd20 | 2019-01-16 16:53:52 +0100 | [diff] [blame] | 1335 | tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); |
| 1336 | if (!tb) { |
| 1337 | err = -ENOBUFS; |
| 1338 | goto errout_mask_alloc; |
| 1339 | } |
| 1340 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 1341 | err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS], |
| 1342 | fl_policy, NULL); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1343 | if (err < 0) |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 1344 | goto errout_tb; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1345 | |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 1346 | if (fold && handle && fold->handle != handle) { |
| 1347 | err = -EINVAL; |
| 1348 | goto errout_tb; |
| 1349 | } |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1350 | |
| 1351 | fnew = kzalloc(sizeof(*fnew), GFP_KERNEL); |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 1352 | if (!fnew) { |
| 1353 | err = -ENOBUFS; |
| 1354 | goto errout_tb; |
| 1355 | } |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1356 | |
Cong Wang | 1421510 | 2019-02-20 21:37:42 -0800 | [diff] [blame] | 1357 | err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 1358 | if (err < 0) |
| 1359 | goto errout; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1360 | |
Vlad Buslov | ecb3dea | 2019-03-06 16:22:12 +0200 | [diff] [blame] | 1361 | if (tb[TCA_FLOWER_FLAGS]) { |
| 1362 | fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]); |
| 1363 | |
| 1364 | if (!tc_flags_valid(fnew->flags)) { |
| 1365 | err = -EINVAL; |
| 1366 | goto errout; |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr, |
| 1371 | tp->chain->tmplt_priv, extack); |
| 1372 | if (err) |
| 1373 | goto errout; |
| 1374 | |
| 1375 | err = fl_check_assign_mask(head, fnew, fold, mask); |
| 1376 | if (err) |
| 1377 | goto errout; |
| 1378 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1379 | if (!handle) { |
Matthew Wilcox | 85bd043 | 2017-11-28 10:53:03 -0500 | [diff] [blame] | 1380 | handle = 1; |
| 1381 | err = idr_alloc_u32(&head->handle_idr, fnew, &handle, |
| 1382 | INT_MAX, GFP_KERNEL); |
| 1383 | } else if (!fold) { |
| 1384 | /* user specifies a handle and it doesn't exist */ |
| 1385 | err = idr_alloc_u32(&head->handle_idr, fnew, &handle, |
| 1386 | handle, GFP_KERNEL); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1387 | } |
Matthew Wilcox | 85bd043 | 2017-11-28 10:53:03 -0500 | [diff] [blame] | 1388 | if (err) |
Vlad Buslov | ecb3dea | 2019-03-06 16:22:12 +0200 | [diff] [blame] | 1389 | goto errout_mask; |
Matthew Wilcox | 85bd043 | 2017-11-28 10:53:03 -0500 | [diff] [blame] | 1390 | fnew->handle = handle; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1391 | |
David S. Miller | 4cc1fee | 2018-12-09 21:27:48 -0800 | [diff] [blame] | 1392 | if (!fold && __fl_lookup(fnew->mask, &fnew->mkey)) { |
Or Gerlitz | 35cc3cef | 2018-12-09 18:10:24 +0200 | [diff] [blame] | 1393 | err = -EEXIST; |
Vlad Buslov | ecb3dea | 2019-03-06 16:22:12 +0200 | [diff] [blame] | 1394 | goto errout_idr; |
Amir Vadai | e69985c | 2016-06-05 17:11:18 +0300 | [diff] [blame] | 1395 | } |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 1396 | |
Or Gerlitz | 35cc3cef | 2018-12-09 18:10:24 +0200 | [diff] [blame] | 1397 | err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node, |
| 1398 | fnew->mask->filter_ht_params); |
| 1399 | if (err) |
Vlad Buslov | ecb3dea | 2019-03-06 16:22:12 +0200 | [diff] [blame] | 1400 | goto errout_idr; |
Or Gerlitz | 35cc3cef | 2018-12-09 18:10:24 +0200 | [diff] [blame] | 1401 | |
Hadar Hen Zion | 7968521 | 2016-12-01 14:06:34 +0200 | [diff] [blame] | 1402 | if (!tc_skip_hw(fnew->flags)) { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1403 | err = fl_hw_replace_filter(tp, fnew, extack); |
Hadar Hen Zion | 7968521 | 2016-12-01 14:06:34 +0200 | [diff] [blame] | 1404 | if (err) |
Petr Machata | c1f7e02 | 2019-02-04 14:50:38 +0000 | [diff] [blame] | 1405 | goto errout_mask_ht; |
Hadar Hen Zion | 7968521 | 2016-12-01 14:06:34 +0200 | [diff] [blame] | 1406 | } |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 1407 | |
Or Gerlitz | 5559396 | 2017-02-16 10:31:13 +0200 | [diff] [blame] | 1408 | if (!tc_in_hw(fnew->flags)) |
| 1409 | fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW; |
| 1410 | |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 1411 | if (fold) { |
Roi Dayan | 599d257 | 2018-12-19 18:07:56 +0200 | [diff] [blame] | 1412 | rhashtable_remove_fast(&fold->mask->ht, |
| 1413 | &fold->ht_node, |
| 1414 | fold->mask->filter_ht_params); |
Hadar Hen Zion | 7968521 | 2016-12-01 14:06:34 +0200 | [diff] [blame] | 1415 | if (!tc_skip_hw(fold->flags)) |
Jakub Kicinski | 1b0f803 | 2018-01-24 12:54:21 -0800 | [diff] [blame] | 1416 | fl_hw_destroy_filter(tp, fold, NULL); |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 1417 | } |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1418 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1419 | *arg = fnew; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1420 | |
| 1421 | if (fold) { |
Matthew Wilcox | 234a462 | 2017-11-28 09:56:36 -0500 | [diff] [blame] | 1422 | idr_replace(&head->handle_idr, fnew, fnew->handle); |
Daniel Borkmann | ff3532f | 2015-07-17 22:38:44 +0200 | [diff] [blame] | 1423 | list_replace_rcu(&fold->list, &fnew->list); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1424 | tcf_unbind_filter(tp, &fold->res); |
Cong Wang | 0dadc11 | 2017-11-06 13:47:24 -0800 | [diff] [blame] | 1425 | tcf_exts_get_net(&fold->exts); |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 1426 | tcf_queue_work(&fold->rwork, fl_destroy_filter_work); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1427 | } else { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1428 | list_add_tail_rcu(&fnew->list, &fnew->mask->filters); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1429 | } |
| 1430 | |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 1431 | kfree(tb); |
Ivan Vecera | 2cddd20 | 2019-01-16 16:53:52 +0100 | [diff] [blame] | 1432 | kfree(mask); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1433 | return 0; |
| 1434 | |
Petr Machata | c1f7e02 | 2019-02-04 14:50:38 +0000 | [diff] [blame] | 1435 | errout_mask_ht: |
| 1436 | rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node, |
| 1437 | fnew->mask->filter_ht_params); |
| 1438 | |
Cong Wang | fe2502e | 2017-09-20 09:18:45 -0700 | [diff] [blame] | 1439 | errout_idr: |
Paul Blakey | 8258d2d | 2018-05-30 11:29:15 +0300 | [diff] [blame] | 1440 | if (!fold) |
Matthew Wilcox | 9c16094 | 2017-11-28 09:48:43 -0500 | [diff] [blame] | 1441 | idr_remove(&head->handle_idr, fnew->handle); |
Vlad Buslov | ecb3dea | 2019-03-06 16:22:12 +0200 | [diff] [blame] | 1442 | |
| 1443 | errout_mask: |
| 1444 | fl_mask_put(head, fnew->mask, false); |
| 1445 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1446 | errout: |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 1447 | tcf_exts_destroy(&fnew->exts); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1448 | kfree(fnew); |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 1449 | errout_tb: |
| 1450 | kfree(tb); |
Ivan Vecera | 2cddd20 | 2019-01-16 16:53:52 +0100 | [diff] [blame] | 1451 | errout_mask_alloc: |
| 1452 | kfree(mask); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1453 | return err; |
| 1454 | } |
| 1455 | |
Alexander Aring | 571acf2 | 2018-01-18 11:20:53 -0500 | [diff] [blame] | 1456 | static int fl_delete(struct tcf_proto *tp, void *arg, bool *last, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1457 | bool rtnl_held, struct netlink_ext_ack *extack) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1458 | { |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame^] | 1459 | struct cls_fl_head *head = fl_head_dereference(tp); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1460 | struct cls_fl_filter *f = arg; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1461 | |
Or Gerlitz | 35cc3cef | 2018-12-09 18:10:24 +0200 | [diff] [blame] | 1462 | rhashtable_remove_fast(&f->mask->ht, &f->ht_node, |
| 1463 | f->mask->filter_ht_params); |
Jakub Kicinski | 1b0f803 | 2018-01-24 12:54:21 -0800 | [diff] [blame] | 1464 | __fl_delete(tp, f, extack); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1465 | *last = list_empty(&head->masks); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1466 | return 0; |
| 1467 | } |
| 1468 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1469 | static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
| 1470 | bool rtnl_held) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1471 | { |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame^] | 1472 | struct cls_fl_head *head = fl_head_dereference(tp); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1473 | struct cls_fl_filter *f; |
| 1474 | |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 1475 | arg->count = arg->skip; |
| 1476 | |
| 1477 | while ((f = idr_get_next_ul(&head->handle_idr, |
| 1478 | &arg->cookie)) != NULL) { |
| 1479 | if (arg->fn(tp, f, arg) < 0) { |
| 1480 | arg->stop = 1; |
| 1481 | break; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1482 | } |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 1483 | arg->cookie = f->handle + 1; |
| 1484 | arg->count++; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1485 | } |
| 1486 | } |
| 1487 | |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 1488 | static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb, |
| 1489 | void *cb_priv, struct netlink_ext_ack *extack) |
| 1490 | { |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame^] | 1491 | struct cls_fl_head *head = fl_head_dereference(tp); |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 1492 | struct tc_cls_flower_offload cls_flower = {}; |
| 1493 | struct tcf_block *block = tp->chain->block; |
| 1494 | struct fl_flow_mask *mask; |
| 1495 | struct cls_fl_filter *f; |
| 1496 | int err; |
| 1497 | |
| 1498 | list_for_each_entry(mask, &head->masks, list) { |
| 1499 | list_for_each_entry(f, &mask->filters, list) { |
| 1500 | if (tc_skip_hw(f->flags)) |
| 1501 | continue; |
| 1502 | |
Pablo Neira Ayuso | e3ab786 | 2019-02-02 12:50:45 +0100 | [diff] [blame] | 1503 | cls_flower.rule = |
| 1504 | flow_rule_alloc(tcf_exts_num_actions(&f->exts)); |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 1505 | if (!cls_flower.rule) |
| 1506 | return -ENOMEM; |
| 1507 | |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 1508 | tc_cls_common_offload_init(&cls_flower.common, tp, |
| 1509 | f->flags, extack); |
| 1510 | cls_flower.command = add ? |
| 1511 | TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY; |
| 1512 | cls_flower.cookie = (unsigned long)f; |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 1513 | cls_flower.rule->match.dissector = &mask->dissector; |
| 1514 | cls_flower.rule->match.mask = &mask->key; |
| 1515 | cls_flower.rule->match.key = &f->mkey; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 1516 | |
| 1517 | err = tc_setup_flow_action(&cls_flower.rule->action, |
| 1518 | &f->exts); |
| 1519 | if (err) { |
| 1520 | kfree(cls_flower.rule); |
Vlad Buslov | 1f15bb4 | 2019-02-12 23:39:06 +0200 | [diff] [blame] | 1521 | if (tc_skip_sw(f->flags)) { |
| 1522 | NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); |
| 1523 | return err; |
| 1524 | } |
| 1525 | continue; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 1526 | } |
| 1527 | |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 1528 | cls_flower.classid = f->res.classid; |
| 1529 | |
| 1530 | err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv); |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 1531 | kfree(cls_flower.rule); |
| 1532 | |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 1533 | if (err) { |
| 1534 | if (add && tc_skip_sw(f->flags)) |
| 1535 | return err; |
| 1536 | continue; |
| 1537 | } |
| 1538 | |
| 1539 | tc_cls_offload_cnt_update(block, &f->in_hw_count, |
| 1540 | &f->flags, add); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | return 0; |
| 1545 | } |
| 1546 | |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 1547 | static int fl_hw_create_tmplt(struct tcf_chain *chain, |
| 1548 | struct fl_flow_tmplt *tmplt) |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 1549 | { |
| 1550 | struct tc_cls_flower_offload cls_flower = {}; |
| 1551 | struct tcf_block *block = chain->block; |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 1552 | |
Pablo Neira Ayuso | e3ab786 | 2019-02-02 12:50:45 +0100 | [diff] [blame] | 1553 | cls_flower.rule = flow_rule_alloc(0); |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 1554 | if (!cls_flower.rule) |
| 1555 | return -ENOMEM; |
| 1556 | |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 1557 | cls_flower.common.chain_index = chain->index; |
| 1558 | cls_flower.command = TC_CLSFLOWER_TMPLT_CREATE; |
| 1559 | cls_flower.cookie = (unsigned long) tmplt; |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 1560 | cls_flower.rule->match.dissector = &tmplt->dissector; |
| 1561 | cls_flower.rule->match.mask = &tmplt->mask; |
| 1562 | cls_flower.rule->match.key = &tmplt->dummy_key; |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 1563 | |
| 1564 | /* We don't care if driver (any of them) fails to handle this |
| 1565 | * call. It serves just as a hint for it. |
| 1566 | */ |
Cong Wang | aeb3fec | 2018-12-11 11:15:46 -0800 | [diff] [blame] | 1567 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false); |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 1568 | kfree(cls_flower.rule); |
| 1569 | |
| 1570 | return 0; |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | static void fl_hw_destroy_tmplt(struct tcf_chain *chain, |
| 1574 | struct fl_flow_tmplt *tmplt) |
| 1575 | { |
| 1576 | struct tc_cls_flower_offload cls_flower = {}; |
| 1577 | struct tcf_block *block = chain->block; |
| 1578 | |
| 1579 | cls_flower.common.chain_index = chain->index; |
| 1580 | cls_flower.command = TC_CLSFLOWER_TMPLT_DESTROY; |
| 1581 | cls_flower.cookie = (unsigned long) tmplt; |
| 1582 | |
Cong Wang | aeb3fec | 2018-12-11 11:15:46 -0800 | [diff] [blame] | 1583 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false); |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 1584 | } |
| 1585 | |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 1586 | static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain, |
| 1587 | struct nlattr **tca, |
| 1588 | struct netlink_ext_ack *extack) |
| 1589 | { |
| 1590 | struct fl_flow_tmplt *tmplt; |
| 1591 | struct nlattr **tb; |
| 1592 | int err; |
| 1593 | |
| 1594 | if (!tca[TCA_OPTIONS]) |
| 1595 | return ERR_PTR(-EINVAL); |
| 1596 | |
| 1597 | tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); |
| 1598 | if (!tb) |
| 1599 | return ERR_PTR(-ENOBUFS); |
| 1600 | err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS], |
| 1601 | fl_policy, NULL); |
| 1602 | if (err) |
| 1603 | goto errout_tb; |
| 1604 | |
| 1605 | tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL); |
Dan Carpenter | 1cbc36a5 | 2018-08-03 22:27:55 +0300 | [diff] [blame] | 1606 | if (!tmplt) { |
| 1607 | err = -ENOMEM; |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 1608 | goto errout_tb; |
Dan Carpenter | 1cbc36a5 | 2018-08-03 22:27:55 +0300 | [diff] [blame] | 1609 | } |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 1610 | tmplt->chain = chain; |
| 1611 | err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack); |
| 1612 | if (err) |
| 1613 | goto errout_tmplt; |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 1614 | |
| 1615 | fl_init_dissector(&tmplt->dissector, &tmplt->mask); |
| 1616 | |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 1617 | err = fl_hw_create_tmplt(chain, tmplt); |
| 1618 | if (err) |
| 1619 | goto errout_tmplt; |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 1620 | |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 1621 | kfree(tb); |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 1622 | return tmplt; |
| 1623 | |
| 1624 | errout_tmplt: |
| 1625 | kfree(tmplt); |
| 1626 | errout_tb: |
| 1627 | kfree(tb); |
| 1628 | return ERR_PTR(err); |
| 1629 | } |
| 1630 | |
| 1631 | static void fl_tmplt_destroy(void *tmplt_priv) |
| 1632 | { |
| 1633 | struct fl_flow_tmplt *tmplt = tmplt_priv; |
| 1634 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 1635 | fl_hw_destroy_tmplt(tmplt->chain, tmplt); |
| 1636 | kfree(tmplt); |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 1637 | } |
| 1638 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1639 | static int fl_dump_key_val(struct sk_buff *skb, |
| 1640 | void *val, int val_type, |
| 1641 | void *mask, int mask_type, int len) |
| 1642 | { |
| 1643 | int err; |
| 1644 | |
| 1645 | if (!memchr_inv(mask, 0, len)) |
| 1646 | return 0; |
| 1647 | err = nla_put(skb, val_type, len, val); |
| 1648 | if (err) |
| 1649 | return err; |
| 1650 | if (mask_type != TCA_FLOWER_UNSPEC) { |
| 1651 | err = nla_put(skb, mask_type, len, mask); |
| 1652 | if (err) |
| 1653 | return err; |
| 1654 | } |
| 1655 | return 0; |
| 1656 | } |
| 1657 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 1658 | static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key, |
| 1659 | struct fl_flow_key *mask) |
| 1660 | { |
| 1661 | if (fl_dump_key_val(skb, &key->tp_min.dst, TCA_FLOWER_KEY_PORT_DST_MIN, |
| 1662 | &mask->tp_min.dst, TCA_FLOWER_UNSPEC, |
| 1663 | sizeof(key->tp_min.dst)) || |
| 1664 | fl_dump_key_val(skb, &key->tp_max.dst, TCA_FLOWER_KEY_PORT_DST_MAX, |
| 1665 | &mask->tp_max.dst, TCA_FLOWER_UNSPEC, |
| 1666 | sizeof(key->tp_max.dst)) || |
| 1667 | fl_dump_key_val(skb, &key->tp_min.src, TCA_FLOWER_KEY_PORT_SRC_MIN, |
| 1668 | &mask->tp_min.src, TCA_FLOWER_UNSPEC, |
| 1669 | sizeof(key->tp_min.src)) || |
| 1670 | fl_dump_key_val(skb, &key->tp_max.src, TCA_FLOWER_KEY_PORT_SRC_MAX, |
| 1671 | &mask->tp_max.src, TCA_FLOWER_UNSPEC, |
| 1672 | sizeof(key->tp_max.src))) |
| 1673 | return -1; |
| 1674 | |
| 1675 | return 0; |
| 1676 | } |
| 1677 | |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 1678 | static int fl_dump_key_mpls(struct sk_buff *skb, |
| 1679 | struct flow_dissector_key_mpls *mpls_key, |
| 1680 | struct flow_dissector_key_mpls *mpls_mask) |
| 1681 | { |
| 1682 | int err; |
| 1683 | |
| 1684 | if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask))) |
| 1685 | return 0; |
| 1686 | if (mpls_mask->mpls_ttl) { |
| 1687 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL, |
| 1688 | mpls_key->mpls_ttl); |
| 1689 | if (err) |
| 1690 | return err; |
| 1691 | } |
| 1692 | if (mpls_mask->mpls_tc) { |
| 1693 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC, |
| 1694 | mpls_key->mpls_tc); |
| 1695 | if (err) |
| 1696 | return err; |
| 1697 | } |
| 1698 | if (mpls_mask->mpls_label) { |
| 1699 | err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL, |
| 1700 | mpls_key->mpls_label); |
| 1701 | if (err) |
| 1702 | return err; |
| 1703 | } |
| 1704 | if (mpls_mask->mpls_bos) { |
| 1705 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS, |
| 1706 | mpls_key->mpls_bos); |
| 1707 | if (err) |
| 1708 | return err; |
| 1709 | } |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 1713 | static int fl_dump_key_ip(struct sk_buff *skb, bool encap, |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 1714 | struct flow_dissector_key_ip *key, |
| 1715 | struct flow_dissector_key_ip *mask) |
| 1716 | { |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 1717 | int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS; |
| 1718 | int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL; |
| 1719 | int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK; |
| 1720 | int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK; |
| 1721 | |
| 1722 | if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) || |
| 1723 | fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl))) |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 1724 | return -1; |
| 1725 | |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1729 | static int fl_dump_key_vlan(struct sk_buff *skb, |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1730 | int vlan_id_key, int vlan_prio_key, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1731 | struct flow_dissector_key_vlan *vlan_key, |
| 1732 | struct flow_dissector_key_vlan *vlan_mask) |
| 1733 | { |
| 1734 | int err; |
| 1735 | |
| 1736 | if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask))) |
| 1737 | return 0; |
| 1738 | if (vlan_mask->vlan_id) { |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1739 | err = nla_put_u16(skb, vlan_id_key, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1740 | vlan_key->vlan_id); |
| 1741 | if (err) |
| 1742 | return err; |
| 1743 | } |
| 1744 | if (vlan_mask->vlan_priority) { |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1745 | err = nla_put_u8(skb, vlan_prio_key, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1746 | vlan_key->vlan_priority); |
| 1747 | if (err) |
| 1748 | return err; |
| 1749 | } |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 1753 | static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask, |
| 1754 | u32 *flower_key, u32 *flower_mask, |
| 1755 | u32 flower_flag_bit, u32 dissector_flag_bit) |
| 1756 | { |
| 1757 | if (dissector_mask & dissector_flag_bit) { |
| 1758 | *flower_mask |= flower_flag_bit; |
| 1759 | if (dissector_key & dissector_flag_bit) |
| 1760 | *flower_key |= flower_flag_bit; |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask) |
| 1765 | { |
| 1766 | u32 key, mask; |
| 1767 | __be32 _key, _mask; |
| 1768 | int err; |
| 1769 | |
| 1770 | if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask))) |
| 1771 | return 0; |
| 1772 | |
| 1773 | key = 0; |
| 1774 | mask = 0; |
| 1775 | |
| 1776 | fl_get_key_flag(flags_key, flags_mask, &key, &mask, |
| 1777 | TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); |
Pieter Jansen van Vuuren | 459d153 | 2018-03-06 18:11:14 +0100 | [diff] [blame] | 1778 | fl_get_key_flag(flags_key, flags_mask, &key, &mask, |
| 1779 | TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, |
| 1780 | FLOW_DIS_FIRST_FRAG); |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 1781 | |
| 1782 | _key = cpu_to_be32(key); |
| 1783 | _mask = cpu_to_be32(mask); |
| 1784 | |
| 1785 | err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key); |
| 1786 | if (err) |
| 1787 | return err; |
| 1788 | |
| 1789 | return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask); |
| 1790 | } |
| 1791 | |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1792 | static int fl_dump_key_geneve_opt(struct sk_buff *skb, |
| 1793 | struct flow_dissector_key_enc_opts *enc_opts) |
| 1794 | { |
| 1795 | struct geneve_opt *opt; |
| 1796 | struct nlattr *nest; |
| 1797 | int opt_off = 0; |
| 1798 | |
| 1799 | nest = nla_nest_start(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE); |
| 1800 | if (!nest) |
| 1801 | goto nla_put_failure; |
| 1802 | |
| 1803 | while (enc_opts->len > opt_off) { |
| 1804 | opt = (struct geneve_opt *)&enc_opts->data[opt_off]; |
| 1805 | |
| 1806 | if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS, |
| 1807 | opt->opt_class)) |
| 1808 | goto nla_put_failure; |
| 1809 | if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE, |
| 1810 | opt->type)) |
| 1811 | goto nla_put_failure; |
| 1812 | if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA, |
| 1813 | opt->length * 4, opt->opt_data)) |
| 1814 | goto nla_put_failure; |
| 1815 | |
| 1816 | opt_off += sizeof(struct geneve_opt) + opt->length * 4; |
| 1817 | } |
| 1818 | nla_nest_end(skb, nest); |
| 1819 | return 0; |
| 1820 | |
| 1821 | nla_put_failure: |
| 1822 | nla_nest_cancel(skb, nest); |
| 1823 | return -EMSGSIZE; |
| 1824 | } |
| 1825 | |
| 1826 | static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type, |
| 1827 | struct flow_dissector_key_enc_opts *enc_opts) |
| 1828 | { |
| 1829 | struct nlattr *nest; |
| 1830 | int err; |
| 1831 | |
| 1832 | if (!enc_opts->len) |
| 1833 | return 0; |
| 1834 | |
| 1835 | nest = nla_nest_start(skb, enc_opt_type); |
| 1836 | if (!nest) |
| 1837 | goto nla_put_failure; |
| 1838 | |
| 1839 | switch (enc_opts->dst_opt_type) { |
| 1840 | case TUNNEL_GENEVE_OPT: |
| 1841 | err = fl_dump_key_geneve_opt(skb, enc_opts); |
| 1842 | if (err) |
| 1843 | goto nla_put_failure; |
| 1844 | break; |
| 1845 | default: |
| 1846 | goto nla_put_failure; |
| 1847 | } |
| 1848 | nla_nest_end(skb, nest); |
| 1849 | return 0; |
| 1850 | |
| 1851 | nla_put_failure: |
| 1852 | nla_nest_cancel(skb, nest); |
| 1853 | return -EMSGSIZE; |
| 1854 | } |
| 1855 | |
| 1856 | static int fl_dump_key_enc_opt(struct sk_buff *skb, |
| 1857 | struct flow_dissector_key_enc_opts *key_opts, |
| 1858 | struct flow_dissector_key_enc_opts *msk_opts) |
| 1859 | { |
| 1860 | int err; |
| 1861 | |
| 1862 | err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts); |
| 1863 | if (err) |
| 1864 | return err; |
| 1865 | |
| 1866 | return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts); |
| 1867 | } |
| 1868 | |
Jiri Pirko | f574908 | 2018-07-23 09:23:08 +0200 | [diff] [blame] | 1869 | static int fl_dump_key(struct sk_buff *skb, struct net *net, |
| 1870 | struct fl_flow_key *key, struct fl_flow_key *mask) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1871 | { |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1872 | if (mask->indev_ifindex) { |
| 1873 | struct net_device *dev; |
| 1874 | |
| 1875 | dev = __dev_get_by_index(net, key->indev_ifindex); |
| 1876 | if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name)) |
| 1877 | goto nla_put_failure; |
| 1878 | } |
| 1879 | |
| 1880 | if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, |
| 1881 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, |
| 1882 | sizeof(key->eth.dst)) || |
| 1883 | fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, |
| 1884 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, |
| 1885 | sizeof(key->eth.src)) || |
| 1886 | fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE, |
| 1887 | &mask->basic.n_proto, TCA_FLOWER_UNSPEC, |
| 1888 | sizeof(key->basic.n_proto))) |
| 1889 | goto nla_put_failure; |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1890 | |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 1891 | if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls)) |
| 1892 | goto nla_put_failure; |
| 1893 | |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1894 | if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID, |
| 1895 | TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan)) |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1896 | goto nla_put_failure; |
| 1897 | |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1898 | if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID, |
| 1899 | TCA_FLOWER_KEY_CVLAN_PRIO, |
| 1900 | &key->cvlan, &mask->cvlan) || |
| 1901 | (mask->cvlan.vlan_tpid && |
Jianbo Liu | 158abbf | 2018-07-25 02:31:25 +0000 | [diff] [blame] | 1902 | nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, |
| 1903 | key->cvlan.vlan_tpid))) |
Jianbo Liu | d306951 | 2018-07-06 05:38:15 +0000 | [diff] [blame] | 1904 | goto nla_put_failure; |
| 1905 | |
Jianbo Liu | 5e9a0fe | 2018-07-09 02:26:20 +0000 | [diff] [blame] | 1906 | if (mask->basic.n_proto) { |
| 1907 | if (mask->cvlan.vlan_tpid) { |
| 1908 | if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE, |
| 1909 | key->basic.n_proto)) |
| 1910 | goto nla_put_failure; |
| 1911 | } else if (mask->vlan.vlan_tpid) { |
| 1912 | if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, |
| 1913 | key->basic.n_proto)) |
| 1914 | goto nla_put_failure; |
| 1915 | } |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1918 | if ((key->basic.n_proto == htons(ETH_P_IP) || |
| 1919 | key->basic.n_proto == htons(ETH_P_IPV6)) && |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 1920 | (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1921 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 1922 | sizeof(key->basic.ip_proto)) || |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 1923 | fl_dump_key_ip(skb, false, &key->ip, &mask->ip))) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1924 | goto nla_put_failure; |
| 1925 | |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1926 | if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1927 | (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
| 1928 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, |
| 1929 | sizeof(key->ipv4.src)) || |
| 1930 | fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, |
| 1931 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, |
| 1932 | sizeof(key->ipv4.dst)))) |
| 1933 | goto nla_put_failure; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 1934 | else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1935 | (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
| 1936 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, |
| 1937 | sizeof(key->ipv6.src)) || |
| 1938 | fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, |
| 1939 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, |
| 1940 | sizeof(key->ipv6.dst)))) |
| 1941 | goto nla_put_failure; |
| 1942 | |
| 1943 | if (key->basic.ip_proto == IPPROTO_TCP && |
| 1944 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC, |
Or Gerlitz | aa72d70 | 2016-09-15 15:28:22 +0300 | [diff] [blame] | 1945 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1946 | sizeof(key->tp.src)) || |
| 1947 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST, |
Or Gerlitz | aa72d70 | 2016-09-15 15:28:22 +0300 | [diff] [blame] | 1948 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
Jiri Pirko | fdfc7dd | 2017-05-23 18:40:45 +0200 | [diff] [blame] | 1949 | sizeof(key->tp.dst)) || |
| 1950 | fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, |
| 1951 | &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, |
| 1952 | sizeof(key->tcp.flags)))) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1953 | goto nla_put_failure; |
| 1954 | else if (key->basic.ip_proto == IPPROTO_UDP && |
| 1955 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC, |
Or Gerlitz | aa72d70 | 2016-09-15 15:28:22 +0300 | [diff] [blame] | 1956 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1957 | sizeof(key->tp.src)) || |
| 1958 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST, |
Or Gerlitz | aa72d70 | 2016-09-15 15:28:22 +0300 | [diff] [blame] | 1959 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1960 | sizeof(key->tp.dst)))) |
| 1961 | goto nla_put_failure; |
Simon Horman | 5976c5f | 2016-11-03 13:24:21 +0100 | [diff] [blame] | 1962 | else if (key->basic.ip_proto == IPPROTO_SCTP && |
| 1963 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, |
| 1964 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, |
| 1965 | sizeof(key->tp.src)) || |
| 1966 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, |
| 1967 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, |
| 1968 | sizeof(key->tp.dst)))) |
| 1969 | goto nla_put_failure; |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 1970 | else if (key->basic.n_proto == htons(ETH_P_IP) && |
| 1971 | key->basic.ip_proto == IPPROTO_ICMP && |
| 1972 | (fl_dump_key_val(skb, &key->icmp.type, |
| 1973 | TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type, |
| 1974 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, |
| 1975 | sizeof(key->icmp.type)) || |
| 1976 | fl_dump_key_val(skb, &key->icmp.code, |
| 1977 | TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code, |
| 1978 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK, |
| 1979 | sizeof(key->icmp.code)))) |
| 1980 | goto nla_put_failure; |
| 1981 | else if (key->basic.n_proto == htons(ETH_P_IPV6) && |
| 1982 | key->basic.ip_proto == IPPROTO_ICMPV6 && |
| 1983 | (fl_dump_key_val(skb, &key->icmp.type, |
| 1984 | TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type, |
| 1985 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, |
| 1986 | sizeof(key->icmp.type)) || |
| 1987 | fl_dump_key_val(skb, &key->icmp.code, |
| 1988 | TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code, |
| 1989 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK, |
| 1990 | sizeof(key->icmp.code)))) |
| 1991 | goto nla_put_failure; |
Simon Horman | 99d3132 | 2017-01-11 14:05:43 +0100 | [diff] [blame] | 1992 | else if ((key->basic.n_proto == htons(ETH_P_ARP) || |
| 1993 | key->basic.n_proto == htons(ETH_P_RARP)) && |
| 1994 | (fl_dump_key_val(skb, &key->arp.sip, |
| 1995 | TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip, |
| 1996 | TCA_FLOWER_KEY_ARP_SIP_MASK, |
| 1997 | sizeof(key->arp.sip)) || |
| 1998 | fl_dump_key_val(skb, &key->arp.tip, |
| 1999 | TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip, |
| 2000 | TCA_FLOWER_KEY_ARP_TIP_MASK, |
| 2001 | sizeof(key->arp.tip)) || |
| 2002 | fl_dump_key_val(skb, &key->arp.op, |
| 2003 | TCA_FLOWER_KEY_ARP_OP, &mask->arp.op, |
| 2004 | TCA_FLOWER_KEY_ARP_OP_MASK, |
| 2005 | sizeof(key->arp.op)) || |
| 2006 | fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, |
| 2007 | mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, |
| 2008 | sizeof(key->arp.sha)) || |
| 2009 | fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, |
| 2010 | mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, |
| 2011 | sizeof(key->arp.tha)))) |
| 2012 | goto nla_put_failure; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2013 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 2014 | if ((key->basic.ip_proto == IPPROTO_TCP || |
| 2015 | key->basic.ip_proto == IPPROTO_UDP || |
| 2016 | key->basic.ip_proto == IPPROTO_SCTP) && |
| 2017 | fl_dump_key_port_range(skb, key, mask)) |
| 2018 | goto nla_put_failure; |
| 2019 | |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 2020 | if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
| 2021 | (fl_dump_key_val(skb, &key->enc_ipv4.src, |
| 2022 | TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src, |
| 2023 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, |
| 2024 | sizeof(key->enc_ipv4.src)) || |
| 2025 | fl_dump_key_val(skb, &key->enc_ipv4.dst, |
| 2026 | TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst, |
| 2027 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, |
| 2028 | sizeof(key->enc_ipv4.dst)))) |
| 2029 | goto nla_put_failure; |
| 2030 | else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && |
| 2031 | (fl_dump_key_val(skb, &key->enc_ipv6.src, |
| 2032 | TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src, |
| 2033 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, |
| 2034 | sizeof(key->enc_ipv6.src)) || |
| 2035 | fl_dump_key_val(skb, &key->enc_ipv6.dst, |
| 2036 | TCA_FLOWER_KEY_ENC_IPV6_DST, |
| 2037 | &mask->enc_ipv6.dst, |
| 2038 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, |
| 2039 | sizeof(key->enc_ipv6.dst)))) |
| 2040 | goto nla_put_failure; |
| 2041 | |
| 2042 | if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID, |
Hadar Hen Zion | eb523f4 | 2016-09-27 11:21:18 +0300 | [diff] [blame] | 2043 | &mask->enc_key_id, TCA_FLOWER_UNSPEC, |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 2044 | sizeof(key->enc_key_id)) || |
| 2045 | fl_dump_key_val(skb, &key->enc_tp.src, |
| 2046 | TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, |
| 2047 | &mask->enc_tp.src, |
| 2048 | TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, |
| 2049 | sizeof(key->enc_tp.src)) || |
| 2050 | fl_dump_key_val(skb, &key->enc_tp.dst, |
| 2051 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT, |
| 2052 | &mask->enc_tp.dst, |
| 2053 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 2054 | sizeof(key->enc_tp.dst)) || |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 2055 | fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) || |
| 2056 | fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts)) |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 2057 | goto nla_put_failure; |
| 2058 | |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 2059 | if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags)) |
| 2060 | goto nla_put_failure; |
| 2061 | |
Jiri Pirko | f574908 | 2018-07-23 09:23:08 +0200 | [diff] [blame] | 2062 | return 0; |
| 2063 | |
| 2064 | nla_put_failure: |
| 2065 | return -EMSGSIZE; |
| 2066 | } |
| 2067 | |
| 2068 | static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2069 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
Jiri Pirko | f574908 | 2018-07-23 09:23:08 +0200 | [diff] [blame] | 2070 | { |
| 2071 | struct cls_fl_filter *f = fh; |
| 2072 | struct nlattr *nest; |
| 2073 | struct fl_flow_key *key, *mask; |
| 2074 | |
| 2075 | if (!f) |
| 2076 | return skb->len; |
| 2077 | |
| 2078 | t->tcm_handle = f->handle; |
| 2079 | |
| 2080 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 2081 | if (!nest) |
| 2082 | goto nla_put_failure; |
| 2083 | |
| 2084 | if (f->res.classid && |
| 2085 | nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid)) |
| 2086 | goto nla_put_failure; |
| 2087 | |
| 2088 | key = &f->key; |
| 2089 | mask = &f->mask->key; |
| 2090 | |
| 2091 | if (fl_dump_key(skb, net, key, mask)) |
| 2092 | goto nla_put_failure; |
| 2093 | |
| 2094 | if (!tc_skip_hw(f->flags)) |
| 2095 | fl_hw_update_stats(tp, f); |
| 2096 | |
Or Gerlitz | 749e672 | 2017-02-16 10:31:10 +0200 | [diff] [blame] | 2097 | if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags)) |
| 2098 | goto nla_put_failure; |
Amir Vadai | e69985c | 2016-06-05 17:11:18 +0300 | [diff] [blame] | 2099 | |
Vlad Buslov | 86c5536 | 2018-09-07 17:22:21 +0300 | [diff] [blame] | 2100 | if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count)) |
| 2101 | goto nla_put_failure; |
| 2102 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2103 | if (tcf_exts_dump(skb, &f->exts)) |
| 2104 | goto nla_put_failure; |
| 2105 | |
| 2106 | nla_nest_end(skb, nest); |
| 2107 | |
| 2108 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) |
| 2109 | goto nla_put_failure; |
| 2110 | |
| 2111 | return skb->len; |
| 2112 | |
| 2113 | nla_put_failure: |
| 2114 | nla_nest_cancel(skb, nest); |
| 2115 | return -1; |
| 2116 | } |
| 2117 | |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 2118 | static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv) |
| 2119 | { |
| 2120 | struct fl_flow_tmplt *tmplt = tmplt_priv; |
| 2121 | struct fl_flow_key *key, *mask; |
| 2122 | struct nlattr *nest; |
| 2123 | |
| 2124 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 2125 | if (!nest) |
| 2126 | goto nla_put_failure; |
| 2127 | |
| 2128 | key = &tmplt->dummy_key; |
| 2129 | mask = &tmplt->mask; |
| 2130 | |
| 2131 | if (fl_dump_key(skb, net, key, mask)) |
| 2132 | goto nla_put_failure; |
| 2133 | |
| 2134 | nla_nest_end(skb, nest); |
| 2135 | |
| 2136 | return skb->len; |
| 2137 | |
| 2138 | nla_put_failure: |
| 2139 | nla_nest_cancel(skb, nest); |
| 2140 | return -EMSGSIZE; |
| 2141 | } |
| 2142 | |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 2143 | static void fl_bind_class(void *fh, u32 classid, unsigned long cl) |
| 2144 | { |
| 2145 | struct cls_fl_filter *f = fh; |
| 2146 | |
| 2147 | if (f && f->res.classid == classid) |
| 2148 | f->res.class = cl; |
| 2149 | } |
| 2150 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2151 | static struct tcf_proto_ops cls_fl_ops __read_mostly = { |
| 2152 | .kind = "flower", |
| 2153 | .classify = fl_classify, |
| 2154 | .init = fl_init, |
| 2155 | .destroy = fl_destroy, |
| 2156 | .get = fl_get, |
| 2157 | .change = fl_change, |
| 2158 | .delete = fl_delete, |
| 2159 | .walk = fl_walk, |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 2160 | .reoffload = fl_reoffload, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2161 | .dump = fl_dump, |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 2162 | .bind_class = fl_bind_class, |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 2163 | .tmplt_create = fl_tmplt_create, |
| 2164 | .tmplt_destroy = fl_tmplt_destroy, |
| 2165 | .tmplt_dump = fl_tmplt_dump, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2166 | .owner = THIS_MODULE, |
| 2167 | }; |
| 2168 | |
| 2169 | static int __init cls_fl_init(void) |
| 2170 | { |
| 2171 | return register_tcf_proto_ops(&cls_fl_ops); |
| 2172 | } |
| 2173 | |
| 2174 | static void __exit cls_fl_exit(void) |
| 2175 | { |
| 2176 | unregister_tcf_proto_ops(&cls_fl_ops); |
| 2177 | } |
| 2178 | |
| 2179 | module_init(cls_fl_init); |
| 2180 | module_exit(cls_fl_exit); |
| 2181 | |
| 2182 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); |
| 2183 | MODULE_DESCRIPTION("Flower classifier"); |
| 2184 | MODULE_LICENSE("GPL v2"); |