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