xref: /freebsd/usr.sbin/rtadvd/rrenum.c (revision f11c7f63056671247335df83a3fe80b94c6616ac)
1 /*	$FreeBSD$	*/
2 /*	$KAME: rrenum.c,v 1.12 2002/06/10 19:59:47 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
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>
37 
38 #include <net/if.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>
45 
46 #include <arpa/inet.h>
47 
48 #include <errno.h>
49 #include <netdb.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <syslog.h>
53 #include "rtadvd.h"
54 #include "rrenum.h"
55 #include "if.h"
56 
57 #define	RR_ISSET_SEGNUM(segnum_bits, segnum) \
58 	((((segnum_bits)[(segnum) >> 5]) & (1 << ((segnum) & 31))) != 0)
59 #define	RR_SET_SEGNUM(segnum_bits, segnum) \
60 	(((segnum_bits)[(segnum) >> 5]) |= (1 << ((segnum) & 31)))
61 
62 struct rr_operation {
63 	u_long	rro_seqnum;
64 	u_long	rro_segnum_bits[8];
65 };
66 
67 static struct rr_operation rro;
68 static int rr_rcvifindex;
69 static int rrcmd2pco[RPM_PCO_MAX] = {
70 	0,
71 	SIOCAIFPREFIX_IN6,
72 	SIOCCIFPREFIX_IN6,
73 	SIOCSGIFPREFIX_IN6
74 };
75 static int s = -1;
76 
77 /*
78  * Check validity of a Prefix Control Operation(PCO).
79  * return 0 on success, 1 on failure.
80  */
81 static int
82 rr_pco_check(int len, struct rr_pco_match *rpm)
83 {
84 	struct rr_pco_use *rpu, *rpulim;
85 	int checklen;
86 
87 	/* rpm->rpm_len must be (4N * 3) as router-renum-05.txt */
88 	if ((rpm->rpm_len - 3) < 0 || /* must be at least 3 */
89 	    (rpm->rpm_len - 3) & 0x3) { /* must be multiple of 4 */
90 		syslog(LOG_WARNING, "<%s> rpm_len %d is not 4N * 3",
91 		    __func__, rpm->rpm_len);
92 		return (1);
93 	}
94 	/* rpm->rpm_code must be valid value */
95 	switch (rpm->rpm_code) {
96 	case RPM_PCO_ADD:
97 	case RPM_PCO_CHANGE:
98 	case RPM_PCO_SETGLOBAL:
99 		break;
100 	default:
101 		syslog(LOG_WARNING, "<%s> unknown rpm_code %d", __func__,
102 		    rpm->rpm_code);
103 		return (1);
104 	}
105 	/* rpm->rpm_matchlen must be 0 to 128 inclusive */
106 	if (rpm->rpm_matchlen > 128) {
107 		syslog(LOG_WARNING, "<%s> rpm_matchlen %d is over 128",
108 		    __func__, rpm->rpm_matchlen);
109 		return (1);
110 	}
111 
112 	/*
113 	 * rpu->rpu_uselen, rpu->rpu_keeplen, and sum of them must be
114 	 * between 0 and 128 inclusive
115 	 */
116 	for (rpu = (struct rr_pco_use *)(rpm + 1),
117 	     rpulim = (struct rr_pco_use *)((char *)rpm + len);
118 	     rpu < rpulim;
119 	     rpu += 1) {
120 		checklen = rpu->rpu_uselen;
121 		checklen += rpu->rpu_keeplen;
122 		/*
123 		 * omit these check, because either of rpu_uselen
124 		 * and rpu_keeplen is unsigned char
125 		 *  (128 > rpu_uselen > 0)
126 		 *  (128 > rpu_keeplen > 0)
127 		 *  (rpu_uselen + rpu_keeplen > 0)
128 		 */
129 		if (checklen > 128) {
130 			syslog(LOG_WARNING, "<%s> sum of rpu_uselen %d and"
131 			    " rpu_keeplen %d is %d(over 128)",
132 			    __func__, rpu->rpu_uselen, rpu->rpu_keeplen,
133 			    rpu->rpu_uselen + rpu->rpu_keeplen);
134 			return (1);
135 		}
136 	}
137 	return (0);
138 }
139 
140 static void
141 do_use_prefix(int len, struct rr_pco_match *rpm,
142 	struct in6_rrenumreq *irr, int ifindex)
143 {
144 	struct rr_pco_use *rpu, *rpulim;
145 	struct rainfo *rai;
146 	struct ifinfo *ifi;
147 	struct prefix *pfx;
148 
149 	rpu = (struct rr_pco_use *)(rpm + 1);
150 	rpulim = (struct rr_pco_use *)((char *)rpm + len);
151 
152 	if (rpu == rpulim) {	/* no use prefix */
153 		if (rpm->rpm_code == RPM_PCO_ADD)
154 			return;
155 
156 		irr->irr_u_uselen = 0;
157 		irr->irr_u_keeplen = 0;
158 		irr->irr_raf_mask_onlink = 0;
159 		irr->irr_raf_mask_auto = 0;
160 		irr->irr_vltime = 0;
161 		irr->irr_pltime = 0;
162 		memset(&irr->irr_flags, 0, sizeof(irr->irr_flags));
163 		irr->irr_useprefix.sin6_len = 0; /* let it mean, no addition */
164 		irr->irr_useprefix.sin6_family = 0;
165 		irr->irr_useprefix.sin6_addr = in6addr_any;
166 		if (ioctl(s, rrcmd2pco[rpm->rpm_code], (caddr_t)irr) < 0 &&
167 		    errno != EADDRNOTAVAIL)
168 			syslog(LOG_ERR, "<%s> ioctl: %s", __func__,
169 			    strerror(errno));
170 		return;
171 	}
172 
173 	for (rpu = (struct rr_pco_use *)(rpm + 1),
174 	     rpulim = (struct rr_pco_use *)((char *)rpm + len);
175 	     rpu < rpulim;
176 	     rpu += 1) {
177 		/* init in6_rrenumreq fields */
178 		irr->irr_u_uselen = rpu->rpu_uselen;
179 		irr->irr_u_keeplen = rpu->rpu_keeplen;
180 		irr->irr_raf_mask_onlink =
181 		    !!(rpu->rpu_ramask & ICMP6_RR_PCOUSE_RAFLAGS_ONLINK);
182 		irr->irr_raf_mask_auto =
183 		    !!(rpu->rpu_ramask & ICMP6_RR_PCOUSE_RAFLAGS_AUTO);
184 		irr->irr_vltime = ntohl(rpu->rpu_vltime);
185 		irr->irr_pltime = ntohl(rpu->rpu_pltime);
186 		irr->irr_raf_onlink =
187 		    (rpu->rpu_raflags & ICMP6_RR_PCOUSE_RAFLAGS_ONLINK) == 0 ?
188 		    0 : 1;
189 		irr->irr_raf_auto =
190 		    (rpu->rpu_raflags & ICMP6_RR_PCOUSE_RAFLAGS_AUTO) == 0 ?
191 		    0 : 1;
192 		irr->irr_rrf_decrvalid =
193 		    (rpu->rpu_flags & ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME) == 0 ?
194 		    0 : 1;
195 		irr->irr_rrf_decrprefd =
196 		    (rpu->rpu_flags & ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME) == 0 ?
197 		    0 : 1;
198 		irr->irr_useprefix.sin6_len = sizeof(irr->irr_useprefix);
199 		irr->irr_useprefix.sin6_family = AF_INET6;
200 		irr->irr_useprefix.sin6_addr = rpu->rpu_prefix;
201 
202 		if (ioctl(s, rrcmd2pco[rpm->rpm_code], (caddr_t)irr) < 0 &&
203 		    errno != EADDRNOTAVAIL)
204 			syslog(LOG_ERR, "<%s> ioctl: %s", __func__,
205 			    strerror(errno));
206 
207 		/* very adhoc: should be rewritten */
208 		if (rpm->rpm_code == RPM_PCO_CHANGE &&
209 		    IN6_ARE_ADDR_EQUAL(&rpm->rpm_prefix, &rpu->rpu_prefix) &&
210 		    rpm->rpm_matchlen == rpu->rpu_uselen &&
211 		    rpu->rpu_uselen == rpu->rpu_keeplen) {
212 			ifi = if_indextoifinfo(ifindex);
213 			if (ifi == NULL || ifi->ifi_rainfo == NULL)
214 				continue; /* non-advertising IF */
215 			rai = ifi->ifi_rainfo;
216 
217 			TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
218 				struct timeval now;
219 
220 				if (prefix_match(&pfx->pfx_prefix,
221 				    pfx->pfx_prefixlen, &rpm->rpm_prefix,
222 				    rpm->rpm_matchlen)) {
223 					/* change parameters */
224 					pfx->pfx_validlifetime =
225 					    ntohl(rpu->rpu_vltime);
226 					pfx->pfx_preflifetime =
227 					    ntohl(rpu->rpu_pltime);
228 					if (irr->irr_rrf_decrvalid) {
229 						gettimeofday(&now, 0);
230 						pfx->pfx_vltimeexpire =
231 						    now.tv_sec +
232 						    pfx->pfx_validlifetime;
233 					} else
234 						pfx->pfx_vltimeexpire = 0;
235 					if (irr->irr_rrf_decrprefd) {
236 						gettimeofday(&now, 0);
237 						pfx->pfx_pltimeexpire =
238 						    now.tv_sec +
239 						    pfx->pfx_preflifetime;
240 					} else
241 						pfx->pfx_pltimeexpire = 0;
242 				}
243 			}
244 		}
245 	}
246 }
247 
248 /*
249  * process a Prefix Control Operation(PCO).
250  * return 0 on success, 1 on failure
251  */
252 static int
253 do_pco(struct icmp6_router_renum *rr, int len, struct rr_pco_match *rpm)
254 {
255 	int ifindex = 0;
256 	struct in6_rrenumreq irr;
257 	struct ifinfo *ifi;
258 
259 	if ((rr_pco_check(len, rpm) != 0))
260 		return (1);
261 
262 	if (s == -1 && (s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
263 		syslog(LOG_ERR, "<%s> socket: %s", __func__,
264 		    strerror(errno));
265 		exit(1);
266 	}
267 
268 	memset(&irr, 0, sizeof(irr));
269 	irr.irr_origin = PR_ORIG_RR;
270 	irr.irr_m_len = rpm->rpm_matchlen;
271 	irr.irr_m_minlen = rpm->rpm_minlen;
272 	irr.irr_m_maxlen = rpm->rpm_maxlen;
273 	irr.irr_matchprefix.sin6_len = sizeof(irr.irr_matchprefix);
274 	irr.irr_matchprefix.sin6_family = AF_INET6;
275 	irr.irr_matchprefix.sin6_addr = rpm->rpm_prefix;
276 
277 	while (if_indextoname(++ifindex, irr.irr_name)) {
278 		ifi = if_indextoifinfo(ifindex);
279 		if (ifi == NULL) {
280 			syslog(LOG_ERR, "<%s> ifindex not found.",
281 			    __func__);
282 			return (1);
283 		}
284 		/*
285 		 * if ICMP6_RR_FLAGS_FORCEAPPLY(A flag) is 0 and
286 		 * IFF_UP is off, the interface is not applied
287 		 */
288 		if ((rr->rr_flags & ICMP6_RR_FLAGS_FORCEAPPLY) == 0 &&
289 		    (ifi->ifi_flags & IFF_UP) == 0)
290 			continue;
291 		/* TODO: interface scope check */
292 		do_use_prefix(len, rpm, &irr, ifindex);
293 	}
294 	if (errno == ENXIO)
295 		return (0);
296 	else if (errno) {
297 		syslog(LOG_ERR, "<%s> if_indextoname: %s", __func__,
298 		    strerror(errno));
299 		return (1);
300 	}
301 	return (0);
302 }
303 
304 /*
305  * call do_pco() for each Prefix Control Operations(PCOs) in a received
306  * Router Renumbering Command packet.
307  * return 0 on success, 1 on failure
308  */
309 static int
310 do_rr(int len, struct icmp6_router_renum *rr)
311 {
312 	struct rr_pco_match *rpm;
313 	char *cp, *lim;
314 
315 	lim = (char *)rr + len;
316 	cp = (char *)(rr + 1);
317 	len -= sizeof(struct icmp6_router_renum);
318 
319 	update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
320 
321 	while (cp < lim) {
322 		int rpmlen;
323 
324 		rpm = (struct rr_pco_match *)cp;
325 		if ((size_t)len < sizeof(struct rr_pco_match)) {
326 		    tooshort:
327 			syslog(LOG_ERR, "<%s> pkt too short. left len = %d. "
328 			    "gabage at end of pkt?", __func__, len);
329 			return (1);
330 		}
331 		rpmlen = rpm->rpm_len << 3;
332 		if (len < rpmlen)
333 			goto tooshort;
334 
335 		if (do_pco(rr, rpmlen, rpm)) {
336 			syslog(LOG_WARNING, "<%s> invalid PCO", __func__);
337 			goto next;
338 		}
339 
340 	    next:
341 		cp += rpmlen;
342 		len -= rpmlen;
343 	}
344 
345 	return (0);
346 }
347 
348 /*
349  * check validity of a router renumbering command packet
350  * return 0 on success, 1 on failure
351  */
352 static int
353 rr_command_check(int len, struct icmp6_router_renum *rr, struct in6_addr *from,
354 	struct in6_addr *dst)
355 {
356 	u_char ntopbuf[INET6_ADDRSTRLEN];
357 
358 	/* omit rr minimal length check. hope kernel have done it. */
359 	/* rr_command length check */
360 	if ((size_t)len < (sizeof(struct icmp6_router_renum) +
361 	    sizeof(struct rr_pco_match))) {
362 		syslog(LOG_ERR,	"<%s> rr_command len %d is too short",
363 		    __func__, len);
364 		return (1);
365 	}
366 
367 	/* destination check. only for multicast. omit unicast check. */
368 	if (IN6_IS_ADDR_MULTICAST(dst) && !IN6_IS_ADDR_MC_LINKLOCAL(dst) &&
369 	    !IN6_IS_ADDR_MC_SITELOCAL(dst)) {
370 		syslog(LOG_ERR,	"<%s> dst mcast addr %s is illegal",
371 		    __func__,
372 		    inet_ntop(AF_INET6, dst, ntopbuf, sizeof(ntopbuf)));
373 		return (1);
374 	}
375 
376 	/* seqnum and segnum check */
377 	if (rro.rro_seqnum > rr->rr_seqnum) {
378 		syslog(LOG_WARNING,
379 		    "<%s> rcvd old seqnum %d from %s",
380 		    __func__, (u_int32_t)ntohl(rr->rr_seqnum),
381 		   inet_ntop(AF_INET6, from, ntopbuf, sizeof(ntopbuf)));
382 		return (1);
383 	}
384 	if (rro.rro_seqnum == rr->rr_seqnum &&
385 	    (rr->rr_flags & ICMP6_RR_FLAGS_TEST) == 0 &&
386 	    RR_ISSET_SEGNUM(rro.rro_segnum_bits, rr->rr_segnum)) {
387 		if ((rr->rr_flags & ICMP6_RR_FLAGS_REQRESULT) != 0)
388 			syslog(LOG_WARNING,
389 			    "<%s> rcvd duped segnum %d from %s",
390 			    __func__, rr->rr_segnum, inet_ntop(AF_INET6, from,
391 				ntopbuf, sizeof(ntopbuf)));
392 		return (0);
393 	}
394 
395 	/* update seqnum */
396 	if (rro.rro_seqnum != rr->rr_seqnum) {
397 		/* then must be "<" */
398 
399 		/* init rro_segnum_bits */
400 		memset(rro.rro_segnum_bits, 0,
401 		    sizeof(rro.rro_segnum_bits));
402 	}
403 	rro.rro_seqnum = rr->rr_seqnum;
404 
405 	return (0);
406 }
407 
408 static void
409 rr_command_input(int len, struct icmp6_router_renum *rr,
410 	struct in6_addr *from, struct in6_addr *dst)
411 {
412 	/* rr_command validity check */
413 	if (rr_command_check(len, rr, from, dst))
414 		goto failed;
415 	if ((rr->rr_flags & (ICMP6_RR_FLAGS_TEST|ICMP6_RR_FLAGS_REQRESULT)) ==
416 	    ICMP6_RR_FLAGS_TEST)
417 		return;
418 
419 	/* do router renumbering */
420 	if (do_rr(len, rr))
421 		goto failed;
422 
423 	/* update segnum */
424 	RR_SET_SEGNUM(rro.rro_segnum_bits, rr->rr_segnum);
425 
426 	return;
427 
428     failed:
429 	syslog(LOG_ERR, "<%s> received RR was invalid", __func__);
430 	return;
431 }
432 
433 void
434 rr_input(int len, struct icmp6_router_renum *rr, struct in6_pktinfo *pi,
435 	struct sockaddr_in6 *from, struct in6_addr *dst)
436 {
437 	u_char ntopbuf[2][INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
438 
439 	syslog(LOG_DEBUG,
440 	    "<%s> RR received from %s to %s on %s",
441 	    __func__,
442 	    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf[0] ,sizeof(ntopbuf[0])),
443 	    inet_ntop(AF_INET6, &dst, ntopbuf[1], sizeof(ntopbuf[1])),
444 	    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
445 
446 	/* packet validation based on Section 4.1 of RFC2894 */
447 	if ((size_t)len < sizeof(struct icmp6_router_renum)) {
448 		syslog(LOG_NOTICE,
449 		    "<%s>: RR short message (size %d) from %s to %s on %s",
450 		    __func__, len,
451 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf[0],
452 			sizeof(ntopbuf[0])),
453 		    inet_ntop(AF_INET6, &dst, ntopbuf[1], sizeof(ntopbuf[1])),
454 		    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
455 		return;
456 	}
457 
458 	/*
459 	 * If the IPv6 destination address is neither an All Routers multicast
460 	 * address [AARCH] nor one of the receiving router's unicast addresses,
461 	 * the message MUST be discarded and SHOULD be logged to network
462 	 * management.
463 	 * We rely on the kernel input routine for unicast addresses, and thus
464 	 * check multicast destinations only.
465 	 */
466 	if (IN6_IS_ADDR_MULTICAST(&pi->ipi6_addr) && !IN6_ARE_ADDR_EQUAL(
467 	    &sin6_sitelocal_allrouters.sin6_addr, &pi->ipi6_addr)) {
468 		syslog(LOG_NOTICE,
469 		    "<%s>: RR message with invalid destination (%s) "
470 		    "from %s on %s",
471 		    __func__,
472 		    inet_ntop(AF_INET6, &dst, ntopbuf[0], sizeof(ntopbuf[0])),
473 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf[1],
474 			      sizeof(ntopbuf[1])),
475 		    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
476 		return;
477 	}
478 
479 	rr_rcvifindex = pi->ipi6_ifindex;
480 
481 	switch (rr->rr_code) {
482 	case ICMP6_ROUTER_RENUMBERING_COMMAND:
483 		rr_command_input(len, rr, &from->sin6_addr, dst);
484 		/* TODO: send reply msg */
485 		break;
486 	case ICMP6_ROUTER_RENUMBERING_RESULT:
487 		/* RESULT will be processed by rrenumd */
488 		break;
489 	case ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET:
490 		/* TODO: sequence number reset */
491 		break;
492 	default:
493 		syslog(LOG_ERR,	"<%s> received unknown code %d",
494 		    __func__, rr->rr_code);
495 		break;
496 
497 	}
498 
499 	return;
500 }
501