Raise WARNS to 6:
[dragonfly.git] / usr.sbin / rtsold / rtsol.c
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.2 2003/06/17 04:30:03 dillon 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 = {sizeof(sin6_allrouters), AF_INET6};
73
74 int
75 sockopen()
76 {
77         int on;
78         struct icmp6_filter filt;
79         static u_char answer[1500];
80         int rcvcmsglen, sndcmsglen;
81         static u_char *rcvcmsgbuf = NULL, *sndcmsgbuf = NULL;
82
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, __FUNCTION__,
87                         "malloc for receive msghdr failed");
88                 return(-1);
89         }
90         if (sndcmsgbuf == NULL && (sndcmsgbuf = malloc(sndcmsglen)) == NULL) { 
91                 warnmsg(LOG_ERR, __FUNCTION__,
92                         "malloc for send msghdr failed");
93                 return(-1);
94         }
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, __FUNCTION__, "inet_pton failed for %s",
101                        ALLROUTER);
102                 return(-1);
103         }
104
105         if ((rssock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
106                 warnmsg(LOG_ERR, __FUNCTION__, "socket: %s", strerror(errno));
107                 return(-1);
108         }
109
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, __FUNCTION__, "IPV6_RECVPKTINFO: %s",
116                        strerror(errno));
117                 exit(1);
118         }
119 #else  /* old adv. API */
120         if (setsockopt(rssock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
121                        sizeof(on)) < 0) {
122                 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_PKTINFO: %s",
123                        strerror(errno));
124                 exit(1);
125         }
126 #endif 
127
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, __FUNCTION__, "IPV6_RECVHOPLIMIT: %s",
134                        strerror(errno));
135                 exit(1);
136         }
137 #else  /* old adv. API */
138         if (setsockopt(rssock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
139                        sizeof(on)) < 0) {
140                 warnmsg(LOG_ERR, __FUNCTION__, "IPV6_HOPLIMIT: %s",
141                        strerror(errno));
142                 exit(1);
143         }
144 #endif 
145
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, __FUNCTION__, "setsockopt(ICMP6_FILTER): %s",
152                        strerror(errno));
153                 return(-1);
154         }
155
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;
165
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;
172
173         return(rssock);
174 }
175
176 void
177 sendpacket(struct ifinfo *ifinfo)
178 {
179         int i;
180         struct cmsghdr *cm;
181         struct in6_pktinfo *pi;
182
183         sndmhdr.msg_name = (caddr_t)&sin6_allrouters;
184         sndmhdr.msg_iov[0].iov_base = (caddr_t)ifinfo->rs_data;
185         sndmhdr.msg_iov[0].iov_len = ifinfo->rs_datalen;
186
187         cm = CMSG_FIRSTHDR(&sndmhdr);
188         /* specify the outgoing interface */
189         cm->cmsg_level = IPPROTO_IPV6;
190         cm->cmsg_type = IPV6_PKTINFO;
191         cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
192         pi = (struct in6_pktinfo *)CMSG_DATA(cm);
193         memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));       /*XXX*/
194         pi->ipi6_ifindex = ifinfo->sdl->sdl_index;
195
196         /* specify the hop limit of the packet */
197         {
198                 int hoplimit = 255;
199
200                 cm = CMSG_NXTHDR(&sndmhdr, cm);
201                 cm->cmsg_level = IPPROTO_IPV6;
202                 cm->cmsg_type = IPV6_HOPLIMIT;
203                 cm->cmsg_len = CMSG_LEN(sizeof(int));
204                 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
205         }
206
207         warnmsg(LOG_DEBUG,
208                __FUNCTION__, "send RS on %s, whose state is %d",
209                ifinfo->ifname, ifinfo->state);
210
211         i = sendmsg(rssock, &sndmhdr, 0);
212
213         if (i < 0 || i != ifinfo->rs_datalen) {
214                 /*
215                  * ENETDOWN is not so serious, especially when using several
216                  * network cards on a mobile node. We ignore it.
217                  */
218                 if (errno != ENETDOWN || dflag > 0)
219                         warnmsg(LOG_ERR, __FUNCTION__, "sendmsg on %s: %s",
220                                 ifinfo->ifname, strerror(errno));
221         }
222
223         /* update counter */
224         ifinfo->probes++;
225 }
226
227 void
228 rtsol_input(int s)
229 {
230         int i;
231         int *hlimp = NULL;
232         struct icmp6_hdr *icp;
233         int ifindex = 0;
234         struct cmsghdr *cm;
235         struct in6_pktinfo *pi = NULL;
236         struct ifinfo *ifi = NULL;
237         u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
238
239         /* get message */
240         if ((i = recvmsg(s, &rcvmhdr, 0)) < 0) {
241                 warnmsg(LOG_ERR, __FUNCTION__, "recvmsg: %s", strerror(errno));
242                 return;
243         }
244
245         /* extract optional information via Advanced API */
246         for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
247              cm;
248              cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
249                 if (cm->cmsg_level == IPPROTO_IPV6 &&
250                     cm->cmsg_type == IPV6_PKTINFO &&
251                     cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
252                         pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
253                         ifindex = pi->ipi6_ifindex;
254                 }
255                 if (cm->cmsg_level == IPPROTO_IPV6 &&
256                     cm->cmsg_type == IPV6_HOPLIMIT &&
257                     cm->cmsg_len == CMSG_LEN(sizeof(int)))
258                         hlimp = (int *)CMSG_DATA(cm);
259         }
260
261         if (ifindex == 0) {
262                 warnmsg(LOG_ERR,
263                        __FUNCTION__, "failed to get receiving interface");
264                 return;
265         }
266         if (hlimp == NULL) {
267                 warnmsg(LOG_ERR,
268                        __FUNCTION__, "failed to get receiving hop limit");
269                 return;
270         }
271
272         if (i < sizeof(struct nd_router_advert)) {
273                 warnmsg(LOG_ERR,
274                        __FUNCTION__, "packet size(%d) is too short", i);
275                 return;
276         }
277
278         icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
279
280         if (icp->icmp6_type != ND_ROUTER_ADVERT) {
281                 warnmsg(LOG_ERR, __FUNCTION__,
282                         "invalid icmp type(%d) from %s on %s", icp->icmp6_type,
283                        inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
284                                  INET6_ADDRSTRLEN),
285                        if_indextoname(pi->ipi6_ifindex, ifnamebuf));
286                 return;
287         }
288
289         if (icp->icmp6_code != 0) {
290                 warnmsg(LOG_ERR, __FUNCTION__,
291                         "invalid icmp code(%d) from %s on %s", icp->icmp6_code,
292                        inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
293                                  INET6_ADDRSTRLEN),
294                        if_indextoname(pi->ipi6_ifindex, ifnamebuf));
295                 return;
296         }
297
298         if (*hlimp != 255) {
299                 warnmsg(LOG_NOTICE, __FUNCTION__,
300                         "invalid RA with hop limit(%d) from %s on %s",
301                        *hlimp,
302                        inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
303                                  INET6_ADDRSTRLEN),
304                        if_indextoname(pi->ipi6_ifindex, ifnamebuf));
305                 return;
306         }
307
308         if (pi && !IN6_IS_ADDR_LINKLOCAL(&from.sin6_addr)) {
309                 warnmsg(LOG_NOTICE, __FUNCTION__,
310                         "invalid RA with non link-local source from %s on %s",
311                        inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
312                                  INET6_ADDRSTRLEN),
313                        if_indextoname(pi->ipi6_ifindex, ifnamebuf));
314                 return;
315         }
316
317         /* xxx: more validation? */
318
319         if ((ifi = find_ifinfo(pi->ipi6_ifindex)) == NULL) {
320                 warnmsg(LOG_NOTICE, __FUNCTION__,
321                         "received RA from %s on an unexpeced IF(%s)",
322                        inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
323                                  INET6_ADDRSTRLEN),
324                        if_indextoname(pi->ipi6_ifindex, ifnamebuf));
325                 return;
326         }
327
328         warnmsg(LOG_DEBUG, __FUNCTION__,
329                 "received RA from %s on %s, state is %d",
330                inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
331                          INET6_ADDRSTRLEN),
332                ifi->ifname, ifi->state);
333
334         ifi->racnt++;
335
336         switch(ifi->state) {
337          case IFS_IDLE:         /* should be ignored */
338          case IFS_DELAY:                /* right? */
339                  break;
340          case IFS_PROBE:
341                  ifi->state = IFS_IDLE;
342                  ifi->probes = 0;
343                  rtsol_timer_update(ifi);
344                  break;
345         }
346 }