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