1/*
2 * Copyright (c) 2000-2022 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * Copyright (c) 1980, 1986, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 *
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 $
62 */
63
64#ifndef _NET_ROUTE_PRIVATE_H_
65#define _NET_ROUTE_PRIVATE_H_
66#include <net/route.h>
67#include <stdint.h>
68#include <sys/types.h>
69#include <sys/socket.h>
70#include <uuid/uuid.h>
71
72struct route_old {
73 void *ro_rt;
74 uint32_t ro_flags;
75 struct sockaddr ro_dst;
76};
77
78#ifdef BSD_KERNEL_PRIVATE
79#include <kern/locks.h>
80#include <net/radix.h>
81#include <sys/eventhandler.h>
82#include <net/if_dl.h>
83#include <netinet/in_private.h>
84
85#include <sys/constrained_ctypes.h>
86
87extern boolean_t trigger_v6_defrtr_select;
88/*
89 * Kernel resident routing tables.
90 *
91 * The routing tables are initialized when interface addresses
92 * are set by making entries for all directly connected interfaces.
93 */
94
95/* forward declarations */
96struct ifnet_llreach_info;
97struct rt_reach_info;
98
99/*
100 * IP route structure
101 *
102 * A route consists of a destination address and a reference
103 * to a routing entry. These are often held by protocols
104 * in their control blocks, e.g. inpcb.
105 */
106struct route {
107 /*
108 * N.B: struct route must begin with ro_{rt, lle, srcia, flags}
109 * because the code does some casts of a 'struct route_in6 *'
110 * to a 'struct route *'.
111 */
112 struct rtentry *ro_rt;
113 struct ifaddr *ro_srcia;
114 uint32_t ro_flags; /* route flags (see below) */
115 struct sockaddr ro_dst;
116};
117
118#define ROF_SRCIF_SELECTED 0x0001 /* source interface was selected */
119
120#define ROUTE_UNUSABLE(_ro) \
121 ((_ro)->ro_rt == NULL || \
122 ((_ro)->ro_rt->rt_flags & (RTF_UP|RTF_CONDEMNED)) != RTF_UP || \
123 RT_GENID_OUTOFSYNC((_ro)->ro_rt))
124
125#define _ROUTE_RELEASE_COMMON(_ro, _rnh_locked) do { \
126 if ((_ro)->ro_rt != NULL) { \
127 RT_LOCK_ASSERT_NOTHELD((_ro)->ro_rt); \
128 if (_rnh_locked) \
129 rtfree_locked((_ro)->ro_rt); \
130 else \
131 rtfree((_ro)->ro_rt); \
132 (_ro)->ro_rt = NULL; \
133 } \
134 if ((_ro)->ro_srcia != NULL) { \
135 ifa_remref((_ro)->ro_srcia); \
136 (_ro)->ro_srcia = NULL; \
137 (_ro)->ro_flags &= ~ROF_SRCIF_SELECTED; \
138 } \
139} while (0)
140
141#define ROUTE_RELEASE_LOCKED(_ro) _ROUTE_RELEASE_COMMON(_ro, TRUE)
142#define ROUTE_RELEASE(_ro) _ROUTE_RELEASE_COMMON(_ro, FALSE)
143
144/*
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.
151 */
152
153#define NRTT_HIST 10
154/*
155 * Kernel routing entry structure.
156 */
157struct rtentry {
158 struct radix_node rt_nodes[2]; /* tree glue, and other values */
159#define rt_key(r) (SA(rn_get_key(&((r)->rt_nodes[0]))))
160#define rt_mask(r) (SA(rn_get_mask(&((r)->rt_nodes[0]))))
161 /*
162 * See bsd/net/route.c for synchronization notes.
163 */
164 decl_lck_mtx_data(, rt_lock); /* lock for routing entry */
165 uint32_t rt_refcnt; /* # held references */
166 uint32_t rt_flags; /* up/down?, host/net */
167 uint32_t rt_genid; /* route generation id */
168 struct sockaddr *rt_gateway; /* value */
169 struct ifnet *rt_ifp; /* the answer: interface to use */
170 struct ifaddr *rt_ifa; /* the answer: interface addr to use */
171 struct sockaddr *rt_genmask; /* for generation of cloned routes */
172 void *rt_llinfo; /* pointer to link level info cache */
173 void (*rt_llinfo_get_ri) /* llinfo get reachability info fn */
174 (struct rtentry *, struct rt_reach_info *);
175 void (*rt_llinfo_get_iflri) /* ifnet llinfo get reach. info fn */
176 (struct rtentry *, struct ifnet_llreach_info *);
177 void (*rt_llinfo_purge)(struct rtentry *); /* llinfo purge fn */
178 void (*rt_llinfo_free)(void *); /* link level info free function */
179 void (*rt_llinfo_refresh) (struct rtentry *); /* expedite llinfo refresh */
180 struct rt_metrics rt_rmx; /* metrics used by rx'ing protocols */
181#define rt_use rt_rmx.rmx_pksent
182 struct rtentry *rt_gwroute; /* implied entry for gatewayed routes */
183 struct rtentry *rt_parent; /* cloning parent of this route */
184 struct nstat_counts *rt_stats; /* route stats */
185 void (*rt_if_ref_fn)(struct ifnet *, int); /* interface ref func */
186
187 uint32_t *rt_tree_genid; /* ptr to per-tree route_genid */
188 uint64_t rt_expire; /* expiration time in uptime seconds */
189 uint64_t base_calendartime; /* calendar time upon entry creation */
190 uint64_t base_uptime; /* uptime upon entry creation */
191 u_int32_t rtt_hist[NRTT_HIST]; /* RTT history sample by TCP connections */
192 u_int32_t rtt_min; /* minimum RTT computed from history */
193 u_int32_t rtt_expire_ts; /* RTT history expire timestamp */
194 u_int8_t rtt_index; /* Index into RTT history */
195 /* Event handler context for the rtentrt */
196 struct eventhandler_lists_ctxt rt_evhdlr_ctxt;
197};
198
199__CCT_DECLARE_CONSTRAINED_PTR_TYPES(struct rtentry, rtentry);
200
201#define rt_key_free(r) ({ \
202 void *__r __single = rt_key(r); \
203 kheap_free_addr(KHEAP_DATA_BUFFERS, __r); \
204})
205
206enum {
207 ROUTE_STATUS_UPDATE = 1,
208 ROUTE_ENTRY_REFRESH,
209 ROUTE_ENTRY_DELETED,
210 ROUTE_LLENTRY_RESOLVED,
211 ROUTE_LLENTRY_UNREACH,
212 ROUTE_LLENTRY_CHANGED,
213 ROUTE_LLENTRY_STALE,
214 ROUTE_LLENTRY_TIMEDOUT,
215 ROUTE_LLENTRY_DELETED,
216 ROUTE_LLENTRY_EXPIRED,
217 ROUTE_LLENTRY_PROBED,
218 ROUTE_EVHDLR_DEREGISTER,
219};
220
221extern const char * route_event2str(int route_event);
222
223typedef void (*route_event_fn) (struct eventhandler_entry_arg,
224 struct sockaddr *, int, struct sockaddr *, int);
225EVENTHANDLER_DECLARE(route_event, route_event_fn);
226
227/*
228 * Synchronize route entry's generation ID with the tree's.
229 */
230#define RT_GENID_SYNC(_rt) do { \
231 if ((_rt)->rt_tree_genid != NULL) \
232 (_rt)->rt_genid = *(_rt)->rt_tree_genid; \
233} while (0)
234
235/*
236 * Indicates whether or not the route entry's generation ID is stale.
237 */
238#define RT_GENID_OUTOFSYNC(_rt) \
239 ((_rt)->rt_tree_genid != NULL && \
240 *(_rt)->rt_tree_genid != (_rt)->rt_genid)
241
242enum {
243 ROUTE_OP_READ,
244 ROUTE_OP_WRITE,
245};
246
247extern int route_op_entitlement_check(struct socket *, kauth_cred_t, int, boolean_t);
248#endif /* BSD_KERNEL_PRIVATE */
249
250struct kev_netevent_apnfallbk_data {
251 pid_t epid; /* effective PID */
252 uuid_t euuid; /* effective UUID */
253};
254
255/*
256 * Route reachability info.
257 */
258struct rt_reach_info {
259 u_int32_t ri_refcnt; /* reference count */
260 u_int32_t ri_probes; /* total # of probes */
261 u_int64_t ri_snd_expire; /* tx expiration (calendar) time */
262 u_int64_t ri_rcv_expire; /* rx expiration (calendar) time */
263 int32_t ri_rssi; /* received signal strength */
264 int32_t ri_lqm; /* link quality metric */
265 int32_t ri_npm; /* node proximity metric */
266};
267
268/*
269 * Extended routing message header (private).
270 */
271struct rt_msghdr_ext {
272 u_short rtm_msglen; /* to skip over non-understood messages */
273 u_char rtm_version; /* future binary compatibility */
274 u_char rtm_type; /* message type */
275 u_int32_t rtm_index; /* index for associated ifp */
276 u_int32_t rtm_flags; /* flags, incl. kern & message, e.g. DONE */
277 u_int32_t rtm_reserved; /* for future use */
278 u_int32_t rtm_addrs; /* bitmask identifying sockaddrs in msg */
279 pid_t rtm_pid; /* identify sender */
280 int rtm_seq; /* for sender to identify action */
281 int rtm_errno; /* why failed */
282 u_int32_t rtm_use; /* from rtentry */
283 u_int32_t rtm_inits; /* which metrics we are initializing */
284 struct rt_metrics rtm_rmx; /* metrics themselves */
285 struct rt_reach_info rtm_ri; /* route reachability info */
286};
287
288/*
289 * Message types.
290 */
291#define RTM_GET_SILENT 0x11
292#define RTM_GET_EXT 0x15
293
294/*
295 * Bitmask values for rtm_inits and rmx_locks.
296 */
297#define RTV_REFRESH_HOST 0x100 /* init host route to expedite refresh */
298
299/*
300 * For scoped routing; a zero interface scope value means nil/no scope.
301 */
302#define IFSCOPE_NONE 0
303#define IFSCOPE_UNKNOWN IFSCOPE_NONE
304
305#ifdef BSD_KERNEL_PRIVATE
306/*
307 * Generic call trace used by some subsystems (e.g. route, ifaddr)
308 */
309#define CTRACE_STACK_SIZE 8 /* depth of stack trace */
310#define CTRACE_HIST_SIZE 4 /* refcnt history size */
311typedef struct ctrace {
312 void *th; /* thread ptr */
313 void *pc[CTRACE_STACK_SIZE]; /* PC stack trace */
314} ctrace_t;
315
316extern void ctrace_record(ctrace_t *);
317
318#define RT_LOCK_ASSERT_HELD(_rt) \
319 LCK_MTX_ASSERT(&(_rt)->rt_lock, LCK_MTX_ASSERT_OWNED)
320
321#define RT_LOCK_ASSERT_NOTHELD(_rt) \
322 LCK_MTX_ASSERT(&(_rt)->rt_lock, LCK_MTX_ASSERT_NOTOWNED)
323
324#define RT_LOCK(_rt) do { \
325 rt_lock(_rt, FALSE); \
326} while (0)
327
328#define RT_LOCK_SPIN(_rt) do { \
329 rt_lock(_rt, TRUE); \
330} while (0)
331
332#define RT_CONVERT_LOCK(_rt) do { \
333 RT_LOCK_ASSERT_HELD(_rt); \
334 lck_mtx_convert_spin(&(_rt)->rt_lock); \
335} while (0)
336
337#define RT_UNLOCK(_rt) do { \
338 rt_unlock(_rt); \
339} while (0)
340
341#define RT_ADDREF_LOCKED(_rt) do { \
342 rtref(_rt); \
343} while (0)
344
345/*
346 * Spin variant mutex is used here; caller is responsible for
347 * converting any previously-held similar lock to full mutex.
348 */
349#define RT_ADDREF(_rt) do { \
350 RT_LOCK_SPIN(_rt); \
351 RT_ADDREF_LOCKED(_rt); \
352 RT_UNLOCK(_rt); \
353} while (0)
354
355#define RT_REMREF_LOCKED(_rt) do { \
356 (void) rtunref(_rt); \
357} while (0)
358
359/*
360 * Spin variant mutex is used here; caller is responsible for
361 * converting any previously-held similar lock to full mutex.
362 */
363#define RT_REMREF(_rt) do { \
364 RT_LOCK_SPIN(_rt); \
365 RT_REMREF_LOCKED(_rt); \
366 RT_UNLOCK(_rt); \
367} while (0)
368
369/*
370 * This macro calculates skew in wall clock, just in case the user changes the
371 * system time. This skew adjustment is required because we now keep the
372 * expiration times in uptime terms in the kernel, but the userland still
373 * expects expiration times in terms of calendar times. This is used when
374 * reporting rt_expire, ln_expire, etc. values to user space.
375 */
376#define NET_CALCULATE_CLOCKSKEW(cc, ic, cu, iu) \
377 ((cc.tv_sec - ic) - (cu - iu))
378
379extern unsigned int rt_verbose;
380extern struct radix_node_head *rt_tables[AF_MAX + 1];
381extern lck_mtx_t rnh_lock_data;
382#define rnh_lock (&rnh_lock_data)
383extern uint32_t route_genid_inet; /* INET route generation count */
384extern uint32_t route_genid_inet6; /* INET6 route generation count */
385extern int rttrash;
386extern unsigned int rte_debug;
387
388struct ifmultiaddr;
389struct proc;
390
391extern void route_init(void);
392extern void routegenid_update(void);
393extern void routegenid_inet_update(void);
394extern void routegenid_inet6_update(void);
395extern void rt_ifmsg(struct ifnet *);
396extern void rt_missmsg(u_char, struct rt_addrinfo *, int, int);
397extern void rt_newaddrmsg(u_char, struct ifaddr *, int, struct rtentry *);
398extern void rt_newmaddrmsg(u_char, struct ifmultiaddr *);
399extern int rt_setgate(struct rtentry *, struct sockaddr *, struct sockaddr *);
400extern void set_primary_ifscope(int, unsigned int);
401extern unsigned int get_primary_ifscope(int);
402extern boolean_t rt_primary_default(struct rtentry *, struct sockaddr *);
403extern struct rtentry *rt_lookup(boolean_t, struct sockaddr *,
404 struct sockaddr *, struct radix_node_head *, unsigned int);
405extern struct rtentry *rt_lookup_coarse(boolean_t, struct sockaddr *,
406 struct sockaddr *, struct radix_node_head *);
407extern void rtalloc(struct route *);
408extern void rtalloc_scoped(struct route *, unsigned int);
409extern void rtalloc_ign(struct route *, uint32_t);
410extern void rtalloc_scoped_ign(struct route *, uint32_t, unsigned int);
411extern struct rtentry *rtalloc1(struct sockaddr *, int, uint32_t);
412extern struct rtentry *rtalloc1_scoped(struct sockaddr *, int, uint32_t,
413 unsigned int);
414extern struct rtentry *rtalloc1_scoped_locked(struct sockaddr *, int,
415 uint32_t, unsigned int);
416extern void rtfree_locked(struct rtentry *);
417extern void rtfree(struct rtentry *);
418extern void rtref(struct rtentry *);
419/*
420 * rtunref will decrement the refcount, rtfree will decrement and free if
421 * the refcount has reached zero and the route is not up.
422 * Unless you have good reason to do otherwise, use rtfree.
423 */
424extern int rtunref(struct rtentry *);
425extern void rtsetifa(struct rtentry *, struct ifaddr *);
426extern int rtinit(struct ifaddr *, uint8_t, int);
427extern int rtinit_locked(struct ifaddr *, uint8_t, int);
428extern int rtioctl(unsigned long, caddr_t, struct proc *);
429extern void rtredirect(struct ifnet *, struct sockaddr *, struct sockaddr *,
430 struct sockaddr *, int, struct sockaddr *, struct rtentry **);
431extern int rtrequest(int, struct sockaddr *,
432 struct sockaddr *, struct sockaddr *, int, struct rtentry **);
433extern int rtrequest_scoped(int, struct sockaddr *, struct sockaddr *,
434 struct sockaddr *, int, struct rtentry **, unsigned int);
435extern int rtrequest_locked(int, struct sockaddr *,
436 struct sockaddr *, struct sockaddr *, int, struct rtentry **);
437extern int rtrequest_scoped_locked(int, struct sockaddr *, struct sockaddr *,
438 struct sockaddr *, int, struct rtentry **, unsigned int);
439extern void sin_set_ifscope(struct sockaddr *, unsigned int);
440extern unsigned int sin_get_ifscope(struct sockaddr *);
441extern unsigned int sin6_get_ifscope(struct sockaddr *);
442extern void rt_lock(struct rtentry *, boolean_t);
443extern void rt_unlock(struct rtentry *);
444extern struct sockaddr *rtm_scrub(int, int, struct sockaddr *,
445 struct sockaddr *, void *buf __sized_by(buflen), uint32_t buflen, kauth_cred_t *);
446extern boolean_t rt_validate(struct rtentry *);
447extern void rt_set_proxy(struct rtentry *, boolean_t);
448extern void rt_set_gwroute(struct rtentry *, struct sockaddr *,
449 struct rtentry *);
450extern void rt_revalidate_gwroute(struct rtentry *, struct rtentry *);
451extern errno_t route_to_gwroute(const struct sockaddr *, struct rtentry *,
452 struct rtentry **);
453extern void rt_setexpire(struct rtentry *, uint64_t);
454extern void rt_str(struct rtentry *, char *, uint32_t, char *, uint32_t);
455extern const char *rtm2str(int);
456extern void route_clear(struct route *);
457extern void route_copyin(struct route *, struct route *, size_t);
458extern void route_copyout(struct route *, const struct route *, size_t);
459extern boolean_t rt_ifa_is_dst(struct sockaddr *, struct ifaddr *);
460extern struct sockaddr *sa_copy(struct sockaddr *, struct sockaddr_storage *,
461 unsigned int *);
462
463/*
464 * The following is used to enqueue work items for route events
465 * and also used to pass route event while walking the tree
466 */
467struct route_event {
468 struct rtentry *rt;
469 /*
470 * There's no reference taken on gwrt.
471 * We only use it to check whether we should
472 * point to rt_gateway or the embedded rt_addr
473 * structure.
474 */
475 struct rtentry *gwrt;
476 union {
477 union sockaddr_in_4_6 _rtev_ipaddr;
478 char _rtev_addr_bytes[DLIL_SDLMAXLEN];
479 } rt_addr;
480 uint32_t route_event_code;
481 eventhandler_tag evtag;
482};
483
484#define rtev_ipaddr rt_addr._rtev_ipaddr
485#define rtev_addr_bytes rt_addr._rtev_addr_bytes
486
487extern void route_event_init(struct route_event *p_route_ev, struct rtentry *rt,
488 struct rtentry *gwrt, int route_ev_code);
489extern int route_event_walktree(struct radix_node *rn, void *arg);
490extern void route_event_enqueue_nwk_wq_entry(struct rtentry *, struct rtentry *,
491 uint32_t, eventhandler_tag, boolean_t);
492#endif /* BSD_KERNEL_PRIVATE */
493#endif /* _NET_ROUTE_PRIVATE_H_ */
494