11558Srgrimes/*
21558Srgrimes * Copyright (c) 1983, 1988, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes *
291558Srgrimes *	@(#)defs.h	8.1 (Berkeley) 6/5/93
301558Srgrimes *
311558Srgrimes * $FreeBSD$
321558Srgrimes */
331558Srgrimes
341558Srgrimes#ifdef  sgi
3513171Swollman#ident "$FreeBSD$"
361558Srgrimes#endif
371558Srgrimes
381558Srgrimes/* Definitions for RIPv2 routing process.
391558Srgrimes *
401558Srgrimes * This code is based on the 4.4BSD `routed` daemon, with extensions to
4137907Scharnier * support:
421558Srgrimes *	RIPv2, including variable length subnet masks.
4337907Scharnier *	Router Discovery
4413171Swollman *	aggregate routes in the kernel tables.
4550476Speter *	aggregate advertised routes.
461558Srgrimes *	maintain spare routes for faster selection of another gateway
471558Srgrimes *		when the current gateway dies.
481558Srgrimes *	timers on routes with second granularity so that selection
491558Srgrimes *		of a new route does not wait 30-60 seconds.
501558Srgrimes *	tolerance of static routes.
511558Srgrimes *	tell the kernel hop counts
521558Srgrimes *	do not advertise if ipforwarding=0
531558Srgrimes *
541558Srgrimes * The vestigial support for other protocols has been removed.  There
551558Srgrimes * is no likelihood that IETF RIPv1 or RIPv2 will ever be used with
561558Srgrimes * other protocols.  The result is far smaller, faster, cleaner, and
571558Srgrimes * perhaps understandable.
5817046Sjulian *
5914092Swollman * The accumulation of special flags and kludges added over the many
601558Srgrimes * years have been simplified and integrated.
6114092Swollman */
621558Srgrimes
631558Srgrimes#include <assert.h>
641558Srgrimes#include <stdio.h>
6520287Swollman#include <netdb.h>
6620287Swollman#include <stdlib.h>
671558Srgrimes#include <unistd.h>
681558Srgrimes#include <errno.h>
691558Srgrimes#include <string.h>
701558Srgrimes#ifdef sgi
7113171Swollman#include <strings.h>
7220287Swollman#include <bstring.h>
731558Srgrimes#endif
741558Srgrimes#include <stdarg.h>
751558Srgrimes#include <syslog.h>
761558Srgrimes#include <time.h>
771558Srgrimes#include <sys/cdefs.h>
781558Srgrimes#include <sys/time.h>
791558Srgrimes#include <sys/types.h>
801558Srgrimes#include <sys/param.h>
811558Srgrimes#include <sys/ioctl.h>
821558Srgrimes#include <sys/sysctl.h>
831558Srgrimes#include <sys/socket.h>
841558Srgrimes#include <sys/queue.h>
851558Srgrimes#ifdef sgi
8617046Sjulian#define _USER_ROUTE_TREE
8714092Swollman#include <net/radix.h>
881558Srgrimes#else
8914092Swollman#include "radix.h"
901558Srgrimes#define UNUSED __attribute__((unused))
911558Srgrimes#define PATTRIB(f,l) __attribute__((format (printf,f,l)))
921558Srgrimes#endif
931558Srgrimes#include <net/if.h>
941558Srgrimes#include <net/route.h>
951558Srgrimes#include <net/if_dl.h>
961558Srgrimes#include <netinet/in.h>
971558Srgrimes#include <arpa/inet.h>
981558Srgrimes#define RIPVERSION RIPv2
991558Srgrimes#include <protocols/routed.h>
1001558Srgrimes
1011558Srgrimes#ifndef __RCSID
10217046Sjulian#define __RCSID(_s) static const char rcsid[] UNUSED = _s
10317046Sjulian#endif
1041558Srgrimes#ifndef __COPYRIGHT
1051558Srgrimes#define __COPYRIGHT(_s) static const char copyright[] UNUSED = _s
1061558Srgrimes#endif
1071558Srgrimes
1081558Srgrimes/* Type of an IP address.
1091558Srgrimes *	Some systems do not like to pass structures, so do not use in_addr.
11018286Sbde *	Some systems think a long has 64 bits, which would be a gross waste.
11113171Swollman * So define it here so it can be changed for the target system.
11218286Sbde * It should be defined somewhere netinet/in.h, but it is not.
1131558Srgrimes */
11413171Swollman#ifdef sgi
1151558Srgrimes#define naddr u_int32_t
1161558Srgrimes#elif defined (__NetBSD__)
11713171Swollman#define naddr u_int32_t
1181558Srgrimes#define _HAVE_SA_LEN
11937907Scharnier#define _HAVE_SIN_LEN
12013171Swollman#else
1211558Srgrimes#define naddr u_long
1221558Srgrimes#define _HAVE_SA_LEN
1231558Srgrimes#define _HAVE_SIN_LEN
1241558Srgrimes#endif
1251558Srgrimes
1261558Srgrimes#define DAY (24*60*60)
1271558Srgrimes#define NEVER DAY			/* a long time */
1281558Srgrimes#define EPOCH NEVER			/* bias time by this to avoid <0 */
1291558Srgrimes
1301558Srgrimes/* Scan the kernel regularly to see if any interfaces have appeared or been
1311558Srgrimes * turned off.  These must be less than STALE_TIME.
1321558Srgrimes */
1331558Srgrimes#define	CHECK_BAD_INTERVAL	5	/* when an interface is known bad */
1341558Srgrimes#define	CHECK_ACT_INTERVAL	30	/* when advertising */
1351558Srgrimes#define	CHECK_QUIET_INTERVAL	300	/* when not */
1361558Srgrimes
1371558Srgrimes#define LIM_SEC(s,l) ((s).tv_sec = MIN((s).tv_sec, (l)))
13837907Scharnier
1391558Srgrimes/* Metric used for fake default routes.  It ought to be 15, but when
1401558Srgrimes * processing advertised routes, previous versions of `routed` added
1411558Srgrimes * to the received metric and discarded the route if the total was 16
1421558Srgrimes * or larger.
1431558Srgrimes */
1441558Srgrimes#define FAKE_METRIC (HOPCNT_INFINITY-2)
1451558Srgrimes
1461558Srgrimes
1471558Srgrimes/* Router Discovery parameters */
1481558Srgrimes#ifndef sgi
1491558Srgrimes#define INADDR_ALLROUTERS_GROUP		0xe0000002  /* 224.0.0.2 */
1501558Srgrimes#endif
1511558Srgrimes#define	MaxMaxAdvertiseInterval		1800
1521558Srgrimes#define	MinMaxAdvertiseInterval		4
1531558Srgrimes#define	DefMaxAdvertiseInterval		600
1541558Srgrimes#define MIN_PreferenceLevel		0x80000000
1551558Srgrimes
1561558Srgrimes#define	MAX_INITIAL_ADVERT_INTERVAL	16
1571558Srgrimes#define	MAX_INITIAL_ADVERTS		3
1581558Srgrimes
1591558Srgrimes#define	MAX_SOLICITATION_DELAY		1
1601558Srgrimes#define	SOLICITATION_INTERVAL		3
1611558Srgrimes#define	MAX_SOLICITATIONS		3
1621558Srgrimes
1631558Srgrimes
1641558Srgrimes/* Bloated packet size for systems that simply add authentication to
1651558Srgrimes * full-sized packets
1661558Srgrimes */
1671558Srgrimes#define OVER_MAXPACKETSIZE (MAXPACKETSIZE+sizeof(struct netinfo)*2)
1681558Srgrimes/* typical packet buffers */
16913171Swollmanunion pkt_buf {
17019209Sfenner	char	packet[OVER_MAXPACKETSIZE*2];
1711558Srgrimes	struct	rip rip;
1721558Srgrimes};
1731558Srgrimes
1741558Srgrimes#define GNAME_LEN   64			/* assumed=64 in parms.c */
1751558Srgrimes/* bigger than IFNAMSIZ, with room for "external()" or "remote()" */
1761558Srgrimes#define IF_NAME_LEN (GNAME_LEN+15)
1771558Srgrimes
1781558Srgrimes/* No more routes than this, to protect ourself in case something goes
1791558Srgrimes * whacko and starts broadcasting zillions of bogus routes.
1801558Srgrimes */
1811558Srgrimes#define MAX_ROUTES  (128*1024)
1821558Srgrimesextern int total_routes;
1831558Srgrimes
1841558Srgrimes/* Main, daemon routing table structure
1851558Srgrimes */
1861558Srgrimesstruct rt_entry {
1871558Srgrimes	struct	radix_node rt_nodes[2];	/* radix tree glue */
1881558Srgrimes	u_int	rt_state;
1891558Srgrimes#	    define RS_IF	0x001	/* for network interface */
1901558Srgrimes#	    define RS_NET_INT	0x002	/* authority route */
1911558Srgrimes#	    define RS_NET_SYN	0x004	/* fake net route for subnet */
1921558Srgrimes#	    define RS_NO_NET_SYN (RS_LOCAL | RS_LOCAL | RS_IF)
1931558Srgrimes#	    define RS_SUBNET	0x008	/* subnet route from any source */
1941558Srgrimes#	    define RS_LOCAL	0x010	/* loopback for pt-to-pt */
1951558Srgrimes#	    define RS_MHOME	0x020	/* from -m */
1961558Srgrimes#	    define RS_STATIC	0x040	/* from the kernel */
1971558Srgrimes#	    define RS_RDISC     0x080	/* from router discovery */
1981558Srgrimes	struct sockaddr_in rt_dst_sock;
1991558Srgrimes	naddr   rt_mask;
2001558Srgrimes	struct rt_spare {
2011558Srgrimes	    struct interface *rts_ifp;
2021558Srgrimes	    naddr   rts_gate;		/* forward packets here */
2031558Srgrimes	    naddr   rts_router;		/* on the authority of this router */
2041558Srgrimes	    char    rts_metric;
2051558Srgrimes	    u_short rts_tag;
2061558Srgrimes	    time_t  rts_time;		/* timer to junk stale routes */
2071558Srgrimes	    u_int   rts_de_ag;		/* de-aggregation level */
2081558Srgrimes#define NUM_SPARES 4
2091558Srgrimes	} rt_spares[NUM_SPARES];
2101558Srgrimes	u_int	rt_seqno;		/* when last changed */
2111558Srgrimes	char	rt_poison_metric;	/* to notice maximum recently */
21213171Swollman	time_t	rt_poison_time;		/*	advertised metric */
2131558Srgrimes};
2141558Srgrimes#define rt_dst	    rt_dst_sock.sin_addr.s_addr
2151558Srgrimes#define rt_ifp	    rt_spares[0].rts_ifp
2161558Srgrimes#define rt_gate	    rt_spares[0].rts_gate
2171558Srgrimes#define rt_router   rt_spares[0].rts_router
2181558Srgrimes#define rt_metric   rt_spares[0].rts_metric
2191558Srgrimes#define rt_tag	    rt_spares[0].rts_tag
2201558Srgrimes#define rt_time	    rt_spares[0].rts_time
2211558Srgrimes#define rt_de_ag    rt_spares[0].rts_de_ag
22217046Sjulian
22317046Sjulian#define HOST_MASK	0xffffffff
22417046Sjulian#define RT_ISHOST(rt)	((rt)->rt_mask == HOST_MASK)
22514092Swollman
2261558Srgrimes/* age all routes that
2271558Srgrimes *	are not from -g, -m, or static routes from the kernel
2281558Srgrimes *	not unbroken interface routes
22914092Swollman *		but not broken interfaces
2301558Srgrimes *	nor non-passive, remote interfaces that are not aliases
2311558Srgrimes *		(i.e. remote & metric=0)
2321558Srgrimes */
2331558Srgrimes#define AGE_RT(rt_state,ifp) (0 == ((rt_state) & (RS_MHOME | RS_STATIC	    \
2341558Srgrimes						  | RS_NET_SYN | RS_RDISC)) \
2351558Srgrimes			      && (!((rt_state) & RS_IF)			    \
2361558Srgrimes				  || (ifp) == 0				    \
2371558Srgrimes				  || (((ifp)->int_state & IS_REMOTE)	    \
2381558Srgrimes				      && !((ifp)->int_state & IS_PASSIVE))))
2391558Srgrimes
2401558Srgrimes/* true if A is better than B
2411558Srgrimes * Better if
2421558Srgrimes *	- A is not a poisoned route
2431558Srgrimes *	- and A is not stale
2441558Srgrimes *	- and A has a shorter path
24513171Swollman *		- or is the router speaking for itself
2461558Srgrimes *		- or the current route is equal but stale
24737907Scharnier *		- or it is a host route advertised by a system for itself
2481558Srgrimes */
24913171Swollman#define BETTER_LINK(rt,A,B) ((A)->rts_metric < HOPCNT_INFINITY		\
2501558Srgrimes			     && now_stale <= (A)->rts_time		\
2511558Srgrimes			     && ((A)->rts_metric < (B)->rts_metric	\
2521558Srgrimes				 || ((A)->rts_gate == (A)->rts_router	\
2531558Srgrimes				     && (B)->rts_gate != (B)->rts_router) \
2541558Srgrimes				 || ((A)->rts_metric == (B)->rts_metric	\
2551558Srgrimes				     && now_stale > (B)->rts_time)	\
2561558Srgrimes				 || (RT_ISHOST(rt)			\
2571558Srgrimes				     && (rt)->rt_dst == (A)->rts_router	\
2581558Srgrimes				     && (A)->rts_metric == (B)->rts_metric)))
2591558Srgrimes
2601558Srgrimes
2611558Srgrimes/* An "interface" is similar to a kernel ifnet structure, except it also
2621558Srgrimes * handles "logical" or "IS_REMOTE" interfaces (remote gateways).
2631558Srgrimes */
2641558Srgrimesstruct interface {
2651558Srgrimes	LIST_ENTRY(interface)		int_list;
2661558Srgrimes	LIST_ENTRY(interface)		remote_list;
2671558Srgrimes	struct interface *int_ahash, **int_ahash_prev;
2681558Srgrimes	struct interface *int_bhash, **int_bhash_prev;
2691558Srgrimes	struct interface *int_nhash, **int_nhash_prev;
2701558Srgrimes	char	int_name[IF_NAME_LEN+1];
2711558Srgrimes	u_short	int_index;
27213171Swollman	naddr	int_addr;		/* address on this host (net order) */
2731558Srgrimes	naddr	int_brdaddr;		/* broadcast address (n) */
2741558Srgrimes	naddr	int_dstaddr;		/* other end of pt-to-pt link (n) */
2751558Srgrimes	naddr	int_net;		/* working network # (host order)*/
2761558Srgrimes	naddr	int_mask;		/* working net mask (host order) */
2771558Srgrimes	naddr	int_ripv1_mask;		/* for inferring a mask (n) */
2781558Srgrimes	naddr	int_std_addr;		/* class A/B/C address (n) */
2791558Srgrimes	naddr	int_std_net;		/* class A/B/C network (h) */
2801558Srgrimes	naddr	int_std_mask;		/* class A/B/C netmask (h) */
2811558Srgrimes	int	int_rip_sock;		/* for queries */
2821558Srgrimes	int	int_if_flags;		/* some bits copied from kernel */
2831558Srgrimes	u_int	int_state;
2841558Srgrimes	time_t	int_act_time;		/* last thought healthy */
2851558Srgrimes	time_t	int_query_time;
2861558Srgrimes	u_short	int_transitions;	/* times gone up-down */
2871558Srgrimes	char	int_metric;
2881558Srgrimes	u_char	int_d_metric;		/* for faked default route */
2891558Srgrimes	u_char	int_adj_inmetric;	/* adjust advertised metrics */
2901558Srgrimes	u_char	int_adj_outmetric;	/*    instead of interface metric */
2911558Srgrimes	struct int_data {
2921558Srgrimes		u_int	ipackets;	/* previous network stats */
2931558Srgrimes		u_int	ierrors;
2941558Srgrimes		u_int	opackets;
2951558Srgrimes		u_int	oerrors;
2961558Srgrimes#ifdef sgi
29719209Sfenner		u_int	odrops;
2981558Srgrimes#endif
2991558Srgrimes		time_t	ts;		/* timestamp on network stats */
3001558Srgrimes	} int_data;
30114092Swollman#	define MAX_AUTH_KEYS 5
3021558Srgrimes	struct auth {			/* authentication info */
30314092Swollman	    u_int16_t type;
3041558Srgrimes	    u_char  key[RIP_AUTH_PW_LEN];
3051558Srgrimes	    u_char  keyid;
3061558Srgrimes	    time_t  start, end;
3071558Srgrimes	} int_auth[MAX_AUTH_KEYS];
30819209Sfenner	/* router discovery parameters */
30919209Sfenner	int	int_rdisc_pref;		/* signed preference to advertise */
3101558Srgrimes	int	int_rdisc_int;		/* MaxAdvertiseInterval */
31119209Sfenner	int	int_rdisc_cnt;
3121558Srgrimes	struct timeval int_rdisc_timer;
3131558Srgrimes};
3141558Srgrimes
3151558Srgrimes/* bits in int_state */
3161558Srgrimes#define IS_ALIAS	    0x0000001	/* interface alias */
3171558Srgrimes#define IS_SUBNET	    0x0000002	/* interface on subnetted network */
3181558Srgrimes#define	IS_REMOTE	    0x0000004	/* interface is not on this machine */
3191558Srgrimes#define	IS_PASSIVE	    0x0000008	/* remote and does not do RIP */
3201558Srgrimes#define IS_EXTERNAL	    0x0000010	/* handled by EGP or something */
3211558Srgrimes#define IS_CHECKED	    0x0000020	/* still exists */
3221558Srgrimes#define IS_ALL_HOSTS	    0x0000040	/* in INADDR_ALLHOSTS_GROUP */
3231558Srgrimes#define IS_ALL_ROUTERS	    0x0000080	/* in INADDR_ALLROUTERS_GROUP */
3241558Srgrimes#define IS_DISTRUST	    0x0000100	/* ignore untrusted routers */
3251558Srgrimes#define IS_REDIRECT_OK	    0x0000200	/* accept ICMP redirects */
3261558Srgrimes#define IS_BROKE	    0x0000400	/* seems to be broken */
3271558Srgrimes#define IS_SICK		    0x0000800	/* seems to be broken */
3281558Srgrimes#define IS_DUP		    0x0001000	/* has a duplicate address */
3291558Srgrimes#define IS_NEED_NET_SYN	    0x0002000	/* need RS_NET_SYN route */
3301558Srgrimes#define IS_NO_AG	    0x0004000	/* do not aggregate subnets */
3311558Srgrimes#define IS_NO_SUPER_AG	    0x0008000	/* do not aggregate networks */
3321558Srgrimes#define IS_NO_RIPV1_IN	    0x0010000	/* no RIPv1 input at all */
3331558Srgrimes#define IS_NO_RIPV2_IN	    0x0020000	/* no RIPv2 input at all */
3341558Srgrimes#define IS_NO_RIP_IN	(IS_NO_RIPV1_IN | IS_NO_RIPV2_IN)
3351558Srgrimes#define IS_RIP_IN_OFF(s) (((s) & IS_NO_RIP_IN) == IS_NO_RIP_IN)
33631958Simp#define IS_NO_RIPV1_OUT	    0x0040000	/* no RIPv1 output at all */
33731958Simp#define IS_NO_RIPV2_OUT	    0x0080000	/* no RIPv2 output at all */
33831958Simp#define IS_NO_RIP_OUT	(IS_NO_RIPV1_OUT | IS_NO_RIPV2_OUT)
33931958Simp#define IS_NO_RIP	(IS_NO_RIP_OUT | IS_NO_RIP_IN)
34013171Swollman#define IS_RIP_OUT_OFF(s) (((s) & IS_NO_RIP_OUT) == IS_NO_RIP_OUT)
34113171Swollman#define IS_RIP_OFF(s)	(((s) & IS_NO_RIP) == IS_NO_RIP)
3421558Srgrimes#define	IS_NO_RIP_MCAST	    0x0100000	/* broadcast RIPv2 */
3431558Srgrimes#define IS_NO_ADV_IN	    0x0200000	/* do not listen to advertisements */
3441558Srgrimes#define IS_NO_SOL_OUT	    0x0400000	/* send no solicitations */
3451558Srgrimes#define IS_SOL_OUT	    0x0800000	/* send solicitations */
3461558Srgrimes#define GROUP_IS_SOL_OUT (IS_SOL_OUT | IS_NO_SOL_OUT)
3471558Srgrimes#define IS_NO_ADV_OUT	    0x1000000	/* do not advertise rdisc */
3481558Srgrimes#define IS_ADV_OUT	    0x2000000	/* advertise rdisc */
34917046Sjulian#define GROUP_IS_ADV_OUT (IS_NO_ADV_OUT | IS_ADV_OUT)
35017046Sjulian#define IS_BCAST_RDISC	    0x4000000	/* broadcast instead of multicast */
35117046Sjulian#define IS_NO_RDISC	(IS_NO_ADV_IN | IS_NO_SOL_OUT | IS_NO_ADV_OUT)
35217046Sjulian#define IS_PM_RDISC	    0x8000000	/* poor-man's router discovery */
35317046Sjulian
35414092Swollman#define iff_up(f) ((f) & IFF_UP)
3551558Srgrimes
3561558SrgrimesLIST_HEAD(ifhead, interface);
35714092Swollman
3581558Srgrimes/* Information for aggregating routes */
3591558Srgrimes#define NUM_AG_SLOTS	32
3601558Srgrimesstruct ag_info {
3611558Srgrimes	struct ag_info *ag_fine;	/* slot with finer netmask */
3621558Srgrimes	struct ag_info *ag_cors;	/* more coarse netmask */
3631558Srgrimes	naddr	ag_dst_h;		/* destination in host byte order */
3641558Srgrimes	naddr	ag_mask;
3651558Srgrimes	naddr	ag_gate;
36619209Sfenner	naddr	ag_nhop;
3671558Srgrimes	char	ag_metric;		/* metric to be advertised */
36819209Sfenner	char	ag_pref;		/* aggregate based on this */
36919209Sfenner	u_int	ag_seqno;
3701558Srgrimes	u_short	ag_tag;
3711558Srgrimes	u_short	ag_state;
3721558Srgrimes#define	    AGS_SUPPRESS    0x001	/* combine with coarser mask */
3731558Srgrimes#define	    AGS_AGGREGATE   0x002	/* synthesize combined routes */
3741558Srgrimes#define	    AGS_REDUN0	    0x004	/* redundant, finer routes output */
3751558Srgrimes#define	    AGS_REDUN1	    0x008
3761558Srgrimes#define	    AG_IS_REDUN(state) (((state) & (AGS_REDUN0 | AGS_REDUN1)) \
3771558Srgrimes				== (AGS_REDUN0 | AGS_REDUN1))
3781558Srgrimes#define	    AGS_GATEWAY	    0x010	/* tell kernel RTF_GATEWAY */
3791558Srgrimes#define	    AGS_IF	    0x020	/* for an interface */
3801558Srgrimes#define	    AGS_RIPV2	    0x040	/* send only as RIPv2 */
3811558Srgrimes#define	    AGS_FINE_GATE   0x080	/* ignore differing ag_gate when this
3821558Srgrimes					 * has the finer netmask */
3831558Srgrimes#define	    AGS_CORS_GATE   0x100	/* ignore differing gate when this
3841558Srgrimes					 * has the coarser netmasks */
38519209Sfenner#define	    AGS_SPLIT_HZ    0x200	/* suppress for split horizon */
3861558Srgrimes
3871558Srgrimes	/* some bits are set if they are set on either route */
3881558Srgrimes#define	    AGS_AGGREGATE_EITHER (AGS_RIPV2 | AGS_GATEWAY |   \
3891558Srgrimes				  AGS_SUPPRESS | AGS_CORS_GATE)
39014092Swollman};
3911558Srgrimes
39214092Swollman
3931558Srgrimes/* parameters for interfaces */
3941558Srgrimesstruct parm {
3951558Srgrimes	struct parm *parm_next;
3961558Srgrimes	char	parm_name[IF_NAME_LEN+1];
3971558Srgrimes	naddr	parm_net;
3981558Srgrimes	naddr	parm_mask;
3991558Srgrimes
4001558Srgrimes	u_char	parm_d_metric;
4011558Srgrimes	u_char	parm_adj_inmetric;
4021558Srgrimes	char	parm_adj_outmetric;
4031558Srgrimes	u_int	parm_int_state;
4041558Srgrimes	int	parm_rdisc_pref;	/* signed IRDP preference */
4051558Srgrimes	int	parm_rdisc_int;		/* IRDP advertising interval */
4061558Srgrimes	struct auth parm_auth[MAX_AUTH_KEYS];
4071558Srgrimes};
4081558Srgrimes
4091558Srgrimes/* authority for internal networks */
4101558Srgrimesextern struct intnet {
4111558Srgrimes	struct intnet *intnet_next;
4121558Srgrimes	naddr	intnet_addr;		/* network byte order */
4131558Srgrimes	naddr	intnet_mask;
4141558Srgrimes	char	intnet_metric;
4151558Srgrimes} *intnets;
4161558Srgrimes
4171558Srgrimes/* defined RIPv1 netmasks */
4181558Srgrimesextern struct r1net {
4191558Srgrimes	struct r1net *r1net_next;
4201558Srgrimes	naddr	r1net_net;		/* host order */
4211558Srgrimes	naddr	r1net_match;
4221558Srgrimes	naddr	r1net_mask;
4231558Srgrimes} *r1nets;
4241558Srgrimes
4251558Srgrimes/* trusted routers */
4261558Srgrimesextern struct tgate {
4271558Srgrimes	struct tgate *tgate_next;
4281558Srgrimes	naddr	tgate_addr;
4291558Srgrimes#define	    MAX_TGATE_NETS 32
43019209Sfenner	struct tgate_net {
4311558Srgrimes	    naddr   net;		/* host order */
4321558Srgrimes	    naddr   mask;
4331558Srgrimes	} tgate_nets[MAX_TGATE_NETS];
4341558Srgrimes} *tgates;
4351558Srgrimes
4361558Srgrimesenum output_type {OUT_QUERY, OUT_UNICAST, OUT_BROADCAST, OUT_MULTICAST,
4371558Srgrimes	NO_OUT_MULTICAST, NO_OUT_RIPV2};
4381558Srgrimes
4391558Srgrimes/* common output buffers */
4401558Srgrimesextern struct ws_buf {
4411558Srgrimes	struct rip	*buf;
4421558Srgrimes	struct netinfo	*n;
4431558Srgrimes	struct netinfo	*base;
4441558Srgrimes	struct netinfo	*lim;
4451558Srgrimes	enum output_type type;
44617046Sjulian} v12buf;
44717046Sjulian
44817046Sjulianextern pid_t	mypid;
44917046Sjulianextern naddr	myaddr;			/* main address of this system */
45017046Sjulian
45114092Swollmanextern int	stopint;		/* !=0 to stop */
4521558Srgrimes
4531558Srgrimesextern int	rip_sock;		/* RIP socket */
4541558Srgrimesextern const struct interface *rip_sock_mcast; /* current multicast interface */
45514092Swollmanextern int	rt_sock;		/* routing socket */
4561558Srgrimesextern int	rt_sock_seqno;
4571558Srgrimesextern int	rdisc_sock;		/* router-discovery raw socket */
4581558Srgrimes
4591558Srgrimesextern int	supplier;		/* process should supply updates */
4601558Srgrimesextern int	supplier_set;		/* -s or -q requested */
4611558Srgrimesextern int	ridhosts;		/* 1=reduce host routes */
4621558Srgrimesextern int	mhome;			/* 1=want multi-homed host route */
4631558Srgrimesextern int	advertise_mhome;	/* 1=must continue advertising it */
4641558Srgrimesextern int	auth_ok;		/* 1=ignore auth if we do not care */
46519209Sfennerextern int	insecure;		/* Reply to special queries or not */
4661558Srgrimes
46719209Sfennerextern struct timeval clk;		/* system clock's idea of time */
46819209Sfennerextern struct timeval epoch;		/* system clock when started */
4691558Srgrimesextern struct timeval now;		/* current idea of time */
4701558Srgrimesextern time_t	now_stale;
4711558Srgrimesextern time_t	now_expire;
4721558Srgrimesextern time_t	now_garbage;
4731558Srgrimes
4741558Srgrimesextern struct timeval age_timer;	/* next check of old routes */
4751558Srgrimesextern struct timeval no_flash;		/* inhibit flash update until then */
4761558Srgrimesextern struct timeval rdisc_timer;	/* next advert. or solicitation */
4771558Srgrimesextern int rdisc_ok;			/* using solicited route */
4781558Srgrimes
4791558Srgrimesextern struct timeval ifinit_timer;	/* time to check interfaces */
4801558Srgrimes
4811558Srgrimesextern naddr	loopaddr;		/* our address on loopback */
4821558Srgrimesextern int	tot_interfaces;		/* # of remote and local interfaces */
4831558Srgrimesextern int	rip_interfaces;		/* # of interfaces doing RIP */
4841558Srgrimesextern struct ifhead ifnet;		/* all interfaces */
4851558Srgrimesextern struct ifhead remote_if;		/* remote interfaces */
4861558Srgrimesextern int	have_ripv1_out;		/* have a RIPv1 interface */
4871558Srgrimesextern int	need_flash;		/* flash update needed */
4881558Srgrimesextern struct timeval need_kern;	/* need to update kernel table */
4891558Srgrimesextern u_int	update_seqno;		/* a route has changed */
4901558Srgrimes
4911558Srgrimesextern int	tracelevel, new_tracelevel;
4921558Srgrimes#define MAX_TRACELEVEL 4
4931558Srgrimes#define TRACEKERNEL (tracelevel >= 4)	/* log kernel changes */
4941558Srgrimes#define	TRACECONTENTS (tracelevel >= 3)	/* display packet contents */
4951558Srgrimes#define TRACEPACKETS (tracelevel >= 2)	/* note packets */
4961558Srgrimes#define	TRACEACTIONS (tracelevel != 0)
4971558Srgrimesextern FILE	*ftrace;		/* output trace file */
4981558Srgrimesextern char inittracename[PATH_MAX];
4991558Srgrimes
5001558Srgrimesextern struct radix_node_head *rhead;
5011558Srgrimes
5021558Srgrimes
5031558Srgrimes#ifdef sgi
5041558Srgrimes/* Fix conflicts */
5051558Srgrimes#define	dup2(x,y)		BSDdup2(x,y)
5061558Srgrimes#endif /* sgi */
5071558Srgrimes
5081558Srgrimesvoid fix_sock(int, const char *);
5091558Srgrimesvoid fix_select(void);
5101558Srgrimesvoid rip_off(void);
5111558Srgrimesvoid rip_on(struct interface *);
5121558Srgrimes
51313171Swollmanvoid bufinit(void);
5141558Srgrimesint  output(enum output_type, struct sockaddr_in *,
5151558Srgrimes		   struct interface *, struct rip *, int);
5161558Srgrimesvoid clr_ws_buf(struct ws_buf *, struct auth *);
5171558Srgrimesvoid rip_query(void);
5181558Srgrimesvoid rip_bcast(int);
5191558Srgrimesvoid supply(struct sockaddr_in *, struct interface *,
5201558Srgrimes		   enum output_type, int, int, int);
5211558Srgrimes
5221558Srgrimesvoid	msglog(const char *, ...) PATTRIB(1,2);
5231558Srgrimesstruct msg_limit {
5241558Srgrimes    time_t	reuse;
5251558Srgrimes    struct msg_sub {
5261558Srgrimes	naddr	addr;
5271558Srgrimes	time_t	until;
5281558Srgrimes#   define MSG_SUBJECT_N 8
52917046Sjulian    } subs[MSG_SUBJECT_N];
53017046Sjulian};
53117046Sjulianvoid	msglim(struct msg_limit *, naddr,
53217046Sjulian		       const char *, ...) PATTRIB(3,4);
5331558Srgrimes#define	LOGERR(msg) msglog(msg ": %s", strerror(errno))
5341558Srgrimesvoid	logbad(int, const char *, ...) PATTRIB(2,3);
5351558Srgrimes#define	BADERR(dump,msg) logbad(dump,msg ": %s", strerror(errno))
5361558Srgrimes#ifdef DEBUG
53714092Swollman#define	DBGERR(dump,msg) BADERR(dump,msg)
5381558Srgrimes#else
5391558Srgrimes#define	DBGERR(dump,msg) LOGERR(msg)
5401558Srgrimes#endif
5411558Srgrimeschar	*naddr_ntoa(naddr);
54214092Swollmanconst char *saddr_ntoa(struct sockaddr *);
5431558Srgrimes
5441558Srgrimesvoid	*rtmalloc(size_t, const char *);
5451558Srgrimesvoid	timevaladd(struct timeval *, struct timeval *);
5462787Spstvoid	intvl_random(struct timeval *, u_long, u_long);
5471558Srgrimesint	getnet(char *, naddr *, naddr *);
5481558Srgrimesint	gethost(char *, naddr *);
5491558Srgrimesvoid	gwkludge(void);
55017591Sjulianconst char *parse_parms(char *, int);
55117591Sjulianconst char *check_parms(struct parm *);
55217591Sjulianvoid	get_parms(struct interface *);
5531558Srgrimes
5541558Srgrimesvoid	lastlog(void);
5551558Srgrimesvoid	trace_close(int);
5561558Srgrimesvoid	set_tracefile(const char *, const char *, int);
5571558Srgrimesvoid	tracelevel_msg(const char *, int);
5581558Srgrimesvoid	trace_off(const char*, ...) PATTRIB(1,2);
5591558Srgrimesvoid	set_tracelevel(void);
5601558Srgrimesvoid	trace_flush(void);
5611558Srgrimesvoid	trace_misc(const char *, ...) PATTRIB(1,2);
5621558Srgrimesvoid	trace_act(const char *, ...) PATTRIB(1,2);
5631558Srgrimesvoid	trace_pkt(const char *, ...) PATTRIB(1,2);
5641558Srgrimesvoid	trace_add_del(const char *, struct rt_entry *);
5651558Srgrimesvoid	trace_change(struct rt_entry *, u_int, struct rt_spare *,
5661558Srgrimes			     const char *);
5671558Srgrimesvoid	trace_if(const char *, struct interface *);
5681558Srgrimesvoid	trace_upslot(struct rt_entry *, struct rt_spare *,
5691558Srgrimes			     struct rt_spare *);
5701558Srgrimesvoid	trace_rip(const char*, const char*, struct sockaddr_in *,
5711558Srgrimes			  struct interface *, struct rip *, int);
5721558Srgrimeschar	*addrname(naddr, naddr, int);
5731558Srgrimeschar	*rtname(naddr, naddr, naddr);
5741558Srgrimes
5751558Srgrimesvoid	rdisc_age(naddr);
5761558Srgrimesvoid	set_rdisc_mg(struct interface *, int);
5771558Srgrimesvoid	set_supplier(void);
5781558Srgrimesvoid	if_bad_rdisc(struct interface *);
5791558Srgrimesvoid	if_ok_rdisc(struct interface *);
5801558Srgrimesvoid	read_rip(int, struct interface *);
5811558Srgrimesvoid	read_rt(void);
5821558Srgrimesvoid	read_d(void);
5831558Srgrimesvoid	rdisc_adv(void);
58447668Sruvoid	rdisc_sol(void);
58547668Sru
5861558Srgrimesvoid	sigtrace_on(int);
5871558Srgrimesvoid	sigtrace_off(int);
5881558Srgrimes
58947668Sruvoid	flush_kern(void);
59047668Sruvoid	age(naddr);
5911558Srgrimes
5921558Srgrimesvoid	ag_flush(naddr, naddr, void (*)(struct ag_info *));
5931558Srgrimesvoid	ag_check(naddr, naddr, naddr, naddr, char, char, u_int,
59447668Sru			 u_short, u_short, void (*)(struct ag_info *));
59547668Sruvoid	del_static(naddr, naddr, naddr, int);
5961558Srgrimesvoid	del_redirects(naddr, time_t);
5971558Srgrimesstruct rt_entry *rtget(naddr, naddr);
5981558Srgrimesstruct rt_entry *rtfind(naddr);
59947668Sruvoid	rtinit(void);
60047668Sruvoid	rtadd(naddr, naddr, u_int, struct rt_spare *);
6011558Srgrimesvoid	rtchange(struct rt_entry *, u_int, struct rt_spare *, char *);
6021558Srgrimesvoid	rtdelete(struct rt_entry *);
6031558Srgrimesvoid	rts_delete(struct rt_entry *, struct rt_spare *);
60447668Sruvoid	rtbad_sub(struct rt_entry *);
60547668Sruvoid	rtswitch(struct rt_entry *, struct rt_spare *);
6061558Srgrimes
6071558Srgrimes#define S_ADDR(x)	(((struct sockaddr_in *)(x))->sin_addr.s_addr)
6081558Srgrimes#define INFO_DST(I)	((I)->rti_info[RTAX_DST])
6091558Srgrimes#define INFO_GATE(I)	((I)->rti_info[RTAX_GATEWAY])
61047668Sru#define INFO_MASK(I)	((I)->rti_info[RTAX_NETMASK])
61147668Sru#define INFO_IFA(I)	((I)->rti_info[RTAX_IFA])
6121558Srgrimes#define INFO_AUTHOR(I)	((I)->rti_info[RTAX_AUTHOR])
6131558Srgrimes#define INFO_BRD(I)	((I)->rti_info[RTAX_BRD])
6141558Srgrimesvoid rt_xaddrs(struct rt_addrinfo *, struct sockaddr *, struct sockaddr *,
6151558Srgrimes	       int);
6161558Srgrimes
6171558Srgrimesnaddr	std_mask(naddr);
6181558Srgrimesnaddr	ripv1_mask_net(naddr, struct interface *);
6191558Srgrimesnaddr	ripv1_mask_host(naddr,struct interface *);
6201558Srgrimes#define		on_net(a,net,mask) (((ntohl(a) ^ (net)) & (mask)) == 0)
6211558Srgrimesint	check_dst(naddr);
6221558Srgrimesstruct interface *check_dup(naddr, naddr, naddr, int);
6231558Srgrimesint	check_remote(struct interface *);
6241558Srgrimesvoid	ifinit(void);
62547668Sruint	walk_bad(struct radix_node *, struct walkarg *);
62647668Sruint	if_ok(struct interface *, const char *);
6271558Srgrimesvoid	if_sick(struct interface *);
6281558Srgrimesvoid	if_link(struct interface *);
6291558Srgrimesstruct interface *ifwithaddr(naddr addr, int bcast, int remote);
6301558Srgrimesstruct interface *ifwithindex(u_short, int);
6311558Srgrimesstruct interface *iflookup(naddr);
6321558Srgrimes
6331558Srgrimesstruct auth *find_auth(struct interface *);
6341558Srgrimesvoid end_md5_auth(struct ws_buf *, struct auth *);
6351558Srgrimes
6361558Srgrimes#if defined(__FreeBSD__) || defined(__NetBSD__)
6371558Srgrimes#include <md5.h>
6381558Srgrimes#else
6391558Srgrimes#define MD5_DIGEST_LEN 16
6401558Srgrimestypedef struct {
6411558Srgrimes	u_int32_t state[4];		/* state (ABCD) */
6421558Srgrimes	u_int32_t count[2];		/* # of bits, modulo 2^64 (LSB 1st) */
6431558Srgrimes	unsigned char buffer[64];	/* input buffer */
6441558Srgrimes} MD5_CTX;
6451558Srgrimesvoid MD5Init(MD5_CTX*);
6461558Srgrimesvoid MD5Update(MD5_CTX*, u_char*, u_int);
6471558Srgrimesvoid MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*);
6481558Srgrimes#endif
6491558Srgrimes