1 /* $KAME: rtsol.c,v 1.12 2001/11/12 11:47:11 jinmei Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD: src/usr.sbin/rtsold/rtsol.c,v 1.1.2.4 2002/04/24 10:22:30 suz Exp $ 32 * $DragonFly: src/usr.sbin/rtsold/rtsol.c,v 1.7 2008/11/02 21:52:46 swildner Exp $ 33 */ 34 35 #include <sys/param.h> 36 #include <sys/socket.h> 37 #include <sys/uio.h> 38 #include <sys/time.h> 39 #include <sys/queue.h> 40 41 #include <net/if.h> 42 #include <net/route.h> 43 #include <net/if_dl.h> 44 45 #include <netinet/in.h> 46 #include <netinet/ip6.h> 47 #include <netinet6/ip6_var.h> 48 #include <netinet/icmp6.h> 49 50 #include <arpa/inet.h> 51 52 #include <time.h> 53 #include <unistd.h> 54 #include <stdio.h> 55 #include <err.h> 56 #include <errno.h> 57 #include <string.h> 58 #include <stdlib.h> 59 #include <syslog.h> 60 #include "rtsold.h" 61 62 #define ALLROUTER "ff02::2" 63 64 static struct msghdr rcvmhdr; 65 static struct msghdr sndmhdr; 66 static struct iovec rcviov[2]; 67 static struct iovec sndiov[2]; 68 static struct sockaddr_in6 from; 69 70 int rssock; 71 72 static struct sockaddr_in6 sin6_allrouters = { 73 sizeof(sin6_allrouters), 74 AF_INET6, 75 0, 76 0, 77 IN6ADDR_ANY_INIT, 78 0 79 }; 80 81 int 82 sockopen(void) 83 { 84 int on; 85 struct icmp6_filter filt; 86 static u_char answer[1500]; 87 int rcvcmsglen, sndcmsglen; 88 static u_char *rcvcmsgbuf = NULL, *sndcmsgbuf = NULL; 89 90 sndcmsglen = rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) + 91 CMSG_SPACE(sizeof(int)); 92 if (rcvcmsgbuf == NULL && (rcvcmsgbuf = malloc(rcvcmsglen)) == NULL) { 93 warnmsg(LOG_ERR, __func__, 94 "malloc for receive msghdr failed"); 95 return(-1); 96 } 97 if (sndcmsgbuf == NULL && (sndcmsgbuf = malloc(sndcmsglen)) == NULL) { 98 warnmsg(LOG_ERR, __func__, 99 "malloc for send msghdr failed"); 100 return(-1); 101 } 102 memset(&sin6_allrouters, 0, sizeof(struct sockaddr_in6)); 103 sin6_allrouters.sin6_family = AF_INET6; 104 sin6_allrouters.sin6_len = sizeof(sin6_allrouters); 105 if (inet_pton(AF_INET6, ALLROUTER, 106 &sin6_allrouters.sin6_addr.s6_addr) != 1) { 107 warnmsg(LOG_ERR, __func__, "inet_pton failed for %s", 108 ALLROUTER); 109 return(-1); 110 } 111 112 if ((rssock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) { 113 warnmsg(LOG_ERR, __func__, "socket: %s", strerror(errno)); 114 return(-1); 115 } 116 117 /* specify to tell receiving interface */ 118 on = 1; 119 #ifdef IPV6_RECVPKTINFO 120 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, 121 sizeof(on)) < 0) { 122 warnmsg(LOG_ERR, __func__, "IPV6_RECVPKTINFO: %s", 123 strerror(errno)); 124 exit(1); 125 } 126 #else /* old adv. API */ 127 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_PKTINFO, &on, 128 sizeof(on)) < 0) { 129 warnmsg(LOG_ERR, __func__, "IPV6_PKTINFO: %s", 130 strerror(errno)); 131 exit(1); 132 } 133 #endif 134 135 on = 1; 136 /* specify to tell value of hoplimit field of received IP6 hdr */ 137 #ifdef IPV6_RECVHOPLIMIT 138 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, 139 sizeof(on)) < 0) { 140 warnmsg(LOG_ERR, __func__, "IPV6_RECVHOPLIMIT: %s", 141 strerror(errno)); 142 exit(1); 143 } 144 #else /* old adv. API */ 145 if (setsockopt(rssock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, 146 sizeof(on)) < 0) { 147 warnmsg(LOG_ERR, __func__, "IPV6_HOPLIMIT: %s", 148 strerror(errno)); 149 exit(1); 150 } 151 #endif 152 153 /* specfiy to accept only router advertisements on the socket */ 154 ICMP6_FILTER_SETBLOCKALL(&filt); 155 ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt); 156 if (setsockopt(rssock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, 157 sizeof(filt)) == -1) { 158 warnmsg(LOG_ERR, __func__, "setsockopt(ICMP6_FILTER): %s", 159 strerror(errno)); 160 return(-1); 161 } 162 163 /* initialize msghdr for receiving packets */ 164 rcviov[0].iov_base = (caddr_t)answer; 165 rcviov[0].iov_len = sizeof(answer); 166 rcvmhdr.msg_name = (caddr_t)&from; 167 rcvmhdr.msg_namelen = sizeof(from); 168 rcvmhdr.msg_iov = rcviov; 169 rcvmhdr.msg_iovlen = 1; 170 rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf; 171 rcvmhdr.msg_controllen = rcvcmsglen; 172 173 /* initialize msghdr for sending packets */ 174 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6); 175 sndmhdr.msg_iov = sndiov; 176 sndmhdr.msg_iovlen = 1; 177 sndmhdr.msg_control = (caddr_t)sndcmsgbuf; 178 sndmhdr.msg_controllen = sndcmsglen; 179 180 return(rssock); 181 } 182 183 void 184 sendpacket(struct ifinfo *ifinfo) 185 { 186 ssize_t i; 187 struct cmsghdr *cm; 188 struct in6_pktinfo *pi; 189 190 sndmhdr.msg_name = (caddr_t)&sin6_allrouters; 191 sndmhdr.msg_iov[0].iov_base = (caddr_t)ifinfo->rs_data; 192 sndmhdr.msg_iov[0].iov_len = ifinfo->rs_datalen; 193 194 cm = CMSG_FIRSTHDR(&sndmhdr); 195 /* specify the outgoing interface */ 196 cm->cmsg_level = IPPROTO_IPV6; 197 cm->cmsg_type = IPV6_PKTINFO; 198 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); 199 pi = (struct in6_pktinfo *)CMSG_DATA(cm); 200 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/ 201 pi->ipi6_ifindex = ifinfo->sdl->sdl_index; 202 203 /* specify the hop limit of the packet */ 204 { 205 int hoplimit = 255; 206 207 cm = CMSG_NXTHDR(&sndmhdr, cm); 208 cm->cmsg_level = IPPROTO_IPV6; 209 cm->cmsg_type = IPV6_HOPLIMIT; 210 cm->cmsg_len = CMSG_LEN(sizeof(int)); 211 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int)); 212 } 213 214 warnmsg(LOG_DEBUG, 215 __func__, "send RS on %s, whose state is %d", 216 ifinfo->ifname, ifinfo->state); 217 218 i = sendmsg(rssock, &sndmhdr, 0); 219 220 if (i < 0 || (size_t)i != ifinfo->rs_datalen) { 221 /* 222 * ENETDOWN is not so serious, especially when using several 223 * network cards on a mobile node. We ignore it. 224 */ 225 if (errno != ENETDOWN || dflag > 0) 226 warnmsg(LOG_ERR, __func__, "sendmsg on %s: %s", 227 ifinfo->ifname, strerror(errno)); 228 } 229 230 /* update counter */ 231 ifinfo->probes++; 232 } 233 234 void 235 rtsol_input(int s) 236 { 237 ssize_t i; 238 int *hlimp = NULL; 239 struct icmp6_hdr *icp; 240 int ifindex = 0; 241 struct cmsghdr *cm; 242 struct in6_pktinfo *pi = NULL; 243 struct ifinfo *ifi = NULL; 244 u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ]; 245 246 /* get message */ 247 if ((i = recvmsg(s, &rcvmhdr, 0)) < 0) { 248 warnmsg(LOG_ERR, __func__, "recvmsg: %s", strerror(errno)); 249 return; 250 } 251 252 /* extract optional information via Advanced API */ 253 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr); 254 cm; 255 cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) { 256 if (cm->cmsg_level == IPPROTO_IPV6 && 257 cm->cmsg_type == IPV6_PKTINFO && 258 cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) { 259 pi = (struct in6_pktinfo *)(CMSG_DATA(cm)); 260 ifindex = pi->ipi6_ifindex; 261 } 262 if (cm->cmsg_level == IPPROTO_IPV6 && 263 cm->cmsg_type == IPV6_HOPLIMIT && 264 cm->cmsg_len == CMSG_LEN(sizeof(int))) 265 hlimp = (int *)CMSG_DATA(cm); 266 } 267 268 if (ifindex == 0) { 269 warnmsg(LOG_ERR, 270 __func__, "failed to get receiving interface"); 271 return; 272 } 273 if (hlimp == NULL) { 274 warnmsg(LOG_ERR, 275 __func__, "failed to get receiving hop limit"); 276 return; 277 } 278 279 if ((size_t)i < sizeof(struct nd_router_advert)) { 280 warnmsg(LOG_ERR, 281 __func__, "packet size(%zd) is too short", i); 282 return; 283 } 284 285 icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base; 286 287 if (icp->icmp6_type != ND_ROUTER_ADVERT) { 288 warnmsg(LOG_ERR, __func__, 289 "invalid icmp type(%d) from %s on %s", icp->icmp6_type, 290 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 291 INET6_ADDRSTRLEN), 292 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 293 return; 294 } 295 296 if (icp->icmp6_code != 0) { 297 warnmsg(LOG_ERR, __func__, 298 "invalid icmp code(%d) from %s on %s", icp->icmp6_code, 299 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 300 INET6_ADDRSTRLEN), 301 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 302 return; 303 } 304 305 if (*hlimp != 255) { 306 warnmsg(LOG_NOTICE, __func__, 307 "invalid RA with hop limit(%d) from %s on %s", 308 *hlimp, 309 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 310 INET6_ADDRSTRLEN), 311 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 312 return; 313 } 314 315 if (pi && !IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr)) { 316 warnmsg(LOG_NOTICE, __func__, 317 "invalid RA with non link-local source from %s on %s", 318 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 319 INET6_ADDRSTRLEN), 320 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 321 return; 322 } 323 324 /* xxx: more validation? */ 325 326 if ((ifi = find_ifinfo(pi->ipi6_ifindex)) == NULL) { 327 warnmsg(LOG_NOTICE, __func__, 328 "received RA from %s on an unexpected IF(%s)", 329 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 330 INET6_ADDRSTRLEN), 331 if_indextoname(pi->ipi6_ifindex, ifnamebuf)); 332 return; 333 } 334 335 warnmsg(LOG_DEBUG, __func__, 336 "received RA from %s on %s, state is %d", 337 inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf, 338 INET6_ADDRSTRLEN), 339 ifi->ifname, ifi->state); 340 341 ifi->racnt++; 342 343 switch(ifi->state) { 344 case IFS_IDLE: /* should be ignored */ 345 case IFS_DELAY: /* right? */ 346 break; 347 case IFS_PROBE: 348 ifi->state = IFS_IDLE; 349 ifi->probes = 0; 350 rtsol_timer_update(ifi); 351 break; 352 } 353 } 354