]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/route/route_ifaddrs.c
MFV f83ac37f1e66: libbsdxml (expat) 2.4.3.
[FreeBSD/FreeBSD.git] / sys / net / route / route_ifaddrs.c
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  * $FreeBSD$
33  */
34
35 #include "opt_route.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 #include <sys/syslog.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/rmlock.h>
46
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/if_dl.h>
50 #include <net/route.h>
51 #include <net/route/route_ctl.h>
52 #include <net/route/route_var.h>
53 #include <net/route/nhop.h>
54 #include <net/vnet.h>
55
56 #include <netinet/in.h>
57
58 /*
59  * Control interface address fib propagation.
60  * By default, interface address routes are added to the fib of the interface.
61  * Once set to non-zero, adds interface address route to all fibs.
62  */
63 VNET_DEFINE(u_int, rt_add_addr_allfibs) = 0;
64 SYSCTL_UINT(_net, OID_AUTO, add_addr_allfibs, CTLFLAG_RWTUN | CTLFLAG_VNET,
65     &VNET_NAME(rt_add_addr_allfibs), 0, "");
66
67 /*
68  * Executes routing tables change specified by @cmd and @info for the fib
69  * @fibnum. Generates routing message on success.
70  * Note: it assumes there is only single route (interface route) for the
71  * provided prefix.
72  * Returns 0 on success or errno.
73  */
74 static int
75 rib_handle_ifaddr_one(uint32_t fibnum, int cmd, struct rt_addrinfo *info)
76 {
77         struct rib_cmd_info rc;
78         struct nhop_object *nh;
79         int error;
80
81         error = rib_action(fibnum, cmd, info, &rc);
82         if (error == 0) {
83                 if (cmd == RTM_ADD)
84                         nh = nhop_select(rc.rc_nh_new, 0);
85                 else
86                         nh = nhop_select(rc.rc_nh_old, 0);
87                 rt_routemsg(cmd, rc.rc_rt, nh, fibnum);
88         }
89
90         return (error);
91 }
92
93 /*
94  * Adds/deletes interface prefix specified by @info to the routing table.
95  * If V_rt_add_addr_allfibs is set, iterates over all existing routing
96  * tables, otherwise uses fib in @fibnum. Generates routing message for
97  *  each table.
98  * Returns 0 on success or errno.
99  */
100 int
101 rib_handle_ifaddr_info(uint32_t fibnum, int cmd, struct rt_addrinfo *info)
102 {
103         int error = 0, last_error = 0;
104         bool didwork = false;
105
106         if (V_rt_add_addr_allfibs == 0) {
107                 error = rib_handle_ifaddr_one(fibnum, cmd, info);
108                 didwork = (error == 0);
109         } else {
110                 for (fibnum = 0; fibnum < V_rt_numfibs; fibnum++) {
111                         error = rib_handle_ifaddr_one(fibnum, cmd, info);
112                         if (error == 0)
113                                 didwork = true;
114                         else
115                                 last_error = error;
116                 }
117         }
118
119         if (cmd == RTM_DELETE) {
120                 if (didwork) {
121                         error = 0;
122                 } else {
123                         /* we only give an error if it wasn't in any table */
124                         error = ((info->rti_flags & RTF_HOST) ?
125                             EHOSTUNREACH : ENETUNREACH);
126                 }
127         } else {
128                 if (last_error != 0) {
129                         /* return an error if any of them failed */
130                         error = last_error;
131                 }
132         }
133         return (error);
134 }
135
136 static int
137 ifa_maintain_loopback_route(int cmd, const char *otype, struct ifaddr *ifa,
138     struct sockaddr *ia)
139 {
140         struct rib_cmd_info rc;
141         struct epoch_tracker et;
142         int error;
143         struct rt_addrinfo info;
144         struct sockaddr_dl null_sdl;
145         struct ifnet *ifp;
146
147         ifp = ifa->ifa_ifp;
148
149         NET_EPOCH_ENTER(et);
150         bzero(&info, sizeof(info));
151         if (cmd != RTM_DELETE)
152                 info.rti_ifp = V_loif;
153         if (cmd == RTM_ADD) {
154                 /* explicitly specify (loopback) ifa */
155                 if (info.rti_ifp != NULL)
156                         info.rti_ifa = ifaof_ifpforaddr(ifa->ifa_addr, info.rti_ifp);
157         }
158         info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED;
159         info.rti_info[RTAX_DST] = ia;
160         info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&null_sdl;
161         link_init_sdl(ifp, (struct sockaddr *)&null_sdl, ifp->if_type);
162
163         error = rib_action(ifp->if_fib, cmd, &info, &rc);
164         NET_EPOCH_EXIT(et);
165
166         if (error == 0 ||
167             (cmd == RTM_ADD && error == EEXIST) ||
168             (cmd == RTM_DELETE && (error == ENOENT || error == ESRCH)))
169                 return (error);
170
171         log(LOG_DEBUG, "%s: %s failed for interface %s: %u\n",
172                 __func__, otype, if_name(ifp), error);
173
174         return (error);
175 }
176
177 int
178 ifa_add_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
179 {
180
181         return (ifa_maintain_loopback_route(RTM_ADD, "insertion", ifa, ia));
182 }
183
184 int
185 ifa_del_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
186 {
187
188         return (ifa_maintain_loopback_route(RTM_DELETE, "deletion", ifa, ia));
189 }
190
191 int
192 ifa_switch_loopback_route(struct ifaddr *ifa, struct sockaddr *ia)
193 {
194
195         return (ifa_maintain_loopback_route(RTM_CHANGE, "switch", ifa, ia));
196 }
197
198