Coverage Report

Created: 2026-07-16 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openvswitch/lib/netlink.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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 "netlink.h"
19
#include <errno.h>
20
#include <inttypes.h>
21
#include <sys/types.h>
22
#include <unistd.h>
23
#include "coverage.h"
24
#include "flow.h"
25
#include "netlink-protocol.h"
26
#include "openvswitch/ofpbuf.h"
27
#include "timeval.h"
28
#include "unaligned.h"
29
#include "openvswitch/vlog.h"
30
#include "util.h"
31
32
VLOG_DEFINE_THIS_MODULE(netlink);
33
34
/* A single (bad) Netlink message can in theory dump out many, many log
35
 * messages, so the burst size is set quite high here to avoid missing useful
36
 * information.  Also, at high logging levels we log *all* Netlink messages. */
37
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 600);
38
39
/* Returns the nlmsghdr at the head of 'msg'.
40
 *
41
 * 'msg' must be at least as large as a nlmsghdr. */
42
struct nlmsghdr *
43
nl_msg_nlmsghdr(const struct ofpbuf *msg)
44
0
{
45
0
    return ofpbuf_at_assert(msg, 0, NLMSG_HDRLEN);
46
0
}
47
48
/* Returns the genlmsghdr just past 'msg''s nlmsghdr.
49
 *
50
 * Returns a null pointer if 'msg' is not large enough to contain an nlmsghdr
51
 * and a genlmsghdr. */
52
struct genlmsghdr *
53
nl_msg_genlmsghdr(const struct ofpbuf *msg)
54
0
{
55
0
    return ofpbuf_at(msg, NLMSG_HDRLEN, GENL_HDRLEN);
56
0
}
57
58
/* Parses the ext ack netlink attributes and, if successful, a pointer
59
 * to the error message, if included, is stored in '*errmsg'. */
60
static void
61
nl_parse_ext_ack(const struct ofpbuf *msg, size_t offset, const char **errmsg)
62
0
{
63
0
    static const struct nl_policy policy[] = {
64
0
        [NLMSGERR_ATTR_MSG]  = { .type = NL_A_STRING, .optional = true },
65
0
    };
66
0
    struct nlattr *attrs[ARRAY_SIZE(policy)];
67
68
0
    if (!nl_policy_parse(msg, offset, policy, attrs, ARRAY_SIZE(policy))) {
69
0
        VLOG_ERR_RL(&rl, "Failed to parse extended ack data");
70
0
        return;
71
0
    }
72
73
0
    if (attrs[NLMSGERR_ATTR_MSG]) {
74
0
        *errmsg = nl_attr_get_string(attrs[NLMSGERR_ATTR_MSG]);
75
0
    }
76
0
}
77
78
/* If 'buffer' is a NLMSG_ERROR message, stores 0 in '*errorp' if it is an ACK
79
 * message, otherwise a positive errno value, and returns true.  If 'buffer' is
80
 * not an NLMSG_ERROR message, returns false.
81
 *
82
 * 'msg' must be at least as large as a nlmsghdr. */
83
bool
84
nl_msg_nlmsgerr(const struct ofpbuf *msg, int *errorp, const char **attr_msg)
85
0
{
86
0
    struct nlmsghdr *nlh = nl_msg_nlmsghdr(msg);
87
88
0
    if (nlh->nlmsg_type == NLMSG_ERROR) {
89
0
        struct nlmsgerr *err = ofpbuf_at(msg, NLMSG_HDRLEN, sizeof *err);
90
0
        int code = EPROTO;
91
0
        if (!err) {
92
0
            VLOG_ERR_RL(&rl, "received invalid nlmsgerr (%"PRIu32" bytes < %"PRIuSIZE")",
93
0
                        msg->size, NLMSG_HDRLEN + sizeof *err);
94
0
        } else if (err->error <= 0 && err->error > INT_MIN) {
95
0
            code = -err->error;
96
0
            if (attr_msg && err->error != 0 &&
97
0
                (nlh->nlmsg_flags & NLM_F_ACK_TLVS)) {
98
0
                size_t offt =  NLMSG_HDRLEN + sizeof *err;
99
100
0
                if (!(nlh->nlmsg_flags & NLM_F_CAPPED)) {
101
0
                    offt += (err->msg.nlmsg_len - NLMSG_HDRLEN);
102
0
                }
103
0
                nl_parse_ext_ack(msg, offt, attr_msg);
104
0
            }
105
0
        }
106
0
        if (errorp) {
107
0
            *errorp = code;
108
0
        }
109
0
        return true;
110
0
    } else {
111
0
        return false;
112
0
    }
113
0
}
114
115
/* Ensures that 'b' has room for at least 'size' bytes plus netlink padding at
116
 * its tail end, reallocating and copying its data if necessary. */
117
void
118
nl_msg_reserve(struct ofpbuf *msg, size_t size)
119
0
{
120
0
    ofpbuf_prealloc_tailroom(msg, NLMSG_ALIGN(size));
121
0
}
122
123
/* Puts a nlmsghdr at the beginning of 'msg', which must be initially empty.
124
 * Uses the given 'type' and 'flags'.  'expected_payload' should be
125
 * an estimate of the number of payload bytes to be supplied; if the size of
126
 * the payload is unknown a value of 0 is acceptable.
127
 *
128
 * 'type' is ordinarily an enumerated value specific to the Netlink protocol
129
 * (e.g. RTM_NEWLINK, for NETLINK_ROUTE protocol).  For Generic Netlink, 'type'
130
 * is the family number obtained via nl_lookup_genl_family().
131
 *
132
 * 'flags' is a bit-mask that indicates what kind of request is being made.  It
133
 * is often NLM_F_REQUEST indicating that a request is being made, commonly
134
 * or'd with NLM_F_ACK to request an acknowledgement.
135
 *
136
 * Sets the new nlmsghdr's nlmsg_len, nlmsg_seq, and nlmsg_pid fields to 0 for
137
 * now.  Functions that send Netlink messages will fill these in just before
138
 * sending the message.
139
 *
140
 * nl_msg_put_genlmsghdr() is more convenient for composing a Generic Netlink
141
 * message. */
