Coverage Report

Created: 2026-02-26 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/ofp-parse.c
Line
Count
Source
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
10.3k
{
40
10.3k
    int value;
41
42
10.3k
    if (!str_to_int(str, 0, &value) || value < 0 || value > 255) {
43
69
        return xasprintf("invalid %s \"%s\"", name, str);
44
69
    }
45
10.2k
    *valuep = value;
46
10.2k
    return NULL;
47
10.3k
}
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
17.1k
{
58
17.1k
    int value;
59
60
17.1k
    if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
61
252
        return xasprintf("invalid %s \"%s\"", name, str);
62
252
    }
63
16.8k
    *valuep = value;
64
16.8k
    return NULL;
65
17.1k
}
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
9.23k
{
74
9.23k
    unsigned long long value;
75
76
9.23k
    if (!str[0]) {
77
49
        return xstrdup("missing required numeric argument");
78
49
    }
79
80
9.18k
    if (!str_to_ullong(str, 0, &value) || value > UINT32_MAX) {
81
639
        return xasprintf("invalid numeric format %s", str);
82
639
    }
83
8.55k
    *valuep = value;
84
8.55k
    return NULL;
85
9.18k
}
86
87
/* Parses 'str' as an 64-bit unsigned integer into '*valuep'.
88
 *
89
 * Returns NULL if successful, otherwise a malloc()'d string describing the
90
 * error.  The caller is responsible for freeing the returned string. */
91
char * OVS_WARN_UNUSED_RESULT
92
str_to_u64(const char *str, uint64_t *valuep)
93
4.98k
{
94
4.98k
    char *tail;
95
4.98k
    uint64_t value;
96
97
4.98k
    if (!str[0]) {
98
29
        return xstrdup("missing required numeric argument");
99
29
    }
100
101
4.98k
    errno = 0;
102
4.95k
    value = strtoull(str, &tail, 0);
103
4.95k
    if (errno == EINVAL || errno == ERANGE || *tail) {
104
19
        return xasprintf("invalid numeric format %s", str);
105
19
    }
106
4.93k
    *valuep = value;
107
4.93k
    return NULL;
108
4.95k
}
109
110
/* Parses 'str' as an 64-bit unsigned integer in network byte order into
111
 * '*valuep'.
112
 *
113
 * Returns NULL if successful, otherwise a malloc()'d string describing the
114
 * error.  The caller is responsible for freeing the returned string. */
115
char * OVS_WARN_UNUSED_RESULT
116
str_to_be64(const char *str, ovs_be64 *valuep)
117
3.69k
{
118
3.69k
    uint64_t value = 0;
119
3.69k
    char *error;
120
121
3.69k
    error = str_to_u64(str, &value);
122
3.69k
    if (!error) {
123
3.65k
        *valuep = htonll(value);
124
3.65k
    }
125
3.69k
    return error;
126
3.69k
}
127
128
/* Parses 'str' as an Ethernet address into 'mac'.
129
 *
130
 * Returns NULL if successful, otherwise a malloc()'d string describing the
131
 * error.  The caller is responsible for freeing the returned string. */
132
char * OVS_WARN_UNUSED_RESULT
133
str_to_mac(const char *str, struct eth_addr *mac)
134
1.09k
{
135
1.09k
    if (!ovs_scan(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(*mac))) {
136
9
        return xasprintf("invalid mac address %s", str);
137
9
    }
138
1.08k
    return NULL;
139
1.09k
}
140
141
/* Parses 'str' as an IP address into '*ip'.
142
 *
143
 * Returns NULL if successful, otherwise a malloc()'d string describing the
144
 * error.  The caller is responsible for freeing the returned string. */
145
char * OVS_WARN_UNUSED_RESULT
146
str_to_ip(const char *str, ovs_be32 *ip)
147
1.79k
{
148
1.79k
    struct in_addr in_addr;
149
150
1.79k
    if (lookup_ip(str, &in_addr)) {
151
8
        return xasprintf("%s: could not convert to IP address", str);
152
8
    }
153
1.78k
    *ip = in_addr.s_addr;
154
1.78k
    return NULL;
155
1.79k
}
156
157
/* Parses 'str' as a conntrack helper into 'alg'.
158
 *
159
 * Returns NULL if successful, otherwise a malloc()'d string describing the
160
 * error.  The caller is responsible for freeing the returned string. */
