route.h revision 122922
11541Srgrimes/*
21541Srgrimes * Copyright (c) 1980, 1986, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes *
3385052Sru *	@(#)route.h	8.4 (Berkeley) 1/9/95
3450477Speter * $FreeBSD: head/sys/net/route.h 122922 2003-11-20 20:07:39Z andre $
351541Srgrimes */
361541Srgrimes
372168Spaul#ifndef _NET_ROUTE_H_
382168Spaul#define _NET_ROUTE_H_
392168Spaul
401541Srgrimes/*
411541Srgrimes * Kernel resident routing tables.
428876Srgrimes *
431541Srgrimes * The routing tables are initialized when interface addresses
441541Srgrimes * are set by making entries for all directly connected interfaces.
451541Srgrimes */
461541Srgrimes
471541Srgrimes/*
481541Srgrimes * A route consists of a destination address and a reference
491541Srgrimes * to a routing entry.  These are often held by protocols
501541Srgrimes * in their control blocks, e.g. inpcb.
511541Srgrimes */
521541Srgrimesstruct route {
531541Srgrimes	struct	rtentry *ro_rt;
541541Srgrimes	struct	sockaddr ro_dst;
551541Srgrimes};
561541Srgrimes
571541Srgrimes/*
581541Srgrimes * These numbers are used by reliable protocols for determining
591541Srgrimes * retransmission behavior and are included in the routing structure.
601541Srgrimes */
61122922Sandrestruct rt_metrics_lite {
62122922Sandre	u_long	rmx_mtu;	/* MTU for this path */
63122922Sandre	u_long	rmx_expire;	/* lifetime for route, e.g. redirect */
64122922Sandre	u_long	rmx_pksent;	/* packets sent using this route */
65122922Sandre};
66122922Sandre
671541Srgrimesstruct rt_metrics {
681541Srgrimes	u_long	rmx_locks;	/* Kernel must leave these values alone */
691541Srgrimes	u_long	rmx_mtu;	/* MTU for this path */
701541Srgrimes	u_long	rmx_hopcount;	/* max hops expected */
711541Srgrimes	u_long	rmx_expire;	/* lifetime for route, e.g. redirect */
7213765Smpp	u_long	rmx_recvpipe;	/* inbound delay-bandwidth product */
7313765Smpp	u_long	rmx_sendpipe;	/* outbound delay-bandwidth product */
741541Srgrimes	u_long	rmx_ssthresh;	/* outbound gateway buffer limit */
751541Srgrimes	u_long	rmx_rtt;	/* estimated round trip time */
761541Srgrimes	u_long	rmx_rttvar;	/* estimated rtt variance */
771541Srgrimes	u_long	rmx_pksent;	/* packets sent using this route */
785791Swollman	u_long	rmx_filler[4];	/* will be used for T/TCP later */
791541Srgrimes};
801541Srgrimes
811541Srgrimes/*
821541Srgrimes * rmx_rtt and rmx_rttvar are stored as microseconds;
831541Srgrimes * RTTTOPRHZ(rtt) converts to a value suitable for use
841541Srgrimes * by a protocol slowtimo counter.
851541Srgrimes */
861541Srgrimes#define	RTM_RTTUNIT	1000000	/* units for rtt, rttvar, as units per sec */
871541Srgrimes#define	RTTTOPRHZ(r)	((r) / (RTM_RTTUNIT / PR_SLOWHZ))
881541Srgrimes
891541Srgrimes/*
905833Sbde * XXX kernel function pointer `rt_output' is visible to applications.
915833Sbde */
925833Sbdestruct mbuf;
935833Sbde
945833Sbde/*
951541Srgrimes * We distinguish between routes to hosts and routes to networks,
961541Srgrimes * preferring the former if available.  For each route we infer
971541Srgrimes * the interface to use from the gateway address supplied when
981541Srgrimes * the route was entered.  Routes that forward packets through
991541Srgrimes * gateways are marked so that the output routines know to address the
1001541Srgrimes * gateway rather than the ultimate destination.
1011541Srgrimes */
1021541Srgrimes#ifndef RNF_NORMAL
1031541Srgrimes#include <net/radix.h>
1041541Srgrimes#endif
1051541Srgrimesstruct rtentry {
1061541Srgrimes	struct	radix_node rt_nodes[2];	/* tree glue, and other values */
1071541Srgrimes#define	rt_key(r)	((struct sockaddr *)((r)->rt_nodes->rn_key))
1081541Srgrimes#define	rt_mask(r)	((struct sockaddr *)((r)->rt_nodes->rn_mask))
1091541Srgrimes	struct	sockaddr *rt_gateway;	/* value */
11048381Smsmith	long	rt_refcnt;		/* # held references */
1117197Swollman	u_long	rt_flags;		/* up/down?, host/net */
1121541Srgrimes	struct	ifnet *rt_ifp;		/* the answer: interface to use */
113122922Sandre	struct	ifaddr *rt_ifa;		/* the answer: interface address to use */
1141541Srgrimes	struct	sockaddr *rt_genmask;	/* for generation of cloned routes */
1151541Srgrimes	caddr_t	rt_llinfo;		/* pointer to link level info cache */
116122922Sandre	struct	rt_metrics_lite rt_rmx;	/* metrics used by rx'ing protocols */
1171541Srgrimes	struct	rtentry *rt_gwroute;	/* implied entry for gatewayed routes */
11893084Sbde	int	(*rt_output)(struct ifnet *, struct mbuf *, struct sockaddr *,
11993084Sbde		    struct rtentry *);
1205791Swollman					/* output routine for this (rt,if) */
1215791Swollman	struct	rtentry *rt_parent; 	/* cloning parent of this route */
122120727Ssam#ifdef _KERNEL
123120727Ssam	/* XXX ugly, user apps use this definition but don't have a mtx def */
124120727Ssam	struct	mtx rt_mtx;		/* mutex for routing entry */
125120727Ssam#endif
1261541Srgrimes};
1271541Srgrimes
1281541Srgrimes/*
1291541Srgrimes * Following structure necessary for 4.3 compatibility;
1301541Srgrimes * We should eventually move it to a compat file.
1311541Srgrimes */
1321541Srgrimesstruct ortentry {
1331541Srgrimes	u_long	rt_hash;		/* to speed lookups */
1341541Srgrimes	struct	sockaddr rt_dst;	/* key */
1351541Srgrimes	struct	sockaddr rt_gateway;	/* value */
1361541Srgrimes	short	rt_flags;		/* up/down?, host/net */
1371541Srgrimes	short	rt_refcnt;		/* # held references */
1381541Srgrimes	u_long	rt_use;			/* raw # packets forwarded */
1391541Srgrimes	struct	ifnet *rt_ifp;		/* the answer: interface to use */
1401541Srgrimes};
1411541Srgrimes
1424104Swollman#define rt_use rt_rmx.rmx_pksent
1434104Swollman
1441541Srgrimes#define	RTF_UP		0x1		/* route usable */
1451541Srgrimes#define	RTF_GATEWAY	0x2		/* destination is a gateway */
1461541Srgrimes#define	RTF_HOST	0x4		/* host entry (net otherwise) */
1471541Srgrimes#define	RTF_REJECT	0x8		/* host or net unreachable */
1481541Srgrimes#define	RTF_DYNAMIC	0x10		/* created dynamically (by redirect) */
1491541Srgrimes#define	RTF_MODIFIED	0x20		/* modified dynamically (by redirect) */
1501541Srgrimes#define RTF_DONE	0x40		/* message confirmed */
15186764Sjlemon/*			0x80		   unused, was RTF_DELCLONE */
1521541Srgrimes#define RTF_CLONING	0x100		/* generate new routes on use */
1531541Srgrimes#define RTF_XRESOLVE	0x200		/* external daemon resolves name */
15417835Sjulian#define RTF_LLINFO	0x400		/* generated by link layer (e.g. ARP) */
1551541Srgrimes#define RTF_STATIC	0x800		/* manually added */
1561541Srgrimes#define RTF_BLACKHOLE	0x1000		/* just discard pkts (during updates) */
1571541Srgrimes#define RTF_PROTO2	0x4000		/* protocol specific routing flag */
1581541Srgrimes#define RTF_PROTO1	0x8000		/* protocol specific routing flag */
1591541Srgrimes
160122921Sandre/* XXX: temporary to stay API/ABI compatible with userland */
161122921Sandre#ifndef _KERNEL
162122921Sandre#define RTF_PRCLONING	0x10000		/* unused, for compatibility */
163122921Sandre#endif
164122921Sandre
1655099Swollman#define RTF_WASCLONED	0x20000		/* route generated through cloning */
1665099Swollman#define RTF_PROTO3	0x40000		/* protocol specific routing flag */
16718839Swollman/*			0x80000		   unused */
1686245Swollman#define RTF_PINNED	0x100000	/* future use */
16915652Swollman#define	RTF_LOCAL	0x200000 	/* route represents a local address */
17015652Swollman#define	RTF_BROADCAST	0x400000	/* route represents a bcast address */
17115652Swollman#define	RTF_MULTICAST	0x800000	/* route represents a mcast address */
17215652Swollman					/* 0x1000000 and up unassigned */
1731541Srgrimes
1741541Srgrimes/*
1751541Srgrimes * Routing statistics.
1761541Srgrimes */
1771541Srgrimesstruct	rtstat {
1781541Srgrimes	short	rts_badredirect;	/* bogus redirect calls */
1791541Srgrimes	short	rts_dynamic;		/* routes created by redirects */
1801541Srgrimes	short	rts_newgateway;		/* routes modified by redirects */
1811541Srgrimes	short	rts_unreach;		/* lookups which failed */
1821541Srgrimes	short	rts_wildcard;		/* lookups satisfied by a wildcard */
1831541Srgrimes};
1841541Srgrimes/*
1851541Srgrimes * Structures for routing messages.
1861541Srgrimes */
1871541Srgrimesstruct rt_msghdr {
1881541Srgrimes	u_short	rtm_msglen;	/* to skip over non-understood messages */
1891541Srgrimes	u_char	rtm_version;	/* future binary compatibility */
1901541Srgrimes	u_char	rtm_type;	/* message type */
1911541Srgrimes	u_short	rtm_index;	/* index for associated ifp */
1921541Srgrimes	int	rtm_flags;	/* flags, incl. kern & message, e.g. DONE */
1931541Srgrimes	int	rtm_addrs;	/* bitmask identifying sockaddrs in msg */
1941541Srgrimes	pid_t	rtm_pid;	/* identify sender */
1951541Srgrimes	int	rtm_seq;	/* for sender to identify action */
1961541Srgrimes	int	rtm_errno;	/* why failed */
1971541Srgrimes	int	rtm_use;	/* from rtentry */
1981541Srgrimes	u_long	rtm_inits;	/* which metrics we are initializing */
1991541Srgrimes	struct	rt_metrics rtm_rmx; /* metrics themselves */
2001541Srgrimes};
2011541Srgrimes
2025791Swollman#define RTM_VERSION	5	/* Up the ante and ignore older versions */
2031541Srgrimes
20451252Sru/*
20551252Sru * Message types.
20651252Sru */
2071541Srgrimes#define RTM_ADD		0x1	/* Add Route */
2081541Srgrimes#define RTM_DELETE	0x2	/* Delete Route */
2091541Srgrimes#define RTM_CHANGE	0x3	/* Change Metrics or flags */
2101541Srgrimes#define RTM_GET		0x4	/* Report Metrics */
2111541Srgrimes#define RTM_LOSING	0x5	/* Kernel Suspects Partitioning */
2121541Srgrimes#define RTM_REDIRECT	0x6	/* Told to use different route */
2131541Srgrimes#define RTM_MISS	0x7	/* Lookup failed on this address */
2141541Srgrimes#define RTM_LOCK	0x8	/* fix specified metrics */
2151541Srgrimes#define RTM_OLDADD	0x9	/* caused by SIOCADDRT */
2161541Srgrimes#define RTM_OLDDEL	0xa	/* caused by SIOCDELRT */
2171541Srgrimes#define RTM_RESOLVE	0xb	/* req to resolve dst to LL addr */
2181541Srgrimes#define RTM_NEWADDR	0xc	/* address being added to iface */
2191541Srgrimes#define RTM_DELADDR	0xd	/* address being removed from iface */
2201541Srgrimes#define RTM_IFINFO	0xe	/* iface going up/down etc. */
22121666Swollman#define	RTM_NEWMADDR	0xf	/* mcast group membership being added to if */
22221666Swollman#define	RTM_DELMADDR	0x10	/* mcast group membership being deleted */
22389498Sru#define	RTM_IFANNOUNCE	0x11	/* iface arrival/departure */
2241541Srgrimes
22551252Sru/*
22651252Sru * Bitmask values for rtm_inits and rmx_locks.
22751252Sru */
2281541Srgrimes#define RTV_MTU		0x1	/* init or lock _mtu */
2291541Srgrimes#define RTV_HOPCOUNT	0x2	/* init or lock _hopcount */
23051252Sru#define RTV_EXPIRE	0x4	/* init or lock _expire */
2311541Srgrimes#define RTV_RPIPE	0x8	/* init or lock _recvpipe */
2321541Srgrimes#define RTV_SPIPE	0x10	/* init or lock _sendpipe */
2331541Srgrimes#define RTV_SSTHRESH	0x20	/* init or lock _ssthresh */
2341541Srgrimes#define RTV_RTT		0x40	/* init or lock _rtt */
2351541Srgrimes#define RTV_RTTVAR	0x80	/* init or lock _rttvar */
2361541Srgrimes
2371541Srgrimes/*
23851252Sru * Bitmask values for rtm_addrs.
2391541Srgrimes */
2401541Srgrimes#define RTA_DST		0x1	/* destination sockaddr present */
2411541Srgrimes#define RTA_GATEWAY	0x2	/* gateway sockaddr present */
2421541Srgrimes#define RTA_NETMASK	0x4	/* netmask sockaddr present */
2431541Srgrimes#define RTA_GENMASK	0x8	/* cloning mask sockaddr present */
2441541Srgrimes#define RTA_IFP		0x10	/* interface name sockaddr present */
2451541Srgrimes#define RTA_IFA		0x20	/* interface addr sockaddr present */
2461541Srgrimes#define RTA_AUTHOR	0x40	/* sockaddr for author of redirect */
2471541Srgrimes#define RTA_BRD		0x80	/* for NEWADDR, broadcast or p-p dest addr */
2481541Srgrimes
2491541Srgrimes/*
2501541Srgrimes * Index offsets for sockaddr array for alternate internal encoding.
2511541Srgrimes */
2521541Srgrimes#define RTAX_DST	0	/* destination sockaddr present */
2531541Srgrimes#define RTAX_GATEWAY	1	/* gateway sockaddr present */
2541541Srgrimes#define RTAX_NETMASK	2	/* netmask sockaddr present */
2551541Srgrimes#define RTAX_GENMASK	3	/* cloning mask sockaddr present */
2561541Srgrimes#define RTAX_IFP	4	/* interface name sockaddr present */
2571541Srgrimes#define RTAX_IFA	5	/* interface addr sockaddr present */
2581541Srgrimes#define RTAX_AUTHOR	6	/* sockaddr for author of redirect */
2591541Srgrimes#define RTAX_BRD	7	/* for NEWADDR, broadcast or p-p dest addr */
2601541Srgrimes#define RTAX_MAX	8	/* size of array to allocate */
2611541Srgrimes
2621541Srgrimesstruct rt_addrinfo {
2631541Srgrimes	int	rti_addrs;
2641541Srgrimes	struct	sockaddr *rti_info[RTAX_MAX];
26585074Sru	int	rti_flags;
26685074Sru	struct	ifaddr *rti_ifa;
26785074Sru	struct	ifnet *rti_ifp;
2681541Srgrimes};
2691541Srgrimes
27055205Speter#ifdef _KERNEL
271117752Shsu
272120727Ssam#define	RT_LOCK_INIT(_rt) \
273120727Ssam	mtx_init(&(_rt)->rt_mtx, "rtentry", NULL, MTX_DEF | MTX_DUPOK)
274120727Ssam#define	RT_LOCK(_rt)		mtx_lock(&(_rt)->rt_mtx)
275120727Ssam#define	RT_UNLOCK(_rt)		mtx_unlock(&(_rt)->rt_mtx)
276120727Ssam#define	RT_LOCK_DESTROY(_rt)	mtx_destroy(&(_rt)->rt_mtx)
277120727Ssam#define	RT_LOCK_ASSERT(_rt)	mtx_assert(&(_rt)->rt_mtx, MA_OWNED)
278117752Shsu
279122334Ssam#define	RT_ADDREF(_rt)	do {					\
280122334Ssam	RT_LOCK_ASSERT(_rt);					\
281122334Ssam	KASSERT((_rt)->rt_refcnt >= 0,				\
282122334Ssam		("negative refcnt %ld", (_rt)->rt_refcnt));	\
283122334Ssam	(_rt)->rt_refcnt++;					\
284122334Ssam} while (0);
285122334Ssam#define	RT_REMREF(_rt)	do {					\
286122334Ssam	RT_LOCK_ASSERT(_rt);					\
287122334Ssam	KASSERT((_rt)->rt_refcnt > 0,				\
288122334Ssam		("bogus refcnt %ld", (_rt)->rt_refcnt));	\
289122334Ssam	(_rt)->rt_refcnt--;					\
290122334Ssam} while (0);
291122334Ssam
292122334Ssam#define	RTFREE_LOCKED(_rt) do {					\
293122334Ssam		if ((_rt)->rt_refcnt <= 1)			\
294122334Ssam			rtfree(_rt);				\
295122334Ssam		else {						\
296122334Ssam			RT_REMREF(_rt);				\
297122334Ssam			RT_UNLOCK(_rt);				\
298122334Ssam		}						\
299122334Ssam		/* guard against invalid refs */		\
300122334Ssam		_rt = 0;					\
30146568Speter	} while (0)
302122334Ssam#define	RTFREE(_rt) do {					\
303122334Ssam		RT_LOCK(_rt);					\
304122334Ssam		RTFREE_LOCKED(_rt);				\
305120727Ssam	} while (0)
3061541Srgrimes
3079759Sbdeextern struct radix_node_head *rt_tables[AF_MAX+1];
3081541Srgrimes
30921666Swollmanstruct ifmultiaddr;
31021666Swollman
31192725Salfredvoid	 route_init(void);
31292725Salfredint	 rt_getifa(struct rt_addrinfo *);
31392725Salfredvoid	 rt_ifannouncemsg(struct ifnet *, int);
31492725Salfredvoid	 rt_ifmsg(struct ifnet *);
31592725Salfredvoid	 rt_missmsg(int, struct rt_addrinfo *, int, int);
31692725Salfredvoid	 rt_newaddrmsg(int, struct ifaddr *, int, struct rtentry *);
31792725Salfredvoid	 rt_newmaddrmsg(int, struct ifmultiaddr *);
318120727Ssamvoid	 rtalloc(struct route *);
31992725Salfredint	 rt_setgate(struct rtentry *, struct sockaddr *, struct sockaddr *);
32092725Salfredvoid	 rtalloc_ign(struct route *, u_long);
321120727Ssam/* NB: the rtentry is returned locked */
322120727Ssamstruct rtentry *rtalloc1(struct sockaddr *, int, u_long);
323121770Ssamint	 rtexpunge(struct rtentry *);
32492725Salfredvoid	 rtfree(struct rtentry *);
32592725Salfredint	 rtinit(struct ifaddr *, int, int);
32692725Salfredint	 rtioctl(u_long, caddr_t);
32792725Salfredvoid	 rtredirect(struct sockaddr *, struct sockaddr *,
328120727Ssam	    struct sockaddr *, int, struct sockaddr *);
32992725Salfredint	 rtrequest(int, struct sockaddr *,
33092725Salfred	    struct sockaddr *, struct sockaddr *, int, struct rtentry **);
33192725Salfredint	 rtrequest1(int, struct rt_addrinfo *, struct rtentry **);
332111767Smdoddint	 rt_check(struct rtentry **, struct rtentry **, struct sockaddr *);
3331541Srgrimes#endif
3342168Spaul
3352168Spaul#endif
336