Coverage Report

Created: 2025-07-01 06:50

/src/openvswitch/lib/ofp-prop.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2014, 2015, 2016 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
19
#include "byte-order.h"
20
#include "openvswitch/ofpbuf.h"
21
#include "openvswitch/ofp-errors.h"
22
#include "openvswitch/ofp-prop.h"
23
#include "openvswitch/vlog.h"
24
#include "unaligned.h"
25
#include "util.h"
26
#include "uuid.h"
27
28
struct ofp_prop_be16 {
29
    ovs_be16 type;
30
    ovs_be16 len;
31
    ovs_be16 value;
32
    uint8_t pad[2];
33
};
34
BUILD_ASSERT_DECL(sizeof(struct ofp_prop_be16) == 8);
35
36
struct ofp_prop_be32 {
37
    ovs_be16 type;
38
    ovs_be16 len;
39
    ovs_be32 value;
40
};
41
BUILD_ASSERT_DECL(sizeof(struct ofp_prop_be32) == 8);
42
43
static uint32_t
44
ofpprop_type_to_exp_id(uint64_t type)
45
0
{
46
0
    return type >> 32;
47
0
}
48
49
static uint32_t
50
ofpprop_type_to_exp_type(uint64_t type)
51
0
{
52
0
    return type & UINT32_MAX;
53
0
}
54
55
/* Pulls a property, beginning with struct ofp_prop_header, from the beginning
56
 * of 'msg'.  Stores the type of the property in '*typep' and, if 'property' is
57
 * nonnull, the entire property, including the header, in '*property'.  Points
58
 * 'property->header' to the property header (which could be ofp_prop_header or
59
 * ofp_prop_experimenter) and 'property->msg' to just past it.  Returns 0 if
60
 * successful, otherwise an OpenFlow error code.
61
 *
62
 * This function treats property types 'min_exp' and larger as introducing
63
 * experimenter properties.  For most kinds of properties, 0xffff is the
64
 * appropriate value for 'min_exp', because 0xffff is the only property type
65
 * used for experimenters, but async config properties also use 0xfffe.  Use
66
 * 0x10000 (or higher) if experimenter properties are not supported.
67
 *
68
 * This function pulls the property's stated size padded out to a multiple of
69
 * 'alignment' bytes.  The common case in OpenFlow is an 'alignment' of 8, so
70
 * you can use ofpprop_pull() for that case. */
71
enum ofperr
72
ofpprop_pull__(struct ofpbuf *msg, struct ofpbuf *property,
73
               unsigned int alignment, unsigned int min_exp,
74
               uint64_t *typep)
75
0
{
76
0
    struct ofp_prop_header *oph;
77
0
    unsigned int padded_len;
78
0
    unsigned int len;
79
80
0
    if (msg->size < sizeof *oph) {
81
0
        return OFPERR_OFPBPC_BAD_LEN;
82
0
    }
83
84
0
    oph = msg->data;
85
0
    len = ntohs(oph->len);
86
0
    padded_len = ROUND_UP(len, alignment);
87
0
    if (len < sizeof *oph || padded_len > msg->size) {
88
0
        return OFPERR_OFPBPC_BAD_LEN;
89
0
    }
90
91
0
    uint16_t type = ntohs(oph->type);
92
0
    if (type < min_exp) {
93
0
        *typep = type;
94
0
    } else {
95
0
        struct ofp_prop_experimenter *ope = msg->data;
96
0
        if (len < sizeof *ope) {
97
0
            return OFPERR_OFPBPC_BAD_LEN;
98
0
        }
99
100
0
        if (!ope->experimenter) {
101
            /* Reject experimenter 0 because it yields ambiguity with standard
102
             * property types. */
103
0
            return OFPERR_OFPBPC_BAD_EXPERIMENTER;
104
0
        }
105
106
0
        *typep = OFPPROP_EXP(ntohl(ope->experimenter), ntohl(ope->exp_type));
107
0
    }
108
109
0
    if (property) {
110
0
        ofpbuf_use_const(property, msg->data, len);
111
0
        property->header = property->data;
112
0
        property->msg = ((uint8_t *) property->data
113
0
                         + (type < min_exp
114
0
                            ? sizeof(struct ofp_prop_header)
115
0
                            : sizeof(struct ofp_prop_experimenter)));
116
0
    }
117
0
    ofpbuf_pull(msg, padded_len);
118
0
    return 0;
119
0
}
120
121
/* Pulls a property, beginning with struct ofp_prop_header, from the beginning
122
 * of 'msg'.  Stores the type of the property in '*typep' and, if 'property' is
123
 * nonnull, the entire property, including the header, in '*property'.  Points
124
 * 'property->header' to the property header (which could be ofp_prop_header or
125
 * ofp_prop_experimenter) and 'property->msg' to just past it.  Returns 0 if
126
 * successful, otherwise an error code.
127
 *
128
 * This function treats property type 0xffff as introducing an experimenter
129
 * property.  Use ofpprop_pull__() instead if some other behavior is needed.
130
 *
131
 * This function pulls the property's stated size padded out to a multiple of 8
132
 * bytes, which is the common case for OpenFlow properties.  Use
133
 * ofpprop_pull__() instead if some other behavior is needed.*/
