]>
git.saurik.com Git - apple/network_cmds.git/blob - ip6conf.tproj/ip6tool.c
37f7846e26a5abb620b2b5fc7b13322d5f25a31f
1 /* Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
3 * @APPLE_LICENSE_HEADER_START@
5 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
29 #include <sys/errno.h>
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <netinet6/in6_var.h>
38 /* From netinet6/in6_var.h */
39 #ifndef SIOCPROTOATTACH_IN6
40 #define SIOCPROTOATTACH_IN6 _IOWR('i', 110, struct in6_aliasreq) /* attach proto to interface */
42 #ifndef SIOCPROTODETACH_IN6
43 #define SIOCPROTODETACH_IN6 _IOWR('i', 111, struct in6_ifreq) /* detach proto from interface */
46 #define SIOCLL_START _IOWR('i', 130, struct in6_aliasreq) /* start aquiring linklocal on interface */
49 #define SIOCLL_STOP _IOWR('i', 131, struct in6_ifreq) /* deconfigure linklocal from interface */
53 #define IPv6_STARTUP 1
54 #define IPv6_SHUTDOWN 2
55 #define IPv6_STARTUP_ALL 3
56 #define IPv6_SHUTDOWN_ALL 4
58 const char *if_exceptions
[] = {"lo0", "gif0", "faith0", "stf0"};
63 int do_protoattach(int s
, char *name
);
64 int do_protodetach(int s
, char *name
);
65 void do_protoattach_all(int s
);
66 void do_protodetach_all(int s
);
70 main(int argc
, char **argv
)
76 char *interface
= NULL
;
78 if ((ch
= getopt(argc
, argv
, "u:d:ax")) != -1) {
81 /* option -u: start up proto */
82 option
= IPv6_STARTUP
;
86 /* option -d: shut down proto */
87 option
= IPv6_SHUTDOWN
;
91 /* option -a: start up proto on all interfaces */
92 option
= IPv6_STARTUP_ALL
;
95 /* option -x: shut down proto on all interfaces */
96 option
= IPv6_SHUTDOWN_ALL
;
108 if ((s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
110 printf("%s: Error %d creating socket.\n", argv
[0], err
);
116 err
= do_protoattach(s
, interface
);
118 printf("%s: Error %d encountered attaching interface %s.\n", argv
[0], err
, interface
);
122 err
= do_protodetach(s
, interface
);
124 printf("%s: Error %d encountered detaching interface %s.\n", argv
[0], err
, interface
);
127 case IPv6_STARTUP_ALL
:
128 do_protoattach_all(s
);
131 case IPv6_SHUTDOWN_ALL
:
132 do_protodetach_all(s
);
148 Start up IPv6 on ALL interfaces: -a\n\
149 Shut down IPv6 on ALL interfaces: -x\n\
150 Start up IPv6 on given interface: -u [interface]\n\
151 Shut down IPv6 on given interface: -d [interface].\n");
155 do_protoattach(int s
, char *name
)
157 struct in6_aliasreq ifr
;
160 bzero(&ifr
, sizeof(ifr
));
161 strncpy(ifr
.ifra_name
, name
, sizeof(ifr
.ifra_name
));
163 if ((err
= ioctl(s
, SIOCPROTOATTACH_IN6
, &ifr
)) != 0)
166 return (ioctl(s
, SIOCLL_START
, &ifr
));
170 do_protodetach(int s
, char *name
)
172 struct in6_ifreq ifr
;
175 bzero(&ifr
, sizeof(ifr
));
176 strncpy(ifr
.ifr_name
, name
, sizeof(ifr
.ifr_name
));
178 if ((err
= ioctl(s
, SIOCLL_STOP
, &ifr
)) != 0)
181 return (ioctl(s
, SIOCPROTODETACH_IN6
, &ifr
));
185 do_protoattach_all(int s
)
187 struct ifaddrs
*ifaddrs
, *ifa
;
189 if (getifaddrs(&ifaddrs
)) {
190 printf("ip6: getifaddrs returned error (%s)", strerror(errno
));
193 for (ifa
= ifaddrs
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
194 /* skip over invalid interfaces */
195 if ((strcmp(ifa
->ifa_name
, if_exceptions
[0])))
196 if (strcmp(ifa
->ifa_name
, if_exceptions
[1]))
197 if (strcmp(ifa
->ifa_name
, if_exceptions
[2]))
198 if (strcmp(ifa
->ifa_name
, if_exceptions
[3])) {
199 /* this is a valid interface */
200 if (do_protoattach(s
, ifa
->ifa_name
)) {
201 printf("ip6: error attaching %s\n", ifa
->ifa_name
);
204 while (ifa
->ifa_next
!= NULL
&&
205 !(strcmp(ifa
->ifa_name
, ifa
->ifa_next
->ifa_name
))) {
206 /* skip multiple entries for same interface */
212 freeifaddrs(ifaddrs
);
218 do_protodetach_all(int s
)
220 struct ifaddrs
*ifaddrs
, *ifa
;
222 if (getifaddrs(&ifaddrs
)) {
223 printf("ip6: getifaddrs returned error (%s)", strerror(errno
));
226 for (ifa
= ifaddrs
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
227 /* skip over invalid interfaces */
228 if ((strcmp(ifa
->ifa_name
, if_exceptions
[0])))
229 if (strcmp(ifa
->ifa_name
, if_exceptions
[1]))
230 if (strcmp(ifa
->ifa_name
, if_exceptions
[2]))
231 if (strcmp(ifa
->ifa_name
, if_exceptions
[3])) {
232 /* this is a valid interface */
233 if (do_protodetach(s
, ifa
->ifa_name
)) {
234 printf("ip6: error detaching %s\n", ifa
->ifa_name
);
237 while (ifa
->ifa_next
!= NULL
&&
238 !(strcmp(ifa
->ifa_name
, ifa
->ifa_next
->ifa_name
))) {
239 /* skip multiple entries for same interface */
245 freeifaddrs(ifaddrs
);