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