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
31 * $FreeBSD: src/usr.sbin/rrenumd/rrenumd.c,v 1.1.2.4 2001/07/09 09:49:49 ume Exp $
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 <netinet6/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);
122 /* Print usage. Don't call this after daemonized. */
126 fprintf(stderr
, "usage: rrenumd [-c conf_file|-s] [-df"
128 #ifdef IPSEC_POLICY_IPSEC
130 #else /* IPSEC_POLICY_IPSEC */
132 #endif /* IPSEC_POLICY_IPSEC */
139 init_sin6(struct sockaddr_in6
*sin6
, const char *addr_ascii
)
141 memset(sin6
, 0, sizeof(*sin6
));
142 sin6
->sin6_len
= sizeof(*sin6
);
143 sin6
->sin6_family
= AF_INET6
;
144 if (inet_pton(AF_INET6
, addr_ascii
, &sin6
->sin6_addr
) != 1)
145 ; /* XXX do something */
148 #if 0 /* XXX: not necessary ?? */
150 join_multi(const char *addrname
)
152 struct ipv6_mreq mreq
;
154 if (inet_pton(AF_INET6
, addrname
, &mreq
.ipv6mr_multiaddr
.s6_addr
)
156 syslog(LOG_ERR
, "<%s> inet_pton failed(library bug?)",
160 /* ADHOC: currently join only one */
162 if ((mreq
.ipv6mr_interface
= if_nametoindex(ifname
)) == 0) {
163 syslog(LOG_ERR
, "<%s> ifname %s should be invalid: %s",
164 __func__
, ifname
, strerror(errno
));
167 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_JOIN_GROUP
,
170 syslog(LOG_ERR
, "<%s> IPV6_JOIN_GROUP on %s: %s",
171 __func__
, ifname
, strerror(errno
));
181 static struct iovec rcviov
;
182 static u_char rprdata
[4500]; /* maximal MTU of connected links */
183 static u_char
*rcvcmsgbuf
= NULL
;
184 static u_char
*sndcmsgbuf
= NULL
;
185 int sndcmsglen
, rcvcmsglen
;
187 /* init ll_allrouters */
188 init_sin6(&sin6_ll_allrouters
, LL_ALLROUTERS
);
190 /* initialize msghdr for receiving packets */
191 rcviov
.iov_base
= (caddr_t
)rprdata
;
192 rcviov
.iov_len
= sizeof(rprdata
);
193 rcvmhdr
.msg_namelen
= sizeof(struct sockaddr_in6
);
194 rcvmhdr
.msg_iov
= &rcviov
;
195 rcvmhdr
.msg_iovlen
= 1;
196 rcvcmsglen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
197 CMSG_SPACE(sizeof(int));
198 if (rcvcmsgbuf
== NULL
&&
199 (rcvcmsgbuf
= (u_char
*)malloc(rcvcmsglen
)) == NULL
) {
200 syslog(LOG_ERR
, "<%s>: malloc failed", __func__
);
203 rcvmhdr
.msg_control
= (caddr_t
)rcvcmsgbuf
;
204 rcvmhdr
.msg_controllen
= rcvcmsglen
;
206 /* initialize msghdr for sending packets */
207 sndmhdr
.msg_namelen
= sizeof(struct sockaddr_in6
);
208 sndmhdr
.msg_iovlen
= 1;
209 sndcmsglen
= CMSG_SPACE(sizeof(struct in6_pktinfo
)) +
210 CMSG_SPACE(sizeof(int));
211 if (sndcmsgbuf
== NULL
&&
212 (sndcmsgbuf
= (u_char
*)malloc(sndcmsglen
)) == NULL
) {
213 syslog(LOG_ERR
, "<%s>: malloc failed", __func__
);
216 sndmhdr
.msg_control
= (caddr_t
)sndcmsgbuf
;
217 sndmhdr
.msg_controllen
= sndcmsglen
;
223 struct payload_list
*pl
;
225 struct icmp6_router_renum
*irr
;
226 struct rr_pco_match
*rpm
;
228 if (parse(fpp
) < 0) {
229 syslog(LOG_ERR
, "<%s> parse failed", __func__
);
233 /* initialize fields not configured by parser */
234 for (pl
= pl_head
; pl
; pl
= pl
->pl_next
) {
235 iov
= (struct iovec
*)&pl
->pl_sndiov
;
236 irr
= (struct icmp6_router_renum
*)&pl
->pl_irr
;
237 rpm
= (struct rr_pco_match
*)&pl
->pl_rpm
;
239 irr
->rr_type
= ICMP6_ROUTER_RENUMBERING
;
242 * now we don't support multiple PCOs in a rr message.
243 * so segment number is not supported.
245 /* TODO: rr flags config in parser */
246 irr
->rr_flags
|= ICMP6_RR_FLAGS_SPECSITE
;
247 /* TODO: max delay config in parser */
250 * means only 1 use_prefix is contained as router-renum-05.txt.
251 * now we don't support multiple PCOs in a rr message,
252 * nor multiple use_prefix in one PCO.
254 rpm
->rpm_len
= 4*1 +3;
255 rpm
->rpm_ordinal
= 0;
256 iov
->iov_base
= (caddr_t
)irr
;
257 iov
->iov_len
= sizeof(struct icmp6_router_renum
)
258 + sizeof(struct rr_pco_match
)
259 + sizeof(struct rr_pco_use
);
264 sock6_open(struct flags
*flags
265 #ifdef IPSEC_POLICY_IPSEC
267 #endif /* IPSEC_POLICY_IPSEC */
270 struct icmp6_filter filt
;
273 #ifndef IPSEC_POLICY_IPSEC
278 if (with_v6dest
== 0)
281 (s6
= socket(AF_INET6
, SOCK_RAW
, IPPROTO_ICMPV6
)) < 0) {
282 syslog(LOG_ERR
, "<%s> socket(v6): %s", __func__
,
288 * join all routers multicast addresses.
290 #if 0 /* XXX: not necessary ?? */
291 join_multi(LL_ALLROUTERS
);
292 join_multi(SL_ALLROUTERS
);
295 /* set icmpv6 filter */
296 ICMP6_FILTER_SETBLOCKALL(&filt
);
297 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING
, &filt
);
298 if (setsockopt(s6
, IPPROTO_ICMPV6
, ICMP6_FILTER
, &filt
,
300 syslog(LOG_ERR
, "<%s> IICMP6_FILTER: %s",
301 __func__
, strerror(errno
));
305 /* specify to tell receiving interface */
307 if (setsockopt(s6
, IPPROTO_IPV6
, IPV6_PKTINFO
, &on
,
309 syslog(LOG_ERR
, "<%s> IPV6_PKTINFO: %s",
310 __func__
, strerror(errno
));
315 #ifdef IPSEC_POLICY_IPSEC
318 buf
= ipsec_set_policy(policy
, strlen(policy
));
320 errx(1, "%s", ipsec_strerror());
321 /* XXX should handle in/out bound policy. */
322 if (setsockopt(s6
, IPPROTO_IPV6
, IPV6_IPSEC_POLICY
,
323 buf
, ipsec_get_policylen(buf
)) < 0)
324 err(1, "setsockopt(IPV6_IPSEC_POLICY)");
327 #else /* IPSEC_POLICY_IPSEC */
329 optval
= IPSEC_LEVEL_REQUIRE
;
330 if (setsockopt(s6
, IPPROTO_IPV6
, IPV6_AUTH_TRANS_LEVEL
,
331 &optval
, sizeof(optval
)) == -1) {
332 syslog(LOG_ERR
, "<%s> IPV6_AUTH_TRANS_LEVEL: %s",
333 __func__
, strerror(errno
));
337 if (flags
->encrypt
) {
338 optval
= IPSEC_LEVEL_REQUIRE
;
339 if (setsockopt(s6
, IPPROTO_IPV6
, IPV6_ESP_TRANS_LEVEL
,
340 &optval
, sizeof(optval
)) == -1) {
341 syslog(LOG_ERR
, "<%s> IPV6_ESP_TRANS_LEVEL: %s",
342 __func__
, strerror(errno
));
346 #endif /* IPSEC_POLICY_IPSEC */
353 sock4_open(struct flags
*flags
354 #ifdef IPSEC_POLICY_IPSEC
356 #endif /* IPSEC_POLICY_IPSEC */
360 #ifndef IPSEC_POLICY_IPSEC
365 if (with_v4dest
== 0)
367 if ((s4
= socket(AF_INET
, SOCK_RAW
, IPPROTO_ICMPV6
)) < 0) {
368 syslog(LOG_ERR
, "<%s> socket(v4): %s", __func__
,
373 #if 0 /* XXX: not necessary ?? */
375 * join all routers multicast addresses.
377 some_join_function();
381 #ifdef IPSEC_POLICY_IPSEC
384 buf
= ipsec_set_policy(policy
, strlen(policy
));
386 errx(1, "%s", ipsec_strerror());
387 /* XXX should handle in/out bound policy. */
388 if (setsockopt(s4
, IPPROTO_IP
, IP_IPSEC_POLICY
,
389 buf
, ipsec_get_policylen(buf
)) < 0)
390 err(1, "setsockopt(IP_IPSEC_POLICY)");
393 #else /* IPSEC_POLICY_IPSEC */
395 optval
= IPSEC_LEVEL_REQUIRE
;
396 if (setsockopt(s4
, IPPROTO_IP
, IP_AUTH_TRANS_LEVEL
,
397 &optval
, sizeof(optval
)) == -1) {
398 syslog(LOG_ERR
, "<%s> IP_AUTH_TRANS_LEVEL: %s",
399 __func__
, strerror(errno
));
403 if (flags
->encrypt
) {
404 optval
= IPSEC_LEVEL_REQUIRE
;
405 if (setsockopt(s4
, IPPROTO_IP
, IP_ESP_TRANS_LEVEL
,
406 &optval
, sizeof(optval
)) == -1) {
407 syslog(LOG_ERR
, "<%s> IP_ESP_TRANS_LEVEL: %s",
408 __func__
, strerror(errno
));
412 #endif /* IPSEC_POLICY_IPSEC */
419 rrenum_output(struct payload_list
*pl
, struct dst_list
*dl
)
423 struct in6_pktinfo
*pi
;
424 struct sockaddr_in6
*sin6
= NULL
;
426 sndmhdr
.msg_name
= (caddr_t
)dl
->dl_dst
;
427 if (dl
->dl_dst
->sa_family
== AF_INET6
)
428 sin6
= (struct sockaddr_in6
*)dl
->dl_dst
;
431 IN6_IS_ADDR_MULTICAST(&sin6
->sin6_addr
)) {
432 int hoplimit
= RR_MCHLIM_DEFAULT
;
434 cm
= CMSG_FIRSTHDR(&sndmhdr
);
435 /* specify the outgoing interface */
436 cm
->cmsg_level
= IPPROTO_IPV6
;
437 cm
->cmsg_type
= IPV6_PKTINFO
;
438 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
439 pi
= (struct in6_pktinfo
*)CMSG_DATA(cm
);
440 memset(&pi
->ipi6_addr
, 0, sizeof(pi
->ipi6_addr
)); /*XXX*/
441 pi
->ipi6_ifindex
= sin6
->sin6_scope_id
;
442 msglen
+= CMSG_LEN(sizeof(struct in6_pktinfo
));
444 /* specify the hop limit of the packet if dest is link local */
445 /* not defined by router-renum-05.txt, but maybe its OK */
446 cm
= CMSG_NXTHDR(&sndmhdr
, cm
);
447 cm
->cmsg_level
= IPPROTO_IPV6
;
448 cm
->cmsg_type
= IPV6_HOPLIMIT
;
449 cm
->cmsg_len
= CMSG_LEN(sizeof(int));
450 memcpy(CMSG_DATA(cm
), &hoplimit
, sizeof(int));
451 msglen
+= CMSG_LEN(sizeof(int));
453 sndmhdr
.msg_controllen
= msglen
;
454 if (sndmhdr
.msg_controllen
== 0)
455 sndmhdr
.msg_control
= 0;
457 sndmhdr
.msg_iov
= &pl
->pl_sndiov
;
458 i
= sendmsg(dl
->dl_dst
->sa_family
== AF_INET
? s4
: s6
, &sndmhdr
, 0);
460 if (i
< 0 || i
!= sndmhdr
.msg_iov
->iov_len
)
461 syslog(LOG_ERR
, "<%s> sendmsg: %s", __func__
,
466 rrenum_snd_eachdst(struct payload_list
*pl
)
470 for (dl
= dl_head
; dl
; dl
= dl
->dl_next
) {
471 rrenum_output(pl
, dl
);
477 rrenum_snd_fullsequence(void)
479 struct payload_list
*pl
;
481 for (pl
= pl_head
; pl
; pl
= pl
->pl_next
) {
482 rrenum_snd_eachdst(pl
);
491 struct icmp6_router_renum
*rr
;
494 if ((i
= recvmsg(s
, &rcvmhdr
, 0)) < 0) {
495 syslog(LOG_ERR
, "<%s> recvmsg: %s", __func__
,
500 i
-= sizeof(struct ip
);
501 if (i
< sizeof(struct icmp6_router_renum
)) {
502 syslog(LOG_ERR
, "<%s> packet size(%d) is too short",
507 struct ip
*ip
= (struct ip
*)rcvmhdr
.msg_iov
->iov_base
;
509 rr
= (struct icmp6_router_renum
*)(ip
+ 1);
511 rr
= (struct icmp6_router_renum
*)rcvmhdr
.msg_iov
->iov_base
;
513 switch(rr
->rr_code
) {
514 case ICMP6_ROUTER_RENUMBERING_COMMAND
:
515 /* COMMAND will be processed by rtadvd */
517 case ICMP6_ROUTER_RENUMBERING_RESULT
:
518 /* TODO: receiving result message */
521 syslog(LOG_ERR
, "<%s> received unknown code %d",
522 __func__
, rr
->rr_code
);
528 main(int argc
, char *argv
[])
532 struct timeval timeout
;
533 int ch
, i
, maxfd
= 0, send_counter
= 0;
535 struct payload_list
*pl
;
536 #ifdef IPSEC_POLICY_IPSEC
540 memset(&flags
, 0, sizeof(flags
));
541 openlog("rrenumd", LOG_PID
, LOG_DAEMON
);
544 while ((ch
= getopt(argc
, argv
, "c:sdf"
546 #ifdef IPSEC_POLICY_IPSEC
548 #else /* IPSEC_POLICY_IPSEC */
550 #endif /* IPSEC_POLICY_IPSEC */
555 if((fp
= fopen(optarg
, "r")) == NULL
) {
557 "<%s> config file %s open failed",
572 #ifdef IPSEC_POLICY_IPSEC
575 policy
= strdup(optarg
);
577 #else /* IPSEC_POLICY_IPSEC */
584 #endif /* IPSEC_POLICY_IPSEC */
594 if (flags
.debug
== 0)
595 setlogmask(LOG_UPTO(LOG_ERR
));
596 if (flags
.debug
== 1)
597 setlogmask(LOG_UPTO(LOG_INFO
));
599 /* init global variables */
605 #ifdef IPSEC_POLICY_IPSEC
607 #endif /* IPSEC_POLICY_IPSEC */
610 #ifdef IPSEC_POLICY_IPSEC
612 #endif /* IPSEC_POLICY_IPSEC */
630 /* ADHOC: timeout each 30seconds */
631 memset(&timeout
, 0, sizeof(timeout
));
633 /* init temporary payload_list and send_counter*/
635 send_counter
= retry
+ 1;
637 struct fd_set select_fd
= fdset
; /* reinitialize */
639 if ((i
= select(maxfd
+ 1, &select_fd
, NULL
, NULL
,
641 syslog(LOG_ERR
, "<%s> select: %s",
642 __func__
, strerror(errno
));
645 if (i
== 0) { /* timeout */
648 rrenum_snd_eachdst(pl
);
651 if (send_counter
== 0) {
654 send_counter
= retry
+ 1;
657 if (FD_ISSET(s4
, &select_fd
))
659 if (FD_ISSET(s6
, &select_fd
))