The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/net/rtsock.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 760317b23ae4867c7fd24c74c9937a47


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.