Coverage Report

Created: 2025-07-01 06:50

/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
0
{
40
0
    int value;
41
42
0
    if (!str_to_int(str, 0, &value) || value < 0 || value > 255) {
43
0
        return xasprintf("invalid %s \"%s\"", name, str);
44
0
    }
45
0
    *valuep = value;
46
0
    return NULL;
47
0
}
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
0
{
58
0
    int value;
59
60
0
    if (!str_to_int(str, 0, &value) || value < 0 || value > 65535) {
61
0
        return xasprintf("invalid %s \"%s\"", name, str);
62
0
    }
63
0
    *valuep = value;
64
0
    return NULL;
65
0
}
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
0
{
74
0
    unsigned long long value;
75
76
0
    if (!str[0]) {
77
0
        return xstrdup("missing required numeric argument");
78
0
    }
79
80
0
    if (!str_to_ullong(str, 0, &value) || value > UINT32_MAX) {
81
0
        return xasprintf("invalid numeric format %s", str);
82
0
    }
83
0
    *valuep = value;
84
0
    return NULL;
85
0
}
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
0
{
94
0
    char *tail;
95
0
    uint64_t value;
96
97
0
    if (!str[0]) {
98
0
        return xstrdup("missing required numeric argument");
99
0
    }
100
101
0
    errno = 0;
102
0
    value = strtoull(str, &tail, 0);
103
0
    if (errno == EINVAL || errno == ERANGE || *tail) {
104
0
        return xasprintf("invalid numeric format %s", str);
105
0
    }
106
0
    *valuep = value;
107
0
    return NULL;
108
0
}
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
0
{
118
0
    uint64_t value = 0;
119
0
    char *error;
120
121
0
    error = str_to_u64(str, &value);
122
0
    if (!error) {
123
0
        *valuep = htonll(value);
124
0
    }
125
0
    return error;
126
0
}
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
0
{
135
0
    if (!ovs_scan(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(*mac))) {
136
0
        return xasprintf("invalid mac address %s", str);
137
0
    }
138
0
    return NULL;
139
0
}
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
0
{
148
0
    struct in_addr in_addr;
149
150
0
    if (lookup_ip(str, &in_addr)) {
151
0
        return xasprintf("%s: could not convert to IP address", str);
152
0
    }
153
0
    *ip = in_addr.s_addr;
154
0
    return NULL;
155
0
}
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
0
{
164
0
    if (!strcmp(str, "ftp")) {
165
0
        *alg = IPPORT_FTP;
166
0
        return NULL;
167
0
    }
168
0
    if (!strcmp(str, "tftp")) {
169
0
        *alg = IPPORT_TFTP;
170
0
        return NULL;
171
0
    }
172
0
    return xasprintf("invalid conntrack helper \"%s\"", str);
173
0
}
174
175
bool
176
ofp_parse_protocol(const char *name, const struct ofp_protocol **p_out)
177
0
{
178
0
    static const struct ofp_protocol protocols[] = {
179
0
        { "ip", ETH_TYPE_IP, 0 },
180
0
        { "ipv4", ETH_TYPE_IP, 0 },
181
0
        { "ip4", ETH_TYPE_IP, 0 },
182
0
        { "arp", ETH_TYPE_ARP, 0 },
183
0
        { "icmp", ETH_TYPE_IP, IPPROTO_ICMP },
184
0
        { "tcp", ETH_TYPE_IP, IPPROTO_TCP },
185
0
        { "udp", ETH_TYPE_IP, IPPROTO_UDP },
186
0
        { "sctp", ETH_TYPE_IP, IPPROTO_SCTP },
187
0
        { "ipv6", ETH_TYPE_IPV6, 0 },
188
0
        { "ip6", ETH_TYPE_IPV6, 0 },
189
0
        { "icmp6", ETH_TYPE_IPV6, IPPROTO_ICMPV6 },
190
0
        { "tcp6", ETH_TYPE_IPV6, IPPROTO_TCP },
191
0
        { "udp6", ETH_TYPE_IPV6, IPPROTO_UDP },
192
0
        { "sctp6", ETH_TYPE_IPV6, IPPROTO_SCTP },
193
0
        { "rarp", ETH_TYPE_RARP, 0},
194
0
        { "mpls", ETH_TYPE_MPLS, 0 },
195
0
        { "mplsm", ETH_TYPE_MPLS_MCAST, 0 },
196
0
    };
197
0
    const struct ofp_protocol *p;
198
199
0
    for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
200
0
        if (!strcmp(p->name, name)) {
201
0
            *p_out = p;
202
0
            return true;
203
0
        }
204
0
    }
