route: Enhance panic message
[dragonfly.git] / sys / net / route.c
CommitLineData
f3ed2586
JH
1/*
2 * Copyright (c) 2004, 2005 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Jeffrey M. Hsu.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The DragonFly Project nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific, prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
984263bc
MD
33/*
34 * Copyright (c) 1980, 1986, 1991, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
dc71b7ab 45 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)route.c 8.3 (Berkeley) 1/9/95
62 * $FreeBSD: src/sys/net/route.c,v 1.59.2.10 2003/01/17 08:04:00 ru Exp $
63 */
64
65#include "opt_inet.h"
9b42cabe 66#include "opt_mpls.h"
984263bc
MD
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/malloc.h>
71#include <sys/mbuf.h>
72#include <sys/socket.h>
73#include <sys/domain.h>
74#include <sys/kernel.h>
72ce0f6d
HP
75#include <sys/sysctl.h>
76#include <sys/globaldata.h>
77#include <sys/thread.h>
984263bc
MD
78
79#include <net/if.h>
80#include <net/route.h>
ecdefdda 81#include <net/netisr.h>
984263bc
MD
82
83#include <netinet/in.h>
1f2de5d4 84#include <net/ip_mroute/ip_mroute.h>
984263bc 85
4599cf19
MD
86#include <sys/thread2.h>
87#include <sys/msgport2.h>
88#include <net/netmsg2.h>
9763404f 89#include <net/netisr2.h>
4599cf19 90
9b42cabe
NA
91#ifdef MPLS
92#include <netproto/mpls/mpls.h>
93#endif
94
72ce0f6d 95static struct rtstatistics rtstatistics_percpu[MAXCPU];
72ce0f6d 96#define rtstat rtstatistics_percpu[mycpuid]
72ce0f6d 97
ecdefdda 98struct radix_node_head *rt_tables[MAXCPU][AF_MAX+1];
984263bc 99
f23061d4
JH
100static void rt_maskedcopy (struct sockaddr *, struct sockaddr *,
101 struct sockaddr *);
ecdefdda 102static void rtable_init(void);
ecdefdda
MD
103static void rtinit_rtrequest_callback(int, int, struct rt_addrinfo *,
104 struct rtentry *, void *);
105
002c1265
MD
106static void rtredirect_msghandler(netmsg_t msg);
107static void rtrequest1_msghandler(netmsg_t msg);
002c1265
MD
108static void rtsearch_msghandler(netmsg_t msg);
109static void rtmask_add_msghandler(netmsg_t msg);
ac0f13c5 110
9b42cabe
NA
111static int rt_setshims(struct rtentry *, struct sockaddr **);
112
72ce0f6d
HP
113SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RW, 0, "Routing");
114
69cb4182
MD
115#ifdef ROUTE_DEBUG
116static int route_debug = 1;
117SYSCTL_INT(_net_route, OID_AUTO, route_debug, CTLFLAG_RW,
118 &route_debug, 0, "");
119#endif
120
3031a6b6 121int route_assert_owner_access = 1;
facaabe1
SZ
122SYSCTL_INT(_net_route, OID_AUTO, assert_owner_access, CTLFLAG_RW,
123 &route_assert_owner_access, 0, "");
3eef1c4e 124
bb58b775
SZ
125u_long route_kmalloc_limit = 0;
126TUNABLE_ULONG("net.route.kmalloc_limit", &route_kmalloc_limit);
127
ecdefdda
MD
128/*
129 * Initialize the route table(s) for protocol domains and
130 * create a helper thread which will be responsible for updating
131 * route table entries on each cpu.
132 */
133void
134route_init(void)
135{
a4d91f46 136 int cpu;
ecdefdda
MD
137
138 for (cpu = 0; cpu < ncpus; ++cpu)
139 bzero(&rtstatistics_percpu[cpu], sizeof(struct rtstatistics));
140 rn_init(); /* initialize all zeroes, all ones, mask table */
a4d91f46
SZ
141 rtable_init(); /* call dom_rtattach() on each cpu */
142
bb58b775
SZ
143 if (route_kmalloc_limit)
144 kmalloc_raise_limit(M_RTABLE, route_kmalloc_limit);
ecdefdda
MD
145}
146
984263bc 147static void
002c1265 148rtable_init_oncpu(netmsg_t msg)
984263bc
MD
149{
150 struct domain *dom;
d564b91a 151 int nextcpu = mycpuid + 1;
2e9572df 152
ecdefdda
MD
153 SLIST_FOREACH(dom, &domains, dom_next) {
154 if (dom->dom_rtattach) {
155 dom->dom_rtattach(
d564b91a 156 (void **)&rt_tables[mycpuid][dom->dom_family],
ecdefdda
MD
157 dom->dom_rtoffset);
158 }
159 }
d564b91a
NA
160 if (nextcpu < ncpus)
161 lwkt_forwardmsg(netisr_cpuport(nextcpu), &msg->lmsg);
162 else
163 lwkt_replymsg(&msg->lmsg, 0);
a4d91f46
SZ
164}
165
166static void
167rtable_init(void)
168{
002c1265 169 struct netmsg_base msg;
a4d91f46 170
002c1265 171 netmsg_init(&msg, NULL, &curthread->td_msgport, 0, rtable_init_oncpu);
d564b91a 172 rt_domsg_global(&msg);
984263bc
MD
173}
174
72ce0f6d
HP
175/*
176 * Routing statistics.
177 */
72ce0f6d
HP
178static int
179sysctl_rtstatistics(SYSCTL_HANDLER_ARGS)
180{
181 int cpu, error = 0;
182
183 for (cpu = 0; cpu < ncpus; ++cpu) {
184 if ((error = SYSCTL_OUT(req, &rtstatistics_percpu[cpu],
185 sizeof(struct rtstatistics))))
186 break;
187 if ((error = SYSCTL_IN(req, &rtstatistics_percpu[cpu],
188 sizeof(struct rtstatistics))))
189 break;
190 }
191
192 return (error);
193}
194SYSCTL_PROC(_net_route, OID_AUTO, stats, (CTLTYPE_OPAQUE|CTLFLAG_RW),
195 0, 0, sysctl_rtstatistics, "S,rtstatistics", "Routing statistics");
72ce0f6d 196
984263bc
MD
197/*
198 * Packet routing routines.
199 */
f23061d4
JH
200
201/*
f3ed2586
JH
202 * Look up and fill in the "ro_rt" rtentry field in a route structure given
203 * an address in the "ro_dst" field. Always send a report on a miss and
204 * always clone routes.
f23061d4 205 */
984263bc 206void
2e9572df 207rtalloc(struct route *ro)
984263bc
MD
208{
209 rtalloc_ign(ro, 0UL);
210}
211
5fe66e68 212/*
f3ed2586
JH
213 * Look up and fill in the "ro_rt" rtentry field in a route structure given
214 * an address in the "ro_dst" field. Always send a report on a miss and
215 * optionally clone routes when RTF_CLONING or RTF_PRCLONING are not being
216 * ignored.
5fe66e68 217 */
984263bc 218void
f3ed2586 219rtalloc_ign(struct route *ro, u_long ignoreflags)
984263bc 220{
f23061d4
JH
221 if (ro->ro_rt != NULL) {
222 if (ro->ro_rt->rt_ifp != NULL && ro->ro_rt->rt_flags & RTF_UP)
984263bc 223 return;
f23061d4 224 rtfree(ro->ro_rt);
984263bc 225 ro->ro_rt = NULL;
984263bc 226 }
f3ed2586 227 ro->ro_rt = _rtlookup(&ro->ro_dst, RTL_REPORTMSG, ignoreflags);
984263bc
MD
228}
229
230/*
5fe66e68 231 * Look up the route that matches the given "dst" address.
f23061d4 232 *
f3ed2586 233 * Route lookup can have the side-effect of creating and returning
6554f2c4 234 * a cloned route instead when "dst" matches a cloning route and the
f3ed2586 235 * RTF_CLONING and RTF_PRCLONING flags are not being ignored.
f23061d4 236 *
f3ed2586 237 * Any route returned has its reference count incremented.
984263bc
MD
238 */
239struct rtentry *
6554f2c4 240_rtlookup(struct sockaddr *dst, boolean_t generate_report, u_long ignore)
984263bc 241{
ecdefdda 242 struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family];
f3ed2586 243 struct rtentry *rt;
984263bc 244
f3ed2586
JH
245 if (rnh == NULL)
246 goto unreach;
247
248 /*
249 * Look up route in the radix tree.
250 */
590b8cd4
JH
251 rt = (struct rtentry *) rnh->rnh_matchaddr((char *)dst, rnh);
252 if (rt == NULL)
f3ed2586 253 goto unreach;
f3ed2586
JH
254
255 /*
256 * Handle cloning routes.
257 */
258 if ((rt->rt_flags & ~ignore & (RTF_CLONING | RTF_PRCLONING)) != 0) {
259 struct rtentry *clonedroute;
260 int error;
261
262 clonedroute = rt; /* copy in/copy out parameter */
263 error = rtrequest(RTM_RESOLVE, dst, NULL, NULL, 0,
264 &clonedroute); /* clone the route */
265 if (error != 0) { /* cloning failed */
6554f2c4 266 if (generate_report)
f3ed2586
JH
267 rt_dstmsg(RTM_MISS, dst, error);
268 rt->rt_refcnt++;
269 return (rt); /* return the uncloned route */
270 }
6554f2c4 271 if (generate_report) {
f3ed2586
JH
272 if (clonedroute->rt_flags & RTF_XRESOLVE)
273 rt_dstmsg(RTM_RESOLVE, dst, 0);
274 else
275 rt_rtmsg(RTM_ADD, clonedroute,
276 clonedroute->rt_ifp, 0);
984263bc 277 }
f3ed2586 278 return (clonedroute); /* return cloned route */
984263bc 279 }
f3ed2586
JH
280
281 /*
282 * Increment the reference count of the matched route and return.
283 */
284 rt->rt_refcnt++;
f23061d4 285 return (rt);
f3ed2586
JH
286
287unreach:
288 rtstat.rts_unreach++;
6554f2c4 289 if (generate_report)
f3ed2586
JH
290 rt_dstmsg(RTM_MISS, dst, 0);
291 return (NULL);
984263bc
MD
292}
293
984263bc 294void
2e9572df 295rtfree(struct rtentry *rt)
984263bc 296{
3eef1c4e
SZ
297 if (rt->rt_cpuid == mycpuid)
298 rtfree_oncpu(rt);
299 else
3031a6b6 300 rtfree_remote(rt);
3eef1c4e
SZ
301}
302
303void
304rtfree_oncpu(struct rtentry *rt)
305{
306 KKASSERT(rt->rt_cpuid == mycpuid);
0bf6c14c 307 KASSERT(rt->rt_refcnt > 0, ("rtfree: rt_refcnt %ld", rt->rt_refcnt));
984263bc 308
f23061d4 309 --rt->rt_refcnt;
0bf6c14c 310 if (rt->rt_refcnt == 0) {
ecdefdda
MD
311 struct radix_node_head *rnh =
312 rt_tables[mycpuid][rt_key(rt)->sa_family];
0bf6c14c
JH
313
314 if (rnh->rnh_close)
315 rnh->rnh_close((struct radix_node *)rt, rnh);
316 if (!(rt->rt_flags & RTF_UP)) {
317 /* deallocate route */
318 if (rt->rt_ifa != NULL)
319 IFAFREE(rt->rt_ifa);
320 if (rt->rt_parent != NULL)
321 RTFREE(rt->rt_parent); /* recursive call! */
322 Free(rt_key(rt));
323 Free(rt);
324 }
984263bc
MD
325 }
326}
327
3eef1c4e 328static void
002c1265 329rtfree_remote_dispatch(netmsg_t msg)
3eef1c4e 330{
002c1265 331 struct lwkt_msg *lmsg = &msg->lmsg;
3eef1c4e
SZ
332 struct rtentry *rt = lmsg->u.ms_resultp;
333
334 rtfree_oncpu(rt);
335 lwkt_replymsg(lmsg, 0);
336}
337
338void
3031a6b6 339rtfree_remote(struct rtentry *rt)
3eef1c4e 340{
9763404f 341 struct netmsg_base *msg;
3eef1c4e
SZ
342 struct lwkt_msg *lmsg;
343
344 KKASSERT(rt->rt_cpuid != mycpuid);
345
3031a6b6 346 if (route_assert_owner_access) {
ed20d0e3 347 panic("rt remote free rt_cpuid %d, mycpuid %d",
3eef1c4e
SZ
348 rt->rt_cpuid, mycpuid);
349 } else {
350 kprintf("rt remote free rt_cpuid %d, mycpuid %d\n",
351 rt->rt_cpuid, mycpuid);
7ce2998e 352 print_backtrace(-1);
3eef1c4e
SZ
353 }
354
9763404f
SZ
355 msg = kmalloc(sizeof(*msg), M_LWKTMSG, M_INTWAIT);
356 netmsg_init(msg, NULL, &netisr_afree_rport, 0, rtfree_remote_dispatch);
357 lmsg = &msg->lmsg;
3eef1c4e
SZ
358 lmsg->u.ms_resultp = rt;
359
9763404f 360 lwkt_sendmsg(netisr_cpuport(rt->rt_cpuid), lmsg);
3eef1c4e
SZ
361}
362
130b7902 363int
ecdefdda
MD
364rtredirect_oncpu(struct sockaddr *dst, struct sockaddr *gateway,
365 struct sockaddr *netmask, int flags, struct sockaddr *src)
984263bc 366{
6554f2c4
JH
367 struct rtentry *rt = NULL;
368 struct rt_addrinfo rtinfo;
984263bc 369 struct ifaddr *ifa;
72ce0f6d 370 u_long *stat = NULL;
f23061d4 371 int error;
984263bc
MD
372
373 /* verify the gateway is directly reachable */
2e9572df 374 if ((ifa = ifa_ifwithnet(gateway)) == NULL) {
984263bc
MD
375 error = ENETUNREACH;
376 goto out;
377 }
f23061d4 378
984263bc 379 /*
6554f2c4
JH
380 * If the redirect isn't from our current router for this destination,
381 * it's either old or wrong.
984263bc 382 */
6554f2c4 383 if (!(flags & RTF_DONE) && /* XXX JH */
f3ed2586 384 (rt = rtpurelookup(dst)) != NULL &&
f23061d4 385 (!sa_equal(src, rt->rt_gateway) || rt->rt_ifa != ifa)) {
984263bc 386 error = EINVAL;
f23061d4 387 goto done;
6554f2c4
JH
388 }
389
390 /*
391 * If it redirects us to ourselves, we have a routing loop,
392 * perhaps as a result of an interface going down recently.
393 */
394 if (ifa_ifwithaddr(gateway)) {
984263bc 395 error = EHOSTUNREACH;
984263bc 396 goto done;
f23061d4
JH
397 }
398
984263bc 399 /*
6554f2c4
JH
400 * Create a new entry if the lookup failed or if we got back
401 * a wildcard entry for the default route. This is necessary
402 * for hosts which use routing redirects generated by smart
403 * gateways to dynamically build the routing tables.
984263bc 404 */
6554f2c4
JH
405 if (rt == NULL)
406 goto create;
407 if ((rt_mask(rt) != NULL && rt_mask(rt)->sa_len < 2)) {
408 rtfree(rt);
984263bc 409 goto create;
6554f2c4 410 }
f23061d4 411
6554f2c4
JH
412 /* Ignore redirects for directly connected hosts. */
413 if (!(rt->rt_flags & RTF_GATEWAY)) {
414 error = EHOSTUNREACH;
415 goto done;
416 }
417
418 if (!(rt->rt_flags & RTF_HOST) && (flags & RTF_HOST)) {
419 /*
420 * Changing from a network route to a host route.
421 * Create a new host route rather than smashing the
422 * network route.
423 */
f23061d4 424create:
6554f2c4
JH
425 flags |= RTF_GATEWAY | RTF_DYNAMIC;
426 bzero(&rtinfo, sizeof(struct rt_addrinfo));
427 rtinfo.rti_info[RTAX_DST] = dst;
428 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
429 rtinfo.rti_info[RTAX_NETMASK] = netmask;
430 rtinfo.rti_flags = flags;
431 rtinfo.rti_ifa = ifa;
432 rt = NULL; /* copy-in/copy-out parameter */
433 error = rtrequest1(RTM_ADD, &rtinfo, &rt);
434 if (rt != NULL)
435 flags = rt->rt_flags;
436 stat = &rtstat.rts_dynamic;
f23061d4 437 } else {
6554f2c4
JH
438 /*
439 * Smash the current notion of the gateway to this destination.
440 * Should check about netmask!!!
441 */
442 rt->rt_flags |= RTF_MODIFIED;
443 flags |= RTF_MODIFIED;
afe44b68
SZ
444
445 /* We only need to report rtmsg on CPU0 */
446 rt_setgate(rt, rt_key(rt), gateway,
447 mycpuid == 0 ? RTL_REPORTMSG : RTL_DONTREPORT);
6554f2c4
JH
448 error = 0;
449 stat = &rtstat.rts_newgateway;
f23061d4
JH
450 }
451
984263bc 452done:
f6870fe3
JH
453 if (rt != NULL)
454 rtfree(rt);
984263bc 455out:
f23061d4 456 if (error != 0)
984263bc
MD
457 rtstat.rts_badredirect++;
458 else if (stat != NULL)
459 (*stat)++;
f23061d4 460
ecdefdda
MD
461 return error;
462}
463
ecdefdda 464struct netmsg_rtredirect {
002c1265 465 struct netmsg_base base;
ecdefdda
MD
466 struct sockaddr *dst;
467 struct sockaddr *gateway;
468 struct sockaddr *netmask;
469 int flags;
470 struct sockaddr *src;
471};
472
ecdefdda
MD
473/*
474 * Force a routing table entry to the specified
475 * destination to go through the given gateway.
476 * Normally called as a result of a routing redirect
477 * message from the network layer.
478 *
479 * N.B.: must be called at splnet
480 */
481void
482rtredirect(struct sockaddr *dst, struct sockaddr *gateway,
483 struct sockaddr *netmask, int flags, struct sockaddr *src)
484{
485 struct rt_addrinfo rtinfo;
486 int error;
ecdefdda
MD
487 struct netmsg_rtredirect msg;
488
002c1265 489 netmsg_init(&msg.base, NULL, &curthread->td_msgport,
48e7b118 490 0, rtredirect_msghandler);
ecdefdda
MD
491 msg.dst = dst;
492 msg.gateway = gateway;
493 msg.netmask = netmask;
494 msg.flags = flags;
495 msg.src = src;
2c4bf548 496 error = rt_domsg_global(&msg.base);
6554f2c4
JH
497 bzero(&rtinfo, sizeof(struct rt_addrinfo));
498 rtinfo.rti_info[RTAX_DST] = dst;
499 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
500 rtinfo.rti_info[RTAX_NETMASK] = netmask;
501 rtinfo.rti_info[RTAX_AUTHOR] = src;
502 rt_missmsg(RTM_REDIRECT, &rtinfo, flags, error);
984263bc
MD
503}
504
4599cf19 505static void
002c1265 506rtredirect_msghandler(netmsg_t msg)
ecdefdda 507{
002c1265 508 struct netmsg_rtredirect *rmsg = (void *)msg;
ecdefdda
MD
509 int nextcpu;
510
002c1265
MD
511 rtredirect_oncpu(rmsg->dst, rmsg->gateway, rmsg->netmask,
512 rmsg->flags, rmsg->src);
ecdefdda
MD
513 nextcpu = mycpuid + 1;
514 if (nextcpu < ncpus)
9763404f 515 lwkt_forwardmsg(netisr_cpuport(nextcpu), &msg->lmsg);
ecdefdda 516 else
002c1265 517 lwkt_replymsg(&msg->lmsg, 0);
ecdefdda
MD
518}
519
984263bc
MD
520/*
521* Routing table ioctl interface.
522*/
523int
87de5057 524rtioctl(u_long req, caddr_t data, struct ucred *cred)
984263bc
MD
525{
526#ifdef INET
527 /* Multicast goop, grrr... */
528 return mrt_ioctl ? mrt_ioctl(req, data) : EOPNOTSUPP;
2e9572df 529#else
984263bc 530 return ENXIO;
2e9572df 531#endif
984263bc
MD
532}
533
534struct ifaddr *
2e9572df 535ifa_ifwithroute(int flags, struct sockaddr *dst, struct sockaddr *gateway)
984263bc 536{
82ed7fc2 537 struct ifaddr *ifa;
2e9572df
JH
538
539 if (!(flags & RTF_GATEWAY)) {
984263bc
MD
540 /*
541 * If we are adding a route to an interface,
f23061d4 542 * and the interface is a point-to-point link,
984263bc
MD
543 * we should search for the destination
544 * as our clue to the interface. Otherwise
545 * we can use the local address.
546 */
2e9572df 547 ifa = NULL;
984263bc
MD
548 if (flags & RTF_HOST) {
549 ifa = ifa_ifwithdstaddr(dst);
550 }
2e9572df 551 if (ifa == NULL)
984263bc
MD
552 ifa = ifa_ifwithaddr(gateway);
553 } else {
554 /*
555 * If we are adding a route to a remote net
556 * or host, the gateway may still be on the
557 * other end of a pt to pt link.
558 */
559 ifa = ifa_ifwithdstaddr(gateway);
560 }
2e9572df 561 if (ifa == NULL)
984263bc 562 ifa = ifa_ifwithnet(gateway);
2e9572df 563 if (ifa == NULL) {
590b8cd4 564 struct rtentry *rt;
f23061d4 565
590b8cd4 566 rt = rtpurelookup(gateway);
2e9572df
JH
567 if (rt == NULL)
568 return (NULL);
984263bc 569 rt->rt_refcnt--;
2e9572df
JH
570 if ((ifa = rt->rt_ifa) == NULL)
571 return (NULL);
984263bc
MD
572 }
573 if (ifa->ifa_addr->sa_family != dst->sa_family) {
6554f2c4 574 struct ifaddr *oldifa = ifa;
f23061d4 575
984263bc 576 ifa = ifaof_ifpforaddr(dst, ifa->ifa_ifp);
2e9572df 577 if (ifa == NULL)
6554f2c4 578 ifa = oldifa;
984263bc
MD
579 }
580 return (ifa);
581}
582
158abb01
RG
583static int rt_fixdelete (struct radix_node *, void *);
584static int rt_fixchange (struct radix_node *, void *);
984263bc
MD
585
586struct rtfc_arg {
587 struct rtentry *rt0;
588 struct radix_node_head *rnh;
589};
590
590b8cd4
JH
591/*
592 * Set rtinfo->rti_ifa and rtinfo->rti_ifp.
593 */
984263bc 594int
590b8cd4 595rt_getifa(struct rt_addrinfo *rtinfo)
984263bc 596{
590b8cd4
JH
597 struct sockaddr *gateway = rtinfo->rti_info[RTAX_GATEWAY];
598 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
599 struct sockaddr *ifaaddr = rtinfo->rti_info[RTAX_IFA];
600 int flags = rtinfo->rti_flags;
984263bc
MD
601
602 /*
603 * ifp may be specified by sockaddr_dl
604 * when protocol address is ambiguous.
605 */
590b8cd4
JH
606 if (rtinfo->rti_ifp == NULL) {
607 struct sockaddr *ifpaddr;
608
609 ifpaddr = rtinfo->rti_info[RTAX_IFP];
610 if (ifpaddr != NULL && ifpaddr->sa_family == AF_LINK) {
611 struct ifaddr *ifa;
612
613 ifa = ifa_ifwithnet(ifpaddr);
614 if (ifa != NULL)
615 rtinfo->rti_ifp = ifa->ifa_ifp;
616 }
617 }
618
619 if (rtinfo->rti_ifa == NULL && ifaaddr != NULL)
620 rtinfo->rti_ifa = ifa_ifwithaddr(ifaaddr);
621 if (rtinfo->rti_ifa == NULL) {
984263bc
MD
622 struct sockaddr *sa;
623
624 sa = ifaaddr != NULL ? ifaaddr :
625 (gateway != NULL ? gateway : dst);
590b8cd4
JH
626 if (sa != NULL && rtinfo->rti_ifp != NULL)
627 rtinfo->rti_ifa = ifaof_ifpforaddr(sa, rtinfo->rti_ifp);
984263bc 628 else if (dst != NULL && gateway != NULL)
590b8cd4 629 rtinfo->rti_ifa = ifa_ifwithroute(flags, dst, gateway);
984263bc 630 else if (sa != NULL)
590b8cd4 631 rtinfo->rti_ifa = ifa_ifwithroute(flags, sa, sa);
984263bc 632 }
590b8cd4
JH
633 if (rtinfo->rti_ifa == NULL)
634 return (ENETUNREACH);
635
636 if (rtinfo->rti_ifp == NULL)
637 rtinfo->rti_ifp = rtinfo->rti_ifa->ifa_ifp;
638 return (0);
984263bc
MD
639}
640
f23061d4
JH
641/*
642 * Do appropriate manipulations of a routing tree given
643 * all the bits of info needed
644 */
645int
646rtrequest(
647 int req,
648 struct sockaddr *dst,
649 struct sockaddr *gateway,
650 struct sockaddr *netmask,
651 int flags,
652 struct rtentry **ret_nrt)
653{
6554f2c4
JH
654 struct rt_addrinfo rtinfo;
655
656 bzero(&rtinfo, sizeof(struct rt_addrinfo));
657 rtinfo.rti_info[RTAX_DST] = dst;
658 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
659 rtinfo.rti_info[RTAX_NETMASK] = netmask;
660 rtinfo.rti_flags = flags;
661 return rtrequest1(req, &rtinfo, ret_nrt);
f23061d4
JH
662}
663
ecdefdda
MD
664int
665rtrequest_global(
666 int req,
667 struct sockaddr *dst,
668 struct sockaddr *gateway,
669 struct sockaddr *netmask,
670 int flags)
671{
672 struct rt_addrinfo rtinfo;
673
674 bzero(&rtinfo, sizeof(struct rt_addrinfo));
675 rtinfo.rti_info[RTAX_DST] = dst;
676 rtinfo.rti_info[RTAX_GATEWAY] = gateway;
677 rtinfo.rti_info[RTAX_NETMASK] = netmask;
678 rtinfo.rti_flags = flags;
04221313 679 return rtrequest1_global(req, &rtinfo, NULL, NULL, RTREQ_PRIO_NORM);
ecdefdda
MD
680}
681
ecdefdda 682struct netmsg_rtq {
002c1265 683 struct netmsg_base base;
ecdefdda
MD
684 int req;
685 struct rt_addrinfo *rtinfo;
686 rtrequest1_callback_func_t callback;
687 void *arg;
688};
689
ecdefdda 690int
04221313
SZ
691rtrequest1_global(int req, struct rt_addrinfo *rtinfo,
692 rtrequest1_callback_func_t callback, void *arg, boolean_t req_prio)
ecdefdda 693{
04221313 694 int error, flags = 0;
ecdefdda
MD
695 struct netmsg_rtq msg;
696
04221313
SZ
697 if (req_prio)
698 flags = MSGF_PRIORITY;
699 netmsg_init(&msg.base, NULL, &curthread->td_msgport, flags,
700 rtrequest1_msghandler);
002c1265 701 msg.base.lmsg.ms_error = -1;
ecdefdda
MD
702 msg.req = req;
703 msg.rtinfo = rtinfo;
704 msg.callback = callback;
705 msg.arg = arg;
2c4bf548 706 error = rt_domsg_global(&msg.base);
ecdefdda
MD
707 return (error);
708}
709
710/*
711 * Handle a route table request on the current cpu. Since the route table's
712 * are supposed to be identical on each cpu, an error occuring later in the
713 * message chain is considered system-fatal.
714 */
4599cf19 715static void
002c1265 716rtrequest1_msghandler(netmsg_t msg)
ecdefdda 717{
002c1265 718 struct netmsg_rtq *rmsg = (void *)msg;
6e6b3e50 719 struct rt_addrinfo rtinfo;
ecdefdda
MD
720 struct rtentry *rt = NULL;
721 int nextcpu;
722 int error;
723
6e6b3e50
SZ
724 /*
725 * Copy the rtinfo. We need to make sure that the original
726 * rtinfo, which is setup by the caller, in the netmsg will
727 * _not_ be changed; else the next CPU on the netmsg forwarding
728 * path will see a different rtinfo than what this CPU has seen.
729 */
002c1265 730 rtinfo = *rmsg->rtinfo;
6e6b3e50 731
002c1265 732 error = rtrequest1(rmsg->req, &rtinfo, &rt);
ecdefdda
MD
733 if (rt)
734 --rt->rt_refcnt;
002c1265
MD
735 if (rmsg->callback)
736 rmsg->callback(rmsg->req, error, &rtinfo, rt, rmsg->arg);
ecdefdda
MD
737
738 /*
739 * RTM_DELETE's are propogated even if an error occurs, since a
740 * cloned route might be undergoing deletion and cloned routes
741 * are not necessarily replicated. An overall error is returned
742 * only if no cpus have the route in question.
743 */
002c1265
MD
744 if (rmsg->base.lmsg.ms_error < 0 || error == 0)
745 rmsg->base.lmsg.ms_error = error;
ecdefdda
MD
746
747 nextcpu = mycpuid + 1;
002c1265 748 if (error && rmsg->req != RTM_DELETE) {
ecdefdda 749 if (mycpuid != 0) {
04362c97
SZ
750 panic("rtrequest1_msghandler: rtrequest table req %d, "
751 "failed on cpu%d, error %d\n",
752 rmsg->req, mycpuid, error);
ecdefdda 753 }
002c1265 754 lwkt_replymsg(&rmsg->base.lmsg, error);
ecdefdda 755 } else if (nextcpu < ncpus) {
9763404f 756 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
ecdefdda 757 } else {
002c1265 758 lwkt_replymsg(&rmsg->base.lmsg, rmsg->base.lmsg.ms_error);
ecdefdda 759 }
ecdefdda
MD
760}
761
984263bc 762int
6554f2c4 763rtrequest1(int req, struct rt_addrinfo *rtinfo, struct rtentry **ret_nrt)
984263bc 764{
6554f2c4 765 struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
82ed7fc2
RG
766 struct rtentry *rt;
767 struct radix_node *rn;
768 struct radix_node_head *rnh;
984263bc
MD
769 struct ifaddr *ifa;
770 struct sockaddr *ndst;
afe44b68 771 boolean_t reportmsg;
2e9572df
JH
772 int error = 0;
773
774#define gotoerr(x) { error = x ; goto bad; }
984263bc 775
69cb4182
MD
776#ifdef ROUTE_DEBUG
777 if (route_debug)
778 rt_addrinfo_print(req, rtinfo);
779#endif
780
4986965b 781 crit_enter();
984263bc
MD
782 /*
783 * Find the correct routing tree to use for this Address Family
784 */
ecdefdda 785 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL)
2e9572df 786 gotoerr(EAFNOSUPPORT);
f23061d4 787
984263bc
MD
788 /*
789 * If we are adding a host route then we don't want to put
790 * a netmask in the tree, nor do we want to clone it.
791 */
6554f2c4
JH
792 if (rtinfo->rti_flags & RTF_HOST) {
793 rtinfo->rti_info[RTAX_NETMASK] = NULL;
794 rtinfo->rti_flags &= ~(RTF_CLONING | RTF_PRCLONING);
984263bc 795 }
f23061d4 796
984263bc
MD
797 switch (req) {
798 case RTM_DELETE:
f23061d4 799 /* Remove the item from the tree. */
6554f2c4
JH
800 rn = rnh->rnh_deladdr((char *)rtinfo->rti_info[RTAX_DST],
801 (char *)rtinfo->rti_info[RTAX_NETMASK],
f23061d4
JH
802 rnh);
803 if (rn == NULL)
2e9572df 804 gotoerr(ESRCH);
f23061d4
JH
805 KASSERT(!(rn->rn_flags & (RNF_ACTIVE | RNF_ROOT)),
806 ("rnh_deladdr returned flags 0x%x", rn->rn_flags));
984263bc
MD
807 rt = (struct rtentry *)rn;
808
ecdefdda
MD
809 /* ref to prevent a deletion race */
810 ++rt->rt_refcnt;
811
f23061d4 812 /* Free any routes cloned from this one. */
984263bc 813 if ((rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) &&
2e9572df 814 rt_mask(rt) != NULL) {
f23061d4 815 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
2e9572df 816 (char *)rt_mask(rt),
984263bc
MD
817 rt_fixdelete, rt);
818 }
819
f23061d4
JH
820 if (rt->rt_gwroute != NULL) {
821 RTFREE(rt->rt_gwroute);
822 rt->rt_gwroute = NULL;
984263bc
MD
823 }
824
825 /*
826 * NB: RTF_UP must be set during the search above,
827 * because we might delete the last ref, causing
828 * rt to get freed prematurely.
984263bc
MD
829 */
830 rt->rt_flags &= ~RTF_UP;
831
69cb4182
MD
832#ifdef ROUTE_DEBUG
833 if (route_debug)
834 rt_print(rtinfo, rt);
835#endif
836
f23061d4 837 /* Give the protocol a chance to keep things in sync. */
984263bc 838 if ((ifa = rt->rt_ifa) && ifa->ifa_rtrequest)
3ffea39d 839 ifa->ifa_rtrequest(RTM_DELETE, rt);
984263bc 840
984263bc
MD
841 /*
842 * If the caller wants it, then it can have it,
843 * but it's up to it to free the rtentry as we won't be
844 * doing it.
845 */
a40401d5
JH
846 KASSERT(rt->rt_refcnt >= 0,
847 ("rtrequest1(DELETE): refcnt %ld", rt->rt_refcnt));
f23061d4 848 if (ret_nrt != NULL) {
ecdefdda 849 /* leave ref intact for return */
984263bc 850 *ret_nrt = rt;
ecdefdda
MD
851 } else {
852 /* deref / attempt to destroy */
984263bc
MD
853 rtfree(rt);
854 }
855 break;
856
857 case RTM_RESOLVE:
2e9572df
JH
858 if (ret_nrt == NULL || (rt = *ret_nrt) == NULL)
859 gotoerr(EINVAL);
6e67a788
SZ
860
861 KASSERT(rt->rt_cpuid == mycpuid,
862 ("rt resolve rt_cpuid %d, mycpuid %d",
863 rt->rt_cpuid, mycpuid));
864
984263bc 865 ifa = rt->rt_ifa;
6554f2c4 866 rtinfo->rti_flags =
590b8cd4 867 rt->rt_flags & ~(RTF_CLONING | RTF_PRCLONING | RTF_STATIC);
6554f2c4
JH
868 rtinfo->rti_flags |= RTF_WASCLONED;
869 rtinfo->rti_info[RTAX_GATEWAY] = rt->rt_gateway;
870 if ((rtinfo->rti_info[RTAX_NETMASK] = rt->rt_genmask) == NULL)
871 rtinfo->rti_flags |= RTF_HOST;
9b42cabe
NA
872 rtinfo->rti_info[RTAX_MPLS1] = rt->rt_shim[0];
873 rtinfo->rti_info[RTAX_MPLS2] = rt->rt_shim[1];
874 rtinfo->rti_info[RTAX_MPLS3] = rt->rt_shim[2];
984263bc
MD
875 goto makeroute;
876
877 case RTM_ADD:
6554f2c4
JH
878 KASSERT(!(rtinfo->rti_flags & RTF_GATEWAY) ||
879 rtinfo->rti_info[RTAX_GATEWAY] != NULL,
f23061d4 880 ("rtrequest: GATEWAY but no gateway"));
984263bc 881
6554f2c4 882 if (rtinfo->rti_ifa == NULL && (error = rt_getifa(rtinfo)))
2e9572df 883 gotoerr(error);
6554f2c4 884 ifa = rtinfo->rti_ifa;
2e9572df 885makeroute:
6554f2c4 886 R_Malloc(rt, struct rtentry *, sizeof(struct rtentry));
38c2eb26
SZ
887 if (rt == NULL) {
888 if (req == RTM_ADD) {
889 kprintf("rtrequest1: alloc rtentry failed on "
890 "cpu%d\n", mycpuid);
891 }
2e9572df 892 gotoerr(ENOBUFS);
38c2eb26 893 }
6554f2c4
JH
894 bzero(rt, sizeof(struct rtentry));
895 rt->rt_flags = RTF_UP | rtinfo->rti_flags;
da14dbcf 896 rt->rt_cpuid = mycpuid;
afe44b68
SZ
897
898 if (mycpuid != 0 && req == RTM_ADD) {
899 /* For RTM_ADD, we have already sent rtmsg on CPU0. */
900 reportmsg = RTL_DONTREPORT;
901 } else {
902 /*
903 * For RTM_ADD, we only send rtmsg on CPU0.
904 * For RTM_RESOLVE, we always send rtmsg. XXX
905 */
906 reportmsg = RTL_REPORTMSG;
907 }
908 error = rt_setgate(rt, dst, rtinfo->rti_info[RTAX_GATEWAY],
909 reportmsg);
f23061d4 910 if (error != 0) {
984263bc 911 Free(rt);
2e9572df 912 gotoerr(error);
984263bc
MD
913 }
914
984263bc 915 ndst = rt_key(rt);
6554f2c4
JH
916 if (rtinfo->rti_info[RTAX_NETMASK] != NULL)
917 rt_maskedcopy(dst, ndst,
918 rtinfo->rti_info[RTAX_NETMASK]);
f23061d4 919 else
2e9572df 920 bcopy(dst, ndst, dst->sa_len);
984263bc 921
9b42cabe
NA
922 if (rtinfo->rti_info[RTAX_MPLS1] != NULL)
923 rt_setshims(rt, rtinfo->rti_info);
924
984263bc
MD
925 /*
926 * Note that we now have a reference to the ifa.
927 * This moved from below so that rnh->rnh_addaddr() can
928 * examine the ifa and ifa->ifa_ifp if it so desires.
929 */
f23061d4 930 IFAREF(ifa);
984263bc
MD
931 rt->rt_ifa = ifa;
932 rt->rt_ifp = ifa->ifa_ifp;
933 /* XXX mtu manipulation will be done in rnh_addaddr -- itojun */
934
2e9572df 935 rn = rnh->rnh_addaddr((char *)ndst,
6554f2c4 936 (char *)rtinfo->rti_info[RTAX_NETMASK],
2e9572df
JH
937 rnh, rt->rt_nodes);
938 if (rn == NULL) {
f23061d4
JH
939 struct rtentry *oldrt;
940
984263bc 941 /*
f23061d4
JH
942 * We already have one of these in the tree.
943 * We do a special hack: if the old route was
944 * cloned, then we blow it away and try
945 * re-inserting the new one.
984263bc 946 */
f3ed2586 947 oldrt = rtpurelookup(ndst);
f23061d4
JH
948 if (oldrt != NULL) {
949 --oldrt->rt_refcnt;
950 if (oldrt->rt_flags & RTF_WASCLONED) {
951 rtrequest(RTM_DELETE, rt_key(oldrt),
952 oldrt->rt_gateway,
953 rt_mask(oldrt),
954 oldrt->rt_flags, NULL);
955 rn = rnh->rnh_addaddr((char *)ndst,
6554f2c4
JH
956 (char *)
957 rtinfo->rti_info[RTAX_NETMASK],
958 rnh, rt->rt_nodes);
f23061d4 959 }
984263bc
MD
960 }
961 }
962
963 /*
964 * If it still failed to go into the tree,
f23061d4 965 * then un-make it (this should be a function).
984263bc 966 */
2e9572df 967 if (rn == NULL) {
f23061d4 968 if (rt->rt_gwroute != NULL)
984263bc 969 rtfree(rt->rt_gwroute);
f23061d4 970 IFAFREE(ifa);
984263bc
MD
971 Free(rt_key(rt));
972 Free(rt);
2e9572df 973 gotoerr(EEXIST);
984263bc
MD
974 }
975
984263bc
MD
976 /*
977 * If we got here from RESOLVE, then we are cloning
978 * so clone the rest, and note that we
979 * are a clone (and increment the parent's references)
980 */
981 if (req == RTM_RESOLVE) {
f23061d4
JH
982 rt->rt_rmx = (*ret_nrt)->rt_rmx; /* copy metrics */
983 rt->rt_rmx.rmx_pksent = 0; /* reset packet counter */
2e9572df 984 if ((*ret_nrt)->rt_flags &
f23061d4 985 (RTF_CLONING | RTF_PRCLONING)) {
ef87f48d 986 rt->rt_parent = *ret_nrt;
984263bc
MD
987 (*ret_nrt)->rt_refcnt++;
988 }
989 }
990
991 /*
992 * if this protocol has something to add to this then
993 * allow it to do that as well.
994 */
f23061d4 995 if (ifa->ifa_rtrequest != NULL)
3ffea39d 996 ifa->ifa_rtrequest(req, rt);
984263bc
MD
997
998 /*
999 * We repeat the same procedure from rt_setgate() here because
1000 * it doesn't fire when we call it there because the node
1001 * hasn't been added to the tree yet.
1002 */
2e9572df
JH
1003 if (req == RTM_ADD && !(rt->rt_flags & RTF_HOST) &&
1004 rt_mask(rt) != NULL) {
f23061d4
JH
1005 struct rtfc_arg arg = { rt, rnh };
1006
2e9572df
JH
1007 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
1008 (char *)rt_mask(rt),
984263bc
MD
1009 rt_fixchange, &arg);
1010 }
1011
69cb4182
MD
1012#ifdef ROUTE_DEBUG
1013 if (route_debug)
1014 rt_print(rtinfo, rt);
1015#endif
984263bc 1016 /*
f23061d4
JH
1017 * Return the resulting rtentry,
1018 * increasing the number of references by one.
984263bc 1019 */
ef87f48d 1020 if (ret_nrt != NULL) {
984263bc 1021 rt->rt_refcnt++;
f23061d4 1022 *ret_nrt = rt;
984263bc
MD
1023 }
1024 break;
1025 default:
1026 error = EOPNOTSUPP;
1027 }
1028bad:
69cb4182
MD
1029#ifdef ROUTE_DEBUG
1030 if (route_debug) {
1031 if (error)
4b1cf444 1032 kprintf("rti %p failed error %d\n", rtinfo, error);
69cb4182 1033 else
4b1cf444 1034 kprintf("rti %p succeeded\n", rtinfo);
69cb4182
MD
1035 }
1036#endif
4986965b 1037 crit_exit();
984263bc 1038 return (error);
984263bc
MD
1039}
1040
1041/*
1042 * Called from rtrequest(RTM_DELETE, ...) to fix up the route's ``family''
1043 * (i.e., the routes related to it by the operation of cloning). This
1044 * routine is iterated over all potential former-child-routes by way of
1045 * rnh->rnh_walktree_from() above, and those that actually are children of
1046 * the late parent (passed in as VP here) are themselves deleted.
1047 */
1048static int
2e9572df 1049rt_fixdelete(struct radix_node *rn, void *vp)
984263bc
MD
1050{
1051 struct rtentry *rt = (struct rtentry *)rn;
1052 struct rtentry *rt0 = vp;
1053
1054 if (rt->rt_parent == rt0 &&
1055 !(rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
f23061d4
JH
1056 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1057 rt->rt_flags, NULL);
984263bc
MD
1058 }
1059 return 0;
1060}
1061
1062/*
1063 * This routine is called from rt_setgate() to do the analogous thing for
1064 * adds and changes. There is the added complication in this case of a
1065 * middle insert; i.e., insertion of a new network route between an older
1066 * network route and (cloned) host routes. For this reason, a simple check
1067 * of rt->rt_parent is insufficient; each candidate route must be tested
1068 * against the (mask, value) of the new route (passed as before in vp)
1069 * to see if the new route matches it.
1070 *
1071 * XXX - it may be possible to do fixdelete() for changes and reserve this
1072 * routine just for adds. I'm not sure why I thought it was necessary to do
1073 * changes this way.
1074 */
1075#ifdef DEBUG
1076static int rtfcdebug = 0;
1077#endif
1078
1079static int
2e9572df 1080rt_fixchange(struct radix_node *rn, void *vp)
984263bc
MD
1081{
1082 struct rtentry *rt = (struct rtentry *)rn;
1083 struct rtfc_arg *ap = vp;
1084 struct rtentry *rt0 = ap->rt0;
1085 struct radix_node_head *rnh = ap->rnh;
1086 u_char *xk1, *xm1, *xk2, *xmp;
1087 int i, len, mlen;
1088
1089#ifdef DEBUG
1090 if (rtfcdebug)
4b1cf444 1091 kprintf("rt_fixchange: rt %p, rt0 %p\n", rt, rt0);
984263bc
MD
1092#endif
1093
f23061d4 1094 if (rt->rt_parent == NULL ||
984263bc
MD
1095 (rt->rt_flags & (RTF_PINNED | RTF_CLONING | RTF_PRCLONING))) {
1096#ifdef DEBUG
4b1cf444 1097 if (rtfcdebug) kprintf("no parent, pinned or cloning\n");
984263bc
MD
1098#endif
1099 return 0;
1100 }
1101
1102 if (rt->rt_parent == rt0) {
1103#ifdef DEBUG
4b1cf444 1104 if (rtfcdebug) kprintf("parent match\n");
984263bc 1105#endif
f23061d4
JH
1106 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1107 rt->rt_flags, NULL);
984263bc
MD
1108 }
1109
1110 /*
1111 * There probably is a function somewhere which does this...
1112 * if not, there should be.
1113 */
f23061d4 1114 len = imin(rt_key(rt0)->sa_len, rt_key(rt)->sa_len);
984263bc
MD
1115
1116 xk1 = (u_char *)rt_key(rt0);
1117 xm1 = (u_char *)rt_mask(rt0);
1118 xk2 = (u_char *)rt_key(rt);
1119
1120 /* avoid applying a less specific route */
1121 xmp = (u_char *)rt_mask(rt->rt_parent);
f23061d4
JH
1122 mlen = rt_key(rt->rt_parent)->sa_len;
1123 if (mlen > rt_key(rt0)->sa_len) {
984263bc
MD
1124#ifdef DEBUG
1125 if (rtfcdebug)
4b1cf444 1126 kprintf("rt_fixchange: inserting a less "
984263bc
MD
1127 "specific route\n");
1128#endif
1129 return 0;
1130 }
1131 for (i = rnh->rnh_treetop->rn_offset; i < mlen; i++) {
1132 if ((xmp[i] & ~(xmp[i] ^ xm1[i])) != xmp[i]) {
1133#ifdef DEBUG
1134 if (rtfcdebug)
4b1cf444 1135 kprintf("rt_fixchange: inserting a less "
984263bc
MD
1136 "specific route\n");
1137#endif
1138 return 0;
1139 }
1140 }
1141
1142 for (i = rnh->rnh_treetop->rn_offset; i < len; i++) {
1143 if ((xk2[i] & xm1[i]) != xk1[i]) {
1144#ifdef DEBUG
4b1cf444 1145 if (rtfcdebug) kprintf("no match\n");
984263bc
MD
1146#endif
1147 return 0;
1148 }
1149 }
1150
1151 /*
1152 * OK, this node is a clone, and matches the node currently being
1153 * changed/added under the node's mask. So, get rid of it.
1154 */
1155#ifdef DEBUG
4b1cf444 1156 if (rtfcdebug) kprintf("deleting\n");
984263bc 1157#endif
f23061d4
JH
1158 return rtrequest(RTM_DELETE, rt_key(rt), NULL, rt_mask(rt),
1159 rt->rt_flags, NULL);
984263bc
MD
1160}
1161
984263bc 1162int
afe44b68
SZ
1163rt_setgate(struct rtentry *rt0, struct sockaddr *dst, struct sockaddr *gate,
1164 boolean_t generate_report)
984263bc 1165{
f23061d4 1166 char *space, *oldspace;
4ff4d99f 1167 int dlen = RT_ROUNDUP(dst->sa_len), glen = RT_ROUNDUP(gate->sa_len);
82ed7fc2 1168 struct rtentry *rt = rt0;
ecdefdda 1169 struct radix_node_head *rnh = rt_tables[mycpuid][dst->sa_family];
984263bc
MD
1170
1171 /*
1172 * A host route with the destination equal to the gateway
1173 * will interfere with keeping LLINFO in the routing
1174 * table, so disallow it.
1175 */
f23061d4
JH
1176 if (((rt0->rt_flags & (RTF_HOST | RTF_GATEWAY | RTF_LLINFO)) ==
1177 (RTF_HOST | RTF_GATEWAY)) &&
1178 dst->sa_len == gate->sa_len &&
1179 sa_equal(dst, gate)) {
984263bc
MD
1180 /*
1181 * The route might already exist if this is an RTM_CHANGE
1182 * or a routing redirect, so try to delete it.
1183 */
2e9572df
JH
1184 if (rt_key(rt0) != NULL)
1185 rtrequest(RTM_DELETE, rt_key(rt0), rt0->rt_gateway,
f23061d4 1186 rt_mask(rt0), rt0->rt_flags, NULL);
984263bc
MD
1187 return EADDRNOTAVAIL;
1188 }
1189
1190 /*
f23061d4 1191 * Both dst and gateway are stored in the same malloc'ed chunk
984263bc
MD
1192 * (If I ever get my hands on....)
1193 * if we need to malloc a new chunk, then keep the old one around
1194 * till we don't need it any more.
1195 */
4ff4d99f
NA
1196 if (rt->rt_gateway == NULL ||
1197 glen > RT_ROUNDUP(rt->rt_gateway->sa_len)) {
f23061d4
JH
1198 oldspace = (char *)rt_key(rt);
1199 R_Malloc(space, char *, dlen + glen);
1200 if (space == NULL)
984263bc 1201 return ENOBUFS;
f23061d4 1202 rt->rt_nodes->rn_key = space;
984263bc 1203 } else {
f23061d4
JH
1204 space = (char *)rt_key(rt); /* Just use the old space. */
1205 oldspace = NULL;
984263bc
MD
1206 }
1207
f23061d4
JH
1208 /* Set the gateway value. */
1209 rt->rt_gateway = (struct sockaddr *)(space + dlen);
2e9572df 1210 bcopy(gate, rt->rt_gateway, glen);
984263bc 1211
f23061d4
JH
1212 if (oldspace != NULL) {
1213 /*
1214 * If we allocated a new chunk, preserve the original dst.
1215 * This way, rt_setgate() really just sets the gate
1216 * and leaves the dst field alone.
1217 */
1218 bcopy(dst, space, dlen);
1219 Free(oldspace);
984263bc
MD
1220 }
1221
1222 /*
f23061d4 1223 * If there is already a gwroute, it's now almost definitely wrong
984263bc
MD
1224 * so drop it.
1225 */
1226 if (rt->rt_gwroute != NULL) {
1227 RTFREE(rt->rt_gwroute);
1228 rt->rt_gwroute = NULL;
1229 }
984263bc 1230 if (rt->rt_flags & RTF_GATEWAY) {
f23061d4
JH
1231 /*
1232 * Cloning loop avoidance: In the presence of
1233 * protocol-cloning and bad configuration, it is
1234 * possible to get stuck in bottomless mutual recursion
1235 * (rtrequest rt_setgate rtlookup). We avoid this
1236 * by not allowing protocol-cloning to operate for
1237 * gateways (which is probably the correct choice
1238 * anyway), and avoid the resulting reference loops
1239 * by disallowing any route to run through itself as
1240 * a gateway. This is obviously mandatory when we
1241 * get rt->rt_output().
1242 *
0c3c561c 1243 * This breaks TTCP for hosts outside the gateway! XXX JH
f23061d4 1244 */
afe44b68
SZ
1245 rt->rt_gwroute = _rtlookup(gate, generate_report,
1246 RTF_PRCLONING);
984263bc 1247 if (rt->rt_gwroute == rt) {
f23061d4
JH
1248 rt->rt_gwroute = NULL;
1249 --rt->rt_refcnt;
984263bc
MD
1250 return EDQUOT; /* failure */
1251 }
1252 }
1253
1254 /*
1255 * This isn't going to do anything useful for host routes, so
1256 * don't bother. Also make sure we have a reasonable mask
1257 * (we don't yet have one during adds).
1258 */
2e9572df 1259 if (!(rt->rt_flags & RTF_HOST) && rt_mask(rt) != NULL) {
f23061d4
JH
1260 struct rtfc_arg arg = { rt, rnh };
1261
1262 rnh->rnh_walktree_from(rnh, (char *)rt_key(rt),
2e9572df 1263 (char *)rt_mask(rt),
984263bc
MD
1264 rt_fixchange, &arg);
1265 }
1266
1267 return 0;
1268}
1269
1270static void
f23061d4
JH
1271rt_maskedcopy(
1272 struct sockaddr *src,
1273 struct sockaddr *dst,
1274 struct sockaddr *netmask)
984263bc 1275{
82ed7fc2
RG
1276 u_char *cp1 = (u_char *)src;
1277 u_char *cp2 = (u_char *)dst;
1278 u_char *cp3 = (u_char *)netmask;
984263bc
MD
1279 u_char *cplim = cp2 + *cp3;
1280 u_char *cplim2 = cp2 + *cp1;
1281
1282 *cp2++ = *cp1++; *cp2++ = *cp1++; /* copies sa_len & sa_family */
1283 cp3 += 2;
1284 if (cplim > cplim2)
1285 cplim = cplim2;
1286 while (cp2 < cplim)
1287 *cp2++ = *cp1++ & *cp3++;
1288 if (cp2 < cplim2)
f23061d4
JH
1289 bzero(cp2, cplim2 - cp2);
1290}
1291
1292int
1293rt_llroute(struct sockaddr *dst, struct rtentry *rt0, struct rtentry **drt)
1294{
1295 struct rtentry *up_rt, *rt;
1296
1297 if (!(rt0->rt_flags & RTF_UP)) {
f3ed2586 1298 up_rt = rtlookup(dst);
f23061d4
JH
1299 if (up_rt == NULL)
1300 return (EHOSTUNREACH);
1301 up_rt->rt_refcnt--;
1302 } else
1303 up_rt = rt0;
1304 if (up_rt->rt_flags & RTF_GATEWAY) {
1305 if (up_rt->rt_gwroute == NULL) {
f3ed2586 1306 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
f23061d4
JH
1307 if (up_rt->rt_gwroute == NULL)
1308 return (EHOSTUNREACH);
1309 } else if (!(up_rt->rt_gwroute->rt_flags & RTF_UP)) {
1310 rtfree(up_rt->rt_gwroute);
f3ed2586 1311 up_rt->rt_gwroute = rtlookup(up_rt->rt_gateway);
f23061d4
JH
1312 if (up_rt->rt_gwroute == NULL)
1313 return (EHOSTUNREACH);
1314 }
1315 rt = up_rt->rt_gwroute;
1316 } else
1317 rt = up_rt;
1318 if (rt->rt_flags & RTF_REJECT &&
1319 (rt->rt_rmx.rmx_expire == 0 || /* rt doesn't expire */
cec73927 1320 time_uptime < rt->rt_rmx.rmx_expire)) /* rt not expired */
f23061d4
JH
1321 return (rt->rt_flags & RTF_HOST ? EHOSTDOWN : EHOSTUNREACH);
1322 *drt = rt;
1323 return 0;
984263bc
MD
1324}
1325
9b42cabe
NA
1326static int
1327rt_setshims(struct rtentry *rt, struct sockaddr **rt_shim){
1328 int i;
1329
1330 for (i=0; i<3; i++) {
1331 struct sockaddr *shim = rt_shim[RTAX_MPLS1 + i];
1332 int shimlen;
1333
1334 if (shim == NULL)
1335 break;
1336
4ff4d99f 1337 shimlen = RT_ROUNDUP(shim->sa_len);
03c05fdf 1338 R_Malloc(rt->rt_shim[i], struct sockaddr *, shimlen);
9b42cabe
NA
1339 bcopy(shim, rt->rt_shim[i], shimlen);
1340 }
1341
1342 return 0;
1343}
1344
69cb4182
MD
1345#ifdef ROUTE_DEBUG
1346
1347/*
1348 * Print out a route table entry
1349 */
1350void
1351rt_print(struct rt_addrinfo *rtinfo, struct rtentry *rn)
1352{
4b1cf444 1353 kprintf("rti %p cpu %d route %p flags %08lx: ",
69cb4182
MD
1354 rtinfo, mycpuid, rn, rn->rt_flags);
1355 sockaddr_print(rt_key(rn));
4b1cf444 1356 kprintf(" mask ");
69cb4182 1357 sockaddr_print(rt_mask(rn));
4b1cf444 1358 kprintf(" gw ");
69cb4182 1359 sockaddr_print(rn->rt_gateway);
4b1cf444
SW
1360 kprintf(" ifc \"%s\"", rn->rt_ifp ? rn->rt_ifp->if_dname : "?");
1361 kprintf(" ifa %p\n", rn->rt_ifa);
69cb4182
MD
1362}
1363
1364void
1365rt_addrinfo_print(int cmd, struct rt_addrinfo *rti)
1366{
1367 int didit = 0;
1368 int i;
1369
1370#ifdef ROUTE_DEBUG
1371 if (cmd == RTM_DELETE && route_debug > 1)
7ce2998e 1372 print_backtrace(-1);
69cb4182
MD
1373#endif
1374
1375 switch(cmd) {
1376 case RTM_ADD:
4b1cf444 1377 kprintf("ADD ");
69cb4182
MD
1378 break;
1379 case RTM_RESOLVE:
4b1cf444 1380 kprintf("RES ");
69cb4182
MD
1381 break;
1382 case RTM_DELETE:
4b1cf444 1383 kprintf("DEL ");
69cb4182
MD
1384 break;
1385 default:
4b1cf444 1386 kprintf("C%02d ", cmd);
69cb4182
MD
1387 break;
1388 }
4b1cf444 1389 kprintf("rti %p cpu %d ", rti, mycpuid);
69cb4182
MD
1390 for (i = 0; i < rti->rti_addrs; ++i) {
1391 if (rti->rti_info[i] == NULL)
1392 continue;
1393 if (didit)
4b1cf444 1394 kprintf(" ,");
69cb4182
MD
1395 switch(i) {
1396 case RTAX_DST:
4b1cf444 1397 kprintf("(DST ");
69cb4182
MD
1398 break;
1399 case RTAX_GATEWAY:
4b1cf444 1400 kprintf("(GWY ");
69cb4182
MD
1401 break;
1402 case RTAX_NETMASK:
4b1cf444 1403 kprintf("(MSK ");
69cb4182
MD
1404 break;
1405 case RTAX_GENMASK:
4b1cf444 1406 kprintf("(GEN ");
69cb4182
MD
1407 break;
1408 case RTAX_IFP:
4b1cf444 1409 kprintf("(IFP ");
69cb4182
MD
1410 break;
1411 case RTAX_IFA:
4b1cf444 1412 kprintf("(IFA ");
69cb4182
MD
1413 break;
1414 case RTAX_AUTHOR:
4b1cf444 1415 kprintf("(AUT ");
69cb4182
MD
1416 break;
1417 case RTAX_BRD:
4b1cf444 1418 kprintf("(BRD ");
69cb4182
MD
1419 break;
1420 default:
4b1cf444 1421 kprintf("(?%02d ", i);
69cb4182
MD
1422 break;
1423 }
1424 sockaddr_print(rti->rti_info[i]);
4b1cf444 1425 kprintf(")");
69cb4182
MD
1426 didit = 1;
1427 }
4b1cf444 1428 kprintf("\n");
69cb4182
MD
1429}
1430
1431void
1432sockaddr_print(struct sockaddr *sa)
1433{
1434 struct sockaddr_in *sa4;
1435 struct sockaddr_in6 *sa6;
1436 int len;
1437 int i;
1438
1439 if (sa == NULL) {
4b1cf444 1440 kprintf("NULL");
69cb4182
MD
1441 return;
1442 }
1443
1444 len = sa->sa_len - offsetof(struct sockaddr, sa_data[0]);
1445
1446 switch(sa->sa_family) {
1447 case AF_INET:
1448 case AF_INET6:
1449 default:
1450 switch(sa->sa_family) {
1451 case AF_INET:
1452 sa4 = (struct sockaddr_in *)sa;
4b1cf444 1453 kprintf("INET %d %d.%d.%d.%d",
69cb4182
MD
1454 ntohs(sa4->sin_port),
1455 (ntohl(sa4->sin_addr.s_addr) >> 24) & 255,
1456 (ntohl(sa4->sin_addr.s_addr) >> 16) & 255,
1457 (ntohl(sa4->sin_addr.s_addr) >> 8) & 255,
1458 (ntohl(sa4->sin_addr.s_addr) >> 0) & 255
1459 );
1460 break;
1461 case AF_INET6:
1462 sa6 = (struct sockaddr_in6 *)sa;
4b1cf444 1463 kprintf("INET6 %d %04x:%04x%04x:%04x:%04x:%04x:%04x:%04x",
69cb4182
MD
1464 ntohs(sa6->sin6_port),
1465 sa6->sin6_addr.s6_addr16[0],
1466 sa6->sin6_addr.s6_addr16[1],
1467 sa6->sin6_addr.s6_addr16[2],
1468 sa6->sin6_addr.s6_addr16[3],
1469 sa6->sin6_addr.s6_addr16[4],
1470 sa6->sin6_addr.s6_addr16[5],
1471 sa6->sin6_addr.s6_addr16[6],
1472 sa6->sin6_addr.s6_addr16[7]
1473 );
1474 break;
1475 default:
4b1cf444 1476 kprintf("AF%d ", sa->sa_family);
69cb4182
MD
1477 while (len > 0 && sa->sa_data[len-1] == 0)
1478 --len;
1479
1480 for (i = 0; i < len; ++i) {
1481 if (i)
4b1cf444
SW
1482 kprintf(".");
1483 kprintf("%d", (unsigned char)sa->sa_data[i]);
69cb4182
MD
1484 }
1485 break;
1486 }
1487 }
1488}
1489
1490#endif
1491
984263bc 1492/*
6554f2c4 1493 * Set up a routing table entry, normally for an interface.
984263bc
MD
1494 */
1495int
2e9572df 1496rtinit(struct ifaddr *ifa, int cmd, int flags)
984263bc 1497{
f23061d4 1498 struct sockaddr *dst, *deldst, *netmask;
f23061d4 1499 struct mbuf *m = NULL;
984263bc
MD
1500 struct radix_node_head *rnh;
1501 struct radix_node *rn;
6554f2c4 1502 struct rt_addrinfo rtinfo;
f23061d4 1503 int error;
984263bc
MD
1504
1505 if (flags & RTF_HOST) {
1506 dst = ifa->ifa_dstaddr;
1507 netmask = NULL;
1508 } else {
1509 dst = ifa->ifa_addr;
1510 netmask = ifa->ifa_netmask;
1511 }
1512 /*
1513 * If it's a delete, check that if it exists, it's on the correct
1514 * interface or we might scrub a route to another ifa which would
1515 * be confusing at best and possibly worse.
1516 */
1517 if (cmd == RTM_DELETE) {
1518 /*
1519 * It's a delete, so it should already exist..
1520 * If it's a net, mask off the host bits
1521 * (Assuming we have a mask)
1522 */
1523 if (netmask != NULL) {
b5523eac 1524 m = m_get(M_NOWAIT, MT_SONAME);
984263bc 1525 if (m == NULL)
f23061d4 1526 return (ENOBUFS);
e9fa4b60 1527 mbuftrackid(m, 34);
984263bc
MD
1528 deldst = mtod(m, struct sockaddr *);
1529 rt_maskedcopy(dst, deldst, netmask);
1530 dst = deldst;
1531 }
1532 /*
1533 * Look up an rtentry that is in the routing tree and
1534 * contains the correct info.
1535 */
ecdefdda 1536 if ((rnh = rt_tables[mycpuid][dst->sa_family]) == NULL ||
f23061d4
JH
1537 (rn = rnh->rnh_lookup((char *)dst,
1538 (char *)netmask, rnh)) == NULL ||
984263bc 1539 ((struct rtentry *)rn)->rt_ifa != ifa ||
f23061d4 1540 !sa_equal((struct sockaddr *)rn->rn_key, dst)) {
ef87f48d 1541 if (m != NULL)
f23061d4 1542 m_free(m);
984263bc
MD
1543 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1544 }
1545 /* XXX */
1546#if 0
1547 else {
1548 /*
1549 * One would think that as we are deleting, and we know
1550 * it doesn't exist, we could just return at this point
1551 * with an "ELSE" clause, but apparently not..
1552 */
1553 return (flags & RTF_HOST ? EHOSTUNREACH : ENETUNREACH);
1554 }
1555#endif
1556 }
1557 /*
1558 * Do the actual request
1559 */
6554f2c4
JH
1560 bzero(&rtinfo, sizeof(struct rt_addrinfo));
1561 rtinfo.rti_info[RTAX_DST] = dst;
1562 rtinfo.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
1563 rtinfo.rti_info[RTAX_NETMASK] = netmask;
1564 rtinfo.rti_flags = flags | ifa->ifa_flags;
1565 rtinfo.rti_ifa = ifa;
04221313
SZ
1566 error = rtrequest1_global(cmd, &rtinfo, rtinit_rtrequest_callback, ifa,
1567 RTREQ_PRIO_HIGH);
ecdefdda
MD
1568 if (m != NULL)
1569 m_free(m);
1570 return (error);
1571}
1572
1573static void
1574rtinit_rtrequest_callback(int cmd, int error,
1575 struct rt_addrinfo *rtinfo, struct rtentry *rt,
1576 void *arg)
1577{
1578 struct ifaddr *ifa = arg;
1579
1580 if (error == 0 && rt) {
1581 if (mycpuid == 0) {
1582 ++rt->rt_refcnt;
1583 rt_newaddrmsg(cmd, ifa, error, rt);
1584 --rt->rt_refcnt;
1585 }
984263bc 1586 if (cmd == RTM_DELETE) {
a40401d5 1587 if (rt->rt_refcnt == 0) {
ecdefdda 1588 ++rt->rt_refcnt;
984263bc
MD
1589 rtfree(rt);
1590 }
984263bc
MD
1591 }
1592 }
984263bc
MD
1593}
1594
2c62ae3b 1595struct netmsg_rts {
002c1265 1596 struct netmsg_base base;
2c62ae3b
SZ
1597 int req;
1598 struct rt_addrinfo *rtinfo;
1599 rtsearch_callback_func_t callback;
1600 void *arg;
1601 boolean_t exact_match;
1602 int found_cnt;
1603};
1604
1605int
1606rtsearch_global(int req, struct rt_addrinfo *rtinfo,
04221313
SZ
1607 rtsearch_callback_func_t callback, void *arg, boolean_t exact_match,
1608 boolean_t req_prio)
2c62ae3b
SZ
1609{
1610 struct netmsg_rts msg;
04221313 1611 int flags = 0;
2c62ae3b 1612
04221313
SZ
1613 if (req_prio)
1614 flags = MSGF_PRIORITY;
1615 netmsg_init(&msg.base, NULL, &curthread->td_msgport, flags,
1616 rtsearch_msghandler);
2c62ae3b
SZ
1617 msg.req = req;
1618 msg.rtinfo = rtinfo;
1619 msg.callback = callback;
1620 msg.arg = arg;
1621 msg.exact_match = exact_match;
1622 msg.found_cnt = 0;
2c4bf548 1623 return rt_domsg_global(&msg.base);
2c62ae3b
SZ
1624}
1625
1626static void
002c1265 1627rtsearch_msghandler(netmsg_t msg)
2c62ae3b 1628{
002c1265 1629 struct netmsg_rts *rmsg = (void *)msg;
6e6b3e50 1630 struct rt_addrinfo rtinfo;
2c62ae3b
SZ
1631 struct radix_node_head *rnh;
1632 struct rtentry *rt;
1633 int nextcpu, error;
1634
6e6b3e50
SZ
1635 /*
1636 * Copy the rtinfo. We need to make sure that the original
1637 * rtinfo, which is setup by the caller, in the netmsg will
1638 * _not_ be changed; else the next CPU on the netmsg forwarding
1639 * path will see a different rtinfo than what this CPU has seen.
1640 */
002c1265 1641 rtinfo = *rmsg->rtinfo;
6e6b3e50 1642
2c62ae3b
SZ
1643 /*
1644 * Find the correct routing tree to use for this Address Family
1645 */
6e6b3e50 1646 if ((rnh = rt_tables[mycpuid][rtinfo.rti_dst->sa_family]) == NULL) {
2c62ae3b 1647 if (mycpuid != 0)
ed20d0e3 1648 panic("partially initialized routing tables");
002c1265 1649 lwkt_replymsg(&rmsg->base.lmsg, EAFNOSUPPORT);
2c62ae3b
SZ
1650 return;
1651 }
1652
1653 /*
1654 * Correct rtinfo for the host route searching.
1655 */
6e6b3e50
SZ
1656 if (rtinfo.rti_flags & RTF_HOST) {
1657 rtinfo.rti_netmask = NULL;
1658 rtinfo.rti_flags &= ~(RTF_CLONING | RTF_PRCLONING);
2c62ae3b
SZ
1659 }
1660
1661 rt = (struct rtentry *)
6e6b3e50
SZ
1662 rnh->rnh_lookup((char *)rtinfo.rti_dst,
1663 (char *)rtinfo.rti_netmask, rnh);
2c62ae3b
SZ
1664
1665 /*
1666 * If we are asked to do the "exact match", we need to make sure
1667 * that host route searching got a host route while a network
1668 * route searching got a network route.
1669 */
002c1265 1670 if (rt != NULL && rmsg->exact_match &&
6e6b3e50 1671 ((rt->rt_flags ^ rtinfo.rti_flags) & RTF_HOST))
2c62ae3b
SZ
1672 rt = NULL;
1673
1674 if (rt == NULL) {
1675 /*
1676 * No matching routes have been found, don't count this
1677 * as a critical error (here, we set 'error' to 0), just
1678 * keep moving on, since at least prcloned routes are not
1679 * duplicated onto each CPU.
1680 */
1681 error = 0;
1682 } else {
002c1265 1683 rmsg->found_cnt++;
2c62ae3b
SZ
1684
1685 rt->rt_refcnt++;
002c1265
MD
1686 error = rmsg->callback(rmsg->req, &rtinfo, rt, rmsg->arg,
1687 rmsg->found_cnt);
2c62ae3b
SZ
1688 rt->rt_refcnt--;
1689
1690 if (error == EJUSTRETURN) {
002c1265 1691 lwkt_replymsg(&rmsg->base.lmsg, 0);
2c62ae3b
SZ
1692 return;
1693 }
1694 }
1695
1696 nextcpu = mycpuid + 1;
1697 if (error) {
002c1265 1698 KKASSERT(rmsg->found_cnt > 0);
2c62ae3b
SZ
1699
1700 /*
1701 * Under following cases, unrecoverable error has
1702 * not occured:
1703 * o Request is RTM_GET
1704 * o The first time that we find the route, but the
1705 * modification fails.
1706 */
002c1265 1707 if (rmsg->req != RTM_GET && rmsg->found_cnt > 1) {
2c62ae3b 1708 panic("rtsearch_msghandler: unrecoverable error "
6e6b3e50 1709 "cpu %d", mycpuid);
2c62ae3b 1710 }
002c1265 1711 lwkt_replymsg(&rmsg->base.lmsg, error);
2c62ae3b 1712 } else if (nextcpu < ncpus) {
9763404f 1713 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
2c62ae3b 1714 } else {
002c1265 1715 if (rmsg->found_cnt == 0) {
2c62ae3b
SZ
1716 /* The requested route was never seen ... */
1717 error = ESRCH;
1718 }
002c1265 1719 lwkt_replymsg(&rmsg->base.lmsg, error);
2c62ae3b
SZ
1720 }
1721}
1722
ac0f13c5 1723int
04221313 1724rtmask_add_global(struct sockaddr *mask, boolean_t req_prio)
ac0f13c5 1725{
002c1265 1726 struct netmsg_base msg;
04221313 1727 int flags = 0;
ac0f13c5 1728
04221313
SZ
1729 if (req_prio)
1730 flags = MSGF_PRIORITY;
1731 netmsg_init(&msg, NULL, &curthread->td_msgport, flags,
1732 rtmask_add_msghandler);
002c1265 1733 msg.lmsg.u.ms_resultp = mask;
ac0f13c5 1734
2c4bf548 1735 return rt_domsg_global(&msg);
ac0f13c5
SZ
1736}
1737
1738struct sockaddr *
1739_rtmask_lookup(struct sockaddr *mask, boolean_t search)
1740{
1741 struct radix_node *n;
1742
1743#define clen(s) (*(u_char *)(s))
b4628cf9 1744 n = rn_addmask((char *)mask, search, 1, rn_cpumaskhead(mycpuid));
ac0f13c5
SZ
1745 if (n != NULL &&
1746 mask->sa_len >= clen(n->rn_key) &&
1747 bcmp((char *)mask + 1,
1748 (char *)n->rn_key + 1, clen(n->rn_key) - 1) == 0) {
1749 return (struct sockaddr *)n->rn_key;
1750 } else {
1751 return NULL;
1752 }
1753#undef clen
1754}
1755
1756static void
002c1265 1757rtmask_add_msghandler(netmsg_t msg)
ac0f13c5 1758{
002c1265 1759 struct lwkt_msg *lmsg = &msg->lmsg;
ac0f13c5
SZ
1760 struct sockaddr *mask = lmsg->u.ms_resultp;
1761 int error = 0, nextcpu;
1762
1763 if (rtmask_lookup(mask) == NULL)
1764 error = ENOBUFS;
1765
1766 nextcpu = mycpuid + 1;
1767 if (!error && nextcpu < ncpus)
9763404f 1768 lwkt_forwardmsg(netisr_cpuport(nextcpu), lmsg);
ac0f13c5
SZ
1769 else
1770 lwkt_replymsg(lmsg, error);
1771}
1772
984263bc
MD
1773/* This must be before ip6_init2(), which is now SI_ORDER_MIDDLE */
1774SYSINIT(route, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, route_init, 0);
2c179393
SZ
1775
1776struct rtchange_arg {
1777 struct ifaddr *old_ifa;
1778 struct ifaddr *new_ifa;
1779 struct rtentry *rt;
1780 int changed;
1781};
1782
1783static void
1784rtchange_ifa(struct rtentry *rt, struct rtchange_arg *ap)
1785{
1786 if (rt->rt_ifa->ifa_rtrequest != NULL)
3ffea39d 1787 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt);
2c179393
SZ
1788 IFAFREE(rt->rt_ifa);
1789
1790 IFAREF(ap->new_ifa);
1791 rt->rt_ifa = ap->new_ifa;
1792 rt->rt_ifp = ap->new_ifa->ifa_ifp;
1793 if (rt->rt_ifa->ifa_rtrequest != NULL)
3ffea39d 1794 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt);
2c179393
SZ
1795
1796 ap->changed = 1;
1797}
1798
1799static int
1800rtchange_callback(struct radix_node *rn, void *xap)
1801{
1802 struct rtchange_arg *ap = xap;
1803 struct rtentry *rt = (struct rtentry *)rn;
1804
1805 if (rt->rt_ifa == ap->old_ifa) {
1806 if (rt->rt_flags & (RTF_CLONING | RTF_PRCLONING)) {
1807 /*
1808 * We could saw the branch off when we are
1809 * still sitting on it, if the ifa_rtrequest
1810 * DEL/ADD are called directly from here.
1811 */
1812 ap->rt = rt;
1813 return EJUSTRETURN;
1814 }
1815 rtchange_ifa(rt, ap);
1816 }
1817 return 0;
1818}
1819
5723b104
SZ
1820struct netmsg_rtchange {
1821 struct netmsg_base base;
1822 struct ifaddr *old_ifa;
1823 struct ifaddr *new_ifa;
1824 int changed;
1825};
1826
1827static void
1828rtchange_dispatch(netmsg_t msg)
2c179393 1829{
5723b104
SZ
1830 struct netmsg_rtchange *rmsg = (void *)msg;
1831 struct radix_node_head *rnh;
2c179393 1832 struct rtchange_arg arg;
5723b104
SZ
1833 int nextcpu, cpu;
1834
1835 cpu = mycpuid;
2c179393
SZ
1836
1837 memset(&arg, 0, sizeof(arg));
5723b104
SZ
1838 arg.old_ifa = rmsg->old_ifa;
1839 arg.new_ifa = rmsg->new_ifa;
1840
1841 rnh = rt_tables[cpu][AF_INET];
1842 for (;;) {
1843 int error;
1844
1845 KKASSERT(arg.rt == NULL);
1846 error = rnh->rnh_walktree(rnh, rtchange_callback, &arg);
1847 if (arg.rt != NULL) {
1848 struct rtentry *rt;
1849
1850 rt = arg.rt;
1851 arg.rt = NULL;
1852 rtchange_ifa(rt, &arg);
1853 } else {
1854 break;
1855 }
1856 }
1857 if (arg.changed)
1858 rmsg->changed = 1;
1859
1860 nextcpu = cpu + 1;
1861 if (nextcpu < ncpus)
9763404f 1862 lwkt_forwardmsg(netisr_cpuport(nextcpu), &rmsg->base.lmsg);
5723b104
SZ
1863 else
1864 lwkt_replymsg(&rmsg->base.lmsg, 0);
1865}
1866
1867int
1868rtchange(struct ifaddr *old_ifa, struct ifaddr *new_ifa)
1869{
1870 struct netmsg_rtchange msg;
2c179393
SZ
1871
1872 /*
1873 * XXX individual requests are not independantly chained,
1874 * which means that the per-cpu route tables will not be
1875 * consistent in the middle of the operation. If routes
1876 * related to the interface are manipulated while we are
1877 * doing this the inconsistancy could trigger a panic.
1878 */
3f48237e 1879 netmsg_init(&msg.base, NULL, &curthread->td_msgport, MSGF_PRIORITY,
5723b104
SZ
1880 rtchange_dispatch);
1881 msg.old_ifa = old_ifa;
1882 msg.new_ifa = new_ifa;
1883 msg.changed = 0;
2c4bf548 1884 rt_domsg_global(&msg.base);
5723b104
SZ
1885
1886 if (msg.changed) {
2c179393
SZ
1887 old_ifa->ifa_flags &= ~IFA_ROUTE;
1888 new_ifa->ifa_flags |= IFA_ROUTE;
1889 return 0;
1890 } else {
1891 return ENOENT;
1892 }
1893}
2c4bf548
SZ
1894
1895int
1896rt_domsg_global(struct netmsg_base *nmsg)
1897{
1898 ASSERT_CANDOMSG_NETISR0(curthread);
1899 return lwkt_domsg(netisr_cpuport(0), &nmsg->lmsg, 0);
1900}