]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_rmx.c
MFV: r362513
[FreeBSD/FreeBSD.git] / sys / netinet / in_rmx.c
1 /*-
2  * Copyright 1994, 1995 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_mpath.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
39 #include <sys/socket.h>
40 #include <sys/mbuf.h>
41
42 #include <net/if.h>
43 #include <net/if_var.h>
44 #include <net/route.h>
45 #include <net/route/route_ctl.h>
46 #include <net/route/route_var.h>
47 #include <net/route/nhop.h>
48 #include <net/route/shared.h>
49 #include <net/vnet.h>
50
51 #include <netinet/in.h>
52 #include <netinet/in_var.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_icmp.h>
55 #include <netinet/ip_var.h>
56
57 extern int      in_inithead(void **head, int off, u_int fibnum);
58 #ifdef VIMAGE
59 extern int      in_detachhead(void **head, int off);
60 #endif
61
62 static int
63 rib4_preadd(u_int fibnum, const struct sockaddr *addr, const struct sockaddr *mask,
64     struct nhop_object *nh)
65 {
66         const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
67         uint16_t nh_type;
68         int rt_flags;
69
70         /* XXX: RTF_LOCAL && RTF_MULTICAST */
71
72         rt_flags = nhop_get_rtflags(nh);
73
74         if (rt_flags & RTF_HOST) {
75
76                 /*
77                  * Backward compatibility:
78                  * if the destination is broadcast,
79                  * mark route as broadcast.
80                  * This behavior was useful when route cloning
81                  * was in place, so there was an explicit cloned
82                  * route for every broadcasted address.
83                  * Currently (2020-04) there is no kernel machinery
84                  * to do route cloning, though someone might explicitly
85                  * add these routes to support some cases with active-active
86                  * load balancing. Given that, retain this support.
87                  */
88                 if (in_broadcast(addr4->sin_addr, nh->nh_ifp)) {
89                         rt_flags |= RTF_BROADCAST;
90                         nhop_set_rtflags(nh, rt_flags);
91                         nh->nh_flags |= NHF_BROADCAST;
92                 }
93         }
94
95         /*
96          * Check route MTU:
97          * inherit interface MTU if not set or
98          * check if MTU is too large.
99          */
100         if (nh->nh_mtu == 0) {
101                 nh->nh_mtu = nh->nh_ifp->if_mtu;
102         } else if (nh->nh_mtu > nh->nh_ifp->if_mtu)
103                 nh->nh_mtu = nh->nh_ifp->if_mtu;
104
105         /* Ensure that default route nhop has special flag */
106         const struct sockaddr_in *mask4 = (const struct sockaddr_in *)mask;
107         if ((rt_flags & RTF_HOST) == 0 && mask4 != NULL &&
108             mask4->sin_addr.s_addr == 0)
109                 nh->nh_flags |= NHF_DEFAULT;
110
111         /* Set nhop type to basic per-AF nhop */
112         if (nhop_get_type(nh) == 0) {
113                 if (nh->nh_flags & NHF_GATEWAY)
114                         nh_type = NH_TYPE_IPV4_ETHER_NHOP;
115                 else
116                         nh_type = NH_TYPE_IPV4_ETHER_RSLV;
117
118                 nhop_set_type(nh, nh_type);
119         }
120
121         return (0);
122 }
123
124 static int _in_rt_was_here;
125 /*
126  * Initialize our routing tree.
127  */
128 int
129 in_inithead(void **head, int off, u_int fibnum)
130 {
131         struct rib_head *rh;
132
133         rh = rt_table_init(32, AF_INET, fibnum);
134         if (rh == NULL)
135                 return (0);
136
137         rh->rnh_preadd = rib4_preadd;
138 #ifdef  RADIX_MPATH
139         rt_mpath_init_rnh(rh);
140 #endif
141         *head = (void *)rh;
142
143         if (_in_rt_was_here == 0 ) {
144                 _in_rt_was_here = 1;
145         }
146         return 1;
147 }
148
149 #ifdef VIMAGE
150 int
151 in_detachhead(void **head, int off)
152 {
153
154         rt_table_destroy((struct rib_head *)(*head));
155         return (1);
156 }
157 #endif
158
159 /*
160  * This zaps old routes when the interface goes down or interface
161  * address is deleted.  In the latter case, it deletes static routes
162  * that point to this address.  If we don't do this, we may end up
163  * using the old address in the future.  The ones we always want to
164  * get rid of are things like ARP entries, since the user might down
165  * the interface, walk over to a completely different network, and
166  * plug back in.
167  */
168 struct in_ifadown_arg {
169         struct ifaddr *ifa;
170         int del;
171 };
172
173 static int
174 in_ifadownkill(const struct rtentry *rt, const struct nhop_object *nh,
175     void *xap)
176 {
177         struct in_ifadown_arg *ap = xap;
178
179         if (nh->nh_ifa != ap->ifa)
180                 return (0);
181
182         if ((nhop_get_rtflags(nh) & RTF_STATIC) != 0 && ap->del == 0)
183                 return (0);
184
185         return (1);
186 }
187
188 void
189 in_ifadown(struct ifaddr *ifa, int delete)
190 {
191         struct in_ifadown_arg arg;
192
193         KASSERT(ifa->ifa_addr->sa_family == AF_INET,
194             ("%s: wrong family", __func__));
195
196         arg.ifa = ifa;
197         arg.del = delete;
198
199         rt_foreach_fib_walk_del(AF_INET, in_ifadownkill, &arg);
200         ifa->ifa_flags &= ~IFA_ROUTE;           /* XXXlocking? */
201 }
202