205
0
    *p_out = NULL;
206
0
    return false;
207
0
}
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
0
{
220
0
    union mf_value value, mask;
221
0
    char *error;
222
223
0
    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
0
        s = "0/0";
227
0
    }
228
229
0
    error = mf_parse(mf, s, port_map, &value, &mask);
230
0
    if (!error) {
231
0
        *usable_protocols &= mf_set(mf, &value, &mask, match, &error);
232
0
        match_add_ethernet_prereq(match, mf);
233
0
    }
234
0
    return error;
235
0
}
236
237
char *
238
ofp_extract_actions(char *s)
239
0
{
240
0
    s = strstr(s, "action");
241
0
    if (s) {
242
0
        *s = '\0';
243
0
        s = strchr(s + 1, '=');
244
0
        return s ? s + 1 : NULL;
245
0
    } else {
246
0
        return NULL;
247
0
    }
248
0
}
249

250
static size_t
251
parse_value(const char *s, const char *delimiters)
252
0
{
253
0
    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
0
    while (!strchr(delimiters, s[n])) {
260
0
        if (s[n] == '(') {
261
0
            int level = 0;
262
0
            do {
263
0
                switch (s[n]) {
264
0
                case '\0':
265
0
                    return n;
266
0
                case '(':
267
0
                    level++;
268
0
                    break;
269
0
                case ')':
270
0
                    level--;
271
0
                    break;
272
0
                }
273
0
                n++;
274
0
            } while (level > 0);
275
0
        } else {
276
0
            n++;
277
0
        }
278
0
    }
279
0
    return n;
280
0
}
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
0
{
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
0
    *stringp += strspn(*stringp, ", \t\r\n");
298
0
    if (**stringp == '\0') {
299
0
        *keyp = *valuep = NULL;
300
0
        return false;
301
0
    }
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
0
    char *key = *stringp;
306
0
    size_t key_len = strcspn(key, ":=(, \t\r\n");
307
0
    char key_delim = key[key_len];
308
0
    key[key_len] = '\0';
309
0
    *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
0
    const char *value_delims;
320
0
    if (key_delim == ':' || key_delim == '=') {
321
0
        value_delims = ", \t\r\n";
322
0
    } else if (key_delim == '(') {
323
0
        value_delims = ")";
324
0
    } else {
325
0
        *keyp = key;
326
0
        *valuep = key + key_len; /* Empty string. */
327
0
        return true;
328
0
    }
329
330
    /* Extract the value.  Advance the input position past the value and
331
     * delimiter. */
332
0
    char *value = *stringp;
333
0
    size_t value_len = parse_value(value, value_delims);
334
0
    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
0
    if (key_delim == '(' && value[value_len] == ')' &&
340
0
        value[value_len + 1] == '-' && value[value_len + 2] == '>') {
341
0
        value_delims = ", \t\r\n";
342
0
        value_len += parse_value(&value[value_len], value_delims);
343
0
        value_delim = value[value_len];
344
0
    }
345
0
    value[value_len] = '\0';
346
0
    *stringp += value_len + (value_delim != '\0');
347
348
0
    *keyp = key;
349
0
    *valuep = value;
350
0
    return true;
351
0
}