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