1139823Simp/*-
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 * 4. Neither the name of the University nor the names of its contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
2985052Sru *	@(#)route.h	8.4 (Berkeley) 1/9/95
3050477Speter * $FreeBSD: stable/11/sys/net/route.h 341241 2018-11-29 15:56:46Z markj $
311541Srgrimes */
321541Srgrimes
332168Spaul#ifndef _NET_ROUTE_H_
342168Spaul#define _NET_ROUTE_H_
352168Spaul
36262763Sglebius#include <sys/counter.h>
37271916Shrs#include <net/vnet.h>
38262763Sglebius
391541Srgrimes/*
401541Srgrimes * Kernel resident routing tables.
418876Srgrimes *
421541Srgrimes * The routing tables are initialized when interface addresses
431541Srgrimes * are set by making entries for all directly connected interfaces.
441541Srgrimes */
451541Srgrimes
461541Srgrimes/*
47293098Smelifaro * Struct route consiste of a destination address,
48293098Smelifaro * a route entry pointer, link-layer prepend data pointer along
49293098Smelifaro * with its length.
501541Srgrimes */
511541Srgrimesstruct route {
521541Srgrimes	struct	rtentry *ro_rt;
53301217Sgnn	struct	llentry *ro_lle;
54301217Sgnn	/*
55301217Sgnn	 * ro_prepend and ro_plen are only used for bpf to pass in a
56301217Sgnn	 * preformed header.  They are not cacheable.
57301217Sgnn	 */
58292978Smelifaro	char		*ro_prepend;
59292978Smelifaro	uint16_t	ro_plen;
60292978Smelifaro	uint16_t	ro_flags;
61293098Smelifaro	uint16_t	ro_mtu;	/* saved ro_rt mtu */
62293098Smelifaro	uint16_t	spare;
631541Srgrimes	struct	sockaddr ro_dst;
641541Srgrimes};
651541Srgrimes
66292978Smelifaro#define	RT_L2_ME_BIT		2	/* dst L2 addr is our address */
67292978Smelifaro#define	RT_MAY_LOOP_BIT		3	/* dst may require loop copy */
68292978Smelifaro#define	RT_HAS_HEADER_BIT	4	/* mbuf already have its header prepended */
69292978Smelifaro
70238092Sglebius#define	RT_CACHING_CONTEXT	0x1	/* XXX: not used anywhere */
71238092Sglebius#define	RT_NORTREF		0x2	/* doesn't hold reference on ro_rt */
72293544Smelifaro#define	RT_L2_ME		(1 << RT_L2_ME_BIT)		/* 0x0004 */
73293544Smelifaro#define	RT_MAY_LOOP		(1 << RT_MAY_LOOP_BIT)		/* 0x0008 */
74293544Smelifaro#define	RT_HAS_HEADER		(1 << RT_HAS_HEADER_BIT)	/* 0x0010 */
75225698Skmacy
76293544Smelifaro#define	RT_REJECT		0x0020		/* Destination is reject */
77293544Smelifaro#define	RT_BLACKHOLE		0x0040		/* Destination is blackhole */
78293544Smelifaro#define	RT_HAS_GW		0x0080		/* Destination has GW  */
79301217Sgnn#define	RT_LLE_CACHE		0x0100		/* Cache link layer  */
80293544Smelifaro
811541Srgrimesstruct rt_metrics {
821541Srgrimes	u_long	rmx_locks;	/* Kernel must leave these values alone */
831541Srgrimes	u_long	rmx_mtu;	/* MTU for this path */
841541Srgrimes	u_long	rmx_hopcount;	/* max hops expected */
851541Srgrimes	u_long	rmx_expire;	/* lifetime for route, e.g. redirect */
8613765Smpp	u_long	rmx_recvpipe;	/* inbound delay-bandwidth product */
8713765Smpp	u_long	rmx_sendpipe;	/* outbound delay-bandwidth product */
881541Srgrimes	u_long	rmx_ssthresh;	/* outbound gateway buffer limit */
891541Srgrimes	u_long	rmx_rtt;	/* estimated round trip time */
901541Srgrimes	u_long	rmx_rttvar;	/* estimated rtt variance */
911541Srgrimes	u_long	rmx_pksent;	/* packets sent using this route */
92191080Skmacy	u_long	rmx_weight;	/* route weight */
93191080Skmacy	u_long	rmx_filler[3];	/* will be used for T/TCP later */
941541Srgrimes};
951541Srgrimes
961541Srgrimes/*
971541Srgrimes * rmx_rtt and rmx_rttvar are stored as microseconds;
981541Srgrimes * RTTTOPRHZ(rtt) converts to a value suitable for use
991541Srgrimes * by a protocol slowtimo counter.
1001541Srgrimes */
1011541Srgrimes#define	RTM_RTTUNIT	1000000	/* units for rtt, rttvar, as units per sec */
1021541Srgrimes#define	RTTTOPRHZ(r)	((r) / (RTM_RTTUNIT / PR_SLOWHZ))
1031541Srgrimes
104292333Smelifaro/* lle state is exported in rmx_state rt_metrics field */
105292333Smelifaro#define	rmx_state	rmx_weight
106292333Smelifaro
107297225Sgnn/*
108297225Sgnn * Keep a generation count of routing table, incremented on route addition,
109297225Sgnn * so we can invalidate caches.  This is accessed without a lock, as precision
110297225Sgnn * is not required.
111297225Sgnn */
112297225Sgnntypedef volatile u_int rt_gen_t;	/* tree generation (for adds) */
113297225Sgnn#define RT_GEN(fibnum, af)	rt_tables_get_gen(fibnum, af)
114297225Sgnn
115231852Sbz#define	RT_DEFAULT_FIB	0	/* Explicitly mark fib=0 restricted cases */
116260460Smelifaro#define	RT_ALL_FIBS	-1	/* Announce event for every fib */
117271923Shrs#ifdef _KERNEL
118260460Smelifaroextern u_int rt_numfibs;	/* number of usable routing tables */
119271916ShrsVNET_DECLARE(u_int, rt_add_addr_allfibs); /* Announce interfaces to all fibs */
120271916Shrs#define	V_rt_add_addr_allfibs	VNET(rt_add_addr_allfibs)
121271923Shrs#endif
1225833Sbde
1235833Sbde/*
1241541Srgrimes * We distinguish between routes to hosts and routes to networks,
1251541Srgrimes * preferring the former if available.  For each route we infer
1261541Srgrimes * the interface to use from the gateway address supplied when
1271541Srgrimes * the route was entered.  Routes that forward packets through
1281541Srgrimes * gateways are marked so that the output routines know to address the
1291541Srgrimes * gateway rather than the ultimate destination.
1301541Srgrimes */
1311541Srgrimes#ifndef RNF_NORMAL
1321541Srgrimes#include <net/radix.h>
133178167Sqingli#ifdef RADIX_MPATH
134178167Sqingli#include <net/radix_mpath.h>
1351541Srgrimes#endif
136178167Sqingli#endif
137262767Sglebius
138262767Sglebius#if defined(_KERNEL) || defined(_WANT_RTENTRY)
1391541Srgrimesstruct rtentry {
1401541Srgrimes	struct	radix_node rt_nodes[2];	/* tree glue, and other values */
141128454Sluigi	/*
142128454Sluigi	 * XXX struct rtentry must begin with a struct radix_node (or two!)
143128454Sluigi	 * because the code does some casts of a 'struct radix_node *'
144128454Sluigi	 * to a 'struct rtentry *'
145128454Sluigi	 */
146132780Skan#define	rt_key(r)	(*((struct sockaddr **)(&(r)->rt_nodes->rn_key)))
147132780Skan#define	rt_mask(r)	(*((struct sockaddr **)(&(r)->rt_nodes->rn_mask)))
1481541Srgrimes	struct	sockaddr *rt_gateway;	/* value */
1491541Srgrimes	struct	ifnet *rt_ifp;		/* the answer: interface to use */
150122922Sandre	struct	ifaddr *rt_ifa;		/* the answer: interface address to use */
151262763Sglebius	int		rt_flags;	/* up/down?, host/net */
152262763Sglebius	int		rt_refcnt;	/* # held references */
153262763Sglebius	u_int		rt_fibnum;	/* which FIB */
154262763Sglebius	u_long		rt_mtu;		/* MTU for this path */
155262763Sglebius	u_long		rt_weight;	/* absolute weight */
156262763Sglebius	u_long		rt_expire;	/* lifetime for route, e.g. redirect */
157262763Sglebius#define	rt_endzero	rt_pksent
158262763Sglebius	counter_u64_t	rt_pksent;	/* packets sent using this route */
159262763Sglebius	struct mtx	rt_mtx;		/* mutex for routing entry */
160291466Smelifaro	struct rtentry	*rt_chain;	/* pointer to next rtentry to delete */
1611541Srgrimes};
162262767Sglebius#endif /* _KERNEL || _WANT_RTENTRY */
1631541Srgrimes
1641541Srgrimes#define	RTF_UP		0x1		/* route usable */
1651541Srgrimes#define	RTF_GATEWAY	0x2		/* destination is a gateway */
1661541Srgrimes#define	RTF_HOST	0x4		/* host entry (net otherwise) */
1671541Srgrimes#define	RTF_REJECT	0x8		/* host or net unreachable */
1681541Srgrimes#define	RTF_DYNAMIC	0x10		/* created dynamically (by redirect) */
1691541Srgrimes#define	RTF_MODIFIED	0x20		/* modified dynamically (by redirect) */
1701541Srgrimes#define RTF_DONE	0x40		/* message confirmed */
17186764Sjlemon/*			0x80		   unused, was RTF_DELCLONE */
172186119Sqingli/*			0x100		   unused, was RTF_CLONING */
1731541Srgrimes#define RTF_XRESOLVE	0x200		/* external daemon resolves name */
174187094Sqingli#define RTF_LLINFO	0x400		/* DEPRECATED - exists ONLY for backward
175187094Sqingli					   compatibility */
176186500Sqingli#define RTF_LLDATA	0x400		/* used by apps to add/del L2 entries */
1771541Srgrimes#define RTF_STATIC	0x800		/* manually added */
1781541Srgrimes#define RTF_BLACKHOLE	0x1000		/* just discard pkts (during updates) */
1791541Srgrimes#define RTF_PROTO2	0x4000		/* protocol specific routing flag */
1801541Srgrimes#define RTF_PROTO1	0x8000		/* protocol specific routing flag */
181263203Sglebius/*			0x10000		   unused, was RTF_PRCLONING */
182186119Sqingli/*			0x20000		   unused, was RTF_WASCLONED */
1835099Swollman#define RTF_PROTO3	0x40000		/* protocol specific routing flag */
184274611Smelifaro#define	RTF_FIXEDMTU	0x80000		/* MTU was explicitly specified */
185248070Smelifaro#define RTF_PINNED	0x100000	/* route is immutable */
18615652Swollman#define	RTF_LOCAL	0x200000 	/* route represents a local address */
18715652Swollman#define	RTF_BROADCAST	0x400000	/* route represents a bcast address */
18815652Swollman#define	RTF_MULTICAST	0x800000	/* route represents a mcast address */
189191080Skmacy					/* 0x8000000 and up unassigned */
190191080Skmacy#define	RTF_STICKY	 0x10000000	/* always route dst->src */
1911541Srgrimes
192320134Sae#define	RTF_RNH_LOCKED	 0x40000000	/* radix node head is locked */
193191080Skmacy
194252184Sqingli#define	RTF_GWFLAG_COMPAT 0x80000000	/* a compatibility bit for interacting
195252184Sqingli					   with existing routing apps */
196252184Sqingli
197156750Sandre/* Mask of RTF flags that are allowed to be modified by RTM_CHANGE. */
198156750Sandre#define RTF_FMASK	\
199156750Sandre	(RTF_PROTO1 | RTF_PROTO2 | RTF_PROTO3 | RTF_BLACKHOLE | \
200191080Skmacy	 RTF_REJECT | RTF_STATIC | RTF_STICKY)
201156750Sandre
2021541Srgrimes/*
203291993Smelifaro * fib_ nexthop API flags.
204291993Smelifaro */
205291993Smelifaro
206291993Smelifaro/* Consumer-visible nexthop info flags */
207291993Smelifaro#define	NHF_REJECT		0x0010	/* RTF_REJECT */
208291993Smelifaro#define	NHF_BLACKHOLE		0x0020	/* RTF_BLACKHOLE */
209291993Smelifaro#define	NHF_REDIRECT		0x0040	/* RTF_DYNAMIC|RTF_MODIFIED */
210291993Smelifaro#define	NHF_DEFAULT		0x0080	/* Default route */
211291993Smelifaro#define	NHF_BROADCAST		0x0100	/* RTF_BROADCAST */
212291993Smelifaro#define	NHF_GATEWAY		0x0200	/* RTF_GATEWAY */
213291993Smelifaro
214291993Smelifaro/* Nexthop request flags */
215291993Smelifaro#define	NHR_IFAIF		0x01	/* Return ifa_ifp interface */
216291993Smelifaro#define	NHR_REF			0x02	/* For future use */
217291993Smelifaro
218293159Smelifaro/* Control plane route request flags */
219293159Smelifaro#define	NHR_COPY		0x100	/* Copy rte data */
220293159Smelifaro
221293611Smelifaro#ifdef _KERNEL
222293544Smelifaro/* rte<>ro_flags translation */
223293544Smelifarostatic inline void
224293544Smelifarort_update_ro_flags(struct route *ro)
225293544Smelifaro{
226293544Smelifaro	int rt_flags = ro->ro_rt->rt_flags;
227293544Smelifaro
228293544Smelifaro	ro->ro_flags &= ~ (RT_REJECT|RT_BLACKHOLE|RT_HAS_GW);
229293544Smelifaro
230293656Smelifaro	ro->ro_flags |= (rt_flags & RTF_REJECT) ? RT_REJECT : 0;
231293544Smelifaro	ro->ro_flags |= (rt_flags & RTF_BLACKHOLE) ? RT_BLACKHOLE : 0;
232293544Smelifaro	ro->ro_flags |= (rt_flags & RTF_GATEWAY) ? RT_HAS_GW : 0;
233293544Smelifaro}
234293611Smelifaro#endif
235293544Smelifaro
236291993Smelifaro/*
2371541Srgrimes * Routing statistics.
2381541Srgrimes */
2391541Srgrimesstruct	rtstat {
2401541Srgrimes	short	rts_badredirect;	/* bogus redirect calls */
2411541Srgrimes	short	rts_dynamic;		/* routes created by redirects */
2421541Srgrimes	short	rts_newgateway;		/* routes modified by redirects */
2431541Srgrimes	short	rts_unreach;		/* lookups which failed */
2441541Srgrimes	short	rts_wildcard;		/* lookups satisfied by a wildcard */
2451541Srgrimes};
2461541Srgrimes/*
2471541Srgrimes * Structures for routing messages.
2481541Srgrimes */
2491541Srgrimesstruct rt_msghdr {
2501541Srgrimes	u_short	rtm_msglen;	/* to skip over non-understood messages */
2511541Srgrimes	u_char	rtm_version;	/* future binary compatibility */
2521541Srgrimes	u_char	rtm_type;	/* message type */
2531541Srgrimes	u_short	rtm_index;	/* index for associated ifp */
254341241Smarkj	u_short _rtm_spare1;
2551541Srgrimes	int	rtm_flags;	/* flags, incl. kern & message, e.g. DONE */
2561541Srgrimes	int	rtm_addrs;	/* bitmask identifying sockaddrs in msg */
2571541Srgrimes	pid_t	rtm_pid;	/* identify sender */
2581541Srgrimes	int	rtm_seq;	/* for sender to identify action */
2591541Srgrimes	int	rtm_errno;	/* why failed */
260156750Sandre	int	rtm_fmask;	/* bitmask used in RTM_CHANGE message */
2611541Srgrimes	u_long	rtm_inits;	/* which metrics we are initializing */
2621541Srgrimes	struct	rt_metrics rtm_rmx; /* metrics themselves */
2631541Srgrimes};
2641541Srgrimes
265191124Skmacy#define RTM_VERSION	5	/* Up the ante and ignore older versions */
2661541Srgrimes
26751252Sru/*
26851252Sru * Message types.
26951252Sru */
2701541Srgrimes#define RTM_ADD		0x1	/* Add Route */
2711541Srgrimes#define RTM_DELETE	0x2	/* Delete Route */
2721541Srgrimes#define RTM_CHANGE	0x3	/* Change Metrics or flags */
2731541Srgrimes#define RTM_GET		0x4	/* Report Metrics */
2741541Srgrimes#define RTM_LOSING	0x5	/* Kernel Suspects Partitioning */
2751541Srgrimes#define RTM_REDIRECT	0x6	/* Told to use different route */
2761541Srgrimes#define RTM_MISS	0x7	/* Lookup failed on this address */
2771541Srgrimes#define RTM_LOCK	0x8	/* fix specified metrics */
278263203Sglebius		    /*	0x9  */
279263203Sglebius		    /*	0xa  */
2801541Srgrimes#define RTM_RESOLVE	0xb	/* req to resolve dst to LL addr */
2811541Srgrimes#define RTM_NEWADDR	0xc	/* address being added to iface */
2821541Srgrimes#define RTM_DELADDR	0xd	/* address being removed from iface */
2831541Srgrimes#define RTM_IFINFO	0xe	/* iface going up/down etc. */
28421666Swollman#define	RTM_NEWMADDR	0xf	/* mcast group membership being added to if */
28521666Swollman#define	RTM_DELMADDR	0x10	/* mcast group membership being deleted */
28689498Sru#define	RTM_IFANNOUNCE	0x11	/* iface arrival/departure */
287136155Ssam#define	RTM_IEEE80211	0x12	/* IEEE80211 wireless event */
2881541Srgrimes
28951252Sru/*
29051252Sru * Bitmask values for rtm_inits and rmx_locks.
29151252Sru */
2921541Srgrimes#define RTV_MTU		0x1	/* init or lock _mtu */
2931541Srgrimes#define RTV_HOPCOUNT	0x2	/* init or lock _hopcount */
29451252Sru#define RTV_EXPIRE	0x4	/* init or lock _expire */
2951541Srgrimes#define RTV_RPIPE	0x8	/* init or lock _recvpipe */
2961541Srgrimes#define RTV_SPIPE	0x10	/* init or lock _sendpipe */
2971541Srgrimes#define RTV_SSTHRESH	0x20	/* init or lock _ssthresh */
2981541Srgrimes#define RTV_RTT		0x40	/* init or lock _rtt */
2991541Srgrimes#define RTV_RTTVAR	0x80	/* init or lock _rttvar */
300191080Skmacy#define RTV_WEIGHT	0x100	/* init or lock _weight */
3011541Srgrimes
3021541Srgrimes/*
30351252Sru * Bitmask values for rtm_addrs.
3041541Srgrimes */
3051541Srgrimes#define RTA_DST		0x1	/* destination sockaddr present */
3061541Srgrimes#define RTA_GATEWAY	0x2	/* gateway sockaddr present */
3071541Srgrimes#define RTA_NETMASK	0x4	/* netmask sockaddr present */
3081541Srgrimes#define RTA_GENMASK	0x8	/* cloning mask sockaddr present */
3091541Srgrimes#define RTA_IFP		0x10	/* interface name sockaddr present */
3101541Srgrimes#define RTA_IFA		0x20	/* interface addr sockaddr present */
3111541Srgrimes#define RTA_AUTHOR	0x40	/* sockaddr for author of redirect */
3121541Srgrimes#define RTA_BRD		0x80	/* for NEWADDR, broadcast or p-p dest addr */
3131541Srgrimes
3141541Srgrimes/*
3151541Srgrimes * Index offsets for sockaddr array for alternate internal encoding.
3161541Srgrimes */
3171541Srgrimes#define RTAX_DST	0	/* destination sockaddr present */
3181541Srgrimes#define RTAX_GATEWAY	1	/* gateway sockaddr present */
3191541Srgrimes#define RTAX_NETMASK	2	/* netmask sockaddr present */
3201541Srgrimes#define RTAX_GENMASK	3	/* cloning mask sockaddr present */
3211541Srgrimes#define RTAX_IFP	4	/* interface name sockaddr present */
3221541Srgrimes#define RTAX_IFA	5	/* interface addr sockaddr present */
3231541Srgrimes#define RTAX_AUTHOR	6	/* sockaddr for author of redirect */
3241541Srgrimes#define RTAX_BRD	7	/* for NEWADDR, broadcast or p-p dest addr */
3251541Srgrimes#define RTAX_MAX	8	/* size of array to allocate */
3261541Srgrimes
327291466Smelifarotypedef int rt_filter_f_t(const struct rtentry *, void *);
328291466Smelifaro
3291541Srgrimesstruct rt_addrinfo {
330291466Smelifaro	int	rti_addrs;			/* Route RTF_ flags */
331291466Smelifaro	int	rti_flags;			/* Route RTF_ flags */
332291466Smelifaro	struct	sockaddr *rti_info[RTAX_MAX];	/* Sockaddr data */
333291466Smelifaro	struct	ifaddr *rti_ifa;		/* value of rt_ifa addr */
334291466Smelifaro	struct	ifnet *rti_ifp;			/* route interface */
335291466Smelifaro	rt_filter_f_t	*rti_filter;		/* filter function */
336291466Smelifaro	void	*rti_filterdata;		/* filter paramenters */
337291466Smelifaro	u_long	rti_mflags;			/* metrics RTV_ flags */
338291466Smelifaro	u_long	rti_spare;			/* Will be used for fib */
339291466Smelifaro	struct	rt_metrics *rti_rmx;		/* Pointer to route metrics */
3401541Srgrimes};
3411541Srgrimes
342128185Sluigi/*
343128185Sluigi * This macro returns the size of a struct sockaddr when passed
344128185Sluigi * through a routing socket. Basically we round up sa_len to
345128185Sluigi * a multiple of sizeof(long), with a minimum of sizeof(long).
346128185Sluigi * The check for a NULL pointer is just a convenience, probably never used.
347128185Sluigi * The case sa_len == 0 should only apply to empty structures.
348128185Sluigi */
349128185Sluigi#define SA_SIZE(sa)						\
350128185Sluigi    (  (!(sa) || ((struct sockaddr *)(sa))->sa_len == 0) ?	\
351128185Sluigi	sizeof(long)		:				\
352128185Sluigi	1 + ( (((struct sockaddr *)(sa))->sa_len - 1) | (sizeof(long) - 1) ) )
353128185Sluigi
354264973Smelifaro#define	sa_equal(a, b) (	\
355287476Smelifaro    (((const struct sockaddr *)(a))->sa_len == ((const struct sockaddr *)(b))->sa_len) && \
356287476Smelifaro    (bcmp((a), (b), ((const struct sockaddr *)(b))->sa_len) == 0))
357264973Smelifaro
35855205Speter#ifdef _KERNEL
359117752Shsu
360205222Sqingli#define RT_LINK_IS_UP(ifp)	(!((ifp)->if_capabilities & IFCAP_LINKSTATE) \
361205024Sqingli				 || (ifp)->if_link_state == LINK_STATE_UP)
362204902Sqingli
363120727Ssam#define	RT_LOCK_INIT(_rt) \
364120727Ssam	mtx_init(&(_rt)->rt_mtx, "rtentry", NULL, MTX_DEF | MTX_DUPOK)
365120727Ssam#define	RT_LOCK(_rt)		mtx_lock(&(_rt)->rt_mtx)
366120727Ssam#define	RT_UNLOCK(_rt)		mtx_unlock(&(_rt)->rt_mtx)
367120727Ssam#define	RT_LOCK_DESTROY(_rt)	mtx_destroy(&(_rt)->rt_mtx)
368120727Ssam#define	RT_LOCK_ASSERT(_rt)	mtx_assert(&(_rt)->rt_mtx, MA_OWNED)
369262806Sglebius#define	RT_UNLOCK_COND(_rt)	do {				\
370262806Sglebius	if (mtx_owned(&(_rt)->rt_mtx))				\
371262806Sglebius		mtx_unlock(&(_rt)->rt_mtx);			\
372262806Sglebius} while (0)
373117752Shsu
374122334Ssam#define	RT_ADDREF(_rt)	do {					\
375122334Ssam	RT_LOCK_ASSERT(_rt);					\
376122334Ssam	KASSERT((_rt)->rt_refcnt >= 0,				\
377186119Sqingli		("negative refcnt %d", (_rt)->rt_refcnt));	\
378122334Ssam	(_rt)->rt_refcnt++;					\
379150130Sandre} while (0)
380182801Sjulian
381122334Ssam#define	RT_REMREF(_rt)	do {					\
382122334Ssam	RT_LOCK_ASSERT(_rt);					\
383122334Ssam	KASSERT((_rt)->rt_refcnt > 0,				\
384186119Sqingli		("bogus refcnt %d", (_rt)->rt_refcnt));	\
385122334Ssam	(_rt)->rt_refcnt--;					\
386150130Sandre} while (0)
387122334Ssam
388122334Ssam#define	RTFREE_LOCKED(_rt) do {					\
389183017Sjulian	if ((_rt)->rt_refcnt <= 1)				\
390183017Sjulian		rtfree(_rt);					\
391183017Sjulian	else {							\
392183017Sjulian		RT_REMREF(_rt);					\
393183017Sjulian		RT_UNLOCK(_rt);					\
394183017Sjulian	}							\
395183017Sjulian	/* guard against invalid refs */			\
396183017Sjulian	_rt = 0;						\
397183017Sjulian} while (0)
398182801Sjulian
399122334Ssam#define	RTFREE(_rt) do {					\
400183017Sjulian	RT_LOCK(_rt);						\
401183017Sjulian	RTFREE_LOCKED(_rt);					\
402183017Sjulian} while (0)
4031541Srgrimes
404238092Sglebius#define	RO_RTFREE(_ro) do {					\
405238092Sglebius	if ((_ro)->ro_rt) {					\
406238092Sglebius		if ((_ro)->ro_flags & RT_NORTREF) {		\
407238092Sglebius			(_ro)->ro_flags &= ~RT_NORTREF;		\
408238092Sglebius			(_ro)->ro_rt = NULL;			\
409301217Sgnn			(_ro)->ro_lle = NULL;			\
410238092Sglebius		} else {					\
411238092Sglebius			RT_LOCK((_ro)->ro_rt);			\
412238092Sglebius			RTFREE_LOCKED((_ro)->ro_rt);		\
413238092Sglebius		}						\
414238092Sglebius	}							\
415238092Sglebius} while (0)
416238092Sglebius
417297225Sgnn/*
418297225Sgnn * Validate a cached route based on a supplied cookie.  If there is an
419297225Sgnn * out-of-date cache, simply free it.  Update the generation number
420297225Sgnn * for the new allocation
421297225Sgnn */
422297225Sgnn#define RT_VALIDATE(ro, cookiep, fibnum) do {				\
423297225Sgnn	rt_gen_t cookie = RT_GEN(fibnum, (ro)->ro_dst.sa_family);	\
424301217Sgnn	if (*(cookiep) != cookie) {					\
425301217Sgnn		if ((ro)->ro_rt != NULL) {				\
426301217Sgnn			RTFREE((ro)->ro_rt);				\
427301217Sgnn			(ro)->ro_rt = NULL;				\
428301217Sgnn		}							\
429297225Sgnn		*(cookiep) = cookie;					\
430297225Sgnn	}								\
431297225Sgnn} while (0)
432297225Sgnn
43321666Swollmanstruct ifmultiaddr;
434294706Smelifarostruct rib_head;
43521666Swollman
436136155Ssamvoid	 rt_ieee80211msg(struct ifnet *, int, void *, size_t);
43792725Salfredvoid	 rt_ifannouncemsg(struct ifnet *, int);
43892725Salfredvoid	 rt_ifmsg(struct ifnet *);
43992725Salfredvoid	 rt_missmsg(int, struct rt_addrinfo *, int, int);
440225837Sbzvoid	 rt_missmsg_fib(int, struct rt_addrinfo *, int, int, int);
44192725Salfredvoid	 rt_newaddrmsg(int, struct ifaddr *, int, struct rtentry *);
442225837Sbzvoid	 rt_newaddrmsg_fib(int, struct ifaddr *, int, struct rtentry *, int);
443260488Smelifaroint	 rt_addrmsg(int, struct ifaddr *, int);
444260488Smelifaroint	 rt_routemsg(int, struct ifnet *ifp, int, struct rtentry *, int);
44592725Salfredvoid	 rt_newmaddrmsg(int, struct ifmultiaddr *);
44692725Salfredint	 rt_setgate(struct rtentry *, struct sockaddr *, struct sockaddr *);
447201282Sqinglivoid 	 rt_maskedcopy(struct sockaddr *, struct sockaddr *, struct sockaddr *);
448294706Smelifarostruct rib_head *rt_table_init(int);
449294706Smelifarovoid	rt_table_destroy(struct rib_head *);
450297234Sbzu_int	rt_tables_get_gen(int table, int fam);
451128621Sluigi
452260488Smelifaroint	rtsock_addrmsg(int, struct ifaddr *, int);
453260488Smelifaroint	rtsock_routemsg(int, struct ifnet *ifp, int, struct rtentry *, int);
454260488Smelifaro
455128621Sluigi/*
456128621Sluigi * Note the following locking behavior:
457128621Sluigi *
458128621Sluigi *    rtalloc1() returns a locked rtentry
459128621Sluigi *
460128621Sluigi *    rtfree() and RTFREE_LOCKED() require a locked rtentry
461128621Sluigi *
462128621Sluigi *    RTFREE() uses an unlocked entry.
463128621Sluigi */
464128621Sluigi
465178888Sjulianvoid	 rtfree(struct rtentry *);
466274611Smelifarovoid	rt_updatemtu(struct ifnet *);
467178888Sjulian
468286458Smelifarotypedef int rt_walktree_f_t(struct rtentry *, void *);
469294706Smelifarotypedef void rt_setwarg_t(struct rib_head *, uint32_t, int, void *);
470286594Smelifarovoid	rt_foreach_fib_walk(int af, rt_setwarg_t *, rt_walktree_f_t *, void *);
471291466Smelifarovoid	rt_foreach_fib_walk_del(int af, rt_filter_f_t *filter_f, void *arg);
472301502Sbzvoid	rt_flushifroutes_af(struct ifnet *, int);
473286458Smelifarovoid	rt_flushifroutes(struct ifnet *ifp);
474286458Smelifaro
475178888Sjulian/* XXX MRT COMPAT VERSIONS THAT SET UNIVERSE to 0 */
476178888Sjulian/* Thes are used by old code not yet converted to use multiple FIBS */
477120727Ssamstruct rtentry *rtalloc1(struct sockaddr *, int, u_long);
47892725Salfredint	 rtinit(struct ifaddr *, int, int);
479174559Skmacy
480178888Sjulian/* XXX MRT NEW VERSIONS THAT USE FIBs
481178888Sjulian * For now the protocol indepedent versions are the same as the AF_INET ones
482178888Sjulian * but this will change..
483178888Sjulian */
484178888Sjulianint	 rt_getifa_fib(struct rt_addrinfo *, u_int fibnum);
485178888Sjulianvoid	 rtalloc_ign_fib(struct route *ro, u_long ignflags, u_int fibnum);
486178888Sjulianstruct rtentry *rtalloc1_fib(struct sockaddr *, int, u_long, u_int);
487178888Sjulianint	 rtioctl_fib(u_long, caddr_t, u_int);
488178888Sjulianvoid	 rtredirect_fib(struct sockaddr *, struct sockaddr *,
489178888Sjulian	    struct sockaddr *, int, struct sockaddr *, u_int);
490178888Sjulianint	 rtrequest_fib(int, struct sockaddr *,
491178888Sjulian	    struct sockaddr *, struct sockaddr *, int, struct rtentry **, u_int);
492178888Sjulianint	 rtrequest1_fib(int, struct rt_addrinfo *, struct rtentry **, u_int);
493293159Smelifaroint	rib_lookup_info(uint32_t, const struct sockaddr *, uint32_t, uint32_t,
494293159Smelifaro	    struct rt_addrinfo *);
495293159Smelifarovoid	rib_free_info(struct rt_addrinfo *info);
496178888Sjulian
4971541Srgrimes#endif
4982168Spaul
4992168Spaul#endif
500