1/* $OpenBSD: route.h,v 1.218 2025/07/14 08:48:51 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/*
39 * Locks used to protect struct members in this file:
40 * I immutable after creation
41 * N net lock
42 * X exclusive net lock, or shared net lock + kernel lock
43 * R rtable lock
44 * r per route entry mutex rt_mtx
45 * L arp/nd6/etc lock for updates, net lock for reads
46 * T rttimer_mtx route timer lists
47 */
48
49/*
50 * Kernel resident routing tables.
51 *
52 * The routing tables are initialized when interface addresses
53 * are set by making entries for all directly connected interfaces.
54 */
55
56#ifdef _KERNEL
57/*
58 * These numbers are used by reliable protocols for determining
59 * retransmission behavior and are included in the routing structure.
60 */
61struct rt_kmetrics {
62 u_int64_t rmx_pksent; /* packets sent using this route */
63 int64_t rmx_expire; /* lifetime for route, e.g. redirect */
64 u_int rmx_locks; /* Kernel must leave these values */
65 u_int rmx_mtu; /* [a] MTU for this path */
66};
67#endif
68
69/*
70 * Huge version for userland compatibility.
71 */
72struct rt_metrics {
73 u_int64_t rmx_pksent; /* packets sent using this route */
74 int64_t rmx_expire; /* lifetime for route, e.g. redirect */
75 u_int rmx_locks; /* Kernel must leave these values */
76 u_int rmx_mtu; /* MTU for this path */
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 u_int rmx_pad;
86};
87
88#ifdef _KERNEL
89/*
90 * rmx_rtt and rmx_rttvar are stored as microseconds;
91 * RTTTOPRHZ(rtt) converts to a value suitable for use
92 * by a protocol slowtimo counter.
93 */
94#define RTM_RTTUNIT 1000000 /* units for rtt, rttvar, as units per sec */
95#define RTTTOPRHZ(r) ((r) / (RTM_RTTUNIT / PR_SLOWHZ))
96
97#include <sys/mutex.h>
98#include <sys/queue.h>
99#include <net/rtable.h>
100
101struct rttimer;
102
103/*
104 * We distinguish between routes to hosts and routes to networks,
105 * preferring the former if available. For each route we infer
106 * the interface to use from the gateway address supplied when
107 * the route was entered. Routes that forward packets through
108 * gateways are marked with RTF_GATEWAY so that the output routines
109 * know to address the gateway rather than the ultimate destination.
110 *
111 * How the RT_gw union is used also depends on RTF_GATEWAY. With
112 * RTF_GATEWAY set, rt_gwroute points at the rtentry for the rt_gateway
113 * address. If RTF_GATEWAY is not set, rt_cachecnt contains the
114 * number of RTF_GATEWAY rtentry structs with their rt_gwroute pointing
115 * at this rtentry.
116 */
117
118struct rtentry {
119 struct sockaddr *rt_dest; /* [I] destination */
120 struct rtentry *rt_next; /* [R] next mpath entry to our dst */
121 struct sockaddr *rt_gateway; /* [X] gateway address */
122 struct ifaddr *rt_ifa; /* [N] interface addr to use */
123 caddr_t rt_llinfo; /* [L] pointer to link level info or
124 an MPLS structure */
125 struct rtentry *rt_gwroute; /* [X] rtentry for rt_gateway */
126 struct rtentry *rt_parent; /* [N] if cloned, parent rtentry */
127 LIST_HEAD(, rttimer) rt_timer; /* queue of timeouts for misc funcs */
128 struct mutex rt_mtx; /* lock members of this struct */
129 struct refcnt rt_refcnt; /* # held references */
130 struct rt_kmetrics rt_rmx; /* metrics used by rx'ing protocols */
131 unsigned int rt_cachecnt; /* [r] # gateway rtentry refs */
132 unsigned int rt_ifidx; /* [N] interface to use */
133 unsigned int rt_flags; /* [X] up/down?, host/net */
134 int rt_plen; /* [I] prefix length */
135 uint16_t rt_labelid; /* [N] route label ID */
136 uint8_t rt_priority; /* [N] routing priority to use */
137};
138#define rt_use rt_rmx.rmx_pksent
139#define rt_expire rt_rmx.rmx_expire
140#define rt_locks rt_rmx.rmx_locks
141#define rt_mtu rt_rmx.rmx_mtu
142
143#endif /* _KERNEL */
144
145/* bitmask values for rtm_flags */
146#define RTF_UP 0x1 /* route usable */
147#define RTF_GATEWAY 0x2 /* destination is a gateway */
148#define RTF_HOST 0x4 /* host entry (net otherwise) */
149#define RTF_REJECT 0x8 /* host or net unreachable */
150#define RTF_DYNAMIC 0x10 /* created dynamically (by redirect) */
151#define RTF_MODIFIED 0x20 /* modified dynamically (by redirect) */
152#define RTF_DONE 0x40 /* message confirmed */
153#define RTF_CLONING 0x100 /* generate new routes on use */
154#define RTF_MULTICAST 0x200 /* route associated to a mcast addr. */
155#define RTF_LLINFO 0x400 /* generated by ARP or ND */
156#define RTF_STATIC 0x800 /* manually added */
157#define RTF_BLACKHOLE 0x1000 /* just discard pkts (during updates) */
158#define RTF_PROTO3 0x2000 /* protocol specific routing flag */
159#define RTF_PROTO2 0x4000 /* protocol specific routing flag */
160#define RTF_ANNOUNCE RTF_PROTO2 /* announce L2 entry */
161#define RTF_PROTO1 0x8000 /* protocol specific routing flag */
162#define RTF_CLONED 0x10000 /* this is a cloned route */
163#define RTF_CACHED 0x20000 /* cached by a RTF_GATEWAY entry */
164#define RTF_MPATH 0x40000 /* multipath route or operation */
165#define RTF_MPLS 0x100000 /* MPLS additional infos */
166#define RTF_LOCAL 0x200000 /* route to a local address */
167#define RTF_BROADCAST 0x400000 /* route associated to a bcast addr. */
168#define RTF_CONNECTED 0x800000 /* interface route */
169#define RTF_BFD 0x1000000 /* Link state controlled by BFD */
170
171/* mask of RTF flags that are allowed to be modified by RTM_CHANGE */
172#define RTF_FMASK \
173 (RTF_LLINFO | RTF_PROTO1 | RTF_PROTO2 | RTF_PROTO3 | RTF_BLACKHOLE | \
174 RTF_REJECT | RTF_STATIC | RTF_MPLS | RTF_BFD)
175
176/* Routing priorities used by the different routing protocols */
177#define RTP_NONE 0 /* unset priority use sane default */
178#define RTP_LOCAL 1 /* local address routes (must be the highest) */
179#define RTP_CONNECTED 4 /* directly connected routes */
180#define RTP_STATIC 8 /* static routes base priority */
181#define RTP_EIGRP 28 /* EIGRP routes */
182#define RTP_OSPF 32 /* OSPF routes */
183#define RTP_ISIS 36 /* IS-IS routes */
184#define RTP_RIP 40 /* RIP routes */
185#define RTP_BGP 48 /* BGP routes */
186#define RTP_DEFAULT 56 /* routes that have nothing set */
187#define RTP_PROPOSAL_STATIC 57
188#define RTP_PROPOSAL_DHCLIENT 58
189#define RTP_PROPOSAL_SLAAC 59
190#define RTP_PROPOSAL_UMB 60
191#define RTP_PROPOSAL_PPP 61
192#define RTP_PROPOSAL_SOLICIT 62 /* request reply of all RTM_PROPOSAL */
193#define RTP_MAX 63 /* maximum priority */
194#define RTP_ANY 64 /* any of the above */
195#define RTP_MASK 0x7f
196#define RTP_DOWN 0x80 /* route/link is down */
197
198/*
199 * Routing statistics.
200 */
201struct rtstat {
202 u_int32_t rts_badredirect; /* bogus redirect calls */
203 u_int32_t rts_dynamic; /* routes created by redirects */
204 u_int32_t rts_newgateway; /* routes modified by redirects */
205 u_int32_t rts_unreach; /* lookups which failed */
206 u_int32_t rts_wildcard; /* lookups satisfied by a wildcard */
207};
208
209/*
210 * Routing Table Info.
211 */
212struct rt_tableinfo {
213 u_short rti_tableid; /* routing table id */
214 u_short rti_domainid; /* routing domain id */
215};
216
217/*
218 * Structures for routing messages.
219 */
220struct rt_msghdr {
221 u_short rtm_msglen; /* to skip over non-understood messages */
222 u_char rtm_version; /* future binary compatibility */
223 u_char rtm_type; /* message type */
224 u_short rtm_hdrlen; /* sizeof(rt_msghdr) to skip over the header */
225 u_short rtm_index; /* index for associated ifp */
226 u_short rtm_tableid; /* routing table id */
227 u_char rtm_priority; /* routing priority */
228 u_char rtm_mpls; /* MPLS additional infos */
229 int rtm_addrs; /* bitmask identifying sockaddrs in msg */
230 int rtm_flags; /* flags, incl. kern & message, e.g. DONE */
231 int rtm_fmask; /* bitmask used in RTM_CHANGE message */
232 pid_t rtm_pid; /* identify sender */
233 int rtm_seq; /* for sender to identify action */
234 int rtm_errno; /* why failed */
235 u_int rtm_inits; /* which metrics we are initializing */
236 struct rt_metrics rtm_rmx; /* metrics themselves */
237};
238/* overload no longer used field */
239#define rtm_use rtm_rmx.rmx_pksent
240
241#define RTM_VERSION 5 /* Up the ante and ignore older versions */
242
243#define RTM_MAXSIZE 2048 /* Maximum size of an accepted route msg */
244
245/* values for rtm_type */
246#define RTM_ADD 0x1 /* Add Route */
247#define RTM_DELETE 0x2 /* Delete Route */
248#define RTM_CHANGE 0x3 /* Change Metrics or flags */
249#define RTM_GET 0x4 /* Report Metrics */
250#define RTM_LOSING 0x5 /* Kernel Suspects Partitioning */
251#define RTM_REDIRECT 0x6 /* Told to use different route */
252#define RTM_MISS 0x7 /* Lookup failed on this address */
253#define RTM_RESOLVE 0xb /* req to resolve dst to LL addr */
254#define RTM_NEWADDR 0xc /* address being added to iface */
255#define RTM_DELADDR 0xd /* address being removed from iface */
256#define RTM_IFINFO 0xe /* iface going up/down etc. */
257#define RTM_IFANNOUNCE 0xf /* iface arrival/departure */
258#define RTM_DESYNC 0x10 /* route socket buffer overflow */
259#define RTM_INVALIDATE 0x11 /* Invalidate cache of L2 route */
260#define RTM_BFD 0x12 /* bidirectional forwarding detection */
261#define RTM_PROPOSAL 0x13 /* proposal for resolvd(8) */
262#define RTM_CHGADDRATTR 0x14 /* address attribute change */
263#define RTM_80211INFO 0x15 /* 80211 iface change */
264#define RTM_SOURCE 0x16 /* set source address */
265
266#define RTV_MTU 0x1 /* init or lock _mtu */
267#define RTV_HOPCOUNT 0x2 /* init or lock _hopcount */
268#define RTV_EXPIRE 0x4 /* init or lock _expire */
269#define RTV_RPIPE 0x8 /* init or lock _recvpipe */
270#define RTV_SPIPE 0x10 /* init or lock _sendpipe */
271#define RTV_SSTHRESH 0x20 /* init or lock _ssthresh */
272#define RTV_RTT 0x40 /* init or lock _rtt */
273#define RTV_RTTVAR 0x80 /* init or lock _rttvar */
274
275/*
276 * Bitmask values for rtm_addrs.
277 */
278#define RTA_DST 0x1 /* destination sockaddr present */
279#define RTA_GATEWAY 0x2 /* gateway sockaddr present */
280#define RTA_NETMASK 0x4 /* netmask sockaddr present */
281#define RTA_GENMASK 0x8 /* cloning mask sockaddr present */
282#define RTA_IFP 0x10 /* interface name sockaddr present */
283#define RTA_IFA 0x20 /* interface addr sockaddr present */
284#define RTA_AUTHOR 0x40 /* sockaddr for author of redirect */
285#define RTA_BRD 0x80 /* for NEWADDR, broadcast or p-p dest addr */
286#define RTA_SRC 0x100 /* source sockaddr present */
287#define RTA_SRCMASK 0x200 /* source netmask present */
288#define RTA_LABEL 0x400 /* route label present */
289#define RTA_BFD 0x800 /* bfd present */
290#define RTA_DNS 0x1000 /* DNS Servers sockaddr present */
291#define RTA_STATIC 0x2000 /* RFC 3442 encoded static routes present */
292#define RTA_SEARCH 0x4000 /* RFC 3397 encoded search path present */
293
294/*
295 * Index offsets for sockaddr array for alternate internal encoding.
296 */
297#define RTAX_DST 0 /* destination sockaddr present */
298#define RTAX_GATEWAY 1 /* gateway sockaddr present */
299#define RTAX_NETMASK 2 /* netmask sockaddr present */
300#define RTAX_GENMASK 3 /* cloning mask sockaddr present */
301#define RTAX_IFP 4 /* interface name sockaddr present */
302#define RTAX_IFA 5 /* interface addr sockaddr present */
303#define RTAX_AUTHOR 6 /* sockaddr for author of redirect */
304#define RTAX_BRD 7 /* for NEWADDR, broadcast or p-p dest addr */
305#define RTAX_SRC 8 /* source sockaddr present */
306#define RTAX_SRCMASK 9 /* source netmask present */
307#define RTAX_LABEL 10 /* route label present */
308#define RTAX_BFD 11 /* bfd present */
309#define RTAX_DNS 12 /* DNS Server(s) sockaddr present */
310#define RTAX_STATIC 13 /* RFC 3442 encoded static routes present */
311#define RTAX_SEARCH 14 /* RFC 3397 encoded search path present */
312#define RTAX_MAX 15 /* size of array to allocate */
313
314/*
315 * setsockopt defines used for the filtering.
316 */
317#define ROUTE_MSGFILTER 1 /* bitmask to specify which types should be
318 sent to the client. */
319#define ROUTE_TABLEFILTER 2 /* change routing table the socket is listening
320 on, RTABLE_ANY listens on all tables. */
321#define ROUTE_PRIOFILTER 3 /* only pass updates with a priority higher or
322 equal (actual value lower) to the specified
323 priority. */
324#define ROUTE_FLAGFILTER 4 /* do not pass updates for routes with flags
325 in this bitmask. */
326
327#define ROUTE_FILTER(m) (1 << (m))
328#define RTABLE_ANY 0xffffffff
329
330#define RTLABEL_LEN 32
331
332struct sockaddr_rtlabel {
333 u_int8_t sr_len; /* total length */
334 sa_family_t sr_family; /* address family */
335 char sr_label[RTLABEL_LEN];
336};
337
338#define RTDNS_LEN 128
339
340struct sockaddr_rtdns {
341 u_int8_t sr_len; /* total length */
342 sa_family_t sr_family; /* address family */
343 char sr_dns[RTDNS_LEN];
344};
345
346#ifdef _KERNEL
347
348static inline struct sockaddr *
349srtdnstosa(struct sockaddr_rtdns *sdns)
350{
351 return ((struct sockaddr *)(sdns));
352}
353
354#endif
355
356#define RTSTATIC_LEN 128
357
358struct sockaddr_rtstatic {
359 u_int8_t sr_len; /* total length */
360 sa_family_t sr_family; /* address family */
361 char sr_static[RTSTATIC_LEN];
362};
363
364#define RTSEARCH_LEN 128
365
366struct sockaddr_rtsearch {
367 u_int8_t sr_len; /* total length */
368 sa_family_t sr_family; /* address family */
369 char sr_search[RTSEARCH_LEN];
370};
371
372struct rt_addrinfo {
373 int rti_addrs;
374 const struct sockaddr *rti_info[RTAX_MAX];
375 int rti_flags;
376 struct ifaddr *rti_ifa;
377 struct rt_msghdr *rti_rtm;
378 u_char rti_mpls;
379};
380
381#ifdef __BSD_VISIBLE
382
383#include <netinet/in.h>
384
385/*
386 * A route consists of a destination address and a reference
387 * to a routing entry. These are often held by protocols
388 * in their control blocks, e.g. inpcb.
389 */
390struct route {
391 struct rtentry *ro_rt;
392 u_long ro_generation;
393 u_long ro_tableid; /* u_long because of alignment */
394 union {
395 struct sockaddr ro_dstsa;
396 struct sockaddr_in ro_dstsin;
397 struct sockaddr_in6 ro_dstsin6;
398 };
399 union {
400 struct in_addr ro_srcin;
401 struct in6_addr ro_srcin6;
402 };
403};
404
405#endif /* __BSD_VISIBLE */
406
407#ifdef _KERNEL
408
409#include <sys/percpu.h>
410
411enum rtstat_counters {
412 rts_badredirect, /* bogus redirect calls */
413 rts_dynamic, /* routes created by redirects */
414 rts_newgateway, /* routes modified by redirects */
415 rts_unreach, /* lookups which failed */
416 rts_wildcard, /* lookups satisfied by a wildcard */
417
418 rts_ncounters
419};
420
421static inline void
422rtstat_inc(enum rtstat_counters c)
423{
424 extern struct cpumem *rtcounters;
425
426 counters_inc(rtcounters, c);
427}
428
429/*
430 * This structure, and the prototypes for the rt_timer_{init,remove_all,
431 * add,timer} functions all used with the kind permission of BSDI.
432 * These allow functions to be called for routes at specific times.
433 */
434struct rttimer_queue {
435 TAILQ_HEAD(, rttimer) rtq_head; /* [T] */
436 LIST_ENTRY(rttimer_queue) rtq_link; /* [T] */
437 void (*rtq_func) /* [I] callback */
438 (struct rtentry *, u_int);
439 unsigned long rtq_count; /* [T] */
440 int rtq_timeout; /* [T] */
441};
442
443const char *rtlabel_id2name_locked(u_int16_t);
444const char *rtlabel_id2name(u_int16_t, char *, size_t);
445u_int16_t rtlabel_name2id(const char *);
446struct sockaddr *rtlabel_id2sa(u_int16_t, struct sockaddr_rtlabel *);
447void rtlabel_unref(u_int16_t);
448
449/*
450 * Values for additional argument to rtalloc()
451 */
452#define RT_RESOLVE 1
453
454extern struct rtstat rtstat;
455extern u_long rtgeneration;
456
457struct mbuf;
458struct socket;
459struct ifnet;
460struct sockaddr_in6;
461struct if_ieee80211_data;
462struct bfd_config;
463
464void route_init(void);
465int route_cache(struct route *, const struct in_addr *,
466 const struct in_addr *, u_int);
467struct rtentry *route_mpath(struct route *, const struct in_addr *,
468 const struct in_addr *, u_int);
469int route6_cache(struct route *, const struct in6_addr *,
470 const struct in6_addr *, u_int);
471struct rtentry *route6_mpath(struct route *, const struct in6_addr *,
472 const struct in6_addr *, u_int);
473void rtm_ifchg(struct ifnet *);
474void rtm_ifannounce(struct ifnet *, int);
475void rtm_bfd(struct bfd_config *);
476void rtm_80211info(struct ifnet *, struct if_ieee80211_data *);
477void rt_maskedcopy(struct sockaddr *,
478 struct sockaddr *, struct sockaddr *);
479struct sockaddr *rt_plen2mask(const struct rtentry *, struct sockaddr_in6 *);
480void rtm_send(struct rtentry *, int, int, unsigned int);
481void rtm_addr(int, struct ifaddr *);
482void rtm_miss(int, struct rt_addrinfo *, int, uint8_t, u_int, int, u_int);
483void rtm_proposal(struct ifnet *, struct rt_addrinfo *, int, uint8_t);
484int rt_setgate(struct rtentry *, const struct sockaddr *, u_int);
485struct rtentry *rt_getll(struct rtentry *);
486
487void rt_timer_init(void);
488int rt_timer_add(struct rtentry *,
489 struct rttimer_queue *, u_int);
490void rt_timer_remove_all(struct rtentry *);
491time_t rt_timer_get_expire(const struct rtentry *);
492void rt_timer_queue_init(struct rttimer_queue *, int,
493 void(*)(struct rtentry *, u_int));
494void rt_timer_queue_change(struct rttimer_queue *, int);
495void rt_timer_queue_flush(struct rttimer_queue *);
496unsigned long rt_timer_queue_count(struct rttimer_queue *);
497void rt_timer_timer(void *);
498
499int rt_mpls_set(struct rtentry *, const struct sockaddr *, uint8_t);
500void rt_mpls_clear(struct rtentry *);
501
502int rtisvalid(struct rtentry *);
503int rt_hash(struct rtentry *, const struct sockaddr *, uint32_t *);
504struct rtentry *rtalloc_mpath(const struct sockaddr *, uint32_t *, u_int);
505struct rtentry *rtalloc(const struct sockaddr *, int, unsigned int);
506void rtref(struct rtentry *);
507void rtfree(struct rtentry *);
508
509int rt_ifa_add(struct ifaddr *, int, struct sockaddr *, unsigned int);
510int rt_ifa_del(struct ifaddr *, int, struct sockaddr *, unsigned int);
511void rt_ifa_purge(struct ifaddr *);
512int rt_ifa_addlocal(struct ifaddr *);
513int rt_ifa_dellocal(struct ifaddr *);
514void rtredirect(struct sockaddr *, struct sockaddr *, struct sockaddr *,
515 struct rtentry **, unsigned int);
516int rtrequest(int, struct rt_addrinfo *, u_int8_t, struct rtentry **,
517 u_int);
518int rtrequest_delete(struct rt_addrinfo *, u_int8_t, struct ifnet *,
519 struct rtentry **, u_int);
520int rt_if_track(struct ifnet *);
521int rt_if_linkstate_change(struct rtentry *, void *, u_int);
522int rtdeletemsg(struct rtentry *, struct ifnet *, u_int);
523#endif /* _KERNEL */
524
525#endif /* _NET_ROUTE_H_ */