3
0

ipaddress.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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(int argc, 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 (argc <= 0) {
  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 (argc > 0) {
  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. argc--;
  426. }
  427. xrtnl_open(&rth);
  428. xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
  429. xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
  430. if (filter_dev) {
  431. filter.ifindex = xll_name_to_index(filter_dev);
  432. }
  433. if (flush) {
  434. char flushb[4096-512];
  435. filter.flushb = flushb;
  436. filter.flushp = 0;
  437. filter.flushe = sizeof(flushb);
  438. filter.rth = &rth;
  439. for (;;) {
  440. xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
  441. filter.flushed = 0;
  442. xrtnl_dump_filter(&rth, print_addrinfo, stdout);
  443. if (filter.flushed == 0) {
  444. return 0;
  445. }
  446. if (flush_update() < 0)
  447. return 1;
  448. }
  449. }
  450. if (filter.family != AF_PACKET) {
  451. xrtnl_wilddump_request(&rth, filter.family, RTM_GETADDR);
  452. xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
  453. }
  454. if (filter.family && filter.family != AF_PACKET) {
  455. struct nlmsg_list **lp;
  456. lp=&linfo;
  457. if (filter.oneline)
  458. no_link = 1;
  459. while ((l=*lp)!=NULL) {
  460. int ok = 0;
  461. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  462. struct nlmsg_list *a;
  463. for (a=ainfo; a; a=a->next) {
  464. struct nlmsghdr *n = &a->h;
  465. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  466. if (ifa->ifa_index != ifi->ifi_index ||
  467. (filter.family && filter.family != ifa->ifa_family))
  468. continue;
  469. if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
  470. continue;
  471. if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
  472. continue;
  473. if (filter.pfx.family || filter.label) {
  474. struct rtattr *tb[IFA_MAX+1];
  475. memset(tb, 0, sizeof(tb));
  476. parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
  477. if (!tb[IFA_LOCAL])
  478. tb[IFA_LOCAL] = tb[IFA_ADDRESS];
  479. if (filter.pfx.family && tb[IFA_LOCAL]) {
  480. inet_prefix dst;
  481. memset(&dst, 0, sizeof(dst));
  482. dst.family = ifa->ifa_family;
  483. memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
  484. if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
  485. continue;
  486. }
  487. if (filter.label) {
  488. SPRINT_BUF(b1);
  489. const char *label;
  490. if (tb[IFA_LABEL])
  491. label = RTA_DATA(tb[IFA_LABEL]);
  492. else
  493. label = ll_idx_n2a(ifa->ifa_index, b1);
  494. if (fnmatch(filter.label, label, 0) != 0)
  495. continue;
  496. }
  497. }
  498. ok = 1;
  499. break;
  500. }
  501. if (!ok)
  502. *lp = l->next;
  503. else
  504. lp = &l->next;
  505. }
  506. }
  507. for (l = linfo; l; l = l->next) {
  508. if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
  509. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  510. if (filter.family != AF_PACKET)
  511. print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
  512. }
  513. fflush(stdout); /* why? */
  514. }
  515. return 0;
  516. }
  517. static int default_scope(inet_prefix *lcl)
  518. {
  519. if (lcl->family == AF_INET) {
  520. if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
  521. return RT_SCOPE_HOST;
  522. }
  523. return 0;
  524. }
  525. /* Return value becomes exitcode. It's okay to not return at all */
  526. static int ipaddr_modify(int cmd, int argc, char **argv)
  527. {
  528. static const char option[] ALIGN1 =
  529. "peer\0""remote\0""broadcast\0""brd\0"
  530. "anycast\0""scope\0""dev\0""label\0""local\0";
  531. struct rtnl_handle rth;
  532. struct {
  533. struct nlmsghdr n;
  534. struct ifaddrmsg ifa;
  535. char buf[256];
  536. } req;
  537. char *d = NULL;
  538. char *l = NULL;
  539. inet_prefix lcl;
  540. inet_prefix peer;
  541. int local_len = 0;
  542. int peer_len = 0;
  543. int brd_len = 0;
  544. int any_len = 0;
  545. bool scoped = 0;
  546. memset(&req, 0, sizeof(req));
  547. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
  548. req.n.nlmsg_flags = NLM_F_REQUEST;
  549. req.n.nlmsg_type = cmd;
  550. req.ifa.ifa_family = preferred_family;
  551. while (argc > 0) {
  552. const int option_num = index_in_strings(option, *argv);
  553. switch (option_num) {
  554. case 0: /* peer */
  555. case 1: /* remote */
  556. NEXT_ARG();
  557. if (peer_len) {
  558. duparg("peer", *argv);
  559. }
  560. get_prefix(&peer, *argv, req.ifa.ifa_family);
  561. peer_len = peer.bytelen;
  562. if (req.ifa.ifa_family == AF_UNSPEC) {
  563. req.ifa.ifa_family = peer.family;
  564. }
  565. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
  566. req.ifa.ifa_prefixlen = peer.bitlen;
  567. break;
  568. case 2: /* broadcast */
  569. case 3: /* brd */
  570. {
  571. inet_prefix addr;
  572. NEXT_ARG();
  573. if (brd_len) {
  574. duparg("broadcast", *argv);
  575. }
  576. if (LONE_CHAR(*argv, '+')) {
  577. brd_len = -1;
  578. }
  579. else if (LONE_DASH(*argv)) {
  580. brd_len = -2;
  581. } else {
  582. get_addr(&addr, *argv, req.ifa.ifa_family);
  583. if (req.ifa.ifa_family == AF_UNSPEC)
  584. req.ifa.ifa_family = addr.family;
  585. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
  586. brd_len = addr.bytelen;
  587. }
  588. break;
  589. }
  590. case 4: /* anycast */
  591. {
  592. inet_prefix addr;
  593. NEXT_ARG();
  594. if (any_len) {
  595. duparg("anycast", *argv);
  596. }
  597. get_addr(&addr, *argv, req.ifa.ifa_family);
  598. if (req.ifa.ifa_family == AF_UNSPEC) {
  599. req.ifa.ifa_family = addr.family;
  600. }
  601. addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
  602. any_len = addr.bytelen;
  603. break;
  604. }
  605. case 5: /* scope */
  606. {
  607. uint32_t scope = 0;
  608. NEXT_ARG();
  609. if (rtnl_rtscope_a2n(&scope, *argv)) {
  610. invarg(*argv, "scope");
  611. }
  612. req.ifa.ifa_scope = scope;
  613. scoped = 1;
  614. break;
  615. }
  616. case 6: /* dev */
  617. NEXT_ARG();
  618. d = *argv;
  619. break;
  620. case 7: /* label */
  621. NEXT_ARG();
  622. l = *argv;
  623. addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
  624. break;
  625. case 8: /* local */
  626. NEXT_ARG();
  627. default:
  628. if (local_len) {
  629. duparg2("local", *argv);
  630. }
  631. get_prefix(&lcl, *argv, req.ifa.ifa_family);
  632. if (req.ifa.ifa_family == AF_UNSPEC) {
  633. req.ifa.ifa_family = lcl.family;
  634. }
  635. addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
  636. local_len = lcl.bytelen;
  637. }
  638. argc--;
  639. argv++;
  640. }
  641. if (d == NULL) {
  642. bb_error_msg(bb_msg_requires_arg,"\"dev\"");
  643. return -1;
  644. }
  645. if (l && strncmp(d, l, strlen(d)) != 0) {
  646. bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
  647. }
  648. if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
  649. peer = lcl;
  650. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
  651. }
  652. if (req.ifa.ifa_prefixlen == 0)
  653. req.ifa.ifa_prefixlen = lcl.bitlen;
  654. if (brd_len < 0 && cmd != RTM_DELADDR) {
  655. inet_prefix brd;
  656. int i;
  657. if (req.ifa.ifa_family != AF_INET) {
  658. bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
  659. }
  660. brd = peer;
  661. if (brd.bitlen <= 30) {
  662. for (i=31; i>=brd.bitlen; i--) {
  663. if (brd_len == -1)
  664. brd.data[0] |= htonl(1<<(31-i));
  665. else
  666. brd.data[0] &= ~htonl(1<<(31-i));
  667. }
  668. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
  669. brd_len = brd.bytelen;
  670. }
  671. }
  672. if (!scoped && cmd != RTM_DELADDR)
  673. req.ifa.ifa_scope = default_scope(&lcl);
  674. xrtnl_open(&rth);
  675. ll_init_map(&rth);
  676. req.ifa.ifa_index = xll_name_to_index(d);
  677. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  678. return 2;
  679. return 0;
  680. }
  681. /* Return value becomes exitcode. It's okay to not return at all */
  682. int do_ipaddr(int argc, char **argv)
  683. {
  684. static const char commands[] ALIGN1 =
  685. "add\0""delete\0""list\0""show\0""lst\0""flush\0";
  686. int command_num = 2; /* default command is list */
  687. if (*argv) {
  688. command_num = index_in_substrings(commands, *argv);
  689. }
  690. if (command_num < 0 || command_num > 5)
  691. bb_error_msg_and_die("unknown command %s", *argv);
  692. --argc;
  693. ++argv;
  694. if (command_num == 0) /* add */
  695. return ipaddr_modify(RTM_NEWADDR, argc, argv);
  696. else if (command_num == 1) /* delete */
  697. return ipaddr_modify(RTM_DELADDR, argc, argv);
  698. else if (command_num == 5) /* flush */
  699. return ipaddr_list_or_flush(argc, argv, 1);
  700. else /* 2 == list, 3 == show, 4 == lst */
  701. return ipaddr_list_or_flush(argc, argv, 0);
  702. }