3
0

ipaddress.c 19 KB

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