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