134
enum ofperr
135
ofpprop_pull(struct ofpbuf *msg, struct ofpbuf *property, uint64_t *typep)
136
0
{
137
0
    return ofpprop_pull__(msg, property, 8, 0xffff, typep);
138
0
}
139
140
/* Attempts to parse 'property' as a property containing a 16-bit value.  If
141
 * successful, stores the value into '*value' and returns 0; otherwise returns
142
 * an OpenFlow error. */
143
enum ofperr
144
ofpprop_parse_be16(const struct ofpbuf *property, ovs_be16 *value)
145
0
{
146
    /* OpenFlow uses 8-byte properties for 16-bit values, which doesn't really
147
     * make sense.  Be forgiving by allowing any size payload as long as it's
148
     * at least big enough.  */
149
0
    ovs_be16 *p = property->msg;
150
0
    if (ofpbuf_msgsize(property) < sizeof *p) {
151
0
        return OFPERR_OFPBPC_BAD_LEN;
152
0
    }
153
0
    *value = *p;
154
0
    return 0;
155
0
}
156
157
/* Attempts to parse 'property' as a property containing a 32-bit value.  If
158
 * successful, stores the value into '*value' and returns 0; otherwise returns
159
 * an OpenFlow error. */
160
enum ofperr
161
ofpprop_parse_be32(const struct ofpbuf *property, ovs_be32 *value)
162
0
{
163
0
    ovs_be32 *p = property->msg;
164
0
    if (ofpbuf_msgsize(property) != sizeof *p) {
165
0
        return OFPERR_OFPBPC_BAD_LEN;
166
0
    }
167
0
    *value = *p;
168
0
    return 0;
169
0
}
170
171
/* Attempts to parse 'property' as a property containing a 64-bit value.  If
172
 * successful, stores the value into '*value' and returns 0; otherwise returns
173
 * an OpenFlow error. */
174
enum ofperr
175
ofpprop_parse_be64(const struct ofpbuf *property, ovs_be64 *value)
176
0
{
177
0
    ovs_be64 *p;
178
0
    size_t be64_offset = ROUND_UP(ofpbuf_headersize(property), 8);
179
0
    if (property->size != be64_offset + sizeof *p) {
180
0
        return OFPERR_OFPBPC_BAD_LEN;
181
0
    }
182
183
0
    p = ALIGNED_CAST(ovs_be64 *, (char *) property->data + be64_offset);
184
0
    *value = *p;
185
0
    return 0;
186
0
}
187
188
/* Attempts to parse 'property' as a property containing a 128-bit value.  If
189
 * successful, stores the value into '*value' and returns 0; otherwise returns
190
 * an OpenFlow error. */
