blob: 7e1d8a2afa63687aeb87121dbae614bad2ec34a3 [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 Graf81bfe3c2015-01-15 03:53:58 +010054struct ovs_len_tbl {
55 int len;
56 const struct ovs_len_tbl *next;
57};
58
59#define OVS_ATTR_NESTED -1
Jesse Gross982b5272015-09-11 18:38:28 -070060#define OVS_ATTR_VARIABLE -2
Thomas Graf81bfe3c2015-01-15 03:53:58 +010061
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 Graf81bfe3c2015-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 Graf81bfe3c2015-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 Graf81bfe3c2015-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 Graf81bfe3c2015-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 Graf81bfe3c2015-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:
Or Gerlitz3d20f1f2017-03-15 18:10:47 +0200640 SW_FLOW_KEY_PUT(match, tun_key.u.ipv6.src,
Jiri Benc6b26ba32015-10-05 13:09:47 +0200641 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;
Kris Murphy8f3dbfd72017-03-16 10:51:28 -0500701 case OVS_TUNNEL_KEY_ATTR_PAD:
702 break;
Pravin B Shelare6445712013-10-03 18:16:47 -0700703 default:
Jiri Benc6b26ba32015-10-05 13:09:47 +0200704 OVS_NLERR(log, "Unknown IP tunnel attribute %d",
Jesse Grossf5796682014-10-03 15:35:33 -0700705 type);
Pravin B Shelare6445712013-10-03 18:16:47 -0700706 return -EINVAL;
707 }
708 }
709
710 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
Jiri Benc00a93ba2015-10-05 13:09:46 +0200711 if (is_mask)
712 SW_FLOW_KEY_MEMSET_FIELD(match, tun_proto, 0xff, true);
713 else
Jiri Benc6b26ba32015-10-05 13:09:47 +0200714 SW_FLOW_KEY_PUT(match, tun_proto, ipv6 ? AF_INET6 : AF_INET,
715 false);
Pravin B Shelare6445712013-10-03 18:16:47 -0700716
717 if (rem > 0) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200718 OVS_NLERR(log, "IP tunnel attribute has %d unknown bytes.",
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800719 rem);
Pravin B Shelare6445712013-10-03 18:16:47 -0700720 return -EINVAL;
721 }
722
Jiri Benc6b26ba32015-10-05 13:09:47 +0200723 if (ipv4 && ipv6) {
724 OVS_NLERR(log, "Mixed IPv4 and IPv6 tunnel attributes");
725 return -EINVAL;
726 }
727
Pravin B Shelare6445712013-10-03 18:16:47 -0700728 if (!is_mask) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200729 if (!ipv4 && !ipv6) {
730 OVS_NLERR(log, "IP tunnel dst address not specified");
731 return -EINVAL;
732 }
733 if (ipv4 && !match->key->tun_key.u.ipv4.dst) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800734 OVS_NLERR(log, "IPv4 tunnel dst address is zero");
Pravin B Shelare6445712013-10-03 18:16:47 -0700735 return -EINVAL;
736 }
Jiri Benc6b26ba32015-10-05 13:09:47 +0200737 if (ipv6 && ipv6_addr_any(&match->key->tun_key.u.ipv6.dst)) {
738 OVS_NLERR(log, "IPv6 tunnel dst address is zero");
739 return -EINVAL;
740 }
Pravin B Shelare6445712013-10-03 18:16:47 -0700741
742 if (!ttl) {
Jiri Benc6b26ba32015-10-05 13:09:47 +0200743 OVS_NLERR(log, "IP tunnel TTL not specified.");
Pravin B Shelare6445712013-10-03 18:16:47 -0700744 return -EINVAL;
745 }
746 }
747
Thomas Graf1dd144c2015-01-15 03:53:59 +0100748 return opts_type;
749}
750
751static int vxlan_opt_to_nlattr(struct sk_buff *skb,
752 const void *tun_opts, int swkey_tun_opts_len)
753{
Thomas Graf614732e2015-07-21 10:44:06 +0200754 const struct vxlan_metadata *opts = tun_opts;
Thomas Graf1dd144c2015-01-15 03:53:59 +0100755 struct nlattr *nla;
756
757 nla = nla_nest_start(skb, OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS);
758 if (!nla)
759 return -EMSGSIZE;
760
761 if (nla_put_u32(skb, OVS_VXLAN_EXT_GBP, opts->gbp) < 0)
762 return -EMSGSIZE;
763
764 nla_nest_end(skb, nla);
Pravin B Shelare6445712013-10-03 18:16:47 -0700765 return 0;
766}
767
Jiri Benc6b26ba32015-10-05 13:09:47 +0200768static int __ip_tun_to_nlattr(struct sk_buff *skb,
769 const struct ip_tunnel_key *output,
770 const void *tun_opts, int swkey_tun_opts_len,
771 unsigned short tun_proto)
Pravin B Shelare6445712013-10-03 18:16:47 -0700772{
Pravin B Shelare6445712013-10-03 18:16:47 -0700773 if (output->tun_flags & TUNNEL_KEY &&
Nicolas Dichtelb46f6de2016-04-22 17:31:18 +0200774 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id,
775 OVS_TUNNEL_KEY_ATTR_PAD))
Pravin B Shelare6445712013-10-03 18:16:47 -0700776 return -EMSGSIZE;
Jiri Benc6b26ba32015-10-05 13:09:47 +0200777 switch (tun_proto) {
778 case AF_INET:
779 if (output->u.ipv4.src &&
780 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
781 output->u.ipv4.src))
782 return -EMSGSIZE;
783 if (output->u.ipv4.dst &&
784 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
785 output->u.ipv4.dst))
786 return -EMSGSIZE;
787 break;
788 case AF_INET6:
789 if (!ipv6_addr_any(&output->u.ipv6.src) &&
790 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_SRC,
791 &output->u.ipv6.src))
792 return -EMSGSIZE;
793 if (!ipv6_addr_any(&output->u.ipv6.dst) &&
794 nla_put_in6_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV6_DST,
795 &output->u.ipv6.dst))
796 return -EMSGSIZE;
797 break;
798 }
Jiri Benc7c383fb2015-08-20 13:56:24 +0200799 if (output->tos &&
800 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->tos))
Pravin B Shelare6445712013-10-03 18:16:47 -0700801 return -EMSGSIZE;
Jiri Benc7c383fb2015-08-20 13:56:24 +0200802 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ttl))
Pravin B Shelare6445712013-10-03 18:16:47 -0700803 return -EMSGSIZE;
804 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700805 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
Pravin B Shelare6445712013-10-03 18:16:47 -0700806 return -EMSGSIZE;
807 if ((output->tun_flags & TUNNEL_CSUM) &&
Jesse Gross67fa0342014-10-03 15:35:30 -0700808 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
809 return -EMSGSIZE;
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800810 if (output->tp_src &&
811 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_SRC, output->tp_src))
812 return -EMSGSIZE;
813 if (output->tp_dst &&
814 nla_put_be16(skb, OVS_TUNNEL_KEY_ATTR_TP_DST, output->tp_dst))
815 return -EMSGSIZE;
Jesse Gross67fa0342014-10-03 15:35:30 -0700816 if ((output->tun_flags & TUNNEL_OAM) &&
817 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_OAM))
Pravin B Shelare6445712013-10-03 18:16:47 -0700818 return -EMSGSIZE;
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700819 if (swkey_tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +0100820 if (output->tun_flags & TUNNEL_GENEVE_OPT &&
821 nla_put(skb, OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS,
822 swkey_tun_opts_len, tun_opts))
823 return -EMSGSIZE;
824 else if (output->tun_flags & TUNNEL_VXLAN_OPT &&
825 vxlan_opt_to_nlattr(skb, tun_opts, swkey_tun_opts_len))
826 return -EMSGSIZE;
827 }
Jesse Grossf5796682014-10-03 15:35:33 -0700828
829 return 0;
830}
831
Jiri Benc6b26ba32015-10-05 13:09:47 +0200832static int ip_tun_to_nlattr(struct sk_buff *skb,
833 const struct ip_tunnel_key *output,
834 const void *tun_opts, int swkey_tun_opts_len,
835 unsigned short tun_proto)
Jesse Grossf5796682014-10-03 15:35:33 -0700836{
837 struct nlattr *nla;
838 int err;
839
840 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
841 if (!nla)
842 return -EMSGSIZE;
843
Jiri Benc6b26ba32015-10-05 13:09:47 +0200844 err = __ip_tun_to_nlattr(skb, output, tun_opts, swkey_tun_opts_len,
845 tun_proto);
Jesse Grossf5796682014-10-03 15:35:33 -0700846 if (err)
847 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -0700848
849 nla_nest_end(skb, nla);
850 return 0;
851}
852
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700853int ovs_nla_put_tunnel_info(struct sk_buff *skb,
854 struct ip_tunnel_info *tun_info)
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800855{
David S. Millerba3e2082015-10-24 06:54:12 -0700856 return __ip_tun_to_nlattr(skb, &tun_info->key,
857 ip_tunnel_info_opts(tun_info),
858 tun_info->options_len,
859 ip_tunnel_info_af(tun_info));
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800860}
861
Eric Garver018c1dd2016-09-07 12:56:59 -0400862static int encode_vlan_from_nlattrs(struct sw_flow_match *match,
863 const struct nlattr *a[],
864 bool is_mask, bool inner)
865{
866 __be16 tci = 0;
867 __be16 tpid = 0;
868
869 if (a[OVS_KEY_ATTR_VLAN])
870 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
871
872 if (a[OVS_KEY_ATTR_ETHERTYPE])
873 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
874
875 if (likely(!inner)) {
876 SW_FLOW_KEY_PUT(match, eth.vlan.tpid, tpid, is_mask);
877 SW_FLOW_KEY_PUT(match, eth.vlan.tci, tci, is_mask);
878 } else {
879 SW_FLOW_KEY_PUT(match, eth.cvlan.tpid, tpid, is_mask);
880 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, tci, is_mask);
881 }
882 return 0;
883}
884
885static int validate_vlan_from_nlattrs(const struct sw_flow_match *match,
886 u64 key_attrs, bool inner,
887 const struct nlattr **a, bool log)
888{
889 __be16 tci = 0;
890
891 if (!((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
892 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
893 eth_type_vlan(nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE])))) {
894 /* Not a VLAN. */
895 return 0;
896 }
897
898 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
899 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
900 OVS_NLERR(log, "Invalid %s frame", (inner) ? "C-VLAN" : "VLAN");
901 return -EINVAL;
902 }
903
904 if (a[OVS_KEY_ATTR_VLAN])
905 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
906
907 if (!(tci & htons(VLAN_TAG_PRESENT))) {
908 if (tci) {
909 OVS_NLERR(log, "%s TCI does not have VLAN_TAG_PRESENT bit set.",
910 (inner) ? "C-VLAN" : "VLAN");
911 return -EINVAL;
912 } else if (nla_len(a[OVS_KEY_ATTR_ENCAP])) {
913 /* Corner case for truncated VLAN header. */
914 OVS_NLERR(log, "Truncated %s header has non-zero encap attribute.",
915 (inner) ? "C-VLAN" : "VLAN");
916 return -EINVAL;
917 }
918 }
919
920 return 1;
921}
922
923static int validate_vlan_mask_from_nlattrs(const struct sw_flow_match *match,
924 u64 key_attrs, bool inner,
925 const struct nlattr **a, bool log)
926{
927 __be16 tci = 0;
928 __be16 tpid = 0;
929 bool encap_valid = !!(match->key->eth.vlan.tci &
930 htons(VLAN_TAG_PRESENT));
931 bool i_encap_valid = !!(match->key->eth.cvlan.tci &
932 htons(VLAN_TAG_PRESENT));
933
934 if (!(key_attrs & (1 << OVS_KEY_ATTR_ENCAP))) {
935 /* Not a VLAN. */
936 return 0;
937 }
938
939 if ((!inner && !encap_valid) || (inner && !i_encap_valid)) {
940 OVS_NLERR(log, "Encap mask attribute is set for non-%s frame.",
941 (inner) ? "C-VLAN" : "VLAN");
942 return -EINVAL;
943 }
944
945 if (a[OVS_KEY_ATTR_VLAN])
946 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
947
948 if (a[OVS_KEY_ATTR_ETHERTYPE])
949 tpid = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
950
951 if (tpid != htons(0xffff)) {
952 OVS_NLERR(log, "Must have an exact match on %s TPID (mask=%x).",
953 (inner) ? "C-VLAN" : "VLAN", ntohs(tpid));
954 return -EINVAL;
955 }
956 if (!(tci & htons(VLAN_TAG_PRESENT))) {
957 OVS_NLERR(log, "%s TCI mask does not have exact match for VLAN_TAG_PRESENT bit.",
958 (inner) ? "C-VLAN" : "VLAN");
959 return -EINVAL;
960 }
961
962 return 1;
963}
964
965static int __parse_vlan_from_nlattrs(struct sw_flow_match *match,
966 u64 *key_attrs, bool inner,
967 const struct nlattr **a, bool is_mask,
968 bool log)
969{
970 int err;
971 const struct nlattr *encap;
972
973 if (!is_mask)
974 err = validate_vlan_from_nlattrs(match, *key_attrs, inner,
975 a, log);
976 else
977 err = validate_vlan_mask_from_nlattrs(match, *key_attrs, inner,
978 a, log);
979 if (err <= 0)
980 return err;
981
982 err = encode_vlan_from_nlattrs(match, a, is_mask, inner);
983 if (err)
984 return err;
985
986 *key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
987 *key_attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
988 *key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
989
990 encap = a[OVS_KEY_ATTR_ENCAP];
991
992 if (!is_mask)
993 err = parse_flow_nlattrs(encap, a, key_attrs, log);
994 else
995 err = parse_flow_mask_nlattrs(encap, a, key_attrs, log);
996
997 return err;
998}
999
1000static int parse_vlan_from_nlattrs(struct sw_flow_match *match,
1001 u64 *key_attrs, const struct nlattr **a,
1002 bool is_mask, bool log)
1003{
1004 int err;
1005 bool encap_valid = false;
1006
1007 err = __parse_vlan_from_nlattrs(match, key_attrs, false, a,
1008 is_mask, log);
1009 if (err)
1010 return err;
1011
1012 encap_valid = !!(match->key->eth.vlan.tci & htons(VLAN_TAG_PRESENT));
1013 if (encap_valid) {
1014 err = __parse_vlan_from_nlattrs(match, key_attrs, true, a,
1015 is_mask, log);
1016 if (err)
1017 return err;
1018 }
1019
1020 return 0;
1021}
1022
Jiri Benc0a6410f2016-11-10 16:28:22 +01001023static int parse_eth_type_from_nlattrs(struct sw_flow_match *match,
1024 u64 *attrs, const struct nlattr **a,
1025 bool is_mask, bool log)
1026{
1027 __be16 eth_type;
1028
1029 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1030 if (is_mask) {
1031 /* Always exact match EtherType. */
1032 eth_type = htons(0xffff);
1033 } else if (!eth_proto_is_802_3(eth_type)) {
1034 OVS_NLERR(log, "EtherType %x is less than min %x",
1035 ntohs(eth_type), ETH_P_802_3_MIN);
1036 return -EINVAL;
1037 }
1038
1039 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1040 *attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1041 return 0;
1042}
1043
Joe Stringerc2ac6672015-08-26 11:31:52 -07001044static int metadata_from_nlattrs(struct net *net, struct sw_flow_match *match,
1045 u64 *attrs, const struct nlattr **a,
1046 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001047{
Jiri Benc0a6410f2016-11-10 16:28:22 +01001048 u8 mac_proto = MAC_PROTO_ETHERNET;
1049
Andy Zhou971427f32014-09-15 19:37:25 -07001050 if (*attrs & (1 << OVS_KEY_ATTR_DP_HASH)) {
1051 u32 hash_val = nla_get_u32(a[OVS_KEY_ATTR_DP_HASH]);
1052
1053 SW_FLOW_KEY_PUT(match, ovs_flow_hash, hash_val, is_mask);
1054 *attrs &= ~(1 << OVS_KEY_ATTR_DP_HASH);
1055 }
1056
1057 if (*attrs & (1 << OVS_KEY_ATTR_RECIRC_ID)) {
1058 u32 recirc_id = nla_get_u32(a[OVS_KEY_ATTR_RECIRC_ID]);
1059
1060 SW_FLOW_KEY_PUT(match, recirc_id, recirc_id, is_mask);
1061 *attrs &= ~(1 << OVS_KEY_ATTR_RECIRC_ID);
1062 }
1063
Pravin B Shelare6445712013-10-03 18:16:47 -07001064 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1065 SW_FLOW_KEY_PUT(match, phy.priority,
1066 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1067 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1068 }
1069
1070 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1071 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1072
Jesse Gross426cda52014-10-06 05:08:38 -07001073 if (is_mask) {
Pravin B Shelare6445712013-10-03 18:16:47 -07001074 in_port = 0xffffffff; /* Always exact match in_port. */
Jesse Gross426cda52014-10-06 05:08:38 -07001075 } else if (in_port >= DP_MAX_PORTS) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001076 OVS_NLERR(log, "Port %d exceeds max allowable %d",
Jesse Gross426cda52014-10-06 05:08:38 -07001077 in_port, DP_MAX_PORTS);
Pravin B Shelare6445712013-10-03 18:16:47 -07001078 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001079 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001080
1081 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1082 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1083 } else if (!is_mask) {
1084 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1085 }
1086
1087 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1088 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1089
1090 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1091 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1092 }
1093 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
Jiri Benc6b26ba32015-10-05 13:09:47 +02001094 if (ip_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1095 is_mask, log) < 0)
Pravin B Shelare6445712013-10-03 18:16:47 -07001096 return -EINVAL;
1097 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1098 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001099
1100 if (*attrs & (1 << OVS_KEY_ATTR_CT_STATE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001101 ovs_ct_verify(net, OVS_KEY_ATTR_CT_STATE)) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001102 u32 ct_state = nla_get_u32(a[OVS_KEY_ATTR_CT_STATE]);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001103
Joe Stringer9e384712015-10-19 19:18:57 -07001104 if (ct_state & ~CT_SUPPORTED_MASK) {
Joe Stringerfbccce52015-10-06 11:00:00 -07001105 OVS_NLERR(log, "ct_state flags %08x unsupported",
Joe Stringer6f225952015-10-06 10:59:59 -07001106 ct_state);
1107 return -EINVAL;
1108 }
Joe Stringer7f8a4362015-08-26 11:31:48 -07001109
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001110 SW_FLOW_KEY_PUT(match, ct_state, ct_state, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001111 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_STATE);
1112 }
1113 if (*attrs & (1 << OVS_KEY_ATTR_CT_ZONE) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001114 ovs_ct_verify(net, OVS_KEY_ATTR_CT_ZONE)) {
Joe Stringer7f8a4362015-08-26 11:31:48 -07001115 u16 ct_zone = nla_get_u16(a[OVS_KEY_ATTR_CT_ZONE]);
1116
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001117 SW_FLOW_KEY_PUT(match, ct_zone, ct_zone, is_mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001118 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ZONE);
1119 }
Joe Stringer182e3042015-08-26 11:31:49 -07001120 if (*attrs & (1 << OVS_KEY_ATTR_CT_MARK) &&
Joe Stringerc2ac6672015-08-26 11:31:52 -07001121 ovs_ct_verify(net, OVS_KEY_ATTR_CT_MARK)) {
Joe Stringer182e3042015-08-26 11:31:49 -07001122 u32 mark = nla_get_u32(a[OVS_KEY_ATTR_CT_MARK]);
1123
1124 SW_FLOW_KEY_PUT(match, ct.mark, mark, is_mask);
1125 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_MARK);
1126 }
Joe Stringer33db4122015-10-01 15:00:37 -07001127 if (*attrs & (1 << OVS_KEY_ATTR_CT_LABELS) &&
1128 ovs_ct_verify(net, OVS_KEY_ATTR_CT_LABELS)) {
1129 const struct ovs_key_ct_labels *cl;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001130
Joe Stringer33db4122015-10-01 15:00:37 -07001131 cl = nla_data(a[OVS_KEY_ATTR_CT_LABELS]);
1132 SW_FLOW_KEY_MEMCPY(match, ct.labels, cl->ct_labels,
Joe Stringerc2ac6672015-08-26 11:31:52 -07001133 sizeof(*cl), is_mask);
Joe Stringer33db4122015-10-01 15:00:37 -07001134 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_LABELS);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001135 }
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001136 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4)) {
1137 const struct ovs_key_ct_tuple_ipv4 *ct;
1138
1139 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4]);
1140
1141 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.src, ct->ipv4_src, is_mask);
1142 SW_FLOW_KEY_PUT(match, ipv4.ct_orig.dst, ct->ipv4_dst, is_mask);
1143 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1144 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001145 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv4_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001146 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4);
1147 }
1148 if (*attrs & (1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6)) {
1149 const struct ovs_key_ct_tuple_ipv6 *ct;
1150
1151 ct = nla_data(a[OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6]);
1152
1153 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.src, &ct->ipv6_src,
1154 sizeof(match->key->ipv6.ct_orig.src),
1155 is_mask);
1156 SW_FLOW_KEY_MEMCPY(match, ipv6.ct_orig.dst, &ct->ipv6_dst,
1157 sizeof(match->key->ipv6.ct_orig.dst),
1158 is_mask);
1159 SW_FLOW_KEY_PUT(match, ct.orig_tp.src, ct->src_port, is_mask);
1160 SW_FLOW_KEY_PUT(match, ct.orig_tp.dst, ct->dst_port, is_mask);
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001161 SW_FLOW_KEY_PUT(match, ct_orig_proto, ct->ipv6_proto, is_mask);
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001162 *attrs &= ~(1ULL << OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6);
1163 }
Jiri Benc329f45b2016-11-10 16:28:18 +01001164
Jiri Benc0a6410f2016-11-10 16:28:22 +01001165 /* For layer 3 packets the Ethernet type is provided
1166 * and treated as metadata but no MAC addresses are provided.
1167 */
1168 if (!(*attrs & (1ULL << OVS_KEY_ATTR_ETHERNET)) &&
1169 (*attrs & (1ULL << OVS_KEY_ATTR_ETHERTYPE)))
1170 mac_proto = MAC_PROTO_NONE;
1171
Jiri Benc329f45b2016-11-10 16:28:18 +01001172 /* Always exact match mac_proto */
Jiri Benc0a6410f2016-11-10 16:28:22 +01001173 SW_FLOW_KEY_PUT(match, mac_proto, is_mask ? 0xff : mac_proto, is_mask);
1174
1175 if (mac_proto == MAC_PROTO_NONE)
1176 return parse_eth_type_from_nlattrs(match, attrs, a, is_mask,
1177 log);
Jiri Benc329f45b2016-11-10 16:28:18 +01001178
Pravin B Shelare6445712013-10-03 18:16:47 -07001179 return 0;
1180}
1181
Joe Stringerc2ac6672015-08-26 11:31:52 -07001182static int ovs_key_from_nlattrs(struct net *net, struct sw_flow_match *match,
1183 u64 attrs, const struct nlattr **a,
1184 bool is_mask, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001185{
1186 int err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001187
Joe Stringerc2ac6672015-08-26 11:31:52 -07001188 err = metadata_from_nlattrs(net, match, &attrs, a, is_mask, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001189 if (err)
1190 return err;
1191
1192 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1193 const struct ovs_key_ethernet *eth_key;
1194
1195 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1196 SW_FLOW_KEY_MEMCPY(match, eth.src,
1197 eth_key->eth_src, ETH_ALEN, is_mask);
1198 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1199 eth_key->eth_dst, ETH_ALEN, is_mask);
1200 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
Pravin B Shelare6445712013-10-03 18:16:47 -07001201
Jiri Benc0a6410f2016-11-10 16:28:22 +01001202 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1203 /* VLAN attribute is always parsed before getting here since it
1204 * may occur multiple times.
1205 */
1206 OVS_NLERR(log, "VLAN attribute unexpected.");
Pravin B Shelare6445712013-10-03 18:16:47 -07001207 return -EINVAL;
1208 }
1209
Jiri Benc0a6410f2016-11-10 16:28:22 +01001210 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1211 err = parse_eth_type_from_nlattrs(match, &attrs, a, is_mask,
1212 log);
1213 if (err)
1214 return err;
1215 } else if (!is_mask) {
1216 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1217 }
1218 } else if (!match->key->eth.type) {
1219 OVS_NLERR(log, "Either Ethernet header or EtherType is required.");
1220 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001221 }
1222
1223 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1224 const struct ovs_key_ipv4 *ipv4_key;
1225
1226 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1227 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001228 OVS_NLERR(log, "IPv4 frag type %d is out of range max %d",
1229 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001230 return -EINVAL;
1231 }
1232 SW_FLOW_KEY_PUT(match, ip.proto,
1233 ipv4_key->ipv4_proto, is_mask);
1234 SW_FLOW_KEY_PUT(match, ip.tos,
1235 ipv4_key->ipv4_tos, is_mask);
1236 SW_FLOW_KEY_PUT(match, ip.ttl,
1237 ipv4_key->ipv4_ttl, is_mask);
1238 SW_FLOW_KEY_PUT(match, ip.frag,
1239 ipv4_key->ipv4_frag, is_mask);
1240 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1241 ipv4_key->ipv4_src, is_mask);
1242 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1243 ipv4_key->ipv4_dst, is_mask);
1244 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1245 }
1246
1247 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1248 const struct ovs_key_ipv6 *ipv6_key;
1249
1250 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1251 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001252 OVS_NLERR(log, "IPv6 frag type %d is out of range max %d",
1253 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
Pravin B Shelare6445712013-10-03 18:16:47 -07001254 return -EINVAL;
1255 }
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001256
Joe Stringerd3052bb2014-11-19 13:54:49 -08001257 if (!is_mask && ipv6_key->ipv6_label & htonl(0xFFF00000)) {
David S. Miller14591432014-11-21 22:28:24 -05001258 OVS_NLERR(log, "IPv6 flow label %x is out of range (max=%x).\n",
Jarno Rajahalmefecaef82014-11-11 14:36:30 -08001259 ntohl(ipv6_key->ipv6_label), (1 << 20) - 1);
1260 return -EINVAL;
1261 }
1262
Pravin B Shelare6445712013-10-03 18:16:47 -07001263 SW_FLOW_KEY_PUT(match, ipv6.label,
1264 ipv6_key->ipv6_label, is_mask);
1265 SW_FLOW_KEY_PUT(match, ip.proto,
1266 ipv6_key->ipv6_proto, is_mask);
1267 SW_FLOW_KEY_PUT(match, ip.tos,
1268 ipv6_key->ipv6_tclass, is_mask);
1269 SW_FLOW_KEY_PUT(match, ip.ttl,
1270 ipv6_key->ipv6_hlimit, is_mask);
1271 SW_FLOW_KEY_PUT(match, ip.frag,
1272 ipv6_key->ipv6_frag, is_mask);
1273 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1274 ipv6_key->ipv6_src,
1275 sizeof(match->key->ipv6.addr.src),
1276 is_mask);
1277 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1278 ipv6_key->ipv6_dst,
1279 sizeof(match->key->ipv6.addr.dst),
1280 is_mask);
1281
1282 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1283 }
1284
1285 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1286 const struct ovs_key_arp *arp_key;
1287
1288 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1289 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001290 OVS_NLERR(log, "Unknown ARP opcode (opcode=%d).",
Pravin B Shelare6445712013-10-03 18:16:47 -07001291 arp_key->arp_op);
1292 return -EINVAL;
1293 }
1294
1295 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1296 arp_key->arp_sip, is_mask);
1297 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1298 arp_key->arp_tip, is_mask);
1299 SW_FLOW_KEY_PUT(match, ip.proto,
1300 ntohs(arp_key->arp_op), is_mask);
1301 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1302 arp_key->arp_sha, ETH_ALEN, is_mask);
1303 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1304 arp_key->arp_tha, ETH_ALEN, is_mask);
1305
1306 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1307 }
1308
Simon Horman25cd9ba2014-10-06 05:05:13 -07001309 if (attrs & (1 << OVS_KEY_ATTR_MPLS)) {
1310 const struct ovs_key_mpls *mpls_key;
1311
1312 mpls_key = nla_data(a[OVS_KEY_ATTR_MPLS]);
1313 SW_FLOW_KEY_PUT(match, mpls.top_lse,
1314 mpls_key->mpls_lse, is_mask);
1315
1316 attrs &= ~(1 << OVS_KEY_ATTR_MPLS);
1317 }
1318
Pravin B Shelare6445712013-10-03 18:16:47 -07001319 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1320 const struct ovs_key_tcp *tcp_key;
1321
1322 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001323 SW_FLOW_KEY_PUT(match, tp.src, tcp_key->tcp_src, is_mask);
1324 SW_FLOW_KEY_PUT(match, tp.dst, tcp_key->tcp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001325 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1326 }
1327
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001328 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
Joe Stringer1b760fb2014-09-07 22:11:08 -07001329 SW_FLOW_KEY_PUT(match, tp.flags,
1330 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
1331 is_mask);
Jarno Rajahalme5eb26b12013-10-23 01:44:59 -07001332 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
1333 }
1334
Pravin B Shelare6445712013-10-03 18:16:47 -07001335 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1336 const struct ovs_key_udp *udp_key;
1337
1338 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001339 SW_FLOW_KEY_PUT(match, tp.src, udp_key->udp_src, is_mask);
1340 SW_FLOW_KEY_PUT(match, tp.dst, udp_key->udp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001341 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1342 }
1343
1344 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1345 const struct ovs_key_sctp *sctp_key;
1346
1347 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001348 SW_FLOW_KEY_PUT(match, tp.src, sctp_key->sctp_src, is_mask);
1349 SW_FLOW_KEY_PUT(match, tp.dst, sctp_key->sctp_dst, is_mask);
Pravin B Shelare6445712013-10-03 18:16:47 -07001350 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1351 }
1352
1353 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1354 const struct ovs_key_icmp *icmp_key;
1355
1356 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001357 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001358 htons(icmp_key->icmp_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001359 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001360 htons(icmp_key->icmp_code), is_mask);
1361 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1362 }
1363
1364 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1365 const struct ovs_key_icmpv6 *icmpv6_key;
1366
1367 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001368 SW_FLOW_KEY_PUT(match, tp.src,
Pravin B Shelare6445712013-10-03 18:16:47 -07001369 htons(icmpv6_key->icmpv6_type), is_mask);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001370 SW_FLOW_KEY_PUT(match, tp.dst,
Pravin B Shelare6445712013-10-03 18:16:47 -07001371 htons(icmpv6_key->icmpv6_code), is_mask);
1372 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1373 }
1374
1375 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1376 const struct ovs_key_nd *nd_key;
1377
1378 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1379 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1380 nd_key->nd_target,
1381 sizeof(match->key->ipv6.nd.target),
1382 is_mask);
1383 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1384 nd_key->nd_sll, ETH_ALEN, is_mask);
1385 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1386 nd_key->nd_tll, ETH_ALEN, is_mask);
1387 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1388 }
1389
Jesse Gross426cda52014-10-06 05:08:38 -07001390 if (attrs != 0) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001391 OVS_NLERR(log, "Unknown key attributes %llx",
Jesse Gross426cda52014-10-06 05:08:38 -07001392 (unsigned long long)attrs);
Pravin B Shelare6445712013-10-03 18:16:47 -07001393 return -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001394 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001395
1396 return 0;
1397}
1398
Thomas Graf81bfe3c2015-01-15 03:53:58 +01001399static void nlattr_set(struct nlattr *attr, u8 val,
1400 const struct ovs_len_tbl *tbl)
Pravin B Shelare6445712013-10-03 18:16:47 -07001401{
Pravin B Shelarf47de062014-10-16 21:55:45 -07001402 struct nlattr *nla;
1403 int rem;
Pravin B Shelare6445712013-10-03 18:16:47 -07001404
Pravin B Shelarf47de062014-10-16 21:55:45 -07001405 /* The nlattr stream should already have been validated */
1406 nla_for_each_nested(nla, attr, rem) {
Jesse Gross982b5272015-09-11 18:38:28 -07001407 if (tbl[nla_type(nla)].len == OVS_ATTR_NESTED) {
1408 if (tbl[nla_type(nla)].next)
1409 tbl = tbl[nla_type(nla)].next;
1410 nlattr_set(nla, val, tbl);
1411 } else {
Pravin B Shelarf47de062014-10-16 21:55:45 -07001412 memset(nla_data(nla), val, nla_len(nla));
Jesse Gross982b5272015-09-11 18:38:28 -07001413 }
Joe Stringer9e384712015-10-19 19:18:57 -07001414
1415 if (nla_type(nla) == OVS_KEY_ATTR_CT_STATE)
1416 *(u32 *)nla_data(nla) &= CT_SUPPORTED_MASK;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001417 }
1418}
1419
1420static void mask_set_nlattr(struct nlattr *attr, u8 val)
1421{
Thomas Graf81bfe3c2015-01-15 03:53:58 +01001422 nlattr_set(attr, val, ovs_key_lens);
Pravin B Shelare6445712013-10-03 18:16:47 -07001423}
1424
1425/**
1426 * ovs_nla_get_match - parses Netlink attributes into a flow key and
1427 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1428 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1429 * does not include any don't care bit.
Joe Stringerc2ac6672015-08-26 11:31:52 -07001430 * @net: Used to determine per-namespace field support.
Pravin B Shelare6445712013-10-03 18:16:47 -07001431 * @match: receives the extracted flow match information.
1432 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1433 * sequence. The fields should of the packet that triggered the creation
1434 * of this flow.
1435 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1436 * attribute specifies the mask field of the wildcarded flow.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001437 * @log: Boolean to allow kernel error logging. Normally true, but when
1438 * probing for feature compatibility this should be passed in as false to
1439 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001440 */
Joe Stringerc2ac6672015-08-26 11:31:52 -07001441int ovs_nla_get_match(struct net *net, struct sw_flow_match *match,
Pravin B Shelara85311b2014-10-19 12:03:40 -07001442 const struct nlattr *nla_key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001443 const struct nlattr *nla_mask,
1444 bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001445{
1446 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
Pravin B Shelarf47de062014-10-16 21:55:45 -07001447 struct nlattr *newmask = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001448 u64 key_attrs = 0;
1449 u64 mask_attrs = 0;
Pravin B Shelare6445712013-10-03 18:16:47 -07001450 int err;
1451
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001452 err = parse_flow_nlattrs(nla_key, a, &key_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001453 if (err)
1454 return err;
1455
Eric Garver018c1dd2016-09-07 12:56:59 -04001456 err = parse_vlan_from_nlattrs(match, &key_attrs, a, false, log);
1457 if (err)
1458 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001459
Joe Stringerc2ac6672015-08-26 11:31:52 -07001460 err = ovs_key_from_nlattrs(net, match, key_attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001461 if (err)
1462 return err;
1463
Pravin B Shelara85311b2014-10-19 12:03:40 -07001464 if (match->mask) {
1465 if (!nla_mask) {
1466 /* Create an exact match mask. We need to set to 0xff
1467 * all the 'match->mask' fields that have been touched
1468 * in 'match->key'. We cannot simply memset
1469 * 'match->mask', because padding bytes and fields not
1470 * specified in 'match->key' should be left to 0.
1471 * Instead, we use a stream of netlink attributes,
1472 * copied from 'key' and set to 0xff.
1473 * ovs_key_from_nlattrs() will take care of filling
1474 * 'match->mask' appropriately.
1475 */
1476 newmask = kmemdup(nla_key,
1477 nla_total_size(nla_len(nla_key)),
1478 GFP_KERNEL);
1479 if (!newmask)
1480 return -ENOMEM;
Pravin B Shelarf47de062014-10-16 21:55:45 -07001481
Pravin B Shelara85311b2014-10-19 12:03:40 -07001482 mask_set_nlattr(newmask, 0xff);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001483
Pravin B Shelara85311b2014-10-19 12:03:40 -07001484 /* The userspace does not send tunnel attributes that
1485 * are 0, but we should not wildcard them nonetheless.
1486 */
Jiri Benc00a93ba2015-10-05 13:09:46 +02001487 if (match->key->tun_proto)
Pravin B Shelara85311b2014-10-19 12:03:40 -07001488 SW_FLOW_KEY_MEMSET_FIELD(match, tun_key,
1489 0xff, true);
Pravin B Shelarf47de062014-10-16 21:55:45 -07001490
Pravin B Shelara85311b2014-10-19 12:03:40 -07001491 nla_mask = newmask;
1492 }
Pravin B Shelarf47de062014-10-16 21:55:45 -07001493
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001494 err = parse_flow_mask_nlattrs(nla_mask, a, &mask_attrs, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001495 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001496 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001497
Pravin B Shelara85311b2014-10-19 12:03:40 -07001498 /* Always match on tci. */
Eric Garver018c1dd2016-09-07 12:56:59 -04001499 SW_FLOW_KEY_PUT(match, eth.vlan.tci, htons(0xffff), true);
1500 SW_FLOW_KEY_PUT(match, eth.cvlan.tci, htons(0xffff), true);
Pravin B Shelara85311b2014-10-19 12:03:40 -07001501
Eric Garver018c1dd2016-09-07 12:56:59 -04001502 err = parse_vlan_from_nlattrs(match, &mask_attrs, a, true, log);
1503 if (err)
1504 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001505
Joe Stringerc2ac6672015-08-26 11:31:52 -07001506 err = ovs_key_from_nlattrs(net, match, mask_attrs, a, true,
1507 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001508 if (err)
Pravin B Shelarf47de062014-10-16 21:55:45 -07001509 goto free_newmask;
Pravin B Shelare6445712013-10-03 18:16:47 -07001510 }
1511
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001512 if (!match_validate(match, key_attrs, mask_attrs, log))
Pravin B Shelarf47de062014-10-16 21:55:45 -07001513 err = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001514
Pravin B Shelarf47de062014-10-16 21:55:45 -07001515free_newmask:
1516 kfree(newmask);
1517 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07001518}
1519
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001520static size_t get_ufid_len(const struct nlattr *attr, bool log)
1521{
1522 size_t len;
1523
1524 if (!attr)
1525 return 0;
1526
1527 len = nla_len(attr);
1528 if (len < 1 || len > MAX_UFID_LENGTH) {
1529 OVS_NLERR(log, "ufid size %u bytes exceeds the range (1, %d)",
1530 nla_len(attr), MAX_UFID_LENGTH);
1531 return 0;
1532 }
1533
1534 return len;
1535}
1536
1537/* Initializes 'flow->ufid', returning true if 'attr' contains a valid UFID,
1538 * or false otherwise.
1539 */
1540bool ovs_nla_get_ufid(struct sw_flow_id *sfid, const struct nlattr *attr,
1541 bool log)
1542{
1543 sfid->ufid_len = get_ufid_len(attr, log);
1544 if (sfid->ufid_len)
1545 memcpy(sfid->ufid, nla_data(attr), sfid->ufid_len);
1546
1547 return sfid->ufid_len;
1548}
1549
1550int ovs_nla_get_identifier(struct sw_flow_id *sfid, const struct nlattr *ufid,
1551 const struct sw_flow_key *key, bool log)
1552{
1553 struct sw_flow_key *new_key;
1554
1555 if (ovs_nla_get_ufid(sfid, ufid, log))
1556 return 0;
1557
1558 /* If UFID was not provided, use unmasked key. */
1559 new_key = kmalloc(sizeof(*new_key), GFP_KERNEL);
1560 if (!new_key)
1561 return -ENOMEM;
1562 memcpy(new_key, key, sizeof(*key));
1563 sfid->unmasked_key = new_key;
1564
1565 return 0;
1566}
1567
1568u32 ovs_nla_get_ufid_flags(const struct nlattr *attr)
1569{
1570 return attr ? nla_get_u32(attr) : 0;
1571}
1572
Pravin B Shelare6445712013-10-03 18:16:47 -07001573/**
1574 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001575 * @net: Network namespace.
1576 * @key: Receives extracted in_port, priority, tun_key, skb_mark and conntrack
1577 * metadata.
1578 * @a: Array of netlink attributes holding parsed %OVS_KEY_ATTR_* Netlink
1579 * attributes.
1580 * @attrs: Bit mask for the netlink attributes included in @a.
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001581 * @log: Boolean to allow kernel error logging. Normally true, but when
1582 * probing for feature compatibility this should be passed in as false to
1583 * suppress unnecessary error logging.
Pravin B Shelare6445712013-10-03 18:16:47 -07001584 *
1585 * This parses a series of Netlink attributes that form a flow key, which must
1586 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1587 * get the metadata, that is, the parts of the flow key that cannot be
1588 * extracted from the packet itself.
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001589 *
1590 * This must be called before the packet key fields are filled in 'key'.
Pravin B Shelare6445712013-10-03 18:16:47 -07001591 */
1592
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001593int ovs_nla_get_flow_metadata(struct net *net,
1594 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1],
1595 u64 attrs, struct sw_flow_key *key, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001596{
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001597 struct sw_flow_match match;
Pravin B Shelare6445712013-10-03 18:16:47 -07001598
1599 memset(&match, 0, sizeof(match));
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001600 match.key = key;
Pravin B Shelare6445712013-10-03 18:16:47 -07001601
Jarno Rajahalme316d4d72017-02-09 11:22:01 -08001602 key->ct_state = 0;
1603 key->ct_zone = 0;
1604 key->ct_orig_proto = 0;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001605 memset(&key->ct, 0, sizeof(key->ct));
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001606 memset(&key->ipv4.ct_orig, 0, sizeof(key->ipv4.ct_orig));
1607 memset(&key->ipv6.ct_orig, 0, sizeof(key->ipv6.ct_orig));
1608
Pravin B Shelar83c8df22014-09-15 19:20:31 -07001609 key->phy.in_port = DP_MAX_PORTS;
Pravin B Shelare6445712013-10-03 18:16:47 -07001610
Joe Stringerc2ac6672015-08-26 11:31:52 -07001611 return metadata_from_nlattrs(net, &match, &attrs, a, false, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001612}
1613
Eric Garver018c1dd2016-09-07 12:56:59 -04001614static int ovs_nla_put_vlan(struct sk_buff *skb, const struct vlan_head *vh,
1615 bool is_mask)
1616{
1617 __be16 eth_type = !is_mask ? vh->tpid : htons(0xffff);
1618
1619 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1620 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, vh->tci))
1621 return -EMSGSIZE;
1622 return 0;
1623}
1624
Joe Stringer5b4237b2015-01-21 16:42:48 -08001625static int __ovs_nla_put_key(const struct sw_flow_key *swkey,
1626 const struct sw_flow_key *output, bool is_mask,
1627 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07001628{
1629 struct ovs_key_ethernet *eth_key;
Eric Garver018c1dd2016-09-07 12:56:59 -04001630 struct nlattr *nla;
1631 struct nlattr *encap = NULL;
1632 struct nlattr *in_encap = NULL;
Pravin B Shelare6445712013-10-03 18:16:47 -07001633
Andy Zhou971427f32014-09-15 19:37:25 -07001634 if (nla_put_u32(skb, OVS_KEY_ATTR_RECIRC_ID, output->recirc_id))
1635 goto nla_put_failure;
1636
1637 if (nla_put_u32(skb, OVS_KEY_ATTR_DP_HASH, output->ovs_flow_hash))
1638 goto nla_put_failure;
1639
Pravin B Shelare6445712013-10-03 18:16:47 -07001640 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1641 goto nla_put_failure;
1642
Jiri Benc00a93ba2015-10-05 13:09:46 +02001643 if ((swkey->tun_proto || is_mask)) {
Thomas Grafd91641d2015-01-15 03:53:57 +01001644 const void *opts = NULL;
Jesse Grossf5796682014-10-03 15:35:33 -07001645
1646 if (output->tun_key.tun_flags & TUNNEL_OPTIONS_PRESENT)
Thomas Grafd91641d2015-01-15 03:53:57 +01001647 opts = TUN_METADATA_OPTS(output, swkey->tun_opts_len);
Jesse Grossf5796682014-10-03 15:35:33 -07001648
Jiri Benc6b26ba32015-10-05 13:09:47 +02001649 if (ip_tun_to_nlattr(skb, &output->tun_key, opts,
1650 swkey->tun_opts_len, swkey->tun_proto))
Jesse Grossf5796682014-10-03 15:35:33 -07001651 goto nla_put_failure;
1652 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001653
1654 if (swkey->phy.in_port == DP_MAX_PORTS) {
1655 if (is_mask && (output->phy.in_port == 0xffff))
1656 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1657 goto nla_put_failure;
1658 } else {
1659 u16 upper_u16;
1660 upper_u16 = !is_mask ? 0 : 0xffff;
1661
1662 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1663 (upper_u16 << 16) | output->phy.in_port))
1664 goto nla_put_failure;
1665 }
1666
1667 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1668 goto nla_put_failure;
1669
Jarno Rajahalme9dd7f892017-02-09 11:21:59 -08001670 if (ovs_ct_put_key(swkey, output, skb))
Joe Stringer7f8a4362015-08-26 11:31:48 -07001671 goto nla_put_failure;
1672
Jiri Benc0a6410f2016-11-10 16:28:22 +01001673 if (ovs_key_mac_proto(swkey) == MAC_PROTO_ETHERNET) {
1674 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1675 if (!nla)
Pravin B Shelare6445712013-10-03 18:16:47 -07001676 goto nla_put_failure;
Eric Garver018c1dd2016-09-07 12:56:59 -04001677
Jiri Benc0a6410f2016-11-10 16:28:22 +01001678 eth_key = nla_data(nla);
1679 ether_addr_copy(eth_key->eth_src, output->eth.src);
1680 ether_addr_copy(eth_key->eth_dst, output->eth.dst);
1681
1682 if (swkey->eth.vlan.tci || eth_type_vlan(swkey->eth.type)) {
1683 if (ovs_nla_put_vlan(skb, &output->eth.vlan, is_mask))
Eric Garver018c1dd2016-09-07 12:56:59 -04001684 goto nla_put_failure;
Jiri Benc0a6410f2016-11-10 16:28:22 +01001685 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1686 if (!swkey->eth.vlan.tci)
Eric Garver018c1dd2016-09-07 12:56:59 -04001687 goto unencap;
Pravin B Shelare6445712013-10-03 18:16:47 -07001688
Jiri Benc0a6410f2016-11-10 16:28:22 +01001689 if (swkey->eth.cvlan.tci || eth_type_vlan(swkey->eth.type)) {
1690 if (ovs_nla_put_vlan(skb, &output->eth.cvlan, is_mask))
1691 goto nla_put_failure;
1692 in_encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1693 if (!swkey->eth.cvlan.tci)
1694 goto unencap;
1695 }
1696 }
1697
1698 if (swkey->eth.type == htons(ETH_P_802_2)) {
1699 /*
1700 * Ethertype 802.2 is represented in the netlink with omitted
1701 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1702 * 0xffff in the mask attribute. Ethertype can also
1703 * be wildcarded.
1704 */
1705 if (is_mask && output->eth.type)
1706 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1707 output->eth.type))
1708 goto nla_put_failure;
1709 goto unencap;
1710 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001711 }
1712
1713 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1714 goto nla_put_failure;
1715
Eric Garver018c1dd2016-09-07 12:56:59 -04001716 if (eth_type_vlan(swkey->eth.type)) {
1717 /* There are 3 VLAN tags, we don't know anything about the rest
1718 * of the packet, so truncate here.
1719 */
1720 WARN_ON_ONCE(!(encap && in_encap));
1721 goto unencap;
1722 }
1723
Pravin B Shelare6445712013-10-03 18:16:47 -07001724 if (swkey->eth.type == htons(ETH_P_IP)) {
1725 struct ovs_key_ipv4 *ipv4_key;
1726
1727 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1728 if (!nla)
1729 goto nla_put_failure;
1730 ipv4_key = nla_data(nla);
1731 ipv4_key->ipv4_src = output->ipv4.addr.src;
1732 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1733 ipv4_key->ipv4_proto = output->ip.proto;
1734 ipv4_key->ipv4_tos = output->ip.tos;
1735 ipv4_key->ipv4_ttl = output->ip.ttl;
1736 ipv4_key->ipv4_frag = output->ip.frag;
1737 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1738 struct ovs_key_ipv6 *ipv6_key;
1739
1740 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1741 if (!nla)
1742 goto nla_put_failure;
1743 ipv6_key = nla_data(nla);
1744 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1745 sizeof(ipv6_key->ipv6_src));
1746 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1747 sizeof(ipv6_key->ipv6_dst));
1748 ipv6_key->ipv6_label = output->ipv6.label;
1749 ipv6_key->ipv6_proto = output->ip.proto;
1750 ipv6_key->ipv6_tclass = output->ip.tos;
1751 ipv6_key->ipv6_hlimit = output->ip.ttl;
1752 ipv6_key->ipv6_frag = output->ip.frag;
1753 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1754 swkey->eth.type == htons(ETH_P_RARP)) {
1755 struct ovs_key_arp *arp_key;
1756
1757 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1758 if (!nla)
1759 goto nla_put_failure;
1760 arp_key = nla_data(nla);
1761 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1762 arp_key->arp_sip = output->ipv4.addr.src;
1763 arp_key->arp_tip = output->ipv4.addr.dst;
1764 arp_key->arp_op = htons(output->ip.proto);
Joe Perches8c63ff02014-02-18 11:15:45 -08001765 ether_addr_copy(arp_key->arp_sha, output->ipv4.arp.sha);
1766 ether_addr_copy(arp_key->arp_tha, output->ipv4.arp.tha);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001767 } else if (eth_p_mpls(swkey->eth.type)) {
1768 struct ovs_key_mpls *mpls_key;
1769
1770 nla = nla_reserve(skb, OVS_KEY_ATTR_MPLS, sizeof(*mpls_key));
1771 if (!nla)
1772 goto nla_put_failure;
1773 mpls_key = nla_data(nla);
1774 mpls_key->mpls_lse = output->mpls.top_lse;
Pravin B Shelare6445712013-10-03 18:16:47 -07001775 }
1776
1777 if ((swkey->eth.type == htons(ETH_P_IP) ||
1778 swkey->eth.type == htons(ETH_P_IPV6)) &&
1779 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1780
1781 if (swkey->ip.proto == IPPROTO_TCP) {
1782 struct ovs_key_tcp *tcp_key;
1783
1784 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1785 if (!nla)
1786 goto nla_put_failure;
1787 tcp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001788 tcp_key->tcp_src = output->tp.src;
1789 tcp_key->tcp_dst = output->tp.dst;
1790 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1791 output->tp.flags))
1792 goto nla_put_failure;
Pravin B Shelare6445712013-10-03 18:16:47 -07001793 } else if (swkey->ip.proto == IPPROTO_UDP) {
1794 struct ovs_key_udp *udp_key;
1795
1796 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1797 if (!nla)
1798 goto nla_put_failure;
1799 udp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001800 udp_key->udp_src = output->tp.src;
1801 udp_key->udp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001802 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1803 struct ovs_key_sctp *sctp_key;
1804
1805 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1806 if (!nla)
1807 goto nla_put_failure;
1808 sctp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001809 sctp_key->sctp_src = output->tp.src;
1810 sctp_key->sctp_dst = output->tp.dst;
Pravin B Shelare6445712013-10-03 18:16:47 -07001811 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1812 swkey->ip.proto == IPPROTO_ICMP) {
1813 struct ovs_key_icmp *icmp_key;
1814
1815 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1816 if (!nla)
1817 goto nla_put_failure;
1818 icmp_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001819 icmp_key->icmp_type = ntohs(output->tp.src);
1820 icmp_key->icmp_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001821 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1822 swkey->ip.proto == IPPROTO_ICMPV6) {
1823 struct ovs_key_icmpv6 *icmpv6_key;
1824
1825 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1826 sizeof(*icmpv6_key));
1827 if (!nla)
1828 goto nla_put_failure;
1829 icmpv6_key = nla_data(nla);
Jarno Rajahalme1139e242014-05-05 09:54:49 -07001830 icmpv6_key->icmpv6_type = ntohs(output->tp.src);
1831 icmpv6_key->icmpv6_code = ntohs(output->tp.dst);
Pravin B Shelare6445712013-10-03 18:16:47 -07001832
1833 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1834 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1835 struct ovs_key_nd *nd_key;
1836
1837 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1838 if (!nla)
1839 goto nla_put_failure;
1840 nd_key = nla_data(nla);
1841 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1842 sizeof(nd_key->nd_target));
Joe Perches8c63ff02014-02-18 11:15:45 -08001843 ether_addr_copy(nd_key->nd_sll, output->ipv6.nd.sll);
1844 ether_addr_copy(nd_key->nd_tll, output->ipv6.nd.tll);
Pravin B Shelare6445712013-10-03 18:16:47 -07001845 }
1846 }
1847 }
1848
1849unencap:
Eric Garver018c1dd2016-09-07 12:56:59 -04001850 if (in_encap)
1851 nla_nest_end(skb, in_encap);
Pravin B Shelare6445712013-10-03 18:16:47 -07001852 if (encap)
1853 nla_nest_end(skb, encap);
1854
1855 return 0;
1856
1857nla_put_failure:
1858 return -EMSGSIZE;
1859}
1860
Joe Stringer5b4237b2015-01-21 16:42:48 -08001861int ovs_nla_put_key(const struct sw_flow_key *swkey,
1862 const struct sw_flow_key *output, int attr, bool is_mask,
1863 struct sk_buff *skb)
1864{
1865 int err;
1866 struct nlattr *nla;
1867
1868 nla = nla_nest_start(skb, attr);
1869 if (!nla)
1870 return -EMSGSIZE;
1871 err = __ovs_nla_put_key(swkey, output, is_mask, skb);
1872 if (err)
1873 return err;
1874 nla_nest_end(skb, nla);
1875
1876 return 0;
1877}
1878
1879/* Called with ovs_mutex or RCU read lock. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001880int ovs_nla_put_identifier(const struct sw_flow *flow, struct sk_buff *skb)
Joe Stringer5b4237b2015-01-21 16:42:48 -08001881{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001882 if (ovs_identifier_is_ufid(&flow->id))
1883 return nla_put(skb, OVS_FLOW_ATTR_UFID, flow->id.ufid_len,
1884 flow->id.ufid);
1885
1886 return ovs_nla_put_key(flow->id.unmasked_key, flow->id.unmasked_key,
1887 OVS_FLOW_ATTR_KEY, false, skb);
1888}
1889
1890/* Called with ovs_mutex or RCU read lock. */
1891int ovs_nla_put_masked_key(const struct sw_flow *flow, struct sk_buff *skb)
1892{
Pravin B Shelar26ad0b82015-02-12 09:58:48 -08001893 return ovs_nla_put_key(&flow->key, &flow->key,
Joe Stringer5b4237b2015-01-21 16:42:48 -08001894 OVS_FLOW_ATTR_KEY, false, skb);
1895}
1896
1897/* Called with ovs_mutex or RCU read lock. */
1898int ovs_nla_put_mask(const struct sw_flow *flow, struct sk_buff *skb)
1899{
1900 return ovs_nla_put_key(&flow->key, &flow->mask->key,
1901 OVS_FLOW_ATTR_MASK, true, skb);
1902}
1903
Pravin B Shelare6445712013-10-03 18:16:47 -07001904#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1905
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001906static struct sw_flow_actions *nla_alloc_flow_actions(int size, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001907{
1908 struct sw_flow_actions *sfa;
1909
Jesse Gross426cda52014-10-06 05:08:38 -07001910 if (size > MAX_ACTIONS_BUFSIZE) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001911 OVS_NLERR(log, "Flow action size %u bytes exceeds max", size);
Pravin B Shelare6445712013-10-03 18:16:47 -07001912 return ERR_PTR(-EINVAL);
Jesse Gross426cda52014-10-06 05:08:38 -07001913 }
Pravin B Shelare6445712013-10-03 18:16:47 -07001914
1915 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1916 if (!sfa)
1917 return ERR_PTR(-ENOMEM);
1918
1919 sfa->actions_len = 0;
1920 return sfa;
1921}
1922
Thomas Graf34ae9322015-07-21 10:44:03 +02001923static void ovs_nla_free_set_action(const struct nlattr *a)
1924{
1925 const struct nlattr *ovs_key = nla_data(a);
1926 struct ovs_tunnel_info *ovs_tun;
1927
1928 switch (nla_type(ovs_key)) {
1929 case OVS_KEY_ATTR_TUNNEL_INFO:
1930 ovs_tun = nla_data(ovs_key);
1931 dst_release((struct dst_entry *)ovs_tun->tun_dst);
1932 break;
1933 }
1934}
1935
Pravin B Shelare6445712013-10-03 18:16:47 -07001936void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1937{
Thomas Graf34ae9322015-07-21 10:44:03 +02001938 const struct nlattr *a;
1939 int rem;
1940
1941 if (!sf_acts)
1942 return;
1943
1944 nla_for_each_attr(a, sf_acts->actions, sf_acts->actions_len, rem) {
1945 switch (nla_type(a)) {
1946 case OVS_ACTION_ATTR_SET:
1947 ovs_nla_free_set_action(a);
1948 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07001949 case OVS_ACTION_ATTR_CT:
1950 ovs_ct_free_action(a);
1951 break;
Thomas Graf34ae9322015-07-21 10:44:03 +02001952 }
1953 }
1954
1955 kfree(sf_acts);
1956}
1957
1958static void __ovs_nla_free_flow_actions(struct rcu_head *head)
1959{
1960 ovs_nla_free_flow_actions(container_of(head, struct sw_flow_actions, rcu));
1961}
1962
1963/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1964 * The caller must hold rcu_read_lock for this to be sensible. */
1965void ovs_nla_free_flow_actions_rcu(struct sw_flow_actions *sf_acts)
1966{
1967 call_rcu(&sf_acts->rcu, __ovs_nla_free_flow_actions);
Pravin B Shelare6445712013-10-03 18:16:47 -07001968}
1969
1970static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001971 int attr_len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07001972{
1973
1974 struct sw_flow_actions *acts;
1975 int new_acts_size;
1976 int req_size = NLA_ALIGN(attr_len);
1977 int next_offset = offsetof(struct sw_flow_actions, actions) +
1978 (*sfa)->actions_len;
1979
1980 if (req_size <= (ksize(*sfa) - next_offset))
1981 goto out;
1982
1983 new_acts_size = ksize(*sfa) * 2;
1984
1985 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1986 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1987 return ERR_PTR(-EMSGSIZE);
1988 new_acts_size = MAX_ACTIONS_BUFSIZE;
1989 }
1990
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001991 acts = nla_alloc_flow_actions(new_acts_size, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07001992 if (IS_ERR(acts))
1993 return (void *)acts;
1994
1995 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1996 acts->actions_len = (*sfa)->actions_len;
Joe Stringer8e2fed12015-08-26 11:31:44 -07001997 acts->orig_len = (*sfa)->orig_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07001998 kfree(*sfa);
1999 *sfa = acts;
2000
2001out:
2002 (*sfa)->actions_len += req_size;
2003 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
2004}
2005
Jesse Grossf0b128c2014-10-03 15:35:31 -07002006static struct nlattr *__add_action(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002007 int attrtype, void *data, int len, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002008{
2009 struct nlattr *a;
2010
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002011 a = reserve_sfa_size(sfa, nla_attr_size(len), log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002012 if (IS_ERR(a))
Jesse Grossf0b128c2014-10-03 15:35:31 -07002013 return a;
Pravin B Shelare6445712013-10-03 18:16:47 -07002014
2015 a->nla_type = attrtype;
2016 a->nla_len = nla_attr_size(len);
2017
2018 if (data)
2019 memcpy(nla_data(a), data, len);
2020 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
2021
Jesse Grossf0b128c2014-10-03 15:35:31 -07002022 return a;
2023}
2024
Joe Stringer7f8a4362015-08-26 11:31:48 -07002025int ovs_nla_add_action(struct sw_flow_actions **sfa, int attrtype, void *data,
2026 int len, bool log)
Jesse Grossf0b128c2014-10-03 15:35:31 -07002027{
2028 struct nlattr *a;
2029
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002030 a = __add_action(sfa, attrtype, data, len, log);
Jesse Grossf0b128c2014-10-03 15:35:31 -07002031
Fabian Frederickf35423c12014-11-14 19:32:58 +01002032 return PTR_ERR_OR_ZERO(a);
Pravin B Shelare6445712013-10-03 18:16:47 -07002033}
2034
2035static inline int add_nested_action_start(struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002036 int attrtype, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002037{
2038 int used = (*sfa)->actions_len;
2039 int err;
2040
Joe Stringer7f8a4362015-08-26 11:31:48 -07002041 err = ovs_nla_add_action(sfa, attrtype, NULL, 0, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002042 if (err)
2043 return err;
2044
2045 return used;
2046}
2047
2048static inline void add_nested_action_end(struct sw_flow_actions *sfa,
2049 int st_offset)
2050{
2051 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
2052 st_offset);
2053
2054 a->nla_len = sfa->actions_len - st_offset;
2055}
2056
Joe Stringer7f8a4362015-08-26 11:31:48 -07002057static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002058 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002059 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002060 __be16 eth_type, __be16 vlan_tci, bool log);
Simon Horman25cd9ba2014-10-06 05:05:13 -07002061
Joe Stringer7f8a4362015-08-26 11:31:48 -07002062static int validate_and_copy_sample(struct net *net, const struct nlattr *attr,
andy zhou798c1662017-03-20 16:32:29 -07002063 const struct sw_flow_key *key,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002064 struct sw_flow_actions **sfa,
andy zhou798c1662017-03-20 16:32:29 -07002065 __be16 eth_type, __be16 vlan_tci,
2066 bool log, bool last)
Pravin B Shelare6445712013-10-03 18:16:47 -07002067{
2068 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
2069 const struct nlattr *probability, *actions;
2070 const struct nlattr *a;
andy zhou798c1662017-03-20 16:32:29 -07002071 int rem, start, err;
2072 struct sample_arg arg;
Pravin B Shelare6445712013-10-03 18:16:47 -07002073
2074 memset(attrs, 0, sizeof(attrs));
2075 nla_for_each_nested(a, attr, rem) {
2076 int type = nla_type(a);
2077 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
2078 return -EINVAL;
2079 attrs[type] = a;
2080 }
2081 if (rem)
2082 return -EINVAL;
2083
2084 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
2085 if (!probability || nla_len(probability) != sizeof(u32))
2086 return -EINVAL;
2087
2088 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
2089 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
2090 return -EINVAL;
2091
2092 /* validation done, copy sample action. */
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002093 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002094 if (start < 0)
2095 return start;
andy zhou798c1662017-03-20 16:32:29 -07002096
2097 /* When both skb and flow may be changed, put the sample
2098 * into a deferred fifo. On the other hand, if only skb
2099 * may be modified, the actions can be executed in place.
2100 *
2101 * Do this analysis at the flow installation time.
2102 * Set 'clone_action->exec' to true if the actions can be
2103 * executed without being deferred.
2104 *
2105 * If the sample is the last action, it can always be excuted
2106 * rather than deferred.
2107 */
2108 arg.exec = last || !actions_may_change_flow(actions);
2109 arg.probability = nla_get_u32(probability);
2110
2111 err = ovs_nla_add_action(sfa, OVS_SAMPLE_ATTR_ARG, &arg, sizeof(arg),
2112 log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002113 if (err)
2114 return err;
Pravin B Shelare6445712013-10-03 18:16:47 -07002115
andy zhou798c1662017-03-20 16:32:29 -07002116 err = __ovs_nla_copy_actions(net, actions, key, sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002117 eth_type, vlan_tci, log);
andy zhou798c1662017-03-20 16:32:29 -07002118
Pravin B Shelare6445712013-10-03 18:16:47 -07002119 if (err)
2120 return err;
2121
Pravin B Shelare6445712013-10-03 18:16:47 -07002122 add_nested_action_end(*sfa, start);
2123
2124 return 0;
2125}
2126
Pravin B Shelare6445712013-10-03 18:16:47 -07002127void ovs_match_init(struct sw_flow_match *match,
2128 struct sw_flow_key *key,
pravin shelar22799942016-09-19 13:51:00 -07002129 bool reset_key,
Pravin B Shelare6445712013-10-03 18:16:47 -07002130 struct sw_flow_mask *mask)
2131{
2132 memset(match, 0, sizeof(*match));
2133 match->key = key;
2134 match->mask = mask;
2135
pravin shelar22799942016-09-19 13:51:00 -07002136 if (reset_key)
2137 memset(key, 0, sizeof(*key));
Pravin B Shelare6445712013-10-03 18:16:47 -07002138
2139 if (mask) {
2140 memset(&mask->key, 0, sizeof(mask->key));
2141 mask->range.start = mask->range.end = 0;
2142 }
2143}
2144
Thomas Grafd91641d2015-01-15 03:53:57 +01002145static int validate_geneve_opts(struct sw_flow_key *key)
2146{
2147 struct geneve_opt *option;
2148 int opts_len = key->tun_opts_len;
2149 bool crit_opt = false;
2150
2151 option = (struct geneve_opt *)TUN_METADATA_OPTS(key, key->tun_opts_len);
2152 while (opts_len > 0) {
2153 int len;
2154
2155 if (opts_len < sizeof(*option))
2156 return -EINVAL;
2157
2158 len = sizeof(*option) + option->length * 4;
2159 if (len > opts_len)
2160 return -EINVAL;
2161
2162 crit_opt |= !!(option->type & GENEVE_CRIT_OPT_TYPE);
2163
2164 option = (struct geneve_opt *)((u8 *)option + len);
2165 opts_len -= len;
2166 };
2167
2168 key->tun_key.tun_flags |= crit_opt ? TUNNEL_CRIT_OPT : 0;
2169
2170 return 0;
2171}
2172
Pravin B Shelare6445712013-10-03 18:16:47 -07002173static int validate_and_copy_set_tun(const struct nlattr *attr,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002174 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002175{
2176 struct sw_flow_match match;
2177 struct sw_flow_key key;
Thomas Graf34ae9322015-07-21 10:44:03 +02002178 struct metadata_dst *tun_dst;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002179 struct ip_tunnel_info *tun_info;
Thomas Graf34ae9322015-07-21 10:44:03 +02002180 struct ovs_tunnel_info *ovs_tun;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002181 struct nlattr *a;
Geert Uytterhoeven13101602015-02-11 11:23:38 +01002182 int err = 0, start, opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002183
pravin shelar22799942016-09-19 13:51:00 -07002184 ovs_match_init(&match, &key, true, NULL);
Jiri Benc6b26ba32015-10-05 13:09:47 +02002185 opts_type = ip_tun_from_nlattr(nla_data(attr), &match, false, log);
Thomas Graf1dd144c2015-01-15 03:53:59 +01002186 if (opts_type < 0)
2187 return opts_type;
Pravin B Shelare6445712013-10-03 18:16:47 -07002188
Jesse Grossf5796682014-10-03 15:35:33 -07002189 if (key.tun_opts_len) {
Thomas Graf1dd144c2015-01-15 03:53:59 +01002190 switch (opts_type) {
2191 case OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS:
2192 err = validate_geneve_opts(&key);
2193 if (err < 0)
2194 return err;
2195 break;
2196 case OVS_TUNNEL_KEY_ATTR_VXLAN_OPTS:
2197 break;
2198 }
Jesse Grossf5796682014-10-03 15:35:33 -07002199 };
2200
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002201 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002202 if (start < 0)
2203 return start;
2204
Thomas Graf34ae9322015-07-21 10:44:03 +02002205 tun_dst = metadata_dst_alloc(key.tun_opts_len, GFP_KERNEL);
2206 if (!tun_dst)
2207 return -ENOMEM;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002208
Paolo Abenid71785f2016-02-12 15:43:57 +01002209 err = dst_cache_init(&tun_dst->u.tun_info.dst_cache, GFP_KERNEL);
2210 if (err) {
2211 dst_release((struct dst_entry *)tun_dst);
2212 return err;
2213 }
2214
Thomas Graf34ae9322015-07-21 10:44:03 +02002215 a = __add_action(sfa, OVS_KEY_ATTR_TUNNEL_INFO, NULL,
2216 sizeof(*ovs_tun), log);
2217 if (IS_ERR(a)) {
2218 dst_release((struct dst_entry *)tun_dst);
2219 return PTR_ERR(a);
2220 }
2221
2222 ovs_tun = nla_data(a);
2223 ovs_tun->tun_dst = tun_dst;
2224
2225 tun_info = &tun_dst->u.tun_info;
2226 tun_info->mode = IP_TUNNEL_INFO_TX;
Jiri Benc00a93ba2015-10-05 13:09:46 +02002227 if (key.tun_proto == AF_INET6)
2228 tun_info->mode |= IP_TUNNEL_INFO_IPV6;
Thomas Graf1d8fff92015-07-21 10:43:54 +02002229 tun_info->key = key.tun_key;
Jesse Grossf5796682014-10-03 15:35:33 -07002230
Pravin B Shelar4c222792015-08-30 18:09:38 -07002231 /* We need to store the options in the action itself since
2232 * everything else will go away after flow setup. We can append
2233 * it to tun_info and then point there.
2234 */
2235 ip_tunnel_info_opts_set(tun_info,
2236 TUN_METADATA_OPTS(&key, key.tun_opts_len),
2237 key.tun_opts_len);
Pravin B Shelare6445712013-10-03 18:16:47 -07002238 add_nested_action_end(*sfa, start);
2239
2240 return err;
2241}
2242
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002243/* Return false if there are any non-masked bits set.
2244 * Mask follows data immediately, before any netlink padding.
2245 */
2246static bool validate_masked(u8 *data, int len)
2247{
2248 u8 *mask = data + len;
2249
2250 while (len--)
2251 if (*data++ & ~*mask++)
2252 return false;
2253
2254 return true;
2255}
2256
Pravin B Shelare6445712013-10-03 18:16:47 -07002257static int validate_set(const struct nlattr *a,
2258 const struct sw_flow_key *flow_key,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002259 struct sw_flow_actions **sfa, bool *skip_copy,
2260 u8 mac_proto, __be16 eth_type, bool masked, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002261{
2262 const struct nlattr *ovs_key = nla_data(a);
2263 int key_type = nla_type(ovs_key);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002264 size_t key_len;
Pravin B Shelare6445712013-10-03 18:16:47 -07002265
2266 /* There can be only one key in a action */
2267 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
2268 return -EINVAL;
2269
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002270 key_len = nla_len(ovs_key);
2271 if (masked)
2272 key_len /= 2;
2273
Pravin B Shelare6445712013-10-03 18:16:47 -07002274 if (key_type > OVS_KEY_ATTR_MAX ||
Jesse Gross982b5272015-09-11 18:38:28 -07002275 !check_attr_len(key_len, ovs_key_lens[key_type].len))
Pravin B Shelare6445712013-10-03 18:16:47 -07002276 return -EINVAL;
2277
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002278 if (masked && !validate_masked(nla_data(ovs_key), key_len))
2279 return -EINVAL;
2280
Pravin B Shelare6445712013-10-03 18:16:47 -07002281 switch (key_type) {
2282 const struct ovs_key_ipv4 *ipv4_key;
2283 const struct ovs_key_ipv6 *ipv6_key;
2284 int err;
2285
2286 case OVS_KEY_ATTR_PRIORITY:
2287 case OVS_KEY_ATTR_SKB_MARK:
Joe Stringer182e3042015-08-26 11:31:49 -07002288 case OVS_KEY_ATTR_CT_MARK:
Joe Stringer33db4122015-10-01 15:00:37 -07002289 case OVS_KEY_ATTR_CT_LABELS:
Pravin B Shelare6445712013-10-03 18:16:47 -07002290 break;
2291
Jiri Benc0a6410f2016-11-10 16:28:22 +01002292 case OVS_KEY_ATTR_ETHERNET:
2293 if (mac_proto != MAC_PROTO_ETHERNET)
2294 return -EINVAL;
Jarno Rajahalme87e159c2016-12-19 17:06:33 -08002295 break;
Jiri Benc0a6410f2016-11-10 16:28:22 +01002296
Pravin B Shelare6445712013-10-03 18:16:47 -07002297 case OVS_KEY_ATTR_TUNNEL:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002298 if (masked)
2299 return -EINVAL; /* Masked tunnel set not supported. */
2300
2301 *skip_copy = true;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002302 err = validate_and_copy_set_tun(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002303 if (err)
2304 return err;
2305 break;
2306
2307 case OVS_KEY_ATTR_IPV4:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002308 if (eth_type != htons(ETH_P_IP))
Pravin B Shelare6445712013-10-03 18:16:47 -07002309 return -EINVAL;
2310
Pravin B Shelare6445712013-10-03 18:16:47 -07002311 ipv4_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002312
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002313 if (masked) {
2314 const struct ovs_key_ipv4 *mask = ipv4_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002315
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002316 /* Non-writeable fields. */
2317 if (mask->ipv4_proto || mask->ipv4_frag)
2318 return -EINVAL;
2319 } else {
2320 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
2321 return -EINVAL;
2322
2323 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
2324 return -EINVAL;
2325 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002326 break;
2327
2328 case OVS_KEY_ATTR_IPV6:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002329 if (eth_type != htons(ETH_P_IPV6))
Pravin B Shelare6445712013-10-03 18:16:47 -07002330 return -EINVAL;
2331
Pravin B Shelare6445712013-10-03 18:16:47 -07002332 ipv6_key = nla_data(ovs_key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002333
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002334 if (masked) {
2335 const struct ovs_key_ipv6 *mask = ipv6_key + 1;
Pravin B Shelare6445712013-10-03 18:16:47 -07002336
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002337 /* Non-writeable fields. */
2338 if (mask->ipv6_proto || mask->ipv6_frag)
2339 return -EINVAL;
2340
2341 /* Invalid bits in the flow label mask? */
2342 if (ntohl(mask->ipv6_label) & 0xFFF00000)
2343 return -EINVAL;
2344 } else {
2345 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
2346 return -EINVAL;
2347
2348 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
2349 return -EINVAL;
2350 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002351 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
2352 return -EINVAL;
2353
2354 break;
2355
2356 case OVS_KEY_ATTR_TCP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002357 if ((eth_type != htons(ETH_P_IP) &&
2358 eth_type != htons(ETH_P_IPV6)) ||
2359 flow_key->ip.proto != IPPROTO_TCP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002360 return -EINVAL;
2361
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002362 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002363
2364 case OVS_KEY_ATTR_UDP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002365 if ((eth_type != htons(ETH_P_IP) &&
2366 eth_type != htons(ETH_P_IPV6)) ||
2367 flow_key->ip.proto != IPPROTO_UDP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002368 return -EINVAL;
2369
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002370 break;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002371
2372 case OVS_KEY_ATTR_MPLS:
2373 if (!eth_p_mpls(eth_type))
2374 return -EINVAL;
2375 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002376
2377 case OVS_KEY_ATTR_SCTP:
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002378 if ((eth_type != htons(ETH_P_IP) &&
2379 eth_type != htons(ETH_P_IPV6)) ||
2380 flow_key->ip.proto != IPPROTO_SCTP)
Pravin B Shelare6445712013-10-03 18:16:47 -07002381 return -EINVAL;
2382
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002383 break;
Pravin B Shelare6445712013-10-03 18:16:47 -07002384
2385 default:
2386 return -EINVAL;
2387 }
2388
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002389 /* Convert non-masked non-tunnel set actions to masked set actions. */
2390 if (!masked && key_type != OVS_KEY_ATTR_TUNNEL) {
2391 int start, len = key_len * 2;
2392 struct nlattr *at;
2393
2394 *skip_copy = true;
2395
2396 start = add_nested_action_start(sfa,
2397 OVS_ACTION_ATTR_SET_TO_MASKED,
2398 log);
2399 if (start < 0)
2400 return start;
2401
2402 at = __add_action(sfa, key_type, NULL, len, log);
2403 if (IS_ERR(at))
2404 return PTR_ERR(at);
2405
2406 memcpy(nla_data(at), nla_data(ovs_key), key_len); /* Key. */
2407 memset(nla_data(at) + key_len, 0xff, key_len); /* Mask. */
2408 /* Clear non-writeable bits from otherwise writeable fields. */
2409 if (key_type == OVS_KEY_ATTR_IPV6) {
2410 struct ovs_key_ipv6 *mask = nla_data(at) + key_len;
2411
2412 mask->ipv6_label &= htonl(0x000FFFFF);
2413 }
2414 add_nested_action_end(*sfa, start);
2415 }
2416
Pravin B Shelare6445712013-10-03 18:16:47 -07002417 return 0;
2418}
2419
2420static int validate_userspace(const struct nlattr *attr)
2421{
2422 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
2423 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
2424 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Wenyu Zhang8f0aad62014-11-06 06:51:24 -08002425 [OVS_USERSPACE_ATTR_EGRESS_TUN_PORT] = {.type = NLA_U32 },
Pravin B Shelare6445712013-10-03 18:16:47 -07002426 };
2427 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
2428 int error;
2429
Johannes Bergfceb6432017-04-12 14:34:07 +02002430 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, attr,
2431 userspace_policy, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -07002432 if (error)
2433 return error;
2434
2435 if (!a[OVS_USERSPACE_ATTR_PID] ||
2436 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
2437 return -EINVAL;
2438
2439 return 0;
2440}
2441
2442static int copy_action(const struct nlattr *from,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002443 struct sw_flow_actions **sfa, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002444{
2445 int totlen = NLA_ALIGN(from->nla_len);
2446 struct nlattr *to;
2447
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002448 to = reserve_sfa_size(sfa, from->nla_len, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002449 if (IS_ERR(to))
2450 return PTR_ERR(to);
2451
2452 memcpy(to, from, totlen);
2453 return 0;
2454}
2455
Joe Stringer7f8a4362015-08-26 11:31:48 -07002456static int __ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002457 const struct sw_flow_key *key,
andy zhou798c1662017-03-20 16:32:29 -07002458 struct sw_flow_actions **sfa,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002459 __be16 eth_type, __be16 vlan_tci, bool log)
Pravin B Shelare6445712013-10-03 18:16:47 -07002460{
Jiri Benc0a6410f2016-11-10 16:28:22 +01002461 u8 mac_proto = ovs_key_mac_proto(key);
Pravin B Shelare6445712013-10-03 18:16:47 -07002462 const struct nlattr *a;
2463 int rem, err;
2464
Pravin B Shelare6445712013-10-03 18:16:47 -07002465 nla_for_each_nested(a, attr, rem) {
2466 /* Expected argument lengths, (u32)-1 for variable length. */
2467 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
2468 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
Andy Zhou971427f32014-09-15 19:37:25 -07002469 [OVS_ACTION_ATTR_RECIRC] = sizeof(u32),
Pravin B Shelare6445712013-10-03 18:16:47 -07002470 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002471 [OVS_ACTION_ATTR_PUSH_MPLS] = sizeof(struct ovs_action_push_mpls),
2472 [OVS_ACTION_ATTR_POP_MPLS] = sizeof(__be16),
Pravin B Shelare6445712013-10-03 18:16:47 -07002473 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
2474 [OVS_ACTION_ATTR_POP_VLAN] = 0,
2475 [OVS_ACTION_ATTR_SET] = (u32)-1,
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002476 [OVS_ACTION_ATTR_SET_MASKED] = (u32)-1,
Andy Zhou971427f32014-09-15 19:37:25 -07002477 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1,
Joe Stringer7f8a4362015-08-26 11:31:48 -07002478 [OVS_ACTION_ATTR_HASH] = sizeof(struct ovs_action_hash),
2479 [OVS_ACTION_ATTR_CT] = (u32)-1,
William Tuf2a4d082016-06-10 11:49:33 -07002480 [OVS_ACTION_ATTR_TRUNC] = sizeof(struct ovs_action_trunc),
Jiri Benc91820da2016-11-10 16:28:23 +01002481 [OVS_ACTION_ATTR_PUSH_ETH] = sizeof(struct ovs_action_push_eth),
2482 [OVS_ACTION_ATTR_POP_ETH] = 0,
Pravin B Shelare6445712013-10-03 18:16:47 -07002483 };
2484 const struct ovs_action_push_vlan *vlan;
2485 int type = nla_type(a);
2486 bool skip_copy;
2487
2488 if (type > OVS_ACTION_ATTR_MAX ||
2489 (action_lens[type] != nla_len(a) &&
2490 action_lens[type] != (u32)-1))
2491 return -EINVAL;
2492
2493 skip_copy = false;
2494 switch (type) {
2495 case OVS_ACTION_ATTR_UNSPEC:
2496 return -EINVAL;
2497
2498 case OVS_ACTION_ATTR_USERSPACE:
2499 err = validate_userspace(a);
2500 if (err)
2501 return err;
2502 break;
2503
2504 case OVS_ACTION_ATTR_OUTPUT:
2505 if (nla_get_u32(a) >= DP_MAX_PORTS)
2506 return -EINVAL;
2507 break;
2508
William Tuf2a4d082016-06-10 11:49:33 -07002509 case OVS_ACTION_ATTR_TRUNC: {
2510 const struct ovs_action_trunc *trunc = nla_data(a);
2511
2512 if (trunc->max_len < ETH_HLEN)
2513 return -EINVAL;
2514 break;
2515 }
2516
Andy Zhou971427f32014-09-15 19:37:25 -07002517 case OVS_ACTION_ATTR_HASH: {
2518 const struct ovs_action_hash *act_hash = nla_data(a);
2519
2520 switch (act_hash->hash_alg) {
2521 case OVS_HASH_ALG_L4:
2522 break;
2523 default:
2524 return -EINVAL;
2525 }
2526
2527 break;
2528 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002529
2530 case OVS_ACTION_ATTR_POP_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01002531 if (mac_proto != MAC_PROTO_ETHERNET)
2532 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002533 vlan_tci = htons(0);
Pravin B Shelare6445712013-10-03 18:16:47 -07002534 break;
2535
2536 case OVS_ACTION_ATTR_PUSH_VLAN:
Jiri Benc0a6410f2016-11-10 16:28:22 +01002537 if (mac_proto != MAC_PROTO_ETHERNET)
2538 return -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -07002539 vlan = nla_data(a);
Eric Garver018c1dd2016-09-07 12:56:59 -04002540 if (!eth_type_vlan(vlan->vlan_tpid))
Pravin B Shelare6445712013-10-03 18:16:47 -07002541 return -EINVAL;
2542 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
2543 return -EINVAL;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002544 vlan_tci = vlan->vlan_tci;
Pravin B Shelare6445712013-10-03 18:16:47 -07002545 break;
2546
Andy Zhou971427f32014-09-15 19:37:25 -07002547 case OVS_ACTION_ATTR_RECIRC:
2548 break;
2549
Simon Horman25cd9ba2014-10-06 05:05:13 -07002550 case OVS_ACTION_ATTR_PUSH_MPLS: {
2551 const struct ovs_action_push_mpls *mpls = nla_data(a);
2552
Simon Horman25cd9ba2014-10-06 05:05:13 -07002553 if (!eth_p_mpls(mpls->mpls_ethertype))
2554 return -EINVAL;
2555 /* Prohibit push MPLS other than to a white list
2556 * for packets that have a known tag order.
2557 */
2558 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2559 (eth_type != htons(ETH_P_IP) &&
2560 eth_type != htons(ETH_P_IPV6) &&
2561 eth_type != htons(ETH_P_ARP) &&
2562 eth_type != htons(ETH_P_RARP) &&
2563 !eth_p_mpls(eth_type)))
2564 return -EINVAL;
2565 eth_type = mpls->mpls_ethertype;
2566 break;
2567 }
2568
2569 case OVS_ACTION_ATTR_POP_MPLS:
2570 if (vlan_tci & htons(VLAN_TAG_PRESENT) ||
2571 !eth_p_mpls(eth_type))
2572 return -EINVAL;
2573
2574 /* Disallow subsequent L2.5+ set and mpls_pop actions
2575 * as there is no check here to ensure that the new
2576 * eth_type is valid and thus set actions could
2577 * write off the end of the packet or otherwise
2578 * corrupt it.
2579 *
2580 * Support for these actions is planned using packet
2581 * recirculation.
2582 */
2583 eth_type = htons(0);
2584 break;
2585
Pravin B Shelare6445712013-10-03 18:16:47 -07002586 case OVS_ACTION_ATTR_SET:
Simon Horman25cd9ba2014-10-06 05:05:13 -07002587 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002588 &skip_copy, mac_proto, eth_type,
2589 false, log);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002590 if (err)
2591 return err;
2592 break;
2593
2594 case OVS_ACTION_ATTR_SET_MASKED:
2595 err = validate_set(a, key, sfa,
Jiri Benc0a6410f2016-11-10 16:28:22 +01002596 &skip_copy, mac_proto, eth_type,
2597 true, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002598 if (err)
2599 return err;
2600 break;
2601
andy zhou798c1662017-03-20 16:32:29 -07002602 case OVS_ACTION_ATTR_SAMPLE: {
2603 bool last = nla_is_last(a, rem);
2604
2605 err = validate_and_copy_sample(net, a, key, sfa,
2606 eth_type, vlan_tci,
2607 log, last);
Pravin B Shelare6445712013-10-03 18:16:47 -07002608 if (err)
2609 return err;
2610 skip_copy = true;
2611 break;
andy zhou798c1662017-03-20 16:32:29 -07002612 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002613
Joe Stringer7f8a4362015-08-26 11:31:48 -07002614 case OVS_ACTION_ATTR_CT:
2615 err = ovs_ct_copy_action(net, a, key, sfa, log);
2616 if (err)
2617 return err;
2618 skip_copy = true;
2619 break;
2620
Jiri Benc91820da2016-11-10 16:28:23 +01002621 case OVS_ACTION_ATTR_PUSH_ETH:
2622 /* Disallow pushing an Ethernet header if one
2623 * is already present */
2624 if (mac_proto != MAC_PROTO_NONE)
2625 return -EINVAL;
2626 mac_proto = MAC_PROTO_NONE;
2627 break;
2628
2629 case OVS_ACTION_ATTR_POP_ETH:
2630 if (mac_proto != MAC_PROTO_ETHERNET)
2631 return -EINVAL;
2632 if (vlan_tci & htons(VLAN_TAG_PRESENT))
2633 return -EINVAL;
2634 mac_proto = MAC_PROTO_ETHERNET;
2635 break;
2636
Pravin B Shelare6445712013-10-03 18:16:47 -07002637 default:
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002638 OVS_NLERR(log, "Unknown Action type %d", type);
Pravin B Shelare6445712013-10-03 18:16:47 -07002639 return -EINVAL;
2640 }
2641 if (!skip_copy) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002642 err = copy_action(a, sfa, log);
Pravin B Shelare6445712013-10-03 18:16:47 -07002643 if (err)
2644 return err;
2645 }
2646 }
2647
2648 if (rem > 0)
2649 return -EINVAL;
2650
2651 return 0;
2652}
2653
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002654/* 'key' must be the masked key. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07002655int ovs_nla_copy_actions(struct net *net, const struct nlattr *attr,
Simon Horman25cd9ba2014-10-06 05:05:13 -07002656 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002657 struct sw_flow_actions **sfa, bool log)
Simon Horman25cd9ba2014-10-06 05:05:13 -07002658{
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002659 int err;
2660
Jarno Rajahalme05da5892014-11-06 07:03:05 -08002661 *sfa = nla_alloc_flow_actions(nla_len(attr), log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002662 if (IS_ERR(*sfa))
2663 return PTR_ERR(*sfa);
2664
Joe Stringer8e2fed12015-08-26 11:31:44 -07002665 (*sfa)->orig_len = nla_len(attr);
andy zhou798c1662017-03-20 16:32:29 -07002666 err = __ovs_nla_copy_actions(net, attr, key, sfa, key->eth.type,
Eric Garver018c1dd2016-09-07 12:56:59 -04002667 key->eth.vlan.tci, log);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002668 if (err)
Thomas Graf34ae9322015-07-21 10:44:03 +02002669 ovs_nla_free_flow_actions(*sfa);
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07002670
2671 return err;
Simon Horman25cd9ba2014-10-06 05:05:13 -07002672}
2673
andy zhou798c1662017-03-20 16:32:29 -07002674static int sample_action_to_attr(const struct nlattr *attr,
2675 struct sk_buff *skb)
Pravin B Shelare6445712013-10-03 18:16:47 -07002676{
andy zhou798c1662017-03-20 16:32:29 -07002677 struct nlattr *start, *ac_start = NULL, *sample_arg;
2678 int err = 0, rem = nla_len(attr);
2679 const struct sample_arg *arg;
2680 struct nlattr *actions;
Pravin B Shelare6445712013-10-03 18:16:47 -07002681
2682 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
2683 if (!start)
2684 return -EMSGSIZE;
2685
andy zhou798c1662017-03-20 16:32:29 -07002686 sample_arg = nla_data(attr);
2687 arg = nla_data(sample_arg);
2688 actions = nla_next(sample_arg, &rem);
Pravin B Shelare6445712013-10-03 18:16:47 -07002689
andy zhou798c1662017-03-20 16:32:29 -07002690 if (nla_put_u32(skb, OVS_SAMPLE_ATTR_PROBABILITY, arg->probability)) {
2691 err = -EMSGSIZE;
2692 goto out;
Pravin B Shelare6445712013-10-03 18:16:47 -07002693 }
2694
andy zhou798c1662017-03-20 16:32:29 -07002695 ac_start = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
2696 if (!ac_start) {
2697 err = -EMSGSIZE;
2698 goto out;
2699 }
2700
2701 err = ovs_nla_put_actions(actions, rem, skb);
2702
2703out:
2704 if (err) {
2705 nla_nest_cancel(skb, ac_start);
2706 nla_nest_cancel(skb, start);
2707 } else {
2708 nla_nest_end(skb, ac_start);
2709 nla_nest_end(skb, start);
2710 }
2711
Pravin B Shelare6445712013-10-03 18:16:47 -07002712 return err;
2713}
2714
2715static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
2716{
2717 const struct nlattr *ovs_key = nla_data(a);
2718 int key_type = nla_type(ovs_key);
2719 struct nlattr *start;
2720 int err;
2721
2722 switch (key_type) {
Jesse Grossf0b128c2014-10-03 15:35:31 -07002723 case OVS_KEY_ATTR_TUNNEL_INFO: {
Thomas Graf34ae9322015-07-21 10:44:03 +02002724 struct ovs_tunnel_info *ovs_tun = nla_data(ovs_key);
2725 struct ip_tunnel_info *tun_info = &ovs_tun->tun_dst->u.tun_info;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002726
Pravin B Shelare6445712013-10-03 18:16:47 -07002727 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2728 if (!start)
2729 return -EMSGSIZE;
2730
Simon Hormane905eab2015-12-18 19:43:15 +09002731 err = ip_tun_to_nlattr(skb, &tun_info->key,
2732 ip_tunnel_info_opts(tun_info),
2733 tun_info->options_len,
2734 ip_tunnel_info_af(tun_info));
Pravin B Shelare6445712013-10-03 18:16:47 -07002735 if (err)
2736 return err;
2737 nla_nest_end(skb, start);
2738 break;
Jesse Grossf0b128c2014-10-03 15:35:31 -07002739 }
Pravin B Shelare6445712013-10-03 18:16:47 -07002740 default:
2741 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
2742 return -EMSGSIZE;
2743 break;
2744 }
2745
2746 return 0;
2747}
2748
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002749static int masked_set_action_to_set_action_attr(const struct nlattr *a,
2750 struct sk_buff *skb)
2751{
2752 const struct nlattr *ovs_key = nla_data(a);
Joe Stringerf4f8e732015-03-02 18:49:56 -08002753 struct nlattr *nla;
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002754 size_t key_len = nla_len(ovs_key) / 2;
2755
2756 /* Revert the conversion we did from a non-masked set action to
2757 * masked set action.
2758 */
Joe Stringerf4f8e732015-03-02 18:49:56 -08002759 nla = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
2760 if (!nla)
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002761 return -EMSGSIZE;
2762
Joe Stringerf4f8e732015-03-02 18:49:56 -08002763 if (nla_put(skb, nla_type(ovs_key), key_len, nla_data(ovs_key)))
2764 return -EMSGSIZE;
2765
2766 nla_nest_end(skb, nla);
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002767 return 0;
2768}
2769
Pravin B Shelare6445712013-10-03 18:16:47 -07002770int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
2771{
2772 const struct nlattr *a;
2773 int rem, err;
2774
2775 nla_for_each_attr(a, attr, len, rem) {
2776 int type = nla_type(a);
2777
2778 switch (type) {
2779 case OVS_ACTION_ATTR_SET:
2780 err = set_action_to_attr(a, skb);
2781 if (err)
2782 return err;
2783 break;
2784
Jarno Rajahalme83d2b9b2015-02-05 13:40:49 -08002785 case OVS_ACTION_ATTR_SET_TO_MASKED:
2786 err = masked_set_action_to_set_action_attr(a, skb);
2787 if (err)
2788 return err;
2789 break;
2790
Pravin B Shelare6445712013-10-03 18:16:47 -07002791 case OVS_ACTION_ATTR_SAMPLE:
2792 err = sample_action_to_attr(a, skb);
2793 if (err)
2794 return err;
2795 break;
Joe Stringer7f8a4362015-08-26 11:31:48 -07002796
2797 case OVS_ACTION_ATTR_CT:
2798 err = ovs_ct_action_to_attr(nla_data(a), skb);
2799 if (err)
2800 return err;
2801 break;
2802
Pravin B Shelare6445712013-10-03 18:16:47 -07002803 default:
2804 if (nla_put(skb, type, nla_len(a), nla_data(a)))
2805 return -EMSGSIZE;
2806 break;
2807 }
2808 }
2809
2810 return 0;
2811}