blob: 3ada4f178d8ef3da905384a90fd542f6eec7df1d [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 Andersen2276d832002-07-11 11:11:56 +000018 * $Id: ifconfig.c,v 1.18 2002/07/11 11:11:52 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.
Eric Andersen51b8bd62002-07-03 11:46:38 +000030 *
31 * 2002-04-20
32 * IPV6 support added by Bart Visscher <magick@linux-fan.com>
Manuel Novoa III df351d62001-03-08 22:57:00 +000033 */
34
Eric Andersenec455952001-02-14 08:11:27 +000035#include <stdio.h>
36#include <stdlib.h>
Eric Andersenec455952001-02-14 08:11:27 +000037#include <string.h> // strcmp and friends
38#include <ctype.h> // isdigit and friends
Manuel Novoa III df351d62001-03-08 22:57:00 +000039#include <stddef.h> /* offsetof */
Eric Andersenec455952001-02-14 08:11:27 +000040#include <sys/ioctl.h>
Eric Andersenec455952001-02-14 08:11:27 +000041#include <net/if_arp.h>
Eric Andersencd8c4362001-11-10 11:22:46 +000042#include <netinet/in.h>
Eric Andersenec455952001-02-14 08:11:27 +000043#include <linux/if_ether.h>
Eric Andersencd8c4362001-11-10 11:22:46 +000044#include <net/if.h>
45#include "inet_common.h"
Manuel Novoa III df351d62001-03-08 22:57:00 +000046#include "busybox.h"
Eric Andersenec455952001-02-14 08:11:27 +000047
Eric Andersenbdfd0d72001-10-24 05:00:29 +000048#ifdef CONFIG_FEATURE_IFCONFIG_SLIP
Manuel Novoa III df351d62001-03-08 22:57:00 +000049#include <linux/if_slip.h>
Eric Andersenf15d4da2001-03-06 00:48:59 +000050#endif
51
Manuel Novoa III df351d62001-03-08 22:57:00 +000052/* I don't know if this is needed for busybox or not. Anyone? */
53#define QUESTIONABLE_ALIAS_CASE
Eric Andersenec455952001-02-14 08:11:27 +000054
55
Manuel Novoa III df351d62001-03-08 22:57:00 +000056/* Defines for glibc2.0 users. */
57#ifndef SIOCSIFTXQLEN
58#define SIOCSIFTXQLEN 0x8943
59#define SIOCGIFTXQLEN 0x8942
60#endif
Eric Andersenec455952001-02-14 08:11:27 +000061
Manuel Novoa III df351d62001-03-08 22:57:00 +000062/* 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
Eric Andersenf15d4da2001-03-06 00:48:59 +000066
Manuel Novoa III df351d62001-03-08 22:57:00 +000067#ifndef IFF_DYNAMIC
68#define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */
69#endif
Eric Andersenf15d4da2001-03-06 00:48:59 +000070
Eric Andersen51b8bd62002-07-03 11:46:38 +000071#if CONFIG_FEATURE_IPV6
72struct in6_ifreq {
73 struct in6_addr ifr6_addr;
74 uint32_t ifr6_prefixlen;
75 int ifr6_ifindex;
76};
77#endif
78
Manuel Novoa III df351d62001-03-08 22:57:00 +000079/*
80 * Here are the bit masks for the "flags" member of struct options below.
81 * N_ signifies no arg prefix; M_ signifies arg prefixed by '-'.
82 * CLR clears the flag; SET sets the flag; ARG signifies (optional) arg.
Eric Andersenf15d4da2001-03-06 00:48:59 +000083 */
Manuel Novoa III df351d62001-03-08 22:57:00 +000084#define N_CLR 0x01
85#define M_CLR 0x02
86#define N_SET 0x04
87#define M_SET 0x08
88#define N_ARG 0x10
89#define M_ARG 0x20
90
91#define M_MASK (M_CLR | M_SET | M_ARG)
92#define N_MASK (N_CLR | N_SET | N_ARG)
93#define SET_MASK (N_SET | M_SET)
94#define CLR_MASK (N_CLR | M_CLR)
95#define SET_CLR_MASK (SET_MASK | CLR_MASK)
96#define ARG_MASK (M_ARG | N_ARG)
97
98/*
99 * Here are the bit masks for the "arg_flags" member of struct options below.
100 */
101
102/*
103 * cast type:
104 * 00 int
105 * 01 char *
106 * 02 HOST_COPY in_ether
107 * 03 HOST_COPY INET_resolve
108 */
109#define A_CAST_TYPE 0x03
110/*
111 * map type:
112 * 00 not a map type (mem_start, io_addr, irq)
113 * 04 memstart (unsigned long)
114 * 08 io_addr (unsigned short)
115 * 0C irq (unsigned char)
116 */
117#define A_MAP_TYPE 0x0C
118#define A_ARG_REQ 0x10 /* Set if an arg is required. */
119#define A_NETMASK 0x20 /* Set if netmask (check for multiple sets). */
120#define A_SET_AFTER 0x40 /* Set a flag at the end. */
121#define A_COLON_CHK 0x80 /* Is this needed? See below. */
122
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/*
133 * These defines are for dealing with the A_MAP_TYPE field.
134 */
135#define A_MAP_ULONG 0x04 /* memstart */
136#define A_MAP_USHORT 0x08 /* io_addr */
137#define A_MAP_UCHAR 0x0C /* irq */
138
139/*
140 * Define the bit masks signifying which operations to perform for each arg.
141 */
142
143#define ARG_METRIC (A_ARG_REQ /*| A_CAST_INT*/)
144#define ARG_MTU (A_ARG_REQ /*| A_CAST_INT*/)
145#define ARG_TXQUEUELEN (A_ARG_REQ /*| A_CAST_INT*/)
146#define ARG_MEM_START (A_ARG_REQ | A_MAP_ULONG)
Eric Andersen72f9a422001-10-28 05:12:20 +0000147#define ARG_IO_ADDR (A_ARG_REQ | A_MAP_ULONG)
Manuel Novoa III df351d62001-03-08 22:57:00 +0000148#define ARG_IRQ (A_ARG_REQ | A_MAP_UCHAR)
149#define ARG_DSTADDR (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE)
150#define ARG_NETMASK (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_NETMASK)
Manuel Novoa III 4fb0b512001-08-10 06:02:23 +0000151#define ARG_BROADCAST (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
Manuel Novoa III df351d62001-03-08 22:57:00 +0000152#define ARG_HW (A_ARG_REQ | A_CAST_HOST_COPY_IN_ETHER)
153#define ARG_POINTOPOINT (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
154#define ARG_KEEPALIVE (A_ARG_REQ | A_CAST_CHAR_PTR)
155#define ARG_OUTFILL (A_ARG_REQ | A_CAST_CHAR_PTR)
Manuel Novoa III 4fb0b512001-08-10 06:02:23 +0000156#define ARG_HOSTNAME (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_COLON_CHK)
Eric Andersen51b8bd62002-07-03 11:46:38 +0000157#define ARG_ADD_DEL (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
Manuel Novoa III df351d62001-03-08 22:57:00 +0000158
159
160/*
161 * Set up the tables. Warning! They must have corresponding order!
162 */
163
164struct arg1opt {
165 const char *name;
166 unsigned short selector;
167 unsigned short ifr_offset;
168};
169
170struct options {
171 const char *name;
172 const unsigned char flags;
Manuel Novoa III 4fb0b512001-08-10 06:02:23 +0000173 const unsigned char arg_flags;
Manuel Novoa III df351d62001-03-08 22:57:00 +0000174 const unsigned short selector;
175};
176
177#define ifreq_offsetof(x) offsetof(struct ifreq, x)
178
179static const struct arg1opt Arg1Opt[] = {
180 {"SIOCSIFMETRIC", SIOCSIFMETRIC, ifreq_offsetof(ifr_metric)},
181 {"SIOCSIFMTU", SIOCSIFMTU, ifreq_offsetof(ifr_mtu)},
182 {"SIOCSIFTXQLEN", SIOCSIFTXQLEN, ifreq_offsetof(ifr_qlen)},
183 {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
184 {"SIOCSIFNETMASK", SIOCSIFNETMASK, ifreq_offsetof(ifr_netmask)},
185 {"SIOCSIFBRDADDR", SIOCSIFBRDADDR, ifreq_offsetof(ifr_broadaddr)},
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000186#ifdef CONFIG_FEATURE_IFCONFIG_HW
Manuel Novoa III df351d62001-03-08 22:57:00 +0000187 {"SIOCSIFHWADDR", SIOCSIFHWADDR, ifreq_offsetof(ifr_hwaddr)},
188#endif
189 {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
Eric Andersenf15d4da2001-03-06 00:48:59 +0000190#ifdef SIOCSKEEPALIVE
Manuel Novoa III df351d62001-03-08 22:57:00 +0000191 {"SIOCSKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data)},
Eric Andersenf15d4da2001-03-06 00:48:59 +0000192#endif
193#ifdef SIOCSOUTFILL
Manuel Novoa III df351d62001-03-08 22:57:00 +0000194 {"SIOCSOUTFILL", SIOCSOUTFILL, ifreq_offsetof(ifr_data)},
Eric Andersenf15d4da2001-03-06 00:48:59 +0000195#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000196#ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
Manuel Novoa III df351d62001-03-08 22:57:00 +0000197 {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.mem_start)},
198 {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.base_addr)},
199 {"SIOCSIFMAP", SIOCSIFMAP, ifreq_offsetof(ifr_map.irq)},
200#endif
201 /* Last entry if for unmatched (possibly hostname) arg. */
Eric Andersen51b8bd62002-07-03 11:46:38 +0000202#if CONFIG_FEATURE_IPV6
203 {"SIOCSIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr)}, /* IPv6 version ignores the offset */
204 {"SIOCDIFADDR", SIOCDIFADDR, ifreq_offsetof(ifr_addr)}, /* IPv6 version ignores the offset */
205#endif
Manuel Novoa III df351d62001-03-08 22:57:00 +0000206 {"SIOCSIFADDR", SIOCSIFADDR, ifreq_offsetof(ifr_addr)},
Eric Andersenf15d4da2001-03-06 00:48:59 +0000207};
208
Manuel Novoa III df351d62001-03-08 22:57:00 +0000209static const struct options OptArray[] = {
210 {"metric", N_ARG, ARG_METRIC, 0},
211 {"mtu", N_ARG, ARG_MTU, 0},
212 {"txqueuelen", N_ARG, ARG_TXQUEUELEN, 0},
213 {"dstaddr", N_ARG, ARG_DSTADDR, 0},
214 {"netmask", N_ARG, ARG_NETMASK, 0},
215 {"broadcast", N_ARG | M_CLR, ARG_BROADCAST, IFF_BROADCAST},
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000216#ifdef CONFIG_FEATURE_IFCONFIG_HW
Manuel Novoa III df351d62001-03-08 22:57:00 +0000217 {"hw", N_ARG, ARG_HW, 0},
218#endif
219 {"pointopoint", N_ARG | M_CLR, ARG_POINTOPOINT, IFF_POINTOPOINT},
220#ifdef SIOCSKEEPALIVE
221 {"keepalive", N_ARG, ARG_KEEPALIVE, 0},
222#endif
223#ifdef SIOCSOUTFILL
224 {"outfill", N_ARG, ARG_OUTFILL, 0},
225#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000226#ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
Manuel Novoa III df351d62001-03-08 22:57:00 +0000227 {"mem_start", N_ARG, ARG_MEM_START, 0},
228 {"io_addr", N_ARG, ARG_IO_ADDR, 0},
229 {"irq", N_ARG, ARG_IRQ, 0},
230#endif
Eric Andersen51b8bd62002-07-03 11:46:38 +0000231#if CONFIG_FEATURE_IPV6
232 {"add", N_ARG, ARG_ADD_DEL, 0},
233 {"del", N_ARG, ARG_ADD_DEL, 0},
234#endif
Manuel Novoa III df351d62001-03-08 22:57:00 +0000235 {"arp", N_CLR | M_SET, 0, IFF_NOARP},
236 {"trailers", N_CLR | M_SET, 0, IFF_NOTRAILERS},
237 {"promisc", N_SET | M_CLR, 0, IFF_PROMISC},
238 {"multicast", N_SET | M_CLR, 0, IFF_MULTICAST},
239 {"allmulti", N_SET | M_CLR, 0, IFF_ALLMULTI},
240 {"dynamic", N_SET | M_CLR, 0, IFF_DYNAMIC},
241 {"up", N_SET , 0, (IFF_UP | IFF_RUNNING)},
242 {"down", N_CLR , 0, IFF_UP},
243 { NULL, 0, ARG_HOSTNAME, (IFF_UP | IFF_RUNNING)}
244};
Eric Andersenf15d4da2001-03-06 00:48:59 +0000245
Manuel Novoa III df351d62001-03-08 22:57:00 +0000246/*
247 * A couple of prototypes.
248 */
Eric Andersenec455952001-02-14 08:11:27 +0000249
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000250#ifdef CONFIG_FEATURE_IFCONFIG_HW
Manuel Novoa III df351d62001-03-08 22:57:00 +0000251static int in_ether(char *bufp, struct sockaddr *sap);
252#endif
253
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000254#ifdef CONFIG_FEATURE_IFCONFIG_STATUS
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000255extern int interface_opt_a;
256extern int display_interfaces(char *ifname);
Manuel Novoa III df351d62001-03-08 22:57:00 +0000257#endif
258
259/*
260 * Our main function.
261 */
262
263int ifconfig_main(int argc, char **argv)
Eric Andersenec455952001-02-14 08:11:27 +0000264{
Manuel Novoa III df351d62001-03-08 22:57:00 +0000265 struct ifreq ifr;
266 struct sockaddr_in sai;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000267#if CONFIG_FEATURE_IPV6
268 struct sockaddr_in6 sai6;
269#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000270#ifdef CONFIG_FEATURE_IFCONFIG_HW
Manuel Novoa III df351d62001-03-08 22:57:00 +0000271 struct sockaddr sa;
272#endif
273 const struct arg1opt *a1op;
274 const struct options *op;
275 int sockfd; /* socket fd we use to manipulate stuff with */
276 int goterr;
277 int selector;
278 char *p;
279 char host[128];
Manuel Novoa III 4fb0b512001-08-10 06:02:23 +0000280 unsigned char mask;
281 unsigned char did_flags;
Eric Andersenec455952001-02-14 08:11:27 +0000282
Manuel Novoa III df351d62001-03-08 22:57:00 +0000283 goterr = 0;
284 did_flags = 0;
285
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000286 /* skip argv[0] */
287 ++argv;
288 --argc;
289
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000290#ifdef CONFIG_FEATURE_IFCONFIG_STATUS
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000291 if ((argc > 0) && (strcmp(*argv,"-a") == 0)) {
292 interface_opt_a = 1;
293 --argc;
294 ++argv;
295 }
296#endif
297
298 if(argc <= 1) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000299#ifdef CONFIG_FEATURE_IFCONFIG_STATUS
Manuel Novoa III 68ea1d02001-03-12 09:57:59 +0000300 return display_interfaces(argc ? *argv : NULL);
Manuel Novoa III df351d62001-03-08 22:57:00 +0000301#else
Eric Andersen48dcc162001-03-15 20:48:45 +0000302 error_msg_and_die( "ifconfig was not compiled with interface status display support.");
Manuel Novoa III df351d62001-03-08 22:57:00 +0000303#endif
Eric Andersenec455952001-02-14 08:11:27 +0000304 }
Manuel Novoa III df351d62001-03-08 22:57:00 +0000305
306 /* Create a channel to the NET kernel. */
307 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
Manuel Novoa III fa45f222001-03-09 23:06:15 +0000308 perror_msg_and_die("socket");
Eric Andersenec455952001-02-14 08:11:27 +0000309 }
Manuel Novoa III df351d62001-03-08 22:57:00 +0000310
Manuel Novoa III df351d62001-03-08 22:57:00 +0000311 /* get interface name */
312 safe_strncpy(ifr.ifr_name, *argv, IFNAMSIZ);
313
314 /* Process the remaining arguments. */
315 while (*++argv != (char *) NULL) {
316 p = *argv;
317 mask = N_MASK;
318 if (*p == '-') { /* If the arg starts with '-'... */
319 ++p; /* advance past it and */
320 mask = M_MASK; /* set the appropriate mask. */
321 }
322 for (op = OptArray ; op->name ; op++) { /* Find table entry. */
Manuel Novoa III 78f57462001-03-10 02:00:54 +0000323 if (strcmp(p,op->name) == 0) { /* If name matches... */
Manuel Novoa III df351d62001-03-08 22:57:00 +0000324 if ((mask &= op->flags)) { /* set the mask and go. */
325 goto FOUND_ARG;;
326 }
327 /* If we get here, there was a valid arg with an */
328 /* invalid '-' prefix. */
329 ++goterr;
330 goto LOOP;
331 }
332 }
333
334 /* We fell through, so treat as possible hostname. */
335 a1op = Arg1Opt + (sizeof(Arg1Opt) / sizeof(Arg1Opt[0])) - 1;
336 mask = op->arg_flags;
337 goto HOSTNAME;
338
339 FOUND_ARG:
340 if (mask & ARG_MASK) {
341 mask = op->arg_flags;
342 a1op = Arg1Opt + (op - OptArray);
343 if (mask & A_NETMASK & did_flags) {
344 show_usage();
345 }
346 if (*++argv == NULL) {
347 if (mask & A_ARG_REQ) {
348 show_usage();
349 } else {
350 --argv;
351 mask &= A_SET_AFTER; /* just for broadcast */
352 }
353 } else { /* got an arg so process it */
354 HOSTNAME:
Manuel Novoa III 4fb0b512001-08-10 06:02:23 +0000355 did_flags |= (mask & A_NETMASK);
Manuel Novoa III df351d62001-03-08 22:57:00 +0000356 if (mask & A_CAST_HOST_COPY) {
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000357#ifdef CONFIG_FEATURE_IFCONFIG_HW
Manuel Novoa III df351d62001-03-08 22:57:00 +0000358 if (mask & A_CAST_RESOLVE) {
359#endif
Eric Andersen51b8bd62002-07-03 11:46:38 +0000360#if CONFIG_FEATURE_IPV6
361 char *prefix;
362 int prefix_len=0;
363#endif
364
Manuel Novoa III df351d62001-03-08 22:57:00 +0000365 safe_strncpy(host, *argv, (sizeof host));
Eric Andersen51b8bd62002-07-03 11:46:38 +0000366#if CONFIG_FEATURE_IPV6
367 if ((prefix = strchr(host, '/'))) {
368 prefix_len = atol(prefix + 1);
369 if ((prefix_len < 0) || (prefix_len > 128)) {
370 ++goterr;
371 goto LOOP;
372 }
373 *prefix = 0;
374 }
375#endif
376
Manuel Novoa III df351d62001-03-08 22:57:00 +0000377 sai.sin_family = AF_INET;
378 sai.sin_port = 0;
Eric Andersencd8c4362001-11-10 11:22:46 +0000379 if (!strcmp(host, bb_INET_default)) {
Manuel Novoa III df351d62001-03-08 22:57:00 +0000380 /* Default is special, meaning 0.0.0.0. */
381 sai.sin_addr.s_addr = INADDR_ANY;
Eric Andersen51b8bd62002-07-03 11:46:38 +0000382#if CONFIG_FEATURE_IPV6
383 } else
384 if (inet_pton(AF_INET6, host, &sai6.sin6_addr) > 0) {
385 int sockfd6;
386 struct in6_ifreq ifr6;
387
388 memcpy((char *) &ifr6.ifr6_addr, (char *) &sai6.sin6_addr,
389 sizeof(struct in6_addr));
390
391 /* Create a channel to the NET kernel. */
392 if ((sockfd6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
393 perror_msg_and_die("socket6");
394 }
395 if (ioctl(sockfd6, SIOGIFINDEX, &ifr) < 0) {
396 perror("SIOGIFINDEX");
397 ++goterr;
398 continue;
399 }
400 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
401 ifr6.ifr6_prefixlen = prefix_len;
402 if (ioctl(sockfd6, a1op->selector, &ifr6) < 0) {
403 perror(a1op->name);
404 ++goterr;
405 }
406 continue;
407#endif
Manuel Novoa III df351d62001-03-08 22:57:00 +0000408 } else if (inet_aton(host, &sai.sin_addr) == 0) {
409 /* It's not a dotted quad. */
410 ++goterr;
411 continue;
412 }
413 p = (char *) &sai;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000414#ifdef CONFIG_FEATURE_IFCONFIG_HW
Manuel Novoa III df351d62001-03-08 22:57:00 +0000415 } else { /* A_CAST_HOST_COPY_IN_ETHER */
416 /* This is the "hw" arg case. */
417 if (strcmp("ether", *argv) || (*++argv == NULL)) {
418 show_usage();
419 }
420 safe_strncpy(host, *argv, (sizeof host));
421 if (in_ether(host, &sa)) {
Eric Andersen4acf8f82001-10-28 09:36:48 +0000422 error_msg("invalid hw-addr %s", host);
Manuel Novoa III df351d62001-03-08 22:57:00 +0000423 ++goterr;
424 continue;
425 }
426 p = (char *) &sa;
427 }
428#endif
429 memcpy((((char *)(&ifr)) + a1op->ifr_offset),
430 p, sizeof(struct sockaddr));
431 } else {
432 unsigned int i = strtoul(*argv,NULL,0);
433 p = ((char *)(&ifr)) + a1op->ifr_offset;
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000434#ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
Manuel Novoa III df351d62001-03-08 22:57:00 +0000435 if (mask & A_MAP_TYPE) {
436 if (ioctl(sockfd, SIOCGIFMAP, &ifr) < 0) {
437 ++goterr;
438 continue;
439 }
440 if ((mask & A_MAP_UCHAR) == A_MAP_UCHAR) {
441 *((unsigned char *) p) = i;
442 } else if (mask & A_MAP_USHORT) {
443 *((unsigned short *) p) = i;
444 } else {
445 *((unsigned long *) p) = i;
446 }
447 } else
448#endif
449 if (mask & A_CAST_CHAR_PTR) {
450 *((caddr_t *) p) = (caddr_t) i;
451 } else { /* A_CAST_INT */
452 *((int *) p) = i;
453 }
454 }
455
456 if (ioctl(sockfd, a1op->selector, &ifr) < 0) {
457 perror(a1op->name);
458 ++goterr;
459 continue;
460 }
461
462#ifdef QUESTIONABLE_ALIAS_CASE
463 if (mask & A_COLON_CHK) {
464 /*
465 * Don't do the set_flag() if the address is an alias with
466 * a - at the end, since it's deleted already! - Roman
467 *
468 * Should really use regex.h here, not sure though how well
469 * it'll go with the cross-platform support etc.
470 */
471 char *ptr;
472 short int found_colon = 0;
473 for (ptr = ifr.ifr_name; *ptr; ptr++ ) {
474 if (*ptr == ':') {
475 found_colon++;
476 }
477 }
478
479 if (found_colon && *(ptr - 1) == '-') {
480 continue;
481 }
482 }
483#endif
484 }
485 if (!(mask & A_SET_AFTER)) {
486 continue;
487 }
488 mask = N_SET;
489 }
490
491 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
492 perror("SIOCGIFFLAGS");
493 ++goterr;
494 } else {
495 selector = op->selector;
496 if (mask & SET_MASK) {
497 ifr.ifr_flags |= selector;
498 } else {
499 ifr.ifr_flags &= ~selector;
500 }
501 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
502 perror("SIOCSIFFLAGS");
503 ++goterr;
504 }
505 }
506 LOOP:
Eric Andersen2276d832002-07-11 11:11:56 +0000507 continue;
Manuel Novoa III df351d62001-03-08 22:57:00 +0000508 } /* end of while-loop */
509
510 return goterr;
Eric Andersenec455952001-02-14 08:11:27 +0000511}
512
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000513#ifdef CONFIG_FEATURE_IFCONFIG_HW
Eric Andersenec455952001-02-14 08:11:27 +0000514/* Input an Ethernet address and convert to binary. */
515static int
516in_ether(char *bufp, struct sockaddr *sap)
517{
518 unsigned char *ptr;
Manuel Novoa III df351d62001-03-08 22:57:00 +0000519 int i, j;
520 unsigned char val;
521 unsigned char c;
Eric Andersenec455952001-02-14 08:11:27 +0000522
523 sap->sa_family = ARPHRD_ETHER;
524 ptr = sap->sa_data;
525
Manuel Novoa III df351d62001-03-08 22:57:00 +0000526 for (i = 0 ; i < ETH_ALEN ; i++) {
Eric Andersenec455952001-02-14 08:11:27 +0000527 val = 0;
Manuel Novoa III df351d62001-03-08 22:57:00 +0000528
529 /* We might get a semicolon here - not required. */
530 if (i && (*bufp == ':')) {
Eric Andersenec455952001-02-14 08:11:27 +0000531 bufp++;
Manuel Novoa III df351d62001-03-08 22:57:00 +0000532 }
533
534 for (j=0 ; j<2 ; j++) {
535 c = *bufp;
536 if (c >= '0' && c <= '9') {
537 c -= '0';
538 } else if (c >= 'a' && c <= 'f') {
Manuel Novoa III 049dc252001-03-26 16:26:16 +0000539 c -= ('a' - 10);
Manuel Novoa III df351d62001-03-08 22:57:00 +0000540 } else if (c >= 'A' && c <= 'F') {
Manuel Novoa III 049dc252001-03-26 16:26:16 +0000541 c -= ('A' - 10);
Manuel Novoa III df351d62001-03-08 22:57:00 +0000542 } else if (j && (c == ':' || c == 0)) {
543 break;
544 } else {
545 return -1;
546 }
547 ++bufp;
548 val <<= 4;
549 val += c;
550 }
551 *ptr++ = val;
Eric Andersenec455952001-02-14 08:11:27 +0000552 }
553
Manuel Novoa III df351d62001-03-08 22:57:00 +0000554 return (int) (*bufp); /* Error if we don't end at end of string. */
Eric Andersenf15d4da2001-03-06 00:48:59 +0000555}
556#endif