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