142
void
143
nl_msg_put_nlmsghdr(struct ofpbuf *msg,
144
                    size_t expected_payload, uint32_t type, uint32_t flags)
145
0
{
146
0
    struct nlmsghdr *nlmsghdr;
147
148
0
    ovs_assert(msg->size == 0);
149
150
0
    nl_msg_reserve(msg, NLMSG_HDRLEN + expected_payload);
151
0
    nlmsghdr = nl_msg_put_uninit(msg, NLMSG_HDRLEN);
152
0
    nlmsghdr->nlmsg_len = 0;
153
0
    nlmsghdr->nlmsg_type = type;
154
0
    nlmsghdr->nlmsg_flags = flags;
155
0
    nlmsghdr->nlmsg_seq = 0;
156
0
    nlmsghdr->nlmsg_pid = 0;
157
0
}
158
159
/* Puts a nlmsghdr and genlmsghdr at the beginning of 'msg', which must be
160
 * initially empty.  'expected_payload' should be an estimate of the number of
161
 * payload bytes to be supplied; if the size of the payload is unknown a value
162
 * of 0 is acceptable.
163
 *
164
 * 'family' is the family number obtained via nl_lookup_genl_family().
165
 *
166
 * 'flags' is a bit-mask that indicates what kind of request is being made.  It
167
 * is often NLM_F_REQUEST indicating that a request is being made, commonly
168
 * or'd with NLM_F_ACK to request an acknowledgement.
169
 *
170
 * 'cmd' is an enumerated value specific to the Generic Netlink family
171
 * (e.g. CTRL_CMD_NEWFAMILY for the GENL_ID_CTRL family).
172
 *
173
 * 'version' is a version number specific to the family and command (often 1).
174
 *
175
 * Sets the new nlmsghdr's nlmsg_pid field to 0 for now.  nl_sock_send() will
176
 * fill it in just before sending the message.
177
 *
178
 * nl_msg_put_nlmsghdr() should be used to compose Netlink messages that are
179
 * not Generic Netlink messages. */
180
void
181
nl_msg_put_genlmsghdr(struct ofpbuf *msg, size_t expected_payload,
182
                      int family, uint32_t flags, uint8_t cmd, uint8_t version)
183
0
{
184
0
    struct genlmsghdr *genlmsghdr;
185
186
0
    nl_msg_put_nlmsghdr(msg, GENL_HDRLEN + expected_payload, family, flags);
187
0
    ovs_assert(msg->size == NLMSG_HDRLEN);
188
0
    genlmsghdr = nl_msg_put_uninit(msg, GENL_HDRLEN);
189
0
    genlmsghdr->cmd = cmd;
190
0
    genlmsghdr->version = version;
191
0
    genlmsghdr->reserved = 0;
192
0
}
193
194
/* Appends the 'size' bytes of data in 'p', plus Netlink padding if needed, to
195
 * the tail end of 'msg'.  Data in 'msg' is reallocated and copied if
196
 * necessary. */
197
void
198
nl_msg_put(struct ofpbuf *msg, const void *data, size_t size)
199
0
{
200
0
    memcpy(nl_msg_put_uninit(msg, size), data, size);
201
0
}
202
203
/* Appends 'size' bytes of data, plus Netlink padding if needed, to the tail
204
 * end of 'msg', reallocating and copying its data if necessary.  Returns a
205
 * pointer to the first byte of the new data, which is left uninitialized. */
206
void *
207
nl_msg_put_uninit(struct ofpbuf *msg, size_t size)
208
0
{
209
0
    size_t pad = PAD_SIZE(size, NLMSG_ALIGNTO);
210
0
    char *p = ofpbuf_put_uninit(msg, size + pad);
211
0
    if (pad) {
212
0
        memset(p + size, 0, pad);
213
0
    }
214
0
    return p;
215
0
}
216
217
/* Prepends the 'size' bytes of data in 'p', plus Netlink padding if needed, to
218
 * the head end of 'msg'.  Data in 'msg' is reallocated and copied if
219
 * necessary. */
220
void
221
nl_msg_push(struct ofpbuf *msg, const void *data, size_t size)
222
0
{
223
0
    memcpy(nl_msg_push_uninit(msg, size), data, size);
224
0
}
225
226
/* Prepends 'size' bytes of data, plus Netlink padding if needed, to the head
227
 * end of 'msg', reallocating and copying its data if necessary.  Returns a
228
 * pointer to the first byte of the new data, which is left uninitialized. */
229
void *
230
nl_msg_push_uninit(struct ofpbuf *msg, size_t size)
231
0
{
232
0
    size_t pad = PAD_SIZE(size, NLMSG_ALIGNTO);
233
0
    char *p = ofpbuf_push_uninit(msg, size + pad);
234
0
    if (pad) {
235
0
        memset(p + size, 0, pad);
236
0
    }
237
0
    return p;
238
0
}
239
240
/* Appends a Netlink attribute of the given 'type' and room for 'size' bytes of
241
 * data as its payload, plus Netlink padding if needed, to the tail end of
242
 * 'msg', reallocating and copying its data if necessary.  Returns a pointer to
243
 * the first byte of data in the attribute, which is left uninitialized. */
