Latest patch from vodz:
[oweals/busybox.git] / networking / ifconfig.c
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.16 2001/11/10 11:22:43 andersen Exp $
19  *
20  */
21
22 /*
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  *
29  * Still missing:  media, tunnel.
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>   // strcmp and friends
35 #include <ctype.h>    // isdigit and friends
36 #include <stddef.h>                             /* offsetof */
37 #include <sys/ioctl.h>
38 #include <net/if_arp.h>
39 #include <netinet/in.h>
40 #include <linux/if_ether.h>
41 #include <net/if.h>
42 #include "inet_common.h"
43 #include "busybox.h"
44
45 #ifdef CONFIG_FEATURE_IFCONFIG_SLIP
46 #include <linux/if_slip.h>
47 #endif
48
49 /* I don't know if this is needed for busybox or not.  Anyone? */
50 #define QUESTIONABLE_ALIAS_CASE
51
52
53 /* Defines for glibc2.0 users. */
54 #ifndef SIOCSIFTXQLEN
55 #define SIOCSIFTXQLEN      0x8943
56 #define SIOCGIFTXQLEN      0x8942
57 #endif
58
59 /* 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
63
64 #ifndef IFF_DYNAMIC
65 #define IFF_DYNAMIC     0x8000  /* dialup device with changing addresses */
66 #endif
67
68 /*
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.
72  */
73 #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)
136 #define ARG_IO_ADDR      (A_ARG_REQ | A_MAP_ULONG)
137 #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)
140 #define ARG_BROADCAST    (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
141 #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)
145 #define ARG_HOSTNAME     (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_COLON_CHK)
146
147
148 /*
149  * Set up the tables.  Warning!  They must have corresponding order!
150  */
151
152 struct arg1opt {
153         const char *name;
154         unsigned short selector;
155         unsigned short ifr_offset;
156 };
157
158 struct options {
159         const char *name;
160         const unsigned char flags;
161         const unsigned char arg_flags;
162         const unsigned short selector;
163 };
164
165 #define ifreq_offsetof(x)  offsetof(struct ifreq, x)
166
167 static 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)},
174 #ifdef CONFIG_FEATURE_IFCONFIG_HW
175         {"SIOCSIFHWADDR",  SIOCSIFHWADDR,  ifreq_offsetof(ifr_hwaddr)},
176 #endif
177         {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)},
178 #ifdef SIOCSKEEPALIVE
179         {"SIOCSKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data)},
180 #endif
181 #ifdef SIOCSOUTFILL
182         {"SIOCSOUTFILL",   SIOCSOUTFILL,   ifreq_offsetof(ifr_data)},
183 #endif
184 #ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
185         {"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)},
191 };
192
193 static 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},
200 #ifdef CONFIG_FEATURE_IFCONFIG_HW
201         {"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
210 #ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
211         {"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 };
225
226 /*
227  * A couple of prototypes.
228  */
229
230 #ifdef CONFIG_FEATURE_IFCONFIG_HW
231 static int in_ether(char *bufp, struct sockaddr *sap);
232 #endif
233
234 #ifdef CONFIG_FEATURE_IFCONFIG_STATUS
235 extern int interface_opt_a;
236 extern int display_interfaces(char *ifname);
237 #endif
238
239 /*
240  * Our main function.
241  */
242
243 int ifconfig_main(int argc, char **argv)
244 {
245         struct ifreq ifr;
246         struct sockaddr_in sai;
247 #ifdef CONFIG_FEATURE_IFCONFIG_HW
248         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];
257         unsigned char mask;
258         unsigned char did_flags;
259
260         goterr = 0;
261         did_flags = 0;
262
263         /* skip argv[0] */
264         ++argv;
265         --argc;
266
267 #ifdef CONFIG_FEATURE_IFCONFIG_STATUS
268         if ((argc > 0) && (strcmp(*argv,"-a") == 0)) {
269                 interface_opt_a = 1;
270                 --argc;
271                 ++argv;
272         }
273 #endif
274
275         if(argc <= 1) {
276 #ifdef CONFIG_FEATURE_IFCONFIG_STATUS
277                 return display_interfaces(argc ? *argv : NULL);
278 #else
279                 error_msg_and_die( "ifconfig was not compiled with interface status display support.");
280 #endif
281         }
282
283         /* Create a channel to the NET kernel. */
284         if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
285                 perror_msg_and_die("socket");
286         }
287
288         /* 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. */
300                         if (strcmp(p,op->name) == 0) { /* If name matches... */
301                                 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:
332                                 did_flags |= (mask & A_NETMASK);
333                                 if (mask & A_CAST_HOST_COPY) {
334 #ifdef CONFIG_FEATURE_IFCONFIG_HW
335                                         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;
340                                                 if (!strcmp(host, bb_INET_default)) {
341                                                         /* 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;
349 #ifdef CONFIG_FEATURE_IFCONFIG_HW
350                                         } 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)) {
357                                                         error_msg("invalid hw-addr %s", host);
358                                                         ++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;
369 #ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
370                                         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;
445 }
446
447 #ifdef CONFIG_FEATURE_IFCONFIG_HW
448 /* Input an Ethernet address and convert to binary. */
449 static int
450 in_ether(char *bufp, struct sockaddr *sap)
451 {
452         unsigned char *ptr;
453         int i, j;
454         unsigned char val;
455         unsigned char c;
456         
457         sap->sa_family = ARPHRD_ETHER;
458         ptr = sap->sa_data;
459         
460         for (i = 0 ; i < ETH_ALEN ; i++) {
461                 val = 0;
462
463                 /* We might get a semicolon here - not required. */
464                 if (i && (*bufp == ':')) {
465                         bufp++;
466                 }
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') {
473                                 c -= ('a' - 10);
474                         } else if (c >= 'A' && c <= 'F') {
475                                 c -= ('A' - 10);
476                         } 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;
486         }
487
488         return (int) (*bufp);           /* Error if we don't end at end of string. */
489 }
490 #endif