interface.c 29 KB

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