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) 2004 Jeffrey M. Hsu.  All rights reserved.
    3  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
    4  *
    5  * This code is derived from software contributed to The DragonFly Project
    6  * by Jeffrey M. Hsu.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. Neither the name of The DragonFly Project nor the names of its
   17  *    contributors may be used to endorse or promote products derived
   18  *    from this software without specific, prior written permission.
   19  *
   20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
   24  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   25  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
   26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
   28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   30  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  */
   33 
   34 /*
   35  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
   36  *      The Regents of the University of California.  All rights reserved.
   37  *
   38  * Redistribution and use in source and binary forms, with or without
   39  * modification, are permitted provided that the following conditions
   40  * are met:
   41  * 1. Redistributions of source code must retain the above copyright
   42  *    notice, this list of conditions and the following disclaimer.
   43  * 2. Redistributions in binary form must reproduce the above copyright
   44  *    notice, this list of conditions and the following disclaimer in the
   45  *    documentation and/or other materials provided with the distribution.
   46  * 3. Neither the name of the University nor the names of its contributors
   47  *    may be used to endorse or promote products derived from this software
   48  *    without specific prior written permission.
   49  *
   50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   60  * SUCH DAMAGE.
   61  *
   62  *      @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
   63  * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $
   64  */
   65 
   66 #include "opt_ipsec.h"
   67 #include "opt_inet6.h"
   68 
   69 #include <sys/param.h>
   70 #include <sys/systm.h>
   71 #include <sys/kernel.h>
   72 #include <sys/malloc.h>
   73 #include <sys/mbuf.h>
   74 #include <sys/domain.h>
   75 #include <sys/proc.h>
   76 #include <sys/priv.h>
   77 #include <sys/protosw.h>
   78 #include <sys/socket.h>
   79 #include <sys/socketvar.h>
   80 #include <sys/sysctl.h>
   81 #include <sys/syslog.h>
   82 #include <sys/in_cksum.h>
   83 
   84 #include <sys/thread2.h>
   85 #include <sys/socketvar2.h>
   86 #include <sys/serialize.h>
   87 
   88 #include <machine/stdarg.h>
   89 
   90 #include <net/if.h>
   91 #include <net/route.h>
   92 #include <net/netmsg2.h>
   93 #include <net/netisr2.h>
   94 
   95 #include <netinet/in.h>
   96 #include <netinet/in_systm.h>
   97 #include <netinet/ip.h>
   98 #ifdef INET6
   99 #include <netinet/ip6.h>
  100 #endif
  101 #include <netinet/in_pcb.h>
  102 #include <netinet/in_var.h>
  103 #include <netinet/ip_var.h>
  104 #ifdef INET6
  105 #include <netinet6/ip6_var.h>
  106 #endif
  107 #include <netinet/ip_icmp.h>
  108 #include <netinet/icmp_var.h>
  109 #include <netinet/udp.h>
  110 #include <netinet/udp_var.h>
  111 
  112 #ifdef FAST_IPSEC
  113 #include <netproto/ipsec/ipsec.h>
  114 #endif
  115 
  116 #ifdef IPSEC
  117 #include <netinet6/ipsec.h>
  118 #endif
  119 
  120 /*
  121  * UDP protocol implementation.
  122  * Per RFC 768, August, 1980.
  123  */
  124 #ifndef COMPAT_42
  125 static int      udpcksum = 1;
  126 #else
  127 static int      udpcksum = 0;           /* XXX */
  128 #endif
  129 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
  130     &udpcksum, 0, "Enable checksumming of UDP packets");
  131 
  132 int     log_in_vain = 0;
  133 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
  134     &log_in_vain, 0, "Log all incoming UDP packets");
  135 
  136 static int      blackhole = 0;
  137 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
  138         &blackhole, 0, "Do not send port unreachables for refused connects");
  139 
  140 static int      strict_mcast_mship = 1;
  141 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
  142         &strict_mcast_mship, 0, "Only send multicast to member sockets");
  143 
  144 int     udp_sosend_async = 1;
  145 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_async, CTLFLAG_RW,
  146         &udp_sosend_async, 0, "UDP asynchronized pru_send");
  147 
  148 int     udp_sosend_prepend = 1;
  149 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_prepend, CTLFLAG_RW,
  150         &udp_sosend_prepend, 0,
  151         "Prepend enough space for proto and link header in pru_send");
  152 
  153 static int udp_reuseport_ext = 1;
  154 SYSCTL_INT(_net_inet_udp, OID_AUTO, reuseport_ext, CTLFLAG_RW,
  155         &udp_reuseport_ext, 0, "SO_REUSEPORT extension");
  156 
  157 struct  inpcbinfo udbinfo;
  158 
  159 static struct netisr_barrier *udbinfo_br;
  160 static struct lwkt_serialize udbinfo_slize = LWKT_SERIALIZE_INITIALIZER;
  161 
  162 #ifndef UDBHASHSIZE
  163 #define UDBHASHSIZE 16
  164 #endif
  165 
  166 struct  udpstat udpstat_percpu[MAXCPU] __cachealign;
  167 
  168 #ifdef INET6
  169 struct udp_in6 {
  170         struct sockaddr_in6     uin6_sin;
  171         u_char                  uin6_init_done : 1;
  172 };
  173 struct udp_ip6 {
  174         struct ip6_hdr          uip6_ip6;
  175         u_char                  uip6_init_done : 1;
  176 };
  177 #else
  178 struct udp_in6;
  179 struct udp_ip6;
  180 #endif /* INET6 */
  181 
  182 static void udp_append (struct inpcb *last, struct ip *ip,
  183     struct mbuf *n, int off, struct sockaddr_in *udp_in,
  184     struct udp_in6 *, struct udp_ip6 *);
  185 #ifdef INET6
  186 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
  187 #endif
  188 
  189 static int udp_connect_oncpu(struct socket *so, struct thread *td,
  190                         struct sockaddr_in *sin, struct sockaddr_in *if_sin);
  191 static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
  192                         struct thread *, int, boolean_t);
  193 
  194 void
  195 udp_init(void)
  196 {
  197         int cpu;
  198 
  199         in_pcbinfo_init(&udbinfo);
  200         udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
  201         udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
  202                                         &udbinfo.porthashmask);
  203         udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
  204                                             &udbinfo.wildcardhashmask);
  205         udbinfo.localgrphashbase = hashinit(UDBHASHSIZE, M_PCB,
  206                                             &udbinfo.localgrphashmask);
  207         udbinfo.ipi_size = sizeof(struct inpcb);
  208 
  209         udbinfo_br = netisr_barrier_create();
  210 
  211         /*
  212          * Initialize UDP statistics counters for each CPU.
  213          */
  214         for (cpu = 0; cpu < ncpus; ++cpu)
  215                 bzero(&udpstat_percpu[cpu], sizeof(struct udpstat));
  216 }
  217 
  218 static int
  219 sysctl_udpstat(SYSCTL_HANDLER_ARGS)
  220 {
  221         int cpu, error = 0;
  222 
  223         for (cpu = 0; cpu < ncpus; ++cpu) {
  224                 if ((error = SYSCTL_OUT(req, &udpstat_percpu[cpu],
  225                                         sizeof(struct udpstat))))
  226                         break;
  227                 if ((error = SYSCTL_IN(req, &udpstat_percpu[cpu],
  228                                        sizeof(struct udpstat))))
  229                         break;
  230         }
  231 
  232         return (error);
  233 }
  234 SYSCTL_PROC(_net_inet_udp, UDPCTL_STATS, stats, (CTLTYPE_OPAQUE | CTLFLAG_RW),
  235     0, 0, sysctl_udpstat, "S,udpstat", "UDP statistics");
  236 
  237 /*
  238  * Check multicast packets to make sure they are only sent to sockets with
  239  * multicast memberships for the packet's destination address and arrival
  240  * interface.  Multicast packets to multicast-unaware sockets are also
  241  * disallowed.
  242  *
  243  * Returns 0 if the packet is acceptable, -1 if it is not.
  244  */
  245 static __inline int
  246 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
  247 {
  248         int mshipno;
  249         struct ip_moptions *mopt;
  250 
  251         if (strict_mcast_mship == 0 ||
  252             !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
  253                 return (0);
  254         }
  255         mopt = inp->inp_moptions;
  256         if (mopt == NULL)
  257                 return (-1);
  258         for (mshipno = 0; mshipno < mopt->imo_num_memberships; ++mshipno) {
  259                 struct in_multi *maddr = mopt->imo_membership[mshipno];
  260 
  261                 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
  262                     m->m_pkthdr.rcvif == maddr->inm_ifp) {
  263                         return (0);
  264                 }
  265         }
  266         return (-1);
  267 }
  268 
  269 int
  270 udp_input(struct mbuf **mp, int *offp, int proto)
  271 {
  272         struct sockaddr_in udp_in = { sizeof udp_in, AF_INET };
  273 #ifdef INET6
  274         struct udp_in6 udp_in6 = {
  275                 { sizeof udp_in6.uin6_sin, AF_INET6 }, 0
  276         };
  277         struct udp_ip6 udp_ip6;
  278 #endif
  279 
  280         int iphlen;
  281         struct ip *ip;
  282         struct udphdr *uh;
  283         struct inpcb *inp;
  284         struct mbuf *m;
  285         struct mbuf *opts = NULL;
  286         int len, off;
  287         struct ip save_ip;
  288         struct sockaddr *append_sa;
  289 
  290         off = *offp;
  291         m = *mp;
  292         *mp = NULL;
  293 
  294         iphlen = off;
  295         udp_stat.udps_ipackets++;
  296 
  297         /*
  298          * Strip IP options, if any; should skip this,
  299          * make available to user, and use on returned packets,
  300          * but we don't yet have a way to check the checksum
  301          * with options still present.
  302          */
  303         if (iphlen > sizeof(struct ip)) {
  304                 ip_stripoptions(m);
  305                 iphlen = sizeof(struct ip);
  306         }
  307 
  308         /*
  309          * IP and UDP headers are together in first mbuf.
  310          * Already checked and pulled up in ip_demux().
  311          */
  312         KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
  313             ("UDP header not in one mbuf"));
  314 
  315         ip = mtod(m, struct ip *);
  316         uh = (struct udphdr *)((caddr_t)ip + iphlen);
  317 
  318         /* destination port of 0 is illegal, based on RFC768. */
  319         if (uh->uh_dport == 0)
  320                 goto bad;
  321 
  322         /*
  323          * Make mbuf data length reflect UDP length.
  324          * If not enough data to reflect UDP length, drop.
  325          */
  326         len = ntohs((u_short)uh->uh_ulen);
  327         if (ip->ip_len != len) {
  328                 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
  329                         udp_stat.udps_badlen++;
  330                         goto bad;
  331                 }
  332                 m_adj(m, len - ip->ip_len);
  333                 /* ip->ip_len = len; */
  334         }
  335         /*
  336          * Save a copy of the IP header in case we want restore it
  337          * for sending an ICMP error message in response.
  338          */
  339         save_ip = *ip;
  340 
  341         /*
  342          * Checksum extended UDP header and data.
  343          */
  344         if (uh->uh_sum) {
  345                 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
  346                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
  347                                 uh->uh_sum = m->m_pkthdr.csum_data;
  348                         else
  349                                 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
  350                                     ip->ip_dst.s_addr, htonl((u_short)len +
  351                                     m->m_pkthdr.csum_data + IPPROTO_UDP));
  352                         uh->uh_sum ^= 0xffff;
  353                 } else {
  354                         char b[9];
  355 
  356                         bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
  357                         bzero(((struct ipovly *)ip)->ih_x1, 9);
  358                         ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
  359                         uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
  360                         bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
  361                 }
  362                 if (uh->uh_sum) {
  363                         udp_stat.udps_badsum++;
  364                         m_freem(m);
  365                         return(IPPROTO_DONE);
  366                 }
  367         } else
  368                 udp_stat.udps_nosum++;
  369 
  370         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
  371             in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
  372                 struct inpcb *last;
  373 
  374                 /*
  375                  * Deliver a multicast or broadcast datagram to *all* sockets
  376                  * for which the local and remote addresses and ports match
  377                  * those of the incoming datagram.  This allows more than
  378                  * one process to receive multi/broadcasts on the same port.
  379                  * (This really ought to be done for unicast datagrams as
  380                  * well, but that would cause problems with existing
  381                  * applications that open both address-specific sockets and
  382                  * a wildcard socket listening to the same port -- they would
  383                  * end up receiving duplicates of every unicast datagram.
  384                  * Those applications open the multiple sockets to overcome an
  385                  * inadequacy of the UDP socket interface, but for backwards
  386                  * compatibility we avoid the problem here rather than
  387                  * fixing the interface.  Maybe 4.5BSD will remedy this?)
  388                  */
  389 
  390                 /*
  391                  * Construct sockaddr format source address.
  392                  */
  393                 udp_in.sin_port = uh->uh_sport;
  394                 udp_in.sin_addr = ip->ip_src;
  395                 /*
  396                  * Locate pcb(s) for datagram.
  397                  * (Algorithm copied from raw_intr().)
  398                  */
  399                 last = NULL;
  400 #ifdef INET6
  401                 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
  402 #endif
  403                 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
  404                         KKASSERT((inp->inp_flags & INP_PLACEMARKER) == 0);
  405 #ifdef INET6
  406                         if (!(inp->inp_vflag & INP_IPV4))
  407                                 continue;
  408 #endif
  409                         if (inp->inp_lport != uh->uh_dport)
  410                                 continue;
  411                         if (inp->inp_laddr.s_addr != INADDR_ANY) {
  412                                 if (inp->inp_laddr.s_addr !=
  413                                     ip->ip_dst.s_addr)
  414                                         continue;
  415                         }
  416                         if (inp->inp_faddr.s_addr != INADDR_ANY) {
  417                                 if (inp->inp_faddr.s_addr !=
  418                                     ip->ip_src.s_addr ||
  419                                     inp->inp_fport != uh->uh_sport)
  420                                         continue;
  421                         }
  422 
  423                         if (check_multicast_membership(ip, inp, m) < 0)
  424                                 continue;
  425 
  426                         if (last != NULL) {
  427                                 struct mbuf *n;
  428 
  429 #ifdef IPSEC
  430                                 /* check AH/ESP integrity. */
  431                                 if (ipsec4_in_reject_so(m, last->inp_socket))
  432                                         ipsecstat.in_polvio++;
  433                                         /* do not inject data to pcb */
  434                                 else
  435 #endif /*IPSEC*/
  436 #ifdef FAST_IPSEC
  437                                 /* check AH/ESP integrity. */
  438                                 if (ipsec4_in_reject(m, last))
  439                                         ;
  440                                 else
  441 #endif /*FAST_IPSEC*/
  442                                 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
  443                                         udp_append(last, ip, n,
  444                                             iphlen + sizeof(struct udphdr),
  445                                             &udp_in,
  446 #ifdef INET6
  447                                             &udp_in6, &udp_ip6
  448 #else
  449                                             NULL, NULL
  450 #endif
  451                                             );
  452                         }
  453                         last = inp;
  454                         /*
  455                          * Don't look for additional matches if this one does
  456                          * not have either the SO_REUSEPORT or SO_REUSEADDR
  457                          * socket options set.  This heuristic avoids searching
  458                          * through all pcbs in the common case of a non-shared
  459                          * port.  It * assumes that an application will never
  460                          * clear these options after setting them.
  461                          */
  462                         if (!(last->inp_socket->so_options &
  463                             (SO_REUSEPORT | SO_REUSEADDR)))
  464                                 break;
  465                 }
  466 
  467                 if (last == NULL) {
  468                         /*
  469                          * No matching pcb found; discard datagram.
  470                          * (No need to send an ICMP Port Unreachable
  471                          * for a broadcast or multicast datgram.)
  472                          */
  473                         udp_stat.udps_noportbcast++;
  474                         goto bad;
  475                 }
  476 #ifdef IPSEC
  477                 /* check AH/ESP integrity. */
  478                 if (ipsec4_in_reject_so(m, last->inp_socket)) {
  479                         ipsecstat.in_polvio++;
  480                         goto bad;
  481                 }
  482 #endif /*IPSEC*/
  483 #ifdef FAST_IPSEC
  484                 /* check AH/ESP integrity. */
  485                 if (ipsec4_in_reject(m, last))
  486                         goto bad;
  487 #endif /*FAST_IPSEC*/
  488                 udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
  489                     &udp_in,
  490 #ifdef INET6
  491                     &udp_in6, &udp_ip6
  492 #else
  493                     NULL, NULL
  494 #endif
  495                     );
  496                 return(IPPROTO_DONE);
  497         }
  498         /*
  499          * Locate pcb for datagram.
  500          */
  501         inp = in_pcblookup_pkthash(&udbinfo, ip->ip_src, uh->uh_sport,
  502             ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif,
  503             udp_reuseport_ext ? m : NULL);
  504         if (inp == NULL) {
  505                 if (log_in_vain) {
  506                         char buf[sizeof "aaa.bbb.ccc.ddd"];
  507 
  508                         strcpy(buf, inet_ntoa(ip->ip_dst));
  509                         log(LOG_INFO,
  510                             "Connection attempt to UDP %s:%d from %s:%d\n",
  511                             buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
  512                             ntohs(uh->uh_sport));
  513                 }
  514                 udp_stat.udps_noport++;
  515                 if (m->m_flags & (M_BCAST | M_MCAST)) {
  516                         udp_stat.udps_noportbcast++;
  517                         goto bad;
  518                 }
  519                 if (blackhole)
  520                         goto bad;
  521 #ifdef ICMP_BANDLIM
  522                 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
  523                         goto bad;
  524 #endif
  525                 *ip = save_ip;
  526                 ip->ip_len += iphlen;
  527                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
  528                 return(IPPROTO_DONE);
  529         }
  530 #ifdef IPSEC
  531         if (ipsec4_in_reject_so(m, inp->inp_socket)) {
  532                 ipsecstat.in_polvio++;
  533                 goto bad;
  534         }
  535 #endif /*IPSEC*/
  536 #ifdef FAST_IPSEC
  537         if (ipsec4_in_reject(m, inp))
  538                 goto bad;
  539 #endif /*FAST_IPSEC*/
  540         /*
  541          * Check the minimum TTL for socket.
  542          */
  543         if (ip->ip_ttl < inp->inp_ip_minttl)
  544                 goto bad;
  545 
  546         /*
  547          * Construct sockaddr format source address.
  548          * Stuff source address and datagram in user buffer.
  549          */
  550         udp_in.sin_port = uh->uh_sport;
  551         udp_in.sin_addr = ip->ip_src;
  552         if ((inp->inp_flags & INP_CONTROLOPTS) ||
  553             (inp->inp_socket->so_options & SO_TIMESTAMP)) {
  554 #ifdef INET6
  555                 if (inp->inp_vflag & INP_IPV6) {
  556                         int savedflags;
  557 
  558                         ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
  559                         savedflags = inp->inp_flags;
  560                         inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
  561                         ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
  562                         inp->inp_flags = savedflags;
  563                 } else
  564 #endif
  565                 ip_savecontrol(inp, &opts, ip, m);
  566         }
  567         m_adj(m, iphlen + sizeof(struct udphdr));
  568 #ifdef INET6
  569         if (inp->inp_vflag & INP_IPV6) {
  570                 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
  571                 append_sa = (struct sockaddr *)&udp_in6;
  572         } else
  573 #endif
  574                 append_sa = (struct sockaddr *)&udp_in;
  575 
  576         lwkt_gettoken(&inp->inp_socket->so_rcv.ssb_token);
  577         if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
  578                 udp_stat.udps_fullsock++;
  579                 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
  580                 goto bad;
  581         }
  582         lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
  583         sorwakeup(inp->inp_socket);
  584         return(IPPROTO_DONE);
  585 bad:
  586         m_freem(m);
  587         if (opts)
  588                 m_freem(opts);
  589         return(IPPROTO_DONE);
  590 }
  591 
  592 #ifdef INET6
  593 static void
  594 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
  595 {
  596         bzero(ip6, sizeof *ip6);
  597 
  598         ip6->ip6_vfc = IPV6_VERSION;
  599         ip6->ip6_plen = ip->ip_len;
  600         ip6->ip6_nxt = ip->ip_p;
  601         ip6->ip6_hlim = ip->ip_ttl;
  602         ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
  603                 IPV6_ADDR_INT32_SMP;
  604         ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
  605         ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
  606 }
  607 #endif
  608 
  609 /*
  610  * subroutine of udp_input(), mainly for source code readability.
  611  * caller must properly init udp_ip6 and udp_in6 beforehand.
  612  */
  613 static void
  614 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
  615     struct sockaddr_in *udp_in,
  616     struct udp_in6 *udp_in6, struct udp_ip6 *udp_ip6)
  617 {
  618         struct sockaddr *append_sa;
  619         struct mbuf *opts = NULL;
  620 
  621         if (last->inp_flags & INP_CONTROLOPTS ||
  622             last->inp_socket->so_options & SO_TIMESTAMP) {
  623 #ifdef INET6
  624                 if (last->inp_vflag & INP_IPV6) {
  625                         int savedflags;
  626 
  627                         if (udp_ip6->uip6_init_done == 0) {
  628                                 ip_2_ip6_hdr(&udp_ip6->uip6_ip6, ip);
  629                                 udp_ip6->uip6_init_done = 1;
  630                         }
  631                         savedflags = last->inp_flags;
  632                         last->inp_flags &= ~INP_UNMAPPABLEOPTS;
  633                         ip6_savecontrol(last, &opts, &udp_ip6->uip6_ip6, n);
  634                         last->inp_flags = savedflags;
  635                 } else
  636 #endif
  637                 ip_savecontrol(last, &opts, ip, n);
  638         }
  639 #ifdef INET6
  640         if (last->inp_vflag & INP_IPV6) {
  641                 if (udp_in6->uin6_init_done == 0) {
  642                         in6_sin_2_v4mapsin6(udp_in, &udp_in6->uin6_sin);
  643                         udp_in6->uin6_init_done = 1;
  644                 }
  645                 append_sa = (struct sockaddr *)&udp_in6->uin6_sin;
  646         } else
  647 #endif
  648                 append_sa = (struct sockaddr *)udp_in;
  649         m_adj(n, off);
  650         lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
  651         if (ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
  652                 m_freem(n);
  653                 if (opts)
  654                         m_freem(opts);
  655                 udp_stat.udps_fullsock++;
  656         } else {
  657                 sorwakeup(last->inp_socket);
  658         }
  659         lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
  660 }
  661 
  662 /*
  663  * Notify a udp user of an asynchronous error;
  664  * just wake up so that he can collect error status.
  665  */
  666 void
  667 udp_notify(struct inpcb *inp, int error)
  668 {
  669         inp->inp_socket->so_error = error;
  670         sorwakeup(inp->inp_socket);
  671         sowwakeup(inp->inp_socket);
  672 }
  673 
  674 struct netmsg_udp_notify {
  675         struct netmsg_base base;
  676         void            (*nm_notify)(struct inpcb *, int);
  677         struct in_addr  nm_faddr;
  678         int             nm_arg;
  679 };
  680 
  681 static void
  682 udp_notifyall_oncpu(netmsg_t msg)
  683 {
  684         struct netmsg_udp_notify *nm = (struct netmsg_udp_notify *)msg;
  685 #if 0
  686         int nextcpu;
  687 #endif
  688 
  689         in_pcbnotifyall(&udbinfo.pcblisthead, nm->nm_faddr,
  690                         nm->nm_arg, nm->nm_notify);
  691         lwkt_replymsg(&nm->base.lmsg, 0);
  692 
  693 #if 0
  694         /* XXX currently udp only runs on cpu 0 */
  695         nextcpu = mycpuid + 1;
  696         if (nextcpu < ncpus2)
  697                 lwkt_forwardmsg(netisr_cpuport(nextcpu), &nm->base.lmsg);
  698         else
  699                 lwkt_replymsg(&nmsg->base.lmsg, 0);
  700 #endif
  701 }
  702 
  703 static void
  704 udp_rtchange(struct inpcb *inp, int err)
  705 {
  706         /* XXX Nuke this, once UDP inpcbs are CPU localized */
  707         if (inp->inp_route.ro_rt && inp->inp_route.ro_rt->rt_cpuid == mycpuid) {
  708                 rtfree(inp->inp_route.ro_rt);
  709                 inp->inp_route.ro_rt = NULL;
  710                 /*
  711                  * A new route can be allocated the next time
  712                  * output is attempted.
  713                  */
  714         }
  715 }
  716 
  717 void
  718 udp_ctlinput(netmsg_t msg)
  719 {
  720         struct sockaddr *sa = msg->ctlinput.nm_arg;
  721         struct ip *ip = msg->ctlinput.nm_extra;
  722         int cmd = msg->ctlinput.nm_cmd;
  723         struct udphdr *uh;
  724         void (*notify) (struct inpcb *, int) = udp_notify;
  725         struct in_addr faddr;
  726         struct inpcb *inp;
  727 
  728         KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
  729 
  730         faddr = ((struct sockaddr_in *)sa)->sin_addr;
  731         if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
  732                 goto done;
  733 
  734         if (PRC_IS_REDIRECT(cmd)) {
  735                 ip = NULL;
  736                 notify = udp_rtchange;
  737         } else if (cmd == PRC_HOSTDEAD) {
  738                 ip = NULL;
  739         } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
  740                 goto done;
  741         }
  742 
  743         if (ip) {
  744                 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
  745                 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
  746                                         ip->ip_src, uh->uh_sport, 0, NULL);
  747                 if (inp != NULL && inp->inp_socket != NULL)
  748                         (*notify)(inp, inetctlerrmap[cmd]);
  749         } else if (PRC_IS_REDIRECT(cmd)) {
  750                 struct netmsg_udp_notify *nm;
  751 
  752                 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
  753                 nm = kmalloc(sizeof(*nm), M_LWKTMSG, M_INTWAIT);
  754                 netmsg_init(&nm->base, NULL, &netisr_afree_rport,
  755                             0, udp_notifyall_oncpu);
  756                 nm->nm_faddr = faddr;
  757                 nm->nm_arg = inetctlerrmap[cmd];
  758                 nm->nm_notify = notify;
  759                 lwkt_sendmsg(netisr_cpuport(0), &nm->base.lmsg);
  760         } else {
  761                 /*
  762                  * XXX We should forward msg upon PRC_HOSTHEAD and ip == NULL,
  763                  * once UDP inpcbs are CPU localized
  764                  */
  765                 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
  766                 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
  767                                 notify);
  768         }
  769 done:
  770         lwkt_replymsg(&msg->lmsg, 0);
  771 }
  772 
  773 static int
  774 udp_pcblist(SYSCTL_HANDLER_ARGS)
  775 {
  776         struct xinpcb *xi;
  777         int error, nxi, i;
  778 
  779         udbinfo_lock();
  780         error = in_pcblist_global_nomarker(oidp, arg1, arg2, req, &xi, &nxi);
  781         udbinfo_unlock();
  782 
  783         if (error) {
  784                 KKASSERT(xi == NULL);
  785                 return error;
  786         }
  787         if (nxi == 0) {
  788                 KKASSERT(xi == NULL);
  789                 return 0;
  790         }
  791 
  792         for (i = 0; i < nxi; ++i) {
  793                 error = SYSCTL_OUT(req, &xi[i], sizeof(xi[i]));
  794                 if (error)
  795                         break;
  796         }
  797         kfree(xi, M_TEMP);
  798 
  799         return error;
  800 }
  801 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
  802             udp_pcblist, "S,xinpcb", "List of active UDP sockets");
  803 
  804 static int
  805 udp_getcred(SYSCTL_HANDLER_ARGS)
  806 {
  807         struct sockaddr_in addrs[2];
  808         struct ucred cred0, *cred = NULL;
  809         struct inpcb *inp;
  810         int error;
  811 
  812         error = priv_check(req->td, PRIV_ROOT);
  813         if (error)
  814                 return (error);
  815         error = SYSCTL_IN(req, addrs, sizeof addrs);
  816         if (error)
  817                 return (error);
  818 
  819         udbinfo_lock();
  820         inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
  821                                 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
  822         if (inp == NULL || inp->inp_socket == NULL) {
  823                 error = ENOENT;
  824         } else {
  825                 if (inp->inp_socket->so_cred != NULL) {
  826                         cred0 = *(inp->inp_socket->so_cred);
  827                         cred = &cred0;
  828                 }
  829         }
  830         udbinfo_unlock();
  831 
  832         if (error)
  833                 return error;
  834 
  835         return SYSCTL_OUT(req, cred, sizeof(struct ucred));
  836 }
  837 
  838 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
  839     0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
  840 
  841 static int
  842 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr,
  843     struct thread *td, int flags, boolean_t held_td)
  844 {
  845         struct udpiphdr *ui;
  846         int len = m->m_pkthdr.len;
  847         struct sockaddr_in *sin;        /* really is initialized before use */
  848         int error = 0, lport_any = 0;
  849 
  850         if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
  851                 error = EMSGSIZE;
  852                 goto release;
  853         }
  854 
  855         if (inp->inp_lport == 0) {      /* unbound socket */
  856                 error = in_pcbbind(inp, NULL, td);
  857                 if (error)
  858                         goto release;
  859 
  860                 udbinfo_barrier_set();
  861                 in_pcbinswildcardhash(inp);
  862                 udbinfo_barrier_rem();
  863                 lport_any = 1;
  864         }
  865 
  866         if (dstaddr != NULL) {          /* destination address specified */
  867                 if (inp->inp_faddr.s_addr != INADDR_ANY) {
  868                         /* already connected */
  869                         error = EISCONN;
  870                         goto release;
  871                 }
  872                 sin = (struct sockaddr_in *)dstaddr;
  873                 if (!prison_remote_ip(td, (struct sockaddr *)&sin)) {
  874                         error = EAFNOSUPPORT; /* IPv6 only jail */
  875                         goto release;
  876                 }
  877         } else {
  878                 if (inp->inp_faddr.s_addr == INADDR_ANY) {
  879                         /* no destination specified and not already connected */
  880                         error = ENOTCONN;
  881                         goto release;
  882                 }
  883                 sin = NULL;
  884         }
  885 
  886         /*
  887          * Calculate data length and get a mbuf
  888          * for UDP and IP headers.
  889          */
  890         M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
  891         if (m == NULL) {
  892                 error = ENOBUFS;
  893                 goto release;
  894         }
  895 
  896         /*
  897          * Fill in mbuf with extended UDP header
  898          * and addresses and length put into network format.
  899          */
  900         ui = mtod(m, struct udpiphdr *);
  901         bzero(ui->ui_x1, sizeof ui->ui_x1);     /* XXX still needed? */
  902         ui->ui_pr = IPPROTO_UDP;
  903 
  904         /*
  905          * Set destination address.
  906          */
  907         if (dstaddr != NULL) {                  /* use specified destination */
  908                 ui->ui_dst = sin->sin_addr;
  909                 ui->ui_dport = sin->sin_port;
  910         } else {                                /* use connected destination */
  911                 ui->ui_dst = inp->inp_faddr;
  912                 ui->ui_dport = inp->inp_fport;
  913         }
  914 
  915         /*
  916          * Set source address.
  917          */
  918         if (inp->inp_laddr.s_addr == INADDR_ANY ||
  919             IN_MULTICAST(ntohl(inp->inp_laddr.s_addr))) {
  920                 struct sockaddr_in *if_sin;
  921 
  922                 if (dstaddr == NULL) {  
  923                         /*
  924                          * connect() had (or should have) failed because
  925                          * the interface had no IP address, but the
  926                          * application proceeded to call send() anyways.
  927                          */
  928                         error = ENOTCONN;
  929                         goto release;
  930                 }
  931 
  932                 /* Look up outgoing interface. */
  933                 error = in_pcbladdr_find(inp, dstaddr, &if_sin, td, 1);
  934                 if (error)
  935                         goto release;
  936                 ui->ui_src = if_sin->sin_addr;  /* use address of interface */
  937         } else {
  938                 ui->ui_src = inp->inp_laddr;    /* use non-null bound address */
  939         }
  940         ui->ui_sport = inp->inp_lport;
  941         KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
  942 
  943         /*
  944          * Release the original thread, since it is no longer used
  945          */
  946         if (held_td) {
  947                 lwkt_rele(td);
  948                 held_td = FALSE;
  949         }
  950 
  951         ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
  952 
  953         /*
  954          * Set up checksum and output datagram.
  955          */
  956         if (udpcksum) {
  957                 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
  958                     htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
  959                 m->m_pkthdr.csum_flags = CSUM_UDP;
  960                 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
  961                 m->m_pkthdr.csum_thlen = sizeof(struct udphdr);
  962         } else {
  963                 ui->ui_sum = 0;
  964         }
  965         ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
  966         ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;    /* XXX */
  967         ((struct ip *)ui)->ip_tos = inp->inp_ip_tos;    /* XXX */
  968         udp_stat.udps_opackets++;
  969 
  970         error = ip_output(m, inp->inp_options, &inp->inp_route,
  971             (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)) |
  972             flags | IP_DEBUGROUTE,
  973             inp->inp_moptions, inp);
  974 
  975         /*
  976          * If this is the first data gram sent on an unbound and unconnected
  977          * UDP socket, lport will be changed in this function.  If target
  978          * CPU after this lport changing is no longer the current CPU, then
  979          * free the route entry allocated on the current CPU.
  980          */
  981         if (lport_any) {
  982                 if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport,
  983                     inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) {
  984 #ifdef notyet
  985                         struct route *ro = &inp->inp_route;
  986 
  987                         if (ro->ro_rt != NULL)
  988                                 RTFREE(ro->ro_rt);
  989                         bzero(ro, sizeof(*ro));
  990 #else
  991                         panic("UDP activity should only be in netisr0");
  992 #endif
  993                 }
  994         }
  995         return (error);
  996 
  997 release:
  998         if (held_td)
  999                 lwkt_rele(td);
 1000         m_freem(m);
 1001         return (error);
 1002 }
 1003 
 1004 u_long  udp_sendspace = 9216;           /* really max datagram size */
 1005                                         /* 40 1K datagrams */
 1006 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
 1007     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
 1008 
 1009 u_long  udp_recvspace = 40 * (1024 +
 1010 #ifdef INET6
 1011                                       sizeof(struct sockaddr_in6)
 1012 #else
 1013                                       sizeof(struct sockaddr_in)
 1014 #endif
 1015                                       );
 1016 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
 1017     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
 1018 
 1019 /*
 1020  * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
 1021  *       will sofree() it when we return.
 1022  */
 1023 static void
 1024 udp_abort(netmsg_t msg)
 1025 {
 1026         struct socket *so = msg->abort.base.nm_so;
 1027         struct inpcb *inp;
 1028         int error;
 1029 
 1030         KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
 1031 
 1032         inp = so->so_pcb;
 1033         if (inp) {
 1034                 soisdisconnected(so);
 1035 
 1036                 udbinfo_barrier_set();
 1037                 in_pcbdetach(inp);
 1038                 udbinfo_barrier_rem();
 1039                 error = 0;
 1040         } else {
 1041                 error = EINVAL;
 1042         }
 1043         lwkt_replymsg(&msg->abort.base.lmsg, error);
 1044 }
 1045 
 1046 static void
 1047 udp_attach(netmsg_t msg)
 1048 {
 1049         struct socket *so = msg->attach.base.nm_so;
 1050         struct pru_attach_info *ai = msg->attach.nm_ai;
 1051         struct inpcb *inp;
 1052         int error;
 1053 
 1054         KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
 1055 
 1056         inp = so->so_pcb;
 1057         if (inp != NULL) {
 1058                 error = EINVAL;
 1059                 goto out;
 1060         }
 1061         error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
 1062         if (error)
 1063                 goto out;
 1064 
 1065         udbinfo_barrier_set();
 1066         error = in_pcballoc(so, &udbinfo);
 1067         udbinfo_barrier_rem();
 1068 
 1069         if (error)
 1070                 goto out;
 1071 
 1072         /*
 1073          * Set default port for protocol processing prior to bind/connect.
 1074          */
 1075         sosetport(so, netisr_cpuport(0));
 1076 
 1077         inp = (struct inpcb *)so->so_pcb;
 1078         inp->inp_vflag |= INP_IPV4;
 1079         inp->inp_ip_ttl = ip_defttl;
 1080         error = 0;
 1081 out:
 1082         lwkt_replymsg(&msg->attach.base.lmsg, error);
 1083 }
 1084 
 1085 static void
 1086 udp_bind(netmsg_t msg)
 1087 {
 1088         struct socket *so = msg->bind.base.nm_so;
 1089         struct sockaddr *nam = msg->bind.nm_nam;
 1090         struct thread *td = msg->bind.nm_td;
 1091         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
 1092         struct inpcb *inp;
 1093         int error;
 1094 
 1095         inp = so->so_pcb;
 1096         if (inp) {
 1097                 error = in_pcbbind(inp, nam, td);
 1098                 if (error == 0) {
 1099                         if (sin->sin_addr.s_addr != INADDR_ANY)
 1100                                 inp->inp_flags |= INP_WASBOUND_NOTANY;
 1101 
 1102                         udbinfo_barrier_set();
 1103                         in_pcbinswildcardhash(inp);
 1104                         udbinfo_barrier_rem();
 1105                 }
 1106         } else {
 1107                 error = EINVAL;
 1108         }
 1109         lwkt_replymsg(&msg->bind.base.lmsg, error);
 1110 }
 1111 
 1112 static void
 1113 udp_connect(netmsg_t msg)
 1114 {
 1115         struct socket *so = msg->connect.base.nm_so;
 1116         struct sockaddr *nam = msg->connect.nm_nam;
 1117         struct thread *td = msg->connect.nm_td;
 1118         struct inpcb *inp;
 1119         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
 1120         struct sockaddr_in *if_sin;
 1121         lwkt_port_t port;
 1122         int error;
 1123 
 1124         KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
 1125 
 1126         inp = so->so_pcb;
 1127         if (inp == NULL) {
 1128                 error = EINVAL;
 1129                 goto out;
 1130         }
 1131 
 1132         if (msg->connect.nm_flags & PRUC_RECONNECT) {
 1133                 panic("UDP does not support RECONNECT");
 1134 #ifdef notyet
 1135                 msg->connect.nm_flags &= ~PRUC_RECONNECT;
 1136                 in_pcblink(inp, &udbinfo);
 1137 #endif
 1138         }
 1139 
 1140         if (inp->inp_faddr.s_addr != INADDR_ANY) {
 1141                 error = EISCONN;
 1142                 goto out;
 1143         }
 1144         error = 0;
 1145 
 1146         /*
 1147          * Bind if we have to
 1148          */
 1149         if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
 1150             inp->inp_laddr.s_addr == INADDR_ANY) {
 1151                 error = in_pcbbind(inp, NULL, td);
 1152                 if (error)
 1153                         goto out;
 1154         }
 1155 
 1156         /*
 1157          * Calculate the correct protocol processing thread.  The connect
 1158          * operation must run there.
 1159          */
 1160         error = in_pcbladdr(inp, nam, &if_sin, td);
 1161         if (error)
 1162                 goto out;
 1163         if (!prison_remote_ip(td, nam)) {
 1164                 error = EAFNOSUPPORT; /* IPv6 only jail */
 1165                 goto out;
 1166         }
 1167 
 1168         port = udp_addrport(sin->sin_addr.s_addr, sin->sin_port,
 1169                             inp->inp_laddr.s_addr, inp->inp_lport);
 1170         if (port != &curthread->td_msgport) {
 1171 #ifdef notyet
 1172                 struct route *ro = &inp->inp_route;
 1173 
 1174                 /*
 1175                  * in_pcbladdr() may have allocated a route entry for us
 1176                  * on the current CPU, but we need a route entry on the
 1177                  * inpcb's owner CPU, so free it here.
 1178                  */
 1179                 if (ro->ro_rt != NULL)
 1180                         RTFREE(ro->ro_rt);
 1181                 bzero(ro, sizeof(*ro));
 1182 
 1183                 /*
 1184                  * We are moving the protocol processing port the socket
 1185                  * is on, we have to unlink here and re-link on the
 1186                  * target cpu.
 1187                  */
 1188                 in_pcbunlink(so->so_pcb, &udbinfo);
 1189                 /* in_pcbunlink(so->so_pcb, &udbinfo[mycpu->gd_cpuid]); */
 1190                 sosetport(so, port);
 1191                 msg->connect.nm_flags |= PRUC_RECONNECT;
 1192                 msg->connect.base.nm_dispatch = udp_connect;
 1193 
 1194                 lwkt_forwardmsg(port, &msg->connect.base.lmsg);
 1195                 /* msg invalid now */
 1196                 return;
 1197 #else
 1198                 panic("UDP activity should only be in netisr0");
 1199 #endif
 1200         }
 1201         KKASSERT(port == &curthread->td_msgport);
 1202         error = udp_connect_oncpu(so, td, sin, if_sin);
 1203 out:
 1204         KKASSERT(msg->connect.nm_m == NULL);
 1205         lwkt_replymsg(&msg->connect.base.lmsg, error);
 1206 }
 1207 
 1208 static int
 1209 udp_connect_oncpu(struct socket *so, struct thread *td,
 1210                   struct sockaddr_in *sin, struct sockaddr_in *if_sin)
 1211 {
 1212         struct inpcb *inp;
 1213         int error;
 1214 
 1215         udbinfo_barrier_set();
 1216 
 1217         inp = so->so_pcb;
 1218         if (inp->inp_flags & INP_WILDCARD)
 1219                 in_pcbremwildcardhash(inp);
 1220         error = in_pcbconnect(inp, (struct sockaddr *)sin, td);
 1221 
 1222         if (error == 0) {
 1223                 /*
 1224                  * No more errors can occur, finish adjusting the socket
 1225                  * and change the processing port to reflect the connected
 1226                  * socket.  Once set we can no longer safely mess with the
 1227                  * socket.
 1228                  */
 1229                 soisconnected(so);
 1230         } else if (error == EAFNOSUPPORT) {     /* connection dissolved */
 1231                 /*
 1232                  * Follow traditional BSD behavior and retain
 1233                  * the local port binding.  But, fix the old misbehavior
 1234                  * of overwriting any previously bound local address.
 1235                  */
 1236                 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
 1237                         inp->inp_laddr.s_addr = INADDR_ANY;
 1238                 in_pcbinswildcardhash(inp);
 1239         }
 1240 
 1241         udbinfo_barrier_rem();
 1242         return error;
 1243 }
 1244 
 1245 static void
 1246 udp_detach(netmsg_t msg)
 1247 {
 1248         struct socket *so = msg->detach.base.nm_so;
 1249         struct inpcb *inp;
 1250         int error;
 1251 
 1252         KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
 1253 
 1254         inp = so->so_pcb;
 1255         if (inp) {
 1256                 udbinfo_barrier_set();
 1257                 in_pcbdetach(inp);
 1258                 udbinfo_barrier_rem();
 1259                 error = 0;
 1260         } else {
 1261                 error = EINVAL;
 1262         }
 1263         lwkt_replymsg(&msg->detach.base.lmsg, error);
 1264 }
 1265 
 1266 static void
 1267 udp_disconnect(netmsg_t msg)
 1268 {
 1269         struct socket *so = msg->disconnect.base.nm_so;
 1270         struct route *ro;
 1271         struct inpcb *inp;
 1272         int error;
 1273 
 1274         KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
 1275 
 1276         inp = so->so_pcb;
 1277         if (inp == NULL) {
 1278                 error = EINVAL;
 1279                 goto out;
 1280         }
 1281         if (inp->inp_faddr.s_addr == INADDR_ANY) {
 1282                 error = ENOTCONN;
 1283                 goto out;
 1284         }
 1285 
 1286         soreference(so);
 1287 
 1288         udbinfo_barrier_set();
 1289         in_pcbdisconnect(inp);
 1290         udbinfo_barrier_rem();
 1291 
 1292         soclrstate(so, SS_ISCONNECTED);         /* XXX */
 1293         sofree(so);
 1294 
 1295         ro = &inp->inp_route;
 1296         if (ro->ro_rt != NULL)
 1297                 RTFREE(ro->ro_rt);
 1298         bzero(ro, sizeof(*ro));
 1299         error = 0;
 1300 out:
 1301         lwkt_replymsg(&msg->disconnect.base.lmsg, error);
 1302 }
 1303 
 1304 static void
 1305 udp_send(netmsg_t msg)
 1306 {
 1307         struct socket *so = msg->send.base.nm_so;
 1308         struct mbuf *m = msg->send.nm_m;
 1309         struct sockaddr *addr = msg->send.nm_addr;
 1310         int pru_flags = msg->send.nm_flags;
 1311         struct inpcb *inp;
 1312         int error;
 1313 
 1314         KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
 1315         KKASSERT(msg->send.nm_control == NULL);
 1316 
 1317         inp = so->so_pcb;
 1318         if (inp) {
 1319                 struct thread *td = msg->send.nm_td;
 1320                 int flags = 0;
 1321 
 1322                 if (pru_flags & PRUS_DONTROUTE)
 1323                         flags |= SO_DONTROUTE;
 1324                 error = udp_output(inp, m, addr, td, flags,
 1325                     (pru_flags & PRUS_HELDTD) ? TRUE : FALSE);
 1326         } else {
 1327                 if (pru_flags & PRUS_HELDTD)
 1328                         lwkt_rele(msg->send.nm_td);
 1329                 m_freem(m);
 1330                 error = EINVAL;
 1331         }
 1332 
 1333         if (pru_flags & PRUS_FREEADDR)
 1334                 kfree(addr, M_SONAME);
 1335 
 1336         if ((pru_flags & PRUS_NOREPLY) == 0)
 1337                 lwkt_replymsg(&msg->send.base.lmsg, error);
 1338 }
 1339 
 1340 void
 1341 udp_shutdown(netmsg_t msg)
 1342 {
 1343         struct socket *so = msg->shutdown.base.nm_so;
 1344         struct inpcb *inp;
 1345         int error;
 1346 
 1347         KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
 1348 
 1349         inp = so->so_pcb;
 1350         if (inp) {
 1351                 socantsendmore(so);
 1352                 error = 0;
 1353         } else {
 1354                 error = EINVAL;
 1355         }
 1356         lwkt_replymsg(&msg->shutdown.base.lmsg, error);
 1357 }
 1358 
 1359 void
 1360 udbinfo_lock(void)
 1361 {
 1362         lwkt_serialize_enter(&udbinfo_slize);
 1363 }
 1364 
 1365 void
 1366 udbinfo_unlock(void)
 1367 {
 1368         lwkt_serialize_exit(&udbinfo_slize);
 1369 }
 1370 
 1371 void
 1372 udbinfo_barrier_set(void)
 1373 {
 1374         netisr_barrier_set(udbinfo_br);
 1375         udbinfo_lock();
 1376 }
 1377 
 1378 void
 1379 udbinfo_barrier_rem(void)
 1380 {
 1381         udbinfo_unlock();
 1382         netisr_barrier_rem(udbinfo_br);
 1383 }
 1384 
 1385 struct pr_usrreqs udp_usrreqs = {
 1386         .pru_abort = udp_abort,
 1387         .pru_accept = pr_generic_notsupp,
 1388         .pru_attach = udp_attach,
 1389         .pru_bind = udp_bind,
 1390         .pru_connect = udp_connect,
 1391         .pru_connect2 = pr_generic_notsupp,
 1392         .pru_control = in_control_dispatch,
 1393         .pru_detach = udp_detach,
 1394         .pru_disconnect = udp_disconnect,
 1395         .pru_listen = pr_generic_notsupp,
 1396         .pru_peeraddr = in_setpeeraddr_dispatch,
 1397         .pru_rcvd = pr_generic_notsupp,
 1398         .pru_rcvoob = pr_generic_notsupp,
 1399         .pru_send = udp_send,
 1400         .pru_sense = pru_sense_null,
 1401         .pru_shutdown = udp_shutdown,
 1402         .pru_sockaddr = in_setsockaddr_dispatch,
 1403         .pru_sosend = sosendudp,
 1404         .pru_soreceive = soreceive
 1405 };
 1406 

Cache object: 63e462ae25f94328519518217bd48c03


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