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