1 /* $KAME: rrenumd.c,v 1.20 2000/11/08 02:40:53 itojun Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/param.h>
35 #include <sys/socket.h>
41 #include <net/route.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/in.h>
45 #include <netinet/ip.h>
46 #include <netinet/ip6.h>
47 #include <netinet/icmp6.h>
49 #include <arpa/inet.h>
52 #include <netipsec/ipsec.h>
64 #define LL_ALLROUTERS "ff02::2"
65 #define SL_ALLROUTERS "ff05::2"
67 #define RR_MCHLIM_DEFAULT 64
69 #ifndef IN6_IS_SCOPE_LINKLOCAL
70 #define IN6_IS_SCOPE_LINKLOCAL(a) \
71 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
72 (IN6_IS_ADDR_MC_LINKLOCAL(a)))
73 #endif /* IN6_IS_SCOPE_LINKLOCAL */
79 #ifdef IPSEC_POLICY_IPSEC
81 #else /* IPSEC_POLICY_IPSEC */
84 #endif /* IPSEC_POLICY_IPSEC */
88 struct msghdr sndmhdr
;
89 struct msghdr rcvmhdr
;
90 struct sockaddr_in6 from
;
91 struct sockaddr_in6 sin6_ll_allrouters
;
94 int with_v4dest
, with_v6dest
;
95 struct in6_addr prefix
; /* ADHOC */
96 int prefixlen
= 64; /* ADHOC */
98 extern int parse(FILE **);
100 static void show_usage(void);
101 static void init_sin6(struct sockaddr_in6
*, const char *);
103 static void join_multi(const char *);
105 static void init_globals(void);
106 static void config(FILE **);
107 #ifdef IPSEC_POLICY_IPSEC
108 static void sock6_open(struct flags
*, char *);
109 static void sock4_open(struct flags
*, char *);
111 static void sock6_open(struct flags
*);
112 static void sock4_open(struct flags
*);
114 static void rrenum_output(struct payload_list
*, struct dst_list
*);
115 static void rrenum_snd_eachdst(struct payload_list
*);
117 static void rrenum_snd_fullsequence(void);
119 static void rrenum_input(int);
120 int main(int, char *[]);
123 /* Print usage. Don't call this after daemonized. */
127 fprintf(stderr
, "usage: rrenumd [-c conf_file|-s] [-df"
129 #ifdef IPSEC_POLICY_IPSEC
131 #else /* IPSEC_POLICY_IPSEC */
133 #endif /* IPSEC_POLICY_IPSEC */
140 init_sin6(struct sockaddr_in6
*sin6
, const char *addr_ascii
)
142 memset(sin6
, 0, sizeof(*sin6
));
143 sin6
->sin6_len
= sizeof(*sin6
);
144 sin6
->sin6_family
= AF_INET6
;
145 if (inet_pton(AF_INET6
, addr_ascii
, &sin6
->sin6_addr
) != 1)
146 ; /* XXX do something */
149 #if 0 /* XXX: not necessary ?? */
151 join_multi(const char *addrname
)
153 struct ipv6_mreq mreq
;
155 if (inet_pton(AF_INET6
, addrname
, &mreq
.ipv6mr_multiaddr
.s6_addr
)
157 syslog(LOG_ERR
, "<%s> inet_pton failed(library bug?)",
161 /* ADHOC: currently join only one */
163 if ((mreq
.ipv6mr_interface
= if_nametoindex(ifname
)) == 0) {
164 syslog(LOG_ERR
, "<%s> ifname %s should be invalid: %s",
165 __func__
, ifname
, strerror(errno
));
168 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
171 syslog(LOG_ERR
, "<%s> IPV6_JOIN_GROUP on %s: %s",
172 __func__
, ifname
, strerror(errno
));
182 static struct iovec rcviov
;
183 static u_char rprdata
[4500]; /* maximal MTU of connected links */
184 static u_char
*rcvcmsgbuf
= NULL
;
185 static u_char
*sndcmsgbuf
= NULL
;
186 int sndcmsglen
, rcvcmsglen
;
188 /* init ll_allrouters */
189 init_sin6(&sin6_ll_allrouters
, LL_ALLROUTERS
);
191 /* initialize msghdr for receiving packets */
192 rcviov
.iov_base
= (caddr_t
)rprdata
;
193 rcviov
.iov_len
= sizeof(rprdata
);
194 rcvmhdr
.msg_namelen
= sizeof(struct sockaddr_in6
);
195 rcvmhdr
.msg_iov
= &rcviov
;
196 rcvmhdr
.msg_iovlen
= 1;
197 rcvcmsglen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
198 CMSG_SPACE(sizeof(int));
199 if (rcvcmsgbuf
== NULL
&&
200 (rcvcmsgbuf
= (u_char
*)malloc(rcvcmsglen
)) == NULL
) {
201 syslog(LOG_ERR
, "<%s>: malloc failed", __func__
);
204 rcvmhdr
.msg_control
= (caddr_t
)rcvcmsgbuf
;
205 rcvmhdr
.msg_controllen
= rcvcmsglen
;
207 /* initialize msghdr for sending packets */
208 sndmhdr
.msg_namelen
= sizeof(struct sockaddr_in6
);
209 sndmhdr
.msg_iovlen
= 1;
210 sndcmsglen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
211 CMSG_SPACE(sizeof(int));
212 if (sndcmsgbuf
== NULL
&&
213 (sndcmsgbuf
= (u_char
*)malloc(sndcmsglen
)) == NULL
) {
214 syslog(LOG_ERR
, "<%s>: malloc failed", __func__
);
217 sndmhdr
.msg_control
= (caddr_t
)sndcmsgbuf
;
218 sndmhdr
.msg_controllen
= sndcmsglen
;
224 struct payload_list
*pl
;
226 struct icmp6_router_renum
*irr
;
227 struct rr_pco_match
*rpm
;
229 if (parse(fpp
) < 0) {
230 syslog(LOG_ERR
, "<%s> parse failed", __func__
);
234 /* initialize fields not configured by parser */
235 for (pl
= pl_head
; pl
; pl
= pl
->pl_next
) {
236 iov
= (struct iovec
*)&pl
->pl_sndiov
;
237 irr
= (struct icmp6_router_renum
*)&pl
->pl_irr
;
238 rpm
= (struct rr_pco_match
*)&pl
->pl_rpm
;
240 irr
->rr_type
= ICMP6_ROUTER_RENUMBERING
;
243 * now we don't support multiple PCOs in a rr message.
244 * so segment number is not supported.
246 /* TODO: rr flags config in parser */
247 irr
->rr_flags
|= ICMP6_RR_FLAGS_SPECSITE
;
248 /* TODO: max delay config in parser */
251 * means only 1 use_prefix is contained as router-renum-05.txt.
252 * now we don't support multiple PCOs in a rr message,
253 * nor multiple use_prefix in one PCO.
255 rpm
->rpm_len
= 4*1 +3;
256 rpm
->rpm_ordinal
= 0;
257 iov
->iov_base
= (caddr_t
)irr
;
258 iov
->iov_len
= sizeof(struct icmp6_router_renum
)
259 + sizeof(struct rr_pco_match
)
260 + sizeof(struct rr_pco_use
);
265 sock6_open(struct flags
*flags
266 #ifdef IPSEC_POLICY_IPSEC
268 #endif /* IPSEC_POLICY_IPSEC */
271 struct icmp6_filter filt
;
274 #ifndef IPSEC_POLICY_IPSEC
279 if (with_v6dest
== 0)
282 (s6
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
)) < 0) {
283 syslog(LOG_ERR
, "<%s> socket(v6): %s", __func__
,
289 * join all routers multicast addresses.
291 #if 0 /* XXX: not necessary ?? */
292 join_multi(LL_ALLROUTERS
);
293 join_multi(SL_ALLROUTERS
);
296 /* set icmpv6 filter */
297 ICMP6_FILTER_SETBLOCKALL(&filt
);
298 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING
, &filt
);
299 if (setsockopt(s6
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
301 syslog(LOG_ERR
, "<%s> IICMP6_FILTER: %s",
302 __func__
, strerror(errno
));
306 /* specify to tell receiving interface */
308 if (setsockopt(s6
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
, &on
,
310 syslog(LOG_ERR
, "<%s> IPV6_RECVPKTINFO: %s",
311 __func__
, strerror(errno
));
316 #ifdef IPSEC_POLICY_IPSEC
319 buf
= ipsec_set_policy(policy
, strlen(policy
));
321 errx(1, "%s", ipsec_strerror());
322 /* XXX should handle in/out bound policy. */
323 if (setsockopt(s6
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
,
324 buf
, ipsec_get_policylen(buf
)) < 0)
325 err(1, "setsockopt(IPV6_IPSEC_POLICY)");
328 #else /* IPSEC_POLICY_IPSEC */
330 optval
= IPSEC_LEVEL_REQUIRE
;
331 if (setsockopt(s6
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
332 &optval
, sizeof(optval
)) == -1) {
333 syslog(LOG_ERR
, "<%s> IPV6_AUTH_TRANS_LEVEL: %s",
334 __func__
, strerror(errno
));
338 if (flags
->encrypt
) {
339 optval
= IPSEC_LEVEL_REQUIRE
;
340 if (setsockopt(s6
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
341 &optval
, sizeof(optval
)) == -1) {
342 syslog(LOG_ERR
, "<%s> IPV6_ESP_TRANS_LEVEL: %s",
343 __func__
, strerror(errno
));
347 #endif /* IPSEC_POLICY_IPSEC */
354 sock4_open(struct flags
*flags
355 #ifdef IPSEC_POLICY_IPSEC
357 #endif /* IPSEC_POLICY_IPSEC */
361 #ifndef IPSEC_POLICY_IPSEC
366 if (with_v4dest
== 0)
368 if ((s4
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMPV6
)) < 0) {
369 syslog(LOG_ERR
, "<%s> socket(v4): %s", __func__
,
374 #if 0 /* XXX: not necessary ?? */
376 * join all routers multicast addresses.
378 some_join_function();
382 #ifdef IPSEC_POLICY_IPSEC
385 buf
= ipsec_set_policy(policy
, strlen(policy
));
387 errx(1, "%s", ipsec_strerror());
388 /* XXX should handle in/out bound policy. */
389 if (setsockopt(s4
, IPPROTO_IP
, IP_IPSEC_POLICY
,
390 buf
, ipsec_get_policylen(buf
)) < 0)
391 err(1, "setsockopt(IP_IPSEC_POLICY)");
394 #else /* IPSEC_POLICY_IPSEC */
396 optval
= IPSEC_LEVEL_REQUIRE
;
397 if (setsockopt(s4
, IPPROTO_IP
, IP_AUTH_TRANS_LEVEL
,
398 &optval
, sizeof(optval
)) == -1) {
399 syslog(LOG_ERR
, "<%s> IP_AUTH_TRANS_LEVEL: %s",
400 __func__
, strerror(errno
));
404 if (flags
->encrypt
) {
405 optval
= IPSEC_LEVEL_REQUIRE
;
406 if (setsockopt(s4
, IPPROTO_IP
, IP_ESP_TRANS_LEVEL
,
407 &optval
, sizeof(optval
)) == -1) {
408 syslog(LOG_ERR
, "<%s> IP_ESP_TRANS_LEVEL: %s",
409 __func__
, strerror(errno
));
413 #endif /* IPSEC_POLICY_IPSEC */
420 rrenum_output(struct payload_list
*pl
, struct dst_list
*dl
)
424 struct in6_pktinfo
*pi
;
425 struct sockaddr_in6
*sin6
= NULL
;
427 sndmhdr
.msg_name
= (caddr_t
)dl
->dl_dst
;
428 if (dl
->dl_dst
->sa_family
== AF_INET6
)
429 sin6
= (struct sockaddr_in6
*)dl
->dl_dst
;
432 IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
433 int hoplimit
= RR_MCHLIM_DEFAULT
;
435 cm
= CMSG_FIRSTHDR(&sndmhdr
);
436 /* specify the outgoing interface */
437 cm
->cmsg_level
= IPPROTO_IPV6
;
438 cm
->cmsg_type
= IPV6_PKTINFO
;
439 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
440 pi
= (struct in6_pktinfo
*)CMSG_DATA(cm
);
441 memset(&pi
->ipi6_addr
, 0, sizeof(pi
->ipi6_addr
)); /*XXX*/
442 pi
->ipi6_ifindex
= sin6
->sin6_scope_id
;
443 msglen
+= CMSG_LEN(sizeof(struct in6_pktinfo
));
445 /* specify the hop limit of the packet if dest is link local */
446 /* not defined by router-renum-05.txt, but maybe its OK */
447 cm
= CMSG_NXTHDR(&sndmhdr
, cm
);
448 cm
->cmsg_level
= IPPROTO_IPV6
;
449 cm
->cmsg_type
= IPV6_HOPLIMIT
;
450 cm
->cmsg_len
= CMSG_LEN(sizeof(int));
451 memcpy(CMSG_DATA(cm
), &hoplimit
, sizeof(int));
452 msglen
+= CMSG_LEN(sizeof(int));
454 sndmhdr
.msg_controllen
= msglen
;
455 if (sndmhdr
.msg_controllen
== 0)
456 sndmhdr
.msg_control
= 0;
458 sndmhdr
.msg_iov
= &pl
->pl_sndiov
;
459 i
= sendmsg(dl
->dl_dst
->sa_family
== AF_INET
? s4
: s6
, &sndmhdr
, 0);
461 if (i
< 0 || i
!= sndmhdr
.msg_iov
->iov_len
)
462 syslog(LOG_ERR
, "<%s> sendmsg: %s", __func__
,
467 rrenum_snd_eachdst(struct payload_list
*pl
)
471 for (dl
= dl_head
; dl
; dl
= dl
->dl_next
) {
472 rrenum_output(pl
, dl
);
478 rrenum_snd_fullsequence()
480 struct payload_list
*pl
;
482 for (pl
= pl_head
; pl
; pl
= pl
->pl_next
) {
483 rrenum_snd_eachdst(pl
);
492 struct icmp6_router_renum
*rr
;
495 if ((i
= recvmsg(s
, &rcvmhdr
, 0)) < 0) {
496 syslog(LOG_ERR
, "<%s> recvmsg: %s", __func__
,
501 i
-= sizeof(struct ip
);
502 if (i
< sizeof(struct icmp6_router_renum
)) {
503 syslog(LOG_ERR
, "<%s> packet size(%d) is too short",
508 struct ip
*ip
= (struct ip
*)rcvmhdr
.msg_iov
->iov_base
;
510 rr
= (struct icmp6_router_renum
*)(ip
+ 1);
512 rr
= (struct icmp6_router_renum
*)rcvmhdr
.msg_iov
->iov_base
;
514 switch(rr
->rr_code
) {
515 case ICMP6_ROUTER_RENUMBERING_COMMAND
:
516 /* COMMAND will be processed by rtadvd */
518 case ICMP6_ROUTER_RENUMBERING_RESULT
:
519 /* TODO: receiving result message */
522 syslog(LOG_ERR
, "<%s> received unknown code %d",
523 __func__
, rr
->rr_code
);
529 main(int argc
, char *argv
[])
533 struct timeval timeout
;
534 int ch
, i
, maxfd
= 0, send_counter
= 0;
536 struct payload_list
*pl
;
537 #ifdef IPSEC_POLICY_IPSEC
541 memset(&flags
, 0, sizeof(flags
));
542 openlog("rrenumd", LOG_PID
, LOG_DAEMON
);
545 while ((ch
= getopt(argc
, argv
, "c:sdf"
547 #ifdef IPSEC_POLICY_IPSEC
549 #else /* IPSEC_POLICY_IPSEC */
551 #endif /* IPSEC_POLICY_IPSEC */
556 if((fp
= fopen(optarg
, "r")) == NULL
) {
558 "<%s> config file %s open failed",
573 #ifdef IPSEC_POLICY_IPSEC
576 policy
= strdup(optarg
);
578 #else /* IPSEC_POLICY_IPSEC */
585 #endif /* IPSEC_POLICY_IPSEC */
595 if (flags
.debug
== 0)
596 (void)setlogmask(LOG_UPTO(LOG_ERR
));
597 if (flags
.debug
== 1)
598 (void)setlogmask(LOG_UPTO(LOG_INFO
));
600 /* init global variables */
606 #ifdef IPSEC_POLICY_IPSEC
608 #endif /* IPSEC_POLICY_IPSEC */
611 #ifdef IPSEC_POLICY_IPSEC
613 #endif /* IPSEC_POLICY_IPSEC */
631 /* ADHOC: timeout each 30seconds */
632 memset(&timeout
, 0, sizeof(timeout
));
634 /* init temporary payload_list and send_counter*/
636 send_counter
= retry
+ 1;
638 struct fd_set select_fd
= fdset
; /* reinitialize */
640 if ((i
= select(maxfd
+ 1, &select_fd
, NULL
, NULL
,
642 syslog(LOG_ERR
, "<%s> select: %s",
643 __func__
, strerror(errno
));
646 if (i
== 0) { /* timeout */
649 rrenum_snd_eachdst(pl
);
652 if (send_counter
== 0) {
655 send_counter
= retry
+ 1;
658 if (FD_ISSET(s4
, &select_fd
))
660 if (FD_ISSET(s6
, &select_fd
))