3
0

ipaddress.c 22 KB

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