]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
e0edde6f | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
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: | |
064af421 | 7 | * |
a14bc59f BP |
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. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
18 | #include "netlink.h" | |
064af421 BP |
19 | #include <errno.h> |
20 | #include <inttypes.h> | |
2fe27d5a | 21 | #include <sys/types.h> |
064af421 BP |
22 | #include <unistd.h> |
23 | #include "coverage.h" | |
064af421 BP |
24 | #include "netlink-protocol.h" |
25 | #include "ofpbuf.h" | |
064af421 | 26 | #include "timeval.h" |
34c7bb50 | 27 | #include "unaligned.h" |
064af421 | 28 | #include "vlog.h" |
5136ce49 | 29 | |
d98e6007 | 30 | VLOG_DEFINE_THIS_MODULE(netlink); |
064af421 | 31 | |
064af421 BP |
32 | /* A single (bad) Netlink message can in theory dump out many, many log |
33 | * messages, so the burst size is set quite high here to avoid missing useful | |
34 | * information. Also, at high logging levels we log *all* Netlink messages. */ | |
35 | static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 600); | |
36 | ||
064af421 BP |
37 | /* Returns the nlmsghdr at the head of 'msg'. |
38 | * | |
39 | * 'msg' must be at least as large as a nlmsghdr. */ | |
40 | struct nlmsghdr * | |
d295e8e9 | 41 | nl_msg_nlmsghdr(const struct ofpbuf *msg) |
064af421 BP |
42 | { |
43 | return ofpbuf_at_assert(msg, 0, NLMSG_HDRLEN); | |
44 | } | |
45 | ||
46 | /* Returns the genlmsghdr just past 'msg''s nlmsghdr. | |
47 | * | |
48 | * Returns a null pointer if 'msg' is not large enough to contain an nlmsghdr | |
49 | * and a genlmsghdr. */ | |
50 | struct genlmsghdr * | |
d295e8e9 | 51 | nl_msg_genlmsghdr(const struct ofpbuf *msg) |
064af421 BP |
52 | { |
53 | return ofpbuf_at(msg, NLMSG_HDRLEN, GENL_HDRLEN); | |
54 | } | |
55 | ||
56 | /* If 'buffer' is a NLMSG_ERROR message, stores 0 in '*errorp' if it is an ACK | |
57 | * message, otherwise a positive errno value, and returns true. If 'buffer' is | |
58 | * not an NLMSG_ERROR message, returns false. | |
59 | * | |
60 | * 'msg' must be at least as large as a nlmsghdr. */ | |
61 | bool | |
d295e8e9 | 62 | nl_msg_nlmsgerr(const struct ofpbuf *msg, int *errorp) |
064af421 BP |
63 | { |
64 | if (nl_msg_nlmsghdr(msg)->nlmsg_type == NLMSG_ERROR) { | |
65 | struct nlmsgerr *err = ofpbuf_at(msg, NLMSG_HDRLEN, sizeof *err); | |
66 | int code = EPROTO; | |
67 | if (!err) { | |
68 | VLOG_ERR_RL(&rl, "received invalid nlmsgerr (%zd bytes < %zd)", | |
69 | msg->size, NLMSG_HDRLEN + sizeof *err); | |
70 | } else if (err->error <= 0 && err->error > INT_MIN) { | |
71 | code = -err->error; | |
72 | } | |
73 | if (errorp) { | |
74 | *errorp = code; | |
75 | } | |
76 | return true; | |
77 | } else { | |
78 | return false; | |
79 | } | |
80 | } | |
81 | ||
82 | /* Ensures that 'b' has room for at least 'size' bytes plus netlink padding at | |
83 | * its tail end, reallocating and copying its data if necessary. */ | |
84 | void | |
d295e8e9 | 85 | nl_msg_reserve(struct ofpbuf *msg, size_t size) |
064af421 BP |
86 | { |
87 | ofpbuf_prealloc_tailroom(msg, NLMSG_ALIGN(size)); | |
88 | } | |
89 | ||
90 | /* Puts a nlmsghdr at the beginning of 'msg', which must be initially empty. | |
69123704 | 91 | * Uses the given 'type' and 'flags'. 'expected_payload' should be |
064af421 BP |
92 | * an estimate of the number of payload bytes to be supplied; if the size of |
93 | * the payload is unknown a value of 0 is acceptable. | |
94 | * | |
95 | * 'type' is ordinarily an enumerated value specific to the Netlink protocol | |
96 | * (e.g. RTM_NEWLINK, for NETLINK_ROUTE protocol). For Generic Netlink, 'type' | |
97 | * is the family number obtained via nl_lookup_genl_family(). | |
98 | * | |
99 | * 'flags' is a bit-mask that indicates what kind of request is being made. It | |
100 | * is often NLM_F_REQUEST indicating that a request is being made, commonly | |
101 | * or'd with NLM_F_ACK to request an acknowledgement. | |
102 | * | |
7d7447df BP |
103 | * Sets the new nlmsghdr's nlmsg_len, nlmsg_seq, and nlmsg_pid fields to 0 for |
104 | * now. Functions that send Netlink messages will fill these in just before | |
105 | * sending the message. | |
69123704 BP |
106 | * |
107 | * nl_msg_put_genlmsghdr() is more convenient for composing a Generic Netlink | |
064af421 BP |
108 | * message. */ |
109 | void | |
69123704 | 110 | nl_msg_put_nlmsghdr(struct ofpbuf *msg, |
d295e8e9 | 111 | size_t expected_payload, uint32_t type, uint32_t flags) |
064af421 BP |
112 | { |
113 | struct nlmsghdr *nlmsghdr; | |
114 | ||
cb22974d | 115 | ovs_assert(msg->size == 0); |
064af421 BP |
116 | |
117 | nl_msg_reserve(msg, NLMSG_HDRLEN + expected_payload); | |
118 | nlmsghdr = nl_msg_put_uninit(msg, NLMSG_HDRLEN); | |
119 | nlmsghdr->nlmsg_len = 0; | |
120 | nlmsghdr->nlmsg_type = type; | |
121 | nlmsghdr->nlmsg_flags = flags; | |
7d7447df | 122 | nlmsghdr->nlmsg_seq = 0; |
69123704 | 123 | nlmsghdr->nlmsg_pid = 0; |
064af421 BP |
124 | } |
125 | ||
126 | /* Puts a nlmsghdr and genlmsghdr at the beginning of 'msg', which must be | |
69123704 BP |
127 | * initially empty. 'expected_payload' should be an estimate of the number of |
128 | * payload bytes to be supplied; if the size of the payload is unknown a value | |
129 | * of 0 is acceptable. | |
064af421 BP |
130 | * |
131 | * 'family' is the family number obtained via nl_lookup_genl_family(). | |
132 | * | |
133 | * 'flags' is a bit-mask that indicates what kind of request is being made. It | |
134 | * is often NLM_F_REQUEST indicating that a request is being made, commonly | |
135 | * or'd with NLM_F_ACK to request an acknowledgement. | |
136 | * | |
137 | * 'cmd' is an enumerated value specific to the Generic Netlink family | |
138 | * (e.g. CTRL_CMD_NEWFAMILY for the GENL_ID_CTRL family). | |
139 | * | |
140 | * 'version' is a version number specific to the family and command (often 1). | |
141 | * | |
69123704 BP |
142 | * Sets the new nlmsghdr's nlmsg_pid field to 0 for now. nl_sock_send() will |
143 | * fill it in just before sending the message. | |
144 | * | |
145 | * nl_msg_put_nlmsghdr() should be used to compose Netlink messages that are | |
146 | * not Generic Netlink messages. */ | |
064af421 | 147 | void |
69123704 BP |
148 | nl_msg_put_genlmsghdr(struct ofpbuf *msg, size_t expected_payload, |
149 | int family, uint32_t flags, uint8_t cmd, uint8_t version) | |
064af421 BP |
150 | { |
151 | struct genlmsghdr *genlmsghdr; | |
152 | ||
69123704 | 153 | nl_msg_put_nlmsghdr(msg, GENL_HDRLEN + expected_payload, family, flags); |
cb22974d | 154 | ovs_assert(msg->size == NLMSG_HDRLEN); |
064af421 BP |
155 | genlmsghdr = nl_msg_put_uninit(msg, GENL_HDRLEN); |
156 | genlmsghdr->cmd = cmd; | |
157 | genlmsghdr->version = version; | |
158 | genlmsghdr->reserved = 0; | |
159 | } | |
160 | ||
161 | /* Appends the 'size' bytes of data in 'p', plus Netlink padding if needed, to | |
162 | * the tail end of 'msg'. Data in 'msg' is reallocated and copied if | |
163 | * necessary. */ | |
164 | void | |
d295e8e9 | 165 | nl_msg_put(struct ofpbuf *msg, const void *data, size_t size) |
064af421 BP |
166 | { |
167 | memcpy(nl_msg_put_uninit(msg, size), data, size); | |
168 | } | |
169 | ||
170 | /* Appends 'size' bytes of data, plus Netlink padding if needed, to the tail | |
171 | * end of 'msg', reallocating and copying its data if necessary. Returns a | |
172 | * pointer to the first byte of the new data, which is left uninitialized. */ | |
173 | void * | |
d295e8e9 | 174 | nl_msg_put_uninit(struct ofpbuf *msg, size_t size) |
064af421 BP |
175 | { |
176 | size_t pad = NLMSG_ALIGN(size) - size; | |
177 | char *p = ofpbuf_put_uninit(msg, size + pad); | |
178 | if (pad) { | |
d295e8e9 | 179 | memset(p + size, 0, pad); |
064af421 BP |
180 | } |
181 | return p; | |
182 | } | |
183 | ||
46d34fef BP |
184 | /* Prepends the 'size' bytes of data in 'p', plus Netlink padding if needed, to |
185 | * the head end of 'msg'. Data in 'msg' is reallocated and copied if | |
186 | * necessary. */ | |
187 | void | |
188 | nl_msg_push(struct ofpbuf *msg, const void *data, size_t size) | |
189 | { | |
190 | memcpy(nl_msg_push_uninit(msg, size), data, size); | |
191 | } | |
192 | ||
193 | /* Prepends 'size' bytes of data, plus Netlink padding if needed, to the head | |
194 | * end of 'msg', reallocating and copying its data if necessary. Returns a | |
195 | * pointer to the first byte of the new data, which is left uninitialized. */ | |
196 | void * | |
197 | nl_msg_push_uninit(struct ofpbuf *msg, size_t size) | |
198 | { | |
199 | size_t pad = NLMSG_ALIGN(size) - size; | |
200 | char *p = ofpbuf_push_uninit(msg, size + pad); | |
201 | if (pad) { | |
202 | memset(p + size, 0, pad); | |
203 | } | |
204 | return p; | |
205 | } | |
206 | ||
064af421 BP |
207 | /* Appends a Netlink attribute of the given 'type' and room for 'size' bytes of |
208 | * data as its payload, plus Netlink padding if needed, to the tail end of | |
209 | * 'msg', reallocating and copying its data if necessary. Returns a pointer to | |
210 | * the first byte of data in the attribute, which is left uninitialized. */ | |
211 | void * | |
d295e8e9 | 212 | nl_msg_put_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size) |
064af421 BP |
213 | { |
214 | size_t total_size = NLA_HDRLEN + size; | |
215 | struct nlattr* nla = nl_msg_put_uninit(msg, total_size); | |
cb22974d | 216 | ovs_assert(NLA_ALIGN(total_size) <= UINT16_MAX); |
064af421 BP |
217 | nla->nla_len = total_size; |
218 | nla->nla_type = type; | |
219 | return nla + 1; | |
220 | } | |
221 | ||
222 | /* Appends a Netlink attribute of the given 'type' and the 'size' bytes of | |
223 | * 'data' as its payload, to the tail end of 'msg', reallocating and copying | |
224 | * its data if necessary. Returns a pointer to the first byte of data in the | |
225 | * attribute, which is left uninitialized. */ | |
226 | void | |
227 | nl_msg_put_unspec(struct ofpbuf *msg, uint16_t type, | |
d295e8e9 | 228 | const void *data, size_t size) |
064af421 BP |
229 | { |
230 | memcpy(nl_msg_put_unspec_uninit(msg, type, size), data, size); | |
231 | } | |
232 | ||
233 | /* Appends a Netlink attribute of the given 'type' and no payload to 'msg'. | |
234 | * (Some Netlink protocols use the presence or absence of an attribute as a | |
235 | * Boolean flag.) */ | |
236 | void | |
d295e8e9 | 237 | nl_msg_put_flag(struct ofpbuf *msg, uint16_t type) |
064af421 BP |
238 | { |
239 | nl_msg_put_unspec(msg, type, NULL, 0); | |
240 | } | |
241 | ||
242 | /* Appends a Netlink attribute of the given 'type' and the given 8-bit 'value' | |
243 | * to 'msg'. */ | |
244 | void | |
d295e8e9 | 245 | nl_msg_put_u8(struct ofpbuf *msg, uint16_t type, uint8_t value) |
064af421 BP |
246 | { |
247 | nl_msg_put_unspec(msg, type, &value, sizeof value); | |
248 | } | |
249 | ||
ee913f98 BP |
250 | /* Appends a Netlink attribute of the given 'type' and the given 16-bit host |
251 | * byte order 'value' to 'msg'. */ | |
064af421 BP |
252 | void |
253 | nl_msg_put_u16(struct ofpbuf *msg, uint16_t type, uint16_t value) | |
254 | { | |
255 | nl_msg_put_unspec(msg, type, &value, sizeof value); | |
256 | } | |
257 | ||
ee913f98 BP |
258 | /* Appends a Netlink attribute of the given 'type' and the given 32-bit host |
259 | * byte order 'value' to 'msg'. */ | |
064af421 BP |
260 | void |
261 | nl_msg_put_u32(struct ofpbuf *msg, uint16_t type, uint32_t value) | |
262 | { | |
263 | nl_msg_put_unspec(msg, type, &value, sizeof value); | |
264 | } | |
265 | ||
ee913f98 BP |
266 | /* Appends a Netlink attribute of the given 'type' and the given 64-bit host |
267 | * byte order 'value' to 'msg'. */ | |
064af421 BP |
268 | void |
269 | nl_msg_put_u64(struct ofpbuf *msg, uint16_t type, uint64_t value) | |
270 | { | |
271 | nl_msg_put_unspec(msg, type, &value, sizeof value); | |
272 | } | |
273 | ||
ee913f98 BP |
274 | /* Appends a Netlink attribute of the given 'type' and the given 16-bit network |
275 | * byte order 'value' to 'msg'. */ | |
276 | void | |
277 | nl_msg_put_be16(struct ofpbuf *msg, uint16_t type, ovs_be16 value) | |
278 | { | |
279 | nl_msg_put_unspec(msg, type, &value, sizeof value); | |
280 | } | |
281 | ||
282 | /* Appends a Netlink attribute of the given 'type' and the given 32-bit network | |
283 | * byte order 'value' to 'msg'. */ | |
284 | void | |
285 | nl_msg_put_be32(struct ofpbuf *msg, uint16_t type, ovs_be32 value) | |
286 | { | |
287 | nl_msg_put_unspec(msg, type, &value, sizeof value); | |
288 | } | |
289 | ||
290 | /* Appends a Netlink attribute of the given 'type' and the given 64-bit network | |
291 | * byte order 'value' to 'msg'. */ | |
292 | void | |
293 | nl_msg_put_be64(struct ofpbuf *msg, uint16_t type, ovs_be64 value) | |
294 | { | |
295 | nl_msg_put_unspec(msg, type, &value, sizeof value); | |
296 | } | |
297 | ||
064af421 BP |
298 | /* Appends a Netlink attribute of the given 'type' and the given |
299 | * null-terminated string 'value' to 'msg'. */ | |
300 | void | |
301 | nl_msg_put_string(struct ofpbuf *msg, uint16_t type, const char *value) | |
302 | { | |
303 | nl_msg_put_unspec(msg, type, value, strlen(value) + 1); | |
304 | } | |
305 | ||
46d34fef BP |
306 | /* Prepends a Netlink attribute of the given 'type' and room for 'size' bytes |
307 | * of data as its payload, plus Netlink padding if needed, to the head end of | |
308 | * 'msg', reallocating and copying its data if necessary. Returns a pointer to | |
309 | * the first byte of data in the attribute, which is left uninitialized. */ | |
310 | void * | |
311 | nl_msg_push_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size) | |
312 | { | |
313 | size_t total_size = NLA_HDRLEN + size; | |
314 | struct nlattr* nla = nl_msg_push_uninit(msg, total_size); | |
cb22974d | 315 | ovs_assert(NLA_ALIGN(total_size) <= UINT16_MAX); |
46d34fef BP |
316 | nla->nla_len = total_size; |
317 | nla->nla_type = type; | |
318 | return nla + 1; | |
319 | } | |
320 | ||
321 | /* Prepends a Netlink attribute of the given 'type' and the 'size' bytes of | |
322 | * 'data' as its payload, to the head end of 'msg', reallocating and copying | |
323 | * its data if necessary. Returns a pointer to the first byte of data in the | |
324 | * attribute, which is left uninitialized. */ | |
325 | void | |
326 | nl_msg_push_unspec(struct ofpbuf *msg, uint16_t type, | |
327 | const void *data, size_t size) | |
328 | { | |
329 | memcpy(nl_msg_push_unspec_uninit(msg, type, size), data, size); | |
330 | } | |
331 | ||
332 | /* Prepends a Netlink attribute of the given 'type' and no payload to 'msg'. | |
333 | * (Some Netlink protocols use the presence or absence of an attribute as a | |
334 | * Boolean flag.) */ | |
335 | void | |
336 | nl_msg_push_flag(struct ofpbuf *msg, uint16_t type) | |
337 | { | |
338 | nl_msg_push_unspec(msg, type, NULL, 0); | |
339 | } | |
340 | ||
341 | /* Prepends a Netlink attribute of the given 'type' and the given 8-bit 'value' | |
342 | * to 'msg'. */ | |
343 | void | |
344 | nl_msg_push_u8(struct ofpbuf *msg, uint16_t type, uint8_t value) | |
345 | { | |
346 | nl_msg_push_unspec(msg, type, &value, sizeof value); | |
347 | } | |
348 | ||
349 | /* Prepends a Netlink attribute of the given 'type' and the given 16-bit host | |
350 | * byte order 'value' to 'msg'. */ | |
351 | void | |
352 | nl_msg_push_u16(struct ofpbuf *msg, uint16_t type, uint16_t value) | |
353 | { | |
354 | nl_msg_push_unspec(msg, type, &value, sizeof value); | |
355 | } | |
356 | ||
357 | /* Prepends a Netlink attribute of the given 'type' and the given 32-bit host | |
358 | * byte order 'value' to 'msg'. */ | |
359 | void | |
360 | nl_msg_push_u32(struct ofpbuf *msg, uint16_t type, uint32_t value) | |
361 | { | |
362 | nl_msg_push_unspec(msg, type, &value, sizeof value); | |
363 | } | |
364 | ||
365 | /* Prepends a Netlink attribute of the given 'type' and the given 64-bit host | |
366 | * byte order 'value' to 'msg'. */ | |
367 | void | |
368 | nl_msg_push_u64(struct ofpbuf *msg, uint16_t type, uint64_t value) | |
369 | { | |
370 | nl_msg_push_unspec(msg, type, &value, sizeof value); | |
371 | } | |
372 | ||
373 | /* Prepends a Netlink attribute of the given 'type' and the given 16-bit | |
374 | * network byte order 'value' to 'msg'. */ | |
375 | void | |
376 | nl_msg_push_be16(struct ofpbuf *msg, uint16_t type, ovs_be16 value) | |
377 | { | |
378 | nl_msg_push_unspec(msg, type, &value, sizeof value); | |
379 | } | |
380 | ||
381 | /* Prepends a Netlink attribute of the given 'type' and the given 32-bit | |
382 | * network byte order 'value' to 'msg'. */ | |
383 | void | |
384 | nl_msg_push_be32(struct ofpbuf *msg, uint16_t type, ovs_be32 value) | |
385 | { | |
386 | nl_msg_push_unspec(msg, type, &value, sizeof value); | |
387 | } | |
388 | ||
389 | /* Prepends a Netlink attribute of the given 'type' and the given 64-bit | |
390 | * network byte order 'value' to 'msg'. */ | |
391 | void | |
392 | nl_msg_push_be64(struct ofpbuf *msg, uint16_t type, ovs_be64 value) | |
393 | { | |
394 | nl_msg_push_unspec(msg, type, &value, sizeof value); | |
395 | } | |
396 | ||
397 | /* Prepends a Netlink attribute of the given 'type' and the given | |
398 | * null-terminated string 'value' to 'msg'. */ | |
399 | void | |
400 | nl_msg_push_string(struct ofpbuf *msg, uint16_t type, const char *value) | |
401 | { | |
402 | nl_msg_push_unspec(msg, type, value, strlen(value) + 1); | |
403 | } | |
404 | ||
38a99756 BP |
405 | /* Adds the header for nested Netlink attributes to 'msg', with the specified |
406 | * 'type', and returns the header's offset within 'msg'. The caller should add | |
407 | * the content for the nested Netlink attribute to 'msg' (e.g. using the other | |
408 | * nl_msg_*() functions), and then pass the returned offset to | |
409 | * nl_msg_end_nested() to finish up the nested attributes. */ | |
410 | size_t | |
411 | nl_msg_start_nested(struct ofpbuf *msg, uint16_t type) | |
412 | { | |
413 | size_t offset = msg->size; | |
414 | nl_msg_put_unspec(msg, type, NULL, 0); | |
415 | return offset; | |
416 | } | |
417 | ||
418 | /* Finalizes a nested Netlink attribute in 'msg'. 'offset' should be the value | |
419 | * returned by nl_msg_start_nested(). */ | |
420 | void | |
421 | nl_msg_end_nested(struct ofpbuf *msg, size_t offset) | |
422 | { | |
423 | struct nlattr *attr = ofpbuf_at_assert(msg, offset, sizeof *attr); | |
424 | attr->nla_len = msg->size - offset; | |
425 | } | |
426 | ||
427 | /* Appends a nested Netlink attribute of the given 'type', with the 'size' | |
428 | * bytes of content starting at 'data', to 'msg'. */ | |
064af421 BP |
429 | void |
430 | nl_msg_put_nested(struct ofpbuf *msg, | |
38a99756 | 431 | uint16_t type, const void *data, size_t size) |
064af421 | 432 | { |
38a99756 BP |
433 | size_t offset = nl_msg_start_nested(msg, type); |
434 | nl_msg_put(msg, data, size); | |
435 | nl_msg_end_nested(msg, offset); | |
064af421 BP |
436 | } |
437 | ||
974d6a6d BP |
438 | /* If 'buffer' begins with a valid "struct nlmsghdr", pulls the header and its |
439 | * payload off 'buffer', stores header and payload in 'msg->data' and | |
440 | * 'msg->size', and returns a pointer to the header. | |
441 | * | |
442 | * If 'buffer' does not begin with a "struct nlmsghdr" or begins with one that | |
443 | * is invalid, returns NULL without modifying 'buffer'. */ | |
444 | struct nlmsghdr * | |
445 | nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg) | |
446 | { | |
447 | if (buffer->size >= sizeof(struct nlmsghdr)) { | |
448 | struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(buffer); | |
449 | size_t len = nlmsghdr->nlmsg_len; | |
450 | if (len >= sizeof *nlmsghdr && len <= buffer->size) { | |
0bc9407d | 451 | ofpbuf_use_const(msg, nlmsghdr, len); |
974d6a6d BP |
452 | ofpbuf_pull(buffer, len); |
453 | return nlmsghdr; | |
454 | } | |
455 | } | |
456 | ||
457 | msg->data = NULL; | |
458 | msg->size = 0; | |
459 | return NULL; | |
460 | } | |
461 | \f | |
462 | /* Attributes. */ | |
463 | ||
7c624478 BP |
464 | /* Returns the bits of 'nla->nla_type' that are significant for determining its |
465 | * type. */ | |
466 | int | |
467 | nl_attr_type(const struct nlattr *nla) | |
468 | { | |
469 | return nla->nla_type & NLA_TYPE_MASK; | |
470 | } | |
471 | ||
064af421 BP |
472 | /* Returns the first byte in the payload of attribute 'nla'. */ |
473 | const void * | |
d295e8e9 | 474 | nl_attr_get(const struct nlattr *nla) |
064af421 | 475 | { |
cb22974d | 476 | ovs_assert(nla->nla_len >= NLA_HDRLEN); |
064af421 BP |
477 | return nla + 1; |
478 | } | |
479 | ||
480 | /* Returns the number of bytes in the payload of attribute 'nla'. */ | |
481 | size_t | |
d295e8e9 | 482 | nl_attr_get_size(const struct nlattr *nla) |
064af421 | 483 | { |
cb22974d | 484 | ovs_assert(nla->nla_len >= NLA_HDRLEN); |
064af421 BP |
485 | return nla->nla_len - NLA_HDRLEN; |
486 | } | |
487 | ||
488 | /* Asserts that 'nla''s payload is at least 'size' bytes long, and returns the | |
489 | * first byte of the payload. */ | |
490 | const void * | |
d295e8e9 | 491 | nl_attr_get_unspec(const struct nlattr *nla, size_t size) |
064af421 | 492 | { |
cb22974d | 493 | ovs_assert(nla->nla_len >= NLA_HDRLEN + size); |
064af421 BP |
494 | return nla + 1; |
495 | } | |
496 | ||
497 | /* Returns true if 'nla' is nonnull. (Some Netlink protocols use the presence | |
498 | * or absence of an attribute as a Boolean flag.) */ | |
499 | bool | |
d295e8e9 | 500 | nl_attr_get_flag(const struct nlattr *nla) |
064af421 BP |
501 | { |
502 | return nla != NULL; | |
503 | } | |
504 | ||
505 | #define NL_ATTR_GET_AS(NLA, TYPE) \ | |
506 | (*(TYPE*) nl_attr_get_unspec(nla, sizeof(TYPE))) | |
507 | ||
508 | /* Returns the 8-bit value in 'nla''s payload. | |
509 | * | |
510 | * Asserts that 'nla''s payload is at least 1 byte long. */ | |
511 | uint8_t | |
d295e8e9 | 512 | nl_attr_get_u8(const struct nlattr *nla) |
064af421 BP |
513 | { |
514 | return NL_ATTR_GET_AS(nla, uint8_t); | |
515 | } | |
516 | ||
ee913f98 | 517 | /* Returns the 16-bit host byte order value in 'nla''s payload. |
064af421 BP |
518 | * |
519 | * Asserts that 'nla''s payload is at least 2 bytes long. */ | |
520 | uint16_t | |
d295e8e9 | 521 | nl_attr_get_u16(const struct nlattr *nla) |
064af421 BP |
522 | { |
523 | return NL_ATTR_GET_AS(nla, uint16_t); | |
524 | } | |
525 | ||
ee913f98 | 526 | /* Returns the 32-bit host byte order value in 'nla''s payload. |
064af421 BP |
527 | * |
528 | * Asserts that 'nla''s payload is at least 4 bytes long. */ | |
529 | uint32_t | |
d295e8e9 | 530 | nl_attr_get_u32(const struct nlattr *nla) |
064af421 BP |
531 | { |
532 | return NL_ATTR_GET_AS(nla, uint32_t); | |
533 | } | |
534 | ||
ee913f98 | 535 | /* Returns the 64-bit host byte order value in 'nla''s payload. |
064af421 BP |
536 | * |
537 | * Asserts that 'nla''s payload is at least 8 bytes long. */ | |
538 | uint64_t | |
d295e8e9 | 539 | nl_attr_get_u64(const struct nlattr *nla) |
064af421 | 540 | { |
34c7bb50 BP |
541 | const ovs_32aligned_u64 *x = nl_attr_get_unspec(nla, sizeof *x); |
542 | return get_32aligned_u64(x); | |
064af421 BP |
543 | } |
544 | ||
ee913f98 BP |
545 | /* Returns the 16-bit network byte order value in 'nla''s payload. |
546 | * | |
547 | * Asserts that 'nla''s payload is at least 2 bytes long. */ | |
548 | ovs_be16 | |
549 | nl_attr_get_be16(const struct nlattr *nla) | |
550 | { | |
551 | return NL_ATTR_GET_AS(nla, ovs_be16); | |
552 | } | |
553 | ||
554 | /* Returns the 32-bit network byte order value in 'nla''s payload. | |
555 | * | |
556 | * Asserts that 'nla''s payload is at least 4 bytes long. */ | |
557 | ovs_be32 | |
558 | nl_attr_get_be32(const struct nlattr *nla) | |
559 | { | |
560 | return NL_ATTR_GET_AS(nla, ovs_be32); | |
561 | } | |
562 | ||
563 | /* Returns the 64-bit network byte order value in 'nla''s payload. | |
564 | * | |
565 | * Asserts that 'nla''s payload is at least 8 bytes long. */ | |
566 | ovs_be64 | |
567 | nl_attr_get_be64(const struct nlattr *nla) | |
568 | { | |
34c7bb50 BP |
569 | const ovs_32aligned_be64 *x = nl_attr_get_unspec(nla, sizeof *x); |
570 | return get_32aligned_be64(x); | |
ee913f98 BP |
571 | } |
572 | ||
064af421 BP |
573 | /* Returns the null-terminated string value in 'nla''s payload. |
574 | * | |
575 | * Asserts that 'nla''s payload contains a null-terminated string. */ | |
576 | const char * | |
d295e8e9 | 577 | nl_attr_get_string(const struct nlattr *nla) |
064af421 | 578 | { |
cb22974d BP |
579 | ovs_assert(nla->nla_len > NLA_HDRLEN); |
580 | ovs_assert(memchr(nl_attr_get(nla), '\0', nla->nla_len - NLA_HDRLEN)); | |
064af421 BP |
581 | return nl_attr_get(nla); |
582 | } | |
583 | ||
0bc9407d | 584 | /* Initializes 'nested' to the payload of 'nla'. */ |
25eeae6a BP |
585 | void |
586 | nl_attr_get_nested(const struct nlattr *nla, struct ofpbuf *nested) | |
587 | { | |
0bc9407d | 588 | ofpbuf_use_const(nested, nl_attr_get(nla), nl_attr_get_size(nla)); |
25eeae6a BP |
589 | } |
590 | ||
064af421 BP |
591 | /* Default minimum and maximum payload sizes for each type of attribute. */ |
592 | static const size_t attr_len_range[][2] = { | |
593 | [0 ... N_NL_ATTR_TYPES - 1] = { 0, SIZE_MAX }, | |
594 | [NL_A_U8] = { 1, 1 }, | |
595 | [NL_A_U16] = { 2, 2 }, | |
596 | [NL_A_U32] = { 4, 4 }, | |
597 | [NL_A_U64] = { 8, 8 }, | |
598 | [NL_A_STRING] = { 1, SIZE_MAX }, | |
599 | [NL_A_FLAG] = { 0, SIZE_MAX }, | |
b4d73e97 | 600 | [NL_A_NESTED] = { 0, SIZE_MAX }, |
064af421 BP |
601 | }; |
602 | ||
be3330a6 BP |
603 | bool |
604 | nl_attr_validate(const struct nlattr *nla, const struct nl_policy *policy) | |
605 | { | |
606 | uint16_t type = nl_attr_type(nla); | |
607 | size_t min_len; | |
608 | size_t max_len; | |
609 | size_t len; | |
610 | ||
611 | if (policy->type == NL_A_NO_ATTR) { | |
612 | return true; | |
613 | } | |
614 | ||
615 | /* Figure out min and max length. */ | |
616 | min_len = policy->min_len; | |
617 | if (!min_len) { | |
618 | min_len = attr_len_range[policy->type][0]; | |
619 | } | |
620 | max_len = policy->max_len; | |
621 | if (!max_len) { | |
622 | max_len = attr_len_range[policy->type][1]; | |
623 | } | |
624 | ||
625 | /* Verify length. */ | |
626 | len = nl_attr_get_size(nla); | |
627 | if (len < min_len || len > max_len) { | |
628 | VLOG_DBG_RL(&rl, "attr %"PRIu16" length %zu not in " | |
629 | "allowed range %zu...%zu", type, len, min_len, max_len); | |
630 | return false; | |
631 | } | |
632 | ||
633 | /* Strings must be null terminated and must not have embedded nulls. */ | |
634 | if (policy->type == NL_A_STRING) { | |
635 | if (((char *) nla)[nla->nla_len - 1]) { | |
636 | VLOG_DBG_RL(&rl, "attr %"PRIu16" lacks null at end", type); | |
637 | return false; | |
638 | } | |
639 | if (memchr(nla + 1, '\0', len - 1) != NULL) { | |
640 | VLOG_DBG_RL(&rl, "attr %"PRIu16" has bad length", type); | |
641 | return false; | |
642 | } | |
643 | } | |
644 | ||
645 | return true; | |
646 | } | |
647 | ||
064af421 BP |
648 | /* Parses the 'msg' starting at the given 'nla_offset' as a sequence of Netlink |
649 | * attributes. 'policy[i]', for 0 <= i < n_attrs, specifies how the attribute | |
650 | * with nla_type == i is parsed; a pointer to attribute i is stored in | |
651 | * attrs[i]. Returns true if successful, false on failure. | |
652 | * | |
653 | * If the Netlink attributes in 'msg' follow a Netlink header and a Generic | |
654 | * Netlink header, then 'nla_offset' should be NLMSG_HDRLEN + GENL_HDRLEN. */ | |
655 | bool | |
656 | nl_policy_parse(const struct ofpbuf *msg, size_t nla_offset, | |
657 | const struct nl_policy policy[], | |
658 | struct nlattr *attrs[], size_t n_attrs) | |
659 | { | |
506564b8 | 660 | struct nlattr *nla; |
506564b8 | 661 | size_t left; |
064af421 BP |
662 | size_t i; |
663 | ||
be3330a6 | 664 | memset(attrs, 0, n_attrs * sizeof *attrs); |
064af421 | 665 | |
506564b8 | 666 | if (msg->size < nla_offset) { |
064af421 BP |
667 | VLOG_DBG_RL(&rl, "missing headers in nl_policy_parse"); |
668 | return false; | |
669 | } | |
064af421 | 670 | |
506564b8 BP |
671 | NL_ATTR_FOR_EACH (nla, left, |
672 | (struct nlattr *) ((char *) msg->data + nla_offset), | |
be3330a6 BP |
673 | msg->size - nla_offset) |
674 | { | |
506564b8 | 675 | uint16_t type = nl_attr_type(nla); |
064af421 | 676 | if (type < n_attrs && policy[type].type != NL_A_NO_ATTR) { |
2a022368 | 677 | const struct nl_policy *e = &policy[type]; |
be3330a6 | 678 | if (!nl_attr_validate(nla, e)) { |
064af421 BP |
679 | return false; |
680 | } | |
33cada0c | 681 | if (attrs[type]) { |
be3330a6 | 682 | VLOG_DBG_RL(&rl, "duplicate attr %"PRIu16, type); |
33cada0c | 683 | } |
064af421 | 684 | attrs[type] = nla; |
064af421 | 685 | } |
506564b8 BP |
686 | } |
687 | if (left) { | |
688 | VLOG_DBG_RL(&rl, "attributes followed by garbage"); | |
689 | return false; | |
064af421 | 690 | } |
be3330a6 BP |
691 | |
692 | for (i = 0; i < n_attrs; i++) { | |
693 | const struct nl_policy *e = &policy[i]; | |
694 | if (!e->optional && e->type != NL_A_NO_ATTR && !attrs[i]) { | |
695 | VLOG_DBG_RL(&rl, "required attr %zu missing", i); | |
696 | return false; | |
697 | } | |
064af421 BP |
698 | } |
699 | return true; | |
700 | } | |
25eeae6a BP |
701 | |
702 | /* Parses the Netlink attributes within 'nla'. 'policy[i]', for 0 <= i < | |
703 | * n_attrs, specifies how the attribute with nla_type == i is parsed; a pointer | |
704 | * to attribute i is stored in attrs[i]. Returns true if successful, false on | |
705 | * failure. */ | |
706 | bool | |
707 | nl_parse_nested(const struct nlattr *nla, const struct nl_policy policy[], | |
708 | struct nlattr *attrs[], size_t n_attrs) | |
709 | { | |
710 | struct ofpbuf buf; | |
711 | ||
712 | nl_attr_get_nested(nla, &buf); | |
713 | return nl_policy_parse(&buf, 0, policy, attrs, n_attrs); | |
714 | } | |
b18fcf8e | 715 | |
0b6c119b | 716 | const struct nlattr * |
b18fcf8e BP |
717 | nl_attr_find__(const struct nlattr *attrs, size_t size, uint16_t type) |
718 | { | |
719 | const struct nlattr *nla; | |
720 | size_t left; | |
721 | ||
722 | NL_ATTR_FOR_EACH (nla, left, attrs, size) { | |
be3330a6 | 723 | if (nl_attr_type(nla) == type) { |
b18fcf8e BP |
724 | return nla; |
725 | } | |
726 | } | |
727 | return NULL; | |
728 | } | |
729 | ||
730 | /* Returns the first Netlink attribute within 'buf' with the specified 'type', | |
731 | * skipping a header of 'hdr_len' bytes at the beginning of 'buf'. | |
732 | * | |
733 | * This function does not validate the attribute's length. */ | |
734 | const struct nlattr * | |
735 | nl_attr_find(const struct ofpbuf *buf, size_t hdr_len, uint16_t type) | |
736 | { | |
737 | const uint8_t *start = (const uint8_t *) buf->data + hdr_len; | |
738 | return nl_attr_find__((const struct nlattr *) start, buf->size - hdr_len, | |
739 | type); | |
740 | } | |
741 | ||
742 | /* Returns the first Netlink attribute within 'nla' with the specified | |
743 | * 'type'. | |
744 | * | |
745 | * This function does not validate the attribute's length. */ | |
746 | const struct nlattr * | |
747 | nl_attr_find_nested(const struct nlattr *nla, uint16_t type) | |
748 | { | |
749 | return nl_attr_find__(nl_attr_get(nla), nl_attr_get_size(nla), type); | |
750 | } |