]> git.proxmox.com Git - mirror_ovs.git/blob - lib/netlink.c
treewide: Avoid undefined behavior passing null in nonnull parameters.
[mirror_ovs.git] / lib / netlink.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "netlink.h"
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include "coverage.h"
24 #include "flow.h"
25 #include "netlink-protocol.h"
26 #include "openvswitch/ofpbuf.h"
27 #include "timeval.h"
28 #include "unaligned.h"
29 #include "openvswitch/vlog.h"
30 #include "util.h"
31
32 VLOG_DEFINE_THIS_MODULE(netlink);
33
34 /* A single (bad) Netlink message can in theory dump out many, many log
35 * messages, so the burst size is set quite high here to avoid missing useful
36 * information. Also, at high logging levels we log *all* Netlink messages. */
37 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 600);
38
39 /* Returns the nlmsghdr at the head of 'msg'.
40 *
41 * 'msg' must be at least as large as a nlmsghdr. */
42 struct nlmsghdr *
43 nl_msg_nlmsghdr(const struct ofpbuf *msg)
44 {
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. */
52 struct genlmsghdr *
53 nl_msg_genlmsghdr(const struct ofpbuf *msg)
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. */
63 bool
64 nl_msg_nlmsgerr(const struct ofpbuf *msg, int *errorp)
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) {
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) {
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. */
86 void
87 nl_msg_reserve(struct ofpbuf *msg, size_t size)
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.
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.
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 *
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.
108 *
109 * nl_msg_put_genlmsghdr() is more convenient for composing a Generic Netlink
110 * message. */
111 void
112 nl_msg_put_nlmsghdr(struct ofpbuf *msg,
113 size_t expected_payload, uint32_t type, uint32_t flags)
114 {
115 struct nlmsghdr *nlmsghdr;
116
117 ovs_assert(msg->size == 0);
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;
124 nlmsghdr->nlmsg_seq = 0;
125 nlmsghdr->nlmsg_pid = 0;
126 }
127
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.
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 *
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. */
149 void
150 nl_msg_put_genlmsghdr(struct ofpbuf *msg, size_t expected_payload,
151 int family, uint32_t flags, uint8_t cmd, uint8_t version)
152 {
153 struct genlmsghdr *genlmsghdr;
154
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;
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. */
166 void
167 nl_msg_put(struct ofpbuf *msg, const void *data, size_t size)
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. */
175 void *
176 nl_msg_put_uninit(struct ofpbuf *msg, size_t size)
177 {
178 size_t pad = PAD_SIZE(size, NLMSG_ALIGNTO);
179 char *p = ofpbuf_put_uninit(msg, size + pad);
180 if (pad) {
181 memset(p + size, 0, pad);
182 }
183 return p;
184 }
185
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. */
189 void
190 nl_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. */
198 void *
199 nl_msg_push_uninit(struct ofpbuf *msg, size_t size)
200 {
201 size_t pad = PAD_SIZE(size, NLMSG_ALIGNTO);
202 char *p = ofpbuf_push_uninit(msg, size + pad);
203 if (pad) {
204 memset(p + size, 0, pad);
205 }
206 return p;
207 }
208
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. */
213 void *
214 nl_msg_put_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size)
215 {
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;
221 return nla + 1;
222 }
223
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. */
228 void *
229 nl_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
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. Returns a pointer to the first byte of data in the
239 * attribute, which is left uninitialized. */
240 void
241 nl_msg_put_unspec(struct ofpbuf *msg, uint16_t type,
242 const void *data, size_t size)
243 {
244 void *ptr;
245
246 ptr = nl_msg_put_unspec_uninit(msg, type, size);
247 nullable_memcpy(ptr, data, size);
248 }
249
250 /* Appends a Netlink attribute of the given 'type' and no payload to 'msg'.
251 * (Some Netlink protocols use the presence or absence of an attribute as a
252 * Boolean flag.) */
253 void
254 nl_msg_put_flag(struct ofpbuf *msg, uint16_t type)
255 {
256 nl_msg_put_unspec(msg, type, NULL, 0);
257 }
258
259 /* Appends a Netlink attribute of the given 'type' and the given 8-bit 'value'
260 * to 'msg'. */
261 void
262 nl_msg_put_u8(struct ofpbuf *msg, uint16_t type, uint8_t value)
263 {
264 nl_msg_put_unspec(msg, type, &value, sizeof value);
265 }
266
267 /* Appends a Netlink attribute of the given 'type' and the given 16-bit host
268 * byte order 'value' to 'msg'. */
269 void
270 nl_msg_put_u16(struct ofpbuf *msg, uint16_t type, uint16_t value)
271 {
272 nl_msg_put_unspec(msg, type, &value, sizeof value);
273 }
274
275 /* Appends a Netlink attribute of the given 'type' and the given 32-bit host
276 * byte order 'value' to 'msg'. */
277 void
278 nl_msg_put_u32(struct ofpbuf *msg, uint16_t type, uint32_t value)
279 {
280 nl_msg_put_unspec(msg, type, &value, sizeof value);
281 }
282
283 /* Appends a Netlink attribute of the given 'type' and the given 64-bit host
284 * byte order 'value' to 'msg'. */
285 void
286 nl_msg_put_u64(struct ofpbuf *msg, uint16_t type, uint64_t value)
287 {
288 nl_msg_put_unspec(msg, type, &value, sizeof value);
289 }
290
291 /* Appends a Netlink attribute of the given 'type' and the given 16-bit network
292 * byte order 'value' to 'msg'. */
293 void
294 nl_msg_put_be16(struct ofpbuf *msg, uint16_t type, ovs_be16 value)
295 {
296 nl_msg_put_unspec(msg, type, &value, sizeof value);
297 }
298
299 /* Appends a Netlink attribute of the given 'type' and the given 32-bit network
300 * byte order 'value' to 'msg'. */
301 void
302 nl_msg_put_be32(struct ofpbuf *msg, uint16_t type, ovs_be32 value)
303 {
304 nl_msg_put_unspec(msg, type, &value, sizeof value);
305 }
306
307 /* Appends a Netlink attribute of the given 'type' and the given 64-bit network
308 * byte order 'value' to 'msg'. */
309 void
310 nl_msg_put_be64(struct ofpbuf *msg, uint16_t type, ovs_be64 value)
311 {
312 nl_msg_put_unspec(msg, type, &value, sizeof value);
313 }
314
315 /* Appends a Netlink attribute of the given 'type' and the given IPv6
316 * address order 'value' to 'msg'. */
317 void
318 nl_msg_put_in6_addr(struct ofpbuf *msg, uint16_t type,
319 const struct in6_addr *value)
320 {
321 nl_msg_put_unspec(msg, type, value, sizeof *value);
322 }
323
324 /* Appends a Netlink attribute of the given 'type' and the given odp_port_t
325 * 'value' to 'msg'. */
326 void
327 nl_msg_put_odp_port(struct ofpbuf *msg, uint16_t type, odp_port_t value)
328 {
329 nl_msg_put_u32(msg, type, odp_to_u32(value));
330 }
331
332 /* Appends a Netlink attribute of the given 'type' with the 'len' characters
333 * of 'value', followed by the null byte to 'msg'. */
334 void
335 nl_msg_put_string__(struct ofpbuf *msg, uint16_t type, const char *value,
336 size_t len)
337 {
338 char *data = nl_msg_put_unspec_uninit(msg, type, len + 1);
339
340 memcpy(data, value, len);
341 data[len] = '\0';
342 }
343
344 /* Appends a Netlink attribute of the given 'type' and the given
345 * null-terminated string 'value' to 'msg'. */
346 void
347 nl_msg_put_string(struct ofpbuf *msg, uint16_t type, const char *value)
348 {
349 nl_msg_put_unspec(msg, type, value, strlen(value) + 1);
350 }
351
352 /* Prepends a Netlink attribute of the given 'type' and room for 'size' bytes
353 * of data as its payload, plus Netlink padding if needed, to the head end of
354 * 'msg', reallocating and copying its data if necessary. Returns a pointer to
355 * the first byte of data in the attribute, which is left uninitialized. */
356 void *
357 nl_msg_push_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size)
358 {
359 size_t total_size = NLA_HDRLEN + size;
360 struct nlattr* nla = nl_msg_push_uninit(msg, total_size);
361 ovs_assert(!nl_attr_oversized(size));
362 nla->nla_len = total_size;
363 nla->nla_type = type;
364 return nla + 1;
365 }
366
367 /* Prepends a Netlink attribute of the given 'type' and the 'size' bytes of
368 * 'data' as its payload, to the head end of 'msg', reallocating and copying
369 * its data if necessary. Returns a pointer to the first byte of data in the
370 * attribute, which is left uninitialized. */
371 void
372 nl_msg_push_unspec(struct ofpbuf *msg, uint16_t type,
373 const void *data, size_t size)
374 {
375 memcpy(nl_msg_push_unspec_uninit(msg, type, size), data, size);
376 }
377
378 /* Prepends a Netlink attribute of the given 'type' and no payload to 'msg'.
379 * (Some Netlink protocols use the presence or absence of an attribute as a
380 * Boolean flag.) */
381 void
382 nl_msg_push_flag(struct ofpbuf *msg, uint16_t type)
383 {
384 nl_msg_push_unspec_uninit(msg, type, 0);
385 }
386
387 /* Prepends a Netlink attribute of the given 'type' and the given 8-bit 'value'
388 * to 'msg'. */
389 void
390 nl_msg_push_u8(struct ofpbuf *msg, uint16_t type, uint8_t value)
391 {
392 nl_msg_push_unspec(msg, type, &value, sizeof value);
393 }
394
395 /* Prepends a Netlink attribute of the given 'type' and the given 16-bit host
396 * byte order 'value' to 'msg'. */
397 void
398 nl_msg_push_u16(struct ofpbuf *msg, uint16_t type, uint16_t value)
399 {
400 nl_msg_push_unspec(msg, type, &value, sizeof value);
401 }
402
403 /* Prepends a Netlink attribute of the given 'type' and the given 32-bit host
404 * byte order 'value' to 'msg'. */
405 void
406 nl_msg_push_u32(struct ofpbuf *msg, uint16_t type, uint32_t value)
407 {
408 nl_msg_push_unspec(msg, type, &value, sizeof value);
409 }
410
411 /* Prepends a Netlink attribute of the given 'type' and the given 64-bit host
412 * byte order 'value' to 'msg'. */
413 void
414 nl_msg_push_u64(struct ofpbuf *msg, uint16_t type, uint64_t value)
415 {
416 nl_msg_push_unspec(msg, type, &value, sizeof value);
417 }
418
419 /* Prepends a Netlink attribute of the given 'type' and the given 16-bit
420 * network byte order 'value' to 'msg'. */
421 void
422 nl_msg_push_be16(struct ofpbuf *msg, uint16_t type, ovs_be16 value)
423 {
424 nl_msg_push_unspec(msg, type, &value, sizeof value);
425 }
426
427 /* Prepends a Netlink attribute of the given 'type' and the given 32-bit
428 * network byte order 'value' to 'msg'. */
429 void
430 nl_msg_push_be32(struct ofpbuf *msg, uint16_t type, ovs_be32 value)
431 {
432 nl_msg_push_unspec(msg, type, &value, sizeof value);
433 }
434
435 /* Prepends a Netlink attribute of the given 'type' and the given 64-bit
436 * network byte order 'value' to 'msg'. */
437 void
438 nl_msg_push_be64(struct ofpbuf *msg, uint16_t type, ovs_be64 value)
439 {
440 nl_msg_push_unspec(msg, type, &value, sizeof value);
441 }
442
443 /* Prepends a Netlink attribute of the given 'type' and the given
444 * null-terminated string 'value' to 'msg'. */
445 void
446 nl_msg_push_string(struct ofpbuf *msg, uint16_t type, const char *value)
447 {
448 nl_msg_push_unspec(msg, type, value, strlen(value) + 1);
449 }
450
451 /* Adds the header for nested Netlink attributes to 'msg', with the specified
452 * 'type', and returns the header's offset within 'msg'. The caller should add
453 * the content for the nested Netlink attribute to 'msg' (e.g. using the other
454 * nl_msg_*() functions), and then pass the returned offset to
455 * nl_msg_end_nested() to finish up the nested attributes. */
456 size_t
457 nl_msg_start_nested(struct ofpbuf *msg, uint16_t type)
458 {
459 size_t offset = msg->size;
460 nl_msg_put_unspec_uninit(msg, type, 0);
461 return offset;
462 }
463
464 /* Finalizes a nested Netlink attribute in 'msg'. 'offset' should be the value
465 * returned by nl_msg_start_nested(). */
466 void
467 nl_msg_end_nested(struct ofpbuf *msg, size_t offset)
468 {
469 struct nlattr *attr = ofpbuf_at_assert(msg, offset, sizeof *attr);
470 attr->nla_len = msg->size - offset;
471 }
472
473 /* Cancel a nested Netlink attribute in 'msg'. 'offset' should be the value
474 * returned by nl_msg_start_nested(). */
475 void
476 nl_msg_cancel_nested(struct ofpbuf *msg, size_t offset)
477 {
478 msg->size = offset;
479 }
480
481 /* Same as nls_msg_end_nested() when the nested Netlink contains non empty
482 * message. Otherwise, drop the nested message header from 'msg'.
483 *
484 * Return true if the nested message has been dropped. */
485 bool
486 nl_msg_end_non_empty_nested(struct ofpbuf *msg, size_t offset)
487 {
488 nl_msg_end_nested(msg, offset);
489
490 struct nlattr *attr = ofpbuf_at_assert(msg, offset, sizeof *attr);
491 if (!nl_attr_get_size(attr)) {
492 nl_msg_cancel_nested(msg, offset);
493 return true;
494 } else {
495 return false;
496 }
497 }
498
499 /* Appends a nested Netlink attribute of the given 'type', with the 'size'
500 * bytes of content starting at 'data', to 'msg'. */
501 void
502 nl_msg_put_nested(struct ofpbuf *msg,
503 uint16_t type, const void *data, size_t size)
504 {
505 size_t offset = nl_msg_start_nested(msg, type);
506 nl_msg_put(msg, data, size);
507 nl_msg_end_nested(msg, offset);
508 }
509
510 /* If 'buffer' begins with a valid "struct nlmsghdr", pulls the header and its
511 * payload off 'buffer', stores header and payload in 'msg->data' and
512 * 'msg->size', and returns a pointer to the header.
513 *
514 * If 'buffer' does not begin with a "struct nlmsghdr" or begins with one that
515 * is invalid, returns NULL and clears 'buffer' and 'msg'. */
516 struct nlmsghdr *
517 nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg)
518 {
519 if (buffer->size >= sizeof(struct nlmsghdr)) {
520 struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(buffer);
521 size_t len = nlmsghdr->nlmsg_len;
522 if (len >= sizeof *nlmsghdr && len <= buffer->size) {
523 ofpbuf_use_const(msg, nlmsghdr, len);
524 ofpbuf_pull(buffer, len);
525 return nlmsghdr;
526 }
527 }
528
529 ofpbuf_clear(buffer);
530 msg->data = NULL;
531 msg->size = 0;
532 return NULL;
533 }
534
535 /* Returns true if a Netlink attribute with a payload that is 'payload_size'
536 * bytes long would be oversized, that is, if it's not possible to create an
537 * nlattr of that size because its size wouldn't fit in the 16-bit nla_len
538 * field. */
539 bool
540 nl_attr_oversized(size_t payload_size)
541 {
542 return payload_size > UINT16_MAX - NLA_HDRLEN;
543 }
544 \f
545 /* Attributes. */
546
547 /* Returns the bits of 'nla->nla_type' that are significant for determining its
548 * type. */
549 int
550 nl_attr_type(const struct nlattr *nla)
551 {
552 return nla->nla_type & NLA_TYPE_MASK;
553 }
554
555 /* Returns the first byte in the payload of attribute 'nla'. */
556 const void *
557 nl_attr_get(const struct nlattr *nla)
558 {
559 ovs_assert(nla->nla_len >= NLA_HDRLEN);
560 return nla + 1;
561 }
562
563 /* Returns the number of bytes in the payload of attribute 'nla'. */
564 size_t
565 nl_attr_get_size(const struct nlattr *nla)
566 {
567 ovs_assert(nla->nla_len >= NLA_HDRLEN);
568 return nla->nla_len - NLA_HDRLEN;
569 }
570
571 /* Asserts that 'nla''s payload is at least 'size' bytes long, and returns the
572 * first byte of the payload. */
573 const void *
574 nl_attr_get_unspec(const struct nlattr *nla, size_t size)
575 {
576 ovs_assert(nla->nla_len >= NLA_HDRLEN + size);
577 return nla + 1;
578 }
579
580 /* Returns true if 'nla' is nonnull. (Some Netlink protocols use the presence
581 * or absence of an attribute as a Boolean flag.) */
582 bool
583 nl_attr_get_flag(const struct nlattr *nla)
584 {
585 return nla != NULL;
586 }
587
588 #define NL_ATTR_GET_AS(NLA, TYPE) \
589 (*(TYPE*) nl_attr_get_unspec(nla, sizeof(TYPE)))
590
591 /* Returns the 8-bit value in 'nla''s payload.
592 *
593 * Asserts that 'nla''s payload is at least 1 byte long. */
594 uint8_t
595 nl_attr_get_u8(const struct nlattr *nla)
596 {
597 return NL_ATTR_GET_AS(nla, uint8_t);
598 }
599
600 /* Returns the 16-bit host byte order value in 'nla''s payload.
601 *
602 * Asserts that 'nla''s payload is at least 2 bytes long. */
603 uint16_t
604 nl_attr_get_u16(const struct nlattr *nla)
605 {
606 return NL_ATTR_GET_AS(nla, uint16_t);
607 }
608
609 /* Returns the 32-bit host byte order value in 'nla''s payload.
610 *
611 * Asserts that 'nla''s payload is at least 4 bytes long. */
612 uint32_t
613 nl_attr_get_u32(const struct nlattr *nla)
614 {
615 return NL_ATTR_GET_AS(nla, uint32_t);
616 }
617
618 /* Returns the 64-bit host byte order value in 'nla''s payload.
619 *
620 * Asserts that 'nla''s payload is at least 8 bytes long. */
621 uint64_t
622 nl_attr_get_u64(const struct nlattr *nla)
623 {
624 const ovs_32aligned_u64 *x = nl_attr_get_unspec(nla, sizeof *x);
625 return get_32aligned_u64(x);
626 }
627
628 /* Returns the 16-bit network byte order value in 'nla''s payload.
629 *
630 * Asserts that 'nla''s payload is at least 2 bytes long. */
631 ovs_be16
632 nl_attr_get_be16(const struct nlattr *nla)
633 {
634 return NL_ATTR_GET_AS(nla, ovs_be16);
635 }
636
637 /* Returns the 32-bit network byte order value in 'nla''s payload.
638 *
639 * Asserts that 'nla''s payload is at least 4 bytes long. */
640 ovs_be32
641 nl_attr_get_be32(const struct nlattr *nla)
642 {
643 return NL_ATTR_GET_AS(nla, ovs_be32);
644 }
645
646 /* Returns the 64-bit network byte order value in 'nla''s payload.
647 *
648 * Asserts that 'nla''s payload is at least 8 bytes long. */
649 ovs_be64
650 nl_attr_get_be64(const struct nlattr *nla)
651 {
652 const ovs_32aligned_be64 *x = nl_attr_get_unspec(nla, sizeof *x);
653 return get_32aligned_be64(x);
654 }
655
656 /* Returns the IPv6 address value in 'nla''s payload.
657 *
658 * Asserts that 'nla''s payload is at least 16 bytes long. */
659 struct in6_addr
660 nl_attr_get_in6_addr(const struct nlattr *nla)
661 {
662 return NL_ATTR_GET_AS(nla, struct in6_addr);
663 }
664
665 /* Returns the 32-bit odp_port_t value in 'nla''s payload.
666 *
667 * Asserts that 'nla''s payload is at least 4 bytes long. */
668 odp_port_t
669 nl_attr_get_odp_port(const struct nlattr *nla)
670 {
671 return u32_to_odp(nl_attr_get_u32(nla));
672 }
673
674 /* Returns the null-terminated string value in 'nla''s payload.
675 *
676 * Asserts that 'nla''s payload contains a null-terminated string. */
677 const char *
678 nl_attr_get_string(const struct nlattr *nla)
679 {
680 ovs_assert(nla->nla_len > NLA_HDRLEN);
681 ovs_assert(memchr(nl_attr_get(nla), '\0', nla->nla_len - NLA_HDRLEN));
682 return nl_attr_get(nla);
683 }
684
685 /* Initializes 'nested' to the payload of 'nla'. */
686 void
687 nl_attr_get_nested(const struct nlattr *nla, struct ofpbuf *nested)
688 {
689 ofpbuf_use_const(nested, nl_attr_get(nla), nl_attr_get_size(nla));
690 }
691
692 /* Default minimum payload size for each type of attribute. */
693 static size_t
694 min_attr_len(enum nl_attr_type type)
695 {
696 switch (type) {
697 case NL_A_NO_ATTR: return 0;
698 case NL_A_UNSPEC: return 0;
699 case NL_A_U8: return 1;
700 case NL_A_U16: return 2;
701 case NL_A_U32: return 4;
702 case NL_A_U64: return 8;
703 case NL_A_STRING: return 1;
704 case NL_A_FLAG: return 0;
705 case NL_A_IPV6: return 16;
706 case NL_A_NESTED: return 0;
707 case N_NL_ATTR_TYPES: default: OVS_NOT_REACHED();
708 }
709 }
710
711 /* Default maximum payload size for each type of attribute. */
712 static size_t
713 max_attr_len(enum nl_attr_type type)
714 {
715 switch (type) {
716 case NL_A_NO_ATTR: return SIZE_MAX;
717 case NL_A_UNSPEC: return SIZE_MAX;
718 case NL_A_U8: return 1;
719 case NL_A_U16: return 2;
720 case NL_A_U32: return 4;
721 case NL_A_U64: return 8;
722 case NL_A_STRING: return SIZE_MAX;
723 case NL_A_FLAG: return SIZE_MAX;
724 case NL_A_IPV6: return 16;
725 case NL_A_NESTED: return SIZE_MAX;
726 case N_NL_ATTR_TYPES: default: OVS_NOT_REACHED();
727 }
728 }
729
730 bool
731 nl_attr_validate(const struct nlattr *nla, const struct nl_policy *policy)
732 {
733 uint16_t type = nl_attr_type(nla);
734 size_t min_len;
735 size_t max_len;
736 size_t len;
737
738 if (policy->type == NL_A_NO_ATTR) {
739 return true;
740 }
741
742 /* Figure out min and max length. */
743 min_len = policy->min_len;
744 if (!min_len) {
745 min_len = min_attr_len(policy->type);
746 }
747 max_len = policy->max_len;
748 if (!max_len) {
749 max_len = max_attr_len(policy->type);
750 }
751
752 /* Verify length. */
753 len = nl_attr_get_size(nla);
754 if (len < min_len || len > max_len) {
755 VLOG_DBG_RL(&rl, "attr %"PRIu16" length %"PRIuSIZE" not in "
756 "allowed range %"PRIuSIZE"...%"PRIuSIZE, type, len, min_len, max_len);
757 return false;
758 }
759
760 /* Strings must be null terminated and must not have embedded nulls. */
761 if (policy->type == NL_A_STRING) {
762 if (((char *) nla)[nla->nla_len - 1]) {
763 VLOG_DBG_RL(&rl, "attr %"PRIu16" lacks null at end", type);
764 return false;
765 }
766 if (memchr(nla + 1, '\0', len - 1) != NULL) {
767 VLOG_DBG_RL(&rl, "attr %"PRIu16" has bad length", type);
768 return false;
769 }
770 }
771
772 return true;
773 }
774
775 /* Parses the 'msg' starting at the given 'nla_offset' as a sequence of Netlink
776 * attributes. 'policy[i]', for 0 <= i < n_attrs, specifies how the attribute
777 * with nla_type == i is parsed; a pointer to attribute i is stored in
778 * attrs[i]. Returns true if successful, false on failure.
779 *
780 * If the Netlink attributes in 'msg' follow a Netlink header and a Generic
781 * Netlink header, then 'nla_offset' should be NLMSG_HDRLEN + GENL_HDRLEN. */
782 bool
783 nl_policy_parse(const struct ofpbuf *msg, size_t nla_offset,
784 const struct nl_policy policy[],
785 struct nlattr *attrs[], size_t n_attrs)
786 {
787 struct nlattr *nla;
788 size_t left;
789 size_t i;
790
791 memset(attrs, 0, n_attrs * sizeof *attrs);
792
793 if (msg->size < nla_offset) {
794 VLOG_DBG_RL(&rl, "missing headers in nl_policy_parse");
795 return false;
796 }
797
798 NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, nla_offset, 0),
799 msg->size - nla_offset)
800 {
801 uint16_t type = nl_attr_type(nla);
802 if (type < n_attrs && policy[type].type != NL_A_NO_ATTR) {
803 const struct nl_policy *e = &policy[type];
804 if (!nl_attr_validate(nla, e)) {
805 return false;
806 }
807 if (attrs[type]) {
808 VLOG_DBG_RL(&rl, "duplicate attr %"PRIu16, type);
809 }
810 attrs[type] = nla;
811 }
812 }
813 if (left) {
814 VLOG_DBG_RL(&rl, "attributes followed by garbage");
815 return false;
816 }
817
818 for (i = 0; i < n_attrs; i++) {
819 const struct nl_policy *e = &policy[i];
820 if (!e->optional && e->type != NL_A_NO_ATTR && !attrs[i]) {
821 VLOG_DBG_RL(&rl, "required attr %"PRIuSIZE" missing", i);
822 return false;
823 }
824 }
825 return true;
826 }
827
828 /* Parses the Netlink attributes within 'nla'. 'policy[i]', for 0 <= i <
829 * n_attrs, specifies how the attribute with nla_type == i is parsed; a pointer
830 * to attribute i is stored in attrs[i]. Returns true if successful, false on
831 * failure. */
832 bool
833 nl_parse_nested(const struct nlattr *nla, const struct nl_policy policy[],
834 struct nlattr *attrs[], size_t n_attrs)
835 {
836 struct ofpbuf buf;
837
838 nl_attr_get_nested(nla, &buf);
839 return nl_policy_parse(&buf, 0, policy, attrs, n_attrs);
840 }
841
842 const struct nlattr *
843 nl_attr_find__(const struct nlattr *attrs, size_t size, uint16_t type)
844 {
845 const struct nlattr *nla;
846 size_t left;
847
848 NL_ATTR_FOR_EACH (nla, left, attrs, size) {
849 if (nl_attr_type(nla) == type) {
850 return nla;
851 }
852 }
853 return NULL;
854 }
855
856 /* Returns the first Netlink attribute within 'buf' with the specified 'type',
857 * skipping a header of 'hdr_len' bytes at the beginning of 'buf'.
858 *
859 * This function does not validate the attribute's length. */
860 const struct nlattr *
861 nl_attr_find(const struct ofpbuf *buf, size_t hdr_len, uint16_t type)
862 {
863 return nl_attr_find__(ofpbuf_at(buf, hdr_len, 0), buf->size - hdr_len,
864 type);
865 }
866
867 /* Returns the first Netlink attribute within 'nla' with the specified
868 * 'type'.
869 *
870 * This function does not validate the attribute's length. */
871 const struct nlattr *
872 nl_attr_find_nested(const struct nlattr *nla, uint16_t type)
873 {
874 return nl_attr_find__(nl_attr_get(nla), nl_attr_get_size(nla), type);
875 }