ipaddress.c 19 KB

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