244
void *
245
nl_msg_put_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size)
246
0
{
247
0
    size_t total_size = NLA_HDRLEN + size;
248
0
    struct nlattr* nla = nl_msg_put_uninit(msg, total_size);
249
0
    ovs_assert(!nl_attr_oversized(size));
250
0
    nla->nla_len = total_size;
251
0
    nla->nla_type = type;
252
0
    return nla + 1;
253
0
}
254
255
/* Appends a Netlink attribute of the given 'type' and room for 'size' bytes of
256
 * data as its payload, plus Netlink padding if needed, to the tail end of
257
 * 'msg', reallocating and copying its data if necessary.  Returns a pointer to
258
 * the first byte of data in the attribute, which is zeroed. */
259
void *
260
nl_msg_put_unspec_zero(struct ofpbuf *msg, uint16_t type, size_t size)
261
0
{
262
0
    void *data = nl_msg_put_unspec_uninit(msg, type, size);
263
0
    memset(data, 0, size);
264
0
    return data;
265
0
}
266
267
/* Appends a Netlink attribute of the given 'type' and the 'size' bytes of
268
 * 'data' as its payload, to the tail end of 'msg', reallocating and copying
269
 * its data if necessary. */
270
void
271
nl_msg_put_unspec(struct ofpbuf *msg, uint16_t type,
272
                  const void *data, size_t size)
273
0
{
274
0
    void *ptr;
275
276
0
    ptr = nl_msg_put_unspec_uninit(msg, type, size);
277
0
    nullable_memcpy(ptr, data, size);
278
0
}
279
280
/* Appends a Netlink attribute of the given 'type' and no payload to 'msg'.
281
 * (Some Netlink protocols use the presence or absence of an attribute as a
282
 * Boolean flag.) */
283
void
284
nl_msg_put_flag(struct ofpbuf *msg, uint16_t type)
285
0
{
286
0
    nl_msg_put_unspec(msg, type, NULL, 0);
287
0
}
288
289
/* Appends a Netlink attribute of the given 'type' and the given 8-bit 'value'
290
 * to 'msg'. */
291
void
292
nl_msg_put_u8(struct ofpbuf *msg, uint16_t type, uint8_t value)
293
0
{
294
0
    nl_msg_put_unspec(msg, type, &value, sizeof value);
295
0
}
296
297
/* Appends a Netlink attribute of the given 'type' and the given 16-bit host
298
 * byte order 'value' to 'msg'. */
299
void
300
nl_msg_put_u16(struct ofpbuf *msg, uint16_t type, uint16_t value)
301
0
{
302
0
    nl_msg_put_unspec(msg, type, &value, sizeof value);
303
0
}
304
305
/* Appends a Netlink attribute of the given 'type' and the given 32-bit host
306
 * byte order 'value' to 'msg'. */
307
void
308
nl_msg_put_u32(struct ofpbuf *msg, uint16_t type, uint32_t value)
309
0
{
310
0
    nl_msg_put_unspec(msg, type, &value, sizeof value);
311
0
}
312
313
/* Appends a Netlink attribute of the given 'type' and the given 64-bit host
314
 * byte order 'value' to 'msg'. */
315
void
316
nl_msg_put_u64(struct ofpbuf *msg, uint16_t type, uint64_t value)
317
0
{
318
0
    nl_msg_put_unspec(msg, type, &value, sizeof value);
319
0
}
320
321
/* Appends a Netlink attribute of the given 'type' and the given 128-bit host
322
 * byte order 'value' to 'msg'. */
323
void
324
nl_msg_put_u128(struct ofpbuf *msg, uint16_t type, ovs_u128 value)
325
0
{
326
0
    nl_msg_put_unspec(msg, type, &value, sizeof value);
327
0
}
328
329
/* Appends a Netlink attribute of the given 'type' and the given 16-bit network
330
 * byte order 'value' to 'msg'. */
331
void
332
nl_msg_put_be16(struct ofpbuf *msg, uint16_t type, ovs_be16 value)
333
0
{
334
0
    nl_msg_put_unspec(msg, type, &value, sizeof value);
335
0
}
336
337
/* Appends a Netlink attribute of the given 'type' and the given 32-bit network
338
 * byte order 'value' to 'msg'. */
339
void
340
nl_msg_put_be32(struct ofpbuf *msg, uint16_t type, ovs_be32 value)
341
0
{
342
0
    nl_msg_put_unspec(msg, type, &value, sizeof value);
343
0
}
344
345
/* Appends a Netlink attribute of the given 'type' and the given 64-bit network
346
 * byte order 'value' to 'msg'. */
347
void
348
nl_msg_put_be64(struct ofpbuf *msg, uint16_t type, ovs_be64 value)
349
0
{
350
0
    nl_msg_put_unspec(msg, type, &value, sizeof value);
351
0
}
352
353
/* Appends a Netlink attribute of the given 'type' and the given 128-bit
354
 * network byte order 'value' to 'msg'. */
355
void
356
nl_msg_put_be128(struct ofpbuf *msg, uint16_t type, ovs_be128 value)
357
0
{
358
0
    nl_msg_put_unspec(msg, type, &value, sizeof value);
359
0
}
360
361
/* Appends a Netlink attribute of the given 'type' and the given IPv6
362
 * address order 'value' to 'msg'. */
363
void
364
nl_msg_put_in6_addr(struct ofpbuf *msg, uint16_t type,
365
                    const struct in6_addr *value)
