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

Cache object: 9fc89839a6653c488f062261bd52975c


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