interface.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * stolen from net-tools-1.59 and stripped down for busybox by
  4. * Erik Andersen <andersen@codepoet.org>
  5. *
  6. * Heavily modified by Manuel Novoa III Mar 12, 2001
  7. *
  8. * Added print_bytes_scaled function to reduce code size.
  9. * Added some (potentially) missing defines.
  10. * Improved display support for -a and for a named interface.
  11. *
  12. * -----------------------------------------------------------
  13. *
  14. * ifconfig This file contains an implementation of the command
  15. * that either displays or sets the characteristics of
  16. * one or more of the system's networking interfaces.
  17. *
  18. *
  19. * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  20. * and others. Copyright 1993 MicroWalt Corporation
  21. *
  22. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  23. *
  24. * Patched to support 'add' and 'del' keywords for INET(4) addresses
  25. * by Mrs. Brisby <mrs.brisby@nimh.org>
  26. *
  27. * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  28. * - gettext instead of catgets for i18n
  29. * 10/1998 - Andi Kleen. Use interface list primitives.
  30. * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu
  31. * (default AF was wrong)
  32. */
  33. #include "libbb.h"
  34. #include "inet_common.h"
  35. #include <net/if.h>
  36. #include <net/if_arp.h>
  37. #ifdef HAVE_NET_ETHERNET_H
  38. # include <net/ethernet.h>
  39. #endif
  40. #if ENABLE_FEATURE_HWIB
  41. /* #include <linux/if_infiniband.h> */
  42. # undef INFINIBAND_ALEN
  43. # define INFINIBAND_ALEN 20
  44. #endif
  45. #if ENABLE_FEATURE_IPV6
  46. # define HAVE_AFINET6 1
  47. #else
  48. # undef HAVE_AFINET6
  49. #endif
  50. #define _PATH_PROCNET_DEV "/proc/net/dev"
  51. #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
  52. #ifdef HAVE_AFINET6
  53. # ifndef _LINUX_IN6_H
  54. /*
  55. * This is from linux/include/net/ipv6.h
  56. */
  57. struct in6_ifreq {
  58. struct in6_addr ifr6_addr;
  59. uint32_t ifr6_prefixlen;
  60. unsigned int ifr6_ifindex;
  61. };
  62. # endif
  63. #endif /* HAVE_AFINET6 */
  64. /* Defines for glibc2.0 users. */
  65. #ifndef SIOCSIFTXQLEN
  66. # define SIOCSIFTXQLEN 0x8943
  67. # define SIOCGIFTXQLEN 0x8942
  68. #endif
  69. /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
  70. #ifndef ifr_qlen
  71. # define ifr_qlen ifr_ifru.ifru_mtu
  72. #endif
  73. #ifndef HAVE_TXQUEUELEN
  74. # define HAVE_TXQUEUELEN 1
  75. #endif
  76. #ifndef IFF_DYNAMIC
  77. # define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
  78. #endif
  79. /* Display an Internet socket address. */
  80. static const char* FAST_FUNC INET_sprint(struct sockaddr *sap, int numeric)
  81. {
  82. if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
  83. return "[NONE SET]";
  84. return auto_string(INET_rresolve((struct sockaddr_in *) sap, numeric, 0xffffff00));
  85. }
  86. #ifdef UNUSED_AND_BUGGY
  87. static int INET_getsock(char *bufp, struct sockaddr *sap)
  88. {
  89. char *sp = bufp, *bp;
  90. unsigned int i;
  91. unsigned val;
  92. struct sockaddr_in *sock_in;
  93. sock_in = (struct sockaddr_in *) sap;
  94. sock_in->sin_family = AF_INET;
  95. sock_in->sin_port = 0;
  96. val = 0;
  97. bp = (char *) &val;
  98. for (i = 0; i < sizeof(sock_in->sin_addr.s_addr); i++) {
  99. *sp = toupper(*sp);
  100. if ((unsigned)(*sp - 'A') <= 5)
  101. bp[i] |= (int) (*sp - ('A' - 10));
  102. else if (isdigit(*sp))
  103. bp[i] |= (int) (*sp - '0');
  104. else
  105. return -1;
  106. bp[i] <<= 4;
  107. sp++;
  108. *sp = toupper(*sp);
  109. if ((unsigned)(*sp - 'A') <= 5)
  110. bp[i] |= (int) (*sp - ('A' - 10));
  111. else if (isdigit(*sp))
  112. bp[i] |= (int) (*sp - '0');
  113. else
  114. return -1;
  115. sp++;
  116. }
  117. sock_in->sin_addr.s_addr = htonl(val);
  118. return (sp - bufp);
  119. }
  120. #endif
  121. static int FAST_FUNC INET_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
  122. {
  123. return INET_resolve(bufp, (struct sockaddr_in *) sap, 0);
  124. /*
  125. switch (type) {
  126. case 1:
  127. return (INET_getsock(bufp, sap));
  128. case 256:
  129. return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
  130. default:
  131. return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
  132. }
  133. */
  134. }
  135. static const struct aftype inet_aftype = {
  136. .name = "inet",
  137. .title = "DARPA Internet",
  138. .af = AF_INET,
  139. .alen = 4,
  140. .sprint = INET_sprint,
  141. .input = INET_input,
  142. };
  143. #ifdef HAVE_AFINET6
  144. /* Display an Internet socket address. */
  145. /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
  146. static const char* FAST_FUNC INET6_sprint(struct sockaddr *sap, int numeric)
  147. {
  148. if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
  149. return "[NONE SET]";
  150. return auto_string(INET6_rresolve((struct sockaddr_in6 *) sap, numeric));
  151. }
  152. #ifdef UNUSED
  153. static int INET6_getsock(char *bufp, struct sockaddr *sap)
  154. {
  155. struct sockaddr_in6 *sin6;
  156. sin6 = (struct sockaddr_in6 *) sap;
  157. sin6->sin6_family = AF_INET6;
  158. sin6->sin6_port = 0;
  159. if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
  160. return -1;
  161. return 16; /* ?;) */
  162. }
  163. #endif
  164. static int FAST_FUNC INET6_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
  165. {
  166. return INET6_resolve(bufp, (struct sockaddr_in6 *) sap);
  167. /*
  168. switch (type) {
  169. case 1:
  170. return (INET6_getsock(bufp, sap));
  171. default:
  172. return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
  173. }
  174. */
  175. }
  176. static const struct aftype inet6_aftype = {
  177. .name = "inet6",
  178. .title = "IPv6",
  179. .af = AF_INET6,
  180. .alen = sizeof(struct in6_addr),
  181. .sprint = INET6_sprint,
  182. .input = INET6_input,
  183. };
  184. #endif /* HAVE_AFINET6 */
  185. /* Display an UNSPEC address. */
  186. static char* FAST_FUNC UNSPEC_print(unsigned char *ptr)
  187. {
  188. char *buff;
  189. char *pos;
  190. unsigned int i;
  191. buff = auto_string(xmalloc(sizeof(struct sockaddr) * 3 + 1));
  192. pos = buff;
  193. for (i = 0; i < sizeof(struct sockaddr); i++) {
  194. /* careful -- not every libc's sprintf returns # bytes written */
  195. sprintf(pos, "%02X-", *ptr++);
  196. pos += 3;
  197. }
  198. /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
  199. *--pos = '\0';
  200. return buff;
  201. }
  202. /* Display an UNSPEC socket address. */
  203. static const char* FAST_FUNC UNSPEC_sprint(struct sockaddr *sap, int numeric UNUSED_PARAM)
  204. {
  205. if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
  206. return "[NONE SET]";
  207. return UNSPEC_print((unsigned char *)sap->sa_data);
  208. }
  209. static const struct aftype unspec_aftype = {
  210. .name = "unspec",
  211. .title = "UNSPEC",
  212. .af = AF_UNSPEC,
  213. .alen = 0,
  214. .print = UNSPEC_print,
  215. .sprint = UNSPEC_sprint,
  216. };
  217. static const struct aftype *const aftypes[] = {
  218. &inet_aftype,
  219. #ifdef HAVE_AFINET6
  220. &inet6_aftype,
  221. #endif
  222. &unspec_aftype,
  223. NULL
  224. };
  225. /* Check our protocol family table for this family. */
  226. const struct aftype* FAST_FUNC get_aftype(const char *name)
  227. {
  228. const struct aftype *const *afp;
  229. afp = aftypes;
  230. while (*afp != NULL) {
  231. if (strcmp((*afp)->name, name) == 0)
  232. return (*afp);
  233. afp++;
  234. }
  235. return NULL;
  236. }
  237. /* Check our protocol family table for this family. */
  238. static const struct aftype *get_afntype(int af)
  239. {
  240. const struct aftype *const *afp;
  241. afp = aftypes;
  242. while (*afp != NULL) {
  243. if ((*afp)->af == af)
  244. return *afp;
  245. afp++;
  246. }
  247. return NULL;
  248. }
  249. struct user_net_device_stats {
  250. unsigned long long rx_packets; /* total packets received */
  251. unsigned long long tx_packets; /* total packets transmitted */
  252. unsigned long long rx_bytes; /* total bytes received */
  253. unsigned long long tx_bytes; /* total bytes transmitted */
  254. unsigned long rx_errors; /* bad packets received */
  255. unsigned long tx_errors; /* packet transmit problems */
  256. unsigned long rx_dropped; /* no space in linux buffers */
  257. unsigned long tx_dropped; /* no space available in linux */
  258. unsigned long rx_multicast; /* multicast packets received */
  259. unsigned long rx_compressed;
  260. unsigned long tx_compressed;
  261. unsigned long collisions;
  262. /* detailed rx_errors: */
  263. unsigned long rx_length_errors;
  264. unsigned long rx_over_errors; /* receiver ring buff overflow */
  265. unsigned long rx_crc_errors; /* recved pkt with crc error */
  266. unsigned long rx_frame_errors; /* recv'd frame alignment error */
  267. unsigned long rx_fifo_errors; /* recv'r fifo overrun */
  268. unsigned long rx_missed_errors; /* receiver missed packet */
  269. /* detailed tx_errors */
  270. unsigned long tx_aborted_errors;
  271. unsigned long tx_carrier_errors;
  272. unsigned long tx_fifo_errors;
  273. unsigned long tx_heartbeat_errors;
  274. unsigned long tx_window_errors;
  275. };
  276. struct interface {
  277. struct interface *next, *prev;
  278. char name[IFNAMSIZ]; /* interface name */
  279. short type; /* if type */
  280. short flags; /* various flags */
  281. int metric; /* routing metric */
  282. int mtu; /* MTU value */
  283. int tx_queue_len; /* transmit queue length */
  284. struct ifmap map; /* hardware setup */
  285. struct sockaddr addr; /* IP address */
  286. struct sockaddr dstaddr; /* P-P IP address */
  287. struct sockaddr broadaddr; /* IP broadcast address */
  288. struct sockaddr netmask; /* IP network mask */
  289. int has_ip;
  290. char hwaddr[32]; /* HW address */
  291. int statistics_valid;
  292. struct user_net_device_stats stats; /* statistics */
  293. int keepalive; /* keepalive value for SLIP */
  294. int outfill; /* outfill value for SLIP */
  295. };
  296. smallint interface_opt_a; /* show all interfaces */
  297. static struct interface *int_list, *int_last;
  298. #if 0
  299. /* like strcmp(), but knows about numbers */
  300. except that the freshly added calls to xatoul() brf on ethernet aliases with
  301. uClibc with e.g.: ife->name='lo' name='eth0:1'
  302. static int nstrcmp(const char *a, const char *b)
  303. {
  304. const char *a_ptr = a;
  305. const char *b_ptr = b;
  306. while (*a == *b) {
  307. if (*a == '\0') {
  308. return 0;
  309. }
  310. if (!isdigit(*a) && isdigit(*(a+1))) {
  311. a_ptr = a+1;
  312. b_ptr = b+1;
  313. }
  314. a++;
  315. b++;
  316. }
  317. if (isdigit(*a) && isdigit(*b)) {
  318. return xatoul(a_ptr) > xatoul(b_ptr) ? 1 : -1;
  319. }
  320. return *a - *b;
  321. }
  322. #endif
  323. static struct interface *add_interface(char *name)
  324. {
  325. struct interface *ife, **nextp, *new;
  326. for (ife = int_last; ife; ife = ife->prev) {
  327. int n = /*n*/strcmp(ife->name, name);
  328. if (n == 0)
  329. return ife;
  330. if (n < 0)
  331. break;
  332. }
  333. new = xzalloc(sizeof(*new));
  334. strncpy_IFNAMSIZ(new->name, name);
  335. nextp = ife ? &ife->next : &int_list;
  336. new->prev = ife;
  337. new->next = *nextp;
  338. if (new->next)
  339. new->next->prev = new;
  340. else
  341. int_last = new;
  342. *nextp = new;
  343. return new;
  344. }
  345. static char *get_name(char *name, char *p)
  346. {
  347. /* Extract <name> from nul-terminated p where p matches
  348. * <name>: after leading whitespace.
  349. * If match is not made, set name empty and return unchanged p
  350. */
  351. char *nameend;
  352. char *namestart = skip_whitespace(p);
  353. nameend = namestart;
  354. while (*nameend && *nameend != ':' && !isspace(*nameend))
  355. nameend++;
  356. if (*nameend == ':') {
  357. if ((nameend - namestart) < IFNAMSIZ) {
  358. memcpy(name, namestart, nameend - namestart);
  359. name[nameend - namestart] = '\0';
  360. p = nameend;
  361. } else {
  362. /* Interface name too large */
  363. name[0] = '\0';
  364. }
  365. } else {
  366. /* trailing ':' not found - return empty */
  367. name[0] = '\0';
  368. }
  369. return p + 1;
  370. }
  371. /* If scanf supports size qualifiers for %n conversions, then we can
  372. * use a modified fmt that simply stores the position in the fields
  373. * having no associated fields in the proc string. Of course, we need
  374. * to zero them again when we're done. But that is smaller than the
  375. * old approach of multiple scanf occurrences with large numbers of
  376. * args. */
  377. /* static const char *const ss_fmt[] = { */
  378. /* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */
  379. /* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */
  380. /* "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" */
  381. /* }; */
  382. /* Lie about the size of the int pointed to for %n. */
  383. #if INT_MAX == LONG_MAX
  384. static const char *const ss_fmt[] = {
  385. "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u",
  386. "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u",
  387. "%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u"
  388. };
  389. #else
  390. static const char *const ss_fmt[] = {
  391. "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu",
  392. "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu",
  393. "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu"
  394. };
  395. #endif
  396. static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
  397. {
  398. memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
  399. sscanf(bp, ss_fmt[procnetdev_vsn],
  400. &ife->stats.rx_bytes, /* missing for 0 */
  401. &ife->stats.rx_packets,
  402. &ife->stats.rx_errors,
  403. &ife->stats.rx_dropped,
  404. &ife->stats.rx_fifo_errors,
  405. &ife->stats.rx_frame_errors,
  406. &ife->stats.rx_compressed, /* missing for <= 1 */
  407. &ife->stats.rx_multicast, /* missing for <= 1 */
  408. &ife->stats.tx_bytes, /* missing for 0 */
  409. &ife->stats.tx_packets,
  410. &ife->stats.tx_errors,
  411. &ife->stats.tx_dropped,
  412. &ife->stats.tx_fifo_errors,
  413. &ife->stats.collisions,
  414. &ife->stats.tx_carrier_errors,
  415. &ife->stats.tx_compressed /* missing for <= 1 */
  416. );
  417. if (procnetdev_vsn <= 1) {
  418. if (procnetdev_vsn == 0) {
  419. ife->stats.rx_bytes = 0;
  420. ife->stats.tx_bytes = 0;
  421. }
  422. ife->stats.rx_multicast = 0;
  423. ife->stats.rx_compressed = 0;
  424. ife->stats.tx_compressed = 0;
  425. }
  426. }
  427. static int procnetdev_version(char *buf)
  428. {
  429. if (strstr(buf, "compressed"))
  430. return 2;
  431. if (strstr(buf, "bytes"))
  432. return 1;
  433. return 0;
  434. }
  435. static int if_readconf(void)
  436. {
  437. int numreqs = 30;
  438. struct ifconf ifc;
  439. struct ifreq *ifr;
  440. int n, err = -1;
  441. int skfd;
  442. ifc.ifc_buf = NULL;
  443. /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
  444. (as of 2.1.128) */
  445. skfd = socket(AF_INET, SOCK_DGRAM, 0);
  446. if (skfd < 0) {
  447. bb_perror_msg("error: no inet socket available");
  448. return -1;
  449. }
  450. for (;;) {
  451. ifc.ifc_len = sizeof(struct ifreq) * numreqs;
  452. ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
  453. if (ioctl_or_warn(skfd, SIOCGIFCONF, &ifc) < 0) {
  454. goto out;
  455. }
  456. if (ifc.ifc_len == (int)(sizeof(struct ifreq) * numreqs)) {
  457. /* assume it overflowed and try again */
  458. numreqs += 10;
  459. continue;
  460. }
  461. break;
  462. }
  463. ifr = ifc.ifc_req;
  464. for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
  465. add_interface(ifr->ifr_name);
  466. ifr++;
  467. }
  468. err = 0;
  469. out:
  470. close(skfd);
  471. free(ifc.ifc_buf);
  472. return err;
  473. }
  474. static int if_readlist_proc(char *target)
  475. {
  476. static smallint proc_read;
  477. FILE *fh;
  478. char buf[512];
  479. struct interface *ife;
  480. int err, procnetdev_vsn;
  481. if (proc_read)
  482. return 0;
  483. if (!target)
  484. proc_read = 1;
  485. fh = fopen_or_warn(_PATH_PROCNET_DEV, "r");
  486. if (!fh) {
  487. return if_readconf();
  488. }
  489. fgets(buf, sizeof buf, fh); /* eat line */
  490. fgets(buf, sizeof buf, fh);
  491. procnetdev_vsn = procnetdev_version(buf);
  492. err = 0;
  493. while (fgets(buf, sizeof buf, fh)) {
  494. char *s, name[128];
  495. s = get_name(name, buf);
  496. ife = add_interface(name);
  497. get_dev_fields(s, ife, procnetdev_vsn);
  498. ife->statistics_valid = 1;
  499. if (target && strcmp(target, name) == 0)
  500. break;
  501. }
  502. if (ferror(fh)) {
  503. bb_perror_msg(_PATH_PROCNET_DEV);
  504. err = -1;
  505. proc_read = 0;
  506. }
  507. fclose(fh);
  508. return err;
  509. }
  510. static int if_readlist(void)
  511. {
  512. int err = if_readlist_proc(NULL);
  513. /* Needed in order to get ethN:M aliases */
  514. if (!err)
  515. err = if_readconf();
  516. return err;
  517. }
  518. /* Fetch the interface configuration from the kernel. */
  519. static int if_fetch(struct interface *ife)
  520. {
  521. struct ifreq ifr;
  522. char *ifname = ife->name;
  523. int skfd;
  524. skfd = xsocket(AF_INET, SOCK_DGRAM, 0);
  525. strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
  526. if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) {
  527. close(skfd);
  528. return -1;
  529. }
  530. ife->flags = ifr.ifr_flags;
  531. strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
  532. memset(ife->hwaddr, 0, 32);
  533. if (ioctl(skfd, SIOCGIFHWADDR, &ifr) >= 0)
  534. memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
  535. ife->type = ifr.ifr_hwaddr.sa_family;
  536. strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
  537. ife->metric = 0;
  538. if (ioctl(skfd, SIOCGIFMETRIC, &ifr) >= 0)
  539. ife->metric = ifr.ifr_metric;
  540. strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
  541. ife->mtu = 0;
  542. if (ioctl(skfd, SIOCGIFMTU, &ifr) >= 0)
  543. ife->mtu = ifr.ifr_mtu;
  544. memset(&ife->map, 0, sizeof(struct ifmap));
  545. #ifdef SIOCGIFMAP
  546. strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
  547. if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
  548. ife->map = ifr.ifr_map;
  549. #endif
  550. #ifdef HAVE_TXQUEUELEN
  551. strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
  552. ife->tx_queue_len = -1; /* unknown value */
  553. if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) >= 0)
  554. ife->tx_queue_len = ifr.ifr_qlen;
  555. #else
  556. ife->tx_queue_len = -1; /* unknown value */
  557. #endif
  558. strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
  559. ifr.ifr_addr.sa_family = AF_INET;
  560. memset(&ife->addr, 0, sizeof(struct sockaddr));
  561. if (ioctl(skfd, SIOCGIFADDR, &ifr) == 0) {
  562. ife->has_ip = 1;
  563. ife->addr = ifr.ifr_addr;
  564. strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
  565. memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
  566. if (ioctl(skfd, SIOCGIFDSTADDR, &ifr) >= 0)
  567. ife->dstaddr = ifr.ifr_dstaddr;
  568. strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
  569. memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
  570. if (ioctl(skfd, SIOCGIFBRDADDR, &ifr) >= 0)
  571. ife->broadaddr = ifr.ifr_broadaddr;
  572. strncpy_IFNAMSIZ(ifr.ifr_name, ifname);
  573. memset(&ife->netmask, 0, sizeof(struct sockaddr));
  574. if (ioctl(skfd, SIOCGIFNETMASK, &ifr) >= 0)
  575. ife->netmask = ifr.ifr_netmask;
  576. }
  577. close(skfd);
  578. return 0;
  579. }
  580. static int do_if_fetch(struct interface *ife)
  581. {
  582. if (if_fetch(ife) < 0) {
  583. const char *errmsg;
  584. if (errno == ENODEV) {
  585. /* Give better error message for this case. */
  586. errmsg = "Device not found";
  587. } else {
  588. errmsg = strerror(errno);
  589. }
  590. bb_error_msg("%s: error fetching interface information: %s",
  591. ife->name, errmsg);
  592. return -1;
  593. }
  594. return 0;
  595. }
  596. static const struct hwtype unspec_hwtype = {
  597. .name = "unspec",
  598. .title = "UNSPEC",
  599. .type = -1,
  600. .print = UNSPEC_print
  601. };
  602. static const struct hwtype loop_hwtype = {
  603. .name = "loop",
  604. .title = "Local Loopback",
  605. .type = ARPHRD_LOOPBACK
  606. };
  607. /* Display an Ethernet address in readable format. */
  608. static char* FAST_FUNC ether_print(unsigned char *ptr)
  609. {
  610. char *buff;
  611. buff = xasprintf("%02X:%02X:%02X:%02X:%02X:%02X",
  612. ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]
  613. );
  614. return auto_string(buff);
  615. }
  616. static const struct hwtype ether_hwtype = {
  617. .name = "ether",
  618. .title = "Ethernet",
  619. .type = ARPHRD_ETHER,
  620. .alen = ETH_ALEN,
  621. .print = ether_print,
  622. .input = in_ether
  623. };
  624. static const struct hwtype ppp_hwtype = {
  625. .name = "ppp",
  626. .title = "Point-to-Point Protocol",
  627. .type = ARPHRD_PPP
  628. };
  629. #if ENABLE_FEATURE_IPV6
  630. static const struct hwtype sit_hwtype = {
  631. .name = "sit",
  632. .title = "IPv6-in-IPv4",
  633. .type = ARPHRD_SIT,
  634. .print = UNSPEC_print,
  635. .suppress_null_addr = 1
  636. };
  637. #endif
  638. #if ENABLE_FEATURE_HWIB
  639. static const struct hwtype ib_hwtype = {
  640. .name = "infiniband",
  641. .title = "InfiniBand",
  642. .type = ARPHRD_INFINIBAND,
  643. .alen = INFINIBAND_ALEN,
  644. .print = UNSPEC_print,
  645. .input = in_ib,
  646. };
  647. #endif
  648. static const struct hwtype *const hwtypes[] = {
  649. &loop_hwtype,
  650. &ether_hwtype,
  651. &ppp_hwtype,
  652. &unspec_hwtype,
  653. #if ENABLE_FEATURE_IPV6
  654. &sit_hwtype,
  655. #endif
  656. #if ENABLE_FEATURE_HWIB
  657. &ib_hwtype,
  658. #endif
  659. NULL
  660. };
  661. #ifdef IFF_PORTSEL
  662. static const char *const if_port_text[] = {
  663. /* Keep in step with <linux/netdevice.h> */
  664. "unknown",
  665. "10base2",
  666. "10baseT",
  667. "AUI",
  668. "100baseT",
  669. "100baseTX",
  670. "100baseFX",
  671. NULL
  672. };
  673. #endif
  674. /* Check our hardware type table for this type. */
  675. const struct hwtype* FAST_FUNC get_hwtype(const char *name)
  676. {
  677. const struct hwtype *const *hwp;
  678. hwp = hwtypes;
  679. while (*hwp != NULL) {
  680. if (strcmp((*hwp)->name, name) == 0)
  681. return (*hwp);
  682. hwp++;
  683. }
  684. return NULL;
  685. }
  686. /* Check our hardware type table for this type. */
  687. const struct hwtype* FAST_FUNC get_hwntype(int type)
  688. {
  689. const struct hwtype *const *hwp;
  690. hwp = hwtypes;
  691. while (*hwp != NULL) {
  692. if ((*hwp)->type == type)
  693. return *hwp;
  694. hwp++;
  695. }
  696. return NULL;
  697. }
  698. /* return 1 if address is all zeros */
  699. static int hw_null_address(const struct hwtype *hw, void *ap)
  700. {
  701. int i;
  702. unsigned char *address = (unsigned char *) ap;
  703. for (i = 0; i < hw->alen; i++)
  704. if (address[i])
  705. return 0;
  706. return 1;
  707. }
  708. static const char TRext[] ALIGN1 = "\0\0\0Ki\0Mi\0Gi\0Ti";
  709. static void print_bytes_scaled(unsigned long long ull, const char *end)
  710. {
  711. unsigned long long int_part;
  712. const char *ext;
  713. unsigned int frac_part;
  714. int i;
  715. frac_part = 0;
  716. ext = TRext;
  717. int_part = ull;
  718. i = 4;
  719. do {
  720. if (int_part >= 1024) {
  721. frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
  722. int_part /= 1024;
  723. ext += 3; /* KiB, MiB, GiB, TiB */
  724. }
  725. --i;
  726. } while (i);
  727. printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end);
  728. }
  729. #ifdef HAVE_AFINET6
  730. #define IPV6_ADDR_ANY 0x0000U
  731. #define IPV6_ADDR_UNICAST 0x0001U
  732. #define IPV6_ADDR_MULTICAST 0x0002U
  733. #define IPV6_ADDR_ANYCAST 0x0004U
  734. #define IPV6_ADDR_LOOPBACK 0x0010U
  735. #define IPV6_ADDR_LINKLOCAL 0x0020U
  736. #define IPV6_ADDR_SITELOCAL 0x0040U
  737. #define IPV6_ADDR_COMPATv4 0x0080U
  738. #define IPV6_ADDR_SCOPE_MASK 0x00f0U
  739. #define IPV6_ADDR_MAPPED 0x1000U
  740. #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
  741. static void ife_print6(struct interface *ptr)
  742. {
  743. FILE *f;
  744. char addr6[40], devname[21];
  745. struct sockaddr_in6 sap;
  746. int plen, scope, dad_status, if_idx;
  747. char addr6p[8][5];
  748. f = fopen_for_read(_PATH_PROCNET_IFINET6);
  749. if (f == NULL)
  750. return;
  751. while (fscanf
  752. (f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
  753. addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
  754. addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
  755. &dad_status, devname) != EOF
  756. ) {
  757. if (strcmp(devname, ptr->name) == 0) {
  758. sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
  759. addr6p[0], addr6p[1], addr6p[2], addr6p[3],
  760. addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
  761. memset(&sap, 0, sizeof(sap));
  762. inet_pton(AF_INET6, addr6,
  763. (struct sockaddr *) &sap.sin6_addr);
  764. sap.sin6_family = AF_INET6;
  765. printf(" inet6 addr: %s/%d",
  766. INET6_sprint((struct sockaddr *) &sap, 1),
  767. plen);
  768. printf(" Scope:");
  769. switch (scope & IPV6_ADDR_SCOPE_MASK) {
  770. case 0:
  771. puts("Global");
  772. break;
  773. case IPV6_ADDR_LINKLOCAL:
  774. puts("Link");
  775. break;
  776. case IPV6_ADDR_SITELOCAL:
  777. puts("Site");
  778. break;
  779. case IPV6_ADDR_COMPATv4:
  780. puts("Compat");
  781. break;
  782. case IPV6_ADDR_LOOPBACK:
  783. puts("Host");
  784. break;
  785. default:
  786. puts("Unknown");
  787. }
  788. }
  789. }
  790. fclose(f);
  791. }
  792. #else
  793. #define ife_print6(a) ((void)0)
  794. #endif
  795. static void ife_print(struct interface *ptr)
  796. {
  797. const struct aftype *ap;
  798. const struct hwtype *hw;
  799. int hf;
  800. int can_compress = 0;
  801. ap = get_afntype(ptr->addr.sa_family);
  802. if (ap == NULL)
  803. ap = get_afntype(0);
  804. hf = ptr->type;
  805. if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
  806. can_compress = 1;
  807. hw = get_hwntype(hf);
  808. if (hw == NULL)
  809. hw = get_hwntype(-1);
  810. printf("%-9s Link encap:%s ", ptr->name, hw->title);
  811. /* For some hardware types (eg Ash, ATM) we don't print the
  812. hardware address if it's null. */
  813. if (hw->print != NULL
  814. && !(hw_null_address(hw, ptr->hwaddr) && hw->suppress_null_addr)
  815. ) {
  816. printf("HWaddr %s ", hw->print((unsigned char *)ptr->hwaddr));
  817. }
  818. #ifdef IFF_PORTSEL
  819. if (ptr->flags & IFF_PORTSEL) {
  820. printf("Media:%s", if_port_text[ptr->map.port] /* [0] */);
  821. if (ptr->flags & IFF_AUTOMEDIA)
  822. printf("(auto)");
  823. }
  824. #endif
  825. bb_putchar('\n');
  826. if (ptr->has_ip) {
  827. printf(" %s addr:%s ", ap->name,
  828. ap->sprint(&ptr->addr, 1));
  829. if (ptr->flags & IFF_POINTOPOINT) {
  830. printf(" P-t-P:%s ", ap->sprint(&ptr->dstaddr, 1));
  831. }
  832. if (ptr->flags & IFF_BROADCAST) {
  833. printf(" Bcast:%s ", ap->sprint(&ptr->broadaddr, 1));
  834. }
  835. printf(" Mask:%s\n", ap->sprint(&ptr->netmask, 1));
  836. }
  837. ife_print6(ptr);
  838. printf(" ");
  839. /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
  840. if (ptr->flags == 0) {
  841. printf("[NO FLAGS] ");
  842. } else {
  843. static const char ife_print_flags_strs[] ALIGN1 =
  844. "UP\0"
  845. "BROADCAST\0"
  846. "DEBUG\0"
  847. "LOOPBACK\0"
  848. "POINTOPOINT\0"
  849. "NOTRAILERS\0"
  850. "RUNNING\0"
  851. "NOARP\0"
  852. "PROMISC\0"
  853. "ALLMULTI\0"
  854. "SLAVE\0"
  855. "MASTER\0"
  856. "MULTICAST\0"
  857. #ifdef HAVE_DYNAMIC
  858. "DYNAMIC\0"
  859. #endif
  860. ;
  861. static const unsigned short ife_print_flags_mask[] ALIGN2 = {
  862. IFF_UP,
  863. IFF_BROADCAST,
  864. IFF_DEBUG,
  865. IFF_LOOPBACK,
  866. IFF_POINTOPOINT,
  867. IFF_NOTRAILERS,
  868. IFF_RUNNING,
  869. IFF_NOARP,
  870. IFF_PROMISC,
  871. IFF_ALLMULTI,
  872. IFF_SLAVE,
  873. IFF_MASTER,
  874. IFF_MULTICAST
  875. #ifdef HAVE_DYNAMIC
  876. ,IFF_DYNAMIC
  877. #endif
  878. };
  879. const unsigned short *mask = ife_print_flags_mask;
  880. const char *str = ife_print_flags_strs;
  881. do {
  882. if (ptr->flags & *mask) {
  883. printf("%s ", str);
  884. }
  885. mask++;
  886. str += strlen(str) + 1;
  887. } while (*str);
  888. }
  889. /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
  890. printf(" MTU:%d Metric:%d", ptr->mtu, ptr->metric ? ptr->metric : 1);
  891. #ifdef SIOCSKEEPALIVE
  892. if (ptr->outfill || ptr->keepalive)
  893. printf(" Outfill:%d Keepalive:%d", ptr->outfill, ptr->keepalive);
  894. #endif
  895. bb_putchar('\n');
  896. /* If needed, display the interface statistics. */
  897. if (ptr->statistics_valid) {
  898. /* XXX: statistics are currently only printed for the primary address,
  899. * not for the aliases, although strictly speaking they're shared
  900. * by all addresses.
  901. */
  902. printf(" ");
  903. printf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n",
  904. ptr->stats.rx_packets, ptr->stats.rx_errors,
  905. ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
  906. ptr->stats.rx_frame_errors);
  907. if (can_compress)
  908. printf(" compressed:%lu\n",
  909. ptr->stats.rx_compressed);
  910. printf(" ");
  911. printf("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n",
  912. ptr->stats.tx_packets, ptr->stats.tx_errors,
  913. ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
  914. ptr->stats.tx_carrier_errors);
  915. printf(" collisions:%lu ", ptr->stats.collisions);
  916. if (can_compress)
  917. printf("compressed:%lu ", ptr->stats.tx_compressed);
  918. if (ptr->tx_queue_len != -1)
  919. printf("txqueuelen:%d ", ptr->tx_queue_len);
  920. printf("\n R");
  921. print_bytes_scaled(ptr->stats.rx_bytes, " T");
  922. print_bytes_scaled(ptr->stats.tx_bytes, "\n");
  923. }
  924. if (ptr->map.irq || ptr->map.mem_start
  925. || ptr->map.dma || ptr->map.base_addr
  926. ) {
  927. printf(" ");
  928. if (ptr->map.irq)
  929. printf("Interrupt:%d ", ptr->map.irq);
  930. if (ptr->map.base_addr >= 0x100) /* Only print devices using it for I/O maps */
  931. printf("Base address:0x%lx ",
  932. (unsigned long) ptr->map.base_addr);
  933. if (ptr->map.mem_start) {
  934. printf("Memory:%lx-%lx ", ptr->map.mem_start,
  935. ptr->map.mem_end);
  936. }
  937. if (ptr->map.dma)
  938. printf("DMA chan:%x ", ptr->map.dma);
  939. bb_putchar('\n');
  940. }
  941. bb_putchar('\n');
  942. }
  943. static int do_if_print(struct interface *ife) /*, int *opt_a)*/
  944. {
  945. int res;
  946. res = do_if_fetch(ife);
  947. if (res >= 0) {
  948. if ((ife->flags & IFF_UP) || interface_opt_a)
  949. ife_print(ife);
  950. }
  951. return res;
  952. }
  953. static struct interface *lookup_interface(char *name)
  954. {
  955. struct interface *ife = NULL;
  956. if (if_readlist_proc(name) < 0)
  957. return NULL;
  958. ife = add_interface(name);
  959. return ife;
  960. }
  961. #ifdef UNUSED
  962. static int for_all_interfaces(int (*doit) (struct interface *, void *),
  963. void *cookie)
  964. {
  965. struct interface *ife;
  966. if (!int_list && (if_readlist() < 0))
  967. return -1;
  968. for (ife = int_list; ife; ife = ife->next) {
  969. int err = doit(ife, cookie);
  970. if (err)
  971. return err;
  972. }
  973. return 0;
  974. }
  975. #endif
  976. /* for ipv4 add/del modes */
  977. static int if_print(char *ifname)
  978. {
  979. struct interface *ife;
  980. int res;
  981. if (!ifname) {
  982. /*res = for_all_interfaces(do_if_print, &interface_opt_a);*/
  983. if (!int_list && (if_readlist() < 0))
  984. return -1;
  985. for (ife = int_list; ife; ife = ife->next) {
  986. int err = do_if_print(ife); /*, &interface_opt_a);*/
  987. if (err)
  988. return err;
  989. }
  990. return 0;
  991. }
  992. ife = lookup_interface(ifname);
  993. res = do_if_fetch(ife);
  994. if (res >= 0)
  995. ife_print(ife);
  996. return res;
  997. }
  998. #if ENABLE_FEATURE_HWIB
  999. /* Input an Infiniband address and convert to binary. */
  1000. int FAST_FUNC in_ib(const char *bufp, struct sockaddr *sap)
  1001. {
  1002. sap->sa_family = ib_hwtype.type;
  1003. //TODO: error check?
  1004. hex2bin((char*)sap->sa_data, bufp, INFINIBAND_ALEN);
  1005. # ifdef HWIB_DEBUG
  1006. fprintf(stderr, "in_ib(%s): %s\n", bufp, UNSPEC_print(sap->sa_data));
  1007. # endif
  1008. return 0;
  1009. }
  1010. #endif
  1011. int FAST_FUNC display_interfaces(char *ifname)
  1012. {
  1013. int status;
  1014. status = if_print(ifname);
  1015. return (status < 0); /* status < 0 == 1 -- error */
  1016. }