| 1 |
/* SPDX-License-Identifier: LGPL-2.1-only */ |
| 2 |
/* |
| 3 |
* Copyright (c) 2003-2012 Thomas Graf <[email protected]> |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* @ingroup core |
| 8 |
* @defgroup msg Message Construction & Parsing |
| 9 |
* Netlink Message Construction/Parsing Interface |
| 10 |
* |
| 11 |
* Related sections in the development guide: |
| 12 |
* - @core_doc{_message_parsing_amp_construction,Message Parsing & Construction} |
| 13 |
* |
| 14 |
* @{ |
| 15 |
* |
| 16 |
* Header |
| 17 |
* ------ |
| 18 |
* ~~~~{.c} |
| 19 |
* #include <netlink/msg.h> |
| 20 |
* ~~~~ |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "nl-default.h" |
| 24 |
|
| 25 |
#include <ctype.h> |
| 26 |
|
| 27 |
#include <linux/socket.h> |
| 28 |
|
| 29 |
#include <netlink/netlink.h> |
| 30 |
#include <netlink/utils.h> |
| 31 |
#include <netlink/cache.h> |
| 32 |
#include <netlink/attr.h> |
| 33 |
|
| 34 |
#include "nl-core.h" |
| 35 |
#include "nl-priv-dynamic-core/nl-core.h" |
| 36 |
#include "nl-priv-dynamic-core/cache-api.h" |
| 37 |
#include "nl-aux-core/nl-core.h" |
| 38 |
|
| 39 |
static size_t default_msg_size; /* GLOBAL! */ |
| 40 |
|
| 41 |
static void _nl_init init_msg_size(void) |
| 42 |
{ |
| 43 |
default_msg_size = getpagesize(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @name Size Calculations |
| 48 |
* @{ |
| 49 |
*/ |
| 50 |
|
| 51 |
/** |
| 52 |
* Calculates size of netlink message based on payload length. |
| 53 |
* @arg payload Length of payload |
| 54 |
* |
| 55 |
* @return size of netlink message without padding. |
| 56 |
*/ |
| 57 |
int nlmsg_size(int payload) |
| 58 |
{ |
| 59 |
return NLMSG_HDRLEN + payload; |
| 60 |
} |
| 61 |
|
| 62 |
static int nlmsg_msg_size(int payload) |
| 63 |
{ |
| 64 |
return nlmsg_size(payload); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Calculates size of netlink message including padding based on payload length |
| 69 |
* @arg payload Length of payload |
| 70 |
* |
| 71 |
* This function is idential to nlmsg_size() + nlmsg_padlen(). |
| 72 |
* |
| 73 |
* @return Size of netlink message including padding. |
| 74 |
*/ |
| 75 |
int nlmsg_total_size(int payload) |
| 76 |
{ |
| 77 |
return NLMSG_ALIGN(nlmsg_msg_size(payload)); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Size of padding that needs to be added at end of message |
| 82 |
* @arg payload Length of payload |
| 83 |
* |
| 84 |
* Calculates the number of bytes of padding which is required to be added to |
| 85 |
* the end of the message to ensure that the next netlink message header begins |
| 86 |
* properly aligned to NLMSG_ALIGNTO. |
| 87 |
* |
| 88 |
* @return Number of bytes of padding needed. |
| 89 |
*/ |
| 90 |
int nlmsg_padlen(int payload) |
| 91 |
{ |
| 92 |
return nlmsg_total_size(payload) - nlmsg_msg_size(payload); |
| 93 |
} |
| 94 |
|
| 95 |
/** @} */ |
| 96 |
|
| 97 |
/** |
| 98 |
* @name Access to Message Payload |
| 99 |
* @{ |
| 100 |
*/ |
| 101 |
|
| 102 |
/** |
| 103 |
* Return pointer to message payload |
| 104 |
* @arg nlh Netlink message header |
| 105 |
* |
| 106 |
* @return Pointer to start of message payload. |
| 107 |
*/ |
| 108 |
void *nlmsg_data(const struct nlmsghdr *nlh) |
| 109 |
{ |
| 110 |
return (unsigned char *) nlh + NLMSG_HDRLEN; |
| 111 |
} |
| 112 |
|
| 113 |
void *nlmsg_tail(const struct nlmsghdr *nlh) |
| 114 |
{ |
| 115 |
return (unsigned char *) nlh + NLMSG_ALIGN(nlh->nlmsg_len); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Return length of message payload |
| 120 |
* @arg nlh Netlink message header |
| 121 |
* |
| 122 |
* @return Length of message payload in bytes. |
| 123 |
*/ |
| 124 |
int nlmsg_datalen(const struct nlmsghdr *nlh) |
| 125 |
{ |
| 126 |
return nlh->nlmsg_len - NLMSG_HDRLEN; |
| 127 |
} |
| 128 |
|
| 129 |
static int nlmsg_len(const struct nlmsghdr *nlh) |
| 130 |
{ |
| 131 |
return nlmsg_datalen(nlh); |
| 132 |
} |
| 133 |
|
| 134 |
/** @} */ |
| 135 |
|
| 136 |
/** |
| 137 |
* @name Attribute Access |
| 138 |
* @{ |
| 139 |
*/ |
| 140 |
|
| 141 |
/** |
| 142 |
* head of attributes data |
| 143 |
* @arg nlh netlink message header |
| 144 |
* @arg hdrlen length of family specific header |
| 145 |
*/ |
| 146 |
struct nlattr *nlmsg_attrdata(const struct nlmsghdr *nlh, int hdrlen) |
| 147 |
{ |
| 148 |
unsigned char *data = nlmsg_data(nlh); |
| 149 |
return (struct nlattr *) (data + NLMSG_ALIGN(hdrlen)); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* length of attributes data |
| 154 |
* @arg nlh netlink message header |
| 155 |
* @arg hdrlen length of family specific header |
| 156 |
*/ |
| 157 |
int nlmsg_attrlen(const struct nlmsghdr *nlh, int hdrlen) |
| 158 |
{ |
| 159 |
return _NL_MAX(nlmsg_len(nlh) - NLMSG_ALIGN(hdrlen), 0u); |
| 160 |
} |
| 161 |
|
| 162 |
/** @} */ |
| 163 |
|
| 164 |
/** |
| 165 |
* @name Message Parsing |
| 166 |
* @{ |
| 167 |
*/ |
| 168 |
|
| 169 |
int nlmsg_valid_hdr(const struct nlmsghdr *nlh, int hdrlen) |
| 170 |
{ |
| 171 |
int s; |
| 172 |
|
| 173 |
s = nlmsg_msg_size(hdrlen); |
| 174 |
if (s < 0 || nlh->nlmsg_len < ((unsigned)s)) |
| 175 |
return 0; |
| 176 |
|
| 177 |
return 1; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* check if the netlink message fits into the remaining bytes |
| 182 |
* @arg nlh netlink message header |
| 183 |
* @arg remaining number of bytes remaining in message stream |
| 184 |
*/ |
| 185 |
int nlmsg_ok(const struct nlmsghdr *nlh, int remaining) |
| 186 |
{ |
| 187 |
return (remaining >= (int)sizeof(struct nlmsghdr) && |
| 188 |
nlh->nlmsg_len >= sizeof(struct nlmsghdr) && |
| 189 |
nlh->nlmsg_len <= ((unsigned)remaining)); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* next netlink message in message stream |
| 194 |
* @arg nlh netlink message header |
| 195 |
* @arg remaining number of bytes remaining in message stream |
| 196 |
* |
| 197 |
* @returns the next netlink message in the message stream and |
| 198 |
* decrements remaining by the size of the current message. |
| 199 |
*/ |
| 200 |
struct nlmsghdr *nlmsg_next(struct nlmsghdr *nlh, int *remaining) |
| 201 |
{ |
| 202 |
int totlen = NLMSG_ALIGN(nlh->nlmsg_len); |
| 203 |
|
| 204 |
*remaining -= totlen; |
| 205 |
|
| 206 |
return (struct nlmsghdr *) ((unsigned char *) nlh + totlen); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* parse attributes of a netlink message |
| 211 |
* @arg nlh netlink message header |
| 212 |
* @arg hdrlen length of family specific header |
| 213 |
* @arg tb destination array with maxtype+1 elements |
| 214 |
* @arg maxtype maximum attribute type to be expected |
| 215 |
* @arg policy validation policy |
| 216 |
* |
| 217 |
* See nla_parse() |
| 218 |
*/ |
| 219 |
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], |
| 220 |
int maxtype, const struct nla_policy *policy) |
| 221 |
{ |
| 222 |
if (!nlmsg_valid_hdr(nlh, hdrlen)) |
| 223 |
return -NLE_MSG_TOOSHORT; |
| 224 |
|
| 225 |
return nla_parse(tb, maxtype, nlmsg_attrdata(nlh, hdrlen), |
| 226 |
nlmsg_attrlen(nlh, hdrlen), policy); |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* nlmsg_find_attr - find a specific attribute in a netlink message |
| 231 |
* @arg nlh netlink message header |
| 232 |
* @arg hdrlen length of familiy specific header |
| 233 |
* @arg attrtype type of attribute to look for |
| 234 |
* |
| 235 |
* Returns the first attribute which matches the specified type. |
| 236 |
*/ |
| 237 |
struct nlattr *nlmsg_find_attr(struct nlmsghdr *nlh, int hdrlen, int attrtype) |
| 238 |
{ |
| 239 |
return nla_find(nlmsg_attrdata(nlh, hdrlen), |
| 240 |
nlmsg_attrlen(nlh, hdrlen), attrtype); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* nlmsg_validate - validate a netlink message including attributes |
| 245 |
* @arg nlh netlinket message header |
| 246 |
* @arg hdrlen length of familiy specific header |
| 247 |
* @arg maxtype maximum attribute type to be expected |
| 248 |
* @arg policy validation policy |
| 249 |
*/ |
| 250 |
int nlmsg_validate(struct nlmsghdr *nlh, int hdrlen, int maxtype, |
| 251 |
const struct nla_policy *policy) |
| 252 |
{ |
| 253 |
if (!nlmsg_valid_hdr(nlh, hdrlen)) |
| 254 |
return -NLE_MSG_TOOSHORT; |
| 255 |
|
| 256 |
return nla_validate(nlmsg_attrdata(nlh, hdrlen), |
| 257 |
nlmsg_attrlen(nlh, hdrlen), maxtype, policy); |
| 258 |
} |
| 259 |
|
| 260 |
/** @} */ |
| 261 |
|
| 262 |
/** |
| 263 |
* @name Message Building/Access |
| 264 |
* @{ |
| 265 |
*/ |
| 266 |
|
| 267 |
static struct nl_msg *__nlmsg_alloc(size_t len) |
| 268 |
{ |
| 269 |
struct nl_msg *nm; |
| 270 |
|
| 271 |
if (len < sizeof(struct nlmsghdr)) |
| 272 |
len = sizeof(struct nlmsghdr); |
| 273 |
|
| 274 |
nm = calloc(1, sizeof(*nm)); |
| 275 |
if (!nm) |
| 276 |
goto errout; |
| 277 |
|
| 278 |
nm->nm_refcnt = 1; |
| 279 |
|
| 280 |
nm->nm_nlh = calloc(1, len); |
| 281 |
if (!nm->nm_nlh) |
| 282 |
goto errout; |
| 283 |
|
| 284 |
nm->nm_protocol = -1; |
| 285 |
nm->nm_size = len; |
| 286 |
nm->nm_nlh->nlmsg_len = nlmsg_total_size(0); |
| 287 |
|
| 288 |
NL_DBG(2, "msg %p: Allocated new message, maxlen=%zu\n", nm, len); |
| 289 |
|
| 290 |
return nm; |
| 291 |
errout: |
| 292 |
free(nm); |
| 293 |
return NULL; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Allocate a new netlink message with the default maximum payload size. |
| 298 |
* |
| 299 |
* Allocates a new netlink message without any further payload. The |
| 300 |
* maximum payload size defaults to PAGESIZE or as otherwise specified |
| 301 |
* with nlmsg_set_default_size(). |
| 302 |
* |
| 303 |
* @return Newly allocated netlink message or NULL. |
| 304 |
*/ |
| 305 |
struct nl_msg *nlmsg_alloc(void) |
| 306 |
{ |
| 307 |
return __nlmsg_alloc(default_msg_size); |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Allocate a new netlink message with maximum payload size specified. |
| 312 |
*/ |
| 313 |
struct nl_msg *nlmsg_alloc_size(size_t max) |
| 314 |
{ |
| 315 |
return __nlmsg_alloc(max); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Allocate a new netlink message and inherit netlink message header |
| 320 |
* @arg hdr Netlink message header template |
| 321 |
* |
| 322 |
* Allocates a new netlink message and inherits the original message |
| 323 |
* header. If \a hdr is not NULL it will be used as a template for |
| 324 |
* the netlink message header, otherwise the header is left blank. |
| 325 |
* |
| 326 |
* @return Newly allocated netlink message or NULL |
| 327 |
*/ |
| 328 |
struct nl_msg *nlmsg_inherit(struct nlmsghdr *hdr) |
| 329 |
{ |
| 330 |
struct nl_msg *nm; |
| 331 |
|
| 332 |
nm = nlmsg_alloc(); |
| 333 |
if (nm && hdr) { |
| 334 |
struct nlmsghdr *new = nm->nm_nlh; |
| 335 |
|
| 336 |
new->nlmsg_type = hdr->nlmsg_type; |
| 337 |
new->nlmsg_flags = hdr->nlmsg_flags; |
| 338 |
new->nlmsg_seq = hdr->nlmsg_seq; |
| 339 |
new->nlmsg_pid = hdr->nlmsg_pid; |
| 340 |
} |
| 341 |
|
| 342 |
return nm; |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Allocate a new netlink message |
| 347 |
* @arg nlmsgtype Netlink message type |
| 348 |
* @arg flags Message flags. |
| 349 |
* |
| 350 |
* @return Newly allocated netlink message or NULL. |
| 351 |
*/ |
| 352 |
struct nl_msg *nlmsg_alloc_simple(int nlmsgtype, int flags) |
| 353 |
{ |
| 354 |
struct nl_msg *msg; |
| 355 |
struct nlmsghdr nlh = { |
| 356 |
.nlmsg_type = nlmsgtype, |
| 357 |
.nlmsg_flags = flags, |
| 358 |
.nlmsg_seq = NL_AUTO_SEQ, |
| 359 |
.nlmsg_pid = NL_AUTO_PID, |
| 360 |
}; |
| 361 |
|
| 362 |
msg = nlmsg_inherit(&nlh); |
| 363 |
if (msg) |
| 364 |
NL_DBG(2, "msg %p: Allocated new simple message\n", msg); |
| 365 |
|
| 366 |
return msg; |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Set the default maximum message payload size for allocated messages |
| 371 |
* @arg max Size of payload in bytes. |
| 372 |
*/ |
| 373 |
void nlmsg_set_default_size(size_t max) |
| 374 |
{ |
| 375 |
size_t s; |
| 376 |
|
| 377 |
s = nlmsg_total_size(0); |
| 378 |
if (max < s) |
| 379 |
max = s; |
| 380 |
|
| 381 |
default_msg_size = max; |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Convert a netlink message received from a netlink socket to a nl_msg |
| 386 |
* @arg hdr Netlink message received from netlink socket. |
| 387 |
* |
| 388 |
* Allocates a new netlink message and copies all of the data pointed to |
| 389 |
* by \a hdr into the new message object. |
| 390 |
* |
| 391 |
* @return Newly allocated netlink message or NULL. |
| 392 |
*/ |
| 393 |
struct nl_msg *nlmsg_convert(struct nlmsghdr *hdr) |
| 394 |
{ |
| 395 |
struct nl_msg *nm; |
| 396 |
|
| 397 |
nm = __nlmsg_alloc(NLMSG_ALIGN(hdr->nlmsg_len)); |
| 398 |
if (!nm) |
| 399 |
return NULL; |
| 400 |
|
| 401 |
memcpy(nm->nm_nlh, hdr, hdr->nlmsg_len); |
| 402 |
|
| 403 |
return nm; |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Reserve room for additional data in a netlink message |
| 408 |
* @arg n netlink message |
| 409 |
* @arg len length of additional data to reserve room for |
| 410 |
* @arg pad number of bytes to align data to |
| 411 |
* |
| 412 |
* Reserves room for additional data at the tail of the an |
| 413 |
* existing netlink message. Eventual padding required will |
| 414 |
* be zeroed out. |
| 415 |
* |
| 416 |
* @return Pointer to start of additional data tailroom or NULL. |
| 417 |
*/ |
| 418 |
void *nlmsg_reserve(struct nl_msg *n, size_t len, int pad) |
| 419 |
{ |
| 420 |
char *buf = (char *) n->nm_nlh; |
| 421 |
size_t nlmsg_len = n->nm_nlh->nlmsg_len; |
| 422 |
size_t tlen; |
| 423 |
|
| 424 |
if (len > n->nm_size) |
| 425 |
return NULL; |
| 426 |
|
| 427 |
tlen = pad ? ((len + (pad - 1)) & ~(pad - 1)) : len; |
| 428 |
|
| 429 |
if ((tlen + nlmsg_len) > n->nm_size) |
| 430 |
return NULL; |
| 431 |
|
| 432 |
buf += nlmsg_len; |
| 433 |
n->nm_nlh->nlmsg_len += tlen; |
| 434 |
|
| 435 |
if (tlen > len) |
| 436 |
memset(buf + len, 0, tlen - len); |
| 437 |
|
| 438 |
NL_DBG(2, "msg %p: Reserved %zu (%zu) bytes, pad=%d, nlmsg_len=%d\n", |
| 439 |
n, tlen, len, pad, n->nm_nlh->nlmsg_len); |
| 440 |
|
| 441 |
return buf; |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Append data to tail of a netlink message |
| 446 |
* @arg n netlink message |
| 447 |
* @arg data data to add |
| 448 |
* @arg len length of data |
| 449 |
* @arg pad Number of bytes to align data to. |
| 450 |
* |
| 451 |
* Extends the netlink message as needed and appends the data of given |
| 452 |
* length to the message. |
| 453 |
* |
| 454 |
* @return 0 on success or a negative error code |
| 455 |
*/ |
| 456 |
int nlmsg_append(struct nl_msg *n, void *data, size_t len, int pad) |
| 457 |
{ |
| 458 |
void *tmp; |
| 459 |
|
| 460 |
tmp = nlmsg_reserve(n, len, pad); |
| 461 |
if (tmp == NULL) |
| 462 |
return -NLE_NOMEM; |
| 463 |
|
| 464 |
memcpy(tmp, data, len); |
| 465 |
NL_DBG(2, "msg %p: Appended %zu bytes with padding %d\n", n, len, pad); |
| 466 |
|
| 467 |
return 0; |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* Expand maximum payload size of a netlink message |
| 472 |
* @arg n Netlink message. |
| 473 |
* @arg newlen New maximum payload size. |
| 474 |
* |
| 475 |
* Reallocates the payload section of a netlink message and increases |
| 476 |
* the maximum payload size of the message. |
| 477 |
* |
| 478 |
* @note Any pointers pointing to old payload block will be stale and |
| 479 |
* need to be refetched. Therfore, do not expand while constructing |
| 480 |
* nested attributes or while reserved data blocks are held. |
| 481 |
* |
| 482 |
* @return 0 on success or a negative error code. |
| 483 |
*/ |
| 484 |
int nlmsg_expand(struct nl_msg *n, size_t newlen) |
| 485 |
{ |
| 486 |
void *tmp; |
| 487 |
|
| 488 |
if (newlen <= n->nm_size) |
| 489 |
return -NLE_INVAL; |
| 490 |
|
| 491 |
tmp = realloc(n->nm_nlh, newlen); |
| 492 |
if (tmp == NULL) |
| 493 |
return -NLE_NOMEM; |
| 494 |
|
| 495 |
n->nm_nlh = tmp; |
| 496 |
n->nm_size = newlen; |
| 497 |
|
| 498 |
return 0; |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Add a netlink message header to a netlink message |
| 503 |
* @arg n netlink message |
| 504 |
* @arg pid netlink process id or NL_AUTO_PID |
| 505 |
* @arg seq sequence number of message or NL_AUTO_SEQ |
| 506 |
* @arg type message type |
| 507 |
* @arg payload length of message payload |
| 508 |
* @arg flags message flags |
| 509 |
* |
| 510 |
* Adds or overwrites the netlink message header in an existing message |
| 511 |
* object. If \a payload is greater-than zero additional room will be |
| 512 |
* reserved, f.e. for family specific headers. It can be accesed via |
| 513 |
* nlmsg_data(). |
| 514 |
* |
| 515 |
* @return A pointer to the netlink message header or NULL. |
| 516 |
*/ |
| 517 |
struct nlmsghdr *nlmsg_put(struct nl_msg *n, uint32_t pid, uint32_t seq, |
| 518 |
int type, int payload, int flags) |
| 519 |
{ |
| 520 |
struct nlmsghdr *nlh; |
| 521 |
|
| 522 |
if (n->nm_nlh->nlmsg_len < NLMSG_HDRLEN) |
| 523 |
BUG(); |
| 524 |
|
| 525 |
nlh = (struct nlmsghdr *) n->nm_nlh; |
| 526 |
nlh->nlmsg_type = type; |
| 527 |
nlh->nlmsg_flags = flags; |
| 528 |
nlh->nlmsg_pid = pid; |
| 529 |
nlh->nlmsg_seq = seq; |
| 530 |
|
| 531 |
NL_DBG(2, "msg %p: Added netlink header type=%d, flags=%d, pid=%d, " |
| 532 |
"seq=%d\n", n, type, flags, pid, seq); |
| 533 |
|
| 534 |
if (payload > 0 && |
| 535 |
nlmsg_reserve(n, payload, NLMSG_ALIGNTO) == NULL) |
| 536 |
return NULL; |
| 537 |
|
| 538 |
return nlh; |
| 539 |
} |
| 540 |
|
| 541 |
/** |
| 542 |
* Return actual netlink message |
| 543 |
* @arg n netlink message |
| 544 |
* |
| 545 |
* Returns the actual netlink message casted to the type of the netlink |
| 546 |
* message header. |
| 547 |
* |
| 548 |
* @return A pointer to the netlink message. |
| 549 |
*/ |
| 550 |
struct nlmsghdr *nlmsg_hdr(struct nl_msg *n) |
| 551 |
{ |
| 552 |
return n->nm_nlh; |
| 553 |
} |
| 554 |
|
| 555 |
/** |
| 556 |
* Acquire a reference on a netlink message |
| 557 |
* @arg msg message to acquire reference from |
| 558 |
*/ |
| 559 |
void nlmsg_get(struct nl_msg *msg) |
| 560 |
{ |
| 561 |
msg->nm_refcnt++; |
| 562 |
NL_DBG(4, "New reference to message %p, total %d\n", |
| 563 |
msg, msg->nm_refcnt); |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* Release a reference from an netlink message |
| 568 |
* @arg msg message to release reference from |
| 569 |
* |
| 570 |
* Frees memory after the last reference has been released. |
| 571 |
*/ |
| 572 |
void nlmsg_free(struct nl_msg *msg) |
| 573 |
{ |
| 574 |
if (!msg) |
| 575 |
return; |
| 576 |
|
| 577 |
msg->nm_refcnt--; |
| 578 |
NL_DBG(4, "Returned message reference %p, %d remaining\n", |
| 579 |
msg, msg->nm_refcnt); |
| 580 |
|
| 581 |
if (msg->nm_refcnt < 0) |
| 582 |
BUG(); |
| 583 |
|
| 584 |
if (msg->nm_refcnt <= 0) { |
| 585 |
free(msg->nm_nlh); |
| 586 |
NL_DBG(2, "msg %p: Freed\n", msg); |
| 587 |
free(msg); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
/** @} */ |
| 592 |
|
| 593 |
/** |
| 594 |
* @name Attributes |
| 595 |
* @{ |
| 596 |
*/ |
| 597 |
|
| 598 |
void nlmsg_set_proto(struct nl_msg *msg, int protocol) |
| 599 |
{ |
| 600 |
msg->nm_protocol = protocol; |
| 601 |
} |
| 602 |
|
| 603 |
int nlmsg_get_proto(struct nl_msg *msg) |
| 604 |
{ |
| 605 |
return msg->nm_protocol; |
| 606 |
} |
| 607 |
|
| 608 |
size_t nlmsg_get_max_size(struct nl_msg *msg) |
| 609 |
{ |
| 610 |
return msg->nm_size; |
| 611 |
} |
| 612 |
|
| 613 |
void nlmsg_set_src(struct nl_msg *msg, struct sockaddr_nl *addr) |
| 614 |
{ |
| 615 |
memcpy(&msg->nm_src, addr, sizeof(*addr)); |
| 616 |
} |
| 617 |
|
| 618 |
struct sockaddr_nl *nlmsg_get_src(struct nl_msg *msg) |
| 619 |
{ |
| 620 |
return &msg->nm_src; |
| 621 |
} |
| 622 |
|
| 623 |
void nlmsg_set_dst(struct nl_msg *msg, struct sockaddr_nl *addr) |
| 624 |
{ |
| 625 |
memcpy(&msg->nm_dst, addr, sizeof(*addr)); |
| 626 |
} |
| 627 |
|
| 628 |
struct sockaddr_nl *nlmsg_get_dst(struct nl_msg *msg) |
| 629 |
{ |
| 630 |
return &msg->nm_dst; |
| 631 |
} |
| 632 |
|
| 633 |
void nlmsg_set_creds(struct nl_msg *msg, struct ucred *creds) |
| 634 |
{ |
| 635 |
memcpy(&msg->nm_creds, creds, sizeof(*creds)); |
| 636 |
msg->nm_flags |= NL_MSG_CRED_PRESENT; |
| 637 |
} |
| 638 |
|
| 639 |
struct ucred *nlmsg_get_creds(struct nl_msg *msg) |
| 640 |
{ |
| 641 |
if (msg->nm_flags & NL_MSG_CRED_PRESENT) |
| 642 |
return &msg->nm_creds; |
| 643 |
return NULL; |
| 644 |
} |
| 645 |
|
| 646 |
/** @} */ |
| 647 |
|
| 648 |
/** |
| 649 |
* @name Netlink Message Type Translations |
| 650 |
* @{ |
| 651 |
*/ |
| 652 |
|
| 653 |
static const struct trans_tbl nl_msgtypes[] = { |
| 654 |
__ADD(NLMSG_NOOP,NOOP), |
| 655 |
__ADD(NLMSG_ERROR,ERROR), |
| 656 |
__ADD(NLMSG_DONE,DONE), |
| 657 |
__ADD(NLMSG_OVERRUN,OVERRUN), |
| 658 |
}; |
| 659 |
|
| 660 |
char *nl_nlmsgtype2str(int type, char *buf, size_t size) |
| 661 |
{ |
| 662 |
return __type2str(type, buf, size, nl_msgtypes, |
| 663 |
ARRAY_SIZE(nl_msgtypes)); |
| 664 |
} |
| 665 |
|
| 666 |
int nl_str2nlmsgtype(const char *name) |
| 667 |
{ |
| 668 |
return __str2type(name, nl_msgtypes, ARRAY_SIZE(nl_msgtypes)); |
| 669 |
} |
| 670 |
|
| 671 |
/** @} */ |
| 672 |
|
| 673 |
/** |
| 674 |
* @name Netlink Message Flags Translations |
| 675 |
* @{ |
| 676 |
*/ |
| 677 |
|
| 678 |
char *nl_nlmsg_flags2str(int flags, char *buf, size_t len) |
| 679 |
{ |
| 680 |
memset(buf, 0, len); |
| 681 |
|
| 682 |
#define PRINT_FLAG(f) \ |
| 683 |
if (flags & NLM_F_##f) { \ |
| 684 |
flags &= ~NLM_F_##f; \ |
| 685 |
strncat(buf, #f, len - strlen(buf) - 1); \ |
| 686 |
if (flags) \ |
| 687 |
strncat(buf, ",", len - strlen(buf) - 1); \ |
| 688 |
} |
| 689 |
|
| 690 |
PRINT_FLAG(REQUEST); |
| 691 |
PRINT_FLAG(MULTI); |
| 692 |
PRINT_FLAG(ACK); |
| 693 |
PRINT_FLAG(ECHO); |
| 694 |
PRINT_FLAG(ROOT); |
| 695 |
PRINT_FLAG(MATCH); |
| 696 |
PRINT_FLAG(ATOMIC); |
| 697 |
PRINT_FLAG(REPLACE); |
| 698 |
PRINT_FLAG(EXCL); |
| 699 |
PRINT_FLAG(CREATE); |
| 700 |
PRINT_FLAG(APPEND); |
| 701 |
|
| 702 |
if (flags) { |
| 703 |
char s[32]; |
| 704 |
snprintf(s, sizeof(s), "0x%x", flags); |
| 705 |
strncat(buf, s, len - strlen(buf) - 1); |
| 706 |
} |
| 707 |
#undef PRINT_FLAG |
| 708 |
|
| 709 |
return buf; |
| 710 |
} |
| 711 |
|
| 712 |
/** @} */ |
| 713 |
|
| 714 |
/** |
| 715 |
* @name Direct Parsing |
| 716 |
* @{ |
| 717 |
*/ |
| 718 |
|
| 719 |
/** @cond SKIP */ |
| 720 |
struct dp_xdata { |
| 721 |
void (*cb)(struct nl_object *, void *); |
| 722 |
void *arg; |
| 723 |
}; |
| 724 |
/** @endcond */ |
| 725 |
|
| 726 |
static int parse_cb(struct nl_object *obj, struct nl_parser_param *p) |
| 727 |
{ |
| 728 |
struct dp_xdata *x = p->pp_arg; |
| 729 |
|
| 730 |
x->cb(obj, x->arg); |
| 731 |
return 0; |
| 732 |
} |
| 733 |
|
| 734 |
int nl_msg_parse(struct nl_msg *msg, void (*cb)(struct nl_object *, void *), |
| 735 |
void *arg) |
| 736 |
{ |
| 737 |
struct nl_cache_ops *ops; |
| 738 |
struct nl_parser_param p = { |
| 739 |
.pp_cb = parse_cb |
| 740 |
}; |
| 741 |
struct dp_xdata x = { |
| 742 |
.cb = cb, |
| 743 |
.arg = arg, |
| 744 |
}; |
| 745 |
int err; |
| 746 |
|
| 747 |
ops = nl_cache_ops_associate_safe(nlmsg_get_proto(msg), |
| 748 |
nlmsg_hdr(msg)->nlmsg_type); |
| 749 |
if (ops == NULL) |
| 750 |
return -NLE_MSGTYPE_NOSUPPORT; |
| 751 |
p.pp_arg = &x; |
| 752 |
|
| 753 |
err = nl_cache_parse(ops, NULL, nlmsg_hdr(msg), &p); |
| 754 |
nl_cache_ops_put(ops); |
| 755 |
|
| 756 |
return err; |
| 757 |
} |
| 758 |
|
| 759 |
/** @} */ |
| 760 |
|
| 761 |
/** |
| 762 |
* @name Dumping |
| 763 |
* @{ |
| 764 |
*/ |
| 765 |
|
| 766 |
static void prefix_line(FILE *ofd, int prefix) |
| 767 |
{ |
| 768 |
int i; |
| 769 |
|
| 770 |
for (i = 0; i < prefix; i++) |
| 771 |
fprintf(ofd, " "); |
| 772 |
} |
| 773 |
|
| 774 |
static inline void dump_hex(FILE *ofd, char *start, int len, int prefix) |
| 775 |
{ |
| 776 |
int i, a, c, limit; |
| 777 |
char ascii[21] = {0}; |
| 778 |
|
| 779 |
limit = 16 - (prefix * 2); |
| 780 |
prefix_line(ofd, prefix); |
| 781 |
fprintf(ofd, " "); |
| 782 |
|
| 783 |
for (i = 0, a = 0, c = 0; i < len; i++) { |
| 784 |
int v = *(uint8_t *) (start + i); |
| 785 |
|
| 786 |
fprintf(ofd, "%02x ", v); |
| 787 |
ascii[a++] = isprint(v) ? v : '.'; |
| 788 |
|
| 789 |
if (++c >= limit) { |
| 790 |
fprintf(ofd, "%s\n", ascii); |
| 791 |
if (i < (len - 1)) { |
| 792 |
prefix_line(ofd, prefix); |
| 793 |
fprintf(ofd, " "); |
| 794 |
} |
| 795 |
a = c = 0; |
| 796 |
memset(ascii, 0, sizeof(ascii)); |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
if (c != 0) { |
| 801 |
for (i = 0; i < (limit - c); i++) |
| 802 |
fprintf(ofd, " "); |
| 803 |
fprintf(ofd, "%s\n", ascii); |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
static void print_hdr(FILE *ofd, struct nl_msg *msg) |
| 808 |
{ |
| 809 |
struct nlmsghdr *nlh = nlmsg_hdr(msg); |
| 810 |
struct nl_cache_ops *ops; |
| 811 |
struct nl_msgtype *mt; |
| 812 |
char buf[128]; |
| 813 |
|
| 814 |
fprintf(ofd, " .nlmsg_len = %d\n", nlh->nlmsg_len); |
| 815 |
|
| 816 |
ops = nl_cache_ops_associate_safe(nlmsg_get_proto(msg), nlh->nlmsg_type); |
| 817 |
if (ops) { |
| 818 |
mt = nl_msgtype_lookup(ops, nlh->nlmsg_type); |
| 819 |
if (!mt) |
| 820 |
BUG(); |
| 821 |
|
| 822 |
snprintf(buf, sizeof(buf), "%s::%s", ops->co_name, mt->mt_name); |
| 823 |
nl_cache_ops_put(ops); |
| 824 |
} else |
| 825 |
nl_nlmsgtype2str(nlh->nlmsg_type, buf, sizeof(buf)); |
| 826 |
|
| 827 |
fprintf(ofd, " .type = %d <%s>\n", nlh->nlmsg_type, buf); |
| 828 |
fprintf(ofd, " .flags = %d <%s>\n", nlh->nlmsg_flags, |
| 829 |
nl_nlmsg_flags2str(nlh->nlmsg_flags, buf, sizeof(buf))); |
| 830 |
fprintf(ofd, " .seq = %d\n", nlh->nlmsg_seq); |
| 831 |
fprintf(ofd, " .port = %d\n", nlh->nlmsg_pid); |
| 832 |
|
| 833 |
} |
| 834 |
|
| 835 |
static void print_genl_hdr(FILE *ofd, void *start) |
| 836 |
{ |
| 837 |
struct genlmsghdr *ghdr = start; |
| 838 |
|
| 839 |
fprintf(ofd, " [GENERIC NETLINK HEADER] %zu octets\n", GENL_HDRLEN); |
| 840 |
fprintf(ofd, " .cmd = %u\n", ghdr->cmd); |
| 841 |
fprintf(ofd, " .version = %u\n", ghdr->version); |
| 842 |
fprintf(ofd, " .unused = %#x\n", ghdr->reserved); |
| 843 |
} |
| 844 |
|
| 845 |
static void *print_genl_msg(struct nl_msg *msg, FILE *ofd, struct nlmsghdr *hdr, |
| 846 |
struct nl_cache_ops *ops, int *payloadlen) |
| 847 |
{ |
| 848 |
char *data = nlmsg_data(hdr); |
| 849 |
|
| 850 |
if (*payloadlen < (int)GENL_HDRLEN) |
| 851 |
return data; |
| 852 |
|
| 853 |
print_genl_hdr(ofd, data); |
| 854 |
|
| 855 |
*payloadlen -= GENL_HDRLEN; |
| 856 |
data += GENL_HDRLEN; |
| 857 |
|
| 858 |
if (ops) { |
| 859 |
int hdrsize = ops->co_hdrsize - GENL_HDRLEN; |
| 860 |
|
| 861 |
if (hdrsize > 0) { |
| 862 |
if (*payloadlen < hdrsize) |
| 863 |
return data; |
| 864 |
|
| 865 |
fprintf(ofd, " [HEADER] %d octets\n", hdrsize); |
| 866 |
dump_hex(ofd, data, hdrsize, 0); |
| 867 |
|
| 868 |
*payloadlen -= hdrsize; |
| 869 |
data += hdrsize; |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
return data; |
| 874 |
} |
| 875 |
|
| 876 |
static void dump_attr(FILE *ofd, struct nlattr *attr, int prefix) |
| 877 |
{ |
| 878 |
int len = nla_len(attr); |
| 879 |
|
| 880 |
dump_hex(ofd, nla_data(attr), len, prefix); |
| 881 |
} |
| 882 |
|
| 883 |
static void dump_attrs(FILE *ofd, struct nlattr *attrs, int attrlen, |
| 884 |
int prefix) |
| 885 |
{ |
| 886 |
int rem; |
| 887 |
struct nlattr *nla; |
| 888 |
|
| 889 |
nla_for_each_attr(nla, attrs, attrlen, rem) { |
| 890 |
int padlen, alen = nla_len(nla); |
| 891 |
|
| 892 |
prefix_line(ofd, prefix); |
| 893 |
|
| 894 |
if (nla->nla_type == 0) |
| 895 |
fprintf(ofd, " [ATTR PADDING] %d octets\n", alen); |
| 896 |
else |
| 897 |
fprintf(ofd, " [ATTR %02d%s] %d octets\n", nla_type(nla), |
| 898 |
nla_is_nested(nla) ? " NESTED" : "", |
| 899 |
alen); |
| 900 |
|
| 901 |
if (nla_is_nested(nla)) |
| 902 |
dump_attrs(ofd, nla_data(nla), alen, prefix+1); |
| 903 |
else |
| 904 |
dump_attr(ofd, nla, prefix); |
| 905 |
|
| 906 |
padlen = nla_padlen(alen); |
| 907 |
if (padlen > 0) { |
| 908 |
prefix_line(ofd, prefix); |
| 909 |
fprintf(ofd, " [PADDING] %d octets\n", |
| 910 |
padlen); |
| 911 |
dump_hex(ofd, (char *) nla_data(nla) + alen, |
| 912 |
padlen, prefix); |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
if (rem) { |
| 917 |
prefix_line(ofd, prefix); |
| 918 |
fprintf(ofd, " [LEFTOVER] %d octets\n", rem); |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
static void dump_error_msg(struct nl_msg *msg, FILE *ofd) |
| 923 |
{ |
| 924 |
struct nlmsghdr *hdr = nlmsg_hdr(msg); |
| 925 |
struct nlmsgerr *err = nlmsg_data(hdr); |
| 926 |
int l; |
| 927 |
|
| 928 |
fprintf(ofd, " [ERRORMSG] %zu octets\n", sizeof(*err)); |
| 929 |
|
| 930 |
l = nlmsg_len(hdr); |
| 931 |
if (l >= 0 && ((unsigned)l) >= sizeof(*err)) { |
| 932 |
struct nl_msg *errmsg; |
| 933 |
|
| 934 |
fprintf(ofd, " .error = %d \"%s\"\n", err->error, |
| 935 |
nl_strerror_l(-err->error)); |
| 936 |
fprintf(ofd, " [ORIGINAL MESSAGE] %zu octets\n", sizeof(*hdr)); |
| 937 |
|
| 938 |
errmsg = nlmsg_inherit(&err->msg); |
| 939 |
print_hdr(ofd, errmsg); |
| 940 |
nlmsg_free(errmsg); |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
static void print_msg(struct nl_msg *msg, FILE *ofd, struct nlmsghdr *hdr) |
| 945 |
{ |
| 946 |
struct nl_cache_ops *ops; |
| 947 |
int payloadlen = nlmsg_len(hdr); |
| 948 |
int attrlen = 0; |
| 949 |
void *data; |
| 950 |
|
| 951 |
data = nlmsg_data(hdr); |
| 952 |
ops = nl_cache_ops_associate_safe(nlmsg_get_proto(msg), |
| 953 |
hdr->nlmsg_type); |
| 954 |
if (ops) { |
| 955 |
attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize); |
| 956 |
payloadlen -= attrlen; |
| 957 |
} |
| 958 |
|
| 959 |
if (msg->nm_protocol == NETLINK_GENERIC) |
| 960 |
data = print_genl_msg(msg, ofd, hdr, ops, &payloadlen); |
| 961 |
|
| 962 |
if (payloadlen) { |
| 963 |
fprintf(ofd, " [PAYLOAD] %d octets\n", payloadlen); |
| 964 |
dump_hex(ofd, data, payloadlen, 0); |
| 965 |
} |
| 966 |
|
| 967 |
if (attrlen) { |
| 968 |
struct nlattr *attrs; |
| 969 |
int attrlen; |
| 970 |
|
| 971 |
attrs = nlmsg_attrdata(hdr, ops->co_hdrsize); |
| 972 |
attrlen = nlmsg_attrlen(hdr, ops->co_hdrsize); |
| 973 |
dump_attrs(ofd, attrs, attrlen, 0); |
| 974 |
} |
| 975 |
|
| 976 |
if (ops) |
| 977 |
nl_cache_ops_put(ops); |
| 978 |
} |
| 979 |
|
| 980 |
/** |
| 981 |
* Dump message in human readable format to file descriptor |
| 982 |
* @arg msg Message to print |
| 983 |
* @arg ofd File descriptor. |
| 984 |
*/ |
| 985 |
void nl_msg_dump(struct nl_msg *msg, FILE *ofd) |
| 986 |
{ |
| 987 |
struct nlmsghdr *hdr = nlmsg_hdr(msg); |
| 988 |
|
| 989 |
fprintf(ofd, |
| 990 |
"-------------------------- BEGIN NETLINK MESSAGE ---------------------------\n"); |
| 991 |
|
| 992 |
fprintf(ofd, " [NETLINK HEADER] %zu octets\n", sizeof(struct nlmsghdr)); |
| 993 |
print_hdr(ofd, msg); |
| 994 |
|
| 995 |
if (hdr->nlmsg_type == NLMSG_ERROR) |
| 996 |
dump_error_msg(msg, ofd); |
| 997 |
else if (nlmsg_len(hdr) > 0) |
| 998 |
print_msg(msg, ofd, hdr); |
| 999 |
|
| 1000 |
fprintf(ofd, |
| 1001 |
"--------------------------- END NETLINK MESSAGE ---------------------------\n"); |
| 1002 |
} |
| 1003 |
|
| 1004 |
/** @} */ |
| 1005 |
|
| 1006 |
/** @} */ |