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

Cache object: 6af130759f1976705edb456f17a50bd6


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