route.c revision 1.8 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1980, 1986, 1991 Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd *
33 1.3 cgd * from: @(#)route.c 7.22 (Berkeley) 6/27/91
34 1.8 cgd * $Id: route.c,v 1.8 1994/03/23 05:05:03 cgd Exp $
35 1.1 cgd */
36 1.2 cgd
37 1.5 mycroft #include <sys/param.h>
38 1.5 mycroft #include <sys/systm.h>
39 1.5 mycroft #include <sys/proc.h>
40 1.5 mycroft #include <sys/mbuf.h>
41 1.5 mycroft #include <sys/socket.h>
42 1.5 mycroft #include <sys/socketvar.h>
43 1.5 mycroft #include <sys/domain.h>
44 1.5 mycroft #include <sys/protosw.h>
45 1.5 mycroft #include <sys/ioctl.h>
46 1.1 cgd
47 1.5 mycroft #include <net/if.h>
48 1.5 mycroft #include <net/route.h>
49 1.5 mycroft #include <net/raw_cb.h>
50 1.5 mycroft #include <net/netisr.h>
51 1.1 cgd
52 1.5 mycroft #include <netinet/in.h>
53 1.5 mycroft #include <netinet/in_var.h>
54 1.1 cgd
55 1.1 cgd #ifdef NS
56 1.5 mycroft #include <netns/ns.h>
57 1.1 cgd #endif
58 1.5 mycroft
59 1.6 cgd #include <machine/cpu.h>
60 1.1 cgd
61 1.1 cgd #define SA(p) ((struct sockaddr *)(p))
62 1.1 cgd
63 1.1 cgd int rttrash; /* routes not in table but not freed */
64 1.1 cgd struct sockaddr wildcard; /* zero valued cookie for wildcard searches */
65 1.1 cgd int rthashsize = RTHASHSIZ; /* for netstat, etc. */
66 1.1 cgd
67 1.1 cgd static int rtinits_done = 0;
68 1.1 cgd struct radix_node_head *ns_rnhead, *in_rnhead;
69 1.1 cgd struct radix_node *rn_match(), *rn_delete(), *rn_addroute();
70 1.1 cgd
71 1.1 cgd rtinitheads()
72 1.1 cgd {
73 1.1 cgd if (rtinits_done == 0 &&
74 1.1 cgd #ifdef NS
75 1.1 cgd rn_inithead(&ns_rnhead, 16, AF_NS) &&
76 1.1 cgd #endif
77 1.1 cgd rn_inithead(&in_rnhead, 32, AF_INET))
78 1.1 cgd rtinits_done = 1;
79 1.1 cgd }
80 1.1 cgd
81 1.1 cgd /*
82 1.1 cgd * Packet routing routines.
83 1.1 cgd */
84 1.1 cgd rtalloc(ro)
85 1.1 cgd register struct route *ro;
86 1.1 cgd {
87 1.1 cgd if (ro->ro_rt && ro->ro_rt->rt_ifp && (ro->ro_rt->rt_flags & RTF_UP))
88 1.1 cgd return; /* XXX */
89 1.1 cgd ro->ro_rt = rtalloc1(&ro->ro_dst, 1);
90 1.1 cgd }
91 1.1 cgd
92 1.1 cgd struct rtentry *
93 1.1 cgd rtalloc1(dst, report)
94 1.1 cgd register struct sockaddr *dst;
95 1.1 cgd int report;
96 1.1 cgd {
97 1.1 cgd register struct radix_node_head *rnh;
98 1.1 cgd register struct rtentry *rt;
99 1.1 cgd register struct radix_node *rn;
100 1.1 cgd struct rtentry *newrt = 0;
101 1.1 cgd int s = splnet(), err = 0, msgtype = RTM_MISS;
102 1.1 cgd
103 1.1 cgd for (rnh = radix_node_head; rnh && (dst->sa_family != rnh->rnh_af); )
104 1.1 cgd rnh = rnh->rnh_next;
105 1.1 cgd if (rnh && rnh->rnh_treetop &&
106 1.1 cgd (rn = rn_match((caddr_t)dst, rnh->rnh_treetop)) &&
107 1.1 cgd ((rn->rn_flags & RNF_ROOT) == 0)) {
108 1.1 cgd newrt = rt = (struct rtentry *)rn;
109 1.1 cgd if (report && (rt->rt_flags & RTF_CLONING)) {
110 1.8 cgd err = rtrequest(RTM_RESOLVE, dst, SA(0),
111 1.8 cgd SA(0), 0, &newrt);
112 1.8 cgd if (err) {
113 1.8 cgd newrt = rt;
114 1.8 cgd rt->rt_refcnt++;
115 1.8 cgd goto miss;
116 1.8 cgd }
117 1.8 cgd if ((rt = newrt) && (rt->rt_flags & RTF_XRESOLVE)) {
118 1.8 cgd msgtype = RTM_RESOLVE;
119 1.8 cgd goto miss;
120 1.8 cgd }
121 1.1 cgd } else
122 1.1 cgd rt->rt_refcnt++;
123 1.1 cgd } else {
124 1.1 cgd rtstat.rts_unreach++;
125 1.1 cgd miss: if (report)
126 1.1 cgd rt_missmsg(msgtype, dst, SA(0), SA(0), SA(0), 0, err);
127 1.1 cgd }
128 1.1 cgd splx(s);
129 1.1 cgd return (newrt);
130 1.1 cgd }
131 1.1 cgd
132 1.1 cgd rtfree(rt)
133 1.1 cgd register struct rtentry *rt;
134 1.1 cgd {
135 1.1 cgd register struct ifaddr *ifa;
136 1.1 cgd if (rt == 0)
137 1.1 cgd panic("rtfree");
138 1.1 cgd rt->rt_refcnt--;
139 1.1 cgd if (rt->rt_refcnt <= 0 && (rt->rt_flags & RTF_UP) == 0) {
140 1.1 cgd rttrash--;
141 1.1 cgd if (rt->rt_nodes->rn_flags & (RNF_ACTIVE | RNF_ROOT))
142 1.1 cgd panic ("rtfree 2");
143 1.1 cgd free((caddr_t)rt, M_RTABLE);
144 1.1 cgd }
145 1.1 cgd }
146 1.1 cgd
147 1.1 cgd /*
148 1.1 cgd * Force a routing table entry to the specified
149 1.1 cgd * destination to go through the given gateway.
150 1.1 cgd * Normally called as a result of a routing redirect
151 1.1 cgd * message from the network layer.
152 1.1 cgd *
153 1.1 cgd * N.B.: must be called at splnet
154 1.1 cgd *
155 1.1 cgd */
156 1.1 cgd rtredirect(dst, gateway, netmask, flags, src, rtp)
157 1.1 cgd struct sockaddr *dst, *gateway, *netmask, *src;
158 1.1 cgd int flags;
159 1.1 cgd struct rtentry **rtp;
160 1.1 cgd {
161 1.2 cgd register struct rtentry *rt = 0;
162 1.1 cgd int error = 0;
163 1.1 cgd short *stat = 0;
164 1.1 cgd
165 1.1 cgd /* verify the gateway is directly reachable */
166 1.1 cgd if (ifa_ifwithnet(gateway) == 0) {
167 1.1 cgd error = ENETUNREACH;
168 1.8 cgd goto out;
169 1.1 cgd }
170 1.1 cgd rt = rtalloc1(dst, 0);
171 1.1 cgd /*
172 1.1 cgd * If the redirect isn't from our current router for this dst,
173 1.1 cgd * it's either old or wrong. If it redirects us to ourselves,
174 1.1 cgd * we have a routing loop, perhaps as a result of an interface
175 1.1 cgd * going down recently.
176 1.1 cgd */
177 1.1 cgd #define equal(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), (a1)->sa_len) == 0)
178 1.1 cgd if (!(flags & RTF_DONE) && rt && !equal(src, rt->rt_gateway))
179 1.1 cgd error = EINVAL;
180 1.1 cgd else if (ifa_ifwithaddr(gateway))
181 1.1 cgd error = EHOSTUNREACH;
182 1.1 cgd if (error)
183 1.1 cgd goto done;
184 1.1 cgd /*
185 1.1 cgd * Create a new entry if we just got back a wildcard entry
186 1.1 cgd * or the the lookup failed. This is necessary for hosts
187 1.1 cgd * which use routing redirects generated by smart gateways
188 1.1 cgd * to dynamically build the routing tables.
189 1.1 cgd */
190 1.1 cgd if ((rt == 0) || (rt_mask(rt) && rt_mask(rt)->sa_len < 2))
191 1.1 cgd goto create;
192 1.1 cgd /*
193 1.1 cgd * Don't listen to the redirect if it's
194 1.1 cgd * for a route to an interface.
195 1.1 cgd */
196 1.1 cgd if (rt->rt_flags & RTF_GATEWAY) {
197 1.1 cgd if (((rt->rt_flags & RTF_HOST) == 0) && (flags & RTF_HOST)) {
198 1.1 cgd /*
199 1.1 cgd * Changing from route to net => route to host.
200 1.1 cgd * Create new route, rather than smashing route to net.
201 1.1 cgd */
202 1.1 cgd create:
203 1.1 cgd flags |= RTF_GATEWAY | RTF_DYNAMIC;
204 1.1 cgd error = rtrequest((int)RTM_ADD, dst, gateway,
205 1.1 cgd SA(0), flags,
206 1.1 cgd (struct rtentry **)0);
207 1.1 cgd stat = &rtstat.rts_dynamic;
208 1.1 cgd } else {
209 1.1 cgd /*
210 1.1 cgd * Smash the current notion of the gateway to
211 1.1 cgd * this destination. Should check about netmask!!!
212 1.1 cgd */
213 1.1 cgd if (gateway->sa_len <= rt->rt_gateway->sa_len) {
214 1.1 cgd Bcopy(gateway, rt->rt_gateway, gateway->sa_len);
215 1.1 cgd rt->rt_flags |= RTF_MODIFIED;
216 1.1 cgd flags |= RTF_MODIFIED;
217 1.1 cgd stat = &rtstat.rts_newgateway;
218 1.1 cgd } else
219 1.1 cgd error = ENOSPC;
220 1.1 cgd }
221 1.1 cgd } else
222 1.1 cgd error = EHOSTUNREACH;
223 1.1 cgd done:
224 1.1 cgd if (rt) {
225 1.1 cgd if (rtp && !error)
226 1.1 cgd *rtp = rt;
227 1.1 cgd else
228 1.1 cgd rtfree(rt);
229 1.1 cgd }
230 1.8 cgd out:
231 1.1 cgd if (error)
232 1.1 cgd rtstat.rts_badredirect++;
233 1.8 cgd else if (stat != NULL)
234 1.8 cgd (*stat)++;
235 1.1 cgd rt_missmsg(RTM_REDIRECT, dst, gateway, netmask, src, flags, error);
236 1.1 cgd }
237 1.1 cgd
238 1.1 cgd /*
239 1.1 cgd * Routing table ioctl interface.
240 1.1 cgd */
241 1.1 cgd rtioctl(req, data, p)
242 1.1 cgd int req;
243 1.1 cgd caddr_t data;
244 1.1 cgd struct proc *p;
245 1.1 cgd {
246 1.1 cgd #ifndef COMPAT_43
247 1.1 cgd return (EOPNOTSUPP);
248 1.1 cgd #else
249 1.1 cgd register struct ortentry *entry = (struct ortentry *)data;
250 1.1 cgd int error;
251 1.1 cgd struct sockaddr *netmask = 0;
252 1.1 cgd
253 1.1 cgd if (req == SIOCADDRT)
254 1.1 cgd req = RTM_ADD;
255 1.1 cgd else if (req == SIOCDELRT)
256 1.1 cgd req = RTM_DELETE;
257 1.1 cgd else
258 1.1 cgd return (EINVAL);
259 1.1 cgd
260 1.1 cgd if (error = suser(p->p_ucred, &p->p_acflag))
261 1.1 cgd return (error);
262 1.1 cgd #if BYTE_ORDER != BIG_ENDIAN
263 1.1 cgd if (entry->rt_dst.sa_family == 0 && entry->rt_dst.sa_len < 16) {
264 1.1 cgd entry->rt_dst.sa_family = entry->rt_dst.sa_len;
265 1.1 cgd entry->rt_dst.sa_len = 16;
266 1.1 cgd }
267 1.1 cgd if (entry->rt_gateway.sa_family == 0 && entry->rt_gateway.sa_len < 16) {
268 1.1 cgd entry->rt_gateway.sa_family = entry->rt_gateway.sa_len;
269 1.1 cgd entry->rt_gateway.sa_len = 16;
270 1.1 cgd }
271 1.1 cgd #else
272 1.1 cgd if (entry->rt_dst.sa_len == 0)
273 1.1 cgd entry->rt_dst.sa_len = 16;
274 1.1 cgd if (entry->rt_gateway.sa_len == 0)
275 1.1 cgd entry->rt_gateway.sa_len = 16;
276 1.1 cgd #endif
277 1.1 cgd if ((entry->rt_flags & RTF_HOST) == 0)
278 1.1 cgd switch (entry->rt_dst.sa_family) {
279 1.1 cgd #ifdef INET
280 1.1 cgd case AF_INET:
281 1.1 cgd {
282 1.1 cgd extern struct sockaddr_in icmpmask;
283 1.1 cgd struct sockaddr_in *dst_in =
284 1.1 cgd (struct sockaddr_in *)&entry->rt_dst;
285 1.1 cgd
286 1.1 cgd in_sockmaskof(dst_in->sin_addr, &icmpmask);
287 1.1 cgd netmask = (struct sockaddr *)&icmpmask;
288 1.1 cgd }
289 1.1 cgd break;
290 1.1 cgd #endif
291 1.1 cgd #ifdef NS
292 1.1 cgd case AF_NS:
293 1.1 cgd {
294 1.1 cgd extern struct sockaddr_ns ns_netmask;
295 1.1 cgd netmask = (struct sockaddr *)&ns_netmask;
296 1.1 cgd }
297 1.1 cgd #endif
298 1.1 cgd }
299 1.1 cgd error = rtrequest(req, &(entry->rt_dst), &(entry->rt_gateway), netmask,
300 1.1 cgd entry->rt_flags, (struct rtentry **)0);
301 1.1 cgd rt_missmsg((req == RTM_ADD ? RTM_OLDADD : RTM_OLDDEL),
302 1.1 cgd &(entry->rt_dst), &(entry->rt_gateway),
303 1.1 cgd netmask, SA(0), entry->rt_flags, error);
304 1.1 cgd return (error);
305 1.1 cgd #endif
306 1.1 cgd }
307 1.1 cgd
308 1.1 cgd struct ifaddr *
309 1.1 cgd ifa_ifwithroute(flags, dst, gateway)
310 1.1 cgd int flags;
311 1.1 cgd struct sockaddr *dst, *gateway;
312 1.1 cgd {
313 1.1 cgd register struct ifaddr *ifa;
314 1.1 cgd if ((flags & RTF_GATEWAY) == 0) {
315 1.1 cgd /*
316 1.1 cgd * If we are adding a route to an interface,
317 1.1 cgd * and the interface is a pt to pt link
318 1.1 cgd * we should search for the destination
319 1.1 cgd * as our clue to the interface. Otherwise
320 1.1 cgd * we can use the local address.
321 1.1 cgd */
322 1.1 cgd ifa = 0;
323 1.1 cgd if (flags & RTF_HOST)
324 1.1 cgd ifa = ifa_ifwithdstaddr(dst);
325 1.1 cgd if (ifa == 0)
326 1.1 cgd ifa = ifa_ifwithaddr(gateway);
327 1.1 cgd } else {
328 1.1 cgd /*
329 1.1 cgd * If we are adding a route to a remote net
330 1.1 cgd * or host, the gateway may still be on the
331 1.1 cgd * other end of a pt to pt link.
332 1.1 cgd */
333 1.1 cgd ifa = ifa_ifwithdstaddr(gateway);
334 1.1 cgd }
335 1.1 cgd if (ifa == 0)
336 1.1 cgd ifa = ifa_ifwithnet(gateway);
337 1.1 cgd if (ifa == 0) {
338 1.1 cgd struct rtentry *rt = rtalloc1(dst, 0);
339 1.1 cgd if (rt == 0)
340 1.1 cgd return (0);
341 1.1 cgd rt->rt_refcnt--;
342 1.1 cgd if ((ifa = rt->rt_ifa) == 0)
343 1.1 cgd return (0);
344 1.1 cgd }
345 1.1 cgd if (ifa->ifa_addr->sa_family != dst->sa_family) {
346 1.1 cgd struct ifaddr *oifa = ifa, *ifaof_ifpforaddr();
347 1.1 cgd ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
348 1.1 cgd if (ifa == 0)
349 1.1 cgd ifa = oifa;
350 1.1 cgd }
351 1.1 cgd return (ifa);
352 1.1 cgd }
353 1.1 cgd
354 1.1 cgd #define ROUNDUP(a) (a>0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
355 1.1 cgd
356 1.1 cgd rtrequest(req, dst, gateway, netmask, flags, ret_nrt)
357 1.1 cgd int req, flags;
358 1.1 cgd struct sockaddr *dst, *gateway, *netmask;
359 1.1 cgd struct rtentry **ret_nrt;
360 1.1 cgd {
361 1.1 cgd int s = splnet(), len, error = 0;
362 1.1 cgd register struct rtentry *rt;
363 1.1 cgd register struct radix_node *rn;
364 1.1 cgd register struct radix_node_head *rnh;
365 1.1 cgd struct ifaddr *ifa, *ifa_ifwithdstaddr();
366 1.1 cgd struct sockaddr *ndst;
367 1.1 cgd u_char af = dst->sa_family;
368 1.1 cgd #define senderr(x) { error = x ; goto bad; }
369 1.1 cgd
370 1.1 cgd if (rtinits_done == 0)
371 1.1 cgd rtinitheads();
372 1.1 cgd for (rnh = radix_node_head; rnh && (af != rnh->rnh_af); )
373 1.1 cgd rnh = rnh->rnh_next;
374 1.1 cgd if (rnh == 0)
375 1.1 cgd senderr(ESRCH);
376 1.1 cgd if (flags & RTF_HOST)
377 1.1 cgd netmask = 0;
378 1.1 cgd switch (req) {
379 1.1 cgd case RTM_DELETE:
380 1.1 cgd if (ret_nrt && (rt = *ret_nrt)) {
381 1.1 cgd RTFREE(rt);
382 1.1 cgd *ret_nrt = 0;
383 1.1 cgd }
384 1.1 cgd if ((rn = rn_delete((caddr_t)dst, (caddr_t)netmask,
385 1.1 cgd rnh->rnh_treetop)) == 0)
386 1.1 cgd senderr(ESRCH);
387 1.1 cgd if (rn->rn_flags & (RNF_ACTIVE | RNF_ROOT))
388 1.1 cgd panic ("rtrequest delete");
389 1.1 cgd rt = (struct rtentry *)rn;
390 1.1 cgd rt->rt_flags &= ~RTF_UP;
391 1.1 cgd if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
392 1.1 cgd ifa->ifa_rtrequest(RTM_DELETE, rt, SA(0));
393 1.1 cgd rttrash++;
394 1.1 cgd if (rt->rt_refcnt <= 0)
395 1.1 cgd rtfree(rt);
396 1.1 cgd break;
397 1.1 cgd
398 1.1 cgd case RTM_RESOLVE:
399 1.1 cgd if (ret_nrt == 0 || (rt = *ret_nrt) == 0)
400 1.1 cgd senderr(EINVAL);
401 1.1 cgd ifa = rt->rt_ifa;
402 1.1 cgd flags = rt->rt_flags & ~RTF_CLONING;
403 1.1 cgd gateway = rt->rt_gateway;
404 1.1 cgd if ((netmask = rt->rt_genmask) == 0)
405 1.1 cgd flags |= RTF_HOST;
406 1.1 cgd goto makeroute;
407 1.1 cgd
408 1.1 cgd case RTM_ADD:
409 1.1 cgd if ((ifa = ifa_ifwithroute(flags, dst, gateway)) == 0)
410 1.1 cgd senderr(ENETUNREACH);
411 1.1 cgd makeroute:
412 1.1 cgd len = sizeof (*rt) + ROUNDUP(gateway->sa_len)
413 1.1 cgd + ROUNDUP(dst->sa_len);
414 1.1 cgd R_Malloc(rt, struct rtentry *, len);
415 1.1 cgd if (rt == 0)
416 1.1 cgd senderr(ENOBUFS);
417 1.1 cgd Bzero(rt, len);
418 1.1 cgd ndst = (struct sockaddr *)(rt + 1);
419 1.1 cgd if (netmask) {
420 1.1 cgd rt_maskedcopy(dst, ndst, netmask);
421 1.1 cgd } else
422 1.1 cgd Bcopy(dst, ndst, dst->sa_len);
423 1.1 cgd rn = rn_addroute((caddr_t)ndst, (caddr_t)netmask,
424 1.1 cgd rnh->rnh_treetop, rt->rt_nodes);
425 1.1 cgd if (rn == 0) {
426 1.1 cgd free((caddr_t)rt, M_RTABLE);
427 1.1 cgd senderr(EEXIST);
428 1.1 cgd }
429 1.1 cgd rt->rt_ifa = ifa;
430 1.1 cgd rt->rt_ifp = ifa->ifa_ifp;
431 1.1 cgd rt->rt_flags = RTF_UP | flags;
432 1.1 cgd rt->rt_gateway = (struct sockaddr *)
433 1.1 cgd (rn->rn_key + ROUNDUP(dst->sa_len));
434 1.1 cgd Bcopy(gateway, rt->rt_gateway, gateway->sa_len);
435 1.1 cgd if (req == RTM_RESOLVE)
436 1.1 cgd rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
437 1.1 cgd if (ifa->ifa_rtrequest)
438 1.1 cgd ifa->ifa_rtrequest(req, rt, SA(ret_nrt ? *ret_nrt : 0));
439 1.1 cgd if (ret_nrt) {
440 1.1 cgd *ret_nrt = rt;
441 1.1 cgd rt->rt_refcnt++;
442 1.1 cgd }
443 1.1 cgd break;
444 1.1 cgd }
445 1.1 cgd bad:
446 1.1 cgd splx(s);
447 1.1 cgd return (error);
448 1.1 cgd }
449 1.1 cgd
450 1.1 cgd rt_maskedcopy(src, dst, netmask)
451 1.1 cgd struct sockaddr *src, *dst, *netmask;
452 1.1 cgd {
453 1.1 cgd register u_char *cp1 = (u_char *)src;
454 1.1 cgd register u_char *cp2 = (u_char *)dst;
455 1.1 cgd register u_char *cp3 = (u_char *)netmask;
456 1.1 cgd u_char *cplim = cp2 + *cp3;
457 1.1 cgd u_char *cplim2 = cp2 + *cp1;
458 1.1 cgd
459 1.1 cgd *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
460 1.1 cgd cp3 += 2;
461 1.1 cgd if (cplim > cplim2)
462 1.1 cgd cplim = cplim2;
463 1.1 cgd while (cp2 < cplim)
464 1.1 cgd *cp2++ = *cp1++ & *cp3++;
465 1.1 cgd if (cp2 < cplim2)
466 1.1 cgd bzero((caddr_t)cp2, (unsigned)(cplim2 - cp2));
467 1.1 cgd }
468 1.1 cgd /*
469 1.1 cgd * Set up a routing table entry, normally
470 1.1 cgd * for an interface.
471 1.1 cgd */
472 1.1 cgd rtinit(ifa, cmd, flags)
473 1.1 cgd register struct ifaddr *ifa;
474 1.1 cgd int cmd, flags;
475 1.1 cgd {
476 1.1 cgd register struct rtentry *rt;
477 1.1 cgd register struct sockaddr *dst;
478 1.1 cgd register struct sockaddr *deldst;
479 1.1 cgd struct mbuf *m = 0;
480 1.1 cgd int error;
481 1.1 cgd
482 1.1 cgd dst = flags & RTF_HOST ? ifa->ifa_dstaddr : ifa->ifa_addr;
483 1.1 cgd if (ifa->ifa_flags & IFA_ROUTE) {
484 1.1 cgd if ((rt = ifa->ifa_rt) && (rt->rt_flags & RTF_UP) == 0) {
485 1.1 cgd RTFREE(rt);
486 1.1 cgd ifa->ifa_rt = 0;
487 1.1 cgd }
488 1.1 cgd }
489 1.1 cgd if (cmd == RTM_DELETE) {
490 1.1 cgd if ((flags & RTF_HOST) == 0 && ifa->ifa_netmask) {
491 1.1 cgd m = m_get(M_WAIT, MT_SONAME);
492 1.1 cgd deldst = mtod(m, struct sockaddr *);
493 1.1 cgd rt_maskedcopy(dst, deldst, ifa->ifa_netmask);
494 1.1 cgd dst = deldst;
495 1.1 cgd }
496 1.1 cgd if (rt = rtalloc1(dst, 0)) {
497 1.1 cgd rt->rt_refcnt--;
498 1.1 cgd if (rt->rt_ifa != ifa) {
499 1.1 cgd if (m)
500 1.1 cgd (void) m_free(m);
501 1.1 cgd return (flags & RTF_HOST ? EHOSTUNREACH
502 1.1 cgd : ENETUNREACH);
503 1.1 cgd }
504 1.1 cgd }
505 1.1 cgd }
506 1.1 cgd error = rtrequest(cmd, dst, ifa->ifa_addr, ifa->ifa_netmask,
507 1.1 cgd flags | ifa->ifa_flags, &ifa->ifa_rt);
508 1.1 cgd if (m)
509 1.1 cgd (void) m_free(m);
510 1.1 cgd if (cmd == RTM_ADD && error == 0 && (rt = ifa->ifa_rt)
511 1.1 cgd && rt->rt_ifa != ifa) {
512 1.1 cgd rt->rt_ifa = ifa;
513 1.1 cgd rt->rt_ifp = ifa->ifa_ifp;
514 1.1 cgd }
515 1.1 cgd return (error);
516 1.1 cgd }
517