1 /*	$OpenBSD: route.h,v 1.31 2005/06/25 19:25:06 henning Exp $	*/
2 /*	$NetBSD: route.h,v 1.9 1996/02/13 22:00:49 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)route.h	8.3 (Berkeley) 4/19/94
33  */
34 
35 #ifndef _NET_ROUTE_H_
36 #define _NET_ROUTE_H_
37 
38 #include <sys/queue.h>
39 
40 /*
41  * Kernel resident routing tables.
42  *
43  * The routing tables are initialized when interface addresses
44  * are set by making entries for all directly connected interfaces.
45  */
46 
47 /*
48  * A route consists of a destination address and a reference
49  * to a routing entry.  These are often held by protocols
50  * in their control blocks, e.g. inpcb.
51  */
52 struct route {
53 	struct	rtentry *ro_rt;
54 	struct	sockaddr ro_dst;
55 };
56 
57 /*
58  * These numbers are used by reliable protocols for determining
59  * retransmission behavior and are included in the routing structure.
60  */
61 struct rt_kmetrics {
62 	u_long	rmx_locks;	/* Kernel must leave these values alone */
63 	u_long	rmx_mtu;	/* MTU for this path */
64 	u_long	rmx_expire;	/* lifetime for route, e.g. redirect */
65 	u_long	rmx_pksent;	/* packets sent using this route */
66 };
67 
68 /*
69  * Huge version for userland compatibility.
70  */
71 struct rt_metrics {
72 	u_long	rmx_locks;	/* Kernel must leave these values alone */
73 	u_long	rmx_mtu;	/* MTU for this path */
74 	u_long	rmx_hopcount;	/* max hops expected */
75 	u_long	rmx_expire;	/* lifetime for route, e.g. redirect */
76 	u_long	rmx_recvpipe;	/* inbound delay-bandwidth product */
77 	u_long	rmx_sendpipe;	/* outbound delay-bandwidth product */
78 	u_long	rmx_ssthresh;	/* outbound gateway buffer limit */
79 	u_long	rmx_rtt;	/* estimated round trip time */
80 	u_long	rmx_rttvar;	/* estimated rtt variance */
81 	u_long	rmx_pksent;	/* packets sent using this route */
82 };
83 
84 /*
85  * rmx_rtt and rmx_rttvar are stored as microseconds;
86  * RTTTOPRHZ(rtt) converts to a value suitable for use
87  * by a protocol slowtimo counter.
88  */
89 #define	RTM_RTTUNIT	1000000	/* units for rtt, rttvar, as units per sec */
90 #define	RTTTOPRHZ(r)	((r) / (RTM_RTTUNIT / PR_SLOWHZ))
91 
92 /*
93  * We distinguish between routes to hosts and routes to networks,
94  * preferring the former if available.  For each route we infer
95  * the interface to use from the gateway address supplied when
96  * the route was entered.  Routes that forward packets through
97  * gateways are marked so that the output routines know to address the
98  * gateway rather than the ultimate destination.
99  */
100 #ifndef RNF_NORMAL
101 #include <net/radix.h>
102 #include <net/radix_mpath.h>
103 #endif
104 struct rtentry {
105 	struct	radix_node rt_nodes[2];	/* tree glue, and other values */
106 #define	rt_key(r)	((struct sockaddr *)((r)->rt_nodes->rn_key))
107 #define	rt_mask(r)	((struct sockaddr *)((r)->rt_nodes->rn_mask))
108 	struct	sockaddr *rt_gateway;	/* value */
109 	u_int	rt_flags;		/* up/down?, host/net */
110 	int	rt_refcnt;		/* # held references */
111 	struct	ifnet *rt_ifp;		/* the answer: interface to use */
112 	struct	ifaddr *rt_ifa;		/* the answer: interface to use */
113 	struct	sockaddr *rt_genmask;	/* for generation of cloned routes */
114 	caddr_t	rt_llinfo;		/* pointer to link level info cache */
115 	struct	rt_kmetrics rt_rmx;	/* metrics used by rx'ing protocols */
116 	struct	rtentry *rt_gwroute;	/* implied entry for gatewayed routes */
117 	struct	rtentry *rt_parent;	/* If cloned, parent of this route. */
118 	LIST_HEAD(, rttimer) rt_timer;  /* queue of timeouts for misc funcs */
119 	u_int16_t rt_labelid;		/* route label ID */
120 };
121 #define	rt_use	rt_rmx.rmx_pksent
122 
123 #define	RTF_UP		0x1		/* route usable */
124 #define	RTF_GATEWAY	0x2		/* destination is a gateway */
125 #define	RTF_HOST	0x4		/* host entry (net otherwise) */
126 #define	RTF_REJECT	0x8		/* host or net unreachable */
127 #define	RTF_DYNAMIC	0x10		/* created dynamically (by redirect) */
128 #define	RTF_MODIFIED	0x20		/* modified dynamically (by redirect) */
129 #define RTF_DONE	0x40		/* message confirmed */
130 #define RTF_MASK	0x80		/* subnet mask present */
131 #define RTF_CLONING	0x100		/* generate new routes on use */
132 #define RTF_XRESOLVE	0x200		/* external daemon resolves name */
133 #define RTF_LLINFO	0x400		/* generated by ARP or ESIS */
134 #define RTF_STATIC	0x800		/* manually added */
135 #define RTF_BLACKHOLE	0x1000		/* just discard pkts (during updates) */
136 #define RTF_PROTO3	0x2000		/* protocol specific routing flag */
137 #define RTF_PROTO2	0x4000		/* protocol specific routing flag */
138 #define RTF_PROTO1	0x8000		/* protocol specific routing flag */
139 #define RTF_CLONED	0x10000		/* this is a cloned route */
140 #define RTF_SOURCE	0x20000		/* this route has a source selector */
141 #define RTF_MPATH	0x40000		/* multipath route or operation */
142 #define RTF_JUMBO	0x80000		/* try to use jumbo frames */
143 
144 #ifndef _KERNEL
145 /* obsoleted */
146 #define	RTF_TUNNEL	0x100000	/* Tunnelling bit. */
147 #endif
148 
149 /*
150  * Routing statistics.
151  */
152 struct	rtstat {
153 	u_int32_t rts_badredirect;	/* bogus redirect calls */
154 	u_int32_t rts_dynamic;		/* routes created by redirects */
155 	u_int32_t rts_newgateway;		/* routes modified by redirects */
156 	u_int32_t rts_unreach;		/* lookups which failed */
157 	u_int32_t rts_wildcard;		/* lookups satisfied by a wildcard */
158 };
159 
160 /*
161  * Structures for routing messages.
162  */
163 struct rt_msghdr {
164 	u_short	rtm_msglen;	/* to skip over non-understood messages */
165 	u_char	rtm_version;	/* future binary compatibility */
166 	u_char	rtm_type;	/* message type */
167 	u_short	rtm_index;	/* index for associated ifp */
168 	int	rtm_flags;	/* flags, incl. kern & message, e.g. DONE */
169 	int	rtm_addrs;	/* bitmask identifying sockaddrs in msg */
170 	pid_t	rtm_pid;	/* identify sender */
171 	int	rtm_seq;	/* for sender to identify action */
172 	int	rtm_errno;	/* why failed */
173 	int	rtm_use;	/* from rtentry */
174 #define rtm_fmask	rtm_use	/* bitmask used in RTM_CHANGE message */
175 	u_long	rtm_inits;	/* which metrics we are initializing */
176 	struct	rt_metrics rtm_rmx; /* metrics themselves */
177 };
178 
179 #define RTM_VERSION	3	/* Up the ante and ignore older versions */
180 
181 #define RTM_ADD		0x1	/* Add Route */
182 #define RTM_DELETE	0x2	/* Delete Route */
183 #define RTM_CHANGE	0x3	/* Change Metrics or flags */
184 #define RTM_GET		0x4	/* Report Metrics */
185 #define RTM_LOSING	0x5	/* Kernel Suspects Partitioning */
186 #define RTM_REDIRECT	0x6	/* Told to use different route */
187 #define RTM_MISS	0x7	/* Lookup failed on this address */
188 #define RTM_LOCK	0x8	/* fix specified metrics */
189 #define RTM_RESOLVE	0xb	/* req to resolve dst to LL addr */
190 #define RTM_NEWADDR	0xc	/* address being added to iface */
191 #define RTM_DELADDR	0xd	/* address being removed from iface */
192 #define RTM_IFINFO	0xe	/* iface going up/down etc. */
193 #define RTM_IFANNOUNCE	0xf	/* iface arrival/departure */
194 
195 #define RTV_MTU		0x1	/* init or lock _mtu */
196 #define RTV_HOPCOUNT	0x2	/* init or lock _hopcount */
197 #define RTV_EXPIRE	0x4	/* init or lock _hopcount */
198 #define RTV_RPIPE	0x8	/* init or lock _recvpipe */
199 #define RTV_SPIPE	0x10	/* init or lock _sendpipe */
200 #define RTV_SSTHRESH	0x20	/* init or lock _ssthresh */
201 #define RTV_RTT		0x40	/* init or lock _rtt */
202 #define RTV_RTTVAR	0x80	/* init or lock _rttvar */
203 
204 /*
205  * Bitmask values for rtm_addr.
206  */
207 #define RTA_DST		0x1	/* destination sockaddr present */
208 #define RTA_GATEWAY	0x2	/* gateway sockaddr present */
209 #define RTA_NETMASK	0x4	/* netmask sockaddr present */
210 #define RTA_GENMASK	0x8	/* cloning mask sockaddr present */
211 #define RTA_IFP		0x10	/* interface name sockaddr present */
212 #define RTA_IFA		0x20	/* interface addr sockaddr present */
213 #define RTA_AUTHOR	0x40	/* sockaddr for author of redirect */
214 #define RTA_BRD		0x80	/* for NEWADDR, broadcast or p-p dest addr */
215 #define RTA_SRC		0x100	/* source sockaddr present */
216 #define RTA_SRCMASK	0x200	/* source netmask present */
217 #define	RTA_LABEL	0x400	/* route label present */
218 
219 /*
220  * Index offsets for sockaddr array for alternate internal encoding.
221  */
222 #define RTAX_DST	0	/* destination sockaddr present */
223 #define RTAX_GATEWAY	1	/* gateway sockaddr present */
224 #define RTAX_NETMASK	2	/* netmask sockaddr present */
225 #define RTAX_GENMASK	3	/* cloning mask sockaddr present */
226 #define RTAX_IFP	4	/* interface name sockaddr present */
227 #define RTAX_IFA	5	/* interface addr sockaddr present */
228 #define RTAX_AUTHOR	6	/* sockaddr for author of redirect */
229 #define RTAX_BRD	7	/* for NEWADDR, broadcast or p-p dest addr */
230 #define RTAX_SRC	8	/* source sockaddr present */
231 #define RTAX_SRCMASK	9	/* source netmask present */
232 #define RTAX_LABEL	10	/* route label present */
233 #define RTAX_MAX	11	/* size of array to allocate */
234 
235 struct rt_addrinfo {
236 	int	rti_addrs;
237 	struct	sockaddr *rti_info[RTAX_MAX];
238 	int	rti_flags;
239 	struct	ifaddr *rti_ifa;
240 	struct	ifnet *rti_ifp;
241 	struct	rt_msghdr *rti_rtm;
242 };
243 
244 struct route_cb {
245 	int	ip_count;
246 	int	ip6_count;
247 	int	any_count;
248 };
249 
250 /*
251  * This structure, and the prototypes for the rt_timer_{init,remove_all,
252  * add,timer} functions all used with the kind permission of BSDI.
253  * These allow functions to be called for routes at specific times.
254  */
255 
256 struct rttimer {
257 	TAILQ_ENTRY(rttimer)	rtt_next;  /* entry on timer queue */
258 	LIST_ENTRY(rttimer) 	rtt_link;  /* multiple timers per rtentry */
259 	struct rttimer_queue	*rtt_queue;/* back pointer to queue */
260 	struct rtentry  	*rtt_rt;   /* Back pointer to the route */
261 	void            	(*rtt_func)(struct rtentry *,
262 						 struct rttimer *);
263 	time_t          	rtt_time; /* When this timer was registered */
264 };
265 
266 struct rttimer_queue {
267 	long				rtq_timeout;
268 	unsigned long			rtq_count;
269 	TAILQ_HEAD(, rttimer)		rtq_head;
270 	LIST_ENTRY(rttimer_queue)	rtq_link;
271 };
272 
273 #define	RTLABEL_LEN	32
274 
275 struct sockaddr_rtlabel {
276 	u_int8_t	sr_len;			/* total length */
277 	sa_family_t	sr_family;		/* address family */
278 	char		sr_label[RTLABEL_LEN];
279 };
280 
281 #ifdef _KERNEL
282 const char	*rtlabel_id2name(u_int16_t);
283 u_int16_t	 rtlabel_name2id(char *);
284 void		 rtlabel_unref(u_int16_t);
285 
286 #define	RTFREE(rt) do { \
287 	if ((rt)->rt_refcnt <= 1) \
288 		rtfree(rt); \
289 	else \
290 		(rt)->rt_refcnt--; \
291 } while (0)
292 
293 /*
294  * Values for additional argument to rtalloc_noclone() and rtalloc2()
295  */
296 #define	ALL_CLONING 0
297 #define	ONNET_CLONING 1
298 #define	NO_CLONING 2
299 
300 extern struct route_cb route_cb;
301 extern struct rtstat rtstat;
302 extern struct radix_node_head *rt_tables[];
303 extern const struct sockaddr_rtin rt_defmask4;
304 
305 struct	socket;
306 void	 route_init(void);
307 int	 route_output(struct mbuf *, ...);
308 int	 route_usrreq(struct socket *, int, struct mbuf *,
309 			   struct mbuf *, struct mbuf *);
310 void	 rt_ifmsg(struct ifnet *);
311 void	 rt_ifannouncemsg(struct ifnet *, int);
312 void	 rt_maskedcopy(struct sockaddr *,
313 	    struct sockaddr *, struct sockaddr *);
314 void	 rt_missmsg(int, struct rt_addrinfo *, int, int);
315 void	 rt_newaddrmsg(int, struct ifaddr *, int, struct rtentry *);
316 int	 rt_setgate(struct rtentry *, struct sockaddr *,
317 			 struct sockaddr *);
318 void	 rt_setmetrics(u_long, struct rt_metrics *, struct rt_kmetrics *);
319 void	 rt_getmetrics(struct rt_kmetrics *, struct rt_metrics *);
320 int      rt_timer_add(struct rtentry *,
321              void(*)(struct rtentry *, struct rttimer *),
322 	     struct rttimer_queue *);
323 void	 rt_timer_init(void);
324 struct rttimer_queue *
325 	 rt_timer_queue_create(u_int);
326 void	 rt_timer_queue_change(struct rttimer_queue *, long);
327 void	 rt_timer_queue_destroy(struct rttimer_queue *, int);
328 void	 rt_timer_remove_all(struct rtentry *);
329 unsigned long	rt_timer_count(struct rttimer_queue *);
330 void	 rt_timer_timer(void *);
331 void	 rtable_init(void **);
332 void	 rtalloc(struct route *);
333 struct rtentry *
334 	 rtalloc1(struct sockaddr *, int);
335 void	 rtalloc_noclone(struct route *, int);
336 struct rtentry *
337 	 rtalloc2(struct sockaddr *, int, int);
338 void	 rtfree(struct rtentry *);
339 int	 rt_getifa(struct rt_addrinfo *);
340 int	 rtinit(struct ifaddr *, int, int);
341 int	 rtioctl(u_long, caddr_t, struct proc *);
342 void	 rtredirect(struct sockaddr *, struct sockaddr *,
343 			 struct sockaddr *, int, struct sockaddr *,
344 			 struct rtentry **);
345 int	 rtrequest(int, struct sockaddr *,
346 			struct sockaddr *, struct sockaddr *, int,
347 			struct rtentry **);
348 int	 rtrequest1(int, struct rt_addrinfo *, struct rtentry **);
349 
350 #endif /* _KERNEL */
351 #endif /* _NET_ROUTE_H_ */
352