ifconfig.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /* vi: set sw=4 ts=4: */
  2. /* ifconfig
  3. *
  4. * Similar to the standard Unix ifconfig, but with only the necessary
  5. * parts for AF_INET, and without any printing of if info (for now).
  6. *
  7. * Bjorn Wesen, Axis Communications AB
  8. *
  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. 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. if (argv[0] && (argv[0][0] == '-' && argv[0][1] == 'a' && !argv[0][2])) {
  323. interface_opt_a = 1;
  324. ++argv;
  325. }
  326. #endif
  327. if (!argv[0] || !argv[1]) { /* one or no args */
  328. #if ENABLE_FEATURE_IFCONFIG_STATUS
  329. return display_interfaces(argv[0] /* can be NULL */);
  330. #else
  331. bb_error_msg_and_die("no support for status display");
  332. #endif
  333. }
  334. /* Create a channel to the NET kernel. */
  335. sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
  336. /* get interface name */
  337. strncpy_IFNAMSIZ(ifr.ifr_name, *argv);
  338. /* Process the remaining arguments. */
  339. while (*++argv != NULL) {
  340. p = *argv;
  341. mask = N_MASK;
  342. if (*p == '-') { /* If the arg starts with '-'... */
  343. ++p; /* advance past it and */
  344. mask = M_MASK; /* set the appropriate mask. */
  345. }
  346. for (op = OptArray; op->name; op++) { /* Find table entry. */
  347. if (strcmp(p, op->name) == 0) { /* If name matches... */
  348. mask &= op->flags;
  349. if (mask) /* set the mask and go. */
  350. goto FOUND_ARG;
  351. /* If we get here, there was a valid arg with an */
  352. /* invalid '-' prefix. */
  353. bb_error_msg_and_die("bad: '%s'", p-1);
  354. }
  355. }
  356. /* We fell through, so treat as possible hostname. */
  357. a1op = Arg1Opt + ARRAY_SIZE(Arg1Opt) - 1;
  358. mask = op->arg_flags;
  359. goto HOSTNAME;
  360. FOUND_ARG:
  361. if (mask & ARG_MASK) {
  362. mask = op->arg_flags;
  363. if (mask & A_NETMASK & did_flags)
  364. bb_show_usage();
  365. a1op = Arg1Opt + (op - OptArray);
  366. if (*++argv == NULL) {
  367. if (mask & A_ARG_REQ)
  368. bb_show_usage();
  369. --argv;
  370. mask &= A_SET_AFTER; /* just for broadcast */
  371. } else { /* got an arg so process it */
  372. HOSTNAME:
  373. did_flags |= (mask & (A_NETMASK|A_HOSTNAME));
  374. if (mask & A_CAST_HOST_COPY) {
  375. #if ENABLE_FEATURE_IFCONFIG_HW
  376. if (mask & A_CAST_RESOLVE) {
  377. #endif
  378. host = *argv;
  379. if (strcmp(host, "inet") == 0)
  380. continue; /* compat stuff */
  381. sai.sin_family = AF_INET;
  382. sai.sin_port = 0;
  383. if (strcmp(host, "default") == 0) {
  384. /* Default is special, meaning 0.0.0.0. */
  385. sai.sin_addr.s_addr = INADDR_ANY;
  386. }
  387. #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
  388. else if ((host[0] == '+' && !host[1])
  389. && (mask & A_BROADCAST)
  390. && (did_flags & (A_NETMASK|A_HOSTNAME)) == (A_NETMASK|A_HOSTNAME)
  391. ) {
  392. /* + is special, meaning broadcast is derived. */
  393. sai.sin_addr.s_addr = (~sai_netmask) | (sai_hostname & sai_netmask);
  394. }
  395. #endif
  396. else {
  397. len_and_sockaddr *lsa;
  398. #if ENABLE_FEATURE_IPV6
  399. char *prefix;
  400. int prefix_len = 0;
  401. prefix = strchr(host, '/');
  402. if (prefix) {
  403. prefix_len = xatou_range(prefix + 1, 0, 128);
  404. *prefix = '\0';
  405. }
  406. resolve:
  407. #endif
  408. lsa = xhost2sockaddr(host, 0);
  409. #if ENABLE_FEATURE_IPV6
  410. if (lsa->u.sa.sa_family != AF_INET6 && prefix) {
  411. /* TODO: we do not support "ifconfig eth0 up 1.2.3.4/17".
  412. * For now, just make it fail instead of silently ignoring "/17" part:
  413. */
  414. *prefix = '/';
  415. goto resolve;
  416. }
  417. if (lsa->u.sa.sa_family == AF_INET6) {
  418. int sockfd6;
  419. struct in6_ifreq ifr6;
  420. sockfd6 = xsocket(AF_INET6, SOCK_DGRAM, 0);
  421. xioctl(sockfd6, SIOCGIFINDEX, &ifr);
  422. ifr6.ifr6_ifindex = ifr.ifr_ifindex;
  423. ifr6.ifr6_prefixlen = prefix_len;
  424. memcpy(&ifr6.ifr6_addr,
  425. &lsa->u.sin6.sin6_addr,
  426. sizeof(struct in6_addr));
  427. ioctl_or_perror_and_die(sockfd6, a1op->selector, &ifr6, "SIOC%s", a1op->name);
  428. if (ENABLE_FEATURE_CLEAN_UP)
  429. free(lsa);
  430. continue;
  431. }
  432. #endif
  433. sai.sin_addr = lsa->u.sin.sin_addr;
  434. if (ENABLE_FEATURE_CLEAN_UP)
  435. free(lsa);
  436. }
  437. #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
  438. if (mask & A_HOSTNAME)
  439. sai_hostname = sai.sin_addr.s_addr;
  440. if (mask & A_NETMASK)
  441. sai_netmask = sai.sin_addr.s_addr;
  442. #endif
  443. p = (char *) &sai;
  444. #if ENABLE_FEATURE_IFCONFIG_HW
  445. } else { /* A_CAST_HOST_COPY_IN_ETHER */
  446. /* This is the "hw" arg case. */
  447. smalluint hw_class = index_in_substrings("ether\0"
  448. IF_FEATURE_HWIB("infiniband\0"), *argv) + 1;
  449. if (!hw_class || !*++argv)
  450. bb_show_usage();
  451. host = *argv;
  452. if (hw_class == 1 ? in_ether(host, &sa) : in_ib(host, &sa))
  453. bb_error_msg_and_die("invalid hw-addr %s", host);
  454. p = (char *) &sa;
  455. }
  456. #endif
  457. memcpy( ((char *)&ifr) + a1op->ifr_offset,
  458. p, sizeof(struct sockaddr));
  459. } else {
  460. /* FIXME: error check?? */
  461. unsigned long i = strtoul(*argv, NULL, 0);
  462. p = ((char *)&ifr) + a1op->ifr_offset;
  463. #if ENABLE_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
  464. if (mask & A_MAP_TYPE) {
  465. xioctl(sockfd, SIOCGIFMAP, &ifr);
  466. if ((mask & A_MAP_UCHAR) == A_MAP_UCHAR)
  467. *(unsigned char *) p = i;
  468. else if (mask & A_MAP_USHORT)
  469. *(unsigned short *) p = i;
  470. else
  471. *(unsigned long *) p = i;
  472. } else
  473. #endif
  474. if (mask & A_CAST_CHAR_PTR)
  475. *(caddr_t *) p = (caddr_t) i;
  476. else /* A_CAST_INT */
  477. *(int *) p = i;
  478. }
  479. ioctl_or_perror_and_die(sockfd, a1op->selector, &ifr, "SIOC%s", a1op->name);
  480. #ifdef QUESTIONABLE_ALIAS_CASE
  481. if (mask & A_COLON_CHK) {
  482. /*
  483. * Don't do the set_flag() if the address is an alias with
  484. * a '-' at the end, since it's deleted already! - Roman
  485. *
  486. * Should really use regex.h here, not sure though how well
  487. * it'll go with the cross-platform support etc.
  488. */
  489. char *ptr;
  490. short int found_colon = 0;
  491. for (ptr = ifr.ifr_name; *ptr; ptr++)
  492. if (*ptr == ':')
  493. found_colon++;
  494. if (found_colon && ptr[-1] == '-')
  495. continue;
  496. }
  497. #endif
  498. }
  499. if (!(mask & A_SET_AFTER))
  500. continue;
  501. mask = N_SET;
  502. } /* if (mask & ARG_MASK) */
  503. xioctl(sockfd, SIOCGIFFLAGS, &ifr);
  504. selector = op->selector;
  505. if (mask & SET_MASK)
  506. ifr.ifr_flags |= selector;
  507. else
  508. ifr.ifr_flags &= ~selector;
  509. xioctl(sockfd, SIOCSIFFLAGS, &ifr);
  510. } /* while () */
  511. if (ENABLE_FEATURE_CLEAN_UP)
  512. close(sockfd);
  513. return 0;
  514. }