118316Swollman/* 218316Swollman * Copyright (c) 1983, 1988, 1993 318316Swollman * The Regents of the University of California. All rights reserved. 418316Swollman * 518316Swollman * Redistribution and use in source and binary forms, with or without 618316Swollman * modification, are permitted provided that the following conditions 718316Swollman * are met: 818316Swollman * 1. Redistributions of source code must retain the above copyright 918316Swollman * notice, this list of conditions and the following disclaimer. 1018316Swollman * 2. Redistributions in binary form must reproduce the above copyright 1118316Swollman * notice, this list of conditions and the following disclaimer in the 1218316Swollman * documentation and/or other materials provided with the distribution. 1318316Swollman * 4. Neither the name of the University nor the names of its contributors 1418316Swollman * may be used to endorse or promote products derived from this software 1518316Swollman * without specific prior written permission. 1618316Swollman * 1718316Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1818316Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1918316Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2018316Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2118316Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2218316Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2318316Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2418316Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2518316Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2618316Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2718316Swollman * SUCH DAMAGE. 2818316Swollman * 2918316Swollman * @(#)defs.h 8.1 (Berkeley) 6/5/93 3046303Smarkm * 3150476Speter * $FreeBSD$ 3218316Swollman */ 3318316Swollman 3446303Smarkm#ifdef sgi 3550969Speter#ident "$FreeBSD$" 3646303Smarkm#endif 3746303Smarkm 3818316Swollman/* Definitions for RIPv2 routing process. 3918316Swollman * 4018316Swollman * This code is based on the 4.4BSD `routed` daemon, with extensions to 4118316Swollman * support: 4218316Swollman * RIPv2, including variable length subnet masks. 4318316Swollman * Router Discovery 4418316Swollman * aggregate routes in the kernel tables. 4518316Swollman * aggregate advertised routes. 4618316Swollman * maintain spare routes for faster selection of another gateway 4718316Swollman * when the current gateway dies. 4818316Swollman * timers on routes with second granularity so that selection 4918316Swollman * of a new route does not wait 30-60 seconds. 5018316Swollman * tolerance of static routes. 5118316Swollman * tell the kernel hop counts 5218316Swollman * do not advertise if ipforwarding=0 5318316Swollman * 5437908Scharnier * The vestigial support for other protocols has been removed. There 5518316Swollman * is no likelihood that IETF RIPv1 or RIPv2 will ever be used with 5618316Swollman * other protocols. The result is far smaller, faster, cleaner, and 5718316Swollman * perhaps understandable. 5818316Swollman * 5918316Swollman * The accumulation of special flags and kludges added over the many 6018316Swollman * years have been simplified and integrated. 6118316Swollman */ 6218316Swollman 63190718Sphk#include <assert.h> 6418316Swollman#include <stdio.h> 6518316Swollman#include <netdb.h> 6618316Swollman#include <stdlib.h> 6718316Swollman#include <unistd.h> 6818316Swollman#include <errno.h> 6918316Swollman#include <string.h> 7018316Swollman#ifdef sgi 7118316Swollman#include <strings.h> 7218316Swollman#include <bstring.h> 7318316Swollman#endif 7418316Swollman#include <stdarg.h> 7518316Swollman#include <syslog.h> 7618316Swollman#include <time.h> 7746303Smarkm#include <sys/cdefs.h> 7846303Smarkm#include <sys/time.h> 7918316Swollman#include <sys/types.h> 8018316Swollman#include <sys/param.h> 8118316Swollman#include <sys/ioctl.h> 8218316Swollman#include <sys/sysctl.h> 8318316Swollman#include <sys/socket.h> 84190711Sphk#include <sys/queue.h> 8518316Swollman#ifdef sgi 8646303Smarkm#define _USER_ROUTE_TREE 8718316Swollman#include <net/radix.h> 8818316Swollman#else 8918316Swollman#include "radix.h" 9046303Smarkm#define UNUSED __attribute__((unused)) 9146303Smarkm#define PATTRIB(f,l) __attribute__((format (printf,f,l))) 9218316Swollman#endif 9318316Swollman#include <net/if.h> 9418316Swollman#include <net/route.h> 9518316Swollman#include <net/if_dl.h> 9618316Swollman#include <netinet/in.h> 9718316Swollman#include <arpa/inet.h> 9818316Swollman#define RIPVERSION RIPv2 9918316Swollman#include <protocols/routed.h> 10018316Swollman 101126250Sbms#ifndef __RCSID 102126250Sbms#define __RCSID(_s) static const char rcsid[] UNUSED = _s 103126250Sbms#endif 104126250Sbms#ifndef __COPYRIGHT 105126250Sbms#define __COPYRIGHT(_s) static const char copyright[] UNUSED = _s 106126250Sbms#endif 10718316Swollman 10818316Swollman/* Type of an IP address. 10918316Swollman * Some systems do not like to pass structures, so do not use in_addr. 11018316Swollman * Some systems think a long has 64 bits, which would be a gross waste. 11118316Swollman * So define it here so it can be changed for the target system. 11218316Swollman * It should be defined somewhere netinet/in.h, but it is not. 11318316Swollman */ 11418316Swollman#ifdef sgi 11518316Swollman#define naddr u_int32_t 11646303Smarkm#elif defined (__NetBSD__) 11746303Smarkm#define naddr u_int32_t 11846303Smarkm#define _HAVE_SA_LEN 11946303Smarkm#define _HAVE_SIN_LEN 12018316Swollman#else 12118316Swollman#define naddr u_long 12218316Swollman#define _HAVE_SA_LEN 12318316Swollman#define _HAVE_SIN_LEN 12418316Swollman#endif 12518316Swollman 12620342Swollman#define DAY (24*60*60) 12720342Swollman#define NEVER DAY /* a long time */ 12818316Swollman#define EPOCH NEVER /* bias time by this to avoid <0 */ 12918316Swollman 13018316Swollman/* Scan the kernel regularly to see if any interfaces have appeared or been 13118316Swollman * turned off. These must be less than STALE_TIME. 13218316Swollman */ 13318316Swollman#define CHECK_BAD_INTERVAL 5 /* when an interface is known bad */ 13418316Swollman#define CHECK_ACT_INTERVAL 30 /* when advertising */ 13518316Swollman#define CHECK_QUIET_INTERVAL 300 /* when not */ 13618316Swollman 13718316Swollman#define LIM_SEC(s,l) ((s).tv_sec = MIN((s).tv_sec, (l))) 13818316Swollman 13919885Swollman/* Metric used for fake default routes. It ought to be 15, but when 14019885Swollman * processing advertised routes, previous versions of `routed` added 14119885Swollman * to the received metric and discarded the route if the total was 16 14219885Swollman * or larger. 14319885Swollman */ 14419885Swollman#define FAKE_METRIC (HOPCNT_INFINITY-2) 14518316Swollman 14619885Swollman 14718316Swollman/* Router Discovery parameters */ 14818316Swollman#ifndef sgi 14918316Swollman#define INADDR_ALLROUTERS_GROUP 0xe0000002 /* 224.0.0.2 */ 15018316Swollman#endif 15118316Swollman#define MaxMaxAdvertiseInterval 1800 15218316Swollman#define MinMaxAdvertiseInterval 4 15318316Swollman#define DefMaxAdvertiseInterval 600 15418316Swollman#define MIN_PreferenceLevel 0x80000000 15518316Swollman 15618316Swollman#define MAX_INITIAL_ADVERT_INTERVAL 16 15718316Swollman#define MAX_INITIAL_ADVERTS 3 15818316Swollman 15918316Swollman#define MAX_SOLICITATION_DELAY 1 16018316Swollman#define SOLICITATION_INTERVAL 3 16118316Swollman#define MAX_SOLICITATIONS 3 16218316Swollman 16318316Swollman 16419885Swollman/* Bloated packet size for systems that simply add authentication to 16519885Swollman * full-sized packets 16619885Swollman */ 16719885Swollman#define OVER_MAXPACKETSIZE (MAXPACKETSIZE+sizeof(struct netinfo)*2) 16818316Swollman/* typical packet buffers */ 16918316Swollmanunion pkt_buf { 17019885Swollman char packet[OVER_MAXPACKETSIZE*2]; 17118316Swollman struct rip rip; 17218316Swollman}; 17318316Swollman 17446303Smarkm#define GNAME_LEN 64 /* assumed=64 in parms.c */ 17546303Smarkm/* bigger than IFNAMSIZ, with room for "external()" or "remote()" */ 17646303Smarkm#define IF_NAME_LEN (GNAME_LEN+15) 17718316Swollman 17819885Swollman/* No more routes than this, to protect ourself in case something goes 17919885Swollman * whacko and starts broadcasting zillions of bogus routes. 18018316Swollman */ 18118316Swollman#define MAX_ROUTES (128*1024) 18218316Swollmanextern int total_routes; 18318316Swollman 18418316Swollman/* Main, daemon routing table structure 18518316Swollman */ 18618316Swollmanstruct rt_entry { 18718316Swollman struct radix_node rt_nodes[2]; /* radix tree glue */ 18818316Swollman u_int rt_state; 18918316Swollman# define RS_IF 0x001 /* for network interface */ 19018316Swollman# define RS_NET_INT 0x002 /* authority route */ 19118316Swollman# define RS_NET_SYN 0x004 /* fake net route for subnet */ 19218316Swollman# define RS_NO_NET_SYN (RS_LOCAL | RS_LOCAL | RS_IF) 19318316Swollman# define RS_SUBNET 0x008 /* subnet route from any source */ 19418316Swollman# define RS_LOCAL 0x010 /* loopback for pt-to-pt */ 19518316Swollman# define RS_MHOME 0x020 /* from -m */ 19618316Swollman# define RS_STATIC 0x040 /* from the kernel */ 19718316Swollman# define RS_RDISC 0x080 /* from router discovery */ 19818316Swollman struct sockaddr_in rt_dst_sock; 19918316Swollman naddr rt_mask; 20018316Swollman struct rt_spare { 20118316Swollman struct interface *rts_ifp; 20218316Swollman naddr rts_gate; /* forward packets here */ 20318316Swollman naddr rts_router; /* on the authority of this router */ 20418316Swollman char rts_metric; 20518316Swollman u_short rts_tag; 20618316Swollman time_t rts_time; /* timer to junk stale routes */ 20746303Smarkm u_int rts_de_ag; /* de-aggregation level */ 20818316Swollman#define NUM_SPARES 4 20918316Swollman } rt_spares[NUM_SPARES]; 21018316Swollman u_int rt_seqno; /* when last changed */ 21118316Swollman char rt_poison_metric; /* to notice maximum recently */ 21218316Swollman time_t rt_poison_time; /* advertised metric */ 21318316Swollman}; 21446303Smarkm#define rt_dst rt_dst_sock.sin_addr.s_addr 21546303Smarkm#define rt_ifp rt_spares[0].rts_ifp 21646303Smarkm#define rt_gate rt_spares[0].rts_gate 21746303Smarkm#define rt_router rt_spares[0].rts_router 21846303Smarkm#define rt_metric rt_spares[0].rts_metric 21946303Smarkm#define rt_tag rt_spares[0].rts_tag 22046303Smarkm#define rt_time rt_spares[0].rts_time 22146303Smarkm#define rt_de_ag rt_spares[0].rts_de_ag 22218316Swollman 22318316Swollman#define HOST_MASK 0xffffffff 22418316Swollman#define RT_ISHOST(rt) ((rt)->rt_mask == HOST_MASK) 22518316Swollman 22618316Swollman/* age all routes that 22718316Swollman * are not from -g, -m, or static routes from the kernel 22818316Swollman * not unbroken interface routes 22918316Swollman * but not broken interfaces 23018316Swollman * nor non-passive, remote interfaces that are not aliases 23118316Swollman * (i.e. remote & metric=0) 23218316Swollman */ 23318316Swollman#define AGE_RT(rt_state,ifp) (0 == ((rt_state) & (RS_MHOME | RS_STATIC \ 23418316Swollman | RS_NET_SYN | RS_RDISC)) \ 23518316Swollman && (!((rt_state) & RS_IF) \ 23618316Swollman || (ifp) == 0 \ 23718316Swollman || (((ifp)->int_state & IS_REMOTE) \ 23818316Swollman && !((ifp)->int_state & IS_PASSIVE)))) 23918316Swollman 24018316Swollman/* true if A is better than B 24118316Swollman * Better if 24218316Swollman * - A is not a poisoned route 24318316Swollman * - and A is not stale 24418316Swollman * - and A has a shorter path 24518316Swollman * - or is the router speaking for itself 24618316Swollman * - or the current route is equal but stale 24718316Swollman * - or it is a host route advertised by a system for itself 24818316Swollman */ 24946303Smarkm#define BETTER_LINK(rt,A,B) ((A)->rts_metric < HOPCNT_INFINITY \ 25018316Swollman && now_stale <= (A)->rts_time \ 25118316Swollman && ((A)->rts_metric < (B)->rts_metric \ 25218316Swollman || ((A)->rts_gate == (A)->rts_router \ 25318316Swollman && (B)->rts_gate != (B)->rts_router) \ 25418316Swollman || ((A)->rts_metric == (B)->rts_metric \ 25518316Swollman && now_stale > (B)->rts_time) \ 25618316Swollman || (RT_ISHOST(rt) \ 25718316Swollman && (rt)->rt_dst == (A)->rts_router \ 25818316Swollman && (A)->rts_metric == (B)->rts_metric))) 25918316Swollman 26018316Swollman 26118316Swollman/* An "interface" is similar to a kernel ifnet structure, except it also 26218316Swollman * handles "logical" or "IS_REMOTE" interfaces (remote gateways). 26318316Swollman */ 26418316Swollmanstruct interface { 265190711Sphk LIST_ENTRY(interface) int_list; 266190713Sphk LIST_ENTRY(interface) remote_list; 26719885Swollman struct interface *int_ahash, **int_ahash_prev; 26819885Swollman struct interface *int_bhash, **int_bhash_prev; 26919885Swollman struct interface *int_nhash, **int_nhash_prev; 27046303Smarkm char int_name[IF_NAME_LEN+1]; 27118316Swollman u_short int_index; 27218316Swollman naddr int_addr; /* address on this host (net order) */ 27318316Swollman naddr int_brdaddr; /* broadcast address (n) */ 27418316Swollman naddr int_dstaddr; /* other end of pt-to-pt link (n) */ 27518316Swollman naddr int_net; /* working network # (host order)*/ 27618316Swollman naddr int_mask; /* working net mask (host order) */ 27718316Swollman naddr int_ripv1_mask; /* for inferring a mask (n) */ 27818316Swollman naddr int_std_addr; /* class A/B/C address (n) */ 27918316Swollman naddr int_std_net; /* class A/B/C network (h) */ 28018316Swollman naddr int_std_mask; /* class A/B/C netmask (h) */ 28118316Swollman int int_rip_sock; /* for queries */ 28218316Swollman int int_if_flags; /* some bits copied from kernel */ 28318316Swollman u_int int_state; 28418316Swollman time_t int_act_time; /* last thought healthy */ 28519885Swollman time_t int_query_time; 28618316Swollman u_short int_transitions; /* times gone up-down */ 28718316Swollman char int_metric; 288126250Sbms u_char int_d_metric; /* for faked default route */ 289126250Sbms u_char int_adj_inmetric; /* adjust advertised metrics */ 290126250Sbms u_char int_adj_outmetric; /* instead of interface metric */ 29118316Swollman struct int_data { 29218316Swollman u_int ipackets; /* previous network stats */ 29318316Swollman u_int ierrors; 29418316Swollman u_int opackets; 29518316Swollman u_int oerrors; 29618316Swollman#ifdef sgi 29718316Swollman u_int odrops; 29818316Swollman#endif 29918316Swollman time_t ts; /* timestamp on network stats */ 30018316Swollman } int_data; 30120342Swollman# define MAX_AUTH_KEYS 5 30219885Swollman struct auth { /* authentication info */ 30346303Smarkm u_int16_t type; 30446303Smarkm u_char key[RIP_AUTH_PW_LEN]; 30520342Swollman u_char keyid; 30620342Swollman time_t start, end; 30720342Swollman } int_auth[MAX_AUTH_KEYS]; 30846303Smarkm /* router discovery parameters */ 30946303Smarkm int int_rdisc_pref; /* signed preference to advertise */ 31018316Swollman int int_rdisc_int; /* MaxAdvertiseInterval */ 31118316Swollman int int_rdisc_cnt; 31218316Swollman struct timeval int_rdisc_timer; 31318316Swollman}; 31418316Swollman 31518316Swollman/* bits in int_state */ 31618316Swollman#define IS_ALIAS 0x0000001 /* interface alias */ 31718316Swollman#define IS_SUBNET 0x0000002 /* interface on subnetted network */ 31818316Swollman#define IS_REMOTE 0x0000004 /* interface is not on this machine */ 31918316Swollman#define IS_PASSIVE 0x0000008 /* remote and does not do RIP */ 32018316Swollman#define IS_EXTERNAL 0x0000010 /* handled by EGP or something */ 32118316Swollman#define IS_CHECKED 0x0000020 /* still exists */ 32218316Swollman#define IS_ALL_HOSTS 0x0000040 /* in INADDR_ALLHOSTS_GROUP */ 32318316Swollman#define IS_ALL_ROUTERS 0x0000080 /* in INADDR_ALLROUTERS_GROUP */ 32419885Swollman#define IS_DISTRUST 0x0000100 /* ignore untrusted routers */ 32520342Swollman#define IS_REDIRECT_OK 0x0000200 /* accept ICMP redirects */ 32620342Swollman#define IS_BROKE 0x0000400 /* seems to be broken */ 32720342Swollman#define IS_SICK 0x0000800 /* seems to be broken */ 32820342Swollman#define IS_DUP 0x0001000 /* has a duplicate address */ 32918316Swollman#define IS_NEED_NET_SYN 0x0002000 /* need RS_NET_SYN route */ 33018316Swollman#define IS_NO_AG 0x0004000 /* do not aggregate subnets */ 33118316Swollman#define IS_NO_SUPER_AG 0x0008000 /* do not aggregate networks */ 33218316Swollman#define IS_NO_RIPV1_IN 0x0010000 /* no RIPv1 input at all */ 33318316Swollman#define IS_NO_RIPV2_IN 0x0020000 /* no RIPv2 input at all */ 33418316Swollman#define IS_NO_RIP_IN (IS_NO_RIPV1_IN | IS_NO_RIPV2_IN) 33518316Swollman#define IS_RIP_IN_OFF(s) (((s) & IS_NO_RIP_IN) == IS_NO_RIP_IN) 33618316Swollman#define IS_NO_RIPV1_OUT 0x0040000 /* no RIPv1 output at all */ 33718316Swollman#define IS_NO_RIPV2_OUT 0x0080000 /* no RIPv2 output at all */ 33818316Swollman#define IS_NO_RIP_OUT (IS_NO_RIPV1_OUT | IS_NO_RIPV2_OUT) 33918316Swollman#define IS_NO_RIP (IS_NO_RIP_OUT | IS_NO_RIP_IN) 34018316Swollman#define IS_RIP_OUT_OFF(s) (((s) & IS_NO_RIP_OUT) == IS_NO_RIP_OUT) 34118316Swollman#define IS_RIP_OFF(s) (((s) & IS_NO_RIP) == IS_NO_RIP) 34246303Smarkm#define IS_NO_RIP_MCAST 0x0100000 /* broadcast RIPv2 */ 34346303Smarkm#define IS_NO_ADV_IN 0x0200000 /* do not listen to advertisements */ 34446303Smarkm#define IS_NO_SOL_OUT 0x0400000 /* send no solicitations */ 34546303Smarkm#define IS_SOL_OUT 0x0800000 /* send solicitations */ 34646303Smarkm#define GROUP_IS_SOL_OUT (IS_SOL_OUT | IS_NO_SOL_OUT) 34746303Smarkm#define IS_NO_ADV_OUT 0x1000000 /* do not advertise rdisc */ 34846303Smarkm#define IS_ADV_OUT 0x2000000 /* advertise rdisc */ 34946303Smarkm#define GROUP_IS_ADV_OUT (IS_NO_ADV_OUT | IS_ADV_OUT) 35046303Smarkm#define IS_BCAST_RDISC 0x4000000 /* broadcast instead of multicast */ 35118316Swollman#define IS_NO_RDISC (IS_NO_ADV_IN | IS_NO_SOL_OUT | IS_NO_ADV_OUT) 35246303Smarkm#define IS_PM_RDISC 0x8000000 /* poor-man's router discovery */ 35318316Swollman 35446303Smarkm#define iff_up(f) ((f) & IFF_UP) 35518316Swollman 356190711SphkLIST_HEAD(ifhead, interface); 35718316Swollman 35818316Swollman/* Information for aggregating routes */ 35918316Swollman#define NUM_AG_SLOTS 32 36018316Swollmanstruct ag_info { 36118316Swollman struct ag_info *ag_fine; /* slot with finer netmask */ 36218316Swollman struct ag_info *ag_cors; /* more coarse netmask */ 36318316Swollman naddr ag_dst_h; /* destination in host byte order */ 36418316Swollman naddr ag_mask; 36518316Swollman naddr ag_gate; 36618316Swollman naddr ag_nhop; 36718316Swollman char ag_metric; /* metric to be advertised */ 36818316Swollman char ag_pref; /* aggregate based on this */ 36918316Swollman u_int ag_seqno; 37018316Swollman u_short ag_tag; 37118316Swollman u_short ag_state; 37237908Scharnier#define AGS_SUPPRESS 0x001 /* combine with coarser mask */ 37346303Smarkm#define AGS_AGGREGATE 0x002 /* synthesize combined routes */ 37418316Swollman#define AGS_REDUN0 0x004 /* redundant, finer routes output */ 37518316Swollman#define AGS_REDUN1 0x008 37618316Swollman#define AG_IS_REDUN(state) (((state) & (AGS_REDUN0 | AGS_REDUN1)) \ 37718316Swollman == (AGS_REDUN0 | AGS_REDUN1)) 37818316Swollman#define AGS_GATEWAY 0x010 /* tell kernel RTF_GATEWAY */ 37918316Swollman#define AGS_IF 0x020 /* for an interface */ 38018316Swollman#define AGS_RIPV2 0x040 /* send only as RIPv2 */ 38118316Swollman#define AGS_FINE_GATE 0x080 /* ignore differing ag_gate when this 38218316Swollman * has the finer netmask */ 38318316Swollman#define AGS_CORS_GATE 0x100 /* ignore differing gate when this 38437908Scharnier * has the coarser netmasks */ 38518316Swollman#define AGS_SPLIT_HZ 0x200 /* suppress for split horizon */ 38618316Swollman 38718316Swollman /* some bits are set if they are set on either route */ 38846303Smarkm#define AGS_AGGREGATE_EITHER (AGS_RIPV2 | AGS_GATEWAY | \ 38946303Smarkm AGS_SUPPRESS | AGS_CORS_GATE) 39018316Swollman}; 39118316Swollman 39218316Swollman 39318316Swollman/* parameters for interfaces */ 394190715Sphkstruct parm { 39518316Swollman struct parm *parm_next; 39646303Smarkm char parm_name[IF_NAME_LEN+1]; 39719885Swollman naddr parm_net; 39818316Swollman naddr parm_mask; 39918316Swollman 400126250Sbms u_char parm_d_metric; 401126250Sbms u_char parm_adj_inmetric; 402126250Sbms char parm_adj_outmetric; 40318316Swollman u_int parm_int_state; 40446303Smarkm int parm_rdisc_pref; /* signed IRDP preference */ 40546303Smarkm int parm_rdisc_int; /* IRDP advertising interval */ 40620342Swollman struct auth parm_auth[MAX_AUTH_KEYS]; 407190715Sphk}; 40818316Swollman 40918316Swollman/* authority for internal networks */ 41018316Swollmanextern struct intnet { 41118316Swollman struct intnet *intnet_next; 41246303Smarkm naddr intnet_addr; /* network byte order */ 41318316Swollman naddr intnet_mask; 41418316Swollman char intnet_metric; 41518316Swollman} *intnets; 41618316Swollman 41746303Smarkm/* defined RIPv1 netmasks */ 41846303Smarkmextern struct r1net { 41946303Smarkm struct r1net *r1net_next; 42046303Smarkm naddr r1net_net; /* host order */ 42146303Smarkm naddr r1net_match; 42246303Smarkm naddr r1net_mask; 42346303Smarkm} *r1nets; 42446303Smarkm 42519885Swollman/* trusted routers */ 42619885Swollmanextern struct tgate { 42719885Swollman struct tgate *tgate_next; 42819885Swollman naddr tgate_addr; 42946303Smarkm#define MAX_TGATE_NETS 32 43046303Smarkm struct tgate_net { 43146303Smarkm naddr net; /* host order */ 43246303Smarkm naddr mask; 43346303Smarkm } tgate_nets[MAX_TGATE_NETS]; 43419885Swollman} *tgates; 43518316Swollman 43619885Swollmanenum output_type {OUT_QUERY, OUT_UNICAST, OUT_BROADCAST, OUT_MULTICAST, 43719885Swollman NO_OUT_MULTICAST, NO_OUT_RIPV2}; 43818316Swollman 43919885Swollman/* common output buffers */ 44019885Swollmanextern struct ws_buf { 44119885Swollman struct rip *buf; 44219885Swollman struct netinfo *n; 44319885Swollman struct netinfo *base; 44419885Swollman struct netinfo *lim; 44519885Swollman enum output_type type; 446190715Sphk} v12buf; 44719885Swollman 44818316Swollmanextern pid_t mypid; 44918316Swollmanextern naddr myaddr; /* main address of this system */ 45018316Swollman 45118316Swollmanextern int stopint; /* !=0 to stop */ 45218316Swollman 45318316Swollmanextern int rip_sock; /* RIP socket */ 454190716Sphkextern const struct interface *rip_sock_mcast; /* current multicast interface */ 45518316Swollmanextern int rt_sock; /* routing socket */ 45618316Swollmanextern int rt_sock_seqno; 45718316Swollmanextern int rdisc_sock; /* router-discovery raw socket */ 45818316Swollman 45918316Swollmanextern int supplier; /* process should supply updates */ 46046303Smarkmextern int supplier_set; /* -s or -q requested */ 46118316Swollmanextern int ridhosts; /* 1=reduce host routes */ 46218316Swollmanextern int mhome; /* 1=want multi-homed host route */ 46337908Scharnierextern int advertise_mhome; /* 1=must continue advertising it */ 46418316Swollmanextern int auth_ok; /* 1=ignore auth if we do not care */ 46518316Swollman 46619885Swollmanextern struct timeval clk; /* system clock's idea of time */ 46719885Swollmanextern struct timeval epoch; /* system clock when started */ 46818316Swollmanextern struct timeval now; /* current idea of time */ 46918316Swollmanextern time_t now_stale; 47018316Swollmanextern time_t now_expire; 47118316Swollmanextern time_t now_garbage; 47218316Swollman 47318316Swollmanextern struct timeval age_timer; /* next check of old routes */ 47418316Swollmanextern struct timeval no_flash; /* inhibit flash update until then */ 47518316Swollmanextern struct timeval rdisc_timer; /* next advert. or solicitation */ 47618316Swollmanextern int rdisc_ok; /* using solicited route */ 47718316Swollman 47818316Swollmanextern struct timeval ifinit_timer; /* time to check interfaces */ 47918316Swollman 48018316Swollmanextern naddr loopaddr; /* our address on loopback */ 48118316Swollmanextern int tot_interfaces; /* # of remote and local interfaces */ 48218316Swollmanextern int rip_interfaces; /* # of interfaces doing RIP */ 483190711Sphkextern struct ifhead ifnet; /* all interfaces */ 484190713Sphkextern struct ifhead remote_if; /* remote interfaces */ 48518316Swollmanextern int have_ripv1_out; /* have a RIPv1 interface */ 48618316Swollmanextern int need_flash; /* flash update needed */ 48718316Swollmanextern struct timeval need_kern; /* need to update kernel table */ 48846303Smarkmextern u_int update_seqno; /* a route has changed */ 48918316Swollman 49020342Swollmanextern int tracelevel, new_tracelevel; 49118316Swollman#define MAX_TRACELEVEL 4 49218316Swollman#define TRACEKERNEL (tracelevel >= 4) /* log kernel changes */ 49318316Swollman#define TRACECONTENTS (tracelevel >= 3) /* display packet contents */ 49418316Swollman#define TRACEPACKETS (tracelevel >= 2) /* note packets */ 49518316Swollman#define TRACEACTIONS (tracelevel != 0) 49618316Swollmanextern FILE *ftrace; /* output trace file */ 497118582Simpextern char inittracename[PATH_MAX]; 49818316Swollman 49918316Swollmanextern struct radix_node_head *rhead; 50018316Swollman 50118316Swollman 50218316Swollman#ifdef sgi 50318316Swollman/* Fix conflicts */ 50418316Swollman#define dup2(x,y) BSDdup2(x,y) 50518316Swollman#endif /* sgi */ 50618316Swollman 507190715Sphkvoid fix_sock(int, const char *); 508190715Sphkvoid fix_select(void); 509190715Sphkvoid rip_off(void); 510190715Sphkvoid rip_on(struct interface *); 51118316Swollman 512190715Sphkvoid bufinit(void); 513190715Sphkint output(enum output_type, struct sockaddr_in *, 51419885Swollman struct interface *, struct rip *, int); 515190715Sphkvoid clr_ws_buf(struct ws_buf *, struct auth *); 516190715Sphkvoid rip_query(void); 517190715Sphkvoid rip_bcast(int); 518190715Sphkvoid supply(struct sockaddr_in *, struct interface *, 51919885Swollman enum output_type, int, int, int); 52018316Swollman 521190715Sphkvoid msglog(const char *, ...) PATTRIB(1,2); 52219885Swollmanstruct msg_limit { 52320342Swollman time_t reuse; 52420342Swollman struct msg_sub { 52519885Swollman naddr addr; 52619885Swollman time_t until; 52720342Swollman# define MSG_SUBJECT_N 8 52820342Swollman } subs[MSG_SUBJECT_N]; 52919885Swollman}; 530190715Sphkvoid msglim(struct msg_limit *, naddr, 53146303Smarkm const char *, ...) PATTRIB(3,4); 53218316Swollman#define LOGERR(msg) msglog(msg ": %s", strerror(errno)) 533190715Sphkvoid logbad(int, const char *, ...) PATTRIB(2,3); 53418316Swollman#define BADERR(dump,msg) logbad(dump,msg ": %s", strerror(errno)) 53518316Swollman#ifdef DEBUG 53618316Swollman#define DBGERR(dump,msg) BADERR(dump,msg) 53718316Swollman#else 53818316Swollman#define DBGERR(dump,msg) LOGERR(msg) 53918316Swollman#endif 540190715Sphkchar *naddr_ntoa(naddr); 541190715Sphkconst char *saddr_ntoa(struct sockaddr *); 54218316Swollman 543190715Sphkvoid *rtmalloc(size_t, const char *); 544190715Sphkvoid timevaladd(struct timeval *, struct timeval *); 545190715Sphkvoid intvl_random(struct timeval *, u_long, u_long); 546190715Sphkint getnet(char *, naddr *, naddr *); 547190715Sphkint gethost(char *, naddr *); 548190715Sphkvoid gwkludge(void); 549190715Sphkconst char *parse_parms(char *, int); 550190715Sphkconst char *check_parms(struct parm *); 551190715Sphkvoid get_parms(struct interface *); 55218316Swollman 553190715Sphkvoid lastlog(void); 554190715Sphkvoid trace_close(int); 555190715Sphkvoid set_tracefile(const char *, const char *, int); 556190715Sphkvoid tracelevel_msg(const char *, int); 557190715Sphkvoid trace_off(const char*, ...) PATTRIB(1,2); 558190715Sphkvoid set_tracelevel(void); 559190715Sphkvoid trace_flush(void); 560190715Sphkvoid trace_misc(const char *, ...) PATTRIB(1,2); 561190715Sphkvoid trace_act(const char *, ...) PATTRIB(1,2); 562190715Sphkvoid trace_pkt(const char *, ...) PATTRIB(1,2); 563190715Sphkvoid trace_add_del(const char *, struct rt_entry *); 564190715Sphkvoid trace_change(struct rt_entry *, u_int, struct rt_spare *, 56546303Smarkm const char *); 566190715Sphkvoid trace_if(const char *, struct interface *); 567190715Sphkvoid trace_upslot(struct rt_entry *, struct rt_spare *, 56846303Smarkm struct rt_spare *); 569190715Sphkvoid trace_rip(const char*, const char*, struct sockaddr_in *, 57018316Swollman struct interface *, struct rip *, int); 571190715Sphkchar *addrname(naddr, naddr, int); 572190715Sphkchar *rtname(naddr, naddr, naddr); 57318316Swollman 574190715Sphkvoid rdisc_age(naddr); 575190715Sphkvoid set_rdisc_mg(struct interface *, int); 576190715Sphkvoid set_supplier(void); 577190715Sphkvoid if_bad_rdisc(struct interface *); 578190715Sphkvoid if_ok_rdisc(struct interface *); 579190715Sphkvoid read_rip(int, struct interface *); 580190715Sphkvoid read_rt(void); 581190715Sphkvoid read_d(void); 582190715Sphkvoid rdisc_adv(void); 583190715Sphkvoid rdisc_sol(void); 58418316Swollman 585190715Sphkvoid sigtrace_on(int); 586190715Sphkvoid sigtrace_off(int); 58718316Swollman 588190715Sphkvoid flush_kern(void); 589190715Sphkvoid age(naddr); 59018316Swollman 591190715Sphkvoid ag_flush(naddr, naddr, void (*)(struct ag_info *)); 592190715Sphkvoid ag_check(naddr, naddr, naddr, naddr, char, char, u_int, 59318316Swollman u_short, u_short, void (*)(struct ag_info *)); 594190715Sphkvoid del_static(naddr, naddr, naddr, int); 595190715Sphkvoid del_redirects(naddr, time_t); 596190715Sphkstruct rt_entry *rtget(naddr, naddr); 597190715Sphkstruct rt_entry *rtfind(naddr); 598190715Sphkvoid rtinit(void); 599190715Sphkvoid rtadd(naddr, naddr, u_int, struct rt_spare *); 600190715Sphkvoid rtchange(struct rt_entry *, u_int, struct rt_spare *, char *); 601190715Sphkvoid rtdelete(struct rt_entry *); 602190715Sphkvoid rts_delete(struct rt_entry *, struct rt_spare *); 603190715Sphkvoid rtbad_sub(struct rt_entry *); 604190715Sphkvoid rtswitch(struct rt_entry *, struct rt_spare *); 60518316Swollman 60618316Swollman#define S_ADDR(x) (((struct sockaddr_in *)(x))->sin_addr.s_addr) 60718316Swollman#define INFO_DST(I) ((I)->rti_info[RTAX_DST]) 60818316Swollman#define INFO_GATE(I) ((I)->rti_info[RTAX_GATEWAY]) 60918316Swollman#define INFO_MASK(I) ((I)->rti_info[RTAX_NETMASK]) 61018316Swollman#define INFO_IFA(I) ((I)->rti_info[RTAX_IFA]) 61118316Swollman#define INFO_AUTHOR(I) ((I)->rti_info[RTAX_AUTHOR]) 61218316Swollman#define INFO_BRD(I) ((I)->rti_info[RTAX_BRD]) 61318316Swollmanvoid rt_xaddrs(struct rt_addrinfo *, struct sockaddr *, struct sockaddr *, 61418316Swollman int); 61518316Swollman 616190715Sphknaddr std_mask(naddr); 617190715Sphknaddr ripv1_mask_net(naddr, struct interface *); 618190715Sphknaddr ripv1_mask_host(naddr,struct interface *); 61918316Swollman#define on_net(a,net,mask) (((ntohl(a) ^ (net)) & (mask)) == 0) 620190715Sphkint check_dst(naddr); 621190715Sphkstruct interface *check_dup(naddr, naddr, naddr, int); 622190715Sphkint check_remote(struct interface *); 623190715Sphkvoid ifinit(void); 624190715Sphkint walk_bad(struct radix_node *, struct walkarg *); 625190715Sphkint if_ok(struct interface *, const char *); 626190715Sphkvoid if_sick(struct interface *); 627190715Sphkvoid if_link(struct interface *); 628190715Sphkstruct interface *ifwithaddr(naddr addr, int bcast, int remote); 629190715Sphkstruct interface *ifwithindex(u_short, int); 630190715Sphkstruct interface *iflookup(naddr); 63119885Swollman 632190715Sphkstruct auth *find_auth(struct interface *); 633190715Sphkvoid end_md5_auth(struct ws_buf *, struct auth *); 63419885Swollman 635126250Sbms#if defined(__FreeBSD__) || defined(__NetBSD__) 636126250Sbms#include <md5.h> 637126250Sbms#else 63846303Smarkm#define MD5_DIGEST_LEN 16 63946303Smarkmtypedef struct { 64046303Smarkm u_int32_t state[4]; /* state (ABCD) */ 64146303Smarkm u_int32_t count[2]; /* # of bits, modulo 2^64 (LSB 1st) */ 64246303Smarkm unsigned char buffer[64]; /* input buffer */ 64346303Smarkm} MD5_CTX; 644190715Sphkvoid MD5Init(MD5_CTX*); 645190715Sphkvoid MD5Update(MD5_CTX*, u_char*, u_int); 646190715Sphkvoid MD5Final(u_char[MD5_DIGEST_LEN], MD5_CTX*); 647126250Sbms#endif 648