blob: b17a4ec348f9f5563f19dc0bcb2c41cfe3a44c6d [file] [log] [blame]
Pravin B Shelare6445712013-10-03 18:16:47 -07001/*
Andy Zhou971427f32014-09-15 19:37:25 -07002 * Copyright (c) 2007-2014 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>
Pravin B Shelare6445712013-10-03 18:16:47 -070051
52#include "flow_netlink.h"
53
Thomas Graf81bfe3c32015-01-15 03:53:58 +010054struct ovs_len_tbl {
55 int len;
56 const struct ovs_len_tbl *next;
57};
58
59#define OVS_ATTR_NESTED -1
60
Pravin B Shelara85311b2014-10-19 12:03:40 -070061static void update_range(struct sw_flow_match *match,
62 size_t offset, size_t size, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -070063{
Pravin B Shelara85311b2014-10-19 12:03:40 -070064 struct sw_flow_key_range *range;
Pravin B Shelare6445712013-10-03 18:16:47 -070065 size_t start = rounddown(offset, sizeof(long));
66 size_t end = roundup(offset + size, sizeof(long));
67
68 if (!is_mask)
69 range = &match->range;
Pravin B Shelara85311b2014-10-19 12:03:40 -070070 else
Pravin B Shelare6445712013-10-03 18:16:47 -070071 range = &match->mask->range;
72
Pravin B Shelare6445712013-10-03 18:16:47 -070073 if (range->start == range->end) {
74 range->start = start;
75 range->end = end;
76 return;
77 }
78
79 if (range->start > start)
80 range->start = start;
81
82 if (range->end < end)
83 range->end = end;
84}
85
86#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
87 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -070088 update_range(match, offsetof(struct sw_flow_key, field), \
89 sizeof((match)->key->field), is_mask); \
90 if (is_mask) \
91 (match)->mask->key.field = value; \
92 else \
Pravin B Shelare6445712013-10-03 18:16:47 -070093 (match)->key->field = value; \
Pravin B Shelare6445712013-10-03 18:16:47 -070094 } while (0)
95
Jesse Grossf5796682014-10-03 15:35:33 -070096#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
97 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -070098 update_range(match, offset, len, is_mask); \
Jesse Grossf5796682014-10-03 15:35:33 -070099 if (is_mask) \
100 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700101 len); \
Jesse Grossf5796682014-10-03 15:35:33 -0700102 else \
103 memcpy((u8 *)(match)->key + offset, value_p, len); \
Pravin B Shelare6445712013-10-03 18:16:47 -0700104 } while (0)
105
Jesse Grossf5796682014-10-03 15:35:33 -0700106#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
107 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
108 value_p, len, is_mask)
109
Pravin B Shelara85311b2014-10-19 12:03:40 -0700110#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
111 do { \
112 update_range(match, offsetof(struct sw_flow_key, field), \
113 sizeof((match)->key->field), is_mask); \
114 if (is_mask) \
115 memset((u8 *)&(match)->mask->key.field, value, \
116 sizeof((match)->mask->key.field)); \
117 else \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700118 memset((u8 *)&(match)->key->field, value, \
119 sizeof((match)->key->field)); \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700120 } while (0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700121
122static bool match_validate(const struct sw_flow_match *match,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800123 u64 key_attrs, u64 mask_attrs, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700124{
125 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
126 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
127
128 /* The following mask attributes allowed only if they
129 * pass the validation tests. */
130 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
131 | (1 << OVS_KEY_ATTR_IPV6)
132 | (1 << OVS_KEY_ATTR_TCP)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700133 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700134 | (1 << OVS_KEY_ATTR_UDP)
135 | (1 << OVS_KEY_ATTR_SCTP)
136 | (1 << OVS_KEY_ATTR_ICMP)
137 | (1 << OVS_KEY_ATTR_ICMPV6)
138 | (1 << OVS_KEY_ATTR_ARP)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700139 | (1 << OVS_KEY_ATTR_ND)
140 | (1 << OVS_KEY_ATTR_MPLS));
Pravin B Shelare6445712013-10-03 18:16:47 -0700141
142 /* Always allowed mask fields. */
143 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
144 | (1 << OVS_KEY_ATTR_IN_PORT)
145 | (1 << OVS_KEY_ATTR_ETHERTYPE));
146
147 /* Check key attributes. */
148 if (match->key->eth.type == htons(ETH_P_ARP)
149 || match->key->eth.type == htons(ETH_P_RARP)) {
150 key_expected |= 1 << OVS_KEY_ATTR_ARP;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800151 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700152 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
153 }
154
Simon Horman25cd9ba2014-10-06 05:05:13 -0700155 if (eth_p_mpls(match->key->eth.type)) {
156 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
157 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
158 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
159 }
160
Pravin B Shelare6445712013-10-03 18:16:47 -0700161 if (match->key->eth.type == htons(ETH_P_IP)) {
162 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
163 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
164 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
165
166 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
167 if (match->key->ip.proto == IPPROTO_UDP) {
168 key_expected |= 1 << OVS_KEY_ATTR_UDP;
169 if (match->mask && (match->mask->key.ip.proto == 0xff))
170 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
171 }
172
173 if (match->key->ip.proto == IPPROTO_SCTP) {
174 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
175 if (match->mask && (match->mask->key.ip.proto == 0xff))
176 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
177 }
178
179 if (match->key->ip.proto == IPPROTO_TCP) {
180 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700181 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
182 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700183 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700184 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
185 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700186 }
187
188 if (match->key->ip.proto == IPPROTO_ICMP) {
189 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
190 if (match->mask && (match->mask->key.ip.proto == 0xff))
191 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
192 }
193 }
194 }
195
196 if (match->key->eth.type == htons(ETH_P_IPV6)) {
197 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
198 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
199 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
200
201 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
202 if (match->key->ip.proto == IPPROTO_UDP) {
203 key_expected |= 1 << OVS_KEY_ATTR_UDP;
204 if (match->mask && (match->mask->key.ip.proto == 0xff))
205 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
206 }
207
208 if (match->key->ip.proto == IPPROTO_SCTP) {
209 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
210 if (match->mask && (match->mask->key.ip.proto == 0xff))
211 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
212 }
213
214 if (match->key->ip.proto == IPPROTO_TCP) {
215 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700216 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
217 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700218 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700219 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
220 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700221 }
222
223 if (match->key->ip.proto == IPPROTO_ICMPV6) {
224 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
225 if (match->mask && (match->mask->key.ip.proto == 0xff))
226 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
227
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700228 if (match->key->tp.src ==
Pravin B Shelare6445712013-10-03 18:16:47 -0700229 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700230 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700231 key_expected |= 1 << OVS_KEY_ATTR_ND;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800232 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700233 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
234 }
235 }
236 }
237 }
238
239 if ((key_attrs & key_expected) != key_expected) {
240 /* Key attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800241 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
242 (unsigned long long)key_attrs,
243 (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700244 return false;
245 }
246
247 if ((mask_attrs & mask_allowed) != mask_attrs) {
248 /* Mask attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800249 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
250 (unsigned long long)mask_attrs,
251 (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700252 return false;
253 }
254
255 return true;
256}
257
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800258size_t ovs_tun_key_attr_size(void)
259{
260 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
261 * updating this function.
262 */
263 return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
264 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
265 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
266 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
267 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
268 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
269 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
270 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
271 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
Thomas Graf1dd144c2015-01-15 03:53:59 +0100272 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
273 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
274 */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800275 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
276 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
277}
278
Joe Stringer41af73e2014-10-18 16:14:14 -0700279size_t ovs_key_attr_size(void)
280{
281 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
282 * updating this function.
283 */
Joe Stringer182e3042015-08-26 11:31:49 -0700284 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 25);
Joe Stringer41af73e2014-10-18 16:14:14 -0700285
286 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
287 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800288 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700289 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
290 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
291 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
292 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700293 + nla_total_size(1) /* OVS_KEY_ATTR_CT_STATE */
294 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
Joe Stringer182e3042015-08-26 11:31:49 -0700295 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
Joe Stringer41af73e2014-10-18 16:14:14 -0700296 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
297 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
298 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
299 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
300 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
301 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
302 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
303 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
304}
305
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100306static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
307 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
308 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
309 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
310 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
311 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
312 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
313 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
314 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
315 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
316 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
317 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_NESTED },
Thomas Graf1dd144c2015-01-15 03:53:59 +0100318 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED },
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100319};
320
Pravin B Shelare6445712013-10-03 18:16:47 -0700321/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100322static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
323 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
324 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
325 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
326 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
327 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
328 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
329 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
330 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
331 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
332 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
333 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
334 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
335 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
336 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
337 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
338 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
339 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
340 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
341 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
342 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
343 .next = ovs_tunnel_key_lens, },
344 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700345 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u8) },
346 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700347 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
Pravin B Shelare6445712013-10-03 18:16:47 -0700348};
349
350static bool is_all_zero(const u8 *fp, size_t size)
351{
352 int i;
353
354 if (!fp)
355 return false;
356
357 for (i = 0; i < size; i++)
358 if (fp[i])
359 return false;
360
361 return true;
362}
363
364static int __parse_flow_nlattrs(const struct nlattr *attr,
365 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800366 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700367{
368 const struct nlattr *nla;
369 u64 attrs;
370 int rem;
371
372 attrs = *attrsp;
373 nla_for_each_nested(nla, attr, rem) {
374 u16 type = nla_type(nla);
375 int expected_len;
376
377 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800378 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700379 type, OVS_KEY_ATTR_MAX);
380 return -EINVAL;
381 }
382
383 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800384 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700385 return -EINVAL;
386 }
387
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100388 expected_len = ovs_key_lens[type].len;
389 if (nla_len(nla) != expected_len && expected_len != OVS_ATTR_NESTED) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800390 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
391 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700392 return -EINVAL;
393 }
394
395 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
396 attrs |= 1 << type;
397 a[type] = nla;
398 }
399 }
400 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800401 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700402 return -EINVAL;
403 }
404
405 *attrsp = attrs;
406 return 0;
407}
408
409static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800410 const struct nlattr *a[], u64 *attrsp,
411 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700412{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800413 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700414}
415
416static int parse_flow_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800417 const struct nlattr *a[], u64 *attrsp,
418 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700419{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800420 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
421}
422
423static int genev_tun_opt_from_nlattr(const struct nlattr *a,
424 struct sw_flow_match *match, bool is_mask,
425 bool log)
426{
427 unsigned long opt_key_offset;
428
429 if (nla_len(a) > sizeof(match->key->tun_opts)) {
430 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
431 nla_len(a), sizeof(match->key->tun_opts));
432 return -EINVAL;
433 }
434
435 if (nla_len(a) % 4 != 0) {
436 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
437 nla_len(a));
438 return -EINVAL;
439 }
440
441 /* We need to record the length of the options passed
442 * down, otherwise packets with the same format but
443 * additional options will be silently matched.
444 */
445 if (!is_mask) {
446 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
447 false);
448 } else {
449 /* This is somewhat unusual because it looks at
450 * both the key and mask while parsing the
451 * attributes (and by extension assumes the key
452 * is parsed first). Normally, we would verify
453 * that each is the correct length and that the
454 * attributes line up in the validate function.
455 * However, that is difficult because this is
456 * variable length and we won't have the
457 * information later.
458 */
459 if (match->key->tun_opts_len != nla_len(a)) {
460 OVS_NLERR(log, "Geneve option len %d != mask len %d",
461 match->key->tun_opts_len, nla_len(a));
462 return -EINVAL;
463 }
464
465 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
466 }
467
Thomas Grafd91641d2015-01-15 03:53:57 +0100468 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800469 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
470 nla_len(a), is_mask);
471 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700472}
473
Thomas Graf1dd144c2015-01-15 03:53:59 +0100474static const struct nla_policy vxlan_opt_policy[OVS_VXLAN_EXT_MAX + 1] = {
475 [OVS_VXLAN_EXT_GBP] = { .type = NLA_U32 },
476};
477
478static int vxlan_tun_opt_from_nlattr(const struct nlattr *a,
479 struct sw_flow_match *match, bool is_mask,
480 bool log)
481{
482 struct nlattr *tb[OVS_VXLAN_EXT_MAX+1];
483 unsigned long opt_key_offset;
Thomas Graf614732e2015-07-21 10:44:06 +0200484 struct vxlan_metadata opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100485 int err;
486
487 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
488
489 err = nla_parse_nested(tb, OVS_VXLAN_EXT_MAX, a, vxlan_opt_policy);
490 if (err < 0)
491 return err;
492
493 memset(&opts, 0, sizeof(opts));
494
495 if (tb[OVS_VXLAN_EXT_GBP])
496 opts.gbp = nla_get_u32(tb[OVS_VXLAN_EXT_GBP]);
497
498 if (!is_mask)
499 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
500 else
501 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
502
503 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
504 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
505 is_mask);
506 return 0;
507}
508
Pravin B Shelare6445712013-10-03 18:16:47 -0700509static int ipv4_tun_from_nlattr(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800510 struct sw_flow_match *match, bool is_mask,
511 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700512{
513 struct nlattr *a;
514 int rem;
515 bool ttl = false;
516 __be16 tun_flags = 0;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100517 int opts_type = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700518
519 nla_for_each_nested(a, attr, rem) {
520 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800521 int err;
522
Pravin B Shelare6445712013-10-03 18:16:47 -0700523 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800524 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
525 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700526 return -EINVAL;
527 }
528
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100529 if (ovs_tunnel_key_lens[type].len != nla_len(a) &&
530 ovs_tunnel_key_lens[type].len != OVS_ATTR_NESTED) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800531 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100532 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700533 return -EINVAL;
534 }
535
536 switch (type) {
537 case OVS_TUNNEL_KEY_ATTR_ID:
538 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
539 nla_get_be64(a), is_mask);
540 tun_flags |= TUNNEL_KEY;
541 break;
542 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200543 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
Jiri Benc67b61f62015-03-29 16:59:26 +0200544 nla_get_in_addr(a), is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700545 break;
546 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200547 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
Jiri Benc67b61f62015-03-29 16:59:26 +0200548 nla_get_in_addr(a), is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700549 break;
550 case OVS_TUNNEL_KEY_ATTR_TOS:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200551 SW_FLOW_KEY_PUT(match, tun_key.tos,
Pravin B Shelare6445712013-10-03 18:16:47 -0700552 nla_get_u8(a), is_mask);
553 break;
554 case OVS_TUNNEL_KEY_ATTR_TTL:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200555 SW_FLOW_KEY_PUT(match, tun_key.ttl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700556 nla_get_u8(a), is_mask);
557 ttl = true;
558 break;
559 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
560 tun_flags |= TUNNEL_DONT_FRAGMENT;
561 break;
562 case OVS_TUNNEL_KEY_ATTR_CSUM:
563 tun_flags |= TUNNEL_CSUM;
564 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800565 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
566 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
567 nla_get_be16(a), is_mask);
568 break;
569 case OVS_TUNNEL_KEY_ATTR_TP_DST:
570 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
571 nla_get_be16(a), is_mask);
572 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700573 case OVS_TUNNEL_KEY_ATTR_OAM:
574 tun_flags |= TUNNEL_OAM;
575 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700576 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Thomas Graf1dd144c2015-01-15 03:53:59 +0100577 if (opts_type) {
578 OVS_NLERR(log, "Multiple metadata blocks provided");
579 return -EINVAL;
580 }
581
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800582 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
583 if (err)
584 return err;
585
Thomas Graf1dd144c2015-01-15 03:53:59 +0100586 tun_flags |= TUNNEL_GENEVE_OPT;
587 opts_type = type;
588 break;
589 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
590 if (opts_type) {
591 OVS_NLERR(log, "Multiple metadata blocks provided");
592 return -EINVAL;
593 }
594
595 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
596 if (err)
597 return err;
598
599 tun_flags |= TUNNEL_VXLAN_OPT;
600 opts_type = type;
Jesse Grossf5796682014-10-03 15:35:33 -0700601 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700602 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800603 OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700604 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700605 return -EINVAL;
606 }
607 }
608
609 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
610
611 if (rem > 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800612 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
613 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700614 return -EINVAL;
615 }
616
617 if (!is_mask) {
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200618 if (!match->key->tun_key.u.ipv4.dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800619 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700620 return -EINVAL;
621 }
622
623 if (!ttl) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800624 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700625 return -EINVAL;
626 }
627 }
628
Thomas Graf1dd144c2015-01-15 03:53:59 +0100629 return opts_type;
630}
631
632static int vxlan_opt_to_nlattr(struct sk_buff *skb,
633 const void *tun_opts, int swkey_tun_opts_len)
634{
Thomas Graf614732e2015-07-21 10:44:06 +0200635 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100636 struct nlattr *nla;
637
638 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
639 if (!nla)
640 return -EMSGSIZE;
641
642 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
643 return -EMSGSIZE;
644
645 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700646 return 0;
647}
648
Jesse Grossf5796682014-10-03 15:35:33 -0700649static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200650 const struct ip_tunnel_key *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100651 const void *tun_opts, int swkey_tun_opts_len)
Pravin B Shelare6445712013-10-03 18:16:47 -0700652{
Pravin B Shelare6445712013-10-03 18:16:47 -0700653 if (output->tun_flags & TUNNEL_KEY &&
654 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
655 return -EMSGSIZE;
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200656 if (output->u.ipv4.src &&
Jiri Benc930345e2015-03-29 16:59:25 +0200657 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200658 output->u.ipv4.src))
Pravin B Shelare6445712013-10-03 18:16:47 -0700659 return -EMSGSIZE;
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200660 if (output->u.ipv4.dst &&
Jiri Benc930345e2015-03-29 16:59:25 +0200661 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200662 output->u.ipv4.dst))
Pravin B Shelare6445712013-10-03 18:16:47 -0700663 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200664 if (output->tos &&
665 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700666 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200667 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700668 return -EMSGSIZE;
669 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700670 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700671 return -EMSGSIZE;
672 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700673 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
674 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800675 if (output->tp_src &&
676 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
677 return -EMSGSIZE;
678 if (output->tp_dst &&
679 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
680 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700681 if ((output->tun_flags & TUNNEL_OAM) &&
682 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700683 return -EMSGSIZE;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100684 if (tun_opts) {
685 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
686 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
687 swkey_tun_opts_len, tun_opts))
688 return -EMSGSIZE;
689 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
690 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
691 return -EMSGSIZE;
692 }
Jesse Grossf5796682014-10-03 15:35:33 -0700693
694 return 0;
695}
696
Jesse Grossf5796682014-10-03 15:35:33 -0700697static int ipv4_tun_to_nlattr(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200698 const struct ip_tunnel_key *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100699 const void *tun_opts, int swkey_tun_opts_len)
Jesse Grossf5796682014-10-03 15:35:33 -0700700{
701 struct nlattr *nla;
702 int err;
703
704 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
705 if (!nla)
706 return -EMSGSIZE;
707
708 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
709 if (err)
710 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700711
712 nla_nest_end(skb, nla);
713 return 0;
714}
715
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800716int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200717 const struct ip_tunnel_info *egress_tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800718{
Thomas Graf1d8fff92015-07-21 10:43:54 +0200719 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->key,
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800720 egress_tun_info->options,
721 egress_tun_info->options_len);
722}
723
Pravin B Shelare6445712013-10-03 18:16:47 -0700724static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800725 const struct nlattr **a, bool is_mask,
726 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700727{
Andy Zhou971427f32014-09-15 19:37:25 -0700728 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
729 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
730
731 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
732 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
733 }
734
735 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
736 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
737
738 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
739 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
740 }
741
Pravin B Shelare6445712013-10-03 18:16:47 -0700742 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
743 SW_FLOW_KEY_PUT(match, phy.priority,
744 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
745 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
746 }
747
748 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
749 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
750
Jesse Gross426cda52014-10-06 05:08:38 -0700751 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700752 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -0700753 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800754 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -0700755 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -0700756 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700757 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700758
759 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
760 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
761 } else if (!is_mask) {
762 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
763 }
764
765 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
766 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
767
768 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
769 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
770 }
771 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
772 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100773 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700774 return -EINVAL;
775 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
776 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700777
778 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
779 ovs_ct_verify(OVS_KEY_ATTR_CT_STATE)) {
780 u8 ct_state = nla_get_u8(a[OVS_KEY_ATTR_CT_STATE]);
781
782 SW_FLOW_KEY_PUT(match, ct.state, ct_state, is_mask);
783 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
784 }
785 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
786 ovs_ct_verify(OVS_KEY_ATTR_CT_ZONE)) {
787 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
788
789 SW_FLOW_KEY_PUT(match, ct.zone, ct_zone, is_mask);
790 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
791 }
Joe Stringer182e3042015-08-26 11:31:49 -0700792 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
793 ovs_ct_verify(OVS_KEY_ATTR_CT_MARK)) {
794 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
795
796 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
797 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
798 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700799 return 0;
800}
801
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700802static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800803 const struct nlattr **a, bool is_mask,
804 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700805{
806 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700807
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800808 err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -0700809 if (err)
810 return err;
811
812 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
813 const struct ovs_key_ethernet *eth_key;
814
815 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
816 SW_FLOW_KEY_MEMCPY(match, eth.src,
817 eth_key->eth_src, ETH_ALEN, is_mask);
818 SW_FLOW_KEY_MEMCPY(match, eth.dst,
819 eth_key->eth_dst, ETH_ALEN, is_mask);
820 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
821 }
822
823 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
824 __be16 tci;
825
826 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
827 if (!(tci & htons(VLAN_TAG_PRESENT))) {
828 if (is_mask)
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800829 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700830 else
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800831 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700832
833 return -EINVAL;
834 }
835
836 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
837 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
Pravin B Shelara85311b2014-10-19 12:03:40 -0700838 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700839
840 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
841 __be16 eth_type;
842
843 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
844 if (is_mask) {
845 /* Always exact match EtherType. */
846 eth_type = htons(0xffff);
Alexander Duyck6713fc92015-05-04 14:34:05 -0700847 } else if (!eth_proto_is_802_3(eth_type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800848 OVS_NLERR(log, "EtherType %x is less than min %x",
849 ntohs(eth_type), ETH_P_802_3_MIN);
Pravin B Shelare6445712013-10-03 18:16:47 -0700850 return -EINVAL;
851 }
852
853 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
854 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
855 } else if (!is_mask) {
856 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
857 }
858
859 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
860 const struct ovs_key_ipv4 *ipv4_key;
861
862 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
863 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800864 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
865 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700866 return -EINVAL;
867 }
868 SW_FLOW_KEY_PUT(match, ip.proto,
869 ipv4_key->ipv4_proto, is_mask);
870 SW_FLOW_KEY_PUT(match, ip.tos,
871 ipv4_key->ipv4_tos, is_mask);
872 SW_FLOW_KEY_PUT(match, ip.ttl,
873 ipv4_key->ipv4_ttl, is_mask);
874 SW_FLOW_KEY_PUT(match, ip.frag,
875 ipv4_key->ipv4_frag, is_mask);
876 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
877 ipv4_key->ipv4_src, is_mask);
878 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
879 ipv4_key->ipv4_dst, is_mask);
880 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
881 }
882
883 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
884 const struct ovs_key_ipv6 *ipv6_key;
885
886 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
887 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800888 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
889 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700890 return -EINVAL;
891 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800892
Joe Stringerd3052bb2014-11-19 13:54:49 -0800893 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
David S. Miller14591432014-11-21 22:28:24 -0500894 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800895 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
896 return -EINVAL;
897 }
898
Pravin B Shelare6445712013-10-03 18:16:47 -0700899 SW_FLOW_KEY_PUT(match, ipv6.label,
900 ipv6_key->ipv6_label, is_mask);
901 SW_FLOW_KEY_PUT(match, ip.proto,
902 ipv6_key->ipv6_proto, is_mask);
903 SW_FLOW_KEY_PUT(match, ip.tos,
904 ipv6_key->ipv6_tclass, is_mask);
905 SW_FLOW_KEY_PUT(match, ip.ttl,
906 ipv6_key->ipv6_hlimit, is_mask);
907 SW_FLOW_KEY_PUT(match, ip.frag,
908 ipv6_key->ipv6_frag, is_mask);
909 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
910 ipv6_key->ipv6_src,
911 sizeof(match->key->ipv6.addr.src),
912 is_mask);
913 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
914 ipv6_key->ipv6_dst,
915 sizeof(match->key->ipv6.addr.dst),
916 is_mask);
917
918 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
919 }
920
921 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
922 const struct ovs_key_arp *arp_key;
923
924 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
925 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800926 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -0700927 arp_key->arp_op);
928 return -EINVAL;
929 }
930
931 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
932 arp_key->arp_sip, is_mask);
933 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
934 arp_key->arp_tip, is_mask);
935 SW_FLOW_KEY_PUT(match, ip.proto,
936 ntohs(arp_key->arp_op), is_mask);
937 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
938 arp_key->arp_sha, ETH_ALEN, is_mask);
939 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
940 arp_key->arp_tha, ETH_ALEN, is_mask);
941
942 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
943 }
944
Simon Horman25cd9ba2014-10-06 05:05:13 -0700945 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
946 const struct ovs_key_mpls *mpls_key;
947
948 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
949 SW_FLOW_KEY_PUT(match, mpls.top_lse,
950 mpls_key->mpls_lse, is_mask);
951
952 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
953 }
954
Pravin B Shelare6445712013-10-03 18:16:47 -0700955 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
956 const struct ovs_key_tcp *tcp_key;
957
958 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700959 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
960 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700961 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
962 }
963
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700964 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -0700965 SW_FLOW_KEY_PUT(match, tp.flags,
966 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
967 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700968 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
969 }
970
Pravin B Shelare6445712013-10-03 18:16:47 -0700971 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
972 const struct ovs_key_udp *udp_key;
973
974 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700975 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
976 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700977 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
978 }
979
980 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
981 const struct ovs_key_sctp *sctp_key;
982
983 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700984 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
985 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700986 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
987 }
988
989 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
990 const struct ovs_key_icmp *icmp_key;
991
992 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700993 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700994 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700995 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700996 htons(icmp_key->icmp_code), is_mask);
997 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
998 }
999
1000 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1001 const struct ovs_key_icmpv6 *icmpv6_key;
1002
1003 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001004 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001005 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001006 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001007 htons(icmpv6_key->icmpv6_code), is_mask);
1008 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1009 }
1010
1011 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1012 const struct ovs_key_nd *nd_key;
1013
1014 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1015 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1016 nd_key->nd_target,
1017 sizeof(match->key->ipv6.nd.target),
1018 is_mask);
1019 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1020 nd_key->nd_sll, ETH_ALEN, is_mask);
1021 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1022 nd_key->nd_tll, ETH_ALEN, is_mask);
1023 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1024 }
1025
Jesse Gross426cda52014-10-06 05:08:38 -07001026 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001027 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001028 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001029 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001030 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001031
1032 return 0;
1033}
1034
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001035static void nlattr_set(struct nlattr *attr, u8 val,
1036 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001037{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001038 struct nlattr *nla;
1039 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001040
Pravin B Shelarf47de062014-10-16 21:55:45 -07001041 /* The nlattr stream should already have been validated */
1042 nla_for_each_nested(nla, attr, rem) {
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001043 if (tbl && tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
1044 nlattr_set(nla, val, tbl[nla_type(nla)].next);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001045 else
1046 memset(nla_data(nla), val, nla_len(nla));
1047 }
1048}
1049
1050static void mask_set_nlattr(struct nlattr *attr, u8 val)
1051{
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001052 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001053}
1054
1055/**
1056 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1057 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1058 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1059 * does not include any don't care bit.
1060 * @match: receives the extracted flow match information.
1061 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1062 * sequence. The fields should of the packet that triggered the creation
1063 * of this flow.
1064 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1065 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001066 * @log: Boolean to allow kernel error logging. Normally true, but when
1067 * probing for feature compatibility this should be passed in as false to
1068 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001069 */
1070int ovs_nla_get_match(struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001071 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001072 const struct nlattr *nla_mask,
1073 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001074{
1075 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1076 const struct nlattr *encap;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001077 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001078 u64 key_attrs = 0;
1079 u64 mask_attrs = 0;
1080 bool encap_valid = false;
1081 int err;
1082
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001083 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001084 if (err)
1085 return err;
1086
1087 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
1088 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
1089 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1090 __be16 tci;
1091
1092 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
1093 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001094 OVS_NLERR(log, "Invalid Vlan frame.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001095 return -EINVAL;
1096 }
1097
1098 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1099 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1100 encap = a[OVS_KEY_ATTR_ENCAP];
1101 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1102 encap_valid = true;
1103
1104 if (tci & htons(VLAN_TAG_PRESENT)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001105 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001106 if (err)
1107 return err;
1108 } else if (!tci) {
1109 /* Corner case for truncated 802.1Q header. */
1110 if (nla_len(encap)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001111 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001112 return -EINVAL;
1113 }
1114 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001115 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
Pravin B Shelare6445712013-10-03 18:16:47 -07001116 return -EINVAL;
1117 }
1118 }
1119
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001120 err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001121 if (err)
1122 return err;
1123
Pravin B Shelara85311b2014-10-19 12:03:40 -07001124 if (match->mask) {
1125 if (!nla_mask) {
1126 /* Create an exact match mask. We need to set to 0xff
1127 * all the 'match->mask' fields that have been touched
1128 * in 'match->key'. We cannot simply memset
1129 * 'match->mask', because padding bytes and fields not
1130 * specified in 'match->key' should be left to 0.
1131 * Instead, we use a stream of netlink attributes,
1132 * copied from 'key' and set to 0xff.
1133 * ovs_key_from_nlattrs() will take care of filling
1134 * 'match->mask' appropriately.
1135 */
1136 newmask = kmemdup(nla_key,
1137 nla_total_size(nla_len(nla_key)),
1138 GFP_KERNEL);
1139 if (!newmask)
1140 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001141
Pravin B Shelara85311b2014-10-19 12:03:40 -07001142 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001143
Pravin B Shelara85311b2014-10-19 12:03:40 -07001144 /* The userspace does not send tunnel attributes that
1145 * are 0, but we should not wildcard them nonetheless.
1146 */
Jiri Bencc1ea5d62015-08-20 13:56:23 +02001147 if (match->key->tun_key.u.ipv4.dst)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001148 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1149 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001150
Pravin B Shelara85311b2014-10-19 12:03:40 -07001151 nla_mask = newmask;
1152 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001153
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001154 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001155 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001156 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001157
Pravin B Shelara85311b2014-10-19 12:03:40 -07001158 /* Always match on tci. */
1159 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1160
Pravin B Shelarf47de062014-10-16 21:55:45 -07001161 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001162 __be16 eth_type = 0;
1163 __be16 tci = 0;
1164
1165 if (!encap_valid) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001166 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
Pravin B Shelarf47de062014-10-16 21:55:45 -07001167 err = -EINVAL;
1168 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001169 }
1170
1171 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1172 if (a[OVS_KEY_ATTR_ETHERTYPE])
1173 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1174
1175 if (eth_type == htons(0xffff)) {
1176 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1177 encap = a[OVS_KEY_ATTR_ENCAP];
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001178 err = parse_flow_mask_nlattrs(encap, a,
1179 &mask_attrs, log);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001180 if (err)
1181 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001182 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001183 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1184 ntohs(eth_type));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001185 err = -EINVAL;
1186 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001187 }
1188
1189 if (a[OVS_KEY_ATTR_VLAN])
1190 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1191
1192 if (!(tci & htons(VLAN_TAG_PRESENT))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001193 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1194 ntohs(tci));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001195 err = -EINVAL;
1196 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001197 }
1198 }
1199
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001200 err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001201 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001202 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001203 }
1204
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001205 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001206 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001207
Pravin B Shelarf47de062014-10-16 21:55:45 -07001208free_newmask:
1209 kfree(newmask);
1210 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001211}
1212
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001213static size_t get_ufid_len(const struct nlattr *attr, bool log)
1214{
1215 size_t len;
1216
1217 if (!attr)
1218 return 0;
1219
1220 len = nla_len(attr);
1221 if (len < 1 || len > MAX_UFID_LENGTH) {
1222 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1223 nla_len(attr), MAX_UFID_LENGTH);
1224 return 0;
1225 }
1226
1227 return len;
1228}
1229
1230/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1231 * or false otherwise.
1232 */
1233bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1234 bool log)
1235{
1236 sfid->ufid_len = get_ufid_len(attr, log);
1237 if (sfid->ufid_len)
1238 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1239
1240 return sfid->ufid_len;
1241}
1242
1243int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1244 const struct sw_flow_key *key, bool log)
1245{
1246 struct sw_flow_key *new_key;
1247
1248 if (ovs_nla_get_ufid(sfid, ufid, log))
1249 return 0;
1250
1251 /* If UFID was not provided, use unmasked key. */
1252 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1253 if (!new_key)
1254 return -ENOMEM;
1255 memcpy(new_key, key, sizeof(*key));
1256 sfid->unmasked_key = new_key;
1257
1258 return 0;
1259}
1260
1261u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1262{
1263 return attr ? nla_get_u32(attr) : 0;
1264}
1265
Pravin B Shelare6445712013-10-03 18:16:47 -07001266/**
1267 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001268 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -07001269 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1270 * sequence.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001271 * @log: Boolean to allow kernel error logging. Normally true, but when
1272 * probing for feature compatibility this should be passed in as false to
1273 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001274 *
1275 * This parses a series of Netlink attributes that form a flow key, which must
1276 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1277 * get the metadata, that is, the parts of the flow key that cannot be
1278 * extracted from the packet itself.
1279 */
1280
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001281int ovs_nla_get_flow_metadata(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001282 struct sw_flow_key *key,
1283 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001284{
Pravin B Shelare6445712013-10-03 18:16:47 -07001285 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001286 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001287 u64 attrs = 0;
1288 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001289
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001290 err = parse_flow_nlattrs(attr, a, &attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001291 if (err)
1292 return -EINVAL;
1293
1294 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001295 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001296
Joe Stringer7f8a4362015-08-26 11:31:48 -07001297 memset(&key->ct, 0, sizeof(key->ct));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001298 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001299
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001300 return metadata_from_nlattrs(&match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001301}
1302
Joe Stringer5b4237b2015-01-21 16:42:48 -08001303static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1304 const struct sw_flow_key *output, bool is_mask,
1305 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001306{
1307 struct ovs_key_ethernet *eth_key;
1308 struct nlattr *nla, *encap;
Pravin B Shelare6445712013-10-03 18:16:47 -07001309
Andy Zhou971427f32014-09-15 19:37:25 -07001310 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1311 goto nla_put_failure;
1312
1313 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1314 goto nla_put_failure;
1315
Pravin B Shelare6445712013-10-03 18:16:47 -07001316 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1317 goto nla_put_failure;
1318
Jiri Bencc1ea5d62015-08-20 13:56:23 +02001319 if ((swkey->tun_key.u.ipv4.dst || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001320 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001321
1322 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001323 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001324
1325 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1326 swkey->tun_opts_len))
1327 goto nla_put_failure;
1328 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001329
1330 if (swkey->phy.in_port == DP_MAX_PORTS) {
1331 if (is_mask && (output->phy.in_port == 0xffff))
1332 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1333 goto nla_put_failure;
1334 } else {
1335 u16 upper_u16;
1336 upper_u16 = !is_mask ? 0 : 0xffff;
1337
1338 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1339 (upper_u16 << 16) | output->phy.in_port))
1340 goto nla_put_failure;
1341 }
1342
1343 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1344 goto nla_put_failure;
1345
Joe Stringer7f8a4362015-08-26 11:31:48 -07001346 if (ovs_ct_put_key(output, skb))
1347 goto nla_put_failure;
1348
Pravin B Shelare6445712013-10-03 18:16:47 -07001349 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1350 if (!nla)
1351 goto nla_put_failure;
1352
1353 eth_key = nla_data(nla);
Joe Perches8c63ff02014-02-18 11:15:45 -08001354 ether_addr_copy(eth_key->eth_src, output->eth.src);
1355 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001356
1357 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1358 __be16 eth_type;
1359 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1360 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1361 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1362 goto nla_put_failure;
1363 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1364 if (!swkey->eth.tci)
1365 goto unencap;
1366 } else
1367 encap = NULL;
1368
1369 if (swkey->eth.type == htons(ETH_P_802_2)) {
1370 /*
1371 * Ethertype 802.2 is represented in the netlink with omitted
1372 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1373 * 0xffff in the mask attribute. Ethertype can also
1374 * be wildcarded.
1375 */
1376 if (is_mask && output->eth.type)
1377 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1378 output->eth.type))
1379 goto nla_put_failure;
1380 goto unencap;
1381 }
1382
1383 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1384 goto nla_put_failure;
1385
1386 if (swkey->eth.type == htons(ETH_P_IP)) {
1387 struct ovs_key_ipv4 *ipv4_key;
1388
1389 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1390 if (!nla)
1391 goto nla_put_failure;
1392 ipv4_key = nla_data(nla);
1393 ipv4_key->ipv4_src = output->ipv4.addr.src;
1394 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1395 ipv4_key->ipv4_proto = output->ip.proto;
1396 ipv4_key->ipv4_tos = output->ip.tos;
1397 ipv4_key->ipv4_ttl = output->ip.ttl;
1398 ipv4_key->ipv4_frag = output->ip.frag;
1399 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1400 struct ovs_key_ipv6 *ipv6_key;
1401
1402 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1403 if (!nla)
1404 goto nla_put_failure;
1405 ipv6_key = nla_data(nla);
1406 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1407 sizeof(ipv6_key->ipv6_src));
1408 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1409 sizeof(ipv6_key->ipv6_dst));
1410 ipv6_key->ipv6_label = output->ipv6.label;
1411 ipv6_key->ipv6_proto = output->ip.proto;
1412 ipv6_key->ipv6_tclass = output->ip.tos;
1413 ipv6_key->ipv6_hlimit = output->ip.ttl;
1414 ipv6_key->ipv6_frag = output->ip.frag;
1415 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1416 swkey->eth.type == htons(ETH_P_RARP)) {
1417 struct ovs_key_arp *arp_key;
1418
1419 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1420 if (!nla)
1421 goto nla_put_failure;
1422 arp_key = nla_data(nla);
1423 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1424 arp_key->arp_sip = output->ipv4.addr.src;
1425 arp_key->arp_tip = output->ipv4.addr.dst;
1426 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001427 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1428 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001429 } else if (eth_p_mpls(swkey->eth.type)) {
1430 struct ovs_key_mpls *mpls_key;
1431
1432 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1433 if (!nla)
1434 goto nla_put_failure;
1435 mpls_key = nla_data(nla);
1436 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001437 }
1438
1439 if ((swkey->eth.type == htons(ETH_P_IP) ||
1440 swkey->eth.type == htons(ETH_P_IPV6)) &&
1441 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1442
1443 if (swkey->ip.proto == IPPROTO_TCP) {
1444 struct ovs_key_tcp *tcp_key;
1445
1446 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1447 if (!nla)
1448 goto nla_put_failure;
1449 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001450 tcp_key->tcp_src = output->tp.src;
1451 tcp_key->tcp_dst = output->tp.dst;
1452 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1453 output->tp.flags))
1454 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001455 } else if (swkey->ip.proto == IPPROTO_UDP) {
1456 struct ovs_key_udp *udp_key;
1457
1458 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1459 if (!nla)
1460 goto nla_put_failure;
1461 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001462 udp_key->udp_src = output->tp.src;
1463 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001464 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1465 struct ovs_key_sctp *sctp_key;
1466
1467 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1468 if (!nla)
1469 goto nla_put_failure;
1470 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001471 sctp_key->sctp_src = output->tp.src;
1472 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001473 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1474 swkey->ip.proto == IPPROTO_ICMP) {
1475 struct ovs_key_icmp *icmp_key;
1476
1477 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1478 if (!nla)
1479 goto nla_put_failure;
1480 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001481 icmp_key->icmp_type = ntohs(output->tp.src);
1482 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001483 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1484 swkey->ip.proto == IPPROTO_ICMPV6) {
1485 struct ovs_key_icmpv6 *icmpv6_key;
1486
1487 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1488 sizeof(*icmpv6_key));
1489 if (!nla)
1490 goto nla_put_failure;
1491 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001492 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1493 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001494
1495 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1496 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1497 struct ovs_key_nd *nd_key;
1498
1499 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1500 if (!nla)
1501 goto nla_put_failure;
1502 nd_key = nla_data(nla);
1503 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1504 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001505 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1506 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001507 }
1508 }
1509 }
1510
1511unencap:
1512 if (encap)
1513 nla_nest_end(skb, encap);
1514
1515 return 0;
1516
1517nla_put_failure:
1518 return -EMSGSIZE;
1519}
1520
Joe Stringer5b4237b2015-01-21 16:42:48 -08001521int ovs_nla_put_key(const struct sw_flow_key *swkey,
1522 const struct sw_flow_key *output, int attr, bool is_mask,
1523 struct sk_buff *skb)
1524{
1525 int err;
1526 struct nlattr *nla;
1527
1528 nla = nla_nest_start(skb, attr);
1529 if (!nla)
1530 return -EMSGSIZE;
1531 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1532 if (err)
1533 return err;
1534 nla_nest_end(skb, nla);
1535
1536 return 0;
1537}
1538
1539/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001540int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08001541{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001542 if (ovs_identifier_is_ufid(&flow->id))
1543 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1544 flow->id.ufid);
1545
1546 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1547 OVS_FLOW_ATTR_KEY, false, skb);
1548}
1549
1550/* Called with ovs_mutex or RCU read lock. */
1551int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1552{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08001553 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08001554 OVS_FLOW_ATTR_KEY, false, skb);
1555}
1556
1557/* Called with ovs_mutex or RCU read lock. */
1558int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1559{
1560 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1561 OVS_FLOW_ATTR_MASK, true, skb);
1562}
1563
Pravin B Shelare6445712013-10-03 18:16:47 -07001564#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1565
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001566static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001567{
1568 struct sw_flow_actions *sfa;
1569
Jesse Gross426cda52014-10-06 05:08:38 -07001570 if (size > MAX_ACTIONS_BUFSIZE) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001571 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001572 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001573 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001574
1575 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1576 if (!sfa)
1577 return ERR_PTR(-ENOMEM);
1578
1579 sfa->actions_len = 0;
1580 return sfa;
1581}
1582
Thomas Graf34ae9322015-07-21 10:44:03 +02001583static void ovs_nla_free_set_action(const struct nlattr *a)
1584{
1585 const struct nlattr *ovs_key = nla_data(a);
1586 struct ovs_tunnel_info *ovs_tun;
1587
1588 switch (nla_type(ovs_key)) {
1589 case OVS_KEY_ATTR_TUNNEL_INFO:
1590 ovs_tun = nla_data(ovs_key);
1591 dst_release((struct dst_entry *)ovs_tun->tun_dst);
1592 break;
1593 }
1594}
1595
Pravin B Shelare6445712013-10-03 18:16:47 -07001596void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1597{
Thomas Graf34ae9322015-07-21 10:44:03 +02001598 const struct nlattr *a;
1599 int rem;
1600
1601 if (!sf_acts)
1602 return;
1603
1604 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1605 switch (nla_type(a)) {
1606 case OVS_ACTION_ATTR_SET:
1607 ovs_nla_free_set_action(a);
1608 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001609 case OVS_ACTION_ATTR_CT:
1610 ovs_ct_free_action(a);
1611 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02001612 }
1613 }
1614
1615 kfree(sf_acts);
1616}
1617
1618static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1619{
1620 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1621}
1622
1623/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1624 * The caller must hold rcu_read_lock for this to be sensible. */
1625void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
1626{
1627 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07001628}
1629
1630static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001631 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001632{
1633
1634 struct sw_flow_actions *acts;
1635 int new_acts_size;
1636 int req_size = NLA_ALIGN(attr_len);
1637 int next_offset = offsetof(struct sw_flow_actions, actions) +
1638 (*sfa)->actions_len;
1639
1640 if (req_size <= (ksize(*sfa) - next_offset))
1641 goto out;
1642
1643 new_acts_size = ksize(*sfa) * 2;
1644
1645 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1646 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1647 return ERR_PTR(-EMSGSIZE);
1648 new_acts_size = MAX_ACTIONS_BUFSIZE;
1649 }
1650
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001651 acts = nla_alloc_flow_actions(new_acts_size, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001652 if (IS_ERR(acts))
1653 return (void *)acts;
1654
1655 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1656 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07001657 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001658 kfree(*sfa);
1659 *sfa = acts;
1660
1661out:
1662 (*sfa)->actions_len += req_size;
1663 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1664}
1665
Jesse Grossf0b128c2014-10-03 15:35:31 -07001666static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001667 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001668{
1669 struct nlattr *a;
1670
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001671 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001672 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001673 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001674
1675 a->nla_type = attrtype;
1676 a->nla_len = nla_attr_size(len);
1677
1678 if (data)
1679 memcpy(nla_data(a), data, len);
1680 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1681
Jesse Grossf0b128c2014-10-03 15:35:31 -07001682 return a;
1683}
1684
Joe Stringer7f8a4362015-08-26 11:31:48 -07001685int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
1686 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07001687{
1688 struct nlattr *a;
1689
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001690 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001691
Fabian Frederickf35423c12014-11-14 19:32:58 +01001692 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07001693}
1694
1695static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001696 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001697{
1698 int used = (*sfa)->actions_len;
1699 int err;
1700
Joe Stringer7f8a4362015-08-26 11:31:48 -07001701 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001702 if (err)
1703 return err;
1704
1705 return used;
1706}
1707
1708static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1709 int st_offset)
1710{
1711 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1712 st_offset);
1713
1714 a->nla_len = sfa->actions_len - st_offset;
1715}
1716
Joe Stringer7f8a4362015-08-26 11:31:48 -07001717static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001718 const struct sw_flow_key *key,
1719 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001720 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001721
Joe Stringer7f8a4362015-08-26 11:31:48 -07001722static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
Pravin B Shelare6445712013-10-03 18:16:47 -07001723 const struct sw_flow_key *key, int depth,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001724 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001725 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001726{
1727 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1728 const struct nlattr *probability, *actions;
1729 const struct nlattr *a;
1730 int rem, start, err, st_acts;
1731
1732 memset(attrs, 0, sizeof(attrs));
1733 nla_for_each_nested(a, attr, rem) {
1734 int type = nla_type(a);
1735 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1736 return -EINVAL;
1737 attrs[type] = a;
1738 }
1739 if (rem)
1740 return -EINVAL;
1741
1742 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1743 if (!probability || nla_len(probability) != sizeof(u32))
1744 return -EINVAL;
1745
1746 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1747 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1748 return -EINVAL;
1749
1750 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001751 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001752 if (start < 0)
1753 return start;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001754 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1755 nla_data(probability), sizeof(u32), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001756 if (err)
1757 return err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001758 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001759 if (st_acts < 0)
1760 return st_acts;
1761
Joe Stringer7f8a4362015-08-26 11:31:48 -07001762 err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001763 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001764 if (err)
1765 return err;
1766
1767 add_nested_action_end(*sfa, st_acts);
1768 add_nested_action_end(*sfa, start);
1769
1770 return 0;
1771}
1772
Pravin B Shelare6445712013-10-03 18:16:47 -07001773void ovs_match_init(struct sw_flow_match *match,
1774 struct sw_flow_key *key,
1775 struct sw_flow_mask *mask)
1776{
1777 memset(match, 0, sizeof(*match));
1778 match->key = key;
1779 match->mask = mask;
1780
1781 memset(key, 0, sizeof(*key));
1782
1783 if (mask) {
1784 memset(&mask->key, 0, sizeof(mask->key));
1785 mask->range.start = mask->range.end = 0;
1786 }
1787}
1788
Thomas Grafd91641d2015-01-15 03:53:57 +01001789static int validate_geneve_opts(struct sw_flow_key *key)
1790{
1791 struct geneve_opt *option;
1792 int opts_len = key->tun_opts_len;
1793 bool crit_opt = false;
1794
1795 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
1796 while (opts_len > 0) {
1797 int len;
1798
1799 if (opts_len < sizeof(*option))
1800 return -EINVAL;
1801
1802 len = sizeof(*option) + option->length * 4;
1803 if (len > opts_len)
1804 return -EINVAL;
1805
1806 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1807
1808 option = (struct geneve_opt *)((u8 *)option + len);
1809 opts_len -= len;
1810 };
1811
1812 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1813
1814 return 0;
1815}
1816
Pravin B Shelare6445712013-10-03 18:16:47 -07001817static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001818 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001819{
1820 struct sw_flow_match match;
1821 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02001822 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02001823 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02001824 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001825 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01001826 int err = 0, start, opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07001827
1828 ovs_match_init(&match, &key, NULL);
Thomas Graf1dd144c2015-01-15 03:53:59 +01001829 opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
1830 if (opts_type < 0)
1831 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07001832
Jesse Grossf5796682014-10-03 15:35:33 -07001833 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01001834 switch (opts_type) {
1835 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1836 err = validate_geneve_opts(&key);
1837 if (err < 0)
1838 return err;
1839 break;
1840 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
1841 break;
1842 }
Jesse Grossf5796682014-10-03 15:35:33 -07001843 };
1844
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001845 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001846 if (start < 0)
1847 return start;
1848
Thomas Graf34ae9322015-07-21 10:44:03 +02001849 tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
1850 if (!tun_dst)
1851 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001852
Thomas Graf34ae9322015-07-21 10:44:03 +02001853 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1854 sizeof(*ovs_tun), log);
1855 if (IS_ERR(a)) {
1856 dst_release((struct dst_entry *)tun_dst);
1857 return PTR_ERR(a);
1858 }
1859
1860 ovs_tun = nla_data(a);
1861 ovs_tun->tun_dst = tun_dst;
1862
1863 tun_info = &tun_dst->u.tun_info;
1864 tun_info->mode = IP_TUNNEL_INFO_TX;
Thomas Graf1d8fff92015-07-21 10:43:54 +02001865 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07001866 tun_info->options_len = key.tun_opts_len;
1867
1868 if (tun_info->options_len) {
1869 /* We need to store the options in the action itself since
1870 * everything else will go away after flow setup. We can append
1871 * it to tun_info and then point there.
1872 */
Thomas Grafd91641d2015-01-15 03:53:57 +01001873 memcpy((tun_info + 1),
1874 TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
1875 tun_info->options = (tun_info + 1);
Jesse Grossf5796682014-10-03 15:35:33 -07001876 } else {
1877 tun_info->options = NULL;
1878 }
Jesse Grossf0b128c2014-10-03 15:35:31 -07001879
Pravin B Shelare6445712013-10-03 18:16:47 -07001880 add_nested_action_end(*sfa, start);
1881
1882 return err;
1883}
1884
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001885/* Return false if there are any non-masked bits set.
1886 * Mask follows data immediately, before any netlink padding.
1887 */
1888static bool validate_masked(u8 *data, int len)
1889{
1890 u8 *mask = data + len;
1891
1892 while (len--)
1893 if (*data++ & ~*mask++)
1894 return false;
1895
1896 return true;
1897}
1898
Pravin B Shelare6445712013-10-03 18:16:47 -07001899static int validate_set(const struct nlattr *a,
1900 const struct sw_flow_key *flow_key,
1901 struct sw_flow_actions **sfa,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001902 bool *skip_copy, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001903{
1904 const struct nlattr *ovs_key = nla_data(a);
1905 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001906 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001907
1908 /* There can be only one key in a action */
1909 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1910 return -EINVAL;
1911
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001912 key_len = nla_len(ovs_key);
1913 if (masked)
1914 key_len /= 2;
1915
Pravin B Shelare6445712013-10-03 18:16:47 -07001916 if (key_type > OVS_KEY_ATTR_MAX ||
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001917 (ovs_key_lens[key_type].len != key_len &&
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001918 ovs_key_lens[key_type].len != OVS_ATTR_NESTED))
Pravin B Shelare6445712013-10-03 18:16:47 -07001919 return -EINVAL;
1920
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001921 if (masked && !validate_masked(nla_data(ovs_key), key_len))
1922 return -EINVAL;
1923
Pravin B Shelare6445712013-10-03 18:16:47 -07001924 switch (key_type) {
1925 const struct ovs_key_ipv4 *ipv4_key;
1926 const struct ovs_key_ipv6 *ipv6_key;
1927 int err;
1928
1929 case OVS_KEY_ATTR_PRIORITY:
1930 case OVS_KEY_ATTR_SKB_MARK:
Joe Stringer182e3042015-08-26 11:31:49 -07001931 case OVS_KEY_ATTR_CT_MARK:
Pravin B Shelare6445712013-10-03 18:16:47 -07001932 case OVS_KEY_ATTR_ETHERNET:
1933 break;
1934
1935 case OVS_KEY_ATTR_TUNNEL:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001936 if (eth_p_mpls(eth_type))
1937 return -EINVAL;
1938
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001939 if (masked)
1940 return -EINVAL; /* Masked tunnel set not supported. */
1941
1942 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001943 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001944 if (err)
1945 return err;
1946 break;
1947
1948 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001949 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07001950 return -EINVAL;
1951
Pravin B Shelare6445712013-10-03 18:16:47 -07001952 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07001953
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001954 if (masked) {
1955 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07001956
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001957 /* Non-writeable fields. */
1958 if (mask->ipv4_proto || mask->ipv4_frag)
1959 return -EINVAL;
1960 } else {
1961 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1962 return -EINVAL;
1963
1964 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1965 return -EINVAL;
1966 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001967 break;
1968
1969 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001970 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07001971 return -EINVAL;
1972
Pravin B Shelare6445712013-10-03 18:16:47 -07001973 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07001974
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001975 if (masked) {
1976 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07001977
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001978 /* Non-writeable fields. */
1979 if (mask->ipv6_proto || mask->ipv6_frag)
1980 return -EINVAL;
1981
1982 /* Invalid bits in the flow label mask? */
1983 if (ntohl(mask->ipv6_label) & 0xFFF00000)
1984 return -EINVAL;
1985 } else {
1986 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1987 return -EINVAL;
1988
1989 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1990 return -EINVAL;
1991 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001992 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1993 return -EINVAL;
1994
1995 break;
1996
1997 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001998 if ((eth_type != htons(ETH_P_IP) &&
1999 eth_type != htons(ETH_P_IPV6)) ||
2000 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002001 return -EINVAL;
2002
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002003 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002004
2005 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002006 if ((eth_type != htons(ETH_P_IP) &&
2007 eth_type != htons(ETH_P_IPV6)) ||
2008 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002009 return -EINVAL;
2010
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002011 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002012
2013 case OVS_KEY_ATTR_MPLS:
2014 if (!eth_p_mpls(eth_type))
2015 return -EINVAL;
2016 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002017
2018 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002019 if ((eth_type != htons(ETH_P_IP) &&
2020 eth_type != htons(ETH_P_IPV6)) ||
2021 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002022 return -EINVAL;
2023
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002024 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002025
2026 default:
2027 return -EINVAL;
2028 }
2029
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002030 /* Convert non-masked non-tunnel set actions to masked set actions. */
2031 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2032 int start, len = key_len * 2;
2033 struct nlattr *at;
2034
2035 *skip_copy = true;
2036
2037 start = add_nested_action_start(sfa,
2038 OVS_ACTION_ATTR_SET_TO_MASKED,
2039 log);
2040 if (start < 0)
2041 return start;
2042
2043 at = __add_action(sfa, key_type, NULL, len, log);
2044 if (IS_ERR(at))
2045 return PTR_ERR(at);
2046
2047 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2048 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2049 /* Clear non-writeable bits from otherwise writeable fields. */
2050 if (key_type == OVS_KEY_ATTR_IPV6) {
2051 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2052
2053 mask->ipv6_label &= htonl(0x000FFFFF);
2054 }
2055 add_nested_action_end(*sfa, start);
2056 }
2057
Pravin B Shelare6445712013-10-03 18:16:47 -07002058 return 0;
2059}
2060
2061static int validate_userspace(const struct nlattr *attr)
2062{
2063 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2064 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2065 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002066 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002067 };
2068 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2069 int error;
2070
2071 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
2072 attr, userspace_policy);
2073 if (error)
2074 return error;
2075
2076 if (!a[OVS_USERSPACE_ATTR_PID] ||
2077 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2078 return -EINVAL;
2079
2080 return 0;
2081}
2082
2083static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002084 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002085{
2086 int totlen = NLA_ALIGN(from->nla_len);
2087 struct nlattr *to;
2088
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002089 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002090 if (IS_ERR(to))
2091 return PTR_ERR(to);
2092
2093 memcpy(to, from, totlen);
2094 return 0;
2095}
2096
Joe Stringer7f8a4362015-08-26 11:31:48 -07002097static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002098 const struct sw_flow_key *key,
2099 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002100 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002101{
2102 const struct nlattr *a;
2103 int rem, err;
2104
2105 if (depth >= SAMPLE_ACTION_DEPTH)
2106 return -EOVERFLOW;
2107
2108 nla_for_each_nested(a, attr, rem) {
2109 /* Expected argument lengths, (u32)-1 for variable length. */
2110 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2111 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002112 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002113 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002114 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2115 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002116 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2117 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2118 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002119 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002120 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002121 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2122 [OVS_ACTION_ATTR_CT] = (u32)-1,
Pravin B Shelare6445712013-10-03 18:16:47 -07002123 };
2124 const struct ovs_action_push_vlan *vlan;
2125 int type = nla_type(a);
2126 bool skip_copy;
2127
2128 if (type > OVS_ACTION_ATTR_MAX ||
2129 (action_lens[type] != nla_len(a) &&
2130 action_lens[type] != (u32)-1))
2131 return -EINVAL;
2132
2133 skip_copy = false;
2134 switch (type) {
2135 case OVS_ACTION_ATTR_UNSPEC:
2136 return -EINVAL;
2137
2138 case OVS_ACTION_ATTR_USERSPACE:
2139 err = validate_userspace(a);
2140 if (err)
2141 return err;
2142 break;
2143
2144 case OVS_ACTION_ATTR_OUTPUT:
2145 if (nla_get_u32(a) >= DP_MAX_PORTS)
2146 return -EINVAL;
2147 break;
2148
Andy Zhou971427f32014-09-15 19:37:25 -07002149 case OVS_ACTION_ATTR_HASH: {
2150 const struct ovs_action_hash *act_hash = nla_data(a);
2151
2152 switch (act_hash->hash_alg) {
2153 case OVS_HASH_ALG_L4:
2154 break;
2155 default:
2156 return -EINVAL;
2157 }
2158
2159 break;
2160 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002161
2162 case OVS_ACTION_ATTR_POP_VLAN:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002163 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07002164 break;
2165
2166 case OVS_ACTION_ATTR_PUSH_VLAN:
2167 vlan = nla_data(a);
2168 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
2169 return -EINVAL;
2170 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2171 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002172 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07002173 break;
2174
Andy Zhou971427f32014-09-15 19:37:25 -07002175 case OVS_ACTION_ATTR_RECIRC:
2176 break;
2177
Simon Horman25cd9ba2014-10-06 05:05:13 -07002178 case OVS_ACTION_ATTR_PUSH_MPLS: {
2179 const struct ovs_action_push_mpls *mpls = nla_data(a);
2180
Simon Horman25cd9ba2014-10-06 05:05:13 -07002181 if (!eth_p_mpls(mpls->mpls_ethertype))
2182 return -EINVAL;
2183 /* Prohibit push MPLS other than to a white list
2184 * for packets that have a known tag order.
2185 */
2186 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2187 (eth_type != htons(ETH_P_IP) &&
2188 eth_type != htons(ETH_P_IPV6) &&
2189 eth_type != htons(ETH_P_ARP) &&
2190 eth_type != htons(ETH_P_RARP) &&
2191 !eth_p_mpls(eth_type)))
2192 return -EINVAL;
2193 eth_type = mpls->mpls_ethertype;
2194 break;
2195 }
2196
2197 case OVS_ACTION_ATTR_POP_MPLS:
2198 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2199 !eth_p_mpls(eth_type))
2200 return -EINVAL;
2201
2202 /* Disallow subsequent L2.5+ set and mpls_pop actions
2203 * as there is no check here to ensure that the new
2204 * eth_type is valid and thus set actions could
2205 * write off the end of the packet or otherwise
2206 * corrupt it.
2207 *
2208 * Support for these actions is planned using packet
2209 * recirculation.
2210 */
2211 eth_type = htons(0);
2212 break;
2213
Pravin B Shelare6445712013-10-03 18:16:47 -07002214 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002215 err = validate_set(a, key, sfa,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002216 &skip_copy, eth_type, false, log);
2217 if (err)
2218 return err;
2219 break;
2220
2221 case OVS_ACTION_ATTR_SET_MASKED:
2222 err = validate_set(a, key, sfa,
2223 &skip_copy, eth_type, true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002224 if (err)
2225 return err;
2226 break;
2227
2228 case OVS_ACTION_ATTR_SAMPLE:
Joe Stringer7f8a4362015-08-26 11:31:48 -07002229 err = validate_and_copy_sample(net, a, key, depth, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002230 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002231 if (err)
2232 return err;
2233 skip_copy = true;
2234 break;
2235
Joe Stringer7f8a4362015-08-26 11:31:48 -07002236 case OVS_ACTION_ATTR_CT:
2237 err = ovs_ct_copy_action(net, a, key, sfa, log);
2238 if (err)
2239 return err;
2240 skip_copy = true;
2241 break;
2242
Pravin B Shelare6445712013-10-03 18:16:47 -07002243 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002244 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002245 return -EINVAL;
2246 }
2247 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002248 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002249 if (err)
2250 return err;
2251 }
2252 }
2253
2254 if (rem > 0)
2255 return -EINVAL;
2256
2257 return 0;
2258}
2259
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002260/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07002261int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002262 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002263 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07002264{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002265 int err;
2266
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002267 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002268 if (IS_ERR(*sfa))
2269 return PTR_ERR(*sfa);
2270
Joe Stringer8e2fed12015-08-26 11:31:44 -07002271 (*sfa)->orig_len = nla_len(attr);
Joe Stringer7f8a4362015-08-26 11:31:48 -07002272 err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002273 key->eth.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002274 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02002275 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002276
2277 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002278}
2279
Pravin B Shelare6445712013-10-03 18:16:47 -07002280static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
2281{
2282 const struct nlattr *a;
2283 struct nlattr *start;
2284 int err = 0, rem;
2285
2286 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2287 if (!start)
2288 return -EMSGSIZE;
2289
2290 nla_for_each_nested(a, attr, rem) {
2291 int type = nla_type(a);
2292 struct nlattr *st_sample;
2293
2294 switch (type) {
2295 case OVS_SAMPLE_ATTR_PROBABILITY:
2296 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
2297 sizeof(u32), nla_data(a)))
2298 return -EMSGSIZE;
2299 break;
2300 case OVS_SAMPLE_ATTR_ACTIONS:
2301 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2302 if (!st_sample)
2303 return -EMSGSIZE;
2304 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
2305 if (err)
2306 return err;
2307 nla_nest_end(skb, st_sample);
2308 break;
2309 }
2310 }
2311
2312 nla_nest_end(skb, start);
2313 return err;
2314}
2315
2316static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2317{
2318 const struct nlattr *ovs_key = nla_data(a);
2319 int key_type = nla_type(ovs_key);
2320 struct nlattr *start;
2321 int err;
2322
2323 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07002324 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02002325 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2326 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002327
Pravin B Shelare6445712013-10-03 18:16:47 -07002328 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2329 if (!start)
2330 return -EMSGSIZE;
2331
Thomas Graf1d8fff92015-07-21 10:43:54 +02002332 err = ipv4_tun_to_nlattr(skb, &tun_info->key,
Jesse Grossf5796682014-10-03 15:35:33 -07002333 tun_info->options_len ?
2334 tun_info->options : NULL,
2335 tun_info->options_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07002336 if (err)
2337 return err;
2338 nla_nest_end(skb, start);
2339 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002340 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002341 default:
2342 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2343 return -EMSGSIZE;
2344 break;
2345 }
2346
2347 return 0;
2348}
2349
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002350static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2351 struct sk_buff *skb)
2352{
2353 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08002354 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002355 size_t key_len = nla_len(ovs_key) / 2;
2356
2357 /* Revert the conversion we did from a non-masked set action to
2358 * masked set action.
2359 */
Joe Stringerf4f8e732015-03-02 18:49:56 -08002360 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2361 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002362 return -EMSGSIZE;
2363
Joe Stringerf4f8e732015-03-02 18:49:56 -08002364 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2365 return -EMSGSIZE;
2366
2367 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002368 return 0;
2369}
2370
Pravin B Shelare6445712013-10-03 18:16:47 -07002371int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2372{
2373 const struct nlattr *a;
2374 int rem, err;
2375
2376 nla_for_each_attr(a, attr, len, rem) {
2377 int type = nla_type(a);
2378
2379 switch (type) {
2380 case OVS_ACTION_ATTR_SET:
2381 err = set_action_to_attr(a, skb);
2382 if (err)
2383 return err;
2384 break;
2385
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002386 case OVS_ACTION_ATTR_SET_TO_MASKED:
2387 err = masked_set_action_to_set_action_attr(a, skb);
2388 if (err)
2389 return err;
2390 break;
2391
Pravin B Shelare6445712013-10-03 18:16:47 -07002392 case OVS_ACTION_ATTR_SAMPLE:
2393 err = sample_action_to_attr(a, skb);
2394 if (err)
2395 return err;
2396 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002397
2398 case OVS_ACTION_ATTR_CT:
2399 err = ovs_ct_action_to_attr(nla_data(a), skb);
2400 if (err)
2401 return err;
2402 break;
2403
Pravin B Shelare6445712013-10-03 18:16:47 -07002404 default:
2405 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2406 return -EMSGSIZE;
2407 break;
2408 }
2409 }
2410
2411 return 0;
2412}