ifconfig.c 16 KB

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