]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/udp_usrreq.c
Move all HDMI-related stuff to hdmi_if.m, hdmi.h is not required
[FreeBSD/FreeBSD.git] / sys / netinet / udp_usrreq.c
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3  *      The Regents of the University of California.
4  * Copyright (c) 2008 Robert N. M. Watson
5  * Copyright (c) 2010-2011 Juniper Networks, Inc.
6  * Copyright (c) 2014 Kevin Lo
7  * All rights reserved.
8  *
9  * Portions of this software were developed by Robert N. M. Watson under
10  * contract to Juniper Networks, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include "opt_ipfw.h"
43 #include "opt_inet.h"
44 #include "opt_inet6.h"
45 #include "opt_ipsec.h"
46 #include "opt_rss.h"
47
48 #include <sys/param.h>
49 #include <sys/domain.h>
50 #include <sys/eventhandler.h>
51 #include <sys/jail.h>
52 #include <sys/kernel.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/mbuf.h>
56 #include <sys/priv.h>
57 #include <sys/proc.h>
58 #include <sys/protosw.h>
59 #include <sys/sdt.h>
60 #include <sys/signalvar.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/sx.h>
64 #include <sys/sysctl.h>
65 #include <sys/syslog.h>
66 #include <sys/systm.h>
67
68 #include <vm/uma.h>
69
70 #include <net/if.h>
71 #include <net/if_var.h>
72 #include <net/route.h>
73 #include <net/rss_config.h>
74
75 #include <netinet/in.h>
76 #include <netinet/in_kdtrace.h>
77 #include <netinet/in_pcb.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #ifdef INET6
82 #include <netinet/ip6.h>
83 #endif
84 #include <netinet/ip_icmp.h>
85 #include <netinet/icmp_var.h>
86 #include <netinet/ip_var.h>
87 #include <netinet/ip_options.h>
88 #ifdef INET6
89 #include <netinet6/ip6_var.h>
90 #endif
91 #include <netinet/udp.h>
92 #include <netinet/udp_var.h>
93 #include <netinet/udplite.h>
94 #include <netinet/in_rss.h>
95
96 #ifdef IPSEC
97 #include <netipsec/ipsec.h>
98 #include <netipsec/esp.h>
99 #endif
100
101 #include <machine/in_cksum.h>
102
103 #include <security/mac/mac_framework.h>
104
105 /*
106  * UDP and UDP-Lite protocols implementation.
107  * Per RFC 768, August, 1980.
108  * Per RFC 3828, July, 2004.
109  */
110
111 /*
112  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
113  * removes the only data integrity mechanism for packets and malformed
114  * packets that would otherwise be discarded due to bad checksums, and may
115  * cause problems (especially for NFS data blocks).
116  */
117 VNET_DEFINE(int, udp_cksum) = 1;
118 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW,
119     &VNET_NAME(udp_cksum), 0, "compute udp checksum");
120
121 int     udp_log_in_vain = 0;
122 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
123     &udp_log_in_vain, 0, "Log all incoming UDP packets");
124
125 VNET_DEFINE(int, udp_blackhole) = 0;
126 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
127     &VNET_NAME(udp_blackhole), 0,
128     "Do not send port unreachables for refused connects");
129
130 u_long  udp_sendspace = 9216;           /* really max datagram size */
131                                         /* 40 1K datagrams */
132 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
133     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
134
135 u_long  udp_recvspace = 40 * (1024 +
136 #ifdef INET6
137                                       sizeof(struct sockaddr_in6)
138 #else
139                                       sizeof(struct sockaddr_in)
140 #endif
141                                       );
142
143 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
144     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
145
146 VNET_DEFINE(struct inpcbhead, udb);             /* from udp_var.h */
147 VNET_DEFINE(struct inpcbinfo, udbinfo);
148 VNET_DEFINE(struct inpcbhead, ulitecb);
149 VNET_DEFINE(struct inpcbinfo, ulitecbinfo);
150 static VNET_DEFINE(uma_zone_t, udpcb_zone);
151 #define V_udpcb_zone                    VNET(udpcb_zone)
152
153 #ifndef UDBHASHSIZE
154 #define UDBHASHSIZE     128
155 #endif
156
157 VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat);          /* from udp_var.h */
158 VNET_PCPUSTAT_SYSINIT(udpstat);
159 SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat,
160     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
161
162 #ifdef VIMAGE
163 VNET_PCPUSTAT_SYSUNINIT(udpstat);
164 #endif /* VIMAGE */
165 #ifdef INET
166 static void     udp_detach(struct socket *so);
167 static int      udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
168                     struct mbuf *, struct thread *);
169 #endif
170
171 #ifdef IPSEC
172 #ifdef IPSEC_NAT_T
173 #define UF_ESPINUDP_ALL (UF_ESPINUDP_NON_IKE|UF_ESPINUDP)
174 #ifdef INET
175 static struct mbuf *udp4_espdecap(struct inpcb *, struct mbuf *, int);
176 #endif
177 #endif /* IPSEC_NAT_T */
178 #endif /* IPSEC */
179
180 static void
181 udp_zone_change(void *tag)
182 {
183
184         uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets);
185         uma_zone_set_max(V_udpcb_zone, maxsockets);
186 }
187
188 static int
189 udp_inpcb_init(void *mem, int size, int flags)
190 {
191         struct inpcb *inp;
192
193         inp = mem;
194         INP_LOCK_INIT(inp, "inp", "udpinp");
195         return (0);
196 }
197
198 static int
199 udplite_inpcb_init(void *mem, int size, int flags)
200 {
201         struct inpcb *inp;
202
203         inp = mem;
204         INP_LOCK_INIT(inp, "inp", "udpliteinp");
205         return (0);
206 }
207
208 void
209 udp_init(void)
210 {
211
212         /*
213          * For now default to 2-tuple UDP hashing - until the fragment
214          * reassembly code can also update the flowid.
215          *
216          * Once we can calculate the flowid that way and re-establish
217          * a 4-tuple, flip this to 4-tuple.
218          */
219         in_pcbinfo_init(&V_udbinfo, "udp", &V_udb, UDBHASHSIZE, UDBHASHSIZE,
220             "udp_inpcb", udp_inpcb_init, NULL, 0,
221             IPI_HASHFIELDS_2TUPLE);
222         V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb),
223             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
224         uma_zone_set_max(V_udpcb_zone, maxsockets);
225         uma_zone_set_warning(V_udpcb_zone, "kern.ipc.maxsockets limit reached");
226         EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
227             EVENTHANDLER_PRI_ANY);
228 }
229
230 void
231 udplite_init(void)
232 {
233
234         in_pcbinfo_init(&V_ulitecbinfo, "udplite", &V_ulitecb, UDBHASHSIZE,
235             UDBHASHSIZE, "udplite_inpcb", udplite_inpcb_init, NULL,
236             0, IPI_HASHFIELDS_2TUPLE);
237 }
238
239 /*
240  * Kernel module interface for updating udpstat.  The argument is an index
241  * into udpstat treated as an array of u_long.  While this encodes the
242  * general layout of udpstat into the caller, it doesn't encode its location,
243  * so that future changes to add, for example, per-CPU stats support won't
244  * cause binary compatibility problems for kernel modules.
245  */
246 void
247 kmod_udpstat_inc(int statnum)
248 {
249
250         counter_u64_add(VNET(udpstat)[statnum], 1);
251 }
252
253 int
254 udp_newudpcb(struct inpcb *inp)
255 {
256         struct udpcb *up;
257
258         up = uma_zalloc(V_udpcb_zone, M_NOWAIT | M_ZERO);
259         if (up == NULL)
260                 return (ENOBUFS);
261         inp->inp_ppcb = up;
262         return (0);
263 }
264
265 void
266 udp_discardcb(struct udpcb *up)
267 {
268
269         uma_zfree(V_udpcb_zone, up);
270 }
271
272 #ifdef VIMAGE
273 void
274 udp_destroy(void)
275 {
276
277         in_pcbinfo_destroy(&V_udbinfo);
278         uma_zdestroy(V_udpcb_zone);
279 }
280
281 void
282 udplite_destroy(void)
283 {
284
285         in_pcbinfo_destroy(&V_ulitecbinfo);
286 }
287 #endif
288
289 #ifdef INET
290 /*
291  * Subroutine of udp_input(), which appends the provided mbuf chain to the
292  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
293  * contains the source address.  If the socket ends up being an IPv6 socket,
294  * udp_append() will convert to a sockaddr_in6 before passing the address
295  * into the socket code.
296  *
297  * In the normal case udp_append() will return 0, indicating that you
298  * must unlock the inp. However if a tunneling protocol is in place we increment
299  * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we
300  * then decrement the reference count. If the inp_rele returns 1, indicating the
301  * inp is gone, we return that to the caller to tell them *not* to unlock
302  * the inp. In the case of multi-cast this will cause the distribution
303  * to stop (though most tunneling protocols known currently do *not* use
304  * multicast).
305  */
306 static int
307 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
308     struct sockaddr_in *udp_in)
309 {
310         struct sockaddr *append_sa;
311         struct socket *so;
312         struct mbuf *opts = 0;
313 #ifdef INET6
314         struct sockaddr_in6 udp_in6;
315 #endif
316         struct udpcb *up;
317
318         INP_LOCK_ASSERT(inp);
319
320         /*
321          * Engage the tunneling protocol.
322          */
323         up = intoudpcb(inp);
324         if (up->u_tun_func != NULL) {
325                 in_pcbref(inp);
326                 INP_RUNLOCK(inp);
327                 (*up->u_tun_func)(n, off, inp, (struct sockaddr *)udp_in,
328                     up->u_tun_ctx);
329                 INP_RLOCK(inp);
330                 return (in_pcbrele_rlocked(inp));
331         }
332
333         off += sizeof(struct udphdr);
334
335 #ifdef IPSEC
336         /* Check AH/ESP integrity. */
337         if (ipsec4_in_reject(n, inp)) {
338                 m_freem(n);
339                 return (0);
340         }
341 #ifdef IPSEC_NAT_T
342         up = intoudpcb(inp);
343         KASSERT(up != NULL, ("%s: udpcb NULL", __func__));
344         if (up->u_flags & UF_ESPINUDP_ALL) {    /* IPSec UDP encaps. */
345                 n = udp4_espdecap(inp, n, off);
346                 if (n == NULL)                          /* Consumed. */
347                         return (0);
348         }
349 #endif /* IPSEC_NAT_T */
350 #endif /* IPSEC */
351 #ifdef MAC
352         if (mac_inpcb_check_deliver(inp, n) != 0) {
353                 m_freem(n);
354                 return (0);
355         }
356 #endif /* MAC */
357         if (inp->inp_flags & INP_CONTROLOPTS ||
358             inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
359 #ifdef INET6
360                 if (inp->inp_vflag & INP_IPV6)
361                         (void)ip6_savecontrol_v4(inp, n, &opts, NULL);
362                 else
363 #endif /* INET6 */
364                         ip_savecontrol(inp, &opts, ip, n);
365         }
366 #ifdef INET6
367         if (inp->inp_vflag & INP_IPV6) {
368                 bzero(&udp_in6, sizeof(udp_in6));
369                 udp_in6.sin6_len = sizeof(udp_in6);
370                 udp_in6.sin6_family = AF_INET6;
371                 in6_sin_2_v4mapsin6(udp_in, &udp_in6);
372                 append_sa = (struct sockaddr *)&udp_in6;
373         } else
374 #endif /* INET6 */
375                 append_sa = (struct sockaddr *)udp_in;
376         m_adj(n, off);
377
378         so = inp->inp_socket;
379         SOCKBUF_LOCK(&so->so_rcv);
380         if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
381                 SOCKBUF_UNLOCK(&so->so_rcv);
382                 m_freem(n);
383                 if (opts)
384                         m_freem(opts);
385                 UDPSTAT_INC(udps_fullsock);
386         } else
387                 sorwakeup_locked(so);
388         return (0);
389 }
390
391 int
392 udp_input(struct mbuf **mp, int *offp, int proto)
393 {
394         struct ip *ip;
395         struct udphdr *uh;
396         struct ifnet *ifp;
397         struct inpcb *inp;
398         uint16_t len, ip_len;
399         struct inpcbinfo *pcbinfo;
400         struct ip save_ip;
401         struct sockaddr_in udp_in;
402         struct mbuf *m;
403         struct m_tag *fwd_tag;
404         int cscov_partial, iphlen;
405
406         m = *mp;
407         iphlen = *offp;
408         ifp = m->m_pkthdr.rcvif;
409         *mp = NULL;
410         UDPSTAT_INC(udps_ipackets);
411
412         /*
413          * Strip IP options, if any; should skip this, make available to
414          * user, and use on returned packets, but we don't yet have a way to
415          * check the checksum with options still present.
416          */
417         if (iphlen > sizeof (struct ip)) {
418                 ip_stripoptions(m);
419                 iphlen = sizeof(struct ip);
420         }
421
422         /*
423          * Get IP and UDP header together in first mbuf.
424          */
425         ip = mtod(m, struct ip *);
426         if (m->m_len < iphlen + sizeof(struct udphdr)) {
427                 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) {
428                         UDPSTAT_INC(udps_hdrops);
429                         return (IPPROTO_DONE);
430                 }
431                 ip = mtod(m, struct ip *);
432         }
433         uh = (struct udphdr *)((caddr_t)ip + iphlen);
434         cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0;
435
436         /*
437          * Destination port of 0 is illegal, based on RFC768.
438          */
439         if (uh->uh_dport == 0)
440                 goto badunlocked;
441
442         /*
443          * Construct sockaddr format source address.  Stuff source address
444          * and datagram in user buffer.
445          */
446         bzero(&udp_in, sizeof(udp_in));
447         udp_in.sin_len = sizeof(udp_in);
448         udp_in.sin_family = AF_INET;
449         udp_in.sin_port = uh->uh_sport;
450         udp_in.sin_addr = ip->ip_src;
451
452         /*
453          * Make mbuf data length reflect UDP length.  If not enough data to
454          * reflect UDP length, drop.
455          */
456         len = ntohs((u_short)uh->uh_ulen);
457         ip_len = ntohs(ip->ip_len) - iphlen;
458         if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) {
459                 /* Zero means checksum over the complete packet. */
460                 if (len == 0)
461                         len = ip_len;
462                 cscov_partial = 0;
463         }
464         if (ip_len != len) {
465                 if (len > ip_len || len < sizeof(struct udphdr)) {
466                         UDPSTAT_INC(udps_badlen);
467                         goto badunlocked;
468                 }
469                 if (proto == IPPROTO_UDP)
470                         m_adj(m, len - ip_len);
471         }
472
473         /*
474          * Save a copy of the IP header in case we want restore it for
475          * sending an ICMP error message in response.
476          */
477         if (!V_udp_blackhole)
478                 save_ip = *ip;
479         else
480                 memset(&save_ip, 0, sizeof(save_ip));
481
482         /*
483          * Checksum extended UDP header and data.
484          */
485         if (uh->uh_sum) {
486                 u_short uh_sum;
487
488                 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
489                     !cscov_partial) {
490                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
491                                 uh_sum = m->m_pkthdr.csum_data;
492                         else
493                                 uh_sum = in_pseudo(ip->ip_src.s_addr,
494                                     ip->ip_dst.s_addr, htonl((u_short)len +
495                                     m->m_pkthdr.csum_data + proto));
496                         uh_sum ^= 0xffff;
497                 } else {
498                         char b[9];
499
500                         bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
501                         bzero(((struct ipovly *)ip)->ih_x1, 9);
502                         ((struct ipovly *)ip)->ih_len = (proto == IPPROTO_UDP) ?
503                             uh->uh_ulen : htons(ip_len);
504                         uh_sum = in_cksum(m, len + sizeof (struct ip));
505                         bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
506                 }
507                 if (uh_sum) {
508                         UDPSTAT_INC(udps_badsum);
509                         m_freem(m);
510                         return (IPPROTO_DONE);
511                 }
512         } else {
513                 if (proto == IPPROTO_UDP) {
514                         UDPSTAT_INC(udps_nosum);
515                 } else {
516                         /* UDPLite requires a checksum */
517                         /* XXX: What is the right UDPLite MIB counter here? */
518                         m_freem(m);
519                         return (IPPROTO_DONE);
520                 }
521         }
522
523         pcbinfo = udp_get_inpcbinfo(proto);
524         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
525             in_broadcast(ip->ip_dst, ifp)) {
526                 struct inpcb *last;
527                 struct inpcbhead *pcblist;
528                 struct ip_moptions *imo;
529
530                 INP_INFO_RLOCK(pcbinfo);
531                 pcblist = udp_get_pcblist(proto);
532                 last = NULL;
533                 LIST_FOREACH(inp, pcblist, inp_list) {
534                         if (inp->inp_lport != uh->uh_dport)
535                                 continue;
536 #ifdef INET6
537                         if ((inp->inp_vflag & INP_IPV4) == 0)
538                                 continue;
539 #endif
540                         if (inp->inp_laddr.s_addr != INADDR_ANY &&
541                             inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
542                                 continue;
543                         if (inp->inp_faddr.s_addr != INADDR_ANY &&
544                             inp->inp_faddr.s_addr != ip->ip_src.s_addr)
545                                 continue;
546                         if (inp->inp_fport != 0 &&
547                             inp->inp_fport != uh->uh_sport)
548                                 continue;
549
550                         INP_RLOCK(inp);
551
552                         /*
553                          * XXXRW: Because we weren't holding either the inpcb
554                          * or the hash lock when we checked for a match
555                          * before, we should probably recheck now that the
556                          * inpcb lock is held.
557                          */
558
559                         /*
560                          * Handle socket delivery policy for any-source
561                          * and source-specific multicast. [RFC3678]
562                          */
563                         imo = inp->inp_moptions;
564                         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
565                                 struct sockaddr_in       group;
566                                 int                      blocked;
567                                 if (imo == NULL) {
568                                         INP_RUNLOCK(inp);
569                                         continue;
570                                 }
571                                 bzero(&group, sizeof(struct sockaddr_in));
572                                 group.sin_len = sizeof(struct sockaddr_in);
573                                 group.sin_family = AF_INET;
574                                 group.sin_addr = ip->ip_dst;
575
576                                 blocked = imo_multi_filter(imo, ifp,
577                                         (struct sockaddr *)&group,
578                                         (struct sockaddr *)&udp_in);
579                                 if (blocked != MCAST_PASS) {
580                                         if (blocked == MCAST_NOTGMEMBER)
581                                                 IPSTAT_INC(ips_notmember);
582                                         if (blocked == MCAST_NOTSMEMBER ||
583                                             blocked == MCAST_MUTED)
584                                                 UDPSTAT_INC(udps_filtermcast);
585                                         INP_RUNLOCK(inp);
586                                         continue;
587                                 }
588                         }
589                         if (last != NULL) {
590                                 struct mbuf *n;
591
592                                 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
593                                         UDP_PROBE(receive, NULL, last, ip,
594                                             last, uh);
595                                         if (udp_append(last, ip, n, iphlen,
596                                                 &udp_in)) {
597                                                 goto inp_lost;
598                                         }
599                                 }
600                                 INP_RUNLOCK(last);
601                         }
602                         last = inp;
603                         /*
604                          * Don't look for additional matches if this one does
605                          * not have either the SO_REUSEPORT or SO_REUSEADDR
606                          * socket options set.  This heuristic avoids
607                          * searching through all pcbs in the common case of a
608                          * non-shared port.  It assumes that an application
609                          * will never clear these options after setting them.
610                          */
611                         if ((last->inp_socket->so_options &
612                             (SO_REUSEPORT|SO_REUSEADDR)) == 0)
613                                 break;
614                 }
615
616                 if (last == NULL) {
617                         /*
618                          * No matching pcb found; discard datagram.  (No need
619                          * to send an ICMP Port Unreachable for a broadcast
620                          * or multicast datgram.)
621                          */
622                         UDPSTAT_INC(udps_noportbcast);
623                         if (inp)
624                                 INP_RUNLOCK(inp);
625                         INP_INFO_RUNLOCK(pcbinfo);
626                         goto badunlocked;
627                 }
628                 UDP_PROBE(receive, NULL, last, ip, last, uh);
629                 if (udp_append(last, ip, m, iphlen, &udp_in) == 0) 
630                         INP_RUNLOCK(last);
631         inp_lost:
632                 INP_INFO_RUNLOCK(pcbinfo);
633                 return (IPPROTO_DONE);
634         }
635
636         /*
637          * Locate pcb for datagram.
638          */
639
640         /*
641          * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
642          */
643         if ((m->m_flags & M_IP_NEXTHOP) &&
644             (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
645                 struct sockaddr_in *next_hop;
646
647                 next_hop = (struct sockaddr_in *)(fwd_tag + 1);
648
649                 /*
650                  * Transparently forwarded. Pretend to be the destination.
651                  * Already got one like this?
652                  */
653                 inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
654                     ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m);
655                 if (!inp) {
656                         /*
657                          * It's new.  Try to find the ambushing socket.
658                          * Because we've rewritten the destination address,
659                          * any hardware-generated hash is ignored.
660                          */
661                         inp = in_pcblookup(pcbinfo, ip->ip_src,
662                             uh->uh_sport, next_hop->sin_addr,
663                             next_hop->sin_port ? htons(next_hop->sin_port) :
664                             uh->uh_dport, INPLOOKUP_WILDCARD |
665                             INPLOOKUP_RLOCKPCB, ifp);
666                 }
667                 /* Remove the tag from the packet. We don't need it anymore. */
668                 m_tag_delete(m, fwd_tag);
669                 m->m_flags &= ~M_IP_NEXTHOP;
670         } else
671                 inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
672                     ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD |
673                     INPLOOKUP_RLOCKPCB, ifp, m);
674         if (inp == NULL) {
675                 if (udp_log_in_vain) {
676                         char buf[4*sizeof "123"];
677
678                         strcpy(buf, inet_ntoa(ip->ip_dst));
679                         log(LOG_INFO,
680                             "Connection attempt to UDP %s:%d from %s:%d\n",
681                             buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
682                             ntohs(uh->uh_sport));
683                 }
684                 UDPSTAT_INC(udps_noport);
685                 if (m->m_flags & (M_BCAST | M_MCAST)) {
686                         UDPSTAT_INC(udps_noportbcast);
687                         goto badunlocked;
688                 }
689                 if (V_udp_blackhole)
690                         goto badunlocked;
691                 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
692                         goto badunlocked;
693                 *ip = save_ip;
694                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
695                 return (IPPROTO_DONE);
696         }
697
698         /*
699          * Check the minimum TTL for socket.
700          */
701         INP_RLOCK_ASSERT(inp);
702         if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
703                 INP_RUNLOCK(inp);
704                 m_freem(m);
705                 return (IPPROTO_DONE);
706         }
707         if (cscov_partial) {
708                 struct udpcb *up;
709
710                 up = intoudpcb(inp);
711                 if (up->u_rxcslen == 0 || up->u_rxcslen > len) {
712                         INP_RUNLOCK(inp);
713                         m_freem(m);
714                         return (IPPROTO_DONE);
715                 }
716         }
717
718         UDP_PROBE(receive, NULL, inp, ip, inp, uh);
719         if (udp_append(inp, ip, m, iphlen, &udp_in) == 0) 
720                 INP_RUNLOCK(inp);
721         return (IPPROTO_DONE);
722
723 badunlocked:
724         m_freem(m);
725         return (IPPROTO_DONE);
726 }
727 #endif /* INET */
728
729 /*
730  * Notify a udp user of an asynchronous error; just wake up so that they can
731  * collect error status.
732  */
733 struct inpcb *
734 udp_notify(struct inpcb *inp, int errno)
735 {
736
737         /*
738          * While udp_ctlinput() always calls udp_notify() with a read lock
739          * when invoking it directly, in_pcbnotifyall() currently uses write
740          * locks due to sharing code with TCP.  For now, accept either a read
741          * or a write lock, but a read lock is sufficient.
742          */
743         INP_LOCK_ASSERT(inp);
744
745         inp->inp_socket->so_error = errno;
746         sorwakeup(inp->inp_socket);
747         sowwakeup(inp->inp_socket);
748         return (inp);
749 }
750
751 #ifdef INET
752 static void
753 udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip,
754     struct inpcbinfo *pcbinfo)
755 {
756         struct ip *ip = vip;
757         struct udphdr *uh;
758         struct in_addr faddr;
759         struct inpcb *inp;
760
761         faddr = ((struct sockaddr_in *)sa)->sin_addr;
762         if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
763                 return;
764
765         /*
766          * Redirects don't need to be handled up here.
767          */
768         if (PRC_IS_REDIRECT(cmd))
769                 return;
770
771         /*
772          * Hostdead is ugly because it goes linearly through all PCBs.
773          *
774          * XXX: We never get this from ICMP, otherwise it makes an excellent
775          * DoS attack on machines with many connections.
776          */
777         if (cmd == PRC_HOSTDEAD)
778                 ip = NULL;
779         else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
780                 return;
781         if (ip != NULL) {
782                 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
783                 inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport,
784                     ip->ip_src, uh->uh_sport, INPLOOKUP_RLOCKPCB, NULL);
785                 if (inp != NULL) {
786                         INP_RLOCK_ASSERT(inp);
787                         if (inp->inp_socket != NULL) {
788                                 udp_notify(inp, inetctlerrmap[cmd]);
789                         }
790                         INP_RUNLOCK(inp);
791                 }
792         } else
793                 in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd],
794                     udp_notify);
795 }
796 void
797 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
798 {
799
800         return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo));
801 }
802
803 void
804 udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip)
805 {
806
807         return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo));
808 }
809 #endif /* INET */
810
811 static int
812 udp_pcblist(SYSCTL_HANDLER_ARGS)
813 {
814         int error, i, n;
815         struct inpcb *inp, **inp_list;
816         inp_gen_t gencnt;
817         struct xinpgen xig;
818
819         /*
820          * The process of preparing the PCB list is too time-consuming and
821          * resource-intensive to repeat twice on every request.
822          */
823         if (req->oldptr == 0) {
824                 n = V_udbinfo.ipi_count;
825                 n += imax(n / 8, 10);
826                 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
827                 return (0);
828         }
829
830         if (req->newptr != 0)
831                 return (EPERM);
832
833         /*
834          * OK, now we're committed to doing something.
835          */
836         INP_INFO_RLOCK(&V_udbinfo);
837         gencnt = V_udbinfo.ipi_gencnt;
838         n = V_udbinfo.ipi_count;
839         INP_INFO_RUNLOCK(&V_udbinfo);
840
841         error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
842                 + n * sizeof(struct xinpcb));
843         if (error != 0)
844                 return (error);
845
846         xig.xig_len = sizeof xig;
847         xig.xig_count = n;
848         xig.xig_gen = gencnt;
849         xig.xig_sogen = so_gencnt;
850         error = SYSCTL_OUT(req, &xig, sizeof xig);
851         if (error)
852                 return (error);
853
854         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
855         if (inp_list == 0)
856                 return (ENOMEM);
857
858         INP_INFO_RLOCK(&V_udbinfo);
859         for (inp = LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n;
860              inp = LIST_NEXT(inp, inp_list)) {
861                 INP_WLOCK(inp);
862                 if (inp->inp_gencnt <= gencnt &&
863                     cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
864                         in_pcbref(inp);
865                         inp_list[i++] = inp;
866                 }
867                 INP_WUNLOCK(inp);
868         }
869         INP_INFO_RUNLOCK(&V_udbinfo);
870         n = i;
871
872         error = 0;
873         for (i = 0; i < n; i++) {
874                 inp = inp_list[i];
875                 INP_RLOCK(inp);
876                 if (inp->inp_gencnt <= gencnt) {
877                         struct xinpcb xi;
878
879                         bzero(&xi, sizeof(xi));
880                         xi.xi_len = sizeof xi;
881                         /* XXX should avoid extra copy */
882                         bcopy(inp, &xi.xi_inp, sizeof *inp);
883                         if (inp->inp_socket)
884                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
885                         xi.xi_inp.inp_gencnt = inp->inp_gencnt;
886                         INP_RUNLOCK(inp);
887                         error = SYSCTL_OUT(req, &xi, sizeof xi);
888                 } else
889                         INP_RUNLOCK(inp);
890         }
891         INP_INFO_WLOCK(&V_udbinfo);
892         for (i = 0; i < n; i++) {
893                 inp = inp_list[i];
894                 INP_RLOCK(inp);
895                 if (!in_pcbrele_rlocked(inp))
896                         INP_RUNLOCK(inp);
897         }
898         INP_INFO_WUNLOCK(&V_udbinfo);
899
900         if (!error) {
901                 /*
902                  * Give the user an updated idea of our state.  If the
903                  * generation differs from what we told her before, she knows
904                  * that something happened while we were processing this
905                  * request, and it might be necessary to retry.
906                  */
907                 INP_INFO_RLOCK(&V_udbinfo);
908                 xig.xig_gen = V_udbinfo.ipi_gencnt;
909                 xig.xig_sogen = so_gencnt;
910                 xig.xig_count = V_udbinfo.ipi_count;
911                 INP_INFO_RUNLOCK(&V_udbinfo);
912                 error = SYSCTL_OUT(req, &xig, sizeof xig);
913         }
914         free(inp_list, M_TEMP);
915         return (error);
916 }
917
918 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
919     CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
920     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
921
922 #ifdef INET
923 static int
924 udp_getcred(SYSCTL_HANDLER_ARGS)
925 {
926         struct xucred xuc;
927         struct sockaddr_in addrs[2];
928         struct inpcb *inp;
929         int error;
930
931         error = priv_check(req->td, PRIV_NETINET_GETCRED);
932         if (error)
933                 return (error);
934         error = SYSCTL_IN(req, addrs, sizeof(addrs));
935         if (error)
936                 return (error);
937         inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
938             addrs[0].sin_addr, addrs[0].sin_port,
939             INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
940         if (inp != NULL) {
941                 INP_RLOCK_ASSERT(inp);
942                 if (inp->inp_socket == NULL)
943                         error = ENOENT;
944                 if (error == 0)
945                         error = cr_canseeinpcb(req->td->td_ucred, inp);
946                 if (error == 0)
947                         cru2x(inp->inp_cred, &xuc);
948                 INP_RUNLOCK(inp);
949         } else
950                 error = ENOENT;
951         if (error == 0)
952                 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
953         return (error);
954 }
955
956 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
957     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
958     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
959 #endif /* INET */
960
961 int
962 udp_ctloutput(struct socket *so, struct sockopt *sopt)
963 {
964         struct inpcb *inp;
965         struct udpcb *up;
966         int isudplite, error, optval;
967
968         error = 0;
969         isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0;
970         inp = sotoinpcb(so);
971         KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
972         INP_WLOCK(inp);
973         if (sopt->sopt_level != so->so_proto->pr_protocol) {
974 #ifdef INET6
975                 if (INP_CHECK_SOCKAF(so, AF_INET6)) {
976                         INP_WUNLOCK(inp);
977                         error = ip6_ctloutput(so, sopt);
978                 }
979 #endif
980 #if defined(INET) && defined(INET6)
981                 else
982 #endif
983 #ifdef INET
984                 {
985                         INP_WUNLOCK(inp);
986                         error = ip_ctloutput(so, sopt);
987                 }
988 #endif
989                 return (error);
990         }
991
992         switch (sopt->sopt_dir) {
993         case SOPT_SET:
994                 switch (sopt->sopt_name) {
995                 case UDP_ENCAP:
996                         INP_WUNLOCK(inp);
997                         error = sooptcopyin(sopt, &optval, sizeof optval,
998                                             sizeof optval);
999                         if (error)
1000                                 break;
1001                         inp = sotoinpcb(so);
1002                         KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1003                         INP_WLOCK(inp);
1004 #ifdef IPSEC_NAT_T
1005                         up = intoudpcb(inp);
1006                         KASSERT(up != NULL, ("%s: up == NULL", __func__));
1007 #endif
1008                         switch (optval) {
1009                         case 0:
1010                                 /* Clear all UDP encap. */
1011 #ifdef IPSEC_NAT_T
1012                                 up->u_flags &= ~UF_ESPINUDP_ALL;
1013 #endif
1014                                 break;
1015 #ifdef IPSEC_NAT_T
1016                         case UDP_ENCAP_ESPINUDP:
1017                         case UDP_ENCAP_ESPINUDP_NON_IKE:
1018                                 up->u_flags &= ~UF_ESPINUDP_ALL;
1019                                 if (optval == UDP_ENCAP_ESPINUDP)
1020                                         up->u_flags |= UF_ESPINUDP;
1021                                 else if (optval == UDP_ENCAP_ESPINUDP_NON_IKE)
1022                                         up->u_flags |= UF_ESPINUDP_NON_IKE;
1023                                 break;
1024 #endif
1025                         default:
1026                                 error = EINVAL;
1027                                 break;
1028                         }
1029                         INP_WUNLOCK(inp);
1030                         break;
1031                 case UDPLITE_SEND_CSCOV:
1032                 case UDPLITE_RECV_CSCOV:
1033                         if (!isudplite) {
1034                                 INP_WUNLOCK(inp);
1035                                 error = ENOPROTOOPT;
1036                                 break;
1037                         }
1038                         INP_WUNLOCK(inp);
1039                         error = sooptcopyin(sopt, &optval, sizeof(optval),
1040                             sizeof(optval));
1041                         if (error != 0)
1042                                 break;
1043                         inp = sotoinpcb(so);
1044                         KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1045                         INP_WLOCK(inp);
1046                         up = intoudpcb(inp);
1047                         KASSERT(up != NULL, ("%s: up == NULL", __func__));
1048                         if ((optval != 0 && optval < 8) || (optval > 65535)) {
1049                                 INP_WUNLOCK(inp);
1050                                 error = EINVAL;
1051                                 break;
1052                         }
1053                         if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1054                                 up->u_txcslen = optval;
1055                         else
1056                                 up->u_rxcslen = optval;
1057                         INP_WUNLOCK(inp);
1058                         break;
1059                 default:
1060                         INP_WUNLOCK(inp);
1061                         error = ENOPROTOOPT;
1062                         break;
1063                 }
1064                 break;
1065         case SOPT_GET:
1066                 switch (sopt->sopt_name) {
1067 #ifdef IPSEC_NAT_T
1068                 case UDP_ENCAP:
1069                         up = intoudpcb(inp);
1070                         KASSERT(up != NULL, ("%s: up == NULL", __func__));
1071                         optval = up->u_flags & UF_ESPINUDP_ALL;
1072                         INP_WUNLOCK(inp);
1073                         error = sooptcopyout(sopt, &optval, sizeof optval);
1074                         break;
1075 #endif
1076                 case UDPLITE_SEND_CSCOV:
1077                 case UDPLITE_RECV_CSCOV:
1078                         if (!isudplite) {
1079                                 INP_WUNLOCK(inp);
1080                                 error = ENOPROTOOPT;
1081                                 break;
1082                         }
1083                         up = intoudpcb(inp);
1084                         KASSERT(up != NULL, ("%s: up == NULL", __func__));
1085                         if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1086                                 optval = up->u_txcslen;
1087                         else
1088                                 optval = up->u_rxcslen;
1089                         INP_WUNLOCK(inp);
1090                         error = sooptcopyout(sopt, &optval, sizeof(optval));
1091                         break;
1092                 default:
1093                         INP_WUNLOCK(inp);
1094                         error = ENOPROTOOPT;
1095                         break;
1096                 }
1097                 break;
1098         }       
1099         return (error);
1100 }
1101
1102 #ifdef INET
1103 #define UH_WLOCKED      2
1104 #define UH_RLOCKED      1
1105 #define UH_UNLOCKED     0
1106 static int
1107 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
1108     struct mbuf *control, struct thread *td)
1109 {
1110         struct udpiphdr *ui;
1111         int len = m->m_pkthdr.len;
1112         struct in_addr faddr, laddr;
1113         struct cmsghdr *cm;
1114         struct inpcbinfo *pcbinfo;
1115         struct sockaddr_in *sin, src;
1116         int cscov_partial = 0;
1117         int error = 0;
1118         int ipflags;
1119         u_short fport, lport;
1120         int unlock_udbinfo;
1121         u_char tos;
1122         uint8_t pr;
1123         uint16_t cscov = 0;
1124         uint32_t flowid = 0;
1125         uint8_t flowtype = M_HASHTYPE_NONE;
1126
1127         /*
1128          * udp_output() may need to temporarily bind or connect the current
1129          * inpcb.  As such, we don't know up front whether we will need the
1130          * pcbinfo lock or not.  Do any work to decide what is needed up
1131          * front before acquiring any locks.
1132          */
1133         if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1134                 if (control)
1135                         m_freem(control);
1136                 m_freem(m);
1137                 return (EMSGSIZE);
1138         }
1139
1140         src.sin_family = 0;
1141         INP_RLOCK(inp);
1142         tos = inp->inp_ip_tos;
1143         if (control != NULL) {
1144                 /*
1145                  * XXX: Currently, we assume all the optional information is
1146                  * stored in a single mbuf.
1147                  */
1148                 if (control->m_next) {
1149                         INP_RUNLOCK(inp);
1150                         m_freem(control);
1151                         m_freem(m);
1152                         return (EINVAL);
1153                 }
1154                 for (; control->m_len > 0;
1155                     control->m_data += CMSG_ALIGN(cm->cmsg_len),
1156                     control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1157                         cm = mtod(control, struct cmsghdr *);
1158                         if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
1159                             || cm->cmsg_len > control->m_len) {
1160                                 error = EINVAL;
1161                                 break;
1162                         }
1163                         if (cm->cmsg_level != IPPROTO_IP)
1164                                 continue;
1165
1166                         switch (cm->cmsg_type) {
1167                         case IP_SENDSRCADDR:
1168                                 if (cm->cmsg_len !=
1169                                     CMSG_LEN(sizeof(struct in_addr))) {
1170                                         error = EINVAL;
1171                                         break;
1172                                 }
1173                                 bzero(&src, sizeof(src));
1174                                 src.sin_family = AF_INET;
1175                                 src.sin_len = sizeof(src);
1176                                 src.sin_port = inp->inp_lport;
1177                                 src.sin_addr =
1178                                     *(struct in_addr *)CMSG_DATA(cm);
1179                                 break;
1180
1181                         case IP_TOS:
1182                                 if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) {
1183                                         error = EINVAL;
1184                                         break;
1185                                 }
1186                                 tos = *(u_char *)CMSG_DATA(cm);
1187                                 break;
1188
1189                         case IP_FLOWID:
1190                                 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1191                                         error = EINVAL;
1192                                         break;
1193                                 }
1194                                 flowid = *(uint32_t *) CMSG_DATA(cm);
1195                                 break;
1196
1197                         case IP_FLOWTYPE:
1198                                 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1199                                         error = EINVAL;
1200                                         break;
1201                                 }
1202                                 flowtype = *(uint32_t *) CMSG_DATA(cm);
1203                                 break;
1204
1205 #ifdef  RSS
1206                         case IP_RSSBUCKETID:
1207                                 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1208                                         error = EINVAL;
1209                                         break;
1210                                 }
1211                                 /* This is just a placeholder for now */
1212                                 break;
1213 #endif  /* RSS */
1214                         default:
1215                                 error = ENOPROTOOPT;
1216                                 break;
1217                         }
1218                         if (error)
1219                                 break;
1220                 }
1221                 m_freem(control);
1222         }
1223         if (error) {
1224                 INP_RUNLOCK(inp);
1225                 m_freem(m);
1226                 return (error);
1227         }
1228
1229         /*
1230          * Depending on whether or not the application has bound or connected
1231          * the socket, we may have to do varying levels of work.  The optimal
1232          * case is for a connected UDP socket, as a global lock isn't
1233          * required at all.
1234          *
1235          * In order to decide which we need, we require stability of the
1236          * inpcb binding, which we ensure by acquiring a read lock on the
1237          * inpcb.  This doesn't strictly follow the lock order, so we play
1238          * the trylock and retry game; note that we may end up with more
1239          * conservative locks than required the second time around, so later
1240          * assertions have to accept that.  Further analysis of the number of
1241          * misses under contention is required.
1242          *
1243          * XXXRW: Check that hash locking update here is correct.
1244          */
1245         pr = inp->inp_socket->so_proto->pr_protocol;
1246         pcbinfo = udp_get_inpcbinfo(pr);
1247         sin = (struct sockaddr_in *)addr;
1248         if (sin != NULL &&
1249             (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
1250                 INP_RUNLOCK(inp);
1251                 INP_WLOCK(inp);
1252                 INP_HASH_WLOCK(pcbinfo);
1253                 unlock_udbinfo = UH_WLOCKED;
1254         } else if ((sin != NULL && (
1255             (sin->sin_addr.s_addr == INADDR_ANY) ||
1256             (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
1257             (inp->inp_laddr.s_addr == INADDR_ANY) ||
1258             (inp->inp_lport == 0))) ||
1259             (src.sin_family == AF_INET)) {
1260                 INP_HASH_RLOCK(pcbinfo);
1261                 unlock_udbinfo = UH_RLOCKED;
1262         } else
1263                 unlock_udbinfo = UH_UNLOCKED;
1264
1265         /*
1266          * If the IP_SENDSRCADDR control message was specified, override the
1267          * source address for this datagram.  Its use is invalidated if the
1268          * address thus specified is incomplete or clobbers other inpcbs.
1269          */
1270         laddr = inp->inp_laddr;
1271         lport = inp->inp_lport;
1272         if (src.sin_family == AF_INET) {
1273                 INP_HASH_LOCK_ASSERT(pcbinfo);
1274                 if ((lport == 0) ||
1275                     (laddr.s_addr == INADDR_ANY &&
1276                      src.sin_addr.s_addr == INADDR_ANY)) {
1277                         error = EINVAL;
1278                         goto release;
1279                 }
1280                 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
1281                     &laddr.s_addr, &lport, td->td_ucred);
1282                 if (error)
1283                         goto release;
1284         }
1285
1286         /*
1287          * If a UDP socket has been connected, then a local address/port will
1288          * have been selected and bound.
1289          *
1290          * If a UDP socket has not been connected to, then an explicit
1291          * destination address must be used, in which case a local
1292          * address/port may not have been selected and bound.
1293          */
1294         if (sin != NULL) {
1295                 INP_LOCK_ASSERT(inp);
1296                 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1297                         error = EISCONN;
1298                         goto release;
1299                 }
1300
1301                 /*
1302                  * Jail may rewrite the destination address, so let it do
1303                  * that before we use it.
1304                  */
1305                 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1306                 if (error)
1307                         goto release;
1308
1309                 /*
1310                  * If a local address or port hasn't yet been selected, or if
1311                  * the destination address needs to be rewritten due to using
1312                  * a special INADDR_ constant, invoke in_pcbconnect_setup()
1313                  * to do the heavy lifting.  Once a port is selected, we
1314                  * commit the binding back to the socket; we also commit the
1315                  * binding of the address if in jail.
1316                  *
1317                  * If we already have a valid binding and we're not
1318                  * requesting a destination address rewrite, use a fast path.
1319                  */
1320                 if (inp->inp_laddr.s_addr == INADDR_ANY ||
1321                     inp->inp_lport == 0 ||
1322                     sin->sin_addr.s_addr == INADDR_ANY ||
1323                     sin->sin_addr.s_addr == INADDR_BROADCAST) {
1324                         INP_HASH_LOCK_ASSERT(pcbinfo);
1325                         error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
1326                             &lport, &faddr.s_addr, &fport, NULL,
1327                             td->td_ucred);
1328                         if (error)
1329                                 goto release;
1330
1331                         /*
1332                          * XXXRW: Why not commit the port if the address is
1333                          * !INADDR_ANY?
1334                          */
1335                         /* Commit the local port if newly assigned. */
1336                         if (inp->inp_laddr.s_addr == INADDR_ANY &&
1337                             inp->inp_lport == 0) {
1338                                 INP_WLOCK_ASSERT(inp);
1339                                 INP_HASH_WLOCK_ASSERT(pcbinfo);
1340                                 /*
1341                                  * Remember addr if jailed, to prevent
1342                                  * rebinding.
1343                                  */
1344                                 if (prison_flag(td->td_ucred, PR_IP4))
1345                                         inp->inp_laddr = laddr;
1346                                 inp->inp_lport = lport;
1347                                 if (in_pcbinshash(inp) != 0) {
1348                                         inp->inp_lport = 0;
1349                                         error = EAGAIN;
1350                                         goto release;
1351                                 }
1352                                 inp->inp_flags |= INP_ANONPORT;
1353                         }
1354                 } else {
1355                         faddr = sin->sin_addr;
1356                         fport = sin->sin_port;
1357                 }
1358         } else {
1359                 INP_LOCK_ASSERT(inp);
1360                 faddr = inp->inp_faddr;
1361                 fport = inp->inp_fport;
1362                 if (faddr.s_addr == INADDR_ANY) {
1363                         error = ENOTCONN;
1364                         goto release;
1365                 }
1366         }
1367
1368         /*
1369          * Calculate data length and get a mbuf for UDP, IP, and possible
1370          * link-layer headers.  Immediate slide the data pointer back forward
1371          * since we won't use that space at this layer.
1372          */
1373         M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT);
1374         if (m == NULL) {
1375                 error = ENOBUFS;
1376                 goto release;
1377         }
1378         m->m_data += max_linkhdr;
1379         m->m_len -= max_linkhdr;
1380         m->m_pkthdr.len -= max_linkhdr;
1381
1382         /*
1383          * Fill in mbuf with extended UDP header and addresses and length put
1384          * into network format.
1385          */
1386         ui = mtod(m, struct udpiphdr *);
1387         bzero(ui->ui_x1, sizeof(ui->ui_x1));    /* XXX still needed? */
1388         ui->ui_pr = pr;
1389         ui->ui_src = laddr;
1390         ui->ui_dst = faddr;
1391         ui->ui_sport = lport;
1392         ui->ui_dport = fport;
1393         ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1394         if (pr == IPPROTO_UDPLITE) {
1395                 struct udpcb *up;
1396                 uint16_t plen;
1397
1398                 up = intoudpcb(inp);
1399                 cscov = up->u_txcslen;
1400                 plen = (u_short)len + sizeof(struct udphdr);
1401                 if (cscov >= plen)
1402                         cscov = 0;
1403                 ui->ui_len = htons(plen);
1404                 ui->ui_ulen = htons(cscov);
1405                 /*
1406                  * For UDP-Lite, checksum coverage length of zero means
1407                  * the entire UDPLite packet is covered by the checksum.
1408                  */
1409                 cscov_partial = (cscov == 0) ? 0 : 1;
1410         } else
1411                 ui->ui_v = IPVERSION << 4;
1412
1413         /*
1414          * Set the Don't Fragment bit in the IP header.
1415          */
1416         if (inp->inp_flags & INP_DONTFRAG) {
1417                 struct ip *ip;
1418
1419                 ip = (struct ip *)&ui->ui_i;
1420                 ip->ip_off |= htons(IP_DF);
1421         }
1422
1423         ipflags = 0;
1424         if (inp->inp_socket->so_options & SO_DONTROUTE)
1425                 ipflags |= IP_ROUTETOIF;
1426         if (inp->inp_socket->so_options & SO_BROADCAST)
1427                 ipflags |= IP_ALLOWBROADCAST;
1428         if (inp->inp_flags & INP_ONESBCAST)
1429                 ipflags |= IP_SENDONES;
1430
1431 #ifdef MAC
1432         mac_inpcb_create_mbuf(inp, m);
1433 #endif
1434
1435         /*
1436          * Set up checksum and output datagram.
1437          */
1438         ui->ui_sum = 0;
1439         if (pr == IPPROTO_UDPLITE) {
1440                 if (inp->inp_flags & INP_ONESBCAST)
1441                         faddr.s_addr = INADDR_BROADCAST;
1442                 if (cscov_partial) {
1443                         if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0)
1444                                 ui->ui_sum = 0xffff;
1445                 } else {
1446                         if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0)
1447                                 ui->ui_sum = 0xffff;
1448                 }
1449         } else if (V_udp_cksum) {
1450                 if (inp->inp_flags & INP_ONESBCAST)
1451                         faddr.s_addr = INADDR_BROADCAST;
1452                 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1453                     htons((u_short)len + sizeof(struct udphdr) + pr));
1454                 m->m_pkthdr.csum_flags = CSUM_UDP;
1455                 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1456         }
1457         ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
1458         ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;    /* XXX */
1459         ((struct ip *)ui)->ip_tos = tos;                /* XXX */
1460         UDPSTAT_INC(udps_opackets);
1461
1462         /*
1463          * Setup flowid / RSS information for outbound socket.
1464          *
1465          * Once the UDP code decides to set a flowid some other way,
1466          * this allows the flowid to be overridden by userland.
1467          */
1468         if (flowtype != M_HASHTYPE_NONE) {
1469                 m->m_pkthdr.flowid = flowid;
1470                 M_HASHTYPE_SET(m, flowtype);
1471 #ifdef  RSS
1472         } else {
1473                 uint32_t hash_val, hash_type;
1474                 /*
1475                  * Calculate an appropriate RSS hash for UDP and
1476                  * UDP Lite.
1477                  *
1478                  * The called function will take care of figuring out
1479                  * whether a 2-tuple or 4-tuple hash is required based
1480                  * on the currently configured scheme.
1481                  *
1482                  * Later later on connected socket values should be
1483                  * cached in the inpcb and reused, rather than constantly
1484                  * re-calculating it.
1485                  *
1486                  * UDP Lite is a different protocol number and will
1487                  * likely end up being hashed as a 2-tuple until
1488                  * RSS / NICs grow UDP Lite protocol awareness.
1489                  */
1490                 if (rss_proto_software_hash_v4(faddr, laddr, fport, lport,
1491                     pr, &hash_val, &hash_type) == 0) {
1492                         m->m_pkthdr.flowid = hash_val;
1493                         M_HASHTYPE_SET(m, hash_type);
1494                 }
1495 #endif
1496         }
1497
1498 #ifdef  RSS
1499         /*
1500          * Don't override with the inp cached flowid value.
1501          *
1502          * Depending upon the kind of send being done, the inp
1503          * flowid/flowtype values may actually not be appropriate
1504          * for this particular socket send.
1505          *
1506          * We should either leave the flowid at zero (which is what is
1507          * currently done) or set it to some software generated
1508          * hash value based on the packet contents.
1509          */
1510         ipflags |= IP_NODEFAULTFLOWID;
1511 #endif  /* RSS */
1512
1513         if (unlock_udbinfo == UH_WLOCKED)
1514                 INP_HASH_WUNLOCK(pcbinfo);
1515         else if (unlock_udbinfo == UH_RLOCKED)
1516                 INP_HASH_RUNLOCK(pcbinfo);
1517         UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
1518         error = ip_output(m, inp->inp_options, NULL, ipflags,
1519             inp->inp_moptions, inp);
1520         if (unlock_udbinfo == UH_WLOCKED)
1521                 INP_WUNLOCK(inp);
1522         else
1523                 INP_RUNLOCK(inp);
1524         return (error);
1525
1526 release:
1527         if (unlock_udbinfo == UH_WLOCKED) {
1528                 INP_HASH_WUNLOCK(pcbinfo);
1529                 INP_WUNLOCK(inp);
1530         } else if (unlock_udbinfo == UH_RLOCKED) {
1531                 INP_HASH_RUNLOCK(pcbinfo);
1532                 INP_RUNLOCK(inp);
1533         } else
1534                 INP_RUNLOCK(inp);
1535         m_freem(m);
1536         return (error);
1537 }
1538
1539
1540 #if defined(IPSEC) && defined(IPSEC_NAT_T)
1541 /*
1542  * Potentially decap ESP in UDP frame.  Check for an ESP header
1543  * and optional marker; if present, strip the UDP header and
1544  * push the result through IPSec.
1545  *
1546  * Returns mbuf to be processed (potentially re-allocated) or
1547  * NULL if consumed and/or processed.
1548  */
1549 static struct mbuf *
1550 udp4_espdecap(struct inpcb *inp, struct mbuf *m, int off)
1551 {
1552         size_t minlen, payload, skip, iphlen;
1553         caddr_t data;
1554         struct udpcb *up;
1555         struct m_tag *tag;
1556         struct udphdr *udphdr;
1557         struct ip *ip;
1558
1559         INP_RLOCK_ASSERT(inp);
1560
1561         /* 
1562          * Pull up data so the longest case is contiguous:
1563          *    IP/UDP hdr + non ESP marker + ESP hdr.
1564          */
1565         minlen = off + sizeof(uint64_t) + sizeof(struct esp);
1566         if (minlen > m->m_pkthdr.len)
1567                 minlen = m->m_pkthdr.len;
1568         if ((m = m_pullup(m, minlen)) == NULL) {
1569                 IPSECSTAT_INC(ips_in_inval);
1570                 return (NULL);          /* Bypass caller processing. */
1571         }
1572         data = mtod(m, caddr_t);        /* Points to ip header. */
1573         payload = m->m_len - off;       /* Size of payload. */
1574
1575         if (payload == 1 && data[off] == '\xff')
1576                 return (m);             /* NB: keepalive packet, no decap. */
1577
1578         up = intoudpcb(inp);
1579         KASSERT(up != NULL, ("%s: udpcb NULL", __func__));
1580         KASSERT((up->u_flags & UF_ESPINUDP_ALL) != 0,
1581             ("u_flags 0x%x", up->u_flags));
1582
1583         /* 
1584          * Check that the payload is large enough to hold an
1585          * ESP header and compute the amount of data to remove.
1586          *
1587          * NB: the caller has already done a pullup for us.
1588          * XXX can we assume alignment and eliminate bcopys?
1589          */
1590         if (up->u_flags & UF_ESPINUDP_NON_IKE) {
1591                 /*
1592                  * draft-ietf-ipsec-nat-t-ike-0[01].txt and
1593                  * draft-ietf-ipsec-udp-encaps-(00/)01.txt, ignoring
1594                  * possible AH mode non-IKE marker+non-ESP marker
1595                  * from draft-ietf-ipsec-udp-encaps-00.txt.
1596                  */
1597                 uint64_t marker;
1598
1599                 if (payload <= sizeof(uint64_t) + sizeof(struct esp))
1600                         return (m);     /* NB: no decap. */
1601                 bcopy(data + off, &marker, sizeof(uint64_t));
1602                 if (marker != 0)        /* Non-IKE marker. */
1603                         return (m);     /* NB: no decap. */
1604                 skip = sizeof(uint64_t) + sizeof(struct udphdr);
1605         } else {
1606                 uint32_t spi;
1607
1608                 if (payload <= sizeof(struct esp)) {
1609                         IPSECSTAT_INC(ips_in_inval);
1610                         m_freem(m);
1611                         return (NULL);  /* Discard. */
1612                 }
1613                 bcopy(data + off, &spi, sizeof(uint32_t));
1614                 if (spi == 0)           /* Non-ESP marker. */
1615                         return (m);     /* NB: no decap. */
1616                 skip = sizeof(struct udphdr);
1617         }
1618
1619         /*
1620          * Setup a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
1621          * the UDP ports. This is required if we want to select
1622          * the right SPD for multiple hosts behind same NAT.
1623          *
1624          * NB: ports are maintained in network byte order everywhere
1625          *     in the NAT-T code.
1626          */
1627         tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
1628                 2 * sizeof(uint16_t), M_NOWAIT);
1629         if (tag == NULL) {
1630                 IPSECSTAT_INC(ips_in_nomem);
1631                 m_freem(m);
1632                 return (NULL);          /* Discard. */
1633         }
1634         iphlen = off - sizeof(struct udphdr);
1635         udphdr = (struct udphdr *)(data + iphlen);
1636         ((uint16_t *)(tag + 1))[0] = udphdr->uh_sport;
1637         ((uint16_t *)(tag + 1))[1] = udphdr->uh_dport;
1638         m_tag_prepend(m, tag);
1639
1640         /*
1641          * Remove the UDP header (and possibly the non ESP marker)
1642          * IP header length is iphlen
1643          * Before:
1644          *   <--- off --->
1645          *   +----+------+-----+
1646          *   | IP |  UDP | ESP |
1647          *   +----+------+-----+
1648          *        <-skip->
1649          * After:
1650          *          +----+-----+
1651          *          | IP | ESP |
1652          *          +----+-----+
1653          *   <-skip->
1654          */
1655         ovbcopy(data, data + skip, iphlen);
1656         m_adj(m, skip);
1657
1658         ip = mtod(m, struct ip *);
1659         ip->ip_len = htons(ntohs(ip->ip_len) - skip);
1660         ip->ip_p = IPPROTO_ESP;
1661
1662         /*
1663          * We cannot yet update the cksums so clear any
1664          * h/w cksum flags as they are no longer valid.
1665          */
1666         if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)
1667                 m->m_pkthdr.csum_flags &= ~(CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
1668
1669         (void) ipsec_common_input(m, iphlen, offsetof(struct ip, ip_p),
1670                                 AF_INET, ip->ip_p);
1671         return (NULL);                  /* NB: consumed, bypass processing. */
1672 }
1673 #endif /* defined(IPSEC) && defined(IPSEC_NAT_T) */
1674
1675 static void
1676 udp_abort(struct socket *so)
1677 {
1678         struct inpcb *inp;
1679         struct inpcbinfo *pcbinfo;
1680
1681         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1682         inp = sotoinpcb(so);
1683         KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
1684         INP_WLOCK(inp);
1685         if (inp->inp_faddr.s_addr != INADDR_ANY) {
1686                 INP_HASH_WLOCK(pcbinfo);
1687                 in_pcbdisconnect(inp);
1688                 inp->inp_laddr.s_addr = INADDR_ANY;
1689                 INP_HASH_WUNLOCK(pcbinfo);
1690                 soisdisconnected(so);
1691         }
1692         INP_WUNLOCK(inp);
1693 }
1694
1695 static int
1696 udp_attach(struct socket *so, int proto, struct thread *td)
1697 {
1698         struct inpcb *inp;
1699         struct inpcbinfo *pcbinfo;
1700         int error;
1701
1702         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1703         inp = sotoinpcb(so);
1704         KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1705         error = soreserve(so, udp_sendspace, udp_recvspace);
1706         if (error)
1707                 return (error);
1708         INP_INFO_WLOCK(pcbinfo);
1709         error = in_pcballoc(so, pcbinfo);
1710         if (error) {
1711                 INP_INFO_WUNLOCK(pcbinfo);
1712                 return (error);
1713         }
1714
1715         inp = sotoinpcb(so);
1716         inp->inp_vflag |= INP_IPV4;
1717         inp->inp_ip_ttl = V_ip_defttl;
1718
1719         error = udp_newudpcb(inp);
1720         if (error) {
1721                 in_pcbdetach(inp);
1722                 in_pcbfree(inp);
1723                 INP_INFO_WUNLOCK(pcbinfo);
1724                 return (error);
1725         }
1726
1727         INP_WUNLOCK(inp);
1728         INP_INFO_WUNLOCK(pcbinfo);
1729         return (0);
1730 }
1731 #endif /* INET */
1732
1733 int
1734 udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, void *ctx)
1735 {
1736         struct inpcb *inp;
1737         struct udpcb *up;
1738
1739         KASSERT(so->so_type == SOCK_DGRAM,
1740             ("udp_set_kernel_tunneling: !dgram"));
1741         inp = sotoinpcb(so);
1742         KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL"));
1743         INP_WLOCK(inp);
1744         up = intoudpcb(inp);
1745         if (up->u_tun_func != NULL) {
1746                 INP_WUNLOCK(inp);
1747                 return (EBUSY);
1748         }
1749         up->u_tun_func = f;
1750         up->u_tun_ctx = ctx;
1751         INP_WUNLOCK(inp);
1752         return (0);
1753 }
1754
1755 #ifdef INET
1756 static int
1757 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1758 {
1759         struct inpcb *inp;
1760         struct inpcbinfo *pcbinfo;
1761         int error;
1762
1763         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1764         inp = sotoinpcb(so);
1765         KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1766         INP_WLOCK(inp);
1767         INP_HASH_WLOCK(pcbinfo);
1768         error = in_pcbbind(inp, nam, td->td_ucred);
1769         INP_HASH_WUNLOCK(pcbinfo);
1770         INP_WUNLOCK(inp);
1771         return (error);
1772 }
1773
1774 static void
1775 udp_close(struct socket *so)
1776 {
1777         struct inpcb *inp;
1778         struct inpcbinfo *pcbinfo;
1779
1780         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1781         inp = sotoinpcb(so);
1782         KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1783         INP_WLOCK(inp);
1784         if (inp->inp_faddr.s_addr != INADDR_ANY) {
1785                 INP_HASH_WLOCK(pcbinfo);
1786                 in_pcbdisconnect(inp);
1787                 inp->inp_laddr.s_addr = INADDR_ANY;
1788                 INP_HASH_WUNLOCK(pcbinfo);
1789                 soisdisconnected(so);
1790         }
1791         INP_WUNLOCK(inp);
1792 }
1793
1794 static int
1795 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1796 {
1797         struct inpcb *inp;
1798         struct inpcbinfo *pcbinfo;
1799         struct sockaddr_in *sin;
1800         int error;
1801
1802         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1803         inp = sotoinpcb(so);
1804         KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1805         INP_WLOCK(inp);
1806         if (inp->inp_faddr.s_addr != INADDR_ANY) {
1807                 INP_WUNLOCK(inp);
1808                 return (EISCONN);
1809         }
1810         sin = (struct sockaddr_in *)nam;
1811         error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1812         if (error != 0) {
1813                 INP_WUNLOCK(inp);
1814                 return (error);
1815         }
1816         INP_HASH_WLOCK(pcbinfo);
1817         error = in_pcbconnect(inp, nam, td->td_ucred);
1818         INP_HASH_WUNLOCK(pcbinfo);
1819         if (error == 0)
1820                 soisconnected(so);
1821         INP_WUNLOCK(inp);
1822         return (error);
1823 }
1824
1825 static void
1826 udp_detach(struct socket *so)
1827 {
1828         struct inpcb *inp;
1829         struct inpcbinfo *pcbinfo;
1830         struct udpcb *up;
1831
1832         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1833         inp = sotoinpcb(so);
1834         KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1835         KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1836             ("udp_detach: not disconnected"));
1837         INP_INFO_WLOCK(pcbinfo);
1838         INP_WLOCK(inp);
1839         up = intoudpcb(inp);
1840         KASSERT(up != NULL, ("%s: up == NULL", __func__));
1841         inp->inp_ppcb = NULL;
1842         in_pcbdetach(inp);
1843         in_pcbfree(inp);
1844         INP_INFO_WUNLOCK(pcbinfo);
1845         udp_discardcb(up);
1846 }
1847
1848 static int
1849 udp_disconnect(struct socket *so)
1850 {
1851         struct inpcb *inp;
1852         struct inpcbinfo *pcbinfo;
1853
1854         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1855         inp = sotoinpcb(so);
1856         KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1857         INP_WLOCK(inp);
1858         if (inp->inp_faddr.s_addr == INADDR_ANY) {
1859                 INP_WUNLOCK(inp);
1860                 return (ENOTCONN);
1861         }
1862         INP_HASH_WLOCK(pcbinfo);
1863         in_pcbdisconnect(inp);
1864         inp->inp_laddr.s_addr = INADDR_ANY;
1865         INP_HASH_WUNLOCK(pcbinfo);
1866         SOCK_LOCK(so);
1867         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
1868         SOCK_UNLOCK(so);
1869         INP_WUNLOCK(inp);
1870         return (0);
1871 }
1872
1873 static int
1874 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1875     struct mbuf *control, struct thread *td)
1876 {
1877         struct inpcb *inp;
1878
1879         inp = sotoinpcb(so);
1880         KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1881         return (udp_output(inp, m, addr, control, td));
1882 }
1883 #endif /* INET */
1884
1885 int
1886 udp_shutdown(struct socket *so)
1887 {
1888         struct inpcb *inp;
1889
1890         inp = sotoinpcb(so);
1891         KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1892         INP_WLOCK(inp);
1893         socantsendmore(so);
1894         INP_WUNLOCK(inp);
1895         return (0);
1896 }
1897
1898 #ifdef INET
1899 struct pr_usrreqs udp_usrreqs = {
1900         .pru_abort =            udp_abort,
1901         .pru_attach =           udp_attach,
1902         .pru_bind =             udp_bind,
1903         .pru_connect =          udp_connect,
1904         .pru_control =          in_control,
1905         .pru_detach =           udp_detach,
1906         .pru_disconnect =       udp_disconnect,
1907         .pru_peeraddr =         in_getpeeraddr,
1908         .pru_send =             udp_send,
1909         .pru_soreceive =        soreceive_dgram,
1910         .pru_sosend =           sosend_dgram,
1911         .pru_shutdown =         udp_shutdown,
1912         .pru_sockaddr =         in_getsockaddr,
1913         .pru_sosetlabel =       in_pcbsosetlabel,
1914         .pru_close =            udp_close,
1915 };
1916 #endif /* INET */