blob: e22c5bfe857562e43ee4b2b50e5fad016ba42e6f [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 Graf81bfe3c2015-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 Stringerc2ac6672015-08-26 11:31:52 -0700284 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 26);
Joe Stringer41af73e2014-10-18 16:14:14 -0700285
286 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
287 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800288 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700289 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
290 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
291 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
292 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700293 + nla_total_size(1) /* OVS_KEY_ATTR_CT_STATE */
294 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
Joe Stringer182e3042015-08-26 11:31:49 -0700295 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
Joe Stringerc2ac6672015-08-26 11:31:52 -0700296 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABEL */
Joe Stringer41af73e2014-10-18 16:14:14 -0700297 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
298 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
299 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
300 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
301 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
302 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
303 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
304 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
305}
306
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100307static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
308 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
309 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
310 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
311 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
312 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
313 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
314 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
315 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
316 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
317 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
318 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_NESTED },
Thomas Graf1dd144c2015-01-15 03:53:59 +0100319 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED },
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100320};
321
Pravin B Shelare6445712013-10-03 18:16:47 -0700322/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100323static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
324 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
325 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
326 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
327 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
328 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
329 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
330 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
331 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
332 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
333 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
334 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
335 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
336 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
337 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
338 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
339 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
340 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
341 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
342 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
343 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
344 .next = ovs_tunnel_key_lens, },
345 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700346 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u8) },
347 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700348 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
Joe Stringerc2ac6672015-08-26 11:31:52 -0700349 [OVS_KEY_ATTR_CT_LABEL] = { .len = sizeof(struct ovs_key_ct_label) },
Pravin B Shelare6445712013-10-03 18:16:47 -0700350};
351
352static bool is_all_zero(const u8 *fp, size_t size)
353{
354 int i;
355
356 if (!fp)
357 return false;
358
359 for (i = 0; i < size; i++)
360 if (fp[i])
361 return false;
362
363 return true;
364}
365
366static int __parse_flow_nlattrs(const struct nlattr *attr,
367 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800368 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700369{
370 const struct nlattr *nla;
371 u64 attrs;
372 int rem;
373
374 attrs = *attrsp;
375 nla_for_each_nested(nla, attr, rem) {
376 u16 type = nla_type(nla);
377 int expected_len;
378
379 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800380 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700381 type, OVS_KEY_ATTR_MAX);
382 return -EINVAL;
383 }
384
385 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800386 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700387 return -EINVAL;
388 }
389
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100390 expected_len = ovs_key_lens[type].len;
391 if (nla_len(nla) != expected_len && expected_len != OVS_ATTR_NESTED) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800392 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
393 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700394 return -EINVAL;
395 }
396
397 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
398 attrs |= 1 << type;
399 a[type] = nla;
400 }
401 }
402 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800403 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700404 return -EINVAL;
405 }
406
407 *attrsp = attrs;
408 return 0;
409}
410
411static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800412 const struct nlattr *a[], u64 *attrsp,
413 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700414{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800415 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700416}
417
418static int parse_flow_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800419 const struct nlattr *a[], u64 *attrsp,
420 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700421{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800422 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
423}
424
425static int genev_tun_opt_from_nlattr(const struct nlattr *a,
426 struct sw_flow_match *match, bool is_mask,
427 bool log)
428{
429 unsigned long opt_key_offset;
430
431 if (nla_len(a) > sizeof(match->key->tun_opts)) {
432 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
433 nla_len(a), sizeof(match->key->tun_opts));
434 return -EINVAL;
435 }
436
437 if (nla_len(a) % 4 != 0) {
438 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
439 nla_len(a));
440 return -EINVAL;
441 }
442
443 /* We need to record the length of the options passed
444 * down, otherwise packets with the same format but
445 * additional options will be silently matched.
446 */
447 if (!is_mask) {
448 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
449 false);
450 } else {
451 /* This is somewhat unusual because it looks at
452 * both the key and mask while parsing the
453 * attributes (and by extension assumes the key
454 * is parsed first). Normally, we would verify
455 * that each is the correct length and that the
456 * attributes line up in the validate function.
457 * However, that is difficult because this is
458 * variable length and we won't have the
459 * information later.
460 */
461 if (match->key->tun_opts_len != nla_len(a)) {
462 OVS_NLERR(log, "Geneve option len %d != mask len %d",
463 match->key->tun_opts_len, nla_len(a));
464 return -EINVAL;
465 }
466
467 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
468 }
469
Thomas Grafd91641d2015-01-15 03:53:57 +0100470 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800471 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
472 nla_len(a), is_mask);
473 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700474}
475
Thomas Graf1dd144c2015-01-15 03:53:59 +0100476static const struct nla_policy vxlan_opt_policy[OVS_VXLAN_EXT_MAX + 1] = {
477 [OVS_VXLAN_EXT_GBP] = { .type = NLA_U32 },
478};
479
480static int vxlan_tun_opt_from_nlattr(const struct nlattr *a,
481 struct sw_flow_match *match, bool is_mask,
482 bool log)
483{
484 struct nlattr *tb[OVS_VXLAN_EXT_MAX+1];
485 unsigned long opt_key_offset;
Thomas Graf614732e2015-07-21 10:44:06 +0200486 struct vxlan_metadata opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100487 int err;
488
489 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
490
491 err = nla_parse_nested(tb, OVS_VXLAN_EXT_MAX, a, vxlan_opt_policy);
492 if (err < 0)
493 return err;
494
495 memset(&opts, 0, sizeof(opts));
496
497 if (tb[OVS_VXLAN_EXT_GBP])
498 opts.gbp = nla_get_u32(tb[OVS_VXLAN_EXT_GBP]);
499
500 if (!is_mask)
501 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
502 else
503 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
504
505 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
506 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
507 is_mask);
508 return 0;
509}
510
Pravin B Shelare6445712013-10-03 18:16:47 -0700511static int ipv4_tun_from_nlattr(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800512 struct sw_flow_match *match, bool is_mask,
513 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700514{
515 struct nlattr *a;
516 int rem;
517 bool ttl = false;
518 __be16 tun_flags = 0;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100519 int opts_type = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700520
521 nla_for_each_nested(a, attr, rem) {
522 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800523 int err;
524
Pravin B Shelare6445712013-10-03 18:16:47 -0700525 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800526 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
527 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700528 return -EINVAL;
529 }
530
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100531 if (ovs_tunnel_key_lens[type].len != nla_len(a) &&
532 ovs_tunnel_key_lens[type].len != OVS_ATTR_NESTED) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800533 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100534 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700535 return -EINVAL;
536 }
537
538 switch (type) {
539 case OVS_TUNNEL_KEY_ATTR_ID:
540 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
541 nla_get_be64(a), is_mask);
542 tun_flags |= TUNNEL_KEY;
543 break;
544 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200545 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
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_IPV4_DST:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200549 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
Jiri Benc67b61f62015-03-29 16:59:26 +0200550 nla_get_in_addr(a), is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700551 break;
552 case OVS_TUNNEL_KEY_ATTR_TOS:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200553 SW_FLOW_KEY_PUT(match, tun_key.tos,
Pravin B Shelare6445712013-10-03 18:16:47 -0700554 nla_get_u8(a), is_mask);
555 break;
556 case OVS_TUNNEL_KEY_ATTR_TTL:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200557 SW_FLOW_KEY_PUT(match, tun_key.ttl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700558 nla_get_u8(a), is_mask);
559 ttl = true;
560 break;
561 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
562 tun_flags |= TUNNEL_DONT_FRAGMENT;
563 break;
564 case OVS_TUNNEL_KEY_ATTR_CSUM:
565 tun_flags |= TUNNEL_CSUM;
566 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800567 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
568 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
569 nla_get_be16(a), is_mask);
570 break;
571 case OVS_TUNNEL_KEY_ATTR_TP_DST:
572 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
573 nla_get_be16(a), is_mask);
574 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700575 case OVS_TUNNEL_KEY_ATTR_OAM:
576 tun_flags |= TUNNEL_OAM;
577 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700578 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Thomas Graf1dd144c2015-01-15 03:53:59 +0100579 if (opts_type) {
580 OVS_NLERR(log, "Multiple metadata blocks provided");
581 return -EINVAL;
582 }
583
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800584 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
585 if (err)
586 return err;
587
Thomas Graf1dd144c2015-01-15 03:53:59 +0100588 tun_flags |= TUNNEL_GENEVE_OPT;
589 opts_type = type;
590 break;
591 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
592 if (opts_type) {
593 OVS_NLERR(log, "Multiple metadata blocks provided");
594 return -EINVAL;
595 }
596
597 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
598 if (err)
599 return err;
600
601 tun_flags |= TUNNEL_VXLAN_OPT;
602 opts_type = type;
Jesse Grossf5796682014-10-03 15:35:33 -0700603 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700604 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800605 OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700606 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700607 return -EINVAL;
608 }
609 }
610
611 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
612
613 if (rem > 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800614 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
615 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700616 return -EINVAL;
617 }
618
619 if (!is_mask) {
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200620 if (!match->key->tun_key.u.ipv4.dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800621 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700622 return -EINVAL;
623 }
624
625 if (!ttl) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800626 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700627 return -EINVAL;
628 }
629 }
630
Thomas Graf1dd144c2015-01-15 03:53:59 +0100631 return opts_type;
632}
633
634static int vxlan_opt_to_nlattr(struct sk_buff *skb,
635 const void *tun_opts, int swkey_tun_opts_len)
636{
Thomas Graf614732e2015-07-21 10:44:06 +0200637 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100638 struct nlattr *nla;
639
640 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
641 if (!nla)
642 return -EMSGSIZE;
643
644 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
645 return -EMSGSIZE;
646
647 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700648 return 0;
649}
650
Jesse Grossf5796682014-10-03 15:35:33 -0700651static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200652 const struct ip_tunnel_key *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100653 const void *tun_opts, int swkey_tun_opts_len)
Pravin B Shelare6445712013-10-03 18:16:47 -0700654{
Pravin B Shelare6445712013-10-03 18:16:47 -0700655 if (output->tun_flags & TUNNEL_KEY &&
656 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
657 return -EMSGSIZE;
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200658 if (output->u.ipv4.src &&
Jiri Benc930345e2015-03-29 16:59:25 +0200659 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200660 output->u.ipv4.src))
Pravin B Shelare6445712013-10-03 18:16:47 -0700661 return -EMSGSIZE;
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200662 if (output->u.ipv4.dst &&
Jiri Benc930345e2015-03-29 16:59:25 +0200663 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200664 output->u.ipv4.dst))
Pravin B Shelare6445712013-10-03 18:16:47 -0700665 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200666 if (output->tos &&
667 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700668 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200669 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700670 return -EMSGSIZE;
671 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700672 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700673 return -EMSGSIZE;
674 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700675 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
676 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800677 if (output->tp_src &&
678 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
679 return -EMSGSIZE;
680 if (output->tp_dst &&
681 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
682 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700683 if ((output->tun_flags & TUNNEL_OAM) &&
684 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700685 return -EMSGSIZE;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100686 if (tun_opts) {
687 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
688 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
689 swkey_tun_opts_len, tun_opts))
690 return -EMSGSIZE;
691 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
692 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
693 return -EMSGSIZE;
694 }
Jesse Grossf5796682014-10-03 15:35:33 -0700695
696 return 0;
697}
698
Jesse Grossf5796682014-10-03 15:35:33 -0700699static int ipv4_tun_to_nlattr(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200700 const struct ip_tunnel_key *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100701 const void *tun_opts, int swkey_tun_opts_len)
Jesse Grossf5796682014-10-03 15:35:33 -0700702{
703 struct nlattr *nla;
704 int err;
705
706 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
707 if (!nla)
708 return -EMSGSIZE;
709
710 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
711 if (err)
712 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700713
714 nla_nest_end(skb, nla);
715 return 0;
716}
717
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800718int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200719 const struct ip_tunnel_info *egress_tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800720{
Thomas Graf1d8fff92015-07-21 10:43:54 +0200721 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->key,
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800722 egress_tun_info->options,
723 egress_tun_info->options_len);
724}
725
Joe Stringerc2ac6672015-08-26 11:31:52 -0700726static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
727 u64 *attrs, const struct nlattr **a,
728 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700729{
Andy Zhou971427f32014-09-15 19:37:25 -0700730 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
731 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
732
733 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
734 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
735 }
736
737 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
738 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
739
740 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
741 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
742 }
743
Pravin B Shelare6445712013-10-03 18:16:47 -0700744 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
745 SW_FLOW_KEY_PUT(match, phy.priority,
746 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
747 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
748 }
749
750 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
751 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
752
Jesse Gross426cda52014-10-06 05:08:38 -0700753 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700754 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -0700755 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800756 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -0700757 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -0700758 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700759 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700760
761 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
762 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
763 } else if (!is_mask) {
764 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
765 }
766
767 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
768 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
769
770 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
771 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
772 }
773 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
774 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100775 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700776 return -EINVAL;
777 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
778 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700779
780 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -0700781 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700782 u8 ct_state = nla_get_u8(a[OVS_KEY_ATTR_CT_STATE]);
783
784 SW_FLOW_KEY_PUT(match, ct.state, ct_state, is_mask);
785 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
786 }
787 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -0700788 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700789 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
790
791 SW_FLOW_KEY_PUT(match, ct.zone, ct_zone, is_mask);
792 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
793 }
Joe Stringer182e3042015-08-26 11:31:49 -0700794 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -0700795 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
Joe Stringer182e3042015-08-26 11:31:49 -0700796 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
797
798 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
799 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
800 }
Joe Stringerc2ac6672015-08-26 11:31:52 -0700801 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABEL) &&
802 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABEL)) {
803 const struct ovs_key_ct_label *cl;
804
805 cl = nla_data(a[OVS_KEY_ATTR_CT_LABEL]);
806 SW_FLOW_KEY_MEMCPY(match, ct.label, cl->ct_label,
807 sizeof(*cl), is_mask);
808 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABEL);
809 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700810 return 0;
811}
812
Joe Stringerc2ac6672015-08-26 11:31:52 -0700813static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
814 u64 attrs, const struct nlattr **a,
815 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700816{
817 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700818
Joe Stringerc2ac6672015-08-26 11:31:52 -0700819 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -0700820 if (err)
821 return err;
822
823 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
824 const struct ovs_key_ethernet *eth_key;
825
826 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
827 SW_FLOW_KEY_MEMCPY(match, eth.src,
828 eth_key->eth_src, ETH_ALEN, is_mask);
829 SW_FLOW_KEY_MEMCPY(match, eth.dst,
830 eth_key->eth_dst, ETH_ALEN, is_mask);
831 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
832 }
833
834 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
835 __be16 tci;
836
837 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
838 if (!(tci & htons(VLAN_TAG_PRESENT))) {
839 if (is_mask)
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800840 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700841 else
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800842 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700843
844 return -EINVAL;
845 }
846
847 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
848 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
Pravin B Shelara85311b2014-10-19 12:03:40 -0700849 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700850
851 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
852 __be16 eth_type;
853
854 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
855 if (is_mask) {
856 /* Always exact match EtherType. */
857 eth_type = htons(0xffff);
Alexander Duyck6713fc92015-05-04 14:34:05 -0700858 } else if (!eth_proto_is_802_3(eth_type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800859 OVS_NLERR(log, "EtherType %x is less than min %x",
860 ntohs(eth_type), ETH_P_802_3_MIN);
Pravin B Shelare6445712013-10-03 18:16:47 -0700861 return -EINVAL;
862 }
863
864 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
865 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
866 } else if (!is_mask) {
867 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
868 }
869
870 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
871 const struct ovs_key_ipv4 *ipv4_key;
872
873 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
874 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800875 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
876 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700877 return -EINVAL;
878 }
879 SW_FLOW_KEY_PUT(match, ip.proto,
880 ipv4_key->ipv4_proto, is_mask);
881 SW_FLOW_KEY_PUT(match, ip.tos,
882 ipv4_key->ipv4_tos, is_mask);
883 SW_FLOW_KEY_PUT(match, ip.ttl,
884 ipv4_key->ipv4_ttl, is_mask);
885 SW_FLOW_KEY_PUT(match, ip.frag,
886 ipv4_key->ipv4_frag, is_mask);
887 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
888 ipv4_key->ipv4_src, is_mask);
889 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
890 ipv4_key->ipv4_dst, is_mask);
891 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
892 }
893
894 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
895 const struct ovs_key_ipv6 *ipv6_key;
896
897 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
898 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800899 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
900 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700901 return -EINVAL;
902 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800903
Joe Stringerd3052bb2014-11-19 13:54:49 -0800904 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
David S. Miller14591432014-11-21 22:28:24 -0500905 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800906 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
907 return -EINVAL;
908 }
909
Pravin B Shelare6445712013-10-03 18:16:47 -0700910 SW_FLOW_KEY_PUT(match, ipv6.label,
911 ipv6_key->ipv6_label, is_mask);
912 SW_FLOW_KEY_PUT(match, ip.proto,
913 ipv6_key->ipv6_proto, is_mask);
914 SW_FLOW_KEY_PUT(match, ip.tos,
915 ipv6_key->ipv6_tclass, is_mask);
916 SW_FLOW_KEY_PUT(match, ip.ttl,
917 ipv6_key->ipv6_hlimit, is_mask);
918 SW_FLOW_KEY_PUT(match, ip.frag,
919 ipv6_key->ipv6_frag, is_mask);
920 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
921 ipv6_key->ipv6_src,
922 sizeof(match->key->ipv6.addr.src),
923 is_mask);
924 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
925 ipv6_key->ipv6_dst,
926 sizeof(match->key->ipv6.addr.dst),
927 is_mask);
928
929 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
930 }
931
932 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
933 const struct ovs_key_arp *arp_key;
934
935 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
936 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800937 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -0700938 arp_key->arp_op);
939 return -EINVAL;
940 }
941
942 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
943 arp_key->arp_sip, is_mask);
944 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
945 arp_key->arp_tip, is_mask);
946 SW_FLOW_KEY_PUT(match, ip.proto,
947 ntohs(arp_key->arp_op), is_mask);
948 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
949 arp_key->arp_sha, ETH_ALEN, is_mask);
950 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
951 arp_key->arp_tha, ETH_ALEN, is_mask);
952
953 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
954 }
955
Simon Horman25cd9ba2014-10-06 05:05:13 -0700956 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
957 const struct ovs_key_mpls *mpls_key;
958
959 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
960 SW_FLOW_KEY_PUT(match, mpls.top_lse,
961 mpls_key->mpls_lse, is_mask);
962
963 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
964 }
965
Pravin B Shelare6445712013-10-03 18:16:47 -0700966 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
967 const struct ovs_key_tcp *tcp_key;
968
969 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700970 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
971 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700972 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
973 }
974
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700975 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -0700976 SW_FLOW_KEY_PUT(match, tp.flags,
977 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
978 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700979 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
980 }
981
Pravin B Shelare6445712013-10-03 18:16:47 -0700982 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
983 const struct ovs_key_udp *udp_key;
984
985 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700986 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
987 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700988 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
989 }
990
991 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
992 const struct ovs_key_sctp *sctp_key;
993
994 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700995 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
996 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700997 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
998 }
999
1000 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1001 const struct ovs_key_icmp *icmp_key;
1002
1003 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001004 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001005 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001006 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001007 htons(icmp_key->icmp_code), is_mask);
1008 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1009 }
1010
1011 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1012 const struct ovs_key_icmpv6 *icmpv6_key;
1013
1014 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001015 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001016 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001017 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001018 htons(icmpv6_key->icmpv6_code), is_mask);
1019 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1020 }
1021
1022 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1023 const struct ovs_key_nd *nd_key;
1024
1025 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1026 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1027 nd_key->nd_target,
1028 sizeof(match->key->ipv6.nd.target),
1029 is_mask);
1030 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1031 nd_key->nd_sll, ETH_ALEN, is_mask);
1032 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1033 nd_key->nd_tll, ETH_ALEN, is_mask);
1034 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1035 }
1036
Jesse Gross426cda52014-10-06 05:08:38 -07001037 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001038 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001039 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001040 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001041 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001042
1043 return 0;
1044}
1045
Thomas Graf81bfe3c2015-01-15 03:53:58 +01001046static void nlattr_set(struct nlattr *attr, u8 val,
1047 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001048{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001049 struct nlattr *nla;
1050 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001051
Pravin B Shelarf47de062014-10-16 21:55:45 -07001052 /* The nlattr stream should already have been validated */
1053 nla_for_each_nested(nla, attr, rem) {
Thomas Graf81bfe3c2015-01-15 03:53:58 +01001054 if (tbl && tbl[nla_type(nla)].len == OVS_ATTR_NESTED)
1055 nlattr_set(nla, val, tbl[nla_type(nla)].next);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001056 else
1057 memset(nla_data(nla), val, nla_len(nla));
1058 }
1059}
1060
1061static void mask_set_nlattr(struct nlattr *attr, u8 val)
1062{
Thomas Graf81bfe3c2015-01-15 03:53:58 +01001063 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001064}
1065
1066/**
1067 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1068 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1069 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1070 * does not include any don't care bit.
Joe Stringerc2ac6672015-08-26 11:31:52 -07001071 * @net: Used to determine per-namespace field support.
Pravin B Shelare6445712013-10-03 18:16:47 -07001072 * @match: receives the extracted flow match information.
1073 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1074 * sequence. The fields should of the packet that triggered the creation
1075 * of this flow.
1076 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1077 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001078 * @log: Boolean to allow kernel error logging. Normally true, but when
1079 * probing for feature compatibility this should be passed in as false to
1080 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001081 */
Joe Stringerc2ac6672015-08-26 11:31:52 -07001082int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001083 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001084 const struct nlattr *nla_mask,
1085 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001086{
1087 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1088 const struct nlattr *encap;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001089 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001090 u64 key_attrs = 0;
1091 u64 mask_attrs = 0;
1092 bool encap_valid = false;
1093 int err;
1094
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001095 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001096 if (err)
1097 return err;
1098
1099 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
1100 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
1101 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1102 __be16 tci;
1103
1104 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
1105 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001106 OVS_NLERR(log, "Invalid Vlan frame.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001107 return -EINVAL;
1108 }
1109
1110 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1111 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1112 encap = a[OVS_KEY_ATTR_ENCAP];
1113 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1114 encap_valid = true;
1115
1116 if (tci & htons(VLAN_TAG_PRESENT)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001117 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001118 if (err)
1119 return err;
1120 } else if (!tci) {
1121 /* Corner case for truncated 802.1Q header. */
1122 if (nla_len(encap)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001123 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001124 return -EINVAL;
1125 }
1126 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001127 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
Pravin B Shelare6445712013-10-03 18:16:47 -07001128 return -EINVAL;
1129 }
1130 }
1131
Joe Stringerc2ac6672015-08-26 11:31:52 -07001132 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001133 if (err)
1134 return err;
1135
Pravin B Shelara85311b2014-10-19 12:03:40 -07001136 if (match->mask) {
1137 if (!nla_mask) {
1138 /* Create an exact match mask. We need to set to 0xff
1139 * all the 'match->mask' fields that have been touched
1140 * in 'match->key'. We cannot simply memset
1141 * 'match->mask', because padding bytes and fields not
1142 * specified in 'match->key' should be left to 0.
1143 * Instead, we use a stream of netlink attributes,
1144 * copied from 'key' and set to 0xff.
1145 * ovs_key_from_nlattrs() will take care of filling
1146 * 'match->mask' appropriately.
1147 */
1148 newmask = kmemdup(nla_key,
1149 nla_total_size(nla_len(nla_key)),
1150 GFP_KERNEL);
1151 if (!newmask)
1152 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001153
Pravin B Shelara85311b2014-10-19 12:03:40 -07001154 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001155
Pravin B Shelara85311b2014-10-19 12:03:40 -07001156 /* The userspace does not send tunnel attributes that
1157 * are 0, but we should not wildcard them nonetheless.
1158 */
Jiri Bencc1ea5d62015-08-20 13:56:23 +02001159 if (match->key->tun_key.u.ipv4.dst)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001160 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1161 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001162
Pravin B Shelara85311b2014-10-19 12:03:40 -07001163 nla_mask = newmask;
1164 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001165
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001166 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001167 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001168 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001169
Pravin B Shelara85311b2014-10-19 12:03:40 -07001170 /* Always match on tci. */
1171 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1172
Pravin B Shelarf47de062014-10-16 21:55:45 -07001173 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001174 __be16 eth_type = 0;
1175 __be16 tci = 0;
1176
1177 if (!encap_valid) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001178 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
Pravin B Shelarf47de062014-10-16 21:55:45 -07001179 err = -EINVAL;
1180 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001181 }
1182
1183 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1184 if (a[OVS_KEY_ATTR_ETHERTYPE])
1185 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1186
1187 if (eth_type == htons(0xffff)) {
1188 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1189 encap = a[OVS_KEY_ATTR_ENCAP];
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001190 err = parse_flow_mask_nlattrs(encap, a,
1191 &mask_attrs, log);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001192 if (err)
1193 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001194 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001195 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1196 ntohs(eth_type));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001197 err = -EINVAL;
1198 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001199 }
1200
1201 if (a[OVS_KEY_ATTR_VLAN])
1202 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1203
1204 if (!(tci & htons(VLAN_TAG_PRESENT))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001205 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1206 ntohs(tci));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001207 err = -EINVAL;
1208 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001209 }
1210 }
1211
Joe Stringerc2ac6672015-08-26 11:31:52 -07001212 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1213 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001214 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001215 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001216 }
1217
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001218 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001219 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001220
Pravin B Shelarf47de062014-10-16 21:55:45 -07001221free_newmask:
1222 kfree(newmask);
1223 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001224}
1225
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001226static size_t get_ufid_len(const struct nlattr *attr, bool log)
1227{
1228 size_t len;
1229
1230 if (!attr)
1231 return 0;
1232
1233 len = nla_len(attr);
1234 if (len < 1 || len > MAX_UFID_LENGTH) {
1235 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1236 nla_len(attr), MAX_UFID_LENGTH);
1237 return 0;
1238 }
1239
1240 return len;
1241}
1242
1243/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1244 * or false otherwise.
1245 */
1246bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1247 bool log)
1248{
1249 sfid->ufid_len = get_ufid_len(attr, log);
1250 if (sfid->ufid_len)
1251 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1252
1253 return sfid->ufid_len;
1254}
1255
1256int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1257 const struct sw_flow_key *key, bool log)
1258{
1259 struct sw_flow_key *new_key;
1260
1261 if (ovs_nla_get_ufid(sfid, ufid, log))
1262 return 0;
1263
1264 /* If UFID was not provided, use unmasked key. */
1265 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1266 if (!new_key)
1267 return -ENOMEM;
1268 memcpy(new_key, key, sizeof(*key));
1269 sfid->unmasked_key = new_key;
1270
1271 return 0;
1272}
1273
1274u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1275{
1276 return attr ? nla_get_u32(attr) : 0;
1277}
1278
Pravin B Shelare6445712013-10-03 18:16:47 -07001279/**
1280 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001281 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -07001282 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1283 * sequence.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001284 * @log: Boolean to allow kernel error logging. Normally true, but when
1285 * probing for feature compatibility this should be passed in as false to
1286 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001287 *
1288 * This parses a series of Netlink attributes that form a flow key, which must
1289 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1290 * get the metadata, that is, the parts of the flow key that cannot be
1291 * extracted from the packet itself.
1292 */
1293
Joe Stringerc2ac6672015-08-26 11:31:52 -07001294int ovs_nla_get_flow_metadata(struct net *net, const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001295 struct sw_flow_key *key,
1296 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001297{
Pravin B Shelare6445712013-10-03 18:16:47 -07001298 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001299 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001300 u64 attrs = 0;
1301 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001302
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001303 err = parse_flow_nlattrs(attr, a, &attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001304 if (err)
1305 return -EINVAL;
1306
1307 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001308 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001309
Joe Stringer7f8a4362015-08-26 11:31:48 -07001310 memset(&key->ct, 0, sizeof(key->ct));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001311 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001312
Joe Stringerc2ac6672015-08-26 11:31:52 -07001313 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001314}
1315
Joe Stringer5b4237b2015-01-21 16:42:48 -08001316static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1317 const struct sw_flow_key *output, bool is_mask,
1318 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001319{
1320 struct ovs_key_ethernet *eth_key;
1321 struct nlattr *nla, *encap;
Pravin B Shelare6445712013-10-03 18:16:47 -07001322
Andy Zhou971427f32014-09-15 19:37:25 -07001323 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1324 goto nla_put_failure;
1325
1326 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1327 goto nla_put_failure;
1328
Pravin B Shelare6445712013-10-03 18:16:47 -07001329 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1330 goto nla_put_failure;
1331
Jiri Bencc1ea5d62015-08-20 13:56:23 +02001332 if ((swkey->tun_key.u.ipv4.dst || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001333 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001334
1335 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001336 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001337
1338 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1339 swkey->tun_opts_len))
1340 goto nla_put_failure;
1341 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001342
1343 if (swkey->phy.in_port == DP_MAX_PORTS) {
1344 if (is_mask && (output->phy.in_port == 0xffff))
1345 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1346 goto nla_put_failure;
1347 } else {
1348 u16 upper_u16;
1349 upper_u16 = !is_mask ? 0 : 0xffff;
1350
1351 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1352 (upper_u16 << 16) | output->phy.in_port))
1353 goto nla_put_failure;
1354 }
1355
1356 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1357 goto nla_put_failure;
1358
Joe Stringer7f8a4362015-08-26 11:31:48 -07001359 if (ovs_ct_put_key(output, skb))
1360 goto nla_put_failure;
1361
Pravin B Shelare6445712013-10-03 18:16:47 -07001362 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1363 if (!nla)
1364 goto nla_put_failure;
1365
1366 eth_key = nla_data(nla);
Joe Perches8c63ff02014-02-18 11:15:45 -08001367 ether_addr_copy(eth_key->eth_src, output->eth.src);
1368 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001369
1370 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1371 __be16 eth_type;
1372 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1373 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1374 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1375 goto nla_put_failure;
1376 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1377 if (!swkey->eth.tci)
1378 goto unencap;
1379 } else
1380 encap = NULL;
1381
1382 if (swkey->eth.type == htons(ETH_P_802_2)) {
1383 /*
1384 * Ethertype 802.2 is represented in the netlink with omitted
1385 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1386 * 0xffff in the mask attribute. Ethertype can also
1387 * be wildcarded.
1388 */
1389 if (is_mask && output->eth.type)
1390 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1391 output->eth.type))
1392 goto nla_put_failure;
1393 goto unencap;
1394 }
1395
1396 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1397 goto nla_put_failure;
1398
1399 if (swkey->eth.type == htons(ETH_P_IP)) {
1400 struct ovs_key_ipv4 *ipv4_key;
1401
1402 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1403 if (!nla)
1404 goto nla_put_failure;
1405 ipv4_key = nla_data(nla);
1406 ipv4_key->ipv4_src = output->ipv4.addr.src;
1407 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1408 ipv4_key->ipv4_proto = output->ip.proto;
1409 ipv4_key->ipv4_tos = output->ip.tos;
1410 ipv4_key->ipv4_ttl = output->ip.ttl;
1411 ipv4_key->ipv4_frag = output->ip.frag;
1412 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1413 struct ovs_key_ipv6 *ipv6_key;
1414
1415 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1416 if (!nla)
1417 goto nla_put_failure;
1418 ipv6_key = nla_data(nla);
1419 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1420 sizeof(ipv6_key->ipv6_src));
1421 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1422 sizeof(ipv6_key->ipv6_dst));
1423 ipv6_key->ipv6_label = output->ipv6.label;
1424 ipv6_key->ipv6_proto = output->ip.proto;
1425 ipv6_key->ipv6_tclass = output->ip.tos;
1426 ipv6_key->ipv6_hlimit = output->ip.ttl;
1427 ipv6_key->ipv6_frag = output->ip.frag;
1428 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1429 swkey->eth.type == htons(ETH_P_RARP)) {
1430 struct ovs_key_arp *arp_key;
1431
1432 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1433 if (!nla)
1434 goto nla_put_failure;
1435 arp_key = nla_data(nla);
1436 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1437 arp_key->arp_sip = output->ipv4.addr.src;
1438 arp_key->arp_tip = output->ipv4.addr.dst;
1439 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001440 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1441 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001442 } else if (eth_p_mpls(swkey->eth.type)) {
1443 struct ovs_key_mpls *mpls_key;
1444
1445 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1446 if (!nla)
1447 goto nla_put_failure;
1448 mpls_key = nla_data(nla);
1449 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001450 }
1451
1452 if ((swkey->eth.type == htons(ETH_P_IP) ||
1453 swkey->eth.type == htons(ETH_P_IPV6)) &&
1454 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1455
1456 if (swkey->ip.proto == IPPROTO_TCP) {
1457 struct ovs_key_tcp *tcp_key;
1458
1459 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1460 if (!nla)
1461 goto nla_put_failure;
1462 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001463 tcp_key->tcp_src = output->tp.src;
1464 tcp_key->tcp_dst = output->tp.dst;
1465 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1466 output->tp.flags))
1467 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001468 } else if (swkey->ip.proto == IPPROTO_UDP) {
1469 struct ovs_key_udp *udp_key;
1470
1471 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1472 if (!nla)
1473 goto nla_put_failure;
1474 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001475 udp_key->udp_src = output->tp.src;
1476 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001477 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1478 struct ovs_key_sctp *sctp_key;
1479
1480 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1481 if (!nla)
1482 goto nla_put_failure;
1483 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001484 sctp_key->sctp_src = output->tp.src;
1485 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001486 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1487 swkey->ip.proto == IPPROTO_ICMP) {
1488 struct ovs_key_icmp *icmp_key;
1489
1490 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1491 if (!nla)
1492 goto nla_put_failure;
1493 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001494 icmp_key->icmp_type = ntohs(output->tp.src);
1495 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001496 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1497 swkey->ip.proto == IPPROTO_ICMPV6) {
1498 struct ovs_key_icmpv6 *icmpv6_key;
1499
1500 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1501 sizeof(*icmpv6_key));
1502 if (!nla)
1503 goto nla_put_failure;
1504 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001505 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1506 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001507
1508 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1509 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1510 struct ovs_key_nd *nd_key;
1511
1512 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1513 if (!nla)
1514 goto nla_put_failure;
1515 nd_key = nla_data(nla);
1516 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1517 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001518 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1519 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001520 }
1521 }
1522 }
1523
1524unencap:
1525 if (encap)
1526 nla_nest_end(skb, encap);
1527
1528 return 0;
1529
1530nla_put_failure:
1531 return -EMSGSIZE;
1532}
1533
Joe Stringer5b4237b2015-01-21 16:42:48 -08001534int ovs_nla_put_key(const struct sw_flow_key *swkey,
1535 const struct sw_flow_key *output, int attr, bool is_mask,
1536 struct sk_buff *skb)
1537{
1538 int err;
1539 struct nlattr *nla;
1540
1541 nla = nla_nest_start(skb, attr);
1542 if (!nla)
1543 return -EMSGSIZE;
1544 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1545 if (err)
1546 return err;
1547 nla_nest_end(skb, nla);
1548
1549 return 0;
1550}
1551
1552/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001553int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08001554{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001555 if (ovs_identifier_is_ufid(&flow->id))
1556 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1557 flow->id.ufid);
1558
1559 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1560 OVS_FLOW_ATTR_KEY, false, skb);
1561}
1562
1563/* Called with ovs_mutex or RCU read lock. */
1564int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1565{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08001566 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08001567 OVS_FLOW_ATTR_KEY, false, skb);
1568}
1569
1570/* Called with ovs_mutex or RCU read lock. */
1571int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1572{
1573 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1574 OVS_FLOW_ATTR_MASK, true, skb);
1575}
1576
Pravin B Shelare6445712013-10-03 18:16:47 -07001577#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1578
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001579static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001580{
1581 struct sw_flow_actions *sfa;
1582
Jesse Gross426cda52014-10-06 05:08:38 -07001583 if (size > MAX_ACTIONS_BUFSIZE) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001584 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001585 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001586 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001587
1588 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1589 if (!sfa)
1590 return ERR_PTR(-ENOMEM);
1591
1592 sfa->actions_len = 0;
1593 return sfa;
1594}
1595
Thomas Graf34ae9322015-07-21 10:44:03 +02001596static void ovs_nla_free_set_action(const struct nlattr *a)
1597{
1598 const struct nlattr *ovs_key = nla_data(a);
1599 struct ovs_tunnel_info *ovs_tun;
1600
1601 switch (nla_type(ovs_key)) {
1602 case OVS_KEY_ATTR_TUNNEL_INFO:
1603 ovs_tun = nla_data(ovs_key);
1604 dst_release((struct dst_entry *)ovs_tun->tun_dst);
1605 break;
1606 }
1607}
1608
Pravin B Shelare6445712013-10-03 18:16:47 -07001609void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1610{
Thomas Graf34ae9322015-07-21 10:44:03 +02001611 const struct nlattr *a;
1612 int rem;
1613
1614 if (!sf_acts)
1615 return;
1616
1617 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1618 switch (nla_type(a)) {
1619 case OVS_ACTION_ATTR_SET:
1620 ovs_nla_free_set_action(a);
1621 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001622 case OVS_ACTION_ATTR_CT:
1623 ovs_ct_free_action(a);
1624 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02001625 }
1626 }
1627
1628 kfree(sf_acts);
1629}
1630
1631static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1632{
1633 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1634}
1635
1636/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1637 * The caller must hold rcu_read_lock for this to be sensible. */
1638void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
1639{
1640 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07001641}
1642
1643static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001644 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001645{
1646
1647 struct sw_flow_actions *acts;
1648 int new_acts_size;
1649 int req_size = NLA_ALIGN(attr_len);
1650 int next_offset = offsetof(struct sw_flow_actions, actions) +
1651 (*sfa)->actions_len;
1652
1653 if (req_size <= (ksize(*sfa) - next_offset))
1654 goto out;
1655
1656 new_acts_size = ksize(*sfa) * 2;
1657
1658 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1659 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1660 return ERR_PTR(-EMSGSIZE);
1661 new_acts_size = MAX_ACTIONS_BUFSIZE;
1662 }
1663
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001664 acts = nla_alloc_flow_actions(new_acts_size, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001665 if (IS_ERR(acts))
1666 return (void *)acts;
1667
1668 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1669 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07001670 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001671 kfree(*sfa);
1672 *sfa = acts;
1673
1674out:
1675 (*sfa)->actions_len += req_size;
1676 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1677}
1678
Jesse Grossf0b128c2014-10-03 15:35:31 -07001679static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001680 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001681{
1682 struct nlattr *a;
1683
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001684 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001685 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001686 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001687
1688 a->nla_type = attrtype;
1689 a->nla_len = nla_attr_size(len);
1690
1691 if (data)
1692 memcpy(nla_data(a), data, len);
1693 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1694
Jesse Grossf0b128c2014-10-03 15:35:31 -07001695 return a;
1696}
1697
Joe Stringer7f8a4362015-08-26 11:31:48 -07001698int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
1699 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07001700{
1701 struct nlattr *a;
1702
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001703 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001704
Fabian Frederickf35423c12014-11-14 19:32:58 +01001705 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07001706}
1707
1708static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001709 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001710{
1711 int used = (*sfa)->actions_len;
1712 int err;
1713
Joe Stringer7f8a4362015-08-26 11:31:48 -07001714 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001715 if (err)
1716 return err;
1717
1718 return used;
1719}
1720
1721static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1722 int st_offset)
1723{
1724 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1725 st_offset);
1726
1727 a->nla_len = sfa->actions_len - st_offset;
1728}
1729
Joe Stringer7f8a4362015-08-26 11:31:48 -07001730static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001731 const struct sw_flow_key *key,
1732 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001733 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001734
Joe Stringer7f8a4362015-08-26 11:31:48 -07001735static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
Pravin B Shelare6445712013-10-03 18:16:47 -07001736 const struct sw_flow_key *key, int depth,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001737 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001738 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001739{
1740 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1741 const struct nlattr *probability, *actions;
1742 const struct nlattr *a;
1743 int rem, start, err, st_acts;
1744
1745 memset(attrs, 0, sizeof(attrs));
1746 nla_for_each_nested(a, attr, rem) {
1747 int type = nla_type(a);
1748 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1749 return -EINVAL;
1750 attrs[type] = a;
1751 }
1752 if (rem)
1753 return -EINVAL;
1754
1755 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1756 if (!probability || nla_len(probability) != sizeof(u32))
1757 return -EINVAL;
1758
1759 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1760 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1761 return -EINVAL;
1762
1763 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001764 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001765 if (start < 0)
1766 return start;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001767 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1768 nla_data(probability), sizeof(u32), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001769 if (err)
1770 return err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001771 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001772 if (st_acts < 0)
1773 return st_acts;
1774
Joe Stringer7f8a4362015-08-26 11:31:48 -07001775 err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001776 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001777 if (err)
1778 return err;
1779
1780 add_nested_action_end(*sfa, st_acts);
1781 add_nested_action_end(*sfa, start);
1782
1783 return 0;
1784}
1785
Pravin B Shelare6445712013-10-03 18:16:47 -07001786void ovs_match_init(struct sw_flow_match *match,
1787 struct sw_flow_key *key,
1788 struct sw_flow_mask *mask)
1789{
1790 memset(match, 0, sizeof(*match));
1791 match->key = key;
1792 match->mask = mask;
1793
1794 memset(key, 0, sizeof(*key));
1795
1796 if (mask) {
1797 memset(&mask->key, 0, sizeof(mask->key));
1798 mask->range.start = mask->range.end = 0;
1799 }
1800}
1801
Thomas Grafd91641d2015-01-15 03:53:57 +01001802static int validate_geneve_opts(struct sw_flow_key *key)
1803{
1804 struct geneve_opt *option;
1805 int opts_len = key->tun_opts_len;
1806 bool crit_opt = false;
1807
1808 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
1809 while (opts_len > 0) {
1810 int len;
1811
1812 if (opts_len < sizeof(*option))
1813 return -EINVAL;
1814
1815 len = sizeof(*option) + option->length * 4;
1816 if (len > opts_len)
1817 return -EINVAL;
1818
1819 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1820
1821 option = (struct geneve_opt *)((u8 *)option + len);
1822 opts_len -= len;
1823 };
1824
1825 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1826
1827 return 0;
1828}
1829
Pravin B Shelare6445712013-10-03 18:16:47 -07001830static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001831 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001832{
1833 struct sw_flow_match match;
1834 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02001835 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02001836 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02001837 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001838 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01001839 int err = 0, start, opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07001840
1841 ovs_match_init(&match, &key, NULL);
Thomas Graf1dd144c2015-01-15 03:53:59 +01001842 opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
1843 if (opts_type < 0)
1844 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07001845
Jesse Grossf5796682014-10-03 15:35:33 -07001846 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01001847 switch (opts_type) {
1848 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1849 err = validate_geneve_opts(&key);
1850 if (err < 0)
1851 return err;
1852 break;
1853 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
1854 break;
1855 }
Jesse Grossf5796682014-10-03 15:35:33 -07001856 };
1857
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001858 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001859 if (start < 0)
1860 return start;
1861
Thomas Graf34ae9322015-07-21 10:44:03 +02001862 tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
1863 if (!tun_dst)
1864 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001865
Thomas Graf34ae9322015-07-21 10:44:03 +02001866 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1867 sizeof(*ovs_tun), log);
1868 if (IS_ERR(a)) {
1869 dst_release((struct dst_entry *)tun_dst);
1870 return PTR_ERR(a);
1871 }
1872
1873 ovs_tun = nla_data(a);
1874 ovs_tun->tun_dst = tun_dst;
1875
1876 tun_info = &tun_dst->u.tun_info;
1877 tun_info->mode = IP_TUNNEL_INFO_TX;
Thomas Graf1d8fff92015-07-21 10:43:54 +02001878 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07001879 tun_info->options_len = key.tun_opts_len;
1880
1881 if (tun_info->options_len) {
1882 /* We need to store the options in the action itself since
1883 * everything else will go away after flow setup. We can append
1884 * it to tun_info and then point there.
1885 */
Thomas Grafd91641d2015-01-15 03:53:57 +01001886 memcpy((tun_info + 1),
1887 TUN_METADATA_OPTS(&key, key.tun_opts_len), key.tun_opts_len);
1888 tun_info->options = (tun_info + 1);
Jesse Grossf5796682014-10-03 15:35:33 -07001889 } else {
1890 tun_info->options = NULL;
1891 }
Jesse Grossf0b128c2014-10-03 15:35:31 -07001892
Pravin B Shelare6445712013-10-03 18:16:47 -07001893 add_nested_action_end(*sfa, start);
1894
1895 return err;
1896}
1897
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001898/* Return false if there are any non-masked bits set.
1899 * Mask follows data immediately, before any netlink padding.
1900 */
1901static bool validate_masked(u8 *data, int len)
1902{
1903 u8 *mask = data + len;
1904
1905 while (len--)
1906 if (*data++ & ~*mask++)
1907 return false;
1908
1909 return true;
1910}
1911
Pravin B Shelare6445712013-10-03 18:16:47 -07001912static int validate_set(const struct nlattr *a,
1913 const struct sw_flow_key *flow_key,
1914 struct sw_flow_actions **sfa,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001915 bool *skip_copy, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001916{
1917 const struct nlattr *ovs_key = nla_data(a);
1918 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001919 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001920
1921 /* There can be only one key in a action */
1922 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1923 return -EINVAL;
1924
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001925 key_len = nla_len(ovs_key);
1926 if (masked)
1927 key_len /= 2;
1928
Pravin B Shelare6445712013-10-03 18:16:47 -07001929 if (key_type > OVS_KEY_ATTR_MAX ||
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001930 (ovs_key_lens[key_type].len != key_len &&
Thomas Graf81bfe3c2015-01-15 03:53:58 +01001931 ovs_key_lens[key_type].len != OVS_ATTR_NESTED))
Pravin B Shelare6445712013-10-03 18:16:47 -07001932 return -EINVAL;
1933
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001934 if (masked && !validate_masked(nla_data(ovs_key), key_len))
1935 return -EINVAL;
1936
Pravin B Shelare6445712013-10-03 18:16:47 -07001937 switch (key_type) {
1938 const struct ovs_key_ipv4 *ipv4_key;
1939 const struct ovs_key_ipv6 *ipv6_key;
1940 int err;
1941
1942 case OVS_KEY_ATTR_PRIORITY:
1943 case OVS_KEY_ATTR_SKB_MARK:
Joe Stringer182e3042015-08-26 11:31:49 -07001944 case OVS_KEY_ATTR_CT_MARK:
Joe Stringerc2ac6672015-08-26 11:31:52 -07001945 case OVS_KEY_ATTR_CT_LABEL:
Pravin B Shelare6445712013-10-03 18:16:47 -07001946 case OVS_KEY_ATTR_ETHERNET:
1947 break;
1948
1949 case OVS_KEY_ATTR_TUNNEL:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001950 if (eth_p_mpls(eth_type))
1951 return -EINVAL;
1952
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001953 if (masked)
1954 return -EINVAL; /* Masked tunnel set not supported. */
1955
1956 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001957 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001958 if (err)
1959 return err;
1960 break;
1961
1962 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001963 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07001964 return -EINVAL;
1965
Pravin B Shelare6445712013-10-03 18:16:47 -07001966 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07001967
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001968 if (masked) {
1969 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07001970
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001971 /* Non-writeable fields. */
1972 if (mask->ipv4_proto || mask->ipv4_frag)
1973 return -EINVAL;
1974 } else {
1975 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1976 return -EINVAL;
1977
1978 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1979 return -EINVAL;
1980 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001981 break;
1982
1983 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001984 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07001985 return -EINVAL;
1986
Pravin B Shelare6445712013-10-03 18:16:47 -07001987 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07001988
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001989 if (masked) {
1990 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07001991
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001992 /* Non-writeable fields. */
1993 if (mask->ipv6_proto || mask->ipv6_frag)
1994 return -EINVAL;
1995
1996 /* Invalid bits in the flow label mask? */
1997 if (ntohl(mask->ipv6_label) & 0xFFF00000)
1998 return -EINVAL;
1999 } else {
2000 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2001 return -EINVAL;
2002
2003 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2004 return -EINVAL;
2005 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002006 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2007 return -EINVAL;
2008
2009 break;
2010
2011 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002012 if ((eth_type != htons(ETH_P_IP) &&
2013 eth_type != htons(ETH_P_IPV6)) ||
2014 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002015 return -EINVAL;
2016
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002017 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002018
2019 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002020 if ((eth_type != htons(ETH_P_IP) &&
2021 eth_type != htons(ETH_P_IPV6)) ||
2022 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002023 return -EINVAL;
2024
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002025 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002026
2027 case OVS_KEY_ATTR_MPLS:
2028 if (!eth_p_mpls(eth_type))
2029 return -EINVAL;
2030 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002031
2032 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002033 if ((eth_type != htons(ETH_P_IP) &&
2034 eth_type != htons(ETH_P_IPV6)) ||
2035 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002036 return -EINVAL;
2037
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002038 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002039
2040 default:
2041 return -EINVAL;
2042 }
2043
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002044 /* Convert non-masked non-tunnel set actions to masked set actions. */
2045 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2046 int start, len = key_len * 2;
2047 struct nlattr *at;
2048
2049 *skip_copy = true;
2050
2051 start = add_nested_action_start(sfa,
2052 OVS_ACTION_ATTR_SET_TO_MASKED,
2053 log);
2054 if (start < 0)
2055 return start;
2056
2057 at = __add_action(sfa, key_type, NULL, len, log);
2058 if (IS_ERR(at))
2059 return PTR_ERR(at);
2060
2061 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2062 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2063 /* Clear non-writeable bits from otherwise writeable fields. */
2064 if (key_type == OVS_KEY_ATTR_IPV6) {
2065 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2066
2067 mask->ipv6_label &= htonl(0x000FFFFF);
2068 }
2069 add_nested_action_end(*sfa, start);
2070 }
2071
Pravin B Shelare6445712013-10-03 18:16:47 -07002072 return 0;
2073}
2074
2075static int validate_userspace(const struct nlattr *attr)
2076{
2077 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2078 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2079 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002080 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002081 };
2082 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2083 int error;
2084
2085 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
2086 attr, userspace_policy);
2087 if (error)
2088 return error;
2089
2090 if (!a[OVS_USERSPACE_ATTR_PID] ||
2091 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2092 return -EINVAL;
2093
2094 return 0;
2095}
2096
2097static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002098 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002099{
2100 int totlen = NLA_ALIGN(from->nla_len);
2101 struct nlattr *to;
2102
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002103 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002104 if (IS_ERR(to))
2105 return PTR_ERR(to);
2106
2107 memcpy(to, from, totlen);
2108 return 0;
2109}
2110
Joe Stringer7f8a4362015-08-26 11:31:48 -07002111static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002112 const struct sw_flow_key *key,
2113 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002114 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002115{
2116 const struct nlattr *a;
2117 int rem, err;
2118
2119 if (depth >= SAMPLE_ACTION_DEPTH)
2120 return -EOVERFLOW;
2121
2122 nla_for_each_nested(a, attr, rem) {
2123 /* Expected argument lengths, (u32)-1 for variable length. */
2124 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2125 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002126 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002127 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002128 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2129 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002130 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2131 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2132 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002133 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002134 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002135 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2136 [OVS_ACTION_ATTR_CT] = (u32)-1,
Pravin B Shelare6445712013-10-03 18:16:47 -07002137 };
2138 const struct ovs_action_push_vlan *vlan;
2139 int type = nla_type(a);
2140 bool skip_copy;
2141
2142 if (type > OVS_ACTION_ATTR_MAX ||
2143 (action_lens[type] != nla_len(a) &&
2144 action_lens[type] != (u32)-1))
2145 return -EINVAL;
2146
2147 skip_copy = false;
2148 switch (type) {
2149 case OVS_ACTION_ATTR_UNSPEC:
2150 return -EINVAL;
2151
2152 case OVS_ACTION_ATTR_USERSPACE:
2153 err = validate_userspace(a);
2154 if (err)
2155 return err;
2156 break;
2157
2158 case OVS_ACTION_ATTR_OUTPUT:
2159 if (nla_get_u32(a) >= DP_MAX_PORTS)
2160 return -EINVAL;
2161 break;
2162
Andy Zhou971427f32014-09-15 19:37:25 -07002163 case OVS_ACTION_ATTR_HASH: {
2164 const struct ovs_action_hash *act_hash = nla_data(a);
2165
2166 switch (act_hash->hash_alg) {
2167 case OVS_HASH_ALG_L4:
2168 break;
2169 default:
2170 return -EINVAL;
2171 }
2172
2173 break;
2174 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002175
2176 case OVS_ACTION_ATTR_POP_VLAN:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002177 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07002178 break;
2179
2180 case OVS_ACTION_ATTR_PUSH_VLAN:
2181 vlan = nla_data(a);
2182 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
2183 return -EINVAL;
2184 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2185 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002186 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07002187 break;
2188
Andy Zhou971427f32014-09-15 19:37:25 -07002189 case OVS_ACTION_ATTR_RECIRC:
2190 break;
2191
Simon Horman25cd9ba2014-10-06 05:05:13 -07002192 case OVS_ACTION_ATTR_PUSH_MPLS: {
2193 const struct ovs_action_push_mpls *mpls = nla_data(a);
2194
Simon Horman25cd9ba2014-10-06 05:05:13 -07002195 if (!eth_p_mpls(mpls->mpls_ethertype))
2196 return -EINVAL;
2197 /* Prohibit push MPLS other than to a white list
2198 * for packets that have a known tag order.
2199 */
2200 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2201 (eth_type != htons(ETH_P_IP) &&
2202 eth_type != htons(ETH_P_IPV6) &&
2203 eth_type != htons(ETH_P_ARP) &&
2204 eth_type != htons(ETH_P_RARP) &&
2205 !eth_p_mpls(eth_type)))
2206 return -EINVAL;
2207 eth_type = mpls->mpls_ethertype;
2208 break;
2209 }
2210
2211 case OVS_ACTION_ATTR_POP_MPLS:
2212 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2213 !eth_p_mpls(eth_type))
2214 return -EINVAL;
2215
2216 /* Disallow subsequent L2.5+ set and mpls_pop actions
2217 * as there is no check here to ensure that the new
2218 * eth_type is valid and thus set actions could
2219 * write off the end of the packet or otherwise
2220 * corrupt it.
2221 *
2222 * Support for these actions is planned using packet
2223 * recirculation.
2224 */
2225 eth_type = htons(0);
2226 break;
2227
Pravin B Shelare6445712013-10-03 18:16:47 -07002228 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002229 err = validate_set(a, key, sfa,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002230 &skip_copy, eth_type, false, log);
2231 if (err)
2232 return err;
2233 break;
2234
2235 case OVS_ACTION_ATTR_SET_MASKED:
2236 err = validate_set(a, key, sfa,
2237 &skip_copy, eth_type, true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002238 if (err)
2239 return err;
2240 break;
2241
2242 case OVS_ACTION_ATTR_SAMPLE:
Joe Stringer7f8a4362015-08-26 11:31:48 -07002243 err = validate_and_copy_sample(net, a, key, depth, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002244 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002245 if (err)
2246 return err;
2247 skip_copy = true;
2248 break;
2249
Joe Stringer7f8a4362015-08-26 11:31:48 -07002250 case OVS_ACTION_ATTR_CT:
2251 err = ovs_ct_copy_action(net, a, key, sfa, log);
2252 if (err)
2253 return err;
2254 skip_copy = true;
2255 break;
2256
Pravin B Shelare6445712013-10-03 18:16:47 -07002257 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002258 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002259 return -EINVAL;
2260 }
2261 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002262 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002263 if (err)
2264 return err;
2265 }
2266 }
2267
2268 if (rem > 0)
2269 return -EINVAL;
2270
2271 return 0;
2272}
2273
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002274/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07002275int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002276 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002277 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07002278{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002279 int err;
2280
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002281 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002282 if (IS_ERR(*sfa))
2283 return PTR_ERR(*sfa);
2284
Joe Stringer8e2fed12015-08-26 11:31:44 -07002285 (*sfa)->orig_len = nla_len(attr);
Joe Stringer7f8a4362015-08-26 11:31:48 -07002286 err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002287 key->eth.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002288 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02002289 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002290
2291 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002292}
2293
Pravin B Shelare6445712013-10-03 18:16:47 -07002294static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
2295{
2296 const struct nlattr *a;
2297 struct nlattr *start;
2298 int err = 0, rem;
2299
2300 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2301 if (!start)
2302 return -EMSGSIZE;
2303
2304 nla_for_each_nested(a, attr, rem) {
2305 int type = nla_type(a);
2306 struct nlattr *st_sample;
2307
2308 switch (type) {
2309 case OVS_SAMPLE_ATTR_PROBABILITY:
2310 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
2311 sizeof(u32), nla_data(a)))
2312 return -EMSGSIZE;
2313 break;
2314 case OVS_SAMPLE_ATTR_ACTIONS:
2315 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2316 if (!st_sample)
2317 return -EMSGSIZE;
2318 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
2319 if (err)
2320 return err;
2321 nla_nest_end(skb, st_sample);
2322 break;
2323 }
2324 }
2325
2326 nla_nest_end(skb, start);
2327 return err;
2328}
2329
2330static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2331{
2332 const struct nlattr *ovs_key = nla_data(a);
2333 int key_type = nla_type(ovs_key);
2334 struct nlattr *start;
2335 int err;
2336
2337 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07002338 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02002339 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2340 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002341
Pravin B Shelare6445712013-10-03 18:16:47 -07002342 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2343 if (!start)
2344 return -EMSGSIZE;
2345
Thomas Graf1d8fff92015-07-21 10:43:54 +02002346 err = ipv4_tun_to_nlattr(skb, &tun_info->key,
Jesse Grossf5796682014-10-03 15:35:33 -07002347 tun_info->options_len ?
2348 tun_info->options : NULL,
2349 tun_info->options_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07002350 if (err)
2351 return err;
2352 nla_nest_end(skb, start);
2353 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002354 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002355 default:
2356 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2357 return -EMSGSIZE;
2358 break;
2359 }
2360
2361 return 0;
2362}
2363
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002364static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2365 struct sk_buff *skb)
2366{
2367 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08002368 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002369 size_t key_len = nla_len(ovs_key) / 2;
2370
2371 /* Revert the conversion we did from a non-masked set action to
2372 * masked set action.
2373 */
Joe Stringerf4f8e732015-03-02 18:49:56 -08002374 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2375 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002376 return -EMSGSIZE;
2377
Joe Stringerf4f8e732015-03-02 18:49:56 -08002378 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2379 return -EMSGSIZE;
2380
2381 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002382 return 0;
2383}
2384
Pravin B Shelare6445712013-10-03 18:16:47 -07002385int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2386{
2387 const struct nlattr *a;
2388 int rem, err;
2389
2390 nla_for_each_attr(a, attr, len, rem) {
2391 int type = nla_type(a);
2392
2393 switch (type) {
2394 case OVS_ACTION_ATTR_SET:
2395 err = set_action_to_attr(a, skb);
2396 if (err)
2397 return err;
2398 break;
2399
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002400 case OVS_ACTION_ATTR_SET_TO_MASKED:
2401 err = masked_set_action_to_set_action_attr(a, skb);
2402 if (err)
2403 return err;
2404 break;
2405
Pravin B Shelare6445712013-10-03 18:16:47 -07002406 case OVS_ACTION_ATTR_SAMPLE:
2407 err = sample_action_to_attr(a, skb);
2408 if (err)
2409 return err;
2410 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002411
2412 case OVS_ACTION_ATTR_CT:
2413 err = ovs_ct_action_to_attr(nla_data(a), skb);
2414 if (err)
2415 return err;
2416 break;
2417
Pravin B Shelare6445712013-10-03 18:16:47 -07002418 default:
2419 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2420 return -EMSGSIZE;
2421 break;
2422 }
2423 }
2424
2425 return 0;
2426}