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