366
0
{
367
0
    nl_msg_put_unspec(msg, type, value, sizeof *value);
368
0
}
369
370
/* Appends a Netlink attribute of the given 'type' and the given odp_port_t
371
 * 'value' to 'msg'. */
372
void
373
nl_msg_put_odp_port(struct ofpbuf *msg, uint16_t type, odp_port_t value)
374
0
{
375
0
    nl_msg_put_u32(msg, type, odp_to_u32(value));
376
0
}
377
378
/* Appends a Netlink attribute of the given 'type' with the 'len' characters
379
 * of 'value', followed by the null byte to 'msg'. */
380
void
381
nl_msg_put_string__(struct ofpbuf *msg, uint16_t type, const char *value,
382
                    size_t len)
383
0
{
384
0
    char *data = nl_msg_put_unspec_uninit(msg, type, len + 1);
385
386
0
    memcpy(data, value, len);
387
0
    data[len] = '\0';
388
0
}
389
390
/* Appends a Netlink attribute of the given 'type' and the given
391
 * null-terminated string 'value' to 'msg'. */
392
void
393
nl_msg_put_string(struct ofpbuf *msg, uint16_t type, const char *value)
394
0
{
395
0
    nl_msg_put_unspec(msg, type, value, strlen(value) + 1);
396
0
}
397
398
/* Prepends a Netlink attribute of the given 'type' and room for 'size' bytes
399
 * of data as its payload, plus Netlink padding if needed, to the head end of
400
 * 'msg', reallocating and copying its data if necessary.  Returns a pointer to
401
 * the first byte of data in the attribute, which is left uninitialized. */
402
void *
403
nl_msg_push_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size)
404
0
{
405
0
    size_t total_size = NLA_HDRLEN + size;
406
0
    struct nlattr* nla = nl_msg_push_uninit(msg, total_size);
407
0
    ovs_assert(!nl_attr_oversized(size));
408
0
    nla->nla_len = total_size;
409
0
    nla->nla_type = type;
410
0
    return nla + 1;
411
0
}
412
413
/* Prepends a Netlink attribute of the given 'type' and the 'size' bytes of
414
 * 'data' as its payload, to the head end of 'msg', reallocating and copying
415
 * its data if necessary.  Returns a pointer to the first byte of data in the
416
 * attribute, which is left uninitialized. */
417
void
418
nl_msg_push_unspec(struct ofpbuf *msg, uint16_t type,
419
                  const void *data, size_t size)
420
0
{
421
0
    memcpy(nl_msg_push_unspec_uninit(msg, type, size), data, size);
422
0
}
423
424
/* Prepends a Netlink attribute of the given 'type' and no payload to 'msg'.
425
 * (Some Netlink protocols use the presence or absence of an attribute as a
426
 * Boolean flag.) */
427
void
428
nl_msg_push_flag(struct ofpbuf *msg, uint16_t type)
429
0
{
430
0
    nl_msg_push_unspec_uninit(msg, type, 0);
431
0
}
432
433
/* Prepends a Netlink attribute of the given 'type' and the given 8-bit 'value'
434
 * to 'msg'. */
435
void
436
nl_msg_push_u8(struct ofpbuf *msg, uint16_t type, uint8_t value)
437
0
{
438
0
    nl_msg_push_unspec(msg, type, &value, sizeof value);
439
0
}
440
441
/* Prepends a Netlink attribute of the given 'type' and the given 16-bit host
442
 * byte order 'value' to 'msg'. */
443
void
444
nl_msg_push_u16(struct ofpbuf *msg, uint16_t type, uint16_t value)
445
0
{
446
0
    nl_msg_push_unspec(msg, type, &value, sizeof value);
447
0
}
448
449
/* Prepends a Netlink attribute of the given 'type' and the given 32-bit host
450
 * byte order 'value' to 'msg'. */
451
void
452
nl_msg_push_u32(struct ofpbuf *msg, uint16_t type, uint32_t value)
453
0
{
454
0
    nl_msg_push_unspec(msg, type, &value, sizeof value);
455
0
}
456
457
/* Prepends a Netlink attribute of the given 'type' and the given 64-bit host
458
 * byte order 'value' to 'msg'. */
459
void
460
nl_msg_push_u64(struct ofpbuf *msg, uint16_t type, uint64_t value)
461
0
{
462
0
    nl_msg_push_unspec(msg, type, &value, sizeof value);
463
0
}
464
465
/* Prepends a Netlink attribute of the given 'type' and the given 128-bit host
466
 * byte order 'value' to 'msg'. */
467
void
468
nl_msg_push_u128(struct ofpbuf *msg, uint16_t type, ovs_u128 value)
469
0
{
470
0
    nl_msg_push_unspec(msg, type, &value, sizeof value);
471
0
}
472
473
/* Prepends a Netlink attribute of the given 'type' and the given 16-bit
474
 * network byte order 'value' to 'msg'. */
475
void
476
nl_msg_push_be16(struct ofpbuf *msg, uint16_t type, ovs_be16 value)
477
0
{
478
0
    nl_msg_push_unspec(msg, type, &value, sizeof value);
479
0
}
480
481
/* Prepends a Netlink attribute of the given 'type' and the given 32-bit
482
 * network byte order 'value' to 'msg'. */
483
void
484
nl_msg_push_be32(struct ofpbuf *msg, uint16_t type, ovs_be32 value)
485
0
{
486
0
    nl_msg_push_unspec(msg, type, &value, sizeof value);
487
0
}
488
489
/* Prepends a Netlink attribute of the given 'type' and the given 64-bit
490
 * network byte order 'value' to 'msg'. */
