ifconfig.c 17 KB

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