ifconfig.c 17 KB

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