xref: /freebsd/sys/net/route/route_ifaddrs.c (revision 4b9d6057)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1986, 1991, 1993
5  *	The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "opt_route.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/socket.h>
38 #include <sys/sysctl.h>
39 #include <sys/syslog.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/rmlock.h>
43 
44 #include <net/if.h>
45 #include <net/if_var.h>
46 #include <net/if_private.h>
47 #include <net/if_dl.h>
48 #include <net/route.h>
49 #include <net/route/route_ctl.h>
50 #include <net/route/route_var.h>
51 #include <net/route/nhop.h>
52 #include <net/vnet.h>
53 
54 #include <netinet/in.h>
55 
56 /*
57  * Control interface address fib propagation.
58  * By default, interface address routes are added to the fib of the interface.
59  * Once set to non-zero, adds interface address route to all fibs.
60  */
61 VNET_DEFINE(u_int, rt_add_addr_allfibs) = 0;
62 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RWTUN | CTLFLAG_VNET,
63     &VNET_NAME(rt_add_addr_allfibs), 0, "");
64 
65 /*
66  * Executes routing tables change specified by @cmd and @info for the fib
67  * @fibnum. Generates routing message on success.
68  * Note: it assumes there is only single route (interface route) for the
69  * provided prefix.
70  * Returns 0 on success or errno.
71  */
72 static int
73 rib_handle_ifaddr_one(uint32_t fibnum, int cmd, struct rt_addrinfo *info)
74 {
75 	struct rib_cmd_info rc;
76 	struct nhop_object *nh;
77 	int error;
78 
79 	error = rib_action(fibnum, cmd, info, &rc);
80 	if (error == 0) {
81 		if (cmd == RTM_ADD)
82 			nh = nhop_select(rc.rc_nh_new, 0);
83 		else
84 			nh = nhop_select(rc.rc_nh_old, 0);
85 		rt_routemsg(cmd, rc.rc_rt, nh, fibnum);
86 	}
87 
88 	return (error);
89 }
90 
91 /*
92  * Adds/deletes interface prefix specified by @info to the routing table.
93  * If V_rt_add_addr_allfibs is set, iterates over all existing routing
94  * tables, otherwise uses fib in @fibnum. Generates routing message for
95  *  each table.
96  * Returns 0 on success or errno.
97  */
98 int
99 rib_handle_ifaddr_info(uint32_t fibnum, int cmd, struct rt_addrinfo *info)
100 {
101 	int error = 0, last_error = 0;
102 	bool didwork = false;
103 
104 	if (V_rt_add_addr_allfibs == 0) {
105 		error = rib_handle_ifaddr_one(fibnum, cmd, info);
106 		didwork = (error == 0);
107 	} else {
108 		for (fibnum = 0; fibnum < V_rt_numfibs; fibnum++) {
109 			error = rib_handle_ifaddr_one(fibnum, cmd, info);
110 			if (error == 0)
111 				didwork = true;
112 			else
113 				last_error = error;
114 		}
115 	}
116 
117 	if (cmd == RTM_DELETE) {
118 		if (didwork) {
119 			error = 0;
120 		} else {
121 			/* we only give an error if it wasn't in any table */
122 			error = ((info->rti_flags & RTF_HOST) ?
123 			    EHOSTUNREACH : ENETUNREACH);
124 		}
125 	} else {
126 		if (last_error != 0) {
127 			/* return an error if any of them failed */
128 			error = last_error;
129 		}
130 	}
131 	return (error);
132 }
133 
134 static int
135 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
136     struct sockaddr *ia)
137 {
138 	struct rib_cmd_info rc;
139 	struct epoch_tracker et;
140 	int error;
141 	struct rt_addrinfo info;
142 	struct sockaddr_dl null_sdl;
143 	struct ifnet *ifp;
144 
145 	ifp = ifa->ifa_ifp;
146 
147 	NET_EPOCH_ENTER(et);
148 	bzero(&info, sizeof(info));
149 	if (cmd != RTM_DELETE)
150 		info.rti_ifp = V_loif;
151 	if (cmd == RTM_ADD) {
152 		/* explicitly specify (loopback) ifa */
153 		if (info.rti_ifp != NULL)
154 			info.rti_ifa = ifaof_ifpforaddr(ifa->ifa_addr, info.rti_ifp);
155 	}
156 	info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
157 	info.rti_info[RTAX_DST] = ia;
158 	info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
159 	link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
160 
161 	error = rib_action(ifp->if_fib, cmd, &info, &rc);
162 	NET_EPOCH_EXIT(et);
163 
164 	if (error == 0 ||
165 	    (cmd == RTM_ADD && error == EEXIST) ||
166 	    (cmd == RTM_DELETE && (error == ENOENT || error == ESRCH)))
167 		return (error);
168 
169 	log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
170 		__func__, otype, if_name(ifp), error);
171 
172 	return (error);
173 }
174 
175 int
176 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
177 {
178 
179 	return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
180 }
181 
182 int
183 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
184 {
185 
186 	return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
187 }
188 
189 int
190 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
191 {
192 
193 	return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
194 }
195 
196 static bool
197 match_kernel_route(const struct rtentry *rt, struct nhop_object *nh)
198 {
199 	if (!NH_IS_NHGRP(nh) && (nhop_get_rtflags(nh) & RTF_PINNED) &&
200 	    nh->nh_aifp->if_fib == nhop_get_fibnum(nh))
201 		return (true);
202 	return (false);
203 }
204 
205 static int
206 pick_kernel_route(struct rtentry *rt, void *arg)
207 {
208 	struct nhop_object *nh = rt->rt_nhop;
209 	struct rib_head *rh_dst = (struct rib_head *)arg;
210 
211 	if (match_kernel_route(rt, nh)) {
212 		struct rib_cmd_info rc = {};
213 		struct route_nhop_data rnd = {
214 			.rnd_nhop = nh,
215 			.rnd_weight = rt->rt_weight,
216 		};
217 		rib_copy_route(rt, &rnd, rh_dst, &rc);
218 	}
219 	return (0);
220 }
221 
222 /*
223  * Tries to copy kernel routes matching pattern from @rh_src to @rh_dst.
224  *
225  * Note: as this function acquires locks for both @rh_src and @rh_dst,
226  *  it needs to be called under RTABLES_LOCK() to avoid deadlocking
227  * with multiple ribs.
228  */
229 void
230 rib_copy_kernel_routes(struct rib_head *rh_src, struct rib_head *rh_dst)
231 {
232 	struct epoch_tracker et;
233 
234 	if (V_rt_add_addr_allfibs == 0)
235 		return;
236 
237 	NET_EPOCH_ENTER(et);
238 	rib_walk_ext_internal(rh_src, false, pick_kernel_route, NULL, rh_dst);
239 	NET_EPOCH_EXIT(et);
240 }
241 
242