]> git.proxmox.com Git - ovs.git/blob - datapath/flow_netlink.c
df9d88ee5b8b4ac689b747dc188495805dd654f1
[ovs.git] / datapath / flow_netlink.c
1 /*
2 * Copyright (c) 2007-2017 Nicira, Inc.
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
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/uaccess.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <net/llc_pdu.h>
27 #include <linux/kernel.h>
28 #include <linux/jhash.h>
29 #include <linux/jiffies.h>
30 #include <linux/llc.h>
31 #include <linux/module.h>
32 #include <linux/in.h>
33 #include <linux/rcupdate.h>
34 #include <linux/if_arp.h>
35 #include <linux/ip.h>
36 #include <linux/ipv6.h>
37 #include <linux/sctp.h>
38 #include <linux/tcp.h>
39 #include <linux/udp.h>
40 #include <linux/icmp.h>
41 #include <linux/icmpv6.h>
42 #include <linux/rculist.h>
43 #include <net/geneve.h>
44 #include <net/ip.h>
45 #include <net/ipv6.h>
46 #include <net/ndisc.h>
47 #include <net/mpls.h>
48 #include <net/vxlan.h>
49
50 #include "datapath.h"
51 #include "conntrack.h"
52 #include "flow.h"
53 #include "flow_netlink.h"
54 #include "gso.h"
55
56 struct ovs_len_tbl {
57 int len;
58 const struct ovs_len_tbl *next;
59 };
60
61 #define OVS_ATTR_NESTED -1
62 #define OVS_ATTR_VARIABLE -2
63
64 static bool actions_may_change_flow(const struct nlattr *actions)
65 {
66 struct nlattr *nla;
67 int rem;
68
69 nla_for_each_nested(nla, actions, rem) {
70 u16 action = nla_type(nla);
71
72 switch (action) {
73 case OVS_ACTION_ATTR_OUTPUT:
74 case OVS_ACTION_ATTR_RECIRC:
75 case OVS_ACTION_ATTR_TRUNC:
76 case OVS_ACTION_ATTR_USERSPACE:
77 break;
78
79 case OVS_ACTION_ATTR_CT:
80 case OVS_ACTION_ATTR_HASH:
81 case OVS_ACTION_ATTR_POP_ETH:
82 case OVS_ACTION_ATTR_POP_MPLS:
83 case OVS_ACTION_ATTR_POP_VLAN:
84 case OVS_ACTION_ATTR_PUSH_ETH:
85 case OVS_ACTION_ATTR_PUSH_MPLS:
86 case OVS_ACTION_ATTR_PUSH_VLAN:
87 case OVS_ACTION_ATTR_SAMPLE:
88 case OVS_ACTION_ATTR_SET:
89 case OVS_ACTION_ATTR_SET_MASKED:
90 default:
91 return true;
92 }
93 }
94 return false;
95 }
96
97 static void update_range(struct sw_flow_match *match,
98 size_t offset, size_t size, bool is_mask)
99 {
100 struct sw_flow_key_range *range;
101 size_t start = rounddown(offset, sizeof(long));
102 size_t end = roundup(offset + size, sizeof(long));
103
104 if (!is_mask)
105 range = &match->range;
106 else
107 range = &match->mask->range;
108
109 if (range->start == range->end) {
110 range->start = start;
111 range->end = end;
112 return;
113 }
114
115 if (range->start > start)
116 range->start = start;
117
118 if (range->end < end)
119 range->end = end;
120 }
121
122 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
123 do { \
124 update_range(match, offsetof(struct sw_flow_key, field), \
125 sizeof((match)->key->field), is_mask); \
126 if (is_mask) \
127 (match)->mask->key.field = value; \
128 else \
129 (match)->key->field = value; \
130 } while (0)
131
132 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
133 do { \
134 update_range(match, offset, len, is_mask); \
135 if (is_mask) \
136 memcpy((u8 *)&(match)->mask->key + offset, value_p, len);\
137 else \
138 memcpy((u8 *)(match)->key + offset, value_p, len); \
139 } while (0)
140
141 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
142 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
143 value_p, len, is_mask)
144
145 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
146 do { \
147 update_range(match, offsetof(struct sw_flow_key, field), \
148 sizeof((match)->key->field), is_mask); \
149 if (is_mask) \
150 memset((u8 *)&(match)->mask->key.field, value, \
151 sizeof((match)->mask->key.field)); \
152 else \
153 memset((u8 *)&(match)->key->field, value, \
154 sizeof((match)->key->field)); \
155 } while (0)
156
157 static bool match_validate(const struct sw_flow_match *match,
158 u64 key_attrs, u64 mask_attrs, bool log)
159 {
160 u64 key_expected = 0;
161 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
162
163 /* The following mask attributes allowed only if they
164 * pass the validation tests.
165 */
166 mask_allowed &= ~((1ULL << OVS_KEY_ATTR_IPV4)
167 | (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
168 | (1ULL << OVS_KEY_ATTR_IPV6)
169 | (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
170 | (1ULL << OVS_KEY_ATTR_TCP)
171 | (1ULL << OVS_KEY_ATTR_TCP_FLAGS)
172 | (1ULL << OVS_KEY_ATTR_UDP)
173 | (1ULL << OVS_KEY_ATTR_SCTP)
174 | (1ULL << OVS_KEY_ATTR_ICMP)
175 | (1ULL << OVS_KEY_ATTR_ICMPV6)
176 | (1ULL << OVS_KEY_ATTR_ARP)
177 | (1ULL << OVS_KEY_ATTR_ND)
178 | (1ULL << OVS_KEY_ATTR_MPLS));
179
180 /* Always allowed mask fields. */
181 mask_allowed |= ((1ULL << OVS_KEY_ATTR_TUNNEL)
182 | (1ULL << OVS_KEY_ATTR_IN_PORT)
183 | (1ULL << OVS_KEY_ATTR_ETHERTYPE));
184
185 /* Check key attributes. */
186 if (match->key->eth.type == htons(ETH_P_ARP)
187 || match->key->eth.type == htons(ETH_P_RARP)) {
188 key_expected |= 1ULL << OVS_KEY_ATTR_ARP;
189 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
190 mask_allowed |= 1ULL << OVS_KEY_ATTR_ARP;
191 }
192
193 if (eth_p_mpls(match->key->eth.type)) {
194 key_expected |= 1ULL << OVS_KEY_ATTR_MPLS;
195 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
196 mask_allowed |= 1ULL << OVS_KEY_ATTR_MPLS;
197 }
198
199 if (match->key->eth.type == htons(ETH_P_IP)) {
200 key_expected |= 1ULL << OVS_KEY_ATTR_IPV4;
201 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
202 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV4;
203 mask_allowed |= 1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
204 }
205
206 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
207 if (match->key->ip.proto == IPPROTO_UDP) {
208 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
209 if (match->mask && (match->mask->key.ip.proto == 0xff))
210 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
211 }
212
213 if (match->key->ip.proto == IPPROTO_SCTP) {
214 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
215 if (match->mask && (match->mask->key.ip.proto == 0xff))
216 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
217 }
218
219 if (match->key->ip.proto == IPPROTO_TCP) {
220 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
221 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
222 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
223 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
224 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
225 }
226 }
227
228 if (match->key->ip.proto == IPPROTO_ICMP) {
229 key_expected |= 1ULL << OVS_KEY_ATTR_ICMP;
230 if (match->mask && (match->mask->key.ip.proto == 0xff))
231 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMP;
232 }
233 }
234 }
235
236 if (match->key->eth.type == htons(ETH_P_IPV6)) {
237 key_expected |= 1ULL << OVS_KEY_ATTR_IPV6;
238 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
239 mask_allowed |= 1ULL << OVS_KEY_ATTR_IPV6;
240 mask_allowed |= 1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
241 }
242
243 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
244 if (match->key->ip.proto == IPPROTO_UDP) {
245 key_expected |= 1ULL << OVS_KEY_ATTR_UDP;
246 if (match->mask && (match->mask->key.ip.proto == 0xff))
247 mask_allowed |= 1ULL << OVS_KEY_ATTR_UDP;
248 }
249
250 if (match->key->ip.proto == IPPROTO_SCTP) {
251 key_expected |= 1ULL << OVS_KEY_ATTR_SCTP;
252 if (match->mask && (match->mask->key.ip.proto == 0xff))
253 mask_allowed |= 1ULL << OVS_KEY_ATTR_SCTP;
254 }
255
256 if (match->key->ip.proto == IPPROTO_TCP) {
257 key_expected |= 1ULL << OVS_KEY_ATTR_TCP;
258 key_expected |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
259 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
260 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP;
261 mask_allowed |= 1ULL << OVS_KEY_ATTR_TCP_FLAGS;
262 }
263 }
264
265 if (match->key->ip.proto == IPPROTO_ICMPV6) {
266 key_expected |= 1ULL << OVS_KEY_ATTR_ICMPV6;
267 if (match->mask && (match->mask->key.ip.proto == 0xff))
268 mask_allowed |= 1ULL << OVS_KEY_ATTR_ICMPV6;
269
270 if (match->key->tp.src ==
271 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
272 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
273 key_expected |= 1ULL << OVS_KEY_ATTR_ND;
274 /* Original direction conntrack tuple
275 * uses the same space as the ND fields
276 * in the key, so both are not allowed
277 * at the same time.
278 */
279 mask_allowed &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
280 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
281 mask_allowed |= 1ULL << OVS_KEY_ATTR_ND;
282 }
283 }
284 }
285 }
286
287 if ((key_attrs & key_expected) != key_expected) {
288 /* Key attributes check failed. */
289 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
290 (unsigned long long)key_attrs,
291 (unsigned long long)key_expected);
292 return false;
293 }
294
295 if ((mask_attrs & mask_allowed) != mask_attrs) {
296 /* Mask attributes check failed. */
297 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
298 (unsigned long long)mask_attrs,
299 (unsigned long long)mask_allowed);
300 return false;
301 }
302
303 return true;
304 }
305
306 size_t ovs_tun_key_attr_size(void)
307 {
308 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
309 * updating this function.
310 */
311 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
312 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
313 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
314 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
315 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
316 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
317 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
318 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
319 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
320 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
321 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
322 */
323 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
324 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
325 }
326
327 size_t ovs_key_attr_size(void)
328 {
329 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
330 * updating this function.
331 */
332 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 28);
333
334 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
335 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
336 + ovs_tun_key_attr_size()
337 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
338 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
339 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
340 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
341 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
342 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
343 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
344 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
345 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
346 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
347 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
348 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
349 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
350 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
351 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
352 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
353 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
354 }
355
356 static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
357 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
358 };
359
360 static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
361 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
362 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
363 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
364 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
365 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
366 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
367 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
368 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
369 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
370 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
371 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
372 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
373 .next = ovs_vxlan_ext_key_lens },
374 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
375 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
376 };
377
378 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
379 static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
380 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
381 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
382 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
383 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
384 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
385 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
386 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
387 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
388 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
389 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
390 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
391 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
392 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
393 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
394 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
395 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
396 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
397 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
398 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
399 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
400 .next = ovs_tunnel_key_lens, },
401 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
402 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
403 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
404 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
405 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
406 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
407 .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
408 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
409 .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
410 };
411
412 static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
413 {
414 return expected_len == attr_len ||
415 expected_len == OVS_ATTR_NESTED ||
416 expected_len == OVS_ATTR_VARIABLE;
417 }
418
419 static bool is_all_zero(const u8 *fp, size_t size)
420 {
421 int i;
422
423 if (!fp)
424 return false;
425
426 for (i = 0; i < size; i++)
427 if (fp[i])
428 return false;
429
430 return true;
431 }
432
433 static int __parse_flow_nlattrs(const struct nlattr *attr,
434 const struct nlattr *a[],
435 u64 *attrsp, bool log, bool nz)
436 {
437 const struct nlattr *nla;
438 u64 attrs;
439 int rem;
440
441 attrs = *attrsp;
442 nla_for_each_nested(nla, attr, rem) {
443 u16 type = nla_type(nla);
444 int expected_len;
445
446 if (type > OVS_KEY_ATTR_MAX) {
447 OVS_NLERR(log, "Key type %d is out of range max %d",
448 type, OVS_KEY_ATTR_MAX);
449 return -EINVAL;
450 }
451
452 if (attrs & (1ULL << type)) {
453 OVS_NLERR(log, "Duplicate key (type %d).", type);
454 return -EINVAL;
455 }
456
457 expected_len = ovs_key_lens[type].len;
458 if (!check_attr_len(nla_len(nla), expected_len)) {
459 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
460 type, nla_len(nla), expected_len);
461 return -EINVAL;
462 }
463
464 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
465 attrs |= 1ULL << type;
466 a[type] = nla;
467 }
468 }
469 if (rem) {
470 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
471 return -EINVAL;
472 }
473
474 *attrsp = attrs;
475 return 0;
476 }
477
478 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
479 const struct nlattr *a[], u64 *attrsp,
480 bool log)
481 {
482 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
483 }
484
485 int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
486 u64 *attrsp, bool log)
487 {
488 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
489 }
490
491 static int genev_tun_opt_from_nlattr(const struct nlattr *a,
492 struct sw_flow_match *match, bool is_mask,
493 bool log)
494 {
495 unsigned long opt_key_offset;
496
497 if (nla_len(a) > sizeof(match->key->tun_opts)) {
498 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
499 nla_len(a), sizeof(match->key->tun_opts));
500 return -EINVAL;
501 }
502
503 if (nla_len(a) % 4 != 0) {
504 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
505 nla_len(a));
506 return -EINVAL;
507 }
508
509 /* We need to record the length of the options passed
510 * down, otherwise packets with the same format but
511 * additional options will be silently matched.
512 */
513 if (!is_mask) {
514 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
515 false);
516 } else {
517 /* This is somewhat unusual because it looks at
518 * both the key and mask while parsing the
519 * attributes (and by extension assumes the key
520 * is parsed first). Normally, we would verify
521 * that each is the correct length and that the
522 * attributes line up in the validate function.
523 * However, that is difficult because this is
524 * variable length and we won't have the
525 * information later.
526 */
527 if (match->key->tun_opts_len != nla_len(a)) {
528 OVS_NLERR(log, "Geneve option len %d != mask len %d",
529 match->key->tun_opts_len, nla_len(a));
530 return -EINVAL;
531 }
532
533 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
534 }
535
536 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
537 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
538 nla_len(a), is_mask);
539 return 0;
540 }
541
542 static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
543 struct sw_flow_match *match, bool is_mask,
544 bool log)
545 {
546 struct nlattr *a;
547 int rem;
548 unsigned long opt_key_offset;
549 struct vxlan_metadata opts;
550
551 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
552
553 memset(&opts, 0, sizeof(opts));
554 nla_for_each_nested(a, attr, rem) {
555 int type = nla_type(a);
556
557 if (type > OVS_VXLAN_EXT_MAX) {
558 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
559 type, OVS_VXLAN_EXT_MAX);
560 return -EINVAL;
561 }
562
563 if (!check_attr_len(nla_len(a),
564 ovs_vxlan_ext_key_lens[type].len)) {
565 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
566 type, nla_len(a),
567 ovs_vxlan_ext_key_lens[type].len);
568 return -EINVAL;
569 }
570
571 switch (type) {
572 case OVS_VXLAN_EXT_GBP:
573 opts.gbp = nla_get_u32(a);
574 break;
575 default:
576 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
577 type);
578 return -EINVAL;
579 }
580 }
581 if (rem) {
582 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
583 rem);
584 return -EINVAL;
585 }
586
587 if (!is_mask)
588 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
589 else
590 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
591
592 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
593 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
594 is_mask);
595 return 0;
596 }
597
598 static int ip_tun_from_nlattr(const struct nlattr *attr,
599 struct sw_flow_match *match, bool is_mask,
600 bool log)
601 {
602 bool ttl = false, ipv4 = false, ipv6 = false;
603 __be16 tun_flags = 0;
604 int opts_type = 0;
605 struct nlattr *a;
606 int rem;
607
608 nla_for_each_nested(a, attr, rem) {
609 int type = nla_type(a);
610 int err;
611
612 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
613 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
614 type, OVS_TUNNEL_KEY_ATTR_MAX);
615 return -EINVAL;
616 }
617
618 if (!check_attr_len(nla_len(a),
619 ovs_tunnel_key_lens[type].len)) {
620 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
621 type, nla_len(a), ovs_tunnel_key_lens[type].len);
622 return -EINVAL;
623 }
624
625 switch (type) {
626 case OVS_TUNNEL_KEY_ATTR_ID:
627 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
628 nla_get_be64(a), is_mask);
629 tun_flags |= TUNNEL_KEY;
630 break;
631 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
632 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
633 nla_get_in_addr(a), is_mask);
634 ipv4 = true;
635 break;
636 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
637 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
638 nla_get_in_addr(a), is_mask);
639 ipv4 = true;
640 break;
641 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
642 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
643 nla_get_in6_addr(a), is_mask);
644 ipv6 = true;
645 break;
646 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
647 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
648 nla_get_in6_addr(a), is_mask);
649 ipv6 = true;
650 break;
651 case OVS_TUNNEL_KEY_ATTR_TOS:
652 SW_FLOW_KEY_PUT(match, tun_key.tos,
653 nla_get_u8(a), is_mask);
654 break;
655 case OVS_TUNNEL_KEY_ATTR_TTL:
656 SW_FLOW_KEY_PUT(match, tun_key.ttl,
657 nla_get_u8(a), is_mask);
658 ttl = true;
659 break;
660 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
661 tun_flags |= TUNNEL_DONT_FRAGMENT;
662 break;
663 case OVS_TUNNEL_KEY_ATTR_CSUM:
664 tun_flags |= TUNNEL_CSUM;
665 break;
666 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
667 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
668 nla_get_be16(a), is_mask);
669 break;
670 case OVS_TUNNEL_KEY_ATTR_TP_DST:
671 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
672 nla_get_be16(a), is_mask);
673 break;
674 case OVS_TUNNEL_KEY_ATTR_OAM:
675 tun_flags |= TUNNEL_OAM;
676 break;
677 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
678 if (opts_type) {
679 OVS_NLERR(log, "Multiple metadata blocks provided");
680 return -EINVAL;
681 }
682
683 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
684 if (err)
685 return err;
686
687 tun_flags |= TUNNEL_GENEVE_OPT;
688 opts_type = type;
689 break;
690 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
691 if (opts_type) {
692 OVS_NLERR(log, "Multiple metadata blocks provided");
693 return -EINVAL;
694 }
695
696 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
697 if (err)
698 return err;
699
700 tun_flags |= TUNNEL_VXLAN_OPT;
701 opts_type = type;
702 break;
703 case OVS_TUNNEL_KEY_ATTR_PAD:
704 break;
705 default:
706 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
707 type);
708 return -EINVAL;
709 }
710 }
711
712 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
713 if (is_mask)
714 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
715 else
716 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
717 false);
718
719 if (rem > 0) {
720 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
721 rem);
722 return -EINVAL;
723 }
724
725 if (ipv4 && ipv6) {
726 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
727 return -EINVAL;
728 }
729
730 if (!is_mask) {
731 if (!ipv4 && !ipv6) {
732 OVS_NLERR(log, "IP tunnel dst address not specified");
733 return -EINVAL;
734 }
735 if (ipv4 && !match->key->tun_key.u.ipv4.dst) {
736 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
737 return -EINVAL;
738 }
739 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
740 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
741 return -EINVAL;
742 }
743
744 if (!ttl) {
745 OVS_NLERR(log, "IP tunnel TTL not specified.");
746 return -EINVAL;
747 }
748 }
749
750 return opts_type;
751 }
752
753 static int vxlan_opt_to_nlattr(struct sk_buff *skb,
754 const void *tun_opts, int swkey_tun_opts_len)
755 {
756 const struct vxlan_metadata *opts = tun_opts;
757 struct nlattr *nla;
758
759 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
760 if (!nla)
761 return -EMSGSIZE;
762
763 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
764 return -EMSGSIZE;
765
766 nla_nest_end(skb, nla);
767 return 0;
768 }
769
770 static int __ip_tun_to_nlattr(struct sk_buff *skb,
771 const struct ip_tunnel_key *output,
772 const void *tun_opts, int swkey_tun_opts_len,
773 unsigned short tun_proto)
774 {
775 if (output->tun_flags & TUNNEL_KEY &&
776 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
777 OVS_TUNNEL_KEY_ATTR_PAD))
778 return -EMSGSIZE;
779 switch (tun_proto) {
780 case AF_INET:
781 if (output->u.ipv4.src &&
782 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
783 output->u.ipv4.src))
784 return -EMSGSIZE;
785 if (output->u.ipv4.dst &&
786 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
787 output->u.ipv4.dst))
788 return -EMSGSIZE;
789 break;
790 case AF_INET6:
791 if (!ipv6_addr_any(&output->u.ipv6.src) &&
792 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
793 &output->u.ipv6.src))
794 return -EMSGSIZE;
795 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
796 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
797 &output->u.ipv6.dst))
798 return -EMSGSIZE;
799 break;
800 }
801 if (output->tos &&
802 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
803 return -EMSGSIZE;
804 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
805 return -EMSGSIZE;
806 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
807 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
808 return -EMSGSIZE;
809 if ((output->tun_flags & TUNNEL_CSUM) &&
810 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
811 return -EMSGSIZE;
812 if (output->tp_src &&
813 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
814 return -EMSGSIZE;
815 if (output->tp_dst &&
816 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
817 return -EMSGSIZE;
818 if ((output->tun_flags & TUNNEL_OAM) &&
819 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
820 return -EMSGSIZE;
821 if (swkey_tun_opts_len) {
822 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
823 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
824 swkey_tun_opts_len, tun_opts))
825 return -EMSGSIZE;
826 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
827 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
828 return -EMSGSIZE;
829 }
830
831 return 0;
832 }
833
834 static int ip_tun_to_nlattr(struct sk_buff *skb,
835 const struct ip_tunnel_key *output,
836 const void *tun_opts, int swkey_tun_opts_len,
837 unsigned short tun_proto)
838 {
839 struct nlattr *nla;
840 int err;
841
842 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
843 if (!nla)
844 return -EMSGSIZE;
845
846 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
847 tun_proto);
848 if (err)
849 return err;
850
851 nla_nest_end(skb, nla);
852 return 0;
853 }
854
855 int ovs_nla_put_tunnel_info(struct sk_buff *skb,
856 struct ip_tunnel_info *tun_info)
857 {
858 return __ip_tun_to_nlattr(skb, &tun_info->key,
859 ip_tunnel_info_opts(tun_info),
860 tun_info->options_len,
861 ip_tunnel_info_af(tun_info));
862 }
863
864 static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
865 const struct nlattr *a[],
866 bool is_mask, bool inner)
867 {
868 __be16 tci = 0;
869 __be16 tpid = 0;
870
871 if (a[OVS_KEY_ATTR_VLAN])
872 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
873
874 if (a[OVS_KEY_ATTR_ETHERTYPE])
875 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
876
877 if (likely(!inner)) {
878 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
879 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
880 } else {
881 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
882 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
883 }
884 return 0;
885 }
886
887 static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
888 u64 key_attrs, bool inner,
889 const struct nlattr **a, bool log)
890 {
891 __be16 tci = 0;
892
893 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
894 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
895 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
896 /* Not a VLAN. */
897 return 0;
898 }
899
900 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
901 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
902 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
903 return -EINVAL;
904 }
905
906 if (a[OVS_KEY_ATTR_VLAN])
907 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
908
909 if (!(tci & htons(VLAN_TAG_PRESENT))) {
910 if (tci) {
911 OVS_NLERR(log, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
912 (inner) ? "C-VLAN" : "VLAN");
913 return -EINVAL;
914 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
915 /* Corner case for truncated VLAN header. */
916 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
917 (inner) ? "C-VLAN" : "VLAN");
918 return -EINVAL;
919 }
920 }
921
922 return 1;
923 }
924
925 static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
926 u64 key_attrs, bool inner,
927 const struct nlattr **a, bool log)
928 {
929 __be16 tci = 0;
930 __be16 tpid = 0;
931 bool encap_valid = !!(match->key->eth.vlan.tci &
932 htons(VLAN_TAG_PRESENT));
933 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
934 htons(VLAN_TAG_PRESENT));
935
936 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
937 /* Not a VLAN. */
938 return 0;
939 }
940
941 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
942 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
943 (inner) ? "C-VLAN" : "VLAN");
944 return -EINVAL;
945 }
946
947 if (a[OVS_KEY_ATTR_VLAN])
948 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
949
950 if (a[OVS_KEY_ATTR_ETHERTYPE])
951 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
952
953 if (tpid != htons(0xffff)) {
954 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
955 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
956 return -EINVAL;
957 }
958 if (!(tci & htons(VLAN_TAG_PRESENT))) {
959 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
960 (inner) ? "C-VLAN" : "VLAN");
961 return -EINVAL;
962 }
963
964 return 1;
965 }
966
967 static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
968 u64 *key_attrs, bool inner,
969 const struct nlattr **a, bool is_mask,
970 bool log)
971 {
972 int err;
973 const struct nlattr *encap;
974
975 if (!is_mask)
976 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
977 a, log);
978 else
979 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
980 a, log);
981 if (err <= 0)
982 return err;
983
984 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
985 if (err)
986 return err;
987
988 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
989 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
990 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
991
992 encap = a[OVS_KEY_ATTR_ENCAP];
993
994 if (!is_mask)
995 err = parse_flow_nlattrs(encap, a, key_attrs, log);
996 else
997 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
998
999 return err;
1000 }
1001
1002 static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
1003 u64 *key_attrs, const struct nlattr **a,
1004 bool is_mask, bool log)
1005 {
1006 int err;
1007 bool encap_valid = false;
1008
1009 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
1010 is_mask, log);
1011 if (err)
1012 return err;
1013
1014 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_TAG_PRESENT));
1015 if (encap_valid) {
1016 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
1017 is_mask, log);
1018 if (err)
1019 return err;
1020 }
1021
1022 return 0;
1023 }
1024
1025 static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
1026 u64 *attrs, const struct nlattr **a,
1027 bool is_mask, bool log)
1028 {
1029 __be16 eth_type;
1030
1031 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1032 if (is_mask) {
1033 /* Always exact match EtherType. */
1034 eth_type = htons(0xffff);
1035 } else if (!eth_proto_is_802_3(eth_type)) {
1036 OVS_NLERR(log, "EtherType %x is less than min %x",
1037 ntohs(eth_type), ETH_P_802_3_MIN);
1038 return -EINVAL;
1039 }
1040
1041 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1042 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1043 return 0;
1044 }
1045
1046 static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1047 u64 *attrs, const struct nlattr **a,
1048 bool is_mask, bool log)
1049 {
1050 u8 mac_proto = MAC_PROTO_ETHERNET;
1051
1052 if (*attrs & (1ULL << OVS_KEY_ATTR_DP_HASH)) {
1053 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1054
1055 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1056 *attrs &= ~(1ULL << OVS_KEY_ATTR_DP_HASH);
1057 }
1058
1059 if (*attrs & (1ULL << OVS_KEY_ATTR_RECIRC_ID)) {
1060 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1061
1062 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1063 *attrs &= ~(1ULL << OVS_KEY_ATTR_RECIRC_ID);
1064 }
1065
1066 if (*attrs & (1ULL << OVS_KEY_ATTR_PRIORITY)) {
1067 SW_FLOW_KEY_PUT(match, phy.priority,
1068 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1069 *attrs &= ~(1ULL << OVS_KEY_ATTR_PRIORITY);
1070 }
1071
1072 if (*attrs & (1ULL << OVS_KEY_ATTR_IN_PORT)) {
1073 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1074
1075 if (is_mask) {
1076 in_port = 0xffffffff; /* Always exact match in_port. */
1077 } else if (in_port >= DP_MAX_PORTS) {
1078 OVS_NLERR(log, "Port %d exceeds max allowable %d",
1079 in_port, DP_MAX_PORTS);
1080 return -EINVAL;
1081 }
1082
1083 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1084 *attrs &= ~(1ULL << OVS_KEY_ATTR_IN_PORT);
1085 } else if (!is_mask) {
1086 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1087 }
1088
1089 if (*attrs & (1ULL << OVS_KEY_ATTR_SKB_MARK)) {
1090 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1091
1092 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1093 *attrs &= ~(1ULL << OVS_KEY_ATTR_SKB_MARK);
1094 }
1095 if (*attrs & (1ULL << OVS_KEY_ATTR_TUNNEL)) {
1096 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1097 is_mask, log) < 0)
1098 return -EINVAL;
1099 *attrs &= ~(1ULL << OVS_KEY_ATTR_TUNNEL);
1100 }
1101
1102 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
1103 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
1104 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
1105
1106 if (ct_state & ~CT_SUPPORTED_MASK) {
1107 OVS_NLERR(log, "ct_state flags %08x unsupported",
1108 ct_state);
1109 return -EINVAL;
1110 }
1111
1112 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
1113 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1114 }
1115 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
1116 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
1117 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1118
1119 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
1120 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1121 }
1122 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
1123 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
1124 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1125
1126 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1127 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1128 }
1129 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1130 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1131 const struct ovs_key_ct_labels *cl;
1132
1133 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1134 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
1135 sizeof(*cl), is_mask);
1136 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
1137 }
1138 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1139 const struct ovs_key_ct_tuple_ipv4 *ct;
1140
1141 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1142
1143 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1144 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1145 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1146 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
1147 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
1148 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1149 }
1150 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1151 const struct ovs_key_ct_tuple_ipv6 *ct;
1152
1153 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1154
1155 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1156 sizeof(match->key->ipv6.ct_orig.src),
1157 is_mask);
1158 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1159 sizeof(match->key->ipv6.ct_orig.dst),
1160 is_mask);
1161 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1162 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
1163 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
1164 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1165 }
1166
1167 /* For layer 3 packets the Ethernet type is provided
1168 * and treated as metadata but no MAC addresses are provided.
1169 */
1170 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1171 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1172 mac_proto = MAC_PROTO_NONE;
1173
1174 /* Always exact match mac_proto */
1175 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1176
1177 if (mac_proto == MAC_PROTO_NONE)
1178 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1179 log);
1180
1181 return 0;
1182 }
1183
1184 static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1185 u64 attrs, const struct nlattr **a,
1186 bool is_mask, bool log)
1187 {
1188 int err;
1189
1190 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
1191 if (err)
1192 return err;
1193
1194 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) {
1195 const struct ovs_key_ethernet *eth_key;
1196
1197 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1198 SW_FLOW_KEY_MEMCPY(match, eth.src,
1199 eth_key->eth_src, ETH_ALEN, is_mask);
1200 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1201 eth_key->eth_dst, ETH_ALEN, is_mask);
1202 attrs &= ~(1ULL << OVS_KEY_ATTR_ETHERNET);
1203
1204 if (attrs & (1ULL << OVS_KEY_ATTR_VLAN)) {
1205 /* VLAN attribute is always parsed before getting here since it
1206 * may occur multiple times.
1207 */
1208 OVS_NLERR(log, "VLAN attribute unexpected.");
1209 return -EINVAL;
1210 }
1211
1212 if (attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)) {
1213 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1214 log);
1215 if (err)
1216 return err;
1217 } else if (!is_mask) {
1218 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1219 }
1220 } else if (!match->key->eth.type) {
1221 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1222 return -EINVAL;
1223 }
1224
1225 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1226 const struct ovs_key_ipv4 *ipv4_key;
1227
1228 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1229 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1230 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1231 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1232 return -EINVAL;
1233 }
1234 SW_FLOW_KEY_PUT(match, ip.proto,
1235 ipv4_key->ipv4_proto, is_mask);
1236 SW_FLOW_KEY_PUT(match, ip.tos,
1237 ipv4_key->ipv4_tos, is_mask);
1238 SW_FLOW_KEY_PUT(match, ip.ttl,
1239 ipv4_key->ipv4_ttl, is_mask);
1240 SW_FLOW_KEY_PUT(match, ip.frag,
1241 ipv4_key->ipv4_frag, is_mask);
1242 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1243 ipv4_key->ipv4_src, is_mask);
1244 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1245 ipv4_key->ipv4_dst, is_mask);
1246 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1247 }
1248
1249 if (attrs & (1ULL << OVS_KEY_ATTR_IPV6)) {
1250 const struct ovs_key_ipv6 *ipv6_key;
1251
1252 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1253 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1254 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1255 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1256 return -EINVAL;
1257 }
1258
1259 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
1260 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x)",
1261 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1262 return -EINVAL;
1263 }
1264
1265 SW_FLOW_KEY_PUT(match, ipv6.label,
1266 ipv6_key->ipv6_label, is_mask);
1267 SW_FLOW_KEY_PUT(match, ip.proto,
1268 ipv6_key->ipv6_proto, is_mask);
1269 SW_FLOW_KEY_PUT(match, ip.tos,
1270 ipv6_key->ipv6_tclass, is_mask);
1271 SW_FLOW_KEY_PUT(match, ip.ttl,
1272 ipv6_key->ipv6_hlimit, is_mask);
1273 SW_FLOW_KEY_PUT(match, ip.frag,
1274 ipv6_key->ipv6_frag, is_mask);
1275 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1276 ipv6_key->ipv6_src,
1277 sizeof(match->key->ipv6.addr.src),
1278 is_mask);
1279 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1280 ipv6_key->ipv6_dst,
1281 sizeof(match->key->ipv6.addr.dst),
1282 is_mask);
1283
1284 attrs &= ~(1ULL << OVS_KEY_ATTR_IPV6);
1285 }
1286
1287 if (attrs & (1ULL << OVS_KEY_ATTR_ARP)) {
1288 const struct ovs_key_arp *arp_key;
1289
1290 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1291 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1292 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
1293 arp_key->arp_op);
1294 return -EINVAL;
1295 }
1296
1297 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1298 arp_key->arp_sip, is_mask);
1299 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1300 arp_key->arp_tip, is_mask);
1301 SW_FLOW_KEY_PUT(match, ip.proto,
1302 ntohs(arp_key->arp_op), is_mask);
1303 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1304 arp_key->arp_sha, ETH_ALEN, is_mask);
1305 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1306 arp_key->arp_tha, ETH_ALEN, is_mask);
1307
1308 attrs &= ~(1ULL << OVS_KEY_ATTR_ARP);
1309 }
1310
1311 if (attrs & (1ULL << OVS_KEY_ATTR_MPLS)) {
1312 const struct ovs_key_mpls *mpls_key;
1313
1314 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1315 SW_FLOW_KEY_PUT(match, mpls.top_lse,
1316 mpls_key->mpls_lse, is_mask);
1317
1318 attrs &= ~(1ULL << OVS_KEY_ATTR_MPLS);
1319 }
1320
1321 if (attrs & (1ULL << OVS_KEY_ATTR_TCP)) {
1322 const struct ovs_key_tcp *tcp_key;
1323
1324 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1325 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1326 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
1327 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP);
1328 }
1329
1330 if (attrs & (1ULL << OVS_KEY_ATTR_TCP_FLAGS)) {
1331 SW_FLOW_KEY_PUT(match, tp.flags,
1332 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1333 is_mask);
1334 attrs &= ~(1ULL << OVS_KEY_ATTR_TCP_FLAGS);
1335 }
1336
1337 if (attrs & (1ULL << OVS_KEY_ATTR_UDP)) {
1338 const struct ovs_key_udp *udp_key;
1339
1340 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1341 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1342 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
1343 attrs &= ~(1ULL << OVS_KEY_ATTR_UDP);
1344 }
1345
1346 if (attrs & (1ULL << OVS_KEY_ATTR_SCTP)) {
1347 const struct ovs_key_sctp *sctp_key;
1348
1349 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1350 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1351 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
1352 attrs &= ~(1ULL << OVS_KEY_ATTR_SCTP);
1353 }
1354
1355 if (attrs & (1ULL << OVS_KEY_ATTR_ICMP)) {
1356 const struct ovs_key_icmp *icmp_key;
1357
1358 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1359 SW_FLOW_KEY_PUT(match, tp.src,
1360 htons(icmp_key->icmp_type), is_mask);
1361 SW_FLOW_KEY_PUT(match, tp.dst,
1362 htons(icmp_key->icmp_code), is_mask);
1363 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMP);
1364 }
1365
1366 if (attrs & (1ULL << OVS_KEY_ATTR_ICMPV6)) {
1367 const struct ovs_key_icmpv6 *icmpv6_key;
1368
1369 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1370 SW_FLOW_KEY_PUT(match, tp.src,
1371 htons(icmpv6_key->icmpv6_type), is_mask);
1372 SW_FLOW_KEY_PUT(match, tp.dst,
1373 htons(icmpv6_key->icmpv6_code), is_mask);
1374 attrs &= ~(1ULL << OVS_KEY_ATTR_ICMPV6);
1375 }
1376
1377 if (attrs & (1ULL << OVS_KEY_ATTR_ND)) {
1378 const struct ovs_key_nd *nd_key;
1379
1380 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1381 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1382 nd_key->nd_target,
1383 sizeof(match->key->ipv6.nd.target),
1384 is_mask);
1385 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1386 nd_key->nd_sll, ETH_ALEN, is_mask);
1387 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1388 nd_key->nd_tll, ETH_ALEN, is_mask);
1389 attrs &= ~(1ULL << OVS_KEY_ATTR_ND);
1390 }
1391
1392 if (attrs != 0) {
1393 OVS_NLERR(log, "Unknown key attributes %llx",
1394 (unsigned long long)attrs);
1395 return -EINVAL;
1396 }
1397
1398 return 0;
1399 }
1400
1401 static void nlattr_set(struct nlattr *attr, u8 val,
1402 const struct ovs_len_tbl *tbl)
1403 {
1404 struct nlattr *nla;
1405 int rem;
1406
1407 /* The nlattr stream should already have been validated */
1408 nla_for_each_nested(nla, attr, rem) {
1409 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1410 if (tbl[nla_type(nla)].next)
1411 tbl = tbl[nla_type(nla)].next;
1412 nlattr_set(nla, val, tbl);
1413 } else {
1414 memset(nla_data(nla), val, nla_len(nla));
1415 }
1416
1417 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1418 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
1419 }
1420 }
1421
1422 static void mask_set_nlattr(struct nlattr *attr, u8 val)
1423 {
1424 nlattr_set(attr, val, ovs_key_lens);
1425 }
1426
1427 /**
1428 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1429 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1430 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1431 * does not include any don't care bit.
1432 * @net: Used to determine per-namespace field support.
1433 * @match: receives the extracted flow match information.
1434 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1435 * sequence. The fields should of the packet that triggered the creation
1436 * of this flow.
1437 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1438 * attribute specifies the mask field of the wildcarded flow.
1439 * @log: Boolean to allow kernel error logging. Normally true, but when
1440 * probing for feature compatibility this should be passed in as false to
1441 * suppress unnecessary error logging.
1442 */
1443 int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
1444 const struct nlattr *nla_key,
1445 const struct nlattr *nla_mask,
1446 bool log)
1447 {
1448 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1449 struct nlattr *newmask = NULL;
1450 u64 key_attrs = 0;
1451 u64 mask_attrs = 0;
1452 int err;
1453
1454 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
1455 if (err)
1456 return err;
1457
1458 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1459 if (err)
1460 return err;
1461
1462 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
1463 if (err)
1464 return err;
1465
1466 if (match->mask) {
1467 if (!nla_mask) {
1468 /* Create an exact match mask. We need to set to 0xff
1469 * all the 'match->mask' fields that have been touched
1470 * in 'match->key'. We cannot simply memset
1471 * 'match->mask', because padding bytes and fields not
1472 * specified in 'match->key' should be left to 0.
1473 * Instead, we use a stream of netlink attributes,
1474 * copied from 'key' and set to 0xff.
1475 * ovs_key_from_nlattrs() will take care of filling
1476 * 'match->mask' appropriately.
1477 */
1478 newmask = kmemdup(nla_key,
1479 nla_total_size(nla_len(nla_key)),
1480 GFP_KERNEL);
1481 if (!newmask)
1482 return -ENOMEM;
1483
1484 mask_set_nlattr(newmask, 0xff);
1485
1486 /* The userspace does not send tunnel attributes that
1487 * are 0, but we should not wildcard them nonetheless.
1488 */
1489 if (match->key->tun_proto)
1490 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1491 0xff, true);
1492
1493 nla_mask = newmask;
1494 }
1495
1496 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
1497 if (err)
1498 goto free_newmask;
1499
1500 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1501 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
1502
1503 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1504 if (err)
1505 goto free_newmask;
1506
1507 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1508 log);
1509 if (err)
1510 goto free_newmask;
1511 }
1512
1513 if (!match_validate(match, key_attrs, mask_attrs, log))
1514 err = -EINVAL;
1515
1516 free_newmask:
1517 kfree(newmask);
1518 return err;
1519 }
1520
1521 static size_t get_ufid_len(const struct nlattr *attr, bool log)
1522 {
1523 size_t len;
1524
1525 if (!attr)
1526 return 0;
1527
1528 len = nla_len(attr);
1529 if (len < 1 || len > MAX_UFID_LENGTH) {
1530 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1531 nla_len(attr), MAX_UFID_LENGTH);
1532 return 0;
1533 }
1534
1535 return len;
1536 }
1537
1538 /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1539 * or false otherwise.
1540 */
1541 bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1542 bool log)
1543 {
1544 sfid->ufid_len = get_ufid_len(attr, log);
1545 if (sfid->ufid_len)
1546 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1547
1548 return sfid->ufid_len;
1549 }
1550
1551 int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1552 const struct sw_flow_key *key, bool log)
1553 {
1554 struct sw_flow_key *new_key;
1555
1556 if (ovs_nla_get_ufid(sfid, ufid, log))
1557 return 0;
1558
1559 /* If UFID was not provided, use unmasked key. */
1560 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1561 if (!new_key)
1562 return -ENOMEM;
1563 memcpy(new_key, key, sizeof(*key));
1564 sfid->unmasked_key = new_key;
1565
1566 return 0;
1567 }
1568
1569 u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1570 {
1571 return attr ? nla_get_u32(attr) : 0;
1572 }
1573
1574 /**
1575 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1576 * @net: Network namespace.
1577 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1578 * metadata.
1579 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1580 * attributes.
1581 * @attrs: Bit mask for the netlink attributes included in @a.
1582 * @log: Boolean to allow kernel error logging. Normally true, but when
1583 * probing for feature compatibility this should be passed in as false to
1584 * suppress unnecessary error logging.
1585 *
1586 * This parses a series of Netlink attributes that form a flow key, which must
1587 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1588 * get the metadata, that is, the parts of the flow key that cannot be
1589 * extracted from the packet itself.
1590 *
1591 * This must be called before the packet key fields are filled in 'key'.
1592 */
1593
1594 int ovs_nla_get_flow_metadata(struct net *net,
1595 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1596 u64 attrs, struct sw_flow_key *key, bool log)
1597 {
1598 struct sw_flow_match match;
1599
1600 memset(&match, 0, sizeof(match));
1601 match.key = key;
1602
1603 key->ct_state = 0;
1604 key->ct_zone = 0;
1605 key->ct_orig_proto = 0;
1606 memset(&key->ct, 0, sizeof(key->ct));
1607 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1608 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1609
1610 key->phy.in_port = DP_MAX_PORTS;
1611
1612 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
1613 }
1614
1615 static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1616 bool is_mask)
1617 {
1618 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1619
1620 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1621 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1622 return -EMSGSIZE;
1623 return 0;
1624 }
1625
1626 static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1627 const struct sw_flow_key *output, bool is_mask,
1628 struct sk_buff *skb)
1629 {
1630 struct ovs_key_ethernet *eth_key;
1631 struct nlattr *nla;
1632 struct nlattr *encap = NULL;
1633 struct nlattr *in_encap = NULL;
1634
1635 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1636 goto nla_put_failure;
1637
1638 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1639 goto nla_put_failure;
1640
1641 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1642 goto nla_put_failure;
1643
1644 if ((swkey->tun_proto || is_mask)) {
1645 const void *opts = NULL;
1646
1647 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
1648 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
1649
1650 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
1651 swkey->tun_opts_len, swkey->tun_proto))
1652 goto nla_put_failure;
1653 }
1654
1655 if (swkey->phy.in_port == DP_MAX_PORTS) {
1656 if (is_mask && (output->phy.in_port == 0xffff))
1657 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1658 goto nla_put_failure;
1659 } else {
1660 u16 upper_u16;
1661 upper_u16 = !is_mask ? 0 : 0xffff;
1662
1663 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1664 (upper_u16 << 16) | output->phy.in_port))
1665 goto nla_put_failure;
1666 }
1667
1668 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1669 goto nla_put_failure;
1670
1671 if (ovs_ct_put_key(swkey, output, skb))
1672 goto nla_put_failure;
1673
1674 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
1675 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1676 if (!nla)
1677 goto nla_put_failure;
1678
1679 eth_key = nla_data(nla);
1680 ether_addr_copy(eth_key->eth_src, output->eth.src);
1681 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1682
1683 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
1684 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
1685 goto nla_put_failure;
1686 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1687 if (!swkey->eth.vlan.tci)
1688 goto unencap;
1689
1690 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
1691 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
1692 goto nla_put_failure;
1693 in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1694 if (!swkey->eth.cvlan.tci)
1695 goto unencap;
1696 }
1697 }
1698
1699 if (swkey->eth.type == htons(ETH_P_802_2)) {
1700 /*
1701 * Ethertype 802.2 is represented in the netlink with omitted
1702 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1703 * 0xffff in the mask attribute. Ethertype can also
1704 * be wildcarded.
1705 */
1706 if (is_mask && output->eth.type)
1707 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1708 output->eth.type))
1709 goto nla_put_failure;
1710 goto unencap;
1711 }
1712 }
1713
1714 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1715 goto nla_put_failure;
1716
1717 if (eth_type_vlan(swkey->eth.type)) {
1718 /* There are 3 VLAN tags, we don't know anything about the rest
1719 * of the packet, so truncate here.
1720 */
1721 WARN_ON_ONCE(!(encap && in_encap));
1722 goto unencap;
1723 }
1724
1725 if (swkey->eth.type == htons(ETH_P_IP)) {
1726 struct ovs_key_ipv4 *ipv4_key;
1727
1728 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1729 if (!nla)
1730 goto nla_put_failure;
1731 ipv4_key = nla_data(nla);
1732 ipv4_key->ipv4_src = output->ipv4.addr.src;
1733 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1734 ipv4_key->ipv4_proto = output->ip.proto;
1735 ipv4_key->ipv4_tos = output->ip.tos;
1736 ipv4_key->ipv4_ttl = output->ip.ttl;
1737 ipv4_key->ipv4_frag = output->ip.frag;
1738 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1739 struct ovs_key_ipv6 *ipv6_key;
1740
1741 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1742 if (!nla)
1743 goto nla_put_failure;
1744 ipv6_key = nla_data(nla);
1745 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1746 sizeof(ipv6_key->ipv6_src));
1747 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1748 sizeof(ipv6_key->ipv6_dst));
1749 ipv6_key->ipv6_label = output->ipv6.label;
1750 ipv6_key->ipv6_proto = output->ip.proto;
1751 ipv6_key->ipv6_tclass = output->ip.tos;
1752 ipv6_key->ipv6_hlimit = output->ip.ttl;
1753 ipv6_key->ipv6_frag = output->ip.frag;
1754 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1755 swkey->eth.type == htons(ETH_P_RARP)) {
1756 struct ovs_key_arp *arp_key;
1757
1758 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1759 if (!nla)
1760 goto nla_put_failure;
1761 arp_key = nla_data(nla);
1762 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1763 arp_key->arp_sip = output->ipv4.addr.src;
1764 arp_key->arp_tip = output->ipv4.addr.dst;
1765 arp_key->arp_op = htons(output->ip.proto);
1766 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1767 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
1768 } else if (eth_p_mpls(swkey->eth.type)) {
1769 struct ovs_key_mpls *mpls_key;
1770
1771 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1772 if (!nla)
1773 goto nla_put_failure;
1774 mpls_key = nla_data(nla);
1775 mpls_key->mpls_lse = output->mpls.top_lse;
1776 }
1777
1778 if ((swkey->eth.type == htons(ETH_P_IP) ||
1779 swkey->eth.type == htons(ETH_P_IPV6)) &&
1780 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1781
1782 if (swkey->ip.proto == IPPROTO_TCP) {
1783 struct ovs_key_tcp *tcp_key;
1784
1785 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1786 if (!nla)
1787 goto nla_put_failure;
1788 tcp_key = nla_data(nla);
1789 tcp_key->tcp_src = output->tp.src;
1790 tcp_key->tcp_dst = output->tp.dst;
1791 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1792 output->tp.flags))
1793 goto nla_put_failure;
1794 } else if (swkey->ip.proto == IPPROTO_UDP) {
1795 struct ovs_key_udp *udp_key;
1796
1797 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1798 if (!nla)
1799 goto nla_put_failure;
1800 udp_key = nla_data(nla);
1801 udp_key->udp_src = output->tp.src;
1802 udp_key->udp_dst = output->tp.dst;
1803 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1804 struct ovs_key_sctp *sctp_key;
1805
1806 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1807 if (!nla)
1808 goto nla_put_failure;
1809 sctp_key = nla_data(nla);
1810 sctp_key->sctp_src = output->tp.src;
1811 sctp_key->sctp_dst = output->tp.dst;
1812 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1813 swkey->ip.proto == IPPROTO_ICMP) {
1814 struct ovs_key_icmp *icmp_key;
1815
1816 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1817 if (!nla)
1818 goto nla_put_failure;
1819 icmp_key = nla_data(nla);
1820 icmp_key->icmp_type = ntohs(output->tp.src);
1821 icmp_key->icmp_code = ntohs(output->tp.dst);
1822 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1823 swkey->ip.proto == IPPROTO_ICMPV6) {
1824 struct ovs_key_icmpv6 *icmpv6_key;
1825
1826 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1827 sizeof(*icmpv6_key));
1828 if (!nla)
1829 goto nla_put_failure;
1830 icmpv6_key = nla_data(nla);
1831 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1832 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
1833
1834 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1835 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1836 struct ovs_key_nd *nd_key;
1837
1838 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1839 if (!nla)
1840 goto nla_put_failure;
1841 nd_key = nla_data(nla);
1842 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1843 sizeof(nd_key->nd_target));
1844 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1845 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
1846 }
1847 }
1848 }
1849
1850 unencap:
1851 if (in_encap)
1852 nla_nest_end(skb, in_encap);
1853 if (encap)
1854 nla_nest_end(skb, encap);
1855
1856 return 0;
1857
1858 nla_put_failure:
1859 return -EMSGSIZE;
1860 }
1861
1862 int ovs_nla_put_key(const struct sw_flow_key *swkey,
1863 const struct sw_flow_key *output, int attr, bool is_mask,
1864 struct sk_buff *skb)
1865 {
1866 int err;
1867 struct nlattr *nla;
1868
1869 nla = nla_nest_start(skb, attr);
1870 if (!nla)
1871 return -EMSGSIZE;
1872 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1873 if (err)
1874 return err;
1875 nla_nest_end(skb, nla);
1876
1877 return 0;
1878 }
1879
1880 /* Called with ovs_mutex or RCU read lock. */
1881 int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
1882 {
1883 if (ovs_identifier_is_ufid(&flow->id))
1884 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1885 flow->id.ufid);
1886
1887 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1888 OVS_FLOW_ATTR_KEY, false, skb);
1889 }
1890
1891 /* Called with ovs_mutex or RCU read lock. */
1892 int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1893 {
1894 return ovs_nla_put_key(&flow->key, &flow->key,
1895 OVS_FLOW_ATTR_KEY, false, skb);
1896 }
1897
1898 /* Called with ovs_mutex or RCU read lock. */
1899 int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1900 {
1901 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1902 OVS_FLOW_ATTR_MASK, true, skb);
1903 }
1904
1905 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
1906
1907 static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
1908 {
1909 struct sw_flow_actions *sfa;
1910
1911 if (size > MAX_ACTIONS_BUFSIZE) {
1912 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
1913 return ERR_PTR(-EINVAL);
1914 }
1915
1916 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1917 if (!sfa)
1918 return ERR_PTR(-ENOMEM);
1919
1920 sfa->actions_len = 0;
1921 return sfa;
1922 }
1923
1924 static void ovs_nla_free_set_action(const struct nlattr *a)
1925 {
1926 const struct nlattr *ovs_key = nla_data(a);
1927 struct ovs_tunnel_info *ovs_tun;
1928
1929 switch (nla_type(ovs_key)) {
1930 case OVS_KEY_ATTR_TUNNEL_INFO:
1931 ovs_tun = nla_data(ovs_key);
1932 ovs_dst_release((struct dst_entry *)ovs_tun->tun_dst);
1933 break;
1934 }
1935 }
1936
1937 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1938 {
1939 const struct nlattr *a;
1940 int rem;
1941
1942 if (!sf_acts)
1943 return;
1944
1945 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1946 switch (nla_type(a)) {
1947 case OVS_ACTION_ATTR_SET:
1948 ovs_nla_free_set_action(a);
1949 break;
1950 case OVS_ACTION_ATTR_CT:
1951 ovs_ct_free_action(a);
1952 break;
1953 }
1954 }
1955
1956 kfree(sf_acts);
1957 }
1958
1959 static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1960 {
1961 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1962 }
1963
1964 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
1965 * The caller must hold rcu_read_lock for this to be sensible. */
1966 void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
1967 {
1968 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
1969 }
1970
1971 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1972 int attr_len, bool log)
1973 {
1974
1975 struct sw_flow_actions *acts;
1976 int new_acts_size;
1977 int req_size = NLA_ALIGN(attr_len);
1978 int next_offset = offsetof(struct sw_flow_actions, actions) +
1979 (*sfa)->actions_len;
1980
1981 if (req_size <= (ksize(*sfa) - next_offset))
1982 goto out;
1983
1984 new_acts_size = ksize(*sfa) * 2;
1985
1986 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1987 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1988 return ERR_PTR(-EMSGSIZE);
1989 new_acts_size = MAX_ACTIONS_BUFSIZE;
1990 }
1991
1992 acts = nla_alloc_flow_actions(new_acts_size, log);
1993 if (IS_ERR(acts))
1994 return (void *)acts;
1995
1996 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1997 acts->actions_len = (*sfa)->actions_len;
1998 acts->orig_len = (*sfa)->orig_len;
1999 kfree(*sfa);
2000 *sfa = acts;
2001
2002 out:
2003 (*sfa)->actions_len += req_size;
2004 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
2005 }
2006
2007 static struct nlattr *__add_action(struct sw_flow_actions **sfa,
2008 int attrtype, void *data, int len, bool log)
2009 {
2010 struct nlattr *a;
2011
2012 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
2013 if (IS_ERR(a))
2014 return a;
2015
2016 a->nla_type = attrtype;
2017 a->nla_len = nla_attr_size(len);
2018
2019 if (data)
2020 memcpy(nla_data(a), data, len);
2021 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
2022
2023 return a;
2024 }
2025
2026 int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
2027 int len, bool log)
2028 {
2029 struct nlattr *a;
2030
2031 a = __add_action(sfa, attrtype, data, len, log);
2032
2033 return PTR_ERR_OR_ZERO(a);
2034 }
2035
2036 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
2037 int attrtype, bool log)
2038 {
2039 int used = (*sfa)->actions_len;
2040 int err;
2041
2042 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
2043 if (err)
2044 return err;
2045
2046 return used;
2047 }
2048
2049 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2050 int st_offset)
2051 {
2052 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2053 st_offset);
2054
2055 a->nla_len = sfa->actions_len - st_offset;
2056 }
2057
2058 static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
2059 const struct sw_flow_key *key,
2060 struct sw_flow_actions **sfa,
2061 __be16 eth_type, __be16 vlan_tci, bool log);
2062
2063 static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
2064 const struct sw_flow_key *key,
2065 struct sw_flow_actions **sfa,
2066 __be16 eth_type, __be16 vlan_tci,
2067 bool log, bool last)
2068 {
2069 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2070 const struct nlattr *probability, *actions;
2071 const struct nlattr *a;
2072 int rem, start, err;
2073 struct sample_arg arg;
2074
2075 memset(attrs, 0, sizeof(attrs));
2076 nla_for_each_nested(a, attr, rem) {
2077 int type = nla_type(a);
2078 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2079 return -EINVAL;
2080 attrs[type] = a;
2081 }
2082 if (rem)
2083 return -EINVAL;
2084
2085 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2086 if (!probability || nla_len(probability) != sizeof(u32))
2087 return -EINVAL;
2088
2089 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2090 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2091 return -EINVAL;
2092
2093 /* validation done, copy sample action. */
2094 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
2095 if (start < 0)
2096 return start;
2097
2098 /* When both skb and flow may be changed, put the sample
2099 * into a deferred fifo. On the other hand, if only skb
2100 * may be modified, the actions can be executed in place.
2101 *
2102 * Do this analysis at the flow installation time.
2103 * Set 'clone_action->exec' to true if the actions can be
2104 * executed without being deferred.
2105 *
2106 * If the sample is the last action, it can always be excuted
2107 * rather than deferred.
2108 */
2109 arg.exec = last || !actions_may_change_flow(actions);
2110 arg.probability = nla_get_u32(probability);
2111
2112 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
2113 log);
2114 if (err)
2115 return err;
2116
2117 err = __ovs_nla_copy_actions(net, actions, key, sfa,
2118 eth_type, vlan_tci, log);
2119
2120 if (err)
2121 return err;
2122
2123 add_nested_action_end(*sfa, start);
2124
2125 return 0;
2126 }
2127
2128 void ovs_match_init(struct sw_flow_match *match,
2129 struct sw_flow_key *key,
2130 bool reset_key,
2131 struct sw_flow_mask *mask)
2132 {
2133 memset(match, 0, sizeof(*match));
2134 match->key = key;
2135 match->mask = mask;
2136
2137 if (reset_key)
2138 memset(key, 0, sizeof(*key));
2139
2140 if (mask) {
2141 memset(&mask->key, 0, sizeof(mask->key));
2142 mask->range.start = mask->range.end = 0;
2143 }
2144 }
2145
2146 static int validate_geneve_opts(struct sw_flow_key *key)
2147 {
2148 struct geneve_opt *option;
2149 int opts_len = key->tun_opts_len;
2150 bool crit_opt = false;
2151
2152 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2153 while (opts_len > 0) {
2154 int len;
2155
2156 if (opts_len < sizeof(*option))
2157 return -EINVAL;
2158
2159 len = sizeof(*option) + option->length * 4;
2160 if (len > opts_len)
2161 return -EINVAL;
2162
2163 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2164
2165 option = (struct geneve_opt *)((u8 *)option + len);
2166 opts_len -= len;
2167 };
2168
2169 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2170
2171 return 0;
2172 }
2173
2174 static int validate_and_copy_set_tun(const struct nlattr *attr,
2175 struct sw_flow_actions **sfa, bool log)
2176 {
2177 struct sw_flow_match match;
2178 struct sw_flow_key key;
2179 struct metadata_dst *tun_dst;
2180 struct ip_tunnel_info *tun_info;
2181 struct ovs_tunnel_info *ovs_tun;
2182 struct nlattr *a;
2183 int err = 0, start, opts_type;
2184
2185 ovs_match_init(&match, &key, true, NULL);
2186 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
2187 if (opts_type < 0)
2188 return opts_type;
2189
2190 if (key.tun_opts_len) {
2191 switch (opts_type) {
2192 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2193 err = validate_geneve_opts(&key);
2194 if (err < 0)
2195 return err;
2196 break;
2197 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2198 break;
2199 }
2200 };
2201
2202 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
2203 if (start < 0)
2204 return start;
2205
2206 tun_dst = metadata_dst_alloc(key.tun_opts_len, METADATA_IP_TUNNEL,
2207 GFP_KERNEL);
2208
2209 if (!tun_dst)
2210 return -ENOMEM;
2211
2212 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2213 if (err) {
2214 dst_release((struct dst_entry *)tun_dst);
2215 return err;
2216 }
2217 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
2218 sizeof(*ovs_tun), log);
2219 if (IS_ERR(a)) {
2220 ovs_dst_release((struct dst_entry *)tun_dst);
2221 return PTR_ERR(a);
2222 }
2223
2224 ovs_tun = nla_data(a);
2225 ovs_tun->tun_dst = tun_dst;
2226
2227 tun_info = &tun_dst->u.tun_info;
2228 tun_info->mode = IP_TUNNEL_INFO_TX;
2229 if (key.tun_proto == AF_INET6)
2230 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
2231 tun_info->key = key.tun_key;
2232
2233 /* We need to store the options in the action itself since
2234 * everything else will go away after flow setup. We can append
2235 * it to tun_info and then point there.
2236 */
2237 ip_tunnel_info_opts_set(tun_info,
2238 TUN_METADATA_OPTS(&key, key.tun_opts_len),
2239 key.tun_opts_len);
2240 add_nested_action_end(*sfa, start);
2241
2242 return err;
2243 }
2244
2245 /* Return false if there are any non-masked bits set.
2246 * Mask follows data immediately, before any netlink padding.
2247 */
2248 static bool validate_masked(u8 *data, int len)
2249 {
2250 u8 *mask = data + len;
2251
2252 while (len--)
2253 if (*data++ & ~*mask++)
2254 return false;
2255
2256 return true;
2257 }
2258
2259 static int validate_set(const struct nlattr *a,
2260 const struct sw_flow_key *flow_key,
2261 struct sw_flow_actions **sfa, bool *skip_copy,
2262 u8 mac_proto, __be16 eth_type, bool masked, bool log)
2263 {
2264 const struct nlattr *ovs_key = nla_data(a);
2265 int key_type = nla_type(ovs_key);
2266 size_t key_len;
2267
2268 /* There can be only one key in a action */
2269 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2270 return -EINVAL;
2271
2272 key_len = nla_len(ovs_key);
2273 if (masked)
2274 key_len /= 2;
2275
2276 if (key_type > OVS_KEY_ATTR_MAX ||
2277 !check_attr_len(key_len, ovs_key_lens[key_type].len))
2278 return -EINVAL;
2279
2280 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2281 return -EINVAL;
2282
2283 switch (key_type) {
2284 const struct ovs_key_ipv4 *ipv4_key;
2285 const struct ovs_key_ipv6 *ipv6_key;
2286 int err;
2287
2288 case OVS_KEY_ATTR_PRIORITY:
2289 case OVS_KEY_ATTR_SKB_MARK:
2290 case OVS_KEY_ATTR_CT_MARK:
2291 case OVS_KEY_ATTR_CT_LABELS:
2292 break;
2293
2294 case OVS_KEY_ATTR_ETHERNET:
2295 if (mac_proto != MAC_PROTO_ETHERNET)
2296 return -EINVAL;
2297 break;
2298
2299 case OVS_KEY_ATTR_TUNNEL:
2300 #ifndef USE_UPSTREAM_TUNNEL
2301 if (eth_p_mpls(eth_type))
2302 return -EINVAL;
2303 #endif
2304 if (masked)
2305 return -EINVAL; /* Masked tunnel set not supported. */
2306
2307 *skip_copy = true;
2308 err = validate_and_copy_set_tun(a, sfa, log);
2309 if (err)
2310 return err;
2311 break;
2312
2313 case OVS_KEY_ATTR_IPV4:
2314 if (eth_type != htons(ETH_P_IP))
2315 return -EINVAL;
2316
2317 ipv4_key = nla_data(ovs_key);
2318
2319 if (masked) {
2320 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
2321
2322 /* Non-writeable fields. */
2323 if (mask->ipv4_proto || mask->ipv4_frag)
2324 return -EINVAL;
2325 } else {
2326 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2327 return -EINVAL;
2328
2329 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2330 return -EINVAL;
2331 }
2332 break;
2333
2334 case OVS_KEY_ATTR_IPV6:
2335 if (eth_type != htons(ETH_P_IPV6))
2336 return -EINVAL;
2337
2338 ipv6_key = nla_data(ovs_key);
2339
2340 if (masked) {
2341 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
2342
2343 /* Non-writeable fields. */
2344 if (mask->ipv6_proto || mask->ipv6_frag)
2345 return -EINVAL;
2346
2347 /* Invalid bits in the flow label mask? */
2348 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2349 return -EINVAL;
2350 } else {
2351 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2352 return -EINVAL;
2353
2354 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2355 return -EINVAL;
2356 }
2357 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2358 return -EINVAL;
2359
2360 break;
2361
2362 case OVS_KEY_ATTR_TCP:
2363 if ((eth_type != htons(ETH_P_IP) &&
2364 eth_type != htons(ETH_P_IPV6)) ||
2365 flow_key->ip.proto != IPPROTO_TCP)
2366 return -EINVAL;
2367
2368 break;
2369
2370 case OVS_KEY_ATTR_UDP:
2371 if ((eth_type != htons(ETH_P_IP) &&
2372 eth_type != htons(ETH_P_IPV6)) ||
2373 flow_key->ip.proto != IPPROTO_UDP)
2374 return -EINVAL;
2375
2376 break;
2377
2378 case OVS_KEY_ATTR_MPLS:
2379 if (!eth_p_mpls(eth_type))
2380 return -EINVAL;
2381 break;
2382
2383 case OVS_KEY_ATTR_SCTP:
2384 if ((eth_type != htons(ETH_P_IP) &&
2385 eth_type != htons(ETH_P_IPV6)) ||
2386 flow_key->ip.proto != IPPROTO_SCTP)
2387 return -EINVAL;
2388
2389 break;
2390
2391 default:
2392 return -EINVAL;
2393 }
2394
2395 /* Convert non-masked non-tunnel set actions to masked set actions. */
2396 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2397 int start, len = key_len * 2;
2398 struct nlattr *at;
2399
2400 *skip_copy = true;
2401
2402 start = add_nested_action_start(sfa,
2403 OVS_ACTION_ATTR_SET_TO_MASKED,
2404 log);
2405 if (start < 0)
2406 return start;
2407
2408 at = __add_action(sfa, key_type, NULL, len, log);
2409 if (IS_ERR(at))
2410 return PTR_ERR(at);
2411
2412 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2413 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2414 /* Clear non-writeable bits from otherwise writeable fields. */
2415 if (key_type == OVS_KEY_ATTR_IPV6) {
2416 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2417
2418 mask->ipv6_label &= htonl(0x000FFFFF);
2419 }
2420 add_nested_action_end(*sfa, start);
2421 }
2422
2423 return 0;
2424 }
2425
2426 static int validate_userspace(const struct nlattr *attr)
2427 {
2428 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2429 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2430 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
2431 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
2432 };
2433 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2434 int error;
2435
2436 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, attr,
2437 userspace_policy, NULL);
2438 if (error)
2439 return error;
2440
2441 if (!a[OVS_USERSPACE_ATTR_PID] ||
2442 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2443 return -EINVAL;
2444
2445 return 0;
2446 }
2447
2448 static int copy_action(const struct nlattr *from,
2449 struct sw_flow_actions **sfa, bool log)
2450 {
2451 int totlen = NLA_ALIGN(from->nla_len);
2452 struct nlattr *to;
2453
2454 to = reserve_sfa_size(sfa, from->nla_len, log);
2455 if (IS_ERR(to))
2456 return PTR_ERR(to);
2457
2458 memcpy(to, from, totlen);
2459 return 0;
2460 }
2461
2462 static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
2463 const struct sw_flow_key *key,
2464 struct sw_flow_actions **sfa,
2465 __be16 eth_type, __be16 vlan_tci, bool log)
2466 {
2467 u8 mac_proto = ovs_key_mac_proto(key);
2468 const struct nlattr *a;
2469 int rem, err;
2470
2471 nla_for_each_nested(a, attr, rem) {
2472 /* Expected argument lengths, (u32)-1 for variable length. */
2473 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2474 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
2475 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
2476 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
2477 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2478 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
2479 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2480 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2481 [OVS_ACTION_ATTR_SET] = (u32)-1,
2482 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
2483 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
2484 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2485 [OVS_ACTION_ATTR_CT] = (u32)-1,
2486 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
2487 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
2488 [OVS_ACTION_ATTR_POP_ETH] = 0,
2489 };
2490 const struct ovs_action_push_vlan *vlan;
2491 int type = nla_type(a);
2492 bool skip_copy;
2493
2494 if (type > OVS_ACTION_ATTR_MAX ||
2495 (action_lens[type] != nla_len(a) &&
2496 action_lens[type] != (u32)-1))
2497 return -EINVAL;
2498
2499 skip_copy = false;
2500 switch (type) {
2501 case OVS_ACTION_ATTR_UNSPEC:
2502 return -EINVAL;
2503
2504 case OVS_ACTION_ATTR_USERSPACE:
2505 err = validate_userspace(a);
2506 if (err)
2507 return err;
2508 break;
2509
2510 case OVS_ACTION_ATTR_OUTPUT:
2511 if (nla_get_u32(a) >= DP_MAX_PORTS)
2512 return -EINVAL;
2513 break;
2514
2515 case OVS_ACTION_ATTR_TRUNC: {
2516 const struct ovs_action_trunc *trunc = nla_data(a);
2517
2518 if (trunc->max_len < ETH_HLEN)
2519 return -EINVAL;
2520 break;
2521 }
2522
2523 case OVS_ACTION_ATTR_HASH: {
2524 const struct ovs_action_hash *act_hash = nla_data(a);
2525
2526 switch (act_hash->hash_alg) {
2527 case OVS_HASH_ALG_L4:
2528 break;
2529 default:
2530 return -EINVAL;
2531 }
2532
2533 break;
2534 }
2535
2536 case OVS_ACTION_ATTR_POP_VLAN:
2537 if (mac_proto != MAC_PROTO_ETHERNET)
2538 return -EINVAL;
2539 vlan_tci = htons(0);
2540 break;
2541
2542 case OVS_ACTION_ATTR_PUSH_VLAN:
2543 if (mac_proto != MAC_PROTO_ETHERNET)
2544 return -EINVAL;
2545 vlan = nla_data(a);
2546 if (!eth_type_vlan(vlan->vlan_tpid))
2547 return -EINVAL;
2548 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2549 return -EINVAL;
2550 vlan_tci = vlan->vlan_tci;
2551 break;
2552
2553 case OVS_ACTION_ATTR_RECIRC:
2554 break;
2555
2556 case OVS_ACTION_ATTR_PUSH_MPLS: {
2557 const struct ovs_action_push_mpls *mpls = nla_data(a);
2558
2559 if (!eth_p_mpls(mpls->mpls_ethertype))
2560 return -EINVAL;
2561 /* Prohibit push MPLS other than to a white list
2562 * for packets that have a known tag order.
2563 */
2564 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2565 (eth_type != htons(ETH_P_IP) &&
2566 eth_type != htons(ETH_P_IPV6) &&
2567 eth_type != htons(ETH_P_ARP) &&
2568 eth_type != htons(ETH_P_RARP) &&
2569 !eth_p_mpls(eth_type)))
2570 return -EINVAL;
2571 eth_type = mpls->mpls_ethertype;
2572 break;
2573 }
2574
2575 case OVS_ACTION_ATTR_POP_MPLS:
2576 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2577 !eth_p_mpls(eth_type))
2578 return -EINVAL;
2579
2580 /* Disallow subsequent L2.5+ set and mpls_pop actions
2581 * as there is no check here to ensure that the new
2582 * eth_type is valid and thus set actions could
2583 * write off the end of the packet or otherwise
2584 * corrupt it.
2585 *
2586 * Support for these actions is planned using packet
2587 * recirculation.
2588 */
2589 eth_type = htons(0);
2590 break;
2591
2592 case OVS_ACTION_ATTR_SET:
2593 err = validate_set(a, key, sfa,
2594 &skip_copy, mac_proto, eth_type,
2595 false, log);
2596 if (err)
2597 return err;
2598 break;
2599
2600 case OVS_ACTION_ATTR_SET_MASKED:
2601 err = validate_set(a, key, sfa,
2602 &skip_copy, mac_proto, eth_type,
2603 true, log);
2604 if (err)
2605 return err;
2606 break;
2607
2608 case OVS_ACTION_ATTR_SAMPLE: {
2609 bool last = nla_is_last(a, rem);
2610
2611 err = validate_and_copy_sample(net, a, key, sfa,
2612 eth_type, vlan_tci,
2613 log, last);
2614 if (err)
2615 return err;
2616 skip_copy = true;
2617 break;
2618 }
2619
2620 case OVS_ACTION_ATTR_CT:
2621 err = ovs_ct_copy_action(net, a, key, sfa, log);
2622 if (err)
2623 return err;
2624 skip_copy = true;
2625 break;
2626
2627 case OVS_ACTION_ATTR_PUSH_ETH:
2628 /* Disallow pushing an Ethernet header if one
2629 * is already present */
2630 if (mac_proto != MAC_PROTO_NONE)
2631 return -EINVAL;
2632 mac_proto = MAC_PROTO_NONE;
2633 break;
2634
2635 case OVS_ACTION_ATTR_POP_ETH:
2636 if (mac_proto != MAC_PROTO_ETHERNET)
2637 return -EINVAL;
2638 if (vlan_tci & htons(VLAN_TAG_PRESENT))
2639 return -EINVAL;
2640 mac_proto = MAC_PROTO_ETHERNET;
2641 break;
2642
2643 default:
2644 OVS_NLERR(log, "Unknown Action type %d", type);
2645 return -EINVAL;
2646 }
2647 if (!skip_copy) {
2648 err = copy_action(a, sfa, log);
2649 if (err)
2650 return err;
2651 }
2652 }
2653
2654 if (rem > 0)
2655 return -EINVAL;
2656
2657 return 0;
2658 }
2659
2660 /* 'key' must be the masked key. */
2661 int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
2662 const struct sw_flow_key *key,
2663 struct sw_flow_actions **sfa, bool log)
2664 {
2665 int err;
2666
2667 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
2668 if (IS_ERR(*sfa))
2669 return PTR_ERR(*sfa);
2670
2671 (*sfa)->orig_len = nla_len(attr);
2672 err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
2673 key->eth.vlan.tci, log);
2674 if (err)
2675 ovs_nla_free_flow_actions(*sfa);
2676
2677 return err;
2678 }
2679
2680 static int sample_action_to_attr(const struct nlattr *attr,
2681 struct sk_buff *skb)
2682 {
2683 struct nlattr *start, *ac_start = NULL, *sample_arg;
2684 int err = 0, rem = nla_len(attr);
2685 const struct sample_arg *arg;
2686 struct nlattr *actions;
2687
2688 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2689 if (!start)
2690 return -EMSGSIZE;
2691
2692 sample_arg = nla_data(attr);
2693 arg = nla_data(sample_arg);
2694 actions = nla_next(sample_arg, &rem);
2695
2696 if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
2697 err = -EMSGSIZE;
2698 goto out;
2699 }
2700
2701 ac_start = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2702 if (!ac_start) {
2703 err = -EMSGSIZE;
2704 goto out;
2705 }
2706
2707 err = ovs_nla_put_actions(actions, rem, skb);
2708
2709 out:
2710 if (err) {
2711 nla_nest_cancel(skb, ac_start);
2712 nla_nest_cancel(skb, start);
2713 } else {
2714 nla_nest_end(skb, ac_start);
2715 nla_nest_end(skb, start);
2716 }
2717
2718 return err;
2719 }
2720
2721 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2722 {
2723 const struct nlattr *ovs_key = nla_data(a);
2724 int key_type = nla_type(ovs_key);
2725 struct nlattr *start;
2726 int err;
2727
2728 switch (key_type) {
2729 case OVS_KEY_ATTR_TUNNEL_INFO: {
2730 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2731 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
2732
2733 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2734 if (!start)
2735 return -EMSGSIZE;
2736
2737 err = ip_tun_to_nlattr(skb, &tun_info->key,
2738 ip_tunnel_info_opts(tun_info),
2739 tun_info->options_len,
2740 ip_tunnel_info_af(tun_info));
2741 if (err)
2742 return err;
2743 nla_nest_end(skb, start);
2744 break;
2745 }
2746 default:
2747 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2748 return -EMSGSIZE;
2749 break;
2750 }
2751
2752 return 0;
2753 }
2754
2755 static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2756 struct sk_buff *skb)
2757 {
2758 const struct nlattr *ovs_key = nla_data(a);
2759 struct nlattr *nla;
2760 size_t key_len = nla_len(ovs_key) / 2;
2761
2762 /* Revert the conversion we did from a non-masked set action to
2763 * masked set action.
2764 */
2765 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2766 if (!nla)
2767 return -EMSGSIZE;
2768
2769 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2770 return -EMSGSIZE;
2771
2772 nla_nest_end(skb, nla);
2773 return 0;
2774 }
2775
2776 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2777 {
2778 const struct nlattr *a;
2779 int rem, err;
2780
2781 nla_for_each_attr(a, attr, len, rem) {
2782 int type = nla_type(a);
2783
2784 switch (type) {
2785 case OVS_ACTION_ATTR_SET:
2786 err = set_action_to_attr(a, skb);
2787 if (err)
2788 return err;
2789 break;
2790
2791 case OVS_ACTION_ATTR_SET_TO_MASKED:
2792 err = masked_set_action_to_set_action_attr(a, skb);
2793 if (err)
2794 return err;
2795 break;
2796
2797 case OVS_ACTION_ATTR_SAMPLE:
2798 err = sample_action_to_attr(a, skb);
2799 if (err)
2800 return err;
2801 break;
2802
2803 case OVS_ACTION_ATTR_CT:
2804 err = ovs_ct_action_to_attr(nla_data(a), skb);
2805 if (err)
2806 return err;
2807 break;
2808
2809 default:
2810 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2811 return -EMSGSIZE;
2812 break;
2813 }
2814 }
2815
2816 return 0;
2817 }