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/udp_usrreq.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, 1990, 1993, 1995
    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  *      @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
   34  * $FreeBSD: releng/5.0/sys/netinet/udp_usrreq.c 107113 2002-11-20 19:00:54Z luigi $
   35  */
   36 
   37 #include "opt_ipsec.h"
   38 #include "opt_inet6.h"
   39 #include "opt_mac.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/domain.h>
   44 #include <sys/jail.h>
   45 #include <sys/kernel.h>
   46 #include <sys/lock.h>
   47 #include <sys/mac.h>
   48 #include <sys/malloc.h>
   49 #include <sys/mbuf.h>
   50 #include <sys/proc.h>
   51 #include <sys/protosw.h>
   52 #include <sys/signalvar.h>
   53 #include <sys/socket.h>
   54 #include <sys/socketvar.h>
   55 #include <sys/sx.h>
   56 #include <sys/sysctl.h>
   57 #include <sys/syslog.h>
   58 
   59 #include <vm/uma.h>
   60 
   61 #include <net/if.h>
   62 #include <net/route.h>
   63 
   64 #include <netinet/in.h>
   65 #include <netinet/in_systm.h>
   66 #include <netinet/in_pcb.h>
   67 #include <netinet/in_var.h>
   68 #include <netinet/ip.h>
   69 #ifdef INET6
   70 #include <netinet/ip6.h>
   71 #endif
   72 #include <netinet/ip_icmp.h>
   73 #include <netinet/icmp_var.h>
   74 #include <netinet/ip_var.h>
   75 #ifdef INET6
   76 #include <netinet6/ip6_var.h>
   77 #endif
   78 #include <netinet/udp.h>
   79 #include <netinet/udp_var.h>
   80 
   81 #ifdef FAST_IPSEC
   82 #include <netipsec/ipsec.h>
   83 #endif /*FAST_IPSEC*/
   84 
   85 #ifdef IPSEC
   86 #include <netinet6/ipsec.h>
   87 #endif /*IPSEC*/
   88 
   89 #include <machine/in_cksum.h>
   90 
   91 /*
   92  * UDP protocol implementation.
   93  * Per RFC 768, August, 1980.
   94  */
   95 #ifndef COMPAT_42
   96 static int      udpcksum = 1;
   97 #else
   98 static int      udpcksum = 0;           /* XXX */
   99 #endif
  100 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
  101                 &udpcksum, 0, "");
  102 
  103 int     log_in_vain = 0;
  104 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 
  105     &log_in_vain, 0, "Log all incoming UDP packets");
  106 
  107 static int      blackhole = 0;
  108 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
  109         &blackhole, 0, "Do not send port unreachables for refused connects");
  110 
  111 struct  inpcbhead udb;          /* from udp_var.h */
  112 #define udb6    udb  /* for KAME src sync over BSD*'s */
  113 struct  inpcbinfo udbinfo;
  114 
  115 #ifndef UDBHASHSIZE
  116 #define UDBHASHSIZE 16
  117 #endif
  118 
  119 struct  udpstat udpstat;        /* from udp_var.h */
  120 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
  121     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
  122 
  123 static struct   sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
  124 #ifdef INET6
  125 struct udp_in6 {
  126         struct sockaddr_in6     uin6_sin;
  127         u_char                  uin6_init_done : 1;
  128 } udp_in6 = {
  129         { sizeof(udp_in6.uin6_sin), AF_INET6 },
  130         0
  131 };
  132 struct udp_ip6 {
  133         struct ip6_hdr          uip6_ip6;
  134         u_char                  uip6_init_done : 1;
  135 } udp_ip6;
  136 #endif /* INET6 */
  137 
  138 static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
  139                 int off);
  140 #ifdef INET6
  141 static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
  142 #endif
  143 
  144 static int udp_detach(struct socket *so);
  145 static  int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
  146                 struct mbuf *, struct thread *);
  147 
  148 void
  149 udp_init()
  150 {
  151         INP_INFO_LOCK_INIT(&udbinfo, "udp");
  152         LIST_INIT(&udb);
  153         udbinfo.listhead = &udb;
  154         udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
  155         udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
  156                                         &udbinfo.porthashmask);
  157         udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
  158             NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  159         uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
  160 }
  161 
  162 void
  163 udp_input(m, off)
  164         register struct mbuf *m;
  165         int off;
  166 {
  167         int iphlen = off;
  168         register struct ip *ip;
  169         register struct udphdr *uh;
  170         register struct inpcb *inp;
  171         struct mbuf *opts = 0;
  172         int len;
  173         struct ip save_ip;
  174 
  175         udpstat.udps_ipackets++;
  176 
  177         /*
  178          * Strip IP options, if any; should skip this,
  179          * make available to user, and use on returned packets,
  180          * but we don't yet have a way to check the checksum
  181          * with options still present.
  182          */
  183         if (iphlen > sizeof (struct ip)) {
  184                 ip_stripoptions(m, (struct mbuf *)0);
  185                 iphlen = sizeof(struct ip);
  186         }
  187 
  188         /*
  189          * Get IP and UDP header together in first mbuf.
  190          */
  191         ip = mtod(m, struct ip *);
  192         if (m->m_len < iphlen + sizeof(struct udphdr)) {
  193                 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
  194                         udpstat.udps_hdrops++;
  195                         return;
  196                 }
  197                 ip = mtod(m, struct ip *);
  198         }
  199         uh = (struct udphdr *)((caddr_t)ip + iphlen);
  200 
  201         /* destination port of 0 is illegal, based on RFC768. */
  202         if (uh->uh_dport == 0)
  203                 goto badunlocked;
  204 
  205         /*
  206          * Construct sockaddr format source address.
  207          * Stuff source address and datagram in user buffer.
  208          */
  209         udp_in.sin_port = uh->uh_sport;
  210         udp_in.sin_addr = ip->ip_src;
  211 #ifdef INET6
  212         udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
  213 #endif
  214 
  215         /*
  216          * Make mbuf data length reflect UDP length.
  217          * If not enough data to reflect UDP length, drop.
  218          */
  219         len = ntohs((u_short)uh->uh_ulen);
  220         if (ip->ip_len != len) {
  221                 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
  222                         udpstat.udps_badlen++;
  223                         goto badunlocked;
  224                 }
  225                 m_adj(m, len - ip->ip_len);
  226                 /* ip->ip_len = len; */
  227         }
  228         /*
  229          * Save a copy of the IP header in case we want restore it
  230          * for sending an ICMP error message in response.
  231          */
  232         if (!blackhole)
  233                 save_ip = *ip;
  234 
  235         /*
  236          * Checksum extended UDP header and data.
  237          */
  238         if (uh->uh_sum) {
  239                 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
  240                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
  241                                 uh->uh_sum = m->m_pkthdr.csum_data;
  242                         else
  243                                 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
  244                                     ip->ip_dst.s_addr, htonl((u_short)len +
  245                                     m->m_pkthdr.csum_data + IPPROTO_UDP));
  246                         uh->uh_sum ^= 0xffff;
  247                 } else {
  248                         char b[9];
  249                         bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
  250                         bzero(((struct ipovly *)ip)->ih_x1, 9);
  251                         ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
  252                         uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
  253                         bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
  254                 }
  255                 if (uh->uh_sum) {
  256                         udpstat.udps_badsum++;
  257                         m_freem(m);
  258                         return;
  259                 }
  260         } else
  261                 udpstat.udps_nosum++;
  262 
  263         INP_INFO_RLOCK(&udbinfo);
  264 
  265         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
  266             in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
  267                 struct inpcb *last;
  268                 /*
  269                  * Deliver a multicast or broadcast datagram to *all* sockets
  270                  * for which the local and remote addresses and ports match
  271                  * those of the incoming datagram.  This allows more than
  272                  * one process to receive multi/broadcasts on the same port.
  273                  * (This really ought to be done for unicast datagrams as
  274                  * well, but that would cause problems with existing
  275                  * applications that open both address-specific sockets and
  276                  * a wildcard socket listening to the same port -- they would
  277                  * end up receiving duplicates of every unicast datagram.
  278                  * Those applications open the multiple sockets to overcome an
  279                  * inadequacy of the UDP socket interface, but for backwards
  280                  * compatibility we avoid the problem here rather than
  281                  * fixing the interface.  Maybe 4.5BSD will remedy this?)
  282                  */
  283 
  284                 /*
  285                  * Locate pcb(s) for datagram.
  286                  * (Algorithm copied from raw_intr().)
  287                  */
  288                 last = NULL;
  289                 LIST_FOREACH(inp, &udb, inp_list) {
  290                         INP_LOCK(inp);
  291                         if (inp->inp_lport != uh->uh_dport) {
  292                 docontinue:
  293                                 INP_UNLOCK(inp);
  294                                 continue;
  295                         }
  296 #ifdef INET6
  297                         if ((inp->inp_vflag & INP_IPV4) == 0)
  298                                 goto docontinue;
  299 #endif
  300                         if (inp->inp_laddr.s_addr != INADDR_ANY) {
  301                                 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
  302                                         goto docontinue;
  303                         }
  304                         if (inp->inp_faddr.s_addr != INADDR_ANY) {
  305                                 if (inp->inp_faddr.s_addr !=
  306                                     ip->ip_src.s_addr ||
  307                                     inp->inp_fport != uh->uh_sport)
  308                                         goto docontinue;
  309                         }
  310 
  311                         if (last != NULL) {
  312                                 struct mbuf *n;
  313 
  314                                 n = m_copy(m, 0, M_COPYALL);
  315                                 if (n != NULL)
  316                                         udp_append(last, ip, n,
  317                                                    iphlen +
  318                                                    sizeof(struct udphdr));
  319                                 INP_UNLOCK(last);
  320                         }
  321                         last = inp;
  322                         /*
  323                          * Don't look for additional matches if this one does
  324                          * not have either the SO_REUSEPORT or SO_REUSEADDR
  325                          * socket options set.  This heuristic avoids searching
  326                          * through all pcbs in the common case of a non-shared
  327                          * port.  It * assumes that an application will never
  328                          * clear these options after setting them.
  329                          */
  330                         if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
  331                                 break;
  332                 }
  333 
  334                 if (last == NULL) {
  335                         /*
  336                          * No matching pcb found; discard datagram.
  337                          * (No need to send an ICMP Port Unreachable
  338                          * for a broadcast or multicast datgram.)
  339                          */
  340                         udpstat.udps_noportbcast++;
  341                         goto badheadlocked;
  342                 }
  343                 INP_INFO_RUNLOCK(&udbinfo);
  344                 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
  345                 INP_UNLOCK(last);
  346                 return;
  347         }
  348         /*
  349          * Locate pcb for datagram.
  350          */
  351         inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
  352             ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
  353         if (inp == NULL) {
  354                 if (log_in_vain) {
  355                         char buf[4*sizeof "123"];
  356 
  357                         strcpy(buf, inet_ntoa(ip->ip_dst));
  358                         log(LOG_INFO,
  359                             "Connection attempt to UDP %s:%d from %s:%d\n",
  360                             buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
  361                             ntohs(uh->uh_sport));
  362                 }
  363                 udpstat.udps_noport++;
  364                 if (m->m_flags & (M_BCAST | M_MCAST)) {
  365                         udpstat.udps_noportbcast++;
  366                         goto badheadlocked;
  367                 }
  368                 if (blackhole)
  369                         goto badheadlocked;
  370                 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
  371                         goto badheadlocked;
  372                 *ip = save_ip;
  373                 ip->ip_len += iphlen;
  374                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
  375                 INP_INFO_RUNLOCK(&udbinfo);
  376                 return;
  377         }
  378         INP_LOCK(inp);
  379         INP_INFO_RUNLOCK(&udbinfo);
  380         udp_append(inp, ip, m, iphlen + sizeof(struct udphdr));
  381         INP_UNLOCK(inp);
  382         return;
  383 
  384 badheadlocked:
  385         INP_INFO_RUNLOCK(&udbinfo);
  386         if (inp)
  387                 INP_UNLOCK(inp);
  388 badunlocked:
  389         m_freem(m);
  390         if (opts)
  391                 m_freem(opts);
  392         return;
  393 }
  394 
  395 #ifdef INET6
  396 static void
  397 ip_2_ip6_hdr(ip6, ip)
  398         struct ip6_hdr *ip6;
  399         struct ip *ip;
  400 {
  401         bzero(ip6, sizeof(*ip6));
  402 
  403         ip6->ip6_vfc = IPV6_VERSION;
  404         ip6->ip6_plen = ip->ip_len;
  405         ip6->ip6_nxt = ip->ip_p;
  406         ip6->ip6_hlim = ip->ip_ttl;
  407         ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
  408                 IPV6_ADDR_INT32_SMP;
  409         ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
  410         ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
  411 }
  412 #endif
  413 
  414 /*
  415  * subroutine of udp_input(), mainly for source code readability.
  416  * caller must properly init udp_ip6 and udp_in6 beforehand.
  417  */
  418 static void
  419 udp_append(last, ip, n, off)
  420         struct inpcb *last;
  421         struct ip *ip;
  422         struct mbuf *n;
  423         int off;
  424 {
  425         struct sockaddr *append_sa;
  426         struct mbuf *opts = 0;
  427 
  428 #ifdef IPSEC
  429         /* check AH/ESP integrity. */
  430         if (ipsec4_in_reject_so(n, last->inp_socket)) {
  431                 ipsecstat.in_polvio++;
  432                 m_freem(n);
  433                 return;
  434         }
  435 #endif /*IPSEC*/
  436 #ifdef FAST_IPSEC
  437         /* check AH/ESP integrity. */
  438         if (ipsec4_in_reject(n, last)) {
  439                 m_freem(n);
  440                 return;
  441         }
  442 #endif /*FAST_IPSEC*/
  443 #ifdef MAC
  444         if (mac_check_socket_deliver(last->inp_socket, n) != 0) {
  445                 m_freem(n);
  446                 return;
  447         }
  448 #endif
  449         if (last->inp_flags & INP_CONTROLOPTS ||
  450             last->inp_socket->so_options & SO_TIMESTAMP) {
  451 #ifdef INET6
  452                 if (last->inp_vflag & INP_IPV6) {
  453                         int savedflags;
  454 
  455                         if (udp_ip6.uip6_init_done == 0) {
  456                                 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
  457                                 udp_ip6.uip6_init_done = 1;
  458                         }
  459                         savedflags = last->inp_flags;
  460                         last->inp_flags &= ~INP_UNMAPPABLEOPTS;
  461                         ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
  462                         last->inp_flags = savedflags;
  463                 } else
  464 #endif
  465                 ip_savecontrol(last, &opts, ip, n);
  466         }
  467 #ifdef INET6
  468         if (last->inp_vflag & INP_IPV6) {
  469                 if (udp_in6.uin6_init_done == 0) {
  470                         in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
  471                         udp_in6.uin6_init_done = 1;
  472                 }
  473                 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
  474         } else
  475 #endif
  476         append_sa = (struct sockaddr *)&udp_in;
  477         m_adj(n, off);
  478         if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
  479                 m_freem(n);
  480                 if (opts)
  481                         m_freem(opts);
  482                 udpstat.udps_fullsock++;
  483         } else
  484                 sorwakeup(last->inp_socket);
  485 }
  486 
  487 /*
  488  * Notify a udp user of an asynchronous error;
  489  * just wake up so that he can collect error status.
  490  */
  491 struct inpcb *
  492 udp_notify(inp, errno)
  493         register struct inpcb *inp;
  494         int errno;
  495 {
  496         inp->inp_socket->so_error = errno;
  497         sorwakeup(inp->inp_socket);
  498         sowwakeup(inp->inp_socket);
  499         return inp;
  500 }
  501 
  502 void
  503 udp_ctlinput(cmd, sa, vip)
  504         int cmd;
  505         struct sockaddr *sa;
  506         void *vip;
  507 {
  508         struct ip *ip = vip;
  509         struct udphdr *uh;
  510         struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
  511         struct in_addr faddr;
  512         struct inpcb *inp;
  513         int s;
  514 
  515         faddr = ((struct sockaddr_in *)sa)->sin_addr;
  516         if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
  517                 return;
  518 
  519         if (PRC_IS_REDIRECT(cmd)) {
  520                 ip = 0;
  521                 notify = in_rtchange;
  522         } else if (cmd == PRC_HOSTDEAD)
  523                 ip = 0;
  524         else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
  525                 return;
  526         if (ip) {
  527                 s = splnet();
  528                 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
  529                 INP_INFO_RLOCK(&udbinfo);
  530                 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
  531                     ip->ip_src, uh->uh_sport, 0, NULL);
  532                 if (inp != NULL) {
  533                         INP_LOCK(inp);
  534                         if(inp->inp_socket != NULL) {
  535                                 (*notify)(inp, inetctlerrmap[cmd]);
  536                         }
  537                         INP_UNLOCK(inp);
  538                 }
  539                 INP_INFO_RUNLOCK(&udbinfo);
  540                 splx(s);
  541         } else
  542                 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
  543 }
  544 
  545 static int
  546 udp_pcblist(SYSCTL_HANDLER_ARGS)
  547 {
  548         int error, i, n, s;
  549         struct inpcb *inp, **inp_list;
  550         inp_gen_t gencnt;
  551         struct xinpgen xig;
  552 
  553         /*
  554          * The process of preparing the TCB list is too time-consuming and
  555          * resource-intensive to repeat twice on every request.
  556          */
  557         if (req->oldptr == 0) {
  558                 n = udbinfo.ipi_count;
  559                 req->oldidx = 2 * (sizeof xig)
  560                         + (n + n/8) * sizeof(struct xinpcb);
  561                 return 0;
  562         }
  563 
  564         if (req->newptr != 0)
  565                 return EPERM;
  566 
  567         /*
  568          * OK, now we're committed to doing something.
  569          */
  570         s = splnet();
  571         gencnt = udbinfo.ipi_gencnt;
  572         n = udbinfo.ipi_count;
  573         splx(s);
  574 
  575         sysctl_wire_old_buffer(req, 2 * (sizeof xig)
  576                 + n * sizeof(struct xinpcb));
  577 
  578         xig.xig_len = sizeof xig;
  579         xig.xig_count = n;
  580         xig.xig_gen = gencnt;
  581         xig.xig_sogen = so_gencnt;
  582         error = SYSCTL_OUT(req, &xig, sizeof xig);
  583         if (error)
  584                 return error;
  585 
  586         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
  587         if (inp_list == 0)
  588                 return ENOMEM;
  589         
  590         s = splnet();
  591         INP_INFO_RLOCK(&udbinfo);
  592         for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
  593              inp = LIST_NEXT(inp, inp_list)) {
  594                 INP_LOCK(inp);
  595                 if (inp->inp_gencnt <= gencnt &&
  596                     cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
  597                         inp_list[i++] = inp;
  598                 INP_UNLOCK(inp);
  599         }
  600         INP_INFO_RUNLOCK(&udbinfo);
  601         splx(s);
  602         n = i;
  603 
  604         error = 0;
  605         for (i = 0; i < n; i++) {
  606                 inp = inp_list[i];
  607                 INP_LOCK(inp);
  608                 if (inp->inp_gencnt <= gencnt) {
  609                         struct xinpcb xi;
  610                         xi.xi_len = sizeof xi;
  611                         /* XXX should avoid extra copy */
  612                         bcopy(inp, &xi.xi_inp, sizeof *inp);
  613                         if (inp->inp_socket)
  614                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
  615                         error = SYSCTL_OUT(req, &xi, sizeof xi);
  616                 }
  617                 INP_UNLOCK(inp);
  618         }
  619         if (!error) {
  620                 /*
  621                  * Give the user an updated idea of our state.
  622                  * If the generation differs from what we told
  623                  * her before, she knows that something happened
  624                  * while we were processing this request, and it
  625                  * might be necessary to retry.
  626                  */
  627                 s = splnet();
  628                 INP_INFO_RLOCK(&udbinfo);
  629                 xig.xig_gen = udbinfo.ipi_gencnt;
  630                 xig.xig_sogen = so_gencnt;
  631                 xig.xig_count = udbinfo.ipi_count;
  632                 INP_INFO_RUNLOCK(&udbinfo);
  633                 splx(s);
  634                 error = SYSCTL_OUT(req, &xig, sizeof xig);
  635         }
  636         free(inp_list, M_TEMP);
  637         return error;
  638 }
  639 
  640 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
  641             udp_pcblist, "S,xinpcb", "List of active UDP sockets");
  642 
  643 static int
  644 udp_getcred(SYSCTL_HANDLER_ARGS)
  645 {
  646         struct xucred xuc;
  647         struct sockaddr_in addrs[2];
  648         struct inpcb *inp;
  649         int error, s;
  650 
  651         error = suser_cred(req->td->td_ucred, PRISON_ROOT);
  652         if (error)
  653                 return (error);
  654         error = SYSCTL_IN(req, addrs, sizeof(addrs));
  655         if (error)
  656                 return (error);
  657         s = splnet();
  658         INP_INFO_RLOCK(&udbinfo);
  659         inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
  660                                 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
  661         if (inp == NULL || inp->inp_socket == NULL) {
  662                 error = ENOENT;
  663                 goto out;
  664         }
  665         error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
  666         if (error)
  667                 goto out;
  668         cru2x(inp->inp_socket->so_cred, &xuc);
  669 out:
  670         INP_INFO_RUNLOCK(&udbinfo);
  671         splx(s);
  672         if (error == 0)
  673                 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
  674         return (error);
  675 }
  676 
  677 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
  678     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
  679     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
  680 
  681 static int
  682 udp_output(inp, m, addr, control, td)
  683         register struct inpcb *inp;
  684         struct mbuf *m;
  685         struct sockaddr *addr;
  686         struct mbuf *control;
  687         struct thread *td;
  688 {
  689         register struct udpiphdr *ui;
  690         register int len = m->m_pkthdr.len;
  691         struct in_addr faddr, laddr;
  692         struct cmsghdr *cm;
  693         struct sockaddr_in *sin, src;
  694         int error = 0;
  695         u_short fport, lport;
  696 
  697 #ifdef MAC
  698         mac_create_mbuf_from_socket(inp->inp_socket, m);
  699 #endif
  700 
  701         if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
  702                 error = EMSGSIZE;
  703                 if (control)
  704                         m_freem(control);
  705                 goto release;
  706         }
  707 
  708         src.sin_addr.s_addr = INADDR_ANY;
  709         if (control != NULL) {
  710                 /*
  711                  * XXX: Currently, we assume all the optional information
  712                  * is stored in a single mbuf.
  713                  */
  714                 if (control->m_next) {
  715                         error = EINVAL;
  716                         m_freem(control);
  717                         goto release;
  718                 }
  719                 for (; control->m_len > 0;
  720                     control->m_data += CMSG_ALIGN(cm->cmsg_len),
  721                     control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
  722                         cm = mtod(control, struct cmsghdr *);
  723                         if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
  724                             cm->cmsg_len > control->m_len) {
  725                                 error = EINVAL;
  726                                 break;
  727                         }
  728                         if (cm->cmsg_level != IPPROTO_IP)
  729                                 continue;
  730 
  731                         switch (cm->cmsg_type) {
  732                         case IP_SENDSRCADDR:
  733                                 if (cm->cmsg_len !=
  734                                     CMSG_LEN(sizeof(struct in_addr))) {
  735                                         error = EINVAL;
  736                                         break;
  737                                 }
  738                                 bzero(&src, sizeof(src));
  739                                 src.sin_family = AF_INET;
  740                                 src.sin_len = sizeof(src);
  741                                 src.sin_port = inp->inp_lport;
  742                                 src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
  743                                 break;
  744                         default:
  745                                 error = ENOPROTOOPT;
  746                                 break;
  747                         }
  748                         if (error)
  749                                 break;
  750                 }
  751                 m_freem(control);
  752         }
  753         if (error)
  754                 goto release;
  755         laddr = inp->inp_laddr;
  756         lport = inp->inp_lport;
  757         if (src.sin_addr.s_addr != INADDR_ANY) {
  758                 if (lport == 0) {
  759                         error = EINVAL;
  760                         goto release;
  761                 }
  762                 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
  763                     &laddr.s_addr, &lport, td);
  764                 if (error)
  765                         goto release;
  766         }
  767 
  768         if (addr) {
  769                 sin = (struct sockaddr_in *)addr;
  770                 if (td && jailed(td->td_ucred))
  771                         prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
  772                 if (inp->inp_faddr.s_addr != INADDR_ANY) {
  773                         error = EISCONN;
  774                         goto release;
  775                 }
  776                 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
  777                     &faddr.s_addr, &fport, NULL, td);
  778                 if (error)
  779                         goto release;
  780 
  781                 /* Commit the local port if newly assigned. */
  782                 if (inp->inp_laddr.s_addr == INADDR_ANY &&
  783                     inp->inp_lport == 0) {
  784                         inp->inp_lport = lport;
  785                         if (in_pcbinshash(inp) != 0) {
  786                                 inp->inp_lport = 0;
  787                                 error = EAGAIN;
  788                                 goto release;
  789                         }
  790                         inp->inp_flags |= INP_ANONPORT;
  791                 }
  792         } else {
  793                 faddr = inp->inp_faddr;
  794                 fport = inp->inp_fport;
  795                 if (faddr.s_addr == INADDR_ANY) {
  796                         error = ENOTCONN;
  797                         goto release;
  798                 }
  799         }
  800         /*
  801          * Calculate data length and get a mbuf
  802          * for UDP and IP headers.
  803          */
  804         M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
  805         if (m == 0) {
  806                 error = ENOBUFS;
  807                 goto release;
  808         }
  809 
  810         /*
  811          * Fill in mbuf with extended UDP header
  812          * and addresses and length put into network format.
  813          */
  814         ui = mtod(m, struct udpiphdr *);
  815         bzero(ui->ui_x1, sizeof(ui->ui_x1));    /* XXX still needed? */
  816         ui->ui_pr = IPPROTO_UDP;
  817         ui->ui_src = laddr;
  818         ui->ui_dst = faddr;
  819         ui->ui_sport = lport;
  820         ui->ui_dport = fport;
  821         ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
  822 
  823         /*
  824          * Set up checksum and output datagram.
  825          */
  826         if (udpcksum) {
  827                 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
  828                     htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
  829                 m->m_pkthdr.csum_flags = CSUM_UDP;
  830                 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
  831         } else {
  832                 ui->ui_sum = 0;
  833         }
  834         ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
  835         ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;    /* XXX */
  836         ((struct ip *)ui)->ip_tos = inp->inp_ip_tos;    /* XXX */
  837         udpstat.udps_opackets++;
  838 
  839         error = ip_output(m, inp->inp_options, &inp->inp_route,
  840             (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
  841             inp->inp_moptions, inp);
  842         return (error);
  843 
  844 release:
  845         m_freem(m);
  846         return (error);
  847 }
  848 
  849 u_long  udp_sendspace = 9216;           /* really max datagram size */
  850                                         /* 40 1K datagrams */
  851 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
  852     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
  853 
  854 u_long  udp_recvspace = 40 * (1024 +
  855 #ifdef INET6
  856                                       sizeof(struct sockaddr_in6)
  857 #else
  858                                       sizeof(struct sockaddr_in)
  859 #endif
  860                                       );
  861 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
  862     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
  863 
  864 static int
  865 udp_abort(struct socket *so)
  866 {
  867         struct inpcb *inp;
  868         int s;
  869 
  870         INP_INFO_WLOCK(&udbinfo);
  871         inp = sotoinpcb(so);
  872         if (inp == 0) {
  873                 INP_INFO_WUNLOCK(&udbinfo);
  874                 return EINVAL;  /* ??? possible? panic instead? */
  875         }
  876         INP_LOCK(inp);
  877         soisdisconnected(so);
  878         s = splnet();
  879         in_pcbdetach(inp);
  880         INP_INFO_WUNLOCK(&udbinfo);
  881         splx(s);
  882         return 0;
  883 }
  884 
  885 static int
  886 udp_attach(struct socket *so, int proto, struct thread *td)
  887 {
  888         struct inpcb *inp;
  889         int s, error;
  890 
  891         INP_INFO_WLOCK(&udbinfo);
  892         inp = sotoinpcb(so);
  893         if (inp != 0) {
  894                 INP_INFO_WUNLOCK(&udbinfo);
  895                 return EINVAL;
  896         }
  897         error = soreserve(so, udp_sendspace, udp_recvspace);
  898         if (error) {
  899                 INP_INFO_WUNLOCK(&udbinfo);
  900                 return error;
  901         }
  902         s = splnet();
  903         error = in_pcballoc(so, &udbinfo, td);
  904         splx(s);
  905         if (error)
  906                 return error;
  907 
  908         inp = (struct inpcb *)so->so_pcb;
  909         INP_LOCK(inp);
  910         INP_INFO_WUNLOCK(&udbinfo);
  911         inp->inp_vflag |= INP_IPV4;
  912         inp->inp_ip_ttl = ip_defttl;
  913         INP_UNLOCK(inp);
  914         return 0;
  915 }
  916 
  917 static int
  918 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  919 {
  920         struct inpcb *inp;
  921         int s, error;
  922 
  923         INP_INFO_WLOCK(&udbinfo);
  924         inp = sotoinpcb(so);
  925         if (inp == 0) {
  926                 INP_INFO_WUNLOCK(&udbinfo);
  927                 return EINVAL;
  928         }
  929         INP_LOCK(inp);
  930         s = splnet();
  931         error = in_pcbbind(inp, nam, td);
  932         splx(s);
  933         INP_UNLOCK(inp);
  934         INP_INFO_WUNLOCK(&udbinfo);
  935         return error;
  936 }
  937 
  938 static int
  939 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  940 {
  941         struct inpcb *inp;
  942         int s, error;
  943         struct sockaddr_in *sin;
  944 
  945         INP_INFO_WLOCK(&udbinfo);
  946         inp = sotoinpcb(so);
  947         if (inp == 0) {
  948                 INP_INFO_WUNLOCK(&udbinfo);
  949                 return EINVAL;
  950         }
  951         INP_LOCK(inp);
  952         if (inp->inp_faddr.s_addr != INADDR_ANY) {
  953                 INP_UNLOCK(inp);
  954                 INP_INFO_WUNLOCK(&udbinfo);
  955                 return EISCONN;
  956         }
  957         s = splnet();
  958         sin = (struct sockaddr_in *)nam;
  959         if (td && jailed(td->td_ucred))
  960                 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
  961         error = in_pcbconnect(inp, nam, td);
  962         splx(s);
  963         if (error == 0)
  964                 soisconnected(so);
  965         INP_UNLOCK(inp);
  966         INP_INFO_WUNLOCK(&udbinfo);
  967         return error;
  968 }
  969 
  970 static int
  971 udp_detach(struct socket *so)
  972 {
  973         struct inpcb *inp;
  974         int s;
  975 
  976         INP_INFO_WLOCK(&udbinfo);
  977         inp = sotoinpcb(so);
  978         if (inp == 0) {
  979                 INP_INFO_WUNLOCK(&udbinfo);
  980                 return EINVAL;
  981         }
  982         INP_LOCK(inp);
  983         s = splnet();
  984         in_pcbdetach(inp);
  985         INP_INFO_WUNLOCK(&udbinfo);
  986         splx(s);
  987         return 0;
  988 }
  989 
  990 static int
  991 udp_disconnect(struct socket *so)
  992 {
  993         struct inpcb *inp;
  994         int s;
  995 
  996         INP_INFO_WLOCK(&udbinfo);
  997         inp = sotoinpcb(so);
  998         if (inp == 0) {
  999                 INP_INFO_WUNLOCK(&udbinfo);
 1000                 return EINVAL;
 1001         }
 1002         INP_LOCK(inp);
 1003         if (inp->inp_faddr.s_addr == INADDR_ANY) {
 1004                 INP_INFO_WUNLOCK(&udbinfo);
 1005                 INP_UNLOCK(inp);
 1006                 return ENOTCONN;
 1007         }
 1008 
 1009         s = splnet();
 1010         in_pcbdisconnect(inp);
 1011         inp->inp_laddr.s_addr = INADDR_ANY;
 1012         INP_UNLOCK(inp);
 1013         INP_INFO_WUNLOCK(&udbinfo);
 1014         splx(s);
 1015         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
 1016         return 0;
 1017 }
 1018 
 1019 static int
 1020 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
 1021             struct mbuf *control, struct thread *td)
 1022 {
 1023         struct inpcb *inp;
 1024         int ret;
 1025 
 1026         INP_INFO_WLOCK(&udbinfo);
 1027         inp = sotoinpcb(so);
 1028         if (inp == 0) {
 1029                 INP_INFO_WUNLOCK(&udbinfo);
 1030                 m_freem(m);
 1031                 return EINVAL;
 1032         }
 1033         INP_LOCK(inp);
 1034         ret = udp_output(inp, m, addr, control, td);
 1035         INP_UNLOCK(inp);
 1036         INP_INFO_WUNLOCK(&udbinfo);
 1037         return ret; 
 1038 }
 1039 
 1040 int
 1041 udp_shutdown(struct socket *so)
 1042 {
 1043         struct inpcb *inp;
 1044 
 1045         INP_INFO_RLOCK(&udbinfo);
 1046         inp = sotoinpcb(so);
 1047         if (inp == 0) {
 1048                 INP_INFO_RUNLOCK(&udbinfo);
 1049                 return EINVAL;
 1050         }
 1051         INP_LOCK(inp);
 1052         INP_INFO_RUNLOCK(&udbinfo);
 1053         socantsendmore(so);
 1054         INP_UNLOCK(inp);
 1055         return 0;
 1056 }
 1057 
 1058 /*
 1059  * This is the wrapper function for in_setsockaddr.  We just pass down 
 1060  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking 
 1061  * here because in_setsockaddr will call malloc and might block.
 1062  */
 1063 static int
 1064 udp_sockaddr(struct socket *so, struct sockaddr **nam)
 1065 {
 1066         return (in_setsockaddr(so, nam, &udbinfo));
 1067 }
 1068 
 1069 /*
 1070  * This is the wrapper function for in_setpeeraddr.  We just pass down
 1071  * the pcbinfo for in_setpeeraddr to lock.
 1072  */
 1073 static int
 1074 udp_peeraddr(struct socket *so, struct sockaddr **nam)
 1075 {
 1076         return (in_setpeeraddr(so, nam, &udbinfo));
 1077 }
 1078 
 1079 struct pr_usrreqs udp_usrreqs = {
 1080         udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 
 1081         pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 
 1082         pru_listen_notsupp, udp_peeraddr, pru_rcvd_notsupp, 
 1083         pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
 1084         udp_sockaddr, sosend, soreceive, sopoll
 1085 };

Cache object: 24ea3fc1cfd4ffd20dbcc0b994d137ac


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