]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ofp-parse.c
cirrus: Use FreeBSD 12.2.
[mirror_ovs.git] / lib / ofp-parse.c
1 /*
2 * Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "openvswitch/ofp-parse.h"
19 #include <errno.h>
20 #include "byte-order.h"
21 #include "openvswitch/match.h"
22 #include "openvswitch/meta-flow.h"
23 #include "openvswitch/ofp-actions.h"
24 #include "openvswitch/ofp-flow.h"
25 #include "openvswitch/ofp-match.h"
26 #include "openvswitch/ofp-table.h"
27 #include "packets.h"
28 #include "socket-util.h"
29 #include "util.h"
30
31 /* Parses 'str' as an 8-bit unsigned integer into '*valuep'.
32 *
33 * 'name' describes the value parsed in an error message, if any.
34 *
35 * Returns NULL if successful, otherwise a malloc()'d string describing the
36 * error. The caller is responsible for freeing the returned string. */
37 char * OVS_WARN_UNUSED_RESULT
38 str_to_u8(const char *str, const char *name, uint8_t *valuep)
39 {
40 int value;
41
42 if (!str_to_int(str, 0, &value) || value < 0 || value > 255) {
43 return xasprintf("invalid %s \"%s\"", name, str);
44 }
45 *valuep = value;
46 return NULL;
47 }
48
49 /* Parses 'str' as a 16-bit unsigned integer into '*valuep'.
50 *
51 * 'name' describes the value parsed in an error message, if any.
52 *
53 * Returns NULL if successful, otherwise a malloc()'d string describing the
54 * error. The caller is responsible for freeing the returned string. */
55 char * OVS_WARN_UNUSED_RESULT
56 str_to_u16(const char *str, const char *name, uint16_t *valuep)
57 {
58 int value;
59
60 if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
61 return xasprintf("invalid %s \"%s\"", name, str);
62 }
63 *valuep = value;
64 return NULL;
65 }
66
67 /* Parses 'str' as a 32-bit unsigned integer into '*valuep'.
68 *
69 * Returns NULL if successful, otherwise a malloc()'d string describing the
70 * error. The caller is responsible for freeing the returned string. */
71 char * OVS_WARN_UNUSED_RESULT
72 str_to_u32(const char *str, uint32_t *valuep)
73 {
74 char *tail;
75 uint32_t value;
76
77 if (!str[0]) {
78 return xstrdup("missing required numeric argument");
79 }
80
81 errno = 0;
82 value = strtoul(str, &tail, 0);
83 if (errno == EINVAL || errno == ERANGE || *tail) {
84 return xasprintf("invalid numeric format %s", str);
85 }
86 *valuep = value;
87 return NULL;
88 }
89
90 /* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
91 *
92 * Returns NULL if successful, otherwise a malloc()'d string describing the
93 * error. The caller is responsible for freeing the returned string. */
94 char * OVS_WARN_UNUSED_RESULT
95 str_to_u64(const char *str, uint64_t *valuep)
96 {
97 char *tail;
98 uint64_t value;
99
100 if (!str[0]) {
101 return xstrdup("missing required numeric argument");
102 }
103
104 errno = 0;
105 value = strtoull(str, &tail, 0);
106 if (errno == EINVAL || errno == ERANGE || *tail) {
107 return xasprintf("invalid numeric format %s", str);
108 }
109 *valuep = value;
110 return NULL;
111 }
112
113 /* Parses 'str' as an 64-bit unsigned integer in network byte order into
114 * '*valuep'.
115 *
116 * Returns NULL if successful, otherwise a malloc()'d string describing the
117 * error. The caller is responsible for freeing the returned string. */
118 char * OVS_WARN_UNUSED_RESULT
119 str_to_be64(const char *str, ovs_be64 *valuep)
120 {
121 uint64_t value = 0;
122 char *error;
123
124 error = str_to_u64(str, &value);
125 if (!error) {
126 *valuep = htonll(value);
127 }
128 return error;
129 }
130
131 /* Parses 'str' as an Ethernet address into 'mac'.
132 *
133 * Returns NULL if successful, otherwise a malloc()'d string describing the
134 * error. The caller is responsible for freeing the returned string. */
135 char * OVS_WARN_UNUSED_RESULT
136 str_to_mac(const char *str, struct eth_addr *mac)
137 {
138 if (!ovs_scan(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(*mac))) {
139 return xasprintf("invalid mac address %s", str);
140 }
141 return NULL;
142 }
143
144 /* Parses 'str' as an IP address into '*ip'.
145 *
146 * Returns NULL if successful, otherwise a malloc()'d string describing the
147 * error. The caller is responsible for freeing the returned string. */
148 char * OVS_WARN_UNUSED_RESULT
149 str_to_ip(const char *str, ovs_be32 *ip)
150 {
151 struct in_addr in_addr;
152
153 if (lookup_ip(str, &in_addr)) {
154 return xasprintf("%s: could not convert to IP address", str);
155 }
156 *ip = in_addr.s_addr;
157 return NULL;
158 }
159
160 /* Parses 'str' as a conntrack helper into 'alg'.
161 *
162 * Returns NULL if successful, otherwise a malloc()'d string describing the
163 * error. The caller is responsible for freeing the returned string. */
164 char * OVS_WARN_UNUSED_RESULT
165 str_to_connhelper(const char *str, uint16_t *alg)
166 {
167 if (!strcmp(str, "ftp")) {
168 *alg = IPPORT_FTP;
169 return NULL;
170 }
171 if (!strcmp(str, "tftp")) {
172 *alg = IPPORT_TFTP;
173 return NULL;
174 }
175 return xasprintf("invalid conntrack helper \"%s\"", str);
176 }
177
178 bool
179 ofp_parse_protocol(const char *name, const struct ofp_protocol **p_out)
180 {
181 static const struct ofp_protocol protocols[] = {
182 { "ip", ETH_TYPE_IP, 0 },
183 { "ipv4", ETH_TYPE_IP, 0 },
184 { "ip4", ETH_TYPE_IP, 0 },
185 { "arp", ETH_TYPE_ARP, 0 },
186 { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
187 { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
188 { "udp", ETH_TYPE_IP, IPPROTO_UDP },
189 { "sctp", ETH_TYPE_IP, IPPROTO_SCTP },
190 { "ipv6", ETH_TYPE_IPV6, 0 },
191 { "ip6", ETH_TYPE_IPV6, 0 },
192 { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
193 { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
194 { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
195 { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP },
196 { "rarp", ETH_TYPE_RARP, 0},
197 { "mpls", ETH_TYPE_MPLS, 0 },
198 { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
199 };
200 const struct ofp_protocol *p;
201
202 for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
203 if (!strcmp(p->name, name)) {
204 *p_out = p;
205 return true;
206 }
207 }
208 *p_out = NULL;
209 return false;
210 }
211
212 /* Parses 's' as the (possibly masked) value of field 'mf', and updates
213 * 'match' appropriately. Restricts the set of usable protocols to ones
214 * supporting the parsed field.
215 *
216 * Returns NULL if successful, otherwise a malloc()'d string describing the
217 * error. The caller is responsible for freeing the returned string. */
218 char * OVS_WARN_UNUSED_RESULT
219 ofp_parse_field(const struct mf_field *mf, const char *s,
220 const struct ofputil_port_map *port_map, struct match *match,
221 enum ofputil_protocol *usable_protocols)
222 {
223 union mf_value value, mask;
224 char *error;
225
226 if (!*s) {
227 /* If there's no string, we're just trying to match on the
228 * existence of the field, so use a no-op value. */
229 s = "0/0";
230 }
231
232 error = mf_parse(mf, s, port_map, &value, &mask);
233 if (!error) {
234 *usable_protocols &= mf_set(mf, &value, &mask, match, &error);
235 match_add_ethernet_prereq(match, mf);
236 }
237 return error;
238 }
239
240 char *
241 ofp_extract_actions(char *s)
242 {
243 s = strstr(s, "action");
244 if (s) {
245 *s = '\0';
246 s = strchr(s + 1, '=');
247 return s ? s + 1 : NULL;
248 } else {
249 return NULL;
250 }
251 }
252 \f
253 static size_t
254 parse_value(const char *s, const char *delimiters)
255 {
256 size_t n = 0;
257
258 /* Iterate until we reach a delimiter.
259 *
260 * strchr(s, '\0') returns s+strlen(s), so this test handles the null
261 * terminator at the end of 's'. */
262 while (!strchr(delimiters, s[n])) {
263 if (s[n] == '(') {
264 int level = 0;
265 do {
266 switch (s[n]) {
267 case '\0':
268 return n;
269 case '(':
270 level++;
271 break;
272 case ')':
273 level--;
274 break;
275 }
276 n++;
277 } while (level > 0);
278 } else {
279 n++;
280 }
281 }
282 return n;
283 }
284
285 /* Parses a key or a key-value pair from '*stringp'.
286 *
287 * On success: Stores the key into '*keyp'. Stores the value, if present, into
288 * '*valuep', otherwise an empty string. Advances '*stringp' past the end of
289 * the key-value pair, preparing it for another call. '*keyp' and '*valuep'
290 * are substrings of '*stringp' created by replacing some of its bytes by null
291 * terminators. Returns true.
292 *
293 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
294 * NULL and returns false. */
295 bool
296 ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
297 {
298 /* Skip white space and delimiters. If that brings us to the end of the
299 * input string, we are done and there are no more key-value pairs. */
300 *stringp += strspn(*stringp, ", \t\r\n");
301 if (**stringp == '\0') {
302 *keyp = *valuep = NULL;
303 return false;
304 }
305
306 /* Extract the key and the delimiter that ends the key-value pair or begins
307 * the value. Advance the input position past the key and delimiter. */
308 char *key = *stringp;
309 size_t key_len = strcspn(key, ":=(, \t\r\n");
310 char key_delim = key[key_len];
311 key[key_len] = '\0';
312 *stringp += key_len + (key_delim != '\0');
313
314 /* Figure out what delimiter ends the value:
315 *
316 * - If key_delim is ":" or "=", the value extends until white space
317 * or a comma.
318 *
319 * - If key_delim is "(", the value extends until ")".
320 *
321 * If there is no value, we are done. */
322 const char *value_delims;
323 if (key_delim == ':' || key_delim == '=') {
324 value_delims = ", \t\r\n";
325 } else if (key_delim == '(') {
326 value_delims = ")";
327 } else {
328 *keyp = key;
329 *valuep = key + key_len; /* Empty string. */
330 return true;
331 }
332
333 /* Extract the value. Advance the input position past the value and
334 * delimiter. */
335 char *value = *stringp;
336 size_t value_len = parse_value(value, value_delims);
337 char value_delim = value[value_len];
338
339 /* Handle the special case if the value is of the form "(x)->y".
340 * After parsing, 'valuep' will be pointing to - "x)->y".
341 * */
342 if (key_delim == '(' && value[value_len] == ')' &&
343 value[value_len + 1] == '-' && value[value_len + 2] == '>') {
344 value_delims = ", \t\r\n";
345 value_len += parse_value(&value[value_len], value_delims);
346 value_delim = value[value_len];
347 }
348 value[value_len] = '\0';
349 *stringp += value_len + (value_delim != '\0');
350
351 *keyp = key;
352 *valuep = value;
353 return true;
354 }