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

Cache object: 03df0831eea883cdfebb5915780af808


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