xref: /dragonfly/contrib/dhcpcd/src/route.h (revision c51f15da)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - route management
4  * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
5  * All rights reserved
6 
7  * rEDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS, WITH OR WITHOUT
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef ROUTE_H
30 #define ROUTE_H
31 
32 #ifdef HAVE_SYS_RBTREE_H
33 #include <sys/rbtree.h>
34 #endif
35 
36 #include <sys/socket.h>
37 #include <net/route.h>
38 
39 #include <stdbool.h>
40 
41 #include "dhcpcd.h"
42 #include "sa.h"
43 
44 /*
45  * Enable the route free list by default as
46  * memory usage is still reported as low/unchanged even
47  * when dealing with millions of routes.
48  */
49 #if !defined(RT_FREE_ROUTE_TABLE)
50 #define	RT_FREE_ROUTE_TABLE 1
51 #elif RT_FREE_ROUTE_TABLE == 0
52 #undef	RT_FREE_ROUTE_TABLE
53 #endif
54 
55 /* Some systems have route metrics.
56  * OpenBSD route priority is not this. */
57 #ifndef HAVE_ROUTE_METRIC
58 # if defined(__linux__)
59 #  define HAVE_ROUTE_METRIC 1
60 # endif
61 #endif
62 
63 #ifdef __linux__
64 # include <linux/version.h> /* RTA_PREF is only an enum.... */
65 # if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
66 #  define HAVE_ROUTE_PREF
67 # endif
68 #endif
69 
70 #if defined(__OpenBSD__) || defined (__sun)
71 #  define ROUTE_PER_GATEWAY
72 /* XXX dhcpcd doesn't really support this yet.
73  * But that's generally OK if only dhcpcd is managing routes. */
74 #endif
75 
76 /* OpenBSD defines this as a "convienience" ..... we work around it. */
77 #ifdef __OpenBSD__
78 #undef rt_mtu
79 #endif
80 
81 struct rt {
82 	union sa_ss		rt_ss_dest;
83 #define rt_dest			rt_ss_dest.sa
84 	union sa_ss		rt_ss_netmask;
85 #define rt_netmask		rt_ss_netmask.sa
86 	union sa_ss		rt_ss_gateway;
87 #define rt_gateway		rt_ss_gateway.sa
88 	struct interface	*rt_ifp;
89 	union sa_ss		rt_ss_ifa;
90 #define rt_ifa			rt_ss_ifa.sa
91 	unsigned int		rt_flags;
92 	unsigned int		rt_mtu;
93 #ifdef HAVE_ROUTE_METRIC
94 	unsigned int		rt_metric;
95 #endif
96 #ifdef HAVE_ROUTE_PREF
97 	int			rt_pref;
98 #endif
99 #define RTPREF_HIGH	1
100 #define RTPREF_MEDIUM	0	/* has to be zero */
101 #define RTPREF_LOW	(-1)
102 #define RTPREF_RESERVED	(-2)
103 #define RTPREF_INVALID	(-3)	/* internal */
104 	unsigned int		rt_dflags;
105 #define	RTDF_IFA_ROUTE		0x01		/* Address generated route */
106 #define	RTDF_FAKE		0x02		/* Maybe us on lease reboot */
107 #define	RTDF_IPV4LL		0x04		/* IPv4LL route */
108 #define	RTDF_RA			0x08		/* Router Advertisement */
109 #define	RTDF_DHCP		0x10		/* DHCP route */
110 #define	RTDF_STATIC		0x20		/* Configured in dhcpcd */
111 #define	RTDF_GATELINK		0x40		/* Gateway is on link */
112 	size_t			rt_order;
113 	rb_node_t		rt_tree;
114 };
115 
116 extern const rb_tree_ops_t rt_compare_list_ops;
117 extern const rb_tree_ops_t rt_compare_proto_ops;
118 
119 void rt_init(struct dhcpcd_ctx *);
120 void rt_dispose(struct dhcpcd_ctx *);
121 void rt_free(struct rt *);
122 void rt_freeif(struct interface *);
123 bool rt_is_default(const struct rt *);
124 void rt_headclear0(struct dhcpcd_ctx *, rb_tree_t *, int);
125 void rt_headclear(rb_tree_t *, int);
126 void rt_headfreeif(rb_tree_t *);
127 struct rt * rt_new0(struct dhcpcd_ctx *);
128 void rt_setif(struct rt *, struct interface *);
129 struct rt * rt_new(struct interface *);
130 struct rt * rt_proto_add_ctx(rb_tree_t *, struct rt *, struct dhcpcd_ctx *);
131 struct rt * rt_proto_add(rb_tree_t *, struct rt *);
132 int rt_cmp_dest(const struct rt *, const struct rt *);
133 void rt_recvrt(int, const struct rt *, pid_t);
134 void rt_build(struct dhcpcd_ctx *, int);
135 
136 #endif
137