interface.c 29 KB

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