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