2 * lib/route/addr.c Addresses
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
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
10 * Baruch Even <baruch@ev-en.org>,
11 * Mediatrix Telecom, inc. <ericb@mediatrix.com>
16 * @defgroup rtaddr Addresses
19 * @par 1) Address Addition
21 * // Allocate an empty address object to be filled out with the attributes
22 * // of the new address.
23 * struct rtnl_addr *addr = rtnl_addr_alloc();
25 * // Fill out the mandatory attributes of the new address. Setting the
26 * // local address will automatically set the address family and the
27 * // prefix length to the correct values.
28 * rtnl_addr_set_ifindex(addr, ifindex);
29 * rtnl_addr_set_local(addr, local_addr);
31 * // The label of the address can be specified, currently only supported
32 * // by IPv4 and DECnet.
33 * rtnl_addr_set_label(addr, "mylabel");
35 * // The peer address can be specified if necessary, in either case a peer
36 * // address will be sent to the kernel in order to fullfil the interface
37 * // requirements. If none is set, it will equal the local address.
38 * // Note: Real peer addresses are only supported by IPv4 for now.
39 * rtnl_addr_set_peer(addr, peer_addr);
41 * // In case you want to have the address have a scope other than global
42 * // it may be overwritten using rtnl_addr_set_scope(). The scope currently
43 * // cannot be set for IPv6 addresses.
44 * rtnl_addr_set_scope(addr, rtnl_str2scope("site"));
46 * // Broadcast and anycast address may be specified using the relevant
47 * // functions, the address family will be verified if one of the other
48 * // addresses has been set already. Currently only works for IPv4.
49 * rtnl_addr_set_broadcast(addr, broadcast_addr);
50 * rtnl_addr_set_anycast(addr, anycast_addr);
52 * // Build the netlink message and send it to the kernel, the operation will
53 * // block until the operation has been completed. Alternatively the required
54 * // netlink message can be built using rtnl_addr_build_add_request() to be
55 * // sent out using nl_send_auto_complete().
56 * rtnl_addr_add(handle, addr, 0);
59 * rtnl_addr_put(addr);
62 * @par 2) Address Deletion
64 * // Allocate an empty address object to be filled out with the attributes
65 * // matching the address to be deleted. Alternatively a fully equipped
66 * // address object out of a cache can be used instead.
67 * struct rtnl_addr *addr = rtnl_addr_alloc();
69 * // The only mandatory parameter besides the address family is the interface
70 * // index the address is on, i.e. leaving out all other parameters will
71 * // result in all addresses of the specified address family interface tuple
73 * rtnl_addr_set_ifindex(addr, ifindex);
75 * // Specyfing the address family manually is only required if neither the
76 * // local nor peer address have been specified.
77 * rtnl_addr_set_family(addr, AF_INET);
79 * // Specyfing the local address is optional but the best choice to delete
80 * // specific addresses.
81 * rtnl_addr_set_local(addr, local_addr);
83 * // The label of the address can be specified, currently only supported
84 * // by IPv4 and DECnet.
85 * rtnl_addr_set_label(addr, "mylabel");
87 * // The peer address can be specified if necessary, in either case a peer
88 * // address will be sent to the kernel in order to fullfil the interface
89 * // requirements. If none is set, it will equal the local address.
90 * // Note: Real peer addresses are only supported by IPv4 for now.
91 * rtnl_addr_set_peer(addr, peer_addr);
93 * // Build the netlink message and send it to the kernel, the operation will
94 * // block until the operation has been completed. Alternatively the required
95 * // netlink message can be built using rtnl_addr_build_delete_request()
96 * // to be sent out using nl_send_auto_complete().
97 * rtnl_addr_delete(handle, addr, 0);
100 * rtnl_addr_put(addr);
105 #include <netlink-local.h>
106 #include <netlink/netlink.h>
107 #include <netlink/route/rtnl.h>
108 #include <netlink/route/addr.h>
109 #include <netlink/route/route.h>
110 #include <netlink/route/link.h>
111 #include <netlink/utils.h>
114 #define ADDR_ATTR_FAMILY 0x0001
115 #define ADDR_ATTR_PREFIXLEN 0x0002
116 #define ADDR_ATTR_FLAGS 0x0004
117 #define ADDR_ATTR_SCOPE 0x0008
118 #define ADDR_ATTR_IFINDEX 0x0010
119 #define ADDR_ATTR_LABEL 0x0020
120 #define ADDR_ATTR_CACHEINFO 0x0040
121 #define ADDR_ATTR_PEER 0x0080
122 #define ADDR_ATTR_LOCAL 0x0100
123 #define ADDR_ATTR_BROADCAST 0x0200
124 #define ADDR_ATTR_ANYCAST 0x0400
125 #define ADDR_ATTR_MULTICAST 0x0800
127 static struct nl_cache_ops rtnl_addr_ops;
130 static void addr_free_data(struct nl_object *obj)
132 struct rtnl_addr *addr = nl_object_priv(obj);
137 nl_addr_put(addr->a_peer);
138 nl_addr_put(addr->a_local);
139 nl_addr_put(addr->a_bcast);
140 nl_addr_put(addr->a_anycast);
141 nl_addr_put(addr->a_multicast);
144 static struct nla_policy addr_policy[IFA_MAX+1] = {
145 [IFA_LABEL] = { .type = NLA_STRING,
146 .maxlen = IFNAMSIZ },
147 [IFA_CACHEINFO] = { .minlen = sizeof(struct ifa_cacheinfo) },
150 static int addr_msg_parser(struct sockaddr_nl *who, struct nlmsghdr *nlh,
153 struct rtnl_addr *addr;
154 struct nl_parser_param *pp = arg;
155 struct ifaddrmsg *ifa;
156 struct nlattr *tb[IFA_MAX+1];
157 int err = -ENOMEM, peer_prefix = 0;
159 addr = rtnl_addr_alloc();
161 err = nl_errno(ENOMEM);
164 addr->ce_msgtype = nlh->nlmsg_type;
166 err = nlmsg_parse(nlh, sizeof(*ifa), tb, IFA_MAX, addr_policy);
170 ifa = nlmsg_data(nlh);
171 addr->a_family = ifa->ifa_family;
172 addr->a_prefixlen = ifa->ifa_prefixlen;
173 addr->a_flags = ifa->ifa_flags;
174 addr->a_scope = ifa->ifa_scope;
175 addr->a_ifindex = ifa->ifa_index;
177 addr->a_mask = (ADDR_ATTR_FAMILY | ADDR_ATTR_PREFIXLEN |
178 ADDR_ATTR_FLAGS | ADDR_ATTR_SCOPE | ADDR_ATTR_IFINDEX);
181 nla_strlcpy(addr->a_label, tb[IFA_LABEL], IFNAMSIZ);
182 addr->a_mask |= ADDR_ATTR_LABEL;
185 if (tb[IFA_CACHEINFO]) {
186 struct ifa_cacheinfo *ca;
188 ca = nla_data(tb[IFA_CACHEINFO]);
189 addr->a_cacheinfo.aci_prefered = ca->ifa_prefered;
190 addr->a_cacheinfo.aci_valid = ca->ifa_valid;
191 addr->a_cacheinfo.aci_cstamp = ca->cstamp;
192 addr->a_cacheinfo.aci_tstamp = ca->tstamp;
193 addr->a_mask |= ADDR_ATTR_CACHEINFO;
197 addr->a_local = nla_get_addr(tb[IFA_LOCAL], addr->a_family);
200 addr->a_mask |= ADDR_ATTR_LOCAL;
203 if (tb[IFA_ADDRESS]) {
206 a = nla_get_addr(tb[IFA_ADDRESS], addr->a_family);
210 /* IPv6 sends the local address as IFA_ADDRESS with
211 * no IFA_LOCAL, IPv4 sends both IFA_LOCAL and IFA_ADDRESS
212 * with IFA_ADDRESS being the peer address if they differ */
213 if (!tb[IFA_LOCAL] || !nl_addr_cmp(a, addr->a_local)) {
214 nl_addr_put(addr->a_local);
216 addr->a_mask |= ADDR_ATTR_LOCAL;
219 addr->a_mask |= ADDR_ATTR_PEER;
224 nl_addr_set_prefixlen(peer_prefix ? addr->a_peer : addr->a_local,
227 if (tb[IFA_BROADCAST]) {
228 addr->a_bcast = nla_get_addr(tb[IFA_BROADCAST], addr->a_family);
232 addr->a_mask |= ADDR_ATTR_BROADCAST;
235 if (tb[IFA_ANYCAST]) {
236 addr->a_anycast = nla_get_addr(tb[IFA_ANYCAST], addr->a_family);
237 if (!addr->a_anycast)
240 addr->a_mask |= ADDR_ATTR_ANYCAST;
243 if (tb[IFA_MULTICAST]) {
244 addr->a_multicast = nla_get_addr(tb[IFA_MULTICAST],
246 if (!addr->a_multicast)
249 addr->a_mask |= ADDR_ATTR_MULTICAST;
252 err = pp->pp_cb((struct nl_object *) addr, pp);
259 rtnl_addr_free(addr);
264 static int addr_request_update(struct nl_cache *cache, struct nl_handle *handle)
266 return nl_rtgen_request(handle, RTM_GETADDR, AF_UNSPEC, NLM_F_DUMP);
269 static int addr_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
271 struct rtnl_addr *addr = (struct rtnl_addr *) obj;
272 struct nl_cache *link_cache;
275 link_cache = nl_cache_mngt_require("route/link");
277 if (addr->a_mask & ADDR_ATTR_LOCAL)
279 nl_addr2str(addr->a_local, buf, sizeof(buf)));
283 if (addr->a_mask & ADDR_ATTR_PEER)
284 dp_dump(p, " peer %s",
285 nl_addr2str(addr->a_peer, buf, sizeof(buf)));
287 dp_dump(p, " %s ", nl_af2str(addr->a_family, buf, sizeof(buf)));
290 dp_dump(p, "dev %s ",
291 rtnl_link_i2name(link_cache, addr->a_ifindex,
294 dp_dump(p, "dev %d ", addr->a_ifindex);
296 dp_dump(p, "scope %s",
297 rtnl_scope2str(addr->a_scope, buf, sizeof(buf)));
299 rtnl_addr_flags2str(addr->a_flags, buf, sizeof(buf));
301 dp_dump(p, " <%s>", buf);
308 static int addr_dump_full(struct nl_object *obj, struct nl_dump_params *p)
310 struct rtnl_addr *addr = (struct rtnl_addr *) obj;
311 int line = addr_dump_brief(obj, p);
314 if (addr->a_mask & (ADDR_ATTR_LABEL | ADDR_ATTR_BROADCAST |
315 ADDR_ATTR_ANYCAST | ADDR_ATTR_MULTICAST)) {
316 dp_dump_line(p, line++, " ");
318 if (addr->a_mask & ADDR_ATTR_LABEL)
319 dp_dump(p, " label %s", addr->a_label);
321 if (addr->a_mask & ADDR_ATTR_BROADCAST)
322 dp_dump(p, " broadcast %s",
323 nl_addr2str(addr->a_bcast, buf, sizeof(buf)));
325 if (addr->a_mask & ADDR_ATTR_ANYCAST)
326 dp_dump(p, " anycast %s",
327 nl_addr2str(addr->a_anycast, buf,
330 if (addr->a_mask & ADDR_ATTR_MULTICAST)
331 dp_dump(p, " multicast %s",
332 nl_addr2str(addr->a_multicast, buf,
338 if (addr->a_mask & ADDR_ATTR_CACHEINFO) {
339 struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
341 dp_dump_line(p, line++, " valid-lifetime %s",
342 ci->aci_valid == 0xFFFFFFFFU ? "forever" :
343 nl_msec2str(ci->aci_valid * 1000,
346 dp_dump(p, " preferred-lifetime %s\n",
347 ci->aci_prefered == 0xFFFFFFFFU ? "forever" :
348 nl_msec2str(ci->aci_prefered * 1000,
351 dp_dump_line(p, line++, " created boot-time+%s ",
352 nl_msec2str(addr->a_cacheinfo.aci_cstamp * 10,
355 dp_dump(p, "last-updated boot-time+%s\n",
356 nl_msec2str(addr->a_cacheinfo.aci_tstamp * 10,
363 static int addr_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
365 return addr_dump_full(obj, p);
368 static int addr_dump_xml(struct nl_object *obj, struct nl_dump_params *p)
370 struct rtnl_addr *addr = (struct rtnl_addr *) obj;
371 struct nl_cache *link_cache;
375 dp_dump_line(p, line++, "<address>\n");
376 dp_dump_line(p, line++, " <family>%s</family>\n",
377 nl_af2str(addr->a_family, buf, sizeof(buf)));
379 if (addr->a_mask & ADDR_ATTR_LOCAL)
380 dp_dump_line(p, line++, " <local>%s</local>\n",
381 nl_addr2str(addr->a_local, buf, sizeof(buf)));
383 if (addr->a_mask & ADDR_ATTR_PEER)
384 dp_dump_line(p, line++, " <peer>%s</peer>\n",
385 nl_addr2str(addr->a_peer, buf, sizeof(buf)));
387 if (addr->a_mask & ADDR_ATTR_BROADCAST)
388 dp_dump_line(p, line++, " <broadcast>%s</broadcast>\n",
389 nl_addr2str(addr->a_bcast, buf, sizeof(buf)));
391 if (addr->a_mask & ADDR_ATTR_ANYCAST)
392 dp_dump_line(p, line++, " <anycast>%s</anycast>\n",
393 nl_addr2str(addr->a_anycast, buf, sizeof(buf)));
395 if (addr->a_mask & ADDR_ATTR_MULTICAST)
396 dp_dump_line(p, line++, " <multicast>%s</multicast>\n",
397 nl_addr2str(addr->a_multicast, buf,
400 if (addr->a_mask & ADDR_ATTR_PREFIXLEN)
401 dp_dump_line(p, line++, " <prefixlen>%u</prefixlen>\n",
403 link_cache = nl_cache_mngt_require("route/link");
406 dp_dump_line(p, line++, " <device>%s</device>\n",
407 rtnl_link_i2name(link_cache, addr->a_ifindex,
410 dp_dump_line(p, line++, " <device>%u</device>\n",
413 if (addr->a_mask & ADDR_ATTR_SCOPE)
414 dp_dump_line(p, line++, " <scope>%s</scope>\n",
415 rtnl_scope2str(addr->a_scope, buf, sizeof(buf)));
417 if (addr->a_mask & ADDR_ATTR_LABEL)
418 dp_dump_line(p, line++, " <label>%s</label>\n", addr->a_label);
420 rtnl_addr_flags2str(addr->a_flags, buf, sizeof(buf));
422 dp_dump_line(p, line++, " <flags>%s</flags>\n", buf);
424 if (addr->a_mask & ADDR_ATTR_CACHEINFO) {
425 struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
427 dp_dump_line(p, line++, " <cacheinfo>\n");
429 dp_dump_line(p, line++, " <valid>%s</valid>\n",
430 ci->aci_valid == 0xFFFFFFFFU ? "forever" :
431 nl_msec2str(ci->aci_valid * 1000,
434 dp_dump_line(p, line++, " <prefered>%s</prefered>\n",
435 ci->aci_prefered == 0xFFFFFFFFU ? "forever" :
436 nl_msec2str(ci->aci_prefered * 1000,
439 dp_dump_line(p, line++, " <created>%s</created>\n",
440 nl_msec2str(addr->a_cacheinfo.aci_cstamp * 10,
443 dp_dump_line(p, line++, " <last-update>%s</last-update>\n",
444 nl_msec2str(addr->a_cacheinfo.aci_tstamp * 10,
447 dp_dump_line(p, line++, " </cacheinfo>\n");
450 dp_dump_line(p, line++, "</address>\n");
455 static int addr_dump_env(struct nl_object *obj, struct nl_dump_params *p)
457 struct rtnl_addr *addr = (struct rtnl_addr *) obj;
458 struct nl_cache *link_cache;
462 dp_dump_line(p, line++, "ADDR_FAMILY=%s\n",
463 nl_af2str(addr->a_family, buf, sizeof(buf)));
465 if (addr->a_mask & ADDR_ATTR_LOCAL)
466 dp_dump_line(p, line++, "ADDR_LOCAL=%s\n",
467 nl_addr2str(addr->a_local, buf, sizeof(buf)));
469 if (addr->a_mask & ADDR_ATTR_PEER)
470 dp_dump_line(p, line++, "ADDR_PEER=%s\n",
471 nl_addr2str(addr->a_peer, buf, sizeof(buf)));
473 if (addr->a_mask & ADDR_ATTR_BROADCAST)
474 dp_dump_line(p, line++, "ADDR_BROADCAST=%s\n",
475 nl_addr2str(addr->a_bcast, buf, sizeof(buf)));
477 if (addr->a_mask & ADDR_ATTR_ANYCAST)
478 dp_dump_line(p, line++, "ADDR_ANYCAST=%s\n",
479 nl_addr2str(addr->a_anycast, buf, sizeof(buf)));
481 if (addr->a_mask & ADDR_ATTR_MULTICAST)
482 dp_dump_line(p, line++, "ADDR_MULTICAST=%s\n",
483 nl_addr2str(addr->a_multicast, buf,
486 if (addr->a_mask & ADDR_ATTR_PREFIXLEN)
487 dp_dump_line(p, line++, "ADDR_PREFIXLEN=%u\n",
489 link_cache = nl_cache_mngt_require("route/link");
491 dp_dump_line(p, line++, "ADDR_IFINDEX=%u\n", addr->a_ifindex);
493 dp_dump_line(p, line++, "ADDR_IFNAME=%s\n",
494 rtnl_link_i2name(link_cache, addr->a_ifindex,
497 if (addr->a_mask & ADDR_ATTR_SCOPE)
498 dp_dump_line(p, line++, "ADDR_SCOPE=%s\n",
499 rtnl_scope2str(addr->a_scope, buf, sizeof(buf)));
501 if (addr->a_mask & ADDR_ATTR_LABEL)
502 dp_dump_line(p, line++, "ADDR_LABEL=%s\n", addr->a_label);
504 rtnl_addr_flags2str(addr->a_flags, buf, sizeof(buf));
506 dp_dump_line(p, line++, "ADDR_FLAGS=%s\n", buf);
508 if (addr->a_mask & ADDR_ATTR_CACHEINFO) {
509 struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
511 dp_dump_line(p, line++, "ADDR_CACHEINFO_VALID=%s\n",
512 ci->aci_valid == 0xFFFFFFFFU ? "forever" :
513 nl_msec2str(ci->aci_valid * 1000,
516 dp_dump_line(p, line++, "ADDR_CACHEINFO_PREFERED=%s\n",
517 ci->aci_prefered == 0xFFFFFFFFU ? "forever" :
518 nl_msec2str(ci->aci_prefered * 1000,
521 dp_dump_line(p, line++, "ADDR_CACHEINFO_CREATED=%s\n",
522 nl_msec2str(addr->a_cacheinfo.aci_cstamp * 10,
525 dp_dump_line(p, line++, "ADDR_CACHEINFO_LASTUPDATE=%s\n",
526 nl_msec2str(addr->a_cacheinfo.aci_tstamp * 10,
533 static int addr_filter(struct nl_object *obj, struct nl_object *filter)
535 struct rtnl_addr *o = (struct rtnl_addr *) obj;
536 struct rtnl_addr *f = (struct rtnl_addr *) filter;
538 #define REQ(F) (f->a_mask & ADDR_ATTR_##F)
539 #define AVAIL(F) (o->a_mask & ADDR_ATTR_##F)
540 #define _O(F, EXPR) (REQ(F) && (!AVAIL(F) || (EXPR)))
541 #define _C(F, N) (REQ(F) && (!AVAIL(F) || (o->N != f->N)))
542 if (_C(IFINDEX, a_ifindex) ||
543 _C(FAMILY, a_family) ||
544 _C(SCOPE, a_scope) ||
545 _O(FLAGS, f->a_flags ^ (o->a_flags & f->a_flag_mask)) ||
546 _O(LABEL, strcmp(o->a_label, f->a_label)) ||
547 _O(PEER, nl_addr_cmp(o->a_peer, f->a_peer)) ||
548 _O(LOCAL, nl_addr_cmp(o->a_local, f->a_local)) ||
549 _O(ANYCAST, nl_addr_cmp(o->a_anycast, f->a_anycast)) ||
550 _O(MULTICAST, nl_addr_cmp(o->a_multicast, f->a_multicast)) ||
551 _O(BROADCAST, nl_addr_cmp(o->a_bcast, f->a_bcast)))
562 * @name Address Object Creation/Deletion
567 * Allocate and initialize a new address object
568 * @note Free the memory after usage using rtnl_addr_put() or rtnl_addr_free().
569 * @return Newly allocated address object or NULL if an error occured.
571 struct rtnl_addr *rtnl_addr_alloc(void)
573 return (struct rtnl_addr *) nl_object_alloc_from_ops(&rtnl_addr_ops);
577 * Give back a reference on a address object.
578 * @arg addr Address object to be given back.
580 * Decrements the reference counter and frees the object if the
581 * last reference has been released.
583 void rtnl_addr_put(struct rtnl_addr *addr)
585 nl_object_put((struct nl_object *) addr);
589 * Free an address object
590 * @arg addr Address object to be freed.
592 * @note Always use rtnl_addr_put() unless you're absolutely sure
593 * that no other user may have a reference on this object.
595 void rtnl_addr_free(struct rtnl_addr *addr)
597 nl_object_free((struct nl_object *) addr);
603 * @name Address Cache Management
608 * Allocate address cache and fill in all configured addresses
609 * @arg handle Netlink handle.
611 * Allocates a new address cache, initializes it properly and updates it
612 * to include all addresses currently configured in the kernel.
614 * @note Free the memory after usage.
615 * @return Newly allocated cache or NULL if an error occured.
617 struct nl_cache *rtnl_addr_alloc_cache(struct nl_handle *handle)
619 struct nl_cache *cache;
621 cache = nl_cache_alloc_from_ops(&rtnl_addr_ops);
625 if (nl_cache_update(handle, cache) < 0) {
626 nl_cache_free(cache);
635 static struct nl_msg *build_addr_msg(struct rtnl_addr *tmpl, int cmd, int flags)
638 struct ifaddrmsg am = {
639 .ifa_family = tmpl->a_family,
640 .ifa_index = tmpl->a_ifindex,
641 .ifa_prefixlen = tmpl->a_prefixlen,
644 if (tmpl->a_mask & ADDR_ATTR_SCOPE)
645 am.ifa_scope = tmpl->a_scope;
647 /* compatibility hack */
648 if (tmpl->a_family == AF_INET &&
649 tmpl->a_mask & ADDR_ATTR_LOCAL &&
650 *((char *) nl_addr_get_binary_addr(tmpl->a_local)) == 127)
651 am.ifa_scope = RT_SCOPE_HOST;
653 am.ifa_scope = RT_SCOPE_UNIVERSE;
656 msg = nlmsg_build_simple(cmd, flags);
658 goto nla_put_failure;
660 if (nlmsg_append(msg, &am, sizeof(am), 1) < 0)
661 goto nla_put_failure;
663 if (tmpl->a_mask & ADDR_ATTR_LOCAL)
664 NLA_PUT_ADDR(msg, IFA_LOCAL, tmpl->a_local);
666 if (tmpl->a_mask & ADDR_ATTR_PEER)
667 NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_peer);
669 NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_local);
671 if (tmpl->a_mask & ADDR_ATTR_LABEL)
672 NLA_PUT_STRING(msg, IFA_LABEL, tmpl->a_label);
674 if (tmpl->a_mask & ADDR_ATTR_BROADCAST)
675 NLA_PUT_ADDR(msg, IFA_BROADCAST, tmpl->a_bcast);
677 if (tmpl->a_mask & ADDR_ATTR_ANYCAST)
678 NLA_PUT_ADDR(msg, IFA_ANYCAST, tmpl->a_anycast);
688 * @name Address Addition
693 * Build netlink request message to request addition of new address
694 * @arg addr Address object representing the new address.
695 * @arg flags Additional netlink message flags.
697 * Builds a new netlink message requesting the addition of a new
698 * address. The netlink message header isn't fully equipped with
699 * all relevant fields and must thus be sent out via nl_send_auto_complete()
700 * or supplemented as needed.
702 * Minimal required attributes:
703 * - interface index (rtnl_addr_set_ifindex())
704 * - local address (rtnl_addr_set_local())
706 * The scope will default to universe except for loopback addresses in
707 * which case a host scope is used if not specified otherwise.
709 * @note Free the memory after usage using nlmsg_free().
710 * @return Newly allocated netlink message or NULL if an error occured.
712 struct nl_msg *rtnl_addr_build_add_request(struct rtnl_addr *addr, int flags)
714 int required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY |
715 ADDR_ATTR_PREFIXLEN | ADDR_ATTR_LOCAL;
717 if ((addr->a_mask & required) != required) {
718 nl_error(EINVAL, "Missing mandatory attributes, required are: "
719 "ifindex, family, prefixlen, local address.");
723 return build_addr_msg(addr, RTM_NEWADDR, NLM_F_CREATE | flags);
727 * Request addition of new address
728 * @arg handle Netlink handle.
729 * @arg addr Address object representing the new address.
730 * @arg flags Additional netlink message flags.
732 * Builds a netlink message by calling rtnl_addr_build_add_request(),
733 * sends the request to the kernel and waits for the next ACK to be
734 * received and thus blocks until the request has been fullfilled.
736 * @see rtnl_addr_build_add_request()
738 * @return 0 on sucess or a negative error if an error occured.
740 int rtnl_addr_add(struct nl_handle *handle, struct rtnl_addr *addr, int flags)
745 msg = rtnl_addr_build_add_request(addr, flags);
747 return nl_get_errno();
749 err = nl_send_auto_complete(handle, msg);
754 return nl_wait_for_ack(handle);
760 * @name Address Deletion
765 * Build a netlink request message to request deletion of an address
766 * @arg addr Address object to be deleteted.
767 * @arg flags Additional netlink message flags.
769 * Builds a new netlink message requesting a deletion of an address.
770 * The netlink message header isn't fully equipped with all relevant
771 * fields and must thus be sent out via nl_send_auto_complete()
772 * or supplemented as needed.
774 * Minimal required attributes:
775 * - interface index (rtnl_addr_set_ifindex())
776 * - address family (rtnl_addr_set_family())
778 * Optional attributes:
779 * - local address (rtnl_addr_set_local())
780 * - label (rtnl_addr_set_label(), IPv4/DECnet only)
781 * - peer address (rtnl_addr_set_peer(), IPv4 only)
783 * @note Free the memory after usage using nlmsg_free().
784 * @return Newly allocated netlink message or NULL if an error occured.
786 struct nl_msg *rtnl_addr_build_delete_request(struct rtnl_addr *addr, int flags)
788 int required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY;
790 if ((addr->a_mask & required) != required) {
791 nl_error(EINVAL, "Missing mandatory attributes, required are: "
796 return build_addr_msg(addr, RTM_DELADDR, flags);
800 * Request deletion of an address
801 * @arg handle Netlink handle.
802 * @arg addr Address object to be deleted.
803 * @arg flags Additional netlink message flags.
805 * Builds a netlink message by calling rtnl_addr_build_delete_request(),
806 * sends the request to the kernel and waits for the next ACK to be
807 * received and thus blocks until the request has been fullfilled.
809 * @see rtnl_addr_build_delete_request();
811 * @return 0 on sucess or a negative error if an error occured.
813 int rtnl_addr_delete(struct nl_handle *handle, struct rtnl_addr *addr,
819 msg = rtnl_addr_build_delete_request(addr, flags);
821 return nl_get_errno();
823 err = nl_send_auto_complete(handle, msg);
828 return nl_wait_for_ack(handle);
834 * @name Attribute Access
839 * Set label of address object
840 * @arg addr Address object to be modified.
841 * @arg label New address label.
843 * @note The maximum size of an address label is IFNAMSIZ.
845 void rtnl_addr_set_label(struct rtnl_addr *addr, const char *label)
847 strncpy(addr->a_label, label, sizeof(addr->a_label) - 1);
848 addr->a_mask |= ADDR_ATTR_LABEL;
852 * Get label of address object
853 * @arg addr Address object.
854 * @return Address label or NULL if not set.
856 char *rtnl_addr_get_label(struct rtnl_addr *addr)
858 if (addr->a_mask & ADDR_ATTR_LABEL)
859 return addr->a_label;
865 * Set interface index of address object
866 * @arg addr Address object ot be modified.
867 * @arg ifindex New interface index this address is on.
869 void rtnl_addr_set_ifindex(struct rtnl_addr *addr, int ifindex)
871 addr->a_ifindex = ifindex;
872 addr->a_mask |= ADDR_ATTR_IFINDEX;
876 * Get interface index of address object
877 * @arg addr Address object.
878 * @return Interface index address is on or RTNL_LINK_NOT_FOUND if not set.
880 int rtnl_addr_get_ifindex(struct rtnl_addr *addr)
882 if (addr->a_mask & ADDR_ATTR_IFINDEX)
883 return addr->a_ifindex;
885 return RTNL_LINK_NOT_FOUND;
889 * Set address family of address object.
890 * @arg addr Address object to be modified.
891 * @arg family New address family
893 * @note The address family is set automatically if one of the addresses
894 * is set and the family hasn't been specified yet. Setting it manually
895 * can be used to enforce family validation while setting addresses.
897 void rtnl_addr_set_family(struct rtnl_addr *addr, int family)
899 addr->a_family = family;
900 addr->a_mask |= ADDR_ATTR_FAMILY;
904 * Get address family of address object.
905 * @arg addr Address object.
906 * @return Address family or AF_UNSPEC if not set.
908 int rtnl_addr_get_family(struct rtnl_addr *addr)
910 if (addr->a_mask & ADDR_ATTR_FAMILY)
911 return addr->a_family;
917 * Set prefix length of address object.
918 * @arg addr Address object to be modified.
919 * @arg prefix New prefix length.
921 void rtnl_addr_set_prefixlen(struct rtnl_addr *addr, int prefix)
923 addr->a_prefixlen = prefix;
924 addr->a_mask |= ADDR_ATTR_PREFIXLEN;
928 * Get prefix length of address object.
929 * @arg addr Address object.
930 * @return Prefix length or a negative number if not set.
932 int rtnl_addr_get_prefixlen(struct rtnl_addr *addr)
934 if (addr->a_mask & ADDR_ATTR_PREFIXLEN)
935 return addr->a_prefixlen;
941 * Set scope of address object.
942 * @arg addr Address object to be modified.
943 * @arg scope New scope.
945 void rtnl_addr_set_scope(struct rtnl_addr *addr, int scope)
947 addr->a_scope = scope;
948 addr->a_mask |= ADDR_ATTR_SCOPE;
952 * Get scope of address object.
953 * @arg addr Address object.
954 * @return Scope or a negative number if not set.
956 int rtnl_addr_get_scope(struct rtnl_addr *addr)
958 if (addr->a_mask & ADDR_ATTR_SCOPE)
959 return addr->a_scope;
965 * Set flags of address object.
966 * @arg addr Address object to be modified.
967 * @arg flags Additional flags to set.
969 * @note Existing flags that have been set will not be overwritten.
971 void rtnl_addr_set_flags(struct rtnl_addr *addr, unsigned int flags)
973 addr->a_flag_mask |= flags;
974 addr->a_flags |= flags;
975 addr->a_mask |= ADDR_ATTR_FLAGS;
979 * Unset flags of address object.
980 * @arg addr Address object to be modified.
981 * @arg flags Flags to unset.
983 void rtnl_addr_unset_flags(struct rtnl_addr *addr, unsigned int flags)
985 addr->a_flag_mask |= flags;
986 addr->a_flags &= ~flags;
987 addr->a_mask |= ADDR_ATTR_FLAGS;
991 * Get flags of address object.
992 * @arg addr Address object.
993 * @return Flags in form of a bitmask.
995 unsigned int rtnl_addr_get_flags(struct rtnl_addr *addr)
997 return addr->a_flags;
1000 static inline int __assign_addr(struct rtnl_addr *addr, struct nl_addr **pos,
1001 struct nl_addr *new, int flag)
1003 if (addr->a_mask & ADDR_ATTR_FAMILY) {
1004 if (new->a_family != addr->a_family)
1005 return nl_error(EINVAL, "Address family mismatch");
1007 addr->a_family = new->a_family;
1012 *pos = nl_addr_get(new);
1013 addr->a_mask |= (flag | ADDR_ATTR_FAMILY);
1019 * Set local address of address object.
1020 * @arg addr Address object to be modified.
1021 * @arg local New local address.
1023 * Assigns the new local address to the specified address object. The
1024 * address is validated against the address family if set already via
1025 * either rtnl_addr_set_family() or by setting one of the other addresses.
1026 * The assignment fails if the address families mismatch. In case the
1027 * address family has not been specified yet, the address family of the
1028 * new address is elected to be the new requirement.
1030 * @note The address may not contain a prefix length if the peer address
1031 * has been specified already.
1033 * @return 0 on success or a negative error code.
1035 int rtnl_addr_set_local(struct rtnl_addr *addr, struct nl_addr *local)
1039 err = __assign_addr(addr, &addr->a_local, local, ADDR_ATTR_LOCAL);
1043 if (!(addr->a_mask & ADDR_ATTR_PEER)) {
1044 addr->a_prefixlen = nl_addr_get_prefixlen(addr->a_local);
1045 addr->a_mask |= ADDR_ATTR_PREFIXLEN;
1052 * Get local address of address object.
1053 * @arg addr Address object.
1054 * @return Local address or NULL if not set.
1056 struct nl_addr *rtnl_addr_get_local(struct rtnl_addr *addr)
1058 if (addr->a_mask & ADDR_ATTR_LOCAL)
1059 return addr->a_local;
1065 * Set peer address of address object.
1066 * @arg addr Address object to be modified.
1067 * @arg peer New peer address.
1069 * Assigns the new peer address to the specified address object. The
1070 * address is validated against the address family if set already via
1071 * either rtnl_addr_set_family() or by setting one of the other addresses.
1072 * The assignment fails if the address families mismatch. In case the
1073 * address family has not been specified yet, the address family of this
1074 * new address is elected to be the requirement.
1076 * @return 0 on success or a negative error code.
1078 int rtnl_addr_set_peer(struct rtnl_addr *addr, struct nl_addr *peer)
1080 return __assign_addr(addr, &addr->a_peer, peer, ADDR_ATTR_PEER);
1082 addr->a_prefixlen = nl_addr_get_prefixlen(addr->a_peer);
1083 addr->a_mask |= ADDR_ATTR_PREFIXLEN;
1089 * Get peer address of address object.
1090 * @arg addr Adress object.
1091 * @return Peer address or NULL if not set.
1093 struct nl_addr *rtnl_addr_get_peer(struct rtnl_addr *addr)
1095 if (addr->a_mask & ADDR_ATTR_PEER)
1096 return addr->a_peer;
1102 * Set broadcast address of address object.
1103 * @arg addr Address object to be modified.
1104 * @arg bcast New broadcast address.
1106 * Assigns the new broadcast address to the specified address object. The
1107 * address is validated against the address family if set already via
1108 * either rtnl_addr_set_family() or by setting one of the other addresses.
1109 * The assignment fails if the address families mismatch. In case the
1110 * address family has not been specified yet, the address family of this
1111 * new address is elected to be the requirement.
1113 * @return 0 on success or a negative error code.
1115 int rtnl_addr_set_broadcast(struct rtnl_addr *addr, struct nl_addr *bcast)
1117 return __assign_addr(addr, &addr->a_bcast, bcast, ADDR_ATTR_BROADCAST);
1121 * Get broadcast address of address object.
1122 * @arg addr Address object.
1123 * @return Broadcast address or NULL if not set.
1125 struct nl_addr *rtnl_addr_get_broadcast(struct rtnl_addr *addr)
1127 if (addr->a_mask & ADDR_ATTR_BROADCAST)
1128 return addr->a_bcast;
1134 * Set anycast address of address object.
1135 * @arg addr Address object to be modified.
1136 * @arg anycast New anycast address.
1138 * Assigns the new anycast address to the specified address object. The
1139 * address is validated against the address family if set already via
1140 * either rtnl_addr_set_family() or by setting one of the other addresses.
1141 * The assignment fails if the address families mismatch. In case the
1142 * address family has not been specified yet, the address family of this
1143 * new address is elected to be the requirement.
1145 * @return 0 on success or a negative error code.
1147 int rtnl_addr_set_anycast(struct rtnl_addr *addr, struct nl_addr *anycast)
1149 return __assign_addr(addr, &addr->a_anycast, anycast,
1154 * Get anycast address of address object.
1155 * @arg addr Address object.
1156 * @return Anycast address or NULL if not set.
1158 struct nl_addr *rtnl_addr_get_anycast(struct rtnl_addr *addr)
1160 if (addr->a_mask & ADDR_ATTR_ANYCAST)
1161 return addr->a_anycast;
1167 * Set multicast address of address object.
1168 * @arg addr Address object to be modified.
1169 * @arg multicast New multicast address.
1171 * Assigns the new multicast address to the specified address object. The
1172 * address is validated against the address family if set already via
1173 * either rtnl_addr_set_family() or by setting one of the other addresses.
1174 * The assignment fails if the address families mismatch. In case the
1175 * address family has not been specified yet, the address family of this
1176 * new address is elected to be the requirement.
1178 * @return 0 on success or a negative error code.
1180 int rtnl_addr_set_multicast(struct rtnl_addr *addr, struct nl_addr *multicast)
1182 return __assign_addr(addr, &addr->a_multicast, multicast,
1183 ADDR_ATTR_MULTICAST);
1187 * Get multicast address of address object.
1188 * @arg addr Address object.
1189 * @return Multicast address or NULL if not set.
1191 struct nl_addr *rtnl_addr_get_multicast(struct rtnl_addr *addr)
1193 if (addr->a_mask & ADDR_ATTR_MULTICAST)
1194 return addr->a_multicast;
1202 * @name Address Flags Translations
1206 static struct trans_tbl addr_flags[] = {
1207 __ADD(IFA_F_SECONDARY, secondary)
1208 __ADD(IFA_F_DEPRECATED, deprecated)
1209 __ADD(IFA_F_TENTATIVE, tentative)
1210 __ADD(IFA_F_PERMANENT, permanent)
1214 * Convert address flags to character string.
1215 * @arg flags Address flags.
1216 * @arg buf Destination buffer.
1217 * @arg size Size of destination buffer.
1219 * Converts address flags to a character string separated by commas and
1220 * stores the resulting character string in the specified destination buffer.
1222 * @return Formatted flags as character string.
1224 char *rtnl_addr_flags2str(int flags, char *buf, size_t size)
1226 return __flags2str(flags, buf, size, addr_flags,
1227 ARRAY_SIZE(addr_flags));
1231 * Convert character string to address flags.
1232 * @arg name Name of address flags.
1234 * Converts the provided character string specifying any number of address
1235 * flags separated by commas to the corresponding numeric bitmask.
1237 * @return Address flags in form of a bitmask.
1239 int rtnl_addr_str2flags(const char *name)
1241 return __str2flags(name, addr_flags, ARRAY_SIZE(addr_flags));
1246 static struct nl_cache_ops rtnl_addr_ops = {
1247 .co_name = "route/addr",
1248 .co_size = sizeof(struct rtnl_addr),
1249 .co_hdrsize = sizeof(struct ifaddrmsg),
1251 { RTM_NEWADDR, "new" },
1252 { RTM_DELADDR, "delete" },
1253 { RTM_GETADDR, "get" },
1256 .co_protocol = NETLINK_ROUTE,
1257 .co_request_update = addr_request_update,
1258 .co_msg_parser = addr_msg_parser,
1259 .co_free_data = addr_free_data,
1260 .co_dump[NL_DUMP_BRIEF] = addr_dump_brief,
1261 .co_dump[NL_DUMP_FULL] = addr_dump_full,
1262 .co_dump[NL_DUMP_STATS] = addr_dump_stats,
1263 .co_dump[NL_DUMP_XML] = addr_dump_xml,
1264 .co_dump[NL_DUMP_ENV] = addr_dump_env,
1265 .co_filter = addr_filter,
1268 static void __init addr_init(void)
1270 nl_cache_mngt_register(&rtnl_addr_ops);
1273 static void __exit neigh_exit(void)
1275 nl_cache_mngt_unregister(&rtnl_addr_ops);