1 /* 2 * lib/route/addr.c Addresses 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation version 2.1 7 * of the License. 8 * 9 * Copyright (c) 2003-2012 Thomas Graf <tgraf (at) suug.ch> 10 * Copyright (c) 2003-2006 Baruch Even <baruch (at) ev-en.org>, 11 * Mediatrix Telecom, inc. <ericb (at) mediatrix.com> 12 */ 13 14 /** 15 * @ingroup rtnl 16 * @defgroup rtaddr Addresses 17 * @brief 18 * 19 * @note The maximum size of an address label is IFNAMSIZ. 20 * 21 * @note The address may not contain a prefix length if the peer address 22 * has been specified already. 23 * 24 * @par 1) Address Addition 25 * @code 26 * // Allocate an empty address object to be filled out with the attributes 27 * // of the new address. 28 * struct rtnl_addr *addr = rtnl_addr_alloc(); 29 * 30 * // Fill out the mandatory attributes of the new address. Setting the 31 * // local address will automatically set the address family and the 32 * // prefix length to the correct values. 33 * rtnl_addr_set_ifindex(addr, ifindex); 34 * rtnl_addr_set_local(addr, local_addr); 35 * 36 * // The label of the address can be specified, currently only supported 37 * // by IPv4 and DECnet. 38 * rtnl_addr_set_label(addr, "mylabel"); 39 * 40 * // The peer address can be specified if necessary, in either case a peer 41 * // address will be sent to the kernel in order to fullfil the interface 42 * // requirements. If none is set, it will equal the local address. 43 * // Note: Real peer addresses are only supported by IPv4 for now. 44 * rtnl_addr_set_peer(addr, peer_addr); 45 * 46 * // In case you want to have the address have a scope other than global 47 * // it may be overwritten using rtnl_addr_set_scope(). The scope currently 48 * // cannot be set for IPv6 addresses. 49 * rtnl_addr_set_scope(addr, rtnl_str2scope("site")); 50 * 51 * // Broadcast address may be specified using the relevant 52 * // functions, the address family will be verified if one of the other 53 * // addresses has been set already. Currently only works for IPv4. 54 * rtnl_addr_set_broadcast(addr, broadcast_addr); 55 * 56 * // Build the netlink message and send it to the kernel, the operation will 57 * // block until the operation has been completed. Alternatively the required 58 * // netlink message can be built using rtnl_addr_build_add_request() to be 59 * // sent out using nl_send_auto_complete(). 60 * rtnl_addr_add(sk, addr, 0); 61 * 62 * // Free the memory 63 * rtnl_addr_put(addr); 64 * @endcode 65 * 66 * @par 2) Address Deletion 67 * @code 68 * // Allocate an empty address object to be filled out with the attributes 69 * // matching the address to be deleted. Alternatively a fully equipped 70 * // address object out of a cache can be used instead. 71 * struct rtnl_addr *addr = rtnl_addr_alloc(); 72 * 73 * // The only mandatory parameter besides the address family is the interface 74 * // index the address is on, i.e. leaving out all other parameters will 75 * // result in all addresses of the specified address family interface tuple 76 * // to be deleted. 77 * rtnl_addr_set_ifindex(addr, ifindex); 78 * 79 * // Specyfing the address family manually is only required if neither the 80 * // local nor peer address have been specified. 81 * rtnl_addr_set_family(addr, AF_INET); 82 * 83 * // Specyfing the local address is optional but the best choice to delete 84 * // specific addresses. 85 * rtnl_addr_set_local(addr, local_addr); 86 * 87 * // The label of the address can be specified, currently only supported 88 * // by IPv4 and DECnet. 89 * rtnl_addr_set_label(addr, "mylabel"); 90 * 91 * // The peer address can be specified if necessary, in either case a peer 92 * // address will be sent to the kernel in order to fullfil the interface 93 * // requirements. If none is set, it will equal the local address. 94 * // Note: Real peer addresses are only supported by IPv4 for now. 95 * rtnl_addr_set_peer(addr, peer_addr); 96 * 97 * // Build the netlink message and send it to the kernel, the operation will 98 * // block until the operation has been completed. Alternatively the required 99 * // netlink message can be built using rtnl_addr_build_delete_request() 100 * // to be sent out using nl_send_auto_complete(). 101 * rtnl_addr_delete(sk, addr, 0); 102 * 103 * // Free the memory 104 * rtnl_addr_put(addr); 105 * @endcode 106 * @{ 107 */ 108 109 #include <netlink-private/netlink.h> 110 #include <netlink/netlink.h> 111 #include <netlink/route/rtnl.h> 112 #include <netlink/route/addr.h> 113 #include <netlink/route/route.h> 114 #include <netlink/route/link.h> 115 #include <netlink/utils.h> 116 117 /** @cond SKIP */ 118 #define ADDR_ATTR_FAMILY 0x0001 119 #define ADDR_ATTR_PREFIXLEN 0x0002 120 #define ADDR_ATTR_FLAGS 0x0004 121 #define ADDR_ATTR_SCOPE 0x0008 122 #define ADDR_ATTR_IFINDEX 0x0010 123 #define ADDR_ATTR_LABEL 0x0020 124 #define ADDR_ATTR_CACHEINFO 0x0040 125 #define ADDR_ATTR_PEER 0x0080 126 #define ADDR_ATTR_LOCAL 0x0100 127 #define ADDR_ATTR_BROADCAST 0x0200 128 #define ADDR_ATTR_MULTICAST 0x0400 129 #define ADDR_ATTR_ANYCAST 0x0800 130 131 static struct nl_cache_ops rtnl_addr_ops; 132 static struct nl_object_ops addr_obj_ops; 133 /** @endcond */ 134 135 static void addr_constructor(struct nl_object *obj) 136 { 137 struct rtnl_addr *addr = nl_object_priv(obj); 138 139 addr->a_scope = RT_SCOPE_NOWHERE; 140 } 141 142 static void addr_free_data(struct nl_object *obj) 143 { 144 struct rtnl_addr *addr = nl_object_priv(obj); 145 146 if (!addr) 147 return; 148 149 nl_addr_put(addr->a_peer); 150 nl_addr_put(addr->a_local); 151 nl_addr_put(addr->a_bcast); 152 nl_addr_put(addr->a_multicast); 153 nl_addr_put(addr->a_anycast); 154 rtnl_link_put(addr->a_link); 155 } 156 157 static int addr_clone(struct nl_object *_dst, struct nl_object *_src) 158 { 159 struct rtnl_addr *dst = nl_object_priv(_dst); 160 struct rtnl_addr *src = nl_object_priv(_src); 161 162 if (src->a_link) { 163 nl_object_get(OBJ_CAST(src->a_link)); 164 dst->a_link = src->a_link; 165 } 166 167 if (src->a_peer) 168 if (!(dst->a_peer = nl_addr_clone(src->a_peer))) 169 return -NLE_NOMEM; 170 171 if (src->a_local) 172 if (!(dst->a_local = nl_addr_clone(src->a_local))) 173 return -NLE_NOMEM; 174 175 if (src->a_bcast) 176 if (!(dst->a_bcast = nl_addr_clone(src->a_bcast))) 177 return -NLE_NOMEM; 178 179 if (src->a_multicast) 180 if (!(dst->a_multicast = nl_addr_clone(src->a_multicast))) 181 return -NLE_NOMEM; 182 183 if (src->a_anycast) 184 if (!(dst->a_anycast = nl_addr_clone(src->a_anycast))) 185 return -NLE_NOMEM; 186 187 return 0; 188 } 189 190 static struct nla_policy addr_policy[IFA_MAX+1] = { 191 [IFA_LABEL] = { .type = NLA_STRING, 192 .maxlen = IFNAMSIZ }, 193 [IFA_CACHEINFO] = { .minlen = sizeof(struct ifa_cacheinfo) }, 194 }; 195 196 static int addr_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who, 197 struct nlmsghdr *nlh, struct nl_parser_param *pp) 198 { 199 struct rtnl_addr *addr; 200 struct ifaddrmsg *ifa; 201 struct nlattr *tb[IFA_MAX+1]; 202 int err, family; 203 struct nl_cache *link_cache; 204 struct nl_addr *plen_addr = NULL; 205 206 addr = rtnl_addr_alloc(); 207 if (!addr) 208 return -NLE_NOMEM; 209 210 addr->ce_msgtype = nlh->nlmsg_type; 211 212 err = nlmsg_parse(nlh, sizeof(*ifa), tb, IFA_MAX, addr_policy); 213 if (err < 0) 214 goto errout; 215 216 ifa = nlmsg_data(nlh); 217 addr->a_family = family = ifa->ifa_family; 218 addr->a_prefixlen = ifa->ifa_prefixlen; 219 addr->a_scope = ifa->ifa_scope; 220 addr->a_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : 221 ifa->ifa_flags; 222 addr->a_ifindex = ifa->ifa_index; 223 224 addr->ce_mask = (ADDR_ATTR_FAMILY | ADDR_ATTR_PREFIXLEN | 225 ADDR_ATTR_FLAGS | ADDR_ATTR_SCOPE | ADDR_ATTR_IFINDEX); 226 227 if (tb[IFA_LABEL]) { 228 nla_strlcpy(addr->a_label, tb[IFA_LABEL], IFNAMSIZ); 229 addr->ce_mask |= ADDR_ATTR_LABEL; 230 } 231 232 /* IPv6 only */ 233 if (tb[IFA_CACHEINFO]) { 234 struct ifa_cacheinfo *ca; 235 236 ca = nla_data(tb[IFA_CACHEINFO]); 237 addr->a_cacheinfo.aci_prefered = ca->ifa_prefered; 238 addr->a_cacheinfo.aci_valid = ca->ifa_valid; 239 addr->a_cacheinfo.aci_cstamp = ca->cstamp; 240 addr->a_cacheinfo.aci_tstamp = ca->tstamp; 241 addr->ce_mask |= ADDR_ATTR_CACHEINFO; 242 } 243 244 if (tb[IFA_LOCAL]) { 245 addr->a_local = nl_addr_alloc_attr(tb[IFA_LOCAL], family); 246 if (!addr->a_local) 247 goto errout_nomem; 248 addr->ce_mask |= ADDR_ATTR_LOCAL; 249 plen_addr = addr->a_local; 250 } 251 252 if (tb[IFA_ADDRESS]) { 253 struct nl_addr *a; 254 255 a = nl_addr_alloc_attr(tb[IFA_ADDRESS], family); 256 if (!a) 257 goto errout_nomem; 258 259 /* IPv6 sends the local address as IFA_ADDRESS with 260 * no IFA_LOCAL, IPv4 sends both IFA_LOCAL and IFA_ADDRESS 261 * with IFA_ADDRESS being the peer address if they differ */ 262 if (!tb[IFA_LOCAL] || !nl_addr_cmp(a, addr->a_local)) { 263 nl_addr_put(addr->a_local); 264 addr->a_local = a; 265 addr->ce_mask |= ADDR_ATTR_LOCAL; 266 } else { 267 addr->a_peer = a; 268 addr->ce_mask |= ADDR_ATTR_PEER; 269 } 270 271 plen_addr = a; 272 } 273 274 if (plen_addr) 275 nl_addr_set_prefixlen(plen_addr, addr->a_prefixlen); 276 277 /* IPv4 only */ 278 if (tb[IFA_BROADCAST]) { 279 addr->a_bcast = nl_addr_alloc_attr(tb[IFA_BROADCAST], family); 280 if (!addr->a_bcast) 281 goto errout_nomem; 282 283 addr->ce_mask |= ADDR_ATTR_BROADCAST; 284 } 285 286 /* IPv6 only */ 287 if (tb[IFA_MULTICAST]) { 288 addr->a_multicast = nl_addr_alloc_attr(tb[IFA_MULTICAST], 289 family); 290 if (!addr->a_multicast) 291 goto errout_nomem; 292 293 addr->ce_mask |= ADDR_ATTR_MULTICAST; 294 } 295 296 /* IPv6 only */ 297 if (tb[IFA_ANYCAST]) { 298 addr->a_anycast = nl_addr_alloc_attr(tb[IFA_ANYCAST], 299 family); 300 if (!addr->a_anycast) 301 goto errout_nomem; 302 303 addr->ce_mask |= ADDR_ATTR_ANYCAST; 304 } 305 306 if ((link_cache = __nl_cache_mngt_require("route/link"))) { 307 struct rtnl_link *link; 308 309 if ((link = rtnl_link_get(link_cache, addr->a_ifindex))) { 310 rtnl_addr_set_link(addr, link); 311 312 /* rtnl_addr_set_link incs refcnt */ 313 rtnl_link_put(link); 314 } 315 } 316 317 err = pp->pp_cb((struct nl_object *) addr, pp); 318 errout: 319 rtnl_addr_put(addr); 320 321 return err; 322 323 errout_nomem: 324 err = -NLE_NOMEM; 325 goto errout; 326 } 327 328 static int addr_request_update(struct nl_cache *cache, struct nl_sock *sk) 329 { 330 return nl_rtgen_request(sk, RTM_GETADDR, AF_UNSPEC, NLM_F_DUMP); 331 } 332 333 static void addr_dump_line(struct nl_object *obj, struct nl_dump_params *p) 334 { 335 struct rtnl_addr *addr = (struct rtnl_addr *) obj; 336 struct nl_cache *link_cache; 337 char buf[128]; 338 339 link_cache = nl_cache_mngt_require_safe("route/link"); 340 341 if (addr->ce_mask & ADDR_ATTR_LOCAL) 342 nl_dump_line(p, "%s", 343 nl_addr2str(addr->a_local, buf, sizeof(buf))); 344 else 345 nl_dump_line(p, "none"); 346 347 if (addr->ce_mask & ADDR_ATTR_PEER) 348 nl_dump(p, " peer %s", 349 nl_addr2str(addr->a_peer, buf, sizeof(buf))); 350 351 nl_dump(p, " %s ", nl_af2str(addr->a_family, buf, sizeof(buf))); 352 353 if (link_cache) 354 nl_dump(p, "dev %s ", 355 rtnl_link_i2name(link_cache, addr->a_ifindex, 356 buf, sizeof(buf))); 357 else 358 nl_dump(p, "dev %d ", addr->a_ifindex); 359 360 nl_dump(p, "scope %s", 361 rtnl_scope2str(addr->a_scope, buf, sizeof(buf))); 362 363 rtnl_addr_flags2str(addr->a_flags, buf, sizeof(buf)); 364 if (buf[0]) 365 nl_dump(p, " <%s>", buf); 366 367 nl_dump(p, "\n"); 368 369 if (link_cache) 370 nl_cache_put(link_cache); 371 } 372 373 static void addr_dump_details(struct nl_object *obj, struct nl_dump_params *p) 374 { 375 struct rtnl_addr *addr = (struct rtnl_addr *) obj; 376 char buf[128]; 377 378 addr_dump_line(obj, p); 379 380 if (addr->ce_mask & (ADDR_ATTR_LABEL | ADDR_ATTR_BROADCAST | 381 ADDR_ATTR_MULTICAST)) { 382 nl_dump_line(p, " "); 383 384 if (addr->ce_mask & ADDR_ATTR_LABEL) 385 nl_dump(p, " label %s", addr->a_label); 386 387 if (addr->ce_mask & ADDR_ATTR_BROADCAST) 388 nl_dump(p, " broadcast %s", 389 nl_addr2str(addr->a_bcast, buf, sizeof(buf))); 390 391 if (addr->ce_mask & ADDR_ATTR_MULTICAST) 392 nl_dump(p, " multicast %s", 393 nl_addr2str(addr->a_multicast, buf, 394 sizeof(buf))); 395 396 if (addr->ce_mask & ADDR_ATTR_ANYCAST) 397 nl_dump(p, " anycast %s", 398 nl_addr2str(addr->a_anycast, buf, 399 sizeof(buf))); 400 401 nl_dump(p, "\n"); 402 } 403 404 if (addr->ce_mask & ADDR_ATTR_CACHEINFO) { 405 struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo; 406 407 nl_dump_line(p, " valid-lifetime %s", 408 ci->aci_valid == 0xFFFFFFFFU ? "forever" : 409 nl_msec2str(ci->aci_valid * 1000, 410 buf, sizeof(buf))); 411 412 nl_dump(p, " preferred-lifetime %s\n", 413 ci->aci_prefered == 0xFFFFFFFFU ? "forever" : 414 nl_msec2str(ci->aci_prefered * 1000, 415 buf, sizeof(buf))); 416 417 nl_dump_line(p, " created boot-time+%s ", 418 nl_msec2str(addr->a_cacheinfo.aci_cstamp * 10, 419 buf, sizeof(buf))); 420 421 nl_dump(p, "last-updated boot-time+%s\n", 422 nl_msec2str(addr->a_cacheinfo.aci_tstamp * 10, 423 buf, sizeof(buf))); 424 } 425 } 426 427 static void addr_dump_stats(struct nl_object *obj, struct nl_dump_params *p) 428 { 429 addr_dump_details(obj, p); 430 } 431 432 static int addr_compare(struct nl_object *_a, struct nl_object *_b, 433 uint32_t attrs, int flags) 434 { 435 struct rtnl_addr *a = (struct rtnl_addr *) _a; 436 struct rtnl_addr *b = (struct rtnl_addr *) _b; 437 int diff = 0; 438 439 #define ADDR_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ADDR_ATTR_##ATTR, a, b, EXPR) 440 441 diff |= ADDR_DIFF(IFINDEX, a->a_ifindex != b->a_ifindex); 442 diff |= ADDR_DIFF(FAMILY, a->a_family != b->a_family); 443 diff |= ADDR_DIFF(SCOPE, a->a_scope != b->a_scope); 444 diff |= ADDR_DIFF(LABEL, strcmp(a->a_label, b->a_label)); 445 diff |= ADDR_DIFF(PEER, nl_addr_cmp(a->a_peer, b->a_peer)); 446 diff |= ADDR_DIFF(LOCAL, nl_addr_cmp(a->a_local, b->a_local)); 447 diff |= ADDR_DIFF(MULTICAST, nl_addr_cmp(a->a_multicast, 448 b->a_multicast)); 449 diff |= ADDR_DIFF(BROADCAST, nl_addr_cmp(a->a_bcast, b->a_bcast)); 450 diff |= ADDR_DIFF(ANYCAST, nl_addr_cmp(a->a_anycast, b->a_anycast)); 451 452 if (flags & LOOSE_COMPARISON) 453 diff |= ADDR_DIFF(FLAGS, 454 (a->a_flags ^ b->a_flags) & b->a_flag_mask); 455 else 456 diff |= ADDR_DIFF(FLAGS, a->a_flags != b->a_flags); 457 458 #undef ADDR_DIFF 459 460 return diff; 461 } 462 463 static const struct trans_tbl addr_attrs[] = { 464 __ADD(ADDR_ATTR_FAMILY, family) 465 __ADD(ADDR_ATTR_PREFIXLEN, prefixlen) 466 __ADD(ADDR_ATTR_FLAGS, flags) 467 __ADD(ADDR_ATTR_SCOPE, scope) 468 __ADD(ADDR_ATTR_IFINDEX, ifindex) 469 __ADD(ADDR_ATTR_LABEL, label) 470 __ADD(ADDR_ATTR_CACHEINFO, cacheinfo) 471 __ADD(ADDR_ATTR_PEER, peer) 472 __ADD(ADDR_ATTR_LOCAL, local) 473 __ADD(ADDR_ATTR_BROADCAST, broadcast) 474 __ADD(ADDR_ATTR_MULTICAST, multicast) 475 }; 476 477 static char *addr_attrs2str(int attrs, char *buf, size_t len) 478 { 479 return __flags2str(attrs, buf, len, addr_attrs, 480 ARRAY_SIZE(addr_attrs)); 481 } 482 483 /** 484 * @name Allocation/Freeing 485 * @{ 486 */ 487 488 struct rtnl_addr *rtnl_addr_alloc(void) 489 { 490 return (struct rtnl_addr *) nl_object_alloc(&addr_obj_ops); 491 } 492 493 void rtnl_addr_put(struct rtnl_addr *addr) 494 { 495 nl_object_put((struct nl_object *) addr); 496 } 497 498 /** @} */ 499 500 /** 501 * @name Cache Management 502 * @{ 503 */ 504 505 int rtnl_addr_alloc_cache(struct nl_sock *sk, struct nl_cache **result) 506 { 507 return nl_cache_alloc_and_fill(&rtnl_addr_ops, sk, result); 508 } 509 510 /** 511 * Search address in cache 512 * @arg cache Address cache 513 * @arg ifindex Interface index of address 514 * @arg addr Local address part 515 * 516 * Searches address cache previously allocated with rtnl_addr_alloc_cache() 517 * for an address with a matching local address. 518 * 519 * The reference counter is incremented before returning the address, therefore 520 * the reference must be given back with rtnl_addr_put() after usage. 521 * 522 * @return Address object or NULL if no match was found. 523 */ 524 struct rtnl_addr *rtnl_addr_get(struct nl_cache *cache, int ifindex, 525 struct nl_addr *addr) 526 { 527 struct rtnl_addr *a; 528 529 if (cache->c_ops != &rtnl_addr_ops) 530 return NULL; 531 532 nl_list_for_each_entry(a, &cache->c_items, ce_list) { 533 if (ifindex && a->a_ifindex != ifindex) 534 continue; 535 536 if (a->ce_mask & ADDR_ATTR_LOCAL && 537 !nl_addr_cmp(a->a_local, addr)) { 538 nl_object_get((struct nl_object *) a); 539 return a; 540 } 541 } 542 543 return NULL; 544 } 545 546 /** @} */ 547 548 static int build_addr_msg(struct rtnl_addr *tmpl, int cmd, int flags, 549 struct nl_msg **result) 550 { 551 struct nl_msg *msg; 552 struct ifaddrmsg am = { 553 .ifa_family = tmpl->a_family, 554 .ifa_index = tmpl->a_ifindex, 555 .ifa_prefixlen = tmpl->a_prefixlen, 556 .ifa_flags = tmpl->a_flags, 557 }; 558 559 if (tmpl->ce_mask & ADDR_ATTR_SCOPE) 560 am.ifa_scope = tmpl->a_scope; 561 else { 562 /* compatibility hack */ 563 if (tmpl->a_family == AF_INET && 564 tmpl->ce_mask & ADDR_ATTR_LOCAL && 565 *((char *) nl_addr_get_binary_addr(tmpl->a_local)) == 127) 566 am.ifa_scope = RT_SCOPE_HOST; 567 else 568 am.ifa_scope = RT_SCOPE_UNIVERSE; 569 } 570 571 msg = nlmsg_alloc_simple(cmd, flags); 572 if (!msg) 573 return -NLE_NOMEM; 574 575 if (nlmsg_append(msg, &am, sizeof(am), NLMSG_ALIGNTO) < 0) 576 goto nla_put_failure; 577 578 if (tmpl->ce_mask & ADDR_ATTR_LOCAL) 579 NLA_PUT_ADDR(msg, IFA_LOCAL, tmpl->a_local); 580 581 if (tmpl->ce_mask & ADDR_ATTR_PEER) 582 NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_peer); 583 else if (tmpl->ce_mask & ADDR_ATTR_LOCAL) 584 NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_local); 585 586 if (tmpl->ce_mask & ADDR_ATTR_LABEL) 587 NLA_PUT_STRING(msg, IFA_LABEL, tmpl->a_label); 588 589 if (tmpl->ce_mask & ADDR_ATTR_BROADCAST) 590 NLA_PUT_ADDR(msg, IFA_BROADCAST, tmpl->a_bcast); 591 592 if (tmpl->ce_mask & ADDR_ATTR_CACHEINFO) { 593 struct ifa_cacheinfo ca = { 594 .ifa_valid = tmpl->a_cacheinfo.aci_valid, 595 .ifa_prefered = tmpl->a_cacheinfo.aci_prefered, 596 }; 597 598 NLA_PUT(msg, IFA_CACHEINFO, sizeof(ca), &ca); 599 } 600 601 if (tmpl->a_flags & ~0xFF) { 602 /* only set the IFA_FLAGS attribute, if they actually contain additional 603 * flags that are not already set to am.ifa_flags. 604 * 605 * Older kernels refuse RTM_NEWADDR and RTM_NEWROUTE messages with EINVAL 606 * if they contain unknown netlink attributes. See net/core/rtnetlink.c, which 607 * was fixed by kernel commit 661d2967b3f1b34eeaa7e212e7b9bbe8ee072b59. 608 * 609 * With this workaround, libnl will function correctly with older kernels, 610 * unless there is a new libnl user that wants to set these flags. In this 611 * case it's up to the user to workaround this issue. */ 612 NLA_PUT_U32(msg, IFA_FLAGS, tmpl->a_flags); 613 } 614 615 *result = msg; 616 return 0; 617 618 nla_put_failure: 619 nlmsg_free(msg); 620 return -NLE_MSGSIZE; 621 } 622 623 /** 624 * @name Addition 625 * @{ 626 */ 627 628 /** 629 * Build netlink request message to request addition of new address 630 * @arg addr Address object representing the new address. 631 * @arg flags Additional netlink message flags. 632 * @arg result Pointer to store resulting message. 633 * 634 * Builds a new netlink message requesting the addition of a new 635 * address. The netlink message header isn't fully equipped with 636 * all relevant fields and must thus be sent out via nl_send_auto_complete() 637 * or supplemented as needed. 638 * 639 * Minimal required attributes: 640 * - interface index (rtnl_addr_set_ifindex()) 641 * - local address (rtnl_addr_set_local()) 642 * 643 * The scope will default to universe except for loopback addresses in 644 * which case a host scope is used if not specified otherwise. 645 * 646 * @note Free the memory after usage using nlmsg_free(). 647 * 648 * @return 0 on success or a negative error code. 649 */ 650 int rtnl_addr_build_add_request(struct rtnl_addr *addr, int flags, 651 struct nl_msg **result) 652 { 653 uint32_t required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY | 654 ADDR_ATTR_PREFIXLEN | ADDR_ATTR_LOCAL; 655 656 if ((addr->ce_mask & required) != required) 657 return -NLE_MISSING_ATTR; 658 659 return build_addr_msg(addr, RTM_NEWADDR, NLM_F_CREATE | flags, result); 660 } 661 662 /** 663 * Request addition of new address 664 * @arg sk Netlink socket. 665 * @arg addr Address object representing the new address. 666 * @arg flags Additional netlink message flags. 667 * 668 * Builds a netlink message by calling rtnl_addr_build_add_request(), 669 * sends the request to the kernel and waits for the next ACK to be 670 * received and thus blocks until the request has been fullfilled. 671 * 672 * @see rtnl_addr_build_add_request() 673 * 674 * @return 0 on sucess or a negative error if an error occured. 675 */ 676 int rtnl_addr_add(struct nl_sock *sk, struct rtnl_addr *addr, int flags) 677 { 678 struct nl_msg *msg; 679 int err; 680 681 if ((err = rtnl_addr_build_add_request(addr, flags, &msg)) < 0) 682 return err; 683 684 err = nl_send_auto_complete(sk, msg); 685 nlmsg_free(msg); 686 if (err < 0) 687 return err; 688 689 return wait_for_ack(sk); 690 } 691 692 /** @} */ 693 694 /** 695 * @name Deletion 696 * @{ 697 */ 698 699 /** 700 * Build a netlink request message to request deletion of an address 701 * @arg addr Address object to be deleteted. 702 * @arg flags Additional netlink message flags. 703 * @arg result Pointer to store resulting message. 704 * 705 * Builds a new netlink message requesting a deletion of an address. 706 * The netlink message header isn't fully equipped with all relevant 707 * fields and must thus be sent out via nl_send_auto_complete() 708 * or supplemented as needed. 709 * 710 * Minimal required attributes: 711 * - interface index (rtnl_addr_set_ifindex()) 712 * - address family (rtnl_addr_set_family()) 713 * 714 * Optional attributes: 715 * - local address (rtnl_addr_set_local()) 716 * - label (rtnl_addr_set_label(), IPv4/DECnet only) 717 * - peer address (rtnl_addr_set_peer(), IPv4 only) 718 * 719 * @note Free the memory after usage using nlmsg_free(). 720 * 721 * @return 0 on success or a negative error code. 722 */ 723 int rtnl_addr_build_delete_request(struct rtnl_addr *addr, int flags, 724 struct nl_msg **result) 725 { 726 uint32_t required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY; 727 728 if ((addr->ce_mask & required) != required) 729 return -NLE_MISSING_ATTR; 730 731 return build_addr_msg(addr, RTM_DELADDR, flags, result); 732 } 733 734 /** 735 * Request deletion of an address 736 * @arg sk Netlink socket. 737 * @arg addr Address object to be deleted. 738 * @arg flags Additional netlink message flags. 739 * 740 * Builds a netlink message by calling rtnl_addr_build_delete_request(), 741 * sends the request to the kernel and waits for the next ACK to be 742 * received and thus blocks until the request has been fullfilled. 743 * 744 * @see rtnl_addr_build_delete_request(); 745 * 746 * @return 0 on sucess or a negative error if an error occured. 747 */ 748 int rtnl_addr_delete(struct nl_sock *sk, struct rtnl_addr *addr, int flags) 749 { 750 struct nl_msg *msg; 751 int err; 752 753 if ((err = rtnl_addr_build_delete_request(addr, flags, &msg)) < 0) 754 return err; 755 756 err = nl_send_auto_complete(sk, msg); 757 nlmsg_free(msg); 758 if (err < 0) 759 return err; 760 761 return wait_for_ack(sk); 762 } 763 764 /** @} */ 765 766 /** 767 * @name Attributes 768 * @{ 769 */ 770 771 int rtnl_addr_set_label(struct rtnl_addr *addr, const char *label) 772 { 773 if (strlen(label) > sizeof(addr->a_label) - 1) 774 return -NLE_RANGE; 775 776 strcpy(addr->a_label, label); 777 addr->ce_mask |= ADDR_ATTR_LABEL; 778 779 return 0; 780 } 781 782 char *rtnl_addr_get_label(struct rtnl_addr *addr) 783 { 784 if (addr->ce_mask & ADDR_ATTR_LABEL) 785 return addr->a_label; 786 else 787 return NULL; 788 } 789 790 void rtnl_addr_set_ifindex(struct rtnl_addr *addr, int ifindex) 791 { 792 addr->a_ifindex = ifindex; 793 addr->ce_mask |= ADDR_ATTR_IFINDEX; 794 } 795 796 int rtnl_addr_get_ifindex(struct rtnl_addr *addr) 797 { 798 return addr->a_ifindex; 799 } 800 801 void rtnl_addr_set_link(struct rtnl_addr *addr, struct rtnl_link *link) 802 { 803 rtnl_link_put(addr->a_link); 804 805 if (!link) 806 return; 807 808 nl_object_get(OBJ_CAST(link)); 809 addr->a_link = link; 810 addr->a_ifindex = link->l_index; 811 addr->ce_mask |= ADDR_ATTR_IFINDEX; 812 } 813 814 struct rtnl_link *rtnl_addr_get_link(struct rtnl_addr *addr) 815 { 816 if (addr->a_link) { 817 nl_object_get(OBJ_CAST(addr->a_link)); 818 return addr->a_link; 819 } 820 821 return NULL; 822 } 823 824 void rtnl_addr_set_family(struct rtnl_addr *addr, int family) 825 { 826 addr->a_family = family; 827 addr->ce_mask |= ADDR_ATTR_FAMILY; 828 } 829 830 int rtnl_addr_get_family(struct rtnl_addr *addr) 831 { 832 return addr->a_family; 833 } 834 835 /** 836 * Set the prefix length / netmask 837 * @arg addr Address 838 * @arg prefixlen Length of prefix (netmask) 839 * 840 * Modifies the length of the prefix. If the address object contains a peer 841 * address the prefix length will apply to it, otherwise the prefix length 842 * will apply to the local address of the address. 843 * 844 * If the address object contains a peer or local address the corresponding 845 * `struct nl_addr` will be updated with the new prefix length. 846 * 847 * @note Specifying a length of 0 will remove the prefix length alltogether. 848 * 849 * @see rtnl_addr_get_prefixlen() 850 */ 851 void rtnl_addr_set_prefixlen(struct rtnl_addr *addr, int prefixlen) 852 { 853 addr->a_prefixlen = prefixlen; 854 855 if (prefixlen) 856 addr->ce_mask |= ADDR_ATTR_PREFIXLEN; 857 else 858 addr->ce_mask &= ~ADDR_ATTR_PREFIXLEN; 859 860 /* 861 * The prefix length always applies to the peer address if 862 * a peer address is present. 863 */ 864 if (addr->a_peer) 865 nl_addr_set_prefixlen(addr->a_peer, prefixlen); 866 else if (addr->a_local) 867 nl_addr_set_prefixlen(addr->a_local, prefixlen); 868 } 869 870 int rtnl_addr_get_prefixlen(struct rtnl_addr *addr) 871 { 872 return addr->a_prefixlen; 873 } 874 875 void rtnl_addr_set_scope(struct rtnl_addr *addr, int scope) 876 { 877 addr->a_scope = scope; 878 addr->ce_mask |= ADDR_ATTR_SCOPE; 879 } 880 881 int rtnl_addr_get_scope(struct rtnl_addr *addr) 882 { 883 return addr->a_scope; 884 } 885 886 void rtnl_addr_set_flags(struct rtnl_addr *addr, unsigned int flags) 887 { 888 addr->a_flag_mask |= flags; 889 addr->a_flags |= flags; 890 addr->ce_mask |= ADDR_ATTR_FLAGS; 891 } 892 893 void rtnl_addr_unset_flags(struct rtnl_addr *addr, unsigned int flags) 894 { 895 addr->a_flag_mask |= flags; 896 addr->a_flags &= ~flags; 897 addr->ce_mask |= ADDR_ATTR_FLAGS; 898 } 899 900 unsigned int rtnl_addr_get_flags(struct rtnl_addr *addr) 901 { 902 return addr->a_flags; 903 } 904 905 static inline int __assign_addr(struct rtnl_addr *addr, struct nl_addr **pos, 906 struct nl_addr *new, int flag) 907 { 908 if (new) { 909 if (addr->ce_mask & ADDR_ATTR_FAMILY) { 910 if (new->a_family != addr->a_family) 911 return -NLE_AF_MISMATCH; 912 } else 913 addr->a_family = new->a_family; 914 915 if (*pos) 916 nl_addr_put(*pos); 917 918 *pos = nl_addr_get(new); 919 addr->ce_mask |= (flag | ADDR_ATTR_FAMILY); 920 } else { 921 if (*pos) 922 nl_addr_put(*pos); 923 924 *pos = NULL; 925 addr->ce_mask &= ~flag; 926 } 927 928 return 0; 929 } 930 931 int rtnl_addr_set_local(struct rtnl_addr *addr, struct nl_addr *local) 932 { 933 int err; 934 935 /* Prohibit local address with prefix length if peer address is present */ 936 if ((addr->ce_mask & ADDR_ATTR_PEER) && local && 937 nl_addr_get_prefixlen(local)) 938 return -NLE_INVAL; 939 940 err = __assign_addr(addr, &addr->a_local, local, ADDR_ATTR_LOCAL); 941 if (err < 0) 942 return err; 943 944 /* Never overwrite the prefix length if a peer address is present */ 945 if (!(addr->ce_mask & ADDR_ATTR_PEER)) 946 rtnl_addr_set_prefixlen(addr, local ? nl_addr_get_prefixlen(local) : 0); 947 948 return 0; 949 } 950 951 struct nl_addr *rtnl_addr_get_local(struct rtnl_addr *addr) 952 { 953 return addr->a_local; 954 } 955 956 int rtnl_addr_set_peer(struct rtnl_addr *addr, struct nl_addr *peer) 957 { 958 int err; 959 960 if (peer && peer->a_family != AF_INET) 961 return -NLE_AF_NOSUPPORT; 962 963 err = __assign_addr(addr, &addr->a_peer, peer, ADDR_ATTR_PEER); 964 if (err < 0) 965 return err; 966 967 rtnl_addr_set_prefixlen(addr, peer ? nl_addr_get_prefixlen(peer) : 0); 968 969 return 0; 970 } 971 972 struct nl_addr *rtnl_addr_get_peer(struct rtnl_addr *addr) 973 { 974 return addr->a_peer; 975 } 976 977 int rtnl_addr_set_broadcast(struct rtnl_addr *addr, struct nl_addr *bcast) 978 { 979 if (bcast && bcast->a_family != AF_INET) 980 return -NLE_AF_NOSUPPORT; 981 982 return __assign_addr(addr, &addr->a_bcast, bcast, ADDR_ATTR_BROADCAST); 983 } 984 985 struct nl_addr *rtnl_addr_get_broadcast(struct rtnl_addr *addr) 986 { 987 return addr->a_bcast; 988 } 989 990 int rtnl_addr_set_multicast(struct rtnl_addr *addr, struct nl_addr *multicast) 991 { 992 if (multicast && multicast->a_family != AF_INET6) 993 return -NLE_AF_NOSUPPORT; 994 995 return __assign_addr(addr, &addr->a_multicast, multicast, 996 ADDR_ATTR_MULTICAST); 997 } 998 999 struct nl_addr *rtnl_addr_get_multicast(struct rtnl_addr *addr) 1000 { 1001 return addr->a_multicast; 1002 } 1003 1004 int rtnl_addr_set_anycast(struct rtnl_addr *addr, struct nl_addr *anycast) 1005 { 1006 if (anycast && anycast->a_family != AF_INET6) 1007 return -NLE_AF_NOSUPPORT; 1008 1009 return __assign_addr(addr, &addr->a_anycast, anycast, 1010 ADDR_ATTR_ANYCAST); 1011 } 1012 1013 struct nl_addr *rtnl_addr_get_anycast(struct rtnl_addr *addr) 1014 { 1015 return addr->a_anycast; 1016 } 1017 1018 uint32_t rtnl_addr_get_valid_lifetime(struct rtnl_addr *addr) 1019 { 1020 if (addr->ce_mask & ADDR_ATTR_CACHEINFO) 1021 return addr->a_cacheinfo.aci_valid; 1022 else 1023 return 0xFFFFFFFFU; 1024 } 1025 1026 void rtnl_addr_set_valid_lifetime(struct rtnl_addr *addr, uint32_t lifetime) 1027 { 1028 addr->a_cacheinfo.aci_valid = lifetime; 1029 addr->ce_mask |= ADDR_ATTR_CACHEINFO; 1030 } 1031 1032 uint32_t rtnl_addr_get_preferred_lifetime(struct rtnl_addr *addr) 1033 { 1034 if (addr->ce_mask & ADDR_ATTR_CACHEINFO) 1035 return addr->a_cacheinfo.aci_prefered; 1036 else 1037 return 0xFFFFFFFFU; 1038 } 1039 1040 void rtnl_addr_set_preferred_lifetime(struct rtnl_addr *addr, uint32_t lifetime) 1041 { 1042 addr->a_cacheinfo.aci_prefered = lifetime; 1043 addr->ce_mask |= ADDR_ATTR_CACHEINFO; 1044 } 1045 1046 uint32_t rtnl_addr_get_create_time(struct rtnl_addr *addr) 1047 { 1048 return addr->a_cacheinfo.aci_cstamp; 1049 } 1050 1051 uint32_t rtnl_addr_get_last_update_time(struct rtnl_addr *addr) 1052 { 1053 return addr->a_cacheinfo.aci_tstamp; 1054 } 1055 1056 /** @} */ 1057 1058 /** 1059 * @name Flags Translations 1060 * @{ 1061 */ 1062 1063 static const struct trans_tbl addr_flags[] = { 1064 __ADD(IFA_F_SECONDARY, secondary) 1065 __ADD(IFA_F_NODAD, nodad) 1066 __ADD(IFA_F_OPTIMISTIC, optimistic) 1067 __ADD(IFA_F_HOMEADDRESS, homeaddress) 1068 __ADD(IFA_F_DEPRECATED, deprecated) 1069 __ADD(IFA_F_TENTATIVE, tentative) 1070 __ADD(IFA_F_PERMANENT, permanent) 1071 __ADD(IFA_F_MANAGETEMPADDR, mngtmpaddr) 1072 __ADD(IFA_F_NOPREFIXROUTE, noprefixroute) 1073 }; 1074 1075 char *rtnl_addr_flags2str(int flags, char *buf, size_t size) 1076 { 1077 return __flags2str(flags, buf, size, addr_flags, 1078 ARRAY_SIZE(addr_flags)); 1079 } 1080 1081 int rtnl_addr_str2flags(const char *name) 1082 { 1083 return __str2flags(name, addr_flags, ARRAY_SIZE(addr_flags)); 1084 } 1085 1086 /** @} */ 1087 1088 static struct nl_object_ops addr_obj_ops = { 1089 .oo_name = "route/addr", 1090 .oo_size = sizeof(struct rtnl_addr), 1091 .oo_constructor = addr_constructor, 1092 .oo_free_data = addr_free_data, 1093 .oo_clone = addr_clone, 1094 .oo_dump = { 1095 [NL_DUMP_LINE] = addr_dump_line, 1096 [NL_DUMP_DETAILS] = addr_dump_details, 1097 [NL_DUMP_STATS] = addr_dump_stats, 1098 }, 1099 .oo_compare = addr_compare, 1100 .oo_attrs2str = addr_attrs2str, 1101 .oo_id_attrs = (ADDR_ATTR_FAMILY | ADDR_ATTR_IFINDEX | 1102 ADDR_ATTR_LOCAL | ADDR_ATTR_PREFIXLEN), 1103 }; 1104 1105 static struct nl_af_group addr_groups[] = { 1106 { AF_INET, RTNLGRP_IPV4_IFADDR }, 1107 { AF_INET6, RTNLGRP_IPV6_IFADDR }, 1108 { END_OF_GROUP_LIST }, 1109 }; 1110 1111 static struct nl_cache_ops rtnl_addr_ops = { 1112 .co_name = "route/addr", 1113 .co_hdrsize = sizeof(struct ifaddrmsg), 1114 .co_msgtypes = { 1115 { RTM_NEWADDR, NL_ACT_NEW, "new" }, 1116 { RTM_DELADDR, NL_ACT_DEL, "del" }, 1117 { RTM_GETADDR, NL_ACT_GET, "get" }, 1118 END_OF_MSGTYPES_LIST, 1119 }, 1120 .co_protocol = NETLINK_ROUTE, 1121 .co_groups = addr_groups, 1122 .co_request_update = addr_request_update, 1123 .co_msg_parser = addr_msg_parser, 1124 .co_obj_ops = &addr_obj_ops, 1125 }; 1126 1127 static void __init addr_init(void) 1128 { 1129 nl_cache_mngt_register(&rtnl_addr_ops); 1130 } 1131 1132 static void __exit addr_exit(void) 1133 { 1134 nl_cache_mngt_unregister(&rtnl_addr_ops); 1135 } 1136 1137 /** @} */ 1138