ipaddress.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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. /* IFLA_OPERSTATE was added to kernel with the same commit as IFF_DORMANT */
  145. #ifdef IFF_DORMANT
  146. if (tb[IFLA_OPERSTATE]) {
  147. static const char operstate_labels[] ALIGN1 =
  148. "UNKNOWN\0""NOTPRESENT\0""DOWN\0""LOWERLAYERDOWN\0"
  149. "TESTING\0""DORMANT\0""UP\0";
  150. printf("state %s ", nth_string(operstate_labels,
  151. *(uint8_t *)RTA_DATA(tb[IFLA_OPERSTATE])));
  152. }
  153. #endif
  154. if (G_filter.showqueue)
  155. print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
  156. if (!G_filter.family || G_filter.family == AF_PACKET) {
  157. SPRINT_BUF(b1);
  158. printf("%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1));
  159. if (tb[IFLA_ADDRESS]) {
  160. fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
  161. RTA_PAYLOAD(tb[IFLA_ADDRESS]),
  162. ifi->ifi_type,
  163. b1, sizeof(b1)), stdout);
  164. }
  165. if (tb[IFLA_BROADCAST]) {
  166. if (ifi->ifi_flags & IFF_POINTOPOINT)
  167. printf(" peer ");
  168. else
  169. printf(" brd ");
  170. fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
  171. RTA_PAYLOAD(tb[IFLA_BROADCAST]),
  172. ifi->ifi_type,
  173. b1, sizeof(b1)), stdout);
  174. }
  175. }
  176. bb_putchar('\n');
  177. /*fflush_all();*/
  178. return 0;
  179. }
  180. static int flush_update(void)
  181. {
  182. if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
  183. bb_perror_msg("can't send flush request");
  184. return -1;
  185. }
  186. G_filter.flushp = 0;
  187. return 0;
  188. }
  189. static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM,
  190. struct nlmsghdr *n, void *arg UNUSED_PARAM)
  191. {
  192. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  193. int len = n->nlmsg_len;
  194. struct rtattr * rta_tb[IFA_MAX+1];
  195. char abuf[256];
  196. SPRINT_BUF(b1);
  197. if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
  198. return 0;
  199. len -= NLMSG_LENGTH(sizeof(*ifa));
  200. if (len < 0) {
  201. bb_error_msg("wrong nlmsg len %d", len);
  202. return -1;
  203. }
  204. if (G_filter.flushb && n->nlmsg_type != RTM_NEWADDR)
  205. return 0;
  206. memset(rta_tb, 0, sizeof(rta_tb));
  207. parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
  208. if (!rta_tb[IFA_LOCAL])
  209. rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
  210. if (!rta_tb[IFA_ADDRESS])
  211. rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
  212. if (G_filter.ifindex && G_filter.ifindex != ifa->ifa_index)
  213. return 0;
  214. if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
  215. return 0;
  216. if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
  217. return 0;
  218. if (G_filter.label) {
  219. const char *label;
  220. if (rta_tb[IFA_LABEL])
  221. label = RTA_DATA(rta_tb[IFA_LABEL]);
  222. else
  223. label = ll_idx_n2a(ifa->ifa_index, b1);
  224. if (fnmatch(G_filter.label, label, 0) != 0)
  225. return 0;
  226. }
  227. if (G_filter.pfx.family) {
  228. if (rta_tb[IFA_LOCAL]) {
  229. inet_prefix dst;
  230. memset(&dst, 0, sizeof(dst));
  231. dst.family = ifa->ifa_family;
  232. memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
  233. if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
  234. return 0;
  235. }
  236. }
  237. if (G_filter.flushb) {
  238. struct nlmsghdr *fn;
  239. if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
  240. if (flush_update())
  241. return -1;
  242. }
  243. fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
  244. memcpy(fn, n, n->nlmsg_len);
  245. fn->nlmsg_type = RTM_DELADDR;
  246. fn->nlmsg_flags = NLM_F_REQUEST;
  247. fn->nlmsg_seq = ++G_filter.rth->seq;
  248. G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
  249. G_filter.flushed = 1;
  250. return 0;
  251. }
  252. if (n->nlmsg_type == RTM_DELADDR)
  253. printf("Deleted ");
  254. if (G_filter.oneline)
  255. printf("%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
  256. if (ifa->ifa_family == AF_INET)
  257. printf(" inet ");
  258. else if (ifa->ifa_family == AF_INET6)
  259. printf(" inet6 ");
  260. else
  261. printf(" family %d ", ifa->ifa_family);
  262. if (rta_tb[IFA_LOCAL]) {
  263. fputs(rt_addr_n2a(ifa->ifa_family,
  264. RTA_DATA(rta_tb[IFA_LOCAL]),
  265. abuf, sizeof(abuf)), stdout);
  266. if (rta_tb[IFA_ADDRESS] == NULL
  267. || memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0
  268. ) {
  269. printf("/%d ", ifa->ifa_prefixlen);
  270. } else {
  271. printf(" peer %s/%d ",
  272. rt_addr_n2a(ifa->ifa_family,
  273. RTA_DATA(rta_tb[IFA_ADDRESS]),
  274. abuf, sizeof(abuf)),
  275. ifa->ifa_prefixlen);
  276. }
  277. }
  278. if (rta_tb[IFA_BROADCAST]) {
  279. printf("brd %s ",
  280. rt_addr_n2a(ifa->ifa_family,
  281. RTA_DATA(rta_tb[IFA_BROADCAST]),
  282. abuf, sizeof(abuf)));
  283. }
  284. if (rta_tb[IFA_ANYCAST]) {
  285. printf("any %s ",
  286. rt_addr_n2a(ifa->ifa_family,
  287. RTA_DATA(rta_tb[IFA_ANYCAST]),
  288. abuf, sizeof(abuf)));
  289. }
  290. printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1));
  291. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  292. ifa->ifa_flags &= ~IFA_F_SECONDARY;
  293. printf("secondary ");
  294. }
  295. if (ifa->ifa_flags & IFA_F_TENTATIVE) {
  296. ifa->ifa_flags &= ~IFA_F_TENTATIVE;
  297. printf("tentative ");
  298. }
  299. if (ifa->ifa_flags & IFA_F_DEPRECATED) {
  300. ifa->ifa_flags &= ~IFA_F_DEPRECATED;
  301. printf("deprecated ");
  302. }
  303. if (!(ifa->ifa_flags & IFA_F_PERMANENT)) {
  304. printf("dynamic ");
  305. } else
  306. ifa->ifa_flags &= ~IFA_F_PERMANENT;
  307. if (ifa->ifa_flags)
  308. printf("flags %02x ", ifa->ifa_flags);
  309. if (rta_tb[IFA_LABEL])
  310. fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), stdout);
  311. if (rta_tb[IFA_CACHEINFO]) {
  312. struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
  313. char buf[128];
  314. bb_putchar(_SL_);
  315. if (ci->ifa_valid == 0xFFFFFFFFU)
  316. sprintf(buf, "valid_lft forever");
  317. else
  318. sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
  319. if (ci->ifa_prefered == 0xFFFFFFFFU)
  320. sprintf(buf+strlen(buf), " preferred_lft forever");
  321. else
  322. sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
  323. printf(" %s", buf);
  324. }
  325. bb_putchar('\n');
  326. /*fflush_all();*/
  327. return 0;
  328. }
  329. struct nlmsg_list {
  330. struct nlmsg_list *next;
  331. struct nlmsghdr h;
  332. };
  333. static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
  334. {
  335. for (; ainfo; ainfo = ainfo->next) {
  336. struct nlmsghdr *n = &ainfo->h;
  337. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  338. if (n->nlmsg_type != RTM_NEWADDR)
  339. continue;
  340. if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
  341. return -1;
  342. if (ifa->ifa_index != ifindex
  343. || (G_filter.family && G_filter.family != ifa->ifa_family)
  344. ) {
  345. continue;
  346. }
  347. print_addrinfo(NULL, n, NULL);
  348. }
  349. return 0;
  350. }
  351. static int FAST_FUNC store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
  352. {
  353. struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
  354. struct nlmsg_list *h;
  355. struct nlmsg_list **lp;
  356. h = xzalloc(n->nlmsg_len + sizeof(void*));
  357. memcpy(&h->h, n, n->nlmsg_len);
  358. /*h->next = NULL; - xzalloc did it */
  359. for (lp = linfo; *lp; lp = &(*lp)->next)
  360. continue;
  361. *lp = h;
  362. ll_remember_index(who, n, NULL);
  363. return 0;
  364. }
  365. static void ipaddr_reset_filter(int _oneline)
  366. {
  367. memset(&G_filter, 0, sizeof(G_filter));
  368. G_filter.oneline = _oneline;
  369. }
  370. /* Return value becomes exitcode. It's okay to not return at all */
  371. int FAST_FUNC ipaddr_list_or_flush(char **argv, int flush)
  372. {
  373. static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0";
  374. struct nlmsg_list *linfo = NULL;
  375. struct nlmsg_list *ainfo = NULL;
  376. struct nlmsg_list *l;
  377. struct rtnl_handle rth;
  378. char *filter_dev = NULL;
  379. int no_link = 0;
  380. ipaddr_reset_filter(oneline);
  381. G_filter.showqueue = 1;
  382. if (G_filter.family == AF_UNSPEC)
  383. G_filter.family = preferred_family;
  384. if (flush) {
  385. if (!*argv) {
  386. bb_error_msg_and_die(bb_msg_requires_arg, "flush");
  387. }
  388. if (G_filter.family == AF_PACKET) {
  389. bb_error_msg_and_die("can't flush link addresses");
  390. }
  391. }
  392. while (*argv) {
  393. const smalluint key = index_in_strings(option, *argv);
  394. if (key == 0) { /* to */
  395. NEXT_ARG();
  396. get_prefix(&G_filter.pfx, *argv, G_filter.family);
  397. if (G_filter.family == AF_UNSPEC) {
  398. G_filter.family = G_filter.pfx.family;
  399. }
  400. } else if (key == 1) { /* scope */
  401. uint32_t scope = 0;
  402. NEXT_ARG();
  403. G_filter.scopemask = -1;
  404. if (rtnl_rtscope_a2n(&scope, *argv)) {
  405. if (strcmp(*argv, "all") != 0) {
  406. invarg(*argv, "scope");
  407. }
  408. scope = RT_SCOPE_NOWHERE;
  409. G_filter.scopemask = 0;
  410. }
  411. G_filter.scope = scope;
  412. } else if (key == 2) { /* up */
  413. G_filter.up = 1;
  414. } else if (key == 3) { /* label */
  415. NEXT_ARG();
  416. G_filter.label = *argv;
  417. } else {
  418. if (key == 4) /* dev */
  419. NEXT_ARG();
  420. if (filter_dev)
  421. duparg2("dev", *argv);
  422. filter_dev = *argv;
  423. }
  424. argv++;
  425. }
  426. xrtnl_open(&rth);
  427. xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
  428. xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
  429. if (filter_dev) {
  430. G_filter.ifindex = xll_name_to_index(filter_dev);
  431. }
  432. if (flush) {
  433. char flushb[4096-512];
  434. G_filter.flushb = flushb;
  435. G_filter.flushp = 0;
  436. G_filter.flushe = sizeof(flushb);
  437. G_filter.rth = &rth;
  438. for (;;) {
  439. xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
  440. G_filter.flushed = 0;
  441. xrtnl_dump_filter(&rth, print_addrinfo, NULL);
  442. if (G_filter.flushed == 0) {
  443. return 0;
  444. }
  445. if (flush_update() < 0) {
  446. return 1;
  447. }
  448. }
  449. }
  450. if (G_filter.family != AF_PACKET) {
  451. xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
  452. xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
  453. }
  454. if (G_filter.family && G_filter.family != AF_PACKET) {
  455. struct nlmsg_list **lp;
  456. lp = &linfo;
  457. if (G_filter.oneline)
  458. no_link = 1;
  459. while ((l = *lp) != NULL) {
  460. int ok = 0;
  461. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  462. struct nlmsg_list *a;
  463. for (a = ainfo; a; a = a->next) {
  464. struct nlmsghdr *n = &a->h;
  465. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  466. if (ifa->ifa_index != ifi->ifi_index
  467. || (G_filter.family && G_filter.family != ifa->ifa_family)
  468. ) {
  469. continue;
  470. }
  471. if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
  472. continue;
  473. if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
  474. continue;
  475. if (G_filter.pfx.family || G_filter.label) {
  476. struct rtattr *tb[IFA_MAX+1];
  477. memset(tb, 0, sizeof(tb));
  478. parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
  479. if (!tb[IFA_LOCAL])
  480. tb[IFA_LOCAL] = tb[IFA_ADDRESS];
  481. if (G_filter.pfx.family && tb[IFA_LOCAL]) {
  482. inet_prefix dst;
  483. memset(&dst, 0, sizeof(dst));
  484. dst.family = ifa->ifa_family;
  485. memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
  486. if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
  487. continue;
  488. }
  489. if (G_filter.label) {
  490. SPRINT_BUF(b1);
  491. const char *label;
  492. if (tb[IFA_LABEL])
  493. label = RTA_DATA(tb[IFA_LABEL]);
  494. else
  495. label = ll_idx_n2a(ifa->ifa_index, b1);
  496. if (fnmatch(G_filter.label, label, 0) != 0)
  497. continue;
  498. }
  499. }
  500. ok = 1;
  501. break;
  502. }
  503. if (!ok)
  504. *lp = l->next;
  505. else
  506. lp = &l->next;
  507. }
  508. }
  509. for (l = linfo; l; l = l->next) {
  510. if (no_link || print_linkinfo(&l->h) == 0) {
  511. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  512. if (G_filter.family != AF_PACKET)
  513. print_selected_addrinfo(ifi->ifi_index, ainfo);
  514. }
  515. }
  516. return 0;
  517. }
  518. static int default_scope(inet_prefix *lcl)
  519. {
  520. if (lcl->family == AF_INET) {
  521. if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
  522. return RT_SCOPE_HOST;
  523. }
  524. return 0;
  525. }
  526. /* Return value becomes exitcode. It's okay to not return at all */
  527. static int ipaddr_modify(int cmd, char **argv)
  528. {
  529. static const char option[] ALIGN1 =
  530. "peer\0""remote\0""broadcast\0""brd\0"
  531. "anycast\0""scope\0""dev\0""label\0""local\0";
  532. struct rtnl_handle rth;
  533. struct {
  534. struct nlmsghdr n;
  535. struct ifaddrmsg ifa;
  536. char buf[256];
  537. } req;
  538. char *d = NULL;
  539. char *l = NULL;
  540. inet_prefix lcl;
  541. inet_prefix peer;
  542. int local_len = 0;
  543. int peer_len = 0;
  544. int brd_len = 0;
  545. int any_len = 0;
  546. bool scoped = 0;
  547. memset(&req, 0, sizeof(req));
  548. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
  549. req.n.nlmsg_flags = NLM_F_REQUEST;
  550. req.n.nlmsg_type = cmd;
  551. req.ifa.ifa_family = preferred_family;
  552. while (*argv) {
  553. const smalluint arg = index_in_strings(option, *argv);
  554. if (arg <= 1) { /* peer, remote */
  555. NEXT_ARG();
  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. NEXT_ARG();
  569. if (brd_len) {
  570. duparg("broadcast", *argv);
  571. }
  572. if (LONE_CHAR(*argv, '+')) {
  573. brd_len = -1;
  574. } else if (LONE_DASH(*argv)) {
  575. brd_len = -2;
  576. } else {
  577. get_addr(&addr, *argv, req.ifa.ifa_family);
  578. if (req.ifa.ifa_family == AF_UNSPEC)
  579. req.ifa.ifa_family = addr.family;
  580. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
  581. brd_len = addr.bytelen;
  582. }
  583. } else if (arg == 4) { /* anycast */
  584. inet_prefix addr;
  585. NEXT_ARG();
  586. if (any_len) {
  587. duparg("anycast", *argv);
  588. }
  589. get_addr(&addr, *argv, req.ifa.ifa_family);
  590. if (req.ifa.ifa_family == AF_UNSPEC) {
  591. req.ifa.ifa_family = addr.family;
  592. }
  593. addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
  594. any_len = addr.bytelen;
  595. } else if (arg == 5) { /* scope */
  596. uint32_t scope = 0;
  597. NEXT_ARG();
  598. if (rtnl_rtscope_a2n(&scope, *argv)) {
  599. invarg(*argv, "scope");
  600. }
  601. req.ifa.ifa_scope = scope;
  602. scoped = 1;
  603. } else if (arg == 6) { /* dev */
  604. NEXT_ARG();
  605. d = *argv;
  606. } else if (arg == 7) { /* label */
  607. NEXT_ARG();
  608. l = *argv;
  609. addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l) + 1);
  610. } else {
  611. if (arg == 8) /* local */
  612. NEXT_ARG();
  613. if (local_len) {
  614. duparg2("local", *argv);
  615. }
  616. get_prefix(&lcl, *argv, req.ifa.ifa_family);
  617. if (req.ifa.ifa_family == AF_UNSPEC) {
  618. req.ifa.ifa_family = lcl.family;
  619. }
  620. addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
  621. local_len = lcl.bytelen;
  622. }
  623. argv++;
  624. }
  625. if (!d) {
  626. /* There was no "dev IFACE", but we need that */
  627. bb_error_msg_and_die("need \"dev IFACE\"");
  628. }
  629. if (l && strncmp(d, l, strlen(d)) != 0) {
  630. bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
  631. }
  632. if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
  633. peer = lcl;
  634. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
  635. }
  636. if (req.ifa.ifa_prefixlen == 0)
  637. req.ifa.ifa_prefixlen = lcl.bitlen;
  638. if (brd_len < 0 && cmd != RTM_DELADDR) {
  639. inet_prefix brd;
  640. int i;
  641. if (req.ifa.ifa_family != AF_INET) {
  642. bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
  643. }
  644. brd = peer;
  645. if (brd.bitlen <= 30) {
  646. for (i=31; i>=brd.bitlen; i--) {
  647. if (brd_len == -1)
  648. brd.data[0] |= htonl(1<<(31-i));
  649. else
  650. brd.data[0] &= ~htonl(1<<(31-i));
  651. }
  652. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
  653. brd_len = brd.bytelen;
  654. }
  655. }
  656. if (!scoped && cmd != RTM_DELADDR)
  657. req.ifa.ifa_scope = default_scope(&lcl);
  658. xrtnl_open(&rth);
  659. ll_init_map(&rth);
  660. req.ifa.ifa_index = xll_name_to_index(d);
  661. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  662. return 2;
  663. return 0;
  664. }
  665. /* Return value becomes exitcode. It's okay to not return at all */
  666. int FAST_FUNC do_ipaddr(char **argv)
  667. {
  668. static const char commands[] ALIGN1 =
  669. "add\0""delete\0""list\0""show\0""lst\0""flush\0";
  670. smalluint cmd = 2;
  671. if (*argv) {
  672. cmd = index_in_substrings(commands, *argv);
  673. if (cmd > 5)
  674. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  675. argv++;
  676. if (cmd <= 1)
  677. return ipaddr_modify((cmd == 0) ? RTM_NEWADDR : RTM_DELADDR, argv);
  678. }
  679. /* 2 == list, 3 == show, 4 == lst */
  680. return ipaddr_list_or_flush(argv, cmd == 5);
  681. }