blob: 2acfb5af2c45215eea4fe47dbda2d68940ba80b5 [file] [log] [blame]
Pravin B Shelare6445712013-10-03 18:16:47 -07001/*
andy zhou798c1662017-03-20 16:32:29 -07002 * Copyright (c) 2007-2017 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
andy zhou798c1662017-03-20 16:32:29 -070062static bool actions_may_change_flow(const struct nlattr *actions)
63{
64 struct nlattr *nla;
65 int rem;
66
67 nla_for_each_nested(nla, actions, rem) {
68 u16 action = nla_type(nla);
69
70 switch (action) {
71 case OVS_ACTION_ATTR_OUTPUT:
72 case OVS_ACTION_ATTR_RECIRC:
73 case OVS_ACTION_ATTR_TRUNC:
74 case OVS_ACTION_ATTR_USERSPACE:
75 break;
76
77 case OVS_ACTION_ATTR_CT:
78 case OVS_ACTION_ATTR_HASH:
79 case OVS_ACTION_ATTR_POP_ETH:
80 case OVS_ACTION_ATTR_POP_MPLS:
81 case OVS_ACTION_ATTR_POP_VLAN:
82 case OVS_ACTION_ATTR_PUSH_ETH:
83 case OVS_ACTION_ATTR_PUSH_MPLS:
84 case OVS_ACTION_ATTR_PUSH_VLAN:
85 case OVS_ACTION_ATTR_SAMPLE:
86 case OVS_ACTION_ATTR_SET:
87 case OVS_ACTION_ATTR_SET_MASKED:
88 default:
89 return true;
90 }
91 }
92 return false;
93}
94
Pravin B Shelara85311b2014-10-19 12:03:40 -070095static void update_range(struct sw_flow_match *match,
96 size_t offset, size_t size, bool is_mask)
Pravin B Shelare6445712013-10-03 18:16:47 -070097{
Pravin B Shelara85311b2014-10-19 12:03:40 -070098 struct sw_flow_key_range *range;
Pravin B Shelare6445712013-10-03 18:16:47 -070099 size_t start = rounddown(offset, sizeof(long));
100 size_t end = roundup(offset + size, sizeof(long));
101
102 if (!is_mask)
103 range = &match->range;
Pravin B Shelara85311b2014-10-19 12:03:40 -0700104 else
Pravin B Shelare6445712013-10-03 18:16:47 -0700105 range = &match->mask->range;
106
Pravin B Shelare6445712013-10-03 18:16:47 -0700107 if (range->start == range->end) {
108 range->start = start;
109 range->end = end;
110 return;
111 }
112
113 if (range->start > start)
114 range->start = start;
115
116 if (range->end < end)
117 range->end = end;
118}
119
120#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
121 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700122 update_range(match, offsetof(struct sw_flow_key, field), \
123 sizeof((match)->key->field), is_mask); \
124 if (is_mask) \
125 (match)->mask->key.field = value; \
126 else \
Pravin B Shelare6445712013-10-03 18:16:47 -0700127 (match)->key->field = value; \
Pravin B Shelare6445712013-10-03 18:16:47 -0700128 } while (0)
129
Jesse Grossf5796682014-10-03 15:35:33 -0700130#define SW_FLOW_KEY_MEMCPY_OFFSET(match, offset, value_p, len, is_mask) \
131 do { \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700132 update_range(match, offset, len, is_mask); \
Jesse Grossf5796682014-10-03 15:35:33 -0700133 if (is_mask) \
134 memcpy((u8 *)&(match)->mask->key + offset, value_p, \
Pravin B Shelara85311b2014-10-19 12:03:40 -0700135 len); \
Jesse Grossf5796682014-10-03 15:35:33 -0700136 else \
137 memcpy((u8 *)(match)->key + offset, value_p, len); \
Pravin B Shelare6445712013-10-03 18:16:47 -0700138 } while (0)
139
Jesse Grossf5796682014-10-03 15:35:33 -0700140#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
141 SW_FLOW_KEY_MEMCPY_OFFSET(match, offsetof(struct sw_flow_key, field), \
142 value_p, len, is_mask)
143
Pravin B Shelara85311b2014-10-19 12:03:40 -0700144#define SW_FLOW_KEY_MEMSET_FIELD(match, field, value, is_mask) \
145 do { \
146 update_range(match, offsetof(struct sw_flow_key, field), \
147 sizeof((match)->key->field), is_mask); \
148 if (is_mask) \
149 memset((u8 *)&(match)->mask->key.field, value, \
150 sizeof((match)->mask->key.field)); \
151 else \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700152 memset((u8 *)&(match)->key->field, value, \
153 sizeof((match)->key->field)); \
Pravin B Shelarf47de062014-10-16 21:55:45 -0700154 } while (0)
Pravin B Shelare6445712013-10-03 18:16:47 -0700155
156static bool match_validate(const struct sw_flow_match *match,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800157 u64 key_attrs, u64 mask_attrs, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700158{
Jiri Benc0a6410f2016-11-10 16:28:22 +0100159 u64 key_expected = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700160 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
161
162 /* The following mask attributes allowed only if they
163 * pass the validation tests. */
164 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800165 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)
Pravin B Shelare6445712013-10-03 18:16:47 -0700166 | (1 << OVS_KEY_ATTR_IPV6)
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800167 | (1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)
Pravin B Shelare6445712013-10-03 18:16:47 -0700168 | (1 << OVS_KEY_ATTR_TCP)
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700169 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
Pravin B Shelare6445712013-10-03 18:16:47 -0700170 | (1 << OVS_KEY_ATTR_UDP)
171 | (1 << OVS_KEY_ATTR_SCTP)
172 | (1 << OVS_KEY_ATTR_ICMP)
173 | (1 << OVS_KEY_ATTR_ICMPV6)
174 | (1 << OVS_KEY_ATTR_ARP)
Simon Horman25cd9ba2014-10-06 05:05:13 -0700175 | (1 << OVS_KEY_ATTR_ND)
176 | (1 << OVS_KEY_ATTR_MPLS));
Pravin B Shelare6445712013-10-03 18:16:47 -0700177
178 /* Always allowed mask fields. */
179 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
180 | (1 << OVS_KEY_ATTR_IN_PORT)
181 | (1 << OVS_KEY_ATTR_ETHERTYPE));
182
183 /* Check key attributes. */
184 if (match->key->eth.type == htons(ETH_P_ARP)
185 || match->key->eth.type == htons(ETH_P_RARP)) {
186 key_expected |= 1 << OVS_KEY_ATTR_ARP;
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800187 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700188 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
189 }
190
Simon Horman25cd9ba2014-10-06 05:05:13 -0700191 if (eth_p_mpls(match->key->eth.type)) {
192 key_expected |= 1 << OVS_KEY_ATTR_MPLS;
193 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
194 mask_allowed |= 1 << OVS_KEY_ATTR_MPLS;
195 }
196
Pravin B Shelare6445712013-10-03 18:16:47 -0700197 if (match->key->eth.type == htons(ETH_P_IP)) {
198 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800199 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700200 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800201 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4;
202 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700203
204 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
205 if (match->key->ip.proto == IPPROTO_UDP) {
206 key_expected |= 1 << OVS_KEY_ATTR_UDP;
207 if (match->mask && (match->mask->key.ip.proto == 0xff))
208 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
209 }
210
211 if (match->key->ip.proto == IPPROTO_SCTP) {
212 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
213 if (match->mask && (match->mask->key.ip.proto == 0xff))
214 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
215 }
216
217 if (match->key->ip.proto == IPPROTO_TCP) {
218 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700219 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
220 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700221 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700222 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
223 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700224 }
225
226 if (match->key->ip.proto == IPPROTO_ICMP) {
227 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
228 if (match->mask && (match->mask->key.ip.proto == 0xff))
229 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
230 }
231 }
232 }
233
234 if (match->key->eth.type == htons(ETH_P_IPV6)) {
235 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800236 if (match->mask && match->mask->key.eth.type == htons(0xffff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700237 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800238 mask_allowed |= 1 << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6;
239 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700240
241 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
242 if (match->key->ip.proto == IPPROTO_UDP) {
243 key_expected |= 1 << OVS_KEY_ATTR_UDP;
244 if (match->mask && (match->mask->key.ip.proto == 0xff))
245 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
246 }
247
248 if (match->key->ip.proto == IPPROTO_SCTP) {
249 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
250 if (match->mask && (match->mask->key.ip.proto == 0xff))
251 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
252 }
253
254 if (match->key->ip.proto == IPPROTO_TCP) {
255 key_expected |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700256 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
257 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700258 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -0700259 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
260 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700261 }
262
263 if (match->key->ip.proto == IPPROTO_ICMPV6) {
264 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
265 if (match->mask && (match->mask->key.ip.proto == 0xff))
266 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
267
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700268 if (match->key->tp.src ==
Pravin B Shelare6445712013-10-03 18:16:47 -0700269 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
Jarno Rajahalme1139e242014-05-05 09:54:49 -0700270 match->key->tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700271 key_expected |= 1 << OVS_KEY_ATTR_ND;
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800272 /* Original direction conntrack tuple
273 * uses the same space as the ND fields
274 * in the key, so both are not allowed
275 * at the same time.
276 */
277 mask_allowed &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
Pravin B Shelarf2a015172014-11-30 23:04:17 -0800278 if (match->mask && (match->mask->key.tp.src == htons(0xff)))
Pravin B Shelare6445712013-10-03 18:16:47 -0700279 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
280 }
281 }
282 }
283 }
284
285 if ((key_attrs & key_expected) != key_expected) {
286 /* Key attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800287 OVS_NLERR(log, "Missing key (keys=%llx, expected=%llx)",
288 (unsigned long long)key_attrs,
289 (unsigned long long)key_expected);
Pravin B Shelare6445712013-10-03 18:16:47 -0700290 return false;
291 }
292
293 if ((mask_attrs & mask_allowed) != mask_attrs) {
294 /* Mask attributes check failed. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800295 OVS_NLERR(log, "Unexpected mask (mask=%llx, allowed=%llx)",
296 (unsigned long long)mask_attrs,
297 (unsigned long long)mask_allowed);
Pravin B Shelare6445712013-10-03 18:16:47 -0700298 return false;
299 }
300
301 return true;
302}
303
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800304size_t ovs_tun_key_attr_size(void)
305{
306 /* Whenever adding new OVS_TUNNEL_KEY_ FIELDS, we should consider
307 * updating this function.
308 */
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200309 return nla_total_size_64bit(8) /* OVS_TUNNEL_KEY_ATTR_ID */
Jiri Benc6b26ba32015-10-05 13:09:47 +0200310 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_SRC */
311 + nla_total_size(16) /* OVS_TUNNEL_KEY_ATTR_IPV[46]_DST */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800312 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
313 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
314 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
315 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
316 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
317 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
Thomas Graf1dd144c2015-01-15 03:53:59 +0100318 /* OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS is mutually exclusive with
319 * OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS and covered by it.
320 */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800321 + nla_total_size(2) /* OVS_TUNNEL_KEY_ATTR_TP_SRC */
322 + nla_total_size(2); /* OVS_TUNNEL_KEY_ATTR_TP_DST */
323}
324
Joe Stringer41af73e2014-10-18 16:14:14 -0700325size_t ovs_key_attr_size(void)
326{
327 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
328 * updating this function.
329 */
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800330 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 28);
Joe Stringer41af73e2014-10-18 16:14:14 -0700331
332 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
333 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800334 + ovs_tun_key_attr_size()
Joe Stringer41af73e2014-10-18 16:14:14 -0700335 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
336 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
337 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
338 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
Joe Stringerfbccce52015-10-06 11:00:00 -0700339 + nla_total_size(4) /* OVS_KEY_ATTR_CT_STATE */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700340 + nla_total_size(2) /* OVS_KEY_ATTR_CT_ZONE */
Joe Stringer182e3042015-08-26 11:31:49 -0700341 + nla_total_size(4) /* OVS_KEY_ATTR_CT_MARK */
Joe Stringer33db4122015-10-01 15:00:37 -0700342 + nla_total_size(16) /* OVS_KEY_ATTR_CT_LABELS */
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800343 + nla_total_size(40) /* OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6 */
Joe Stringer41af73e2014-10-18 16:14:14 -0700344 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
345 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
346 + nla_total_size(4) /* OVS_KEY_ATTR_VLAN */
347 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
348 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
349 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
350 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
351 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
352}
353
Jesse Gross982b5272015-09-11 18:38:28 -0700354static const struct ovs_len_tbl ovs_vxlan_ext_key_lens[OVS_VXLAN_EXT_MAX + 1] = {
355 [OVS_VXLAN_EXT_GBP] = { .len = sizeof(u32) },
356};
357
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100358static const struct ovs_len_tbl ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
359 [OVS_TUNNEL_KEY_ATTR_ID] = { .len = sizeof(u64) },
360 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = { .len = sizeof(u32) },
361 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = { .len = sizeof(u32) },
362 [OVS_TUNNEL_KEY_ATTR_TOS] = { .len = 1 },
363 [OVS_TUNNEL_KEY_ATTR_TTL] = { .len = 1 },
364 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = { .len = 0 },
365 [OVS_TUNNEL_KEY_ATTR_CSUM] = { .len = 0 },
366 [OVS_TUNNEL_KEY_ATTR_TP_SRC] = { .len = sizeof(u16) },
367 [OVS_TUNNEL_KEY_ATTR_TP_DST] = { .len = sizeof(u16) },
368 [OVS_TUNNEL_KEY_ATTR_OAM] = { .len = 0 },
Jesse Gross982b5272015-09-11 18:38:28 -0700369 [OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS] = { .len = OVS_ATTR_VARIABLE },
370 [OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS] = { .len = OVS_ATTR_NESTED,
371 .next = ovs_vxlan_ext_key_lens },
Jiri Benc6b26ba32015-10-05 13:09:47 +0200372 [OVS_TUNNEL_KEY_ATTR_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
373 [OVS_TUNNEL_KEY_ATTR_IPV6_DST] = { .len = sizeof(struct in6_addr) },
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100374};
375
Pravin B Shelare6445712013-10-03 18:16:47 -0700376/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100377static const struct ovs_len_tbl ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
378 [OVS_KEY_ATTR_ENCAP] = { .len = OVS_ATTR_NESTED },
379 [OVS_KEY_ATTR_PRIORITY] = { .len = sizeof(u32) },
380 [OVS_KEY_ATTR_IN_PORT] = { .len = sizeof(u32) },
381 [OVS_KEY_ATTR_SKB_MARK] = { .len = sizeof(u32) },
382 [OVS_KEY_ATTR_ETHERNET] = { .len = sizeof(struct ovs_key_ethernet) },
383 [OVS_KEY_ATTR_VLAN] = { .len = sizeof(__be16) },
384 [OVS_KEY_ATTR_ETHERTYPE] = { .len = sizeof(__be16) },
385 [OVS_KEY_ATTR_IPV4] = { .len = sizeof(struct ovs_key_ipv4) },
386 [OVS_KEY_ATTR_IPV6] = { .len = sizeof(struct ovs_key_ipv6) },
387 [OVS_KEY_ATTR_TCP] = { .len = sizeof(struct ovs_key_tcp) },
388 [OVS_KEY_ATTR_TCP_FLAGS] = { .len = sizeof(__be16) },
389 [OVS_KEY_ATTR_UDP] = { .len = sizeof(struct ovs_key_udp) },
390 [OVS_KEY_ATTR_SCTP] = { .len = sizeof(struct ovs_key_sctp) },
391 [OVS_KEY_ATTR_ICMP] = { .len = sizeof(struct ovs_key_icmp) },
392 [OVS_KEY_ATTR_ICMPV6] = { .len = sizeof(struct ovs_key_icmpv6) },
393 [OVS_KEY_ATTR_ARP] = { .len = sizeof(struct ovs_key_arp) },
394 [OVS_KEY_ATTR_ND] = { .len = sizeof(struct ovs_key_nd) },
395 [OVS_KEY_ATTR_RECIRC_ID] = { .len = sizeof(u32) },
396 [OVS_KEY_ATTR_DP_HASH] = { .len = sizeof(u32) },
397 [OVS_KEY_ATTR_TUNNEL] = { .len = OVS_ATTR_NESTED,
398 .next = ovs_tunnel_key_lens, },
399 [OVS_KEY_ATTR_MPLS] = { .len = sizeof(struct ovs_key_mpls) },
Joe Stringerfbccce52015-10-06 11:00:00 -0700400 [OVS_KEY_ATTR_CT_STATE] = { .len = sizeof(u32) },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700401 [OVS_KEY_ATTR_CT_ZONE] = { .len = sizeof(u16) },
Joe Stringer182e3042015-08-26 11:31:49 -0700402 [OVS_KEY_ATTR_CT_MARK] = { .len = sizeof(u32) },
Joe Stringer33db4122015-10-01 15:00:37 -0700403 [OVS_KEY_ATTR_CT_LABELS] = { .len = sizeof(struct ovs_key_ct_labels) },
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800404 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4] = {
405 .len = sizeof(struct ovs_key_ct_tuple_ipv4) },
406 [OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6] = {
407 .len = sizeof(struct ovs_key_ct_tuple_ipv6) },
Pravin B Shelare6445712013-10-03 18:16:47 -0700408};
409
Jesse Gross982b5272015-09-11 18:38:28 -0700410static bool check_attr_len(unsigned int attr_len, unsigned int expected_len)
411{
412 return expected_len == attr_len ||
413 expected_len == OVS_ATTR_NESTED ||
414 expected_len == OVS_ATTR_VARIABLE;
415}
416
Pravin B Shelare6445712013-10-03 18:16:47 -0700417static bool is_all_zero(const u8 *fp, size_t size)
418{
419 int i;
420
421 if (!fp)
422 return false;
423
424 for (i = 0; i < size; i++)
425 if (fp[i])
426 return false;
427
428 return true;
429}
430
431static int __parse_flow_nlattrs(const struct nlattr *attr,
432 const struct nlattr *a[],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800433 u64 *attrsp, bool log, bool nz)
Pravin B Shelare6445712013-10-03 18:16:47 -0700434{
435 const struct nlattr *nla;
436 u64 attrs;
437 int rem;
438
439 attrs = *attrsp;
440 nla_for_each_nested(nla, attr, rem) {
441 u16 type = nla_type(nla);
442 int expected_len;
443
444 if (type > OVS_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800445 OVS_NLERR(log, "Key type %d is out of range max %d",
Pravin B Shelare6445712013-10-03 18:16:47 -0700446 type, OVS_KEY_ATTR_MAX);
447 return -EINVAL;
448 }
449
450 if (attrs & (1 << type)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800451 OVS_NLERR(log, "Duplicate key (type %d).", type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700452 return -EINVAL;
453 }
454
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100455 expected_len = ovs_key_lens[type].len;
Jesse Gross982b5272015-09-11 18:38:28 -0700456 if (!check_attr_len(nla_len(nla), expected_len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800457 OVS_NLERR(log, "Key %d has unexpected len %d expected %d",
458 type, nla_len(nla), expected_len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700459 return -EINVAL;
460 }
461
462 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
463 attrs |= 1 << type;
464 a[type] = nla;
465 }
466 }
467 if (rem) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800468 OVS_NLERR(log, "Message has %d unknown bytes.", rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700469 return -EINVAL;
470 }
471
472 *attrsp = attrs;
473 return 0;
474}
475
476static int parse_flow_mask_nlattrs(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800477 const struct nlattr *a[], u64 *attrsp,
478 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700479{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800480 return __parse_flow_nlattrs(attr, a, attrsp, log, true);
Pravin B Shelare6445712013-10-03 18:16:47 -0700481}
482
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -0800483int parse_flow_nlattrs(const struct nlattr *attr, const struct nlattr *a[],
484 u64 *attrsp, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700485{
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800486 return __parse_flow_nlattrs(attr, a, attrsp, log, false);
487}
488
489static int genev_tun_opt_from_nlattr(const struct nlattr *a,
490 struct sw_flow_match *match, bool is_mask,
491 bool log)
492{
493 unsigned long opt_key_offset;
494
495 if (nla_len(a) > sizeof(match->key->tun_opts)) {
496 OVS_NLERR(log, "Geneve option length err (len %d, max %zu).",
497 nla_len(a), sizeof(match->key->tun_opts));
498 return -EINVAL;
499 }
500
501 if (nla_len(a) % 4 != 0) {
502 OVS_NLERR(log, "Geneve opt len %d is not a multiple of 4.",
503 nla_len(a));
504 return -EINVAL;
505 }
506
507 /* We need to record the length of the options passed
508 * down, otherwise packets with the same format but
509 * additional options will be silently matched.
510 */
511 if (!is_mask) {
512 SW_FLOW_KEY_PUT(match, tun_opts_len, nla_len(a),
513 false);
514 } else {
515 /* This is somewhat unusual because it looks at
516 * both the key and mask while parsing the
517 * attributes (and by extension assumes the key
518 * is parsed first). Normally, we would verify
519 * that each is the correct length and that the
520 * attributes line up in the validate function.
521 * However, that is difficult because this is
522 * variable length and we won't have the
523 * information later.
524 */
525 if (match->key->tun_opts_len != nla_len(a)) {
526 OVS_NLERR(log, "Geneve option len %d != mask len %d",
527 match->key->tun_opts_len, nla_len(a));
528 return -EINVAL;
529 }
530
531 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
532 }
533
Thomas Grafd91641d2015-01-15 03:53:57 +0100534 opt_key_offset = TUN_METADATA_OFFSET(nla_len(a));
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800535 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, nla_data(a),
536 nla_len(a), is_mask);
537 return 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700538}
539
Jesse Gross982b5272015-09-11 18:38:28 -0700540static int vxlan_tun_opt_from_nlattr(const struct nlattr *attr,
Thomas Graf1dd144c2015-01-15 03:53:59 +0100541 struct sw_flow_match *match, bool is_mask,
542 bool log)
543{
Jesse Gross982b5272015-09-11 18:38:28 -0700544 struct nlattr *a;
545 int rem;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100546 unsigned long opt_key_offset;
Thomas Graf614732e2015-07-21 10:44:06 +0200547 struct vxlan_metadata opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100548
549 BUILD_BUG_ON(sizeof(opts) > sizeof(match->key->tun_opts));
550
Thomas Graf1dd144c2015-01-15 03:53:59 +0100551 memset(&opts, 0, sizeof(opts));
Jesse Gross982b5272015-09-11 18:38:28 -0700552 nla_for_each_nested(a, attr, rem) {
553 int type = nla_type(a);
Thomas Graf1dd144c2015-01-15 03:53:59 +0100554
Jesse Gross982b5272015-09-11 18:38:28 -0700555 if (type > OVS_VXLAN_EXT_MAX) {
556 OVS_NLERR(log, "VXLAN extension %d out of range max %d",
557 type, OVS_VXLAN_EXT_MAX);
558 return -EINVAL;
559 }
560
561 if (!check_attr_len(nla_len(a),
562 ovs_vxlan_ext_key_lens[type].len)) {
563 OVS_NLERR(log, "VXLAN extension %d has unexpected len %d expected %d",
564 type, nla_len(a),
565 ovs_vxlan_ext_key_lens[type].len);
566 return -EINVAL;
567 }
568
569 switch (type) {
570 case OVS_VXLAN_EXT_GBP:
571 opts.gbp = nla_get_u32(a);
572 break;
573 default:
574 OVS_NLERR(log, "Unknown VXLAN extension attribute %d",
575 type);
576 return -EINVAL;
577 }
578 }
579 if (rem) {
580 OVS_NLERR(log, "VXLAN extension message has %d unknown bytes.",
581 rem);
582 return -EINVAL;
583 }
Thomas Graf1dd144c2015-01-15 03:53:59 +0100584
585 if (!is_mask)
586 SW_FLOW_KEY_PUT(match, tun_opts_len, sizeof(opts), false);
587 else
588 SW_FLOW_KEY_PUT(match, tun_opts_len, 0xff, true);
589
590 opt_key_offset = TUN_METADATA_OFFSET(sizeof(opts));
591 SW_FLOW_KEY_MEMCPY_OFFSET(match, opt_key_offset, &opts, sizeof(opts),
592 is_mask);
593 return 0;
594}
595
Jiri Benc6b26ba32015-10-05 13:09:47 +0200596static int ip_tun_from_nlattr(const struct nlattr *attr,
597 struct sw_flow_match *match, bool is_mask,
598 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -0700599{
Pravin B Shelar99e28f12015-10-20 20:47:46 -0700600 bool ttl = false, ipv4 = false, ipv6 = false;
601 __be16 tun_flags = 0;
602 int opts_type = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -0700603 struct nlattr *a;
604 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -0700605
606 nla_for_each_nested(a, attr, rem) {
607 int type = nla_type(a);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800608 int err;
609
Pravin B Shelare6445712013-10-03 18:16:47 -0700610 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800611 OVS_NLERR(log, "Tunnel attr %d out of range max %d",
612 type, OVS_TUNNEL_KEY_ATTR_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -0700613 return -EINVAL;
614 }
615
Jesse Gross982b5272015-09-11 18:38:28 -0700616 if (!check_attr_len(nla_len(a),
617 ovs_tunnel_key_lens[type].len)) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800618 OVS_NLERR(log, "Tunnel attr %d has unexpected len %d expected %d",
Thomas Graf81bfe3c32015-01-15 03:53:58 +0100619 type, nla_len(a), ovs_tunnel_key_lens[type].len);
Pravin B Shelare6445712013-10-03 18:16:47 -0700620 return -EINVAL;
621 }
622
623 switch (type) {
624 case OVS_TUNNEL_KEY_ATTR_ID:
625 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
626 nla_get_be64(a), is_mask);
627 tun_flags |= TUNNEL_KEY;
628 break;
629 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200630 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.src,
Jiri Benc67b61f62015-03-29 16:59:26 +0200631 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200632 ipv4 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700633 break;
634 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
Jiri Bencc1ea5d62015-08-20 13:56:23 +0200635 SW_FLOW_KEY_PUT(match, tun_key.u.ipv4.dst,
Jiri Benc67b61f62015-03-29 16:59:26 +0200636 nla_get_in_addr(a), is_mask);
Jiri Benc6b26ba32015-10-05 13:09:47 +0200637 ipv4 = true;
638 break;
639 case OVS_TUNNEL_KEY_ATTR_IPV6_SRC:
640 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
641 nla_get_in6_addr(a), is_mask);
642 ipv6 = true;
643 break;
644 case OVS_TUNNEL_KEY_ATTR_IPV6_DST:
645 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.dst,
646 nla_get_in6_addr(a), is_mask);
647 ipv6 = true;
Pravin B Shelare6445712013-10-03 18:16:47 -0700648 break;
649 case OVS_TUNNEL_KEY_ATTR_TOS:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200650 SW_FLOW_KEY_PUT(match, tun_key.tos,
Pravin B Shelare6445712013-10-03 18:16:47 -0700651 nla_get_u8(a), is_mask);
652 break;
653 case OVS_TUNNEL_KEY_ATTR_TTL:
Jiri Benc7c383fb2015-08-20 13:56:24 +0200654 SW_FLOW_KEY_PUT(match, tun_key.ttl,
Pravin B Shelare6445712013-10-03 18:16:47 -0700655 nla_get_u8(a), is_mask);
656 ttl = true;
657 break;
658 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
659 tun_flags |= TUNNEL_DONT_FRAGMENT;
660 break;
661 case OVS_TUNNEL_KEY_ATTR_CSUM:
662 tun_flags |= TUNNEL_CSUM;
663 break;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800664 case OVS_TUNNEL_KEY_ATTR_TP_SRC:
665 SW_FLOW_KEY_PUT(match, tun_key.tp_src,
666 nla_get_be16(a), is_mask);
667 break;
668 case OVS_TUNNEL_KEY_ATTR_TP_DST:
669 SW_FLOW_KEY_PUT(match, tun_key.tp_dst,
670 nla_get_be16(a), is_mask);
671 break;
Jesse Gross67fa0342014-10-03 15:35:30 -0700672 case OVS_TUNNEL_KEY_ATTR_OAM:
673 tun_flags |= TUNNEL_OAM;
674 break;
Jesse Grossf5796682014-10-03 15:35:33 -0700675 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
Thomas Graf1dd144c2015-01-15 03:53:59 +0100676 if (opts_type) {
677 OVS_NLERR(log, "Multiple metadata blocks provided");
678 return -EINVAL;
679 }
680
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800681 err = genev_tun_opt_from_nlattr(a, match, is_mask, log);
682 if (err)
683 return err;
684
Thomas Graf1dd144c2015-01-15 03:53:59 +0100685 tun_flags |= TUNNEL_GENEVE_OPT;
686 opts_type = type;
687 break;
688 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
689 if (opts_type) {
690 OVS_NLERR(log, "Multiple metadata blocks provided");
691 return -EINVAL;
692 }
693
694 err = vxlan_tun_opt_from_nlattr(a, match, is_mask, log);
695 if (err)
696 return err;
697
698 tun_flags |= TUNNEL_VXLAN_OPT;
699 opts_type = type;
Jesse Grossf5796682014-10-03 15:35:33 -0700700 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700701 default:
Jiri Benc6b26ba32015-10-05 13:09:47 +0200702 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700703 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700704 return -EINVAL;
705 }
706 }
707
708 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
Jiri Benc00a93ba2015-10-05 13:09:46 +0200709 if (is_mask)
710 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
711 else
Jiri Benc6b26ba32015-10-05 13:09:47 +0200712 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
713 false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700714
715 if (rem > 0) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200716 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800717 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700718 return -EINVAL;
719 }
720
Jiri Benc6b26ba32015-10-05 13:09:47 +0200721 if (ipv4 && ipv6) {
722 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
723 return -EINVAL;
724 }
725
Pravin B Shelare6445712013-10-03 18:16:47 -0700726 if (!is_mask) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200727 if (!ipv4 && !ipv6) {
728 OVS_NLERR(log, "IP tunnel dst address not specified");
729 return -EINVAL;
730 }
731 if (ipv4 && !match->key->tun_key.u.ipv4.dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800732 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700733 return -EINVAL;
734 }
Jiri Benc6b26ba32015-10-05 13:09:47 +0200735 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
736 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
737 return -EINVAL;
738 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700739
740 if (!ttl) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200741 OVS_NLERR(log, "IP tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700742 return -EINVAL;
743 }
744 }
745
Thomas Graf1dd144c2015-01-15 03:53:59 +0100746 return opts_type;
747}
748
749static int vxlan_opt_to_nlattr(struct sk_buff *skb,
750 const void *tun_opts, int swkey_tun_opts_len)
751{
Thomas Graf614732e2015-07-21 10:44:06 +0200752 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100753 struct nlattr *nla;
754
755 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
756 if (!nla)
757 return -EMSGSIZE;
758
759 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
760 return -EMSGSIZE;
761
762 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700763 return 0;
764}
765
Jiri Benc6b26ba32015-10-05 13:09:47 +0200766static int __ip_tun_to_nlattr(struct sk_buff *skb,
767 const struct ip_tunnel_key *output,
768 const void *tun_opts, int swkey_tun_opts_len,
769 unsigned short tun_proto)
Pravin B Shelare6445712013-10-03 18:16:47 -0700770{
Pravin B Shelare6445712013-10-03 18:16:47 -0700771 if (output->tun_flags & TUNNEL_KEY &&
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200772 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
773 OVS_TUNNEL_KEY_ATTR_PAD))
Pravin B Shelare6445712013-10-03 18:16:47 -0700774 return -EMSGSIZE;
Jiri Benc6b26ba32015-10-05 13:09:47 +0200775 switch (tun_proto) {
776 case AF_INET:
777 if (output->u.ipv4.src &&
778 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
779 output->u.ipv4.src))
780 return -EMSGSIZE;
781 if (output->u.ipv4.dst &&
782 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
783 output->u.ipv4.dst))
784 return -EMSGSIZE;
785 break;
786 case AF_INET6:
787 if (!ipv6_addr_any(&output->u.ipv6.src) &&
788 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
789 &output->u.ipv6.src))
790 return -EMSGSIZE;
791 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
792 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
793 &output->u.ipv6.dst))
794 return -EMSGSIZE;
795 break;
796 }
Jiri Benc7c383fb2015-08-20 13:56:24 +0200797 if (output->tos &&
798 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700799 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200800 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700801 return -EMSGSIZE;
802 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700803 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700804 return -EMSGSIZE;
805 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700806 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
807 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800808 if (output->tp_src &&
809 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
810 return -EMSGSIZE;
811 if (output->tp_dst &&
812 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
813 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700814 if ((output->tun_flags & TUNNEL_OAM) &&
815 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700816 return -EMSGSIZE;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700817 if (swkey_tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +0100818 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
819 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
820 swkey_tun_opts_len, tun_opts))
821 return -EMSGSIZE;
822 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
823 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
824 return -EMSGSIZE;
825 }
Jesse Grossf5796682014-10-03 15:35:33 -0700826
827 return 0;
828}
829
Jiri Benc6b26ba32015-10-05 13:09:47 +0200830static int ip_tun_to_nlattr(struct sk_buff *skb,
831 const struct ip_tunnel_key *output,
832 const void *tun_opts, int swkey_tun_opts_len,
833 unsigned short tun_proto)
Jesse Grossf5796682014-10-03 15:35:33 -0700834{
835 struct nlattr *nla;
836 int err;
837
838 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
839 if (!nla)
840 return -EMSGSIZE;
841
Jiri Benc6b26ba32015-10-05 13:09:47 +0200842 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
843 tun_proto);
Jesse Grossf5796682014-10-03 15:35:33 -0700844 if (err)
845 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700846
847 nla_nest_end(skb, nla);
848 return 0;
849}
850
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700851int ovs_nla_put_tunnel_info(struct sk_buff *skb,
852 struct ip_tunnel_info *tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800853{
David S. Millerba3e2082015-10-24 06:54:12 -0700854 return __ip_tun_to_nlattr(skb, &tun_info->key,
855 ip_tunnel_info_opts(tun_info),
856 tun_info->options_len,
857 ip_tunnel_info_af(tun_info));
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800858}
859
Eric Garver018c1dd2016-09-07 12:56:59 -0400860static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
861 const struct nlattr *a[],
862 bool is_mask, bool inner)
863{
864 __be16 tci = 0;
865 __be16 tpid = 0;
866
867 if (a[OVS_KEY_ATTR_VLAN])
868 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
869
870 if (a[OVS_KEY_ATTR_ETHERTYPE])
871 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
872
873 if (likely(!inner)) {
874 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
875 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
876 } else {
877 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
878 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
879 }
880 return 0;
881}
882
883static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
884 u64 key_attrs, bool inner,
885 const struct nlattr **a, bool log)
886{
887 __be16 tci = 0;
888
889 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
890 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
891 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
892 /* Not a VLAN. */
893 return 0;
894 }
895
896 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
897 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
898 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
899 return -EINVAL;
900 }
901
902 if (a[OVS_KEY_ATTR_VLAN])
903 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
904
905 if (!(tci & htons(VLAN_TAG_PRESENT))) {
906 if (tci) {
907 OVS_NLERR(log, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
908 (inner) ? "C-VLAN" : "VLAN");
909 return -EINVAL;
910 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
911 /* Corner case for truncated VLAN header. */
912 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
913 (inner) ? "C-VLAN" : "VLAN");
914 return -EINVAL;
915 }
916 }
917
918 return 1;
919}
920
921static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
922 u64 key_attrs, bool inner,
923 const struct nlattr **a, bool log)
924{
925 __be16 tci = 0;
926 __be16 tpid = 0;
927 bool encap_valid = !!(match->key->eth.vlan.tci &
928 htons(VLAN_TAG_PRESENT));
929 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
930 htons(VLAN_TAG_PRESENT));
931
932 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
933 /* Not a VLAN. */
934 return 0;
935 }
936
937 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
938 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
939 (inner) ? "C-VLAN" : "VLAN");
940 return -EINVAL;
941 }
942
943 if (a[OVS_KEY_ATTR_VLAN])
944 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
945
946 if (a[OVS_KEY_ATTR_ETHERTYPE])
947 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
948
949 if (tpid != htons(0xffff)) {
950 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
951 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
952 return -EINVAL;
953 }
954 if (!(tci & htons(VLAN_TAG_PRESENT))) {
955 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
956 (inner) ? "C-VLAN" : "VLAN");
957 return -EINVAL;
958 }
959
960 return 1;
961}
962
963static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
964 u64 *key_attrs, bool inner,
965 const struct nlattr **a, bool is_mask,
966 bool log)
967{
968 int err;
969 const struct nlattr *encap;
970
971 if (!is_mask)
972 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
973 a, log);
974 else
975 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
976 a, log);
977 if (err <= 0)
978 return err;
979
980 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
981 if (err)
982 return err;
983
984 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
985 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
986 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
987
988 encap = a[OVS_KEY_ATTR_ENCAP];
989
990 if (!is_mask)
991 err = parse_flow_nlattrs(encap, a, key_attrs, log);
992 else
993 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
994
995 return err;
996}
997
998static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
999 u64 *key_attrs, const struct nlattr **a,
1000 bool is_mask, bool log)
1001{
1002 int err;
1003 bool encap_valid = false;
1004
1005 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
1006 is_mask, log);
1007 if (err)
1008 return err;
1009
1010 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_TAG_PRESENT));
1011 if (encap_valid) {
1012 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
1013 is_mask, log);
1014 if (err)
1015 return err;
1016 }
1017
1018 return 0;
1019}
1020
Jiri Benc0a6410f2016-11-10 16:28:22 +01001021static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
1022 u64 *attrs, const struct nlattr **a,
1023 bool is_mask, bool log)
1024{
1025 __be16 eth_type;
1026
1027 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1028 if (is_mask) {
1029 /* Always exact match EtherType. */
1030 eth_type = htons(0xffff);
1031 } else if (!eth_proto_is_802_3(eth_type)) {
1032 OVS_NLERR(log, "EtherType %x is less than min %x",
1033 ntohs(eth_type), ETH_P_802_3_MIN);
1034 return -EINVAL;
1035 }
1036
1037 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1038 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1039 return 0;
1040}
1041
Joe Stringerc2ac6672015-08-26 11:31:52 -07001042static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1043 u64 *attrs, const struct nlattr **a,
1044 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001045{
Jiri Benc0a6410f2016-11-10 16:28:22 +01001046 u8 mac_proto = MAC_PROTO_ETHERNET;
1047
Andy Zhou971427f32014-09-15 19:37:25 -07001048 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
1049 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1050
1051 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1052 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
1053 }
1054
1055 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
1056 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1057
1058 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1059 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
1060 }
1061
Pravin B Shelare6445712013-10-03 18:16:47 -07001062 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1063 SW_FLOW_KEY_PUT(match, phy.priority,
1064 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1065 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1066 }
1067
1068 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1069 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1070
Jesse Gross426cda52014-10-06 05:08:38 -07001071 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001072 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -07001073 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001074 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -07001075 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -07001076 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001077 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001078
1079 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1080 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1081 } else if (!is_mask) {
1082 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1083 }
1084
1085 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1086 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1087
1088 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1089 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1090 }
1091 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
Jiri Benc6b26ba32015-10-05 13:09:47 +02001092 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1093 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -07001094 return -EINVAL;
1095 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1096 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001097
1098 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001099 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001100 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001101
Joe Stringer9e384712015-10-19 19:18:57 -07001102 if (ct_state & ~CT_SUPPORTED_MASK) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001103 OVS_NLERR(log, "ct_state flags %08x unsupported",
Joe Stringer6f225952015-10-06 10:59:59 -07001104 ct_state);
1105 return -EINVAL;
1106 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001107
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001108 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001109 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1110 }
1111 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001112 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -07001113 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1114
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001115 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001116 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1117 }
Joe Stringer182e3042015-08-26 11:31:49 -07001118 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001119 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
Joe Stringer182e3042015-08-26 11:31:49 -07001120 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1121
1122 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1123 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1124 }
Joe Stringer33db4122015-10-01 15:00:37 -07001125 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1126 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1127 const struct ovs_key_ct_labels *cl;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001128
Joe Stringer33db4122015-10-01 15:00:37 -07001129 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1130 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
Joe Stringerc2ac6672015-08-26 11:31:52 -07001131 sizeof(*cl), is_mask);
Joe Stringer33db4122015-10-01 15:00:37 -07001132 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001133 }
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001134 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1135 const struct ovs_key_ct_tuple_ipv4 *ct;
1136
1137 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1138
1139 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1140 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1141 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1142 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001143 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001144 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1145 }
1146 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1147 const struct ovs_key_ct_tuple_ipv6 *ct;
1148
1149 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1150
1151 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1152 sizeof(match->key->ipv6.ct_orig.src),
1153 is_mask);
1154 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1155 sizeof(match->key->ipv6.ct_orig.dst),
1156 is_mask);
1157 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1158 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001159 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001160 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1161 }
Jiri Benc329f45b2016-11-10 16:28:18 +01001162
Jiri Benc0a6410f2016-11-10 16:28:22 +01001163 /* For layer 3 packets the Ethernet type is provided
1164 * and treated as metadata but no MAC addresses are provided.
1165 */
1166 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1167 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1168 mac_proto = MAC_PROTO_NONE;
1169
Jiri Benc329f45b2016-11-10 16:28:18 +01001170 /* Always exact match mac_proto */
Jiri Benc0a6410f2016-11-10 16:28:22 +01001171 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1172
1173 if (mac_proto == MAC_PROTO_NONE)
1174 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1175 log);
Jiri Benc329f45b2016-11-10 16:28:18 +01001176
Pravin B Shelare6445712013-10-03 18:16:47 -07001177 return 0;
1178}
1179
Joe Stringerc2ac6672015-08-26 11:31:52 -07001180static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1181 u64 attrs, const struct nlattr **a,
1182 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001183{
1184 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001185
Joe Stringerc2ac6672015-08-26 11:31:52 -07001186 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001187 if (err)
1188 return err;
1189
1190 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1191 const struct ovs_key_ethernet *eth_key;
1192
1193 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1194 SW_FLOW_KEY_MEMCPY(match, eth.src,
1195 eth_key->eth_src, ETH_ALEN, is_mask);
1196 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1197 eth_key->eth_dst, ETH_ALEN, is_mask);
1198 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
Pravin B Shelare6445712013-10-03 18:16:47 -07001199
Jiri Benc0a6410f2016-11-10 16:28:22 +01001200 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1201 /* VLAN attribute is always parsed before getting here since it
1202 * may occur multiple times.
1203 */
1204 OVS_NLERR(log, "VLAN attribute unexpected.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001205 return -EINVAL;
1206 }
1207
Jiri Benc0a6410f2016-11-10 16:28:22 +01001208 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1209 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1210 log);
1211 if (err)
1212 return err;
1213 } else if (!is_mask) {
1214 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1215 }
1216 } else if (!match->key->eth.type) {
1217 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1218 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001219 }
1220
1221 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1222 const struct ovs_key_ipv4 *ipv4_key;
1223
1224 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1225 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001226 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1227 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001228 return -EINVAL;
1229 }
1230 SW_FLOW_KEY_PUT(match, ip.proto,
1231 ipv4_key->ipv4_proto, is_mask);
1232 SW_FLOW_KEY_PUT(match, ip.tos,
1233 ipv4_key->ipv4_tos, is_mask);
1234 SW_FLOW_KEY_PUT(match, ip.ttl,
1235 ipv4_key->ipv4_ttl, is_mask);
1236 SW_FLOW_KEY_PUT(match, ip.frag,
1237 ipv4_key->ipv4_frag, is_mask);
1238 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1239 ipv4_key->ipv4_src, is_mask);
1240 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1241 ipv4_key->ipv4_dst, is_mask);
1242 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1243 }
1244
1245 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1246 const struct ovs_key_ipv6 *ipv6_key;
1247
1248 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1249 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001250 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1251 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001252 return -EINVAL;
1253 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001254
Joe Stringerd3052bb2014-11-19 13:54:49 -08001255 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
David S. Miller14591432014-11-21 22:28:24 -05001256 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001257 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1258 return -EINVAL;
1259 }
1260
Pravin B Shelare6445712013-10-03 18:16:47 -07001261 SW_FLOW_KEY_PUT(match, ipv6.label,
1262 ipv6_key->ipv6_label, is_mask);
1263 SW_FLOW_KEY_PUT(match, ip.proto,
1264 ipv6_key->ipv6_proto, is_mask);
1265 SW_FLOW_KEY_PUT(match, ip.tos,
1266 ipv6_key->ipv6_tclass, is_mask);
1267 SW_FLOW_KEY_PUT(match, ip.ttl,
1268 ipv6_key->ipv6_hlimit, is_mask);
1269 SW_FLOW_KEY_PUT(match, ip.frag,
1270 ipv6_key->ipv6_frag, is_mask);
1271 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1272 ipv6_key->ipv6_src,
1273 sizeof(match->key->ipv6.addr.src),
1274 is_mask);
1275 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1276 ipv6_key->ipv6_dst,
1277 sizeof(match->key->ipv6.addr.dst),
1278 is_mask);
1279
1280 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1281 }
1282
1283 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1284 const struct ovs_key_arp *arp_key;
1285
1286 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1287 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001288 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -07001289 arp_key->arp_op);
1290 return -EINVAL;
1291 }
1292
1293 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1294 arp_key->arp_sip, is_mask);
1295 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1296 arp_key->arp_tip, is_mask);
1297 SW_FLOW_KEY_PUT(match, ip.proto,
1298 ntohs(arp_key->arp_op), is_mask);
1299 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1300 arp_key->arp_sha, ETH_ALEN, is_mask);
1301 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1302 arp_key->arp_tha, ETH_ALEN, is_mask);
1303
1304 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1305 }
1306
Simon Horman25cd9ba2014-10-06 05:05:13 -07001307 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
1308 const struct ovs_key_mpls *mpls_key;
1309
1310 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1311 SW_FLOW_KEY_PUT(match, mpls.top_lse,
1312 mpls_key->mpls_lse, is_mask);
1313
1314 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1315 }
1316
Pravin B Shelare6445712013-10-03 18:16:47 -07001317 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1318 const struct ovs_key_tcp *tcp_key;
1319
1320 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001321 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1322 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001323 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1324 }
1325
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001326 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -07001327 SW_FLOW_KEY_PUT(match, tp.flags,
1328 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1329 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001330 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1331 }
1332
Pravin B Shelare6445712013-10-03 18:16:47 -07001333 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1334 const struct ovs_key_udp *udp_key;
1335
1336 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001337 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1338 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001339 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1340 }
1341
1342 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1343 const struct ovs_key_sctp *sctp_key;
1344
1345 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001346 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1347 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001348 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1349 }
1350
1351 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1352 const struct ovs_key_icmp *icmp_key;
1353
1354 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001355 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001356 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001357 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001358 htons(icmp_key->icmp_code), is_mask);
1359 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1360 }
1361
1362 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1363 const struct ovs_key_icmpv6 *icmpv6_key;
1364
1365 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001366 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001367 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001368 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001369 htons(icmpv6_key->icmpv6_code), is_mask);
1370 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1371 }
1372
1373 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1374 const struct ovs_key_nd *nd_key;
1375
1376 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1377 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1378 nd_key->nd_target,
1379 sizeof(match->key->ipv6.nd.target),
1380 is_mask);
1381 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1382 nd_key->nd_sll, ETH_ALEN, is_mask);
1383 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1384 nd_key->nd_tll, ETH_ALEN, is_mask);
1385 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1386 }
1387
Jesse Gross426cda52014-10-06 05:08:38 -07001388 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001389 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001390 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001391 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001392 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001393
1394 return 0;
1395}
1396
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001397static void nlattr_set(struct nlattr *attr, u8 val,
1398 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001399{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001400 struct nlattr *nla;
1401 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001402
Pravin B Shelarf47de062014-10-16 21:55:45 -07001403 /* The nlattr stream should already have been validated */
1404 nla_for_each_nested(nla, attr, rem) {
Jesse Gross982b5272015-09-11 18:38:28 -07001405 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1406 if (tbl[nla_type(nla)].next)
1407 tbl = tbl[nla_type(nla)].next;
1408 nlattr_set(nla, val, tbl);
1409 } else {
Pravin B Shelarf47de062014-10-16 21:55:45 -07001410 memset(nla_data(nla), val, nla_len(nla));
Jesse Gross982b5272015-09-11 18:38:28 -07001411 }
Joe Stringer9e384712015-10-19 19:18:57 -07001412
1413 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1414 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001415 }
1416}
1417
1418static void mask_set_nlattr(struct nlattr *attr, u8 val)
1419{
Thomas Graf81bfe3c32015-01-15 03:53:58 +01001420 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001421}
1422
1423/**
1424 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1425 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1426 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1427 * does not include any don't care bit.
Joe Stringerc2ac6672015-08-26 11:31:52 -07001428 * @net: Used to determine per-namespace field support.
Pravin B Shelare6445712013-10-03 18:16:47 -07001429 * @match: receives the extracted flow match information.
1430 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1431 * sequence. The fields should of the packet that triggered the creation
1432 * of this flow.
1433 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1434 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001435 * @log: Boolean to allow kernel error logging. Normally true, but when
1436 * probing for feature compatibility this should be passed in as false to
1437 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001438 */
Joe Stringerc2ac6672015-08-26 11:31:52 -07001439int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001440 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001441 const struct nlattr *nla_mask,
1442 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001443{
1444 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelarf47de062014-10-16 21:55:45 -07001445 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001446 u64 key_attrs = 0;
1447 u64 mask_attrs = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001448 int err;
1449
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001450 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001451 if (err)
1452 return err;
1453
Eric Garver018c1dd2016-09-07 12:56:59 -04001454 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1455 if (err)
1456 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001457
Joe Stringerc2ac6672015-08-26 11:31:52 -07001458 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001459 if (err)
1460 return err;
1461
Pravin B Shelara85311b2014-10-19 12:03:40 -07001462 if (match->mask) {
1463 if (!nla_mask) {
1464 /* Create an exact match mask. We need to set to 0xff
1465 * all the 'match->mask' fields that have been touched
1466 * in 'match->key'. We cannot simply memset
1467 * 'match->mask', because padding bytes and fields not
1468 * specified in 'match->key' should be left to 0.
1469 * Instead, we use a stream of netlink attributes,
1470 * copied from 'key' and set to 0xff.
1471 * ovs_key_from_nlattrs() will take care of filling
1472 * 'match->mask' appropriately.
1473 */
1474 newmask = kmemdup(nla_key,
1475 nla_total_size(nla_len(nla_key)),
1476 GFP_KERNEL);
1477 if (!newmask)
1478 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001479
Pravin B Shelara85311b2014-10-19 12:03:40 -07001480 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001481
Pravin B Shelara85311b2014-10-19 12:03:40 -07001482 /* The userspace does not send tunnel attributes that
1483 * are 0, but we should not wildcard them nonetheless.
1484 */
Jiri Benc00a93ba2015-10-05 13:09:46 +02001485 if (match->key->tun_proto)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001486 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1487 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001488
Pravin B Shelara85311b2014-10-19 12:03:40 -07001489 nla_mask = newmask;
1490 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001491
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001492 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001493 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001494 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001495
Pravin B Shelara85311b2014-10-19 12:03:40 -07001496 /* Always match on tci. */
Eric Garver018c1dd2016-09-07 12:56:59 -04001497 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1498 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
Pravin B Shelara85311b2014-10-19 12:03:40 -07001499
Eric Garver018c1dd2016-09-07 12:56:59 -04001500 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1501 if (err)
1502 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001503
Joe Stringerc2ac6672015-08-26 11:31:52 -07001504 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1505 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001506 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001507 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001508 }
1509
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001510 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001511 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001512
Pravin B Shelarf47de062014-10-16 21:55:45 -07001513free_newmask:
1514 kfree(newmask);
1515 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001516}
1517
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001518static size_t get_ufid_len(const struct nlattr *attr, bool log)
1519{
1520 size_t len;
1521
1522 if (!attr)
1523 return 0;
1524
1525 len = nla_len(attr);
1526 if (len < 1 || len > MAX_UFID_LENGTH) {
1527 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1528 nla_len(attr), MAX_UFID_LENGTH);
1529 return 0;
1530 }
1531
1532 return len;
1533}
1534
1535/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1536 * or false otherwise.
1537 */
1538bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1539 bool log)
1540{
1541 sfid->ufid_len = get_ufid_len(attr, log);
1542 if (sfid->ufid_len)
1543 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1544
1545 return sfid->ufid_len;
1546}
1547
1548int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1549 const struct sw_flow_key *key, bool log)
1550{
1551 struct sw_flow_key *new_key;
1552
1553 if (ovs_nla_get_ufid(sfid, ufid, log))
1554 return 0;
1555
1556 /* If UFID was not provided, use unmasked key. */
1557 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1558 if (!new_key)
1559 return -ENOMEM;
1560 memcpy(new_key, key, sizeof(*key));
1561 sfid->unmasked_key = new_key;
1562
1563 return 0;
1564}
1565
1566u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1567{
1568 return attr ? nla_get_u32(attr) : 0;
1569}
1570
Pravin B Shelare6445712013-10-03 18:16:47 -07001571/**
1572 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001573 * @net: Network namespace.
1574 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1575 * metadata.
1576 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1577 * attributes.
1578 * @attrs: Bit mask for the netlink attributes included in @a.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001579 * @log: Boolean to allow kernel error logging. Normally true, but when
1580 * probing for feature compatibility this should be passed in as false to
1581 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001582 *
1583 * This parses a series of Netlink attributes that form a flow key, which must
1584 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1585 * get the metadata, that is, the parts of the flow key that cannot be
1586 * extracted from the packet itself.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001587 *
1588 * This must be called before the packet key fields are filled in 'key'.
Pravin B Shelare6445712013-10-03 18:16:47 -07001589 */
1590
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001591int ovs_nla_get_flow_metadata(struct net *net,
1592 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1593 u64 attrs, struct sw_flow_key *key, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001594{
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001595 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001596
1597 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001598 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001599
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001600 key->ct_state = 0;
1601 key->ct_zone = 0;
1602 key->ct_orig_proto = 0;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001603 memset(&key->ct, 0, sizeof(key->ct));
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001604 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1605 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1606
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001607 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001608
Joe Stringerc2ac6672015-08-26 11:31:52 -07001609 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001610}
1611
Eric Garver018c1dd2016-09-07 12:56:59 -04001612static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1613 bool is_mask)
1614{
1615 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1616
1617 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1618 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1619 return -EMSGSIZE;
1620 return 0;
1621}
1622
Joe Stringer5b4237b2015-01-21 16:42:48 -08001623static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1624 const struct sw_flow_key *output, bool is_mask,
1625 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001626{
1627 struct ovs_key_ethernet *eth_key;
Eric Garver018c1dd2016-09-07 12:56:59 -04001628 struct nlattr *nla;
1629 struct nlattr *encap = NULL;
1630 struct nlattr *in_encap = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001631
Andy Zhou971427f32014-09-15 19:37:25 -07001632 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1633 goto nla_put_failure;
1634
1635 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1636 goto nla_put_failure;
1637
Pravin B Shelare6445712013-10-03 18:16:47 -07001638 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1639 goto nla_put_failure;
1640
Jiri Benc00a93ba2015-10-05 13:09:46 +02001641 if ((swkey->tun_proto || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001642 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001643
1644 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001645 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001646
Jiri Benc6b26ba32015-10-05 13:09:47 +02001647 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
1648 swkey->tun_opts_len, swkey->tun_proto))
Jesse Grossf5796682014-10-03 15:35:33 -07001649 goto nla_put_failure;
1650 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001651
1652 if (swkey->phy.in_port == DP_MAX_PORTS) {
1653 if (is_mask && (output->phy.in_port == 0xffff))
1654 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1655 goto nla_put_failure;
1656 } else {
1657 u16 upper_u16;
1658 upper_u16 = !is_mask ? 0 : 0xffff;
1659
1660 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1661 (upper_u16 << 16) | output->phy.in_port))
1662 goto nla_put_failure;
1663 }
1664
1665 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1666 goto nla_put_failure;
1667
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001668 if (ovs_ct_put_key(swkey, output, skb))
Joe Stringer7f8a4362015-08-26 11:31:48 -07001669 goto nla_put_failure;
1670
Jiri Benc0a6410f2016-11-10 16:28:22 +01001671 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
1672 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1673 if (!nla)
Pravin B Shelare6445712013-10-03 18:16:47 -07001674 goto nla_put_failure;
Eric Garver018c1dd2016-09-07 12:56:59 -04001675
Jiri Benc0a6410f2016-11-10 16:28:22 +01001676 eth_key = nla_data(nla);
1677 ether_addr_copy(eth_key->eth_src, output->eth.src);
1678 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1679
1680 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
1681 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
Eric Garver018c1dd2016-09-07 12:56:59 -04001682 goto nla_put_failure;
Jiri Benc0a6410f2016-11-10 16:28:22 +01001683 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1684 if (!swkey->eth.vlan.tci)
Eric Garver018c1dd2016-09-07 12:56:59 -04001685 goto unencap;
Pravin B Shelare6445712013-10-03 18:16:47 -07001686
Jiri Benc0a6410f2016-11-10 16:28:22 +01001687 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
1688 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
1689 goto nla_put_failure;
1690 in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1691 if (!swkey->eth.cvlan.tci)
1692 goto unencap;
1693 }
1694 }
1695
1696 if (swkey->eth.type == htons(ETH_P_802_2)) {
1697 /*
1698 * Ethertype 802.2 is represented in the netlink with omitted
1699 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1700 * 0xffff in the mask attribute. Ethertype can also
1701 * be wildcarded.
1702 */
1703 if (is_mask && output->eth.type)
1704 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1705 output->eth.type))
1706 goto nla_put_failure;
1707 goto unencap;
1708 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001709 }
1710
1711 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1712 goto nla_put_failure;
1713
Eric Garver018c1dd2016-09-07 12:56:59 -04001714 if (eth_type_vlan(swkey->eth.type)) {
1715 /* There are 3 VLAN tags, we don't know anything about the rest
1716 * of the packet, so truncate here.
1717 */
1718 WARN_ON_ONCE(!(encap && in_encap));
1719 goto unencap;
1720 }
1721
Pravin B Shelare6445712013-10-03 18:16:47 -07001722 if (swkey->eth.type == htons(ETH_P_IP)) {
1723 struct ovs_key_ipv4 *ipv4_key;
1724
1725 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1726 if (!nla)
1727 goto nla_put_failure;
1728 ipv4_key = nla_data(nla);
1729 ipv4_key->ipv4_src = output->ipv4.addr.src;
1730 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1731 ipv4_key->ipv4_proto = output->ip.proto;
1732 ipv4_key->ipv4_tos = output->ip.tos;
1733 ipv4_key->ipv4_ttl = output->ip.ttl;
1734 ipv4_key->ipv4_frag = output->ip.frag;
1735 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1736 struct ovs_key_ipv6 *ipv6_key;
1737
1738 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1739 if (!nla)
1740 goto nla_put_failure;
1741 ipv6_key = nla_data(nla);
1742 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1743 sizeof(ipv6_key->ipv6_src));
1744 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1745 sizeof(ipv6_key->ipv6_dst));
1746 ipv6_key->ipv6_label = output->ipv6.label;
1747 ipv6_key->ipv6_proto = output->ip.proto;
1748 ipv6_key->ipv6_tclass = output->ip.tos;
1749 ipv6_key->ipv6_hlimit = output->ip.ttl;
1750 ipv6_key->ipv6_frag = output->ip.frag;
1751 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1752 swkey->eth.type == htons(ETH_P_RARP)) {
1753 struct ovs_key_arp *arp_key;
1754
1755 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1756 if (!nla)
1757 goto nla_put_failure;
1758 arp_key = nla_data(nla);
1759 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1760 arp_key->arp_sip = output->ipv4.addr.src;
1761 arp_key->arp_tip = output->ipv4.addr.dst;
1762 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001763 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1764 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001765 } else if (eth_p_mpls(swkey->eth.type)) {
1766 struct ovs_key_mpls *mpls_key;
1767
1768 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1769 if (!nla)
1770 goto nla_put_failure;
1771 mpls_key = nla_data(nla);
1772 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001773 }
1774
1775 if ((swkey->eth.type == htons(ETH_P_IP) ||
1776 swkey->eth.type == htons(ETH_P_IPV6)) &&
1777 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1778
1779 if (swkey->ip.proto == IPPROTO_TCP) {
1780 struct ovs_key_tcp *tcp_key;
1781
1782 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1783 if (!nla)
1784 goto nla_put_failure;
1785 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001786 tcp_key->tcp_src = output->tp.src;
1787 tcp_key->tcp_dst = output->tp.dst;
1788 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1789 output->tp.flags))
1790 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001791 } else if (swkey->ip.proto == IPPROTO_UDP) {
1792 struct ovs_key_udp *udp_key;
1793
1794 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1795 if (!nla)
1796 goto nla_put_failure;
1797 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001798 udp_key->udp_src = output->tp.src;
1799 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001800 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1801 struct ovs_key_sctp *sctp_key;
1802
1803 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1804 if (!nla)
1805 goto nla_put_failure;
1806 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001807 sctp_key->sctp_src = output->tp.src;
1808 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001809 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1810 swkey->ip.proto == IPPROTO_ICMP) {
1811 struct ovs_key_icmp *icmp_key;
1812
1813 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1814 if (!nla)
1815 goto nla_put_failure;
1816 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001817 icmp_key->icmp_type = ntohs(output->tp.src);
1818 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001819 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1820 swkey->ip.proto == IPPROTO_ICMPV6) {
1821 struct ovs_key_icmpv6 *icmpv6_key;
1822
1823 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1824 sizeof(*icmpv6_key));
1825 if (!nla)
1826 goto nla_put_failure;
1827 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001828 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1829 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001830
1831 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1832 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1833 struct ovs_key_nd *nd_key;
1834
1835 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1836 if (!nla)
1837 goto nla_put_failure;
1838 nd_key = nla_data(nla);
1839 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1840 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001841 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1842 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001843 }
1844 }
1845 }
1846
1847unencap:
Eric Garver018c1dd2016-09-07 12:56:59 -04001848 if (in_encap)
1849 nla_nest_end(skb, in_encap);
Pravin B Shelare6445712013-10-03 18:16:47 -07001850 if (encap)
1851 nla_nest_end(skb, encap);
1852
1853 return 0;
1854
1855nla_put_failure:
1856 return -EMSGSIZE;
1857}
1858
Joe Stringer5b4237b2015-01-21 16:42:48 -08001859int ovs_nla_put_key(const struct sw_flow_key *swkey,
1860 const struct sw_flow_key *output, int attr, bool is_mask,
1861 struct sk_buff *skb)
1862{
1863 int err;
1864 struct nlattr *nla;
1865
1866 nla = nla_nest_start(skb, attr);
1867 if (!nla)
1868 return -EMSGSIZE;
1869 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1870 if (err)
1871 return err;
1872 nla_nest_end(skb, nla);
1873
1874 return 0;
1875}
1876
1877/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001878int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08001879{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001880 if (ovs_identifier_is_ufid(&flow->id))
1881 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1882 flow->id.ufid);
1883
1884 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1885 OVS_FLOW_ATTR_KEY, false, skb);
1886}
1887
1888/* Called with ovs_mutex or RCU read lock. */
1889int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1890{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08001891 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08001892 OVS_FLOW_ATTR_KEY, false, skb);
1893}
1894
1895/* Called with ovs_mutex or RCU read lock. */
1896int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1897{
1898 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1899 OVS_FLOW_ATTR_MASK, true, skb);
1900}
1901
Pravin B Shelare6445712013-10-03 18:16:47 -07001902#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1903
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001904static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001905{
1906 struct sw_flow_actions *sfa;
1907
Jesse Gross426cda52014-10-06 05:08:38 -07001908 if (size > MAX_ACTIONS_BUFSIZE) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001909 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001910 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001911 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001912
1913 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1914 if (!sfa)
1915 return ERR_PTR(-ENOMEM);
1916
1917 sfa->actions_len = 0;
1918 return sfa;
1919}
1920
Thomas Graf34ae9322015-07-21 10:44:03 +02001921static void ovs_nla_free_set_action(const struct nlattr *a)
1922{
1923 const struct nlattr *ovs_key = nla_data(a);
1924 struct ovs_tunnel_info *ovs_tun;
1925
1926 switch (nla_type(ovs_key)) {
1927 case OVS_KEY_ATTR_TUNNEL_INFO:
1928 ovs_tun = nla_data(ovs_key);
1929 dst_release((struct dst_entry *)ovs_tun->tun_dst);
1930 break;
1931 }
1932}
1933
Pravin B Shelare6445712013-10-03 18:16:47 -07001934void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1935{
Thomas Graf34ae9322015-07-21 10:44:03 +02001936 const struct nlattr *a;
1937 int rem;
1938
1939 if (!sf_acts)
1940 return;
1941
1942 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1943 switch (nla_type(a)) {
1944 case OVS_ACTION_ATTR_SET:
1945 ovs_nla_free_set_action(a);
1946 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001947 case OVS_ACTION_ATTR_CT:
1948 ovs_ct_free_action(a);
1949 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02001950 }
1951 }
1952
1953 kfree(sf_acts);
1954}
1955
1956static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1957{
1958 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1959}
1960
1961/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1962 * The caller must hold rcu_read_lock for this to be sensible. */
1963void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
1964{
1965 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07001966}
1967
1968static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001969 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001970{
1971
1972 struct sw_flow_actions *acts;
1973 int new_acts_size;
1974 int req_size = NLA_ALIGN(attr_len);
1975 int next_offset = offsetof(struct sw_flow_actions, actions) +
1976 (*sfa)->actions_len;
1977
1978 if (req_size <= (ksize(*sfa) - next_offset))
1979 goto out;
1980
1981 new_acts_size = ksize(*sfa) * 2;
1982
1983 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1984 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1985 return ERR_PTR(-EMSGSIZE);
1986 new_acts_size = MAX_ACTIONS_BUFSIZE;
1987 }
1988
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001989 acts = nla_alloc_flow_actions(new_acts_size, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001990 if (IS_ERR(acts))
1991 return (void *)acts;
1992
1993 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1994 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07001995 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001996 kfree(*sfa);
1997 *sfa = acts;
1998
1999out:
2000 (*sfa)->actions_len += req_size;
2001 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
2002}
2003
Jesse Grossf0b128c2014-10-03 15:35:31 -07002004static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002005 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002006{
2007 struct nlattr *a;
2008
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002009 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002010 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07002011 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07002012
2013 a->nla_type = attrtype;
2014 a->nla_len = nla_attr_size(len);
2015
2016 if (data)
2017 memcpy(nla_data(a), data, len);
2018 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
2019
Jesse Grossf0b128c2014-10-03 15:35:31 -07002020 return a;
2021}
2022
Joe Stringer7f8a4362015-08-26 11:31:48 -07002023int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
2024 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07002025{
2026 struct nlattr *a;
2027
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002028 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07002029
Fabian Frederickf35423c12014-11-14 19:32:58 +01002030 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07002031}
2032
2033static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002034 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002035{
2036 int used = (*sfa)->actions_len;
2037 int err;
2038
Joe Stringer7f8a4362015-08-26 11:31:48 -07002039 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002040 if (err)
2041 return err;
2042
2043 return used;
2044}
2045
2046static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2047 int st_offset)
2048{
2049 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2050 st_offset);
2051
2052 a->nla_len = sfa->actions_len - st_offset;
2053}
2054
Joe Stringer7f8a4362015-08-26 11:31:48 -07002055static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002056 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002057 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002058 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002059
Joe Stringer7f8a4362015-08-26 11:31:48 -07002060static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
andy zhou798c1662017-03-20 16:32:29 -07002061 const struct sw_flow_key *key,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002062 struct sw_flow_actions **sfa,
andy zhou798c1662017-03-20 16:32:29 -07002063 __be16 eth_type, __be16 vlan_tci,
2064 bool log, bool last)
Pravin B Shelare6445712013-10-03 18:16:47 -07002065{
2066 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2067 const struct nlattr *probability, *actions;
2068 const struct nlattr *a;
andy zhou798c1662017-03-20 16:32:29 -07002069 int rem, start, err;
2070 struct sample_arg arg;
Pravin B Shelare6445712013-10-03 18:16:47 -07002071
2072 memset(attrs, 0, sizeof(attrs));
2073 nla_for_each_nested(a, attr, rem) {
2074 int type = nla_type(a);
2075 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2076 return -EINVAL;
2077 attrs[type] = a;
2078 }
2079 if (rem)
2080 return -EINVAL;
2081
2082 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2083 if (!probability || nla_len(probability) != sizeof(u32))
2084 return -EINVAL;
2085
2086 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2087 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2088 return -EINVAL;
2089
2090 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002091 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002092 if (start < 0)
2093 return start;
andy zhou798c1662017-03-20 16:32:29 -07002094
2095 /* When both skb and flow may be changed, put the sample
2096 * into a deferred fifo. On the other hand, if only skb
2097 * may be modified, the actions can be executed in place.
2098 *
2099 * Do this analysis at the flow installation time.
2100 * Set 'clone_action->exec' to true if the actions can be
2101 * executed without being deferred.
2102 *
2103 * If the sample is the last action, it can always be excuted
2104 * rather than deferred.
2105 */
2106 arg.exec = last || !actions_may_change_flow(actions);
2107 arg.probability = nla_get_u32(probability);
2108
2109 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
2110 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002111 if (err)
2112 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07002113
andy zhou798c1662017-03-20 16:32:29 -07002114 err = __ovs_nla_copy_actions(net, actions, key, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002115 eth_type, vlan_tci, log);
andy zhou798c1662017-03-20 16:32:29 -07002116
Pravin B Shelare6445712013-10-03 18:16:47 -07002117 if (err)
2118 return err;
2119
Pravin B Shelare6445712013-10-03 18:16:47 -07002120 add_nested_action_end(*sfa, start);
2121
2122 return 0;
2123}
2124
Pravin B Shelare6445712013-10-03 18:16:47 -07002125void ovs_match_init(struct sw_flow_match *match,
2126 struct sw_flow_key *key,
pravin shelar22799942016-09-19 13:51:00 -07002127 bool reset_key,
Pravin B Shelare6445712013-10-03 18:16:47 -07002128 struct sw_flow_mask *mask)
2129{
2130 memset(match, 0, sizeof(*match));
2131 match->key = key;
2132 match->mask = mask;
2133
pravin shelar22799942016-09-19 13:51:00 -07002134 if (reset_key)
2135 memset(key, 0, sizeof(*key));
Pravin B Shelare6445712013-10-03 18:16:47 -07002136
2137 if (mask) {
2138 memset(&mask->key, 0, sizeof(mask->key));
2139 mask->range.start = mask->range.end = 0;
2140 }
2141}
2142
Thomas Grafd91641d2015-01-15 03:53:57 +01002143static int validate_geneve_opts(struct sw_flow_key *key)
2144{
2145 struct geneve_opt *option;
2146 int opts_len = key->tun_opts_len;
2147 bool crit_opt = false;
2148
2149 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2150 while (opts_len > 0) {
2151 int len;
2152
2153 if (opts_len < sizeof(*option))
2154 return -EINVAL;
2155
2156 len = sizeof(*option) + option->length * 4;
2157 if (len > opts_len)
2158 return -EINVAL;
2159
2160 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2161
2162 option = (struct geneve_opt *)((u8 *)option + len);
2163 opts_len -= len;
2164 };
2165
2166 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2167
2168 return 0;
2169}
2170
Pravin B Shelare6445712013-10-03 18:16:47 -07002171static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002172 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002173{
2174 struct sw_flow_match match;
2175 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02002176 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002177 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02002178 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002179 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01002180 int err = 0, start, opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002181
pravin shelar22799942016-09-19 13:51:00 -07002182 ovs_match_init(&match, &key, true, NULL);
Jiri Benc6b26ba32015-10-05 13:09:47 +02002183 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
Thomas Graf1dd144c2015-01-15 03:53:59 +01002184 if (opts_type < 0)
2185 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002186
Jesse Grossf5796682014-10-03 15:35:33 -07002187 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01002188 switch (opts_type) {
2189 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2190 err = validate_geneve_opts(&key);
2191 if (err < 0)
2192 return err;
2193 break;
2194 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2195 break;
2196 }
Jesse Grossf5796682014-10-03 15:35:33 -07002197 };
2198
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002199 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002200 if (start < 0)
2201 return start;
2202
Thomas Graf34ae9322015-07-21 10:44:03 +02002203 tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
2204 if (!tun_dst)
2205 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002206
Paolo Abenid71785f2016-02-12 15:43:57 +01002207 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2208 if (err) {
2209 dst_release((struct dst_entry *)tun_dst);
2210 return err;
2211 }
2212
Thomas Graf34ae9322015-07-21 10:44:03 +02002213 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
2214 sizeof(*ovs_tun), log);
2215 if (IS_ERR(a)) {
2216 dst_release((struct dst_entry *)tun_dst);
2217 return PTR_ERR(a);
2218 }
2219
2220 ovs_tun = nla_data(a);
2221 ovs_tun->tun_dst = tun_dst;
2222
2223 tun_info = &tun_dst->u.tun_info;
2224 tun_info->mode = IP_TUNNEL_INFO_TX;
Jiri Benc00a93ba2015-10-05 13:09:46 +02002225 if (key.tun_proto == AF_INET6)
2226 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002227 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07002228
Pravin B Shelar4c222792015-08-30 18:09:38 -07002229 /* We need to store the options in the action itself since
2230 * everything else will go away after flow setup. We can append
2231 * it to tun_info and then point there.
2232 */
2233 ip_tunnel_info_opts_set(tun_info,
2234 TUN_METADATA_OPTS(&key, key.tun_opts_len),
2235 key.tun_opts_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07002236 add_nested_action_end(*sfa, start);
2237
2238 return err;
2239}
2240
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002241/* Return false if there are any non-masked bits set.
2242 * Mask follows data immediately, before any netlink padding.
2243 */
2244static bool validate_masked(u8 *data, int len)
2245{
2246 u8 *mask = data + len;
2247
2248 while (len--)
2249 if (*data++ & ~*mask++)
2250 return false;
2251
2252 return true;
2253}
2254
Pravin B Shelare6445712013-10-03 18:16:47 -07002255static int validate_set(const struct nlattr *a,
2256 const struct sw_flow_key *flow_key,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002257 struct sw_flow_actions **sfa, bool *skip_copy,
2258 u8 mac_proto, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002259{
2260 const struct nlattr *ovs_key = nla_data(a);
2261 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002262 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002263
2264 /* There can be only one key in a action */
2265 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2266 return -EINVAL;
2267
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002268 key_len = nla_len(ovs_key);
2269 if (masked)
2270 key_len /= 2;
2271
Pravin B Shelare6445712013-10-03 18:16:47 -07002272 if (key_type > OVS_KEY_ATTR_MAX ||
Jesse Gross982b5272015-09-11 18:38:28 -07002273 !check_attr_len(key_len, ovs_key_lens[key_type].len))
Pravin B Shelare6445712013-10-03 18:16:47 -07002274 return -EINVAL;
2275
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002276 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2277 return -EINVAL;
2278
Pravin B Shelare6445712013-10-03 18:16:47 -07002279 switch (key_type) {
2280 const struct ovs_key_ipv4 *ipv4_key;
2281 const struct ovs_key_ipv6 *ipv6_key;
2282 int err;
2283
2284 case OVS_KEY_ATTR_PRIORITY:
2285 case OVS_KEY_ATTR_SKB_MARK:
Joe Stringer182e3042015-08-26 11:31:49 -07002286 case OVS_KEY_ATTR_CT_MARK:
Joe Stringer33db4122015-10-01 15:00:37 -07002287 case OVS_KEY_ATTR_CT_LABELS:
Pravin B Shelare6445712013-10-03 18:16:47 -07002288 break;
2289
Jiri Benc0a6410f2016-11-10 16:28:22 +01002290 case OVS_KEY_ATTR_ETHERNET:
2291 if (mac_proto != MAC_PROTO_ETHERNET)
2292 return -EINVAL;
Jarno Rajahalme87e159c2016-12-19 17:06:33 -08002293 break;
Jiri Benc0a6410f2016-11-10 16:28:22 +01002294
Pravin B Shelare6445712013-10-03 18:16:47 -07002295 case OVS_KEY_ATTR_TUNNEL:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002296 if (masked)
2297 return -EINVAL; /* Masked tunnel set not supported. */
2298
2299 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002300 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002301 if (err)
2302 return err;
2303 break;
2304
2305 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002306 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07002307 return -EINVAL;
2308
Pravin B Shelare6445712013-10-03 18:16:47 -07002309 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002310
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002311 if (masked) {
2312 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002313
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002314 /* Non-writeable fields. */
2315 if (mask->ipv4_proto || mask->ipv4_frag)
2316 return -EINVAL;
2317 } else {
2318 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2319 return -EINVAL;
2320
2321 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2322 return -EINVAL;
2323 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002324 break;
2325
2326 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002327 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07002328 return -EINVAL;
2329
Pravin B Shelare6445712013-10-03 18:16:47 -07002330 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002331
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002332 if (masked) {
2333 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002334
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002335 /* Non-writeable fields. */
2336 if (mask->ipv6_proto || mask->ipv6_frag)
2337 return -EINVAL;
2338
2339 /* Invalid bits in the flow label mask? */
2340 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2341 return -EINVAL;
2342 } else {
2343 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2344 return -EINVAL;
2345
2346 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2347 return -EINVAL;
2348 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002349 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2350 return -EINVAL;
2351
2352 break;
2353
2354 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002355 if ((eth_type != htons(ETH_P_IP) &&
2356 eth_type != htons(ETH_P_IPV6)) ||
2357 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002358 return -EINVAL;
2359
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002360 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002361
2362 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002363 if ((eth_type != htons(ETH_P_IP) &&
2364 eth_type != htons(ETH_P_IPV6)) ||
2365 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002366 return -EINVAL;
2367
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002368 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002369
2370 case OVS_KEY_ATTR_MPLS:
2371 if (!eth_p_mpls(eth_type))
2372 return -EINVAL;
2373 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002374
2375 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002376 if ((eth_type != htons(ETH_P_IP) &&
2377 eth_type != htons(ETH_P_IPV6)) ||
2378 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002379 return -EINVAL;
2380
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002381 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002382
2383 default:
2384 return -EINVAL;
2385 }
2386
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002387 /* Convert non-masked non-tunnel set actions to masked set actions. */
2388 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2389 int start, len = key_len * 2;
2390 struct nlattr *at;
2391
2392 *skip_copy = true;
2393
2394 start = add_nested_action_start(sfa,
2395 OVS_ACTION_ATTR_SET_TO_MASKED,
2396 log);
2397 if (start < 0)
2398 return start;
2399
2400 at = __add_action(sfa, key_type, NULL, len, log);
2401 if (IS_ERR(at))
2402 return PTR_ERR(at);
2403
2404 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2405 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2406 /* Clear non-writeable bits from otherwise writeable fields. */
2407 if (key_type == OVS_KEY_ATTR_IPV6) {
2408 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2409
2410 mask->ipv6_label &= htonl(0x000FFFFF);
2411 }
2412 add_nested_action_end(*sfa, start);
2413 }
2414
Pravin B Shelare6445712013-10-03 18:16:47 -07002415 return 0;
2416}
2417
2418static int validate_userspace(const struct nlattr *attr)
2419{
2420 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2421 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2422 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002423 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002424 };
2425 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2426 int error;
2427
2428 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
2429 attr, userspace_policy);
2430 if (error)
2431 return error;
2432
2433 if (!a[OVS_USERSPACE_ATTR_PID] ||
2434 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2435 return -EINVAL;
2436
2437 return 0;
2438}
2439
2440static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002441 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002442{
2443 int totlen = NLA_ALIGN(from->nla_len);
2444 struct nlattr *to;
2445
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002446 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002447 if (IS_ERR(to))
2448 return PTR_ERR(to);
2449
2450 memcpy(to, from, totlen);
2451 return 0;
2452}
2453
Joe Stringer7f8a4362015-08-26 11:31:48 -07002454static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002455 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002456 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002457 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002458{
Jiri Benc0a6410f2016-11-10 16:28:22 +01002459 u8 mac_proto = ovs_key_mac_proto(key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002460 const struct nlattr *a;
2461 int rem, err;
2462
Pravin B Shelare6445712013-10-03 18:16:47 -07002463 nla_for_each_nested(a, attr, rem) {
2464 /* Expected argument lengths, (u32)-1 for variable length. */
2465 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2466 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002467 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002468 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002469 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2470 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002471 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2472 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2473 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002474 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002475 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002476 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2477 [OVS_ACTION_ATTR_CT] = (u32)-1,
William Tuf2a4d082016-06-10 11:49:33 -07002478 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
Jiri Benc91820da2016-11-10 16:28:23 +01002479 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
2480 [OVS_ACTION_ATTR_POP_ETH] = 0,
Pravin B Shelare6445712013-10-03 18:16:47 -07002481 };
2482 const struct ovs_action_push_vlan *vlan;
2483 int type = nla_type(a);
2484 bool skip_copy;
2485
2486 if (type > OVS_ACTION_ATTR_MAX ||
2487 (action_lens[type] != nla_len(a) &&
2488 action_lens[type] != (u32)-1))
2489 return -EINVAL;
2490
2491 skip_copy = false;
2492 switch (type) {
2493 case OVS_ACTION_ATTR_UNSPEC:
2494 return -EINVAL;
2495
2496 case OVS_ACTION_ATTR_USERSPACE:
2497 err = validate_userspace(a);
2498 if (err)
2499 return err;
2500 break;
2501
2502 case OVS_ACTION_ATTR_OUTPUT:
2503 if (nla_get_u32(a) >= DP_MAX_PORTS)
2504 return -EINVAL;
2505 break;
2506
William Tuf2a4d082016-06-10 11:49:33 -07002507 case OVS_ACTION_ATTR_TRUNC: {
2508 const struct ovs_action_trunc *trunc = nla_data(a);
2509
2510 if (trunc->max_len < ETH_HLEN)
2511 return -EINVAL;
2512 break;
2513 }
2514
Andy Zhou971427f32014-09-15 19:37:25 -07002515 case OVS_ACTION_ATTR_HASH: {
2516 const struct ovs_action_hash *act_hash = nla_data(a);
2517
2518 switch (act_hash->hash_alg) {
2519 case OVS_HASH_ALG_L4:
2520 break;
2521 default:
2522 return -EINVAL;
2523 }
2524
2525 break;
2526 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002527
2528 case OVS_ACTION_ATTR_POP_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01002529 if (mac_proto != MAC_PROTO_ETHERNET)
2530 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002531 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07002532 break;
2533
2534 case OVS_ACTION_ATTR_PUSH_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01002535 if (mac_proto != MAC_PROTO_ETHERNET)
2536 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07002537 vlan = nla_data(a);
Eric Garver018c1dd2016-09-07 12:56:59 -04002538 if (!eth_type_vlan(vlan->vlan_tpid))
Pravin B Shelare6445712013-10-03 18:16:47 -07002539 return -EINVAL;
2540 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2541 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002542 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07002543 break;
2544
Andy Zhou971427f32014-09-15 19:37:25 -07002545 case OVS_ACTION_ATTR_RECIRC:
2546 break;
2547
Simon Horman25cd9ba2014-10-06 05:05:13 -07002548 case OVS_ACTION_ATTR_PUSH_MPLS: {
2549 const struct ovs_action_push_mpls *mpls = nla_data(a);
2550
Simon Horman25cd9ba2014-10-06 05:05:13 -07002551 if (!eth_p_mpls(mpls->mpls_ethertype))
2552 return -EINVAL;
2553 /* Prohibit push MPLS other than to a white list
2554 * for packets that have a known tag order.
2555 */
2556 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2557 (eth_type != htons(ETH_P_IP) &&
2558 eth_type != htons(ETH_P_IPV6) &&
2559 eth_type != htons(ETH_P_ARP) &&
2560 eth_type != htons(ETH_P_RARP) &&
2561 !eth_p_mpls(eth_type)))
2562 return -EINVAL;
2563 eth_type = mpls->mpls_ethertype;
2564 break;
2565 }
2566
2567 case OVS_ACTION_ATTR_POP_MPLS:
2568 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2569 !eth_p_mpls(eth_type))
2570 return -EINVAL;
2571
2572 /* Disallow subsequent L2.5+ set and mpls_pop actions
2573 * as there is no check here to ensure that the new
2574 * eth_type is valid and thus set actions could
2575 * write off the end of the packet or otherwise
2576 * corrupt it.
2577 *
2578 * Support for these actions is planned using packet
2579 * recirculation.
2580 */
2581 eth_type = htons(0);
2582 break;
2583
Pravin B Shelare6445712013-10-03 18:16:47 -07002584 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002585 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002586 &skip_copy, mac_proto, eth_type,
2587 false, log);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002588 if (err)
2589 return err;
2590 break;
2591
2592 case OVS_ACTION_ATTR_SET_MASKED:
2593 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002594 &skip_copy, mac_proto, eth_type,
2595 true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002596 if (err)
2597 return err;
2598 break;
2599
andy zhou798c1662017-03-20 16:32:29 -07002600 case OVS_ACTION_ATTR_SAMPLE: {
2601 bool last = nla_is_last(a, rem);
2602
2603 err = validate_and_copy_sample(net, a, key, sfa,
2604 eth_type, vlan_tci,
2605 log, last);
Pravin B Shelare6445712013-10-03 18:16:47 -07002606 if (err)
2607 return err;
2608 skip_copy = true;
2609 break;
andy zhou798c1662017-03-20 16:32:29 -07002610 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002611
Joe Stringer7f8a4362015-08-26 11:31:48 -07002612 case OVS_ACTION_ATTR_CT:
2613 err = ovs_ct_copy_action(net, a, key, sfa, log);
2614 if (err)
2615 return err;
2616 skip_copy = true;
2617 break;
2618
Jiri Benc91820da2016-11-10 16:28:23 +01002619 case OVS_ACTION_ATTR_PUSH_ETH:
2620 /* Disallow pushing an Ethernet header if one
2621 * is already present */
2622 if (mac_proto != MAC_PROTO_NONE)
2623 return -EINVAL;
2624 mac_proto = MAC_PROTO_NONE;
2625 break;
2626
2627 case OVS_ACTION_ATTR_POP_ETH:
2628 if (mac_proto != MAC_PROTO_ETHERNET)
2629 return -EINVAL;
2630 if (vlan_tci & htons(VLAN_TAG_PRESENT))
2631 return -EINVAL;
2632 mac_proto = MAC_PROTO_ETHERNET;
2633 break;
2634
Pravin B Shelare6445712013-10-03 18:16:47 -07002635 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002636 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002637 return -EINVAL;
2638 }
2639 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002640 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002641 if (err)
2642 return err;
2643 }
2644 }
2645
2646 if (rem > 0)
2647 return -EINVAL;
2648
2649 return 0;
2650}
2651
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002652/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07002653int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002654 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002655 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07002656{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002657 int err;
2658
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002659 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002660 if (IS_ERR(*sfa))
2661 return PTR_ERR(*sfa);
2662
Joe Stringer8e2fed12015-08-26 11:31:44 -07002663 (*sfa)->orig_len = nla_len(attr);
andy zhou798c1662017-03-20 16:32:29 -07002664 err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
Eric Garver018c1dd2016-09-07 12:56:59 -04002665 key->eth.vlan.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002666 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02002667 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002668
2669 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002670}
2671
andy zhou798c1662017-03-20 16:32:29 -07002672static int sample_action_to_attr(const struct nlattr *attr,
2673 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07002674{
andy zhou798c1662017-03-20 16:32:29 -07002675 struct nlattr *start, *ac_start = NULL, *sample_arg;
2676 int err = 0, rem = nla_len(attr);
2677 const struct sample_arg *arg;
2678 struct nlattr *actions;
Pravin B Shelare6445712013-10-03 18:16:47 -07002679
2680 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2681 if (!start)
2682 return -EMSGSIZE;
2683
andy zhou798c1662017-03-20 16:32:29 -07002684 sample_arg = nla_data(attr);
2685 arg = nla_data(sample_arg);
2686 actions = nla_next(sample_arg, &rem);
Pravin B Shelare6445712013-10-03 18:16:47 -07002687
andy zhou798c1662017-03-20 16:32:29 -07002688 if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
2689 err = -EMSGSIZE;
2690 goto out;
Pravin B Shelare6445712013-10-03 18:16:47 -07002691 }
2692
andy zhou798c1662017-03-20 16:32:29 -07002693 ac_start = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2694 if (!ac_start) {
2695 err = -EMSGSIZE;
2696 goto out;
2697 }
2698
2699 err = ovs_nla_put_actions(actions, rem, skb);
2700
2701out:
2702 if (err) {
2703 nla_nest_cancel(skb, ac_start);
2704 nla_nest_cancel(skb, start);
2705 } else {
2706 nla_nest_end(skb, ac_start);
2707 nla_nest_end(skb, start);
2708 }
2709
Pravin B Shelare6445712013-10-03 18:16:47 -07002710 return err;
2711}
2712
2713static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2714{
2715 const struct nlattr *ovs_key = nla_data(a);
2716 int key_type = nla_type(ovs_key);
2717 struct nlattr *start;
2718 int err;
2719
2720 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07002721 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02002722 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2723 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002724
Pravin B Shelare6445712013-10-03 18:16:47 -07002725 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2726 if (!start)
2727 return -EMSGSIZE;
2728
Simon Hormane905eab2015-12-18 19:43:15 +09002729 err = ip_tun_to_nlattr(skb, &tun_info->key,
2730 ip_tunnel_info_opts(tun_info),
2731 tun_info->options_len,
2732 ip_tunnel_info_af(tun_info));
Pravin B Shelare6445712013-10-03 18:16:47 -07002733 if (err)
2734 return err;
2735 nla_nest_end(skb, start);
2736 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002737 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002738 default:
2739 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2740 return -EMSGSIZE;
2741 break;
2742 }
2743
2744 return 0;
2745}
2746
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002747static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2748 struct sk_buff *skb)
2749{
2750 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08002751 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002752 size_t key_len = nla_len(ovs_key) / 2;
2753
2754 /* Revert the conversion we did from a non-masked set action to
2755 * masked set action.
2756 */
Joe Stringerf4f8e732015-03-02 18:49:56 -08002757 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2758 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002759 return -EMSGSIZE;
2760
Joe Stringerf4f8e732015-03-02 18:49:56 -08002761 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2762 return -EMSGSIZE;
2763
2764 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002765 return 0;
2766}
2767
Pravin B Shelare6445712013-10-03 18:16:47 -07002768int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2769{
2770 const struct nlattr *a;
2771 int rem, err;
2772
2773 nla_for_each_attr(a, attr, len, rem) {
2774 int type = nla_type(a);
2775
2776 switch (type) {
2777 case OVS_ACTION_ATTR_SET:
2778 err = set_action_to_attr(a, skb);
2779 if (err)
2780 return err;
2781 break;
2782
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002783 case OVS_ACTION_ATTR_SET_TO_MASKED:
2784 err = masked_set_action_to_set_action_attr(a, skb);
2785 if (err)
2786 return err;
2787 break;
2788
Pravin B Shelare6445712013-10-03 18:16:47 -07002789 case OVS_ACTION_ATTR_SAMPLE:
2790 err = sample_action_to_attr(a, skb);
2791 if (err)
2792 return err;
2793 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002794
2795 case OVS_ACTION_ATTR_CT:
2796 err = ovs_ct_action_to_attr(nla_data(a), skb);
2797 if (err)
2798 return err;
2799 break;
2800
Pravin B Shelare6445712013-10-03 18:16:47 -07002801 default:
2802 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2803 return -EMSGSIZE;
2804 break;
2805 }
2806 }
2807
2808 return 0;
2809}