1 /* $KAME: rrenum.c,v 1.12 2002/06/10 19:59:47 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 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
38 #include <net/if_var.h>
39 #include <net/route.h>
40 #include <netinet/in.h>
41 #include <netinet/in_var.h>
42 #include <netinet/icmp6.h>
44 #include <arpa/inet.h>
54 #define RR_ISSET_SEGNUM(segnum_bits, segnum) \
55 ((((segnum_bits)[(segnum) >> 5]) & (1 << ((segnum) & 31))) != 0)
56 #define RR_SET_SEGNUM(segnum_bits, segnum) \
57 (((segnum_bits)[(segnum) >> 5]) |= (1 << ((segnum) & 31)))
61 u_long rro_segnum_bits
[8];
64 static struct rr_operation rro
;
65 static int rr_rcvifindex
;
66 static int rrcmd2pco
[RPM_PCO_MAX
] = {
75 * Check validity of a Prefix Control Operation(PCO).
76 * Return 0 on success, 1 on failure.
79 rr_pco_check(int len
, struct rr_pco_match
*rpm
)
81 struct rr_pco_use
*rpu
, *rpulim
;
84 /* rpm->rpm_len must be (4N * 3) as router-renum-05.txt */
85 if ((rpm
->rpm_len
- 3) < 0 || /* must be at least 3 */
86 (rpm
->rpm_len
- 3) & 0x3) { /* must be multiple of 4 */
87 syslog(LOG_WARNING
, "<%s> rpm_len %d is not 4N * 3",
88 __func__
, rpm
->rpm_len
);
91 /* rpm->rpm_code must be valid value */
92 switch (rpm
->rpm_code
) {
95 case RPM_PCO_SETGLOBAL
:
98 syslog(LOG_WARNING
, "<%s> unknown rpm_code %d", __func__
,
102 /* rpm->rpm_matchlen must be 0 to 128 inclusive */
103 if (rpm
->rpm_matchlen
> 128) {
104 syslog(LOG_WARNING
, "<%s> rpm_matchlen %d is over 128",
105 __func__
, rpm
->rpm_matchlen
);
110 * rpu->rpu_uselen, rpu->rpu_keeplen, and sum of them must be
111 * between 0 and 128 inclusive
113 for (rpu
= (struct rr_pco_use
*)(rpm
+ 1),
114 rpulim
= (struct rr_pco_use
*)((char *)rpm
+ len
);
117 checklen
= rpu
->rpu_uselen
;
118 checklen
+= rpu
->rpu_keeplen
;
120 * omit these check, because either of rpu_uselen
121 * and rpu_keeplen is unsigned char
122 * (128 > rpu_uselen > 0)
123 * (128 > rpu_keeplen > 0)
124 * (rpu_uselen + rpu_keeplen > 0)
126 if (checklen
> 128) {
127 syslog(LOG_WARNING
, "<%s> sum of rpu_uselen %d and"
128 " rpu_keeplen %d is %d(over 128)",
129 __func__
, rpu
->rpu_uselen
,
131 rpu
->rpu_uselen
+ rpu
->rpu_keeplen
);
139 do_use_prefix(int len
, struct rr_pco_match
*rpm
,
140 struct in6_rrenumreq
*irr
, int ifindex
)
142 struct rr_pco_use
*rpu
, *rpulim
;
146 rpu
= (struct rr_pco_use
*)(rpm
+ 1);
147 rpulim
= (struct rr_pco_use
*)((char *)rpm
+ len
);
149 if (rpu
== rpulim
) { /* no use prefix */
150 if (rpm
->rpm_code
== RPM_PCO_ADD
)
153 irr
->irr_u_uselen
= 0;
154 irr
->irr_u_keeplen
= 0;
155 irr
->irr_raf_mask_onlink
= 0;
156 irr
->irr_raf_mask_auto
= 0;
159 memset(&irr
->irr_flags
, 0, sizeof(irr
->irr_flags
));
160 irr
->irr_useprefix
.sin6_len
= 0; /* let it mean, no addition */
161 irr
->irr_useprefix
.sin6_family
= 0;
162 irr
->irr_useprefix
.sin6_addr
= in6addr_any
;
163 if (ioctl(s
, rrcmd2pco
[rpm
->rpm_code
], (caddr_t
)irr
) < 0 &&
164 errno
!= EADDRNOTAVAIL
)
165 syslog(LOG_ERR
, "<%s> ioctl: %s", __func__
,
170 for (rpu
= (struct rr_pco_use
*)(rpm
+ 1),
171 rpulim
= (struct rr_pco_use
*)((char *)rpm
+ len
);
174 /* init in6_rrenumreq fields */
175 irr
->irr_u_uselen
= rpu
->rpu_uselen
;
176 irr
->irr_u_keeplen
= rpu
->rpu_keeplen
;
177 irr
->irr_raf_mask_onlink
=
178 !!(rpu
->rpu_ramask
& ICMP6_RR_PCOUSE_RAFLAGS_ONLINK
);
179 irr
->irr_raf_mask_auto
=
180 !!(rpu
->rpu_ramask
& ICMP6_RR_PCOUSE_RAFLAGS_AUTO
);
181 irr
->irr_vltime
= ntohl(rpu
->rpu_vltime
);
182 irr
->irr_pltime
= ntohl(rpu
->rpu_pltime
);
183 irr
->irr_raf_onlink
=
184 (rpu
->rpu_raflags
& ICMP6_RR_PCOUSE_RAFLAGS_ONLINK
) == 0 ? 0 : 1;
186 (rpu
->rpu_raflags
& ICMP6_RR_PCOUSE_RAFLAGS_AUTO
) == 0 ? 0 : 1;
187 irr
->irr_rrf_decrvalid
=
188 (rpu
->rpu_flags
& ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME
) == 0 ? 0 : 1;
189 irr
->irr_rrf_decrprefd
=
190 (rpu
->rpu_flags
& ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME
) == 0 ? 0 : 1;
191 irr
->irr_useprefix
.sin6_len
= sizeof(irr
->irr_useprefix
);
192 irr
->irr_useprefix
.sin6_family
= AF_INET6
;
193 irr
->irr_useprefix
.sin6_addr
= rpu
->rpu_prefix
;
195 if (ioctl(s
, rrcmd2pco
[rpm
->rpm_code
], (caddr_t
)irr
) < 0 &&
196 errno
!= EADDRNOTAVAIL
)
197 syslog(LOG_ERR
, "<%s> ioctl: %s", __func__
,
200 /* very adhoc: should be rewritten */
201 if (rpm
->rpm_code
== RPM_PCO_CHANGE
&&
202 IN6_ARE_ADDR_EQUAL(&rpm
->rpm_prefix
, &rpu
->rpu_prefix
) &&
203 rpm
->rpm_matchlen
== rpu
->rpu_uselen
&&
204 rpu
->rpu_uselen
== rpu
->rpu_keeplen
) {
205 if ((rai
= if_indextorainfo(ifindex
)) == NULL
)
206 continue; /* non-advertising IF */
208 for (pp
= rai
->prefix
.next
; pp
!= &rai
->prefix
;
212 if (prefix_match(&pp
->prefix
, pp
->prefixlen
,
214 rpm
->rpm_matchlen
)) {
215 /* change parameters */
216 pp
->validlifetime
= ntohl(rpu
->rpu_vltime
);
217 pp
->preflifetime
= ntohl(rpu
->rpu_pltime
);
218 if (irr
->irr_rrf_decrvalid
) {
219 gettimeofday(&now
, 0);
221 now
.tv_sec
+ pp
->validlifetime
;
223 pp
->vltimeexpire
= 0;
224 if (irr
->irr_rrf_decrprefd
) {
225 gettimeofday(&now
, 0);
227 now
.tv_sec
+ pp
->preflifetime
;
229 pp
->pltimeexpire
= 0;
237 * process a Prefix Control Operation(PCO).
238 * return 0 on success, 1 on failure
241 do_pco(struct icmp6_router_renum
*rr
, int len
, struct rr_pco_match
*rpm
)
244 struct in6_rrenumreq irr
;
246 if ((rr_pco_check(len
, rpm
) != 0))
249 if (s
== -1 && (s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
250 syslog(LOG_ERR
, "<%s> socket: %s", __func__
,
255 memset(&irr
, 0, sizeof(irr
));
256 irr
.irr_origin
= PR_ORIG_RR
;
257 irr
.irr_m_len
= rpm
->rpm_matchlen
;
258 irr
.irr_m_minlen
= rpm
->rpm_minlen
;
259 irr
.irr_m_maxlen
= rpm
->rpm_maxlen
;
260 irr
.irr_matchprefix
.sin6_len
= sizeof(irr
.irr_matchprefix
);
261 irr
.irr_matchprefix
.sin6_family
= AF_INET6
;
262 irr
.irr_matchprefix
.sin6_addr
= rpm
->rpm_prefix
;
264 while (if_indextoname(++ifindex
, irr
.irr_name
)) {
266 * if ICMP6_RR_FLAGS_FORCEAPPLY(A flag) is 0 and IFF_UP is off,
267 * the interface is not applied
269 if ((rr
->rr_flags
& ICMP6_RR_FLAGS_FORCEAPPLY
) == 0 &&
270 (iflist
[ifindex
]->ifm_flags
& IFF_UP
) == 0)
272 /* TODO: interface scope check */
273 do_use_prefix(len
, rpm
, &irr
, ifindex
);
278 syslog(LOG_ERR
, "<%s> if_indextoname: %s", __func__
,
286 * call do_pco() for each Prefix Control Operations(PCOs) in a received
287 * Router Renumbering Command packet.
288 * return 0 on success, 1 on failure
291 do_rr(int len
, struct icmp6_router_renum
*rr
)
293 struct rr_pco_match
*rpm
;
296 lim
= (char *)rr
+ len
;
297 cp
= (char *)(rr
+ 1);
298 len
-= sizeof(struct icmp6_router_renum
);
300 /* get iflist block from kernel again, to get up-to-date information */
306 rpm
= (struct rr_pco_match
*)cp
;
307 if (len
< sizeof(struct rr_pco_match
)) {
309 syslog(LOG_ERR
, "<%s> pkt too short. left len = %d. "
310 "gabage at end of pkt?", __func__
, len
);
313 rpmlen
= rpm
->rpm_len
<< 3;
317 if (do_pco(rr
, rpmlen
, rpm
)) {
318 syslog(LOG_WARNING
, "<%s> invalid PCO", __func__
);
331 * check validity of a router renumbering command packet
332 * return 0 on success, 1 on failure
335 rr_command_check(int len
, struct icmp6_router_renum
*rr
, struct in6_addr
*from
,
336 struct in6_addr
*dst
)
338 char ntopbuf
[INET6_ADDRSTRLEN
];
340 /* omit rr minimal length check. hope kernel have done it. */
341 /* rr_command length check */
342 if (len
< (sizeof(struct icmp6_router_renum
) +
343 sizeof(struct rr_pco_match
))) {
344 syslog(LOG_ERR
, "<%s> rr_command len %d is too short",
349 /* destination check. only for multicast. omit unicast check. */
350 if (IN6_IS_ADDR_MULTICAST(dst
) && !IN6_IS_ADDR_MC_LINKLOCAL(dst
) &&
351 !IN6_IS_ADDR_MC_SITELOCAL(dst
)) {
352 syslog(LOG_ERR
, "<%s> dst mcast addr %s is illegal",
354 inet_ntop(AF_INET6
, dst
, ntopbuf
, INET6_ADDRSTRLEN
));
358 /* seqnum and segnum check */
359 if (rro
.rro_seqnum
> rr
->rr_seqnum
) {
361 "<%s> rcvd old seqnum %d from %s",
362 __func__
, (u_int32_t
)ntohl(rr
->rr_seqnum
),
363 inet_ntop(AF_INET6
, from
, ntopbuf
, INET6_ADDRSTRLEN
));
366 if (rro
.rro_seqnum
== rr
->rr_seqnum
&&
367 (rr
->rr_flags
& ICMP6_RR_FLAGS_TEST
) == 0 &&
368 RR_ISSET_SEGNUM(rro
.rro_segnum_bits
, rr
->rr_segnum
)) {
369 if ((rr
->rr_flags
& ICMP6_RR_FLAGS_REQRESULT
) != 0)
371 "<%s> rcvd duped segnum %d from %s",
372 __func__
, rr
->rr_segnum
,
373 inet_ntop(AF_INET6
, from
, ntopbuf
,
379 if (rro
.rro_seqnum
!= rr
->rr_seqnum
) {
380 /* then must be "<" */
382 /* init rro_segnum_bits */
383 memset(rro
.rro_segnum_bits
, 0,
384 sizeof(rro
.rro_segnum_bits
));
386 rro
.rro_seqnum
= rr
->rr_seqnum
;
392 rr_command_input(int len
, struct icmp6_router_renum
*rr
,
393 struct in6_addr
*from
, struct in6_addr
*dst
)
395 /* rr_command validity check */
396 if (rr_command_check(len
, rr
, from
, dst
))
398 if ((rr
->rr_flags
& (ICMP6_RR_FLAGS_TEST
|ICMP6_RR_FLAGS_REQRESULT
)) ==
402 /* do router renumbering */
403 if (do_rr(len
, rr
)) {
408 RR_SET_SEGNUM(rro
.rro_segnum_bits
, rr
->rr_segnum
);
413 syslog(LOG_ERR
, "<%s> received RR was invalid", __func__
);
418 rr_input(int len
, struct icmp6_router_renum
*rr
, struct in6_pktinfo
*pi
,
419 struct sockaddr_in6
*from
, struct in6_addr
*dst
)
421 char ntopbuf
[2][INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
424 "<%s> RR received from %s to %s on %s",
426 inet_ntop(AF_INET6
, &from
->sin6_addr
,
427 ntopbuf
[0], INET6_ADDRSTRLEN
),
428 inet_ntop(AF_INET6
, &dst
, ntopbuf
[1], INET6_ADDRSTRLEN
),
429 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
431 /* packet validation based on Section 4.1 of RFC2894 */
432 if (len
< sizeof(struct icmp6_router_renum
)) {
434 "<%s>: RR short message (size %d) from %s to %s on %s",
436 inet_ntop(AF_INET6
, &from
->sin6_addr
,
437 ntopbuf
[0], INET6_ADDRSTRLEN
),
438 inet_ntop(AF_INET6
, &dst
, ntopbuf
[1], INET6_ADDRSTRLEN
),
439 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
444 * If the IPv6 destination address is neither an All Routers multicast
445 * address [AARCH] nor one of the receiving router's unicast addresses,
446 * the message MUST be discarded and SHOULD be logged to network
448 * We rely on the kernel input routine for unicast addresses, and thus
449 * check multicast destinations only.
451 if (IN6_IS_ADDR_MULTICAST(&pi
->ipi6_addr
) &&
452 !IN6_ARE_ADDR_EQUAL(&in6a_site_allrouters
, &pi
->ipi6_addr
)) {
454 "<%s>: RR message with invalid destination (%s) "
457 inet_ntop(AF_INET6
, &dst
, ntopbuf
[0], INET6_ADDRSTRLEN
),
458 inet_ntop(AF_INET6
, &from
->sin6_addr
,
459 ntopbuf
[1], INET6_ADDRSTRLEN
),
460 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
464 rr_rcvifindex
= pi
->ipi6_ifindex
;
466 switch (rr
->rr_code
) {
467 case ICMP6_ROUTER_RENUMBERING_COMMAND
:
468 rr_command_input(len
, rr
, &from
->sin6_addr
, dst
);
469 /* TODO: send reply msg */
471 case ICMP6_ROUTER_RENUMBERING_RESULT
:
472 /* RESULT will be processed by rrenumd */
474 case ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET
:
475 /* TODO: sequence number reset */
478 syslog(LOG_ERR
, "<%s> received unknown code %d",
479 __func__
, rr
->rr_code
);