netdev-dpdk: fix mbuf leaks
[cascardo/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 "ofpbuf.h"
27 #include "timeval.h"
28 #include "unaligned.h"
29 #include "openvswitch/vlog.h"
30
31 VLOG_DEFINE_THIS_MODULE(netlink);
32
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);
37
38 /* Returns the nlmsghdr at the head of 'msg'.
39  *
40  * 'msg' must be at least as large as a nlmsghdr. */
41 struct nlmsghdr *
42 nl_msg_nlmsghdr(const struct ofpbuf *msg)
43 {
44     return ofpbuf_at_assert(msg, 0, NLMSG_HDRLEN);
45 }
46
47 /* Returns the genlmsghdr just past 'msg''s nlmsghdr.
48  *
49  * Returns a null pointer if 'msg' is not large enough to contain an nlmsghdr
50  * and a genlmsghdr. */
51 struct genlmsghdr *
52 nl_msg_genlmsghdr(const struct ofpbuf *msg)
53 {
54     return ofpbuf_at(msg, NLMSG_HDRLEN, GENL_HDRLEN);
55 }
56
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.
60  *
61  * 'msg' must be at least as large as a nlmsghdr. */
62 bool
63 nl_msg_nlmsgerr(const struct ofpbuf *msg, int *errorp)
64 {
65     if (nl_msg_nlmsghdr(msg)->nlmsg_type == NLMSG_ERROR) {
66         struct nlmsgerr *err = ofpbuf_at(msg, NLMSG_HDRLEN, sizeof *err);
67         int code = EPROTO;
68         if (!err) {
69             VLOG_ERR_RL(&rl, "received invalid nlmsgerr (%"PRIu32" bytes < %"PRIuSIZE")",
70                         msg->size, NLMSG_HDRLEN + sizeof *err);
71         } else if (err->error <= 0 && err->error > INT_MIN) {
72             code = -err->error;
73         }
74         if (errorp) {
75             *errorp = code;
76         }
77         return true;
78     } else {
79         return false;
80     }
81 }
82
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. */
85 void
86 nl_msg_reserve(struct ofpbuf *msg, size_t size)
87 {
88     ofpbuf_prealloc_tailroom(msg, NLMSG_ALIGN(size));
89 }
90
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.
95  *
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().
99  *
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.
103  *
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.
107  *
108  * nl_msg_put_genlmsghdr() is more convenient for composing a Generic Netlink
109  * message. */
110 void
111 nl_msg_put_nlmsghdr(struct ofpbuf *msg,
112                     size_t expected_payload, uint32_t type, uint32_t flags)
113 {
114     struct nlmsghdr *nlmsghdr;
115
116     ovs_assert(msg->size == 0);
117
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;
125 }
126
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.
131  *
132  * 'family' is the family number obtained via nl_lookup_genl_family().
133  *
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.
137  *
138  * 'cmd' is an enumerated value specific to the Generic Netlink family
139  * (e.g. CTRL_CMD_NEWFAMILY for the GENL_ID_CTRL family).
140  *
141  * 'version' is a version number specific to the family and command (often 1).
142  *
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.
145  *
146  * nl_msg_put_nlmsghdr() should be used to compose Netlink messages that are
147  * not Generic Netlink messages. */
148 void
149 nl_msg_put_genlmsghdr(struct ofpbuf *msg, size_t expected_payload,
150                       int family, uint32_t flags, uint8_t cmd, uint8_t version)
151 {
152     struct genlmsghdr *genlmsghdr;
153
154     nl_msg_put_nlmsghdr(msg, GENL_HDRLEN + expected_payload, family, flags);
155     ovs_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;
160 }
161
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
164  * necessary. */
165 void
166 nl_msg_put(struct ofpbuf *msg, const void *data, size_t size)
167 {
168     memcpy(nl_msg_put_uninit(msg, size), data, size);
169 }
170
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. */
174 void *
175 nl_msg_put_uninit(struct ofpbuf *msg, size_t size)
176 {
177     size_t pad = PAD_SIZE(size, NLMSG_ALIGNTO);
178     char *p = ofpbuf_put_uninit(msg, size + pad);
179     if (pad) {
180         memset(p + size, 0, pad);
181     }
182     return p;
183 }
184
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
187  * necessary. */
188 void
189 nl_msg_push(struct ofpbuf *msg, const void *data, size_t size)
190 {
191     memcpy(nl_msg_push_uninit(msg, size), data, size);
192 }
193
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. */
197 void *
198 nl_msg_push_uninit(struct ofpbuf *msg, size_t size)
199 {
200     size_t pad = PAD_SIZE(size, NLMSG_ALIGNTO);
201     char *p = ofpbuf_push_uninit(msg, size + pad);
202     if (pad) {
203         memset(p + size, 0, pad);
204     }
205     return p;
206 }
207
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. */
212 void *
213 nl_msg_put_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size)
214 {
215     size_t total_size = NLA_HDRLEN + size;
216     struct nlattr* nla = nl_msg_put_uninit(msg, total_size);
217     ovs_assert(!nl_attr_oversized(size));
218     nla->nla_len = total_size;
219     nla->nla_type = type;
220     return nla + 1;
221 }
222
223 /* Appends a Netlink attribute of the given 'type' and room for 'size' bytes of
224  * data as its payload, plus Netlink padding if needed, to the tail end of
225  * 'msg', reallocating and copying its data if necessary.  Returns a pointer to
226  * the first byte of data in the attribute, which is zeroed. */
227 void *
228 nl_msg_put_unspec_zero(struct ofpbuf *msg, uint16_t type, size_t size)
229 {
230     void *data = nl_msg_put_unspec_uninit(msg, type, size);
231     memset(data, 0, size);
232     return data;
233 }
234
235 /* Appends a Netlink attribute of the given 'type' and the 'size' bytes of
236  * 'data' as its payload, to the tail end of 'msg', reallocating and copying
237  * its data if necessary.  Returns a pointer to the first byte of data in the
238  * attribute, which is left uninitialized. */
239 void
240 nl_msg_put_unspec(struct ofpbuf *msg, uint16_t type,
241                   const void *data, size_t size)
242 {
243     memcpy(nl_msg_put_unspec_uninit(msg, type, size), data, size);
244 }
245
246 /* Appends a Netlink attribute of the given 'type' and no payload to 'msg'.
247  * (Some Netlink protocols use the presence or absence of an attribute as a
248  * Boolean flag.) */
249 void
250 nl_msg_put_flag(struct ofpbuf *msg, uint16_t type)
251 {
252     nl_msg_put_unspec(msg, type, NULL, 0);
253 }
254
255 /* Appends a Netlink attribute of the given 'type' and the given 8-bit 'value'
256  * to 'msg'. */
257 void
258 nl_msg_put_u8(struct ofpbuf *msg, uint16_t type, uint8_t value)
259 {
260     nl_msg_put_unspec(msg, type, &value, sizeof value);
261 }
262
263 /* Appends a Netlink attribute of the given 'type' and the given 16-bit host
264  * byte order 'value' to 'msg'. */
265 void
266 nl_msg_put_u16(struct ofpbuf *msg, uint16_t type, uint16_t value)
267 {
268     nl_msg_put_unspec(msg, type, &value, sizeof value);
269 }
270
271 /* Appends a Netlink attribute of the given 'type' and the given 32-bit host
272  * byte order 'value' to 'msg'. */
273 void
274 nl_msg_put_u32(struct ofpbuf *msg, uint16_t type, uint32_t value)
275 {
276     nl_msg_put_unspec(msg, type, &value, sizeof value);
277 }
278
279 /* Appends a Netlink attribute of the given 'type' and the given 64-bit host
280  * byte order 'value' to 'msg'. */
281 void
282 nl_msg_put_u64(struct ofpbuf *msg, uint16_t type, uint64_t value)
283 {
284     nl_msg_put_unspec(msg, type, &value, sizeof value);
285 }
286
287 /* Appends a Netlink attribute of the given 'type' and the given 16-bit network
288  * byte order 'value' to 'msg'. */
289 void
290 nl_msg_put_be16(struct ofpbuf *msg, uint16_t type, ovs_be16 value)
291 {
292     nl_msg_put_unspec(msg, type, &value, sizeof value);
293 }
294
295 /* Appends a Netlink attribute of the given 'type' and the given 32-bit network
296  * byte order 'value' to 'msg'. */
297 void
298 nl_msg_put_be32(struct ofpbuf *msg, uint16_t type, ovs_be32 value)
299 {
300     nl_msg_put_unspec(msg, type, &value, sizeof value);
301 }
302
303 /* Appends a Netlink attribute of the given 'type' and the given 64-bit network
304  * byte order 'value' to 'msg'. */
305 void
306 nl_msg_put_be64(struct ofpbuf *msg, uint16_t type, ovs_be64 value)
307 {
308     nl_msg_put_unspec(msg, type, &value, sizeof value);
309 }
310
311 /* Appends a Netlink attribute of the given 'type' and the given IPv6
312  * address order 'value' to 'msg'. */
313 void
314 nl_msg_put_in6_addr(struct ofpbuf *msg, uint16_t type,
315                     const struct in6_addr *value)
316 {
317     nl_msg_put_unspec(msg, type, value, sizeof *value);
318 }
319
320 /* Appends a Netlink attribute of the given 'type' and the given odp_port_t
321  * 'value' to 'msg'. */
322 void
323 nl_msg_put_odp_port(struct ofpbuf *msg, uint16_t type, odp_port_t value)
324 {
325     nl_msg_put_u32(msg, type, odp_to_u32(value));
326 }
327
328 /* Appends a Netlink attribute of the given 'type' with the 'len' characters
329  * of 'value', followed by the null byte to 'msg'. */
330 void
331 nl_msg_put_string__(struct ofpbuf *msg, uint16_t type, const char *value,
332                     size_t len)
333 {
334     char *data = nl_msg_put_unspec_uninit(msg, type, len + 1);
335
336     memcpy(data, value, len);
337     data[len] = '\0';
338 }
339
340 /* Appends a Netlink attribute of the given 'type' and the given
341  * null-terminated string 'value' to 'msg'. */
342 void
343 nl_msg_put_string(struct ofpbuf *msg, uint16_t type, const char *value)
344 {
345     nl_msg_put_unspec(msg, type, value, strlen(value) + 1);
346 }
347
348 /* Prepends a Netlink attribute of the given 'type' and room for 'size' bytes
349  * of data as its payload, plus Netlink padding if needed, to the head end of
350  * 'msg', reallocating and copying its data if necessary.  Returns a pointer to
351  * the first byte of data in the attribute, which is left uninitialized. */
352 void *
353 nl_msg_push_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size)
354 {
355     size_t total_size = NLA_HDRLEN + size;
356     struct nlattr* nla = nl_msg_push_uninit(msg, total_size);
357     ovs_assert(!nl_attr_oversized(size));
358     nla->nla_len = total_size;
359     nla->nla_type = type;
360     return nla + 1;
361 }
362
363 /* Prepends a Netlink attribute of the given 'type' and the 'size' bytes of
364  * 'data' as its payload, to the head end of 'msg', reallocating and copying
365  * its data if necessary.  Returns a pointer to the first byte of data in the
366  * attribute, which is left uninitialized. */
367 void
368 nl_msg_push_unspec(struct ofpbuf *msg, uint16_t type,
369                   const void *data, size_t size)
370 {
371     memcpy(nl_msg_push_unspec_uninit(msg, type, size), data, size);
372 }
373
374 /* Prepends a Netlink attribute of the given 'type' and no payload to 'msg'.
375  * (Some Netlink protocols use the presence or absence of an attribute as a
376  * Boolean flag.) */
377 void
378 nl_msg_push_flag(struct ofpbuf *msg, uint16_t type)
379 {
380     nl_msg_push_unspec(msg, type, NULL, 0);
381 }
382
383 /* Prepends a Netlink attribute of the given 'type' and the given 8-bit 'value'
384  * to 'msg'. */
385 void
386 nl_msg_push_u8(struct ofpbuf *msg, uint16_t type, uint8_t value)
387 {
388     nl_msg_push_unspec(msg, type, &value, sizeof value);
389 }
390
391 /* Prepends a Netlink attribute of the given 'type' and the given 16-bit host
392  * byte order 'value' to 'msg'. */
393 void
394 nl_msg_push_u16(struct ofpbuf *msg, uint16_t type, uint16_t value)
395 {
396     nl_msg_push_unspec(msg, type, &value, sizeof value);
397 }
398
399 /* Prepends a Netlink attribute of the given 'type' and the given 32-bit host
400  * byte order 'value' to 'msg'. */
401 void
402 nl_msg_push_u32(struct ofpbuf *msg, uint16_t type, uint32_t value)
403 {
404     nl_msg_push_unspec(msg, type, &value, sizeof value);
405 }
406
407 /* Prepends a Netlink attribute of the given 'type' and the given 64-bit host
408  * byte order 'value' to 'msg'. */
409 void
410 nl_msg_push_u64(struct ofpbuf *msg, uint16_t type, uint64_t value)
411 {
412     nl_msg_push_unspec(msg, type, &value, sizeof value);
413 }
414
415 /* Prepends a Netlink attribute of the given 'type' and the given 16-bit
416  * network byte order 'value' to 'msg'. */
417 void
418 nl_msg_push_be16(struct ofpbuf *msg, uint16_t type, ovs_be16 value)
419 {
420     nl_msg_push_unspec(msg, type, &value, sizeof value);
421 }
422
423 /* Prepends a Netlink attribute of the given 'type' and the given 32-bit
424  * network byte order 'value' to 'msg'. */
425 void
426 nl_msg_push_be32(struct ofpbuf *msg, uint16_t type, ovs_be32 value)
427 {
428     nl_msg_push_unspec(msg, type, &value, sizeof value);
429 }
430
431 /* Prepends a Netlink attribute of the given 'type' and the given 64-bit
432  * network byte order 'value' to 'msg'. */
433 void
434 nl_msg_push_be64(struct ofpbuf *msg, uint16_t type, ovs_be64 value)
435 {
436     nl_msg_push_unspec(msg, type, &value, sizeof value);
437 }
438
439 /* Prepends a Netlink attribute of the given 'type' and the given
440  * null-terminated string 'value' to 'msg'. */
441 void
442 nl_msg_push_string(struct ofpbuf *msg, uint16_t type, const char *value)
443 {
444     nl_msg_push_unspec(msg, type, value, strlen(value) + 1);
445 }
446
447 /* Adds the header for nested Netlink attributes to 'msg', with the specified
448  * 'type', and returns the header's offset within 'msg'.  The caller should add
449  * the content for the nested Netlink attribute to 'msg' (e.g. using the other
450  * nl_msg_*() functions), and then pass the returned offset to
451  * nl_msg_end_nested() to finish up the nested attributes. */
452 size_t
453 nl_msg_start_nested(struct ofpbuf *msg, uint16_t type)
454 {
455     size_t offset = msg->size;
456     nl_msg_put_unspec(msg, type, NULL, 0);
457     return offset;
458 }
459
460 /* Finalizes a nested Netlink attribute in 'msg'.  'offset' should be the value
461  * returned by nl_msg_start_nested(). */
462 void
463 nl_msg_end_nested(struct ofpbuf *msg, size_t offset)
464 {
465     struct nlattr *attr = ofpbuf_at_assert(msg, offset, sizeof *attr);
466     attr->nla_len = msg->size - offset;
467 }
468
469 /* Appends a nested Netlink attribute of the given 'type', with the 'size'
470  * bytes of content starting at 'data', to 'msg'. */
471 void
472 nl_msg_put_nested(struct ofpbuf *msg,
473                   uint16_t type, const void *data, size_t size)
474 {
475     size_t offset = nl_msg_start_nested(msg, type);
476     nl_msg_put(msg, data, size);
477     nl_msg_end_nested(msg, offset);
478 }
479
480 /* If 'buffer' begins with a valid "struct nlmsghdr", pulls the header and its
481  * payload off 'buffer', stores header and payload in 'msg->data' and
482  * 'msg->size', and returns a pointer to the header.
483  *
484  * If 'buffer' does not begin with a "struct nlmsghdr" or begins with one that
485  * is invalid, returns NULL and clears 'buffer' and 'msg'. */
486 struct nlmsghdr *
487 nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg)
488 {
489     if (buffer->size >= sizeof(struct nlmsghdr)) {
490         struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(buffer);
491         size_t len = nlmsghdr->nlmsg_len;
492         if (len >= sizeof *nlmsghdr && len <= buffer->size) {
493             ofpbuf_use_const(msg, nlmsghdr, len);
494             ofpbuf_pull(buffer, len);
495             return nlmsghdr;
496         }
497     }
498
499     ofpbuf_clear(buffer);
500     msg->data = NULL;
501     msg->size = 0;
502     return NULL;
503 }
504
505 /* Returns true if a Netlink attribute with a payload that is 'payload_size'
506  * bytes long would be oversized, that is, if it's not possible to create an
507  * nlattr of that size because its size wouldn't fit in the 16-bit nla_len
508  * field. */
509 bool
510 nl_attr_oversized(size_t payload_size)
511 {
512     return payload_size > UINT16_MAX - NLA_HDRLEN;
513 }
514 \f
515 /* Attributes. */
516
517 /* Returns the bits of 'nla->nla_type' that are significant for determining its
518  * type. */
519 int
520 nl_attr_type(const struct nlattr *nla)
521 {
522     return nla->nla_type & NLA_TYPE_MASK;
523 }
524
525 /* Returns the first byte in the payload of attribute 'nla'. */
526 const void *
527 nl_attr_get(const struct nlattr *nla)
528 {
529     ovs_assert(nla->nla_len >= NLA_HDRLEN);
530     return nla + 1;
531 }
532
533 /* Returns the number of bytes in the payload of attribute 'nla'. */
534 size_t
535 nl_attr_get_size(const struct nlattr *nla)
536 {
537     ovs_assert(nla->nla_len >= NLA_HDRLEN);
538     return nla->nla_len - NLA_HDRLEN;
539 }
540
541 /* Asserts that 'nla''s payload is at least 'size' bytes long, and returns the
542  * first byte of the payload. */
543 const void *
544 nl_attr_get_unspec(const struct nlattr *nla, size_t size)
545 {
546     ovs_assert(nla->nla_len >= NLA_HDRLEN + size);
547     return nla + 1;
548 }
549
550 /* Returns true if 'nla' is nonnull.  (Some Netlink protocols use the presence
551  * or absence of an attribute as a Boolean flag.) */
552 bool
553 nl_attr_get_flag(const struct nlattr *nla)
554 {
555     return nla != NULL;
556 }
557
558 #define NL_ATTR_GET_AS(NLA, TYPE) \
559         (*(TYPE*) nl_attr_get_unspec(nla, sizeof(TYPE)))
560
561 /* Returns the 8-bit value in 'nla''s payload.
562  *
563  * Asserts that 'nla''s payload is at least 1 byte long. */
564 uint8_t
565 nl_attr_get_u8(const struct nlattr *nla)
566 {
567     return NL_ATTR_GET_AS(nla, uint8_t);
568 }
569
570 /* Returns the 16-bit host byte order value in 'nla''s payload.
571  *
572  * Asserts that 'nla''s payload is at least 2 bytes long. */
573 uint16_t
574 nl_attr_get_u16(const struct nlattr *nla)
575 {
576     return NL_ATTR_GET_AS(nla, uint16_t);
577 }
578
579 /* Returns the 32-bit host byte order value in 'nla''s payload.
580  *
581  * Asserts that 'nla''s payload is at least 4 bytes long. */
582 uint32_t
583 nl_attr_get_u32(const struct nlattr *nla)
584 {
585     return NL_ATTR_GET_AS(nla, uint32_t);
586 }
587
588 /* Returns the 64-bit host byte order value in 'nla''s payload.
589  *
590  * Asserts that 'nla''s payload is at least 8 bytes long. */
591 uint64_t
592 nl_attr_get_u64(const struct nlattr *nla)
593 {
594     const ovs_32aligned_u64 *x = nl_attr_get_unspec(nla, sizeof *x);
595     return get_32aligned_u64(x);
596 }
597
598 /* Returns the 16-bit network byte order value in 'nla''s payload.
599  *
600  * Asserts that 'nla''s payload is at least 2 bytes long. */
601 ovs_be16
602 nl_attr_get_be16(const struct nlattr *nla)
603 {
604     return NL_ATTR_GET_AS(nla, ovs_be16);
605 }
606
607 /* Returns the 32-bit network byte order value in 'nla''s payload.
608  *
609  * Asserts that 'nla''s payload is at least 4 bytes long. */
610 ovs_be32
611 nl_attr_get_be32(const struct nlattr *nla)
612 {
613     return NL_ATTR_GET_AS(nla, ovs_be32);
614 }
615
616 /* Returns the 64-bit network byte order value in 'nla''s payload.
617  *
618  * Asserts that 'nla''s payload is at least 8 bytes long. */
619 ovs_be64
620 nl_attr_get_be64(const struct nlattr *nla)
621 {
622     const ovs_32aligned_be64 *x = nl_attr_get_unspec(nla, sizeof *x);
623     return get_32aligned_be64(x);
624 }
625
626 /* Returns the IPv6 address value in 'nla''s payload.
627  *
628  * Asserts that 'nla''s payload is at least 16 bytes long. */
629 struct in6_addr
630 nl_attr_get_in6_addr(const struct nlattr *nla)
631 {
632     return NL_ATTR_GET_AS(nla, struct in6_addr);
633 }
634
635 /* Returns the 32-bit odp_port_t value in 'nla''s payload.
636  *
637  * Asserts that 'nla''s payload is at least 4 bytes long. */
638 odp_port_t
639 nl_attr_get_odp_port(const struct nlattr *nla)
640 {
641     return u32_to_odp(nl_attr_get_u32(nla));
642 }
643
644 /* Returns the null-terminated string value in 'nla''s payload.
645  *
646  * Asserts that 'nla''s payload contains a null-terminated string. */
647 const char *
648 nl_attr_get_string(const struct nlattr *nla)
649 {
650     ovs_assert(nla->nla_len > NLA_HDRLEN);
651     ovs_assert(memchr(nl_attr_get(nla), '\0', nla->nla_len - NLA_HDRLEN));
652     return nl_attr_get(nla);
653 }
654
655 /* Initializes 'nested' to the payload of 'nla'. */
656 void
657 nl_attr_get_nested(const struct nlattr *nla, struct ofpbuf *nested)
658 {
659     ofpbuf_use_const(nested, nl_attr_get(nla), nl_attr_get_size(nla));
660 }
661
662 /* Default minimum payload size for each type of attribute. */
663 static size_t
664 min_attr_len(enum nl_attr_type type)
665 {
666     switch (type) {
667     case NL_A_NO_ATTR: return 0;
668     case NL_A_UNSPEC: return 0;
669     case NL_A_U8: return 1;
670     case NL_A_U16: return 2;
671     case NL_A_U32: return 4;
672     case NL_A_U64: return 8;
673     case NL_A_STRING: return 1;
674     case NL_A_FLAG: return 0;
675     case NL_A_IPV6: return 16;
676     case NL_A_NESTED: return 0;
677     case N_NL_ATTR_TYPES: default: OVS_NOT_REACHED();
678     }
679 }
680
681 /* Default maximum payload size for each type of attribute. */
682 static size_t
683 max_attr_len(enum nl_attr_type type)
684 {
685     switch (type) {
686     case NL_A_NO_ATTR: return SIZE_MAX;
687     case NL_A_UNSPEC: return SIZE_MAX;
688     case NL_A_U8: return 1;
689     case NL_A_U16: return 2;
690     case NL_A_U32: return 4;
691     case NL_A_U64: return 8;
692     case NL_A_STRING: return SIZE_MAX;
693     case NL_A_FLAG: return SIZE_MAX;
694     case NL_A_IPV6: return 16;
695     case NL_A_NESTED: return SIZE_MAX;
696     case N_NL_ATTR_TYPES: default: OVS_NOT_REACHED();
697     }
698 }
699
700 bool
701 nl_attr_validate(const struct nlattr *nla, const struct nl_policy *policy)
702 {
703     uint16_t type = nl_attr_type(nla);
704     size_t min_len;
705     size_t max_len;
706     size_t len;
707
708     if (policy->type == NL_A_NO_ATTR) {
709         return true;
710     }
711
712     /* Figure out min and max length. */
713     min_len = policy->min_len;
714     if (!min_len) {
715         min_len = min_attr_len(policy->type);
716     }
717     max_len = policy->max_len;
718     if (!max_len) {
719         max_len = max_attr_len(policy->type);
720     }
721
722     /* Verify length. */
723     len = nl_attr_get_size(nla);
724     if (len < min_len || len > max_len) {
725         VLOG_DBG_RL(&rl, "attr %"PRIu16" length %"PRIuSIZE" not in "
726                     "allowed range %"PRIuSIZE"...%"PRIuSIZE, type, len, min_len, max_len);
727         return false;
728     }
729
730     /* Strings must be null terminated and must not have embedded nulls. */
731     if (policy->type == NL_A_STRING) {
732         if (((char *) nla)[nla->nla_len - 1]) {
733             VLOG_DBG_RL(&rl, "attr %"PRIu16" lacks null at end", type);
734             return false;
735         }
736         if (memchr(nla + 1, '\0', len - 1) != NULL) {
737             VLOG_DBG_RL(&rl, "attr %"PRIu16" has bad length", type);
738             return false;
739         }
740     }
741
742     return true;
743 }
744
745 /* Parses the 'msg' starting at the given 'nla_offset' as a sequence of Netlink
746  * attributes.  'policy[i]', for 0 <= i < n_attrs, specifies how the attribute
747  * with nla_type == i is parsed; a pointer to attribute i is stored in
748  * attrs[i].  Returns true if successful, false on failure.
749  *
750  * If the Netlink attributes in 'msg' follow a Netlink header and a Generic
751  * Netlink header, then 'nla_offset' should be NLMSG_HDRLEN + GENL_HDRLEN. */
752 bool
753 nl_policy_parse(const struct ofpbuf *msg, size_t nla_offset,
754                 const struct nl_policy policy[],
755                 struct nlattr *attrs[], size_t n_attrs)
756 {
757     struct nlattr *nla;
758     size_t left;
759     size_t i;
760
761     memset(attrs, 0, n_attrs * sizeof *attrs);
762
763     if (msg->size < nla_offset) {
764         VLOG_DBG_RL(&rl, "missing headers in nl_policy_parse");
765         return false;
766     }
767
768     NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, nla_offset, 0),
769                       msg->size - nla_offset)
770     {
771         uint16_t type = nl_attr_type(nla);
772         if (type < n_attrs && policy[type].type != NL_A_NO_ATTR) {
773             const struct nl_policy *e = &policy[type];
774             if (!nl_attr_validate(nla, e)) {
775                 return false;
776             }
777             if (attrs[type]) {
778                 VLOG_DBG_RL(&rl, "duplicate attr %"PRIu16, type);
779             }
780             attrs[type] = nla;
781         }
782     }
783     if (left) {
784         VLOG_DBG_RL(&rl, "attributes followed by garbage");
785         return false;
786     }
787
788     for (i = 0; i < n_attrs; i++) {
789         const struct nl_policy *e = &policy[i];
790         if (!e->optional && e->type != NL_A_NO_ATTR && !attrs[i]) {
791             VLOG_DBG_RL(&rl, "required attr %"PRIuSIZE" missing", i);
792             return false;
793         }
794     }
795     return true;
796 }
797
798 /* Parses the Netlink attributes within 'nla'.  'policy[i]', for 0 <= i <
799  * n_attrs, specifies how the attribute with nla_type == i is parsed; a pointer
800  * to attribute i is stored in attrs[i].  Returns true if successful, false on
801  * failure. */
802 bool
803 nl_parse_nested(const struct nlattr *nla, const struct nl_policy policy[],
804                 struct nlattr *attrs[], size_t n_attrs)
805 {
806     struct ofpbuf buf;
807
808     nl_attr_get_nested(nla, &buf);
809     return nl_policy_parse(&buf, 0, policy, attrs, n_attrs);
810 }
811
812 const struct nlattr *
813 nl_attr_find__(const struct nlattr *attrs, size_t size, uint16_t type)
814 {
815     const struct nlattr *nla;
816     size_t left;
817
818     NL_ATTR_FOR_EACH (nla, left, attrs, size) {
819         if (nl_attr_type(nla) == type) {
820             return nla;
821         }
822     }
823     return NULL;
824 }
825
826 /* Returns the first Netlink attribute within 'buf' with the specified 'type',
827  * skipping a header of 'hdr_len' bytes at the beginning of 'buf'.
828  *
829  * This function does not validate the attribute's length. */
830 const struct nlattr *
831 nl_attr_find(const struct ofpbuf *buf, size_t hdr_len, uint16_t type)
832 {
833     return nl_attr_find__(ofpbuf_at(buf, hdr_len, 0), buf->size - hdr_len,
834                           type);
835 }
836
837 /* Returns the first Netlink attribute within 'nla' with the specified
838  * 'type'.
839  *
840  * This function does not validate the attribute's length. */
841 const struct nlattr *
842 nl_attr_find_nested(const struct nlattr *nla, uint16_t type)
843 {
844     return nl_attr_find__(nl_attr_get(nla), nl_attr_get_size(nla), type);
845 }