xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/in.ripngd/main.c (revision 351:7f8801c20e08)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*351Spj157603  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include "defs.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate struct		sockaddr_in6 allrouters;
400Sstevel@tonic-gate char		*control;
410Sstevel@tonic-gate boolean_t	dopoison = _B_TRUE;	/* Do poison reverse */
420Sstevel@tonic-gate int		iocsoc;
430Sstevel@tonic-gate struct		timeval lastfullupdate;	/* last time full table multicast */
440Sstevel@tonic-gate struct		timeval lastmcast;	/* last time all/changes multicast */
450Sstevel@tonic-gate int		max_poll_ifs = START_POLL_SIZE;
460Sstevel@tonic-gate struct		rip6 *msg;
470Sstevel@tonic-gate boolean_t	needupdate;		/* true if need update at nextmcast */
480Sstevel@tonic-gate struct		timeval nextmcast;	/* time to wait before changes mcast */
490Sstevel@tonic-gate struct		timeval now;		/* current idea of time */
500Sstevel@tonic-gate char		*packet;
510Sstevel@tonic-gate struct		pollfd *poll_ifs = NULL;
520Sstevel@tonic-gate int		poll_ifs_num = 0;
530Sstevel@tonic-gate int		rip6_port;
540Sstevel@tonic-gate boolean_t	supplier = _B_TRUE;	/* process should supply updates */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate struct		in6_addr allrouters_in6 = {
570Sstevel@tonic-gate /* BEGIN CSTYLED */
580Sstevel@tonic-gate     { 0xff, 0x2, 0x0, 0x0,
590Sstevel@tonic-gate       0x0,  0x0, 0x0, 0x0,
600Sstevel@tonic-gate       0x0,  0x0, 0x0, 0x0,
610Sstevel@tonic-gate       0x0,  0x0, 0x0, 0x9 }
620Sstevel@tonic-gate /* END CSTYLED */
630Sstevel@tonic-gate };
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static void	timevalsub(struct timeval *t1, struct timeval *t2);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static void
usage(char * fname)680Sstevel@tonic-gate usage(char *fname)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 	(void) fprintf(stderr,
710Sstevel@tonic-gate 	    "usage: "
720Sstevel@tonic-gate 	    "%s [ -P ] [ -p port ] [ -q ] [ -s ] [ -t ] [ -v ] [<logfile>]\n",
730Sstevel@tonic-gate 	    fname);
740Sstevel@tonic-gate 	exit(EXIT_FAILURE);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
77*351Spj157603 int
main(int argc,char * argv[])780Sstevel@tonic-gate main(int argc, char *argv[])
790Sstevel@tonic-gate {
800Sstevel@tonic-gate 	int i, n;
810Sstevel@tonic-gate 	struct interface *ifp;
820Sstevel@tonic-gate 	int c;
830Sstevel@tonic-gate 	struct timeval waittime;
840Sstevel@tonic-gate 	int timeout;
850Sstevel@tonic-gate 	boolean_t daemon = _B_TRUE;	/* Fork off a detached daemon */
860Sstevel@tonic-gate 	FILE *pidfp;
870Sstevel@tonic-gate 	mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	rip6_port = htons(IPPORT_ROUTESERVER6);
900Sstevel@tonic-gate 	allrouters.sin6_family = AF_INET6;
910Sstevel@tonic-gate 	allrouters.sin6_port = rip6_port;
920Sstevel@tonic-gate 	allrouters.sin6_addr = allrouters_in6;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "nsqvTtdgPp:")) != EOF) {
950Sstevel@tonic-gate 		switch (c) {
960Sstevel@tonic-gate 		case 'n':
970Sstevel@tonic-gate 			install = _B_FALSE;
980Sstevel@tonic-gate 			break;
990Sstevel@tonic-gate 		case 's':
1000Sstevel@tonic-gate 			supplier = _B_TRUE;
1010Sstevel@tonic-gate 			break;
1020Sstevel@tonic-gate 		case 'q':
1030Sstevel@tonic-gate 			supplier = _B_FALSE;
1040Sstevel@tonic-gate 			break;
1050Sstevel@tonic-gate 		case 'v':
1060Sstevel@tonic-gate 			tracing |= ACTION_BIT;
1070Sstevel@tonic-gate 			break;
1080Sstevel@tonic-gate 		case 'T':
1090Sstevel@tonic-gate 			daemon = _B_FALSE;
1100Sstevel@tonic-gate 			break;
1110Sstevel@tonic-gate 		case 't':
1120Sstevel@tonic-gate 			tracepackets = _B_TRUE;
1130Sstevel@tonic-gate 			daemon = _B_FALSE;
1140Sstevel@tonic-gate 			tracing |= (INPUT_BIT | OUTPUT_BIT);
1150Sstevel@tonic-gate 			break;
1160Sstevel@tonic-gate 		case 'd':
1170Sstevel@tonic-gate 			break;
1180Sstevel@tonic-gate 		case 'P':
1190Sstevel@tonic-gate 			dopoison = _B_FALSE;
1200Sstevel@tonic-gate 			break;
1210Sstevel@tonic-gate 		case 'p':
1220Sstevel@tonic-gate 			rip6_port = htons(atoi(optarg));
1230Sstevel@tonic-gate 			allrouters.sin6_port = rip6_port;
1240Sstevel@tonic-gate 			break;
1250Sstevel@tonic-gate 		default:
1260Sstevel@tonic-gate 			usage(argv[0]);
1270Sstevel@tonic-gate 			/* NOTREACHED */
1280Sstevel@tonic-gate 		}
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	/*
1320Sstevel@tonic-gate 	 * Any extra argument is considered
1330Sstevel@tonic-gate 	 * a tracing log file.
1340Sstevel@tonic-gate 	 */
1350Sstevel@tonic-gate 	if (optind < argc) {
1360Sstevel@tonic-gate 		traceon(argv[optind]);
1370Sstevel@tonic-gate 	} else if (tracing && !daemon) {
1380Sstevel@tonic-gate 		traceonfp(stdout);
1390Sstevel@tonic-gate 	} else if (tracing) {
1400Sstevel@tonic-gate 		(void) fprintf(stderr, "Need logfile with -v\n");
1410Sstevel@tonic-gate 		usage(argv[0]);
1420Sstevel@tonic-gate 		/* NOTREACHED */
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	if (daemon) {
1460Sstevel@tonic-gate 		int t;
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		if (fork())
1490Sstevel@tonic-gate 			exit(EXIT_SUCCESS);
1500Sstevel@tonic-gate 		for (t = 0; t < 20; t++) {
1510Sstevel@tonic-gate 			if (!tracing || (t != fileno(ftrace)))
1520Sstevel@tonic-gate 				(void) close(t);
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 		(void) open("/", 0);
1550Sstevel@tonic-gate 		(void) dup2(0, 1);
1560Sstevel@tonic-gate 		(void) dup2(0, 2);
1570Sstevel@tonic-gate 		(void) setsid();
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/* Store our process id, blow away any existing file if it exists. */
1610Sstevel@tonic-gate 	if ((pidfp = fopen(PATH_PID, "w")) == NULL) {
1620Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: unable to open " PATH_PID ": %s\n",
1630Sstevel@tonic-gate 		    argv[0], strerror(errno));
1640Sstevel@tonic-gate 	} else {
1650Sstevel@tonic-gate 		(void) fprintf(pidfp, "%ld\n", getpid());
1660Sstevel@tonic-gate 		(void) fclose(pidfp);
1670Sstevel@tonic-gate 		(void) chmod(PATH_PID, pidmode);
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	iocsoc = socket(AF_INET6, SOCK_DGRAM, 0);
1720Sstevel@tonic-gate 	if (iocsoc < 0) {
1730Sstevel@tonic-gate 		syslog(LOG_ERR, "main: socket: %m");
1740Sstevel@tonic-gate 		exit(EXIT_FAILURE);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	setup_rtsock();
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	/*
1800Sstevel@tonic-gate 	 * Allocate the buffer to hold the RIPng packet.  In reality, it will be
1810Sstevel@tonic-gate 	 * smaller than IPV6_MAX_PACKET octets due to (at least) the IPv6 and
1820Sstevel@tonic-gate 	 * UDP headers but IPV6_MAX_PACKET is a convenient size.
1830Sstevel@tonic-gate 	 */
1840Sstevel@tonic-gate 	packet = (char *)malloc(IPV6_MAX_PACKET);
1850Sstevel@tonic-gate 	if (packet == NULL) {
1860Sstevel@tonic-gate 		syslog(LOG_ERR, "main: malloc: %m");
1870Sstevel@tonic-gate 		exit(EXIT_FAILURE);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 	msg = (struct rip6 *)packet;
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	/*
1920Sstevel@tonic-gate 	 * Allocate the buffer to hold the ancillary data.  This data is used to
1930Sstevel@tonic-gate 	 * insure that the incoming hop count of a RIPCMD6_RESPONSE message is
1940Sstevel@tonic-gate 	 * IPV6_MAX_HOPS which indicates that it came from a direct neighbor
1950Sstevel@tonic-gate 	 * (namely, no intervening router decremented it).
1960Sstevel@tonic-gate 	 */
1970Sstevel@tonic-gate 	control = (char *)malloc(IPV6_MAX_PACKET);
1980Sstevel@tonic-gate 	if (control == NULL) {
1990Sstevel@tonic-gate 		syslog(LOG_ERR, "main: malloc: %m");
2000Sstevel@tonic-gate 		exit(EXIT_FAILURE);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	openlog("in.ripngd", LOG_PID | LOG_CONS, LOG_DAEMON);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	(void) gettimeofday(&now, (struct timezone *)NULL);
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 	initifs();
2080Sstevel@tonic-gate 	solicitall(&allrouters);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	if (supplier)
2110Sstevel@tonic-gate 		supplyall(&allrouters, 0, (struct interface *)NULL, _B_TRUE);
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	(void) sigset(SIGALRM, (void (*)(int))timer);
2140Sstevel@tonic-gate 	(void) sigset(SIGHUP, (void (*)(int))initifs);
2150Sstevel@tonic-gate 	(void) sigset(SIGTERM, (void (*)(int))term);
2160Sstevel@tonic-gate 	(void) sigset(SIGUSR1, (void (*)(int))if_dump);
2170Sstevel@tonic-gate 	(void) sigset(SIGUSR2, (void (*)(int))rtdump);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	/*
2200Sstevel@tonic-gate 	 * Seed the pseudo-random number generator for GET_RANDOM().
2210Sstevel@tonic-gate 	 */
2220Sstevel@tonic-gate 	srandom((uint_t)gethostid());
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	timer();
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	for (;;) {
2270Sstevel@tonic-gate 		if (needupdate) {
2280Sstevel@tonic-gate 			waittime = nextmcast;
2290Sstevel@tonic-gate 			timevalsub(&waittime, &now);
2300Sstevel@tonic-gate 			if (waittime.tv_sec < 0) {
2310Sstevel@tonic-gate 				timeout = 0;
2320Sstevel@tonic-gate 			} else {
2330Sstevel@tonic-gate 				timeout = TIME_TO_MSECS(waittime);
2340Sstevel@tonic-gate 			}
2350Sstevel@tonic-gate 			if (tracing & ACTION_BIT) {
2360Sstevel@tonic-gate 				(void) fprintf(ftrace,
2370Sstevel@tonic-gate 				    "poll until dynamic update in %d msec\n",
2380Sstevel@tonic-gate 				    timeout);
2390Sstevel@tonic-gate 				(void) fflush(ftrace);
2400Sstevel@tonic-gate 			}
2410Sstevel@tonic-gate 		} else {
2420Sstevel@tonic-gate 			timeout = INFTIM;
2430Sstevel@tonic-gate 		}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 		if ((n = poll(poll_ifs, poll_ifs_num, timeout)) < 0) {
2460Sstevel@tonic-gate 			if (errno == EINTR)
2470Sstevel@tonic-gate 				continue;
2480Sstevel@tonic-gate 			syslog(LOG_ERR, "main: poll: %m");
2490Sstevel@tonic-gate 			exit(EXIT_FAILURE);
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 		(void) sighold(SIGALRM);
2520Sstevel@tonic-gate 		(void) sighold(SIGHUP);
2530Sstevel@tonic-gate 		/*
2540Sstevel@tonic-gate 		 * Poll timed out.
2550Sstevel@tonic-gate 		 */
2560Sstevel@tonic-gate 		if (n == 0) {
2570Sstevel@tonic-gate 			if (needupdate) {
2580Sstevel@tonic-gate 				TRACE_ACTION("send delayed dynamic update",
2590Sstevel@tonic-gate 				    (struct rt_entry *)NULL);
2600Sstevel@tonic-gate 				(void) gettimeofday(&now,
2610Sstevel@tonic-gate 				    (struct timezone *)NULL);
2620Sstevel@tonic-gate 				supplyall(&allrouters, RTS_CHANGED,
2630Sstevel@tonic-gate 				    (struct interface *)NULL, _B_TRUE);
2640Sstevel@tonic-gate 				lastmcast = now;
2650Sstevel@tonic-gate 				needupdate = _B_FALSE;
2660Sstevel@tonic-gate 				nextmcast.tv_sec = 0;
2670Sstevel@tonic-gate 			}
2680Sstevel@tonic-gate 			(void) sigrelse(SIGHUP);
2690Sstevel@tonic-gate 			(void) sigrelse(SIGALRM);
2700Sstevel@tonic-gate 			continue;
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 		(void) gettimeofday(&now, (struct timezone *)NULL);
2730Sstevel@tonic-gate 		for (i = 0; i < poll_ifs_num; i++) {
2740Sstevel@tonic-gate 			/*
2750Sstevel@tonic-gate 			 * This case should never happen.
2760Sstevel@tonic-gate 			 */
2770Sstevel@tonic-gate 			if (poll_ifs[i].revents & POLLERR) {
2780Sstevel@tonic-gate 				syslog(LOG_ERR,
2790Sstevel@tonic-gate 				    "main: poll returned a POLLERR event");
2800Sstevel@tonic-gate 				continue;
2810Sstevel@tonic-gate 			}
2820Sstevel@tonic-gate 			if (poll_ifs[i].revents & POLLIN) {
2830Sstevel@tonic-gate 				for (ifp = ifnet; ifp != NULL;
2840Sstevel@tonic-gate 				    ifp = ifp->int_next) {
2850Sstevel@tonic-gate 					if (poll_ifs[i].fd == ifp->int_sock)
2860Sstevel@tonic-gate 						in_data(ifp);
2870Sstevel@tonic-gate 				}
2880Sstevel@tonic-gate 			}
2890Sstevel@tonic-gate 		}
2900Sstevel@tonic-gate 		(void) sigrelse(SIGHUP);
2910Sstevel@tonic-gate 		(void) sigrelse(SIGALRM);
2920Sstevel@tonic-gate 	}
293*351Spj157603 
294*351Spj157603 	return (0);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate void
timevaladd(struct timeval * t1,struct timeval * t2)2980Sstevel@tonic-gate timevaladd(struct timeval *t1, struct timeval *t2)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	t1->tv_sec += t2->tv_sec;
3010Sstevel@tonic-gate 	if ((t1->tv_usec += t2->tv_usec) > 1000000) {
3020Sstevel@tonic-gate 		t1->tv_sec++;
3030Sstevel@tonic-gate 		t1->tv_usec -= 1000000;
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate void
timevalsub(struct timeval * t1,struct timeval * t2)3080Sstevel@tonic-gate timevalsub(struct timeval *t1, struct timeval *t2)
3090Sstevel@tonic-gate {
3100Sstevel@tonic-gate 	t1->tv_sec -= t2->tv_sec;
3110Sstevel@tonic-gate 	if ((t1->tv_usec -= t2->tv_usec) < 0) {
3120Sstevel@tonic-gate 		t1->tv_sec--;
3130Sstevel@tonic-gate 		t1->tv_usec += 1000000;
3140Sstevel@tonic-gate 	}
3150Sstevel@tonic-gate }
316