2 * Copyright (c) 2009-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 1983, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 static const char rcsid
[] =
60 "$FreeBSD: src/sbin/ifconfig/af_inet6.c,v 1.6.6.1 2008/11/25 02:59:29 kensmith Exp $";
63 #include <sys/param.h>
64 #include <sys/ioctl.h>
65 #include <sys/socket.h>
75 #include <arpa/inet.h>
77 #include <netinet/in.h>
78 #include <net/if_var.h> /* for struct ifaddr */
79 #include <netinet/in_var.h>
80 #include <arpa/inet.h>
83 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */
87 static struct in6_ifreq in6_ridreq
;
88 static struct in6_aliasreq in6_addreq
=
94 { 0, 0, ND6_INFINITE_LIFETIME
, ND6_INFINITE_LIFETIME
} };
95 static int ip6lifetime
;
97 static void in6_fillscopeid(struct sockaddr_in6
*sin6
);
98 static int prefix(void *, int);
99 static char *sec2str(time_t);
100 static int explicit_prefix
= 0;
102 static char addr_buf
[MAXHOSTNAMELEN
*2 + 1]; /*for getnameinfo()*/
105 setifprefixlen(const char *addr
, int dummy __unused
, int s
,
106 const struct afswtch
*afp
)
108 if (afp
->af_getprefix
!= NULL
)
109 afp
->af_getprefix(addr
, MASK
);
114 setip6flags(const char *dummyaddr __unused
, int flag
, int dummysoc __unused
,
115 const struct afswtch
*afp
)
117 if (afp
->af_af
!= AF_INET6
)
118 err(1, "address flags can be set only for inet6 addresses");
121 in6_addreq
.ifra_flags
&= ~(-flag
);
123 in6_addreq
.ifra_flags
|= flag
;
127 setip6lifetime(const char *cmd
, const char *val
, int s
,
128 const struct afswtch
*afp
)
134 newval
= (time_t)strtoul(val
, &ep
, 0);
136 errx(1, "invalid %s", cmd
);
137 if (afp
->af_af
!= AF_INET6
)
138 errx(1, "%s not allowed for the AF", cmd
);
139 if (strcmp(cmd
, "vltime") == 0) {
140 in6_addreq
.ifra_lifetime
.ia6t_expire
= t
+ newval
;
141 in6_addreq
.ifra_lifetime
.ia6t_vltime
= newval
;
142 } else if (strcmp(cmd
, "pltime") == 0) {
143 in6_addreq
.ifra_lifetime
.ia6t_preferred
= t
+ newval
;
144 in6_addreq
.ifra_lifetime
.ia6t_pltime
= newval
;
149 setip6pltime(const char *seconds
, int dummy __unused
, int s
,
150 const struct afswtch
*afp
)
152 setip6lifetime("pltime", seconds
, s
, afp
);
156 setip6vltime(const char *seconds
, int dummy __unused
, int s
,
157 const struct afswtch
*afp
)
159 setip6lifetime("vltime", seconds
, s
, afp
);
163 setip6eui64(const char *cmd
, int dummy __unused
, int s
,
164 const struct afswtch
*afp
)
166 struct ifaddrs
*ifap
, *ifa
;
167 const struct sockaddr_in6
*sin6
= NULL
;
168 const struct in6_addr
*lladdr
= NULL
;
169 struct in6_addr
*in6
;
171 if (afp
->af_af
!= AF_INET6
)
172 errx(EXIT_FAILURE
, "%s not allowed for the AF", cmd
);
173 in6
= (struct in6_addr
*)&in6_addreq
.ifra_addr
.sin6_addr
;
174 if (memcmp(&in6addr_any
.s6_addr
[8], &in6
->s6_addr
[8], 8) != 0)
175 errx(EXIT_FAILURE
, "interface index is already filled");
176 if (getifaddrs(&ifap
) != 0)
177 err(EXIT_FAILURE
, "getifaddrs");
178 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
179 if (ifa
->ifa_addr
->sa_family
== AF_INET6
&&
180 strcmp(ifa
->ifa_name
, name
) == 0) {
181 sin6
= (const struct sockaddr_in6
*)ifa
->ifa_addr
;
182 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
183 lladdr
= &sin6
->sin6_addr
;
189 errx(EXIT_FAILURE
, "could not determine link local address");
191 memcpy(&in6
->s6_addr
[8], &lladdr
->s6_addr
[8], 8);
197 in6_fillscopeid(struct sockaddr_in6
*sin6
)
199 #if defined(__KAME__) && defined(KAME_SCOPEID)
200 if (IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
)) {
201 sin6
->sin6_scope_id
=
202 ntohs(*(u_int16_t
*)&sin6
->sin6_addr
.s6_addr
[2]);
203 sin6
->sin6_addr
.s6_addr
[2] = sin6
->sin6_addr
.s6_addr
[3] = 0;
209 in6_status(int s __unused
, const struct ifaddrs
*ifa
)
211 struct sockaddr_in6
*sin
, null_sin
;
212 struct in6_ifreq ifr6
;
215 struct in6_addrlifetime lifetime
;
216 time_t t
= time(NULL
);
220 memset(&null_sin
, 0, sizeof(null_sin
));
222 sin
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
226 strncpy(ifr6
.ifr_name
, ifr
.ifr_name
, sizeof(ifr
.ifr_name
));
227 if ((s6
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
228 warn("socket(AF_INET6,SOCK_DGRAM)");
231 ifr6
.ifr_addr
= *sin
;
232 if (ioctl(s6
, SIOCGIFAFLAG_IN6
, &ifr6
) < 0) {
233 warn("ioctl(SIOCGIFAFLAG_IN6)");
237 flags6
= ifr6
.ifr_ifru
.ifru_flags6
;
238 memset(&lifetime
, 0, sizeof(lifetime
));
239 ifr6
.ifr_addr
= *sin
;
240 if (ioctl(s6
, SIOCGIFALIFETIME_IN6
, &ifr6
) < 0) {
241 warn("ioctl(SIOCGIFALIFETIME_IN6)");
245 lifetime
= ifr6
.ifr_ifru
.ifru_lifetime
;
248 /* XXX: embedded link local addr check */
249 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
) &&
250 *(u_short
*)&sin
->sin6_addr
.s6_addr
[2] != 0) {
253 index
= *(u_short
*)&sin
->sin6_addr
.s6_addr
[2];
254 *(u_short
*)&sin
->sin6_addr
.s6_addr
[2] = 0;
255 if (sin
->sin6_scope_id
== 0)
256 sin
->sin6_scope_id
= ntohs(index
);
258 scopeid
= sin
->sin6_scope_id
;
260 error
= getnameinfo((struct sockaddr
*)sin
, sin
->sin6_len
, addr_buf
,
261 sizeof(addr_buf
), NULL
, 0, NI_NUMERICHOST
);
263 inet_ntop(AF_INET6
, &sin
->sin6_addr
, addr_buf
,
265 printf("\tinet6 %s ", addr_buf
);
267 if (ifa
->ifa_flags
& IFF_POINTOPOINT
) {
268 sin
= (struct sockaddr_in6
*)ifa
->ifa_dstaddr
;
270 * some of the interfaces do not have valid destination
273 if (sin
!= NULL
&& sin
->sin6_family
== AF_INET6
) {
276 /* XXX: embedded link local addr check */
277 if (IN6_IS_ADDR_LINKLOCAL(&sin
->sin6_addr
) &&
278 *(u_short
*)&sin
->sin6_addr
.s6_addr
[2] != 0) {
281 index
= *(u_short
*)&sin
->sin6_addr
.s6_addr
[2];
282 *(u_short
*)&sin
->sin6_addr
.s6_addr
[2] = 0;
283 if (sin
->sin6_scope_id
== 0)
284 sin
->sin6_scope_id
= ntohs(index
);
287 error
= getnameinfo((struct sockaddr
*)sin
,
288 sin
->sin6_len
, addr_buf
,
289 sizeof(addr_buf
), NULL
, 0,
292 inet_ntop(AF_INET6
, &sin
->sin6_addr
, addr_buf
,
294 printf("--> %s ", addr_buf
);
298 sin
= (struct sockaddr_in6
*)ifa
->ifa_netmask
;
301 printf("prefixlen %d ", prefix(&sin
->sin6_addr
,
302 sizeof(struct in6_addr
)));
304 if ((flags6
& IN6_IFF_ANYCAST
) != 0)
306 if ((flags6
& IN6_IFF_TENTATIVE
) != 0)
307 printf("tentative ");
308 if ((flags6
& IN6_IFF_OPTIMISTIC
) != 0)
309 printf("optimistic ");
310 if ((flags6
& IN6_IFF_DUPLICATED
) != 0)
311 printf("duplicated ");
312 if ((flags6
& IN6_IFF_DETACHED
) != 0)
314 if ((flags6
& IN6_IFF_DEPRECATED
) != 0)
315 printf("deprecated ");
316 if ((flags6
& IN6_IFF_AUTOCONF
) != 0)
318 if ((flags6
& IN6_IFF_TEMPORARY
) != 0)
319 printf("temporary ");
322 printf("scopeid 0x%x ", scopeid
);
324 if (ip6lifetime
&& (lifetime
.ia6t_preferred
|| lifetime
.ia6t_expire
)) {
326 if (lifetime
.ia6t_preferred
) {
327 printf("%s ", lifetime
.ia6t_preferred
< t
328 ? "0" : sec2str(lifetime
.ia6t_preferred
- t
));
333 if (lifetime
.ia6t_expire
) {
334 printf("%s ", lifetime
.ia6t_expire
< t
335 ? "0" : sec2str(lifetime
.ia6t_expire
- t
));
343 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
344 static struct sockaddr_in6
*sin6tab
[] = {
345 SIN6(in6_ridreq
.ifr_addr
), SIN6(in6_addreq
.ifra_addr
),
346 SIN6(in6_addreq
.ifra_prefixmask
), SIN6(in6_addreq
.ifra_dstaddr
)
350 in6_getprefix(const char *plen
, int which
)
352 struct sockaddr_in6
*sin
= sin6tab
[which
];
354 int len
= atoi(plen
);
356 if ((len
< 0) || (len
> 128))
357 errx(1, "%s: bad value", plen
);
358 sin
->sin6_len
= sizeof(*sin
);
360 sin
->sin6_family
= AF_INET6
;
361 if ((len
== 0) || (len
== 128)) {
362 memset(&sin
->sin6_addr
, 0xff, sizeof(struct in6_addr
));
365 memset((void *)&sin
->sin6_addr
, 0x00, sizeof(sin
->sin6_addr
));
366 for (cp
= (u_char
*)&sin
->sin6_addr
; len
> 7; len
-= 8)
368 *cp
= 0xff << (8 - len
);
372 in6_getaddr(const char *s
, int which
)
374 struct sockaddr_in6
*sin
= sin6tab
[which
];
375 struct addrinfo hints
, *res
;
380 sin
->sin6_len
= sizeof(*sin
);
382 sin
->sin6_family
= AF_INET6
;
386 if((p
= strrchr(s
, '/')) != NULL
) {
388 in6_getprefix(p
+ 1, MASK
);
393 if (sin
->sin6_family
== AF_INET6
) {
394 bzero(&hints
, sizeof(struct addrinfo
));
395 hints
.ai_family
= AF_INET6
;
396 error
= getaddrinfo(s
, NULL
, &hints
, &res
);
399 if (inet_pton(AF_INET6
, s
, &sin
->sin6_addr
) != 1)
400 errx(1, "%s: bad value", s
);
402 bcopy(res
->ai_addr
, sin
, res
->ai_addrlen
);
406 prefix(void *val
, int size
)
408 u_char
*name
= (u_char
*)val
;
409 int byte
, bit
, plen
= 0;
411 for (byte
= 0; byte
< size
; byte
++, plen
+= 8)
412 if (name
[byte
] != 0xff)
416 for (bit
= 7; bit
!= 0; bit
--, plen
++)
417 if (!(name
[byte
] & (1 << bit
)))
419 for (; bit
!= 0; bit
--)
420 if (name
[byte
] & (1 << bit
))
423 for (; byte
< size
; byte
++)
430 sec2str(time_t total
)
432 static char result
[256];
433 int days
, hours
, mins
, secs
;
438 days
= total
/ 3600 / 24;
439 hours
= (total
/ 3600) % 24;
440 mins
= (total
/ 60) % 60;
445 p
+= snprintf(p
, sizeof(result
) - (p
- result
), "%dd", days
);
447 if (!first
|| hours
) {
449 p
+= snprintf(p
, sizeof(result
) - (p
- result
), "%dh", hours
);
451 if (!first
|| mins
) {
453 p
+= snprintf(p
, sizeof(result
) - (p
- result
), "%dm", mins
);
455 snprintf(p
, sizeof(result
) - (p
- result
), "%ds", secs
);
457 snprintf(result
, sizeof(result
), "%lu", (unsigned long)total
);
463 in6_postproc(int s
, const struct afswtch
*afp
)
465 if (explicit_prefix
== 0) {
466 /* Aggregatable address architecture defines all prefixes
467 are 64. So, it is convenient to set prefixlen to 64 if
468 it is not specified. */
469 setifprefixlen("64", 0, s
, afp
);
470 /* in6_getprefix("64", MASK) if MASK is available here... */
475 in6_status_tunnel(int s
)
477 char src
[NI_MAXHOST
];
478 char dst
[NI_MAXHOST
];
479 struct in6_ifreq in6_ifr
;
480 const struct sockaddr
*sa
= (const struct sockaddr
*) &in6_ifr
.ifr_addr
;
482 memset(&in6_ifr
, 0, sizeof(in6_ifr
));
483 strncpy(in6_ifr
.ifr_name
, name
, IFNAMSIZ
);
485 if (ioctl(s
, SIOCGIFPSRCADDR_IN6
, (caddr_t
)&in6_ifr
) < 0)
487 if (sa
->sa_family
!= AF_INET6
)
489 in6_fillscopeid(&in6_ifr
.ifr_addr
);
490 if (getnameinfo(sa
, sa
->sa_len
, src
, sizeof(src
), 0, 0,
491 NI_NUMERICHOST
) != 0)
494 if (ioctl(s
, SIOCGIFPDSTADDR_IN6
, (caddr_t
)&in6_ifr
) < 0)
496 if (sa
->sa_family
!= AF_INET6
)
498 in6_fillscopeid(&in6_ifr
.ifr_addr
);
499 if (getnameinfo(sa
, sa
->sa_len
, dst
, sizeof(dst
), 0, 0,
500 NI_NUMERICHOST
) != 0)
503 printf("\ttunnel inet6 %s --> %s\n", src
, dst
);
507 in6_set_tunnel(int s
, struct addrinfo
*srcres
, struct addrinfo
*dstres
)
509 struct in6_aliasreq in6_addreq
;
511 memset(&in6_addreq
, 0, sizeof(in6_addreq
));
512 strncpy(in6_addreq
.ifra_name
, name
, IFNAMSIZ
);
513 memcpy(&in6_addreq
.ifra_addr
, srcres
->ai_addr
, srcres
->ai_addr
->sa_len
);
514 memcpy(&in6_addreq
.ifra_dstaddr
, dstres
->ai_addr
,
515 dstres
->ai_addr
->sa_len
);
517 if (ioctl(s
, SIOCSIFPHYADDR_IN6
, &in6_addreq
) < 0)
518 warn("SIOCSIFPHYADDR_IN6");
522 in6_set_router(int s
, int enable
)
526 bzero(&ifr
, sizeof (ifr
));
527 strncpy(ifr
.ifr_name
, name
, IFNAMSIZ
);
528 ifr
.ifr_intval
= enable
;
530 if (ioctl(s
, SIOCSETROUTERMODE_IN6
, &ifr
) < 0)
531 warn("SIOCSETROUTERMODE_IN6");
534 static struct cmd inet6_cmds
[] = {
535 DEF_CMD_ARG("prefixlen", setifprefixlen
),
536 DEF_CMD("anycast", IN6_IFF_ANYCAST
, setip6flags
),
537 DEF_CMD("tentative", IN6_IFF_TENTATIVE
, setip6flags
),
538 DEF_CMD("-tentative", -IN6_IFF_TENTATIVE
, setip6flags
),
539 /* RFC 4429, section 3.1, says:
540 * "Optimistic DAD SHOULD NOT be used for manually entered
543 * DEF_CMD("optimistic", IN6_IFF_OPTIMISTIC, setip6flags),
544 * DEF_CMD("-optimistic", -IN6_IFF_OPTIMISTIC, setip6flags),
546 DEF_CMD("deprecated", IN6_IFF_DEPRECATED
, setip6flags
),
547 DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED
, setip6flags
),
548 DEF_CMD("autoconf", IN6_IFF_AUTOCONF
, setip6flags
),
549 DEF_CMD("-autoconf", -IN6_IFF_AUTOCONF
, setip6flags
),
550 DEF_CMD_ARG("pltime", setip6pltime
),
551 DEF_CMD_ARG("vltime", setip6vltime
),
552 DEF_CMD("eui64", 0, setip6eui64
),
555 static struct afswtch af_inet6
= {
558 .af_status
= in6_status
,
559 .af_getaddr
= in6_getaddr
,
560 .af_getprefix
= in6_getprefix
,
561 .af_postproc
= in6_postproc
,
562 .af_status_tunnel
= in6_status_tunnel
,
563 .af_settunnel
= in6_set_tunnel
,
564 .af_setrouter
= in6_set_router
,
565 .af_difaddr
= SIOCDIFADDR_IN6
,
566 .af_aifaddr
= SIOCAIFADDR_IN6
,
567 .af_ridreq
= &in6_ridreq
,
568 .af_addreq
= &in6_addreq
,
572 in6_Lopt_cb(const char *optarg __unused
)
574 ip6lifetime
++; /* print IPv6 address lifetime */
576 static struct option in6_Lopt
= { "L", "[-L]", in6_Lopt_cb
};
578 static __constructor
void
581 #define N(a) (sizeof(a) / sizeof(a[0]))
584 for (i
= 0; i
< N(inet6_cmds
); i
++)
585 cmd_register(&inet6_cmds
[i
]);
586 af_register(&af_inet6
);
587 opt_register(&in6_Lopt
);