ipaddress.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
  121. if (iflink == 0)
  122. printf("@NONE: ");
  123. else {
  124. printf("@%s: ", ll_index_to_name(iflink));
  125. m_flag = ll_index_to_flags(iflink);
  126. m_flag = !(m_flag & IFF_UP);
  127. }
  128. } else {
  129. printf(": ");
  130. }
  131. print_link_flags(ifi->ifi_flags, m_flag);
  132. }
  133. if (tb[IFLA_MTU])
  134. printf("mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
  135. if (tb[IFLA_QDISC])
  136. printf("qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
  137. #ifdef IFLA_MASTER
  138. if (tb[IFLA_MASTER]) {
  139. printf("master %s ", ll_index_to_name(*(int*)RTA_DATA(tb[IFLA_MASTER])));
  140. }
  141. #endif
  142. /* IFLA_OPERSTATE was added to kernel with the same commit as IFF_DORMANT */
  143. #ifdef IFF_DORMANT
  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. #endif
  152. if (G_filter.showqueue)
  153. print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
  154. if (!G_filter.family || G_filter.family == AF_PACKET) {
  155. SPRINT_BUF(b1);
  156. printf("%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1));
  157. if (tb[IFLA_ADDRESS]) {
  158. fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
  159. RTA_PAYLOAD(tb[IFLA_ADDRESS]),
  160. ifi->ifi_type,
  161. b1, sizeof(b1)), stdout);
  162. }
  163. if (tb[IFLA_BROADCAST]) {
  164. if (ifi->ifi_flags & IFF_POINTOPOINT)
  165. printf(" peer ");
  166. else
  167. printf(" brd ");
  168. fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
  169. RTA_PAYLOAD(tb[IFLA_BROADCAST]),
  170. ifi->ifi_type,
  171. b1, sizeof(b1)), stdout);
  172. }
  173. }
  174. bb_putchar('\n');
  175. /*fflush_all();*/
  176. return 0;
  177. }
  178. static int flush_update(void)
  179. {
  180. if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
  181. bb_perror_msg("can't send flush request");
  182. return -1;
  183. }
  184. G_filter.flushp = 0;
  185. return 0;
  186. }
  187. static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM,
  188. struct nlmsghdr *n, void *arg UNUSED_PARAM)
  189. {
  190. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  191. int len = n->nlmsg_len;
  192. struct rtattr * rta_tb[IFA_MAX+1];
  193. char abuf[256];
  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_index_to_name(ifa->ifa_index);
  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. }
  282. if (rta_tb[IFA_ANYCAST]) {
  283. printf("any %s ",
  284. rt_addr_n2a(ifa->ifa_family,
  285. RTA_DATA(rta_tb[IFA_ANYCAST]),
  286. abuf, sizeof(abuf))
  287. );
  288. }
  289. printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope));
  290. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  291. ifa->ifa_flags &= ~IFA_F_SECONDARY;
  292. printf("secondary ");
  293. }
  294. if (ifa->ifa_flags & IFA_F_TENTATIVE) {
  295. ifa->ifa_flags &= ~IFA_F_TENTATIVE;
  296. printf("tentative ");
  297. }
  298. if (ifa->ifa_flags & IFA_F_DEPRECATED) {
  299. ifa->ifa_flags &= ~IFA_F_DEPRECATED;
  300. printf("deprecated ");
  301. }
  302. if (!(ifa->ifa_flags & IFA_F_PERMANENT)) {
  303. printf("dynamic ");
  304. } else
  305. ifa->ifa_flags &= ~IFA_F_PERMANENT;
  306. if (ifa->ifa_flags)
  307. printf("flags %02x ", ifa->ifa_flags);
  308. if (rta_tb[IFA_LABEL])
  309. fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), stdout);
  310. if (rta_tb[IFA_CACHEINFO]) {
  311. struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
  312. char buf[128];
  313. bb_putchar(_SL_);
  314. if (ci->ifa_valid == 0xFFFFFFFFU)
  315. sprintf(buf, "valid_lft forever");
  316. else
  317. sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
  318. if (ci->ifa_prefered == 0xFFFFFFFFU)
  319. sprintf(buf+strlen(buf), " preferred_lft forever");
  320. else
  321. sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
  322. printf(" %s", buf);
  323. }
  324. bb_putchar('\n');
  325. /*fflush_all();*/
  326. return 0;
  327. }
  328. struct nlmsg_list {
  329. struct nlmsg_list *next;
  330. struct nlmsghdr h;
  331. };
  332. static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
  333. {
  334. for (; ainfo; ainfo = ainfo->next) {
  335. struct nlmsghdr *n = &ainfo->h;
  336. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  337. if (n->nlmsg_type != RTM_NEWADDR)
  338. continue;
  339. if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
  340. return -1;
  341. if (ifa->ifa_index != ifindex
  342. || (G_filter.family && G_filter.family != ifa->ifa_family)
  343. ) {
  344. continue;
  345. }
  346. print_addrinfo(NULL, n, NULL);
  347. }
  348. return 0;
  349. }
  350. static int FAST_FUNC store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
  351. {
  352. struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
  353. struct nlmsg_list *h;
  354. struct nlmsg_list **lp;
  355. h = xzalloc(n->nlmsg_len + sizeof(void*));
  356. memcpy(&h->h, n, n->nlmsg_len);
  357. /*h->next = NULL; - xzalloc did it */
  358. for (lp = linfo; *lp; lp = &(*lp)->next)
  359. continue;
  360. *lp = h;
  361. ll_remember_index(who, n, NULL);
  362. return 0;
  363. }
  364. static void ipaddr_reset_filter(int _oneline)
  365. {
  366. memset(&G_filter, 0, sizeof(G_filter));
  367. G_filter.oneline = _oneline;
  368. }
  369. /* Return value becomes exitcode. It's okay to not return at all */
  370. int FAST_FUNC ipaddr_list_or_flush(char **argv, int flush)
  371. {
  372. static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0";
  373. struct nlmsg_list *linfo = NULL;
  374. struct nlmsg_list *ainfo = NULL;
  375. struct nlmsg_list *l;
  376. struct rtnl_handle rth;
  377. char *filter_dev = NULL;
  378. int no_link = 0;
  379. ipaddr_reset_filter(oneline);
  380. G_filter.showqueue = 1;
  381. if (G_filter.family == AF_UNSPEC)
  382. G_filter.family = preferred_family;
  383. if (flush) {
  384. if (!*argv) {
  385. bb_error_msg_and_die(bb_msg_requires_arg, "flush");
  386. }
  387. if (G_filter.family == AF_PACKET) {
  388. bb_error_msg_and_die("can't flush link addresses");
  389. }
  390. }
  391. while (*argv) {
  392. const smalluint key = index_in_strings(option, *argv);
  393. if (key == 0) { /* to */
  394. NEXT_ARG();
  395. get_prefix(&G_filter.pfx, *argv, G_filter.family);
  396. if (G_filter.family == AF_UNSPEC) {
  397. G_filter.family = G_filter.pfx.family;
  398. }
  399. } else if (key == 1) { /* scope */
  400. uint32_t scope = 0;
  401. NEXT_ARG();
  402. G_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. G_filter.scopemask = 0;
  409. }
  410. G_filter.scope = scope;
  411. } else if (key == 2) { /* up */
  412. G_filter.up = 1;
  413. } else if (key == 3) { /* label */
  414. NEXT_ARG();
  415. G_filter.label = *argv;
  416. } else {
  417. if (key == 4) /* dev */
  418. NEXT_ARG();
  419. if (filter_dev)
  420. duparg2("dev", *argv);
  421. filter_dev = *argv;
  422. }
  423. argv++;
  424. }
  425. xrtnl_open(&rth);
  426. xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
  427. xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
  428. if (filter_dev) {
  429. G_filter.ifindex = xll_name_to_index(filter_dev);
  430. }
  431. if (flush) {
  432. char flushb[4096-512];
  433. G_filter.flushb = flushb;
  434. G_filter.flushp = 0;
  435. G_filter.flushe = sizeof(flushb);
  436. G_filter.rth = &rth;
  437. for (;;) {
  438. xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
  439. G_filter.flushed = 0;
  440. xrtnl_dump_filter(&rth, print_addrinfo, NULL);
  441. if (G_filter.flushed == 0) {
  442. return 0;
  443. }
  444. if (flush_update() < 0) {
  445. return 1;
  446. }
  447. }
  448. }
  449. if (G_filter.family != AF_PACKET) {
  450. xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
  451. xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
  452. }
  453. if (G_filter.family && G_filter.family != AF_PACKET) {
  454. struct nlmsg_list **lp;
  455. lp = &linfo;
  456. if (G_filter.oneline)
  457. no_link = 1;
  458. while ((l = *lp) != NULL) {
  459. int ok = 0;
  460. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  461. struct nlmsg_list *a;
  462. for (a = ainfo; a; a = a->next) {
  463. struct nlmsghdr *n = &a->h;
  464. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  465. if (ifa->ifa_index != ifi->ifi_index
  466. || (G_filter.family && G_filter.family != ifa->ifa_family)
  467. ) {
  468. continue;
  469. }
  470. if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
  471. continue;
  472. if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
  473. continue;
  474. if (G_filter.pfx.family || G_filter.label) {
  475. struct rtattr *tb[IFA_MAX+1];
  476. memset(tb, 0, sizeof(tb));
  477. parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
  478. if (!tb[IFA_LOCAL])
  479. tb[IFA_LOCAL] = tb[IFA_ADDRESS];
  480. if (G_filter.pfx.family && tb[IFA_LOCAL]) {
  481. inet_prefix dst;
  482. memset(&dst, 0, sizeof(dst));
  483. dst.family = ifa->ifa_family;
  484. memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
  485. if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
  486. continue;
  487. }
  488. if (G_filter.label) {
  489. const char *label;
  490. if (tb[IFA_LABEL])
  491. label = RTA_DATA(tb[IFA_LABEL]);
  492. else
  493. label = ll_index_to_name(ifa->ifa_index);
  494. if (fnmatch(G_filter.label, label, 0) != 0)
  495. continue;
  496. }
  497. }
  498. ok = 1;
  499. break;
  500. }
  501. if (!ok)
  502. *lp = l->next;
  503. else
  504. lp = &l->next;
  505. }
  506. }
  507. for (l = linfo; l; l = l->next) {
  508. if (no_link || print_linkinfo(&l->h) == 0) {
  509. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  510. if (G_filter.family != AF_PACKET)
  511. print_selected_addrinfo(ifi->ifi_index, ainfo);
  512. }
  513. }
  514. return 0;
  515. }
  516. static int default_scope(inet_prefix *lcl)
  517. {
  518. if (lcl->family == AF_INET) {
  519. if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
  520. return RT_SCOPE_HOST;
  521. }
  522. return 0;
  523. }
  524. /* Return value becomes exitcode. It's okay to not return at all */
  525. static int ipaddr_modify(int cmd, int flags, char **argv)
  526. {
  527. static const char option[] ALIGN1 =
  528. "peer\0""remote\0""broadcast\0""brd\0"
  529. "anycast\0""scope\0""dev\0""label\0""local\0";
  530. struct rtnl_handle rth;
  531. struct {
  532. struct nlmsghdr n;
  533. struct ifaddrmsg ifa;
  534. char buf[256];
  535. } req;
  536. char *d = NULL;
  537. char *l = NULL;
  538. inet_prefix lcl;
  539. inet_prefix peer;
  540. int local_len = 0;
  541. int peer_len = 0;
  542. int brd_len = 0;
  543. int any_len = 0;
  544. bool scoped = 0;
  545. memset(&req, 0, sizeof(req));
  546. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
  547. req.n.nlmsg_flags = NLM_F_REQUEST | flags;
  548. req.n.nlmsg_type = cmd;
  549. req.ifa.ifa_family = preferred_family;
  550. while (*argv) {
  551. unsigned arg = index_in_strings(option, *argv);
  552. /* if search fails, "local" is assumed */
  553. if ((int)arg >= 0)
  554. NEXT_ARG();
  555. if (arg <= 1) { /* peer, remote */
  556. if (peer_len) {
  557. duparg("peer", *argv);
  558. }
  559. get_prefix(&peer, *argv, req.ifa.ifa_family);
  560. peer_len = peer.bytelen;
  561. if (req.ifa.ifa_family == AF_UNSPEC) {
  562. req.ifa.ifa_family = peer.family;
  563. }
  564. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
  565. req.ifa.ifa_prefixlen = peer.bitlen;
  566. } else if (arg <= 3) { /* broadcast, brd */
  567. inet_prefix addr;
  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. } else if (arg == 4) { /* anycast */
  583. inet_prefix addr;
  584. if (any_len) {
  585. duparg("anycast", *argv);
  586. }
  587. get_addr(&addr, *argv, req.ifa.ifa_family);
  588. if (req.ifa.ifa_family == AF_UNSPEC) {
  589. req.ifa.ifa_family = addr.family;
  590. }
  591. addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
  592. any_len = addr.bytelen;
  593. } else if (arg == 5) { /* scope */
  594. uint32_t scope = 0;
  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. d = *argv;
  602. } else if (arg == 7) { /* label */
  603. l = *argv;
  604. addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l) + 1);
  605. } else {
  606. /* local (specified or assumed) */
  607. if (local_len) {
  608. duparg2("local", *argv);
  609. }
  610. get_prefix(&lcl, *argv, req.ifa.ifa_family);
  611. if (req.ifa.ifa_family == AF_UNSPEC) {
  612. req.ifa.ifa_family = lcl.family;
  613. }
  614. addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
  615. local_len = lcl.bytelen;
  616. }
  617. argv++;
  618. }
  619. if (!d) {
  620. /* There was no "dev IFACE", but we need that */
  621. bb_error_msg_and_die("need \"dev IFACE\"");
  622. }
  623. if (l && !is_prefixed_with(l, d)) {
  624. bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
  625. }
  626. if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
  627. peer = lcl;
  628. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
  629. }
  630. if (req.ifa.ifa_prefixlen == 0)
  631. req.ifa.ifa_prefixlen = lcl.bitlen;
  632. if (brd_len < 0 && cmd != RTM_DELADDR) {
  633. inet_prefix brd;
  634. int i;
  635. if (req.ifa.ifa_family != AF_INET) {
  636. bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
  637. }
  638. brd = peer;
  639. if (brd.bitlen <= 30) {
  640. for (i = 31; i >= brd.bitlen; i--) {
  641. if (brd_len == -1)
  642. brd.data[0] |= htonl(1<<(31-i));
  643. else
  644. brd.data[0] &= ~htonl(1<<(31-i));
  645. }
  646. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
  647. brd_len = brd.bytelen;
  648. }
  649. }
  650. if (!scoped && cmd != RTM_DELADDR)
  651. req.ifa.ifa_scope = default_scope(&lcl);
  652. xrtnl_open(&rth);
  653. ll_init_map(&rth);
  654. req.ifa.ifa_index = xll_name_to_index(d);
  655. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  656. return 2;
  657. return 0;
  658. }
  659. /* Return value becomes exitcode. It's okay to not return at all */
  660. int FAST_FUNC do_ipaddr(char **argv)
  661. {
  662. static const char commands[] ALIGN1 =
  663. /* 0 1 2 3 4 5 6 7 8 */
  664. "add\0""change\0""chg\0""replace\0""delete\0""list\0""show\0""lst\0""flush\0";
  665. int cmd = 2;
  666. if (*argv) {
  667. cmd = index_in_substrings(commands, *argv);
  668. if (cmd < 0)
  669. invarg(*argv, applet_name);
  670. argv++;
  671. if (cmd <= 4) {
  672. return ipaddr_modify(
  673. /*cmd:*/ cmd == 4 ? RTM_DELADDR : RTM_NEWADDR,
  674. /*flags:*/
  675. cmd == 0 ? NLM_F_CREATE|NLM_F_EXCL : /* add */
  676. cmd == 1 || cmd == 2 ? NLM_F_REPLACE : /* change */
  677. cmd == 3 ? NLM_F_CREATE|NLM_F_REPLACE : /* replace */
  678. 0 /* delete */
  679. , argv);
  680. }
  681. }
  682. return ipaddr_list_or_flush(argv, cmd == 8);
  683. }