]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/rtsock.c
Merge lld trunk r321017 to contrib/llvm/tools/lld.
[FreeBSD/FreeBSD.git] / sys / net / rtsock.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1988, 1991, 1993
5  *      The Regents of the University of California.  All rights reserved.
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 University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      @(#)rtsock.c    8.7 (Berkeley) 10/12/95
32  * $FreeBSD$
33  */
34 #include "opt_compat.h"
35 #include "opt_mpath.h"
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38
39 #include <sys/param.h>
40 #include <sys/jail.h>
41 #include <sys/kernel.h>
42 #include <sys/domain.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/protosw.h>
49 #include <sys/rwlock.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
55
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_dl.h>
59 #include <net/if_llatbl.h>
60 #include <net/if_types.h>
61 #include <net/netisr.h>
62 #include <net/raw_cb.h>
63 #include <net/route.h>
64 #include <net/route_var.h>
65 #include <net/vnet.h>
66
67 #include <netinet/in.h>
68 #include <netinet/if_ether.h>
69 #include <netinet/ip_carp.h>
70 #ifdef INET6
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/scope6_var.h>
73 #endif
74
75 #ifdef COMPAT_FREEBSD32
76 #include <sys/mount.h>
77 #include <compat/freebsd32/freebsd32.h>
78
79 struct if_msghdr32 {
80         uint16_t ifm_msglen;
81         uint8_t ifm_version;
82         uint8_t ifm_type;
83         int32_t ifm_addrs;
84         int32_t ifm_flags;
85         uint16_t ifm_index;
86         struct  if_data ifm_data;
87 };
88
89 struct if_msghdrl32 {
90         uint16_t ifm_msglen;
91         uint8_t ifm_version;
92         uint8_t ifm_type;
93         int32_t ifm_addrs;
94         int32_t ifm_flags;
95         uint16_t ifm_index;
96         uint16_t _ifm_spare1;
97         uint16_t ifm_len;
98         uint16_t ifm_data_off;
99         struct  if_data ifm_data;
100 };
101
102 struct ifa_msghdrl32 {
103         uint16_t ifam_msglen;
104         uint8_t ifam_version;
105         uint8_t ifam_type;
106         int32_t ifam_addrs;
107         int32_t ifam_flags;
108         uint16_t ifam_index;
109         uint16_t _ifam_spare1;
110         uint16_t ifam_len;
111         uint16_t ifam_data_off;
112         int32_t ifam_metric;
113         struct  if_data ifam_data;
114 };
115 #endif /* COMPAT_FREEBSD32 */
116
117 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
118
119 /* NB: these are not modified */
120 static struct   sockaddr route_src = { 2, PF_ROUTE, };
121 static struct   sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, };
122
123 /* These are external hooks for CARP. */
124 int     (*carp_get_vhid_p)(struct ifaddr *);
125
126 /*
127  * Used by rtsock/raw_input callback code to decide whether to filter the update
128  * notification to a socket bound to a particular FIB.
129  */
130 #define RTS_FILTER_FIB  M_PROTO8
131
132 typedef struct {
133         int     ip_count;       /* attached w/ AF_INET */
134         int     ip6_count;      /* attached w/ AF_INET6 */
135         int     any_count;      /* total attached */
136 } route_cb_t;
137 static VNET_DEFINE(route_cb_t, route_cb);
138 #define V_route_cb VNET(route_cb)
139
140 struct mtx rtsock_mtx;
141 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
142
143 #define RTSOCK_LOCK()   mtx_lock(&rtsock_mtx)
144 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
145 #define RTSOCK_LOCK_ASSERT()    mtx_assert(&rtsock_mtx, MA_OWNED)
146
147 static SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
148
149 struct walkarg {
150         int     w_tmemsize;
151         int     w_op, w_arg;
152         caddr_t w_tmem;
153         struct sysctl_req *w_req;
154 };
155
156 static void     rts_input(struct mbuf *m);
157 static struct mbuf *rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo);
158 static int      rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo,
159                         struct walkarg *w, int *plen);
160 static int      rt_xaddrs(caddr_t cp, caddr_t cplim,
161                         struct rt_addrinfo *rtinfo);
162 static int      sysctl_dumpentry(struct radix_node *rn, void *vw);
163 static int      sysctl_iflist(int af, struct walkarg *w);
164 static int      sysctl_ifmalist(int af, struct walkarg *w);
165 static int      route_output(struct mbuf *m, struct socket *so, ...);
166 static void     rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out);
167 static void     rt_dispatch(struct mbuf *, sa_family_t);
168 static struct sockaddr  *rtsock_fix_netmask(struct sockaddr *dst,
169                         struct sockaddr *smask, struct sockaddr_storage *dmask);
170
171 static struct netisr_handler rtsock_nh = {
172         .nh_name = "rtsock",
173         .nh_handler = rts_input,
174         .nh_proto = NETISR_ROUTE,
175         .nh_policy = NETISR_POLICY_SOURCE,
176 };
177
178 static int
179 sysctl_route_netisr_maxqlen(SYSCTL_HANDLER_ARGS)
180 {
181         int error, qlimit;
182
183         netisr_getqlimit(&rtsock_nh, &qlimit);
184         error = sysctl_handle_int(oidp, &qlimit, 0, req);
185         if (error || !req->newptr)
186                 return (error);
187         if (qlimit < 1)
188                 return (EINVAL);
189         return (netisr_setqlimit(&rtsock_nh, qlimit));
190 }
191 SYSCTL_PROC(_net_route, OID_AUTO, netisr_maxqlen, CTLTYPE_INT|CTLFLAG_RW,
192     0, 0, sysctl_route_netisr_maxqlen, "I",
193     "maximum routing socket dispatch queue length");
194
195 static void
196 vnet_rts_init(void)
197 {
198         int tmp;
199
200         if (IS_DEFAULT_VNET(curvnet)) {
201                 if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
202                         rtsock_nh.nh_qlimit = tmp;
203                 netisr_register(&rtsock_nh);
204         }
205 #ifdef VIMAGE
206          else
207                 netisr_register_vnet(&rtsock_nh);
208 #endif
209 }
210 VNET_SYSINIT(vnet_rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
211     vnet_rts_init, 0);
212
213 #ifdef VIMAGE
214 static void
215 vnet_rts_uninit(void)
216 {
217
218         netisr_unregister_vnet(&rtsock_nh);
219 }
220 VNET_SYSUNINIT(vnet_rts_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD,
221     vnet_rts_uninit, 0);
222 #endif
223
224 static int
225 raw_input_rts_cb(struct mbuf *m, struct sockproto *proto, struct sockaddr *src,
226     struct rawcb *rp)
227 {
228         int fibnum;
229
230         KASSERT(m != NULL, ("%s: m is NULL", __func__));
231         KASSERT(proto != NULL, ("%s: proto is NULL", __func__));
232         KASSERT(rp != NULL, ("%s: rp is NULL", __func__));
233
234         /* No filtering requested. */
235         if ((m->m_flags & RTS_FILTER_FIB) == 0)
236                 return (0);
237
238         /* Check if it is a rts and the fib matches the one of the socket. */
239         fibnum = M_GETFIB(m);
240         if (proto->sp_family != PF_ROUTE ||
241             rp->rcb_socket == NULL ||
242             rp->rcb_socket->so_fibnum == fibnum)
243                 return (0);
244
245         /* Filtering requested and no match, the socket shall be skipped. */
246         return (1);
247 }
248
249 static void
250 rts_input(struct mbuf *m)
251 {
252         struct sockproto route_proto;
253         unsigned short *family;
254         struct m_tag *tag;
255
256         route_proto.sp_family = PF_ROUTE;
257         tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
258         if (tag != NULL) {
259                 family = (unsigned short *)(tag + 1);
260                 route_proto.sp_protocol = *family;
261                 m_tag_delete(m, tag);
262         } else
263                 route_proto.sp_protocol = 0;
264
265         raw_input_ext(m, &route_proto, &route_src, raw_input_rts_cb);
266 }
267
268 /*
269  * It really doesn't make any sense at all for this code to share much
270  * with raw_usrreq.c, since its functionality is so restricted.  XXX
271  */
272 static void
273 rts_abort(struct socket *so)
274 {
275
276         raw_usrreqs.pru_abort(so);
277 }
278
279 static void
280 rts_close(struct socket *so)
281 {
282
283         raw_usrreqs.pru_close(so);
284 }
285
286 /* pru_accept is EOPNOTSUPP */
287
288 static int
289 rts_attach(struct socket *so, int proto, struct thread *td)
290 {
291         struct rawcb *rp;
292         int error;
293
294         KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
295
296         /* XXX */
297         rp = malloc(sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
298
299         so->so_pcb = (caddr_t)rp;
300         so->so_fibnum = td->td_proc->p_fibnum;
301         error = raw_attach(so, proto);
302         rp = sotorawcb(so);
303         if (error) {
304                 so->so_pcb = NULL;
305                 free(rp, M_PCB);
306                 return error;
307         }
308         RTSOCK_LOCK();
309         switch(rp->rcb_proto.sp_protocol) {
310         case AF_INET:
311                 V_route_cb.ip_count++;
312                 break;
313         case AF_INET6:
314                 V_route_cb.ip6_count++;
315                 break;
316         }
317         V_route_cb.any_count++;
318         RTSOCK_UNLOCK();
319         soisconnected(so);
320         so->so_options |= SO_USELOOPBACK;
321         return 0;
322 }
323
324 static int
325 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
326 {
327
328         return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
329 }
330
331 static int
332 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
333 {
334
335         return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
336 }
337
338 /* pru_connect2 is EOPNOTSUPP */
339 /* pru_control is EOPNOTSUPP */
340
341 static void
342 rts_detach(struct socket *so)
343 {
344         struct rawcb *rp = sotorawcb(so);
345
346         KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
347
348         RTSOCK_LOCK();
349         switch(rp->rcb_proto.sp_protocol) {
350         case AF_INET:
351                 V_route_cb.ip_count--;
352                 break;
353         case AF_INET6:
354                 V_route_cb.ip6_count--;
355                 break;
356         }
357         V_route_cb.any_count--;
358         RTSOCK_UNLOCK();
359         raw_usrreqs.pru_detach(so);
360 }
361
362 static int
363 rts_disconnect(struct socket *so)
364 {
365
366         return (raw_usrreqs.pru_disconnect(so));
367 }
368
369 /* pru_listen is EOPNOTSUPP */
370
371 static int
372 rts_peeraddr(struct socket *so, struct sockaddr **nam)
373 {
374
375         return (raw_usrreqs.pru_peeraddr(so, nam));
376 }
377
378 /* pru_rcvd is EOPNOTSUPP */
379 /* pru_rcvoob is EOPNOTSUPP */
380
381 static int
382 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
383          struct mbuf *control, struct thread *td)
384 {
385
386         return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
387 }
388
389 /* pru_sense is null */
390
391 static int
392 rts_shutdown(struct socket *so)
393 {
394
395         return (raw_usrreqs.pru_shutdown(so));
396 }
397
398 static int
399 rts_sockaddr(struct socket *so, struct sockaddr **nam)
400 {
401
402         return (raw_usrreqs.pru_sockaddr(so, nam));
403 }
404
405 static struct pr_usrreqs route_usrreqs = {
406         .pru_abort =            rts_abort,
407         .pru_attach =           rts_attach,
408         .pru_bind =             rts_bind,
409         .pru_connect =          rts_connect,
410         .pru_detach =           rts_detach,
411         .pru_disconnect =       rts_disconnect,
412         .pru_peeraddr =         rts_peeraddr,
413         .pru_send =             rts_send,
414         .pru_shutdown =         rts_shutdown,
415         .pru_sockaddr =         rts_sockaddr,
416         .pru_close =            rts_close,
417 };
418
419 #ifndef _SOCKADDR_UNION_DEFINED
420 #define _SOCKADDR_UNION_DEFINED
421 /*
422  * The union of all possible address formats we handle.
423  */
424 union sockaddr_union {
425         struct sockaddr         sa;
426         struct sockaddr_in      sin;
427         struct sockaddr_in6     sin6;
428 };
429 #endif /* _SOCKADDR_UNION_DEFINED */
430
431 static int
432 rtm_get_jailed(struct rt_addrinfo *info, struct ifnet *ifp,
433     struct rtentry *rt, union sockaddr_union *saun, struct ucred *cred)
434 {
435
436         /* First, see if the returned address is part of the jail. */
437         if (prison_if(cred, rt->rt_ifa->ifa_addr) == 0) {
438                 info->rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
439                 return (0);
440         }
441
442         switch (info->rti_info[RTAX_DST]->sa_family) {
443 #ifdef INET
444         case AF_INET:
445         {
446                 struct in_addr ia;
447                 struct ifaddr *ifa;
448                 int found;
449
450                 found = 0;
451                 /*
452                  * Try to find an address on the given outgoing interface
453                  * that belongs to the jail.
454                  */
455                 IF_ADDR_RLOCK(ifp);
456                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
457                         struct sockaddr *sa;
458                         sa = ifa->ifa_addr;
459                         if (sa->sa_family != AF_INET)
460                                 continue;
461                         ia = ((struct sockaddr_in *)sa)->sin_addr;
462                         if (prison_check_ip4(cred, &ia) == 0) {
463                                 found = 1;
464                                 break;
465                         }
466                 }
467                 IF_ADDR_RUNLOCK(ifp);
468                 if (!found) {
469                         /*
470                          * As a last resort return the 'default' jail address.
471                          */
472                         ia = ((struct sockaddr_in *)rt->rt_ifa->ifa_addr)->
473                             sin_addr;
474                         if (prison_get_ip4(cred, &ia) != 0)
475                                 return (ESRCH);
476                 }
477                 bzero(&saun->sin, sizeof(struct sockaddr_in));
478                 saun->sin.sin_len = sizeof(struct sockaddr_in);
479                 saun->sin.sin_family = AF_INET;
480                 saun->sin.sin_addr.s_addr = ia.s_addr;
481                 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin;
482                 break;
483         }
484 #endif
485 #ifdef INET6
486         case AF_INET6:
487         {
488                 struct in6_addr ia6;
489                 struct ifaddr *ifa;
490                 int found;
491
492                 found = 0;
493                 /*
494                  * Try to find an address on the given outgoing interface
495                  * that belongs to the jail.
496                  */
497                 IF_ADDR_RLOCK(ifp);
498                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
499                         struct sockaddr *sa;
500                         sa = ifa->ifa_addr;
501                         if (sa->sa_family != AF_INET6)
502                                 continue;
503                         bcopy(&((struct sockaddr_in6 *)sa)->sin6_addr,
504                             &ia6, sizeof(struct in6_addr));
505                         if (prison_check_ip6(cred, &ia6) == 0) {
506                                 found = 1;
507                                 break;
508                         }
509                 }
510                 IF_ADDR_RUNLOCK(ifp);
511                 if (!found) {
512                         /*
513                          * As a last resort return the 'default' jail address.
514                          */
515                         ia6 = ((struct sockaddr_in6 *)rt->rt_ifa->ifa_addr)->
516                             sin6_addr;
517                         if (prison_get_ip6(cred, &ia6) != 0)
518                                 return (ESRCH);
519                 }
520                 bzero(&saun->sin6, sizeof(struct sockaddr_in6));
521                 saun->sin6.sin6_len = sizeof(struct sockaddr_in6);
522                 saun->sin6.sin6_family = AF_INET6;
523                 bcopy(&ia6, &saun->sin6.sin6_addr, sizeof(struct in6_addr));
524                 if (sa6_recoverscope(&saun->sin6) != 0)
525                         return (ESRCH);
526                 info->rti_info[RTAX_IFA] = (struct sockaddr *)&saun->sin6;
527                 break;
528         }
529 #endif
530         default:
531                 return (ESRCH);
532         }
533         return (0);
534 }
535
536 /*ARGSUSED*/
537 static int
538 route_output(struct mbuf *m, struct socket *so, ...)
539 {
540         struct rt_msghdr *rtm = NULL;
541         struct rtentry *rt = NULL;
542         struct rib_head *rnh;
543         struct rt_addrinfo info;
544         struct sockaddr_storage ss;
545 #ifdef INET6
546         struct sockaddr_in6 *sin6;
547         int i, rti_need_deembed = 0;
548 #endif
549         int alloc_len = 0, len, error = 0, fibnum;
550         struct ifnet *ifp = NULL;
551         union sockaddr_union saun;
552         sa_family_t saf = AF_UNSPEC;
553         struct rawcb *rp = NULL;
554         struct walkarg w;
555
556         fibnum = so->so_fibnum;
557
558 #define senderr(e) { error = e; goto flush;}
559         if (m == NULL || ((m->m_len < sizeof(long)) &&
560                        (m = m_pullup(m, sizeof(long))) == NULL))
561                 return (ENOBUFS);
562         if ((m->m_flags & M_PKTHDR) == 0)
563                 panic("route_output");
564         len = m->m_pkthdr.len;
565         if (len < sizeof(*rtm) ||
566             len != mtod(m, struct rt_msghdr *)->rtm_msglen)
567                 senderr(EINVAL);
568
569         /*
570          * Most of current messages are in range 200-240 bytes,
571          * minimize possible re-allocation on reply using larger size
572          * buffer aligned on 1k boundaty.
573          */
574         alloc_len = roundup2(len, 1024);
575         if ((rtm = malloc(alloc_len, M_TEMP, M_NOWAIT)) == NULL)
576                 senderr(ENOBUFS);
577
578         m_copydata(m, 0, len, (caddr_t)rtm);
579         bzero(&info, sizeof(info));
580         bzero(&w, sizeof(w));
581
582         if (rtm->rtm_version != RTM_VERSION) {
583                 /* Do not touch message since format is unknown */
584                 free(rtm, M_TEMP);
585                 rtm = NULL;
586                 senderr(EPROTONOSUPPORT);
587         }
588
589         /*
590          * Starting from here, it is possible
591          * to alter original message and insert
592          * caller PID and error value.
593          */
594
595         rtm->rtm_pid = curproc->p_pid;
596         info.rti_addrs = rtm->rtm_addrs;
597
598         info.rti_mflags = rtm->rtm_inits;
599         info.rti_rmx = &rtm->rtm_rmx;
600
601         /*
602          * rt_xaddrs() performs s6_addr[2] := sin6_scope_id for AF_INET6
603          * link-local address because rtrequest requires addresses with
604          * embedded scope id.
605          */
606         if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info))
607                 senderr(EINVAL);
608
609         info.rti_flags = rtm->rtm_flags;
610         if (info.rti_info[RTAX_DST] == NULL ||
611             info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
612             (info.rti_info[RTAX_GATEWAY] != NULL &&
613              info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
614                 senderr(EINVAL);
615         saf = info.rti_info[RTAX_DST]->sa_family;
616         /*
617          * Verify that the caller has the appropriate privilege; RTM_GET
618          * is the only operation the non-superuser is allowed.
619          */
620         if (rtm->rtm_type != RTM_GET) {
621                 error = priv_check(curthread, PRIV_NET_ROUTE);
622                 if (error)
623                         senderr(error);
624         }
625
626         /*
627          * The given gateway address may be an interface address.
628          * For example, issuing a "route change" command on a route
629          * entry that was created from a tunnel, and the gateway
630          * address given is the local end point. In this case the 
631          * RTF_GATEWAY flag must be cleared or the destination will
632          * not be reachable even though there is no error message.
633          */
634         if (info.rti_info[RTAX_GATEWAY] != NULL &&
635             info.rti_info[RTAX_GATEWAY]->sa_family != AF_LINK) {
636                 struct rt_addrinfo ginfo;
637                 struct sockaddr *gdst;
638
639                 bzero(&ginfo, sizeof(ginfo));
640                 bzero(&ss, sizeof(ss));
641                 ss.ss_len = sizeof(ss);
642
643                 ginfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&ss;
644                 gdst = info.rti_info[RTAX_GATEWAY];
645
646                 /* 
647                  * A host route through the loopback interface is 
648                  * installed for each interface adddress. In pre 8.0
649                  * releases the interface address of a PPP link type
650                  * is not reachable locally. This behavior is fixed as 
651                  * part of the new L2/L3 redesign and rewrite work. The
652                  * signature of this interface address route is the
653                  * AF_LINK sa_family type of the rt_gateway, and the
654                  * rt_ifp has the IFF_LOOPBACK flag set.
655                  */
656                 if (rib_lookup_info(fibnum, gdst, NHR_REF, 0, &ginfo) == 0) {
657                         if (ss.ss_family == AF_LINK &&
658                             ginfo.rti_ifp->if_flags & IFF_LOOPBACK) {
659                                 info.rti_flags &= ~RTF_GATEWAY;
660                                 info.rti_flags |= RTF_GWFLAG_COMPAT;
661                         }
662                         rib_free_info(&ginfo);
663                 }
664         }
665
666         switch (rtm->rtm_type) {
667                 struct rtentry *saved_nrt;
668
669         case RTM_ADD:
670         case RTM_CHANGE:
671                 if (info.rti_info[RTAX_GATEWAY] == NULL)
672                         senderr(EINVAL);
673                 saved_nrt = NULL;
674
675                 /* support for new ARP code */
676                 if (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK &&
677                     (rtm->rtm_flags & RTF_LLDATA) != 0) {
678                         error = lla_rt_output(rtm, &info);
679 #ifdef INET6
680                         if (error == 0)
681                                 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
682 #endif
683                         break;
684                 }
685                 error = rtrequest1_fib(rtm->rtm_type, &info, &saved_nrt,
686                     fibnum);
687                 if (error == 0 && saved_nrt != NULL) {
688 #ifdef INET6
689                         rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
690 #endif
691                         RT_LOCK(saved_nrt);
692                         rtm->rtm_index = saved_nrt->rt_ifp->if_index;
693                         RT_REMREF(saved_nrt);
694                         RT_UNLOCK(saved_nrt);
695                 }
696                 break;
697
698         case RTM_DELETE:
699                 saved_nrt = NULL;
700                 /* support for new ARP code */
701                 if (info.rti_info[RTAX_GATEWAY] && 
702                     (info.rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) &&
703                     (rtm->rtm_flags & RTF_LLDATA) != 0) {
704                         error = lla_rt_output(rtm, &info);
705 #ifdef INET6
706                         if (error == 0)
707                                 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
708 #endif
709                         break;
710                 }
711                 error = rtrequest1_fib(RTM_DELETE, &info, &saved_nrt, fibnum);
712                 if (error == 0) {
713                         RT_LOCK(saved_nrt);
714                         rt = saved_nrt;
715                         goto report;
716                 }
717 #ifdef INET6
718                 /* rt_msg2() will not be used when RTM_DELETE fails. */
719                 rti_need_deembed = (V_deembed_scopeid) ? 1 : 0;
720 #endif
721                 break;
722
723         case RTM_GET:
724                 rnh = rt_tables_get_rnh(fibnum, saf);
725                 if (rnh == NULL)
726                         senderr(EAFNOSUPPORT);
727
728                 RIB_RLOCK(rnh);
729
730                 if (info.rti_info[RTAX_NETMASK] == NULL &&
731                     rtm->rtm_type == RTM_GET) {
732                         /*
733                          * Provide longest prefix match for
734                          * address lookup (no mask).
735                          * 'route -n get addr'
736                          */
737                         rt = (struct rtentry *) rnh->rnh_matchaddr(
738                             info.rti_info[RTAX_DST], &rnh->head);
739                 } else
740                         rt = (struct rtentry *) rnh->rnh_lookup(
741                             info.rti_info[RTAX_DST],
742                             info.rti_info[RTAX_NETMASK], &rnh->head);
743
744                 if (rt == NULL) {
745                         RIB_RUNLOCK(rnh);
746                         senderr(ESRCH);
747                 }
748 #ifdef RADIX_MPATH
749                 /*
750                  * for RTM_CHANGE/LOCK, if we got multipath routes,
751                  * we require users to specify a matching RTAX_GATEWAY.
752                  *
753                  * for RTM_GET, gate is optional even with multipath.
754                  * if gate == NULL the first match is returned.
755                  * (no need to call rt_mpath_matchgate if gate == NULL)
756                  */
757                 if (rt_mpath_capable(rnh) &&
758                     (rtm->rtm_type != RTM_GET || info.rti_info[RTAX_GATEWAY])) {
759                         rt = rt_mpath_matchgate(rt, info.rti_info[RTAX_GATEWAY]);
760                         if (!rt) {
761                                 RIB_RUNLOCK(rnh);
762                                 senderr(ESRCH);
763                         }
764                 }
765 #endif
766                 /*
767                  * If performing proxied L2 entry insertion, and
768                  * the actual PPP host entry is found, perform
769                  * another search to retrieve the prefix route of
770                  * the local end point of the PPP link.
771                  */
772                 if (rtm->rtm_flags & RTF_ANNOUNCE) {
773                         struct sockaddr laddr;
774
775                         if (rt->rt_ifp != NULL && 
776                             rt->rt_ifp->if_type == IFT_PROPVIRTUAL) {
777                                 struct ifaddr *ifa;
778
779                                 ifa = ifa_ifwithnet(info.rti_info[RTAX_DST], 1,
780                                                 RT_ALL_FIBS);
781                                 if (ifa != NULL)
782                                         rt_maskedcopy(ifa->ifa_addr,
783                                                       &laddr,
784                                                       ifa->ifa_netmask);
785                         } else
786                                 rt_maskedcopy(rt->rt_ifa->ifa_addr,
787                                               &laddr,
788                                               rt->rt_ifa->ifa_netmask);
789                         /* 
790                          * refactor rt and no lock operation necessary
791                          */
792                         rt = (struct rtentry *)rnh->rnh_matchaddr(&laddr,
793                             &rnh->head);
794                         if (rt == NULL) {
795                                 RIB_RUNLOCK(rnh);
796                                 senderr(ESRCH);
797                         }
798                 } 
799                 RT_LOCK(rt);
800                 RT_ADDREF(rt);
801                 RIB_RUNLOCK(rnh);
802
803 report:
804                 RT_LOCK_ASSERT(rt);
805                 if ((rt->rt_flags & RTF_HOST) == 0
806                     ? jailed_without_vnet(curthread->td_ucred)
807                     : prison_if(curthread->td_ucred,
808                     rt_key(rt)) != 0) {
809                         RT_UNLOCK(rt);
810                         senderr(ESRCH);
811                 }
812                 info.rti_info[RTAX_DST] = rt_key(rt);
813                 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
814                 info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
815                     rt_mask(rt), &ss);
816                 info.rti_info[RTAX_GENMASK] = 0;
817                 if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
818                         ifp = rt->rt_ifp;
819                         if (ifp) {
820                                 info.rti_info[RTAX_IFP] =
821                                     ifp->if_addr->ifa_addr;
822                                 error = rtm_get_jailed(&info, ifp, rt,
823                                     &saun, curthread->td_ucred);
824                                 if (error != 0) {
825                                         RT_UNLOCK(rt);
826                                         senderr(error);
827                                 }
828                                 if (ifp->if_flags & IFF_POINTOPOINT)
829                                         info.rti_info[RTAX_BRD] =
830                                             rt->rt_ifa->ifa_dstaddr;
831                                 rtm->rtm_index = ifp->if_index;
832                         } else {
833                                 info.rti_info[RTAX_IFP] = NULL;
834                                 info.rti_info[RTAX_IFA] = NULL;
835                         }
836                 } else if ((ifp = rt->rt_ifp) != NULL) {
837                         rtm->rtm_index = ifp->if_index;
838                 }
839
840                 /* Check if we need to realloc storage */
841                 rtsock_msg_buffer(rtm->rtm_type, &info, NULL, &len);
842                 if (len > alloc_len) {
843                         struct rt_msghdr *new_rtm;
844                         new_rtm = malloc(len, M_TEMP, M_NOWAIT);
845                         if (new_rtm == NULL) {
846                                 RT_UNLOCK(rt);
847                                 senderr(ENOBUFS);
848                         }
849                         bcopy(rtm, new_rtm, rtm->rtm_msglen);
850                         free(rtm, M_TEMP);
851                         rtm = new_rtm;
852                         alloc_len = len;
853                 }
854
855                 w.w_tmem = (caddr_t)rtm;
856                 w.w_tmemsize = alloc_len;
857                 rtsock_msg_buffer(rtm->rtm_type, &info, &w, &len);
858
859                 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
860                         rtm->rtm_flags = RTF_GATEWAY | 
861                                 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
862                 else
863                         rtm->rtm_flags = rt->rt_flags;
864                 rt_getmetrics(rt, &rtm->rtm_rmx);
865                 rtm->rtm_addrs = info.rti_addrs;
866
867                 RT_UNLOCK(rt);
868                 break;
869
870         default:
871                 senderr(EOPNOTSUPP);
872         }
873
874 flush:
875         if (rt != NULL)
876                 RTFREE(rt);
877         /*
878          * Check to see if we don't want our own messages.
879          */
880         if ((so->so_options & SO_USELOOPBACK) == 0) {
881                 if (V_route_cb.any_count <= 1) {
882                         if (rtm != NULL)
883                                 free(rtm, M_TEMP);
884                         m_freem(m);
885                         return (error);
886                 }
887                 /* There is another listener, so construct message */
888                 rp = sotorawcb(so);
889         }
890
891         if (rtm != NULL) {
892 #ifdef INET6
893                 if (rti_need_deembed) {
894                         /* sin6_scope_id is recovered before sending rtm. */
895                         sin6 = (struct sockaddr_in6 *)&ss;
896                         for (i = 0; i < RTAX_MAX; i++) {
897                                 if (info.rti_info[i] == NULL)
898                                         continue;
899                                 if (info.rti_info[i]->sa_family != AF_INET6)
900                                         continue;
901                                 bcopy(info.rti_info[i], sin6, sizeof(*sin6));
902                                 if (sa6_recoverscope(sin6) == 0)
903                                         bcopy(sin6, info.rti_info[i],
904                                                     sizeof(*sin6));
905                         }
906                 }
907 #endif
908                 if (error != 0)
909                         rtm->rtm_errno = error;
910                 else
911                         rtm->rtm_flags |= RTF_DONE;
912
913                 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
914                 if (m->m_pkthdr.len < rtm->rtm_msglen) {
915                         m_freem(m);
916                         m = NULL;
917                 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
918                         m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
919
920                 free(rtm, M_TEMP);
921         }
922         if (m != NULL) {
923                 M_SETFIB(m, fibnum);
924                 m->m_flags |= RTS_FILTER_FIB;
925                 if (rp) {
926                         /*
927                          * XXX insure we don't get a copy by
928                          * invalidating our protocol
929                          */
930                         unsigned short family = rp->rcb_proto.sp_family;
931                         rp->rcb_proto.sp_family = 0;
932                         rt_dispatch(m, saf);
933                         rp->rcb_proto.sp_family = family;
934                 } else
935                         rt_dispatch(m, saf);
936         }
937
938         return (error);
939 }
940
941 static void
942 rt_getmetrics(const struct rtentry *rt, struct rt_metrics *out)
943 {
944
945         bzero(out, sizeof(*out));
946         out->rmx_mtu = rt->rt_mtu;
947         out->rmx_weight = rt->rt_weight;
948         out->rmx_pksent = counter_u64_fetch(rt->rt_pksent);
949         /* Kernel -> userland timebase conversion. */
950         out->rmx_expire = rt->rt_expire ?
951             rt->rt_expire - time_uptime + time_second : 0;
952 }
953
954 /*
955  * Extract the addresses of the passed sockaddrs.
956  * Do a little sanity checking so as to avoid bad memory references.
957  * This data is derived straight from userland.
958  */
959 static int
960 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
961 {
962         struct sockaddr *sa;
963         int i;
964
965         for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
966                 if ((rtinfo->rti_addrs & (1 << i)) == 0)
967                         continue;
968                 sa = (struct sockaddr *)cp;
969                 /*
970                  * It won't fit.
971                  */
972                 if (cp + sa->sa_len > cplim)
973                         return (EINVAL);
974                 /*
975                  * there are no more.. quit now
976                  * If there are more bits, they are in error.
977                  * I've seen this. route(1) can evidently generate these. 
978                  * This causes kernel to core dump.
979                  * for compatibility, If we see this, point to a safe address.
980                  */
981                 if (sa->sa_len == 0) {
982                         rtinfo->rti_info[i] = &sa_zero;
983                         return (0); /* should be EINVAL but for compat */
984                 }
985                 /* accept it */
986 #ifdef INET6
987                 if (sa->sa_family == AF_INET6)
988                         sa6_embedscope((struct sockaddr_in6 *)sa,
989                             V_ip6_use_defzone);
990 #endif
991                 rtinfo->rti_info[i] = sa;
992                 cp += SA_SIZE(sa);
993         }
994         return (0);
995 }
996
997 /*
998  * Fill in @dmask with valid netmask leaving original @smask
999  * intact. Mostly used with radix netmasks.
1000  */
1001 static struct sockaddr *
1002 rtsock_fix_netmask(struct sockaddr *dst, struct sockaddr *smask,
1003     struct sockaddr_storage *dmask)
1004 {
1005         if (dst == NULL || smask == NULL)
1006                 return (NULL);
1007
1008         memset(dmask, 0, dst->sa_len);
1009         memcpy(dmask, smask, smask->sa_len);
1010         dmask->ss_len = dst->sa_len;
1011         dmask->ss_family = dst->sa_family;
1012
1013         return ((struct sockaddr *)dmask);
1014 }
1015
1016 /*
1017  * Writes information related to @rtinfo object to newly-allocated mbuf.
1018  * Assumes MCLBYTES is enough to construct any message.
1019  * Used for OS notifications of vaious events (if/ifa announces,etc)
1020  *
1021  * Returns allocated mbuf or NULL on failure.
1022  */
1023 static struct mbuf *
1024 rtsock_msg_mbuf(int type, struct rt_addrinfo *rtinfo)
1025 {
1026         struct rt_msghdr *rtm;
1027         struct mbuf *m;
1028         int i;
1029         struct sockaddr *sa;
1030 #ifdef INET6
1031         struct sockaddr_storage ss;
1032         struct sockaddr_in6 *sin6;
1033 #endif
1034         int len, dlen;
1035
1036         switch (type) {
1037
1038         case RTM_DELADDR:
1039         case RTM_NEWADDR:
1040                 len = sizeof(struct ifa_msghdr);
1041                 break;
1042
1043         case RTM_DELMADDR:
1044         case RTM_NEWMADDR:
1045                 len = sizeof(struct ifma_msghdr);
1046                 break;
1047
1048         case RTM_IFINFO:
1049                 len = sizeof(struct if_msghdr);
1050                 break;
1051
1052         case RTM_IFANNOUNCE:
1053         case RTM_IEEE80211:
1054                 len = sizeof(struct if_announcemsghdr);
1055                 break;
1056
1057         default:
1058                 len = sizeof(struct rt_msghdr);
1059         }
1060
1061         /* XXXGL: can we use MJUMPAGESIZE cluster here? */
1062         KASSERT(len <= MCLBYTES, ("%s: message too big", __func__));
1063         if (len > MHLEN)
1064                 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1065         else
1066                 m = m_gethdr(M_NOWAIT, MT_DATA);
1067         if (m == NULL)
1068                 return (m);
1069
1070         m->m_pkthdr.len = m->m_len = len;
1071         rtm = mtod(m, struct rt_msghdr *);
1072         bzero((caddr_t)rtm, len);
1073         for (i = 0; i < RTAX_MAX; i++) {
1074                 if ((sa = rtinfo->rti_info[i]) == NULL)
1075                         continue;
1076                 rtinfo->rti_addrs |= (1 << i);
1077                 dlen = SA_SIZE(sa);
1078 #ifdef INET6
1079                 if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1080                         sin6 = (struct sockaddr_in6 *)&ss;
1081                         bcopy(sa, sin6, sizeof(*sin6));
1082                         if (sa6_recoverscope(sin6) == 0)
1083                                 sa = (struct sockaddr *)sin6;
1084                 }
1085 #endif
1086                 m_copyback(m, len, dlen, (caddr_t)sa);
1087                 len += dlen;
1088         }
1089         if (m->m_pkthdr.len != len) {
1090                 m_freem(m);
1091                 return (NULL);
1092         }
1093         rtm->rtm_msglen = len;
1094         rtm->rtm_version = RTM_VERSION;
1095         rtm->rtm_type = type;
1096         return (m);
1097 }
1098
1099 /*
1100  * Writes information related to @rtinfo object to preallocated buffer.
1101  * Stores needed size in @plen. If @w is NULL, calculates size without
1102  * writing.
1103  * Used for sysctl dumps and rtsock answers (RTM_DEL/RTM_GET) generation.
1104  *
1105  * Returns 0 on success.
1106  *
1107  */
1108 static int
1109 rtsock_msg_buffer(int type, struct rt_addrinfo *rtinfo, struct walkarg *w, int *plen)
1110 {
1111         int i;
1112         int len, buflen = 0, dlen;
1113         caddr_t cp = NULL;
1114         struct rt_msghdr *rtm = NULL;
1115 #ifdef INET6
1116         struct sockaddr_storage ss;
1117         struct sockaddr_in6 *sin6;
1118 #endif
1119
1120         switch (type) {
1121
1122         case RTM_DELADDR:
1123         case RTM_NEWADDR:
1124                 if (w != NULL && w->w_op == NET_RT_IFLISTL) {
1125 #ifdef COMPAT_FREEBSD32
1126                         if (w->w_req->flags & SCTL_MASK32)
1127                                 len = sizeof(struct ifa_msghdrl32);
1128                         else
1129 #endif
1130                                 len = sizeof(struct ifa_msghdrl);
1131                 } else
1132                         len = sizeof(struct ifa_msghdr);
1133                 break;
1134
1135         case RTM_IFINFO:
1136 #ifdef COMPAT_FREEBSD32
1137                 if (w != NULL && w->w_req->flags & SCTL_MASK32) {
1138                         if (w->w_op == NET_RT_IFLISTL)
1139                                 len = sizeof(struct if_msghdrl32);
1140                         else
1141                                 len = sizeof(struct if_msghdr32);
1142                         break;
1143                 }
1144 #endif
1145                 if (w != NULL && w->w_op == NET_RT_IFLISTL)
1146                         len = sizeof(struct if_msghdrl);
1147                 else
1148                         len = sizeof(struct if_msghdr);
1149                 break;
1150
1151         case RTM_NEWMADDR:
1152                 len = sizeof(struct ifma_msghdr);
1153                 break;
1154
1155         default:
1156                 len = sizeof(struct rt_msghdr);
1157         }
1158
1159         if (w != NULL) {
1160                 rtm = (struct rt_msghdr *)w->w_tmem;
1161                 buflen = w->w_tmemsize - len;
1162                 cp = (caddr_t)w->w_tmem + len;
1163         }
1164
1165         rtinfo->rti_addrs = 0;
1166         for (i = 0; i < RTAX_MAX; i++) {
1167                 struct sockaddr *sa;
1168
1169                 if ((sa = rtinfo->rti_info[i]) == NULL)
1170                         continue;
1171                 rtinfo->rti_addrs |= (1 << i);
1172                 dlen = SA_SIZE(sa);
1173                 if (cp != NULL && buflen >= dlen) {
1174 #ifdef INET6
1175                         if (V_deembed_scopeid && sa->sa_family == AF_INET6) {
1176                                 sin6 = (struct sockaddr_in6 *)&ss;
1177                                 bcopy(sa, sin6, sizeof(*sin6));
1178                                 if (sa6_recoverscope(sin6) == 0)
1179                                         sa = (struct sockaddr *)sin6;
1180                         }
1181 #endif
1182                         bcopy((caddr_t)sa, cp, (unsigned)dlen);
1183                         cp += dlen;
1184                         buflen -= dlen;
1185                 } else if (cp != NULL) {
1186                         /*
1187                          * Buffer too small. Count needed size
1188                          * and return with error.
1189                          */
1190                         cp = NULL;
1191                 }
1192
1193                 len += dlen;
1194         }
1195
1196         if (cp != NULL) {
1197                 dlen = ALIGN(len) - len;
1198                 if (buflen < dlen)
1199                         cp = NULL;
1200                 else
1201                         buflen -= dlen;
1202         }
1203         len = ALIGN(len);
1204
1205         if (cp != NULL) {
1206                 /* fill header iff buffer is large enough */
1207                 rtm->rtm_version = RTM_VERSION;
1208                 rtm->rtm_type = type;
1209                 rtm->rtm_msglen = len;
1210         }
1211
1212         *plen = len;
1213
1214         if (w != NULL && cp == NULL)
1215                 return (ENOBUFS);
1216
1217         return (0);
1218 }
1219
1220 /*
1221  * This routine is called to generate a message from the routing
1222  * socket indicating that a redirect has occurred, a routing lookup
1223  * has failed, or that a protocol has detected timeouts to a particular
1224  * destination.
1225  */
1226 void
1227 rt_missmsg_fib(int type, struct rt_addrinfo *rtinfo, int flags, int error,
1228     int fibnum)
1229 {
1230         struct rt_msghdr *rtm;
1231         struct mbuf *m;
1232         struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
1233
1234         if (V_route_cb.any_count == 0)
1235                 return;
1236         m = rtsock_msg_mbuf(type, rtinfo);
1237         if (m == NULL)
1238                 return;
1239
1240         if (fibnum != RT_ALL_FIBS) {
1241                 KASSERT(fibnum >= 0 && fibnum < rt_numfibs, ("%s: fibnum out "
1242                     "of range 0 <= %d < %d", __func__, fibnum, rt_numfibs));
1243                 M_SETFIB(m, fibnum);
1244                 m->m_flags |= RTS_FILTER_FIB;
1245         }
1246
1247         rtm = mtod(m, struct rt_msghdr *);
1248         rtm->rtm_flags = RTF_DONE | flags;
1249         rtm->rtm_errno = error;
1250         rtm->rtm_addrs = rtinfo->rti_addrs;
1251         rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1252 }
1253
1254 void
1255 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
1256 {
1257
1258         rt_missmsg_fib(type, rtinfo, flags, error, RT_ALL_FIBS);
1259 }
1260
1261 /*
1262  * This routine is called to generate a message from the routing
1263  * socket indicating that the status of a network interface has changed.
1264  */
1265 void
1266 rt_ifmsg(struct ifnet *ifp)
1267 {
1268         struct if_msghdr *ifm;
1269         struct mbuf *m;
1270         struct rt_addrinfo info;
1271
1272         if (V_route_cb.any_count == 0)
1273                 return;
1274         bzero((caddr_t)&info, sizeof(info));
1275         m = rtsock_msg_mbuf(RTM_IFINFO, &info);
1276         if (m == NULL)
1277                 return;
1278         ifm = mtod(m, struct if_msghdr *);
1279         ifm->ifm_index = ifp->if_index;
1280         ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1281         if_data_copy(ifp, &ifm->ifm_data);
1282         ifm->ifm_addrs = 0;
1283         rt_dispatch(m, AF_UNSPEC);
1284 }
1285
1286 /*
1287  * Announce interface address arrival/withdraw.
1288  * Please do not call directly, use rt_addrmsg().
1289  * Assume input data to be valid.
1290  * Returns 0 on success.
1291  */
1292 int
1293 rtsock_addrmsg(int cmd, struct ifaddr *ifa, int fibnum)
1294 {
1295         struct rt_addrinfo info;
1296         struct sockaddr *sa;
1297         int ncmd;
1298         struct mbuf *m;
1299         struct ifa_msghdr *ifam;
1300         struct ifnet *ifp = ifa->ifa_ifp;
1301         struct sockaddr_storage ss;
1302
1303         if (V_route_cb.any_count == 0)
1304                 return (0);
1305
1306         ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
1307
1308         bzero((caddr_t)&info, sizeof(info));
1309         info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
1310         info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
1311         info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1312             info.rti_info[RTAX_IFP], ifa->ifa_netmask, &ss);
1313         info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1314         if ((m = rtsock_msg_mbuf(ncmd, &info)) == NULL)
1315                 return (ENOBUFS);
1316         ifam = mtod(m, struct ifa_msghdr *);
1317         ifam->ifam_index = ifp->if_index;
1318         ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1319         ifam->ifam_flags = ifa->ifa_flags;
1320         ifam->ifam_addrs = info.rti_addrs;
1321
1322         if (fibnum != RT_ALL_FIBS) {
1323                 M_SETFIB(m, fibnum);
1324                 m->m_flags |= RTS_FILTER_FIB;
1325         }
1326
1327         rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1328
1329         return (0);
1330 }
1331
1332 /*
1333  * Announce route addition/removal.
1334  * Please do not call directly, use rt_routemsg().
1335  * Note that @rt data MAY be inconsistent/invalid:
1336  * if some userland app sends us "invalid" route message (invalid mask,
1337  * no dst, wrong address families, etc...) we need to pass it back
1338  * to app (and any other rtsock consumers) with rtm_errno field set to
1339  * non-zero value.
1340  *
1341  * Returns 0 on success.
1342  */
1343 int
1344 rtsock_routemsg(int cmd, struct ifnet *ifp, int error, struct rtentry *rt,
1345     int fibnum)
1346 {
1347         struct rt_addrinfo info;
1348         struct sockaddr *sa;
1349         struct mbuf *m;
1350         struct rt_msghdr *rtm;
1351         struct sockaddr_storage ss;
1352
1353         if (V_route_cb.any_count == 0)
1354                 return (0);
1355
1356         bzero((caddr_t)&info, sizeof(info));
1357         info.rti_info[RTAX_DST] = sa = rt_key(rt);
1358         info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(sa, rt_mask(rt), &ss);
1359         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1360         if ((m = rtsock_msg_mbuf(cmd, &info)) == NULL)
1361                 return (ENOBUFS);
1362         rtm = mtod(m, struct rt_msghdr *);
1363         rtm->rtm_index = ifp->if_index;
1364         rtm->rtm_flags |= rt->rt_flags;
1365         rtm->rtm_errno = error;
1366         rtm->rtm_addrs = info.rti_addrs;
1367
1368         if (fibnum != RT_ALL_FIBS) {
1369                 M_SETFIB(m, fibnum);
1370                 m->m_flags |= RTS_FILTER_FIB;
1371         }
1372
1373         rt_dispatch(m, sa ? sa->sa_family : AF_UNSPEC);
1374
1375         return (0);
1376 }
1377
1378 /*
1379  * This is the analogue to the rt_newaddrmsg which performs the same
1380  * function but for multicast group memberhips.  This is easier since
1381  * there is no route state to worry about.
1382  */
1383 void
1384 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
1385 {
1386         struct rt_addrinfo info;
1387         struct mbuf *m = NULL;
1388         struct ifnet *ifp = ifma->ifma_ifp;
1389         struct ifma_msghdr *ifmam;
1390
1391         if (V_route_cb.any_count == 0)
1392                 return;
1393
1394         bzero((caddr_t)&info, sizeof(info));
1395         info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1396         info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
1397         /*
1398          * If a link-layer address is present, present it as a ``gateway''
1399          * (similarly to how ARP entries, e.g., are presented).
1400          */
1401         info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
1402         m = rtsock_msg_mbuf(cmd, &info);
1403         if (m == NULL)
1404                 return;
1405         ifmam = mtod(m, struct ifma_msghdr *);
1406         KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
1407             __func__));
1408         ifmam->ifmam_index = ifp->if_index;
1409         ifmam->ifmam_addrs = info.rti_addrs;
1410         rt_dispatch(m, ifma->ifma_addr ? ifma->ifma_addr->sa_family : AF_UNSPEC);
1411 }
1412
1413 static struct mbuf *
1414 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
1415         struct rt_addrinfo *info)
1416 {
1417         struct if_announcemsghdr *ifan;
1418         struct mbuf *m;
1419
1420         if (V_route_cb.any_count == 0)
1421                 return NULL;
1422         bzero((caddr_t)info, sizeof(*info));
1423         m = rtsock_msg_mbuf(type, info);
1424         if (m != NULL) {
1425                 ifan = mtod(m, struct if_announcemsghdr *);
1426                 ifan->ifan_index = ifp->if_index;
1427                 strlcpy(ifan->ifan_name, ifp->if_xname,
1428                         sizeof(ifan->ifan_name));
1429                 ifan->ifan_what = what;
1430         }
1431         return m;
1432 }
1433
1434 /*
1435  * This is called to generate routing socket messages indicating
1436  * IEEE80211 wireless events.
1437  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
1438  */
1439 void
1440 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
1441 {
1442         struct mbuf *m;
1443         struct rt_addrinfo info;
1444
1445         m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
1446         if (m != NULL) {
1447                 /*
1448                  * Append the ieee80211 data.  Try to stick it in the
1449                  * mbuf containing the ifannounce msg; otherwise allocate
1450                  * a new mbuf and append.
1451                  *
1452                  * NB: we assume m is a single mbuf.
1453                  */
1454                 if (data_len > M_TRAILINGSPACE(m)) {
1455                         struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
1456                         if (n == NULL) {
1457                                 m_freem(m);
1458                                 return;
1459                         }
1460                         bcopy(data, mtod(n, void *), data_len);
1461                         n->m_len = data_len;
1462                         m->m_next = n;
1463                 } else if (data_len > 0) {
1464                         bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
1465                         m->m_len += data_len;
1466                 }
1467                 if (m->m_flags & M_PKTHDR)
1468                         m->m_pkthdr.len += data_len;
1469                 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
1470                 rt_dispatch(m, AF_UNSPEC);
1471         }
1472 }
1473
1474 /*
1475  * This is called to generate routing socket messages indicating
1476  * network interface arrival and departure.
1477  */
1478 void
1479 rt_ifannouncemsg(struct ifnet *ifp, int what)
1480 {
1481         struct mbuf *m;
1482         struct rt_addrinfo info;
1483
1484         m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
1485         if (m != NULL)
1486                 rt_dispatch(m, AF_UNSPEC);
1487 }
1488
1489 static void
1490 rt_dispatch(struct mbuf *m, sa_family_t saf)
1491 {
1492         struct m_tag *tag;
1493
1494         /*
1495          * Preserve the family from the sockaddr, if any, in an m_tag for
1496          * use when injecting the mbuf into the routing socket buffer from
1497          * the netisr.
1498          */
1499         if (saf != AF_UNSPEC) {
1500                 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
1501                     M_NOWAIT);
1502                 if (tag == NULL) {
1503                         m_freem(m);
1504                         return;
1505                 }
1506                 *(unsigned short *)(tag + 1) = saf;
1507                 m_tag_prepend(m, tag);
1508         }
1509 #ifdef VIMAGE
1510         if (V_loif)
1511                 m->m_pkthdr.rcvif = V_loif;
1512         else {
1513                 m_freem(m);
1514                 return;
1515         }
1516 #endif
1517         netisr_queue(NETISR_ROUTE, m);  /* mbuf is free'd on failure. */
1518 }
1519
1520 /*
1521  * This is used in dumping the kernel table via sysctl().
1522  */
1523 static int
1524 sysctl_dumpentry(struct radix_node *rn, void *vw)
1525 {
1526         struct walkarg *w = vw;
1527         struct rtentry *rt = (struct rtentry *)rn;
1528         int error = 0, size;
1529         struct rt_addrinfo info;
1530         struct sockaddr_storage ss;
1531
1532         if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
1533                 return 0;
1534         if ((rt->rt_flags & RTF_HOST) == 0
1535             ? jailed_without_vnet(w->w_req->td->td_ucred)
1536             : prison_if(w->w_req->td->td_ucred, rt_key(rt)) != 0)
1537                 return (0);
1538         bzero((caddr_t)&info, sizeof(info));
1539         info.rti_info[RTAX_DST] = rt_key(rt);
1540         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1541         info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(rt_key(rt),
1542             rt_mask(rt), &ss);
1543         info.rti_info[RTAX_GENMASK] = 0;
1544         if (rt->rt_ifp) {
1545                 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
1546                 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
1547                 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
1548                         info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
1549         }
1550         if ((error = rtsock_msg_buffer(RTM_GET, &info, w, &size)) != 0)
1551                 return (error);
1552         if (w->w_req && w->w_tmem) {
1553                 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
1554
1555                 if (rt->rt_flags & RTF_GWFLAG_COMPAT)
1556                         rtm->rtm_flags = RTF_GATEWAY | 
1557                                 (rt->rt_flags & ~RTF_GWFLAG_COMPAT);
1558                 else
1559                         rtm->rtm_flags = rt->rt_flags;
1560                 rt_getmetrics(rt, &rtm->rtm_rmx);
1561                 rtm->rtm_index = rt->rt_ifp->if_index;
1562                 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
1563                 rtm->rtm_addrs = info.rti_addrs;
1564                 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
1565                 return (error);
1566         }
1567         return (error);
1568 }
1569
1570 static int
1571 sysctl_iflist_ifml(struct ifnet *ifp, const struct if_data *src_ifd,
1572     struct rt_addrinfo *info, struct walkarg *w, int len)
1573 {
1574         struct if_msghdrl *ifm;
1575         struct if_data *ifd;
1576
1577         ifm = (struct if_msghdrl *)w->w_tmem;
1578
1579 #ifdef COMPAT_FREEBSD32
1580         if (w->w_req->flags & SCTL_MASK32) {
1581                 struct if_msghdrl32 *ifm32;
1582
1583                 ifm32 = (struct if_msghdrl32 *)ifm;
1584                 ifm32->ifm_addrs = info->rti_addrs;
1585                 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1586                 ifm32->ifm_index = ifp->if_index;
1587                 ifm32->_ifm_spare1 = 0;
1588                 ifm32->ifm_len = sizeof(*ifm32);
1589                 ifm32->ifm_data_off = offsetof(struct if_msghdrl32, ifm_data);
1590                 ifd = &ifm32->ifm_data;
1591         } else
1592 #endif
1593         {
1594                 ifm->ifm_addrs = info->rti_addrs;
1595                 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1596                 ifm->ifm_index = ifp->if_index;
1597                 ifm->_ifm_spare1 = 0;
1598                 ifm->ifm_len = sizeof(*ifm);
1599                 ifm->ifm_data_off = offsetof(struct if_msghdrl, ifm_data);
1600                 ifd = &ifm->ifm_data;
1601         }
1602
1603         memcpy(ifd, src_ifd, sizeof(*ifd));
1604
1605         return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1606 }
1607
1608 static int
1609 sysctl_iflist_ifm(struct ifnet *ifp, const struct if_data *src_ifd,
1610     struct rt_addrinfo *info, struct walkarg *w, int len)
1611 {
1612         struct if_msghdr *ifm;
1613         struct if_data *ifd;
1614
1615         ifm = (struct if_msghdr *)w->w_tmem;
1616
1617 #ifdef COMPAT_FREEBSD32
1618         if (w->w_req->flags & SCTL_MASK32) {
1619                 struct if_msghdr32 *ifm32;
1620
1621                 ifm32 = (struct if_msghdr32 *)ifm;
1622                 ifm32->ifm_addrs = info->rti_addrs;
1623                 ifm32->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1624                 ifm32->ifm_index = ifp->if_index;
1625                 ifd = &ifm32->ifm_data;
1626         } else
1627 #endif
1628         {
1629                 ifm->ifm_addrs = info->rti_addrs;
1630                 ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
1631                 ifm->ifm_index = ifp->if_index;
1632                 ifd = &ifm->ifm_data;
1633         }
1634
1635         memcpy(ifd, src_ifd, sizeof(*ifd));
1636
1637         return (SYSCTL_OUT(w->w_req, (caddr_t)ifm, len));
1638 }
1639
1640 static int
1641 sysctl_iflist_ifaml(struct ifaddr *ifa, struct rt_addrinfo *info,
1642     struct walkarg *w, int len)
1643 {
1644         struct ifa_msghdrl *ifam;
1645         struct if_data *ifd;
1646
1647         ifam = (struct ifa_msghdrl *)w->w_tmem;
1648
1649 #ifdef COMPAT_FREEBSD32
1650         if (w->w_req->flags & SCTL_MASK32) {
1651                 struct ifa_msghdrl32 *ifam32;
1652
1653                 ifam32 = (struct ifa_msghdrl32 *)ifam;
1654                 ifam32->ifam_addrs = info->rti_addrs;
1655                 ifam32->ifam_flags = ifa->ifa_flags;
1656                 ifam32->ifam_index = ifa->ifa_ifp->if_index;
1657                 ifam32->_ifam_spare1 = 0;
1658                 ifam32->ifam_len = sizeof(*ifam32);
1659                 ifam32->ifam_data_off =
1660                     offsetof(struct ifa_msghdrl32, ifam_data);
1661                 ifam32->ifam_metric = ifa->ifa_ifp->if_metric;
1662                 ifd = &ifam32->ifam_data;
1663         } else
1664 #endif
1665         {
1666                 ifam->ifam_addrs = info->rti_addrs;
1667                 ifam->ifam_flags = ifa->ifa_flags;
1668                 ifam->ifam_index = ifa->ifa_ifp->if_index;
1669                 ifam->_ifam_spare1 = 0;
1670                 ifam->ifam_len = sizeof(*ifam);
1671                 ifam->ifam_data_off = offsetof(struct ifa_msghdrl, ifam_data);
1672                 ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1673                 ifd = &ifam->ifam_data;
1674         }
1675
1676         bzero(ifd, sizeof(*ifd));
1677         ifd->ifi_datalen = sizeof(struct if_data);
1678         ifd->ifi_ipackets = counter_u64_fetch(ifa->ifa_ipackets);
1679         ifd->ifi_opackets = counter_u64_fetch(ifa->ifa_opackets);
1680         ifd->ifi_ibytes = counter_u64_fetch(ifa->ifa_ibytes);
1681         ifd->ifi_obytes = counter_u64_fetch(ifa->ifa_obytes);
1682
1683         /* Fixup if_data carp(4) vhid. */
1684         if (carp_get_vhid_p != NULL)
1685                 ifd->ifi_vhid = (*carp_get_vhid_p)(ifa);
1686
1687         return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1688 }
1689
1690 static int
1691 sysctl_iflist_ifam(struct ifaddr *ifa, struct rt_addrinfo *info,
1692     struct walkarg *w, int len)
1693 {
1694         struct ifa_msghdr *ifam;
1695
1696         ifam = (struct ifa_msghdr *)w->w_tmem;
1697         ifam->ifam_addrs = info->rti_addrs;
1698         ifam->ifam_flags = ifa->ifa_flags;
1699         ifam->ifam_index = ifa->ifa_ifp->if_index;
1700         ifam->ifam_metric = ifa->ifa_ifp->if_metric;
1701
1702         return (SYSCTL_OUT(w->w_req, w->w_tmem, len));
1703 }
1704
1705 static int
1706 sysctl_iflist(int af, struct walkarg *w)
1707 {
1708         struct ifnet *ifp;
1709         struct ifaddr *ifa;
1710         struct if_data ifd;
1711         struct rt_addrinfo info;
1712         int len, error = 0;
1713         struct sockaddr_storage ss;
1714
1715         bzero((caddr_t)&info, sizeof(info));
1716         bzero(&ifd, sizeof(ifd));
1717         IFNET_RLOCK_NOSLEEP();
1718         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1719                 if (w->w_arg && w->w_arg != ifp->if_index)
1720                         continue;
1721                 if_data_copy(ifp, &ifd);
1722                 IF_ADDR_RLOCK(ifp);
1723                 ifa = ifp->if_addr;
1724                 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
1725                 error = rtsock_msg_buffer(RTM_IFINFO, &info, w, &len);
1726                 if (error != 0)
1727                         goto done;
1728                 info.rti_info[RTAX_IFP] = NULL;
1729                 if (w->w_req && w->w_tmem) {
1730                         if (w->w_op == NET_RT_IFLISTL)
1731                                 error = sysctl_iflist_ifml(ifp, &ifd, &info, w,
1732                                     len);
1733                         else
1734                                 error = sysctl_iflist_ifm(ifp, &ifd, &info, w,
1735                                     len);
1736                         if (error)
1737                                 goto done;
1738                 }
1739                 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
1740                         if (af && af != ifa->ifa_addr->sa_family)
1741                                 continue;
1742                         if (prison_if(w->w_req->td->td_ucred,
1743                             ifa->ifa_addr) != 0)
1744                                 continue;
1745                         info.rti_info[RTAX_IFA] = ifa->ifa_addr;
1746                         info.rti_info[RTAX_NETMASK] = rtsock_fix_netmask(
1747                             ifa->ifa_addr, ifa->ifa_netmask, &ss);
1748                         info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
1749                         error = rtsock_msg_buffer(RTM_NEWADDR, &info, w, &len);
1750                         if (error != 0)
1751                                 goto done;
1752                         if (w->w_req && w->w_tmem) {
1753                                 if (w->w_op == NET_RT_IFLISTL)
1754                                         error = sysctl_iflist_ifaml(ifa, &info,
1755                                             w, len);
1756                                 else
1757                                         error = sysctl_iflist_ifam(ifa, &info,
1758                                             w, len);
1759                                 if (error)
1760                                         goto done;
1761                         }
1762                 }
1763                 IF_ADDR_RUNLOCK(ifp);
1764                 info.rti_info[RTAX_IFA] = NULL;
1765                 info.rti_info[RTAX_NETMASK] = NULL;
1766                 info.rti_info[RTAX_BRD] = NULL;
1767         }
1768 done:
1769         if (ifp != NULL)
1770                 IF_ADDR_RUNLOCK(ifp);
1771         IFNET_RUNLOCK_NOSLEEP();
1772         return (error);
1773 }
1774
1775 static int
1776 sysctl_ifmalist(int af, struct walkarg *w)
1777 {
1778         struct rt_addrinfo info;
1779         struct ifaddr *ifa;
1780         struct ifmultiaddr *ifma;
1781         struct ifnet *ifp;
1782         int error, len;
1783
1784         error = 0;
1785         bzero((caddr_t)&info, sizeof(info));
1786
1787         IFNET_RLOCK_NOSLEEP();
1788         TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1789                 if (w->w_arg && w->w_arg != ifp->if_index)
1790                         continue;
1791                 ifa = ifp->if_addr;
1792                 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
1793                 IF_ADDR_RLOCK(ifp);
1794                 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1795                         if (af && af != ifma->ifma_addr->sa_family)
1796                                 continue;
1797                         if (prison_if(w->w_req->td->td_ucred,
1798                             ifma->ifma_addr) != 0)
1799                                 continue;
1800                         info.rti_info[RTAX_IFA] = ifma->ifma_addr;
1801                         info.rti_info[RTAX_GATEWAY] =
1802                             (ifma->ifma_addr->sa_family != AF_LINK) ?
1803                             ifma->ifma_lladdr : NULL;
1804                         error = rtsock_msg_buffer(RTM_NEWMADDR, &info, w, &len);
1805                         if (error != 0)
1806                                 break;
1807                         if (w->w_req && w->w_tmem) {
1808                                 struct ifma_msghdr *ifmam;
1809
1810                                 ifmam = (struct ifma_msghdr *)w->w_tmem;
1811                                 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
1812                                 ifmam->ifmam_flags = 0;
1813                                 ifmam->ifmam_addrs = info.rti_addrs;
1814                                 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
1815                                 if (error != 0)
1816                                         break;
1817                         }
1818                 }
1819                 IF_ADDR_RUNLOCK(ifp);
1820                 if (error != 0)
1821                         break;
1822         }
1823         IFNET_RUNLOCK_NOSLEEP();
1824         return (error);
1825 }
1826
1827 static int
1828 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
1829 {
1830         int     *name = (int *)arg1;
1831         u_int   namelen = arg2;
1832         struct rib_head *rnh = NULL; /* silence compiler. */
1833         int     i, lim, error = EINVAL;
1834         int     fib = 0;
1835         u_char  af;
1836         struct  walkarg w;
1837
1838         name ++;
1839         namelen--;
1840         if (req->newptr)
1841                 return (EPERM);
1842         if (name[1] == NET_RT_DUMP) {
1843                 if (namelen == 3)
1844                         fib = req->td->td_proc->p_fibnum;
1845                 else if (namelen == 4)
1846                         fib = (name[3] == RT_ALL_FIBS) ?
1847                             req->td->td_proc->p_fibnum : name[3];
1848                 else
1849                         return ((namelen < 3) ? EISDIR : ENOTDIR);
1850                 if (fib < 0 || fib >= rt_numfibs)
1851                         return (EINVAL);
1852         } else if (namelen != 3)
1853                 return ((namelen < 3) ? EISDIR : ENOTDIR);
1854         af = name[0];
1855         if (af > AF_MAX)
1856                 return (EINVAL);
1857         bzero(&w, sizeof(w));
1858         w.w_op = name[1];
1859         w.w_arg = name[2];
1860         w.w_req = req;
1861
1862         error = sysctl_wire_old_buffer(req, 0);
1863         if (error)
1864                 return (error);
1865         
1866         /*
1867          * Allocate reply buffer in advance.
1868          * All rtsock messages has maximum length of u_short.
1869          */
1870         w.w_tmemsize = 65536;
1871         w.w_tmem = malloc(w.w_tmemsize, M_TEMP, M_WAITOK);
1872
1873         switch (w.w_op) {
1874
1875         case NET_RT_DUMP:
1876         case NET_RT_FLAGS:
1877                 if (af == 0) {                  /* dump all tables */
1878                         i = 1;
1879                         lim = AF_MAX;
1880                 } else                          /* dump only one table */
1881                         i = lim = af;
1882
1883                 /*
1884                  * take care of llinfo entries, the caller must
1885                  * specify an AF
1886                  */
1887                 if (w.w_op == NET_RT_FLAGS &&
1888                     (w.w_arg == 0 || w.w_arg & RTF_LLINFO)) {
1889                         if (af != 0)
1890                                 error = lltable_sysctl_dumparp(af, w.w_req);
1891                         else
1892                                 error = EINVAL;
1893                         break;
1894                 }
1895                 /*
1896                  * take care of routing entries
1897                  */
1898                 for (error = 0; error == 0 && i <= lim; i++) {
1899                         rnh = rt_tables_get_rnh(fib, i);
1900                         if (rnh != NULL) {
1901                                 RIB_RLOCK(rnh); 
1902                                 error = rnh->rnh_walktree(&rnh->head,
1903                                     sysctl_dumpentry, &w);
1904                                 RIB_RUNLOCK(rnh);
1905                         } else if (af != 0)
1906                                 error = EAFNOSUPPORT;
1907                 }
1908                 break;
1909
1910         case NET_RT_IFLIST:
1911         case NET_RT_IFLISTL:
1912                 error = sysctl_iflist(af, &w);
1913                 break;
1914
1915         case NET_RT_IFMALIST:
1916                 error = sysctl_ifmalist(af, &w);
1917                 break;
1918         }
1919
1920         free(w.w_tmem, M_TEMP);
1921         return (error);
1922 }
1923
1924 static SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
1925
1926 /*
1927  * Definitions of protocols supported in the ROUTE domain.
1928  */
1929
1930 static struct domain routedomain;               /* or at least forward */
1931
1932 static struct protosw routesw[] = {
1933 {
1934         .pr_type =              SOCK_RAW,
1935         .pr_domain =            &routedomain,
1936         .pr_flags =             PR_ATOMIC|PR_ADDR,
1937         .pr_output =            route_output,
1938         .pr_ctlinput =          raw_ctlinput,
1939         .pr_init =              raw_init,
1940         .pr_usrreqs =           &route_usrreqs
1941 }
1942 };
1943
1944 static struct domain routedomain = {
1945         .dom_family =           PF_ROUTE,
1946         .dom_name =              "route",
1947         .dom_protosw =          routesw,
1948         .dom_protoswNPROTOSW =  &routesw[nitems(routesw)]
1949 };
1950
1951 VNET_DOMAIN_SET(route);