blob: 0b834e7c78645bc953e745742baa68970404a6b4 [file] [log] [blame]
Eric Andersenec455952001-02-14 08:11:27 +00001/* 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 *
Eric Andersencd8c4362001-11-10 11:22:46 +000018 * $Id: ifconfig.c,v 1.16 2001/11/10 11:22:43 andersen Exp $
Eric Andersenec455952001-02-14 08:11:27 +000019 *
20 */
21
Manuel Novoa III df351d62001-03-08 22:57:00 +000022/*
23 * Heavily modified by Manuel Novoa III Mar 6, 2001
24 *
25 * From initial port to busybox, removed most of the redundancy by
26 * converting to a table-driven approach. Added several (optional)
27 * args missing from initial port.
28 *
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +000029 * Still missing: media, tunnel.
Manuel Novoa III df351d62001-03-08 22:57:00 +000030 */
31
Eric Andersenec455952001-02-14 08:11:27 +000032#include <stdio.h>
33#include <stdlib.h>
Eric Andersenec455952001-02-14 08:11:27 +000034#include <string.h> // strcmp and friends
35#include <ctype.h> // isdigit and friends
Manuel Novoa III df351d62001-03-08 22:57:00 +000036#include <stddef.h> /* offsetof */
Eric Andersenec455952001-02-14 08:11:27 +000037#include <sys/ioctl.h>
Eric Andersenec455952001-02-14 08:11:27 +000038#include <net/if_arp.h>
Eric Andersencd8c4362001-11-10 11:22:46 +000039#include <netinet/in.h>
Eric Andersenec455952001-02-14 08:11:27 +000040#include <linux/if_ether.h>
Eric Andersencd8c4362001-11-10 11:22:46 +000041#include <net/if.h>
42#include "inet_common.h"
Manuel Novoa III df351d62001-03-08 22:57:00 +000043#include "busybox.h"
Eric Andersenec455952001-02-14 08:11:27 +000044
Eric Andersenbdfd0d72001-10-24 05:00:29 +000045#ifdef CONFIG_FEATURE_IFCONFIG_SLIP
Manuel Novoa III df351d62001-03-08 22:57:00 +000046#include <linux/if_slip.h>
Eric Andersenf15d4da2001-03-06 00:48:59 +000047#endif
48
Manuel Novoa III df351d62001-03-08 22:57:00 +000049/* I don't know if this is needed for busybox or not. Anyone? */
50#define QUESTIONABLE_ALIAS_CASE
Eric Andersenec455952001-02-14 08:11:27 +000051
52
Manuel Novoa III df351d62001-03-08 22:57:00 +000053/* Defines for glibc2.0 users. */
54#ifndef SIOCSIFTXQLEN
55#define SIOCSIFTXQLEN 0x8943
56#define SIOCGIFTXQLEN 0x8942
57#endif
Eric Andersenec455952001-02-14 08:11:27 +000058
Manuel Novoa III df351d62001-03-08 22:57:00 +000059/* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
60#ifndef ifr_qlen
61#define ifr_qlen ifr_ifru.ifru_mtu
62#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +000063
Manuel Novoa III df351d62001-03-08 22:57:00 +000064#ifndef IFF_DYNAMIC
65#define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
66#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +000067
Manuel Novoa III df351d62001-03-08 22:57:00 +000068/*
69 * Here are the bit masks for the "flags" member of struct options below.
70 * N_ signifies no arg prefix; M_ signifies arg prefixed by '-'.
71 * CLR clears the flag; SET sets the flag; ARG signifies (optional) arg.
Eric Andersenf15d4da2001-03-06 00:48:59 +000072 */
Manuel Novoa III df351d62001-03-08 22:57:00 +000073#define N_CLR 0x01
74#define M_CLR 0x02
75#define N_SET 0x04
76#define M_SET 0x08
77#define N_ARG 0x10
78#define M_ARG 0x20
79
80#define M_MASK (M_CLR | M_SET | M_ARG)
81#define N_MASK (N_CLR | N_SET | N_ARG)
82#define SET_MASK (N_SET | M_SET)
83#define CLR_MASK (N_CLR | M_CLR)
84#define SET_CLR_MASK (SET_MASK | CLR_MASK)
85#define ARG_MASK (M_ARG | N_ARG)
86
87/*
88 * Here are the bit masks for the "arg_flags" member of struct options below.
89 */
90
91/*
92 * cast type:
93 * 00 int
94 * 01 char *
95 * 02 HOST_COPY in_ether
96 * 03 HOST_COPY INET_resolve
97 */
98#define A_CAST_TYPE 0x03
99/*
100 * map type:
101 * 00 not a map type (mem_start, io_addr, irq)
102 * 04 memstart (unsigned long)
103 * 08 io_addr (unsigned short)
104 * 0C irq (unsigned char)
105 */
106#define A_MAP_TYPE 0x0C
107#define A_ARG_REQ 0x10 /* Set if an arg is required. */
108#define A_NETMASK 0x20 /* Set if netmask (check for multiple sets). */
109#define A_SET_AFTER 0x40 /* Set a flag at the end. */
110#define A_COLON_CHK 0x80 /* Is this needed? See below. */
111
112/*
113 * These defines are for dealing with the A_CAST_TYPE field.
114 */
115#define A_CAST_CHAR_PTR 0x01
116#define A_CAST_RESOLVE 0x01
117#define A_CAST_HOST_COPY 0x02
118#define A_CAST_HOST_COPY_IN_ETHER A_CAST_HOST_COPY
119#define A_CAST_HOST_COPY_RESOLVE (A_CAST_HOST_COPY | A_CAST_RESOLVE)
120
121/*
122 * These defines are for dealing with the A_MAP_TYPE field.
123 */
124#define A_MAP_ULONG 0x04 /* memstart */
125#define A_MAP_USHORT 0x08 /* io_addr */
126#define A_MAP_UCHAR 0x0C /* irq */
127
128/*
129 * Define the bit masks signifying which operations to perform for each arg.
130 */
131
132#define ARG_METRIC (A_ARG_REQ /*| A_CAST_INT*/)
133#define ARG_MTU (A_ARG_REQ /*| A_CAST_INT*/)
134#define ARG_TXQUEUELEN (A_ARG_REQ /*| A_CAST_INT*/)
135#define ARG_MEM_START (A_ARG_REQ | A_MAP_ULONG)
Eric Andersen72f9a422001-10-28 05:12:20 +0000136#define ARG_IO_ADDR (A_ARG_REQ | A_MAP_ULONG)
Manuel Novoa III df351d62001-03-08 22:57:00 +0000137#define ARG_IRQ (A_ARG_REQ | A_MAP_UCHAR)
138#define ARG_DSTADDR (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE)
139#define ARG_NETMASK (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_NETMASK)
Manuel Novoa III 4fb0b512001-08-10 06:02:23 +0000140#define ARG_BROADCAST (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
Manuel Novoa III df351d62001-03-08 22:57:00 +0000141#define ARG_HW (A_ARG_REQ | A_CAST_HOST_COPY_IN_ETHER)
142#define ARG_POINTOPOINT (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
143#define ARG_KEEPALIVE (A_ARG_REQ | A_CAST_CHAR_PTR)
144#define ARG_OUTFILL (A_ARG_REQ | A_CAST_CHAR_PTR)
Manuel Novoa III 4fb0b512001-08-10 06:02:23 +0000145#define ARG_HOSTNAME (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_COLON_CHK)
Manuel Novoa III df351d62001-03-08 22:57:00 +0000146
147
148/*
149 * Set up the tables. Warning! They must have corresponding order!
150 */
151
152struct arg1opt {
153 const char *name;
154 unsigned short selector;
155 unsigned short ifr_offset;
156};
157
158struct options {
159 const char *name;
160 const unsigned char flags;
Manuel Novoa III 4fb0b512001-08-10 06:02:23 +0000161 const unsigned char arg_flags;
Manuel Novoa III df351d62001-03-08 22:57:00 +0000162 const unsigned short selector;
163};
164
165#define ifreq_offsetof(x) offsetof(struct ifreq, x)
166
167static const struct arg1opt Arg1Opt[] = {
168 {"SIOCSIFMETRIC", SIOCSIFMETRIC, ifreq_offsetof(ifr_metric)},
169 {"SIOCSIFMTU", SIOCSIFMTU, ifreq_offsetof(ifr_mtu)},
170 {"SIOCSIFTXQLEN", SIOCSIFTXQLEN, ifreq_offsetof(ifr_qlen)},
171 {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
172 {"SIOCSIFNETMASK", SIOCSIFNETMASK, ifreq_offsetof(ifr_netmask)},
173 {"SIOCSIFBRDADDR", SIOCSIFBRDADDR, ifreq_offsetof(ifr_broadaddr)},
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000174#ifdef CONFIG_FEATURE_IFCONFIG_HW
Manuel Novoa III df351d62001-03-08 22:57:00 +0000175 {"SIOCSIFHWADDR", SIOCSIFHWADDR, ifreq_offsetof(ifr_hwaddr)},
176#endif
177 {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
Eric Andersenf15d4da2001-03-06 00:48:59 +0000178#ifdef SIOCSKEEPALIVE
Manuel Novoa III df351d62001-03-08 22:57:00 +0000179 {"SIOCSKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data)},
Eric Andersenf15d4da2001-03-06 00:48:59 +0000180#endif
181#ifdef SIOCSOUTFILL
Manuel Novoa III df351d62001-03-08 22:57:00 +0000182 {"SIOCSOUTFILL", SIOCSOUTFILL, ifreq_offsetof(ifr_data)},
Eric Andersenf15d4da2001-03-06 00:48:59 +0000183#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000184#ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
Manuel Novoa III df351d62001-03-08 22:57:00 +0000185 {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.mem_start)},
186 {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.base_addr)},
187 {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.irq)},
188#endif
189 /* Last entry if for unmatched (possibly hostname) arg. */
190 {"SIOCSIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr)},
Eric Andersenf15d4da2001-03-06 00:48:59 +0000191};
192
Manuel Novoa III df351d62001-03-08 22:57:00 +0000193static const struct options OptArray[] = {
194 {"metric", N_ARG, ARG_METRIC, 0},
195 {"mtu", N_ARG, ARG_MTU, 0},
196 {"txqueuelen", N_ARG, ARG_TXQUEUELEN, 0},
197 {"dstaddr", N_ARG, ARG_DSTADDR, 0},
198 {"netmask", N_ARG, ARG_NETMASK, 0},
199 {"broadcast", N_ARG | M_CLR, ARG_BROADCAST, IFF_BROADCAST},
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000200#ifdef CONFIG_FEATURE_IFCONFIG_HW
Manuel Novoa III df351d62001-03-08 22:57:00 +0000201 {"hw", N_ARG, ARG_HW, 0},
202#endif
203 {"pointopoint", N_ARG | M_CLR, ARG_POINTOPOINT, IFF_POINTOPOINT},
204#ifdef SIOCSKEEPALIVE
205 {"keepalive", N_ARG, ARG_KEEPALIVE, 0},
206#endif
207#ifdef SIOCSOUTFILL
208 {"outfill", N_ARG, ARG_OUTFILL, 0},
209#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000210#ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
Manuel Novoa III df351d62001-03-08 22:57:00 +0000211 {"mem_start", N_ARG, ARG_MEM_START, 0},
212 {"io_addr", N_ARG, ARG_IO_ADDR, 0},
213 {"irq", N_ARG, ARG_IRQ, 0},
214#endif
215 {"arp", N_CLR | M_SET, 0, IFF_NOARP},
216 {"trailers", N_CLR | M_SET, 0, IFF_NOTRAILERS},
217 {"promisc", N_SET | M_CLR, 0, IFF_PROMISC},
218 {"multicast", N_SET | M_CLR, 0, IFF_MULTICAST},
219 {"allmulti", N_SET | M_CLR, 0, IFF_ALLMULTI},
220 {"dynamic", N_SET | M_CLR, 0, IFF_DYNAMIC},
221 {"up", N_SET , 0, (IFF_UP | IFF_RUNNING)},
222 {"down", N_CLR , 0, IFF_UP},
223 { NULL, 0, ARG_HOSTNAME, (IFF_UP | IFF_RUNNING)}
224};
Eric Andersenf15d4da2001-03-06 00:48:59 +0000225
Manuel Novoa III df351d62001-03-08 22:57:00 +0000226/*
227 * A couple of prototypes.
228 */
Eric Andersenec455952001-02-14 08:11:27 +0000229
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000230#ifdef CONFIG_FEATURE_IFCONFIG_HW
Manuel Novoa III df351d62001-03-08 22:57:00 +0000231static int in_ether(char *bufp, struct sockaddr *sap);
232#endif
233
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000234#ifdef CONFIG_FEATURE_IFCONFIG_STATUS
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000235extern int interface_opt_a;
236extern int display_interfaces(char *ifname);
Manuel Novoa III df351d62001-03-08 22:57:00 +0000237#endif
238
239/*
240 * Our main function.
241 */
242
243int ifconfig_main(int argc, char **argv)
Eric Andersenec455952001-02-14 08:11:27 +0000244{
Manuel Novoa III df351d62001-03-08 22:57:00 +0000245 struct ifreq ifr;
246 struct sockaddr_in sai;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000247#ifdef CONFIG_FEATURE_IFCONFIG_HW
Manuel Novoa III df351d62001-03-08 22:57:00 +0000248 struct sockaddr sa;
249#endif
250 const struct arg1opt *a1op;
251 const struct options *op;
252 int sockfd; /* socket fd we use to manipulate stuff with */
253 int goterr;
254 int selector;
255 char *p;
256 char host[128];
Manuel Novoa III 4fb0b512001-08-10 06:02:23 +0000257 unsigned char mask;
258 unsigned char did_flags;
Eric Andersenec455952001-02-14 08:11:27 +0000259
Manuel Novoa III df351d62001-03-08 22:57:00 +0000260 goterr = 0;
261 did_flags = 0;
262
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000263 /* skip argv[0] */
264 ++argv;
265 --argc;
266
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000267#ifdef CONFIG_FEATURE_IFCONFIG_STATUS
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000268 if ((argc > 0) && (strcmp(*argv,"-a") == 0)) {
269 interface_opt_a = 1;
270 --argc;
271 ++argv;
272 }
273#endif
274
275 if(argc <= 1) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000276#ifdef CONFIG_FEATURE_IFCONFIG_STATUS
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000277 return display_interfaces(argc ? *argv : NULL);
Manuel Novoa III df351d62001-03-08 22:57:00 +0000278#else
Eric Andersen48dcc162001-03-15 20:48:45 +0000279 error_msg_and_die( "ifconfig was not compiled with interface status display support.");
Manuel Novoa III df351d62001-03-08 22:57:00 +0000280#endif
Eric Andersenec455952001-02-14 08:11:27 +0000281 }
Manuel Novoa III df351d62001-03-08 22:57:00 +0000282
283 /* Create a channel to the NET kernel. */
284 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
Manuel Novoa III fa45f222001-03-09 23:06:15 +0000285 perror_msg_and_die("socket");
Eric Andersenec455952001-02-14 08:11:27 +0000286 }
Manuel Novoa III df351d62001-03-08 22:57:00 +0000287
Manuel Novoa III df351d62001-03-08 22:57:00 +0000288 /* get interface name */
289 safe_strncpy(ifr.ifr_name, *argv, IFNAMSIZ);
290
291 /* Process the remaining arguments. */
292 while (*++argv != (char *) NULL) {
293 p = *argv;
294 mask = N_MASK;
295 if (*p == '-') { /* If the arg starts with '-'... */
296 ++p; /* advance past it and */
297 mask = M_MASK; /* set the appropriate mask. */
298 }
299 for (op = OptArray ; op->name ; op++) { /* Find table entry. */
Manuel Novoa III 78f57462001-03-10 02:00:54 +0000300 if (strcmp(p,op->name) == 0) { /* If name matches... */
Manuel Novoa III df351d62001-03-08 22:57:00 +0000301 if ((mask &= op->flags)) { /* set the mask and go. */
302 goto FOUND_ARG;;
303 }
304 /* If we get here, there was a valid arg with an */
305 /* invalid '-' prefix. */
306 ++goterr;
307 goto LOOP;
308 }
309 }
310
311 /* We fell through, so treat as possible hostname. */
312 a1op = Arg1Opt + (sizeof(Arg1Opt) / sizeof(Arg1Opt[0])) - 1;
313 mask = op->arg_flags;
314 goto HOSTNAME;
315
316 FOUND_ARG:
317 if (mask & ARG_MASK) {
318 mask = op->arg_flags;
319 a1op = Arg1Opt + (op - OptArray);
320 if (mask & A_NETMASK & did_flags) {
321 show_usage();
322 }
323 if (*++argv == NULL) {
324 if (mask & A_ARG_REQ) {
325 show_usage();
326 } else {
327 --argv;
328 mask &= A_SET_AFTER; /* just for broadcast */
329 }
330 } else { /* got an arg so process it */
331 HOSTNAME:
Manuel Novoa III 4fb0b512001-08-10 06:02:23 +0000332 did_flags |= (mask & A_NETMASK);
Manuel Novoa III df351d62001-03-08 22:57:00 +0000333 if (mask & A_CAST_HOST_COPY) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000334#ifdef CONFIG_FEATURE_IFCONFIG_HW
Manuel Novoa III df351d62001-03-08 22:57:00 +0000335 if (mask & A_CAST_RESOLVE) {
336#endif
337 safe_strncpy(host, *argv, (sizeof host));
338 sai.sin_family = AF_INET;
339 sai.sin_port = 0;
Eric Andersencd8c4362001-11-10 11:22:46 +0000340 if (!strcmp(host, bb_INET_default)) {
Manuel Novoa III df351d62001-03-08 22:57:00 +0000341 /* Default is special, meaning 0.0.0.0. */
342 sai.sin_addr.s_addr = INADDR_ANY;
343 } else if (inet_aton(host, &sai.sin_addr) == 0) {
344 /* It's not a dotted quad. */
345 ++goterr;
346 continue;
347 }
348 p = (char *) &sai;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000349#ifdef CONFIG_FEATURE_IFCONFIG_HW
Manuel Novoa III df351d62001-03-08 22:57:00 +0000350 } else { /* A_CAST_HOST_COPY_IN_ETHER */
351 /* This is the "hw" arg case. */
352 if (strcmp("ether", *argv) || (*++argv == NULL)) {
353 show_usage();
354 }
355 safe_strncpy(host, *argv, (sizeof host));
356 if (in_ether(host, &sa)) {
Eric Andersen4acf8f82001-10-28 09:36:48 +0000357 error_msg("invalid hw-addr %s", host);
Manuel Novoa III df351d62001-03-08 22:57:00 +0000358 ++goterr;
359 continue;
360 }
361 p = (char *) &sa;
362 }
363#endif
364 memcpy((((char *)(&ifr)) + a1op->ifr_offset),
365 p, sizeof(struct sockaddr));
366 } else {
367 unsigned int i = strtoul(*argv,NULL,0);
368 p = ((char *)(&ifr)) + a1op->ifr_offset;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000369#ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
Manuel Novoa III df351d62001-03-08 22:57:00 +0000370 if (mask & A_MAP_TYPE) {
371 if (ioctl(sockfd, SIOCGIFMAP, &ifr) < 0) {
372 ++goterr;
373 continue;
374 }
375 if ((mask & A_MAP_UCHAR) == A_MAP_UCHAR) {
376 *((unsigned char *) p) = i;
377 } else if (mask & A_MAP_USHORT) {
378 *((unsigned short *) p) = i;
379 } else {
380 *((unsigned long *) p) = i;
381 }
382 } else
383#endif
384 if (mask & A_CAST_CHAR_PTR) {
385 *((caddr_t *) p) = (caddr_t) i;
386 } else { /* A_CAST_INT */
387 *((int *) p) = i;
388 }
389 }
390
391 if (ioctl(sockfd, a1op->selector, &ifr) < 0) {
392 perror(a1op->name);
393 ++goterr;
394 continue;
395 }
396
397#ifdef QUESTIONABLE_ALIAS_CASE
398 if (mask & A_COLON_CHK) {
399 /*
400 * Don't do the set_flag() if the address is an alias with
401 * a - at the end, since it's deleted already! - Roman
402 *
403 * Should really use regex.h here, not sure though how well
404 * it'll go with the cross-platform support etc.
405 */
406 char *ptr;
407 short int found_colon = 0;
408 for (ptr = ifr.ifr_name; *ptr; ptr++ ) {
409 if (*ptr == ':') {
410 found_colon++;
411 }
412 }
413
414 if (found_colon && *(ptr - 1) == '-') {
415 continue;
416 }
417 }
418#endif
419 }
420 if (!(mask & A_SET_AFTER)) {
421 continue;
422 }
423 mask = N_SET;
424 }
425
426 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
427 perror("SIOCGIFFLAGS");
428 ++goterr;
429 } else {
430 selector = op->selector;
431 if (mask & SET_MASK) {
432 ifr.ifr_flags |= selector;
433 } else {
434 ifr.ifr_flags &= ~selector;
435 }
436 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
437 perror("SIOCSIFFLAGS");
438 ++goterr;
439 }
440 }
441 LOOP:
442 } /* end of while-loop */
443
444 return goterr;
Eric Andersenec455952001-02-14 08:11:27 +0000445}
446
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000447#ifdef CONFIG_FEATURE_IFCONFIG_HW
Eric Andersenec455952001-02-14 08:11:27 +0000448/* Input an Ethernet address and convert to binary. */
449static int
450in_ether(char *bufp, struct sockaddr *sap)
451{
452 unsigned char *ptr;
Manuel Novoa III df351d62001-03-08 22:57:00 +0000453 int i, j;
454 unsigned char val;
455 unsigned char c;
Eric Andersenec455952001-02-14 08:11:27 +0000456
457 sap->sa_family = ARPHRD_ETHER;
458 ptr = sap->sa_data;
459
Manuel Novoa III df351d62001-03-08 22:57:00 +0000460 for (i = 0 ; i < ETH_ALEN ; i++) {
Eric Andersenec455952001-02-14 08:11:27 +0000461 val = 0;
Manuel Novoa III df351d62001-03-08 22:57:00 +0000462
463 /* We might get a semicolon here - not required. */
464 if (i && (*bufp == ':')) {
Eric Andersenec455952001-02-14 08:11:27 +0000465 bufp++;
Manuel Novoa III df351d62001-03-08 22:57:00 +0000466 }
467
468 for (j=0 ; j<2 ; j++) {
469 c = *bufp;
470 if (c >= '0' && c <= '9') {
471 c -= '0';
472 } else if (c >= 'a' && c <= 'f') {
Manuel Novoa III 049dc252001-03-26 16:26:16 +0000473 c -= ('a' - 10);
Manuel Novoa III df351d62001-03-08 22:57:00 +0000474 } else if (c >= 'A' && c <= 'F') {
Manuel Novoa III 049dc252001-03-26 16:26:16 +0000475 c -= ('A' - 10);
Manuel Novoa III df351d62001-03-08 22:57:00 +0000476 } else if (j && (c == ':' || c == 0)) {
477 break;
478 } else {
479 return -1;
480 }
481 ++bufp;
482 val <<= 4;
483 val += c;
484 }
485 *ptr++ = val;
Eric Andersenec455952001-02-14 08:11:27 +0000486 }
487
Manuel Novoa III df351d62001-03-08 22:57:00 +0000488 return (int) (*bufp); /* Error if we don't end at end of string. */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000489}
490#endif