]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
4e022ec0 | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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" | |
4e022ec0 | 24 | #include "flow.h" |
064af421 BP |
25 | #include "netlink-protocol.h" |
26 | #include "ofpbuf.h" | |
064af421 | 27 | #include "timeval.h" |
34c7bb50 | 28 | #include "unaligned.h" |
064af421 | 29 | #include "vlog.h" |
5136ce49 | 30 | |
d98e6007 | 31 | VLOG_DEFINE_THIS_MODULE(netlink); |
064af421 | 32 | |
064af421 BP |
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 | ||
064af421 BP |
38 | /* Returns the nlmsghdr at the head of 'msg'. |
39 | * | |
40 | * 'msg' must be at least as large as a nlmsghdr. */ | |
41 | struct nlmsghdr * | |
d295e8e9 | 42 | nl_msg_nlmsghdr(const struct ofpbuf *msg) |
064af421 BP |
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 * | |
d295e8e9 | 52 | nl_msg_genlmsghdr(const struct ofpbuf *msg) |
064af421 BP |
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 | |
d295e8e9 | 63 | nl_msg_nlmsgerr(const struct ofpbuf *msg, int *errorp) |
064af421 BP |
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) { | |
34582733 | 69 | VLOG_ERR_RL(&rl, "received invalid nlmsgerr (%"PRIuSIZE"d bytes < %"PRIuSIZE"d)", |
064af421 BP |
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 | |
d295e8e9 | 86 | nl_msg_reserve(struct ofpbuf *msg, size_t size) |
064af421 BP |
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. | |
69123704 | 92 | * Uses the given 'type' and 'flags'. 'expected_payload' should be |
064af421 BP |
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 | * | |
7d7447df BP |
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. | |
69123704 BP |
107 | * |
108 | * nl_msg_put_genlmsghdr() is more convenient for composing a Generic Netlink | |
064af421 BP |
109 | * message. */ |
110 | void | |
69123704 | 111 | nl_msg_put_nlmsghdr(struct ofpbuf *msg, |
d295e8e9 | 112 | size_t expected_payload, uint32_t type, uint32_t flags) |
064af421 BP |
113 | { |
114 | struct nlmsghdr *nlmsghdr; | |
115 | ||
cb22974d | 116 | ovs_assert(msg->size == 0); |
064af421 BP |
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; | |
7d7447df | 123 | nlmsghdr->nlmsg_seq = 0; |
69123704 | 124 | nlmsghdr->nlmsg_pid = 0; |
064af421 BP |
125 | } |
126 | ||
127 | /* Puts a nlmsghdr and genlmsghdr at the beginning of 'msg', which must be | |
69123704 BP |
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. | |
064af421 BP |
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 | * | |
69123704 BP |
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. */ | |
064af421 | 148 | void |
69123704 BP |
149 | nl_msg_put_genlmsghdr(struct ofpbuf *msg, size_t expected_payload, |
150 | int family, uint32_t flags, uint8_t cmd, uint8_t version) | |
064af421 BP |
151 | { |
152 | struct genlmsghdr *genlmsghdr; | |
153 | ||
69123704 | 154 | nl_msg_put_nlmsghdr(msg, GENL_HDRLEN + expected_payload, family, flags); |
cb22974d | 155 | ovs_assert(msg->size == NLMSG_HDRLEN); |
064af421 BP |
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 | |
d295e8e9 | 166 | nl_msg_put(struct ofpbuf *msg, const void *data, size_t size) |
064af421 BP |
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 * | |
d295e8e9 | 175 | nl_msg_put_uninit(struct ofpbuf *msg, size_t size) |
064af421 BP |
176 | { |
177 | size_t pad = NLMSG_ALIGN(size) - size; | |
178 | char *p = ofpbuf_put_uninit(msg, size + pad); | |
179 | if (pad) { | |
d295e8e9 | 180 | memset(p + size, 0, pad); |
064af421 BP |
181 | } |
182 | return p; | |
183 | } | |
184 | ||
46d34fef BP |
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 | ||
064af421 BP |
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 * | |
d295e8e9 | 213 | nl_msg_put_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size) |
064af421 BP |
214 | { |
215 | size_t total_size = NLA_HDRLEN + size; | |
216 | struct nlattr* nla = nl_msg_put_uninit(msg, total_size); | |
cb22974d | 217 | ovs_assert(NLA_ALIGN(total_size) <= UINT16_MAX); |
064af421 BP |
218 | nla->nla_len = total_size; |
219 | nla->nla_type = type; | |
220 | return nla + 1; | |
221 | } | |
222 | ||
9ddf12cc BP |
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 | ||
064af421 BP |
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, | |
d295e8e9 | 241 | const void *data, size_t size) |
064af421 BP |
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 | |
d295e8e9 | 250 | nl_msg_put_flag(struct ofpbuf *msg, uint16_t type) |
064af421 BP |
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 | |
d295e8e9 | 258 | nl_msg_put_u8(struct ofpbuf *msg, uint16_t type, uint8_t value) |
064af421 BP |
259 | { |
260 | nl_msg_put_unspec(msg, type, &value, sizeof value); | |
261 | } | |
262 | ||
ee913f98 BP |
263 | /* Appends a Netlink attribute of the given 'type' and the given 16-bit host |
264 | * byte order 'value' to 'msg'. */ | |
064af421 BP |
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 | ||
ee913f98 BP |
271 | /* Appends a Netlink attribute of the given 'type' and the given 32-bit host |
272 | * byte order 'value' to 'msg'. */ | |
064af421 BP |
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 | ||
ee913f98 BP |
279 | /* Appends a Netlink attribute of the given 'type' and the given 64-bit host |
280 | * byte order 'value' to 'msg'. */ | |
064af421 BP |
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 | ||
ee913f98 BP |
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 | ||
4e022ec0 AW |
311 | /* Appends a Netlink attribute of the given 'type' and the given odp_port_t |
312 | * 'value' to 'msg'. */ | |
313 | void | |
314 | nl_msg_put_odp_port(struct ofpbuf *msg, uint16_t type, odp_port_t value) | |
315 | { | |
316 | nl_msg_put_u32(msg, type, odp_to_u32(value)); | |
317 | } | |
318 | ||
319 | ||
064af421 BP |
320 | /* Appends a Netlink attribute of the given 'type' and the given |
321 | * null-terminated string 'value' to 'msg'. */ | |
322 | void | |
323 | nl_msg_put_string(struct ofpbuf *msg, uint16_t type, const char *value) | |
324 | { | |
325 | nl_msg_put_unspec(msg, type, value, strlen(value) + 1); | |
326 | } | |
327 | ||
46d34fef BP |
328 | /* Prepends a Netlink attribute of the given 'type' and room for 'size' bytes |
329 | * of data as its payload, plus Netlink padding if needed, to the head end of | |
330 | * 'msg', reallocating and copying its data if necessary. Returns a pointer to | |
331 | * the first byte of data in the attribute, which is left uninitialized. */ | |
332 | void * | |
333 | nl_msg_push_unspec_uninit(struct ofpbuf *msg, uint16_t type, size_t size) | |
334 | { | |
335 | size_t total_size = NLA_HDRLEN + size; | |
336 | struct nlattr* nla = nl_msg_push_uninit(msg, total_size); | |
542024c4 | 337 | ovs_assert(!nl_attr_oversized(size)); |
46d34fef BP |
338 | nla->nla_len = total_size; |
339 | nla->nla_type = type; | |
340 | return nla + 1; | |
341 | } | |
342 | ||
343 | /* Prepends a Netlink attribute of the given 'type' and the 'size' bytes of | |
344 | * 'data' as its payload, to the head end of 'msg', reallocating and copying | |
345 | * its data if necessary. Returns a pointer to the first byte of data in the | |
346 | * attribute, which is left uninitialized. */ | |
347 | void | |
348 | nl_msg_push_unspec(struct ofpbuf *msg, uint16_t type, | |
349 | const void *data, size_t size) | |
350 | { | |
351 | memcpy(nl_msg_push_unspec_uninit(msg, type, size), data, size); | |
352 | } | |
353 | ||
354 | /* Prepends a Netlink attribute of the given 'type' and no payload to 'msg'. | |
355 | * (Some Netlink protocols use the presence or absence of an attribute as a | |
356 | * Boolean flag.) */ | |
357 | void | |
358 | nl_msg_push_flag(struct ofpbuf *msg, uint16_t type) | |
359 | { | |
360 | nl_msg_push_unspec(msg, type, NULL, 0); | |
361 | } | |
362 | ||
363 | /* Prepends a Netlink attribute of the given 'type' and the given 8-bit 'value' | |
364 | * to 'msg'. */ | |
365 | void | |
366 | nl_msg_push_u8(struct ofpbuf *msg, uint16_t type, uint8_t value) | |
367 | { | |
368 | nl_msg_push_unspec(msg, type, &value, sizeof value); | |
369 | } | |
370 | ||
371 | /* Prepends a Netlink attribute of the given 'type' and the given 16-bit host | |
372 | * byte order 'value' to 'msg'. */ | |
373 | void | |
374 | nl_msg_push_u16(struct ofpbuf *msg, uint16_t type, uint16_t value) | |
375 | { | |
376 | nl_msg_push_unspec(msg, type, &value, sizeof value); | |
377 | } | |
378 | ||
379 | /* Prepends a Netlink attribute of the given 'type' and the given 32-bit host | |
380 | * byte order 'value' to 'msg'. */ | |
381 | void | |
382 | nl_msg_push_u32(struct ofpbuf *msg, uint16_t type, uint32_t value) | |
383 | { | |
384 | nl_msg_push_unspec(msg, type, &value, sizeof value); | |
385 | } | |
386 | ||
387 | /* Prepends a Netlink attribute of the given 'type' and the given 64-bit host | |
388 | * byte order 'value' to 'msg'. */ | |
389 | void | |
390 | nl_msg_push_u64(struct ofpbuf *msg, uint16_t type, uint64_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 | |
396 | * network byte order 'value' to 'msg'. */ | |
397 | void | |
398 | nl_msg_push_be16(struct ofpbuf *msg, uint16_t type, ovs_be16 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 | |
404 | * network byte order 'value' to 'msg'. */ | |
405 | void | |
406 | nl_msg_push_be32(struct ofpbuf *msg, uint16_t type, ovs_be32 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 | |
412 | * network byte order 'value' to 'msg'. */ | |
413 | void | |
414 | nl_msg_push_be64(struct ofpbuf *msg, uint16_t type, ovs_be64 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 | |
420 | * null-terminated string 'value' to 'msg'. */ | |
421 | void | |
422 | nl_msg_push_string(struct ofpbuf *msg, uint16_t type, const char *value) | |
423 | { | |
424 | nl_msg_push_unspec(msg, type, value, strlen(value) + 1); | |
425 | } | |
426 | ||
38a99756 BP |
427 | /* Adds the header for nested Netlink attributes to 'msg', with the specified |
428 | * 'type', and returns the header's offset within 'msg'. The caller should add | |
429 | * the content for the nested Netlink attribute to 'msg' (e.g. using the other | |
430 | * nl_msg_*() functions), and then pass the returned offset to | |
431 | * nl_msg_end_nested() to finish up the nested attributes. */ | |
432 | size_t | |
433 | nl_msg_start_nested(struct ofpbuf *msg, uint16_t type) | |
434 | { | |
435 | size_t offset = msg->size; | |
436 | nl_msg_put_unspec(msg, type, NULL, 0); | |
437 | return offset; | |
438 | } | |
439 | ||
440 | /* Finalizes a nested Netlink attribute in 'msg'. 'offset' should be the value | |
441 | * returned by nl_msg_start_nested(). */ | |
442 | void | |
443 | nl_msg_end_nested(struct ofpbuf *msg, size_t offset) | |
444 | { | |
445 | struct nlattr *attr = ofpbuf_at_assert(msg, offset, sizeof *attr); | |
446 | attr->nla_len = msg->size - offset; | |
447 | } | |
448 | ||
449 | /* Appends a nested Netlink attribute of the given 'type', with the 'size' | |
450 | * bytes of content starting at 'data', to 'msg'. */ | |
064af421 BP |
451 | void |
452 | nl_msg_put_nested(struct ofpbuf *msg, | |
38a99756 | 453 | uint16_t type, const void *data, size_t size) |
064af421 | 454 | { |
38a99756 BP |
455 | size_t offset = nl_msg_start_nested(msg, type); |
456 | nl_msg_put(msg, data, size); | |
457 | nl_msg_end_nested(msg, offset); | |
064af421 BP |
458 | } |
459 | ||
974d6a6d BP |
460 | /* If 'buffer' begins with a valid "struct nlmsghdr", pulls the header and its |
461 | * payload off 'buffer', stores header and payload in 'msg->data' and | |
462 | * 'msg->size', and returns a pointer to the header. | |
463 | * | |
464 | * If 'buffer' does not begin with a "struct nlmsghdr" or begins with one that | |
465 | * is invalid, returns NULL without modifying 'buffer'. */ | |
466 | struct nlmsghdr * | |
467 | nl_msg_next(struct ofpbuf *buffer, struct ofpbuf *msg) | |
468 | { | |
469 | if (buffer->size >= sizeof(struct nlmsghdr)) { | |
470 | struct nlmsghdr *nlmsghdr = nl_msg_nlmsghdr(buffer); | |
471 | size_t len = nlmsghdr->nlmsg_len; | |
472 | if (len >= sizeof *nlmsghdr && len <= buffer->size) { | |
0bc9407d | 473 | ofpbuf_use_const(msg, nlmsghdr, len); |
974d6a6d BP |
474 | ofpbuf_pull(buffer, len); |
475 | return nlmsghdr; | |
476 | } | |
477 | } | |
478 | ||
479 | msg->data = NULL; | |
480 | msg->size = 0; | |
481 | return NULL; | |
482 | } | |
542024c4 BP |
483 | |
484 | /* Returns true if a Netlink attribute with a payload that is 'payload_size' | |
485 | * bytes long would be oversized, that is, if it's not possible to create an | |
486 | * nlattr of that size because its size wouldn't fit in the 16-bit nla_len | |
487 | * field. */ | |
488 | bool | |
489 | nl_attr_oversized(size_t payload_size) | |
490 | { | |
491 | return NL_ATTR_SIZE(payload_size) > UINT16_MAX; | |
492 | } | |
974d6a6d BP |
493 | \f |
494 | /* Attributes. */ | |
495 | ||
7c624478 BP |
496 | /* Returns the bits of 'nla->nla_type' that are significant for determining its |
497 | * type. */ | |
498 | int | |
499 | nl_attr_type(const struct nlattr *nla) | |
500 | { | |
501 | return nla->nla_type & NLA_TYPE_MASK; | |
502 | } | |
503 | ||
064af421 BP |
504 | /* Returns the first byte in the payload of attribute 'nla'. */ |
505 | const void * | |
d295e8e9 | 506 | nl_attr_get(const struct nlattr *nla) |
064af421 | 507 | { |
cb22974d | 508 | ovs_assert(nla->nla_len >= NLA_HDRLEN); |
064af421 BP |
509 | return nla + 1; |
510 | } | |
511 | ||
512 | /* Returns the number of bytes in the payload of attribute 'nla'. */ | |
513 | size_t | |
d295e8e9 | 514 | nl_attr_get_size(const struct nlattr *nla) |
064af421 | 515 | { |
cb22974d | 516 | ovs_assert(nla->nla_len >= NLA_HDRLEN); |
064af421 BP |
517 | return nla->nla_len - NLA_HDRLEN; |
518 | } | |
519 | ||
520 | /* Asserts that 'nla''s payload is at least 'size' bytes long, and returns the | |
521 | * first byte of the payload. */ | |
522 | const void * | |
d295e8e9 | 523 | nl_attr_get_unspec(const struct nlattr *nla, size_t size) |
064af421 | 524 | { |
cb22974d | 525 | ovs_assert(nla->nla_len >= NLA_HDRLEN + size); |
064af421 BP |
526 | return nla + 1; |
527 | } | |
528 | ||
529 | /* Returns true if 'nla' is nonnull. (Some Netlink protocols use the presence | |
530 | * or absence of an attribute as a Boolean flag.) */ | |
531 | bool | |
d295e8e9 | 532 | nl_attr_get_flag(const struct nlattr *nla) |
064af421 BP |
533 | { |
534 | return nla != NULL; | |
535 | } | |
536 | ||
537 | #define NL_ATTR_GET_AS(NLA, TYPE) \ | |
538 | (*(TYPE*) nl_attr_get_unspec(nla, sizeof(TYPE))) | |
539 | ||
540 | /* Returns the 8-bit value in 'nla''s payload. | |
541 | * | |
542 | * Asserts that 'nla''s payload is at least 1 byte long. */ | |
543 | uint8_t | |
d295e8e9 | 544 | nl_attr_get_u8(const struct nlattr *nla) |
064af421 BP |
545 | { |
546 | return NL_ATTR_GET_AS(nla, uint8_t); | |
547 | } | |
548 | ||
ee913f98 | 549 | /* Returns the 16-bit host byte order value in 'nla''s payload. |
064af421 BP |
550 | * |
551 | * Asserts that 'nla''s payload is at least 2 bytes long. */ | |
552 | uint16_t | |
d295e8e9 | 553 | nl_attr_get_u16(const struct nlattr *nla) |
064af421 BP |
554 | { |
555 | return NL_ATTR_GET_AS(nla, uint16_t); | |
556 | } | |
557 | ||
ee913f98 | 558 | /* Returns the 32-bit host byte order value in 'nla''s payload. |
064af421 BP |
559 | * |
560 | * Asserts that 'nla''s payload is at least 4 bytes long. */ | |
561 | uint32_t | |
d295e8e9 | 562 | nl_attr_get_u32(const struct nlattr *nla) |
064af421 BP |
563 | { |
564 | return NL_ATTR_GET_AS(nla, uint32_t); | |
565 | } | |
566 | ||
ee913f98 | 567 | /* Returns the 64-bit host byte order value in 'nla''s payload. |
064af421 BP |
568 | * |
569 | * Asserts that 'nla''s payload is at least 8 bytes long. */ | |
570 | uint64_t | |
d295e8e9 | 571 | nl_attr_get_u64(const struct nlattr *nla) |
064af421 | 572 | { |
34c7bb50 BP |
573 | const ovs_32aligned_u64 *x = nl_attr_get_unspec(nla, sizeof *x); |
574 | return get_32aligned_u64(x); | |
064af421 BP |
575 | } |
576 | ||
ee913f98 BP |
577 | /* Returns the 16-bit network byte order value in 'nla''s payload. |
578 | * | |
579 | * Asserts that 'nla''s payload is at least 2 bytes long. */ | |
580 | ovs_be16 | |
581 | nl_attr_get_be16(const struct nlattr *nla) | |
582 | { | |
583 | return NL_ATTR_GET_AS(nla, ovs_be16); | |
584 | } | |
585 | ||
586 | /* Returns the 32-bit network byte order value in 'nla''s payload. | |
587 | * | |
588 | * Asserts that 'nla''s payload is at least 4 bytes long. */ | |
589 | ovs_be32 | |
590 | nl_attr_get_be32(const struct nlattr *nla) | |
591 | { | |
592 | return NL_ATTR_GET_AS(nla, ovs_be32); | |
593 | } | |
594 | ||
595 | /* Returns the 64-bit network byte order value in 'nla''s payload. | |
596 | * | |
597 | * Asserts that 'nla''s payload is at least 8 bytes long. */ | |
598 | ovs_be64 | |
599 | nl_attr_get_be64(const struct nlattr *nla) | |
600 | { | |
34c7bb50 BP |
601 | const ovs_32aligned_be64 *x = nl_attr_get_unspec(nla, sizeof *x); |
602 | return get_32aligned_be64(x); | |
ee913f98 BP |
603 | } |
604 | ||
4e022ec0 AW |
605 | /* Returns the 32-bit odp_port_t value in 'nla''s payload. |
606 | * | |
607 | * Asserts that 'nla''s payload is at least 4 bytes long. */ | |
608 | odp_port_t | |
609 | nl_attr_get_odp_port(const struct nlattr *nla) | |
610 | { | |
611 | return u32_to_odp(nl_attr_get_u32(nla)); | |
612 | } | |
613 | ||
064af421 BP |
614 | /* Returns the null-terminated string value in 'nla''s payload. |
615 | * | |
616 | * Asserts that 'nla''s payload contains a null-terminated string. */ | |
617 | const char * | |
d295e8e9 | 618 | nl_attr_get_string(const struct nlattr *nla) |
064af421 | 619 | { |
cb22974d BP |
620 | ovs_assert(nla->nla_len > NLA_HDRLEN); |
621 | ovs_assert(memchr(nl_attr_get(nla), '\0', nla->nla_len - NLA_HDRLEN)); | |
064af421 BP |
622 | return nl_attr_get(nla); |
623 | } | |
624 | ||
0bc9407d | 625 | /* Initializes 'nested' to the payload of 'nla'. */ |
25eeae6a BP |
626 | void |
627 | nl_attr_get_nested(const struct nlattr *nla, struct ofpbuf *nested) | |
628 | { | |
0bc9407d | 629 | ofpbuf_use_const(nested, nl_attr_get(nla), nl_attr_get_size(nla)); |
25eeae6a BP |
630 | } |
631 | ||
42b1bc99 BP |
632 | /* Default minimum payload size for each type of attribute. */ |
633 | static size_t | |
634 | min_attr_len(enum nl_attr_type type) | |
635 | { | |
636 | switch (type) { | |
637 | case NL_A_NO_ATTR: return 0; | |
638 | case NL_A_UNSPEC: return 0; | |
639 | case NL_A_U8: return 1; | |
640 | case NL_A_U16: return 2; | |
641 | case NL_A_U32: return 4; | |
642 | case NL_A_U64: return 8; | |
643 | case NL_A_STRING: return 1; | |
644 | case NL_A_FLAG: return 0; | |
645 | case NL_A_NESTED: return 0; | |
646 | case N_NL_ATTR_TYPES: default: NOT_REACHED(); | |
647 | } | |
648 | } | |
649 | ||
650 | /* Default maximum payload size for each type of attribute. */ | |
651 | static size_t | |
652 | max_attr_len(enum nl_attr_type type) | |
653 | { | |
654 | switch (type) { | |
655 | case NL_A_NO_ATTR: return SIZE_MAX; | |
656 | case NL_A_UNSPEC: return SIZE_MAX; | |
657 | case NL_A_U8: return 1; | |
658 | case NL_A_U16: return 2; | |
659 | case NL_A_U32: return 4; | |
660 | case NL_A_U64: return 8; | |
661 | case NL_A_STRING: return SIZE_MAX; | |
662 | case NL_A_FLAG: return SIZE_MAX; | |
663 | case NL_A_NESTED: return SIZE_MAX; | |
664 | case N_NL_ATTR_TYPES: default: NOT_REACHED(); | |
665 | } | |
666 | } | |
064af421 | 667 | |
be3330a6 BP |
668 | bool |
669 | nl_attr_validate(const struct nlattr *nla, const struct nl_policy *policy) | |
670 | { | |
671 | uint16_t type = nl_attr_type(nla); | |
672 | size_t min_len; | |
673 | size_t max_len; | |
674 | size_t len; | |
675 | ||
676 | if (policy->type == NL_A_NO_ATTR) { | |
677 | return true; | |
678 | } | |
679 | ||
680 | /* Figure out min and max length. */ | |
681 | min_len = policy->min_len; | |
682 | if (!min_len) { | |
42b1bc99 | 683 | min_len = min_attr_len(policy->type); |
be3330a6 BP |
684 | } |
685 | max_len = policy->max_len; | |
686 | if (!max_len) { | |
42b1bc99 | 687 | max_len = max_attr_len(policy->type); |
be3330a6 BP |
688 | } |
689 | ||
690 | /* Verify length. */ | |
691 | len = nl_attr_get_size(nla); | |
692 | if (len < min_len || len > max_len) { | |
34582733 AS |
693 | VLOG_DBG_RL(&rl, "attr %"PRIu16" length %"PRIuSIZE" not in " |
694 | "allowed range %"PRIuSIZE"...%"PRIuSIZE, type, len, min_len, max_len); | |
be3330a6 BP |
695 | return false; |
696 | } | |
697 | ||
698 | /* Strings must be null terminated and must not have embedded nulls. */ | |
699 | if (policy->type == NL_A_STRING) { | |
700 | if (((char *) nla)[nla->nla_len - 1]) { | |
701 | VLOG_DBG_RL(&rl, "attr %"PRIu16" lacks null at end", type); | |
702 | return false; | |
703 | } | |
704 | if (memchr(nla + 1, '\0', len - 1) != NULL) { | |
705 | VLOG_DBG_RL(&rl, "attr %"PRIu16" has bad length", type); | |
706 | return false; | |
707 | } | |
708 | } | |
709 | ||
710 | return true; | |
711 | } | |
712 | ||
064af421 BP |
713 | /* Parses the 'msg' starting at the given 'nla_offset' as a sequence of Netlink |
714 | * attributes. 'policy[i]', for 0 <= i < n_attrs, specifies how the attribute | |
715 | * with nla_type == i is parsed; a pointer to attribute i is stored in | |
716 | * attrs[i]. Returns true if successful, false on failure. | |
717 | * | |
718 | * If the Netlink attributes in 'msg' follow a Netlink header and a Generic | |
719 | * Netlink header, then 'nla_offset' should be NLMSG_HDRLEN + GENL_HDRLEN. */ | |
720 | bool | |
721 | nl_policy_parse(const struct ofpbuf *msg, size_t nla_offset, | |
722 | const struct nl_policy policy[], | |
723 | struct nlattr *attrs[], size_t n_attrs) | |
724 | { | |
506564b8 | 725 | struct nlattr *nla; |
506564b8 | 726 | size_t left; |
064af421 BP |
727 | size_t i; |
728 | ||
be3330a6 | 729 | memset(attrs, 0, n_attrs * sizeof *attrs); |
064af421 | 730 | |
506564b8 | 731 | if (msg->size < nla_offset) { |
064af421 BP |
732 | VLOG_DBG_RL(&rl, "missing headers in nl_policy_parse"); |
733 | return false; | |
734 | } | |
064af421 | 735 | |
db5a1019 | 736 | NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, nla_offset, 0), |
be3330a6 BP |
737 | msg->size - nla_offset) |
738 | { | |
506564b8 | 739 | uint16_t type = nl_attr_type(nla); |
064af421 | 740 | if (type < n_attrs && policy[type].type != NL_A_NO_ATTR) { |
2a022368 | 741 | const struct nl_policy *e = &policy[type]; |
be3330a6 | 742 | if (!nl_attr_validate(nla, e)) { |
064af421 BP |
743 | return false; |
744 | } | |
33cada0c | 745 | if (attrs[type]) { |
be3330a6 | 746 | VLOG_DBG_RL(&rl, "duplicate attr %"PRIu16, type); |
33cada0c | 747 | } |
064af421 | 748 | attrs[type] = nla; |
064af421 | 749 | } |
506564b8 BP |
750 | } |
751 | if (left) { | |
752 | VLOG_DBG_RL(&rl, "attributes followed by garbage"); | |
753 | return false; | |
064af421 | 754 | } |
be3330a6 BP |
755 | |
756 | for (i = 0; i < n_attrs; i++) { | |
757 | const struct nl_policy *e = &policy[i]; | |
758 | if (!e->optional && e->type != NL_A_NO_ATTR && !attrs[i]) { | |
34582733 | 759 | VLOG_DBG_RL(&rl, "required attr %"PRIuSIZE" missing", i); |
be3330a6 BP |
760 | return false; |
761 | } | |
064af421 BP |
762 | } |
763 | return true; | |
764 | } | |
25eeae6a BP |
765 | |
766 | /* Parses the Netlink attributes within 'nla'. 'policy[i]', for 0 <= i < | |
767 | * n_attrs, specifies how the attribute with nla_type == i is parsed; a pointer | |
768 | * to attribute i is stored in attrs[i]. Returns true if successful, false on | |
769 | * failure. */ | |
770 | bool | |
771 | nl_parse_nested(const struct nlattr *nla, const struct nl_policy policy[], | |
772 | struct nlattr *attrs[], size_t n_attrs) | |
773 | { | |
774 | struct ofpbuf buf; | |
775 | ||
776 | nl_attr_get_nested(nla, &buf); | |
777 | return nl_policy_parse(&buf, 0, policy, attrs, n_attrs); | |
778 | } | |
b18fcf8e | 779 | |
0b6c119b | 780 | const struct nlattr * |
b18fcf8e BP |
781 | nl_attr_find__(const struct nlattr *attrs, size_t size, uint16_t type) |
782 | { | |
783 | const struct nlattr *nla; | |
784 | size_t left; | |
785 | ||
786 | NL_ATTR_FOR_EACH (nla, left, attrs, size) { | |
be3330a6 | 787 | if (nl_attr_type(nla) == type) { |
b18fcf8e BP |
788 | return nla; |
789 | } | |
790 | } | |
791 | return NULL; | |
792 | } | |
793 | ||
794 | /* Returns the first Netlink attribute within 'buf' with the specified 'type', | |
795 | * skipping a header of 'hdr_len' bytes at the beginning of 'buf'. | |
796 | * | |
797 | * This function does not validate the attribute's length. */ | |
798 | const struct nlattr * | |
799 | nl_attr_find(const struct ofpbuf *buf, size_t hdr_len, uint16_t type) | |
800 | { | |
db5a1019 | 801 | return nl_attr_find__(ofpbuf_at(buf, hdr_len, 0), buf->size - hdr_len, |
b18fcf8e BP |
802 | type); |
803 | } | |
804 | ||
805 | /* Returns the first Netlink attribute within 'nla' with the specified | |
806 | * 'type'. | |
807 | * | |
808 | * This function does not validate the attribute's length. */ | |
809 | const struct nlattr * | |
810 | nl_attr_find_nested(const struct nlattr *nla, uint16_t type) | |
811 | { | |
812 | return nl_attr_find__(nl_attr_get(nla), nl_attr_get_size(nla), type); | |
813 | } |