]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/netlink.c
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 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>
25 #include "netlink-protocol.h"
26 #include "openvswitch/ofpbuf.h"
28 #include "unaligned.h"
29 #include "openvswitch/vlog.h"
32 VLOG_DEFINE_THIS_MODULE(netlink
);
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);
39 /* Returns the nlmsghdr at the head of 'msg'.
41 * 'msg' must be at least as large as a nlmsghdr. */
43 nl_msg_nlmsghdr(const struct ofpbuf
*msg
)
45 return ofpbuf_at_assert(msg
, 0, NLMSG_HDRLEN
);
48 /* Returns the genlmsghdr just past 'msg''s nlmsghdr.
50 * Returns a null pointer if 'msg' is not large enough to contain an nlmsghdr
51 * and a genlmsghdr. */
53 nl_msg_genlmsghdr(const struct ofpbuf
*msg
)
55 return ofpbuf_at(msg
, NLMSG_HDRLEN
, GENL_HDRLEN
);
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.
62 * 'msg' must be at least as large as a nlmsghdr. */
64 nl_msg_nlmsgerr(const struct ofpbuf
*msg
, int *errorp
)
66 if (nl_msg_nlmsghdr(msg
)->nlmsg_type
== NLMSG_ERROR
) {
67 struct nlmsgerr
*err
= ofpbuf_at(msg
, NLMSG_HDRLEN
, sizeof *err
);
70 VLOG_ERR_RL(&rl
, "received invalid nlmsgerr (%"PRIu32
" bytes < %"PRIuSIZE
")",
71 msg
->size
, NLMSG_HDRLEN
+ sizeof *err
);
72 } else if (err
->error
<= 0 && err
->error
> INT_MIN
) {
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. */
87 nl_msg_reserve(struct ofpbuf
*msg
, size_t size
)
89 ofpbuf_prealloc_tailroom(msg
, NLMSG_ALIGN(size
));
92 /* Puts a nlmsghdr at the beginning of 'msg', which must be initially empty.
93 * Uses the given 'type' and 'flags'. 'expected_payload' should be
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.
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().
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.
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.
109 * nl_msg_put_genlmsghdr() is more convenient for composing a Generic Netlink
112 nl_msg_put_nlmsghdr(struct ofpbuf
*msg
,
113 size_t expected_payload
, uint32_t type
, uint32_t flags
)
115 struct nlmsghdr
*nlmsghdr
;
117 ovs_assert(msg
->size
== 0);
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
;
124 nlmsghdr
->nlmsg_seq
= 0;
125 nlmsghdr
->nlmsg_pid
= 0;
128 /* Puts a nlmsghdr and genlmsghdr at the beginning of 'msg', which must be
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.
133 * 'family' is the family number obtained via nl_lookup_genl_family().
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.
139 * 'cmd' is an enumerated value specific to the Generic Netlink family
140 * (e.g. CTRL_CMD_NEWFAMILY for the GENL_ID_CTRL family).
142 * 'version' is a version number specific to the family and command (often 1).
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.
147 * nl_msg_put_nlmsghdr() should be used to compose Netlink messages that are
148 * not Generic Netlink messages. */
150 nl_msg_put_genlmsghdr(struct ofpbuf
*msg
, size_t expected_payload
,
151 int family
, uint32_t flags
, uint8_t cmd
, uint8_t version
)
153 struct genlmsghdr
*genlmsghdr
;
155 nl_msg_put_nlmsghdr(msg
, GENL_HDRLEN
+ expected_payload
, family
, flags
);
156 ovs_assert(msg
->size
== NLMSG_HDRLEN
);
157 genlmsghdr
= nl_msg_put_uninit(msg
, GENL_HDRLEN
);
158 genlmsghdr
->cmd
= cmd
;
159 genlmsghdr
->version
= version
;
160 genlmsghdr
->reserved
= 0;
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
167 nl_msg_put(struct ofpbuf
*msg
, const void *data
, size_t size
)
169 memcpy(nl_msg_put_uninit(msg
, size
), data
, size
);
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. */
176 nl_msg_put_uninit(struct ofpbuf
*msg
, size_t size
)
178 size_t pad
= PAD_SIZE(size
, NLMSG_ALIGNTO
);
179 char *p
= ofpbuf_put_uninit(msg
, size
+ pad
);
181 memset(p
+ size
, 0, pad
);
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
190 nl_msg_push(struct ofpbuf
*msg
, const void *data
, size_t size
)
192 memcpy(nl_msg_push_uninit(msg
, size
), data
, size
);
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. */
199 nl_msg_push_uninit(struct ofpbuf
*msg
, size_t size
)
201 size_t pad
= PAD_SIZE(size
, NLMSG_ALIGNTO
);
202 char *p
= ofpbuf_push_uninit(msg
, size
+ pad
);
204 memset(p
+ size
, 0, pad
);
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. */
214 nl_msg_put_unspec_uninit(struct ofpbuf
*msg
, uint16_t type
, size_t size
)
216 size_t total_size
= NLA_HDRLEN
+ size
;
217 struct nlattr
* nla
= nl_msg_put_uninit(msg
, total_size
);
218 ovs_assert(!nl_attr_oversized(size
));
219 nla
->nla_len
= total_size
;
220 nla
->nla_type
= type
;
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. */
229 nl_msg_put_unspec_zero(struct ofpbuf
*msg
, uint16_t type
, size_t size
)
231 void *data
= nl_msg_put_unspec_uninit(msg
, type
, size
);
232 memset(data
, 0, size
);
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
238 * its data if necessary. */
240 nl_msg_put_unspec(struct ofpbuf
*msg
, uint16_t type
,
241 const void *data
, size_t size
)
245 ptr
= nl_msg_put_unspec_uninit(msg
, type
, size
);
246 nullable_memcpy(ptr
, data
, size
);
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
253 nl_msg_put_flag(struct ofpbuf
*msg
, uint16_t type
)
255 nl_msg_put_unspec(msg
, type
, NULL
, 0);
258 /* Appends a Netlink attribute of the given 'type' and the given 8-bit 'value'
261 nl_msg_put_u8(struct ofpbuf
*msg
, uint16_t type
, uint8_t value
)
263 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
266 /* Appends a Netlink attribute of the given 'type' and the given 16-bit host
267 * byte order 'value' to 'msg'. */
269 nl_msg_put_u16(struct ofpbuf
*msg
, uint16_t type
, uint16_t value
)
271 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
274 /* Appends a Netlink attribute of the given 'type' and the given 32-bit host
275 * byte order 'value' to 'msg'. */
277 nl_msg_put_u32(struct ofpbuf
*msg
, uint16_t type
, uint32_t value
)
279 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
282 /* Appends a Netlink attribute of the given 'type' and the given 64-bit host
283 * byte order 'value' to 'msg'. */
285 nl_msg_put_u64(struct ofpbuf
*msg
, uint16_t type
, uint64_t value
)
287 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
290 /* Appends a Netlink attribute of the given 'type' and the given 128-bit host
291 * byte order 'value' to 'msg'. */
293 nl_msg_put_u128(struct ofpbuf
*msg
, uint16_t type
, ovs_u128 value
)
295 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
298 /* Appends a Netlink attribute of the given 'type' and the given 16-bit network
299 * byte order 'value' to 'msg'. */
301 nl_msg_put_be16(struct ofpbuf
*msg
, uint16_t type
, ovs_be16 value
)
303 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
306 /* Appends a Netlink attribute of the given 'type' and the given 32-bit network
307 * byte order 'value' to 'msg'. */
309 nl_msg_put_be32(struct ofpbuf
*msg
, uint16_t type
, ovs_be32 value
)
311 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
314 /* Appends a Netlink attribute of the given 'type' and the given 64-bit network
315 * byte order 'value' to 'msg'. */
317 nl_msg_put_be64(struct ofpbuf
*msg
, uint16_t type
, ovs_be64 value
)
319 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
322 /* Appends a Netlink attribute of the given 'type' and the given 128-bit
323 * network byte order 'value' to 'msg'. */
325 nl_msg_put_be128(struct ofpbuf
*msg
, uint16_t type
, ovs_be128 value
)
327 nl_msg_put_unspec(msg
, type
, &value
, sizeof value
);
330 /* Appends a Netlink attribute of the given 'type' and the given IPv6
331 * address order 'value' to 'msg'. */
333 nl_msg_put_in6_addr(struct ofpbuf
*msg
, uint16_t type
,
334 const struct in6_addr
*value
)
336 nl_msg_put_unspec(msg
, type
, value
, sizeof *value
);
339 /* Appends a Netlink attribute of the given 'type' and the given odp_port_t
340 * 'value' to 'msg'. */
342 nl_msg_put_odp_port(struct ofpbuf
*msg
, uint16_t type
, odp_port_t value
)
344 nl_msg_put_u32(msg
, type
, odp_to_u32(value
));
347 /* Appends a Netlink attribute of the given 'type' with the 'len' characters
348 * of 'value', followed by the null byte to 'msg'. */
350 nl_msg_put_string__(struct ofpbuf
*msg
, uint16_t type
, const char *value
,
353 char *data
= nl_msg_put_unspec_uninit(msg
, type
, len
+ 1);
355 memcpy(data
, value
, len
);
359 /* Appends a Netlink attribute of the given 'type' and the given
360 * null-terminated string 'value' to 'msg'. */
362 nl_msg_put_string(struct ofpbuf
*msg
, uint16_t type
, const char *value
)
364 nl_msg_put_unspec(msg
, type
, value
, strlen(value
) + 1);
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. */
372 nl_msg_push_unspec_uninit(struct ofpbuf
*msg
, uint16_t type
, size_t size
)
374 size_t total_size
= NLA_HDRLEN
+ size
;
375 struct nlattr
* nla
= nl_msg_push_uninit(msg
, total_size
);
376 ovs_assert(!nl_attr_oversized(size
));
377 nla
->nla_len
= total_size
;
378 nla
->nla_type
= type
;
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. */
387 nl_msg_push_unspec(struct ofpbuf
*msg
, uint16_t type
,
388 const void *data
, size_t size
)
390 memcpy(nl_msg_push_unspec_uninit(msg
, type
, size
), data
, size
);
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
397 nl_msg_push_flag(struct ofpbuf
*msg
, uint16_t type
)
399 nl_msg_push_unspec_uninit(msg
, type
, 0);
402 /* Prepends a Netlink attribute of the given 'type' and the given 8-bit 'value'
405 nl_msg_push_u8(struct ofpbuf
*msg
, uint16_t type
, uint8_t value
)
407 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
410 /* Prepends a Netlink attribute of the given 'type' and the given 16-bit host
411 * byte order 'value' to 'msg'. */
413 nl_msg_push_u16(struct ofpbuf
*msg
, uint16_t type
, uint16_t value
)
415 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
418 /* Prepends a Netlink attribute of the given 'type' and the given 32-bit host
419 * byte order 'value' to 'msg'. */
421 nl_msg_push_u32(struct ofpbuf
*msg
, uint16_t type
, uint32_t value
)
423 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
426 /* Prepends a Netlink attribute of the given 'type' and the given 64-bit host
427 * byte order 'value' to 'msg'. */
429 nl_msg_push_u64(struct ofpbuf
*msg
, uint16_t type
, uint64_t value
)
431 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
434 /* Prepends a Netlink attribute of the given 'type' and the given 128-bit host
435 * byte order 'value' to 'msg'. */
437 nl_msg_push_u128(struct ofpbuf
*msg
, uint16_t type
, ovs_u128 value
)
439 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
442 /* Prepends a Netlink attribute of the given 'type' and the given 16-bit
443 * network byte order 'value' to 'msg'. */
445 nl_msg_push_be16(struct ofpbuf
*msg
, uint16_t type
, ovs_be16 value
)
447 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
450 /* Prepends a Netlink attribute of the given 'type' and the given 32-bit
451 * network byte order 'value' to 'msg'. */
453 nl_msg_push_be32(struct ofpbuf
*msg
, uint16_t type
, ovs_be32 value
)
455 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
458 /* Prepends a Netlink attribute of the given 'type' and the given 64-bit
459 * network byte order 'value' to 'msg'. */
461 nl_msg_push_be64(struct ofpbuf
*msg
, uint16_t type
, ovs_be64 value
)
463 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
466 /* Prepends a Netlink attribute of the given 'type' and the given 128-bit
467 * network byte order 'value' to 'msg'. */
469 nl_msg_push_be128(struct ofpbuf
*msg
, uint16_t type
, ovs_be128 value
)
471 nl_msg_push_unspec(msg
, type
, &value
, sizeof value
);
474 /* Prepends a Netlink attribute of the given 'type' and the given
475 * null-terminated string 'value' to 'msg'. */
477 nl_msg_push_string(struct ofpbuf
*msg
, uint16_t type
, const char *value
)
479 nl_msg_push_unspec(msg
, type
, value
, strlen(value
) + 1);
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. */
488 nl_msg_start_nested(struct ofpbuf
*msg
, uint16_t type
)
490 size_t offset
= msg
->size
;
491 nl_msg_put_unspec_uninit(msg
, type
, 0);
495 /* Finalizes a nested Netlink attribute in 'msg'. 'offset' should be the value
496 * returned by nl_msg_start_nested(). */
498 nl_msg_end_nested(struct ofpbuf
*msg
, size_t offset
)
500 struct nlattr
*attr
= ofpbuf_at_assert(msg
, offset
, sizeof *attr
);
501 attr
->nla_len
= msg
->size
- offset
;
504 /* Cancel a nested Netlink attribute in 'msg'. 'offset' should be the value
505 * returned by nl_msg_start_nested(). */
507 nl_msg_cancel_nested(struct ofpbuf
*msg
, size_t offset
)
512 /* Same as nls_msg_end_nested() when the nested Netlink contains non empty
513 * message. Otherwise, drop the nested message header from 'msg'.
515 * Return true if the nested message has been dropped. */
517 nl_msg_end_non_empty_nested(struct ofpbuf
*msg
, size_t offset
)
519 nl_msg_end_nested(msg
, offset
);
521 struct nlattr
*attr
= ofpbuf_at_assert(msg
, offset
, sizeof *attr
);
522 if (!nl_attr_get_size(attr
)) {
523 nl_msg_cancel_nested(msg
, offset
);
530 /* Appends a nested Netlink attribute of the given 'type', with the 'size'
531 * bytes of content starting at 'data', to 'msg'. */
533 nl_msg_put_nested(struct ofpbuf
*msg
,
534 uint16_t type
, const void *data
, size_t size
)
536 size_t offset
= nl_msg_start_nested(msg
, type
);
537 nl_msg_put(msg
, data
, size
);
538 nl_msg_end_nested(msg
, offset
);
541 /* If 'buffer' begins with a valid "struct nlmsghdr", pulls the header and its
542 * payload off 'buffer', stores header and payload in 'msg->data' and
543 * 'msg->size', and returns a pointer to the header.
545 * If 'buffer' does not begin with a "struct nlmsghdr" or begins with one that
546 * is invalid, returns NULL and clears 'buffer' and 'msg'. */
548 nl_msg_next(struct ofpbuf
*buffer
, struct ofpbuf
*msg
)
550 if (buffer
->size
>= sizeof(struct nlmsghdr
)) {
551 struct nlmsghdr
*nlmsghdr
= nl_msg_nlmsghdr(buffer
);
552 size_t len
= nlmsghdr
->nlmsg_len
;
553 if (len
>= sizeof *nlmsghdr
&& len
<= buffer
->size
) {
554 ofpbuf_use_const(msg
, nlmsghdr
, len
);
555 ofpbuf_pull(buffer
, len
);
560 ofpbuf_clear(buffer
);
566 /* Returns true if a Netlink attribute with a payload that is 'payload_size'
567 * bytes long would be oversized, that is, if it's not possible to create an
568 * nlattr of that size because its size wouldn't fit in the 16-bit nla_len
571 nl_attr_oversized(size_t payload_size
)
573 return payload_size
> UINT16_MAX
- NLA_HDRLEN
;
578 /* Returns the bits of 'nla->nla_type' that are significant for determining its
581 nl_attr_type(const struct nlattr
*nla
)
583 return nla
->nla_type
& NLA_TYPE_MASK
;
586 /* Returns the first byte in the payload of attribute 'nla'. */
588 nl_attr_get(const struct nlattr
*nla
)
590 ovs_assert(nla
->nla_len
>= NLA_HDRLEN
);
594 /* Returns the number of bytes in the payload of attribute 'nla'. */
596 nl_attr_get_size(const struct nlattr
*nla
)
598 ovs_assert(nla
->nla_len
>= NLA_HDRLEN
);
599 return nla
->nla_len
- NLA_HDRLEN
;
602 /* Asserts that 'nla''s payload is at least 'size' bytes long, and returns the
603 * first byte of the payload. */
605 nl_attr_get_unspec(const struct nlattr
*nla
, size_t size
)
607 ovs_assert(nla
->nla_len
>= NLA_HDRLEN
+ size
);
611 /* Returns true if 'nla' is nonnull. (Some Netlink protocols use the presence
612 * or absence of an attribute as a Boolean flag.) */
614 nl_attr_get_flag(const struct nlattr
*nla
)
619 #define NL_ATTR_GET_AS(NLA, TYPE) \
620 (*(TYPE*) nl_attr_get_unspec(nla, sizeof(TYPE)))
622 /* Returns the 8-bit value in 'nla''s payload.
624 * Asserts that 'nla''s payload is at least 1 byte long. */
626 nl_attr_get_u8(const struct nlattr
*nla
)
628 return NL_ATTR_GET_AS(nla
, uint8_t);
631 /* Returns the 16-bit host byte order value in 'nla''s payload.
633 * Asserts that 'nla''s payload is at least 2 bytes long. */
635 nl_attr_get_u16(const struct nlattr
*nla
)
637 return NL_ATTR_GET_AS(nla
, uint16_t);
640 /* Returns the 32-bit host byte order value in 'nla''s payload.
642 * Asserts that 'nla''s payload is at least 4 bytes long. */
644 nl_attr_get_u32(const struct nlattr
*nla
)
646 return NL_ATTR_GET_AS(nla
, uint32_t);
649 /* Returns the 64-bit host byte order value in 'nla''s payload.
651 * Asserts that 'nla''s payload is at least 8 bytes long. */
653 nl_attr_get_u64(const struct nlattr
*nla
)
655 const ovs_32aligned_u64
*x
= nl_attr_get_unspec(nla
, sizeof *x
);
656 return get_32aligned_u64(x
);
659 /* Returns the 128-bit host byte order value in 'nla''s payload.
661 * Asserts that 'nla''s payload is at least 16 bytes long. */
663 nl_attr_get_u128(const struct nlattr
*nla
)
665 const ovs_32aligned_u128
*x
= nl_attr_get_unspec(nla
, sizeof *x
);
666 return get_32aligned_u128(x
);
669 /* Returns the 16-bit network byte order value in 'nla''s payload.
671 * Asserts that 'nla''s payload is at least 2 bytes long. */
673 nl_attr_get_be16(const struct nlattr
*nla
)
675 return NL_ATTR_GET_AS(nla
, ovs_be16
);
678 /* Returns the 32-bit network byte order value in 'nla''s payload.
680 * Asserts that 'nla''s payload is at least 4 bytes long. */
682 nl_attr_get_be32(const struct nlattr
*nla
)
684 return NL_ATTR_GET_AS(nla
, ovs_be32
);
687 /* Returns the 64-bit network byte order value in 'nla''s payload.
689 * Asserts that 'nla''s payload is at least 8 bytes long. */
691 nl_attr_get_be64(const struct nlattr
*nla
)
693 const ovs_32aligned_be64
*x
= nl_attr_get_unspec(nla
, sizeof *x
);
694 return get_32aligned_be64(x
);
697 /* Returns the 128-bit network byte order value in 'nla''s payload.
699 * Asserts that 'nla''s payload is at least 16 bytes long. */
701 nl_attr_get_be128(const struct nlattr
*nla
)
703 const ovs_32aligned_be128
*x
= nl_attr_get_unspec(nla
, sizeof *x
);
704 return get_32aligned_be128(x
);
707 /* Returns the IPv6 address value in 'nla''s payload.
709 * Asserts that 'nla''s payload is at least 16 bytes long. */
711 nl_attr_get_in6_addr(const struct nlattr
*nla
)
713 return NL_ATTR_GET_AS(nla
, struct in6_addr
);
716 /* Returns the 32-bit odp_port_t value in 'nla''s payload.
718 * Asserts that 'nla''s payload is at least 4 bytes long. */
720 nl_attr_get_odp_port(const struct nlattr
*nla
)
722 return u32_to_odp(nl_attr_get_u32(nla
));
725 /* Returns the null-terminated string value in 'nla''s payload.
727 * Asserts that 'nla''s payload contains a null-terminated string. */
729 nl_attr_get_string(const struct nlattr
*nla
)
731 ovs_assert(nla
->nla_len
> NLA_HDRLEN
);
732 ovs_assert(memchr(nl_attr_get(nla
), '\0', nla
->nla_len
- NLA_HDRLEN
));
733 return nl_attr_get(nla
);
736 /* Initializes 'nested' to the payload of 'nla'. */
738 nl_attr_get_nested(const struct nlattr
*nla
, struct ofpbuf
*nested
)
740 ofpbuf_use_const(nested
, nl_attr_get(nla
), nl_attr_get_size(nla
));
743 /* Default minimum payload size for each type of attribute. */
745 min_attr_len(enum nl_attr_type type
)
748 case NL_A_NO_ATTR
: return 0;
749 case NL_A_UNSPEC
: return 0;
750 case NL_A_U8
: return 1;
751 case NL_A_U16
: return 2;
752 case NL_A_U32
: return 4;
753 case NL_A_U64
: return 8;
754 case NL_A_U128
: return 16;
755 case NL_A_STRING
: return 1;
756 case NL_A_FLAG
: return 0;
757 case NL_A_IPV6
: return 16;
758 case NL_A_NESTED
: return 0;
759 case N_NL_ATTR_TYPES
: default: OVS_NOT_REACHED();
763 /* Default maximum payload size for each type of attribute. */
765 max_attr_len(enum nl_attr_type type
)
768 case NL_A_NO_ATTR
: return SIZE_MAX
;
769 case NL_A_UNSPEC
: return SIZE_MAX
;
770 case NL_A_U8
: return 1;
771 case NL_A_U16
: return 2;
772 case NL_A_U32
: return 4;
773 case NL_A_U64
: return 8;
774 case NL_A_U128
: return 16;
775 case NL_A_STRING
: return SIZE_MAX
;
776 case NL_A_FLAG
: return SIZE_MAX
;
777 case NL_A_IPV6
: return 16;
778 case NL_A_NESTED
: return SIZE_MAX
;
779 case N_NL_ATTR_TYPES
: default: OVS_NOT_REACHED();
784 nl_attr_validate(const struct nlattr
*nla
, const struct nl_policy
*policy
)
786 uint16_t type
= nl_attr_type(nla
);
791 if (policy
->type
== NL_A_NO_ATTR
) {
795 /* Figure out min and max length. */
796 min_len
= policy
->min_len
;
798 min_len
= min_attr_len(policy
->type
);
800 max_len
= policy
->max_len
;
802 max_len
= max_attr_len(policy
->type
);
806 len
= nl_attr_get_size(nla
);
807 if (len
< min_len
|| len
> max_len
) {
808 VLOG_DBG_RL(&rl
, "attr %"PRIu16
" length %"PRIuSIZE
" not in "
809 "allowed range %"PRIuSIZE
"...%"PRIuSIZE
, type
, len
, min_len
, max_len
);
813 /* Strings must be null terminated and must not have embedded nulls. */
814 if (policy
->type
== NL_A_STRING
) {
815 if (((char *) nla
)[nla
->nla_len
- 1]) {
816 VLOG_DBG_RL(&rl
, "attr %"PRIu16
" lacks null at end", type
);
819 if (memchr(nla
+ 1, '\0', len
- 1) != NULL
) {
820 VLOG_DBG_RL(&rl
, "attr %"PRIu16
" has bad length", type
);
828 /* Parses the 'msg' starting at the given 'nla_offset' as a sequence of Netlink
829 * attributes. 'policy[i]', for 0 <= i < n_attrs, specifies how the attribute
830 * with nla_type == i is parsed; a pointer to attribute i is stored in
831 * attrs[i]. Returns true if successful, false on failure.
833 * If the Netlink attributes in 'msg' follow a Netlink header and a Generic
834 * Netlink header, then 'nla_offset' should be NLMSG_HDRLEN + GENL_HDRLEN. */
836 nl_policy_parse(const struct ofpbuf
*msg
, size_t nla_offset
,
837 const struct nl_policy policy
[],
838 struct nlattr
*attrs
[], size_t n_attrs
)
844 memset(attrs
, 0, n_attrs
* sizeof *attrs
);
846 if (msg
->size
< nla_offset
) {
847 VLOG_DBG_RL(&rl
, "missing headers in nl_policy_parse");
851 NL_ATTR_FOR_EACH (nla
, left
, ofpbuf_at(msg
, nla_offset
, 0),
852 msg
->size
- nla_offset
)
854 uint16_t type
= nl_attr_type(nla
);
855 if (type
< n_attrs
&& policy
[type
].type
!= NL_A_NO_ATTR
) {
856 const struct nl_policy
*e
= &policy
[type
];
857 if (!nl_attr_validate(nla
, e
)) {
861 VLOG_DBG_RL(&rl
, "duplicate attr %"PRIu16
, type
);
867 VLOG_DBG_RL(&rl
, "attributes followed by garbage");
871 for (i
= 0; i
< n_attrs
; i
++) {
872 const struct nl_policy
*e
= &policy
[i
];
873 if (!e
->optional
&& e
->type
!= NL_A_NO_ATTR
&& !attrs
[i
]) {
874 VLOG_DBG_RL(&rl
, "required attr %"PRIuSIZE
" missing", i
);
881 /* Parses the Netlink attributes within 'nla'. 'policy[i]', for 0 <= i <
882 * n_attrs, specifies how the attribute with nla_type == i is parsed; a pointer
883 * to attribute i is stored in attrs[i]. Returns true if successful, false on
886 nl_parse_nested(const struct nlattr
*nla
, const struct nl_policy policy
[],
887 struct nlattr
*attrs
[], size_t n_attrs
)
891 nl_attr_get_nested(nla
, &buf
);
892 return nl_policy_parse(&buf
, 0, policy
, attrs
, n_attrs
);
895 const struct nlattr
*
896 nl_attr_find__(const struct nlattr
*attrs
, size_t size
, uint16_t type
)
898 const struct nlattr
*nla
;
901 NL_ATTR_FOR_EACH (nla
, left
, attrs
, size
) {
902 if (nl_attr_type(nla
) == type
) {
909 /* Returns the first Netlink attribute within 'buf' with the specified 'type',
910 * skipping a header of 'hdr_len' bytes at the beginning of 'buf'.
912 * This function does not validate the attribute's length. */
913 const struct nlattr
*
914 nl_attr_find(const struct ofpbuf
*buf
, size_t hdr_len
, uint16_t type
)
916 return nl_attr_find__(ofpbuf_at(buf
, hdr_len
, 0), buf
->size
- hdr_len
,
920 /* Returns the first Netlink attribute within 'nla' with the specified
923 * This function does not validate the attribute's length. */
924 const struct nlattr
*
925 nl_attr_find_nested(const struct nlattr
*nla
, uint16_t type
)
927 return nl_attr_find__(nl_attr_get(nla
), nl_attr_get_size(nla
), type
);