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> |
Paul Blakey | ec624fe | 2021-12-14 19:24:33 +0200 | [diff] [blame] | 22 | #include <net/pkt_sched.h> |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 23 | #include <net/ip.h> |
| 24 | #include <net/flow_dissector.h> |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 25 | #include <net/geneve.h> |
Xin Long | d8f9dfa | 2019-11-21 18:03:28 +0800 | [diff] [blame] | 26 | #include <net/vxlan.h> |
Xin Long | 79b1011 | 2019-11-21 18:03:29 +0800 | [diff] [blame] | 27 | #include <net/erspan.h> |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 28 | |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 29 | #include <net/dst.h> |
| 30 | #include <net/dst_metadata.h> |
| 31 | |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 32 | #include <uapi/linux/netfilter/nf_conntrack_common.h> |
| 33 | |
wenxu | 1bcc51a | 2021-02-09 14:37:49 +0800 | [diff] [blame] | 34 | #define TCA_FLOWER_KEY_CT_FLAGS_MAX \ |
| 35 | ((__TCA_FLOWER_KEY_CT_FLAGS_MAX - 1) << 1) |
| 36 | #define TCA_FLOWER_KEY_CT_FLAGS_MASK \ |
| 37 | (TCA_FLOWER_KEY_CT_FLAGS_MAX - 1) |
| 38 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 39 | struct fl_flow_key { |
Jiri Pirko | 8212ed7 | 2019-06-19 09:41:03 +0300 | [diff] [blame] | 40 | struct flow_dissector_key_meta meta; |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 41 | struct flow_dissector_key_control control; |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 42 | struct flow_dissector_key_control enc_control; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 43 | struct flow_dissector_key_basic basic; |
| 44 | struct flow_dissector_key_eth_addrs eth; |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 45 | struct flow_dissector_key_vlan vlan; |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 46 | struct flow_dissector_key_vlan cvlan; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 47 | union { |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 48 | struct flow_dissector_key_ipv4_addrs ipv4; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 49 | struct flow_dissector_key_ipv6_addrs ipv6; |
| 50 | }; |
| 51 | struct flow_dissector_key_ports tp; |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 52 | struct flow_dissector_key_icmp icmp; |
Simon Horman | 99d3132 | 2017-01-11 14:05:43 +0100 | [diff] [blame] | 53 | struct flow_dissector_key_arp arp; |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 54 | struct flow_dissector_key_keyid enc_key_id; |
| 55 | union { |
| 56 | struct flow_dissector_key_ipv4_addrs enc_ipv4; |
| 57 | struct flow_dissector_key_ipv6_addrs enc_ipv6; |
| 58 | }; |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 59 | struct flow_dissector_key_ports enc_tp; |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 60 | struct flow_dissector_key_mpls mpls; |
Jiri Pirko | fdfc7dd | 2017-05-23 18:40:45 +0200 | [diff] [blame] | 61 | struct flow_dissector_key_tcp tcp; |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 62 | struct flow_dissector_key_ip ip; |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 63 | struct flow_dissector_key_ip enc_ip; |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 64 | struct flow_dissector_key_enc_opts enc_opts; |
Yoshiki Komachi | 8ffb055 | 2019-12-03 19:40:12 +0900 | [diff] [blame] | 65 | union { |
| 66 | struct flow_dissector_key_ports tp; |
| 67 | struct { |
| 68 | struct flow_dissector_key_ports tp_min; |
| 69 | struct flow_dissector_key_ports tp_max; |
| 70 | }; |
| 71 | } tp_range; |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 72 | struct flow_dissector_key_ct ct; |
Ariel Levkovich | 5923b8f | 2020-07-23 01:03:01 +0300 | [diff] [blame] | 73 | struct flow_dissector_key_hash hash; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 74 | } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */ |
| 75 | |
| 76 | struct fl_flow_mask_range { |
| 77 | unsigned short int start; |
| 78 | unsigned short int end; |
| 79 | }; |
| 80 | |
| 81 | struct fl_flow_mask { |
| 82 | struct fl_flow_key key; |
| 83 | struct fl_flow_mask_range range; |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 84 | u32 flags; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 85 | struct rhash_head ht_node; |
| 86 | struct rhashtable ht; |
| 87 | struct rhashtable_params filter_ht_params; |
| 88 | struct flow_dissector dissector; |
| 89 | struct list_head filters; |
Paolo Abeni | 44a5cd4 | 2018-06-21 20:02:16 +0200 | [diff] [blame] | 90 | struct rcu_work rwork; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 91 | struct list_head list; |
Vlad Buslov | f48ef4d | 2019-03-21 15:17:37 +0200 | [diff] [blame] | 92 | refcount_t refcnt; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 93 | }; |
| 94 | |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 95 | struct fl_flow_tmplt { |
| 96 | struct fl_flow_key dummy_key; |
| 97 | struct fl_flow_key mask; |
| 98 | struct flow_dissector dissector; |
| 99 | struct tcf_chain *chain; |
| 100 | }; |
| 101 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 102 | struct cls_fl_head { |
| 103 | struct rhashtable ht; |
Vlad Buslov | 259e60f | 2019-03-21 15:17:39 +0200 | [diff] [blame] | 104 | spinlock_t masks_lock; /* Protect masks list */ |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 105 | struct list_head masks; |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 106 | struct list_head hw_filters; |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 107 | struct rcu_work rwork; |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 108 | struct idr handle_idr; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | struct cls_fl_filter { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 112 | struct fl_flow_mask *mask; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 113 | struct rhash_head ht_node; |
| 114 | struct fl_flow_key mkey; |
| 115 | struct tcf_exts exts; |
| 116 | struct tcf_result res; |
| 117 | struct fl_flow_key key; |
| 118 | struct list_head list; |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 119 | struct list_head hw_list; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 120 | u32 handle; |
Amir Vadai | e69985c | 2016-06-05 17:11:18 +0300 | [diff] [blame] | 121 | u32 flags; |
Vlad Buslov | 86c5536 | 2018-09-07 17:22:21 +0300 | [diff] [blame] | 122 | u32 in_hw_count; |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 123 | struct rcu_work rwork; |
Hadar Hen Zion | 7091d8c | 2016-12-01 14:06:37 +0200 | [diff] [blame] | 124 | struct net_device *hw_dev; |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 125 | /* Flower classifier is unlocked, which means that its reference counter |
| 126 | * can be changed concurrently without any kind of external |
| 127 | * synchronization. Use atomic reference counter to be concurrency-safe. |
| 128 | */ |
| 129 | refcount_t refcnt; |
Vlad Buslov | b2552b8 | 2019-03-21 15:17:36 +0200 | [diff] [blame] | 130 | bool deleted; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 131 | }; |
| 132 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 133 | static const struct rhashtable_params mask_ht_params = { |
| 134 | .key_offset = offsetof(struct fl_flow_mask, key), |
| 135 | .key_len = sizeof(struct fl_flow_key), |
| 136 | .head_offset = offsetof(struct fl_flow_mask, ht_node), |
| 137 | .automatic_shrinking = true, |
| 138 | }; |
| 139 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 140 | static unsigned short int fl_mask_range(const struct fl_flow_mask *mask) |
| 141 | { |
| 142 | return mask->range.end - mask->range.start; |
| 143 | } |
| 144 | |
| 145 | static void fl_mask_update_range(struct fl_flow_mask *mask) |
| 146 | { |
| 147 | const u8 *bytes = (const u8 *) &mask->key; |
| 148 | size_t size = sizeof(mask->key); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 149 | size_t i, first = 0, last; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 150 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 151 | for (i = 0; i < size; i++) { |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 152 | if (bytes[i]) { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 153 | first = i; |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | last = first; |
| 158 | for (i = size - 1; i != first; i--) { |
| 159 | if (bytes[i]) { |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 160 | last = i; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 161 | break; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | mask->range.start = rounddown(first, sizeof(long)); |
| 165 | mask->range.end = roundup(last + 1, sizeof(long)); |
| 166 | } |
| 167 | |
| 168 | static void *fl_key_get_start(struct fl_flow_key *key, |
| 169 | const struct fl_flow_mask *mask) |
| 170 | { |
| 171 | return (u8 *) key + mask->range.start; |
| 172 | } |
| 173 | |
| 174 | static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key, |
| 175 | struct fl_flow_mask *mask) |
| 176 | { |
| 177 | const long *lkey = fl_key_get_start(key, mask); |
| 178 | const long *lmask = fl_key_get_start(&mask->key, mask); |
| 179 | long *lmkey = fl_key_get_start(mkey, mask); |
| 180 | int i; |
| 181 | |
| 182 | for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) |
| 183 | *lmkey++ = *lkey++ & *lmask++; |
| 184 | } |
| 185 | |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 186 | static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt, |
| 187 | struct fl_flow_mask *mask) |
| 188 | { |
| 189 | const long *lmask = fl_key_get_start(&mask->key, mask); |
| 190 | const long *ltmplt; |
| 191 | int i; |
| 192 | |
| 193 | if (!tmplt) |
| 194 | return true; |
| 195 | ltmplt = fl_key_get_start(&tmplt->mask, mask); |
| 196 | for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) { |
| 197 | if (~*ltmplt++ & *lmask++) |
| 198 | return false; |
| 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 203 | static void fl_clear_masked_range(struct fl_flow_key *key, |
| 204 | struct fl_flow_mask *mask) |
| 205 | { |
| 206 | memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask)); |
| 207 | } |
| 208 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 209 | static bool fl_range_port_dst_cmp(struct cls_fl_filter *filter, |
| 210 | struct fl_flow_key *key, |
| 211 | struct fl_flow_key *mkey) |
| 212 | { |
Vladimir Oltean | 6215afc | 2021-03-21 23:05:48 +0200 | [diff] [blame] | 213 | u16 min_mask, max_mask, min_val, max_val; |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 214 | |
Vladimir Oltean | 6215afc | 2021-03-21 23:05:48 +0200 | [diff] [blame] | 215 | min_mask = ntohs(filter->mask->key.tp_range.tp_min.dst); |
| 216 | max_mask = ntohs(filter->mask->key.tp_range.tp_max.dst); |
| 217 | min_val = ntohs(filter->key.tp_range.tp_min.dst); |
| 218 | max_val = ntohs(filter->key.tp_range.tp_max.dst); |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 219 | |
| 220 | if (min_mask && max_mask) { |
Vladimir Oltean | 6215afc | 2021-03-21 23:05:48 +0200 | [diff] [blame] | 221 | if (ntohs(key->tp_range.tp.dst) < min_val || |
| 222 | ntohs(key->tp_range.tp.dst) > max_val) |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 223 | return false; |
| 224 | |
| 225 | /* skb does not have min and max values */ |
Yoshiki Komachi | 8ffb055 | 2019-12-03 19:40:12 +0900 | [diff] [blame] | 226 | mkey->tp_range.tp_min.dst = filter->mkey.tp_range.tp_min.dst; |
| 227 | mkey->tp_range.tp_max.dst = filter->mkey.tp_range.tp_max.dst; |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 228 | } |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | static bool fl_range_port_src_cmp(struct cls_fl_filter *filter, |
| 233 | struct fl_flow_key *key, |
| 234 | struct fl_flow_key *mkey) |
| 235 | { |
Vladimir Oltean | 6215afc | 2021-03-21 23:05:48 +0200 | [diff] [blame] | 236 | u16 min_mask, max_mask, min_val, max_val; |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 237 | |
Vladimir Oltean | 6215afc | 2021-03-21 23:05:48 +0200 | [diff] [blame] | 238 | min_mask = ntohs(filter->mask->key.tp_range.tp_min.src); |
| 239 | max_mask = ntohs(filter->mask->key.tp_range.tp_max.src); |
| 240 | min_val = ntohs(filter->key.tp_range.tp_min.src); |
| 241 | max_val = ntohs(filter->key.tp_range.tp_max.src); |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 242 | |
| 243 | if (min_mask && max_mask) { |
Vladimir Oltean | 6215afc | 2021-03-21 23:05:48 +0200 | [diff] [blame] | 244 | if (ntohs(key->tp_range.tp.src) < min_val || |
| 245 | ntohs(key->tp_range.tp.src) > max_val) |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 246 | return false; |
| 247 | |
| 248 | /* skb does not have min and max values */ |
Yoshiki Komachi | 8ffb055 | 2019-12-03 19:40:12 +0900 | [diff] [blame] | 249 | mkey->tp_range.tp_min.src = filter->mkey.tp_range.tp_min.src; |
| 250 | mkey->tp_range.tp_max.src = filter->mkey.tp_range.tp_max.src; |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 251 | } |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | static struct cls_fl_filter *__fl_lookup(struct fl_flow_mask *mask, |
| 256 | struct fl_flow_key *mkey) |
Paul Blakey | a3308d8 | 2017-01-16 10:45:13 +0200 | [diff] [blame] | 257 | { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 258 | return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask), |
| 259 | mask->filter_ht_params); |
Paul Blakey | a3308d8 | 2017-01-16 10:45:13 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 262 | static struct cls_fl_filter *fl_lookup_range(struct fl_flow_mask *mask, |
| 263 | struct fl_flow_key *mkey, |
| 264 | struct fl_flow_key *key) |
| 265 | { |
| 266 | struct cls_fl_filter *filter, *f; |
| 267 | |
| 268 | list_for_each_entry_rcu(filter, &mask->filters, list) { |
| 269 | if (!fl_range_port_dst_cmp(filter, key, mkey)) |
| 270 | continue; |
| 271 | |
| 272 | if (!fl_range_port_src_cmp(filter, key, mkey)) |
| 273 | continue; |
| 274 | |
| 275 | f = __fl_lookup(mask, mkey); |
| 276 | if (f) |
| 277 | return f; |
| 278 | } |
| 279 | return NULL; |
| 280 | } |
| 281 | |
Arnd Bergmann | 0af413b | 2020-05-29 22:13:58 +0200 | [diff] [blame] | 282 | static noinline_for_stack |
| 283 | struct cls_fl_filter *fl_mask_lookup(struct fl_flow_mask *mask, struct fl_flow_key *key) |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 284 | { |
Arnd Bergmann | 0af413b | 2020-05-29 22:13:58 +0200 | [diff] [blame] | 285 | struct fl_flow_key mkey; |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 286 | |
Arnd Bergmann | 0af413b | 2020-05-29 22:13:58 +0200 | [diff] [blame] | 287 | fl_set_masked_key(&mkey, key, mask); |
| 288 | if ((mask->flags & TCA_FLOWER_MASK_FLAGS_RANGE)) |
| 289 | return fl_lookup_range(mask, &mkey, key); |
| 290 | |
| 291 | return __fl_lookup(mask, &mkey); |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 294 | static u16 fl_ct_info_to_flower_map[] = { |
| 295 | [IP_CT_ESTABLISHED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | |
| 296 | TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED, |
| 297 | [IP_CT_RELATED] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | |
| 298 | TCA_FLOWER_KEY_CT_FLAGS_RELATED, |
| 299 | [IP_CT_ESTABLISHED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | |
Paul Blakey | 8c85d18 | 2021-01-27 16:32:45 +0200 | [diff] [blame] | 300 | TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED | |
| 301 | TCA_FLOWER_KEY_CT_FLAGS_REPLY, |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 302 | [IP_CT_RELATED_REPLY] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | |
Paul Blakey | 8c85d18 | 2021-01-27 16:32:45 +0200 | [diff] [blame] | 303 | TCA_FLOWER_KEY_CT_FLAGS_RELATED | |
| 304 | TCA_FLOWER_KEY_CT_FLAGS_REPLY, |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 305 | [IP_CT_NEW] = TCA_FLOWER_KEY_CT_FLAGS_TRACKED | |
| 306 | TCA_FLOWER_KEY_CT_FLAGS_NEW, |
| 307 | }; |
| 308 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 309 | static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 310 | struct tcf_result *res) |
| 311 | { |
| 312 | struct cls_fl_head *head = rcu_dereference_bh(tp->root); |
Paul Blakey | ec624fe | 2021-12-14 19:24:33 +0200 | [diff] [blame] | 313 | bool post_ct = tc_skb_cb(skb)->post_ct; |
Paul Blakey | 3849595 | 2021-12-14 19:24:34 +0200 | [diff] [blame] | 314 | u16 zone = tc_skb_cb(skb)->zone; |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 315 | struct fl_flow_key skb_key; |
| 316 | struct fl_flow_mask *mask; |
| 317 | struct cls_fl_filter *f; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 318 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 319 | list_for_each_entry_rcu(mask, &head->masks, list) { |
Jason Baron | 8a9093c | 2020-02-17 15:38:09 -0500 | [diff] [blame] | 320 | flow_dissector_init_keys(&skb_key.control, &skb_key.basic); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 321 | fl_clear_masked_range(&skb_key, mask); |
Amir Vadai | e69985c | 2016-06-05 17:11:18 +0300 | [diff] [blame] | 322 | |
Jiri Pirko | 8212ed7 | 2019-06-19 09:41:03 +0300 | [diff] [blame] | 323 | skb_flow_dissect_meta(skb, &mask->dissector, &skb_key); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 324 | /* skb_flow_dissect() does not set n_proto in case an unknown |
| 325 | * protocol, so do it rather here. |
| 326 | */ |
Toke Høiland-Jørgensen | d7bf2eb | 2020-07-03 22:26:43 +0200 | [diff] [blame] | 327 | skb_key.basic.n_proto = skb_protocol(skb, false); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 328 | skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key); |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 329 | skb_flow_dissect_ct(skb, &mask->dissector, &skb_key, |
| 330 | fl_ct_info_to_flower_map, |
wenxu | 7baf242 | 2021-01-19 16:31:50 +0800 | [diff] [blame] | 331 | ARRAY_SIZE(fl_ct_info_to_flower_map), |
Paul Blakey | 3849595 | 2021-12-14 19:24:34 +0200 | [diff] [blame] | 332 | post_ct, zone); |
Ariel Levkovich | 5923b8f | 2020-07-23 01:03:01 +0300 | [diff] [blame] | 333 | skb_flow_dissect_hash(skb, &mask->dissector, &skb_key); |
Yoshiki Komachi | 6de6e46 | 2021-10-29 09:21:41 +0000 | [diff] [blame] | 334 | skb_flow_dissect(skb, &mask->dissector, &skb_key, |
| 335 | FLOW_DISSECTOR_F_STOP_BEFORE_ENCAP); |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 336 | |
Arnd Bergmann | 0af413b | 2020-05-29 22:13:58 +0200 | [diff] [blame] | 337 | f = fl_mask_lookup(mask, &skb_key); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 338 | if (f && !tc_skip_sw(f->flags)) { |
| 339 | *res = f->res; |
| 340 | return tcf_exts_exec(skb, &f->exts, res); |
| 341 | } |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 342 | } |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | static int fl_init(struct tcf_proto *tp) |
| 347 | { |
| 348 | struct cls_fl_head *head; |
| 349 | |
| 350 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 351 | if (!head) |
| 352 | return -ENOBUFS; |
| 353 | |
Vlad Buslov | 259e60f | 2019-03-21 15:17:39 +0200 | [diff] [blame] | 354 | spin_lock_init(&head->masks_lock); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 355 | INIT_LIST_HEAD_RCU(&head->masks); |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 356 | INIT_LIST_HEAD(&head->hw_filters); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 357 | rcu_assign_pointer(tp->root, head); |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 358 | idr_init(&head->handle_idr); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 359 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 360 | return rhashtable_init(&head->ht, &mask_ht_params); |
| 361 | } |
| 362 | |
Vlad Buslov | 99815f5 | 2019-06-13 17:54:04 +0300 | [diff] [blame] | 363 | 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] | 364 | { |
Vlad Buslov | 99815f5 | 2019-06-13 17:54:04 +0300 | [diff] [blame] | 365 | /* temporary masks don't have their filters list and ht initialized */ |
| 366 | if (mask_init_done) { |
| 367 | WARN_ON(!list_empty(&mask->filters)); |
| 368 | rhashtable_destroy(&mask->ht); |
| 369 | } |
Paolo Abeni | 44a5cd4 | 2018-06-21 20:02:16 +0200 | [diff] [blame] | 370 | kfree(mask); |
| 371 | } |
| 372 | |
| 373 | static void fl_mask_free_work(struct work_struct *work) |
| 374 | { |
| 375 | struct fl_flow_mask *mask = container_of(to_rcu_work(work), |
| 376 | struct fl_flow_mask, rwork); |
| 377 | |
Vlad Buslov | 99815f5 | 2019-06-13 17:54:04 +0300 | [diff] [blame] | 378 | fl_mask_free(mask, true); |
| 379 | } |
| 380 | |
| 381 | static void fl_uninit_mask_free_work(struct work_struct *work) |
| 382 | { |
| 383 | struct fl_flow_mask *mask = container_of(to_rcu_work(work), |
| 384 | struct fl_flow_mask, rwork); |
| 385 | |
| 386 | fl_mask_free(mask, false); |
Paolo Abeni | 44a5cd4 | 2018-06-21 20:02:16 +0200 | [diff] [blame] | 387 | } |
| 388 | |
Vlad Buslov | 9994677 | 2019-04-12 00:54:19 +0300 | [diff] [blame] | 389 | 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] | 390 | { |
Vlad Buslov | f48ef4d | 2019-03-21 15:17:37 +0200 | [diff] [blame] | 391 | if (!refcount_dec_and_test(&mask->refcnt)) |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 392 | return false; |
| 393 | |
| 394 | rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params); |
Vlad Buslov | 259e60f | 2019-03-21 15:17:39 +0200 | [diff] [blame] | 395 | |
| 396 | spin_lock(&head->masks_lock); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 397 | list_del_rcu(&mask->list); |
Vlad Buslov | 259e60f | 2019-03-21 15:17:39 +0200 | [diff] [blame] | 398 | spin_unlock(&head->masks_lock); |
| 399 | |
Vlad Buslov | 9994677 | 2019-04-12 00:54:19 +0300 | [diff] [blame] | 400 | tcf_queue_work(&mask->rwork, fl_mask_free_work); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 401 | |
| 402 | return true; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 405 | static struct cls_fl_head *fl_head_dereference(struct tcf_proto *tp) |
| 406 | { |
| 407 | /* Flower classifier only changes root pointer during init and destroy. |
| 408 | * Users must obtain reference to tcf_proto instance before calling its |
| 409 | * API, so tp->root pointer is protected from concurrent call to |
| 410 | * fl_destroy() by reference counting. |
| 411 | */ |
| 412 | return rcu_dereference_raw(tp->root); |
| 413 | } |
| 414 | |
Cong Wang | 0dadc11 | 2017-11-06 13:47:24 -0800 | [diff] [blame] | 415 | static void __fl_destroy_filter(struct cls_fl_filter *f) |
| 416 | { |
| 417 | tcf_exts_destroy(&f->exts); |
| 418 | tcf_exts_put_net(&f->exts); |
| 419 | kfree(f); |
| 420 | } |
| 421 | |
Cong Wang | 0552c8a | 2017-10-26 18:24:33 -0700 | [diff] [blame] | 422 | static void fl_destroy_filter_work(struct work_struct *work) |
| 423 | { |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 424 | struct cls_fl_filter *f = container_of(to_rcu_work(work), |
| 425 | struct cls_fl_filter, rwork); |
Cong Wang | 0552c8a | 2017-10-26 18:24:33 -0700 | [diff] [blame] | 426 | |
Cong Wang | 0dadc11 | 2017-11-06 13:47:24 -0800 | [diff] [blame] | 427 | __fl_destroy_filter(f); |
Cong Wang | 0552c8a | 2017-10-26 18:24:33 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Jakub Kicinski | 1b0f803 | 2018-01-24 12:54:21 -0800 | [diff] [blame] | 430 | 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] | 431 | bool rtnl_held, 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 = {}; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 435 | |
Pieter Jansen van Vuuren | d678714 | 2019-05-06 17:24:21 -0700 | [diff] [blame] | 436 | 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] | 437 | cls_flower.command = FLOW_CLS_DESTROY; |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 438 | cls_flower.cookie = (unsigned long) f; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 439 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 440 | tc_setup_cb_destroy(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, false, |
Vlad Buslov | 918190f | 2019-08-26 16:45:06 +0300 | [diff] [blame] | 441 | &f->flags, &f->in_hw_count, rtnl_held); |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 442 | |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Amir Vadai | e8eb36c | 2016-06-13 12:06:39 +0300 | [diff] [blame] | 445 | static int fl_hw_replace_filter(struct tcf_proto *tp, |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 446 | struct cls_fl_filter *f, bool rtnl_held, |
Quentin Monnet | 4100203 | 2018-01-19 17:44:43 -0800 | [diff] [blame] | 447 | struct netlink_ext_ack *extack) |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 448 | { |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 449 | struct tcf_block *block = tp->chain->block; |
Pablo Neira Ayuso | f9e3008 | 2019-07-09 22:55:49 +0200 | [diff] [blame] | 450 | struct flow_cls_offload cls_flower = {}; |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 451 | bool skip_sw = tc_skip_sw(f->flags); |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 452 | int err = 0; |
| 453 | |
Pablo Neira Ayuso | e3ab786 | 2019-02-02 12:50:45 +0100 | [diff] [blame] | 454 | cls_flower.rule = flow_rule_alloc(tcf_exts_num_actions(&f->exts)); |
Vlad Buslov | 918190f | 2019-08-26 16:45:06 +0300 | [diff] [blame] | 455 | if (!cls_flower.rule) |
| 456 | return -ENOMEM; |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 457 | |
Pieter Jansen van Vuuren | d678714 | 2019-05-06 17:24:21 -0700 | [diff] [blame] | 458 | 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] | 459 | cls_flower.command = FLOW_CLS_REPLACE; |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 460 | cls_flower.cookie = (unsigned long) f; |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 461 | cls_flower.rule->match.dissector = &f->mask->dissector; |
| 462 | cls_flower.rule->match.mask = &f->mask->key; |
| 463 | cls_flower.rule->match.key = &f->mkey; |
Amritha Nambiar | 384c181 | 2017-10-27 02:35:34 -0700 | [diff] [blame] | 464 | cls_flower.classid = f->res.classid; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 465 | |
Baowen Zheng | 9c1c0e1 | 2021-12-17 19:16:20 +0100 | [diff] [blame] | 466 | err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts); |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 467 | if (err) { |
| 468 | kfree(cls_flower.rule); |
Vlad Buslov | 918190f | 2019-08-26 16:45:06 +0300 | [diff] [blame] | 469 | if (skip_sw) { |
Vlad Buslov | 1f15bb4 | 2019-02-12 23:39:06 +0200 | [diff] [blame] | 470 | NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); |
Vlad Buslov | 918190f | 2019-08-26 16:45:06 +0300 | [diff] [blame] | 471 | return err; |
| 472 | } |
| 473 | return 0; |
Pablo Neira Ayuso | 3a7b686 | 2019-02-02 12:50:46 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 476 | err = tc_setup_cb_add(block, tp, TC_SETUP_CLSFLOWER, &cls_flower, |
Vlad Buslov | 918190f | 2019-08-26 16:45:06 +0300 | [diff] [blame] | 477 | skip_sw, &f->flags, &f->in_hw_count, rtnl_held); |
Baowen Zheng | 9c1c0e1 | 2021-12-17 19:16:20 +0100 | [diff] [blame] | 478 | tc_cleanup_offload_action(&cls_flower.rule->action); |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 479 | kfree(cls_flower.rule); |
| 480 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 481 | if (err) { |
Vlad Buslov | 918190f | 2019-08-26 16:45:06 +0300 | [diff] [blame] | 482 | fl_hw_destroy_filter(tp, f, rtnl_held, NULL); |
| 483 | return err; |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 484 | } |
| 485 | |
Vlad Buslov | 918190f | 2019-08-26 16:45:06 +0300 | [diff] [blame] | 486 | if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW)) |
| 487 | return -EINVAL; |
Jiri Pirko | 717503b | 2017-10-11 09:41:09 +0200 | [diff] [blame] | 488 | |
Vlad Buslov | 918190f | 2019-08-26 16:45:06 +0300 | [diff] [blame] | 489 | return 0; |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 490 | } |
| 491 | |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 492 | static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f, |
| 493 | bool rtnl_held) |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 494 | { |
Jiri Pirko | 208c0f4 | 2017-10-19 15:50:32 +0200 | [diff] [blame] | 495 | struct tcf_block *block = tp->chain->block; |
Pablo Neira Ayuso | f9e3008 | 2019-07-09 22:55:49 +0200 | [diff] [blame] | 496 | struct flow_cls_offload cls_flower = {}; |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 497 | |
Pieter Jansen van Vuuren | d678714 | 2019-05-06 17:24:21 -0700 | [diff] [blame] | 498 | 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] | 499 | cls_flower.command = FLOW_CLS_STATS; |
Jiri Pirko | de4784c | 2017-08-07 10:15:32 +0200 | [diff] [blame] | 500 | cls_flower.cookie = (unsigned long) f; |
Amritha Nambiar | 384c181 | 2017-10-27 02:35:34 -0700 | [diff] [blame] | 501 | cls_flower.classid = f->res.classid; |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 502 | |
Vlad Buslov | 918190f | 2019-08-26 16:45:06 +0300 | [diff] [blame] | 503 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, |
| 504 | rtnl_held); |
Pablo Neira Ayuso | 3b1903e | 2019-02-02 12:50:47 +0100 | [diff] [blame] | 505 | |
Baowen Zheng | bcd6436 | 2021-12-17 19:16:24 +0100 | [diff] [blame] | 506 | tcf_exts_hw_stats_update(&f->exts, cls_flower.stats.bytes, |
| 507 | cls_flower.stats.pkts, |
| 508 | cls_flower.stats.drops, |
| 509 | cls_flower.stats.lastused, |
| 510 | cls_flower.stats.used_hw_stats, |
| 511 | cls_flower.stats.used_hw_stats_valid); |
Amir Vadai | 10cbc68 | 2016-05-13 12:55:37 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 514 | static void __fl_put(struct cls_fl_filter *f) |
| 515 | { |
| 516 | if (!refcount_dec_and_test(&f->refcnt)) |
| 517 | return; |
| 518 | |
| 519 | if (tcf_exts_get_net(&f->exts)) |
| 520 | tcf_queue_work(&f->rwork, fl_destroy_filter_work); |
| 521 | else |
| 522 | __fl_destroy_filter(f); |
| 523 | } |
| 524 | |
| 525 | static struct cls_fl_filter *__fl_get(struct cls_fl_head *head, u32 handle) |
| 526 | { |
| 527 | struct cls_fl_filter *f; |
| 528 | |
| 529 | rcu_read_lock(); |
| 530 | f = idr_find(&head->handle_idr, handle); |
| 531 | if (f && !refcount_inc_not_zero(&f->refcnt)) |
| 532 | f = NULL; |
| 533 | rcu_read_unlock(); |
| 534 | |
| 535 | return f; |
| 536 | } |
| 537 | |
Vlad Buslov | b2552b8 | 2019-03-21 15:17:36 +0200 | [diff] [blame] | 538 | 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] | 539 | bool *last, bool rtnl_held, |
| 540 | struct netlink_ext_ack *extack) |
Roi Dayan | 13fa876 | 2016-11-01 16:08:29 +0200 | [diff] [blame] | 541 | { |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame] | 542 | struct cls_fl_head *head = fl_head_dereference(tp); |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 543 | |
Vlad Buslov | b2552b8 | 2019-03-21 15:17:36 +0200 | [diff] [blame] | 544 | *last = false; |
| 545 | |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 546 | spin_lock(&tp->lock); |
| 547 | if (f->deleted) { |
| 548 | spin_unlock(&tp->lock); |
Vlad Buslov | b2552b8 | 2019-03-21 15:17:36 +0200 | [diff] [blame] | 549 | return -ENOENT; |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 550 | } |
Vlad Buslov | b2552b8 | 2019-03-21 15:17:36 +0200 | [diff] [blame] | 551 | |
| 552 | f->deleted = true; |
| 553 | rhashtable_remove_fast(&f->mask->ht, &f->ht_node, |
| 554 | f->mask->filter_ht_params); |
Matthew Wilcox | 9c16094 | 2017-11-28 09:48:43 -0500 | [diff] [blame] | 555 | idr_remove(&head->handle_idr, f->handle); |
Roi Dayan | 13fa876 | 2016-11-01 16:08:29 +0200 | [diff] [blame] | 556 | list_del_rcu(&f->list); |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 557 | spin_unlock(&tp->lock); |
| 558 | |
Vlad Buslov | 9994677 | 2019-04-12 00:54:19 +0300 | [diff] [blame] | 559 | *last = fl_mask_put(head, f->mask); |
Hadar Hen Zion | 7968521 | 2016-12-01 14:06:34 +0200 | [diff] [blame] | 560 | if (!tc_skip_hw(f->flags)) |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 561 | fl_hw_destroy_filter(tp, f, rtnl_held, extack); |
Roi Dayan | 13fa876 | 2016-11-01 16:08:29 +0200 | [diff] [blame] | 562 | tcf_unbind_filter(tp, &f->res); |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 563 | __fl_put(f); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 564 | |
Vlad Buslov | b2552b8 | 2019-03-21 15:17:36 +0200 | [diff] [blame] | 565 | return 0; |
Roi Dayan | 13fa876 | 2016-11-01 16:08:29 +0200 | [diff] [blame] | 566 | } |
| 567 | |
Daniel Borkmann | d936377 | 2016-11-27 01:18:01 +0100 | [diff] [blame] | 568 | static void fl_destroy_sleepable(struct work_struct *work) |
| 569 | { |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 570 | struct cls_fl_head *head = container_of(to_rcu_work(work), |
| 571 | struct cls_fl_head, |
| 572 | rwork); |
Paul Blakey | de9dc65 | 2018-06-03 10:06:13 +0300 | [diff] [blame] | 573 | |
| 574 | rhashtable_destroy(&head->ht); |
Daniel Borkmann | d936377 | 2016-11-27 01:18:01 +0100 | [diff] [blame] | 575 | kfree(head); |
| 576 | module_put(THIS_MODULE); |
| 577 | } |
| 578 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 579 | static void fl_destroy(struct tcf_proto *tp, bool rtnl_held, |
| 580 | struct netlink_ext_ack *extack) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 581 | { |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame] | 582 | struct cls_fl_head *head = fl_head_dereference(tp); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 583 | struct fl_flow_mask *mask, *next_mask; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 584 | struct cls_fl_filter *f, *next; |
Vlad Buslov | b2552b8 | 2019-03-21 15:17:36 +0200 | [diff] [blame] | 585 | bool last; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 586 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 587 | list_for_each_entry_safe(mask, next_mask, &head->masks, list) { |
| 588 | list_for_each_entry_safe(f, next, &mask->filters, list) { |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 589 | __fl_delete(tp, f, &last, rtnl_held, extack); |
Vlad Buslov | b2552b8 | 2019-03-21 15:17:36 +0200 | [diff] [blame] | 590 | if (last) |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | } |
Chris Mi | c15ab23 | 2017-08-30 02:31:58 -0400 | [diff] [blame] | 594 | idr_destroy(&head->handle_idr); |
Daniel Borkmann | d936377 | 2016-11-27 01:18:01 +0100 | [diff] [blame] | 595 | |
| 596 | __module_get(THIS_MODULE); |
Cong Wang | aaa908f | 2018-05-23 15:26:53 -0700 | [diff] [blame] | 597 | tcf_queue_work(&head->rwork, fl_destroy_sleepable); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 598 | } |
| 599 | |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 600 | static void fl_put(struct tcf_proto *tp, void *arg) |
| 601 | { |
| 602 | struct cls_fl_filter *f = arg; |
| 603 | |
| 604 | __fl_put(f); |
| 605 | } |
| 606 | |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 607 | static void *fl_get(struct tcf_proto *tp, u32 handle) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 608 | { |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame] | 609 | struct cls_fl_head *head = fl_head_dereference(tp); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 610 | |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 611 | return __fl_get(head, handle); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = { |
| 615 | [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC }, |
| 616 | [TCA_FLOWER_CLASSID] = { .type = NLA_U32 }, |
| 617 | [TCA_FLOWER_INDEV] = { .type = NLA_STRING, |
| 618 | .len = IFNAMSIZ }, |
| 619 | [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN }, |
| 620 | [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN }, |
| 621 | [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN }, |
| 622 | [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN }, |
| 623 | [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 }, |
| 624 | [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 }, |
| 625 | [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 }, |
| 626 | [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 }, |
| 627 | [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 }, |
| 628 | [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 }, |
| 629 | [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, |
| 630 | [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, |
| 631 | [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) }, |
| 632 | [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, |
| 633 | [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 }, |
| 634 | [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 }, |
Jamal Hadi Salim | b175c3a | 2015-06-25 06:55:27 -0400 | [diff] [blame] | 635 | [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 }, |
| 636 | [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 }, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 637 | [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 }, |
| 638 | [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 }, |
| 639 | [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 }, |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 640 | [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 }, |
| 641 | [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 }, |
| 642 | [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 }, |
| 643 | [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 }, |
| 644 | [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 }, |
| 645 | [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) }, |
| 646 | [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) }, |
| 647 | [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) }, |
| 648 | [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) }, |
Or Gerlitz | aa72d70 | 2016-09-15 15:28:22 +0300 | [diff] [blame] | 649 | [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 }, |
| 650 | [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 }, |
| 651 | [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 }, |
| 652 | [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 }, |
Simon Horman | 5976c5f | 2016-11-03 13:24:21 +0100 | [diff] [blame] | 653 | [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 }, |
| 654 | [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 }, |
| 655 | [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 }, |
| 656 | [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 }, |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 657 | [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 }, |
| 658 | [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 }, |
| 659 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 }, |
| 660 | [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 }, |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 661 | [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 }, |
| 662 | [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 }, |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 663 | [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 }, |
| 664 | [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 }, |
| 665 | [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 }, |
| 666 | [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 }, |
| 667 | [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 }, |
| 668 | [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 }, |
| 669 | [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 }, |
| 670 | [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 }, |
Simon Horman | 99d3132 | 2017-01-11 14:05:43 +0100 | [diff] [blame] | 671 | [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 }, |
| 672 | [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 }, |
| 673 | [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 }, |
| 674 | [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 }, |
| 675 | [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 }, |
| 676 | [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 }, |
| 677 | [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN }, |
| 678 | [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN }, |
| 679 | [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN }, |
| 680 | [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN }, |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 681 | [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 }, |
| 682 | [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 }, |
| 683 | [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 }, |
| 684 | [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 }, |
Guillaume Nault | 61aec25 | 2020-05-26 14:29:04 +0200 | [diff] [blame] | 685 | [TCA_FLOWER_KEY_MPLS_OPTS] = { .type = NLA_NESTED }, |
Jiri Pirko | fdfc7dd | 2017-05-23 18:40:45 +0200 | [diff] [blame] | 686 | [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 }, |
| 687 | [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 }, |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 688 | [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 }, |
| 689 | [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 }, |
| 690 | [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 }, |
| 691 | [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 }, |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 692 | [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 }, |
| 693 | [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 }, |
| 694 | [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 }, |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 695 | [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 }, |
| 696 | [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 }, |
| 697 | [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 }, |
| 698 | [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 }, |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 699 | [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED }, |
| 700 | [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED }, |
wenxu | 1bcc51a | 2021-02-09 14:37:49 +0800 | [diff] [blame] | 701 | [TCA_FLOWER_KEY_CT_STATE] = |
| 702 | NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK), |
| 703 | [TCA_FLOWER_KEY_CT_STATE_MASK] = |
| 704 | NLA_POLICY_MASK(NLA_U16, TCA_FLOWER_KEY_CT_FLAGS_MASK), |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 705 | [TCA_FLOWER_KEY_CT_ZONE] = { .type = NLA_U16 }, |
| 706 | [TCA_FLOWER_KEY_CT_ZONE_MASK] = { .type = NLA_U16 }, |
| 707 | [TCA_FLOWER_KEY_CT_MARK] = { .type = NLA_U32 }, |
| 708 | [TCA_FLOWER_KEY_CT_MARK_MASK] = { .type = NLA_U32 }, |
| 709 | [TCA_FLOWER_KEY_CT_LABELS] = { .type = NLA_BINARY, |
| 710 | .len = 128 / BITS_PER_BYTE }, |
| 711 | [TCA_FLOWER_KEY_CT_LABELS_MASK] = { .type = NLA_BINARY, |
| 712 | .len = 128 / BITS_PER_BYTE }, |
Davide Caratti | e2debf0 | 2020-02-11 19:33:40 +0100 | [diff] [blame] | 713 | [TCA_FLOWER_FLAGS] = { .type = NLA_U32 }, |
Ariel Levkovich | 5923b8f | 2020-07-23 01:03:01 +0300 | [diff] [blame] | 714 | [TCA_FLOWER_KEY_HASH] = { .type = NLA_U32 }, |
| 715 | [TCA_FLOWER_KEY_HASH_MASK] = { .type = NLA_U32 }, |
| 716 | |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 717 | }; |
| 718 | |
| 719 | static const struct nla_policy |
| 720 | enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = { |
Xin Long | d8f9dfa | 2019-11-21 18:03:28 +0800 | [diff] [blame] | 721 | [TCA_FLOWER_KEY_ENC_OPTS_UNSPEC] = { |
| 722 | .strict_start_type = TCA_FLOWER_KEY_ENC_OPTS_VXLAN }, |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 723 | [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED }, |
Xin Long | d8f9dfa | 2019-11-21 18:03:28 +0800 | [diff] [blame] | 724 | [TCA_FLOWER_KEY_ENC_OPTS_VXLAN] = { .type = NLA_NESTED }, |
Xin Long | 79b1011 | 2019-11-21 18:03:29 +0800 | [diff] [blame] | 725 | [TCA_FLOWER_KEY_ENC_OPTS_ERSPAN] = { .type = NLA_NESTED }, |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 726 | }; |
| 727 | |
| 728 | static const struct nla_policy |
| 729 | geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = { |
| 730 | [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 }, |
| 731 | [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 }, |
| 732 | [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY, |
| 733 | .len = 128 }, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 734 | }; |
| 735 | |
Xin Long | d8f9dfa | 2019-11-21 18:03:28 +0800 | [diff] [blame] | 736 | static const struct nla_policy |
| 737 | vxlan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1] = { |
| 738 | [TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP] = { .type = NLA_U32 }, |
| 739 | }; |
| 740 | |
Xin Long | 79b1011 | 2019-11-21 18:03:29 +0800 | [diff] [blame] | 741 | static const struct nla_policy |
| 742 | erspan_opt_policy[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1] = { |
| 743 | [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER] = { .type = NLA_U8 }, |
| 744 | [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX] = { .type = NLA_U32 }, |
| 745 | [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] = { .type = NLA_U8 }, |
| 746 | [TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID] = { .type = NLA_U8 }, |
| 747 | }; |
| 748 | |
Guillaume Nault | 61aec25 | 2020-05-26 14:29:04 +0200 | [diff] [blame] | 749 | static const struct nla_policy |
Guillaume Nault | 61aec25 | 2020-05-26 14:29:04 +0200 | [diff] [blame] | 750 | mpls_stack_entry_policy[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1] = { |
| 751 | [TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH] = { .type = NLA_U8 }, |
| 752 | [TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL] = { .type = NLA_U8 }, |
| 753 | [TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS] = { .type = NLA_U8 }, |
| 754 | [TCA_FLOWER_KEY_MPLS_OPT_LSE_TC] = { .type = NLA_U8 }, |
| 755 | [TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL] = { .type = NLA_U32 }, |
| 756 | }; |
| 757 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 758 | static void fl_set_key_val(struct nlattr **tb, |
| 759 | void *val, int val_type, |
| 760 | void *mask, int mask_type, int len) |
| 761 | { |
| 762 | if (!tb[val_type]) |
| 763 | return; |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 764 | nla_memcpy(val, tb[val_type], len); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 765 | if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type]) |
| 766 | memset(mask, 0xff, len); |
| 767 | else |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 768 | nla_memcpy(mask, tb[mask_type], len); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 769 | } |
| 770 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 771 | static int fl_set_key_port_range(struct nlattr **tb, struct fl_flow_key *key, |
Guillaume Nault | bd7d4c1 | 2020-03-23 21:48:51 +0100 | [diff] [blame] | 772 | struct fl_flow_key *mask, |
| 773 | struct netlink_ext_ack *extack) |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 774 | { |
Yoshiki Komachi | 8ffb055 | 2019-12-03 19:40:12 +0900 | [diff] [blame] | 775 | fl_set_key_val(tb, &key->tp_range.tp_min.dst, |
| 776 | TCA_FLOWER_KEY_PORT_DST_MIN, &mask->tp_range.tp_min.dst, |
| 777 | TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.dst)); |
| 778 | fl_set_key_val(tb, &key->tp_range.tp_max.dst, |
| 779 | TCA_FLOWER_KEY_PORT_DST_MAX, &mask->tp_range.tp_max.dst, |
| 780 | TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.dst)); |
| 781 | fl_set_key_val(tb, &key->tp_range.tp_min.src, |
| 782 | TCA_FLOWER_KEY_PORT_SRC_MIN, &mask->tp_range.tp_min.src, |
| 783 | TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_min.src)); |
| 784 | fl_set_key_val(tb, &key->tp_range.tp_max.src, |
| 785 | TCA_FLOWER_KEY_PORT_SRC_MAX, &mask->tp_range.tp_max.src, |
| 786 | TCA_FLOWER_UNSPEC, sizeof(key->tp_range.tp_max.src)); |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 787 | |
Guillaume Nault | bd7d4c1 | 2020-03-23 21:48:51 +0100 | [diff] [blame] | 788 | if (mask->tp_range.tp_min.dst && mask->tp_range.tp_max.dst && |
Vladimir Oltean | 6215afc | 2021-03-21 23:05:48 +0200 | [diff] [blame] | 789 | ntohs(key->tp_range.tp_max.dst) <= |
| 790 | ntohs(key->tp_range.tp_min.dst)) { |
Guillaume Nault | bd7d4c1 | 2020-03-23 21:48:51 +0100 | [diff] [blame] | 791 | NL_SET_ERR_MSG_ATTR(extack, |
| 792 | tb[TCA_FLOWER_KEY_PORT_DST_MIN], |
| 793 | "Invalid destination port range (min must be strictly smaller than max)"); |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 794 | return -EINVAL; |
Guillaume Nault | bd7d4c1 | 2020-03-23 21:48:51 +0100 | [diff] [blame] | 795 | } |
| 796 | if (mask->tp_range.tp_min.src && mask->tp_range.tp_max.src && |
Vladimir Oltean | 6215afc | 2021-03-21 23:05:48 +0200 | [diff] [blame] | 797 | ntohs(key->tp_range.tp_max.src) <= |
| 798 | ntohs(key->tp_range.tp_min.src)) { |
Guillaume Nault | bd7d4c1 | 2020-03-23 21:48:51 +0100 | [diff] [blame] | 799 | NL_SET_ERR_MSG_ATTR(extack, |
| 800 | tb[TCA_FLOWER_KEY_PORT_SRC_MIN], |
| 801 | "Invalid source port range (min must be strictly smaller than max)"); |
| 802 | return -EINVAL; |
| 803 | } |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 804 | |
| 805 | return 0; |
| 806 | } |
| 807 | |
Guillaume Nault | 61aec25 | 2020-05-26 14:29:04 +0200 | [diff] [blame] | 808 | static int fl_set_key_mpls_lse(const struct nlattr *nla_lse, |
| 809 | struct flow_dissector_key_mpls *key_val, |
| 810 | struct flow_dissector_key_mpls *key_mask, |
| 811 | struct netlink_ext_ack *extack) |
| 812 | { |
| 813 | struct nlattr *tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX + 1]; |
| 814 | struct flow_dissector_mpls_lse *lse_mask; |
| 815 | struct flow_dissector_mpls_lse *lse_val; |
| 816 | u8 lse_index; |
| 817 | u8 depth; |
| 818 | int err; |
| 819 | |
| 820 | err = nla_parse_nested(tb, TCA_FLOWER_KEY_MPLS_OPT_LSE_MAX, nla_lse, |
| 821 | mpls_stack_entry_policy, extack); |
| 822 | if (err < 0) |
| 823 | return err; |
| 824 | |
| 825 | if (!tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]) { |
| 826 | NL_SET_ERR_MSG(extack, "Missing MPLS option \"depth\""); |
| 827 | return -EINVAL; |
| 828 | } |
| 829 | |
| 830 | depth = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH]); |
| 831 | |
| 832 | /* LSE depth starts at 1, for consistency with terminology used by |
| 833 | * RFC 3031 (section 3.9), where depth 0 refers to unlabeled packets. |
| 834 | */ |
| 835 | if (depth < 1 || depth > FLOW_DIS_MPLS_MAX) { |
| 836 | NL_SET_ERR_MSG_ATTR(extack, |
| 837 | tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH], |
| 838 | "Invalid MPLS depth"); |
| 839 | return -EINVAL; |
| 840 | } |
| 841 | lse_index = depth - 1; |
| 842 | |
| 843 | dissector_set_mpls_lse(key_val, lse_index); |
| 844 | dissector_set_mpls_lse(key_mask, lse_index); |
| 845 | |
| 846 | lse_val = &key_val->ls[lse_index]; |
| 847 | lse_mask = &key_mask->ls[lse_index]; |
| 848 | |
| 849 | if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]) { |
| 850 | lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL]); |
| 851 | lse_mask->mpls_ttl = MPLS_TTL_MASK; |
| 852 | } |
| 853 | if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]) { |
| 854 | u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS]); |
| 855 | |
| 856 | if (bos & ~MPLS_BOS_MASK) { |
| 857 | NL_SET_ERR_MSG_ATTR(extack, |
| 858 | tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS], |
| 859 | "Bottom Of Stack (BOS) must be 0 or 1"); |
| 860 | return -EINVAL; |
| 861 | } |
| 862 | lse_val->mpls_bos = bos; |
| 863 | lse_mask->mpls_bos = MPLS_BOS_MASK; |
| 864 | } |
| 865 | if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]) { |
| 866 | u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC]); |
| 867 | |
| 868 | if (tc & ~MPLS_TC_MASK) { |
| 869 | NL_SET_ERR_MSG_ATTR(extack, |
| 870 | tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_TC], |
| 871 | "Traffic Class (TC) must be between 0 and 7"); |
| 872 | return -EINVAL; |
| 873 | } |
| 874 | lse_val->mpls_tc = tc; |
| 875 | lse_mask->mpls_tc = MPLS_TC_MASK; |
| 876 | } |
| 877 | if (tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]) { |
| 878 | u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL]); |
| 879 | |
| 880 | if (label & ~MPLS_LABEL_MASK) { |
| 881 | NL_SET_ERR_MSG_ATTR(extack, |
| 882 | tb[TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL], |
| 883 | "Label must be between 0 and 1048575"); |
| 884 | return -EINVAL; |
| 885 | } |
| 886 | lse_val->mpls_label = label; |
| 887 | lse_mask->mpls_label = MPLS_LABEL_MASK; |
| 888 | } |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
| 893 | static int fl_set_key_mpls_opts(const struct nlattr *nla_mpls_opts, |
| 894 | struct flow_dissector_key_mpls *key_val, |
| 895 | struct flow_dissector_key_mpls *key_mask, |
| 896 | struct netlink_ext_ack *extack) |
| 897 | { |
| 898 | struct nlattr *nla_lse; |
| 899 | int rem; |
| 900 | int err; |
| 901 | |
| 902 | if (!(nla_mpls_opts->nla_type & NLA_F_NESTED)) { |
| 903 | NL_SET_ERR_MSG_ATTR(extack, nla_mpls_opts, |
| 904 | "NLA_F_NESTED is missing"); |
| 905 | return -EINVAL; |
| 906 | } |
| 907 | |
| 908 | nla_for_each_nested(nla_lse, nla_mpls_opts, rem) { |
| 909 | if (nla_type(nla_lse) != TCA_FLOWER_KEY_MPLS_OPTS_LSE) { |
| 910 | NL_SET_ERR_MSG_ATTR(extack, nla_lse, |
| 911 | "Invalid MPLS option type"); |
| 912 | return -EINVAL; |
| 913 | } |
| 914 | |
| 915 | err = fl_set_key_mpls_lse(nla_lse, key_val, key_mask, extack); |
| 916 | if (err < 0) |
| 917 | return err; |
| 918 | } |
| 919 | if (rem) { |
| 920 | NL_SET_ERR_MSG(extack, |
| 921 | "Bytes leftover after parsing MPLS options"); |
| 922 | return -EINVAL; |
| 923 | } |
| 924 | |
| 925 | return 0; |
| 926 | } |
| 927 | |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 928 | static int fl_set_key_mpls(struct nlattr **tb, |
| 929 | struct flow_dissector_key_mpls *key_val, |
Guillaume Nault | 442f730 | 2020-03-23 21:48:49 +0100 | [diff] [blame] | 930 | struct flow_dissector_key_mpls *key_mask, |
| 931 | struct netlink_ext_ack *extack) |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 932 | { |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 933 | struct flow_dissector_mpls_lse *lse_mask; |
| 934 | struct flow_dissector_mpls_lse *lse_val; |
| 935 | |
Guillaume Nault | 61aec25 | 2020-05-26 14:29:04 +0200 | [diff] [blame] | 936 | if (tb[TCA_FLOWER_KEY_MPLS_OPTS]) { |
| 937 | if (tb[TCA_FLOWER_KEY_MPLS_TTL] || |
| 938 | tb[TCA_FLOWER_KEY_MPLS_BOS] || |
| 939 | tb[TCA_FLOWER_KEY_MPLS_TC] || |
| 940 | tb[TCA_FLOWER_KEY_MPLS_LABEL]) { |
| 941 | NL_SET_ERR_MSG_ATTR(extack, |
| 942 | tb[TCA_FLOWER_KEY_MPLS_OPTS], |
| 943 | "MPLS label, Traffic Class, Bottom Of Stack and Time To Live must be encapsulated in the MPLS options attribute"); |
| 944 | return -EBADMSG; |
| 945 | } |
| 946 | |
| 947 | return fl_set_key_mpls_opts(tb[TCA_FLOWER_KEY_MPLS_OPTS], |
| 948 | key_val, key_mask, extack); |
| 949 | } |
| 950 | |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 951 | lse_val = &key_val->ls[0]; |
| 952 | lse_mask = &key_mask->ls[0]; |
| 953 | |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 954 | if (tb[TCA_FLOWER_KEY_MPLS_TTL]) { |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 955 | lse_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]); |
| 956 | lse_mask->mpls_ttl = MPLS_TTL_MASK; |
| 957 | dissector_set_mpls_lse(key_val, 0); |
| 958 | dissector_set_mpls_lse(key_mask, 0); |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 959 | } |
| 960 | if (tb[TCA_FLOWER_KEY_MPLS_BOS]) { |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 961 | u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]); |
| 962 | |
Guillaume Nault | 442f730 | 2020-03-23 21:48:49 +0100 | [diff] [blame] | 963 | if (bos & ~MPLS_BOS_MASK) { |
| 964 | NL_SET_ERR_MSG_ATTR(extack, |
| 965 | tb[TCA_FLOWER_KEY_MPLS_BOS], |
| 966 | "Bottom Of Stack (BOS) must be 0 or 1"); |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 967 | return -EINVAL; |
Guillaume Nault | 442f730 | 2020-03-23 21:48:49 +0100 | [diff] [blame] | 968 | } |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 969 | lse_val->mpls_bos = bos; |
| 970 | lse_mask->mpls_bos = MPLS_BOS_MASK; |
| 971 | dissector_set_mpls_lse(key_val, 0); |
| 972 | dissector_set_mpls_lse(key_mask, 0); |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 973 | } |
| 974 | if (tb[TCA_FLOWER_KEY_MPLS_TC]) { |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 975 | u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]); |
| 976 | |
Guillaume Nault | 442f730 | 2020-03-23 21:48:49 +0100 | [diff] [blame] | 977 | if (tc & ~MPLS_TC_MASK) { |
| 978 | NL_SET_ERR_MSG_ATTR(extack, |
| 979 | tb[TCA_FLOWER_KEY_MPLS_TC], |
| 980 | "Traffic Class (TC) must be between 0 and 7"); |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 981 | return -EINVAL; |
Guillaume Nault | 442f730 | 2020-03-23 21:48:49 +0100 | [diff] [blame] | 982 | } |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 983 | lse_val->mpls_tc = tc; |
| 984 | lse_mask->mpls_tc = MPLS_TC_MASK; |
| 985 | dissector_set_mpls_lse(key_val, 0); |
| 986 | dissector_set_mpls_lse(key_mask, 0); |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 987 | } |
| 988 | if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) { |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 989 | u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]); |
| 990 | |
Guillaume Nault | 442f730 | 2020-03-23 21:48:49 +0100 | [diff] [blame] | 991 | if (label & ~MPLS_LABEL_MASK) { |
| 992 | NL_SET_ERR_MSG_ATTR(extack, |
| 993 | tb[TCA_FLOWER_KEY_MPLS_LABEL], |
| 994 | "Label must be between 0 and 1048575"); |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 995 | return -EINVAL; |
Guillaume Nault | 442f730 | 2020-03-23 21:48:49 +0100 | [diff] [blame] | 996 | } |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 997 | lse_val->mpls_label = label; |
| 998 | lse_mask->mpls_label = MPLS_LABEL_MASK; |
| 999 | dissector_set_mpls_lse(key_val, 0); |
| 1000 | dissector_set_mpls_lse(key_mask, 0); |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 1001 | } |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 1002 | return 0; |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 1003 | } |
| 1004 | |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1005 | static void fl_set_key_vlan(struct nlattr **tb, |
Jianbo Liu | aaab083 | 2018-07-06 05:38:13 +0000 | [diff] [blame] | 1006 | __be16 ethertype, |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1007 | int vlan_id_key, int vlan_prio_key, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1008 | struct flow_dissector_key_vlan *key_val, |
| 1009 | struct flow_dissector_key_vlan *key_mask) |
| 1010 | { |
| 1011 | #define VLAN_PRIORITY_MASK 0x7 |
| 1012 | |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1013 | if (tb[vlan_id_key]) { |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1014 | key_val->vlan_id = |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1015 | nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK; |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1016 | key_mask->vlan_id = VLAN_VID_MASK; |
| 1017 | } |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1018 | if (tb[vlan_prio_key]) { |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1019 | key_val->vlan_priority = |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1020 | nla_get_u8(tb[vlan_prio_key]) & |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1021 | VLAN_PRIORITY_MASK; |
| 1022 | key_mask->vlan_priority = VLAN_PRIORITY_MASK; |
| 1023 | } |
Jianbo Liu | aaab083 | 2018-07-06 05:38:13 +0000 | [diff] [blame] | 1024 | key_val->vlan_tpid = ethertype; |
| 1025 | key_mask->vlan_tpid = cpu_to_be16(~0); |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1026 | } |
| 1027 | |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 1028 | static void fl_set_key_flag(u32 flower_key, u32 flower_mask, |
| 1029 | u32 *dissector_key, u32 *dissector_mask, |
| 1030 | u32 flower_flag_bit, u32 dissector_flag_bit) |
| 1031 | { |
| 1032 | if (flower_mask & flower_flag_bit) { |
| 1033 | *dissector_mask |= dissector_flag_bit; |
| 1034 | if (flower_key & flower_flag_bit) |
| 1035 | *dissector_key |= dissector_flag_bit; |
| 1036 | } |
| 1037 | } |
| 1038 | |
Guillaume Nault | e304e21 | 2020-03-23 21:48:53 +0100 | [diff] [blame] | 1039 | static int fl_set_key_flags(struct nlattr **tb, u32 *flags_key, |
| 1040 | u32 *flags_mask, struct netlink_ext_ack *extack) |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 1041 | { |
| 1042 | u32 key, mask; |
| 1043 | |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 1044 | /* mask is mandatory for flags */ |
Guillaume Nault | e304e21 | 2020-03-23 21:48:53 +0100 | [diff] [blame] | 1045 | if (!tb[TCA_FLOWER_KEY_FLAGS_MASK]) { |
| 1046 | NL_SET_ERR_MSG(extack, "Missing flags mask"); |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 1047 | return -EINVAL; |
Guillaume Nault | e304e21 | 2020-03-23 21:48:53 +0100 | [diff] [blame] | 1048 | } |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 1049 | |
Vladimir Oltean | abee13f | 2021-03-21 23:05:49 +0200 | [diff] [blame] | 1050 | key = be32_to_cpu(nla_get_be32(tb[TCA_FLOWER_KEY_FLAGS])); |
| 1051 | mask = be32_to_cpu(nla_get_be32(tb[TCA_FLOWER_KEY_FLAGS_MASK])); |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 1052 | |
| 1053 | *flags_key = 0; |
| 1054 | *flags_mask = 0; |
| 1055 | |
| 1056 | fl_set_key_flag(key, mask, flags_key, flags_mask, |
| 1057 | TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); |
Pieter Jansen van Vuuren | 459d153 | 2018-03-06 18:11:14 +0100 | [diff] [blame] | 1058 | fl_set_key_flag(key, mask, flags_key, flags_mask, |
| 1059 | TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, |
| 1060 | FLOW_DIS_FIRST_FRAG); |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 1061 | |
| 1062 | return 0; |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 1063 | } |
| 1064 | |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 1065 | static void fl_set_key_ip(struct nlattr **tb, bool encap, |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 1066 | struct flow_dissector_key_ip *key, |
| 1067 | struct flow_dissector_key_ip *mask) |
| 1068 | { |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 1069 | int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS; |
| 1070 | int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL; |
| 1071 | int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK; |
| 1072 | 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] | 1073 | |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 1074 | fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)); |
| 1075 | 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] | 1076 | } |
| 1077 | |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1078 | static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key, |
| 1079 | int depth, int option_len, |
| 1080 | struct netlink_ext_ack *extack) |
| 1081 | { |
| 1082 | struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1]; |
| 1083 | struct nlattr *class = NULL, *type = NULL, *data = NULL; |
| 1084 | struct geneve_opt *opt; |
| 1085 | int err, data_len = 0; |
| 1086 | |
| 1087 | if (option_len > sizeof(struct geneve_opt)) |
| 1088 | data_len = option_len - sizeof(struct geneve_opt); |
| 1089 | |
| 1090 | opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len]; |
| 1091 | memset(opt, 0xff, option_len); |
| 1092 | opt->length = data_len / 4; |
| 1093 | opt->r1 = 0; |
| 1094 | opt->r2 = 0; |
| 1095 | opt->r3 = 0; |
| 1096 | |
| 1097 | /* If no mask has been prodived we assume an exact match. */ |
| 1098 | if (!depth) |
| 1099 | return sizeof(struct geneve_opt) + data_len; |
| 1100 | |
| 1101 | if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) { |
| 1102 | NL_SET_ERR_MSG(extack, "Non-geneve option type for mask"); |
| 1103 | return -EINVAL; |
| 1104 | } |
| 1105 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1106 | err = nla_parse_nested_deprecated(tb, |
| 1107 | TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX, |
| 1108 | nla, geneve_opt_policy, extack); |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1109 | if (err < 0) |
| 1110 | return err; |
| 1111 | |
| 1112 | /* We are not allowed to omit any of CLASS, TYPE or DATA |
| 1113 | * fields from the key. |
| 1114 | */ |
| 1115 | if (!option_len && |
| 1116 | (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] || |
| 1117 | !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] || |
| 1118 | !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) { |
| 1119 | NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data"); |
| 1120 | return -EINVAL; |
| 1121 | } |
| 1122 | |
| 1123 | /* Omitting any of CLASS, TYPE or DATA fields is allowed |
| 1124 | * for the mask. |
| 1125 | */ |
| 1126 | if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) { |
| 1127 | int new_len = key->enc_opts.len; |
| 1128 | |
| 1129 | data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]; |
| 1130 | data_len = nla_len(data); |
| 1131 | if (data_len < 4) { |
| 1132 | NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long"); |
| 1133 | return -ERANGE; |
| 1134 | } |
| 1135 | if (data_len % 4) { |
| 1136 | NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long"); |
| 1137 | return -ERANGE; |
| 1138 | } |
| 1139 | |
| 1140 | new_len += sizeof(struct geneve_opt) + data_len; |
| 1141 | BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX); |
| 1142 | if (new_len > FLOW_DIS_TUN_OPTS_MAX) { |
| 1143 | NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size"); |
| 1144 | return -ERANGE; |
| 1145 | } |
| 1146 | opt->length = data_len / 4; |
| 1147 | memcpy(opt->opt_data, nla_data(data), data_len); |
| 1148 | } |
| 1149 | |
| 1150 | if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) { |
| 1151 | class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]; |
| 1152 | opt->opt_class = nla_get_be16(class); |
| 1153 | } |
| 1154 | |
| 1155 | if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) { |
| 1156 | type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]; |
| 1157 | opt->type = nla_get_u8(type); |
| 1158 | } |
| 1159 | |
| 1160 | return sizeof(struct geneve_opt) + data_len; |
| 1161 | } |
| 1162 | |
Xin Long | d8f9dfa | 2019-11-21 18:03:28 +0800 | [diff] [blame] | 1163 | static int fl_set_vxlan_opt(const struct nlattr *nla, struct fl_flow_key *key, |
| 1164 | int depth, int option_len, |
| 1165 | struct netlink_ext_ack *extack) |
| 1166 | { |
| 1167 | struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX + 1]; |
| 1168 | struct vxlan_metadata *md; |
| 1169 | int err; |
| 1170 | |
| 1171 | md = (struct vxlan_metadata *)&key->enc_opts.data[key->enc_opts.len]; |
| 1172 | memset(md, 0xff, sizeof(*md)); |
| 1173 | |
| 1174 | if (!depth) |
| 1175 | return sizeof(*md); |
| 1176 | |
| 1177 | if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_VXLAN) { |
| 1178 | NL_SET_ERR_MSG(extack, "Non-vxlan option type for mask"); |
| 1179 | return -EINVAL; |
| 1180 | } |
| 1181 | |
| 1182 | err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_MAX, nla, |
| 1183 | vxlan_opt_policy, extack); |
| 1184 | if (err < 0) |
| 1185 | return err; |
| 1186 | |
| 1187 | if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) { |
| 1188 | NL_SET_ERR_MSG(extack, "Missing tunnel key vxlan option gbp"); |
| 1189 | return -EINVAL; |
| 1190 | } |
| 1191 | |
Xin Long | 13e6ce9 | 2020-09-13 19:51:50 +0800 | [diff] [blame] | 1192 | if (tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]) { |
Xin Long | d8f9dfa | 2019-11-21 18:03:28 +0800 | [diff] [blame] | 1193 | md->gbp = nla_get_u32(tb[TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP]); |
Xin Long | 13e6ce9 | 2020-09-13 19:51:50 +0800 | [diff] [blame] | 1194 | md->gbp &= VXLAN_GBP_MASK; |
| 1195 | } |
Xin Long | d8f9dfa | 2019-11-21 18:03:28 +0800 | [diff] [blame] | 1196 | |
| 1197 | return sizeof(*md); |
| 1198 | } |
| 1199 | |
Xin Long | 79b1011 | 2019-11-21 18:03:29 +0800 | [diff] [blame] | 1200 | static int fl_set_erspan_opt(const struct nlattr *nla, struct fl_flow_key *key, |
| 1201 | int depth, int option_len, |
| 1202 | struct netlink_ext_ack *extack) |
| 1203 | { |
| 1204 | struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX + 1]; |
| 1205 | struct erspan_metadata *md; |
| 1206 | int err; |
| 1207 | |
| 1208 | md = (struct erspan_metadata *)&key->enc_opts.data[key->enc_opts.len]; |
| 1209 | memset(md, 0xff, sizeof(*md)); |
| 1210 | md->version = 1; |
| 1211 | |
| 1212 | if (!depth) |
| 1213 | return sizeof(*md); |
| 1214 | |
| 1215 | if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_ERSPAN) { |
| 1216 | NL_SET_ERR_MSG(extack, "Non-erspan option type for mask"); |
| 1217 | return -EINVAL; |
| 1218 | } |
| 1219 | |
| 1220 | err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_MAX, nla, |
| 1221 | erspan_opt_policy, extack); |
| 1222 | if (err < 0) |
| 1223 | return err; |
| 1224 | |
| 1225 | if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) { |
| 1226 | NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option ver"); |
| 1227 | return -EINVAL; |
| 1228 | } |
| 1229 | |
| 1230 | if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]) |
| 1231 | md->version = nla_get_u8(tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER]); |
| 1232 | |
| 1233 | if (md->version == 1) { |
| 1234 | if (!option_len && !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) { |
| 1235 | NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option index"); |
| 1236 | return -EINVAL; |
| 1237 | } |
| 1238 | if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]) { |
| 1239 | nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX]; |
Xin Long | 8e1b3ac | 2020-09-13 19:43:03 +0800 | [diff] [blame] | 1240 | memset(&md->u, 0x00, sizeof(md->u)); |
Xin Long | 79b1011 | 2019-11-21 18:03:29 +0800 | [diff] [blame] | 1241 | md->u.index = nla_get_be32(nla); |
| 1242 | } |
| 1243 | } else if (md->version == 2) { |
| 1244 | if (!option_len && (!tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR] || |
| 1245 | !tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID])) { |
| 1246 | NL_SET_ERR_MSG(extack, "Missing tunnel key erspan option dir or hwid"); |
| 1247 | return -EINVAL; |
| 1248 | } |
| 1249 | if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]) { |
| 1250 | nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR]; |
| 1251 | md->u.md2.dir = nla_get_u8(nla); |
| 1252 | } |
| 1253 | if (tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]) { |
| 1254 | nla = tb[TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID]; |
| 1255 | set_hwid(&md->u.md2, nla_get_u8(nla)); |
| 1256 | } |
| 1257 | } else { |
| 1258 | NL_SET_ERR_MSG(extack, "Tunnel key erspan option ver is incorrect"); |
| 1259 | return -EINVAL; |
| 1260 | } |
| 1261 | |
| 1262 | return sizeof(*md); |
| 1263 | } |
| 1264 | |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1265 | static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key, |
| 1266 | struct fl_flow_key *mask, |
| 1267 | struct netlink_ext_ack *extack) |
| 1268 | { |
| 1269 | 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] | 1270 | int err, option_len, key_depth, msk_depth = 0; |
| 1271 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1272 | err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS], |
| 1273 | TCA_FLOWER_KEY_ENC_OPTS_MAX, |
| 1274 | enc_opts_policy, extack); |
Jakub Kicinski | 63c8299 | 2018-11-09 21:06:26 -0800 | [diff] [blame] | 1275 | if (err) |
| 1276 | return err; |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1277 | |
| 1278 | nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]); |
| 1279 | |
| 1280 | if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) { |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 1281 | err = nla_validate_nested_deprecated(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK], |
| 1282 | TCA_FLOWER_KEY_ENC_OPTS_MAX, |
| 1283 | enc_opts_policy, extack); |
Jakub Kicinski | 63c8299 | 2018-11-09 21:06:26 -0800 | [diff] [blame] | 1284 | if (err) |
| 1285 | return err; |
| 1286 | |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1287 | nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); |
| 1288 | msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]); |
Cong Wang | c96adff | 2021-01-15 10:50:24 -0800 | [diff] [blame] | 1289 | if (!nla_ok(nla_opt_msk, msk_depth)) { |
| 1290 | NL_SET_ERR_MSG(extack, "Invalid nested attribute for masks"); |
| 1291 | return -EINVAL; |
| 1292 | } |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | nla_for_each_attr(nla_opt_key, nla_enc_key, |
| 1296 | nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) { |
| 1297 | switch (nla_type(nla_opt_key)) { |
| 1298 | case TCA_FLOWER_KEY_ENC_OPTS_GENEVE: |
Xin Long | d8f9dfa | 2019-11-21 18:03:28 +0800 | [diff] [blame] | 1299 | if (key->enc_opts.dst_opt_type && |
| 1300 | key->enc_opts.dst_opt_type != TUNNEL_GENEVE_OPT) { |
| 1301 | NL_SET_ERR_MSG(extack, "Duplicate type for geneve options"); |
| 1302 | return -EINVAL; |
| 1303 | } |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1304 | option_len = 0; |
| 1305 | key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT; |
| 1306 | option_len = fl_set_geneve_opt(nla_opt_key, key, |
| 1307 | key_depth, option_len, |
| 1308 | extack); |
| 1309 | if (option_len < 0) |
| 1310 | return option_len; |
| 1311 | |
| 1312 | key->enc_opts.len += option_len; |
| 1313 | /* At the same time we need to parse through the mask |
| 1314 | * in order to verify exact and mask attribute lengths. |
| 1315 | */ |
| 1316 | mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT; |
| 1317 | option_len = fl_set_geneve_opt(nla_opt_msk, mask, |
| 1318 | msk_depth, option_len, |
| 1319 | extack); |
| 1320 | if (option_len < 0) |
| 1321 | return option_len; |
| 1322 | |
| 1323 | mask->enc_opts.len += option_len; |
| 1324 | if (key->enc_opts.len != mask->enc_opts.len) { |
| 1325 | NL_SET_ERR_MSG(extack, "Key and mask miss aligned"); |
| 1326 | return -EINVAL; |
| 1327 | } |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1328 | break; |
Xin Long | d8f9dfa | 2019-11-21 18:03:28 +0800 | [diff] [blame] | 1329 | case TCA_FLOWER_KEY_ENC_OPTS_VXLAN: |
| 1330 | if (key->enc_opts.dst_opt_type) { |
| 1331 | NL_SET_ERR_MSG(extack, "Duplicate type for vxlan options"); |
| 1332 | return -EINVAL; |
| 1333 | } |
| 1334 | option_len = 0; |
| 1335 | key->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT; |
| 1336 | option_len = fl_set_vxlan_opt(nla_opt_key, key, |
| 1337 | key_depth, option_len, |
| 1338 | extack); |
| 1339 | if (option_len < 0) |
| 1340 | return option_len; |
| 1341 | |
| 1342 | key->enc_opts.len += option_len; |
| 1343 | /* At the same time we need to parse through the mask |
| 1344 | * in order to verify exact and mask attribute lengths. |
| 1345 | */ |
| 1346 | mask->enc_opts.dst_opt_type = TUNNEL_VXLAN_OPT; |
| 1347 | option_len = fl_set_vxlan_opt(nla_opt_msk, mask, |
| 1348 | msk_depth, option_len, |
| 1349 | extack); |
| 1350 | if (option_len < 0) |
| 1351 | return option_len; |
| 1352 | |
| 1353 | mask->enc_opts.len += option_len; |
| 1354 | if (key->enc_opts.len != mask->enc_opts.len) { |
| 1355 | NL_SET_ERR_MSG(extack, "Key and mask miss aligned"); |
| 1356 | return -EINVAL; |
| 1357 | } |
Xin Long | d8f9dfa | 2019-11-21 18:03:28 +0800 | [diff] [blame] | 1358 | break; |
Xin Long | 79b1011 | 2019-11-21 18:03:29 +0800 | [diff] [blame] | 1359 | case TCA_FLOWER_KEY_ENC_OPTS_ERSPAN: |
| 1360 | if (key->enc_opts.dst_opt_type) { |
| 1361 | NL_SET_ERR_MSG(extack, "Duplicate type for erspan options"); |
| 1362 | return -EINVAL; |
| 1363 | } |
| 1364 | option_len = 0; |
| 1365 | key->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT; |
| 1366 | option_len = fl_set_erspan_opt(nla_opt_key, key, |
| 1367 | key_depth, option_len, |
| 1368 | extack); |
| 1369 | if (option_len < 0) |
| 1370 | return option_len; |
| 1371 | |
| 1372 | key->enc_opts.len += option_len; |
| 1373 | /* At the same time we need to parse through the mask |
| 1374 | * in order to verify exact and mask attribute lengths. |
| 1375 | */ |
| 1376 | mask->enc_opts.dst_opt_type = TUNNEL_ERSPAN_OPT; |
| 1377 | option_len = fl_set_erspan_opt(nla_opt_msk, mask, |
| 1378 | msk_depth, option_len, |
| 1379 | extack); |
| 1380 | if (option_len < 0) |
| 1381 | return option_len; |
| 1382 | |
| 1383 | mask->enc_opts.len += option_len; |
| 1384 | if (key->enc_opts.len != mask->enc_opts.len) { |
| 1385 | NL_SET_ERR_MSG(extack, "Key and mask miss aligned"); |
| 1386 | return -EINVAL; |
| 1387 | } |
Xin Long | 79b1011 | 2019-11-21 18:03:29 +0800 | [diff] [blame] | 1388 | break; |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1389 | default: |
| 1390 | NL_SET_ERR_MSG(extack, "Unknown tunnel option type"); |
| 1391 | return -EINVAL; |
| 1392 | } |
Cong Wang | c96adff | 2021-01-15 10:50:24 -0800 | [diff] [blame] | 1393 | |
| 1394 | if (!msk_depth) |
| 1395 | continue; |
| 1396 | |
| 1397 | if (!nla_ok(nla_opt_msk, msk_depth)) { |
| 1398 | NL_SET_ERR_MSG(extack, "A mask attribute is invalid"); |
| 1399 | return -EINVAL; |
| 1400 | } |
| 1401 | nla_opt_msk = nla_next(nla_opt_msk, &msk_depth); |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
wenxu | 1bcc51a | 2021-02-09 14:37:49 +0800 | [diff] [blame] | 1407 | static int fl_validate_ct_state(u16 state, struct nlattr *tb, |
| 1408 | struct netlink_ext_ack *extack) |
| 1409 | { |
| 1410 | if (state && !(state & TCA_FLOWER_KEY_CT_FLAGS_TRACKED)) { |
| 1411 | NL_SET_ERR_MSG_ATTR(extack, tb, |
| 1412 | "no trk, so no other flag can be set"); |
| 1413 | return -EINVAL; |
| 1414 | } |
| 1415 | |
| 1416 | if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW && |
| 1417 | state & TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED) { |
| 1418 | NL_SET_ERR_MSG_ATTR(extack, tb, |
| 1419 | "new and est are mutually exclusive"); |
| 1420 | return -EINVAL; |
| 1421 | } |
| 1422 | |
wenxu | 3aed8b6 | 2021-02-23 15:11:55 +0800 | [diff] [blame] | 1423 | if (state & TCA_FLOWER_KEY_CT_FLAGS_INVALID && |
| 1424 | state & ~(TCA_FLOWER_KEY_CT_FLAGS_TRACKED | |
| 1425 | TCA_FLOWER_KEY_CT_FLAGS_INVALID)) { |
| 1426 | NL_SET_ERR_MSG_ATTR(extack, tb, |
| 1427 | "when inv is set, only trk may be set"); |
| 1428 | return -EINVAL; |
| 1429 | } |
| 1430 | |
| 1431 | if (state & TCA_FLOWER_KEY_CT_FLAGS_NEW && |
| 1432 | state & TCA_FLOWER_KEY_CT_FLAGS_REPLY) { |
| 1433 | NL_SET_ERR_MSG_ATTR(extack, tb, |
| 1434 | "new and rpl are mutually exclusive"); |
| 1435 | return -EINVAL; |
| 1436 | } |
| 1437 | |
wenxu | 1bcc51a | 2021-02-09 14:37:49 +0800 | [diff] [blame] | 1438 | return 0; |
| 1439 | } |
| 1440 | |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 1441 | static int fl_set_key_ct(struct nlattr **tb, |
| 1442 | struct flow_dissector_key_ct *key, |
| 1443 | struct flow_dissector_key_ct *mask, |
| 1444 | struct netlink_ext_ack *extack) |
| 1445 | { |
| 1446 | if (tb[TCA_FLOWER_KEY_CT_STATE]) { |
wenxu | 1bcc51a | 2021-02-09 14:37:49 +0800 | [diff] [blame] | 1447 | int err; |
| 1448 | |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 1449 | if (!IS_ENABLED(CONFIG_NF_CONNTRACK)) { |
| 1450 | NL_SET_ERR_MSG(extack, "Conntrack isn't enabled"); |
| 1451 | return -EOPNOTSUPP; |
| 1452 | } |
| 1453 | fl_set_key_val(tb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE, |
| 1454 | &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK, |
| 1455 | sizeof(key->ct_state)); |
wenxu | 1bcc51a | 2021-02-09 14:37:49 +0800 | [diff] [blame] | 1456 | |
wenxu | afa536d | 2021-03-17 12:02:43 +0800 | [diff] [blame] | 1457 | err = fl_validate_ct_state(key->ct_state & mask->ct_state, |
wenxu | 1bcc51a | 2021-02-09 14:37:49 +0800 | [diff] [blame] | 1458 | tb[TCA_FLOWER_KEY_CT_STATE_MASK], |
| 1459 | extack); |
| 1460 | if (err) |
| 1461 | return err; |
| 1462 | |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 1463 | } |
| 1464 | if (tb[TCA_FLOWER_KEY_CT_ZONE]) { |
| 1465 | if (!IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES)) { |
| 1466 | NL_SET_ERR_MSG(extack, "Conntrack zones isn't enabled"); |
| 1467 | return -EOPNOTSUPP; |
| 1468 | } |
| 1469 | fl_set_key_val(tb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE, |
| 1470 | &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK, |
| 1471 | sizeof(key->ct_zone)); |
| 1472 | } |
| 1473 | if (tb[TCA_FLOWER_KEY_CT_MARK]) { |
| 1474 | if (!IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)) { |
| 1475 | NL_SET_ERR_MSG(extack, "Conntrack mark isn't enabled"); |
| 1476 | return -EOPNOTSUPP; |
| 1477 | } |
| 1478 | fl_set_key_val(tb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK, |
| 1479 | &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK, |
| 1480 | sizeof(key->ct_mark)); |
| 1481 | } |
| 1482 | if (tb[TCA_FLOWER_KEY_CT_LABELS]) { |
| 1483 | if (!IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS)) { |
| 1484 | NL_SET_ERR_MSG(extack, "Conntrack labels aren't enabled"); |
| 1485 | return -EOPNOTSUPP; |
| 1486 | } |
| 1487 | fl_set_key_val(tb, key->ct_labels, TCA_FLOWER_KEY_CT_LABELS, |
| 1488 | mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK, |
| 1489 | sizeof(key->ct_labels)); |
| 1490 | } |
| 1491 | |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1495 | static int fl_set_key(struct net *net, struct nlattr **tb, |
Alexander Aring | 1057c55 | 2018-01-18 11:20:54 -0500 | [diff] [blame] | 1496 | struct fl_flow_key *key, struct fl_flow_key *mask, |
| 1497 | struct netlink_ext_ack *extack) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1498 | { |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1499 | __be16 ethertype; |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 1500 | int ret = 0; |
Jiri Pirko | a514862 | 2019-06-15 11:03:49 +0200 | [diff] [blame] | 1501 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1502 | if (tb[TCA_FLOWER_INDEV]) { |
Alexander Aring | 1057c55 | 2018-01-18 11:20:54 -0500 | [diff] [blame] | 1503 | int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1504 | if (err < 0) |
| 1505 | return err; |
Jiri Pirko | 8212ed7 | 2019-06-19 09:41:03 +0300 | [diff] [blame] | 1506 | key->meta.ingress_ifindex = err; |
| 1507 | mask->meta.ingress_ifindex = 0xffffffff; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1508 | } |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1509 | |
| 1510 | fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, |
| 1511 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, |
| 1512 | sizeof(key->eth.dst)); |
| 1513 | fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, |
| 1514 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, |
| 1515 | sizeof(key->eth.src)); |
Jamal Hadi Salim | 66530bd | 2016-01-10 11:47:01 -0500 | [diff] [blame] | 1516 | |
Arnd Bergmann | 0b498a5 | 2016-08-26 17:25:45 +0200 | [diff] [blame] | 1517 | if (tb[TCA_FLOWER_KEY_ETH_TYPE]) { |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1518 | ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]); |
| 1519 | |
Jianbo Liu | aaab083 | 2018-07-06 05:38:13 +0000 | [diff] [blame] | 1520 | if (eth_type_vlan(ethertype)) { |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1521 | fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID, |
| 1522 | TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, |
| 1523 | &mask->vlan); |
| 1524 | |
Jianbo Liu | 5e9a0fe | 2018-07-09 02:26:20 +0000 | [diff] [blame] | 1525 | if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) { |
| 1526 | ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]); |
| 1527 | if (eth_type_vlan(ethertype)) { |
| 1528 | fl_set_key_vlan(tb, ethertype, |
| 1529 | TCA_FLOWER_KEY_CVLAN_ID, |
| 1530 | TCA_FLOWER_KEY_CVLAN_PRIO, |
| 1531 | &key->cvlan, &mask->cvlan); |
| 1532 | fl_set_key_val(tb, &key->basic.n_proto, |
| 1533 | TCA_FLOWER_KEY_CVLAN_ETH_TYPE, |
| 1534 | &mask->basic.n_proto, |
| 1535 | TCA_FLOWER_UNSPEC, |
| 1536 | sizeof(key->basic.n_proto)); |
| 1537 | } else { |
| 1538 | key->basic.n_proto = ethertype; |
| 1539 | mask->basic.n_proto = cpu_to_be16(~0); |
| 1540 | } |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1541 | } |
Arnd Bergmann | 0b498a5 | 2016-08-26 17:25:45 +0200 | [diff] [blame] | 1542 | } else { |
| 1543 | key->basic.n_proto = ethertype; |
| 1544 | mask->basic.n_proto = cpu_to_be16(~0); |
| 1545 | } |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1546 | } |
Jamal Hadi Salim | 66530bd | 2016-01-10 11:47:01 -0500 | [diff] [blame] | 1547 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1548 | if (key->basic.n_proto == htons(ETH_P_IP) || |
| 1549 | key->basic.n_proto == htons(ETH_P_IPV6)) { |
| 1550 | fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO, |
| 1551 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, |
| 1552 | sizeof(key->basic.ip_proto)); |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 1553 | fl_set_key_ip(tb, false, &key->ip, &mask->ip); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1554 | } |
Jamal Hadi Salim | 66530bd | 2016-01-10 11:47:01 -0500 | [diff] [blame] | 1555 | |
| 1556 | if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) { |
| 1557 | key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
Paul Blakey | 970bfcd | 2016-12-14 19:00:57 +0200 | [diff] [blame] | 1558 | mask->control.addr_type = ~0; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1559 | fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
| 1560 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, |
| 1561 | sizeof(key->ipv4.src)); |
| 1562 | fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, |
| 1563 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, |
| 1564 | sizeof(key->ipv4.dst)); |
Jamal Hadi Salim | 66530bd | 2016-01-10 11:47:01 -0500 | [diff] [blame] | 1565 | } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) { |
| 1566 | key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
Paul Blakey | 970bfcd | 2016-12-14 19:00:57 +0200 | [diff] [blame] | 1567 | mask->control.addr_type = ~0; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1568 | fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
| 1569 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, |
| 1570 | sizeof(key->ipv6.src)); |
| 1571 | fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, |
| 1572 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, |
| 1573 | sizeof(key->ipv6.dst)); |
| 1574 | } |
Jamal Hadi Salim | 66530bd | 2016-01-10 11:47:01 -0500 | [diff] [blame] | 1575 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1576 | if (key->basic.ip_proto == IPPROTO_TCP) { |
| 1577 | 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] | 1578 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1579 | sizeof(key->tp.src)); |
| 1580 | 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] | 1581 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1582 | sizeof(key->tp.dst)); |
Jiri Pirko | fdfc7dd | 2017-05-23 18:40:45 +0200 | [diff] [blame] | 1583 | fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, |
| 1584 | &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, |
| 1585 | sizeof(key->tcp.flags)); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1586 | } else if (key->basic.ip_proto == IPPROTO_UDP) { |
| 1587 | 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] | 1588 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1589 | sizeof(key->tp.src)); |
| 1590 | 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] | 1591 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1592 | sizeof(key->tp.dst)); |
Simon Horman | 5976c5f | 2016-11-03 13:24:21 +0100 | [diff] [blame] | 1593 | } else if (key->basic.ip_proto == IPPROTO_SCTP) { |
| 1594 | fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, |
| 1595 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, |
| 1596 | sizeof(key->tp.src)); |
| 1597 | fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, |
| 1598 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, |
| 1599 | sizeof(key->tp.dst)); |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 1600 | } else if (key->basic.n_proto == htons(ETH_P_IP) && |
| 1601 | key->basic.ip_proto == IPPROTO_ICMP) { |
| 1602 | fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE, |
| 1603 | &mask->icmp.type, |
| 1604 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, |
| 1605 | sizeof(key->icmp.type)); |
| 1606 | fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE, |
| 1607 | &mask->icmp.code, |
| 1608 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK, |
| 1609 | sizeof(key->icmp.code)); |
| 1610 | } else if (key->basic.n_proto == htons(ETH_P_IPV6) && |
| 1611 | key->basic.ip_proto == IPPROTO_ICMPV6) { |
| 1612 | fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE, |
| 1613 | &mask->icmp.type, |
| 1614 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, |
| 1615 | sizeof(key->icmp.type)); |
Simon Horman | 040587a | 2017-01-30 16:19:02 +0100 | [diff] [blame] | 1616 | 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] | 1617 | &mask->icmp.code, |
Simon Horman | 040587a | 2017-01-30 16:19:02 +0100 | [diff] [blame] | 1618 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK, |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 1619 | sizeof(key->icmp.code)); |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 1620 | } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) || |
| 1621 | key->basic.n_proto == htons(ETH_P_MPLS_MC)) { |
Guillaume Nault | 442f730 | 2020-03-23 21:48:49 +0100 | [diff] [blame] | 1622 | ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls, extack); |
Benjamin LaHaise | 1a7fca6 | 2017-05-01 09:58:40 -0400 | [diff] [blame] | 1623 | if (ret) |
| 1624 | return ret; |
Simon Horman | 99d3132 | 2017-01-11 14:05:43 +0100 | [diff] [blame] | 1625 | } else if (key->basic.n_proto == htons(ETH_P_ARP) || |
| 1626 | key->basic.n_proto == htons(ETH_P_RARP)) { |
| 1627 | fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP, |
| 1628 | &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK, |
| 1629 | sizeof(key->arp.sip)); |
| 1630 | fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP, |
| 1631 | &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK, |
| 1632 | sizeof(key->arp.tip)); |
| 1633 | fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP, |
| 1634 | &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK, |
| 1635 | sizeof(key->arp.op)); |
| 1636 | fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, |
| 1637 | mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, |
| 1638 | sizeof(key->arp.sha)); |
| 1639 | fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, |
| 1640 | mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, |
| 1641 | sizeof(key->arp.tha)); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1642 | } |
| 1643 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 1644 | if (key->basic.ip_proto == IPPROTO_TCP || |
| 1645 | key->basic.ip_proto == IPPROTO_UDP || |
| 1646 | key->basic.ip_proto == IPPROTO_SCTP) { |
Guillaume Nault | bd7d4c1 | 2020-03-23 21:48:51 +0100 | [diff] [blame] | 1647 | ret = fl_set_key_port_range(tb, key, mask, extack); |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 1648 | if (ret) |
| 1649 | return ret; |
| 1650 | } |
| 1651 | |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 1652 | if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] || |
| 1653 | tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) { |
| 1654 | key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS; |
Paul Blakey | 970bfcd | 2016-12-14 19:00:57 +0200 | [diff] [blame] | 1655 | mask->enc_control.addr_type = ~0; |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 1656 | fl_set_key_val(tb, &key->enc_ipv4.src, |
| 1657 | TCA_FLOWER_KEY_ENC_IPV4_SRC, |
| 1658 | &mask->enc_ipv4.src, |
| 1659 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, |
| 1660 | sizeof(key->enc_ipv4.src)); |
| 1661 | fl_set_key_val(tb, &key->enc_ipv4.dst, |
| 1662 | TCA_FLOWER_KEY_ENC_IPV4_DST, |
| 1663 | &mask->enc_ipv4.dst, |
| 1664 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, |
| 1665 | sizeof(key->enc_ipv4.dst)); |
| 1666 | } |
| 1667 | |
| 1668 | if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] || |
| 1669 | tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) { |
| 1670 | key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS; |
Paul Blakey | 970bfcd | 2016-12-14 19:00:57 +0200 | [diff] [blame] | 1671 | mask->enc_control.addr_type = ~0; |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 1672 | fl_set_key_val(tb, &key->enc_ipv6.src, |
| 1673 | TCA_FLOWER_KEY_ENC_IPV6_SRC, |
| 1674 | &mask->enc_ipv6.src, |
| 1675 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, |
| 1676 | sizeof(key->enc_ipv6.src)); |
| 1677 | fl_set_key_val(tb, &key->enc_ipv6.dst, |
| 1678 | TCA_FLOWER_KEY_ENC_IPV6_DST, |
| 1679 | &mask->enc_ipv6.dst, |
| 1680 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, |
| 1681 | sizeof(key->enc_ipv6.dst)); |
| 1682 | } |
| 1683 | |
| 1684 | 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] | 1685 | &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC, |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 1686 | sizeof(key->enc_key_id.keyid)); |
| 1687 | |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 1688 | fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, |
| 1689 | &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, |
| 1690 | sizeof(key->enc_tp.src)); |
| 1691 | |
| 1692 | fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT, |
| 1693 | &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, |
| 1694 | sizeof(key->enc_tp.dst)); |
| 1695 | |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 1696 | fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip); |
| 1697 | |
Ariel Levkovich | 5923b8f | 2020-07-23 01:03:01 +0300 | [diff] [blame] | 1698 | fl_set_key_val(tb, &key->hash.hash, TCA_FLOWER_KEY_HASH, |
| 1699 | &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK, |
| 1700 | sizeof(key->hash.hash)); |
| 1701 | |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1702 | if (tb[TCA_FLOWER_KEY_ENC_OPTS]) { |
| 1703 | ret = fl_set_enc_opt(tb, key, mask, extack); |
| 1704 | if (ret) |
| 1705 | return ret; |
| 1706 | } |
| 1707 | |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 1708 | ret = fl_set_key_ct(tb, &key->ct, &mask->ct, extack); |
| 1709 | if (ret) |
| 1710 | return ret; |
| 1711 | |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 1712 | if (tb[TCA_FLOWER_KEY_FLAGS]) |
Guillaume Nault | e304e21 | 2020-03-23 21:48:53 +0100 | [diff] [blame] | 1713 | ret = fl_set_key_flags(tb, &key->control.flags, |
| 1714 | &mask->control.flags, extack); |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 1715 | |
Or Gerlitz | d972477 | 2016-12-22 14:28:15 +0200 | [diff] [blame] | 1716 | return ret; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1717 | } |
| 1718 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1719 | static void fl_mask_copy(struct fl_flow_mask *dst, |
| 1720 | struct fl_flow_mask *src) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1721 | { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1722 | const void *psrc = fl_key_get_start(&src->key, src); |
| 1723 | void *pdst = fl_key_get_start(&dst->key, src); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1724 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1725 | memcpy(pdst, psrc, fl_mask_range(src)); |
| 1726 | dst->range = src->range; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | static const struct rhashtable_params fl_ht_params = { |
| 1730 | .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */ |
| 1731 | .head_offset = offsetof(struct cls_fl_filter, ht_node), |
| 1732 | .automatic_shrinking = true, |
| 1733 | }; |
| 1734 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1735 | static int fl_init_mask_hashtable(struct fl_flow_mask *mask) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1736 | { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1737 | mask->filter_ht_params = fl_ht_params; |
| 1738 | mask->filter_ht_params.key_len = fl_mask_range(mask); |
| 1739 | mask->filter_ht_params.key_offset += mask->range.start; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1740 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1741 | return rhashtable_init(&mask->ht, &mask->filter_ht_params); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member) |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 1745 | #define FL_KEY_MEMBER_SIZE(member) sizeof_field(struct fl_flow_key, member) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1746 | |
Hadar Hen Zion | 339ba87 | 2016-08-17 13:36:12 +0300 | [diff] [blame] | 1747 | #define FL_KEY_IS_MASKED(mask, member) \ |
| 1748 | memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \ |
| 1749 | 0, FL_KEY_MEMBER_SIZE(member)) \ |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1750 | |
| 1751 | #define FL_KEY_SET(keys, cnt, id, member) \ |
| 1752 | do { \ |
| 1753 | keys[cnt].key_id = id; \ |
| 1754 | keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \ |
| 1755 | cnt++; \ |
| 1756 | } while(0); |
| 1757 | |
Hadar Hen Zion | 339ba87 | 2016-08-17 13:36:12 +0300 | [diff] [blame] | 1758 | #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \ |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1759 | do { \ |
Hadar Hen Zion | 339ba87 | 2016-08-17 13:36:12 +0300 | [diff] [blame] | 1760 | if (FL_KEY_IS_MASKED(mask, member)) \ |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1761 | FL_KEY_SET(keys, cnt, id, member); \ |
| 1762 | } while(0); |
| 1763 | |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1764 | static void fl_init_dissector(struct flow_dissector *dissector, |
| 1765 | struct fl_flow_key *mask) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1766 | { |
| 1767 | struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX]; |
| 1768 | size_t cnt = 0; |
| 1769 | |
Jiri Pirko | 8212ed7 | 2019-06-19 09:41:03 +0300 | [diff] [blame] | 1770 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
| 1771 | FLOW_DISSECTOR_KEY_META, meta); |
Tom Herbert | 42aecaa | 2015-06-04 09:16:39 -0700 | [diff] [blame] | 1772 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1773 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1774 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 339ba87 | 2016-08-17 13:36:12 +0300 | [diff] [blame] | 1775 | FLOW_DISSECTOR_KEY_ETH_ADDRS, eth); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1776 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 339ba87 | 2016-08-17 13:36:12 +0300 | [diff] [blame] | 1777 | FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1778 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 339ba87 | 2016-08-17 13:36:12 +0300 | [diff] [blame] | 1779 | FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6); |
Yoshiki Komachi | 8ffb055 | 2019-12-03 19:40:12 +0900 | [diff] [blame] | 1780 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
| 1781 | FLOW_DISSECTOR_KEY_PORTS, tp); |
| 1782 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
| 1783 | FLOW_DISSECTOR_KEY_PORTS_RANGE, tp_range); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1784 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 1785 | FLOW_DISSECTOR_KEY_IP, ip); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1786 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Jiri Pirko | fdfc7dd | 2017-05-23 18:40:45 +0200 | [diff] [blame] | 1787 | FLOW_DISSECTOR_KEY_TCP, tcp); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1788 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 1789 | FLOW_DISSECTOR_KEY_ICMP, icmp); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1790 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Simon Horman | 99d3132 | 2017-01-11 14:05:43 +0100 | [diff] [blame] | 1791 | FLOW_DISSECTOR_KEY_ARP, arp); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1792 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 1793 | FLOW_DISSECTOR_KEY_MPLS, mpls); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1794 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 1795 | FLOW_DISSECTOR_KEY_VLAN, vlan); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1796 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 1797 | FLOW_DISSECTOR_KEY_CVLAN, cvlan); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1798 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 519d105 | 2016-11-07 15:14:38 +0200 | [diff] [blame] | 1799 | FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1800 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 519d105 | 2016-11-07 15:14:38 +0200 | [diff] [blame] | 1801 | FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1802 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | 519d105 | 2016-11-07 15:14:38 +0200 | [diff] [blame] | 1803 | FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1804 | if (FL_KEY_IS_MASKED(mask, enc_ipv4) || |
| 1805 | FL_KEY_IS_MASKED(mask, enc_ipv6)) |
Hadar Hen Zion | 519d105 | 2016-11-07 15:14:38 +0200 | [diff] [blame] | 1806 | FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL, |
| 1807 | enc_control); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1808 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 1809 | FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp); |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1810 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 1811 | FLOW_DISSECTOR_KEY_ENC_IP, enc_ip); |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 1812 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
| 1813 | FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts); |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 1814 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
| 1815 | FLOW_DISSECTOR_KEY_CT, ct); |
Ariel Levkovich | 5923b8f | 2020-07-23 01:03:01 +0300 | [diff] [blame] | 1816 | FL_KEY_SET_IF_MASKED(mask, keys, cnt, |
| 1817 | FLOW_DISSECTOR_KEY_HASH, hash); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1818 | |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1819 | skb_flow_dissector_init(dissector, keys, cnt); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head, |
| 1823 | struct fl_flow_mask *mask) |
| 1824 | { |
| 1825 | struct fl_flow_mask *newmask; |
| 1826 | int err; |
| 1827 | |
| 1828 | newmask = kzalloc(sizeof(*newmask), GFP_KERNEL); |
| 1829 | if (!newmask) |
| 1830 | return ERR_PTR(-ENOMEM); |
| 1831 | |
| 1832 | fl_mask_copy(newmask, mask); |
| 1833 | |
Yoshiki Komachi | 8ffb055 | 2019-12-03 19:40:12 +0900 | [diff] [blame] | 1834 | if ((newmask->key.tp_range.tp_min.dst && |
| 1835 | newmask->key.tp_range.tp_max.dst) || |
| 1836 | (newmask->key.tp_range.tp_min.src && |
| 1837 | newmask->key.tp_range.tp_max.src)) |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 1838 | newmask->flags |= TCA_FLOWER_MASK_FLAGS_RANGE; |
| 1839 | |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1840 | err = fl_init_mask_hashtable(newmask); |
| 1841 | if (err) |
| 1842 | goto errout_free; |
| 1843 | |
Jiri Pirko | 33fb5cb | 2018-07-23 09:23:09 +0200 | [diff] [blame] | 1844 | fl_init_dissector(&newmask->dissector, &newmask->key); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1845 | |
| 1846 | INIT_LIST_HEAD_RCU(&newmask->filters); |
| 1847 | |
Vlad Buslov | f48ef4d | 2019-03-21 15:17:37 +0200 | [diff] [blame] | 1848 | refcount_set(&newmask->refcnt, 1); |
Vlad Buslov | 195c234 | 2019-03-21 15:17:38 +0200 | [diff] [blame] | 1849 | err = rhashtable_replace_fast(&head->ht, &mask->ht_node, |
| 1850 | &newmask->ht_node, mask_ht_params); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1851 | if (err) |
| 1852 | goto errout_destroy; |
| 1853 | |
Vlad Buslov | 259e60f | 2019-03-21 15:17:39 +0200 | [diff] [blame] | 1854 | spin_lock(&head->masks_lock); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1855 | list_add_tail_rcu(&newmask->list, &head->masks); |
Vlad Buslov | 259e60f | 2019-03-21 15:17:39 +0200 | [diff] [blame] | 1856 | spin_unlock(&head->masks_lock); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1857 | |
| 1858 | return newmask; |
| 1859 | |
| 1860 | errout_destroy: |
| 1861 | rhashtable_destroy(&newmask->ht); |
| 1862 | errout_free: |
| 1863 | kfree(newmask); |
| 1864 | |
| 1865 | return ERR_PTR(err); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | static int fl_check_assign_mask(struct cls_fl_head *head, |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1869 | struct cls_fl_filter *fnew, |
| 1870 | struct cls_fl_filter *fold, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1871 | struct fl_flow_mask *mask) |
| 1872 | { |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1873 | struct fl_flow_mask *newmask; |
Vlad Buslov | f48ef4d | 2019-03-21 15:17:37 +0200 | [diff] [blame] | 1874 | int ret = 0; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1875 | |
Vlad Buslov | f48ef4d | 2019-03-21 15:17:37 +0200 | [diff] [blame] | 1876 | rcu_read_lock(); |
Vlad Buslov | 195c234 | 2019-03-21 15:17:38 +0200 | [diff] [blame] | 1877 | |
| 1878 | /* Insert mask as temporary node to prevent concurrent creation of mask |
| 1879 | * with same key. Any concurrent lookups with same key will return |
Vlad Buslov | 99815f5 | 2019-06-13 17:54:04 +0300 | [diff] [blame] | 1880 | * -EAGAIN because mask's refcnt is zero. |
Vlad Buslov | 195c234 | 2019-03-21 15:17:38 +0200 | [diff] [blame] | 1881 | */ |
| 1882 | fnew->mask = rhashtable_lookup_get_insert_fast(&head->ht, |
| 1883 | &mask->ht_node, |
| 1884 | mask_ht_params); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1885 | if (!fnew->mask) { |
Vlad Buslov | f48ef4d | 2019-03-21 15:17:37 +0200 | [diff] [blame] | 1886 | rcu_read_unlock(); |
| 1887 | |
Vlad Buslov | 195c234 | 2019-03-21 15:17:38 +0200 | [diff] [blame] | 1888 | if (fold) { |
| 1889 | ret = -EINVAL; |
| 1890 | goto errout_cleanup; |
| 1891 | } |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1892 | |
| 1893 | newmask = fl_create_new_mask(head, mask); |
Vlad Buslov | 195c234 | 2019-03-21 15:17:38 +0200 | [diff] [blame] | 1894 | if (IS_ERR(newmask)) { |
| 1895 | ret = PTR_ERR(newmask); |
| 1896 | goto errout_cleanup; |
| 1897 | } |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 1898 | |
| 1899 | fnew->mask = newmask; |
Vlad Buslov | f48ef4d | 2019-03-21 15:17:37 +0200 | [diff] [blame] | 1900 | return 0; |
Vlad Buslov | 195c234 | 2019-03-21 15:17:38 +0200 | [diff] [blame] | 1901 | } else if (IS_ERR(fnew->mask)) { |
| 1902 | ret = PTR_ERR(fnew->mask); |
Paul Blakey | f6521c5 | 2018-06-03 10:06:14 +0300 | [diff] [blame] | 1903 | } else if (fold && fold->mask != fnew->mask) { |
Vlad Buslov | f48ef4d | 2019-03-21 15:17:37 +0200 | [diff] [blame] | 1904 | ret = -EINVAL; |
| 1905 | } else if (!refcount_inc_not_zero(&fnew->mask->refcnt)) { |
| 1906 | /* Mask was deleted concurrently, try again */ |
| 1907 | ret = -EAGAIN; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1908 | } |
Vlad Buslov | f48ef4d | 2019-03-21 15:17:37 +0200 | [diff] [blame] | 1909 | rcu_read_unlock(); |
| 1910 | return ret; |
Vlad Buslov | 195c234 | 2019-03-21 15:17:38 +0200 | [diff] [blame] | 1911 | |
| 1912 | errout_cleanup: |
| 1913 | rhashtable_remove_fast(&head->ht, &mask->ht_node, |
| 1914 | mask_ht_params); |
Vlad Buslov | 195c234 | 2019-03-21 15:17:38 +0200 | [diff] [blame] | 1915 | return ret; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | static int fl_set_parms(struct net *net, struct tcf_proto *tp, |
| 1919 | struct cls_fl_filter *f, struct fl_flow_mask *mask, |
| 1920 | unsigned long base, struct nlattr **tb, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1921 | struct nlattr *est, |
Baowen Zheng | c86e020 | 2021-12-17 19:16:28 +0100 | [diff] [blame] | 1922 | struct fl_flow_tmplt *tmplt, |
| 1923 | u32 flags, u32 fl_flags, |
Alexander Aring | 50a5619 | 2018-01-18 11:20:52 -0500 | [diff] [blame] | 1924 | struct netlink_ext_ack *extack) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1925 | { |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1926 | int err; |
| 1927 | |
Baowen Zheng | c86e020 | 2021-12-17 19:16:28 +0100 | [diff] [blame] | 1928 | err = tcf_exts_validate_ex(net, tp, tb, est, &f->exts, flags, |
| 1929 | fl_flags, extack); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1930 | if (err < 0) |
| 1931 | return err; |
| 1932 | |
| 1933 | if (tb[TCA_FLOWER_CLASSID]) { |
| 1934 | f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]); |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1935 | if (flags & TCA_ACT_FLAGS_NO_RTNL) |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 1936 | rtnl_lock(); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1937 | tcf_bind_filter(tp, &f->res, base); |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1938 | if (flags & TCA_ACT_FLAGS_NO_RTNL) |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 1939 | rtnl_unlock(); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1940 | } |
| 1941 | |
Alexander Aring | 1057c55 | 2018-01-18 11:20:54 -0500 | [diff] [blame] | 1942 | err = fl_set_key(net, tb, &f->key, &mask->key, extack); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1943 | if (err) |
Jiri Pirko | 4550752 | 2017-08-04 14:29:06 +0200 | [diff] [blame] | 1944 | return err; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1945 | |
| 1946 | fl_mask_update_range(mask); |
| 1947 | fl_set_masked_key(&f->mkey, &f->key, mask); |
| 1948 | |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 1949 | if (!fl_mask_fits_tmplt(tmplt, mask)) { |
| 1950 | NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template"); |
| 1951 | return -EINVAL; |
| 1952 | } |
| 1953 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1954 | return 0; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1955 | } |
| 1956 | |
Vlad Buslov | 1f17f77 | 2019-04-05 20:56:26 +0300 | [diff] [blame] | 1957 | static int fl_ht_insert_unique(struct cls_fl_filter *fnew, |
| 1958 | struct cls_fl_filter *fold, |
| 1959 | bool *in_ht) |
| 1960 | { |
| 1961 | struct fl_flow_mask *mask = fnew->mask; |
| 1962 | int err; |
| 1963 | |
Vlad Buslov | 9e35552 | 2019-04-11 19:12:20 +0300 | [diff] [blame] | 1964 | err = rhashtable_lookup_insert_fast(&mask->ht, |
| 1965 | &fnew->ht_node, |
| 1966 | mask->filter_ht_params); |
Vlad Buslov | 1f17f77 | 2019-04-05 20:56:26 +0300 | [diff] [blame] | 1967 | if (err) { |
| 1968 | *in_ht = false; |
| 1969 | /* It is okay if filter with same key exists when |
| 1970 | * overwriting. |
| 1971 | */ |
| 1972 | return fold && err == -EEXIST ? 0 : err; |
| 1973 | } |
| 1974 | |
| 1975 | *in_ht = true; |
| 1976 | return 0; |
| 1977 | } |
| 1978 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1979 | static int fl_change(struct net *net, struct sk_buff *in_skb, |
| 1980 | struct tcf_proto *tp, unsigned long base, |
| 1981 | u32 handle, struct nlattr **tca, |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1982 | void **arg, u32 flags, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 1983 | struct netlink_ext_ack *extack) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1984 | { |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame] | 1985 | struct cls_fl_head *head = fl_head_dereference(tp); |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 1986 | bool rtnl_held = !(flags & TCA_ACT_FLAGS_NO_RTNL); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 1987 | struct cls_fl_filter *fold = *arg; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1988 | struct cls_fl_filter *fnew; |
Ivan Vecera | 2cddd20 | 2019-01-16 16:53:52 +0100 | [diff] [blame] | 1989 | struct fl_flow_mask *mask; |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 1990 | struct nlattr **tb; |
Vlad Buslov | 1f17f77 | 2019-04-05 20:56:26 +0300 | [diff] [blame] | 1991 | bool in_ht; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1992 | int err; |
| 1993 | |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 1994 | if (!tca[TCA_OPTIONS]) { |
| 1995 | err = -EINVAL; |
| 1996 | goto errout_fold; |
| 1997 | } |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 1998 | |
Ivan Vecera | 2cddd20 | 2019-01-16 16:53:52 +0100 | [diff] [blame] | 1999 | mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL); |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 2000 | if (!mask) { |
| 2001 | err = -ENOBUFS; |
| 2002 | goto errout_fold; |
| 2003 | } |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 2004 | |
Ivan Vecera | 2cddd20 | 2019-01-16 16:53:52 +0100 | [diff] [blame] | 2005 | tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); |
| 2006 | if (!tb) { |
| 2007 | err = -ENOBUFS; |
| 2008 | goto errout_mask_alloc; |
| 2009 | } |
| 2010 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2011 | err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX, |
| 2012 | tca[TCA_OPTIONS], fl_policy, NULL); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2013 | if (err < 0) |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 2014 | goto errout_tb; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2015 | |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 2016 | if (fold && handle && fold->handle != handle) { |
| 2017 | err = -EINVAL; |
| 2018 | goto errout_tb; |
| 2019 | } |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2020 | |
| 2021 | fnew = kzalloc(sizeof(*fnew), GFP_KERNEL); |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 2022 | if (!fnew) { |
| 2023 | err = -ENOBUFS; |
| 2024 | goto errout_tb; |
| 2025 | } |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 2026 | INIT_LIST_HEAD(&fnew->hw_list); |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 2027 | refcount_set(&fnew->refcnt, 1); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2028 | |
Cong Wang | 1421510 | 2019-02-20 21:37:42 -0800 | [diff] [blame] | 2029 | err = tcf_exts_init(&fnew->exts, net, TCA_FLOWER_ACT, 0); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 2030 | if (err < 0) |
| 2031 | goto errout; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2032 | |
Vlad Buslov | ecb3dea | 2019-03-06 16:22:12 +0200 | [diff] [blame] | 2033 | if (tb[TCA_FLOWER_FLAGS]) { |
| 2034 | fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]); |
| 2035 | |
| 2036 | if (!tc_flags_valid(fnew->flags)) { |
| 2037 | err = -EINVAL; |
| 2038 | goto errout; |
| 2039 | } |
| 2040 | } |
| 2041 | |
Cong Wang | 695176b | 2021-07-29 16:12:14 -0700 | [diff] [blame] | 2042 | err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], |
Baowen Zheng | c86e020 | 2021-12-17 19:16:28 +0100 | [diff] [blame] | 2043 | tp->chain->tmplt_priv, flags, fnew->flags, |
| 2044 | extack); |
Vlad Buslov | ecb3dea | 2019-03-06 16:22:12 +0200 | [diff] [blame] | 2045 | if (err) |
| 2046 | goto errout; |
| 2047 | |
| 2048 | err = fl_check_assign_mask(head, fnew, fold, mask); |
| 2049 | if (err) |
| 2050 | goto errout; |
| 2051 | |
Vlad Buslov | 1f17f77 | 2019-04-05 20:56:26 +0300 | [diff] [blame] | 2052 | err = fl_ht_insert_unique(fnew, fold, &in_ht); |
| 2053 | if (err) |
| 2054 | goto errout_mask; |
| 2055 | |
Hadar Hen Zion | 7968521 | 2016-12-01 14:06:34 +0200 | [diff] [blame] | 2056 | if (!tc_skip_hw(fnew->flags)) { |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 2057 | err = fl_hw_replace_filter(tp, fnew, rtnl_held, extack); |
Hadar Hen Zion | 7968521 | 2016-12-01 14:06:34 +0200 | [diff] [blame] | 2058 | if (err) |
Vlad Buslov | 1f17f77 | 2019-04-05 20:56:26 +0300 | [diff] [blame] | 2059 | goto errout_ht; |
Hadar Hen Zion | 7968521 | 2016-12-01 14:06:34 +0200 | [diff] [blame] | 2060 | } |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 2061 | |
Or Gerlitz | 5559396 | 2017-02-16 10:31:13 +0200 | [diff] [blame] | 2062 | if (!tc_in_hw(fnew->flags)) |
| 2063 | fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW; |
| 2064 | |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 2065 | spin_lock(&tp->lock); |
| 2066 | |
Vlad Buslov | 272ffaa | 2019-03-21 15:17:41 +0200 | [diff] [blame] | 2067 | /* tp was deleted concurrently. -EAGAIN will cause caller to lookup |
| 2068 | * proto again or create new one, if necessary. |
| 2069 | */ |
| 2070 | if (tp->deleting) { |
| 2071 | err = -EAGAIN; |
| 2072 | goto errout_hw; |
| 2073 | } |
| 2074 | |
Amir Vadai | 5b33f48 | 2016-03-08 12:42:29 +0200 | [diff] [blame] | 2075 | if (fold) { |
Vlad Buslov | b2552b8 | 2019-03-21 15:17:36 +0200 | [diff] [blame] | 2076 | /* Fold filter was deleted concurrently. Retry lookup. */ |
| 2077 | if (fold->deleted) { |
| 2078 | err = -EAGAIN; |
| 2079 | goto errout_hw; |
| 2080 | } |
| 2081 | |
Vlad Buslov | 620da48 | 2019-03-21 15:17:34 +0200 | [diff] [blame] | 2082 | fnew->handle = handle; |
| 2083 | |
Vlad Buslov | 1f17f77 | 2019-04-05 20:56:26 +0300 | [diff] [blame] | 2084 | if (!in_ht) { |
| 2085 | struct rhashtable_params params = |
| 2086 | fnew->mask->filter_ht_params; |
| 2087 | |
| 2088 | err = rhashtable_insert_fast(&fnew->mask->ht, |
| 2089 | &fnew->ht_node, |
| 2090 | params); |
| 2091 | if (err) |
| 2092 | goto errout_hw; |
| 2093 | in_ht = true; |
| 2094 | } |
Vlad Buslov | 620da48 | 2019-03-21 15:17:34 +0200 | [diff] [blame] | 2095 | |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 2096 | refcount_inc(&fnew->refcnt); |
Roi Dayan | 599d257 | 2018-12-19 18:07:56 +0200 | [diff] [blame] | 2097 | rhashtable_remove_fast(&fold->mask->ht, |
| 2098 | &fold->ht_node, |
| 2099 | fold->mask->filter_ht_params); |
Matthew Wilcox | 234a462 | 2017-11-28 09:56:36 -0500 | [diff] [blame] | 2100 | idr_replace(&head->handle_idr, fnew, fnew->handle); |
Daniel Borkmann | ff3532f | 2015-07-17 22:38:44 +0200 | [diff] [blame] | 2101 | list_replace_rcu(&fold->list, &fnew->list); |
Vlad Buslov | b2552b8 | 2019-03-21 15:17:36 +0200 | [diff] [blame] | 2102 | fold->deleted = true; |
Vlad Buslov | 620da48 | 2019-03-21 15:17:34 +0200 | [diff] [blame] | 2103 | |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 2104 | spin_unlock(&tp->lock); |
| 2105 | |
Vlad Buslov | 9994677 | 2019-04-12 00:54:19 +0300 | [diff] [blame] | 2106 | fl_mask_put(head, fold->mask); |
Vlad Buslov | 620da48 | 2019-03-21 15:17:34 +0200 | [diff] [blame] | 2107 | if (!tc_skip_hw(fold->flags)) |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 2108 | fl_hw_destroy_filter(tp, fold, rtnl_held, NULL); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2109 | tcf_unbind_filter(tp, &fold->res); |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 2110 | /* Caller holds reference to fold, so refcnt is always > 0 |
| 2111 | * after this. |
| 2112 | */ |
| 2113 | refcount_dec(&fold->refcnt); |
| 2114 | __fl_put(fold); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2115 | } else { |
Vlad Buslov | 620da48 | 2019-03-21 15:17:34 +0200 | [diff] [blame] | 2116 | if (handle) { |
| 2117 | /* user specifies a handle and it doesn't exist */ |
| 2118 | err = idr_alloc_u32(&head->handle_idr, fnew, &handle, |
| 2119 | handle, GFP_ATOMIC); |
Vlad Buslov | 9a2d938 | 2019-03-21 15:17:40 +0200 | [diff] [blame] | 2120 | |
| 2121 | /* Filter with specified handle was concurrently |
| 2122 | * inserted after initial check in cls_api. This is not |
| 2123 | * necessarily an error if NLM_F_EXCL is not set in |
| 2124 | * message flags. Returning EAGAIN will cause cls_api to |
| 2125 | * try to update concurrently inserted rule. |
| 2126 | */ |
| 2127 | if (err == -ENOSPC) |
| 2128 | err = -EAGAIN; |
Vlad Buslov | 620da48 | 2019-03-21 15:17:34 +0200 | [diff] [blame] | 2129 | } else { |
| 2130 | handle = 1; |
| 2131 | err = idr_alloc_u32(&head->handle_idr, fnew, &handle, |
| 2132 | INT_MAX, GFP_ATOMIC); |
| 2133 | } |
| 2134 | if (err) |
| 2135 | goto errout_hw; |
| 2136 | |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 2137 | refcount_inc(&fnew->refcnt); |
Vlad Buslov | 620da48 | 2019-03-21 15:17:34 +0200 | [diff] [blame] | 2138 | fnew->handle = handle; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 2139 | list_add_tail_rcu(&fnew->list, &fnew->mask->filters); |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 2140 | spin_unlock(&tp->lock); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2141 | } |
| 2142 | |
Vlad Buslov | 620da48 | 2019-03-21 15:17:34 +0200 | [diff] [blame] | 2143 | *arg = fnew; |
| 2144 | |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 2145 | kfree(tb); |
Vlad Buslov | 99815f5 | 2019-06-13 17:54:04 +0300 | [diff] [blame] | 2146 | tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2147 | return 0; |
| 2148 | |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 2149 | errout_ht: |
| 2150 | spin_lock(&tp->lock); |
Vlad Buslov | 620da48 | 2019-03-21 15:17:34 +0200 | [diff] [blame] | 2151 | errout_hw: |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 2152 | fnew->deleted = true; |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 2153 | spin_unlock(&tp->lock); |
Vlad Buslov | 620da48 | 2019-03-21 15:17:34 +0200 | [diff] [blame] | 2154 | if (!tc_skip_hw(fnew->flags)) |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 2155 | fl_hw_destroy_filter(tp, fnew, rtnl_held, NULL); |
Vlad Buslov | 1f17f77 | 2019-04-05 20:56:26 +0300 | [diff] [blame] | 2156 | if (in_ht) |
| 2157 | rhashtable_remove_fast(&fnew->mask->ht, &fnew->ht_node, |
| 2158 | fnew->mask->filter_ht_params); |
Vlad Buslov | ecb3dea | 2019-03-06 16:22:12 +0200 | [diff] [blame] | 2159 | errout_mask: |
Vlad Buslov | 9994677 | 2019-04-12 00:54:19 +0300 | [diff] [blame] | 2160 | fl_mask_put(head, fnew->mask); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2161 | errout: |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 2162 | __fl_put(fnew); |
Arnd Bergmann | 39b7b6a | 2017-01-19 10:45:31 +0100 | [diff] [blame] | 2163 | errout_tb: |
| 2164 | kfree(tb); |
Ivan Vecera | 2cddd20 | 2019-01-16 16:53:52 +0100 | [diff] [blame] | 2165 | errout_mask_alloc: |
Vlad Buslov | 99815f5 | 2019-06-13 17:54:04 +0300 | [diff] [blame] | 2166 | tcf_queue_work(&mask->rwork, fl_uninit_mask_free_work); |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 2167 | errout_fold: |
| 2168 | if (fold) |
| 2169 | __fl_put(fold); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2170 | return err; |
| 2171 | } |
| 2172 | |
Alexander Aring | 571acf2 | 2018-01-18 11:20:53 -0500 | [diff] [blame] | 2173 | static int fl_delete(struct tcf_proto *tp, void *arg, bool *last, |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2174 | bool rtnl_held, struct netlink_ext_ack *extack) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2175 | { |
Vlad Buslov | e474619 | 2019-03-21 15:17:33 +0200 | [diff] [blame] | 2176 | struct cls_fl_head *head = fl_head_dereference(tp); |
WANG Cong | 8113c09 | 2017-08-04 21:31:43 -0700 | [diff] [blame] | 2177 | struct cls_fl_filter *f = arg; |
Vlad Buslov | b2552b8 | 2019-03-21 15:17:36 +0200 | [diff] [blame] | 2178 | bool last_on_mask; |
| 2179 | int err = 0; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2180 | |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 2181 | err = __fl_delete(tp, f, &last_on_mask, rtnl_held, extack); |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 2182 | *last = list_empty(&head->masks); |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 2183 | __fl_put(f); |
| 2184 | |
Vlad Buslov | b2552b8 | 2019-03-21 15:17:36 +0200 | [diff] [blame] | 2185 | return err; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2186 | } |
| 2187 | |
Vlad Buslov | 12db03b | 2019-02-11 10:55:45 +0200 | [diff] [blame] | 2188 | static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg, |
| 2189 | bool rtnl_held) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2190 | { |
Cong Wang | d39d714 | 2019-06-28 11:03:42 -0700 | [diff] [blame] | 2191 | struct cls_fl_head *head = fl_head_dereference(tp); |
| 2192 | unsigned long id = arg->cookie, tmp; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2193 | struct cls_fl_filter *f; |
| 2194 | |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 2195 | arg->count = arg->skip; |
| 2196 | |
Vlad Buslov | d5ef190 | 2021-09-29 18:08:49 +0300 | [diff] [blame] | 2197 | rcu_read_lock(); |
Cong Wang | d39d714 | 2019-06-28 11:03:42 -0700 | [diff] [blame] | 2198 | idr_for_each_entry_continue_ul(&head->handle_idr, f, tmp, id) { |
| 2199 | /* don't return filters that are being deleted */ |
| 2200 | if (!refcount_inc_not_zero(&f->refcnt)) |
| 2201 | continue; |
Vlad Buslov | d5ef190 | 2021-09-29 18:08:49 +0300 | [diff] [blame] | 2202 | rcu_read_unlock(); |
| 2203 | |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 2204 | if (arg->fn(tp, f, arg) < 0) { |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 2205 | __fl_put(f); |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 2206 | arg->stop = 1; |
Vlad Buslov | d5ef190 | 2021-09-29 18:08:49 +0300 | [diff] [blame] | 2207 | rcu_read_lock(); |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 2208 | break; |
Paul Blakey | 05cd271 | 2018-04-30 14:28:30 +0300 | [diff] [blame] | 2209 | } |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 2210 | __fl_put(f); |
Vlad Buslov | 01683a1 | 2018-07-09 13:29:11 +0300 | [diff] [blame] | 2211 | arg->count++; |
Vlad Buslov | d5ef190 | 2021-09-29 18:08:49 +0300 | [diff] [blame] | 2212 | rcu_read_lock(); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2213 | } |
Vlad Buslov | d5ef190 | 2021-09-29 18:08:49 +0300 | [diff] [blame] | 2214 | rcu_read_unlock(); |
Cong Wang | d39d714 | 2019-06-28 11:03:42 -0700 | [diff] [blame] | 2215 | arg->cookie = id; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2216 | } |
| 2217 | |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 2218 | static struct cls_fl_filter * |
| 2219 | fl_get_next_hw_filter(struct tcf_proto *tp, struct cls_fl_filter *f, bool add) |
| 2220 | { |
| 2221 | struct cls_fl_head *head = fl_head_dereference(tp); |
| 2222 | |
| 2223 | spin_lock(&tp->lock); |
| 2224 | if (list_empty(&head->hw_filters)) { |
| 2225 | spin_unlock(&tp->lock); |
| 2226 | return NULL; |
| 2227 | } |
| 2228 | |
| 2229 | if (!f) |
| 2230 | f = list_entry(&head->hw_filters, struct cls_fl_filter, |
| 2231 | hw_list); |
| 2232 | list_for_each_entry_continue(f, &head->hw_filters, hw_list) { |
| 2233 | if (!(add && f->deleted) && refcount_inc_not_zero(&f->refcnt)) { |
| 2234 | spin_unlock(&tp->lock); |
| 2235 | return f; |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | spin_unlock(&tp->lock); |
| 2240 | return NULL; |
| 2241 | } |
| 2242 | |
Pablo Neira Ayuso | a732331 | 2019-07-19 18:20:15 +0200 | [diff] [blame] | 2243 | 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] | 2244 | void *cb_priv, struct netlink_ext_ack *extack) |
| 2245 | { |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 2246 | struct tcf_block *block = tp->chain->block; |
Pablo Neira Ayuso | f9e3008 | 2019-07-09 22:55:49 +0200 | [diff] [blame] | 2247 | struct flow_cls_offload cls_flower = {}; |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 2248 | struct cls_fl_filter *f = NULL; |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 2249 | int err; |
| 2250 | |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 2251 | /* hw_filters list can only be changed by hw offload functions after |
| 2252 | * obtaining rtnl lock. Make sure it is not changed while reoffload is |
| 2253 | * iterating it. |
| 2254 | */ |
| 2255 | ASSERT_RTNL(); |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 2256 | |
Vlad Buslov | c049d56 | 2019-04-24 09:53:31 +0300 | [diff] [blame] | 2257 | while ((f = fl_get_next_hw_filter(tp, f, add))) { |
John Hurley | 95e27a4 | 2019-04-02 23:53:20 +0100 | [diff] [blame] | 2258 | cls_flower.rule = |
| 2259 | flow_rule_alloc(tcf_exts_num_actions(&f->exts)); |
| 2260 | if (!cls_flower.rule) { |
| 2261 | __fl_put(f); |
| 2262 | return -ENOMEM; |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 2263 | } |
John Hurley | 95e27a4 | 2019-04-02 23:53:20 +0100 | [diff] [blame] | 2264 | |
| 2265 | 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] | 2266 | extack); |
John Hurley | 95e27a4 | 2019-04-02 23:53:20 +0100 | [diff] [blame] | 2267 | cls_flower.command = add ? |
Pablo Neira Ayuso | f9e3008 | 2019-07-09 22:55:49 +0200 | [diff] [blame] | 2268 | FLOW_CLS_REPLACE : FLOW_CLS_DESTROY; |
John Hurley | 95e27a4 | 2019-04-02 23:53:20 +0100 | [diff] [blame] | 2269 | cls_flower.cookie = (unsigned long)f; |
| 2270 | cls_flower.rule->match.dissector = &f->mask->dissector; |
| 2271 | cls_flower.rule->match.mask = &f->mask->key; |
| 2272 | cls_flower.rule->match.key = &f->mkey; |
| 2273 | |
Baowen Zheng | 9c1c0e1 | 2021-12-17 19:16:20 +0100 | [diff] [blame] | 2274 | err = tc_setup_offload_action(&cls_flower.rule->action, &f->exts); |
John Hurley | 95e27a4 | 2019-04-02 23:53:20 +0100 | [diff] [blame] | 2275 | if (err) { |
| 2276 | kfree(cls_flower.rule); |
| 2277 | if (tc_skip_sw(f->flags)) { |
| 2278 | NL_SET_ERR_MSG_MOD(extack, "Failed to setup flow action"); |
| 2279 | __fl_put(f); |
| 2280 | return err; |
| 2281 | } |
| 2282 | goto next_flow; |
| 2283 | } |
| 2284 | |
| 2285 | cls_flower.classid = f->res.classid; |
| 2286 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 2287 | err = tc_setup_cb_reoffload(block, tp, add, cb, |
| 2288 | TC_SETUP_CLSFLOWER, &cls_flower, |
| 2289 | cb_priv, &f->flags, |
| 2290 | &f->in_hw_count); |
Baowen Zheng | 9c1c0e1 | 2021-12-17 19:16:20 +0100 | [diff] [blame] | 2291 | tc_cleanup_offload_action(&cls_flower.rule->action); |
John Hurley | 95e27a4 | 2019-04-02 23:53:20 +0100 | [diff] [blame] | 2292 | kfree(cls_flower.rule); |
| 2293 | |
| 2294 | if (err) { |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 2295 | __fl_put(f); |
| 2296 | return err; |
John Hurley | 95e27a4 | 2019-04-02 23:53:20 +0100 | [diff] [blame] | 2297 | } |
John Hurley | 95e27a4 | 2019-04-02 23:53:20 +0100 | [diff] [blame] | 2298 | next_flow: |
John Hurley | 95e27a4 | 2019-04-02 23:53:20 +0100 | [diff] [blame] | 2299 | __fl_put(f); |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | return 0; |
| 2303 | } |
| 2304 | |
Vlad Buslov | a449a3e | 2019-08-26 16:45:00 +0300 | [diff] [blame] | 2305 | static void fl_hw_add(struct tcf_proto *tp, void *type_data) |
| 2306 | { |
| 2307 | struct flow_cls_offload *cls_flower = type_data; |
| 2308 | struct cls_fl_filter *f = |
| 2309 | (struct cls_fl_filter *) cls_flower->cookie; |
| 2310 | struct cls_fl_head *head = fl_head_dereference(tp); |
| 2311 | |
| 2312 | spin_lock(&tp->lock); |
| 2313 | list_add(&f->hw_list, &head->hw_filters); |
| 2314 | spin_unlock(&tp->lock); |
| 2315 | } |
| 2316 | |
| 2317 | static void fl_hw_del(struct tcf_proto *tp, void *type_data) |
| 2318 | { |
| 2319 | struct flow_cls_offload *cls_flower = type_data; |
| 2320 | struct cls_fl_filter *f = |
| 2321 | (struct cls_fl_filter *) cls_flower->cookie; |
| 2322 | |
| 2323 | spin_lock(&tp->lock); |
| 2324 | if (!list_empty(&f->hw_list)) |
| 2325 | list_del_init(&f->hw_list); |
| 2326 | spin_unlock(&tp->lock); |
| 2327 | } |
| 2328 | |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 2329 | static int fl_hw_create_tmplt(struct tcf_chain *chain, |
| 2330 | struct fl_flow_tmplt *tmplt) |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 2331 | { |
Pablo Neira Ayuso | f9e3008 | 2019-07-09 22:55:49 +0200 | [diff] [blame] | 2332 | struct flow_cls_offload cls_flower = {}; |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 2333 | struct tcf_block *block = chain->block; |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 2334 | |
Pablo Neira Ayuso | e3ab786 | 2019-02-02 12:50:45 +0100 | [diff] [blame] | 2335 | cls_flower.rule = flow_rule_alloc(0); |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 2336 | if (!cls_flower.rule) |
| 2337 | return -ENOMEM; |
| 2338 | |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 2339 | cls_flower.common.chain_index = chain->index; |
Pablo Neira Ayuso | f9e3008 | 2019-07-09 22:55:49 +0200 | [diff] [blame] | 2340 | cls_flower.command = FLOW_CLS_TMPLT_CREATE; |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 2341 | cls_flower.cookie = (unsigned long) tmplt; |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 2342 | cls_flower.rule->match.dissector = &tmplt->dissector; |
| 2343 | cls_flower.rule->match.mask = &tmplt->mask; |
| 2344 | cls_flower.rule->match.key = &tmplt->dummy_key; |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 2345 | |
| 2346 | /* We don't care if driver (any of them) fails to handle this |
| 2347 | * call. It serves just as a hint for it. |
| 2348 | */ |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 2349 | 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] | 2350 | kfree(cls_flower.rule); |
| 2351 | |
| 2352 | return 0; |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | static void fl_hw_destroy_tmplt(struct tcf_chain *chain, |
| 2356 | struct fl_flow_tmplt *tmplt) |
| 2357 | { |
Pablo Neira Ayuso | f9e3008 | 2019-07-09 22:55:49 +0200 | [diff] [blame] | 2358 | struct flow_cls_offload cls_flower = {}; |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 2359 | struct tcf_block *block = chain->block; |
| 2360 | |
| 2361 | cls_flower.common.chain_index = chain->index; |
Pablo Neira Ayuso | f9e3008 | 2019-07-09 22:55:49 +0200 | [diff] [blame] | 2362 | cls_flower.command = FLOW_CLS_TMPLT_DESTROY; |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 2363 | cls_flower.cookie = (unsigned long) tmplt; |
| 2364 | |
Vlad Buslov | 4011921 | 2019-08-26 16:44:59 +0300 | [diff] [blame] | 2365 | tc_setup_cb_call(block, TC_SETUP_CLSFLOWER, &cls_flower, false, true); |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 2366 | } |
| 2367 | |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 2368 | static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain, |
| 2369 | struct nlattr **tca, |
| 2370 | struct netlink_ext_ack *extack) |
| 2371 | { |
| 2372 | struct fl_flow_tmplt *tmplt; |
| 2373 | struct nlattr **tb; |
| 2374 | int err; |
| 2375 | |
| 2376 | if (!tca[TCA_OPTIONS]) |
| 2377 | return ERR_PTR(-EINVAL); |
| 2378 | |
| 2379 | tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL); |
| 2380 | if (!tb) |
| 2381 | return ERR_PTR(-ENOBUFS); |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 2382 | err = nla_parse_nested_deprecated(tb, TCA_FLOWER_MAX, |
| 2383 | tca[TCA_OPTIONS], fl_policy, NULL); |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 2384 | if (err) |
| 2385 | goto errout_tb; |
| 2386 | |
| 2387 | tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL); |
Dan Carpenter | 1cbc36a5 | 2018-08-03 22:27:55 +0300 | [diff] [blame] | 2388 | if (!tmplt) { |
| 2389 | err = -ENOMEM; |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 2390 | goto errout_tb; |
Dan Carpenter | 1cbc36a5 | 2018-08-03 22:27:55 +0300 | [diff] [blame] | 2391 | } |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 2392 | tmplt->chain = chain; |
| 2393 | err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack); |
| 2394 | if (err) |
| 2395 | goto errout_tmplt; |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 2396 | |
| 2397 | fl_init_dissector(&tmplt->dissector, &tmplt->mask); |
| 2398 | |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 2399 | err = fl_hw_create_tmplt(chain, tmplt); |
| 2400 | if (err) |
| 2401 | goto errout_tmplt; |
Jiri Pirko | 3473845 | 2018-07-23 09:23:11 +0200 | [diff] [blame] | 2402 | |
Pablo Neira Ayuso | 8f25662 | 2019-02-02 12:50:43 +0100 | [diff] [blame] | 2403 | kfree(tb); |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 2404 | return tmplt; |
| 2405 | |
| 2406 | errout_tmplt: |
| 2407 | kfree(tmplt); |
| 2408 | errout_tb: |
| 2409 | kfree(tb); |
| 2410 | return ERR_PTR(err); |
| 2411 | } |
| 2412 | |
| 2413 | static void fl_tmplt_destroy(void *tmplt_priv) |
| 2414 | { |
| 2415 | struct fl_flow_tmplt *tmplt = tmplt_priv; |
| 2416 | |
Cong Wang | 95278dd | 2018-10-02 12:50:19 -0700 | [diff] [blame] | 2417 | fl_hw_destroy_tmplt(tmplt->chain, tmplt); |
| 2418 | kfree(tmplt); |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 2419 | } |
| 2420 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2421 | static int fl_dump_key_val(struct sk_buff *skb, |
| 2422 | void *val, int val_type, |
| 2423 | void *mask, int mask_type, int len) |
| 2424 | { |
| 2425 | int err; |
| 2426 | |
| 2427 | if (!memchr_inv(mask, 0, len)) |
| 2428 | return 0; |
| 2429 | err = nla_put(skb, val_type, len, val); |
| 2430 | if (err) |
| 2431 | return err; |
| 2432 | if (mask_type != TCA_FLOWER_UNSPEC) { |
| 2433 | err = nla_put(skb, mask_type, len, mask); |
| 2434 | if (err) |
| 2435 | return err; |
| 2436 | } |
| 2437 | return 0; |
| 2438 | } |
| 2439 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 2440 | static int fl_dump_key_port_range(struct sk_buff *skb, struct fl_flow_key *key, |
| 2441 | struct fl_flow_key *mask) |
| 2442 | { |
Yoshiki Komachi | 8ffb055 | 2019-12-03 19:40:12 +0900 | [diff] [blame] | 2443 | if (fl_dump_key_val(skb, &key->tp_range.tp_min.dst, |
| 2444 | TCA_FLOWER_KEY_PORT_DST_MIN, |
| 2445 | &mask->tp_range.tp_min.dst, TCA_FLOWER_UNSPEC, |
| 2446 | sizeof(key->tp_range.tp_min.dst)) || |
| 2447 | fl_dump_key_val(skb, &key->tp_range.tp_max.dst, |
| 2448 | TCA_FLOWER_KEY_PORT_DST_MAX, |
| 2449 | &mask->tp_range.tp_max.dst, TCA_FLOWER_UNSPEC, |
| 2450 | sizeof(key->tp_range.tp_max.dst)) || |
| 2451 | fl_dump_key_val(skb, &key->tp_range.tp_min.src, |
| 2452 | TCA_FLOWER_KEY_PORT_SRC_MIN, |
| 2453 | &mask->tp_range.tp_min.src, TCA_FLOWER_UNSPEC, |
| 2454 | sizeof(key->tp_range.tp_min.src)) || |
| 2455 | fl_dump_key_val(skb, &key->tp_range.tp_max.src, |
| 2456 | TCA_FLOWER_KEY_PORT_SRC_MAX, |
| 2457 | &mask->tp_range.tp_max.src, TCA_FLOWER_UNSPEC, |
| 2458 | sizeof(key->tp_range.tp_max.src))) |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 2459 | return -1; |
| 2460 | |
| 2461 | return 0; |
| 2462 | } |
| 2463 | |
Guillaume Nault | 61aec25 | 2020-05-26 14:29:04 +0200 | [diff] [blame] | 2464 | static int fl_dump_key_mpls_opt_lse(struct sk_buff *skb, |
| 2465 | struct flow_dissector_key_mpls *mpls_key, |
| 2466 | struct flow_dissector_key_mpls *mpls_mask, |
| 2467 | u8 lse_index) |
| 2468 | { |
| 2469 | struct flow_dissector_mpls_lse *lse_mask = &mpls_mask->ls[lse_index]; |
| 2470 | struct flow_dissector_mpls_lse *lse_key = &mpls_key->ls[lse_index]; |
| 2471 | int err; |
| 2472 | |
| 2473 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_DEPTH, |
| 2474 | lse_index + 1); |
| 2475 | if (err) |
| 2476 | return err; |
| 2477 | |
| 2478 | if (lse_mask->mpls_ttl) { |
| 2479 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TTL, |
| 2480 | lse_key->mpls_ttl); |
| 2481 | if (err) |
| 2482 | return err; |
| 2483 | } |
| 2484 | if (lse_mask->mpls_bos) { |
| 2485 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_BOS, |
| 2486 | lse_key->mpls_bos); |
| 2487 | if (err) |
| 2488 | return err; |
| 2489 | } |
| 2490 | if (lse_mask->mpls_tc) { |
| 2491 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_TC, |
| 2492 | lse_key->mpls_tc); |
| 2493 | if (err) |
| 2494 | return err; |
| 2495 | } |
| 2496 | if (lse_mask->mpls_label) { |
Guillaume Nault | 7fdd375 | 2020-12-09 16:48:41 +0100 | [diff] [blame] | 2497 | err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_OPT_LSE_LABEL, |
| 2498 | lse_key->mpls_label); |
Guillaume Nault | 61aec25 | 2020-05-26 14:29:04 +0200 | [diff] [blame] | 2499 | if (err) |
| 2500 | return err; |
| 2501 | } |
| 2502 | |
| 2503 | return 0; |
| 2504 | } |
| 2505 | |
| 2506 | static int fl_dump_key_mpls_opts(struct sk_buff *skb, |
| 2507 | struct flow_dissector_key_mpls *mpls_key, |
| 2508 | struct flow_dissector_key_mpls *mpls_mask) |
| 2509 | { |
| 2510 | struct nlattr *opts; |
| 2511 | struct nlattr *lse; |
| 2512 | u8 lse_index; |
| 2513 | int err; |
| 2514 | |
| 2515 | opts = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS); |
| 2516 | if (!opts) |
| 2517 | return -EMSGSIZE; |
| 2518 | |
| 2519 | for (lse_index = 0; lse_index < FLOW_DIS_MPLS_MAX; lse_index++) { |
| 2520 | if (!(mpls_mask->used_lses & 1 << lse_index)) |
| 2521 | continue; |
| 2522 | |
| 2523 | lse = nla_nest_start(skb, TCA_FLOWER_KEY_MPLS_OPTS_LSE); |
| 2524 | if (!lse) { |
| 2525 | err = -EMSGSIZE; |
| 2526 | goto err_opts; |
| 2527 | } |
| 2528 | |
| 2529 | err = fl_dump_key_mpls_opt_lse(skb, mpls_key, mpls_mask, |
| 2530 | lse_index); |
| 2531 | if (err) |
| 2532 | goto err_opts_lse; |
| 2533 | nla_nest_end(skb, lse); |
| 2534 | } |
| 2535 | nla_nest_end(skb, opts); |
| 2536 | |
| 2537 | return 0; |
| 2538 | |
| 2539 | err_opts_lse: |
| 2540 | nla_nest_cancel(skb, lse); |
| 2541 | err_opts: |
| 2542 | nla_nest_cancel(skb, opts); |
| 2543 | |
| 2544 | return err; |
| 2545 | } |
| 2546 | |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 2547 | static int fl_dump_key_mpls(struct sk_buff *skb, |
| 2548 | struct flow_dissector_key_mpls *mpls_key, |
| 2549 | struct flow_dissector_key_mpls *mpls_mask) |
| 2550 | { |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 2551 | struct flow_dissector_mpls_lse *lse_mask; |
| 2552 | struct flow_dissector_mpls_lse *lse_key; |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 2553 | int err; |
| 2554 | |
Guillaume Nault | 61aec25 | 2020-05-26 14:29:04 +0200 | [diff] [blame] | 2555 | if (!mpls_mask->used_lses) |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 2556 | return 0; |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 2557 | |
| 2558 | lse_mask = &mpls_mask->ls[0]; |
| 2559 | lse_key = &mpls_key->ls[0]; |
| 2560 | |
Guillaume Nault | 61aec25 | 2020-05-26 14:29:04 +0200 | [diff] [blame] | 2561 | /* For backward compatibility, don't use the MPLS nested attributes if |
| 2562 | * the rule can be expressed using the old attributes. |
| 2563 | */ |
| 2564 | if (mpls_mask->used_lses & ~1 || |
| 2565 | (!lse_mask->mpls_ttl && !lse_mask->mpls_bos && |
| 2566 | !lse_mask->mpls_tc && !lse_mask->mpls_label)) |
| 2567 | return fl_dump_key_mpls_opts(skb, mpls_key, mpls_mask); |
| 2568 | |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 2569 | if (lse_mask->mpls_ttl) { |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 2570 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL, |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 2571 | lse_key->mpls_ttl); |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 2572 | if (err) |
| 2573 | return err; |
| 2574 | } |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 2575 | if (lse_mask->mpls_tc) { |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 2576 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC, |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 2577 | lse_key->mpls_tc); |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 2578 | if (err) |
| 2579 | return err; |
| 2580 | } |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 2581 | if (lse_mask->mpls_label) { |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 2582 | err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL, |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 2583 | lse_key->mpls_label); |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 2584 | if (err) |
| 2585 | return err; |
| 2586 | } |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 2587 | if (lse_mask->mpls_bos) { |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 2588 | err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS, |
Guillaume Nault | 58cff78 | 2020-05-26 14:29:00 +0200 | [diff] [blame] | 2589 | lse_key->mpls_bos); |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 2590 | if (err) |
| 2591 | return err; |
| 2592 | } |
| 2593 | return 0; |
| 2594 | } |
| 2595 | |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 2596 | static int fl_dump_key_ip(struct sk_buff *skb, bool encap, |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 2597 | struct flow_dissector_key_ip *key, |
| 2598 | struct flow_dissector_key_ip *mask) |
| 2599 | { |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 2600 | int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS; |
| 2601 | int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL; |
| 2602 | int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK; |
| 2603 | int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK; |
| 2604 | |
| 2605 | if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) || |
| 2606 | 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] | 2607 | return -1; |
| 2608 | |
| 2609 | return 0; |
| 2610 | } |
| 2611 | |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 2612 | static int fl_dump_key_vlan(struct sk_buff *skb, |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 2613 | int vlan_id_key, int vlan_prio_key, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 2614 | struct flow_dissector_key_vlan *vlan_key, |
| 2615 | struct flow_dissector_key_vlan *vlan_mask) |
| 2616 | { |
| 2617 | int err; |
| 2618 | |
| 2619 | if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask))) |
| 2620 | return 0; |
| 2621 | if (vlan_mask->vlan_id) { |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 2622 | err = nla_put_u16(skb, vlan_id_key, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 2623 | vlan_key->vlan_id); |
| 2624 | if (err) |
| 2625 | return err; |
| 2626 | } |
| 2627 | if (vlan_mask->vlan_priority) { |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 2628 | err = nla_put_u8(skb, vlan_prio_key, |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 2629 | vlan_key->vlan_priority); |
| 2630 | if (err) |
| 2631 | return err; |
| 2632 | } |
| 2633 | return 0; |
| 2634 | } |
| 2635 | |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 2636 | static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask, |
| 2637 | u32 *flower_key, u32 *flower_mask, |
| 2638 | u32 flower_flag_bit, u32 dissector_flag_bit) |
| 2639 | { |
| 2640 | if (dissector_mask & dissector_flag_bit) { |
| 2641 | *flower_mask |= flower_flag_bit; |
| 2642 | if (dissector_key & dissector_flag_bit) |
| 2643 | *flower_key |= flower_flag_bit; |
| 2644 | } |
| 2645 | } |
| 2646 | |
| 2647 | static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask) |
| 2648 | { |
| 2649 | u32 key, mask; |
| 2650 | __be32 _key, _mask; |
| 2651 | int err; |
| 2652 | |
| 2653 | if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask))) |
| 2654 | return 0; |
| 2655 | |
| 2656 | key = 0; |
| 2657 | mask = 0; |
| 2658 | |
| 2659 | fl_get_key_flag(flags_key, flags_mask, &key, &mask, |
| 2660 | TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT); |
Pieter Jansen van Vuuren | 459d153 | 2018-03-06 18:11:14 +0100 | [diff] [blame] | 2661 | fl_get_key_flag(flags_key, flags_mask, &key, &mask, |
| 2662 | TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST, |
| 2663 | FLOW_DIS_FIRST_FRAG); |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 2664 | |
| 2665 | _key = cpu_to_be32(key); |
| 2666 | _mask = cpu_to_be32(mask); |
| 2667 | |
| 2668 | err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key); |
| 2669 | if (err) |
| 2670 | return err; |
| 2671 | |
| 2672 | return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask); |
| 2673 | } |
| 2674 | |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 2675 | static int fl_dump_key_geneve_opt(struct sk_buff *skb, |
| 2676 | struct flow_dissector_key_enc_opts *enc_opts) |
| 2677 | { |
| 2678 | struct geneve_opt *opt; |
| 2679 | struct nlattr *nest; |
| 2680 | int opt_off = 0; |
| 2681 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2682 | 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] | 2683 | if (!nest) |
| 2684 | goto nla_put_failure; |
| 2685 | |
| 2686 | while (enc_opts->len > opt_off) { |
| 2687 | opt = (struct geneve_opt *)&enc_opts->data[opt_off]; |
| 2688 | |
| 2689 | if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS, |
| 2690 | opt->opt_class)) |
| 2691 | goto nla_put_failure; |
| 2692 | if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE, |
| 2693 | opt->type)) |
| 2694 | goto nla_put_failure; |
| 2695 | if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA, |
| 2696 | opt->length * 4, opt->opt_data)) |
| 2697 | goto nla_put_failure; |
| 2698 | |
| 2699 | opt_off += sizeof(struct geneve_opt) + opt->length * 4; |
| 2700 | } |
| 2701 | nla_nest_end(skb, nest); |
| 2702 | return 0; |
| 2703 | |
| 2704 | nla_put_failure: |
| 2705 | nla_nest_cancel(skb, nest); |
| 2706 | return -EMSGSIZE; |
| 2707 | } |
| 2708 | |
Xin Long | d8f9dfa | 2019-11-21 18:03:28 +0800 | [diff] [blame] | 2709 | static int fl_dump_key_vxlan_opt(struct sk_buff *skb, |
| 2710 | struct flow_dissector_key_enc_opts *enc_opts) |
| 2711 | { |
| 2712 | struct vxlan_metadata *md; |
| 2713 | struct nlattr *nest; |
| 2714 | |
| 2715 | nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_VXLAN); |
| 2716 | if (!nest) |
| 2717 | goto nla_put_failure; |
| 2718 | |
| 2719 | md = (struct vxlan_metadata *)&enc_opts->data[0]; |
| 2720 | if (nla_put_u32(skb, TCA_FLOWER_KEY_ENC_OPT_VXLAN_GBP, md->gbp)) |
| 2721 | goto nla_put_failure; |
| 2722 | |
| 2723 | nla_nest_end(skb, nest); |
| 2724 | return 0; |
| 2725 | |
| 2726 | nla_put_failure: |
| 2727 | nla_nest_cancel(skb, nest); |
| 2728 | return -EMSGSIZE; |
| 2729 | } |
| 2730 | |
Xin Long | 79b1011 | 2019-11-21 18:03:29 +0800 | [diff] [blame] | 2731 | static int fl_dump_key_erspan_opt(struct sk_buff *skb, |
| 2732 | struct flow_dissector_key_enc_opts *enc_opts) |
| 2733 | { |
| 2734 | struct erspan_metadata *md; |
| 2735 | struct nlattr *nest; |
| 2736 | |
| 2737 | nest = nla_nest_start_noflag(skb, TCA_FLOWER_KEY_ENC_OPTS_ERSPAN); |
| 2738 | if (!nest) |
| 2739 | goto nla_put_failure; |
| 2740 | |
| 2741 | md = (struct erspan_metadata *)&enc_opts->data[0]; |
| 2742 | if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_VER, md->version)) |
| 2743 | goto nla_put_failure; |
| 2744 | |
| 2745 | if (md->version == 1 && |
| 2746 | nla_put_be32(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_INDEX, md->u.index)) |
| 2747 | goto nla_put_failure; |
| 2748 | |
| 2749 | if (md->version == 2 && |
| 2750 | (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_DIR, |
| 2751 | md->u.md2.dir) || |
| 2752 | nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_ERSPAN_HWID, |
| 2753 | get_hwid(&md->u.md2)))) |
| 2754 | goto nla_put_failure; |
| 2755 | |
| 2756 | nla_nest_end(skb, nest); |
| 2757 | return 0; |
| 2758 | |
| 2759 | nla_put_failure: |
| 2760 | nla_nest_cancel(skb, nest); |
| 2761 | return -EMSGSIZE; |
| 2762 | } |
| 2763 | |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 2764 | static int fl_dump_key_ct(struct sk_buff *skb, |
| 2765 | struct flow_dissector_key_ct *key, |
| 2766 | struct flow_dissector_key_ct *mask) |
| 2767 | { |
| 2768 | if (IS_ENABLED(CONFIG_NF_CONNTRACK) && |
| 2769 | fl_dump_key_val(skb, &key->ct_state, TCA_FLOWER_KEY_CT_STATE, |
| 2770 | &mask->ct_state, TCA_FLOWER_KEY_CT_STATE_MASK, |
| 2771 | sizeof(key->ct_state))) |
| 2772 | goto nla_put_failure; |
| 2773 | |
| 2774 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) && |
| 2775 | fl_dump_key_val(skb, &key->ct_zone, TCA_FLOWER_KEY_CT_ZONE, |
| 2776 | &mask->ct_zone, TCA_FLOWER_KEY_CT_ZONE_MASK, |
| 2777 | sizeof(key->ct_zone))) |
| 2778 | goto nla_put_failure; |
| 2779 | |
| 2780 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && |
| 2781 | fl_dump_key_val(skb, &key->ct_mark, TCA_FLOWER_KEY_CT_MARK, |
| 2782 | &mask->ct_mark, TCA_FLOWER_KEY_CT_MARK_MASK, |
| 2783 | sizeof(key->ct_mark))) |
| 2784 | goto nla_put_failure; |
| 2785 | |
| 2786 | if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) && |
| 2787 | fl_dump_key_val(skb, &key->ct_labels, TCA_FLOWER_KEY_CT_LABELS, |
| 2788 | &mask->ct_labels, TCA_FLOWER_KEY_CT_LABELS_MASK, |
| 2789 | sizeof(key->ct_labels))) |
| 2790 | goto nla_put_failure; |
| 2791 | |
| 2792 | return 0; |
| 2793 | |
| 2794 | nla_put_failure: |
| 2795 | return -EMSGSIZE; |
| 2796 | } |
| 2797 | |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 2798 | static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type, |
| 2799 | struct flow_dissector_key_enc_opts *enc_opts) |
| 2800 | { |
| 2801 | struct nlattr *nest; |
| 2802 | int err; |
| 2803 | |
| 2804 | if (!enc_opts->len) |
| 2805 | return 0; |
| 2806 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 2807 | nest = nla_nest_start_noflag(skb, enc_opt_type); |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 2808 | if (!nest) |
| 2809 | goto nla_put_failure; |
| 2810 | |
| 2811 | switch (enc_opts->dst_opt_type) { |
| 2812 | case TUNNEL_GENEVE_OPT: |
| 2813 | err = fl_dump_key_geneve_opt(skb, enc_opts); |
| 2814 | if (err) |
| 2815 | goto nla_put_failure; |
| 2816 | break; |
Xin Long | d8f9dfa | 2019-11-21 18:03:28 +0800 | [diff] [blame] | 2817 | case TUNNEL_VXLAN_OPT: |
| 2818 | err = fl_dump_key_vxlan_opt(skb, enc_opts); |
| 2819 | if (err) |
| 2820 | goto nla_put_failure; |
| 2821 | break; |
Xin Long | 79b1011 | 2019-11-21 18:03:29 +0800 | [diff] [blame] | 2822 | case TUNNEL_ERSPAN_OPT: |
| 2823 | err = fl_dump_key_erspan_opt(skb, enc_opts); |
| 2824 | if (err) |
| 2825 | goto nla_put_failure; |
| 2826 | break; |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 2827 | default: |
| 2828 | goto nla_put_failure; |
| 2829 | } |
| 2830 | nla_nest_end(skb, nest); |
| 2831 | return 0; |
| 2832 | |
| 2833 | nla_put_failure: |
| 2834 | nla_nest_cancel(skb, nest); |
| 2835 | return -EMSGSIZE; |
| 2836 | } |
| 2837 | |
| 2838 | static int fl_dump_key_enc_opt(struct sk_buff *skb, |
| 2839 | struct flow_dissector_key_enc_opts *key_opts, |
| 2840 | struct flow_dissector_key_enc_opts *msk_opts) |
| 2841 | { |
| 2842 | int err; |
| 2843 | |
| 2844 | err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts); |
| 2845 | if (err) |
| 2846 | return err; |
| 2847 | |
| 2848 | return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts); |
| 2849 | } |
| 2850 | |
Jiri Pirko | f574908 | 2018-07-23 09:23:08 +0200 | [diff] [blame] | 2851 | static int fl_dump_key(struct sk_buff *skb, struct net *net, |
| 2852 | struct fl_flow_key *key, struct fl_flow_key *mask) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2853 | { |
Jiri Pirko | 8212ed7 | 2019-06-19 09:41:03 +0300 | [diff] [blame] | 2854 | if (mask->meta.ingress_ifindex) { |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2855 | struct net_device *dev; |
| 2856 | |
Jiri Pirko | 8212ed7 | 2019-06-19 09:41:03 +0300 | [diff] [blame] | 2857 | dev = __dev_get_by_index(net, key->meta.ingress_ifindex); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2858 | if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name)) |
| 2859 | goto nla_put_failure; |
| 2860 | } |
| 2861 | |
| 2862 | if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST, |
| 2863 | mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK, |
| 2864 | sizeof(key->eth.dst)) || |
| 2865 | fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC, |
| 2866 | mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK, |
| 2867 | sizeof(key->eth.src)) || |
| 2868 | fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE, |
| 2869 | &mask->basic.n_proto, TCA_FLOWER_UNSPEC, |
| 2870 | sizeof(key->basic.n_proto))) |
| 2871 | goto nla_put_failure; |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 2872 | |
Benjamin LaHaise | a577d8f | 2017-04-22 16:52:47 -0400 | [diff] [blame] | 2873 | if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls)) |
| 2874 | goto nla_put_failure; |
| 2875 | |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 2876 | if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID, |
| 2877 | TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan)) |
Hadar Hen Zion | 9399ae9 | 2016-08-17 13:36:13 +0300 | [diff] [blame] | 2878 | goto nla_put_failure; |
| 2879 | |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 2880 | if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID, |
| 2881 | TCA_FLOWER_KEY_CVLAN_PRIO, |
| 2882 | &key->cvlan, &mask->cvlan) || |
| 2883 | (mask->cvlan.vlan_tpid && |
Jianbo Liu | 158abbf | 2018-07-25 02:31:25 +0000 | [diff] [blame] | 2884 | nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, |
| 2885 | key->cvlan.vlan_tpid))) |
Jianbo Liu | d306951 | 2018-07-06 05:38:15 +0000 | [diff] [blame] | 2886 | goto nla_put_failure; |
| 2887 | |
Jianbo Liu | 5e9a0fe | 2018-07-09 02:26:20 +0000 | [diff] [blame] | 2888 | if (mask->basic.n_proto) { |
| 2889 | if (mask->cvlan.vlan_tpid) { |
| 2890 | if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE, |
| 2891 | key->basic.n_proto)) |
| 2892 | goto nla_put_failure; |
| 2893 | } else if (mask->vlan.vlan_tpid) { |
| 2894 | if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE, |
| 2895 | key->basic.n_proto)) |
| 2896 | goto nla_put_failure; |
| 2897 | } |
Jianbo Liu | d64efd0 | 2018-07-06 05:38:16 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2900 | if ((key->basic.n_proto == htons(ETH_P_IP) || |
| 2901 | key->basic.n_proto == htons(ETH_P_IPV6)) && |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 2902 | (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] | 2903 | &mask->basic.ip_proto, TCA_FLOWER_UNSPEC, |
Or Gerlitz | 4d80cc0 | 2017-06-01 21:37:38 +0300 | [diff] [blame] | 2904 | sizeof(key->basic.ip_proto)) || |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 2905 | fl_dump_key_ip(skb, false, &key->ip, &mask->ip))) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2906 | goto nla_put_failure; |
| 2907 | |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 2908 | if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2909 | (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC, |
| 2910 | &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK, |
| 2911 | sizeof(key->ipv4.src)) || |
| 2912 | fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST, |
| 2913 | &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK, |
| 2914 | sizeof(key->ipv4.dst)))) |
| 2915 | goto nla_put_failure; |
Tom Herbert | c3f8324 | 2015-06-04 09:16:40 -0700 | [diff] [blame] | 2916 | else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2917 | (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC, |
| 2918 | &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK, |
| 2919 | sizeof(key->ipv6.src)) || |
| 2920 | fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST, |
| 2921 | &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK, |
| 2922 | sizeof(key->ipv6.dst)))) |
| 2923 | goto nla_put_failure; |
| 2924 | |
| 2925 | if (key->basic.ip_proto == IPPROTO_TCP && |
| 2926 | (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] | 2927 | &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2928 | sizeof(key->tp.src)) || |
| 2929 | 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] | 2930 | &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK, |
Jiri Pirko | fdfc7dd | 2017-05-23 18:40:45 +0200 | [diff] [blame] | 2931 | sizeof(key->tp.dst)) || |
| 2932 | fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS, |
| 2933 | &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK, |
| 2934 | sizeof(key->tcp.flags)))) |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2935 | goto nla_put_failure; |
| 2936 | else if (key->basic.ip_proto == IPPROTO_UDP && |
| 2937 | (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] | 2938 | &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2939 | sizeof(key->tp.src)) || |
| 2940 | 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] | 2941 | &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2942 | sizeof(key->tp.dst)))) |
| 2943 | goto nla_put_failure; |
Simon Horman | 5976c5f | 2016-11-03 13:24:21 +0100 | [diff] [blame] | 2944 | else if (key->basic.ip_proto == IPPROTO_SCTP && |
| 2945 | (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC, |
| 2946 | &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK, |
| 2947 | sizeof(key->tp.src)) || |
| 2948 | fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST, |
| 2949 | &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK, |
| 2950 | sizeof(key->tp.dst)))) |
| 2951 | goto nla_put_failure; |
Simon Horman | 7b68488 | 2016-12-07 13:48:28 +0100 | [diff] [blame] | 2952 | else if (key->basic.n_proto == htons(ETH_P_IP) && |
| 2953 | key->basic.ip_proto == IPPROTO_ICMP && |
| 2954 | (fl_dump_key_val(skb, &key->icmp.type, |
| 2955 | TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type, |
| 2956 | TCA_FLOWER_KEY_ICMPV4_TYPE_MASK, |
| 2957 | sizeof(key->icmp.type)) || |
| 2958 | fl_dump_key_val(skb, &key->icmp.code, |
| 2959 | TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code, |
| 2960 | TCA_FLOWER_KEY_ICMPV4_CODE_MASK, |
| 2961 | sizeof(key->icmp.code)))) |
| 2962 | goto nla_put_failure; |
| 2963 | else if (key->basic.n_proto == htons(ETH_P_IPV6) && |
| 2964 | key->basic.ip_proto == IPPROTO_ICMPV6 && |
| 2965 | (fl_dump_key_val(skb, &key->icmp.type, |
| 2966 | TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type, |
| 2967 | TCA_FLOWER_KEY_ICMPV6_TYPE_MASK, |
| 2968 | sizeof(key->icmp.type)) || |
| 2969 | fl_dump_key_val(skb, &key->icmp.code, |
| 2970 | TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code, |
| 2971 | TCA_FLOWER_KEY_ICMPV6_CODE_MASK, |
| 2972 | sizeof(key->icmp.code)))) |
| 2973 | goto nla_put_failure; |
Simon Horman | 99d3132 | 2017-01-11 14:05:43 +0100 | [diff] [blame] | 2974 | else if ((key->basic.n_proto == htons(ETH_P_ARP) || |
| 2975 | key->basic.n_proto == htons(ETH_P_RARP)) && |
| 2976 | (fl_dump_key_val(skb, &key->arp.sip, |
| 2977 | TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip, |
| 2978 | TCA_FLOWER_KEY_ARP_SIP_MASK, |
| 2979 | sizeof(key->arp.sip)) || |
| 2980 | fl_dump_key_val(skb, &key->arp.tip, |
| 2981 | TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip, |
| 2982 | TCA_FLOWER_KEY_ARP_TIP_MASK, |
| 2983 | sizeof(key->arp.tip)) || |
| 2984 | fl_dump_key_val(skb, &key->arp.op, |
| 2985 | TCA_FLOWER_KEY_ARP_OP, &mask->arp.op, |
| 2986 | TCA_FLOWER_KEY_ARP_OP_MASK, |
| 2987 | sizeof(key->arp.op)) || |
| 2988 | fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA, |
| 2989 | mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK, |
| 2990 | sizeof(key->arp.sha)) || |
| 2991 | fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA, |
| 2992 | mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK, |
| 2993 | sizeof(key->arp.tha)))) |
| 2994 | goto nla_put_failure; |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 2995 | |
Amritha Nambiar | 5c72299 | 2018-11-12 16:15:55 -0800 | [diff] [blame] | 2996 | if ((key->basic.ip_proto == IPPROTO_TCP || |
| 2997 | key->basic.ip_proto == IPPROTO_UDP || |
| 2998 | key->basic.ip_proto == IPPROTO_SCTP) && |
| 2999 | fl_dump_key_port_range(skb, key, mask)) |
| 3000 | goto nla_put_failure; |
| 3001 | |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 3002 | if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS && |
| 3003 | (fl_dump_key_val(skb, &key->enc_ipv4.src, |
| 3004 | TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src, |
| 3005 | TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK, |
| 3006 | sizeof(key->enc_ipv4.src)) || |
| 3007 | fl_dump_key_val(skb, &key->enc_ipv4.dst, |
| 3008 | TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst, |
| 3009 | TCA_FLOWER_KEY_ENC_IPV4_DST_MASK, |
| 3010 | sizeof(key->enc_ipv4.dst)))) |
| 3011 | goto nla_put_failure; |
| 3012 | else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS && |
| 3013 | (fl_dump_key_val(skb, &key->enc_ipv6.src, |
| 3014 | TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src, |
| 3015 | TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK, |
| 3016 | sizeof(key->enc_ipv6.src)) || |
| 3017 | fl_dump_key_val(skb, &key->enc_ipv6.dst, |
| 3018 | TCA_FLOWER_KEY_ENC_IPV6_DST, |
| 3019 | &mask->enc_ipv6.dst, |
| 3020 | TCA_FLOWER_KEY_ENC_IPV6_DST_MASK, |
| 3021 | sizeof(key->enc_ipv6.dst)))) |
| 3022 | goto nla_put_failure; |
| 3023 | |
| 3024 | 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] | 3025 | &mask->enc_key_id, TCA_FLOWER_UNSPEC, |
Hadar Hen Zion | f4d997f | 2016-11-07 15:14:39 +0200 | [diff] [blame] | 3026 | sizeof(key->enc_key_id)) || |
| 3027 | fl_dump_key_val(skb, &key->enc_tp.src, |
| 3028 | TCA_FLOWER_KEY_ENC_UDP_SRC_PORT, |
| 3029 | &mask->enc_tp.src, |
| 3030 | TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK, |
| 3031 | sizeof(key->enc_tp.src)) || |
| 3032 | fl_dump_key_val(skb, &key->enc_tp.dst, |
| 3033 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT, |
| 3034 | &mask->enc_tp.dst, |
| 3035 | TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK, |
Or Gerlitz | 0e2c17b | 2018-07-17 19:27:18 +0300 | [diff] [blame] | 3036 | sizeof(key->enc_tp.dst)) || |
Pieter Jansen van Vuuren | 0a6e777 | 2018-08-07 17:36:01 +0200 | [diff] [blame] | 3037 | fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) || |
| 3038 | fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts)) |
Amir Vadai | bc3103f | 2016-09-08 16:23:47 +0300 | [diff] [blame] | 3039 | goto nla_put_failure; |
| 3040 | |
Paul Blakey | e0ace68 | 2019-07-09 10:30:50 +0300 | [diff] [blame] | 3041 | if (fl_dump_key_ct(skb, &key->ct, &mask->ct)) |
| 3042 | goto nla_put_failure; |
| 3043 | |
Or Gerlitz | faa3ffc | 2016-12-07 14:03:10 +0200 | [diff] [blame] | 3044 | if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags)) |
| 3045 | goto nla_put_failure; |
| 3046 | |
Ariel Levkovich | 5923b8f | 2020-07-23 01:03:01 +0300 | [diff] [blame] | 3047 | if (fl_dump_key_val(skb, &key->hash.hash, TCA_FLOWER_KEY_HASH, |
| 3048 | &mask->hash.hash, TCA_FLOWER_KEY_HASH_MASK, |
| 3049 | sizeof(key->hash.hash))) |
| 3050 | goto nla_put_failure; |
| 3051 | |
Jiri Pirko | f574908 | 2018-07-23 09:23:08 +0200 | [diff] [blame] | 3052 | return 0; |
| 3053 | |
| 3054 | nla_put_failure: |
| 3055 | return -EMSGSIZE; |
| 3056 | } |
| 3057 | |
| 3058 | 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] | 3059 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
Jiri Pirko | f574908 | 2018-07-23 09:23:08 +0200 | [diff] [blame] | 3060 | { |
| 3061 | struct cls_fl_filter *f = fh; |
| 3062 | struct nlattr *nest; |
| 3063 | struct fl_flow_key *key, *mask; |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 3064 | bool skip_hw; |
Jiri Pirko | f574908 | 2018-07-23 09:23:08 +0200 | [diff] [blame] | 3065 | |
| 3066 | if (!f) |
| 3067 | return skb->len; |
| 3068 | |
| 3069 | t->tcm_handle = f->handle; |
| 3070 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3071 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
Jiri Pirko | f574908 | 2018-07-23 09:23:08 +0200 | [diff] [blame] | 3072 | if (!nest) |
| 3073 | goto nla_put_failure; |
| 3074 | |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 3075 | spin_lock(&tp->lock); |
| 3076 | |
Jiri Pirko | f574908 | 2018-07-23 09:23:08 +0200 | [diff] [blame] | 3077 | if (f->res.classid && |
| 3078 | nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid)) |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 3079 | goto nla_put_failure_locked; |
Jiri Pirko | f574908 | 2018-07-23 09:23:08 +0200 | [diff] [blame] | 3080 | |
| 3081 | key = &f->key; |
| 3082 | mask = &f->mask->key; |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 3083 | skip_hw = tc_skip_hw(f->flags); |
Jiri Pirko | f574908 | 2018-07-23 09:23:08 +0200 | [diff] [blame] | 3084 | |
| 3085 | if (fl_dump_key(skb, net, key, mask)) |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 3086 | goto nla_put_failure_locked; |
Jiri Pirko | f574908 | 2018-07-23 09:23:08 +0200 | [diff] [blame] | 3087 | |
Or Gerlitz | 749e672 | 2017-02-16 10:31:10 +0200 | [diff] [blame] | 3088 | if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags)) |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 3089 | goto nla_put_failure_locked; |
| 3090 | |
| 3091 | spin_unlock(&tp->lock); |
| 3092 | |
| 3093 | if (!skip_hw) |
Vlad Buslov | c24e43d8 | 2019-03-21 15:17:43 +0200 | [diff] [blame] | 3094 | fl_hw_update_stats(tp, f, rtnl_held); |
Amir Vadai | e69985c | 2016-06-05 17:11:18 +0300 | [diff] [blame] | 3095 | |
Vlad Buslov | 86c5536 | 2018-09-07 17:22:21 +0300 | [diff] [blame] | 3096 | if (nla_put_u32(skb, TCA_FLOWER_IN_HW_COUNT, f->in_hw_count)) |
| 3097 | goto nla_put_failure; |
| 3098 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 3099 | if (tcf_exts_dump(skb, &f->exts)) |
| 3100 | goto nla_put_failure; |
| 3101 | |
| 3102 | nla_nest_end(skb, nest); |
| 3103 | |
| 3104 | if (tcf_exts_dump_stats(skb, &f->exts) < 0) |
| 3105 | goto nla_put_failure; |
| 3106 | |
| 3107 | return skb->len; |
| 3108 | |
Vlad Buslov | 3d81e71 | 2019-03-21 15:17:42 +0200 | [diff] [blame] | 3109 | nla_put_failure_locked: |
| 3110 | spin_unlock(&tp->lock); |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 3111 | nla_put_failure: |
| 3112 | nla_nest_cancel(skb, nest); |
| 3113 | return -1; |
| 3114 | } |
| 3115 | |
Vlad Buslov | 0348451 | 2020-05-15 14:40:13 +0300 | [diff] [blame] | 3116 | static int fl_terse_dump(struct net *net, struct tcf_proto *tp, void *fh, |
| 3117 | struct sk_buff *skb, struct tcmsg *t, bool rtnl_held) |
| 3118 | { |
| 3119 | struct cls_fl_filter *f = fh; |
| 3120 | struct nlattr *nest; |
| 3121 | bool skip_hw; |
| 3122 | |
| 3123 | if (!f) |
| 3124 | return skb->len; |
| 3125 | |
| 3126 | t->tcm_handle = f->handle; |
| 3127 | |
| 3128 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
| 3129 | if (!nest) |
| 3130 | goto nla_put_failure; |
| 3131 | |
| 3132 | spin_lock(&tp->lock); |
| 3133 | |
| 3134 | skip_hw = tc_skip_hw(f->flags); |
| 3135 | |
| 3136 | if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags)) |
| 3137 | goto nla_put_failure_locked; |
| 3138 | |
| 3139 | spin_unlock(&tp->lock); |
| 3140 | |
| 3141 | if (!skip_hw) |
| 3142 | fl_hw_update_stats(tp, f, rtnl_held); |
| 3143 | |
| 3144 | if (tcf_exts_terse_dump(skb, &f->exts)) |
| 3145 | goto nla_put_failure; |
| 3146 | |
| 3147 | nla_nest_end(skb, nest); |
| 3148 | |
| 3149 | return skb->len; |
| 3150 | |
| 3151 | nla_put_failure_locked: |
| 3152 | spin_unlock(&tp->lock); |
| 3153 | nla_put_failure: |
| 3154 | nla_nest_cancel(skb, nest); |
| 3155 | return -1; |
| 3156 | } |
| 3157 | |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 3158 | static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv) |
| 3159 | { |
| 3160 | struct fl_flow_tmplt *tmplt = tmplt_priv; |
| 3161 | struct fl_flow_key *key, *mask; |
| 3162 | struct nlattr *nest; |
| 3163 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3164 | nest = nla_nest_start_noflag(skb, TCA_OPTIONS); |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 3165 | if (!nest) |
| 3166 | goto nla_put_failure; |
| 3167 | |
| 3168 | key = &tmplt->dummy_key; |
| 3169 | mask = &tmplt->mask; |
| 3170 | |
| 3171 | if (fl_dump_key(skb, net, key, mask)) |
| 3172 | goto nla_put_failure; |
| 3173 | |
| 3174 | nla_nest_end(skb, nest); |
| 3175 | |
| 3176 | return skb->len; |
| 3177 | |
| 3178 | nla_put_failure: |
| 3179 | nla_nest_cancel(skb, nest); |
| 3180 | return -EMSGSIZE; |
| 3181 | } |
| 3182 | |
Cong Wang | 2e24cd7 | 2020-01-23 16:26:18 -0800 | [diff] [blame] | 3183 | static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q, |
| 3184 | unsigned long base) |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 3185 | { |
| 3186 | struct cls_fl_filter *f = fh; |
| 3187 | |
Cong Wang | 2e24cd7 | 2020-01-23 16:26:18 -0800 | [diff] [blame] | 3188 | if (f && f->res.classid == classid) { |
| 3189 | if (cl) |
| 3190 | __tcf_bind_filter(q, &f->res, base); |
| 3191 | else |
| 3192 | __tcf_unbind_filter(q, &f->res); |
| 3193 | } |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 3194 | } |
| 3195 | |
Davide Caratti | a5b72a0 | 2019-12-28 16:36:58 +0100 | [diff] [blame] | 3196 | static bool fl_delete_empty(struct tcf_proto *tp) |
| 3197 | { |
| 3198 | struct cls_fl_head *head = fl_head_dereference(tp); |
| 3199 | |
| 3200 | spin_lock(&tp->lock); |
| 3201 | tp->deleting = idr_is_empty(&head->handle_idr); |
| 3202 | spin_unlock(&tp->lock); |
| 3203 | |
| 3204 | return tp->deleting; |
| 3205 | } |
| 3206 | |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 3207 | static struct tcf_proto_ops cls_fl_ops __read_mostly = { |
| 3208 | .kind = "flower", |
| 3209 | .classify = fl_classify, |
| 3210 | .init = fl_init, |
| 3211 | .destroy = fl_destroy, |
| 3212 | .get = fl_get, |
Vlad Buslov | 0617755 | 2019-03-21 15:17:35 +0200 | [diff] [blame] | 3213 | .put = fl_put, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 3214 | .change = fl_change, |
| 3215 | .delete = fl_delete, |
Davide Caratti | a5b72a0 | 2019-12-28 16:36:58 +0100 | [diff] [blame] | 3216 | .delete_empty = fl_delete_empty, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 3217 | .walk = fl_walk, |
John Hurley | 31533cb | 2018-06-25 14:30:06 -0700 | [diff] [blame] | 3218 | .reoffload = fl_reoffload, |
Vlad Buslov | a449a3e | 2019-08-26 16:45:00 +0300 | [diff] [blame] | 3219 | .hw_add = fl_hw_add, |
| 3220 | .hw_del = fl_hw_del, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 3221 | .dump = fl_dump, |
Vlad Buslov | 0348451 | 2020-05-15 14:40:13 +0300 | [diff] [blame] | 3222 | .terse_dump = fl_terse_dump, |
Cong Wang | 07d79fc | 2017-08-30 14:30:36 -0700 | [diff] [blame] | 3223 | .bind_class = fl_bind_class, |
Jiri Pirko | b95ec7e | 2018-07-23 09:23:10 +0200 | [diff] [blame] | 3224 | .tmplt_create = fl_tmplt_create, |
| 3225 | .tmplt_destroy = fl_tmplt_destroy, |
| 3226 | .tmplt_dump = fl_tmplt_dump, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 3227 | .owner = THIS_MODULE, |
Vlad Buslov | 9214919 | 2019-03-21 15:17:44 +0200 | [diff] [blame] | 3228 | .flags = TCF_PROTO_OPS_DOIT_UNLOCKED, |
Jiri Pirko | 77b9900 | 2015-05-12 14:56:21 +0200 | [diff] [blame] | 3229 | }; |
| 3230 | |
| 3231 | static int __init cls_fl_init(void) |
| 3232 | { |
| 3233 | return register_tcf_proto_ops(&cls_fl_ops); |
| 3234 | } |
| 3235 | |
| 3236 | static void __exit cls_fl_exit(void) |
| 3237 | { |
| 3238 | unregister_tcf_proto_ops(&cls_fl_ops); |
| 3239 | } |
| 3240 | |
| 3241 | module_init(cls_fl_init); |
| 3242 | module_exit(cls_fl_exit); |
| 3243 | |
| 3244 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); |
| 3245 | MODULE_DESCRIPTION("Flower classifier"); |
| 3246 | MODULE_LICENSE("GPL v2"); |