interface.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  1. /*
  2. * stolen from net-tools-1.59 and stripped down for busybox by
  3. * Erik Andersen <andersen@codepoet.org>
  4. *
  5. * Heavily modified by Manuel Novoa III Mar 12, 2001
  6. *
  7. * Pruned unused code using KEEP_UNUSED define.
  8. * Added print_bytes_scaled function to reduce code size.
  9. * Added some (potentially) missing defines.
  10. * Improved display support for -a and for a named interface.
  11. *
  12. * -----------------------------------------------------------
  13. *
  14. * ifconfig This file contains an implementation of the command
  15. * that either displays or sets the characteristics of
  16. * one or more of the system's networking interfaces.
  17. *
  18. * Version: $Id: interface.c,v 1.25 2004/08/26 21:45:21 andersen Exp $
  19. *
  20. * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  21. * and others. Copyright 1993 MicroWalt Corporation
  22. *
  23. * This program is free software; you can redistribute it
  24. * and/or modify it under the terms of the GNU General
  25. * Public License as published by the Free Software
  26. * Foundation; either version 2 of the License, or (at
  27. * your option) any later version.
  28. *
  29. * Patched to support 'add' and 'del' keywords for INET(4) addresses
  30. * by Mrs. Brisby <mrs.brisby@nimh.org>
  31. *
  32. * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  33. * - gettext instead of catgets for i18n
  34. * 10/1998 - Andi Kleen. Use interface list primitives.
  35. * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu
  36. * (default AF was wrong)
  37. */
  38. /* #define KEEP_UNUSED */
  39. /*
  40. *
  41. * Protocol Families.
  42. *
  43. */
  44. #define HAVE_AFINET 1
  45. #undef HAVE_AFIPX
  46. #undef HAVE_AFATALK
  47. #undef HAVE_AFNETROM
  48. #undef HAVE_AFX25
  49. #undef HAVE_AFECONET
  50. #undef HAVE_AFASH
  51. /*
  52. *
  53. * Device Hardware types.
  54. *
  55. */
  56. #define HAVE_HWETHER 1
  57. #define HAVE_HWPPP 1
  58. #undef HAVE_HWSLIP
  59. #include "inet_common.h"
  60. #include <stdio.h>
  61. #include <errno.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64. #include <unistd.h>
  65. #include <fcntl.h>
  66. #include <ctype.h>
  67. #include <sys/ioctl.h>
  68. #include <sys/types.h>
  69. #include <net/if.h>
  70. #include <net/if_arp.h>
  71. #include "libbb.h"
  72. #ifdef CONFIG_FEATURE_IPV6
  73. # define HAVE_AFINET6 1
  74. #else
  75. # undef HAVE_AFINET6
  76. #endif
  77. #define _(x) x
  78. #define _PATH_PROCNET_DEV "/proc/net/dev"
  79. #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
  80. #define new(p) ((p) = xcalloc(1,sizeof(*(p))))
  81. #define KRELEASE(maj,min,patch) ((maj) * 65536 + (min)*256 + (patch))
  82. #ifdef HAVE_HWSLIP
  83. #include <net/if_slip.h>
  84. #endif
  85. #if HAVE_AFINET6
  86. #ifndef _LINUX_IN6_H
  87. /*
  88. * This is in linux/include/net/ipv6.h.
  89. */
  90. struct in6_ifreq {
  91. struct in6_addr ifr6_addr;
  92. uint32_t ifr6_prefixlen;
  93. unsigned int ifr6_ifindex;
  94. };
  95. #endif
  96. #endif /* HAVE_AFINET6 */
  97. #if HAVE_AFIPX
  98. #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
  99. #include <netipx/ipx.h>
  100. #else
  101. #include "ipx.h"
  102. #endif
  103. #endif
  104. /* Defines for glibc2.0 users. */
  105. #ifndef SIOCSIFTXQLEN
  106. #define SIOCSIFTXQLEN 0x8943
  107. #define SIOCGIFTXQLEN 0x8942
  108. #endif
  109. /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
  110. #ifndef ifr_qlen
  111. #define ifr_qlen ifr_ifru.ifru_mtu
  112. #endif
  113. #ifndef HAVE_TXQUEUELEN
  114. #define HAVE_TXQUEUELEN 1
  115. #endif
  116. #ifndef IFF_DYNAMIC
  117. #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
  118. #endif
  119. /* This structure defines protocol families and their handlers. */
  120. struct aftype {
  121. const char *name;
  122. const char *title;
  123. int af;
  124. int alen;
  125. char *(*print) (unsigned char *);
  126. char *(*sprint) (struct sockaddr *, int numeric);
  127. int (*input) (int type, char *bufp, struct sockaddr *);
  128. void (*herror) (char *text);
  129. int (*rprint) (int options);
  130. int (*rinput) (int typ, int ext, char **argv);
  131. /* may modify src */
  132. int (*getmask) (char *src, struct sockaddr * mask, char *name);
  133. int fd;
  134. char *flag_file;
  135. };
  136. #ifdef KEEP_UNUSED
  137. static int flag_unx;
  138. #ifdef HAVE_AFIPX
  139. static int flag_ipx;
  140. #endif
  141. #ifdef HAVE_AFX25
  142. static int flag_ax25;
  143. #endif
  144. #ifdef HAVE_AFATALK
  145. static int flag_ddp;
  146. #endif
  147. #ifdef HAVE_AFNETROM
  148. static int flag_netrom;
  149. #endif
  150. static int flag_inet;
  151. #ifdef HAVE_AFINET6
  152. static int flag_inet6;
  153. #endif
  154. #ifdef HAVE_AFECONET
  155. static int flag_econet;
  156. #endif
  157. #ifdef HAVE_AFX25
  158. static int flag_x25 = 0;
  159. #endif
  160. #ifdef HAVE_AFASH
  161. static int flag_ash;
  162. #endif
  163. static struct aftrans_t {
  164. char *alias;
  165. char *name;
  166. int *flag;
  167. } aftrans[] = {
  168. #ifdef HAVE_AFX25
  169. {
  170. "ax25", "ax25", &flag_ax25},
  171. #endif
  172. {
  173. "ip", "inet", &flag_inet},
  174. #ifdef HAVE_AFINET6
  175. {
  176. "ip6", "inet6", &flag_inet6},
  177. #endif
  178. #ifdef HAVE_AFIPX
  179. {
  180. "ipx", "ipx", &flag_ipx},
  181. #endif
  182. #ifdef HAVE_AFATALK
  183. {
  184. "appletalk", "ddp", &flag_ddp},
  185. #endif
  186. #ifdef HAVE_AFNETROM
  187. {
  188. "netrom", "netrom", &flag_netrom},
  189. #endif
  190. {
  191. "inet", "inet", &flag_inet},
  192. #ifdef HAVE_AFINET6
  193. {
  194. "inet6", "inet6", &flag_inet6},
  195. #endif
  196. #ifdef HAVE_AFATALK
  197. {
  198. "ddp", "ddp", &flag_ddp},
  199. #endif
  200. {
  201. "unix", "unix", &flag_unx}, {
  202. "tcpip", "inet", &flag_inet},
  203. #ifdef HAVE_AFECONET
  204. {
  205. "econet", "ec", &flag_econet},
  206. #endif
  207. #ifdef HAVE_AFX25
  208. {
  209. "x25", "x25", &flag_x25},
  210. #endif
  211. #ifdef HAVE_AFASH
  212. {
  213. "ash", "ash", &flag_ash},
  214. #endif
  215. {
  216. 0, 0, 0}
  217. };
  218. static char afname[256] = "";
  219. #endif /* KEEP_UNUSED */
  220. #if HAVE_AFUNIX
  221. /* Display a UNIX domain address. */
  222. static char *UNIX_print(unsigned char *ptr)
  223. {
  224. return (ptr);
  225. }
  226. /* Display a UNIX domain address. */
  227. static char *UNIX_sprint(struct sockaddr *sap, int numeric)
  228. {
  229. static char buf[64];
  230. if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
  231. return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
  232. return (UNIX_print(sap->sa_data));
  233. }
  234. static struct aftype unix_aftype = {
  235. "unix", "UNIX Domain", AF_UNIX, 0,
  236. UNIX_print, UNIX_sprint, NULL, NULL,
  237. NULL, NULL, NULL,
  238. -1,
  239. "/proc/net/unix"
  240. };
  241. #endif /* HAVE_AFUNIX */
  242. #if HAVE_AFINET
  243. #ifdef KEEP_UNUSED
  244. static void INET_reserror(char *text)
  245. {
  246. herror(text);
  247. }
  248. /* Display an Internet socket address. */
  249. static char *INET_print(unsigned char *ptr)
  250. {
  251. return (inet_ntoa((*(struct in_addr *) ptr)));
  252. }
  253. #endif /* KEEP_UNUSED */
  254. /* Display an Internet socket address. */
  255. static char *INET_sprint(struct sockaddr *sap, int numeric)
  256. {
  257. static char buff[128];
  258. if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
  259. return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
  260. if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
  261. numeric, 0xffffff00) != 0)
  262. return (NULL);
  263. return (buff);
  264. }
  265. #ifdef KEEP_UNUSED
  266. static char *INET_sprintmask(struct sockaddr *sap, int numeric,
  267. unsigned int netmask)
  268. {
  269. static char buff[128];
  270. if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
  271. return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
  272. if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
  273. numeric, netmask) != 0)
  274. return (NULL);
  275. return (buff);
  276. }
  277. static int INET_getsock(char *bufp, struct sockaddr *sap)
  278. {
  279. char *sp = bufp, *bp;
  280. unsigned int i;
  281. unsigned val;
  282. struct sockaddr_in *sin;
  283. sin = (struct sockaddr_in *) sap;
  284. sin->sin_family = AF_INET;
  285. sin->sin_port = 0;
  286. val = 0;
  287. bp = (char *) &val;
  288. for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) {
  289. *sp = toupper(*sp);
  290. if ((*sp >= 'A') && (*sp <= 'F'))
  291. bp[i] |= (int) (*sp - 'A') + 10;
  292. else if ((*sp >= '0') && (*sp <= '9'))
  293. bp[i] |= (int) (*sp - '0');
  294. else
  295. return (-1);
  296. bp[i] <<= 4;
  297. sp++;
  298. *sp = toupper(*sp);
  299. if ((*sp >= 'A') && (*sp <= 'F'))
  300. bp[i] |= (int) (*sp - 'A') + 10;
  301. else if ((*sp >= '0') && (*sp <= '9'))
  302. bp[i] |= (int) (*sp - '0');
  303. else
  304. return (-1);
  305. sp++;
  306. }
  307. sin->sin_addr.s_addr = htonl(val);
  308. return (sp - bufp);
  309. }
  310. static int INET_input(int type, char *bufp, struct sockaddr *sap)
  311. {
  312. switch (type) {
  313. case 1:
  314. return (INET_getsock(bufp, sap));
  315. case 256:
  316. return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1));
  317. default:
  318. return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
  319. }
  320. }
  321. static int INET_getnetmask(char *adr, struct sockaddr *m, char *name)
  322. {
  323. struct sockaddr_in *mask = (struct sockaddr_in *) m;
  324. char *slash, *end;
  325. int prefix;
  326. if ((slash = strchr(adr, '/')) == NULL)
  327. return 0;
  328. *slash++ = '\0';
  329. prefix = strtoul(slash, &end, 0);
  330. if (*end != '\0')
  331. return -1;
  332. if (name) {
  333. sprintf(name, "/%d", prefix);
  334. }
  335. mask->sin_family = AF_INET;
  336. mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix));
  337. return 1;
  338. }
  339. #endif /* KEEP_UNUSED */
  340. static struct aftype inet_aftype = {
  341. "inet", "DARPA Internet", AF_INET, sizeof(unsigned long),
  342. NULL /* UNUSED INET_print */ , INET_sprint,
  343. NULL /* UNUSED INET_input */ , NULL /* UNUSED INET_reserror */ ,
  344. NULL /*INET_rprint */ , NULL /*INET_rinput */ ,
  345. NULL /* UNUSED INET_getnetmask */ ,
  346. -1,
  347. NULL
  348. };
  349. #endif /* HAVE_AFINET */
  350. #if HAVE_AFINET6
  351. #ifdef KEEP_UNUSED
  352. static void INET6_reserror(char *text)
  353. {
  354. herror(text);
  355. }
  356. /* Display an Internet socket address. */
  357. static char *INET6_print(unsigned char *ptr)
  358. {
  359. static char name[80];
  360. inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80);
  361. return name;
  362. }
  363. #endif /* KEEP_UNUSED */
  364. /* Display an Internet socket address. */
  365. /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
  366. static char *INET6_sprint(struct sockaddr *sap, int numeric)
  367. {
  368. static char buff[128];
  369. if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
  370. return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff));
  371. if (INET6_rresolve
  372. (buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric) != 0)
  373. return safe_strncpy(buff, _("[UNKNOWN]"), sizeof(buff));
  374. return (buff);
  375. }
  376. #ifdef KEEP_UNUSED
  377. static int INET6_getsock(char *bufp, struct sockaddr *sap)
  378. {
  379. struct sockaddr_in6 *sin6;
  380. sin6 = (struct sockaddr_in6 *) sap;
  381. sin6->sin6_family = AF_INET6;
  382. sin6->sin6_port = 0;
  383. if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0)
  384. return (-1);
  385. return 16; /* ?;) */
  386. }
  387. static int INET6_input(int type, char *bufp, struct sockaddr *sap)
  388. {
  389. switch (type) {
  390. case 1:
  391. return (INET6_getsock(bufp, sap));
  392. default:
  393. return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
  394. }
  395. }
  396. #endif /* KEEP_UNUSED */
  397. static struct aftype inet6_aftype = {
  398. "inet6", "IPv6", AF_INET6, sizeof(struct in6_addr),
  399. NULL /* UNUSED INET6_print */ , INET6_sprint,
  400. NULL /* UNUSED INET6_input */ , NULL /* UNUSED INET6_reserror */ ,
  401. NULL /*INET6_rprint */ , NULL /*INET6_rinput */ ,
  402. NULL /* UNUSED INET6_getnetmask */ ,
  403. -1,
  404. NULL
  405. };
  406. #endif /* HAVE_AFINET6 */
  407. /* Display an UNSPEC address. */
  408. static char *UNSPEC_print(unsigned char *ptr)
  409. {
  410. static char buff[sizeof(struct sockaddr) * 3 + 1];
  411. char *pos;
  412. unsigned int i;
  413. pos = buff;
  414. for (i = 0; i < sizeof(struct sockaddr); i++) {
  415. /* careful -- not every libc's sprintf returns # bytes written */
  416. sprintf(pos, "%02X-", (*ptr++ & 0377));
  417. pos += 3;
  418. }
  419. /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */
  420. *--pos = '\0';
  421. return (buff);
  422. }
  423. /* Display an UNSPEC socket address. */
  424. static char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
  425. {
  426. static char buf[64];
  427. if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
  428. return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf));
  429. return (UNSPEC_print(sap->sa_data));
  430. }
  431. static struct aftype unspec_aftype = {
  432. "unspec", "UNSPEC", AF_UNSPEC, 0,
  433. UNSPEC_print, UNSPEC_sprint, NULL, NULL,
  434. NULL,
  435. };
  436. static struct aftype *aftypes[] = {
  437. #if HAVE_AFUNIX
  438. &unix_aftype,
  439. #endif
  440. #if HAVE_AFINET
  441. &inet_aftype,
  442. #endif
  443. #if HAVE_AFINET6
  444. &inet6_aftype,
  445. #endif
  446. #if HAVE_AFAX25
  447. &ax25_aftype,
  448. #endif
  449. #if HAVE_AFNETROM
  450. &netrom_aftype,
  451. #endif
  452. #if HAVE_AFROSE
  453. &rose_aftype,
  454. #endif
  455. #if HAVE_AFIPX
  456. &ipx_aftype,
  457. #endif
  458. #if HAVE_AFATALK
  459. &ddp_aftype,
  460. #endif
  461. #if HAVE_AFECONET
  462. &ec_aftype,
  463. #endif
  464. #if HAVE_AFASH
  465. &ash_aftype,
  466. #endif
  467. #if HAVE_AFX25
  468. &x25_aftype,
  469. #endif
  470. &unspec_aftype,
  471. NULL
  472. };
  473. #ifdef KEEP_UNUSED
  474. static short sVafinit = 0;
  475. static void afinit()
  476. {
  477. unspec_aftype.title = _("UNSPEC");
  478. #if HAVE_AFUNIX
  479. unix_aftype.title = _("UNIX Domain");
  480. #endif
  481. #if HAVE_AFINET
  482. inet_aftype.title = _("DARPA Internet");
  483. #endif
  484. #if HAVE_AFINET6
  485. inet6_aftype.title = _("IPv6");
  486. #endif
  487. #if HAVE_AFAX25
  488. ax25_aftype.title = _("AMPR AX.25");
  489. #endif
  490. #if HAVE_AFNETROM
  491. netrom_aftype.title = _("AMPR NET/ROM");
  492. #endif
  493. #if HAVE_AFIPX
  494. ipx_aftype.title = _("Novell IPX");
  495. #endif
  496. #if HAVE_AFATALK
  497. ddp_aftype.title = _("Appletalk DDP");
  498. #endif
  499. #if HAVE_AFECONET
  500. ec_aftype.title = _("Econet");
  501. #endif
  502. #if HAVE_AFX25
  503. x25_aftype.title = _("CCITT X.25");
  504. #endif
  505. #if HAVE_AFROSE
  506. rose_aftype.title = _("AMPR ROSE");
  507. #endif
  508. #if HAVE_AFASH
  509. ash_aftype.title = _("Ash");
  510. #endif
  511. sVafinit = 1;
  512. }
  513. static int aftrans_opt(const char *arg)
  514. {
  515. struct aftrans_t *paft;
  516. char *tmp1, *tmp2;
  517. char buf[256];
  518. safe_strncpy(buf, arg, sizeof(buf));
  519. tmp1 = buf;
  520. while (tmp1) {
  521. tmp2 = strchr(tmp1, ',');
  522. if (tmp2)
  523. *(tmp2++) = '\0';
  524. paft = aftrans;
  525. for (paft = aftrans; paft->alias; paft++) {
  526. if (strcmp(tmp1, paft->alias))
  527. continue;
  528. if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
  529. bb_error_msg(_("Too many address family arguments."));
  530. return (0);
  531. }
  532. if (paft->flag)
  533. (*paft->flag)++;
  534. if (afname[0])
  535. strcat(afname, ",");
  536. strcat(afname, paft->name);
  537. break;
  538. }
  539. if (!paft->alias) {
  540. bb_error_msg(_("Unknown address family `%s'."), tmp1);
  541. return (1);
  542. }
  543. tmp1 = tmp2;
  544. }
  545. return (0);
  546. }
  547. /* set the default AF list from the program name or a constant value */
  548. static void aftrans_def(char *tool, char *argv0, char *dflt)
  549. {
  550. char *tmp;
  551. char *buf;
  552. strcpy(afname, dflt);
  553. if (!(tmp = strrchr(argv0, '/')))
  554. tmp = argv0; /* no slash?! */
  555. else
  556. tmp++;
  557. if (!(buf = strdup(tmp)))
  558. return;
  559. if (strlen(tool) >= strlen(tmp)) {
  560. free(buf);
  561. return;
  562. }
  563. tmp = buf + (strlen(tmp) - strlen(tool));
  564. if (strcmp(tmp, tool) != 0) {
  565. free(buf);
  566. return;
  567. }
  568. *tmp = '\0';
  569. if ((tmp = strchr(buf, '_')))
  570. *tmp = '\0';
  571. afname[0] = '\0';
  572. if (aftrans_opt(buf))
  573. strcpy(afname, buf);
  574. free(buf);
  575. }
  576. /* Check our protocol family table for this family. */
  577. static struct aftype *get_aftype(const char *name)
  578. {
  579. struct aftype **afp;
  580. #ifdef KEEP_UNUSED
  581. if (!sVafinit)
  582. afinit();
  583. #endif /* KEEP_UNUSED */
  584. afp = aftypes;
  585. while (*afp != NULL) {
  586. if (!strcmp((*afp)->name, name))
  587. return (*afp);
  588. afp++;
  589. }
  590. if (strchr(name, ','))
  591. bb_error_msg(_("Please don't supply more than one address family."));
  592. return (NULL);
  593. }
  594. #endif /* KEEP_UNUSED */
  595. /* Check our protocol family table for this family. */
  596. static struct aftype *get_afntype(int af)
  597. {
  598. struct aftype **afp;
  599. #ifdef KEEP_UNUSED
  600. if (!sVafinit)
  601. afinit();
  602. #endif /* KEEP_UNUSED */
  603. afp = aftypes;
  604. while (*afp != NULL) {
  605. if ((*afp)->af == af)
  606. return (*afp);
  607. afp++;
  608. }
  609. return (NULL);
  610. }
  611. /* Check our protocol family table for this family and return its socket */
  612. static int get_socket_for_af(int af)
  613. {
  614. struct aftype **afp;
  615. #ifdef KEEP_UNUSED
  616. if (!sVafinit)
  617. afinit();
  618. #endif /* KEEP_UNUSED */
  619. afp = aftypes;
  620. while (*afp != NULL) {
  621. if ((*afp)->af == af)
  622. return (*afp)->fd;
  623. afp++;
  624. }
  625. return -1;
  626. }
  627. #ifdef KEEP_UNUSED
  628. /* type: 0=all, 1=getroute */
  629. static void print_aflist(int type)
  630. {
  631. int count = 0;
  632. char *txt;
  633. struct aftype **afp;
  634. #ifdef KEEP_UNUSED
  635. if (!sVafinit)
  636. afinit();
  637. #endif /* KEEP_UNUSED */
  638. afp = aftypes;
  639. while (*afp != NULL) {
  640. if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) {
  641. afp++;
  642. continue;
  643. }
  644. if ((count % 3) == 0)
  645. fprintf(stderr, count ? "\n " : " ");
  646. txt = (*afp)->name;
  647. if (!txt)
  648. txt = "..";
  649. fprintf(stderr, "%s (%s) ", txt, _((*afp)->title));
  650. count++;
  651. afp++;
  652. }
  653. fprintf(stderr, "\n");
  654. }
  655. #endif /* KEEP_UNUSED */
  656. struct user_net_device_stats {
  657. unsigned long long rx_packets; /* total packets received */
  658. unsigned long long tx_packets; /* total packets transmitted */
  659. unsigned long long rx_bytes; /* total bytes received */
  660. unsigned long long tx_bytes; /* total bytes transmitted */
  661. unsigned long rx_errors; /* bad packets received */
  662. unsigned long tx_errors; /* packet transmit problems */
  663. unsigned long rx_dropped; /* no space in linux buffers */
  664. unsigned long tx_dropped; /* no space available in linux */
  665. unsigned long rx_multicast; /* multicast packets received */
  666. unsigned long rx_compressed;
  667. unsigned long tx_compressed;
  668. unsigned long collisions;
  669. /* detailed rx_errors: */
  670. unsigned long rx_length_errors;
  671. unsigned long rx_over_errors; /* receiver ring buff overflow */
  672. unsigned long rx_crc_errors; /* recved pkt with crc error */
  673. unsigned long rx_frame_errors; /* recv'd frame alignment error */
  674. unsigned long rx_fifo_errors; /* recv'r fifo overrun */
  675. unsigned long rx_missed_errors; /* receiver missed packet */
  676. /* detailed tx_errors */
  677. unsigned long tx_aborted_errors;
  678. unsigned long tx_carrier_errors;
  679. unsigned long tx_fifo_errors;
  680. unsigned long tx_heartbeat_errors;
  681. unsigned long tx_window_errors;
  682. };
  683. struct interface {
  684. struct interface *next, *prev;
  685. char name[IFNAMSIZ]; /* interface name */
  686. short type; /* if type */
  687. short flags; /* various flags */
  688. int metric; /* routing metric */
  689. int mtu; /* MTU value */
  690. int tx_queue_len; /* transmit queue length */
  691. struct ifmap map; /* hardware setup */
  692. struct sockaddr addr; /* IP address */
  693. struct sockaddr dstaddr; /* P-P IP address */
  694. struct sockaddr broadaddr; /* IP broadcast address */
  695. struct sockaddr netmask; /* IP network mask */
  696. struct sockaddr ipxaddr_bb; /* IPX network address */
  697. struct sockaddr ipxaddr_sn; /* IPX network address */
  698. struct sockaddr ipxaddr_e3; /* IPX network address */
  699. struct sockaddr ipxaddr_e2; /* IPX network address */
  700. struct sockaddr ddpaddr; /* Appletalk DDP address */
  701. struct sockaddr ecaddr; /* Econet address */
  702. int has_ip;
  703. int has_ipx_bb;
  704. int has_ipx_sn;
  705. int has_ipx_e3;
  706. int has_ipx_e2;
  707. int has_ax25;
  708. int has_ddp;
  709. int has_econet;
  710. char hwaddr[32]; /* HW address */
  711. int statistics_valid;
  712. struct user_net_device_stats stats; /* statistics */
  713. int keepalive; /* keepalive value for SLIP */
  714. int outfill; /* outfill value for SLIP */
  715. };
  716. int interface_opt_a = 0; /* show all interfaces */
  717. #ifdef KEEP_UNUSED
  718. static int opt_i = 0; /* show the statistics */
  719. static int opt_v = 0; /* debugging output flag */
  720. static int addr_family = 0; /* currently selected AF */
  721. #endif /* KEEP_UNUSED */
  722. static struct interface *int_list, *int_last;
  723. static int skfd = -1; /* generic raw socket desc. */
  724. static int sockets_open(int family)
  725. {
  726. struct aftype **aft;
  727. int sfd = -1;
  728. static int force = -1;
  729. if (force < 0) {
  730. force = 0;
  731. if (get_kernel_revision() < KRELEASE(2, 1, 0))
  732. force = 1;
  733. if (access("/proc/net", R_OK))
  734. force = 1;
  735. }
  736. for (aft = aftypes; *aft; aft++) {
  737. struct aftype *af = *aft;
  738. int type = SOCK_DGRAM;
  739. if (af->af == AF_UNSPEC)
  740. continue;
  741. if (family && family != af->af)
  742. continue;
  743. if (af->fd != -1) {
  744. sfd = af->fd;
  745. continue;
  746. }
  747. /* Check some /proc file first to not stress kmod */
  748. if (!family && !force && af->flag_file) {
  749. if (access(af->flag_file, R_OK))
  750. continue;
  751. }
  752. #if HAVE_AFNETROM
  753. if (af->af == AF_NETROM)
  754. type = SOCK_SEQPACKET;
  755. #endif
  756. #if HAVE_AFX25
  757. if (af->af == AF_X25)
  758. type = SOCK_SEQPACKET;
  759. #endif
  760. af->fd = socket(af->af, type, 0);
  761. if (af->fd >= 0)
  762. sfd = af->fd;
  763. }
  764. if (sfd < 0) {
  765. bb_error_msg(_("No usable address families found."));
  766. }
  767. return sfd;
  768. }
  769. #ifdef CONFIG_FEATURE_CLEAN_UP
  770. static void sockets_close(void)
  771. {
  772. struct aftype **aft;
  773. for (aft = aftypes; *aft != NULL; aft++) {
  774. struct aftype *af = *aft;
  775. if( af->fd != -1 ) {
  776. close(af->fd);
  777. af->fd = -1;
  778. }
  779. }
  780. }
  781. #endif
  782. /* like strcmp(), but knows about numbers */
  783. static int nstrcmp(const char *a, const char *b)
  784. {
  785. const char *a_ptr = a;
  786. const char *b_ptr = b;
  787. while (*a == *b) {
  788. if (*a == '\0') {
  789. return 0;
  790. }
  791. if (!isdigit(*a) && isdigit(*(a+1))) {
  792. a_ptr = a+1;
  793. b_ptr = b+1;
  794. }
  795. a++;
  796. b++;
  797. }
  798. if (isdigit(*a) && isdigit(*b)) {
  799. return atoi(a_ptr) > atoi(b_ptr) ? 1 : -1;
  800. }
  801. return *a - *b;
  802. }
  803. static struct interface *add_interface(char *name)
  804. {
  805. struct interface *ife, **nextp, *new;
  806. for (ife = int_last; ife; ife = ife->prev) {
  807. int n = nstrcmp(ife->name, name);
  808. if (n == 0)
  809. return ife;
  810. if (n < 0)
  811. break;
  812. }
  813. new(new);
  814. safe_strncpy(new->name, name, IFNAMSIZ);
  815. nextp = ife ? &ife->next : &int_list;
  816. new->prev = ife;
  817. new->next = *nextp;
  818. if (new->next)
  819. new->next->prev = new;
  820. else
  821. int_last = new;
  822. *nextp = new;
  823. return new;
  824. }
  825. static int if_readconf(void)
  826. {
  827. int numreqs = 30;
  828. struct ifconf ifc;
  829. struct ifreq *ifr;
  830. int n, err = -1;
  831. int skfd2;
  832. /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets
  833. (as of 2.1.128) */
  834. skfd2 = get_socket_for_af(AF_INET);
  835. if (skfd2 < 0) {
  836. bb_perror_msg(("warning: no inet socket available"));
  837. /* Try to soldier on with whatever socket we can get hold of. */
  838. skfd2 = sockets_open(0);
  839. if (skfd2 < 0)
  840. return -1;
  841. }
  842. ifc.ifc_buf = NULL;
  843. for (;;) {
  844. ifc.ifc_len = sizeof(struct ifreq) * numreqs;
  845. ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len);
  846. if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) {
  847. perror("SIOCGIFCONF");
  848. goto out;
  849. }
  850. if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) {
  851. /* assume it overflowed and try again */
  852. numreqs += 10;
  853. continue;
  854. }
  855. break;
  856. }
  857. ifr = ifc.ifc_req;
  858. for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) {
  859. add_interface(ifr->ifr_name);
  860. ifr++;
  861. }
  862. err = 0;
  863. out:
  864. free(ifc.ifc_buf);
  865. return err;
  866. }
  867. char *get_name(char *name, char *p)
  868. {
  869. /* Extract <name>[:<alias>] from nul-terminated p where p matches
  870. <name>[:<alias>]: after leading whitespace.
  871. If match is not made, set name empty and return unchanged p */
  872. int namestart=0, nameend=0, aliasend;
  873. while (isspace(p[namestart]))
  874. namestart++;
  875. nameend=namestart;
  876. while (p[nameend] && p[nameend]!=':' && !isspace(p[nameend]))
  877. nameend++;
  878. if (p[nameend]==':') {
  879. aliasend=nameend+1;
  880. while (p[aliasend] && isdigit(p[aliasend]))
  881. aliasend++;
  882. if (p[aliasend]==':') {
  883. nameend=aliasend;
  884. }
  885. if ((nameend-namestart)<IFNAMSIZ) {
  886. memcpy(name,&p[namestart],nameend-namestart);
  887. name[nameend-namestart]='\0';
  888. p=&p[nameend];
  889. } else {
  890. /* Interface name too large */
  891. name[0]='\0';
  892. }
  893. } else {
  894. /* first ':' not found - return empty */
  895. name[0]='\0';
  896. }
  897. return p + 1;
  898. }
  899. /* If scanf supports size qualifiers for %n conversions, then we can
  900. * use a modified fmt that simply stores the position in the fields
  901. * having no associated fields in the proc string. Of course, we need
  902. * to zero them again when we're done. But that is smaller than the
  903. * old approach of multiple scanf occurrences with large numbers of
  904. * args. */
  905. /* static const char *ss_fmt[] = { */
  906. /* "%Ln%Lu%lu%lu%lu%lu%ln%ln%Ln%Lu%lu%lu%lu%lu%lu", */
  907. /* "%Lu%Lu%lu%lu%lu%lu%ln%ln%Lu%Lu%lu%lu%lu%lu%lu", */
  908. /* "%Lu%Lu%lu%lu%lu%lu%lu%lu%Lu%Lu%lu%lu%lu%lu%lu%lu" */
  909. /* }; */
  910. /* Lie about the size of the int pointed to for %n. */
  911. #if INT_MAX == LONG_MAX
  912. static const char *ss_fmt[] = {
  913. "%n%Lu%u%u%u%u%n%n%n%Lu%u%u%u%u%u",
  914. "%Lu%Lu%u%u%u%u%n%n%Lu%Lu%u%u%u%u%u",
  915. "%Lu%Lu%u%u%u%u%u%u%Lu%Lu%u%u%u%u%u%u"
  916. };
  917. #else
  918. static const char *ss_fmt[] = {
  919. "%n%Lu%lu%lu%lu%lu%n%n%n%Lu%lu%lu%lu%lu%lu",
  920. "%Lu%Lu%lu%lu%lu%lu%n%n%Lu%Lu%lu%lu%lu%lu%lu",
  921. "%Lu%Lu%lu%lu%lu%lu%lu%lu%Lu%Lu%lu%lu%lu%lu%lu%lu"
  922. };
  923. #endif
  924. static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn)
  925. {
  926. memset(&ife->stats, 0, sizeof(struct user_net_device_stats));
  927. sscanf(bp, ss_fmt[procnetdev_vsn],
  928. &ife->stats.rx_bytes, /* missing for 0 */
  929. &ife->stats.rx_packets,
  930. &ife->stats.rx_errors,
  931. &ife->stats.rx_dropped,
  932. &ife->stats.rx_fifo_errors,
  933. &ife->stats.rx_frame_errors,
  934. &ife->stats.rx_compressed, /* missing for <= 1 */
  935. &ife->stats.rx_multicast, /* missing for <= 1 */
  936. &ife->stats.tx_bytes, /* missing for 0 */
  937. &ife->stats.tx_packets,
  938. &ife->stats.tx_errors,
  939. &ife->stats.tx_dropped,
  940. &ife->stats.tx_fifo_errors,
  941. &ife->stats.collisions,
  942. &ife->stats.tx_carrier_errors,
  943. &ife->stats.tx_compressed /* missing for <= 1 */
  944. );
  945. if (procnetdev_vsn <= 1) {
  946. if (procnetdev_vsn == 0) {
  947. ife->stats.rx_bytes = 0;
  948. ife->stats.tx_bytes = 0;
  949. }
  950. ife->stats.rx_multicast = 0;
  951. ife->stats.rx_compressed = 0;
  952. ife->stats.tx_compressed = 0;
  953. }
  954. }
  955. static inline int procnetdev_version(char *buf)
  956. {
  957. if (strstr(buf, "compressed"))
  958. return 2;
  959. if (strstr(buf, "bytes"))
  960. return 1;
  961. return 0;
  962. }
  963. static int if_readlist_proc(char *target)
  964. {
  965. static int proc_read;
  966. FILE *fh;
  967. char buf[512];
  968. struct interface *ife;
  969. int err, procnetdev_vsn;
  970. if (proc_read)
  971. return 0;
  972. if (!target)
  973. proc_read = 1;
  974. fh = fopen(_PATH_PROCNET_DEV, "r");
  975. if (!fh) {
  976. bb_perror_msg(_("Warning: cannot open %s. Limited output."), _PATH_PROCNET_DEV);
  977. return if_readconf();
  978. }
  979. fgets(buf, sizeof buf, fh); /* eat line */
  980. fgets(buf, sizeof buf, fh);
  981. procnetdev_vsn = procnetdev_version(buf);
  982. err = 0;
  983. while (fgets(buf, sizeof buf, fh)) {
  984. char *s, name[128];
  985. s = get_name(name, buf);
  986. ife = add_interface(name);
  987. get_dev_fields(s, ife, procnetdev_vsn);
  988. ife->statistics_valid = 1;
  989. if (target && !strcmp(target, name))
  990. break;
  991. }
  992. if (ferror(fh)) {
  993. perror(_PATH_PROCNET_DEV);
  994. err = -1;
  995. proc_read = 0;
  996. }
  997. fclose(fh);
  998. return err;
  999. }
  1000. static int if_readlist(void)
  1001. {
  1002. int err = if_readlist_proc(NULL);
  1003. if (!err)
  1004. err = if_readconf();
  1005. return err;
  1006. }
  1007. static int for_all_interfaces(int (*doit) (struct interface *, void *),
  1008. void *cookie)
  1009. {
  1010. struct interface *ife;
  1011. if (!int_list && (if_readlist() < 0))
  1012. return -1;
  1013. for (ife = int_list; ife; ife = ife->next) {
  1014. int err = doit(ife, cookie);
  1015. if (err)
  1016. return err;
  1017. }
  1018. return 0;
  1019. }
  1020. /* Support for fetching an IPX address */
  1021. #if HAVE_AFIPX
  1022. static int ipx_getaddr(int sock, int ft, struct ifreq *ifr)
  1023. {
  1024. ((struct sockaddr_ipx *) &ifr->ifr_addr)->sipx_type = ft;
  1025. return ioctl(sock, SIOCGIFADDR, ifr);
  1026. }
  1027. #endif
  1028. /* Fetch the interface configuration from the kernel. */
  1029. static int if_fetch(struct interface *ife)
  1030. {
  1031. struct ifreq ifr;
  1032. int fd;
  1033. char *ifname = ife->name;
  1034. strcpy(ifr.ifr_name, ifname);
  1035. if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0)
  1036. return (-1);
  1037. ife->flags = ifr.ifr_flags;
  1038. strcpy(ifr.ifr_name, ifname);
  1039. if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0)
  1040. memset(ife->hwaddr, 0, 32);
  1041. else
  1042. memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8);
  1043. ife->type = ifr.ifr_hwaddr.sa_family;
  1044. strcpy(ifr.ifr_name, ifname);
  1045. if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0)
  1046. ife->metric = 0;
  1047. else
  1048. ife->metric = ifr.ifr_metric;
  1049. strcpy(ifr.ifr_name, ifname);
  1050. if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0)
  1051. ife->mtu = 0;
  1052. else
  1053. ife->mtu = ifr.ifr_mtu;
  1054. #ifdef HAVE_HWSLIP
  1055. if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP ||
  1056. ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 ||
  1057. ife->type == ARPHRD_ADAPT) {
  1058. #ifdef SIOCGOUTFILL
  1059. strcpy(ifr.ifr_name, ifname);
  1060. if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0)
  1061. ife->outfill = 0;
  1062. else
  1063. ife->outfill = (unsigned int) ifr.ifr_data;
  1064. #endif
  1065. #ifdef SIOCGKEEPALIVE
  1066. strcpy(ifr.ifr_name, ifname);
  1067. if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0)
  1068. ife->keepalive = 0;
  1069. else
  1070. ife->keepalive = (unsigned int) ifr.ifr_data;
  1071. #endif
  1072. }
  1073. #endif
  1074. #ifdef SIOCGIFMAP
  1075. strcpy(ifr.ifr_name, ifname);
  1076. if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0)
  1077. ife->map = ifr.ifr_map;
  1078. else
  1079. #endif
  1080. memset(&ife->map, 0, sizeof(struct ifmap));
  1081. #ifdef HAVE_TXQUEUELEN
  1082. strcpy(ifr.ifr_name, ifname);
  1083. if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0)
  1084. ife->tx_queue_len = -1; /* unknown value */
  1085. else
  1086. ife->tx_queue_len = ifr.ifr_qlen;
  1087. #else
  1088. ife->tx_queue_len = -1; /* unknown value */
  1089. #endif
  1090. #if HAVE_AFINET
  1091. /* IPv4 address? */
  1092. fd = get_socket_for_af(AF_INET);
  1093. if (fd >= 0) {
  1094. strcpy(ifr.ifr_name, ifname);
  1095. ifr.ifr_addr.sa_family = AF_INET;
  1096. if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
  1097. ife->has_ip = 1;
  1098. ife->addr = ifr.ifr_addr;
  1099. strcpy(ifr.ifr_name, ifname);
  1100. if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0)
  1101. memset(&ife->dstaddr, 0, sizeof(struct sockaddr));
  1102. else
  1103. ife->dstaddr = ifr.ifr_dstaddr;
  1104. strcpy(ifr.ifr_name, ifname);
  1105. if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0)
  1106. memset(&ife->broadaddr, 0, sizeof(struct sockaddr));
  1107. else
  1108. ife->broadaddr = ifr.ifr_broadaddr;
  1109. strcpy(ifr.ifr_name, ifname);
  1110. if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0)
  1111. memset(&ife->netmask, 0, sizeof(struct sockaddr));
  1112. else
  1113. ife->netmask = ifr.ifr_netmask;
  1114. } else
  1115. memset(&ife->addr, 0, sizeof(struct sockaddr));
  1116. }
  1117. #endif
  1118. #if HAVE_AFATALK
  1119. /* DDP address maybe ? */
  1120. fd = get_socket_for_af(AF_APPLETALK);
  1121. if (fd >= 0) {
  1122. strcpy(ifr.ifr_name, ifname);
  1123. if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
  1124. ife->ddpaddr = ifr.ifr_addr;
  1125. ife->has_ddp = 1;
  1126. }
  1127. }
  1128. #endif
  1129. #if HAVE_AFIPX
  1130. /* Look for IPX addresses with all framing types */
  1131. fd = get_socket_for_af(AF_IPX);
  1132. if (fd >= 0) {
  1133. strcpy(ifr.ifr_name, ifname);
  1134. if (!ipx_getaddr(fd, IPX_FRAME_ETHERII, &ifr)) {
  1135. ife->has_ipx_bb = 1;
  1136. ife->ipxaddr_bb = ifr.ifr_addr;
  1137. }
  1138. strcpy(ifr.ifr_name, ifname);
  1139. if (!ipx_getaddr(fd, IPX_FRAME_SNAP, &ifr)) {
  1140. ife->has_ipx_sn = 1;
  1141. ife->ipxaddr_sn = ifr.ifr_addr;
  1142. }
  1143. strcpy(ifr.ifr_name, ifname);
  1144. if (!ipx_getaddr(fd, IPX_FRAME_8023, &ifr)) {
  1145. ife->has_ipx_e3 = 1;
  1146. ife->ipxaddr_e3 = ifr.ifr_addr;
  1147. }
  1148. strcpy(ifr.ifr_name, ifname);
  1149. if (!ipx_getaddr(fd, IPX_FRAME_8022, &ifr)) {
  1150. ife->has_ipx_e2 = 1;
  1151. ife->ipxaddr_e2 = ifr.ifr_addr;
  1152. }
  1153. }
  1154. #endif
  1155. #if HAVE_AFECONET
  1156. /* Econet address maybe? */
  1157. fd = get_socket_for_af(AF_ECONET);
  1158. if (fd >= 0) {
  1159. strcpy(ifr.ifr_name, ifname);
  1160. if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) {
  1161. ife->ecaddr = ifr.ifr_addr;
  1162. ife->has_econet = 1;
  1163. }
  1164. }
  1165. #endif
  1166. return 0;
  1167. }
  1168. static int do_if_fetch(struct interface *ife)
  1169. {
  1170. if (if_fetch(ife) < 0) {
  1171. char *errmsg;
  1172. if (errno == ENODEV) {
  1173. /* Give better error message for this case. */
  1174. errmsg = _("Device not found");
  1175. } else {
  1176. errmsg = strerror(errno);
  1177. }
  1178. bb_error_msg(_("%s: error fetching interface information: %s\n"),
  1179. ife->name, errmsg);
  1180. return -1;
  1181. }
  1182. return 0;
  1183. }
  1184. /* This structure defines hardware protocols and their handlers. */
  1185. struct hwtype {
  1186. const char *name;
  1187. const char *title;
  1188. int type;
  1189. int alen;
  1190. char *(*print) (unsigned char *);
  1191. int (*input) (char *, struct sockaddr *);
  1192. int (*activate) (int fd);
  1193. int suppress_null_addr;
  1194. };
  1195. static struct hwtype unspec_hwtype = {
  1196. "unspec", "UNSPEC", -1, 0,
  1197. UNSPEC_print, NULL, NULL
  1198. };
  1199. static struct hwtype loop_hwtype = {
  1200. "loop", "Local Loopback", ARPHRD_LOOPBACK, 0,
  1201. NULL, NULL, NULL
  1202. };
  1203. #if HAVE_HWETHER
  1204. #include <net/if_arp.h>
  1205. #if (__GLIBC__ >=2 && __GLIBC_MINOR >= 1) || defined(_NEWLIB_VERSION)
  1206. #include <net/ethernet.h>
  1207. #else
  1208. #include <linux/if_ether.h>
  1209. #endif
  1210. /* Display an Ethernet address in readable format. */
  1211. static char *pr_ether(unsigned char *ptr)
  1212. {
  1213. static char buff[64];
  1214. snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X",
  1215. (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377),
  1216. (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377)
  1217. );
  1218. return (buff);
  1219. }
  1220. #ifdef KEEP_UNUSED
  1221. /* Input an Ethernet address and convert to binary. */
  1222. static int in_ether(char *bufp, struct sockaddr *sap)
  1223. {
  1224. unsigned char *ptr;
  1225. char c, *orig;
  1226. int i;
  1227. unsigned val;
  1228. sap->sa_family = ether_hwtype.type;
  1229. ptr = sap->sa_data;
  1230. i = 0;
  1231. orig = bufp;
  1232. while ((*bufp != '\0') && (i < ETH_ALEN)) {
  1233. val = 0;
  1234. c = *bufp++;
  1235. if (isdigit(c))
  1236. val = c - '0';
  1237. else if (c >= 'a' && c <= 'f')
  1238. val = c - 'a' + 10;
  1239. else if (c >= 'A' && c <= 'F')
  1240. val = c - 'A' + 10;
  1241. else {
  1242. #ifdef DEBUG
  1243. bb_error_msg(_("in_ether(%s): invalid ether address!\n"), orig);
  1244. #endif
  1245. errno = EINVAL;
  1246. return (-1);
  1247. }
  1248. val <<= 4;
  1249. c = *bufp;
  1250. if (isdigit(c))
  1251. val |= c - '0';
  1252. else if (c >= 'a' && c <= 'f')
  1253. val |= c - 'a' + 10;
  1254. else if (c >= 'A' && c <= 'F')
  1255. val |= c - 'A' + 10;
  1256. else if (c == ':' || c == 0)
  1257. val >>= 4;
  1258. else {
  1259. #ifdef DEBUG
  1260. bb_error_msg(_("in_ether(%s): invalid ether address!"), orig);
  1261. #endif
  1262. errno = EINVAL;
  1263. return (-1);
  1264. }
  1265. if (c != 0)
  1266. bufp++;
  1267. *ptr++ = (unsigned char) (val & 0377);
  1268. i++;
  1269. /* We might get a semicolon here - not required. */
  1270. if (*bufp == ':') {
  1271. #ifdef DEBUG
  1272. if (i == ETH_ALEN) {
  1273. bb_error_msg(_("in_ether(%s): trailing : ignored!"), orig);
  1274. }
  1275. #endif
  1276. bufp++;
  1277. }
  1278. }
  1279. #ifdef DEBUG
  1280. /* That's it. Any trailing junk? */
  1281. if ((i == ETH_ALEN) && (*bufp != '\0')) {
  1282. bb_error_msg(_("in_ether(%s): trailing junk!"), orig);
  1283. errno = EINVAL;
  1284. return (-1);
  1285. }
  1286. bb_error_msg("in_ether(%s): %s", orig, pr_ether(sap->sa_data));
  1287. #endif
  1288. return (0);
  1289. }
  1290. #endif /* KEEP_UNUSED */
  1291. static struct hwtype ether_hwtype = {
  1292. "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN,
  1293. pr_ether, NULL /* UNUSED in_ether */ , NULL
  1294. };
  1295. #endif /* HAVE_HWETHER */
  1296. #if HAVE_HWPPP
  1297. #include <net/if_arp.h>
  1298. #ifdef KEEP_UNUSED
  1299. /* Start the PPP encapsulation on the file descriptor. */
  1300. static int do_ppp(int fd)
  1301. {
  1302. bb_error_msg(_("You cannot start PPP with this program."));
  1303. return -1;
  1304. }
  1305. #endif /* KEEP_UNUSED */
  1306. static struct hwtype ppp_hwtype = {
  1307. "ppp", "Point-Point Protocol", ARPHRD_PPP, 0,
  1308. NULL, NULL, NULL /* UNUSED do_ppp */ , 0
  1309. };
  1310. #endif /* HAVE_PPP */
  1311. static struct hwtype *hwtypes[] = {
  1312. &loop_hwtype,
  1313. #if HAVE_HWSLIP
  1314. &slip_hwtype,
  1315. &cslip_hwtype,
  1316. &slip6_hwtype,
  1317. &cslip6_hwtype,
  1318. &adaptive_hwtype,
  1319. #endif
  1320. #if HAVE_HWSTRIP
  1321. &strip_hwtype,
  1322. #endif
  1323. #if HAVE_HWASH
  1324. &ash_hwtype,
  1325. #endif
  1326. #if HAVE_HWETHER
  1327. &ether_hwtype,
  1328. #endif
  1329. #if HAVE_HWTR
  1330. &tr_hwtype,
  1331. #ifdef ARPHRD_IEEE802_TR
  1332. &tr_hwtype1,
  1333. #endif
  1334. #endif
  1335. #if HAVE_HWAX25
  1336. &ax25_hwtype,
  1337. #endif
  1338. #if HAVE_HWNETROM
  1339. &netrom_hwtype,
  1340. #endif
  1341. #if HAVE_HWROSE
  1342. &rose_hwtype,
  1343. #endif
  1344. #if HAVE_HWTUNNEL
  1345. &tunnel_hwtype,
  1346. #endif
  1347. #if HAVE_HWPPP
  1348. &ppp_hwtype,
  1349. #endif
  1350. #if HAVE_HWHDLCLAPB
  1351. &hdlc_hwtype,
  1352. &lapb_hwtype,
  1353. #endif
  1354. #if HAVE_HWARC
  1355. &arcnet_hwtype,
  1356. #endif
  1357. #if HAVE_HWFR
  1358. &dlci_hwtype,
  1359. &frad_hwtype,
  1360. #endif
  1361. #if HAVE_HWSIT
  1362. &sit_hwtype,
  1363. #endif
  1364. #if HAVE_HWFDDI
  1365. &fddi_hwtype,
  1366. #endif
  1367. #if HAVE_HWHIPPI
  1368. &hippi_hwtype,
  1369. #endif
  1370. #if HAVE_HWIRDA
  1371. &irda_hwtype,
  1372. #endif
  1373. #if HAVE_HWEC
  1374. &ec_hwtype,
  1375. #endif
  1376. #if HAVE_HWX25
  1377. &x25_hwtype,
  1378. #endif
  1379. &unspec_hwtype,
  1380. NULL
  1381. };
  1382. #ifdef KEEP_UNUSED
  1383. static short sVhwinit = 0;
  1384. static void hwinit()
  1385. {
  1386. loop_hwtype.title = _("Local Loopback");
  1387. unspec_hwtype.title = _("UNSPEC");
  1388. #if HAVE_HWSLIP
  1389. slip_hwtype.title = _("Serial Line IP");
  1390. cslip_hwtype.title = _("VJ Serial Line IP");
  1391. slip6_hwtype.title = _("6-bit Serial Line IP");
  1392. cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
  1393. adaptive_hwtype.title = _("Adaptive Serial Line IP");
  1394. #endif
  1395. #if HAVE_HWETHER
  1396. ether_hwtype.title = _("Ethernet");
  1397. #endif
  1398. #if HAVE_HWASH
  1399. ash_hwtype.title = _("Ash");
  1400. #endif
  1401. #if HAVE_HWFDDI
  1402. fddi_hwtype.title = _("Fiber Distributed Data Interface");
  1403. #endif
  1404. #if HAVE_HWHIPPI
  1405. hippi_hwtype.title = _("HIPPI");
  1406. #endif
  1407. #if HAVE_HWAX25
  1408. ax25_hwtype.title = _("AMPR AX.25");
  1409. #endif
  1410. #if HAVE_HWROSE
  1411. rose_hwtype.title = _("AMPR ROSE");
  1412. #endif
  1413. #if HAVE_HWNETROM
  1414. netrom_hwtype.title = _("AMPR NET/ROM");
  1415. #endif
  1416. #if HAVE_HWX25
  1417. x25_hwtype.title = _("generic X.25");
  1418. #endif
  1419. #if HAVE_HWTUNNEL
  1420. tunnel_hwtype.title = _("IPIP Tunnel");
  1421. #endif
  1422. #if HAVE_HWPPP
  1423. ppp_hwtype.title = _("Point-to-Point Protocol");
  1424. #endif
  1425. #if HAVE_HWHDLCLAPB
  1426. hdlc_hwtype.title = _("(Cisco)-HDLC");
  1427. lapb_hwtype.title = _("LAPB");
  1428. #endif
  1429. #if HAVE_HWARC
  1430. arcnet_hwtype.title = _("ARCnet");
  1431. #endif
  1432. #if HAVE_HWFR
  1433. dlci_hwtype.title = _("Frame Relay DLCI");
  1434. frad_hwtype.title = _("Frame Relay Access Device");
  1435. #endif
  1436. #if HAVE_HWSIT
  1437. sit_hwtype.title = _("IPv6-in-IPv4");
  1438. #endif
  1439. #if HAVE_HWIRDA
  1440. irda_hwtype.title = _("IrLAP");
  1441. #endif
  1442. #if HAVE_HWTR
  1443. tr_hwtype.title = _("16/4 Mbps Token Ring");
  1444. #ifdef ARPHRD_IEEE802_TR
  1445. tr_hwtype1.title = _("16/4 Mbps Token Ring (New)");
  1446. #endif
  1447. #endif
  1448. #if HAVE_HWEC
  1449. ec_hwtype.title = _("Econet");
  1450. #endif
  1451. sVhwinit = 1;
  1452. }
  1453. #endif /* KEEP_UNUSED */
  1454. #ifdef IFF_PORTSEL
  1455. #if 0
  1456. static const char * const if_port_text[][4] = {
  1457. /* Keep in step with <linux/netdevice.h> */
  1458. {"unknown", NULL, NULL, NULL},
  1459. {"10base2", "bnc", "coax", NULL},
  1460. {"10baseT", "utp", "tpe", NULL},
  1461. {"AUI", "thick", "db15", NULL},
  1462. {"100baseT", NULL, NULL, NULL},
  1463. {"100baseTX", NULL, NULL, NULL},
  1464. {"100baseFX", NULL, NULL, NULL},
  1465. {NULL, NULL, NULL, NULL},
  1466. };
  1467. #else
  1468. static const char * const if_port_text[] = {
  1469. /* Keep in step with <linux/netdevice.h> */
  1470. "unknown",
  1471. "10base2",
  1472. "10baseT",
  1473. "AUI",
  1474. "100baseT",
  1475. "100baseTX",
  1476. "100baseFX",
  1477. NULL
  1478. };
  1479. #endif
  1480. #endif
  1481. /* Check our hardware type table for this type. */
  1482. static struct hwtype *get_hwntype(int type)
  1483. {
  1484. struct hwtype **hwp;
  1485. #ifdef KEEP_UNUSED
  1486. if (!sVhwinit)
  1487. hwinit();
  1488. #endif /* KEEP_UNUSED */
  1489. hwp = hwtypes;
  1490. while (*hwp != NULL) {
  1491. if ((*hwp)->type == type)
  1492. return (*hwp);
  1493. hwp++;
  1494. }
  1495. return (NULL);
  1496. }
  1497. /* return 1 if address is all zeros */
  1498. static int hw_null_address(struct hwtype *hw, void *ap)
  1499. {
  1500. unsigned int i;
  1501. unsigned char *address = (unsigned char *) ap;
  1502. for (i = 0; i < hw->alen; i++)
  1503. if (address[i])
  1504. return 0;
  1505. return 1;
  1506. }
  1507. static const char TRext[] = "\0\0\0Ki\0Mi\0Gi\0Ti";
  1508. static void print_bytes_scaled(unsigned long long ull, const char *end)
  1509. {
  1510. unsigned long long int_part;
  1511. const char *ext;
  1512. unsigned int frac_part;
  1513. int i;
  1514. frac_part = 0;
  1515. ext = TRext;
  1516. int_part = ull;
  1517. i = 4;
  1518. do {
  1519. #if 0
  1520. /* This does correct rounding and is a little larger. But it
  1521. * uses KiB as the smallest displayed unit. */
  1522. if ((int_part < (1024*1024 - 51)) || !--i) {
  1523. i = 0;
  1524. int_part += 51; /* 1024*.05 = 51.2 */
  1525. frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
  1526. }
  1527. int_part /= 1024;
  1528. ext += 3; /* KiB, MiB, GiB, TiB */
  1529. #else
  1530. if (int_part >= 1024) {
  1531. frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024;
  1532. int_part /= 1024;
  1533. ext += 3; /* KiB, MiB, GiB, TiB */
  1534. }
  1535. --i;
  1536. #endif
  1537. } while (i);
  1538. printf("X bytes:%Lu (%Lu.%u %sB)%s", ull, int_part, frac_part, ext, end);
  1539. }
  1540. static const char * const ife_print_flags_strs[] = {
  1541. "UP ",
  1542. "BROADCAST ",
  1543. "DEBUG ",
  1544. "LOOPBACK ",
  1545. "POINTOPOINT ",
  1546. "NOTRAILERS ",
  1547. "RUNNING ",
  1548. "NOARP ",
  1549. "PROMISC ",
  1550. "ALLMULTI ",
  1551. "SLAVE ",
  1552. "MASTER ",
  1553. "MULTICAST ",
  1554. #ifdef HAVE_DYNAMIC
  1555. "DYNAMIC "
  1556. #endif
  1557. };
  1558. static const unsigned short ife_print_flags_mask[] = {
  1559. IFF_UP,
  1560. IFF_BROADCAST,
  1561. IFF_DEBUG,
  1562. IFF_LOOPBACK,
  1563. IFF_POINTOPOINT,
  1564. IFF_NOTRAILERS,
  1565. IFF_RUNNING,
  1566. IFF_NOARP,
  1567. IFF_PROMISC,
  1568. IFF_ALLMULTI,
  1569. IFF_SLAVE,
  1570. IFF_MASTER,
  1571. IFF_MULTICAST,
  1572. #ifdef HAVE_DYNAMIC
  1573. IFF_DYNAMIC
  1574. #endif
  1575. 0
  1576. };
  1577. static void ife_print(struct interface *ptr)
  1578. {
  1579. struct aftype *ap;
  1580. struct hwtype *hw;
  1581. int hf;
  1582. int can_compress = 0;
  1583. #if HAVE_AFIPX
  1584. static struct aftype *ipxtype = NULL;
  1585. #endif
  1586. #if HAVE_AFECONET
  1587. static struct aftype *ectype = NULL;
  1588. #endif
  1589. #if HAVE_AFATALK
  1590. static struct aftype *ddptype = NULL;
  1591. #endif
  1592. #if HAVE_AFINET6
  1593. FILE *f;
  1594. char addr6[40], devname[20];
  1595. struct sockaddr_in6 sap;
  1596. int plen, scope, dad_status, if_idx;
  1597. char addr6p[8][5];
  1598. #endif
  1599. ap = get_afntype(ptr->addr.sa_family);
  1600. if (ap == NULL)
  1601. ap = get_afntype(0);
  1602. hf = ptr->type;
  1603. if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6)
  1604. can_compress = 1;
  1605. hw = get_hwntype(hf);
  1606. if (hw == NULL)
  1607. hw = get_hwntype(-1);
  1608. printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title));
  1609. /* For some hardware types (eg Ash, ATM) we don't print the
  1610. hardware address if it's null. */
  1611. if (hw->print != NULL && (!(hw_null_address(hw, ptr->hwaddr) &&
  1612. hw->suppress_null_addr)))
  1613. printf(_("HWaddr %s "), hw->print(ptr->hwaddr));
  1614. #ifdef IFF_PORTSEL
  1615. if (ptr->flags & IFF_PORTSEL) {
  1616. printf(_("Media:%s"), if_port_text[ptr->map.port] /* [0] */);
  1617. if (ptr->flags & IFF_AUTOMEDIA)
  1618. printf(_("(auto)"));
  1619. }
  1620. #endif
  1621. printf("\n");
  1622. #if HAVE_AFINET
  1623. if (ptr->has_ip) {
  1624. printf(_(" %s addr:%s "), ap->name,
  1625. ap->sprint(&ptr->addr, 1));
  1626. if (ptr->flags & IFF_POINTOPOINT) {
  1627. printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1));
  1628. }
  1629. if (ptr->flags & IFF_BROADCAST) {
  1630. printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1));
  1631. }
  1632. printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1));
  1633. }
  1634. #endif
  1635. #if HAVE_AFINET6
  1636. #define IPV6_ADDR_ANY 0x0000U
  1637. #define IPV6_ADDR_UNICAST 0x0001U
  1638. #define IPV6_ADDR_MULTICAST 0x0002U
  1639. #define IPV6_ADDR_ANYCAST 0x0004U
  1640. #define IPV6_ADDR_LOOPBACK 0x0010U
  1641. #define IPV6_ADDR_LINKLOCAL 0x0020U
  1642. #define IPV6_ADDR_SITELOCAL 0x0040U
  1643. #define IPV6_ADDR_COMPATv4 0x0080U
  1644. #define IPV6_ADDR_SCOPE_MASK 0x00f0U
  1645. #define IPV6_ADDR_MAPPED 0x1000U
  1646. #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
  1647. if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) {
  1648. while (fscanf
  1649. (f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n",
  1650. addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4],
  1651. addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope,
  1652. &dad_status, devname) != EOF) {
  1653. if (!strcmp(devname, ptr->name)) {
  1654. sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s",
  1655. addr6p[0], addr6p[1], addr6p[2], addr6p[3],
  1656. addr6p[4], addr6p[5], addr6p[6], addr6p[7]);
  1657. inet_pton(AF_INET6, addr6,
  1658. (struct sockaddr *) &sap.sin6_addr);
  1659. sap.sin6_family = AF_INET6;
  1660. printf(_(" inet6 addr: %s/%d"),
  1661. inet6_aftype.sprint((struct sockaddr *) &sap, 1),
  1662. plen);
  1663. printf(_(" Scope:"));
  1664. switch (scope & IPV6_ADDR_SCOPE_MASK) {
  1665. case 0:
  1666. printf(_("Global"));
  1667. break;
  1668. case IPV6_ADDR_LINKLOCAL:
  1669. printf(_("Link"));
  1670. break;
  1671. case IPV6_ADDR_SITELOCAL:
  1672. printf(_("Site"));
  1673. break;
  1674. case IPV6_ADDR_COMPATv4:
  1675. printf(_("Compat"));
  1676. break;
  1677. case IPV6_ADDR_LOOPBACK:
  1678. printf(_("Host"));
  1679. break;
  1680. default:
  1681. printf(_("Unknown"));
  1682. }
  1683. printf("\n");
  1684. }
  1685. }
  1686. fclose(f);
  1687. }
  1688. #endif
  1689. #if HAVE_AFIPX
  1690. if (ipxtype == NULL)
  1691. ipxtype = get_afntype(AF_IPX);
  1692. if (ipxtype != NULL) {
  1693. if (ptr->has_ipx_bb)
  1694. printf(_(" IPX/Ethernet II addr:%s\n"),
  1695. ipxtype->sprint(&ptr->ipxaddr_bb, 1));
  1696. if (ptr->has_ipx_sn)
  1697. printf(_(" IPX/Ethernet SNAP addr:%s\n"),
  1698. ipxtype->sprint(&ptr->ipxaddr_sn, 1));
  1699. if (ptr->has_ipx_e2)
  1700. printf(_(" IPX/Ethernet 802.2 addr:%s\n"),
  1701. ipxtype->sprint(&ptr->ipxaddr_e2, 1));
  1702. if (ptr->has_ipx_e3)
  1703. printf(_(" IPX/Ethernet 802.3 addr:%s\n"),
  1704. ipxtype->sprint(&ptr->ipxaddr_e3, 1));
  1705. }
  1706. #endif
  1707. #if HAVE_AFATALK
  1708. if (ddptype == NULL)
  1709. ddptype = get_afntype(AF_APPLETALK);
  1710. if (ddptype != NULL) {
  1711. if (ptr->has_ddp)
  1712. printf(_(" EtherTalk Phase 2 addr:%s\n"),
  1713. ddptype->sprint(&ptr->ddpaddr, 1));
  1714. }
  1715. #endif
  1716. #if HAVE_AFECONET
  1717. if (ectype == NULL)
  1718. ectype = get_afntype(AF_ECONET);
  1719. if (ectype != NULL) {
  1720. if (ptr->has_econet)
  1721. printf(_(" econet addr:%s\n"),
  1722. ectype->sprint(&ptr->ecaddr, 1));
  1723. }
  1724. #endif
  1725. printf(" ");
  1726. /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */
  1727. if (ptr->flags == 0) {
  1728. printf(_("[NO FLAGS] "));
  1729. } else {
  1730. int i = 0;
  1731. do {
  1732. if (ptr->flags & ife_print_flags_mask[i]) {
  1733. printf(_(ife_print_flags_strs[i]));
  1734. }
  1735. } while (ife_print_flags_mask[++i]);
  1736. }
  1737. /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */
  1738. printf(_(" MTU:%d Metric:%d"), ptr->mtu, ptr->metric ? ptr->metric : 1);
  1739. #ifdef SIOCSKEEPALIVE
  1740. if (ptr->outfill || ptr->keepalive)
  1741. printf(_(" Outfill:%d Keepalive:%d"), ptr->outfill, ptr->keepalive);
  1742. #endif
  1743. printf("\n");
  1744. /* If needed, display the interface statistics. */
  1745. if (ptr->statistics_valid) {
  1746. /* XXX: statistics are currently only printed for the primary address,
  1747. * not for the aliases, although strictly speaking they're shared
  1748. * by all addresses.
  1749. */
  1750. printf(" ");
  1751. printf(_("RX packets:%Lu errors:%lu dropped:%lu overruns:%lu frame:%lu\n"),
  1752. ptr->stats.rx_packets, ptr->stats.rx_errors,
  1753. ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors,
  1754. ptr->stats.rx_frame_errors);
  1755. if (can_compress)
  1756. printf(_(" compressed:%lu\n"),
  1757. ptr->stats.rx_compressed);
  1758. printf(" ");
  1759. printf(_("TX packets:%Lu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n"),
  1760. ptr->stats.tx_packets, ptr->stats.tx_errors,
  1761. ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors,
  1762. ptr->stats.tx_carrier_errors);
  1763. printf(_(" collisions:%lu "), ptr->stats.collisions);
  1764. if (can_compress)
  1765. printf(_("compressed:%lu "), ptr->stats.tx_compressed);
  1766. if (ptr->tx_queue_len != -1)
  1767. printf(_("txqueuelen:%d "), ptr->tx_queue_len);
  1768. printf("\n R");
  1769. print_bytes_scaled(ptr->stats.rx_bytes, " T");
  1770. print_bytes_scaled(ptr->stats.tx_bytes, "\n");
  1771. }
  1772. if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma ||
  1773. ptr->map.base_addr)) {
  1774. printf(" ");
  1775. if (ptr->map.irq)
  1776. printf(_("Interrupt:%d "), ptr->map.irq);
  1777. if (ptr->map.base_addr >= 0x100) /* Only print devices using it for
  1778. I/O maps */
  1779. printf(_("Base address:0x%lx "),
  1780. (unsigned long) ptr->map.base_addr);
  1781. if (ptr->map.mem_start) {
  1782. printf(_("Memory:%lx-%lx "), ptr->map.mem_start,
  1783. ptr->map.mem_end);
  1784. }
  1785. if (ptr->map.dma)
  1786. printf(_("DMA chan:%x "), ptr->map.dma);
  1787. printf("\n");
  1788. }
  1789. printf("\n");
  1790. }
  1791. static int do_if_print(struct interface *ife, void *cookie)
  1792. {
  1793. int *opt_a = (int *) cookie;
  1794. int res;
  1795. res = do_if_fetch(ife);
  1796. if (res >= 0) {
  1797. if ((ife->flags & IFF_UP) || *opt_a)
  1798. ife_print(ife);
  1799. }
  1800. return res;
  1801. }
  1802. static struct interface *lookup_interface(char *name)
  1803. {
  1804. struct interface *ife = NULL;
  1805. if (if_readlist_proc(name) < 0)
  1806. return NULL;
  1807. ife = add_interface(name);
  1808. return ife;
  1809. }
  1810. /* for ipv4 add/del modes */
  1811. static int if_print(char *ifname)
  1812. {
  1813. int res;
  1814. if (!ifname) {
  1815. res = for_all_interfaces(do_if_print, &interface_opt_a);
  1816. } else {
  1817. struct interface *ife;
  1818. ife = lookup_interface(ifname);
  1819. res = do_if_fetch(ife);
  1820. if (res >= 0)
  1821. ife_print(ife);
  1822. }
  1823. return res;
  1824. }
  1825. int display_interfaces(char *ifname)
  1826. {
  1827. int status;
  1828. /* Create a channel to the NET kernel. */
  1829. if ((skfd = sockets_open(0)) < 0) {
  1830. bb_perror_msg_and_die("socket");
  1831. }
  1832. /* Do we have to show the current setup? */
  1833. status = if_print(ifname);
  1834. #ifdef CONFIG_FEATURE_CLEAN_UP
  1835. sockets_close();
  1836. #endif
  1837. exit(status < 0);
  1838. }