ipaddress.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. /* Flush cmd buf. If !NULL, print_addrinfo() constructs flush commands in it */
  24. char *flushb;
  25. struct rtnl_handle *rth;
  26. int scope, scopemask;
  27. int flags, flagmask;
  28. int flushp;
  29. int flushe;
  30. int ifindex;
  31. family_t family;
  32. smallint showqueue;
  33. smallint oneline;
  34. smallint up;
  35. /* Misnomer. Does not mean "flushed something" */
  36. /* More like "flush commands were constructed by print_addrinfo()" */
  37. smallint flushed;
  38. inet_prefix pfx;
  39. } FIX_ALIASING;
  40. typedef struct filter_t filter_t;
  41. #define G_filter (*(filter_t*)bb_common_bufsiz1)
  42. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  43. static void print_link_flags(unsigned flags, unsigned mdown)
  44. {
  45. static const int flag_masks[] = {
  46. IFF_LOOPBACK, IFF_BROADCAST, IFF_POINTOPOINT,
  47. IFF_MULTICAST, IFF_NOARP, IFF_UP, IFF_LOWER_UP };
  48. static const char flag_labels[] ALIGN1 =
  49. "LOOPBACK\0""BROADCAST\0""POINTOPOINT\0"
  50. "MULTICAST\0""NOARP\0""UP\0""LOWER_UP\0";
  51. bb_putchar('<');
  52. if (flags & IFF_UP && !(flags & IFF_RUNNING))
  53. printf("NO-CARRIER,");
  54. flags &= ~IFF_RUNNING;
  55. #if 0
  56. _PF(ALLMULTI);
  57. _PF(PROMISC);
  58. _PF(MASTER);
  59. _PF(SLAVE);
  60. _PF(DEBUG);
  61. _PF(DYNAMIC);
  62. _PF(AUTOMEDIA);
  63. _PF(PORTSEL);
  64. _PF(NOTRAILERS);
  65. #endif
  66. flags = print_flags_separated(flag_masks, flag_labels, flags, ",");
  67. if (flags)
  68. printf("%x", flags);
  69. if (mdown)
  70. printf(",M-DOWN");
  71. printf("> ");
  72. }
  73. static void print_queuelen(char *name)
  74. {
  75. struct ifreq ifr;
  76. int s;
  77. s = socket(AF_INET, SOCK_STREAM, 0);
  78. if (s < 0)
  79. return;
  80. memset(&ifr, 0, sizeof(ifr));
  81. strncpy_IFNAMSIZ(ifr.ifr_name, name);
  82. if (ioctl_or_warn(s, SIOCGIFTXQLEN, &ifr) < 0) {
  83. close(s);
  84. return;
  85. }
  86. close(s);
  87. if (ifr.ifr_qlen)
  88. printf("qlen %d", ifr.ifr_qlen);
  89. }
  90. static NOINLINE int print_linkinfo(const struct nlmsghdr *n)
  91. {
  92. struct ifinfomsg *ifi = NLMSG_DATA(n);
  93. struct rtattr *tb[IFLA_MAX+1];
  94. int len = n->nlmsg_len;
  95. if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
  96. return 0;
  97. len -= NLMSG_LENGTH(sizeof(*ifi));
  98. if (len < 0)
  99. return -1;
  100. if (G_filter.ifindex && ifi->ifi_index != G_filter.ifindex)
  101. return 0;
  102. if (G_filter.up && !(ifi->ifi_flags & IFF_UP))
  103. return 0;
  104. //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
  105. parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
  106. if (tb[IFLA_IFNAME] == NULL) {
  107. bb_simple_error_msg("nil ifname");
  108. return -1;
  109. }
  110. if (G_filter.label
  111. && (!G_filter.family || G_filter.family == AF_PACKET)
  112. && fnmatch(G_filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)
  113. ) {
  114. return 0;
  115. }
  116. if (n->nlmsg_type == RTM_DELLINK)
  117. printf("Deleted ");
  118. printf("%d: %s", ifi->ifi_index,
  119. /*tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>" - we checked tb[IFLA_IFNAME] above*/
  120. (char*)RTA_DATA(tb[IFLA_IFNAME])
  121. );
  122. {
  123. unsigned m_flag = 0;
  124. if (tb[IFLA_LINK]) {
  125. int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
  126. if (iflink == 0)
  127. printf("@NONE: ");
  128. else {
  129. printf("@%s: ", ll_index_to_name(iflink));
  130. m_flag = ll_index_to_flags(iflink);
  131. m_flag = !(m_flag & IFF_UP);
  132. }
  133. } else {
  134. printf(": ");
  135. }
  136. print_link_flags(ifi->ifi_flags, m_flag);
  137. }
  138. if (tb[IFLA_MTU])
  139. printf("mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
  140. if (tb[IFLA_QDISC])
  141. printf("qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
  142. #ifdef IFLA_MASTER
  143. if (tb[IFLA_MASTER]) {
  144. printf("master %s ", ll_index_to_name(*(int*)RTA_DATA(tb[IFLA_MASTER])));
  145. }
  146. #endif
  147. /* IFLA_OPERSTATE was added to kernel with the same commit as IFF_DORMANT */
  148. #ifdef IFF_DORMANT
  149. if (tb[IFLA_OPERSTATE]) {
  150. static const char operstate_labels[] ALIGN1 =
  151. "UNKNOWN\0""NOTPRESENT\0""DOWN\0""LOWERLAYERDOWN\0"
  152. "TESTING\0""DORMANT\0""UP\0";
  153. printf("state %s ", nth_string(operstate_labels,
  154. *(uint8_t *)RTA_DATA(tb[IFLA_OPERSTATE])));
  155. }
  156. #endif
  157. if (G_filter.showqueue)
  158. print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
  159. if (!G_filter.family || G_filter.family == AF_PACKET) {
  160. SPRINT_BUF(b1);
  161. printf("%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1));
  162. if (tb[IFLA_ADDRESS]) {
  163. fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
  164. RTA_PAYLOAD(tb[IFLA_ADDRESS]),
  165. ifi->ifi_type,
  166. b1, sizeof(b1)), stdout);
  167. }
  168. if (tb[IFLA_BROADCAST]) {
  169. if (ifi->ifi_flags & IFF_POINTOPOINT)
  170. printf(" peer ");
  171. else
  172. printf(" brd ");
  173. fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
  174. RTA_PAYLOAD(tb[IFLA_BROADCAST]),
  175. ifi->ifi_type,
  176. b1, sizeof(b1)), stdout);
  177. }
  178. }
  179. bb_putchar('\n');
  180. /*fflush_all();*/
  181. return 0;
  182. }
  183. static int flush_update(void)
  184. {
  185. if (rtnl_send_check(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
  186. bb_simple_perror_msg("can't send flush request");
  187. return -1;
  188. }
  189. G_filter.flushp = 0;
  190. return 0;
  191. }
  192. static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM,
  193. struct nlmsghdr *n, void *arg UNUSED_PARAM)
  194. {
  195. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  196. int len = n->nlmsg_len;
  197. struct rtattr *rta_tb[IFA_MAX+1];
  198. if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
  199. return 0;
  200. len -= NLMSG_LENGTH(sizeof(*ifa));
  201. if (len < 0) {
  202. bb_error_msg("wrong nlmsg len %d", len);
  203. return -1;
  204. }
  205. if (G_filter.flushb && n->nlmsg_type != RTM_NEWADDR)
  206. return 0;
  207. //memset(rta_tb, 0, sizeof(rta_tb)); - parse_rtattr does this
  208. parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
  209. if (!rta_tb[IFA_LOCAL])
  210. rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
  211. if (!rta_tb[IFA_ADDRESS])
  212. rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
  213. if (G_filter.ifindex && G_filter.ifindex != ifa->ifa_index)
  214. return 0;
  215. if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
  216. return 0;
  217. if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
  218. return 0;
  219. if (G_filter.label) {
  220. const char *label;
  221. if (rta_tb[IFA_LABEL])
  222. label = RTA_DATA(rta_tb[IFA_LABEL]);
  223. else
  224. label = ll_index_to_name(ifa->ifa_index);
  225. if (fnmatch(G_filter.label, label, 0) != 0)
  226. return 0;
  227. }
  228. if (G_filter.pfx.family) {
  229. if (rta_tb[IFA_LOCAL]) {
  230. inet_prefix dst;
  231. memset(&dst, 0, sizeof(dst));
  232. dst.family = ifa->ifa_family;
  233. memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
  234. if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
  235. return 0;
  236. }
  237. }
  238. if (G_filter.flushb) {
  239. struct nlmsghdr *fn;
  240. if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
  241. if (flush_update())
  242. return -1;
  243. }
  244. fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
  245. memcpy(fn, n, n->nlmsg_len);
  246. fn->nlmsg_type = RTM_DELADDR;
  247. fn->nlmsg_flags = NLM_F_REQUEST;
  248. fn->nlmsg_seq = ++G_filter.rth->seq;
  249. G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
  250. G_filter.flushed = 1;
  251. return 0;
  252. }
  253. if (n->nlmsg_type == RTM_DELADDR)
  254. printf("Deleted ");
  255. if (G_filter.oneline)
  256. printf("%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
  257. if (ifa->ifa_family == AF_INET)
  258. printf(" inet ");
  259. else if (ifa->ifa_family == AF_INET6)
  260. printf(" inet6 ");
  261. else
  262. printf(" family %d ", ifa->ifa_family);
  263. if (rta_tb[IFA_LOCAL]) {
  264. fputs(rt_addr_n2a(ifa->ifa_family, RTA_DATA(rta_tb[IFA_LOCAL])),
  265. stdout
  266. );
  267. if (rta_tb[IFA_ADDRESS] == NULL
  268. || memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0
  269. ) {
  270. printf("/%d ", ifa->ifa_prefixlen);
  271. } else {
  272. printf(" peer %s/%d ",
  273. rt_addr_n2a(ifa->ifa_family, RTA_DATA(rta_tb[IFA_ADDRESS])),
  274. ifa->ifa_prefixlen
  275. );
  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. );
  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. );
  289. }
  290. printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope));
  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_DADFAILED) {
  300. ifa->ifa_flags &= ~IFA_F_DADFAILED;
  301. printf("dadfailed ");
  302. }
  303. if (ifa->ifa_flags & IFA_F_DEPRECATED) {
  304. ifa->ifa_flags &= ~IFA_F_DEPRECATED;
  305. printf("deprecated ");
  306. }
  307. if (!(ifa->ifa_flags & IFA_F_PERMANENT)) {
  308. printf("dynamic ");
  309. } else
  310. ifa->ifa_flags &= ~IFA_F_PERMANENT;
  311. if (ifa->ifa_flags)
  312. printf("flags %02x ", ifa->ifa_flags);
  313. if (rta_tb[IFA_LABEL])
  314. fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), stdout);
  315. if (rta_tb[IFA_CACHEINFO]) {
  316. struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
  317. char buf[128];
  318. bb_putchar(_SL_);
  319. if (ci->ifa_valid == 0xFFFFFFFFU)
  320. sprintf(buf, "valid_lft forever");
  321. else
  322. sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
  323. if (ci->ifa_prefered == 0xFFFFFFFFU)
  324. sprintf(buf+strlen(buf), " preferred_lft forever");
  325. else
  326. sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
  327. printf(" %s", buf);
  328. }
  329. bb_putchar('\n');
  330. /*fflush_all();*/
  331. return 0;
  332. }
  333. struct nlmsg_list {
  334. struct nlmsg_list *next;
  335. struct nlmsghdr h;
  336. };
  337. static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
  338. {
  339. for (; ainfo; ainfo = ainfo->next) {
  340. struct nlmsghdr *n = &ainfo->h;
  341. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  342. if (n->nlmsg_type != RTM_NEWADDR)
  343. continue;
  344. if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
  345. return -1;
  346. if (ifa->ifa_index != ifindex
  347. || (G_filter.family && G_filter.family != ifa->ifa_family)
  348. ) {
  349. continue;
  350. }
  351. print_addrinfo(NULL, n, NULL);
  352. }
  353. return 0;
  354. }
  355. static int FAST_FUNC store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
  356. {
  357. struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
  358. struct nlmsg_list *h;
  359. struct nlmsg_list **lp;
  360. h = xzalloc(n->nlmsg_len + sizeof(void*));
  361. memcpy(&h->h, n, n->nlmsg_len);
  362. /*h->next = NULL; - xzalloc did it */
  363. for (lp = linfo; *lp; lp = &(*lp)->next)
  364. continue;
  365. *lp = h;
  366. ll_remember_index(who, n, NULL);
  367. return 0;
  368. }
  369. static void ipaddr_reset_filter(int _oneline)
  370. {
  371. memset(&G_filter, 0, sizeof(G_filter));
  372. G_filter.oneline = _oneline;
  373. }
  374. /* Return value becomes exitcode. It's okay to not return at all */
  375. int FAST_FUNC ipaddr_list_or_flush(char **argv, int flush)
  376. {
  377. static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0";
  378. struct nlmsg_list *linfo = NULL;
  379. struct nlmsg_list *ainfo = NULL;
  380. struct nlmsg_list *l;
  381. struct rtnl_handle rth;
  382. char *filter_dev = NULL;
  383. ipaddr_reset_filter(oneline);
  384. G_filter.showqueue = 1;
  385. if (G_filter.family == AF_UNSPEC)
  386. G_filter.family = preferred_family;
  387. if (flush) {
  388. if (!*argv) {
  389. bb_error_msg_and_die(bb_msg_requires_arg, "flush");
  390. }
  391. if (G_filter.family == AF_PACKET) {
  392. bb_simple_error_msg_and_die("can't flush link addresses");
  393. }
  394. }
  395. while (*argv) {
  396. const smalluint key = index_in_strings(option, *argv);
  397. if (key == 0) { /* to */
  398. NEXT_ARG();
  399. get_prefix(&G_filter.pfx, *argv, G_filter.family);
  400. if (G_filter.family == AF_UNSPEC) {
  401. G_filter.family = G_filter.pfx.family;
  402. }
  403. } else if (key == 1) { /* scope */
  404. uint32_t scope = 0;
  405. NEXT_ARG();
  406. G_filter.scopemask = -1;
  407. if (rtnl_rtscope_a2n(&scope, *argv)) {
  408. if (strcmp(*argv, "all") != 0) {
  409. invarg_1_to_2(*argv, "scope");
  410. }
  411. scope = RT_SCOPE_NOWHERE;
  412. G_filter.scopemask = 0;
  413. }
  414. G_filter.scope = scope;
  415. } else if (key == 2) { /* up */
  416. G_filter.up = 1;
  417. } else if (key == 3) { /* label */
  418. NEXT_ARG();
  419. G_filter.label = *argv;
  420. } else {
  421. if (key == 4) /* dev */
  422. NEXT_ARG();
  423. if (filter_dev)
  424. duparg2("dev", *argv);
  425. filter_dev = *argv;
  426. }
  427. argv++;
  428. }
  429. xrtnl_open(&rth);
  430. xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
  431. xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
  432. if (filter_dev) {
  433. G_filter.ifindex = xll_name_to_index(filter_dev);
  434. }
  435. if (flush) {
  436. char flushb[4096-512];
  437. G_filter.flushb = flushb;
  438. G_filter.flushp = 0;
  439. G_filter.flushe = sizeof(flushb);
  440. G_filter.rth = &rth;
  441. for (;;) {
  442. xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
  443. G_filter.flushed = 0;
  444. xrtnl_dump_filter(&rth, print_addrinfo, NULL);
  445. if (G_filter.flushed == 0) {
  446. return 0;
  447. }
  448. if (flush_update() < 0) {
  449. return 1;
  450. }
  451. }
  452. }
  453. if (G_filter.family != AF_PACKET) {
  454. xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
  455. xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
  456. }
  457. if (G_filter.family && G_filter.family != AF_PACKET) {
  458. struct nlmsg_list **lp;
  459. lp = &linfo;
  460. while ((l = *lp) != NULL) {
  461. int ok = 0;
  462. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  463. struct nlmsg_list *a;
  464. for (a = ainfo; a; a = a->next) {
  465. struct nlmsghdr *n = &a->h;
  466. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  467. if (ifa->ifa_index != ifi->ifi_index
  468. || (G_filter.family && G_filter.family != ifa->ifa_family)
  469. ) {
  470. continue;
  471. }
  472. if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
  473. continue;
  474. if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
  475. continue;
  476. if (G_filter.pfx.family || G_filter.label) {
  477. struct rtattr *tb[IFA_MAX+1];
  478. //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
  479. parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
  480. if (!tb[IFA_LOCAL])
  481. tb[IFA_LOCAL] = tb[IFA_ADDRESS];
  482. if (G_filter.pfx.family && tb[IFA_LOCAL]) {
  483. inet_prefix dst;
  484. memset(&dst, 0, sizeof(dst));
  485. dst.family = ifa->ifa_family;
  486. memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
  487. if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
  488. continue;
  489. }
  490. if (G_filter.label) {
  491. const char *label;
  492. if (tb[IFA_LABEL])
  493. label = RTA_DATA(tb[IFA_LABEL]);
  494. else
  495. label = ll_index_to_name(ifa->ifa_index);
  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 ((oneline && G_filter.family != AF_PACKET)
  511. /* ^^^^^^^^^ "ip -oneline a" does not print link info */
  512. || (print_linkinfo(&l->h) == 0)
  513. ) {
  514. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  515. if (G_filter.family != AF_PACKET)
  516. print_selected_addrinfo(ifi->ifi_index, ainfo);
  517. }
  518. }
  519. return 0;
  520. }
  521. static int default_scope(inet_prefix *lcl)
  522. {
  523. if (lcl->family == AF_INET) {
  524. if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
  525. return RT_SCOPE_HOST;
  526. }
  527. return 0;
  528. }
  529. /* Return value becomes exitcode. It's okay to not return at all */
  530. static int ipaddr_modify(int cmd, int flags, char **argv)
  531. {
  532. /* If you add stuff here, update ipaddr_full_usage */
  533. static const char option[] ALIGN1 =
  534. "peer\0""remote\0""broadcast\0""brd\0"
  535. "anycast\0""scope\0""dev\0""label\0""local\0";
  536. #define option_peer option
  537. #define option_broadcast (option + sizeof("peer") + sizeof("remote"))
  538. #define option_anycast (option_broadcast + sizeof("broadcast") + sizeof("brd"))
  539. struct rtnl_handle rth;
  540. struct {
  541. struct nlmsghdr n;
  542. struct ifaddrmsg ifa;
  543. char buf[256];
  544. } req;
  545. char *d = NULL;
  546. char *l = NULL;
  547. inet_prefix lcl;
  548. inet_prefix peer;
  549. int local_len = 0;
  550. int peer_len = 0;
  551. int brd_len = 0;
  552. int any_len = 0;
  553. bool scoped = 0;
  554. memset(&req, 0, sizeof(req));
  555. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
  556. req.n.nlmsg_flags = NLM_F_REQUEST | flags;
  557. req.n.nlmsg_type = cmd;
  558. req.ifa.ifa_family = preferred_family;
  559. while (*argv) {
  560. unsigned arg = index_in_strings(option, *argv);
  561. /* if search fails, "local" is assumed */
  562. if ((int)arg >= 0)
  563. NEXT_ARG();
  564. if (arg <= 1) { /* peer, remote */
  565. if (peer_len) {
  566. duparg(option_peer, *argv);
  567. }
  568. get_prefix(&peer, *argv, req.ifa.ifa_family);
  569. peer_len = peer.bytelen;
  570. if (req.ifa.ifa_family == AF_UNSPEC) {
  571. req.ifa.ifa_family = peer.family;
  572. }
  573. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
  574. req.ifa.ifa_prefixlen = peer.bitlen;
  575. } else if (arg <= 3) { /* broadcast, brd */
  576. inet_prefix addr;
  577. if (brd_len) {
  578. duparg(option_broadcast, *argv);
  579. }
  580. if (LONE_CHAR(*argv, '+')) {
  581. brd_len = -1;
  582. } else if (LONE_DASH(*argv)) {
  583. brd_len = -2;
  584. } else {
  585. get_addr(&addr, *argv, req.ifa.ifa_family);
  586. if (req.ifa.ifa_family == AF_UNSPEC)
  587. req.ifa.ifa_family = addr.family;
  588. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
  589. brd_len = addr.bytelen;
  590. }
  591. } else if (arg == 4) { /* anycast */
  592. inet_prefix addr;
  593. if (any_len) {
  594. duparg(option_anycast, *argv);
  595. }
  596. get_addr(&addr, *argv, req.ifa.ifa_family);
  597. if (req.ifa.ifa_family == AF_UNSPEC) {
  598. req.ifa.ifa_family = addr.family;
  599. }
  600. addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
  601. any_len = addr.bytelen;
  602. } else if (arg == 5) { /* scope */
  603. uint32_t scope = 0;
  604. if (rtnl_rtscope_a2n(&scope, *argv)) {
  605. invarg_1_to_2(*argv, "scope");
  606. }
  607. req.ifa.ifa_scope = scope;
  608. scoped = 1;
  609. } else if (arg == 6) { /* dev */
  610. d = *argv;
  611. } else if (arg == 7) { /* label */
  612. l = *argv;
  613. addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l) + 1);
  614. } else {
  615. /* local (specified or assumed) */
  616. if (local_len) {
  617. duparg2("local", *argv);
  618. }
  619. get_prefix(&lcl, *argv, req.ifa.ifa_family);
  620. if (req.ifa.ifa_family == AF_UNSPEC) {
  621. req.ifa.ifa_family = lcl.family;
  622. }
  623. addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
  624. local_len = lcl.bytelen;
  625. }
  626. argv++;
  627. }
  628. if (!d) {
  629. /* There was no "dev IFACE", but we need that */
  630. bb_simple_error_msg_and_die("need \"dev IFACE\"");
  631. }
  632. if (l && !is_prefixed_with(l, d)) {
  633. bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
  634. }
  635. if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
  636. peer = lcl;
  637. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
  638. }
  639. if (req.ifa.ifa_prefixlen == 0)
  640. req.ifa.ifa_prefixlen = lcl.bitlen;
  641. if (brd_len < 0 && cmd != RTM_DELADDR) {
  642. inet_prefix brd;
  643. int i;
  644. if (req.ifa.ifa_family != AF_INET) {
  645. bb_simple_error_msg_and_die("broadcast can be set only for IPv4 addresses");
  646. }
  647. brd = peer;
  648. if (brd.bitlen <= 30) {
  649. for (i = 31; i >= brd.bitlen; i--) {
  650. if (brd_len == -1)
  651. brd.data[0] |= htonl(1<<(31-i));
  652. else
  653. brd.data[0] &= ~htonl(1<<(31-i));
  654. }
  655. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
  656. brd_len = brd.bytelen;
  657. }
  658. }
  659. if (!scoped && cmd != RTM_DELADDR)
  660. req.ifa.ifa_scope = default_scope(&lcl);
  661. xrtnl_open(&rth);
  662. ll_init_map(&rth);
  663. req.ifa.ifa_index = xll_name_to_index(d);
  664. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  665. return 2;
  666. return 0;
  667. }
  668. /* Return value becomes exitcode. It's okay to not return at all */
  669. int FAST_FUNC do_ipaddr(char **argv)
  670. {
  671. static const char commands[] ALIGN1 =
  672. /* 0 1 2 3 4 5 6 7 8 */
  673. "add\0""change\0""chg\0""replace\0""delete\0""list\0""show\0""lst\0""flush\0";
  674. int cmd = 2;
  675. INIT_G();
  676. if (*argv) {
  677. cmd = index_in_substrings(commands, *argv);
  678. if (cmd < 0)
  679. invarg_1_to_2(*argv, applet_name);
  680. argv++;
  681. if (cmd <= 4) {
  682. return ipaddr_modify(
  683. /*cmd:*/ cmd == 4 ? RTM_DELADDR : RTM_NEWADDR,
  684. /*flags:*/
  685. cmd == 0 ? NLM_F_CREATE|NLM_F_EXCL : /* add */
  686. cmd == 1 || cmd == 2 ? NLM_F_REPLACE : /* change */
  687. cmd == 3 ? NLM_F_CREATE|NLM_F_REPLACE : /* replace */
  688. 0 /* delete */
  689. , argv);
  690. }
  691. }
  692. return ipaddr_list_or_flush(argv, cmd == 8);
  693. }