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  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      @(#)raw_ip.c    8.7 (Berkeley) 5/15/95
   34  * $FreeBSD$
   35  */
   36 
   37 #include "opt_inet6.h"
   38 #include "opt_ipsec.h"
   39 #include "opt_random_ip_id.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/kernel.h>
   44 #include <sys/malloc.h>
   45 #include <sys/mbuf.h>
   46 #include <sys/proc.h>
   47 #include <sys/protosw.h>
   48 #include <sys/socket.h>
   49 #include <sys/socketvar.h>
   50 #include <sys/sysctl.h>
   51 
   52 #include <vm/vm_zone.h>
   53 
   54 #include <net/if.h>
   55 #include <net/route.h>
   56 
   57 #define _IP_VHL
   58 #include <netinet/in.h>
   59 #include <netinet/in_systm.h>
   60 #include <netinet/ip.h>
   61 #include <netinet/in_pcb.h>
   62 #include <netinet/in_var.h>
   63 #include <netinet/ip_var.h>
   64 #include <netinet/ip_mroute.h>
   65 
   66 #include <netinet/ip_fw.h>
   67 #include <netinet/ip_dummynet.h>
   68 
   69 #ifdef FAST_IPSEC
   70 #include <netipsec/ipsec.h>
   71 #endif /*FAST_IPSEC*/
   72 
   73 #ifdef IPSEC
   74 #include <netinet6/ipsec.h>
   75 #endif /*IPSEC*/
   76 
   77 struct  inpcbhead ripcb;
   78 struct  inpcbinfo ripcbinfo;
   79 
   80 /* control hooks for ipfw and dummynet */
   81 ip_fw_ctl_t *ip_fw_ctl_ptr;
   82 ip_dn_ctl_t *ip_dn_ctl_ptr;
   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, int proto);
  103 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
  104 void (*ip_rsvp_force_done)(struct socket *);
  105 
  106 /*
  107  * Nominal space allocated to a raw ip socket.
  108  */
  109 #define RIPSNDQ         8192
  110 #define RIPRCVQ         8192
  111 
  112 /*
  113  * Raw interface to IP protocol.
  114  */
  115 
  116 /*
  117  * Initialize raw connection block queue.
  118  */
  119 void
  120 rip_init(void)
  121 {
  122         LIST_INIT(&ripcb);
  123         ripcbinfo.listhead = &ripcb;
  124         /*
  125          * XXX We don't use the hash list for raw IP, but it's easier
  126          * to allocate a one entry hash list than it is to check all
  127          * over the place for hashbase == NULL.
  128          */
  129         ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
  130         ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
  131         ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
  132                                    maxsockets, ZONE_INTERRUPT, 0);
  133 }
  134 
  135 /*
  136  * XXX ripsrc is modified in rip_input, so we must be fix this
  137  * when we want to make this code smp-friendly.
  138  */
  139 static struct   sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
  140 
  141 /*
  142  * Setup generic address and protocol structures
  143  * for raw_input routine, then pass them along with
  144  * mbuf chain.
  145  */
  146 void
  147 rip_input(struct mbuf *m, int off, int proto)
  148 {
  149         struct ip *ip = mtod(m, struct ip *);
  150         struct inpcb *inp;
  151         struct inpcb *last = NULL;
  152         struct mbuf *opts = NULL;
  153 
  154         ripsrc.sin_addr = ip->ip_src;
  155         LIST_FOREACH(inp, &ripcb, inp_list) {
  156 #ifdef INET6
  157                 if ((inp->inp_vflag & INP_IPV4) == 0)
  158                         continue;
  159 #endif
  160                 if (inp->inp_ip_p && inp->inp_ip_p != proto)
  161                         continue;
  162                 if (inp->inp_laddr.s_addr != INADDR_ANY &&
  163                     inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
  164                         continue;
  165                 if (inp->inp_faddr.s_addr != INADDR_ANY &&
  166                     inp->inp_faddr.s_addr != ip->ip_src.s_addr)
  167                         continue;
  168                 if (last) {
  169                         struct mbuf *n = m_copypacket(m, M_DONTWAIT);
  170 
  171 #ifdef IPSEC
  172                         /* check AH/ESP integrity. */
  173                         if (n && ipsec4_in_reject_so(n, last->inp_socket)) {
  174                                 m_freem(n);
  175                                 ipsecstat.in_polvio++;
  176                                 /* do not inject data to pcb */
  177                         } else
  178 #endif /*IPSEC*/
  179 #ifdef FAST_IPSEC
  180                         /* check AH/ESP integrity. */
  181                         if (ipsec4_in_reject(n, last)) {
  182                                 m_freem(n);
  183                                 /* do not inject data to pcb */
  184                         } else
  185 #endif /*FAST_IPSEC*/
  186                         if (n) {
  187                                 if (last->inp_flags & INP_CONTROLOPTS ||
  188                                     last->inp_socket->so_options & SO_TIMESTAMP)
  189                                     ip_savecontrol(last, &opts, ip, n);
  190                                 if (sbappendaddr(&last->inp_socket->so_rcv,
  191                                     (struct sockaddr *)&ripsrc, n,
  192                                     opts) == 0) {
  193                                         /* should notify about lost packet */
  194                                         m_freem(n);
  195                                         if (opts)
  196                                             m_freem(opts);
  197                                 } else
  198                                         sorwakeup(last->inp_socket);
  199                                 opts = 0;
  200                         }
  201                 }
  202                 last = inp;
  203         }
  204 #ifdef IPSEC
  205         /* check AH/ESP integrity. */
  206         if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
  207                 m_freem(m);
  208                 ipsecstat.in_polvio++;
  209                 ipstat.ips_delivered--;
  210                 /* do not inject data to pcb */
  211         } else
  212 #endif /*IPSEC*/
  213 #ifdef FAST_IPSEC
  214         /* check AH/ESP integrity. */
  215         if (last && ipsec4_in_reject(m, last)) {
  216                 m_freem(m);
  217                 ipstat.ips_delivered--;
  218                 /* do not inject data to pcb */
  219         } else
  220 #endif /*FAST_IPSEC*/
  221         if (last) {
  222                 if (last->inp_flags & INP_CONTROLOPTS ||
  223                     last->inp_socket->so_options & SO_TIMESTAMP)
  224                         ip_savecontrol(last, &opts, ip, m);
  225                 if (sbappendaddr(&last->inp_socket->so_rcv,
  226                     (struct sockaddr *)&ripsrc, m, opts) == 0) {
  227                         m_freem(m);
  228                         if (opts)
  229                             m_freem(opts);
  230                 } else
  231                         sorwakeup(last->inp_socket);
  232         } else {
  233                 m_freem(m);
  234                 ipstat.ips_noproto++;
  235                 ipstat.ips_delivered--;
  236         }
  237 }
  238 
  239 /*
  240  * Generate IP header and pass packet to ip_output.
  241  * Tack on options user may have setup with control call.
  242  */
  243 int
  244 rip_output(struct mbuf *m, struct socket *so, u_long dst)
  245 {
  246         struct ip *ip;
  247         struct inpcb *inp = sotoinpcb(so);
  248         int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
  249 
  250         /*
  251          * If the user handed us a complete IP packet, use it.
  252          * Otherwise, allocate an mbuf for a header and fill it in.
  253          */
  254         if ((inp->inp_flags & INP_HDRINCL) == 0) {
  255                 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
  256                         m_freem(m);
  257                         return(EMSGSIZE);
  258                 }
  259                 M_PREPEND(m, sizeof(struct ip), M_WAIT);
  260                 if (m == NULL)
  261                         return(ENOBUFS);
  262                 ip = mtod(m, struct ip *);
  263                 ip->ip_tos = inp->inp_ip_tos;
  264                 ip->ip_off = 0;
  265                 ip->ip_p = inp->inp_ip_p;
  266                 ip->ip_len = m->m_pkthdr.len;
  267                 ip->ip_src = inp->inp_laddr;
  268                 ip->ip_dst.s_addr = dst;
  269                 ip->ip_ttl = inp->inp_ip_ttl;
  270         } else {
  271                 if (m->m_pkthdr.len > IP_MAXPACKET) {
  272                         m_freem(m);
  273                         return(EMSGSIZE);
  274                 }
  275                 ip = mtod(m, struct ip *);
  276                 /* don't allow both user specified and setsockopt options,
  277                    and don't allow packet length sizes that will crash */
  278                 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
  279                      && inp->inp_options)
  280                     || (ip->ip_len > m->m_pkthdr.len)
  281                     || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
  282                         m_freem(m);
  283                         return EINVAL;
  284                 }
  285                 if (ip->ip_id == 0)
  286 #ifdef RANDOM_IP_ID
  287                         ip->ip_id = ip_randomid();
  288 #else
  289                         ip->ip_id = htons(ip_id++);
  290 #endif
  291                 /* XXX prevent ip_output from overwriting header fields */
  292                 flags |= IP_RAWOUTPUT;
  293                 ipstat.ips_rawout++;
  294         }
  295 
  296         if (inp->inp_flags & INP_ONESBCAST)
  297                 flags |= IP_SENDONES;
  298 
  299         return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
  300                           inp->inp_moptions, inp));
  301 }
  302 
  303 /*
  304  * Raw IP socket option processing.
  305  */
  306 int
  307 rip_ctloutput(struct socket *so, struct sockopt *sopt)
  308 {
  309         struct  inpcb *inp = sotoinpcb(so);
  310         int     error, optval;
  311 
  312         if (sopt->sopt_level != IPPROTO_IP)
  313                 return (EINVAL);
  314 
  315         error = 0;
  316 
  317         switch (sopt->sopt_dir) {
  318         case SOPT_GET:
  319                 switch (sopt->sopt_name) {
  320                 case IP_HDRINCL:
  321                         optval = inp->inp_flags & INP_HDRINCL;
  322                         error = sooptcopyout(sopt, &optval, sizeof optval);
  323                         break;
  324 
  325                 case IP_FW_ADD: /* ADD actually returns the body... */
  326                 case IP_FW_GET:
  327                 case IP_FW_TABLE_GETSIZE:
  328                 case IP_FW_TABLE_LIST:
  329                         if (IPFW_LOADED)
  330                                 error = ip_fw_ctl_ptr(sopt);
  331                         else
  332                                 error = ENOPROTOOPT;
  333                         break;
  334 
  335                 case IP_DUMMYNET_GET:
  336                         if (DUMMYNET_LOADED)
  337                                 error = ip_dn_ctl_ptr(sopt);
  338                         else
  339                                 error = ENOPROTOOPT;
  340                         break ;
  341 
  342                 case MRT_INIT:
  343                 case MRT_DONE:
  344                 case MRT_ADD_VIF:
  345                 case MRT_DEL_VIF:
  346                 case MRT_ADD_MFC:
  347                 case MRT_DEL_MFC:
  348                 case MRT_VERSION:
  349                 case MRT_ASSERT:
  350                 case MRT_API_SUPPORT:
  351                 case MRT_API_CONFIG:
  352                 case MRT_ADD_BW_UPCALL:
  353                 case MRT_DEL_BW_UPCALL:
  354                         error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
  355                                 EOPNOTSUPP;
  356                         break;
  357 
  358                 default:
  359                         error = ip_ctloutput(so, sopt);
  360                         break;
  361                 }
  362                 break;
  363 
  364         case SOPT_SET:
  365                 switch (sopt->sopt_name) {
  366                 case IP_HDRINCL:
  367                         error = sooptcopyin(sopt, &optval, sizeof optval,
  368                                             sizeof optval);
  369                         if (error)
  370                                 break;
  371                         if (optval)
  372                                 inp->inp_flags |= INP_HDRINCL;
  373                         else
  374                                 inp->inp_flags &= ~INP_HDRINCL;
  375                         break;
  376 
  377                 case IP_FW_ADD:
  378                 case IP_FW_DEL:
  379                 case IP_FW_FLUSH:
  380                 case IP_FW_ZERO:
  381                 case IP_FW_RESETLOG:
  382                 case IP_FW_TABLE_ADD:
  383                 case IP_FW_TABLE_DEL:
  384                 case IP_FW_TABLE_FLUSH:
  385                         if (IPFW_LOADED)
  386                                 error = ip_fw_ctl_ptr(sopt);
  387                         else
  388                                 error = ENOPROTOOPT;
  389                         break;
  390 
  391                 case IP_DUMMYNET_CONFIGURE:
  392                 case IP_DUMMYNET_DEL:
  393                 case IP_DUMMYNET_FLUSH:
  394                         if (DUMMYNET_LOADED)
  395                                 error = ip_dn_ctl_ptr(sopt);
  396                         else
  397                                 error = ENOPROTOOPT ;
  398                         break ;
  399 
  400                 case IP_RSVP_ON:
  401                         error = ip_rsvp_init(so);
  402                         break;
  403 
  404                 case IP_RSVP_OFF:
  405                         error = ip_rsvp_done();
  406                         break;
  407 
  408                 case IP_RSVP_VIF_ON:
  409                 case IP_RSVP_VIF_OFF:
  410                         error = ip_rsvp_vif ?
  411                                 ip_rsvp_vif(so, sopt) : EINVAL;
  412                         break;
  413 
  414                 case MRT_INIT:
  415                 case MRT_DONE:
  416                 case MRT_ADD_VIF:
  417                 case MRT_DEL_VIF:
  418                 case MRT_ADD_MFC:
  419                 case MRT_DEL_MFC:
  420                 case MRT_VERSION:
  421                 case MRT_ASSERT:
  422                 case MRT_API_SUPPORT:
  423                 case MRT_API_CONFIG:
  424                 case MRT_ADD_BW_UPCALL:
  425                 case MRT_DEL_BW_UPCALL:
  426                         error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
  427                                         EOPNOTSUPP;
  428                         break;
  429 
  430                 default:
  431                         error = ip_ctloutput(so, sopt);
  432                         break;
  433                 }
  434                 break;
  435         }
  436 
  437         return (error);
  438 }
  439 
  440 /*
  441  * This function exists solely to receive the PRC_IFDOWN messages which
  442  * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
  443  * and calls in_ifadown() to remove all routes corresponding to that address.
  444  * It also receives the PRC_IFUP messages from if_up() and reinstalls the
  445  * interface routes.
  446  */
  447 void
  448 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
  449 {
  450         struct in_ifaddr *ia;
  451         struct ifnet *ifp;
  452         int err;
  453         int flags;
  454 
  455         switch (cmd) {
  456         case PRC_IFDOWN:
  457                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
  458                         if (ia->ia_ifa.ifa_addr == sa
  459                             && (ia->ia_flags & IFA_ROUTE)) {
  460                                 /*
  461                                  * in_ifscrub kills the interface route.
  462                                  */
  463                                 in_ifscrub(ia->ia_ifp, ia);
  464                                 /*
  465                                  * in_ifadown gets rid of all the rest of
  466                                  * the routes.  This is not quite the right
  467                                  * thing to do, but at least if we are running
  468                                  * a routing process they will come back.
  469                                  */
  470                                 in_ifadown(&ia->ia_ifa, 0);
  471                                 break;
  472                         }
  473                 }
  474                 break;
  475 
  476         case PRC_IFUP:
  477                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
  478                         if (ia->ia_ifa.ifa_addr == sa)
  479                                 break;
  480                 }
  481                 if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
  482                         return;
  483                 flags = RTF_UP;
  484                 ifp = ia->ia_ifa.ifa_ifp;
  485 
  486                 if ((ifp->if_flags & IFF_LOOPBACK)
  487                     || (ifp->if_flags & IFF_POINTOPOINT))
  488                         flags |= RTF_HOST;
  489 
  490                 err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
  491                 if (err == 0)
  492                         ia->ia_flags |= IFA_ROUTE;
  493                 break;
  494         }
  495 }
  496 
  497 u_long  rip_sendspace = RIPSNDQ;
  498 u_long  rip_recvspace = RIPRCVQ;
  499 
  500 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
  501     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
  502 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
  503     &rip_recvspace, 0, "Maximum incoming raw IP datagram size");
  504 
  505 static int
  506 rip_attach(struct socket *so, int proto, struct proc *p)
  507 {
  508         struct inpcb *inp;
  509         int error, s;
  510 
  511         inp = sotoinpcb(so);
  512         if (inp)
  513                 panic("rip_attach");
  514         if (p && (error = suser(p)) != 0)
  515                 return error;
  516 
  517         error = soreserve(so, rip_sendspace, rip_recvspace);
  518         if (error)
  519                 return error;
  520         s = splnet();
  521         error = in_pcballoc(so, &ripcbinfo, p);
  522         splx(s);
  523         if (error)
  524                 return error;
  525         inp = (struct inpcb *)so->so_pcb;
  526         inp->inp_vflag |= INP_IPV4;
  527         inp->inp_ip_p = proto;
  528         inp->inp_ip_ttl = ip_defttl;
  529         return 0;
  530 }
  531 
  532 static int
  533 rip_detach(struct socket *so)
  534 {
  535         struct inpcb *inp;
  536 
  537         inp = sotoinpcb(so);
  538         if (inp == 0)
  539                 panic("rip_detach");
  540         if (so == ip_mrouter && ip_mrouter_done)
  541                 ip_mrouter_done();
  542         if (ip_rsvp_force_done)
  543                 ip_rsvp_force_done(so);
  544         if (so == ip_rsvpd)
  545                 ip_rsvp_done();
  546         in_pcbdetach(inp);
  547         return 0;
  548 }
  549 
  550 static int
  551 rip_abort(struct socket *so)
  552 {
  553         soisdisconnected(so);
  554         if (so->so_state & SS_NOFDREF)
  555                 return rip_detach(so);
  556         return 0;
  557 }
  558 
  559 static int
  560 rip_disconnect(struct socket *so)
  561 {
  562         if ((so->so_state & SS_ISCONNECTED) == 0)
  563                 return ENOTCONN;
  564         return rip_abort(so);
  565 }
  566 
  567 static int
  568 rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
  569 {
  570         struct inpcb *inp = sotoinpcb(so);
  571         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
  572 
  573         if (nam->sa_len != sizeof(*addr))
  574                 return EINVAL;
  575 
  576         if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
  577                                     (addr->sin_family != AF_IMPLINK)) ||
  578             (addr->sin_addr.s_addr != INADDR_ANY &&
  579              ifa_ifwithaddr((struct sockaddr *)addr) == 0))
  580                 return EADDRNOTAVAIL;
  581         inp->inp_laddr = addr->sin_addr;
  582         return 0;
  583 }
  584 
  585 static int
  586 rip_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
  587 {
  588         struct inpcb *inp = sotoinpcb(so);
  589         struct sockaddr_in *addr = (struct sockaddr_in *)nam;
  590 
  591         if (nam->sa_len != sizeof(*addr))
  592                 return EINVAL;
  593         if (TAILQ_EMPTY(&ifnet))
  594                 return EADDRNOTAVAIL;
  595         if ((addr->sin_family != AF_INET) &&
  596             (addr->sin_family != AF_IMPLINK))
  597                 return EAFNOSUPPORT;
  598         inp->inp_faddr = addr->sin_addr;
  599         soisconnected(so);
  600         return 0;
  601 }
  602 
  603 static int
  604 rip_shutdown(struct socket *so)
  605 {
  606         socantsendmore(so);
  607         return 0;
  608 }
  609 
  610 static int
  611 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
  612          struct mbuf *control, struct proc *p)
  613 {
  614         struct inpcb *inp = sotoinpcb(so);
  615         u_long dst;
  616 
  617         if (so->so_state & SS_ISCONNECTED) {
  618                 if (nam) {
  619                         m_freem(m);
  620                         return EISCONN;
  621                 }
  622                 dst = inp->inp_faddr.s_addr;
  623         } else {
  624                 if (nam == NULL) {
  625                         m_freem(m);
  626                         return ENOTCONN;
  627                 }
  628                 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
  629         }
  630         return rip_output(m, so, dst);
  631 }
  632 
  633 static int
  634 rip_pcblist(SYSCTL_HANDLER_ARGS)
  635 {
  636         int error, i, n, s;
  637         struct inpcb *inp, **inp_list;
  638         inp_gen_t gencnt;
  639         struct xinpgen xig;
  640 
  641         /*
  642          * The process of preparing the TCB list is too time-consuming and
  643          * resource-intensive to repeat twice on every request.
  644          */
  645         if (req->oldptr == 0) {
  646                 n = ripcbinfo.ipi_count;
  647                 req->oldidx = 2 * (sizeof xig)
  648                         + (n + n/8) * sizeof(struct xinpcb);
  649                 return 0;
  650         }
  651 
  652         if (req->newptr != 0)
  653                 return EPERM;
  654 
  655         /*
  656          * OK, now we're committed to doing something.
  657          */
  658         s = splnet();
  659         gencnt = ripcbinfo.ipi_gencnt;
  660         n = ripcbinfo.ipi_count;
  661         splx(s);
  662 
  663         xig.xig_len = sizeof xig;
  664         xig.xig_count = n;
  665         xig.xig_gen = gencnt;
  666         xig.xig_sogen = so_gencnt;
  667         error = SYSCTL_OUT(req, &xig, sizeof xig);
  668         if (error)
  669                 return error;
  670 
  671         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
  672         if (inp_list == 0)
  673                 return ENOMEM;
  674         
  675         s = splnet();
  676         for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n;
  677              inp = LIST_NEXT(inp, inp_list)) {
  678                 if (inp->inp_gencnt <= gencnt)
  679                         inp_list[i++] = inp;
  680         }
  681         splx(s);
  682         n = i;
  683 
  684         error = 0;
  685         for (i = 0; i < n; i++) {
  686                 inp = inp_list[i];
  687                 if (inp->inp_gencnt <= gencnt) {
  688                         struct xinpcb xi;
  689                         bzero(&xi, sizeof(xi));
  690                         xi.xi_len = sizeof xi;
  691                         /* XXX should avoid extra copy */
  692                         bcopy(inp, &xi.xi_inp, sizeof *inp);
  693                         if (inp->inp_socket)
  694                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
  695                         error = SYSCTL_OUT(req, &xi, sizeof xi);
  696                 }
  697         }
  698         if (!error) {
  699                 /*
  700                  * Give the user an updated idea of our state.
  701                  * If the generation differs from what we told
  702                  * her before, she knows that something happened
  703                  * while we were processing this request, and it
  704                  * might be necessary to retry.
  705                  */
  706                 s = splnet();
  707                 xig.xig_gen = ripcbinfo.ipi_gencnt;
  708                 xig.xig_sogen = so_gencnt;
  709                 xig.xig_count = ripcbinfo.ipi_count;
  710                 splx(s);
  711                 error = SYSCTL_OUT(req, &xig, sizeof xig);
  712         }
  713         free(inp_list, M_TEMP);
  714         return error;
  715 }
  716 
  717 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
  718             rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
  719 
  720 struct pr_usrreqs rip_usrreqs = {
  721         rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
  722         pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
  723         pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
  724         pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
  725         in_setsockaddr, sosend, soreceive, sopoll
  726 };

Cache object: ada01c392382a13303850e5e0393ed0d


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