191
enum ofperr
192
ofpprop_parse_be128(const struct ofpbuf *property, ovs_be128 *value)
193
0
{
194
0
    ovs_32aligned_be128 *p = property->msg;
195
196
0
    if (ofpbuf_msgsize(property) != sizeof *p) {
197
0
        return OFPERR_OFPBPC_BAD_LEN;
198
0
    }
199
0
    *value = get_32aligned_be128(p);
200
0
    return 0;
201
0
}
202
203
/* Attempts to parse 'property' as a property containing a 8-bit value.  If
204
 * successful, stores the value into '*value' and returns 0; otherwise returns
205
 * an OpenFlow error. */
206
enum ofperr
207
ofpprop_parse_u8(const struct ofpbuf *property, uint8_t *value)
208
0
{
209
    /* OpenFlow 1.5 and earlier don't have any 8-bit properties, but it uses
210
     * 8-byte properties for 16-bit values, which doesn't really make sense.
211
     * Be forgiving by allowing any size payload as long as it's at least big
212
     * enough. */
213
0
    uint8_t *p = property->msg;
214
0
    if (ofpbuf_msgsize(property) < sizeof *p) {
215
0
        return OFPERR_OFPBPC_BAD_LEN;
216
0
    }
217
0
    *value = *p;
218
0
    return 0;
219
0
}
220
221
/* Attempts to parse 'property' as a property containing a 16-bit value.  If
222
 * successful, stores the value into '*value' and returns 0; otherwise returns
223
 * an OpenFlow error. */
224
enum ofperr
225
ofpprop_parse_u16(const struct ofpbuf *property, uint16_t *value)
226
0
{
227
    /* OpenFlow uses 8-byte properties for 16-bit values, which doesn't really
228
     * make sense.  Be forgiving by allowing any size payload as long as it's
229
     * at least big enough.  */
230
0
    ovs_be16 *p = property->msg;
231
0
    if (ofpbuf_msgsize(property) < sizeof *p) {
232
0
        return OFPERR_OFPBPC_BAD_LEN;
233
0
    }
234
0
    *value = ntohs(*p);
235
0
    return 0;
236
0
}
237
238
/* Attempts to parse 'property' as a property containing a 32-bit value.  If
239
 * successful, stores the value into '*value' and returns 0; otherwise returns
240
 * an OpenFlow error. */
241
enum ofperr
242
ofpprop_parse_u32(const struct ofpbuf *property, uint32_t *value)
243
0
{
244
0
    ovs_be32 *p = property->msg;
245
0
    if (ofpbuf_msgsize(property) != sizeof *p) {
246
0
        return OFPERR_OFPBPC_BAD_LEN;
247
0
    }
248
0
    *value = ntohl(*p);
249
0
    return 0;
250
0
}
251
252
/* Attempts to parse 'property' as a property containing a 64-bit value.  If
253
 * successful, stores the value into '*value' and returns 0; otherwise returns
254
 * an OpenFlow error. */
255
enum ofperr
256
ofpprop_parse_u64(const struct ofpbuf *property, uint64_t *value)
257
0
{
258
0
    ovs_be64 *p;
259
0
    size_t be64_offset = ROUND_UP(ofpbuf_headersize(property), 8);
260
0
    if (property->size != be64_offset + sizeof *p) {
261
0
        return OFPERR_OFPBPC_BAD_LEN;
262
0
    }
263
264
0
    p = ALIGNED_CAST(ovs_be64 *, (char *) property->data + be64_offset);
265
0
    *value = ntohll(*p);
266
0
    return 0;
267
0
}
268
269
/* Attempts to parse 'property' as a property containing a 128-bit value.  If
270
 * successful, stores the value into '*value' and returns 0; otherwise returns
271
 * an OpenFlow error. */
272
enum ofperr
273
ofpprop_parse_u128(const struct ofpbuf *property, ovs_u128 *value)
274
0
{
275
0
    enum ofperr error = ofpprop_parse_be128(property, (ovs_be128 *) value);
276
277
0
    if (!error) {
278
0
        *value = ntoh128(*(ovs_be128 *) value);
279
0
    }
280
281
0
    return error;
282
0
}
283
284
/* Attempts to parse 'property' as a property containing a UUID.  If
285
 * successful, stores the value into '*uuid' and returns 0; otherwise returns
286
 * an OpenFlow error. */
