3
0

ifconfig.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * ifconfig
  4. *
  5. * Similar to the standard Unix ifconfig, but with only the necessary
  6. * parts for AF_INET, and without any printing of if info (for now).
  7. *
  8. * Bjorn Wesen, Axis Communications AB
  9. *
  10. * Authors of the original ifconfig was:
  11. * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  12. *
  13. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  14. */
  15. /*
  16. * Heavily modified by Manuel Novoa III Mar 6, 2001
  17. *
  18. * From initial port to busybox, removed most of the redundancy by
  19. * converting to a table-driven approach. Added several (optional)
  20. * args missing from initial port.
  21. *
  22. * Still missing: media, tunnel.
  23. *
  24. * 2002-04-20
  25. * IPV6 support added by Bart Visscher <magick@linux-fan.com>
  26. */
  27. //config:config IFCONFIG
  28. //config: bool "ifconfig (12 kb)"
  29. //config: default y
  30. //config: help
  31. //config: Ifconfig is used to configure the kernel-resident network interfaces.
  32. //config:
  33. //config:config FEATURE_IFCONFIG_STATUS
  34. //config: bool "Enable status reporting output (+7k)"
  35. //config: default y
  36. //config: depends on IFCONFIG
  37. //config: help
  38. //config: If ifconfig is called with no arguments it will display the status
  39. //config: of the currently active interfaces.
  40. //config:
  41. //config:config FEATURE_IFCONFIG_SLIP
  42. //config: bool "Enable slip-specific options \"keepalive\" and \"outfill\""
  43. //config: default y
  44. //config: depends on IFCONFIG
  45. //config: help
  46. //config: Allow "keepalive" and "outfill" support for SLIP. If you're not
  47. //config: planning on using serial lines, leave this unchecked.
  48. //config:
  49. //config:config FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
  50. //config: bool "Enable options \"mem_start\", \"io_addr\", and \"irq\""
  51. //config: default y
  52. //config: depends on IFCONFIG
  53. //config: help
  54. //config: Allow the start address for shared memory, start address for I/O,
  55. //config: and/or the interrupt line used by the specified device.
  56. //config:
  57. //config:config FEATURE_IFCONFIG_HW
  58. //config: bool "Enable option \"hw\" (ether only)"
  59. //config: default y
  60. //config: depends on IFCONFIG
  61. //config: help
  62. //config: Set the hardware address of this interface, if the device driver
  63. //config: supports this operation. Currently, we only support the 'ether'
  64. //config: class.
  65. //config:
  66. //config:config FEATURE_IFCONFIG_BROADCAST_PLUS
  67. //config: bool "Set the broadcast automatically"
  68. //config: default y
  69. //config: depends on IFCONFIG
  70. //config: help
  71. //config: Setting this will make ifconfig attempt to find the broadcast
  72. //config: automatically if the value '+' is used.
  73. //applet:IF_IFCONFIG(APPLET(ifconfig, BB_DIR_SBIN, BB_SUID_DROP))
  74. //kbuild:lib-$(CONFIG_IFCONFIG) += ifconfig.o interface.o
  75. //usage:#define ifconfig_trivial_usage
  76. //usage: IF_FEATURE_IFCONFIG_STATUS("[-a]") " [IFACE] [ADDRESS]"
  77. //usage:#define ifconfig_full_usage "\n\n"
  78. //usage: "Configure a network interface\n"
  79. //usage: "\n"
  80. //usage: IF_FEATURE_IPV6(
  81. //usage: " [add ADDRESS[/PREFIXLEN]]\n")
  82. //usage: IF_FEATURE_IPV6(
  83. //usage: " [del ADDRESS[/PREFIXLEN]]\n")
  84. //usage: " [[-]broadcast [ADDRESS]] [[-]pointopoint [ADDRESS]]\n"
  85. //usage: " [netmask ADDRESS] [dstaddr ADDRESS]\n"
  86. //usage: IF_FEATURE_IFCONFIG_SLIP(
  87. //usage: " [outfill NN] [keepalive NN]\n")
  88. //usage: " " IF_FEATURE_IFCONFIG_HW("[hw ether" IF_FEATURE_HWIB("|infiniband")" ADDRESS] ") "[metric NN] [mtu NN]\n"
  89. //usage: " [[-]trailers] [[-]arp] [[-]allmulti]\n"
  90. //usage: " [multicast] [[-]promisc] [txqueuelen NN] [[-]dynamic]\n"
  91. //usage: IF_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ(
  92. //usage: " [mem_start NN] [io_addr NN] [irq NN]\n")
  93. //usage: " [up|down] ..."
  94. #include "libbb.h"
  95. #include "inet_common.h"
  96. #include <net/if.h>
  97. #include <net/if_arp.h>
  98. #include <netinet/in.h>
  99. #ifdef HAVE_NET_ETHERNET_H
  100. # include <net/ethernet.h>
  101. #endif
  102. #if ENABLE_FEATURE_IFCONFIG_SLIP
  103. # include <linux/if_slip.h>
  104. #endif
  105. /* I don't know if this is needed for busybox or not. Anyone? */
  106. #define QUESTIONABLE_ALIAS_CASE
  107. /* Defines for glibc2.0 users. */
  108. #ifndef SIOCSIFTXQLEN
  109. # define SIOCSIFTXQLEN 0x8943
  110. # define SIOCGIFTXQLEN 0x8942
  111. #endif
  112. /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
  113. #ifndef ifr_qlen
  114. # define ifr_qlen ifr_ifru.ifru_mtu
  115. #endif
  116. #ifndef IFF_DYNAMIC
  117. # define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
  118. #endif
  119. #if ENABLE_FEATURE_IPV6
  120. struct in6_ifreq {
  121. struct in6_addr ifr6_addr;
  122. uint32_t ifr6_prefixlen;
  123. int ifr6_ifindex;
  124. };
  125. #endif
  126. /*
  127. * Here are the bit masks for the "flags" member of struct options below.
  128. * N_ signifies no arg prefix; M_ signifies arg prefixed by '-'.
  129. * CLR clears the flag; SET sets the flag; ARG signifies (optional) arg.
  130. */
  131. #define N_CLR 0x01
  132. #define M_CLR 0x02
  133. #define N_SET 0x04
  134. #define M_SET 0x08
  135. #define N_ARG 0x10
  136. #define M_ARG 0x20
  137. #define M_MASK (M_CLR | M_SET | M_ARG)
  138. #define N_MASK (N_CLR | N_SET | N_ARG)
  139. #define SET_MASK (N_SET | M_SET)
  140. #define CLR_MASK (N_CLR | M_CLR)
  141. #define SET_CLR_MASK (SET_MASK | CLR_MASK)
  142. #define ARG_MASK (M_ARG | N_ARG)
  143. /*
  144. * Here are the bit masks for the "arg_flags" member of struct options below.
  145. */
  146. /*
  147. * cast type:
  148. * 00 int
  149. * 01 char *
  150. * 02 HOST_COPY in_ether
  151. * 03 HOST_COPY INET_resolve
  152. */
  153. #define A_CAST_TYPE 0x03
  154. /*
  155. * map type:
  156. * 00 not a map type (mem_start, io_addr, irq)
  157. * 04 memstart (unsigned long)
  158. * 08 io_addr (unsigned short)
  159. * 0C irq (unsigned char)
  160. */
  161. #define A_MAP_TYPE 0x0C
  162. #define A_ARG_REQ 0x10 /* Set if an arg is required. */
  163. #define A_NETMASK 0x20 /* Set if netmask (check for multiple sets). */
  164. #define A_SET_AFTER 0x40 /* Set a flag at the end. */
  165. #define A_COLON_CHK 0x80 /* Is this needed? See below. */
  166. #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
  167. #define A_HOSTNAME 0x100 /* Set if it is ip addr. */
  168. #define A_BROADCAST 0x200 /* Set if it is broadcast addr. */
  169. #else
  170. #define A_HOSTNAME 0
  171. #define A_BROADCAST 0
  172. #endif
  173. /*
  174. * These defines are for dealing with the A_CAST_TYPE field.
  175. */
  176. #define A_CAST_CHAR_PTR 0x01
  177. #define A_CAST_RESOLVE 0x01
  178. #define A_CAST_HOST_COPY 0x02
  179. #define A_CAST_HOST_COPY_IN_ETHER A_CAST_HOST_COPY
  180. #define A_CAST_HOST_COPY_RESOLVE (A_CAST_HOST_COPY | A_CAST_RESOLVE)
  181. /*
  182. * These defines are for dealing with the A_MAP_TYPE field.
  183. */
  184. #define A_MAP_ULONG 0x04 /* memstart */
  185. #define A_MAP_USHORT 0x08 /* io_addr */
  186. #define A_MAP_UCHAR 0x0C /* irq */
  187. /*
  188. * Define the bit masks signifying which operations to perform for each arg.
  189. */
  190. #define ARG_METRIC (A_ARG_REQ /*| A_CAST_INT*/)
  191. #define ARG_MTU (A_ARG_REQ /*| A_CAST_INT*/)
  192. #define ARG_TXQUEUELEN (A_ARG_REQ /*| A_CAST_INT*/)
  193. #define ARG_MEM_START (A_ARG_REQ | A_MAP_ULONG)
  194. #define ARG_IO_ADDR (A_ARG_REQ | A_MAP_ULONG)
  195. #define ARG_IRQ (A_ARG_REQ | A_MAP_UCHAR)
  196. #define ARG_DSTADDR (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE)
  197. #define ARG_NETMASK (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_NETMASK)
  198. #define ARG_BROADCAST (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_BROADCAST)
  199. #define ARG_HW (A_ARG_REQ | A_CAST_HOST_COPY_IN_ETHER)
  200. #define ARG_POINTOPOINT (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
  201. #define ARG_KEEPALIVE (A_ARG_REQ | A_CAST_CHAR_PTR)
  202. #define ARG_OUTFILL (A_ARG_REQ | A_CAST_CHAR_PTR)
  203. #define ARG_HOSTNAME (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_COLON_CHK | A_HOSTNAME)
  204. #define ARG_ADD_DEL (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
  205. struct arg1opt {
  206. const char *name;
  207. unsigned short selector;
  208. unsigned short ifr_offset;
  209. };
  210. struct options {
  211. const char *name;
  212. #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
  213. const unsigned int flags:6;
  214. const unsigned int arg_flags:10;
  215. #else
  216. const unsigned char flags;
  217. const unsigned char arg_flags;
  218. #endif
  219. const unsigned short selector;
  220. };
  221. #define ifreq_offsetof(x) offsetof(struct ifreq, x)
  222. /*
  223. * Set up the tables. Warning! They must have corresponding order!
  224. */
  225. static const struct arg1opt Arg1Opt[] ALIGN_PTR = {
  226. { "SIFMETRIC", SIOCSIFMETRIC, ifreq_offsetof(ifr_metric) },
  227. { "SIFMTU", SIOCSIFMTU, ifreq_offsetof(ifr_mtu) },
  228. { "SIFTXQLEN", SIOCSIFTXQLEN, ifreq_offsetof(ifr_qlen) },
  229. { "SIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr) },
  230. { "SIFNETMASK", SIOCSIFNETMASK, ifreq_offsetof(ifr_netmask) },
  231. { "SIFBRDADDR", SIOCSIFBRDADDR, ifreq_offsetof(ifr_broadaddr) },
  232. #if ENABLE_FEATURE_IFCONFIG_HW
  233. { "SIFHWADDR", SIOCSIFHWADDR, ifreq_offsetof(ifr_hwaddr) },
  234. #endif
  235. { "SIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr) },
  236. #ifdef SIOCSKEEPALIVE
  237. { "SKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data) },
  238. #endif
  239. #ifdef SIOCSOUTFILL
  240. { "SOUTFILL", SIOCSOUTFILL, ifreq_offsetof(ifr_data) },
  241. #endif
  242. #if ENABLE_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
  243. { "SIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.mem_start) },
  244. { "SIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.base_addr) },
  245. { "SIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.irq) },
  246. #endif
  247. #if ENABLE_FEATURE_IPV6
  248. { "SIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr) }, /* IPv6 version ignores the offset */
  249. { "DIFADDR", SIOCDIFADDR, ifreq_offsetof(ifr_addr) }, /* IPv6 version ignores the offset */
  250. #endif
  251. /* Last entry is for unmatched (assumed to be hostname/address) arg. */
  252. { "SIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr) },
  253. };
  254. static const struct options OptArray[] ALIGN_PTR = {
  255. { "metric", N_ARG, ARG_METRIC, 0 },
  256. { "mtu", N_ARG, ARG_MTU, 0 },
  257. { "txqueuelen", N_ARG, ARG_TXQUEUELEN, 0 },
  258. { "dstaddr", N_ARG, ARG_DSTADDR, 0 },
  259. { "netmask", N_ARG, ARG_NETMASK, 0 },
  260. { "broadcast", N_ARG | M_CLR, ARG_BROADCAST, IFF_BROADCAST },
  261. #if ENABLE_FEATURE_IFCONFIG_HW
  262. { "hw", N_ARG, ARG_HW, 0 },
  263. #endif
  264. { "pointopoint", N_ARG | M_CLR, ARG_POINTOPOINT, IFF_POINTOPOINT },
  265. #ifdef SIOCSKEEPALIVE
  266. { "keepalive", N_ARG, ARG_KEEPALIVE, 0 },
  267. #endif
  268. #ifdef SIOCSOUTFILL
  269. { "outfill", N_ARG, ARG_OUTFILL, 0 },
  270. #endif
  271. #if ENABLE_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
  272. { "mem_start", N_ARG, ARG_MEM_START, 0 },
  273. { "io_addr", N_ARG, ARG_IO_ADDR, 0 },
  274. { "irq", N_ARG, ARG_IRQ, 0 },
  275. #endif
  276. #if ENABLE_FEATURE_IPV6
  277. { "add", N_ARG, ARG_ADD_DEL, 0 },
  278. { "del", N_ARG, ARG_ADD_DEL, 0 },
  279. #endif
  280. { "arp", N_CLR | M_SET, 0, IFF_NOARP },
  281. { "trailers", N_CLR | M_SET, 0, IFF_NOTRAILERS },
  282. { "promisc", N_SET | M_CLR, 0, IFF_PROMISC },
  283. { "multicast", N_SET | M_CLR, 0, IFF_MULTICAST },
  284. { "allmulti", N_SET | M_CLR, 0, IFF_ALLMULTI },
  285. { "dynamic", N_SET | M_CLR, 0, IFF_DYNAMIC },
  286. { "up", N_SET, 0, (IFF_UP | IFF_RUNNING) },
  287. { "down", N_CLR, 0, IFF_UP },
  288. { NULL, 0, ARG_HOSTNAME, (IFF_UP | IFF_RUNNING) }
  289. };
  290. int ifconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  291. int ifconfig_main(int argc UNUSED_PARAM, char **argv)
  292. {
  293. struct ifreq ifr;
  294. struct sockaddr_in sai;
  295. #if ENABLE_FEATURE_IFCONFIG_HW
  296. struct sockaddr sa;
  297. #endif
  298. const struct arg1opt *a1op;
  299. const struct options *op;
  300. int sockfd; /* socket fd we use to manipulate stuff with */
  301. int selector;
  302. #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
  303. unsigned int mask;
  304. unsigned int did_flags;
  305. unsigned int sai_hostname, sai_netmask;
  306. #else
  307. unsigned char mask;
  308. unsigned char did_flags;
  309. #endif
  310. char *p;
  311. /*char host[128];*/
  312. const char *host = NULL; /* make gcc happy */
  313. IF_FEATURE_IFCONFIG_STATUS(char *show_all_param;)
  314. did_flags = 0;
  315. #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
  316. sai_hostname = 0;
  317. sai_netmask = 0;
  318. #endif
  319. /* skip argv[0] */
  320. ++argv;
  321. #if ENABLE_FEATURE_IFCONFIG_STATUS
  322. show_all_param = NULL;
  323. if (argv[0] && argv[0][0] == '-' && argv[0][1] == 'a' && !argv[0][2]) {
  324. ++argv;
  325. show_all_param = IFNAME_SHOW_DOWNED_TOO;
  326. }
  327. #endif
  328. if (!argv[0] || !argv[1]) { /* one or no args */
  329. #if ENABLE_FEATURE_IFCONFIG_STATUS
  330. return display_interfaces(argv[0] ? argv[0] : show_all_param);
  331. #else
  332. bb_simple_error_msg_and_die("no support for status display");
  333. #endif
  334. }
  335. /* Create a channel to the NET kernel. */
  336. sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
  337. /* get interface name */
  338. strncpy_IFNAMSIZ(ifr.ifr_name, *argv);
  339. /* Process the remaining arguments. */
  340. while (*++argv != NULL) {
  341. p = *argv;
  342. mask = N_MASK;
  343. if (*p == '-') { /* If the arg starts with '-'... */
  344. ++p; /* advance past it and */
  345. mask = M_MASK; /* set the appropriate mask. */
  346. }
  347. for (op = OptArray; op->name; op++) { /* Find table entry. */
  348. if (strcmp(p, op->name) == 0) { /* If name matches... */
  349. mask &= op->flags;
  350. if (mask) /* set the mask and go. */
  351. goto FOUND_ARG;
  352. /* If we get here, there was a valid arg with an */
  353. /* invalid '-' prefix. */
  354. bb_error_msg_and_die("bad: '%s'", p-1);
  355. }
  356. }
  357. /* We fell through, so treat as possible hostname. */
  358. a1op = Arg1Opt + ARRAY_SIZE(Arg1Opt) - 1;
  359. mask = op->arg_flags;
  360. goto HOSTNAME;
  361. FOUND_ARG:
  362. if (mask & ARG_MASK) {
  363. mask = op->arg_flags;
  364. if (mask & A_NETMASK & did_flags)
  365. bb_show_usage();
  366. a1op = Arg1Opt + (op - OptArray);
  367. if (*++argv == NULL) {
  368. if (mask & A_ARG_REQ)
  369. bb_show_usage();
  370. --argv;
  371. mask &= A_SET_AFTER; /* just for broadcast */
  372. } else { /* got an arg so process it */
  373. HOSTNAME:
  374. did_flags |= (mask & (A_NETMASK|A_HOSTNAME));
  375. if (mask & A_CAST_HOST_COPY) {
  376. #if ENABLE_FEATURE_IFCONFIG_HW
  377. if (mask & A_CAST_RESOLVE) {
  378. #endif
  379. host = *argv;
  380. if (strcmp(host, "inet") == 0)
  381. continue; /* compat stuff */
  382. sai.sin_family = AF_INET;
  383. sai.sin_port = 0;
  384. if (strcmp(host, "default") == 0) {
  385. /* Default is special, meaning 0.0.0.0. */
  386. sai.sin_addr.s_addr = INADDR_ANY;
  387. }
  388. #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
  389. else if ((host[0] == '+' && !host[1])
  390. && (mask & A_BROADCAST)
  391. && (did_flags & (A_NETMASK|A_HOSTNAME)) == (A_NETMASK|A_HOSTNAME)
  392. ) {
  393. /* + is special, meaning broadcast is derived. */
  394. sai.sin_addr.s_addr = (~sai_netmask) | (sai_hostname & sai_netmask);
  395. }
  396. #endif
  397. else {
  398. len_and_sockaddr *lsa;
  399. #if ENABLE_FEATURE_IPV6
  400. char *prefix;
  401. int prefix_len = 0;
  402. prefix = strchr(host, '/');
  403. if (prefix) {
  404. prefix_len = xatou_range(prefix + 1, 0, 128);
  405. *prefix = '\0';
  406. }
  407. resolve:
  408. #endif
  409. lsa = xhost2sockaddr(host, 0);
  410. #if ENABLE_FEATURE_IPV6
  411. if (lsa->u.sa.sa_family != AF_INET6 && prefix) {
  412. /* TODO: we do not support "ifconfig eth0 up 1.2.3.4/17".
  413. * For now, just make it fail instead of silently ignoring "/17" part:
  414. */
  415. *prefix = '/';
  416. goto resolve;
  417. }
  418. if (lsa->u.sa.sa_family == AF_INET6) {
  419. int sockfd6;
  420. struct in6_ifreq ifr6;
  421. sockfd6 = xsocket(AF_INET6, SOCK_DGRAM, 0);
  422. xioctl(sockfd6, SIOCGIFINDEX, &ifr);
  423. ifr6.ifr6_ifindex = ifr.ifr_ifindex;
  424. ifr6.ifr6_prefixlen = prefix_len;
  425. memcpy(&ifr6.ifr6_addr,
  426. &lsa->u.sin6.sin6_addr,
  427. sizeof(struct in6_addr));
  428. ioctl_or_perror_and_die(sockfd6, a1op->selector, &ifr6, "SIOC%s", a1op->name);
  429. if (ENABLE_FEATURE_CLEAN_UP)
  430. free(lsa);
  431. continue;
  432. }
  433. #endif
  434. sai.sin_addr = lsa->u.sin.sin_addr;
  435. if (ENABLE_FEATURE_CLEAN_UP)
  436. free(lsa);
  437. }
  438. #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
  439. if (mask & A_HOSTNAME)
  440. sai_hostname = sai.sin_addr.s_addr;
  441. if (mask & A_NETMASK)
  442. sai_netmask = sai.sin_addr.s_addr;
  443. #endif
  444. p = (char *) &sai;
  445. #if ENABLE_FEATURE_IFCONFIG_HW
  446. } else { /* A_CAST_HOST_COPY_IN_ETHER */
  447. /* This is the "hw" arg case. */
  448. smalluint hw_class = index_in_substrings("ether\0"
  449. IF_FEATURE_HWIB("infiniband\0"), *argv) + 1;
  450. if (!hw_class || !*++argv)
  451. bb_show_usage();
  452. host = *argv;
  453. if (hw_class == 1 ? in_ether(host, &sa) : in_ib(host, &sa))
  454. bb_error_msg_and_die("invalid hw-addr %s", host);
  455. p = (char *) &sa;
  456. }
  457. #endif
  458. memcpy( ((char *)&ifr) + a1op->ifr_offset,
  459. p, sizeof(struct sockaddr));
  460. } else {
  461. /* FIXME: error check?? */
  462. unsigned long i = strtoul(*argv, NULL, 0);
  463. p = ((char *)&ifr) + a1op->ifr_offset;
  464. #if ENABLE_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
  465. if (mask & A_MAP_TYPE) {
  466. xioctl(sockfd, SIOCGIFMAP, &ifr);
  467. if ((mask & A_MAP_UCHAR) == A_MAP_UCHAR)
  468. *(unsigned char *) p = i;
  469. else if (mask & A_MAP_USHORT)
  470. *(unsigned short *) p = i;
  471. else
  472. *(unsigned long *) p = i;
  473. } else
  474. #endif
  475. if (mask & A_CAST_CHAR_PTR)
  476. *(caddr_t *) p = (caddr_t) i;
  477. else /* A_CAST_INT */
  478. *(int *) p = i;
  479. }
  480. ioctl_or_perror_and_die(sockfd, a1op->selector, &ifr, "SIOC%s", a1op->name);
  481. #ifdef QUESTIONABLE_ALIAS_CASE
  482. if (mask & A_COLON_CHK) {
  483. /*
  484. * Don't do the set_flag() if the address is an alias with
  485. * a '-' at the end, since it's deleted already! - Roman
  486. *
  487. * Should really use regex.h here, not sure though how well
  488. * it'll go with the cross-platform support etc.
  489. */
  490. char *ptr;
  491. short int found_colon = 0;
  492. for (ptr = ifr.ifr_name; *ptr; ptr++)
  493. if (*ptr == ':')
  494. found_colon++;
  495. if (found_colon && ptr[-1] == '-')
  496. continue;
  497. }
  498. #endif
  499. }
  500. if (!(mask & A_SET_AFTER))
  501. continue;
  502. mask = N_SET;
  503. } /* if (mask & ARG_MASK) */
  504. xioctl(sockfd, SIOCGIFFLAGS, &ifr);
  505. selector = op->selector;
  506. if (mask & SET_MASK)
  507. ifr.ifr_flags |= selector;
  508. else
  509. ifr.ifr_flags &= ~selector;
  510. xioctl(sockfd, SIOCSIFFLAGS, &ifr);
  511. } /* while () */
  512. if (ENABLE_FEATURE_CLEAN_UP)
  513. close(sockfd);
  514. return 0;
  515. }