cal: make it NOEXEC
[oweals/busybox.git] / networking / ifconfig.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * ifconfig
4  *
5  * Similar to the standard Unix ifconfig, but with only the necessary
6  * parts for AF_INET, and without any printing of if info (for now).
7  *
8  * Bjorn Wesen, Axis Communications AB
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 source tree.
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 //config:config IFCONFIG
28 //config:       bool "ifconfig (12 kb)"
29 //config:       default y
30 //config:       select PLATFORM_LINUX
31 //config:       help
32 //config:       Ifconfig is used to configure the kernel-resident network interfaces.
33 //config:
34 //config:config FEATURE_IFCONFIG_STATUS
35 //config:       bool "Enable status reporting output (+7k)"
36 //config:       default y
37 //config:       depends on IFCONFIG
38 //config:       help
39 //config:       If ifconfig is called with no arguments it will display the status
40 //config:       of the currently active interfaces.
41 //config:
42 //config:config FEATURE_IFCONFIG_SLIP
43 //config:       bool "Enable slip-specific options \"keepalive\" and \"outfill\""
44 //config:       default y
45 //config:       depends on IFCONFIG
46 //config:       help
47 //config:       Allow "keepalive" and "outfill" support for SLIP. If you're not
48 //config:       planning on using serial lines, leave this unchecked.
49 //config:
50 //config:config FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
51 //config:       bool "Enable options \"mem_start\", \"io_addr\", and \"irq\""
52 //config:       default y
53 //config:       depends on IFCONFIG
54 //config:       help
55 //config:       Allow the start address for shared memory, start address for I/O,
56 //config:       and/or the interrupt line used by the specified device.
57 //config:
58 //config:config FEATURE_IFCONFIG_HW
59 //config:       bool "Enable option \"hw\" (ether only)"
60 //config:       default y
61 //config:       depends on IFCONFIG
62 //config:       help
63 //config:       Set the hardware address of this interface, if the device driver
64 //config:       supports  this  operation. Currently, we only support the 'ether'
65 //config:       class.
66 //config:
67 //config:config FEATURE_IFCONFIG_BROADCAST_PLUS
68 //config:       bool "Set the broadcast automatically"
69 //config:       default y
70 //config:       depends on IFCONFIG
71 //config:       help
72 //config:       Setting this will make ifconfig attempt to find the broadcast
73 //config:       automatically if the value '+' is used.
74
75 //applet:IF_IFCONFIG(APPLET(ifconfig, BB_DIR_SBIN, BB_SUID_DROP))
76
77 //kbuild:lib-$(CONFIG_IFCONFIG) += ifconfig.o interface.o
78
79 //usage:#define ifconfig_trivial_usage
80 //usage:        IF_FEATURE_IFCONFIG_STATUS("[-a]") " interface [address]"
81 //usage:#define ifconfig_full_usage "\n\n"
82 //usage:       "Configure a network interface\n"
83 //usage:     "\n"
84 //usage:        IF_FEATURE_IPV6(
85 //usage:       "        [add ADDRESS[/PREFIXLEN]]\n")
86 //usage:        IF_FEATURE_IPV6(
87 //usage:       "        [del ADDRESS[/PREFIXLEN]]\n")
88 //usage:       "        [[-]broadcast [ADDRESS]] [[-]pointopoint [ADDRESS]]\n"
89 //usage:       "        [netmask ADDRESS] [dstaddr ADDRESS]\n"
90 //usage:        IF_FEATURE_IFCONFIG_SLIP(
91 //usage:       "        [outfill NN] [keepalive NN]\n")
92 //usage:       "        " IF_FEATURE_IFCONFIG_HW("[hw ether" IF_FEATURE_HWIB("|infiniband")" ADDRESS] ") "[metric NN] [mtu NN]\n"
93 //usage:       "        [[-]trailers] [[-]arp] [[-]allmulti]\n"
94 //usage:       "        [multicast] [[-]promisc] [txqueuelen NN] [[-]dynamic]\n"
95 //usage:        IF_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ(
96 //usage:       "        [mem_start NN] [io_addr NN] [irq NN]\n")
97 //usage:       "        [up|down] ..."
98
99 #include "libbb.h"
100 #include "inet_common.h"
101 #include <net/if.h>
102 #include <net/if_arp.h>
103 #include <netinet/in.h>
104 #ifdef HAVE_NET_ETHERNET_H
105 # include <net/ethernet.h>
106 #endif
107
108 #if ENABLE_FEATURE_IFCONFIG_SLIP
109 # include <linux/if_slip.h>
110 #endif
111
112 /* I don't know if this is needed for busybox or not.  Anyone? */
113 #define QUESTIONABLE_ALIAS_CASE
114
115
116 /* Defines for glibc2.0 users. */
117 #ifndef SIOCSIFTXQLEN
118 # define SIOCSIFTXQLEN      0x8943
119 # define SIOCGIFTXQLEN      0x8942
120 #endif
121
122 /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */
123 #ifndef ifr_qlen
124 # define ifr_qlen        ifr_ifru.ifru_mtu
125 #endif
126
127 #ifndef IFF_DYNAMIC
128 # define IFF_DYNAMIC     0x8000 /* dialup device with changing addresses */
129 #endif
130
131 #if ENABLE_FEATURE_IPV6
132 struct in6_ifreq {
133         struct in6_addr ifr6_addr;
134         uint32_t ifr6_prefixlen;
135         int ifr6_ifindex;
136 };
137 #endif
138
139 /*
140  * Here are the bit masks for the "flags" member of struct options below.
141  * N_ signifies no arg prefix; M_ signifies arg prefixed by '-'.
142  * CLR clears the flag; SET sets the flag; ARG signifies (optional) arg.
143  */
144 #define N_CLR            0x01
145 #define M_CLR            0x02
146 #define N_SET            0x04
147 #define M_SET            0x08
148 #define N_ARG            0x10
149 #define M_ARG            0x20
150
151 #define M_MASK           (M_CLR | M_SET | M_ARG)
152 #define N_MASK           (N_CLR | N_SET | N_ARG)
153 #define SET_MASK         (N_SET | M_SET)
154 #define CLR_MASK         (N_CLR | M_CLR)
155 #define SET_CLR_MASK     (SET_MASK | CLR_MASK)
156 #define ARG_MASK         (M_ARG | N_ARG)
157
158 /*
159  * Here are the bit masks for the "arg_flags" member of struct options below.
160  */
161
162 /*
163  * cast type:
164  *   00 int
165  *   01 char *
166  *   02 HOST_COPY in_ether
167  *   03 HOST_COPY INET_resolve
168  */
169 #define A_CAST_TYPE      0x03
170 /*
171  * map type:
172  *   00 not a map type (mem_start, io_addr, irq)
173  *   04 memstart (unsigned long)
174  *   08 io_addr  (unsigned short)
175  *   0C irq      (unsigned char)
176  */
177 #define A_MAP_TYPE       0x0C
178 #define A_ARG_REQ        0x10   /* Set if an arg is required. */
179 #define A_NETMASK        0x20   /* Set if netmask (check for multiple sets). */
180 #define A_SET_AFTER      0x40   /* Set a flag at the end. */
181 #define A_COLON_CHK      0x80   /* Is this needed?  See below. */
182 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
183 #define A_HOSTNAME      0x100   /* Set if it is ip addr. */
184 #define A_BROADCAST     0x200   /* Set if it is broadcast addr. */
185 #else
186 #define A_HOSTNAME          0
187 #define A_BROADCAST         0
188 #endif
189
190 /*
191  * These defines are for dealing with the A_CAST_TYPE field.
192  */
193 #define A_CAST_CHAR_PTR  0x01
194 #define A_CAST_RESOLVE   0x01
195 #define A_CAST_HOST_COPY 0x02
196 #define A_CAST_HOST_COPY_IN_ETHER    A_CAST_HOST_COPY
197 #define A_CAST_HOST_COPY_RESOLVE     (A_CAST_HOST_COPY | A_CAST_RESOLVE)
198
199 /*
200  * These defines are for dealing with the A_MAP_TYPE field.
201  */
202 #define A_MAP_ULONG      0x04   /* memstart */
203 #define A_MAP_USHORT     0x08   /* io_addr */
204 #define A_MAP_UCHAR      0x0C   /* irq */
205
206 /*
207  * Define the bit masks signifying which operations to perform for each arg.
208  */
209
210 #define ARG_METRIC       (A_ARG_REQ /*| A_CAST_INT*/)
211 #define ARG_MTU          (A_ARG_REQ /*| A_CAST_INT*/)
212 #define ARG_TXQUEUELEN   (A_ARG_REQ /*| A_CAST_INT*/)
213 #define ARG_MEM_START    (A_ARG_REQ | A_MAP_ULONG)
214 #define ARG_IO_ADDR      (A_ARG_REQ | A_MAP_ULONG)
215 #define ARG_IRQ          (A_ARG_REQ | A_MAP_UCHAR)
216 #define ARG_DSTADDR      (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE)
217 #define ARG_NETMASK      (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_NETMASK)
218 #define ARG_BROADCAST    (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_BROADCAST)
219 #define ARG_HW           (A_ARG_REQ | A_CAST_HOST_COPY_IN_ETHER)
220 #define ARG_POINTOPOINT  (A_ARG_REQ | A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
221 #define ARG_KEEPALIVE    (A_ARG_REQ | A_CAST_CHAR_PTR)
222 #define ARG_OUTFILL      (A_ARG_REQ | A_CAST_CHAR_PTR)
223 #define ARG_HOSTNAME     (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER | A_COLON_CHK | A_HOSTNAME)
224 #define ARG_ADD_DEL      (A_CAST_HOST_COPY_RESOLVE | A_SET_AFTER)
225
226
227 struct arg1opt {
228         const char *name;
229         unsigned short selector;
230         unsigned short ifr_offset;
231 };
232
233 struct options {
234         const char *name;
235 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
236         const unsigned int flags:6;
237         const unsigned int arg_flags:10;
238 #else
239         const unsigned char flags;
240         const unsigned char arg_flags;
241 #endif
242         const unsigned short selector;
243 };
244
245 #define ifreq_offsetof(x)  offsetof(struct ifreq, x)
246
247 /*
248  * Set up the tables.  Warning!  They must have corresponding order!
249  */
250
251 static const struct arg1opt Arg1Opt[] = {
252         { "SIFMETRIC",  SIOCSIFMETRIC,  ifreq_offsetof(ifr_metric) },
253         { "SIFMTU",     SIOCSIFMTU,     ifreq_offsetof(ifr_mtu) },
254         { "SIFTXQLEN",  SIOCSIFTXQLEN,  ifreq_offsetof(ifr_qlen) },
255         { "SIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr) },
256         { "SIFNETMASK", SIOCSIFNETMASK, ifreq_offsetof(ifr_netmask) },
257         { "SIFBRDADDR", SIOCSIFBRDADDR, ifreq_offsetof(ifr_broadaddr) },
258 #if ENABLE_FEATURE_IFCONFIG_HW
259         { "SIFHWADDR",  SIOCSIFHWADDR,  ifreq_offsetof(ifr_hwaddr) },
260 #endif
261         { "SIFDSTADDR", SIOCSIFDSTADDR, ifreq_offsetof(ifr_dstaddr) },
262 #ifdef SIOCSKEEPALIVE
263         { "SKEEPALIVE", SIOCSKEEPALIVE, ifreq_offsetof(ifr_data) },
264 #endif
265 #ifdef SIOCSOUTFILL
266         { "SOUTFILL",   SIOCSOUTFILL,   ifreq_offsetof(ifr_data) },
267 #endif
268 #if ENABLE_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
269         { "SIFMAP",     SIOCSIFMAP,     ifreq_offsetof(ifr_map.mem_start) },
270         { "SIFMAP",     SIOCSIFMAP,     ifreq_offsetof(ifr_map.base_addr) },
271         { "SIFMAP",     SIOCSIFMAP,     ifreq_offsetof(ifr_map.irq) },
272 #endif
273 #if ENABLE_FEATURE_IPV6
274         { "SIFADDR",    SIOCSIFADDR,    ifreq_offsetof(ifr_addr) }, /* IPv6 version ignores the offset */
275         { "DIFADDR",    SIOCDIFADDR,    ifreq_offsetof(ifr_addr) }, /* IPv6 version ignores the offset */
276 #endif
277         /* Last entry is for unmatched (assumed to be hostname/address) arg. */
278         { "SIFADDR",    SIOCSIFADDR,    ifreq_offsetof(ifr_addr) },
279 };
280
281 static const struct options OptArray[] = {
282         { "metric",      N_ARG,         ARG_METRIC,      0 },
283         { "mtu",         N_ARG,         ARG_MTU,         0 },
284         { "txqueuelen",  N_ARG,         ARG_TXQUEUELEN,  0 },
285         { "dstaddr",     N_ARG,         ARG_DSTADDR,     0 },
286         { "netmask",     N_ARG,         ARG_NETMASK,     0 },
287         { "broadcast",   N_ARG | M_CLR, ARG_BROADCAST,   IFF_BROADCAST },
288 #if ENABLE_FEATURE_IFCONFIG_HW
289         { "hw",          N_ARG,         ARG_HW,          0 },
290 #endif
291         { "pointopoint", N_ARG | M_CLR, ARG_POINTOPOINT, IFF_POINTOPOINT },
292 #ifdef SIOCSKEEPALIVE
293         { "keepalive",   N_ARG,         ARG_KEEPALIVE,   0 },
294 #endif
295 #ifdef SIOCSOUTFILL
296         { "outfill",     N_ARG,         ARG_OUTFILL,     0 },
297 #endif
298 #if ENABLE_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
299         { "mem_start",   N_ARG,         ARG_MEM_START,   0 },
300         { "io_addr",     N_ARG,         ARG_IO_ADDR,     0 },
301         { "irq",         N_ARG,         ARG_IRQ,         0 },
302 #endif
303 #if ENABLE_FEATURE_IPV6
304         { "add",         N_ARG,         ARG_ADD_DEL,     0 },
305         { "del",         N_ARG,         ARG_ADD_DEL,     0 },
306 #endif
307         { "arp",         N_CLR | M_SET, 0,               IFF_NOARP },
308         { "trailers",    N_CLR | M_SET, 0,               IFF_NOTRAILERS },
309         { "promisc",     N_SET | M_CLR, 0,               IFF_PROMISC },
310         { "multicast",   N_SET | M_CLR, 0,               IFF_MULTICAST },
311         { "allmulti",    N_SET | M_CLR, 0,               IFF_ALLMULTI },
312         { "dynamic",     N_SET | M_CLR, 0,               IFF_DYNAMIC },
313         { "up",          N_SET,         0,               (IFF_UP | IFF_RUNNING) },
314         { "down",        N_CLR,         0,               IFF_UP },
315         { NULL,          0,             ARG_HOSTNAME,    (IFF_UP | IFF_RUNNING) }
316 };
317
318 int ifconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
319 int ifconfig_main(int argc UNUSED_PARAM, char **argv)
320 {
321         struct ifreq ifr;
322         struct sockaddr_in sai;
323 #if ENABLE_FEATURE_IFCONFIG_HW
324         struct sockaddr sa;
325 #endif
326         const struct arg1opt *a1op;
327         const struct options *op;
328         int sockfd;                     /* socket fd we use to manipulate stuff with */
329         int selector;
330 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
331         unsigned int mask;
332         unsigned int did_flags;
333         unsigned int sai_hostname, sai_netmask;
334 #else
335         unsigned char mask;
336         unsigned char did_flags;
337 #endif
338         char *p;
339         /*char host[128];*/
340         const char *host = NULL; /* make gcc happy */
341
342         did_flags = 0;
343 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
344         sai_hostname = 0;
345         sai_netmask = 0;
346 #endif
347
348         /* skip argv[0] */
349         ++argv;
350
351 #if ENABLE_FEATURE_IFCONFIG_STATUS
352         if (argv[0] && (argv[0][0] == '-' && argv[0][1] == 'a' && !argv[0][2])) {
353                 interface_opt_a = 1;
354                 ++argv;
355         }
356 #endif
357
358         if (!argv[0] || !argv[1]) { /* one or no args */
359 #if ENABLE_FEATURE_IFCONFIG_STATUS
360                 return display_interfaces(argv[0] /* can be NULL */);
361 #else
362                 bb_error_msg_and_die("no support for status display");
363 #endif
364         }
365
366         /* Create a channel to the NET kernel. */
367         sockfd = xsocket(AF_INET, SOCK_DGRAM, 0);
368
369         /* get interface name */
370         strncpy_IFNAMSIZ(ifr.ifr_name, *argv);
371
372         /* Process the remaining arguments. */
373         while (*++argv != NULL) {
374                 p = *argv;
375                 mask = N_MASK;
376                 if (*p == '-') {        /* If the arg starts with '-'... */
377                         ++p;            /*    advance past it and */
378                         mask = M_MASK;  /*    set the appropriate mask. */
379                 }
380                 for (op = OptArray; op->name; op++) {   /* Find table entry. */
381                         if (strcmp(p, op->name) == 0) { /* If name matches... */
382                                 mask &= op->flags;
383                                 if (mask)       /* set the mask and go. */
384                                         goto FOUND_ARG;
385                                 /* If we get here, there was a valid arg with an */
386                                 /* invalid '-' prefix. */
387                                 bb_error_msg_and_die("bad: '%s'", p-1);
388                         }
389                 }
390
391                 /* We fell through, so treat as possible hostname. */
392                 a1op = Arg1Opt + ARRAY_SIZE(Arg1Opt) - 1;
393                 mask = op->arg_flags;
394                 goto HOSTNAME;
395
396  FOUND_ARG:
397                 if (mask & ARG_MASK) {
398                         mask = op->arg_flags;
399                         if (mask & A_NETMASK & did_flags)
400                                 bb_show_usage();
401                         a1op = Arg1Opt + (op - OptArray);
402                         if (*++argv == NULL) {
403                                 if (mask & A_ARG_REQ)
404                                         bb_show_usage();
405                                 --argv;
406                                 mask &= A_SET_AFTER;    /* just for broadcast */
407                         } else {        /* got an arg so process it */
408  HOSTNAME:
409                                 did_flags |= (mask & (A_NETMASK|A_HOSTNAME));
410                                 if (mask & A_CAST_HOST_COPY) {
411 #if ENABLE_FEATURE_IFCONFIG_HW
412                                         if (mask & A_CAST_RESOLVE) {
413 #endif
414                                                 host = *argv;
415                                                 if (strcmp(host, "inet") == 0)
416                                                         continue; /* compat stuff */
417                                                 sai.sin_family = AF_INET;
418                                                 sai.sin_port = 0;
419                                                 if (strcmp(host, "default") == 0) {
420                                                         /* Default is special, meaning 0.0.0.0. */
421                                                         sai.sin_addr.s_addr = INADDR_ANY;
422                                                 }
423 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
424                                                 else if ((host[0] == '+' && !host[1])
425                                                  && (mask & A_BROADCAST)
426                                                  && (did_flags & (A_NETMASK|A_HOSTNAME)) == (A_NETMASK|A_HOSTNAME)
427                                                 ) {
428                                                         /* + is special, meaning broadcast is derived. */
429                                                         sai.sin_addr.s_addr = (~sai_netmask) | (sai_hostname & sai_netmask);
430                                                 }
431 #endif
432                                                 else {
433                                                         len_and_sockaddr *lsa;
434 #if ENABLE_FEATURE_IPV6
435                                                         char *prefix;
436                                                         int prefix_len = 0;
437                                                         prefix = strchr(host, '/');
438                                                         if (prefix) {
439                                                                 prefix_len = xatou_range(prefix + 1, 0, 128);
440                                                                 *prefix = '\0';
441                                                         }
442  resolve:
443 #endif
444                                                         lsa = xhost2sockaddr(host, 0);
445 #if ENABLE_FEATURE_IPV6
446                                                         if (lsa->u.sa.sa_family != AF_INET6 && prefix) {
447 /* TODO: we do not support "ifconfig eth0 up 1.2.3.4/17".
448  * For now, just make it fail instead of silently ignoring "/17" part:
449  */
450                                                                 *prefix = '/';
451                                                                 goto resolve;
452                                                         }
453                                                         if (lsa->u.sa.sa_family == AF_INET6) {
454                                                                 int sockfd6;
455                                                                 struct in6_ifreq ifr6;
456
457                                                                 sockfd6 = xsocket(AF_INET6, SOCK_DGRAM, 0);
458                                                                 xioctl(sockfd6, SIOCGIFINDEX, &ifr);
459                                                                 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
460                                                                 ifr6.ifr6_prefixlen = prefix_len;
461                                                                 memcpy(&ifr6.ifr6_addr,
462                                                                                 &lsa->u.sin6.sin6_addr,
463                                                                                 sizeof(struct in6_addr));
464                                                                 ioctl_or_perror_and_die(sockfd6, a1op->selector, &ifr6, "SIOC%s", a1op->name);
465                                                                 if (ENABLE_FEATURE_CLEAN_UP)
466                                                                         free(lsa);
467                                                                 continue;
468                                                         }
469 #endif
470                                                         sai.sin_addr = lsa->u.sin.sin_addr;
471                                                         if (ENABLE_FEATURE_CLEAN_UP)
472                                                                 free(lsa);
473                                                 }
474 #if ENABLE_FEATURE_IFCONFIG_BROADCAST_PLUS
475                                                 if (mask & A_HOSTNAME)
476                                                         sai_hostname = sai.sin_addr.s_addr;
477                                                 if (mask & A_NETMASK)
478                                                         sai_netmask = sai.sin_addr.s_addr;
479 #endif
480                                                 p = (char *) &sai;
481 #if ENABLE_FEATURE_IFCONFIG_HW
482                                         } else {        /* A_CAST_HOST_COPY_IN_ETHER */
483                                                 /* This is the "hw" arg case. */
484                                                 smalluint hw_class = index_in_substrings("ether\0"
485                                                                 IF_FEATURE_HWIB("infiniband\0"), *argv) + 1;
486                                                 if (!hw_class || !*++argv)
487                                                         bb_show_usage();
488                                                 host = *argv;
489                                                 if (hw_class == 1 ? in_ether(host, &sa) : in_ib(host, &sa))
490                                                         bb_error_msg_and_die("invalid hw-addr %s", host);
491                                                 p = (char *) &sa;
492                                         }
493 #endif
494                                         memcpy( ((char *)&ifr) + a1op->ifr_offset,
495                                                 p, sizeof(struct sockaddr));
496                                 } else {
497                                         /* FIXME: error check?? */
498                                         unsigned long i = strtoul(*argv, NULL, 0);
499                                         p = ((char *)&ifr) + a1op->ifr_offset;
500 #if ENABLE_FEATURE_IFCONFIG_MEMSTART_IOADDR_IRQ
501                                         if (mask & A_MAP_TYPE) {
502                                                 xioctl(sockfd, SIOCGIFMAP, &ifr);
503                                                 if ((mask & A_MAP_UCHAR) == A_MAP_UCHAR)
504                                                         *(unsigned char *) p = i;
505                                                 else if (mask & A_MAP_USHORT)
506                                                         *(unsigned short *) p = i;
507                                                 else
508                                                         *(unsigned long *) p = i;
509                                         } else
510 #endif
511                                         if (mask & A_CAST_CHAR_PTR)
512                                                 *(caddr_t *) p = (caddr_t) i;
513                                         else    /* A_CAST_INT */
514                                                 *(int *) p = i;
515                                 }
516
517                                 ioctl_or_perror_and_die(sockfd, a1op->selector, &ifr, "SIOC%s", a1op->name);
518 #ifdef QUESTIONABLE_ALIAS_CASE
519                                 if (mask & A_COLON_CHK) {
520                                         /*
521                                          * Don't do the set_flag() if the address is an alias with
522                                          * a '-' at the end, since it's deleted already! - Roman
523                                          *
524                                          * Should really use regex.h here, not sure though how well
525                                          * it'll go with the cross-platform support etc.
526                                          */
527                                         char *ptr;
528                                         short int found_colon = 0;
529                                         for (ptr = ifr.ifr_name; *ptr; ptr++)
530                                                 if (*ptr == ':')
531                                                         found_colon++;
532                                         if (found_colon && ptr[-1] == '-')
533                                                 continue;
534                                 }
535 #endif
536                         }
537                         if (!(mask & A_SET_AFTER))
538                                 continue;
539                         mask = N_SET;
540                 } /* if (mask & ARG_MASK) */
541
542                 xioctl(sockfd, SIOCGIFFLAGS, &ifr);
543                 selector = op->selector;
544                 if (mask & SET_MASK)
545                         ifr.ifr_flags |= selector;
546                 else
547                         ifr.ifr_flags &= ~selector;
548                 xioctl(sockfd, SIOCSIFFLAGS, &ifr);
549         } /* while () */
550
551         if (ENABLE_FEATURE_CLEAN_UP)
552                 close(sockfd);
553         return 0;
554 }