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

Cache object: f2ced03e7b87402a9cc01e707e0b572e


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