3
0

ipaddress.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 "common_bufsiz.h"
  15. #include "rt_names.h"
  16. #include "utils.h"
  17. #ifndef IFF_LOWER_UP
  18. /* from linux/if.h */
  19. #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
  20. #endif
  21. struct filter_t {
  22. char *label;
  23. char *flushb;
  24. struct rtnl_handle *rth;
  25. int scope, scopemask;
  26. int flags, flagmask;
  27. int flushp;
  28. int flushe;
  29. int ifindex;
  30. family_t family;
  31. smallint showqueue;
  32. smallint oneline;
  33. smallint up;
  34. smallint flushed;
  35. inet_prefix pfx;
  36. } FIX_ALIASING;
  37. typedef struct filter_t filter_t;
  38. #define G_filter (*(filter_t*)bb_common_bufsiz1)
  39. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  40. static void print_link_flags(unsigned flags, unsigned mdown)
  41. {
  42. static const int flag_masks[] = {
  43. IFF_LOOPBACK, IFF_BROADCAST, IFF_POINTOPOINT,
  44. IFF_MULTICAST, IFF_NOARP, IFF_UP, IFF_LOWER_UP };
  45. static const char flag_labels[] ALIGN1 =
  46. "LOOPBACK\0""BROADCAST\0""POINTOPOINT\0"
  47. "MULTICAST\0""NOARP\0""UP\0""LOWER_UP\0";
  48. bb_putchar('<');
  49. if (flags & IFF_UP && !(flags & IFF_RUNNING))
  50. printf("NO-CARRIER,");
  51. flags &= ~IFF_RUNNING;
  52. #if 0
  53. _PF(ALLMULTI);
  54. _PF(PROMISC);
  55. _PF(MASTER);
  56. _PF(SLAVE);
  57. _PF(DEBUG);
  58. _PF(DYNAMIC);
  59. _PF(AUTOMEDIA);
  60. _PF(PORTSEL);
  61. _PF(NOTRAILERS);
  62. #endif
  63. flags = print_flags_separated(flag_masks, flag_labels, flags, ",");
  64. if (flags)
  65. printf("%x", flags);
  66. if (mdown)
  67. printf(",M-DOWN");
  68. printf("> ");
  69. }
  70. static void print_queuelen(char *name)
  71. {
  72. struct ifreq ifr;
  73. int s;
  74. s = socket(AF_INET, SOCK_STREAM, 0);
  75. if (s < 0)
  76. return;
  77. memset(&ifr, 0, sizeof(ifr));
  78. strncpy_IFNAMSIZ(ifr.ifr_name, name);
  79. if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) {
  80. close(s);
  81. return;
  82. }
  83. close(s);
  84. if (ifr.ifr_qlen)
  85. printf("qlen %d", ifr.ifr_qlen);
  86. }
  87. static NOINLINE int print_linkinfo(const struct nlmsghdr *n)
  88. {
  89. struct ifinfomsg *ifi = NLMSG_DATA(n);
  90. struct rtattr *tb[IFLA_MAX+1];
  91. int len = n->nlmsg_len;
  92. if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
  93. return 0;
  94. len -= NLMSG_LENGTH(sizeof(*ifi));
  95. if (len < 0)
  96. return -1;
  97. if (G_filter.ifindex && ifi->ifi_index != G_filter.ifindex)
  98. return 0;
  99. if (G_filter.up && !(ifi->ifi_flags & IFF_UP))
  100. return 0;
  101. memset(tb, 0, sizeof(tb));
  102. parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
  103. if (tb[IFLA_IFNAME] == NULL) {
  104. bb_error_msg("nil ifname");
  105. return -1;
  106. }
  107. if (G_filter.label
  108. && (!G_filter.family || G_filter.family == AF_PACKET)
  109. && fnmatch(G_filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)
  110. ) {
  111. return 0;
  112. }
  113. if (n->nlmsg_type == RTM_DELLINK)
  114. printf("Deleted ");
  115. printf("%d: %s", ifi->ifi_index,
  116. /*tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>" - we checked tb[IFLA_IFNAME] above*/
  117. (char*)RTA_DATA(tb[IFLA_IFNAME])
  118. );
  119. {
  120. unsigned m_flag = 0;
  121. if (tb[IFLA_LINK]) {
  122. int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
  123. if (iflink == 0)
  124. printf("@NONE: ");
  125. else {
  126. printf("@%s: ", ll_index_to_name(iflink));
  127. m_flag = ll_index_to_flags(iflink);
  128. m_flag = !(m_flag & IFF_UP);
  129. }
  130. } else {
  131. printf(": ");
  132. }
  133. print_link_flags(ifi->ifi_flags, m_flag);
  134. }
  135. if (tb[IFLA_MTU])
  136. printf("mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
  137. if (tb[IFLA_QDISC])
  138. printf("qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
  139. #ifdef IFLA_MASTER
  140. if (tb[IFLA_MASTER]) {
  141. printf("master %s ", ll_index_to_name(*(int*)RTA_DATA(tb[IFLA_MASTER])));
  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. if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
  196. return 0;
  197. len -= NLMSG_LENGTH(sizeof(*ifa));
  198. if (len < 0) {
  199. bb_error_msg("wrong nlmsg len %d", len);
  200. return -1;
  201. }
  202. if (G_filter.flushb && n->nlmsg_type != RTM_NEWADDR)
  203. return 0;
  204. memset(rta_tb, 0, sizeof(rta_tb));
  205. parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
  206. if (!rta_tb[IFA_LOCAL])
  207. rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
  208. if (!rta_tb[IFA_ADDRESS])
  209. rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
  210. if (G_filter.ifindex && G_filter.ifindex != ifa->ifa_index)
  211. return 0;
  212. if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
  213. return 0;
  214. if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
  215. return 0;
  216. if (G_filter.label) {
  217. const char *label;
  218. if (rta_tb[IFA_LABEL])
  219. label = RTA_DATA(rta_tb[IFA_LABEL]);
  220. else
  221. label = ll_index_to_name(ifa->ifa_index);
  222. if (fnmatch(G_filter.label, label, 0) != 0)
  223. return 0;
  224. }
  225. if (G_filter.pfx.family) {
  226. if (rta_tb[IFA_LOCAL]) {
  227. inet_prefix dst;
  228. memset(&dst, 0, sizeof(dst));
  229. dst.family = ifa->ifa_family;
  230. memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
  231. if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
  232. return 0;
  233. }
  234. }
  235. if (G_filter.flushb) {
  236. struct nlmsghdr *fn;
  237. if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
  238. if (flush_update())
  239. return -1;
  240. }
  241. fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
  242. memcpy(fn, n, n->nlmsg_len);
  243. fn->nlmsg_type = RTM_DELADDR;
  244. fn->nlmsg_flags = NLM_F_REQUEST;
  245. fn->nlmsg_seq = ++G_filter.rth->seq;
  246. G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
  247. G_filter.flushed = 1;
  248. return 0;
  249. }
  250. if (n->nlmsg_type == RTM_DELADDR)
  251. printf("Deleted ");
  252. if (G_filter.oneline)
  253. printf("%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
  254. if (ifa->ifa_family == AF_INET)
  255. printf(" inet ");
  256. else if (ifa->ifa_family == AF_INET6)
  257. printf(" inet6 ");
  258. else
  259. printf(" family %d ", ifa->ifa_family);
  260. if (rta_tb[IFA_LOCAL]) {
  261. fputs(rt_addr_n2a(ifa->ifa_family, RTA_DATA(rta_tb[IFA_LOCAL])),
  262. stdout
  263. );
  264. if (rta_tb[IFA_ADDRESS] == NULL
  265. || memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0
  266. ) {
  267. printf("/%d ", ifa->ifa_prefixlen);
  268. } else {
  269. printf(" peer %s/%d ",
  270. rt_addr_n2a(ifa->ifa_family, RTA_DATA(rta_tb[IFA_ADDRESS])),
  271. ifa->ifa_prefixlen
  272. );
  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. );
  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. );
  286. }
  287. printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope));
  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_1_to_2(*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. const char *label;
  488. if (tb[IFA_LABEL])
  489. label = RTA_DATA(tb[IFA_LABEL]);
  490. else
  491. label = ll_index_to_name(ifa->ifa_index);
  492. if (fnmatch(G_filter.label, label, 0) != 0)
  493. continue;
  494. }
  495. }
  496. ok = 1;
  497. break;
  498. }
  499. if (!ok)
  500. *lp = l->next;
  501. else
  502. lp = &l->next;
  503. }
  504. }
  505. for (l = linfo; l; l = l->next) {
  506. if (no_link || print_linkinfo(&l->h) == 0) {
  507. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  508. if (G_filter.family != AF_PACKET)
  509. print_selected_addrinfo(ifi->ifi_index, ainfo);
  510. }
  511. }
  512. return 0;
  513. }
  514. static int default_scope(inet_prefix *lcl)
  515. {
  516. if (lcl->family == AF_INET) {
  517. if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
  518. return RT_SCOPE_HOST;
  519. }
  520. return 0;
  521. }
  522. /* Return value becomes exitcode. It's okay to not return at all */
  523. static int ipaddr_modify(int cmd, int flags, char **argv)
  524. {
  525. /* If you add stuff here, update ipaddr_full_usage */
  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. #define option_peer option
  530. #define option_broadcast (option + sizeof("peer") + sizeof("remote"))
  531. #define option_anycast (option_broadcast + sizeof("broadcast") + sizeof("brd"))
  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 | flags;
  550. req.n.nlmsg_type = cmd;
  551. req.ifa.ifa_family = preferred_family;
  552. while (*argv) {
  553. unsigned arg = index_in_strings(option, *argv);
  554. /* if search fails, "local" is assumed */
  555. if ((int)arg >= 0)
  556. NEXT_ARG();
  557. if (arg <= 1) { /* peer, remote */
  558. if (peer_len) {
  559. duparg(option_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. if (brd_len) {
  571. duparg(option_broadcast, *argv);
  572. }
  573. if (LONE_CHAR(*argv, '+')) {
  574. brd_len = -1;
  575. } else if (LONE_DASH(*argv)) {
  576. brd_len = -2;
  577. } else {
  578. get_addr(&addr, *argv, req.ifa.ifa_family);
  579. if (req.ifa.ifa_family == AF_UNSPEC)
  580. req.ifa.ifa_family = addr.family;
  581. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
  582. brd_len = addr.bytelen;
  583. }
  584. } else if (arg == 4) { /* anycast */
  585. inet_prefix addr;
  586. if (any_len) {
  587. duparg(option_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. if (rtnl_rtscope_a2n(&scope, *argv)) {
  598. invarg_1_to_2(*argv, "scope");
  599. }
  600. req.ifa.ifa_scope = scope;
  601. scoped = 1;
  602. } else if (arg == 6) { /* dev */
  603. d = *argv;
  604. } else if (arg == 7) { /* label */
  605. l = *argv;
  606. addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l) + 1);
  607. } else {
  608. /* local (specified or assumed) */
  609. if (local_len) {
  610. duparg2("local", *argv);
  611. }
  612. get_prefix(&lcl, *argv, req.ifa.ifa_family);
  613. if (req.ifa.ifa_family == AF_UNSPEC) {
  614. req.ifa.ifa_family = lcl.family;
  615. }
  616. addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
  617. local_len = lcl.bytelen;
  618. }
  619. argv++;
  620. }
  621. if (!d) {
  622. /* There was no "dev IFACE", but we need that */
  623. bb_error_msg_and_die("need \"dev IFACE\"");
  624. }
  625. if (l && !is_prefixed_with(l, d)) {
  626. bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
  627. }
  628. if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
  629. peer = lcl;
  630. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
  631. }
  632. if (req.ifa.ifa_prefixlen == 0)
  633. req.ifa.ifa_prefixlen = lcl.bitlen;
  634. if (brd_len < 0 && cmd != RTM_DELADDR) {
  635. inet_prefix brd;
  636. int i;
  637. if (req.ifa.ifa_family != AF_INET) {
  638. bb_error_msg_and_die("broadcast can be set only for IPv4 addresses");
  639. }
  640. brd = peer;
  641. if (brd.bitlen <= 30) {
  642. for (i = 31; i >= brd.bitlen; i--) {
  643. if (brd_len == -1)
  644. brd.data[0] |= htonl(1<<(31-i));
  645. else
  646. brd.data[0] &= ~htonl(1<<(31-i));
  647. }
  648. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
  649. brd_len = brd.bytelen;
  650. }
  651. }
  652. if (!scoped && cmd != RTM_DELADDR)
  653. req.ifa.ifa_scope = default_scope(&lcl);
  654. xrtnl_open(&rth);
  655. ll_init_map(&rth);
  656. req.ifa.ifa_index = xll_name_to_index(d);
  657. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  658. return 2;
  659. return 0;
  660. }
  661. /* Return value becomes exitcode. It's okay to not return at all */
  662. int FAST_FUNC do_ipaddr(char **argv)
  663. {
  664. static const char commands[] ALIGN1 =
  665. /* 0 1 2 3 4 5 6 7 8 */
  666. "add\0""change\0""chg\0""replace\0""delete\0""list\0""show\0""lst\0""flush\0";
  667. int cmd = 2;
  668. INIT_G();
  669. if (*argv) {
  670. cmd = index_in_substrings(commands, *argv);
  671. if (cmd < 0)
  672. invarg_1_to_2(*argv, applet_name);
  673. argv++;
  674. if (cmd <= 4) {
  675. return ipaddr_modify(
  676. /*cmd:*/ cmd == 4 ? RTM_DELADDR : RTM_NEWADDR,
  677. /*flags:*/
  678. cmd == 0 ? NLM_F_CREATE|NLM_F_EXCL : /* add */
  679. cmd == 1 || cmd == 2 ? NLM_F_REPLACE : /* change */
  680. cmd == 3 ? NLM_F_CREATE|NLM_F_REPLACE : /* replace */
  681. 0 /* delete */
  682. , argv);
  683. }
  684. }
  685. return ipaddr_list_or_flush(argv, cmd == 8);
  686. }