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