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