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>
53 #define RR_ISSET_SEGNUM(segnum_bits, segnum) \
54 ((((segnum_bits)[(segnum) >> 5]) & (1 << ((segnum) & 31))) != 0)
55 #define RR_SET_SEGNUM(segnum_bits, segnum) \
56 (((segnum_bits)[(segnum) >> 5]) |= (1 << ((segnum) & 31)))
60 u_long rro_segnum_bits
[8];
63 static struct rr_operation rro
;
64 static int rr_rcvifindex
;
65 static int rrcmd2pco
[RPM_PCO_MAX
] = {
74 * Check validity of a Prefix Control Operation(PCO).
75 * Return 0 on success, 1 on failure.
78 rr_pco_check(int len
, struct rr_pco_match
*rpm
)
80 struct rr_pco_use
*rpu
, *rpulim
;
83 /* rpm->rpm_len must be (4N * 3) as router-renum-05.txt */
84 if ((rpm
->rpm_len
- 3) < 0 || /* must be at least 3 */
85 (rpm
->rpm_len
- 3) & 0x3) { /* must be multiple of 4 */
86 errorlog("<%s> rpm_len %d is not 4N * 3",
87 __func__
, rpm
->rpm_len
);
90 /* rpm->rpm_code must be valid value */
91 switch (rpm
->rpm_code
) {
94 case RPM_PCO_SETGLOBAL
:
97 errorlog("<%s> unknown rpm_code %d", __func__
,
101 /* rpm->rpm_matchlen must be 0 to 128 inclusive */
102 if (rpm
->rpm_matchlen
> 128) {
103 errorlog("<%s> rpm_matchlen %d is over 128",
104 __func__
, rpm
->rpm_matchlen
);
109 * rpu->rpu_uselen, rpu->rpu_keeplen, and sum of them must be
110 * between 0 and 128 inclusive
112 for (rpu
= (struct rr_pco_use
*)(rpm
+ 1),
113 rpulim
= (struct rr_pco_use
*)((char *)rpm
+ len
);
116 checklen
= rpu
->rpu_uselen
;
117 checklen
+= rpu
->rpu_keeplen
;
119 * omit these check, because either of rpu_uselen
120 * and rpu_keeplen is unsigned char
121 * (128 > rpu_uselen > 0)
122 * (128 > rpu_keeplen > 0)
123 * (rpu_uselen + rpu_keeplen > 0)
125 if (checklen
> 128) {
126 errorlog("<%s> sum of rpu_uselen %d and"
127 " rpu_keeplen %d is %d(over 128)",
128 __func__
, rpu
->rpu_uselen
,
130 rpu
->rpu_uselen
+ rpu
->rpu_keeplen
);
138 do_use_prefix(int len
, struct rr_pco_match
*rpm
,
139 struct in6_rrenumreq
*irr
, int ifindex
)
141 struct rr_pco_use
*rpu
, *rpulim
;
145 rpu
= (struct rr_pco_use
*)(rpm
+ 1);
146 rpulim
= (struct rr_pco_use
*)((char *)rpm
+ len
);
148 if (rpu
== rpulim
) { /* no use prefix */
149 if (rpm
->rpm_code
== RPM_PCO_ADD
)
152 irr
->irr_u_uselen
= 0;
153 irr
->irr_u_keeplen
= 0;
154 irr
->irr_raf_mask_onlink
= 0;
155 irr
->irr_raf_mask_auto
= 0;
158 memset(&irr
->irr_flags
, 0, sizeof(irr
->irr_flags
));
159 irr
->irr_useprefix
.sin6_len
= 0; /* let it mean, no addition */
160 irr
->irr_useprefix
.sin6_family
= 0;
161 irr
->irr_useprefix
.sin6_addr
= in6addr_any
;
162 if (ioctl(s
, rrcmd2pco
[rpm
->rpm_code
], (caddr_t
)irr
) < 0 &&
163 errno
!= EADDRNOTAVAIL
)
164 errorlog("<%s> ioctl: %s", __func__
,
169 for (rpu
= (struct rr_pco_use
*)(rpm
+ 1),
170 rpulim
= (struct rr_pco_use
*)((char *)rpm
+ len
);
173 /* init in6_rrenumreq fields */
174 irr
->irr_u_uselen
= rpu
->rpu_uselen
;
175 irr
->irr_u_keeplen
= rpu
->rpu_keeplen
;
176 irr
->irr_raf_mask_onlink
=
177 !!(rpu
->rpu_ramask
& ICMP6_RR_PCOUSE_RAFLAGS_ONLINK
);
178 irr
->irr_raf_mask_auto
=
179 !!(rpu
->rpu_ramask
& ICMP6_RR_PCOUSE_RAFLAGS_AUTO
);
180 irr
->irr_vltime
= ntohl(rpu
->rpu_vltime
);
181 irr
->irr_pltime
= ntohl(rpu
->rpu_pltime
);
182 irr
->irr_raf_onlink
=
183 (rpu
->rpu_raflags
& ICMP6_RR_PCOUSE_RAFLAGS_ONLINK
) == 0 ? 0 : 1;
185 (rpu
->rpu_raflags
& ICMP6_RR_PCOUSE_RAFLAGS_AUTO
) == 0 ? 0 : 1;
186 irr
->irr_rrf_decrvalid
=
187 (rpu
->rpu_flags
& ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME
) == 0 ? 0 : 1;
188 irr
->irr_rrf_decrprefd
=
189 (rpu
->rpu_flags
& ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME
) == 0 ? 0 : 1;
190 irr
->irr_useprefix
.sin6_len
= sizeof(irr
->irr_useprefix
);
191 irr
->irr_useprefix
.sin6_family
= AF_INET6
;
192 irr
->irr_useprefix
.sin6_addr
= rpu
->rpu_prefix
;
194 if (ioctl(s
, rrcmd2pco
[rpm
->rpm_code
], (caddr_t
)irr
) < 0 &&
195 errno
!= EADDRNOTAVAIL
)
196 errorlog("<%s> ioctl: %s", __func__
,
199 /* very adhoc: should be rewritten */
200 if (rpm
->rpm_code
== RPM_PCO_CHANGE
&&
201 IN6_ARE_ADDR_EQUAL(&rpm
->rpm_prefix
, &rpu
->rpu_prefix
) &&
202 rpm
->rpm_matchlen
== rpu
->rpu_uselen
&&
203 rpu
->rpu_uselen
== rpu
->rpu_keeplen
) {
204 if ((rai
= if_indextorainfo(ifindex
)) == NULL
)
205 continue; /* non-advertising IF */
207 for (pp
= rai
->prefix
.next
; pp
!= &rai
->prefix
;
211 if (prefix_match(&pp
->prefix
, pp
->prefixlen
,
213 rpm
->rpm_matchlen
)) {
214 /* change parameters */
215 pp
->validlifetime
= ntohl(rpu
->rpu_vltime
);
216 pp
->preflifetime
= ntohl(rpu
->rpu_pltime
);
217 if (irr
->irr_rrf_decrvalid
) {
218 gettimeofday(&now
, 0);
220 now
.tv_sec
+ pp
->validlifetime
;
222 pp
->vltimeexpire
= 0;
223 if (irr
->irr_rrf_decrprefd
) {
224 gettimeofday(&now
, 0);
226 now
.tv_sec
+ pp
->preflifetime
;
228 pp
->pltimeexpire
= 0;
236 * process a Prefix Control Operation(PCO).
237 * return 0 on success, 1 on failure
240 do_pco(struct icmp6_router_renum
*rr
, int len
, struct rr_pco_match
*rpm
)
243 struct in6_rrenumreq irr
;
245 if ((rr_pco_check(len
, rpm
) != 0))
248 if (s
== -1 && (s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
249 errorlog("<%s> socket: %s", __func__
,
254 memset(&irr
, 0, sizeof(irr
));
255 irr
.irr_origin
= PR_ORIG_RR
;
256 irr
.irr_m_len
= rpm
->rpm_matchlen
;
257 irr
.irr_m_minlen
= rpm
->rpm_minlen
;
258 irr
.irr_m_maxlen
= rpm
->rpm_maxlen
;
259 irr
.irr_matchprefix
.sin6_len
= sizeof(irr
.irr_matchprefix
);
260 irr
.irr_matchprefix
.sin6_family
= AF_INET6
;
261 irr
.irr_matchprefix
.sin6_addr
= rpm
->rpm_prefix
;
263 while (if_indextoname(++ifindex
, irr
.irr_name
)) {
264 struct if_msghdr
* ifm
= get_interface_entry(ifindex
);
266 debuglog("Couldn't find interface entry for %d. Skipping.", ifindex
);
270 * if ICMP6_RR_FLAGS_FORCEAPPLY(A flag) is 0 and IFF_UP is off,
271 * the interface is not applied
273 if ((rr
->rr_flags
& ICMP6_RR_FLAGS_FORCEAPPLY
) == 0 &&
274 (ifm
->ifm_flags
& IFF_UP
) == 0)
276 /* TODO: interface scope check */
277 do_use_prefix(len
, rpm
, &irr
, ifindex
);
282 errorlog("<%s> if_indextoname: %s", __func__
,
290 * call do_pco() for each Prefix Control Operations(PCOs) in a received
291 * Router Renumbering Command packet.
292 * return 0 on success, 1 on failure
295 do_rr(int len
, struct icmp6_router_renum
*rr
)
297 struct rr_pco_match
*rpm
;
300 lim
= (char *)rr
+ len
;
301 cp
= (char *)(rr
+ 1);
302 len
-= sizeof(struct icmp6_router_renum
);
304 /* get iflist block from kernel again, to get up-to-date information */
310 rpm
= (struct rr_pco_match
*)cp
;
311 if (len
< sizeof(struct rr_pco_match
)) {
313 errorlog("<%s> pkt too short. left len = %d. "
314 "gabage at end of pkt?", __func__
, len
);
317 rpmlen
= rpm
->rpm_len
<< 3;
321 if (do_pco(rr
, rpmlen
, rpm
)) {
322 errorlog("<%s> invalid PCO", __func__
);
335 * check validity of a router renumbering command packet
336 * return 0 on success, 1 on failure
339 rr_command_check(int len
, struct icmp6_router_renum
*rr
, struct in6_addr
*from
,
340 struct in6_addr
*dst
)
342 char ntopbuf
[INET6_ADDRSTRLEN
];
344 /* omit rr minimal length check. hope kernel have done it. */
345 /* rr_command length check */
346 if (len
< (sizeof(struct icmp6_router_renum
) +
347 sizeof(struct rr_pco_match
))) {
348 errorlog("<%s> rr_command len %d is too short",
353 /* destination check. only for multicast. omit unicast check. */
354 if (IN6_IS_ADDR_MULTICAST(dst
) && !IN6_IS_ADDR_MC_LINKLOCAL(dst
) &&
355 !IN6_IS_ADDR_MC_SITELOCAL(dst
)) {
356 errorlog("<%s> dst mcast addr %s is illegal",
358 inet_ntop(AF_INET6
, dst
, ntopbuf
, INET6_ADDRSTRLEN
));
362 /* seqnum and segnum check */
363 if (rro
.rro_seqnum
> rr
->rr_seqnum
) {
364 errorlog("<%s> rcvd old seqnum %d from %s",
365 __func__
, (u_int32_t
)ntohl(rr
->rr_seqnum
),
366 inet_ntop(AF_INET6
, from
, ntopbuf
, INET6_ADDRSTRLEN
));
369 if (rro
.rro_seqnum
== rr
->rr_seqnum
&&
370 (rr
->rr_flags
& ICMP6_RR_FLAGS_TEST
) == 0 &&
371 RR_ISSET_SEGNUM(rro
.rro_segnum_bits
, rr
->rr_segnum
)) {
372 if ((rr
->rr_flags
& ICMP6_RR_FLAGS_REQRESULT
) != 0)
373 errorlog("<%s> rcvd duped segnum %d from %s",
374 __func__
, rr
->rr_segnum
,
375 inet_ntop(AF_INET6
, from
, ntopbuf
,
381 if (rro
.rro_seqnum
!= rr
->rr_seqnum
) {
382 /* then must be "<" */
384 /* init rro_segnum_bits */
385 memset(rro
.rro_segnum_bits
, 0,
386 sizeof(rro
.rro_segnum_bits
));
388 rro
.rro_seqnum
= rr
->rr_seqnum
;
394 rr_command_input(int len
, struct icmp6_router_renum
*rr
,
395 struct in6_addr
*from
, struct in6_addr
*dst
)
397 /* rr_command validity check */
398 if (rr_command_check(len
, rr
, from
, dst
))
400 if ((rr
->rr_flags
& (ICMP6_RR_FLAGS_TEST
|ICMP6_RR_FLAGS_REQRESULT
)) ==
404 /* do router renumbering */
405 if (do_rr(len
, rr
)) {
410 RR_SET_SEGNUM(rro
.rro_segnum_bits
, rr
->rr_segnum
);
415 errorlog("<%s> received RR was invalid", __func__
);
420 rr_input(int len
, struct icmp6_router_renum
*rr
, struct in6_pktinfo
*pi
,
421 struct sockaddr_in6
*from
, struct in6_addr
*dst
)
423 char ntopbuf
[2][INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
425 debuglog("<%s> RR received from %s to %s on %s",
427 inet_ntop(AF_INET6
, &from
->sin6_addr
,
428 ntopbuf
[0], INET6_ADDRSTRLEN
),
429 inet_ntop(AF_INET6
, &dst
, ntopbuf
[1], INET6_ADDRSTRLEN
),
430 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
432 /* packet validation based on Section 4.1 of RFC2894 */
433 if (len
< sizeof(struct icmp6_router_renum
)) {
434 noticelog("<%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
)) {
453 noticelog("<%s>: RR message with invalid destination (%s) "
456 inet_ntop(AF_INET6
, &dst
, ntopbuf
[0], INET6_ADDRSTRLEN
),
457 inet_ntop(AF_INET6
, &from
->sin6_addr
,
458 ntopbuf
[1], INET6_ADDRSTRLEN
),
459 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
463 rr_rcvifindex
= pi
->ipi6_ifindex
;
465 switch (rr
->rr_code
) {
466 case ICMP6_ROUTER_RENUMBERING_COMMAND
:
467 rr_command_input(len
, rr
, &from
->sin6_addr
, dst
);
468 /* TODO: send reply msg */
470 case ICMP6_ROUTER_RENUMBERING_RESULT
:
471 /* RESULT will be processed by rrenumd */
473 case ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET
:
474 /* TODO: sequence number reset */
477 errorlog("<%s> received unknown code %d",
478 __func__
, rr
->rr_code
);