ifconfig.c 18 KB

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