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