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