blob: 38536c137c54d0d4ebcebcce613fefdb89799b5a [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
Jesse Gross982b5272015-09-11 18:38:28 -070060#define OVS_ATTR_VARIABLE -2
Thomas Graf81bfe3c2015-01-15 03:53:58 +010061
Pravin B Shelara85311b2014-10-19 12:03:40 -070062static void update_range(struct sw_flow_match *match,
63 size_t offset, size_t size, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -070064{
Pravin B Shelara85311b2014-10-19 12:03:40 -070065 struct sw_flow_key_range *range;
Pravin B Shelare6445712013-10-03 18:16:47 -070066 size_t start = rounddown(offset, sizeof(long));
67 size_t end = roundup(offset + size, sizeof(long));
68
69 if (!is_mask)
70 range = &match->range;
Pravin B Shelara85311b2014-10-19 12:03:40 -070071 else
Pravin B Shelare6445712013-10-03 18:16:47 -070072 range = &match->mask->range;
73
Pravin B Shelare6445712013-10-03 18:16:47 -070074 if (range->start == range->end) {
75 range->start = start;
76 range->end = end;
77 return;
78 }
79
80 if (range->start > start)
81 range->start = start;
82
83 if (range->end < end)
84 range->end = end;
85}
86
87#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
88 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -070089 update_range(match, offsetof(struct sw_flow_key, field), \
90 sizeof((match)->key->field), is_mask); \
91 if (is_mask) \
92 (match)->mask->key.field = value; \
93 else \
Pravin B Shelare6445712013-10-03 18:16:47 -070094 (match)->key->field = value; \
Pravin B Shelare6445712013-10-03 18:16:47 -070095 } while (0)
96
Jesse Grossf5796682014-10-03 15:35:33 -070097#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
98 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -070099 update_range(match, offset, len, is_mask); \
Jesse Grossf5796682014-10-03 15:35:33 -0700100 if (is_mask) \
101 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700102 len); \
Jesse Grossf5796682014-10-03 15:35:33 -0700103 else \
104 memcpy((u8 *)(match)->key + offset, value_p, len); \
Pravin B Shelare6445712013-10-03 18:16:47 -0700105 } while (0)
106
Jesse Grossf5796682014-10-03 15:35:33 -0700107#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
108 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
109 value_p, len, is_mask)
110
Pravin B Shelara85311b2014-10-19 12:03:40 -0700111#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
112 do { \
113 update_range(match, offsetof(struct sw_flow_key, field), \
114 sizeof((match)->key->field), is_mask); \
115 if (is_mask) \
116 memset((u8 *)&(match)->mask->key.field, value, \
117 sizeof((match)->mask->key.field)); \
118 else \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700119 memset((u8 *)&(match)->key->field, value, \
120 sizeof((match)->key->field)); \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700121 } while (0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700122
123static bool match_validate(const struct sw_flow_match *match,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800124 u64 key_attrs, u64 mask_attrs, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700125{
126 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
127 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
128
129 /* The following mask attributes allowed only if they
130 * pass the validation tests. */
131 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
132 | (1 << OVS_KEY_ATTR_IPV6)
133 | (1 << OVS_KEY_ATTR_TCP)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700134 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700135 | (1 << OVS_KEY_ATTR_UDP)
136 | (1 << OVS_KEY_ATTR_SCTP)
137 | (1 << OVS_KEY_ATTR_ICMP)
138 | (1 << OVS_KEY_ATTR_ICMPV6)
139 | (1 << OVS_KEY_ATTR_ARP)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700140 | (1 << OVS_KEY_ATTR_ND)
141 | (1 << OVS_KEY_ATTR_MPLS));
Pravin B Shelare6445712013-10-03 18:16:47 -0700142
143 /* Always allowed mask fields. */
144 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
145 | (1 << OVS_KEY_ATTR_IN_PORT)
146 | (1 << OVS_KEY_ATTR_ETHERTYPE));
147
148 /* Check key attributes. */
149 if (match->key->eth.type == htons(ETH_P_ARP)
150 || match->key->eth.type == htons(ETH_P_RARP)) {
151 key_expected |= 1 << OVS_KEY_ATTR_ARP;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800152 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700153 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
154 }
155
Simon Horman25cd9ba2014-10-06 05:05:13 -0700156 if (eth_p_mpls(match->key->eth.type)) {
157 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
158 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
159 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
160 }
161
Pravin B Shelare6445712013-10-03 18:16:47 -0700162 if (match->key->eth.type == htons(ETH_P_IP)) {
163 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
164 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
165 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
166
167 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
168 if (match->key->ip.proto == IPPROTO_UDP) {
169 key_expected |= 1 << OVS_KEY_ATTR_UDP;
170 if (match->mask && (match->mask->key.ip.proto == 0xff))
171 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
172 }
173
174 if (match->key->ip.proto == IPPROTO_SCTP) {
175 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
176 if (match->mask && (match->mask->key.ip.proto == 0xff))
177 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
178 }
179
180 if (match->key->ip.proto == IPPROTO_TCP) {
181 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700182 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
183 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700184 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700185 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
186 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700187 }
188
189 if (match->key->ip.proto == IPPROTO_ICMP) {
190 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
191 if (match->mask && (match->mask->key.ip.proto == 0xff))
192 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
193 }
194 }
195 }
196
197 if (match->key->eth.type == htons(ETH_P_IPV6)) {
198 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
199 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
200 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
201
202 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
203 if (match->key->ip.proto == IPPROTO_UDP) {
204 key_expected |= 1 << OVS_KEY_ATTR_UDP;
205 if (match->mask && (match->mask->key.ip.proto == 0xff))
206 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
207 }
208
209 if (match->key->ip.proto == IPPROTO_SCTP) {
210 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
211 if (match->mask && (match->mask->key.ip.proto == 0xff))
212 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
213 }
214
215 if (match->key->ip.proto == IPPROTO_TCP) {
216 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700217 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
218 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700219 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700220 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
221 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700222 }
223
224 if (match->key->ip.proto == IPPROTO_ICMPV6) {
225 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
226 if (match->mask && (match->mask->key.ip.proto == 0xff))
227 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
228
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700229 if (match->key->tp.src ==
Pravin B Shelare6445712013-10-03 18:16:47 -0700230 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700231 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700232 key_expected |= 1 << OVS_KEY_ATTR_ND;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800233 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700234 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
235 }
236 }
237 }
238 }
239
240 if ((key_attrs & key_expected) != key_expected) {
241 /* Key attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800242 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
243 (unsigned long long)key_attrs,
244 (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700245 return false;
246 }
247
248 if ((mask_attrs & mask_allowed) != mask_attrs) {
249 /* Mask attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800250 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
251 (unsigned long long)mask_attrs,
252 (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700253 return false;
254 }
255
256 return true;
257}
258
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800259size_t ovs_tun_key_attr_size(void)
260{
261 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
262 * updating this function.
263 */
264 return nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
265 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
266 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
267 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
268 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
269 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
270 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
271 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
272 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
Thomas Graf1dd144c2015-01-15 03:53:59 +0100273 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
274 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
275 */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800276 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
277 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
278}
279
Joe Stringer41af73e2014-10-18 16:14:14 -0700280size_t ovs_key_attr_size(void)
281{
282 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
283 * updating this function.
284 */
Joe Stringerc2ac6672015-08-26 11:31:52 -0700285 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 26);
Joe Stringer41af73e2014-10-18 16:14:14 -0700286
287 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
288 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800289 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700290 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
291 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
292 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
293 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
Joe Stringerfbccce52015-10-06 11:00:00 -0700294 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700295 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
Joe Stringer182e3042015-08-26 11:31:49 -0700296 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
Joe Stringer33db4122015-10-01 15:00:37 -0700297 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
Joe Stringer41af73e2014-10-18 16:14:14 -0700298 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
299 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
300 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
301 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
302 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
303 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
304 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
305 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
306}
307
Jesse Gross982b5272015-09-11 18:38:28 -0700308static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
309 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
310};
311
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100312static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
313 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
314 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
315 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
316 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
317 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
318 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
319 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
320 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
321 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
322 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
Jesse Gross982b5272015-09-11 18:38:28 -0700323 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
324 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
325 .next = ovs_vxlan_ext_key_lens },
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100326};
327
Pravin B Shelare6445712013-10-03 18:16:47 -0700328/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100329static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
330 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
331 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
332 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
333 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
334 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
335 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
336 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
337 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
338 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
339 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
340 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
341 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
342 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
343 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
344 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
345 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
346 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
347 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
348 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
349 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
350 .next = ovs_tunnel_key_lens, },
351 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
Joe Stringerfbccce52015-10-06 11:00:00 -0700352 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700353 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700354 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
Joe Stringer33db4122015-10-01 15:00:37 -0700355 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
Pravin B Shelare6445712013-10-03 18:16:47 -0700356};
357
Jesse Gross982b5272015-09-11 18:38:28 -0700358static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
359{
360 return expected_len == attr_len ||
361 expected_len == OVS_ATTR_NESTED ||
362 expected_len == OVS_ATTR_VARIABLE;
363}
364
Pravin B Shelare6445712013-10-03 18:16:47 -0700365static bool is_all_zero(const u8 *fp, size_t size)
366{
367 int i;
368
369 if (!fp)
370 return false;
371
372 for (i = 0; i < size; i++)
373 if (fp[i])
374 return false;
375
376 return true;
377}
378
379static int __parse_flow_nlattrs(const struct nlattr *attr,
380 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800381 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700382{
383 const struct nlattr *nla;
384 u64 attrs;
385 int rem;
386
387 attrs = *attrsp;
388 nla_for_each_nested(nla, attr, rem) {
389 u16 type = nla_type(nla);
390 int expected_len;
391
392 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800393 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700394 type, OVS_KEY_ATTR_MAX);
395 return -EINVAL;
396 }
397
398 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800399 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700400 return -EINVAL;
401 }
402
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100403 expected_len = ovs_key_lens[type].len;
Jesse Gross982b5272015-09-11 18:38:28 -0700404 if (!check_attr_len(nla_len(nla), expected_len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800405 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
406 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700407 return -EINVAL;
408 }
409
410 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
411 attrs |= 1 << type;
412 a[type] = nla;
413 }
414 }
415 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800416 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700417 return -EINVAL;
418 }
419
420 *attrsp = attrs;
421 return 0;
422}
423
424static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800425 const struct nlattr *a[], u64 *attrsp,
426 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700427{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800428 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700429}
430
431static int parse_flow_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800432 const struct nlattr *a[], u64 *attrsp,
433 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700434{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800435 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
436}
437
438static int genev_tun_opt_from_nlattr(const struct nlattr *a,
439 struct sw_flow_match *match, bool is_mask,
440 bool log)
441{
442 unsigned long opt_key_offset;
443
444 if (nla_len(a) > sizeof(match->key->tun_opts)) {
445 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
446 nla_len(a), sizeof(match->key->tun_opts));
447 return -EINVAL;
448 }
449
450 if (nla_len(a) % 4 != 0) {
451 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
452 nla_len(a));
453 return -EINVAL;
454 }
455
456 /* We need to record the length of the options passed
457 * down, otherwise packets with the same format but
458 * additional options will be silently matched.
459 */
460 if (!is_mask) {
461 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
462 false);
463 } else {
464 /* This is somewhat unusual because it looks at
465 * both the key and mask while parsing the
466 * attributes (and by extension assumes the key
467 * is parsed first). Normally, we would verify
468 * that each is the correct length and that the
469 * attributes line up in the validate function.
470 * However, that is difficult because this is
471 * variable length and we won't have the
472 * information later.
473 */
474 if (match->key->tun_opts_len != nla_len(a)) {
475 OVS_NLERR(log, "Geneve option len %d != mask len %d",
476 match->key->tun_opts_len, nla_len(a));
477 return -EINVAL;
478 }
479
480 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
481 }
482
Thomas Grafd91641d2015-01-15 03:53:57 +0100483 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800484 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
485 nla_len(a), is_mask);
486 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700487}
488
Jesse Gross982b5272015-09-11 18:38:28 -0700489static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100490 struct sw_flow_match *match, bool is_mask,
491 bool log)
492{
Jesse Gross982b5272015-09-11 18:38:28 -0700493 struct nlattr *a;
494 int rem;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100495 unsigned long opt_key_offset;
Thomas Graf614732e2015-07-21 10:44:06 +0200496 struct vxlan_metadata opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100497
498 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
499
Thomas Graf1dd144c2015-01-15 03:53:59 +0100500 memset(&opts, 0, sizeof(opts));
Jesse Gross982b5272015-09-11 18:38:28 -0700501 nla_for_each_nested(a, attr, rem) {
502 int type = nla_type(a);
Thomas Graf1dd144c2015-01-15 03:53:59 +0100503
Jesse Gross982b5272015-09-11 18:38:28 -0700504 if (type > OVS_VXLAN_EXT_MAX) {
505 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
506 type, OVS_VXLAN_EXT_MAX);
507 return -EINVAL;
508 }
509
510 if (!check_attr_len(nla_len(a),
511 ovs_vxlan_ext_key_lens[type].len)) {
512 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
513 type, nla_len(a),
514 ovs_vxlan_ext_key_lens[type].len);
515 return -EINVAL;
516 }
517
518 switch (type) {
519 case OVS_VXLAN_EXT_GBP:
520 opts.gbp = nla_get_u32(a);
521 break;
522 default:
523 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
524 type);
525 return -EINVAL;
526 }
527 }
528 if (rem) {
529 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
530 rem);
531 return -EINVAL;
532 }
Thomas Graf1dd144c2015-01-15 03:53:59 +0100533
534 if (!is_mask)
535 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
536 else
537 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
538
539 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
540 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
541 is_mask);
542 return 0;
543}
544
Pravin B Shelare6445712013-10-03 18:16:47 -0700545static int ipv4_tun_from_nlattr(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800546 struct sw_flow_match *match, bool is_mask,
547 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700548{
549 struct nlattr *a;
550 int rem;
551 bool ttl = false;
552 __be16 tun_flags = 0;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100553 int opts_type = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700554
555 nla_for_each_nested(a, attr, rem) {
556 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800557 int err;
558
Pravin B Shelare6445712013-10-03 18:16:47 -0700559 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800560 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
561 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700562 return -EINVAL;
563 }
564
Jesse Gross982b5272015-09-11 18:38:28 -0700565 if (!check_attr_len(nla_len(a),
566 ovs_tunnel_key_lens[type].len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800567 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c2015-01-15 03:53:58 +0100568 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700569 return -EINVAL;
570 }
571
572 switch (type) {
573 case OVS_TUNNEL_KEY_ATTR_ID:
574 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
575 nla_get_be64(a), is_mask);
576 tun_flags |= TUNNEL_KEY;
577 break;
578 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200579 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
Jiri Benc67b61f62015-03-29 16:59:26 +0200580 nla_get_in_addr(a), is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700581 break;
582 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200583 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
Jiri Benc67b61f62015-03-29 16:59:26 +0200584 nla_get_in_addr(a), is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700585 break;
586 case OVS_TUNNEL_KEY_ATTR_TOS:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200587 SW_FLOW_KEY_PUT(match, tun_key.tos,
Pravin B Shelare6445712013-10-03 18:16:47 -0700588 nla_get_u8(a), is_mask);
589 break;
590 case OVS_TUNNEL_KEY_ATTR_TTL:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200591 SW_FLOW_KEY_PUT(match, tun_key.ttl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700592 nla_get_u8(a), is_mask);
593 ttl = true;
594 break;
595 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
596 tun_flags |= TUNNEL_DONT_FRAGMENT;
597 break;
598 case OVS_TUNNEL_KEY_ATTR_CSUM:
599 tun_flags |= TUNNEL_CSUM;
600 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800601 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
602 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
603 nla_get_be16(a), is_mask);
604 break;
605 case OVS_TUNNEL_KEY_ATTR_TP_DST:
606 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
607 nla_get_be16(a), is_mask);
608 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700609 case OVS_TUNNEL_KEY_ATTR_OAM:
610 tun_flags |= TUNNEL_OAM;
611 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700612 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Thomas Graf1dd144c2015-01-15 03:53:59 +0100613 if (opts_type) {
614 OVS_NLERR(log, "Multiple metadata blocks provided");
615 return -EINVAL;
616 }
617
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800618 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
619 if (err)
620 return err;
621
Thomas Graf1dd144c2015-01-15 03:53:59 +0100622 tun_flags |= TUNNEL_GENEVE_OPT;
623 opts_type = type;
624 break;
625 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
626 if (opts_type) {
627 OVS_NLERR(log, "Multiple metadata blocks provided");
628 return -EINVAL;
629 }
630
631 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
632 if (err)
633 return err;
634
635 tun_flags |= TUNNEL_VXLAN_OPT;
636 opts_type = type;
Jesse Grossf5796682014-10-03 15:35:33 -0700637 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700638 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800639 OVS_NLERR(log, "Unknown IPv4 tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700640 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700641 return -EINVAL;
642 }
643 }
644
645 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
646
647 if (rem > 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800648 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
649 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700650 return -EINVAL;
651 }
652
653 if (!is_mask) {
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200654 if (!match->key->tun_key.u.ipv4.dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800655 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700656 return -EINVAL;
657 }
658
659 if (!ttl) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800660 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700661 return -EINVAL;
662 }
663 }
664
Thomas Graf1dd144c2015-01-15 03:53:59 +0100665 return opts_type;
666}
667
668static int vxlan_opt_to_nlattr(struct sk_buff *skb,
669 const void *tun_opts, int swkey_tun_opts_len)
670{
Thomas Graf614732e2015-07-21 10:44:06 +0200671 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100672 struct nlattr *nla;
673
674 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
675 if (!nla)
676 return -EMSGSIZE;
677
678 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
679 return -EMSGSIZE;
680
681 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700682 return 0;
683}
684
Jesse Grossf5796682014-10-03 15:35:33 -0700685static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200686 const struct ip_tunnel_key *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100687 const void *tun_opts, int swkey_tun_opts_len)
Pravin B Shelare6445712013-10-03 18:16:47 -0700688{
Pravin B Shelare6445712013-10-03 18:16:47 -0700689 if (output->tun_flags & TUNNEL_KEY &&
690 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
691 return -EMSGSIZE;
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200692 if (output->u.ipv4.src &&
Jiri Benc930345e2015-03-29 16:59:25 +0200693 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200694 output->u.ipv4.src))
Pravin B Shelare6445712013-10-03 18:16:47 -0700695 return -EMSGSIZE;
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200696 if (output->u.ipv4.dst &&
Jiri Benc930345e2015-03-29 16:59:25 +0200697 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200698 output->u.ipv4.dst))
Pravin B Shelare6445712013-10-03 18:16:47 -0700699 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200700 if (output->tos &&
701 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700702 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200703 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700704 return -EMSGSIZE;
705 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700706 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700707 return -EMSGSIZE;
708 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700709 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
710 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800711 if (output->tp_src &&
712 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
713 return -EMSGSIZE;
714 if (output->tp_dst &&
715 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
716 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700717 if ((output->tun_flags & TUNNEL_OAM) &&
718 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700719 return -EMSGSIZE;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700720 if (swkey_tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +0100721 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
722 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
723 swkey_tun_opts_len, tun_opts))
724 return -EMSGSIZE;
725 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
726 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
727 return -EMSGSIZE;
728 }
Jesse Grossf5796682014-10-03 15:35:33 -0700729
730 return 0;
731}
732
Jesse Grossf5796682014-10-03 15:35:33 -0700733static int ipv4_tun_to_nlattr(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200734 const struct ip_tunnel_key *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100735 const void *tun_opts, int swkey_tun_opts_len)
Jesse Grossf5796682014-10-03 15:35:33 -0700736{
737 struct nlattr *nla;
738 int err;
739
740 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
741 if (!nla)
742 return -EMSGSIZE;
743
744 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
745 if (err)
746 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700747
748 nla_nest_end(skb, nla);
749 return 0;
750}
751
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700752int ovs_nla_put_tunnel_info(struct sk_buff *skb,
753 struct ip_tunnel_info *tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800754{
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700755 return __ipv4_tun_to_nlattr(skb, &tun_info->key,
756 ip_tunnel_info_opts(tun_info),
757 tun_info->options_len);
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800758}
759
Joe Stringerc2ac6672015-08-26 11:31:52 -0700760static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
761 u64 *attrs, const struct nlattr **a,
762 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700763{
Andy Zhou971427f32014-09-15 19:37:25 -0700764 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
765 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
766
767 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
768 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
769 }
770
771 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
772 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
773
774 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
775 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
776 }
777
Pravin B Shelare6445712013-10-03 18:16:47 -0700778 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
779 SW_FLOW_KEY_PUT(match, phy.priority,
780 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
781 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
782 }
783
784 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
785 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
786
Jesse Gross426cda52014-10-06 05:08:38 -0700787 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700788 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -0700789 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800790 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -0700791 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -0700792 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700793 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700794
795 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
796 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
797 } else if (!is_mask) {
798 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
799 }
800
801 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
802 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
803
804 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
805 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
806 }
807 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
808 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100809 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700810 return -EINVAL;
811 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
812 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700813
814 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -0700815 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
Joe Stringerfbccce52015-10-06 11:00:00 -0700816 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700817
Joe Stringer9e384712015-10-19 19:18:57 -0700818 if (ct_state & ~CT_SUPPORTED_MASK) {
Joe Stringerfbccce52015-10-06 11:00:00 -0700819 OVS_NLERR(log, "ct_state flags %08x unsupported",
Joe Stringer6f225952015-10-06 10:59:59 -0700820 ct_state);
821 return -EINVAL;
822 }
823
Joe Stringer7f8a4362015-08-26 11:31:48 -0700824 SW_FLOW_KEY_PUT(match, ct.state, ct_state, is_mask);
825 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
826 }
827 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -0700828 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700829 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
830
831 SW_FLOW_KEY_PUT(match, ct.zone, ct_zone, is_mask);
832 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
833 }
Joe Stringer182e3042015-08-26 11:31:49 -0700834 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -0700835 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
Joe Stringer182e3042015-08-26 11:31:49 -0700836 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
837
838 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
839 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
840 }
Joe Stringer33db4122015-10-01 15:00:37 -0700841 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
842 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
843 const struct ovs_key_ct_labels *cl;
Joe Stringerc2ac6672015-08-26 11:31:52 -0700844
Joe Stringer33db4122015-10-01 15:00:37 -0700845 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
846 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
Joe Stringerc2ac6672015-08-26 11:31:52 -0700847 sizeof(*cl), is_mask);
Joe Stringer33db4122015-10-01 15:00:37 -0700848 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700849 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700850 return 0;
851}
852
Joe Stringerc2ac6672015-08-26 11:31:52 -0700853static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
854 u64 attrs, const struct nlattr **a,
855 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700856{
857 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700858
Joe Stringerc2ac6672015-08-26 11:31:52 -0700859 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -0700860 if (err)
861 return err;
862
863 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
864 const struct ovs_key_ethernet *eth_key;
865
866 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
867 SW_FLOW_KEY_MEMCPY(match, eth.src,
868 eth_key->eth_src, ETH_ALEN, is_mask);
869 SW_FLOW_KEY_MEMCPY(match, eth.dst,
870 eth_key->eth_dst, ETH_ALEN, is_mask);
871 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
872 }
873
874 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
875 __be16 tci;
876
877 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
878 if (!(tci & htons(VLAN_TAG_PRESENT))) {
879 if (is_mask)
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800880 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700881 else
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800882 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700883
884 return -EINVAL;
885 }
886
887 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
888 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
Pravin B Shelara85311b2014-10-19 12:03:40 -0700889 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700890
891 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
892 __be16 eth_type;
893
894 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
895 if (is_mask) {
896 /* Always exact match EtherType. */
897 eth_type = htons(0xffff);
Alexander Duyck6713fc92015-05-04 14:34:05 -0700898 } else if (!eth_proto_is_802_3(eth_type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800899 OVS_NLERR(log, "EtherType %x is less than min %x",
900 ntohs(eth_type), ETH_P_802_3_MIN);
Pravin B Shelare6445712013-10-03 18:16:47 -0700901 return -EINVAL;
902 }
903
904 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
905 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
906 } else if (!is_mask) {
907 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
908 }
909
910 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
911 const struct ovs_key_ipv4 *ipv4_key;
912
913 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
914 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800915 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
916 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700917 return -EINVAL;
918 }
919 SW_FLOW_KEY_PUT(match, ip.proto,
920 ipv4_key->ipv4_proto, is_mask);
921 SW_FLOW_KEY_PUT(match, ip.tos,
922 ipv4_key->ipv4_tos, is_mask);
923 SW_FLOW_KEY_PUT(match, ip.ttl,
924 ipv4_key->ipv4_ttl, is_mask);
925 SW_FLOW_KEY_PUT(match, ip.frag,
926 ipv4_key->ipv4_frag, is_mask);
927 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
928 ipv4_key->ipv4_src, is_mask);
929 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
930 ipv4_key->ipv4_dst, is_mask);
931 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
932 }
933
934 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
935 const struct ovs_key_ipv6 *ipv6_key;
936
937 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
938 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800939 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
940 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700941 return -EINVAL;
942 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800943
Joe Stringerd3052bb2014-11-19 13:54:49 -0800944 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
David S. Miller14591432014-11-21 22:28:24 -0500945 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800946 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
947 return -EINVAL;
948 }
949
Pravin B Shelare6445712013-10-03 18:16:47 -0700950 SW_FLOW_KEY_PUT(match, ipv6.label,
951 ipv6_key->ipv6_label, is_mask);
952 SW_FLOW_KEY_PUT(match, ip.proto,
953 ipv6_key->ipv6_proto, is_mask);
954 SW_FLOW_KEY_PUT(match, ip.tos,
955 ipv6_key->ipv6_tclass, is_mask);
956 SW_FLOW_KEY_PUT(match, ip.ttl,
957 ipv6_key->ipv6_hlimit, is_mask);
958 SW_FLOW_KEY_PUT(match, ip.frag,
959 ipv6_key->ipv6_frag, is_mask);
960 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
961 ipv6_key->ipv6_src,
962 sizeof(match->key->ipv6.addr.src),
963 is_mask);
964 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
965 ipv6_key->ipv6_dst,
966 sizeof(match->key->ipv6.addr.dst),
967 is_mask);
968
969 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
970 }
971
972 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
973 const struct ovs_key_arp *arp_key;
974
975 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
976 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800977 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -0700978 arp_key->arp_op);
979 return -EINVAL;
980 }
981
982 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
983 arp_key->arp_sip, is_mask);
984 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
985 arp_key->arp_tip, is_mask);
986 SW_FLOW_KEY_PUT(match, ip.proto,
987 ntohs(arp_key->arp_op), is_mask);
988 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
989 arp_key->arp_sha, ETH_ALEN, is_mask);
990 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
991 arp_key->arp_tha, ETH_ALEN, is_mask);
992
993 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
994 }
995
Simon Horman25cd9ba2014-10-06 05:05:13 -0700996 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
997 const struct ovs_key_mpls *mpls_key;
998
999 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1000 SW_FLOW_KEY_PUT(match, mpls.top_lse,
1001 mpls_key->mpls_lse, is_mask);
1002
1003 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1004 }
1005
Pravin B Shelare6445712013-10-03 18:16:47 -07001006 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1007 const struct ovs_key_tcp *tcp_key;
1008
1009 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001010 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1011 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001012 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1013 }
1014
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001015 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -07001016 SW_FLOW_KEY_PUT(match, tp.flags,
1017 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1018 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001019 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1020 }
1021
Pravin B Shelare6445712013-10-03 18:16:47 -07001022 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1023 const struct ovs_key_udp *udp_key;
1024
1025 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001026 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1027 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001028 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1029 }
1030
1031 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1032 const struct ovs_key_sctp *sctp_key;
1033
1034 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001035 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1036 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001037 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1038 }
1039
1040 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1041 const struct ovs_key_icmp *icmp_key;
1042
1043 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001044 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001045 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001046 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001047 htons(icmp_key->icmp_code), is_mask);
1048 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1049 }
1050
1051 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1052 const struct ovs_key_icmpv6 *icmpv6_key;
1053
1054 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001055 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001056 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001057 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001058 htons(icmpv6_key->icmpv6_code), is_mask);
1059 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1060 }
1061
1062 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1063 const struct ovs_key_nd *nd_key;
1064
1065 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1066 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1067 nd_key->nd_target,
1068 sizeof(match->key->ipv6.nd.target),
1069 is_mask);
1070 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1071 nd_key->nd_sll, ETH_ALEN, is_mask);
1072 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1073 nd_key->nd_tll, ETH_ALEN, is_mask);
1074 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1075 }
1076
Jesse Gross426cda52014-10-06 05:08:38 -07001077 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001078 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001079 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001080 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001081 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001082
1083 return 0;
1084}
1085
Thomas Graf81bfe3c2015-01-15 03:53:58 +01001086static void nlattr_set(struct nlattr *attr, u8 val,
1087 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001088{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001089 struct nlattr *nla;
1090 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001091
Pravin B Shelarf47de062014-10-16 21:55:45 -07001092 /* The nlattr stream should already have been validated */
1093 nla_for_each_nested(nla, attr, rem) {
Jesse Gross982b5272015-09-11 18:38:28 -07001094 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1095 if (tbl[nla_type(nla)].next)
1096 tbl = tbl[nla_type(nla)].next;
1097 nlattr_set(nla, val, tbl);
1098 } else {
Pravin B Shelarf47de062014-10-16 21:55:45 -07001099 memset(nla_data(nla), val, nla_len(nla));
Jesse Gross982b5272015-09-11 18:38:28 -07001100 }
Joe Stringer9e384712015-10-19 19:18:57 -07001101
1102 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1103 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001104 }
1105}
1106
1107static void mask_set_nlattr(struct nlattr *attr, u8 val)
1108{
Thomas Graf81bfe3c2015-01-15 03:53:58 +01001109 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001110}
1111
1112/**
1113 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1114 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1115 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1116 * does not include any don't care bit.
Joe Stringerc2ac6672015-08-26 11:31:52 -07001117 * @net: Used to determine per-namespace field support.
Pravin B Shelare6445712013-10-03 18:16:47 -07001118 * @match: receives the extracted flow match information.
1119 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1120 * sequence. The fields should of the packet that triggered the creation
1121 * of this flow.
1122 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1123 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001124 * @log: Boolean to allow kernel error logging. Normally true, but when
1125 * probing for feature compatibility this should be passed in as false to
1126 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001127 */
Joe Stringerc2ac6672015-08-26 11:31:52 -07001128int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001129 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001130 const struct nlattr *nla_mask,
1131 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001132{
1133 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1134 const struct nlattr *encap;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001135 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001136 u64 key_attrs = 0;
1137 u64 mask_attrs = 0;
1138 bool encap_valid = false;
1139 int err;
1140
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001141 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001142 if (err)
1143 return err;
1144
1145 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
1146 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
1147 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1148 __be16 tci;
1149
1150 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
1151 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001152 OVS_NLERR(log, "Invalid Vlan frame.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001153 return -EINVAL;
1154 }
1155
1156 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1157 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1158 encap = a[OVS_KEY_ATTR_ENCAP];
1159 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1160 encap_valid = true;
1161
1162 if (tci & htons(VLAN_TAG_PRESENT)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001163 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001164 if (err)
1165 return err;
1166 } else if (!tci) {
1167 /* Corner case for truncated 802.1Q header. */
1168 if (nla_len(encap)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001169 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001170 return -EINVAL;
1171 }
1172 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001173 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
Pravin B Shelare6445712013-10-03 18:16:47 -07001174 return -EINVAL;
1175 }
1176 }
1177
Joe Stringerc2ac6672015-08-26 11:31:52 -07001178 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001179 if (err)
1180 return err;
1181
Pravin B Shelara85311b2014-10-19 12:03:40 -07001182 if (match->mask) {
1183 if (!nla_mask) {
1184 /* Create an exact match mask. We need to set to 0xff
1185 * all the 'match->mask' fields that have been touched
1186 * in 'match->key'. We cannot simply memset
1187 * 'match->mask', because padding bytes and fields not
1188 * specified in 'match->key' should be left to 0.
1189 * Instead, we use a stream of netlink attributes,
1190 * copied from 'key' and set to 0xff.
1191 * ovs_key_from_nlattrs() will take care of filling
1192 * 'match->mask' appropriately.
1193 */
1194 newmask = kmemdup(nla_key,
1195 nla_total_size(nla_len(nla_key)),
1196 GFP_KERNEL);
1197 if (!newmask)
1198 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001199
Pravin B Shelara85311b2014-10-19 12:03:40 -07001200 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001201
Pravin B Shelara85311b2014-10-19 12:03:40 -07001202 /* The userspace does not send tunnel attributes that
1203 * are 0, but we should not wildcard them nonetheless.
1204 */
Jiri Bencc1ea5d62015-08-20 13:56:23 +02001205 if (match->key->tun_key.u.ipv4.dst)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001206 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1207 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001208
Pravin B Shelara85311b2014-10-19 12:03:40 -07001209 nla_mask = newmask;
1210 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001211
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001212 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001213 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001214 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001215
Pravin B Shelara85311b2014-10-19 12:03:40 -07001216 /* Always match on tci. */
1217 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1218
Pravin B Shelarf47de062014-10-16 21:55:45 -07001219 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001220 __be16 eth_type = 0;
1221 __be16 tci = 0;
1222
1223 if (!encap_valid) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001224 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
Pravin B Shelarf47de062014-10-16 21:55:45 -07001225 err = -EINVAL;
1226 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001227 }
1228
1229 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1230 if (a[OVS_KEY_ATTR_ETHERTYPE])
1231 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1232
1233 if (eth_type == htons(0xffff)) {
1234 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1235 encap = a[OVS_KEY_ATTR_ENCAP];
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001236 err = parse_flow_mask_nlattrs(encap, a,
1237 &mask_attrs, log);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001238 if (err)
1239 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001240 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001241 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1242 ntohs(eth_type));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001243 err = -EINVAL;
1244 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001245 }
1246
1247 if (a[OVS_KEY_ATTR_VLAN])
1248 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1249
1250 if (!(tci & htons(VLAN_TAG_PRESENT))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001251 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1252 ntohs(tci));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001253 err = -EINVAL;
1254 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001255 }
1256 }
1257
Joe Stringerc2ac6672015-08-26 11:31:52 -07001258 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1259 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001260 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001261 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001262 }
1263
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001264 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001265 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001266
Pravin B Shelarf47de062014-10-16 21:55:45 -07001267free_newmask:
1268 kfree(newmask);
1269 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001270}
1271
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001272static size_t get_ufid_len(const struct nlattr *attr, bool log)
1273{
1274 size_t len;
1275
1276 if (!attr)
1277 return 0;
1278
1279 len = nla_len(attr);
1280 if (len < 1 || len > MAX_UFID_LENGTH) {
1281 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1282 nla_len(attr), MAX_UFID_LENGTH);
1283 return 0;
1284 }
1285
1286 return len;
1287}
1288
1289/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1290 * or false otherwise.
1291 */
1292bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1293 bool log)
1294{
1295 sfid->ufid_len = get_ufid_len(attr, log);
1296 if (sfid->ufid_len)
1297 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1298
1299 return sfid->ufid_len;
1300}
1301
1302int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1303 const struct sw_flow_key *key, bool log)
1304{
1305 struct sw_flow_key *new_key;
1306
1307 if (ovs_nla_get_ufid(sfid, ufid, log))
1308 return 0;
1309
1310 /* If UFID was not provided, use unmasked key. */
1311 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1312 if (!new_key)
1313 return -ENOMEM;
1314 memcpy(new_key, key, sizeof(*key));
1315 sfid->unmasked_key = new_key;
1316
1317 return 0;
1318}
1319
1320u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1321{
1322 return attr ? nla_get_u32(attr) : 0;
1323}
1324
Pravin B Shelare6445712013-10-03 18:16:47 -07001325/**
1326 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001327 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -07001328 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1329 * sequence.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001330 * @log: Boolean to allow kernel error logging. Normally true, but when
1331 * probing for feature compatibility this should be passed in as false to
1332 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001333 *
1334 * This parses a series of Netlink attributes that form a flow key, which must
1335 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1336 * get the metadata, that is, the parts of the flow key that cannot be
1337 * extracted from the packet itself.
1338 */
1339
Joe Stringerc2ac6672015-08-26 11:31:52 -07001340int ovs_nla_get_flow_metadata(struct net *net, const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001341 struct sw_flow_key *key,
1342 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001343{
Pravin B Shelare6445712013-10-03 18:16:47 -07001344 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001345 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001346 u64 attrs = 0;
1347 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001348
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001349 err = parse_flow_nlattrs(attr, a, &attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001350 if (err)
1351 return -EINVAL;
1352
1353 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001354 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001355
Joe Stringer7f8a4362015-08-26 11:31:48 -07001356 memset(&key->ct, 0, sizeof(key->ct));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001357 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001358
Joe Stringerc2ac6672015-08-26 11:31:52 -07001359 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001360}
1361
Joe Stringer5b4237b2015-01-21 16:42:48 -08001362static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1363 const struct sw_flow_key *output, bool is_mask,
1364 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001365{
1366 struct ovs_key_ethernet *eth_key;
1367 struct nlattr *nla, *encap;
Pravin B Shelare6445712013-10-03 18:16:47 -07001368
Andy Zhou971427f32014-09-15 19:37:25 -07001369 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1370 goto nla_put_failure;
1371
1372 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1373 goto nla_put_failure;
1374
Pravin B Shelare6445712013-10-03 18:16:47 -07001375 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1376 goto nla_put_failure;
1377
Jiri Bencc1ea5d62015-08-20 13:56:23 +02001378 if ((swkey->tun_key.u.ipv4.dst || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001379 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001380
1381 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001382 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001383
1384 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1385 swkey->tun_opts_len))
1386 goto nla_put_failure;
1387 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001388
1389 if (swkey->phy.in_port == DP_MAX_PORTS) {
1390 if (is_mask && (output->phy.in_port == 0xffff))
1391 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1392 goto nla_put_failure;
1393 } else {
1394 u16 upper_u16;
1395 upper_u16 = !is_mask ? 0 : 0xffff;
1396
1397 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1398 (upper_u16 << 16) | output->phy.in_port))
1399 goto nla_put_failure;
1400 }
1401
1402 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1403 goto nla_put_failure;
1404
Joe Stringer7f8a4362015-08-26 11:31:48 -07001405 if (ovs_ct_put_key(output, skb))
1406 goto nla_put_failure;
1407
Pravin B Shelare6445712013-10-03 18:16:47 -07001408 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1409 if (!nla)
1410 goto nla_put_failure;
1411
1412 eth_key = nla_data(nla);
Joe Perches8c63ff02014-02-18 11:15:45 -08001413 ether_addr_copy(eth_key->eth_src, output->eth.src);
1414 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001415
1416 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1417 __be16 eth_type;
1418 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1419 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1420 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1421 goto nla_put_failure;
1422 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1423 if (!swkey->eth.tci)
1424 goto unencap;
1425 } else
1426 encap = NULL;
1427
1428 if (swkey->eth.type == htons(ETH_P_802_2)) {
1429 /*
1430 * Ethertype 802.2 is represented in the netlink with omitted
1431 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1432 * 0xffff in the mask attribute. Ethertype can also
1433 * be wildcarded.
1434 */
1435 if (is_mask && output->eth.type)
1436 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1437 output->eth.type))
1438 goto nla_put_failure;
1439 goto unencap;
1440 }
1441
1442 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1443 goto nla_put_failure;
1444
1445 if (swkey->eth.type == htons(ETH_P_IP)) {
1446 struct ovs_key_ipv4 *ipv4_key;
1447
1448 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1449 if (!nla)
1450 goto nla_put_failure;
1451 ipv4_key = nla_data(nla);
1452 ipv4_key->ipv4_src = output->ipv4.addr.src;
1453 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1454 ipv4_key->ipv4_proto = output->ip.proto;
1455 ipv4_key->ipv4_tos = output->ip.tos;
1456 ipv4_key->ipv4_ttl = output->ip.ttl;
1457 ipv4_key->ipv4_frag = output->ip.frag;
1458 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1459 struct ovs_key_ipv6 *ipv6_key;
1460
1461 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1462 if (!nla)
1463 goto nla_put_failure;
1464 ipv6_key = nla_data(nla);
1465 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1466 sizeof(ipv6_key->ipv6_src));
1467 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1468 sizeof(ipv6_key->ipv6_dst));
1469 ipv6_key->ipv6_label = output->ipv6.label;
1470 ipv6_key->ipv6_proto = output->ip.proto;
1471 ipv6_key->ipv6_tclass = output->ip.tos;
1472 ipv6_key->ipv6_hlimit = output->ip.ttl;
1473 ipv6_key->ipv6_frag = output->ip.frag;
1474 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1475 swkey->eth.type == htons(ETH_P_RARP)) {
1476 struct ovs_key_arp *arp_key;
1477
1478 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1479 if (!nla)
1480 goto nla_put_failure;
1481 arp_key = nla_data(nla);
1482 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1483 arp_key->arp_sip = output->ipv4.addr.src;
1484 arp_key->arp_tip = output->ipv4.addr.dst;
1485 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001486 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1487 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001488 } else if (eth_p_mpls(swkey->eth.type)) {
1489 struct ovs_key_mpls *mpls_key;
1490
1491 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1492 if (!nla)
1493 goto nla_put_failure;
1494 mpls_key = nla_data(nla);
1495 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001496 }
1497
1498 if ((swkey->eth.type == htons(ETH_P_IP) ||
1499 swkey->eth.type == htons(ETH_P_IPV6)) &&
1500 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1501
1502 if (swkey->ip.proto == IPPROTO_TCP) {
1503 struct ovs_key_tcp *tcp_key;
1504
1505 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1506 if (!nla)
1507 goto nla_put_failure;
1508 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001509 tcp_key->tcp_src = output->tp.src;
1510 tcp_key->tcp_dst = output->tp.dst;
1511 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1512 output->tp.flags))
1513 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001514 } else if (swkey->ip.proto == IPPROTO_UDP) {
1515 struct ovs_key_udp *udp_key;
1516
1517 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1518 if (!nla)
1519 goto nla_put_failure;
1520 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001521 udp_key->udp_src = output->tp.src;
1522 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001523 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1524 struct ovs_key_sctp *sctp_key;
1525
1526 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1527 if (!nla)
1528 goto nla_put_failure;
1529 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001530 sctp_key->sctp_src = output->tp.src;
1531 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001532 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1533 swkey->ip.proto == IPPROTO_ICMP) {
1534 struct ovs_key_icmp *icmp_key;
1535
1536 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1537 if (!nla)
1538 goto nla_put_failure;
1539 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001540 icmp_key->icmp_type = ntohs(output->tp.src);
1541 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001542 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1543 swkey->ip.proto == IPPROTO_ICMPV6) {
1544 struct ovs_key_icmpv6 *icmpv6_key;
1545
1546 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1547 sizeof(*icmpv6_key));
1548 if (!nla)
1549 goto nla_put_failure;
1550 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001551 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1552 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001553
1554 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1555 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1556 struct ovs_key_nd *nd_key;
1557
1558 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1559 if (!nla)
1560 goto nla_put_failure;
1561 nd_key = nla_data(nla);
1562 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1563 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001564 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1565 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001566 }
1567 }
1568 }
1569
1570unencap:
1571 if (encap)
1572 nla_nest_end(skb, encap);
1573
1574 return 0;
1575
1576nla_put_failure:
1577 return -EMSGSIZE;
1578}
1579
Joe Stringer5b4237b2015-01-21 16:42:48 -08001580int ovs_nla_put_key(const struct sw_flow_key *swkey,
1581 const struct sw_flow_key *output, int attr, bool is_mask,
1582 struct sk_buff *skb)
1583{
1584 int err;
1585 struct nlattr *nla;
1586
1587 nla = nla_nest_start(skb, attr);
1588 if (!nla)
1589 return -EMSGSIZE;
1590 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1591 if (err)
1592 return err;
1593 nla_nest_end(skb, nla);
1594
1595 return 0;
1596}
1597
1598/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001599int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08001600{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001601 if (ovs_identifier_is_ufid(&flow->id))
1602 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1603 flow->id.ufid);
1604
1605 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1606 OVS_FLOW_ATTR_KEY, false, skb);
1607}
1608
1609/* Called with ovs_mutex or RCU read lock. */
1610int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1611{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08001612 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08001613 OVS_FLOW_ATTR_KEY, false, skb);
1614}
1615
1616/* Called with ovs_mutex or RCU read lock. */
1617int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1618{
1619 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1620 OVS_FLOW_ATTR_MASK, true, skb);
1621}
1622
Pravin B Shelare6445712013-10-03 18:16:47 -07001623#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1624
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001625static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001626{
1627 struct sw_flow_actions *sfa;
1628
Jesse Gross426cda52014-10-06 05:08:38 -07001629 if (size > MAX_ACTIONS_BUFSIZE) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001630 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001631 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001632 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001633
1634 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1635 if (!sfa)
1636 return ERR_PTR(-ENOMEM);
1637
1638 sfa->actions_len = 0;
1639 return sfa;
1640}
1641
Thomas Graf34ae9322015-07-21 10:44:03 +02001642static void ovs_nla_free_set_action(const struct nlattr *a)
1643{
1644 const struct nlattr *ovs_key = nla_data(a);
1645 struct ovs_tunnel_info *ovs_tun;
1646
1647 switch (nla_type(ovs_key)) {
1648 case OVS_KEY_ATTR_TUNNEL_INFO:
1649 ovs_tun = nla_data(ovs_key);
1650 dst_release((struct dst_entry *)ovs_tun->tun_dst);
1651 break;
1652 }
1653}
1654
Pravin B Shelare6445712013-10-03 18:16:47 -07001655void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1656{
Thomas Graf34ae9322015-07-21 10:44:03 +02001657 const struct nlattr *a;
1658 int rem;
1659
1660 if (!sf_acts)
1661 return;
1662
1663 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1664 switch (nla_type(a)) {
1665 case OVS_ACTION_ATTR_SET:
1666 ovs_nla_free_set_action(a);
1667 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001668 case OVS_ACTION_ATTR_CT:
1669 ovs_ct_free_action(a);
1670 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02001671 }
1672 }
1673
1674 kfree(sf_acts);
1675}
1676
1677static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1678{
1679 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1680}
1681
1682/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1683 * The caller must hold rcu_read_lock for this to be sensible. */
1684void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
1685{
1686 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07001687}
1688
1689static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001690 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001691{
1692
1693 struct sw_flow_actions *acts;
1694 int new_acts_size;
1695 int req_size = NLA_ALIGN(attr_len);
1696 int next_offset = offsetof(struct sw_flow_actions, actions) +
1697 (*sfa)->actions_len;
1698
1699 if (req_size <= (ksize(*sfa) - next_offset))
1700 goto out;
1701
1702 new_acts_size = ksize(*sfa) * 2;
1703
1704 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1705 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1706 return ERR_PTR(-EMSGSIZE);
1707 new_acts_size = MAX_ACTIONS_BUFSIZE;
1708 }
1709
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001710 acts = nla_alloc_flow_actions(new_acts_size, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001711 if (IS_ERR(acts))
1712 return (void *)acts;
1713
1714 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1715 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07001716 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001717 kfree(*sfa);
1718 *sfa = acts;
1719
1720out:
1721 (*sfa)->actions_len += req_size;
1722 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1723}
1724
Jesse Grossf0b128c2014-10-03 15:35:31 -07001725static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001726 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001727{
1728 struct nlattr *a;
1729
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001730 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001731 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001732 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001733
1734 a->nla_type = attrtype;
1735 a->nla_len = nla_attr_size(len);
1736
1737 if (data)
1738 memcpy(nla_data(a), data, len);
1739 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1740
Jesse Grossf0b128c2014-10-03 15:35:31 -07001741 return a;
1742}
1743
Joe Stringer7f8a4362015-08-26 11:31:48 -07001744int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
1745 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07001746{
1747 struct nlattr *a;
1748
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001749 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001750
Fabian Frederickf35423c12014-11-14 19:32:58 +01001751 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07001752}
1753
1754static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001755 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001756{
1757 int used = (*sfa)->actions_len;
1758 int err;
1759
Joe Stringer7f8a4362015-08-26 11:31:48 -07001760 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001761 if (err)
1762 return err;
1763
1764 return used;
1765}
1766
1767static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1768 int st_offset)
1769{
1770 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1771 st_offset);
1772
1773 a->nla_len = sfa->actions_len - st_offset;
1774}
1775
Joe Stringer7f8a4362015-08-26 11:31:48 -07001776static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001777 const struct sw_flow_key *key,
1778 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001779 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001780
Joe Stringer7f8a4362015-08-26 11:31:48 -07001781static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
Pravin B Shelare6445712013-10-03 18:16:47 -07001782 const struct sw_flow_key *key, int depth,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001783 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001784 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001785{
1786 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1787 const struct nlattr *probability, *actions;
1788 const struct nlattr *a;
1789 int rem, start, err, st_acts;
1790
1791 memset(attrs, 0, sizeof(attrs));
1792 nla_for_each_nested(a, attr, rem) {
1793 int type = nla_type(a);
1794 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1795 return -EINVAL;
1796 attrs[type] = a;
1797 }
1798 if (rem)
1799 return -EINVAL;
1800
1801 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1802 if (!probability || nla_len(probability) != sizeof(u32))
1803 return -EINVAL;
1804
1805 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1806 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1807 return -EINVAL;
1808
1809 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001810 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001811 if (start < 0)
1812 return start;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001813 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1814 nla_data(probability), sizeof(u32), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001815 if (err)
1816 return err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001817 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001818 if (st_acts < 0)
1819 return st_acts;
1820
Joe Stringer7f8a4362015-08-26 11:31:48 -07001821 err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001822 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001823 if (err)
1824 return err;
1825
1826 add_nested_action_end(*sfa, st_acts);
1827 add_nested_action_end(*sfa, start);
1828
1829 return 0;
1830}
1831
Pravin B Shelare6445712013-10-03 18:16:47 -07001832void ovs_match_init(struct sw_flow_match *match,
1833 struct sw_flow_key *key,
1834 struct sw_flow_mask *mask)
1835{
1836 memset(match, 0, sizeof(*match));
1837 match->key = key;
1838 match->mask = mask;
1839
1840 memset(key, 0, sizeof(*key));
1841
1842 if (mask) {
1843 memset(&mask->key, 0, sizeof(mask->key));
1844 mask->range.start = mask->range.end = 0;
1845 }
1846}
1847
Thomas Grafd91641d2015-01-15 03:53:57 +01001848static int validate_geneve_opts(struct sw_flow_key *key)
1849{
1850 struct geneve_opt *option;
1851 int opts_len = key->tun_opts_len;
1852 bool crit_opt = false;
1853
1854 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
1855 while (opts_len > 0) {
1856 int len;
1857
1858 if (opts_len < sizeof(*option))
1859 return -EINVAL;
1860
1861 len = sizeof(*option) + option->length * 4;
1862 if (len > opts_len)
1863 return -EINVAL;
1864
1865 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1866
1867 option = (struct geneve_opt *)((u8 *)option + len);
1868 opts_len -= len;
1869 };
1870
1871 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1872
1873 return 0;
1874}
1875
Pravin B Shelare6445712013-10-03 18:16:47 -07001876static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001877 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001878{
1879 struct sw_flow_match match;
1880 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02001881 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02001882 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02001883 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001884 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01001885 int err = 0, start, opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07001886
1887 ovs_match_init(&match, &key, NULL);
Thomas Graf1dd144c2015-01-15 03:53:59 +01001888 opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
1889 if (opts_type < 0)
1890 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07001891
Jesse Grossf5796682014-10-03 15:35:33 -07001892 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01001893 switch (opts_type) {
1894 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1895 err = validate_geneve_opts(&key);
1896 if (err < 0)
1897 return err;
1898 break;
1899 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
1900 break;
1901 }
Jesse Grossf5796682014-10-03 15:35:33 -07001902 };
1903
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001904 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001905 if (start < 0)
1906 return start;
1907
Thomas Graf34ae9322015-07-21 10:44:03 +02001908 tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
1909 if (!tun_dst)
1910 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001911
Thomas Graf34ae9322015-07-21 10:44:03 +02001912 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1913 sizeof(*ovs_tun), log);
1914 if (IS_ERR(a)) {
1915 dst_release((struct dst_entry *)tun_dst);
1916 return PTR_ERR(a);
1917 }
1918
1919 ovs_tun = nla_data(a);
1920 ovs_tun->tun_dst = tun_dst;
1921
1922 tun_info = &tun_dst->u.tun_info;
1923 tun_info->mode = IP_TUNNEL_INFO_TX;
Thomas Graf1d8fff92015-07-21 10:43:54 +02001924 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07001925
Pravin B Shelar4c222792015-08-30 18:09:38 -07001926 /* We need to store the options in the action itself since
1927 * everything else will go away after flow setup. We can append
1928 * it to tun_info and then point there.
1929 */
1930 ip_tunnel_info_opts_set(tun_info,
1931 TUN_METADATA_OPTS(&key, key.tun_opts_len),
1932 key.tun_opts_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07001933 add_nested_action_end(*sfa, start);
1934
1935 return err;
1936}
1937
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001938/* Return false if there are any non-masked bits set.
1939 * Mask follows data immediately, before any netlink padding.
1940 */
1941static bool validate_masked(u8 *data, int len)
1942{
1943 u8 *mask = data + len;
1944
1945 while (len--)
1946 if (*data++ & ~*mask++)
1947 return false;
1948
1949 return true;
1950}
1951
Pravin B Shelare6445712013-10-03 18:16:47 -07001952static int validate_set(const struct nlattr *a,
1953 const struct sw_flow_key *flow_key,
1954 struct sw_flow_actions **sfa,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001955 bool *skip_copy, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001956{
1957 const struct nlattr *ovs_key = nla_data(a);
1958 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001959 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001960
1961 /* There can be only one key in a action */
1962 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1963 return -EINVAL;
1964
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001965 key_len = nla_len(ovs_key);
1966 if (masked)
1967 key_len /= 2;
1968
Pravin B Shelare6445712013-10-03 18:16:47 -07001969 if (key_type > OVS_KEY_ATTR_MAX ||
Jesse Gross982b5272015-09-11 18:38:28 -07001970 !check_attr_len(key_len, ovs_key_lens[key_type].len))
Pravin B Shelare6445712013-10-03 18:16:47 -07001971 return -EINVAL;
1972
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001973 if (masked && !validate_masked(nla_data(ovs_key), key_len))
1974 return -EINVAL;
1975
Pravin B Shelare6445712013-10-03 18:16:47 -07001976 switch (key_type) {
1977 const struct ovs_key_ipv4 *ipv4_key;
1978 const struct ovs_key_ipv6 *ipv6_key;
1979 int err;
1980
1981 case OVS_KEY_ATTR_PRIORITY:
1982 case OVS_KEY_ATTR_SKB_MARK:
Joe Stringer182e3042015-08-26 11:31:49 -07001983 case OVS_KEY_ATTR_CT_MARK:
Joe Stringer33db4122015-10-01 15:00:37 -07001984 case OVS_KEY_ATTR_CT_LABELS:
Pravin B Shelare6445712013-10-03 18:16:47 -07001985 case OVS_KEY_ATTR_ETHERNET:
1986 break;
1987
1988 case OVS_KEY_ATTR_TUNNEL:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001989 if (eth_p_mpls(eth_type))
1990 return -EINVAL;
1991
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001992 if (masked)
1993 return -EINVAL; /* Masked tunnel set not supported. */
1994
1995 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001996 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001997 if (err)
1998 return err;
1999 break;
2000
2001 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002002 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07002003 return -EINVAL;
2004
Pravin B Shelare6445712013-10-03 18:16:47 -07002005 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002006
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002007 if (masked) {
2008 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002009
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002010 /* Non-writeable fields. */
2011 if (mask->ipv4_proto || mask->ipv4_frag)
2012 return -EINVAL;
2013 } else {
2014 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2015 return -EINVAL;
2016
2017 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2018 return -EINVAL;
2019 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002020 break;
2021
2022 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002023 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07002024 return -EINVAL;
2025
Pravin B Shelare6445712013-10-03 18:16:47 -07002026 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002027
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002028 if (masked) {
2029 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002030
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002031 /* Non-writeable fields. */
2032 if (mask->ipv6_proto || mask->ipv6_frag)
2033 return -EINVAL;
2034
2035 /* Invalid bits in the flow label mask? */
2036 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2037 return -EINVAL;
2038 } else {
2039 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2040 return -EINVAL;
2041
2042 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2043 return -EINVAL;
2044 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002045 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2046 return -EINVAL;
2047
2048 break;
2049
2050 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002051 if ((eth_type != htons(ETH_P_IP) &&
2052 eth_type != htons(ETH_P_IPV6)) ||
2053 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002054 return -EINVAL;
2055
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002056 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002057
2058 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002059 if ((eth_type != htons(ETH_P_IP) &&
2060 eth_type != htons(ETH_P_IPV6)) ||
2061 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002062 return -EINVAL;
2063
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002064 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002065
2066 case OVS_KEY_ATTR_MPLS:
2067 if (!eth_p_mpls(eth_type))
2068 return -EINVAL;
2069 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002070
2071 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002072 if ((eth_type != htons(ETH_P_IP) &&
2073 eth_type != htons(ETH_P_IPV6)) ||
2074 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002075 return -EINVAL;
2076
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002077 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002078
2079 default:
2080 return -EINVAL;
2081 }
2082
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002083 /* Convert non-masked non-tunnel set actions to masked set actions. */
2084 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2085 int start, len = key_len * 2;
2086 struct nlattr *at;
2087
2088 *skip_copy = true;
2089
2090 start = add_nested_action_start(sfa,
2091 OVS_ACTION_ATTR_SET_TO_MASKED,
2092 log);
2093 if (start < 0)
2094 return start;
2095
2096 at = __add_action(sfa, key_type, NULL, len, log);
2097 if (IS_ERR(at))
2098 return PTR_ERR(at);
2099
2100 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2101 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2102 /* Clear non-writeable bits from otherwise writeable fields. */
2103 if (key_type == OVS_KEY_ATTR_IPV6) {
2104 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2105
2106 mask->ipv6_label &= htonl(0x000FFFFF);
2107 }
2108 add_nested_action_end(*sfa, start);
2109 }
2110
Pravin B Shelare6445712013-10-03 18:16:47 -07002111 return 0;
2112}
2113
2114static int validate_userspace(const struct nlattr *attr)
2115{
2116 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2117 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2118 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002119 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002120 };
2121 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2122 int error;
2123
2124 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
2125 attr, userspace_policy);
2126 if (error)
2127 return error;
2128
2129 if (!a[OVS_USERSPACE_ATTR_PID] ||
2130 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2131 return -EINVAL;
2132
2133 return 0;
2134}
2135
2136static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002137 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002138{
2139 int totlen = NLA_ALIGN(from->nla_len);
2140 struct nlattr *to;
2141
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002142 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002143 if (IS_ERR(to))
2144 return PTR_ERR(to);
2145
2146 memcpy(to, from, totlen);
2147 return 0;
2148}
2149
Joe Stringer7f8a4362015-08-26 11:31:48 -07002150static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002151 const struct sw_flow_key *key,
2152 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002153 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002154{
2155 const struct nlattr *a;
2156 int rem, err;
2157
2158 if (depth >= SAMPLE_ACTION_DEPTH)
2159 return -EOVERFLOW;
2160
2161 nla_for_each_nested(a, attr, rem) {
2162 /* Expected argument lengths, (u32)-1 for variable length. */
2163 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2164 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002165 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002166 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002167 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2168 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002169 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2170 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2171 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002172 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002173 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002174 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2175 [OVS_ACTION_ATTR_CT] = (u32)-1,
Pravin B Shelare6445712013-10-03 18:16:47 -07002176 };
2177 const struct ovs_action_push_vlan *vlan;
2178 int type = nla_type(a);
2179 bool skip_copy;
2180
2181 if (type > OVS_ACTION_ATTR_MAX ||
2182 (action_lens[type] != nla_len(a) &&
2183 action_lens[type] != (u32)-1))
2184 return -EINVAL;
2185
2186 skip_copy = false;
2187 switch (type) {
2188 case OVS_ACTION_ATTR_UNSPEC:
2189 return -EINVAL;
2190
2191 case OVS_ACTION_ATTR_USERSPACE:
2192 err = validate_userspace(a);
2193 if (err)
2194 return err;
2195 break;
2196
2197 case OVS_ACTION_ATTR_OUTPUT:
2198 if (nla_get_u32(a) >= DP_MAX_PORTS)
2199 return -EINVAL;
2200 break;
2201
Andy Zhou971427f32014-09-15 19:37:25 -07002202 case OVS_ACTION_ATTR_HASH: {
2203 const struct ovs_action_hash *act_hash = nla_data(a);
2204
2205 switch (act_hash->hash_alg) {
2206 case OVS_HASH_ALG_L4:
2207 break;
2208 default:
2209 return -EINVAL;
2210 }
2211
2212 break;
2213 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002214
2215 case OVS_ACTION_ATTR_POP_VLAN:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002216 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07002217 break;
2218
2219 case OVS_ACTION_ATTR_PUSH_VLAN:
2220 vlan = nla_data(a);
2221 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
2222 return -EINVAL;
2223 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2224 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002225 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07002226 break;
2227
Andy Zhou971427f32014-09-15 19:37:25 -07002228 case OVS_ACTION_ATTR_RECIRC:
2229 break;
2230
Simon Horman25cd9ba2014-10-06 05:05:13 -07002231 case OVS_ACTION_ATTR_PUSH_MPLS: {
2232 const struct ovs_action_push_mpls *mpls = nla_data(a);
2233
Simon Horman25cd9ba2014-10-06 05:05:13 -07002234 if (!eth_p_mpls(mpls->mpls_ethertype))
2235 return -EINVAL;
2236 /* Prohibit push MPLS other than to a white list
2237 * for packets that have a known tag order.
2238 */
2239 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2240 (eth_type != htons(ETH_P_IP) &&
2241 eth_type != htons(ETH_P_IPV6) &&
2242 eth_type != htons(ETH_P_ARP) &&
2243 eth_type != htons(ETH_P_RARP) &&
2244 !eth_p_mpls(eth_type)))
2245 return -EINVAL;
2246 eth_type = mpls->mpls_ethertype;
2247 break;
2248 }
2249
2250 case OVS_ACTION_ATTR_POP_MPLS:
2251 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2252 !eth_p_mpls(eth_type))
2253 return -EINVAL;
2254
2255 /* Disallow subsequent L2.5+ set and mpls_pop actions
2256 * as there is no check here to ensure that the new
2257 * eth_type is valid and thus set actions could
2258 * write off the end of the packet or otherwise
2259 * corrupt it.
2260 *
2261 * Support for these actions is planned using packet
2262 * recirculation.
2263 */
2264 eth_type = htons(0);
2265 break;
2266
Pravin B Shelare6445712013-10-03 18:16:47 -07002267 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002268 err = validate_set(a, key, sfa,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002269 &skip_copy, eth_type, false, log);
2270 if (err)
2271 return err;
2272 break;
2273
2274 case OVS_ACTION_ATTR_SET_MASKED:
2275 err = validate_set(a, key, sfa,
2276 &skip_copy, eth_type, true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002277 if (err)
2278 return err;
2279 break;
2280
2281 case OVS_ACTION_ATTR_SAMPLE:
Joe Stringer7f8a4362015-08-26 11:31:48 -07002282 err = validate_and_copy_sample(net, a, key, depth, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002283 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002284 if (err)
2285 return err;
2286 skip_copy = true;
2287 break;
2288
Joe Stringer7f8a4362015-08-26 11:31:48 -07002289 case OVS_ACTION_ATTR_CT:
2290 err = ovs_ct_copy_action(net, a, key, sfa, log);
2291 if (err)
2292 return err;
2293 skip_copy = true;
2294 break;
2295
Pravin B Shelare6445712013-10-03 18:16:47 -07002296 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002297 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002298 return -EINVAL;
2299 }
2300 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002301 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002302 if (err)
2303 return err;
2304 }
2305 }
2306
2307 if (rem > 0)
2308 return -EINVAL;
2309
2310 return 0;
2311}
2312
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002313/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07002314int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002315 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002316 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07002317{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002318 int err;
2319
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002320 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002321 if (IS_ERR(*sfa))
2322 return PTR_ERR(*sfa);
2323
Joe Stringer8e2fed12015-08-26 11:31:44 -07002324 (*sfa)->orig_len = nla_len(attr);
Joe Stringer7f8a4362015-08-26 11:31:48 -07002325 err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002326 key->eth.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002327 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02002328 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002329
2330 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002331}
2332
Pravin B Shelare6445712013-10-03 18:16:47 -07002333static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
2334{
2335 const struct nlattr *a;
2336 struct nlattr *start;
2337 int err = 0, rem;
2338
2339 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2340 if (!start)
2341 return -EMSGSIZE;
2342
2343 nla_for_each_nested(a, attr, rem) {
2344 int type = nla_type(a);
2345 struct nlattr *st_sample;
2346
2347 switch (type) {
2348 case OVS_SAMPLE_ATTR_PROBABILITY:
2349 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
2350 sizeof(u32), nla_data(a)))
2351 return -EMSGSIZE;
2352 break;
2353 case OVS_SAMPLE_ATTR_ACTIONS:
2354 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2355 if (!st_sample)
2356 return -EMSGSIZE;
2357 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
2358 if (err)
2359 return err;
2360 nla_nest_end(skb, st_sample);
2361 break;
2362 }
2363 }
2364
2365 nla_nest_end(skb, start);
2366 return err;
2367}
2368
2369static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2370{
2371 const struct nlattr *ovs_key = nla_data(a);
2372 int key_type = nla_type(ovs_key);
2373 struct nlattr *start;
2374 int err;
2375
2376 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07002377 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02002378 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2379 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002380
Pravin B Shelare6445712013-10-03 18:16:47 -07002381 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2382 if (!start)
2383 return -EMSGSIZE;
2384
Pravin B Shelarfc4099f2015-10-22 18:17:16 -07002385 err = ovs_nla_put_tunnel_info(skb, tun_info);
Pravin B Shelare6445712013-10-03 18:16:47 -07002386 if (err)
2387 return err;
2388 nla_nest_end(skb, start);
2389 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002390 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002391 default:
2392 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2393 return -EMSGSIZE;
2394 break;
2395 }
2396
2397 return 0;
2398}
2399
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002400static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2401 struct sk_buff *skb)
2402{
2403 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08002404 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002405 size_t key_len = nla_len(ovs_key) / 2;
2406
2407 /* Revert the conversion we did from a non-masked set action to
2408 * masked set action.
2409 */
Joe Stringerf4f8e732015-03-02 18:49:56 -08002410 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2411 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002412 return -EMSGSIZE;
2413
Joe Stringerf4f8e732015-03-02 18:49:56 -08002414 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2415 return -EMSGSIZE;
2416
2417 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002418 return 0;
2419}
2420
Pravin B Shelare6445712013-10-03 18:16:47 -07002421int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2422{
2423 const struct nlattr *a;
2424 int rem, err;
2425
2426 nla_for_each_attr(a, attr, len, rem) {
2427 int type = nla_type(a);
2428
2429 switch (type) {
2430 case OVS_ACTION_ATTR_SET:
2431 err = set_action_to_attr(a, skb);
2432 if (err)
2433 return err;
2434 break;
2435
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002436 case OVS_ACTION_ATTR_SET_TO_MASKED:
2437 err = masked_set_action_to_set_action_attr(a, skb);
2438 if (err)
2439 return err;
2440 break;
2441
Pravin B Shelare6445712013-10-03 18:16:47 -07002442 case OVS_ACTION_ATTR_SAMPLE:
2443 err = sample_action_to_attr(a, skb);
2444 if (err)
2445 return err;
2446 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002447
2448 case OVS_ACTION_ATTR_CT:
2449 err = ovs_ct_action_to_attr(nla_data(a), skb);
2450 if (err)
2451 return err;
2452 break;
2453
Pravin B Shelare6445712013-10-03 18:16:47 -07002454 default:
2455 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2456 return -EMSGSIZE;
2457 break;
2458 }
2459 }
2460
2461 return 0;
2462}