blob: 4e795b289eb766fced9f84fe09ba138abfe658bd [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 Stringer7f8a4362015-08-26 11:31:48 -0700284 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 24);
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 Stringer41af73e2014-10-18 16:14:14 -0700295 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
296 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
297 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
298 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
299 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
300 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
301 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
302 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
303}
304
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100305static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
306 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
307 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
308 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
309 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
310 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
311 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
312 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
313 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
314 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
315 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
316 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_NESTED },
Thomas Graf1dd144c2015-01-15 03:53:59 +0100317 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED },
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100318};
319
Pravin B Shelare6445712013-10-03 18:16:47 -0700320/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100321static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
322 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
323 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
324 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
325 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
326 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
327 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
328 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
329 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
330 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
331 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
332 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
333 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
334 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
335 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
336 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
337 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
338 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
339 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
340 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
341 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
342 .next = ovs_tunnel_key_lens, },
343 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700344 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u8) },
345 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
Pravin B Shelare6445712013-10-03 18:16:47 -0700346};
347
348static bool is_all_zero(const u8 *fp, size_t size)
349{
350 int i;
351
352 if (!fp)
353 return false;
354
355 for (i = 0; i < size; i++)
356 if (fp[i])
357 return false;
358
359 return true;
360}
361
362static int __parse_flow_nlattrs(const struct nlattr *attr,
363 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800364 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700365{
366 const struct nlattr *nla;
367 u64 attrs;
368 int rem;
369
370 attrs = *attrsp;
371 nla_for_each_nested(nla, attr, rem) {
372 u16 type = nla_type(nla);
373 int expected_len;
374
375 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800376 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700377 type, OVS_KEY_ATTR_MAX);
378 return -EINVAL;
379 }
380
381 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800382 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700383 return -EINVAL;
384 }
385
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100386 expected_len = ovs_key_lens[type].len;
387 if (nla_len(nla) != expected_len && expected_len != OVS_ATTR_NESTED) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800388 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
389 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700390 return -EINVAL;
391 }
392
393 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
394 attrs |= 1 << type;
395 a[type] = nla;
396 }
397 }
398 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800399 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700400 return -EINVAL;
401 }
402
403 *attrsp = attrs;
404 return 0;
405}
406
407static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800408 const struct nlattr *a[], u64 *attrsp,
409 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700410{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800411 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700412}
413
414static int parse_flow_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800415 const struct nlattr *a[], u64 *attrsp,
416 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700417{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800418 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
419}
420
421static int genev_tun_opt_from_nlattr(const struct nlattr *a,
422 struct sw_flow_match *match, bool is_mask,
423 bool log)
424{
425 unsigned long opt_key_offset;
426
427 if (nla_len(a) > sizeof(match->key->tun_opts)) {
428 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
429 nla_len(a), sizeof(match->key->tun_opts));
430 return -EINVAL;
431 }
432
433 if (nla_len(a) % 4 != 0) {
434 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
435 nla_len(a));
436 return -EINVAL;
437 }
438
439 /* We need to record the length of the options passed
440 * down, otherwise packets with the same format but
441 * additional options will be silently matched.
442 */
443 if (!is_mask) {
444 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
445 false);
446 } else {
447 /* This is somewhat unusual because it looks at
448 * both the key and mask while parsing the
449 * attributes (and by extension assumes the key
450 * is parsed first). Normally, we would verify
451 * that each is the correct length and that the
452 * attributes line up in the validate function.
453 * However, that is difficult because this is
454 * variable length and we won't have the
455 * information later.
456 */
457 if (match->key->tun_opts_len != nla_len(a)) {
458 OVS_NLERR(log, "Geneve option len %d != mask len %d",
459 match->key->tun_opts_len, nla_len(a));
460 return -EINVAL;
461 }
462
463 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
464 }
465
Thomas Grafd91641d2015-01-15 03:53:57 +0100466 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800467 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
468 nla_len(a), is_mask);
469 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700470}
471
Thomas Graf1dd144c2015-01-15 03:53:59 +0100472static const struct nla_policy vxlan_opt_policy[OVS_VXLAN_EXT_MAX + 1] = {
473 [OVS_VXLAN_EXT_GBP] = { .type = NLA_U32 },
474};
475
476static int vxlan_tun_opt_from_nlattr(const struct nlattr *a,
477 struct sw_flow_match *match, bool is_mask,
478 bool log)
479{
480 struct nlattr *tb[OVS_VXLAN_EXT_MAX+1];
481 unsigned long opt_key_offset;
Thomas Graf614732e2015-07-21 10:44:06 +0200482 struct vxlan_metadata opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100483 int err;
484
485 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
486
487 err = nla_parse_nested(tb, OVS_VXLAN_EXT_MAX, a, vxlan_opt_policy);
488 if (err < 0)
489 return err;
490
491 memset(&opts, 0, sizeof(opts));
492
493 if (tb[OVS_VXLAN_EXT_GBP])
494 opts.gbp = nla_get_u32(tb[OVS_VXLAN_EXT_GBP]);
495
496 if (!is_mask)
497 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
498 else
499 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
500
501 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
502 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
503 is_mask);
504 return 0;
505}
506
Pravin B Shelare6445712013-10-03 18:16:47 -0700507static int ipv4_tun_from_nlattr(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800508 struct sw_flow_match *match, bool is_mask,
509 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700510{
511 struct nlattr *a;
512 int rem;
513 bool ttl = false;
514 __be16 tun_flags = 0;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100515 int opts_type = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700516
517 nla_for_each_nested(a, attr, rem) {
518 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800519 int err;
520
Pravin B Shelare6445712013-10-03 18:16:47 -0700521 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800522 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
523 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700524 return -EINVAL;
525 }
526
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100527 if (ovs_tunnel_key_lens[type].len != nla_len(a) &&
528 ovs_tunnel_key_lens[type].len != OVS_ATTR_NESTED) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800529 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100530 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700531 return -EINVAL;
532 }
533
534 switch (type) {
535 case OVS_TUNNEL_KEY_ATTR_ID:
536 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
537 nla_get_be64(a), is_mask);
538 tun_flags |= TUNNEL_KEY;
539 break;
540 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200541 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
Jiri Benc67b61f62015-03-29 16:59:26 +0200542 nla_get_in_addr(a), is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700543 break;
544 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200545 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
Jiri Benc67b61f62015-03-29 16:59:26 +0200546 nla_get_in_addr(a), is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700547 break;
548 case OVS_TUNNEL_KEY_ATTR_TOS:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200549 SW_FLOW_KEY_PUT(match, tun_key.tos,
Pravin B Shelare6445712013-10-03 18:16:47 -0700550 nla_get_u8(a), is_mask);
551 break;
552 case OVS_TUNNEL_KEY_ATTR_TTL:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200553 SW_FLOW_KEY_PUT(match, tun_key.ttl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700554 nla_get_u8(a), is_mask);
555 ttl = true;
556 break;
557 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
558 tun_flags |= TUNNEL_DONT_FRAGMENT;
559 break;
560 case OVS_TUNNEL_KEY_ATTR_CSUM:
561 tun_flags |= TUNNEL_CSUM;
562 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800563 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
564 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
565 nla_get_be16(a), is_mask);
566 break;
567 case OVS_TUNNEL_KEY_ATTR_TP_DST:
568 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
569 nla_get_be16(a), is_mask);
570 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700571 case OVS_TUNNEL_KEY_ATTR_OAM:
572 tun_flags |= TUNNEL_OAM;
573 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700574 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Thomas Graf1dd144c2015-01-15 03:53:59 +0100575 if (opts_type) {
576 OVS_NLERR(log, "Multiple metadata blocks provided");
577 return -EINVAL;
578 }
579
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800580 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
581 if (err)
582 return err;
583
Thomas Graf1dd144c2015-01-15 03:53:59 +0100584 tun_flags |= TUNNEL_GENEVE_OPT;
585 opts_type = type;
586 break;
587 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
588 if (opts_type) {
589 OVS_NLERR(log, "Multiple metadata blocks provided");
590 return -EINVAL;
591 }
592
593 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
594 if (err)
595 return err;
596
597 tun_flags |= TUNNEL_VXLAN_OPT;
598 opts_type = type;
Jesse Grossf5796682014-10-03 15:35:33 -0700599 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700600 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800601 OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700602 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700603 return -EINVAL;
604 }
605 }
606
607 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
608
609 if (rem > 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800610 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
611 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700612 return -EINVAL;
613 }
614
615 if (!is_mask) {
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200616 if (!match->key->tun_key.u.ipv4.dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800617 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700618 return -EINVAL;
619 }
620
621 if (!ttl) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800622 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700623 return -EINVAL;
624 }
625 }
626
Thomas Graf1dd144c2015-01-15 03:53:59 +0100627 return opts_type;
628}
629
630static int vxlan_opt_to_nlattr(struct sk_buff *skb,
631 const void *tun_opts, int swkey_tun_opts_len)
632{
Thomas Graf614732e2015-07-21 10:44:06 +0200633 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100634 struct nlattr *nla;
635
636 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
637 if (!nla)
638 return -EMSGSIZE;
639
640 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
641 return -EMSGSIZE;
642
643 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700644 return 0;
645}
646
Jesse Grossf5796682014-10-03 15:35:33 -0700647static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200648 const struct ip_tunnel_key *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100649 const void *tun_opts, int swkey_tun_opts_len)
Pravin B Shelare6445712013-10-03 18:16:47 -0700650{
Pravin B Shelare6445712013-10-03 18:16:47 -0700651 if (output->tun_flags & TUNNEL_KEY &&
652 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
653 return -EMSGSIZE;
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200654 if (output->u.ipv4.src &&
Jiri Benc930345e2015-03-29 16:59:25 +0200655 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200656 output->u.ipv4.src))
Pravin B Shelare6445712013-10-03 18:16:47 -0700657 return -EMSGSIZE;
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200658 if (output->u.ipv4.dst &&
Jiri Benc930345e2015-03-29 16:59:25 +0200659 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200660 output->u.ipv4.dst))
Pravin B Shelare6445712013-10-03 18:16:47 -0700661 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200662 if (output->tos &&
663 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700664 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200665 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700666 return -EMSGSIZE;
667 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700668 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700669 return -EMSGSIZE;
670 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700671 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
672 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800673 if (output->tp_src &&
674 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
675 return -EMSGSIZE;
676 if (output->tp_dst &&
677 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
678 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700679 if ((output->tun_flags & TUNNEL_OAM) &&
680 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700681 return -EMSGSIZE;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100682 if (tun_opts) {
683 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
684 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
685 swkey_tun_opts_len, tun_opts))
686 return -EMSGSIZE;
687 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
688 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
689 return -EMSGSIZE;
690 }
Jesse Grossf5796682014-10-03 15:35:33 -0700691
692 return 0;
693}
694
Jesse Grossf5796682014-10-03 15:35:33 -0700695static int ipv4_tun_to_nlattr(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200696 const struct ip_tunnel_key *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100697 const void *tun_opts, int swkey_tun_opts_len)
Jesse Grossf5796682014-10-03 15:35:33 -0700698{
699 struct nlattr *nla;
700 int err;
701
702 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
703 if (!nla)
704 return -EMSGSIZE;
705
706 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
707 if (err)
708 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700709
710 nla_nest_end(skb, nla);
711 return 0;
712}
713
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800714int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200715 const struct ip_tunnel_info *egress_tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800716{
Thomas Graf1d8fff92015-07-21 10:43:54 +0200717 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->key,
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800718 egress_tun_info->options,
719 egress_tun_info->options_len);
720}
721
Pravin B Shelare6445712013-10-03 18:16:47 -0700722static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800723 const struct nlattr **a, bool is_mask,
724 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700725{
Andy Zhou971427f32014-09-15 19:37:25 -0700726 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
727 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
728
729 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
730 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
731 }
732
733 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
734 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
735
736 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
737 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
738 }
739
Pravin B Shelare6445712013-10-03 18:16:47 -0700740 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
741 SW_FLOW_KEY_PUT(match, phy.priority,
742 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
743 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
744 }
745
746 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
747 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
748
Jesse Gross426cda52014-10-06 05:08:38 -0700749 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700750 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -0700751 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800752 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -0700753 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -0700754 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700755 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700756
757 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
758 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
759 } else if (!is_mask) {
760 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
761 }
762
763 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
764 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
765
766 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
767 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
768 }
769 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
770 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100771 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700772 return -EINVAL;
773 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
774 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700775
776 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
777 ovs_ct_verify(OVS_KEY_ATTR_CT_STATE)) {
778 u8 ct_state = nla_get_u8(a[OVS_KEY_ATTR_CT_STATE]);
779
780 SW_FLOW_KEY_PUT(match, ct.state, ct_state, is_mask);
781 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
782 }
783 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
784 ovs_ct_verify(OVS_KEY_ATTR_CT_ZONE)) {
785 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
786
787 SW_FLOW_KEY_PUT(match, ct.zone, ct_zone, is_mask);
788 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
789 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700790 return 0;
791}
792
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700793static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800794 const struct nlattr **a, bool is_mask,
795 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700796{
797 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700798
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800799 err = metadata_from_nlattrs(match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -0700800 if (err)
801 return err;
802
803 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
804 const struct ovs_key_ethernet *eth_key;
805
806 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
807 SW_FLOW_KEY_MEMCPY(match, eth.src,
808 eth_key->eth_src, ETH_ALEN, is_mask);
809 SW_FLOW_KEY_MEMCPY(match, eth.dst,
810 eth_key->eth_dst, ETH_ALEN, is_mask);
811 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
812 }
813
814 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
815 __be16 tci;
816
817 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
818 if (!(tci & htons(VLAN_TAG_PRESENT))) {
819 if (is_mask)
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800820 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700821 else
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800822 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700823
824 return -EINVAL;
825 }
826
827 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
828 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
Pravin B Shelara85311b2014-10-19 12:03:40 -0700829 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700830
831 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
832 __be16 eth_type;
833
834 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
835 if (is_mask) {
836 /* Always exact match EtherType. */
837 eth_type = htons(0xffff);
Alexander Duyck6713fc92015-05-04 14:34:05 -0700838 } else if (!eth_proto_is_802_3(eth_type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800839 OVS_NLERR(log, "EtherType %x is less than min %x",
840 ntohs(eth_type), ETH_P_802_3_MIN);
Pravin B Shelare6445712013-10-03 18:16:47 -0700841 return -EINVAL;
842 }
843
844 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
845 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
846 } else if (!is_mask) {
847 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
848 }
849
850 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
851 const struct ovs_key_ipv4 *ipv4_key;
852
853 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
854 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800855 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
856 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700857 return -EINVAL;
858 }
859 SW_FLOW_KEY_PUT(match, ip.proto,
860 ipv4_key->ipv4_proto, is_mask);
861 SW_FLOW_KEY_PUT(match, ip.tos,
862 ipv4_key->ipv4_tos, is_mask);
863 SW_FLOW_KEY_PUT(match, ip.ttl,
864 ipv4_key->ipv4_ttl, is_mask);
865 SW_FLOW_KEY_PUT(match, ip.frag,
866 ipv4_key->ipv4_frag, is_mask);
867 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
868 ipv4_key->ipv4_src, is_mask);
869 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
870 ipv4_key->ipv4_dst, is_mask);
871 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
872 }
873
874 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
875 const struct ovs_key_ipv6 *ipv6_key;
876
877 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
878 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800879 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
880 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700881 return -EINVAL;
882 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800883
Joe Stringerd3052bb2014-11-19 13:54:49 -0800884 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
David S. Miller14591432014-11-21 22:28:24 -0500885 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800886 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
887 return -EINVAL;
888 }
889
Pravin B Shelare6445712013-10-03 18:16:47 -0700890 SW_FLOW_KEY_PUT(match, ipv6.label,
891 ipv6_key->ipv6_label, is_mask);
892 SW_FLOW_KEY_PUT(match, ip.proto,
893 ipv6_key->ipv6_proto, is_mask);
894 SW_FLOW_KEY_PUT(match, ip.tos,
895 ipv6_key->ipv6_tclass, is_mask);
896 SW_FLOW_KEY_PUT(match, ip.ttl,
897 ipv6_key->ipv6_hlimit, is_mask);
898 SW_FLOW_KEY_PUT(match, ip.frag,
899 ipv6_key->ipv6_frag, is_mask);
900 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
901 ipv6_key->ipv6_src,
902 sizeof(match->key->ipv6.addr.src),
903 is_mask);
904 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
905 ipv6_key->ipv6_dst,
906 sizeof(match->key->ipv6.addr.dst),
907 is_mask);
908
909 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
910 }
911
912 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
913 const struct ovs_key_arp *arp_key;
914
915 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
916 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800917 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -0700918 arp_key->arp_op);
919 return -EINVAL;
920 }
921
922 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
923 arp_key->arp_sip, is_mask);
924 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
925 arp_key->arp_tip, is_mask);
926 SW_FLOW_KEY_PUT(match, ip.proto,
927 ntohs(arp_key->arp_op), is_mask);
928 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
929 arp_key->arp_sha, ETH_ALEN, is_mask);
930 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
931 arp_key->arp_tha, ETH_ALEN, is_mask);
932
933 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
934 }
935
Simon Horman25cd9ba2014-10-06 05:05:13 -0700936 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
937 const struct ovs_key_mpls *mpls_key;
938
939 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
940 SW_FLOW_KEY_PUT(match, mpls.top_lse,
941 mpls_key->mpls_lse, is_mask);
942
943 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
944 }
945
Pravin B Shelare6445712013-10-03 18:16:47 -0700946 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
947 const struct ovs_key_tcp *tcp_key;
948
949 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700950 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
951 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700952 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
953 }
954
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700955 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -0700956 SW_FLOW_KEY_PUT(match, tp.flags,
957 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
958 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700959 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
960 }
961
Pravin B Shelare6445712013-10-03 18:16:47 -0700962 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
963 const struct ovs_key_udp *udp_key;
964
965 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700966 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
967 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700968 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
969 }
970
971 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
972 const struct ovs_key_sctp *sctp_key;
973
974 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700975 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
976 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700977 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
978 }
979
980 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
981 const struct ovs_key_icmp *icmp_key;
982
983 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700984 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700985 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700986 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700987 htons(icmp_key->icmp_code), is_mask);
988 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
989 }
990
991 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
992 const struct ovs_key_icmpv6 *icmpv6_key;
993
994 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700995 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -0700996 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700997 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -0700998 htons(icmpv6_key->icmpv6_code), is_mask);
999 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1000 }
1001
1002 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1003 const struct ovs_key_nd *nd_key;
1004
1005 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1006 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1007 nd_key->nd_target,
1008 sizeof(match->key->ipv6.nd.target),
1009 is_mask);
1010 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1011 nd_key->nd_sll, ETH_ALEN, is_mask);
1012 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1013 nd_key->nd_tll, ETH_ALEN, is_mask);
1014 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1015 }
1016
Jesse Gross426cda52014-10-06 05:08:38 -07001017 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001018 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001019 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001020 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001021 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001022
1023 return 0;
1024}
1025
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001026static void nlattr_set(struct nlattr *attr, u8 val,
1027 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001028{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001029 struct nlattr *nla;
1030 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001031
Pravin B Shelarf47de062014-10-16 21:55:45 -07001032 /* The nlattr stream should already have been validated */
1033 nla_for_each_nested(nla, attr, rem) {
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001034 if (tbl && tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
1035 nlattr_set(nla, val, tbl[nla_type(nla)].next);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001036 else
1037 memset(nla_data(nla), val, nla_len(nla));
1038 }
1039}
1040
1041static void mask_set_nlattr(struct nlattr *attr, u8 val)
1042{
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001043 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001044}
1045
1046/**
1047 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1048 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1049 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1050 * does not include any don't care bit.
1051 * @match: receives the extracted flow match information.
1052 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1053 * sequence. The fields should of the packet that triggered the creation
1054 * of this flow.
1055 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1056 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001057 * @log: Boolean to allow kernel error logging. Normally true, but when
1058 * probing for feature compatibility this should be passed in as false to
1059 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001060 */
1061int ovs_nla_get_match(struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001062 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001063 const struct nlattr *nla_mask,
1064 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001065{
1066 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1067 const struct nlattr *encap;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001068 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001069 u64 key_attrs = 0;
1070 u64 mask_attrs = 0;
1071 bool encap_valid = false;
1072 int err;
1073
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001074 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001075 if (err)
1076 return err;
1077
1078 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
1079 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
1080 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1081 __be16 tci;
1082
1083 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
1084 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001085 OVS_NLERR(log, "Invalid Vlan frame.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001086 return -EINVAL;
1087 }
1088
1089 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1090 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1091 encap = a[OVS_KEY_ATTR_ENCAP];
1092 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1093 encap_valid = true;
1094
1095 if (tci & htons(VLAN_TAG_PRESENT)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001096 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001097 if (err)
1098 return err;
1099 } else if (!tci) {
1100 /* Corner case for truncated 802.1Q header. */
1101 if (nla_len(encap)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001102 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001103 return -EINVAL;
1104 }
1105 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001106 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
Pravin B Shelare6445712013-10-03 18:16:47 -07001107 return -EINVAL;
1108 }
1109 }
1110
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001111 err = ovs_key_from_nlattrs(match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001112 if (err)
1113 return err;
1114
Pravin B Shelara85311b2014-10-19 12:03:40 -07001115 if (match->mask) {
1116 if (!nla_mask) {
1117 /* Create an exact match mask. We need to set to 0xff
1118 * all the 'match->mask' fields that have been touched
1119 * in 'match->key'. We cannot simply memset
1120 * 'match->mask', because padding bytes and fields not
1121 * specified in 'match->key' should be left to 0.
1122 * Instead, we use a stream of netlink attributes,
1123 * copied from 'key' and set to 0xff.
1124 * ovs_key_from_nlattrs() will take care of filling
1125 * 'match->mask' appropriately.
1126 */
1127 newmask = kmemdup(nla_key,
1128 nla_total_size(nla_len(nla_key)),
1129 GFP_KERNEL);
1130 if (!newmask)
1131 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001132
Pravin B Shelara85311b2014-10-19 12:03:40 -07001133 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001134
Pravin B Shelara85311b2014-10-19 12:03:40 -07001135 /* The userspace does not send tunnel attributes that
1136 * are 0, but we should not wildcard them nonetheless.
1137 */
Jiri Bencc1ea5d62015-08-20 13:56:23 +02001138 if (match->key->tun_key.u.ipv4.dst)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001139 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1140 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001141
Pravin B Shelara85311b2014-10-19 12:03:40 -07001142 nla_mask = newmask;
1143 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001144
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001145 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001146 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001147 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001148
Pravin B Shelara85311b2014-10-19 12:03:40 -07001149 /* Always match on tci. */
1150 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1151
Pravin B Shelarf47de062014-10-16 21:55:45 -07001152 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001153 __be16 eth_type = 0;
1154 __be16 tci = 0;
1155
1156 if (!encap_valid) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001157 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
Pravin B Shelarf47de062014-10-16 21:55:45 -07001158 err = -EINVAL;
1159 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001160 }
1161
1162 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1163 if (a[OVS_KEY_ATTR_ETHERTYPE])
1164 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1165
1166 if (eth_type == htons(0xffff)) {
1167 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1168 encap = a[OVS_KEY_ATTR_ENCAP];
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001169 err = parse_flow_mask_nlattrs(encap, a,
1170 &mask_attrs, log);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001171 if (err)
1172 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001173 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001174 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1175 ntohs(eth_type));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001176 err = -EINVAL;
1177 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001178 }
1179
1180 if (a[OVS_KEY_ATTR_VLAN])
1181 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1182
1183 if (!(tci & htons(VLAN_TAG_PRESENT))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001184 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1185 ntohs(tci));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001186 err = -EINVAL;
1187 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001188 }
1189 }
1190
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001191 err = ovs_key_from_nlattrs(match, mask_attrs, a, true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001192 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001193 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001194 }
1195
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001196 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001197 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001198
Pravin B Shelarf47de062014-10-16 21:55:45 -07001199free_newmask:
1200 kfree(newmask);
1201 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001202}
1203
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001204static size_t get_ufid_len(const struct nlattr *attr, bool log)
1205{
1206 size_t len;
1207
1208 if (!attr)
1209 return 0;
1210
1211 len = nla_len(attr);
1212 if (len < 1 || len > MAX_UFID_LENGTH) {
1213 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1214 nla_len(attr), MAX_UFID_LENGTH);
1215 return 0;
1216 }
1217
1218 return len;
1219}
1220
1221/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1222 * or false otherwise.
1223 */
1224bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1225 bool log)
1226{
1227 sfid->ufid_len = get_ufid_len(attr, log);
1228 if (sfid->ufid_len)
1229 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1230
1231 return sfid->ufid_len;
1232}
1233
1234int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1235 const struct sw_flow_key *key, bool log)
1236{
1237 struct sw_flow_key *new_key;
1238
1239 if (ovs_nla_get_ufid(sfid, ufid, log))
1240 return 0;
1241
1242 /* If UFID was not provided, use unmasked key. */
1243 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1244 if (!new_key)
1245 return -ENOMEM;
1246 memcpy(new_key, key, sizeof(*key));
1247 sfid->unmasked_key = new_key;
1248
1249 return 0;
1250}
1251
1252u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1253{
1254 return attr ? nla_get_u32(attr) : 0;
1255}
1256
Pravin B Shelare6445712013-10-03 18:16:47 -07001257/**
1258 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001259 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -07001260 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1261 * sequence.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001262 * @log: Boolean to allow kernel error logging. Normally true, but when
1263 * probing for feature compatibility this should be passed in as false to
1264 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001265 *
1266 * This parses a series of Netlink attributes that form a flow key, which must
1267 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1268 * get the metadata, that is, the parts of the flow key that cannot be
1269 * extracted from the packet itself.
1270 */
1271
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001272int ovs_nla_get_flow_metadata(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001273 struct sw_flow_key *key,
1274 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001275{
Pravin B Shelare6445712013-10-03 18:16:47 -07001276 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001277 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001278 u64 attrs = 0;
1279 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001280
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001281 err = parse_flow_nlattrs(attr, a, &attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001282 if (err)
1283 return -EINVAL;
1284
1285 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001286 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001287
Joe Stringer7f8a4362015-08-26 11:31:48 -07001288 memset(&key->ct, 0, sizeof(key->ct));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001289 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001290
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001291 return metadata_from_nlattrs(&match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001292}
1293
Joe Stringer5b4237b2015-01-21 16:42:48 -08001294static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1295 const struct sw_flow_key *output, bool is_mask,
1296 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001297{
1298 struct ovs_key_ethernet *eth_key;
1299 struct nlattr *nla, *encap;
Pravin B Shelare6445712013-10-03 18:16:47 -07001300
Andy Zhou971427f32014-09-15 19:37:25 -07001301 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1302 goto nla_put_failure;
1303
1304 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1305 goto nla_put_failure;
1306
Pravin B Shelare6445712013-10-03 18:16:47 -07001307 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1308 goto nla_put_failure;
1309
Jiri Bencc1ea5d62015-08-20 13:56:23 +02001310 if ((swkey->tun_key.u.ipv4.dst || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001311 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001312
1313 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001314 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001315
1316 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1317 swkey->tun_opts_len))
1318 goto nla_put_failure;
1319 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001320
1321 if (swkey->phy.in_port == DP_MAX_PORTS) {
1322 if (is_mask && (output->phy.in_port == 0xffff))
1323 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1324 goto nla_put_failure;
1325 } else {
1326 u16 upper_u16;
1327 upper_u16 = !is_mask ? 0 : 0xffff;
1328
1329 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1330 (upper_u16 << 16) | output->phy.in_port))
1331 goto nla_put_failure;
1332 }
1333
1334 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1335 goto nla_put_failure;
1336
Joe Stringer7f8a4362015-08-26 11:31:48 -07001337 if (ovs_ct_put_key(output, skb))
1338 goto nla_put_failure;
1339
Pravin B Shelare6445712013-10-03 18:16:47 -07001340 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1341 if (!nla)
1342 goto nla_put_failure;
1343
1344 eth_key = nla_data(nla);
Joe Perches8c63ff02014-02-18 11:15:45 -08001345 ether_addr_copy(eth_key->eth_src, output->eth.src);
1346 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001347
1348 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1349 __be16 eth_type;
1350 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1351 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1352 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1353 goto nla_put_failure;
1354 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1355 if (!swkey->eth.tci)
1356 goto unencap;
1357 } else
1358 encap = NULL;
1359
1360 if (swkey->eth.type == htons(ETH_P_802_2)) {
1361 /*
1362 * Ethertype 802.2 is represented in the netlink with omitted
1363 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1364 * 0xffff in the mask attribute. Ethertype can also
1365 * be wildcarded.
1366 */
1367 if (is_mask && output->eth.type)
1368 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1369 output->eth.type))
1370 goto nla_put_failure;
1371 goto unencap;
1372 }
1373
1374 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1375 goto nla_put_failure;
1376
1377 if (swkey->eth.type == htons(ETH_P_IP)) {
1378 struct ovs_key_ipv4 *ipv4_key;
1379
1380 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1381 if (!nla)
1382 goto nla_put_failure;
1383 ipv4_key = nla_data(nla);
1384 ipv4_key->ipv4_src = output->ipv4.addr.src;
1385 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1386 ipv4_key->ipv4_proto = output->ip.proto;
1387 ipv4_key->ipv4_tos = output->ip.tos;
1388 ipv4_key->ipv4_ttl = output->ip.ttl;
1389 ipv4_key->ipv4_frag = output->ip.frag;
1390 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1391 struct ovs_key_ipv6 *ipv6_key;
1392
1393 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1394 if (!nla)
1395 goto nla_put_failure;
1396 ipv6_key = nla_data(nla);
1397 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1398 sizeof(ipv6_key->ipv6_src));
1399 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1400 sizeof(ipv6_key->ipv6_dst));
1401 ipv6_key->ipv6_label = output->ipv6.label;
1402 ipv6_key->ipv6_proto = output->ip.proto;
1403 ipv6_key->ipv6_tclass = output->ip.tos;
1404 ipv6_key->ipv6_hlimit = output->ip.ttl;
1405 ipv6_key->ipv6_frag = output->ip.frag;
1406 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1407 swkey->eth.type == htons(ETH_P_RARP)) {
1408 struct ovs_key_arp *arp_key;
1409
1410 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1411 if (!nla)
1412 goto nla_put_failure;
1413 arp_key = nla_data(nla);
1414 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1415 arp_key->arp_sip = output->ipv4.addr.src;
1416 arp_key->arp_tip = output->ipv4.addr.dst;
1417 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001418 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1419 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001420 } else if (eth_p_mpls(swkey->eth.type)) {
1421 struct ovs_key_mpls *mpls_key;
1422
1423 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1424 if (!nla)
1425 goto nla_put_failure;
1426 mpls_key = nla_data(nla);
1427 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001428 }
1429
1430 if ((swkey->eth.type == htons(ETH_P_IP) ||
1431 swkey->eth.type == htons(ETH_P_IPV6)) &&
1432 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1433
1434 if (swkey->ip.proto == IPPROTO_TCP) {
1435 struct ovs_key_tcp *tcp_key;
1436
1437 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1438 if (!nla)
1439 goto nla_put_failure;
1440 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001441 tcp_key->tcp_src = output->tp.src;
1442 tcp_key->tcp_dst = output->tp.dst;
1443 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1444 output->tp.flags))
1445 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001446 } else if (swkey->ip.proto == IPPROTO_UDP) {
1447 struct ovs_key_udp *udp_key;
1448
1449 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1450 if (!nla)
1451 goto nla_put_failure;
1452 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001453 udp_key->udp_src = output->tp.src;
1454 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001455 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1456 struct ovs_key_sctp *sctp_key;
1457
1458 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1459 if (!nla)
1460 goto nla_put_failure;
1461 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001462 sctp_key->sctp_src = output->tp.src;
1463 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001464 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1465 swkey->ip.proto == IPPROTO_ICMP) {
1466 struct ovs_key_icmp *icmp_key;
1467
1468 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1469 if (!nla)
1470 goto nla_put_failure;
1471 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001472 icmp_key->icmp_type = ntohs(output->tp.src);
1473 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001474 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1475 swkey->ip.proto == IPPROTO_ICMPV6) {
1476 struct ovs_key_icmpv6 *icmpv6_key;
1477
1478 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1479 sizeof(*icmpv6_key));
1480 if (!nla)
1481 goto nla_put_failure;
1482 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001483 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1484 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001485
1486 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1487 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1488 struct ovs_key_nd *nd_key;
1489
1490 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1491 if (!nla)
1492 goto nla_put_failure;
1493 nd_key = nla_data(nla);
1494 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1495 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001496 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1497 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001498 }
1499 }
1500 }
1501
1502unencap:
1503 if (encap)
1504 nla_nest_end(skb, encap);
1505
1506 return 0;
1507
1508nla_put_failure:
1509 return -EMSGSIZE;
1510}
1511
Joe Stringer5b4237b2015-01-21 16:42:48 -08001512int ovs_nla_put_key(const struct sw_flow_key *swkey,
1513 const struct sw_flow_key *output, int attr, bool is_mask,
1514 struct sk_buff *skb)
1515{
1516 int err;
1517 struct nlattr *nla;
1518
1519 nla = nla_nest_start(skb, attr);
1520 if (!nla)
1521 return -EMSGSIZE;
1522 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1523 if (err)
1524 return err;
1525 nla_nest_end(skb, nla);
1526
1527 return 0;
1528}
1529
1530/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001531int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08001532{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001533 if (ovs_identifier_is_ufid(&flow->id))
1534 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1535 flow->id.ufid);
1536
1537 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1538 OVS_FLOW_ATTR_KEY, false, skb);
1539}
1540
1541/* Called with ovs_mutex or RCU read lock. */
1542int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1543{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08001544 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08001545 OVS_FLOW_ATTR_KEY, false, skb);
1546}
1547
1548/* Called with ovs_mutex or RCU read lock. */
1549int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1550{
1551 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1552 OVS_FLOW_ATTR_MASK, true, skb);
1553}
1554
Pravin B Shelare6445712013-10-03 18:16:47 -07001555#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1556
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001557static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001558{
1559 struct sw_flow_actions *sfa;
1560
Jesse Gross426cda52014-10-06 05:08:38 -07001561 if (size > MAX_ACTIONS_BUFSIZE) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001562 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001563 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001564 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001565
1566 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1567 if (!sfa)
1568 return ERR_PTR(-ENOMEM);
1569
1570 sfa->actions_len = 0;
1571 return sfa;
1572}
1573
Thomas Graf34ae9322015-07-21 10:44:03 +02001574static void ovs_nla_free_set_action(const struct nlattr *a)
1575{
1576 const struct nlattr *ovs_key = nla_data(a);
1577 struct ovs_tunnel_info *ovs_tun;
1578
1579 switch (nla_type(ovs_key)) {
1580 case OVS_KEY_ATTR_TUNNEL_INFO:
1581 ovs_tun = nla_data(ovs_key);
1582 dst_release((struct dst_entry *)ovs_tun->tun_dst);
1583 break;
1584 }
1585}
1586
Pravin B Shelare6445712013-10-03 18:16:47 -07001587void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1588{
Thomas Graf34ae9322015-07-21 10:44:03 +02001589 const struct nlattr *a;
1590 int rem;
1591
1592 if (!sf_acts)
1593 return;
1594
1595 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1596 switch (nla_type(a)) {
1597 case OVS_ACTION_ATTR_SET:
1598 ovs_nla_free_set_action(a);
1599 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001600 case OVS_ACTION_ATTR_CT:
1601 ovs_ct_free_action(a);
1602 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02001603 }
1604 }
1605
1606 kfree(sf_acts);
1607}
1608
1609static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1610{
1611 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1612}
1613
1614/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1615 * The caller must hold rcu_read_lock for this to be sensible. */
1616void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
1617{
1618 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07001619}
1620
1621static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001622 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001623{
1624
1625 struct sw_flow_actions *acts;
1626 int new_acts_size;
1627 int req_size = NLA_ALIGN(attr_len);
1628 int next_offset = offsetof(struct sw_flow_actions, actions) +
1629 (*sfa)->actions_len;
1630
1631 if (req_size <= (ksize(*sfa) - next_offset))
1632 goto out;
1633
1634 new_acts_size = ksize(*sfa) * 2;
1635
1636 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1637 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1638 return ERR_PTR(-EMSGSIZE);
1639 new_acts_size = MAX_ACTIONS_BUFSIZE;
1640 }
1641
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001642 acts = nla_alloc_flow_actions(new_acts_size, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001643 if (IS_ERR(acts))
1644 return (void *)acts;
1645
1646 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1647 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07001648 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001649 kfree(*sfa);
1650 *sfa = acts;
1651
1652out:
1653 (*sfa)->actions_len += req_size;
1654 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1655}
1656
Jesse Grossf0b128c2014-10-03 15:35:31 -07001657static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001658 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001659{
1660 struct nlattr *a;
1661
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001662 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001663 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001664 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001665
1666 a->nla_type = attrtype;
1667 a->nla_len = nla_attr_size(len);
1668
1669 if (data)
1670 memcpy(nla_data(a), data, len);
1671 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1672
Jesse Grossf0b128c2014-10-03 15:35:31 -07001673 return a;
1674}
1675
Joe Stringer7f8a4362015-08-26 11:31:48 -07001676int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
1677 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07001678{
1679 struct nlattr *a;
1680
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001681 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001682
Fabian Frederickf35423c12014-11-14 19:32:58 +01001683 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07001684}
1685
1686static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001687 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001688{
1689 int used = (*sfa)->actions_len;
1690 int err;
1691
Joe Stringer7f8a4362015-08-26 11:31:48 -07001692 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001693 if (err)
1694 return err;
1695
1696 return used;
1697}
1698
1699static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1700 int st_offset)
1701{
1702 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1703 st_offset);
1704
1705 a->nla_len = sfa->actions_len - st_offset;
1706}
1707
Joe Stringer7f8a4362015-08-26 11:31:48 -07001708static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001709 const struct sw_flow_key *key,
1710 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001711 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001712
Joe Stringer7f8a4362015-08-26 11:31:48 -07001713static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
Pravin B Shelare6445712013-10-03 18:16:47 -07001714 const struct sw_flow_key *key, int depth,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001715 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001716 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001717{
1718 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1719 const struct nlattr *probability, *actions;
1720 const struct nlattr *a;
1721 int rem, start, err, st_acts;
1722
1723 memset(attrs, 0, sizeof(attrs));
1724 nla_for_each_nested(a, attr, rem) {
1725 int type = nla_type(a);
1726 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1727 return -EINVAL;
1728 attrs[type] = a;
1729 }
1730 if (rem)
1731 return -EINVAL;
1732
1733 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1734 if (!probability || nla_len(probability) != sizeof(u32))
1735 return -EINVAL;
1736
1737 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1738 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1739 return -EINVAL;
1740
1741 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001742 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001743 if (start < 0)
1744 return start;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001745 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1746 nla_data(probability), sizeof(u32), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001747 if (err)
1748 return err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001749 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001750 if (st_acts < 0)
1751 return st_acts;
1752
Joe Stringer7f8a4362015-08-26 11:31:48 -07001753 err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001754 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001755 if (err)
1756 return err;
1757
1758 add_nested_action_end(*sfa, st_acts);
1759 add_nested_action_end(*sfa, start);
1760
1761 return 0;
1762}
1763
Pravin B Shelare6445712013-10-03 18:16:47 -07001764void ovs_match_init(struct sw_flow_match *match,
1765 struct sw_flow_key *key,
1766 struct sw_flow_mask *mask)
1767{
1768 memset(match, 0, sizeof(*match));
1769 match->key = key;
1770 match->mask = mask;
1771
1772 memset(key, 0, sizeof(*key));
1773
1774 if (mask) {
1775 memset(&mask->key, 0, sizeof(mask->key));
1776 mask->range.start = mask->range.end = 0;
1777 }
1778}
1779
Thomas Grafd91641d2015-01-15 03:53:57 +01001780static int validate_geneve_opts(struct sw_flow_key *key)
1781{
1782 struct geneve_opt *option;
1783 int opts_len = key->tun_opts_len;
1784 bool crit_opt = false;
1785
1786 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
1787 while (opts_len > 0) {
1788 int len;
1789
1790 if (opts_len < sizeof(*option))
1791 return -EINVAL;
1792
1793 len = sizeof(*option) + option->length * 4;
1794 if (len > opts_len)
1795 return -EINVAL;
1796
1797 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1798
1799 option = (struct geneve_opt *)((u8 *)option + len);
1800 opts_len -= len;
1801 };
1802
1803 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1804
1805 return 0;
1806}
1807
Pravin B Shelare6445712013-10-03 18:16:47 -07001808static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001809 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001810{
1811 struct sw_flow_match match;
1812 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02001813 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02001814 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02001815 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001816 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01001817 int err = 0, start, opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07001818
1819 ovs_match_init(&match, &key, NULL);
Thomas Graf1dd144c2015-01-15 03:53:59 +01001820 opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
1821 if (opts_type < 0)
1822 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07001823
Jesse Grossf5796682014-10-03 15:35:33 -07001824 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01001825 switch (opts_type) {
1826 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1827 err = validate_geneve_opts(&key);
1828 if (err < 0)
1829 return err;
1830 break;
1831 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
1832 break;
1833 }
Jesse Grossf5796682014-10-03 15:35:33 -07001834 };
1835
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001836 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001837 if (start < 0)
1838 return start;
1839
Thomas Graf34ae9322015-07-21 10:44:03 +02001840 tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
1841 if (!tun_dst)
1842 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001843
Thomas Graf34ae9322015-07-21 10:44:03 +02001844 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1845 sizeof(*ovs_tun), log);
1846 if (IS_ERR(a)) {
1847 dst_release((struct dst_entry *)tun_dst);
1848 return PTR_ERR(a);
1849 }
1850
1851 ovs_tun = nla_data(a);
1852 ovs_tun->tun_dst = tun_dst;
1853
1854 tun_info = &tun_dst->u.tun_info;
1855 tun_info->mode = IP_TUNNEL_INFO_TX;
Thomas Graf1d8fff92015-07-21 10:43:54 +02001856 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07001857 tun_info->options_len = key.tun_opts_len;
1858
1859 if (tun_info->options_len) {
1860 /* We need to store the options in the action itself since
1861 * everything else will go away after flow setup. We can append
1862 * it to tun_info and then point there.
1863 */
Thomas Grafd91641d2015-01-15 03:53:57 +01001864 memcpy((tun_info + 1),
1865 TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
1866 tun_info->options = (tun_info + 1);
Jesse Grossf5796682014-10-03 15:35:33 -07001867 } else {
1868 tun_info->options = NULL;
1869 }
Jesse Grossf0b128c2014-10-03 15:35:31 -07001870
Pravin B Shelare6445712013-10-03 18:16:47 -07001871 add_nested_action_end(*sfa, start);
1872
1873 return err;
1874}
1875
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001876/* Return false if there are any non-masked bits set.
1877 * Mask follows data immediately, before any netlink padding.
1878 */
1879static bool validate_masked(u8 *data, int len)
1880{
1881 u8 *mask = data + len;
1882
1883 while (len--)
1884 if (*data++ & ~*mask++)
1885 return false;
1886
1887 return true;
1888}
1889
Pravin B Shelare6445712013-10-03 18:16:47 -07001890static int validate_set(const struct nlattr *a,
1891 const struct sw_flow_key *flow_key,
1892 struct sw_flow_actions **sfa,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001893 bool *skip_copy, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001894{
1895 const struct nlattr *ovs_key = nla_data(a);
1896 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001897 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001898
1899 /* There can be only one key in a action */
1900 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1901 return -EINVAL;
1902
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001903 key_len = nla_len(ovs_key);
1904 if (masked)
1905 key_len /= 2;
1906
Pravin B Shelare6445712013-10-03 18:16:47 -07001907 if (key_type > OVS_KEY_ATTR_MAX ||
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001908 (ovs_key_lens[key_type].len != key_len &&
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001909 ovs_key_lens[key_type].len != OVS_ATTR_NESTED))
Pravin B Shelare6445712013-10-03 18:16:47 -07001910 return -EINVAL;
1911
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001912 if (masked && !validate_masked(nla_data(ovs_key), key_len))
1913 return -EINVAL;
1914
Pravin B Shelare6445712013-10-03 18:16:47 -07001915 switch (key_type) {
1916 const struct ovs_key_ipv4 *ipv4_key;
1917 const struct ovs_key_ipv6 *ipv6_key;
1918 int err;
1919
1920 case OVS_KEY_ATTR_PRIORITY:
1921 case OVS_KEY_ATTR_SKB_MARK:
1922 case OVS_KEY_ATTR_ETHERNET:
1923 break;
1924
1925 case OVS_KEY_ATTR_TUNNEL:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001926 if (eth_p_mpls(eth_type))
1927 return -EINVAL;
1928
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001929 if (masked)
1930 return -EINVAL; /* Masked tunnel set not supported. */
1931
1932 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001933 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001934 if (err)
1935 return err;
1936 break;
1937
1938 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001939 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07001940 return -EINVAL;
1941
Pravin B Shelare6445712013-10-03 18:16:47 -07001942 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07001943
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001944 if (masked) {
1945 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07001946
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001947 /* Non-writeable fields. */
1948 if (mask->ipv4_proto || mask->ipv4_frag)
1949 return -EINVAL;
1950 } else {
1951 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1952 return -EINVAL;
1953
1954 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1955 return -EINVAL;
1956 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001957 break;
1958
1959 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001960 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07001961 return -EINVAL;
1962
Pravin B Shelare6445712013-10-03 18:16:47 -07001963 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07001964
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001965 if (masked) {
1966 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07001967
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001968 /* Non-writeable fields. */
1969 if (mask->ipv6_proto || mask->ipv6_frag)
1970 return -EINVAL;
1971
1972 /* Invalid bits in the flow label mask? */
1973 if (ntohl(mask->ipv6_label) & 0xFFF00000)
1974 return -EINVAL;
1975 } else {
1976 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1977 return -EINVAL;
1978
1979 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1980 return -EINVAL;
1981 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001982 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1983 return -EINVAL;
1984
1985 break;
1986
1987 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001988 if ((eth_type != htons(ETH_P_IP) &&
1989 eth_type != htons(ETH_P_IPV6)) ||
1990 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07001991 return -EINVAL;
1992
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001993 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07001994
1995 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001996 if ((eth_type != htons(ETH_P_IP) &&
1997 eth_type != htons(ETH_P_IPV6)) ||
1998 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07001999 return -EINVAL;
2000
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002001 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002002
2003 case OVS_KEY_ATTR_MPLS:
2004 if (!eth_p_mpls(eth_type))
2005 return -EINVAL;
2006 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002007
2008 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002009 if ((eth_type != htons(ETH_P_IP) &&
2010 eth_type != htons(ETH_P_IPV6)) ||
2011 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002012 return -EINVAL;
2013
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002014 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002015
2016 default:
2017 return -EINVAL;
2018 }
2019
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002020 /* Convert non-masked non-tunnel set actions to masked set actions. */
2021 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2022 int start, len = key_len * 2;
2023 struct nlattr *at;
2024
2025 *skip_copy = true;
2026
2027 start = add_nested_action_start(sfa,
2028 OVS_ACTION_ATTR_SET_TO_MASKED,
2029 log);
2030 if (start < 0)
2031 return start;
2032
2033 at = __add_action(sfa, key_type, NULL, len, log);
2034 if (IS_ERR(at))
2035 return PTR_ERR(at);
2036
2037 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2038 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2039 /* Clear non-writeable bits from otherwise writeable fields. */
2040 if (key_type == OVS_KEY_ATTR_IPV6) {
2041 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2042
2043 mask->ipv6_label &= htonl(0x000FFFFF);
2044 }
2045 add_nested_action_end(*sfa, start);
2046 }
2047
Pravin B Shelare6445712013-10-03 18:16:47 -07002048 return 0;
2049}
2050
2051static int validate_userspace(const struct nlattr *attr)
2052{
2053 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2054 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2055 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002056 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002057 };
2058 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2059 int error;
2060
2061 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
2062 attr, userspace_policy);
2063 if (error)
2064 return error;
2065
2066 if (!a[OVS_USERSPACE_ATTR_PID] ||
2067 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2068 return -EINVAL;
2069
2070 return 0;
2071}
2072
2073static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002074 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002075{
2076 int totlen = NLA_ALIGN(from->nla_len);
2077 struct nlattr *to;
2078
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002079 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002080 if (IS_ERR(to))
2081 return PTR_ERR(to);
2082
2083 memcpy(to, from, totlen);
2084 return 0;
2085}
2086
Joe Stringer7f8a4362015-08-26 11:31:48 -07002087static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002088 const struct sw_flow_key *key,
2089 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002090 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002091{
2092 const struct nlattr *a;
2093 int rem, err;
2094
2095 if (depth >= SAMPLE_ACTION_DEPTH)
2096 return -EOVERFLOW;
2097
2098 nla_for_each_nested(a, attr, rem) {
2099 /* Expected argument lengths, (u32)-1 for variable length. */
2100 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2101 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002102 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002103 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002104 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2105 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002106 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2107 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2108 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002109 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002110 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002111 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2112 [OVS_ACTION_ATTR_CT] = (u32)-1,
Pravin B Shelare6445712013-10-03 18:16:47 -07002113 };
2114 const struct ovs_action_push_vlan *vlan;
2115 int type = nla_type(a);
2116 bool skip_copy;
2117
2118 if (type > OVS_ACTION_ATTR_MAX ||
2119 (action_lens[type] != nla_len(a) &&
2120 action_lens[type] != (u32)-1))
2121 return -EINVAL;
2122
2123 skip_copy = false;
2124 switch (type) {
2125 case OVS_ACTION_ATTR_UNSPEC:
2126 return -EINVAL;
2127
2128 case OVS_ACTION_ATTR_USERSPACE:
2129 err = validate_userspace(a);
2130 if (err)
2131 return err;
2132 break;
2133
2134 case OVS_ACTION_ATTR_OUTPUT:
2135 if (nla_get_u32(a) >= DP_MAX_PORTS)
2136 return -EINVAL;
2137 break;
2138
Andy Zhou971427f32014-09-15 19:37:25 -07002139 case OVS_ACTION_ATTR_HASH: {
2140 const struct ovs_action_hash *act_hash = nla_data(a);
2141
2142 switch (act_hash->hash_alg) {
2143 case OVS_HASH_ALG_L4:
2144 break;
2145 default:
2146 return -EINVAL;
2147 }
2148
2149 break;
2150 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002151
2152 case OVS_ACTION_ATTR_POP_VLAN:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002153 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07002154 break;
2155
2156 case OVS_ACTION_ATTR_PUSH_VLAN:
2157 vlan = nla_data(a);
2158 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
2159 return -EINVAL;
2160 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2161 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002162 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07002163 break;
2164
Andy Zhou971427f32014-09-15 19:37:25 -07002165 case OVS_ACTION_ATTR_RECIRC:
2166 break;
2167
Simon Horman25cd9ba2014-10-06 05:05:13 -07002168 case OVS_ACTION_ATTR_PUSH_MPLS: {
2169 const struct ovs_action_push_mpls *mpls = nla_data(a);
2170
Simon Horman25cd9ba2014-10-06 05:05:13 -07002171 if (!eth_p_mpls(mpls->mpls_ethertype))
2172 return -EINVAL;
2173 /* Prohibit push MPLS other than to a white list
2174 * for packets that have a known tag order.
2175 */
2176 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2177 (eth_type != htons(ETH_P_IP) &&
2178 eth_type != htons(ETH_P_IPV6) &&
2179 eth_type != htons(ETH_P_ARP) &&
2180 eth_type != htons(ETH_P_RARP) &&
2181 !eth_p_mpls(eth_type)))
2182 return -EINVAL;
2183 eth_type = mpls->mpls_ethertype;
2184 break;
2185 }
2186
2187 case OVS_ACTION_ATTR_POP_MPLS:
2188 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2189 !eth_p_mpls(eth_type))
2190 return -EINVAL;
2191
2192 /* Disallow subsequent L2.5+ set and mpls_pop actions
2193 * as there is no check here to ensure that the new
2194 * eth_type is valid and thus set actions could
2195 * write off the end of the packet or otherwise
2196 * corrupt it.
2197 *
2198 * Support for these actions is planned using packet
2199 * recirculation.
2200 */
2201 eth_type = htons(0);
2202 break;
2203
Pravin B Shelare6445712013-10-03 18:16:47 -07002204 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002205 err = validate_set(a, key, sfa,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002206 &skip_copy, eth_type, false, log);
2207 if (err)
2208 return err;
2209 break;
2210
2211 case OVS_ACTION_ATTR_SET_MASKED:
2212 err = validate_set(a, key, sfa,
2213 &skip_copy, eth_type, true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002214 if (err)
2215 return err;
2216 break;
2217
2218 case OVS_ACTION_ATTR_SAMPLE:
Joe Stringer7f8a4362015-08-26 11:31:48 -07002219 err = validate_and_copy_sample(net, a, key, depth, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002220 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002221 if (err)
2222 return err;
2223 skip_copy = true;
2224 break;
2225
Joe Stringer7f8a4362015-08-26 11:31:48 -07002226 case OVS_ACTION_ATTR_CT:
2227 err = ovs_ct_copy_action(net, a, key, sfa, log);
2228 if (err)
2229 return err;
2230 skip_copy = true;
2231 break;
2232
Pravin B Shelare6445712013-10-03 18:16:47 -07002233 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002234 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002235 return -EINVAL;
2236 }
2237 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002238 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002239 if (err)
2240 return err;
2241 }
2242 }
2243
2244 if (rem > 0)
2245 return -EINVAL;
2246
2247 return 0;
2248}
2249
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002250/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07002251int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002252 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002253 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07002254{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002255 int err;
2256
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002257 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002258 if (IS_ERR(*sfa))
2259 return PTR_ERR(*sfa);
2260
Joe Stringer8e2fed12015-08-26 11:31:44 -07002261 (*sfa)->orig_len = nla_len(attr);
Joe Stringer7f8a4362015-08-26 11:31:48 -07002262 err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002263 key->eth.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002264 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02002265 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002266
2267 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002268}
2269
Pravin B Shelare6445712013-10-03 18:16:47 -07002270static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
2271{
2272 const struct nlattr *a;
2273 struct nlattr *start;
2274 int err = 0, rem;
2275
2276 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2277 if (!start)
2278 return -EMSGSIZE;
2279
2280 nla_for_each_nested(a, attr, rem) {
2281 int type = nla_type(a);
2282 struct nlattr *st_sample;
2283
2284 switch (type) {
2285 case OVS_SAMPLE_ATTR_PROBABILITY:
2286 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
2287 sizeof(u32), nla_data(a)))
2288 return -EMSGSIZE;
2289 break;
2290 case OVS_SAMPLE_ATTR_ACTIONS:
2291 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2292 if (!st_sample)
2293 return -EMSGSIZE;
2294 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
2295 if (err)
2296 return err;
2297 nla_nest_end(skb, st_sample);
2298 break;
2299 }
2300 }
2301
2302 nla_nest_end(skb, start);
2303 return err;
2304}
2305
2306static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2307{
2308 const struct nlattr *ovs_key = nla_data(a);
2309 int key_type = nla_type(ovs_key);
2310 struct nlattr *start;
2311 int err;
2312
2313 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07002314 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02002315 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2316 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002317
Pravin B Shelare6445712013-10-03 18:16:47 -07002318 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2319 if (!start)
2320 return -EMSGSIZE;
2321
Thomas Graf1d8fff92015-07-21 10:43:54 +02002322 err = ipv4_tun_to_nlattr(skb, &tun_info->key,
Jesse Grossf5796682014-10-03 15:35:33 -07002323 tun_info->options_len ?
2324 tun_info->options : NULL,
2325 tun_info->options_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07002326 if (err)
2327 return err;
2328 nla_nest_end(skb, start);
2329 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002330 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002331 default:
2332 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2333 return -EMSGSIZE;
2334 break;
2335 }
2336
2337 return 0;
2338}
2339
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002340static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2341 struct sk_buff *skb)
2342{
2343 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08002344 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002345 size_t key_len = nla_len(ovs_key) / 2;
2346
2347 /* Revert the conversion we did from a non-masked set action to
2348 * masked set action.
2349 */
Joe Stringerf4f8e732015-03-02 18:49:56 -08002350 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2351 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002352 return -EMSGSIZE;
2353
Joe Stringerf4f8e732015-03-02 18:49:56 -08002354 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2355 return -EMSGSIZE;
2356
2357 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002358 return 0;
2359}
2360
Pravin B Shelare6445712013-10-03 18:16:47 -07002361int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2362{
2363 const struct nlattr *a;
2364 int rem, err;
2365
2366 nla_for_each_attr(a, attr, len, rem) {
2367 int type = nla_type(a);
2368
2369 switch (type) {
2370 case OVS_ACTION_ATTR_SET:
2371 err = set_action_to_attr(a, skb);
2372 if (err)
2373 return err;
2374 break;
2375
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002376 case OVS_ACTION_ATTR_SET_TO_MASKED:
2377 err = masked_set_action_to_set_action_attr(a, skb);
2378 if (err)
2379 return err;
2380 break;
2381
Pravin B Shelare6445712013-10-03 18:16:47 -07002382 case OVS_ACTION_ATTR_SAMPLE:
2383 err = sample_action_to_attr(a, skb);
2384 if (err)
2385 return err;
2386 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002387
2388 case OVS_ACTION_ATTR_CT:
2389 err = ovs_ct_action_to_attr(nla_data(a), skb);
2390 if (err)
2391 return err;
2392 break;
2393
Pravin B Shelare6445712013-10-03 18:16:47 -07002394 default:
2395 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2396 return -EMSGSIZE;
2397 break;
2398 }
2399 }
2400
2401 return 0;
2402}