]>
git.saurik.com Git - apple/network_cmds.git/blob - ip6conf.tproj/ip6tool.c
1 /* Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 * @APPLE_LICENSE_HEADER_START@
5 * This file contains Original Code and/or Modifications of Original Code
6 * as defined in and that are subject to the Apple Public Source License
7 * Version 2.0 (the 'License'). You may not use this file except in
8 * compliance with the License. Please obtain a copy of the License at
9 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * The Original Code and all software distributed under the License are
13 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
17 * Please see the License for the specific language governing rights and
18 * limitations under the License.
20 * @APPLE_LICENSE_HEADER_END@
27 #include <sys/errno.h>
28 #include <sys/socket.h>
29 #include <sys/ioctl.h>
31 #include <sys/types.h>
32 #include <netinet/in.h>
33 #include <netinet6/in6_var.h>
36 /* From netinet6/in6_var.h */
37 #ifndef SIOCPROTOATTACH_IN6
38 #define SIOCPROTOATTACH_IN6 _IOWR('i', 110, struct in6_aliasreq) /* attach proto to interface */
40 #ifndef SIOCPROTODETACH_IN6
41 #define SIOCPROTODETACH_IN6 _IOWR('i', 111, struct in6_ifreq) /* detach proto from interface */
44 #define SIOCLL_START _IOWR('i', 130, struct in6_aliasreq) /* start aquiring linklocal on interface */
47 #define SIOCLL_STOP _IOWR('i', 131, struct in6_ifreq) /* deconfigure linklocal from interface */
51 #define IPv6_STARTUP 1
52 #define IPv6_SHUTDOWN 2
53 #define IPv6_STARTUP_ALL 3
54 #define IPv6_SHUTDOWN_ALL 4
56 const char *if_exceptions
[] = {"lo0", "gif0", "faith0", "stf0"};
61 int do_protoattach(int s
, char *name
);
62 int do_protodetach(int s
, char *name
);
63 void do_protoattach_all(int s
);
64 void do_protodetach_all(int s
);
68 main(int argc
, char **argv
)
74 char *interface
= NULL
;
76 if ((ch
= getopt(argc
, argv
, "u:d:ax")) != -1) {
79 /* option -u: start up proto */
80 option
= IPv6_STARTUP
;
84 /* option -d: shut down proto */
85 option
= IPv6_SHUTDOWN
;
89 /* option -a: start up proto on all interfaces */
90 option
= IPv6_STARTUP_ALL
;
93 /* option -x: shut down proto on all interfaces */
94 option
= IPv6_SHUTDOWN_ALL
;
106 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
108 printf("%s: Error %d creating socket.\n", argv
[0], err
);
114 err
= do_protoattach(s
, interface
);
116 printf("%s: Error %d encountered attaching interface %s.\n", argv
[0], err
, interface
);
120 err
= do_protodetach(s
, interface
);
122 printf("%s: Error %d encountered detaching interface %s.\n", argv
[0], err
, interface
);
125 case IPv6_STARTUP_ALL
:
126 do_protoattach_all(s
);
129 case IPv6_SHUTDOWN_ALL
:
130 do_protodetach_all(s
);
146 Start up IPv6 on ALL interfaces: -a\n\
147 Shut down IPv6 on ALL interfaces: -x\n\
148 Start up IPv6 on given interface: -u [interface]\n\
149 Shut down IPv6 on given interface: -d [interface].\n");
153 do_protoattach(int s
, char *name
)
155 struct in6_aliasreq ifr
;
158 bzero(&ifr
, sizeof(ifr
));
159 strncpy(ifr
.ifra_name
, name
, sizeof(ifr
.ifra_name
));
161 if ((err
= ioctl(s
, SIOCPROTOATTACH_IN6
, &ifr
)) != 0)
164 return (ioctl(s
, SIOCLL_START
, &ifr
));
168 do_protodetach(int s
, char *name
)
170 struct in6_ifreq ifr
;
173 bzero(&ifr
, sizeof(ifr
));
174 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
176 if ((err
= ioctl(s
, SIOCLL_STOP
, &ifr
)) != 0)
179 return (ioctl(s
, SIOCPROTODETACH_IN6
, &ifr
));
183 do_protoattach_all(int s
)
185 struct ifaddrs
*ifaddrs
, *ifa
;
187 if (getifaddrs(&ifaddrs
)) {
188 printf("ip6: getifaddrs returned error (%s)", strerror(errno
));
191 for (ifa
= ifaddrs
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
192 /* skip over invalid interfaces */
193 if ((strcmp(ifa
->ifa_name
, if_exceptions
[0])))
194 if (strcmp(ifa
->ifa_name
, if_exceptions
[1]))
195 if (strcmp(ifa
->ifa_name
, if_exceptions
[2]))
196 if (strcmp(ifa
->ifa_name
, if_exceptions
[3])) {
197 /* this is a valid interface */
198 if (do_protoattach(s
, ifa
->ifa_name
)) {
199 printf("ip6: error attaching %s\n", ifa
->ifa_name
);
202 while (ifa
->ifa_next
!= NULL
&&
203 !(strcmp(ifa
->ifa_name
, ifa
->ifa_next
->ifa_name
))) {
204 /* skip multiple entries for same interface */
210 freeifaddrs(ifaddrs
);
216 do_protodetach_all(int s
)
218 struct ifaddrs
*ifaddrs
, *ifa
;
220 if (getifaddrs(&ifaddrs
)) {
221 printf("ip6: getifaddrs returned error (%s)", strerror(errno
));
224 for (ifa
= ifaddrs
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
225 /* skip over invalid interfaces */
226 if ((strcmp(ifa
->ifa_name
, if_exceptions
[0])))
227 if (strcmp(ifa
->ifa_name
, if_exceptions
[1]))
228 if (strcmp(ifa
->ifa_name
, if_exceptions
[2]))
229 if (strcmp(ifa
->ifa_name
, if_exceptions
[3])) {
230 /* this is a valid interface */
231 if (do_protodetach(s
, ifa
->ifa_name
)) {
232 printf("ip6: error detaching %s\n", ifa
->ifa_name
);
235 while (ifa
->ifa_next
!= NULL
&&
236 !(strcmp(ifa
->ifa_name
, ifa
->ifa_next
->ifa_name
))) {
237 /* skip multiple entries for same interface */
243 freeifaddrs(ifaddrs
);