/src/openvswitch/lib/ofp-parse.c
Line | Count | Source (jump to first uncovered line) |
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 | 9.53k | { |
40 | 9.53k | int value; |
41 | | |
42 | 9.53k | if (!str_to_int(str, 0, &value) || value < 0 || value > 255) { |
43 | 75 | return xasprintf("invalid %s \"%s\"", name, str); |
44 | 75 | } |
45 | 9.45k | *valuep = value; |
46 | 9.45k | return NULL; |
47 | 9.53k | } |
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 | 13.5k | { |
58 | 13.5k | int value; |
59 | | |
60 | 13.5k | if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) { |
61 | 288 | return xasprintf("invalid %s \"%s\"", name, str); |
62 | 288 | } |
63 | 13.2k | *valuep = value; |
64 | 13.2k | return NULL; |
65 | 13.5k | } |
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 | 4.64k | { |
74 | 4.64k | char *tail; |
75 | 4.64k | uint32_t value; |
76 | | |
77 | 4.64k | if (!str[0]) { |
78 | 56 | return xstrdup("missing required numeric argument"); |
79 | 56 | } |
80 | | |
81 | 4.59k | errno = 0; |
82 | 4.59k | value = strtoul(str, &tail, 0); |
83 | 4.59k | if (errno == EINVAL || errno == ERANGE || *tail) { |
84 | 20 | return xasprintf("invalid numeric format %s", str); |
85 | 20 | } |
86 | 4.57k | *valuep = value; |
87 | 4.57k | return NULL; |
88 | 4.59k | } |
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 | 4.82k | { |
97 | 4.82k | char *tail; |
98 | 4.82k | uint64_t value; |
99 | | |
100 | 4.82k | if (!str[0]) { |
101 | 30 | return xstrdup("missing required numeric argument"); |
102 | 30 | } |
103 | | |
104 | 4.79k | errno = 0; |
105 | 4.79k | value = strtoull(str, &tail, 0); |
106 | 4.79k | if (errno == EINVAL || errno == ERANGE || *tail) { |
107 | 39 | return xasprintf("invalid numeric format %s", str); |
108 | 39 | } |
109 | 4.76k | *valuep = value; |
110 | 4.76k | return NULL; |
111 | 4.79k | } |
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 | 3.53k | { |
121 | 3.53k | uint64_t value = 0; |
122 | 3.53k | char *error; |
123 | | |
124 | 3.53k | error = str_to_u64(str, &value); |
125 | 3.53k | if (!error) { |
126 | 3.48k | *valuep = htonll(value); |
127 | 3.48k | } |
128 | 3.53k | return error; |
129 | 3.53k | } |
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 | 963 | { |
138 | 963 | if (!ovs_scan(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(*mac))) { |
139 | 10 | return xasprintf("invalid mac address %s", str); |
140 | 10 | } |
141 | 953 | return NULL; |
142 | 963 | } |
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 | 1.73k | { |
151 | 1.73k | struct in_addr in_addr; |
152 | | |
153 | 1.73k | if (lookup_ip(str, &in_addr)) { |
154 | 8 | return xasprintf("%s: could not convert to IP address", str); |
155 | 8 | } |
156 | 1.73k | *ip = in_addr.s_addr; |
157 | 1.73k | return NULL; |
158 | 1.73k | } |
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 | 203 | { |
167 | 203 | if (!strcmp(str, "ftp")) { |
168 | 80 | *alg = IPPORT_FTP; |
169 | 80 | return NULL; |
170 | 80 | } |
171 | 123 | if (!strcmp(str, "tftp")) { |
172 | 78 | *alg = IPPORT_TFTP; |
173 | 78 | return NULL; |
174 | 78 | } |
175 | 45 | return xasprintf("invalid conntrack helper \"%s\"", str); |
176 | 123 | } |
177 | | |
178 | | bool |
179 | | ofp_parse_protocol(const char *name, const struct ofp_protocol **p_out) |
180 | 150k | { |
181 | 150k | static const struct ofp_protocol protocols[] = { |
182 | 150k | { "ip", ETH_TYPE_IP, 0 }, |
183 | 150k | { "ipv4", ETH_TYPE_IP, 0 }, |
184 | 150k | { "ip4", ETH_TYPE_IP, 0 }, |
185 | 150k | { "arp", ETH_TYPE_ARP, 0 }, |
186 | 150k | { "icmp", ETH_TYPE_IP, IPPROTO_ICMP }, |
187 | 150k | { "tcp", ETH_TYPE_IP, IPPROTO_TCP }, |
188 | 150k | { "udp", ETH_TYPE_IP, IPPROTO_UDP }, |
189 | 150k | { "sctp", ETH_TYPE_IP, IPPROTO_SCTP }, |
190 | 150k | { "ipv6", ETH_TYPE_IPV6, 0 }, |
191 | 150k | { "ip6", ETH_TYPE_IPV6, 0 }, |
192 | 150k | { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 }, |
193 | 150k | { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP }, |
194 | 150k | { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP }, |
195 | 150k | { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP }, |
196 | 150k | { "rarp", ETH_TYPE_RARP, 0}, |
197 | 150k | { "mpls", ETH_TYPE_MPLS, 0 }, |
198 | 150k | { "mplsm", ETH_TYPE_MPLS_MCAST, 0 }, |
199 | 150k | }; |
200 | 150k | const struct ofp_protocol *p; |
201 | | |
202 | 2.36M | for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) { |
203 | 2.23M | if (!strcmp(p->name, name)) { |
204 | 26.8k | *p_out = p; |
205 | 26.8k | return true; |
206 | 26.8k | } |
207 | 2.23M | } |
208 | 123k | *p_out = NULL; |
209 | 123k | return false; |
210 | 150k | } |
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 | 62.9k | { |
223 | 62.9k | union mf_value value, mask; |
224 | 62.9k | char *error; |
225 | | |
226 | 62.9k | 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 | 27.4k | s = "0/0"; |
230 | 27.4k | } |
231 | | |
232 | 62.9k | error = mf_parse(mf, s, port_map, &value, &mask); |
233 | 62.9k | if (!error) { |
234 | 62.2k | *usable_protocols &= mf_set(mf, &value, &mask, match, &error); |
235 | 62.2k | match_add_ethernet_prereq(match, mf); |
236 | 62.2k | } |
237 | 62.9k | return error; |
238 | 62.9k | } |
239 | | |
240 | | char * |
241 | | ofp_extract_actions(char *s) |
242 | 16.6k | { |
243 | 16.6k | s = strstr(s, "action"); |
244 | 16.6k | if (s) { |
245 | 16.4k | *s = '\0'; |
246 | 16.4k | s = strchr(s + 1, '='); |
247 | 16.4k | return s ? s + 1 : NULL; |
248 | 16.4k | } else { |
249 | 212 | return NULL; |
250 | 212 | } |
251 | 16.6k | } |
252 | | |
253 | | static size_t |
254 | | parse_value(const char *s, const char *delimiters) |
255 | 489k | { |
256 | 489k | 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 | 16.8M | while (!strchr(delimiters, s[n])) { |
263 | 16.3M | if (s[n] == '(') { |
264 | 17.5k | int level = 0; |
265 | 79.5M | do { |
266 | 79.5M | switch (s[n]) { |
267 | 4.55k | case '\0': |
268 | 4.55k | return n; |
269 | 311k | case '(': |
270 | 311k | level++; |
271 | 311k | break; |
272 | 66.3k | case ')': |
273 | 66.3k | level--; |
274 | 66.3k | break; |
275 | 79.5M | } |
276 | 79.5M | n++; |
277 | 79.5M | } while (level > 0); |
278 | 16.3M | } else { |
279 | 16.3M | n++; |
280 | 16.3M | } |
281 | 16.3M | } |
282 | 485k | return n; |
283 | 489k | } |
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 | 739k | { |
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 | 739k | *stringp += strspn(*stringp, ", \t\r\n"); |
301 | 739k | if (**stringp == '\0') { |
302 | 120k | *keyp = *valuep = NULL; |
303 | 120k | return false; |
304 | 120k | } |
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 | 618k | char *key = *stringp; |
309 | 618k | size_t key_len = strcspn(key, ":=(, \t\r\n"); |
310 | 618k | char key_delim = key[key_len]; |
311 | 618k | key[key_len] = '\0'; |
312 | 618k | *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 | 618k | const char *value_delims; |
323 | 618k | if (key_delim == ':' || key_delim == '=') { |
324 | 428k | value_delims = ", \t\r\n"; |
325 | 428k | } else if (key_delim == '(') { |
326 | 60.1k | value_delims = ")"; |
327 | 129k | } else { |
328 | 129k | *keyp = key; |
329 | 129k | *valuep = key + key_len; /* Empty string. */ |
330 | 129k | return true; |
331 | 129k | } |
332 | | |
333 | | /* Extract the value. Advance the input position past the value and |
334 | | * delimiter. */ |
335 | 488k | char *value = *stringp; |
336 | 488k | size_t value_len = parse_value(value, value_delims); |
337 | 488k | 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 | 488k | if (key_delim == '(' && value[value_len] == ')' && |
343 | 488k | value[value_len + 1] == '-' && value[value_len + 2] == '>') { |
344 | 1.05k | value_delims = ", \t\r\n"; |
345 | 1.05k | value_len += parse_value(&value[value_len], value_delims); |
346 | 1.05k | value_delim = value[value_len]; |
347 | 1.05k | } |
348 | 488k | value[value_len] = '\0'; |
349 | 488k | *stringp += value_len + (value_delim != '\0'); |
350 | | |
351 | 488k | *keyp = key; |
352 | 488k | *valuep = value; |
353 | 488k | return true; |
354 | 618k | } |