287
enum ofperr
288
ofpprop_parse_uuid(const struct ofpbuf *property, struct uuid *uuid)
289
0
{
290
0
    struct uuid *p = property->msg;
291
0
    if (ofpbuf_msgsize(property) != sizeof *p) {
292
0
        return OFPERR_OFPBPC_BAD_LEN;
293
0
    }
294
0
    *uuid = *p;
295
0
    return 0;
296
0
}
297
298
/* Attempts to parse 'property' as a property that contains nested properties.
299
 * If successful, stores the nested data into '*nested' and returns 0;
300
 * otherwise returns an OpenFlow error.
301
 *
302
 * The only thing special about nested properties is that the property header
303
 * is followed by 4 bytes of padding, so that the nested properties begin at an
304
 * 8-byte aligned offset.  This function can be used in other situations where
305
 * this is the case. */
306
enum ofperr
307
ofpprop_parse_nested(const struct ofpbuf *property, struct ofpbuf *nested)
308
0
{
309
0
    size_t nested_offset = ROUND_UP(ofpbuf_headersize(property), 8);
310
0
    if (property->size < nested_offset) {
311
0
        return OFPERR_OFPBPC_BAD_LEN;
312
0
    }
313
314
0
    ofpbuf_use_const(nested, property->data, property->size);
315
0
    ofpbuf_pull(nested, nested_offset);
316
0
    return 0;
317
0
}
318
319
/* Adds a property with the given 'type' and 'len'-byte contents 'value' to
320
 * 'msg', padding the property out to a multiple of 8 bytes. */
321
void
322
ofpprop_put(struct ofpbuf *msg, uint64_t type, const void *value, size_t len)
323
0
{
324
0
    size_t start_ofs = ofpprop_start(msg, type);
325
0
    ofpbuf_put(msg, value, len);
326
0
    ofpprop_end(msg, start_ofs);
327
0
}
328
329
/* Adds a property with the given 'type' to 'msg', consisting of a struct
330
 * ofp_prop_header or ofp_prop_experimenter followed by enough zero bytes to
331
 * total 'len' bytes, followed by padding to bring the property up to a
332
 * multiple of 8 bytes.  Returns the property header. */
333
void *
334
ofpprop_put_zeros(struct ofpbuf *msg, uint64_t type, size_t len)
335
0
{
336
0
    void *header = ofpbuf_put_zeros(msg, ROUND_UP(len, 8));
337
0
    if (!ofpprop_is_experimenter(type)) {
338
0
        struct ofp_prop_header *oph = header;
339
0
        oph->type = htons(type);
340
0
        oph->len = htons(len);
341
0
    } else {
342
0
        struct ofp_prop_experimenter *ope = header;
343
0
        ope->type = htons(0xffff);
344
0
        ope->len = htons(len);
345
0
        ope->experimenter = htonl(ofpprop_type_to_exp_id(type));
346
0
        ope->exp_type = htonl(ofpprop_type_to_exp_type(type));
347
0
    }
348
0
    return header;
349
0
}
350
351
/* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
352
void
353
ofpprop_put_be16(struct ofpbuf *msg, uint64_t type, ovs_be16 value)
354
0
{
355
0
    if (!ofpprop_is_experimenter(type)) {
356
        /* The OpenFlow specs consistently (at least they're consistent!)  give
357
         * properties with a 16-bit integer value a length of 8, not 6, so add
358
         * two bytes of padding.  */
359
0
        ovs_be16 padded_value[2] = { value, 0 };
360
0
        ofpprop_put(msg, type, padded_value, sizeof padded_value);
361
0
    } else {
362
        /* There's no precedent but let's assume that this is generally done
363
         * sanely. */
364
0
        ofpprop_put(msg, type, &value, sizeof value);
365
0
    }
