]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_rmx.c
Add initial testcases for bin/ls
[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 <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/vnet.h>
44
45 #include <netinet/in.h>
46 #include <netinet/in_var.h>
47 #include <netinet/ip.h>
48 #include <netinet/ip_icmp.h>
49 #include <netinet/ip_var.h>
50
51 extern int      in_inithead(void **head, int off);
52 #ifdef VIMAGE
53 extern int      in_detachhead(void **head, int off);
54 #endif
55
56 static void in_setifarnh(struct radix_node_head *rnh, uint32_t fibnum,
57     int af, void *_arg);
58
59 /*
60  * Do what we need to do when inserting a route.
61  */
62 static struct radix_node *
63 in_addroute(void *v_arg, void *n_arg, struct radix_node_head *head,
64     struct radix_node *treenodes)
65 {
66         struct rtentry *rt = (struct rtentry *)treenodes;
67         struct sockaddr_in *sin = (struct sockaddr_in *)rt_key(rt);
68
69         RADIX_NODE_HEAD_WLOCK_ASSERT(head);
70         /*
71          * A little bit of help for both IP output and input:
72          *   For host routes, we make sure that RTF_BROADCAST
73          *   is set for anything that looks like a broadcast address.
74          *   This way, we can avoid an expensive call to in_broadcast()
75          *   in ip_output() most of the time (because the route passed
76          *   to ip_output() is almost always a host route).
77          *
78          *   We also do the same for local addresses, with the thought
79          *   that this might one day be used to speed up ip_input().
80          *
81          * We also mark routes to multicast addresses as such, because
82          * it's easy to do and might be useful (but this is much more
83          * dubious since it's so easy to inspect the address).
84          */
85         if (rt->rt_flags & RTF_HOST) {
86                 if (in_broadcast(sin->sin_addr, rt->rt_ifp)) {
87                         rt->rt_flags |= RTF_BROADCAST;
88                 } else if (satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr ==
89                     sin->sin_addr.s_addr) {
90                         rt->rt_flags |= RTF_LOCAL;
91                 }
92         }
93         if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
94                 rt->rt_flags |= RTF_MULTICAST;
95
96         if (rt->rt_ifp != NULL) {
97
98                 /*
99                  * Check route MTU:
100                  * inherit interface MTU if not set or
101                  * check if MTU is too large.
102                  */
103                 if (rt->rt_mtu == 0) {
104                         rt->rt_mtu = rt->rt_ifp->if_mtu;
105                 } else if (rt->rt_mtu > rt->rt_ifp->if_mtu)
106                         rt->rt_mtu = rt->rt_ifp->if_mtu;
107         }
108
109         return (rn_addroute(v_arg, n_arg, head, treenodes));
110 }
111
112 static int _in_rt_was_here;
113 /*
114  * Initialize our routing tree.
115  */
116 int
117 in_inithead(void **head, int off)
118 {
119         struct radix_node_head *rnh;
120
121         if (!rn_inithead(head, 32))
122                 return 0;
123
124         rnh = *head;
125         RADIX_NODE_HEAD_LOCK_INIT(rnh);
126
127         rnh->rnh_addaddr = in_addroute;
128         if (_in_rt_was_here == 0 ) {
129                 _in_rt_was_here = 1;
130         }
131         return 1;
132 }
133
134 #ifdef VIMAGE
135 int
136 in_detachhead(void **head, int off)
137 {
138
139         return (1);
140 }
141 #endif
142
143 /*
144  * This zaps old routes when the interface goes down or interface
145  * address is deleted.  In the latter case, it deletes static routes
146  * that point to this address.  If we don't do this, we may end up
147  * using the old address in the future.  The ones we always want to
148  * get rid of are things like ARP entries, since the user might down
149  * the interface, walk over to a completely different network, and
150  * plug back in.
151  */
152 struct in_ifadown_arg {
153         struct radix_node_head *rnh;
154         struct ifaddr *ifa;
155         int del;
156 };
157
158 static int
159 in_ifadownkill(struct rtentry *rt, void *xap)
160 {
161         struct in_ifadown_arg *ap = xap;
162
163         RT_LOCK(rt);
164         if (rt->rt_ifa == ap->ifa &&
165             (ap->del || !(rt->rt_flags & RTF_STATIC))) {
166                 /*
167                  * Aquire a reference so that it can later be freed
168                  * as the refcount would be 0 here in case of at least
169                  * ap->del.
170                  */
171                 RT_ADDREF(rt);
172                 /*
173                  * Disconnect it from the tree and permit protocols
174                  * to cleanup.
175                  */
176                 rt_expunge(ap->rnh, rt);
177                 /*
178                  * At this point it is an rttrash node, and in case
179                  * the above is the only reference we must free it.
180                  * If we do not noone will have a pointer and the
181                  * rtentry will be leaked forever.
182                  * In case someone else holds a reference, we are
183                  * fine as we only decrement the refcount. In that
184                  * case if the other entity calls RT_REMREF, we
185                  * will still be leaking but at least we tried.
186                  */
187                 RTFREE_LOCKED(rt);
188                 return (0);
189         }
190         RT_UNLOCK(rt);
191         return 0;
192 }
193
194 static void
195 in_setifarnh(struct radix_node_head *rnh, uint32_t fibnum, int af,
196     void *_arg)
197 {
198         struct in_ifadown_arg *arg;
199
200         arg = (struct in_ifadown_arg *)_arg;
201
202         arg->rnh = rnh;
203 }
204
205 void
206 in_ifadown(struct ifaddr *ifa, int delete)
207 {
208         struct in_ifadown_arg arg;
209
210         KASSERT(ifa->ifa_addr->sa_family == AF_INET,
211             ("%s: wrong family", __func__));
212
213         arg.ifa = ifa;
214         arg.del = delete;
215
216         rt_foreach_fib_walk(AF_INET, in_setifarnh, in_ifadownkill, &arg);
217         ifa->ifa_flags &= ~IFA_ROUTE;           /* XXXlocking? */
218 }
219
220 /*
221  * inet versions of rt functions. These have fib extensions and 
222  * for now will just reference the _fib variants.
223  * eventually this order will be reversed,
224  */
225 void
226 in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum)
227 {
228         rtalloc_ign_fib(ro, ignflags, fibnum);
229 }
230
231 int
232 in_rtrequest( int req,
233         struct sockaddr *dst,
234         struct sockaddr *gateway,
235         struct sockaddr *netmask,
236         int flags,
237         struct rtentry **ret_nrt,
238         u_int fibnum)
239 {
240         return (rtrequest_fib(req, dst, gateway, netmask, 
241             flags, ret_nrt, fibnum));
242 }
243
244 struct rtentry *
245 in_rtalloc1(struct sockaddr *dst, int report, u_long ignflags, u_int fibnum)
246 {
247         return (rtalloc1_fib(dst, report, ignflags, fibnum));
248 }
249
250 void
251 in_rtredirect(struct sockaddr *dst,
252         struct sockaddr *gateway,
253         struct sockaddr *netmask,
254         int flags,
255         struct sockaddr *src,
256         u_int fibnum)
257 {
258         rtredirect_fib(dst, gateway, netmask, flags, src, fibnum);
259 }
260  
261 void
262 in_rtalloc(struct route *ro, u_int fibnum)
263 {
264         rtalloc_ign_fib(ro, 0UL, fibnum);
265 }
266
267 #if 0
268 int      in_rt_getifa(struct rt_addrinfo *, u_int fibnum);
269 int      in_rtioctl(u_long, caddr_t, u_int);
270 int      in_rtrequest1(int, struct rt_addrinfo *, struct rtentry **, u_int);
271 #endif
272
273