491
void
492
nl_msg_push_be64(struct ofpbuf *msg, uint16_t type, ovs_be64 value)
493
0
{
494
0
    nl_msg_push_unspec(msg, type, &value, sizeof value);
495
0
}
496
497
/* Prepends a Netlink attribute of the given 'type' and the given 128-bit
498
 * network byte order 'value' to 'msg'. */
499
void
500
nl_msg_push_be128(struct ofpbuf *msg, uint16_t type, ovs_be128 value)
501
0
{
502
0
    nl_msg_push_unspec(msg, type, &value, sizeof value);
503
0
}
504
505
/* Prepends a Netlink attribute of the given 'type' and the given
506
 * null-terminated string 'value' to 'msg'. */
507
void
508
nl_msg_push_string(struct ofpbuf *msg, uint16_t type, const char *value)
509
0
{
510
0
    nl_msg_push_unspec(msg, type, value, strlen(value) + 1);
511
0
}
512
513
/* Adds the header for nested Netlink attributes to 'msg', with the specified
514
 * 'type', and returns the header's offset within 'msg'.  The caller should add
515
 * the content for the nested Netlink attribute to 'msg' (e.g. using the other
516
 * nl_msg_*() functions), and then pass the returned offset to
517
 * nl_msg_end_nested() to finish up the nested attributes. */
518
size_t
519
nl_msg_start_nested(struct ofpbuf *msg, uint16_t type)
520
0
{
521
0
    size_t offset = msg->size;
522
0
    nl_msg_put_unspec_uninit(msg, type, 0);
523
0
    return offset;
524
0
}
525
526
/* Adds the header for nested Netlink attributes to 'msg', with the specified
527
 * 'type', and returns the header's offset within 'msg'. It's similar to
528
 * nl_msg_start_nested() and uses NLA_F_NESTED flag mandatorily. */
529
size_t
530
nl_msg_start_nested_with_flag(struct ofpbuf *msg, uint16_t type)
531
0
{
532
0
    return nl_msg_start_nested(msg, type | NLA_F_NESTED);
533
0
}
534
535
/* Finalizes a nested Netlink attribute in 'msg'.  'offset' should be the value
536
 * returned by nl_msg_start_nested(). */
537
void
538
nl_msg_end_nested(struct ofpbuf *msg, size_t offset)
539
0
{
540
0
    struct nlattr *attr = ofpbuf_at_assert(msg, offset, sizeof *attr);
541
0
    ovs_assert(!nl_attr_oversized(msg->size - offset - NLA_HDRLEN));
542
0
    attr->nla_len = msg->size - offset;
543
0
}
544
545
/* Cancel a nested Netlink attribute in 'msg'.  'offset' should be the value
546
 * returned by nl_msg_start_nested(). */
547
void
548
nl_msg_cancel_nested(struct ofpbuf *msg, size_t offset)
549
0
{
550
0
    msg->size = offset;
551
0
}
552
553
/* Same as nls_msg_end_nested() when the nested Netlink contains non empty
554
 * message. Otherwise, drop the nested message header from 'msg'.
555
 *
556
 * Return true if the nested message has been dropped.  */
557
bool
558
nl_msg_end_non_empty_nested(struct ofpbuf *msg, size_t offset)
559
0
{
560
0
    nl_msg_end_nested(msg, offset);
561
562
0
    struct nlattr *attr = ofpbuf_at_assert(msg, offset, sizeof *attr);
563
0
    if (!nl_attr_get_size(attr)) {
564
0
        nl_msg_cancel_nested(msg, offset);
565
0
        return true;
566
0
    } else {
567
0
        return false;
568
0
    }
569
0
}
570
571
/* Appends a nested Netlink attribute of the given 'type', with the 'size'
572
 * bytes of content starting at 'data', to 'msg'. */
573
void
574
nl_msg_put_nested(struct ofpbuf *msg,
575
                  uint16_t type, const void *data, size_t size)
576
0
{
577
0
    size_t offset = nl_msg_start_nested(msg, type);
578
0
    nl_msg_put(msg, data, size);
579
0
    nl_msg_end_nested(msg, offset);
580
0
}
581
582
/* Inserts a Netlink attribute header of the given 'type' at 'offset' in
583
 * 'msg', enclosing 'size' bytes of existing data as its payload. */
584
void
585
nl_msg_wrap_nested(struct ofpbuf *msg, uint16_t type,
586
                   size_t offset, size_t size)
587
0
{
588
0
    struct nlattr nla = { .nla_len = NLA_HDRLEN + size, .nla_type = type };
589
590
0
    ofpbuf_insert(msg, offset, &nla, sizeof nla);
591
0
}
592
593
/* Reset message size to offset. */
594
void
595
nl_msg_reset_size(struct ofpbuf *msg, size_t offset)
596
0
{
597
0
    msg->size = offset;
598
0
}
599
600
/* If 'buffer' begins with a valid "struct nlmsghdr", pulls the header and its
601
 * payload off 'buffer', stores header and payload in 'msg->data' and
602
 * 'msg->size', and returns a pointer to the header.
603
 *
604
 * If 'buffer' does not begin with a "struct nlmsghdr" or begins with one that
605
 * is invalid, returns NULL and clears 'buffer' and 'msg'. */
