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.  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  *      @(#)raw_ip.c    8.7 (Berkeley) 5/15/95
   30  * $FreeBSD: releng/6.3/sys/netinet/raw_ip.c 167946 2007-03-27 17:16:22Z bms $
   31  */
   32 
   33 #include "opt_inet6.h"
   34 #include "opt_ipsec.h"
   35 #include "opt_mac.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/jail.h>
   39 #include <sys/kernel.h>
   40 #include <sys/lock.h>
   41 #include <sys/mac.h>
   42 #include <sys/malloc.h>
   43 #include <sys/mbuf.h>
   44 #include <sys/proc.h>
   45 #include <sys/protosw.h>
   46 #include <sys/signalvar.h>
   47 #include <sys/socket.h>
   48 #include <sys/socketvar.h>
   49 #include <sys/sx.h>
   50 #include <sys/sysctl.h>
   51 #include <sys/systm.h>
   52 
   53 #include <vm/uma.h>
   54 
   55 #include <net/if.h>
   56 #include <net/route.h>
   57 
   58 #include <netinet/in.h>
   59 #include <netinet/in_systm.h>
   60 #include <netinet/in_pcb.h>
   61 #include <netinet/in_var.h>
   62 #include <netinet/ip.h>
   63 #include <netinet/ip_var.h>
   64 #include <netinet/ip_mroute.h>
   65 
   66 #include <netinet/ip_fw.h>
   67 #include <netinet/ip_dummynet.h>
   68 
   69 #ifdef FAST_IPSEC
   70 #include <netipsec/ipsec.h>
   71 #endif /*FAST_IPSEC*/
   72 
   73 #ifdef IPSEC
   74 #include <netinet6/ipsec.h>
   75 #endif /*IPSEC*/
   76 
   77 struct  inpcbhead ripcb;
   78 struct  inpcbinfo ripcbinfo;
   79 
   80 /* control hooks for ipfw and dummynet */
   81 ip_fw_ctl_t *ip_fw_ctl_ptr = NULL;
   82 ip_dn_ctl_t *ip_dn_ctl_ptr = NULL;
   83 
   84 /*
   85  * hooks for multicast routing. They all default to NULL,
   86  * so leave them not initialized and rely on BSS being set to 0.
   87  */
   88 
   89 /* The socket used to communicate with the multicast routing daemon.  */
   90 struct socket  *ip_mrouter;
   91 
   92 /* The various mrouter and rsvp functions */
   93 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
   94 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
   95 int (*ip_mrouter_done)(void);
   96 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
   97                    struct ip_moptions *);
   98 int (*mrt_ioctl)(int, caddr_t);
   99 int (*legal_vif_num)(int);
  100 u_long (*ip_mcast_src)(int);
  101 
  102 void (*rsvp_input_p)(struct mbuf *m, int off);
  103 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
  104 void (*ip_rsvp_force_done)(struct socket *);
  105 
  106 /*
  107  * Raw interface to IP protocol.
  108  */
  109 
  110 /*
  111  * Initialize raw connection block q.
  112  */
  113 static void
  114 rip_zone_change(void *tag)
  115 {
  116 
  117         uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
  118 }
  119 
  120 static int
  121 rip_inpcb_init(void *mem, int size, int flags)
  122 {
  123         struct inpcb *inp = mem;
  124 
  125         INP_LOCK_INIT(inp, "inp", "rawinp");
  126         return (0);
  127 }
  128 
  129 void
  130 rip_init()
  131 {
  132         INP_INFO_LOCK_INIT(&ripcbinfo, "rip");
  133         LIST_INIT(&ripcb);
  134         ripcbinfo.listhead = &ripcb;
  135         /*
  136          * XXX We don't use the hash list for raw IP, but it's easier
  137          * to allocate a one entry hash list than it is to check all
  138          * over the place for hashbase == NULL.
  139          */
  140         ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
  141         ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
  142         ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
  143             NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  144         uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
  145         EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change,
  146                 NULL, EVENTHANDLER_PRI_ANY);
  147 }
  148 
  149 static struct   sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
  150 
  151 static int
  152 raw_append(struct inpcb *last, struct ip *ip, struct mbuf *n)
  153 {
  154         int policyfail = 0;
  155 
  156         INP_LOCK_ASSERT(last);
  157 
  158 #if defined(IPSEC) || defined(FAST_IPSEC)
  159         /* check AH/ESP integrity. */
  160         if (ipsec4_in_reject(n, last)) {
  161                 policyfail = 1;
  162 #ifdef IPSEC
  163                 ipsecstat.in_polvio++;
  164 #endif /*IPSEC*/
  165                 /* do not inject data to pcb */
  166         }
  167 #endif /*IPSEC || FAST_IPSEC*/
  168 #ifdef MAC
  169         if (!policyfail && mac_check_inpcb_deliver(last, n) != 0)
  170                 policyfail = 1;
  171 #endif
  172         /* Check the minimum TTL for socket. */
  173         if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
  174                 policyfail = 1;
  175         if (!policyfail) {
  176                 struct mbuf *opts = NULL;
  177                 struct socket *so;
  178 
  179                 so = last->inp_socket;
  180                 if ((last->inp_flags & INP_CONTROLOPTS) ||
  181                     (so->so_options & SO_TIMESTAMP))
  182                         ip_savecontrol(last, &opts, ip, n);
  183                 SOCKBUF_LOCK(&so->so_rcv);
  184                 if (sbappendaddr_locked(&so->so_rcv,
  185                     (struct sockaddr *)&ripsrc, n, opts) == 0) {
  186                         /* should notify about lost packet */
  187                         m_freem(n);
  188                         if (opts)
  189                                 m_freem(opts);
  190                         SOCKBUF_UNLOCK(&so->so_rcv);
  191                 } else
  192                         sorwakeup_locked(so);
  193         } else
  194                 m_freem(n);
  195         return policyfail;
  196 }
  197 
  198 /*
  199  * Setup generic address and protocol structures
  200  * for raw_input routine, then pass them along with
  201  * mbuf chain.
  202  */
  203 void
  204 rip_input(struct mbuf *m, int off)
  205 {
  206         struct ip *ip = mtod(m, struct ip *);
  207         int proto = ip->ip_p;
  208         struct inpcb *inp, *last;
  209 
  210         INP_INFO_RLOCK(&ripcbinfo);
  211         ripsrc.sin_addr = ip->ip_src;
  212         last = NULL;
  213         LIST_FOREACH(inp, &ripcb, inp_list) {
  214                 INP_LOCK(inp);
  215                 if (inp->inp_ip_p && inp->inp_ip_p != proto) {
  216         docontinue:
  217                         INP_UNLOCK(inp);
  218                         continue;
  219                 }
  220 #ifdef INET6
  221                 if ((inp->inp_vflag & INP_IPV4) == 0)
  222                         goto docontinue;
  223 #endif
  224                 if (inp->inp_laddr.s_addr &&
  225                     inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
  226                         goto docontinue;
  227                 if (inp->inp_faddr.s_addr &&
  228                     inp->inp_faddr.s_addr != ip->ip_src.s_addr)
  229                         goto docontinue;
  230                 if (jailed(inp->inp_socket->so_cred))
  231                         if (htonl(prison_getip(inp->inp_socket->so_cred)) !=
  232                             ip->ip_dst.s_addr)
  233                                 goto docontinue;
  234                 if (last) {
  235                         struct mbuf *n;
  236 
  237                         n = m_copy(m, 0, (int)M_COPYALL);
  238                         if (n != NULL)
  239                                 (void) raw_append(last, ip, n);
  240                         /* XXX count dropped packet */
  241                         INP_UNLOCK(last);
  242                 }
  243                 last = inp;
  244         }
  245         if (last != NULL) {
  246                 if (raw_append(last, ip, m) != 0)
  247                         ipstat.ips_delivered--;
  248                 INP_UNLOCK(last);
  249         } else {
  250                 m_freem(m);
  251                 ipstat.ips_noproto++;
  252                 ipstat.ips_delivered--;
  253         }
  254         INP_INFO_RUNLOCK(&ripcbinfo);
  255 }
  256 
  257 /*
  258  * Generate IP header and pass packet to ip_output.
  259  * Tack on options user may have setup with control call.
  260  */
  261 int
  262 rip_output(struct mbuf *m, struct socket *so, u_long dst)
  263 {
  264         struct ip *ip;
  265         int error;
  266         struct inpcb *inp = sotoinpcb(so);
  267         int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
  268             IP_ALLOWBROADCAST;
  269 
  270         /*
  271          * If the user handed us a complete IP packet, use it.
  272          * Otherwise, allocate an mbuf for a header and fill it in.
  273          */
  274         if ((inp->inp_flags & INP_HDRINCL) == 0) {
  275                 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
  276                         m_freem(m);
  277                         return(EMSGSIZE);
  278                 }
  279                 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
  280                 if (m == NULL)
  281                         return(ENOBUFS);
  282 
  283                 INP_LOCK(inp);
  284                 ip = mtod(m, struct ip *);
  285                 ip->ip_tos = inp->inp_ip_tos;
  286                 if (inp->inp_flags & INP_DONTFRAG)
  287                         ip->ip_off = IP_DF;
  288                 else
  289                         ip->ip_off = 0;
  290                 ip->ip_p = inp->inp_ip_p;
  291                 ip->ip_len = m->m_pkthdr.len;
  292                 if (jailed(inp->inp_socket->so_cred))
  293                         ip->ip_src.s_addr =
  294                             htonl(prison_getip(inp->inp_socket->so_cred));
  295                 else
  296                         ip->ip_src = inp->inp_laddr;
  297                 ip->ip_dst.s_addr = dst;
  298                 ip->ip_ttl = inp->inp_ip_ttl;
  299         } else {
  300                 if (m->m_pkthdr.len > IP_MAXPACKET) {
  301                         m_freem(m);
  302                         return(EMSGSIZE);
  303                 }
  304                 INP_LOCK(inp);
  305                 ip = mtod(m, struct ip *);
  306                 if (jailed(inp->inp_socket->so_cred)) {
  307                         if (ip->ip_src.s_addr !=
  308                             htonl(prison_getip(inp->inp_socket->so_cred))) {
  309                                 INP_UNLOCK(inp);
  310                                 m_freem(m);
  311                                 return (EPERM);
  312                         }
  313                 }
  314                 /* don't allow both user specified and setsockopt options,
  315                    and don't allow packet length sizes that will crash */
  316                 if (((ip->ip_hl != (sizeof (*ip) >> 2))
  317                      && inp->inp_options)
  318                     || (ip->ip_len > m->m_pkthdr.len)
  319                     || (ip->ip_len < (ip->ip_hl << 2))) {
  320                         INP_UNLOCK(inp);
  321                         m_freem(m);
  322                         return EINVAL;
  323                 }
  324                 if (ip->ip_id == 0)
  325                         ip->ip_id = ip_newid();
  326                 /* XXX prevent ip_output from overwriting header fields */
  327                 flags |= IP_RAWOUTPUT;
  328                 ipstat.ips_rawout++;
  329         }
  330 
  331         if (inp->inp_flags & INP_ONESBCAST)
  332                 flags |= IP_SENDONES;
  333 
  334 #ifdef MAC
  335         mac_create_mbuf_from_inpcb(inp, m);
  336 #endif
  337 
  338         error = ip_output(m, inp->inp_options, NULL, flags,
  339             inp->inp_moptions, inp);
  340         INP_UNLOCK(inp);
  341         return error;
  342 }
  343 
  344 /*
  345  * Raw IP socket option processing.
  346  *
  347  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
  348  * only be created by a privileged process, and as such, socket option
  349  * operations to manage system properties on any raw socket were allowed to
  350  * take place without explicit additional access control checks.  However,
  351  * raw sockets can now also be created in jail(), and therefore explicit
  352  * checks are now required.  Likewise, raw sockets can be used by a process
  353  * after it gives up privilege, so some caution is required.  For options
  354  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
  355  * performed in ip_ctloutput() and therefore no check occurs here.
  356  * Unilaterally checking suser() here breaks normal IP socket option
  357  * operations on raw sockets.
  358  *
  359  * When adding new socket options here, make sure to add access control
  360  * checks here as necessary.
  361  */
  362 int
  363 rip_ctloutput(struct socket *so, struct sockopt *sopt)
  364 {
  365         struct  inpcb *inp = sotoinpcb(so);
  366         int     error, optval;
  367 
  368         if (sopt->sopt_level != IPPROTO_IP)
  369                 return (EINVAL);
  370 
  371         error = 0;
  372         switch (sopt->sopt_dir) {
  373         case SOPT_GET:
  374                 switch (sopt->sopt_name) {
  375                 case IP_HDRINCL:
  376                         optval = inp->inp_flags & INP_HDRINCL;
  377                         error = sooptcopyout(sopt, &optval, sizeof optval);
  378                         break;
  379 
  380                 case IP_FW_ADD: /* ADD actually returns the body... */
  381                 case IP_FW_GET:
  382                 case IP_FW_TABLE_GETSIZE:
  383                 case IP_FW_TABLE_LIST:
  384                         error = suser(curthread);
  385                         if (error != 0)
  386                                 return (error);
  387                         if (ip_fw_ctl_ptr != NULL)
  388                                 error = ip_fw_ctl_ptr(sopt);
  389                         else
  390                                 error = ENOPROTOOPT;
  391                         break;
  392 
  393                 case IP_DUMMYNET_GET:
  394                         error = suser(curthread);
  395                         if (error != 0)
  396                                 return (error);
  397                         if (ip_dn_ctl_ptr != NULL)
  398                                 error = ip_dn_ctl_ptr(sopt);
  399                         else
  400                                 error = ENOPROTOOPT;
  401                         break ;
  402 
  403                 case MRT_INIT:
  404                 case MRT_DONE:
  405                 case MRT_ADD_VIF:
  406                 case MRT_DEL_VIF:
  407                 case MRT_ADD_MFC:
  408                 case MRT_DEL_MFC:
  409                 case MRT_VERSION:
  410                 case MRT_ASSERT:
  411                 case MRT_API_SUPPORT:
  412                 case MRT_API_CONFIG:
  413                 case MRT_ADD_BW_UPCALL:
  414                 case MRT_DEL_BW_UPCALL:
  415                         error = suser(curthread);
  416                         if (error != 0)
  417                                 return (error);
  418                         error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
  419                                 EOPNOTSUPP;
  420                         break;
  421 
  422                 default:
  423                         error = ip_ctloutput(so, sopt);
  424                         break;
  425                 }
  426                 break;
  427 
  428         case SOPT_SET:
  429                 switch (sopt->sopt_name) {
  430                 case IP_HDRINCL:
  431                         error = sooptcopyin(sopt, &optval, sizeof optval,
  432                                             sizeof optval);
  433                         if (error)
  434                                 break;
  435                         if (optval)
  436                                 inp->inp_flags |= INP_HDRINCL;
  437                         else
  438                                 inp->inp_flags &= ~INP_HDRINCL;
  439                         break;
  440 
  441                 case IP_FW_ADD:
  442                 case IP_FW_DEL:
  443                 case IP_FW_FLUSH:
  444                 case IP_FW_ZERO:
  445                 case IP_FW_RESETLOG:
  446                 case IP_FW_TABLE_ADD:
  447                 case IP_FW_TABLE_DEL:
  448                 case IP_FW_TABLE_FLUSH:
  449                         error = suser(curthread);
  450                         if (error != 0)
  451                                 return (error);
  452                         if (ip_fw_ctl_ptr != NULL)
  453                                 error = ip_fw_ctl_ptr(sopt);
  454                         else
  455                                 error = ENOPROTOOPT;
  456                         break;
  457 
  458                 case IP_DUMMYNET_CONFIGURE:
  459                 case IP_DUMMYNET_DEL:
  460                 case IP_DUMMYNET_FLUSH:
  461                         error = suser(curthread);
  462                         if (error != 0)
  463                                 return (error);
  464                         if (ip_dn_ctl_ptr != NULL)
  465                                 error = ip_dn_ctl_ptr(sopt);
  466                         else
  467                                 error = ENOPROTOOPT ;
  468                         break ;
  469 
  470                 case IP_RSVP_ON:
  471                         error = suser(curthread);
  472                         if (error != 0)
  473                                 return (error);
  474                         error = ip_rsvp_init(so);
  475                         break;
  476 
  477                 case IP_RSVP_OFF:
  478                         error = suser(curthread);
  479                         if (error != 0)
  480                                 return (error);
  481                         error = ip_rsvp_done();
  482                         break;
  483 
  484                 case IP_RSVP_VIF_ON:
  485                 case IP_RSVP_VIF_OFF:
  486                         error = suser(curthread);
  487                         if (error != 0)
  488                                 return (error);
  489                         error = ip_rsvp_vif ?
  490                                 ip_rsvp_vif(so, sopt) : EINVAL;
  491                         break;
  492 
  493                 case MRT_INIT:
  494                 case MRT_DONE:
  495                 case MRT_ADD_VIF:
  496                 case MRT_DEL_VIF:
  497                 case MRT_ADD_MFC:
  498                 case MRT_DEL_MFC:
  499                 case MRT_VERSION:
  500                 case MRT_ASSERT:
  501                 case MRT_API_SUPPORT:
  502                 case MRT_API_CONFIG:
  503                 case MRT_ADD_BW_UPCALL:
  504                 case MRT_DEL_BW_UPCALL:
  505                         error = suser(curthread);
  506                         if (error != 0)
  507                                 return (error);
  508                         error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
  509                                         EOPNOTSUPP;
  510                         break;
  511 
  512                 default:
  513                         error = ip_ctloutput(so, sopt);
  514                         break;
  515                 }
  516                 break;
  517         }
  518 
  519         return (error);
  520 }
  521 
  522 /*
  523  * This function exists solely to receive the PRC_IFDOWN messages which
  524  * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
  525  * and calls in_ifadown() to remove all routes corresponding to that address.
  526  * It also receives the PRC_IFUP messages from if_up() and reinstalls the
  527  * interface routes.
  528  */
  529 void
  530 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
  531 {
  532         struct in_ifaddr *ia;
  533         struct ifnet *ifp;
  534         int err;
  535         int flags;
  536 
  537         switch (cmd) {
  538         case PRC_IFDOWN:
  539                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
  540                         if (ia->ia_ifa.ifa_addr == sa
  541                             && (ia->ia_flags & IFA_ROUTE)) {
  542                                 /*
  543                                  * in_ifscrub kills the interface route.
  544                                  */
  545                                 in_ifscrub(ia->ia_ifp, ia);
  546                                 /*
  547                                  * in_ifadown gets rid of all the rest of
  548                                  * the routes.  This is not quite the right
  549                                  * thing to do, but at least if we are running
  550                                  * a routing process they will come back.
  551                                  */
  552                                 in_ifadown(&ia->ia_ifa, 0);
  553                                 break;
  554                         }
  555                 }
  556                 break;
  557 
  558         case PRC_IFUP:
  559                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
  560                         if (ia->ia_ifa.ifa_addr == sa)
  561                                 break;
  562                 }
  563                 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
  564                         return;
  565                 flags = RTF_UP;
  566                 ifp = ia->ia_ifa.ifa_ifp;
  567 
  568                 if ((ifp->if_flags & IFF_LOOPBACK)
  569                     || (ifp->if_flags & IFF_POINTOPOINT))
  570                         flags |= RTF_HOST;
  571 
  572                 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
  573                 if (err == 0)
  574                         ia->ia_flags |= IFA_ROUTE;
  575                 break;
  576         }
  577 }
  578 
  579 u_long  rip_sendspace = 9216;
  580 u_long  rip_recvspace = 9216;
  581 
  582 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
  583     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
  584 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
  585     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
  586 
  587 static int
  588 rip_attach(struct socket *so, int proto, struct thread *td)
  589 {
  590         struct inpcb *inp;
  591         int error;
  592 
  593         /* XXX why not lower? */
  594         INP_INFO_WLOCK(&ripcbinfo);
  595         inp = sotoinpcb(so);
  596         if (inp) {
  597                 /* XXX counter, printf */
  598                 INP_INFO_WUNLOCK(&ripcbinfo);
  599                 return EINVAL;
  600         }
  601         if (jailed(td->td_ucred) && !jail_allow_raw_sockets) {
  602                 INP_INFO_WUNLOCK(&ripcbinfo);
  603                 return (EPERM);
  604         }
  605         if ((error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL)) != 0) {
  606                 INP_INFO_WUNLOCK(&ripcbinfo);
  607                 return error;
  608         }
  609         if (proto >= IPPROTO_MAX || proto < 0) {
  610                 INP_INFO_WUNLOCK(&ripcbinfo);
  611                 return EPROTONOSUPPORT;
  612         }
  613 
  614         error = soreserve(so, rip_sendspace, rip_recvspace);
  615         if (error) {
  616                 INP_INFO_WUNLOCK(&ripcbinfo);
  617                 return error;
  618         }
  619         error = in_pcballoc(so, &ripcbinfo);
  620         if (error) {
  621                 INP_INFO_WUNLOCK(&ripcbinfo);
  622                 return error;
  623         }
  624         inp = (struct inpcb *)so->so_pcb;
  625         INP_INFO_WUNLOCK(&ripcbinfo);
  626         inp->inp_vflag |= INP_IPV4;
  627         inp->inp_ip_p = proto;
  628         inp->inp_ip_ttl = ip_defttl;
  629         INP_UNLOCK(inp);
  630         return 0;
  631 }
  632 
  633 static void
  634 rip_pcbdetach(struct socket *so, struct inpcb *inp)
  635 {
  636 
  637         INP_INFO_WLOCK_ASSERT(&ripcbinfo);
  638         INP_LOCK_ASSERT(inp);
  639 
  640         if (so == ip_mrouter && ip_mrouter_done)
  641                 ip_mrouter_done();
  642         if (ip_rsvp_force_done)
  643                 ip_rsvp_force_done(so);
  644         if (so == ip_rsvpd)
  645                 ip_rsvp_done();
  646         in_pcbdetach(inp);
  647 }
  648 
  649 static int
  650 rip_detach(struct socket *so)
  651 {
  652         struct inpcb *inp;
  653 
  654         INP_INFO_WLOCK(&ripcbinfo);
  655         inp = sotoinpcb(so);
  656         if (inp == 0) {
  657                 /* XXX counter, printf */
  658                 INP_INFO_WUNLOCK(&ripcbinfo);
  659                 return EINVAL;
  660         }
  661         INP_LOCK(inp);
  662         rip_pcbdetach(so, inp);
  663         INP_INFO_WUNLOCK(&ripcbinfo);
  664         return 0;
  665 }
  666 
  667 static int
  668 rip_abort(struct socket *so)
  669 {
  670         struct inpcb *inp;
  671 
  672         INP_INFO_WLOCK(&ripcbinfo);
  673         inp = sotoinpcb(so);
  674         if (inp == 0) {
  675                 INP_INFO_WUNLOCK(&ripcbinfo);
  676                 return EINVAL;  /* ??? possible? panic instead? */
  677         }
  678         INP_LOCK(inp);
  679         soisdisconnected(so);
  680         if (so->so_state & SS_NOFDREF)
  681                 rip_pcbdetach(so, inp);
  682         else
  683                 INP_UNLOCK(inp);
  684         INP_INFO_WUNLOCK(&ripcbinfo);
  685         return 0;
  686 }
  687 
  688 static int
  689 rip_disconnect(struct socket *so)
  690 {
  691         if ((so->so_state & SS_ISCONNECTED) == 0)
  692                 return ENOTCONN;
  693         return rip_abort(so);
  694 }
  695 
  696 static int
  697 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  698 {
  699         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
  700         struct inpcb *inp;
  701 
  702         if (nam->sa_len != sizeof(*addr))
  703                 return EINVAL;
  704 
  705         if (jailed(td->td_ucred)) {
  706                 if (addr->sin_addr.s_addr == INADDR_ANY)
  707                         addr->sin_addr.s_addr =
  708                             htonl(prison_getip(td->td_ucred));
  709                 if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr)
  710                         return (EADDRNOTAVAIL);
  711         }
  712 
  713         if (TAILQ_EMPTY(&ifnet) ||
  714             (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
  715             (addr->sin_addr.s_addr &&
  716              ifa_ifwithaddr((struct sockaddr *)addr) == 0))
  717                 return EADDRNOTAVAIL;
  718 
  719         INP_INFO_WLOCK(&ripcbinfo);
  720         inp = sotoinpcb(so);
  721         if (inp == 0) {
  722                 INP_INFO_WUNLOCK(&ripcbinfo);
  723                 return EINVAL;
  724         }
  725         INP_LOCK(inp);
  726         inp->inp_laddr = addr->sin_addr;
  727         INP_UNLOCK(inp);
  728         INP_INFO_WUNLOCK(&ripcbinfo);
  729         return 0;
  730 }
  731 
  732 static int
  733 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  734 {
  735         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
  736         struct inpcb *inp;
  737 
  738         if (nam->sa_len != sizeof(*addr))
  739                 return EINVAL;
  740         if (TAILQ_EMPTY(&ifnet))
  741                 return EADDRNOTAVAIL;
  742         if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
  743                 return EAFNOSUPPORT;
  744 
  745         INP_INFO_WLOCK(&ripcbinfo);
  746         inp = sotoinpcb(so);
  747         if (inp == 0) {
  748                 INP_INFO_WUNLOCK(&ripcbinfo);
  749                 return EINVAL;
  750         }
  751         INP_LOCK(inp);
  752         inp->inp_faddr = addr->sin_addr;
  753         soisconnected(so);
  754         INP_UNLOCK(inp);
  755         INP_INFO_WUNLOCK(&ripcbinfo);
  756         return 0;
  757 }
  758 
  759 static int
  760 rip_shutdown(struct socket *so)
  761 {
  762         struct inpcb *inp;
  763 
  764         INP_INFO_RLOCK(&ripcbinfo);
  765         inp = sotoinpcb(so);
  766         if (inp == 0) {
  767                 INP_INFO_RUNLOCK(&ripcbinfo);
  768                 return EINVAL;
  769         }
  770         INP_LOCK(inp);
  771         INP_INFO_RUNLOCK(&ripcbinfo);
  772         socantsendmore(so);
  773         INP_UNLOCK(inp);
  774         return 0;
  775 }
  776 
  777 static int
  778 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
  779          struct mbuf *control, struct thread *td)
  780 {
  781         struct inpcb *inp;
  782         u_long dst;
  783         int ret;
  784 
  785         INP_INFO_WLOCK(&ripcbinfo);
  786         inp = sotoinpcb(so);
  787         if (so->so_state & SS_ISCONNECTED) {
  788                 if (nam) {
  789                         INP_INFO_WUNLOCK(&ripcbinfo);
  790                         m_freem(m);
  791                         return EISCONN;
  792                 }
  793                 dst = inp->inp_faddr.s_addr;
  794         } else {
  795                 if (nam == NULL) {
  796                         INP_INFO_WUNLOCK(&ripcbinfo);
  797                         m_freem(m);
  798                         return ENOTCONN;
  799                 }
  800                 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
  801         }
  802         ret = rip_output(m, so, dst);
  803         INP_INFO_WUNLOCK(&ripcbinfo);
  804         return ret;
  805 }
  806 
  807 static int
  808 rip_pcblist(SYSCTL_HANDLER_ARGS)
  809 {
  810         int error, i, n;
  811         struct inpcb *inp, **inp_list;
  812         inp_gen_t gencnt;
  813         struct xinpgen xig;
  814 
  815         /*
  816          * The process of preparing the TCB list is too time-consuming and
  817          * resource-intensive to repeat twice on every request.
  818          */
  819         if (req->oldptr == 0) {
  820                 n = ripcbinfo.ipi_count;
  821                 req->oldidx = 2 * (sizeof xig)
  822                         + (n + n/8) * sizeof(struct xinpcb);
  823                 return 0;
  824         }
  825 
  826         if (req->newptr != 0)
  827                 return EPERM;
  828 
  829         /*
  830          * OK, now we're committed to doing something.
  831          */
  832         INP_INFO_RLOCK(&ripcbinfo);
  833         gencnt = ripcbinfo.ipi_gencnt;
  834         n = ripcbinfo.ipi_count;
  835         INP_INFO_RUNLOCK(&ripcbinfo);
  836 
  837         xig.xig_len = sizeof xig;
  838         xig.xig_count = n;
  839         xig.xig_gen = gencnt;
  840         xig.xig_sogen = so_gencnt;
  841         error = SYSCTL_OUT(req, &xig, sizeof xig);
  842         if (error)
  843                 return error;
  844 
  845         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
  846         if (inp_list == 0)
  847                 return ENOMEM;
  848         
  849         INP_INFO_RLOCK(&ripcbinfo);
  850         for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n;
  851              inp = LIST_NEXT(inp, inp_list)) {
  852                 INP_LOCK(inp);
  853                 if (inp->inp_gencnt <= gencnt &&
  854                     cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) {
  855                         /* XXX held references? */
  856                         inp_list[i++] = inp;
  857                 }
  858                 INP_UNLOCK(inp);
  859         }
  860         INP_INFO_RUNLOCK(&ripcbinfo);
  861         n = i;
  862 
  863         error = 0;
  864         for (i = 0; i < n; i++) {
  865                 inp = inp_list[i];
  866                 INP_LOCK(inp);
  867                 if (inp->inp_gencnt <= gencnt) {
  868                         struct xinpcb xi;
  869                         bzero(&xi, sizeof(xi));
  870                         xi.xi_len = sizeof xi;
  871                         /* XXX should avoid extra copy */
  872                         bcopy(inp, &xi.xi_inp, sizeof *inp);
  873                         if (inp->inp_socket)
  874                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
  875                         INP_UNLOCK(inp);
  876                         error = SYSCTL_OUT(req, &xi, sizeof xi);
  877                 } else
  878                         INP_UNLOCK(inp);
  879         }
  880         if (!error) {
  881                 /*
  882                  * Give the user an updated idea of our state.
  883                  * If the generation differs from what we told
  884                  * her before, she knows that something happened
  885                  * while we were processing this request, and it
  886                  * might be necessary to retry.
  887                  */
  888                 INP_INFO_RLOCK(&ripcbinfo);
  889                 xig.xig_gen = ripcbinfo.ipi_gencnt;
  890                 xig.xig_sogen = so_gencnt;
  891                 xig.xig_count = ripcbinfo.ipi_count;
  892                 INP_INFO_RUNLOCK(&ripcbinfo);
  893                 error = SYSCTL_OUT(req, &xig, sizeof xig);
  894         }
  895         free(inp_list, M_TEMP);
  896         return error;
  897 }
  898 
  899 /*
  900  * This is the wrapper function for in_setsockaddr.  We just pass down
  901  * the pcbinfo for in_setpeeraddr to lock.
  902  */
  903 static int
  904 rip_sockaddr(struct socket *so, struct sockaddr **nam)
  905 {
  906         return (in_setsockaddr(so, nam, &ripcbinfo));
  907 }
  908 
  909 /*
  910  * This is the wrapper function for in_setpeeraddr.  We just pass down
  911  * the pcbinfo for in_setpeeraddr to lock.
  912  */
  913 static int
  914 rip_peeraddr(struct socket *so, struct sockaddr **nam)
  915 {
  916         return (in_setpeeraddr(so, nam, &ripcbinfo));
  917 }
  918 
  919 
  920 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
  921             rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
  922 
  923 struct pr_usrreqs rip_usrreqs = {
  924         .pru_abort =            rip_abort,
  925         .pru_attach =           rip_attach,
  926         .pru_bind =             rip_bind,
  927         .pru_connect =          rip_connect,
  928         .pru_control =          in_control,
  929         .pru_detach =           rip_detach,
  930         .pru_disconnect =       rip_disconnect,
  931         .pru_peeraddr =         rip_peeraddr,
  932         .pru_send =             rip_send,
  933         .pru_shutdown =         rip_shutdown,
  934         .pru_sockaddr =         rip_sockaddr,
  935         .pru_sosetlabel =       in_pcbsosetlabel
  936 };

Cache object: 6066b7d793661fe0c7681866889539ea


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