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

Cache object: c6d43c93b42c9e2a3d74b5bd928a2b59


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