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