1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009 Hiroki Sato. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #ifndef lint 29 static const char rcsid[] = 30 "$FreeBSD$"; 31 #endif /* not lint */ 32 33 #include <sys/param.h> 34 #include <sys/ioctl.h> 35 #include <sys/socket.h> 36 #include <sys/sysctl.h> 37 #include <net/if.h> 38 #include <net/route.h> 39 40 #include <err.h> 41 #include <errno.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 <netinet/in_var.h> 52 #include <arpa/inet.h> 53 #include <netdb.h> 54 55 #include <netinet6/nd6.h> 56 57 #include "ifconfig.h" 58 59 #define MAX_SYSCTL_TRY 5 60 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG 61 #define ND6BITS "\020\001PERFORMNUD\002ACCEPT_RTADV\003PREFER_SOURCE" \ 62 "\004IFDISABLED\005DONT_SET_IFROUTE\006AUTO_LINKLOCAL" \ 63 "\007NO_RADR\010NO_PREFER_IFACE\011NO_DAD" \ 64 "\012IPV6_ONLY\013IPV6_ONLY_MANUAL" \ 65 "\020DEFAULTIF" 66 #else 67 #define ND6BITS "\020\001PERFORMNUD\002ACCEPT_RTADV\003PREFER_SOURCE" \ 68 "\004IFDISABLED\005DONT_SET_IFROUTE\006AUTO_LINKLOCAL" \ 69 "\007NO_RADR\010NO_PREFER_IFACE\011NO_DAD\020DEFAULTIF" 70 #endif 71 72 static int isnd6defif(int); 73 void setnd6flags(if_ctx *, const char *, int); 74 void setnd6defif(if_ctx *,const char *, int); 75 void nd6_status(if_ctx *); 76 77 void 78 setnd6flags(if_ctx *ctx, const char *dummyaddr __unused, int d) 79 { 80 struct in6_ndireq nd; 81 int error; 82 83 memset(&nd, 0, sizeof(nd)); 84 strlcpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname)); 85 error = ioctl_ctx(ctx, SIOCGIFINFO_IN6, &nd); 86 if (error) { 87 warn("ioctl(SIOCGIFINFO_IN6)"); 88 return; 89 } 90 if (d < 0) 91 nd.ndi.flags &= ~(-d); 92 else 93 nd.ndi.flags |= d; 94 error = ioctl_ctx(ctx, SIOCSIFINFO_IN6, (caddr_t)&nd); 95 if (error) 96 warn("ioctl(SIOCSIFINFO_IN6)"); 97 } 98 99 void 100 setnd6defif(if_ctx *ctx, const char *dummyaddr __unused, int d) 101 { 102 struct in6_ndifreq ndifreq; 103 int ifindex; 104 int error; 105 106 memset(&ndifreq, 0, sizeof(ndifreq)); 107 strlcpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname)); 108 109 if (d < 0) { 110 if (isnd6defif(ctx->io_s)) { 111 /* ifindex = 0 means to remove default if */ 112 ifindex = 0; 113 } else 114 return; 115 } else if ((ifindex = if_nametoindex(ndifreq.ifname)) == 0) { 116 warn("if_nametoindex(%s)", ndifreq.ifname); 117 return; 118 } 119 120 ndifreq.ifindex = ifindex; 121 error = ioctl_ctx(ctx, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq); 122 if (error) 123 warn("ioctl(SIOCSDEFIFACE_IN6)"); 124 } 125 126 static int 127 isnd6defif(int s) 128 { 129 struct in6_ndifreq ndifreq; 130 unsigned int ifindex; 131 int error; 132 133 memset(&ndifreq, 0, sizeof(ndifreq)); 134 strlcpy(ndifreq.ifname, ifr.ifr_name, sizeof(ndifreq.ifname)); 135 136 ifindex = if_nametoindex(ndifreq.ifname); 137 error = ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq); 138 if (error) { 139 warn("ioctl(SIOCGDEFIFACE_IN6)"); 140 return (error); 141 } 142 return (ndifreq.ifindex == ifindex); 143 } 144 145 void 146 nd6_status(if_ctx *ctx __unused) 147 { 148 struct in6_ndireq nd; 149 int s6; 150 int error; 151 int isdefif; 152 153 memset(&nd, 0, sizeof(nd)); 154 strlcpy(nd.ifname, ifr.ifr_name, sizeof(nd.ifname)); 155 if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 156 if (errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT) 157 warn("socket(AF_INET6, SOCK_DGRAM)"); 158 return; 159 } 160 error = ioctl(s6, SIOCGIFINFO_IN6, &nd); 161 if (error) { 162 if (errno != EPFNOSUPPORT) 163 warn("ioctl(SIOCGIFINFO_IN6)"); 164 close(s6); 165 return; 166 } 167 isdefif = isnd6defif(s6); 168 close(s6); 169 if (nd.ndi.flags == 0 && !isdefif) 170 return; 171 printb("\tnd6 options", 172 (unsigned int)(nd.ndi.flags | (isdefif << 15)), ND6BITS); 173 putchar('\n'); 174 } 175