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>
60 #define LL_ALLROUTERS "ff02::2"
61 #define SL_ALLROUTERS "ff05::2"
63 #define RR_MCHLIM_DEFAULT 64
65 #ifndef IN6_IS_SCOPE_LINKLOCAL
66 #define IN6_IS_SCOPE_LINKLOCAL(a) \
67 ((IN6_IS_ADDR_LINKLOCAL(a)) || \
68 (IN6_IS_ADDR_MC_LINKLOCAL(a)))
69 #endif /* IN6_IS_SCOPE_LINKLOCAL */
76 struct msghdr sndmhdr;
77 struct msghdr rcvmhdr;
78 struct sockaddr_in6 from;
79 struct sockaddr_in6 sin6_ll_allrouters;
82 int with_v4dest, with_v6dest;
83 struct in6_addr prefix; /* ADHOC */
84 int prefixlen = 64; /* ADHOC */
86 extern int parse(FILE **);
88 static void show_usage(void);
89 static void init_sin6(struct sockaddr_in6 *, const char *);
91 static void join_multi(const char *);
93 static void init_globals(void);
94 static void config(FILE **);
95 static void sock6_open(struct flags *);
96 static void sock4_open(struct flags *);
97 static void rrenum_output(struct payload_list *, struct dst_list *);
98 static void rrenum_snd_eachdst(struct payload_list *);
100 static void rrenum_snd_fullsequence(void);
102 static void rrenum_input(int);
105 /* Print usage. Don't call this after daemonized. */
109 fprintf(stderr, "usage: rrenumd [-c conf_file|-s] [-df]\n");
114 init_sin6(struct sockaddr_in6 *sin6, const char *addr_ascii)
116 memset(sin6, 0, sizeof(*sin6));
117 sin6->sin6_len = sizeof(*sin6);
118 sin6->sin6_family = AF_INET6;
119 if (inet_pton(AF_INET6, addr_ascii, &sin6->sin6_addr) != 1)
120 ; /* XXX do something */
123 #if 0 /* XXX: not necessary ?? */
125 join_multi(const char *addrname)
127 struct ipv6_mreq mreq;
129 if (inet_pton(AF_INET6, addrname, &mreq.ipv6mr_multiaddr.s6_addr)
131 syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
135 /* ADHOC: currently join only one */
137 if ((mreq.ipv6mr_interface = if_nametoindex(ifname)) == 0) {
138 syslog(LOG_ERR, "<%s> ifname %s should be invalid: %s",
139 __func__, ifname, strerror(errno));
142 if (setsockopt(s, IPPROTO_IPV6, IPV6_JOIN_GROUP,
145 syslog(LOG_ERR, "<%s> IPV6_JOIN_GROUP on %s: %s",
146 __func__, ifname, strerror(errno));
156 static struct iovec rcviov;
157 static u_char rprdata[4500]; /* maximal MTU of connected links */
158 static u_char *rcvcmsgbuf = NULL;
159 static u_char *sndcmsgbuf = NULL;
160 int sndcmsglen, rcvcmsglen;
162 /* init ll_allrouters */
163 init_sin6(&sin6_ll_allrouters, LL_ALLROUTERS);
165 /* initialize msghdr for receiving packets */
166 rcviov.iov_base = (caddr_t)rprdata;
167 rcviov.iov_len = sizeof(rprdata);
168 rcvmhdr.msg_namelen = sizeof(struct sockaddr_in6);
169 rcvmhdr.msg_iov = &rcviov;
170 rcvmhdr.msg_iovlen = 1;
171 rcvcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
172 CMSG_SPACE(sizeof(int));
173 if (rcvcmsgbuf == NULL &&
174 (rcvcmsgbuf = (u_char *)malloc(rcvcmsglen)) == NULL) {
175 syslog(LOG_ERR, "<%s>: malloc failed", __func__);
178 rcvmhdr.msg_control = (caddr_t)rcvcmsgbuf;
179 rcvmhdr.msg_controllen = rcvcmsglen;
181 /* initialize msghdr for sending packets */
182 sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
183 sndmhdr.msg_iovlen = 1;
184 sndcmsglen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
185 CMSG_SPACE(sizeof(int));
186 if (sndcmsgbuf == NULL &&
187 (sndcmsgbuf = (u_char *)malloc(sndcmsglen)) == NULL) {
188 syslog(LOG_ERR, "<%s>: malloc failed", __func__);
191 sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
192 sndmhdr.msg_controllen = sndcmsglen;
198 struct payload_list *pl;
200 struct icmp6_router_renum *irr;
201 struct rr_pco_match *rpm;
203 if (parse(fpp) < 0) {
204 syslog(LOG_ERR, "<%s> parse failed", __func__);
208 /* initialize fields not configured by parser */
209 for (pl = pl_head; pl; pl = pl->pl_next) {
210 iov = (struct iovec *)&pl->pl_sndiov;
211 irr = (struct icmp6_router_renum *)&pl->pl_irr;
212 rpm = (struct rr_pco_match *)&pl->pl_rpm;
214 irr->rr_type = ICMP6_ROUTER_RENUMBERING;
217 * now we don't support multiple PCOs in a rr message.
218 * so segment number is not supported.
220 /* TODO: rr flags config in parser */
221 irr->rr_flags |= ICMP6_RR_FLAGS_SPECSITE;
222 /* TODO: max delay config in parser */
225 * means only 1 use_prefix is contained as router-renum-05.txt.
226 * now we don't support multiple PCOs in a rr message,
227 * nor multiple use_prefix in one PCO.
229 rpm->rpm_len = 4*1 +3;
230 rpm->rpm_ordinal = 0;
231 iov->iov_base = (caddr_t)irr;
232 iov->iov_len = sizeof(struct icmp6_router_renum)
233 + sizeof(struct rr_pco_match)
234 + sizeof(struct rr_pco_use);
239 sock6_open(struct flags *flags)
241 struct icmp6_filter filt;
244 if (with_v6dest == 0)
247 (s6 = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
248 syslog(LOG_ERR, "<%s> socket(v6): %s", __func__,
254 * join all routers multicast addresses.
256 #if 0 /* XXX: not necessary ?? */
257 join_multi(LL_ALLROUTERS);
258 join_multi(SL_ALLROUTERS);
261 /* set icmpv6 filter */
262 ICMP6_FILTER_SETBLOCKALL(&filt);
263 ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
264 if (setsockopt(s6, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
266 syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
267 __func__, strerror(errno));
271 /* specify to tell receiving interface */
273 if (setsockopt(s6, IPPROTO_IPV6, IPV6_PKTINFO, &on,
275 syslog(LOG_ERR, "<%s> IPV6_PKTINFO: %s",
276 __func__, strerror(errno));
284 sock4_open(struct flags *flags)
286 if (with_v4dest == 0)
288 if ((s4 = socket(AF_INET, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
289 syslog(LOG_ERR, "<%s> socket(v4): %s", __func__,
294 #if 0 /* XXX: not necessary ?? */
296 * join all routers multicast addresses.
298 some_join_function();
305 rrenum_output(struct payload_list *pl, struct dst_list *dl)
309 struct in6_pktinfo *pi;
310 struct sockaddr_in6 *sin6 = NULL;
312 sndmhdr.msg_name = (caddr_t)dl->dl_dst;
313 if (dl->dl_dst->sa_family == AF_INET6)
314 sin6 = (struct sockaddr_in6 *)dl->dl_dst;
317 IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
318 int hoplimit = RR_MCHLIM_DEFAULT;
320 cm = CMSG_FIRSTHDR(&sndmhdr);
321 /* specify the outgoing interface */
322 cm->cmsg_level = IPPROTO_IPV6;
323 cm->cmsg_type = IPV6_PKTINFO;
324 cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
325 pi = (struct in6_pktinfo *)CMSG_DATA(cm);
326 memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr)); /*XXX*/
327 pi->ipi6_ifindex = sin6->sin6_scope_id;
328 msglen += CMSG_LEN(sizeof(struct in6_pktinfo));
330 /* specify the hop limit of the packet if dest is link local */
331 /* not defined by router-renum-05.txt, but maybe its OK */
332 cm = CMSG_NXTHDR(&sndmhdr, cm);
333 cm->cmsg_level = IPPROTO_IPV6;
334 cm->cmsg_type = IPV6_HOPLIMIT;
335 cm->cmsg_len = CMSG_LEN(sizeof(int));
336 memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
337 msglen += CMSG_LEN(sizeof(int));
339 sndmhdr.msg_controllen = msglen;
340 if (sndmhdr.msg_controllen == 0)
341 sndmhdr.msg_control = 0;
343 sndmhdr.msg_iov = &pl->pl_sndiov;
344 i = sendmsg(dl->dl_dst->sa_family == AF_INET ? s4 : s6, &sndmhdr, 0);
346 if (i < 0 || i != sndmhdr.msg_iov->iov_len)
347 syslog(LOG_ERR, "<%s> sendmsg: %s", __func__,
352 rrenum_snd_eachdst(struct payload_list *pl)
356 for (dl = dl_head; dl; dl = dl->dl_next) {
357 rrenum_output(pl, dl);
363 rrenum_snd_fullsequence(void)
365 struct payload_list *pl;
367 for (pl = pl_head; pl; pl = pl->pl_next) {
368 rrenum_snd_eachdst(pl);
377 struct icmp6_router_renum *rr;
380 if ((i = recvmsg(s, &rcvmhdr, 0)) < 0) {
381 syslog(LOG_ERR, "<%s> recvmsg: %s", __func__,
386 i -= sizeof(struct ip);
387 if (i < sizeof(struct icmp6_router_renum)) {
388 syslog(LOG_ERR, "<%s> packet size(%d) is too short",
393 struct ip *ip = (struct ip *)rcvmhdr.msg_iov->iov_base;
395 rr = (struct icmp6_router_renum *)(ip + 1);
397 rr = (struct icmp6_router_renum *)rcvmhdr.msg_iov->iov_base;
399 switch(rr->rr_code) {
400 case ICMP6_ROUTER_RENUMBERING_COMMAND:
401 /* COMMAND will be processed by rtadvd */
403 case ICMP6_ROUTER_RENUMBERING_RESULT:
404 /* TODO: receiving result message */
407 syslog(LOG_ERR, "<%s> received unknown code %d",
408 __func__, rr->rr_code);
414 main(int argc, char *argv[])
418 struct timeval timeout;
419 int ch, i, maxfd = 0, send_counter = 0;
421 struct payload_list *pl;
423 memset(&flags, 0, sizeof(flags));
424 openlog("rrenumd", LOG_PID, LOG_DAEMON);
427 while ((ch = getopt(argc, argv, "c:sdf")) != -1) {
430 if((fp = fopen(optarg, "r")) == NULL) {
432 "<%s> config file %s open failed",
454 if (flags.debug == 0)
455 setlogmask(LOG_UPTO(LOG_ERR));
456 if (flags.debug == 1)
457 setlogmask(LOG_UPTO(LOG_INFO));
459 /* init global variables */
482 /* ADHOC: timeout each 30seconds */
483 memset(&timeout, 0, sizeof(timeout));
485 /* init temporary payload_list and send_counter*/
487 send_counter = retry + 1;
489 struct fd_set select_fd = fdset; /* reinitialize */
491 if ((i = select(maxfd + 1, &select_fd, NULL, NULL,
493 syslog(LOG_ERR, "<%s> select: %s",
494 __func__, strerror(errno));
497 if (i == 0) { /* timeout */
500 rrenum_snd_eachdst(pl);
503 if (send_counter == 0) {
506 send_counter = retry + 1;
509 if (FD_ISSET(s4, &select_fd))
511 if (FD_ISSET(s6, &select_fd))