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