3
0

interface.c 27 KB

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