]> git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/af_inet6.c
0f32774d64b4b82b7b73ef86db89a7d1690c2a07
[apple/network_cmds.git] / ifconfig.tproj / af_inet6.c
1 /*
2 * Copyright (c) 2009-2011 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_OPTIMISTIC) != 0)
309 printf("optimistic ");
310 if ((flags6 & IN6_IFF_DUPLICATED) != 0)
311 printf("duplicated ");
312 if ((flags6 & IN6_IFF_DETACHED) != 0)
313 printf("detached ");
314 if ((flags6 & IN6_IFF_DEPRECATED) != 0)
315 printf("deprecated ");
316 if ((flags6 & IN6_IFF_AUTOCONF) != 0)
317 printf("autoconf ");
318 if ((flags6 & IN6_IFF_TEMPORARY) != 0)
319 printf("temporary ");
320
321 if (scopeid)
322 printf("scopeid 0x%x ", scopeid);
323
324 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) {
325 printf("pltime ");
326 if (lifetime.ia6t_preferred) {
327 printf("%s ", lifetime.ia6t_preferred < t
328 ? "0" : sec2str(lifetime.ia6t_preferred - t));
329 } else
330 printf("infty ");
331
332 printf("vltime ");
333 if (lifetime.ia6t_expire) {
334 printf("%s ", lifetime.ia6t_expire < t
335 ? "0" : sec2str(lifetime.ia6t_expire - t));
336 } else
337 printf("infty ");
338 }
339
340 putchar('\n');
341 }
342
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)
347 };
348
349 static void
350 in6_getprefix(const char *plen, int which)
351 {
352 struct sockaddr_in6 *sin = sin6tab[which];
353 u_char *cp;
354 int len = atoi(plen);
355
356 if ((len < 0) || (len > 128))
357 errx(1, "%s: bad value", plen);
358 sin->sin6_len = sizeof(*sin);
359 if (which != MASK)
360 sin->sin6_family = AF_INET6;
361 if ((len == 0) || (len == 128)) {
362 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr));
363 return;
364 }
365 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr));
366 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8)
367 *cp++ = 0xff;
368 *cp = 0xff << (8 - len);
369 }
370
371 static void
372 in6_getaddr(const char *s, int which)
373 {
374 struct sockaddr_in6 *sin = sin6tab[which];
375 struct addrinfo hints, *res;
376 int error = -1;
377
378 newaddr &= 1;
379
380 sin->sin6_len = sizeof(*sin);
381 if (which != MASK)
382 sin->sin6_family = AF_INET6;
383
384 if (which == ADDR) {
385 char *p = NULL;
386 if((p = strrchr(s, '/')) != NULL) {
387 *p = '\0';
388 in6_getprefix(p + 1, MASK);
389 explicit_prefix = 1;
390 }
391 }
392
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);
397 }
398 if (error != 0) {
399 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1)
400 errx(1, "%s: bad value", s);
401 } else
402 bcopy(res->ai_addr, sin, res->ai_addrlen);
403 }
404
405 static int
406 prefix(void *val, int size)
407 {
408 u_char *name = (u_char *)val;
409 int byte, bit, plen = 0;
410
411 for (byte = 0; byte < size; byte++, plen += 8)
412 if (name[byte] != 0xff)
413 break;
414 if (byte == size)
415 return (plen);
416 for (bit = 7; bit != 0; bit--, plen++)
417 if (!(name[byte] & (1 << bit)))
418 break;
419 for (; bit != 0; bit--)
420 if (name[byte] & (1 << bit))
421 return(0);
422 byte++;
423 for (; byte < size; byte++)
424 if (name[byte])
425 return(0);
426 return (plen);
427 }
428
429 static char *
430 sec2str(time_t total)
431 {
432 static char result[256];
433 int days, hours, mins, secs;
434 int first = 1;
435 char *p = result;
436
437 if (0) {
438 days = total / 3600 / 24;
439 hours = (total / 3600) % 24;
440 mins = (total / 60) % 60;
441 secs = total % 60;
442
443 if (days) {
444 first = 0;
445 p += snprintf(p, sizeof(result) - (p - result), "%dd", days);
446 }
447 if (!first || hours) {
448 first = 0;
449 p += snprintf(p, sizeof(result) - (p - result), "%dh", hours);
450 }
451 if (!first || mins) {
452 first = 0;
453 p += snprintf(p, sizeof(result) - (p - result), "%dm", mins);
454 }
455 snprintf(p, sizeof(result) - (p - result), "%ds", secs);
456 } else
457 snprintf(result, sizeof(result), "%lu", (unsigned long)total);
458
459 return(result);
460 }
461
462 static void
463 in6_postproc(int s, const struct afswtch *afp)
464 {
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... */
471 }
472 }
473
474 static void
475 in6_status_tunnel(int s)
476 {
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;
481
482 memset(&in6_ifr, 0, sizeof(in6_ifr));
483 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ);
484
485 if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0)
486 return;
487 if (sa->sa_family != AF_INET6)
488 return;
489 in6_fillscopeid(&in6_ifr.ifr_addr);
490 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0,
491 NI_NUMERICHOST) != 0)
492 src[0] = '\0';
493
494 if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0)
495 return;
496 if (sa->sa_family != AF_INET6)
497 return;
498 in6_fillscopeid(&in6_ifr.ifr_addr);
499 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0,
500 NI_NUMERICHOST) != 0)
501 dst[0] = '\0';
502
503 printf("\ttunnel inet6 %s --> %s\n", src, dst);
504 }
505
506 static void
507 in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres)
508 {
509 struct in6_aliasreq in6_addreq;
510
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);
516
517 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0)
518 warn("SIOCSIFPHYADDR_IN6");
519 }
520
521 static void
522 in6_set_router(int s, int enable)
523 {
524 struct ifreq ifr;
525
526 bzero(&ifr, sizeof (ifr));
527 strncpy(ifr.ifr_name, name, IFNAMSIZ);
528 ifr.ifr_intval = enable;
529
530 if (ioctl(s, SIOCSETROUTERMODE_IN6, &ifr) < 0)
531 warn("SIOCSETROUTERMODE_IN6");
532 }
533
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
541 * addresses."
542 *
543 * DEF_CMD("optimistic", IN6_IFF_OPTIMISTIC, setip6flags),
544 * DEF_CMD("-optimistic", -IN6_IFF_OPTIMISTIC, setip6flags),
545 */
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),
553 };
554
555 static struct afswtch af_inet6 = {
556 .af_name = "inet6",
557 .af_af = 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,
569 };
570
571 static void
572 in6_Lopt_cb(const char *optarg __unused)
573 {
574 ip6lifetime++; /* print IPv6 address lifetime */
575 }
576 static struct option in6_Lopt = { "L", "[-L]", in6_Lopt_cb };
577
578 static __constructor void
579 inet6_ctor(void)
580 {
581 #define N(a) (sizeof(a) / sizeof(a[0]))
582 int i;
583
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);
588 #undef N
589 }