366
0
}
367
368
/* Adds a property with the given 'type' and 32-bit 'value' to 'msg'. */
369
void
370
ofpprop_put_be32(struct ofpbuf *msg, uint64_t type, ovs_be32 value)
371
0
{
372
0
    ofpprop_put(msg, type, &value, sizeof value);
373
0
}
374
375
/* Adds a property with the given 'type' and 64-bit 'value' to 'msg'. */
376
void
377
ofpprop_put_be64(struct ofpbuf *msg, uint64_t type, ovs_be64 value)
378
0
{
379
0
    size_t start = ofpprop_start(msg, type);
380
0
    ofpbuf_put_zeros(msg, 4);
381
0
    ofpbuf_put(msg, &value, sizeof value);
382
0
    ofpprop_end(msg, start);
383
0
}
384
385
/* Adds a property with the given 'type' and 128-bit 'value' to 'msg'. */
386
void
387
ofpprop_put_be128(struct ofpbuf *msg, uint64_t type, ovs_be128 value)
388
0
{
389
0
    ofpprop_put(msg, type, &value, sizeof value);
390
0
}
391
392
/* Adds a property with the given 'type' and 8-bit 'value' to 'msg'. */
393
void
394
ofpprop_put_u8(struct ofpbuf *msg, uint64_t type, uint8_t value)
395
0
{
396
    /* There's no precedent for 8-bit properties in OpenFlow 1.5 and earlier
397
     * but let's assume they're done sanely. */
398
0
    ofpprop_put(msg, type, &value, 1);
399
0
}
400
401
/* Adds a property with the given 'type' and 16-bit 'value' to 'msg'. */
402
void
403
ofpprop_put_u16(struct ofpbuf *msg, uint64_t type, uint16_t value)
404
0
{
405
0
    ofpprop_put_be16(msg, type, htons(value));
406
0
}
407
408
/* Adds a property with the given 'type' and 32-bit 'value' to 'msg'. */
409
void
410
ofpprop_put_u32(struct ofpbuf *msg, uint64_t type, uint32_t value)
411
0
{
412
0
    ofpprop_put_be32(msg, type, htonl(value));
413
0
}
414
415
/* Adds a property with the given 'type' and 64-bit 'value' to 'msg'. */
416
void
417
ofpprop_put_u64(struct ofpbuf *msg, uint64_t type, uint64_t value)
418
0
{
419
0
    ofpprop_put_be64(msg, type, htonll(value));
420
0
}
421
422
/* Adds a property with the given 'type' and 128-bit 'value' to 'msg'. */
423
void
424
ofpprop_put_u128(struct ofpbuf *msg, uint64_t type, ovs_u128 value)
425
0
{
426
0
    ofpprop_put_be128(msg, type, hton128(value));
427
0
}
428
429
/* Appends a property to 'msg' whose type is 'type' and whose contents is a
430
 * series of property headers, one for each 1-bit in 'bitmap'. */
431
void
432
ofpprop_put_bitmap(struct ofpbuf *msg, uint64_t type, uint64_t bitmap)
433
0
{
434
0
    size_t start_ofs = ofpprop_start(msg, type);
435
436
0
    for (; bitmap; bitmap = zero_rightmost_1bit(bitmap)) {
437
0
        ofpprop_start(msg, rightmost_1bit_idx(bitmap));
438
0
    }
439
0
    ofpprop_end(msg, start_ofs);
440
0
}
441
442
/* Appends a content-free property with the given 'type' to 'msg'.
443
 *
444
 * (The idea is that the presence of the property acts as a flag.) */
445
void
446
ofpprop_put_flag(struct ofpbuf *msg, uint64_t type)
447
0
{
448
0
    size_t start = ofpprop_start(msg, type);
449
0
    ofpprop_end(msg, start);
450
0
}
451
452
/* Appends a property to 'msg' with the given 'type' and 'uuid' as its
453
 * value. */
454
void
455
ofpprop_put_uuid(struct ofpbuf *msg, uint64_t type, const struct uuid *uuid)
456
0
{
457
0
    ofpprop_put(msg, type, uuid, sizeof *uuid);
458
0
}
459
460
/* Appends a property of type 'type' to 'msg' whose contents are padding to
461
 * 8-byte alignment followed by 'nested'.  This is a suitable way to add nested
462
 * properties to 'msg'. */
463
void
464
ofpprop_put_nested(struct ofpbuf *msg, uint64_t type,
465
                   const struct ofpbuf *nested)
