ipaddress.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ipaddress.c "ip address".
  4. *
  5. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  6. *
  7. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  8. *
  9. * Changes:
  10. * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
  11. */
  12. //#include <sys/socket.h>
  13. //#include <sys/ioctl.h>
  14. #include <fnmatch.h>
  15. #include <net/if.h>
  16. #include <net/if_arp.h>
  17. #include "ip_common.h" /* #include "libbb.h" is inside */
  18. #include "rt_names.h"
  19. #include "utils.h"
  20. typedef struct filter_t {
  21. int ifindex;
  22. int family;
  23. int oneline;
  24. int showqueue;
  25. inet_prefix pfx;
  26. int scope, scopemask;
  27. int flags, flagmask;
  28. int up;
  29. char *label;
  30. int flushed;
  31. char *flushb;
  32. int flushp;
  33. int flushe;
  34. struct rtnl_handle *rth;
  35. } filter_t;
  36. #define filter (*(filter_t*)&bb_common_bufsiz1)
  37. static void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
  38. {
  39. fprintf(fp, "<");
  40. flags &= ~IFF_RUNNING;
  41. #define _PF(f) if (flags&IFF_##f) { \
  42. flags &= ~IFF_##f; \
  43. fprintf(fp, #f "%s", flags ? "," : ""); }
  44. _PF(LOOPBACK);
  45. _PF(BROADCAST);
  46. _PF(POINTOPOINT);
  47. _PF(MULTICAST);
  48. _PF(NOARP);
  49. #if 0
  50. _PF(ALLMULTI);
  51. _PF(PROMISC);
  52. _PF(MASTER);
  53. _PF(SLAVE);
  54. _PF(DEBUG);
  55. _PF(DYNAMIC);
  56. _PF(AUTOMEDIA);
  57. _PF(PORTSEL);
  58. _PF(NOTRAILERS);
  59. #endif
  60. _PF(UP);
  61. #undef _PF
  62. if (flags)
  63. fprintf(fp, "%x", flags);
  64. if (mdown)
  65. fprintf(fp, ",M-DOWN");
  66. fprintf(fp, "> ");
  67. }
  68. static void print_queuelen(char *name)
  69. {
  70. struct ifreq ifr;
  71. int s;
  72. s = socket(AF_INET, SOCK_STREAM, 0);
  73. if (s < 0)
  74. return;
  75. memset(&ifr, 0, sizeof(ifr));
  76. strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
  77. if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) {
  78. close(s);
  79. return;
  80. }
  81. close(s);
  82. if (ifr.ifr_qlen)
  83. printf("qlen %d", ifr.ifr_qlen);
  84. }
  85. static int print_linkinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
  86. const struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
  87. {
  88. FILE *fp = (FILE*)arg;
  89. struct ifinfomsg *ifi = NLMSG_DATA(n);
  90. struct rtattr * tb[IFLA_MAX+1];
  91. int len = n->nlmsg_len;
  92. unsigned m_flag = 0;
  93. if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
  94. return 0;
  95. len -= NLMSG_LENGTH(sizeof(*ifi));
  96. if (len < 0)
  97. return -1;
  98. if (filter.ifindex && ifi->ifi_index != filter.ifindex)
  99. return 0;
  100. if (filter.up && !(ifi->ifi_flags&IFF_UP))
  101. return 0;
  102. memset(tb, 0, sizeof(tb));
  103. parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
  104. if (tb[IFLA_IFNAME] == NULL) {
  105. bb_error_msg("nil ifname");
  106. return -1;
  107. }
  108. if (filter.label
  109. && (!filter.family || filter.family == AF_PACKET)
  110. && fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)
  111. ) {
  112. return 0;
  113. }
  114. if (n->nlmsg_type == RTM_DELLINK)
  115. fprintf(fp, "Deleted ");
  116. fprintf(fp, "%d: %s", ifi->ifi_index,
  117. tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
  118. if (tb[IFLA_LINK]) {
  119. SPRINT_BUF(b1);
  120. int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
  121. if (iflink == 0)
  122. fprintf(fp, "@NONE: ");
  123. else {
  124. fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
  125. m_flag = ll_index_to_flags(iflink);
  126. m_flag = !(m_flag & IFF_UP);
  127. }
  128. } else {
  129. fprintf(fp, ": ");
  130. }
  131. print_link_flags(fp, ifi->ifi_flags, m_flag);
  132. if (tb[IFLA_MTU])
  133. fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
  134. if (tb[IFLA_QDISC])
  135. fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
  136. #ifdef IFLA_MASTER
  137. if (tb[IFLA_MASTER]) {
  138. SPRINT_BUF(b1);
  139. fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
  140. }
  141. #endif
  142. if (filter.showqueue)
  143. print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
  144. if (!filter.family || filter.family == AF_PACKET) {
  145. SPRINT_BUF(b1);
  146. fprintf(fp, "%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
  147. if (tb[IFLA_ADDRESS]) {
  148. fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
  149. RTA_PAYLOAD(tb[IFLA_ADDRESS]),
  150. ifi->ifi_type,
  151. b1, sizeof(b1)), fp);
  152. }
  153. if (tb[IFLA_BROADCAST]) {
  154. if (ifi->ifi_flags & IFF_POINTOPOINT)
  155. fprintf(fp, " peer ");
  156. else
  157. fprintf(fp, " brd ");
  158. fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
  159. RTA_PAYLOAD(tb[IFLA_BROADCAST]),
  160. ifi->ifi_type,
  161. b1, sizeof(b1)), fp);
  162. }
  163. }
  164. fputc('\n', fp);
  165. fflush(fp);
  166. return 0;
  167. }
  168. static int flush_update(void)
  169. {
  170. if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
  171. bb_perror_msg("failed to send flush request");
  172. return -1;
  173. }
  174. filter.flushp = 0;
  175. return 0;
  176. }
  177. static int print_addrinfo(struct sockaddr_nl ATTRIBUTE_UNUSED *who,
  178. struct nlmsghdr *n, void ATTRIBUTE_UNUSED *arg)
  179. {
  180. FILE *fp = (FILE*)arg;
  181. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  182. int len = n->nlmsg_len;
  183. struct rtattr * rta_tb[IFA_MAX+1];
  184. char abuf[256];
  185. SPRINT_BUF(b1);
  186. if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
  187. return 0;
  188. len -= NLMSG_LENGTH(sizeof(*ifa));
  189. if (len < 0) {
  190. bb_error_msg("wrong nlmsg len %d", len);
  191. return -1;
  192. }
  193. if (filter.flushb && n->nlmsg_type != RTM_NEWADDR)
  194. return 0;
  195. memset(rta_tb, 0, sizeof(rta_tb));
  196. parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
  197. if (!rta_tb[IFA_LOCAL])
  198. rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
  199. if (!rta_tb[IFA_ADDRESS])
  200. rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
  201. if (filter.ifindex && filter.ifindex != ifa->ifa_index)
  202. return 0;
  203. if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
  204. return 0;
  205. if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
  206. return 0;
  207. if (filter.label) {
  208. const char *label;
  209. if (rta_tb[IFA_LABEL])
  210. label = RTA_DATA(rta_tb[IFA_LABEL]);
  211. else
  212. label = ll_idx_n2a(ifa->ifa_index, b1);
  213. if (fnmatch(filter.label, label, 0) != 0)
  214. return 0;
  215. }
  216. if (filter.pfx.family) {
  217. if (rta_tb[IFA_LOCAL]) {
  218. inet_prefix dst;
  219. memset(&dst, 0, sizeof(dst));
  220. dst.family = ifa->ifa_family;
  221. memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
  222. if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
  223. return 0;
  224. }
  225. }
  226. if (filter.flushb) {
  227. struct nlmsghdr *fn;
  228. if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
  229. if (flush_update())
  230. return -1;
  231. }
  232. fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
  233. memcpy(fn, n, n->nlmsg_len);
  234. fn->nlmsg_type = RTM_DELADDR;
  235. fn->nlmsg_flags = NLM_F_REQUEST;
  236. fn->nlmsg_seq = ++filter.rth->seq;
  237. filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
  238. filter.flushed++;
  239. return 0;
  240. }
  241. if (n->nlmsg_type == RTM_DELADDR)
  242. fprintf(fp, "Deleted ");
  243. if (filter.oneline)
  244. fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
  245. if (ifa->ifa_family == AF_INET)
  246. fprintf(fp, " inet ");
  247. else if (ifa->ifa_family == AF_INET6)
  248. fprintf(fp, " inet6 ");
  249. else
  250. fprintf(fp, " family %d ", ifa->ifa_family);
  251. if (rta_tb[IFA_LOCAL]) {
  252. fputs(rt_addr_n2a(ifa->ifa_family,
  253. RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
  254. RTA_DATA(rta_tb[IFA_LOCAL]),
  255. abuf, sizeof(abuf)), fp);
  256. if (rta_tb[IFA_ADDRESS] == NULL ||
  257. memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
  258. fprintf(fp, "/%d ", ifa->ifa_prefixlen);
  259. } else {
  260. fprintf(fp, " peer %s/%d ",
  261. rt_addr_n2a(ifa->ifa_family,
  262. RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
  263. RTA_DATA(rta_tb[IFA_ADDRESS]),
  264. abuf, sizeof(abuf)),
  265. ifa->ifa_prefixlen);
  266. }
  267. }
  268. if (rta_tb[IFA_BROADCAST]) {
  269. fprintf(fp, "brd %s ",
  270. rt_addr_n2a(ifa->ifa_family,
  271. RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
  272. RTA_DATA(rta_tb[IFA_BROADCAST]),
  273. abuf, sizeof(abuf)));
  274. }
  275. if (rta_tb[IFA_ANYCAST]) {
  276. fprintf(fp, "any %s ",
  277. rt_addr_n2a(ifa->ifa_family,
  278. RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
  279. RTA_DATA(rta_tb[IFA_ANYCAST]),
  280. abuf, sizeof(abuf)));
  281. }
  282. fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
  283. if (ifa->ifa_flags&IFA_F_SECONDARY) {
  284. ifa->ifa_flags &= ~IFA_F_SECONDARY;
  285. fprintf(fp, "secondary ");
  286. }
  287. if (ifa->ifa_flags&IFA_F_TENTATIVE) {
  288. ifa->ifa_flags &= ~IFA_F_TENTATIVE;
  289. fprintf(fp, "tentative ");
  290. }
  291. if (ifa->ifa_flags&IFA_F_DEPRECATED) {
  292. ifa->ifa_flags &= ~IFA_F_DEPRECATED;
  293. fprintf(fp, "deprecated ");
  294. }
  295. if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
  296. fprintf(fp, "dynamic ");
  297. } else
  298. ifa->ifa_flags &= ~IFA_F_PERMANENT;
  299. if (ifa->ifa_flags)
  300. fprintf(fp, "flags %02x ", ifa->ifa_flags);
  301. if (rta_tb[IFA_LABEL])
  302. fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), fp);
  303. if (rta_tb[IFA_CACHEINFO]) {
  304. struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
  305. char buf[128];
  306. fputc(_SL_, fp);
  307. if (ci->ifa_valid == 0xFFFFFFFFU)
  308. sprintf(buf, "valid_lft forever");
  309. else
  310. sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
  311. if (ci->ifa_prefered == 0xFFFFFFFFU)
  312. sprintf(buf+strlen(buf), " preferred_lft forever");
  313. else
  314. sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
  315. fprintf(fp, " %s", buf);
  316. }
  317. fputc('\n', fp);
  318. fflush(fp);
  319. return 0;
  320. }
  321. struct nlmsg_list
  322. {
  323. struct nlmsg_list *next;
  324. struct nlmsghdr h;
  325. };
  326. static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
  327. {
  328. for (; ainfo; ainfo = ainfo->next) {
  329. struct nlmsghdr *n = &ainfo->h;
  330. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  331. if (n->nlmsg_type != RTM_NEWADDR)
  332. continue;
  333. if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
  334. return -1;
  335. if (ifa->ifa_index != ifindex ||
  336. (filter.family && filter.family != ifa->ifa_family))
  337. continue;
  338. print_addrinfo(NULL, n, fp);
  339. }
  340. return 0;
  341. }
  342. static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
  343. {
  344. struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
  345. struct nlmsg_list *h;
  346. struct nlmsg_list **lp;
  347. h = malloc(n->nlmsg_len+sizeof(void*));
  348. if (h == NULL)
  349. return -1;
  350. memcpy(&h->h, n, n->nlmsg_len);
  351. h->next = NULL;
  352. for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
  353. *lp = h;
  354. ll_remember_index(who, n, NULL);
  355. return 0;
  356. }
  357. static void ipaddr_reset_filter(int _oneline)
  358. {
  359. memset(&filter, 0, sizeof(filter));
  360. filter.oneline = _oneline;
  361. }
  362. /* Return value becomes exitcode. It's okay to not return at all */
  363. int ipaddr_list_or_flush(char **argv, int flush)
  364. {
  365. static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0";
  366. struct nlmsg_list *linfo = NULL;
  367. struct nlmsg_list *ainfo = NULL;
  368. struct nlmsg_list *l;
  369. struct rtnl_handle rth;
  370. char *filter_dev = NULL;
  371. int no_link = 0;
  372. ipaddr_reset_filter(oneline);
  373. filter.showqueue = 1;
  374. if (filter.family == AF_UNSPEC)
  375. filter.family = preferred_family;
  376. if (flush) {
  377. if (!*argv) {
  378. bb_error_msg_and_die(bb_msg_requires_arg, "flush");
  379. }
  380. if (filter.family == AF_PACKET) {
  381. bb_error_msg_and_die("cannot flush link addresses");
  382. }
  383. }
  384. while (*argv) {
  385. const int option_num = index_in_strings(option, *argv);
  386. switch (option_num) {
  387. case 0: /* to */
  388. NEXT_ARG();
  389. get_prefix(&filter.pfx, *argv, filter.family);
  390. if (filter.family == AF_UNSPEC) {
  391. filter.family = filter.pfx.family;
  392. }
  393. break;
  394. case 1: /* scope */
  395. {
  396. uint32_t scope = 0;
  397. NEXT_ARG();
  398. filter.scopemask = -1;
  399. if (rtnl_rtscope_a2n(&scope, *argv)) {
  400. if (strcmp(*argv, "all") != 0) {
  401. invarg(*argv, "scope");
  402. }
  403. scope = RT_SCOPE_NOWHERE;
  404. filter.scopemask = 0;
  405. }
  406. filter.scope = scope;
  407. break;
  408. }
  409. case 2: /* up */
  410. filter.up = 1;
  411. break;
  412. case 3: /* label */
  413. NEXT_ARG();
  414. filter.label = *argv;
  415. break;
  416. case 4: /* dev */
  417. NEXT_ARG();
  418. default:
  419. if (filter_dev) {
  420. duparg2("dev", *argv);
  421. }
  422. filter_dev = *argv;
  423. }
  424. argv++;
  425. }
  426. xrtnl_open(&rth);
  427. xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
  428. xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
  429. if (filter_dev) {
  430. filter.ifindex = xll_name_to_index(filter_dev);
  431. }
  432. if (flush) {
  433. char flushb[4096-512];
  434. filter.flushb = flushb;
  435. filter.flushp = 0;
  436. filter.flushe = sizeof(flushb);
  437. filter.rth = &rth;
  438. for (;;) {
  439. xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
  440. filter.flushed = 0;
  441. xrtnl_dump_filter(&rth, print_addrinfo, stdout);
  442. if (filter.flushed == 0) {
  443. return 0;
  444. }
  445. if (flush_update() < 0)
  446. return 1;
  447. }
  448. }
  449. if (filter.family != AF_PACKET) {
  450. xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
  451. xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
  452. }
  453. if (filter.family && filter.family != AF_PACKET) {
  454. struct nlmsg_list **lp;
  455. lp = &linfo;
  456. if (filter.oneline)
  457. no_link = 1;
  458. while ((l = *lp) != NULL) {
  459. int ok = 0;
  460. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  461. struct nlmsg_list *a;
  462. for (a = ainfo; a; a = a->next) {
  463. struct nlmsghdr *n = &a->h;
  464. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  465. if (ifa->ifa_index != ifi->ifi_index ||
  466. (filter.family && filter.family != ifa->ifa_family))
  467. continue;
  468. if ((filter.scope ^ ifa->ifa_scope) & filter.scopemask)
  469. continue;
  470. if ((filter.flags ^ ifa->ifa_flags) & filter.flagmask)
  471. continue;
  472. if (filter.pfx.family || filter.label) {
  473. struct rtattr *tb[IFA_MAX+1];
  474. memset(tb, 0, sizeof(tb));
  475. parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
  476. if (!tb[IFA_LOCAL])
  477. tb[IFA_LOCAL] = tb[IFA_ADDRESS];
  478. if (filter.pfx.family && tb[IFA_LOCAL]) {
  479. inet_prefix dst;
  480. memset(&dst, 0, sizeof(dst));
  481. dst.family = ifa->ifa_family;
  482. memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
  483. if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
  484. continue;
  485. }
  486. if (filter.label) {
  487. SPRINT_BUF(b1);
  488. const char *label;
  489. if (tb[IFA_LABEL])
  490. label = RTA_DATA(tb[IFA_LABEL]);
  491. else
  492. label = ll_idx_n2a(ifa->ifa_index, b1);
  493. if (fnmatch(filter.label, label, 0) != 0)
  494. continue;
  495. }
  496. }
  497. ok = 1;
  498. break;
  499. }
  500. if (!ok)
  501. *lp = l->next;
  502. else
  503. lp = &l->next;
  504. }
  505. }
  506. for (l = linfo; l; l = l->next) {
  507. if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
  508. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  509. if (filter.family != AF_PACKET)
  510. print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
  511. }
  512. }
  513. return 0;
  514. }
  515. static int default_scope(inet_prefix *lcl)
  516. {
  517. if (lcl->family == AF_INET) {
  518. if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
  519. return RT_SCOPE_HOST;
  520. }
  521. return 0;
  522. }
  523. /* Return value becomes exitcode. It's okay to not return at all */
  524. static int ipaddr_modify(int cmd, char **argv)
  525. {
  526. static const char option[] ALIGN1 =
  527. "peer\0""remote\0""broadcast\0""brd\0"
  528. "anycast\0""scope\0""dev\0""label\0""local\0";
  529. struct rtnl_handle rth;
  530. struct {
  531. struct nlmsghdr n;
  532. struct ifaddrmsg ifa;
  533. char buf[256];
  534. } req;
  535. char *d = NULL;
  536. char *l = NULL;
  537. inet_prefix lcl;
  538. inet_prefix peer;
  539. int local_len = 0;
  540. int peer_len = 0;
  541. int brd_len = 0;
  542. int any_len = 0;
  543. bool scoped = 0;
  544. memset(&req, 0, sizeof(req));
  545. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
  546. req.n.nlmsg_flags = NLM_F_REQUEST;
  547. req.n.nlmsg_type = cmd;
  548. req.ifa.ifa_family = preferred_family;
  549. while (*argv) {
  550. const int option_num = index_in_strings(option, *argv);
  551. switch (option_num) {
  552. case 0: /* peer */
  553. case 1: /* remote */
  554. NEXT_ARG();
  555. if (peer_len) {
  556. duparg("peer", *argv);
  557. }
  558. get_prefix(&peer, *argv, req.ifa.ifa_family);
  559. peer_len = peer.bytelen;
  560. if (req.ifa.ifa_family == AF_UNSPEC) {
  561. req.ifa.ifa_family = peer.family;
  562. }
  563. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
  564. req.ifa.ifa_prefixlen = peer.bitlen;
  565. break;
  566. case 2: /* broadcast */
  567. case 3: /* brd */
  568. {
  569. inet_prefix addr;
  570. NEXT_ARG();
  571. if (brd_len) {
  572. duparg("broadcast", *argv);
  573. }
  574. if (LONE_CHAR(*argv, '+')) {
  575. brd_len = -1;
  576. } else if (LONE_DASH(*argv)) {
  577. brd_len = -2;
  578. } else {
  579. get_addr(&addr, *argv, req.ifa.ifa_family);
  580. if (req.ifa.ifa_family == AF_UNSPEC)
  581. req.ifa.ifa_family = addr.family;
  582. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
  583. brd_len = addr.bytelen;
  584. }
  585. break;
  586. }
  587. case 4: /* anycast */
  588. {
  589. inet_prefix addr;
  590. NEXT_ARG();
  591. if (any_len) {
  592. duparg("anycast", *argv);
  593. }
  594. get_addr(&addr, *argv, req.ifa.ifa_family);
  595. if (req.ifa.ifa_family == AF_UNSPEC) {
  596. req.ifa.ifa_family = addr.family;
  597. }
  598. addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
  599. any_len = addr.bytelen;
  600. break;
  601. }
  602. case 5: /* scope */
  603. {
  604. uint32_t scope = 0;
  605. NEXT_ARG();
  606. if (rtnl_rtscope_a2n(&scope, *argv)) {
  607. invarg(*argv, "scope");
  608. }
  609. req.ifa.ifa_scope = scope;
  610. scoped = 1;
  611. break;
  612. }
  613. case 6: /* dev */
  614. NEXT_ARG();
  615. d = *argv;
  616. break;
  617. case 7: /* label */
  618. NEXT_ARG();
  619. l = *argv;
  620. addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
  621. break;
  622. case 8: /* local */
  623. NEXT_ARG();
  624. default:
  625. if (local_len) {
  626. duparg2("local", *argv);
  627. }
  628. get_prefix(&lcl, *argv, req.ifa.ifa_family);
  629. if (req.ifa.ifa_family == AF_UNSPEC) {
  630. req.ifa.ifa_family = lcl.family;
  631. }
  632. addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
  633. local_len = lcl.bytelen;
  634. }
  635. argv++;
  636. }
  637. if (d == NULL) {
  638. bb_error_msg(bb_msg_requires_arg, "\"dev\"");
  639. return -1;
  640. }
  641. if (l && strncmp(d, l, strlen(d)) != 0) {
  642. bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
  643. }
  644. if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
  645. peer = lcl;
  646. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
  647. }
  648. if (req.ifa.ifa_prefixlen == 0)
  649. req.ifa.ifa_prefixlen = lcl.bitlen;
  650. if (brd_len < 0 && cmd != RTM_DELADDR) {
  651. inet_prefix brd;
  652. int i;
  653. if (req.ifa.ifa_family != AF_INET) {
  654. bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
  655. }
  656. brd = peer;
  657. if (brd.bitlen <= 30) {
  658. for (i=31; i>=brd.bitlen; i--) {
  659. if (brd_len == -1)
  660. brd.data[0] |= htonl(1<<(31-i));
  661. else
  662. brd.data[0] &= ~htonl(1<<(31-i));
  663. }
  664. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
  665. brd_len = brd.bytelen;
  666. }
  667. }
  668. if (!scoped && cmd != RTM_DELADDR)
  669. req.ifa.ifa_scope = default_scope(&lcl);
  670. xrtnl_open(&rth);
  671. ll_init_map(&rth);
  672. req.ifa.ifa_index = xll_name_to_index(d);
  673. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  674. return 2;
  675. return 0;
  676. }
  677. /* Return value becomes exitcode. It's okay to not return at all */
  678. int do_ipaddr(char **argv)
  679. {
  680. static const char commands[] ALIGN1 =
  681. "add\0""delete\0""list\0""show\0""lst\0""flush\0";
  682. int command_num = 2; /* default command is list */
  683. if (*argv) {
  684. command_num = index_in_substrings(commands, *argv);
  685. if (command_num < 0 || command_num > 5)
  686. bb_error_msg_and_die("unknown command %s", *argv);
  687. argv++;
  688. }
  689. if (command_num == 0) /* add */
  690. return ipaddr_modify(RTM_NEWADDR, argv);
  691. if (command_num == 1) /* delete */
  692. return ipaddr_modify(RTM_DELADDR, argv);
  693. if (command_num == 5) /* flush */
  694. return ipaddr_list_or_flush(argv, 1);
  695. /* 2 == list, 3 == show, 4 == lst */
  696. return ipaddr_list_or_flush(argv, 0);
  697. }