rtadvd.c revision 1.1 1 1.1 itojun /*
2 1.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 1.1 itojun * All rights reserved.
4 1.1 itojun *
5 1.1 itojun * Redistribution and use in source and binary forms, with or without
6 1.1 itojun * modification, are permitted provided that the following conditions
7 1.1 itojun * are met:
8 1.1 itojun * 1. Redistributions of source code must retain the above copyright
9 1.1 itojun * notice, this list of conditions and the following disclaimer.
10 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer in the
12 1.1 itojun * documentation and/or other materials provided with the distribution.
13 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
14 1.1 itojun * may be used to endorse or promote products derived from this software
15 1.1 itojun * without specific prior written permission.
16 1.1 itojun *
17 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 itojun * SUCH DAMAGE.
28 1.1 itojun */
29 1.1 itojun
30 1.1 itojun #include <sys/param.h>
31 1.1 itojun #include <sys/socket.h>
32 1.1 itojun #include <sys/uio.h>
33 1.1 itojun #include <sys/time.h>
34 1.1 itojun
35 1.1 itojun #include <net/if.h>
36 1.1 itojun #include <net/route.h>
37 1.1 itojun #include <net/if_dl.h>
38 1.1 itojun #include <netinet/in.h>
39 1.1 itojun #include <netinet/ip6.h>
40 1.1 itojun #include <netinet6/ip6_var.h>
41 1.1 itojun #include <netinet/icmp6.h>
42 1.1 itojun
43 1.1 itojun #include <arpa/inet.h>
44 1.1 itojun
45 1.1 itojun #include <time.h>
46 1.1 itojun #include <unistd.h>
47 1.1 itojun #include <stdio.h>
48 1.1 itojun #include <err.h>
49 1.1 itojun #include <errno.h>
50 1.1 itojun #include <string.h>
51 1.1 itojun #include <stdlib.h>
52 1.1 itojun #include <syslog.h>
53 1.1 itojun #include "rtadvd.h"
54 1.1 itojun #include "rrenum.h"
55 1.1 itojun #include "advcap.h"
56 1.1 itojun #include "timer.h"
57 1.1 itojun #include "if.h"
58 1.1 itojun #include "config.h"
59 1.1 itojun
60 1.1 itojun struct msghdr rcvmhdr;
61 1.1 itojun static u_char rcvcmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo)) +
62 1.1 itojun CMSG_SPACE(sizeof(int))];
63 1.1 itojun struct msghdr sndmhdr;
64 1.1 itojun struct iovec rcviov[2];
65 1.1 itojun struct iovec sndiov[2];
66 1.1 itojun struct sockaddr_in6 from;
67 1.1 itojun struct sockaddr_in6 sin6_allnodes = {sizeof(sin6_allnodes), AF_INET6};
68 1.1 itojun int sock, rtsock;
69 1.1 itojun int accept_rr = 1;
70 1.1 itojun int dflag = 0, sflag = 0;
71 1.1 itojun
72 1.1 itojun u_char *conffile = NULL;
73 1.1 itojun
74 1.1 itojun struct rainfo *ralist = NULL;
75 1.1 itojun struct nd_optlist {
76 1.1 itojun struct nd_optlist *next;
77 1.1 itojun struct nd_opt_hdr *opt;
78 1.1 itojun };
79 1.1 itojun union nd_opts {
80 1.1 itojun struct nd_opt_hdr *nd_opt_array[7];
81 1.1 itojun struct {
82 1.1 itojun struct nd_opt_hdr *zero;
83 1.1 itojun struct nd_opt_hdr *src_lladdr;
84 1.1 itojun struct nd_opt_hdr *tgt_lladdr;
85 1.1 itojun struct nd_opt_prefix_info *pi;
86 1.1 itojun struct nd_opt_rd_hdr *rh;
87 1.1 itojun struct nd_opt_mtu *mtu;
88 1.1 itojun struct nd_optlist *list;
89 1.1 itojun } nd_opt_each;
90 1.1 itojun };
91 1.1 itojun #define nd_opts_src_lladdr nd_opt_each.src_lladdr
92 1.1 itojun #define nd_opts_tgt_lladdr nd_opt_each.tgt_lladdr
93 1.1 itojun #define nd_opts_pi nd_opt_each.pi
94 1.1 itojun #define nd_opts_rh nd_opt_each.rh
95 1.1 itojun #define nd_opts_mtu nd_opt_each.mtu
96 1.1 itojun #define nd_opts_list nd_opt_each.list
97 1.1 itojun
98 1.1 itojun #define NDOPT_FLAG_SRCLINKADDR 0x1
99 1.1 itojun #define NDOPT_FLAG_TGTLINKADDR 0x2
100 1.1 itojun #define NDOPT_FLAG_PREFIXINFO 0x4
101 1.1 itojun #define NDOPT_FLAG_RDHDR 0x8
102 1.1 itojun #define NDOPT_FLAG_MTU 0x10
103 1.1 itojun
104 1.1 itojun u_int32_t ndopt_flags[] = {
105 1.1 itojun 0, NDOPT_FLAG_SRCLINKADDR, NDOPT_FLAG_TGTLINKADDR,
106 1.1 itojun NDOPT_FLAG_PREFIXINFO, NDOPT_FLAG_RDHDR, NDOPT_FLAG_MTU
107 1.1 itojun };
108 1.1 itojun
109 1.1 itojun int main __P((int, char *[]));
110 1.1 itojun static void sock_open __P((void));
111 1.1 itojun static void rtsock_open __P((void));
112 1.1 itojun static void rtadvd_input __P((void));
113 1.1 itojun static void rs_input __P((int, struct nd_router_solicit *,
114 1.1 itojun struct in6_pktinfo *, struct sockaddr_in6 *));
115 1.1 itojun static void ra_input __P((int, struct nd_router_advert *,
116 1.1 itojun struct in6_pktinfo *, struct sockaddr_in6 *));
117 1.1 itojun static void prefix_check __P((struct nd_opt_prefix_info *, struct rainfo *,
118 1.1 itojun struct sockaddr_in6 *));
119 1.1 itojun static int nd6_options __P((struct nd_opt_hdr *, int,
120 1.1 itojun union nd_opts *, u_int32_t));
121 1.1 itojun static void free_ndopts __P((union nd_opts *));
122 1.1 itojun static struct rainfo *if_indextorainfo __P((int));
123 1.1 itojun static void ra_output __P((struct rainfo *));
124 1.1 itojun static void rtmsg_input __P((void));
125 1.1 itojun struct prefix *find_prefix __P((struct rainfo *, struct in6_addr *, int));
126 1.1 itojun
127 1.1 itojun
128 1.1 itojun int
129 1.1 itojun main(argc, argv)
130 1.1 itojun int argc;
131 1.1 itojun char *argv[];
132 1.1 itojun {
133 1.1 itojun fd_set fdset;
134 1.1 itojun int maxfd = 0;
135 1.1 itojun struct timeval *timeout;
136 1.1 itojun int i, ch;
137 1.1 itojun int fflag = 0;
138 1.1 itojun
139 1.1 itojun openlog(*argv, LOG_NDELAY|LOG_PID, LOG_DAEMON);
140 1.1 itojun
141 1.1 itojun /* get command line options and arguments */
142 1.1 itojun while ((ch = getopt(argc, argv, "c:dDfs")) != -1) {
143 1.1 itojun switch(ch) {
144 1.1 itojun case 'c':
145 1.1 itojun conffile = optarg;
146 1.1 itojun break;
147 1.1 itojun case 'd':
148 1.1 itojun dflag = 1;
149 1.1 itojun break;
150 1.1 itojun case 'D':
151 1.1 itojun dflag = 2;
152 1.1 itojun break;
153 1.1 itojun case 'f':
154 1.1 itojun fflag = 1;
155 1.1 itojun break;
156 1.1 itojun case 's':
157 1.1 itojun sflag = 1;
158 1.1 itojun }
159 1.1 itojun }
160 1.1 itojun argc -= optind;
161 1.1 itojun argv += optind;
162 1.1 itojun if (argc == 0) {
163 1.1 itojun fprintf(stderr,
164 1.1 itojun "usage: rtadvd [-c conffile] [-d|D] [-f] [-s]"
165 1.1 itojun "interfaces...\n");
166 1.1 itojun exit(1);
167 1.1 itojun }
168 1.1 itojun
169 1.1 itojun /* set log level */
170 1.1 itojun if (dflag == 0)
171 1.1 itojun (void)setlogmask(LOG_UPTO(LOG_ERR));
172 1.1 itojun if (dflag == 1)
173 1.1 itojun (void)setlogmask(LOG_UPTO(LOG_INFO));
174 1.1 itojun
175 1.1 itojun /* timer initialization */
176 1.1 itojun rtadvd_timer_init();
177 1.1 itojun
178 1.1 itojun /* random value initialization */
179 1.1 itojun srandom((u_long)time(NULL));
180 1.1 itojun
181 1.1 itojun /* get iflist block from kernel */
182 1.1 itojun init_iflist();
183 1.1 itojun
184 1.1 itojun while (argc--)
185 1.1 itojun getconfig(*argv++);
186 1.1 itojun
187 1.1 itojun inet_pton(AF_INET6, ALLNODES, &sin6_allnodes.sin6_addr);
188 1.1 itojun sock_open();
189 1.1 itojun
190 1.1 itojun if (!fflag)
191 1.1 itojun daemon(1, 0);
192 1.1 itojun
193 1.1 itojun FD_ZERO(&fdset);
194 1.1 itojun FD_SET(sock, &fdset);
195 1.1 itojun maxfd = sock;
196 1.1 itojun if (sflag == 0) {
197 1.1 itojun rtsock_open();
198 1.1 itojun FD_SET(rtsock, &fdset);
199 1.1 itojun if (rtsock > sock)
200 1.1 itojun maxfd = rtsock;
201 1.1 itojun }
202 1.1 itojun
203 1.1 itojun while (1) {
204 1.1 itojun struct fd_set select_fd = fdset; /* reinitialize */
205 1.1 itojun
206 1.1 itojun /* timer expiration check and reset the timer */
207 1.1 itojun timeout = rtadvd_check_timer();
208 1.1 itojun
209 1.1 itojun syslog(LOG_DEBUG,
210 1.1 itojun "<%s> set timer to %ld:%ld. waiting for inputs "
211 1.1 itojun "or timeout",
212 1.1 itojun __FUNCTION__,
213 1.1 itojun timeout->tv_sec, timeout->tv_usec);
214 1.1 itojun
215 1.1 itojun if ((i = select(maxfd + 1, &select_fd,
216 1.1 itojun NULL, NULL, timeout)) < 0){
217 1.1 itojun syslog(LOG_ERR, "<%s> select: %s",
218 1.1 itojun __FUNCTION__, strerror(errno));
219 1.1 itojun continue;
220 1.1 itojun }
221 1.1 itojun if (i == 0) /* timeout */
222 1.1 itojun continue;
223 1.1 itojun if (sflag == 0 && FD_ISSET(rtsock, &select_fd))
224 1.1 itojun rtmsg_input();
225 1.1 itojun if (FD_ISSET(sock, &select_fd))
226 1.1 itojun rtadvd_input();
227 1.1 itojun }
228 1.1 itojun exit(0); /* NOTREACHED */
229 1.1 itojun }
230 1.1 itojun
231 1.1 itojun static void
232 1.1 itojun rtmsg_input()
233 1.1 itojun {
234 1.1 itojun int n, len, type, ifindex, plen;
235 1.1 itojun char msg[2048], *next, *lim;
236 1.1 itojun u_char ifname[16];
237 1.1 itojun struct prefix *prefix;
238 1.1 itojun struct rainfo *rai;
239 1.1 itojun struct in6_addr *addr;
240 1.1 itojun char addrbuf[INET6_ADDRSTRLEN];
241 1.1 itojun
242 1.1 itojun n = read(rtsock, msg, 2048);
243 1.1 itojun if (dflag > 1) {
244 1.1 itojun syslog(LOG_DEBUG,
245 1.1 itojun "<%s> received a routing message "
246 1.1 itojun "(type = %d, len = %d)",
247 1.1 itojun __FUNCTION__,
248 1.1 itojun rtmsg_type(msg), n);
249 1.1 itojun }
250 1.1 itojun if (n > rtmsg_len(msg)) {
251 1.1 itojun /*
252 1.1 itojun * This usually won't happen for messages received on
253 1.1 itojun * an routing socket.
254 1.1 itojun */
255 1.1 itojun if (dflag > 1)
256 1.1 itojun syslog(LOG_DEBUG,
257 1.1 itojun "<%s> received data length is larger than"
258 1.1 itojun "1st routing message len. multiple messages?"
259 1.1 itojun " read %d bytes, but 1st msg len = %d",
260 1.1 itojun __FUNCTION__, n, rtmsg_len(msg));
261 1.1 itojun #if 0
262 1.1 itojun /* adjust length */
263 1.1 itojun n = rtmsg_len(msg);
264 1.1 itojun #endif
265 1.1 itojun }
266 1.1 itojun
267 1.1 itojun lim = msg + n;
268 1.1 itojun for (next = msg; next < lim; next += len) {
269 1.1 itojun next = get_next_msg(next, lim, 0, &len,
270 1.1 itojun RTADV_TYPE2BITMASK(RTM_ADD) |
271 1.1 itojun RTADV_TYPE2BITMASK(RTM_DELETE) |
272 1.1 itojun RTADV_TYPE2BITMASK(RTM_NEWADDR) |
273 1.1 itojun RTADV_TYPE2BITMASK(RTM_DELADDR) |
274 1.1 itojun RTADV_TYPE2BITMASK(RTM_IFINFO));
275 1.1 itojun if (len == 0)
276 1.1 itojun break;
277 1.1 itojun type = rtmsg_type(next);
278 1.1 itojun switch (type) {
279 1.1 itojun case RTM_ADD:
280 1.1 itojun case RTM_DELETE:
281 1.1 itojun ifindex = get_rtm_ifindex(next);
282 1.1 itojun break;
283 1.1 itojun case RTM_NEWADDR:
284 1.1 itojun case RTM_DELADDR:
285 1.1 itojun ifindex = get_ifam_ifindex(next);
286 1.1 itojun break;
287 1.1 itojun case RTM_IFINFO:
288 1.1 itojun ifindex = get_ifm_ifindex(next);
289 1.1 itojun break;
290 1.1 itojun default:
291 1.1 itojun /* should not reach here */
292 1.1 itojun if (dflag > 1) {
293 1.1 itojun syslog(LOG_DEBUG,
294 1.1 itojun "<%s:%d> unknown rtmsg %d on %s",
295 1.1 itojun __FUNCTION__, __LINE__, type,
296 1.1 itojun if_indextoname(ifindex, ifname));
297 1.1 itojun }
298 1.1 itojun return;
299 1.1 itojun }
300 1.1 itojun
301 1.1 itojun if ((rai = if_indextorainfo(ifindex)) == NULL) {
302 1.1 itojun if (dflag > 1) {
303 1.1 itojun syslog(LOG_DEBUG,
304 1.1 itojun "<%s> route changed on "
305 1.1 itojun "non advertising interface(%s)",
306 1.1 itojun __FUNCTION__,
307 1.1 itojun if_indextoname(ifindex, ifname));
308 1.1 itojun }
309 1.1 itojun return;
310 1.1 itojun }
311 1.1 itojun
312 1.1 itojun switch(type) {
313 1.1 itojun case RTM_ADD:
314 1.1 itojun /* init iffalgs because it may have changed */
315 1.1 itojun iflist[ifindex]->ifm_flags =
316 1.1 itojun if_getflags(ifindex,
317 1.1 itojun iflist[ifindex]->ifm_flags);
318 1.1 itojun
319 1.1 itojun addr = get_addr(msg);
320 1.1 itojun plen = get_prefixlen(msg);
321 1.1 itojun /* sanity check for plen */
322 1.1 itojun if (plen < 4 /* as RFC2373, prefixlen is at least 4 */
323 1.1 itojun || plen > 127) {
324 1.1 itojun syslog(LOG_INFO, "<%s> new interface route's"
325 1.1 itojun "plen %d is invalid for a prefix",
326 1.1 itojun __FUNCTION__, plen);
327 1.1 itojun return;
328 1.1 itojun }
329 1.1 itojun prefix = find_prefix(rai, addr, plen);
330 1.1 itojun if (prefix) {
331 1.1 itojun if (dflag > 1) {
332 1.1 itojun syslog(LOG_DEBUG,
333 1.1 itojun "<%s> new prefix(%s/%d) "
334 1.1 itojun "added on %s, "
335 1.1 itojun "but it was already in list",
336 1.1 itojun __FUNCTION__,
337 1.1 itojun inet_ntop(AF_INET6,
338 1.1 itojun addr, (char *)addrbuf,
339 1.1 itojun INET6_ADDRSTRLEN),
340 1.1 itojun plen,
341 1.1 itojun rai->ifname);
342 1.1 itojun }
343 1.1 itojun return;
344 1.1 itojun }
345 1.1 itojun make_prefix(rai, ifindex, addr, plen);
346 1.1 itojun break;
347 1.1 itojun case RTM_DELETE:
348 1.1 itojun /* init ifflags because it may have changed */
349 1.1 itojun iflist[ifindex]->ifm_flags =
350 1.1 itojun if_getflags(ifindex,
351 1.1 itojun iflist[ifindex]->ifm_flags);
352 1.1 itojun
353 1.1 itojun addr = get_addr(msg);
354 1.1 itojun plen = get_prefixlen(msg);
355 1.1 itojun /* sanity check for plen */
356 1.1 itojun if (plen < 4 /* as RFC2373, prefixlen is at least 4 */
357 1.1 itojun || plen > 127) {
358 1.1 itojun syslog(LOG_INFO, "<%s> deleted interface"
359 1.1 itojun "route's"
360 1.1 itojun "plen %d is invalid for a prefix",
361 1.1 itojun __FUNCTION__, plen);
362 1.1 itojun return;
363 1.1 itojun }
364 1.1 itojun prefix = find_prefix(rai, addr, plen);
365 1.1 itojun if (prefix == NULL) {
366 1.1 itojun if (dflag > 1) {
367 1.1 itojun syslog(LOG_DEBUG,
368 1.1 itojun "<%s> prefix(%s/%d) was "
369 1.1 itojun "deleted on %s, "
370 1.1 itojun "but it was not in list",
371 1.1 itojun __FUNCTION__,
372 1.1 itojun inet_ntop(AF_INET6,
373 1.1 itojun addr, (char *)addrbuf,
374 1.1 itojun INET6_ADDRSTRLEN),
375 1.1 itojun plen,
376 1.1 itojun rai->ifname);
377 1.1 itojun }
378 1.1 itojun return;
379 1.1 itojun }
380 1.1 itojun delete_prefix(rai, prefix);
381 1.1 itojun break;
382 1.1 itojun case RTM_NEWADDR:
383 1.1 itojun case RTM_DELADDR:
384 1.1 itojun /* init ifflags because it may have changed */
385 1.1 itojun iflist[ifindex]->ifm_flags =
386 1.1 itojun if_getflags(ifindex,
387 1.1 itojun iflist[ifindex]->ifm_flags);
388 1.1 itojun break;
389 1.1 itojun case RTM_IFINFO:
390 1.1 itojun iflist[ifindex]->ifm_flags = get_ifm_flags(next);
391 1.1 itojun break;
392 1.1 itojun default:
393 1.1 itojun /* should not reach here */
394 1.1 itojun if (dflag > 1) {
395 1.1 itojun syslog(LOG_DEBUG,
396 1.1 itojun "<%s:%d> unknown rtmsg %d on %s",
397 1.1 itojun __FUNCTION__, __LINE__, type,
398 1.1 itojun if_indextoname(ifindex, ifname));
399 1.1 itojun }
400 1.1 itojun return;
401 1.1 itojun }
402 1.1 itojun }
403 1.1 itojun
404 1.1 itojun return;
405 1.1 itojun }
406 1.1 itojun
407 1.1 itojun void
408 1.1 itojun rtadvd_input()
409 1.1 itojun {
410 1.1 itojun int i;
411 1.1 itojun int *hlimp = NULL;
412 1.1 itojun #ifdef OLDRAWSOCKET
413 1.1 itojun struct ip6_hdr *ip;
414 1.1 itojun #endif
415 1.1 itojun struct icmp6_hdr *icp;
416 1.1 itojun int ifindex = 0;
417 1.1 itojun struct cmsghdr *cm;
418 1.1 itojun struct in6_pktinfo *pi = NULL;
419 1.1 itojun u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
420 1.1 itojun struct in6_addr dst = in6addr_any;
421 1.1 itojun
422 1.1 itojun /*
423 1.1 itojun * Get message. We reset msg_controllen since the field could
424 1.1 itojun * be modified if we had received a message before setting
425 1.1 itojun * receive options.
426 1.1 itojun */
427 1.1 itojun rcvmhdr.msg_controllen = sizeof(rcvcmsgbuf);
428 1.1 itojun if ((i = recvmsg(sock, &rcvmhdr, 0)) < 0)
429 1.1 itojun return;
430 1.1 itojun
431 1.1 itojun /* extract optional information via Advanced API */
432 1.1 itojun for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
433 1.1 itojun cm;
434 1.1 itojun cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
435 1.1 itojun if (cm->cmsg_level == IPPROTO_IPV6 &&
436 1.1 itojun cm->cmsg_type == IPV6_PKTINFO &&
437 1.1 itojun cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
438 1.1 itojun pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
439 1.1 itojun ifindex = pi->ipi6_ifindex;
440 1.1 itojun dst = pi->ipi6_addr;
441 1.1 itojun }
442 1.1 itojun if (cm->cmsg_level == IPPROTO_IPV6 &&
443 1.1 itojun cm->cmsg_type == IPV6_HOPLIMIT &&
444 1.1 itojun cm->cmsg_len == CMSG_LEN(sizeof(int)))
445 1.1 itojun hlimp = (int *)CMSG_DATA(cm);
446 1.1 itojun }
447 1.1 itojun if (ifindex == 0) {
448 1.1 itojun syslog(LOG_ERR,
449 1.1 itojun "<%s> failed to get receiving interface",
450 1.1 itojun __FUNCTION__);
451 1.1 itojun return;
452 1.1 itojun }
453 1.1 itojun if (hlimp == NULL) {
454 1.1 itojun syslog(LOG_ERR,
455 1.1 itojun "<%s> failed to get receiving hop limit",
456 1.1 itojun __FUNCTION__);
457 1.1 itojun return;
458 1.1 itojun }
459 1.1 itojun
460 1.1 itojun #ifdef OLDRAWSOCKET
461 1.1 itojun if (i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
462 1.1 itojun syslog(LOG_ERR,
463 1.1 itojun "<%s> packet size(%d) is too short",
464 1.1 itojun __FUNCTION__, i);
465 1.1 itojun return;
466 1.1 itojun }
467 1.1 itojun
468 1.1 itojun ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
469 1.1 itojun icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
470 1.1 itojun #else
471 1.1 itojun if (i < sizeof(struct icmp6_hdr)) {
472 1.1 itojun syslog(LOG_ERR,
473 1.1 itojun "<%s> packet size(%d) is too short",
474 1.1 itojun __FUNCTION__, i);
475 1.1 itojun return;
476 1.1 itojun }
477 1.1 itojun
478 1.1 itojun icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
479 1.1 itojun #endif
480 1.1 itojun
481 1.1 itojun switch(icp->icmp6_type) {
482 1.1 itojun case ND_ROUTER_SOLICIT:
483 1.1 itojun /* hop limit verification - RFC-2461 6.1.1 */
484 1.1 itojun if (*hlimp != 255) {
485 1.1 itojun syslog(LOG_NOTICE,
486 1.1 itojun "<%s> invalid hop limit(%d) "
487 1.1 itojun "received from %s on %s",
488 1.1 itojun __FUNCTION__, *hlimp,
489 1.1 itojun inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
490 1.1 itojun INET6_ADDRSTRLEN),
491 1.1 itojun if_indextoname(pi->ipi6_ifindex, ifnamebuf));
492 1.1 itojun return;
493 1.1 itojun }
494 1.1 itojun rs_input(i, (struct nd_router_solicit *)icp, pi, &from);
495 1.1 itojun break;
496 1.1 itojun case ND_ROUTER_ADVERT:
497 1.1 itojun /* hop limit verification - RFC-2461 6.1.1 */
498 1.1 itojun if (*hlimp != 255) {
499 1.1 itojun syslog(LOG_NOTICE,
500 1.1 itojun "<%s> invalid hop limit(%d) "
501 1.1 itojun "received from %s on %s",
502 1.1 itojun __FUNCTION__, *hlimp,
503 1.1 itojun inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
504 1.1 itojun INET6_ADDRSTRLEN),
505 1.1 itojun if_indextoname(pi->ipi6_ifindex, ifnamebuf));
506 1.1 itojun return;
507 1.1 itojun }
508 1.1 itojun ra_input(i, (struct nd_router_advert *)icp, pi, &from);
509 1.1 itojun break;
510 1.1 itojun case ICMP6_ROUTER_RENUMBERING:
511 1.1 itojun if (accept_rr == 0) {
512 1.1 itojun syslog(LOG_ERR,
513 1.1 itojun "<%s> received a router renumbering "
514 1.1 itojun "message, but not allowed to be accepted",
515 1.1 itojun __FUNCTION__);
516 1.1 itojun break;
517 1.1 itojun }
518 1.1 itojun rr_input(i, (struct icmp6_router_renum *)icp, pi, &from,
519 1.1 itojun &dst);
520 1.1 itojun break;
521 1.1 itojun default:
522 1.1 itojun /*
523 1.1 itojun * Note that this case is POSSIBLE, especially just
524 1.1 itojun * after invocation of the daemon. This is because we
525 1.1 itojun * could receive message after opening the socket and
526 1.1 itojun * before setting ICMP6 type filter(see sock_open()).
527 1.1 itojun */
528 1.1 itojun syslog(LOG_ERR,
529 1.1 itojun "<%s> invalid icmp type(%d)",
530 1.1 itojun __FUNCTION__, icp->icmp6_type);
531 1.1 itojun return;
532 1.1 itojun }
533 1.1 itojun
534 1.1 itojun return;
535 1.1 itojun }
536 1.1 itojun
537 1.1 itojun static void
538 1.1 itojun rs_input(int len, struct nd_router_solicit *rs,
539 1.1 itojun struct in6_pktinfo *pi, struct sockaddr_in6 *from)
540 1.1 itojun {
541 1.1 itojun u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
542 1.1 itojun union nd_opts ndopts;
543 1.1 itojun struct rainfo *ra;
544 1.1 itojun
545 1.1 itojun syslog(LOG_DEBUG,
546 1.1 itojun "<%s> RS received from %s on %s",
547 1.1 itojun __FUNCTION__,
548 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
549 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
550 1.1 itojun if_indextoname(pi->ipi6_ifindex, ifnamebuf));
551 1.1 itojun
552 1.1 itojun /* ND option check */
553 1.1 itojun memset(&ndopts, 0, sizeof(ndopts));
554 1.1 itojun if (nd6_options((struct nd_opt_hdr *)(rs + 1),
555 1.1 itojun len - sizeof(struct nd_router_solicit),
556 1.1 itojun &ndopts, NDOPT_FLAG_SRCLINKADDR)) {
557 1.1 itojun syslog(LOG_DEBUG,
558 1.1 itojun "<%s> ND option check failed for an RS from %s on %s",
559 1.1 itojun __FUNCTION__,
560 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
561 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
562 1.1 itojun if_indextoname(pi->ipi6_ifindex, ifnamebuf));
563 1.1 itojun return;
564 1.1 itojun }
565 1.1 itojun
566 1.1 itojun /*
567 1.1 itojun * If the IP source address is the unspecified address, there
568 1.1 itojun * must be no source link-layer address option in the message.
569 1.1 itojun * (RFC-2461 6.1.1)
570 1.1 itojun */
571 1.1 itojun if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
572 1.1 itojun ndopts.nd_opts_src_lladdr) {
573 1.1 itojun syslog(LOG_ERR,
574 1.1 itojun "<%s> RS from unspecified src on %s has a link-layer"
575 1.1 itojun " address option",
576 1.1 itojun __FUNCTION__,
577 1.1 itojun if_indextoname(pi->ipi6_ifindex, ifnamebuf));
578 1.1 itojun goto done;
579 1.1 itojun }
580 1.1 itojun
581 1.1 itojun ra = ralist;
582 1.1 itojun while (ra != NULL) {
583 1.1 itojun if (pi->ipi6_ifindex == ra->ifindex)
584 1.1 itojun break;
585 1.1 itojun ra = ra->next;
586 1.1 itojun }
587 1.1 itojun if (ra == NULL) {
588 1.1 itojun syslog(LOG_INFO,
589 1.1 itojun "<%s> RS received on non advertising interface(%s)",
590 1.1 itojun __FUNCTION__,
591 1.1 itojun if_indextoname(pi->ipi6_ifindex, ifnamebuf));
592 1.1 itojun goto done;
593 1.1 itojun }
594 1.1 itojun
595 1.1 itojun /*
596 1.1 itojun * Decide whether to send RA according to the rate-limit
597 1.1 itojun * consideration.
598 1.1 itojun */
599 1.1 itojun {
600 1.1 itojun long delay; /* must not be greater than 1000000 */
601 1.1 itojun struct timeval interval, now, min_delay, tm_tmp, *rest;
602 1.1 itojun
603 1.1 itojun /*
604 1.1 itojun * If there is already a waiting RS packet, don't
605 1.1 itojun * update the timer.
606 1.1 itojun */
607 1.1 itojun if (ra->waiting++)
608 1.1 itojun goto done;
609 1.1 itojun
610 1.1 itojun /*
611 1.1 itojun * Compute a random delay. If the computed value
612 1.1 itojun * corresponds to a time later than the time the next
613 1.1 itojun * multicast RA is scheduled to be sent, ignore the random
614 1.1 itojun * delay and send the advertisement at the
615 1.1 itojun * already-scheduled time. RFC-2461 6.2.6
616 1.1 itojun */
617 1.1 itojun delay = random() % MAX_RA_DELAY_TIME;
618 1.1 itojun interval.tv_sec = 0;
619 1.1 itojun interval.tv_usec = delay;
620 1.1 itojun rest = rtadvd_timer_rest(ra->timer);
621 1.1 itojun if (TIMEVAL_LT(*rest, interval)) {
622 1.1 itojun syslog(LOG_DEBUG,
623 1.1 itojun "<%s> random delay is larger than "
624 1.1 itojun "the rest of normal timer",
625 1.1 itojun __FUNCTION__);
626 1.1 itojun interval = *rest;
627 1.1 itojun }
628 1.1 itojun
629 1.1 itojun /*
630 1.1 itojun * If we sent a multicast Router Advertisement within
631 1.1 itojun * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
632 1.1 itojun * the advertisement to be sent at a time corresponding to
633 1.1 itojun * MIN_DELAY_BETWEEN_RAS plus the random value after the
634 1.1 itojun * previous advertisement was sent.
635 1.1 itojun */
636 1.1 itojun gettimeofday(&now, NULL);
637 1.1 itojun TIMEVAL_SUB(&now, &ra->lastsent, &tm_tmp);
638 1.1 itojun min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
639 1.1 itojun min_delay.tv_usec = 0;
640 1.1 itojun if (TIMEVAL_LT(tm_tmp, min_delay)) {
641 1.1 itojun TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay);
642 1.1 itojun TIMEVAL_ADD(&min_delay, &interval, &interval);
643 1.1 itojun }
644 1.1 itojun rtadvd_set_timer(&interval, ra->timer);
645 1.1 itojun goto done;
646 1.1 itojun }
647 1.1 itojun
648 1.1 itojun done:
649 1.1 itojun free_ndopts(&ndopts);
650 1.1 itojun return;
651 1.1 itojun }
652 1.1 itojun
653 1.1 itojun static void
654 1.1 itojun ra_input(int len, struct nd_router_advert *ra,
655 1.1 itojun struct in6_pktinfo *pi, struct sockaddr_in6 *from)
656 1.1 itojun {
657 1.1 itojun struct rainfo *rai;
658 1.1 itojun u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
659 1.1 itojun union nd_opts ndopts;
660 1.1 itojun char *on_off[] = {"OFF", "ON"};
661 1.1 itojun u_int32_t reachabletime, retranstimer, mtu;
662 1.1 itojun
663 1.1 itojun syslog(LOG_DEBUG,
664 1.1 itojun "<%s> RA received from %s on %s",
665 1.1 itojun __FUNCTION__,
666 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
667 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
668 1.1 itojun if_indextoname(pi->ipi6_ifindex, ifnamebuf));
669 1.1 itojun
670 1.1 itojun /* ND option check */
671 1.1 itojun memset(&ndopts, 0, sizeof(ndopts));
672 1.1 itojun if (nd6_options((struct nd_opt_hdr *)(ra + 1),
673 1.1 itojun len - sizeof(struct nd_router_advert),
674 1.1 itojun &ndopts,
675 1.1 itojun NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU)) {
676 1.1 itojun syslog(LOG_ERR,
677 1.1 itojun "<%s> ND option check failed for an RA from %s on %s",
678 1.1 itojun __FUNCTION__,
679 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
680 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
681 1.1 itojun if_indextoname(pi->ipi6_ifindex, ifnamebuf));
682 1.1 itojun return;
683 1.1 itojun }
684 1.1 itojun
685 1.1 itojun /*
686 1.1 itojun * RA consistency check according to RFC-2461 6.2.7
687 1.1 itojun */
688 1.1 itojun if ((rai = if_indextorainfo(pi->ipi6_ifindex)) == 0) {
689 1.1 itojun syslog(LOG_INFO,
690 1.1 itojun "<%s> received RA from %s on non-advertising"
691 1.1 itojun " interface(%s)",
692 1.1 itojun __FUNCTION__,
693 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
694 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
695 1.1 itojun if_indextoname(pi->ipi6_ifindex, ifnamebuf));
696 1.1 itojun goto done;
697 1.1 itojun }
698 1.1 itojun /* Cur Hop Limit value */
699 1.1 itojun if (ra->nd_ra_curhoplimit && rai->hoplimit &&
700 1.1 itojun ra->nd_ra_curhoplimit != rai->hoplimit) {
701 1.1 itojun syslog(LOG_WARNING,
702 1.1 itojun "<%s> CurHopLimit inconsistent on %s:"
703 1.1 itojun " %d from %s, %d from us",
704 1.1 itojun __FUNCTION__,
705 1.1 itojun rai->ifname,
706 1.1 itojun ra->nd_ra_curhoplimit,
707 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
708 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
709 1.1 itojun rai->hoplimit);
710 1.1 itojun }
711 1.1 itojun /* M flag */
712 1.1 itojun if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
713 1.1 itojun rai->managedflg) {
714 1.1 itojun syslog(LOG_WARNING,
715 1.1 itojun "<%s> M flag inconsistent on %s:"
716 1.1 itojun " %s from %s, %s from us",
717 1.1 itojun __FUNCTION__,
718 1.1 itojun rai->ifname,
719 1.1 itojun on_off[!rai->managedflg],
720 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
721 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
722 1.1 itojun on_off[rai->managedflg]);
723 1.1 itojun }
724 1.1 itojun /* O flag */
725 1.1 itojun if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
726 1.1 itojun rai->otherflg) {
727 1.1 itojun syslog(LOG_WARNING,
728 1.1 itojun "<%s> O flag inconsistent on %s:"
729 1.1 itojun " %s from %s, %s from us",
730 1.1 itojun __FUNCTION__,
731 1.1 itojun rai->ifname,
732 1.1 itojun on_off[!rai->otherflg],
733 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
734 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
735 1.1 itojun on_off[rai->otherflg]);
736 1.1 itojun }
737 1.1 itojun /* Reachable Time */
738 1.1 itojun reachabletime = ntohl(ra->nd_ra_reachable);
739 1.1 itojun if (reachabletime && rai->reachabletime &&
740 1.1 itojun reachabletime != rai->reachabletime) {
741 1.1 itojun syslog(LOG_WARNING,
742 1.1 itojun "<%s> ReachableTime inconsistent on %s:"
743 1.1 itojun " %d from %s, %d from us",
744 1.1 itojun __FUNCTION__,
745 1.1 itojun rai->ifname,
746 1.1 itojun reachabletime,
747 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
748 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
749 1.1 itojun rai->reachabletime);
750 1.1 itojun }
751 1.1 itojun /* Retrans Timer */
752 1.1 itojun retranstimer = ntohl(ra->nd_ra_retransmit);
753 1.1 itojun if (retranstimer && rai->retranstimer &&
754 1.1 itojun retranstimer != rai->retranstimer) {
755 1.1 itojun syslog(LOG_WARNING,
756 1.1 itojun "<%s> RetranceTimer inconsistent on %s:"
757 1.1 itojun " %d from %s, %d from us",
758 1.1 itojun __FUNCTION__,
759 1.1 itojun rai->ifname,
760 1.1 itojun retranstimer,
761 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
762 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
763 1.1 itojun rai->retranstimer);
764 1.1 itojun }
765 1.1 itojun /* Values in the MTU options */
766 1.1 itojun if (ndopts.nd_opts_mtu) {
767 1.1 itojun mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
768 1.1 itojun if (mtu && rai->linkmtu && mtu != rai->linkmtu) {
769 1.1 itojun syslog(LOG_WARNING,
770 1.1 itojun "<%s> MTU option value inconsistent on %s:"
771 1.1 itojun " %d from %s, %d from us",
772 1.1 itojun __FUNCTION__,
773 1.1 itojun rai->ifname, mtu,
774 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
775 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
776 1.1 itojun rai->linkmtu);
777 1.1 itojun }
778 1.1 itojun }
779 1.1 itojun /* Preferred and Valid Lifetimes for prefixes */
780 1.1 itojun {
781 1.1 itojun struct nd_optlist *optp = ndopts.nd_opts_list;
782 1.1 itojun
783 1.1 itojun if (ndopts.nd_opts_pi)
784 1.1 itojun prefix_check(ndopts.nd_opts_pi, rai, from);
785 1.1 itojun while (optp) {
786 1.1 itojun prefix_check((struct nd_opt_prefix_info *)optp->opt,
787 1.1 itojun rai, from);
788 1.1 itojun optp = optp->next;
789 1.1 itojun }
790 1.1 itojun }
791 1.1 itojun
792 1.1 itojun done:
793 1.1 itojun free_ndopts(&ndopts);
794 1.1 itojun return;
795 1.1 itojun }
796 1.1 itojun
797 1.1 itojun static void
798 1.1 itojun prefix_check(struct nd_opt_prefix_info *pinfo,
799 1.1 itojun struct rainfo *rai, struct sockaddr_in6 *from)
800 1.1 itojun {
801 1.1 itojun u_int32_t preferred_time, valid_time;
802 1.1 itojun struct prefix *pp;
803 1.1 itojun u_char ntopbuf[INET6_ADDRSTRLEN], prefixbuf[INET6_ADDRSTRLEN];
804 1.1 itojun
805 1.1 itojun #if 0 /* impossible */
806 1.1 itojun if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
807 1.1 itojun return;
808 1.1 itojun #endif
809 1.1 itojun
810 1.1 itojun /*
811 1.1 itojun * log if the adveritsed prefix has link-local scope(sanity check?)
812 1.1 itojun */
813 1.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix)) {
814 1.1 itojun syslog(LOG_INFO,
815 1.1 itojun "<%s> link-local prefix %s/%d is advertised "
816 1.1 itojun "from %s on %s",
817 1.1 itojun __FUNCTION__,
818 1.1 itojun inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
819 1.1 itojun prefixbuf, INET6_ADDRSTRLEN),
820 1.1 itojun pinfo->nd_opt_pi_prefix_len,
821 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
822 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
823 1.1 itojun rai->ifname);
824 1.1 itojun }
825 1.1 itojun
826 1.1 itojun if ((pp = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
827 1.1 itojun pinfo->nd_opt_pi_prefix_len)) == NULL) {
828 1.1 itojun syslog(LOG_INFO,
829 1.1 itojun "<%s> prefix %s/%d from %s on %s is not in our list",
830 1.1 itojun __FUNCTION__,
831 1.1 itojun inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
832 1.1 itojun prefixbuf, INET6_ADDRSTRLEN),
833 1.1 itojun pinfo->nd_opt_pi_prefix_len,
834 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
835 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
836 1.1 itojun rai->ifname);
837 1.1 itojun return;
838 1.1 itojun }
839 1.1 itojun
840 1.1 itojun preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
841 1.1 itojun if (preferred_time != pp->preflifetime)
842 1.1 itojun syslog(LOG_WARNING,
843 1.1 itojun "<%s> prefeerred lifetime for %s/%d"
844 1.1 itojun " inconsistent on %s:"
845 1.1 itojun " %d from %s, %d from us",
846 1.1 itojun __FUNCTION__,
847 1.1 itojun inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
848 1.1 itojun prefixbuf, INET6_ADDRSTRLEN),
849 1.1 itojun pinfo->nd_opt_pi_prefix_len,
850 1.1 itojun rai->ifname, preferred_time,
851 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
852 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
853 1.1 itojun pp->preflifetime);
854 1.1 itojun
855 1.1 itojun valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
856 1.1 itojun if (valid_time != pp->validlifetime)
857 1.1 itojun syslog(LOG_WARNING,
858 1.1 itojun "<%s> valid lifetime for %s/%d"
859 1.1 itojun " inconsistent on %s:"
860 1.1 itojun " %d from %s, %d from us",
861 1.1 itojun __FUNCTION__,
862 1.1 itojun inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
863 1.1 itojun prefixbuf, INET6_ADDRSTRLEN),
864 1.1 itojun pinfo->nd_opt_pi_prefix_len,
865 1.1 itojun rai->ifname, valid_time,
866 1.1 itojun inet_ntop(AF_INET6, &from->sin6_addr,
867 1.1 itojun ntopbuf, INET6_ADDRSTRLEN),
868 1.1 itojun pp->validlifetime);
869 1.1 itojun }
870 1.1 itojun
871 1.1 itojun struct prefix *
872 1.1 itojun find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
873 1.1 itojun {
874 1.1 itojun struct prefix *pp;
875 1.1 itojun int bytelen, bitlen;
876 1.1 itojun
877 1.1 itojun for (pp = rai->prefix.next; pp != &rai->prefix; pp = pp->next) {
878 1.1 itojun if (plen != pp->prefixlen)
879 1.1 itojun continue;
880 1.1 itojun bytelen = plen / 8;
881 1.1 itojun bitlen = plen % 8;
882 1.1 itojun if (memcmp((void *)prefix, (void *)&pp->prefix, bytelen))
883 1.1 itojun continue;
884 1.1 itojun if (prefix->s6_addr[bytelen] >> (8 - bitlen) ==
885 1.1 itojun pp->prefix.s6_addr[bytelen] >> (8 - bitlen))
886 1.1 itojun return(pp);
887 1.1 itojun }
888 1.1 itojun
889 1.1 itojun return(NULL);
890 1.1 itojun }
891 1.1 itojun
892 1.1 itojun static int
893 1.1 itojun nd6_options(struct nd_opt_hdr *hdr, int limit,
894 1.1 itojun union nd_opts *ndopts, u_int32_t optflags)
895 1.1 itojun {
896 1.1 itojun int optlen = 0;
897 1.1 itojun
898 1.1 itojun for (; limit > 0; limit -= optlen) {
899 1.1 itojun hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
900 1.1 itojun optlen = hdr->nd_opt_len << 3;
901 1.1 itojun if (hdr->nd_opt_len == 0) {
902 1.1 itojun syslog(LOG_ERR,
903 1.1 itojun "<%s> bad ND option length(0) (type = %d)",
904 1.1 itojun __FUNCTION__, hdr->nd_opt_type);
905 1.1 itojun goto bad;
906 1.1 itojun }
907 1.1 itojun
908 1.1 itojun if (hdr->nd_opt_type > ND_OPT_MTU) {
909 1.1 itojun syslog(LOG_INFO,
910 1.1 itojun "<%s> unknown ND option(type %d)",
911 1.1 itojun __FUNCTION__,
912 1.1 itojun hdr->nd_opt_type);
913 1.1 itojun continue;
914 1.1 itojun }
915 1.1 itojun
916 1.1 itojun if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
917 1.1 itojun syslog(LOG_INFO,
918 1.1 itojun "<%s> unexpected ND option(type %d)",
919 1.1 itojun __FUNCTION__,
920 1.1 itojun hdr->nd_opt_type);
921 1.1 itojun continue;
922 1.1 itojun }
923 1.1 itojun
924 1.1 itojun switch(hdr->nd_opt_type) {
925 1.1 itojun case ND_OPT_SOURCE_LINKADDR:
926 1.1 itojun case ND_OPT_TARGET_LINKADDR:
927 1.1 itojun case ND_OPT_REDIRECTED_HEADER:
928 1.1 itojun case ND_OPT_MTU:
929 1.1 itojun if (ndopts->nd_opt_array[hdr->nd_opt_type]) {
930 1.1 itojun syslog(LOG_INFO,
931 1.1 itojun "<%s> duplicated ND option"
932 1.1 itojun " (type = %d)",
933 1.1 itojun __FUNCTION__,
934 1.1 itojun hdr->nd_opt_type);
935 1.1 itojun }
936 1.1 itojun ndopts->nd_opt_array[hdr->nd_opt_type] = hdr;
937 1.1 itojun break;
938 1.1 itojun case ND_OPT_PREFIX_INFORMATION:
939 1.1 itojun {
940 1.1 itojun struct nd_optlist *pfxlist;
941 1.1 itojun
942 1.1 itojun if (ndopts->nd_opts_pi == 0) {
943 1.1 itojun ndopts->nd_opts_pi =
944 1.1 itojun (struct nd_opt_prefix_info *)hdr;
945 1.1 itojun continue;
946 1.1 itojun }
947 1.1 itojun if ((pfxlist = malloc(sizeof(*pfxlist))) == NULL) {
948 1.1 itojun syslog(LOG_ERR,
949 1.1 itojun "<%s> can't allocate memory",
950 1.1 itojun __FUNCTION__);
951 1.1 itojun goto bad;
952 1.1 itojun }
953 1.1 itojun pfxlist->next = ndopts->nd_opts_list;
954 1.1 itojun pfxlist->opt = hdr;
955 1.1 itojun ndopts->nd_opts_list = pfxlist;
956 1.1 itojun
957 1.1 itojun break;
958 1.1 itojun }
959 1.1 itojun default: /* impossible */
960 1.1 itojun break;
961 1.1 itojun }
962 1.1 itojun }
963 1.1 itojun
964 1.1 itojun return(0);
965 1.1 itojun
966 1.1 itojun bad:
967 1.1 itojun free_ndopts(ndopts);
968 1.1 itojun
969 1.1 itojun return(-1);
970 1.1 itojun }
971 1.1 itojun
972 1.1 itojun static void
973 1.1 itojun free_ndopts(union nd_opts *ndopts)
974 1.1 itojun {
975 1.1 itojun struct nd_optlist *opt = ndopts->nd_opts_list, *next;
976 1.1 itojun
977 1.1 itojun while(opt) {
978 1.1 itojun next = opt->next;
979 1.1 itojun free(opt);
980 1.1 itojun opt = next;
981 1.1 itojun }
982 1.1 itojun }
983 1.1 itojun
984 1.1 itojun void
985 1.1 itojun sock_open()
986 1.1 itojun {
987 1.1 itojun struct icmp6_filter filt;
988 1.1 itojun struct ipv6_mreq mreq;
989 1.1 itojun struct rainfo *ra = ralist;
990 1.1 itojun int on;
991 1.1 itojun /* XXX: should be max MTU attached to the node */
992 1.1 itojun static u_char answer[1500];
993 1.1 itojun static u_char sndcmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo)) +
994 1.1 itojun CMSG_SPACE(sizeof(int))];
995 1.1 itojun
996 1.1 itojun if ((sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
997 1.1 itojun syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
998 1.1 itojun strerror(errno));
999 1.1 itojun exit(1);
1000 1.1 itojun }
1001 1.1 itojun
1002 1.1 itojun /* specify to tell receiving interface */
1003 1.1 itojun on = 1;
1004 1.1 itojun if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
1005 1.1 itojun sizeof(on)) < 0) {
1006 1.1 itojun syslog(LOG_ERR, "<%s> IPV6_PKTINFO: %s",
1007 1.1 itojun __FUNCTION__, strerror(errno));
1008 1.1 itojun exit(1);
1009 1.1 itojun }
1010 1.1 itojun
1011 1.1 itojun on = 1;
1012 1.1 itojun /* specify to tell value of hoplimit field of received IP6 hdr */
1013 1.1 itojun if (setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
1014 1.1 itojun sizeof(on)) < 0) {
1015 1.1 itojun syslog(LOG_ERR, "<%s> IPV6_HOPLIMIT: %s",
1016 1.1 itojun __FUNCTION__, strerror(errno));
1017 1.1 itojun exit(1);
1018 1.1 itojun }
1019 1.1 itojun
1020 1.1 itojun ICMP6_FILTER_SETBLOCKALL(&filt);
1021 1.1 itojun ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
1022 1.1 itojun ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1023 1.1 itojun if (accept_rr)
1024 1.1 itojun ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1025 1.1 itojun if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1026 1.1 itojun sizeof(filt)) < 0) {
1027 1.1 itojun syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1028 1.1 itojun __FUNCTION__, strerror(errno));
1029 1.1 itojun exit(1);
1030 1.1 itojun }
1031 1.1 itojun
1032 1.1 itojun /*
1033 1.1 itojun * join all routers multicast address on each advertising interface.
1034 1.1 itojun */
1035 1.1 itojun if (inet_pton(AF_INET6, ALLROUTERS, &mreq.ipv6mr_multiaddr.s6_addr)
1036 1.1 itojun != 1) {
1037 1.1 itojun syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
1038 1.1 itojun __FUNCTION__);
1039 1.1 itojun exit(1);
1040 1.1 itojun }
1041 1.1 itojun while(ra) {
1042 1.1 itojun mreq.ipv6mr_interface = ra->ifindex;
1043 1.1 itojun if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
1044 1.1 itojun &mreq,
1045 1.1 itojun sizeof(mreq)) < 0) {
1046 1.1 itojun syslog(LOG_ERR, "<%s> IPV6_JOIN_GROUP on %s: %s",
1047 1.1 itojun __FUNCTION__, ra->ifname, strerror(errno));
1048 1.1 itojun exit(1);
1049 1.1 itojun }
1050 1.1 itojun ra = ra->next;
1051 1.1 itojun }
1052 1.1 itojun
1053 1.1 itojun /* initialize msghdr for receiving packets */
1054 1.1 itojun rcviov[0].iov_base = (caddr_t)answer;
1055 1.1 itojun rcviov[0].iov_len = sizeof(answer);
1056 1.1 itojun rcvmhdr.msg_name = (caddr_t)&from;
1057 1.1 itojun rcvmhdr.msg_namelen = sizeof(from);
1058 1.1 itojun rcvmhdr.msg_iov = rcviov;
1059 1.1 itojun rcvmhdr.msg_iovlen = 1;
1060 1.1 itojun rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
1061 1.1 itojun rcvmhdr.msg_controllen = sizeof(rcvcmsgbuf);
1062 1.1 itojun
1063 1.1 itojun /* initialize msghdr for sending packets */
1064 1.1 itojun sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
1065 1.1 itojun sndmhdr.msg_iov = sndiov;
1066 1.1 itojun sndmhdr.msg_iovlen = 1;
1067 1.1 itojun sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
1068 1.1 itojun sndmhdr.msg_controllen = sizeof(sndcmsgbuf);
1069 1.1 itojun
1070 1.1 itojun return;
1071 1.1 itojun }
1072 1.1 itojun
1073 1.1 itojun /* open a routing socket to watch the routing table */
1074 1.1 itojun static void
1075 1.1 itojun rtsock_open()
1076 1.1 itojun {
1077 1.1 itojun if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1078 1.1 itojun syslog(LOG_ERR,
1079 1.1 itojun "<%s> socket: %s", __FUNCTION__, strerror(errno));
1080 1.1 itojun exit(1);
1081 1.1 itojun }
1082 1.1 itojun }
1083 1.1 itojun
1084 1.1 itojun static struct rainfo *
1085 1.1 itojun if_indextorainfo(int index)
1086 1.1 itojun {
1087 1.1 itojun struct rainfo *rai = ralist;
1088 1.1 itojun
1089 1.1 itojun for (rai = ralist; rai; rai = rai->next) {
1090 1.1 itojun if (rai->ifindex == index)
1091 1.1 itojun return(rai);
1092 1.1 itojun }
1093 1.1 itojun
1094 1.1 itojun return(NULL); /* search failed */
1095 1.1 itojun }
1096 1.1 itojun
1097 1.1 itojun static void
1098 1.1 itojun ra_output(rainfo)
1099 1.1 itojun struct rainfo *rainfo;
1100 1.1 itojun {
1101 1.1 itojun int i;
1102 1.1 itojun
1103 1.1 itojun struct cmsghdr *cm;
1104 1.1 itojun struct in6_pktinfo *pi;
1105 1.1 itojun
1106 1.1 itojun sndmhdr.msg_name = (caddr_t)&sin6_allnodes;
1107 1.1 itojun sndmhdr.msg_iov[0].iov_base = (caddr_t)rainfo->ra_data;
1108 1.1 itojun sndmhdr.msg_iov[0].iov_len = rainfo->ra_datalen;
1109 1.1 itojun
1110 1.1 itojun cm = CMSG_FIRSTHDR(&sndmhdr);
1111 1.1 itojun /* specify the outgoing interface */
1112 1.1 itojun cm->cmsg_level = IPPROTO_IPV6;
1113 1.1 itojun cm->cmsg_type = IPV6_PKTINFO;
1114 1.1 itojun cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1115 1.1 itojun pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1116 1.1 itojun memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
1117 1.1 itojun pi->ipi6_ifindex = rainfo->ifindex;
1118 1.1 itojun
1119 1.1 itojun /* specify the hop limit of the packet */
1120 1.1 itojun {
1121 1.1 itojun int hoplimit = 255;
1122 1.1 itojun
1123 1.1 itojun cm = CMSG_NXTHDR(&sndmhdr, cm);
1124 1.1 itojun cm->cmsg_level = IPPROTO_IPV6;
1125 1.1 itojun cm->cmsg_type = IPV6_HOPLIMIT;
1126 1.1 itojun cm->cmsg_len = CMSG_LEN(sizeof(int));
1127 1.1 itojun memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1128 1.1 itojun }
1129 1.1 itojun
1130 1.1 itojun syslog(LOG_DEBUG,
1131 1.1 itojun "<%s> send RA on %s, # of waitings = %d",
1132 1.1 itojun __FUNCTION__, rainfo->ifname, rainfo->waiting);
1133 1.1 itojun
1134 1.1 itojun i = sendmsg(sock, &sndmhdr, 0);
1135 1.1 itojun
1136 1.1 itojun if (i < 0 || i != rainfo->ra_datalen) {
1137 1.1 itojun if (i < 0) {
1138 1.1 itojun syslog(LOG_ERR, "<%s> sendmsg: %s",
1139 1.1 itojun __FUNCTION__, strerror(errno));
1140 1.1 itojun }
1141 1.1 itojun }
1142 1.1 itojun
1143 1.1 itojun /* update counter */
1144 1.1 itojun if (rainfo->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS)
1145 1.1 itojun rainfo->initcounter++;
1146 1.1 itojun
1147 1.1 itojun /* update timestamp */
1148 1.1 itojun gettimeofday(&rainfo->lastsent, NULL);
1149 1.1 itojun
1150 1.1 itojun /* reset waiting conter */
1151 1.1 itojun rainfo->waiting = 0;
1152 1.1 itojun }
1153 1.1 itojun
1154 1.1 itojun /* process RA timer */
1155 1.1 itojun void
1156 1.1 itojun ra_timeout(void *data)
1157 1.1 itojun {
1158 1.1 itojun struct rainfo *rai = (struct rainfo *)data;
1159 1.1 itojun
1160 1.1 itojun #ifdef notyet
1161 1.1 itojun /* if necessary, reconstruct the packet. */
1162 1.1 itojun #endif
1163 1.1 itojun
1164 1.1 itojun syslog(LOG_DEBUG,
1165 1.1 itojun "<%s> RA timer on %s is expired",
1166 1.1 itojun __FUNCTION__, rai->ifname);
1167 1.1 itojun
1168 1.1 itojun if (iflist[rai->ifindex]->ifm_flags & IFF_UP)
1169 1.1 itojun ra_output(rai);
1170 1.1 itojun else
1171 1.1 itojun syslog(LOG_DEBUG, "<%s> %s is not up, skip sending RA",
1172 1.1 itojun __FUNCTION__, rai->ifname);
1173 1.1 itojun }
1174 1.1 itojun
1175 1.1 itojun /* update RA timer */
1176 1.1 itojun void
1177 1.1 itojun ra_timer_update(void *data, struct timeval *tm)
1178 1.1 itojun {
1179 1.1 itojun struct rainfo *rai = (struct rainfo *)data;
1180 1.1 itojun long interval;
1181 1.1 itojun
1182 1.1 itojun /*
1183 1.1 itojun * Whenever a multicast advertisement is sent from an interface,
1184 1.1 itojun * the timer is reset to a uniformly-distributed random value
1185 1.1 itojun * between the interface's configured MinRtrAdvInterval and
1186 1.1 itojun * MaxRtrAdvInterval(discovery-v2-02 6.2.4).
1187 1.1 itojun */
1188 1.1 itojun interval = rai->mininterval;
1189 1.1 itojun interval += random() % (rai->maxinterval - rai->mininterval);
1190 1.1 itojun
1191 1.1 itojun /*
1192 1.1 itojun * For the first few advertisements (up to
1193 1.1 itojun * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen interval
1194 1.1 itojun * is greater than MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer
1195 1.1 itojun * SHOULD be set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.
1196 1.1 itojun * (RFC-2461 6.2.4)
1197 1.1 itojun */
1198 1.1 itojun if (rai->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS &&
1199 1.1 itojun interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)
1200 1.1 itojun interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
1201 1.1 itojun
1202 1.1 itojun tm->tv_sec = interval;
1203 1.1 itojun tm->tv_usec = 0;
1204 1.1 itojun
1205 1.1 itojun syslog(LOG_DEBUG,
1206 1.1 itojun "<%s> RA timer on %s is set to %ld:%ld",
1207 1.1 itojun __FUNCTION__, rai->ifname, tm->tv_sec, tm->tv_usec);
1208 1.1 itojun
1209 1.1 itojun return;
1210 1.1 itojun }
1211