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/raw_ip.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.
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 4. Neither the name of the University nor the names of its contributors
   15  *    may be used to endorse or promote products derived from this software
   16  *    without specific prior written permission.
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   28  * SUCH DAMAGE.
   29  *
   30  *      @(#)raw_ip.c    8.7 (Berkeley) 5/15/95
   31  */
   32 
   33 #include <sys/cdefs.h>
   34 __FBSDID("$FreeBSD: releng/8.2/sys/netinet/raw_ip.c 211889 2010-08-27 18:50:12Z jhb $");
   35 
   36 #include "opt_inet6.h"
   37 #include "opt_ipsec.h"
   38 
   39 #include <sys/param.h>
   40 #include <sys/jail.h>
   41 #include <sys/kernel.h>
   42 #include <sys/lock.h>
   43 #include <sys/malloc.h>
   44 #include <sys/mbuf.h>
   45 #include <sys/priv.h>
   46 #include <sys/proc.h>
   47 #include <sys/protosw.h>
   48 #include <sys/rwlock.h>
   49 #include <sys/signalvar.h>
   50 #include <sys/socket.h>
   51 #include <sys/socketvar.h>
   52 #include <sys/sx.h>
   53 #include <sys/sysctl.h>
   54 #include <sys/systm.h>
   55 
   56 #include <vm/uma.h>
   57 
   58 #include <net/if.h>
   59 #include <net/route.h>
   60 #include <net/vnet.h>
   61 
   62 #include <netinet/in.h>
   63 #include <netinet/in_systm.h>
   64 #include <netinet/in_pcb.h>
   65 #include <netinet/in_var.h>
   66 #include <netinet/ip.h>
   67 #include <netinet/ip_var.h>
   68 #include <netinet/ip_mroute.h>
   69 
   70 #ifdef IPSEC
   71 #include <netipsec/ipsec.h>
   72 #endif /*IPSEC*/
   73 
   74 #include <security/mac/mac_framework.h>
   75 
   76 VNET_DEFINE(struct inpcbhead, ripcb);
   77 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
   78 
   79 #define V_ripcb                 VNET(ripcb)
   80 #define V_ripcbinfo             VNET(ripcbinfo)
   81 
   82 /*
   83  * Control and data hooks for ipfw, dummynet, divert and so on.
   84  * The data hooks are not used here but it is convenient
   85  * to keep them all in one place.
   86  */
   87 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
   88 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
   89 
   90 int     (*ip_dn_ctl_ptr)(struct sockopt *);
   91 int     (*ip_dn_io_ptr)(struct mbuf **, int, struct ip_fw_args *);
   92 void    (*ip_divert_ptr)(struct mbuf *, int);
   93 int     (*ng_ipfw_input_p)(struct mbuf **, int,
   94                         struct ip_fw_args *, int);
   95 
   96 /*
   97  * Hooks for multicast routing. They all default to NULL, so leave them not
   98  * initialized and rely on BSS being set to 0.
   99  */
  100 
  101 /*
  102  * The socket used to communicate with the multicast routing daemon.
  103  */
  104 VNET_DEFINE(struct socket *, ip_mrouter);
  105 
  106 /*
  107  * The various mrouter and rsvp functions.
  108  */
  109 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
  110 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
  111 int (*ip_mrouter_done)(void);
  112 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
  113                    struct ip_moptions *);
  114 int (*mrt_ioctl)(u_long, caddr_t, int);
  115 int (*legal_vif_num)(int);
  116 u_long (*ip_mcast_src)(int);
  117 
  118 void (*rsvp_input_p)(struct mbuf *m, int off);
  119 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
  120 void (*ip_rsvp_force_done)(struct socket *);
  121 
  122 /*
  123  * Hash functions
  124  */
  125 
  126 #define INP_PCBHASH_RAW_SIZE    256
  127 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
  128         (((proto) + (laddr) + (faddr)) % (mask) + 1)
  129 
  130 static void
  131 rip_inshash(struct inpcb *inp)
  132 {
  133         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
  134         struct inpcbhead *pcbhash;
  135         int hash;
  136 
  137         INP_INFO_WLOCK_ASSERT(pcbinfo);
  138         INP_WLOCK_ASSERT(inp);
  139         
  140         if (inp->inp_ip_p != 0 &&
  141             inp->inp_laddr.s_addr != INADDR_ANY &&
  142             inp->inp_faddr.s_addr != INADDR_ANY) {
  143                 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
  144                     inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
  145         } else
  146                 hash = 0;
  147         pcbhash = &pcbinfo->ipi_hashbase[hash];
  148         LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
  149 }
  150 
  151 static void
  152 rip_delhash(struct inpcb *inp)
  153 {
  154 
  155         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  156         INP_WLOCK_ASSERT(inp);
  157 
  158         LIST_REMOVE(inp, inp_hash);
  159 }
  160 
  161 /*
  162  * Raw interface to IP protocol.
  163  */
  164 
  165 /*
  166  * Initialize raw connection block q.
  167  */
  168 static void
  169 rip_zone_change(void *tag)
  170 {
  171 
  172         uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
  173 }
  174 
  175 static int
  176 rip_inpcb_init(void *mem, int size, int flags)
  177 {
  178         struct inpcb *inp = mem;
  179 
  180         INP_LOCK_INIT(inp, "inp", "rawinp");
  181         return (0);
  182 }
  183 
  184 void
  185 rip_init(void)
  186 {
  187 
  188         INP_INFO_LOCK_INIT(&V_ripcbinfo, "rip");
  189         LIST_INIT(&V_ripcb);
  190 #ifdef VIMAGE
  191         V_ripcbinfo.ipi_vnet = curvnet;
  192 #endif
  193         V_ripcbinfo.ipi_listhead = &V_ripcb;
  194         V_ripcbinfo.ipi_hashbase =
  195             hashinit(INP_PCBHASH_RAW_SIZE, M_PCB, &V_ripcbinfo.ipi_hashmask);
  196         V_ripcbinfo.ipi_porthashbase =
  197             hashinit(1, M_PCB, &V_ripcbinfo.ipi_porthashmask);
  198         V_ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
  199             NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  200         uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
  201         EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
  202             EVENTHANDLER_PRI_ANY);
  203 }
  204 
  205 #ifdef VIMAGE
  206 void
  207 rip_destroy(void)
  208 {
  209 
  210         hashdestroy(V_ripcbinfo.ipi_hashbase, M_PCB,
  211             V_ripcbinfo.ipi_hashmask);
  212         hashdestroy(V_ripcbinfo.ipi_porthashbase, M_PCB,
  213             V_ripcbinfo.ipi_porthashmask);
  214 }
  215 #endif
  216 
  217 static int
  218 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
  219     struct sockaddr_in *ripsrc)
  220 {
  221         int policyfail = 0;
  222 
  223         INP_RLOCK_ASSERT(last);
  224 
  225 #ifdef IPSEC
  226         /* check AH/ESP integrity. */
  227         if (ipsec4_in_reject(n, last)) {
  228                 policyfail = 1;
  229         }
  230 #endif /* IPSEC */
  231 #ifdef MAC
  232         if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
  233                 policyfail = 1;
  234 #endif
  235         /* Check the minimum TTL for socket. */
  236         if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
  237                 policyfail = 1;
  238         if (!policyfail) {
  239                 struct mbuf *opts = NULL;
  240                 struct socket *so;
  241 
  242                 so = last->inp_socket;
  243                 if ((last->inp_flags & INP_CONTROLOPTS) ||
  244                     (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
  245                         ip_savecontrol(last, &opts, ip, n);
  246                 SOCKBUF_LOCK(&so->so_rcv);
  247                 if (sbappendaddr_locked(&so->so_rcv,
  248                     (struct sockaddr *)ripsrc, n, opts) == 0) {
  249                         /* should notify about lost packet */
  250                         m_freem(n);
  251                         if (opts)
  252                                 m_freem(opts);
  253                         SOCKBUF_UNLOCK(&so->so_rcv);
  254                 } else
  255                         sorwakeup_locked(so);
  256         } else
  257                 m_freem(n);
  258         return (policyfail);
  259 }
  260 
  261 /*
  262  * Setup generic address and protocol structures for raw_input routine, then
  263  * pass them along with mbuf chain.
  264  */
  265 void
  266 rip_input(struct mbuf *m, int off)
  267 {
  268         struct ifnet *ifp;
  269         struct ip *ip = mtod(m, struct ip *);
  270         int proto = ip->ip_p;
  271         struct inpcb *inp, *last;
  272         struct sockaddr_in ripsrc;
  273         int hash;
  274 
  275         bzero(&ripsrc, sizeof(ripsrc));
  276         ripsrc.sin_len = sizeof(ripsrc);
  277         ripsrc.sin_family = AF_INET;
  278         ripsrc.sin_addr = ip->ip_src;
  279         last = NULL;
  280 
  281         ifp = m->m_pkthdr.rcvif;
  282 
  283         hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
  284             ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
  285         INP_INFO_RLOCK(&V_ripcbinfo);
  286         LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
  287                 if (inp->inp_ip_p != proto)
  288                         continue;
  289 #ifdef INET6
  290                 /* XXX inp locking */
  291                 if ((inp->inp_vflag & INP_IPV4) == 0)
  292                         continue;
  293 #endif
  294                 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
  295                         continue;
  296                 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
  297                         continue;
  298                 if (jailed_without_vnet(inp->inp_cred)) {
  299                         /*
  300                          * XXX: If faddr was bound to multicast group,
  301                          * jailed raw socket will drop datagram.
  302                          */
  303                         if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
  304                                 continue;
  305                 }
  306                 if (last != NULL) {
  307                         struct mbuf *n;
  308 
  309                         n = m_copy(m, 0, (int)M_COPYALL);
  310                         if (n != NULL)
  311                             (void) rip_append(last, ip, n, &ripsrc);
  312                         /* XXX count dropped packet */
  313                         INP_RUNLOCK(last);
  314                 }
  315                 INP_RLOCK(inp);
  316                 last = inp;
  317         }
  318         LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
  319                 if (inp->inp_ip_p && inp->inp_ip_p != proto)
  320                         continue;
  321 #ifdef INET6
  322                 /* XXX inp locking */
  323                 if ((inp->inp_vflag & INP_IPV4) == 0)
  324                         continue;
  325 #endif
  326                 if (!in_nullhost(inp->inp_laddr) &&
  327                     !in_hosteq(inp->inp_laddr, ip->ip_dst))
  328                         continue;
  329                 if (!in_nullhost(inp->inp_faddr) &&
  330                     !in_hosteq(inp->inp_faddr, ip->ip_src))
  331                         continue;
  332                 if (jailed_without_vnet(inp->inp_cred)) {
  333                         /*
  334                          * Allow raw socket in jail to receive multicast;
  335                          * assume process had PRIV_NETINET_RAW at attach,
  336                          * and fall through into normal filter path if so.
  337                          */
  338                         if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
  339                             prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
  340                                 continue;
  341                 }
  342                 /*
  343                  * If this raw socket has multicast state, and we
  344                  * have received a multicast, check if this socket
  345                  * should receive it, as multicast filtering is now
  346                  * the responsibility of the transport layer.
  347                  */
  348                 if (inp->inp_moptions != NULL &&
  349                     IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
  350                         /*
  351                          * If the incoming datagram is for IGMP, allow it
  352                          * through unconditionally to the raw socket.
  353                          *
  354                          * In the case of IGMPv2, we may not have explicitly
  355                          * joined the group, and may have set IFF_ALLMULTI
  356                          * on the interface. imo_multi_filter() may discard
  357                          * control traffic we actually need to see.
  358                          *
  359                          * Userland multicast routing daemons should continue
  360                          * filter the control traffic appropriately.
  361                          */
  362                         int blocked;
  363 
  364                         blocked = MCAST_PASS;
  365                         if (proto != IPPROTO_IGMP) {
  366                                 struct sockaddr_in group;
  367 
  368                                 bzero(&group, sizeof(struct sockaddr_in));
  369                                 group.sin_len = sizeof(struct sockaddr_in);
  370                                 group.sin_family = AF_INET;
  371                                 group.sin_addr = ip->ip_dst;
  372 
  373                                 blocked = imo_multi_filter(inp->inp_moptions,
  374                                     ifp,
  375                                     (struct sockaddr *)&group,
  376                                     (struct sockaddr *)&ripsrc);
  377                         }
  378 
  379                         if (blocked != MCAST_PASS) {
  380                                 IPSTAT_INC(ips_notmember);
  381                                 continue;
  382                         }
  383                 }
  384                 if (last != NULL) {
  385                         struct mbuf *n;
  386 
  387                         n = m_copy(m, 0, (int)M_COPYALL);
  388                         if (n != NULL)
  389                                 (void) rip_append(last, ip, n, &ripsrc);
  390                         /* XXX count dropped packet */
  391                         INP_RUNLOCK(last);
  392                 }
  393                 INP_RLOCK(inp);
  394                 last = inp;
  395         }
  396         INP_INFO_RUNLOCK(&V_ripcbinfo);
  397         if (last != NULL) {
  398                 if (rip_append(last, ip, m, &ripsrc) != 0)
  399                         IPSTAT_INC(ips_delivered);
  400                 INP_RUNLOCK(last);
  401         } else {
  402                 m_freem(m);
  403                 IPSTAT_INC(ips_noproto);
  404                 IPSTAT_DEC(ips_delivered);
  405         }
  406 }
  407 
  408 /*
  409  * Generate IP header and pass packet to ip_output.  Tack on options user may
  410  * have setup with control call.
  411  */
  412 int
  413 rip_output(struct mbuf *m, struct socket *so, u_long dst)
  414 {
  415         struct ip *ip;
  416         int error;
  417         struct inpcb *inp = sotoinpcb(so);
  418         int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
  419             IP_ALLOWBROADCAST;
  420 
  421         /*
  422          * If the user handed us a complete IP packet, use it.  Otherwise,
  423          * allocate an mbuf for a header and fill it in.
  424          */
  425         if ((inp->inp_flags & INP_HDRINCL) == 0) {
  426                 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
  427                         m_freem(m);
  428                         return(EMSGSIZE);
  429                 }
  430                 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
  431                 if (m == NULL)
  432                         return(ENOBUFS);
  433 
  434                 INP_RLOCK(inp);
  435                 ip = mtod(m, struct ip *);
  436                 ip->ip_tos = inp->inp_ip_tos;
  437                 if (inp->inp_flags & INP_DONTFRAG)
  438                         ip->ip_off = IP_DF;
  439                 else
  440                         ip->ip_off = 0;
  441                 ip->ip_p = inp->inp_ip_p;
  442                 ip->ip_len = m->m_pkthdr.len;
  443                 ip->ip_src = inp->inp_laddr;
  444                 if (jailed(inp->inp_cred)) {
  445                         /*
  446                          * prison_local_ip4() would be good enough but would
  447                          * let a source of INADDR_ANY pass, which we do not
  448                          * want to see from jails. We do not go through the
  449                          * pain of in_pcbladdr() for raw sockets.
  450                          */
  451                         if (ip->ip_src.s_addr == INADDR_ANY)
  452                                 error = prison_get_ip4(inp->inp_cred,
  453                                     &ip->ip_src);
  454                         else
  455                                 error = prison_local_ip4(inp->inp_cred,
  456                                     &ip->ip_src);
  457                         if (error != 0) {
  458                                 INP_RUNLOCK(inp);
  459                                 m_freem(m);
  460                                 return (error);
  461                         }
  462                 }
  463                 ip->ip_dst.s_addr = dst;
  464                 ip->ip_ttl = inp->inp_ip_ttl;
  465         } else {
  466                 if (m->m_pkthdr.len > IP_MAXPACKET) {
  467                         m_freem(m);
  468                         return(EMSGSIZE);
  469                 }
  470                 INP_RLOCK(inp);
  471                 ip = mtod(m, struct ip *);
  472                 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
  473                 if (error != 0) {
  474                         INP_RUNLOCK(inp);
  475                         m_freem(m);
  476                         return (error);
  477                 }
  478 
  479                 /*
  480                  * Don't allow both user specified and setsockopt options,
  481                  * and don't allow packet length sizes that will crash.
  482                  */
  483                 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
  484                     || (ip->ip_len > m->m_pkthdr.len)
  485                     || (ip->ip_len < (ip->ip_hl << 2))) {
  486                         INP_RUNLOCK(inp);
  487                         m_freem(m);
  488                         return (EINVAL);
  489                 }
  490                 if (ip->ip_id == 0)
  491                         ip->ip_id = ip_newid();
  492 
  493                 /*
  494                  * XXX prevent ip_output from overwriting header fields.
  495                  */
  496                 flags |= IP_RAWOUTPUT;
  497                 IPSTAT_INC(ips_rawout);
  498         }
  499 
  500         if (inp->inp_flags & INP_ONESBCAST)
  501                 flags |= IP_SENDONES;
  502 
  503 #ifdef MAC
  504         mac_inpcb_create_mbuf(inp, m);
  505 #endif
  506 
  507         error = ip_output(m, inp->inp_options, NULL, flags,
  508             inp->inp_moptions, inp);
  509         INP_RUNLOCK(inp);
  510         return (error);
  511 }
  512 
  513 /*
  514  * Raw IP socket option processing.
  515  *
  516  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
  517  * only be created by a privileged process, and as such, socket option
  518  * operations to manage system properties on any raw socket were allowed to
  519  * take place without explicit additional access control checks.  However,
  520  * raw sockets can now also be created in jail(), and therefore explicit
  521  * checks are now required.  Likewise, raw sockets can be used by a process
  522  * after it gives up privilege, so some caution is required.  For options
  523  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
  524  * performed in ip_ctloutput() and therefore no check occurs here.
  525  * Unilaterally checking priv_check() here breaks normal IP socket option
  526  * operations on raw sockets.
  527  *
  528  * When adding new socket options here, make sure to add access control
  529  * checks here as necessary.
  530  */
  531 int
  532 rip_ctloutput(struct socket *so, struct sockopt *sopt)
  533 {
  534         struct  inpcb *inp = sotoinpcb(so);
  535         int     error, optval;
  536 
  537         if (sopt->sopt_level != IPPROTO_IP) {
  538                 if ((sopt->sopt_level == SOL_SOCKET) &&
  539                     (sopt->sopt_name == SO_SETFIB)) {
  540                         inp->inp_inc.inc_fibnum = so->so_fibnum;
  541                         return (0);
  542                 }
  543                 return (EINVAL);
  544         }
  545 
  546         error = 0;
  547         switch (sopt->sopt_dir) {
  548         case SOPT_GET:
  549                 switch (sopt->sopt_name) {
  550                 case IP_HDRINCL:
  551                         optval = inp->inp_flags & INP_HDRINCL;
  552                         error = sooptcopyout(sopt, &optval, sizeof optval);
  553                         break;
  554 
  555                 case IP_FW3:    /* generic ipfw v.3 functions */
  556                 case IP_FW_ADD: /* ADD actually returns the body... */
  557                 case IP_FW_GET:
  558                 case IP_FW_TABLE_GETSIZE:
  559                 case IP_FW_TABLE_LIST:
  560                 case IP_FW_NAT_GET_CONFIG:
  561                 case IP_FW_NAT_GET_LOG:
  562                         if (V_ip_fw_ctl_ptr != NULL)
  563                                 error = V_ip_fw_ctl_ptr(sopt);
  564                         else
  565                                 error = ENOPROTOOPT;
  566                         break;
  567 
  568                 case IP_DUMMYNET3:      /* generic dummynet v.3 functions */
  569                 case IP_DUMMYNET_GET:
  570                         if (ip_dn_ctl_ptr != NULL)
  571                                 error = ip_dn_ctl_ptr(sopt);
  572                         else
  573                                 error = ENOPROTOOPT;
  574                         break ;
  575 
  576                 case MRT_INIT:
  577                 case MRT_DONE:
  578                 case MRT_ADD_VIF:
  579                 case MRT_DEL_VIF:
  580                 case MRT_ADD_MFC:
  581                 case MRT_DEL_MFC:
  582                 case MRT_VERSION:
  583                 case MRT_ASSERT:
  584                 case MRT_API_SUPPORT:
  585                 case MRT_API_CONFIG:
  586                 case MRT_ADD_BW_UPCALL:
  587                 case MRT_DEL_BW_UPCALL:
  588                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
  589                         if (error != 0)
  590                                 return (error);
  591                         error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
  592                                 EOPNOTSUPP;
  593                         break;
  594 
  595                 default:
  596                         error = ip_ctloutput(so, sopt);
  597                         break;
  598                 }
  599                 break;
  600 
  601         case SOPT_SET:
  602                 switch (sopt->sopt_name) {
  603                 case IP_HDRINCL:
  604                         error = sooptcopyin(sopt, &optval, sizeof optval,
  605                                             sizeof optval);
  606                         if (error)
  607                                 break;
  608                         if (optval)
  609                                 inp->inp_flags |= INP_HDRINCL;
  610                         else
  611                                 inp->inp_flags &= ~INP_HDRINCL;
  612                         break;
  613 
  614                 case IP_FW3:    /* generic ipfw v.3 functions */
  615                 case IP_FW_ADD:
  616                 case IP_FW_DEL:
  617                 case IP_FW_FLUSH:
  618                 case IP_FW_ZERO:
  619                 case IP_FW_RESETLOG:
  620                 case IP_FW_TABLE_ADD:
  621                 case IP_FW_TABLE_DEL:
  622                 case IP_FW_TABLE_FLUSH:
  623                 case IP_FW_NAT_CFG:
  624                 case IP_FW_NAT_DEL:
  625                         if (V_ip_fw_ctl_ptr != NULL)
  626                                 error = V_ip_fw_ctl_ptr(sopt);
  627                         else
  628                                 error = ENOPROTOOPT;
  629                         break;
  630 
  631                 case IP_DUMMYNET3:      /* generic dummynet v.3 functions */
  632                 case IP_DUMMYNET_CONFIGURE:
  633                 case IP_DUMMYNET_DEL:
  634                 case IP_DUMMYNET_FLUSH:
  635                         if (ip_dn_ctl_ptr != NULL)
  636                                 error = ip_dn_ctl_ptr(sopt);
  637                         else
  638                                 error = ENOPROTOOPT ;
  639                         break ;
  640 
  641                 case IP_RSVP_ON:
  642                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
  643                         if (error != 0)
  644                                 return (error);
  645                         error = ip_rsvp_init(so);
  646                         break;
  647 
  648                 case IP_RSVP_OFF:
  649                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
  650                         if (error != 0)
  651                                 return (error);
  652                         error = ip_rsvp_done();
  653                         break;
  654 
  655                 case IP_RSVP_VIF_ON:
  656                 case IP_RSVP_VIF_OFF:
  657                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
  658                         if (error != 0)
  659                                 return (error);
  660                         error = ip_rsvp_vif ?
  661                                 ip_rsvp_vif(so, sopt) : EINVAL;
  662                         break;
  663 
  664                 case MRT_INIT:
  665                 case MRT_DONE:
  666                 case MRT_ADD_VIF:
  667                 case MRT_DEL_VIF:
  668                 case MRT_ADD_MFC:
  669                 case MRT_DEL_MFC:
  670                 case MRT_VERSION:
  671                 case MRT_ASSERT:
  672                 case MRT_API_SUPPORT:
  673                 case MRT_API_CONFIG:
  674                 case MRT_ADD_BW_UPCALL:
  675                 case MRT_DEL_BW_UPCALL:
  676                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
  677                         if (error != 0)
  678                                 return (error);
  679                         error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
  680                                         EOPNOTSUPP;
  681                         break;
  682 
  683                 default:
  684                         error = ip_ctloutput(so, sopt);
  685                         break;
  686                 }
  687                 break;
  688         }
  689 
  690         return (error);
  691 }
  692 
  693 /*
  694  * This function exists solely to receive the PRC_IFDOWN messages which are
  695  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
  696  * in_ifadown() to remove all routes corresponding to that address.  It also
  697  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
  698  * routes.
  699  */
  700 void
  701 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
  702 {
  703         struct in_ifaddr *ia;
  704         struct ifnet *ifp;
  705         int err;
  706         int flags;
  707 
  708         switch (cmd) {
  709         case PRC_IFDOWN:
  710                 IN_IFADDR_RLOCK();
  711                 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
  712                         if (ia->ia_ifa.ifa_addr == sa
  713                             && (ia->ia_flags & IFA_ROUTE)) {
  714                                 ifa_ref(&ia->ia_ifa);
  715                                 IN_IFADDR_RUNLOCK();
  716                                 /*
  717                                  * in_ifscrub kills the interface route.
  718                                  */
  719                                 in_ifscrub(ia->ia_ifp, ia);
  720                                 /*
  721                                  * in_ifadown gets rid of all the rest of the
  722                                  * routes.  This is not quite the right thing
  723                                  * to do, but at least if we are running a
  724                                  * routing process they will come back.
  725                                  */
  726                                 in_ifadown(&ia->ia_ifa, 0);
  727                                 ifa_free(&ia->ia_ifa);
  728                                 break;
  729                         }
  730                 }
  731                 if (ia == NULL)         /* If ia matched, already unlocked. */
  732                         IN_IFADDR_RUNLOCK();
  733                 break;
  734 
  735         case PRC_IFUP:
  736                 IN_IFADDR_RLOCK();
  737                 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
  738                         if (ia->ia_ifa.ifa_addr == sa)
  739                                 break;
  740                 }
  741                 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
  742                         IN_IFADDR_RUNLOCK();
  743                         return;
  744                 }
  745                 ifa_ref(&ia->ia_ifa);
  746                 IN_IFADDR_RUNLOCK();
  747                 flags = RTF_UP;
  748                 ifp = ia->ia_ifa.ifa_ifp;
  749 
  750                 if ((ifp->if_flags & IFF_LOOPBACK)
  751                     || (ifp->if_flags & IFF_POINTOPOINT))
  752                         flags |= RTF_HOST;
  753 
  754                 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
  755                 if (err == 0)
  756                         ia->ia_flags |= IFA_ROUTE;
  757                 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
  758                 ifa_free(&ia->ia_ifa);
  759                 break;
  760         }
  761 }
  762 
  763 u_long  rip_sendspace = 9216;
  764 u_long  rip_recvspace = 9216;
  765 
  766 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
  767     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
  768 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
  769     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
  770 
  771 static int
  772 rip_attach(struct socket *so, int proto, struct thread *td)
  773 {
  774         struct inpcb *inp;
  775         int error;
  776 
  777         inp = sotoinpcb(so);
  778         KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
  779 
  780         error = priv_check(td, PRIV_NETINET_RAW);
  781         if (error)
  782                 return (error);
  783         if (proto >= IPPROTO_MAX || proto < 0)
  784                 return EPROTONOSUPPORT;
  785         error = soreserve(so, rip_sendspace, rip_recvspace);
  786         if (error)
  787                 return (error);
  788         INP_INFO_WLOCK(&V_ripcbinfo);
  789         error = in_pcballoc(so, &V_ripcbinfo);
  790         if (error) {
  791                 INP_INFO_WUNLOCK(&V_ripcbinfo);
  792                 return (error);
  793         }
  794         inp = (struct inpcb *)so->so_pcb;
  795         inp->inp_vflag |= INP_IPV4;
  796         inp->inp_ip_p = proto;
  797         inp->inp_ip_ttl = V_ip_defttl;
  798         rip_inshash(inp);
  799         INP_INFO_WUNLOCK(&V_ripcbinfo);
  800         INP_WUNLOCK(inp);
  801         return (0);
  802 }
  803 
  804 static void
  805 rip_detach(struct socket *so)
  806 {
  807         struct inpcb *inp;
  808 
  809         inp = sotoinpcb(so);
  810         KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
  811         KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 
  812             ("rip_detach: not closed"));
  813 
  814         INP_INFO_WLOCK(&V_ripcbinfo);
  815         INP_WLOCK(inp);
  816         rip_delhash(inp);
  817         if (so == V_ip_mrouter && ip_mrouter_done)
  818                 ip_mrouter_done();
  819         if (ip_rsvp_force_done)
  820                 ip_rsvp_force_done(so);
  821         if (so == V_ip_rsvpd)
  822                 ip_rsvp_done();
  823         in_pcbdetach(inp);
  824         in_pcbfree(inp);
  825         INP_INFO_WUNLOCK(&V_ripcbinfo);
  826 }
  827 
  828 static void
  829 rip_dodisconnect(struct socket *so, struct inpcb *inp)
  830 {
  831 
  832         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  833         INP_WLOCK_ASSERT(inp);
  834 
  835         rip_delhash(inp);
  836         inp->inp_faddr.s_addr = INADDR_ANY;
  837         rip_inshash(inp);
  838         SOCK_LOCK(so);
  839         so->so_state &= ~SS_ISCONNECTED;
  840         SOCK_UNLOCK(so);
  841 }
  842 
  843 static void
  844 rip_abort(struct socket *so)
  845 {
  846         struct inpcb *inp;
  847 
  848         inp = sotoinpcb(so);
  849         KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
  850 
  851         INP_INFO_WLOCK(&V_ripcbinfo);
  852         INP_WLOCK(inp);
  853         rip_dodisconnect(so, inp);
  854         INP_WUNLOCK(inp);
  855         INP_INFO_WUNLOCK(&V_ripcbinfo);
  856 }
  857 
  858 static void
  859 rip_close(struct socket *so)
  860 {
  861         struct inpcb *inp;
  862 
  863         inp = sotoinpcb(so);
  864         KASSERT(inp != NULL, ("rip_close: inp == NULL"));
  865 
  866         INP_INFO_WLOCK(&V_ripcbinfo);
  867         INP_WLOCK(inp);
  868         rip_dodisconnect(so, inp);
  869         INP_WUNLOCK(inp);
  870         INP_INFO_WUNLOCK(&V_ripcbinfo);
  871 }
  872 
  873 static int
  874 rip_disconnect(struct socket *so)
  875 {
  876         struct inpcb *inp;
  877 
  878         if ((so->so_state & SS_ISCONNECTED) == 0)
  879                 return (ENOTCONN);
  880 
  881         inp = sotoinpcb(so);
  882         KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
  883 
  884         INP_INFO_WLOCK(&V_ripcbinfo);
  885         INP_WLOCK(inp);
  886         rip_dodisconnect(so, inp);
  887         INP_WUNLOCK(inp);
  888         INP_INFO_WUNLOCK(&V_ripcbinfo);
  889         return (0);
  890 }
  891 
  892 static int
  893 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  894 {
  895         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
  896         struct inpcb *inp;
  897         int error;
  898 
  899         if (nam->sa_len != sizeof(*addr))
  900                 return (EINVAL);
  901 
  902         error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
  903         if (error != 0)
  904                 return (error);
  905 
  906         inp = sotoinpcb(so);
  907         KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
  908 
  909         if (TAILQ_EMPTY(&V_ifnet) ||
  910             (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
  911             (addr->sin_addr.s_addr &&
  912              (inp->inp_flags & INP_BINDANY) == 0 &&
  913              ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
  914                 return (EADDRNOTAVAIL);
  915 
  916         INP_INFO_WLOCK(&V_ripcbinfo);
  917         INP_WLOCK(inp);
  918         rip_delhash(inp);
  919         inp->inp_laddr = addr->sin_addr;
  920         rip_inshash(inp);
  921         INP_WUNLOCK(inp);
  922         INP_INFO_WUNLOCK(&V_ripcbinfo);
  923         return (0);
  924 }
  925 
  926 static int
  927 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  928 {
  929         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
  930         struct inpcb *inp;
  931 
  932         if (nam->sa_len != sizeof(*addr))
  933                 return (EINVAL);
  934         if (TAILQ_EMPTY(&V_ifnet))
  935                 return (EADDRNOTAVAIL);
  936         if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
  937                 return (EAFNOSUPPORT);
  938 
  939         inp = sotoinpcb(so);
  940         KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
  941 
  942         INP_INFO_WLOCK(&V_ripcbinfo);
  943         INP_WLOCK(inp);
  944         rip_delhash(inp);
  945         inp->inp_faddr = addr->sin_addr;
  946         rip_inshash(inp);
  947         soisconnected(so);
  948         INP_WUNLOCK(inp);
  949         INP_INFO_WUNLOCK(&V_ripcbinfo);
  950         return (0);
  951 }
  952 
  953 static int
  954 rip_shutdown(struct socket *so)
  955 {
  956         struct inpcb *inp;
  957 
  958         inp = sotoinpcb(so);
  959         KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
  960 
  961         INP_WLOCK(inp);
  962         socantsendmore(so);
  963         INP_WUNLOCK(inp);
  964         return (0);
  965 }
  966 
  967 static int
  968 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
  969     struct mbuf *control, struct thread *td)
  970 {
  971         struct inpcb *inp;
  972         u_long dst;
  973 
  974         inp = sotoinpcb(so);
  975         KASSERT(inp != NULL, ("rip_send: inp == NULL"));
  976 
  977         /*
  978          * Note: 'dst' reads below are unlocked.
  979          */
  980         if (so->so_state & SS_ISCONNECTED) {
  981                 if (nam) {
  982                         m_freem(m);
  983                         return (EISCONN);
  984                 }
  985                 dst = inp->inp_faddr.s_addr;    /* Unlocked read. */
  986         } else {
  987                 if (nam == NULL) {
  988                         m_freem(m);
  989                         return (ENOTCONN);
  990                 }
  991                 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
  992         }
  993         return (rip_output(m, so, dst));
  994 }
  995 
  996 static int
  997 rip_pcblist(SYSCTL_HANDLER_ARGS)
  998 {
  999         int error, i, n;
 1000         struct inpcb *inp, **inp_list;
 1001         inp_gen_t gencnt;
 1002         struct xinpgen xig;
 1003 
 1004         /*
 1005          * The process of preparing the TCB list is too time-consuming and
 1006          * resource-intensive to repeat twice on every request.
 1007          */
 1008         if (req->oldptr == 0) {
 1009                 n = V_ripcbinfo.ipi_count;
 1010                 n += imax(n / 8, 10);
 1011                 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
 1012                 return (0);
 1013         }
 1014 
 1015         if (req->newptr != 0)
 1016                 return (EPERM);
 1017 
 1018         /*
 1019          * OK, now we're committed to doing something.
 1020          */
 1021         INP_INFO_RLOCK(&V_ripcbinfo);
 1022         gencnt = V_ripcbinfo.ipi_gencnt;
 1023         n = V_ripcbinfo.ipi_count;
 1024         INP_INFO_RUNLOCK(&V_ripcbinfo);
 1025 
 1026         xig.xig_len = sizeof xig;
 1027         xig.xig_count = n;
 1028         xig.xig_gen = gencnt;
 1029         xig.xig_sogen = so_gencnt;
 1030         error = SYSCTL_OUT(req, &xig, sizeof xig);
 1031         if (error)
 1032                 return (error);
 1033 
 1034         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
 1035         if (inp_list == 0)
 1036                 return (ENOMEM);
 1037 
 1038         INP_INFO_RLOCK(&V_ripcbinfo);
 1039         for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n;
 1040              inp = LIST_NEXT(inp, inp_list)) {
 1041                 INP_WLOCK(inp);
 1042                 if (inp->inp_gencnt <= gencnt &&
 1043                     cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
 1044                         in_pcbref(inp);
 1045                         inp_list[i++] = inp;
 1046                 }
 1047                 INP_WUNLOCK(inp);
 1048         }
 1049         INP_INFO_RUNLOCK(&V_ripcbinfo);
 1050         n = i;
 1051 
 1052         error = 0;
 1053         for (i = 0; i < n; i++) {
 1054                 inp = inp_list[i];
 1055                 INP_RLOCK(inp);
 1056                 if (inp->inp_gencnt <= gencnt) {
 1057                         struct xinpcb xi;
 1058 
 1059                         bzero(&xi, sizeof(xi));
 1060                         xi.xi_len = sizeof xi;
 1061                         /* XXX should avoid extra copy */
 1062                         bcopy(inp, &xi.xi_inp, sizeof *inp);
 1063                         if (inp->inp_socket)
 1064                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
 1065                         INP_RUNLOCK(inp);
 1066                         error = SYSCTL_OUT(req, &xi, sizeof xi);
 1067                 } else
 1068                         INP_RUNLOCK(inp);
 1069         }
 1070         INP_INFO_WLOCK(&V_ripcbinfo);
 1071         for (i = 0; i < n; i++) {
 1072                 inp = inp_list[i];
 1073                 INP_WLOCK(inp);
 1074                 if (!in_pcbrele(inp))
 1075                         INP_WUNLOCK(inp);
 1076         }
 1077         INP_INFO_WUNLOCK(&V_ripcbinfo);
 1078 
 1079         if (!error) {
 1080                 /*
 1081                  * Give the user an updated idea of our state.  If the
 1082                  * generation differs from what we told her before, she knows
 1083                  * that something happened while we were processing this
 1084                  * request, and it might be necessary to retry.
 1085                  */
 1086                 INP_INFO_RLOCK(&V_ripcbinfo);
 1087                 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
 1088                 xig.xig_sogen = so_gencnt;
 1089                 xig.xig_count = V_ripcbinfo.ipi_count;
 1090                 INP_INFO_RUNLOCK(&V_ripcbinfo);
 1091                 error = SYSCTL_OUT(req, &xig, sizeof xig);
 1092         }
 1093         free(inp_list, M_TEMP);
 1094         return (error);
 1095 }
 1096 
 1097 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
 1098     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
 1099 
 1100 struct pr_usrreqs rip_usrreqs = {
 1101         .pru_abort =            rip_abort,
 1102         .pru_attach =           rip_attach,
 1103         .pru_bind =             rip_bind,
 1104         .pru_connect =          rip_connect,
 1105         .pru_control =          in_control,
 1106         .pru_detach =           rip_detach,
 1107         .pru_disconnect =       rip_disconnect,
 1108         .pru_peeraddr =         in_getpeeraddr,
 1109         .pru_send =             rip_send,
 1110         .pru_shutdown =         rip_shutdown,
 1111         .pru_sockaddr =         in_getsockaddr,
 1112         .pru_sosetlabel =       in_pcbsosetlabel,
 1113         .pru_close =            rip_close,
 1114 };

Cache object: f07978a944d16068a626ef7dd4b496b4


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