ifconfig.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* ifconfig
  2. *
  3. * Similar to the standard Unix ifconfig, but with only the necessary
  4. * parts for AF_INET, and without any printing of if info (for now).
  5. *
  6. * Bjorn Wesen, Axis Communications AB
  7. *
  8. *
  9. * Authors of the original ifconfig was:
  10. * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  11. *
  12. * This program is free software; you can redistribute it
  13. * and/or modify it under the terms of the GNU General
  14. * Public License as published by the Free Software
  15. * Foundation; either version 2 of the License, or (at
  16. * your option) any later version.
  17. *
  18. * $Id: ifconfig.c,v 1.16 2003/11/14 03:04:56 andersen Exp $
  19. *
  20. */
  21. /*
  22. * Heavily modified by Manuel Novoa III Mar 6, 2001
  23. *
  24. * From initial port to busybox, removed most of the redundancy by
  25. * converting to a table-driven approach. Added several (optional)
  26. * args missing from initial port.
  27. *
  28. * Still missing: media, tunnel.
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h> // strcmp and friends
  33. #include <ctype.h> // isdigit and friends
  34. #include <stddef.h> /* offsetof */
  35. #include <sys/types.h>
  36. #include <sys/socket.h>
  37. #include <sys/ioctl.h>
  38. #include <netinet/in.h>
  39. #include <arpa/inet.h>
  40. #include <net/if.h>
  41. #include <net/if_arp.h>
  42. #include <linux/if_ether.h>
  43. #include "busybox.h"
  44. #ifdef BB_FEATURE_IFCONFIG_SLIP
  45. #include <linux/if_slip.h>
  46. #endif
  47. /* I don't know if this is needed for busybox or not. Anyone? */
  48. #define QUESTIONABLE_ALIAS_CASE
  49. /* Defines for glibc2.0 users. */
  50. #ifndef SIOCSIFTXQLEN
  51. #define SIOCSIFTXQLEN 0x8943
  52. #define SIOCGIFTXQLEN 0x8942
  53. #endif
  54. /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
  55. #ifndef ifr_qlen
  56. #define ifr_qlen ifr_ifru.ifru_mtu
  57. #endif
  58. #ifndef IFF_DYNAMIC
  59. #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
  60. #endif
  61. /*
  62. * Here are the bit masks for the "flags" member of struct options below.
  63. * N_ signifies no arg prefix; M_ signifies arg prefixed by '-'.
  64. * CLR clears the flag; SET sets the flag; ARG signifies (optional) arg.
  65. */
  66. #define N_CLR 0x01
  67. #define M_CLR 0x02
  68. #define N_SET 0x04
  69. #define M_SET 0x08
  70. #define N_ARG 0x10
  71. #define M_ARG 0x20
  72. #define M_MASK (M_CLR | M_SET | M_ARG)
  73. #define N_MASK (N_CLR | N_SET | N_ARG)
  74. #define SET_MASK (N_SET | M_SET)
  75. #define CLR_MASK (N_CLR | M_CLR)
  76. #define SET_CLR_MASK (SET_MASK | CLR_MASK)
  77. #define ARG_MASK (M_ARG | N_ARG)
  78. /*
  79. * Here are the bit masks for the "arg_flags" member of struct options below.
  80. */
  81. /*
  82. * cast type:
  83. * 00 int
  84. * 01 char *
  85. * 02 HOST_COPY in_ether
  86. * 03 HOST_COPY INET_resolve
  87. */
  88. #define A_CAST_TYPE 0x03
  89. /*
  90. * map type:
  91. * 00 not a map type (mem_start, io_addr, irq)
  92. * 04 memstart (unsigned long)
  93. * 08 io_addr (unsigned short)
  94. * 0C irq (unsigned char)
  95. */
  96. #define A_MAP_TYPE 0x0C
  97. #define A_ARG_REQ 0x10 /* Set if an arg is required. */
  98. #define A_NETMASK 0x20 /* Set if netmask (check for multiple sets). */
  99. #define A_SET_AFTER 0x40 /* Set a flag at the end. */
  100. #define A_COLON_CHK 0x80 /* Is this needed? See below. */
  101. /*
  102. * These defines are for dealing with the A_CAST_TYPE field.
  103. */
  104. #define A_CAST_CHAR_PTR 0x01
  105. #define A_CAST_RESOLVE 0x01
  106. #define A_CAST_HOST_COPY 0x02
  107. #define A_CAST_HOST_COPY_IN_ETHER A_CAST_HOST_COPY
  108. #define A_CAST_HOST_COPY_RESOLVE (A_CAST_HOST_COPY | A_CAST_RESOLVE)
  109. /*
  110. * These defines are for dealing with the A_MAP_TYPE field.
  111. */
  112. #define A_MAP_ULONG 0x04 /* memstart */
  113. #define A_MAP_USHORT 0x08 /* io_addr */
  114. #define A_MAP_UCHAR 0x0C /* irq */
  115. /*
  116. * Define the bit masks signifying which operations to perform for each arg.
  117. */
  118. #define ARG_METRIC (A_ARG_REQ /*| A_CAST_INT*/)
  119. #define ARG_MTU (A_ARG_REQ /*| A_CAST_INT*/)
  120. #define ARG_TXQUEUELEN (A_ARG_REQ /*| A_CAST_INT*/)
  121. #define ARG_MEM_START (A_ARG_REQ | A_MAP_ULONG)
  122. #define ARG_IO_ADDR (A_ARG_REQ | A_MAP_ULONG)
  123. #define ARG_IRQ (A_ARG_REQ | A_MAP_UCHAR)
  124. #define ARG_DSTADDR (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE)
  125. #define ARG_NETMASK (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_NETMASK)
  126. #define ARG_BROADCAST (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
  127. #define ARG_HW (A_ARG_REQ | A_CAST_HOST_COPY_IN_ETHER)
  128. #define ARG_POINTOPOINT (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
  129. #define ARG_KEEPALIVE (A_ARG_REQ | A_CAST_CHAR_PTR)
  130. #define ARG_OUTFILL (A_ARG_REQ | A_CAST_CHAR_PTR)
  131. #define ARG_HOSTNAME (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_COLON_CHK)
  132. /*
  133. * Set up the tables. Warning! They must have corresponding order!
  134. */
  135. struct arg1opt {
  136. const char *name;
  137. unsigned short selector;
  138. unsigned short ifr_offset;
  139. };
  140. struct options {
  141. const char *name;
  142. const unsigned char flags;
  143. const unsigned char arg_flags;
  144. const unsigned short selector;
  145. };
  146. #define ifreq_offsetof(x) offsetof(struct ifreq, x)
  147. static const struct arg1opt Arg1Opt[] = {
  148. {"SIOCSIFMETRIC", SIOCSIFMETRIC, ifreq_offsetof(ifr_metric)},
  149. {"SIOCSIFMTU", SIOCSIFMTU, ifreq_offsetof(ifr_mtu)},
  150. {"SIOCSIFTXQLEN", SIOCSIFTXQLEN, ifreq_offsetof(ifr_qlen)},
  151. {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
  152. {"SIOCSIFNETMASK", SIOCSIFNETMASK, ifreq_offsetof(ifr_netmask)},
  153. {"SIOCSIFBRDADDR", SIOCSIFBRDADDR, ifreq_offsetof(ifr_broadaddr)},
  154. #ifdef BB_FEATURE_IFCONFIG_HW
  155. {"SIOCSIFHWADDR", SIOCSIFHWADDR, ifreq_offsetof(ifr_hwaddr)},
  156. #endif
  157. {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
  158. #ifdef SIOCSKEEPALIVE
  159. {"SIOCSKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data)},
  160. #endif
  161. #ifdef SIOCSOUTFILL
  162. {"SIOCSOUTFILL", SIOCSOUTFILL, ifreq_offsetof(ifr_data)},
  163. #endif
  164. #ifdef BB_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
  165. {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.mem_start)},
  166. {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.base_addr)},
  167. {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.irq)},
  168. #endif
  169. /* Last entry if for unmatched (possibly hostname) arg. */
  170. {"SIOCSIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr)},
  171. };
  172. static const struct options OptArray[] = {
  173. {"metric", N_ARG, ARG_METRIC, 0},
  174. {"mtu", N_ARG, ARG_MTU, 0},
  175. {"txqueuelen", N_ARG, ARG_TXQUEUELEN, 0},
  176. {"dstaddr", N_ARG, ARG_DSTADDR, 0},
  177. {"netmask", N_ARG, ARG_NETMASK, 0},
  178. {"broadcast", N_ARG | M_CLR, ARG_BROADCAST, IFF_BROADCAST},
  179. #ifdef BB_FEATURE_IFCONFIG_HW
  180. {"hw", N_ARG, ARG_HW, 0},
  181. #endif
  182. {"pointopoint", N_ARG | M_CLR, ARG_POINTOPOINT, IFF_POINTOPOINT},
  183. #ifdef SIOCSKEEPALIVE
  184. {"keepalive", N_ARG, ARG_KEEPALIVE, 0},
  185. #endif
  186. #ifdef SIOCSOUTFILL
  187. {"outfill", N_ARG, ARG_OUTFILL, 0},
  188. #endif
  189. #ifdef BB_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
  190. {"mem_start", N_ARG, ARG_MEM_START, 0},
  191. {"io_addr", N_ARG, ARG_IO_ADDR, 0},
  192. {"irq", N_ARG, ARG_IRQ, 0},
  193. #endif
  194. {"arp", N_CLR | M_SET, 0, IFF_NOARP},
  195. {"trailers", N_CLR | M_SET, 0, IFF_NOTRAILERS},
  196. {"promisc", N_SET | M_CLR, 0, IFF_PROMISC},
  197. {"multicast", N_SET | M_CLR, 0, IFF_MULTICAST},
  198. {"allmulti", N_SET | M_CLR, 0, IFF_ALLMULTI},
  199. {"dynamic", N_SET | M_CLR, 0, IFF_DYNAMIC},
  200. {"up", N_SET , 0, (IFF_UP | IFF_RUNNING)},
  201. {"down", N_CLR , 0, IFF_UP},
  202. { NULL, 0, ARG_HOSTNAME, (IFF_UP | IFF_RUNNING)}
  203. };
  204. /*
  205. * A couple of prototypes.
  206. */
  207. #ifdef BB_FEATURE_IFCONFIG_HW
  208. static int in_ether(char *bufp, struct sockaddr *sap);
  209. #endif
  210. #ifdef BB_FEATURE_IFCONFIG_STATUS
  211. extern int interface_opt_a;
  212. extern int display_interfaces(char *ifname);
  213. #endif
  214. /*
  215. * Our main function.
  216. */
  217. int ifconfig_main(int argc, char **argv)
  218. {
  219. struct ifreq ifr;
  220. struct sockaddr_in sai;
  221. #ifdef BB_FEATURE_IFCONFIG_HW
  222. struct sockaddr sa;
  223. #endif
  224. const struct arg1opt *a1op;
  225. const struct options *op;
  226. int sockfd; /* socket fd we use to manipulate stuff with */
  227. int goterr;
  228. int selector;
  229. char *p;
  230. char host[128];
  231. unsigned char mask;
  232. unsigned char did_flags;
  233. goterr = 0;
  234. did_flags = 0;
  235. /* skip argv[0] */
  236. ++argv;
  237. --argc;
  238. #ifdef BB_FEATURE_IFCONFIG_STATUS
  239. if ((argc > 0) && (strcmp(*argv,"-a") == 0)) {
  240. interface_opt_a = 1;
  241. --argc;
  242. ++argv;
  243. }
  244. #endif
  245. if(argc <= 1) {
  246. #ifdef BB_FEATURE_IFCONFIG_STATUS
  247. return display_interfaces(argc ? *argv : NULL);
  248. #else
  249. error_msg_and_die( "ifconfig was not compiled with interface status display support.");
  250. #endif
  251. }
  252. /* Create a channel to the NET kernel. */
  253. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  254. perror_msg_and_die("socket");
  255. }
  256. /* get interface name */
  257. safe_strncpy(ifr.ifr_name, *argv, IFNAMSIZ);
  258. /* Process the remaining arguments. */
  259. while (*++argv != (char *) NULL) {
  260. p = *argv;
  261. mask = N_MASK;
  262. if (*p == '-') { /* If the arg starts with '-'... */
  263. ++p; /* advance past it and */
  264. mask = M_MASK; /* set the appropriate mask. */
  265. }
  266. for (op = OptArray ; op->name ; op++) { /* Find table entry. */
  267. if (strcmp(p,op->name) == 0) { /* If name matches... */
  268. if ((mask &= op->flags)) { /* set the mask and go. */
  269. goto FOUND_ARG;;
  270. }
  271. /* If we get here, there was a valid arg with an */
  272. /* invalid '-' prefix. */
  273. ++goterr;
  274. goto LOOP;
  275. }
  276. }
  277. /* We fell through, so treat as possible hostname. */
  278. a1op = Arg1Opt + (sizeof(Arg1Opt) / sizeof(Arg1Opt[0])) - 1;
  279. mask = op->arg_flags;
  280. goto HOSTNAME;
  281. FOUND_ARG:
  282. if (mask & ARG_MASK) {
  283. mask = op->arg_flags;
  284. a1op = Arg1Opt + (op - OptArray);
  285. if (mask & A_NETMASK & did_flags) {
  286. show_usage();
  287. }
  288. if (*++argv == NULL) {
  289. if (mask & A_ARG_REQ) {
  290. show_usage();
  291. } else {
  292. --argv;
  293. mask &= A_SET_AFTER; /* just for broadcast */
  294. }
  295. } else { /* got an arg so process it */
  296. HOSTNAME:
  297. did_flags |= (mask & A_NETMASK);
  298. if (mask & A_CAST_HOST_COPY) {
  299. #ifdef BB_FEATURE_IFCONFIG_HW
  300. if (mask & A_CAST_RESOLVE) {
  301. #endif
  302. safe_strncpy(host, *argv, (sizeof host));
  303. sai.sin_family = AF_INET;
  304. sai.sin_port = 0;
  305. if (!strcmp(host, "default")) {
  306. /* Default is special, meaning 0.0.0.0. */
  307. sai.sin_addr.s_addr = INADDR_ANY;
  308. } else if (inet_aton(host, &sai.sin_addr) == 0) {
  309. /* It's not a dotted quad. */
  310. ++goterr;
  311. continue;
  312. }
  313. p = (char *) &sai;
  314. #ifdef BB_FEATURE_IFCONFIG_HW
  315. } else { /* A_CAST_HOST_COPY_IN_ETHER */
  316. /* This is the "hw" arg case. */
  317. if (strcmp("ether", *argv) || (*++argv == NULL)) {
  318. show_usage();
  319. }
  320. safe_strncpy(host, *argv, (sizeof host));
  321. if (in_ether(host, &sa)) {
  322. fprintf(stderr, "invalid hw-addr %s\n", host);
  323. ++goterr;
  324. continue;
  325. }
  326. p = (char *) &sa;
  327. }
  328. #endif
  329. memcpy((((char *)(&ifr)) + a1op->ifr_offset),
  330. p, sizeof(struct sockaddr));
  331. } else {
  332. unsigned int i = strtoul(*argv,NULL,0);
  333. p = ((char *)(&ifr)) + a1op->ifr_offset;
  334. #ifdef BB_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
  335. if (mask & A_MAP_TYPE) {
  336. if (ioctl(sockfd, SIOCGIFMAP, &ifr) < 0) {
  337. ++goterr;
  338. continue;
  339. }
  340. if ((mask & A_MAP_UCHAR) == A_MAP_UCHAR) {
  341. *((unsigned char *) p) = i;
  342. } else if (mask & A_MAP_USHORT) {
  343. *((unsigned short *) p) = i;
  344. } else {
  345. *((unsigned long *) p) = i;
  346. }
  347. } else
  348. #endif
  349. if (mask & A_CAST_CHAR_PTR) {
  350. *((caddr_t *) p) = (caddr_t) i;
  351. } else { /* A_CAST_INT */
  352. *((int *) p) = i;
  353. }
  354. }
  355. if (ioctl(sockfd, a1op->selector, &ifr) < 0) {
  356. perror(a1op->name);
  357. ++goterr;
  358. continue;
  359. }
  360. #ifdef QUESTIONABLE_ALIAS_CASE
  361. if (mask & A_COLON_CHK) {
  362. /*
  363. * Don't do the set_flag() if the address is an alias with
  364. * a - at the end, since it's deleted already! - Roman
  365. *
  366. * Should really use regex.h here, not sure though how well
  367. * it'll go with the cross-platform support etc.
  368. */
  369. char *ptr;
  370. short int found_colon = 0;
  371. for (ptr = ifr.ifr_name; *ptr; ptr++ ) {
  372. if (*ptr == ':') {
  373. found_colon++;
  374. }
  375. }
  376. if (found_colon && *(ptr - 1) == '-') {
  377. continue;
  378. }
  379. }
  380. #endif
  381. }
  382. if (!(mask & A_SET_AFTER)) {
  383. continue;
  384. }
  385. mask = N_SET;
  386. }
  387. if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
  388. perror("SIOCGIFFLAGS");
  389. ++goterr;
  390. } else {
  391. selector = op->selector;
  392. if (mask & SET_MASK) {
  393. ifr.ifr_flags |= selector;
  394. } else {
  395. ifr.ifr_flags &= ~selector;
  396. }
  397. if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
  398. perror("SIOCSIFFLAGS");
  399. ++goterr;
  400. }
  401. }
  402. LOOP:
  403. continue;
  404. } /* end of while-loop */
  405. return goterr;
  406. }
  407. #ifdef BB_FEATURE_IFCONFIG_HW
  408. /* Input an Ethernet address and convert to binary. */
  409. static int
  410. in_ether(char *bufp, struct sockaddr *sap)
  411. {
  412. unsigned char *ptr;
  413. int i, j;
  414. unsigned char val;
  415. unsigned char c;
  416. sap->sa_family = ARPHRD_ETHER;
  417. ptr = sap->sa_data;
  418. for (i = 0 ; i < ETH_ALEN ; i++) {
  419. val = 0;
  420. /* We might get a semicolon here - not required. */
  421. if (i && (*bufp == ':')) {
  422. bufp++;
  423. }
  424. for (j=0 ; j<2 ; j++) {
  425. c = *bufp;
  426. if (c >= '0' && c <= '9') {
  427. c -= '0';
  428. } else if (c >= 'a' && c <= 'f') {
  429. c -= ('a' - 10);
  430. } else if (c >= 'A' && c <= 'F') {
  431. c -= ('A' - 10);
  432. } else if (j && (c == ':' || c == 0)) {
  433. break;
  434. } else {
  435. return -1;
  436. }
  437. ++bufp;
  438. val <<= 4;
  439. val += c;
  440. }
  441. *ptr++ = val;
  442. }
  443. return (int) (*bufp); /* Error if we don't end at end of string. */
  444. }
  445. #endif