2 /* $KAME: rrenum.c,v 1.12 2002/06/10 19:59:47 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
39 #include <net/if_var.h>
40 #include <net/route.h>
41 #include <netinet/in.h>
42 #include <netinet/in_var.h>
43 #include <netinet/icmp6.h>
45 #include <arpa/inet.h>
56 #define RR_ISSET_SEGNUM(segnum_bits, segnum) \
57 ((((segnum_bits)[(segnum) >> 5]) & (1 << ((segnum) & 31))) != 0)
58 #define RR_SET_SEGNUM(segnum_bits, segnum) \
59 (((segnum_bits)[(segnum) >> 5]) |= (1 << ((segnum) & 31)))
63 u_long rro_segnum_bits[8];
66 static struct rr_operation rro;
67 static int rr_rcvifindex;
68 static int rrcmd2pco[RPM_PCO_MAX] = {
77 * Check validity of a Prefix Control Operation(PCO).
78 * return 0 on success, 1 on failure.
81 rr_pco_check(int len, struct rr_pco_match *rpm)
83 struct rr_pco_use *rpu, *rpulim;
86 /* rpm->rpm_len must be (4N * 3) as router-renum-05.txt */
87 if ((rpm->rpm_len - 3) < 0 || /* must be at least 3 */
88 (rpm->rpm_len - 3) & 0x3) { /* must be multiple of 4 */
89 syslog(LOG_WARNING, "<%s> rpm_len %d is not 4N * 3",
90 __func__, rpm->rpm_len);
93 /* rpm->rpm_code must be valid value */
94 switch (rpm->rpm_code) {
97 case RPM_PCO_SETGLOBAL:
100 syslog(LOG_WARNING, "<%s> unknown rpm_code %d", __func__,
104 /* rpm->rpm_matchlen must be 0 to 128 inclusive */
105 if (rpm->rpm_matchlen > 128) {
106 syslog(LOG_WARNING, "<%s> rpm_matchlen %d is over 128",
107 __func__, rpm->rpm_matchlen);
112 * rpu->rpu_uselen, rpu->rpu_keeplen, and sum of them must be
113 * between 0 and 128 inclusive
115 for (rpu = (struct rr_pco_use *)(rpm + 1),
116 rpulim = (struct rr_pco_use *)((char *)rpm + len);
119 checklen = rpu->rpu_uselen;
120 checklen += rpu->rpu_keeplen;
122 * omit these check, because either of rpu_uselen
123 * and rpu_keeplen is unsigned char
124 * (128 > rpu_uselen > 0)
125 * (128 > rpu_keeplen > 0)
126 * (rpu_uselen + rpu_keeplen > 0)
128 if (checklen > 128) {
129 syslog(LOG_WARNING, "<%s> sum of rpu_uselen %d and"
130 " rpu_keeplen %d is %d(over 128)",
131 __func__, rpu->rpu_uselen, rpu->rpu_keeplen,
132 rpu->rpu_uselen + rpu->rpu_keeplen);
140 do_use_prefix(int len, struct rr_pco_match *rpm,
141 struct in6_rrenumreq *irr, int ifindex)
143 struct rr_pco_use *rpu, *rpulim;
147 rpu = (struct rr_pco_use *)(rpm + 1);
148 rpulim = (struct rr_pco_use *)((char *)rpm + len);
150 if (rpu == rpulim) { /* no use prefix */
151 if (rpm->rpm_code == RPM_PCO_ADD)
154 irr->irr_u_uselen = 0;
155 irr->irr_u_keeplen = 0;
156 irr->irr_raf_mask_onlink = 0;
157 irr->irr_raf_mask_auto = 0;
160 memset(&irr->irr_flags, 0, sizeof(irr->irr_flags));
161 irr->irr_useprefix.sin6_len = 0; /* let it mean, no addition */
162 irr->irr_useprefix.sin6_family = 0;
163 irr->irr_useprefix.sin6_addr = in6addr_any;
164 if (ioctl(s, rrcmd2pco[rpm->rpm_code], (caddr_t)irr) < 0 &&
165 errno != EADDRNOTAVAIL)
166 syslog(LOG_ERR, "<%s> ioctl: %s", __func__,
171 for (rpu = (struct rr_pco_use *)(rpm + 1),
172 rpulim = (struct rr_pco_use *)((char *)rpm + len);
175 /* init in6_rrenumreq fields */
176 irr->irr_u_uselen = rpu->rpu_uselen;
177 irr->irr_u_keeplen = rpu->rpu_keeplen;
178 irr->irr_raf_mask_onlink =
179 !!(rpu->rpu_ramask & ICMP6_RR_PCOUSE_RAFLAGS_ONLINK);
180 irr->irr_raf_mask_auto =
181 !!(rpu->rpu_ramask & ICMP6_RR_PCOUSE_RAFLAGS_AUTO);
182 irr->irr_vltime = ntohl(rpu->rpu_vltime);
183 irr->irr_pltime = ntohl(rpu->rpu_pltime);
184 irr->irr_raf_onlink =
185 (rpu->rpu_raflags & ICMP6_RR_PCOUSE_RAFLAGS_ONLINK) == 0 ?
188 (rpu->rpu_raflags & ICMP6_RR_PCOUSE_RAFLAGS_AUTO) == 0 ?
190 irr->irr_rrf_decrvalid =
191 (rpu->rpu_flags & ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME) == 0 ?
193 irr->irr_rrf_decrprefd =
194 (rpu->rpu_flags & ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME) == 0 ?
196 irr->irr_useprefix.sin6_len = sizeof(irr->irr_useprefix);
197 irr->irr_useprefix.sin6_family = AF_INET6;
198 irr->irr_useprefix.sin6_addr = rpu->rpu_prefix;
200 if (ioctl(s, rrcmd2pco[rpm->rpm_code], (caddr_t)irr) < 0 &&
201 errno != EADDRNOTAVAIL)
202 syslog(LOG_ERR, "<%s> ioctl: %s", __func__,
205 /* very adhoc: should be rewritten */
206 if (rpm->rpm_code == RPM_PCO_CHANGE &&
207 IN6_ARE_ADDR_EQUAL(&rpm->rpm_prefix, &rpu->rpu_prefix) &&
208 rpm->rpm_matchlen == rpu->rpu_uselen &&
209 rpu->rpu_uselen == rpu->rpu_keeplen) {
210 if ((rai = if_indextorainfo(ifindex)) == NULL)
211 continue; /* non-advertising IF */
213 TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
216 if (prefix_match(&pfx->pfx_prefix,
217 pfx->pfx_prefixlen, &rpm->rpm_prefix,
218 rpm->rpm_matchlen)) {
219 /* change parameters */
220 pfx->pfx_validlifetime =
221 ntohl(rpu->rpu_vltime);
222 pfx->pfx_preflifetime =
223 ntohl(rpu->rpu_pltime);
224 if (irr->irr_rrf_decrvalid) {
225 gettimeofday(&now, 0);
226 pfx->pfx_vltimeexpire =
228 pfx->pfx_validlifetime;
230 pfx->pfx_vltimeexpire = 0;
231 if (irr->irr_rrf_decrprefd) {
232 gettimeofday(&now, 0);
233 pfx->pfx_pltimeexpire =
235 pfx->pfx_preflifetime;
237 pfx->pfx_pltimeexpire = 0;
245 * process a Prefix Control Operation(PCO).
246 * return 0 on success, 1 on failure
249 do_pco(struct icmp6_router_renum *rr, int len, struct rr_pco_match *rpm)
252 struct in6_rrenumreq irr;
254 if ((rr_pco_check(len, rpm) != 0))
257 if (s == -1 && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
258 syslog(LOG_ERR, "<%s> socket: %s", __func__,
263 memset(&irr, 0, sizeof(irr));
264 irr.irr_origin = PR_ORIG_RR;
265 irr.irr_m_len = rpm->rpm_matchlen;
266 irr.irr_m_minlen = rpm->rpm_minlen;
267 irr.irr_m_maxlen = rpm->rpm_maxlen;
268 irr.irr_matchprefix.sin6_len = sizeof(irr.irr_matchprefix);
269 irr.irr_matchprefix.sin6_family = AF_INET6;
270 irr.irr_matchprefix.sin6_addr = rpm->rpm_prefix;
272 while (if_indextoname(++ifindex, irr.irr_name)) {
274 * if ICMP6_RR_FLAGS_FORCEAPPLY(A flag) is 0 and
275 * IFF_UP is off, the interface is not applied
277 if ((rr->rr_flags & ICMP6_RR_FLAGS_FORCEAPPLY) == 0 &&
278 (iflist[ifindex]->ifm_flags & IFF_UP) == 0)
280 /* TODO: interface scope check */
281 do_use_prefix(len, rpm, &irr, ifindex);
286 syslog(LOG_ERR, "<%s> if_indextoname: %s", __func__,
294 * call do_pco() for each Prefix Control Operations(PCOs) in a received
295 * Router Renumbering Command packet.
296 * return 0 on success, 1 on failure
299 do_rr(int len, struct icmp6_router_renum *rr)
301 struct rr_pco_match *rpm;
304 lim = (char *)rr + len;
305 cp = (char *)(rr + 1);
306 len -= sizeof(struct icmp6_router_renum);
308 /* get iflist block from kernel again, to get up-to-date information */
314 rpm = (struct rr_pco_match *)cp;
315 if ((size_t)len < sizeof(struct rr_pco_match)) {
317 syslog(LOG_ERR, "<%s> pkt too short. left len = %d. "
318 "gabage at end of pkt?", __func__, len);
321 rpmlen = rpm->rpm_len << 3;
325 if (do_pco(rr, rpmlen, rpm)) {
326 syslog(LOG_WARNING, "<%s> invalid PCO", __func__);
339 * check validity of a router renumbering command packet
340 * return 0 on success, 1 on failure
343 rr_command_check(int len, struct icmp6_router_renum *rr, struct in6_addr *from,
344 struct in6_addr *dst)
346 u_char ntopbuf[INET6_ADDRSTRLEN];
348 /* omit rr minimal length check. hope kernel have done it. */
349 /* rr_command length check */
350 if ((size_t)len < (sizeof(struct icmp6_router_renum) +
351 sizeof(struct rr_pco_match))) {
352 syslog(LOG_ERR, "<%s> rr_command len %d is too short",
357 /* destination check. only for multicast. omit unicast check. */
358 if (IN6_IS_ADDR_MULTICAST(dst) && !IN6_IS_ADDR_MC_LINKLOCAL(dst) &&
359 !IN6_IS_ADDR_MC_SITELOCAL(dst)) {
360 syslog(LOG_ERR, "<%s> dst mcast addr %s is illegal",
362 inet_ntop(AF_INET6, dst, ntopbuf, sizeof(ntopbuf)));
366 /* seqnum and segnum check */
367 if (rro.rro_seqnum > rr->rr_seqnum) {
369 "<%s> rcvd old seqnum %d from %s",
370 __func__, (u_int32_t)ntohl(rr->rr_seqnum),
371 inet_ntop(AF_INET6, from, ntopbuf, sizeof(ntopbuf)));
374 if (rro.rro_seqnum == rr->rr_seqnum &&
375 (rr->rr_flags & ICMP6_RR_FLAGS_TEST) == 0 &&
376 RR_ISSET_SEGNUM(rro.rro_segnum_bits, rr->rr_segnum)) {
377 if ((rr->rr_flags & ICMP6_RR_FLAGS_REQRESULT) != 0)
379 "<%s> rcvd duped segnum %d from %s",
380 __func__, rr->rr_segnum, inet_ntop(AF_INET6, from,
381 ntopbuf, sizeof(ntopbuf)));
386 if (rro.rro_seqnum != rr->rr_seqnum) {
387 /* then must be "<" */
389 /* init rro_segnum_bits */
390 memset(rro.rro_segnum_bits, 0,
391 sizeof(rro.rro_segnum_bits));
393 rro.rro_seqnum = rr->rr_seqnum;
399 rr_command_input(int len, struct icmp6_router_renum *rr,
400 struct in6_addr *from, struct in6_addr *dst)
402 /* rr_command validity check */
403 if (rr_command_check(len, rr, from, dst))
405 if ((rr->rr_flags & (ICMP6_RR_FLAGS_TEST|ICMP6_RR_FLAGS_REQRESULT)) ==
409 /* do router renumbering */
414 RR_SET_SEGNUM(rro.rro_segnum_bits, rr->rr_segnum);
419 syslog(LOG_ERR, "<%s> received RR was invalid", __func__);
424 rr_input(int len, struct icmp6_router_renum *rr, struct in6_pktinfo *pi,
425 struct sockaddr_in6 *from, struct in6_addr *dst)
427 u_char ntopbuf[2][INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
430 "<%s> RR received from %s to %s on %s",
432 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf[0] ,sizeof(ntopbuf[0])),
433 inet_ntop(AF_INET6, &dst, ntopbuf[1], sizeof(ntopbuf[1])),
434 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
436 /* packet validation based on Section 4.1 of RFC2894 */
437 if ((size_t)len < sizeof(struct icmp6_router_renum)) {
439 "<%s>: RR short message (size %d) from %s to %s on %s",
441 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf[0],
443 inet_ntop(AF_INET6, &dst, ntopbuf[1], sizeof(ntopbuf[1])),
444 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
449 * If the IPv6 destination address is neither an All Routers multicast
450 * address [AARCH] nor one of the receiving router's unicast addresses,
451 * the message MUST be discarded and SHOULD be logged to network
453 * We rely on the kernel input routine for unicast addresses, and thus
454 * check multicast destinations only.
456 if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && !IN6_ARE_ADDR_EQUAL(
457 &sin6_sitelocal_allrouters.sin6_addr, &pi->ipi6_addr)) {
459 "<%s>: RR message with invalid destination (%s) "
462 inet_ntop(AF_INET6, &dst, ntopbuf[0], sizeof(ntopbuf[0])),
463 inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf[1],
465 if_indextoname(pi->ipi6_ifindex, ifnamebuf));
469 rr_rcvifindex = pi->ipi6_ifindex;
471 switch (rr->rr_code) {
472 case ICMP6_ROUTER_RENUMBERING_COMMAND:
473 rr_command_input(len, rr, &from->sin6_addr, dst);
474 /* TODO: send reply msg */
476 case ICMP6_ROUTER_RENUMBERING_RESULT:
477 /* RESULT will be processed by rrenumd */
479 case ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET:
480 /* TODO: sequence number reset */
483 syslog(LOG_ERR, "<%s> received unknown code %d",
484 __func__, rr->rr_code);