161
char * OVS_WARN_UNUSED_RESULT
162
str_to_connhelper(const char *str, uint16_t *alg)
163
272
{
164
272
    if (!strcmp(str, "ftp")) {
165
130
        *alg = IPPORT_FTP;
166
130
        return NULL;
167
130
    }
168
142
    if (!strcmp(str, "tftp")) {
169
84
        *alg = IPPORT_TFTP;
170
84
        return NULL;
171
84
    }
172
58
    return xasprintf("invalid conntrack helper \"%s\"", str);
173
142
}
174
175
bool
176
ofp_parse_protocol(const char *name, const struct ofp_protocol **p_out)
177
174k
{
178
174k
    static const struct ofp_protocol protocols[] = {
179
174k
        { "ip", ETH_TYPE_IP, 0 },
180
174k
        { "ipv4", ETH_TYPE_IP, 0 },
181
174k
        { "ip4", ETH_TYPE_IP, 0 },
182
174k
        { "arp", ETH_TYPE_ARP, 0 },
183
174k
        { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
184
174k
        { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
185
174k
        { "udp", ETH_TYPE_IP, IPPROTO_UDP },
186
174k
        { "sctp", ETH_TYPE_IP, IPPROTO_SCTP },
187
174k
        { "ipv6", ETH_TYPE_IPV6, 0 },
188
174k
        { "ip6", ETH_TYPE_IPV6, 0 },
189
174k
        { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
190
174k
        { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
191
174k
        { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
192
174k
        { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP },
193
174k
        { "rarp", ETH_TYPE_RARP, 0},
194
174k
        { "mpls", ETH_TYPE_MPLS, 0 },
195
174k
        { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
196
174k
    };
197
174k
    const struct ofp_protocol *p;
198
199
2.92M
    for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
200
2.76M
        if (!strcmp(p->name, name)) {
201
17.7k
            *p_out = p;
202
17.7k
            return true;
203
17.7k
        }
204
2.76M
    }
205
157k
    *p_out = NULL;
206
157k
    return false;
207
174k
}
208
209
/* Parses 's' as the (possibly masked) value of field 'mf', and updates
210
 * 'match' appropriately.  Restricts the set of usable protocols to ones
211
 * supporting the parsed field.
212
 *
213
 * Returns NULL if successful, otherwise a malloc()'d string describing the
214
 * error.  The caller is responsible for freeing the returned string. */
215
char * OVS_WARN_UNUSED_RESULT
216
ofp_parse_field(const struct mf_field *mf, const char *s,
217
                const struct ofputil_port_map *port_map, struct match *match,
218
                enum ofputil_protocol *usable_protocols)
219
80.6k
{
220
80.6k
    union mf_value value, mask;
221
80.6k
    char *error;
222
223
80.6k
    if (!*s) {
224
        /* If there's no string, we're just trying to match on the
225
         * existence of the field, so use a no-op value. */
226
39.3k
        s = "0/0";
227
39.3k
    }
228
229
80.6k
    error = mf_parse(mf, s, port_map, &value, &mask);
230
80.6k
    if (!error) {
231
79.9k
        *usable_protocols &= mf_set(mf, &value, &mask, match, &error);
232
79.9k
        match_add_ethernet_prereq(match, mf);
233
79.9k
    }
234
80.6k
    return error;
235
80.6k
}
236
237
char *
238
ofp_extract_actions(char *s)
239
15.8k
{
240
15.8k
    s = strstr(s, "action");
241
15.8k
    if (s) {
242
15.6k
        *s = '\0';
243
15.6k
        s = strchr(s + 1, '=');
244
15.6k
        return s ? s + 1 : NULL;
245
15.6k
    } else {
246
217
        return NULL;
247
217
    }
248
15.8k
}
249

250
static size_t
251
parse_value(const char *s, const char *delimiters)
252
666k
{
253
666k
    size_t n = 0;
254
255
    /* Iterate until we reach a delimiter.
256
     *
257
     * strchr(s, '\0') returns s+strlen(s), so this test handles the null
258
     * terminator at the end of 's'.  */
259
19.8M
    while (!strchr(delimiters, s[n])) {
260
19.1M
        if (s[n] == '(') {
261
20.6k
            int level = 0;
262
59.9M
            do {
263
59.9M
                switch (s[n]) {
264
3.92k
                case '\0':
265
3.92k
                    return n;
266
172k
                case '(':
267
172k
                    level++;
268
172k
                    break;
269
66.9k
                case ')':
270
66.9k
                    level--;
271
66.9k
                    break;
272
59.9M
                }
273
59.9M
                n++;
274
59.9M
            } while (level > 0);
275
19.1M
        } else {
276
19.1M
            n++;
277
19.1M
        }
278
19.1M
    }
279
662k
    return n;
280
666k
}
281
282
/* Parses a key or a key-value pair from '*stringp'.
283
 *
284
 * On success: Stores the key into '*keyp'.  Stores the value, if present, into
285
 * '*valuep', otherwise an empty string.  Advances '*stringp' past the end of
286
 * the key-value pair, preparing it for another call.  '*keyp' and '*valuep'
287
 * are substrings of '*stringp' created by replacing some of its bytes by null
288
 * terminators.  Returns true.
289
 *
290
 * If '*stringp' is just white space or commas, sets '*keyp' and '*valuep' to
291
 * NULL and returns false. */
292
bool
293
ofputil_parse_key_value(char **stringp, char **keyp, char **valuep)
294
1.03M
{
295
    /* Skip white space and delimiters.  If that brings us to the end of the
296
     * input string, we are done and there are no more key-value pairs. */
297
1.03M
    *stringp += strspn(*stringp, ", \t\r\n");
298
1.03M
    if (**stringp == '\0') {
299
215k
        *keyp = *valuep = NULL;
300
215k
        return false;
301
215k
    }
302
303
    /* Extract the key and the delimiter that ends the key-value pair or begins
304
     * the value.  Advance the input position past the key and delimiter. */
305
817k
    char *key = *stringp;
306
817k
    size_t key_len = strcspn(key, ":=(, \t\r\n");
307
817k
    char key_delim = key[key_len];
308
817k
    key[key_len] = '\0';
309
817k
    *stringp += key_len + (key_delim != '\0');
310
311
    /* Figure out what delimiter ends the value:
312
     *
313
     *     - If key_delim is ":" or "=", the value extends until white space
314
     *       or a comma.
315
     *
316
     *     - If key_delim is "(", the value extends until ")".
317
     *
318
     * If there is no value, we are done. */
319
817k
    const char *value_delims;
320
817k
    if (key_delim == ':' || key_delim == '=') {
321
604k
        value_delims = ", \t\r\n";
322
604k
    } else if (key_delim == '(') {
323
61.5k
        value_delims = ")";
324
151k
    } else {
325
151k
        *keyp = key;
326
151k
        *valuep = key + key_len; /* Empty string. */
327
151k
        return true;
328
151k
    }
329
330
    /* Extract the value.  Advance the input position past the value and
331
     * delimiter. */
332
666k
    char *value = *stringp;
333
666k
    size_t value_len = parse_value(value, value_delims);
334
666k
    char value_delim = value[value_len];
335
336
    /* Handle the special case if the value is of the form "(x)->y".
337
     * After parsing, 'valuep' will be pointing to - "x)->y".
338
     * */
339
666k
    if (key_delim == '(' && value[value_len] == ')' &&
340
53.0k
        value[value_len + 1] == '-' && value[value_len + 2] == '>') {
341
733
        value_delims = ", \t\r\n";
342
733
        value_len += parse_value(&value[value_len], value_delims);
343
733
        value_delim = value[value_len];
344
733
    }
345
666k
    value[value_len] = '\0';
346
666k
    *stringp += value_len + (value_delim != '\0');
347
348
666k
    *keyp = key;
349
666k
    *valuep = value;
350
    return true;
351
817k
}