Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 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 | * |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 18 | * $Id: ifconfig.c,v 1.16 2001/11/10 11:22:43 andersen Exp $ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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 | * |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 29 | * Still missing: media, tunnel. |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 34 | #include <string.h> // strcmp and friends |
| 35 | #include <ctype.h> // isdigit and friends |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 36 | #include <stddef.h> /* offsetof */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 37 | #include <sys/ioctl.h> |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 38 | #include <net/if_arp.h> |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 39 | #include <netinet/in.h> |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 40 | #include <linux/if_ether.h> |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 41 | #include <net/if.h> |
| 42 | #include "inet_common.h" |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 43 | #include "busybox.h" |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 44 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 45 | #ifdef CONFIG_FEATURE_IFCONFIG_SLIP |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 46 | #include <linux/if_slip.h> |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 49 | /* I don't know if this is needed for busybox or not. Anyone? */ |
| 50 | #define QUESTIONABLE_ALIAS_CASE |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 51 | |
| 52 | |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 53 | /* Defines for glibc2.0 users. */ |
| 54 | #ifndef SIOCSIFTXQLEN |
| 55 | #define SIOCSIFTXQLEN 0x8943 |
| 56 | #define SIOCGIFTXQLEN 0x8942 |
| 57 | #endif |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 58 | |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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 |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 63 | |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 64 | #ifndef IFF_DYNAMIC |
| 65 | #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */ |
| 66 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 67 | |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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. |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 72 | */ |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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) |
Eric Andersen | 72f9a42 | 2001-10-28 05:12:20 +0000 | [diff] [blame] | 136 | #define ARG_IO_ADDR (A_ARG_REQ | A_MAP_ULONG) |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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) |
Manuel Novoa III | 4fb0b51 | 2001-08-10 06:02:23 +0000 | [diff] [blame] | 140 | #define ARG_BROADCAST (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER) |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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) |
Manuel Novoa III | 4fb0b51 | 2001-08-10 06:02:23 +0000 | [diff] [blame] | 145 | #define ARG_HOSTNAME (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_COLON_CHK) |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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; |
Manuel Novoa III | 4fb0b51 | 2001-08-10 06:02:23 +0000 | [diff] [blame] | 161 | const unsigned char arg_flags; |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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)}, |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 174 | #ifdef CONFIG_FEATURE_IFCONFIG_HW |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 175 | {"SIOCSIFHWADDR", SIOCSIFHWADDR, ifreq_offsetof(ifr_hwaddr)}, |
| 176 | #endif |
| 177 | {"SIOCSIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr)}, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 178 | #ifdef SIOCSKEEPALIVE |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 179 | {"SIOCSKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data)}, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 180 | #endif |
| 181 | #ifdef SIOCSOUTFILL |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 182 | {"SIOCSOUTFILL", SIOCSOUTFILL, ifreq_offsetof(ifr_data)}, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 183 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 184 | #ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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)}, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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}, |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 200 | #ifdef CONFIG_FEATURE_IFCONFIG_HW |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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 |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 210 | #ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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 | }; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 225 | |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 226 | /* |
| 227 | * A couple of prototypes. |
| 228 | */ |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 229 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 230 | #ifdef CONFIG_FEATURE_IFCONFIG_HW |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 231 | static int in_ether(char *bufp, struct sockaddr *sap); |
| 232 | #endif |
| 233 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 234 | #ifdef CONFIG_FEATURE_IFCONFIG_STATUS |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 235 | extern int interface_opt_a; |
| 236 | extern int display_interfaces(char *ifname); |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 237 | #endif |
| 238 | |
| 239 | /* |
| 240 | * Our main function. |
| 241 | */ |
| 242 | |
| 243 | int ifconfig_main(int argc, char **argv) |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 244 | { |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 245 | struct ifreq ifr; |
| 246 | struct sockaddr_in sai; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 247 | #ifdef CONFIG_FEATURE_IFCONFIG_HW |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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]; |
Manuel Novoa III | 4fb0b51 | 2001-08-10 06:02:23 +0000 | [diff] [blame] | 257 | unsigned char mask; |
| 258 | unsigned char did_flags; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 259 | |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 260 | goterr = 0; |
| 261 | did_flags = 0; |
| 262 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 263 | /* skip argv[0] */ |
| 264 | ++argv; |
| 265 | --argc; |
| 266 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 267 | #ifdef CONFIG_FEATURE_IFCONFIG_STATUS |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 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) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 276 | #ifdef CONFIG_FEATURE_IFCONFIG_STATUS |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 277 | return display_interfaces(argc ? *argv : NULL); |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 278 | #else |
Eric Andersen | 48dcc16 | 2001-03-15 20:48:45 +0000 | [diff] [blame] | 279 | error_msg_and_die( "ifconfig was not compiled with interface status display support."); |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 280 | #endif |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 281 | } |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 282 | |
| 283 | /* Create a channel to the NET kernel. */ |
| 284 | if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
Manuel Novoa III | fa45f22 | 2001-03-09 23:06:15 +0000 | [diff] [blame] | 285 | perror_msg_and_die("socket"); |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 286 | } |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 287 | |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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. */ |
Manuel Novoa III | 78f5746 | 2001-03-10 02:00:54 +0000 | [diff] [blame] | 300 | if (strcmp(p,op->name) == 0) { /* If name matches... */ |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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: |
Manuel Novoa III | 4fb0b51 | 2001-08-10 06:02:23 +0000 | [diff] [blame] | 332 | did_flags |= (mask & A_NETMASK); |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 333 | if (mask & A_CAST_HOST_COPY) { |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 334 | #ifdef CONFIG_FEATURE_IFCONFIG_HW |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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; |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 340 | if (!strcmp(host, bb_INET_default)) { |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 349 | #ifdef CONFIG_FEATURE_IFCONFIG_HW |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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)) { |
Eric Andersen | 4acf8f8 | 2001-10-28 09:36:48 +0000 | [diff] [blame] | 357 | error_msg("invalid hw-addr %s", host); |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 369 | #ifdef CONFIG_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 447 | #ifdef CONFIG_FEATURE_IFCONFIG_HW |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 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; |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 453 | int i, j; |
| 454 | unsigned char val; |
| 455 | unsigned char c; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 456 | |
| 457 | sap->sa_family = ARPHRD_ETHER; |
| 458 | ptr = sap->sa_data; |
| 459 | |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 460 | for (i = 0 ; i < ETH_ALEN ; i++) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 461 | val = 0; |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 462 | |
| 463 | /* We might get a semicolon here - not required. */ |
| 464 | if (i && (*bufp == ':')) { |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 465 | bufp++; |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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') { |
Manuel Novoa III | 049dc25 | 2001-03-26 16:26:16 +0000 | [diff] [blame] | 473 | c -= ('a' - 10); |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 474 | } else if (c >= 'A' && c <= 'F') { |
Manuel Novoa III | 049dc25 | 2001-03-26 16:26:16 +0000 | [diff] [blame] | 475 | c -= ('A' - 10); |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 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; |
Eric Andersen | ec45595 | 2001-02-14 08:11:27 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Manuel Novoa III | df351d6 | 2001-03-08 22:57:00 +0000 | [diff] [blame] | 488 | return (int) (*bufp); /* Error if we don't end at end of string. */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 489 | } |
| 490 | #endif |