altq: Make sure mbuf contains pkthdr in enqueue method
[dragonfly.git] / sys / net / rtsock.c
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
33 /*
34  * Copyright (c) 1988, 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.
45  * 3. Neither the name of the University nor the names of its contributors
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  *      @(#)rtsock.c    8.7 (Berkeley) 10/12/95
62  * $FreeBSD: src/sys/net/rtsock.c,v 1.44.2.11 2002/12/04 14:05:41 ru Exp $
63  */
64
65 #include "opt_sctp.h"
66
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/sysctl.h>
71 #include <sys/proc.h>
72 #include <sys/priv.h>
73 #include <sys/malloc.h>
74 #include <sys/mbuf.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/domain.h>
79
80 #include <sys/thread2.h>
81 #include <sys/socketvar2.h>
82
83 #include <net/if.h>
84 #include <net/route.h>
85 #include <net/raw_cb.h>
86 #include <net/netmsg2.h>
87 #include <net/netisr2.h>
88
89 #ifdef SCTP
90 extern void sctp_add_ip_address(struct ifaddr *ifa);
91 extern void sctp_delete_ip_address(struct ifaddr *ifa);
92 #endif /* SCTP */
93
94 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
95
96 static struct route_cb {
97         int     ip_count;
98         int     ip6_count;
99         int     ipx_count;
100         int     ns_count;
101         int     any_count;
102 } route_cb;
103
104 static const struct sockaddr route_src = { 2, PF_ROUTE, };
105
106 struct walkarg {
107         int     w_tmemsize;
108         int     w_op, w_arg;
109         void    *w_tmem;
110         struct sysctl_req *w_req;
111 };
112
113 static struct mbuf *
114                 rt_msg_mbuf (int, struct rt_addrinfo *);
115 static void     rt_msg_buffer (int, struct rt_addrinfo *, void *buf, int len);
116 static int      rt_msgsize (int type, struct rt_addrinfo *rtinfo);
117 static int      rt_xaddrs (char *, char *, struct rt_addrinfo *);
118 static int      sysctl_dumpentry (struct radix_node *rn, void *vw);
119 static int      sysctl_iflist (int af, struct walkarg *w);
120 static int      route_output(struct mbuf *, struct socket *, ...);
121 static void     rt_setmetrics (u_long, struct rt_metrics *,
122                                struct rt_metrics *);
123
124 /*
125  * It really doesn't make any sense at all for this code to share much
126  * with raw_usrreq.c, since its functionality is so restricted.  XXX
127  */
128 static void
129 rts_abort(netmsg_t msg)
130 {
131         crit_enter();
132         raw_usrreqs.pru_abort(msg);
133         /* msg invalid now */
134         crit_exit();
135 }
136
137 /* pru_accept is EOPNOTSUPP */
138
139 static void
140 rts_attach(netmsg_t msg)
141 {
142         struct socket *so = msg->base.nm_so;
143         struct pru_attach_info *ai = msg->attach.nm_ai;
144         struct rawcb *rp;
145         int proto = msg->attach.nm_proto;
146         int error;
147
148         crit_enter();
149         if (sotorawcb(so) != NULL) {
150                 error = EISCONN;
151                 goto done;
152         }
153
154         rp = kmalloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
155
156         /*
157          * The critical section is necessary to block protocols from sending
158          * error notifications (like RTM_REDIRECT or RTM_LOSING) while
159          * this PCB is extant but incompletely initialized.
160          * Probably we should try to do more of this work beforehand and
161          * eliminate the critical section.
162          */
163         so->so_pcb = rp;
164         soreference(so);        /* so_pcb assignment */
165         error = raw_attach(so, proto, ai->sb_rlimit);
166         rp = sotorawcb(so);
167         if (error) {
168                 kfree(rp, M_PCB);
169                 goto done;
170         }
171         switch(rp->rcb_proto.sp_protocol) {
172         case AF_INET:
173                 route_cb.ip_count++;
174                 break;
175         case AF_INET6:
176                 route_cb.ip6_count++;
177                 break;
178         case AF_IPX:
179                 route_cb.ipx_count++;
180                 break;
181         case AF_NS:
182                 route_cb.ns_count++;
183                 break;
184         }
185         rp->rcb_faddr = &route_src;
186         route_cb.any_count++;
187         soisconnected(so);
188         so->so_options |= SO_USELOOPBACK;
189         error = 0;
190 done:
191         crit_exit();
192         lwkt_replymsg(&msg->lmsg, error);
193 }
194
195 static void
196 rts_bind(netmsg_t msg)
197 {
198         crit_enter();
199         raw_usrreqs.pru_bind(msg); /* xxx just EINVAL */
200         /* msg invalid now */
201         crit_exit();
202 }
203
204 static void
205 rts_connect(netmsg_t msg)
206 {
207         crit_enter();
208         raw_usrreqs.pru_connect(msg); /* XXX just EINVAL */
209         /* msg invalid now */
210         crit_exit();
211 }
212
213 /* pru_connect2 is EOPNOTSUPP */
214 /* pru_control is EOPNOTSUPP */
215
216 static void
217 rts_detach(netmsg_t msg)
218 {
219         struct socket *so = msg->base.nm_so;
220         struct rawcb *rp = sotorawcb(so);
221
222         crit_enter();
223         if (rp != NULL) {
224                 switch(rp->rcb_proto.sp_protocol) {
225                 case AF_INET:
226                         route_cb.ip_count--;
227                         break;
228                 case AF_INET6:
229                         route_cb.ip6_count--;
230                         break;
231                 case AF_IPX:
232                         route_cb.ipx_count--;
233                         break;
234                 case AF_NS:
235                         route_cb.ns_count--;
236                         break;
237                 }
238                 route_cb.any_count--;
239         }
240         raw_usrreqs.pru_detach(msg);
241         /* msg invalid now */
242         crit_exit();
243 }
244
245 static void
246 rts_disconnect(netmsg_t msg)
247 {
248         crit_enter();
249         raw_usrreqs.pru_disconnect(msg);
250         /* msg invalid now */
251         crit_exit();
252 }
253
254 /* pru_listen is EOPNOTSUPP */
255
256 static void
257 rts_peeraddr(netmsg_t msg)
258 {
259         crit_enter();
260         raw_usrreqs.pru_peeraddr(msg);
261         /* msg invalid now */
262         crit_exit();
263 }
264
265 /* pru_rcvd is EOPNOTSUPP */
266 /* pru_rcvoob is EOPNOTSUPP */
267
268 static void
269 rts_send(netmsg_t msg)
270 {
271         crit_enter();
272         raw_usrreqs.pru_send(msg);
273         /* msg invalid now */
274         crit_exit();
275 }
276
277 /* pru_sense is null */
278
279 static void
280 rts_shutdown(netmsg_t msg)
281 {
282         crit_enter();
283         raw_usrreqs.pru_shutdown(msg);
284         /* msg invalid now */
285         crit_exit();
286 }
287
288 static void
289 rts_sockaddr(netmsg_t msg)
290 {
291         crit_enter();
292         raw_usrreqs.pru_sockaddr(msg);
293         /* msg invalid now */
294         crit_exit();
295 }
296
297 static struct pr_usrreqs route_usrreqs = {
298         .pru_abort = rts_abort,
299         .pru_accept = pr_generic_notsupp,
300         .pru_attach = rts_attach,
301         .pru_bind = rts_bind,
302         .pru_connect = rts_connect,
303         .pru_connect2 = pr_generic_notsupp,
304         .pru_control = pr_generic_notsupp,
305         .pru_detach = rts_detach,
306         .pru_disconnect = rts_disconnect,
307         .pru_listen = pr_generic_notsupp,
308         .pru_peeraddr = rts_peeraddr,
309         .pru_rcvd = pr_generic_notsupp,
310         .pru_rcvoob = pr_generic_notsupp,
311         .pru_send = rts_send,
312         .pru_sense = pru_sense_null,
313         .pru_shutdown = rts_shutdown,
314         .pru_sockaddr = rts_sockaddr,
315         .pru_sosend = sosend,
316         .pru_soreceive = soreceive
317 };
318
319 static __inline sa_family_t
320 familyof(struct sockaddr *sa)
321 {
322         return (sa != NULL ? sa->sa_family : 0);
323 }
324
325 /*
326  * Routing socket input function.  The packet must be serialized onto cpu 0.
327  * We use the cpu0_soport() netisr processing loop to handle it.
328  *
329  * This looks messy but it means that anyone, including interrupt code,
330  * can send a message to the routing socket.
331  */
332 static void
333 rts_input_handler(netmsg_t msg)
334 {
335         static const struct sockaddr route_dst = { 2, PF_ROUTE, };
336         struct sockproto route_proto;
337         struct netmsg_packet *pmsg = &msg->packet;
338         struct mbuf *m;
339         sa_family_t family;
340         struct rawcb *skip;
341
342         family = pmsg->base.lmsg.u.ms_result;
343         route_proto.sp_family = PF_ROUTE;
344         route_proto.sp_protocol = family;
345
346         m = pmsg->nm_packet;
347         M_ASSERTPKTHDR(m);
348
349         skip = m->m_pkthdr.header;
350         m->m_pkthdr.header = NULL;
351
352         raw_input(m, &route_proto, &route_src, &route_dst, skip);
353 }
354
355 static void
356 rts_input_skip(struct mbuf *m, sa_family_t family, struct rawcb *skip)
357 {
358         struct netmsg_packet *pmsg;
359         lwkt_port_t port;
360
361         M_ASSERTPKTHDR(m);
362
363         port = netisr_cpuport(0);       /* XXX same as for routing socket */
364         pmsg = &m->m_hdr.mh_netmsg;
365         netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
366                     0, rts_input_handler);
367         pmsg->nm_packet = m;
368         pmsg->base.lmsg.u.ms_result = family;
369         m->m_pkthdr.header = skip; /* XXX steal field in pkthdr */
370         lwkt_sendmsg(port, &pmsg->base.lmsg);
371 }
372
373 static __inline void
374 rts_input(struct mbuf *m, sa_family_t family)
375 {
376         rts_input_skip(m, family, NULL);
377 }
378
379 static void *
380 reallocbuf_nofree(void *ptr, size_t len, size_t olen)
381 {
382         void *newptr;
383
384         newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
385         if (newptr == NULL)
386                 return NULL;
387         bcopy(ptr, newptr, olen);
388         return (newptr);
389 }
390
391 /*
392  * Internal helper routine for route_output().
393  */
394 static int
395 _fillrtmsg(struct rt_msghdr **prtm, struct rtentry *rt,
396            struct rt_addrinfo *rtinfo)
397 {
398         int msglen;
399         struct rt_msghdr *rtm = *prtm;
400
401         /* Fill in rt_addrinfo for call to rt_msg_buffer(). */
402         rtinfo->rti_dst = rt_key(rt);
403         rtinfo->rti_gateway = rt->rt_gateway;
404         rtinfo->rti_netmask = rt_mask(rt);              /* might be NULL */
405         rtinfo->rti_genmask = rt->rt_genmask;           /* might be NULL */
406         if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
407                 if (rt->rt_ifp != NULL) {
408                         rtinfo->rti_ifpaddr =
409                             TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])
410                             ->ifa->ifa_addr;
411                         rtinfo->rti_ifaaddr = rt->rt_ifa->ifa_addr;
412                         if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
413                                 rtinfo->rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
414                         rtm->rtm_index = rt->rt_ifp->if_index;
415                 } else {
416                         rtinfo->rti_ifpaddr = NULL;
417                         rtinfo->rti_ifaaddr = NULL;
418                 }
419         } else if (rt->rt_ifp != NULL) {
420                 rtm->rtm_index = rt->rt_ifp->if_index;
421         }
422
423         msglen = rt_msgsize(rtm->rtm_type, rtinfo);
424         if (rtm->rtm_msglen < msglen) {
425                 /* NOTE: Caller will free the old rtm accordingly */
426                 rtm = reallocbuf_nofree(rtm, msglen, rtm->rtm_msglen);
427                 if (rtm == NULL)
428                         return (ENOBUFS);
429                 *prtm = rtm;
430         }
431         rt_msg_buffer(rtm->rtm_type, rtinfo, rtm, msglen);
432
433         rtm->rtm_flags = rt->rt_flags;
434         rtm->rtm_rmx = rt->rt_rmx;
435         rtm->rtm_addrs = rtinfo->rti_addrs;
436
437         return (0);
438 }
439
440 struct rtm_arg {
441         struct rt_msghdr        *bak_rtm;
442         struct rt_msghdr        *new_rtm;
443 };
444
445 static int
446 fillrtmsg(struct rtm_arg *arg, struct rtentry *rt,
447           struct rt_addrinfo *rtinfo)
448 {
449         struct rt_msghdr *rtm = arg->new_rtm;
450         int error;
451
452         error = _fillrtmsg(&rtm, rt, rtinfo);
453         if (!error) {
454                 if (arg->new_rtm != rtm) {
455                         /*
456                          * _fillrtmsg() just allocated a new rtm;
457                          * if the previously allocated rtm is not
458                          * the backing rtm, it should be freed.
459                          */
460                         if (arg->new_rtm != arg->bak_rtm)
461                                 kfree(arg->new_rtm, M_RTABLE);
462                         arg->new_rtm = rtm;
463                 }
464         }
465         return error;
466 }
467
468 static void route_output_add_callback(int, int, struct rt_addrinfo *,
469                                         struct rtentry *, void *);
470 static void route_output_delete_callback(int, int, struct rt_addrinfo *,
471                                         struct rtentry *, void *);
472 static int route_output_get_callback(int, struct rt_addrinfo *,
473                                      struct rtentry *, void *, int);
474 static int route_output_change_callback(int, struct rt_addrinfo *,
475                                         struct rtentry *, void *, int);
476 static int route_output_lock_callback(int, struct rt_addrinfo *,
477                                       struct rtentry *, void *, int);
478
479 /*ARGSUSED*/
480 static int
481 route_output(struct mbuf *m, struct socket *so, ...)
482 {
483         struct rtm_arg arg;
484         struct rt_msghdr *rtm = NULL;
485         struct rawcb *rp = NULL;
486         struct pr_output_info *oi;
487         struct rt_addrinfo rtinfo;
488         sa_family_t family;
489         int len, error = 0;
490         __va_list ap;
491
492         M_ASSERTPKTHDR(m);
493
494         __va_start(ap, so);
495         oi = __va_arg(ap, struct pr_output_info *);
496         __va_end(ap);
497
498         family = familyof(NULL);
499
500 #define gotoerr(e) { error = e; goto flush;}
501
502         if (m == NULL ||
503             (m->m_len < sizeof(long) &&
504              (m = m_pullup(m, sizeof(long))) == NULL))
505                 return (ENOBUFS);
506         len = m->m_pkthdr.len;
507         if (len < sizeof(struct rt_msghdr) ||
508             len != mtod(m, struct rt_msghdr *)->rtm_msglen)
509                 gotoerr(EINVAL);
510
511         rtm = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
512         if (rtm == NULL)
513                 gotoerr(ENOBUFS);
514
515         m_copydata(m, 0, len, (caddr_t)rtm);
516         if (rtm->rtm_version != RTM_VERSION)
517                 gotoerr(EPROTONOSUPPORT);
518
519         rtm->rtm_pid = oi->p_pid;
520         bzero(&rtinfo, sizeof(struct rt_addrinfo));
521         rtinfo.rti_addrs = rtm->rtm_addrs;
522         if (rt_xaddrs((char *)(rtm + 1), (char *)rtm + len, &rtinfo) != 0)
523                 gotoerr(EINVAL);
524
525         rtinfo.rti_flags = rtm->rtm_flags;
526         if (rtinfo.rti_dst == NULL || rtinfo.rti_dst->sa_family >= AF_MAX ||
527             (rtinfo.rti_gateway && rtinfo.rti_gateway->sa_family >= AF_MAX))
528                 gotoerr(EINVAL);
529
530         family = familyof(rtinfo.rti_dst);
531
532         if (rtinfo.rti_genmask != NULL) {
533                 error = rtmask_add_global(rtinfo.rti_genmask);
534                 if (error)
535                         goto flush;
536         }
537
538         /*
539          * Verify that the caller has the appropriate privilege; RTM_GET
540          * is the only operation the non-superuser is allowed.
541          */
542         if (rtm->rtm_type != RTM_GET &&
543             priv_check_cred(so->so_cred, PRIV_ROOT, 0) != 0)
544                 gotoerr(EPERM);
545
546         switch (rtm->rtm_type) {
547         case RTM_ADD:
548                 if (rtinfo.rti_gateway == NULL) {
549                         error = EINVAL;
550                 } else {
551                         error = rtrequest1_global(RTM_ADD, &rtinfo, 
552                                           route_output_add_callback, rtm);
553                 }
554                 break;
555         case RTM_DELETE:
556                 /*
557                  * Backing rtm (bak_rtm) could _not_ be freed during
558                  * rtrequest1_global or rtsearch_global, even if the
559                  * callback reallocates the rtm due to its size changes,
560                  * since rtinfo points to the backing rtm's memory area.
561                  * After rtrequest1_global or rtsearch_global returns,
562                  * it is safe to free the backing rtm, since rtinfo will
563                  * not be used anymore.
564                  *
565                  * new_rtm will be used to save the new rtm allocated
566                  * by rtrequest1_global or rtsearch_global.
567                  */
568                 arg.bak_rtm = rtm;
569                 arg.new_rtm = rtm;
570                 error = rtrequest1_global(RTM_DELETE, &rtinfo,
571                                           route_output_delete_callback, &arg);
572                 rtm = arg.new_rtm;
573                 if (rtm != arg.bak_rtm)
574                         kfree(arg.bak_rtm, M_RTABLE);
575                 break;
576         case RTM_GET:
577                 /* See the comment in RTM_DELETE */
578                 arg.bak_rtm = rtm;
579                 arg.new_rtm = rtm;
580                 error = rtsearch_global(RTM_GET, &rtinfo,
581                                         route_output_get_callback, &arg,
582                                         RTS_NOEXACTMATCH);
583                 rtm = arg.new_rtm;
584                 if (rtm != arg.bak_rtm)
585                         kfree(arg.bak_rtm, M_RTABLE);
586                 break;
587         case RTM_CHANGE:
588                 error = rtsearch_global(RTM_CHANGE, &rtinfo,
589                                         route_output_change_callback, rtm,
590                                         RTS_EXACTMATCH);
591                 break;
592         case RTM_LOCK:
593                 error = rtsearch_global(RTM_LOCK, &rtinfo,
594                                         route_output_lock_callback, rtm,
595                                         RTS_EXACTMATCH);
596                 break;
597         default:
598                 error = EOPNOTSUPP;
599                 break;
600         }
601 flush:
602         if (rtm != NULL) {
603                 if (error != 0)
604                         rtm->rtm_errno = error;
605                 else
606                         rtm->rtm_flags |= RTF_DONE;
607         }
608
609         /*
610          * Check to see if we don't want our own messages.
611          */
612         if (!(so->so_options & SO_USELOOPBACK)) {
613                 if (route_cb.any_count <= 1) {
614                         if (rtm != NULL)
615                                 kfree(rtm, M_RTABLE);
616                         m_freem(m);
617                         return (error);
618                 }
619                 /* There is another listener, so construct message */
620                 rp = sotorawcb(so);
621         }
622         if (rtm != NULL) {
623                 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
624                 if (m->m_pkthdr.len < rtm->rtm_msglen) {
625                         m_freem(m);
626                         m = NULL;
627                 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
628                         m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
629                 kfree(rtm, M_RTABLE);
630         }
631         if (m != NULL)
632                 rts_input_skip(m, family, rp);
633         return (error);
634 }
635
636 static void
637 route_output_add_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
638                           struct rtentry *rt, void *arg)
639 {
640         struct rt_msghdr *rtm = arg;
641
642         if (error == 0 && rt != NULL) {
643                 rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
644                     &rt->rt_rmx);
645                 rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
646                 rt->rt_rmx.rmx_locks |=
647                     (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
648                 if (rtinfo->rti_genmask != NULL) {
649                         rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask);
650                         if (rt->rt_genmask == NULL) {
651                                 /*
652                                  * This should not happen, since we
653                                  * have already installed genmask
654                                  * on each CPU before we reach here.
655                                  */
656                                 panic("genmask is gone!?");
657                         }
658                 } else {
659                         rt->rt_genmask = NULL;
660                 }
661                 rtm->rtm_index = rt->rt_ifp->if_index;
662         }
663 }
664
665 static void
666 route_output_delete_callback(int cmd, int error, struct rt_addrinfo *rtinfo,
667                           struct rtentry *rt, void *arg)
668 {
669         if (error == 0 && rt) {
670                 ++rt->rt_refcnt;
671                 if (fillrtmsg(arg, rt, rtinfo) != 0) {
672                         error = ENOBUFS;
673                         /* XXX no way to return the error */
674                 }
675                 --rt->rt_refcnt;
676         }
677         if (rt && rt->rt_refcnt == 0) {
678                 ++rt->rt_refcnt;
679                 rtfree(rt);
680         }
681 }
682
683 static int
684 route_output_get_callback(int cmd, struct rt_addrinfo *rtinfo,
685                           struct rtentry *rt, void *arg, int found_cnt)
686 {
687         int error, found = 0;
688
689         if (((rtinfo->rti_flags ^ rt->rt_flags) & RTF_HOST) == 0)
690                 found = 1;
691
692         error = fillrtmsg(arg, rt, rtinfo);
693         if (!error && found) {
694                 /* Got the exact match, we could return now! */
695                 error = EJUSTRETURN;
696         }
697         return error;
698 }
699
700 static int
701 route_output_change_callback(int cmd, struct rt_addrinfo *rtinfo,
702                              struct rtentry *rt, void *arg, int found_cnt)
703 {
704         struct rt_msghdr *rtm = arg;
705         struct ifaddr *ifa;
706         int error = 0;
707
708         /*
709          * new gateway could require new ifaddr, ifp;
710          * flags may also be different; ifp may be specified
711          * by ll sockaddr when protocol address is ambiguous
712          */
713         if (((rt->rt_flags & RTF_GATEWAY) && rtinfo->rti_gateway != NULL) ||
714             rtinfo->rti_ifpaddr != NULL ||
715             (rtinfo->rti_ifaaddr != NULL &&
716              !sa_equal(rtinfo->rti_ifaaddr, rt->rt_ifa->ifa_addr))) {
717                 error = rt_getifa(rtinfo);
718                 if (error != 0)
719                         goto done;
720         }
721         if (rtinfo->rti_gateway != NULL) {
722                 /*
723                  * We only need to generate rtmsg upon the
724                  * first route to be changed.
725                  */
726                 error = rt_setgate(rt, rt_key(rt), rtinfo->rti_gateway,
727                         found_cnt == 1 ? RTL_REPORTMSG : RTL_DONTREPORT);
728                 if (error != 0)
729                         goto done;
730         }
731         if ((ifa = rtinfo->rti_ifa) != NULL) {
732                 struct ifaddr *oifa = rt->rt_ifa;
733
734                 if (oifa != ifa) {
735                         if (oifa && oifa->ifa_rtrequest)
736                                 oifa->ifa_rtrequest(RTM_DELETE, rt);
737                         IFAFREE(rt->rt_ifa);
738                         IFAREF(ifa);
739                         rt->rt_ifa = ifa;
740                         rt->rt_ifp = rtinfo->rti_ifp;
741                 }
742         }
743         rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx, &rt->rt_rmx);
744         if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
745                 rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt);
746         if (rtinfo->rti_genmask != NULL) {
747                 rt->rt_genmask = rtmask_purelookup(rtinfo->rti_genmask);
748                 if (rt->rt_genmask == NULL) {
749                         /*
750                          * This should not happen, since we
751                          * have already installed genmask
752                          * on each CPU before we reach here.
753                          */
754                         panic("genmask is gone!?");
755                 }
756         }
757         rtm->rtm_index = rt->rt_ifp->if_index;
758 done:
759         return error;
760 }
761
762 static int
763 route_output_lock_callback(int cmd, struct rt_addrinfo *rtinfo,
764                            struct rtentry *rt, void *arg,
765                            int found_cnt __unused)
766 {
767         struct rt_msghdr *rtm = arg;
768
769         rt->rt_rmx.rmx_locks &= ~(rtm->rtm_inits);
770         rt->rt_rmx.rmx_locks |=
771                 (rtm->rtm_inits & rtm->rtm_rmx.rmx_locks);
772         return 0;
773 }
774
775 static void
776 rt_setmetrics(u_long which, struct rt_metrics *in, struct rt_metrics *out)
777 {
778 #define setmetric(flag, elt) if (which & (flag)) out->elt = in->elt;
779         setmetric(RTV_RPIPE, rmx_recvpipe);
780         setmetric(RTV_SPIPE, rmx_sendpipe);
781         setmetric(RTV_SSTHRESH, rmx_ssthresh);
782         setmetric(RTV_RTT, rmx_rtt);
783         setmetric(RTV_RTTVAR, rmx_rttvar);
784         setmetric(RTV_HOPCOUNT, rmx_hopcount);
785         setmetric(RTV_MTU, rmx_mtu);
786         setmetric(RTV_EXPIRE, rmx_expire);
787         setmetric(RTV_MSL, rmx_msl);
788         setmetric(RTV_IWMAXSEGS, rmx_iwmaxsegs);
789         setmetric(RTV_IWCAPSEGS, rmx_iwcapsegs);
790 #undef setmetric
791 }
792
793 #define ROUNDUP(a) \
794         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
795
796 /*
797  * Extract the addresses of the passed sockaddrs.
798  * Do a little sanity checking so as to avoid bad memory references.
799  * This data is derived straight from userland.
800  */
801 static int
802 rt_xaddrs(char *cp, char *cplim, struct rt_addrinfo *rtinfo)
803 {
804         struct sockaddr *sa;
805         int i;
806
807         for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
808                 if ((rtinfo->rti_addrs & (1 << i)) == 0)
809                         continue;
810                 sa = (struct sockaddr *)cp;
811                 /*
812                  * It won't fit.
813                  */
814                 if ((cp + sa->sa_len) > cplim) {
815                         return (EINVAL);
816                 }
817
818                 /*
819                  * There are no more...  Quit now.
820                  * If there are more bits, they are in error.
821                  * I've seen this.  route(1) can evidently generate these. 
822                  * This causes kernel to core dump.
823                  * For compatibility, if we see this, point to a safe address.
824                  */
825                 if (sa->sa_len == 0) {
826                         static struct sockaddr sa_zero = {
827                                 sizeof sa_zero, AF_INET,
828                         };
829
830                         rtinfo->rti_info[i] = &sa_zero;
831                         kprintf("rtsock: received more addr bits than sockaddrs.\n");
832                         return (0); /* should be EINVAL but for compat */
833                 }
834
835                 /* Accept the sockaddr. */
836                 rtinfo->rti_info[i] = sa;
837                 cp += ROUNDUP(sa->sa_len);
838         }
839         return (0);
840 }
841
842 static int
843 rt_msghdrsize(int type)
844 {
845         switch (type) {
846         case RTM_DELADDR:
847         case RTM_NEWADDR:
848                 return sizeof(struct ifa_msghdr);
849         case RTM_DELMADDR:
850         case RTM_NEWMADDR:
851                 return sizeof(struct ifma_msghdr);
852         case RTM_IFINFO:
853                 return sizeof(struct if_msghdr);
854         case RTM_IFANNOUNCE:
855         case RTM_IEEE80211:
856                 return sizeof(struct if_announcemsghdr);
857         default:
858                 return sizeof(struct rt_msghdr);
859         }
860 }
861
862 static int
863 rt_msgsize(int type, struct rt_addrinfo *rtinfo)
864 {
865         int len, i;
866
867         len = rt_msghdrsize(type);
868         for (i = 0; i < RTAX_MAX; i++) {
869                 if (rtinfo->rti_info[i] != NULL)
870                         len += ROUNDUP(rtinfo->rti_info[i]->sa_len);
871         }
872         len = ALIGN(len);
873         return len;
874 }
875
876 /*
877  * Build a routing message in a buffer.
878  * Copy the addresses in the rtinfo->rti_info[] sockaddr array
879  * to the end of the buffer after the message header.
880  *
881  * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
882  * This side-effect can be avoided if we reorder the addrs bitmask field in all
883  * the route messages to line up so we can set it here instead of back in the
884  * calling routine.
885  */
886 static void
887 rt_msg_buffer(int type, struct rt_addrinfo *rtinfo, void *buf, int msglen)
888 {
889         struct rt_msghdr *rtm;
890         char *cp;
891         int dlen, i;
892
893         rtm = (struct rt_msghdr *) buf;
894         rtm->rtm_version = RTM_VERSION;
895         rtm->rtm_type = type;
896         rtm->rtm_msglen = msglen;
897
898         cp = (char *)buf + rt_msghdrsize(type);
899         rtinfo->rti_addrs = 0;
900         for (i = 0; i < RTAX_MAX; i++) {
901                 struct sockaddr *sa;
902
903                 if ((sa = rtinfo->rti_info[i]) == NULL)
904                         continue;
905                 rtinfo->rti_addrs |= (1 << i);
906                 dlen = ROUNDUP(sa->sa_len);
907                 bcopy(sa, cp, dlen);
908                 cp += dlen;
909         }
910 }
911
912 /*
913  * Build a routing message in a mbuf chain.
914  * Copy the addresses in the rtinfo->rti_info[] sockaddr array
915  * to the end of the mbuf after the message header.
916  *
917  * Set the rtinfo->rti_addrs bitmask of addresses present in rtinfo->rti_info[].
918  * This side-effect can be avoided if we reorder the addrs bitmask field in all
919  * the route messages to line up so we can set it here instead of back in the
920  * calling routine.
921  */
922 static struct mbuf *
923 rt_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
924 {
925         struct mbuf *m;
926         struct rt_msghdr *rtm;
927         int hlen, len;
928         int i;
929
930         hlen = rt_msghdrsize(type);
931         KASSERT(hlen <= MCLBYTES, ("rt_msg_mbuf: hlen %d doesn't fit", hlen));
932
933         m = m_getl(hlen, MB_DONTWAIT, MT_DATA, M_PKTHDR, NULL);
934         if (m == NULL)
935                 return (NULL);
936         mbuftrackid(m, 32);
937         m->m_pkthdr.len = m->m_len = hlen;
938         m->m_pkthdr.rcvif = NULL;
939         rtinfo->rti_addrs = 0;
940         len = hlen;
941         for (i = 0; i < RTAX_MAX; i++) {
942                 struct sockaddr *sa;
943                 int dlen;
944
945                 if ((sa = rtinfo->rti_info[i]) == NULL)
946                         continue;
947                 rtinfo->rti_addrs |= (1 << i);
948                 dlen = ROUNDUP(sa->sa_len);
949                 m_copyback(m, len, dlen, (caddr_t)sa); /* can grow mbuf chain */
950                 len += dlen;
951         }
952         if (m->m_pkthdr.len != len) { /* one of the m_copyback() calls failed */
953                 m_freem(m);
954                 return (NULL);
955         }
956         rtm = mtod(m, struct rt_msghdr *);
957         bzero(rtm, hlen);
958         rtm->rtm_msglen = len;
959         rtm->rtm_version = RTM_VERSION;
960         rtm->rtm_type = type;
961         return (m);
962 }
963
964 /*
965  * This routine is called to generate a message from the routing
966  * socket indicating that a redirect has occurred, a routing lookup
967  * has failed, or that a protocol has detected timeouts to a particular
968  * destination.
969  */
970 void
971 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
972 {
973         struct sockaddr *dst = rtinfo->rti_info[RTAX_DST];
974         struct rt_msghdr *rtm;
975         struct mbuf *m;
976
977         if (route_cb.any_count == 0)
978                 return;
979         m = rt_msg_mbuf(type, rtinfo);
980         if (m == NULL)
981                 return;
982         rtm = mtod(m, struct rt_msghdr *);
983         rtm->rtm_flags = RTF_DONE | flags;
984         rtm->rtm_errno = error;
985         rtm->rtm_addrs = rtinfo->rti_addrs;
986         rts_input(m, familyof(dst));
987 }
988
989 void
990 rt_dstmsg(int type, struct sockaddr *dst, int error)
991 {
992         struct rt_msghdr *rtm;
993         struct rt_addrinfo addrs;
994         struct mbuf *m;
995
996         if (route_cb.any_count == 0)
997                 return;
998         bzero(&addrs, sizeof(struct rt_addrinfo));
999         addrs.rti_info[RTAX_DST] = dst;
1000         m = rt_msg_mbuf(type, &addrs);
1001         if (m == NULL)
1002                 return;
1003         rtm = mtod(m, struct rt_msghdr *);
1004         rtm->rtm_flags = RTF_DONE;
1005         rtm->rtm_errno = error;
1006         rtm->rtm_addrs = addrs.rti_addrs;
1007         rts_input(m, familyof(dst));
1008 }
1009
1010 /*
1011  * This routine is called to generate a message from the routing
1012  * socket indicating that the status of a network interface has changed.
1013  */
1014 void
1015 rt_ifmsg(struct ifnet *ifp)
1016 {
1017         struct if_msghdr *ifm;
1018         struct mbuf *m;
1019         struct rt_addrinfo rtinfo;
1020
1021         if (route_cb.any_count == 0)
1022                 return;
1023         bzero(&rtinfo, sizeof(struct rt_addrinfo));
1024         m = rt_msg_mbuf(RTM_IFINFO, &rtinfo);
1025         if (m == NULL)
1026                 return;
1027         ifm = mtod(m, struct if_msghdr *);
1028         ifm->ifm_index = ifp->if_index;
1029         ifm->ifm_flags = ifp->if_flags;
1030         ifm->ifm_data = ifp->if_data;
1031         ifm->ifm_addrs = 0;
1032         rts_input(m, 0);
1033 }
1034
1035 static void
1036 rt_ifamsg(int cmd, struct ifaddr *ifa)
1037 {
1038         struct ifa_msghdr *ifam;
1039         struct rt_addrinfo rtinfo;
1040         struct mbuf *m;
1041         struct ifnet *ifp = ifa->ifa_ifp;
1042
1043         bzero(&rtinfo, sizeof(struct rt_addrinfo));
1044         rtinfo.rti_ifaaddr = ifa->ifa_addr;
1045         rtinfo.rti_ifpaddr =
1046                 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1047         rtinfo.rti_netmask = ifa->ifa_netmask;
1048         rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1049
1050         m = rt_msg_mbuf(cmd, &rtinfo);
1051         if (m == NULL)
1052                 return;
1053
1054         ifam = mtod(m, struct ifa_msghdr *);
1055         ifam->ifam_index = ifp->if_index;
1056         ifam->ifam_metric = ifa->ifa_metric;
1057         ifam->ifam_flags = ifa->ifa_flags;
1058         ifam->ifam_addrs = rtinfo.rti_addrs;
1059
1060         rts_input(m, familyof(ifa->ifa_addr));
1061 }
1062
1063 void
1064 rt_rtmsg(int cmd, struct rtentry *rt, struct ifnet *ifp, int error)
1065 {
1066         struct rt_msghdr *rtm;
1067         struct rt_addrinfo rtinfo;
1068         struct mbuf *m;
1069         struct sockaddr *dst;
1070
1071         if (rt == NULL)
1072                 return;
1073
1074         bzero(&rtinfo, sizeof(struct rt_addrinfo));
1075         rtinfo.rti_dst = dst = rt_key(rt);
1076         rtinfo.rti_gateway = rt->rt_gateway;
1077         rtinfo.rti_netmask = rt_mask(rt);
1078         if (ifp != NULL) {
1079                 rtinfo.rti_ifpaddr =
1080                 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1081         }
1082         rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
1083
1084         m = rt_msg_mbuf(cmd, &rtinfo);
1085         if (m == NULL)
1086                 return;
1087
1088         rtm = mtod(m, struct rt_msghdr *);
1089         if (ifp != NULL)
1090                 rtm->rtm_index = ifp->if_index;
1091         rtm->rtm_flags |= rt->rt_flags;
1092         rtm->rtm_errno = error;
1093         rtm->rtm_addrs = rtinfo.rti_addrs;
1094
1095         rts_input(m, familyof(dst));
1096 }
1097
1098 /*
1099  * This is called to generate messages from the routing socket
1100  * indicating a network interface has had addresses associated with it.
1101  * if we ever reverse the logic and replace messages TO the routing
1102  * socket indicate a request to configure interfaces, then it will
1103  * be unnecessary as the routing socket will automatically generate
1104  * copies of it.
1105  */
1106 void
1107 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
1108 {
1109 #ifdef SCTP
1110         /*
1111          * notify the SCTP stack
1112          * this will only get called when an address is added/deleted
1113          * XXX pass the ifaddr struct instead if ifa->ifa_addr...
1114          */
1115         if (cmd == RTM_ADD)
1116                 sctp_add_ip_address(ifa);
1117         else if (cmd == RTM_DELETE)
1118                 sctp_delete_ip_address(ifa);
1119 #endif /* SCTP */
1120
1121         if (route_cb.any_count == 0)
1122                 return;
1123
1124         if (cmd == RTM_ADD) {
1125                 rt_ifamsg(RTM_NEWADDR, ifa);
1126                 rt_rtmsg(RTM_ADD, rt, ifa->ifa_ifp, error);
1127         } else {
1128                 KASSERT((cmd == RTM_DELETE), ("unknown cmd %d", cmd));
1129                 rt_rtmsg(RTM_DELETE, rt, ifa->ifa_ifp, error);
1130                 rt_ifamsg(RTM_DELADDR, ifa);
1131         }
1132 }
1133
1134 /*
1135  * This is the analogue to the rt_newaddrmsg which performs the same
1136  * function but for multicast group memberhips.  This is easier since
1137  * there is no route state to worry about.
1138  */
1139 void
1140 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1141 {
1142         struct rt_addrinfo rtinfo;
1143         struct mbuf *m = NULL;
1144         struct ifnet *ifp = ifma->ifma_ifp;
1145         struct ifma_msghdr *ifmam;
1146
1147         if (route_cb.any_count == 0)
1148                 return;
1149
1150         bzero(&rtinfo, sizeof(struct rt_addrinfo));
1151         rtinfo.rti_ifaaddr = ifma->ifma_addr;
1152         if (ifp != NULL && !TAILQ_EMPTY(&ifp->if_addrheads[mycpuid])) {
1153                 rtinfo.rti_ifpaddr =
1154                 TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1155         }
1156         /*
1157          * If a link-layer address is present, present it as a ``gateway''
1158          * (similarly to how ARP entries, e.g., are presented).
1159          */
1160         rtinfo.rti_gateway = ifma->ifma_lladdr;
1161
1162         m = rt_msg_mbuf(cmd, &rtinfo);
1163         if (m == NULL)
1164                 return;
1165
1166         ifmam = mtod(m, struct ifma_msghdr *);
1167         ifmam->ifmam_index = ifp->if_index;
1168         ifmam->ifmam_addrs = rtinfo.rti_addrs;
1169
1170         rts_input(m, familyof(ifma->ifma_addr));
1171 }
1172
1173 static struct mbuf *
1174 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1175                      struct rt_addrinfo *info)
1176 {
1177         struct if_announcemsghdr *ifan;
1178         struct mbuf *m;
1179
1180         if (route_cb.any_count == 0)
1181                 return NULL;
1182
1183         bzero(info, sizeof(*info));
1184         m = rt_msg_mbuf(type, info);
1185         if (m == NULL)
1186                 return NULL;
1187
1188         ifan = mtod(m, struct if_announcemsghdr *);
1189         ifan->ifan_index = ifp->if_index;
1190         strlcpy(ifan->ifan_name, ifp->if_xname, sizeof ifan->ifan_name);
1191         ifan->ifan_what = what;
1192         return m;
1193 }
1194
1195 /*
1196  * This is called to generate routing socket messages indicating
1197  * IEEE80211 wireless events.
1198  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1199  */
1200 void
1201 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1202 {
1203         struct rt_addrinfo info;
1204         struct mbuf *m;
1205
1206         m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1207         if (m == NULL)
1208                 return;
1209
1210         /*
1211          * Append the ieee80211 data.  Try to stick it in the
1212          * mbuf containing the ifannounce msg; otherwise allocate
1213          * a new mbuf and append.
1214          *
1215          * NB: we assume m is a single mbuf.
1216          */
1217         if (data_len > M_TRAILINGSPACE(m)) {
1218                 /* XXX use m_getb(data_len, MB_DONTWAIT, MT_DATA, 0); */
1219                 struct mbuf *n = m_get(MB_DONTWAIT, MT_DATA);
1220                 if (n == NULL) {
1221                         m_freem(m);
1222                         return;
1223                 }
1224                 KKASSERT(data_len <= M_TRAILINGSPACE(n));
1225                 bcopy(data, mtod(n, void *), data_len);
1226                 n->m_len = data_len;
1227                 m->m_next = n;
1228         } else if (data_len > 0) {
1229                 bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1230                 m->m_len += data_len;
1231         }
1232         mbuftrackid(m, 33);
1233         if (m->m_flags & M_PKTHDR)
1234                 m->m_pkthdr.len += data_len;
1235         mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1236         rts_input(m, 0);
1237 }
1238
1239 /*
1240  * This is called to generate routing socket messages indicating
1241  * network interface arrival and departure.
1242  */
1243 void
1244 rt_ifannouncemsg(struct ifnet *ifp, int what)
1245 {
1246         struct rt_addrinfo addrinfo;
1247         struct mbuf *m;
1248
1249         m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &addrinfo);
1250         if (m != NULL)
1251                 rts_input(m, 0);
1252 }
1253
1254 static int
1255 resizewalkarg(struct walkarg *w, int len)
1256 {
1257         void *newptr;
1258
1259         newptr = kmalloc(len, M_RTABLE, M_INTWAIT | M_NULLOK);
1260         if (newptr == NULL)
1261                 return (ENOMEM);
1262         if (w->w_tmem != NULL)
1263                 kfree(w->w_tmem, M_RTABLE);
1264         w->w_tmem = newptr;
1265         w->w_tmemsize = len;
1266         return (0);
1267 }
1268
1269 /*
1270  * This is used in dumping the kernel table via sysctl().
1271  */
1272 int
1273 sysctl_dumpentry(struct radix_node *rn, void *vw)
1274 {
1275         struct walkarg *w = vw;
1276         struct rtentry *rt = (struct rtentry *)rn;
1277         struct rt_addrinfo rtinfo;
1278         int error, msglen;
1279
1280         if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1281                 return 0;
1282
1283         bzero(&rtinfo, sizeof(struct rt_addrinfo));
1284         rtinfo.rti_dst = rt_key(rt);
1285         rtinfo.rti_gateway = rt->rt_gateway;
1286         rtinfo.rti_netmask = rt_mask(rt);
1287         rtinfo.rti_genmask = rt->rt_genmask;
1288         if (rt->rt_ifp != NULL) {
1289                 rtinfo.rti_ifpaddr =
1290                 TAILQ_FIRST(&rt->rt_ifp->if_addrheads[mycpuid])->ifa->ifa_addr;
1291                 rtinfo.rti_ifaaddr = rt->rt_ifa->ifa_addr;
1292                 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1293                         rtinfo.rti_bcastaddr = rt->rt_ifa->ifa_dstaddr;
1294         }
1295         msglen = rt_msgsize(RTM_GET, &rtinfo);
1296         if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1297                 return (ENOMEM);
1298         rt_msg_buffer(RTM_GET, &rtinfo, w->w_tmem, msglen);
1299         if (w->w_req != NULL) {
1300                 struct rt_msghdr *rtm = w->w_tmem;
1301
1302                 rtm->rtm_flags = rt->rt_flags;
1303                 rtm->rtm_use = rt->rt_use;
1304                 rtm->rtm_rmx = rt->rt_rmx;
1305                 rtm->rtm_index = rt->rt_ifp->if_index;
1306                 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1307                 rtm->rtm_addrs = rtinfo.rti_addrs;
1308                 error = SYSCTL_OUT(w->w_req, rtm, msglen);
1309                 return (error);
1310         }
1311         return (0);
1312 }
1313
1314 static int
1315 sysctl_iflist(int af, struct walkarg *w)
1316 {
1317         struct ifnet *ifp;
1318         struct rt_addrinfo rtinfo;
1319         int msglen, error;
1320
1321         bzero(&rtinfo, sizeof(struct rt_addrinfo));
1322         TAILQ_FOREACH(ifp, &ifnet, if_link) {
1323                 struct ifaddr_container *ifac;
1324                 struct ifaddr *ifa;
1325
1326                 if (w->w_arg && w->w_arg != ifp->if_index)
1327                         continue;
1328                 ifac = TAILQ_FIRST(&ifp->if_addrheads[mycpuid]);
1329                 ifa = ifac->ifa;
1330                 rtinfo.rti_ifpaddr = ifa->ifa_addr;
1331                 msglen = rt_msgsize(RTM_IFINFO, &rtinfo);
1332                 if (w->w_tmemsize < msglen && resizewalkarg(w, msglen) != 0)
1333                         return (ENOMEM);
1334                 rt_msg_buffer(RTM_IFINFO, &rtinfo, w->w_tmem, msglen);
1335                 rtinfo.rti_ifpaddr = NULL;
1336                 if (w->w_req != NULL && w->w_tmem != NULL) {
1337                         struct if_msghdr *ifm = w->w_tmem;
1338
1339                         ifm->ifm_index = ifp->if_index;
1340                         ifm->ifm_flags = ifp->if_flags;
1341                         ifm->ifm_data = ifp->if_data;
1342                         ifm->ifm_addrs = rtinfo.rti_addrs;
1343                         error = SYSCTL_OUT(w->w_req, ifm, msglen);
1344                         if (error)
1345                                 return (error);
1346                 }
1347                 while ((ifac = TAILQ_NEXT(ifac, ifa_link)) != NULL) {
1348                         ifa = ifac->ifa;
1349
1350                         if (af && af != ifa->ifa_addr->sa_family)
1351                                 continue;
1352                         if (curproc->p_ucred->cr_prison &&
1353                             prison_if(curproc->p_ucred, ifa->ifa_addr))
1354                                 continue;
1355                         rtinfo.rti_ifaaddr = ifa->ifa_addr;
1356                         rtinfo.rti_netmask = ifa->ifa_netmask;
1357                         rtinfo.rti_bcastaddr = ifa->ifa_dstaddr;
1358                         msglen = rt_msgsize(RTM_NEWADDR, &rtinfo);
1359                         if (w->w_tmemsize < msglen &&
1360                             resizewalkarg(w, msglen) != 0)
1361                                 return (ENOMEM);
1362                         rt_msg_buffer(RTM_NEWADDR, &rtinfo, w->w_tmem, msglen);
1363                         if (w->w_req != NULL) {
1364                                 struct ifa_msghdr *ifam = w->w_tmem;
1365
1366                                 ifam->ifam_index = ifa->ifa_ifp->if_index;
1367                                 ifam->ifam_flags = ifa->ifa_flags;
1368                                 ifam->ifam_metric = ifa->ifa_metric;
1369                                 ifam->ifam_addrs = rtinfo.rti_addrs;
1370                                 error = SYSCTL_OUT(w->w_req, w->w_tmem, msglen);
1371                                 if (error)
1372                                         return (error);
1373                         }
1374                 }
1375                 rtinfo.rti_netmask = NULL;
1376                 rtinfo.rti_ifaaddr = NULL;
1377                 rtinfo.rti_bcastaddr = NULL;
1378         }
1379         return (0);
1380 }
1381
1382 static int
1383 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1384 {
1385         int     *name = (int *)arg1;
1386         u_int   namelen = arg2;
1387         struct radix_node_head *rnh;
1388         int     i, error = EINVAL;
1389         int     origcpu;
1390         u_char  af;
1391         struct  walkarg w;
1392
1393         name ++;
1394         namelen--;
1395         if (req->newptr)
1396                 return (EPERM);
1397         if (namelen != 3 && namelen != 4)
1398                 return (EINVAL);
1399         af = name[0];
1400         bzero(&w, sizeof w);
1401         w.w_op = name[1];
1402         w.w_arg = name[2];
1403         w.w_req = req;
1404
1405         /*
1406          * Optional third argument specifies cpu, used primarily for
1407          * debugging the route table.
1408          */
1409         if (namelen == 4) {
1410                 if (name[3] < 0 || name[3] >= ncpus)
1411                         return (EINVAL);
1412                 origcpu = mycpuid;
1413                 lwkt_migratecpu(name[3]);
1414         } else {
1415                 origcpu = -1;
1416         }
1417         crit_enter();
1418         switch (w.w_op) {
1419         case NET_RT_DUMP:
1420         case NET_RT_FLAGS:
1421                 for (i = 1; i <= AF_MAX; i++)
1422                         if ((rnh = rt_tables[mycpuid][i]) &&
1423                             (af == 0 || af == i) &&
1424                             (error = rnh->rnh_walktree(rnh,
1425                                                        sysctl_dumpentry, &w)))
1426                                 break;
1427                 break;
1428
1429         case NET_RT_IFLIST:
1430                 error = sysctl_iflist(af, &w);
1431         }
1432         crit_exit();
1433         if (w.w_tmem != NULL)
1434                 kfree(w.w_tmem, M_RTABLE);
1435         if (origcpu >= 0)
1436                 lwkt_migratecpu(origcpu);
1437         return (error);
1438 }
1439
1440 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1441
1442 /*
1443  * Definitions of protocols supported in the ROUTE domain.
1444  */
1445
1446 static struct domain routedomain;               /* or at least forward */
1447
1448 static struct protosw routesw[] = {
1449     {
1450         .pr_type = SOCK_RAW,
1451         .pr_domain = &routedomain,
1452         .pr_protocol = 0,
1453         .pr_flags = PR_ATOMIC|PR_ADDR,
1454         .pr_input = NULL,
1455         .pr_output = route_output,
1456         .pr_ctlinput = raw_ctlinput,
1457         .pr_ctloutput = NULL,
1458         .pr_ctlport = cpu0_ctlport,
1459
1460         .pr_init = raw_init,
1461         .pr_usrreqs = &route_usrreqs
1462     }
1463 };
1464
1465 static struct domain routedomain = {
1466         PF_ROUTE, "route", NULL, NULL, NULL,
1467         routesw, &routesw[(sizeof routesw)/(sizeof routesw[0])],
1468 };
1469
1470 DOMAIN_SET(route);
1471