1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * dhcpcd - route management 4 * Copyright (c) 2006-2023 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 /* Maximum interface index is generally USHORT_MAX or 65535. 97 * Add some padding for other stuff and we get offsets for the 98 * below that should work automatically. 99 * This is only an issue if the user defines higher metrics in 100 * their configuration, but then they might wish to override also. */ 101 #define RTMETRIC_BASE 1000U 102 #define RTMETRIC_WIRELESS 2000U 103 #define RTMETRIC_IPV4LL 1000000U 104 #define RTMETRIC_ROAM 2000000U 105 #ifdef HAVE_ROUTE_PREF 106 int rt_pref; 107 #endif 108 #define RTPREF_HIGH 1 109 #define RTPREF_MEDIUM 0 /* has to be zero */ 110 #define RTPREF_LOW (-1) 111 #define RTPREF_RESERVED (-2) 112 #define RTPREF_INVALID (-3) /* internal */ 113 unsigned int rt_dflags; 114 #define RTDF_IFA_ROUTE 0x01 /* Address generated route */ 115 #define RTDF_FAKE 0x02 /* Maybe us on lease reboot */ 116 #define RTDF_IPV4LL 0x04 /* IPv4LL route */ 117 #define RTDF_RA 0x08 /* Router Advertisement */ 118 #define RTDF_DHCP 0x10 /* DHCP route */ 119 #define RTDF_STATIC 0x20 /* Configured in dhcpcd */ 120 #define RTDF_GATELINK 0x40 /* Gateway is on link */ 121 size_t rt_order; 122 rb_node_t rt_tree; 123 }; 124 125 extern const rb_tree_ops_t rt_compare_list_ops; 126 extern const rb_tree_ops_t rt_compare_proto_ops; 127 128 void rt_init(struct dhcpcd_ctx *); 129 void rt_dispose(struct dhcpcd_ctx *); 130 void rt_free(struct rt *); 131 void rt_freeif(struct interface *); 132 bool rt_is_default(const struct rt *); 133 void rt_headclear0(struct dhcpcd_ctx *, rb_tree_t *, int); 134 void rt_headclear(rb_tree_t *, int); 135 void rt_headfreeif(rb_tree_t *); 136 struct rt * rt_new0(struct dhcpcd_ctx *); 137 void rt_setif(struct rt *, struct interface *); 138 struct rt * rt_new(struct interface *); 139 struct rt * rt_proto_add_ctx(rb_tree_t *, struct rt *, struct dhcpcd_ctx *); 140 struct rt * rt_proto_add(rb_tree_t *, struct rt *); 141 int rt_cmp_dest(const struct rt *, const struct rt *); 142 void rt_recvrt(int, const struct rt *, pid_t); 143 void rt_build(struct dhcpcd_ctx *, int); 144 145 #endif 146