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