blob: a08ff834676ba9a8f82bb602cbc15d0d20e76478 [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{
Jiri Benc0a6410f2016-11-10 16:28:22 +0100126 u64 key_expected = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700127 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)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800132 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
Pravin B Shelare6445712013-10-03 18:16:47 -0700133 | (1 << OVS_KEY_ATTR_IPV6)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800134 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
Pravin B Shelare6445712013-10-03 18:16:47 -0700135 | (1 << OVS_KEY_ATTR_TCP)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700136 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700137 | (1 << OVS_KEY_ATTR_UDP)
138 | (1 << OVS_KEY_ATTR_SCTP)
139 | (1 << OVS_KEY_ATTR_ICMP)
140 | (1 << OVS_KEY_ATTR_ICMPV6)
141 | (1 << OVS_KEY_ATTR_ARP)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700142 | (1 << OVS_KEY_ATTR_ND)
143 | (1 << OVS_KEY_ATTR_MPLS));
Pravin B Shelare6445712013-10-03 18:16:47 -0700144
145 /* Always allowed mask fields. */
146 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
147 | (1 << OVS_KEY_ATTR_IN_PORT)
148 | (1 << OVS_KEY_ATTR_ETHERTYPE));
149
150 /* Check key attributes. */
151 if (match->key->eth.type == htons(ETH_P_ARP)
152 || match->key->eth.type == htons(ETH_P_RARP)) {
153 key_expected |= 1 << OVS_KEY_ATTR_ARP;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800154 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700155 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
156 }
157
Simon Horman25cd9ba2014-10-06 05:05:13 -0700158 if (eth_p_mpls(match->key->eth.type)) {
159 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
160 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
161 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
162 }
163
Pravin B Shelare6445712013-10-03 18:16:47 -0700164 if (match->key->eth.type == htons(ETH_P_IP)) {
165 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800166 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700167 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800168 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
169 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700170
171 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
172 if (match->key->ip.proto == IPPROTO_UDP) {
173 key_expected |= 1 << OVS_KEY_ATTR_UDP;
174 if (match->mask && (match->mask->key.ip.proto == 0xff))
175 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
176 }
177
178 if (match->key->ip.proto == IPPROTO_SCTP) {
179 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
180 if (match->mask && (match->mask->key.ip.proto == 0xff))
181 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
182 }
183
184 if (match->key->ip.proto == IPPROTO_TCP) {
185 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700186 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
187 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700188 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700189 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
190 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700191 }
192
193 if (match->key->ip.proto == IPPROTO_ICMP) {
194 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
195 if (match->mask && (match->mask->key.ip.proto == 0xff))
196 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
197 }
198 }
199 }
200
201 if (match->key->eth.type == htons(ETH_P_IPV6)) {
202 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800203 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700204 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800205 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
206 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700207
208 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
209 if (match->key->ip.proto == IPPROTO_UDP) {
210 key_expected |= 1 << OVS_KEY_ATTR_UDP;
211 if (match->mask && (match->mask->key.ip.proto == 0xff))
212 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
213 }
214
215 if (match->key->ip.proto == IPPROTO_SCTP) {
216 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
217 if (match->mask && (match->mask->key.ip.proto == 0xff))
218 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
219 }
220
221 if (match->key->ip.proto == IPPROTO_TCP) {
222 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700223 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
224 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700225 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700226 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
227 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700228 }
229
230 if (match->key->ip.proto == IPPROTO_ICMPV6) {
231 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
232 if (match->mask && (match->mask->key.ip.proto == 0xff))
233 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
234
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700235 if (match->key->tp.src ==
Pravin B Shelare6445712013-10-03 18:16:47 -0700236 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700237 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700238 key_expected |= 1 << OVS_KEY_ATTR_ND;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800239 /* Original direction conntrack tuple
240 * uses the same space as the ND fields
241 * in the key, so both are not allowed
242 * at the same time.
243 */
244 mask_allowed &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800245 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700246 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
247 }
248 }
249 }
250 }
251
252 if ((key_attrs & key_expected) != key_expected) {
253 /* Key attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800254 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
255 (unsigned long long)key_attrs,
256 (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700257 return false;
258 }
259
260 if ((mask_attrs & mask_allowed) != mask_attrs) {
261 /* Mask attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800262 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
263 (unsigned long long)mask_attrs,
264 (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700265 return false;
266 }
267
268 return true;
269}
270
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800271size_t ovs_tun_key_attr_size(void)
272{
273 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
274 * updating this function.
275 */
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200276 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
Jiri Benc6b26ba32015-10-05 13:09:47 +0200277 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
278 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800279 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
280 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
281 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
282 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
283 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
284 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
Thomas Graf1dd144c2015-01-15 03:53:59 +0100285 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
286 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
287 */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800288 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
289 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
290}
291
Joe Stringer41af73e2014-10-18 16:14:14 -0700292size_t ovs_key_attr_size(void)
293{
294 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
295 * updating this function.
296 */
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800297 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 28);
Joe Stringer41af73e2014-10-18 16:14:14 -0700298
299 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
300 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800301 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700302 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
303 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
304 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
305 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
Joe Stringerfbccce52015-10-06 11:00:00 -0700306 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700307 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
Joe Stringer182e3042015-08-26 11:31:49 -0700308 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
Joe Stringer33db4122015-10-01 15:00:37 -0700309 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800310 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
Joe Stringer41af73e2014-10-18 16:14:14 -0700311 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
312 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
313 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
314 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
315 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
316 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
317 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
318 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
319}
320
Jesse Gross982b5272015-09-11 18:38:28 -0700321static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
322 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
323};
324
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100325static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
326 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
327 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
328 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
329 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
330 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
331 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
332 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
333 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
334 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
335 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
Jesse Gross982b5272015-09-11 18:38:28 -0700336 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
337 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
338 .next = ovs_vxlan_ext_key_lens },
Jiri Benc6b26ba32015-10-05 13:09:47 +0200339 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
340 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100341};
342
Pravin B Shelare6445712013-10-03 18:16:47 -0700343/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100344static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
345 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
346 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
347 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
348 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
349 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
350 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
351 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
352 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
353 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
354 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
355 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
356 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
357 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
358 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
359 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
360 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
361 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
362 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
363 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
364 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
365 .next = ovs_tunnel_key_lens, },
366 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
Joe Stringerfbccce52015-10-06 11:00:00 -0700367 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700368 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700369 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
Joe Stringer33db4122015-10-01 15:00:37 -0700370 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800371 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
372 .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
373 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
374 .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
Pravin B Shelare6445712013-10-03 18:16:47 -0700375};
376
Jesse Gross982b5272015-09-11 18:38:28 -0700377static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
378{
379 return expected_len == attr_len ||
380 expected_len == OVS_ATTR_NESTED ||
381 expected_len == OVS_ATTR_VARIABLE;
382}
383
Pravin B Shelare6445712013-10-03 18:16:47 -0700384static bool is_all_zero(const u8 *fp, size_t size)
385{
386 int i;
387
388 if (!fp)
389 return false;
390
391 for (i = 0; i < size; i++)
392 if (fp[i])
393 return false;
394
395 return true;
396}
397
398static int __parse_flow_nlattrs(const struct nlattr *attr,
399 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800400 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700401{
402 const struct nlattr *nla;
403 u64 attrs;
404 int rem;
405
406 attrs = *attrsp;
407 nla_for_each_nested(nla, attr, rem) {
408 u16 type = nla_type(nla);
409 int expected_len;
410
411 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800412 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700413 type, OVS_KEY_ATTR_MAX);
414 return -EINVAL;
415 }
416
417 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800418 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700419 return -EINVAL;
420 }
421
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100422 expected_len = ovs_key_lens[type].len;
Jesse Gross982b5272015-09-11 18:38:28 -0700423 if (!check_attr_len(nla_len(nla), expected_len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800424 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
425 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700426 return -EINVAL;
427 }
428
429 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
430 attrs |= 1 << type;
431 a[type] = nla;
432 }
433 }
434 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800435 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700436 return -EINVAL;
437 }
438
439 *attrsp = attrs;
440 return 0;
441}
442
443static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800444 const struct nlattr *a[], u64 *attrsp,
445 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700446{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800447 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700448}
449
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800450int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
451 u64 *attrsp, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700452{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800453 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
454}
455
456static int genev_tun_opt_from_nlattr(const struct nlattr *a,
457 struct sw_flow_match *match, bool is_mask,
458 bool log)
459{
460 unsigned long opt_key_offset;
461
462 if (nla_len(a) > sizeof(match->key->tun_opts)) {
463 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
464 nla_len(a), sizeof(match->key->tun_opts));
465 return -EINVAL;
466 }
467
468 if (nla_len(a) % 4 != 0) {
469 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
470 nla_len(a));
471 return -EINVAL;
472 }
473
474 /* We need to record the length of the options passed
475 * down, otherwise packets with the same format but
476 * additional options will be silently matched.
477 */
478 if (!is_mask) {
479 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
480 false);
481 } else {
482 /* This is somewhat unusual because it looks at
483 * both the key and mask while parsing the
484 * attributes (and by extension assumes the key
485 * is parsed first). Normally, we would verify
486 * that each is the correct length and that the
487 * attributes line up in the validate function.
488 * However, that is difficult because this is
489 * variable length and we won't have the
490 * information later.
491 */
492 if (match->key->tun_opts_len != nla_len(a)) {
493 OVS_NLERR(log, "Geneve option len %d != mask len %d",
494 match->key->tun_opts_len, nla_len(a));
495 return -EINVAL;
496 }
497
498 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
499 }
500
Thomas Grafd91641d2015-01-15 03:53:57 +0100501 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800502 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
503 nla_len(a), is_mask);
504 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700505}
506
Jesse Gross982b5272015-09-11 18:38:28 -0700507static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100508 struct sw_flow_match *match, bool is_mask,
509 bool log)
510{
Jesse Gross982b5272015-09-11 18:38:28 -0700511 struct nlattr *a;
512 int rem;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100513 unsigned long opt_key_offset;
Thomas Graf614732e2015-07-21 10:44:06 +0200514 struct vxlan_metadata opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100515
516 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
517
Thomas Graf1dd144c2015-01-15 03:53:59 +0100518 memset(&opts, 0, sizeof(opts));
Jesse Gross982b5272015-09-11 18:38:28 -0700519 nla_for_each_nested(a, attr, rem) {
520 int type = nla_type(a);
Thomas Graf1dd144c2015-01-15 03:53:59 +0100521
Jesse Gross982b5272015-09-11 18:38:28 -0700522 if (type > OVS_VXLAN_EXT_MAX) {
523 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
524 type, OVS_VXLAN_EXT_MAX);
525 return -EINVAL;
526 }
527
528 if (!check_attr_len(nla_len(a),
529 ovs_vxlan_ext_key_lens[type].len)) {
530 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
531 type, nla_len(a),
532 ovs_vxlan_ext_key_lens[type].len);
533 return -EINVAL;
534 }
535
536 switch (type) {
537 case OVS_VXLAN_EXT_GBP:
538 opts.gbp = nla_get_u32(a);
539 break;
540 default:
541 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
542 type);
543 return -EINVAL;
544 }
545 }
546 if (rem) {
547 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
548 rem);
549 return -EINVAL;
550 }
Thomas Graf1dd144c2015-01-15 03:53:59 +0100551
552 if (!is_mask)
553 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
554 else
555 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
556
557 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
558 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
559 is_mask);
560 return 0;
561}
562
Jiri Benc6b26ba32015-10-05 13:09:47 +0200563static int ip_tun_from_nlattr(const struct nlattr *attr,
564 struct sw_flow_match *match, bool is_mask,
565 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700566{
Pravin B Shelar99e28f12015-10-20 20:47:46 -0700567 bool ttl = false, ipv4 = false, ipv6 = false;
568 __be16 tun_flags = 0;
569 int opts_type = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700570 struct nlattr *a;
571 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700572
573 nla_for_each_nested(a, attr, rem) {
574 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800575 int err;
576
Pravin B Shelare6445712013-10-03 18:16:47 -0700577 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800578 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
579 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700580 return -EINVAL;
581 }
582
Jesse Gross982b5272015-09-11 18:38:28 -0700583 if (!check_attr_len(nla_len(a),
584 ovs_tunnel_key_lens[type].len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800585 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100586 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700587 return -EINVAL;
588 }
589
590 switch (type) {
591 case OVS_TUNNEL_KEY_ATTR_ID:
592 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
593 nla_get_be64(a), is_mask);
594 tun_flags |= TUNNEL_KEY;
595 break;
596 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200597 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
Jiri Benc67b61f62015-03-29 16:59:26 +0200598 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200599 ipv4 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700600 break;
601 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200602 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
Jiri Benc67b61f62015-03-29 16:59:26 +0200603 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200604 ipv4 = true;
605 break;
606 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
Or Gerlitz3d20f1f2017-03-15 18:10:47 +0200607 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
Jiri Benc6b26ba32015-10-05 13:09:47 +0200608 nla_get_in6_addr(a), is_mask);
609 ipv6 = true;
610 break;
611 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
612 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
613 nla_get_in6_addr(a), is_mask);
614 ipv6 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700615 break;
616 case OVS_TUNNEL_KEY_ATTR_TOS:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200617 SW_FLOW_KEY_PUT(match, tun_key.tos,
Pravin B Shelare6445712013-10-03 18:16:47 -0700618 nla_get_u8(a), is_mask);
619 break;
620 case OVS_TUNNEL_KEY_ATTR_TTL:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200621 SW_FLOW_KEY_PUT(match, tun_key.ttl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700622 nla_get_u8(a), is_mask);
623 ttl = true;
624 break;
625 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
626 tun_flags |= TUNNEL_DONT_FRAGMENT;
627 break;
628 case OVS_TUNNEL_KEY_ATTR_CSUM:
629 tun_flags |= TUNNEL_CSUM;
630 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800631 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
632 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
633 nla_get_be16(a), is_mask);
634 break;
635 case OVS_TUNNEL_KEY_ATTR_TP_DST:
636 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
637 nla_get_be16(a), is_mask);
638 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700639 case OVS_TUNNEL_KEY_ATTR_OAM:
640 tun_flags |= TUNNEL_OAM;
641 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700642 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Thomas Graf1dd144c2015-01-15 03:53:59 +0100643 if (opts_type) {
644 OVS_NLERR(log, "Multiple metadata blocks provided");
645 return -EINVAL;
646 }
647
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800648 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
649 if (err)
650 return err;
651
Thomas Graf1dd144c2015-01-15 03:53:59 +0100652 tun_flags |= TUNNEL_GENEVE_OPT;
653 opts_type = type;
654 break;
655 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
656 if (opts_type) {
657 OVS_NLERR(log, "Multiple metadata blocks provided");
658 return -EINVAL;
659 }
660
661 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
662 if (err)
663 return err;
664
665 tun_flags |= TUNNEL_VXLAN_OPT;
666 opts_type = type;
Jesse Grossf5796682014-10-03 15:35:33 -0700667 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700668 default:
Jiri Benc6b26ba32015-10-05 13:09:47 +0200669 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700670 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700671 return -EINVAL;
672 }
673 }
674
675 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
Jiri Benc00a93ba2015-10-05 13:09:46 +0200676 if (is_mask)
677 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
678 else
Jiri Benc6b26ba32015-10-05 13:09:47 +0200679 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
680 false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700681
682 if (rem > 0) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200683 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800684 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700685 return -EINVAL;
686 }
687
Jiri Benc6b26ba32015-10-05 13:09:47 +0200688 if (ipv4 && ipv6) {
689 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
690 return -EINVAL;
691 }
692
Pravin B Shelare6445712013-10-03 18:16:47 -0700693 if (!is_mask) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200694 if (!ipv4 && !ipv6) {
695 OVS_NLERR(log, "IP tunnel dst address not specified");
696 return -EINVAL;
697 }
698 if (ipv4 && !match->key->tun_key.u.ipv4.dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800699 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700700 return -EINVAL;
701 }
Jiri Benc6b26ba32015-10-05 13:09:47 +0200702 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
703 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
704 return -EINVAL;
705 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700706
707 if (!ttl) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200708 OVS_NLERR(log, "IP tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700709 return -EINVAL;
710 }
711 }
712
Thomas Graf1dd144c2015-01-15 03:53:59 +0100713 return opts_type;
714}
715
716static int vxlan_opt_to_nlattr(struct sk_buff *skb,
717 const void *tun_opts, int swkey_tun_opts_len)
718{
Thomas Graf614732e2015-07-21 10:44:06 +0200719 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100720 struct nlattr *nla;
721
722 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
723 if (!nla)
724 return -EMSGSIZE;
725
726 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
727 return -EMSGSIZE;
728
729 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700730 return 0;
731}
732
Jiri Benc6b26ba32015-10-05 13:09:47 +0200733static int __ip_tun_to_nlattr(struct sk_buff *skb,
734 const struct ip_tunnel_key *output,
735 const void *tun_opts, int swkey_tun_opts_len,
736 unsigned short tun_proto)
Pravin B Shelare6445712013-10-03 18:16:47 -0700737{
Pravin B Shelare6445712013-10-03 18:16:47 -0700738 if (output->tun_flags & TUNNEL_KEY &&
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200739 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
740 OVS_TUNNEL_KEY_ATTR_PAD))
Pravin B Shelare6445712013-10-03 18:16:47 -0700741 return -EMSGSIZE;
Jiri Benc6b26ba32015-10-05 13:09:47 +0200742 switch (tun_proto) {
743 case AF_INET:
744 if (output->u.ipv4.src &&
745 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
746 output->u.ipv4.src))
747 return -EMSGSIZE;
748 if (output->u.ipv4.dst &&
749 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
750 output->u.ipv4.dst))
751 return -EMSGSIZE;
752 break;
753 case AF_INET6:
754 if (!ipv6_addr_any(&output->u.ipv6.src) &&
755 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
756 &output->u.ipv6.src))
757 return -EMSGSIZE;
758 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
759 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
760 &output->u.ipv6.dst))
761 return -EMSGSIZE;
762 break;
763 }
Jiri Benc7c383fb2015-08-20 13:56:24 +0200764 if (output->tos &&
765 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700766 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200767 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700768 return -EMSGSIZE;
769 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700770 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700771 return -EMSGSIZE;
772 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700773 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
774 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800775 if (output->tp_src &&
776 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
777 return -EMSGSIZE;
778 if (output->tp_dst &&
779 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
780 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700781 if ((output->tun_flags & TUNNEL_OAM) &&
782 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700783 return -EMSGSIZE;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700784 if (swkey_tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +0100785 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
786 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
787 swkey_tun_opts_len, tun_opts))
788 return -EMSGSIZE;
789 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
790 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
791 return -EMSGSIZE;
792 }
Jesse Grossf5796682014-10-03 15:35:33 -0700793
794 return 0;
795}
796
Jiri Benc6b26ba32015-10-05 13:09:47 +0200797static int ip_tun_to_nlattr(struct sk_buff *skb,
798 const struct ip_tunnel_key *output,
799 const void *tun_opts, int swkey_tun_opts_len,
800 unsigned short tun_proto)
Jesse Grossf5796682014-10-03 15:35:33 -0700801{
802 struct nlattr *nla;
803 int err;
804
805 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
806 if (!nla)
807 return -EMSGSIZE;
808
Jiri Benc6b26ba32015-10-05 13:09:47 +0200809 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
810 tun_proto);
Jesse Grossf5796682014-10-03 15:35:33 -0700811 if (err)
812 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700813
814 nla_nest_end(skb, nla);
815 return 0;
816}
817
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700818int ovs_nla_put_tunnel_info(struct sk_buff *skb,
819 struct ip_tunnel_info *tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800820{
David S. Millerba3e2082015-10-24 06:54:12 -0700821 return __ip_tun_to_nlattr(skb, &tun_info->key,
822 ip_tunnel_info_opts(tun_info),
823 tun_info->options_len,
824 ip_tunnel_info_af(tun_info));
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800825}
826
Eric Garver018c1dd2016-09-07 12:56:59 -0400827static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
828 const struct nlattr *a[],
829 bool is_mask, bool inner)
830{
831 __be16 tci = 0;
832 __be16 tpid = 0;
833
834 if (a[OVS_KEY_ATTR_VLAN])
835 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
836
837 if (a[OVS_KEY_ATTR_ETHERTYPE])
838 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
839
840 if (likely(!inner)) {
841 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
842 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
843 } else {
844 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
845 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
846 }
847 return 0;
848}
849
850static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
851 u64 key_attrs, bool inner,
852 const struct nlattr **a, bool log)
853{
854 __be16 tci = 0;
855
856 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
857 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
858 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
859 /* Not a VLAN. */
860 return 0;
861 }
862
863 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
864 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
865 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
866 return -EINVAL;
867 }
868
869 if (a[OVS_KEY_ATTR_VLAN])
870 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
871
872 if (!(tci & htons(VLAN_TAG_PRESENT))) {
873 if (tci) {
874 OVS_NLERR(log, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
875 (inner) ? "C-VLAN" : "VLAN");
876 return -EINVAL;
877 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
878 /* Corner case for truncated VLAN header. */
879 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
880 (inner) ? "C-VLAN" : "VLAN");
881 return -EINVAL;
882 }
883 }
884
885 return 1;
886}
887
888static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
889 u64 key_attrs, bool inner,
890 const struct nlattr **a, bool log)
891{
892 __be16 tci = 0;
893 __be16 tpid = 0;
894 bool encap_valid = !!(match->key->eth.vlan.tci &
895 htons(VLAN_TAG_PRESENT));
896 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
897 htons(VLAN_TAG_PRESENT));
898
899 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
900 /* Not a VLAN. */
901 return 0;
902 }
903
904 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
905 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
906 (inner) ? "C-VLAN" : "VLAN");
907 return -EINVAL;
908 }
909
910 if (a[OVS_KEY_ATTR_VLAN])
911 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
912
913 if (a[OVS_KEY_ATTR_ETHERTYPE])
914 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
915
916 if (tpid != htons(0xffff)) {
917 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
918 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
919 return -EINVAL;
920 }
921 if (!(tci & htons(VLAN_TAG_PRESENT))) {
922 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
923 (inner) ? "C-VLAN" : "VLAN");
924 return -EINVAL;
925 }
926
927 return 1;
928}
929
930static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
931 u64 *key_attrs, bool inner,
932 const struct nlattr **a, bool is_mask,
933 bool log)
934{
935 int err;
936 const struct nlattr *encap;
937
938 if (!is_mask)
939 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
940 a, log);
941 else
942 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
943 a, log);
944 if (err <= 0)
945 return err;
946
947 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
948 if (err)
949 return err;
950
951 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
952 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
953 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
954
955 encap = a[OVS_KEY_ATTR_ENCAP];
956
957 if (!is_mask)
958 err = parse_flow_nlattrs(encap, a, key_attrs, log);
959 else
960 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
961
962 return err;
963}
964
965static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
966 u64 *key_attrs, const struct nlattr **a,
967 bool is_mask, bool log)
968{
969 int err;
970 bool encap_valid = false;
971
972 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
973 is_mask, log);
974 if (err)
975 return err;
976
977 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_TAG_PRESENT));
978 if (encap_valid) {
979 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
980 is_mask, log);
981 if (err)
982 return err;
983 }
984
985 return 0;
986}
987
Jiri Benc0a6410f2016-11-10 16:28:22 +0100988static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
989 u64 *attrs, const struct nlattr **a,
990 bool is_mask, bool log)
991{
992 __be16 eth_type;
993
994 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
995 if (is_mask) {
996 /* Always exact match EtherType. */
997 eth_type = htons(0xffff);
998 } else if (!eth_proto_is_802_3(eth_type)) {
999 OVS_NLERR(log, "EtherType %x is less than min %x",
1000 ntohs(eth_type), ETH_P_802_3_MIN);
1001 return -EINVAL;
1002 }
1003
1004 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1005 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1006 return 0;
1007}
1008
Joe Stringerc2ac6672015-08-26 11:31:52 -07001009static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1010 u64 *attrs, const struct nlattr **a,
1011 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001012{
Jiri Benc0a6410f2016-11-10 16:28:22 +01001013 u8 mac_proto = MAC_PROTO_ETHERNET;
1014
Andy Zhou971427f32014-09-15 19:37:25 -07001015 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
1016 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1017
1018 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1019 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
1020 }
1021
1022 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
1023 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1024
1025 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1026 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
1027 }
1028
Pravin B Shelare6445712013-10-03 18:16:47 -07001029 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1030 SW_FLOW_KEY_PUT(match, phy.priority,
1031 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1032 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1033 }
1034
1035 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1036 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1037
Jesse Gross426cda52014-10-06 05:08:38 -07001038 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001039 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -07001040 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001041 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -07001042 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -07001043 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001044 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001045
1046 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1047 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1048 } else if (!is_mask) {
1049 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1050 }
1051
1052 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1053 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1054
1055 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1056 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1057 }
1058 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
Jiri Benc6b26ba32015-10-05 13:09:47 +02001059 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1060 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -07001061 return -EINVAL;
1062 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1063 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001064
1065 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001066 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001067 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001068
Joe Stringer9e384712015-10-19 19:18:57 -07001069 if (ct_state & ~CT_SUPPORTED_MASK) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001070 OVS_NLERR(log, "ct_state flags %08x unsupported",
Joe Stringer6f225952015-10-06 10:59:59 -07001071 ct_state);
1072 return -EINVAL;
1073 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001074
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001075 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001076 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1077 }
1078 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001079 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -07001080 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1081
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001082 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001083 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1084 }
Joe Stringer182e3042015-08-26 11:31:49 -07001085 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001086 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
Joe Stringer182e3042015-08-26 11:31:49 -07001087 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1088
1089 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1090 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1091 }
Joe Stringer33db4122015-10-01 15:00:37 -07001092 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1093 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1094 const struct ovs_key_ct_labels *cl;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001095
Joe Stringer33db4122015-10-01 15:00:37 -07001096 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1097 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
Joe Stringerc2ac6672015-08-26 11:31:52 -07001098 sizeof(*cl), is_mask);
Joe Stringer33db4122015-10-01 15:00:37 -07001099 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001100 }
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001101 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1102 const struct ovs_key_ct_tuple_ipv4 *ct;
1103
1104 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1105
1106 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1107 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1108 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1109 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001110 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001111 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1112 }
1113 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1114 const struct ovs_key_ct_tuple_ipv6 *ct;
1115
1116 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1117
1118 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1119 sizeof(match->key->ipv6.ct_orig.src),
1120 is_mask);
1121 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1122 sizeof(match->key->ipv6.ct_orig.dst),
1123 is_mask);
1124 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1125 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001126 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001127 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1128 }
Jiri Benc329f45b2016-11-10 16:28:18 +01001129
Jiri Benc0a6410f2016-11-10 16:28:22 +01001130 /* For layer 3 packets the Ethernet type is provided
1131 * and treated as metadata but no MAC addresses are provided.
1132 */
1133 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1134 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1135 mac_proto = MAC_PROTO_NONE;
1136
Jiri Benc329f45b2016-11-10 16:28:18 +01001137 /* Always exact match mac_proto */
Jiri Benc0a6410f2016-11-10 16:28:22 +01001138 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1139
1140 if (mac_proto == MAC_PROTO_NONE)
1141 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1142 log);
Jiri Benc329f45b2016-11-10 16:28:18 +01001143
Pravin B Shelare6445712013-10-03 18:16:47 -07001144 return 0;
1145}
1146
Joe Stringerc2ac6672015-08-26 11:31:52 -07001147static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1148 u64 attrs, const struct nlattr **a,
1149 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001150{
1151 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001152
Joe Stringerc2ac6672015-08-26 11:31:52 -07001153 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001154 if (err)
1155 return err;
1156
1157 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1158 const struct ovs_key_ethernet *eth_key;
1159
1160 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1161 SW_FLOW_KEY_MEMCPY(match, eth.src,
1162 eth_key->eth_src, ETH_ALEN, is_mask);
1163 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1164 eth_key->eth_dst, ETH_ALEN, is_mask);
1165 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
Pravin B Shelare6445712013-10-03 18:16:47 -07001166
Jiri Benc0a6410f2016-11-10 16:28:22 +01001167 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1168 /* VLAN attribute is always parsed before getting here since it
1169 * may occur multiple times.
1170 */
1171 OVS_NLERR(log, "VLAN attribute unexpected.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001172 return -EINVAL;
1173 }
1174
Jiri Benc0a6410f2016-11-10 16:28:22 +01001175 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1176 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1177 log);
1178 if (err)
1179 return err;
1180 } else if (!is_mask) {
1181 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1182 }
1183 } else if (!match->key->eth.type) {
1184 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1185 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001186 }
1187
1188 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1189 const struct ovs_key_ipv4 *ipv4_key;
1190
1191 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1192 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001193 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1194 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001195 return -EINVAL;
1196 }
1197 SW_FLOW_KEY_PUT(match, ip.proto,
1198 ipv4_key->ipv4_proto, is_mask);
1199 SW_FLOW_KEY_PUT(match, ip.tos,
1200 ipv4_key->ipv4_tos, is_mask);
1201 SW_FLOW_KEY_PUT(match, ip.ttl,
1202 ipv4_key->ipv4_ttl, is_mask);
1203 SW_FLOW_KEY_PUT(match, ip.frag,
1204 ipv4_key->ipv4_frag, is_mask);
1205 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1206 ipv4_key->ipv4_src, is_mask);
1207 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1208 ipv4_key->ipv4_dst, is_mask);
1209 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1210 }
1211
1212 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1213 const struct ovs_key_ipv6 *ipv6_key;
1214
1215 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1216 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001217 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1218 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001219 return -EINVAL;
1220 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001221
Joe Stringerd3052bb2014-11-19 13:54:49 -08001222 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
David S. Miller14591432014-11-21 22:28:24 -05001223 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001224 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1225 return -EINVAL;
1226 }
1227
Pravin B Shelare6445712013-10-03 18:16:47 -07001228 SW_FLOW_KEY_PUT(match, ipv6.label,
1229 ipv6_key->ipv6_label, is_mask);
1230 SW_FLOW_KEY_PUT(match, ip.proto,
1231 ipv6_key->ipv6_proto, is_mask);
1232 SW_FLOW_KEY_PUT(match, ip.tos,
1233 ipv6_key->ipv6_tclass, is_mask);
1234 SW_FLOW_KEY_PUT(match, ip.ttl,
1235 ipv6_key->ipv6_hlimit, is_mask);
1236 SW_FLOW_KEY_PUT(match, ip.frag,
1237 ipv6_key->ipv6_frag, is_mask);
1238 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1239 ipv6_key->ipv6_src,
1240 sizeof(match->key->ipv6.addr.src),
1241 is_mask);
1242 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1243 ipv6_key->ipv6_dst,
1244 sizeof(match->key->ipv6.addr.dst),
1245 is_mask);
1246
1247 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1248 }
1249
1250 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1251 const struct ovs_key_arp *arp_key;
1252
1253 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1254 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001255 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -07001256 arp_key->arp_op);
1257 return -EINVAL;
1258 }
1259
1260 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1261 arp_key->arp_sip, is_mask);
1262 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1263 arp_key->arp_tip, is_mask);
1264 SW_FLOW_KEY_PUT(match, ip.proto,
1265 ntohs(arp_key->arp_op), is_mask);
1266 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1267 arp_key->arp_sha, ETH_ALEN, is_mask);
1268 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1269 arp_key->arp_tha, ETH_ALEN, is_mask);
1270
1271 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1272 }
1273
Simon Horman25cd9ba2014-10-06 05:05:13 -07001274 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
1275 const struct ovs_key_mpls *mpls_key;
1276
1277 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1278 SW_FLOW_KEY_PUT(match, mpls.top_lse,
1279 mpls_key->mpls_lse, is_mask);
1280
1281 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1282 }
1283
Pravin B Shelare6445712013-10-03 18:16:47 -07001284 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1285 const struct ovs_key_tcp *tcp_key;
1286
1287 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001288 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1289 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001290 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1291 }
1292
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001293 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -07001294 SW_FLOW_KEY_PUT(match, tp.flags,
1295 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1296 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001297 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1298 }
1299
Pravin B Shelare6445712013-10-03 18:16:47 -07001300 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1301 const struct ovs_key_udp *udp_key;
1302
1303 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001304 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1305 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001306 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1307 }
1308
1309 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1310 const struct ovs_key_sctp *sctp_key;
1311
1312 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001313 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1314 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001315 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1316 }
1317
1318 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1319 const struct ovs_key_icmp *icmp_key;
1320
1321 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001322 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001323 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001324 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001325 htons(icmp_key->icmp_code), is_mask);
1326 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1327 }
1328
1329 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1330 const struct ovs_key_icmpv6 *icmpv6_key;
1331
1332 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001333 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001334 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001335 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001336 htons(icmpv6_key->icmpv6_code), is_mask);
1337 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1338 }
1339
1340 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1341 const struct ovs_key_nd *nd_key;
1342
1343 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1344 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1345 nd_key->nd_target,
1346 sizeof(match->key->ipv6.nd.target),
1347 is_mask);
1348 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1349 nd_key->nd_sll, ETH_ALEN, is_mask);
1350 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1351 nd_key->nd_tll, ETH_ALEN, is_mask);
1352 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1353 }
1354
Jesse Gross426cda52014-10-06 05:08:38 -07001355 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001356 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001357 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001358 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001359 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001360
1361 return 0;
1362}
1363
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001364static void nlattr_set(struct nlattr *attr, u8 val,
1365 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001366{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001367 struct nlattr *nla;
1368 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001369
Pravin B Shelarf47de062014-10-16 21:55:45 -07001370 /* The nlattr stream should already have been validated */
1371 nla_for_each_nested(nla, attr, rem) {
Jesse Gross982b5272015-09-11 18:38:28 -07001372 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1373 if (tbl[nla_type(nla)].next)
1374 tbl = tbl[nla_type(nla)].next;
1375 nlattr_set(nla, val, tbl);
1376 } else {
Pravin B Shelarf47de062014-10-16 21:55:45 -07001377 memset(nla_data(nla), val, nla_len(nla));
Jesse Gross982b5272015-09-11 18:38:28 -07001378 }
Joe Stringer9e384712015-10-19 19:18:57 -07001379
1380 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1381 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001382 }
1383}
1384
1385static void mask_set_nlattr(struct nlattr *attr, u8 val)
1386{
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001387 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001388}
1389
1390/**
1391 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1392 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1393 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1394 * does not include any don't care bit.
Joe Stringerc2ac6672015-08-26 11:31:52 -07001395 * @net: Used to determine per-namespace field support.
Pravin B Shelare6445712013-10-03 18:16:47 -07001396 * @match: receives the extracted flow match information.
1397 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1398 * sequence. The fields should of the packet that triggered the creation
1399 * of this flow.
1400 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1401 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001402 * @log: Boolean to allow kernel error logging. Normally true, but when
1403 * probing for feature compatibility this should be passed in as false to
1404 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001405 */
Joe Stringerc2ac6672015-08-26 11:31:52 -07001406int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001407 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001408 const struct nlattr *nla_mask,
1409 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001410{
1411 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelarf47de062014-10-16 21:55:45 -07001412 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001413 u64 key_attrs = 0;
1414 u64 mask_attrs = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001415 int err;
1416
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001417 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001418 if (err)
1419 return err;
1420
Eric Garver018c1dd2016-09-07 12:56:59 -04001421 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1422 if (err)
1423 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001424
Joe Stringerc2ac6672015-08-26 11:31:52 -07001425 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001426 if (err)
1427 return err;
1428
Pravin B Shelara85311b2014-10-19 12:03:40 -07001429 if (match->mask) {
1430 if (!nla_mask) {
1431 /* Create an exact match mask. We need to set to 0xff
1432 * all the 'match->mask' fields that have been touched
1433 * in 'match->key'. We cannot simply memset
1434 * 'match->mask', because padding bytes and fields not
1435 * specified in 'match->key' should be left to 0.
1436 * Instead, we use a stream of netlink attributes,
1437 * copied from 'key' and set to 0xff.
1438 * ovs_key_from_nlattrs() will take care of filling
1439 * 'match->mask' appropriately.
1440 */
1441 newmask = kmemdup(nla_key,
1442 nla_total_size(nla_len(nla_key)),
1443 GFP_KERNEL);
1444 if (!newmask)
1445 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001446
Pravin B Shelara85311b2014-10-19 12:03:40 -07001447 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001448
Pravin B Shelara85311b2014-10-19 12:03:40 -07001449 /* The userspace does not send tunnel attributes that
1450 * are 0, but we should not wildcard them nonetheless.
1451 */
Jiri Benc00a93ba2015-10-05 13:09:46 +02001452 if (match->key->tun_proto)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001453 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1454 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001455
Pravin B Shelara85311b2014-10-19 12:03:40 -07001456 nla_mask = newmask;
1457 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001458
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001459 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001460 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001461 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001462
Pravin B Shelara85311b2014-10-19 12:03:40 -07001463 /* Always match on tci. */
Eric Garver018c1dd2016-09-07 12:56:59 -04001464 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1465 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
Pravin B Shelara85311b2014-10-19 12:03:40 -07001466
Eric Garver018c1dd2016-09-07 12:56:59 -04001467 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1468 if (err)
1469 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001470
Joe Stringerc2ac6672015-08-26 11:31:52 -07001471 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1472 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001473 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001474 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001475 }
1476
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001477 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001478 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001479
Pravin B Shelarf47de062014-10-16 21:55:45 -07001480free_newmask:
1481 kfree(newmask);
1482 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001483}
1484
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001485static size_t get_ufid_len(const struct nlattr *attr, bool log)
1486{
1487 size_t len;
1488
1489 if (!attr)
1490 return 0;
1491
1492 len = nla_len(attr);
1493 if (len < 1 || len > MAX_UFID_LENGTH) {
1494 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1495 nla_len(attr), MAX_UFID_LENGTH);
1496 return 0;
1497 }
1498
1499 return len;
1500}
1501
1502/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1503 * or false otherwise.
1504 */
1505bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1506 bool log)
1507{
1508 sfid->ufid_len = get_ufid_len(attr, log);
1509 if (sfid->ufid_len)
1510 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1511
1512 return sfid->ufid_len;
1513}
1514
1515int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1516 const struct sw_flow_key *key, bool log)
1517{
1518 struct sw_flow_key *new_key;
1519
1520 if (ovs_nla_get_ufid(sfid, ufid, log))
1521 return 0;
1522
1523 /* If UFID was not provided, use unmasked key. */
1524 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1525 if (!new_key)
1526 return -ENOMEM;
1527 memcpy(new_key, key, sizeof(*key));
1528 sfid->unmasked_key = new_key;
1529
1530 return 0;
1531}
1532
1533u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1534{
1535 return attr ? nla_get_u32(attr) : 0;
1536}
1537
Pravin B Shelare6445712013-10-03 18:16:47 -07001538/**
1539 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001540 * @net: Network namespace.
1541 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1542 * metadata.
1543 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1544 * attributes.
1545 * @attrs: Bit mask for the netlink attributes included in @a.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001546 * @log: Boolean to allow kernel error logging. Normally true, but when
1547 * probing for feature compatibility this should be passed in as false to
1548 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001549 *
1550 * This parses a series of Netlink attributes that form a flow key, which must
1551 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1552 * get the metadata, that is, the parts of the flow key that cannot be
1553 * extracted from the packet itself.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001554 *
1555 * This must be called before the packet key fields are filled in 'key'.
Pravin B Shelare6445712013-10-03 18:16:47 -07001556 */
1557
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001558int ovs_nla_get_flow_metadata(struct net *net,
1559 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1560 u64 attrs, struct sw_flow_key *key, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001561{
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001562 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001563
1564 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001565 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001566
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001567 key->ct_state = 0;
1568 key->ct_zone = 0;
1569 key->ct_orig_proto = 0;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001570 memset(&key->ct, 0, sizeof(key->ct));
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001571 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1572 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1573
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001574 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001575
Joe Stringerc2ac6672015-08-26 11:31:52 -07001576 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001577}
1578
Eric Garver018c1dd2016-09-07 12:56:59 -04001579static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1580 bool is_mask)
1581{
1582 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1583
1584 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1585 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1586 return -EMSGSIZE;
1587 return 0;
1588}
1589
Joe Stringer5b4237b2015-01-21 16:42:48 -08001590static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1591 const struct sw_flow_key *output, bool is_mask,
1592 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001593{
1594 struct ovs_key_ethernet *eth_key;
Eric Garver018c1dd2016-09-07 12:56:59 -04001595 struct nlattr *nla;
1596 struct nlattr *encap = NULL;
1597 struct nlattr *in_encap = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001598
Andy Zhou971427f32014-09-15 19:37:25 -07001599 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1600 goto nla_put_failure;
1601
1602 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1603 goto nla_put_failure;
1604
Pravin B Shelare6445712013-10-03 18:16:47 -07001605 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1606 goto nla_put_failure;
1607
Jiri Benc00a93ba2015-10-05 13:09:46 +02001608 if ((swkey->tun_proto || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001609 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001610
1611 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001612 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001613
Jiri Benc6b26ba32015-10-05 13:09:47 +02001614 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
1615 swkey->tun_opts_len, swkey->tun_proto))
Jesse Grossf5796682014-10-03 15:35:33 -07001616 goto nla_put_failure;
1617 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001618
1619 if (swkey->phy.in_port == DP_MAX_PORTS) {
1620 if (is_mask && (output->phy.in_port == 0xffff))
1621 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1622 goto nla_put_failure;
1623 } else {
1624 u16 upper_u16;
1625 upper_u16 = !is_mask ? 0 : 0xffff;
1626
1627 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1628 (upper_u16 << 16) | output->phy.in_port))
1629 goto nla_put_failure;
1630 }
1631
1632 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1633 goto nla_put_failure;
1634
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001635 if (ovs_ct_put_key(swkey, output, skb))
Joe Stringer7f8a4362015-08-26 11:31:48 -07001636 goto nla_put_failure;
1637
Jiri Benc0a6410f2016-11-10 16:28:22 +01001638 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
1639 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1640 if (!nla)
Pravin B Shelare6445712013-10-03 18:16:47 -07001641 goto nla_put_failure;
Eric Garver018c1dd2016-09-07 12:56:59 -04001642
Jiri Benc0a6410f2016-11-10 16:28:22 +01001643 eth_key = nla_data(nla);
1644 ether_addr_copy(eth_key->eth_src, output->eth.src);
1645 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1646
1647 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
1648 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
Eric Garver018c1dd2016-09-07 12:56:59 -04001649 goto nla_put_failure;
Jiri Benc0a6410f2016-11-10 16:28:22 +01001650 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1651 if (!swkey->eth.vlan.tci)
Eric Garver018c1dd2016-09-07 12:56:59 -04001652 goto unencap;
Pravin B Shelare6445712013-10-03 18:16:47 -07001653
Jiri Benc0a6410f2016-11-10 16:28:22 +01001654 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
1655 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
1656 goto nla_put_failure;
1657 in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1658 if (!swkey->eth.cvlan.tci)
1659 goto unencap;
1660 }
1661 }
1662
1663 if (swkey->eth.type == htons(ETH_P_802_2)) {
1664 /*
1665 * Ethertype 802.2 is represented in the netlink with omitted
1666 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1667 * 0xffff in the mask attribute. Ethertype can also
1668 * be wildcarded.
1669 */
1670 if (is_mask && output->eth.type)
1671 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1672 output->eth.type))
1673 goto nla_put_failure;
1674 goto unencap;
1675 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001676 }
1677
1678 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1679 goto nla_put_failure;
1680
Eric Garver018c1dd2016-09-07 12:56:59 -04001681 if (eth_type_vlan(swkey->eth.type)) {
1682 /* There are 3 VLAN tags, we don't know anything about the rest
1683 * of the packet, so truncate here.
1684 */
1685 WARN_ON_ONCE(!(encap && in_encap));
1686 goto unencap;
1687 }
1688
Pravin B Shelare6445712013-10-03 18:16:47 -07001689 if (swkey->eth.type == htons(ETH_P_IP)) {
1690 struct ovs_key_ipv4 *ipv4_key;
1691
1692 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1693 if (!nla)
1694 goto nla_put_failure;
1695 ipv4_key = nla_data(nla);
1696 ipv4_key->ipv4_src = output->ipv4.addr.src;
1697 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1698 ipv4_key->ipv4_proto = output->ip.proto;
1699 ipv4_key->ipv4_tos = output->ip.tos;
1700 ipv4_key->ipv4_ttl = output->ip.ttl;
1701 ipv4_key->ipv4_frag = output->ip.frag;
1702 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1703 struct ovs_key_ipv6 *ipv6_key;
1704
1705 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1706 if (!nla)
1707 goto nla_put_failure;
1708 ipv6_key = nla_data(nla);
1709 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1710 sizeof(ipv6_key->ipv6_src));
1711 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1712 sizeof(ipv6_key->ipv6_dst));
1713 ipv6_key->ipv6_label = output->ipv6.label;
1714 ipv6_key->ipv6_proto = output->ip.proto;
1715 ipv6_key->ipv6_tclass = output->ip.tos;
1716 ipv6_key->ipv6_hlimit = output->ip.ttl;
1717 ipv6_key->ipv6_frag = output->ip.frag;
1718 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1719 swkey->eth.type == htons(ETH_P_RARP)) {
1720 struct ovs_key_arp *arp_key;
1721
1722 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1723 if (!nla)
1724 goto nla_put_failure;
1725 arp_key = nla_data(nla);
1726 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1727 arp_key->arp_sip = output->ipv4.addr.src;
1728 arp_key->arp_tip = output->ipv4.addr.dst;
1729 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001730 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1731 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001732 } else if (eth_p_mpls(swkey->eth.type)) {
1733 struct ovs_key_mpls *mpls_key;
1734
1735 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1736 if (!nla)
1737 goto nla_put_failure;
1738 mpls_key = nla_data(nla);
1739 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001740 }
1741
1742 if ((swkey->eth.type == htons(ETH_P_IP) ||
1743 swkey->eth.type == htons(ETH_P_IPV6)) &&
1744 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1745
1746 if (swkey->ip.proto == IPPROTO_TCP) {
1747 struct ovs_key_tcp *tcp_key;
1748
1749 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1750 if (!nla)
1751 goto nla_put_failure;
1752 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001753 tcp_key->tcp_src = output->tp.src;
1754 tcp_key->tcp_dst = output->tp.dst;
1755 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1756 output->tp.flags))
1757 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001758 } else if (swkey->ip.proto == IPPROTO_UDP) {
1759 struct ovs_key_udp *udp_key;
1760
1761 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1762 if (!nla)
1763 goto nla_put_failure;
1764 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001765 udp_key->udp_src = output->tp.src;
1766 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001767 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1768 struct ovs_key_sctp *sctp_key;
1769
1770 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1771 if (!nla)
1772 goto nla_put_failure;
1773 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001774 sctp_key->sctp_src = output->tp.src;
1775 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001776 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1777 swkey->ip.proto == IPPROTO_ICMP) {
1778 struct ovs_key_icmp *icmp_key;
1779
1780 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1781 if (!nla)
1782 goto nla_put_failure;
1783 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001784 icmp_key->icmp_type = ntohs(output->tp.src);
1785 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001786 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1787 swkey->ip.proto == IPPROTO_ICMPV6) {
1788 struct ovs_key_icmpv6 *icmpv6_key;
1789
1790 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1791 sizeof(*icmpv6_key));
1792 if (!nla)
1793 goto nla_put_failure;
1794 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001795 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1796 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001797
1798 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1799 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1800 struct ovs_key_nd *nd_key;
1801
1802 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1803 if (!nla)
1804 goto nla_put_failure;
1805 nd_key = nla_data(nla);
1806 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1807 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001808 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1809 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001810 }
1811 }
1812 }
1813
1814unencap:
Eric Garver018c1dd2016-09-07 12:56:59 -04001815 if (in_encap)
1816 nla_nest_end(skb, in_encap);
Pravin B Shelare6445712013-10-03 18:16:47 -07001817 if (encap)
1818 nla_nest_end(skb, encap);
1819
1820 return 0;
1821
1822nla_put_failure:
1823 return -EMSGSIZE;
1824}
1825
Joe Stringer5b4237b2015-01-21 16:42:48 -08001826int ovs_nla_put_key(const struct sw_flow_key *swkey,
1827 const struct sw_flow_key *output, int attr, bool is_mask,
1828 struct sk_buff *skb)
1829{
1830 int err;
1831 struct nlattr *nla;
1832
1833 nla = nla_nest_start(skb, attr);
1834 if (!nla)
1835 return -EMSGSIZE;
1836 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1837 if (err)
1838 return err;
1839 nla_nest_end(skb, nla);
1840
1841 return 0;
1842}
1843
1844/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001845int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08001846{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001847 if (ovs_identifier_is_ufid(&flow->id))
1848 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1849 flow->id.ufid);
1850
1851 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1852 OVS_FLOW_ATTR_KEY, false, skb);
1853}
1854
1855/* Called with ovs_mutex or RCU read lock. */
1856int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1857{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08001858 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08001859 OVS_FLOW_ATTR_KEY, false, skb);
1860}
1861
1862/* Called with ovs_mutex or RCU read lock. */
1863int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1864{
1865 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1866 OVS_FLOW_ATTR_MASK, true, skb);
1867}
1868
Pravin B Shelare6445712013-10-03 18:16:47 -07001869#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1870
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001871static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001872{
1873 struct sw_flow_actions *sfa;
1874
Jesse Gross426cda52014-10-06 05:08:38 -07001875 if (size > MAX_ACTIONS_BUFSIZE) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001876 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001877 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001878 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001879
1880 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1881 if (!sfa)
1882 return ERR_PTR(-ENOMEM);
1883
1884 sfa->actions_len = 0;
1885 return sfa;
1886}
1887
Thomas Graf34ae9322015-07-21 10:44:03 +02001888static void ovs_nla_free_set_action(const struct nlattr *a)
1889{
1890 const struct nlattr *ovs_key = nla_data(a);
1891 struct ovs_tunnel_info *ovs_tun;
1892
1893 switch (nla_type(ovs_key)) {
1894 case OVS_KEY_ATTR_TUNNEL_INFO:
1895 ovs_tun = nla_data(ovs_key);
1896 dst_release((struct dst_entry *)ovs_tun->tun_dst);
1897 break;
1898 }
1899}
1900
Pravin B Shelare6445712013-10-03 18:16:47 -07001901void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1902{
Thomas Graf34ae9322015-07-21 10:44:03 +02001903 const struct nlattr *a;
1904 int rem;
1905
1906 if (!sf_acts)
1907 return;
1908
1909 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1910 switch (nla_type(a)) {
1911 case OVS_ACTION_ATTR_SET:
1912 ovs_nla_free_set_action(a);
1913 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001914 case OVS_ACTION_ATTR_CT:
1915 ovs_ct_free_action(a);
1916 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02001917 }
1918 }
1919
1920 kfree(sf_acts);
1921}
1922
1923static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1924{
1925 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1926}
1927
1928/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1929 * The caller must hold rcu_read_lock for this to be sensible. */
1930void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
1931{
1932 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07001933}
1934
1935static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001936 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001937{
1938
1939 struct sw_flow_actions *acts;
1940 int new_acts_size;
1941 int req_size = NLA_ALIGN(attr_len);
1942 int next_offset = offsetof(struct sw_flow_actions, actions) +
1943 (*sfa)->actions_len;
1944
1945 if (req_size <= (ksize(*sfa) - next_offset))
1946 goto out;
1947
1948 new_acts_size = ksize(*sfa) * 2;
1949
1950 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1951 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1952 return ERR_PTR(-EMSGSIZE);
1953 new_acts_size = MAX_ACTIONS_BUFSIZE;
1954 }
1955
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001956 acts = nla_alloc_flow_actions(new_acts_size, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001957 if (IS_ERR(acts))
1958 return (void *)acts;
1959
1960 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1961 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07001962 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001963 kfree(*sfa);
1964 *sfa = acts;
1965
1966out:
1967 (*sfa)->actions_len += req_size;
1968 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1969}
1970
Jesse Grossf0b128c2014-10-03 15:35:31 -07001971static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001972 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001973{
1974 struct nlattr *a;
1975
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001976 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001977 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07001978 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07001979
1980 a->nla_type = attrtype;
1981 a->nla_len = nla_attr_size(len);
1982
1983 if (data)
1984 memcpy(nla_data(a), data, len);
1985 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1986
Jesse Grossf0b128c2014-10-03 15:35:31 -07001987 return a;
1988}
1989
Joe Stringer7f8a4362015-08-26 11:31:48 -07001990int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
1991 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07001992{
1993 struct nlattr *a;
1994
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001995 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07001996
Fabian Frederickf35423c12014-11-14 19:32:58 +01001997 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07001998}
1999
2000static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002001 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002002{
2003 int used = (*sfa)->actions_len;
2004 int err;
2005
Joe Stringer7f8a4362015-08-26 11:31:48 -07002006 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002007 if (err)
2008 return err;
2009
2010 return used;
2011}
2012
2013static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2014 int st_offset)
2015{
2016 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2017 st_offset);
2018
2019 a->nla_len = sfa->actions_len - st_offset;
2020}
2021
Joe Stringer7f8a4362015-08-26 11:31:48 -07002022static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002023 const struct sw_flow_key *key,
2024 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002025 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002026
Joe Stringer7f8a4362015-08-26 11:31:48 -07002027static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
Pravin B Shelare6445712013-10-03 18:16:47 -07002028 const struct sw_flow_key *key, int depth,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002029 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002030 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002031{
2032 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2033 const struct nlattr *probability, *actions;
2034 const struct nlattr *a;
2035 int rem, start, err, st_acts;
2036
2037 memset(attrs, 0, sizeof(attrs));
2038 nla_for_each_nested(a, attr, rem) {
2039 int type = nla_type(a);
2040 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2041 return -EINVAL;
2042 attrs[type] = a;
2043 }
2044 if (rem)
2045 return -EINVAL;
2046
2047 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2048 if (!probability || nla_len(probability) != sizeof(u32))
2049 return -EINVAL;
2050
2051 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2052 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2053 return -EINVAL;
2054
2055 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002056 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002057 if (start < 0)
2058 return start;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002059 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
2060 nla_data(probability), sizeof(u32), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002061 if (err)
2062 return err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002063 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002064 if (st_acts < 0)
2065 return st_acts;
2066
Joe Stringer7f8a4362015-08-26 11:31:48 -07002067 err = __ovs_nla_copy_actions(net, actions, key, depth + 1, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002068 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002069 if (err)
2070 return err;
2071
2072 add_nested_action_end(*sfa, st_acts);
2073 add_nested_action_end(*sfa, start);
2074
2075 return 0;
2076}
2077
Pravin B Shelare6445712013-10-03 18:16:47 -07002078void ovs_match_init(struct sw_flow_match *match,
2079 struct sw_flow_key *key,
pravin shelar22799942016-09-19 13:51:00 -07002080 bool reset_key,
Pravin B Shelare6445712013-10-03 18:16:47 -07002081 struct sw_flow_mask *mask)
2082{
2083 memset(match, 0, sizeof(*match));
2084 match->key = key;
2085 match->mask = mask;
2086
pravin shelar22799942016-09-19 13:51:00 -07002087 if (reset_key)
2088 memset(key, 0, sizeof(*key));
Pravin B Shelare6445712013-10-03 18:16:47 -07002089
2090 if (mask) {
2091 memset(&mask->key, 0, sizeof(mask->key));
2092 mask->range.start = mask->range.end = 0;
2093 }
2094}
2095
Thomas Grafd91641d2015-01-15 03:53:57 +01002096static int validate_geneve_opts(struct sw_flow_key *key)
2097{
2098 struct geneve_opt *option;
2099 int opts_len = key->tun_opts_len;
2100 bool crit_opt = false;
2101
2102 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2103 while (opts_len > 0) {
2104 int len;
2105
2106 if (opts_len < sizeof(*option))
2107 return -EINVAL;
2108
2109 len = sizeof(*option) + option->length * 4;
2110 if (len > opts_len)
2111 return -EINVAL;
2112
2113 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2114
2115 option = (struct geneve_opt *)((u8 *)option + len);
2116 opts_len -= len;
2117 };
2118
2119 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2120
2121 return 0;
2122}
2123
Pravin B Shelare6445712013-10-03 18:16:47 -07002124static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002125 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002126{
2127 struct sw_flow_match match;
2128 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02002129 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002130 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02002131 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002132 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01002133 int err = 0, start, opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002134
pravin shelar22799942016-09-19 13:51:00 -07002135 ovs_match_init(&match, &key, true, NULL);
Jiri Benc6b26ba32015-10-05 13:09:47 +02002136 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
Thomas Graf1dd144c2015-01-15 03:53:59 +01002137 if (opts_type < 0)
2138 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002139
Jesse Grossf5796682014-10-03 15:35:33 -07002140 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01002141 switch (opts_type) {
2142 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2143 err = validate_geneve_opts(&key);
2144 if (err < 0)
2145 return err;
2146 break;
2147 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2148 break;
2149 }
Jesse Grossf5796682014-10-03 15:35:33 -07002150 };
2151
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002152 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002153 if (start < 0)
2154 return start;
2155
Thomas Graf34ae9322015-07-21 10:44:03 +02002156 tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
2157 if (!tun_dst)
2158 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002159
Paolo Abenid71785f2016-02-12 15:43:57 +01002160 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2161 if (err) {
2162 dst_release((struct dst_entry *)tun_dst);
2163 return err;
2164 }
2165
Thomas Graf34ae9322015-07-21 10:44:03 +02002166 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
2167 sizeof(*ovs_tun), log);
2168 if (IS_ERR(a)) {
2169 dst_release((struct dst_entry *)tun_dst);
2170 return PTR_ERR(a);
2171 }
2172
2173 ovs_tun = nla_data(a);
2174 ovs_tun->tun_dst = tun_dst;
2175
2176 tun_info = &tun_dst->u.tun_info;
2177 tun_info->mode = IP_TUNNEL_INFO_TX;
Jiri Benc00a93ba2015-10-05 13:09:46 +02002178 if (key.tun_proto == AF_INET6)
2179 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002180 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07002181
Pravin B Shelar4c222792015-08-30 18:09:38 -07002182 /* We need to store the options in the action itself since
2183 * everything else will go away after flow setup. We can append
2184 * it to tun_info and then point there.
2185 */
2186 ip_tunnel_info_opts_set(tun_info,
2187 TUN_METADATA_OPTS(&key, key.tun_opts_len),
2188 key.tun_opts_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07002189 add_nested_action_end(*sfa, start);
2190
2191 return err;
2192}
2193
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002194/* Return false if there are any non-masked bits set.
2195 * Mask follows data immediately, before any netlink padding.
2196 */
2197static bool validate_masked(u8 *data, int len)
2198{
2199 u8 *mask = data + len;
2200
2201 while (len--)
2202 if (*data++ & ~*mask++)
2203 return false;
2204
2205 return true;
2206}
2207
Pravin B Shelare6445712013-10-03 18:16:47 -07002208static int validate_set(const struct nlattr *a,
2209 const struct sw_flow_key *flow_key,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002210 struct sw_flow_actions **sfa, bool *skip_copy,
2211 u8 mac_proto, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002212{
2213 const struct nlattr *ovs_key = nla_data(a);
2214 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002215 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002216
2217 /* There can be only one key in a action */
2218 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2219 return -EINVAL;
2220
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002221 key_len = nla_len(ovs_key);
2222 if (masked)
2223 key_len /= 2;
2224
Pravin B Shelare6445712013-10-03 18:16:47 -07002225 if (key_type > OVS_KEY_ATTR_MAX ||
Jesse Gross982b5272015-09-11 18:38:28 -07002226 !check_attr_len(key_len, ovs_key_lens[key_type].len))
Pravin B Shelare6445712013-10-03 18:16:47 -07002227 return -EINVAL;
2228
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002229 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2230 return -EINVAL;
2231
Pravin B Shelare6445712013-10-03 18:16:47 -07002232 switch (key_type) {
2233 const struct ovs_key_ipv4 *ipv4_key;
2234 const struct ovs_key_ipv6 *ipv6_key;
2235 int err;
2236
2237 case OVS_KEY_ATTR_PRIORITY:
2238 case OVS_KEY_ATTR_SKB_MARK:
Joe Stringer182e3042015-08-26 11:31:49 -07002239 case OVS_KEY_ATTR_CT_MARK:
Joe Stringer33db4122015-10-01 15:00:37 -07002240 case OVS_KEY_ATTR_CT_LABELS:
Pravin B Shelare6445712013-10-03 18:16:47 -07002241 break;
2242
Jiri Benc0a6410f2016-11-10 16:28:22 +01002243 case OVS_KEY_ATTR_ETHERNET:
2244 if (mac_proto != MAC_PROTO_ETHERNET)
2245 return -EINVAL;
Jarno Rajahalme87e159c2016-12-19 17:06:33 -08002246 break;
Jiri Benc0a6410f2016-11-10 16:28:22 +01002247
Pravin B Shelare6445712013-10-03 18:16:47 -07002248 case OVS_KEY_ATTR_TUNNEL:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002249 if (masked)
2250 return -EINVAL; /* Masked tunnel set not supported. */
2251
2252 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002253 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002254 if (err)
2255 return err;
2256 break;
2257
2258 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002259 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07002260 return -EINVAL;
2261
Pravin B Shelare6445712013-10-03 18:16:47 -07002262 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002263
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002264 if (masked) {
2265 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002266
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002267 /* Non-writeable fields. */
2268 if (mask->ipv4_proto || mask->ipv4_frag)
2269 return -EINVAL;
2270 } else {
2271 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2272 return -EINVAL;
2273
2274 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2275 return -EINVAL;
2276 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002277 break;
2278
2279 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002280 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07002281 return -EINVAL;
2282
Pravin B Shelare6445712013-10-03 18:16:47 -07002283 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002284
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002285 if (masked) {
2286 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002287
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002288 /* Non-writeable fields. */
2289 if (mask->ipv6_proto || mask->ipv6_frag)
2290 return -EINVAL;
2291
2292 /* Invalid bits in the flow label mask? */
2293 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2294 return -EINVAL;
2295 } else {
2296 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2297 return -EINVAL;
2298
2299 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2300 return -EINVAL;
2301 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002302 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2303 return -EINVAL;
2304
2305 break;
2306
2307 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002308 if ((eth_type != htons(ETH_P_IP) &&
2309 eth_type != htons(ETH_P_IPV6)) ||
2310 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002311 return -EINVAL;
2312
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002313 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002314
2315 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002316 if ((eth_type != htons(ETH_P_IP) &&
2317 eth_type != htons(ETH_P_IPV6)) ||
2318 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002319 return -EINVAL;
2320
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002321 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002322
2323 case OVS_KEY_ATTR_MPLS:
2324 if (!eth_p_mpls(eth_type))
2325 return -EINVAL;
2326 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002327
2328 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002329 if ((eth_type != htons(ETH_P_IP) &&
2330 eth_type != htons(ETH_P_IPV6)) ||
2331 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002332 return -EINVAL;
2333
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002334 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002335
2336 default:
2337 return -EINVAL;
2338 }
2339
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002340 /* Convert non-masked non-tunnel set actions to masked set actions. */
2341 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2342 int start, len = key_len * 2;
2343 struct nlattr *at;
2344
2345 *skip_copy = true;
2346
2347 start = add_nested_action_start(sfa,
2348 OVS_ACTION_ATTR_SET_TO_MASKED,
2349 log);
2350 if (start < 0)
2351 return start;
2352
2353 at = __add_action(sfa, key_type, NULL, len, log);
2354 if (IS_ERR(at))
2355 return PTR_ERR(at);
2356
2357 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2358 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2359 /* Clear non-writeable bits from otherwise writeable fields. */
2360 if (key_type == OVS_KEY_ATTR_IPV6) {
2361 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2362
2363 mask->ipv6_label &= htonl(0x000FFFFF);
2364 }
2365 add_nested_action_end(*sfa, start);
2366 }
2367
Pravin B Shelare6445712013-10-03 18:16:47 -07002368 return 0;
2369}
2370
2371static int validate_userspace(const struct nlattr *attr)
2372{
2373 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2374 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2375 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002376 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002377 };
2378 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2379 int error;
2380
2381 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
2382 attr, userspace_policy);
2383 if (error)
2384 return error;
2385
2386 if (!a[OVS_USERSPACE_ATTR_PID] ||
2387 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2388 return -EINVAL;
2389
2390 return 0;
2391}
2392
2393static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002394 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002395{
2396 int totlen = NLA_ALIGN(from->nla_len);
2397 struct nlattr *to;
2398
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002399 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002400 if (IS_ERR(to))
2401 return PTR_ERR(to);
2402
2403 memcpy(to, from, totlen);
2404 return 0;
2405}
2406
Joe Stringer7f8a4362015-08-26 11:31:48 -07002407static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002408 const struct sw_flow_key *key,
2409 int depth, struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002410 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002411{
Jiri Benc0a6410f2016-11-10 16:28:22 +01002412 u8 mac_proto = ovs_key_mac_proto(key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002413 const struct nlattr *a;
2414 int rem, err;
2415
2416 if (depth >= SAMPLE_ACTION_DEPTH)
2417 return -EOVERFLOW;
2418
2419 nla_for_each_nested(a, attr, rem) {
2420 /* Expected argument lengths, (u32)-1 for variable length. */
2421 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2422 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002423 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002424 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002425 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2426 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002427 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2428 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2429 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002430 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002431 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002432 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2433 [OVS_ACTION_ATTR_CT] = (u32)-1,
William Tuf2a4d082016-06-10 11:49:33 -07002434 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
Jiri Benc91820da2016-11-10 16:28:23 +01002435 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
2436 [OVS_ACTION_ATTR_POP_ETH] = 0,
Pravin B Shelare6445712013-10-03 18:16:47 -07002437 };
2438 const struct ovs_action_push_vlan *vlan;
2439 int type = nla_type(a);
2440 bool skip_copy;
2441
2442 if (type > OVS_ACTION_ATTR_MAX ||
2443 (action_lens[type] != nla_len(a) &&
2444 action_lens[type] != (u32)-1))
2445 return -EINVAL;
2446
2447 skip_copy = false;
2448 switch (type) {
2449 case OVS_ACTION_ATTR_UNSPEC:
2450 return -EINVAL;
2451
2452 case OVS_ACTION_ATTR_USERSPACE:
2453 err = validate_userspace(a);
2454 if (err)
2455 return err;
2456 break;
2457
2458 case OVS_ACTION_ATTR_OUTPUT:
2459 if (nla_get_u32(a) >= DP_MAX_PORTS)
2460 return -EINVAL;
2461 break;
2462
William Tuf2a4d082016-06-10 11:49:33 -07002463 case OVS_ACTION_ATTR_TRUNC: {
2464 const struct ovs_action_trunc *trunc = nla_data(a);
2465
2466 if (trunc->max_len < ETH_HLEN)
2467 return -EINVAL;
2468 break;
2469 }
2470
Andy Zhou971427f32014-09-15 19:37:25 -07002471 case OVS_ACTION_ATTR_HASH: {
2472 const struct ovs_action_hash *act_hash = nla_data(a);
2473
2474 switch (act_hash->hash_alg) {
2475 case OVS_HASH_ALG_L4:
2476 break;
2477 default:
2478 return -EINVAL;
2479 }
2480
2481 break;
2482 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002483
2484 case OVS_ACTION_ATTR_POP_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01002485 if (mac_proto != MAC_PROTO_ETHERNET)
2486 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002487 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07002488 break;
2489
2490 case OVS_ACTION_ATTR_PUSH_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01002491 if (mac_proto != MAC_PROTO_ETHERNET)
2492 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07002493 vlan = nla_data(a);
Eric Garver018c1dd2016-09-07 12:56:59 -04002494 if (!eth_type_vlan(vlan->vlan_tpid))
Pravin B Shelare6445712013-10-03 18:16:47 -07002495 return -EINVAL;
2496 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2497 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002498 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07002499 break;
2500
Andy Zhou971427f32014-09-15 19:37:25 -07002501 case OVS_ACTION_ATTR_RECIRC:
2502 break;
2503
Simon Horman25cd9ba2014-10-06 05:05:13 -07002504 case OVS_ACTION_ATTR_PUSH_MPLS: {
2505 const struct ovs_action_push_mpls *mpls = nla_data(a);
2506
Simon Horman25cd9ba2014-10-06 05:05:13 -07002507 if (!eth_p_mpls(mpls->mpls_ethertype))
2508 return -EINVAL;
2509 /* Prohibit push MPLS other than to a white list
2510 * for packets that have a known tag order.
2511 */
2512 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2513 (eth_type != htons(ETH_P_IP) &&
2514 eth_type != htons(ETH_P_IPV6) &&
2515 eth_type != htons(ETH_P_ARP) &&
2516 eth_type != htons(ETH_P_RARP) &&
2517 !eth_p_mpls(eth_type)))
2518 return -EINVAL;
2519 eth_type = mpls->mpls_ethertype;
2520 break;
2521 }
2522
2523 case OVS_ACTION_ATTR_POP_MPLS:
2524 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2525 !eth_p_mpls(eth_type))
2526 return -EINVAL;
2527
2528 /* Disallow subsequent L2.5+ set and mpls_pop actions
2529 * as there is no check here to ensure that the new
2530 * eth_type is valid and thus set actions could
2531 * write off the end of the packet or otherwise
2532 * corrupt it.
2533 *
2534 * Support for these actions is planned using packet
2535 * recirculation.
2536 */
2537 eth_type = htons(0);
2538 break;
2539
Pravin B Shelare6445712013-10-03 18:16:47 -07002540 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002541 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002542 &skip_copy, mac_proto, eth_type,
2543 false, log);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002544 if (err)
2545 return err;
2546 break;
2547
2548 case OVS_ACTION_ATTR_SET_MASKED:
2549 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002550 &skip_copy, mac_proto, eth_type,
2551 true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002552 if (err)
2553 return err;
2554 break;
2555
2556 case OVS_ACTION_ATTR_SAMPLE:
Joe Stringer7f8a4362015-08-26 11:31:48 -07002557 err = validate_and_copy_sample(net, a, key, depth, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002558 eth_type, vlan_tci, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002559 if (err)
2560 return err;
2561 skip_copy = true;
2562 break;
2563
Joe Stringer7f8a4362015-08-26 11:31:48 -07002564 case OVS_ACTION_ATTR_CT:
2565 err = ovs_ct_copy_action(net, a, key, sfa, log);
2566 if (err)
2567 return err;
2568 skip_copy = true;
2569 break;
2570
Jiri Benc91820da2016-11-10 16:28:23 +01002571 case OVS_ACTION_ATTR_PUSH_ETH:
2572 /* Disallow pushing an Ethernet header if one
2573 * is already present */
2574 if (mac_proto != MAC_PROTO_NONE)
2575 return -EINVAL;
2576 mac_proto = MAC_PROTO_NONE;
2577 break;
2578
2579 case OVS_ACTION_ATTR_POP_ETH:
2580 if (mac_proto != MAC_PROTO_ETHERNET)
2581 return -EINVAL;
2582 if (vlan_tci & htons(VLAN_TAG_PRESENT))
2583 return -EINVAL;
2584 mac_proto = MAC_PROTO_ETHERNET;
2585 break;
2586
Pravin B Shelare6445712013-10-03 18:16:47 -07002587 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002588 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002589 return -EINVAL;
2590 }
2591 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002592 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002593 if (err)
2594 return err;
2595 }
2596 }
2597
2598 if (rem > 0)
2599 return -EINVAL;
2600
2601 return 0;
2602}
2603
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002604/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07002605int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002606 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002607 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07002608{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002609 int err;
2610
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002611 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002612 if (IS_ERR(*sfa))
2613 return PTR_ERR(*sfa);
2614
Joe Stringer8e2fed12015-08-26 11:31:44 -07002615 (*sfa)->orig_len = nla_len(attr);
Joe Stringer7f8a4362015-08-26 11:31:48 -07002616 err = __ovs_nla_copy_actions(net, attr, key, 0, sfa, key->eth.type,
Eric Garver018c1dd2016-09-07 12:56:59 -04002617 key->eth.vlan.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002618 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02002619 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002620
2621 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002622}
2623
Pravin B Shelare6445712013-10-03 18:16:47 -07002624static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
2625{
2626 const struct nlattr *a;
2627 struct nlattr *start;
2628 int err = 0, rem;
2629
2630 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2631 if (!start)
2632 return -EMSGSIZE;
2633
2634 nla_for_each_nested(a, attr, rem) {
2635 int type = nla_type(a);
2636 struct nlattr *st_sample;
2637
2638 switch (type) {
2639 case OVS_SAMPLE_ATTR_PROBABILITY:
2640 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
2641 sizeof(u32), nla_data(a)))
2642 return -EMSGSIZE;
2643 break;
2644 case OVS_SAMPLE_ATTR_ACTIONS:
2645 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2646 if (!st_sample)
2647 return -EMSGSIZE;
2648 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
2649 if (err)
2650 return err;
2651 nla_nest_end(skb, st_sample);
2652 break;
2653 }
2654 }
2655
2656 nla_nest_end(skb, start);
2657 return err;
2658}
2659
2660static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2661{
2662 const struct nlattr *ovs_key = nla_data(a);
2663 int key_type = nla_type(ovs_key);
2664 struct nlattr *start;
2665 int err;
2666
2667 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07002668 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02002669 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2670 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002671
Pravin B Shelare6445712013-10-03 18:16:47 -07002672 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2673 if (!start)
2674 return -EMSGSIZE;
2675
Simon Hormane905eab2015-12-18 19:43:15 +09002676 err = ip_tun_to_nlattr(skb, &tun_info->key,
2677 ip_tunnel_info_opts(tun_info),
2678 tun_info->options_len,
2679 ip_tunnel_info_af(tun_info));
Pravin B Shelare6445712013-10-03 18:16:47 -07002680 if (err)
2681 return err;
2682 nla_nest_end(skb, start);
2683 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002684 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002685 default:
2686 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2687 return -EMSGSIZE;
2688 break;
2689 }
2690
2691 return 0;
2692}
2693
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002694static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2695 struct sk_buff *skb)
2696{
2697 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08002698 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002699 size_t key_len = nla_len(ovs_key) / 2;
2700
2701 /* Revert the conversion we did from a non-masked set action to
2702 * masked set action.
2703 */
Joe Stringerf4f8e732015-03-02 18:49:56 -08002704 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2705 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002706 return -EMSGSIZE;
2707
Joe Stringerf4f8e732015-03-02 18:49:56 -08002708 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2709 return -EMSGSIZE;
2710
2711 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002712 return 0;
2713}
2714
Pravin B Shelare6445712013-10-03 18:16:47 -07002715int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2716{
2717 const struct nlattr *a;
2718 int rem, err;
2719
2720 nla_for_each_attr(a, attr, len, rem) {
2721 int type = nla_type(a);
2722
2723 switch (type) {
2724 case OVS_ACTION_ATTR_SET:
2725 err = set_action_to_attr(a, skb);
2726 if (err)
2727 return err;
2728 break;
2729
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002730 case OVS_ACTION_ATTR_SET_TO_MASKED:
2731 err = masked_set_action_to_set_action_attr(a, skb);
2732 if (err)
2733 return err;
2734 break;
2735
Pravin B Shelare6445712013-10-03 18:16:47 -07002736 case OVS_ACTION_ATTR_SAMPLE:
2737 err = sample_action_to_attr(a, skb);
2738 if (err)
2739 return err;
2740 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002741
2742 case OVS_ACTION_ATTR_CT:
2743 err = ovs_ct_action_to_attr(nla_data(a), skb);
2744 if (err)
2745 return err;
2746 break;
2747
Pravin B Shelare6445712013-10-03 18:16:47 -07002748 default:
2749 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2750 return -EMSGSIZE;
2751 break;
2752 }
2753 }
2754
2755 return 0;
2756}