1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2007-2017 Nicira, Inc.
4 */
5
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8 #include "flow.h"
9 #include "datapath.h"
10 #include <linux/uaccess.h>
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/if_ether.h>
14 #include <linux/if_vlan.h>
15 #include <net/llc_pdu.h>
16 #include <linux/kernel.h>
17 #include <linux/jhash.h>
18 #include <linux/jiffies.h>
19 #include <linux/llc.h>
20 #include <linux/module.h>
21 #include <linux/in.h>
22 #include <linux/rcupdate.h>
23 #include <linux/if_arp.h>
24 #include <linux/ip.h>
25 #include <linux/ipv6.h>
26 #include <linux/sctp.h>
27 #include <linux/tcp.h>
28 #include <linux/udp.h>
29 #include <linux/icmp.h>
30 #include <linux/icmpv6.h>
31 #include <linux/rculist.h>
32 #include <net/geneve.h>
33 #include <net/ip.h>
34 #include <net/ipv6.h>
35 #include <net/ndisc.h>
36 #include <net/mpls.h>
37 #include <net/vxlan.h>
38 #include <net/tun_proto.h>
39 #include <net/erspan.h>
40
41 #include "flow_netlink.h"
42
43 struct ovs_len_tbl {
44 int len;
45 const struct ovs_len_tbl *next;
46 };
47
48 #define OVS_ATTR_NESTED -1
49 #define OVS_ATTR_VARIABLE -2
50 #define OVS_COPY_ACTIONS_MAX_DEPTH 16
51
actions_may_change_flow(const struct nlattr * actions)52 static bool actions_may_change_flow(const struct nlattr *actions)
53 {
54 struct nlattr *nla;
55 int rem;
56
57 nla_for_each_nested(nla, actions, rem) {
58 u16 action = nla_type(nla);
59
60 switch (action) {
61 case OVS_ACTION_ATTR_OUTPUT:
62 case OVS_ACTION_ATTR_RECIRC:
63 case OVS_ACTION_ATTR_TRUNC:
64 case OVS_ACTION_ATTR_USERSPACE:
65 break;
66
67 case OVS_ACTION_ATTR_CT:
68 case OVS_ACTION_ATTR_CT_CLEAR:
69 case OVS_ACTION_ATTR_HASH:
70 case OVS_ACTION_ATTR_POP_ETH:
71 case OVS_ACTION_ATTR_POP_MPLS:
72 case OVS_ACTION_ATTR_POP_NSH:
73 case OVS_ACTION_ATTR_POP_VLAN:
74 case OVS_ACTION_ATTR_PUSH_ETH:
75 case OVS_ACTION_ATTR_PUSH_MPLS:
76 case OVS_ACTION_ATTR_PUSH_NSH:
77 case OVS_ACTION_ATTR_PUSH_VLAN:
78 case OVS_ACTION_ATTR_SAMPLE:
79 case OVS_ACTION_ATTR_SET:
80 case OVS_ACTION_ATTR_SET_MASKED:
81 case OVS_ACTION_ATTR_METER:
82 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
83 case OVS_ACTION_ATTR_ADD_MPLS:
84 case OVS_ACTION_ATTR_DEC_TTL:
85 default:
86 return true;
87 }
88 }
89 return false;
90 }
91
update_range(struct sw_flow_match * match,size_t offset,size_t size,bool is_mask)92 static void update_range(struct sw_flow_match *match,
93 size_t offset, size_t size, bool is_mask)
94 {
95 struct sw_flow_key_range *range;
96 size_t start = rounddown(offset, sizeof(long));
97 size_t end = roundup(offset + size, sizeof(long));
98
99 if (!is_mask)
100 range = &match->range;
101 else
102 range = &match->mask->range;
103
104 if (range->start == range->end) {
105 range->start = start;
106 range->end = end;
107 return;
108 }
109
110 if (range->start > start)
111 range->start = start;
112
113 if (range->end < end)
114 range->end = end;
115 }
116
117 #define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
118 do { \
119 update_range(match, offsetof(struct sw_flow_key, field), \
120 sizeof((match)->key->field), is_mask); \
121 if (is_mask) \
122 (match)->mask->key.field = value; \
123 else \
124 (match)->key->field = value; \
125 } while (0)
126
127 #define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
128 do { \
129 update_range(match, offset, len, is_mask); \
130 if (is_mask) \
131 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
132 len); \
133 else \
134 memcpy((u8 *)(match)->key + offset, value_p, len); \
135 } while (0)
136
137 #define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
138 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
139 value_p, len, is_mask)
140
141 #define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
142 do { \
143 update_range(match, offsetof(struct sw_flow_key, field), \
144 sizeof((match)->key->field), is_mask); \
145 if (is_mask) \
146 memset((u8 *)&(match)->mask->key.field, value, \
147 sizeof((match)->mask->key.field)); \
148 else \
149 memset((u8 *)&(match)->key->field, value, \
150 sizeof((match)->key->field)); \
151 } while (0)
152
match_validate(const struct sw_flow_match * match,u64 key_attrs,u64 mask_attrs,bool log)153 static bool match_validate(const struct sw_flow_match *match,
154 u64 key_attrs, u64 mask_attrs, bool log)
155 {
156 u64 key_expected = 0;
157 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
158
159 /* The following mask attributes allowed only if they
160 * pass the validation tests. */
161 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
162 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
163 | (1 << OVS_KEY_ATTR_IPV6)
164 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
165 | (1 << OVS_KEY_ATTR_TCP)
166 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
167 | (1 << OVS_KEY_ATTR_UDP)
168 | (1 << OVS_KEY_ATTR_SCTP)
169 | (1 << OVS_KEY_ATTR_ICMP)
170 | (1 << OVS_KEY_ATTR_ICMPV6)
171 | (1 << OVS_KEY_ATTR_ARP)
172 | (1 << OVS_KEY_ATTR_ND)
173 | (1 << OVS_KEY_ATTR_MPLS)
174 | (1 << OVS_KEY_ATTR_NSH));
175
176 /* Always allowed mask fields. */
177 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
178 | (1 << OVS_KEY_ATTR_IN_PORT)
179 | (1 << OVS_KEY_ATTR_ETHERTYPE));
180
181 /* Check key attributes. */
182 if (match->key->eth.type == htons(ETH_P_ARP)
183 || match->key->eth.type == htons(ETH_P_RARP)) {
184 key_expected |= 1 << OVS_KEY_ATTR_ARP;
185 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
186 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
187 }
188
189 if (eth_p_mpls(match->key->eth.type)) {
190 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
191 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
192 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
193 }
194
195 if (match->key->eth.type == htons(ETH_P_IP)) {
196 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
197 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
198 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
199 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
200 }
201
202 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
203 if (match->key->ip.proto == IPPROTO_UDP) {
204 key_expected |= 1 << OVS_KEY_ATTR_UDP;
205 if (match->mask && (match->mask->key.ip.proto == 0xff))
206 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
207 }
208
209 if (match->key->ip.proto == IPPROTO_SCTP) {
210 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
211 if (match->mask && (match->mask->key.ip.proto == 0xff))
212 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
213 }
214
215 if (match->key->ip.proto == IPPROTO_TCP) {
216 key_expected |= 1 << OVS_KEY_ATTR_TCP;
217 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
218 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
219 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
220 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
221 }
222 }
223
224 if (match->key->ip.proto == IPPROTO_ICMP) {
225 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
226 if (match->mask && (match->mask->key.ip.proto == 0xff))
227 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
228 }
229 }
230 }
231
232 if (match->key->eth.type == htons(ETH_P_IPV6)) {
233 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
234 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
235 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
236 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
237 }
238
239 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
240 if (match->key->ip.proto == IPPROTO_UDP) {
241 key_expected |= 1 << OVS_KEY_ATTR_UDP;
242 if (match->mask && (match->mask->key.ip.proto == 0xff))
243 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
244 }
245
246 if (match->key->ip.proto == IPPROTO_SCTP) {
247 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
248 if (match->mask && (match->mask->key.ip.proto == 0xff))
249 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
250 }
251
252 if (match->key->ip.proto == IPPROTO_TCP) {
253 key_expected |= 1 << OVS_KEY_ATTR_TCP;
254 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
255 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
256 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
257 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
258 }
259 }
260
261 if (match->key->ip.proto == IPPROTO_ICMPV6) {
262 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
263 if (match->mask && (match->mask->key.ip.proto == 0xff))
264 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
265
266 if (match->key->tp.src ==
267 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
268 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
269 key_expected |= 1 << OVS_KEY_ATTR_ND;
270 /* Original direction conntrack tuple
271 * uses the same space as the ND fields
272 * in the key, so both are not allowed
273 * at the same time.
274 */
275 mask_allowed &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
276 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
277 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
278 }
279 }
280 }
281 }
282
283 if (match->key->eth.type == htons(ETH_P_NSH)) {
284 key_expected |= 1 << OVS_KEY_ATTR_NSH;
285 if (match->mask &&
286 match->mask->key.eth.type == htons(0xffff)) {
287 mask_allowed |= 1 << OVS_KEY_ATTR_NSH;
288 }
289 }
290
291 if ((key_attrs & key_expected) != key_expected) {
292 /* Key attributes check failed. */
293 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
294 (unsigned long long)key_attrs,
295 (unsigned long long)key_expected);
296 return false;
297 }
298
299 if ((mask_attrs & mask_allowed) != mask_attrs) {
300 /* Mask attributes check failed. */
301 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
302 (unsigned long long)mask_attrs,
303 (unsigned long long)mask_allowed);
304 return false;
305 }
306
307 return true;
308 }
309
ovs_tun_key_attr_size(void)310 size_t ovs_tun_key_attr_size(void)
311 {
312 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
313 * updating this function.
314 */
315 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
316 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
317 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
318 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
319 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
320 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
321 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
322 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
323 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
324 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS and
325 * OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS is mutually exclusive with
326 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
327 */
328 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
329 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
330 }
331
ovs_nsh_key_attr_size(void)332 static size_t ovs_nsh_key_attr_size(void)
333 {
334 /* Whenever adding new OVS_NSH_KEY_ FIELDS, we should consider
335 * updating this function.
336 */
337 return nla_total_size(NSH_BASE_HDR_LEN) /* OVS_NSH_KEY_ATTR_BASE */
338 /* OVS_NSH_KEY_ATTR_MD1 and OVS_NSH_KEY_ATTR_MD2 are
339 * mutually exclusive, so the bigger one can cover
340 * the small one.
341 */
342 + nla_total_size(NSH_CTX_HDRS_MAX_LEN);
343 }
344
ovs_key_attr_size(void)345 size_t ovs_key_attr_size(void)
346 {
347 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
348 * updating this function.
349 */
350 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 29);
351
352 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
353 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
354 + ovs_tun_key_attr_size()
355 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
356 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
357 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
358 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
359 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
360 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
361 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
362 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
363 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
364 + nla_total_size(0) /* OVS_KEY_ATTR_NSH */
365 + ovs_nsh_key_attr_size()
366 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
367 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
368 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
369 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
370 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
371 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
372 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
373 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
374 }
375
376 static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
377 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
378 };
379
380 static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
381 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
382 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
383 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
384 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
385 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
386 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
387 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
388 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
389 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
390 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
391 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
392 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
393 .next = ovs_vxlan_ext_key_lens },
394 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
395 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
396 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = OVS_ATTR_VARIABLE },
397 [OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE] = { .len = 0 },
398 };
399
400 static const struct ovs_len_tbl
401 ovs_nsh_key_attr_lens[OVS_NSH_KEY_ATTR_MAX + 1] = {
402 [OVS_NSH_KEY_ATTR_BASE] = { .len = sizeof(struct ovs_nsh_key_base) },
403 [OVS_NSH_KEY_ATTR_MD1] = { .len = sizeof(struct ovs_nsh_key_md1) },
404 [OVS_NSH_KEY_ATTR_MD2] = { .len = OVS_ATTR_VARIABLE },
405 };
406
407 /* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
408 static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
409 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
410 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
411 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
412 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
413 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
414 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
415 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
416 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
417 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
418 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
419 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
420 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
421 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
422 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
423 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
424 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
425 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
426 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
427 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
428 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
429 .next = ovs_tunnel_key_lens, },
430 [OVS_KEY_ATTR_MPLS] = { .len = OVS_ATTR_VARIABLE },
431 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
432 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
433 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
434 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
435 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
436 .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
437 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
438 .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
439 [OVS_KEY_ATTR_NSH] = { .len = OVS_ATTR_NESTED,
440 .next = ovs_nsh_key_attr_lens, },
441 };
442
check_attr_len(unsigned int attr_len,unsigned int expected_len)443 static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
444 {
445 return expected_len == attr_len ||
446 expected_len == OVS_ATTR_NESTED ||
447 expected_len == OVS_ATTR_VARIABLE;
448 }
449
is_all_zero(const u8 * fp,size_t size)450 static bool is_all_zero(const u8 *fp, size_t size)
451 {
452 int i;
453
454 if (!fp)
455 return false;
456
457 for (i = 0; i < size; i++)
458 if (fp[i])
459 return false;
460
461 return true;
462 }
463
__parse_flow_nlattrs(const struct nlattr * attr,const struct nlattr * a[],u64 * attrsp,bool log,bool nz)464 static int __parse_flow_nlattrs(const struct nlattr *attr,
465 const struct nlattr *a[],
466 u64 *attrsp, bool log, bool nz)
467 {
468 const struct nlattr *nla;
469 u64 attrs;
470 int rem;
471
472 attrs = *attrsp;
473 nla_for_each_nested(nla, attr, rem) {
474 u16 type = nla_type(nla);
475 int expected_len;
476
477 if (type > OVS_KEY_ATTR_MAX) {
478 OVS_NLERR(log, "Key type %d is out of range max %d",
479 type, OVS_KEY_ATTR_MAX);
480 return -EINVAL;
481 }
482
483 if (attrs & (1 << type)) {
484 OVS_NLERR(log, "Duplicate key (type %d).", type);
485 return -EINVAL;
486 }
487
488 expected_len = ovs_key_lens[type].len;
489 if (!check_attr_len(nla_len(nla), expected_len)) {
490 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
491 type, nla_len(nla), expected_len);
492 return -EINVAL;
493 }
494
495 if (!nz || !is_all_zero(nla_data(nla), nla_len(nla))) {
496 attrs |= 1 << type;
497 a[type] = nla;
498 }
499 }
500 if (rem) {
501 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
502 return -EINVAL;
503 }
504
505 *attrsp = attrs;
506 return 0;
507 }
508
parse_flow_mask_nlattrs(const struct nlattr * attr,const struct nlattr * a[],u64 * attrsp,bool log)509 static int parse_flow_mask_nlattrs(const struct nlattr *attr,
510 const struct nlattr *a[], u64 *attrsp,
511 bool log)
512 {
513 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
514 }
515
parse_flow_nlattrs(const struct nlattr * attr,const struct nlattr * a[],u64 * attrsp,bool log)516 int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
517 u64 *attrsp, bool log)
518 {
519 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
520 }
521
genev_tun_opt_from_nlattr(const struct nlattr * a,struct sw_flow_match * match,bool is_mask,bool log)522 static int genev_tun_opt_from_nlattr(const struct nlattr *a,
523 struct sw_flow_match *match, bool is_mask,
524 bool log)
525 {
526 unsigned long opt_key_offset;
527
528 if (nla_len(a) > sizeof(match->key->tun_opts)) {
529 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
530 nla_len(a), sizeof(match->key->tun_opts));
531 return -EINVAL;
532 }
533
534 if (nla_len(a) % 4 != 0) {
535 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
536 nla_len(a));
537 return -EINVAL;
538 }
539
540 /* We need to record the length of the options passed
541 * down, otherwise packets with the same format but
542 * additional options will be silently matched.
543 */
544 if (!is_mask) {
545 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
546 false);
547 } else {
548 /* This is somewhat unusual because it looks at
549 * both the key and mask while parsing the
550 * attributes (and by extension assumes the key
551 * is parsed first). Normally, we would verify
552 * that each is the correct length and that the
553 * attributes line up in the validate function.
554 * However, that is difficult because this is
555 * variable length and we won't have the
556 * information later.
557 */
558 if (match->key->tun_opts_len != nla_len(a)) {
559 OVS_NLERR(log, "Geneve option len %d != mask len %d",
560 match->key->tun_opts_len, nla_len(a));
561 return -EINVAL;
562 }
563
564 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
565 }
566
567 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
568 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
569 nla_len(a), is_mask);
570 return 0;
571 }
572
vxlan_tun_opt_from_nlattr(const struct nlattr * attr,struct sw_flow_match * match,bool is_mask,bool log)573 static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
574 struct sw_flow_match *match, bool is_mask,
575 bool log)
576 {
577 struct nlattr *a;
578 int rem;
579 unsigned long opt_key_offset;
580 struct vxlan_metadata opts;
581
582 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
583
584 memset(&opts, 0, sizeof(opts));
585 nla_for_each_nested(a, attr, rem) {
586 int type = nla_type(a);
587
588 if (type > OVS_VXLAN_EXT_MAX) {
589 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
590 type, OVS_VXLAN_EXT_MAX);
591 return -EINVAL;
592 }
593
594 if (!check_attr_len(nla_len(a),
595 ovs_vxlan_ext_key_lens[type].len)) {
596 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
597 type, nla_len(a),
598 ovs_vxlan_ext_key_lens[type].len);
599 return -EINVAL;
600 }
601
602 switch (type) {
603 case OVS_VXLAN_EXT_GBP:
604 opts.gbp = nla_get_u32(a);
605 break;
606 default:
607 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
608 type);
609 return -EINVAL;
610 }
611 }
612 if (rem) {
613 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
614 rem);
615 return -EINVAL;
616 }
617
618 if (!is_mask)
619 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
620 else
621 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
622
623 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
624 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
625 is_mask);
626 return 0;
627 }
628
erspan_tun_opt_from_nlattr(const struct nlattr * a,struct sw_flow_match * match,bool is_mask,bool log)629 static int erspan_tun_opt_from_nlattr(const struct nlattr *a,
630 struct sw_flow_match *match, bool is_mask,
631 bool log)
632 {
633 unsigned long opt_key_offset;
634
635 BUILD_BUG_ON(sizeof(struct erspan_metadata) >
636 sizeof(match->key->tun_opts));
637
638 if (nla_len(a) > sizeof(match->key->tun_opts)) {
639 OVS_NLERR(log, "ERSPAN option length err (len %d, max %zu).",
640 nla_len(a), sizeof(match->key->tun_opts));
641 return -EINVAL;
642 }
643
644 if (!is_mask)
645 SW_FLOW_KEY_PUT(match, tun_opts_len,
646 sizeof(struct erspan_metadata), false);
647 else
648 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
649
650 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
651 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
652 nla_len(a), is_mask);
653 return 0;
654 }
655
ip_tun_from_nlattr(const struct nlattr * attr,struct sw_flow_match * match,bool is_mask,bool log)656 static int ip_tun_from_nlattr(const struct nlattr *attr,
657 struct sw_flow_match *match, bool is_mask,
658 bool log)
659 {
660 bool ttl = false, ipv4 = false, ipv6 = false;
661 bool info_bridge_mode = false;
662 __be16 tun_flags = 0;
663 int opts_type = 0;
664 struct nlattr *a;
665 int rem;
666
667 nla_for_each_nested(a, attr, rem) {
668 int type = nla_type(a);
669 int err;
670
671 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
672 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
673 type, OVS_TUNNEL_KEY_ATTR_MAX);
674 return -EINVAL;
675 }
676
677 if (!check_attr_len(nla_len(a),
678 ovs_tunnel_key_lens[type].len)) {
679 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
680 type, nla_len(a), ovs_tunnel_key_lens[type].len);
681 return -EINVAL;
682 }
683
684 switch (type) {
685 case OVS_TUNNEL_KEY_ATTR_ID:
686 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
687 nla_get_be64(a), is_mask);
688 tun_flags |= TUNNEL_KEY;
689 break;
690 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
691 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
692 nla_get_in_addr(a), is_mask);
693 ipv4 = true;
694 break;
695 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
696 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
697 nla_get_in_addr(a), is_mask);
698 ipv4 = true;
699 break;
700 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
701 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
702 nla_get_in6_addr(a), is_mask);
703 ipv6 = true;
704 break;
705 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
706 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
707 nla_get_in6_addr(a), is_mask);
708 ipv6 = true;
709 break;
710 case OVS_TUNNEL_KEY_ATTR_TOS:
711 SW_FLOW_KEY_PUT(match, tun_key.tos,
712 nla_get_u8(a), is_mask);
713 break;
714 case OVS_TUNNEL_KEY_ATTR_TTL:
715 SW_FLOW_KEY_PUT(match, tun_key.ttl,
716 nla_get_u8(a), is_mask);
717 ttl = true;
718 break;
719 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
720 tun_flags |= TUNNEL_DONT_FRAGMENT;
721 break;
722 case OVS_TUNNEL_KEY_ATTR_CSUM:
723 tun_flags |= TUNNEL_CSUM;
724 break;
725 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
726 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
727 nla_get_be16(a), is_mask);
728 break;
729 case OVS_TUNNEL_KEY_ATTR_TP_DST:
730 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
731 nla_get_be16(a), is_mask);
732 break;
733 case OVS_TUNNEL_KEY_ATTR_OAM:
734 tun_flags |= TUNNEL_OAM;
735 break;
736 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
737 if (opts_type) {
738 OVS_NLERR(log, "Multiple metadata blocks provided");
739 return -EINVAL;
740 }
741
742 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
743 if (err)
744 return err;
745
746 tun_flags |= TUNNEL_GENEVE_OPT;
747 opts_type = type;
748 break;
749 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
750 if (opts_type) {
751 OVS_NLERR(log, "Multiple metadata blocks provided");
752 return -EINVAL;
753 }
754
755 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
756 if (err)
757 return err;
758
759 tun_flags |= TUNNEL_VXLAN_OPT;
760 opts_type = type;
761 break;
762 case OVS_TUNNEL_KEY_ATTR_PAD:
763 break;
764 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
765 if (opts_type) {
766 OVS_NLERR(log, "Multiple metadata blocks provided");
767 return -EINVAL;
768 }
769
770 err = erspan_tun_opt_from_nlattr(a, match, is_mask,
771 log);
772 if (err)
773 return err;
774
775 tun_flags |= TUNNEL_ERSPAN_OPT;
776 opts_type = type;
777 break;
778 case OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE:
779 info_bridge_mode = true;
780 ipv4 = true;
781 break;
782 default:
783 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
784 type);
785 return -EINVAL;
786 }
787 }
788
789 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
790 if (is_mask)
791 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
792 else
793 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
794 false);
795
796 if (rem > 0) {
797 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
798 rem);
799 return -EINVAL;
800 }
801
802 if (ipv4 && ipv6) {
803 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
804 return -EINVAL;
805 }
806
807 if (!is_mask) {
808 if (!ipv4 && !ipv6) {
809 OVS_NLERR(log, "IP tunnel dst address not specified");
810 return -EINVAL;
811 }
812 if (ipv4) {
813 if (info_bridge_mode) {
814 if (match->key->tun_key.u.ipv4.src ||
815 match->key->tun_key.u.ipv4.dst ||
816 match->key->tun_key.tp_src ||
817 match->key->tun_key.tp_dst ||
818 match->key->tun_key.ttl ||
819 match->key->tun_key.tos ||
820 tun_flags & ~TUNNEL_KEY) {
821 OVS_NLERR(log, "IPv4 tun info is not correct");
822 return -EINVAL;
823 }
824 } else if (!match->key->tun_key.u.ipv4.dst) {
825 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
826 return -EINVAL;
827 }
828 }
829 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
830 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
831 return -EINVAL;
832 }
833
834 if (!ttl && !info_bridge_mode) {
835 OVS_NLERR(log, "IP tunnel TTL not specified.");
836 return -EINVAL;
837 }
838 }
839
840 return opts_type;
841 }
842
vxlan_opt_to_nlattr(struct sk_buff * skb,const void * tun_opts,int swkey_tun_opts_len)843 static int vxlan_opt_to_nlattr(struct sk_buff *skb,
844 const void *tun_opts, int swkey_tun_opts_len)
845 {
846 const struct vxlan_metadata *opts = tun_opts;
847 struct nlattr *nla;
848
849 nla = nla_nest_start_noflag(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
850 if (!nla)
851 return -EMSGSIZE;
852
853 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
854 return -EMSGSIZE;
855
856 nla_nest_end(skb, nla);
857 return 0;
858 }
859
__ip_tun_to_nlattr(struct sk_buff * skb,const struct ip_tunnel_key * output,const void * tun_opts,int swkey_tun_opts_len,unsigned short tun_proto,u8 mode)860 static int __ip_tun_to_nlattr(struct sk_buff *skb,
861 const struct ip_tunnel_key *output,
862 const void *tun_opts, int swkey_tun_opts_len,
863 unsigned short tun_proto, u8 mode)
864 {
865 if (output->tun_flags & TUNNEL_KEY &&
866 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
867 OVS_TUNNEL_KEY_ATTR_PAD))
868 return -EMSGSIZE;
869
870 if (mode & IP_TUNNEL_INFO_BRIDGE)
871 return nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE)
872 ? -EMSGSIZE : 0;
873
874 switch (tun_proto) {
875 case AF_INET:
876 if (output->u.ipv4.src &&
877 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
878 output->u.ipv4.src))
879 return -EMSGSIZE;
880 if (output->u.ipv4.dst &&
881 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
882 output->u.ipv4.dst))
883 return -EMSGSIZE;
884 break;
885 case AF_INET6:
886 if (!ipv6_addr_any(&output->u.ipv6.src) &&
887 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
888 &output->u.ipv6.src))
889 return -EMSGSIZE;
890 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
891 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
892 &output->u.ipv6.dst))
893 return -EMSGSIZE;
894 break;
895 }
896 if (output->tos &&
897 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
898 return -EMSGSIZE;
899 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
900 return -EMSGSIZE;
901 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
902 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
903 return -EMSGSIZE;
904 if ((output->tun_flags & TUNNEL_CSUM) &&
905 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
906 return -EMSGSIZE;
907 if (output->tp_src &&
908 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
909 return -EMSGSIZE;
910 if (output->tp_dst &&
911 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
912 return -EMSGSIZE;
913 if ((output->tun_flags & TUNNEL_OAM) &&
914 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
915 return -EMSGSIZE;
916 if (swkey_tun_opts_len) {
917 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
918 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
919 swkey_tun_opts_len, tun_opts))
920 return -EMSGSIZE;
921 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
922 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
923 return -EMSGSIZE;
924 else if (output->tun_flags & TUNNEL_ERSPAN_OPT &&
925 nla_put(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
926 swkey_tun_opts_len, tun_opts))
927 return -EMSGSIZE;
928 }
929
930 return 0;
931 }
932
ip_tun_to_nlattr(struct sk_buff * skb,const struct ip_tunnel_key * output,const void * tun_opts,int swkey_tun_opts_len,unsigned short tun_proto,u8 mode)933 static int ip_tun_to_nlattr(struct sk_buff *skb,
934 const struct ip_tunnel_key *output,
935 const void *tun_opts, int swkey_tun_opts_len,
936 unsigned short tun_proto, u8 mode)
937 {
938 struct nlattr *nla;
939 int err;
940
941 nla = nla_nest_start_noflag(skb, OVS_KEY_ATTR_TUNNEL);
942 if (!nla)
943 return -EMSGSIZE;
944
945 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
946 tun_proto, mode);
947 if (err)
948 return err;
949
950 nla_nest_end(skb, nla);
951 return 0;
952 }
953
ovs_nla_put_tunnel_info(struct sk_buff * skb,struct ip_tunnel_info * tun_info)954 int ovs_nla_put_tunnel_info(struct sk_buff *skb,
955 struct ip_tunnel_info *tun_info)
956 {
957 return __ip_tun_to_nlattr(skb, &tun_info->key,
958 ip_tunnel_info_opts(tun_info),
959 tun_info->options_len,
960 ip_tunnel_info_af(tun_info), tun_info->mode);
961 }
962
encode_vlan_from_nlattrs(struct sw_flow_match * match,const struct nlattr * a[],bool is_mask,bool inner)963 static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
964 const struct nlattr *a[],
965 bool is_mask, bool inner)
966 {
967 __be16 tci = 0;
968 __be16 tpid = 0;
969
970 if (a[OVS_KEY_ATTR_VLAN])
971 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
972
973 if (a[OVS_KEY_ATTR_ETHERTYPE])
974 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
975
976 if (likely(!inner)) {
977 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
978 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
979 } else {
980 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
981 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
982 }
983 return 0;
984 }
985
validate_vlan_from_nlattrs(const struct sw_flow_match * match,u64 key_attrs,bool inner,const struct nlattr ** a,bool log)986 static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
987 u64 key_attrs, bool inner,
988 const struct nlattr **a, bool log)
989 {
990 __be16 tci = 0;
991
992 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
993 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
994 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
995 /* Not a VLAN. */
996 return 0;
997 }
998
999 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
1000 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
1001 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
1002 return -EINVAL;
1003 }
1004
1005 if (a[OVS_KEY_ATTR_VLAN])
1006 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1007
1008 if (!(tci & htons(VLAN_CFI_MASK))) {
1009 if (tci) {
1010 OVS_NLERR(log, "%s TCI does not have VLAN_CFI_MASK bit set.",
1011 (inner) ? "C-VLAN" : "VLAN");
1012 return -EINVAL;
1013 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
1014 /* Corner case for truncated VLAN header. */
1015 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
1016 (inner) ? "C-VLAN" : "VLAN");
1017 return -EINVAL;
1018 }
1019 }
1020
1021 return 1;
1022 }
1023
validate_vlan_mask_from_nlattrs(const struct sw_flow_match * match,u64 key_attrs,bool inner,const struct nlattr ** a,bool log)1024 static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
1025 u64 key_attrs, bool inner,
1026 const struct nlattr **a, bool log)
1027 {
1028 __be16 tci = 0;
1029 __be16 tpid = 0;
1030 bool encap_valid = !!(match->key->eth.vlan.tci &
1031 htons(VLAN_CFI_MASK));
1032 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
1033 htons(VLAN_CFI_MASK));
1034
1035 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
1036 /* Not a VLAN. */
1037 return 0;
1038 }
1039
1040 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
1041 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
1042 (inner) ? "C-VLAN" : "VLAN");
1043 return -EINVAL;
1044 }
1045
1046 if (a[OVS_KEY_ATTR_VLAN])
1047 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1048
1049 if (a[OVS_KEY_ATTR_ETHERTYPE])
1050 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1051
1052 if (tpid != htons(0xffff)) {
1053 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
1054 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
1055 return -EINVAL;
1056 }
1057 if (!(tci & htons(VLAN_CFI_MASK))) {
1058 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_CFI_MASK bit.",
1059 (inner) ? "C-VLAN" : "VLAN");
1060 return -EINVAL;
1061 }
1062
1063 return 1;
1064 }
1065
__parse_vlan_from_nlattrs(struct sw_flow_match * match,u64 * key_attrs,bool inner,const struct nlattr ** a,bool is_mask,bool log)1066 static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
1067 u64 *key_attrs, bool inner,
1068 const struct nlattr **a, bool is_mask,
1069 bool log)
1070 {
1071 int err;
1072 const struct nlattr *encap;
1073
1074 if (!is_mask)
1075 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
1076 a, log);
1077 else
1078 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
1079 a, log);
1080 if (err <= 0)
1081 return err;
1082
1083 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
1084 if (err)
1085 return err;
1086
1087 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1088 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
1089 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1090
1091 encap = a[OVS_KEY_ATTR_ENCAP];
1092
1093 if (!is_mask)
1094 err = parse_flow_nlattrs(encap, a, key_attrs, log);
1095 else
1096 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
1097
1098 return err;
1099 }
1100
parse_vlan_from_nlattrs(struct sw_flow_match * match,u64 * key_attrs,const struct nlattr ** a,bool is_mask,bool log)1101 static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
1102 u64 *key_attrs, const struct nlattr **a,
1103 bool is_mask, bool log)
1104 {
1105 int err;
1106 bool encap_valid = false;
1107
1108 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
1109 is_mask, log);
1110 if (err)
1111 return err;
1112
1113 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_CFI_MASK));
1114 if (encap_valid) {
1115 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
1116 is_mask, log);
1117 if (err)
1118 return err;
1119 }
1120
1121 return 0;
1122 }
1123
parse_eth_type_from_nlattrs(struct sw_flow_match * match,u64 * attrs,const struct nlattr ** a,bool is_mask,bool log)1124 static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
1125 u64 *attrs, const struct nlattr **a,
1126 bool is_mask, bool log)
1127 {
1128 __be16 eth_type;
1129
1130 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1131 if (is_mask) {
1132 /* Always exact match EtherType. */
1133 eth_type = htons(0xffff);
1134 } else if (!eth_proto_is_802_3(eth_type)) {
1135 OVS_NLERR(log, "EtherType %x is less than min %x",
1136 ntohs(eth_type), ETH_P_802_3_MIN);
1137 return -EINVAL;
1138 }
1139
1140 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1141 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1142 return 0;
1143 }
1144
metadata_from_nlattrs(struct net * net,struct sw_flow_match * match,u64 * attrs,const struct nlattr ** a,bool is_mask,bool log)1145 static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1146 u64 *attrs, const struct nlattr **a,
1147 bool is_mask, bool log)
1148 {
1149 u8 mac_proto = MAC_PROTO_ETHERNET;
1150
1151 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
1152 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1153
1154 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1155 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
1156 }
1157
1158 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
1159 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1160
1161 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1162 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
1163 }
1164
1165 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1166 SW_FLOW_KEY_PUT(match, phy.priority,
1167 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1168 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1169 }
1170
1171 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1172 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1173
1174 if (is_mask) {
1175 in_port = 0xffffffff; /* Always exact match in_port. */
1176 } else if (in_port >= DP_MAX_PORTS) {
1177 OVS_NLERR(log, "Port %d exceeds max allowable %d",
1178 in_port, DP_MAX_PORTS);
1179 return -EINVAL;
1180 }
1181
1182 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1183 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1184 } else if (!is_mask) {
1185 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1186 }
1187
1188 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1189 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1190
1191 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1192 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1193 }
1194 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
1195 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1196 is_mask, log) < 0)
1197 return -EINVAL;
1198 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1199 }
1200
1201 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
1202 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
1203 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
1204
1205 if (ct_state & ~CT_SUPPORTED_MASK) {
1206 OVS_NLERR(log, "ct_state flags %08x unsupported",
1207 ct_state);
1208 return -EINVAL;
1209 }
1210
1211 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
1212 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1213 }
1214 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
1215 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
1216 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1217
1218 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
1219 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1220 }
1221 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
1222 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
1223 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1224
1225 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1226 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1227 }
1228 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1229 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1230 const struct ovs_key_ct_labels *cl;
1231
1232 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1233 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
1234 sizeof(*cl), is_mask);
1235 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
1236 }
1237 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1238 const struct ovs_key_ct_tuple_ipv4 *ct;
1239
1240 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1241
1242 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1243 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1244 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1245 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
1246 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
1247 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1248 }
1249 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1250 const struct ovs_key_ct_tuple_ipv6 *ct;
1251
1252 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1253
1254 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1255 sizeof(match->key->ipv6.ct_orig.src),
1256 is_mask);
1257 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1258 sizeof(match->key->ipv6.ct_orig.dst),
1259 is_mask);
1260 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1261 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
1262 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
1263 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1264 }
1265
1266 /* For layer 3 packets the Ethernet type is provided
1267 * and treated as metadata but no MAC addresses are provided.
1268 */
1269 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1270 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1271 mac_proto = MAC_PROTO_NONE;
1272
1273 /* Always exact match mac_proto */
1274 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1275
1276 if (mac_proto == MAC_PROTO_NONE)
1277 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1278 log);
1279
1280 return 0;
1281 }
1282
nsh_hdr_from_nlattr(const struct nlattr * attr,struct nshhdr * nh,size_t size)1283 int nsh_hdr_from_nlattr(const struct nlattr *attr,
1284 struct nshhdr *nh, size_t size)
1285 {
1286 struct nlattr *a;
1287 int rem;
1288 u8 flags = 0;
1289 u8 ttl = 0;
1290 int mdlen = 0;
1291
1292 /* validate_nsh has check this, so we needn't do duplicate check here
1293 */
1294 if (size < NSH_BASE_HDR_LEN)
1295 return -ENOBUFS;
1296
1297 nla_for_each_nested(a, attr, rem) {
1298 int type = nla_type(a);
1299
1300 switch (type) {
1301 case OVS_NSH_KEY_ATTR_BASE: {
1302 const struct ovs_nsh_key_base *base = nla_data(a);
1303
1304 flags = base->flags;
1305 ttl = base->ttl;
1306 nh->np = base->np;
1307 nh->mdtype = base->mdtype;
1308 nh->path_hdr = base->path_hdr;
1309 break;
1310 }
1311 case OVS_NSH_KEY_ATTR_MD1:
1312 mdlen = nla_len(a);
1313 if (mdlen > size - NSH_BASE_HDR_LEN)
1314 return -ENOBUFS;
1315 memcpy(&nh->md1, nla_data(a), mdlen);
1316 break;
1317
1318 case OVS_NSH_KEY_ATTR_MD2:
1319 mdlen = nla_len(a);
1320 if (mdlen > size - NSH_BASE_HDR_LEN)
1321 return -ENOBUFS;
1322 memcpy(&nh->md2, nla_data(a), mdlen);
1323 break;
1324
1325 default:
1326 return -EINVAL;
1327 }
1328 }
1329
1330 /* nsh header length = NSH_BASE_HDR_LEN + mdlen */
1331 nh->ver_flags_ttl_len = 0;
1332 nsh_set_flags_ttl_len(nh, flags, ttl, NSH_BASE_HDR_LEN + mdlen);
1333
1334 return 0;
1335 }
1336
nsh_key_from_nlattr(const struct nlattr * attr,struct ovs_key_nsh * nsh,struct ovs_key_nsh * nsh_mask)1337 int nsh_key_from_nlattr(const struct nlattr *attr,
1338 struct ovs_key_nsh *nsh, struct ovs_key_nsh *nsh_mask)
1339 {
1340 struct nlattr *a;
1341 int rem;
1342
1343 /* validate_nsh has check this, so we needn't do duplicate check here
1344 */
1345 nla_for_each_nested(a, attr, rem) {
1346 int type = nla_type(a);
1347
1348 switch (type) {
1349 case OVS_NSH_KEY_ATTR_BASE: {
1350 const struct ovs_nsh_key_base *base = nla_data(a);
1351 const struct ovs_nsh_key_base *base_mask = base + 1;
1352
1353 nsh->base = *base;
1354 nsh_mask->base = *base_mask;
1355 break;
1356 }
1357 case OVS_NSH_KEY_ATTR_MD1: {
1358 const struct ovs_nsh_key_md1 *md1 = nla_data(a);
1359 const struct ovs_nsh_key_md1 *md1_mask = md1 + 1;
1360
1361 memcpy(nsh->context, md1->context, sizeof(*md1));
1362 memcpy(nsh_mask->context, md1_mask->context,
1363 sizeof(*md1_mask));
1364 break;
1365 }
1366 case OVS_NSH_KEY_ATTR_MD2:
1367 /* Not supported yet */
1368 return -ENOTSUPP;
1369 default:
1370 return -EINVAL;
1371 }
1372 }
1373
1374 return 0;
1375 }
1376
nsh_key_put_from_nlattr(const struct nlattr * attr,struct sw_flow_match * match,bool is_mask,bool is_push_nsh,bool log)1377 static int nsh_key_put_from_nlattr(const struct nlattr *attr,
1378 struct sw_flow_match *match, bool is_mask,
1379 bool is_push_nsh, bool log)
1380 {
1381 struct nlattr *a;
1382 int rem;
1383 bool has_base = false;
1384 bool has_md1 = false;
1385 bool has_md2 = false;
1386 u8 mdtype = 0;
1387 int mdlen = 0;
1388
1389 if (WARN_ON(is_push_nsh && is_mask))
1390 return -EINVAL;
1391
1392 nla_for_each_nested(a, attr, rem) {
1393 int type = nla_type(a);
1394 int i;
1395
1396 if (type > OVS_NSH_KEY_ATTR_MAX) {
1397 OVS_NLERR(log, "nsh attr %d is out of range max %d",
1398 type, OVS_NSH_KEY_ATTR_MAX);
1399 return -EINVAL;
1400 }
1401
1402 if (!check_attr_len(nla_len(a),
1403 ovs_nsh_key_attr_lens[type].len)) {
1404 OVS_NLERR(
1405 log,
1406 "nsh attr %d has unexpected len %d expected %d",
1407 type,
1408 nla_len(a),
1409 ovs_nsh_key_attr_lens[type].len
1410 );
1411 return -EINVAL;
1412 }
1413
1414 switch (type) {
1415 case OVS_NSH_KEY_ATTR_BASE: {
1416 const struct ovs_nsh_key_base *base = nla_data(a);
1417
1418 has_base = true;
1419 mdtype = base->mdtype;
1420 SW_FLOW_KEY_PUT(match, nsh.base.flags,
1421 base->flags, is_mask);
1422 SW_FLOW_KEY_PUT(match, nsh.base.ttl,
1423 base->ttl, is_mask);
1424 SW_FLOW_KEY_PUT(match, nsh.base.mdtype,
1425 base->mdtype, is_mask);
1426 SW_FLOW_KEY_PUT(match, nsh.base.np,
1427 base->np, is_mask);
1428 SW_FLOW_KEY_PUT(match, nsh.base.path_hdr,
1429 base->path_hdr, is_mask);
1430 break;
1431 }
1432 case OVS_NSH_KEY_ATTR_MD1: {
1433 const struct ovs_nsh_key_md1 *md1 = nla_data(a);
1434
1435 has_md1 = true;
1436 for (i = 0; i < NSH_MD1_CONTEXT_SIZE; i++)
1437 SW_FLOW_KEY_PUT(match, nsh.context[i],
1438 md1->context[i], is_mask);
1439 break;
1440 }
1441 case OVS_NSH_KEY_ATTR_MD2:
1442 if (!is_push_nsh) /* Not supported MD type 2 yet */
1443 return -ENOTSUPP;
1444
1445 has_md2 = true;
1446 mdlen = nla_len(a);
1447 if (mdlen > NSH_CTX_HDRS_MAX_LEN || mdlen <= 0) {
1448 OVS_NLERR(
1449 log,
1450 "Invalid MD length %d for MD type %d",
1451 mdlen,
1452 mdtype
1453 );
1454 return -EINVAL;
1455 }
1456 break;
1457 default:
1458 OVS_NLERR(log, "Unknown nsh attribute %d",
1459 type);
1460 return -EINVAL;
1461 }
1462 }
1463
1464 if (rem > 0) {
1465 OVS_NLERR(log, "nsh attribute has %d unknown bytes.", rem);
1466 return -EINVAL;
1467 }
1468
1469 if (has_md1 && has_md2) {
1470 OVS_NLERR(
1471 1,
1472 "invalid nsh attribute: md1 and md2 are exclusive."
1473 );
1474 return -EINVAL;
1475 }
1476
1477 if (!is_mask) {
1478 if ((has_md1 && mdtype != NSH_M_TYPE1) ||
1479 (has_md2 && mdtype != NSH_M_TYPE2)) {
1480 OVS_NLERR(1, "nsh attribute has unmatched MD type %d.",
1481 mdtype);
1482 return -EINVAL;
1483 }
1484
1485 if (is_push_nsh &&
1486 (!has_base || (!has_md1 && !has_md2))) {
1487 OVS_NLERR(
1488 1,
1489 "push_nsh: missing base or metadata attributes"
1490 );
1491 return -EINVAL;
1492 }
1493 }
1494
1495 return 0;
1496 }
1497
ovs_key_from_nlattrs(struct net * net,struct sw_flow_match * match,u64 attrs,const struct nlattr ** a,bool is_mask,bool log)1498 static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1499 u64 attrs, const struct nlattr **a,
1500 bool is_mask, bool log)
1501 {
1502 int err;
1503
1504 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
1505 if (err)
1506 return err;
1507
1508 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1509 const struct ovs_key_ethernet *eth_key;
1510
1511 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1512 SW_FLOW_KEY_MEMCPY(match, eth.src,
1513 eth_key->eth_src, ETH_ALEN, is_mask);
1514 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1515 eth_key->eth_dst, ETH_ALEN, is_mask);
1516 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1517
1518 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1519 /* VLAN attribute is always parsed before getting here since it
1520 * may occur multiple times.
1521 */
1522 OVS_NLERR(log, "VLAN attribute unexpected.");
1523 return -EINVAL;
1524 }
1525
1526 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1527 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1528 log);
1529 if (err)
1530 return err;
1531 } else if (!is_mask) {
1532 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1533 }
1534 } else if (!match->key->eth.type) {
1535 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1536 return -EINVAL;
1537 }
1538
1539 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1540 const struct ovs_key_ipv4 *ipv4_key;
1541
1542 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1543 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1544 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1545 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1546 return -EINVAL;
1547 }
1548 SW_FLOW_KEY_PUT(match, ip.proto,
1549 ipv4_key->ipv4_proto, is_mask);
1550 SW_FLOW_KEY_PUT(match, ip.tos,
1551 ipv4_key->ipv4_tos, is_mask);
1552 SW_FLOW_KEY_PUT(match, ip.ttl,
1553 ipv4_key->ipv4_ttl, is_mask);
1554 SW_FLOW_KEY_PUT(match, ip.frag,
1555 ipv4_key->ipv4_frag, is_mask);
1556 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1557 ipv4_key->ipv4_src, is_mask);
1558 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1559 ipv4_key->ipv4_dst, is_mask);
1560 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1561 }
1562
1563 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1564 const struct ovs_key_ipv6 *ipv6_key;
1565
1566 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1567 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1568 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1569 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1570 return -EINVAL;
1571 }
1572
1573 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
1574 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x)",
1575 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1576 return -EINVAL;
1577 }
1578
1579 SW_FLOW_KEY_PUT(match, ipv6.label,
1580 ipv6_key->ipv6_label, is_mask);
1581 SW_FLOW_KEY_PUT(match, ip.proto,
1582 ipv6_key->ipv6_proto, is_mask);
1583 SW_FLOW_KEY_PUT(match, ip.tos,
1584 ipv6_key->ipv6_tclass, is_mask);
1585 SW_FLOW_KEY_PUT(match, ip.ttl,
1586 ipv6_key->ipv6_hlimit, is_mask);
1587 SW_FLOW_KEY_PUT(match, ip.frag,
1588 ipv6_key->ipv6_frag, is_mask);
1589 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1590 ipv6_key->ipv6_src,
1591 sizeof(match->key->ipv6.addr.src),
1592 is_mask);
1593 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1594 ipv6_key->ipv6_dst,
1595 sizeof(match->key->ipv6.addr.dst),
1596 is_mask);
1597
1598 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1599 }
1600
1601 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1602 const struct ovs_key_arp *arp_key;
1603
1604 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1605 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1606 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
1607 arp_key->arp_op);
1608 return -EINVAL;
1609 }
1610
1611 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1612 arp_key->arp_sip, is_mask);
1613 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1614 arp_key->arp_tip, is_mask);
1615 SW_FLOW_KEY_PUT(match, ip.proto,
1616 ntohs(arp_key->arp_op), is_mask);
1617 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1618 arp_key->arp_sha, ETH_ALEN, is_mask);
1619 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1620 arp_key->arp_tha, ETH_ALEN, is_mask);
1621
1622 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1623 }
1624
1625 if (attrs & (1 << OVS_KEY_ATTR_NSH)) {
1626 if (nsh_key_put_from_nlattr(a[OVS_KEY_ATTR_NSH], match,
1627 is_mask, false, log) < 0)
1628 return -EINVAL;
1629 attrs &= ~(1 << OVS_KEY_ATTR_NSH);
1630 }
1631
1632 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
1633 const struct ovs_key_mpls *mpls_key;
1634 u32 hdr_len;
1635 u32 label_count, label_count_mask, i;
1636
1637 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1638 hdr_len = nla_len(a[OVS_KEY_ATTR_MPLS]);
1639 label_count = hdr_len / sizeof(struct ovs_key_mpls);
1640
1641 if (label_count == 0 || label_count > MPLS_LABEL_DEPTH ||
1642 hdr_len % sizeof(struct ovs_key_mpls))
1643 return -EINVAL;
1644
1645 label_count_mask = GENMASK(label_count - 1, 0);
1646
1647 for (i = 0 ; i < label_count; i++)
1648 SW_FLOW_KEY_PUT(match, mpls.lse[i],
1649 mpls_key[i].mpls_lse, is_mask);
1650
1651 SW_FLOW_KEY_PUT(match, mpls.num_labels_mask,
1652 label_count_mask, is_mask);
1653
1654 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1655 }
1656
1657 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1658 const struct ovs_key_tcp *tcp_key;
1659
1660 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1661 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1662 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
1663 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1664 }
1665
1666 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
1667 SW_FLOW_KEY_PUT(match, tp.flags,
1668 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1669 is_mask);
1670 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1671 }
1672
1673 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1674 const struct ovs_key_udp *udp_key;
1675
1676 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1677 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1678 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
1679 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1680 }
1681
1682 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1683 const struct ovs_key_sctp *sctp_key;
1684
1685 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1686 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1687 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
1688 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1689 }
1690
1691 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1692 const struct ovs_key_icmp *icmp_key;
1693
1694 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1695 SW_FLOW_KEY_PUT(match, tp.src,
1696 htons(icmp_key->icmp_type), is_mask);
1697 SW_FLOW_KEY_PUT(match, tp.dst,
1698 htons(icmp_key->icmp_code), is_mask);
1699 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1700 }
1701
1702 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1703 const struct ovs_key_icmpv6 *icmpv6_key;
1704
1705 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1706 SW_FLOW_KEY_PUT(match, tp.src,
1707 htons(icmpv6_key->icmpv6_type), is_mask);
1708 SW_FLOW_KEY_PUT(match, tp.dst,
1709 htons(icmpv6_key->icmpv6_code), is_mask);
1710 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1711 }
1712
1713 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1714 const struct ovs_key_nd *nd_key;
1715
1716 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1717 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1718 nd_key->nd_target,
1719 sizeof(match->key->ipv6.nd.target),
1720 is_mask);
1721 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1722 nd_key->nd_sll, ETH_ALEN, is_mask);
1723 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1724 nd_key->nd_tll, ETH_ALEN, is_mask);
1725 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1726 }
1727
1728 if (attrs != 0) {
1729 OVS_NLERR(log, "Unknown key attributes %llx",
1730 (unsigned long long)attrs);
1731 return -EINVAL;
1732 }
1733
1734 return 0;
1735 }
1736
nlattr_set(struct nlattr * attr,u8 val,const struct ovs_len_tbl * tbl)1737 static void nlattr_set(struct nlattr *attr, u8 val,
1738 const struct ovs_len_tbl *tbl)
1739 {
1740 struct nlattr *nla;
1741 int rem;
1742
1743 /* The nlattr stream should already have been validated */
1744 nla_for_each_nested(nla, attr, rem) {
1745 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
1746 nlattr_set(nla, val, tbl[nla_type(nla)].next ? : tbl);
1747 else
1748 memset(nla_data(nla), val, nla_len(nla));
1749
1750 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1751 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
1752 }
1753 }
1754
mask_set_nlattr(struct nlattr * attr,u8 val)1755 static void mask_set_nlattr(struct nlattr *attr, u8 val)
1756 {
1757 nlattr_set(attr, val, ovs_key_lens);
1758 }
1759
1760 /**
1761 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1762 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1763 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1764 * does not include any don't care bit.
1765 * @net: Used to determine per-namespace field support.
1766 * @match: receives the extracted flow match information.
1767 * @nla_key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1768 * sequence. The fields should of the packet that triggered the creation
1769 * of this flow.
1770 * @nla_mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_*
1771 * Netlink attribute specifies the mask field of the wildcarded flow.
1772 * @log: Boolean to allow kernel error logging. Normally true, but when
1773 * probing for feature compatibility this should be passed in as false to
1774 * suppress unnecessary error logging.
1775 */
ovs_nla_get_match(struct net * net,struct sw_flow_match * match,const struct nlattr * nla_key,const struct nlattr * nla_mask,bool log)1776 int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
1777 const struct nlattr *nla_key,
1778 const struct nlattr *nla_mask,
1779 bool log)
1780 {
1781 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1782 struct nlattr *newmask = NULL;
1783 u64 key_attrs = 0;
1784 u64 mask_attrs = 0;
1785 int err;
1786
1787 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
1788 if (err)
1789 return err;
1790
1791 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1792 if (err)
1793 return err;
1794
1795 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
1796 if (err)
1797 return err;
1798
1799 if (match->mask) {
1800 if (!nla_mask) {
1801 /* Create an exact match mask. We need to set to 0xff
1802 * all the 'match->mask' fields that have been touched
1803 * in 'match->key'. We cannot simply memset
1804 * 'match->mask', because padding bytes and fields not
1805 * specified in 'match->key' should be left to 0.
1806 * Instead, we use a stream of netlink attributes,
1807 * copied from 'key' and set to 0xff.
1808 * ovs_key_from_nlattrs() will take care of filling
1809 * 'match->mask' appropriately.
1810 */
1811 newmask = kmemdup(nla_key,
1812 nla_total_size(nla_len(nla_key)),
1813 GFP_KERNEL);
1814 if (!newmask)
1815 return -ENOMEM;
1816
1817 mask_set_nlattr(newmask, 0xff);
1818
1819 /* The userspace does not send tunnel attributes that
1820 * are 0, but we should not wildcard them nonetheless.
1821 */
1822 if (match->key->tun_proto)
1823 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1824 0xff, true);
1825
1826 nla_mask = newmask;
1827 }
1828
1829 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
1830 if (err)
1831 goto free_newmask;
1832
1833 /* Always match on tci. */
1834 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1835 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
1836
1837 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1838 if (err)
1839 goto free_newmask;
1840
1841 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1842 log);
1843 if (err)
1844 goto free_newmask;
1845 }
1846
1847 if (!match_validate(match, key_attrs, mask_attrs, log))
1848 err = -EINVAL;
1849
1850 free_newmask:
1851 kfree(newmask);
1852 return err;
1853 }
1854
get_ufid_len(const struct nlattr * attr,bool log)1855 static size_t get_ufid_len(const struct nlattr *attr, bool log)
1856 {
1857 size_t len;
1858
1859 if (!attr)
1860 return 0;
1861
1862 len = nla_len(attr);
1863 if (len < 1 || len > MAX_UFID_LENGTH) {
1864 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1865 nla_len(attr), MAX_UFID_LENGTH);
1866 return 0;
1867 }
1868
1869 return len;
1870 }
1871
1872 /* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1873 * or false otherwise.
1874 */
ovs_nla_get_ufid(struct sw_flow_id * sfid,const struct nlattr * attr,bool log)1875 bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1876 bool log)
1877 {
1878 sfid->ufid_len = get_ufid_len(attr, log);
1879 if (sfid->ufid_len)
1880 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1881
1882 return sfid->ufid_len;
1883 }
1884
ovs_nla_get_identifier(struct sw_flow_id * sfid,const struct nlattr * ufid,const struct sw_flow_key * key,bool log)1885 int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1886 const struct sw_flow_key *key, bool log)
1887 {
1888 struct sw_flow_key *new_key;
1889
1890 if (ovs_nla_get_ufid(sfid, ufid, log))
1891 return 0;
1892
1893 /* If UFID was not provided, use unmasked key. */
1894 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1895 if (!new_key)
1896 return -ENOMEM;
1897 memcpy(new_key, key, sizeof(*key));
1898 sfid->unmasked_key = new_key;
1899
1900 return 0;
1901 }
1902
ovs_nla_get_ufid_flags(const struct nlattr * attr)1903 u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1904 {
1905 return attr ? nla_get_u32(attr) : 0;
1906 }
1907
1908 /**
1909 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
1910 * @net: Network namespace.
1911 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1912 * metadata.
1913 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1914 * attributes.
1915 * @attrs: Bit mask for the netlink attributes included in @a.
1916 * @log: Boolean to allow kernel error logging. Normally true, but when
1917 * probing for feature compatibility this should be passed in as false to
1918 * suppress unnecessary error logging.
1919 *
1920 * This parses a series of Netlink attributes that form a flow key, which must
1921 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1922 * get the metadata, that is, the parts of the flow key that cannot be
1923 * extracted from the packet itself.
1924 *
1925 * This must be called before the packet key fields are filled in 'key'.
1926 */
1927
ovs_nla_get_flow_metadata(struct net * net,const struct nlattr * a[OVS_KEY_ATTR_MAX+1],u64 attrs,struct sw_flow_key * key,bool log)1928 int ovs_nla_get_flow_metadata(struct net *net,
1929 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1930 u64 attrs, struct sw_flow_key *key, bool log)
1931 {
1932 struct sw_flow_match match;
1933
1934 memset(&match, 0, sizeof(match));
1935 match.key = key;
1936
1937 key->ct_state = 0;
1938 key->ct_zone = 0;
1939 key->ct_orig_proto = 0;
1940 memset(&key->ct, 0, sizeof(key->ct));
1941 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1942 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1943
1944 key->phy.in_port = DP_MAX_PORTS;
1945
1946 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
1947 }
1948
ovs_nla_put_vlan(struct sk_buff * skb,const struct vlan_head * vh,bool is_mask)1949 static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1950 bool is_mask)
1951 {
1952 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1953
1954 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1955 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1956 return -EMSGSIZE;
1957 return 0;
1958 }
1959
nsh_key_to_nlattr(const struct ovs_key_nsh * nsh,bool is_mask,struct sk_buff * skb)1960 static int nsh_key_to_nlattr(const struct ovs_key_nsh *nsh, bool is_mask,
1961 struct sk_buff *skb)
1962 {
1963 struct nlattr *start;
1964
1965 start = nla_nest_start_noflag(skb, OVS_KEY_ATTR_NSH);
1966 if (!start)
1967 return -EMSGSIZE;
1968
1969 if (nla_put(skb, OVS_NSH_KEY_ATTR_BASE, sizeof(nsh->base), &nsh->base))
1970 goto nla_put_failure;
1971
1972 if (is_mask || nsh->base.mdtype == NSH_M_TYPE1) {
1973 if (nla_put(skb, OVS_NSH_KEY_ATTR_MD1,
1974 sizeof(nsh->context), nsh->context))
1975 goto nla_put_failure;
1976 }
1977
1978 /* Don't support MD type 2 yet */
1979
1980 nla_nest_end(skb, start);
1981
1982 return 0;
1983
1984 nla_put_failure:
1985 return -EMSGSIZE;
1986 }
1987
__ovs_nla_put_key(const struct sw_flow_key * swkey,const struct sw_flow_key * output,bool is_mask,struct sk_buff * skb)1988 static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1989 const struct sw_flow_key *output, bool is_mask,
1990 struct sk_buff *skb)
1991 {
1992 struct ovs_key_ethernet *eth_key;
1993 struct nlattr *nla;
1994 struct nlattr *encap = NULL;
1995 struct nlattr *in_encap = NULL;
1996
1997 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1998 goto nla_put_failure;
1999
2000 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
2001 goto nla_put_failure;
2002
2003 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
2004 goto nla_put_failure;
2005
2006 if ((swkey->tun_proto || is_mask)) {
2007 const void *opts = NULL;
2008
2009 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
2010 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
2011
2012 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
2013 swkey->tun_opts_len, swkey->tun_proto, 0))
2014 goto nla_put_failure;
2015 }
2016
2017 if (swkey->phy.in_port == DP_MAX_PORTS) {
2018 if (is_mask && (output->phy.in_port == 0xffff))
2019 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
2020 goto nla_put_failure;
2021 } else {
2022 u16 upper_u16;
2023 upper_u16 = !is_mask ? 0 : 0xffff;
2024
2025 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
2026 (upper_u16 << 16) | output->phy.in_port))
2027 goto nla_put_failure;
2028 }
2029
2030 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
2031 goto nla_put_failure;
2032
2033 if (ovs_ct_put_key(swkey, output, skb))
2034 goto nla_put_failure;
2035
2036 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
2037 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
2038 if (!nla)
2039 goto nla_put_failure;
2040
2041 eth_key = nla_data(nla);
2042 ether_addr_copy(eth_key->eth_src, output->eth.src);
2043 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
2044
2045 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
2046 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
2047 goto nla_put_failure;
2048 encap = nla_nest_start_noflag(skb, OVS_KEY_ATTR_ENCAP);
2049 if (!swkey->eth.vlan.tci)
2050 goto unencap;
2051
2052 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
2053 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
2054 goto nla_put_failure;
2055 in_encap = nla_nest_start_noflag(skb,
2056 OVS_KEY_ATTR_ENCAP);
2057 if (!swkey->eth.cvlan.tci)
2058 goto unencap;
2059 }
2060 }
2061
2062 if (swkey->eth.type == htons(ETH_P_802_2)) {
2063 /*
2064 * Ethertype 802.2 is represented in the netlink with omitted
2065 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
2066 * 0xffff in the mask attribute. Ethertype can also
2067 * be wildcarded.
2068 */
2069 if (is_mask && output->eth.type)
2070 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
2071 output->eth.type))
2072 goto nla_put_failure;
2073 goto unencap;
2074 }
2075 }
2076
2077 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
2078 goto nla_put_failure;
2079
2080 if (eth_type_vlan(swkey->eth.type)) {
2081 /* There are 3 VLAN tags, we don't know anything about the rest
2082 * of the packet, so truncate here.
2083 */
2084 WARN_ON_ONCE(!(encap && in_encap));
2085 goto unencap;
2086 }
2087
2088 if (swkey->eth.type == htons(ETH_P_IP)) {
2089 struct ovs_key_ipv4 *ipv4_key;
2090
2091 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
2092 if (!nla)
2093 goto nla_put_failure;
2094 ipv4_key = nla_data(nla);
2095 ipv4_key->ipv4_src = output->ipv4.addr.src;
2096 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
2097 ipv4_key->ipv4_proto = output->ip.proto;
2098 ipv4_key->ipv4_tos = output->ip.tos;
2099 ipv4_key->ipv4_ttl = output->ip.ttl;
2100 ipv4_key->ipv4_frag = output->ip.frag;
2101 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
2102 struct ovs_key_ipv6 *ipv6_key;
2103
2104 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
2105 if (!nla)
2106 goto nla_put_failure;
2107 ipv6_key = nla_data(nla);
2108 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
2109 sizeof(ipv6_key->ipv6_src));
2110 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
2111 sizeof(ipv6_key->ipv6_dst));
2112 ipv6_key->ipv6_label = output->ipv6.label;
2113 ipv6_key->ipv6_proto = output->ip.proto;
2114 ipv6_key->ipv6_tclass = output->ip.tos;
2115 ipv6_key->ipv6_hlimit = output->ip.ttl;
2116 ipv6_key->ipv6_frag = output->ip.frag;
2117 } else if (swkey->eth.type == htons(ETH_P_NSH)) {
2118 if (nsh_key_to_nlattr(&output->nsh, is_mask, skb))
2119 goto nla_put_failure;
2120 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
2121 swkey->eth.type == htons(ETH_P_RARP)) {
2122 struct ovs_key_arp *arp_key;
2123
2124 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
2125 if (!nla)
2126 goto nla_put_failure;
2127 arp_key = nla_data(nla);
2128 memset(arp_key, 0, sizeof(struct ovs_key_arp));
2129 arp_key->arp_sip = output->ipv4.addr.src;
2130 arp_key->arp_tip = output->ipv4.addr.dst;
2131 arp_key->arp_op = htons(output->ip.proto);
2132 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
2133 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
2134 } else if (eth_p_mpls(swkey->eth.type)) {
2135 u8 i, num_labels;
2136 struct ovs_key_mpls *mpls_key;
2137
2138 num_labels = hweight_long(output->mpls.num_labels_mask);
2139 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS,
2140 num_labels * sizeof(*mpls_key));
2141 if (!nla)
2142 goto nla_put_failure;
2143
2144 mpls_key = nla_data(nla);
2145 for (i = 0; i < num_labels; i++)
2146 mpls_key[i].mpls_lse = output->mpls.lse[i];
2147 }
2148
2149 if ((swkey->eth.type == htons(ETH_P_IP) ||
2150 swkey->eth.type == htons(ETH_P_IPV6)) &&
2151 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
2152
2153 if (swkey->ip.proto == IPPROTO_TCP) {
2154 struct ovs_key_tcp *tcp_key;
2155
2156 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
2157 if (!nla)
2158 goto nla_put_failure;
2159 tcp_key = nla_data(nla);
2160 tcp_key->tcp_src = output->tp.src;
2161 tcp_key->tcp_dst = output->tp.dst;
2162 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
2163 output->tp.flags))
2164 goto nla_put_failure;
2165 } else if (swkey->ip.proto == IPPROTO_UDP) {
2166 struct ovs_key_udp *udp_key;
2167
2168 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
2169 if (!nla)
2170 goto nla_put_failure;
2171 udp_key = nla_data(nla);
2172 udp_key->udp_src = output->tp.src;
2173 udp_key->udp_dst = output->tp.dst;
2174 } else if (swkey->ip.proto == IPPROTO_SCTP) {
2175 struct ovs_key_sctp *sctp_key;
2176
2177 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
2178 if (!nla)
2179 goto nla_put_failure;
2180 sctp_key = nla_data(nla);
2181 sctp_key->sctp_src = output->tp.src;
2182 sctp_key->sctp_dst = output->tp.dst;
2183 } else if (swkey->eth.type == htons(ETH_P_IP) &&
2184 swkey->ip.proto == IPPROTO_ICMP) {
2185 struct ovs_key_icmp *icmp_key;
2186
2187 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
2188 if (!nla)
2189 goto nla_put_failure;
2190 icmp_key = nla_data(nla);
2191 icmp_key->icmp_type = ntohs(output->tp.src);
2192 icmp_key->icmp_code = ntohs(output->tp.dst);
2193 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
2194 swkey->ip.proto == IPPROTO_ICMPV6) {
2195 struct ovs_key_icmpv6 *icmpv6_key;
2196
2197 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
2198 sizeof(*icmpv6_key));
2199 if (!nla)
2200 goto nla_put_failure;
2201 icmpv6_key = nla_data(nla);
2202 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
2203 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
2204
2205 if (swkey->tp.src == htons(NDISC_NEIGHBOUR_SOLICITATION) ||
2206 swkey->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
2207 struct ovs_key_nd *nd_key;
2208
2209 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
2210 if (!nla)
2211 goto nla_put_failure;
2212 nd_key = nla_data(nla);
2213 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
2214 sizeof(nd_key->nd_target));
2215 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
2216 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
2217 }
2218 }
2219 }
2220
2221 unencap:
2222 if (in_encap)
2223 nla_nest_end(skb, in_encap);
2224 if (encap)
2225 nla_nest_end(skb, encap);
2226
2227 return 0;
2228
2229 nla_put_failure:
2230 return -EMSGSIZE;
2231 }
2232
ovs_nla_put_key(const struct sw_flow_key * swkey,const struct sw_flow_key * output,int attr,bool is_mask,struct sk_buff * skb)2233 int ovs_nla_put_key(const struct sw_flow_key *swkey,
2234 const struct sw_flow_key *output, int attr, bool is_mask,
2235 struct sk_buff *skb)
2236 {
2237 int err;
2238 struct nlattr *nla;
2239
2240 nla = nla_nest_start_noflag(skb, attr);
2241 if (!nla)
2242 return -EMSGSIZE;
2243 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
2244 if (err)
2245 return err;
2246 nla_nest_end(skb, nla);
2247
2248 return 0;
2249 }
2250
2251 /* Called with ovs_mutex or RCU read lock. */
ovs_nla_put_identifier(const struct sw_flow * flow,struct sk_buff * skb)2252 int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
2253 {
2254 if (ovs_identifier_is_ufid(&flow->id))
2255 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
2256 flow->id.ufid);
2257
2258 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
2259 OVS_FLOW_ATTR_KEY, false, skb);
2260 }
2261
2262 /* Called with ovs_mutex or RCU read lock. */
ovs_nla_put_masked_key(const struct sw_flow * flow,struct sk_buff * skb)2263 int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
2264 {
2265 return ovs_nla_put_key(&flow->key, &flow->key,
2266 OVS_FLOW_ATTR_KEY, false, skb);
2267 }
2268
2269 /* Called with ovs_mutex or RCU read lock. */
ovs_nla_put_mask(const struct sw_flow * flow,struct sk_buff * skb)2270 int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
2271 {
2272 return ovs_nla_put_key(&flow->key, &flow->mask->key,
2273 OVS_FLOW_ATTR_MASK, true, skb);
2274 }
2275
2276 #define MAX_ACTIONS_BUFSIZE (32 * 1024)
2277
nla_alloc_flow_actions(int size)2278 static struct sw_flow_actions *nla_alloc_flow_actions(int size)
2279 {
2280 struct sw_flow_actions *sfa;
2281
2282 WARN_ON_ONCE(size > MAX_ACTIONS_BUFSIZE);
2283
2284 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
2285 if (!sfa)
2286 return ERR_PTR(-ENOMEM);
2287
2288 sfa->actions_len = 0;
2289 return sfa;
2290 }
2291
2292 static void ovs_nla_free_nested_actions(const struct nlattr *actions, int len);
2293
ovs_nla_free_check_pkt_len_action(const struct nlattr * action)2294 static void ovs_nla_free_check_pkt_len_action(const struct nlattr *action)
2295 {
2296 const struct nlattr *a;
2297 int rem;
2298
2299 nla_for_each_nested(a, action, rem) {
2300 switch (nla_type(a)) {
2301 case OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL:
2302 case OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER:
2303 ovs_nla_free_nested_actions(nla_data(a), nla_len(a));
2304 break;
2305 }
2306 }
2307 }
2308
ovs_nla_free_clone_action(const struct nlattr * action)2309 static void ovs_nla_free_clone_action(const struct nlattr *action)
2310 {
2311 const struct nlattr *a = nla_data(action);
2312 int rem = nla_len(action);
2313
2314 switch (nla_type(a)) {
2315 case OVS_CLONE_ATTR_EXEC:
2316 /* The real list of actions follows this attribute. */
2317 a = nla_next(a, &rem);
2318 ovs_nla_free_nested_actions(a, rem);
2319 break;
2320 }
2321 }
2322
ovs_nla_free_dec_ttl_action(const struct nlattr * action)2323 static void ovs_nla_free_dec_ttl_action(const struct nlattr *action)
2324 {
2325 const struct nlattr *a = nla_data(action);
2326
2327 switch (nla_type(a)) {
2328 case OVS_DEC_TTL_ATTR_ACTION:
2329 ovs_nla_free_nested_actions(nla_data(a), nla_len(a));
2330 break;
2331 }
2332 }
2333
ovs_nla_free_sample_action(const struct nlattr * action)2334 static void ovs_nla_free_sample_action(const struct nlattr *action)
2335 {
2336 const struct nlattr *a = nla_data(action);
2337 int rem = nla_len(action);
2338
2339 switch (nla_type(a)) {
2340 case OVS_SAMPLE_ATTR_ARG:
2341 /* The real list of actions follows this attribute. */
2342 a = nla_next(a, &rem);
2343 ovs_nla_free_nested_actions(a, rem);
2344 break;
2345 }
2346 }
2347
ovs_nla_free_set_action(const struct nlattr * a)2348 static void ovs_nla_free_set_action(const struct nlattr *a)
2349 {
2350 const struct nlattr *ovs_key = nla_data(a);
2351 struct ovs_tunnel_info *ovs_tun;
2352
2353 switch (nla_type(ovs_key)) {
2354 case OVS_KEY_ATTR_TUNNEL_INFO:
2355 ovs_tun = nla_data(ovs_key);
2356 dst_release((struct dst_entry *)ovs_tun->tun_dst);
2357 break;
2358 }
2359 }
2360
ovs_nla_free_nested_actions(const struct nlattr * actions,int len)2361 static void ovs_nla_free_nested_actions(const struct nlattr *actions, int len)
2362 {
2363 const struct nlattr *a;
2364 int rem;
2365
2366 /* Whenever new actions are added, the need to update this
2367 * function should be considered.
2368 */
2369 BUILD_BUG_ON(OVS_ACTION_ATTR_MAX != 23);
2370
2371 if (!actions)
2372 return;
2373
2374 nla_for_each_attr(a, actions, len, rem) {
2375 switch (nla_type(a)) {
2376 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
2377 ovs_nla_free_check_pkt_len_action(a);
2378 break;
2379
2380 case OVS_ACTION_ATTR_CLONE:
2381 ovs_nla_free_clone_action(a);
2382 break;
2383
2384 case OVS_ACTION_ATTR_CT:
2385 ovs_ct_free_action(a);
2386 break;
2387
2388 case OVS_ACTION_ATTR_DEC_TTL:
2389 ovs_nla_free_dec_ttl_action(a);
2390 break;
2391
2392 case OVS_ACTION_ATTR_SAMPLE:
2393 ovs_nla_free_sample_action(a);
2394 break;
2395
2396 case OVS_ACTION_ATTR_SET:
2397 ovs_nla_free_set_action(a);
2398 break;
2399 }
2400 }
2401 }
2402
ovs_nla_free_flow_actions(struct sw_flow_actions * sf_acts)2403 void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
2404 {
2405 if (!sf_acts)
2406 return;
2407
2408 ovs_nla_free_nested_actions(sf_acts->actions, sf_acts->actions_len);
2409 kfree(sf_acts);
2410 }
2411
__ovs_nla_free_flow_actions(struct rcu_head * head)2412 static void __ovs_nla_free_flow_actions(struct rcu_head *head)
2413 {
2414 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
2415 }
2416
2417 /* Schedules 'sf_acts' to be freed after the next RCU grace period.
2418 * The caller must hold rcu_read_lock for this to be sensible. */
ovs_nla_free_flow_actions_rcu(struct sw_flow_actions * sf_acts)2419 void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
2420 {
2421 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
2422 }
2423
reserve_sfa_size(struct sw_flow_actions ** sfa,int attr_len,bool log)2424 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
2425 int attr_len, bool log)
2426 {
2427
2428 struct sw_flow_actions *acts;
2429 int new_acts_size;
2430 size_t req_size = NLA_ALIGN(attr_len);
2431 int next_offset = offsetof(struct sw_flow_actions, actions) +
2432 (*sfa)->actions_len;
2433
2434 if (req_size <= (ksize(*sfa) - next_offset))
2435 goto out;
2436
2437 new_acts_size = max(next_offset + req_size, ksize(*sfa) * 2);
2438
2439 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
2440 if ((next_offset + req_size) > MAX_ACTIONS_BUFSIZE) {
2441 OVS_NLERR(log, "Flow action size exceeds max %u",
2442 MAX_ACTIONS_BUFSIZE);
2443 return ERR_PTR(-EMSGSIZE);
2444 }
2445 new_acts_size = MAX_ACTIONS_BUFSIZE;
2446 }
2447
2448 acts = nla_alloc_flow_actions(new_acts_size);
2449 if (IS_ERR(acts))
2450 return (void *)acts;
2451
2452 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
2453 acts->actions_len = (*sfa)->actions_len;
2454 acts->orig_len = (*sfa)->orig_len;
2455 kfree(*sfa);
2456 *sfa = acts;
2457
2458 out:
2459 (*sfa)->actions_len += req_size;
2460 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
2461 }
2462
__add_action(struct sw_flow_actions ** sfa,int attrtype,void * data,int len,bool log)2463 static struct nlattr *__add_action(struct sw_flow_actions **sfa,
2464 int attrtype, void *data, int len, bool log)
2465 {
2466 struct nlattr *a;
2467
2468 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
2469 if (IS_ERR(a))
2470 return a;
2471
2472 a->nla_type = attrtype;
2473 a->nla_len = nla_attr_size(len);
2474
2475 if (data)
2476 memcpy(nla_data(a), data, len);
2477 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
2478
2479 return a;
2480 }
2481
ovs_nla_add_action(struct sw_flow_actions ** sfa,int attrtype,void * data,int len,bool log)2482 int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
2483 int len, bool log)
2484 {
2485 struct nlattr *a;
2486
2487 a = __add_action(sfa, attrtype, data, len, log);
2488
2489 return PTR_ERR_OR_ZERO(a);
2490 }
2491
add_nested_action_start(struct sw_flow_actions ** sfa,int attrtype,bool log)2492 static inline int add_nested_action_start(struct sw_flow_actions **sfa,
2493 int attrtype, bool log)
2494 {
2495 int used = (*sfa)->actions_len;
2496 int err;
2497
2498 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
2499 if (err)
2500 return err;
2501
2502 return used;
2503 }
2504
add_nested_action_end(struct sw_flow_actions * sfa,int st_offset)2505 static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2506 int st_offset)
2507 {
2508 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2509 st_offset);
2510
2511 a->nla_len = sfa->actions_len - st_offset;
2512 }
2513
2514 static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
2515 const struct sw_flow_key *key,
2516 struct sw_flow_actions **sfa,
2517 __be16 eth_type, __be16 vlan_tci,
2518 u32 mpls_label_count, bool log,
2519 u32 depth);
2520
validate_and_copy_sample(struct net * net,const struct nlattr * attr,const struct sw_flow_key * key,struct sw_flow_actions ** sfa,__be16 eth_type,__be16 vlan_tci,u32 mpls_label_count,bool log,bool last,u32 depth)2521 static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
2522 const struct sw_flow_key *key,
2523 struct sw_flow_actions **sfa,
2524 __be16 eth_type, __be16 vlan_tci,
2525 u32 mpls_label_count, bool log, bool last,
2526 u32 depth)
2527 {
2528 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2529 const struct nlattr *probability, *actions;
2530 const struct nlattr *a;
2531 int rem, start, err;
2532 struct sample_arg arg;
2533
2534 memset(attrs, 0, sizeof(attrs));
2535 nla_for_each_nested(a, attr, rem) {
2536 int type = nla_type(a);
2537 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2538 return -EINVAL;
2539 attrs[type] = a;
2540 }
2541 if (rem)
2542 return -EINVAL;
2543
2544 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2545 if (!probability || nla_len(probability) != sizeof(u32))
2546 return -EINVAL;
2547
2548 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2549 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2550 return -EINVAL;
2551
2552 /* validation done, copy sample action. */
2553 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
2554 if (start < 0)
2555 return start;
2556
2557 /* When both skb and flow may be changed, put the sample
2558 * into a deferred fifo. On the other hand, if only skb
2559 * may be modified, the actions can be executed in place.
2560 *
2561 * Do this analysis at the flow installation time.
2562 * Set 'clone_action->exec' to true if the actions can be
2563 * executed without being deferred.
2564 *
2565 * If the sample is the last action, it can always be excuted
2566 * rather than deferred.
2567 */
2568 arg.exec = last || !actions_may_change_flow(actions);
2569 arg.probability = nla_get_u32(probability);
2570
2571 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
2572 log);
2573 if (err)
2574 return err;
2575
2576 err = __ovs_nla_copy_actions(net, actions, key, sfa,
2577 eth_type, vlan_tci, mpls_label_count, log,
2578 depth + 1);
2579
2580 if (err)
2581 return err;
2582
2583 add_nested_action_end(*sfa, start);
2584
2585 return 0;
2586 }
2587
validate_and_copy_dec_ttl(struct net * net,const struct nlattr * attr,const struct sw_flow_key * key,struct sw_flow_actions ** sfa,__be16 eth_type,__be16 vlan_tci,u32 mpls_label_count,bool log,u32 depth)2588 static int validate_and_copy_dec_ttl(struct net *net,
2589 const struct nlattr *attr,
2590 const struct sw_flow_key *key,
2591 struct sw_flow_actions **sfa,
2592 __be16 eth_type, __be16 vlan_tci,
2593 u32 mpls_label_count, bool log,
2594 u32 depth)
2595 {
2596 const struct nlattr *attrs[OVS_DEC_TTL_ATTR_MAX + 1];
2597 int start, action_start, err, rem;
2598 const struct nlattr *a, *actions;
2599
2600 memset(attrs, 0, sizeof(attrs));
2601 nla_for_each_nested(a, attr, rem) {
2602 int type = nla_type(a);
2603
2604 /* Ignore unknown attributes to be future proof. */
2605 if (type > OVS_DEC_TTL_ATTR_MAX)
2606 continue;
2607
2608 if (!type || attrs[type]) {
2609 OVS_NLERR(log, "Duplicate or invalid key (type %d).",
2610 type);
2611 return -EINVAL;
2612 }
2613
2614 attrs[type] = a;
2615 }
2616
2617 if (rem) {
2618 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
2619 return -EINVAL;
2620 }
2621
2622 actions = attrs[OVS_DEC_TTL_ATTR_ACTION];
2623 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN)) {
2624 OVS_NLERR(log, "Missing valid actions attribute.");
2625 return -EINVAL;
2626 }
2627
2628 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_DEC_TTL, log);
2629 if (start < 0)
2630 return start;
2631
2632 action_start = add_nested_action_start(sfa, OVS_DEC_TTL_ATTR_ACTION, log);
2633 if (action_start < 0)
2634 return action_start;
2635
2636 err = __ovs_nla_copy_actions(net, actions, key, sfa, eth_type,
2637 vlan_tci, mpls_label_count, log,
2638 depth + 1);
2639 if (err)
2640 return err;
2641
2642 add_nested_action_end(*sfa, action_start);
2643 add_nested_action_end(*sfa, start);
2644 return 0;
2645 }
2646
validate_and_copy_clone(struct net * net,const struct nlattr * attr,const struct sw_flow_key * key,struct sw_flow_actions ** sfa,__be16 eth_type,__be16 vlan_tci,u32 mpls_label_count,bool log,bool last,u32 depth)2647 static int validate_and_copy_clone(struct net *net,
2648 const struct nlattr *attr,
2649 const struct sw_flow_key *key,
2650 struct sw_flow_actions **sfa,
2651 __be16 eth_type, __be16 vlan_tci,
2652 u32 mpls_label_count, bool log, bool last,
2653 u32 depth)
2654 {
2655 int start, err;
2656 u32 exec;
2657
2658 if (nla_len(attr) && nla_len(attr) < NLA_HDRLEN)
2659 return -EINVAL;
2660
2661 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CLONE, log);
2662 if (start < 0)
2663 return start;
2664
2665 exec = last || !actions_may_change_flow(attr);
2666
2667 err = ovs_nla_add_action(sfa, OVS_CLONE_ATTR_EXEC, &exec,
2668 sizeof(exec), log);
2669 if (err)
2670 return err;
2671
2672 err = __ovs_nla_copy_actions(net, attr, key, sfa,
2673 eth_type, vlan_tci, mpls_label_count, log,
2674 depth + 1);
2675 if (err)
2676 return err;
2677
2678 add_nested_action_end(*sfa, start);
2679
2680 return 0;
2681 }
2682
ovs_match_init(struct sw_flow_match * match,struct sw_flow_key * key,bool reset_key,struct sw_flow_mask * mask)2683 void ovs_match_init(struct sw_flow_match *match,
2684 struct sw_flow_key *key,
2685 bool reset_key,
2686 struct sw_flow_mask *mask)
2687 {
2688 memset(match, 0, sizeof(*match));
2689 match->key = key;
2690 match->mask = mask;
2691
2692 if (reset_key)
2693 memset(key, 0, sizeof(*key));
2694
2695 if (mask) {
2696 memset(&mask->key, 0, sizeof(mask->key));
2697 mask->range.start = mask->range.end = 0;
2698 }
2699 }
2700
validate_geneve_opts(struct sw_flow_key * key)2701 static int validate_geneve_opts(struct sw_flow_key *key)
2702 {
2703 struct geneve_opt *option;
2704 int opts_len = key->tun_opts_len;
2705 bool crit_opt = false;
2706
2707 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2708 while (opts_len > 0) {
2709 int len;
2710
2711 if (opts_len < sizeof(*option))
2712 return -EINVAL;
2713
2714 len = sizeof(*option) + option->length * 4;
2715 if (len > opts_len)
2716 return -EINVAL;
2717
2718 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2719
2720 option = (struct geneve_opt *)((u8 *)option + len);
2721 opts_len -= len;
2722 }
2723
2724 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2725
2726 return 0;
2727 }
2728
validate_and_copy_set_tun(const struct nlattr * attr,struct sw_flow_actions ** sfa,bool log)2729 static int validate_and_copy_set_tun(const struct nlattr *attr,
2730 struct sw_flow_actions **sfa, bool log)
2731 {
2732 struct sw_flow_match match;
2733 struct sw_flow_key key;
2734 struct metadata_dst *tun_dst;
2735 struct ip_tunnel_info *tun_info;
2736 struct ovs_tunnel_info *ovs_tun;
2737 struct nlattr *a;
2738 int err = 0, start, opts_type;
2739 __be16 dst_opt_type;
2740
2741 dst_opt_type = 0;
2742 ovs_match_init(&match, &key, true, NULL);
2743 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
2744 if (opts_type < 0)
2745 return opts_type;
2746
2747 if (key.tun_opts_len) {
2748 switch (opts_type) {
2749 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2750 err = validate_geneve_opts(&key);
2751 if (err < 0)
2752 return err;
2753 dst_opt_type = TUNNEL_GENEVE_OPT;
2754 break;
2755 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2756 dst_opt_type = TUNNEL_VXLAN_OPT;
2757 break;
2758 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
2759 dst_opt_type = TUNNEL_ERSPAN_OPT;
2760 break;
2761 }
2762 }
2763
2764 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
2765 if (start < 0)
2766 return start;
2767
2768 tun_dst = metadata_dst_alloc(key.tun_opts_len, METADATA_IP_TUNNEL,
2769 GFP_KERNEL);
2770
2771 if (!tun_dst)
2772 return -ENOMEM;
2773
2774 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2775 if (err) {
2776 dst_release((struct dst_entry *)tun_dst);
2777 return err;
2778 }
2779
2780 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
2781 sizeof(*ovs_tun), log);
2782 if (IS_ERR(a)) {
2783 dst_release((struct dst_entry *)tun_dst);
2784 return PTR_ERR(a);
2785 }
2786
2787 ovs_tun = nla_data(a);
2788 ovs_tun->tun_dst = tun_dst;
2789
2790 tun_info = &tun_dst->u.tun_info;
2791 tun_info->mode = IP_TUNNEL_INFO_TX;
2792 if (key.tun_proto == AF_INET6)
2793 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
2794 else if (key.tun_proto == AF_INET && key.tun_key.u.ipv4.dst == 0)
2795 tun_info->mode |= IP_TUNNEL_INFO_BRIDGE;
2796 tun_info->key = key.tun_key;
2797
2798 /* We need to store the options in the action itself since
2799 * everything else will go away after flow setup. We can append
2800 * it to tun_info and then point there.
2801 */
2802 ip_tunnel_info_opts_set(tun_info,
2803 TUN_METADATA_OPTS(&key, key.tun_opts_len),
2804 key.tun_opts_len, dst_opt_type);
2805 add_nested_action_end(*sfa, start);
2806
2807 return err;
2808 }
2809
validate_nsh(const struct nlattr * attr,bool is_mask,bool is_push_nsh,bool log)2810 static bool validate_nsh(const struct nlattr *attr, bool is_mask,
2811 bool is_push_nsh, bool log)
2812 {
2813 struct sw_flow_match match;
2814 struct sw_flow_key key;
2815 int ret = 0;
2816
2817 ovs_match_init(&match, &key, true, NULL);
2818 ret = nsh_key_put_from_nlattr(attr, &match, is_mask,
2819 is_push_nsh, log);
2820 return !ret;
2821 }
2822
2823 /* Return false if there are any non-masked bits set.
2824 * Mask follows data immediately, before any netlink padding.
2825 */
validate_masked(u8 * data,int len)2826 static bool validate_masked(u8 *data, int len)
2827 {
2828 u8 *mask = data + len;
2829
2830 while (len--)
2831 if (*data++ & ~*mask++)
2832 return false;
2833
2834 return true;
2835 }
2836
validate_set(const struct nlattr * a,const struct sw_flow_key * flow_key,struct sw_flow_actions ** sfa,bool * skip_copy,u8 mac_proto,__be16 eth_type,bool masked,bool log)2837 static int validate_set(const struct nlattr *a,
2838 const struct sw_flow_key *flow_key,
2839 struct sw_flow_actions **sfa, bool *skip_copy,
2840 u8 mac_proto, __be16 eth_type, bool masked, bool log)
2841 {
2842 const struct nlattr *ovs_key = nla_data(a);
2843 int key_type = nla_type(ovs_key);
2844 size_t key_len;
2845
2846 /* There can be only one key in a action */
2847 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2848 return -EINVAL;
2849
2850 key_len = nla_len(ovs_key);
2851 if (masked)
2852 key_len /= 2;
2853
2854 if (key_type > OVS_KEY_ATTR_MAX ||
2855 !check_attr_len(key_len, ovs_key_lens[key_type].len))
2856 return -EINVAL;
2857
2858 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2859 return -EINVAL;
2860
2861 switch (key_type) {
2862 case OVS_KEY_ATTR_PRIORITY:
2863 case OVS_KEY_ATTR_SKB_MARK:
2864 case OVS_KEY_ATTR_CT_MARK:
2865 case OVS_KEY_ATTR_CT_LABELS:
2866 break;
2867
2868 case OVS_KEY_ATTR_ETHERNET:
2869 if (mac_proto != MAC_PROTO_ETHERNET)
2870 return -EINVAL;
2871 break;
2872
2873 case OVS_KEY_ATTR_TUNNEL: {
2874 int err;
2875
2876 if (masked)
2877 return -EINVAL; /* Masked tunnel set not supported. */
2878
2879 *skip_copy = true;
2880 err = validate_and_copy_set_tun(a, sfa, log);
2881 if (err)
2882 return err;
2883 break;
2884 }
2885 case OVS_KEY_ATTR_IPV4: {
2886 const struct ovs_key_ipv4 *ipv4_key;
2887
2888 if (eth_type != htons(ETH_P_IP))
2889 return -EINVAL;
2890
2891 ipv4_key = nla_data(ovs_key);
2892
2893 if (masked) {
2894 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
2895
2896 /* Non-writeable fields. */
2897 if (mask->ipv4_proto || mask->ipv4_frag)
2898 return -EINVAL;
2899 } else {
2900 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2901 return -EINVAL;
2902
2903 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2904 return -EINVAL;
2905 }
2906 break;
2907 }
2908 case OVS_KEY_ATTR_IPV6: {
2909 const struct ovs_key_ipv6 *ipv6_key;
2910
2911 if (eth_type != htons(ETH_P_IPV6))
2912 return -EINVAL;
2913
2914 ipv6_key = nla_data(ovs_key);
2915
2916 if (masked) {
2917 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
2918
2919 /* Non-writeable fields. */
2920 if (mask->ipv6_proto || mask->ipv6_frag)
2921 return -EINVAL;
2922
2923 /* Invalid bits in the flow label mask? */
2924 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2925 return -EINVAL;
2926 } else {
2927 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2928 return -EINVAL;
2929
2930 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2931 return -EINVAL;
2932 }
2933 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2934 return -EINVAL;
2935
2936 break;
2937 }
2938 case OVS_KEY_ATTR_TCP:
2939 if ((eth_type != htons(ETH_P_IP) &&
2940 eth_type != htons(ETH_P_IPV6)) ||
2941 flow_key->ip.proto != IPPROTO_TCP)
2942 return -EINVAL;
2943
2944 break;
2945
2946 case OVS_KEY_ATTR_UDP:
2947 if ((eth_type != htons(ETH_P_IP) &&
2948 eth_type != htons(ETH_P_IPV6)) ||
2949 flow_key->ip.proto != IPPROTO_UDP)
2950 return -EINVAL;
2951
2952 break;
2953
2954 case OVS_KEY_ATTR_MPLS:
2955 if (!eth_p_mpls(eth_type))
2956 return -EINVAL;
2957 break;
2958
2959 case OVS_KEY_ATTR_SCTP:
2960 if ((eth_type != htons(ETH_P_IP) &&
2961 eth_type != htons(ETH_P_IPV6)) ||
2962 flow_key->ip.proto != IPPROTO_SCTP)
2963 return -EINVAL;
2964
2965 break;
2966
2967 case OVS_KEY_ATTR_NSH:
2968 if (eth_type != htons(ETH_P_NSH))
2969 return -EINVAL;
2970 if (!validate_nsh(nla_data(a), masked, false, log))
2971 return -EINVAL;
2972 break;
2973
2974 default:
2975 return -EINVAL;
2976 }
2977
2978 /* Convert non-masked non-tunnel set actions to masked set actions. */
2979 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2980 int start, len = key_len * 2;
2981 struct nlattr *at;
2982
2983 *skip_copy = true;
2984
2985 start = add_nested_action_start(sfa,
2986 OVS_ACTION_ATTR_SET_TO_MASKED,
2987 log);
2988 if (start < 0)
2989 return start;
2990
2991 at = __add_action(sfa, key_type, NULL, len, log);
2992 if (IS_ERR(at))
2993 return PTR_ERR(at);
2994
2995 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2996 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2997 /* Clear non-writeable bits from otherwise writeable fields. */
2998 if (key_type == OVS_KEY_ATTR_IPV6) {
2999 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
3000
3001 mask->ipv6_label &= htonl(0x000FFFFF);
3002 }
3003 add_nested_action_end(*sfa, start);
3004 }
3005
3006 return 0;
3007 }
3008
validate_userspace(const struct nlattr * attr)3009 static int validate_userspace(const struct nlattr *attr)
3010 {
3011 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
3012 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
3013 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
3014 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
3015 };
3016 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
3017 int error;
3018
3019 error = nla_parse_nested_deprecated(a, OVS_USERSPACE_ATTR_MAX, attr,
3020 userspace_policy, NULL);
3021 if (error)
3022 return error;
3023
3024 if (!a[OVS_USERSPACE_ATTR_PID] ||
3025 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
3026 return -EINVAL;
3027
3028 return 0;
3029 }
3030
3031 static const struct nla_policy cpl_policy[OVS_CHECK_PKT_LEN_ATTR_MAX + 1] = {
3032 [OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] = {.type = NLA_U16 },
3033 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER] = {.type = NLA_NESTED },
3034 [OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL] = {.type = NLA_NESTED },
3035 };
3036
validate_and_copy_check_pkt_len(struct net * net,const struct nlattr * attr,const struct sw_flow_key * key,struct sw_flow_actions ** sfa,__be16 eth_type,__be16 vlan_tci,u32 mpls_label_count,bool log,bool last,u32 depth)3037 static int validate_and_copy_check_pkt_len(struct net *net,
3038 const struct nlattr *attr,
3039 const struct sw_flow_key *key,
3040 struct sw_flow_actions **sfa,
3041 __be16 eth_type, __be16 vlan_tci,
3042 u32 mpls_label_count,
3043 bool log, bool last, u32 depth)
3044 {
3045 const struct nlattr *acts_if_greater, *acts_if_lesser_eq;
3046 struct nlattr *a[OVS_CHECK_PKT_LEN_ATTR_MAX + 1];
3047 struct check_pkt_len_arg arg;
3048 int nested_acts_start;
3049 int start, err;
3050
3051 err = nla_parse_deprecated_strict(a, OVS_CHECK_PKT_LEN_ATTR_MAX,
3052 nla_data(attr), nla_len(attr),
3053 cpl_policy, NULL);
3054 if (err)
3055 return err;
3056
3057 if (!a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN] ||
3058 !nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]))
3059 return -EINVAL;
3060
3061 acts_if_lesser_eq = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL];
3062 acts_if_greater = a[OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER];
3063
3064 /* Both the nested action should be present. */
3065 if (!acts_if_greater || !acts_if_lesser_eq)
3066 return -EINVAL;
3067
3068 /* validation done, copy the nested actions. */
3069 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_CHECK_PKT_LEN,
3070 log);
3071 if (start < 0)
3072 return start;
3073
3074 arg.pkt_len = nla_get_u16(a[OVS_CHECK_PKT_LEN_ATTR_PKT_LEN]);
3075 arg.exec_for_lesser_equal =
3076 last || !actions_may_change_flow(acts_if_lesser_eq);
3077 arg.exec_for_greater =
3078 last || !actions_may_change_flow(acts_if_greater);
3079
3080 err = ovs_nla_add_action(sfa, OVS_CHECK_PKT_LEN_ATTR_ARG, &arg,
3081 sizeof(arg), log);
3082 if (err)
3083 return err;
3084
3085 nested_acts_start = add_nested_action_start(sfa,
3086 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL, log);
3087 if (nested_acts_start < 0)
3088 return nested_acts_start;
3089
3090 err = __ovs_nla_copy_actions(net, acts_if_lesser_eq, key, sfa,
3091 eth_type, vlan_tci, mpls_label_count, log,
3092 depth + 1);
3093
3094 if (err)
3095 return err;
3096
3097 add_nested_action_end(*sfa, nested_acts_start);
3098
3099 nested_acts_start = add_nested_action_start(sfa,
3100 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER, log);
3101 if (nested_acts_start < 0)
3102 return nested_acts_start;
3103
3104 err = __ovs_nla_copy_actions(net, acts_if_greater, key, sfa,
3105 eth_type, vlan_tci, mpls_label_count, log,
3106 depth + 1);
3107
3108 if (err)
3109 return err;
3110
3111 add_nested_action_end(*sfa, nested_acts_start);
3112 add_nested_action_end(*sfa, start);
3113 return 0;
3114 }
3115
copy_action(const struct nlattr * from,struct sw_flow_actions ** sfa,bool log)3116 static int copy_action(const struct nlattr *from,
3117 struct sw_flow_actions **sfa, bool log)
3118 {
3119 int totlen = NLA_ALIGN(from->nla_len);
3120 struct nlattr *to;
3121
3122 to = reserve_sfa_size(sfa, from->nla_len, log);
3123 if (IS_ERR(to))
3124 return PTR_ERR(to);
3125
3126 memcpy(to, from, totlen);
3127 return 0;
3128 }
3129
__ovs_nla_copy_actions(struct net * net,const struct nlattr * attr,const struct sw_flow_key * key,struct sw_flow_actions ** sfa,__be16 eth_type,__be16 vlan_tci,u32 mpls_label_count,bool log,u32 depth)3130 static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
3131 const struct sw_flow_key *key,
3132 struct sw_flow_actions **sfa,
3133 __be16 eth_type, __be16 vlan_tci,
3134 u32 mpls_label_count, bool log,
3135 u32 depth)
3136 {
3137 u8 mac_proto = ovs_key_mac_proto(key);
3138 const struct nlattr *a;
3139 int rem, err;
3140
3141 if (depth > OVS_COPY_ACTIONS_MAX_DEPTH)
3142 return -EOVERFLOW;
3143
3144 nla_for_each_nested(a, attr, rem) {
3145 /* Expected argument lengths, (u32)-1 for variable length. */
3146 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
3147 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
3148 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
3149 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
3150 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
3151 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
3152 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
3153 [OVS_ACTION_ATTR_POP_VLAN] = 0,
3154 [OVS_ACTION_ATTR_SET] = (u32)-1,
3155 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
3156 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
3157 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
3158 [OVS_ACTION_ATTR_CT] = (u32)-1,
3159 [OVS_ACTION_ATTR_CT_CLEAR] = 0,
3160 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
3161 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
3162 [OVS_ACTION_ATTR_POP_ETH] = 0,
3163 [OVS_ACTION_ATTR_PUSH_NSH] = (u32)-1,
3164 [OVS_ACTION_ATTR_POP_NSH] = 0,
3165 [OVS_ACTION_ATTR_METER] = sizeof(u32),
3166 [OVS_ACTION_ATTR_CLONE] = (u32)-1,
3167 [OVS_ACTION_ATTR_CHECK_PKT_LEN] = (u32)-1,
3168 [OVS_ACTION_ATTR_ADD_MPLS] = sizeof(struct ovs_action_add_mpls),
3169 [OVS_ACTION_ATTR_DEC_TTL] = (u32)-1,
3170 };
3171 const struct ovs_action_push_vlan *vlan;
3172 int type = nla_type(a);
3173 bool skip_copy;
3174
3175 if (type > OVS_ACTION_ATTR_MAX ||
3176 (action_lens[type] != nla_len(a) &&
3177 action_lens[type] != (u32)-1))
3178 return -EINVAL;
3179
3180 skip_copy = false;
3181 switch (type) {
3182 case OVS_ACTION_ATTR_UNSPEC:
3183 return -EINVAL;
3184
3185 case OVS_ACTION_ATTR_USERSPACE:
3186 err = validate_userspace(a);
3187 if (err)
3188 return err;
3189 break;
3190
3191 case OVS_ACTION_ATTR_OUTPUT:
3192 if (nla_get_u32(a) >= DP_MAX_PORTS)
3193 return -EINVAL;
3194 break;
3195
3196 case OVS_ACTION_ATTR_TRUNC: {
3197 const struct ovs_action_trunc *trunc = nla_data(a);
3198
3199 if (trunc->max_len < ETH_HLEN)
3200 return -EINVAL;
3201 break;
3202 }
3203
3204 case OVS_ACTION_ATTR_HASH: {
3205 const struct ovs_action_hash *act_hash = nla_data(a);
3206
3207 switch (act_hash->hash_alg) {
3208 case OVS_HASH_ALG_L4:
3209 break;
3210 default:
3211 return -EINVAL;
3212 }
3213
3214 break;
3215 }
3216
3217 case OVS_ACTION_ATTR_POP_VLAN:
3218 if (mac_proto != MAC_PROTO_ETHERNET)
3219 return -EINVAL;
3220 vlan_tci = htons(0);
3221 break;
3222
3223 case OVS_ACTION_ATTR_PUSH_VLAN:
3224 if (mac_proto != MAC_PROTO_ETHERNET)
3225 return -EINVAL;
3226 vlan = nla_data(a);
3227 if (!eth_type_vlan(vlan->vlan_tpid))
3228 return -EINVAL;
3229 if (!(vlan->vlan_tci & htons(VLAN_CFI_MASK)))
3230 return -EINVAL;
3231 vlan_tci = vlan->vlan_tci;
3232 break;
3233
3234 case OVS_ACTION_ATTR_RECIRC:
3235 break;
3236
3237 case OVS_ACTION_ATTR_ADD_MPLS: {
3238 const struct ovs_action_add_mpls *mpls = nla_data(a);
3239
3240 if (!eth_p_mpls(mpls->mpls_ethertype))
3241 return -EINVAL;
3242
3243 if (mpls->tun_flags & OVS_MPLS_L3_TUNNEL_FLAG_MASK) {
3244 if (vlan_tci & htons(VLAN_CFI_MASK) ||
3245 (eth_type != htons(ETH_P_IP) &&
3246 eth_type != htons(ETH_P_IPV6) &&
3247 eth_type != htons(ETH_P_ARP) &&
3248 eth_type != htons(ETH_P_RARP) &&
3249 !eth_p_mpls(eth_type)))
3250 return -EINVAL;
3251 mpls_label_count++;
3252 } else {
3253 if (mac_proto == MAC_PROTO_ETHERNET) {
3254 mpls_label_count = 1;
3255 mac_proto = MAC_PROTO_NONE;
3256 } else {
3257 mpls_label_count++;
3258 }
3259 }
3260 eth_type = mpls->mpls_ethertype;
3261 break;
3262 }
3263
3264 case OVS_ACTION_ATTR_PUSH_MPLS: {
3265 const struct ovs_action_push_mpls *mpls = nla_data(a);
3266
3267 if (!eth_p_mpls(mpls->mpls_ethertype))
3268 return -EINVAL;
3269 /* Prohibit push MPLS other than to a white list
3270 * for packets that have a known tag order.
3271 */
3272 if (vlan_tci & htons(VLAN_CFI_MASK) ||
3273 (eth_type != htons(ETH_P_IP) &&
3274 eth_type != htons(ETH_P_IPV6) &&
3275 eth_type != htons(ETH_P_ARP) &&
3276 eth_type != htons(ETH_P_RARP) &&
3277 !eth_p_mpls(eth_type)))
3278 return -EINVAL;
3279 eth_type = mpls->mpls_ethertype;
3280 mpls_label_count++;
3281 break;
3282 }
3283
3284 case OVS_ACTION_ATTR_POP_MPLS: {
3285 __be16 proto;
3286 if (vlan_tci & htons(VLAN_CFI_MASK) ||
3287 !eth_p_mpls(eth_type))
3288 return -EINVAL;
3289
3290 /* Disallow subsequent L2.5+ set actions and mpls_pop
3291 * actions once the last MPLS label in the packet is
3292 * is popped as there is no check here to ensure that
3293 * the new eth type is valid and thus set actions could
3294 * write off the end of the packet or otherwise corrupt
3295 * it.
3296 *
3297 * Support for these actions is planned using packet
3298 * recirculation.
3299 */
3300 proto = nla_get_be16(a);
3301
3302 if (proto == htons(ETH_P_TEB) &&
3303 mac_proto != MAC_PROTO_NONE)
3304 return -EINVAL;
3305
3306 mpls_label_count--;
3307
3308 if (!eth_p_mpls(proto) || !mpls_label_count)
3309 eth_type = htons(0);
3310 else
3311 eth_type = proto;
3312
3313 break;
3314 }
3315
3316 case OVS_ACTION_ATTR_SET:
3317 err = validate_set(a, key, sfa,
3318 &skip_copy, mac_proto, eth_type,
3319 false, log);
3320 if (err)
3321 return err;
3322 break;
3323
3324 case OVS_ACTION_ATTR_SET_MASKED:
3325 err = validate_set(a, key, sfa,
3326 &skip_copy, mac_proto, eth_type,
3327 true, log);
3328 if (err)
3329 return err;
3330 break;
3331
3332 case OVS_ACTION_ATTR_SAMPLE: {
3333 bool last = nla_is_last(a, rem);
3334
3335 err = validate_and_copy_sample(net, a, key, sfa,
3336 eth_type, vlan_tci,
3337 mpls_label_count,
3338 log, last, depth);
3339 if (err)
3340 return err;
3341 skip_copy = true;
3342 break;
3343 }
3344
3345 case OVS_ACTION_ATTR_CT:
3346 err = ovs_ct_copy_action(net, a, key, sfa, log);
3347 if (err)
3348 return err;
3349 skip_copy = true;
3350 break;
3351
3352 case OVS_ACTION_ATTR_CT_CLEAR:
3353 break;
3354
3355 case OVS_ACTION_ATTR_PUSH_ETH:
3356 /* Disallow pushing an Ethernet header if one
3357 * is already present */
3358 if (mac_proto != MAC_PROTO_NONE)
3359 return -EINVAL;
3360 mac_proto = MAC_PROTO_ETHERNET;
3361 break;
3362
3363 case OVS_ACTION_ATTR_POP_ETH:
3364 if (mac_proto != MAC_PROTO_ETHERNET)
3365 return -EINVAL;
3366 if (vlan_tci & htons(VLAN_CFI_MASK))
3367 return -EINVAL;
3368 mac_proto = MAC_PROTO_NONE;
3369 break;
3370
3371 case OVS_ACTION_ATTR_PUSH_NSH:
3372 if (mac_proto != MAC_PROTO_ETHERNET) {
3373 u8 next_proto;
3374
3375 next_proto = tun_p_from_eth_p(eth_type);
3376 if (!next_proto)
3377 return -EINVAL;
3378 }
3379 mac_proto = MAC_PROTO_NONE;
3380 if (!validate_nsh(nla_data(a), false, true, true))
3381 return -EINVAL;
3382 break;
3383
3384 case OVS_ACTION_ATTR_POP_NSH: {
3385 __be16 inner_proto;
3386
3387 if (eth_type != htons(ETH_P_NSH))
3388 return -EINVAL;
3389 inner_proto = tun_p_to_eth_p(key->nsh.base.np);
3390 if (!inner_proto)
3391 return -EINVAL;
3392 if (key->nsh.base.np == TUN_P_ETHERNET)
3393 mac_proto = MAC_PROTO_ETHERNET;
3394 else
3395 mac_proto = MAC_PROTO_NONE;
3396 break;
3397 }
3398
3399 case OVS_ACTION_ATTR_METER:
3400 /* Non-existent meters are simply ignored. */
3401 break;
3402
3403 case OVS_ACTION_ATTR_CLONE: {
3404 bool last = nla_is_last(a, rem);
3405
3406 err = validate_and_copy_clone(net, a, key, sfa,
3407 eth_type, vlan_tci,
3408 mpls_label_count,
3409 log, last, depth);
3410 if (err)
3411 return err;
3412 skip_copy = true;
3413 break;
3414 }
3415
3416 case OVS_ACTION_ATTR_CHECK_PKT_LEN: {
3417 bool last = nla_is_last(a, rem);
3418
3419 err = validate_and_copy_check_pkt_len(net, a, key, sfa,
3420 eth_type,
3421 vlan_tci,
3422 mpls_label_count,
3423 log, last,
3424 depth);
3425 if (err)
3426 return err;
3427 skip_copy = true;
3428 break;
3429 }
3430
3431 case OVS_ACTION_ATTR_DEC_TTL:
3432 err = validate_and_copy_dec_ttl(net, a, key, sfa,
3433 eth_type, vlan_tci,
3434 mpls_label_count, log,
3435 depth);
3436 if (err)
3437 return err;
3438 skip_copy = true;
3439 break;
3440
3441 default:
3442 OVS_NLERR(log, "Unknown Action type %d", type);
3443 return -EINVAL;
3444 }
3445 if (!skip_copy) {
3446 err = copy_action(a, sfa, log);
3447 if (err)
3448 return err;
3449 }
3450 }
3451
3452 if (rem > 0)
3453 return -EINVAL;
3454
3455 return 0;
3456 }
3457
3458 /* 'key' must be the masked key. */
ovs_nla_copy_actions(struct net * net,const struct nlattr * attr,const struct sw_flow_key * key,struct sw_flow_actions ** sfa,bool log)3459 int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
3460 const struct sw_flow_key *key,
3461 struct sw_flow_actions **sfa, bool log)
3462 {
3463 int err;
3464 u32 mpls_label_count = 0;
3465
3466 *sfa = nla_alloc_flow_actions(min(nla_len(attr), MAX_ACTIONS_BUFSIZE));
3467 if (IS_ERR(*sfa))
3468 return PTR_ERR(*sfa);
3469
3470 if (eth_p_mpls(key->eth.type))
3471 mpls_label_count = hweight_long(key->mpls.num_labels_mask);
3472
3473 (*sfa)->orig_len = nla_len(attr);
3474 err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
3475 key->eth.vlan.tci, mpls_label_count, log,
3476 0);
3477 if (err)
3478 ovs_nla_free_flow_actions(*sfa);
3479
3480 return err;
3481 }
3482
sample_action_to_attr(const struct nlattr * attr,struct sk_buff * skb)3483 static int sample_action_to_attr(const struct nlattr *attr,
3484 struct sk_buff *skb)
3485 {
3486 struct nlattr *start, *ac_start = NULL, *sample_arg;
3487 int err = 0, rem = nla_len(attr);
3488 const struct sample_arg *arg;
3489 struct nlattr *actions;
3490
3491 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_SAMPLE);
3492 if (!start)
3493 return -EMSGSIZE;
3494
3495 sample_arg = nla_data(attr);
3496 arg = nla_data(sample_arg);
3497 actions = nla_next(sample_arg, &rem);
3498
3499 if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
3500 err = -EMSGSIZE;
3501 goto out;
3502 }
3503
3504 ac_start = nla_nest_start_noflag(skb, OVS_SAMPLE_ATTR_ACTIONS);
3505 if (!ac_start) {
3506 err = -EMSGSIZE;
3507 goto out;
3508 }
3509
3510 err = ovs_nla_put_actions(actions, rem, skb);
3511
3512 out:
3513 if (err) {
3514 nla_nest_cancel(skb, ac_start);
3515 nla_nest_cancel(skb, start);
3516 } else {
3517 nla_nest_end(skb, ac_start);
3518 nla_nest_end(skb, start);
3519 }
3520
3521 return err;
3522 }
3523
clone_action_to_attr(const struct nlattr * attr,struct sk_buff * skb)3524 static int clone_action_to_attr(const struct nlattr *attr,
3525 struct sk_buff *skb)
3526 {
3527 struct nlattr *start;
3528 int err = 0, rem = nla_len(attr);
3529
3530 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CLONE);
3531 if (!start)
3532 return -EMSGSIZE;
3533
3534 /* Skipping the OVS_CLONE_ATTR_EXEC that is always the first attribute. */
3535 attr = nla_next(nla_data(attr), &rem);
3536 err = ovs_nla_put_actions(attr, rem, skb);
3537
3538 if (err)
3539 nla_nest_cancel(skb, start);
3540 else
3541 nla_nest_end(skb, start);
3542
3543 return err;
3544 }
3545
check_pkt_len_action_to_attr(const struct nlattr * attr,struct sk_buff * skb)3546 static int check_pkt_len_action_to_attr(const struct nlattr *attr,
3547 struct sk_buff *skb)
3548 {
3549 struct nlattr *start, *ac_start = NULL;
3550 const struct check_pkt_len_arg *arg;
3551 const struct nlattr *a, *cpl_arg;
3552 int err = 0, rem = nla_len(attr);
3553
3554 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_CHECK_PKT_LEN);
3555 if (!start)
3556 return -EMSGSIZE;
3557
3558 /* The first nested attribute in 'attr' is always
3559 * 'OVS_CHECK_PKT_LEN_ATTR_ARG'.
3560 */
3561 cpl_arg = nla_data(attr);
3562 arg = nla_data(cpl_arg);
3563
3564 if (nla_put_u16(skb, OVS_CHECK_PKT_LEN_ATTR_PKT_LEN, arg->pkt_len)) {
3565 err = -EMSGSIZE;
3566 goto out;
3567 }
3568
3569 /* Second nested attribute in 'attr' is always
3570 * 'OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL'.
3571 */
3572 a = nla_next(cpl_arg, &rem);
3573 ac_start = nla_nest_start_noflag(skb,
3574 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_LESS_EQUAL);
3575 if (!ac_start) {
3576 err = -EMSGSIZE;
3577 goto out;
3578 }
3579
3580 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
3581 if (err) {
3582 nla_nest_cancel(skb, ac_start);
3583 goto out;
3584 } else {
3585 nla_nest_end(skb, ac_start);
3586 }
3587
3588 /* Third nested attribute in 'attr' is always
3589 * OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER.
3590 */
3591 a = nla_next(a, &rem);
3592 ac_start = nla_nest_start_noflag(skb,
3593 OVS_CHECK_PKT_LEN_ATTR_ACTIONS_IF_GREATER);
3594 if (!ac_start) {
3595 err = -EMSGSIZE;
3596 goto out;
3597 }
3598
3599 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
3600 if (err) {
3601 nla_nest_cancel(skb, ac_start);
3602 goto out;
3603 } else {
3604 nla_nest_end(skb, ac_start);
3605 }
3606
3607 nla_nest_end(skb, start);
3608 return 0;
3609
3610 out:
3611 nla_nest_cancel(skb, start);
3612 return err;
3613 }
3614
dec_ttl_action_to_attr(const struct nlattr * attr,struct sk_buff * skb)3615 static int dec_ttl_action_to_attr(const struct nlattr *attr,
3616 struct sk_buff *skb)
3617 {
3618 struct nlattr *start, *action_start;
3619 const struct nlattr *a;
3620 int err = 0, rem;
3621
3622 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_DEC_TTL);
3623 if (!start)
3624 return -EMSGSIZE;
3625
3626 nla_for_each_attr(a, nla_data(attr), nla_len(attr), rem) {
3627 switch (nla_type(a)) {
3628 case OVS_DEC_TTL_ATTR_ACTION:
3629
3630 action_start = nla_nest_start_noflag(skb, OVS_DEC_TTL_ATTR_ACTION);
3631 if (!action_start) {
3632 err = -EMSGSIZE;
3633 goto out;
3634 }
3635
3636 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
3637 if (err)
3638 goto out;
3639
3640 nla_nest_end(skb, action_start);
3641 break;
3642
3643 default:
3644 /* Ignore all other option to be future compatible */
3645 break;
3646 }
3647 }
3648
3649 nla_nest_end(skb, start);
3650 return 0;
3651
3652 out:
3653 nla_nest_cancel(skb, start);
3654 return err;
3655 }
3656
set_action_to_attr(const struct nlattr * a,struct sk_buff * skb)3657 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
3658 {
3659 const struct nlattr *ovs_key = nla_data(a);
3660 int key_type = nla_type(ovs_key);
3661 struct nlattr *start;
3662 int err;
3663
3664 switch (key_type) {
3665 case OVS_KEY_ATTR_TUNNEL_INFO: {
3666 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
3667 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
3668
3669 start = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_SET);
3670 if (!start)
3671 return -EMSGSIZE;
3672
3673 err = ip_tun_to_nlattr(skb, &tun_info->key,
3674 ip_tunnel_info_opts(tun_info),
3675 tun_info->options_len,
3676 ip_tunnel_info_af(tun_info), tun_info->mode);
3677 if (err)
3678 return err;
3679 nla_nest_end(skb, start);
3680 break;
3681 }
3682 default:
3683 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
3684 return -EMSGSIZE;
3685 break;
3686 }
3687
3688 return 0;
3689 }
3690
masked_set_action_to_set_action_attr(const struct nlattr * a,struct sk_buff * skb)3691 static int masked_set_action_to_set_action_attr(const struct nlattr *a,
3692 struct sk_buff *skb)
3693 {
3694 const struct nlattr *ovs_key = nla_data(a);
3695 struct nlattr *nla;
3696 size_t key_len = nla_len(ovs_key) / 2;
3697
3698 /* Revert the conversion we did from a non-masked set action to
3699 * masked set action.
3700 */
3701 nla = nla_nest_start_noflag(skb, OVS_ACTION_ATTR_SET);
3702 if (!nla)
3703 return -EMSGSIZE;
3704
3705 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
3706 return -EMSGSIZE;
3707
3708 nla_nest_end(skb, nla);
3709 return 0;
3710 }
3711
ovs_nla_put_actions(const struct nlattr * attr,int len,struct sk_buff * skb)3712 int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
3713 {
3714 const struct nlattr *a;
3715 int rem, err;
3716
3717 nla_for_each_attr(a, attr, len, rem) {
3718 int type = nla_type(a);
3719
3720 switch (type) {
3721 case OVS_ACTION_ATTR_SET:
3722 err = set_action_to_attr(a, skb);
3723 if (err)
3724 return err;
3725 break;
3726
3727 case OVS_ACTION_ATTR_SET_TO_MASKED:
3728 err = masked_set_action_to_set_action_attr(a, skb);
3729 if (err)
3730 return err;
3731 break;
3732
3733 case OVS_ACTION_ATTR_SAMPLE:
3734 err = sample_action_to_attr(a, skb);
3735 if (err)
3736 return err;
3737 break;
3738
3739 case OVS_ACTION_ATTR_CT:
3740 err = ovs_ct_action_to_attr(nla_data(a), skb);
3741 if (err)
3742 return err;
3743 break;
3744
3745 case OVS_ACTION_ATTR_CLONE:
3746 err = clone_action_to_attr(a, skb);
3747 if (err)
3748 return err;
3749 break;
3750
3751 case OVS_ACTION_ATTR_CHECK_PKT_LEN:
3752 err = check_pkt_len_action_to_attr(a, skb);
3753 if (err)
3754 return err;
3755 break;
3756
3757 case OVS_ACTION_ATTR_DEC_TTL:
3758 err = dec_ttl_action_to_attr(a, skb);
3759 if (err)
3760 return err;
3761 break;
3762
3763 default:
3764 if (nla_put(skb, type, nla_len(a), nla_data(a)))
3765 return -EMSGSIZE;
3766 break;
3767 }
3768 }
3769
3770 return 0;
3771 }
3772