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