interface.c 30 KB

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