2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1980, 1986, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)route.h 8.3 (Berkeley) 4/19/94
61 * $FreeBSD: src/sys/net/route.h,v 1.36.2.1 2000/08/16 06:14:23 jayanth Exp $
66 #include <sys/appleapiopts.h>
68 #include <sys/types.h>
69 #include <sys/socket.h>
72 * Kernel resident routing tables.
74 * The routing tables are initialized when interface addresses
75 * are set by making entries for all directly connected interfaces.
79 * A route consists of a destination address and a reference
80 * to a routing entry. These are often held by protocols
81 * in their control blocks, e.g. inpcb.
87 * N.B: struct route must begin with ro_rt and ro_flags
88 * because the code does some casts of a 'struct route_in6 *'
89 * to a 'struct route *'.
91 struct rtentry
*ro_rt
;
92 uint32_t ro_flags
; /* route flags (see below) */
93 struct sockaddr ro_dst
;
96 #define ROF_SRCIF_SELECTED 0x1 /* source interface was selected */
99 * Route reachability info (private)
101 struct rt_reach_info
{
102 u_int32_t ri_refcnt
; /* reference count */
103 u_int32_t ri_probes
; /* total # of probes */
104 u_int64_t ri_snd_expire
; /* transmit expiration (calendar) time */
105 u_int64_t ri_rcv_expire
; /* receive expiration (calendar) time */
112 * These numbers are used by reliable protocols for determining
113 * retransmission behavior and are included in the routing structure.
116 u_int32_t rmx_locks
; /* Kernel must leave these values alone */
117 u_int32_t rmx_mtu
; /* MTU for this path */
118 u_int32_t rmx_hopcount
; /* max hops expected */
119 int32_t rmx_expire
; /* lifetime for route, e.g. redirect */
120 u_int32_t rmx_recvpipe
; /* inbound delay-bandwidth product */
121 u_int32_t rmx_sendpipe
; /* outbound delay-bandwidth product */
122 u_int32_t rmx_ssthresh
; /* outbound gateway buffer limit */
123 u_int32_t rmx_rtt
; /* estimated round trip time */
124 u_int32_t rmx_rttvar
; /* estimated rtt variance */
125 u_int32_t rmx_pksent
; /* packets sent using this route */
126 u_int32_t rmx_filler
[4]; /* will be used for T/TCP later */
130 * rmx_rtt and rmx_rttvar are stored as microseconds;
132 #define RTM_RTTUNIT 1000000 /* units for rtt, rttvar, as units per sec */
134 #ifdef KERNEL_PRIVATE
136 * New expiry value (in seconds) when dealing with interfaces which implement
137 * the if_want_aggressive_drain behavior. Otherwise the event mechanism wouldn't
138 * fire quick enough to cause any sort of significant gains in performance.
140 #define RT_IF_IDLE_EXPIRE_TIMEOUT 30
141 #define RT_IF_IDLE_DRAIN_INTERVAL 10
142 #endif /* KERNEL_PRIVATE */
145 * We distinguish between routes to hosts and routes to networks,
146 * preferring the former if available. For each route we infer
147 * the interface to use from the gateway address supplied when
148 * the route was entered. Routes that forward packets through
149 * gateways are marked so that the output routines know to address the
150 * gateway rather than the ultimate destination.
152 #ifdef KERNEL_PRIVATE
153 #include <kern/locks.h>
155 #include <net/radix.h>
158 * Kernel routing entry structure (private).
161 struct radix_node rt_nodes
[2]; /* tree glue, and other values */
162 #define rt_key(r) ((struct sockaddr *)((r)->rt_nodes->rn_key))
163 #define rt_mask(r) ((struct sockaddr *)((r)->rt_nodes->rn_mask))
164 struct sockaddr
*rt_gateway
; /* value */
165 int32_t rt_refcnt
; /* # held references */
166 uint32_t rt_flags
; /* up/down?, host/net */
167 struct ifnet
*rt_ifp
; /* the answer: interface to use */
168 struct ifaddr
*rt_ifa
; /* the answer: interface addr to use */
169 struct sockaddr
*rt_genmask
; /* for generation of cloned routes */
170 void *rt_llinfo
; /* pointer to link level info cache */
171 void (*rt_llinfo_get_ri
) /* llinfo get reachability info fn */
172 (struct rtentry
*, struct rt_reach_info
*);
173 void (*rt_llinfo_purge
)(struct rtentry
*); /* llinfo purge fn */
174 void (*rt_llinfo_free
)(void *); /* link level info free function */
175 struct rt_metrics rt_rmx
; /* metrics used by rx'ing protocols */
176 struct rtentry
*rt_gwroute
; /* implied entry for gatewayed routes */
177 struct rtentry
*rt_parent
; /* cloning parent of this route */
178 uint32_t generation_id
; /* route generation id */
180 * See bsd/net/route.c for synchronization notes.
182 decl_lck_mtx_data(, rt_lock
); /* lock for routing entry */
183 struct nstat_counts
*rt_stats
;
184 void (*rt_if_ref_fn
)(struct ifnet
*, int); /* interface ref func */
186 uint64_t rt_expire
; /* expiration time in uptime seconds */
187 uint64_t base_calendartime
; /* calendar time upon entry creation */
188 uint64_t base_uptime
;/* uptime upon entry creation */
191 extern void rt_setexpire(struct rtentry
*, uint64_t);
192 #endif /* KERNEL_PRIVATE */
194 #ifdef KERNEL_PRIVATE
195 #define rt_use rt_rmx.rmx_pksent
196 #endif /* KERNEL_PRIVATE */
198 #define RTF_UP 0x1 /* route usable */
199 #define RTF_GATEWAY 0x2 /* destination is a gateway */
200 #define RTF_HOST 0x4 /* host entry (net otherwise) */
201 #define RTF_REJECT 0x8 /* host or net unreachable */
202 #define RTF_DYNAMIC 0x10 /* created dynamically (by redirect) */
203 #define RTF_MODIFIED 0x20 /* modified dynamically (by redirect) */
204 #define RTF_DONE 0x40 /* message confirmed */
205 #define RTF_DELCLONE 0x80 /* delete cloned route */
206 #define RTF_CLONING 0x100 /* generate new routes on use */
207 #define RTF_XRESOLVE 0x200 /* external daemon resolves name */
208 #define RTF_LLINFO 0x400 /* generated by link layer (e.g. ARP) */
209 #define RTF_STATIC 0x800 /* manually added */
210 #define RTF_BLACKHOLE 0x1000 /* just discard pkts (during updates) */
211 #define RTF_PROTO2 0x4000 /* protocol specific routing flag */
212 #define RTF_PROTO1 0x8000 /* protocol specific routing flag */
214 #define RTF_PRCLONING 0x10000 /* protocol requires cloning */
215 #define RTF_WASCLONED 0x20000 /* route generated through cloning */
216 #define RTF_PROTO3 0x40000 /* protocol specific routing flag */
218 #define RTF_PINNED 0x100000 /* future use */
219 #define RTF_LOCAL 0x200000 /* route represents a local address */
220 #define RTF_BROADCAST 0x400000 /* route represents a bcast address */
221 #define RTF_MULTICAST 0x800000 /* route represents a mcast address */
222 #define RTF_IFSCOPE 0x1000000 /* has valid interface scope */
223 #define RTF_CONDEMNED 0x2000000 /* defunct; no longer modifiable */
224 #define RTF_IFREF 0x4000000 /* route holds a ref to interface */
225 /* 0x8000000 and up unassigned */
228 * Routing statistics.
231 short rts_badredirect
; /* bogus redirect calls */
232 short rts_dynamic
; /* routes created by redirects */
233 short rts_newgateway
; /* routes modified by redirects */
234 short rts_unreach
; /* lookups which failed */
235 short rts_wildcard
; /* lookups satisfied by a wildcard */
239 * Structures for routing messages.
242 u_short rtm_msglen
; /* to skip over non-understood messages */
243 u_char rtm_version
; /* future binary compatibility */
244 u_char rtm_type
; /* message type */
245 u_short rtm_index
; /* index for associated ifp */
246 int rtm_flags
; /* flags, incl. kern & message, e.g. DONE */
247 int rtm_addrs
; /* bitmask identifying sockaddrs in msg */
248 pid_t rtm_pid
; /* identify sender */
249 int rtm_seq
; /* for sender to identify action */
250 int rtm_errno
; /* why failed */
251 int rtm_use
; /* from rtentry */
252 u_int32_t rtm_inits
; /* which metrics we are initializing */
253 struct rt_metrics rtm_rmx
; /* metrics themselves */
257 u_short rtm_msglen
; /* to skip over non-understood messages */
258 u_char rtm_version
; /* future binary compatibility */
259 u_char rtm_type
; /* message type */
260 u_short rtm_index
; /* index for associated ifp */
261 int rtm_flags
; /* flags, incl. kern & message, e.g. DONE */
262 int rtm_addrs
; /* bitmask identifying sockaddrs in msg */
263 int32_t rtm_refcnt
; /* reference count */
264 int rtm_parentflags
; /* flags of the parent route */
265 int rtm_reserved
; /* reserved field set to 0 */
266 int rtm_use
; /* from rtentry */
267 u_int32_t rtm_inits
; /* which metrics we are initializing */
268 struct rt_metrics rtm_rmx
; /* metrics themselves */
273 * Extended routing message header (private).
275 struct rt_msghdr_ext
{
276 u_short rtm_msglen
; /* to skip over non-understood messages */
277 u_char rtm_version
; /* future binary compatibility */
278 u_char rtm_type
; /* message type */
279 u_int32_t rtm_index
; /* index for associated ifp */
280 u_int32_t rtm_flags
; /* flags, incl. kern & message, e.g. DONE */
281 u_int32_t rtm_reserved
; /* for future use */
282 u_int32_t rtm_addrs
; /* bitmask identifying sockaddrs in msg */
283 pid_t rtm_pid
; /* identify sender */
284 int rtm_seq
; /* for sender to identify action */
285 int rtm_errno
; /* why failed */
286 u_int32_t rtm_use
; /* from rtentry */
287 u_int32_t rtm_inits
; /* which metrics we are initializing */
288 struct rt_metrics rtm_rmx
; /* metrics themselves */
289 struct rt_reach_info rtm_ri
; /* route reachability info */
293 #define RTM_VERSION 5 /* Up the ante and ignore older versions */
298 #define RTM_ADD 0x1 /* Add Route */
299 #define RTM_DELETE 0x2 /* Delete Route */
300 #define RTM_CHANGE 0x3 /* Change Metrics or flags */
301 #define RTM_GET 0x4 /* Report Metrics */
302 #define RTM_LOSING 0x5 /* Kernel Suspects Partitioning */
303 #define RTM_REDIRECT 0x6 /* Told to use different route */
304 #define RTM_MISS 0x7 /* Lookup failed on this address */
305 #define RTM_LOCK 0x8 /* fix specified metrics */
306 #define RTM_OLDADD 0x9 /* caused by SIOCADDRT */
307 #define RTM_OLDDEL 0xa /* caused by SIOCDELRT */
308 #define RTM_RESOLVE 0xb /* req to resolve dst to LL addr */
309 #define RTM_NEWADDR 0xc /* address being added to iface */
310 #define RTM_DELADDR 0xd /* address being removed from iface */
311 #define RTM_IFINFO 0xe /* iface going up/down etc. */
312 #define RTM_NEWMADDR 0xf /* mcast group membership being added to if */
313 #define RTM_DELMADDR 0x10 /* mcast group membership being deleted */
315 #define RTM_GET_SILENT 0x11
317 #define RTM_IFINFO2 0x12 /* */
318 #define RTM_NEWMADDR2 0x13 /* */
319 #define RTM_GET2 0x14 /* */
321 #define RTM_GET_EXT 0x15
325 * Bitmask values for rtm_inits and rmx_locks.
327 #define RTV_MTU 0x1 /* init or lock _mtu */
328 #define RTV_HOPCOUNT 0x2 /* init or lock _hopcount */
329 #define RTV_EXPIRE 0x4 /* init or lock _expire */
330 #define RTV_RPIPE 0x8 /* init or lock _recvpipe */
331 #define RTV_SPIPE 0x10 /* init or lock _sendpipe */
332 #define RTV_SSTHRESH 0x20 /* init or lock _ssthresh */
333 #define RTV_RTT 0x40 /* init or lock _rtt */
334 #define RTV_RTTVAR 0x80 /* init or lock _rttvar */
337 * Bitmask values for rtm_addrs.
339 #define RTA_DST 0x1 /* destination sockaddr present */
340 #define RTA_GATEWAY 0x2 /* gateway sockaddr present */
341 #define RTA_NETMASK 0x4 /* netmask sockaddr present */
342 #define RTA_GENMASK 0x8 /* cloning mask sockaddr present */
343 #define RTA_IFP 0x10 /* interface name sockaddr present */
344 #define RTA_IFA 0x20 /* interface addr sockaddr present */
345 #define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */
346 #define RTA_BRD 0x80 /* for NEWADDR, broadcast or p-p dest addr */
349 * Index offsets for sockaddr array for alternate internal encoding.
351 #define RTAX_DST 0 /* destination sockaddr present */
352 #define RTAX_GATEWAY 1 /* gateway sockaddr present */
353 #define RTAX_NETMASK 2 /* netmask sockaddr present */
354 #define RTAX_GENMASK 3 /* cloning mask sockaddr present */
355 #define RTAX_IFP 4 /* interface name sockaddr present */
356 #define RTAX_IFA 5 /* interface addr sockaddr present */
357 #define RTAX_AUTHOR 6 /* sockaddr for author of redirect */
358 #define RTAX_BRD 7 /* for NEWADDR, broadcast or p-p dest addr */
359 #define RTAX_MAX 8 /* size of array to allocate */
363 struct sockaddr
*rti_info
[RTAX_MAX
];
377 * For scoped routing; a zero interface scope value means nil/no scope.
379 #define IFSCOPE_NONE 0
382 #ifdef KERNEL_PRIVATE
384 * Generic call trace used by some subsystems (e.g. route, ifaddr)
386 #define CTRACE_STACK_SIZE 8 /* depth of stack trace */
387 #define CTRACE_HIST_SIZE 4 /* refcnt history size */
388 typedef struct ctrace
{
389 void *th
; /* thread ptr */
390 void *pc
[CTRACE_STACK_SIZE
]; /* PC stack trace */
393 extern void ctrace_record(ctrace_t
*);
395 #define RT_LOCK_ASSERT_HELD(_rt) \
396 lck_mtx_assert(&(_rt)->rt_lock, LCK_MTX_ASSERT_OWNED)
398 #define RT_LOCK_ASSERT_NOTHELD(_rt) \
399 lck_mtx_assert(&(_rt)->rt_lock, LCK_MTX_ASSERT_NOTOWNED)
401 #define RT_LOCK(_rt) do { \
403 lck_mtx_lock(&(_rt)->rt_lock); \
405 rt_lock(_rt, FALSE); \
408 #define RT_LOCK_SPIN(_rt) do { \
410 lck_mtx_lock_spin(&(_rt)->rt_lock); \
412 rt_lock(_rt, TRUE); \
415 #define RT_CONVERT_LOCK(_rt) do { \
416 RT_LOCK_ASSERT_HELD(_rt); \
417 lck_mtx_convert_spin(&(_rt)->rt_lock); \
420 #define RT_UNLOCK(_rt) do { \
422 lck_mtx_unlock(&(_rt)->rt_lock); \
427 #define RT_ADDREF_LOCKED(_rt) do { \
429 RT_LOCK_ASSERT_HELD(_rt); \
430 if (++(_rt)->rt_refcnt == 0) \
431 panic("RT_ADDREF(%p) bad refcnt\n", _rt); \
438 * Spin variant mutex is used here; caller is responsible for
439 * converting any previously-held similar lock to full mutex.
441 #define RT_ADDREF(_rt) do { \
443 RT_ADDREF_LOCKED(_rt); \
447 #define RT_REMREF_LOCKED(_rt) do { \
449 RT_LOCK_ASSERT_HELD(_rt); \
450 if ((_rt)->rt_refcnt == 0) \
451 panic("RT_REMREF(%p) bad refcnt\n", _rt); \
452 --(_rt)->rt_refcnt; \
454 (void) rtunref(_rt); \
459 * Spin variant mutex is used here; caller is responsible for
460 * converting any previously-held similar lock to full mutex.
462 #define RT_REMREF(_rt) do { \
464 RT_REMREF_LOCKED(_rt); \
468 #define RTFREE(_rt) rtfree(_rt)
469 #define RTFREE_LOCKED(_rt) rtfree_locked(_rt)
471 extern struct route_cb route_cb
;
472 extern struct radix_node_head
*rt_tables
[AF_MAX
+1];
473 __private_extern__ lck_mtx_t
*rnh_lock
;
474 __private_extern__
int use_routegenid
;
475 __private_extern__
uint32_t route_generation
;
476 __private_extern__
int rttrash
;
477 __private_extern__
unsigned int rte_debug
;
482 extern void route_init(void) __attribute__((section("__TEXT, initcode")));
483 extern void routegenid_update(void);
484 extern void rt_ifmsg(struct ifnet
*);
485 extern void rt_missmsg(int, struct rt_addrinfo
*, int, int);
486 extern void rt_newaddrmsg(int, struct ifaddr
*, int, struct rtentry
*);
487 extern void rt_newmaddrmsg(int, struct ifmultiaddr
*);
488 extern int rt_setgate(struct rtentry
*, struct sockaddr
*, struct sockaddr
*);
489 extern void set_primary_ifscope(int, unsigned int);
490 extern unsigned int get_primary_ifscope(int);
491 extern boolean_t
rt_primary_default(struct rtentry
*, struct sockaddr
*);
492 extern struct rtentry
*rt_lookup(boolean_t
, struct sockaddr
*,
493 struct sockaddr
*, struct radix_node_head
*, unsigned int);
494 extern void rtalloc(struct route
*);
495 extern void rtalloc_scoped(struct route
*, unsigned int);
496 extern void rtalloc_ign(struct route
*, uint32_t);
497 extern void rtalloc_scoped_ign(struct route
*, uint32_t, unsigned int);
498 extern struct rtentry
*rtalloc1(struct sockaddr
*, int, uint32_t);
499 extern struct rtentry
*rtalloc1_scoped(struct sockaddr
*, int, uint32_t,
501 extern struct rtentry
*rtalloc1_scoped_locked(struct sockaddr
*, int,
502 uint32_t, unsigned int);
503 extern void rtfree(struct rtentry
*);
504 extern void rtfree_locked(struct rtentry
*);
505 extern void rtref(struct rtentry
*);
507 * rtunref will decrement the refcount, rtfree will decrement and free if
508 * the refcount has reached zero and the route is not up.
509 * Unless you have good reason to do otherwise, use rtfree.
511 extern int rtunref(struct rtentry
*);
512 extern void rtsetifa(struct rtentry
*, struct ifaddr
*);
513 extern int rtinit(struct ifaddr
*, int, int);
514 extern int rtinit_locked(struct ifaddr
*, int, int);
515 extern int rtioctl(unsigned long, caddr_t
, struct proc
*);
516 extern void rtredirect(struct ifnet
*, struct sockaddr
*, struct sockaddr
*,
517 struct sockaddr
*, int, struct sockaddr
*, struct rtentry
**);
518 extern int rtrequest(int, struct sockaddr
*,
519 struct sockaddr
*, struct sockaddr
*, int, struct rtentry
**);
520 extern int rtrequest_scoped(int, struct sockaddr
*, struct sockaddr
*,
521 struct sockaddr
*, int, struct rtentry
**, unsigned int);
522 extern int rtrequest_locked(int, struct sockaddr
*,
523 struct sockaddr
*, struct sockaddr
*, int, struct rtentry
**);
524 extern int rtrequest_scoped_locked(int, struct sockaddr
*, struct sockaddr
*,
525 struct sockaddr
*, int, struct rtentry
**, unsigned int);
526 extern void sin_set_ifscope(struct sockaddr
*, unsigned int);
527 extern unsigned int sin_get_ifscope(struct sockaddr
*);
528 extern unsigned int sin6_get_ifscope(struct sockaddr
*);
529 extern void rt_lock(struct rtentry
*, boolean_t
);
530 extern void rt_unlock(struct rtentry
*);
531 extern struct sockaddr
*rtm_scrub_ifscope(int, int, struct sockaddr
*,
532 struct sockaddr
*, struct sockaddr_storage
*);
533 extern u_int64_t
rt_expiry(struct rtentry
*, u_int64_t
, u_int32_t
);
534 extern void rt_set_idleref(struct rtentry
*);
535 extern void rt_clear_idleref(struct rtentry
*);
536 extern void rt_aggdrain(int);
537 extern boolean_t
rt_validate(struct rtentry
*);
539 #ifdef XNU_KERNEL_PRIVATE
540 extern void route_copyin(struct route
*src
, struct route
*dst
, size_t length
);
541 extern void route_copyout(struct route
*dst
, const struct route
*src
, size_t length
);
542 #endif /* XNU_KERNEL_PRIVATE */
544 #endif /* KERNEL_PRIVATE */