Commit | Line | Data |
---|---|---|
ca74a0a2 SZ |
1 | /* |
2 | * Copyright (c) 1983, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
dc71b7ab | 13 | * 3. Neither the name of the University nor the names of its contributors |
ca74a0a2 SZ |
14 | * may be used to endorse or promote products derived from this software |
15 | * without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
27 | * SUCH DAMAGE. | |
28 | * | |
29 | * $FreeBSD: src/sbin/ifconfig/af_inet6.c,v 1.3 2005/06/16 19:37:09 ume Exp $ | |
ca74a0a2 SZ |
30 | */ |
31 | ||
32 | #include <sys/param.h> | |
33 | #include <sys/ioctl.h> | |
34 | #include <sys/socket.h> | |
35 | #include <net/if.h> | |
46158ff5 | 36 | #include <net/if_var.h> /* for struct ifaddr */ |
46158ff5 AL |
37 | #include <netinet/in.h> |
38 | #include <netinet/in_var.h> | |
39 | #include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */ | |
40 | #include <arpa/inet.h> | |
41 | #include <netdb.h> | |
ca74a0a2 SZ |
42 | |
43 | #include <err.h> | |
5ecbd701 | 44 | #include <ifaddrs.h> |
6dc504c3 | 45 | #include <stdbool.h> |
ca74a0a2 SZ |
46 | #include <stdio.h> |
47 | #include <stdlib.h> | |
48 | #include <string.h> | |
65c2d39e | 49 | #include <time.h> |
5ecbd701 | 50 | #include <unistd.h> |
ca74a0a2 | 51 | |
ca74a0a2 SZ |
52 | #include "ifconfig.h" |
53 | ||
54 | static struct in6_ifreq in6_ridreq; | |
46158ff5 AL |
55 | static struct in6_aliasreq in6_addreq = |
56 | { { 0 }, | |
57 | { 0 }, | |
58 | { 0 }, | |
59 | { 0 }, | |
60 | 0, | |
ca74a0a2 SZ |
61 | { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } }; |
62 | static int ip6lifetime; | |
63 | ||
64 | static void in6_fillscopeid(struct sockaddr_in6 *sin6); | |
65 | static int prefix(void *, int); | |
66 | static char *sec2str(time_t); | |
67 | static int explicit_prefix = 0; | |
68 | ||
51a3d09e | 69 | static char addr_buf[NI_MAXHOST]; /* for getnameinfo() */ |
ca74a0a2 SZ |
70 | |
71 | static void | |
63f85492 | 72 | setifprefixlen(const char *addr, int dummy __unused, int s __unused, |
ca74a0a2 SZ |
73 | const struct afswtch *afp) |
74 | { | |
75 | if (afp->af_getprefix != NULL) | |
76 | afp->af_getprefix(addr, MASK); | |
77 | explicit_prefix = 1; | |
78 | } | |
79 | ||
80 | static void | |
63f85492 | 81 | setip6flags(const char *addr __unused, int flag, int s __unused, |
ca74a0a2 SZ |
82 | const struct afswtch *afp) |
83 | { | |
84 | if (afp->af_af != AF_INET6) | |
85 | err(1, "address flags can be set only for inet6 addresses"); | |
86 | ||
87 | if (flag < 0) | |
88 | in6_addreq.ifra_flags &= ~(-flag); | |
89 | else | |
90 | in6_addreq.ifra_flags |= flag; | |
91 | } | |
92 | ||
93 | static void | |
63f85492 | 94 | setip6lifetime(const char *cmd, const char *val, int s __unused, |
ca74a0a2 SZ |
95 | const struct afswtch *afp) |
96 | { | |
65c2d39e SW |
97 | struct timespec now; |
98 | time_t newval; | |
ca74a0a2 SZ |
99 | char *ep; |
100 | ||
65c2d39e | 101 | clock_gettime(CLOCK_MONOTONIC_FAST, &now); |
ca74a0a2 SZ |
102 | newval = (time_t)strtoul(val, &ep, 0); |
103 | if (val == ep) | |
104 | errx(1, "invalid %s", cmd); | |
105 | if (afp->af_af != AF_INET6) | |
106 | errx(1, "%s not allowed for the AF", cmd); | |
107 | if (strcmp(cmd, "vltime") == 0) { | |
65c2d39e | 108 | in6_addreq.ifra_lifetime.ia6t_expire = now.tv_sec + newval; |
ca74a0a2 SZ |
109 | in6_addreq.ifra_lifetime.ia6t_vltime = newval; |
110 | } else if (strcmp(cmd, "pltime") == 0) { | |
65c2d39e | 111 | in6_addreq.ifra_lifetime.ia6t_preferred = now.tv_sec + newval; |
ca74a0a2 SZ |
112 | in6_addreq.ifra_lifetime.ia6t_pltime = newval; |
113 | } | |
114 | } | |
115 | ||
116 | static void | |
46158ff5 | 117 | setip6pltime(const char *seconds, int dummy __unused, int s, |
ca74a0a2 SZ |
118 | const struct afswtch *afp) |
119 | { | |
120 | setip6lifetime("pltime", seconds, s, afp); | |
121 | } | |
122 | ||
123 | static void | |
46158ff5 | 124 | setip6vltime(const char *seconds, int dummy __unused, int s, |
ca74a0a2 SZ |
125 | const struct afswtch *afp) |
126 | { | |
127 | setip6lifetime("vltime", seconds, s, afp); | |
128 | } | |
129 | ||
130 | static void | |
63f85492 | 131 | setip6eui64(const char *cmd, int dummy __unused, int s __unused, |
ca74a0a2 SZ |
132 | const struct afswtch *afp) |
133 | { | |
134 | struct ifaddrs *ifap, *ifa; | |
135 | const struct sockaddr_in6 *sin6 = NULL; | |
136 | const struct in6_addr *lladdr = NULL; | |
137 | struct in6_addr *in6; | |
138 | ||
139 | if (afp->af_af != AF_INET6) | |
140 | errx(EXIT_FAILURE, "%s not allowed for the AF", cmd); | |
6e278935 | 141 | in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr; |
ca74a0a2 SZ |
142 | if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) |
143 | errx(EXIT_FAILURE, "interface index is already filled"); | |
144 | if (getifaddrs(&ifap) != 0) | |
145 | err(EXIT_FAILURE, "getifaddrs"); | |
146 | for (ifa = ifap; ifa; ifa = ifa->ifa_next) { | |
147 | if (ifa->ifa_addr->sa_family == AF_INET6 && | |
7203d4e3 | 148 | strcmp(ifa->ifa_name, IfName) == 0) { |
ca74a0a2 SZ |
149 | sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr; |
150 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { | |
151 | lladdr = &sin6->sin6_addr; | |
152 | break; | |
153 | } | |
154 | } | |
155 | } | |
156 | if (!lladdr) | |
46158ff5 | 157 | errx(EXIT_FAILURE, "could not determine link local address"); |
ca74a0a2 | 158 | |
6e278935 | 159 | memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8); |
ca74a0a2 SZ |
160 | |
161 | freeifaddrs(ifap); | |
162 | } | |
163 | ||
164 | static void | |
165 | in6_fillscopeid(struct sockaddr_in6 *sin6) | |
166 | { | |
167 | #if defined(__KAME__) && defined(KAME_SCOPEID) | |
168 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { | |
169 | sin6->sin6_scope_id = | |
170 | ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]); | |
171 | sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0; | |
172 | } | |
63f85492 AL |
173 | #else |
174 | (void)sin6; | |
ca74a0a2 SZ |
175 | #endif |
176 | } | |
177 | ||
178 | static void | |
5ecbd701 | 179 | in6_status(int s __unused, const struct ifaddrs *ifa) |
ca74a0a2 SZ |
180 | { |
181 | struct sockaddr_in6 *sin, null_sin; | |
182 | struct in6_ifreq ifr6; | |
183 | int s6; | |
184 | u_int32_t flags6; | |
185 | struct in6_addrlifetime lifetime; | |
65c2d39e | 186 | struct timespec now; |
51a3d09e | 187 | int n_flags, prefixlen; |
ca74a0a2 SZ |
188 | int error; |
189 | u_int32_t scopeid; | |
190 | ||
65c2d39e SW |
191 | clock_gettime(CLOCK_MONOTONIC_FAST, &now); |
192 | ||
ca74a0a2 SZ |
193 | memset(&null_sin, 0, sizeof(null_sin)); |
194 | ||
5ecbd701 | 195 | sin = (struct sockaddr_in6 *)ifa->ifa_addr; |
ca74a0a2 SZ |
196 | if (sin == NULL) |
197 | return; | |
198 | ||
7203d4e3 | 199 | strlcpy(ifr6.ifr_name, IfName, sizeof(ifr6.ifr_name)); |
ca74a0a2 SZ |
200 | if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { |
201 | warn("socket(AF_INET6,SOCK_DGRAM)"); | |
202 | return; | |
203 | } | |
204 | ifr6.ifr_addr = *sin; | |
205 | if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) { | |
206 | warn("ioctl(SIOCGIFAFLAG_IN6)"); | |
207 | close(s6); | |
208 | return; | |
209 | } | |
210 | flags6 = ifr6.ifr_ifru.ifru_flags6; | |
211 | memset(&lifetime, 0, sizeof(lifetime)); | |
212 | ifr6.ifr_addr = *sin; | |
213 | if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) { | |
214 | warn("ioctl(SIOCGIFALIFETIME_IN6)"); | |
215 | close(s6); | |
216 | return; | |
217 | } | |
218 | lifetime = ifr6.ifr_ifru.ifru_lifetime; | |
219 | close(s6); | |
220 | ||
221 | /* XXX: embedded link local addr check */ | |
222 | if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) && | |
223 | *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) { | |
224 | u_short index; | |
225 | ||
226 | index = *(u_short *)&sin->sin6_addr.s6_addr[2]; | |
227 | *(u_short *)&sin->sin6_addr.s6_addr[2] = 0; | |
228 | if (sin->sin6_scope_id == 0) | |
229 | sin->sin6_scope_id = ntohs(index); | |
230 | } | |
231 | scopeid = sin->sin6_scope_id; | |
232 | ||
51a3d09e AL |
233 | if (f_addr != NULL && strcmp(f_addr, "fqdn") == 0) |
234 | n_flags = 0; | |
235 | else if (f_addr != NULL && strcmp(f_addr, "host") == 0) | |
236 | n_flags = NI_NOFQDN; | |
237 | else | |
238 | n_flags = NI_NUMERICHOST; | |
239 | ||
ca74a0a2 | 240 | error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf, |
51a3d09e | 241 | sizeof(addr_buf), NULL, 0, n_flags); |
ca74a0a2 SZ |
242 | if (error != 0) |
243 | inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, | |
244 | sizeof(addr_buf)); | |
51a3d09e | 245 | printf("\tinet6 %s", addr_buf); |
ca74a0a2 | 246 | |
5ecbd701 AL |
247 | if (ifa->ifa_flags & IFF_POINTOPOINT) { |
248 | sin = (struct sockaddr_in6 *)ifa->ifa_dstaddr; | |
ca74a0a2 SZ |
249 | /* |
250 | * some of the interfaces do not have valid destination | |
251 | * address. | |
252 | */ | |
5ecbd701 | 253 | if (sin != NULL && sin->sin6_family == AF_INET6) { |
ca74a0a2 SZ |
254 | /* XXX: embedded link local addr check */ |
255 | if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) && | |
256 | *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) { | |
257 | u_short index; | |
258 | ||
259 | index = *(u_short *)&sin->sin6_addr.s6_addr[2]; | |
260 | *(u_short *)&sin->sin6_addr.s6_addr[2] = 0; | |
261 | if (sin->sin6_scope_id == 0) | |
262 | sin->sin6_scope_id = ntohs(index); | |
263 | } | |
264 | ||
265 | error = getnameinfo((struct sockaddr *)sin, | |
266 | sin->sin6_len, addr_buf, | |
267 | sizeof(addr_buf), NULL, 0, | |
268 | NI_NUMERICHOST); | |
269 | if (error != 0) | |
270 | inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, | |
271 | sizeof(addr_buf)); | |
51a3d09e | 272 | printf(" --> %s", addr_buf); |
ca74a0a2 SZ |
273 | } |
274 | } | |
275 | ||
5ecbd701 AL |
276 | sin = (struct sockaddr_in6 *)ifa->ifa_netmask; |
277 | if (sin == NULL) | |
ca74a0a2 | 278 | sin = &null_sin; |
51a3d09e AL |
279 | prefixlen = prefix(&sin->sin6_addr, sizeof(struct in6_addr)); |
280 | if (f_inet6 != NULL && strcmp(f_inet6, "cidr") == 0) | |
281 | printf("/%d", prefixlen); | |
282 | else | |
283 | printf(" prefixlen %d", prefixlen); | |
ca74a0a2 SZ |
284 | |
285 | if ((flags6 & IN6_IFF_ANYCAST) != 0) | |
a143db09 | 286 | printf(" anycast"); |
ca74a0a2 | 287 | if ((flags6 & IN6_IFF_TENTATIVE) != 0) |
a143db09 | 288 | printf(" tentative"); |
ca74a0a2 | 289 | if ((flags6 & IN6_IFF_DUPLICATED) != 0) |
a143db09 | 290 | printf(" duplicated"); |
ca74a0a2 | 291 | if ((flags6 & IN6_IFF_DETACHED) != 0) |
a143db09 | 292 | printf(" detached"); |
ca74a0a2 | 293 | if ((flags6 & IN6_IFF_DEPRECATED) != 0) |
a143db09 | 294 | printf(" deprecated"); |
ca74a0a2 | 295 | if ((flags6 & IN6_IFF_AUTOCONF) != 0) |
a143db09 | 296 | printf(" autoconf"); |
ca74a0a2 | 297 | if ((flags6 & IN6_IFF_TEMPORARY) != 0) |
a143db09 | 298 | printf(" temporary"); |
ca74a0a2 SZ |
299 | |
300 | if (scopeid) | |
a143db09 | 301 | printf(" scopeid 0x%x", scopeid); |
ca74a0a2 SZ |
302 | |
303 | if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) { | |
a143db09 | 304 | printf(" pltime"); |
ca74a0a2 | 305 | if (lifetime.ia6t_preferred) { |
a143db09 | 306 | printf(" %s", lifetime.ia6t_preferred < now.tv_sec |
65c2d39e | 307 | ? "0" : sec2str(lifetime.ia6t_preferred - now.tv_sec)); |
a143db09 AL |
308 | } else { |
309 | printf(" infty"); | |
310 | } | |
ca74a0a2 | 311 | |
a143db09 | 312 | printf(" vltime"); |
ca74a0a2 | 313 | if (lifetime.ia6t_expire) { |
a143db09 | 314 | printf(" %s", lifetime.ia6t_expire < now.tv_sec |
65c2d39e | 315 | ? "0" : sec2str(lifetime.ia6t_expire - now.tv_sec)); |
a143db09 AL |
316 | } else { |
317 | printf(" infty"); | |
318 | } | |
ca74a0a2 SZ |
319 | } |
320 | ||
321 | putchar('\n'); | |
322 | } | |
323 | ||
324 | #define SIN6(x) ((struct sockaddr_in6 *) &(x)) | |
325 | static struct sockaddr_in6 *sin6tab[] = { | |
326 | SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr), | |
327 | SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr) | |
328 | }; | |
329 | ||
330 | static void | |
331 | in6_getprefix(const char *plen, int which) | |
332 | { | |
333 | struct sockaddr_in6 *sin = sin6tab[which]; | |
334 | u_char *cp; | |
335 | int len = atoi(plen); | |
336 | ||
337 | if ((len < 0) || (len > 128)) | |
338 | errx(1, "%s: bad value", plen); | |
339 | sin->sin6_len = sizeof(*sin); | |
340 | if (which != MASK) | |
341 | sin->sin6_family = AF_INET6; | |
342 | if ((len == 0) || (len == 128)) { | |
343 | memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr)); | |
344 | return; | |
345 | } | |
68a422ab | 346 | memset(&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr)); |
ca74a0a2 SZ |
347 | for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8) |
348 | *cp++ = 0xff; | |
349 | *cp = 0xff << (8 - len); | |
350 | } | |
351 | ||
352 | static void | |
353 | in6_getaddr(const char *s, int which) | |
354 | { | |
355 | struct sockaddr_in6 *sin = sin6tab[which]; | |
356 | struct addrinfo hints, *res; | |
357 | int error = -1; | |
358 | ||
6dc504c3 | 359 | newaddr = true; |
ca74a0a2 SZ |
360 | |
361 | sin->sin6_len = sizeof(*sin); | |
362 | if (which != MASK) | |
363 | sin->sin6_family = AF_INET6; | |
364 | ||
365 | if (which == ADDR) { | |
366 | char *p = NULL; | |
367 | if((p = strrchr(s, '/')) != NULL) { | |
368 | *p = '\0'; | |
369 | in6_getprefix(p + 1, MASK); | |
370 | explicit_prefix = 1; | |
371 | } | |
372 | } | |
373 | ||
374 | if (sin->sin6_family == AF_INET6) { | |
0989c651 | 375 | memset(&hints, 0, sizeof(struct addrinfo)); |
ca74a0a2 SZ |
376 | hints.ai_family = AF_INET6; |
377 | error = getaddrinfo(s, NULL, &hints, &res); | |
378 | } | |
379 | if (error != 0) { | |
380 | if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1) | |
381 | errx(1, "%s: bad value", s); | |
382 | } else | |
0989c651 | 383 | memcpy(sin, res->ai_addr, res->ai_addrlen); |
ca74a0a2 SZ |
384 | } |
385 | ||
386 | static int | |
387 | prefix(void *val, int size) | |
388 | { | |
389 | u_char *name = (u_char *)val; | |
390 | int byte, bit, plen = 0; | |
391 | ||
392 | for (byte = 0; byte < size; byte++, plen += 8) | |
393 | if (name[byte] != 0xff) | |
394 | break; | |
395 | if (byte == size) | |
396 | return (plen); | |
397 | for (bit = 7; bit != 0; bit--, plen++) | |
398 | if (!(name[byte] & (1 << bit))) | |
399 | break; | |
400 | for (; bit != 0; bit--) | |
401 | if (name[byte] & (1 << bit)) | |
402 | return(0); | |
403 | byte++; | |
404 | for (; byte < size; byte++) | |
405 | if (name[byte]) | |
406 | return(0); | |
407 | return (plen); | |
408 | } | |
409 | ||
410 | static char * | |
411 | sec2str(time_t total) | |
412 | { | |
413 | static char result[256]; | |
414 | int days, hours, mins, secs; | |
415 | int first = 1; | |
416 | char *p = result; | |
417 | ||
418 | if (0) { | |
419 | days = total / 3600 / 24; | |
420 | hours = (total / 3600) % 24; | |
421 | mins = (total / 60) % 60; | |
422 | secs = total % 60; | |
423 | ||
424 | if (days) { | |
425 | first = 0; | |
426 | p += sprintf(p, "%dd", days); | |
427 | } | |
428 | if (!first || hours) { | |
429 | first = 0; | |
430 | p += sprintf(p, "%dh", hours); | |
431 | } | |
432 | if (!first || mins) { | |
433 | first = 0; | |
434 | p += sprintf(p, "%dm", mins); | |
435 | } | |
436 | sprintf(p, "%ds", secs); | |
437 | } else | |
438 | sprintf(result, "%lu", (unsigned long)total); | |
439 | ||
440 | return(result); | |
441 | } | |
442 | ||
443 | static void | |
444 | in6_postproc(int s, const struct afswtch *afp) | |
445 | { | |
446 | if (explicit_prefix == 0) { | |
447 | /* Aggregatable address architecture defines all prefixes | |
448 | are 64. So, it is convenient to set prefixlen to 64 if | |
449 | it is not specified. */ | |
450 | setifprefixlen("64", 0, s, afp); | |
451 | /* in6_getprefix("64", MASK) if MASK is available here... */ | |
452 | } | |
453 | } | |
454 | ||
455 | static void | |
456 | in6_status_tunnel(int s) | |
457 | { | |
458 | char src[NI_MAXHOST]; | |
459 | char dst[NI_MAXHOST]; | |
460 | struct in6_ifreq in6_ifr; | |
461 | const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr; | |
462 | ||
463 | memset(&in6_ifr, 0, sizeof(in6_ifr)); | |
7203d4e3 | 464 | strlcpy(in6_ifr.ifr_name, IfName, sizeof(in6_ifr.ifr_name)); |
ca74a0a2 | 465 | |
0b22c3cf | 466 | if (ioctl(s, SIOCGIFPSRCADDR_IN6, &in6_ifr) < 0) |
ca74a0a2 SZ |
467 | return; |
468 | if (sa->sa_family != AF_INET6) | |
469 | return; | |
470 | in6_fillscopeid(&in6_ifr.ifr_addr); | |
471 | if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, | |
472 | NI_NUMERICHOST) != 0) | |
473 | src[0] = '\0'; | |
474 | ||
0b22c3cf | 475 | if (ioctl(s, SIOCGIFPDSTADDR_IN6, &in6_ifr) < 0) |
ca74a0a2 SZ |
476 | return; |
477 | if (sa->sa_family != AF_INET6) | |
478 | return; | |
479 | in6_fillscopeid(&in6_ifr.ifr_addr); | |
480 | if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, | |
481 | NI_NUMERICHOST) != 0) | |
482 | dst[0] = '\0'; | |
483 | ||
484 | printf("\ttunnel inet6 %s --> %s\n", src, dst); | |
485 | } | |
486 | ||
487 | static void | |
488 | in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres) | |
489 | { | |
18983bb4 | 490 | struct in6_aliasreq addreq; |
ca74a0a2 | 491 | |
18983bb4 | 492 | memset(&addreq, 0, sizeof(addreq)); |
7203d4e3 | 493 | strlcpy(addreq.ifra_name, IfName, sizeof(addreq.ifra_name)); |
18983bb4 AL |
494 | memcpy(&addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len); |
495 | memcpy(&addreq.ifra_dstaddr, dstres->ai_addr, dstres->ai_addr->sa_len); | |
ca74a0a2 | 496 | |
18983bb4 | 497 | if (ioctl(s, SIOCSIFPHYADDR_IN6, &addreq) < 0) |
ca74a0a2 SZ |
498 | warn("SIOCSIFPHYADDR_IN6"); |
499 | } | |
500 | ||
501 | static struct cmd inet6_cmds[] = { | |
502 | DEF_CMD_ARG("prefixlen", setifprefixlen), | |
503 | DEF_CMD("anycast", IN6_IFF_ANYCAST, setip6flags), | |
504 | DEF_CMD("tentative", IN6_IFF_TENTATIVE, setip6flags), | |
505 | DEF_CMD("-tentative", -IN6_IFF_TENTATIVE, setip6flags), | |
506 | DEF_CMD("deprecated", IN6_IFF_DEPRECATED, setip6flags), | |
507 | DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED, setip6flags), | |
508 | DEF_CMD("autoconf", IN6_IFF_AUTOCONF, setip6flags), | |
509 | DEF_CMD("-autoconf", -IN6_IFF_AUTOCONF, setip6flags), | |
46158ff5 AL |
510 | DEF_CMD_ARG("pltime", setip6pltime), |
511 | DEF_CMD_ARG("vltime", setip6vltime), | |
ca74a0a2 SZ |
512 | DEF_CMD("eui64", 0, setip6eui64), |
513 | }; | |
514 | ||
515 | static struct afswtch af_inet6 = { | |
516 | .af_name = "inet6", | |
517 | .af_af = AF_INET6, | |
518 | .af_status = in6_status, | |
519 | .af_getaddr = in6_getaddr, | |
520 | .af_getprefix = in6_getprefix, | |
521 | .af_postproc = in6_postproc, | |
522 | .af_status_tunnel = in6_status_tunnel, | |
523 | .af_settunnel = in6_set_tunnel, | |
524 | .af_difaddr = SIOCDIFADDR_IN6, | |
525 | .af_aifaddr = SIOCAIFADDR_IN6, | |
dd1649aa | 526 | .af_ridreq = &in6_ridreq, |
ca74a0a2 SZ |
527 | .af_addreq = &in6_addreq, |
528 | }; | |
529 | ||
530 | static void | |
18983bb4 | 531 | in6_Lopt_cb(const char *arg __unused) |
ca74a0a2 SZ |
532 | { |
533 | ip6lifetime++; /* print IPv6 address lifetime */ | |
534 | } | |
46158ff5 | 535 | static struct option in6_Lopt = { "L", "[-L]", in6_Lopt_cb, NULL }; |
ca74a0a2 | 536 | |
ec7e0eeb AL |
537 | __constructor(113) |
538 | static void | |
ca74a0a2 SZ |
539 | inet6_ctor(void) |
540 | { | |
46158ff5 | 541 | size_t i; |
ca74a0a2 | 542 | |
b6b91ec7 | 543 | for (i = 0; i < nitems(inet6_cmds); i++) |
ca74a0a2 | 544 | cmd_register(&inet6_cmds[i]); |
93b0f758 | 545 | |
ca74a0a2 SZ |
546 | af_register(&af_inet6); |
547 | opt_register(&in6_Lopt); | |
ca74a0a2 | 548 | } |