xref: /openbsd-src/sys/net/route.h (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: route.h,v 1.60 2009/03/31 01:31:26 dlg 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_int64_t	rmx_pksent;	/* packets sent using this route */
63 	u_int		rmx_locks;	/* Kernel must leave these values */
64 	u_int		rmx_mtu;	/* MTU for this path */
65 	u_int		rmx_expire;	/* lifetime for route, e.g. redirect */
66 	u_int		rmx_pad;
67 };
68 
69 /*
70  * Huge version for userland compatibility.
71  */
72 struct rt_metrics {
73 	u_int64_t	rmx_pksent;	/* packets sent using this route */
74 	u_int		rmx_locks;	/* Kernel must leave these values */
75 	u_int		rmx_mtu;	/* MTU for this path */
76 	u_int		rmx_expire;	/* lifetime for route, e.g. redirect */
77 	u_int		rmx_refcnt;	/* # references hold */
78 	/* some apps may still need these no longer used metrics */
79 	u_int		rmx_hopcount;	/* max hops expected */
80 	u_int		rmx_recvpipe;	/* inbound delay-bandwidth product */
81 	u_int		rmx_sendpipe;	/* outbound delay-bandwidth product */
82 	u_int		rmx_ssthresh;	/* outbound gateway buffer limit */
83 	u_int		rmx_rtt;	/* estimated round trip time */
84 	u_int		rmx_rttvar;	/* estimated rtt variance */
85 };
86 
87 /*
88  * rmx_rtt and rmx_rttvar are stored as microseconds;
89  * RTTTOPRHZ(rtt) converts to a value suitable for use
90  * by a protocol slowtimo counter.
91  */
92 #define	RTM_RTTUNIT	1000000	/* units for rtt, rttvar, as units per sec */
93 #define	RTTTOPRHZ(r)	((r) / (RTM_RTTUNIT / PR_SLOWHZ))
94 
95 /*
96  * We distinguish between routes to hosts and routes to networks,
97  * preferring the former if available.  For each route we infer
98  * the interface to use from the gateway address supplied when
99  * the route was entered.  Routes that forward packets through
100  * gateways are marked so that the output routines know to address the
101  * gateway rather than the ultimate destination.
102  */
103 #ifndef RNF_NORMAL
104 #include <net/radix.h>
105 #include <net/radix_mpath.h>
106 #endif
107 struct rtentry {
108 	struct	radix_node rt_nodes[2];	/* tree glue, and other values */
109 #define	rt_key(r)	((struct sockaddr *)((r)->rt_nodes->rn_key))
110 #define	rt_mask(r)	((struct sockaddr *)((r)->rt_nodes->rn_mask))
111 	struct	sockaddr *rt_gateway;	/* value */
112 	u_int	rt_flags;		/* up/down?, host/net */
113 	int	rt_refcnt;		/* # held references */
114 	struct	ifnet *rt_ifp;		/* the answer: interface to use */
115 	struct	ifaddr *rt_ifa;		/* the answer: interface addr to use */
116 	struct	sockaddr *rt_genmask;	/* for generation of cloned routes */
117 	caddr_t	rt_llinfo;		/* pointer to link level info cache or
118 					   to an MPLS structure */
119 	struct	rt_kmetrics rt_rmx;	/* metrics used by rx'ing protocols */
120 	struct	rtentry *rt_gwroute;	/* implied entry for gatewayed routes */
121 	struct	rtentry *rt_parent;	/* If cloned, parent of this route. */
122 	LIST_HEAD(, rttimer) rt_timer;  /* queue of timeouts for misc funcs */
123 	u_int16_t rt_labelid;		/* route label ID */
124 	u_int8_t rt_priority;		/* routing priority to use */
125 };
126 #define	rt_use	rt_rmx.rmx_pksent
127 
128 #define	RTF_UP		0x1		/* route usable */
129 #define	RTF_GATEWAY	0x2		/* destination is a gateway */
130 #define	RTF_HOST	0x4		/* host entry (net otherwise) */
131 #define	RTF_REJECT	0x8		/* host or net unreachable */
132 #define	RTF_DYNAMIC	0x10		/* created dynamically (by redirect) */
133 #define	RTF_MODIFIED	0x20		/* modified dynamically (by redirect) */
134 #define RTF_DONE	0x40		/* message confirmed */
135 #define RTF_MASK	0x80		/* subnet mask present */
136 #define RTF_CLONING	0x100		/* generate new routes on use */
137 #define RTF_XRESOLVE	0x200		/* external daemon resolves name */
138 #define RTF_LLINFO	0x400		/* generated by ARP or ESIS */
139 #define RTF_STATIC	0x800		/* manually added */
140 #define RTF_BLACKHOLE	0x1000		/* just discard pkts (during updates) */
141 #define RTF_PROTO3	0x2000		/* protocol specific routing flag */
142 #define RTF_PROTO2	0x4000		/* protocol specific routing flag */
143 #define RTF_PROTO1	0x8000		/* protocol specific routing flag */
144 #define RTF_CLONED	0x10000		/* this is a cloned route */
145 #define RTF_MPATH	0x40000		/* multipath route or operation */
146 #define RTF_JUMBO	0x80000		/* try to use jumbo frames */
147 #define RTF_MPLS	0x100000	/* MPLS additional infos */
148 
149 /* mask of RTF flags that are allowed to be modified by RTM_CHANGE */
150 #define RTF_FMASK	\
151     (RTF_JUMBO | RTF_PROTO1 | RTF_PROTO2 | RTF_PROTO3 | RTF_BLACKHOLE | \
152      RTF_REJECT | RTF_STATIC)
153 
154 #ifndef _KERNEL
155 /* obsoleted */
156 #define RTF_SOURCE	0x20000		/* this route has a source selector */
157 #define RTF_TUNNEL	0x100000	/* Tunnelling bit. */
158 #endif
159 
160 /* Routing priorities used by the different routing protocols */
161 #define RTP_NONE	0	/* unset priority use sane default */
162 #define RTP_CONNECTED	4	/* directly connected routes */
163 #define RTP_STATIC	8	/* static routes base priority */
164 #define RTP_OSPF	32	/* OSPF routes */
165 #define RTP_ISIS	36	/* IS-IS routes */
166 #define RTP_RIP		40	/* RIP routes */
167 #define RTP_BGP		48	/* BGP routes */
168 #define RTP_DEFAULT	56	/* routes that have nothing set */
169 #define RTP_MAX		63	/* maximum priority */
170 #define RTP_ANY		64	/* any of the above */
171 #define RTP_MASK	0x7f
172 #define RTP_DOWN	0x80	/* route/link is down */
173 
174 /*
175  * Routing statistics.
176  */
177 struct	rtstat {
178 	u_int32_t rts_badredirect;	/* bogus redirect calls */
179 	u_int32_t rts_dynamic;		/* routes created by redirects */
180 	u_int32_t rts_newgateway;	/* routes modified by redirects */
181 	u_int32_t rts_unreach;		/* lookups which failed */
182 	u_int32_t rts_wildcard;		/* lookups satisfied by a wildcard */
183 };
184 
185 /*
186  * Structures for routing messages.
187  */
188 struct rt_msghdr {
189 	u_short	rtm_msglen;	/* to skip over non-understood messages */
190 	u_char	rtm_version;	/* future binary compatibility */
191 	u_char	rtm_type;	/* message type */
192 	u_short	rtm_hdrlen;	/* sizeof(rt_msghdr) to skip over the header */
193 	u_short	rtm_index;	/* index for associated ifp */
194 	u_short rtm_tableid;	/* routing table id */
195 	u_char	rtm_priority;	/* routing priority */
196 	u_char	rtm_mpls;	/* MPLS additional infos */
197 	int	rtm_addrs;	/* bitmask identifying sockaddrs in msg */
198 	int	rtm_flags;	/* flags, incl. kern & message, e.g. DONE */
199 	int	rtm_fmask;	/* bitmask used in RTM_CHANGE message */
200 	pid_t	rtm_pid;	/* identify sender */
201 	int	rtm_seq;	/* for sender to identify action */
202 	int	rtm_errno;	/* why failed */
203 	u_int	rtm_inits;	/* which metrics we are initializing */
204 	struct	rt_metrics rtm_rmx; /* metrics themselves */
205 };
206 /* overload no longer used field */
207 #define rtm_use	rtm_rmx.rmx_pksent
208 
209 /*
210  * Comaptibility structures for version 3 messages.
211  * Keep them till after OpenBSD 4.4
212  */
213 struct rt_ometrics {
214 	u_long	rmx_locks;	/* Kernel must leave these values alone */
215 	u_long	rmx_mtu;	/* MTU for this path */
216 	u_long	rmx_hopcount;	/* max hops expected (deprecated) */
217 	u_long	rmx_expire;	/* lifetime for route, e.g. redirect */
218 	u_long	rmx_recvpipe;	/* inbound delay-bandwidth product */
219 	u_long	rmx_sendpipe;	/* outbound delay-bandwidth product */
220 	u_long	rmx_ssthresh;	/* outbound gateway buffer limit (deprecated) */
221 	u_long	rmx_rtt;	/* overloaded with rmx_rt_tableid */
222 	u_long	rmx_rttvar;	/* estimated rtt variance (deprecated) */
223 	u_long	rmx_pksent;	/* packets sent using this route */
224 };
225 
226 struct rt_omsghdr {
227 	u_short	rtm_msglen;	/* to skip over non-understood messages */
228 	u_char	rtm_version;	/* future binary compatibility */
229 	u_char	rtm_type;	/* message type */
230 	u_short	rtm_index;	/* index for associated ifp */
231 	int	rtm_flags;	/* flags, incl. kern & message, e.g. DONE */
232 	int	rtm_addrs;	/* bitmask identifying sockaddrs in msg */
233 	pid_t	rtm_pid;	/* identify sender */
234 	int	rtm_seq;	/* for sender to identify action */
235 	int	rtm_errno;	/* why failed */
236 	int	rtm_fmask;	/* was once rtm_use */
237 	u_long	rtm_inits;	/* which metrics we are initializing */
238 	struct	rt_ometrics rtm_rmx; /* metrics themselves */
239 };
240 
241 #define RTM_OVERSION	3	/* provide some minimal backward compat */
242 #define RTM_VERSION	4	/* Up the ante and ignore older versions */
243 
244 #define RTM_ADD		0x1	/* Add Route */
245 #define RTM_DELETE	0x2	/* Delete Route */
246 #define RTM_CHANGE	0x3	/* Change Metrics or flags */
247 #define RTM_GET		0x4	/* Report Metrics */
248 #define RTM_LOSING	0x5	/* Kernel Suspects Partitioning */
249 #define RTM_REDIRECT	0x6	/* Told to use different route */
250 #define RTM_MISS	0x7	/* Lookup failed on this address */
251 #define RTM_LOCK	0x8	/* fix specified metrics */
252 #define RTM_RESOLVE	0xb	/* req to resolve dst to LL addr */
253 #define RTM_NEWADDR	0xc	/* address being added to iface */
254 #define RTM_DELADDR	0xd	/* address being removed from iface */
255 #define RTM_IFINFO	0xe	/* iface going up/down etc. */
256 #define RTM_IFANNOUNCE	0xf	/* iface arrival/departure */
257 
258 #define RTV_MTU		0x1	/* init or lock _mtu */
259 #define RTV_HOPCOUNT	0x2	/* init or lock _hopcount */
260 #define RTV_EXPIRE	0x4	/* init or lock _hopcount */
261 #define RTV_RPIPE	0x8	/* init or lock _recvpipe */
262 #define RTV_SPIPE	0x10	/* init or lock _sendpipe */
263 #define RTV_SSTHRESH	0x20	/* init or lock _ssthresh */
264 #define RTV_RTT		0x40	/* init or lock _rtt */
265 #define RTV_RTTVAR	0x80	/* init or lock _rttvar */
266 
267 /*
268  * Bitmask values for rtm_addr.
269  */
270 #define RTA_DST		0x1	/* destination sockaddr present */
271 #define RTA_GATEWAY	0x2	/* gateway sockaddr present */
272 #define RTA_NETMASK	0x4	/* netmask sockaddr present */
273 #define RTA_GENMASK	0x8	/* cloning mask sockaddr present */
274 #define RTA_IFP		0x10	/* interface name sockaddr present */
275 #define RTA_IFA		0x20	/* interface addr sockaddr present */
276 #define RTA_AUTHOR	0x40	/* sockaddr for author of redirect */
277 #define RTA_BRD		0x80	/* for NEWADDR, broadcast or p-p dest addr */
278 #define RTA_SRC		0x100	/* source sockaddr present */
279 #define RTA_SRCMASK	0x200	/* source netmask present */
280 #define	RTA_LABEL	0x400	/* route label present */
281 
282 /*
283  * Index offsets for sockaddr array for alternate internal encoding.
284  */
285 #define RTAX_DST	0	/* destination sockaddr present */
286 #define RTAX_GATEWAY	1	/* gateway sockaddr present */
287 #define RTAX_NETMASK	2	/* netmask sockaddr present */
288 #define RTAX_GENMASK	3	/* cloning mask sockaddr present */
289 #define RTAX_IFP	4	/* interface name sockaddr present */
290 #define RTAX_IFA	5	/* interface addr sockaddr present */
291 #define RTAX_AUTHOR	6	/* sockaddr for author of redirect */
292 #define RTAX_BRD	7	/* for NEWADDR, broadcast or p-p dest addr */
293 #define RTAX_SRC	8	/* source sockaddr present */
294 #define RTAX_SRCMASK	9	/* source netmask present */
295 #define RTAX_LABEL	10	/* route label present */
296 #define RTAX_MAX	11	/* size of array to allocate */
297 
298 /*
299  * setsockopt defines used for the filtering.
300  */
301 #define ROUTE_MSGFILTER	1	/* bitmask to specifiy which types should be
302 				   sent to the client. */
303 
304 #define ROUTE_FILTER(m)	(1 << (m))
305 
306 struct rt_addrinfo {
307 	int	rti_addrs;
308 	struct	sockaddr *rti_info[RTAX_MAX];
309 	int	rti_flags;
310 	struct	ifaddr *rti_ifa;
311 	struct	ifnet *rti_ifp;
312 	struct	rt_msghdr *rti_rtm;
313 	u_char	rti_mpls;
314 };
315 
316 struct route_cb {
317 	int	ip_count;
318 	int	ip6_count;
319 	int     mpls_count;
320 	int	any_count;
321 };
322 
323 /*
324  * This structure, and the prototypes for the rt_timer_{init,remove_all,
325  * add,timer} functions all used with the kind permission of BSDI.
326  * These allow functions to be called for routes at specific times.
327  */
328 
329 struct rttimer {
330 	TAILQ_ENTRY(rttimer)	rtt_next;  /* entry on timer queue */
331 	LIST_ENTRY(rttimer) 	rtt_link;  /* multiple timers per rtentry */
332 	struct rttimer_queue	*rtt_queue;/* back pointer to queue */
333 	struct rtentry  	*rtt_rt;   /* Back pointer to the route */
334 	void            	(*rtt_func)(struct rtentry *,
335 						 struct rttimer *);
336 	time_t          	rtt_time; /* When this timer was registered */
337 };
338 
339 struct rttimer_queue {
340 	long				rtq_timeout;
341 	unsigned long			rtq_count;
342 	TAILQ_HEAD(, rttimer)		rtq_head;
343 	LIST_ENTRY(rttimer_queue)	rtq_link;
344 };
345 
346 #define	RTLABEL_LEN	32
347 
348 struct sockaddr_rtlabel {
349 	u_int8_t	sr_len;			/* total length */
350 	sa_family_t	sr_family;		/* address family */
351 	char		sr_label[RTLABEL_LEN];
352 };
353 
354 #define	RT_TABLEID_MAX	255
355 
356 #ifdef _KERNEL
357 const char	*rtlabel_id2name(u_int16_t);
358 u_int16_t	 rtlabel_name2id(char *);
359 void		 rtlabel_unref(u_int16_t);
360 
361 #define	RTFREE(rt) do {							\
362 	if ((rt)->rt_refcnt <= 1)					\
363 		rtfree(rt);						\
364 	else								\
365 		(rt)->rt_refcnt--;					\
366 } while (/* CONSTCOND */0)
367 
368 /*
369  * Values for additional argument to rtalloc_noclone() and rtalloc2()
370  */
371 #define	ALL_CLONING 0
372 #define	ONNET_CLONING 1
373 #define	NO_CLONING 2
374 
375 extern struct route_cb route_cb;
376 extern struct rtstat rtstat;
377 extern const struct sockaddr_rtin rt_defmask4;
378 
379 struct	socket;
380 void	 route_init(void);
381 int	 rtable_add(u_int);
382 int	 rtable_exists(u_int);
383 int	 route_output(struct mbuf *, ...);
384 int	 route_usrreq(struct socket *, int, struct mbuf *,
385 			   struct mbuf *, struct mbuf *, struct proc *);
386 void	 rt_ifmsg(struct ifnet *);
387 void	 rt_ifannouncemsg(struct ifnet *, int);
388 void	 rt_maskedcopy(struct sockaddr *,
389 	    struct sockaddr *, struct sockaddr *);
390 void	 rt_missmsg(int, struct rt_addrinfo *, int, struct ifnet *, int,
391 	    u_int);
392 void	 rt_newaddrmsg(int, struct ifaddr *, int, struct rtentry *);
393 int	 rt_setgate(struct rtentry *, struct sockaddr *,
394 	    struct sockaddr *, u_int);
395 void	 rt_setmetrics(u_long, struct rt_metrics *, struct rt_kmetrics *);
396 void	 rt_getmetrics(struct rt_kmetrics *, struct rt_metrics *);
397 int      rt_timer_add(struct rtentry *,
398              void(*)(struct rtentry *, struct rttimer *),
399 	     struct rttimer_queue *);
400 void	 rt_timer_init(void);
401 struct rttimer_queue *
402 	 rt_timer_queue_create(u_int);
403 void	 rt_timer_queue_change(struct rttimer_queue *, long);
404 void	 rt_timer_queue_destroy(struct rttimer_queue *, int);
405 void	 rt_timer_remove_all(struct rtentry *);
406 unsigned long	rt_timer_count(struct rttimer_queue *);
407 void	 rt_timer_timer(void *);
408 void	 rtalloc(struct route *);
409 #ifdef SMALL_KERNEL
410 #define	rtalloc_mpath(r, s, t)	rtalloc(r)
411 #endif
412 struct rtentry *
413 	 rtalloc1(struct sockaddr *, int, u_int);
414 void	 rtalloc_noclone(struct route *, int);
415 struct rtentry *
416 	 rtalloc2(struct sockaddr *, int, int);
417 void	 rtfree(struct rtentry *);
418 int	 rt_getifa(struct rt_addrinfo *);
419 int	 rtinit(struct ifaddr *, int, int);
420 int	 rtioctl(u_long, caddr_t, struct proc *);
421 void	 rtredirect(struct sockaddr *, struct sockaddr *,
422 			 struct sockaddr *, int, struct sockaddr *,
423 			 struct rtentry **);
424 int	 rtrequest1(int, struct rt_addrinfo *, u_int8_t, struct rtentry **,
425 	     u_int);
426 void	 rt_if_remove(struct ifnet *);
427 #ifndef SMALL_KERNEL
428 void	 rt_if_track(struct ifnet *);
429 #endif
430 int	 rtdeletemsg(struct rtentry *, u_int);
431 
432 struct radix_node_head	*rt_gettable(sa_family_t, u_int);
433 struct radix_node	*rt_lookup(struct sockaddr *, struct sockaddr *, int);
434 #endif /* _KERNEL */
435 #endif /* _NET_ROUTE_H_ */
436