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.54 2001/11/01 03:00:13 ecki 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.42 (2001-04-13)";
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
, unsigned long *nm
,
99 static int set_ifstate(char *parent
, unsigned long ip
,
100 unsigned long nm
, unsigned long bc
,
103 static int if_print(char *ifname
)
108 printf(_("Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg\n"));
111 res
= for_all_interfaces(do_if_print
, &opt_a
);
113 struct interface
*ife
;
115 ife
= lookup_interface(ifname
);
116 res
= do_if_fetch(ife
);
123 /* Set a certain interface flag. */
124 static int set_flag(char *ifname
, short flag
)
128 safe_strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
129 if (ioctl(skfd
, SIOCGIFFLAGS
, &ifr
) < 0) {
130 fprintf(stderr
, _("%s: ERROR while getting interface flags: %s\n"),
131 ifname
, strerror(errno
));
134 safe_strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
135 ifr
.ifr_flags
|= flag
;
136 if (ioctl(skfd
, SIOCSIFFLAGS
, &ifr
) < 0) {
137 perror("SIOCSIFFLAGS");
143 /* Clear a certain interface flag. */
144 static int clr_flag(char *ifname
, short flag
)
149 if (strchr(ifname
, ':')) {
150 /* This is a v4 alias interface. Downing it via a socket for
151 another AF may have bad consequences. */
152 fd
= get_socket_for_af(AF_INET
);
154 fprintf(stderr
, _("No support for INET on this system.\n"));
160 safe_strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
161 if (ioctl(fd
, SIOCGIFFLAGS
, &ifr
) < 0) {
162 fprintf(stderr
, _("%s: ERROR while getting interface flags: %s\n"),
163 ifname
, strerror(errno
));
166 safe_strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
167 ifr
.ifr_flags
&= ~flag
;
168 if (ioctl(fd
, SIOCSIFFLAGS
, &ifr
) < 0) {
169 perror("SIOCSIFFLAGS");
175 /** test is a specified flag is set */
176 static int test_flag(char *ifname
, short flags
)
181 if (strchr(ifname
, ':')) {
182 /* This is a v4 alias interface. Downing it via a socket for
183 another AF may have bad consequences. */
184 fd
= get_socket_for_af(AF_INET
);
186 fprintf(stderr
, _("No support for INET on this system.\n"));
192 safe_strncpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
193 if (ioctl(fd
, SIOCGIFFLAGS
, &ifr
) < 0) {
194 fprintf(stderr
, _("%s: ERROR while testing interface flags: %s\n"),
195 ifname
, strerror(errno
));
198 return (ifr
.ifr_flags
& flags
);
201 static void usage(void)
203 fprintf(stderr
, _("Usage:\n ifconfig [-a] [-i] [-v] [-s] <interface> [[<AF>] <address>]\n"));
205 fprintf(stderr
, _(" [add <address>[/<prefixlen>]]\n"));
206 fprintf(stderr
, _(" [del <address>[/<prefixlen>]]\n"));
207 fprintf(stderr
, _(" [[-]broadcast [<address>]] [[-]pointopoint [<address>]]\n"));
208 fprintf(stderr
, _(" [netmask <address>] [dstaddr <address>] [tunnel <address>]\n"));
210 #ifdef SIOCSKEEPALIVE
211 fprintf(stderr
, _(" [outfill <NN>] [keepalive <NN>]\n"));
213 fprintf(stderr
, _(" [hw <HW> <address>] [metric <NN>] [mtu <NN>]\n"));
214 fprintf(stderr
, _(" [[-]trailers] [[-]arp] [[-]allmulti]\n"));
215 fprintf(stderr
, _(" [multicast] [[-]promisc]\n"));
216 fprintf(stderr
, _(" [mem_start <NN>] [io_addr <NN>] [irq <NN>] [media <type>]\n"));
217 #ifdef HAVE_TXQUEUELEN
218 fprintf(stderr
, _(" [txqueuelen <NN>]\n"));
221 fprintf(stderr
, _(" [[-]dynamic]\n"));
223 fprintf(stderr
, _(" [up|down] ...\n\n"));
225 fprintf(stderr
, _(" <HW>=Hardware Type.\n"));
226 fprintf(stderr
, _(" List of possible hardware types:\n"));
227 print_hwlist(0); /* 1 = ARPable */
228 fprintf(stderr
, _(" <AF>=Address family. Default: %s\n"), DFLT_AF
);
229 fprintf(stderr
, _(" List of possible address families:\n"));
230 print_aflist(0); /* 1 = routeable */
234 static void version(void)
236 fprintf(stderr
, "%s\n%s\n", Release
, Version
);
240 static int set_netmask(int skfd
, struct ifreq
*ifr
, struct sockaddr
*sa
)
244 memcpy((char *) &ifr
->ifr_netmask
, (char *) sa
,
245 sizeof(struct sockaddr
));
246 if (ioctl(skfd
, SIOCSIFNETMASK
, ifr
) < 0) {
247 fprintf(stderr
, "SIOCSIFNETMASK: %s\n",
254 int main(int argc
, char **argv
)
257 struct sockaddr samask
;
258 struct sockaddr_in sin
;
263 int goterr
= 0, didnetmask
= 0, neednetmask
=0;
267 extern struct aftype inet6_aftype
;
268 struct sockaddr_in6 sa6
;
269 struct in6_ifreq ifr6
;
270 unsigned long prefix_len
;
274 extern struct aftype inet_aftype
;
278 setlocale (LC_ALL
, "");
279 bindtextdomain("net-tools", "/usr/share/locale");
280 textdomain("net-tools");
283 /* Find any options. */
286 while (argc
&& *argv
[0] == '-') {
287 if (!strcmp(*argv
, "-a"))
290 else if (!strcmp(*argv
, "-s"))
293 else if (!strcmp(*argv
, "-v"))
296 else if (!strcmp(*argv
, "-V") || !strcmp(*argv
, "-version") ||
297 !strcmp(*argv
, "--version"))
300 else if (!strcmp(*argv
, "-?") || !strcmp(*argv
, "-h") ||
301 !strcmp(*argv
, "-help") || !strcmp(*argv
, "--help"))
305 fprintf(stderr
, _("ifconfig: option `%s' not recognised.\n"),
307 fprintf(stderr
, _("ifconfig: `--help' gives usage information.\n"));
315 /* Create a channel to the NET kernel. */
316 if ((skfd
= sockets_open(0)) < 0) {
321 /* Do we have to show the current setup? */
323 int err
= if_print((char *) NULL
);
327 /* No. Fetch the interface name. */
329 safe_strncpy(ifr
.ifr_name
, *spp
++, IFNAMSIZ
);
330 if (*spp
== (char *) NULL
) {
331 int err
= if_print(ifr
.ifr_name
);
336 /* The next argument is either an address family name, or an option. */
337 if ((ap
= get_aftype(*spp
)) != NULL
)
338 spp
++; /* it was a AF name */
340 ap
= get_aftype(DFLT_AF
);
343 addr_family
= ap
->af
;
347 /* Process the remaining arguments. */
348 while (*spp
!= (char *) NULL
) {
349 if (!strcmp(*spp
, "arp")) {
350 goterr
|= clr_flag(ifr
.ifr_name
, IFF_NOARP
);
354 if (!strcmp(*spp
, "-arp")) {
355 goterr
|= set_flag(ifr
.ifr_name
, IFF_NOARP
);
360 if (!strcmp(*spp
, "media") || !strcmp(*spp
, "port")) {
363 if (!strcasecmp(*spp
, "auto")) {
364 goterr
|= set_flag(ifr
.ifr_name
, IFF_AUTOMEDIA
);
368 newport
= strtol(*spp
, &endp
, 10);
371 for (i
= 0; if_port_text
[i
][0] && newport
== -1; i
++) {
372 for (j
= 0; if_port_text
[i
][j
]; j
++) {
373 if (!strcasecmp(*spp
, if_port_text
[i
][j
])) {
382 fprintf(stderr
, _("Unknown media type.\n"));
385 if (ioctl(skfd
, SIOCGIFMAP
, &ifr
) < 0) {
386 perror("port: SIOCGIFMAP");
390 ifr
.ifr_map
.port
= newport
;
391 if (ioctl(skfd
, SIOCSIFMAP
, &ifr
) < 0) {
392 perror("port: SIOCSIFMAP");
401 if (!strcmp(*spp
, "trailers")) {
402 goterr
|= clr_flag(ifr
.ifr_name
, IFF_NOTRAILERS
);
406 if (!strcmp(*spp
, "-trailers")) {
407 goterr
|= set_flag(ifr
.ifr_name
, IFF_NOTRAILERS
);
411 if (!strcmp(*spp
, "promisc")) {
412 goterr
|= set_flag(ifr
.ifr_name
, IFF_PROMISC
);
416 if (!strcmp(*spp
, "-promisc")) {
417 goterr
|= clr_flag(ifr
.ifr_name
, IFF_PROMISC
);
418 if (test_flag(ifr
.ifr_name
, IFF_PROMISC
) > 0)
419 fprintf(stderr
, _("Warning: Interface %s still in promisc mode... maybe other application is running?\n"), ifr
.ifr_name
);
423 if (!strcmp(*spp
, "multicast")) {
424 goterr
|= set_flag(ifr
.ifr_name
, IFF_MULTICAST
);
428 if (!strcmp(*spp
, "-multicast")) {
429 goterr
|= clr_flag(ifr
.ifr_name
, IFF_MULTICAST
);
430 if (test_flag(ifr
.ifr_name
, IFF_MULTICAST
) > 0)
431 fprintf(stderr
, _("Warning: Interface %s still in MULTICAST mode.\n"), ifr
.ifr_name
);
435 if (!strcmp(*spp
, "allmulti")) {
436 goterr
|= set_flag(ifr
.ifr_name
, IFF_ALLMULTI
);
440 if (!strcmp(*spp
, "-allmulti")) {
441 goterr
|= clr_flag(ifr
.ifr_name
, IFF_ALLMULTI
);
442 if (test_flag(ifr
.ifr_name
, IFF_MULTICAST
) > 0)
443 fprintf(stderr
, _("Warning: Interface %s still in ALLMULTI mode.\n"), ifr
.ifr_name
);
447 if (!strcmp(*spp
, "up")) {
448 goterr
|= set_flag(ifr
.ifr_name
, (IFF_UP
| IFF_RUNNING
));
452 if (!strcmp(*spp
, "down")) {
453 goterr
|= clr_flag(ifr
.ifr_name
, IFF_UP
);
458 if (!strcmp(*spp
, "dynamic")) {
459 goterr
|= set_flag(ifr
.ifr_name
, IFF_DYNAMIC
);
463 if (!strcmp(*spp
, "-dynamic")) {
464 goterr
|= clr_flag(ifr
.ifr_name
, IFF_DYNAMIC
);
466 if (test_flag(ifr
.ifr_name
, IFF_MULTICAST
) > 0)
467 fprintf(stderr
, _("Warning: Interface %s still in DYNAMIC mode.\n"), ifr
.ifr_name
);
472 if (!strcmp(*spp
, "metric")) {
475 ifr
.ifr_metric
= atoi(*spp
);
476 if (ioctl(skfd
, SIOCSIFMETRIC
, &ifr
) < 0) {
477 fprintf(stderr
, "SIOCSIFMETRIC: %s\n", strerror(errno
));
483 if (!strcmp(*spp
, "mtu")) {
486 ifr
.ifr_mtu
= atoi(*spp
);
487 if (ioctl(skfd
, SIOCSIFMTU
, &ifr
) < 0) {
488 fprintf(stderr
, "SIOCSIFMTU: %s\n", strerror(errno
));
494 #ifdef SIOCSKEEPALIVE
495 if (!strcmp(*spp
, "keepalive")) {
498 ifr
.ifr_data
= (caddr_t
) atoi(*spp
);
499 if (ioctl(skfd
, SIOCSKEEPALIVE
, &ifr
) < 0) {
500 fprintf(stderr
, "SIOCSKEEPALIVE: %s\n", strerror(errno
));
509 if (!strcmp(*spp
, "outfill")) {
512 ifr
.ifr_data
= (caddr_t
) atoi(*spp
);
513 if (ioctl(skfd
, SIOCSOUTFILL
, &ifr
) < 0) {
514 fprintf(stderr
, "SIOCSOUTFILL: %s\n", strerror(errno
));
522 if (!strcmp(*spp
, "-broadcast")) {
523 goterr
|= clr_flag(ifr
.ifr_name
, IFF_BROADCAST
);
524 if (test_flag(ifr
.ifr_name
, IFF_MULTICAST
) > 0)
525 fprintf(stderr
, _("Warning: Interface %s still in BROADCAST mode.\n"), ifr
.ifr_name
);
529 if (!strcmp(*spp
, "broadcast")) {
530 if (*++spp
!= NULL
) {
531 safe_strncpy(host
, *spp
, (sizeof host
));
532 if (ap
->input(0, host
, &sa
) < 0) {
538 memcpy((char *) &ifr
.ifr_broadaddr
, (char *) &sa
,
539 sizeof(struct sockaddr
));
540 if (ioctl(ap
->fd
, SIOCSIFBRDADDR
, &ifr
) < 0) {
541 fprintf(stderr
, "SIOCSIFBRDADDR: %s\n",
547 goterr
|= set_flag(ifr
.ifr_name
, IFF_BROADCAST
);
550 if (!strcmp(*spp
, "dstaddr")) {
553 safe_strncpy(host
, *spp
, (sizeof host
));
554 if (ap
->input(0, host
, &sa
) < 0) {
560 memcpy((char *) &ifr
.ifr_dstaddr
, (char *) &sa
,
561 sizeof(struct sockaddr
));
562 if (ioctl(ap
->fd
, SIOCSIFDSTADDR
, &ifr
) < 0) {
563 fprintf(stderr
, "SIOCSIFDSTADDR: %s\n",
570 if (!strcmp(*spp
, "netmask")) {
571 if (*++spp
== NULL
|| didnetmask
)
573 safe_strncpy(host
, *spp
, (sizeof host
));
574 if (ap
->input(0, host
, &sa
) < 0) {
581 goterr
|= set_netmask(ap
->fd
, &ifr
, &sa
);
585 #ifdef HAVE_TXQUEUELEN
586 if (!strcmp(*spp
, "txqueuelen")) {
589 ifr
.ifr_qlen
= strtoul(*spp
, NULL
, 0);
590 if (ioctl(skfd
, SIOCSIFTXQLEN
, &ifr
) < 0) {
591 fprintf(stderr
, "SIOCSIFTXQLEN: %s\n", strerror(errno
));
599 if (!strcmp(*spp
, "mem_start")) {
602 if (ioctl(skfd
, SIOCGIFMAP
, &ifr
) < 0) {
603 fprintf(stderr
, "mem_start: SIOCGIFMAP: %s\n", strerror(errno
));
608 ifr
.ifr_map
.mem_start
= strtoul(*spp
, NULL
, 0);
609 if (ioctl(skfd
, SIOCSIFMAP
, &ifr
) < 0) {
610 fprintf(stderr
, "mem_start: SIOCSIFMAP: %s\n", strerror(errno
));
616 if (!strcmp(*spp
, "io_addr")) {
619 if (ioctl(skfd
, SIOCGIFMAP
, &ifr
) < 0) {
620 fprintf(stderr
, "io_addr: SIOCGIFMAP: %s\n", strerror(errno
));
625 ifr
.ifr_map
.base_addr
= strtol(*spp
, NULL
, 0);
626 if (ioctl(skfd
, SIOCSIFMAP
, &ifr
) < 0) {
627 fprintf(stderr
, "io_addr: SIOCSIFMAP: %s\n", strerror(errno
));
633 if (!strcmp(*spp
, "irq")) {
636 if (ioctl(skfd
, SIOCGIFMAP
, &ifr
) < 0) {
637 fprintf(stderr
, "irq: SIOCGIFMAP: %s\n", strerror(errno
));
642 ifr
.ifr_map
.irq
= atoi(*spp
);
643 if (ioctl(skfd
, SIOCSIFMAP
, &ifr
) < 0) {
644 fprintf(stderr
, "irq: SIOCSIFMAP: %s\n", strerror(errno
));
650 if (!strcmp(*spp
, "-pointopoint")) {
651 goterr
|= clr_flag(ifr
.ifr_name
, IFF_POINTOPOINT
);
653 if (test_flag(ifr
.ifr_name
, IFF_MULTICAST
) > 0)
654 fprintf(stderr
, _("Warning: Interface %s still in POINTOPOINT mode.\n"), ifr
.ifr_name
);
657 if (!strcmp(*spp
, "pointopoint")) {
658 if (*(spp
+ 1) != NULL
) {
660 safe_strncpy(host
, *spp
, (sizeof host
));
661 if (ap
->input(0, host
, &sa
)) {
667 memcpy((char *) &ifr
.ifr_dstaddr
, (char *) &sa
,
668 sizeof(struct sockaddr
));
669 if (ioctl(ap
->fd
, SIOCSIFDSTADDR
, &ifr
) < 0) {
670 fprintf(stderr
, "SIOCSIFDSTADDR: %s\n",
675 goterr
|= set_flag(ifr
.ifr_name
, IFF_POINTOPOINT
);
680 if (!strcmp(*spp
, "hw")) {
683 if ((hw
= get_hwtype(*spp
)) == NULL
)
685 if (hw
->input
== NULL
) {
686 fprintf(stderr
, _("hw address type `%s' has no handler to set address. failed.\n"), *spp
);
693 safe_strncpy(host
, *spp
, (sizeof host
));
694 if (hw
->input(host
, &sa
) < 0) {
695 fprintf(stderr
, _("%s: invalid %s address.\n"), host
, hw
->name
);
700 memcpy((char *) &ifr
.ifr_hwaddr
, (char *) &sa
,
701 sizeof(struct sockaddr
));
702 if (ioctl(skfd
, SIOCSIFHWADDR
, &ifr
) < 0) {
703 fprintf(stderr
, "SIOCSIFHWADDR: %s\n",
710 #if HAVE_AFINET || HAVE_AFINET6
711 if (!strcmp(*spp
, "add")) {
715 if (strchr(*spp
, ':')) {
717 if ((cp
= strchr(*spp
, '/'))) {
718 prefix_len
= atol(cp
+ 1);
719 if ((prefix_len
< 0) || (prefix_len
> 128))
725 safe_strncpy(host
, *spp
, (sizeof host
));
726 if (inet6_aftype
.input(1, host
,
727 (struct sockaddr
*) &sa6
) < 0) {
728 inet6_aftype
.herror(host
);
733 memcpy((char *) &ifr6
.ifr6_addr
, (char *) &sa6
.sin6_addr
,
734 sizeof(struct in6_addr
));
736 fd
= get_socket_for_af(AF_INET6
);
739 _("No support for INET6 on this system.\n"));
744 if (ioctl(fd
, SIOGIFINDEX
, &ifr
) < 0) {
745 perror("SIOGIFINDEX");
750 ifr6
.ifr6_ifindex
= ifr
.ifr_ifindex
;
751 ifr6
.ifr6_prefixlen
= prefix_len
;
752 if (ioctl(fd
, SIOCSIFADDR
, &ifr6
) < 0) {
753 perror("SIOCSIFADDR");
761 { /* ipv4 address a.b.c.d */
762 unsigned long ip
, nm
, bc
;
763 safe_strncpy(host
, *spp
, (sizeof host
));
764 if (inet_aftype
.input(0, host
, (struct sockaddr
*)&sin
) < 0) {
770 fd
= get_socket_for_af(AF_INET
);
773 _("No support for INET on this system.\n"));
779 memcpy(&ip
, &sin
.sin_addr
.s_addr
, sizeof(unsigned long));
781 if (get_nmbc_parent(ifr
.ifr_name
, &nm
, &bc
) < 0) {
782 fprintf(stderr
, _("Interface %s not initialized\n"),
788 set_ifstate(ifr
.ifr_name
, ip
, nm
, bc
, 1);
794 fprintf(stderr
, _("Bad address.\n"));
799 #if HAVE_AFINET || HAVE_AFINET6
800 if (!strcmp(*spp
, "del")) {
806 if (strchr(*spp
, ':')) { /* INET6 */
807 if ((cp
= strchr(*spp
, '/'))) {
808 prefix_len
= atol(cp
+ 1);
809 if ((prefix_len
< 0) || (prefix_len
> 128))
815 safe_strncpy(host
, *spp
, (sizeof host
));
816 if (inet6_aftype
.input(1, host
,
817 (struct sockaddr
*) &sa6
) < 0) {
818 inet6_aftype
.herror(host
);
823 memcpy((char *) &ifr6
.ifr6_addr
, (char *) &sa6
.sin6_addr
,
824 sizeof(struct in6_addr
));
826 fd
= get_socket_for_af(AF_INET6
);
829 _("No support for INET6 on this system.\n"));
834 if (ioctl(fd
, SIOGIFINDEX
, &ifr
) < 0) {
835 perror("SIOGIFINDEX");
840 ifr6
.ifr6_ifindex
= ifr
.ifr_ifindex
;
841 ifr6
.ifr6_prefixlen
= prefix_len
;
842 if (ioctl(fd
, SIOCDIFADDR
, &ifr6
) < 0) {
843 fprintf(stderr
, "SIOCDIFADDR: %s\n",
853 /* ipv4 address a.b.c.d */
854 unsigned long ip
, nm
, bc
;
855 safe_strncpy(host
, *spp
, (sizeof host
));
856 if (inet_aftype
.input(0, host
, (struct sockaddr
*)&sin
) < 0) {
862 fd
= get_socket_for_af(AF_INET
);
864 fprintf(stderr
, _("No support for INET on this system.\n"));
870 memcpy(&ip
, &sin
.sin_addr
.s_addr
, sizeof(unsigned long));
872 if (get_nmbc_parent(ifr
.ifr_name
, &nm
, &bc
) < 0) {
873 fprintf(stderr
, _("Interface %s not initialized\n"),
879 set_ifstate(ifr
.ifr_name
, ip
, nm
, bc
, 0);
884 fprintf(stderr
, _("Bad address.\n"));
887 fprintf(stderr
, _("Address deletion not supported on this system.\n"));
892 if (!strcmp(*spp
, "tunnel")) {
895 if ((cp
= strchr(*spp
, '/'))) {
896 prefix_len
= atol(cp
+ 1);
897 if ((prefix_len
< 0) || (prefix_len
> 128))
903 safe_strncpy(host
, *spp
, (sizeof host
));
904 if (inet6_aftype
.input(1, host
, (struct sockaddr
*) &sa6
) < 0) {
905 inet6_aftype
.herror(host
);
910 memcpy((char *) &ifr6
.ifr6_addr
, (char *) &sa6
.sin6_addr
,
911 sizeof(struct in6_addr
));
913 fd
= get_socket_for_af(AF_INET6
);
915 fprintf(stderr
, _("No support for INET6 on this system.\n"));
920 if (ioctl(fd
, SIOGIFINDEX
, &ifr
) < 0) {
921 perror("SIOGIFINDEX");
926 ifr6
.ifr6_ifindex
= ifr
.ifr_ifindex
;
927 ifr6
.ifr6_prefixlen
= prefix_len
;
929 if (ioctl(fd
, SIOCSIFDSTADDR
, &ifr6
) < 0) {
930 fprintf(stderr
, "SIOCSIFDSTADDR: %s\n",
939 /* If the next argument is a valid hostname, assume OK. */
940 safe_strncpy(host
, *spp
, (sizeof host
));
942 /* FIXME: sa is too small for INET6 addresses, inet6 should use that too,
943 broadcast is unexpected */
945 switch (ap
->getmask(host
, &samask
, NULL
)) {
953 // remeber to set the netmask from samask later
958 if (ap
->input
== NULL
) {
959 fprintf(stderr
, _("ifconfig: Cannot set address for this protocol family.\n"));
962 if (ap
->input(0, host
, &sa
) < 0) {
964 fprintf(stderr
, _("ifconfig: `--help' gives usage information.\n"));
967 memcpy((char *) &ifr
.ifr_addr
, (char *) &sa
, sizeof(struct sockaddr
));
969 int r
= 0; /* to shut gcc up */
973 fd
= get_socket_for_af(AF_INET
);
975 fprintf(stderr
, _("No support for INET on this system.\n"));
978 r
= ioctl(fd
, SIOCSIFADDR
, &ifr
);
983 fd
= get_socket_for_af(AF_ECONET
);
985 fprintf(stderr
, _("No support for ECONET on this system.\n"));
988 r
= ioctl(fd
, SIOCSIFADDR
, &ifr
);
993 _("Don't know how to set addresses for family %d.\n"), ap
->af
);
997 perror("SIOCSIFADDR");
1003 * Don't do the set_flag() if the address is an alias with a - at the
1004 * end, since it's deleted already! - Roman
1006 * Should really use regex.h here, not sure though how well it'll go
1007 * with the cross-platform support etc.
1011 short int found_colon
= 0;
1012 for (ptr
= ifr
.ifr_name
; *ptr
; ptr
++ )
1013 if (*ptr
== ':') found_colon
++;
1015 if (!(found_colon
&& *(ptr
- 1) == '-'))
1016 goterr
|= set_flag(ifr
.ifr_name
, (IFF_UP
| IFF_RUNNING
));
1023 goterr
|= set_netmask(skfd
, &ifr
, &samask
);
1027 if (opt_v
&& goterr
)
1028 fprintf(stderr
, _("WARNING: at least one error occured. (%d)\n"), goterr
);
1040 static unsigned char searcher
[256];
1042 static int set_ip_using(const char *name
, int c
, unsigned long ip
)
1045 struct sockaddr_in sin
;
1047 safe_strncpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
1048 memset(&sin
, 0, sizeof(struct sockaddr
));
1049 sin
.sin_family
= AF_INET
;
1050 sin
.sin_addr
.s_addr
= ip
;
1051 memcpy(&ifr
.ifr_addr
, &sin
, sizeof(struct sockaddr
));
1052 if (ioctl(skfd
, c
, &ifr
) < 0)
1057 static int do_ifcmd(struct interface
*x
, struct ifcmd
*ptr
)
1060 struct sockaddr_in
*sin
;
1063 if (do_if_fetch(x
) < 0)
1065 if (strncmp(x
->name
, ptr
->base
, ptr
->baselen
) != 0)
1066 return 0; /* skip */
1067 z
= strchr(x
->name
, ':');
1071 for (e
= z
; *e
; e
++)
1072 if (*e
== '-') /* deleted */
1075 if (i
< 0 || i
> 255)
1080 sin
= (struct sockaddr_in
*)&x
->dstaddr
;
1081 if (sin
->sin_addr
.s_addr
!= ptr
->addr
) {
1087 if (set_flag(x
->name
, IFF_UP
| IFF_RUNNING
) == -1)
1091 if (clr_flag(x
->name
, IFF_UP
) == -1)
1095 return 1; /* all done! */
1099 static int get_nmbc_parent(char *parent
,
1100 unsigned long *nm
, unsigned long *bc
)
1102 struct interface
*i
;
1103 struct sockaddr_in
*sin
;
1105 i
= lookup_interface(parent
);
1108 if (do_if_fetch(i
) < 0)
1110 sin
= (struct sockaddr_in
*)&i
->netmask
;
1111 memcpy(nm
, &sin
->sin_addr
.s_addr
, sizeof(unsigned long));
1112 sin
= (struct sockaddr_in
*)&i
->broadaddr
;
1113 memcpy(bc
, &sin
->sin_addr
.s_addr
, sizeof(unsigned long));
1117 static int set_ifstate(char *parent
, unsigned long ip
,
1118 unsigned long nm
, unsigned long bc
,
1126 pt
.baselen
= strlen(parent
);
1129 memset(searcher
, 0, sizeof(searcher
));
1130 i
= for_all_interfaces((int (*)(struct interface
*,void *))do_ifcmd
,
1137 /* add a new interface */
1138 for (i
= 0; i
< 256; i
++)
1139 if (searcher
[i
] == 0)
1143 return -1; /* FAILURE!!! out of ip addresses */
1145 if (snprintf(buf
, IFNAMSIZ
, "%s:%d", parent
, i
) > IFNAMSIZ
)
1147 if (set_ip_using(buf
, SIOCSIFADDR
, ip
) == -1)
1149 if (set_ip_using(buf
, SIOCSIFNETMASK
, nm
) == -1)
1151 if (set_ip_using(buf
, SIOCSIFBRDADDR
, bc
) == -1)
1153 if (set_flag(buf
, IFF_BROADCAST
) == -1)