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$
   31  */
   32 #include "opt_sctp.h"
   33 #include <sys/param.h>
   34 #include <sys/domain.h>
   35 #include <sys/kernel.h>
   36 #include <sys/jail.h>
   37 #include <sys/malloc.h>
   38 #include <sys/mbuf.h>
   39 #include <sys/priv.h>
   40 #include <sys/proc.h>
   41 #include <sys/protosw.h>
   42 #include <sys/signalvar.h>
   43 #include <sys/socket.h>
   44 #include <sys/socketvar.h>
   45 #include <sys/sysctl.h>
   46 #include <sys/systm.h>
   47 
   48 #include <net/if.h>
   49 #include <net/netisr.h>
   50 #include <net/raw_cb.h>
   51 #include <net/route.h>
   52 
   53 #include <netinet/in.h>
   54 
   55 #ifdef SCTP
   56 extern void sctp_addr_change(struct ifaddr *ifa, int cmd);
   57 #endif /* SCTP */
   58 
   59 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
   60 
   61 /* NB: these are not modified */
   62 static struct   sockaddr route_dst = { 2, PF_ROUTE, };
   63 static struct   sockaddr route_src = { 2, PF_ROUTE, };
   64 static struct   sockaddr sa_zero   = { sizeof(sa_zero), AF_INET, };
   65 
   66 static struct {
   67         int     ip_count;       /* attached w/ AF_INET */
   68         int     ip6_count;      /* attached w/ AF_INET6 */
   69         int     ipx_count;      /* attached w/ AF_IPX */
   70         int     any_count;      /* total attached */
   71 } route_cb;
   72 
   73 struct mtx rtsock_mtx;
   74 MTX_SYSINIT(rtsock, &rtsock_mtx, "rtsock route_cb lock", MTX_DEF);
   75 
   76 #define RTSOCK_LOCK()   mtx_lock(&rtsock_mtx)
   77 #define RTSOCK_UNLOCK() mtx_unlock(&rtsock_mtx)
   78 #define RTSOCK_LOCK_ASSERT()    mtx_assert(&rtsock_mtx, MA_OWNED)
   79 
   80 static struct   ifqueue rtsintrq;
   81 
   82 SYSCTL_NODE(_net, OID_AUTO, route, CTLFLAG_RD, 0, "");
   83 SYSCTL_INT(_net_route, OID_AUTO, netisr_maxqlen, CTLFLAG_RW,
   84     &rtsintrq.ifq_maxlen, 0, "maximum routing socket dispatch queue length");
   85 
   86 struct walkarg {
   87         int     w_tmemsize;
   88         int     w_op, w_arg;
   89         caddr_t w_tmem;
   90         struct sysctl_req *w_req;
   91 };
   92 
   93 static void     rts_input(struct mbuf *m);
   94 static struct mbuf *rt_msg1(int type, struct rt_addrinfo *rtinfo);
   95 static int      rt_msg2(int type, struct rt_addrinfo *rtinfo,
   96                         caddr_t cp, struct walkarg *w);
   97 static int      rt_xaddrs(caddr_t cp, caddr_t cplim,
   98                         struct rt_addrinfo *rtinfo);
   99 static int      sysctl_dumpentry(struct radix_node *rn, void *vw);
  100 static int      sysctl_iflist(int af, struct walkarg *w);
  101 static int      sysctl_ifmalist(int af, struct walkarg *w);
  102 static int      route_output(struct mbuf *m, struct socket *so);
  103 static void     rt_setmetrics(u_long which, const struct rt_metrics *in,
  104                         struct rt_metrics_lite *out);
  105 static void     rt_getmetrics(const struct rt_metrics_lite *in,
  106                         struct rt_metrics *out);
  107 static void     rt_dispatch(struct mbuf *, const struct sockaddr *);
  108 
  109 static void
  110 rts_init(void)
  111 {
  112         int tmp;
  113 
  114         rtsintrq.ifq_maxlen = 256;
  115         if (TUNABLE_INT_FETCH("net.route.netisr_maxqlen", &tmp))
  116                 rtsintrq.ifq_maxlen = tmp;
  117         mtx_init(&rtsintrq.ifq_mtx, "rts_inq", NULL, MTX_DEF);
  118         netisr_register(NETISR_ROUTE, rts_input, &rtsintrq, NETISR_MPSAFE);
  119 }
  120 SYSINIT(rtsock, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, rts_init, 0)
  121 
  122 static void
  123 rts_input(struct mbuf *m)
  124 {
  125         struct sockproto route_proto;
  126         unsigned short *family;
  127         struct m_tag *tag;
  128 
  129         route_proto.sp_family = PF_ROUTE;
  130         tag = m_tag_find(m, PACKET_TAG_RTSOCKFAM, NULL);
  131         if (tag != NULL) {
  132                 family = (unsigned short *)(tag + 1);
  133                 route_proto.sp_protocol = *family;
  134                 m_tag_delete(m, tag);
  135         } else
  136                 route_proto.sp_protocol = 0;
  137 
  138         raw_input(m, &route_proto, &route_src, &route_dst);
  139 }
  140 
  141 /*
  142  * It really doesn't make any sense at all for this code to share much
  143  * with raw_usrreq.c, since its functionality is so restricted.  XXX
  144  */
  145 static void
  146 rts_abort(struct socket *so)
  147 {
  148 
  149         raw_usrreqs.pru_abort(so);
  150 }
  151 
  152 static void
  153 rts_close(struct socket *so)
  154 {
  155 
  156         raw_usrreqs.pru_close(so);
  157 }
  158 
  159 /* pru_accept is EOPNOTSUPP */
  160 
  161 static int
  162 rts_attach(struct socket *so, int proto, struct thread *td)
  163 {
  164         struct rawcb *rp;
  165         int s, error;
  166 
  167         KASSERT(so->so_pcb == NULL, ("rts_attach: so_pcb != NULL"));
  168 
  169         /* XXX */
  170         MALLOC(rp, struct rawcb *, sizeof *rp, M_PCB, M_WAITOK | M_ZERO);
  171         if (rp == NULL)
  172                 return ENOBUFS;
  173 
  174         /*
  175          * The splnet() is necessary to block protocols from sending
  176          * error notifications (like RTM_REDIRECT or RTM_LOSING) while
  177          * this PCB is extant but incompletely initialized.
  178          * Probably we should try to do more of this work beforehand and
  179          * eliminate the spl.
  180          */
  181         s = splnet();
  182         so->so_pcb = (caddr_t)rp;
  183         error = raw_attach(so, proto);
  184         rp = sotorawcb(so);
  185         if (error) {
  186                 splx(s);
  187                 so->so_pcb = NULL;
  188                 free(rp, M_PCB);
  189                 return error;
  190         }
  191         RTSOCK_LOCK();
  192         switch(rp->rcb_proto.sp_protocol) {
  193         case AF_INET:
  194                 route_cb.ip_count++;
  195                 break;
  196         case AF_INET6:
  197                 route_cb.ip6_count++;
  198                 break;
  199         case AF_IPX:
  200                 route_cb.ipx_count++;
  201                 break;
  202         }
  203         rp->rcb_faddr = &route_src;
  204         route_cb.any_count++;
  205         RTSOCK_UNLOCK();
  206         soisconnected(so);
  207         so->so_options |= SO_USELOOPBACK;
  208         splx(s);
  209         return 0;
  210 }
  211 
  212 static int
  213 rts_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  214 {
  215 
  216         return (raw_usrreqs.pru_bind(so, nam, td)); /* xxx just EINVAL */
  217 }
  218 
  219 static int
  220 rts_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  221 {
  222 
  223         return (raw_usrreqs.pru_connect(so, nam, td)); /* XXX just EINVAL */
  224 }
  225 
  226 /* pru_connect2 is EOPNOTSUPP */
  227 /* pru_control is EOPNOTSUPP */
  228 
  229 static void
  230 rts_detach(struct socket *so)
  231 {
  232         struct rawcb *rp = sotorawcb(so);
  233 
  234         KASSERT(rp != NULL, ("rts_detach: rp == NULL"));
  235 
  236         RTSOCK_LOCK();
  237         switch(rp->rcb_proto.sp_protocol) {
  238         case AF_INET:
  239                 route_cb.ip_count--;
  240                 break;
  241         case AF_INET6:
  242                 route_cb.ip6_count--;
  243                 break;
  244         case AF_IPX:
  245                 route_cb.ipx_count--;
  246                 break;
  247         }
  248         route_cb.any_count--;
  249         RTSOCK_UNLOCK();
  250         raw_usrreqs.pru_detach(so);
  251 }
  252 
  253 static int
  254 rts_disconnect(struct socket *so)
  255 {
  256 
  257         return (raw_usrreqs.pru_disconnect(so));
  258 }
  259 
  260 /* pru_listen is EOPNOTSUPP */
  261 
  262 static int
  263 rts_peeraddr(struct socket *so, struct sockaddr **nam)
  264 {
  265 
  266         return (raw_usrreqs.pru_peeraddr(so, nam));
  267 }
  268 
  269 /* pru_rcvd is EOPNOTSUPP */
  270 /* pru_rcvoob is EOPNOTSUPP */
  271 
  272 static int
  273 rts_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
  274          struct mbuf *control, struct thread *td)
  275 {
  276 
  277         return (raw_usrreqs.pru_send(so, flags, m, nam, control, td));
  278 }
  279 
  280 /* pru_sense is null */
  281 
  282 static int
  283 rts_shutdown(struct socket *so)
  284 {
  285 
  286         return (raw_usrreqs.pru_shutdown(so));
  287 }
  288 
  289 static int
  290 rts_sockaddr(struct socket *so, struct sockaddr **nam)
  291 {
  292 
  293         return (raw_usrreqs.pru_sockaddr(so, nam));
  294 }
  295 
  296 static struct pr_usrreqs route_usrreqs = {
  297         .pru_abort =            rts_abort,
  298         .pru_attach =           rts_attach,
  299         .pru_bind =             rts_bind,
  300         .pru_connect =          rts_connect,
  301         .pru_detach =           rts_detach,
  302         .pru_disconnect =       rts_disconnect,
  303         .pru_peeraddr =         rts_peeraddr,
  304         .pru_send =             rts_send,
  305         .pru_shutdown =         rts_shutdown,
  306         .pru_sockaddr =         rts_sockaddr,
  307         .pru_close =            rts_close,
  308 };
  309 
  310 /*ARGSUSED*/
  311 static int
  312 route_output(struct mbuf *m, struct socket *so)
  313 {
  314 #define sa_equal(a1, a2) (bcmp((a1), (a2), (a1)->sa_len) == 0)
  315         struct rt_msghdr *rtm = NULL;
  316         struct rtentry *rt = NULL;
  317         struct radix_node_head *rnh;
  318         struct rt_addrinfo info;
  319         int len, error = 0;
  320         struct ifnet *ifp = NULL;
  321         struct sockaddr_in jail;
  322 
  323 #define senderr(e) { error = e; goto flush;}
  324         if (m == NULL || ((m->m_len < sizeof(long)) &&
  325                        (m = m_pullup(m, sizeof(long))) == NULL))
  326                 return (ENOBUFS);
  327         if ((m->m_flags & M_PKTHDR) == 0)
  328                 panic("route_output");
  329         len = m->m_pkthdr.len;
  330         if (len < sizeof(*rtm) ||
  331             len != mtod(m, struct rt_msghdr *)->rtm_msglen) {
  332                 info.rti_info[RTAX_DST] = NULL;
  333                 senderr(EINVAL);
  334         }
  335         R_Malloc(rtm, struct rt_msghdr *, len);
  336         if (rtm == NULL) {
  337                 info.rti_info[RTAX_DST] = NULL;
  338                 senderr(ENOBUFS);
  339         }
  340         m_copydata(m, 0, len, (caddr_t)rtm);
  341         if (rtm->rtm_version != RTM_VERSION) {
  342                 info.rti_info[RTAX_DST] = NULL;
  343                 senderr(EPROTONOSUPPORT);
  344         }
  345         rtm->rtm_pid = curproc->p_pid;
  346         bzero(&info, sizeof(info));
  347         info.rti_addrs = rtm->rtm_addrs;
  348         if (rt_xaddrs((caddr_t)(rtm + 1), len + (caddr_t)rtm, &info)) {
  349                 info.rti_info[RTAX_DST] = NULL;
  350                 senderr(EINVAL);
  351         }
  352         info.rti_flags = rtm->rtm_flags;
  353         if (info.rti_info[RTAX_DST] == NULL ||
  354             info.rti_info[RTAX_DST]->sa_family >= AF_MAX ||
  355             (info.rti_info[RTAX_GATEWAY] != NULL &&
  356              info.rti_info[RTAX_GATEWAY]->sa_family >= AF_MAX))
  357                 senderr(EINVAL);
  358         if (info.rti_info[RTAX_GENMASK]) {
  359                 struct radix_node *t;
  360                 t = rn_addmask((caddr_t) info.rti_info[RTAX_GENMASK], 0, 1);
  361                 if (t != NULL &&
  362                     bcmp((char *)(void *)info.rti_info[RTAX_GENMASK] + 1,
  363                     (char *)(void *)t->rn_key + 1,
  364                     ((struct sockaddr *)t->rn_key)->sa_len - 1) == 0)
  365                         info.rti_info[RTAX_GENMASK] =
  366                             (struct sockaddr *)t->rn_key;
  367                 else
  368                         senderr(ENOBUFS);
  369         }
  370 
  371         /*
  372          * Verify that the caller has the appropriate privilege; RTM_GET
  373          * is the only operation the non-superuser is allowed.
  374          */
  375         if (rtm->rtm_type != RTM_GET) {
  376                 error = priv_check(curthread, PRIV_NET_ROUTE);
  377                 if (error)
  378                         senderr(error);
  379         }
  380 
  381         switch (rtm->rtm_type) {
  382                 struct rtentry *saved_nrt;
  383 
  384         case RTM_ADD:
  385                 if (info.rti_info[RTAX_GATEWAY] == NULL)
  386                         senderr(EINVAL);
  387                 saved_nrt = NULL;
  388                 error = rtrequest1(RTM_ADD, &info, &saved_nrt);
  389                 if (error == 0 && saved_nrt) {
  390                         RT_LOCK(saved_nrt);
  391                         rt_setmetrics(rtm->rtm_inits,
  392                                 &rtm->rtm_rmx, &saved_nrt->rt_rmx);
  393                         rtm->rtm_index = saved_nrt->rt_ifp->if_index;
  394                         RT_REMREF(saved_nrt);
  395                         saved_nrt->rt_genmask = info.rti_info[RTAX_GENMASK];
  396                         RT_UNLOCK(saved_nrt);
  397                 }
  398                 break;
  399 
  400         case RTM_DELETE:
  401                 saved_nrt = NULL;
  402                 error = rtrequest1(RTM_DELETE, &info, &saved_nrt);
  403                 if (error == 0) {
  404                         RT_LOCK(saved_nrt);
  405                         rt = saved_nrt;
  406                         goto report;
  407                 }
  408                 break;
  409 
  410         case RTM_GET:
  411         case RTM_CHANGE:
  412         case RTM_LOCK:
  413                 rnh = rt_tables[info.rti_info[RTAX_DST]->sa_family];
  414                 if (rnh == NULL)
  415                         senderr(EAFNOSUPPORT);
  416                 RADIX_NODE_HEAD_LOCK(rnh);
  417                 rt = (struct rtentry *) rnh->rnh_lookup(info.rti_info[RTAX_DST],
  418                         info.rti_info[RTAX_NETMASK], rnh);
  419                 if (rt == NULL) {       /* XXX looks bogus */
  420                         RADIX_NODE_HEAD_UNLOCK(rnh);
  421                         senderr(ESRCH);
  422                 }
  423                 RT_LOCK(rt);
  424                 RT_ADDREF(rt);
  425                 RADIX_NODE_HEAD_UNLOCK(rnh);
  426 
  427                 /* 
  428                  * Fix for PR: 82974
  429                  *
  430                  * RTM_CHANGE/LOCK need a perfect match, rn_lookup()
  431                  * returns a perfect match in case a netmask is
  432                  * specified.  For host routes only a longest prefix
  433                  * match is returned so it is necessary to compare the
  434                  * existence of the netmask.  If both have a netmask
  435                  * rnh_lookup() did a perfect match and if none of them
  436                  * have a netmask both are host routes which is also a
  437                  * perfect match.
  438                  */
  439 
  440                 if (rtm->rtm_type != RTM_GET && 
  441                     (!rt_mask(rt) != !info.rti_info[RTAX_NETMASK])) {
  442                         RT_UNLOCK(rt);
  443                         senderr(ESRCH);
  444                 }
  445 
  446                 switch(rtm->rtm_type) {
  447 
  448                 case RTM_GET:
  449                 report:
  450                         RT_LOCK_ASSERT(rt);
  451                         info.rti_info[RTAX_DST] = rt_key(rt);
  452                         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
  453                         info.rti_info[RTAX_NETMASK] = rt_mask(rt);
  454                         info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
  455                         if (rtm->rtm_addrs & (RTA_IFP | RTA_IFA)) {
  456                                 ifp = rt->rt_ifp;
  457                                 if (ifp) {
  458                                         info.rti_info[RTAX_IFP] =
  459                                             ifp->if_addr->ifa_addr;
  460                                         if (jailed(so->so_cred)) {
  461                                                 bzero(&jail, sizeof(jail));
  462                                                 jail.sin_family = PF_INET;
  463                                                 jail.sin_len = sizeof(jail);
  464                                                 jail.sin_addr.s_addr =
  465                                                 htonl(prison_getip(so->so_cred));
  466                                                 info.rti_info[RTAX_IFA] =
  467                                                     (struct sockaddr *)&jail;
  468                                         } else
  469                                                 info.rti_info[RTAX_IFA] =
  470                                                     rt->rt_ifa->ifa_addr;
  471                                         if (ifp->if_flags & IFF_POINTOPOINT)
  472                                                 info.rti_info[RTAX_BRD] =
  473                                                     rt->rt_ifa->ifa_dstaddr;
  474                                         rtm->rtm_index = ifp->if_index;
  475                                 } else {
  476                                         info.rti_info[RTAX_IFP] = NULL;
  477                                         info.rti_info[RTAX_IFA] = NULL;
  478                                 }
  479                         } else if ((ifp = rt->rt_ifp) != NULL) {
  480                                 rtm->rtm_index = ifp->if_index;
  481                         }
  482                         len = rt_msg2(rtm->rtm_type, &info, NULL, NULL);
  483                         if (len > rtm->rtm_msglen) {
  484                                 struct rt_msghdr *new_rtm;
  485                                 R_Malloc(new_rtm, struct rt_msghdr *, len);
  486                                 if (new_rtm == NULL) {
  487                                         RT_UNLOCK(rt);
  488                                         senderr(ENOBUFS);
  489                                 }
  490                                 bcopy(rtm, new_rtm, rtm->rtm_msglen);
  491                                 Free(rtm); rtm = new_rtm;
  492                         }
  493                         (void)rt_msg2(rtm->rtm_type, &info, (caddr_t)rtm, NULL);
  494                         rtm->rtm_flags = rt->rt_flags;
  495                         rtm->rtm_use = 0;
  496                         rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
  497                         rtm->rtm_addrs = info.rti_addrs;
  498                         break;
  499 
  500                 case RTM_CHANGE:
  501                         /*
  502                          * New gateway could require new ifaddr, ifp;
  503                          * flags may also be different; ifp may be specified
  504                          * by ll sockaddr when protocol address is ambiguous
  505                          */
  506                         if (((rt->rt_flags & RTF_GATEWAY) &&
  507                              info.rti_info[RTAX_GATEWAY] != NULL) ||
  508                             info.rti_info[RTAX_IFP] != NULL ||
  509                             (info.rti_info[RTAX_IFA] != NULL &&
  510                              !sa_equal(info.rti_info[RTAX_IFA],
  511                                        rt->rt_ifa->ifa_addr))) {
  512                                 RT_UNLOCK(rt);
  513                                 if ((error = rt_getifa(&info)) != 0)
  514                                         senderr(error);
  515                                 RT_LOCK(rt);
  516                         }
  517                         if (info.rti_ifa != NULL &&
  518                             info.rti_ifa != rt->rt_ifa &&
  519                             rt->rt_ifa != NULL &&
  520                             rt->rt_ifa->ifa_rtrequest != NULL) {
  521                                 rt->rt_ifa->ifa_rtrequest(RTM_DELETE, rt,
  522                                     &info);
  523                                 IFAFREE(rt->rt_ifa);
  524                         }
  525                         if (info.rti_info[RTAX_GATEWAY] != NULL) {
  526                                 if ((error = rt_setgate(rt, rt_key(rt),
  527                                         info.rti_info[RTAX_GATEWAY])) != 0) {
  528                                         RT_UNLOCK(rt);
  529                                         senderr(error);
  530                                 }
  531                                 if (!(rt->rt_flags & RTF_LLINFO))
  532                                         rt->rt_flags |= RTF_GATEWAY;
  533                         }
  534                         if (info.rti_ifa != NULL &&
  535                             info.rti_ifa != rt->rt_ifa) {
  536                                 IFAREF(info.rti_ifa);
  537                                 rt->rt_ifa = info.rti_ifa;
  538                                 rt->rt_ifp = info.rti_ifp;
  539                         }
  540                         /* Allow some flags to be toggled on change. */
  541                         if (rtm->rtm_fmask & RTF_FMASK)
  542                                 rt->rt_flags = (rt->rt_flags &
  543                                     ~rtm->rtm_fmask) |
  544                                     (rtm->rtm_flags & rtm->rtm_fmask);
  545                         rt_setmetrics(rtm->rtm_inits, &rtm->rtm_rmx,
  546                                         &rt->rt_rmx);
  547                         rtm->rtm_index = rt->rt_ifp->if_index;
  548                         if (rt->rt_ifa && rt->rt_ifa->ifa_rtrequest)
  549                                rt->rt_ifa->ifa_rtrequest(RTM_ADD, rt, &info);
  550                         if (info.rti_info[RTAX_GENMASK])
  551                                 rt->rt_genmask = info.rti_info[RTAX_GENMASK];
  552                         /* FALLTHROUGH */
  553                 case RTM_LOCK:
  554                         /* We don't support locks anymore */
  555                         break;
  556                 }
  557                 RT_UNLOCK(rt);
  558                 break;
  559 
  560         default:
  561                 senderr(EOPNOTSUPP);
  562         }
  563 
  564 flush:
  565         if (rtm) {
  566                 if (error)
  567                         rtm->rtm_errno = error;
  568                 else
  569                         rtm->rtm_flags |= RTF_DONE;
  570         }
  571         if (rt)         /* XXX can this be true? */
  572                 RTFREE(rt);
  573     {
  574         struct rawcb *rp = NULL;
  575         /*
  576          * Check to see if we don't want our own messages.
  577          */
  578         if ((so->so_options & SO_USELOOPBACK) == 0) {
  579                 if (route_cb.any_count <= 1) {
  580                         if (rtm)
  581                                 Free(rtm);
  582                         m_freem(m);
  583                         return (error);
  584                 }
  585                 /* There is another listener, so construct message */
  586                 rp = sotorawcb(so);
  587         }
  588         if (rtm) {
  589                 m_copyback(m, 0, rtm->rtm_msglen, (caddr_t)rtm);
  590                 if (m->m_pkthdr.len < rtm->rtm_msglen) {
  591                         m_freem(m);
  592                         m = NULL;
  593                 } else if (m->m_pkthdr.len > rtm->rtm_msglen)
  594                         m_adj(m, rtm->rtm_msglen - m->m_pkthdr.len);
  595                 Free(rtm);
  596         }
  597         if (m) {
  598                 if (rp) {
  599                         /*
  600                          * XXX insure we don't get a copy by
  601                          * invalidating our protocol
  602                          */
  603                         unsigned short family = rp->rcb_proto.sp_family;
  604                         rp->rcb_proto.sp_family = 0;
  605                         rt_dispatch(m, info.rti_info[RTAX_DST]);
  606                         rp->rcb_proto.sp_family = family;
  607                 } else
  608                         rt_dispatch(m, info.rti_info[RTAX_DST]);
  609         }
  610     }
  611         return (error);
  612 #undef  sa_equal
  613 }
  614 
  615 static void
  616 rt_setmetrics(u_long which, const struct rt_metrics *in,
  617         struct rt_metrics_lite *out)
  618 {
  619 #define metric(f, e) if (which & (f)) out->e = in->e;
  620         /*
  621          * Only these are stored in the routing entry since introduction
  622          * of tcp hostcache. The rest is ignored.
  623          */
  624         metric(RTV_MTU, rmx_mtu);
  625         /* Userland -> kernel timebase conversion. */
  626         if (which & RTV_EXPIRE)
  627                 out->rmx_expire = in->rmx_expire ?
  628                     in->rmx_expire - time_second + time_uptime : 0;
  629 #undef metric
  630 }
  631 
  632 static void
  633 rt_getmetrics(const struct rt_metrics_lite *in, struct rt_metrics *out)
  634 {
  635 #define metric(e) out->e = in->e;
  636         bzero(out, sizeof(*out));
  637         metric(rmx_mtu);
  638         /* Kernel -> userland timebase conversion. */
  639         out->rmx_expire = in->rmx_expire ?
  640             in->rmx_expire - time_uptime + time_second : 0;
  641 #undef metric
  642 }
  643 
  644 /*
  645  * Extract the addresses of the passed sockaddrs.
  646  * Do a little sanity checking so as to avoid bad memory references.
  647  * This data is derived straight from userland.
  648  */
  649 static int
  650 rt_xaddrs(caddr_t cp, caddr_t cplim, struct rt_addrinfo *rtinfo)
  651 {
  652         struct sockaddr *sa;
  653         int i;
  654 
  655         for (i = 0; i < RTAX_MAX && cp < cplim; i++) {
  656                 if ((rtinfo->rti_addrs & (1 << i)) == 0)
  657                         continue;
  658                 sa = (struct sockaddr *)cp;
  659                 /*
  660                  * It won't fit.
  661                  */
  662                 if (cp + sa->sa_len > cplim)
  663                         return (EINVAL);
  664                 /*
  665                  * there are no more.. quit now
  666                  * If there are more bits, they are in error.
  667                  * I've seen this. route(1) can evidently generate these. 
  668                  * This causes kernel to core dump.
  669                  * for compatibility, If we see this, point to a safe address.
  670                  */
  671                 if (sa->sa_len == 0) {
  672                         rtinfo->rti_info[i] = &sa_zero;
  673                         return (0); /* should be EINVAL but for compat */
  674                 }
  675                 /* accept it */
  676                 rtinfo->rti_info[i] = sa;
  677                 cp += SA_SIZE(sa);
  678         }
  679         return (0);
  680 }
  681 
  682 static struct mbuf *
  683 rt_msg1(int type, struct rt_addrinfo *rtinfo)
  684 {
  685         struct rt_msghdr *rtm;
  686         struct mbuf *m;
  687         int i;
  688         struct sockaddr *sa;
  689         int len, dlen;
  690 
  691         switch (type) {
  692 
  693         case RTM_DELADDR:
  694         case RTM_NEWADDR:
  695                 len = sizeof(struct ifa_msghdr);
  696                 break;
  697 
  698         case RTM_DELMADDR:
  699         case RTM_NEWMADDR:
  700                 len = sizeof(struct ifma_msghdr);
  701                 break;
  702 
  703         case RTM_IFINFO:
  704                 len = sizeof(struct if_msghdr);
  705                 break;
  706 
  707         case RTM_IFANNOUNCE:
  708         case RTM_IEEE80211:
  709                 len = sizeof(struct if_announcemsghdr);
  710                 break;
  711 
  712         default:
  713                 len = sizeof(struct rt_msghdr);
  714         }
  715         if (len > MCLBYTES)
  716                 panic("rt_msg1");
  717         m = m_gethdr(M_DONTWAIT, MT_DATA);
  718         if (m && len > MHLEN) {
  719                 MCLGET(m, M_DONTWAIT);
  720                 if ((m->m_flags & M_EXT) == 0) {
  721                         m_free(m);
  722                         m = NULL;
  723                 }
  724         }
  725         if (m == NULL)
  726                 return (m);
  727         m->m_pkthdr.len = m->m_len = len;
  728         m->m_pkthdr.rcvif = NULL;
  729         rtm = mtod(m, struct rt_msghdr *);
  730         bzero((caddr_t)rtm, len);
  731         for (i = 0; i < RTAX_MAX; i++) {
  732                 if ((sa = rtinfo->rti_info[i]) == NULL)
  733                         continue;
  734                 rtinfo->rti_addrs |= (1 << i);
  735                 dlen = SA_SIZE(sa);
  736                 m_copyback(m, len, dlen, (caddr_t)sa);
  737                 len += dlen;
  738         }
  739         if (m->m_pkthdr.len != len) {
  740                 m_freem(m);
  741                 return (NULL);
  742         }
  743         rtm->rtm_msglen = len;
  744         rtm->rtm_version = RTM_VERSION;
  745         rtm->rtm_type = type;
  746         return (m);
  747 }
  748 
  749 static int
  750 rt_msg2(int type, struct rt_addrinfo *rtinfo, caddr_t cp, struct walkarg *w)
  751 {
  752         int i;
  753         int len, dlen, second_time = 0;
  754         caddr_t cp0;
  755 
  756         rtinfo->rti_addrs = 0;
  757 again:
  758         switch (type) {
  759 
  760         case RTM_DELADDR:
  761         case RTM_NEWADDR:
  762                 len = sizeof(struct ifa_msghdr);
  763                 break;
  764 
  765         case RTM_IFINFO:
  766                 len = sizeof(struct if_msghdr);
  767                 break;
  768 
  769         case RTM_NEWMADDR:
  770                 len = sizeof(struct ifma_msghdr);
  771                 break;
  772 
  773         default:
  774                 len = sizeof(struct rt_msghdr);
  775         }
  776         cp0 = cp;
  777         if (cp0)
  778                 cp += len;
  779         for (i = 0; i < RTAX_MAX; i++) {
  780                 struct sockaddr *sa;
  781 
  782                 if ((sa = rtinfo->rti_info[i]) == NULL)
  783                         continue;
  784                 rtinfo->rti_addrs |= (1 << i);
  785                 dlen = SA_SIZE(sa);
  786                 if (cp) {
  787                         bcopy((caddr_t)sa, cp, (unsigned)dlen);
  788                         cp += dlen;
  789                 }
  790                 len += dlen;
  791         }
  792         len = ALIGN(len);
  793         if (cp == NULL && w != NULL && !second_time) {
  794                 struct walkarg *rw = w;
  795 
  796                 if (rw->w_req) {
  797                         if (rw->w_tmemsize < len) {
  798                                 if (rw->w_tmem)
  799                                         free(rw->w_tmem, M_RTABLE);
  800                                 rw->w_tmem = (caddr_t)
  801                                         malloc(len, M_RTABLE, M_NOWAIT);
  802                                 if (rw->w_tmem)
  803                                         rw->w_tmemsize = len;
  804                         }
  805                         if (rw->w_tmem) {
  806                                 cp = rw->w_tmem;
  807                                 second_time = 1;
  808                                 goto again;
  809                         }
  810                 }
  811         }
  812         if (cp) {
  813                 struct rt_msghdr *rtm = (struct rt_msghdr *)cp0;
  814 
  815                 rtm->rtm_version = RTM_VERSION;
  816                 rtm->rtm_type = type;
  817                 rtm->rtm_msglen = len;
  818         }
  819         return (len);
  820 }
  821 
  822 /*
  823  * This routine is called to generate a message from the routing
  824  * socket indicating that a redirect has occured, a routing lookup
  825  * has failed, or that a protocol has detected timeouts to a particular
  826  * destination.
  827  */
  828 void
  829 rt_missmsg(int type, struct rt_addrinfo *rtinfo, int flags, int error)
  830 {
  831         struct rt_msghdr *rtm;
  832         struct mbuf *m;
  833         struct sockaddr *sa = rtinfo->rti_info[RTAX_DST];
  834 
  835         if (route_cb.any_count == 0)
  836                 return;
  837         m = rt_msg1(type, rtinfo);
  838         if (m == NULL)
  839                 return;
  840         rtm = mtod(m, struct rt_msghdr *);
  841         rtm->rtm_flags = RTF_DONE | flags;
  842         rtm->rtm_errno = error;
  843         rtm->rtm_addrs = rtinfo->rti_addrs;
  844         rt_dispatch(m, sa);
  845 }
  846 
  847 /*
  848  * This routine is called to generate a message from the routing
  849  * socket indicating that the status of a network interface has changed.
  850  */
  851 void
  852 rt_ifmsg(struct ifnet *ifp)
  853 {
  854         struct if_msghdr *ifm;
  855         struct mbuf *m;
  856         struct rt_addrinfo info;
  857 
  858         if (route_cb.any_count == 0)
  859                 return;
  860         bzero((caddr_t)&info, sizeof(info));
  861         m = rt_msg1(RTM_IFINFO, &info);
  862         if (m == NULL)
  863                 return;
  864         ifm = mtod(m, struct if_msghdr *);
  865         ifm->ifm_index = ifp->if_index;
  866         ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
  867         ifm->ifm_data = ifp->if_data;
  868         ifm->ifm_addrs = 0;
  869         rt_dispatch(m, NULL);
  870 }
  871 
  872 /*
  873  * This is called to generate messages from the routing socket
  874  * indicating a network interface has had addresses associated with it.
  875  * if we ever reverse the logic and replace messages TO the routing
  876  * socket indicate a request to configure interfaces, then it will
  877  * be unnecessary as the routing socket will automatically generate
  878  * copies of it.
  879  */
  880 void
  881 rt_newaddrmsg(int cmd, struct ifaddr *ifa, int error, struct rtentry *rt)
  882 {
  883         struct rt_addrinfo info;
  884         struct sockaddr *sa = NULL;
  885         int pass;
  886         struct mbuf *m = NULL;
  887         struct ifnet *ifp = ifa->ifa_ifp;
  888 
  889         KASSERT(cmd == RTM_ADD || cmd == RTM_DELETE,
  890                 ("unexpected cmd %u", cmd));
  891 #ifdef SCTP
  892         /*
  893          * notify the SCTP stack
  894          * this will only get called when an address is added/deleted
  895          * XXX pass the ifaddr struct instead if ifa->ifa_addr...
  896          */
  897         sctp_addr_change(ifa, cmd);
  898 #endif /* SCTP */
  899         if (route_cb.any_count == 0)
  900                 return;
  901         for (pass = 1; pass < 3; pass++) {
  902                 bzero((caddr_t)&info, sizeof(info));
  903                 if ((cmd == RTM_ADD && pass == 1) ||
  904                     (cmd == RTM_DELETE && pass == 2)) {
  905                         struct ifa_msghdr *ifam;
  906                         int ncmd = cmd == RTM_ADD ? RTM_NEWADDR : RTM_DELADDR;
  907 
  908                         info.rti_info[RTAX_IFA] = sa = ifa->ifa_addr;
  909                         info.rti_info[RTAX_IFP] = ifp->if_addr->ifa_addr;
  910                         info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
  911                         info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
  912                         if ((m = rt_msg1(ncmd, &info)) == NULL)
  913                                 continue;
  914                         ifam = mtod(m, struct ifa_msghdr *);
  915                         ifam->ifam_index = ifp->if_index;
  916                         ifam->ifam_metric = ifa->ifa_metric;
  917                         ifam->ifam_flags = ifa->ifa_flags;
  918                         ifam->ifam_addrs = info.rti_addrs;
  919                 }
  920                 if ((cmd == RTM_ADD && pass == 2) ||
  921                     (cmd == RTM_DELETE && pass == 1)) {
  922                         struct rt_msghdr *rtm;
  923 
  924                         if (rt == NULL)
  925                                 continue;
  926                         info.rti_info[RTAX_NETMASK] = rt_mask(rt);
  927                         info.rti_info[RTAX_DST] = sa = rt_key(rt);
  928                         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
  929                         if ((m = rt_msg1(cmd, &info)) == NULL)
  930                                 continue;
  931                         rtm = mtod(m, struct rt_msghdr *);
  932                         rtm->rtm_index = ifp->if_index;
  933                         rtm->rtm_flags |= rt->rt_flags;
  934                         rtm->rtm_errno = error;
  935                         rtm->rtm_addrs = info.rti_addrs;
  936                 }
  937                 rt_dispatch(m, sa);
  938         }
  939 }
  940 
  941 /*
  942  * This is the analogue to the rt_newaddrmsg which performs the same
  943  * function but for multicast group memberhips.  This is easier since
  944  * there is no route state to worry about.
  945  */
  946 void
  947 rt_newmaddrmsg(int cmd, struct ifmultiaddr *ifma)
  948 {
  949         struct rt_addrinfo info;
  950         struct mbuf *m = NULL;
  951         struct ifnet *ifp = ifma->ifma_ifp;
  952         struct ifma_msghdr *ifmam;
  953 
  954         if (route_cb.any_count == 0)
  955                 return;
  956 
  957         bzero((caddr_t)&info, sizeof(info));
  958         info.rti_info[RTAX_IFA] = ifma->ifma_addr;
  959         info.rti_info[RTAX_IFP] = ifp ? ifp->if_addr->ifa_addr : NULL;
  960         /*
  961          * If a link-layer address is present, present it as a ``gateway''
  962          * (similarly to how ARP entries, e.g., are presented).
  963          */
  964         info.rti_info[RTAX_GATEWAY] = ifma->ifma_lladdr;
  965         m = rt_msg1(cmd, &info);
  966         if (m == NULL)
  967                 return;
  968         ifmam = mtod(m, struct ifma_msghdr *);
  969         KASSERT(ifp != NULL, ("%s: link-layer multicast address w/o ifp\n",
  970             __func__));
  971         ifmam->ifmam_index = ifp->if_index;
  972         ifmam->ifmam_addrs = info.rti_addrs;
  973         rt_dispatch(m, ifma->ifma_addr);
  974 }
  975 
  976 static struct mbuf *
  977 rt_makeifannouncemsg(struct ifnet *ifp, int type, int what,
  978         struct rt_addrinfo *info)
  979 {
  980         struct if_announcemsghdr *ifan;
  981         struct mbuf *m;
  982 
  983         if (route_cb.any_count == 0)
  984                 return NULL;
  985         bzero((caddr_t)info, sizeof(*info));
  986         m = rt_msg1(type, info);
  987         if (m != NULL) {
  988                 ifan = mtod(m, struct if_announcemsghdr *);
  989                 ifan->ifan_index = ifp->if_index;
  990                 strlcpy(ifan->ifan_name, ifp->if_xname,
  991                         sizeof(ifan->ifan_name));
  992                 ifan->ifan_what = what;
  993         }
  994         return m;
  995 }
  996 
  997 /*
  998  * This is called to generate routing socket messages indicating
  999  * IEEE80211 wireless events.
 1000  * XXX we piggyback on the RTM_IFANNOUNCE msg format in a clumsy way.
 1001  */
 1002 void
 1003 rt_ieee80211msg(struct ifnet *ifp, int what, void *data, size_t data_len)
 1004 {
 1005         struct mbuf *m;
 1006         struct rt_addrinfo info;
 1007 
 1008         m = rt_makeifannouncemsg(ifp, RTM_IEEE80211, what, &info);
 1009         if (m != NULL) {
 1010                 /*
 1011                  * Append the ieee80211 data.  Try to stick it in the
 1012                  * mbuf containing the ifannounce msg; otherwise allocate
 1013                  * a new mbuf and append.
 1014                  *
 1015                  * NB: we assume m is a single mbuf.
 1016                  */
 1017                 if (data_len > M_TRAILINGSPACE(m)) {
 1018                         struct mbuf *n = m_get(M_NOWAIT, MT_DATA);
 1019                         if (n == NULL) {
 1020                                 m_freem(m);
 1021                                 return;
 1022                         }
 1023                         bcopy(data, mtod(n, void *), data_len);
 1024                         n->m_len = data_len;
 1025                         m->m_next = n;
 1026                 } else if (data_len > 0) {
 1027                         bcopy(data, mtod(m, u_int8_t *) + m->m_len, data_len);
 1028                         m->m_len += data_len;
 1029                 }
 1030                 if (m->m_flags & M_PKTHDR)
 1031                         m->m_pkthdr.len += data_len;
 1032                 mtod(m, struct if_announcemsghdr *)->ifan_msglen += data_len;
 1033                 rt_dispatch(m, NULL);
 1034         }
 1035 }
 1036 
 1037 /*
 1038  * This is called to generate routing socket messages indicating
 1039  * network interface arrival and departure.
 1040  */
 1041 void
 1042 rt_ifannouncemsg(struct ifnet *ifp, int what)
 1043 {
 1044         struct mbuf *m;
 1045         struct rt_addrinfo info;
 1046 
 1047         m = rt_makeifannouncemsg(ifp, RTM_IFANNOUNCE, what, &info);
 1048         if (m != NULL)
 1049                 rt_dispatch(m, NULL);
 1050 }
 1051 
 1052 static void
 1053 rt_dispatch(struct mbuf *m, const struct sockaddr *sa)
 1054 {
 1055         struct m_tag *tag;
 1056 
 1057         /*
 1058          * Preserve the family from the sockaddr, if any, in an m_tag for
 1059          * use when injecting the mbuf into the routing socket buffer from
 1060          * the netisr.
 1061          */
 1062         if (sa != NULL) {
 1063                 tag = m_tag_get(PACKET_TAG_RTSOCKFAM, sizeof(unsigned short),
 1064                     M_NOWAIT);
 1065                 if (tag == NULL) {
 1066                         m_freem(m);
 1067                         return;
 1068                 }
 1069                 *(unsigned short *)(tag + 1) = sa->sa_family;
 1070                 m_tag_prepend(m, tag);
 1071         }
 1072         netisr_queue(NETISR_ROUTE, m);  /* mbuf is free'd on failure. */
 1073 }
 1074 
 1075 /*
 1076  * This is used in dumping the kernel table via sysctl().
 1077  */
 1078 static int
 1079 sysctl_dumpentry(struct radix_node *rn, void *vw)
 1080 {
 1081         struct walkarg *w = vw;
 1082         struct rtentry *rt = (struct rtentry *)rn;
 1083         int error = 0, size;
 1084         struct rt_addrinfo info;
 1085 
 1086         if (w->w_op == NET_RT_FLAGS && !(rt->rt_flags & w->w_arg))
 1087                 return 0;
 1088         bzero((caddr_t)&info, sizeof(info));
 1089         info.rti_info[RTAX_DST] = rt_key(rt);
 1090         info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
 1091         info.rti_info[RTAX_NETMASK] = rt_mask(rt);
 1092         info.rti_info[RTAX_GENMASK] = rt->rt_genmask;
 1093         if (rt->rt_ifp) {
 1094                 info.rti_info[RTAX_IFP] = rt->rt_ifp->if_addr->ifa_addr;
 1095                 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr;
 1096                 if (rt->rt_ifp->if_flags & IFF_POINTOPOINT)
 1097                         info.rti_info[RTAX_BRD] = rt->rt_ifa->ifa_dstaddr;
 1098         }
 1099         size = rt_msg2(RTM_GET, &info, NULL, w);
 1100         if (w->w_req && w->w_tmem) {
 1101                 struct rt_msghdr *rtm = (struct rt_msghdr *)w->w_tmem;
 1102 
 1103                 rtm->rtm_flags = rt->rt_flags;
 1104                 rtm->rtm_use = rt->rt_rmx.rmx_pksent;
 1105                 rt_getmetrics(&rt->rt_rmx, &rtm->rtm_rmx);
 1106                 rtm->rtm_index = rt->rt_ifp->if_index;
 1107                 rtm->rtm_errno = rtm->rtm_pid = rtm->rtm_seq = 0;
 1108                 rtm->rtm_addrs = info.rti_addrs;
 1109                 error = SYSCTL_OUT(w->w_req, (caddr_t)rtm, size);
 1110                 return (error);
 1111         }
 1112         return (error);
 1113 }
 1114 
 1115 static int
 1116 sysctl_iflist(int af, struct walkarg *w)
 1117 {
 1118         struct ifnet *ifp;
 1119         struct ifaddr *ifa;
 1120         struct rt_addrinfo info;
 1121         int len, error = 0;
 1122 
 1123         bzero((caddr_t)&info, sizeof(info));
 1124         IFNET_RLOCK();
 1125         TAILQ_FOREACH(ifp, &ifnet, if_link) {
 1126                 if (w->w_arg && w->w_arg != ifp->if_index)
 1127                         continue;
 1128                 ifa = ifp->if_addr;
 1129                 info.rti_info[RTAX_IFP] = ifa->ifa_addr;
 1130                 len = rt_msg2(RTM_IFINFO, &info, NULL, w);
 1131                 info.rti_info[RTAX_IFP] = NULL;
 1132                 if (w->w_req && w->w_tmem) {
 1133                         struct if_msghdr *ifm;
 1134 
 1135                         ifm = (struct if_msghdr *)w->w_tmem;
 1136                         ifm->ifm_index = ifp->if_index;
 1137                         ifm->ifm_flags = ifp->if_flags | ifp->if_drv_flags;
 1138                         ifm->ifm_data = ifp->if_data;
 1139                         ifm->ifm_addrs = info.rti_addrs;
 1140                         error = SYSCTL_OUT(w->w_req,(caddr_t)ifm, len);
 1141                         if (error)
 1142                                 goto done;
 1143                 }
 1144                 while ((ifa = TAILQ_NEXT(ifa, ifa_link)) != NULL) {
 1145                         if (af && af != ifa->ifa_addr->sa_family)
 1146                                 continue;
 1147                         if (jailed(curthread->td_ucred) &&
 1148                             prison_if(curthread->td_ucred, ifa->ifa_addr))
 1149                                 continue;
 1150                         info.rti_info[RTAX_IFA] = ifa->ifa_addr;
 1151                         info.rti_info[RTAX_NETMASK] = ifa->ifa_netmask;
 1152                         info.rti_info[RTAX_BRD] = ifa->ifa_dstaddr;
 1153                         len = rt_msg2(RTM_NEWADDR, &info, NULL, w);
 1154                         if (w->w_req && w->w_tmem) {
 1155                                 struct ifa_msghdr *ifam;
 1156 
 1157                                 ifam = (struct ifa_msghdr *)w->w_tmem;
 1158                                 ifam->ifam_index = ifa->ifa_ifp->if_index;
 1159                                 ifam->ifam_flags = ifa->ifa_flags;
 1160                                 ifam->ifam_metric = ifa->ifa_metric;
 1161                                 ifam->ifam_addrs = info.rti_addrs;
 1162                                 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
 1163                                 if (error)
 1164                                         goto done;
 1165                         }
 1166                 }
 1167                 info.rti_info[RTAX_IFA] = info.rti_info[RTAX_NETMASK] =
 1168                         info.rti_info[RTAX_BRD] = NULL;
 1169         }
 1170 done:
 1171         IFNET_RUNLOCK();
 1172         return (error);
 1173 }
 1174 
 1175 int
 1176 sysctl_ifmalist(int af, struct walkarg *w)
 1177 {
 1178         struct ifnet *ifp;
 1179         struct ifmultiaddr *ifma;
 1180         struct  rt_addrinfo info;
 1181         int     len, error = 0;
 1182         struct ifaddr *ifa;
 1183 
 1184         bzero((caddr_t)&info, sizeof(info));
 1185         IFNET_RLOCK();
 1186         TAILQ_FOREACH(ifp, &ifnet, if_link) {
 1187                 if (w->w_arg && w->w_arg != ifp->if_index)
 1188                         continue;
 1189                 ifa = ifp->if_addr;
 1190                 info.rti_info[RTAX_IFP] = ifa ? ifa->ifa_addr : NULL;
 1191                 IF_ADDR_LOCK(ifp);
 1192                 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 1193                         if (af && af != ifma->ifma_addr->sa_family)
 1194                                 continue;
 1195                         if (jailed(curproc->p_ucred) &&
 1196                             prison_if(curproc->p_ucred, ifma->ifma_addr))
 1197                                 continue;
 1198                         info.rti_info[RTAX_IFA] = ifma->ifma_addr;
 1199                         info.rti_info[RTAX_GATEWAY] =
 1200                             (ifma->ifma_addr->sa_family != AF_LINK) ?
 1201                             ifma->ifma_lladdr : NULL;
 1202                         len = rt_msg2(RTM_NEWMADDR, &info, NULL, w);
 1203                         if (w->w_req && w->w_tmem) {
 1204                                 struct ifma_msghdr *ifmam;
 1205 
 1206                                 ifmam = (struct ifma_msghdr *)w->w_tmem;
 1207                                 ifmam->ifmam_index = ifma->ifma_ifp->if_index;
 1208                                 ifmam->ifmam_flags = 0;
 1209                                 ifmam->ifmam_addrs = info.rti_addrs;
 1210                                 error = SYSCTL_OUT(w->w_req, w->w_tmem, len);
 1211                                 if (error) {
 1212                                         IF_ADDR_UNLOCK(ifp);
 1213                                         goto done;
 1214                                 }
 1215                         }
 1216                 }
 1217                 IF_ADDR_UNLOCK(ifp);
 1218         }
 1219 done:
 1220         IFNET_RUNLOCK();
 1221         return (error);
 1222 }
 1223 
 1224 static int
 1225 sysctl_rtsock(SYSCTL_HANDLER_ARGS)
 1226 {
 1227         int     *name = (int *)arg1;
 1228         u_int   namelen = arg2;
 1229         struct radix_node_head *rnh;
 1230         int     i, lim, error = EINVAL;
 1231         u_char  af;
 1232         struct  walkarg w;
 1233 
 1234         name ++;
 1235         namelen--;
 1236         if (req->newptr)
 1237                 return (EPERM);
 1238         if (namelen != 3)
 1239                 return ((namelen < 3) ? EISDIR : ENOTDIR);
 1240         af = name[0];
 1241         if (af > AF_MAX)
 1242                 return (EINVAL);
 1243         bzero(&w, sizeof(w));
 1244         w.w_op = name[1];
 1245         w.w_arg = name[2];
 1246         w.w_req = req;
 1247 
 1248         error = sysctl_wire_old_buffer(req, 0);
 1249         if (error)
 1250                 return (error);
 1251         switch (w.w_op) {
 1252 
 1253         case NET_RT_DUMP:
 1254         case NET_RT_FLAGS:
 1255                 if (af == 0) {                  /* dump all tables */
 1256                         i = 1;
 1257                         lim = AF_MAX;
 1258                 } else                          /* dump only one table */
 1259                         i = lim = af;
 1260                 for (error = 0; error == 0 && i <= lim; i++)
 1261                         if ((rnh = rt_tables[i]) != NULL) {
 1262                                 RADIX_NODE_HEAD_LOCK(rnh); 
 1263                                 error = rnh->rnh_walktree(rnh,
 1264                                     sysctl_dumpentry, &w);
 1265                                 RADIX_NODE_HEAD_UNLOCK(rnh);
 1266                         } else if (af != 0)
 1267                                 error = EAFNOSUPPORT;
 1268                 break;
 1269 
 1270         case NET_RT_IFLIST:
 1271                 error = sysctl_iflist(af, &w);
 1272                 break;
 1273 
 1274         case NET_RT_IFMALIST:
 1275                 error = sysctl_ifmalist(af, &w);
 1276                 break;
 1277         }
 1278         if (w.w_tmem)
 1279                 free(w.w_tmem, M_RTABLE);
 1280         return (error);
 1281 }
 1282 
 1283 SYSCTL_NODE(_net, PF_ROUTE, routetable, CTLFLAG_RD, sysctl_rtsock, "");
 1284 
 1285 /*
 1286  * Definitions of protocols supported in the ROUTE domain.
 1287  */
 1288 
 1289 static struct domain routedomain;               /* or at least forward */
 1290 
 1291 static struct protosw routesw[] = {
 1292 {
 1293         .pr_type =              SOCK_RAW,
 1294         .pr_domain =            &routedomain,
 1295         .pr_flags =             PR_ATOMIC|PR_ADDR,
 1296         .pr_output =            route_output,
 1297         .pr_ctlinput =          raw_ctlinput,
 1298         .pr_init =              raw_init,
 1299         .pr_usrreqs =           &route_usrreqs
 1300 }
 1301 };
 1302 
 1303 static struct domain routedomain = {
 1304         .dom_family =           PF_ROUTE,
 1305         .dom_name =              "route",
 1306         .dom_protosw =          routesw,
 1307         .dom_protoswNPROTOSW =  &routesw[sizeof(routesw)/sizeof(routesw[0])]
 1308 };
 1309 
 1310 DOMAIN_SET(route);

Cache object: d2d2f0de1372a05ed88191af9a80dcc4


[ 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.