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$");
   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 (jailed(inp->inp_cred) &&
  269                     (htonl(prison_getip(inp->inp_cred)) !=
  270                     ip->ip_dst.s_addr)) {
  271                         continue;
  272                 }
  273                 if (last) {
  274                         struct mbuf *n;
  275 
  276                         n = m_copy(m, 0, (int)M_COPYALL);
  277                         if (n != NULL)
  278                             (void) rip_append(last, ip, n, &ripsrc);
  279                         /* XXX count dropped packet */
  280                         INP_RUNLOCK(last);
  281                 }
  282                 INP_RLOCK(inp);
  283                 last = inp;
  284         }
  285         LIST_FOREACH(inp, &ripcbinfo.ipi_hashbase[0], inp_hash) {
  286                 if (inp->inp_ip_p && inp->inp_ip_p != proto)
  287                         continue;
  288 #ifdef INET6
  289                 /* XXX inp locking */
  290                 if ((inp->inp_vflag & INP_IPV4) == 0)
  291                         continue;
  292 #endif
  293                 if (inp->inp_laddr.s_addr &&
  294                     inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
  295                         continue;
  296                 if (inp->inp_faddr.s_addr &&
  297                     inp->inp_faddr.s_addr != ip->ip_src.s_addr)
  298                         continue;
  299                 if (jailed(inp->inp_cred) &&
  300                     (htonl(prison_getip(inp->inp_cred)) !=
  301                     ip->ip_dst.s_addr)) {
  302                         continue;
  303                 }
  304                 if (last) {
  305                         struct mbuf *n;
  306 
  307                         n = m_copy(m, 0, (int)M_COPYALL);
  308                         if (n != NULL)
  309                                 (void) rip_append(last, ip, n, &ripsrc);
  310                         /* XXX count dropped packet */
  311                         INP_RUNLOCK(last);
  312                 }
  313                 INP_RLOCK(inp);
  314                 last = inp;
  315         }
  316         INP_INFO_RUNLOCK(&ripcbinfo);
  317         if (last != NULL) {
  318                 if (rip_append(last, ip, m, &ripsrc) != 0)
  319                         ipstat.ips_delivered--;
  320                 INP_RUNLOCK(last);
  321         } else {
  322                 m_freem(m);
  323                 ipstat.ips_noproto++;
  324                 ipstat.ips_delivered--;
  325         }
  326 }
  327 
  328 /*
  329  * Generate IP header and pass packet to ip_output.  Tack on options user may
  330  * have setup with control call.
  331  */
  332 int
  333 rip_output(struct mbuf *m, struct socket *so, u_long dst)
  334 {
  335         struct ip *ip;
  336         int error;
  337         struct inpcb *inp = sotoinpcb(so);
  338         int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
  339             IP_ALLOWBROADCAST;
  340 
  341         /*
  342          * If the user handed us a complete IP packet, use it.  Otherwise,
  343          * allocate an mbuf for a header and fill it in.
  344          */
  345         if ((inp->inp_flags & INP_HDRINCL) == 0) {
  346                 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
  347                         m_freem(m);
  348                         return(EMSGSIZE);
  349                 }
  350                 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
  351                 if (m == NULL)
  352                         return(ENOBUFS);
  353 
  354                 INP_RLOCK(inp);
  355                 ip = mtod(m, struct ip *);
  356                 ip->ip_tos = inp->inp_ip_tos;
  357                 if (inp->inp_flags & INP_DONTFRAG)
  358                         ip->ip_off = IP_DF;
  359                 else
  360                         ip->ip_off = 0;
  361                 ip->ip_p = inp->inp_ip_p;
  362                 ip->ip_len = m->m_pkthdr.len;
  363                 if (jailed(inp->inp_cred))
  364                         ip->ip_src.s_addr =
  365                             htonl(prison_getip(inp->inp_cred));
  366                 else
  367                         ip->ip_src = inp->inp_laddr;
  368                 ip->ip_dst.s_addr = dst;
  369                 ip->ip_ttl = inp->inp_ip_ttl;
  370         } else {
  371                 if (m->m_pkthdr.len > IP_MAXPACKET) {
  372                         m_freem(m);
  373                         return(EMSGSIZE);
  374                 }
  375                 INP_RLOCK(inp);
  376                 ip = mtod(m, struct ip *);
  377                 if (jailed(inp->inp_cred)) {
  378                         if (ip->ip_src.s_addr !=
  379                             htonl(prison_getip(inp->inp_cred))) {
  380                                 INP_RUNLOCK(inp);
  381                                 m_freem(m);
  382                                 return (EPERM);
  383                         }
  384                 }
  385 
  386                 /*
  387                  * Don't allow both user specified and setsockopt options,
  388                  * and don't allow packet length sizes that will crash.
  389                  */
  390                 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
  391                     || (ip->ip_len > m->m_pkthdr.len)
  392                     || (ip->ip_len < (ip->ip_hl << 2))) {
  393                         INP_RUNLOCK(inp);
  394                         m_freem(m);
  395                         return (EINVAL);
  396                 }
  397                 if (ip->ip_id == 0)
  398                         ip->ip_id = ip_newid();
  399 
  400                 /*
  401                  * XXX prevent ip_output from overwriting header fields.
  402                  */
  403                 flags |= IP_RAWOUTPUT;
  404                 ipstat.ips_rawout++;
  405         }
  406 
  407         if (inp->inp_flags & INP_ONESBCAST)
  408                 flags |= IP_SENDONES;
  409 
  410 #ifdef MAC
  411         mac_create_mbuf_from_inpcb(inp, m);
  412 #endif
  413 
  414         error = ip_output(m, inp->inp_options, NULL, flags,
  415             inp->inp_moptions, inp);
  416         INP_RUNLOCK(inp);
  417         return (error);
  418 }
  419 
  420 /*
  421  * Raw IP socket option processing.
  422  *
  423  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
  424  * only be created by a privileged process, and as such, socket option
  425  * operations to manage system properties on any raw socket were allowed to
  426  * take place without explicit additional access control checks.  However,
  427  * raw sockets can now also be created in jail(), and therefore explicit
  428  * checks are now required.  Likewise, raw sockets can be used by a process
  429  * after it gives up privilege, so some caution is required.  For options
  430  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
  431  * performed in ip_ctloutput() and therefore no check occurs here.
  432  * Unilaterally checking priv_check() here breaks normal IP socket option
  433  * operations on raw sockets.
  434  *
  435  * When adding new socket options here, make sure to add access control
  436  * checks here as necessary.
  437  */
  438 int
  439 rip_ctloutput(struct socket *so, struct sockopt *sopt)
  440 {
  441         struct  inpcb *inp = sotoinpcb(so);
  442         int     error, optval;
  443 
  444         if (sopt->sopt_level != IPPROTO_IP) {
  445                 if ((sopt->sopt_level == SOL_SOCKET) &&
  446                     (sopt->sopt_name == SO_SETFIB)) {
  447                         inp->inp_inc.inc_fibnum = so->so_fibnum;
  448                         return (0);
  449                 }
  450                 return (EINVAL);
  451         }
  452 
  453         error = 0;
  454         switch (sopt->sopt_dir) {
  455         case SOPT_GET:
  456                 switch (sopt->sopt_name) {
  457                 case IP_HDRINCL:
  458                         optval = inp->inp_flags & INP_HDRINCL;
  459                         error = sooptcopyout(sopt, &optval, sizeof optval);
  460                         break;
  461 
  462                 case IP_FW_ADD: /* ADD actually returns the body... */
  463                 case IP_FW_GET:
  464                 case IP_FW_TABLE_GETSIZE:
  465                 case IP_FW_TABLE_LIST:
  466                 case IP_FW_NAT_GET_CONFIG:
  467                 case IP_FW_NAT_GET_LOG:
  468                         if (ip_fw_ctl_ptr != NULL)
  469                                 error = ip_fw_ctl_ptr(sopt);
  470                         else
  471                                 error = ENOPROTOOPT;
  472                         break;
  473 
  474                 case IP_DUMMYNET_GET:
  475                         if (ip_dn_ctl_ptr != NULL)
  476                                 error = ip_dn_ctl_ptr(sopt);
  477                         else
  478                                 error = ENOPROTOOPT;
  479                         break ;
  480 
  481                 case MRT_INIT:
  482                 case MRT_DONE:
  483                 case MRT_ADD_VIF:
  484                 case MRT_DEL_VIF:
  485                 case MRT_ADD_MFC:
  486                 case MRT_DEL_MFC:
  487                 case MRT_VERSION:
  488                 case MRT_ASSERT:
  489                 case MRT_API_SUPPORT:
  490                 case MRT_API_CONFIG:
  491                 case MRT_ADD_BW_UPCALL:
  492                 case MRT_DEL_BW_UPCALL:
  493                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
  494                         if (error != 0)
  495                                 return (error);
  496                         error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
  497                                 EOPNOTSUPP;
  498                         break;
  499 
  500                 default:
  501                         error = ip_ctloutput(so, sopt);
  502                         break;
  503                 }
  504                 break;
  505 
  506         case SOPT_SET:
  507                 switch (sopt->sopt_name) {
  508                 case IP_HDRINCL:
  509                         error = sooptcopyin(sopt, &optval, sizeof optval,
  510                                             sizeof optval);
  511                         if (error)
  512                                 break;
  513                         if (optval)
  514                                 inp->inp_flags |= INP_HDRINCL;
  515                         else
  516                                 inp->inp_flags &= ~INP_HDRINCL;
  517                         break;
  518 
  519                 case IP_FW_ADD:
  520                 case IP_FW_DEL:
  521                 case IP_FW_FLUSH:
  522                 case IP_FW_ZERO:
  523                 case IP_FW_RESETLOG:
  524                 case IP_FW_TABLE_ADD:
  525                 case IP_FW_TABLE_DEL:
  526                 case IP_FW_TABLE_FLUSH:
  527                 case IP_FW_NAT_CFG:
  528                 case IP_FW_NAT_DEL:
  529                         if (ip_fw_ctl_ptr != NULL)
  530                                 error = ip_fw_ctl_ptr(sopt);
  531                         else
  532                                 error = ENOPROTOOPT;
  533                         break;
  534 
  535                 case IP_DUMMYNET_CONFIGURE:
  536                 case IP_DUMMYNET_DEL:
  537                 case IP_DUMMYNET_FLUSH:
  538                         if (ip_dn_ctl_ptr != NULL)
  539                                 error = ip_dn_ctl_ptr(sopt);
  540                         else
  541                                 error = ENOPROTOOPT ;
  542                         break ;
  543 
  544                 case IP_RSVP_ON:
  545                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
  546                         if (error != 0)
  547                                 return (error);
  548                         error = ip_rsvp_init(so);
  549                         break;
  550 
  551                 case IP_RSVP_OFF:
  552                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
  553                         if (error != 0)
  554                                 return (error);
  555                         error = ip_rsvp_done();
  556                         break;
  557 
  558                 case IP_RSVP_VIF_ON:
  559                 case IP_RSVP_VIF_OFF:
  560                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
  561                         if (error != 0)
  562                                 return (error);
  563                         error = ip_rsvp_vif ?
  564                                 ip_rsvp_vif(so, sopt) : EINVAL;
  565                         break;
  566 
  567                 case MRT_INIT:
  568                 case MRT_DONE:
  569                 case MRT_ADD_VIF:
  570                 case MRT_DEL_VIF:
  571                 case MRT_ADD_MFC:
  572                 case MRT_DEL_MFC:
  573                 case MRT_VERSION:
  574                 case MRT_ASSERT:
  575                 case MRT_API_SUPPORT:
  576                 case MRT_API_CONFIG:
  577                 case MRT_ADD_BW_UPCALL:
  578                 case MRT_DEL_BW_UPCALL:
  579                         error = priv_check(curthread, PRIV_NETINET_MROUTE);
  580                         if (error != 0)
  581                                 return (error);
  582                         error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
  583                                         EOPNOTSUPP;
  584                         break;
  585 
  586                 default:
  587                         error = ip_ctloutput(so, sopt);
  588                         break;
  589                 }
  590                 break;
  591         }
  592 
  593         return (error);
  594 }
  595 
  596 /*
  597  * This function exists solely to receive the PRC_IFDOWN messages which are
  598  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
  599  * in_ifadown() to remove all routes corresponding to that address.  It also
  600  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
  601  * routes.
  602  */
  603 void
  604 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
  605 {
  606         struct in_ifaddr *ia;
  607         struct ifnet *ifp;
  608         int err;
  609         int flags;
  610 
  611         switch (cmd) {
  612         case PRC_IFDOWN:
  613                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
  614                         if (ia->ia_ifa.ifa_addr == sa
  615                             && (ia->ia_flags & IFA_ROUTE)) {
  616                                 /*
  617                                  * in_ifscrub kills the interface route.
  618                                  */
  619                                 in_ifscrub(ia->ia_ifp, ia);
  620                                 /*
  621                                  * in_ifadown gets rid of all the rest of the
  622                                  * routes.  This is not quite the right thing
  623                                  * to do, but at least if we are running a
  624                                  * routing process they will come back.
  625                                  */
  626                                 in_ifadown(&ia->ia_ifa, 0);
  627                                 break;
  628                         }
  629                 }
  630                 break;
  631 
  632         case PRC_IFUP:
  633                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
  634                         if (ia->ia_ifa.ifa_addr == sa)
  635                                 break;
  636                 }
  637                 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
  638                         return;
  639                 flags = RTF_UP;
  640                 ifp = ia->ia_ifa.ifa_ifp;
  641 
  642                 if ((ifp->if_flags & IFF_LOOPBACK)
  643                     || (ifp->if_flags & IFF_POINTOPOINT))
  644                         flags |= RTF_HOST;
  645 
  646                 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
  647                 if (err == 0)
  648                         ia->ia_flags |= IFA_ROUTE;
  649                 break;
  650         }
  651 }
  652 
  653 u_long  rip_sendspace = 9216;
  654 u_long  rip_recvspace = 9216;
  655 
  656 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
  657     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
  658 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
  659     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
  660 
  661 static int
  662 rip_attach(struct socket *so, int proto, struct thread *td)
  663 {
  664         struct inpcb *inp;
  665         int error;
  666 
  667         inp = sotoinpcb(so);
  668         KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
  669 
  670         error = priv_check(td, PRIV_NETINET_RAW);
  671         if (error)
  672                 return (error);
  673         if (proto >= IPPROTO_MAX || proto < 0)
  674                 return EPROTONOSUPPORT;
  675         error = soreserve(so, rip_sendspace, rip_recvspace);
  676         if (error)
  677                 return (error);
  678         INP_INFO_WLOCK(&ripcbinfo);
  679         error = in_pcballoc(so, &ripcbinfo);
  680         if (error) {
  681                 INP_INFO_WUNLOCK(&ripcbinfo);
  682                 return (error);
  683         }
  684         inp = (struct inpcb *)so->so_pcb;
  685         inp->inp_vflag |= INP_IPV4;
  686         inp->inp_ip_p = proto;
  687         inp->inp_ip_ttl = ip_defttl;
  688         rip_inshash(inp);
  689         INP_INFO_WUNLOCK(&ripcbinfo);
  690         INP_WUNLOCK(inp);
  691         return (0);
  692 }
  693 
  694 static void
  695 rip_detach(struct socket *so)
  696 {
  697         struct inpcb *inp;
  698 
  699         inp = sotoinpcb(so);
  700         KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
  701         KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 
  702             ("rip_detach: not closed"));
  703 
  704         INP_INFO_WLOCK(&ripcbinfo);
  705         INP_WLOCK(inp);
  706         rip_delhash(inp);
  707         if (so == ip_mrouter && ip_mrouter_done)
  708                 ip_mrouter_done();
  709         if (ip_rsvp_force_done)
  710                 ip_rsvp_force_done(so);
  711         if (so == ip_rsvpd)
  712                 ip_rsvp_done();
  713         in_pcbdetach(inp);
  714         in_pcbfree(inp);
  715         INP_INFO_WUNLOCK(&ripcbinfo);
  716 }
  717 
  718 static void
  719 rip_dodisconnect(struct socket *so, struct inpcb *inp)
  720 {
  721 
  722         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
  723         INP_WLOCK_ASSERT(inp);
  724 
  725         rip_delhash(inp);
  726         inp->inp_faddr.s_addr = INADDR_ANY;
  727         rip_inshash(inp);
  728         SOCK_LOCK(so);
  729         so->so_state &= ~SS_ISCONNECTED;
  730         SOCK_UNLOCK(so);
  731 }
  732 
  733 static void
  734 rip_abort(struct socket *so)
  735 {
  736         struct inpcb *inp;
  737 
  738         inp = sotoinpcb(so);
  739         KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
  740 
  741         INP_INFO_WLOCK(&ripcbinfo);
  742         INP_WLOCK(inp);
  743         rip_dodisconnect(so, inp);
  744         INP_WUNLOCK(inp);
  745         INP_INFO_WUNLOCK(&ripcbinfo);
  746 }
  747 
  748 static void
  749 rip_close(struct socket *so)
  750 {
  751         struct inpcb *inp;
  752 
  753         inp = sotoinpcb(so);
  754         KASSERT(inp != NULL, ("rip_close: inp == NULL"));
  755 
  756         INP_INFO_WLOCK(&ripcbinfo);
  757         INP_WLOCK(inp);
  758         rip_dodisconnect(so, inp);
  759         INP_WUNLOCK(inp);
  760         INP_INFO_WUNLOCK(&ripcbinfo);
  761 }
  762 
  763 static int
  764 rip_disconnect(struct socket *so)
  765 {
  766         struct inpcb *inp;
  767 
  768         if ((so->so_state & SS_ISCONNECTED) == 0)
  769                 return (ENOTCONN);
  770 
  771         inp = sotoinpcb(so);
  772         KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
  773 
  774         INP_INFO_WLOCK(&ripcbinfo);
  775         INP_WLOCK(inp);
  776         rip_dodisconnect(so, inp);
  777         INP_WUNLOCK(inp);
  778         INP_INFO_WUNLOCK(&ripcbinfo);
  779         return (0);
  780 }
  781 
  782 static int
  783 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  784 {
  785         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
  786         struct inpcb *inp;
  787 
  788         if (nam->sa_len != sizeof(*addr))
  789                 return (EINVAL);
  790 
  791         if (jailed(td->td_ucred)) {
  792                 if (addr->sin_addr.s_addr == INADDR_ANY)
  793                         addr->sin_addr.s_addr =
  794                             htonl(prison_getip(td->td_ucred));
  795                 if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr)
  796                         return (EADDRNOTAVAIL);
  797         }
  798 
  799         if (TAILQ_EMPTY(&ifnet) ||
  800             (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
  801             (addr->sin_addr.s_addr &&
  802              ifa_ifwithaddr((struct sockaddr *)addr) == 0))
  803                 return (EADDRNOTAVAIL);
  804 
  805         inp = sotoinpcb(so);
  806         KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
  807 
  808         INP_INFO_WLOCK(&ripcbinfo);
  809         INP_WLOCK(inp);
  810         rip_delhash(inp);
  811         inp->inp_laddr = addr->sin_addr;
  812         rip_inshash(inp);
  813         INP_WUNLOCK(inp);
  814         INP_INFO_WUNLOCK(&ripcbinfo);
  815         return (0);
  816 }
  817 
  818 static int
  819 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  820 {
  821         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
  822         struct inpcb *inp;
  823 
  824         if (nam->sa_len != sizeof(*addr))
  825                 return (EINVAL);
  826         if (TAILQ_EMPTY(&ifnet))
  827                 return (EADDRNOTAVAIL);
  828         if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
  829                 return (EAFNOSUPPORT);
  830 
  831         inp = sotoinpcb(so);
  832         KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
  833 
  834         INP_INFO_WLOCK(&ripcbinfo);
  835         INP_WLOCK(inp);
  836         rip_delhash(inp);
  837         inp->inp_faddr = addr->sin_addr;
  838         rip_inshash(inp);
  839         soisconnected(so);
  840         INP_WUNLOCK(inp);
  841         INP_INFO_WUNLOCK(&ripcbinfo);
  842         return (0);
  843 }
  844 
  845 static int
  846 rip_shutdown(struct socket *so)
  847 {
  848         struct inpcb *inp;
  849 
  850         inp = sotoinpcb(so);
  851         KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
  852 
  853         INP_WLOCK(inp);
  854         socantsendmore(so);
  855         INP_WUNLOCK(inp);
  856         return (0);
  857 }
  858 
  859 static int
  860 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
  861     struct mbuf *control, struct thread *td)
  862 {
  863         struct inpcb *inp;
  864         u_long dst;
  865 
  866         inp = sotoinpcb(so);
  867         KASSERT(inp != NULL, ("rip_send: inp == NULL"));
  868 
  869         /*
  870          * Note: 'dst' reads below are unlocked.
  871          */
  872         if (so->so_state & SS_ISCONNECTED) {
  873                 if (nam) {
  874                         m_freem(m);
  875                         return (EISCONN);
  876                 }
  877                 dst = inp->inp_faddr.s_addr;    /* Unlocked read. */
  878         } else {
  879                 if (nam == NULL) {
  880                         m_freem(m);
  881                         return (ENOTCONN);
  882                 }
  883                 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
  884         }
  885         return (rip_output(m, so, dst));
  886 }
  887 
  888 static int
  889 rip_pcblist(SYSCTL_HANDLER_ARGS)
  890 {
  891         int error, i, n;
  892         struct inpcb *inp, **inp_list;
  893         inp_gen_t gencnt;
  894         struct xinpgen xig;
  895 
  896         /*
  897          * The process of preparing the TCB list is too time-consuming and
  898          * resource-intensive to repeat twice on every request.
  899          */
  900         if (req->oldptr == 0) {
  901                 n = ripcbinfo.ipi_count;
  902                 req->oldidx = 2 * (sizeof xig)
  903                     + (n + n/8) * sizeof(struct xinpcb);
  904                 return (0);
  905         }
  906 
  907         if (req->newptr != 0)
  908                 return (EPERM);
  909 
  910         /*
  911          * OK, now we're committed to doing something.
  912          */
  913         INP_INFO_RLOCK(&ripcbinfo);
  914         gencnt = ripcbinfo.ipi_gencnt;
  915         n = ripcbinfo.ipi_count;
  916         INP_INFO_RUNLOCK(&ripcbinfo);
  917 
  918         xig.xig_len = sizeof xig;
  919         xig.xig_count = n;
  920         xig.xig_gen = gencnt;
  921         xig.xig_sogen = so_gencnt;
  922         error = SYSCTL_OUT(req, &xig, sizeof xig);
  923         if (error)
  924                 return (error);
  925 
  926         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
  927         if (inp_list == 0)
  928                 return (ENOMEM);
  929 
  930         INP_INFO_RLOCK(&ripcbinfo);
  931         for (inp = LIST_FIRST(ripcbinfo.ipi_listhead), i = 0; inp && i < n;
  932              inp = LIST_NEXT(inp, inp_list)) {
  933                 INP_RLOCK(inp);
  934                 if (inp->inp_gencnt <= gencnt &&
  935                     cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
  936                         /* XXX held references? */
  937                         inp_list[i++] = inp;
  938                 }
  939                 INP_RUNLOCK(inp);
  940         }
  941         INP_INFO_RUNLOCK(&ripcbinfo);
  942         n = i;
  943 
  944         error = 0;
  945         for (i = 0; i < n; i++) {
  946                 inp = inp_list[i];
  947                 INP_RLOCK(inp);
  948                 if (inp->inp_gencnt <= gencnt) {
  949                         struct xinpcb xi;
  950                         bzero(&xi, sizeof(xi));
  951                         xi.xi_len = sizeof xi;
  952                         /* XXX should avoid extra copy */
  953                         bcopy(inp, &xi.xi_inp, sizeof *inp);
  954                         if (inp->inp_socket)
  955                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
  956                         INP_RUNLOCK(inp);
  957                         error = SYSCTL_OUT(req, &xi, sizeof xi);
  958                 } else
  959                         INP_RUNLOCK(inp);
  960         }
  961         if (!error) {
  962                 /*
  963                  * Give the user an updated idea of our state.  If the
  964                  * generation differs from what we told her before, she knows
  965                  * that something happened while we were processing this
  966                  * request, and it might be necessary to retry.
  967                  */
  968                 INP_INFO_RLOCK(&ripcbinfo);
  969                 xig.xig_gen = ripcbinfo.ipi_gencnt;
  970                 xig.xig_sogen = so_gencnt;
  971                 xig.xig_count = ripcbinfo.ipi_count;
  972                 INP_INFO_RUNLOCK(&ripcbinfo);
  973                 error = SYSCTL_OUT(req, &xig, sizeof xig);
  974         }
  975         free(inp_list, M_TEMP);
  976         return (error);
  977 }
  978 
  979 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
  980     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
  981 
  982 struct pr_usrreqs rip_usrreqs = {
  983         .pru_abort =            rip_abort,
  984         .pru_attach =           rip_attach,
  985         .pru_bind =             rip_bind,
  986         .pru_connect =          rip_connect,
  987         .pru_control =          in_control,
  988         .pru_detach =           rip_detach,
  989         .pru_disconnect =       rip_disconnect,
  990         .pru_peeraddr =         in_getpeeraddr,
  991         .pru_send =             rip_send,
  992         .pru_shutdown =         rip_shutdown,
  993         .pru_sockaddr =         in_getsockaddr,
  994         .pru_sosetlabel =       in_pcbsosetlabel,
  995         .pru_close =            rip_close,
  996 };

Cache object: 38437977e184a1c3bad1bc0d02e92419


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