ipaddress.c 19 KB

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