interface.c 27 KB

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