1 /* 2 * dhcpcd - route management 3 * SPDX-License-Identifier: BSD-2-Clause 4 * Copyright (c) 2006-2025 Roy Marples <roy (at) 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 #else 35 #include "rbtree.h" 36 #endif 37 38 #include <sys/socket.h> 39 40 #include <net/route.h> 41 42 #include <stdbool.h> 43 44 #include "dhcpcd.h" 45 #include "sa.h" 46 47 /* 48 * Enable the route free list by default as 49 * memory usage is still reported as low/unchanged even 50 * when dealing with millions of routes. 51 */ 52 #if !defined(RT_FREE_ROUTE_TABLE) 53 #define RT_FREE_ROUTE_TABLE 1 54 #elif RT_FREE_ROUTE_TABLE == 0 55 #undef RT_FREE_ROUTE_TABLE 56 #endif 57 58 /* Some systems have route metrics. 59 * OpenBSD route priority is not this. */ 60 #ifndef HAVE_ROUTE_METRIC 61 #if defined(__linux__) 62 #define HAVE_ROUTE_METRIC 1 63 #endif 64 #endif 65 66 #ifdef __linux__ 67 #include <linux/version.h> /* RTA_PREF is only an enum.... */ 68 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0) 69 #define HAVE_ROUTE_PREF 70 #endif 71 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) 72 #define HAVE_ROUTE_LIFETIME /* For IPv6 routes only */ 73 #endif 74 #endif 75 76 #if defined(BSD) 77 #define HAVE_RT_MISSFILTER 78 #endif 79 80 #if defined(__OpenBSD__) || defined(__sun) 81 #define ROUTE_PER_GATEWAY 82 /* XXX dhcpcd doesn't really support this yet. 83 * But that's generally OK if only dhcpcd is managing routes. */ 84 #endif 85 86 /* OpenBSD defines this as a "convienience" ..... we work around it. */ 87 #ifdef __OpenBSD__ 88 #undef rt_mtu 89 #endif 90 91 struct rt { 92 struct sockaddr *rt_dest; 93 struct sockaddr *rt_netmask; 94 struct sockaddr *rt_gateway; 95 struct interface *rt_ifp; 96 struct sockaddr *rt_ifa; 97 98 /* Storage for the above sockaddrs */ 99 struct sockaddr_storage rt_ss_dest; 100 struct sockaddr_storage rt_ss_netmask; 101 struct sockaddr_storage rt_ss_gateway; 102 struct sockaddr_storage rt_ss_ifa; 103 104 unsigned int rt_flags; 105 unsigned int rt_mtu; 106 107 #ifdef HAVE_ROUTE_METRIC 108 unsigned int rt_metric; 109 #endif 110 /* Maximum interface index is generally USHORT_MAX or 65535. 111 * Add some padding for other stuff and we get offsets for the 112 * below that should work automatically. 113 * This is only an issue if the user defines higher metrics in 114 * their configuration, but then they might wish to override also. */ 115 #define RTMETRIC_BASE 1000U 116 #define RTMETRIC_WIRELESS 2000U 117 #define RTMETRIC_IPV4LL 1000000U 118 #define RTMETRIC_ROAM 2000000U 119 120 #ifdef HAVE_ROUTE_PREF 121 int rt_pref; 122 #endif 123 #define RTPREF_HIGH 1 124 #define RTPREF_MEDIUM 0 /* has to be zero */ 125 #define RTPREF_LOW (-1) 126 #define RTPREF_RESERVED (-2) 127 #define RTPREF_INVALID (-3) /* internal */ 128 129 unsigned int rt_dflags; 130 #define RTDF_IFA_ROUTE 0x01 /* Address generated route */ 131 #define RTDF_FAKE 0x02 /* Maybe us on lease reboot */ 132 #define RTDF_IPV4LL 0x04 /* IPv4LL route */ 133 #define RTDF_RA 0x08 /* Router Advertisement */ 134 #define RTDF_DHCP 0x10 /* DHCP route */ 135 #define RTDF_STATIC 0x20 /* Configured in dhcpcd */ 136 #define RTDF_GATELINK 0x40 /* Gateway is on link */ 137 138 size_t rt_order; 139 rb_node_t rt_tree; 140 141 #ifdef HAVE_ROUTE_LIFETIME 142 struct timespec rt_acquired; /* timestamp of acquisition */ 143 uint32_t rt_lifetime; /* current lifetime of route */ 144 #define RTLIFETIME_DEV_MAX 2 /* max deviation for cmp */ 145 #endif 146 }; 147 148 extern const rb_tree_ops_t rt_compare_list_ops; 149 extern const rb_tree_ops_t rt_compare_proto_ops; 150 151 void rt_init_routes(struct dhcpcd_ctx *); 152 void rt_dispose(struct dhcpcd_ctx *); 153 void rt_free(struct rt *); 154 void rt_freeif(struct interface *); 155 bool rt_is_default(const struct rt *); 156 void rt_headclear0(struct dhcpcd_ctx *, rb_tree_t *, int); 157 void rt_headclear(rb_tree_t *, int); 158 void rt_headfreeif(rb_tree_t *); 159 void rt_init(struct rt *); 160 void rt_copy(struct rt *, const struct rt *); 161 struct rt *rt_new0(struct dhcpcd_ctx *); 162 void rt_setif(struct rt *, struct interface *); 163 struct rt *rt_new(struct interface *); 164 struct rt *rt_proto_add_ctx(rb_tree_t *, struct rt *, struct dhcpcd_ctx *); 165 struct rt *rt_proto_add(rb_tree_t *, struct rt *); 166 int rt_cmp_dest(const struct rt *, const struct rt *); 167 void rt_recvrt(int, const struct rt *, pid_t); 168 void rt_build(struct dhcpcd_ctx *, int); 169 170 #endif 171