1 /* $KAME: rrenum.c,v 1.10 2001/01/21 15:32:16 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/rtadvd/rrenum.c,v 1.2.2.2 2001/07/03 11:02:14 ume Exp $
32 * $DragonFly: src/usr.sbin/rtadvd/rrenum.c,v 1.5 2008/06/05 18:06:33 swildner Exp $
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
41 #if defined(__DragonFly__)
42 #include <net/if_var.h>
43 #endif /* __DragonFly__ */
44 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/in_var.h>
47 #include <netinet/icmp6.h>
49 #include <arpa/inet.h>
59 #define RR_ISSET_SEGNUM(segnum_bits, segnum) \
60 ((((segnum_bits)[(segnum) >> 5]) & (1 << ((segnum) & 31))) != 0)
61 #define RR_SET_SEGNUM(segnum_bits, segnum) \
62 (((segnum_bits)[(segnum) >> 5]) |= (1 << ((segnum) & 31)))
66 u_long rro_segnum_bits
[8];
69 static struct rr_operation rro
;
70 static int rr_rcvifindex
;
71 static int rrcmd2pco
[RPM_PCO_MAX
] = {
80 * Check validity of a Prefix Control Operation(PCO).
81 * Return 0 on success, 1 on failure.
84 rr_pco_check(int len
, struct rr_pco_match
*rpm
)
86 struct rr_pco_use
*rpu
, *rpulim
;
89 /* rpm->rpm_len must be (4N * 3) as router-renum-05.txt */
90 if ((rpm
->rpm_len
- 3) < 0 || /* must be at least 3 */
91 (rpm
->rpm_len
- 3) & 0x3) { /* must be multiple of 4 */
92 syslog(LOG_WARNING
, "<%s> rpm_len %d is not 4N * 3",
93 __func__
, rpm
->rpm_len
);
96 /* rpm->rpm_code must be valid value */
97 switch(rpm
->rpm_code
) {
100 case RPM_PCO_SETGLOBAL
:
103 syslog(LOG_WARNING
, "<%s> unknown rpm_code %d", __func__
,
107 /* rpm->rpm_matchlen must be 0 to 128 inclusive */
108 if (rpm
->rpm_matchlen
> 128) {
109 syslog(LOG_WARNING
, "<%s> rpm_matchlen %d is over 128",
110 __func__
, rpm
->rpm_matchlen
);
115 * rpu->rpu_uselen, rpu->rpu_keeplen, and sum of them must be
116 * between 0 and 128 inclusive
118 for (rpu
= (struct rr_pco_use
*)(rpm
+ 1),
119 rpulim
= (struct rr_pco_use
*)((char *)rpm
+ len
);
122 checklen
= rpu
->rpu_uselen
;
123 checklen
+= rpu
->rpu_keeplen
;
125 * omit these check, because either of rpu_uselen
126 * and rpu_keeplen is unsigned char
127 * (128 > rpu_uselen > 0)
128 * (128 > rpu_keeplen > 0)
129 * (rpu_uselen + rpu_keeplen > 0)
131 if (checklen
> 128) {
132 syslog(LOG_WARNING
, "<%s> sum of rpu_uselen %d and"
133 " rpu_keeplen %d is %d(over 128)",
134 __func__
, rpu
->rpu_uselen
,
136 rpu
->rpu_uselen
+ rpu
->rpu_keeplen
);
144 do_use_prefix(int len
, struct rr_pco_match
*rpm
,
145 struct in6_rrenumreq
*irr
, int ifindex
)
147 struct rr_pco_use
*rpu
, *rpulim
;
151 rpu
= (struct rr_pco_use
*)(rpm
+ 1);
152 rpulim
= (struct rr_pco_use
*)((char *)rpm
+ len
);
154 if (rpu
== rpulim
) { /* no use prefix */
155 if (rpm
->rpm_code
== RPM_PCO_ADD
)
158 irr
->irr_u_uselen
= 0;
159 irr
->irr_u_keeplen
= 0;
160 irr
->irr_raf_mask_onlink
= 0;
161 irr
->irr_raf_mask_auto
= 0;
164 memset(&irr
->irr_flags
, 0, sizeof(irr
->irr_flags
));
165 irr
->irr_useprefix
.sin6_len
= 0; /* let it mean, no addition */
166 irr
->irr_useprefix
.sin6_family
= 0;
167 irr
->irr_useprefix
.sin6_addr
= in6addr_any
;
168 if (ioctl(s
, rrcmd2pco
[rpm
->rpm_code
], (caddr_t
)irr
) < 0 &&
169 errno
!= EADDRNOTAVAIL
)
170 syslog(LOG_ERR
, "<%s> ioctl: %s", __func__
,
175 for (rpu
= (struct rr_pco_use
*)(rpm
+ 1),
176 rpulim
= (struct rr_pco_use
*)((char *)rpm
+ len
);
179 /* init in6_rrenumreq fields */
180 irr
->irr_u_uselen
= rpu
->rpu_uselen
;
181 irr
->irr_u_keeplen
= rpu
->rpu_keeplen
;
182 irr
->irr_raf_mask_onlink
=
183 (rpu
->rpu_ramask
& ICMP6_RR_PCOUSE_RAFLAGS_ONLINK
) == 0 ? 0 : 1;
184 irr
->irr_raf_mask_auto
=
185 (rpu
->rpu_ramask
& ICMP6_RR_PCOUSE_RAFLAGS_AUTO
);
186 irr
->irr_vltime
= ntohl(rpu
->rpu_vltime
);
187 irr
->irr_pltime
= ntohl(rpu
->rpu_pltime
);
188 irr
->irr_raf_onlink
=
189 (rpu
->rpu_raflags
& ICMP6_RR_PCOUSE_RAFLAGS_ONLINK
) == 0 ? 0 : 1;
191 (rpu
->rpu_raflags
& ICMP6_RR_PCOUSE_RAFLAGS_AUTO
) == 0 ? 0 : 1;
192 irr
->irr_rrf_decrvalid
=
193 (rpu
->rpu_flags
& ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME
) == 0 ? 0 : 1;
194 irr
->irr_rrf_decrprefd
=
195 (rpu
->rpu_flags
& ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME
) == 0 ? 0 : 1;
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 for (pp
= rai
->prefix
.next
; pp
!= &rai
->prefix
;
217 if (prefix_match(&pp
->prefix
, pp
->prefixlen
,
219 rpm
->rpm_matchlen
)) {
220 /* change parameters */
221 pp
->validlifetime
= ntohl(rpu
->rpu_vltime
);
222 pp
->preflifetime
= ntohl(rpu
->rpu_pltime
);
223 if (irr
->irr_rrf_decrvalid
) {
224 gettimeofday(&now
, 0);
226 now
.tv_sec
+ pp
->validlifetime
;
228 pp
->vltimeexpire
= 0;
229 if (irr
->irr_rrf_decrprefd
) {
230 gettimeofday(&now
, 0);
232 now
.tv_sec
+ pp
->preflifetime
;
234 pp
->pltimeexpire
= 0;
242 * process a Prefix Control Operation(PCO).
243 * return 0 on success, 1 on failure
246 do_pco(struct icmp6_router_renum
*rr
, int len
, struct rr_pco_match
*rpm
)
249 struct in6_rrenumreq irr
;
251 if ((rr_pco_check(len
, rpm
) != 0))
254 if (s
== -1 && (s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
255 syslog(LOG_ERR
, "<%s> socket: %s", __func__
,
260 memset(&irr
, 0, sizeof(irr
));
261 irr
.irr_origin
= PR_ORIG_RR
;
262 irr
.irr_m_len
= rpm
->rpm_matchlen
;
263 irr
.irr_m_minlen
= rpm
->rpm_minlen
;
264 irr
.irr_m_maxlen
= rpm
->rpm_maxlen
;
265 irr
.irr_matchprefix
.sin6_len
= sizeof(irr
.irr_matchprefix
);
266 irr
.irr_matchprefix
.sin6_family
= AF_INET6
;
267 irr
.irr_matchprefix
.sin6_addr
= rpm
->rpm_prefix
;
269 while (if_indextoname(++ifindex
, irr
.irr_name
)) {
271 * if ICMP6_RR_FLAGS_FORCEAPPLY(A flag) is 0 and IFF_UP is off,
272 * the interface is not applied
274 if ((rr
->rr_flags
& ICMP6_RR_FLAGS_FORCEAPPLY
) == 0 &&
275 (iflist
[ifindex
]->ifm_flags
& IFF_UP
) == 0)
277 /* TODO: interface scope check */
278 do_use_prefix(len
, rpm
, &irr
, ifindex
);
283 syslog(LOG_ERR
, "<%s> if_indextoname: %s", __func__
,
291 * call do_pco() for each Prefix Control Operations(PCOs) in a received
292 * Router Renumbering Command packet.
293 * return 0 on success, 1 on failure
296 do_rr(int len
, struct icmp6_router_renum
*rr
)
298 struct rr_pco_match
*rpm
;
301 lim
= (char *)rr
+ len
;
302 cp
= (char *)(rr
+ 1);
303 len
-= sizeof(struct icmp6_router_renum
);
305 /* get iflist block from kernel again, to get up-to-date information */
311 rpm
= (struct rr_pco_match
*)cp
;
312 if (len
< sizeof(struct rr_pco_match
)) {
314 syslog(LOG_ERR
, "<%s> pkt too short. left len = %d. "
315 "gabage at end of pkt?", __func__
, len
);
318 rpmlen
= rpm
->rpm_len
<< 3;
322 if (do_pco(rr
, rpmlen
, rpm
)) {
323 syslog(LOG_WARNING
, "<%s> invalid PCO", __func__
);
336 * check validity of a router renumbering command packet
337 * return 0 on success, 1 on failure
340 rr_command_check(int len
, struct icmp6_router_renum
*rr
, struct in6_addr
*from
,
341 struct in6_addr
*dst
)
343 u_char ntopbuf
[INET6_ADDRSTRLEN
];
345 /* omit rr minimal length check. hope kernel have done it. */
346 /* rr_command length check */
347 if (len
< (sizeof(struct icmp6_router_renum
) +
348 sizeof(struct rr_pco_match
))) {
349 syslog(LOG_ERR
, "<%s> rr_command len %d is too short",
354 /* destination check. only for multicast. omit unicast check. */
355 if (IN6_IS_ADDR_MULTICAST(dst
) && !IN6_IS_ADDR_MC_LINKLOCAL(dst
) &&
356 !IN6_IS_ADDR_MC_SITELOCAL(dst
)) {
357 syslog(LOG_ERR
, "<%s> dst mcast addr %s is illegal",
359 inet_ntop(AF_INET6
, dst
, ntopbuf
, INET6_ADDRSTRLEN
));
363 /* seqnum and segnum check */
364 if (rro
.rro_seqnum
> rr
->rr_seqnum
) {
366 "<%s> rcvd old seqnum %d from %s",
367 __func__
, (u_int32_t
)ntohl(rr
->rr_seqnum
),
368 inet_ntop(AF_INET6
, from
, ntopbuf
, INET6_ADDRSTRLEN
));
371 if (rro
.rro_seqnum
== rr
->rr_seqnum
&&
372 (rr
->rr_flags
& ICMP6_RR_FLAGS_TEST
) == 0 &&
373 RR_ISSET_SEGNUM(rro
.rro_segnum_bits
, rr
->rr_segnum
)) {
374 if ((rr
->rr_flags
& ICMP6_RR_FLAGS_REQRESULT
) != 0)
376 "<%s> rcvd duped segnum %d from %s",
377 __func__
, rr
->rr_segnum
,
378 inet_ntop(AF_INET6
, from
, ntopbuf
,
384 if (rro
.rro_seqnum
!= rr
->rr_seqnum
) {
385 /* then must be "<" */
387 /* init rro_segnum_bits */
388 memset(rro
.rro_segnum_bits
, 0,
389 sizeof(rro
.rro_segnum_bits
));
391 rro
.rro_seqnum
= rr
->rr_seqnum
;
397 rr_command_input(int len
, struct icmp6_router_renum
*rr
,
398 struct in6_addr
*from
, struct in6_addr
*dst
)
400 /* rr_command validity check */
401 if (rr_command_check(len
, rr
, from
, dst
))
403 if ((rr
->rr_flags
& (ICMP6_RR_FLAGS_TEST
|ICMP6_RR_FLAGS_REQRESULT
)) ==
407 /* do router renumbering */
408 if (do_rr(len
, rr
)) {
413 RR_SET_SEGNUM(rro
.rro_segnum_bits
, rr
->rr_segnum
);
418 syslog(LOG_ERR
, "<%s> received RR was invalid", __func__
);
423 rr_input(int len
, struct icmp6_router_renum
*rr
, struct in6_pktinfo
*pi
,
424 struct sockaddr_in6
*from
, struct in6_addr
*dst
)
426 u_char ntopbuf
[2][INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
429 "<%s> RR received from %s to %s on %s",
431 inet_ntop(AF_INET6
, &from
->sin6_addr
,
432 ntopbuf
[0], INET6_ADDRSTRLEN
),
433 inet_ntop(AF_INET6
, &dst
, ntopbuf
[1], INET6_ADDRSTRLEN
),
434 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
436 /* packet validation based on Section 4.1 of RFC2894 */
437 if (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
,
442 ntopbuf
[0], INET6_ADDRSTRLEN
),
443 inet_ntop(AF_INET6
, &dst
, ntopbuf
[1], INET6_ADDRSTRLEN
),
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
) &&
457 !IN6_ARE_ADDR_EQUAL(&in6a_site_allrouters
, &pi
->ipi6_addr
)) {
459 "<%s>: RR message with invalid destination (%s) "
462 inet_ntop(AF_INET6
, &dst
, ntopbuf
[0], INET6_ADDRSTRLEN
),
463 inet_ntop(AF_INET6
, &from
->sin6_addr
,
464 ntopbuf
[1], INET6_ADDRSTRLEN
),
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
);