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 $
32 * $DragonFly: src/usr.sbin/rrenumd/rrenumd.c,v 1.5 2005/02/17 14:00:10 joerg Exp $
35 #include <sys/param.h>
36 #include <sys/socket.h>
42 #include <net/route.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/in.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip6.h>
48 #include <netinet/icmp6.h>
50 #include <arpa/inet.h>
53 #include <netinet6/ipsec.h>
65 #define LL_ALLROUTERS "ff02::2"
66 #define SL_ALLROUTERS "ff05::2"
68 #define RR_MCHLIM_DEFAULT 64
70 #ifndef IN6_IS_SCOPE_LINKLOCAL
71 #define IN6_IS_SCOPE_LINKLOCAL(a) \
72 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
73 (IN6_IS_ADDR_MC_LINKLOCAL(a)))
74 #endif /* IN6_IS_SCOPE_LINKLOCAL */
80 #ifdef IPSEC_POLICY_IPSEC
82 #else /* IPSEC_POLICY_IPSEC */
85 #endif /* IPSEC_POLICY_IPSEC */
89 struct msghdr sndmhdr;
90 struct msghdr rcvmhdr;
91 struct sockaddr_in6 from;
92 struct sockaddr_in6 sin6_ll_allrouters;
95 int with_v4dest, with_v6dest;
96 struct in6_addr prefix; /* ADHOC */
97 int prefixlen = 64; /* ADHOC */
99 extern int parse(FILE **);
101 static void show_usage(void);
102 static void init_sin6(struct sockaddr_in6 *, const char *);
104 static void join_multi(const char *);
106 static void init_globals(void);
107 static void config(FILE **);
108 #ifdef IPSEC_POLICY_IPSEC
109 static void sock6_open(struct flags *, char *);
110 static void sock4_open(struct flags *, char *);
112 static void sock6_open(struct flags *);
113 static void sock4_open(struct flags *);
115 static void rrenum_output(struct payload_list *, struct dst_list *);
116 static void rrenum_snd_eachdst(struct payload_list *);
118 static void rrenum_snd_fullsequence(void);
120 static void rrenum_input(int);
121 int main(int, char *[]);
124 /* Print usage. Don't call this after daemonized. */
128 fprintf(stderr, "usage: rrenumd [-c conf_file|-s] [-df"
130 #ifdef IPSEC_POLICY_IPSEC
132 #else /* IPSEC_POLICY_IPSEC */
134 #endif /* IPSEC_POLICY_IPSEC */
141 init_sin6(struct sockaddr_in6 *sin6, const char *addr_ascii)
143 memset(sin6, 0, sizeof(*sin6));
144 sin6->sin6_len = sizeof(*sin6);
145 sin6->sin6_family = AF_INET6;
146 if (inet_pton(AF_INET6, addr_ascii, &sin6->sin6_addr) != 1)
147 ; /* XXX do something */
150 #if 0 /* XXX: not necessary ?? */
152 join_multi(const char *addrname)
154 struct ipv6_mreq mreq;
156 if (inet_pton(AF_INET6, addrname, &mreq.ipv6mr_multiaddr.s6_addr)
158 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
162 /* ADHOC: currently join only one */
164 if ((mreq.ipv6mr_interface = if_nametoindex(ifname)) == 0) {
165 syslog(LOG_ERR, "<%s> ifname %s should be invalid: %s",
166 __func__, ifname, strerror(errno));
169 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP,
172 syslog(LOG_ERR, "<%s> IPV6_JOIN_GROUP on %s: %s",
173 __func__, ifname, strerror(errno));
183 static struct iovec rcviov;
184 static u_char rprdata[4500]; /* maximal MTU of connected links */
185 static u_char *rcvcmsgbuf = NULL;
186 static u_char *sndcmsgbuf = NULL;
187 int sndcmsglen, rcvcmsglen;
189 /* init ll_allrouters */
190 init_sin6(&sin6_ll_allrouters, LL_ALLROUTERS);
192 /* initialize msghdr for receiving packets */
193 rcviov.iov_base = (caddr_t)rprdata;
194 rcviov.iov_len = sizeof(rprdata);
195 rcvmhdr.msg_namelen = sizeof(struct sockaddr_in6);
196 rcvmhdr.msg_iov = &rcviov;
197 rcvmhdr.msg_iovlen = 1;
198 rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
199 CMSG_SPACE(sizeof(int));
200 if (rcvcmsgbuf == NULL &&
201 (rcvcmsgbuf = (u_char *)malloc(rcvcmsglen)) == NULL) {
202 syslog(LOG_ERR, "<%s>: malloc failed", __func__);
205 rcvmhdr.msg_control = (caddr_t)rcvcmsgbuf;
206 rcvmhdr.msg_controllen = rcvcmsglen;
208 /* initialize msghdr for sending packets */
209 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
210 sndmhdr.msg_iovlen = 1;
211 sndcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
212 CMSG_SPACE(sizeof(int));
213 if (sndcmsgbuf == NULL &&
214 (sndcmsgbuf = (u_char *)malloc(sndcmsglen)) == NULL) {
215 syslog(LOG_ERR, "<%s>: malloc failed", __func__);
218 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
219 sndmhdr.msg_controllen = sndcmsglen;
225 struct payload_list *pl;
227 struct icmp6_router_renum *irr;
228 struct rr_pco_match *rpm;
230 if (parse(fpp) < 0) {
231 syslog(LOG_ERR, "<%s> parse failed", __func__);
235 /* initialize fields not configured by parser */
236 for (pl = pl_head; pl; pl = pl->pl_next) {
237 iov = (struct iovec *)&pl->pl_sndiov;
238 irr = (struct icmp6_router_renum *)&pl->pl_irr;
239 rpm = (struct rr_pco_match *)&pl->pl_rpm;
241 irr->rr_type = ICMP6_ROUTER_RENUMBERING;
244 * now we don't support multiple PCOs in a rr message.
245 * so segment number is not supported.
247 /* TODO: rr flags config in parser */
248 irr->rr_flags |= ICMP6_RR_FLAGS_SPECSITE;
249 /* TODO: max delay config in parser */
252 * means only 1 use_prefix is contained as router-renum-05.txt.
253 * now we don't support multiple PCOs in a rr message,
254 * nor multiple use_prefix in one PCO.
256 rpm->rpm_len = 4*1 +3;
257 rpm->rpm_ordinal = 0;
258 iov->iov_base = (caddr_t)irr;
259 iov->iov_len = sizeof(struct icmp6_router_renum)
260 + sizeof(struct rr_pco_match)
261 + sizeof(struct rr_pco_use);
266 sock6_open(struct flags *flags
267 #ifdef IPSEC_POLICY_IPSEC
269 #endif /* IPSEC_POLICY_IPSEC */
272 struct icmp6_filter filt;
275 #ifndef IPSEC_POLICY_IPSEC
280 if (with_v6dest == 0)
283 (s6 = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
284 syslog(LOG_ERR, "<%s> socket(v6): %s", __func__,
290 * join all routers multicast addresses.
292 #if 0 /* XXX: not necessary ?? */
293 join_multi(LL_ALLROUTERS);
294 join_multi(SL_ALLROUTERS);
297 /* set icmpv6 filter */
298 ICMP6_FILTER_SETBLOCKALL(&filt);
299 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
300 if (setsockopt(s6, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
302 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
303 __func__, strerror(errno));
307 /* specify to tell receiving interface */
309 if (setsockopt(s6, IPPROTO_IPV6, IPV6_PKTINFO, &on,
311 syslog(LOG_ERR, "<%s> IPV6_PKTINFO: %s",
312 __func__, strerror(errno));
317 #ifdef IPSEC_POLICY_IPSEC
320 buf = ipsec_set_policy(policy, strlen(policy));
322 errx(1, "%s", ipsec_strerror());
323 /* XXX should handle in/out bound policy. */
324 if (setsockopt(s6, IPPROTO_IPV6, IPV6_IPSEC_POLICY,
325 buf, ipsec_get_policylen(buf)) < 0)
326 err(1, "setsockopt(IPV6_IPSEC_POLICY)");
329 #else /* IPSEC_POLICY_IPSEC */
331 optval = IPSEC_LEVEL_REQUIRE;
332 if (setsockopt(s6, IPPROTO_IPV6, IPV6_AUTH_TRANS_LEVEL,
333 &optval, sizeof(optval)) == -1) {
334 syslog(LOG_ERR, "<%s> IPV6_AUTH_TRANS_LEVEL: %s",
335 __func__, strerror(errno));
339 if (flags->encrypt) {
340 optval = IPSEC_LEVEL_REQUIRE;
341 if (setsockopt(s6, IPPROTO_IPV6, IPV6_ESP_TRANS_LEVEL,
342 &optval, sizeof(optval)) == -1) {
343 syslog(LOG_ERR, "<%s> IPV6_ESP_TRANS_LEVEL: %s",
344 __func__, strerror(errno));
348 #endif /* IPSEC_POLICY_IPSEC */
355 sock4_open(struct flags *flags
356 #ifdef IPSEC_POLICY_IPSEC
358 #endif /* IPSEC_POLICY_IPSEC */
362 #ifndef IPSEC_POLICY_IPSEC
367 if (with_v4dest == 0)
369 if ((s4 = socket(AF_INET, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
370 syslog(LOG_ERR, "<%s> socket(v4): %s", __func__,
375 #if 0 /* XXX: not necessary ?? */
377 * join all routers multicast addresses.
379 some_join_function();
383 #ifdef IPSEC_POLICY_IPSEC
386 buf = ipsec_set_policy(policy, strlen(policy));
388 errx(1, "%s", ipsec_strerror());
389 /* XXX should handle in/out bound policy. */
390 if (setsockopt(s4, IPPROTO_IP, IP_IPSEC_POLICY,
391 buf, ipsec_get_policylen(buf)) < 0)
392 err(1, "setsockopt(IP_IPSEC_POLICY)");
395 #else /* IPSEC_POLICY_IPSEC */
397 optval = IPSEC_LEVEL_REQUIRE;
398 if (setsockopt(s4, IPPROTO_IP, IP_AUTH_TRANS_LEVEL,
399 &optval, sizeof(optval)) == -1) {
400 syslog(LOG_ERR, "<%s> IP_AUTH_TRANS_LEVEL: %s",
401 __func__, strerror(errno));
405 if (flags->encrypt) {
406 optval = IPSEC_LEVEL_REQUIRE;
407 if (setsockopt(s4, IPPROTO_IP, IP_ESP_TRANS_LEVEL,
408 &optval, sizeof(optval)) == -1) {
409 syslog(LOG_ERR, "<%s> IP_ESP_TRANS_LEVEL: %s",
410 __func__, strerror(errno));
414 #endif /* IPSEC_POLICY_IPSEC */
421 rrenum_output(struct payload_list *pl, struct dst_list *dl)
425 struct in6_pktinfo *pi;
426 struct sockaddr_in6 *sin6 = NULL;
428 sndmhdr.msg_name = (caddr_t)dl->dl_dst;
429 if (dl->dl_dst->sa_family == AF_INET6)
430 sin6 = (struct sockaddr_in6 *)dl->dl_dst;
433 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
434 int hoplimit = RR_MCHLIM_DEFAULT;
436 cm = CMSG_FIRSTHDR(&sndmhdr);
437 /* specify the outgoing interface */
438 cm->cmsg_level = IPPROTO_IPV6;
439 cm->cmsg_type = IPV6_PKTINFO;
440 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
441 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
442 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
443 pi->ipi6_ifindex = sin6->sin6_scope_id;
444 msglen += CMSG_LEN(sizeof(struct in6_pktinfo));
446 /* specify the hop limit of the packet if dest is link local */
447 /* not defined by router-renum-05.txt, but maybe its OK */
448 cm = CMSG_NXTHDR(&sndmhdr, cm);
449 cm->cmsg_level = IPPROTO_IPV6;
450 cm->cmsg_type = IPV6_HOPLIMIT;
451 cm->cmsg_len = CMSG_LEN(sizeof(int));
452 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
453 msglen += CMSG_LEN(sizeof(int));
455 sndmhdr.msg_controllen = msglen;
456 if (sndmhdr.msg_controllen == 0)
457 sndmhdr.msg_control = 0;
459 sndmhdr.msg_iov = &pl->pl_sndiov;
460 i = sendmsg(dl->dl_dst->sa_family == AF_INET ? s4 : s6, &sndmhdr, 0);
462 if (i < 0 || i != sndmhdr.msg_iov->iov_len)
463 syslog(LOG_ERR, "<%s> sendmsg: %s", __func__,
468 rrenum_snd_eachdst(struct payload_list *pl)
472 for (dl = dl_head; dl; dl = dl->dl_next) {
473 rrenum_output(pl, dl);
479 rrenum_snd_fullsequence()
481 struct payload_list *pl;
483 for (pl = pl_head; pl; pl = pl->pl_next) {
484 rrenum_snd_eachdst(pl);
493 struct icmp6_router_renum *rr;
496 if ((i = recvmsg(s, &rcvmhdr, 0)) < 0) {
497 syslog(LOG_ERR, "<%s> recvmsg: %s", __func__,
502 i -= sizeof(struct ip);
503 if (i < sizeof(struct icmp6_router_renum)) {
504 syslog(LOG_ERR, "<%s> packet size(%d) is too short",
509 struct ip *ip = (struct ip *)rcvmhdr.msg_iov->iov_base;
511 rr = (struct icmp6_router_renum *)(ip + 1);
513 rr = (struct icmp6_router_renum *)rcvmhdr.msg_iov->iov_base;
515 switch(rr->rr_code) {
516 case ICMP6_ROUTER_RENUMBERING_COMMAND:
517 /* COMMAND will be processed by rtadvd */
519 case ICMP6_ROUTER_RENUMBERING_RESULT:
520 /* TODO: receiving result message */
523 syslog(LOG_ERR, "<%s> received unknown code %d",
524 __func__, rr->rr_code);
530 main(int argc, char *argv[])
534 struct timeval timeout;
535 int ch, i, maxfd = 0, send_counter = 0;
537 struct payload_list *pl;
538 #ifdef IPSEC_POLICY_IPSEC
542 memset(&flags, 0, sizeof(flags));
543 openlog("rrenumd", LOG_PID, LOG_DAEMON);
546 while ((ch = getopt(argc, argv, "c:sdf"
548 #ifdef IPSEC_POLICY_IPSEC
550 #else /* IPSEC_POLICY_IPSEC */
552 #endif /* IPSEC_POLICY_IPSEC */
557 if((fp = fopen(optarg, "r")) == NULL) {
559 "<%s> config file %s open failed",
574 #ifdef IPSEC_POLICY_IPSEC
577 policy = strdup(optarg);
579 #else /* IPSEC_POLICY_IPSEC */
586 #endif /* IPSEC_POLICY_IPSEC */
596 if (flags.debug == 0)
597 setlogmask(LOG_UPTO(LOG_ERR));
598 if (flags.debug == 1)
599 setlogmask(LOG_UPTO(LOG_INFO));
601 /* init global variables */
607 #ifdef IPSEC_POLICY_IPSEC
609 #endif /* IPSEC_POLICY_IPSEC */
612 #ifdef IPSEC_POLICY_IPSEC
614 #endif /* IPSEC_POLICY_IPSEC */
632 /* ADHOC: timeout each 30seconds */
633 memset(&timeout, 0, sizeof(timeout));
635 /* init temporary payload_list and send_counter*/
637 send_counter = retry + 1;
639 struct fd_set select_fd = fdset; /* reinitialize */
641 if ((i = select(maxfd + 1, &select_fd, NULL, NULL,
643 syslog(LOG_ERR, "<%s> select: %s",
644 __func__, strerror(errno));
647 if (i == 0) { /* timeout */
650 rrenum_snd_eachdst(pl);
653 if (send_counter == 0) {
656 send_counter = retry + 1;
659 if (FD_ISSET(s4, &select_fd))
661 if (FD_ISSET(s6, &select_fd))