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

Cache object: 28b1d04c183bc635c5763c42d889efdd


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