blob: 6be701f6b31b73e111f9d97b408054496b7d99ea [file] [log] [blame]
Pravin B Shelare6445712013-10-03 18:16:47 -07001/*
Andy Zhou971427f32014-09-15 19:37:25 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Pravin B Shelare6445712013-10-03 18:16:47 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
Joe Perches2235ad12014-02-03 17:18:21 -080019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Pravin B Shelare6445712013-10-03 18:16:47 -070021#include "flow.h"
22#include "datapath.h"
23#include <linux/uaccess.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
28#include <net/llc_pdu.h>
29#include <linux/kernel.h>
30#include <linux/jhash.h>
31#include <linux/jiffies.h>
32#include <linux/llc.h>
33#include <linux/module.h>
34#include <linux/in.h>
35#include <linux/rcupdate.h>
36#include <linux/if_arp.h>
37#include <linux/ip.h>
38#include <linux/ipv6.h>
39#include <linux/sctp.h>
40#include <linux/tcp.h>
41#include <linux/udp.h>
42#include <linux/icmp.h>
43#include <linux/icmpv6.h>
44#include <linux/rculist.h>
Jesse Grossf5796682014-10-03 15:35:33 -070045#include <net/geneve.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070046#include <net/ip.h>
47#include <net/ipv6.h>
48#include <net/ndisc.h>
Simon Horman25cd9ba2014-10-06 05:05:13 -070049#include <net/mpls.h>
Thomas Graf614732e2015-07-21 10:44:06 +020050#include <net/vxlan.h>
Pravin B Shelare6445712013-10-03 18:16:47 -070051
52#include "flow_netlink.h"
53
Thomas Graf81bfe3c32015-01-15 03:53:58 +010054struct ovs_len_tbl {
55 int len;
56 const struct ovs_len_tbl *next;
57};
58
59#define OVS_ATTR_NESTED -1
Jesse Gross982b5272015-09-11 18:38:28 -070060#define OVS_ATTR_VARIABLE -2
Thomas Graf81bfe3c32015-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 Stringer7f8a4362015-08-26 11:31:48 -0700294 + nla_total_size(1) /* OVS_KEY_ATTR_CT_STATE */
295 + 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 Stringerc2ac6672015-08-26 11:31:52 -0700297 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABEL */
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 Graf81bfe3c32015-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 Graf81bfe3c32015-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 Graf81bfe3c32015-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 Stringer7f8a4362015-08-26 11:31:48 -0700352 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u8) },
353 [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 Stringerc2ac6672015-08-26 11:31:52 -0700355 [OVS_KEY_ATTR_CT_LABEL] = { .len = sizeof(struct ovs_key_ct_label) },
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 Graf81bfe3c32015-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 Graf81bfe3c32015-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);
Jiri Benc00a93ba2015-10-05 13:09:46 +0200646 if (is_mask)
647 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
648 else
649 SW_FLOW_KEY_PUT(match, tun_proto, AF_INET, false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700650
651 if (rem > 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800652 OVS_NLERR(log, "IPv4 tunnel attribute has %d unknown bytes.",
653 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700654 return -EINVAL;
655 }
656
657 if (!is_mask) {
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200658 if (!match->key->tun_key.u.ipv4.dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800659 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700660 return -EINVAL;
661 }
662
663 if (!ttl) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800664 OVS_NLERR(log, "IPv4 tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700665 return -EINVAL;
666 }
667 }
668
Thomas Graf1dd144c2015-01-15 03:53:59 +0100669 return opts_type;
670}
671
672static int vxlan_opt_to_nlattr(struct sk_buff *skb,
673 const void *tun_opts, int swkey_tun_opts_len)
674{
Thomas Graf614732e2015-07-21 10:44:06 +0200675 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100676 struct nlattr *nla;
677
678 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
679 if (!nla)
680 return -EMSGSIZE;
681
682 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
683 return -EMSGSIZE;
684
685 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700686 return 0;
687}
688
Jesse Grossf5796682014-10-03 15:35:33 -0700689static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200690 const struct ip_tunnel_key *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100691 const void *tun_opts, int swkey_tun_opts_len)
Pravin B Shelare6445712013-10-03 18:16:47 -0700692{
Pravin B Shelare6445712013-10-03 18:16:47 -0700693 if (output->tun_flags & TUNNEL_KEY &&
694 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
695 return -EMSGSIZE;
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200696 if (output->u.ipv4.src &&
Jiri Benc930345e2015-03-29 16:59:25 +0200697 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200698 output->u.ipv4.src))
Pravin B Shelare6445712013-10-03 18:16:47 -0700699 return -EMSGSIZE;
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200700 if (output->u.ipv4.dst &&
Jiri Benc930345e2015-03-29 16:59:25 +0200701 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200702 output->u.ipv4.dst))
Pravin B Shelare6445712013-10-03 18:16:47 -0700703 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200704 if (output->tos &&
705 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700706 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200707 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700708 return -EMSGSIZE;
709 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700710 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700711 return -EMSGSIZE;
712 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700713 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
714 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800715 if (output->tp_src &&
716 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
717 return -EMSGSIZE;
718 if (output->tp_dst &&
719 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
720 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700721 if ((output->tun_flags & TUNNEL_OAM) &&
722 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700723 return -EMSGSIZE;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100724 if (tun_opts) {
725 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
726 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
727 swkey_tun_opts_len, tun_opts))
728 return -EMSGSIZE;
729 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
730 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
731 return -EMSGSIZE;
732 }
Jesse Grossf5796682014-10-03 15:35:33 -0700733
734 return 0;
735}
736
Jesse Grossf5796682014-10-03 15:35:33 -0700737static int ipv4_tun_to_nlattr(struct sk_buff *skb,
Thomas Graf1d8fff92015-07-21 10:43:54 +0200738 const struct ip_tunnel_key *output,
Thomas Grafd91641d2015-01-15 03:53:57 +0100739 const void *tun_opts, int swkey_tun_opts_len)
Jesse Grossf5796682014-10-03 15:35:33 -0700740{
741 struct nlattr *nla;
742 int err;
743
744 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
745 if (!nla)
746 return -EMSGSIZE;
747
748 err = __ipv4_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len);
749 if (err)
750 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700751
752 nla_nest_end(skb, nla);
753 return 0;
754}
755
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800756int ovs_nla_put_egress_tunnel_key(struct sk_buff *skb,
Pravin B Shelar4c222792015-08-30 18:09:38 -0700757 const struct ip_tunnel_info *egress_tun_info,
758 const void *egress_tun_opts)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800759{
Thomas Graf1d8fff92015-07-21 10:43:54 +0200760 return __ipv4_tun_to_nlattr(skb, &egress_tun_info->key,
Pravin B Shelar4c222792015-08-30 18:09:38 -0700761 egress_tun_opts,
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800762 egress_tun_info->options_len);
763}
764
Joe Stringerc2ac6672015-08-26 11:31:52 -0700765static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
766 u64 *attrs, const struct nlattr **a,
767 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700768{
Andy Zhou971427f32014-09-15 19:37:25 -0700769 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
770 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
771
772 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
773 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
774 }
775
776 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
777 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
778
779 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
780 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
781 }
782
Pravin B Shelare6445712013-10-03 18:16:47 -0700783 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
784 SW_FLOW_KEY_PUT(match, phy.priority,
785 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
786 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
787 }
788
789 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
790 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
791
Jesse Gross426cda52014-10-06 05:08:38 -0700792 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700793 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -0700794 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800795 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -0700796 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -0700797 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700798 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700799
800 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
801 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
802 } else if (!is_mask) {
803 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
804 }
805
806 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
807 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
808
809 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
810 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
811 }
812 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
813 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100814 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700815 return -EINVAL;
816 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
817 }
Joe Stringer7f8a4362015-08-26 11:31:48 -0700818
819 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -0700820 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700821 u8 ct_state = nla_get_u8(a[OVS_KEY_ATTR_CT_STATE]);
822
823 SW_FLOW_KEY_PUT(match, ct.state, ct_state, is_mask);
824 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
825 }
826 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -0700827 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -0700828 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
829
830 SW_FLOW_KEY_PUT(match, ct.zone, ct_zone, is_mask);
831 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
832 }
Joe Stringer182e3042015-08-26 11:31:49 -0700833 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -0700834 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
Joe Stringer182e3042015-08-26 11:31:49 -0700835 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
836
837 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
838 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
839 }
Joe Stringerc2ac6672015-08-26 11:31:52 -0700840 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABEL) &&
841 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABEL)) {
842 const struct ovs_key_ct_label *cl;
843
844 cl = nla_data(a[OVS_KEY_ATTR_CT_LABEL]);
845 SW_FLOW_KEY_MEMCPY(match, ct.label, cl->ct_label,
846 sizeof(*cl), is_mask);
847 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABEL);
848 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700849 return 0;
850}
851
Joe Stringerc2ac6672015-08-26 11:31:52 -0700852static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
853 u64 attrs, const struct nlattr **a,
854 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700855{
856 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700857
Joe Stringerc2ac6672015-08-26 11:31:52 -0700858 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -0700859 if (err)
860 return err;
861
862 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
863 const struct ovs_key_ethernet *eth_key;
864
865 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
866 SW_FLOW_KEY_MEMCPY(match, eth.src,
867 eth_key->eth_src, ETH_ALEN, is_mask);
868 SW_FLOW_KEY_MEMCPY(match, eth.dst,
869 eth_key->eth_dst, ETH_ALEN, is_mask);
870 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
871 }
872
873 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
874 __be16 tci;
875
876 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
877 if (!(tci & htons(VLAN_TAG_PRESENT))) {
878 if (is_mask)
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800879 OVS_NLERR(log, "VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700880 else
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800881 OVS_NLERR(log, "VLAN TCI does not have VLAN_TAG_PRESENT bit set.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700882
883 return -EINVAL;
884 }
885
886 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
887 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
Pravin B Shelara85311b2014-10-19 12:03:40 -0700888 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700889
890 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
891 __be16 eth_type;
892
893 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
894 if (is_mask) {
895 /* Always exact match EtherType. */
896 eth_type = htons(0xffff);
Alexander Duyck6713fc92015-05-04 14:34:05 -0700897 } else if (!eth_proto_is_802_3(eth_type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800898 OVS_NLERR(log, "EtherType %x is less than min %x",
899 ntohs(eth_type), ETH_P_802_3_MIN);
Pravin B Shelare6445712013-10-03 18:16:47 -0700900 return -EINVAL;
901 }
902
903 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
904 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
905 } else if (!is_mask) {
906 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
907 }
908
909 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
910 const struct ovs_key_ipv4 *ipv4_key;
911
912 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
913 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800914 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
915 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700916 return -EINVAL;
917 }
918 SW_FLOW_KEY_PUT(match, ip.proto,
919 ipv4_key->ipv4_proto, is_mask);
920 SW_FLOW_KEY_PUT(match, ip.tos,
921 ipv4_key->ipv4_tos, is_mask);
922 SW_FLOW_KEY_PUT(match, ip.ttl,
923 ipv4_key->ipv4_ttl, is_mask);
924 SW_FLOW_KEY_PUT(match, ip.frag,
925 ipv4_key->ipv4_frag, is_mask);
926 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
927 ipv4_key->ipv4_src, is_mask);
928 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
929 ipv4_key->ipv4_dst, is_mask);
930 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
931 }
932
933 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
934 const struct ovs_key_ipv6 *ipv6_key;
935
936 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
937 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800938 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
939 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700940 return -EINVAL;
941 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800942
Joe Stringerd3052bb2014-11-19 13:54:49 -0800943 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
David S. Miller14591432014-11-21 22:28:24 -0500944 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -0800945 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
946 return -EINVAL;
947 }
948
Pravin B Shelare6445712013-10-03 18:16:47 -0700949 SW_FLOW_KEY_PUT(match, ipv6.label,
950 ipv6_key->ipv6_label, is_mask);
951 SW_FLOW_KEY_PUT(match, ip.proto,
952 ipv6_key->ipv6_proto, is_mask);
953 SW_FLOW_KEY_PUT(match, ip.tos,
954 ipv6_key->ipv6_tclass, is_mask);
955 SW_FLOW_KEY_PUT(match, ip.ttl,
956 ipv6_key->ipv6_hlimit, is_mask);
957 SW_FLOW_KEY_PUT(match, ip.frag,
958 ipv6_key->ipv6_frag, is_mask);
959 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
960 ipv6_key->ipv6_src,
961 sizeof(match->key->ipv6.addr.src),
962 is_mask);
963 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
964 ipv6_key->ipv6_dst,
965 sizeof(match->key->ipv6.addr.dst),
966 is_mask);
967
968 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
969 }
970
971 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
972 const struct ovs_key_arp *arp_key;
973
974 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
975 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800976 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -0700977 arp_key->arp_op);
978 return -EINVAL;
979 }
980
981 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
982 arp_key->arp_sip, is_mask);
983 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
984 arp_key->arp_tip, is_mask);
985 SW_FLOW_KEY_PUT(match, ip.proto,
986 ntohs(arp_key->arp_op), is_mask);
987 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
988 arp_key->arp_sha, ETH_ALEN, is_mask);
989 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
990 arp_key->arp_tha, ETH_ALEN, is_mask);
991
992 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
993 }
994
Simon Horman25cd9ba2014-10-06 05:05:13 -0700995 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
996 const struct ovs_key_mpls *mpls_key;
997
998 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
999 SW_FLOW_KEY_PUT(match, mpls.top_lse,
1000 mpls_key->mpls_lse, is_mask);
1001
1002 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1003 }
1004
Pravin B Shelare6445712013-10-03 18:16:47 -07001005 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1006 const struct ovs_key_tcp *tcp_key;
1007
1008 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001009 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1010 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001011 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1012 }
1013
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001014 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -07001015 SW_FLOW_KEY_PUT(match, tp.flags,
1016 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1017 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001018 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1019 }
1020
Pravin B Shelare6445712013-10-03 18:16:47 -07001021 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1022 const struct ovs_key_udp *udp_key;
1023
1024 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001025 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1026 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001027 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1028 }
1029
1030 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1031 const struct ovs_key_sctp *sctp_key;
1032
1033 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001034 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1035 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001036 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1037 }
1038
1039 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1040 const struct ovs_key_icmp *icmp_key;
1041
1042 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001043 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001044 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001045 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001046 htons(icmp_key->icmp_code), is_mask);
1047 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1048 }
1049
1050 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1051 const struct ovs_key_icmpv6 *icmpv6_key;
1052
1053 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001054 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001055 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001056 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001057 htons(icmpv6_key->icmpv6_code), is_mask);
1058 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1059 }
1060
1061 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1062 const struct ovs_key_nd *nd_key;
1063
1064 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1065 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1066 nd_key->nd_target,
1067 sizeof(match->key->ipv6.nd.target),
1068 is_mask);
1069 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1070 nd_key->nd_sll, ETH_ALEN, is_mask);
1071 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1072 nd_key->nd_tll, ETH_ALEN, is_mask);
1073 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1074 }
1075
Jesse Gross426cda52014-10-06 05:08:38 -07001076 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001077 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001078 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001079 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001080 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001081
1082 return 0;
1083}
1084
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001085static void nlattr_set(struct nlattr *attr, u8 val,
1086 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001087{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001088 struct nlattr *nla;
1089 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001090
Pravin B Shelarf47de062014-10-16 21:55:45 -07001091 /* The nlattr stream should already have been validated */
1092 nla_for_each_nested(nla, attr, rem) {
Jesse Gross982b5272015-09-11 18:38:28 -07001093 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1094 if (tbl[nla_type(nla)].next)
1095 tbl = tbl[nla_type(nla)].next;
1096 nlattr_set(nla, val, tbl);
1097 } else {
Pravin B Shelarf47de062014-10-16 21:55:45 -07001098 memset(nla_data(nla), val, nla_len(nla));
Jesse Gross982b5272015-09-11 18:38:28 -07001099 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001100 }
1101}
1102
1103static void mask_set_nlattr(struct nlattr *attr, u8 val)
1104{
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001105 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001106}
1107
1108/**
1109 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1110 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1111 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1112 * does not include any don't care bit.
Joe Stringerc2ac6672015-08-26 11:31:52 -07001113 * @net: Used to determine per-namespace field support.
Pravin B Shelare6445712013-10-03 18:16:47 -07001114 * @match: receives the extracted flow match information.
1115 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1116 * sequence. The fields should of the packet that triggered the creation
1117 * of this flow.
1118 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1119 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001120 * @log: Boolean to allow kernel error logging. Normally true, but when
1121 * probing for feature compatibility this should be passed in as false to
1122 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001123 */
Joe Stringerc2ac6672015-08-26 11:31:52 -07001124int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001125 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001126 const struct nlattr *nla_mask,
1127 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001128{
1129 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1130 const struct nlattr *encap;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001131 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001132 u64 key_attrs = 0;
1133 u64 mask_attrs = 0;
1134 bool encap_valid = false;
1135 int err;
1136
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001137 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001138 if (err)
1139 return err;
1140
1141 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
1142 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
1143 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1144 __be16 tci;
1145
1146 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
1147 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001148 OVS_NLERR(log, "Invalid Vlan frame.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001149 return -EINVAL;
1150 }
1151
1152 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1153 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1154 encap = a[OVS_KEY_ATTR_ENCAP];
1155 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1156 encap_valid = true;
1157
1158 if (tci & htons(VLAN_TAG_PRESENT)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001159 err = parse_flow_nlattrs(encap, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001160 if (err)
1161 return err;
1162 } else if (!tci) {
1163 /* Corner case for truncated 802.1Q header. */
1164 if (nla_len(encap)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001165 OVS_NLERR(log, "Truncated 802.1Q header has non-zero encap attribute.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001166 return -EINVAL;
1167 }
1168 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001169 OVS_NLERR(log, "Encap attr is set for non-VLAN frame");
Pravin B Shelare6445712013-10-03 18:16:47 -07001170 return -EINVAL;
1171 }
1172 }
1173
Joe Stringerc2ac6672015-08-26 11:31:52 -07001174 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001175 if (err)
1176 return err;
1177
Pravin B Shelara85311b2014-10-19 12:03:40 -07001178 if (match->mask) {
1179 if (!nla_mask) {
1180 /* Create an exact match mask. We need to set to 0xff
1181 * all the 'match->mask' fields that have been touched
1182 * in 'match->key'. We cannot simply memset
1183 * 'match->mask', because padding bytes and fields not
1184 * specified in 'match->key' should be left to 0.
1185 * Instead, we use a stream of netlink attributes,
1186 * copied from 'key' and set to 0xff.
1187 * ovs_key_from_nlattrs() will take care of filling
1188 * 'match->mask' appropriately.
1189 */
1190 newmask = kmemdup(nla_key,
1191 nla_total_size(nla_len(nla_key)),
1192 GFP_KERNEL);
1193 if (!newmask)
1194 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001195
Pravin B Shelara85311b2014-10-19 12:03:40 -07001196 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001197
Pravin B Shelara85311b2014-10-19 12:03:40 -07001198 /* The userspace does not send tunnel attributes that
1199 * are 0, but we should not wildcard them nonetheless.
1200 */
Jiri Benc00a93ba2015-10-05 13:09:46 +02001201 if (match->key->tun_proto)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001202 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1203 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001204
Pravin B Shelara85311b2014-10-19 12:03:40 -07001205 nla_mask = newmask;
1206 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001207
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001208 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001209 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001210 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001211
Pravin B Shelara85311b2014-10-19 12:03:40 -07001212 /* Always match on tci. */
1213 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1214
Pravin B Shelarf47de062014-10-16 21:55:45 -07001215 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001216 __be16 eth_type = 0;
1217 __be16 tci = 0;
1218
1219 if (!encap_valid) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001220 OVS_NLERR(log, "Encap mask attribute is set for non-VLAN frame.");
Pravin B Shelarf47de062014-10-16 21:55:45 -07001221 err = -EINVAL;
1222 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001223 }
1224
1225 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1226 if (a[OVS_KEY_ATTR_ETHERTYPE])
1227 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1228
1229 if (eth_type == htons(0xffff)) {
1230 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1231 encap = a[OVS_KEY_ATTR_ENCAP];
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001232 err = parse_flow_mask_nlattrs(encap, a,
1233 &mask_attrs, log);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001234 if (err)
1235 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001236 } else {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001237 OVS_NLERR(log, "VLAN frames must have an exact match on the TPID (mask=%x).",
1238 ntohs(eth_type));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001239 err = -EINVAL;
1240 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001241 }
1242
1243 if (a[OVS_KEY_ATTR_VLAN])
1244 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1245
1246 if (!(tci & htons(VLAN_TAG_PRESENT))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001247 OVS_NLERR(log, "VLAN tag present bit must have an exact match (tci_mask=%x).",
1248 ntohs(tci));
Pravin B Shelarf47de062014-10-16 21:55:45 -07001249 err = -EINVAL;
1250 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001251 }
1252 }
1253
Joe Stringerc2ac6672015-08-26 11:31:52 -07001254 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1255 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001256 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001257 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001258 }
1259
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001260 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001261 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001262
Pravin B Shelarf47de062014-10-16 21:55:45 -07001263free_newmask:
1264 kfree(newmask);
1265 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001266}
1267
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001268static size_t get_ufid_len(const struct nlattr *attr, bool log)
1269{
1270 size_t len;
1271
1272 if (!attr)
1273 return 0;
1274
1275 len = nla_len(attr);
1276 if (len < 1 || len > MAX_UFID_LENGTH) {
1277 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1278 nla_len(attr), MAX_UFID_LENGTH);
1279 return 0;
1280 }
1281
1282 return len;
1283}
1284
1285/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1286 * or false otherwise.
1287 */
1288bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1289 bool log)
1290{
1291 sfid->ufid_len = get_ufid_len(attr, log);
1292 if (sfid->ufid_len)
1293 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1294
1295 return sfid->ufid_len;
1296}
1297
1298int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1299 const struct sw_flow_key *key, bool log)
1300{
1301 struct sw_flow_key *new_key;
1302
1303 if (ovs_nla_get_ufid(sfid, ufid, log))
1304 return 0;
1305
1306 /* If UFID was not provided, use unmasked key. */
1307 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1308 if (!new_key)
1309 return -ENOMEM;
1310 memcpy(new_key, key, sizeof(*key));
1311 sfid->unmasked_key = new_key;
1312
1313 return 0;
1314}
1315
1316u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1317{
1318 return attr ? nla_get_u32(attr) : 0;
1319}
1320
Pravin B Shelare6445712013-10-03 18:16:47 -07001321/**
1322 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001323 * @key: Receives extracted in_port, priority, tun_key and skb_mark.
Pravin B Shelare6445712013-10-03 18:16:47 -07001324 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1325 * sequence.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001326 * @log: Boolean to allow kernel error logging. Normally true, but when
1327 * probing for feature compatibility this should be passed in as false to
1328 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001329 *
1330 * This parses a series of Netlink attributes that form a flow key, which must
1331 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1332 * get the metadata, that is, the parts of the flow key that cannot be
1333 * extracted from the packet itself.
1334 */
1335
Joe Stringerc2ac6672015-08-26 11:31:52 -07001336int ovs_nla_get_flow_metadata(struct net *net, const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001337 struct sw_flow_key *key,
1338 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001339{
Pravin B Shelare6445712013-10-03 18:16:47 -07001340 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001341 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001342 u64 attrs = 0;
1343 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001344
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001345 err = parse_flow_nlattrs(attr, a, &attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001346 if (err)
1347 return -EINVAL;
1348
1349 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001350 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001351
Joe Stringer7f8a4362015-08-26 11:31:48 -07001352 memset(&key->ct, 0, sizeof(key->ct));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001353 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001354
Joe Stringerc2ac6672015-08-26 11:31:52 -07001355 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001356}
1357
Joe Stringer5b4237b2015-01-21 16:42:48 -08001358static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1359 const struct sw_flow_key *output, bool is_mask,
1360 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001361{
1362 struct ovs_key_ethernet *eth_key;
1363 struct nlattr *nla, *encap;
Pravin B Shelare6445712013-10-03 18:16:47 -07001364
Andy Zhou971427f32014-09-15 19:37:25 -07001365 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1366 goto nla_put_failure;
1367
1368 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1369 goto nla_put_failure;
1370
Pravin B Shelare6445712013-10-03 18:16:47 -07001371 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1372 goto nla_put_failure;
1373
Jiri Benc00a93ba2015-10-05 13:09:46 +02001374 if ((swkey->tun_proto || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001375 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001376
1377 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001378 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001379
1380 if (ipv4_tun_to_nlattr(skb, &output->tun_key, opts,
1381 swkey->tun_opts_len))
1382 goto nla_put_failure;
1383 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001384
1385 if (swkey->phy.in_port == DP_MAX_PORTS) {
1386 if (is_mask && (output->phy.in_port == 0xffff))
1387 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1388 goto nla_put_failure;
1389 } else {
1390 u16 upper_u16;
1391 upper_u16 = !is_mask ? 0 : 0xffff;
1392
1393 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1394 (upper_u16 << 16) | output->phy.in_port))
1395 goto nla_put_failure;
1396 }
1397
1398 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1399 goto nla_put_failure;
1400
Joe Stringer7f8a4362015-08-26 11:31:48 -07001401 if (ovs_ct_put_key(output, skb))
1402 goto nla_put_failure;
1403
Pravin B Shelare6445712013-10-03 18:16:47 -07001404 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1405 if (!nla)
1406 goto nla_put_failure;
1407
1408 eth_key = nla_data(nla);
Joe Perches8c63ff02014-02-18 11:15:45 -08001409 ether_addr_copy(eth_key->eth_src, output->eth.src);
1410 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001411
1412 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1413 __be16 eth_type;
1414 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1415 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1416 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1417 goto nla_put_failure;
1418 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1419 if (!swkey->eth.tci)
1420 goto unencap;
1421 } else
1422 encap = NULL;
1423
1424 if (swkey->eth.type == htons(ETH_P_802_2)) {
1425 /*
1426 * Ethertype 802.2 is represented in the netlink with omitted
1427 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1428 * 0xffff in the mask attribute. Ethertype can also
1429 * be wildcarded.
1430 */
1431 if (is_mask && output->eth.type)
1432 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1433 output->eth.type))
1434 goto nla_put_failure;
1435 goto unencap;
1436 }
1437
1438 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1439 goto nla_put_failure;
1440
1441 if (swkey->eth.type == htons(ETH_P_IP)) {
1442 struct ovs_key_ipv4 *ipv4_key;
1443
1444 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1445 if (!nla)
1446 goto nla_put_failure;
1447 ipv4_key = nla_data(nla);
1448 ipv4_key->ipv4_src = output->ipv4.addr.src;
1449 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1450 ipv4_key->ipv4_proto = output->ip.proto;
1451 ipv4_key->ipv4_tos = output->ip.tos;
1452 ipv4_key->ipv4_ttl = output->ip.ttl;
1453 ipv4_key->ipv4_frag = output->ip.frag;
1454 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1455 struct ovs_key_ipv6 *ipv6_key;
1456
1457 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1458 if (!nla)
1459 goto nla_put_failure;
1460 ipv6_key = nla_data(nla);
1461 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1462 sizeof(ipv6_key->ipv6_src));
1463 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1464 sizeof(ipv6_key->ipv6_dst));
1465 ipv6_key->ipv6_label = output->ipv6.label;
1466 ipv6_key->ipv6_proto = output->ip.proto;
1467 ipv6_key->ipv6_tclass = output->ip.tos;
1468 ipv6_key->ipv6_hlimit = output->ip.ttl;
1469 ipv6_key->ipv6_frag = output->ip.frag;
1470 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1471 swkey->eth.type == htons(ETH_P_RARP)) {
1472 struct ovs_key_arp *arp_key;
1473
1474 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1475 if (!nla)
1476 goto nla_put_failure;
1477 arp_key = nla_data(nla);
1478 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1479 arp_key->arp_sip = output->ipv4.addr.src;
1480 arp_key->arp_tip = output->ipv4.addr.dst;
1481 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001482 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1483 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001484 } else if (eth_p_mpls(swkey->eth.type)) {
1485 struct ovs_key_mpls *mpls_key;
1486
1487 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1488 if (!nla)
1489 goto nla_put_failure;
1490 mpls_key = nla_data(nla);
1491 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001492 }
1493
1494 if ((swkey->eth.type == htons(ETH_P_IP) ||
1495 swkey->eth.type == htons(ETH_P_IPV6)) &&
1496 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1497
1498 if (swkey->ip.proto == IPPROTO_TCP) {
1499 struct ovs_key_tcp *tcp_key;
1500
1501 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1502 if (!nla)
1503 goto nla_put_failure;
1504 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001505 tcp_key->tcp_src = output->tp.src;
1506 tcp_key->tcp_dst = output->tp.dst;
1507 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1508 output->tp.flags))
1509 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001510 } else if (swkey->ip.proto == IPPROTO_UDP) {
1511 struct ovs_key_udp *udp_key;
1512
1513 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1514 if (!nla)
1515 goto nla_put_failure;
1516 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001517 udp_key->udp_src = output->tp.src;
1518 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001519 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1520 struct ovs_key_sctp *sctp_key;
1521
1522 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1523 if (!nla)
1524 goto nla_put_failure;
1525 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001526 sctp_key->sctp_src = output->tp.src;
1527 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001528 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1529 swkey->ip.proto == IPPROTO_ICMP) {
1530 struct ovs_key_icmp *icmp_key;
1531
1532 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1533 if (!nla)
1534 goto nla_put_failure;
1535 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001536 icmp_key->icmp_type = ntohs(output->tp.src);
1537 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001538 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1539 swkey->ip.proto == IPPROTO_ICMPV6) {
1540 struct ovs_key_icmpv6 *icmpv6_key;
1541
1542 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1543 sizeof(*icmpv6_key));
1544 if (!nla)
1545 goto nla_put_failure;
1546 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001547 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1548 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001549
1550 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1551 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1552 struct ovs_key_nd *nd_key;
1553
1554 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1555 if (!nla)
1556 goto nla_put_failure;
1557 nd_key = nla_data(nla);
1558 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1559 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001560 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1561 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001562 }
1563 }
1564 }
1565
1566unencap:
1567 if (encap)
1568 nla_nest_end(skb, encap);
1569
1570 return 0;
1571
1572nla_put_failure:
1573 return -EMSGSIZE;
1574}
1575
Joe Stringer5b4237b2015-01-21 16:42:48 -08001576int ovs_nla_put_key(const struct sw_flow_key *swkey,
1577 const struct sw_flow_key *output, int attr, bool is_mask,
1578 struct sk_buff *skb)
1579{
1580 int err;
1581 struct nlattr *nla;
1582
1583 nla = nla_nest_start(skb, attr);
1584 if (!nla)
1585 return -EMSGSIZE;
1586 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1587 if (err)
1588 return err;
1589 nla_nest_end(skb, nla);
1590
1591 return 0;
1592}
1593
1594/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001595int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08001596{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001597 if (ovs_identifier_is_ufid(&flow->id))
1598 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1599 flow->id.ufid);
1600
1601 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1602 OVS_FLOW_ATTR_KEY, false, skb);
1603}
1604
1605/* Called with ovs_mutex or RCU read lock. */
1606int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1607{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08001608 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08001609 OVS_FLOW_ATTR_KEY, false, skb);
1610}
1611
1612/* Called with ovs_mutex or RCU read lock. */
1613int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1614{
1615 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1616 OVS_FLOW_ATTR_MASK, true, skb);
1617}
1618
Pravin B Shelare6445712013-10-03 18:16:47 -07001619#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1620
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001621static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001622{
1623 struct sw_flow_actions *sfa;
1624
Jesse Gross426cda52014-10-06 05:08:38 -07001625 if (size > MAX_ACTIONS_BUFSIZE) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001626 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001627 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001628 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001629
1630 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1631 if (!sfa)
1632 return ERR_PTR(-ENOMEM);
1633
1634 sfa->actions_len = 0;
1635 return sfa;
1636}
1637
Thomas Graf34ae9322015-07-21 10:44:03 +02001638static void ovs_nla_free_set_action(const struct nlattr *a)
1639{
1640 const struct nlattr *ovs_key = nla_data(a);
1641 struct ovs_tunnel_info *ovs_tun;
1642
1643 switch (nla_type(ovs_key)) {
1644 case OVS_KEY_ATTR_TUNNEL_INFO:
1645 ovs_tun = nla_data(ovs_key);
1646 dst_release((struct dst_entry *)ovs_tun->tun_dst);
1647 break;
1648 }
1649}
1650
Pravin B Shelare6445712013-10-03 18:16:47 -07001651void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1652{
Thomas Graf34ae9322015-07-21 10:44:03 +02001653 const struct nlattr *a;
1654 int rem;
1655
1656 if (!sf_acts)
1657 return;
1658
1659 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1660 switch (nla_type(a)) {
1661 case OVS_ACTION_ATTR_SET:
1662 ovs_nla_free_set_action(a);
1663 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001664 case OVS_ACTION_ATTR_CT:
1665 ovs_ct_free_action(a);
1666 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02001667 }
1668 }
1669
1670 kfree(sf_acts);
1671}
1672
1673static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1674{
1675 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1676}
1677
1678/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1679 * The caller must hold rcu_read_lock for this to be sensible. */
1680void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
1681{
1682 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07001683}
1684
1685static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001686 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001687{
1688
1689 struct sw_flow_actions *acts;
1690 int new_acts_size;
1691 int req_size = NLA_ALIGN(attr_len);
1692 int next_offset = offsetof(struct sw_flow_actions, actions) +
1693 (*sfa)->actions_len;
1694
1695 if (req_size <= (ksize(*sfa) - next_offset))
1696 goto out;
1697
1698 new_acts_size = ksize(*sfa) * 2;
1699
1700 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1701 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1702 return ERR_PTR(-EMSGSIZE);
1703 new_acts_size = MAX_ACTIONS_BUFSIZE;
1704 }
1705
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001706 acts = nla_alloc_flow_actions(new_acts_size, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001707 if (IS_ERR(acts))
1708 return (void *)acts;
1709
1710 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1711 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07001712 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001713 kfree(*sfa);
1714 *sfa = acts;
1715
1716out:
1717 (*sfa)->actions_len += req_size;
1718 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1719}
1720
Jesse Grossf0b128c2014-10-03 15:35:31 -07001721static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001722 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001723{
1724 struct nlattr *a;
1725
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001726 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001727 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001728 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001729
1730 a->nla_type = attrtype;
1731 a->nla_len = nla_attr_size(len);
1732
1733 if (data)
1734 memcpy(nla_data(a), data, len);
1735 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1736
Jesse Grossf0b128c2014-10-03 15:35:31 -07001737 return a;
1738}
1739
Joe Stringer7f8a4362015-08-26 11:31:48 -07001740int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
1741 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07001742{
1743 struct nlattr *a;
1744
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001745 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001746
Fabian Frederickf35423c12014-11-14 19:32:58 +01001747 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07001748}
1749
1750static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001751 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001752{
1753 int used = (*sfa)->actions_len;
1754 int err;
1755
Joe Stringer7f8a4362015-08-26 11:31:48 -07001756 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001757 if (err)
1758 return err;
1759
1760 return used;
1761}
1762
1763static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1764 int st_offset)
1765{
1766 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1767 st_offset);
1768
1769 a->nla_len = sfa->actions_len - st_offset;
1770}
1771
Joe Stringer7f8a4362015-08-26 11:31:48 -07001772static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001773 const struct sw_flow_key *key,
1774 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001775 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001776
Joe Stringer7f8a4362015-08-26 11:31:48 -07001777static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
Pravin B Shelare6445712013-10-03 18:16:47 -07001778 const struct sw_flow_key *key, int depth,
Simon Horman25cd9ba2014-10-06 05:05:13 -07001779 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001780 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001781{
1782 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1783 const struct nlattr *probability, *actions;
1784 const struct nlattr *a;
1785 int rem, start, err, st_acts;
1786
1787 memset(attrs, 0, sizeof(attrs));
1788 nla_for_each_nested(a, attr, rem) {
1789 int type = nla_type(a);
1790 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1791 return -EINVAL;
1792 attrs[type] = a;
1793 }
1794 if (rem)
1795 return -EINVAL;
1796
1797 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1798 if (!probability || nla_len(probability) != sizeof(u32))
1799 return -EINVAL;
1800
1801 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1802 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1803 return -EINVAL;
1804
1805 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001806 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001807 if (start < 0)
1808 return start;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001809 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1810 nla_data(probability), sizeof(u32), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001811 if (err)
1812 return err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001813 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001814 if (st_acts < 0)
1815 return st_acts;
1816
Joe Stringer7f8a4362015-08-26 11:31:48 -07001817 err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001818 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001819 if (err)
1820 return err;
1821
1822 add_nested_action_end(*sfa, st_acts);
1823 add_nested_action_end(*sfa, start);
1824
1825 return 0;
1826}
1827
Pravin B Shelare6445712013-10-03 18:16:47 -07001828void ovs_match_init(struct sw_flow_match *match,
1829 struct sw_flow_key *key,
1830 struct sw_flow_mask *mask)
1831{
1832 memset(match, 0, sizeof(*match));
1833 match->key = key;
1834 match->mask = mask;
1835
1836 memset(key, 0, sizeof(*key));
1837
1838 if (mask) {
1839 memset(&mask->key, 0, sizeof(mask->key));
1840 mask->range.start = mask->range.end = 0;
1841 }
1842}
1843
Thomas Grafd91641d2015-01-15 03:53:57 +01001844static int validate_geneve_opts(struct sw_flow_key *key)
1845{
1846 struct geneve_opt *option;
1847 int opts_len = key->tun_opts_len;
1848 bool crit_opt = false;
1849
1850 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
1851 while (opts_len > 0) {
1852 int len;
1853
1854 if (opts_len < sizeof(*option))
1855 return -EINVAL;
1856
1857 len = sizeof(*option) + option->length * 4;
1858 if (len > opts_len)
1859 return -EINVAL;
1860
1861 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
1862
1863 option = (struct geneve_opt *)((u8 *)option + len);
1864 opts_len -= len;
1865 };
1866
1867 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
1868
1869 return 0;
1870}
1871
Pravin B Shelare6445712013-10-03 18:16:47 -07001872static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001873 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001874{
1875 struct sw_flow_match match;
1876 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02001877 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02001878 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02001879 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001880 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01001881 int err = 0, start, opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07001882
1883 ovs_match_init(&match, &key, NULL);
Thomas Graf1dd144c2015-01-15 03:53:59 +01001884 opts_type = ipv4_tun_from_nlattr(nla_data(attr), &match, false, log);
1885 if (opts_type < 0)
1886 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07001887
Jesse Grossf5796682014-10-03 15:35:33 -07001888 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01001889 switch (opts_type) {
1890 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
1891 err = validate_geneve_opts(&key);
1892 if (err < 0)
1893 return err;
1894 break;
1895 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
1896 break;
1897 }
Jesse Grossf5796682014-10-03 15:35:33 -07001898 };
1899
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001900 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001901 if (start < 0)
1902 return start;
1903
Thomas Graf34ae9322015-07-21 10:44:03 +02001904 tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
1905 if (!tun_dst)
1906 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07001907
Thomas Graf34ae9322015-07-21 10:44:03 +02001908 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
1909 sizeof(*ovs_tun), log);
1910 if (IS_ERR(a)) {
1911 dst_release((struct dst_entry *)tun_dst);
1912 return PTR_ERR(a);
1913 }
1914
1915 ovs_tun = nla_data(a);
1916 ovs_tun->tun_dst = tun_dst;
1917
1918 tun_info = &tun_dst->u.tun_info;
1919 tun_info->mode = IP_TUNNEL_INFO_TX;
Jiri Benc00a93ba2015-10-05 13:09:46 +02001920 if (key.tun_proto == AF_INET6)
1921 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
Thomas Graf1d8fff92015-07-21 10:43:54 +02001922 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07001923
Pravin B Shelar4c222792015-08-30 18:09:38 -07001924 /* We need to store the options in the action itself since
1925 * everything else will go away after flow setup. We can append
1926 * it to tun_info and then point there.
1927 */
1928 ip_tunnel_info_opts_set(tun_info,
1929 TUN_METADATA_OPTS(&key, key.tun_opts_len),
1930 key.tun_opts_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07001931 add_nested_action_end(*sfa, start);
1932
1933 return err;
1934}
1935
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001936/* Return false if there are any non-masked bits set.
1937 * Mask follows data immediately, before any netlink padding.
1938 */
1939static bool validate_masked(u8 *data, int len)
1940{
1941 u8 *mask = data + len;
1942
1943 while (len--)
1944 if (*data++ & ~*mask++)
1945 return false;
1946
1947 return true;
1948}
1949
Pravin B Shelare6445712013-10-03 18:16:47 -07001950static int validate_set(const struct nlattr *a,
1951 const struct sw_flow_key *flow_key,
1952 struct sw_flow_actions **sfa,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001953 bool *skip_copy, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001954{
1955 const struct nlattr *ovs_key = nla_data(a);
1956 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001957 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001958
1959 /* There can be only one key in a action */
1960 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1961 return -EINVAL;
1962
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001963 key_len = nla_len(ovs_key);
1964 if (masked)
1965 key_len /= 2;
1966
Pravin B Shelare6445712013-10-03 18:16:47 -07001967 if (key_type > OVS_KEY_ATTR_MAX ||
Jesse Gross982b5272015-09-11 18:38:28 -07001968 !check_attr_len(key_len, ovs_key_lens[key_type].len))
Pravin B Shelare6445712013-10-03 18:16:47 -07001969 return -EINVAL;
1970
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001971 if (masked && !validate_masked(nla_data(ovs_key), key_len))
1972 return -EINVAL;
1973
Pravin B Shelare6445712013-10-03 18:16:47 -07001974 switch (key_type) {
1975 const struct ovs_key_ipv4 *ipv4_key;
1976 const struct ovs_key_ipv6 *ipv6_key;
1977 int err;
1978
1979 case OVS_KEY_ATTR_PRIORITY:
1980 case OVS_KEY_ATTR_SKB_MARK:
Joe Stringer182e3042015-08-26 11:31:49 -07001981 case OVS_KEY_ATTR_CT_MARK:
Joe Stringerc2ac6672015-08-26 11:31:52 -07001982 case OVS_KEY_ATTR_CT_LABEL:
Pravin B Shelare6445712013-10-03 18:16:47 -07001983 case OVS_KEY_ATTR_ETHERNET:
1984 break;
1985
1986 case OVS_KEY_ATTR_TUNNEL:
Simon Horman25cd9ba2014-10-06 05:05:13 -07001987 if (eth_p_mpls(eth_type))
1988 return -EINVAL;
1989
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08001990 if (masked)
1991 return -EINVAL; /* Masked tunnel set not supported. */
1992
1993 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001994 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001995 if (err)
1996 return err;
1997 break;
1998
1999 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002000 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07002001 return -EINVAL;
2002
Pravin B Shelare6445712013-10-03 18:16:47 -07002003 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002004
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002005 if (masked) {
2006 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002007
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002008 /* Non-writeable fields. */
2009 if (mask->ipv4_proto || mask->ipv4_frag)
2010 return -EINVAL;
2011 } else {
2012 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2013 return -EINVAL;
2014
2015 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2016 return -EINVAL;
2017 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002018 break;
2019
2020 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002021 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07002022 return -EINVAL;
2023
Pravin B Shelare6445712013-10-03 18:16:47 -07002024 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002025
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002026 if (masked) {
2027 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002028
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002029 /* Non-writeable fields. */
2030 if (mask->ipv6_proto || mask->ipv6_frag)
2031 return -EINVAL;
2032
2033 /* Invalid bits in the flow label mask? */
2034 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2035 return -EINVAL;
2036 } else {
2037 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2038 return -EINVAL;
2039
2040 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2041 return -EINVAL;
2042 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002043 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2044 return -EINVAL;
2045
2046 break;
2047
2048 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002049 if ((eth_type != htons(ETH_P_IP) &&
2050 eth_type != htons(ETH_P_IPV6)) ||
2051 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002052 return -EINVAL;
2053
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002054 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002055
2056 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002057 if ((eth_type != htons(ETH_P_IP) &&
2058 eth_type != htons(ETH_P_IPV6)) ||
2059 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002060 return -EINVAL;
2061
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002062 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002063
2064 case OVS_KEY_ATTR_MPLS:
2065 if (!eth_p_mpls(eth_type))
2066 return -EINVAL;
2067 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002068
2069 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002070 if ((eth_type != htons(ETH_P_IP) &&
2071 eth_type != htons(ETH_P_IPV6)) ||
2072 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002073 return -EINVAL;
2074
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002075 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002076
2077 default:
2078 return -EINVAL;
2079 }
2080
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002081 /* Convert non-masked non-tunnel set actions to masked set actions. */
2082 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2083 int start, len = key_len * 2;
2084 struct nlattr *at;
2085
2086 *skip_copy = true;
2087
2088 start = add_nested_action_start(sfa,
2089 OVS_ACTION_ATTR_SET_TO_MASKED,
2090 log);
2091 if (start < 0)
2092 return start;
2093
2094 at = __add_action(sfa, key_type, NULL, len, log);
2095 if (IS_ERR(at))
2096 return PTR_ERR(at);
2097
2098 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2099 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2100 /* Clear non-writeable bits from otherwise writeable fields. */
2101 if (key_type == OVS_KEY_ATTR_IPV6) {
2102 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2103
2104 mask->ipv6_label &= htonl(0x000FFFFF);
2105 }
2106 add_nested_action_end(*sfa, start);
2107 }
2108
Pravin B Shelare6445712013-10-03 18:16:47 -07002109 return 0;
2110}
2111
2112static int validate_userspace(const struct nlattr *attr)
2113{
2114 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2115 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2116 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002117 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002118 };
2119 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2120 int error;
2121
2122 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
2123 attr, userspace_policy);
2124 if (error)
2125 return error;
2126
2127 if (!a[OVS_USERSPACE_ATTR_PID] ||
2128 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2129 return -EINVAL;
2130
2131 return 0;
2132}
2133
2134static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002135 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002136{
2137 int totlen = NLA_ALIGN(from->nla_len);
2138 struct nlattr *to;
2139
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002140 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002141 if (IS_ERR(to))
2142 return PTR_ERR(to);
2143
2144 memcpy(to, from, totlen);
2145 return 0;
2146}
2147
Joe Stringer7f8a4362015-08-26 11:31:48 -07002148static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002149 const struct sw_flow_key *key,
2150 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002151 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002152{
2153 const struct nlattr *a;
2154 int rem, err;
2155
2156 if (depth >= SAMPLE_ACTION_DEPTH)
2157 return -EOVERFLOW;
2158
2159 nla_for_each_nested(a, attr, rem) {
2160 /* Expected argument lengths, (u32)-1 for variable length. */
2161 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2162 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002163 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002164 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002165 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2166 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002167 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2168 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2169 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002170 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002171 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002172 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2173 [OVS_ACTION_ATTR_CT] = (u32)-1,
Pravin B Shelare6445712013-10-03 18:16:47 -07002174 };
2175 const struct ovs_action_push_vlan *vlan;
2176 int type = nla_type(a);
2177 bool skip_copy;
2178
2179 if (type > OVS_ACTION_ATTR_MAX ||
2180 (action_lens[type] != nla_len(a) &&
2181 action_lens[type] != (u32)-1))
2182 return -EINVAL;
2183
2184 skip_copy = false;
2185 switch (type) {
2186 case OVS_ACTION_ATTR_UNSPEC:
2187 return -EINVAL;
2188
2189 case OVS_ACTION_ATTR_USERSPACE:
2190 err = validate_userspace(a);
2191 if (err)
2192 return err;
2193 break;
2194
2195 case OVS_ACTION_ATTR_OUTPUT:
2196 if (nla_get_u32(a) >= DP_MAX_PORTS)
2197 return -EINVAL;
2198 break;
2199
Andy Zhou971427f32014-09-15 19:37:25 -07002200 case OVS_ACTION_ATTR_HASH: {
2201 const struct ovs_action_hash *act_hash = nla_data(a);
2202
2203 switch (act_hash->hash_alg) {
2204 case OVS_HASH_ALG_L4:
2205 break;
2206 default:
2207 return -EINVAL;
2208 }
2209
2210 break;
2211 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002212
2213 case OVS_ACTION_ATTR_POP_VLAN:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002214 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07002215 break;
2216
2217 case OVS_ACTION_ATTR_PUSH_VLAN:
2218 vlan = nla_data(a);
2219 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
2220 return -EINVAL;
2221 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2222 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002223 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07002224 break;
2225
Andy Zhou971427f32014-09-15 19:37:25 -07002226 case OVS_ACTION_ATTR_RECIRC:
2227 break;
2228
Simon Horman25cd9ba2014-10-06 05:05:13 -07002229 case OVS_ACTION_ATTR_PUSH_MPLS: {
2230 const struct ovs_action_push_mpls *mpls = nla_data(a);
2231
Simon Horman25cd9ba2014-10-06 05:05:13 -07002232 if (!eth_p_mpls(mpls->mpls_ethertype))
2233 return -EINVAL;
2234 /* Prohibit push MPLS other than to a white list
2235 * for packets that have a known tag order.
2236 */
2237 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2238 (eth_type != htons(ETH_P_IP) &&
2239 eth_type != htons(ETH_P_IPV6) &&
2240 eth_type != htons(ETH_P_ARP) &&
2241 eth_type != htons(ETH_P_RARP) &&
2242 !eth_p_mpls(eth_type)))
2243 return -EINVAL;
2244 eth_type = mpls->mpls_ethertype;
2245 break;
2246 }
2247
2248 case OVS_ACTION_ATTR_POP_MPLS:
2249 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2250 !eth_p_mpls(eth_type))
2251 return -EINVAL;
2252
2253 /* Disallow subsequent L2.5+ set and mpls_pop actions
2254 * as there is no check here to ensure that the new
2255 * eth_type is valid and thus set actions could
2256 * write off the end of the packet or otherwise
2257 * corrupt it.
2258 *
2259 * Support for these actions is planned using packet
2260 * recirculation.
2261 */
2262 eth_type = htons(0);
2263 break;
2264
Pravin B Shelare6445712013-10-03 18:16:47 -07002265 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002266 err = validate_set(a, key, sfa,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002267 &skip_copy, eth_type, false, log);
2268 if (err)
2269 return err;
2270 break;
2271
2272 case OVS_ACTION_ATTR_SET_MASKED:
2273 err = validate_set(a, key, sfa,
2274 &skip_copy, eth_type, true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002275 if (err)
2276 return err;
2277 break;
2278
2279 case OVS_ACTION_ATTR_SAMPLE:
Joe Stringer7f8a4362015-08-26 11:31:48 -07002280 err = validate_and_copy_sample(net, a, key, depth, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002281 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002282 if (err)
2283 return err;
2284 skip_copy = true;
2285 break;
2286
Joe Stringer7f8a4362015-08-26 11:31:48 -07002287 case OVS_ACTION_ATTR_CT:
2288 err = ovs_ct_copy_action(net, a, key, sfa, log);
2289 if (err)
2290 return err;
2291 skip_copy = true;
2292 break;
2293
Pravin B Shelare6445712013-10-03 18:16:47 -07002294 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002295 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002296 return -EINVAL;
2297 }
2298 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002299 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002300 if (err)
2301 return err;
2302 }
2303 }
2304
2305 if (rem > 0)
2306 return -EINVAL;
2307
2308 return 0;
2309}
2310
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002311/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07002312int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002313 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002314 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07002315{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002316 int err;
2317
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002318 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002319 if (IS_ERR(*sfa))
2320 return PTR_ERR(*sfa);
2321
Joe Stringer8e2fed12015-08-26 11:31:44 -07002322 (*sfa)->orig_len = nla_len(attr);
Joe Stringer7f8a4362015-08-26 11:31:48 -07002323 err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002324 key->eth.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002325 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02002326 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002327
2328 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002329}
2330
Pravin B Shelare6445712013-10-03 18:16:47 -07002331static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
2332{
2333 const struct nlattr *a;
2334 struct nlattr *start;
2335 int err = 0, rem;
2336
2337 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2338 if (!start)
2339 return -EMSGSIZE;
2340
2341 nla_for_each_nested(a, attr, rem) {
2342 int type = nla_type(a);
2343 struct nlattr *st_sample;
2344
2345 switch (type) {
2346 case OVS_SAMPLE_ATTR_PROBABILITY:
2347 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
2348 sizeof(u32), nla_data(a)))
2349 return -EMSGSIZE;
2350 break;
2351 case OVS_SAMPLE_ATTR_ACTIONS:
2352 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2353 if (!st_sample)
2354 return -EMSGSIZE;
2355 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
2356 if (err)
2357 return err;
2358 nla_nest_end(skb, st_sample);
2359 break;
2360 }
2361 }
2362
2363 nla_nest_end(skb, start);
2364 return err;
2365}
2366
2367static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2368{
2369 const struct nlattr *ovs_key = nla_data(a);
2370 int key_type = nla_type(ovs_key);
2371 struct nlattr *start;
2372 int err;
2373
2374 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07002375 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02002376 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2377 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002378
Pravin B Shelare6445712013-10-03 18:16:47 -07002379 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2380 if (!start)
2381 return -EMSGSIZE;
2382
Thomas Graf1d8fff92015-07-21 10:43:54 +02002383 err = ipv4_tun_to_nlattr(skb, &tun_info->key,
Jesse Grossf5796682014-10-03 15:35:33 -07002384 tun_info->options_len ?
Pravin B Shelar4c222792015-08-30 18:09:38 -07002385 ip_tunnel_info_opts(tun_info) : NULL,
Jesse Grossf5796682014-10-03 15:35:33 -07002386 tun_info->options_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07002387 if (err)
2388 return err;
2389 nla_nest_end(skb, start);
2390 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002391 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002392 default:
2393 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2394 return -EMSGSIZE;
2395 break;
2396 }
2397
2398 return 0;
2399}
2400
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002401static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2402 struct sk_buff *skb)
2403{
2404 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08002405 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002406 size_t key_len = nla_len(ovs_key) / 2;
2407
2408 /* Revert the conversion we did from a non-masked set action to
2409 * masked set action.
2410 */
Joe Stringerf4f8e732015-03-02 18:49:56 -08002411 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2412 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002413 return -EMSGSIZE;
2414
Joe Stringerf4f8e732015-03-02 18:49:56 -08002415 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2416 return -EMSGSIZE;
2417
2418 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002419 return 0;
2420}
2421
Pravin B Shelare6445712013-10-03 18:16:47 -07002422int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2423{
2424 const struct nlattr *a;
2425 int rem, err;
2426
2427 nla_for_each_attr(a, attr, len, rem) {
2428 int type = nla_type(a);
2429
2430 switch (type) {
2431 case OVS_ACTION_ATTR_SET:
2432 err = set_action_to_attr(a, skb);
2433 if (err)
2434 return err;
2435 break;
2436
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002437 case OVS_ACTION_ATTR_SET_TO_MASKED:
2438 err = masked_set_action_to_set_action_attr(a, skb);
2439 if (err)
2440 return err;
2441 break;
2442
Pravin B Shelare6445712013-10-03 18:16:47 -07002443 case OVS_ACTION_ATTR_SAMPLE:
2444 err = sample_action_to_attr(a, skb);
2445 if (err)
2446 return err;
2447 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002448
2449 case OVS_ACTION_ATTR_CT:
2450 err = ovs_ct_action_to_attr(nla_data(a), skb);
2451 if (err)
2452 return err;
2453 break;
2454
Pravin B Shelare6445712013-10-03 18:16:47 -07002455 default:
2456 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2457 return -EMSGSIZE;
2458 break;
2459 }
2460 }
2461
2462 return 0;
2463}