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