2 * ifconfig This file contains an implementation of the command
3 * that either displays or sets the characteristics of
4 * one or more of the system's networking interfaces.
6 * Version: $Id: ifconfig.c,v 1.47 2001/04/08 17:05:05 pb Exp $
8 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
9 * and others. Copyright 1993 MicroWalt Corporation
11 * This program is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU General
13 * Public License as published by the Free Software
14 * Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * Patched to support 'add' and 'del' keywords for INET(4) addresses
18 * by Mrs. Brisby <mrs.brisby@nimh.org>
20 * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
21 * - gettext instead of catgets for i18n
22 * 10/1998 - Andi Kleen. Use interface list primitives.
23 * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu
24 * (default AF was wrong)
25 * 20010404 - Arnaldo Carvalho de Melo, use setlocale
28 #define DFLT_AF "inet"
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <netinet/in.h>
38 #include <net/if_arp.h>
48 /* Ugh. But libc5 doesn't provide POSIX types. */
49 #include <asm/types.h>
53 #include <linux/if_slip.h>
60 * This is in linux/include/net/ipv6.h.
64 struct in6_addr ifr6_addr
;
66 unsigned int ifr6_ifindex
;
71 #endif /* HAVE_AFINET6 */
74 #if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
75 #include <netipx/ipx.h>
80 #include "net-support.h"
81 #include "pathnames.h"
84 #include "interface.h"
88 char *Release
= RELEASE
, *Version
= "ifconfig 1.41 (2001-04-01)";
90 int opt_a
= 0; /* show all interfaces */
91 int opt_i
= 0; /* show the statistics */
92 int opt_v
= 0; /* debugging output flag */
94 int addr_family
= 0; /* currently selected AF */
96 /* for ipv4 add/del modes */
97 static int get_nmbc_parent(char *parent
,
98 unsigned long *nm
, unsigned long *bc
);
99 static int set_ifstate(char *parent
,
106 static int if_print(char *ifname
)
111 printf(_("Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg\n"));
114 res
= for_all_interfaces(do_if_print
, &opt_a
);
116 struct interface
*ife
;
118 ife
= lookup_interface(ifname
);
119 res
= do_if_fetch(ife
);
126 /* Set a certain interface flag. */
127 static int set_flag(char *ifname
, short flag
)
131 strcpy(ifr
.ifr_name
, ifname
);
132 if (ioctl(skfd
, SIOCGIFFLAGS
, &ifr
) < 0) {
133 fprintf(stderr
, _("%s: unknown interface: %s\n"),
134 ifname
, strerror(errno
));
137 strcpy(ifr
.ifr_name
, ifname
);
138 ifr
.ifr_flags
|= flag
;
139 if (ioctl(skfd
, SIOCSIFFLAGS
, &ifr
) < 0) {
140 perror("SIOCSIFFLAGS");
146 /* Clear a certain interface flag. */
147 static int clr_flag(char *ifname
, short flag
)
152 if (strchr(ifname
, ':')) {
153 /* This is a v4 alias interface. Downing it via a socket for
154 another AF may have bad consequences. */
155 fd
= get_socket_for_af(AF_INET
);
157 fprintf(stderr
, _("No support for INET on this system.\n"));
163 strcpy(ifr
.ifr_name
, ifname
);
164 if (ioctl(fd
, SIOCGIFFLAGS
, &ifr
) < 0) {
165 fprintf(stderr
, _("%s: unknown interface: %s\n"),
166 ifname
, strerror(errno
));
169 strcpy(ifr
.ifr_name
, ifname
);
170 ifr
.ifr_flags
&= ~flag
;
171 if (ioctl(fd
, SIOCSIFFLAGS
, &ifr
) < 0) {
172 perror("SIOCSIFFLAGS");
178 static void usage(void)
180 fprintf(stderr
, _("Usage:\n ifconfig [-a] [-i] [-v] [-s] <interface> [[<AF>] <address>]\n"));
182 fprintf(stderr
, _(" [add <address>[/<prefixlen>]]\n"));
183 fprintf(stderr
, _(" [del <address>[/<prefixlen>]]\n"));
184 fprintf(stderr
, _(" [[-]broadcast [<address>]] [[-]pointopoint [<address>]]\n"));
185 fprintf(stderr
, _(" [netmask <address>] [dstaddr <address>] [tunnel <address>]\n"));
187 #ifdef SIOCSKEEPALIVE
188 fprintf(stderr
, _(" [outfill <NN>] [keepalive <NN>]\n"));
190 fprintf(stderr
, _(" [hw <HW> <address>] [metric <NN>] [mtu <NN>]\n"));
191 fprintf(stderr
, _(" [[-]trailers] [[-]arp] [[-]allmulti]\n"));
192 fprintf(stderr
, _(" [multicast] [[-]promisc]\n"));
193 fprintf(stderr
, _(" [mem_start <NN>] [io_addr <NN>] [irq <NN>] [media <type>]\n"));
194 #ifdef HAVE_TXQUEUELEN
195 fprintf(stderr
, _(" [txqueuelen <NN>]\n"));
198 fprintf(stderr
, _(" [[-]dynamic]\n"));
200 fprintf(stderr
, _(" [up|down] ...\n\n"));
202 fprintf(stderr
, _(" <HW>=Hardware Type.\n"));
203 fprintf(stderr
, _(" List of possible hardware types:\n"));
204 print_hwlist(0); /* 1 = ARPable */
205 fprintf(stderr
, _(" <AF>=Address family. Default: %s\n"), DFLT_AF
);
206 fprintf(stderr
, _(" List of possible address families:\n"));
207 print_aflist(0); /* 1 = routeable */
211 static void version(void)
213 fprintf(stderr
, "%s\n%s\n", Release
, Version
);
217 static int set_netmask(int skfd
, struct ifreq
*ifr
, struct sockaddr
*sa
)
221 memcpy((char *) &ifr
->ifr_netmask
, (char *) sa
,
222 sizeof(struct sockaddr
));
223 if (ioctl(skfd
, SIOCSIFNETMASK
, ifr
) < 0) {
224 fprintf(stderr
, "SIOCSIFNETMASK: %s\n",
231 int main(int argc
, char **argv
)
234 struct sockaddr_in sin
;
239 int goterr
= 0, didnetmask
= 0;
243 extern struct aftype inet6_aftype
;
244 struct sockaddr_in6 sa6
;
245 struct in6_ifreq ifr6
;
246 unsigned long prefix_len
;
250 extern struct aftype inet_aftype
;
254 setlocale (LC_ALL
, "");
255 bindtextdomain("net-tools", "/usr/share/locale");
256 textdomain("net-tools");
259 /* Find any options. */
262 while (argc
&& *argv
[0] == '-') {
263 if (!strcmp(*argv
, "-a"))
266 else if (!strcmp(*argv
, "-s"))
269 else if (!strcmp(*argv
, "-v"))
272 else if (!strcmp(*argv
, "-V") || !strcmp(*argv
, "-version") ||
273 !strcmp(*argv
, "--version"))
276 else if (!strcmp(*argv
, "-?") || !strcmp(*argv
, "-h") ||
277 !strcmp(*argv
, "-help") || !strcmp(*argv
, "--help"))
281 fprintf(stderr
, _("ifconfig: option `%s' not recognised.\n"),
283 fprintf(stderr
, _("ifconfig: `--help' gives usage information.\n"));
291 /* Create a channel to the NET kernel. */
292 if ((skfd
= sockets_open(0)) < 0) {
297 /* Do we have to show the current setup? */
299 int err
= if_print((char *) NULL
);
303 /* No. Fetch the interface name. */
305 safe_strncpy(ifr
.ifr_name
, *spp
++, IFNAMSIZ
);
306 if (*spp
== (char *) NULL
) {
307 int err
= if_print(ifr
.ifr_name
);
312 /* The next argument is either an address family name, or an option. */
313 if ((ap
= get_aftype(*spp
)) != NULL
)
314 spp
++; /* it was a AF name */
316 ap
= get_aftype(DFLT_AF
);
319 addr_family
= ap
->af
;
323 /* Process the remaining arguments. */
324 while (*spp
!= (char *) NULL
) {
325 if (!strcmp(*spp
, "arp")) {
326 goterr
|= clr_flag(ifr
.ifr_name
, IFF_NOARP
);
330 if (!strcmp(*spp
, "-arp")) {
331 goterr
|= set_flag(ifr
.ifr_name
, IFF_NOARP
);
336 if (!strcmp(*spp
, "media") || !strcmp(*spp
, "port")) {
339 if (!strcasecmp(*spp
, "auto")) {
340 goterr
|= set_flag(ifr
.ifr_name
, IFF_AUTOMEDIA
);
344 newport
= strtol(*spp
, &endp
, 10);
347 for (i
= 0; if_port_text
[i
][0] && newport
== -1; i
++) {
348 for (j
= 0; if_port_text
[i
][j
]; j
++) {
349 if (!strcasecmp(*spp
, if_port_text
[i
][j
])) {
358 fprintf(stderr
, _("Unknown media type.\n"));
361 if (ioctl(skfd
, SIOCGIFMAP
, &ifr
) < 0) {
362 perror("port: SIOCGIFMAP");
366 ifr
.ifr_map
.port
= newport
;
367 if (ioctl(skfd
, SIOCSIFMAP
, &ifr
) < 0) {
368 perror("port: SIOCSIFMAP");
377 if (!strcmp(*spp
, "trailers")) {
378 goterr
|= clr_flag(ifr
.ifr_name
, IFF_NOTRAILERS
);
382 if (!strcmp(*spp
, "-trailers")) {
383 goterr
|= set_flag(ifr
.ifr_name
, IFF_NOTRAILERS
);
387 if (!strcmp(*spp
, "promisc")) {
388 goterr
|= set_flag(ifr
.ifr_name
, IFF_PROMISC
);
392 if (!strcmp(*spp
, "-promisc")) {
393 goterr
|= clr_flag(ifr
.ifr_name
, IFF_PROMISC
);
397 if (!strcmp(*spp
, "multicast")) {
398 goterr
|= set_flag(ifr
.ifr_name
, IFF_MULTICAST
);
402 if (!strcmp(*spp
, "-multicast")) {
403 goterr
|= clr_flag(ifr
.ifr_name
, IFF_MULTICAST
);
407 if (!strcmp(*spp
, "allmulti")) {
408 goterr
|= set_flag(ifr
.ifr_name
, IFF_ALLMULTI
);
412 if (!strcmp(*spp
, "-allmulti")) {
413 goterr
|= clr_flag(ifr
.ifr_name
, IFF_ALLMULTI
);
417 if (!strcmp(*spp
, "up")) {
418 goterr
|= set_flag(ifr
.ifr_name
, (IFF_UP
| IFF_RUNNING
));
422 if (!strcmp(*spp
, "down")) {
423 goterr
|= clr_flag(ifr
.ifr_name
, IFF_UP
);
428 if (!strcmp(*spp
, "dynamic")) {
429 goterr
|= set_flag(ifr
.ifr_name
, IFF_DYNAMIC
);
433 if (!strcmp(*spp
, "-dynamic")) {
434 goterr
|= clr_flag(ifr
.ifr_name
, IFF_DYNAMIC
);
440 if (!strcmp(*spp
, "metric")) {
443 ifr
.ifr_metric
= atoi(*spp
);
444 if (ioctl(skfd
, SIOCSIFMETRIC
, &ifr
) < 0) {
445 fprintf(stderr
, "SIOCSIFMETRIC: %s\n", strerror(errno
));
451 if (!strcmp(*spp
, "mtu")) {
454 ifr
.ifr_mtu
= atoi(*spp
);
455 if (ioctl(skfd
, SIOCSIFMTU
, &ifr
) < 0) {
456 fprintf(stderr
, "SIOCSIFMTU: %s\n", strerror(errno
));
462 #ifdef SIOCSKEEPALIVE
463 if (!strcmp(*spp
, "keepalive")) {
466 ifr
.ifr_data
= (caddr_t
) atoi(*spp
);
467 if (ioctl(skfd
, SIOCSKEEPALIVE
, &ifr
) < 0) {
468 fprintf(stderr
, "SIOCSKEEPALIVE: %s\n", strerror(errno
));
477 if (!strcmp(*spp
, "outfill")) {
480 ifr
.ifr_data
= (caddr_t
) atoi(*spp
);
481 if (ioctl(skfd
, SIOCSOUTFILL
, &ifr
) < 0) {
482 fprintf(stderr
, "SIOCSOUTFILL: %s\n", strerror(errno
));
490 if (!strcmp(*spp
, "-broadcast")) {
491 goterr
|= clr_flag(ifr
.ifr_name
, IFF_BROADCAST
);
495 if (!strcmp(*spp
, "broadcast")) {
496 if (*++spp
!= NULL
) {
497 safe_strncpy(host
, *spp
, (sizeof host
));
498 if (ap
->input(0, host
, &sa
) < 0) {
504 memcpy((char *) &ifr
.ifr_broadaddr
, (char *) &sa
,
505 sizeof(struct sockaddr
));
506 if (ioctl(ap
->fd
, SIOCSIFBRDADDR
, &ifr
) < 0) {
507 fprintf(stderr
, "SIOCSIFBRDADDR: %s\n",
513 goterr
|= set_flag(ifr
.ifr_name
, IFF_BROADCAST
);
516 if (!strcmp(*spp
, "dstaddr")) {
519 safe_strncpy(host
, *spp
, (sizeof host
));
520 if (ap
->input(0, host
, &sa
) < 0) {
526 memcpy((char *) &ifr
.ifr_dstaddr
, (char *) &sa
,
527 sizeof(struct sockaddr
));
528 if (ioctl(ap
->fd
, SIOCSIFDSTADDR
, &ifr
) < 0) {
529 fprintf(stderr
, "SIOCSIFDSTADDR: %s\n",
536 if (!strcmp(*spp
, "netmask")) {
537 if (*++spp
== NULL
|| didnetmask
)
539 safe_strncpy(host
, *spp
, (sizeof host
));
540 if (ap
->input(0, host
, &sa
) < 0) {
547 goterr
= set_netmask(ap
->fd
, &ifr
, &sa
);
551 #ifdef HAVE_TXQUEUELEN
552 if (!strcmp(*spp
, "txqueuelen")) {
555 ifr
.ifr_qlen
= strtoul(*spp
, NULL
, 0);
556 if (ioctl(skfd
, SIOCSIFTXQLEN
, &ifr
) < 0) {
557 fprintf(stderr
, "SIOCSIFTXQLEN: %s\n", strerror(errno
));
565 if (!strcmp(*spp
, "mem_start")) {
568 if (ioctl(skfd
, SIOCGIFMAP
, &ifr
) < 0) {
569 fprintf(stderr
, "mem_start: SIOCGIFMAP: %s\n", strerror(errno
));
574 ifr
.ifr_map
.mem_start
= strtoul(*spp
, NULL
, 0);
575 if (ioctl(skfd
, SIOCSIFMAP
, &ifr
) < 0) {
576 fprintf(stderr
, "mem_start: SIOCSIFMAP: %s\n", strerror(errno
));
582 if (!strcmp(*spp
, "io_addr")) {
585 if (ioctl(skfd
, SIOCGIFMAP
, &ifr
) < 0) {
586 fprintf(stderr
, "io_addr: SIOCGIFMAP: %s\n", strerror(errno
));
591 ifr
.ifr_map
.base_addr
= strtol(*spp
, NULL
, 0);
592 if (ioctl(skfd
, SIOCSIFMAP
, &ifr
) < 0) {
593 fprintf(stderr
, "io_addr: SIOCSIFMAP: %s\n", strerror(errno
));
599 if (!strcmp(*spp
, "irq")) {
602 if (ioctl(skfd
, SIOCGIFMAP
, &ifr
) < 0) {
603 fprintf(stderr
, "irq: SIOCGIFMAP: %s\n", strerror(errno
));
608 ifr
.ifr_map
.irq
= atoi(*spp
);
609 if (ioctl(skfd
, SIOCSIFMAP
, &ifr
) < 0) {
610 fprintf(stderr
, "irq: SIOCSIFMAP: %s\n", strerror(errno
));
616 if (!strcmp(*spp
, "-pointopoint")) {
617 goterr
|= clr_flag(ifr
.ifr_name
, IFF_POINTOPOINT
);
621 if (!strcmp(*spp
, "pointopoint")) {
622 if (*(spp
+ 1) != NULL
) {
624 safe_strncpy(host
, *spp
, (sizeof host
));
625 if (ap
->input(0, host
, &sa
)) {
631 memcpy((char *) &ifr
.ifr_dstaddr
, (char *) &sa
,
632 sizeof(struct sockaddr
));
633 if (ioctl(ap
->fd
, SIOCSIFDSTADDR
, &ifr
) < 0) {
634 fprintf(stderr
, "SIOCSIFDSTADDR: %s\n",
639 goterr
|= set_flag(ifr
.ifr_name
, IFF_POINTOPOINT
);
644 if (!strcmp(*spp
, "hw")) {
647 if ((hw
= get_hwtype(*spp
)) == NULL
)
649 if (hw
->input
== NULL
) {
650 fprintf(stderr
, _("hw address type `%s' has no handler to set address. failed.\n"), *spp
);
657 safe_strncpy(host
, *spp
, (sizeof host
));
658 if (hw
->input(host
, &sa
) < 0) {
659 fprintf(stderr
, _("%s: invalid %s address.\n"), host
, hw
->name
);
664 memcpy((char *) &ifr
.ifr_hwaddr
, (char *) &sa
,
665 sizeof(struct sockaddr
));
666 if (ioctl(skfd
, SIOCSIFHWADDR
, &ifr
) < 0) {
667 fprintf(stderr
, "SIOCSIFHWADDR: %s\n",
674 #if HAVE_AFINET || HAVE_AFINET6
675 if (!strcmp(*spp
, "add")) {
679 if (strchr(*spp
, ':')) {
681 if ((cp
= strchr(*spp
, '/'))) {
682 prefix_len
= atol(cp
+ 1);
683 if ((prefix_len
< 0) || (prefix_len
> 128))
689 safe_strncpy(host
, *spp
, (sizeof host
));
690 if (inet6_aftype
.input(1, host
,
691 (struct sockaddr
*) &sa6
) < 0) {
692 inet6_aftype
.herror(host
);
697 memcpy((char *) &ifr6
.ifr6_addr
, (char *) &sa6
.sin6_addr
,
698 sizeof(struct in6_addr
));
700 fd
= get_socket_for_af(AF_INET6
);
703 _("No support for INET6 on this system.\n"));
708 if (ioctl(fd
, SIOGIFINDEX
, &ifr
) < 0) {
709 perror("SIOGIFINDEX");
714 ifr6
.ifr6_ifindex
= ifr
.ifr_ifindex
;
715 ifr6
.ifr6_prefixlen
= prefix_len
;
716 if (ioctl(fd
, SIOCSIFADDR
, &ifr6
) < 0) {
717 perror("SIOCSIFADDR");
725 { /* ipv4 address a.b.c.d */
726 unsigned long ip
, nm
, bc
;
727 safe_strncpy(host
, *spp
, (sizeof host
));
728 if (inet_aftype
.input(0, host
, (struct sockaddr
*)&sin
) < 0) {
734 fd
= get_socket_for_af(AF_INET
);
737 _("No support for INET on this system.\n"));
743 memcpy(&ip
, &sin
.sin_addr
.s_addr
, sizeof(unsigned long));
745 if (get_nmbc_parent(ifr
.ifr_name
, &nm
, &bc
) < 0) {
746 fprintf(stderr
, _("Interface %s not initialized\n"),
752 set_ifstate(ifr
.ifr_name
, ip
, nm
, bc
, 1);
758 fprintf(stderr
, _("Bad address.\n"));
763 #if HAVE_AFINET || HAVE_AFINET6
764 if (!strcmp(*spp
, "del")) {
770 if (strchr(*spp
, ':')) { /* INET6 */
771 if ((cp
= strchr(*spp
, '/'))) {
772 prefix_len
= atol(cp
+ 1);
773 if ((prefix_len
< 0) || (prefix_len
> 128))
779 safe_strncpy(host
, *spp
, (sizeof host
));
780 if (inet6_aftype
.input(1, host
,
781 (struct sockaddr
*) &sa6
) < 0) {
782 inet6_aftype
.herror(host
);
787 memcpy((char *) &ifr6
.ifr6_addr
, (char *) &sa6
.sin6_addr
,
788 sizeof(struct in6_addr
));
790 fd
= get_socket_for_af(AF_INET6
);
793 _("No support for INET6 on this system.\n"));
798 if (ioctl(fd
, SIOGIFINDEX
, &ifr
) < 0) {
799 perror("SIOGIFINDEX");
804 ifr6
.ifr6_ifindex
= ifr
.ifr_ifindex
;
805 ifr6
.ifr6_prefixlen
= prefix_len
;
806 if (ioctl(fd
, SIOCDIFADDR
, &ifr6
) < 0) {
807 fprintf(stderr
, "SIOCDIFADDR: %s\n",
817 /* ipv4 address a.b.c.d */
818 unsigned long ip
, nm
, bc
;
819 safe_strncpy(host
, *spp
, (sizeof host
));
820 if (inet_aftype
.input(0, host
, (struct sockaddr
*)&sin
) < 0) {
826 fd
= get_socket_for_af(AF_INET
);
828 fprintf(stderr
, _("No support for INET on this system.\n"));
834 memcpy(&ip
, &sin
.sin_addr
.s_addr
, sizeof(unsigned long));
836 if (get_nmbc_parent(ifr
.ifr_name
, &nm
, &bc
) < 0) {
837 fprintf(stderr
, _("Interface %s not initialized\n"),
843 set_ifstate(ifr
.ifr_name
, ip
, nm
, bc
, 0);
848 fprintf(stderr
, _("Bad address.\n"));
851 fprintf(stderr
, _("Address deletion not supported on this system.\n"));
856 if (!strcmp(*spp
, "tunnel")) {
859 if ((cp
= strchr(*spp
, '/'))) {
860 prefix_len
= atol(cp
+ 1);
861 if ((prefix_len
< 0) || (prefix_len
> 128))
867 safe_strncpy(host
, *spp
, (sizeof host
));
868 if (inet6_aftype
.input(1, host
, (struct sockaddr
*) &sa6
) < 0) {
869 inet6_aftype
.herror(host
);
874 memcpy((char *) &ifr6
.ifr6_addr
, (char *) &sa6
.sin6_addr
,
875 sizeof(struct in6_addr
));
877 fd
= get_socket_for_af(AF_INET6
);
879 fprintf(stderr
, _("No support for INET6 on this system.\n"));
884 if (ioctl(fd
, SIOGIFINDEX
, &ifr
) < 0) {
885 perror("SIOGIFINDEX");
890 ifr6
.ifr6_ifindex
= ifr
.ifr_ifindex
;
891 ifr6
.ifr6_prefixlen
= prefix_len
;
893 if (ioctl(fd
, SIOCSIFDSTADDR
, &ifr6
) < 0) {
894 fprintf(stderr
, "SIOCSIFDSTADDR: %s\n",
903 /* If the next argument is a valid hostname, assume OK. */
904 safe_strncpy(host
, *spp
, (sizeof host
));
906 /* FIXME: sa is too small for INET6 addresses, inet6 should use that too,
907 broadcast is unexpected */
909 switch (ap
->getmask(host
, &sa
, NULL
)) {
917 goterr
= set_netmask(skfd
, &ifr
, &sa
);
922 if (ap
->input(0, host
, &sa
) < 0) {
924 fprintf(stderr
, _("ifconfig: `--help' gives usage information.\n"));
926 memcpy((char *) &ifr
.ifr_addr
, (char *) &sa
, sizeof(struct sockaddr
));
928 int r
= 0; /* to shut gcc up */
932 fd
= get_socket_for_af(AF_INET
);
934 fprintf(stderr
, _("No support for INET on this system.\n"));
937 r
= ioctl(fd
, SIOCSIFADDR
, &ifr
);
942 fd
= get_socket_for_af(AF_ECONET
);
944 fprintf(stderr
, _("No support for ECONET on this system.\n"));
947 r
= ioctl(fd
, SIOCSIFADDR
, &ifr
);
952 _("Don't know how to set addresses for family %d.\n"), ap
->af
);
956 perror("SIOCSIFADDR");
961 * Don't do the set_flag() if the address is an alias with a - at the
962 * end, since it's deleted already! - Roman
964 * Should really use regex.h here, not sure though how well it'll go
965 * with the cross-platform support etc.
969 short int found_colon
= 0;
970 for (ptr
= ifr
.ifr_name
; *ptr
; ptr
++ )
971 if (*ptr
== ':') found_colon
++;
973 if (!(found_colon
&& *(ptr
- 1) == '-'))
974 goterr
|= set_flag(ifr
.ifr_name
, (IFF_UP
| IFF_RUNNING
));
988 static unsigned char searcher
[256];
990 static int set_ip_using(const char *name
, int c
, unsigned long ip
)
993 struct sockaddr_in sin
;
995 strcpy(ifr
.ifr_name
, name
);
996 memset(&sin
, 0, sizeof(struct sockaddr
));
997 sin
.sin_family
= AF_INET
;
998 sin
.sin_addr
.s_addr
= ip
;
999 memcpy(&ifr
.ifr_addr
, &sin
, sizeof(struct sockaddr
));
1000 if (ioctl(skfd
, c
, &ifr
) < 0)
1004 static int do_ifcmd(struct interface
*x
, struct ifcmd
*ptr
)
1007 struct sockaddr_in
*sin
;
1010 if (do_if_fetch(x
) < 0)
1012 if (strncmp(x
->name
, ptr
->base
, ptr
->baselen
) != 0)
1013 return 0; /* skip */
1014 z
= strchr(x
->name
, ':');
1018 for (e
= z
; *e
; e
++)
1019 if (*e
== '-') /* deleted */
1025 sin
= (struct sockaddr_in
*)&x
->dstaddr
;
1026 if (sin
->sin_addr
.s_addr
!= ptr
->addr
) {
1032 if (set_flag(x
->name
, IFF_UP
| IFF_RUNNING
) == -1)
1036 if (clr_flag(x
->name
, IFF_UP
) == -1)
1040 return 1; /* all done! */
1044 static int get_nmbc_parent(char *parent
,
1045 unsigned long *nm
, unsigned long *bc
)
1047 struct interface
*i
;
1048 struct sockaddr_in
*sin
;
1050 i
= lookup_interface(parent
);
1053 if (do_if_fetch(i
) < 0)
1055 sin
= (struct sockaddr_in
*)&i
->netmask
;
1056 memcpy(nm
, &sin
->sin_addr
.s_addr
, sizeof(unsigned long));
1057 sin
= (struct sockaddr_in
*)&i
->broadaddr
;
1058 memcpy(bc
, &sin
->sin_addr
.s_addr
, sizeof(unsigned long));
1062 static int set_ifstate(char *parent
, unsigned long ip
,
1063 unsigned long nm
, unsigned long bc
,
1071 pt
.baselen
= strlen(parent
);
1074 memset(searcher
, 0, sizeof(searcher
));
1075 i
= for_all_interfaces((int (*)(struct interface
*,void *))do_ifcmd
,
1082 /* add a new interface */
1083 for (i
= 0; i
< 256; i
++)
1084 if (searcher
[i
] == 0)
1087 return -1; /* FAILURE!!! out of ip addresses */
1089 sprintf(buf
, "%s:%d", parent
, i
);
1090 if (set_ip_using(buf
, SIOCSIFADDR
, ip
) == -1)
1092 if (set_ip_using(buf
, SIOCSIFNETMASK
, nm
) == -1)
1094 if (set_ip_using(buf
, SIOCSIFBRDADDR
, bc
) == -1)
1096 if (set_flag(buf
, IFF_BROADCAST
) == -1)