606
struct nlmsghdr *
607
nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg)
608
0
{
609
0
    if (buffer->size >= sizeof(struct nlmsghdr)) {
610
0
        struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(buffer);
611
0
        size_t len = nlmsghdr->nlmsg_len;
612
0
        if (len >= sizeof *nlmsghdr && len <= buffer->size) {
613
0
            ofpbuf_use_const(msg, nlmsghdr, len);
614
0
            ofpbuf_pull(buffer, len);
615
0
            return nlmsghdr;
616
0
        }
617
0
    }
618
619
0
    ofpbuf_clear(buffer);
620
0
    msg->data = NULL;
621
0
    msg->size = 0;
622
0
    return NULL;
623
0
}
624
625
/* Returns true if a Netlink attribute with a payload that is 'payload_size'
626
 * bytes long would be oversized, that is, if it's not possible to create an
627
 * nlattr of that size because its size wouldn't fit in the 16-bit nla_len
628
 * field. */
629
bool
630
nl_attr_oversized(size_t payload_size)
631
0
{
632
0
    return payload_size > UINT16_MAX - NLA_HDRLEN;
633
0
}
634

635
/* Attributes. */
636
637
/* Returns the bits of 'nla->nla_type' that are significant for determining its
638
 * type. */
639
int
640
nl_attr_type(const struct nlattr *nla)
641
0
{
642
0
    return nla->nla_type & NLA_TYPE_MASK;
643
0
}
644
645
/* Returns the first byte in the payload of attribute 'nla'. */
646
const void *
647
nl_attr_get(const struct nlattr *nla)
648
0
{
649
0
    ovs_assert(nla->nla_len >= NLA_HDRLEN);
650
0
    return nla + 1;
651
0
}
652
653
/* Returns the number of bytes in the payload of attribute 'nla'. */
654
size_t
655
nl_attr_get_size(const struct nlattr *nla)
656
0
{
657
0
    ovs_assert(nla->nla_len >= NLA_HDRLEN);
658
0
    return nla->nla_len - NLA_HDRLEN;
659
0
}
660
661
/* Asserts that 'nla''s payload is at least 'size' bytes long, and returns the
662
 * first byte of the payload. */
663
const void *
664
nl_attr_get_unspec(const struct nlattr *nla, size_t size)
665
0
{
666
0
    ovs_assert(nla->nla_len >= NLA_HDRLEN + size);
667
0
    return nla + 1;
668
0
}
669
670
/* Returns true if 'nla' is nonnull.  (Some Netlink protocols use the presence
671
 * or absence of an attribute as a Boolean flag.) */
672
bool
673
nl_attr_get_flag(const struct nlattr *nla)
674
0
{
675
0
    return nla != NULL;
676
0
}
677
678
#define NL_ATTR_GET_AS(NLA, TYPE) \
679
0
        (*(TYPE*) nl_attr_get_unspec(nla, sizeof(TYPE)))
680
681
/* Returns the 8-bit value in 'nla''s payload.
682
 *
683
 * Asserts that 'nla''s payload is at least 1 byte long. */
684
uint8_t
685
nl_attr_get_u8(const struct nlattr *nla)
686
0
{
687
0
    return NL_ATTR_GET_AS(nla, uint8_t);
688
0
}
689
690
/* Returns the 16-bit host byte order value in 'nla''s payload.
691
 *
692
 * Asserts that 'nla''s payload is at least 2 bytes long. */
693
uint16_t
694
nl_attr_get_u16(const struct nlattr *nla)
695
0
{
696
0
    return NL_ATTR_GET_AS(nla, uint16_t);
697
0
}
698
699
/* Returns the 32-bit host byte order value in 'nla''s payload.
700
 *
701
 * Asserts that 'nla''s payload is at least 4 bytes long. */
702
uint32_t
703
nl_attr_get_u32(const struct nlattr *nla)
704
0
{
705
0
    return NL_ATTR_GET_AS(nla, uint32_t);
706
0
}
707
708
/* Returns the 64-bit host byte order value in 'nla''s payload.
709
 *
710
 * Asserts that 'nla''s payload is at least 8 bytes long. */
711
uint64_t
712
nl_attr_get_u64(const struct nlattr *nla)
713
0
{
714
0
    const ovs_32aligned_u64 *x = nl_attr_get_unspec(nla, sizeof *x);
715
0
    return get_32aligned_u64(x);
716
0
}
717
718
/* Returns the 128-bit host byte order value in 'nla''s payload.
719
 *
720
 * Asserts that 'nla''s payload is at least 16 bytes long. */
721
ovs_u128
722
nl_attr_get_u128(const struct nlattr *nla)
723
0
{
724
0
    const ovs_32aligned_u128 *x = nl_attr_get_unspec(nla, sizeof *x);
725
0
    return get_32aligned_u128(x);
726
0
}
727
728
/* Returns the 16-bit network byte order value in 'nla''s payload.
729
 *
730
 * Asserts that 'nla''s payload is at least 2 bytes long. */
731
ovs_be16
732
nl_attr_get_be16(const struct nlattr *nla)
733
0
{
734
0
    return NL_ATTR_GET_AS(nla, ovs_be16);
735
0
}
736
737
/* Returns the 32-bit network byte order value in 'nla''s payload.
738
 *
739
 * Asserts that 'nla''s payload is at least 4 bytes long. */
740
ovs_be32
741
nl_attr_get_be32(const struct nlattr *nla)
742
0
{
743
0
    return NL_ATTR_GET_AS(nla, ovs_be32);
744
0
}
745
746
/* Returns the 64-bit network byte order value in 'nla''s payload.
747
 *
748
 * Asserts that 'nla''s payload is at least 8 bytes long. */
