ipaddress.c 18 KB

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