1 /* $FreeBSD: stable/10/usr.sbin/rtadvd/rrenum.c 253970 2013-08-05 20:13:02Z hrs $ */
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_dl.h>
40 #include <net/if_var.h>
41 #include <net/route.h>
42 #include <netinet/in.h>
43 #include <netinet/in_var.h>
44 #include <netinet/icmp6.h>
46 #include <arpa/inet.h>
58 #define RR_ISSET_SEGNUM(segnum_bits, segnum) \
59 ((((segnum_bits)[(segnum) >> 5]) & (1 << ((segnum) & 31))) != 0)
60 #define RR_SET_SEGNUM(segnum_bits, segnum) \
61 (((segnum_bits)[(segnum) >> 5]) |= (1 << ((segnum) & 31)))
65 u_long rro_segnum_bits
[8];
68 static struct rr_operation rro
;
69 static int rr_rcvifindex
;
70 static int rrcmd2pco
[RPM_PCO_MAX
] = {
79 * Check validity of a Prefix Control Operation(PCO).
80 * return 0 on success, 1 on failure.
83 rr_pco_check(int len
, struct rr_pco_match
*rpm
)
85 struct rr_pco_use
*rpu
, *rpulim
;
88 /* rpm->rpm_len must be (4N * 3) as router-renum-05.txt */
89 if ((rpm
->rpm_len
- 3) < 0 || /* must be at least 3 */
90 (rpm
->rpm_len
- 3) & 0x3) { /* must be multiple of 4 */
91 syslog(LOG_WARNING
, "<%s> rpm_len %d is not 4N * 3",
92 __func__
, rpm
->rpm_len
);
95 /* rpm->rpm_code must be valid value */
96 switch (rpm
->rpm_code
) {
99 case RPM_PCO_SETGLOBAL
:
102 syslog(LOG_WARNING
, "<%s> unknown rpm_code %d", __func__
,
106 /* rpm->rpm_matchlen must be 0 to 128 inclusive */
107 if (rpm
->rpm_matchlen
> 128) {
108 syslog(LOG_WARNING
, "<%s> rpm_matchlen %d is over 128",
109 __func__
, rpm
->rpm_matchlen
);
114 * rpu->rpu_uselen, rpu->rpu_keeplen, and sum of them must be
115 * between 0 and 128 inclusive
117 for (rpu
= (struct rr_pco_use
*)(rpm
+ 1),
118 rpulim
= (struct rr_pco_use
*)((char *)rpm
+ len
);
121 checklen
= rpu
->rpu_uselen
;
122 checklen
+= rpu
->rpu_keeplen
;
124 * omit these check, because either of rpu_uselen
125 * and rpu_keeplen is unsigned char
126 * (128 > rpu_uselen > 0)
127 * (128 > rpu_keeplen > 0)
128 * (rpu_uselen + rpu_keeplen > 0)
130 if (checklen
> 128) {
131 syslog(LOG_WARNING
, "<%s> sum of rpu_uselen %d and"
132 " rpu_keeplen %d is %d(over 128)",
133 __func__
, rpu
->rpu_uselen
, rpu
->rpu_keeplen
,
134 rpu
->rpu_uselen
+ rpu
->rpu_keeplen
);
142 do_use_prefix(int len
, struct rr_pco_match
*rpm
,
143 struct in6_rrenumreq
*irr
, int ifindex
)
145 struct rr_pco_use
*rpu
, *rpulim
;
150 rpu
= (struct rr_pco_use
*)(rpm
+ 1);
151 rpulim
= (struct rr_pco_use
*)((char *)rpm
+ len
);
153 if (rpu
== rpulim
) { /* no use prefix */
154 if (rpm
->rpm_code
== RPM_PCO_ADD
)
157 irr
->irr_u_uselen
= 0;
158 irr
->irr_u_keeplen
= 0;
159 irr
->irr_raf_mask_onlink
= 0;
160 irr
->irr_raf_mask_auto
= 0;
163 memset(&irr
->irr_flags
, 0, sizeof(irr
->irr_flags
));
164 irr
->irr_useprefix
.sin6_len
= 0; /* let it mean, no addition */
165 irr
->irr_useprefix
.sin6_family
= 0;
166 irr
->irr_useprefix
.sin6_addr
= in6addr_any
;
167 if (ioctl(s
, rrcmd2pco
[rpm
->rpm_code
], (caddr_t
)irr
) < 0 &&
168 errno
!= EADDRNOTAVAIL
)
169 syslog(LOG_ERR
, "<%s> ioctl: %s", __func__
,
174 for (rpu
= (struct rr_pco_use
*)(rpm
+ 1),
175 rpulim
= (struct rr_pco_use
*)((char *)rpm
+ len
);
178 /* init in6_rrenumreq fields */
179 irr
->irr_u_uselen
= rpu
->rpu_uselen
;
180 irr
->irr_u_keeplen
= rpu
->rpu_keeplen
;
181 irr
->irr_raf_mask_onlink
=
182 !!(rpu
->rpu_ramask
& ICMP6_RR_PCOUSE_RAFLAGS_ONLINK
);
183 irr
->irr_raf_mask_auto
=
184 !!(rpu
->rpu_ramask
& ICMP6_RR_PCOUSE_RAFLAGS_AUTO
);
185 irr
->irr_vltime
= ntohl(rpu
->rpu_vltime
);
186 irr
->irr_pltime
= ntohl(rpu
->rpu_pltime
);
187 irr
->irr_raf_onlink
=
188 (rpu
->rpu_raflags
& ICMP6_RR_PCOUSE_RAFLAGS_ONLINK
) == 0 ?
191 (rpu
->rpu_raflags
& ICMP6_RR_PCOUSE_RAFLAGS_AUTO
) == 0 ?
193 irr
->irr_rrf_decrvalid
=
194 (rpu
->rpu_flags
& ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME
) == 0 ?
196 irr
->irr_rrf_decrprefd
=
197 (rpu
->rpu_flags
& ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME
) == 0 ?
199 irr
->irr_useprefix
.sin6_len
= sizeof(irr
->irr_useprefix
);
200 irr
->irr_useprefix
.sin6_family
= AF_INET6
;
201 irr
->irr_useprefix
.sin6_addr
= rpu
->rpu_prefix
;
203 if (ioctl(s
, rrcmd2pco
[rpm
->rpm_code
], (caddr_t
)irr
) < 0 &&
204 errno
!= EADDRNOTAVAIL
)
205 syslog(LOG_ERR
, "<%s> ioctl: %s", __func__
,
208 /* very adhoc: should be rewritten */
209 if (rpm
->rpm_code
== RPM_PCO_CHANGE
&&
210 IN6_ARE_ADDR_EQUAL(&rpm
->rpm_prefix
, &rpu
->rpu_prefix
) &&
211 rpm
->rpm_matchlen
== rpu
->rpu_uselen
&&
212 rpu
->rpu_uselen
== rpu
->rpu_keeplen
) {
213 ifi
= if_indextoifinfo(ifindex
);
214 if (ifi
== NULL
|| ifi
->ifi_rainfo
== NULL
)
215 continue; /* non-advertising IF */
216 rai
= ifi
->ifi_rainfo
;
218 TAILQ_FOREACH(pfx
, &rai
->rai_prefix
, pfx_next
) {
221 if (prefix_match(&pfx
->pfx_prefix
,
222 pfx
->pfx_prefixlen
, &rpm
->rpm_prefix
,
223 rpm
->rpm_matchlen
)) {
224 /* change parameters */
225 pfx
->pfx_validlifetime
=
226 ntohl(rpu
->rpu_vltime
);
227 pfx
->pfx_preflifetime
=
228 ntohl(rpu
->rpu_pltime
);
229 if (irr
->irr_rrf_decrvalid
) {
230 clock_gettime(CLOCK_MONOTONIC_FAST
,
232 pfx
->pfx_vltimeexpire
=
234 pfx
->pfx_validlifetime
;
236 pfx
->pfx_vltimeexpire
= 0;
237 if (irr
->irr_rrf_decrprefd
) {
238 clock_gettime(CLOCK_MONOTONIC_FAST
,
240 pfx
->pfx_pltimeexpire
=
242 pfx
->pfx_preflifetime
;
244 pfx
->pfx_pltimeexpire
= 0;
252 * process a Prefix Control Operation(PCO).
253 * return 0 on success, 1 on failure
256 do_pco(struct icmp6_router_renum
*rr
, int len
, struct rr_pco_match
*rpm
)
259 struct in6_rrenumreq irr
;
262 if ((rr_pco_check(len
, rpm
) != 0))
265 if (s
== -1 && (s
= socket(AF_INET6
, SOCK_DGRAM
, 0)) < 0) {
266 syslog(LOG_ERR
, "<%s> socket: %s", __func__
,
271 memset(&irr
, 0, sizeof(irr
));
272 irr
.irr_origin
= PR_ORIG_RR
;
273 irr
.irr_m_len
= rpm
->rpm_matchlen
;
274 irr
.irr_m_minlen
= rpm
->rpm_minlen
;
275 irr
.irr_m_maxlen
= rpm
->rpm_maxlen
;
276 irr
.irr_matchprefix
.sin6_len
= sizeof(irr
.irr_matchprefix
);
277 irr
.irr_matchprefix
.sin6_family
= AF_INET6
;
278 irr
.irr_matchprefix
.sin6_addr
= rpm
->rpm_prefix
;
280 while (if_indextoname(++ifindex
, irr
.irr_name
)) {
281 ifi
= if_indextoifinfo(ifindex
);
283 syslog(LOG_ERR
, "<%s> ifindex not found.",
288 * if ICMP6_RR_FLAGS_FORCEAPPLY(A flag) is 0 and
289 * IFF_UP is off, the interface is not applied
291 if ((rr
->rr_flags
& ICMP6_RR_FLAGS_FORCEAPPLY
) == 0 &&
292 (ifi
->ifi_flags
& IFF_UP
) == 0)
294 /* TODO: interface scope check */
295 do_use_prefix(len
, rpm
, &irr
, ifindex
);
300 syslog(LOG_ERR
, "<%s> if_indextoname: %s", __func__
,
308 * call do_pco() for each Prefix Control Operations(PCOs) in a received
309 * Router Renumbering Command packet.
310 * return 0 on success, 1 on failure
313 do_rr(int len
, struct icmp6_router_renum
*rr
)
315 struct rr_pco_match
*rpm
;
318 lim
= (char *)rr
+ len
;
319 cp
= (char *)(rr
+ 1);
320 len
-= sizeof(struct icmp6_router_renum
);
322 update_ifinfo(&ifilist
, UPDATE_IFINFO_ALL
);
327 rpm
= (struct rr_pco_match
*)cp
;
328 if ((size_t)len
< sizeof(struct rr_pco_match
)) {
330 syslog(LOG_ERR
, "<%s> pkt too short. left len = %d. "
331 "garbage at end of pkt?", __func__
, len
);
334 rpmlen
= rpm
->rpm_len
<< 3;
338 if (do_pco(rr
, rpmlen
, rpm
)) {
339 syslog(LOG_WARNING
, "<%s> invalid PCO", __func__
);
352 * check validity of a router renumbering command packet
353 * return 0 on success, 1 on failure
356 rr_command_check(int len
, struct icmp6_router_renum
*rr
, struct in6_addr
*from
,
357 struct in6_addr
*dst
)
359 u_char ntopbuf
[INET6_ADDRSTRLEN
];
361 /* omit rr minimal length check. hope kernel have done it. */
362 /* rr_command length check */
363 if ((size_t)len
< (sizeof(struct icmp6_router_renum
) +
364 sizeof(struct rr_pco_match
))) {
365 syslog(LOG_ERR
, "<%s> rr_command len %d is too short",
370 /* destination check. only for multicast. omit unicast check. */
371 if (IN6_IS_ADDR_MULTICAST(dst
) && !IN6_IS_ADDR_MC_LINKLOCAL(dst
) &&
372 !IN6_IS_ADDR_MC_SITELOCAL(dst
)) {
373 syslog(LOG_ERR
, "<%s> dst mcast addr %s is illegal",
375 inet_ntop(AF_INET6
, dst
, ntopbuf
, sizeof(ntopbuf
)));
379 /* seqnum and segnum check */
380 if (rro
.rro_seqnum
> rr
->rr_seqnum
) {
382 "<%s> rcvd old seqnum %d from %s",
383 __func__
, (u_int32_t
)ntohl(rr
->rr_seqnum
),
384 inet_ntop(AF_INET6
, from
, ntopbuf
, sizeof(ntopbuf
)));
387 if (rro
.rro_seqnum
== rr
->rr_seqnum
&&
388 (rr
->rr_flags
& ICMP6_RR_FLAGS_TEST
) == 0 &&
389 RR_ISSET_SEGNUM(rro
.rro_segnum_bits
, rr
->rr_segnum
)) {
390 if ((rr
->rr_flags
& ICMP6_RR_FLAGS_REQRESULT
) != 0)
392 "<%s> rcvd duped segnum %d from %s",
393 __func__
, rr
->rr_segnum
, inet_ntop(AF_INET6
, from
,
394 ntopbuf
, sizeof(ntopbuf
)));
399 if (rro
.rro_seqnum
!= rr
->rr_seqnum
) {
400 /* then must be "<" */
402 /* init rro_segnum_bits */
403 memset(rro
.rro_segnum_bits
, 0,
404 sizeof(rro
.rro_segnum_bits
));
406 rro
.rro_seqnum
= rr
->rr_seqnum
;
412 rr_command_input(int len
, struct icmp6_router_renum
*rr
,
413 struct in6_addr
*from
, struct in6_addr
*dst
)
415 /* rr_command validity check */
416 if (rr_command_check(len
, rr
, from
, dst
))
418 if ((rr
->rr_flags
& (ICMP6_RR_FLAGS_TEST
|ICMP6_RR_FLAGS_REQRESULT
)) ==
422 /* do router renumbering */
427 RR_SET_SEGNUM(rro
.rro_segnum_bits
, rr
->rr_segnum
);
432 syslog(LOG_ERR
, "<%s> received RR was invalid", __func__
);
437 rr_input(int len
, struct icmp6_router_renum
*rr
, struct in6_pktinfo
*pi
,
438 struct sockaddr_in6
*from
, struct in6_addr
*dst
)
440 u_char ntopbuf
[2][INET6_ADDRSTRLEN
], ifnamebuf
[IFNAMSIZ
];
443 "<%s> RR received from %s to %s on %s",
445 inet_ntop(AF_INET6
, &from
->sin6_addr
, ntopbuf
[0] ,sizeof(ntopbuf
[0])),
446 inet_ntop(AF_INET6
, &dst
, ntopbuf
[1], sizeof(ntopbuf
[1])),
447 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
449 /* packet validation based on Section 4.1 of RFC2894 */
450 if ((size_t)len
< sizeof(struct icmp6_router_renum
)) {
452 "<%s>: RR short message (size %d) from %s to %s on %s",
454 inet_ntop(AF_INET6
, &from
->sin6_addr
, ntopbuf
[0],
456 inet_ntop(AF_INET6
, &dst
, ntopbuf
[1], sizeof(ntopbuf
[1])),
457 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
462 * If the IPv6 destination address is neither an All Routers multicast
463 * address [AARCH] nor one of the receiving router's unicast addresses,
464 * the message MUST be discarded and SHOULD be logged to network
466 * We rely on the kernel input routine for unicast addresses, and thus
467 * check multicast destinations only.
469 if (IN6_IS_ADDR_MULTICAST(&pi
->ipi6_addr
) && !IN6_ARE_ADDR_EQUAL(
470 &sin6_sitelocal_allrouters
.sin6_addr
, &pi
->ipi6_addr
)) {
472 "<%s>: RR message with invalid destination (%s) "
475 inet_ntop(AF_INET6
, &dst
, ntopbuf
[0], sizeof(ntopbuf
[0])),
476 inet_ntop(AF_INET6
, &from
->sin6_addr
, ntopbuf
[1],
478 if_indextoname(pi
->ipi6_ifindex
, ifnamebuf
));
482 rr_rcvifindex
= pi
->ipi6_ifindex
;
484 switch (rr
->rr_code
) {
485 case ICMP6_ROUTER_RENUMBERING_COMMAND
:
486 rr_command_input(len
, rr
, &from
->sin6_addr
, dst
);
487 /* TODO: send reply msg */
489 case ICMP6_ROUTER_RENUMBERING_RESULT
:
490 /* RESULT will be processed by rrenumd */
492 case ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET
:
493 /* TODO: sequence number reset */
496 syslog(LOG_ERR
, "<%s> received unknown code %d",
497 __func__
, rr
->rr_code
);