xref: /freebsd/sys/netinet/in_rmx.c (revision edca4938f74db18d091868237592abbf7e718669)
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 <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/sysctl.h>
37 #include <sys/socket.h>
38 #include <sys/mbuf.h>
39 
40 #include <net/if.h>
41 #include <net/if_var.h>
42 #include <net/route.h>
43 #include <net/route_var.h>
44 #include <net/vnet.h>
45 
46 #include <netinet/in.h>
47 #include <netinet/in_var.h>
48 #include <netinet/ip.h>
49 #include <netinet/ip_icmp.h>
50 #include <netinet/ip_var.h>
51 
52 extern int	in_inithead(void **head, int off);
53 #ifdef VIMAGE
54 extern int	in_detachhead(void **head, int off);
55 #endif
56 
57 /*
58  * Do what we need to do when inserting a route.
59  */
60 static struct radix_node *
61 in_addroute(void *v_arg, void *n_arg, struct radix_head *head,
62     struct radix_node *treenodes)
63 {
64 	struct rtentry *rt = (struct rtentry *)treenodes;
65 	struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
66 
67 	/*
68 	 * A little bit of help for both IP output and input:
69 	 *   For host routes, we make sure that RTF_BROADCAST
70 	 *   is set for anything that looks like a broadcast address.
71 	 *   This way, we can avoid an expensive call to in_broadcast()
72 	 *   in ip_output() most of the time (because the route passed
73 	 *   to ip_output() is almost always a host route).
74 	 *
75 	 *   We also do the same for local addresses, with the thought
76 	 *   that this might one day be used to speed up ip_input().
77 	 *
78 	 * We also mark routes to multicast addresses as such, because
79 	 * it's easy to do and might be useful (but this is much more
80 	 * dubious since it's so easy to inspect the address).
81 	 */
82 	if (rt->rt_flags & RTF_HOST) {
83 		struct epoch_tracker et;
84 		bool bcast;
85 
86 		NET_EPOCH_ENTER(et);
87 		bcast = in_broadcast(sin->sin_addr, rt->rt_ifp);
88 		NET_EPOCH_EXIT(et);
89 		if (bcast)
90 			rt->rt_flags |= RTF_BROADCAST;
91 		else if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr ==
92 		    sin->sin_addr.s_addr)
93 			rt->rt_flags |= RTF_LOCAL;
94 	}
95 	if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
96 		rt->rt_flags |= RTF_MULTICAST;
97 
98 	if (rt->rt_ifp != NULL) {
99 
100 		/*
101 		 * Check route MTU:
102 		 * inherit interface MTU if not set or
103 		 * check if MTU is too large.
104 		 */
105 		if (rt->rt_mtu == 0) {
106 			rt->rt_mtu = rt->rt_ifp->if_mtu;
107 		} else if (rt->rt_mtu > rt->rt_ifp->if_mtu)
108 			rt->rt_mtu = rt->rt_ifp->if_mtu;
109 	}
110 
111 	return (rn_addroute(v_arg, n_arg, head, treenodes));
112 }
113 
114 static int _in_rt_was_here;
115 /*
116  * Initialize our routing tree.
117  */
118 int
119 in_inithead(void **head, int off)
120 {
121 	struct rib_head *rh;
122 
123 	rh = rt_table_init(32);
124 	if (rh == NULL)
125 		return (0);
126 
127 	rh->rnh_addaddr = in_addroute;
128 	*head = (void *)rh;
129 
130 	if (_in_rt_was_here == 0 ) {
131 		_in_rt_was_here = 1;
132 	}
133 	return 1;
134 }
135 
136 #ifdef VIMAGE
137 int
138 in_detachhead(void **head, int off)
139 {
140 
141 	rt_table_destroy((struct rib_head *)(*head));
142 	return (1);
143 }
144 #endif
145 
146 /*
147  * This zaps old routes when the interface goes down or interface
148  * address is deleted.  In the latter case, it deletes static routes
149  * that point to this address.  If we don't do this, we may end up
150  * using the old address in the future.  The ones we always want to
151  * get rid of are things like ARP entries, since the user might down
152  * the interface, walk over to a completely different network, and
153  * plug back in.
154  */
155 struct in_ifadown_arg {
156 	struct ifaddr *ifa;
157 	int del;
158 };
159 
160 static int
161 in_ifadownkill(const struct rtentry *rt, void *xap)
162 {
163 	struct in_ifadown_arg *ap = xap;
164 
165 	if (rt->rt_ifa != ap->ifa)
166 		return (0);
167 
168 	if ((rt->rt_flags & RTF_STATIC) != 0 && ap->del == 0)
169 		return (0);
170 
171 	return (1);
172 }
173 
174 void
175 in_ifadown(struct ifaddr *ifa, int delete)
176 {
177 	struct in_ifadown_arg arg;
178 
179 	KASSERT(ifa->ifa_addr->sa_family == AF_INET,
180 	    ("%s: wrong family", __func__));
181 
182 	arg.ifa = ifa;
183 	arg.del = delete;
184 
185 	rt_foreach_fib_walk_del(AF_INET, in_ifadownkill, &arg);
186 	ifa->ifa_flags &= ~IFA_ROUTE;		/* XXXlocking? */
187 }
188 
189 /*
190  * inet versions of rt functions. These have fib extensions and
191  * for now will just reference the _fib variants.
192  * eventually this order will be reversed,
193  */
194 void
195 in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum)
196 {
197 	rtalloc_ign_fib(ro, ignflags, fibnum);
198 }
199 
200 void
201 in_rtredirect(struct sockaddr *dst,
202 	struct sockaddr *gateway,
203 	struct sockaddr *netmask,
204 	int flags,
205 	struct sockaddr *src,
206 	u_int fibnum)
207 {
208 	rtredirect_fib(dst, gateway, netmask, flags, src, fibnum);
209 }
210 
211