odp-util: Fix converting masked VLAN from flow.
[sliver-openvswitch.git] / lib / netlink.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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 "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 (%zd bytes < %zd)",
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 = NLMSG_ALIGN(size) - size;
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 = NLMSG_ALIGN(size) - size;
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(NLA_ALIGN(total_size) <= UINT16_MAX);
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 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. */
227 void
228 nl_msg_put_unspec(struct ofpbuf *msg, uint16_t type,
229                   const void *data, size_t size)
230 {
231     memcpy(nl_msg_put_unspec_uninit(msg, type, size), data, size);
232 }
233
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
236  * Boolean flag.) */
237 void
238 nl_msg_put_flag(struct ofpbuf *msg, uint16_t type)
239 {
240     nl_msg_put_unspec(msg, type, NULL, 0);
241 }
242
243 /* Appends a Netlink attribute of the given 'type' and the given 8-bit 'value'
244  * to 'msg'. */
245 void
246 nl_msg_put_u8(struct ofpbuf *msg, uint16_t type, uint8_t value)
247 {
248     nl_msg_put_unspec(msg, type, &value, sizeof value);
249 }
250
251 /* Appends a Netlink attribute of the given 'type' and the given 16-bit host
252  * byte order 'value' to 'msg'. */
253 void
254 nl_msg_put_u16(struct ofpbuf *msg, uint16_t type, uint16_t value)
255 {
256     nl_msg_put_unspec(msg, type, &value, sizeof value);
257 }
258
259 /* Appends a Netlink attribute of the given 'type' and the given 32-bit host
260  * byte order 'value' to 'msg'. */
261 void
262 nl_msg_put_u32(struct ofpbuf *msg, uint16_t type, uint32_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 64-bit host
268  * byte order 'value' to 'msg'. */
269 void
270 nl_msg_put_u64(struct ofpbuf *msg, uint16_t type, uint64_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 16-bit network
276  * byte order 'value' to 'msg'. */
277 void
278 nl_msg_put_be16(struct ofpbuf *msg, uint16_t type, ovs_be16 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 32-bit network
284  * byte order 'value' to 'msg'. */
285 void
286 nl_msg_put_be32(struct ofpbuf *msg, uint16_t type, ovs_be32 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 64-bit network
292  * byte order 'value' to 'msg'. */
293 void
294 nl_msg_put_be64(struct ofpbuf *msg, uint16_t type, ovs_be64 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 odp_port_t
300  * 'value' to 'msg'. */
301 void
302 nl_msg_put_odp_port(struct ofpbuf *msg, uint16_t type, odp_port_t value)
303 {
304     nl_msg_put_u32(msg, type, odp_to_u32(value));
305 }
306
307
308 /* Appends a Netlink attribute of the given 'type' and the given
309  * null-terminated string 'value' to 'msg'. */
310 void
311 nl_msg_put_string(struct ofpbuf *msg, uint16_t type, const char *value)
312 {
313     nl_msg_put_unspec(msg, type, value, strlen(value) + 1);
314 }
315
316 /* Prepends a Netlink attribute of the given 'type' and room for 'size' bytes
317  * of data as its payload, plus Netlink padding if needed, to the head end of
318  * 'msg', reallocating and copying its data if necessary.  Returns a pointer to
319  * the first byte of data in the attribute, which is left uninitialized. */
320 void *
321 nl_msg_push_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size)
322 {
323     size_t total_size = NLA_HDRLEN + size;
324     struct nlattr* nla = nl_msg_push_uninit(msg, total_size);
325     ovs_assert(NLA_ALIGN(total_size) <= UINT16_MAX);
326     nla->nla_len = total_size;
327     nla->nla_type = type;
328     return nla + 1;
329 }
330
331 /* Prepends a Netlink attribute of the given 'type' and the 'size' bytes of
332  * 'data' as its payload, to the head end of 'msg', reallocating and copying
333  * its data if necessary.  Returns a pointer to the first byte of data in the
334  * attribute, which is left uninitialized. */
335 void
336 nl_msg_push_unspec(struct ofpbuf *msg, uint16_t type,
337                   const void *data, size_t size)
338 {
339     memcpy(nl_msg_push_unspec_uninit(msg, type, size), data, size);
340 }
341
342 /* Prepends a Netlink attribute of the given 'type' and no payload to 'msg'.
343  * (Some Netlink protocols use the presence or absence of an attribute as a
344  * Boolean flag.) */
345 void
346 nl_msg_push_flag(struct ofpbuf *msg, uint16_t type)
347 {
348     nl_msg_push_unspec(msg, type, NULL, 0);
349 }
350
351 /* Prepends a Netlink attribute of the given 'type' and the given 8-bit 'value'
352  * to 'msg'. */
353 void
354 nl_msg_push_u8(struct ofpbuf *msg, uint16_t type, uint8_t value)
355 {
356     nl_msg_push_unspec(msg, type, &value, sizeof value);
357 }
358
359 /* Prepends a Netlink attribute of the given 'type' and the given 16-bit host
360  * byte order 'value' to 'msg'. */
361 void
362 nl_msg_push_u16(struct ofpbuf *msg, uint16_t type, uint16_t value)
363 {
364     nl_msg_push_unspec(msg, type, &value, sizeof value);
365 }
366
367 /* Prepends a Netlink attribute of the given 'type' and the given 32-bit host
368  * byte order 'value' to 'msg'. */
369 void
370 nl_msg_push_u32(struct ofpbuf *msg, uint16_t type, uint32_t value)
371 {
372     nl_msg_push_unspec(msg, type, &value, sizeof value);
373 }
374
375 /* Prepends a Netlink attribute of the given 'type' and the given 64-bit host
376  * byte order 'value' to 'msg'. */
377 void
378 nl_msg_push_u64(struct ofpbuf *msg, uint16_t type, uint64_t value)
379 {
380     nl_msg_push_unspec(msg, type, &value, sizeof value);
381 }
382
383 /* Prepends a Netlink attribute of the given 'type' and the given 16-bit
384  * network byte order 'value' to 'msg'. */
385 void
386 nl_msg_push_be16(struct ofpbuf *msg, uint16_t type, ovs_be16 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 32-bit
392  * network byte order 'value' to 'msg'. */
393 void
394 nl_msg_push_be32(struct ofpbuf *msg, uint16_t type, ovs_be32 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 64-bit
400  * network byte order 'value' to 'msg'. */
401 void
402 nl_msg_push_be64(struct ofpbuf *msg, uint16_t type, ovs_be64 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
408  * null-terminated string 'value' to 'msg'. */
409 void
410 nl_msg_push_string(struct ofpbuf *msg, uint16_t type, const char *value)
411 {
412     nl_msg_push_unspec(msg, type, value, strlen(value) + 1);
413 }
414
415 /* Adds the header for nested Netlink attributes to 'msg', with the specified
416  * 'type', and returns the header's offset within 'msg'.  The caller should add
417  * the content for the nested Netlink attribute to 'msg' (e.g. using the other
418  * nl_msg_*() functions), and then pass the returned offset to
419  * nl_msg_end_nested() to finish up the nested attributes. */
420 size_t
421 nl_msg_start_nested(struct ofpbuf *msg, uint16_t type)
422 {
423     size_t offset = msg->size;
424     nl_msg_put_unspec(msg, type, NULL, 0);
425     return offset;
426 }
427
428 /* Finalizes a nested Netlink attribute in 'msg'.  'offset' should be the value
429  * returned by nl_msg_start_nested(). */
430 void
431 nl_msg_end_nested(struct ofpbuf *msg, size_t offset)
432 {
433     struct nlattr *attr = ofpbuf_at_assert(msg, offset, sizeof *attr);
434     attr->nla_len = msg->size - offset;
435 }
436
437 /* Appends a nested Netlink attribute of the given 'type', with the 'size'
438  * bytes of content starting at 'data', to 'msg'. */
439 void
440 nl_msg_put_nested(struct ofpbuf *msg,
441                   uint16_t type, const void *data, size_t size)
442 {
443     size_t offset = nl_msg_start_nested(msg, type);
444     nl_msg_put(msg, data, size);
445     nl_msg_end_nested(msg, offset);
446 }
447
448 /* If 'buffer' begins with a valid "struct nlmsghdr", pulls the header and its
449  * payload off 'buffer', stores header and payload in 'msg->data' and
450  * 'msg->size', and returns a pointer to the header.
451  *
452  * If 'buffer' does not begin with a "struct nlmsghdr" or begins with one that
453  * is invalid, returns NULL without modifying 'buffer'. */
454 struct nlmsghdr *
455 nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg)
456 {
457     if (buffer->size >= sizeof(struct nlmsghdr)) {
458         struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(buffer);
459         size_t len = nlmsghdr->nlmsg_len;
460         if (len >= sizeof *nlmsghdr && len <= buffer->size) {
461             ofpbuf_use_const(msg, nlmsghdr, len);
462             ofpbuf_pull(buffer, len);
463             return nlmsghdr;
464         }
465     }
466
467     msg->data = NULL;
468     msg->size = 0;
469     return NULL;
470 }
471 \f
472 /* Attributes. */
473
474 /* Returns the bits of 'nla->nla_type' that are significant for determining its
475  * type. */
476 int
477 nl_attr_type(const struct nlattr *nla)
478 {
479     return nla->nla_type & NLA_TYPE_MASK;
480 }
481
482 /* Returns the first byte in the payload of attribute 'nla'. */
483 const void *
484 nl_attr_get(const struct nlattr *nla)
485 {
486     ovs_assert(nla->nla_len >= NLA_HDRLEN);
487     return nla + 1;
488 }
489
490 /* Returns the number of bytes in the payload of attribute 'nla'. */
491 size_t
492 nl_attr_get_size(const struct nlattr *nla)
493 {
494     ovs_assert(nla->nla_len >= NLA_HDRLEN);
495     return nla->nla_len - NLA_HDRLEN;
496 }
497
498 /* Asserts that 'nla''s payload is at least 'size' bytes long, and returns the
499  * first byte of the payload. */
500 const void *
501 nl_attr_get_unspec(const struct nlattr *nla, size_t size)
502 {
503     ovs_assert(nla->nla_len >= NLA_HDRLEN + size);
504     return nla + 1;
505 }
506
507 /* Returns true if 'nla' is nonnull.  (Some Netlink protocols use the presence
508  * or absence of an attribute as a Boolean flag.) */
509 bool
510 nl_attr_get_flag(const struct nlattr *nla)
511 {
512     return nla != NULL;
513 }
514
515 #define NL_ATTR_GET_AS(NLA, TYPE) \
516         (*(TYPE*) nl_attr_get_unspec(nla, sizeof(TYPE)))
517
518 /* Returns the 8-bit value in 'nla''s payload.
519  *
520  * Asserts that 'nla''s payload is at least 1 byte long. */
521 uint8_t
522 nl_attr_get_u8(const struct nlattr *nla)
523 {
524     return NL_ATTR_GET_AS(nla, uint8_t);
525 }
526
527 /* Returns the 16-bit host byte order value in 'nla''s payload.
528  *
529  * Asserts that 'nla''s payload is at least 2 bytes long. */
530 uint16_t
531 nl_attr_get_u16(const struct nlattr *nla)
532 {
533     return NL_ATTR_GET_AS(nla, uint16_t);
534 }
535
536 /* Returns the 32-bit host byte order value in 'nla''s payload.
537  *
538  * Asserts that 'nla''s payload is at least 4 bytes long. */
539 uint32_t
540 nl_attr_get_u32(const struct nlattr *nla)
541 {
542     return NL_ATTR_GET_AS(nla, uint32_t);
543 }
544
545 /* Returns the 64-bit host byte order value in 'nla''s payload.
546  *
547  * Asserts that 'nla''s payload is at least 8 bytes long. */
548 uint64_t
549 nl_attr_get_u64(const struct nlattr *nla)
550 {
551     const ovs_32aligned_u64 *x = nl_attr_get_unspec(nla, sizeof *x);
552     return get_32aligned_u64(x);
553 }
554
555 /* Returns the 16-bit network byte order value in 'nla''s payload.
556  *
557  * Asserts that 'nla''s payload is at least 2 bytes long. */
558 ovs_be16
559 nl_attr_get_be16(const struct nlattr *nla)
560 {
561     return NL_ATTR_GET_AS(nla, ovs_be16);
562 }
563
564 /* Returns the 32-bit network byte order value in 'nla''s payload.
565  *
566  * Asserts that 'nla''s payload is at least 4 bytes long. */
567 ovs_be32
568 nl_attr_get_be32(const struct nlattr *nla)
569 {
570     return NL_ATTR_GET_AS(nla, ovs_be32);
571 }
572
573 /* Returns the 64-bit network byte order value in 'nla''s payload.
574  *
575  * Asserts that 'nla''s payload is at least 8 bytes long. */
576 ovs_be64
577 nl_attr_get_be64(const struct nlattr *nla)
578 {
579     const ovs_32aligned_be64 *x = nl_attr_get_unspec(nla, sizeof *x);
580     return get_32aligned_be64(x);
581 }
582
583 /* Returns the 32-bit odp_port_t value in 'nla''s payload.
584  *
585  * Asserts that 'nla''s payload is at least 4 bytes long. */
586 odp_port_t
587 nl_attr_get_odp_port(const struct nlattr *nla)
588 {
589     return u32_to_odp(nl_attr_get_u32(nla));
590 }
591
592 /* Returns the null-terminated string value in 'nla''s payload.
593  *
594  * Asserts that 'nla''s payload contains a null-terminated string. */
595 const char *
596 nl_attr_get_string(const struct nlattr *nla)
597 {
598     ovs_assert(nla->nla_len > NLA_HDRLEN);
599     ovs_assert(memchr(nl_attr_get(nla), '\0', nla->nla_len - NLA_HDRLEN));
600     return nl_attr_get(nla);
601 }
602
603 /* Initializes 'nested' to the payload of 'nla'. */
604 void
605 nl_attr_get_nested(const struct nlattr *nla, struct ofpbuf *nested)
606 {
607     ofpbuf_use_const(nested, nl_attr_get(nla), nl_attr_get_size(nla));
608 }
609
610 /* Default minimum and maximum payload sizes for each type of attribute. */
611 static const size_t attr_len_range[][2] = {
612     [0 ... N_NL_ATTR_TYPES - 1] = { 0, SIZE_MAX },
613     [NL_A_U8] = { 1, 1 },
614     [NL_A_U16] = { 2, 2 },
615     [NL_A_U32] = { 4, 4 },
616     [NL_A_U64] = { 8, 8 },
617     [NL_A_STRING] = { 1, SIZE_MAX },
618     [NL_A_FLAG] = { 0, SIZE_MAX },
619     [NL_A_NESTED] = { 0, SIZE_MAX },
620 };
621
622 bool
623 nl_attr_validate(const struct nlattr *nla, const struct nl_policy *policy)
624 {
625     uint16_t type = nl_attr_type(nla);
626     size_t min_len;
627     size_t max_len;
628     size_t len;
629
630     if (policy->type == NL_A_NO_ATTR) {
631         return true;
632     }
633
634     /* Figure out min and max length. */
635     min_len = policy->min_len;
636     if (!min_len) {
637         min_len = attr_len_range[policy->type][0];
638     }
639     max_len = policy->max_len;
640     if (!max_len) {
641         max_len = attr_len_range[policy->type][1];
642     }
643
644     /* Verify length. */
645     len = nl_attr_get_size(nla);
646     if (len < min_len || len > max_len) {
647         VLOG_DBG_RL(&rl, "attr %"PRIu16" length %zu not in "
648                     "allowed range %zu...%zu", type, len, min_len, max_len);
649         return false;
650     }
651
652     /* Strings must be null terminated and must not have embedded nulls. */
653     if (policy->type == NL_A_STRING) {
654         if (((char *) nla)[nla->nla_len - 1]) {
655             VLOG_DBG_RL(&rl, "attr %"PRIu16" lacks null at end", type);
656             return false;
657         }
658         if (memchr(nla + 1, '\0', len - 1) != NULL) {
659             VLOG_DBG_RL(&rl, "attr %"PRIu16" has bad length", type);
660             return false;
661         }
662     }
663
664     return true;
665 }
666
667 /* Parses the 'msg' starting at the given 'nla_offset' as a sequence of Netlink
668  * attributes.  'policy[i]', for 0 <= i < n_attrs, specifies how the attribute
669  * with nla_type == i is parsed; a pointer to attribute i is stored in
670  * attrs[i].  Returns true if successful, false on failure.
671  *
672  * If the Netlink attributes in 'msg' follow a Netlink header and a Generic
673  * Netlink header, then 'nla_offset' should be NLMSG_HDRLEN + GENL_HDRLEN. */
674 bool
675 nl_policy_parse(const struct ofpbuf *msg, size_t nla_offset,
676                 const struct nl_policy policy[],
677                 struct nlattr *attrs[], size_t n_attrs)
678 {
679     struct nlattr *nla;
680     size_t left;
681     size_t i;
682
683     memset(attrs, 0, n_attrs * sizeof *attrs);
684
685     if (msg->size < nla_offset) {
686         VLOG_DBG_RL(&rl, "missing headers in nl_policy_parse");
687         return false;
688     }
689
690     NL_ATTR_FOR_EACH (nla, left,
691                       (struct nlattr *) ((char *) msg->data + nla_offset),
692                       msg->size - nla_offset)
693     {
694         uint16_t type = nl_attr_type(nla);
695         if (type < n_attrs && policy[type].type != NL_A_NO_ATTR) {
696             const struct nl_policy *e = &policy[type];
697             if (!nl_attr_validate(nla, e)) {
698                 return false;
699             }
700             if (attrs[type]) {
701                 VLOG_DBG_RL(&rl, "duplicate attr %"PRIu16, type);
702             }
703             attrs[type] = nla;
704         }
705     }
706     if (left) {
707         VLOG_DBG_RL(&rl, "attributes followed by garbage");
708         return false;
709     }
710
711     for (i = 0; i < n_attrs; i++) {
712         const struct nl_policy *e = &policy[i];
713         if (!e->optional && e->type != NL_A_NO_ATTR && !attrs[i]) {
714             VLOG_DBG_RL(&rl, "required attr %zu missing", i);
715             return false;
716         }
717     }
718     return true;
719 }
720
721 /* Parses the Netlink attributes within 'nla'.  'policy[i]', for 0 <= i <
722  * n_attrs, specifies how the attribute with nla_type == i is parsed; a pointer
723  * to attribute i is stored in attrs[i].  Returns true if successful, false on
724  * failure. */
725 bool
726 nl_parse_nested(const struct nlattr *nla, const struct nl_policy policy[],
727                 struct nlattr *attrs[], size_t n_attrs)
728 {
729     struct ofpbuf buf;
730
731     nl_attr_get_nested(nla, &buf);
732     return nl_policy_parse(&buf, 0, policy, attrs, n_attrs);
733 }
734
735 const struct nlattr *
736 nl_attr_find__(const struct nlattr *attrs, size_t size, uint16_t type)
737 {
738     const struct nlattr *nla;
739     size_t left;
740
741     NL_ATTR_FOR_EACH (nla, left, attrs, size) {
742         if (nl_attr_type(nla) == type) {
743             return nla;
744         }
745     }
746     return NULL;
747 }
748
749 /* Returns the first Netlink attribute within 'buf' with the specified 'type',
750  * skipping a header of 'hdr_len' bytes at the beginning of 'buf'.
751  *
752  * This function does not validate the attribute's length. */
753 const struct nlattr *
754 nl_attr_find(const struct ofpbuf *buf, size_t hdr_len, uint16_t type)
755 {
756     const uint8_t *start = (const uint8_t *) buf->data + hdr_len;
757     return nl_attr_find__((const struct nlattr *) start, buf->size - hdr_len,
758                           type);
759 }
760
761 /* Returns the first Netlink attribute within 'nla' with the specified
762  * 'type'.
763  *
764  * This function does not validate the attribute's length. */
765 const struct nlattr *
766 nl_attr_find_nested(const struct nlattr *nla, uint16_t type)
767 {
768     return nl_attr_find__(nl_attr_get(nla), nl_attr_get_size(nla), type);
769 }