1 /* 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * 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 * 3. Neither the name of the project 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 PROJECT 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 PROJECT 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$ 30 */ 31 32 #include <sys/param.h> 33 #include <sys/socket.h> 34 #include <sys/uio.h> 35 #include <sys/time.h> 36 37 #include <net/if.h> 38 #include <net/route.h> 39 #include <net/if_dl.h> 40 41 #include <netinet/in.h> 42 #include <netinet/ip6.h> 43 #include <netinet6/ip6_var.h> 44 #include <netinet/icmp6.h> 45 46 #include <arpa/inet.h> 47 48 #include <time.h> 49 #include <unistd.h> 50 #include <stdio.h> 51 #include <err.h> 52 #include <errno.h> 53 #include <string.h> 54 #include <stdlib.h> 55 #include <syslog.h> 56 #include "rtsold.h" 57 58 #define ALLROUTER "ff02::2" 59 60 static struct msghdr rcvmhdr; 61 static struct msghdr sndmhdr; 62 static struct iovec rcviov[2]; 63 static struct iovec sndiov[2]; 64 static struct sockaddr_in6 from; 65 66 static int rssock; 67 68 static struct sockaddr_in6 sin6_allrouters = {sizeof(sin6_allrouters), AF_INET6}; 69 70 int 71 sockopen() 72 { 73 int on; 74 struct icmp6_filter filt; 75 static u_char answer[1500]; 76 static u_char rcvcmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo)) + 77 CMSG_SPACE(sizeof(int))]; 78 static u_char sndcmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo)) + 79 CMSG_SPACE(sizeof(int))]; 80 81 memset(&sin6_allrouters, 0, sizeof(struct sockaddr_in6)); 82 if (inet_pton(AF_INET6, ALLROUTER, 83 &sin6_allrouters.sin6_addr.s6_addr) != 1) { 84 warnmsg(LOG_ERR, __FUNCTION__, "inet_pton failed for %s", 85 ALLROUTER); 86 return(-1); 87 } 88 89 if ((rssock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) { 90 warnmsg(LOG_ERR, __FUNCTION__, "socket: %s", strerror(errno)); 91 return(-1); 92 } 93 94 /* specify to tell receiving interface */ 95 on = 1; 96 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_PKTINFO, &on, 97 sizeof(on)) < 0) { 98 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_PKTINFO: %s", 99 strerror(errno)); 100 exit(1); 101 } 102 103 on = 1; 104 /* specify to tell value of hoplimit field of received IP6 hdr */ 105 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, 106 sizeof(on)) < 0) { 107 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_HOPLIMIT: %s", 108 strerror(errno)); 109 exit(1); 110 } 111 112 /* specfiy to accept only router advertisements on the socket */ 113 ICMP6_FILTER_SETBLOCKALL(&filt); 114 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt); 115 if (setsockopt(rssock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, 116 sizeof(filt)) == -1) { 117 warnmsg(LOG_ERR, __FUNCTION__, "setsockopt(ICMP6_FILTER): %s", 118 strerror(errno)); 119 return(-1); 120 } 121 122 /* initialize msghdr for receiving packets */ 123 rcviov[0].iov_base = (caddr_t)answer; 124 rcviov[0].iov_len = sizeof(answer); 125 rcvmhdr.msg_name = (caddr_t)&from; 126 rcvmhdr.msg_namelen = sizeof(from); 127 rcvmhdr.msg_iov = rcviov; 128 rcvmhdr.msg_iovlen = 1; 129 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf; 130 rcvmhdr.msg_controllen = sizeof(rcvcmsgbuf); 131 132 /* initialize msghdr for sending packets */ 133 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6); 134 sndmhdr.msg_iov = sndiov; 135 sndmhdr.msg_iovlen = 1; 136 sndmhdr.msg_control = (caddr_t)sndcmsgbuf; 137 sndmhdr.msg_controllen = sizeof(sndcmsgbuf); 138 139 return(rssock); 140 } 141 142 void 143 sendpacket(struct ifinfo *ifinfo) 144 { 145 int i; 146 struct cmsghdr *cm; 147 struct in6_pktinfo *pi; 148 149 sndmhdr.msg_name = (caddr_t)&sin6_allrouters; 150 sndmhdr.msg_iov[0].iov_base = (caddr_t)ifinfo->rs_data; 151 sndmhdr.msg_iov[0].iov_len = ifinfo->rs_datalen; 152 153 cm = CMSG_FIRSTHDR(&sndmhdr); 154 /* specify the outgoing interface */ 155 cm->cmsg_level = IPPROTO_IPV6; 156 cm->cmsg_type = IPV6_PKTINFO; 157 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 158 pi = (struct in6_pktinfo *)CMSG_DATA(cm); 159 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/ 160 pi->ipi6_ifindex = ifinfo->sdl->sdl_index; 161 162 /* specify the hop limit of the packet */ 163 { 164 int hoplimit = 255; 165 166 cm = CMSG_NXTHDR(&sndmhdr, cm); 167 cm->cmsg_level = IPPROTO_IPV6; 168 cm->cmsg_type = IPV6_HOPLIMIT; 169 cm->cmsg_len = CMSG_LEN(sizeof(int)); 170 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int)); 171 } 172 173 warnmsg(LOG_DEBUG, 174 __FUNCTION__, "send RS on %s, whose state is %d", 175 ifinfo->ifname, ifinfo->state); 176 177 i = sendmsg(rssock, &sndmhdr, 0); 178 179 if (i < 0 || i != ifinfo->rs_datalen) { 180 /* 181 * ENETDOWN is not so serious, especially when using several 182 * network cards on a mobile node. We ignore it. 183 */ 184 if (errno != ENETDOWN || dflag > 0) 185 warnmsg(LOG_ERR, __FUNCTION__, "sendmsg on %s: %s", 186 ifinfo->ifname, strerror(errno)); 187 } 188 189 /* update counter */ 190 ifinfo->probes++; 191 } 192 193 void 194 rtsol_input(int s) 195 { 196 int i; 197 int *hlimp = NULL; 198 struct icmp6_hdr *icp; 199 int ifindex = 0; 200 struct cmsghdr *cm; 201 struct in6_pktinfo *pi = NULL; 202 struct ifinfo *ifi = NULL; 203 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ]; 204 205 /* get message */ 206 if ((i = recvmsg(s, &rcvmhdr, 0)) < 0) { 207 warnmsg(LOG_ERR, __FUNCTION__, "recvmsg: %s", strerror(errno)); 208 return; 209 } 210 211 /* extract optional information via Advanced API */ 212 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr); 213 cm; 214 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) { 215 if (cm->cmsg_level == IPPROTO_IPV6 && 216 cm->cmsg_type == IPV6_PKTINFO && 217 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) { 218 pi = (struct in6_pktinfo *)(CMSG_DATA(cm)); 219 ifindex = pi->ipi6_ifindex; 220 } 221 if (cm->cmsg_level == IPPROTO_IPV6 && 222 cm->cmsg_type == IPV6_HOPLIMIT && 223 cm->cmsg_len == CMSG_LEN(sizeof(int))) 224 hlimp = (int *)CMSG_DATA(cm); 225 } 226 227 if (ifindex == 0) { 228 warnmsg(LOG_ERR, 229 __FUNCTION__, "failed to get receiving interface"); 230 return; 231 } 232 if (hlimp == NULL) { 233 warnmsg(LOG_ERR, 234 __FUNCTION__, "failed to get receiving hop limit"); 235 return; 236 } 237 238 if (i < sizeof(struct nd_router_advert)) { 239 warnmsg(LOG_ERR, 240 __FUNCTION__, "packet size(%d) is too short", i); 241 return; 242 } 243 244 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base; 245 246 if (icp->icmp6_type != ND_ROUTER_ADVERT) { 247 warnmsg(LOG_ERR, __FUNCTION__, 248 "invalid icmp type(%d) from %s on %s", icp->icmp6_type, 249 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 250 INET6_ADDRSTRLEN), 251 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 252 return; 253 } 254 255 if (icp->icmp6_code != 0) { 256 warnmsg(LOG_ERR, __FUNCTION__, 257 "invalid icmp code(%d) from %s on %s", icp->icmp6_code, 258 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 259 INET6_ADDRSTRLEN), 260 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 261 return; 262 } 263 264 if (*hlimp != 255) { 265 warnmsg(LOG_NOTICE, __FUNCTION__, 266 "invalid RA with hop limit(%d) from %s on %s", 267 *hlimp, 268 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 269 INET6_ADDRSTRLEN), 270 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 271 return; 272 } 273 274 if (pi && !IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr)) { 275 warnmsg(LOG_NOTICE, __FUNCTION__, 276 "invalid RA with non link-local source from %s on %s", 277 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 278 INET6_ADDRSTRLEN), 279 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 280 return; 281 } 282 283 /* xxx: more validation? */ 284 285 if ((ifi = find_ifinfo(pi->ipi6_ifindex)) == NULL) { 286 warnmsg(LOG_NOTICE, __FUNCTION__, 287 "received RA from %s on an unexpeced IF(%s)", 288 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 289 INET6_ADDRSTRLEN), 290 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 291 return; 292 } 293 294 warnmsg(LOG_DEBUG, __FUNCTION__, 295 "received RA from %s on %s, state is %d", 296 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 297 INET6_ADDRSTRLEN), 298 ifi->ifname, ifi->state); 299 300 ifi->racnt++; 301 302 switch(ifi->state) { 303 case IFS_IDLE: /* should be ignored */ 304 case IFS_DELAY: /* right? */ 305 break; 306 case IFS_PROBE: 307 ifi->state = IFS_IDLE; 308 ifi->probes = 0; 309 rtsol_timer_update(ifi); 310 break; 311 } 312 } 313