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. 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. }
  285. if (rta_tb[IFA_ANYCAST]) {
  286. printf("any %s ",
  287. rt_addr_n2a(ifa->ifa_family,
  288. RTA_DATA(rta_tb[IFA_ANYCAST]),
  289. abuf, sizeof(abuf))
  290. );
  291. }
  292. printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1));
  293. if (ifa->ifa_flags & IFA_F_SECONDARY) {
  294. ifa->ifa_flags &= ~IFA_F_SECONDARY;
  295. printf("secondary ");
  296. }
  297. if (ifa->ifa_flags & IFA_F_TENTATIVE) {
  298. ifa->ifa_flags &= ~IFA_F_TENTATIVE;
  299. printf("tentative ");
  300. }
  301. if (ifa->ifa_flags & IFA_F_DEPRECATED) {
  302. ifa->ifa_flags &= ~IFA_F_DEPRECATED;
  303. printf("deprecated ");
  304. }
  305. if (!(ifa->ifa_flags & IFA_F_PERMANENT)) {
  306. printf("dynamic ");
  307. } else
  308. ifa->ifa_flags &= ~IFA_F_PERMANENT;
  309. if (ifa->ifa_flags)
  310. printf("flags %02x ", ifa->ifa_flags);
  311. if (rta_tb[IFA_LABEL])
  312. fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), stdout);
  313. if (rta_tb[IFA_CACHEINFO]) {
  314. struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
  315. char buf[128];
  316. bb_putchar(_SL_);
  317. if (ci->ifa_valid == 0xFFFFFFFFU)
  318. sprintf(buf, "valid_lft forever");
  319. else
  320. sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
  321. if (ci->ifa_prefered == 0xFFFFFFFFU)
  322. sprintf(buf+strlen(buf), " preferred_lft forever");
  323. else
  324. sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
  325. printf(" %s", buf);
  326. }
  327. bb_putchar('\n');
  328. /*fflush_all();*/
  329. return 0;
  330. }
  331. struct nlmsg_list {
  332. struct nlmsg_list *next;
  333. struct nlmsghdr h;
  334. };
  335. static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
  336. {
  337. for (; ainfo; ainfo = ainfo->next) {
  338. struct nlmsghdr *n = &ainfo->h;
  339. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  340. if (n->nlmsg_type != RTM_NEWADDR)
  341. continue;
  342. if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
  343. return -1;
  344. if (ifa->ifa_index != ifindex
  345. || (G_filter.family && G_filter.family != ifa->ifa_family)
  346. ) {
  347. continue;
  348. }
  349. print_addrinfo(NULL, n, NULL);
  350. }
  351. return 0;
  352. }
  353. static int FAST_FUNC store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
  354. {
  355. struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
  356. struct nlmsg_list *h;
  357. struct nlmsg_list **lp;
  358. h = xzalloc(n->nlmsg_len + sizeof(void*));
  359. memcpy(&h->h, n, n->nlmsg_len);
  360. /*h->next = NULL; - xzalloc did it */
  361. for (lp = linfo; *lp; lp = &(*lp)->next)
  362. continue;
  363. *lp = h;
  364. ll_remember_index(who, n, NULL);
  365. return 0;
  366. }
  367. static void ipaddr_reset_filter(int _oneline)
  368. {
  369. memset(&G_filter, 0, sizeof(G_filter));
  370. G_filter.oneline = _oneline;
  371. }
  372. /* Return value becomes exitcode. It's okay to not return at all */
  373. int FAST_FUNC ipaddr_list_or_flush(char **argv, int flush)
  374. {
  375. static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0";
  376. struct nlmsg_list *linfo = NULL;
  377. struct nlmsg_list *ainfo = NULL;
  378. struct nlmsg_list *l;
  379. struct rtnl_handle rth;
  380. char *filter_dev = NULL;
  381. int no_link = 0;
  382. ipaddr_reset_filter(oneline);
  383. G_filter.showqueue = 1;
  384. if (G_filter.family == AF_UNSPEC)
  385. G_filter.family = preferred_family;
  386. if (flush) {
  387. if (!*argv) {
  388. bb_error_msg_and_die(bb_msg_requires_arg, "flush");
  389. }
  390. if (G_filter.family == AF_PACKET) {
  391. bb_error_msg_and_die("can't flush link addresses");
  392. }
  393. }
  394. while (*argv) {
  395. const smalluint key = index_in_strings(option, *argv);
  396. if (key == 0) { /* to */
  397. NEXT_ARG();
  398. get_prefix(&G_filter.pfx, *argv, G_filter.family);
  399. if (G_filter.family == AF_UNSPEC) {
  400. G_filter.family = G_filter.pfx.family;
  401. }
  402. } else if (key == 1) { /* scope */
  403. uint32_t scope = 0;
  404. NEXT_ARG();
  405. G_filter.scopemask = -1;
  406. if (rtnl_rtscope_a2n(&scope, *argv)) {
  407. if (strcmp(*argv, "all") != 0) {
  408. invarg(*argv, "scope");
  409. }
  410. scope = RT_SCOPE_NOWHERE;
  411. G_filter.scopemask = 0;
  412. }
  413. G_filter.scope = scope;
  414. } else if (key == 2) { /* up */
  415. G_filter.up = 1;
  416. } else if (key == 3) { /* label */
  417. NEXT_ARG();
  418. G_filter.label = *argv;
  419. } else {
  420. if (key == 4) /* dev */
  421. NEXT_ARG();
  422. if (filter_dev)
  423. duparg2("dev", *argv);
  424. filter_dev = *argv;
  425. }
  426. argv++;
  427. }
  428. xrtnl_open(&rth);
  429. xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
  430. xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
  431. if (filter_dev) {
  432. G_filter.ifindex = xll_name_to_index(filter_dev);
  433. }
  434. if (flush) {
  435. char flushb[4096-512];
  436. G_filter.flushb = flushb;
  437. G_filter.flushp = 0;
  438. G_filter.flushe = sizeof(flushb);
  439. G_filter.rth = &rth;
  440. for (;;) {
  441. xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
  442. G_filter.flushed = 0;
  443. xrtnl_dump_filter(&rth, print_addrinfo, NULL);
  444. if (G_filter.flushed == 0) {
  445. return 0;
  446. }
  447. if (flush_update() < 0) {
  448. return 1;
  449. }
  450. }
  451. }
  452. if (G_filter.family != AF_PACKET) {
  453. xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
  454. xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
  455. }
  456. if (G_filter.family && G_filter.family != AF_PACKET) {
  457. struct nlmsg_list **lp;
  458. lp = &linfo;
  459. if (G_filter.oneline)
  460. no_link = 1;
  461. while ((l = *lp) != NULL) {
  462. int ok = 0;
  463. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  464. struct nlmsg_list *a;
  465. for (a = ainfo; a; a = a->next) {
  466. struct nlmsghdr *n = &a->h;
  467. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  468. if (ifa->ifa_index != ifi->ifi_index
  469. || (G_filter.family && G_filter.family != ifa->ifa_family)
  470. ) {
  471. continue;
  472. }
  473. if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
  474. continue;
  475. if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
  476. continue;
  477. if (G_filter.pfx.family || G_filter.label) {
  478. struct rtattr *tb[IFA_MAX+1];
  479. memset(tb, 0, sizeof(tb));
  480. parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
  481. if (!tb[IFA_LOCAL])
  482. tb[IFA_LOCAL] = tb[IFA_ADDRESS];
  483. if (G_filter.pfx.family && tb[IFA_LOCAL]) {
  484. inet_prefix dst;
  485. memset(&dst, 0, sizeof(dst));
  486. dst.family = ifa->ifa_family;
  487. memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
  488. if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
  489. continue;
  490. }
  491. if (G_filter.label) {
  492. SPRINT_BUF(b1);
  493. const char *label;
  494. if (tb[IFA_LABEL])
  495. label = RTA_DATA(tb[IFA_LABEL]);
  496. else
  497. label = ll_idx_n2a(ifa->ifa_index, b1);
  498. if (fnmatch(G_filter.label, label, 0) != 0)
  499. continue;
  500. }
  501. }
  502. ok = 1;
  503. break;
  504. }
  505. if (!ok)
  506. *lp = l->next;
  507. else
  508. lp = &l->next;
  509. }
  510. }
  511. for (l = linfo; l; l = l->next) {
  512. if (no_link || print_linkinfo(&l->h) == 0) {
  513. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  514. if (G_filter.family != AF_PACKET)
  515. print_selected_addrinfo(ifi->ifi_index, ainfo);
  516. }
  517. }
  518. return 0;
  519. }
  520. static int default_scope(inet_prefix *lcl)
  521. {
  522. if (lcl->family == AF_INET) {
  523. if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
  524. return RT_SCOPE_HOST;
  525. }
  526. return 0;
  527. }
  528. /* Return value becomes exitcode. It's okay to not return at all */
  529. static int ipaddr_modify(int cmd, char **argv)
  530. {
  531. static const char option[] ALIGN1 =
  532. "peer\0""remote\0""broadcast\0""brd\0"
  533. "anycast\0""scope\0""dev\0""label\0""local\0";
  534. struct rtnl_handle rth;
  535. struct {
  536. struct nlmsghdr n;
  537. struct ifaddrmsg ifa;
  538. char buf[256];
  539. } req;
  540. char *d = NULL;
  541. char *l = NULL;
  542. inet_prefix lcl;
  543. inet_prefix peer;
  544. int local_len = 0;
  545. int peer_len = 0;
  546. int brd_len = 0;
  547. int any_len = 0;
  548. bool scoped = 0;
  549. memset(&req, 0, sizeof(req));
  550. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
  551. req.n.nlmsg_flags = NLM_F_REQUEST;
  552. req.n.nlmsg_type = cmd;
  553. req.ifa.ifa_family = preferred_family;
  554. while (*argv) {
  555. const smalluint arg = index_in_strings(option, *argv);
  556. if (arg <= 1) { /* peer, remote */
  557. NEXT_ARG();
  558. if (peer_len) {
  559. duparg("peer", *argv);
  560. }
  561. get_prefix(&peer, *argv, req.ifa.ifa_family);
  562. peer_len = peer.bytelen;
  563. if (req.ifa.ifa_family == AF_UNSPEC) {
  564. req.ifa.ifa_family = peer.family;
  565. }
  566. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
  567. req.ifa.ifa_prefixlen = peer.bitlen;
  568. } else if (arg <= 3) { /* broadcast, brd */
  569. inet_prefix addr;
  570. NEXT_ARG();
  571. if (brd_len) {
  572. duparg("broadcast", *argv);
  573. }
  574. if (LONE_CHAR(*argv, '+')) {
  575. brd_len = -1;
  576. } else if (LONE_DASH(*argv)) {
  577. brd_len = -2;
  578. } else {
  579. get_addr(&addr, *argv, req.ifa.ifa_family);
  580. if (req.ifa.ifa_family == AF_UNSPEC)
  581. req.ifa.ifa_family = addr.family;
  582. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
  583. brd_len = addr.bytelen;
  584. }
  585. } else if (arg == 4) { /* anycast */
  586. inet_prefix addr;
  587. NEXT_ARG();
  588. if (any_len) {
  589. duparg("anycast", *argv);
  590. }
  591. get_addr(&addr, *argv, req.ifa.ifa_family);
  592. if (req.ifa.ifa_family == AF_UNSPEC) {
  593. req.ifa.ifa_family = addr.family;
  594. }
  595. addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
  596. any_len = addr.bytelen;
  597. } else if (arg == 5) { /* scope */
  598. uint32_t scope = 0;
  599. NEXT_ARG();
  600. if (rtnl_rtscope_a2n(&scope, *argv)) {
  601. invarg(*argv, "scope");
  602. }
  603. req.ifa.ifa_scope = scope;
  604. scoped = 1;
  605. } else if (arg == 6) { /* dev */
  606. NEXT_ARG();
  607. d = *argv;
  608. } else if (arg == 7) { /* label */
  609. NEXT_ARG();
  610. l = *argv;
  611. addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l) + 1);
  612. } else {
  613. if (arg == 8) /* local */
  614. NEXT_ARG();
  615. if (local_len) {
  616. duparg2("local", *argv);
  617. }
  618. get_prefix(&lcl, *argv, req.ifa.ifa_family);
  619. if (req.ifa.ifa_family == AF_UNSPEC) {
  620. req.ifa.ifa_family = lcl.family;
  621. }
  622. addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
  623. local_len = lcl.bytelen;
  624. }
  625. argv++;
  626. }
  627. if (!d) {
  628. /* There was no "dev IFACE", but we need that */
  629. bb_error_msg_and_die("need \"dev IFACE\"");
  630. }
  631. if (l && strncmp(d, l, strlen(d)) != 0) {
  632. bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
  633. }
  634. if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
  635. peer = lcl;
  636. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
  637. }
  638. if (req.ifa.ifa_prefixlen == 0)
  639. req.ifa.ifa_prefixlen = lcl.bitlen;
  640. if (brd_len < 0 && cmd != RTM_DELADDR) {
  641. inet_prefix brd;
  642. int i;
  643. if (req.ifa.ifa_family != AF_INET) {
  644. bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
  645. }
  646. brd = peer;
  647. if (brd.bitlen <= 30) {
  648. for (i=31; i>=brd.bitlen; i--) {
  649. if (brd_len == -1)
  650. brd.data[0] |= htonl(1<<(31-i));
  651. else
  652. brd.data[0] &= ~htonl(1<<(31-i));
  653. }
  654. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
  655. brd_len = brd.bytelen;
  656. }
  657. }
  658. if (!scoped && cmd != RTM_DELADDR)
  659. req.ifa.ifa_scope = default_scope(&lcl);
  660. xrtnl_open(&rth);
  661. ll_init_map(&rth);
  662. req.ifa.ifa_index = xll_name_to_index(d);
  663. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  664. return 2;
  665. return 0;
  666. }
  667. /* Return value becomes exitcode. It's okay to not return at all */
  668. int FAST_FUNC do_ipaddr(char **argv)
  669. {
  670. static const char commands[] ALIGN1 =
  671. "add\0""delete\0""list\0""show\0""lst\0""flush\0";
  672. smalluint cmd = 2;
  673. if (*argv) {
  674. cmd = index_in_substrings(commands, *argv);
  675. if (cmd > 5)
  676. bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
  677. argv++;
  678. if (cmd <= 1)
  679. return ipaddr_modify((cmd == 0) ? RTM_NEWADDR : RTM_DELADDR, argv);
  680. }
  681. /* 2 == list, 3 == show, 4 == lst */
  682. return ipaddr_list_or_flush(argv, cmd == 5);
  683. }