466
0
{
467
0
    size_t start = ofpprop_start_nested(msg, type);
468
0
    ofpbuf_put(msg, nested->data, nested->size);
469
0
    ofpprop_end(msg, start);
470
0
}
471
472
/* Appends a header for a property of type 'type' to 'msg'.  The caller should
473
 * add the contents of the property to 'msg', then finish it by calling
474
 * ofpprop_end().  Returns the offset of the beginning of the property (to pass
475
 * to ofpprop_end() later). */
476
size_t
477
ofpprop_start(struct ofpbuf *msg, uint64_t type)
478
0
{
479
0
    size_t start_ofs = msg->size;
480
0
    if (!ofpprop_is_experimenter(type)) {
481
0
        struct ofp_prop_header *oph = ofpbuf_put_uninit(msg, sizeof *oph);
482
0
        oph->type = htons(type);
483
0
        oph->len = htons(4);
484
0
    } else {
485
0
        struct ofp_prop_experimenter *ope
486
0
            = ofpbuf_put_uninit(msg, sizeof *ope);
487
0
        ope->type = htons(0xffff);
488
0
        ope->len = htons(12);
489
0
        ope->experimenter = htonl(ofpprop_type_to_exp_id(type));
490
0
        ope->exp_type = htonl(ofpprop_type_to_exp_type(type));
491
0
    }
492
0
    return start_ofs;
493
0
}
494
495
/* Finishes serializing a property that was begun with ofpprop_start(), by
496
 * padding 'msg' to a multiple of 8 bytes and updating the property's length.
497
 * 'start_ofs' should be the offset of the beginning of the property, as
498
 * returned by ofpprop_start(). */
499
void
500
ofpprop_end(struct ofpbuf *msg, size_t start_ofs)
501
0
{
502
0
    struct ofp_prop_header *oph;
503
504
0
    oph = ofpbuf_at_assert(msg, start_ofs, sizeof *oph);
505
0
    oph->len = htons(msg->size - start_ofs);
506
0
    ofpbuf_padto(msg, ROUND_UP(msg->size, 8));
507
0
}
508
509
/* Appends a header for a property of type 'type' to 'msg', followed by padding
510
 * suitable for putting nested properties into the property; that is, padding
511
 * to an 8-byte alignment.
512
 *
513
 * This otherwise works like ofpprop_start().
514
 *
515
 * There's no need for ofpprop_end_nested(), because ofpprop_end() works fine
516
 * for this case. */
517
size_t
518
ofpprop_start_nested(struct ofpbuf *msg, uint64_t type)
519
0
{
520
0
    size_t start_ofs = ofpprop_start(msg, type);
521
0
    ofpbuf_padto(msg, ROUND_UP(msg->size, 8));
522
0
    return start_ofs;
523
0
}
524
525
enum ofperr
526
ofpprop_unknown(struct vlog_module *module, bool loose, const char *msg,
527
                uint64_t type)
528
0
{
529
0
    bool is_experimenter = ofpprop_is_experimenter(type);
530
531
0
    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 5);
532
0
    enum vlog_level level = loose ? VLL_DBG : VLL_WARN;
533
0
    if (!is_experimenter) {
534
0
        vlog_rate_limit(module, level, &rl, "unknown %s property type %"PRId64,
535
0
                        msg, type);
536
0
    } else {
537
0
        vlog_rate_limit(module, level, &rl,
538
0
                        "unknown %s property type for exp_id 0x%"PRIx32", "
539
0
                        "exp_type %"PRId32, msg,
540
0
                        ofpprop_type_to_exp_id(type),
541
0
                        ofpprop_type_to_exp_type(type));
542
0
    }
543
544
    /* There's an error OFPBPC_BAD_EXPERIMENTER that we could use for
545
     * experimenter IDs that we don't know at all, but that seems like a
546
     * difficult distinction and OFPERR_OFPBPC_BAD_EXP_TYPE communicates the
547
     * problem quite well. */
548
0
    return (loose ? 0
549
0
            : is_experimenter ? OFPERR_OFPBPC_BAD_EXP_TYPE
550
0
            : OFPERR_OFPBPC_BAD_TYPE);
551
0
}
552