]>
Commit | Line | Data |
---|---|---|
f22716dc | 1 | /* |
50f96b10 | 2 | * Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Nicira, Inc. |
f22716dc JP |
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> | |
0d71302e | 18 | #include "openvswitch/ofp-parse.h" |
f22716dc | 19 | #include <errno.h> |
10a24935 | 20 | #include "byte-order.h" |
0d71302e | 21 | #include "openvswitch/match.h" |
b598f214 | 22 | #include "openvswitch/meta-flow.h" |
93ca7e55 | 23 | #include "openvswitch/ofp-actions.h" |
0d71302e BP |
24 | #include "openvswitch/ofp-flow.h" |
25 | #include "openvswitch/ofp-match.h" | |
26 | #include "openvswitch/ofp-table.h" | |
f22716dc JP |
27 | #include "packets.h" |
28 | #include "socket-util.h" | |
ee89ea7b | 29 | #include "util.h" |
f22716dc | 30 | |
bdda5aca BP |
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. */ | |
cab50449 | 37 | char * OVS_WARN_UNUSED_RESULT |
bdda5aca | 38 | str_to_u8(const char *str, const char *name, uint8_t *valuep) |
c3636ffc | 39 | { |
638a19b0 | 40 | int value; |
c3636ffc | 41 | |
bdda5aca BP |
42 | if (!str_to_int(str, 0, &value) || value < 0 || value > 255) { |
43 | return xasprintf("invalid %s \"%s\"", name, str); | |
c3636ffc | 44 | } |
bdda5aca BP |
45 | *valuep = value; |
46 | return NULL; | |
c3636ffc BP |
47 | } |
48 | ||
bdda5aca BP |
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. */ | |
cab50449 | 55 | char * OVS_WARN_UNUSED_RESULT |
bdda5aca | 56 | str_to_u16(const char *str, const char *name, uint16_t *valuep) |
c3636ffc BP |
57 | { |
58 | int value; | |
59 | ||
60 | if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) { | |
bdda5aca | 61 | return xasprintf("invalid %s \"%s\"", name, str); |
c3636ffc | 62 | } |
bdda5aca BP |
63 | *valuep = value; |
64 | return NULL; | |
c3636ffc BP |
65 | } |
66 | ||
bdda5aca BP |
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. */ | |
cab50449 | 71 | char * OVS_WARN_UNUSED_RESULT |
bdda5aca | 72 | str_to_u32(const char *str, uint32_t *valuep) |
f22716dc JP |
73 | { |
74 | char *tail; | |
75 | uint32_t value; | |
76 | ||
c4894ed4 | 77 | if (!str[0]) { |
bdda5aca | 78 | return xstrdup("missing required numeric argument"); |
ce5452cf EJ |
79 | } |
80 | ||
f22716dc JP |
81 | errno = 0; |
82 | value = strtoul(str, &tail, 0); | |
83 | if (errno == EINVAL || errno == ERANGE || *tail) { | |
bdda5aca | 84 | return xasprintf("invalid numeric format %s", str); |
f22716dc | 85 | } |
bdda5aca BP |
86 | *valuep = value; |
87 | return NULL; | |
f22716dc JP |
88 | } |
89 | ||
bdda5aca BP |
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. */ | |
cab50449 | 94 | char * OVS_WARN_UNUSED_RESULT |
bdda5aca | 95 | str_to_u64(const char *str, uint64_t *valuep) |
f22716dc JP |
96 | { |
97 | char *tail; | |
98 | uint64_t value; | |
99 | ||
c4894ed4 | 100 | if (!str[0]) { |
bdda5aca | 101 | return xstrdup("missing required numeric argument"); |
c4894ed4 BP |
102 | } |
103 | ||
f22716dc JP |
104 | errno = 0; |
105 | value = strtoull(str, &tail, 0); | |
106 | if (errno == EINVAL || errno == ERANGE || *tail) { | |
bdda5aca | 107 | return xasprintf("invalid numeric format %s", str); |
f22716dc | 108 | } |
bdda5aca BP |
109 | *valuep = value; |
110 | return NULL; | |
f22716dc JP |
111 | } |
112 | ||
bdda5aca BP |
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. */ | |
cab50449 | 118 | char * OVS_WARN_UNUSED_RESULT |
bdda5aca BP |
119 | str_to_be64(const char *str, ovs_be64 *valuep) |
120 | { | |
4be17953 | 121 | uint64_t value = 0; |
bdda5aca BP |
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. */ | |
cab50449 | 135 | char * OVS_WARN_UNUSED_RESULT |
74ff3298 | 136 | str_to_mac(const char *str, struct eth_addr *mac) |
f22716dc | 137 | { |
74ff3298 | 138 | if (!ovs_scan(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(*mac))) { |
bdda5aca | 139 | return xasprintf("invalid mac address %s", str); |
f22716dc | 140 | } |
bdda5aca | 141 | return NULL; |
f22716dc JP |
142 | } |
143 | ||
bdda5aca BP |
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. */ | |
cab50449 | 148 | char * OVS_WARN_UNUSED_RESULT |
6a885fd0 | 149 | str_to_ip(const char *str, ovs_be32 *ip) |
cb8ca532 | 150 | { |
f22716dc | 151 | struct in_addr in_addr; |
f22716dc | 152 | |
6a885fd0 | 153 | if (lookup_ip(str, &in_addr)) { |
bdda5aca | 154 | return xasprintf("%s: could not convert to IP address", str); |
f22716dc JP |
155 | } |
156 | *ip = in_addr.s_addr; | |
bdda5aca | 157 | return NULL; |
d31f1109 JP |
158 | } |
159 | ||
d787ad39 JS |
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; | |
40c7b2fc JS |
170 | } |
171 | if (!strcmp(str, "tftp")) { | |
172 | *alg = IPPORT_TFTP; | |
173 | return NULL; | |
d787ad39 JS |
174 | } |
175 | return xasprintf("invalid conntrack helper \"%s\"", str); | |
176 | } | |
177 | ||
0d71302e BP |
178 | bool |
179 | ofp_parse_protocol(const char *name, const struct ofp_protocol **p_out) | |
f22716dc | 180 | { |
0d71302e | 181 | static const struct ofp_protocol protocols[] = { |
f22716dc | 182 | { "ip", ETH_TYPE_IP, 0 }, |
64cd4a05 JP |
183 | { "ipv4", ETH_TYPE_IP, 0 }, |
184 | { "ip4", ETH_TYPE_IP, 0 }, | |
f22716dc | 185 | { "arp", ETH_TYPE_ARP, 0 }, |
6767a2cc JP |
186 | { "icmp", ETH_TYPE_IP, IPPROTO_ICMP }, |
187 | { "tcp", ETH_TYPE_IP, IPPROTO_TCP }, | |
188 | { "udp", ETH_TYPE_IP, IPPROTO_UDP }, | |
0d56eaf2 | 189 | { "sctp", ETH_TYPE_IP, IPPROTO_SCTP }, |
d31f1109 JP |
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 }, | |
0d56eaf2 | 195 | { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP }, |
8087f5ff | 196 | { "rarp", ETH_TYPE_RARP, 0}, |
b02475c5 SH |
197 | { "mpls", ETH_TYPE_MPLS, 0 }, |
198 | { "mplsm", ETH_TYPE_MPLS_MCAST, 0 }, | |
199 | }; | |
0d71302e | 200 | const struct ofp_protocol *p; |
f22716dc JP |
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 | ||
bdda5aca | 212 | /* Parses 's' as the (possibly masked) value of field 'mf', and updates |
db0b6c29 JR |
213 | * 'match' appropriately. Restricts the set of usable protocols to ones |
214 | * supporting the parsed field. | |
bdda5aca BP |
215 | * |
216 | * Returns NULL if successful, otherwise a malloc()'d string describing the | |
217 | * error. The caller is responsible for freeing the returned string. */ | |
0d71302e BP |
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) | |
8050b31d | 222 | { |
6a885fd0 BP |
223 | union mf_value value, mask; |
224 | char *error; | |
bad68a99 | 225 | |
0eede6b6 JG |
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 | ||
50f96b10 | 232 | error = mf_parse(mf, s, port_map, &value, &mask); |
bdda5aca | 233 | if (!error) { |
4f7b100c | 234 | *usable_protocols &= mf_set(mf, &value, &mask, match, &error); |
3d4b2e6e | 235 | match_add_ethernet_prereq(match, mf); |
8050b31d | 236 | } |
bdda5aca | 237 | return error; |
00b1c62f BP |
238 | } |
239 | ||
0d71302e BP |
240 | char * |
241 | ofp_extract_actions(char *s) | |
c2d936a4 BP |
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 | } | |
0d71302e BP |
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; | |
45952551 | 275 | } |
0d71302e BP |
276 | n++; |
277 | } while (level > 0); | |
7395c052 | 278 | } else { |
0d71302e | 279 | n++; |
7395c052 | 280 | } |
7395c052 | 281 | } |
0d71302e | 282 | return n; |
7395c052 | 283 | } |
6159c531 | 284 | |
0d71302e | 285 | /* Parses a key or a key-value pair from '*stringp'. |
25070e04 | 286 | * |
0d71302e BP |
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; | |
25070e04 JR |
331 | } |
332 | ||
0d71302e BP |
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]; | |
5b34f8fc NS |
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 | } | |
0d71302e BP |
348 | value[value_len] = '\0'; |
349 | *stringp += value_len + (value_delim != '\0'); | |
25070e04 | 350 | |
0d71302e BP |
351 | *keyp = key; |
352 | *valuep = value; | |
353 | return true; | |
6159c531 | 354 | } |