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/netinet/ip_divert.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) 1982, 1986, 1988, 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 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD: releng/8.1/sys/netinet/ip_divert.c 208766 2010-06-03 08:55:45Z rwatson $");
   32 
   33 #if !defined(KLD_MODULE)
   34 #include "opt_inet.h"
   35 #include "opt_sctp.h"
   36 #ifndef INET
   37 #error "IPDIVERT requires INET."
   38 #endif
   39 #endif
   40 
   41 #include <sys/param.h>
   42 #include <sys/kernel.h>
   43 #include <sys/lock.h>
   44 #include <sys/malloc.h>
   45 #include <sys/mbuf.h>
   46 #include <sys/module.h>
   47 #include <sys/kernel.h>
   48 #include <sys/priv.h>
   49 #include <sys/proc.h>
   50 #include <sys/protosw.h>
   51 #include <sys/rwlock.h>
   52 #include <sys/signalvar.h>
   53 #include <sys/socket.h>
   54 #include <sys/socketvar.h>
   55 #include <sys/sx.h>
   56 #include <sys/sysctl.h>
   57 #include <sys/systm.h>
   58 
   59 #include <vm/uma.h>
   60 
   61 #include <net/if.h>
   62 #include <net/netisr.h> 
   63 #include <net/route.h>
   64 #include <net/vnet.h>
   65 
   66 #include <netinet/in.h>
   67 #include <netinet/in_pcb.h>
   68 #include <netinet/in_systm.h>
   69 #include <netinet/in_var.h>
   70 #include <netinet/ip.h>
   71 #include <netinet/ip_var.h>
   72 #ifdef SCTP
   73 #include <netinet/sctp_crc32.h>
   74 #endif
   75 
   76 #include <security/mac/mac_framework.h>
   77 
   78 /*
   79  * Divert sockets
   80  */
   81 
   82 /*
   83  * Allocate enough space to hold a full IP packet
   84  */
   85 #define DIVSNDQ         (65536 + 100)
   86 #define DIVRCVQ         (65536 + 100)
   87 
   88 /*
   89  * Divert sockets work in conjunction with ipfw or other packet filters,
   90  * see the divert(4) manpage for features.
   91  * Packets are selected by the packet filter and tagged with an
   92  * MTAG_IPFW_RULE tag carrying the 'divert port' number (as set by
   93  * the packet filter) and information on the matching filter rule for
   94  * subsequent reinjection. The divert_port is used to put the packet
   95  * on the corresponding divert socket, while the rule number is passed
   96  * up (at least partially) as the sin_port in the struct sockaddr.
   97  *
   98  * Packets written to the divert socket carry in sin_addr a
   99  * destination address, and in sin_port the number of the filter rule
  100  * after which to continue processing.
  101  * If the destination address is INADDR_ANY, the packet is treated as
  102  * as outgoing and sent to ip_output(); otherwise it is treated as
  103  * incoming and sent to ip_input().
  104  * Further, sin_zero carries some information on the interface,
  105  * which can be used in the reinject -- see comments in the code.
  106  *
  107  * On reinjection, processing in ip_input() and ip_output()
  108  * will be exactly the same as for the original packet, except that
  109  * packet filter processing will start at the rule number after the one
  110  * written in the sin_port (ipfw does not allow a rule #0, so sin_port=0
  111  * will apply the entire ruleset to the packet).
  112  */
  113 
  114 /* Internal variables. */
  115 static VNET_DEFINE(struct inpcbhead, divcb);
  116 static VNET_DEFINE(struct inpcbinfo, divcbinfo);
  117 
  118 #define V_divcb                         VNET(divcb)
  119 #define V_divcbinfo                     VNET(divcbinfo)
  120 
  121 static u_long   div_sendspace = DIVSNDQ;        /* XXX sysctl ? */
  122 static u_long   div_recvspace = DIVRCVQ;        /* XXX sysctl ? */
  123 
  124 static eventhandler_tag ip_divert_event_tag;
  125 
  126 /*
  127  * Initialize divert connection block queue.
  128  */
  129 static void
  130 div_zone_change(void *tag)
  131 {
  132 
  133         uma_zone_set_max(V_divcbinfo.ipi_zone, maxsockets);
  134 }
  135 
  136 static int
  137 div_inpcb_init(void *mem, int size, int flags)
  138 {
  139         struct inpcb *inp = mem;
  140 
  141         INP_LOCK_INIT(inp, "inp", "divinp");
  142         return (0);
  143 }
  144 
  145 static void
  146 div_inpcb_fini(void *mem, int size)
  147 {
  148         struct inpcb *inp = mem;
  149 
  150         INP_LOCK_DESTROY(inp);
  151 }
  152 
  153 static void
  154 div_init(void)
  155 {
  156 
  157         INP_INFO_LOCK_INIT(&V_divcbinfo, "div");
  158         LIST_INIT(&V_divcb);
  159         V_divcbinfo.ipi_listhead = &V_divcb;
  160 #ifdef VIMAGE
  161         V_divcbinfo.ipi_vnet = curvnet;
  162 #endif
  163         /*
  164          * XXX We don't use the hash list for divert IP, but it's easier
  165          * to allocate a one entry hash list than it is to check all
  166          * over the place for hashbase == NULL.
  167          */
  168         V_divcbinfo.ipi_hashbase = hashinit(1, M_PCB, &V_divcbinfo.ipi_hashmask);
  169         V_divcbinfo.ipi_porthashbase = hashinit(1, M_PCB,
  170             &V_divcbinfo.ipi_porthashmask);
  171         V_divcbinfo.ipi_zone = uma_zcreate("divcb", sizeof(struct inpcb),
  172             NULL, NULL, div_inpcb_init, div_inpcb_fini, UMA_ALIGN_PTR,
  173             UMA_ZONE_NOFREE);
  174         uma_zone_set_max(V_divcbinfo.ipi_zone, maxsockets);
  175 }
  176 
  177 static void
  178 div_destroy(void)
  179 {
  180 
  181         INP_INFO_LOCK_DESTROY(&V_divcbinfo);
  182         uma_zdestroy(V_divcbinfo.ipi_zone);
  183         hashdestroy(V_divcbinfo.ipi_hashbase, M_PCB, V_divcbinfo.ipi_hashmask);
  184         hashdestroy(V_divcbinfo.ipi_porthashbase, M_PCB,
  185             V_divcbinfo.ipi_porthashmask);
  186 }
  187 
  188 /*
  189  * IPPROTO_DIVERT is not in the real IP protocol number space; this
  190  * function should never be called.  Just in case, drop any packets.
  191  */
  192 static void
  193 div_input(struct mbuf *m, int off)
  194 {
  195 
  196         KMOD_IPSTAT_INC(ips_noproto);
  197         m_freem(m);
  198 }
  199 
  200 /*
  201  * Divert a packet by passing it up to the divert socket at port 'port'.
  202  *
  203  * Setup generic address and protocol structures for div_input routine,
  204  * then pass them along with mbuf chain.
  205  */
  206 static void
  207 divert_packet(struct mbuf *m, int incoming)
  208 {
  209         struct ip *ip;
  210         struct inpcb *inp;
  211         struct socket *sa;
  212         u_int16_t nport;
  213         struct sockaddr_in divsrc;
  214         struct m_tag *mtag;
  215 
  216         mtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL);
  217         if (mtag == NULL) {
  218                 m_freem(m);
  219                 return;
  220         }
  221         /* Assure header */
  222         if (m->m_len < sizeof(struct ip) &&
  223             (m = m_pullup(m, sizeof(struct ip))) == 0)
  224                 return;
  225         ip = mtod(m, struct ip *);
  226 
  227         /* Delayed checksums are currently not compatible with divert. */
  228         if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
  229                 ip->ip_len = ntohs(ip->ip_len);
  230                 in_delayed_cksum(m);
  231                 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
  232                 ip->ip_len = htons(ip->ip_len);
  233         }
  234 #ifdef SCTP
  235         if (m->m_pkthdr.csum_flags & CSUM_SCTP) {
  236                 ip->ip_len = ntohs(ip->ip_len);
  237                 sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2));
  238                 m->m_pkthdr.csum_flags &= ~CSUM_SCTP;
  239                 ip->ip_len = htons(ip->ip_len);
  240         }
  241 #endif
  242         bzero(&divsrc, sizeof(divsrc));
  243         divsrc.sin_len = sizeof(divsrc);
  244         divsrc.sin_family = AF_INET;
  245         /* record matching rule, in host format */
  246         divsrc.sin_port = ((struct ipfw_rule_ref *)(mtag+1))->rulenum;
  247         /*
  248          * Record receive interface address, if any.
  249          * But only for incoming packets.
  250          */
  251         if (incoming) {
  252                 struct ifaddr *ifa;
  253                 struct ifnet *ifp;
  254 
  255                 /* Sanity check */
  256                 M_ASSERTPKTHDR(m);
  257 
  258                 /* Find IP address for receive interface */
  259                 ifp = m->m_pkthdr.rcvif;
  260                 if_addr_rlock(ifp);
  261                 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
  262                         if (ifa->ifa_addr->sa_family != AF_INET)
  263                                 continue;
  264                         divsrc.sin_addr =
  265                             ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
  266                         break;
  267                 }
  268                 if_addr_runlock(ifp);
  269         }
  270         /*
  271          * Record the incoming interface name whenever we have one.
  272          */
  273         if (m->m_pkthdr.rcvif) {
  274                 /*
  275                  * Hide the actual interface name in there in the 
  276                  * sin_zero array. XXX This needs to be moved to a
  277                  * different sockaddr type for divert, e.g.
  278                  * sockaddr_div with multiple fields like 
  279                  * sockaddr_dl. Presently we have only 7 bytes
  280                  * but that will do for now as most interfaces
  281                  * are 4 or less + 2 or less bytes for unit.
  282                  * There is probably a faster way of doing this,
  283                  * possibly taking it from the sockaddr_dl on the iface.
  284                  * This solves the problem of a P2P link and a LAN interface
  285                  * having the same address, which can result in the wrong
  286                  * interface being assigned to the packet when fed back
  287                  * into the divert socket. Theoretically if the daemon saves
  288                  * and re-uses the sockaddr_in as suggested in the man pages,
  289                  * this iface name will come along for the ride.
  290                  * (see div_output for the other half of this.)
  291                  */ 
  292                 strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname,
  293                     sizeof(divsrc.sin_zero));
  294         }
  295 
  296         /* Put packet on socket queue, if any */
  297         sa = NULL;
  298         nport = htons((u_int16_t)(((struct ipfw_rule_ref *)(mtag+1))->info));
  299         INP_INFO_RLOCK(&V_divcbinfo);
  300         LIST_FOREACH(inp, &V_divcb, inp_list) {
  301                 /* XXX why does only one socket match? */
  302                 if (inp->inp_lport == nport) {
  303                         INP_RLOCK(inp);
  304                         sa = inp->inp_socket;
  305                         SOCKBUF_LOCK(&sa->so_rcv);
  306                         if (sbappendaddr_locked(&sa->so_rcv,
  307                             (struct sockaddr *)&divsrc, m,
  308                             (struct mbuf *)0) == 0) {
  309                                 SOCKBUF_UNLOCK(&sa->so_rcv);
  310                                 sa = NULL;      /* force mbuf reclaim below */
  311                         } else
  312                                 sorwakeup_locked(sa);
  313                         INP_RUNLOCK(inp);
  314                         break;
  315                 }
  316         }
  317         INP_INFO_RUNLOCK(&V_divcbinfo);
  318         if (sa == NULL) {
  319                 m_freem(m);
  320                 KMOD_IPSTAT_INC(ips_noproto);
  321                 KMOD_IPSTAT_DEC(ips_delivered);
  322         }
  323 }
  324 
  325 /*
  326  * Deliver packet back into the IP processing machinery.
  327  *
  328  * If no address specified, or address is 0.0.0.0, send to ip_output();
  329  * otherwise, send to ip_input() and mark as having been received on
  330  * the interface with that address.
  331  */
  332 static int
  333 div_output(struct socket *so, struct mbuf *m, struct sockaddr_in *sin,
  334     struct mbuf *control)
  335 {
  336         struct m_tag *mtag;
  337         struct ipfw_rule_ref *dt;
  338         int error = 0;
  339         struct mbuf *options;
  340 
  341         /*
  342          * An mbuf may hasn't come from userland, but we pretend
  343          * that it has.
  344          */
  345         m->m_pkthdr.rcvif = NULL;
  346         m->m_nextpkt = NULL;
  347         M_SETFIB(m, so->so_fibnum);
  348 
  349         if (control)
  350                 m_freem(control);               /* XXX */
  351 
  352         mtag = m_tag_locate(m, MTAG_IPFW_RULE, 0, NULL);
  353         if (mtag == NULL) {
  354                 /* this should be normal */
  355                 mtag = m_tag_alloc(MTAG_IPFW_RULE, 0,
  356                     sizeof(struct ipfw_rule_ref), M_NOWAIT | M_ZERO);
  357                 if (mtag == NULL) {
  358                         error = ENOBUFS;
  359                         goto cantsend;
  360                 }
  361                 m_tag_prepend(m, mtag);
  362         }
  363         dt = (struct ipfw_rule_ref *)(mtag+1);
  364 
  365         /* Loopback avoidance and state recovery */
  366         if (sin) {
  367                 int i;
  368 
  369                 /* set the starting point. We provide a non-zero slot,
  370                  * but a non_matching chain_id to skip that info and use
  371                  * the rulenum/rule_id.
  372                  */
  373                 dt->slot = 1; /* dummy, chain_id is invalid */
  374                 dt->chain_id = 0;
  375                 dt->rulenum = sin->sin_port+1; /* host format ? */
  376                 dt->rule_id = 0;
  377                 /*
  378                  * Find receive interface with the given name, stuffed
  379                  * (if it exists) in the sin_zero[] field.
  380                  * The name is user supplied data so don't trust its size
  381                  * or that it is zero terminated.
  382                  */
  383                 for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++)
  384                         ;
  385                 if ( i > 0 && i < sizeof(sin->sin_zero))
  386                         m->m_pkthdr.rcvif = ifunit(sin->sin_zero);
  387         }
  388 
  389         /* Reinject packet into the system as incoming or outgoing */
  390         if (!sin || sin->sin_addr.s_addr == 0) {
  391                 struct ip *const ip = mtod(m, struct ip *);
  392                 struct inpcb *inp;
  393 
  394                 dt->info |= IPFW_IS_DIVERT | IPFW_INFO_OUT;
  395                 inp = sotoinpcb(so);
  396                 INP_RLOCK(inp);
  397                 /*
  398                  * Don't allow both user specified and setsockopt options,
  399                  * and don't allow packet length sizes that will crash
  400                  */
  401                 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) ||
  402                      ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) {
  403                         error = EINVAL;
  404                         INP_RUNLOCK(inp);
  405                         m_freem(m);
  406                 } else {
  407                         /* Convert fields to host order for ip_output() */
  408                         ip->ip_len = ntohs(ip->ip_len);
  409                         ip->ip_off = ntohs(ip->ip_off);
  410 
  411                         /* Send packet to output processing */
  412                         KMOD_IPSTAT_INC(ips_rawout);            /* XXX */
  413 
  414 #ifdef MAC
  415                         mac_inpcb_create_mbuf(inp, m);
  416 #endif
  417                         /*
  418                          * Get ready to inject the packet into ip_output().
  419                          * Just in case socket options were specified on the
  420                          * divert socket, we duplicate them.  This is done
  421                          * to avoid having to hold the PCB locks over the call
  422                          * to ip_output(), as doing this results in a number of
  423                          * lock ordering complexities.
  424                          *
  425                          * Note that we set the multicast options argument for
  426                          * ip_output() to NULL since it should be invariant that
  427                          * they are not present.
  428                          */
  429                         KASSERT(inp->inp_moptions == NULL,
  430                             ("multicast options set on a divert socket"));
  431                         options = NULL;
  432                         /*
  433                          * XXXCSJP: It is unclear to me whether or not it makes
  434                          * sense for divert sockets to have options.  However,
  435                          * for now we will duplicate them with the INP locks
  436                          * held so we can use them in ip_output() without
  437                          * requring a reference to the pcb.
  438                          */
  439                         if (inp->inp_options != NULL) {
  440                                 options = m_dup(inp->inp_options, M_DONTWAIT);
  441                                 if (options == NULL)
  442                                         error = ENOBUFS;
  443                         }
  444                         INP_RUNLOCK(inp);
  445                         if (error == ENOBUFS) {
  446                                 m_freem(m);
  447                                 return (error);
  448                         }
  449                         error = ip_output(m, options, NULL,
  450                             ((so->so_options & SO_DONTROUTE) ?
  451                             IP_ROUTETOIF : 0) | IP_ALLOWBROADCAST |
  452                             IP_RAWOUTPUT, NULL, NULL);
  453                         if (options != NULL)
  454                                 m_freem(options);
  455                 }
  456         } else {
  457                 dt->info |= IPFW_IS_DIVERT | IPFW_INFO_IN;
  458                 if (m->m_pkthdr.rcvif == NULL) {
  459                         /*
  460                          * No luck with the name, check by IP address.
  461                          * Clear the port and the ifname to make sure
  462                          * there are no distractions for ifa_ifwithaddr.
  463                          */
  464                         struct  ifaddr *ifa;
  465 
  466                         bzero(sin->sin_zero, sizeof(sin->sin_zero));
  467                         sin->sin_port = 0;
  468                         ifa = ifa_ifwithaddr((struct sockaddr *) sin);
  469                         if (ifa == NULL) {
  470                                 error = EADDRNOTAVAIL;
  471                                 goto cantsend;
  472                         }
  473                         m->m_pkthdr.rcvif = ifa->ifa_ifp;
  474                         ifa_free(ifa);
  475                 }
  476 #ifdef MAC
  477                 mac_socket_create_mbuf(so, m);
  478 #endif
  479                 /* Send packet to input processing via netisr */
  480                 netisr_queue_src(NETISR_IP, (uintptr_t)so, m);
  481         }
  482 
  483         return error;
  484 
  485 cantsend:
  486         m_freem(m);
  487         return error;
  488 }
  489 
  490 static int
  491 div_attach(struct socket *so, int proto, struct thread *td)
  492 {
  493         struct inpcb *inp;
  494         int error;
  495 
  496         inp  = sotoinpcb(so);
  497         KASSERT(inp == NULL, ("div_attach: inp != NULL"));
  498         if (td != NULL) {
  499                 error = priv_check(td, PRIV_NETINET_DIVERT);
  500                 if (error)
  501                         return (error);
  502         }
  503         error = soreserve(so, div_sendspace, div_recvspace);
  504         if (error)
  505                 return error;
  506         INP_INFO_WLOCK(&V_divcbinfo);
  507         error = in_pcballoc(so, &V_divcbinfo);
  508         if (error) {
  509                 INP_INFO_WUNLOCK(&V_divcbinfo);
  510                 return error;
  511         }
  512         inp = (struct inpcb *)so->so_pcb;
  513         INP_INFO_WUNLOCK(&V_divcbinfo);
  514         inp->inp_ip_p = proto;
  515         inp->inp_vflag |= INP_IPV4;
  516         inp->inp_flags |= INP_HDRINCL;
  517         INP_WUNLOCK(inp);
  518         return 0;
  519 }
  520 
  521 static void
  522 div_detach(struct socket *so)
  523 {
  524         struct inpcb *inp;
  525 
  526         inp = sotoinpcb(so);
  527         KASSERT(inp != NULL, ("div_detach: inp == NULL"));
  528         INP_INFO_WLOCK(&V_divcbinfo);
  529         INP_WLOCK(inp);
  530         in_pcbdetach(inp);
  531         in_pcbfree(inp);
  532         INP_INFO_WUNLOCK(&V_divcbinfo);
  533 }
  534 
  535 static int
  536 div_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  537 {
  538         struct inpcb *inp;
  539         int error;
  540 
  541         inp = sotoinpcb(so);
  542         KASSERT(inp != NULL, ("div_bind: inp == NULL"));
  543         /* in_pcbbind assumes that nam is a sockaddr_in
  544          * and in_pcbbind requires a valid address. Since divert
  545          * sockets don't we need to make sure the address is
  546          * filled in properly.
  547          * XXX -- divert should not be abusing in_pcbind
  548          * and should probably have its own family.
  549          */
  550         if (nam->sa_family != AF_INET)
  551                 return EAFNOSUPPORT;
  552         ((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY;
  553         INP_INFO_WLOCK(&V_divcbinfo);
  554         INP_WLOCK(inp);
  555         error = in_pcbbind(inp, nam, td->td_ucred);
  556         INP_WUNLOCK(inp);
  557         INP_INFO_WUNLOCK(&V_divcbinfo);
  558         return error;
  559 }
  560 
  561 static int
  562 div_shutdown(struct socket *so)
  563 {
  564         struct inpcb *inp;
  565 
  566         inp = sotoinpcb(so);
  567         KASSERT(inp != NULL, ("div_shutdown: inp == NULL"));
  568         INP_WLOCK(inp);
  569         socantsendmore(so);
  570         INP_WUNLOCK(inp);
  571         return 0;
  572 }
  573 
  574 static int
  575 div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
  576     struct mbuf *control, struct thread *td)
  577 {
  578 
  579         /* Packet must have a header (but that's about it) */
  580         if (m->m_len < sizeof (struct ip) &&
  581             (m = m_pullup(m, sizeof (struct ip))) == 0) {
  582                 KMOD_IPSTAT_INC(ips_toosmall);
  583                 m_freem(m);
  584                 return EINVAL;
  585         }
  586 
  587         /* Send packet */
  588         return div_output(so, m, (struct sockaddr_in *)nam, control);
  589 }
  590 
  591 static void
  592 div_ctlinput(int cmd, struct sockaddr *sa, void *vip)
  593 {
  594         struct in_addr faddr;
  595 
  596         faddr = ((struct sockaddr_in *)sa)->sin_addr;
  597         if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
  598                 return;
  599         if (PRC_IS_REDIRECT(cmd))
  600                 return;
  601 }
  602 
  603 static int
  604 div_pcblist(SYSCTL_HANDLER_ARGS)
  605 {
  606         int error, i, n;
  607         struct inpcb *inp, **inp_list;
  608         inp_gen_t gencnt;
  609         struct xinpgen xig;
  610 
  611         /*
  612          * The process of preparing the TCB list is too time-consuming and
  613          * resource-intensive to repeat twice on every request.
  614          */
  615         if (req->oldptr == 0) {
  616                 n = V_divcbinfo.ipi_count;
  617                 req->oldidx = 2 * (sizeof xig)
  618                         + (n + n/8) * sizeof(struct xinpcb);
  619                 return 0;
  620         }
  621 
  622         if (req->newptr != 0)
  623                 return EPERM;
  624 
  625         /*
  626          * OK, now we're committed to doing something.
  627          */
  628         INP_INFO_RLOCK(&V_divcbinfo);
  629         gencnt = V_divcbinfo.ipi_gencnt;
  630         n = V_divcbinfo.ipi_count;
  631         INP_INFO_RUNLOCK(&V_divcbinfo);
  632 
  633         error = sysctl_wire_old_buffer(req,
  634             2 * sizeof(xig) + n*sizeof(struct xinpcb));
  635         if (error != 0)
  636                 return (error);
  637 
  638         xig.xig_len = sizeof xig;
  639         xig.xig_count = n;
  640         xig.xig_gen = gencnt;
  641         xig.xig_sogen = so_gencnt;
  642         error = SYSCTL_OUT(req, &xig, sizeof xig);
  643         if (error)
  644                 return error;
  645 
  646         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
  647         if (inp_list == 0)
  648                 return ENOMEM;
  649         
  650         INP_INFO_RLOCK(&V_divcbinfo);
  651         for (inp = LIST_FIRST(V_divcbinfo.ipi_listhead), i = 0; inp && i < n;
  652              inp = LIST_NEXT(inp, inp_list)) {
  653                 INP_WLOCK(inp);
  654                 if (inp->inp_gencnt <= gencnt &&
  655                     cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
  656                         in_pcbref(inp);
  657                         inp_list[i++] = inp;
  658                 }
  659                 INP_WUNLOCK(inp);
  660         }
  661         INP_INFO_RUNLOCK(&V_divcbinfo);
  662         n = i;
  663 
  664         error = 0;
  665         for (i = 0; i < n; i++) {
  666                 inp = inp_list[i];
  667                 INP_RLOCK(inp);
  668                 if (inp->inp_gencnt <= gencnt) {
  669                         struct xinpcb xi;
  670                         bzero(&xi, sizeof(xi));
  671                         xi.xi_len = sizeof xi;
  672                         /* XXX should avoid extra copy */
  673                         bcopy(inp, &xi.xi_inp, sizeof *inp);
  674                         if (inp->inp_socket)
  675                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
  676                         INP_RUNLOCK(inp);
  677                         error = SYSCTL_OUT(req, &xi, sizeof xi);
  678                 } else
  679                         INP_RUNLOCK(inp);
  680         }
  681         INP_INFO_WLOCK(&V_divcbinfo);
  682         for (i = 0; i < n; i++) {
  683                 inp = inp_list[i];
  684                 INP_WLOCK(inp);
  685                 if (!in_pcbrele(inp))
  686                         INP_WUNLOCK(inp);
  687         }
  688         INP_INFO_WUNLOCK(&V_divcbinfo);
  689 
  690         if (!error) {
  691                 /*
  692                  * Give the user an updated idea of our state.
  693                  * If the generation differs from what we told
  694                  * her before, she knows that something happened
  695                  * while we were processing this request, and it
  696                  * might be necessary to retry.
  697                  */
  698                 INP_INFO_RLOCK(&V_divcbinfo);
  699                 xig.xig_gen = V_divcbinfo.ipi_gencnt;
  700                 xig.xig_sogen = so_gencnt;
  701                 xig.xig_count = V_divcbinfo.ipi_count;
  702                 INP_INFO_RUNLOCK(&V_divcbinfo);
  703                 error = SYSCTL_OUT(req, &xig, sizeof xig);
  704         }
  705         free(inp_list, M_TEMP);
  706         return error;
  707 }
  708 
  709 #ifdef SYSCTL_NODE
  710 SYSCTL_NODE(_net_inet, IPPROTO_DIVERT, divert, CTLFLAG_RW, 0, "IPDIVERT");
  711 SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, CTLFLAG_RD, 0, 0,
  712             div_pcblist, "S,xinpcb", "List of active divert sockets");
  713 #endif
  714 
  715 struct pr_usrreqs div_usrreqs = {
  716         .pru_attach =           div_attach,
  717         .pru_bind =             div_bind,
  718         .pru_control =          in_control,
  719         .pru_detach =           div_detach,
  720         .pru_peeraddr =         in_getpeeraddr,
  721         .pru_send =             div_send,
  722         .pru_shutdown =         div_shutdown,
  723         .pru_sockaddr =         in_getsockaddr,
  724         .pru_sosetlabel =       in_pcbsosetlabel
  725 };
  726 
  727 struct protosw div_protosw = {
  728         .pr_type =              SOCK_RAW,
  729         .pr_protocol =          IPPROTO_DIVERT,
  730         .pr_flags =             PR_ATOMIC|PR_ADDR,
  731         .pr_input =             div_input,
  732         .pr_ctlinput =          div_ctlinput,
  733         .pr_ctloutput =         ip_ctloutput,
  734         .pr_init =              div_init,
  735 #ifdef VIMAGE
  736         .pr_destroy =           div_destroy,
  737 #endif
  738         .pr_usrreqs =           &div_usrreqs
  739 };
  740 
  741 static int
  742 div_modevent(module_t mod, int type, void *unused)
  743 {
  744         int err = 0;
  745 #ifndef VIMAGE
  746         int n;
  747 #endif
  748 
  749         switch (type) {
  750         case MOD_LOAD:
  751                 /*
  752                  * Protocol will be initialized by pf_proto_register().
  753                  * We don't have to register ip_protox because we are not
  754                  * a true IP protocol that goes over the wire.
  755                  */
  756                 err = pf_proto_register(PF_INET, &div_protosw);
  757                 if (err != 0)
  758                         return (err);
  759                 ip_divert_ptr = divert_packet;
  760                 ip_divert_event_tag = EVENTHANDLER_REGISTER(maxsockets_change,
  761                     div_zone_change, NULL, EVENTHANDLER_PRI_ANY);
  762                 break;
  763         case MOD_QUIESCE:
  764                 /*
  765                  * IPDIVERT may normally not be unloaded because of the
  766                  * potential race conditions.  Tell kldunload we can't be
  767                  * unloaded unless the unload is forced.
  768                  */
  769                 err = EPERM;
  770                 break;
  771         case MOD_UNLOAD:
  772 #ifdef VIMAGE
  773                 err = EPERM;
  774                 break;
  775 #else
  776                 /*
  777                  * Forced unload.
  778                  *
  779                  * Module ipdivert can only be unloaded if no sockets are
  780                  * connected.  Maybe this can be changed later to forcefully
  781                  * disconnect any open sockets.
  782                  *
  783                  * XXXRW: Note that there is a slight race here, as a new
  784                  * socket open request could be spinning on the lock and then
  785                  * we destroy the lock.
  786                  */
  787                 INP_INFO_WLOCK(&V_divcbinfo);
  788                 n = V_divcbinfo.ipi_count;
  789                 if (n != 0) {
  790                         err = EBUSY;
  791                         INP_INFO_WUNLOCK(&V_divcbinfo);
  792                         break;
  793                 }
  794                 ip_divert_ptr = NULL;
  795                 err = pf_proto_unregister(PF_INET, IPPROTO_DIVERT, SOCK_RAW);
  796                 INP_INFO_WUNLOCK(&V_divcbinfo);
  797                 div_destroy();
  798                 EVENTHANDLER_DEREGISTER(maxsockets_change, ip_divert_event_tag);
  799                 break;
  800 #endif /* !VIMAGE */
  801         default:
  802                 err = EOPNOTSUPP;
  803                 break;
  804         }
  805         return err;
  806 }
  807 
  808 static moduledata_t ipdivertmod = {
  809         "ipdivert",
  810         div_modevent,
  811         0
  812 };
  813 
  814 DECLARE_MODULE(ipdivert, ipdivertmod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
  815 MODULE_DEPEND(ipdivert, ipfw, 2, 2, 2);
  816 MODULE_VERSION(ipdivert, 1);

Cache object: 09ab3f235aad6f673fc05cf84ac8621a


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