rrenum.c revision 57852
1/*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/usr.sbin/rtadvd/rrenum.c 57852 2000-03-09 14:51:05Z shin $
30 */
31
32#include <sys/param.h>
33#include <sys/ioctl.h>
34#include <sys/socket.h>
35#include <sys/sysctl.h>
36
37#include <net/if.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>
43
44#include <arpa/inet.h>
45
46#include <errno.h>
47#include <string.h>
48#include <stdlib.h>
49#include <syslog.h>
50#include "rrenum.h"
51#include "if.h"
52
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)))
57
58struct rr_operation {
59	u_long	rro_seqnum;
60	u_long	rro_segnum_bits[8];
61};
62
63static struct	rr_operation rro;
64static int	rr_rcvifindex;
65static int	rrcmd2pco[4] = {0,
66			   SIOCAIFPREFIX_IN6,
67			   SIOCCIFPREFIX_IN6,
68			   SIOCSGIFPREFIX_IN6
69};
70static int	s;
71
72/*
73 * Check validity of a Prefix Control Operation(PCO).
74 * Return 0 on success, 1 on failure.
75 */
76static int
77rr_pco_check(int len, struct rr_pco_match *rpm)
78{
79	struct rr_pco_use *rpu, *rpulim;
80	int checklen;
81
82	/* rpm->rpm_len must be (4N * 3) as router-renum-05.txt */
83	if ((rpm->rpm_len - 3) < 0 || /* must be at least 3 */
84	    (rpm->rpm_len - 3) & 0x3) { /* must be multiple of 4 */
85		syslog(LOG_WARNING, "<%s> rpm_len %d is not 4N * 3",
86		       __FUNCTION__, rpm->rpm_len);
87		return 1;
88	}
89	/* rpm->rpm_code must be valid value */
90	switch(rpm->rpm_code) {
91	case RPM_PCO_ADD:
92	case RPM_PCO_CHANGE:
93	case RPM_PCO_SETGLOBAL:
94		break;
95	default:
96		syslog(LOG_WARNING, "<%s> unknown rpm_code %d", __FUNCTION__,
97		       rpm->rpm_code);
98		return 1;
99	}
100	/* rpm->rpm_matchlen must be 0 to 128 inclusive */
101	if (rpm->rpm_matchlen > 128) {
102		syslog(LOG_WARNING, "<%s> rpm_matchlen %d is over 128",
103		       __FUNCTION__, rpm->rpm_matchlen);
104		return 1;
105	}
106
107	/*
108	 * rpu->rpu_uselen, rpu->rpu_keeplen, and sum of them must be
109	 * between 0 and 128 inclusive
110	 */
111	for (rpu = (struct rr_pco_use *)(rpm + 1),
112	     rpulim = (struct rr_pco_use *)((char *)rpm + len);
113	     rpu < rpulim;
114	     rpu += 1) {
115		checklen = rpu->rpu_uselen;
116		checklen += rpu->rpu_keeplen;
117		/*
118		 * omit these check, because either of rpu_uselen
119		 * and rpu_keeplen is unsigned char
120		 *  (128 > rpu_uselen > 0)
121		 *  (128 > rpu_keeplen > 0)
122		 *  (rpu_uselen + rpu_keeplen > 0)
123		 */
124		if (checklen > 128) {
125			syslog(LOG_WARNING, "<%s> sum of rpu_uselen %d and"
126			       " rpu_keeplen %d is %d(over 128)",
127			       __FUNCTION__, rpu->rpu_uselen,
128			       rpu->rpu_keeplen,
129			       rpu->rpu_uselen + rpu->rpu_keeplen);
130			return 1;
131		}
132	}
133	return 0;
134}
135
136static void
137do_use_prefix(int len, struct rr_pco_match *rpm, struct in6_rrenumreq *irr) {
138	struct rr_pco_use *rpu, *rpulim;
139
140	rpu = (struct rr_pco_use *)(rpm + 1);
141	rpulim = (struct rr_pco_use *)((char *)rpm + len);
142
143	if (rpu == rpulim) {
144		if (rpm->rpm_code == RPM_PCO_ADD)
145			return;
146
147		irr->irr_u_uselen = 0;
148		irr->irr_u_keeplen = 0;
149		irr->irr_raf_mask_onlink = 0;
150		irr->irr_raf_mask_auto = 0;
151		irr->irr_vltime = 0;
152		irr->irr_pltime = 0;
153		memset(&irr->irr_flags, 0, sizeof(irr->irr_flags));
154		irr->irr_useprefix.sin6_len = 0; /* let it mean, no addition */
155		irr->irr_useprefix.sin6_family = 0;
156		irr->irr_useprefix.sin6_addr = in6addr_any;
157		if (ioctl(s, rrcmd2pco[rpm->rpm_code], (caddr_t)irr) < 0 &&
158		    errno != EADDRNOTAVAIL)
159			syslog(LOG_ERR, "<%s> ioctl: %s", __FUNCTION__,
160			       strerror(errno));
161		return;
162	}
163
164	for (rpu = (struct rr_pco_use *)(rpm + 1),
165	     rpulim = (struct rr_pco_use *)((char *)rpm + len);
166	     rpu < rpulim;
167	     rpu += 1) {
168		/* init in6_rrenumreq fields */
169		irr->irr_u_uselen = rpu->rpu_uselen;
170		irr->irr_u_keeplen = rpu->rpu_keeplen;
171		irr->irr_raf_mask_onlink =
172			(rpu->rpu_ramask & ICMP6_RR_PCOUSE_RAFLAGS_ONLINK);
173		irr->irr_raf_mask_auto =
174			(rpu->rpu_ramask & ICMP6_RR_PCOUSE_RAFLAGS_AUTO);
175		irr->irr_vltime = rpu->rpu_vltime;
176		irr->irr_pltime = rpu->rpu_pltime;
177		irr->irr_raf_onlink =
178			(rpu->rpu_raflags & ICMP6_RR_PCOUSE_RAFLAGS_ONLINK);
179		irr->irr_raf_auto =
180			(rpu->rpu_raflags & ICMP6_RR_PCOUSE_RAFLAGS_AUTO);
181		irr->irr_rrf_decrvalid =
182			(rpu->rpu_flags & ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME);
183		irr->irr_rrf_decrprefd =
184			(rpu->rpu_flags & ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME);
185		irr->irr_useprefix.sin6_len = sizeof(irr->irr_useprefix);
186		irr->irr_useprefix.sin6_family = AF_INET6;
187		irr->irr_useprefix.sin6_addr = rpu->rpu_prefix;
188
189		if (ioctl(s, rrcmd2pco[rpm->rpm_code], (caddr_t)irr) < 0 &&
190		    errno != EADDRNOTAVAIL)
191			syslog(LOG_ERR, "<%s> ioctl: %s", __FUNCTION__,
192			       strerror(errno));
193	}
194}
195
196/*
197 * process a Prefix Control Operation(PCO).
198 * return 0 on success, 1 on failure
199 */
200static int
201do_pco(struct icmp6_router_renum *rr, int len, struct rr_pco_match *rpm)
202{
203	int ifindex = 0;
204	struct in6_rrenumreq irr;
205
206	if ((rr_pco_check(len, rpm) != NULL))
207		return 1;
208
209	memset(&irr, 0, sizeof(irr));
210	irr.irr_origin = PR_ORIG_RR;
211	irr.irr_m_len = rpm->rpm_matchlen;
212	irr.irr_m_minlen = rpm->rpm_minlen;
213	irr.irr_m_maxlen = rpm->rpm_maxlen;
214	irr.irr_matchprefix.sin6_len = sizeof(irr.irr_matchprefix);
215	irr.irr_matchprefix.sin6_family = AF_INET6;
216	irr.irr_matchprefix.sin6_addr = rpm->rpm_prefix;
217
218	while (if_indextoname(++ifindex, irr.irr_name)) {
219		/*
220		 * if ICMP6_RR_FLAGS_FORCEAPPLY(A flag) is 0 and IFF_UP is off,
221		 * the interface is not applied
222		 */
223		if ((rr->rr_flags & ICMP6_RR_FLAGS_FORCEAPPLY) == 0 &&
224		    (iflist[ifindex]->ifm_flags & IFF_UP) == 0)
225			continue;
226		/* TODO: interface scope check */
227		do_use_prefix(len, rpm, &irr);
228	}
229	if (errno == ENXIO)
230		return 0;
231	else if (errno) {
232		syslog(LOG_ERR, "<%s> if_indextoname: %s", __FUNCTION__,
233		       strerror(errno));
234		return 1;
235	}
236	return 0;
237}
238
239/*
240 * call do_pco() for each Prefix Control Operations(PCOs) in a received
241 * Router Renumbering Command packet.
242 * return 0 on success, 1 on failure
243 */
244static int
245do_rr(int len, struct icmp6_router_renum *rr)
246{
247	struct rr_pco_match *rpm;
248	char *cp, *lim;
249
250	lim = (char *)rr + len;
251	cp = (char *)(rr + 1);
252	len -= sizeof(struct icmp6_router_renum);
253
254	/* get iflist block from kernel again, to get up-to-date information */
255	init_iflist();
256
257	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
258		syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
259		       strerror(errno));
260		exit(1);
261	}
262
263	while (cp < lim) {
264		int rpmlen;
265
266		rpm = (struct rr_pco_match *)cp;
267		if (len < sizeof(struct rr_pco_match)) {
268		    tooshort:
269			syslog(LOG_ERR, "<%s> pkt too short. left len = %d. "
270			       "gabage at end of pkt?", __FUNCTION__, len);
271			close(s);
272			return 1;
273		}
274		rpmlen = rpm->rpm_len << 3;
275		if (len < rpmlen)
276			goto tooshort;
277
278		if (do_pco(rr, rpmlen, rpm)) {
279			syslog(LOG_WARNING, "<%s> invalid PCO", __FUNCTION__);
280			goto next;
281		}
282
283	    next:
284		cp += rpmlen;
285		len -= rpmlen;
286	}
287	close(s);
288	return 0;
289}
290
291/*
292 * check validity of a router renumbering command packet
293 * return 0 on success, 1 on failure
294 */
295static int
296rr_command_check(int len, struct icmp6_router_renum *rr, struct in6_addr *from,
297		 struct in6_addr *dst)
298{
299	u_char ntopbuf[INET6_ADDRSTRLEN];
300
301	/* omit rr minimal length check. hope kernel have done it. */
302	/* rr_command length check */
303	if (len < (sizeof(struct icmp6_router_renum) +
304		   sizeof(struct rr_pco_match))) {
305		syslog(LOG_ERR,	"<%s> rr_command len %d is too short",
306		       __FUNCTION__, len);
307		return 1;
308	}
309
310	/* destination check. only for multicast. omit unicast check. */
311	if (IN6_IS_ADDR_MULTICAST(dst) && !IN6_IS_ADDR_MC_LINKLOCAL(dst) &&
312	    !IN6_IS_ADDR_MC_SITELOCAL(dst)) {
313		syslog(LOG_ERR,	"<%s> dst mcast addr %s is illegal",
314		       __FUNCTION__,
315		       inet_ntop(AF_INET6, dst, ntopbuf, INET6_ADDRSTRLEN));
316		return 1;
317	}
318
319	/* seqnum and segnum check */
320	if (rro.rro_seqnum > rr->rr_seqnum) {
321		syslog(LOG_WARNING,
322		       "<%s> rcvd old seqnum %d from %s",
323		       __FUNCTION__, (u_int32_t)ntohl(rr->rr_seqnum),
324		       inet_ntop(AF_INET6, from, ntopbuf, INET6_ADDRSTRLEN));
325		return 1;
326	}
327	if (rro.rro_seqnum == rr->rr_seqnum &&
328	    (rr->rr_flags & ICMP6_RR_FLAGS_TEST) == 0 &&
329	    RR_ISSET_SEGNUM(rro.rro_segnum_bits, rr->rr_segnum)) {
330		if ((rr->rr_flags & ICMP6_RR_FLAGS_REQRESULT) != 0)
331			syslog(LOG_WARNING,
332			       "<%s> rcvd duped segnum %d from %s",
333			       __FUNCTION__, rr->rr_segnum,
334			       inet_ntop(AF_INET6, from, ntopbuf,
335					 INET6_ADDRSTRLEN));
336		return 0;
337	}
338
339	/* update seqnum */
340	if (rro.rro_seqnum != rr->rr_seqnum) {
341		/* then must be "<" */
342
343		/* init rro_segnum_bits */
344		memset(rro.rro_segnum_bits, 0,
345		       sizeof(rro.rro_segnum_bits));
346	}
347	rro.rro_seqnum = rr->rr_seqnum;
348
349	return 0;
350}
351
352static void
353rr_command_input(int len, struct icmp6_router_renum *rr,
354		 struct in6_addr *from, struct in6_addr *dst)
355{
356	/* rr_command validity check */
357	if (rr_command_check(len, rr, from, dst))
358		goto failed;
359	if ((rr->rr_flags & (ICMP6_RR_FLAGS_TEST|ICMP6_RR_FLAGS_REQRESULT)) ==
360	    ICMP6_RR_FLAGS_TEST)
361		return;
362
363	/* do router renumbering */
364	if (do_rr(len, rr)) {
365		goto failed;
366	}
367
368	/* update segnum */
369	RR_SET_SEGNUM(rro.rro_segnum_bits, rr->rr_segnum);
370
371	return;
372
373    failed:
374	syslog(LOG_ERR, "<%s> received RR was invalid", __FUNCTION__);
375	return;
376}
377
378void
379rr_input(int len, struct icmp6_router_renum *rr, struct in6_pktinfo *pi,
380	 struct sockaddr_in6 *from, struct in6_addr *dst)
381{
382	u_char ntopbuf[2][INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
383
384	syslog(LOG_DEBUG,
385	       "<%s> RR received from %s to %s on %s",
386	       __FUNCTION__,
387	       inet_ntop(AF_INET6, &from->sin6_addr,
388			 ntopbuf[0], INET6_ADDRSTRLEN),
389	       inet_ntop(AF_INET6, &dst, ntopbuf[1], INET6_ADDRSTRLEN),
390	       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
391
392	rr_rcvifindex = pi->ipi6_ifindex;
393
394	/* TODO: some consistency check. */
395
396	switch (rr->rr_code) {
397	case ICMP6_ROUTER_RENUMBERING_COMMAND:
398		rr_command_input(len, rr, &from->sin6_addr, dst);
399		/* TODO: send reply msg */
400		break;
401	case ICMP6_ROUTER_RENUMBERING_RESULT:
402		/* RESULT will be processed by rrenumd */
403		break;
404	case ICMP6_ROUTER_RENUMBERING_SEQNUM_RESET:
405		/* TODO: sequence number reset */
406		break;
407	default:
408		syslog(LOG_ERR,	"<%s> received unknown code %d",
409		       __FUNCTION__, rr->rr_code);
410		break;
411
412	}
413
414	return;
415}
416