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. 13 * 4. Neither the name of the University nor the names of its contributors 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 30#ifndef lint 31static const char rcsid[] =
| 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. 13 * 4. Neither the name of the University nor the names of its contributors 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 30#ifndef lint 31static const char rcsid[] =
|
32 "$FreeBSD: head/sbin/ifconfig/af_inet6.c 138593 2004-12-08 19:18:07Z sam $";
| 32 "$FreeBSD: head/sbin/ifconfig/af_inet6.c 146187 2005-05-13 16:31:11Z ume $";
|
33#endif /* not lint */ 34 35#include <sys/param.h> 36#include <sys/ioctl.h> 37#include <sys/socket.h> 38#include <net/if.h> 39#include <net/route.h> /* for RTX_IFA */ 40 41#include <err.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <unistd.h> 46#include <ifaddrs.h> 47 48#include <arpa/inet.h> 49 50#include <netinet/in.h> 51#include <net/if_var.h> /* for struct ifaddr */ 52#include <netinet/in_var.h> 53#include <arpa/inet.h> 54#include <netdb.h> 55 56#include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */ 57 58#include "ifconfig.h" 59
| 33#endif /* not lint */ 34 35#include <sys/param.h> 36#include <sys/ioctl.h> 37#include <sys/socket.h> 38#include <net/if.h> 39#include <net/route.h> /* for RTX_IFA */ 40 41#include <err.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <unistd.h> 46#include <ifaddrs.h> 47 48#include <arpa/inet.h> 49 50#include <netinet/in.h> 51#include <net/if_var.h> /* for struct ifaddr */ 52#include <netinet/in_var.h> 53#include <arpa/inet.h> 54#include <netdb.h> 55 56#include <netinet6/nd6.h> /* Define ND6_INFINITE_LIFETIME */ 57 58#include "ifconfig.h" 59
|
60/* wrapper for KAME-special getnameinfo() */ 61#ifndef NI_WITHSCOPEID 62#define NI_WITHSCOPEID 0 63#endif 64
| |
65static struct in6_ifreq in6_ridreq; 66static struct in6_aliasreq in6_addreq = 67 { { 0 }, 68 { 0 }, 69 { 0 }, 70 { 0 }, 71 0, 72 { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } }; 73static int ip6lifetime; 74 75static void in6_fillscopeid(struct sockaddr_in6 *sin6); 76static int prefix(void *, int); 77static char *sec2str(time_t); 78static int explicit_prefix = 0; 79 80static char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/ 81 82static void 83setifprefixlen(const char *addr, int dummy __unused, int s, 84 const struct afswtch *afp) 85{ 86 if (afp->af_getprefix != NULL) 87 afp->af_getprefix(addr, MASK); 88 explicit_prefix = 1; 89} 90 91static void 92setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused, 93 const struct afswtch *afp) 94{ 95 if (afp->af_af != AF_INET6) 96 err(1, "address flags can be set only for inet6 addresses"); 97 98 if (flag < 0) 99 in6_addreq.ifra_flags &= ~(-flag); 100 else 101 in6_addreq.ifra_flags |= flag; 102} 103 104static void 105setip6lifetime(const char *cmd, const char *val, int s, 106 const struct afswtch *afp) 107{ 108 time_t newval, t; 109 char *ep; 110 111 t = time(NULL); 112 newval = (time_t)strtoul(val, &ep, 0); 113 if (val == ep) 114 errx(1, "invalid %s", cmd); 115 if (afp->af_af != AF_INET6) 116 errx(1, "%s not allowed for the AF", cmd); 117 if (strcmp(cmd, "vltime") == 0) { 118 in6_addreq.ifra_lifetime.ia6t_expire = t + newval; 119 in6_addreq.ifra_lifetime.ia6t_vltime = newval; 120 } else if (strcmp(cmd, "pltime") == 0) { 121 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval; 122 in6_addreq.ifra_lifetime.ia6t_pltime = newval; 123 } 124} 125 126static void 127setip6pltime(const char *seconds, int dummy __unused, int s, 128 const struct afswtch *afp) 129{ 130 setip6lifetime("pltime", seconds, s, afp); 131} 132 133static void 134setip6vltime(const char *seconds, int dummy __unused, int s, 135 const struct afswtch *afp) 136{ 137 setip6lifetime("vltime", seconds, s, afp); 138} 139 140static void 141setip6eui64(const char *cmd, int dummy __unused, int s, 142 const struct afswtch *afp) 143{ 144 struct ifaddrs *ifap, *ifa; 145 const struct sockaddr_in6 *sin6 = NULL; 146 const struct in6_addr *lladdr = NULL; 147 struct in6_addr *in6; 148 149 if (afp->af_af != AF_INET6) 150 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd); 151 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr; 152 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) 153 errx(EXIT_FAILURE, "interface index is already filled"); 154 if (getifaddrs(&ifap) != 0) 155 err(EXIT_FAILURE, "getifaddrs"); 156 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 157 if (ifa->ifa_addr->sa_family == AF_INET6 && 158 strcmp(ifa->ifa_name, name) == 0) { 159 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr; 160 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 161 lladdr = &sin6->sin6_addr; 162 break; 163 } 164 } 165 } 166 if (!lladdr) 167 errx(EXIT_FAILURE, "could not determine link local address"); 168 169 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8); 170 171 freeifaddrs(ifap); 172} 173 174static void 175in6_fillscopeid(struct sockaddr_in6 *sin6) 176{ 177#if defined(__KAME__) && defined(KAME_SCOPEID) 178 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 179 sin6->sin6_scope_id = 180 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]); 181 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0; 182 } 183#endif 184} 185 186static void 187in6_status(int s __unused, const struct rt_addrinfo * info) 188{ 189 struct sockaddr_in6 *sin, null_sin; 190 struct in6_ifreq ifr6; 191 int s6; 192 u_int32_t flags6; 193 struct in6_addrlifetime lifetime; 194 time_t t = time(NULL); 195 int error; 196 u_int32_t scopeid; 197 198 memset(&null_sin, 0, sizeof(null_sin)); 199 200 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA]; 201 if (sin == NULL) 202 return; 203 204 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name)); 205 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 206 warn("socket(AF_INET6,SOCK_DGRAM)"); 207 return; 208 } 209 ifr6.ifr_addr = *sin; 210 if (ioctl(s6, SIOCGIFAFLAG_IN6, &ifr6) < 0) { 211 warn("ioctl(SIOCGIFAFLAG_IN6)"); 212 close(s6); 213 return; 214 } 215 flags6 = ifr6.ifr_ifru.ifru_flags6; 216 memset(&lifetime, 0, sizeof(lifetime)); 217 ifr6.ifr_addr = *sin; 218 if (ioctl(s6, SIOCGIFALIFETIME_IN6, &ifr6) < 0) { 219 warn("ioctl(SIOCGIFALIFETIME_IN6)"); 220 close(s6); 221 return; 222 } 223 lifetime = ifr6.ifr_ifru.ifru_lifetime; 224 close(s6); 225 226 /* XXX: embedded link local addr check */ 227 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) && 228 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) { 229 u_short index; 230 231 index = *(u_short *)&sin->sin6_addr.s6_addr[2]; 232 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0; 233 if (sin->sin6_scope_id == 0) 234 sin->sin6_scope_id = ntohs(index); 235 } 236 scopeid = sin->sin6_scope_id; 237 238 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
| 60static struct in6_ifreq in6_ridreq; 61static struct in6_aliasreq in6_addreq = 62 { { 0 }, 63 { 0 }, 64 { 0 }, 65 { 0 }, 66 0, 67 { 0, 0, ND6_INFINITE_LIFETIME, ND6_INFINITE_LIFETIME } }; 68static int ip6lifetime; 69 70static void in6_fillscopeid(struct sockaddr_in6 *sin6); 71static int prefix(void *, int); 72static char *sec2str(time_t); 73static int explicit_prefix = 0; 74 75static char addr_buf[MAXHOSTNAMELEN *2 + 1]; /*for getnameinfo()*/ 76 77static void 78setifprefixlen(const char *addr, int dummy __unused, int s, 79 const struct afswtch *afp) 80{ 81 if (afp->af_getprefix != NULL) 82 afp->af_getprefix(addr, MASK); 83 explicit_prefix = 1; 84} 85 86static void 87setip6flags(const char *dummyaddr __unused, int flag, int dummysoc __unused, 88 const struct afswtch *afp) 89{ 90 if (afp->af_af != AF_INET6) 91 err(1, "address flags can be set only for inet6 addresses"); 92 93 if (flag < 0) 94 in6_addreq.ifra_flags &= ~(-flag); 95 else 96 in6_addreq.ifra_flags |= flag; 97} 98 99static void 100setip6lifetime(const char *cmd, const char *val, int s, 101 const struct afswtch *afp) 102{ 103 time_t newval, t; 104 char *ep; 105 106 t = time(NULL); 107 newval = (time_t)strtoul(val, &ep, 0); 108 if (val == ep) 109 errx(1, "invalid %s", cmd); 110 if (afp->af_af != AF_INET6) 111 errx(1, "%s not allowed for the AF", cmd); 112 if (strcmp(cmd, "vltime") == 0) { 113 in6_addreq.ifra_lifetime.ia6t_expire = t + newval; 114 in6_addreq.ifra_lifetime.ia6t_vltime = newval; 115 } else if (strcmp(cmd, "pltime") == 0) { 116 in6_addreq.ifra_lifetime.ia6t_preferred = t + newval; 117 in6_addreq.ifra_lifetime.ia6t_pltime = newval; 118 } 119} 120 121static void 122setip6pltime(const char *seconds, int dummy __unused, int s, 123 const struct afswtch *afp) 124{ 125 setip6lifetime("pltime", seconds, s, afp); 126} 127 128static void 129setip6vltime(const char *seconds, int dummy __unused, int s, 130 const struct afswtch *afp) 131{ 132 setip6lifetime("vltime", seconds, s, afp); 133} 134 135static void 136setip6eui64(const char *cmd, int dummy __unused, int s, 137 const struct afswtch *afp) 138{ 139 struct ifaddrs *ifap, *ifa; 140 const struct sockaddr_in6 *sin6 = NULL; 141 const struct in6_addr *lladdr = NULL; 142 struct in6_addr *in6; 143 144 if (afp->af_af != AF_INET6) 145 errx(EXIT_FAILURE, "%s not allowed for the AF", cmd); 146 in6 = (struct in6_addr *)&in6_addreq.ifra_addr.sin6_addr; 147 if (memcmp(&in6addr_any.s6_addr[8], &in6->s6_addr[8], 8) != 0) 148 errx(EXIT_FAILURE, "interface index is already filled"); 149 if (getifaddrs(&ifap) != 0) 150 err(EXIT_FAILURE, "getifaddrs"); 151 for (ifa = ifap; ifa; ifa = ifa->ifa_next) { 152 if (ifa->ifa_addr->sa_family == AF_INET6 && 153 strcmp(ifa->ifa_name, name) == 0) { 154 sin6 = (const struct sockaddr_in6 *)ifa->ifa_addr; 155 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 156 lladdr = &sin6->sin6_addr; 157 break; 158 } 159 } 160 } 161 if (!lladdr) 162 errx(EXIT_FAILURE, "could not determine link local address"); 163 164 memcpy(&in6->s6_addr[8], &lladdr->s6_addr[8], 8); 165 166 freeifaddrs(ifap); 167} 168 169static void 170in6_fillscopeid(struct sockaddr_in6 *sin6) 171{ 172#if defined(__KAME__) && defined(KAME_SCOPEID) 173 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { 174 sin6->sin6_scope_id = 175 ntohs(*(u_int16_t *)&sin6->sin6_addr.s6_addr[2]); 176 sin6->sin6_addr.s6_addr[2] = sin6->sin6_addr.s6_addr[3] = 0; 177 } 178#endif 179} 180 181static void 182in6_status(int s __unused, const struct rt_addrinfo * info) 183{ 184 struct sockaddr_in6 *sin, null_sin; 185 struct in6_ifreq ifr6; 186 int s6; 187 u_int32_t flags6; 188 struct in6_addrlifetime lifetime; 189 time_t t = time(NULL); 190 int error; 191 u_int32_t scopeid; 192 193 memset(&null_sin, 0, sizeof(null_sin)); 194 195 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_IFA]; 196 if (sin == NULL) 197 return; 198 199 strncpy(ifr6.ifr_name, ifr.ifr_name, sizeof(ifr.ifr_name)); 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 233 error = getnameinfo((struct sockaddr *)sin, sin->sin6_len, addr_buf,
|
239 sizeof(addr_buf), NULL, 0, 240 NI_NUMERICHOST|NI_WITHSCOPEID);
| 234 sizeof(addr_buf), NULL, 0, NI_NUMERICHOST);
|
241 if (error != 0) 242 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, 243 sizeof(addr_buf)); 244 printf("\tinet6 %s ", addr_buf); 245 246 if (flags & IFF_POINTOPOINT) { 247 /* note RTAX_BRD overlap with IFF_BROADCAST */ 248 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD]; 249 /* 250 * some of the interfaces do not have valid destination 251 * address. 252 */ 253 if (sin && sin->sin6_family == AF_INET6) { 254 int error; 255 256 /* XXX: embedded link local addr check */ 257 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) && 258 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) { 259 u_short index; 260 261 index = *(u_short *)&sin->sin6_addr.s6_addr[2]; 262 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0; 263 if (sin->sin6_scope_id == 0) 264 sin->sin6_scope_id = ntohs(index); 265 } 266 267 error = getnameinfo((struct sockaddr *)sin, 268 sin->sin6_len, addr_buf, 269 sizeof(addr_buf), NULL, 0,
| 235 if (error != 0) 236 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, 237 sizeof(addr_buf)); 238 printf("\tinet6 %s ", addr_buf); 239 240 if (flags & IFF_POINTOPOINT) { 241 /* note RTAX_BRD overlap with IFF_BROADCAST */ 242 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_BRD]; 243 /* 244 * some of the interfaces do not have valid destination 245 * address. 246 */ 247 if (sin && sin->sin6_family == AF_INET6) { 248 int error; 249 250 /* XXX: embedded link local addr check */ 251 if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr) && 252 *(u_short *)&sin->sin6_addr.s6_addr[2] != 0) { 253 u_short index; 254 255 index = *(u_short *)&sin->sin6_addr.s6_addr[2]; 256 *(u_short *)&sin->sin6_addr.s6_addr[2] = 0; 257 if (sin->sin6_scope_id == 0) 258 sin->sin6_scope_id = ntohs(index); 259 } 260 261 error = getnameinfo((struct sockaddr *)sin, 262 sin->sin6_len, addr_buf, 263 sizeof(addr_buf), NULL, 0,
|
270 NI_NUMERICHOST|NI_WITHSCOPEID);
| 264 NI_NUMERICHOST);
|
271 if (error != 0) 272 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, 273 sizeof(addr_buf)); 274 printf("--> %s ", addr_buf); 275 } 276 } 277 278 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK]; 279 if (!sin) 280 sin = &null_sin; 281 printf("prefixlen %d ", prefix(&sin->sin6_addr, 282 sizeof(struct in6_addr))); 283 284 if ((flags6 & IN6_IFF_ANYCAST) != 0) 285 printf("anycast "); 286 if ((flags6 & IN6_IFF_TENTATIVE) != 0) 287 printf("tentative "); 288 if ((flags6 & IN6_IFF_DUPLICATED) != 0) 289 printf("duplicated "); 290 if ((flags6 & IN6_IFF_DETACHED) != 0) 291 printf("detached "); 292 if ((flags6 & IN6_IFF_DEPRECATED) != 0) 293 printf("deprecated "); 294 if ((flags6 & IN6_IFF_AUTOCONF) != 0) 295 printf("autoconf "); 296 if ((flags6 & IN6_IFF_TEMPORARY) != 0) 297 printf("temporary "); 298 299 if (scopeid) 300 printf("scopeid 0x%x ", scopeid); 301 302 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) { 303 printf("pltime "); 304 if (lifetime.ia6t_preferred) { 305 printf("%s ", lifetime.ia6t_preferred < t 306 ? "0" : sec2str(lifetime.ia6t_preferred - t)); 307 } else 308 printf("infty "); 309 310 printf("vltime "); 311 if (lifetime.ia6t_expire) { 312 printf("%s ", lifetime.ia6t_expire < t 313 ? "0" : sec2str(lifetime.ia6t_expire - t)); 314 } else 315 printf("infty "); 316 } 317 318 putchar('\n'); 319} 320 321#define SIN6(x) ((struct sockaddr_in6 *) &(x)) 322static struct sockaddr_in6 *sin6tab[] = { 323 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr), 324 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr) 325}; 326 327static void 328in6_getprefix(const char *plen, int which) 329{ 330 struct sockaddr_in6 *sin = sin6tab[which]; 331 u_char *cp; 332 int len = atoi(plen); 333 334 if ((len < 0) || (len > 128)) 335 errx(1, "%s: bad value", plen); 336 sin->sin6_len = sizeof(*sin); 337 if (which != MASK) 338 sin->sin6_family = AF_INET6; 339 if ((len == 0) || (len == 128)) { 340 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr)); 341 return; 342 } 343 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr)); 344 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8) 345 *cp++ = 0xff; 346 *cp = 0xff << (8 - len); 347} 348 349static void 350in6_getaddr(const char *s, int which) 351{ 352 struct sockaddr_in6 *sin = sin6tab[which]; 353 struct addrinfo hints, *res; 354 int error = -1; 355 356 newaddr &= 1; 357 358 sin->sin6_len = sizeof(*sin); 359 if (which != MASK) 360 sin->sin6_family = AF_INET6; 361 362 if (which == ADDR) { 363 char *p = NULL; 364 if((p = strrchr(s, '/')) != NULL) { 365 *p = '\0'; 366 in6_getprefix(p + 1, MASK); 367 explicit_prefix = 1; 368 } 369 } 370 371 if (sin->sin6_family == AF_INET6) { 372 bzero(&hints, sizeof(struct addrinfo)); 373 hints.ai_family = AF_INET6; 374 error = getaddrinfo(s, NULL, &hints, &res); 375 } 376 if (error != 0) { 377 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1) 378 errx(1, "%s: bad value", s); 379 } else 380 bcopy(res->ai_addr, sin, res->ai_addrlen); 381} 382 383static int 384prefix(void *val, int size) 385{ 386 u_char *name = (u_char *)val; 387 int byte, bit, plen = 0; 388 389 for (byte = 0; byte < size; byte++, plen += 8) 390 if (name[byte] != 0xff) 391 break; 392 if (byte == size) 393 return (plen); 394 for (bit = 7; bit != 0; bit--, plen++) 395 if (!(name[byte] & (1 << bit))) 396 break; 397 for (; bit != 0; bit--) 398 if (name[byte] & (1 << bit)) 399 return(0); 400 byte++; 401 for (; byte < size; byte++) 402 if (name[byte]) 403 return(0); 404 return (plen); 405} 406 407static char * 408sec2str(time_t total) 409{ 410 static char result[256]; 411 int days, hours, mins, secs; 412 int first = 1; 413 char *p = result; 414 415 if (0) { 416 days = total / 3600 / 24; 417 hours = (total / 3600) % 24; 418 mins = (total / 60) % 60; 419 secs = total % 60; 420 421 if (days) { 422 first = 0; 423 p += sprintf(p, "%dd", days); 424 } 425 if (!first || hours) { 426 first = 0; 427 p += sprintf(p, "%dh", hours); 428 } 429 if (!first || mins) { 430 first = 0; 431 p += sprintf(p, "%dm", mins); 432 } 433 sprintf(p, "%ds", secs); 434 } else 435 sprintf(result, "%lu", (unsigned long)total); 436 437 return(result); 438} 439 440static void 441in6_postproc(int s, const struct afswtch *afp) 442{ 443 if (explicit_prefix == 0) { 444 /* Aggregatable address architecture defines all prefixes 445 are 64. So, it is convenient to set prefixlen to 64 if 446 it is not specified. */ 447 setifprefixlen("64", 0, s, afp); 448 /* in6_getprefix("64", MASK) if MASK is available here... */ 449 } 450} 451 452static void 453in6_status_tunnel(int s) 454{ 455 char src[NI_MAXHOST]; 456 char dst[NI_MAXHOST];
| 265 if (error != 0) 266 inet_ntop(AF_INET6, &sin->sin6_addr, addr_buf, 267 sizeof(addr_buf)); 268 printf("--> %s ", addr_buf); 269 } 270 } 271 272 sin = (struct sockaddr_in6 *)info->rti_info[RTAX_NETMASK]; 273 if (!sin) 274 sin = &null_sin; 275 printf("prefixlen %d ", prefix(&sin->sin6_addr, 276 sizeof(struct in6_addr))); 277 278 if ((flags6 & IN6_IFF_ANYCAST) != 0) 279 printf("anycast "); 280 if ((flags6 & IN6_IFF_TENTATIVE) != 0) 281 printf("tentative "); 282 if ((flags6 & IN6_IFF_DUPLICATED) != 0) 283 printf("duplicated "); 284 if ((flags6 & IN6_IFF_DETACHED) != 0) 285 printf("detached "); 286 if ((flags6 & IN6_IFF_DEPRECATED) != 0) 287 printf("deprecated "); 288 if ((flags6 & IN6_IFF_AUTOCONF) != 0) 289 printf("autoconf "); 290 if ((flags6 & IN6_IFF_TEMPORARY) != 0) 291 printf("temporary "); 292 293 if (scopeid) 294 printf("scopeid 0x%x ", scopeid); 295 296 if (ip6lifetime && (lifetime.ia6t_preferred || lifetime.ia6t_expire)) { 297 printf("pltime "); 298 if (lifetime.ia6t_preferred) { 299 printf("%s ", lifetime.ia6t_preferred < t 300 ? "0" : sec2str(lifetime.ia6t_preferred - t)); 301 } else 302 printf("infty "); 303 304 printf("vltime "); 305 if (lifetime.ia6t_expire) { 306 printf("%s ", lifetime.ia6t_expire < t 307 ? "0" : sec2str(lifetime.ia6t_expire - t)); 308 } else 309 printf("infty "); 310 } 311 312 putchar('\n'); 313} 314 315#define SIN6(x) ((struct sockaddr_in6 *) &(x)) 316static struct sockaddr_in6 *sin6tab[] = { 317 SIN6(in6_ridreq.ifr_addr), SIN6(in6_addreq.ifra_addr), 318 SIN6(in6_addreq.ifra_prefixmask), SIN6(in6_addreq.ifra_dstaddr) 319}; 320 321static void 322in6_getprefix(const char *plen, int which) 323{ 324 struct sockaddr_in6 *sin = sin6tab[which]; 325 u_char *cp; 326 int len = atoi(plen); 327 328 if ((len < 0) || (len > 128)) 329 errx(1, "%s: bad value", plen); 330 sin->sin6_len = sizeof(*sin); 331 if (which != MASK) 332 sin->sin6_family = AF_INET6; 333 if ((len == 0) || (len == 128)) { 334 memset(&sin->sin6_addr, 0xff, sizeof(struct in6_addr)); 335 return; 336 } 337 memset((void *)&sin->sin6_addr, 0x00, sizeof(sin->sin6_addr)); 338 for (cp = (u_char *)&sin->sin6_addr; len > 7; len -= 8) 339 *cp++ = 0xff; 340 *cp = 0xff << (8 - len); 341} 342 343static void 344in6_getaddr(const char *s, int which) 345{ 346 struct sockaddr_in6 *sin = sin6tab[which]; 347 struct addrinfo hints, *res; 348 int error = -1; 349 350 newaddr &= 1; 351 352 sin->sin6_len = sizeof(*sin); 353 if (which != MASK) 354 sin->sin6_family = AF_INET6; 355 356 if (which == ADDR) { 357 char *p = NULL; 358 if((p = strrchr(s, '/')) != NULL) { 359 *p = '\0'; 360 in6_getprefix(p + 1, MASK); 361 explicit_prefix = 1; 362 } 363 } 364 365 if (sin->sin6_family == AF_INET6) { 366 bzero(&hints, sizeof(struct addrinfo)); 367 hints.ai_family = AF_INET6; 368 error = getaddrinfo(s, NULL, &hints, &res); 369 } 370 if (error != 0) { 371 if (inet_pton(AF_INET6, s, &sin->sin6_addr) != 1) 372 errx(1, "%s: bad value", s); 373 } else 374 bcopy(res->ai_addr, sin, res->ai_addrlen); 375} 376 377static int 378prefix(void *val, int size) 379{ 380 u_char *name = (u_char *)val; 381 int byte, bit, plen = 0; 382 383 for (byte = 0; byte < size; byte++, plen += 8) 384 if (name[byte] != 0xff) 385 break; 386 if (byte == size) 387 return (plen); 388 for (bit = 7; bit != 0; bit--, plen++) 389 if (!(name[byte] & (1 << bit))) 390 break; 391 for (; bit != 0; bit--) 392 if (name[byte] & (1 << bit)) 393 return(0); 394 byte++; 395 for (; byte < size; byte++) 396 if (name[byte]) 397 return(0); 398 return (plen); 399} 400 401static char * 402sec2str(time_t total) 403{ 404 static char result[256]; 405 int days, hours, mins, secs; 406 int first = 1; 407 char *p = result; 408 409 if (0) { 410 days = total / 3600 / 24; 411 hours = (total / 3600) % 24; 412 mins = (total / 60) % 60; 413 secs = total % 60; 414 415 if (days) { 416 first = 0; 417 p += sprintf(p, "%dd", days); 418 } 419 if (!first || hours) { 420 first = 0; 421 p += sprintf(p, "%dh", hours); 422 } 423 if (!first || mins) { 424 first = 0; 425 p += sprintf(p, "%dm", mins); 426 } 427 sprintf(p, "%ds", secs); 428 } else 429 sprintf(result, "%lu", (unsigned long)total); 430 431 return(result); 432} 433 434static void 435in6_postproc(int s, const struct afswtch *afp) 436{ 437 if (explicit_prefix == 0) { 438 /* Aggregatable address architecture defines all prefixes 439 are 64. So, it is convenient to set prefixlen to 64 if 440 it is not specified. */ 441 setifprefixlen("64", 0, s, afp); 442 /* in6_getprefix("64", MASK) if MASK is available here... */ 443 } 444} 445 446static void 447in6_status_tunnel(int s) 448{ 449 char src[NI_MAXHOST]; 450 char dst[NI_MAXHOST];
|
457#ifdef NI_WITHSCOPEID 458 const int niflag = NI_NUMERICHOST | NI_WITHSCOPEID; 459#else 460 const int niflag = NI_NUMERICHOST; 461#endif
| |
462 struct in6_ifreq in6_ifr; 463 const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr; 464 465 memset(&in6_ifr, 0, sizeof(in6_ifr)); 466 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ); 467 468 if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0) 469 return; 470 if (sa->sa_family == AF_INET6) 471 in6_fillscopeid(&in6_ifr.ifr_addr);
| 451 struct in6_ifreq in6_ifr; 452 const struct sockaddr *sa = (const struct sockaddr *) &in6_ifr.ifr_addr; 453 454 memset(&in6_ifr, 0, sizeof(in6_ifr)); 455 strncpy(in6_ifr.ifr_name, name, IFNAMSIZ); 456 457 if (ioctl(s, SIOCGIFPSRCADDR_IN6, (caddr_t)&in6_ifr) < 0) 458 return; 459 if (sa->sa_family == AF_INET6) 460 in6_fillscopeid(&in6_ifr.ifr_addr);
|
472 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, niflag) != 0)
| 461 if (getnameinfo(sa, sa->sa_len, src, sizeof(src), 0, 0, 462 NI_NUMERICHOST) != 0)
|
473 src[0] = '\0'; 474 475 if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0) 476 return; 477 if (sa->sa_family == AF_INET6) 478 in6_fillscopeid(&in6_ifr.ifr_addr);
| 463 src[0] = '\0'; 464 465 if (ioctl(s, SIOCGIFPDSTADDR_IN6, (caddr_t)&in6_ifr) < 0) 466 return; 467 if (sa->sa_family == AF_INET6) 468 in6_fillscopeid(&in6_ifr.ifr_addr);
|
479 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, niflag) != 0)
| 469 if (getnameinfo(sa, sa->sa_len, dst, sizeof(dst), 0, 0, 470 NI_NUMERICHOST) != 0)
|
480 dst[0] = '\0'; 481 482 printf("\ttunnel inet6 %s --> %s\n", src, dst); 483} 484 485static void 486in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres) 487{ 488 struct in6_aliasreq in6_addreq; 489 490 memset(&in6_addreq, 0, sizeof(in6_addreq)); 491 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ); 492 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len); 493 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr, 494 dstres->ai_addr->sa_len); 495 496 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0) 497 warn("SIOCSIFPHYADDR_IN6"); 498} 499 500static struct cmd inet6_cmds[] = { 501 DEF_CMD_ARG("prefixlen", setifprefixlen), 502 DEF_CMD("anycast", IN6_IFF_ANYCAST, setip6flags), 503 DEF_CMD("tentative", IN6_IFF_TENTATIVE, setip6flags), 504 DEF_CMD("-tentative", -IN6_IFF_TENTATIVE, setip6flags), 505 DEF_CMD("deprecated", IN6_IFF_DEPRECATED, setip6flags), 506 DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED, setip6flags), 507 DEF_CMD("autoconf", IN6_IFF_AUTOCONF, setip6flags), 508 DEF_CMD("-autoconf", -IN6_IFF_AUTOCONF, setip6flags), 509 DEF_CMD_ARG("pltime", setip6pltime), 510 DEF_CMD_ARG("vltime", setip6vltime), 511 DEF_CMD("eui64", 0, setip6eui64), 512}; 513 514static struct afswtch af_inet6 = { 515 .af_name = "inet6", 516 .af_af = AF_INET6, 517 .af_status = in6_status, 518 .af_getaddr = in6_getaddr, 519 .af_getprefix = in6_getprefix, 520 .af_postproc = in6_postproc, 521 .af_status_tunnel = in6_status_tunnel, 522 .af_settunnel = in6_set_tunnel, 523 .af_difaddr = SIOCDIFADDR_IN6, 524 .af_aifaddr = SIOCAIFADDR_IN6, 525 .af_ridreq = &in6_addreq, 526 .af_addreq = &in6_addreq, 527}; 528 529static void 530in6_Lopt_cb(const char *optarg __unused) 531{ 532 ip6lifetime++; /* print IPv6 address lifetime */ 533} 534static struct option in6_Lopt = { "L", "[-L]", in6_Lopt_cb }; 535 536static __constructor void 537inet6_ctor(void) 538{ 539#define N(a) (sizeof(a) / sizeof(a[0])) 540 int i; 541 542 for (i = 0; i < N(inet6_cmds); i++) 543 cmd_register(&inet6_cmds[i]); 544 af_register(&af_inet6); 545 opt_register(&in6_Lopt); 546#undef N 547}
| 471 dst[0] = '\0'; 472 473 printf("\ttunnel inet6 %s --> %s\n", src, dst); 474} 475 476static void 477in6_set_tunnel(int s, struct addrinfo *srcres, struct addrinfo *dstres) 478{ 479 struct in6_aliasreq in6_addreq; 480 481 memset(&in6_addreq, 0, sizeof(in6_addreq)); 482 strncpy(in6_addreq.ifra_name, name, IFNAMSIZ); 483 memcpy(&in6_addreq.ifra_addr, srcres->ai_addr, srcres->ai_addr->sa_len); 484 memcpy(&in6_addreq.ifra_dstaddr, dstres->ai_addr, 485 dstres->ai_addr->sa_len); 486 487 if (ioctl(s, SIOCSIFPHYADDR_IN6, &in6_addreq) < 0) 488 warn("SIOCSIFPHYADDR_IN6"); 489} 490 491static struct cmd inet6_cmds[] = { 492 DEF_CMD_ARG("prefixlen", setifprefixlen), 493 DEF_CMD("anycast", IN6_IFF_ANYCAST, setip6flags), 494 DEF_CMD("tentative", IN6_IFF_TENTATIVE, setip6flags), 495 DEF_CMD("-tentative", -IN6_IFF_TENTATIVE, setip6flags), 496 DEF_CMD("deprecated", IN6_IFF_DEPRECATED, setip6flags), 497 DEF_CMD("-deprecated", -IN6_IFF_DEPRECATED, setip6flags), 498 DEF_CMD("autoconf", IN6_IFF_AUTOCONF, setip6flags), 499 DEF_CMD("-autoconf", -IN6_IFF_AUTOCONF, setip6flags), 500 DEF_CMD_ARG("pltime", setip6pltime), 501 DEF_CMD_ARG("vltime", setip6vltime), 502 DEF_CMD("eui64", 0, setip6eui64), 503}; 504 505static struct afswtch af_inet6 = { 506 .af_name = "inet6", 507 .af_af = AF_INET6, 508 .af_status = in6_status, 509 .af_getaddr = in6_getaddr, 510 .af_getprefix = in6_getprefix, 511 .af_postproc = in6_postproc, 512 .af_status_tunnel = in6_status_tunnel, 513 .af_settunnel = in6_set_tunnel, 514 .af_difaddr = SIOCDIFADDR_IN6, 515 .af_aifaddr = SIOCAIFADDR_IN6, 516 .af_ridreq = &in6_addreq, 517 .af_addreq = &in6_addreq, 518}; 519 520static void 521in6_Lopt_cb(const char *optarg __unused) 522{ 523 ip6lifetime++; /* print IPv6 address lifetime */ 524} 525static struct option in6_Lopt = { "L", "[-L]", in6_Lopt_cb }; 526 527static __constructor void 528inet6_ctor(void) 529{ 530#define N(a) (sizeof(a) / sizeof(a[0])) 531 int i; 532 533 for (i = 0; i < N(inet6_cmds); i++) 534 cmd_register(&inet6_cmds[i]); 535 af_register(&af_inet6); 536 opt_register(&in6_Lopt); 537#undef N 538}
|