]>
git.proxmox.com Git - ovs.git/blob - lib/netlink.c
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/types.h>
24 #include "netlink-protocol.h"
27 #include "unaligned.h"
30 VLOG_DEFINE_THIS_MODULE(netlink
);
32 /* A single (bad) Netlink message can in theory dump out many, many log
33 * messages, so the burst size is set quite high here to avoid missing useful
34 * information. Also, at high logging levels we log *all* Netlink messages. */
35 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(60, 600);
37 /* Returns the nlmsghdr at the head of 'msg'.
39 * 'msg' must be at least as large as a nlmsghdr. */
41 nl_msg_nlmsghdr(const struct ofpbuf
*msg
)
43 return ofpbuf_at_assert(msg
, 0, NLMSG_HDRLEN
);
46 /* Returns the genlmsghdr just past 'msg''s nlmsghdr.
48 * Returns a null pointer if 'msg' is not large enough to contain an nlmsghdr
49 * and a genlmsghdr. */
51 nl_msg_genlmsghdr(const struct ofpbuf
*msg
)
53 return ofpbuf_at(msg
, NLMSG_HDRLEN
, GENL_HDRLEN
);
56 /* If 'buffer' is a NLMSG_ERROR message, stores 0 in '*errorp' if it is an ACK
57 * message, otherwise a positive errno value, and returns true. If 'buffer' is
58 * not an NLMSG_ERROR message, returns false.
60 * 'msg' must be at least as large as a nlmsghdr. */
62 nl_msg_nlmsgerr(const struct ofpbuf
*msg
, int *errorp
)
64 if (nl_msg_nlmsghdr(msg
)->nlmsg_type
== NLMSG_ERROR
) {
65 struct nlmsgerr
*err
= ofpbuf_at(msg
, NLMSG_HDRLEN
, sizeof *err
);
68 VLOG_ERR_RL(&rl
, "received invalid nlmsgerr (%zd bytes < %zd)",
69 msg
->size
, NLMSG_HDRLEN
+ sizeof *err
);
70 } else if (err
->error
<= 0 && err
->error
> INT_MIN
) {
82 /* Ensures that 'b' has room for at least 'size' bytes plus netlink padding at
83 * its tail end, reallocating and copying its data if necessary. */
85 nl_msg_reserve(struct ofpbuf
*msg
, size_t size
)
87 ofpbuf_prealloc_tailroom(msg
, NLMSG_ALIGN(size
));
90 /* Puts a nlmsghdr at the beginning of 'msg', which must be initially empty.
91 * Uses the given 'type' and 'flags'. 'expected_payload' should be
92 * an estimate of the number of payload bytes to be supplied; if the size of
93 * the payload is unknown a value of 0 is acceptable.
95 * 'type' is ordinarily an enumerated value specific to the Netlink protocol
96 * (e.g. RTM_NEWLINK, for NETLINK_ROUTE protocol). For Generic Netlink, 'type'
97 * is the family number obtained via nl_lookup_genl_family().
99 * 'flags' is a bit-mask that indicates what kind of request is being made. It
100 * is often NLM_F_REQUEST indicating that a request is being made, commonly
101 * or'd with NLM_F_ACK to request an acknowledgement.
103 * Sets the new nlmsghdr's nlmsg_len, nlmsg_seq, and nlmsg_pid fields to 0 for
104 * now. Functions that send Netlink messages will fill these in just before
105 * sending the message.
107 * nl_msg_put_genlmsghdr() is more convenient for composing a Generic Netlink
110 nl_msg_put_nlmsghdr(struct ofpbuf
*msg
,
111 size_t expected_payload
, uint32_t type
, uint32_t flags
)
113 struct nlmsghdr
*nlmsghdr
;
115 ovs_assert(msg
->size
== 0);
117 nl_msg_reserve(msg
, NLMSG_HDRLEN
+ expected_payload
);
118 nlmsghdr
= nl_msg_put_uninit(msg
, NLMSG_HDRLEN
);
119 nlmsghdr
->nlmsg_len
= 0;
120 nlmsghdr
->nlmsg_type
= type
;
121 nlmsghdr
->nlmsg_flags
= flags
;
122 nlmsghdr
->nlmsg_seq
= 0;
123 nlmsghdr
->nlmsg_pid
= 0;
126 /* Puts a nlmsghdr and genlmsghdr at the beginning of 'msg', which must be
127 * initially empty. 'expected_payload' should be an estimate of the number of
128 * payload bytes to be supplied; if the size of the payload is unknown a value
129 * of 0 is acceptable.
131 * 'family' is the family number obtained via nl_lookup_genl_family().
133 * 'flags' is a bit-mask that indicates what kind of request is being made. It
134 * is often NLM_F_REQUEST indicating that a request is being made, commonly
135 * or'd with NLM_F_ACK to request an acknowledgement.
137 * 'cmd' is an enumerated value specific to the Generic Netlink family
138 * (e.g. CTRL_CMD_NEWFAMILY for the GENL_ID_CTRL family).
140 * 'version' is a version number specific to the family and command (often 1).
142 * Sets the new nlmsghdr's nlmsg_pid field to 0 for now. nl_sock_send() will
143 * fill it in just before sending the message.
145 * nl_msg_put_nlmsghdr() should be used to compose Netlink messages that are
146 * not Generic Netlink messages. */
148 nl_msg_put_genlmsghdr(struct ofpbuf
*msg
, size_t expected_payload
,
149 int family
, uint32_t flags
, uint8_t cmd
, uint8_t version
)
151 struct genlmsghdr
*genlmsghdr
;
153 nl_msg_put_nlmsghdr(msg
, GENL_HDRLEN
+ expected_payload
, family
, flags
);
154 ovs_assert(msg
->size
== NLMSG_HDRLEN
);
155 genlmsghdr
= nl_msg_put_uninit(msg
, GENL_HDRLEN
);
156 genlmsghdr
->cmd
= cmd
;
157 genlmsghdr
->version
= version
;
158 genlmsghdr
->reserved
= 0;
161 /* Appends the 'size' bytes of data in 'p', plus Netlink padding if needed, to
162 * the tail end of 'msg'. Data in 'msg' is reallocated and copied if
165 nl_msg_put(struct ofpbuf
*msg
, const void *data
, size_t size
)
167 memcpy(nl_msg_put_uninit(msg
, size
), data
, size
);
170 /* Appends 'size' bytes of data, plus Netlink padding if needed, to the tail
171 * end of 'msg', reallocating and copying its data if necessary. Returns a
172 * pointer to the first byte of the new data, which is left uninitialized. */
174 nl_msg_put_uninit(struct ofpbuf
*msg
, size_t size
)
176 size_t pad
= NLMSG_ALIGN(size
) - size
;
177 char *p
= ofpbuf_put_uninit(msg
, size
+ pad
);
179 memset(p
+ size
, 0, pad
);
184 /* Prepends the 'size' bytes of data in 'p', plus Netlink padding if needed, to
185 * the head end of 'msg'. Data in 'msg' is reallocated and copied if
188 nl_msg_push(struct ofpbuf
*msg
, const void *data
, size_t size
)
190 memcpy(nl_msg_push_uninit(msg
, size
), data
, size
);
193 /* Prepends 'size' bytes of data, plus Netlink padding if needed, to the head
194 * end of 'msg', reallocating and copying its data if necessary. Returns a
195 * pointer to the first byte of the new data, which is left uninitialized. */
197 nl_msg_push_uninit(struct ofpbuf
*msg
, size_t size
)
199 size_t pad
= NLMSG_ALIGN(size
) - size
;
200 char *p
= ofpbuf_push_uninit(msg
, size
+ pad
);
202 memset(p
+ size
, 0, pad
);
207 /* Appends a Netlink attribute of the given 'type' and room for 'size' bytes of
208 * data as its payload, plus Netlink padding if needed, to the tail end of
209 * 'msg', reallocating and copying its data if necessary. Returns a pointer to
210 * the first byte of data in the attribute, which is left uninitialized. */
212 nl_msg_put_unspec_uninit(struct ofpbuf
*msg
, uint16_t type
, size_t size
)
214 size_t total_size
= NLA_HDRLEN
+ size
;
215 struct nlattr
* nla
= nl_msg_put_uninit(msg
, total_size
);
216 ovs_assert(NLA_ALIGN(total_size
) <= UINT16_MAX
);
217 nla
->nla_len
= total_size
;
218 nla
->nla_type
= type
;
222 /* Appends a Netlink attribute of the given 'type' and the 'size' bytes of
223 * 'data' as its payload, to the tail end of 'msg', reallocating and copying
224 * its data if necessary. Returns a pointer to the first byte of data in the
225 * attribute, which is left uninitialized. */
227 nl_msg_put_unspec(struct ofpbuf
*msg
, uint16_t type
,
228 const void *data
, size_t size
)
230 memcpy(nl_msg_put_unspec_uninit(msg
, type
, size
), data
, size
);
233 /* Appends a Netlink attribute of the given 'type' and no payload to 'msg'.
234 * (Some Netlink protocols use the presence or absence of an attribute as a
237 nl_msg_put_flag(struct ofpbuf
*msg
, uint16_t type
)
239 nl_msg_put_unspec(msg
, type
, NULL
, 0);
242 /* Appends a Netlink attribute of the given 'type' and the given 8-bit 'value'
245 nl_msg_put_u8(struct ofpbuf
*msg
, uint16_t type
, uint8_t value
)
247 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
250 /* Appends a Netlink attribute of the given 'type' and the given 16-bit host
251 * byte order 'value' to 'msg'. */
253 nl_msg_put_u16(struct ofpbuf
*msg
, uint16_t type
, uint16_t value
)
255 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
258 /* Appends a Netlink attribute of the given 'type' and the given 32-bit host
259 * byte order 'value' to 'msg'. */
261 nl_msg_put_u32(struct ofpbuf
*msg
, uint16_t type
, uint32_t value
)
263 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
266 /* Appends a Netlink attribute of the given 'type' and the given 64-bit host
267 * byte order 'value' to 'msg'. */
269 nl_msg_put_u64(struct ofpbuf
*msg
, uint16_t type
, uint64_t value
)
271 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
274 /* Appends a Netlink attribute of the given 'type' and the given 16-bit network
275 * byte order 'value' to 'msg'. */
277 nl_msg_put_be16(struct ofpbuf
*msg
, uint16_t type
, ovs_be16 value
)
279 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
282 /* Appends a Netlink attribute of the given 'type' and the given 32-bit network
283 * byte order 'value' to 'msg'. */
285 nl_msg_put_be32(struct ofpbuf
*msg
, uint16_t type
, ovs_be32 value
)
287 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
290 /* Appends a Netlink attribute of the given 'type' and the given 64-bit network
291 * byte order 'value' to 'msg'. */
293 nl_msg_put_be64(struct ofpbuf
*msg
, uint16_t type
, ovs_be64 value
)
295 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
298 /* Appends a Netlink attribute of the given 'type' and the given
299 * null-terminated string 'value' to 'msg'. */
301 nl_msg_put_string(struct ofpbuf
*msg
, uint16_t type
, const char *value
)
303 nl_msg_put_unspec(msg
, type
, value
, strlen(value
) + 1);
306 /* Prepends a Netlink attribute of the given 'type' and room for 'size' bytes
307 * of data as its payload, plus Netlink padding if needed, to the head end of
308 * 'msg', reallocating and copying its data if necessary. Returns a pointer to
309 * the first byte of data in the attribute, which is left uninitialized. */
311 nl_msg_push_unspec_uninit(struct ofpbuf
*msg
, uint16_t type
, size_t size
)
313 size_t total_size
= NLA_HDRLEN
+ size
;
314 struct nlattr
* nla
= nl_msg_push_uninit(msg
, total_size
);
315 ovs_assert(NLA_ALIGN(total_size
) <= UINT16_MAX
);
316 nla
->nla_len
= total_size
;
317 nla
->nla_type
= type
;
321 /* Prepends a Netlink attribute of the given 'type' and the 'size' bytes of
322 * 'data' as its payload, to the head end of 'msg', reallocating and copying
323 * its data if necessary. Returns a pointer to the first byte of data in the
324 * attribute, which is left uninitialized. */
326 nl_msg_push_unspec(struct ofpbuf
*msg
, uint16_t type
,
327 const void *data
, size_t size
)
329 memcpy(nl_msg_push_unspec_uninit(msg
, type
, size
), data
, size
);
332 /* Prepends a Netlink attribute of the given 'type' and no payload to 'msg'.
333 * (Some Netlink protocols use the presence or absence of an attribute as a
336 nl_msg_push_flag(struct ofpbuf
*msg
, uint16_t type
)
338 nl_msg_push_unspec(msg
, type
, NULL
, 0);
341 /* Prepends a Netlink attribute of the given 'type' and the given 8-bit 'value'
344 nl_msg_push_u8(struct ofpbuf
*msg
, uint16_t type
, uint8_t value
)
346 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
349 /* Prepends a Netlink attribute of the given 'type' and the given 16-bit host
350 * byte order 'value' to 'msg'. */
352 nl_msg_push_u16(struct ofpbuf
*msg
, uint16_t type
, uint16_t value
)
354 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
357 /* Prepends a Netlink attribute of the given 'type' and the given 32-bit host
358 * byte order 'value' to 'msg'. */
360 nl_msg_push_u32(struct ofpbuf
*msg
, uint16_t type
, uint32_t value
)
362 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
365 /* Prepends a Netlink attribute of the given 'type' and the given 64-bit host
366 * byte order 'value' to 'msg'. */
368 nl_msg_push_u64(struct ofpbuf
*msg
, uint16_t type
, uint64_t value
)
370 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
373 /* Prepends a Netlink attribute of the given 'type' and the given 16-bit
374 * network byte order 'value' to 'msg'. */
376 nl_msg_push_be16(struct ofpbuf
*msg
, uint16_t type
, ovs_be16 value
)
378 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
381 /* Prepends a Netlink attribute of the given 'type' and the given 32-bit
382 * network byte order 'value' to 'msg'. */
384 nl_msg_push_be32(struct ofpbuf
*msg
, uint16_t type
, ovs_be32 value
)
386 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
389 /* Prepends a Netlink attribute of the given 'type' and the given 64-bit
390 * network byte order 'value' to 'msg'. */
392 nl_msg_push_be64(struct ofpbuf
*msg
, uint16_t type
, ovs_be64 value
)
394 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
397 /* Prepends a Netlink attribute of the given 'type' and the given
398 * null-terminated string 'value' to 'msg'. */
400 nl_msg_push_string(struct ofpbuf
*msg
, uint16_t type
, const char *value
)
402 nl_msg_push_unspec(msg
, type
, value
, strlen(value
) + 1);
405 /* Adds the header for nested Netlink attributes to 'msg', with the specified
406 * 'type', and returns the header's offset within 'msg'. The caller should add
407 * the content for the nested Netlink attribute to 'msg' (e.g. using the other
408 * nl_msg_*() functions), and then pass the returned offset to
409 * nl_msg_end_nested() to finish up the nested attributes. */
411 nl_msg_start_nested(struct ofpbuf
*msg
, uint16_t type
)
413 size_t offset
= msg
->size
;
414 nl_msg_put_unspec(msg
, type
, NULL
, 0);
418 /* Finalizes a nested Netlink attribute in 'msg'. 'offset' should be the value
419 * returned by nl_msg_start_nested(). */
421 nl_msg_end_nested(struct ofpbuf
*msg
, size_t offset
)
423 struct nlattr
*attr
= ofpbuf_at_assert(msg
, offset
, sizeof *attr
);
424 attr
->nla_len
= msg
->size
- offset
;
427 /* Appends a nested Netlink attribute of the given 'type', with the 'size'
428 * bytes of content starting at 'data', to 'msg'. */
430 nl_msg_put_nested(struct ofpbuf
*msg
,
431 uint16_t type
, const void *data
, size_t size
)
433 size_t offset
= nl_msg_start_nested(msg
, type
);
434 nl_msg_put(msg
, data
, size
);
435 nl_msg_end_nested(msg
, offset
);
438 /* If 'buffer' begins with a valid "struct nlmsghdr", pulls the header and its
439 * payload off 'buffer', stores header and payload in 'msg->data' and
440 * 'msg->size', and returns a pointer to the header.
442 * If 'buffer' does not begin with a "struct nlmsghdr" or begins with one that
443 * is invalid, returns NULL without modifying 'buffer'. */
445 nl_msg_next(struct ofpbuf
*buffer
, struct ofpbuf
*msg
)
447 if (buffer
->size
>= sizeof(struct nlmsghdr
)) {
448 struct nlmsghdr
*nlmsghdr
= nl_msg_nlmsghdr(buffer
);
449 size_t len
= nlmsghdr
->nlmsg_len
;
450 if (len
>= sizeof *nlmsghdr
&& len
<= buffer
->size
) {
451 ofpbuf_use_const(msg
, nlmsghdr
, len
);
452 ofpbuf_pull(buffer
, len
);
464 /* Returns the bits of 'nla->nla_type' that are significant for determining its
467 nl_attr_type(const struct nlattr
*nla
)
469 return nla
->nla_type
& NLA_TYPE_MASK
;
472 /* Returns the first byte in the payload of attribute 'nla'. */
474 nl_attr_get(const struct nlattr
*nla
)
476 ovs_assert(nla
->nla_len
>= NLA_HDRLEN
);
480 /* Returns the number of bytes in the payload of attribute 'nla'. */
482 nl_attr_get_size(const struct nlattr
*nla
)
484 ovs_assert(nla
->nla_len
>= NLA_HDRLEN
);
485 return nla
->nla_len
- NLA_HDRLEN
;
488 /* Asserts that 'nla''s payload is at least 'size' bytes long, and returns the
489 * first byte of the payload. */
491 nl_attr_get_unspec(const struct nlattr
*nla
, size_t size
)
493 ovs_assert(nla
->nla_len
>= NLA_HDRLEN
+ size
);
497 /* Returns true if 'nla' is nonnull. (Some Netlink protocols use the presence
498 * or absence of an attribute as a Boolean flag.) */
500 nl_attr_get_flag(const struct nlattr
*nla
)
505 #define NL_ATTR_GET_AS(NLA, TYPE) \
506 (*(TYPE*) nl_attr_get_unspec(nla, sizeof(TYPE)))
508 /* Returns the 8-bit value in 'nla''s payload.
510 * Asserts that 'nla''s payload is at least 1 byte long. */
512 nl_attr_get_u8(const struct nlattr
*nla
)
514 return NL_ATTR_GET_AS(nla
, uint8_t);
517 /* Returns the 16-bit host byte order value in 'nla''s payload.
519 * Asserts that 'nla''s payload is at least 2 bytes long. */
521 nl_attr_get_u16(const struct nlattr
*nla
)
523 return NL_ATTR_GET_AS(nla
, uint16_t);
526 /* Returns the 32-bit host byte order value in 'nla''s payload.
528 * Asserts that 'nla''s payload is at least 4 bytes long. */
530 nl_attr_get_u32(const struct nlattr
*nla
)
532 return NL_ATTR_GET_AS(nla
, uint32_t);
535 /* Returns the 64-bit host byte order value in 'nla''s payload.
537 * Asserts that 'nla''s payload is at least 8 bytes long. */
539 nl_attr_get_u64(const struct nlattr
*nla
)
541 const ovs_32aligned_u64
*x
= nl_attr_get_unspec(nla
, sizeof *x
);
542 return get_32aligned_u64(x
);
545 /* Returns the 16-bit network byte order value in 'nla''s payload.
547 * Asserts that 'nla''s payload is at least 2 bytes long. */
549 nl_attr_get_be16(const struct nlattr
*nla
)
551 return NL_ATTR_GET_AS(nla
, ovs_be16
);
554 /* Returns the 32-bit network byte order value in 'nla''s payload.
556 * Asserts that 'nla''s payload is at least 4 bytes long. */
558 nl_attr_get_be32(const struct nlattr
*nla
)
560 return NL_ATTR_GET_AS(nla
, ovs_be32
);
563 /* Returns the 64-bit network byte order value in 'nla''s payload.
565 * Asserts that 'nla''s payload is at least 8 bytes long. */
567 nl_attr_get_be64(const struct nlattr
*nla
)
569 const ovs_32aligned_be64
*x
= nl_attr_get_unspec(nla
, sizeof *x
);
570 return get_32aligned_be64(x
);
573 /* Returns the null-terminated string value in 'nla''s payload.
575 * Asserts that 'nla''s payload contains a null-terminated string. */
577 nl_attr_get_string(const struct nlattr
*nla
)
579 ovs_assert(nla
->nla_len
> NLA_HDRLEN
);
580 ovs_assert(memchr(nl_attr_get(nla
), '\0', nla
->nla_len
- NLA_HDRLEN
));
581 return nl_attr_get(nla
);
584 /* Initializes 'nested' to the payload of 'nla'. */
586 nl_attr_get_nested(const struct nlattr
*nla
, struct ofpbuf
*nested
)
588 ofpbuf_use_const(nested
, nl_attr_get(nla
), nl_attr_get_size(nla
));
591 /* Default minimum and maximum payload sizes for each type of attribute. */
592 static const size_t attr_len_range
[][2] = {
593 [0 ... N_NL_ATTR_TYPES
- 1] = { 0, SIZE_MAX
},
594 [NL_A_U8
] = { 1, 1 },
595 [NL_A_U16
] = { 2, 2 },
596 [NL_A_U32
] = { 4, 4 },
597 [NL_A_U64
] = { 8, 8 },
598 [NL_A_STRING
] = { 1, SIZE_MAX
},
599 [NL_A_FLAG
] = { 0, SIZE_MAX
},
600 [NL_A_NESTED
] = { 0, SIZE_MAX
},
604 nl_attr_validate(const struct nlattr
*nla
, const struct nl_policy
*policy
)
606 uint16_t type
= nl_attr_type(nla
);
611 if (policy
->type
== NL_A_NO_ATTR
) {
615 /* Figure out min and max length. */
616 min_len
= policy
->min_len
;
618 min_len
= attr_len_range
[policy
->type
][0];
620 max_len
= policy
->max_len
;
622 max_len
= attr_len_range
[policy
->type
][1];
626 len
= nl_attr_get_size(nla
);
627 if (len
< min_len
|| len
> max_len
) {
628 VLOG_DBG_RL(&rl
, "attr %"PRIu16
" length %zu not in "
629 "allowed range %zu...%zu", type
, len
, min_len
, max_len
);
633 /* Strings must be null terminated and must not have embedded nulls. */
634 if (policy
->type
== NL_A_STRING
) {
635 if (((char *) nla
)[nla
->nla_len
- 1]) {
636 VLOG_DBG_RL(&rl
, "attr %"PRIu16
" lacks null at end", type
);
639 if (memchr(nla
+ 1, '\0', len
- 1) != NULL
) {
640 VLOG_DBG_RL(&rl
, "attr %"PRIu16
" has bad length", type
);
648 /* Parses the 'msg' starting at the given 'nla_offset' as a sequence of Netlink
649 * attributes. 'policy[i]', for 0 <= i < n_attrs, specifies how the attribute
650 * with nla_type == i is parsed; a pointer to attribute i is stored in
651 * attrs[i]. Returns true if successful, false on failure.
653 * If the Netlink attributes in 'msg' follow a Netlink header and a Generic
654 * Netlink header, then 'nla_offset' should be NLMSG_HDRLEN + GENL_HDRLEN. */
656 nl_policy_parse(const struct ofpbuf
*msg
, size_t nla_offset
,
657 const struct nl_policy policy
[],
658 struct nlattr
*attrs
[], size_t n_attrs
)
664 memset(attrs
, 0, n_attrs
* sizeof *attrs
);
666 if (msg
->size
< nla_offset
) {
667 VLOG_DBG_RL(&rl
, "missing headers in nl_policy_parse");
671 NL_ATTR_FOR_EACH (nla
, left
,
672 (struct nlattr
*) ((char *) msg
->data
+ nla_offset
),
673 msg
->size
- nla_offset
)
675 uint16_t type
= nl_attr_type(nla
);
676 if (type
< n_attrs
&& policy
[type
].type
!= NL_A_NO_ATTR
) {
677 const struct nl_policy
*e
= &policy
[type
];
678 if (!nl_attr_validate(nla
, e
)) {
682 VLOG_DBG_RL(&rl
, "duplicate attr %"PRIu16
, type
);
688 VLOG_DBG_RL(&rl
, "attributes followed by garbage");
692 for (i
= 0; i
< n_attrs
; i
++) {
693 const struct nl_policy
*e
= &policy
[i
];
694 if (!e
->optional
&& e
->type
!= NL_A_NO_ATTR
&& !attrs
[i
]) {
695 VLOG_DBG_RL(&rl
, "required attr %zu missing", i
);
702 /* Parses the Netlink attributes within 'nla'. 'policy[i]', for 0 <= i <
703 * n_attrs, specifies how the attribute with nla_type == i is parsed; a pointer
704 * to attribute i is stored in attrs[i]. Returns true if successful, false on
707 nl_parse_nested(const struct nlattr
*nla
, const struct nl_policy policy
[],
708 struct nlattr
*attrs
[], size_t n_attrs
)
712 nl_attr_get_nested(nla
, &buf
);
713 return nl_policy_parse(&buf
, 0, policy
, attrs
, n_attrs
);
716 const struct nlattr
*
717 nl_attr_find__(const struct nlattr
*attrs
, size_t size
, uint16_t type
)
719 const struct nlattr
*nla
;
722 NL_ATTR_FOR_EACH (nla
, left
, attrs
, size
) {
723 if (nl_attr_type(nla
) == type
) {
730 /* Returns the first Netlink attribute within 'buf' with the specified 'type',
731 * skipping a header of 'hdr_len' bytes at the beginning of 'buf'.
733 * This function does not validate the attribute's length. */
734 const struct nlattr
*
735 nl_attr_find(const struct ofpbuf
*buf
, size_t hdr_len
, uint16_t type
)
737 const uint8_t *start
= (const uint8_t *) buf
->data
+ hdr_len
;
738 return nl_attr_find__((const struct nlattr
*) start
, buf
->size
- hdr_len
,
742 /* Returns the first Netlink attribute within 'nla' with the specified
745 * This function does not validate the attribute's length. */
746 const struct nlattr
*
747 nl_attr_find_nested(const struct nlattr
*nla
, uint16_t type
)
749 return nl_attr_find__(nl_attr_get(nla
), nl_attr_get_size(nla
), type
);