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 * @(#)route.c 8.3.1.1 (Berkeley) 2/23/95
32 */
33
34 #include "opt_route.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/socket.h>
40 #include <sys/sysctl.h>
41 #include <sys/syslog.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/rmlock.h>
45
46 #include <net/if.h>
47 #include <net/if_var.h>
48 #include <net/if_dl.h>
49 #include <net/route.h>
50 #include <net/route/route_ctl.h>
51 #include <net/route/route_var.h>
52 #include <net/route/nhop.h>
53 #include <net/vnet.h>
54
55 #include <netinet/in.h>
56
57 /*
58 * Control interface address fib propagation.
59 * By default, interface address routes are added to the fib of the interface.
60 * Once set to non-zero, adds interface address route to all fibs.
61 */
62 VNET_DEFINE(u_int, rt_add_addr_allfibs) = 0;
63 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RWTUN | CTLFLAG_VNET,
64 &VNET_NAME(rt_add_addr_allfibs), 0, "");
65
66 /*
67 * Executes routing tables change specified by @cmd and @info for the fib
68 * @fibnum. Generates routing message on success.
69 * Note: it assumes there is only single route (interface route) for the
70 * provided prefix.
71 * Returns 0 on success or errno.
72 */
73 static int
rib_handle_ifaddr_one(uint32_t fibnum,int cmd,struct rt_addrinfo * info)74 rib_handle_ifaddr_one(uint32_t fibnum, int cmd, struct rt_addrinfo *info)
75 {
76 struct rib_cmd_info rc;
77 struct nhop_object *nh;
78 int error;
79
80 error = rib_action(fibnum, cmd, info, &rc);
81 if (error == 0) {
82 if (cmd == RTM_ADD)
83 nh = nhop_select(rc.rc_nh_new, 0);
84 else
85 nh = nhop_select(rc.rc_nh_old, 0);
86 rt_routemsg(cmd, rc.rc_rt, nh, fibnum);
87 }
88
89 return (error);
90 }
91
92 /*
93 * Adds/deletes interface prefix specified by @info to the routing table.
94 * If V_rt_add_addr_allfibs is set, iterates over all existing routing
95 * tables, otherwise uses fib in @fibnum. Generates routing message for
96 * each table.
97 * Returns 0 on success or errno.
98 */
99 int
rib_handle_ifaddr_info(uint32_t fibnum,int cmd,struct rt_addrinfo * info)100 rib_handle_ifaddr_info(uint32_t fibnum, int cmd, struct rt_addrinfo *info)
101 {
102 int error = 0, last_error = 0;
103 bool didwork = false;
104
105 if (V_rt_add_addr_allfibs == 0) {
106 error = rib_handle_ifaddr_one(fibnum, cmd, info);
107 didwork = (error == 0);
108 } else {
109 for (fibnum = 0; fibnum < V_rt_numfibs; fibnum++) {
110 error = rib_handle_ifaddr_one(fibnum, cmd, info);
111 if (error == 0)
112 didwork = true;
113 else
114 last_error = error;
115 }
116 }
117
118 if (cmd == RTM_DELETE) {
119 if (didwork) {
120 error = 0;
121 } else {
122 /* we only give an error if it wasn't in any table */
123 error = ((info->rti_flags & RTF_HOST) ?
124 EHOSTUNREACH : ENETUNREACH);
125 }
126 } else {
127 if (last_error != 0) {
128 /* return an error if any of them failed */
129 error = last_error;
130 }
131 }
132 return (error);
133 }
134
135 static int
ifa_maintain_loopback_route(int cmd,const char * otype,struct ifaddr * ifa,struct sockaddr * ia)136 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
137 struct sockaddr *ia)
138 {
139 struct rib_cmd_info rc;
140 struct epoch_tracker et;
141 int error;
142 struct rt_addrinfo info;
143 struct sockaddr_dl null_sdl;
144 struct ifnet *ifp;
145
146 ifp = ifa->ifa_ifp;
147
148 NET_EPOCH_ENTER(et);
149 bzero(&info, sizeof(info));
150 if (cmd != RTM_DELETE)
151 info.rti_ifp = V_loif;
152 if (cmd == RTM_ADD) {
153 /* explicitly specify (loopback) ifa */
154 if (info.rti_ifp != NULL)
155 info.rti_ifa = ifaof_ifpforaddr(ifa->ifa_addr, info.rti_ifp);
156 }
157 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
158 info.rti_info[RTAX_DST] = ia;
159 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
160 link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
161
162 error = rib_action(ifp->if_fib, cmd, &info, &rc);
163 NET_EPOCH_EXIT(et);
164
165 if (error == 0 ||
166 (cmd == RTM_ADD && error == EEXIST) ||
167 (cmd == RTM_DELETE && (error == ENOENT || error == ESRCH)))
168 return (error);
169
170 log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
171 __func__, otype, if_name(ifp), error);
172
173 return (error);
174 }
175
176 int
ifa_add_loopback_route(struct ifaddr * ifa,struct sockaddr * ia)177 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
178 {
179
180 return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
181 }
182
183 int
ifa_del_loopback_route(struct ifaddr * ifa,struct sockaddr * ia)184 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
185 {
186
187 return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
188 }
189
190 int
ifa_switch_loopback_route(struct ifaddr * ifa,struct sockaddr * ia)191 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
192 {
193
194 return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
195 }
196
197 static bool
match_kernel_route(const struct rtentry * rt,struct nhop_object * nh)198 match_kernel_route(const struct rtentry *rt, struct nhop_object *nh)
199 {
200 if (!NH_IS_NHGRP(nh) && (nhop_get_rtflags(nh) & RTF_PINNED) &&
201 nh->nh_aifp->if_fib == nhop_get_fibnum(nh))
202 return (true);
203 return (false);
204 }
205
206 static int
pick_kernel_route(struct rtentry * rt,void * arg)207 pick_kernel_route(struct rtentry *rt, void *arg)
208 {
209 struct nhop_object *nh = rt->rt_nhop;
210 struct rib_head *rh_dst = (struct rib_head *)arg;
211
212 if (match_kernel_route(rt, nh)) {
213 struct rib_cmd_info rc = {};
214 struct route_nhop_data rnd = {
215 .rnd_nhop = nh,
216 .rnd_weight = rt->rt_weight,
217 };
218 rib_copy_route(rt, &rnd, rh_dst, &rc);
219 }
220 return (0);
221 }
222
223 /*
224 * Tries to copy kernel routes matching pattern from @rh_src to @rh_dst.
225 *
226 * Note: as this function acquires locks for both @rh_src and @rh_dst,
227 * it needs to be called under RTABLES_LOCK() to avoid deadlocking
228 * with multiple ribs.
229 */
230 void
rib_copy_kernel_routes(struct rib_head * rh_src,struct rib_head * rh_dst)231 rib_copy_kernel_routes(struct rib_head *rh_src, struct rib_head *rh_dst)
232 {
233 struct epoch_tracker et;
234
235 if (V_rt_add_addr_allfibs == 0)
236 return;
237
238 NET_EPOCH_ENTER(et);
239 rib_walk_ext_internal(rh_src, false, pick_kernel_route, NULL, rh_dst);
240 NET_EPOCH_EXIT(et);
241 }
242
243