blob: fc0ca9a89b8e6d147b02dca0701d93b087e05d80 [file] [log] [blame]
Pravin B Shelare6445712013-10-03 18:16:47 -07001/*
andy zhou798c1662017-03-20 16:32:29 -07002 * Copyright (c) 2007-2017 Nicira, Inc.
Pravin B Shelare6445712013-10-03 18:16:47 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
Joe Perches2235ad12014-02-03 17:18:21 -080019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Pravin B Shelare6445712013-10-03 18:16:47 -070021#include "flow.h"
22#include "datapath.h"
23#include <linux/uaccess.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
28#include <net/llc_pdu.h>
29#include <linux/kernel.h>
30#include <linux/jhash.h>
31#include <linux/jiffies.h>
32#include <linux/llc.h>
33#include <linux/module.h>
34#include <linux/in.h>
35#include <linux/rcupdate.h>
36#include <linux/if_arp.h>
37#include <linux/ip.h>
38#include <linux/ipv6.h>
39#include <linux/sctp.h>
40#include <linux/tcp.h>
41#include <linux/udp.h>
42#include <linux/icmp.h>
43#include <linux/icmpv6.h>
44#include <linux/rculist.h>
Jesse Grossf5796682014-10-03 15:35:33 -070045#include <net/geneve.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070046#include <net/ip.h>
47#include <net/ipv6.h>
48#include <net/ndisc.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -070049#include <net/mpls.h>
Thomas Graf614732e2015-07-21 10:44:06 +020050#include <net/vxlan.h>
William Tuceaa0012017-10-04 17:03:12 -070051#include <net/erspan.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070052
53#include "flow_netlink.h"
54
Thomas Graf81bfe3c2015-01-15 03:53:58 +010055struct ovs_len_tbl {
56 int len;
57 const struct ovs_len_tbl *next;
58};
59
60#define OVS_ATTR_NESTED -1
Jesse Gross982b5272015-09-11 18:38:28 -070061#define OVS_ATTR_VARIABLE -2
Thomas Graf81bfe3c2015-01-15 03:53:58 +010062
andy zhou798c1662017-03-20 16:32:29 -070063static bool actions_may_change_flow(const struct nlattr *actions)
64{
65 struct nlattr *nla;
66 int rem;
67
68 nla_for_each_nested(nla, actions, rem) {
69 u16 action = nla_type(nla);
70
71 switch (action) {
72 case OVS_ACTION_ATTR_OUTPUT:
73 case OVS_ACTION_ATTR_RECIRC:
74 case OVS_ACTION_ATTR_TRUNC:
75 case OVS_ACTION_ATTR_USERSPACE:
76 break;
77
78 case OVS_ACTION_ATTR_CT:
79 case OVS_ACTION_ATTR_HASH:
80 case OVS_ACTION_ATTR_POP_ETH:
81 case OVS_ACTION_ATTR_POP_MPLS:
82 case OVS_ACTION_ATTR_POP_VLAN:
83 case OVS_ACTION_ATTR_PUSH_ETH:
84 case OVS_ACTION_ATTR_PUSH_MPLS:
85 case OVS_ACTION_ATTR_PUSH_VLAN:
86 case OVS_ACTION_ATTR_SAMPLE:
87 case OVS_ACTION_ATTR_SET:
88 case OVS_ACTION_ATTR_SET_MASKED:
89 default:
90 return true;
91 }
92 }
93 return false;
94}
95
Pravin B Shelara85311b2014-10-19 12:03:40 -070096static void update_range(struct sw_flow_match *match,
97 size_t offset, size_t size, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -070098{
Pravin B Shelara85311b2014-10-19 12:03:40 -070099 struct sw_flow_key_range *range;
Pravin B Shelare6445712013-10-03 18:16:47 -0700100 size_t start = rounddown(offset, sizeof(long));
101 size_t end = roundup(offset + size, sizeof(long));
102
103 if (!is_mask)
104 range = &match->range;
Pravin B Shelara85311b2014-10-19 12:03:40 -0700105 else
Pravin B Shelare6445712013-10-03 18:16:47 -0700106 range = &match->mask->range;
107
Pravin B Shelare6445712013-10-03 18:16:47 -0700108 if (range->start == range->end) {
109 range->start = start;
110 range->end = end;
111 return;
112 }
113
114 if (range->start > start)
115 range->start = start;
116
117 if (range->end < end)
118 range->end = end;
119}
120
121#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
122 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700123 update_range(match, offsetof(struct sw_flow_key, field), \
124 sizeof((match)->key->field), is_mask); \
125 if (is_mask) \
126 (match)->mask->key.field = value; \
127 else \
Pravin B Shelare6445712013-10-03 18:16:47 -0700128 (match)->key->field = value; \
Pravin B Shelare6445712013-10-03 18:16:47 -0700129 } while (0)
130
Jesse Grossf5796682014-10-03 15:35:33 -0700131#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
132 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700133 update_range(match, offset, len, is_mask); \
Jesse Grossf5796682014-10-03 15:35:33 -0700134 if (is_mask) \
135 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700136 len); \
Jesse Grossf5796682014-10-03 15:35:33 -0700137 else \
138 memcpy((u8 *)(match)->key + offset, value_p, len); \
Pravin B Shelare6445712013-10-03 18:16:47 -0700139 } while (0)
140
Jesse Grossf5796682014-10-03 15:35:33 -0700141#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
142 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
143 value_p, len, is_mask)
144
Pravin B Shelara85311b2014-10-19 12:03:40 -0700145#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
146 do { \
147 update_range(match, offsetof(struct sw_flow_key, field), \
148 sizeof((match)->key->field), is_mask); \
149 if (is_mask) \
150 memset((u8 *)&(match)->mask->key.field, value, \
151 sizeof((match)->mask->key.field)); \
152 else \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700153 memset((u8 *)&(match)->key->field, value, \
154 sizeof((match)->key->field)); \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700155 } while (0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700156
157static bool match_validate(const struct sw_flow_match *match,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800158 u64 key_attrs, u64 mask_attrs, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700159{
Jiri Benc0a6410f2016-11-10 16:28:22 +0100160 u64 key_expected = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700161 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
162
163 /* The following mask attributes allowed only if they
164 * pass the validation tests. */
165 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800166 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
Pravin B Shelare6445712013-10-03 18:16:47 -0700167 | (1 << OVS_KEY_ATTR_IPV6)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800168 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
Pravin B Shelare6445712013-10-03 18:16:47 -0700169 | (1 << OVS_KEY_ATTR_TCP)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700170 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700171 | (1 << OVS_KEY_ATTR_UDP)
172 | (1 << OVS_KEY_ATTR_SCTP)
173 | (1 << OVS_KEY_ATTR_ICMP)
174 | (1 << OVS_KEY_ATTR_ICMPV6)
175 | (1 << OVS_KEY_ATTR_ARP)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700176 | (1 << OVS_KEY_ATTR_ND)
177 | (1 << OVS_KEY_ATTR_MPLS));
Pravin B Shelare6445712013-10-03 18:16:47 -0700178
179 /* Always allowed mask fields. */
180 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
181 | (1 << OVS_KEY_ATTR_IN_PORT)
182 | (1 << OVS_KEY_ATTR_ETHERTYPE));
183
184 /* Check key attributes. */
185 if (match->key->eth.type == htons(ETH_P_ARP)
186 || match->key->eth.type == htons(ETH_P_RARP)) {
187 key_expected |= 1 << OVS_KEY_ATTR_ARP;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800188 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700189 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
190 }
191
Simon Horman25cd9ba2014-10-06 05:05:13 -0700192 if (eth_p_mpls(match->key->eth.type)) {
193 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
194 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
195 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
196 }
197
Pravin B Shelare6445712013-10-03 18:16:47 -0700198 if (match->key->eth.type == htons(ETH_P_IP)) {
199 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800200 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700201 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800202 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
203 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700204
205 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
206 if (match->key->ip.proto == IPPROTO_UDP) {
207 key_expected |= 1 << OVS_KEY_ATTR_UDP;
208 if (match->mask && (match->mask->key.ip.proto == 0xff))
209 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
210 }
211
212 if (match->key->ip.proto == IPPROTO_SCTP) {
213 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
214 if (match->mask && (match->mask->key.ip.proto == 0xff))
215 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
216 }
217
218 if (match->key->ip.proto == IPPROTO_TCP) {
219 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700220 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
221 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700222 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700223 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
224 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700225 }
226
227 if (match->key->ip.proto == IPPROTO_ICMP) {
228 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
229 if (match->mask && (match->mask->key.ip.proto == 0xff))
230 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
231 }
232 }
233 }
234
235 if (match->key->eth.type == htons(ETH_P_IPV6)) {
236 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800237 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700238 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800239 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
240 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700241
242 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
243 if (match->key->ip.proto == IPPROTO_UDP) {
244 key_expected |= 1 << OVS_KEY_ATTR_UDP;
245 if (match->mask && (match->mask->key.ip.proto == 0xff))
246 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
247 }
248
249 if (match->key->ip.proto == IPPROTO_SCTP) {
250 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
251 if (match->mask && (match->mask->key.ip.proto == 0xff))
252 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
253 }
254
255 if (match->key->ip.proto == IPPROTO_TCP) {
256 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700257 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
258 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700259 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700260 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
261 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700262 }
263
264 if (match->key->ip.proto == IPPROTO_ICMPV6) {
265 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
266 if (match->mask && (match->mask->key.ip.proto == 0xff))
267 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
268
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700269 if (match->key->tp.src ==
Pravin B Shelare6445712013-10-03 18:16:47 -0700270 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700271 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700272 key_expected |= 1 << OVS_KEY_ATTR_ND;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800273 /* Original direction conntrack tuple
274 * uses the same space as the ND fields
275 * in the key, so both are not allowed
276 * at the same time.
277 */
278 mask_allowed &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800279 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700280 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
281 }
282 }
283 }
284 }
285
286 if ((key_attrs & key_expected) != key_expected) {
287 /* Key attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800288 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
289 (unsigned long long)key_attrs,
290 (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700291 return false;
292 }
293
294 if ((mask_attrs & mask_allowed) != mask_attrs) {
295 /* Mask attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800296 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
297 (unsigned long long)mask_attrs,
298 (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700299 return false;
300 }
301
302 return true;
303}
304
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800305size_t ovs_tun_key_attr_size(void)
306{
307 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
308 * updating this function.
309 */
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200310 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
Jiri Benc6b26ba32015-10-05 13:09:47 +0200311 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
312 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800313 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
314 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
315 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
316 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
317 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
318 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
Thomas Graf1dd144c2015-01-15 03:53:59 +0100319 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
320 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
321 */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800322 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
William Tuceaa0012017-10-04 17:03:12 -0700323 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_DST */
324 + nla_total_size(4); /* OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800325}
326
Joe Stringer41af73e2014-10-18 16:14:14 -0700327size_t ovs_key_attr_size(void)
328{
329 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
330 * updating this function.
331 */
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800332 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 28);
Joe Stringer41af73e2014-10-18 16:14:14 -0700333
334 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
335 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800336 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700337 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
338 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
339 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
340 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
Joe Stringerfbccce52015-10-06 11:00:00 -0700341 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700342 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
Joe Stringer182e3042015-08-26 11:31:49 -0700343 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
Joe Stringer33db4122015-10-01 15:00:37 -0700344 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800345 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
Joe Stringer41af73e2014-10-18 16:14:14 -0700346 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
347 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
348 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
349 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
350 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
351 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
352 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
353 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
354}
355
Jesse Gross982b5272015-09-11 18:38:28 -0700356static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
357 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
358};
359
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100360static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
361 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
362 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
363 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
364 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
365 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
366 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
367 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
368 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
369 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
370 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
Jesse Gross982b5272015-09-11 18:38:28 -0700371 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
372 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
373 .next = ovs_vxlan_ext_key_lens },
Jiri Benc6b26ba32015-10-05 13:09:47 +0200374 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
375 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
William Tuceaa0012017-10-04 17:03:12 -0700376 [OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS] = { .len = sizeof(u32) },
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100377};
378
Pravin B Shelare6445712013-10-03 18:16:47 -0700379/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100380static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
381 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
382 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
383 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
384 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
385 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
386 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
387 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
388 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
389 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
390 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
391 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
392 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
393 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
394 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
395 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
396 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
397 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
398 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
399 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
400 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
401 .next = ovs_tunnel_key_lens, },
402 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
Joe Stringerfbccce52015-10-06 11:00:00 -0700403 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700404 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700405 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
Joe Stringer33db4122015-10-01 15:00:37 -0700406 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800407 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
408 .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
409 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
410 .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
Pravin B Shelare6445712013-10-03 18:16:47 -0700411};
412
Jesse Gross982b5272015-09-11 18:38:28 -0700413static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
414{
415 return expected_len == attr_len ||
416 expected_len == OVS_ATTR_NESTED ||
417 expected_len == OVS_ATTR_VARIABLE;
418}
419
Pravin B Shelare6445712013-10-03 18:16:47 -0700420static bool is_all_zero(const u8 *fp, size_t size)
421{
422 int i;
423
424 if (!fp)
425 return false;
426
427 for (i = 0; i < size; i++)
428 if (fp[i])
429 return false;
430
431 return true;
432}
433
434static int __parse_flow_nlattrs(const struct nlattr *attr,
435 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800436 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700437{
438 const struct nlattr *nla;
439 u64 attrs;
440 int rem;
441
442 attrs = *attrsp;
443 nla_for_each_nested(nla, attr, rem) {
444 u16 type = nla_type(nla);
445 int expected_len;
446
447 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800448 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700449 type, OVS_KEY_ATTR_MAX);
450 return -EINVAL;
451 }
452
453 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800454 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700455 return -EINVAL;
456 }
457
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100458 expected_len = ovs_key_lens[type].len;
Jesse Gross982b5272015-09-11 18:38:28 -0700459 if (!check_attr_len(nla_len(nla), expected_len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800460 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
461 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700462 return -EINVAL;
463 }
464
465 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
466 attrs |= 1 << type;
467 a[type] = nla;
468 }
469 }
470 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800471 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700472 return -EINVAL;
473 }
474
475 *attrsp = attrs;
476 return 0;
477}
478
479static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800480 const struct nlattr *a[], u64 *attrsp,
481 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700482{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800483 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700484}
485
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800486int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
487 u64 *attrsp, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700488{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800489 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
490}
491
492static int genev_tun_opt_from_nlattr(const struct nlattr *a,
493 struct sw_flow_match *match, bool is_mask,
494 bool log)
495{
496 unsigned long opt_key_offset;
497
498 if (nla_len(a) > sizeof(match->key->tun_opts)) {
499 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
500 nla_len(a), sizeof(match->key->tun_opts));
501 return -EINVAL;
502 }
503
504 if (nla_len(a) % 4 != 0) {
505 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
506 nla_len(a));
507 return -EINVAL;
508 }
509
510 /* We need to record the length of the options passed
511 * down, otherwise packets with the same format but
512 * additional options will be silently matched.
513 */
514 if (!is_mask) {
515 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
516 false);
517 } else {
518 /* This is somewhat unusual because it looks at
519 * both the key and mask while parsing the
520 * attributes (and by extension assumes the key
521 * is parsed first). Normally, we would verify
522 * that each is the correct length and that the
523 * attributes line up in the validate function.
524 * However, that is difficult because this is
525 * variable length and we won't have the
526 * information later.
527 */
528 if (match->key->tun_opts_len != nla_len(a)) {
529 OVS_NLERR(log, "Geneve option len %d != mask len %d",
530 match->key->tun_opts_len, nla_len(a));
531 return -EINVAL;
532 }
533
534 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
535 }
536
Thomas Grafd91641d2015-01-15 03:53:57 +0100537 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800538 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
539 nla_len(a), is_mask);
540 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700541}
542
Jesse Gross982b5272015-09-11 18:38:28 -0700543static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100544 struct sw_flow_match *match, bool is_mask,
545 bool log)
546{
Jesse Gross982b5272015-09-11 18:38:28 -0700547 struct nlattr *a;
548 int rem;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100549 unsigned long opt_key_offset;
Thomas Graf614732e2015-07-21 10:44:06 +0200550 struct vxlan_metadata opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100551
552 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
553
Thomas Graf1dd144c2015-01-15 03:53:59 +0100554 memset(&opts, 0, sizeof(opts));
Jesse Gross982b5272015-09-11 18:38:28 -0700555 nla_for_each_nested(a, attr, rem) {
556 int type = nla_type(a);
Thomas Graf1dd144c2015-01-15 03:53:59 +0100557
Jesse Gross982b5272015-09-11 18:38:28 -0700558 if (type > OVS_VXLAN_EXT_MAX) {
559 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
560 type, OVS_VXLAN_EXT_MAX);
561 return -EINVAL;
562 }
563
564 if (!check_attr_len(nla_len(a),
565 ovs_vxlan_ext_key_lens[type].len)) {
566 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
567 type, nla_len(a),
568 ovs_vxlan_ext_key_lens[type].len);
569 return -EINVAL;
570 }
571
572 switch (type) {
573 case OVS_VXLAN_EXT_GBP:
574 opts.gbp = nla_get_u32(a);
575 break;
576 default:
577 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
578 type);
579 return -EINVAL;
580 }
581 }
582 if (rem) {
583 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
584 rem);
585 return -EINVAL;
586 }
Thomas Graf1dd144c2015-01-15 03:53:59 +0100587
588 if (!is_mask)
589 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
590 else
591 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
592
593 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
594 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
595 is_mask);
596 return 0;
597}
598
William Tuceaa0012017-10-04 17:03:12 -0700599static int erspan_tun_opt_from_nlattr(const struct nlattr *attr,
600 struct sw_flow_match *match, bool is_mask,
601 bool log)
602{
603 unsigned long opt_key_offset;
604 struct erspan_metadata opts;
605
606 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
607
608 memset(&opts, 0, sizeof(opts));
609 opts.index = nla_get_be32(attr);
610
611 /* Index has only 20-bit */
612 if (ntohl(opts.index) & ~INDEX_MASK) {
613 OVS_NLERR(log, "ERSPAN index number %x too large.",
614 ntohl(opts.index));
615 return -EINVAL;
616 }
617
618 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), is_mask);
619 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
620 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
621 is_mask);
622
623 return 0;
624}
625
Jiri Benc6b26ba32015-10-05 13:09:47 +0200626static int ip_tun_from_nlattr(const struct nlattr *attr,
627 struct sw_flow_match *match, bool is_mask,
628 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700629{
Pravin B Shelar99e28f12015-10-20 20:47:46 -0700630 bool ttl = false, ipv4 = false, ipv6 = false;
631 __be16 tun_flags = 0;
632 int opts_type = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700633 struct nlattr *a;
634 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700635
636 nla_for_each_nested(a, attr, rem) {
637 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800638 int err;
639
Pravin B Shelare6445712013-10-03 18:16:47 -0700640 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800641 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
642 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700643 return -EINVAL;
644 }
645
Jesse Gross982b5272015-09-11 18:38:28 -0700646 if (!check_attr_len(nla_len(a),
647 ovs_tunnel_key_lens[type].len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800648 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100649 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700650 return -EINVAL;
651 }
652
653 switch (type) {
654 case OVS_TUNNEL_KEY_ATTR_ID:
655 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
656 nla_get_be64(a), is_mask);
657 tun_flags |= TUNNEL_KEY;
658 break;
659 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200660 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
Jiri Benc67b61f62015-03-29 16:59:26 +0200661 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200662 ipv4 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700663 break;
664 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200665 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
Jiri Benc67b61f62015-03-29 16:59:26 +0200666 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200667 ipv4 = true;
668 break;
669 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
Or Gerlitz3d20f1f2017-03-15 18:10:47 +0200670 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
Jiri Benc6b26ba32015-10-05 13:09:47 +0200671 nla_get_in6_addr(a), is_mask);
672 ipv6 = true;
673 break;
674 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
675 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
676 nla_get_in6_addr(a), is_mask);
677 ipv6 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700678 break;
679 case OVS_TUNNEL_KEY_ATTR_TOS:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200680 SW_FLOW_KEY_PUT(match, tun_key.tos,
Pravin B Shelare6445712013-10-03 18:16:47 -0700681 nla_get_u8(a), is_mask);
682 break;
683 case OVS_TUNNEL_KEY_ATTR_TTL:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200684 SW_FLOW_KEY_PUT(match, tun_key.ttl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700685 nla_get_u8(a), is_mask);
686 ttl = true;
687 break;
688 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
689 tun_flags |= TUNNEL_DONT_FRAGMENT;
690 break;
691 case OVS_TUNNEL_KEY_ATTR_CSUM:
692 tun_flags |= TUNNEL_CSUM;
693 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800694 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
695 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
696 nla_get_be16(a), is_mask);
697 break;
698 case OVS_TUNNEL_KEY_ATTR_TP_DST:
699 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
700 nla_get_be16(a), is_mask);
701 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700702 case OVS_TUNNEL_KEY_ATTR_OAM:
703 tun_flags |= TUNNEL_OAM;
704 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700705 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Thomas Graf1dd144c2015-01-15 03:53:59 +0100706 if (opts_type) {
707 OVS_NLERR(log, "Multiple metadata blocks provided");
708 return -EINVAL;
709 }
710
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800711 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
712 if (err)
713 return err;
714
Thomas Graf1dd144c2015-01-15 03:53:59 +0100715 tun_flags |= TUNNEL_GENEVE_OPT;
716 opts_type = type;
717 break;
718 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
719 if (opts_type) {
720 OVS_NLERR(log, "Multiple metadata blocks provided");
721 return -EINVAL;
722 }
723
724 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
725 if (err)
726 return err;
727
728 tun_flags |= TUNNEL_VXLAN_OPT;
729 opts_type = type;
Jesse Grossf5796682014-10-03 15:35:33 -0700730 break;
Kris Murphy8f3dbfd72017-03-16 10:51:28 -0500731 case OVS_TUNNEL_KEY_ATTR_PAD:
732 break;
William Tuceaa0012017-10-04 17:03:12 -0700733 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
734 if (opts_type) {
735 OVS_NLERR(log, "Multiple metadata blocks provided");
736 return -EINVAL;
737 }
738
739 err = erspan_tun_opt_from_nlattr(a, match, is_mask, log);
740 if (err)
741 return err;
742
743 tun_flags |= TUNNEL_ERSPAN_OPT;
744 opts_type = type;
745 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700746 default:
Jiri Benc6b26ba32015-10-05 13:09:47 +0200747 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700748 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700749 return -EINVAL;
750 }
751 }
752
753 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
Jiri Benc00a93ba2015-10-05 13:09:46 +0200754 if (is_mask)
755 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
756 else
Jiri Benc6b26ba32015-10-05 13:09:47 +0200757 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
758 false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700759
760 if (rem > 0) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200761 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800762 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700763 return -EINVAL;
764 }
765
Jiri Benc6b26ba32015-10-05 13:09:47 +0200766 if (ipv4 && ipv6) {
767 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
768 return -EINVAL;
769 }
770
Pravin B Shelare6445712013-10-03 18:16:47 -0700771 if (!is_mask) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200772 if (!ipv4 && !ipv6) {
773 OVS_NLERR(log, "IP tunnel dst address not specified");
774 return -EINVAL;
775 }
776 if (ipv4 && !match->key->tun_key.u.ipv4.dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800777 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700778 return -EINVAL;
779 }
Jiri Benc6b26ba32015-10-05 13:09:47 +0200780 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
781 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
782 return -EINVAL;
783 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700784
785 if (!ttl) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200786 OVS_NLERR(log, "IP tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700787 return -EINVAL;
788 }
789 }
790
Thomas Graf1dd144c2015-01-15 03:53:59 +0100791 return opts_type;
792}
793
794static int vxlan_opt_to_nlattr(struct sk_buff *skb,
795 const void *tun_opts, int swkey_tun_opts_len)
796{
Thomas Graf614732e2015-07-21 10:44:06 +0200797 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100798 struct nlattr *nla;
799
800 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
801 if (!nla)
802 return -EMSGSIZE;
803
804 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
805 return -EMSGSIZE;
806
807 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700808 return 0;
809}
810
Jiri Benc6b26ba32015-10-05 13:09:47 +0200811static int __ip_tun_to_nlattr(struct sk_buff *skb,
812 const struct ip_tunnel_key *output,
813 const void *tun_opts, int swkey_tun_opts_len,
814 unsigned short tun_proto)
Pravin B Shelare6445712013-10-03 18:16:47 -0700815{
Pravin B Shelare6445712013-10-03 18:16:47 -0700816 if (output->tun_flags & TUNNEL_KEY &&
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200817 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
818 OVS_TUNNEL_KEY_ATTR_PAD))
Pravin B Shelare6445712013-10-03 18:16:47 -0700819 return -EMSGSIZE;
Jiri Benc6b26ba32015-10-05 13:09:47 +0200820 switch (tun_proto) {
821 case AF_INET:
822 if (output->u.ipv4.src &&
823 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
824 output->u.ipv4.src))
825 return -EMSGSIZE;
826 if (output->u.ipv4.dst &&
827 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
828 output->u.ipv4.dst))
829 return -EMSGSIZE;
830 break;
831 case AF_INET6:
832 if (!ipv6_addr_any(&output->u.ipv6.src) &&
833 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
834 &output->u.ipv6.src))
835 return -EMSGSIZE;
836 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
837 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
838 &output->u.ipv6.dst))
839 return -EMSGSIZE;
840 break;
841 }
Jiri Benc7c383fb2015-08-20 13:56:24 +0200842 if (output->tos &&
843 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700844 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200845 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700846 return -EMSGSIZE;
847 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700848 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700849 return -EMSGSIZE;
850 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700851 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
852 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800853 if (output->tp_src &&
854 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
855 return -EMSGSIZE;
856 if (output->tp_dst &&
857 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
858 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700859 if ((output->tun_flags & TUNNEL_OAM) &&
860 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700861 return -EMSGSIZE;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700862 if (swkey_tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +0100863 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
864 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
865 swkey_tun_opts_len, tun_opts))
866 return -EMSGSIZE;
867 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
868 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
869 return -EMSGSIZE;
William Tuceaa0012017-10-04 17:03:12 -0700870 else if (output->tun_flags & TUNNEL_ERSPAN_OPT &&
871 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
872 ((struct erspan_metadata *)tun_opts)->index))
873 return -EMSGSIZE;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100874 }
Jesse Grossf5796682014-10-03 15:35:33 -0700875
876 return 0;
877}
878
Jiri Benc6b26ba32015-10-05 13:09:47 +0200879static int ip_tun_to_nlattr(struct sk_buff *skb,
880 const struct ip_tunnel_key *output,
881 const void *tun_opts, int swkey_tun_opts_len,
882 unsigned short tun_proto)
Jesse Grossf5796682014-10-03 15:35:33 -0700883{
884 struct nlattr *nla;
885 int err;
886
887 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
888 if (!nla)
889 return -EMSGSIZE;
890
Jiri Benc6b26ba32015-10-05 13:09:47 +0200891 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
892 tun_proto);
Jesse Grossf5796682014-10-03 15:35:33 -0700893 if (err)
894 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700895
896 nla_nest_end(skb, nla);
897 return 0;
898}
899
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700900int ovs_nla_put_tunnel_info(struct sk_buff *skb,
901 struct ip_tunnel_info *tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800902{
David S. Millerba3e2082015-10-24 06:54:12 -0700903 return __ip_tun_to_nlattr(skb, &tun_info->key,
904 ip_tunnel_info_opts(tun_info),
905 tun_info->options_len,
906 ip_tunnel_info_af(tun_info));
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800907}
908
Eric Garver018c1dd2016-09-07 12:56:59 -0400909static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
910 const struct nlattr *a[],
911 bool is_mask, bool inner)
912{
913 __be16 tci = 0;
914 __be16 tpid = 0;
915
916 if (a[OVS_KEY_ATTR_VLAN])
917 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
918
919 if (a[OVS_KEY_ATTR_ETHERTYPE])
920 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
921
922 if (likely(!inner)) {
923 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
924 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
925 } else {
926 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
927 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
928 }
929 return 0;
930}
931
932static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
933 u64 key_attrs, bool inner,
934 const struct nlattr **a, bool log)
935{
936 __be16 tci = 0;
937
938 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
939 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
940 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
941 /* Not a VLAN. */
942 return 0;
943 }
944
945 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
946 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
947 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
948 return -EINVAL;
949 }
950
951 if (a[OVS_KEY_ATTR_VLAN])
952 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
953
954 if (!(tci & htons(VLAN_TAG_PRESENT))) {
955 if (tci) {
956 OVS_NLERR(log, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
957 (inner) ? "C-VLAN" : "VLAN");
958 return -EINVAL;
959 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
960 /* Corner case for truncated VLAN header. */
961 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
962 (inner) ? "C-VLAN" : "VLAN");
963 return -EINVAL;
964 }
965 }
966
967 return 1;
968}
969
970static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
971 u64 key_attrs, bool inner,
972 const struct nlattr **a, bool log)
973{
974 __be16 tci = 0;
975 __be16 tpid = 0;
976 bool encap_valid = !!(match->key->eth.vlan.tci &
977 htons(VLAN_TAG_PRESENT));
978 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
979 htons(VLAN_TAG_PRESENT));
980
981 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
982 /* Not a VLAN. */
983 return 0;
984 }
985
986 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
987 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
988 (inner) ? "C-VLAN" : "VLAN");
989 return -EINVAL;
990 }
991
992 if (a[OVS_KEY_ATTR_VLAN])
993 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
994
995 if (a[OVS_KEY_ATTR_ETHERTYPE])
996 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
997
998 if (tpid != htons(0xffff)) {
999 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
1000 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
1001 return -EINVAL;
1002 }
1003 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1004 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
1005 (inner) ? "C-VLAN" : "VLAN");
1006 return -EINVAL;
1007 }
1008
1009 return 1;
1010}
1011
1012static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
1013 u64 *key_attrs, bool inner,
1014 const struct nlattr **a, bool is_mask,
1015 bool log)
1016{
1017 int err;
1018 const struct nlattr *encap;
1019
1020 if (!is_mask)
1021 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
1022 a, log);
1023 else
1024 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
1025 a, log);
1026 if (err <= 0)
1027 return err;
1028
1029 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
1030 if (err)
1031 return err;
1032
1033 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1034 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
1035 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1036
1037 encap = a[OVS_KEY_ATTR_ENCAP];
1038
1039 if (!is_mask)
1040 err = parse_flow_nlattrs(encap, a, key_attrs, log);
1041 else
1042 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
1043
1044 return err;
1045}
1046
1047static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
1048 u64 *key_attrs, const struct nlattr **a,
1049 bool is_mask, bool log)
1050{
1051 int err;
1052 bool encap_valid = false;
1053
1054 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
1055 is_mask, log);
1056 if (err)
1057 return err;
1058
1059 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_TAG_PRESENT));
1060 if (encap_valid) {
1061 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
1062 is_mask, log);
1063 if (err)
1064 return err;
1065 }
1066
1067 return 0;
1068}
1069
Jiri Benc0a6410f2016-11-10 16:28:22 +01001070static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
1071 u64 *attrs, const struct nlattr **a,
1072 bool is_mask, bool log)
1073{
1074 __be16 eth_type;
1075
1076 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1077 if (is_mask) {
1078 /* Always exact match EtherType. */
1079 eth_type = htons(0xffff);
1080 } else if (!eth_proto_is_802_3(eth_type)) {
1081 OVS_NLERR(log, "EtherType %x is less than min %x",
1082 ntohs(eth_type), ETH_P_802_3_MIN);
1083 return -EINVAL;
1084 }
1085
1086 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1087 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1088 return 0;
1089}
1090
Joe Stringerc2ac6672015-08-26 11:31:52 -07001091static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1092 u64 *attrs, const struct nlattr **a,
1093 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001094{
Jiri Benc0a6410f2016-11-10 16:28:22 +01001095 u8 mac_proto = MAC_PROTO_ETHERNET;
1096
Andy Zhou971427f32014-09-15 19:37:25 -07001097 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
1098 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1099
1100 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1101 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
1102 }
1103
1104 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
1105 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1106
1107 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1108 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
1109 }
1110
Pravin B Shelare6445712013-10-03 18:16:47 -07001111 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1112 SW_FLOW_KEY_PUT(match, phy.priority,
1113 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1114 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1115 }
1116
1117 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1118 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1119
Jesse Gross426cda52014-10-06 05:08:38 -07001120 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001121 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -07001122 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001123 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -07001124 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -07001125 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001126 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001127
1128 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1129 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1130 } else if (!is_mask) {
1131 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1132 }
1133
1134 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1135 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1136
1137 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1138 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1139 }
1140 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
Jiri Benc6b26ba32015-10-05 13:09:47 +02001141 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1142 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -07001143 return -EINVAL;
1144 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1145 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001146
1147 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001148 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001149 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001150
Joe Stringer9e384712015-10-19 19:18:57 -07001151 if (ct_state & ~CT_SUPPORTED_MASK) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001152 OVS_NLERR(log, "ct_state flags %08x unsupported",
Joe Stringer6f225952015-10-06 10:59:59 -07001153 ct_state);
1154 return -EINVAL;
1155 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001156
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001157 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001158 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1159 }
1160 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001161 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -07001162 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1163
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001164 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001165 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1166 }
Joe Stringer182e3042015-08-26 11:31:49 -07001167 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001168 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
Joe Stringer182e3042015-08-26 11:31:49 -07001169 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1170
1171 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1172 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1173 }
Joe Stringer33db4122015-10-01 15:00:37 -07001174 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1175 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1176 const struct ovs_key_ct_labels *cl;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001177
Joe Stringer33db4122015-10-01 15:00:37 -07001178 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1179 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
Joe Stringerc2ac6672015-08-26 11:31:52 -07001180 sizeof(*cl), is_mask);
Joe Stringer33db4122015-10-01 15:00:37 -07001181 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001182 }
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001183 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1184 const struct ovs_key_ct_tuple_ipv4 *ct;
1185
1186 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1187
1188 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1189 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1190 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1191 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001192 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001193 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1194 }
1195 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1196 const struct ovs_key_ct_tuple_ipv6 *ct;
1197
1198 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1199
1200 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1201 sizeof(match->key->ipv6.ct_orig.src),
1202 is_mask);
1203 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1204 sizeof(match->key->ipv6.ct_orig.dst),
1205 is_mask);
1206 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1207 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001208 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001209 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1210 }
Jiri Benc329f45b2016-11-10 16:28:18 +01001211
Jiri Benc0a6410f2016-11-10 16:28:22 +01001212 /* For layer 3 packets the Ethernet type is provided
1213 * and treated as metadata but no MAC addresses are provided.
1214 */
1215 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1216 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1217 mac_proto = MAC_PROTO_NONE;
1218
Jiri Benc329f45b2016-11-10 16:28:18 +01001219 /* Always exact match mac_proto */
Jiri Benc0a6410f2016-11-10 16:28:22 +01001220 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1221
1222 if (mac_proto == MAC_PROTO_NONE)
1223 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1224 log);
Jiri Benc329f45b2016-11-10 16:28:18 +01001225
Pravin B Shelare6445712013-10-03 18:16:47 -07001226 return 0;
1227}
1228
Joe Stringerc2ac6672015-08-26 11:31:52 -07001229static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1230 u64 attrs, const struct nlattr **a,
1231 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001232{
1233 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001234
Joe Stringerc2ac6672015-08-26 11:31:52 -07001235 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001236 if (err)
1237 return err;
1238
1239 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1240 const struct ovs_key_ethernet *eth_key;
1241
1242 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1243 SW_FLOW_KEY_MEMCPY(match, eth.src,
1244 eth_key->eth_src, ETH_ALEN, is_mask);
1245 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1246 eth_key->eth_dst, ETH_ALEN, is_mask);
1247 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
Pravin B Shelare6445712013-10-03 18:16:47 -07001248
Jiri Benc0a6410f2016-11-10 16:28:22 +01001249 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1250 /* VLAN attribute is always parsed before getting here since it
1251 * may occur multiple times.
1252 */
1253 OVS_NLERR(log, "VLAN attribute unexpected.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001254 return -EINVAL;
1255 }
1256
Jiri Benc0a6410f2016-11-10 16:28:22 +01001257 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1258 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1259 log);
1260 if (err)
1261 return err;
1262 } else if (!is_mask) {
1263 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1264 }
1265 } else if (!match->key->eth.type) {
1266 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1267 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001268 }
1269
1270 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1271 const struct ovs_key_ipv4 *ipv4_key;
1272
1273 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1274 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001275 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1276 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001277 return -EINVAL;
1278 }
1279 SW_FLOW_KEY_PUT(match, ip.proto,
1280 ipv4_key->ipv4_proto, is_mask);
1281 SW_FLOW_KEY_PUT(match, ip.tos,
1282 ipv4_key->ipv4_tos, is_mask);
1283 SW_FLOW_KEY_PUT(match, ip.ttl,
1284 ipv4_key->ipv4_ttl, is_mask);
1285 SW_FLOW_KEY_PUT(match, ip.frag,
1286 ipv4_key->ipv4_frag, is_mask);
1287 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1288 ipv4_key->ipv4_src, is_mask);
1289 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1290 ipv4_key->ipv4_dst, is_mask);
1291 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1292 }
1293
1294 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1295 const struct ovs_key_ipv6 *ipv6_key;
1296
1297 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1298 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001299 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1300 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001301 return -EINVAL;
1302 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001303
Joe Stringerd3052bb2014-11-19 13:54:49 -08001304 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
Joe Perches0ed80da2017-08-11 04:26:26 -07001305 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x)",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001306 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1307 return -EINVAL;
1308 }
1309
Pravin B Shelare6445712013-10-03 18:16:47 -07001310 SW_FLOW_KEY_PUT(match, ipv6.label,
1311 ipv6_key->ipv6_label, is_mask);
1312 SW_FLOW_KEY_PUT(match, ip.proto,
1313 ipv6_key->ipv6_proto, is_mask);
1314 SW_FLOW_KEY_PUT(match, ip.tos,
1315 ipv6_key->ipv6_tclass, is_mask);
1316 SW_FLOW_KEY_PUT(match, ip.ttl,
1317 ipv6_key->ipv6_hlimit, is_mask);
1318 SW_FLOW_KEY_PUT(match, ip.frag,
1319 ipv6_key->ipv6_frag, is_mask);
1320 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1321 ipv6_key->ipv6_src,
1322 sizeof(match->key->ipv6.addr.src),
1323 is_mask);
1324 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1325 ipv6_key->ipv6_dst,
1326 sizeof(match->key->ipv6.addr.dst),
1327 is_mask);
1328
1329 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1330 }
1331
1332 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1333 const struct ovs_key_arp *arp_key;
1334
1335 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1336 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001337 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -07001338 arp_key->arp_op);
1339 return -EINVAL;
1340 }
1341
1342 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1343 arp_key->arp_sip, is_mask);
1344 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1345 arp_key->arp_tip, is_mask);
1346 SW_FLOW_KEY_PUT(match, ip.proto,
1347 ntohs(arp_key->arp_op), is_mask);
1348 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1349 arp_key->arp_sha, ETH_ALEN, is_mask);
1350 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1351 arp_key->arp_tha, ETH_ALEN, is_mask);
1352
1353 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1354 }
1355
Simon Horman25cd9ba2014-10-06 05:05:13 -07001356 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
1357 const struct ovs_key_mpls *mpls_key;
1358
1359 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1360 SW_FLOW_KEY_PUT(match, mpls.top_lse,
1361 mpls_key->mpls_lse, is_mask);
1362
1363 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1364 }
1365
Pravin B Shelare6445712013-10-03 18:16:47 -07001366 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1367 const struct ovs_key_tcp *tcp_key;
1368
1369 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001370 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1371 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001372 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1373 }
1374
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001375 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -07001376 SW_FLOW_KEY_PUT(match, tp.flags,
1377 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1378 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001379 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1380 }
1381
Pravin B Shelare6445712013-10-03 18:16:47 -07001382 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1383 const struct ovs_key_udp *udp_key;
1384
1385 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001386 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1387 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001388 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1389 }
1390
1391 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1392 const struct ovs_key_sctp *sctp_key;
1393
1394 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001395 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1396 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001397 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1398 }
1399
1400 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1401 const struct ovs_key_icmp *icmp_key;
1402
1403 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001404 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001405 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001406 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001407 htons(icmp_key->icmp_code), is_mask);
1408 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1409 }
1410
1411 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1412 const struct ovs_key_icmpv6 *icmpv6_key;
1413
1414 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001415 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001416 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001417 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001418 htons(icmpv6_key->icmpv6_code), is_mask);
1419 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1420 }
1421
1422 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1423 const struct ovs_key_nd *nd_key;
1424
1425 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1426 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1427 nd_key->nd_target,
1428 sizeof(match->key->ipv6.nd.target),
1429 is_mask);
1430 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1431 nd_key->nd_sll, ETH_ALEN, is_mask);
1432 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1433 nd_key->nd_tll, ETH_ALEN, is_mask);
1434 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1435 }
1436
Jesse Gross426cda52014-10-06 05:08:38 -07001437 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001438 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001439 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001440 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001441 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001442
1443 return 0;
1444}
1445
Thomas Graf81bfe3c2015-01-15 03:53:58 +01001446static void nlattr_set(struct nlattr *attr, u8 val,
1447 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001448{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001449 struct nlattr *nla;
1450 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001451
Pravin B Shelarf47de062014-10-16 21:55:45 -07001452 /* The nlattr stream should already have been validated */
1453 nla_for_each_nested(nla, attr, rem) {
Jesse Gross982b5272015-09-11 18:38:28 -07001454 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1455 if (tbl[nla_type(nla)].next)
1456 tbl = tbl[nla_type(nla)].next;
1457 nlattr_set(nla, val, tbl);
1458 } else {
Pravin B Shelarf47de062014-10-16 21:55:45 -07001459 memset(nla_data(nla), val, nla_len(nla));
Jesse Gross982b5272015-09-11 18:38:28 -07001460 }
Joe Stringer9e384712015-10-19 19:18:57 -07001461
1462 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1463 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001464 }
1465}
1466
1467static void mask_set_nlattr(struct nlattr *attr, u8 val)
1468{
Thomas Graf81bfe3c2015-01-15 03:53:58 +01001469 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001470}
1471
1472/**
1473 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1474 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1475 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1476 * does not include any don't care bit.
Joe Stringerc2ac6672015-08-26 11:31:52 -07001477 * @net: Used to determine per-namespace field support.
Pravin B Shelare6445712013-10-03 18:16:47 -07001478 * @match: receives the extracted flow match information.
1479 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1480 * sequence. The fields should of the packet that triggered the creation
1481 * of this flow.
1482 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1483 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001484 * @log: Boolean to allow kernel error logging. Normally true, but when
1485 * probing for feature compatibility this should be passed in as false to
1486 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001487 */
Joe Stringerc2ac6672015-08-26 11:31:52 -07001488int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001489 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001490 const struct nlattr *nla_mask,
1491 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001492{
1493 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelarf47de062014-10-16 21:55:45 -07001494 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001495 u64 key_attrs = 0;
1496 u64 mask_attrs = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001497 int err;
1498
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001499 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001500 if (err)
1501 return err;
1502
Eric Garver018c1dd2016-09-07 12:56:59 -04001503 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1504 if (err)
1505 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001506
Joe Stringerc2ac6672015-08-26 11:31:52 -07001507 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001508 if (err)
1509 return err;
1510
Pravin B Shelara85311b2014-10-19 12:03:40 -07001511 if (match->mask) {
1512 if (!nla_mask) {
1513 /* Create an exact match mask. We need to set to 0xff
1514 * all the 'match->mask' fields that have been touched
1515 * in 'match->key'. We cannot simply memset
1516 * 'match->mask', because padding bytes and fields not
1517 * specified in 'match->key' should be left to 0.
1518 * Instead, we use a stream of netlink attributes,
1519 * copied from 'key' and set to 0xff.
1520 * ovs_key_from_nlattrs() will take care of filling
1521 * 'match->mask' appropriately.
1522 */
1523 newmask = kmemdup(nla_key,
1524 nla_total_size(nla_len(nla_key)),
1525 GFP_KERNEL);
1526 if (!newmask)
1527 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001528
Pravin B Shelara85311b2014-10-19 12:03:40 -07001529 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001530
Pravin B Shelara85311b2014-10-19 12:03:40 -07001531 /* The userspace does not send tunnel attributes that
1532 * are 0, but we should not wildcard them nonetheless.
1533 */
Jiri Benc00a93ba2015-10-05 13:09:46 +02001534 if (match->key->tun_proto)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001535 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1536 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001537
Pravin B Shelara85311b2014-10-19 12:03:40 -07001538 nla_mask = newmask;
1539 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001540
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001541 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001542 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001543 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001544
Pravin B Shelara85311b2014-10-19 12:03:40 -07001545 /* Always match on tci. */
Eric Garver018c1dd2016-09-07 12:56:59 -04001546 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1547 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
Pravin B Shelara85311b2014-10-19 12:03:40 -07001548
Eric Garver018c1dd2016-09-07 12:56:59 -04001549 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1550 if (err)
1551 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001552
Joe Stringerc2ac6672015-08-26 11:31:52 -07001553 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1554 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001555 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001556 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001557 }
1558
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001559 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001560 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001561
Pravin B Shelarf47de062014-10-16 21:55:45 -07001562free_newmask:
1563 kfree(newmask);
1564 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001565}
1566
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001567static size_t get_ufid_len(const struct nlattr *attr, bool log)
1568{
1569 size_t len;
1570
1571 if (!attr)
1572 return 0;
1573
1574 len = nla_len(attr);
1575 if (len < 1 || len > MAX_UFID_LENGTH) {
1576 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1577 nla_len(attr), MAX_UFID_LENGTH);
1578 return 0;
1579 }
1580
1581 return len;
1582}
1583
1584/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1585 * or false otherwise.
1586 */
1587bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1588 bool log)
1589{
1590 sfid->ufid_len = get_ufid_len(attr, log);
1591 if (sfid->ufid_len)
1592 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1593
1594 return sfid->ufid_len;
1595}
1596
1597int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1598 const struct sw_flow_key *key, bool log)
1599{
1600 struct sw_flow_key *new_key;
1601
1602 if (ovs_nla_get_ufid(sfid, ufid, log))
1603 return 0;
1604
1605 /* If UFID was not provided, use unmasked key. */
1606 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1607 if (!new_key)
1608 return -ENOMEM;
1609 memcpy(new_key, key, sizeof(*key));
1610 sfid->unmasked_key = new_key;
1611
1612 return 0;
1613}
1614
1615u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1616{
1617 return attr ? nla_get_u32(attr) : 0;
1618}
1619
Pravin B Shelare6445712013-10-03 18:16:47 -07001620/**
1621 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001622 * @net: Network namespace.
1623 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1624 * metadata.
1625 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1626 * attributes.
1627 * @attrs: Bit mask for the netlink attributes included in @a.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001628 * @log: Boolean to allow kernel error logging. Normally true, but when
1629 * probing for feature compatibility this should be passed in as false to
1630 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001631 *
1632 * This parses a series of Netlink attributes that form a flow key, which must
1633 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1634 * get the metadata, that is, the parts of the flow key that cannot be
1635 * extracted from the packet itself.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001636 *
1637 * This must be called before the packet key fields are filled in 'key'.
Pravin B Shelare6445712013-10-03 18:16:47 -07001638 */
1639
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001640int ovs_nla_get_flow_metadata(struct net *net,
1641 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1642 u64 attrs, struct sw_flow_key *key, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001643{
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001644 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001645
1646 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001647 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001648
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001649 key->ct_state = 0;
1650 key->ct_zone = 0;
1651 key->ct_orig_proto = 0;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001652 memset(&key->ct, 0, sizeof(key->ct));
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001653 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1654 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1655
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001656 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001657
Joe Stringerc2ac6672015-08-26 11:31:52 -07001658 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001659}
1660
Eric Garver018c1dd2016-09-07 12:56:59 -04001661static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1662 bool is_mask)
1663{
1664 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1665
1666 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1667 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1668 return -EMSGSIZE;
1669 return 0;
1670}
1671
Joe Stringer5b4237b2015-01-21 16:42:48 -08001672static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1673 const struct sw_flow_key *output, bool is_mask,
1674 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001675{
1676 struct ovs_key_ethernet *eth_key;
Eric Garver018c1dd2016-09-07 12:56:59 -04001677 struct nlattr *nla;
1678 struct nlattr *encap = NULL;
1679 struct nlattr *in_encap = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001680
Andy Zhou971427f32014-09-15 19:37:25 -07001681 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1682 goto nla_put_failure;
1683
1684 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1685 goto nla_put_failure;
1686
Pravin B Shelare6445712013-10-03 18:16:47 -07001687 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1688 goto nla_put_failure;
1689
Jiri Benc00a93ba2015-10-05 13:09:46 +02001690 if ((swkey->tun_proto || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001691 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001692
1693 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001694 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001695
Jiri Benc6b26ba32015-10-05 13:09:47 +02001696 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
1697 swkey->tun_opts_len, swkey->tun_proto))
Jesse Grossf5796682014-10-03 15:35:33 -07001698 goto nla_put_failure;
1699 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001700
1701 if (swkey->phy.in_port == DP_MAX_PORTS) {
1702 if (is_mask && (output->phy.in_port == 0xffff))
1703 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1704 goto nla_put_failure;
1705 } else {
1706 u16 upper_u16;
1707 upper_u16 = !is_mask ? 0 : 0xffff;
1708
1709 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1710 (upper_u16 << 16) | output->phy.in_port))
1711 goto nla_put_failure;
1712 }
1713
1714 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1715 goto nla_put_failure;
1716
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001717 if (ovs_ct_put_key(swkey, output, skb))
Joe Stringer7f8a4362015-08-26 11:31:48 -07001718 goto nla_put_failure;
1719
Jiri Benc0a6410f2016-11-10 16:28:22 +01001720 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
1721 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1722 if (!nla)
Pravin B Shelare6445712013-10-03 18:16:47 -07001723 goto nla_put_failure;
Eric Garver018c1dd2016-09-07 12:56:59 -04001724
Jiri Benc0a6410f2016-11-10 16:28:22 +01001725 eth_key = nla_data(nla);
1726 ether_addr_copy(eth_key->eth_src, output->eth.src);
1727 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1728
1729 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
1730 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
Eric Garver018c1dd2016-09-07 12:56:59 -04001731 goto nla_put_failure;
Jiri Benc0a6410f2016-11-10 16:28:22 +01001732 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1733 if (!swkey->eth.vlan.tci)
Eric Garver018c1dd2016-09-07 12:56:59 -04001734 goto unencap;
Pravin B Shelare6445712013-10-03 18:16:47 -07001735
Jiri Benc0a6410f2016-11-10 16:28:22 +01001736 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
1737 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
1738 goto nla_put_failure;
1739 in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1740 if (!swkey->eth.cvlan.tci)
1741 goto unencap;
1742 }
1743 }
1744
1745 if (swkey->eth.type == htons(ETH_P_802_2)) {
1746 /*
1747 * Ethertype 802.2 is represented in the netlink with omitted
1748 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1749 * 0xffff in the mask attribute. Ethertype can also
1750 * be wildcarded.
1751 */
1752 if (is_mask && output->eth.type)
1753 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1754 output->eth.type))
1755 goto nla_put_failure;
1756 goto unencap;
1757 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001758 }
1759
1760 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1761 goto nla_put_failure;
1762
Eric Garver018c1dd2016-09-07 12:56:59 -04001763 if (eth_type_vlan(swkey->eth.type)) {
1764 /* There are 3 VLAN tags, we don't know anything about the rest
1765 * of the packet, so truncate here.
1766 */
1767 WARN_ON_ONCE(!(encap && in_encap));
1768 goto unencap;
1769 }
1770
Pravin B Shelare6445712013-10-03 18:16:47 -07001771 if (swkey->eth.type == htons(ETH_P_IP)) {
1772 struct ovs_key_ipv4 *ipv4_key;
1773
1774 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1775 if (!nla)
1776 goto nla_put_failure;
1777 ipv4_key = nla_data(nla);
1778 ipv4_key->ipv4_src = output->ipv4.addr.src;
1779 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1780 ipv4_key->ipv4_proto = output->ip.proto;
1781 ipv4_key->ipv4_tos = output->ip.tos;
1782 ipv4_key->ipv4_ttl = output->ip.ttl;
1783 ipv4_key->ipv4_frag = output->ip.frag;
1784 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1785 struct ovs_key_ipv6 *ipv6_key;
1786
1787 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1788 if (!nla)
1789 goto nla_put_failure;
1790 ipv6_key = nla_data(nla);
1791 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1792 sizeof(ipv6_key->ipv6_src));
1793 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1794 sizeof(ipv6_key->ipv6_dst));
1795 ipv6_key->ipv6_label = output->ipv6.label;
1796 ipv6_key->ipv6_proto = output->ip.proto;
1797 ipv6_key->ipv6_tclass = output->ip.tos;
1798 ipv6_key->ipv6_hlimit = output->ip.ttl;
1799 ipv6_key->ipv6_frag = output->ip.frag;
1800 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1801 swkey->eth.type == htons(ETH_P_RARP)) {
1802 struct ovs_key_arp *arp_key;
1803
1804 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1805 if (!nla)
1806 goto nla_put_failure;
1807 arp_key = nla_data(nla);
1808 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1809 arp_key->arp_sip = output->ipv4.addr.src;
1810 arp_key->arp_tip = output->ipv4.addr.dst;
1811 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001812 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1813 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001814 } else if (eth_p_mpls(swkey->eth.type)) {
1815 struct ovs_key_mpls *mpls_key;
1816
1817 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1818 if (!nla)
1819 goto nla_put_failure;
1820 mpls_key = nla_data(nla);
1821 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001822 }
1823
1824 if ((swkey->eth.type == htons(ETH_P_IP) ||
1825 swkey->eth.type == htons(ETH_P_IPV6)) &&
1826 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1827
1828 if (swkey->ip.proto == IPPROTO_TCP) {
1829 struct ovs_key_tcp *tcp_key;
1830
1831 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1832 if (!nla)
1833 goto nla_put_failure;
1834 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001835 tcp_key->tcp_src = output->tp.src;
1836 tcp_key->tcp_dst = output->tp.dst;
1837 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1838 output->tp.flags))
1839 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001840 } else if (swkey->ip.proto == IPPROTO_UDP) {
1841 struct ovs_key_udp *udp_key;
1842
1843 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1844 if (!nla)
1845 goto nla_put_failure;
1846 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001847 udp_key->udp_src = output->tp.src;
1848 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001849 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1850 struct ovs_key_sctp *sctp_key;
1851
1852 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1853 if (!nla)
1854 goto nla_put_failure;
1855 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001856 sctp_key->sctp_src = output->tp.src;
1857 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001858 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1859 swkey->ip.proto == IPPROTO_ICMP) {
1860 struct ovs_key_icmp *icmp_key;
1861
1862 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1863 if (!nla)
1864 goto nla_put_failure;
1865 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001866 icmp_key->icmp_type = ntohs(output->tp.src);
1867 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001868 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1869 swkey->ip.proto == IPPROTO_ICMPV6) {
1870 struct ovs_key_icmpv6 *icmpv6_key;
1871
1872 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1873 sizeof(*icmpv6_key));
1874 if (!nla)
1875 goto nla_put_failure;
1876 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001877 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1878 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001879
1880 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1881 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1882 struct ovs_key_nd *nd_key;
1883
1884 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1885 if (!nla)
1886 goto nla_put_failure;
1887 nd_key = nla_data(nla);
1888 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1889 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001890 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1891 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001892 }
1893 }
1894 }
1895
1896unencap:
Eric Garver018c1dd2016-09-07 12:56:59 -04001897 if (in_encap)
1898 nla_nest_end(skb, in_encap);
Pravin B Shelare6445712013-10-03 18:16:47 -07001899 if (encap)
1900 nla_nest_end(skb, encap);
1901
1902 return 0;
1903
1904nla_put_failure:
1905 return -EMSGSIZE;
1906}
1907
Joe Stringer5b4237b2015-01-21 16:42:48 -08001908int ovs_nla_put_key(const struct sw_flow_key *swkey,
1909 const struct sw_flow_key *output, int attr, bool is_mask,
1910 struct sk_buff *skb)
1911{
1912 int err;
1913 struct nlattr *nla;
1914
1915 nla = nla_nest_start(skb, attr);
1916 if (!nla)
1917 return -EMSGSIZE;
1918 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1919 if (err)
1920 return err;
1921 nla_nest_end(skb, nla);
1922
1923 return 0;
1924}
1925
1926/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001927int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08001928{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001929 if (ovs_identifier_is_ufid(&flow->id))
1930 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1931 flow->id.ufid);
1932
1933 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1934 OVS_FLOW_ATTR_KEY, false, skb);
1935}
1936
1937/* Called with ovs_mutex or RCU read lock. */
1938int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1939{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08001940 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08001941 OVS_FLOW_ATTR_KEY, false, skb);
1942}
1943
1944/* Called with ovs_mutex or RCU read lock. */
1945int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1946{
1947 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1948 OVS_FLOW_ATTR_MASK, true, skb);
1949}
1950
Pravin B Shelare6445712013-10-03 18:16:47 -07001951#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1952
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001953static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001954{
1955 struct sw_flow_actions *sfa;
1956
Jesse Gross426cda52014-10-06 05:08:38 -07001957 if (size > MAX_ACTIONS_BUFSIZE) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001958 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001959 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001960 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001961
1962 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1963 if (!sfa)
1964 return ERR_PTR(-ENOMEM);
1965
1966 sfa->actions_len = 0;
1967 return sfa;
1968}
1969
Thomas Graf34ae9322015-07-21 10:44:03 +02001970static void ovs_nla_free_set_action(const struct nlattr *a)
1971{
1972 const struct nlattr *ovs_key = nla_data(a);
1973 struct ovs_tunnel_info *ovs_tun;
1974
1975 switch (nla_type(ovs_key)) {
1976 case OVS_KEY_ATTR_TUNNEL_INFO:
1977 ovs_tun = nla_data(ovs_key);
1978 dst_release((struct dst_entry *)ovs_tun->tun_dst);
1979 break;
1980 }
1981}
1982
Pravin B Shelare6445712013-10-03 18:16:47 -07001983void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1984{
Thomas Graf34ae9322015-07-21 10:44:03 +02001985 const struct nlattr *a;
1986 int rem;
1987
1988 if (!sf_acts)
1989 return;
1990
1991 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1992 switch (nla_type(a)) {
1993 case OVS_ACTION_ATTR_SET:
1994 ovs_nla_free_set_action(a);
1995 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001996 case OVS_ACTION_ATTR_CT:
1997 ovs_ct_free_action(a);
1998 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02001999 }
2000 }
2001
2002 kfree(sf_acts);
2003}
2004
2005static void __ovs_nla_free_flow_actions(struct rcu_head *head)
2006{
2007 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
2008}
2009
2010/* Schedules 'sf_acts' to be freed after the next RCU grace period.
2011 * The caller must hold rcu_read_lock for this to be sensible. */
2012void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
2013{
2014 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07002015}
2016
2017static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002018 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002019{
2020
2021 struct sw_flow_actions *acts;
2022 int new_acts_size;
2023 int req_size = NLA_ALIGN(attr_len);
2024 int next_offset = offsetof(struct sw_flow_actions, actions) +
2025 (*sfa)->actions_len;
2026
2027 if (req_size <= (ksize(*sfa) - next_offset))
2028 goto out;
2029
2030 new_acts_size = ksize(*sfa) * 2;
2031
2032 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
2033 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
2034 return ERR_PTR(-EMSGSIZE);
2035 new_acts_size = MAX_ACTIONS_BUFSIZE;
2036 }
2037
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002038 acts = nla_alloc_flow_actions(new_acts_size, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002039 if (IS_ERR(acts))
2040 return (void *)acts;
2041
2042 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
2043 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07002044 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002045 kfree(*sfa);
2046 *sfa = acts;
2047
2048out:
2049 (*sfa)->actions_len += req_size;
2050 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
2051}
2052
Jesse Grossf0b128c2014-10-03 15:35:31 -07002053static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002054 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002055{
2056 struct nlattr *a;
2057
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002058 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002059 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07002060 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07002061
2062 a->nla_type = attrtype;
2063 a->nla_len = nla_attr_size(len);
2064
2065 if (data)
2066 memcpy(nla_data(a), data, len);
2067 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
2068
Jesse Grossf0b128c2014-10-03 15:35:31 -07002069 return a;
2070}
2071
Joe Stringer7f8a4362015-08-26 11:31:48 -07002072int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
2073 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07002074{
2075 struct nlattr *a;
2076
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002077 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07002078
Fabian Frederickf35423c12014-11-14 19:32:58 +01002079 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07002080}
2081
2082static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002083 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002084{
2085 int used = (*sfa)->actions_len;
2086 int err;
2087
Joe Stringer7f8a4362015-08-26 11:31:48 -07002088 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002089 if (err)
2090 return err;
2091
2092 return used;
2093}
2094
2095static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2096 int st_offset)
2097{
2098 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2099 st_offset);
2100
2101 a->nla_len = sfa->actions_len - st_offset;
2102}
2103
Joe Stringer7f8a4362015-08-26 11:31:48 -07002104static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002105 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002106 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002107 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002108
Joe Stringer7f8a4362015-08-26 11:31:48 -07002109static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
andy zhou798c1662017-03-20 16:32:29 -07002110 const struct sw_flow_key *key,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002111 struct sw_flow_actions **sfa,
andy zhou798c1662017-03-20 16:32:29 -07002112 __be16 eth_type, __be16 vlan_tci,
2113 bool log, bool last)
Pravin B Shelare6445712013-10-03 18:16:47 -07002114{
2115 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2116 const struct nlattr *probability, *actions;
2117 const struct nlattr *a;
andy zhou798c1662017-03-20 16:32:29 -07002118 int rem, start, err;
2119 struct sample_arg arg;
Pravin B Shelare6445712013-10-03 18:16:47 -07002120
2121 memset(attrs, 0, sizeof(attrs));
2122 nla_for_each_nested(a, attr, rem) {
2123 int type = nla_type(a);
2124 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2125 return -EINVAL;
2126 attrs[type] = a;
2127 }
2128 if (rem)
2129 return -EINVAL;
2130
2131 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2132 if (!probability || nla_len(probability) != sizeof(u32))
2133 return -EINVAL;
2134
2135 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2136 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2137 return -EINVAL;
2138
2139 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002140 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002141 if (start < 0)
2142 return start;
andy zhou798c1662017-03-20 16:32:29 -07002143
2144 /* When both skb and flow may be changed, put the sample
2145 * into a deferred fifo. On the other hand, if only skb
2146 * may be modified, the actions can be executed in place.
2147 *
2148 * Do this analysis at the flow installation time.
2149 * Set 'clone_action->exec' to true if the actions can be
2150 * executed without being deferred.
2151 *
2152 * If the sample is the last action, it can always be excuted
2153 * rather than deferred.
2154 */
2155 arg.exec = last || !actions_may_change_flow(actions);
2156 arg.probability = nla_get_u32(probability);
2157
2158 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
2159 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002160 if (err)
2161 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07002162
andy zhou798c1662017-03-20 16:32:29 -07002163 err = __ovs_nla_copy_actions(net, actions, key, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002164 eth_type, vlan_tci, log);
andy zhou798c1662017-03-20 16:32:29 -07002165
Pravin B Shelare6445712013-10-03 18:16:47 -07002166 if (err)
2167 return err;
2168
Pravin B Shelare6445712013-10-03 18:16:47 -07002169 add_nested_action_end(*sfa, start);
2170
2171 return 0;
2172}
2173
Pravin B Shelare6445712013-10-03 18:16:47 -07002174void ovs_match_init(struct sw_flow_match *match,
2175 struct sw_flow_key *key,
pravin shelar22799942016-09-19 13:51:00 -07002176 bool reset_key,
Pravin B Shelare6445712013-10-03 18:16:47 -07002177 struct sw_flow_mask *mask)
2178{
2179 memset(match, 0, sizeof(*match));
2180 match->key = key;
2181 match->mask = mask;
2182
pravin shelar22799942016-09-19 13:51:00 -07002183 if (reset_key)
2184 memset(key, 0, sizeof(*key));
Pravin B Shelare6445712013-10-03 18:16:47 -07002185
2186 if (mask) {
2187 memset(&mask->key, 0, sizeof(mask->key));
2188 mask->range.start = mask->range.end = 0;
2189 }
2190}
2191
Thomas Grafd91641d2015-01-15 03:53:57 +01002192static int validate_geneve_opts(struct sw_flow_key *key)
2193{
2194 struct geneve_opt *option;
2195 int opts_len = key->tun_opts_len;
2196 bool crit_opt = false;
2197
2198 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2199 while (opts_len > 0) {
2200 int len;
2201
2202 if (opts_len < sizeof(*option))
2203 return -EINVAL;
2204
2205 len = sizeof(*option) + option->length * 4;
2206 if (len > opts_len)
2207 return -EINVAL;
2208
2209 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2210
2211 option = (struct geneve_opt *)((u8 *)option + len);
2212 opts_len -= len;
2213 };
2214
2215 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2216
2217 return 0;
2218}
2219
Pravin B Shelare6445712013-10-03 18:16:47 -07002220static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002221 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002222{
2223 struct sw_flow_match match;
2224 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02002225 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002226 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02002227 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002228 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01002229 int err = 0, start, opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002230
pravin shelar22799942016-09-19 13:51:00 -07002231 ovs_match_init(&match, &key, true, NULL);
Jiri Benc6b26ba32015-10-05 13:09:47 +02002232 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
Thomas Graf1dd144c2015-01-15 03:53:59 +01002233 if (opts_type < 0)
2234 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002235
Jesse Grossf5796682014-10-03 15:35:33 -07002236 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01002237 switch (opts_type) {
2238 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2239 err = validate_geneve_opts(&key);
2240 if (err < 0)
2241 return err;
2242 break;
2243 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2244 break;
William Tuceaa0012017-10-04 17:03:12 -07002245 case OVS_TUNNEL_KEY_ATTR_ERSPAN_OPTS:
2246 break;
Thomas Graf1dd144c2015-01-15 03:53:59 +01002247 }
Jesse Grossf5796682014-10-03 15:35:33 -07002248 };
2249
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002250 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002251 if (start < 0)
2252 return start;
2253
Jakub Kicinski3fcece12017-06-23 22:11:58 +02002254 tun_dst = metadata_dst_alloc(key.tun_opts_len, METADATA_IP_TUNNEL,
2255 GFP_KERNEL);
2256
Thomas Graf34ae9322015-07-21 10:44:03 +02002257 if (!tun_dst)
2258 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002259
Paolo Abenid71785f2016-02-12 15:43:57 +01002260 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2261 if (err) {
2262 dst_release((struct dst_entry *)tun_dst);
2263 return err;
2264 }
2265
Thomas Graf34ae9322015-07-21 10:44:03 +02002266 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
2267 sizeof(*ovs_tun), log);
2268 if (IS_ERR(a)) {
2269 dst_release((struct dst_entry *)tun_dst);
2270 return PTR_ERR(a);
2271 }
2272
2273 ovs_tun = nla_data(a);
2274 ovs_tun->tun_dst = tun_dst;
2275
2276 tun_info = &tun_dst->u.tun_info;
2277 tun_info->mode = IP_TUNNEL_INFO_TX;
Jiri Benc00a93ba2015-10-05 13:09:46 +02002278 if (key.tun_proto == AF_INET6)
2279 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002280 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07002281
Pravin B Shelar4c222792015-08-30 18:09:38 -07002282 /* We need to store the options in the action itself since
2283 * everything else will go away after flow setup. We can append
2284 * it to tun_info and then point there.
2285 */
2286 ip_tunnel_info_opts_set(tun_info,
2287 TUN_METADATA_OPTS(&key, key.tun_opts_len),
2288 key.tun_opts_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07002289 add_nested_action_end(*sfa, start);
2290
2291 return err;
2292}
2293
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002294/* Return false if there are any non-masked bits set.
2295 * Mask follows data immediately, before any netlink padding.
2296 */
2297static bool validate_masked(u8 *data, int len)
2298{
2299 u8 *mask = data + len;
2300
2301 while (len--)
2302 if (*data++ & ~*mask++)
2303 return false;
2304
2305 return true;
2306}
2307
Pravin B Shelare6445712013-10-03 18:16:47 -07002308static int validate_set(const struct nlattr *a,
2309 const struct sw_flow_key *flow_key,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002310 struct sw_flow_actions **sfa, bool *skip_copy,
2311 u8 mac_proto, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002312{
2313 const struct nlattr *ovs_key = nla_data(a);
2314 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002315 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002316
2317 /* There can be only one key in a action */
2318 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2319 return -EINVAL;
2320
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002321 key_len = nla_len(ovs_key);
2322 if (masked)
2323 key_len /= 2;
2324
Pravin B Shelare6445712013-10-03 18:16:47 -07002325 if (key_type > OVS_KEY_ATTR_MAX ||
Jesse Gross982b5272015-09-11 18:38:28 -07002326 !check_attr_len(key_len, ovs_key_lens[key_type].len))
Pravin B Shelare6445712013-10-03 18:16:47 -07002327 return -EINVAL;
2328
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002329 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2330 return -EINVAL;
2331
Pravin B Shelare6445712013-10-03 18:16:47 -07002332 switch (key_type) {
2333 const struct ovs_key_ipv4 *ipv4_key;
2334 const struct ovs_key_ipv6 *ipv6_key;
2335 int err;
2336
2337 case OVS_KEY_ATTR_PRIORITY:
2338 case OVS_KEY_ATTR_SKB_MARK:
Joe Stringer182e3042015-08-26 11:31:49 -07002339 case OVS_KEY_ATTR_CT_MARK:
Joe Stringer33db4122015-10-01 15:00:37 -07002340 case OVS_KEY_ATTR_CT_LABELS:
Pravin B Shelare6445712013-10-03 18:16:47 -07002341 break;
2342
Jiri Benc0a6410f2016-11-10 16:28:22 +01002343 case OVS_KEY_ATTR_ETHERNET:
2344 if (mac_proto != MAC_PROTO_ETHERNET)
2345 return -EINVAL;
Jarno Rajahalme87e159c2016-12-19 17:06:33 -08002346 break;
Jiri Benc0a6410f2016-11-10 16:28:22 +01002347
Pravin B Shelare6445712013-10-03 18:16:47 -07002348 case OVS_KEY_ATTR_TUNNEL:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002349 if (masked)
2350 return -EINVAL; /* Masked tunnel set not supported. */
2351
2352 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002353 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002354 if (err)
2355 return err;
2356 break;
2357
2358 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002359 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07002360 return -EINVAL;
2361
Pravin B Shelare6445712013-10-03 18:16:47 -07002362 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002363
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002364 if (masked) {
2365 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002366
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002367 /* Non-writeable fields. */
2368 if (mask->ipv4_proto || mask->ipv4_frag)
2369 return -EINVAL;
2370 } else {
2371 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2372 return -EINVAL;
2373
2374 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2375 return -EINVAL;
2376 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002377 break;
2378
2379 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002380 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07002381 return -EINVAL;
2382
Pravin B Shelare6445712013-10-03 18:16:47 -07002383 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002384
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002385 if (masked) {
2386 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002387
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002388 /* Non-writeable fields. */
2389 if (mask->ipv6_proto || mask->ipv6_frag)
2390 return -EINVAL;
2391
2392 /* Invalid bits in the flow label mask? */
2393 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2394 return -EINVAL;
2395 } else {
2396 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2397 return -EINVAL;
2398
2399 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2400 return -EINVAL;
2401 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002402 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2403 return -EINVAL;
2404
2405 break;
2406
2407 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002408 if ((eth_type != htons(ETH_P_IP) &&
2409 eth_type != htons(ETH_P_IPV6)) ||
2410 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002411 return -EINVAL;
2412
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002413 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002414
2415 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002416 if ((eth_type != htons(ETH_P_IP) &&
2417 eth_type != htons(ETH_P_IPV6)) ||
2418 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002419 return -EINVAL;
2420
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002421 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002422
2423 case OVS_KEY_ATTR_MPLS:
2424 if (!eth_p_mpls(eth_type))
2425 return -EINVAL;
2426 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002427
2428 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002429 if ((eth_type != htons(ETH_P_IP) &&
2430 eth_type != htons(ETH_P_IPV6)) ||
2431 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002432 return -EINVAL;
2433
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002434 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002435
2436 default:
2437 return -EINVAL;
2438 }
2439
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002440 /* Convert non-masked non-tunnel set actions to masked set actions. */
2441 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2442 int start, len = key_len * 2;
2443 struct nlattr *at;
2444
2445 *skip_copy = true;
2446
2447 start = add_nested_action_start(sfa,
2448 OVS_ACTION_ATTR_SET_TO_MASKED,
2449 log);
2450 if (start < 0)
2451 return start;
2452
2453 at = __add_action(sfa, key_type, NULL, len, log);
2454 if (IS_ERR(at))
2455 return PTR_ERR(at);
2456
2457 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2458 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2459 /* Clear non-writeable bits from otherwise writeable fields. */
2460 if (key_type == OVS_KEY_ATTR_IPV6) {
2461 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2462
2463 mask->ipv6_label &= htonl(0x000FFFFF);
2464 }
2465 add_nested_action_end(*sfa, start);
2466 }
2467
Pravin B Shelare6445712013-10-03 18:16:47 -07002468 return 0;
2469}
2470
2471static int validate_userspace(const struct nlattr *attr)
2472{
2473 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2474 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2475 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002476 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002477 };
2478 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2479 int error;
2480
Johannes Bergfceb6432017-04-12 14:34:07 +02002481 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, attr,
2482 userspace_policy, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -07002483 if (error)
2484 return error;
2485
2486 if (!a[OVS_USERSPACE_ATTR_PID] ||
2487 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2488 return -EINVAL;
2489
2490 return 0;
2491}
2492
2493static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002494 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002495{
2496 int totlen = NLA_ALIGN(from->nla_len);
2497 struct nlattr *to;
2498
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002499 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002500 if (IS_ERR(to))
2501 return PTR_ERR(to);
2502
2503 memcpy(to, from, totlen);
2504 return 0;
2505}
2506
Joe Stringer7f8a4362015-08-26 11:31:48 -07002507static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002508 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002509 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002510 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002511{
Jiri Benc0a6410f2016-11-10 16:28:22 +01002512 u8 mac_proto = ovs_key_mac_proto(key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002513 const struct nlattr *a;
2514 int rem, err;
2515
Pravin B Shelare6445712013-10-03 18:16:47 -07002516 nla_for_each_nested(a, attr, rem) {
2517 /* Expected argument lengths, (u32)-1 for variable length. */
2518 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2519 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002520 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002521 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002522 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2523 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002524 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2525 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2526 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002527 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002528 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002529 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2530 [OVS_ACTION_ATTR_CT] = (u32)-1,
William Tuf2a4d082016-06-10 11:49:33 -07002531 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
Jiri Benc91820da2016-11-10 16:28:23 +01002532 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
2533 [OVS_ACTION_ATTR_POP_ETH] = 0,
Pravin B Shelare6445712013-10-03 18:16:47 -07002534 };
2535 const struct ovs_action_push_vlan *vlan;
2536 int type = nla_type(a);
2537 bool skip_copy;
2538
2539 if (type > OVS_ACTION_ATTR_MAX ||
2540 (action_lens[type] != nla_len(a) &&
2541 action_lens[type] != (u32)-1))
2542 return -EINVAL;
2543
2544 skip_copy = false;
2545 switch (type) {
2546 case OVS_ACTION_ATTR_UNSPEC:
2547 return -EINVAL;
2548
2549 case OVS_ACTION_ATTR_USERSPACE:
2550 err = validate_userspace(a);
2551 if (err)
2552 return err;
2553 break;
2554
2555 case OVS_ACTION_ATTR_OUTPUT:
2556 if (nla_get_u32(a) >= DP_MAX_PORTS)
2557 return -EINVAL;
2558 break;
2559
William Tuf2a4d082016-06-10 11:49:33 -07002560 case OVS_ACTION_ATTR_TRUNC: {
2561 const struct ovs_action_trunc *trunc = nla_data(a);
2562
2563 if (trunc->max_len < ETH_HLEN)
2564 return -EINVAL;
2565 break;
2566 }
2567
Andy Zhou971427f32014-09-15 19:37:25 -07002568 case OVS_ACTION_ATTR_HASH: {
2569 const struct ovs_action_hash *act_hash = nla_data(a);
2570
2571 switch (act_hash->hash_alg) {
2572 case OVS_HASH_ALG_L4:
2573 break;
2574 default:
2575 return -EINVAL;
2576 }
2577
2578 break;
2579 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002580
2581 case OVS_ACTION_ATTR_POP_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01002582 if (mac_proto != MAC_PROTO_ETHERNET)
2583 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002584 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07002585 break;
2586
2587 case OVS_ACTION_ATTR_PUSH_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01002588 if (mac_proto != MAC_PROTO_ETHERNET)
2589 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07002590 vlan = nla_data(a);
Eric Garver018c1dd2016-09-07 12:56:59 -04002591 if (!eth_type_vlan(vlan->vlan_tpid))
Pravin B Shelare6445712013-10-03 18:16:47 -07002592 return -EINVAL;
2593 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2594 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002595 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07002596 break;
2597
Andy Zhou971427f32014-09-15 19:37:25 -07002598 case OVS_ACTION_ATTR_RECIRC:
2599 break;
2600
Simon Horman25cd9ba2014-10-06 05:05:13 -07002601 case OVS_ACTION_ATTR_PUSH_MPLS: {
2602 const struct ovs_action_push_mpls *mpls = nla_data(a);
2603
Simon Horman25cd9ba2014-10-06 05:05:13 -07002604 if (!eth_p_mpls(mpls->mpls_ethertype))
2605 return -EINVAL;
2606 /* Prohibit push MPLS other than to a white list
2607 * for packets that have a known tag order.
2608 */
2609 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2610 (eth_type != htons(ETH_P_IP) &&
2611 eth_type != htons(ETH_P_IPV6) &&
2612 eth_type != htons(ETH_P_ARP) &&
2613 eth_type != htons(ETH_P_RARP) &&
2614 !eth_p_mpls(eth_type)))
2615 return -EINVAL;
2616 eth_type = mpls->mpls_ethertype;
2617 break;
2618 }
2619
2620 case OVS_ACTION_ATTR_POP_MPLS:
2621 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2622 !eth_p_mpls(eth_type))
2623 return -EINVAL;
2624
2625 /* Disallow subsequent L2.5+ set and mpls_pop actions
2626 * as there is no check here to ensure that the new
2627 * eth_type is valid and thus set actions could
2628 * write off the end of the packet or otherwise
2629 * corrupt it.
2630 *
2631 * Support for these actions is planned using packet
2632 * recirculation.
2633 */
2634 eth_type = htons(0);
2635 break;
2636
Pravin B Shelare6445712013-10-03 18:16:47 -07002637 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002638 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002639 &skip_copy, mac_proto, eth_type,
2640 false, log);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002641 if (err)
2642 return err;
2643 break;
2644
2645 case OVS_ACTION_ATTR_SET_MASKED:
2646 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002647 &skip_copy, mac_proto, eth_type,
2648 true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002649 if (err)
2650 return err;
2651 break;
2652
andy zhou798c1662017-03-20 16:32:29 -07002653 case OVS_ACTION_ATTR_SAMPLE: {
2654 bool last = nla_is_last(a, rem);
2655
2656 err = validate_and_copy_sample(net, a, key, sfa,
2657 eth_type, vlan_tci,
2658 log, last);
Pravin B Shelare6445712013-10-03 18:16:47 -07002659 if (err)
2660 return err;
2661 skip_copy = true;
2662 break;
andy zhou798c1662017-03-20 16:32:29 -07002663 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002664
Joe Stringer7f8a4362015-08-26 11:31:48 -07002665 case OVS_ACTION_ATTR_CT:
2666 err = ovs_ct_copy_action(net, a, key, sfa, log);
2667 if (err)
2668 return err;
2669 skip_copy = true;
2670 break;
2671
Jiri Benc91820da2016-11-10 16:28:23 +01002672 case OVS_ACTION_ATTR_PUSH_ETH:
2673 /* Disallow pushing an Ethernet header if one
2674 * is already present */
2675 if (mac_proto != MAC_PROTO_NONE)
2676 return -EINVAL;
2677 mac_proto = MAC_PROTO_NONE;
2678 break;
2679
2680 case OVS_ACTION_ATTR_POP_ETH:
2681 if (mac_proto != MAC_PROTO_ETHERNET)
2682 return -EINVAL;
2683 if (vlan_tci & htons(VLAN_TAG_PRESENT))
2684 return -EINVAL;
2685 mac_proto = MAC_PROTO_ETHERNET;
2686 break;
2687
Pravin B Shelare6445712013-10-03 18:16:47 -07002688 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002689 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002690 return -EINVAL;
2691 }
2692 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002693 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002694 if (err)
2695 return err;
2696 }
2697 }
2698
2699 if (rem > 0)
2700 return -EINVAL;
2701
2702 return 0;
2703}
2704
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002705/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07002706int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002707 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002708 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07002709{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002710 int err;
2711
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002712 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002713 if (IS_ERR(*sfa))
2714 return PTR_ERR(*sfa);
2715
Joe Stringer8e2fed12015-08-26 11:31:44 -07002716 (*sfa)->orig_len = nla_len(attr);
andy zhou798c1662017-03-20 16:32:29 -07002717 err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
Eric Garver018c1dd2016-09-07 12:56:59 -04002718 key->eth.vlan.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002719 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02002720 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002721
2722 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002723}
2724
andy zhou798c1662017-03-20 16:32:29 -07002725static int sample_action_to_attr(const struct nlattr *attr,
2726 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07002727{
andy zhou798c1662017-03-20 16:32:29 -07002728 struct nlattr *start, *ac_start = NULL, *sample_arg;
2729 int err = 0, rem = nla_len(attr);
2730 const struct sample_arg *arg;
2731 struct nlattr *actions;
Pravin B Shelare6445712013-10-03 18:16:47 -07002732
2733 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2734 if (!start)
2735 return -EMSGSIZE;
2736
andy zhou798c1662017-03-20 16:32:29 -07002737 sample_arg = nla_data(attr);
2738 arg = nla_data(sample_arg);
2739 actions = nla_next(sample_arg, &rem);
Pravin B Shelare6445712013-10-03 18:16:47 -07002740
andy zhou798c1662017-03-20 16:32:29 -07002741 if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
2742 err = -EMSGSIZE;
2743 goto out;
Pravin B Shelare6445712013-10-03 18:16:47 -07002744 }
2745
andy zhou798c1662017-03-20 16:32:29 -07002746 ac_start = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2747 if (!ac_start) {
2748 err = -EMSGSIZE;
2749 goto out;
2750 }
2751
2752 err = ovs_nla_put_actions(actions, rem, skb);
2753
2754out:
2755 if (err) {
2756 nla_nest_cancel(skb, ac_start);
2757 nla_nest_cancel(skb, start);
2758 } else {
2759 nla_nest_end(skb, ac_start);
2760 nla_nest_end(skb, start);
2761 }
2762
Pravin B Shelare6445712013-10-03 18:16:47 -07002763 return err;
2764}
2765
2766static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2767{
2768 const struct nlattr *ovs_key = nla_data(a);
2769 int key_type = nla_type(ovs_key);
2770 struct nlattr *start;
2771 int err;
2772
2773 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07002774 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02002775 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2776 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002777
Pravin B Shelare6445712013-10-03 18:16:47 -07002778 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2779 if (!start)
2780 return -EMSGSIZE;
2781
Simon Hormane905eab2015-12-18 19:43:15 +09002782 err = ip_tun_to_nlattr(skb, &tun_info->key,
2783 ip_tunnel_info_opts(tun_info),
2784 tun_info->options_len,
2785 ip_tunnel_info_af(tun_info));
Pravin B Shelare6445712013-10-03 18:16:47 -07002786 if (err)
2787 return err;
2788 nla_nest_end(skb, start);
2789 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002790 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002791 default:
2792 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2793 return -EMSGSIZE;
2794 break;
2795 }
2796
2797 return 0;
2798}
2799
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002800static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2801 struct sk_buff *skb)
2802{
2803 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08002804 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002805 size_t key_len = nla_len(ovs_key) / 2;
2806
2807 /* Revert the conversion we did from a non-masked set action to
2808 * masked set action.
2809 */
Joe Stringerf4f8e732015-03-02 18:49:56 -08002810 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2811 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002812 return -EMSGSIZE;
2813
Joe Stringerf4f8e732015-03-02 18:49:56 -08002814 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2815 return -EMSGSIZE;
2816
2817 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002818 return 0;
2819}
2820
Pravin B Shelare6445712013-10-03 18:16:47 -07002821int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2822{
2823 const struct nlattr *a;
2824 int rem, err;
2825
2826 nla_for_each_attr(a, attr, len, rem) {
2827 int type = nla_type(a);
2828
2829 switch (type) {
2830 case OVS_ACTION_ATTR_SET:
2831 err = set_action_to_attr(a, skb);
2832 if (err)
2833 return err;
2834 break;
2835
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002836 case OVS_ACTION_ATTR_SET_TO_MASKED:
2837 err = masked_set_action_to_set_action_attr(a, skb);
2838 if (err)
2839 return err;
2840 break;
2841
Pravin B Shelare6445712013-10-03 18:16:47 -07002842 case OVS_ACTION_ATTR_SAMPLE:
2843 err = sample_action_to_attr(a, skb);
2844 if (err)
2845 return err;
2846 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002847
2848 case OVS_ACTION_ATTR_CT:
2849 err = ovs_ct_action_to_attr(nla_data(a), skb);
2850 if (err)
2851 return err;
2852 break;
2853
Pravin B Shelare6445712013-10-03 18:16:47 -07002854 default:
2855 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2856 return -EMSGSIZE;
2857 break;
2858 }
2859 }
2860
2861 return 0;
2862}