3
0

ipaddress.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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_stdout(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
  174. RTA_PAYLOAD(tb[IFLA_ADDRESS]),
  175. ifi->ifi_type,
  176. b1, sizeof(b1)));
  177. }
  178. if (tb[IFLA_BROADCAST]) {
  179. if (ifi->ifi_flags & IFF_POINTOPOINT)
  180. printf(" peer ");
  181. else
  182. printf(" brd ");
  183. fputs_stdout(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
  184. RTA_PAYLOAD(tb[IFLA_BROADCAST]),
  185. ifi->ifi_type,
  186. b1, sizeof(b1)));
  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_stdout(rt_addr_n2a(ifa->ifa_family, RTA_DATA(rta_tb[IFA_LOCAL])));
  277. if (rta_tb[IFA_ADDRESS] == NULL
  278. || memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0
  279. ) {
  280. printf("/%d ", ifa->ifa_prefixlen);
  281. } else {
  282. printf(" peer %s/%d ",
  283. rt_addr_n2a(ifa->ifa_family, RTA_DATA(rta_tb[IFA_ADDRESS])),
  284. ifa->ifa_prefixlen
  285. );
  286. }
  287. }
  288. if (rta_tb[IFA_BROADCAST]) {
  289. printf("brd %s ",
  290. rt_addr_n2a(ifa->ifa_family,
  291. RTA_DATA(rta_tb[IFA_BROADCAST]))
  292. );
  293. }
  294. if (rta_tb[IFA_ANYCAST]) {
  295. printf("any %s ",
  296. rt_addr_n2a(ifa->ifa_family,
  297. RTA_DATA(rta_tb[IFA_ANYCAST]))
  298. );
  299. }
  300. printf("scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope));
  301. if (ifa_flags & IFA_F_SECONDARY) {
  302. ifa_flags &= ~IFA_F_SECONDARY;
  303. printf("secondary ");
  304. }
  305. if (ifa_flags & IFA_F_TENTATIVE) {
  306. ifa_flags &= ~IFA_F_TENTATIVE;
  307. printf("tentative ");
  308. }
  309. if (ifa_flags & IFA_F_DADFAILED) {
  310. ifa_flags &= ~IFA_F_DADFAILED;
  311. printf("dadfailed ");
  312. }
  313. if (ifa_flags & IFA_F_DEPRECATED) {
  314. ifa_flags &= ~IFA_F_DEPRECATED;
  315. printf("deprecated ");
  316. }
  317. if (!(ifa_flags & IFA_F_PERMANENT)) {
  318. printf("dynamic ");
  319. } else
  320. ifa_flags &= ~IFA_F_PERMANENT;
  321. if (ifa_flags & IFA_F_NOPREFIXROUTE) {
  322. ifa_flags &= ~IFA_F_NOPREFIXROUTE;
  323. printf("noprefixroute ");
  324. }
  325. if (ifa_flags)
  326. printf("flags %02x ", ifa_flags);
  327. if (rta_tb[IFA_LABEL])
  328. fputs_stdout((char*)RTA_DATA(rta_tb[IFA_LABEL]));
  329. if (rta_tb[IFA_CACHEINFO]) {
  330. struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
  331. char buf[128];
  332. bb_putchar(_SL_);
  333. if (ci->ifa_valid == 0xFFFFFFFFU)
  334. sprintf(buf, "valid_lft forever");
  335. else
  336. sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
  337. if (ci->ifa_prefered == 0xFFFFFFFFU)
  338. sprintf(buf+strlen(buf), " preferred_lft forever");
  339. else
  340. sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
  341. printf(" %s", buf);
  342. }
  343. bb_putchar('\n');
  344. /*fflush_all();*/
  345. return 0;
  346. }
  347. struct nlmsg_list {
  348. struct nlmsg_list *next;
  349. struct nlmsghdr h;
  350. };
  351. static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo)
  352. {
  353. for (; ainfo; ainfo = ainfo->next) {
  354. struct nlmsghdr *n = &ainfo->h;
  355. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  356. if (n->nlmsg_type != RTM_NEWADDR)
  357. continue;
  358. if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
  359. return -1;
  360. if (ifa->ifa_index != ifindex
  361. || (G_filter.family && G_filter.family != ifa->ifa_family)
  362. ) {
  363. continue;
  364. }
  365. print_addrinfo(NULL, n, NULL);
  366. }
  367. return 0;
  368. }
  369. static int FAST_FUNC store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
  370. {
  371. struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
  372. struct nlmsg_list *h;
  373. struct nlmsg_list **lp;
  374. h = xzalloc(n->nlmsg_len + sizeof(void*));
  375. memcpy(&h->h, n, n->nlmsg_len);
  376. /*h->next = NULL; - xzalloc did it */
  377. for (lp = linfo; *lp; lp = &(*lp)->next)
  378. continue;
  379. *lp = h;
  380. ll_remember_index(who, n, NULL);
  381. return 0;
  382. }
  383. static void ipaddr_reset_filter(int _oneline)
  384. {
  385. memset(&G_filter, 0, sizeof(G_filter));
  386. G_filter.oneline = _oneline;
  387. }
  388. /* Return value becomes exitcode. It's okay to not return at all */
  389. int FAST_FUNC ipaddr_list_or_flush(char **argv, int flush)
  390. {
  391. static const char option[] ALIGN1 = "to\0""scope\0""up\0""label\0""dev\0";
  392. struct nlmsg_list *linfo = NULL;
  393. struct nlmsg_list *ainfo = NULL;
  394. struct nlmsg_list *l;
  395. struct rtnl_handle rth;
  396. char *filter_dev = NULL;
  397. ipaddr_reset_filter(oneline);
  398. G_filter.showqueue = 1;
  399. if (G_filter.family == AF_UNSPEC)
  400. G_filter.family = preferred_family;
  401. if (flush) {
  402. if (!*argv) {
  403. bb_error_msg_and_die(bb_msg_requires_arg, "flush");
  404. }
  405. if (G_filter.family == AF_PACKET) {
  406. bb_simple_error_msg_and_die("can't flush link addresses");
  407. }
  408. }
  409. while (*argv) {
  410. const smalluint key = index_in_strings(option, *argv);
  411. if (key == 0) { /* to */
  412. NEXT_ARG();
  413. get_prefix(&G_filter.pfx, *argv, G_filter.family);
  414. if (G_filter.family == AF_UNSPEC) {
  415. G_filter.family = G_filter.pfx.family;
  416. }
  417. } else if (key == 1) { /* scope */
  418. uint32_t scope = 0;
  419. NEXT_ARG();
  420. G_filter.scopemask = -1;
  421. if (rtnl_rtscope_a2n(&scope, *argv)) {
  422. if (strcmp(*argv, "all") != 0) {
  423. invarg_1_to_2(*argv, "scope");
  424. }
  425. scope = RT_SCOPE_NOWHERE;
  426. G_filter.scopemask = 0;
  427. }
  428. G_filter.scope = scope;
  429. } else if (key == 2) { /* up */
  430. G_filter.up = 1;
  431. } else if (key == 3) { /* label */
  432. NEXT_ARG();
  433. G_filter.label = *argv;
  434. } else {
  435. if (key == 4) /* dev */
  436. NEXT_ARG();
  437. if (filter_dev)
  438. duparg2("dev", *argv);
  439. filter_dev = *argv;
  440. }
  441. argv++;
  442. }
  443. xrtnl_open(&rth);
  444. xrtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK);
  445. xrtnl_dump_filter(&rth, store_nlmsg, &linfo);
  446. if (filter_dev) {
  447. G_filter.ifindex = xll_name_to_index(filter_dev);
  448. }
  449. if (flush) {
  450. char flushb[4096-512];
  451. G_filter.flushb = flushb;
  452. G_filter.flushp = 0;
  453. G_filter.flushe = sizeof(flushb);
  454. G_filter.rth = &rth;
  455. for (;;) {
  456. xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
  457. G_filter.flushed = 0;
  458. xrtnl_dump_filter(&rth, print_addrinfo, NULL);
  459. if (G_filter.flushed == 0) {
  460. return 0;
  461. }
  462. if (flush_update() < 0) {
  463. return 1;
  464. }
  465. }
  466. }
  467. if (G_filter.family != AF_PACKET) {
  468. xrtnl_wilddump_request(&rth, G_filter.family, RTM_GETADDR);
  469. xrtnl_dump_filter(&rth, store_nlmsg, &ainfo);
  470. }
  471. if (G_filter.family && G_filter.family != AF_PACKET) {
  472. struct nlmsg_list **lp;
  473. lp = &linfo;
  474. while ((l = *lp) != NULL) {
  475. int ok = 0;
  476. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  477. struct nlmsg_list *a;
  478. for (a = ainfo; a; a = a->next) {
  479. struct nlmsghdr *n = &a->h;
  480. struct ifaddrmsg *ifa = NLMSG_DATA(n);
  481. if (ifa->ifa_index != ifi->ifi_index
  482. || (G_filter.family && G_filter.family != ifa->ifa_family)
  483. ) {
  484. continue;
  485. }
  486. if ((G_filter.scope ^ ifa->ifa_scope) & G_filter.scopemask)
  487. continue;
  488. if ((G_filter.flags ^ ifa->ifa_flags) & G_filter.flagmask)
  489. continue;
  490. if (G_filter.pfx.family || G_filter.label) {
  491. struct rtattr *tb[IFA_MAX+1];
  492. //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
  493. parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
  494. if (!tb[IFA_LOCAL])
  495. tb[IFA_LOCAL] = tb[IFA_ADDRESS];
  496. if (G_filter.pfx.family && tb[IFA_LOCAL]) {
  497. inet_prefix dst;
  498. memset(&dst, 0, sizeof(dst));
  499. dst.family = ifa->ifa_family;
  500. memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
  501. if (inet_addr_match(&dst, &G_filter.pfx, G_filter.pfx.bitlen))
  502. continue;
  503. }
  504. if (G_filter.label) {
  505. const char *label;
  506. if (tb[IFA_LABEL])
  507. label = RTA_DATA(tb[IFA_LABEL]);
  508. else
  509. label = ll_index_to_name(ifa->ifa_index);
  510. if (fnmatch(G_filter.label, label, 0) != 0)
  511. continue;
  512. }
  513. }
  514. ok = 1;
  515. break;
  516. }
  517. if (!ok)
  518. *lp = l->next;
  519. else
  520. lp = &l->next;
  521. }
  522. }
  523. for (l = linfo; l; l = l->next) {
  524. if ((oneline && G_filter.family != AF_PACKET)
  525. /* ^^^^^^^^^ "ip -oneline a" does not print link info */
  526. || (print_linkinfo(&l->h) == 0)
  527. ) {
  528. struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
  529. if (G_filter.family != AF_PACKET)
  530. print_selected_addrinfo(ifi->ifi_index, ainfo);
  531. }
  532. }
  533. return 0;
  534. }
  535. static void set_lifetime(unsigned int *lifetime, char *argv, const char *errmsg)
  536. {
  537. if (strcmp(argv, "forever") == 0)
  538. *lifetime = INFINITY_LIFE_TIME;
  539. else
  540. *lifetime = get_u32(argv, errmsg);
  541. }
  542. static int default_scope(inet_prefix *lcl)
  543. {
  544. if (lcl->family == AF_INET) {
  545. if (lcl->bytelen >= 1 && *(uint8_t*)&lcl->data == 127)
  546. return RT_SCOPE_HOST;
  547. }
  548. return 0;
  549. }
  550. /* Return value becomes exitcode. It's okay to not return at all */
  551. static int ipaddr_modify(int cmd, int flags, char **argv)
  552. {
  553. /* If you add stuff here, update ipaddr_full_usage */
  554. static const char option[] ALIGN1 =
  555. "peer\0""remote\0""broadcast\0""brd\0"
  556. "anycast\0""valid_lft\0""preferred_lft\0"
  557. "scope\0""dev\0""label\0""noprefixroute\0""local\0";
  558. #define option_peer option
  559. #define option_broadcast (option + sizeof("peer") + sizeof("remote"))
  560. #define option_anycast (option_broadcast + sizeof("broadcast") + sizeof("brd"))
  561. #define option_valid_lft (option_anycast + sizeof("anycast"))
  562. #define option_pref_lft (option_valid_lft + sizeof("valid_lft"))
  563. struct rtnl_handle rth;
  564. struct {
  565. struct nlmsghdr n;
  566. struct ifaddrmsg ifa;
  567. char buf[256];
  568. } req;
  569. char *d = NULL;
  570. char *l = NULL;
  571. char *valid_lftp = NULL;
  572. char *preferred_lftp = NULL;
  573. inet_prefix lcl;
  574. inet_prefix peer;
  575. int local_len = 0;
  576. int peer_len = 0;
  577. int brd_len = 0;
  578. int any_len = 0;
  579. bool scoped = 0;
  580. __u32 valid_lft = INFINITY_LIFE_TIME;
  581. __u32 preferred_lft = INFINITY_LIFE_TIME;
  582. unsigned int ifa_flags = 0;
  583. memset(&req, 0, sizeof(req));
  584. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
  585. req.n.nlmsg_flags = NLM_F_REQUEST | flags;
  586. req.n.nlmsg_type = cmd;
  587. req.ifa.ifa_family = preferred_family;
  588. while (*argv) {
  589. unsigned arg = index_in_strings(option, *argv);
  590. /* if search fails, "local" is assumed */
  591. if (arg <= 1) { /* peer, remote */
  592. NEXT_ARG();
  593. if (peer_len) {
  594. duparg(option_peer, *argv);
  595. }
  596. get_prefix(&peer, *argv, req.ifa.ifa_family);
  597. peer_len = peer.bytelen;
  598. if (req.ifa.ifa_family == AF_UNSPEC) {
  599. req.ifa.ifa_family = peer.family;
  600. }
  601. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
  602. req.ifa.ifa_prefixlen = peer.bitlen;
  603. } else if (arg <= 3) { /* broadcast, brd */
  604. inet_prefix addr;
  605. NEXT_ARG();
  606. if (brd_len) {
  607. duparg(option_broadcast, *argv);
  608. }
  609. if (LONE_CHAR(*argv, '+')) {
  610. brd_len = -1;
  611. } else if (LONE_DASH(*argv)) {
  612. brd_len = -2;
  613. } else {
  614. get_addr(&addr, *argv, req.ifa.ifa_family);
  615. if (req.ifa.ifa_family == AF_UNSPEC)
  616. req.ifa.ifa_family = addr.family;
  617. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
  618. brd_len = addr.bytelen;
  619. }
  620. } else if (arg == 4) { /* anycast */
  621. inet_prefix addr;
  622. NEXT_ARG();
  623. if (any_len) {
  624. duparg(option_anycast, *argv);
  625. }
  626. get_addr(&addr, *argv, req.ifa.ifa_family);
  627. if (req.ifa.ifa_family == AF_UNSPEC) {
  628. req.ifa.ifa_family = addr.family;
  629. }
  630. addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
  631. any_len = addr.bytelen;
  632. } else if (arg == 5) { /* valid_lft */
  633. if (valid_lftp)
  634. duparg(option_valid_lft, *argv);
  635. NEXT_ARG();
  636. valid_lftp = *argv;
  637. set_lifetime(&valid_lft, *argv, option_valid_lft);
  638. } else if (arg == 6) { /* preferred_lft */
  639. if (preferred_lftp)
  640. duparg(option_pref_lft, *argv);
  641. NEXT_ARG();
  642. preferred_lftp = *argv;
  643. set_lifetime(&preferred_lft, *argv, option_pref_lft);
  644. } else if (arg == 7) { /* scope */
  645. uint32_t scope = 0;
  646. NEXT_ARG();
  647. if (rtnl_rtscope_a2n(&scope, *argv)) {
  648. invarg_1_to_2(*argv, "scope");
  649. }
  650. req.ifa.ifa_scope = scope;
  651. scoped = 1;
  652. } else if (arg == 8) { /* dev */
  653. NEXT_ARG();
  654. d = *argv;
  655. } else if (arg == 9) { /* label */
  656. NEXT_ARG();
  657. l = *argv;
  658. addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l) + 1);
  659. } else if (arg == 10) { /* noprefixroute */
  660. ifa_flags |= IFA_F_NOPREFIXROUTE;
  661. } else {
  662. /* local (specified or assumed) */
  663. if ((int)arg >= 0)
  664. NEXT_ARG();
  665. if (local_len) {
  666. duparg2("local", *argv);
  667. }
  668. get_prefix(&lcl, *argv, req.ifa.ifa_family);
  669. if (req.ifa.ifa_family == AF_UNSPEC) {
  670. req.ifa.ifa_family = lcl.family;
  671. }
  672. addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
  673. local_len = lcl.bytelen;
  674. }
  675. argv++;
  676. }
  677. if (ifa_flags <= 0xff)
  678. req.ifa.ifa_flags = ifa_flags;
  679. else
  680. addattr32(&req.n, sizeof(req), IFA_FLAGS, ifa_flags);
  681. if (!d) {
  682. /* There was no "dev IFACE", but we need that */
  683. bb_simple_error_msg_and_die("need \"dev IFACE\"");
  684. }
  685. if (l && !is_prefixed_with(l, d)) {
  686. bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s)", d, l);
  687. }
  688. if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
  689. peer = lcl;
  690. addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
  691. }
  692. if (req.ifa.ifa_prefixlen == 0)
  693. req.ifa.ifa_prefixlen = lcl.bitlen;
  694. if (brd_len < 0 && cmd != RTM_DELADDR) {
  695. inet_prefix brd;
  696. int i;
  697. if (req.ifa.ifa_family != AF_INET) {
  698. bb_simple_error_msg_and_die("broadcast can be set only for IPv4 addresses");
  699. }
  700. brd = peer;
  701. if (brd.bitlen <= 30) {
  702. for (i = 31; i >= brd.bitlen; i--) {
  703. if (brd_len == -1)
  704. brd.data[0] |= htonl(1<<(31-i));
  705. else
  706. brd.data[0] &= ~htonl(1<<(31-i));
  707. }
  708. addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
  709. brd_len = brd.bytelen;
  710. }
  711. }
  712. if (!scoped && cmd != RTM_DELADDR)
  713. req.ifa.ifa_scope = default_scope(&lcl);
  714. xrtnl_open(&rth);
  715. ll_init_map(&rth);
  716. req.ifa.ifa_index = xll_name_to_index(d);
  717. if (valid_lftp || preferred_lftp) {
  718. struct ifa_cacheinfo cinfo = {};
  719. if (!valid_lft) {
  720. fprintf(stderr, "valid_lft is zero\n");
  721. return 1;
  722. }
  723. if (valid_lft < preferred_lft) {
  724. fprintf(stderr, "preferred_lft is greater than valid_lft\n");
  725. return 1;
  726. }
  727. cinfo.ifa_prefered = preferred_lft;
  728. cinfo.ifa_valid = valid_lft;
  729. addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo,
  730. sizeof(cinfo));
  731. }
  732. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
  733. return 2;
  734. return 0;
  735. }
  736. /* Return value becomes exitcode. It's okay to not return at all */
  737. int FAST_FUNC do_ipaddr(char **argv)
  738. {
  739. static const char commands[] ALIGN1 =
  740. /* 0 1 2 3 4 5 6 7 8 */
  741. "add\0""change\0""chg\0""replace\0""delete\0""list\0""show\0""lst\0""flush\0";
  742. int cmd = 2;
  743. INIT_G();
  744. if (*argv) {
  745. cmd = index_in_substrings(commands, *argv);
  746. if (cmd < 0)
  747. invarg_1_to_2(*argv, applet_name);
  748. argv++;
  749. if (cmd <= 4) {
  750. return ipaddr_modify(
  751. /*cmd:*/ cmd == 4 ? RTM_DELADDR : RTM_NEWADDR,
  752. /*flags:*/
  753. cmd == 0 ? NLM_F_CREATE|NLM_F_EXCL : /* add */
  754. cmd == 1 || cmd == 2 ? NLM_F_REPLACE : /* change */
  755. cmd == 3 ? NLM_F_CREATE|NLM_F_REPLACE : /* replace */
  756. 0 /* delete */
  757. , argv);
  758. }
  759. }
  760. return ipaddr_list_or_flush(argv, cmd == 8);
  761. }