3
0

interface.c 29 KB

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