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