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