749
ovs_be64
750
nl_attr_get_be64(const struct nlattr *nla)
751
0
{
752
0
    const ovs_32aligned_be64 *x = nl_attr_get_unspec(nla, sizeof *x);
753
0
    return get_32aligned_be64(x);
754
0
}
755
756
/* Returns the 128-bit network byte order value in 'nla''s payload.
757
 *
758
 * Asserts that 'nla''s payload is at least 16 bytes long. */
759
ovs_be128
760
nl_attr_get_be128(const struct nlattr *nla)
761
0
{
762
0
    const ovs_32aligned_be128 *x = nl_attr_get_unspec(nla, sizeof *x);
763
0
    return get_32aligned_be128(x);
764
0
}
765
766
/* Returns the IPv6 address value in 'nla''s payload.
767
 *
768
 * Asserts that 'nla''s payload is at least 16 bytes long. */
769
struct in6_addr
770
nl_attr_get_in6_addr(const struct nlattr *nla)
771
0
{
772
0
    return NL_ATTR_GET_AS(nla, struct in6_addr);
773
0
}
774
775
/* Returns the 32-bit odp_port_t value in 'nla''s payload.
776
 *
777
 * Asserts that 'nla''s payload is at least 4 bytes long. */
778
odp_port_t
779
nl_attr_get_odp_port(const struct nlattr *nla)
780
0
{
781
0
    return u32_to_odp(nl_attr_get_u32(nla));
782
0
}
783
784
/* Returns the null-terminated string value in 'nla''s payload.
785
 *
786
 * Asserts that 'nla''s payload contains a null-terminated string. */
787
const char *
788
nl_attr_get_string(const struct nlattr *nla)
789
0
{
790
0
    ovs_assert(nla->nla_len > NLA_HDRLEN);
791
0
    ovs_assert(memchr(nl_attr_get(nla), '\0', nla->nla_len - NLA_HDRLEN));
792
0
    return nl_attr_get(nla);
793
0
}
794
795
/* Initializes 'nested' to the payload of 'nla'. */
796
void
797
nl_attr_get_nested(const struct nlattr *nla, struct ofpbuf *nested)
798
0
{
799
0
    ofpbuf_use_const(nested, nl_attr_get(nla), nl_attr_get_size(nla));
800
0
}
801
802
/* Returns the Ethernet Address value in 'nla''s payload. */
803
struct eth_addr
804
nl_attr_get_eth_addr(const struct nlattr *nla)
805
0
{
806
0
    return NL_ATTR_GET_AS(nla, struct eth_addr);
807
0
}
808
809
/* Returns the Infiniband LL Address value in 'nla''s payload. */
810
struct ib_addr
811
nl_attr_get_ib_addr(const struct nlattr *nla)
812
0
{
813
0
    return NL_ATTR_GET_AS(nla, struct ib_addr);
814
0
}
815
816
/* Default minimum payload size for each type of attribute. */
817
static size_t
818
min_attr_len(enum nl_attr_type type)
819
0
{
820
0
    switch (type) {
821
0
    case NL_A_NO_ATTR: return 0;
822
0
    case NL_A_UNSPEC: return 0;
823
0
    case NL_A_U8: return 1;
824
0
    case NL_A_U16: return 2;
825
0
    case NL_A_U32: return 4;
826
0
    case NL_A_U64: return 8;
827
0
    case NL_A_U128: return 16;
828
0
    case NL_A_STRING: return 1;
829
0
    case NL_A_FLAG: return 0;
830
0
    case NL_A_IPV6: return 16;
831
0
    case NL_A_NESTED: return 0;
832
0
    case NL_A_LL_ADDR: return 6; /* ETH_ALEN */
833
0
    case NL_A_RTA_VIA: return 2 + 4; /* struct rtvia + struct in_addr. */
834
0
    case N_NL_ATTR_TYPES: default: OVS_NOT_REACHED();
835
0
    }
836
0
}
837
838
/* Default maximum payload size for each type of attribute. */
839
static size_t
840
max_attr_len(enum nl_attr_type type)
841
0
{
842
0
    switch (type) {
843
0
    case NL_A_NO_ATTR: return SIZE_MAX;
844
0
    case NL_A_UNSPEC: return SIZE_MAX;
845
0
    case NL_A_U8: return 1;
846
0
    case NL_A_U16: return 2;
847
0
    case NL_A_U32: return 4;
848
0
    case NL_A_U64: return 8;
849
0
    case NL_A_U128: return 16;
850
0
    case NL_A_STRING: return SIZE_MAX;
851
0
    case NL_A_FLAG: return SIZE_MAX;
852
0
    case NL_A_IPV6: return 16;
853
0
    case NL_A_NESTED: return SIZE_MAX;
854
0
    case NL_A_LL_ADDR: return 20; /* INFINIBAND_ALEN */
855
0
    case NL_A_RTA_VIA: return 2 + 16; /* struct rtvia + struct in6_addr. */
856
0
    case N_NL_ATTR_TYPES: default: OVS_NOT_REACHED();
857
0
    }
858
0
}
859
860
bool
861
nl_attr_validate(const struct nlattr *nla, const struct nl_policy *policy)
862
0
{
863
0
    uint16_t type = nl_attr_type(nla);
864
0
    size_t min_len;
865
0
    size_t max_len;
866
0
    size_t len;
867
868
0
    if (policy->type == NL_A_NO_ATTR) {
869
0
        return true;
870
0
    }
871
872
    /* Figure out min and max length. */
873
0
    min_len = policy->min_len;
874
0
    if (!min_len) {
875
0
        min_len = min_attr_len(policy->type);
876
0
    }
877
0
    max_len = policy->max_len;
878
0
    if (!max_len) {
879
0
        max_len = max_attr_len(policy->type);
880
0
    }
881
882
    /* Verify length. */
883
0
    len = nl_attr_get_size(nla);
884
0
    if (len < min_len || len > max_len) {
885
0
        VLOG_DBG_RL(&rl, "attr %"PRIu16" length %"PRIuSIZE" not in "
886
0
                    "allowed range %"PRIuSIZE"...%"PRIuSIZE, type, len, min_len, max_len);
887
0
        return false;
888
0
    }
889
890
    /* Strings must be null terminated and must not have embedded nulls. */
891
0
    if (policy->type == NL_A_STRING) {
892
0
        if (((char *) nla)[nla->nla_len - 1]) {
893
0
            VLOG_DBG_RL(&rl, "attr %"PRIu16" lacks null at end", type);
894
0
            return false;
895
0
        }
896
0
        if (memchr(nla + 1, '\0', len - 1) != NULL) {
897
0
            VLOG_DBG_RL(&rl, "attr %"PRIu16" has bad length", type);
898
0
            return false;
899
0
        }
900
0
    }
901
902
0
    return true;
903
0
}
904
905
/* Parses the 'msg' starting at the given 'nla_offset' as a sequence of Netlink
906
 * attributes.  'policy[i]', for 0 <= i < n_attrs, specifies how the attribute
907
 * with nla_type == i is parsed; a pointer to attribute i is stored in
908
 * attrs[i].  Returns true if successful, false on failure.
909
 *
910
 * If the Netlink attributes in 'msg' follow a Netlink header and a Generic
911
 * Netlink header, then 'nla_offset' should be NLMSG_HDRLEN + GENL_HDRLEN. */
