1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * dhcpcd - route management 4 * Copyright (c) 2006-2019 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 #if defined(__OpenBSD__) || defined (__sun) 64 # define ROUTE_PER_GATEWAY 65 /* XXX dhcpcd doesn't really support this yet. 66 * But that's generally OK if only dhcpcd is managing routes. */ 67 #endif 68 69 /* OpenBSD defines this as a "convienience" ..... we work around it. */ 70 #ifdef __OpenBSD__ 71 #undef rt_mtu 72 #endif 73 74 struct rt { 75 union sa_ss rt_ss_dest; 76 #define rt_dest rt_ss_dest.sa 77 union sa_ss rt_ss_netmask; 78 #define rt_netmask rt_ss_netmask.sa 79 union sa_ss rt_ss_gateway; 80 #define rt_gateway rt_ss_gateway.sa 81 struct interface *rt_ifp; 82 union sa_ss rt_ss_ifa; 83 #define rt_ifa rt_ss_ifa.sa 84 unsigned int rt_flags; 85 unsigned int rt_mtu; 86 #ifdef HAVE_ROUTE_METRIC 87 unsigned int rt_metric; 88 #endif 89 unsigned int rt_dflags; 90 #define RTDF_IFA_ROUTE 0x02 /* Address generated route */ 91 #define RTDF_FAKE 0x04 /* Maybe us on lease reboot */ 92 #define RTDF_RA 0x08 /* Router Advertisement */ 93 #define RTDF_DHCP 0x10 /* DHCP route */ 94 #define RTDF_STATIC 0x20 /* Configured in dhcpcd */ 95 #define RTDF_GATELINK 0x40 /* Gateway is on link */ 96 size_t rt_order; 97 rb_node_t rt_tree; 98 }; 99 100 extern const rb_tree_ops_t rt_compare_list_ops; 101 extern const rb_tree_ops_t rt_compare_proto_ops; 102 103 void rt_init(struct dhcpcd_ctx *); 104 void rt_dispose(struct dhcpcd_ctx *); 105 void rt_free(struct rt *); 106 void rt_freeif(struct interface *); 107 bool rt_is_default(const struct rt *); 108 void rt_headclear0(struct dhcpcd_ctx *, rb_tree_t *, int); 109 void rt_headclear(rb_tree_t *, int); 110 void rt_headfreeif(rb_tree_t *); 111 struct rt * rt_new0(struct dhcpcd_ctx *); 112 void rt_setif(struct rt *, struct interface *); 113 struct rt * rt_new(struct interface *); 114 struct rt * rt_proto_add_ctx(rb_tree_t *, struct rt *, struct dhcpcd_ctx *); 115 struct rt * rt_proto_add(rb_tree_t *, struct rt *); 116 int rt_cmp_dest(const struct rt *, const struct rt *); 117 void rt_recvrt(int, const struct rt *, pid_t); 118 void rt_build(struct dhcpcd_ctx *, int); 119 120 #endif 121