]>
Commit | Line | Data |
---|---|---|
a097c0b2 | 1 | /* |
05499369 | 2 | * Copyright (c) 2007-2014 Nicira, Inc. |
a097c0b2 PS |
3 | * |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of version 2 of the GNU General Public | |
6 | * License as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
16 | * 02110-1301, USA | |
17 | */ | |
18 | ||
0a0857df JP |
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
20 | ||
a097c0b2 PS |
21 | #include <linux/uaccess.h> |
22 | #include <linux/netdevice.h> | |
23 | #include <linux/etherdevice.h> | |
24 | #include <linux/if_ether.h> | |
25 | #include <linux/if_vlan.h> | |
26 | #include <net/llc_pdu.h> | |
27 | #include <linux/kernel.h> | |
28 | #include <linux/jhash.h> | |
29 | #include <linux/jiffies.h> | |
30 | #include <linux/llc.h> | |
31 | #include <linux/module.h> | |
32 | #include <linux/in.h> | |
33 | #include <linux/rcupdate.h> | |
34 | #include <linux/if_arp.h> | |
35 | #include <linux/ip.h> | |
36 | #include <linux/ipv6.h> | |
37 | #include <linux/sctp.h> | |
38 | #include <linux/tcp.h> | |
39 | #include <linux/udp.h> | |
40 | #include <linux/icmp.h> | |
41 | #include <linux/icmpv6.h> | |
42 | #include <linux/rculist.h> | |
c1fc1411 | 43 | #include <net/geneve.h> |
a097c0b2 PS |
44 | #include <net/ip.h> |
45 | #include <net/ipv6.h> | |
46 | #include <net/ndisc.h> | |
2baf0e0c | 47 | #include <net/mpls.h> |
a097c0b2 | 48 | |
2baf0e0c PS |
49 | #include "datapath.h" |
50 | #include "flow.h" | |
a097c0b2 PS |
51 | #include "flow_netlink.h" |
52 | ||
f3ccd17d PS |
53 | static void update_range(struct sw_flow_match *match, |
54 | size_t offset, size_t size, bool is_mask) | |
a097c0b2 | 55 | { |
f3ccd17d | 56 | struct sw_flow_key_range *range; |
a097c0b2 PS |
57 | size_t start = rounddown(offset, sizeof(long)); |
58 | size_t end = roundup(offset + size, sizeof(long)); | |
59 | ||
60 | if (!is_mask) | |
61 | range = &match->range; | |
f3ccd17d | 62 | else |
a097c0b2 PS |
63 | range = &match->mask->range; |
64 | ||
a097c0b2 PS |
65 | if (range->start == range->end) { |
66 | range->start = start; | |
67 | range->end = end; | |
68 | return; | |
69 | } | |
70 | ||
71 | if (range->start > start) | |
72 | range->start = start; | |
73 | ||
74 | if (range->end < end) | |
75 | range->end = end; | |
76 | } | |
77 | ||
78 | #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \ | |
79 | do { \ | |
f3ccd17d PS |
80 | update_range(match, offsetof(struct sw_flow_key, field), \ |
81 | sizeof((match)->key->field), is_mask); \ | |
82 | if (is_mask) \ | |
83 | (match)->mask->key.field = value; \ | |
84 | else \ | |
a097c0b2 | 85 | (match)->key->field = value; \ |
a097c0b2 PS |
86 | } while (0) |
87 | ||
f3ccd17d PS |
88 | #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \ |
89 | do { \ | |
90 | update_range(match, offset, len, is_mask); \ | |
91 | if (is_mask) \ | |
92 | memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\ | |
93 | else \ | |
94 | memcpy((u8 *)(match)->key + offset, value_p, len); \ | |
a097c0b2 PS |
95 | } while (0) |
96 | ||
f3ccd17d | 97 | #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \ |
c1fc1411 JG |
98 | SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \ |
99 | value_p, len, is_mask) | |
100 | ||
f3ccd17d PS |
101 | #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \ |
102 | do { \ | |
103 | update_range(match, offsetof(struct sw_flow_key, field), \ | |
104 | sizeof((match)->key->field), is_mask); \ | |
105 | if (is_mask) \ | |
106 | memset((u8 *)&(match)->mask->key.field, value, \ | |
107 | sizeof((match)->mask->key.field)); \ | |
108 | else \ | |
0b496cda DDP |
109 | memset((u8 *)&(match)->key->field, value, \ |
110 | sizeof((match)->key->field)); \ | |
0b496cda DDP |
111 | } while (0) |
112 | ||
a097c0b2 | 113 | static bool match_validate(const struct sw_flow_match *match, |
9233cef7 | 114 | u64 key_attrs, u64 mask_attrs, bool log) |
a097c0b2 PS |
115 | { |
116 | u64 key_expected = 1ULL << OVS_KEY_ATTR_ETHERNET; | |
117 | u64 mask_allowed = key_attrs; /* At most allow all key attributes */ | |
118 | ||
119 | /* The following mask attributes allowed only if they | |
af465b67 PS |
120 | * pass the validation tests. |
121 | */ | |
a097c0b2 PS |
122 | mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4) |
123 | | (1ULL << OVS_KEY_ATTR_IPV6) | |
124 | | (1ULL << OVS_KEY_ATTR_TCP) | |
dc235f7f | 125 | | (1ULL << OVS_KEY_ATTR_TCP_FLAGS) |
a097c0b2 PS |
126 | | (1ULL << OVS_KEY_ATTR_UDP) |
127 | | (1ULL << OVS_KEY_ATTR_SCTP) | |
128 | | (1ULL << OVS_KEY_ATTR_ICMP) | |
129 | | (1ULL << OVS_KEY_ATTR_ICMPV6) | |
130 | | (1ULL << OVS_KEY_ATTR_ARP) | |
ccf43786 SH |
131 | | (1ULL << OVS_KEY_ATTR_ND) |
132 | | (1ULL << OVS_KEY_ATTR_MPLS)); | |
a097c0b2 PS |
133 | |
134 | /* Always allowed mask fields. */ | |
135 | mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL) | |
136 | | (1ULL << OVS_KEY_ATTR_IN_PORT) | |
137 | | (1ULL << OVS_KEY_ATTR_ETHERTYPE)); | |
138 | ||
139 | /* Check key attributes. */ | |
140 | if (match->key->eth.type == htons(ETH_P_ARP) | |
141 | || match->key->eth.type == htons(ETH_P_RARP)) { | |
142 | key_expected |= 1ULL << OVS_KEY_ATTR_ARP; | |
143 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) | |
144 | mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP; | |
145 | } | |
146 | ||
ccf43786 SH |
147 | if (eth_p_mpls(match->key->eth.type)) { |
148 | key_expected |= 1ULL << OVS_KEY_ATTR_MPLS; | |
149 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) | |
150 | mask_allowed |= 1ULL << OVS_KEY_ATTR_MPLS; | |
151 | } | |
152 | ||
a097c0b2 PS |
153 | if (match->key->eth.type == htons(ETH_P_IP)) { |
154 | key_expected |= 1ULL << OVS_KEY_ATTR_IPV4; | |
155 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) | |
156 | mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4; | |
157 | ||
158 | if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) { | |
159 | if (match->key->ip.proto == IPPROTO_UDP) { | |
160 | key_expected |= 1ULL << OVS_KEY_ATTR_UDP; | |
161 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
162 | mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP; | |
163 | } | |
164 | ||
165 | if (match->key->ip.proto == IPPROTO_SCTP) { | |
166 | key_expected |= 1ULL << OVS_KEY_ATTR_SCTP; | |
167 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
168 | mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP; | |
169 | } | |
170 | ||
171 | if (match->key->ip.proto == IPPROTO_TCP) { | |
172 | key_expected |= 1ULL << OVS_KEY_ATTR_TCP; | |
dc235f7f JR |
173 | key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS; |
174 | if (match->mask && (match->mask->key.ip.proto == 0xff)) { | |
a097c0b2 | 175 | mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP; |
dc235f7f JR |
176 | mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS; |
177 | } | |
a097c0b2 PS |
178 | } |
179 | ||
180 | if (match->key->ip.proto == IPPROTO_ICMP) { | |
181 | key_expected |= 1ULL << OVS_KEY_ATTR_ICMP; | |
182 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
183 | mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP; | |
184 | } | |
185 | } | |
186 | } | |
187 | ||
188 | if (match->key->eth.type == htons(ETH_P_IPV6)) { | |
189 | key_expected |= 1ULL << OVS_KEY_ATTR_IPV6; | |
190 | if (match->mask && (match->mask->key.eth.type == htons(0xffff))) | |
191 | mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6; | |
192 | ||
193 | if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) { | |
194 | if (match->key->ip.proto == IPPROTO_UDP) { | |
195 | key_expected |= 1ULL << OVS_KEY_ATTR_UDP; | |
196 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
197 | mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP; | |
198 | } | |
199 | ||
200 | if (match->key->ip.proto == IPPROTO_SCTP) { | |
201 | key_expected |= 1ULL << OVS_KEY_ATTR_SCTP; | |
202 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
203 | mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP; | |
204 | } | |
205 | ||
206 | if (match->key->ip.proto == IPPROTO_TCP) { | |
207 | key_expected |= 1ULL << OVS_KEY_ATTR_TCP; | |
dc235f7f JR |
208 | key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS; |
209 | if (match->mask && (match->mask->key.ip.proto == 0xff)) { | |
a097c0b2 | 210 | mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP; |
dc235f7f JR |
211 | mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS; |
212 | } | |
a097c0b2 PS |
213 | } |
214 | ||
215 | if (match->key->ip.proto == IPPROTO_ICMPV6) { | |
216 | key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6; | |
217 | if (match->mask && (match->mask->key.ip.proto == 0xff)) | |
218 | mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6; | |
219 | ||
708fb4c5 | 220 | if (match->key->tp.src == |
a097c0b2 | 221 | htons(NDISC_NEIGHBOUR_SOLICITATION) || |
708fb4c5 | 222 | match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) { |
a097c0b2 | 223 | key_expected |= 1ULL << OVS_KEY_ATTR_ND; |
560f3099 | 224 | if (match->mask && (match->mask->key.tp.src == htons(0xff))) |
a097c0b2 PS |
225 | mask_allowed |= 1ULL << OVS_KEY_ATTR_ND; |
226 | } | |
227 | } | |
228 | } | |
229 | } | |
230 | ||
231 | if ((key_attrs & key_expected) != key_expected) { | |
232 | /* Key attributes check failed. */ | |
7d16c847 | 233 | OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)", |
9233cef7 JR |
234 | (unsigned long long)key_attrs, |
235 | (unsigned long long)key_expected); | |
a097c0b2 PS |
236 | return false; |
237 | } | |
238 | ||
239 | if ((mask_attrs & mask_allowed) != mask_attrs) { | |
240 | /* Mask attributes check failed. */ | |
7d16c847 | 241 | OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)", |
9233cef7 JR |
242 | (unsigned long long)mask_attrs, |
243 | (unsigned long long)mask_allowed); | |
a097c0b2 PS |
244 | return false; |
245 | } | |
246 | ||
247 | return true; | |
248 | } | |
249 | ||
8b7ea2d4 WZ |
250 | size_t ovs_tun_key_attr_size(void) |
251 | { | |
252 | /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider | |
af465b67 PS |
253 | * updating this function. |
254 | */ | |
8b7ea2d4 WZ |
255 | return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */ |
256 | + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */ | |
257 | + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */ | |
258 | + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */ | |
259 | + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */ | |
260 | + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */ | |
261 | + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */ | |
262 | + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */ | |
263 | + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */ | |
264 | + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */ | |
265 | + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */ | |
266 | } | |
267 | ||
4e25b8c1 JS |
268 | size_t ovs_key_attr_size(void) |
269 | { | |
270 | /* Whenever adding new OVS_KEY_ FIELDS, we should consider | |
af465b67 PS |
271 | * updating this function. |
272 | */ | |
4e25b8c1 JS |
273 | BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22); |
274 | ||
275 | return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */ | |
276 | + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */ | |
8b7ea2d4 | 277 | + ovs_tun_key_attr_size() |
4e25b8c1 JS |
278 | + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */ |
279 | + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */ | |
280 | + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */ | |
281 | + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */ | |
282 | + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */ | |
283 | + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ | |
284 | + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */ | |
285 | + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */ | |
286 | + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ | |
287 | + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */ | |
288 | + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */ | |
289 | + nla_total_size(28); /* OVS_KEY_ATTR_ND */ | |
290 | } | |
291 | ||
a097c0b2 PS |
292 | /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */ |
293 | static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = { | |
294 | [OVS_KEY_ATTR_ENCAP] = -1, | |
295 | [OVS_KEY_ATTR_PRIORITY] = sizeof(u32), | |
296 | [OVS_KEY_ATTR_IN_PORT] = sizeof(u32), | |
297 | [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32), | |
298 | [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet), | |
299 | [OVS_KEY_ATTR_VLAN] = sizeof(__be16), | |
300 | [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16), | |
301 | [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4), | |
302 | [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6), | |
303 | [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp), | |
dc235f7f | 304 | [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16), |
a097c0b2 PS |
305 | [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp), |
306 | [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp), | |
307 | [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp), | |
308 | [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6), | |
309 | [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp), | |
310 | [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd), | |
a6059080 | 311 | [OVS_KEY_ATTR_RECIRC_ID] = sizeof(u32), |
7d16c847 | 312 | [OVS_KEY_ATTR_DP_HASH] = sizeof(u32), |
a097c0b2 | 313 | [OVS_KEY_ATTR_TUNNEL] = -1, |
ccf43786 | 314 | [OVS_KEY_ATTR_MPLS] = sizeof(struct ovs_key_mpls), |
a097c0b2 PS |
315 | }; |
316 | ||
317 | static bool is_all_zero(const u8 *fp, size_t size) | |
318 | { | |
319 | int i; | |
320 | ||
321 | if (!fp) | |
322 | return false; | |
323 | ||
324 | for (i = 0; i < size; i++) | |
325 | if (fp[i]) | |
326 | return false; | |
327 | ||
328 | return true; | |
329 | } | |
330 | ||
331 | static int __parse_flow_nlattrs(const struct nlattr *attr, | |
332 | const struct nlattr *a[], | |
9233cef7 | 333 | u64 *attrsp, bool log, bool nz) |
a097c0b2 PS |
334 | { |
335 | const struct nlattr *nla; | |
336 | u64 attrs; | |
337 | int rem; | |
338 | ||
339 | attrs = *attrsp; | |
340 | nla_for_each_nested(nla, attr, rem) { | |
341 | u16 type = nla_type(nla); | |
342 | int expected_len; | |
343 | ||
344 | if (type > OVS_KEY_ATTR_MAX) { | |
7d16c847 | 345 | OVS_NLERR(log, "Key type %d is out of range max %d", |
a097c0b2 PS |
346 | type, OVS_KEY_ATTR_MAX); |
347 | return -EINVAL; | |
348 | } | |
349 | ||
350 | if (attrs & (1ULL << type)) { | |
7d16c847 | 351 | OVS_NLERR(log, "Duplicate key (type %d).", type); |
a097c0b2 PS |
352 | return -EINVAL; |
353 | } | |
354 | ||
355 | expected_len = ovs_key_lens[type]; | |
356 | if (nla_len(nla) != expected_len && expected_len != -1) { | |
7d16c847 PS |
357 | OVS_NLERR(log, "Key %d has unexpected len %d expected %d", |
358 | type, nla_len(nla), expected_len); | |
a097c0b2 PS |
359 | return -EINVAL; |
360 | } | |
361 | ||
362 | if (!nz || !is_all_zero(nla_data(nla), expected_len)) { | |
363 | attrs |= 1ULL << type; | |
364 | a[type] = nla; | |
365 | } | |
366 | } | |
367 | if (rem) { | |
9233cef7 | 368 | OVS_NLERR(log, "Message has %d unknown bytes.", rem); |
a097c0b2 PS |
369 | return -EINVAL; |
370 | } | |
371 | ||
372 | *attrsp = attrs; | |
373 | return 0; | |
374 | } | |
375 | ||
376 | static int parse_flow_mask_nlattrs(const struct nlattr *attr, | |
9233cef7 JR |
377 | const struct nlattr *a[], u64 *attrsp, |
378 | bool log) | |
a097c0b2 | 379 | { |
9233cef7 | 380 | return __parse_flow_nlattrs(attr, a, attrsp, log, true); |
a097c0b2 PS |
381 | } |
382 | ||
383 | static int parse_flow_nlattrs(const struct nlattr *attr, | |
9233cef7 JR |
384 | const struct nlattr *a[], u64 *attrsp, |
385 | bool log) | |
a097c0b2 | 386 | { |
9233cef7 | 387 | return __parse_flow_nlattrs(attr, a, attrsp, log, false); |
a097c0b2 PS |
388 | } |
389 | ||
7d16c847 PS |
390 | static int genev_tun_opt_from_nlattr(const struct nlattr *a, |
391 | struct sw_flow_match *match, bool is_mask, | |
392 | bool log) | |
393 | { | |
394 | unsigned long opt_key_offset; | |
395 | ||
396 | if (nla_len(a) > sizeof(match->key->tun_opts)) { | |
397 | OVS_NLERR(log, "Geneve option length err (len %d, max %zu).", | |
398 | nla_len(a), sizeof(match->key->tun_opts)); | |
399 | return -EINVAL; | |
400 | } | |
401 | ||
402 | if (nla_len(a) % 4 != 0) { | |
403 | OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.", | |
404 | nla_len(a)); | |
405 | return -EINVAL; | |
406 | } | |
407 | ||
408 | /* We need to record the length of the options passed | |
409 | * down, otherwise packets with the same format but | |
410 | * additional options will be silently matched. | |
411 | */ | |
412 | if (!is_mask) { | |
413 | SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a), | |
414 | false); | |
415 | } else { | |
416 | /* This is somewhat unusual because it looks at | |
417 | * both the key and mask while parsing the | |
418 | * attributes (and by extension assumes the key | |
419 | * is parsed first). Normally, we would verify | |
420 | * that each is the correct length and that the | |
421 | * attributes line up in the validate function. | |
422 | * However, that is difficult because this is | |
423 | * variable length and we won't have the | |
424 | * information later. | |
425 | */ | |
426 | if (match->key->tun_opts_len != nla_len(a)) { | |
427 | OVS_NLERR(log, "Geneve option len %d != mask len %d", | |
428 | match->key->tun_opts_len, nla_len(a)); | |
429 | return -EINVAL; | |
430 | } | |
431 | ||
432 | SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true); | |
433 | } | |
434 | ||
435 | opt_key_offset = (unsigned long)GENEVE_OPTS((struct sw_flow_key *)0, | |
436 | nla_len(a)); | |
437 | SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a), | |
438 | nla_len(a), is_mask); | |
439 | return 0; | |
440 | } | |
441 | ||
a097c0b2 | 442 | static int ipv4_tun_from_nlattr(const struct nlattr *attr, |
9233cef7 JR |
443 | struct sw_flow_match *match, bool is_mask, |
444 | bool log) | |
a097c0b2 PS |
445 | { |
446 | struct nlattr *a; | |
447 | int rem; | |
448 | bool ttl = false; | |
449 | __be16 tun_flags = 0; | |
450 | ||
451 | nla_for_each_nested(a, attr, rem) { | |
452 | int type = nla_type(a); | |
7d16c847 PS |
453 | int err; |
454 | ||
a097c0b2 PS |
455 | static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = { |
456 | [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64), | |
457 | [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32), | |
458 | [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32), | |
459 | [OVS_TUNNEL_KEY_ATTR_TOS] = 1, | |
460 | [OVS_TUNNEL_KEY_ATTR_TTL] = 1, | |
461 | [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0, | |
462 | [OVS_TUNNEL_KEY_ATTR_CSUM] = 0, | |
8b7ea2d4 WZ |
463 | [OVS_TUNNEL_KEY_ATTR_TP_SRC] = sizeof(u16), |
464 | [OVS_TUNNEL_KEY_ATTR_TP_DST] = sizeof(u16), | |
94872594 | 465 | [OVS_TUNNEL_KEY_ATTR_OAM] = 0, |
c1fc1411 | 466 | [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = -1, |
a097c0b2 PS |
467 | }; |
468 | ||
469 | if (type > OVS_TUNNEL_KEY_ATTR_MAX) { | |
7d16c847 | 470 | OVS_NLERR(log, "Tunnel attr %d out of range max %d", |
9233cef7 | 471 | type, OVS_TUNNEL_KEY_ATTR_MAX); |
a097c0b2 PS |
472 | return -EINVAL; |
473 | } | |
474 | ||
c1fc1411 JG |
475 | if (ovs_tunnel_key_lens[type] != nla_len(a) && |
476 | ovs_tunnel_key_lens[type] != -1) { | |
7d16c847 PS |
477 | OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d", |
478 | type, nla_len(a), ovs_tunnel_key_lens[type]); | |
a097c0b2 PS |
479 | return -EINVAL; |
480 | } | |
481 | ||
482 | switch (type) { | |
483 | case OVS_TUNNEL_KEY_ATTR_ID: | |
484 | SW_FLOW_KEY_PUT(match, tun_key.tun_id, | |
485 | nla_get_be64(a), is_mask); | |
486 | tun_flags |= TUNNEL_KEY; | |
487 | break; | |
488 | case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: | |
489 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_src, | |
490 | nla_get_be32(a), is_mask); | |
491 | break; | |
492 | case OVS_TUNNEL_KEY_ATTR_IPV4_DST: | |
493 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst, | |
494 | nla_get_be32(a), is_mask); | |
495 | break; | |
496 | case OVS_TUNNEL_KEY_ATTR_TOS: | |
497 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos, | |
498 | nla_get_u8(a), is_mask); | |
499 | break; | |
500 | case OVS_TUNNEL_KEY_ATTR_TTL: | |
501 | SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl, | |
502 | nla_get_u8(a), is_mask); | |
503 | ttl = true; | |
504 | break; | |
505 | case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT: | |
506 | tun_flags |= TUNNEL_DONT_FRAGMENT; | |
507 | break; | |
508 | case OVS_TUNNEL_KEY_ATTR_CSUM: | |
509 | tun_flags |= TUNNEL_CSUM; | |
510 | break; | |
8b7ea2d4 WZ |
511 | case OVS_TUNNEL_KEY_ATTR_TP_SRC: |
512 | SW_FLOW_KEY_PUT(match, tun_key.tp_src, | |
513 | nla_get_be16(a), is_mask); | |
514 | break; | |
515 | case OVS_TUNNEL_KEY_ATTR_TP_DST: | |
516 | SW_FLOW_KEY_PUT(match, tun_key.tp_dst, | |
517 | nla_get_be16(a), is_mask); | |
518 | break; | |
94872594 JG |
519 | case OVS_TUNNEL_KEY_ATTR_OAM: |
520 | tun_flags |= TUNNEL_OAM; | |
521 | break; | |
c1fc1411 | 522 | case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS: |
7d16c847 PS |
523 | err = genev_tun_opt_from_nlattr(a, match, is_mask, log); |
524 | if (err) | |
525 | return err; | |
c1fc1411 | 526 | |
7d16c847 | 527 | tun_flags |= TUNNEL_OPTIONS_PRESENT; |
c1fc1411 | 528 | break; |
a097c0b2 | 529 | default: |
7d16c847 | 530 | OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d", |
9233cef7 | 531 | type); |
a097c0b2 PS |
532 | return -EINVAL; |
533 | } | |
534 | } | |
535 | ||
536 | SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask); | |
537 | ||
538 | if (rem > 0) { | |
9233cef7 JR |
539 | OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.", |
540 | rem); | |
a097c0b2 PS |
541 | return -EINVAL; |
542 | } | |
543 | ||
544 | if (!is_mask) { | |
545 | if (!match->key->tun_key.ipv4_dst) { | |
7d16c847 | 546 | OVS_NLERR(log, "IPv4 tunnel dst address is zero"); |
a097c0b2 PS |
547 | return -EINVAL; |
548 | } | |
549 | ||
550 | if (!ttl) { | |
9233cef7 | 551 | OVS_NLERR(log, "IPv4 tunnel TTL not specified."); |
a097c0b2 PS |
552 | return -EINVAL; |
553 | } | |
554 | } | |
555 | ||
556 | return 0; | |
557 | } | |
558 | ||
8b7ea2d4 WZ |
559 | static int __ipv4_tun_to_nlattr(struct sk_buff *skb, |
560 | const struct ovs_key_ipv4_tunnel *output, | |
561 | const struct geneve_opt *tun_opts, | |
562 | int swkey_tun_opts_len) | |
a097c0b2 | 563 | { |
a097c0b2 PS |
564 | if (output->tun_flags & TUNNEL_KEY && |
565 | nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id)) | |
566 | return -EMSGSIZE; | |
567 | if (output->ipv4_src && | |
7d16c847 | 568 | nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src)) |
a097c0b2 PS |
569 | return -EMSGSIZE; |
570 | if (output->ipv4_dst && | |
7d16c847 | 571 | nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst)) |
a097c0b2 PS |
572 | return -EMSGSIZE; |
573 | if (output->ipv4_tos && | |
7d16c847 | 574 | nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos)) |
a097c0b2 PS |
575 | return -EMSGSIZE; |
576 | if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl)) | |
577 | return -EMSGSIZE; | |
578 | if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) && | |
7d16c847 | 579 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT)) |
a097c0b2 PS |
580 | return -EMSGSIZE; |
581 | if ((output->tun_flags & TUNNEL_CSUM) && | |
7d16c847 | 582 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM)) |
a097c0b2 | 583 | return -EMSGSIZE; |
8b7ea2d4 | 584 | if (output->tp_src && |
7d16c847 | 585 | nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src)) |
8b7ea2d4 WZ |
586 | return -EMSGSIZE; |
587 | if (output->tp_dst && | |
7d16c847 | 588 | nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst)) |
8b7ea2d4 | 589 | return -EMSGSIZE; |
94872594 | 590 | if ((output->tun_flags & TUNNEL_OAM) && |
7d16c847 | 591 | nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM)) |
94872594 | 592 | return -EMSGSIZE; |
c1fc1411 JG |
593 | if (tun_opts && |
594 | nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS, | |
05499369 BP |
595 | swkey_tun_opts_len, tun_opts)) |
596 | return -EMSGSIZE; | |
a097c0b2 | 597 | |
8b7ea2d4 WZ |
598 | return 0; |
599 | } | |
600 | ||
8b7ea2d4 WZ |
601 | static int ipv4_tun_to_nlattr(struct sk_buff *skb, |
602 | const struct ovs_key_ipv4_tunnel *output, | |
603 | const struct geneve_opt *tun_opts, | |
604 | int swkey_tun_opts_len) | |
605 | { | |
606 | struct nlattr *nla; | |
607 | int err; | |
608 | ||
609 | nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL); | |
610 | if (!nla) | |
611 | return -EMSGSIZE; | |
612 | ||
613 | err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len); | |
614 | if (err) | |
615 | return err; | |
616 | ||
a097c0b2 PS |
617 | nla_nest_end(skb, nla); |
618 | return 0; | |
619 | } | |
620 | ||
8b7ea2d4 WZ |
621 | int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb, |
622 | const struct ovs_tunnel_info *egress_tun_info) | |
623 | { | |
624 | return __ipv4_tun_to_nlattr(skb, &egress_tun_info->tunnel, | |
625 | egress_tun_info->options, | |
626 | egress_tun_info->options_len); | |
627 | } | |
a097c0b2 PS |
628 | |
629 | static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs, | |
9233cef7 JR |
630 | const struct nlattr **a, bool is_mask, |
631 | bool log) | |
a097c0b2 | 632 | { |
7804df20 AZ |
633 | if (*attrs & (1ULL << OVS_KEY_ATTR_DP_HASH)) { |
634 | u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]); | |
635 | ||
636 | SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask); | |
637 | *attrs &= ~(1ULL << OVS_KEY_ATTR_DP_HASH); | |
638 | } | |
639 | ||
a6059080 AZ |
640 | if (*attrs & (1ULL << OVS_KEY_ATTR_RECIRC_ID)) { |
641 | u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]); | |
642 | ||
643 | SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask); | |
644 | *attrs &= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID); | |
645 | } | |
646 | ||
a097c0b2 PS |
647 | if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) { |
648 | SW_FLOW_KEY_PUT(match, phy.priority, | |
649 | nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask); | |
650 | *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY); | |
651 | } | |
652 | ||
653 | if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) { | |
654 | u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]); | |
655 | ||
a473df5b | 656 | if (is_mask) { |
a097c0b2 | 657 | in_port = 0xffffffff; /* Always exact match in_port. */ |
a473df5b | 658 | } else if (in_port >= DP_MAX_PORTS) { |
7d16c847 | 659 | OVS_NLERR(log, "Port %d exceeds max allowable %d", |
a473df5b | 660 | in_port, DP_MAX_PORTS); |
a097c0b2 | 661 | return -EINVAL; |
a473df5b | 662 | } |
a097c0b2 PS |
663 | |
664 | SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask); | |
665 | *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT); | |
666 | } else if (!is_mask) { | |
667 | SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask); | |
668 | } | |
669 | ||
670 | if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) { | |
671 | uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]); | |
672 | ||
673 | SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask); | |
674 | *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK); | |
675 | } | |
676 | if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) { | |
677 | if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match, | |
9233cef7 | 678 | is_mask, log)) |
a097c0b2 PS |
679 | return -EINVAL; |
680 | *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL); | |
681 | } | |
682 | return 0; | |
683 | } | |
684 | ||
df65fec1 | 685 | static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs, |
9233cef7 JR |
686 | const struct nlattr **a, bool is_mask, |
687 | bool log) | |
a097c0b2 PS |
688 | { |
689 | int err; | |
a097c0b2 | 690 | |
9233cef7 | 691 | err = metadata_from_nlattrs(match, &attrs, a, is_mask, log); |
a097c0b2 PS |
692 | if (err) |
693 | return err; | |
694 | ||
695 | if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) { | |
696 | const struct ovs_key_ethernet *eth_key; | |
697 | ||
698 | eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]); | |
699 | SW_FLOW_KEY_MEMCPY(match, eth.src, | |
700 | eth_key->eth_src, ETH_ALEN, is_mask); | |
701 | SW_FLOW_KEY_MEMCPY(match, eth.dst, | |
702 | eth_key->eth_dst, ETH_ALEN, is_mask); | |
703 | attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET); | |
704 | } | |
705 | ||
706 | if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) { | |
707 | __be16 tci; | |
708 | ||
709 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); | |
710 | if (!(tci & htons(VLAN_TAG_PRESENT))) { | |
711 | if (is_mask) | |
7d16c847 | 712 | OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit."); |
a097c0b2 | 713 | else |
7d16c847 | 714 | OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set."); |
a097c0b2 PS |
715 | |
716 | return -EINVAL; | |
717 | } | |
718 | ||
719 | SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask); | |
720 | attrs &= ~(1ULL << OVS_KEY_ATTR_VLAN); | |
f3ccd17d | 721 | } |
a097c0b2 PS |
722 | |
723 | if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) { | |
724 | __be16 eth_type; | |
725 | ||
726 | eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); | |
727 | if (is_mask) { | |
728 | /* Always exact match EtherType. */ | |
729 | eth_type = htons(0xffff); | |
730 | } else if (ntohs(eth_type) < ETH_P_802_3_MIN) { | |
7d16c847 | 731 | OVS_NLERR(log, "EtherType %x is less than min %x", |
9233cef7 | 732 | ntohs(eth_type), ETH_P_802_3_MIN); |
a097c0b2 PS |
733 | return -EINVAL; |
734 | } | |
735 | ||
736 | SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask); | |
737 | attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE); | |
738 | } else if (!is_mask) { | |
739 | SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask); | |
740 | } | |
741 | ||
742 | if (attrs & (1ULL << OVS_KEY_ATTR_IPV4)) { | |
743 | const struct ovs_key_ipv4 *ipv4_key; | |
744 | ||
745 | ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]); | |
746 | if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) { | |
7d16c847 | 747 | OVS_NLERR(log, "IPv4 frag type %d is out of range max %d", |
9233cef7 | 748 | ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX); |
a097c0b2 PS |
749 | return -EINVAL; |
750 | } | |
751 | SW_FLOW_KEY_PUT(match, ip.proto, | |
752 | ipv4_key->ipv4_proto, is_mask); | |
753 | SW_FLOW_KEY_PUT(match, ip.tos, | |
754 | ipv4_key->ipv4_tos, is_mask); | |
755 | SW_FLOW_KEY_PUT(match, ip.ttl, | |
756 | ipv4_key->ipv4_ttl, is_mask); | |
757 | SW_FLOW_KEY_PUT(match, ip.frag, | |
758 | ipv4_key->ipv4_frag, is_mask); | |
759 | SW_FLOW_KEY_PUT(match, ipv4.addr.src, | |
760 | ipv4_key->ipv4_src, is_mask); | |
761 | SW_FLOW_KEY_PUT(match, ipv4.addr.dst, | |
762 | ipv4_key->ipv4_dst, is_mask); | |
763 | attrs &= ~(1ULL << OVS_KEY_ATTR_IPV4); | |
764 | } | |
765 | ||
766 | if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) { | |
767 | const struct ovs_key_ipv6 *ipv6_key; | |
768 | ||
769 | ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]); | |
770 | if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) { | |
7d16c847 | 771 | OVS_NLERR(log, "IPv6 frag type %d is out of range max %d", |
9233cef7 | 772 | ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX); |
a097c0b2 PS |
773 | return -EINVAL; |
774 | } | |
65721038 | 775 | if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) { |
9233cef7 JR |
776 | OVS_NLERR(log, |
777 | "Invalid IPv6 flow label value (value=%x, max=%x).", | |
67782539 JR |
778 | ntohl(ipv6_key->ipv6_label), (1 << 20) - 1); |
779 | return -EINVAL; | |
780 | } | |
a097c0b2 PS |
781 | SW_FLOW_KEY_PUT(match, ipv6.label, |
782 | ipv6_key->ipv6_label, is_mask); | |
783 | SW_FLOW_KEY_PUT(match, ip.proto, | |
784 | ipv6_key->ipv6_proto, is_mask); | |
785 | SW_FLOW_KEY_PUT(match, ip.tos, | |
786 | ipv6_key->ipv6_tclass, is_mask); | |
787 | SW_FLOW_KEY_PUT(match, ip.ttl, | |
788 | ipv6_key->ipv6_hlimit, is_mask); | |
789 | SW_FLOW_KEY_PUT(match, ip.frag, | |
790 | ipv6_key->ipv6_frag, is_mask); | |
791 | SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src, | |
792 | ipv6_key->ipv6_src, | |
793 | sizeof(match->key->ipv6.addr.src), | |
794 | is_mask); | |
795 | SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst, | |
796 | ipv6_key->ipv6_dst, | |
797 | sizeof(match->key->ipv6.addr.dst), | |
798 | is_mask); | |
799 | ||
800 | attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6); | |
801 | } | |
802 | ||
803 | if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) { | |
804 | const struct ovs_key_arp *arp_key; | |
805 | ||
806 | arp_key = nla_data(a[OVS_KEY_ATTR_ARP]); | |
807 | if (!is_mask && (arp_key->arp_op & htons(0xff00))) { | |
9233cef7 | 808 | OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).", |
a097c0b2 PS |
809 | arp_key->arp_op); |
810 | return -EINVAL; | |
811 | } | |
812 | ||
813 | SW_FLOW_KEY_PUT(match, ipv4.addr.src, | |
814 | arp_key->arp_sip, is_mask); | |
815 | SW_FLOW_KEY_PUT(match, ipv4.addr.dst, | |
816 | arp_key->arp_tip, is_mask); | |
817 | SW_FLOW_KEY_PUT(match, ip.proto, | |
818 | ntohs(arp_key->arp_op), is_mask); | |
819 | SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha, | |
820 | arp_key->arp_sha, ETH_ALEN, is_mask); | |
821 | SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha, | |
822 | arp_key->arp_tha, ETH_ALEN, is_mask); | |
823 | ||
824 | attrs &= ~(1ULL << OVS_KEY_ATTR_ARP); | |
825 | } | |
826 | ||
ccf43786 SH |
827 | if (attrs & (1ULL << OVS_KEY_ATTR_MPLS)) { |
828 | const struct ovs_key_mpls *mpls_key; | |
829 | ||
830 | mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]); | |
831 | SW_FLOW_KEY_PUT(match, mpls.top_lse, | |
832 | mpls_key->mpls_lse, is_mask); | |
833 | ||
834 | attrs &= ~(1ULL << OVS_KEY_ATTR_MPLS); | |
62974663 | 835 | } |
ccf43786 | 836 | |
a097c0b2 PS |
837 | if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) { |
838 | const struct ovs_key_tcp *tcp_key; | |
839 | ||
840 | tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]); | |
708fb4c5 JR |
841 | SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask); |
842 | SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask); | |
a097c0b2 PS |
843 | attrs &= ~(1ULL << OVS_KEY_ATTR_TCP); |
844 | } | |
845 | ||
dc235f7f | 846 | if (attrs & (1ULL << OVS_KEY_ATTR_TCP_FLAGS)) { |
cab29271 JS |
847 | SW_FLOW_KEY_PUT(match, tp.flags, |
848 | nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]), | |
849 | is_mask); | |
dc235f7f JR |
850 | attrs &= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS); |
851 | } | |
852 | ||
a097c0b2 PS |
853 | if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) { |
854 | const struct ovs_key_udp *udp_key; | |
855 | ||
856 | udp_key = nla_data(a[OVS_KEY_ATTR_UDP]); | |
708fb4c5 JR |
857 | SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask); |
858 | SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask); | |
a097c0b2 PS |
859 | attrs &= ~(1ULL << OVS_KEY_ATTR_UDP); |
860 | } | |
861 | ||
862 | if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) { | |
863 | const struct ovs_key_sctp *sctp_key; | |
864 | ||
865 | sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]); | |
708fb4c5 JR |
866 | SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask); |
867 | SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask); | |
a097c0b2 PS |
868 | attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP); |
869 | } | |
870 | ||
871 | if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) { | |
872 | const struct ovs_key_icmp *icmp_key; | |
873 | ||
874 | icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]); | |
708fb4c5 | 875 | SW_FLOW_KEY_PUT(match, tp.src, |
a097c0b2 | 876 | htons(icmp_key->icmp_type), is_mask); |
708fb4c5 | 877 | SW_FLOW_KEY_PUT(match, tp.dst, |
a097c0b2 PS |
878 | htons(icmp_key->icmp_code), is_mask); |
879 | attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP); | |
880 | } | |
881 | ||
882 | if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) { | |
883 | const struct ovs_key_icmpv6 *icmpv6_key; | |
884 | ||
885 | icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]); | |
708fb4c5 | 886 | SW_FLOW_KEY_PUT(match, tp.src, |
a097c0b2 | 887 | htons(icmpv6_key->icmpv6_type), is_mask); |
708fb4c5 | 888 | SW_FLOW_KEY_PUT(match, tp.dst, |
a097c0b2 PS |
889 | htons(icmpv6_key->icmpv6_code), is_mask); |
890 | attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6); | |
891 | } | |
892 | ||
893 | if (attrs & (1ULL << OVS_KEY_ATTR_ND)) { | |
894 | const struct ovs_key_nd *nd_key; | |
895 | ||
896 | nd_key = nla_data(a[OVS_KEY_ATTR_ND]); | |
897 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target, | |
898 | nd_key->nd_target, | |
899 | sizeof(match->key->ipv6.nd.target), | |
900 | is_mask); | |
901 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll, | |
902 | nd_key->nd_sll, ETH_ALEN, is_mask); | |
903 | SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll, | |
904 | nd_key->nd_tll, ETH_ALEN, is_mask); | |
905 | attrs &= ~(1ULL << OVS_KEY_ATTR_ND); | |
906 | } | |
907 | ||
a473df5b | 908 | if (attrs != 0) { |
7d16c847 | 909 | OVS_NLERR(log, "Unknown key attributes %llx", |
a473df5b | 910 | (unsigned long long)attrs); |
a097c0b2 | 911 | return -EINVAL; |
a473df5b | 912 | } |
a097c0b2 PS |
913 | |
914 | return 0; | |
915 | } | |
916 | ||
62974663 | 917 | static void nlattr_set(struct nlattr *attr, u8 val, bool is_attr_mask_key) |
a097c0b2 | 918 | { |
62974663 DDP |
919 | struct nlattr *nla; |
920 | int rem; | |
a097c0b2 | 921 | |
62974663 DDP |
922 | /* The nlattr stream should already have been validated */ |
923 | nla_for_each_nested(nla, attr, rem) { | |
924 | /* We assume that ovs_key_lens[type] == -1 means that type is a | |
925 | * nested attribute | |
926 | */ | |
927 | if (is_attr_mask_key && ovs_key_lens[nla_type(nla)] == -1) | |
928 | nlattr_set(nla, val, false); | |
929 | else | |
930 | memset(nla_data(nla), val, nla_len(nla)); | |
931 | } | |
932 | } | |
933 | ||
934 | static void mask_set_nlattr(struct nlattr *attr, u8 val) | |
935 | { | |
936 | nlattr_set(attr, val, true); | |
a097c0b2 PS |
937 | } |
938 | ||
939 | /** | |
940 | * ovs_nla_get_match - parses Netlink attributes into a flow key and | |
941 | * mask. In case the 'mask' is NULL, the flow is treated as exact match | |
942 | * flow. Otherwise, it is treated as a wildcarded flow, except the mask | |
943 | * does not include any don't care bit. | |
944 | * @match: receives the extracted flow match information. | |
945 | * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute | |
946 | * sequence. The fields should of the packet that triggered the creation | |
947 | * of this flow. | |
948 | * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink | |
949 | * attribute specifies the mask field of the wildcarded flow. | |
9233cef7 JR |
950 | * @log: Boolean to allow kernel error logging. Normally true, but when |
951 | * probing for feature compatibility this should be passed in as false to | |
952 | * suppress unnecessary error logging. | |
a097c0b2 PS |
953 | */ |
954 | int ovs_nla_get_match(struct sw_flow_match *match, | |
f3ccd17d | 955 | const struct nlattr *nla_key, |
9233cef7 JR |
956 | const struct nlattr *nla_mask, |
957 | bool log) | |
a097c0b2 PS |
958 | { |
959 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; | |
960 | const struct nlattr *encap; | |
62974663 | 961 | struct nlattr *newmask = NULL; |
a097c0b2 PS |
962 | u64 key_attrs = 0; |
963 | u64 mask_attrs = 0; | |
964 | bool encap_valid = false; | |
965 | int err; | |
966 | ||
9233cef7 | 967 | err = parse_flow_nlattrs(nla_key, a, &key_attrs, log); |
a097c0b2 PS |
968 | if (err) |
969 | return err; | |
970 | ||
971 | if ((key_attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) && | |
972 | (key_attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) && | |
973 | (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) { | |
974 | __be16 tci; | |
975 | ||
976 | if (!((key_attrs & (1ULL << OVS_KEY_ATTR_VLAN)) && | |
977 | (key_attrs & (1ULL << OVS_KEY_ATTR_ENCAP)))) { | |
9233cef7 | 978 | OVS_NLERR(log, "Invalid Vlan frame."); |
a097c0b2 PS |
979 | return -EINVAL; |
980 | } | |
981 | ||
982 | key_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE); | |
983 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); | |
984 | encap = a[OVS_KEY_ATTR_ENCAP]; | |
985 | key_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP); | |
986 | encap_valid = true; | |
987 | ||
988 | if (tci & htons(VLAN_TAG_PRESENT)) { | |
9233cef7 | 989 | err = parse_flow_nlattrs(encap, a, &key_attrs, log); |
a097c0b2 PS |
990 | if (err) |
991 | return err; | |
992 | } else if (!tci) { | |
993 | /* Corner case for truncated 802.1Q header. */ | |
994 | if (nla_len(encap)) { | |
7d16c847 | 995 | OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute."); |
a097c0b2 PS |
996 | return -EINVAL; |
997 | } | |
998 | } else { | |
7d16c847 | 999 | OVS_NLERR(log, "Encap attr is set for non-VLAN frame"); |
a097c0b2 PS |
1000 | return -EINVAL; |
1001 | } | |
1002 | } | |
1003 | ||
9233cef7 | 1004 | err = ovs_key_from_nlattrs(match, key_attrs, a, false, log); |
a097c0b2 PS |
1005 | if (err) |
1006 | return err; | |
1007 | ||
f3ccd17d | 1008 | if (match->mask) { |
f3ccd17d PS |
1009 | if (!nla_mask) { |
1010 | /* Create an exact match mask. We need to set to 0xff | |
1011 | * all the 'match->mask' fields that have been touched | |
1012 | * in 'match->key'. We cannot simply memset | |
1013 | * 'match->mask', because padding bytes and fields not | |
1014 | * specified in 'match->key' should be left to 0. | |
1015 | * Instead, we use a stream of netlink attributes, | |
7d16c847 PS |
1016 | * copied from 'key' and set to 0xff. |
1017 | * ovs_key_from_nlattrs() will take care of filling | |
1018 | * 'match->mask' appropriately. | |
f3ccd17d PS |
1019 | */ |
1020 | newmask = kmemdup(nla_key, | |
1021 | nla_total_size(nla_len(nla_key)), | |
1022 | GFP_KERNEL); | |
1023 | if (!newmask) | |
1024 | return -ENOMEM; | |
62974663 | 1025 | |
f3ccd17d | 1026 | mask_set_nlattr(newmask, 0xff); |
62974663 | 1027 | |
f3ccd17d PS |
1028 | /* The userspace does not send tunnel attributes that |
1029 | * are 0, but we should not wildcard them nonetheless. | |
1030 | */ | |
1031 | if (match->key->tun_key.ipv4_dst) | |
1032 | SW_FLOW_KEY_MEMSET_FIELD(match, tun_key, | |
1033 | 0xff, true); | |
0b496cda | 1034 | |
f3ccd17d PS |
1035 | nla_mask = newmask; |
1036 | } | |
62974663 | 1037 | |
9233cef7 | 1038 | err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log); |
a097c0b2 | 1039 | if (err) |
62974663 | 1040 | goto free_newmask; |
a097c0b2 | 1041 | |
f3ccd17d PS |
1042 | /* Always match on tci. */ |
1043 | SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true); | |
1044 | ||
62974663 | 1045 | if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) { |
a097c0b2 PS |
1046 | __be16 eth_type = 0; |
1047 | __be16 tci = 0; | |
1048 | ||
1049 | if (!encap_valid) { | |
7d16c847 | 1050 | OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame."); |
62974663 DDP |
1051 | err = -EINVAL; |
1052 | goto free_newmask; | |
a097c0b2 PS |
1053 | } |
1054 | ||
1055 | mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ENCAP); | |
1056 | if (a[OVS_KEY_ATTR_ETHERTYPE]) | |
1057 | eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]); | |
1058 | ||
1059 | if (eth_type == htons(0xffff)) { | |
1060 | mask_attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERTYPE); | |
1061 | encap = a[OVS_KEY_ATTR_ENCAP]; | |
9233cef7 JR |
1062 | err = parse_flow_mask_nlattrs(encap, a, |
1063 | &mask_attrs, log); | |
3854ab21 AW |
1064 | if (err) |
1065 | goto free_newmask; | |
a097c0b2 | 1066 | } else { |
7d16c847 | 1067 | OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).", |
9233cef7 | 1068 | ntohs(eth_type)); |
62974663 DDP |
1069 | err = -EINVAL; |
1070 | goto free_newmask; | |
a097c0b2 PS |
1071 | } |
1072 | ||
1073 | if (a[OVS_KEY_ATTR_VLAN]) | |
1074 | tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]); | |
1075 | ||
1076 | if (!(tci & htons(VLAN_TAG_PRESENT))) { | |
7d16c847 | 1077 | OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).", |
9233cef7 | 1078 | ntohs(tci)); |
62974663 DDP |
1079 | err = -EINVAL; |
1080 | goto free_newmask; | |
a097c0b2 PS |
1081 | } |
1082 | } | |
1083 | ||
9233cef7 | 1084 | err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log); |
a097c0b2 | 1085 | if (err) |
62974663 | 1086 | goto free_newmask; |
a097c0b2 PS |
1087 | } |
1088 | ||
9233cef7 | 1089 | if (!match_validate(match, key_attrs, mask_attrs, log)) |
62974663 | 1090 | err = -EINVAL; |
a097c0b2 | 1091 | |
62974663 DDP |
1092 | free_newmask: |
1093 | kfree(newmask); | |
1094 | return err; | |
a097c0b2 PS |
1095 | } |
1096 | ||
1097 | /** | |
1098 | * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key. | |
c135bba1 | 1099 | * @key: Receives extracted in_port, priority, tun_key and skb_mark. |
a097c0b2 PS |
1100 | * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute |
1101 | * sequence. | |
9233cef7 JR |
1102 | * @log: Boolean to allow kernel error logging. Normally true, but when |
1103 | * probing for feature compatibility this should be passed in as false to | |
1104 | * suppress unnecessary error logging. | |
a097c0b2 PS |
1105 | * |
1106 | * This parses a series of Netlink attributes that form a flow key, which must | |
1107 | * take the same form accepted by flow_from_nlattrs(), but only enough of it to | |
1108 | * get the metadata, that is, the parts of the flow key that cannot be | |
1109 | * extracted from the packet itself. | |
1110 | */ | |
7d16c847 | 1111 | |
c135bba1 | 1112 | int ovs_nla_get_flow_metadata(const struct nlattr *attr, |
9233cef7 JR |
1113 | struct sw_flow_key *key, |
1114 | bool log) | |
a097c0b2 | 1115 | { |
a097c0b2 | 1116 | const struct nlattr *a[OVS_KEY_ATTR_MAX + 1]; |
c135bba1 | 1117 | struct sw_flow_match match; |
a097c0b2 PS |
1118 | u64 attrs = 0; |
1119 | int err; | |
a097c0b2 | 1120 | |
9233cef7 | 1121 | err = parse_flow_nlattrs(attr, a, &attrs, log); |
a097c0b2 PS |
1122 | if (err) |
1123 | return -EINVAL; | |
1124 | ||
1125 | memset(&match, 0, sizeof(match)); | |
c135bba1 | 1126 | match.key = key; |
a097c0b2 | 1127 | |
7f45215a | 1128 | memset(key, 0, OVS_SW_FLOW_KEY_METADATA_SIZE); |
c135bba1 | 1129 | key->phy.in_port = DP_MAX_PORTS; |
a097c0b2 | 1130 | |
9233cef7 | 1131 | return metadata_from_nlattrs(&match, &attrs, a, false, log); |
a097c0b2 PS |
1132 | } |
1133 | ||
2c622e5a | 1134 | int ovs_nla_put_flow(const struct sw_flow_key *swkey, |
a097c0b2 PS |
1135 | const struct sw_flow_key *output, struct sk_buff *skb) |
1136 | { | |
1137 | struct ovs_key_ethernet *eth_key; | |
1138 | struct nlattr *nla, *encap; | |
1139 | bool is_mask = (swkey != output); | |
1140 | ||
7d16c847 | 1141 | if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id)) |
7804df20 AZ |
1142 | goto nla_put_failure; |
1143 | ||
7d16c847 | 1144 | if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash)) |
a6059080 AZ |
1145 | goto nla_put_failure; |
1146 | ||
a097c0b2 PS |
1147 | if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority)) |
1148 | goto nla_put_failure; | |
1149 | ||
c1fc1411 JG |
1150 | if ((swkey->tun_key.ipv4_dst || is_mask)) { |
1151 | const struct geneve_opt *opts = NULL; | |
1152 | ||
fa6395df AA |
1153 | if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT) |
1154 | opts = GENEVE_OPTS(output, swkey->tun_opts_len); | |
c1fc1411 JG |
1155 | |
1156 | if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts, | |
7d16c847 | 1157 | swkey->tun_opts_len)) |
c1fc1411 JG |
1158 | goto nla_put_failure; |
1159 | } | |
a097c0b2 PS |
1160 | |
1161 | if (swkey->phy.in_port == DP_MAX_PORTS) { | |
1162 | if (is_mask && (output->phy.in_port == 0xffff)) | |
1163 | if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff)) | |
1164 | goto nla_put_failure; | |
1165 | } else { | |
1166 | u16 upper_u16; | |
1167 | upper_u16 = !is_mask ? 0 : 0xffff; | |
1168 | ||
1169 | if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, | |
1170 | (upper_u16 << 16) | output->phy.in_port)) | |
1171 | goto nla_put_failure; | |
1172 | } | |
1173 | ||
1174 | if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark)) | |
1175 | goto nla_put_failure; | |
1176 | ||
1177 | nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key)); | |
1178 | if (!nla) | |
1179 | goto nla_put_failure; | |
1180 | ||
1181 | eth_key = nla_data(nla); | |
982a47ec JP |
1182 | ether_addr_copy(eth_key->eth_src, output->eth.src); |
1183 | ether_addr_copy(eth_key->eth_dst, output->eth.dst); | |
a097c0b2 PS |
1184 | |
1185 | if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) { | |
1186 | __be16 eth_type; | |
1187 | eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff); | |
1188 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) || | |
1189 | nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci)) | |
1190 | goto nla_put_failure; | |
1191 | encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP); | |
1192 | if (!swkey->eth.tci) | |
1193 | goto unencap; | |
1194 | } else | |
1195 | encap = NULL; | |
1196 | ||
1197 | if (swkey->eth.type == htons(ETH_P_802_2)) { | |
1198 | /* | |
1199 | * Ethertype 802.2 is represented in the netlink with omitted | |
1200 | * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and | |
1201 | * 0xffff in the mask attribute. Ethertype can also | |
1202 | * be wildcarded. | |
1203 | */ | |
1204 | if (is_mask && output->eth.type) | |
1205 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, | |
1206 | output->eth.type)) | |
1207 | goto nla_put_failure; | |
1208 | goto unencap; | |
1209 | } | |
1210 | ||
1211 | if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type)) | |
1212 | goto nla_put_failure; | |
1213 | ||
1214 | if (swkey->eth.type == htons(ETH_P_IP)) { | |
1215 | struct ovs_key_ipv4 *ipv4_key; | |
1216 | ||
1217 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key)); | |
1218 | if (!nla) | |
1219 | goto nla_put_failure; | |
1220 | ipv4_key = nla_data(nla); | |
1221 | ipv4_key->ipv4_src = output->ipv4.addr.src; | |
1222 | ipv4_key->ipv4_dst = output->ipv4.addr.dst; | |
1223 | ipv4_key->ipv4_proto = output->ip.proto; | |
1224 | ipv4_key->ipv4_tos = output->ip.tos; | |
1225 | ipv4_key->ipv4_ttl = output->ip.ttl; | |
1226 | ipv4_key->ipv4_frag = output->ip.frag; | |
1227 | } else if (swkey->eth.type == htons(ETH_P_IPV6)) { | |
1228 | struct ovs_key_ipv6 *ipv6_key; | |
1229 | ||
1230 | nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key)); | |
1231 | if (!nla) | |
1232 | goto nla_put_failure; | |
1233 | ipv6_key = nla_data(nla); | |
1234 | memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src, | |
1235 | sizeof(ipv6_key->ipv6_src)); | |
1236 | memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst, | |
1237 | sizeof(ipv6_key->ipv6_dst)); | |
1238 | ipv6_key->ipv6_label = output->ipv6.label; | |
1239 | ipv6_key->ipv6_proto = output->ip.proto; | |
1240 | ipv6_key->ipv6_tclass = output->ip.tos; | |
1241 | ipv6_key->ipv6_hlimit = output->ip.ttl; | |
1242 | ipv6_key->ipv6_frag = output->ip.frag; | |
1243 | } else if (swkey->eth.type == htons(ETH_P_ARP) || | |
1244 | swkey->eth.type == htons(ETH_P_RARP)) { | |
1245 | struct ovs_key_arp *arp_key; | |
1246 | ||
1247 | nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key)); | |
1248 | if (!nla) | |
1249 | goto nla_put_failure; | |
1250 | arp_key = nla_data(nla); | |
1251 | memset(arp_key, 0, sizeof(struct ovs_key_arp)); | |
1252 | arp_key->arp_sip = output->ipv4.addr.src; | |
1253 | arp_key->arp_tip = output->ipv4.addr.dst; | |
1254 | arp_key->arp_op = htons(output->ip.proto); | |
982a47ec JP |
1255 | ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha); |
1256 | ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha); | |
ccf43786 SH |
1257 | } else if (eth_p_mpls(swkey->eth.type)) { |
1258 | struct ovs_key_mpls *mpls_key; | |
1259 | ||
1260 | nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key)); | |
1261 | if (!nla) | |
1262 | goto nla_put_failure; | |
1263 | mpls_key = nla_data(nla); | |
1264 | mpls_key->mpls_lse = output->mpls.top_lse; | |
a097c0b2 PS |
1265 | } |
1266 | ||
1267 | if ((swkey->eth.type == htons(ETH_P_IP) || | |
1268 | swkey->eth.type == htons(ETH_P_IPV6)) && | |
1269 | swkey->ip.frag != OVS_FRAG_TYPE_LATER) { | |
1270 | ||
1271 | if (swkey->ip.proto == IPPROTO_TCP) { | |
1272 | struct ovs_key_tcp *tcp_key; | |
1273 | ||
1274 | nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key)); | |
1275 | if (!nla) | |
1276 | goto nla_put_failure; | |
1277 | tcp_key = nla_data(nla); | |
708fb4c5 JR |
1278 | tcp_key->tcp_src = output->tp.src; |
1279 | tcp_key->tcp_dst = output->tp.dst; | |
1280 | if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS, | |
1281 | output->tp.flags)) | |
1282 | goto nla_put_failure; | |
a097c0b2 PS |
1283 | } else if (swkey->ip.proto == IPPROTO_UDP) { |
1284 | struct ovs_key_udp *udp_key; | |
1285 | ||
1286 | nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key)); | |
1287 | if (!nla) | |
1288 | goto nla_put_failure; | |
1289 | udp_key = nla_data(nla); | |
708fb4c5 JR |
1290 | udp_key->udp_src = output->tp.src; |
1291 | udp_key->udp_dst = output->tp.dst; | |
a097c0b2 PS |
1292 | } else if (swkey->ip.proto == IPPROTO_SCTP) { |
1293 | struct ovs_key_sctp *sctp_key; | |
1294 | ||
1295 | nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key)); | |
1296 | if (!nla) | |
1297 | goto nla_put_failure; | |
1298 | sctp_key = nla_data(nla); | |
708fb4c5 JR |
1299 | sctp_key->sctp_src = output->tp.src; |
1300 | sctp_key->sctp_dst = output->tp.dst; | |
a097c0b2 PS |
1301 | } else if (swkey->eth.type == htons(ETH_P_IP) && |
1302 | swkey->ip.proto == IPPROTO_ICMP) { | |
1303 | struct ovs_key_icmp *icmp_key; | |
1304 | ||
1305 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key)); | |
1306 | if (!nla) | |
1307 | goto nla_put_failure; | |
1308 | icmp_key = nla_data(nla); | |
708fb4c5 JR |
1309 | icmp_key->icmp_type = ntohs(output->tp.src); |
1310 | icmp_key->icmp_code = ntohs(output->tp.dst); | |
a097c0b2 PS |
1311 | } else if (swkey->eth.type == htons(ETH_P_IPV6) && |
1312 | swkey->ip.proto == IPPROTO_ICMPV6) { | |
1313 | struct ovs_key_icmpv6 *icmpv6_key; | |
1314 | ||
1315 | nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6, | |
1316 | sizeof(*icmpv6_key)); | |
1317 | if (!nla) | |
1318 | goto nla_put_failure; | |
1319 | icmpv6_key = nla_data(nla); | |
708fb4c5 JR |
1320 | icmpv6_key->icmpv6_type = ntohs(output->tp.src); |
1321 | icmpv6_key->icmpv6_code = ntohs(output->tp.dst); | |
a097c0b2 PS |
1322 | |
1323 | if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION || | |
1324 | icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) { | |
1325 | struct ovs_key_nd *nd_key; | |
1326 | ||
1327 | nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key)); | |
1328 | if (!nla) | |
1329 | goto nla_put_failure; | |
1330 | nd_key = nla_data(nla); | |
1331 | memcpy(nd_key->nd_target, &output->ipv6.nd.target, | |
1332 | sizeof(nd_key->nd_target)); | |
982a47ec JP |
1333 | ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll); |
1334 | ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll); | |
a097c0b2 PS |
1335 | } |
1336 | } | |
1337 | } | |
1338 | ||
1339 | unencap: | |
1340 | if (encap) | |
1341 | nla_nest_end(skb, encap); | |
1342 | ||
1343 | return 0; | |
1344 | ||
1345 | nla_put_failure: | |
1346 | return -EMSGSIZE; | |
1347 | } | |
1348 | ||
1349 | #define MAX_ACTIONS_BUFSIZE (32 * 1024) | |
1350 | ||
9233cef7 | 1351 | static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log) |
a097c0b2 PS |
1352 | { |
1353 | struct sw_flow_actions *sfa; | |
1354 | ||
a473df5b | 1355 | if (size > MAX_ACTIONS_BUFSIZE) { |
7d16c847 | 1356 | OVS_NLERR(log, "Flow action size %u bytes exceeds max", size); |
a097c0b2 | 1357 | return ERR_PTR(-EINVAL); |
a473df5b | 1358 | } |
a097c0b2 PS |
1359 | |
1360 | sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL); | |
1361 | if (!sfa) | |
1362 | return ERR_PTR(-ENOMEM); | |
1363 | ||
1364 | sfa->actions_len = 0; | |
1365 | return sfa; | |
1366 | } | |
1367 | ||
1368 | /* RCU callback used by ovs_nla_free_flow_actions. */ | |
1369 | static void rcu_free_acts_callback(struct rcu_head *rcu) | |
1370 | { | |
1371 | struct sw_flow_actions *sf_acts = container_of(rcu, | |
1372 | struct sw_flow_actions, rcu); | |
1373 | kfree(sf_acts); | |
1374 | } | |
1375 | ||
1376 | /* Schedules 'sf_acts' to be freed after the next RCU grace period. | |
af465b67 PS |
1377 | * The caller must hold rcu_read_lock for this to be sensible. |
1378 | */ | |
a097c0b2 PS |
1379 | void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts) |
1380 | { | |
1381 | call_rcu(&sf_acts->rcu, rcu_free_acts_callback); | |
1382 | } | |
1383 | ||
1384 | static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, | |
9233cef7 | 1385 | int attr_len, bool log) |
a097c0b2 PS |
1386 | { |
1387 | ||
1388 | struct sw_flow_actions *acts; | |
1389 | int new_acts_size; | |
1390 | int req_size = NLA_ALIGN(attr_len); | |
1391 | int next_offset = offsetof(struct sw_flow_actions, actions) + | |
1392 | (*sfa)->actions_len; | |
1393 | ||
1394 | if (req_size <= (ksize(*sfa) - next_offset)) | |
1395 | goto out; | |
1396 | ||
1397 | new_acts_size = ksize(*sfa) * 2; | |
1398 | ||
1399 | if (new_acts_size > MAX_ACTIONS_BUFSIZE) { | |
1400 | if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size) | |
1401 | return ERR_PTR(-EMSGSIZE); | |
1402 | new_acts_size = MAX_ACTIONS_BUFSIZE; | |
1403 | } | |
1404 | ||
9233cef7 | 1405 | acts = nla_alloc_flow_actions(new_acts_size, log); |
a097c0b2 PS |
1406 | if (IS_ERR(acts)) |
1407 | return (void *)acts; | |
1408 | ||
1409 | memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len); | |
1410 | acts->actions_len = (*sfa)->actions_len; | |
1411 | kfree(*sfa); | |
1412 | *sfa = acts; | |
1413 | ||
1414 | out: | |
1415 | (*sfa)->actions_len += req_size; | |
1416 | return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset); | |
1417 | } | |
1418 | ||
7d16c847 PS |
1419 | static struct nlattr *__add_action(struct sw_flow_actions **sfa, |
1420 | int attrtype, void *data, int len, bool log) | |
a097c0b2 PS |
1421 | { |
1422 | struct nlattr *a; | |
1423 | ||
9233cef7 | 1424 | a = reserve_sfa_size(sfa, nla_attr_size(len), log); |
a097c0b2 | 1425 | if (IS_ERR(a)) |
f0cd669f | 1426 | return a; |
a097c0b2 PS |
1427 | |
1428 | a->nla_type = attrtype; | |
1429 | a->nla_len = nla_attr_size(len); | |
1430 | ||
1431 | if (data) | |
1432 | memcpy(nla_data(a), data, len); | |
1433 | memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len)); | |
1434 | ||
f0cd669f JG |
1435 | return a; |
1436 | } | |
1437 | ||
1438 | static int add_action(struct sw_flow_actions **sfa, int attrtype, | |
9233cef7 | 1439 | void *data, int len, bool log) |
f0cd669f JG |
1440 | { |
1441 | struct nlattr *a; | |
1442 | ||
9233cef7 | 1443 | a = __add_action(sfa, attrtype, data, len, log); |
f0cd669f JG |
1444 | if (IS_ERR(a)) |
1445 | return PTR_ERR(a); | |
1446 | ||
a097c0b2 PS |
1447 | return 0; |
1448 | } | |
1449 | ||
1450 | static inline int add_nested_action_start(struct sw_flow_actions **sfa, | |
9233cef7 | 1451 | int attrtype, bool log) |
a097c0b2 PS |
1452 | { |
1453 | int used = (*sfa)->actions_len; | |
1454 | int err; | |
1455 | ||
9233cef7 | 1456 | err = add_action(sfa, attrtype, NULL, 0, log); |
a097c0b2 PS |
1457 | if (err) |
1458 | return err; | |
1459 | ||
1460 | return used; | |
1461 | } | |
1462 | ||
1463 | static inline void add_nested_action_end(struct sw_flow_actions *sfa, | |
1464 | int st_offset) | |
1465 | { | |
1466 | struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + | |
1467 | st_offset); | |
1468 | ||
1469 | a->nla_len = sfa->actions_len - st_offset; | |
1470 | } | |
1471 | ||
ff27161e | 1472 | static int __ovs_nla_copy_actions(const struct nlattr *attr, |
ccf43786 SH |
1473 | const struct sw_flow_key *key, |
1474 | int depth, struct sw_flow_actions **sfa, | |
9233cef7 | 1475 | __be16 eth_type, __be16 vlan_tci, bool log); |
ccf43786 | 1476 | |
a097c0b2 PS |
1477 | static int validate_and_copy_sample(const struct nlattr *attr, |
1478 | const struct sw_flow_key *key, int depth, | |
ccf43786 | 1479 | struct sw_flow_actions **sfa, |
9233cef7 | 1480 | __be16 eth_type, __be16 vlan_tci, bool log) |
a097c0b2 PS |
1481 | { |
1482 | const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1]; | |
1483 | const struct nlattr *probability, *actions; | |
1484 | const struct nlattr *a; | |
1485 | int rem, start, err, st_acts; | |
1486 | ||
1487 | memset(attrs, 0, sizeof(attrs)); | |
1488 | nla_for_each_nested(a, attr, rem) { | |
1489 | int type = nla_type(a); | |
1490 | if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type]) | |
1491 | return -EINVAL; | |
1492 | attrs[type] = a; | |
1493 | } | |
1494 | if (rem) | |
1495 | return -EINVAL; | |
1496 | ||
1497 | probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY]; | |
1498 | if (!probability || nla_len(probability) != sizeof(u32)) | |
1499 | return -EINVAL; | |
1500 | ||
1501 | actions = attrs[OVS_SAMPLE_ATTR_ACTIONS]; | |
1502 | if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN)) | |
1503 | return -EINVAL; | |
1504 | ||
1505 | /* validation done, copy sample action. */ | |
9233cef7 | 1506 | start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log); |
a097c0b2 PS |
1507 | if (start < 0) |
1508 | return start; | |
1509 | err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, | |
9233cef7 | 1510 | nla_data(probability), sizeof(u32), log); |
a097c0b2 PS |
1511 | if (err) |
1512 | return err; | |
9233cef7 | 1513 | st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log); |
a097c0b2 PS |
1514 | if (st_acts < 0) |
1515 | return st_acts; | |
1516 | ||
ff27161e | 1517 | err = __ovs_nla_copy_actions(actions, key, depth + 1, sfa, |
9233cef7 | 1518 | eth_type, vlan_tci, log); |
a097c0b2 PS |
1519 | if (err) |
1520 | return err; | |
1521 | ||
1522 | add_nested_action_end(*sfa, st_acts); | |
1523 | add_nested_action_end(*sfa, start); | |
1524 | ||
1525 | return 0; | |
1526 | } | |
1527 | ||
ccf43786 SH |
1528 | static int validate_tp_port(const struct sw_flow_key *flow_key, |
1529 | __be16 eth_type) | |
a097c0b2 | 1530 | { |
ccf43786 | 1531 | if ((eth_type == htons(ETH_P_IP) || eth_type == htons(ETH_P_IPV6)) && |
708fb4c5 JR |
1532 | (flow_key->tp.src || flow_key->tp.dst)) |
1533 | return 0; | |
a097c0b2 PS |
1534 | |
1535 | return -EINVAL; | |
1536 | } | |
1537 | ||
1538 | void ovs_match_init(struct sw_flow_match *match, | |
1539 | struct sw_flow_key *key, | |
1540 | struct sw_flow_mask *mask) | |
1541 | { | |
1542 | memset(match, 0, sizeof(*match)); | |
1543 | match->key = key; | |
1544 | match->mask = mask; | |
1545 | ||
1546 | memset(key, 0, sizeof(*key)); | |
1547 | ||
1548 | if (mask) { | |
1549 | memset(&mask->key, 0, sizeof(mask->key)); | |
1550 | mask->range.start = mask->range.end = 0; | |
1551 | } | |
1552 | } | |
1553 | ||
1554 | static int validate_and_copy_set_tun(const struct nlattr *attr, | |
9233cef7 | 1555 | struct sw_flow_actions **sfa, bool log) |
a097c0b2 PS |
1556 | { |
1557 | struct sw_flow_match match; | |
1558 | struct sw_flow_key key; | |
f0cd669f JG |
1559 | struct ovs_tunnel_info *tun_info; |
1560 | struct nlattr *a; | |
a097c0b2 PS |
1561 | int err, start; |
1562 | ||
1563 | ovs_match_init(&match, &key, NULL); | |
9233cef7 | 1564 | err = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log); |
a097c0b2 PS |
1565 | if (err) |
1566 | return err; | |
1567 | ||
c1fc1411 JG |
1568 | if (key.tun_opts_len) { |
1569 | struct geneve_opt *option = GENEVE_OPTS(&key, | |
1570 | key.tun_opts_len); | |
1571 | int opts_len = key.tun_opts_len; | |
1572 | bool crit_opt = false; | |
1573 | ||
1574 | while (opts_len > 0) { | |
1575 | int len; | |
1576 | ||
1577 | if (opts_len < sizeof(*option)) | |
1578 | return -EINVAL; | |
1579 | ||
1580 | len = sizeof(*option) + option->length * 4; | |
1581 | if (len > opts_len) | |
1582 | return -EINVAL; | |
1583 | ||
1584 | crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE); | |
1585 | ||
1586 | option = (struct geneve_opt *)((u8 *)option + len); | |
1587 | opts_len -= len; | |
1588 | }; | |
1589 | ||
1590 | key.tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0; | |
1591 | }; | |
1592 | ||
9233cef7 | 1593 | start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log); |
a097c0b2 PS |
1594 | if (start < 0) |
1595 | return start; | |
1596 | ||
f0cd669f | 1597 | a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL, |
9233cef7 | 1598 | sizeof(*tun_info) + key.tun_opts_len, log); |
f0cd669f JG |
1599 | if (IS_ERR(a)) |
1600 | return PTR_ERR(a); | |
1601 | ||
1602 | tun_info = nla_data(a); | |
1603 | tun_info->tunnel = key.tun_key; | |
c1fc1411 JG |
1604 | tun_info->options_len = key.tun_opts_len; |
1605 | ||
1606 | if (tun_info->options_len) { | |
1607 | /* We need to store the options in the action itself since | |
1608 | * everything else will go away after flow setup. We can append | |
1609 | * it to tun_info and then point there. | |
1610 | */ | |
f1f60b85 | 1611 | memcpy((tun_info + 1), GENEVE_OPTS(&key, key.tun_opts_len), |
7d16c847 | 1612 | key.tun_opts_len); |
f1f60b85 | 1613 | tun_info->options = (struct geneve_opt *)(tun_info + 1); |
c1fc1411 JG |
1614 | } else { |
1615 | tun_info->options = NULL; | |
1616 | } | |
f0cd669f | 1617 | |
a097c0b2 PS |
1618 | add_nested_action_end(*sfa, start); |
1619 | ||
1620 | return err; | |
1621 | } | |
1622 | ||
1623 | static int validate_set(const struct nlattr *a, | |
1624 | const struct sw_flow_key *flow_key, | |
1625 | struct sw_flow_actions **sfa, | |
9233cef7 | 1626 | bool *set_tun, __be16 eth_type, bool log) |
a097c0b2 PS |
1627 | { |
1628 | const struct nlattr *ovs_key = nla_data(a); | |
1629 | int key_type = nla_type(ovs_key); | |
1630 | ||
1631 | /* There can be only one key in a action */ | |
1632 | if (nla_total_size(nla_len(ovs_key)) != nla_len(a)) | |
1633 | return -EINVAL; | |
1634 | ||
1635 | if (key_type > OVS_KEY_ATTR_MAX || | |
1636 | (ovs_key_lens[key_type] != nla_len(ovs_key) && | |
1637 | ovs_key_lens[key_type] != -1)) | |
1638 | return -EINVAL; | |
1639 | ||
1640 | switch (key_type) { | |
1641 | const struct ovs_key_ipv4 *ipv4_key; | |
1642 | const struct ovs_key_ipv6 *ipv6_key; | |
1643 | int err; | |
1644 | ||
1645 | case OVS_KEY_ATTR_PRIORITY: | |
1646 | case OVS_KEY_ATTR_SKB_MARK: | |
1647 | case OVS_KEY_ATTR_ETHERNET: | |
1648 | break; | |
1649 | ||
1650 | case OVS_KEY_ATTR_TUNNEL: | |
2baf0e0c PS |
1651 | if (eth_p_mpls(eth_type)) |
1652 | return -EINVAL; | |
1653 | ||
a097c0b2 | 1654 | *set_tun = true; |
9233cef7 | 1655 | err = validate_and_copy_set_tun(a, sfa, log); |
a097c0b2 PS |
1656 | if (err) |
1657 | return err; | |
1658 | break; | |
1659 | ||
1660 | case OVS_KEY_ATTR_IPV4: | |
ccf43786 | 1661 | if (eth_type != htons(ETH_P_IP)) |
a097c0b2 PS |
1662 | return -EINVAL; |
1663 | ||
1664 | if (!flow_key->ip.proto) | |
1665 | return -EINVAL; | |
1666 | ||
1667 | ipv4_key = nla_data(ovs_key); | |
1668 | if (ipv4_key->ipv4_proto != flow_key->ip.proto) | |
1669 | return -EINVAL; | |
1670 | ||
1671 | if (ipv4_key->ipv4_frag != flow_key->ip.frag) | |
1672 | return -EINVAL; | |
1673 | ||
1674 | break; | |
1675 | ||
1676 | case OVS_KEY_ATTR_IPV6: | |
ccf43786 | 1677 | if (eth_type != htons(ETH_P_IPV6)) |
a097c0b2 PS |
1678 | return -EINVAL; |
1679 | ||
1680 | if (!flow_key->ip.proto) | |
1681 | return -EINVAL; | |
1682 | ||
1683 | ipv6_key = nla_data(ovs_key); | |
1684 | if (ipv6_key->ipv6_proto != flow_key->ip.proto) | |
1685 | return -EINVAL; | |
1686 | ||
1687 | if (ipv6_key->ipv6_frag != flow_key->ip.frag) | |
1688 | return -EINVAL; | |
1689 | ||
1690 | if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000) | |
1691 | return -EINVAL; | |
1692 | ||
1693 | break; | |
1694 | ||
1695 | case OVS_KEY_ATTR_TCP: | |
1696 | if (flow_key->ip.proto != IPPROTO_TCP) | |
1697 | return -EINVAL; | |
1698 | ||
ccf43786 | 1699 | return validate_tp_port(flow_key, eth_type); |
a097c0b2 PS |
1700 | |
1701 | case OVS_KEY_ATTR_UDP: | |
1702 | if (flow_key->ip.proto != IPPROTO_UDP) | |
1703 | return -EINVAL; | |
1704 | ||
ccf43786 SH |
1705 | return validate_tp_port(flow_key, eth_type); |
1706 | ||
1707 | case OVS_KEY_ATTR_MPLS: | |
1708 | if (!eth_p_mpls(eth_type)) | |
1709 | return -EINVAL; | |
1710 | break; | |
a097c0b2 PS |
1711 | |
1712 | case OVS_KEY_ATTR_SCTP: | |
1713 | if (flow_key->ip.proto != IPPROTO_SCTP) | |
1714 | return -EINVAL; | |
1715 | ||
ccf43786 | 1716 | return validate_tp_port(flow_key, eth_type); |
a097c0b2 PS |
1717 | |
1718 | default: | |
1719 | return -EINVAL; | |
1720 | } | |
1721 | ||
1722 | return 0; | |
1723 | } | |
1724 | ||
1725 | static int validate_userspace(const struct nlattr *attr) | |
1726 | { | |
1727 | static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = { | |
1728 | [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 }, | |
1729 | [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC }, | |
8b7ea2d4 | 1730 | [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 }, |
a097c0b2 PS |
1731 | }; |
1732 | struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1]; | |
1733 | int error; | |
1734 | ||
1735 | error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, | |
1736 | attr, userspace_policy); | |
1737 | if (error) | |
1738 | return error; | |
1739 | ||
1740 | if (!a[OVS_USERSPACE_ATTR_PID] || | |
1741 | !nla_get_u32(a[OVS_USERSPACE_ATTR_PID])) | |
1742 | return -EINVAL; | |
1743 | ||
1744 | return 0; | |
1745 | } | |
1746 | ||
1747 | static int copy_action(const struct nlattr *from, | |
9233cef7 | 1748 | struct sw_flow_actions **sfa, bool log) |
a097c0b2 PS |
1749 | { |
1750 | int totlen = NLA_ALIGN(from->nla_len); | |
1751 | struct nlattr *to; | |
1752 | ||
9233cef7 | 1753 | to = reserve_sfa_size(sfa, from->nla_len, log); |
a097c0b2 PS |
1754 | if (IS_ERR(to)) |
1755 | return PTR_ERR(to); | |
1756 | ||
1757 | memcpy(to, from, totlen); | |
1758 | return 0; | |
1759 | } | |
1760 | ||
ff27161e | 1761 | static int __ovs_nla_copy_actions(const struct nlattr *attr, |
ccf43786 SH |
1762 | const struct sw_flow_key *key, |
1763 | int depth, struct sw_flow_actions **sfa, | |
9233cef7 | 1764 | __be16 eth_type, __be16 vlan_tci, bool log) |
a097c0b2 PS |
1765 | { |
1766 | const struct nlattr *a; | |
1767 | int rem, err; | |
1768 | ||
1769 | if (depth >= SAMPLE_ACTION_DEPTH) | |
1770 | return -EOVERFLOW; | |
1771 | ||
1772 | nla_for_each_nested(a, attr, rem) { | |
1773 | /* Expected argument lengths, (u32)-1 for variable length. */ | |
1774 | static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = { | |
1775 | [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32), | |
a6059080 | 1776 | [OVS_ACTION_ATTR_RECIRC] = sizeof(u32), |
a097c0b2 | 1777 | [OVS_ACTION_ATTR_USERSPACE] = (u32)-1, |
ccf43786 SH |
1778 | [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls), |
1779 | [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16), | |
a097c0b2 PS |
1780 | [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan), |
1781 | [OVS_ACTION_ATTR_POP_VLAN] = 0, | |
1782 | [OVS_ACTION_ATTR_SET] = (u32)-1, | |
7804df20 AZ |
1783 | [OVS_ACTION_ATTR_SAMPLE] = (u32)-1, |
1784 | [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash) | |
a097c0b2 PS |
1785 | }; |
1786 | const struct ovs_action_push_vlan *vlan; | |
1787 | int type = nla_type(a); | |
1788 | bool skip_copy; | |
1789 | ||
1790 | if (type > OVS_ACTION_ATTR_MAX || | |
1791 | (action_lens[type] != nla_len(a) && | |
1792 | action_lens[type] != (u32)-1)) | |
1793 | return -EINVAL; | |
1794 | ||
1795 | skip_copy = false; | |
1796 | switch (type) { | |
1797 | case OVS_ACTION_ATTR_UNSPEC: | |
1798 | return -EINVAL; | |
1799 | ||
1800 | case OVS_ACTION_ATTR_USERSPACE: | |
1801 | err = validate_userspace(a); | |
1802 | if (err) | |
1803 | return err; | |
1804 | break; | |
1805 | ||
1806 | case OVS_ACTION_ATTR_OUTPUT: | |
1807 | if (nla_get_u32(a) >= DP_MAX_PORTS) | |
1808 | return -EINVAL; | |
7d16c847 | 1809 | |
a097c0b2 PS |
1810 | break; |
1811 | ||
7804df20 AZ |
1812 | case OVS_ACTION_ATTR_HASH: { |
1813 | const struct ovs_action_hash *act_hash = nla_data(a); | |
1814 | ||
1815 | switch (act_hash->hash_alg) { | |
1816 | case OVS_HASH_ALG_L4: | |
1817 | break; | |
1818 | default: | |
1819 | return -EINVAL; | |
1820 | } | |
1821 | ||
1822 | break; | |
1823 | } | |
a097c0b2 PS |
1824 | |
1825 | case OVS_ACTION_ATTR_POP_VLAN: | |
e0b8f73f | 1826 | vlan_tci = htons(0); |
a097c0b2 PS |
1827 | break; |
1828 | ||
1829 | case OVS_ACTION_ATTR_PUSH_VLAN: | |
1830 | vlan = nla_data(a); | |
1831 | if (vlan->vlan_tpid != htons(ETH_P_8021Q)) | |
1832 | return -EINVAL; | |
1833 | if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT))) | |
1834 | return -EINVAL; | |
ccf43786 | 1835 | vlan_tci = vlan->vlan_tci; |
a097c0b2 PS |
1836 | break; |
1837 | ||
a6059080 AZ |
1838 | case OVS_ACTION_ATTR_RECIRC: |
1839 | break; | |
1840 | ||
ccf43786 SH |
1841 | case OVS_ACTION_ATTR_PUSH_MPLS: { |
1842 | const struct ovs_action_push_mpls *mpls = nla_data(a); | |
1843 | ||
1844 | if (!eth_p_mpls(mpls->mpls_ethertype)) | |
1845 | return -EINVAL; | |
92a39735 | 1846 | |
ccf43786 SH |
1847 | /* Prohibit push MPLS other than to a white list |
1848 | * for packets that have a known tag order. | |
e0b8f73f | 1849 | */ |
ccf43786 SH |
1850 | if (vlan_tci & htons(VLAN_TAG_PRESENT) || |
1851 | (eth_type != htons(ETH_P_IP) && | |
1852 | eth_type != htons(ETH_P_IPV6) && | |
1853 | eth_type != htons(ETH_P_ARP) && | |
1854 | eth_type != htons(ETH_P_RARP) && | |
1855 | !eth_p_mpls(eth_type))) | |
1856 | return -EINVAL; | |
1857 | eth_type = mpls->mpls_ethertype; | |
1858 | break; | |
1859 | } | |
1860 | ||
1861 | case OVS_ACTION_ATTR_POP_MPLS: | |
1862 | if (vlan_tci & htons(VLAN_TAG_PRESENT) || | |
1863 | !eth_p_mpls(eth_type)) | |
1864 | return -EINVAL; | |
1865 | ||
1866 | /* Disallow subsequent L2.5+ set and mpls_pop actions | |
1867 | * as there is no check here to ensure that the new | |
1868 | * eth_type is valid and thus set actions could | |
1869 | * write off the end of the packet or otherwise | |
1870 | * corrupt it. | |
1871 | * | |
1872 | * Support for these actions is planned using packet | |
1873 | * recirculation. | |
1874 | */ | |
1875 | eth_type = htons(0); | |
1876 | break; | |
1877 | ||
a097c0b2 | 1878 | case OVS_ACTION_ATTR_SET: |
2baf0e0c | 1879 | err = validate_set(a, key, sfa, |
92a39735 | 1880 | &skip_copy, eth_type, log); |
a097c0b2 PS |
1881 | if (err) |
1882 | return err; | |
1883 | break; | |
1884 | ||
1885 | case OVS_ACTION_ATTR_SAMPLE: | |
ccf43786 | 1886 | err = validate_and_copy_sample(a, key, depth, sfa, |
9233cef7 | 1887 | eth_type, vlan_tci, log); |
a097c0b2 PS |
1888 | if (err) |
1889 | return err; | |
1890 | skip_copy = true; | |
1891 | break; | |
1892 | ||
1893 | default: | |
7d16c847 | 1894 | OVS_NLERR(log, "Unknown Action type %d", type); |
a097c0b2 PS |
1895 | return -EINVAL; |
1896 | } | |
1897 | if (!skip_copy) { | |
9233cef7 | 1898 | err = copy_action(a, sfa, log); |
a097c0b2 PS |
1899 | if (err) |
1900 | return err; | |
1901 | } | |
1902 | } | |
1903 | ||
1904 | if (rem > 0) | |
1905 | return -EINVAL; | |
1906 | ||
1907 | return 0; | |
1908 | } | |
1909 | ||
ccf43786 SH |
1910 | int ovs_nla_copy_actions(const struct nlattr *attr, |
1911 | const struct sw_flow_key *key, | |
9233cef7 | 1912 | struct sw_flow_actions **sfa, bool log) |
ccf43786 | 1913 | { |
ff27161e PS |
1914 | int err; |
1915 | ||
9233cef7 | 1916 | *sfa = nla_alloc_flow_actions(nla_len(attr), log); |
ff27161e PS |
1917 | if (IS_ERR(*sfa)) |
1918 | return PTR_ERR(*sfa); | |
1919 | ||
1920 | err = __ovs_nla_copy_actions(attr, key, 0, sfa, key->eth.type, | |
9233cef7 | 1921 | key->eth.tci, log); |
ff27161e PS |
1922 | if (err) |
1923 | kfree(*sfa); | |
1924 | ||
1925 | return err; | |
ccf43786 SH |
1926 | } |
1927 | ||
a097c0b2 PS |
1928 | static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb) |
1929 | { | |
1930 | const struct nlattr *a; | |
1931 | struct nlattr *start; | |
1932 | int err = 0, rem; | |
1933 | ||
1934 | start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE); | |
1935 | if (!start) | |
1936 | return -EMSGSIZE; | |
1937 | ||
1938 | nla_for_each_nested(a, attr, rem) { | |
1939 | int type = nla_type(a); | |
1940 | struct nlattr *st_sample; | |
1941 | ||
1942 | switch (type) { | |
1943 | case OVS_SAMPLE_ATTR_PROBABILITY: | |
1944 | if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, | |
1945 | sizeof(u32), nla_data(a))) | |
1946 | return -EMSGSIZE; | |
1947 | break; | |
1948 | case OVS_SAMPLE_ATTR_ACTIONS: | |
1949 | st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS); | |
1950 | if (!st_sample) | |
1951 | return -EMSGSIZE; | |
1952 | err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb); | |
1953 | if (err) | |
1954 | return err; | |
1955 | nla_nest_end(skb, st_sample); | |
1956 | break; | |
1957 | } | |
1958 | } | |
1959 | ||
1960 | nla_nest_end(skb, start); | |
1961 | return err; | |
1962 | } | |
1963 | ||
1964 | static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb) | |
1965 | { | |
1966 | const struct nlattr *ovs_key = nla_data(a); | |
1967 | int key_type = nla_type(ovs_key); | |
1968 | struct nlattr *start; | |
1969 | int err; | |
1970 | ||
1971 | switch (key_type) { | |
f0cd669f JG |
1972 | case OVS_KEY_ATTR_TUNNEL_INFO: { |
1973 | struct ovs_tunnel_info *tun_info = nla_data(ovs_key); | |
1974 | ||
a097c0b2 PS |
1975 | start = nla_nest_start(skb, OVS_ACTION_ATTR_SET); |
1976 | if (!start) | |
1977 | return -EMSGSIZE; | |
1978 | ||
f0cd669f | 1979 | err = ipv4_tun_to_nlattr(skb, &tun_info->tunnel, |
c1fc1411 JG |
1980 | tun_info->options_len ? |
1981 | tun_info->options : NULL, | |
1982 | tun_info->options_len); | |
a097c0b2 PS |
1983 | if (err) |
1984 | return err; | |
1985 | nla_nest_end(skb, start); | |
1986 | break; | |
f0cd669f | 1987 | } |
a097c0b2 PS |
1988 | default: |
1989 | if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key)) | |
1990 | return -EMSGSIZE; | |
1991 | break; | |
1992 | } | |
1993 | ||
1994 | return 0; | |
1995 | } | |
1996 | ||
1997 | int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb) | |
1998 | { | |
1999 | const struct nlattr *a; | |
2000 | int rem, err; | |
2001 | ||
2002 | nla_for_each_attr(a, attr, len, rem) { | |
2003 | int type = nla_type(a); | |
2004 | ||
2005 | switch (type) { | |
2006 | case OVS_ACTION_ATTR_SET: | |
2007 | err = set_action_to_attr(a, skb); | |
2008 | if (err) | |
2009 | return err; | |
2010 | break; | |
2011 | ||
2012 | case OVS_ACTION_ATTR_SAMPLE: | |
2013 | err = sample_action_to_attr(a, skb); | |
2014 | if (err) | |
2015 | return err; | |
2016 | break; | |
2017 | default: | |
2018 | if (nla_put(skb, type, nla_len(a), nla_data(a))) | |
2019 | return -EMSGSIZE; | |
2020 | break; | |
2021 | } | |
2022 | } | |
2023 | ||
2024 | return 0; | |
2025 | } |