912
bool
913
nl_policy_parse(const struct ofpbuf *msg, size_t nla_offset,
914
                const struct nl_policy policy[],
915
                struct nlattr *attrs[], size_t n_attrs)
916
0
{
917
0
    struct nlattr *nla;
918
0
    size_t left;
919
0
    size_t i;
920
921
0
    memset(attrs, 0, n_attrs * sizeof *attrs);
922
923
0
    if (msg->size < nla_offset) {
924
0
        VLOG_DBG_RL(&rl, "missing headers in nl_policy_parse");
925
0
        return false;
926
0
    }
927
928
0
    NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, nla_offset, 0),
929
0
                      msg->size - nla_offset)
930
0
    {
931
0
        uint16_t type = nl_attr_type(nla);
932
0
        if (type < n_attrs && policy[type].type != NL_A_NO_ATTR) {
933
0
            const struct nl_policy *e = &policy[type];
934
0
            if (!nl_attr_validate(nla, e)) {
935
0
                return false;
936
0
            }
937
0
            if (attrs[type]) {
938
0
                VLOG_DBG_RL(&rl, "duplicate attr %"PRIu16, type);
939
0
            }
940
0
            attrs[type] = nla;
941
0
        }
942
0
    }
943
0
    if (left) {
944
0
        VLOG_DBG_RL(&rl, "attributes followed by garbage");
945
0
        return false;
946
0
    }
947
948
0
    for (i = 0; i < n_attrs; i++) {
949
0
        const struct nl_policy *e = &policy[i];
950
0
        if (!e->optional && e->type != NL_A_NO_ATTR && !attrs[i]) {
951
0
            VLOG_DBG_RL(&rl, "required attr %"PRIuSIZE" missing", i);
952
0
            return false;
953
0
        }
954
0
    }
955
0
    return true;
956
0
}
957
958
/* Parses the Netlink attributes within 'nla'.  'policy[i]', for 0 <= i <
959
 * n_attrs, specifies how the attribute with nla_type == i is parsed; a pointer
960
 * to attribute i is stored in attrs[i].  Returns true if successful, false on
961
 * failure. */
962
bool
963
nl_parse_nested(const struct nlattr *nla, const struct nl_policy policy[],
964
                struct nlattr *attrs[], size_t n_attrs)
965
0
{
966
0
    struct ofpbuf buf;
967
968
0
    nl_attr_get_nested(nla, &buf);
969
0
    return nl_policy_parse(&buf, 0, policy, attrs, n_attrs);
970
0
}
971
972
const struct nlattr *
973
nl_attr_find__(const struct nlattr *attrs, size_t size, uint16_t type)
974
0
{
975
0
    const struct nlattr *nla;
976
0
    size_t left;
977
978
0
    NL_ATTR_FOR_EACH (nla, left, attrs, size) {
979
0
        if (nl_attr_type(nla) == type) {
980
0
            return nla;
981
0
        }
982
0
    }
983
0
    return NULL;
984
0
}
985
986
/* Returns the first Netlink attribute within 'buf' with the specified 'type',
987
 * skipping a header of 'hdr_len' bytes at the beginning of 'buf'.
988
 *
989
 * This function does not validate the attribute's length. */
990
const struct nlattr *
991
nl_attr_find(const struct ofpbuf *buf, size_t hdr_len, uint16_t type)
992
0
{
993
0
    return nl_attr_find__(ofpbuf_at(buf, hdr_len, 0), buf->size - hdr_len,
994
0
                          type);
995
0
}
996
997
/* Returns the first Netlink attribute within 'nla' with the specified
998
 * 'type'.
999
 *
1000
 * This function does not validate the attribute's length. */
1001
const struct nlattr *
1002
nl_attr_find_nested(const struct nlattr *nla, uint16_t type)
1003
0
{
1004
0
    return nl_attr_find__(nl_attr_get(nla), nl_attr_get_size(nla), type);
1005
0
}