addr.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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-2006 Thomas Graf <tgraf@suug.ch>
  10. * Baruch Even <baruch@ev-en.org>,
  11. * Mediatrix Telecom, inc. <ericb@mediatrix.com>
  12. */
  13. /**
  14. * @ingroup rtnl
  15. * @defgroup rtaddr Addresses
  16. * @brief
  17. *
  18. * @note The maximum size of an address label is IFNAMSIZ.
  19. *
  20. * @note The address may not contain a prefix length if the peer address
  21. * has been specified already.
  22. *
  23. * @par 1) Address Addition
  24. * @code
  25. * // Allocate an empty address object to be filled out with the attributes
  26. * // of the new address.
  27. * struct rtnl_addr *addr = rtnl_addr_alloc();
  28. *
  29. * // Fill out the mandatory attributes of the new address. Setting the
  30. * // local address will automatically set the address family and the
  31. * // prefix length to the correct values.
  32. * rtnl_addr_set_ifindex(addr, ifindex);
  33. * rtnl_addr_set_local(addr, local_addr);
  34. *
  35. * // The label of the address can be specified, currently only supported
  36. * // by IPv4 and DECnet.
  37. * rtnl_addr_set_label(addr, "mylabel");
  38. *
  39. * // The peer address can be specified if necessary, in either case a peer
  40. * // address will be sent to the kernel in order to fullfil the interface
  41. * // requirements. If none is set, it will equal the local address.
  42. * // Note: Real peer addresses are only supported by IPv4 for now.
  43. * rtnl_addr_set_peer(addr, peer_addr);
  44. *
  45. * // In case you want to have the address have a scope other than global
  46. * // it may be overwritten using rtnl_addr_set_scope(). The scope currently
  47. * // cannot be set for IPv6 addresses.
  48. * rtnl_addr_set_scope(addr, rtnl_str2scope("site"));
  49. *
  50. * // Broadcast and anycast address may be specified using the relevant
  51. * // functions, the address family will be verified if one of the other
  52. * // addresses has been set already. Currently only works for IPv4.
  53. * rtnl_addr_set_broadcast(addr, broadcast_addr);
  54. * rtnl_addr_set_anycast(addr, anycast_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(handle, 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(handle, addr, 0);
  102. *
  103. * // Free the memory
  104. * rtnl_addr_put(addr);
  105. * @endcode
  106. * @{
  107. */
  108. #include <netlink-local.h>
  109. #include <netlink/netlink.h>
  110. #include <netlink/route/rtnl.h>
  111. #include <netlink/route/addr.h>
  112. #include <netlink/route/route.h>
  113. #include <netlink/route/link.h>
  114. #include <netlink/utils.h>
  115. /** @cond SKIP */
  116. #define ADDR_ATTR_FAMILY 0x0001
  117. #define ADDR_ATTR_PREFIXLEN 0x0002
  118. #define ADDR_ATTR_FLAGS 0x0004
  119. #define ADDR_ATTR_SCOPE 0x0008
  120. #define ADDR_ATTR_IFINDEX 0x0010
  121. #define ADDR_ATTR_LABEL 0x0020
  122. #define ADDR_ATTR_CACHEINFO 0x0040
  123. #define ADDR_ATTR_PEER 0x0080
  124. #define ADDR_ATTR_LOCAL 0x0100
  125. #define ADDR_ATTR_BROADCAST 0x0200
  126. #define ADDR_ATTR_ANYCAST 0x0400
  127. #define ADDR_ATTR_MULTICAST 0x0800
  128. static struct nl_cache_ops rtnl_addr_ops;
  129. static struct nl_object_ops addr_obj_ops;
  130. /** @endcond */
  131. static void addr_free_data(struct nl_object *obj)
  132. {
  133. struct rtnl_addr *addr = nl_object_priv(obj);
  134. if (!addr)
  135. return;
  136. nl_addr_put(addr->a_peer);
  137. nl_addr_put(addr->a_local);
  138. nl_addr_put(addr->a_bcast);
  139. nl_addr_put(addr->a_anycast);
  140. nl_addr_put(addr->a_multicast);
  141. }
  142. static int addr_clone(struct nl_object *_dst, struct nl_object *_src)
  143. {
  144. struct rtnl_addr *dst = nl_object_priv(_dst);
  145. struct rtnl_addr *src = nl_object_priv(_src);
  146. if (src->a_peer)
  147. if (!(dst->a_peer = nl_addr_clone(src->a_peer)))
  148. goto errout;
  149. if (src->a_local)
  150. if (!(dst->a_local = nl_addr_clone(src->a_local)))
  151. goto errout;
  152. if (src->a_bcast)
  153. if (!(dst->a_bcast = nl_addr_clone(src->a_bcast)))
  154. goto errout;
  155. if (src->a_anycast)
  156. if (!(dst->a_anycast = nl_addr_clone(src->a_anycast)))
  157. goto errout;
  158. if (src->a_multicast)
  159. if (!(dst->a_multicast = nl_addr_clone(src->a_multicast)))
  160. goto errout;
  161. return 0;
  162. errout:
  163. return nl_get_errno();
  164. }
  165. static struct nla_policy addr_policy[IFA_MAX+1] = {
  166. [IFA_LABEL] = { .type = NLA_STRING,
  167. .maxlen = IFNAMSIZ },
  168. [IFA_CACHEINFO] = { .minlen = sizeof(struct ifa_cacheinfo) },
  169. };
  170. static int addr_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
  171. struct nlmsghdr *nlh, struct nl_parser_param *pp)
  172. {
  173. struct rtnl_addr *addr;
  174. struct ifaddrmsg *ifa;
  175. struct nlattr *tb[IFA_MAX+1];
  176. int err = -ENOMEM, peer_prefix = 0;
  177. addr = rtnl_addr_alloc();
  178. if (!addr) {
  179. err = nl_errno(ENOMEM);
  180. goto errout;
  181. }
  182. addr->ce_msgtype = nlh->nlmsg_type;
  183. err = nlmsg_parse(nlh, sizeof(*ifa), tb, IFA_MAX, addr_policy);
  184. if (err < 0)
  185. goto errout_free;
  186. ifa = nlmsg_data(nlh);
  187. addr->a_family = ifa->ifa_family;
  188. addr->a_prefixlen = ifa->ifa_prefixlen;
  189. addr->a_flags = ifa->ifa_flags;
  190. addr->a_scope = ifa->ifa_scope;
  191. addr->a_ifindex = ifa->ifa_index;
  192. addr->ce_mask = (ADDR_ATTR_FAMILY | ADDR_ATTR_PREFIXLEN |
  193. ADDR_ATTR_FLAGS | ADDR_ATTR_SCOPE | ADDR_ATTR_IFINDEX);
  194. if (tb[IFA_LABEL]) {
  195. nla_strlcpy(addr->a_label, tb[IFA_LABEL], IFNAMSIZ);
  196. addr->ce_mask |= ADDR_ATTR_LABEL;
  197. }
  198. if (tb[IFA_CACHEINFO]) {
  199. struct ifa_cacheinfo *ca;
  200. ca = nla_data(tb[IFA_CACHEINFO]);
  201. addr->a_cacheinfo.aci_prefered = ca->ifa_prefered;
  202. addr->a_cacheinfo.aci_valid = ca->ifa_valid;
  203. addr->a_cacheinfo.aci_cstamp = ca->cstamp;
  204. addr->a_cacheinfo.aci_tstamp = ca->tstamp;
  205. addr->ce_mask |= ADDR_ATTR_CACHEINFO;
  206. }
  207. if (tb[IFA_LOCAL]) {
  208. addr->a_local = nla_get_addr(tb[IFA_LOCAL], addr->a_family);
  209. if (!addr->a_local)
  210. goto errout_free;
  211. addr->ce_mask |= ADDR_ATTR_LOCAL;
  212. }
  213. if (tb[IFA_ADDRESS]) {
  214. struct nl_addr *a;
  215. a = nla_get_addr(tb[IFA_ADDRESS], addr->a_family);
  216. if (!a)
  217. goto errout_free;
  218. /* IPv6 sends the local address as IFA_ADDRESS with
  219. * no IFA_LOCAL, IPv4 sends both IFA_LOCAL and IFA_ADDRESS
  220. * with IFA_ADDRESS being the peer address if they differ */
  221. if (!tb[IFA_LOCAL] || !nl_addr_cmp(a, addr->a_local)) {
  222. nl_addr_put(addr->a_local);
  223. addr->a_local = a;
  224. addr->ce_mask |= ADDR_ATTR_LOCAL;
  225. } else {
  226. addr->a_peer = a;
  227. addr->ce_mask |= ADDR_ATTR_PEER;
  228. peer_prefix = 1;
  229. }
  230. }
  231. nl_addr_set_prefixlen(peer_prefix ? addr->a_peer : addr->a_local,
  232. addr->a_prefixlen);
  233. if (tb[IFA_BROADCAST]) {
  234. addr->a_bcast = nla_get_addr(tb[IFA_BROADCAST], addr->a_family);
  235. if (!addr->a_bcast)
  236. goto errout_free;
  237. addr->ce_mask |= ADDR_ATTR_BROADCAST;
  238. }
  239. if (tb[IFA_ANYCAST]) {
  240. addr->a_anycast = nla_get_addr(tb[IFA_ANYCAST], addr->a_family);
  241. if (!addr->a_anycast)
  242. goto errout_free;
  243. addr->ce_mask |= ADDR_ATTR_ANYCAST;
  244. }
  245. if (tb[IFA_MULTICAST]) {
  246. addr->a_multicast = nla_get_addr(tb[IFA_MULTICAST],
  247. addr->a_family);
  248. if (!addr->a_multicast)
  249. goto errout_free;
  250. addr->ce_mask |= ADDR_ATTR_MULTICAST;
  251. }
  252. err = pp->pp_cb((struct nl_object *) addr, pp);
  253. if (err < 0)
  254. goto errout_free;
  255. err = P_ACCEPT;
  256. errout_free:
  257. rtnl_addr_put(addr);
  258. errout:
  259. return err;
  260. }
  261. static int addr_request_update(struct nl_cache *cache, struct nl_handle *handle)
  262. {
  263. return nl_rtgen_request(handle, RTM_GETADDR, AF_UNSPEC, NLM_F_DUMP);
  264. }
  265. static int addr_dump_brief(struct nl_object *obj, struct nl_dump_params *p)
  266. {
  267. struct rtnl_addr *addr = (struct rtnl_addr *) obj;
  268. struct nl_cache *link_cache;
  269. char buf[128];
  270. link_cache = nl_cache_mngt_require("route/link");
  271. if (addr->ce_mask & ADDR_ATTR_LOCAL)
  272. dp_dump(p, "%s",
  273. nl_addr2str(addr->a_local, buf, sizeof(buf)));
  274. else
  275. dp_dump(p, "none");
  276. if (addr->ce_mask & ADDR_ATTR_PEER)
  277. dp_dump(p, " peer %s",
  278. nl_addr2str(addr->a_peer, buf, sizeof(buf)));
  279. dp_dump(p, " %s ", nl_af2str(addr->a_family, buf, sizeof(buf)));
  280. if (link_cache)
  281. dp_dump(p, "dev %s ",
  282. rtnl_link_i2name(link_cache, addr->a_ifindex,
  283. buf, sizeof(buf)));
  284. else
  285. dp_dump(p, "dev %d ", addr->a_ifindex);
  286. dp_dump(p, "scope %s",
  287. rtnl_scope2str(addr->a_scope, buf, sizeof(buf)));
  288. rtnl_addr_flags2str(addr->a_flags, buf, sizeof(buf));
  289. if (buf[0])
  290. dp_dump(p, " <%s>", buf);
  291. dp_dump(p, "\n");
  292. return 1;
  293. }
  294. static int addr_dump_full(struct nl_object *obj, struct nl_dump_params *p)
  295. {
  296. struct rtnl_addr *addr = (struct rtnl_addr *) obj;
  297. int line = addr_dump_brief(obj, p);
  298. char buf[128];
  299. if (addr->ce_mask & (ADDR_ATTR_LABEL | ADDR_ATTR_BROADCAST |
  300. ADDR_ATTR_ANYCAST | ADDR_ATTR_MULTICAST)) {
  301. dp_dump_line(p, line++, " ");
  302. if (addr->ce_mask & ADDR_ATTR_LABEL)
  303. dp_dump(p, " label %s", addr->a_label);
  304. if (addr->ce_mask & ADDR_ATTR_BROADCAST)
  305. dp_dump(p, " broadcast %s",
  306. nl_addr2str(addr->a_bcast, buf, sizeof(buf)));
  307. if (addr->ce_mask & ADDR_ATTR_ANYCAST)
  308. dp_dump(p, " anycast %s",
  309. nl_addr2str(addr->a_anycast, buf,
  310. sizeof(buf)));
  311. if (addr->ce_mask & ADDR_ATTR_MULTICAST)
  312. dp_dump(p, " multicast %s",
  313. nl_addr2str(addr->a_multicast, buf,
  314. sizeof(buf)));
  315. dp_dump(p, "\n");
  316. }
  317. if (addr->ce_mask & ADDR_ATTR_CACHEINFO) {
  318. struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
  319. dp_dump_line(p, line++, " valid-lifetime %s",
  320. ci->aci_valid == 0xFFFFFFFFU ? "forever" :
  321. nl_msec2str(ci->aci_valid * 1000,
  322. buf, sizeof(buf)));
  323. dp_dump(p, " preferred-lifetime %s\n",
  324. ci->aci_prefered == 0xFFFFFFFFU ? "forever" :
  325. nl_msec2str(ci->aci_prefered * 1000,
  326. buf, sizeof(buf)));
  327. dp_dump_line(p, line++, " created boot-time+%s ",
  328. nl_msec2str(addr->a_cacheinfo.aci_cstamp * 10,
  329. buf, sizeof(buf)));
  330. dp_dump(p, "last-updated boot-time+%s\n",
  331. nl_msec2str(addr->a_cacheinfo.aci_tstamp * 10,
  332. buf, sizeof(buf)));
  333. }
  334. return line;
  335. }
  336. static int addr_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
  337. {
  338. return addr_dump_full(obj, p);
  339. }
  340. static int addr_dump_xml(struct nl_object *obj, struct nl_dump_params *p)
  341. {
  342. struct rtnl_addr *addr = (struct rtnl_addr *) obj;
  343. struct nl_cache *link_cache;
  344. char buf[128];
  345. int line = 0;
  346. dp_dump_line(p, line++, "<address>\n");
  347. dp_dump_line(p, line++, " <family>%s</family>\n",
  348. nl_af2str(addr->a_family, buf, sizeof(buf)));
  349. if (addr->ce_mask & ADDR_ATTR_LOCAL)
  350. dp_dump_line(p, line++, " <local>%s</local>\n",
  351. nl_addr2str(addr->a_local, buf, sizeof(buf)));
  352. if (addr->ce_mask & ADDR_ATTR_PEER)
  353. dp_dump_line(p, line++, " <peer>%s</peer>\n",
  354. nl_addr2str(addr->a_peer, buf, sizeof(buf)));
  355. if (addr->ce_mask & ADDR_ATTR_BROADCAST)
  356. dp_dump_line(p, line++, " <broadcast>%s</broadcast>\n",
  357. nl_addr2str(addr->a_bcast, buf, sizeof(buf)));
  358. if (addr->ce_mask & ADDR_ATTR_ANYCAST)
  359. dp_dump_line(p, line++, " <anycast>%s</anycast>\n",
  360. nl_addr2str(addr->a_anycast, buf, sizeof(buf)));
  361. if (addr->ce_mask & ADDR_ATTR_MULTICAST)
  362. dp_dump_line(p, line++, " <multicast>%s</multicast>\n",
  363. nl_addr2str(addr->a_multicast, buf,
  364. sizeof(buf)));
  365. if (addr->ce_mask & ADDR_ATTR_PREFIXLEN)
  366. dp_dump_line(p, line++, " <prefixlen>%u</prefixlen>\n",
  367. addr->a_prefixlen);
  368. link_cache = nl_cache_mngt_require("route/link");
  369. if (link_cache)
  370. dp_dump_line(p, line++, " <device>%s</device>\n",
  371. rtnl_link_i2name(link_cache, addr->a_ifindex,
  372. buf, sizeof(buf)));
  373. else
  374. dp_dump_line(p, line++, " <device>%u</device>\n",
  375. addr->a_ifindex);
  376. if (addr->ce_mask & ADDR_ATTR_SCOPE)
  377. dp_dump_line(p, line++, " <scope>%s</scope>\n",
  378. rtnl_scope2str(addr->a_scope, buf, sizeof(buf)));
  379. if (addr->ce_mask & ADDR_ATTR_LABEL)
  380. dp_dump_line(p, line++, " <label>%s</label>\n", addr->a_label);
  381. rtnl_addr_flags2str(addr->a_flags, buf, sizeof(buf));
  382. if (buf[0])
  383. dp_dump_line(p, line++, " <flags>%s</flags>\n", buf);
  384. if (addr->ce_mask & ADDR_ATTR_CACHEINFO) {
  385. struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
  386. dp_dump_line(p, line++, " <cacheinfo>\n");
  387. dp_dump_line(p, line++, " <valid>%s</valid>\n",
  388. ci->aci_valid == 0xFFFFFFFFU ? "forever" :
  389. nl_msec2str(ci->aci_valid * 1000,
  390. buf, sizeof(buf)));
  391. dp_dump_line(p, line++, " <prefered>%s</prefered>\n",
  392. ci->aci_prefered == 0xFFFFFFFFU ? "forever" :
  393. nl_msec2str(ci->aci_prefered * 1000,
  394. buf, sizeof(buf)));
  395. dp_dump_line(p, line++, " <created>%s</created>\n",
  396. nl_msec2str(addr->a_cacheinfo.aci_cstamp * 10,
  397. buf, sizeof(buf)));
  398. dp_dump_line(p, line++, " <last-update>%s</last-update>\n",
  399. nl_msec2str(addr->a_cacheinfo.aci_tstamp * 10,
  400. buf, sizeof(buf)));
  401. dp_dump_line(p, line++, " </cacheinfo>\n");
  402. }
  403. dp_dump_line(p, line++, "</address>\n");
  404. return line;
  405. }
  406. static int addr_dump_env(struct nl_object *obj, struct nl_dump_params *p)
  407. {
  408. struct rtnl_addr *addr = (struct rtnl_addr *) obj;
  409. struct nl_cache *link_cache;
  410. char buf[128];
  411. int line = 0;
  412. dp_dump_line(p, line++, "ADDR_FAMILY=%s\n",
  413. nl_af2str(addr->a_family, buf, sizeof(buf)));
  414. if (addr->ce_mask & ADDR_ATTR_LOCAL)
  415. dp_dump_line(p, line++, "ADDR_LOCAL=%s\n",
  416. nl_addr2str(addr->a_local, buf, sizeof(buf)));
  417. if (addr->ce_mask & ADDR_ATTR_PEER)
  418. dp_dump_line(p, line++, "ADDR_PEER=%s\n",
  419. nl_addr2str(addr->a_peer, buf, sizeof(buf)));
  420. if (addr->ce_mask & ADDR_ATTR_BROADCAST)
  421. dp_dump_line(p, line++, "ADDR_BROADCAST=%s\n",
  422. nl_addr2str(addr->a_bcast, buf, sizeof(buf)));
  423. if (addr->ce_mask & ADDR_ATTR_ANYCAST)
  424. dp_dump_line(p, line++, "ADDR_ANYCAST=%s\n",
  425. nl_addr2str(addr->a_anycast, buf, sizeof(buf)));
  426. if (addr->ce_mask & ADDR_ATTR_MULTICAST)
  427. dp_dump_line(p, line++, "ADDR_MULTICAST=%s\n",
  428. nl_addr2str(addr->a_multicast, buf,
  429. sizeof(buf)));
  430. if (addr->ce_mask & ADDR_ATTR_PREFIXLEN)
  431. dp_dump_line(p, line++, "ADDR_PREFIXLEN=%u\n",
  432. addr->a_prefixlen);
  433. link_cache = nl_cache_mngt_require("route/link");
  434. dp_dump_line(p, line++, "ADDR_IFINDEX=%u\n", addr->a_ifindex);
  435. if (link_cache)
  436. dp_dump_line(p, line++, "ADDR_IFNAME=%s\n",
  437. rtnl_link_i2name(link_cache, addr->a_ifindex,
  438. buf, sizeof(buf)));
  439. if (addr->ce_mask & ADDR_ATTR_SCOPE)
  440. dp_dump_line(p, line++, "ADDR_SCOPE=%s\n",
  441. rtnl_scope2str(addr->a_scope, buf, sizeof(buf)));
  442. if (addr->ce_mask & ADDR_ATTR_LABEL)
  443. dp_dump_line(p, line++, "ADDR_LABEL=%s\n", addr->a_label);
  444. rtnl_addr_flags2str(addr->a_flags, buf, sizeof(buf));
  445. if (buf[0])
  446. dp_dump_line(p, line++, "ADDR_FLAGS=%s\n", buf);
  447. if (addr->ce_mask & ADDR_ATTR_CACHEINFO) {
  448. struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
  449. dp_dump_line(p, line++, "ADDR_CACHEINFO_VALID=%s\n",
  450. ci->aci_valid == 0xFFFFFFFFU ? "forever" :
  451. nl_msec2str(ci->aci_valid * 1000,
  452. buf, sizeof(buf)));
  453. dp_dump_line(p, line++, "ADDR_CACHEINFO_PREFERED=%s\n",
  454. ci->aci_prefered == 0xFFFFFFFFU ? "forever" :
  455. nl_msec2str(ci->aci_prefered * 1000,
  456. buf, sizeof(buf)));
  457. dp_dump_line(p, line++, "ADDR_CACHEINFO_CREATED=%s\n",
  458. nl_msec2str(addr->a_cacheinfo.aci_cstamp * 10,
  459. buf, sizeof(buf)));
  460. dp_dump_line(p, line++, "ADDR_CACHEINFO_LASTUPDATE=%s\n",
  461. nl_msec2str(addr->a_cacheinfo.aci_tstamp * 10,
  462. buf, sizeof(buf)));
  463. }
  464. return line;
  465. }
  466. static int addr_compare(struct nl_object *_a, struct nl_object *_b,
  467. uint32_t attrs, int flags)
  468. {
  469. struct rtnl_addr *a = (struct rtnl_addr *) _a;
  470. struct rtnl_addr *b = (struct rtnl_addr *) _b;
  471. int diff = 0;
  472. #define ADDR_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ADDR_ATTR_##ATTR, a, b, EXPR)
  473. diff |= ADDR_DIFF(IFINDEX, a->a_ifindex != b->a_ifindex);
  474. diff |= ADDR_DIFF(FAMILY, a->a_family != b->a_family);
  475. diff |= ADDR_DIFF(SCOPE, a->a_scope != b->a_scope);
  476. diff |= ADDR_DIFF(LABEL, strcmp(a->a_label, b->a_label));
  477. diff |= ADDR_DIFF(PEER, nl_addr_cmp(a->a_peer, b->a_peer));
  478. diff |= ADDR_DIFF(LOCAL, nl_addr_cmp(a->a_local, b->a_local));
  479. diff |= ADDR_DIFF(ANYCAST, nl_addr_cmp(a->a_anycast,b->a_anycast));
  480. diff |= ADDR_DIFF(MULTICAST, nl_addr_cmp(a->a_multicast,
  481. b->a_multicast));
  482. diff |= ADDR_DIFF(BROADCAST, nl_addr_cmp(a->a_bcast, b->a_bcast));
  483. if (flags & LOOSE_FLAG_COMPARISON)
  484. diff |= ADDR_DIFF(FLAGS,
  485. (a->a_flags ^ b->a_flags) & b->a_flag_mask);
  486. else
  487. diff |= ADDR_DIFF(FLAGS, a->a_flags != b->a_flags);
  488. #undef ADDR_DIFF
  489. return diff;
  490. }
  491. static struct trans_tbl addr_attrs[] = {
  492. __ADD(ADDR_ATTR_FAMILY, family)
  493. __ADD(ADDR_ATTR_PREFIXLEN, prefixlen)
  494. __ADD(ADDR_ATTR_FLAGS, flags)
  495. __ADD(ADDR_ATTR_SCOPE, scope)
  496. __ADD(ADDR_ATTR_IFINDEX, ifindex)
  497. __ADD(ADDR_ATTR_LABEL, label)
  498. __ADD(ADDR_ATTR_CACHEINFO, cacheinfo)
  499. __ADD(ADDR_ATTR_PEER, peer)
  500. __ADD(ADDR_ATTR_LOCAL, local)
  501. __ADD(ADDR_ATTR_BROADCAST, broadcast)
  502. __ADD(ADDR_ATTR_ANYCAST, anycast)
  503. __ADD(ADDR_ATTR_MULTICAST, multicast)
  504. };
  505. static char *addr_attrs2str(int attrs, char *buf, size_t len)
  506. {
  507. return __flags2str(attrs, buf, len, addr_attrs,
  508. ARRAY_SIZE(addr_attrs));
  509. }
  510. /**
  511. * @name Allocation/Freeing
  512. * @{
  513. */
  514. struct rtnl_addr *rtnl_addr_alloc(void)
  515. {
  516. return (struct rtnl_addr *) nl_object_alloc(&addr_obj_ops);
  517. }
  518. void rtnl_addr_put(struct rtnl_addr *addr)
  519. {
  520. nl_object_put((struct nl_object *) addr);
  521. }
  522. /** @} */
  523. /**
  524. * @name Cache Management
  525. * @{
  526. */
  527. struct nl_cache *rtnl_addr_alloc_cache(struct nl_handle *handle)
  528. {
  529. struct nl_cache *cache;
  530. cache = nl_cache_alloc(&rtnl_addr_ops);
  531. if (!cache)
  532. return NULL;
  533. if (handle && nl_cache_refill(handle, cache) < 0) {
  534. nl_cache_free(cache);
  535. return NULL;
  536. }
  537. return cache;
  538. }
  539. /** @} */
  540. static struct nl_msg *build_addr_msg(struct rtnl_addr *tmpl, int cmd, int flags)
  541. {
  542. struct nl_msg *msg;
  543. struct ifaddrmsg am = {
  544. .ifa_family = tmpl->a_family,
  545. .ifa_index = tmpl->a_ifindex,
  546. .ifa_prefixlen = tmpl->a_prefixlen,
  547. };
  548. if (tmpl->ce_mask & ADDR_ATTR_SCOPE)
  549. am.ifa_scope = tmpl->a_scope;
  550. else {
  551. /* compatibility hack */
  552. if (tmpl->a_family == AF_INET &&
  553. tmpl->ce_mask & ADDR_ATTR_LOCAL &&
  554. *((char *) nl_addr_get_binary_addr(tmpl->a_local)) == 127)
  555. am.ifa_scope = RT_SCOPE_HOST;
  556. else
  557. am.ifa_scope = RT_SCOPE_UNIVERSE;
  558. }
  559. msg = nlmsg_alloc_simple(cmd, flags);
  560. if (!msg)
  561. goto nla_put_failure;
  562. if (nlmsg_append(msg, &am, sizeof(am), NLMSG_ALIGNTO) < 0)
  563. goto nla_put_failure;
  564. if (tmpl->ce_mask & ADDR_ATTR_LOCAL)
  565. NLA_PUT_ADDR(msg, IFA_LOCAL, tmpl->a_local);
  566. if (tmpl->ce_mask & ADDR_ATTR_PEER)
  567. NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_peer);
  568. else
  569. NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_local);
  570. if (tmpl->ce_mask & ADDR_ATTR_LABEL)
  571. NLA_PUT_STRING(msg, IFA_LABEL, tmpl->a_label);
  572. if (tmpl->ce_mask & ADDR_ATTR_BROADCAST)
  573. NLA_PUT_ADDR(msg, IFA_BROADCAST, tmpl->a_bcast);
  574. if (tmpl->ce_mask & ADDR_ATTR_ANYCAST)
  575. NLA_PUT_ADDR(msg, IFA_ANYCAST, tmpl->a_anycast);
  576. return msg;
  577. nla_put_failure:
  578. nlmsg_free(msg);
  579. return NULL;
  580. }
  581. /**
  582. * @name Addition
  583. * @{
  584. */
  585. /**
  586. * Build netlink request message to request addition of new address
  587. * @arg addr Address object representing the new address.
  588. * @arg flags Additional netlink message flags.
  589. *
  590. * Builds a new netlink message requesting the addition of a new
  591. * address. The netlink message header isn't fully equipped with
  592. * all relevant fields and must thus be sent out via nl_send_auto_complete()
  593. * or supplemented as needed.
  594. *
  595. * Minimal required attributes:
  596. * - interface index (rtnl_addr_set_ifindex())
  597. * - local address (rtnl_addr_set_local())
  598. *
  599. * The scope will default to universe except for loopback addresses in
  600. * which case a host scope is used if not specified otherwise.
  601. *
  602. * @note Free the memory after usage using nlmsg_free().
  603. * @return Newly allocated netlink message or NULL if an error occured.
  604. */
  605. struct nl_msg *rtnl_addr_build_add_request(struct rtnl_addr *addr, int flags)
  606. {
  607. int required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY |
  608. ADDR_ATTR_PREFIXLEN | ADDR_ATTR_LOCAL;
  609. if ((addr->ce_mask & required) != required) {
  610. nl_error(EINVAL, "Missing mandatory attributes, required are: "
  611. "ifindex, family, prefixlen, local address.");
  612. return NULL;
  613. }
  614. return build_addr_msg(addr, RTM_NEWADDR, NLM_F_CREATE | flags);
  615. }
  616. /**
  617. * Request addition of new address
  618. * @arg handle Netlink handle.
  619. * @arg addr Address object representing the new address.
  620. * @arg flags Additional netlink message flags.
  621. *
  622. * Builds a netlink message by calling rtnl_addr_build_add_request(),
  623. * sends the request to the kernel and waits for the next ACK to be
  624. * received and thus blocks until the request has been fullfilled.
  625. *
  626. * @see rtnl_addr_build_add_request()
  627. *
  628. * @return 0 on sucess or a negative error if an error occured.
  629. */
  630. int rtnl_addr_add(struct nl_handle *handle, struct rtnl_addr *addr, int flags)
  631. {
  632. struct nl_msg *msg;
  633. int err;
  634. msg = rtnl_addr_build_add_request(addr, flags);
  635. if (!msg)
  636. return nl_get_errno();
  637. err = nl_send_auto_complete(handle, msg);
  638. nlmsg_free(msg);
  639. if (err < 0)
  640. return err;
  641. return nl_wait_for_ack(handle);
  642. }
  643. /** @} */
  644. /**
  645. * @name Deletion
  646. * @{
  647. */
  648. /**
  649. * Build a netlink request message to request deletion of an address
  650. * @arg addr Address object to be deleteted.
  651. * @arg flags Additional netlink message flags.
  652. *
  653. * Builds a new netlink message requesting a deletion of an address.
  654. * The netlink message header isn't fully equipped with all relevant
  655. * fields and must thus be sent out via nl_send_auto_complete()
  656. * or supplemented as needed.
  657. *
  658. * Minimal required attributes:
  659. * - interface index (rtnl_addr_set_ifindex())
  660. * - address family (rtnl_addr_set_family())
  661. *
  662. * Optional attributes:
  663. * - local address (rtnl_addr_set_local())
  664. * - label (rtnl_addr_set_label(), IPv4/DECnet only)
  665. * - peer address (rtnl_addr_set_peer(), IPv4 only)
  666. *
  667. * @note Free the memory after usage using nlmsg_free().
  668. * @return Newly allocated netlink message or NULL if an error occured.
  669. */
  670. struct nl_msg *rtnl_addr_build_delete_request(struct rtnl_addr *addr, int flags)
  671. {
  672. int required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY;
  673. if ((addr->ce_mask & required) != required) {
  674. nl_error(EINVAL, "Missing mandatory attributes, required are: "
  675. "ifindex, family");
  676. return NULL;
  677. }
  678. return build_addr_msg(addr, RTM_DELADDR, flags);
  679. }
  680. /**
  681. * Request deletion of an address
  682. * @arg handle Netlink handle.
  683. * @arg addr Address object to be deleted.
  684. * @arg flags Additional netlink message flags.
  685. *
  686. * Builds a netlink message by calling rtnl_addr_build_delete_request(),
  687. * sends the request to the kernel and waits for the next ACK to be
  688. * received and thus blocks until the request has been fullfilled.
  689. *
  690. * @see rtnl_addr_build_delete_request();
  691. *
  692. * @return 0 on sucess or a negative error if an error occured.
  693. */
  694. int rtnl_addr_delete(struct nl_handle *handle, struct rtnl_addr *addr,
  695. int flags)
  696. {
  697. struct nl_msg *msg;
  698. int err;
  699. msg = rtnl_addr_build_delete_request(addr, flags);
  700. if (!msg)
  701. return nl_get_errno();
  702. err = nl_send_auto_complete(handle, msg);
  703. nlmsg_free(msg);
  704. if (err < 0)
  705. return err;
  706. return nl_wait_for_ack(handle);
  707. }
  708. /** @} */
  709. /**
  710. * @name Attributes
  711. * @{
  712. */
  713. void rtnl_addr_set_label(struct rtnl_addr *addr, const char *label)
  714. {
  715. strncpy(addr->a_label, label, sizeof(addr->a_label) - 1);
  716. addr->ce_mask |= ADDR_ATTR_LABEL;
  717. }
  718. char *rtnl_addr_get_label(struct rtnl_addr *addr)
  719. {
  720. if (addr->ce_mask & ADDR_ATTR_LABEL)
  721. return addr->a_label;
  722. else
  723. return NULL;
  724. }
  725. void rtnl_addr_set_ifindex(struct rtnl_addr *addr, int ifindex)
  726. {
  727. addr->a_ifindex = ifindex;
  728. addr->ce_mask |= ADDR_ATTR_IFINDEX;
  729. }
  730. int rtnl_addr_get_ifindex(struct rtnl_addr *addr)
  731. {
  732. if (addr->ce_mask & ADDR_ATTR_IFINDEX)
  733. return addr->a_ifindex;
  734. else
  735. return RTNL_LINK_NOT_FOUND;
  736. }
  737. void rtnl_addr_set_family(struct rtnl_addr *addr, int family)
  738. {
  739. addr->a_family = family;
  740. addr->ce_mask |= ADDR_ATTR_FAMILY;
  741. }
  742. int rtnl_addr_get_family(struct rtnl_addr *addr)
  743. {
  744. if (addr->ce_mask & ADDR_ATTR_FAMILY)
  745. return addr->a_family;
  746. else
  747. return AF_UNSPEC;
  748. }
  749. void rtnl_addr_set_prefixlen(struct rtnl_addr *addr, int prefix)
  750. {
  751. addr->a_prefixlen = prefix;
  752. addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
  753. }
  754. int rtnl_addr_get_prefixlen(struct rtnl_addr *addr)
  755. {
  756. if (addr->ce_mask & ADDR_ATTR_PREFIXLEN)
  757. return addr->a_prefixlen;
  758. else
  759. return -1;
  760. }
  761. void rtnl_addr_set_scope(struct rtnl_addr *addr, int scope)
  762. {
  763. addr->a_scope = scope;
  764. addr->ce_mask |= ADDR_ATTR_SCOPE;
  765. }
  766. int rtnl_addr_get_scope(struct rtnl_addr *addr)
  767. {
  768. if (addr->ce_mask & ADDR_ATTR_SCOPE)
  769. return addr->a_scope;
  770. else
  771. return -1;
  772. }
  773. void rtnl_addr_set_flags(struct rtnl_addr *addr, unsigned int flags)
  774. {
  775. addr->a_flag_mask |= flags;
  776. addr->a_flags |= flags;
  777. addr->ce_mask |= ADDR_ATTR_FLAGS;
  778. }
  779. void rtnl_addr_unset_flags(struct rtnl_addr *addr, unsigned int flags)
  780. {
  781. addr->a_flag_mask |= flags;
  782. addr->a_flags &= ~flags;
  783. addr->ce_mask |= ADDR_ATTR_FLAGS;
  784. }
  785. unsigned int rtnl_addr_get_flags(struct rtnl_addr *addr)
  786. {
  787. return addr->a_flags;
  788. }
  789. static inline int __assign_addr(struct rtnl_addr *addr, struct nl_addr **pos,
  790. struct nl_addr *new, int flag)
  791. {
  792. if (addr->ce_mask & ADDR_ATTR_FAMILY) {
  793. if (new->a_family != addr->a_family)
  794. return nl_error(EINVAL, "Address family mismatch");
  795. } else
  796. addr->a_family = new->a_family;
  797. if (*pos)
  798. nl_addr_put(*pos);
  799. *pos = nl_addr_get(new);
  800. addr->ce_mask |= (flag | ADDR_ATTR_FAMILY);
  801. return 0;
  802. }
  803. int rtnl_addr_set_local(struct rtnl_addr *addr, struct nl_addr *local)
  804. {
  805. int err;
  806. err = __assign_addr(addr, &addr->a_local, local, ADDR_ATTR_LOCAL);
  807. if (err < 0)
  808. return err;
  809. if (!(addr->ce_mask & ADDR_ATTR_PEER)) {
  810. addr->a_prefixlen = nl_addr_get_prefixlen(addr->a_local);
  811. addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
  812. }
  813. return 0;
  814. }
  815. struct nl_addr *rtnl_addr_get_local(struct rtnl_addr *addr)
  816. {
  817. if (addr->ce_mask & ADDR_ATTR_LOCAL)
  818. return addr->a_local;
  819. else
  820. return NULL;
  821. }
  822. int rtnl_addr_set_peer(struct rtnl_addr *addr, struct nl_addr *peer)
  823. {
  824. return __assign_addr(addr, &addr->a_peer, peer, ADDR_ATTR_PEER);
  825. addr->a_prefixlen = nl_addr_get_prefixlen(addr->a_peer);
  826. addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
  827. return 0;
  828. }
  829. struct nl_addr *rtnl_addr_get_peer(struct rtnl_addr *addr)
  830. {
  831. if (addr->ce_mask & ADDR_ATTR_PEER)
  832. return addr->a_peer;
  833. else
  834. return NULL;
  835. }
  836. int rtnl_addr_set_broadcast(struct rtnl_addr *addr, struct nl_addr *bcast)
  837. {
  838. return __assign_addr(addr, &addr->a_bcast, bcast, ADDR_ATTR_BROADCAST);
  839. }
  840. struct nl_addr *rtnl_addr_get_broadcast(struct rtnl_addr *addr)
  841. {
  842. if (addr->ce_mask & ADDR_ATTR_BROADCAST)
  843. return addr->a_bcast;
  844. else
  845. return NULL;
  846. }
  847. int rtnl_addr_set_anycast(struct rtnl_addr *addr, struct nl_addr *anycast)
  848. {
  849. return __assign_addr(addr, &addr->a_anycast, anycast,
  850. ADDR_ATTR_ANYCAST);
  851. }
  852. struct nl_addr *rtnl_addr_get_anycast(struct rtnl_addr *addr)
  853. {
  854. if (addr->ce_mask & ADDR_ATTR_ANYCAST)
  855. return addr->a_anycast;
  856. else
  857. return NULL;
  858. }
  859. int rtnl_addr_set_multicast(struct rtnl_addr *addr, struct nl_addr *multicast)
  860. {
  861. return __assign_addr(addr, &addr->a_multicast, multicast,
  862. ADDR_ATTR_MULTICAST);
  863. }
  864. struct nl_addr *rtnl_addr_get_multicast(struct rtnl_addr *addr)
  865. {
  866. if (addr->ce_mask & ADDR_ATTR_MULTICAST)
  867. return addr->a_multicast;
  868. else
  869. return NULL;
  870. }
  871. /** @} */
  872. /**
  873. * @name Flags Translations
  874. * @{
  875. */
  876. static struct trans_tbl addr_flags[] = {
  877. __ADD(IFA_F_SECONDARY, secondary)
  878. __ADD(IFA_F_DEPRECATED, deprecated)
  879. __ADD(IFA_F_TENTATIVE, tentative)
  880. __ADD(IFA_F_PERMANENT, permanent)
  881. };
  882. char *rtnl_addr_flags2str(int flags, char *buf, size_t size)
  883. {
  884. return __flags2str(flags, buf, size, addr_flags,
  885. ARRAY_SIZE(addr_flags));
  886. }
  887. int rtnl_addr_str2flags(const char *name)
  888. {
  889. return __str2flags(name, addr_flags, ARRAY_SIZE(addr_flags));
  890. }
  891. /** @} */
  892. static struct nl_object_ops addr_obj_ops = {
  893. .oo_name = "route/addr",
  894. .oo_size = sizeof(struct rtnl_addr),
  895. .oo_free_data = addr_free_data,
  896. .oo_clone = addr_clone,
  897. .oo_dump[NL_DUMP_BRIEF] = addr_dump_brief,
  898. .oo_dump[NL_DUMP_FULL] = addr_dump_full,
  899. .oo_dump[NL_DUMP_STATS] = addr_dump_stats,
  900. .oo_dump[NL_DUMP_XML] = addr_dump_xml,
  901. .oo_dump[NL_DUMP_ENV] = addr_dump_env,
  902. .oo_compare = addr_compare,
  903. .oo_attrs2str = addr_attrs2str,
  904. .oo_id_attrs = (ADDR_ATTR_FAMILY | ADDR_ATTR_IFINDEX |
  905. ADDR_ATTR_LOCAL | ADDR_ATTR_PREFIXLEN |
  906. ADDR_ATTR_PEER),
  907. };
  908. static struct nl_af_group addr_groups[] = {
  909. { AF_INET, RTNLGRP_IPV4_IFADDR },
  910. { AF_INET6, RTNLGRP_IPV6_IFADDR },
  911. { END_OF_GROUP_LIST },
  912. };
  913. static struct nl_cache_ops rtnl_addr_ops = {
  914. .co_name = "route/addr",
  915. .co_hdrsize = sizeof(struct ifaddrmsg),
  916. .co_msgtypes = {
  917. { RTM_NEWADDR, NL_ACT_NEW, "new" },
  918. { RTM_DELADDR, NL_ACT_DEL, "del" },
  919. { RTM_GETADDR, NL_ACT_GET, "get" },
  920. END_OF_MSGTYPES_LIST,
  921. },
  922. .co_protocol = NETLINK_ROUTE,
  923. .co_groups = addr_groups,
  924. .co_request_update = addr_request_update,
  925. .co_msg_parser = addr_msg_parser,
  926. .co_obj_ops = &addr_obj_ops,
  927. };
  928. static void __init addr_init(void)
  929. {
  930. nl_cache_mngt_register(&rtnl_addr_ops);
  931. }
  932. static void __exit addr_exit(void)
  933. {
  934. nl_cache_mngt_unregister(&rtnl_addr_ops);
  935. }
  936. /** @} */