]> git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/af_inet6.c
network_cmds-356.9.tar.gz
[apple/network_cmds.git] / ifconfig.tproj / af_inet6.c
1 /*
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 /*
30 * Copyright (c) 1983, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
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.
44 *
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
55 * SUCH DAMAGE.
56 */
57
58 #ifndef lint
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 $";
61 #endif /* not lint */
62
63 #include <sys/param.h>
64 #include <sys/ioctl.h>
65 #include <sys/socket.h>
66 #include <net/if.h>
67
68 #include <err.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <unistd.h>
73 #include <ifaddrs.h>
74
75 #include <arpa/inet.h>
76
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>
81 #include <netdb.h>
82
83 #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */
84
85 #include "ifconfig.h"
86
87 static struct in6_ifreq in6_ridreq;
88 static struct in6_aliasreq in6_addreq =
89 { { 0 },
90 { 0 },
91 { 0 },
92 { 0 },
93 0,
94 { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } };
95 static int ip6lifetime;
96
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;
101
102 static char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/
103
104 static void
105 setifprefixlen(const char *addr, int dummy __unused, int s,
106 const struct afswtch *afp)
107 {
108 if (afp->af_getprefix != NULL)
109 afp->af_getprefix(addr, MASK);
110 explicit_prefix = 1;
111 }
112
113 static void
114 setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused,
115 const struct afswtch *afp)
116 {
117 if (afp->af_af != AF_INET6)
118 err(1, "address flags can be set only for inet6 addresses");
119
120 if (flag < 0)
121 in6_addreq.ifra_flags &= ~(-flag);
122 else
123 in6_addreq.ifra_flags |= flag;
124 }
125
126 static void
127 setip6lifetime(const char *cmd, const char *val, int s,
128 const struct afswtch *afp)
129 {
130 time_t newval, t;
131 char *ep;
132
133 t = time(NULL);
134 newval = (time_t)strtoul(val, &ep, 0);
135 if (val == ep)
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;
145 }
146 }
147
148 static void
149 setip6pltime(const char *seconds, int dummy __unused, int s,
150 const struct afswtch *afp)
151 {
152 setip6lifetime("pltime", seconds, s, afp);
153 }
154
155 static void
156 setip6vltime(const char *seconds, int dummy __unused, int s,
157 const struct afswtch *afp)
158 {
159 setip6lifetime("vltime", seconds, s, afp);
160 }
161
162 static void
163 setip6eui64(const char *cmd, int dummy __unused, int s,
164 const struct afswtch *afp)
165 {
166 struct ifaddrs *ifap, *ifa;
167 const struct sockaddr_in6 *sin6 = NULL;
168 const struct in6_addr *lladdr = NULL;
169 struct in6_addr *in6;
170
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;
184 break;
185 }
186 }
187 }
188 if (!lladdr)
189 errx(EXIT_FAILURE, "could not determine link local address");
190
191 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8);
192
193 freeifaddrs(ifap);
194 }
195
196 static void
197 in6_fillscopeid(struct sockaddr_in6 *sin6)
198 {
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;
204 }
205 #endif
206 }
207
208 static void
209 in6_status(int s __unused, const struct ifaddrs *ifa)
210 {
211 struct sockaddr_in6 *sin, null_sin;
212 struct in6_ifreq ifr6;
213 int s6;
214 u_int32_t flags6;
215 struct in6_addrlifetime lifetime;
216 time_t t = time(NULL);
217 int error;
218 u_int32_t scopeid;
219
220 memset(&null_sin, 0, sizeof(null_sin));
221
222 sin = (struct sockaddr_in6 *)ifa->ifa_addr;
223 if (sin == NULL)
224 return;
225
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)");
229 return;
230 }
231 ifr6.ifr_addr = *sin;
232 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
233 warn("ioctl(SIOCGIFAFLAG_IN6)");
234 close(s6);
235 return;
236 }
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)");
242 close(s6);
243 return;
244 }
245 lifetime = ifr6.ifr_ifru.ifru_lifetime;
246 close(s6);
247
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) {
251 u_short index;
252
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);
257 }
258 scopeid = sin->sin6_scope_id;
259
260 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
261 sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
262 if (error != 0)
263 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
264 sizeof(addr_buf));
265 printf("\tinet6 %s ", addr_buf);
266
267 if (ifa->ifa_flags & IFF_POINTOPOINT) {
268 sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
269 /*
270 * some of the interfaces do not have valid destination
271 * address.
272 */
273 if (sin != NULL && sin->sin6_family == AF_INET6) {
274 int error;
275
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) {
279 u_short index;
280
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);
285 }
286
287 error = getnameinfo((struct sockaddr *)sin,
288 sin->sin6_len, addr_buf,
289 sizeof(addr_buf), NULL, 0,
290 NI_NUMERICHOST);
291 if (error != 0)
292 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf,
293 sizeof(addr_buf));
294 printf("--> %s ", addr_buf);
295 }
296 }
297
298 sin = (struct sockaddr_in6 *)ifa->ifa_netmask;
299 if (sin == NULL)
300 sin = &null_sin;
301 printf("prefixlen %d ", prefix(&sin->sin6_addr,
302 sizeof(struct in6_addr)));
303
304 if ((flags6 & IN6_IFF_ANYCAST) != 0)
305 printf("anycast ");
306 if ((flags6 & IN6_IFF_TENTATIVE) != 0)
307 printf("tentative ");
308 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
309 printf("duplicated ");
310 if ((flags6 & IN6_IFF_DETACHED) != 0)
311 printf("detached ");
312 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
313 printf("deprecated ");
314 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
315 printf("autoconf ");
316 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
317 printf("temporary ");
318
319 if (scopeid)
320 printf("scopeid 0x%x ", scopeid);
321
322 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
323 printf("pltime ");
324 if (lifetime.ia6t_preferred) {
325 printf("%s ", lifetime.ia6t_preferred < t
326 ? "0" : sec2str(lifetime.ia6t_preferred - t));
327 } else
328 printf("infty ");
329
330 printf("vltime ");
331 if (lifetime.ia6t_expire) {
332 printf("%s ", lifetime.ia6t_expire < t
333 ? "0" : sec2str(lifetime.ia6t_expire - t));
334 } else
335 printf("infty ");
336 }
337
338 putchar('\n');
339 }
340
341 #define SIN6(x) ((struct sockaddr_in6 *) &(x))
342 static struct sockaddr_in6 *sin6tab[] = {
343 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr),
344 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr)
345 };
346
347 static void
348 in6_getprefix(const char *plen, int which)
349 {
350 struct sockaddr_in6 *sin = sin6tab[which];
351 u_char *cp;
352 int len = atoi(plen);
353
354 if ((len < 0) || (len > 128))
355 errx(1, "%s: bad value", plen);
356 sin->sin6_len = sizeof(*sin);
357 if (which != MASK)
358 sin->sin6_family = AF_INET6;
359 if ((len == 0) || (len == 128)) {
360 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
361 return;
362 }
363 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
364 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
365 *cp++ = 0xff;
366 *cp = 0xff << (8 - len);
367 }
368
369 static void
370 in6_getaddr(const char *s, int which)
371 {
372 struct sockaddr_in6 *sin = sin6tab[which];
373 struct addrinfo hints, *res;
374 int error = -1;
375
376 newaddr &= 1;
377
378 sin->sin6_len = sizeof(*sin);
379 if (which != MASK)
380 sin->sin6_family = AF_INET6;
381
382 if (which == ADDR) {
383 char *p = NULL;
384 if((p = strrchr(s, '/')) != NULL) {
385 *p = '\0';
386 in6_getprefix(p + 1, MASK);
387 explicit_prefix = 1;
388 }
389 }
390
391 if (sin->sin6_family == AF_INET6) {
392 bzero(&hints, sizeof(struct addrinfo));
393 hints.ai_family = AF_INET6;
394 error = getaddrinfo(s, NULL, &hints, &res);
395 }
396 if (error != 0) {
397 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
398 errx(1, "%s: bad value", s);
399 } else
400 bcopy(res->ai_addr, sin, res->ai_addrlen);
401 }
402
403 static int
404 prefix(void *val, int size)
405 {
406 u_char *name = (u_char *)val;
407 int byte, bit, plen = 0;
408
409 for (byte = 0; byte < size; byte++, plen += 8)
410 if (name[byte] != 0xff)
411 break;
412 if (byte == size)
413 return (plen);
414 for (bit = 7; bit != 0; bit--, plen++)
415 if (!(name[byte] & (1 << bit)))
416 break;
417 for (; bit != 0; bit--)
418 if (name[byte] & (1 << bit))
419 return(0);
420 byte++;
421 for (; byte < size; byte++)
422 if (name[byte])
423 return(0);
424 return (plen);
425 }
426
427 static char *
428 sec2str(time_t total)
429 {
430 static char result[256];
431 int days, hours, mins, secs;
432 int first = 1;
433 char *p = result;
434
435 if (0) {
436 days = total / 3600 / 24;
437 hours = (total / 3600) % 24;
438 mins = (total / 60) % 60;
439 secs = total % 60;
440
441 if (days) {
442 first = 0;
443 p += snprintf(p, sizeof(result) - (p - result), "%dd", days);
444 }
445 if (!first || hours) {
446 first = 0;
447 p += snprintf(p, sizeof(result) - (p - result), "%dh", hours);
448 }
449 if (!first || mins) {
450 first = 0;
451 p += snprintf(p, sizeof(result) - (p - result), "%dm", mins);
452 }
453 snprintf(p, sizeof(result) - (p - result), "%ds", secs);
454 } else
455 snprintf(result, sizeof(result), "%lu", (unsigned long)total);
456
457 return(result);
458 }
459
460 static void
461 in6_postproc(int s, const struct afswtch *afp)
462 {
463 if (explicit_prefix == 0) {
464 /* Aggregatable address architecture defines all prefixes
465 are 64. So, it is convenient to set prefixlen to 64 if
466 it is not specified. */
467 setifprefixlen("64", 0, s, afp);
468 /* in6_getprefix("64", MASK) if MASK is available here... */
469 }
470 }
471
472 static void
473 in6_status_tunnel(int s)
474 {
475 char src[NI_MAXHOST];
476 char dst[NI_MAXHOST];
477 struct in6_ifreq in6_ifr;
478 const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr;
479
480 memset(&in6_ifr, 0, sizeof(in6_ifr));
481 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
482
483 if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
484 return;
485 if (sa->sa_family != AF_INET6)
486 return;
487 in6_fillscopeid(&in6_ifr.ifr_addr);
488 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
489 NI_NUMERICHOST) != 0)
490 src[0] = '\0';
491
492 if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
493 return;
494 if (sa->sa_family != AF_INET6)
495 return;
496 in6_fillscopeid(&in6_ifr.ifr_addr);
497 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
498 NI_NUMERICHOST) != 0)
499 dst[0] = '\0';
500
501 printf("\ttunnel inet6 %s --> %s\n", src, dst);
502 }
503
504 static void
505 in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
506 {
507 struct in6_aliasreq in6_addreq;
508
509 memset(&in6_addreq, 0, sizeof(in6_addreq));
510 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ);
511 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len);
512 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr,
513 dstres->ai_addr->sa_len);
514
515 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
516 warn("SIOCSIFPHYADDR_IN6");
517 }
518
519 static struct cmd inet6_cmds[] = {
520 DEF_CMD_ARG("prefixlen", setifprefixlen),
521 DEF_CMD("anycast", IN6_IFF_ANYCAST, setip6flags),
522 DEF_CMD("tentative", IN6_IFF_TENTATIVE, setip6flags),
523 DEF_CMD("-tentative", -IN6_IFF_TENTATIVE, setip6flags),
524 DEF_CMD("deprecated", IN6_IFF_DEPRECATED, setip6flags),
525 DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED, setip6flags),
526 DEF_CMD("autoconf", IN6_IFF_AUTOCONF, setip6flags),
527 DEF_CMD("-autoconf", -IN6_IFF_AUTOCONF, setip6flags),
528 DEF_CMD_ARG("pltime", setip6pltime),
529 DEF_CMD_ARG("vltime", setip6vltime),
530 DEF_CMD("eui64", 0, setip6eui64),
531 };
532
533 static struct afswtch af_inet6 = {
534 .af_name = "inet6",
535 .af_af = AF_INET6,
536 .af_status = in6_status,
537 .af_getaddr = in6_getaddr,
538 .af_getprefix = in6_getprefix,
539 .af_postproc = in6_postproc,
540 .af_status_tunnel = in6_status_tunnel,
541 .af_settunnel = in6_set_tunnel,
542 .af_difaddr = SIOCDIFADDR_IN6,
543 .af_aifaddr = SIOCAIFADDR_IN6,
544 .af_ridreq = &in6_addreq,
545 .af_addreq = &in6_addreq,
546 };
547
548 static void
549 in6_Lopt_cb(const char *optarg __unused)
550 {
551 ip6lifetime++; /* print IPv6 address lifetime */
552 }
553 static struct option in6_Lopt = { "L", "[-L]", in6_Lopt_cb };
554
555 static __constructor void
556 inet6_ctor(void)
557 {
558 #define N(a) (sizeof(a) / sizeof(a[0]))
559 int i;
560
561 for (i = 0; i < N(inet6_cmds); i++)
562 cmd_register(&inet6_cmds[i]);
563 af_register(&af_inet6);
564 opt_register(&in6_Lopt);
565 #undef N
566 }