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

Cache object: a9ac4ecefc734175b9f4a62049d4df9f


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