]>
git.proxmox.com Git - mirror_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.
22 #include <sys/types.h>
25 #include "netlink-protocol.h"
28 #include "unaligned.h"
31 VLOG_DEFINE_THIS_MODULE(netlink
);
33 /* A single (bad) Netlink message can in theory dump out many, many log
34 * messages, so the burst size is set quite high here to avoid missing useful
35 * information. Also, at high logging levels we log *all* Netlink messages. */
36 static struct vlog_rate_limit rl
= VLOG_RATE_LIMIT_INIT(60, 600);
38 /* Returns the nlmsghdr at the head of 'msg'.
40 * 'msg' must be at least as large as a nlmsghdr. */
42 nl_msg_nlmsghdr(const struct ofpbuf
*msg
)
44 return ofpbuf_at_assert(msg
, 0, NLMSG_HDRLEN
);
47 /* Returns the genlmsghdr just past 'msg''s nlmsghdr.
49 * Returns a null pointer if 'msg' is not large enough to contain an nlmsghdr
50 * and a genlmsghdr. */
52 nl_msg_genlmsghdr(const struct ofpbuf
*msg
)
54 return ofpbuf_at(msg
, NLMSG_HDRLEN
, GENL_HDRLEN
);
57 /* If 'buffer' is a NLMSG_ERROR message, stores 0 in '*errorp' if it is an ACK
58 * message, otherwise a positive errno value, and returns true. If 'buffer' is
59 * not an NLMSG_ERROR message, returns false.
61 * 'msg' must be at least as large as a nlmsghdr. */
63 nl_msg_nlmsgerr(const struct ofpbuf
*msg
, int *errorp
)
65 if (nl_msg_nlmsghdr(msg
)->nlmsg_type
== NLMSG_ERROR
) {
66 struct nlmsgerr
*err
= ofpbuf_at(msg
, NLMSG_HDRLEN
, sizeof *err
);
69 VLOG_ERR_RL(&rl
, "received invalid nlmsgerr (%zd bytes < %zd)",
70 msg
->size
, NLMSG_HDRLEN
+ sizeof *err
);
71 } else if (err
->error
<= 0 && err
->error
> INT_MIN
) {
83 /* Ensures that 'b' has room for at least 'size' bytes plus netlink padding at
84 * its tail end, reallocating and copying its data if necessary. */
86 nl_msg_reserve(struct ofpbuf
*msg
, size_t size
)
88 ofpbuf_prealloc_tailroom(msg
, NLMSG_ALIGN(size
));
91 /* Puts a nlmsghdr at the beginning of 'msg', which must be initially empty.
92 * Uses the given 'type' and 'flags'. 'expected_payload' should be
93 * an estimate of the number of payload bytes to be supplied; if the size of
94 * the payload is unknown a value of 0 is acceptable.
96 * 'type' is ordinarily an enumerated value specific to the Netlink protocol
97 * (e.g. RTM_NEWLINK, for NETLINK_ROUTE protocol). For Generic Netlink, 'type'
98 * is the family number obtained via nl_lookup_genl_family().
100 * 'flags' is a bit-mask that indicates what kind of request is being made. It
101 * is often NLM_F_REQUEST indicating that a request is being made, commonly
102 * or'd with NLM_F_ACK to request an acknowledgement.
104 * Sets the new nlmsghdr's nlmsg_len, nlmsg_seq, and nlmsg_pid fields to 0 for
105 * now. Functions that send Netlink messages will fill these in just before
106 * sending the message.
108 * nl_msg_put_genlmsghdr() is more convenient for composing a Generic Netlink
111 nl_msg_put_nlmsghdr(struct ofpbuf
*msg
,
112 size_t expected_payload
, uint32_t type
, uint32_t flags
)
114 struct nlmsghdr
*nlmsghdr
;
116 assert(msg
->size
== 0);
118 nl_msg_reserve(msg
, NLMSG_HDRLEN
+ expected_payload
);
119 nlmsghdr
= nl_msg_put_uninit(msg
, NLMSG_HDRLEN
);
120 nlmsghdr
->nlmsg_len
= 0;
121 nlmsghdr
->nlmsg_type
= type
;
122 nlmsghdr
->nlmsg_flags
= flags
;
123 nlmsghdr
->nlmsg_seq
= 0;
124 nlmsghdr
->nlmsg_pid
= 0;
127 /* Puts a nlmsghdr and genlmsghdr at the beginning of 'msg', which must be
128 * initially empty. 'expected_payload' should be an estimate of the number of
129 * payload bytes to be supplied; if the size of the payload is unknown a value
130 * of 0 is acceptable.
132 * 'family' is the family number obtained via nl_lookup_genl_family().
134 * 'flags' is a bit-mask that indicates what kind of request is being made. It
135 * is often NLM_F_REQUEST indicating that a request is being made, commonly
136 * or'd with NLM_F_ACK to request an acknowledgement.
138 * 'cmd' is an enumerated value specific to the Generic Netlink family
139 * (e.g. CTRL_CMD_NEWFAMILY for the GENL_ID_CTRL family).
141 * 'version' is a version number specific to the family and command (often 1).
143 * Sets the new nlmsghdr's nlmsg_pid field to 0 for now. nl_sock_send() will
144 * fill it in just before sending the message.
146 * nl_msg_put_nlmsghdr() should be used to compose Netlink messages that are
147 * not Generic Netlink messages. */
149 nl_msg_put_genlmsghdr(struct ofpbuf
*msg
, size_t expected_payload
,
150 int family
, uint32_t flags
, uint8_t cmd
, uint8_t version
)
152 struct genlmsghdr
*genlmsghdr
;
154 nl_msg_put_nlmsghdr(msg
, GENL_HDRLEN
+ expected_payload
, family
, flags
);
155 assert(msg
->size
== NLMSG_HDRLEN
);
156 genlmsghdr
= nl_msg_put_uninit(msg
, GENL_HDRLEN
);
157 genlmsghdr
->cmd
= cmd
;
158 genlmsghdr
->version
= version
;
159 genlmsghdr
->reserved
= 0;
162 /* Appends the 'size' bytes of data in 'p', plus Netlink padding if needed, to
163 * the tail end of 'msg'. Data in 'msg' is reallocated and copied if
166 nl_msg_put(struct ofpbuf
*msg
, const void *data
, size_t size
)
168 memcpy(nl_msg_put_uninit(msg
, size
), data
, size
);
171 /* Appends 'size' bytes of data, plus Netlink padding if needed, to the tail
172 * end of 'msg', reallocating and copying its data if necessary. Returns a
173 * pointer to the first byte of the new data, which is left uninitialized. */
175 nl_msg_put_uninit(struct ofpbuf
*msg
, size_t size
)
177 size_t pad
= NLMSG_ALIGN(size
) - size
;
178 char *p
= ofpbuf_put_uninit(msg
, size
+ pad
);
180 memset(p
+ size
, 0, pad
);
185 /* Prepends the 'size' bytes of data in 'p', plus Netlink padding if needed, to
186 * the head end of 'msg'. Data in 'msg' is reallocated and copied if
189 nl_msg_push(struct ofpbuf
*msg
, const void *data
, size_t size
)
191 memcpy(nl_msg_push_uninit(msg
, size
), data
, size
);
194 /* Prepends 'size' bytes of data, plus Netlink padding if needed, to the head
195 * end of 'msg', reallocating and copying its data if necessary. Returns a
196 * pointer to the first byte of the new data, which is left uninitialized. */
198 nl_msg_push_uninit(struct ofpbuf
*msg
, size_t size
)
200 size_t pad
= NLMSG_ALIGN(size
) - size
;
201 char *p
= ofpbuf_push_uninit(msg
, size
+ pad
);
203 memset(p
+ size
, 0, pad
);
208 /* Appends a Netlink attribute of the given 'type' and room for 'size' bytes of
209 * data as its payload, plus Netlink padding if needed, to the tail end of
210 * 'msg', reallocating and copying its data if necessary. Returns a pointer to
211 * the first byte of data in the attribute, which is left uninitialized. */
213 nl_msg_put_unspec_uninit(struct ofpbuf
*msg
, uint16_t type
, size_t size
)
215 size_t total_size
= NLA_HDRLEN
+ size
;
216 struct nlattr
* nla
= nl_msg_put_uninit(msg
, total_size
);
217 assert(NLA_ALIGN(total_size
) <= UINT16_MAX
);
218 nla
->nla_len
= total_size
;
219 nla
->nla_type
= type
;
223 /* Appends a Netlink attribute of the given 'type' and the 'size' bytes of
224 * 'data' as its payload, to the tail end of 'msg', reallocating and copying
225 * its data if necessary. Returns a pointer to the first byte of data in the
226 * attribute, which is left uninitialized. */
228 nl_msg_put_unspec(struct ofpbuf
*msg
, uint16_t type
,
229 const void *data
, size_t size
)
231 memcpy(nl_msg_put_unspec_uninit(msg
, type
, size
), data
, size
);
234 /* Appends a Netlink attribute of the given 'type' and no payload to 'msg'.
235 * (Some Netlink protocols use the presence or absence of an attribute as a
238 nl_msg_put_flag(struct ofpbuf
*msg
, uint16_t type
)
240 nl_msg_put_unspec(msg
, type
, NULL
, 0);
243 /* Appends a Netlink attribute of the given 'type' and the given 8-bit 'value'
246 nl_msg_put_u8(struct ofpbuf
*msg
, uint16_t type
, uint8_t value
)
248 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
251 /* Appends a Netlink attribute of the given 'type' and the given 16-bit host
252 * byte order 'value' to 'msg'. */
254 nl_msg_put_u16(struct ofpbuf
*msg
, uint16_t type
, uint16_t value
)
256 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
259 /* Appends a Netlink attribute of the given 'type' and the given 32-bit host
260 * byte order 'value' to 'msg'. */
262 nl_msg_put_u32(struct ofpbuf
*msg
, uint16_t type
, uint32_t value
)
264 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
267 /* Appends a Netlink attribute of the given 'type' and the given 64-bit host
268 * byte order 'value' to 'msg'. */
270 nl_msg_put_u64(struct ofpbuf
*msg
, uint16_t type
, uint64_t value
)
272 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
275 /* Appends a Netlink attribute of the given 'type' and the given 16-bit network
276 * byte order 'value' to 'msg'. */
278 nl_msg_put_be16(struct ofpbuf
*msg
, uint16_t type
, ovs_be16 value
)
280 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
283 /* Appends a Netlink attribute of the given 'type' and the given 32-bit network
284 * byte order 'value' to 'msg'. */
286 nl_msg_put_be32(struct ofpbuf
*msg
, uint16_t type
, ovs_be32 value
)
288 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
291 /* Appends a Netlink attribute of the given 'type' and the given 64-bit network
292 * byte order 'value' to 'msg'. */
294 nl_msg_put_be64(struct ofpbuf
*msg
, uint16_t type
, ovs_be64 value
)
296 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
299 /* Appends a Netlink attribute of the given 'type' and the given
300 * null-terminated string 'value' to 'msg'. */
302 nl_msg_put_string(struct ofpbuf
*msg
, uint16_t type
, const char *value
)
304 nl_msg_put_unspec(msg
, type
, value
, strlen(value
) + 1);
307 /* Prepends a Netlink attribute of the given 'type' and room for 'size' bytes
308 * of data as its payload, plus Netlink padding if needed, to the head end of
309 * 'msg', reallocating and copying its data if necessary. Returns a pointer to
310 * the first byte of data in the attribute, which is left uninitialized. */
312 nl_msg_push_unspec_uninit(struct ofpbuf
*msg
, uint16_t type
, size_t size
)
314 size_t total_size
= NLA_HDRLEN
+ size
;
315 struct nlattr
* nla
= nl_msg_push_uninit(msg
, total_size
);
316 assert(NLA_ALIGN(total_size
) <= UINT16_MAX
);
317 nla
->nla_len
= total_size
;
318 nla
->nla_type
= type
;
322 /* Prepends a Netlink attribute of the given 'type' and the 'size' bytes of
323 * 'data' as its payload, to the head end of 'msg', reallocating and copying
324 * its data if necessary. Returns a pointer to the first byte of data in the
325 * attribute, which is left uninitialized. */
327 nl_msg_push_unspec(struct ofpbuf
*msg
, uint16_t type
,
328 const void *data
, size_t size
)
330 memcpy(nl_msg_push_unspec_uninit(msg
, type
, size
), data
, size
);
333 /* Prepends a Netlink attribute of the given 'type' and no payload to 'msg'.
334 * (Some Netlink protocols use the presence or absence of an attribute as a
337 nl_msg_push_flag(struct ofpbuf
*msg
, uint16_t type
)
339 nl_msg_push_unspec(msg
, type
, NULL
, 0);
342 /* Prepends a Netlink attribute of the given 'type' and the given 8-bit 'value'
345 nl_msg_push_u8(struct ofpbuf
*msg
, uint16_t type
, uint8_t value
)
347 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
350 /* Prepends a Netlink attribute of the given 'type' and the given 16-bit host
351 * byte order 'value' to 'msg'. */
353 nl_msg_push_u16(struct ofpbuf
*msg
, uint16_t type
, uint16_t value
)
355 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
358 /* Prepends a Netlink attribute of the given 'type' and the given 32-bit host
359 * byte order 'value' to 'msg'. */
361 nl_msg_push_u32(struct ofpbuf
*msg
, uint16_t type
, uint32_t value
)
363 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
366 /* Prepends a Netlink attribute of the given 'type' and the given 64-bit host
367 * byte order 'value' to 'msg'. */
369 nl_msg_push_u64(struct ofpbuf
*msg
, uint16_t type
, uint64_t value
)
371 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
374 /* Prepends a Netlink attribute of the given 'type' and the given 16-bit
375 * network byte order 'value' to 'msg'. */
377 nl_msg_push_be16(struct ofpbuf
*msg
, uint16_t type
, ovs_be16 value
)
379 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
382 /* Prepends a Netlink attribute of the given 'type' and the given 32-bit
383 * network byte order 'value' to 'msg'. */
385 nl_msg_push_be32(struct ofpbuf
*msg
, uint16_t type
, ovs_be32 value
)
387 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
390 /* Prepends a Netlink attribute of the given 'type' and the given 64-bit
391 * network byte order 'value' to 'msg'. */
393 nl_msg_push_be64(struct ofpbuf
*msg
, uint16_t type
, ovs_be64 value
)
395 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
398 /* Prepends a Netlink attribute of the given 'type' and the given
399 * null-terminated string 'value' to 'msg'. */
401 nl_msg_push_string(struct ofpbuf
*msg
, uint16_t type
, const char *value
)
403 nl_msg_push_unspec(msg
, type
, value
, strlen(value
) + 1);
406 /* Adds the header for nested Netlink attributes to 'msg', with the specified
407 * 'type', and returns the header's offset within 'msg'. The caller should add
408 * the content for the nested Netlink attribute to 'msg' (e.g. using the other
409 * nl_msg_*() functions), and then pass the returned offset to
410 * nl_msg_end_nested() to finish up the nested attributes. */
412 nl_msg_start_nested(struct ofpbuf
*msg
, uint16_t type
)
414 size_t offset
= msg
->size
;
415 nl_msg_put_unspec(msg
, type
, NULL
, 0);
419 /* Finalizes a nested Netlink attribute in 'msg'. 'offset' should be the value
420 * returned by nl_msg_start_nested(). */
422 nl_msg_end_nested(struct ofpbuf
*msg
, size_t offset
)
424 struct nlattr
*attr
= ofpbuf_at_assert(msg
, offset
, sizeof *attr
);
425 attr
->nla_len
= msg
->size
- offset
;
428 /* Appends a nested Netlink attribute of the given 'type', with the 'size'
429 * bytes of content starting at 'data', to 'msg'. */
431 nl_msg_put_nested(struct ofpbuf
*msg
,
432 uint16_t type
, const void *data
, size_t size
)
434 size_t offset
= nl_msg_start_nested(msg
, type
);
435 nl_msg_put(msg
, data
, size
);
436 nl_msg_end_nested(msg
, offset
);
439 /* If 'buffer' begins with a valid "struct nlmsghdr", pulls the header and its
440 * payload off 'buffer', stores header and payload in 'msg->data' and
441 * 'msg->size', and returns a pointer to the header.
443 * If 'buffer' does not begin with a "struct nlmsghdr" or begins with one that
444 * is invalid, returns NULL without modifying 'buffer'. */
446 nl_msg_next(struct ofpbuf
*buffer
, struct ofpbuf
*msg
)
448 if (buffer
->size
>= sizeof(struct nlmsghdr
)) {
449 struct nlmsghdr
*nlmsghdr
= nl_msg_nlmsghdr(buffer
);
450 size_t len
= nlmsghdr
->nlmsg_len
;
451 if (len
>= sizeof *nlmsghdr
&& len
<= buffer
->size
) {
452 ofpbuf_use_const(msg
, nlmsghdr
, len
);
453 ofpbuf_pull(buffer
, len
);
465 /* Returns the bits of 'nla->nla_type' that are significant for determining its
468 nl_attr_type(const struct nlattr
*nla
)
470 return nla
->nla_type
& NLA_TYPE_MASK
;
473 /* Returns the first byte in the payload of attribute 'nla'. */
475 nl_attr_get(const struct nlattr
*nla
)
477 assert(nla
->nla_len
>= NLA_HDRLEN
);
481 /* Returns the number of bytes in the payload of attribute 'nla'. */
483 nl_attr_get_size(const struct nlattr
*nla
)
485 assert(nla
->nla_len
>= NLA_HDRLEN
);
486 return nla
->nla_len
- NLA_HDRLEN
;
489 /* Asserts that 'nla''s payload is at least 'size' bytes long, and returns the
490 * first byte of the payload. */
492 nl_attr_get_unspec(const struct nlattr
*nla
, size_t size
)
494 assert(nla
->nla_len
>= NLA_HDRLEN
+ size
);
498 /* Returns true if 'nla' is nonnull. (Some Netlink protocols use the presence
499 * or absence of an attribute as a Boolean flag.) */
501 nl_attr_get_flag(const struct nlattr
*nla
)
506 #define NL_ATTR_GET_AS(NLA, TYPE) \
507 (*(TYPE*) nl_attr_get_unspec(nla, sizeof(TYPE)))
509 /* Returns the 8-bit value in 'nla''s payload.
511 * Asserts that 'nla''s payload is at least 1 byte long. */
513 nl_attr_get_u8(const struct nlattr
*nla
)
515 return NL_ATTR_GET_AS(nla
, uint8_t);
518 /* Returns the 16-bit host byte order value in 'nla''s payload.
520 * Asserts that 'nla''s payload is at least 2 bytes long. */
522 nl_attr_get_u16(const struct nlattr
*nla
)
524 return NL_ATTR_GET_AS(nla
, uint16_t);
527 /* Returns the 32-bit host byte order value in 'nla''s payload.
529 * Asserts that 'nla''s payload is at least 4 bytes long. */
531 nl_attr_get_u32(const struct nlattr
*nla
)
533 return NL_ATTR_GET_AS(nla
, uint32_t);
536 /* Returns the 64-bit host byte order value in 'nla''s payload.
538 * Asserts that 'nla''s payload is at least 8 bytes long. */
540 nl_attr_get_u64(const struct nlattr
*nla
)
542 const ovs_32aligned_u64
*x
= nl_attr_get_unspec(nla
, sizeof *x
);
543 return get_32aligned_u64(x
);
546 /* Returns the 16-bit network byte order value in 'nla''s payload.
548 * Asserts that 'nla''s payload is at least 2 bytes long. */
550 nl_attr_get_be16(const struct nlattr
*nla
)
552 return NL_ATTR_GET_AS(nla
, ovs_be16
);
555 /* Returns the 32-bit network byte order value in 'nla''s payload.
557 * Asserts that 'nla''s payload is at least 4 bytes long. */
559 nl_attr_get_be32(const struct nlattr
*nla
)
561 return NL_ATTR_GET_AS(nla
, ovs_be32
);
564 /* Returns the 64-bit network byte order value in 'nla''s payload.
566 * Asserts that 'nla''s payload is at least 8 bytes long. */
568 nl_attr_get_be64(const struct nlattr
*nla
)
570 const ovs_32aligned_be64
*x
= nl_attr_get_unspec(nla
, sizeof *x
);
571 return get_32aligned_be64(x
);
574 /* Returns the null-terminated string value in 'nla''s payload.
576 * Asserts that 'nla''s payload contains a null-terminated string. */
578 nl_attr_get_string(const struct nlattr
*nla
)
580 assert(nla
->nla_len
> NLA_HDRLEN
);
581 assert(memchr(nl_attr_get(nla
), '\0', nla
->nla_len
- NLA_HDRLEN
) != NULL
);
582 return nl_attr_get(nla
);
585 /* Initializes 'nested' to the payload of 'nla'. */
587 nl_attr_get_nested(const struct nlattr
*nla
, struct ofpbuf
*nested
)
589 ofpbuf_use_const(nested
, nl_attr_get(nla
), nl_attr_get_size(nla
));
592 /* Default minimum and maximum payload sizes for each type of attribute. */
593 static const size_t attr_len_range
[][2] = {
594 [0 ... N_NL_ATTR_TYPES
- 1] = { 0, SIZE_MAX
},
595 [NL_A_U8
] = { 1, 1 },
596 [NL_A_U16
] = { 2, 2 },
597 [NL_A_U32
] = { 4, 4 },
598 [NL_A_U64
] = { 8, 8 },
599 [NL_A_STRING
] = { 1, SIZE_MAX
},
600 [NL_A_FLAG
] = { 0, SIZE_MAX
},
601 [NL_A_NESTED
] = { 0, SIZE_MAX
},
605 nl_attr_validate(const struct nlattr
*nla
, const struct nl_policy
*policy
)
607 uint16_t type
= nl_attr_type(nla
);
612 if (policy
->type
== NL_A_NO_ATTR
) {
616 /* Figure out min and max length. */
617 min_len
= policy
->min_len
;
619 min_len
= attr_len_range
[policy
->type
][0];
621 max_len
= policy
->max_len
;
623 max_len
= attr_len_range
[policy
->type
][1];
627 len
= nl_attr_get_size(nla
);
628 if (len
< min_len
|| len
> max_len
) {
629 VLOG_DBG_RL(&rl
, "attr %"PRIu16
" length %zu not in "
630 "allowed range %zu...%zu", type
, len
, min_len
, max_len
);
634 /* Strings must be null terminated and must not have embedded nulls. */
635 if (policy
->type
== NL_A_STRING
) {
636 if (((char *) nla
)[nla
->nla_len
- 1]) {
637 VLOG_DBG_RL(&rl
, "attr %"PRIu16
" lacks null at end", type
);
640 if (memchr(nla
+ 1, '\0', len
- 1) != NULL
) {
641 VLOG_DBG_RL(&rl
, "attr %"PRIu16
" has bad length", type
);
649 /* Parses the 'msg' starting at the given 'nla_offset' as a sequence of Netlink
650 * attributes. 'policy[i]', for 0 <= i < n_attrs, specifies how the attribute
651 * with nla_type == i is parsed; a pointer to attribute i is stored in
652 * attrs[i]. Returns true if successful, false on failure.
654 * If the Netlink attributes in 'msg' follow a Netlink header and a Generic
655 * Netlink header, then 'nla_offset' should be NLMSG_HDRLEN + GENL_HDRLEN. */
657 nl_policy_parse(const struct ofpbuf
*msg
, size_t nla_offset
,
658 const struct nl_policy policy
[],
659 struct nlattr
*attrs
[], size_t n_attrs
)
665 memset(attrs
, 0, n_attrs
* sizeof *attrs
);
667 if (msg
->size
< nla_offset
) {
668 VLOG_DBG_RL(&rl
, "missing headers in nl_policy_parse");
672 NL_ATTR_FOR_EACH (nla
, left
,
673 (struct nlattr
*) ((char *) msg
->data
+ nla_offset
),
674 msg
->size
- nla_offset
)
676 uint16_t type
= nl_attr_type(nla
);
677 if (type
< n_attrs
&& policy
[type
].type
!= NL_A_NO_ATTR
) {
678 const struct nl_policy
*e
= &policy
[type
];
679 if (!nl_attr_validate(nla
, e
)) {
683 VLOG_DBG_RL(&rl
, "duplicate attr %"PRIu16
, type
);
689 VLOG_DBG_RL(&rl
, "attributes followed by garbage");
693 for (i
= 0; i
< n_attrs
; i
++) {
694 const struct nl_policy
*e
= &policy
[i
];
695 if (!e
->optional
&& e
->type
!= NL_A_NO_ATTR
&& !attrs
[i
]) {
696 VLOG_DBG_RL(&rl
, "required attr %zu missing", i
);
703 /* Parses the Netlink attributes within 'nla'. 'policy[i]', for 0 <= i <
704 * n_attrs, specifies how the attribute with nla_type == i is parsed; a pointer
705 * to attribute i is stored in attrs[i]. Returns true if successful, false on
708 nl_parse_nested(const struct nlattr
*nla
, const struct nl_policy policy
[],
709 struct nlattr
*attrs
[], size_t n_attrs
)
713 nl_attr_get_nested(nla
, &buf
);
714 return nl_policy_parse(&buf
, 0, policy
, attrs
, n_attrs
);
717 const struct nlattr
*
718 nl_attr_find__(const struct nlattr
*attrs
, size_t size
, uint16_t type
)
720 const struct nlattr
*nla
;
723 NL_ATTR_FOR_EACH (nla
, left
, attrs
, size
) {
724 if (nl_attr_type(nla
) == type
) {
731 /* Returns the first Netlink attribute within 'buf' with the specified 'type',
732 * skipping a header of 'hdr_len' bytes at the beginning of 'buf'.
734 * This function does not validate the attribute's length. */
735 const struct nlattr
*
736 nl_attr_find(const struct ofpbuf
*buf
, size_t hdr_len
, uint16_t type
)
738 const uint8_t *start
= (const uint8_t *) buf
->data
+ hdr_len
;
739 return nl_attr_find__((const struct nlattr
*) start
, buf
->size
- hdr_len
,
743 /* Returns the first Netlink attribute within 'nla' with the specified
746 * This function does not validate the attribute's length. */
747 const struct nlattr
*
748 nl_attr_find_nested(const struct nlattr
*nla
, uint16_t type
)
750 return nl_attr_find__(nl_attr_get(nla
), nl_attr_get_size(nla
), type
);