Add correct register offsets
[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) 2004 Jeffrey M. Hsu.  All rights reserved.
36  *
37  * License terms: all terms for the DragonFly license above plus the following:
38  *
39  * 4. All advertising materials mentioning features or use of this software
40  *    must display the following acknowledgement:
41  *
42  *      This product includes software developed by Jeffrey M. Hsu
43  *      for the DragonFly Project.
44  *
45  *    This requirement may be waived with permission from Jeffrey Hsu.
46  *    This requirement will sunset and may be removed on July 8 2005,
47  *    after which the standard DragonFly license (as shown above) will
48  *    apply.
49  */
50
51 /*
52  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
53  *      The Regents of the University of California.  All rights reserved.
54  *
55  * Redistribution and use in source and binary forms, with or without
56  * modification, are permitted provided that the following conditions
57  * are met:
58  * 1. Redistributions of source code must retain the above copyright
59  *    notice, this list of conditions and the following disclaimer.
60  * 2. Redistributions in binary form must reproduce the above copyright
61  *    notice, this list of conditions and the following disclaimer in the
62  *    documentation and/or other materials provided with the distribution.
63  * 3. All advertising materials mentioning features or use of this software
64  *    must display the following acknowledgement:
65  *      This product includes software developed by the University of
66  *      California, Berkeley and its contributors.
67  * 4. Neither the name of the University nor the names of its contributors
68  *    may be used to endorse or promote products derived from this software
69  *    without specific prior written permission.
70  *
71  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81  * SUCH DAMAGE.
82  *
83  *      @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
84  * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $
85  * $DragonFly: src/sys/netinet/udp_usrreq.c,v 1.36 2006/01/14 11:33:50 swildner Exp $
86  */
87
88 #include "opt_ipsec.h"
89 #include "opt_inet6.h"
90
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/kernel.h>
94 #include <sys/malloc.h>
95 #include <sys/mbuf.h>
96 #include <sys/domain.h>
97 #include <sys/proc.h>
98 #include <sys/protosw.h>
99 #include <sys/socket.h>
100 #include <sys/socketvar.h>
101 #include <sys/sysctl.h>
102 #include <sys/syslog.h>
103 #include <sys/thread2.h>
104 #include <sys/in_cksum.h>
105
106 #include <machine/stdarg.h>
107
108 #include <vm/vm_zone.h>
109
110 #include <net/if.h>
111 #include <net/route.h>
112
113 #include <netinet/in.h>
114 #include <netinet/in_systm.h>
115 #include <netinet/ip.h>
116 #ifdef INET6
117 #include <netinet/ip6.h>
118 #endif
119 #include <netinet/in_pcb.h>
120 #include <netinet/in_var.h>
121 #include <netinet/ip_var.h>
122 #ifdef INET6
123 #include <netinet6/ip6_var.h>
124 #endif
125 #include <netinet/ip_icmp.h>
126 #include <netinet/icmp_var.h>
127 #include <netinet/udp.h>
128 #include <netinet/udp_var.h>
129
130 #ifdef FAST_IPSEC
131 #include <netproto/ipsec/ipsec.h>
132 #endif
133
134 #ifdef IPSEC
135 #include <netinet6/ipsec.h>
136 #endif
137
138 /*
139  * UDP protocol implementation.
140  * Per RFC 768, August, 1980.
141  */
142 #ifndef COMPAT_42
143 static int      udpcksum = 1;
144 #else
145 static int      udpcksum = 0;           /* XXX */
146 #endif
147 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
148                 &udpcksum, 0, "");
149
150 int     log_in_vain = 0;
151 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
152     &log_in_vain, 0, "Log all incoming UDP packets");
153
154 static int      blackhole = 0;
155 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
156         &blackhole, 0, "Do not send port unreachables for refused connects");
157
158 static int      strict_mcast_mship = 1;
159 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
160         &strict_mcast_mship, 0, "Only send multicast to member sockets");
161
162 struct  inpcbinfo udbinfo;
163
164 #ifndef UDBHASHSIZE
165 #define UDBHASHSIZE 16
166 #endif
167
168 struct  udpstat udpstat;        /* from udp_var.h */
169 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
170     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
171
172 static struct   sockaddr_in udp_in = { sizeof udp_in, AF_INET };
173 #ifdef INET6
174 struct udp_in6 {
175         struct sockaddr_in6     uin6_sin;
176         u_char                  uin6_init_done : 1;
177 } udp_in6 = {
178         { sizeof udp_in6.uin6_sin, AF_INET6 },
179         0
180 };
181 struct udp_ip6 {
182         struct ip6_hdr          uip6_ip6;
183         u_char                  uip6_init_done : 1;
184 } udp_ip6;
185 #endif /* INET6 */
186
187 static void udp_append (struct inpcb *last, struct ip *ip,
188                             struct mbuf *n, int off);
189 #ifdef INET6
190 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
191 #endif
192
193 static int udp_detach (struct socket *so);
194 static  int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
195                             struct mbuf *, struct thread *);
196
197 void
198 udp_init(void)
199 {
200         in_pcbinfo_init(&udbinfo);
201         udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
202         udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
203                                         &udbinfo.porthashmask);
204         udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
205                                             &udbinfo.wildcardhashmask);
206         udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
207                                  ZONE_INTERRUPT, 0);
208         udp_thread_init();
209 }
210
211 /*
212  * Check multicast packets to make sure they are only sent to sockets with
213  * multicast memberships for the packet's destination address and arrival
214  * interface.  Multicast packets to multicast-unaware sockets are also
215  * disallowed.
216  *
217  * Returns 0 if the packet is acceptable, -1 if it is not.
218  */
219 static __inline int
220 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
221 {
222         int mshipno;
223         struct ip_moptions *mopt;
224
225         if (strict_mcast_mship == 0 ||
226             !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
227                 return (0);
228         }
229         mopt = inp->inp_moptions;
230         if (mopt == NULL)
231                 return (-1);
232         for (mshipno = 0; mshipno <= mopt->imo_num_memberships; ++mshipno) {
233                 struct in_multi *maddr = mopt->imo_membership[mshipno];
234
235                 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
236                     m->m_pkthdr.rcvif == maddr->inm_ifp) {
237                         return (0);
238                 }
239         }
240         return (-1);
241 }
242
243 void
244 udp_input(struct mbuf *m, ...)
245 {
246         int iphlen;
247         struct ip *ip;
248         struct udphdr *uh;
249         struct inpcb *inp;
250         struct mbuf *opts = NULL;
251         int len, off, proto;
252         struct ip save_ip;
253         struct sockaddr *append_sa;
254         __va_list ap;
255
256         __va_start(ap, m);
257         off = __va_arg(ap, int);
258         proto = __va_arg(ap, int);
259         __va_end(ap);
260
261         iphlen = off;
262         udpstat.udps_ipackets++;
263
264         /*
265          * Strip IP options, if any; should skip this,
266          * make available to user, and use on returned packets,
267          * but we don't yet have a way to check the checksum
268          * with options still present.
269          */
270         if (iphlen > sizeof(struct ip)) {
271                 ip_stripoptions(m);
272                 iphlen = sizeof(struct ip);
273         }
274
275         /*
276          * IP and UDP headers are together in first mbuf.
277          * Already checked and pulled up in ip_demux().
278          */
279         KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
280             ("UDP header not in one mbuf"));
281
282         ip = mtod(m, struct ip *);
283         uh = (struct udphdr *)((caddr_t)ip + iphlen);
284
285         /* destination port of 0 is illegal, based on RFC768. */
286         if (uh->uh_dport == 0)
287                 goto bad;
288
289         /*
290          * Make mbuf data length reflect UDP length.
291          * If not enough data to reflect UDP length, drop.
292          */
293         len = ntohs((u_short)uh->uh_ulen);
294         if (ip->ip_len != len) {
295                 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
296                         udpstat.udps_badlen++;
297                         goto bad;
298                 }
299                 m_adj(m, len - ip->ip_len);
300                 /* ip->ip_len = len; */
301         }
302         /*
303          * Save a copy of the IP header in case we want restore it
304          * for sending an ICMP error message in response.
305          */
306         save_ip = *ip;
307
308         /*
309          * Checksum extended UDP header and data.
310          */
311         if (uh->uh_sum) {
312                 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
313                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
314                                 uh->uh_sum = m->m_pkthdr.csum_data;
315                         else
316                                 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
317                                     ip->ip_dst.s_addr, htonl((u_short)len +
318                                     m->m_pkthdr.csum_data + IPPROTO_UDP));
319                         uh->uh_sum ^= 0xffff;
320                 } else {
321                         char b[9];
322
323                         bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
324                         bzero(((struct ipovly *)ip)->ih_x1, 9);
325                         ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
326                         uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
327                         bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
328                 }
329                 if (uh->uh_sum) {
330                         udpstat.udps_badsum++;
331                         m_freem(m);
332                         return;
333                 }
334         } else
335                 udpstat.udps_nosum++;
336
337         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
338             in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
339                 struct inpcb *last;
340
341                 /*
342                  * Deliver a multicast or broadcast datagram to *all* sockets
343                  * for which the local and remote addresses and ports match
344                  * those of the incoming datagram.  This allows more than
345                  * one process to receive multi/broadcasts on the same port.
346                  * (This really ought to be done for unicast datagrams as
347                  * well, but that would cause problems with existing
348                  * applications that open both address-specific sockets and
349                  * a wildcard socket listening to the same port -- they would
350                  * end up receiving duplicates of every unicast datagram.
351                  * Those applications open the multiple sockets to overcome an
352                  * inadequacy of the UDP socket interface, but for backwards
353                  * compatibility we avoid the problem here rather than
354                  * fixing the interface.  Maybe 4.5BSD will remedy this?)
355                  */
356
357                 /*
358                  * Construct sockaddr format source address.
359                  */
360                 udp_in.sin_port = uh->uh_sport;
361                 udp_in.sin_addr = ip->ip_src;
362                 /*
363                  * Locate pcb(s) for datagram.
364                  * (Algorithm copied from raw_intr().)
365                  */
366                 last = NULL;
367 #ifdef INET6
368                 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
369 #endif
370                 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
371                         if (inp->inp_flags & INP_PLACEMARKER)
372                                 continue;
373 #ifdef INET6
374                         if (!(inp->inp_vflag & INP_IPV4))
375                                 continue;
376 #endif
377                         if (inp->inp_lport != uh->uh_dport)
378                                 continue;
379                         if (inp->inp_laddr.s_addr != INADDR_ANY) {
380                                 if (inp->inp_laddr.s_addr !=
381                                     ip->ip_dst.s_addr)
382                                         continue;
383                         }
384                         if (inp->inp_faddr.s_addr != INADDR_ANY) {
385                                 if (inp->inp_faddr.s_addr !=
386                                     ip->ip_src.s_addr ||
387                                     inp->inp_fport != uh->uh_sport)
388                                         continue;
389                         }
390
391                         if (check_multicast_membership(ip, inp, m) < 0)
392                                 continue;
393
394                         if (last != NULL) {
395                                 struct mbuf *n;
396
397 #ifdef IPSEC
398                                 /* check AH/ESP integrity. */
399                                 if (ipsec4_in_reject_so(m, last->inp_socket))
400                                         ipsecstat.in_polvio++;
401                                         /* do not inject data to pcb */
402                                 else
403 #endif /*IPSEC*/
404 #ifdef FAST_IPSEC
405                                 /* check AH/ESP integrity. */
406                                 if (ipsec4_in_reject(m, last))
407                                         ;
408                                 else
409 #endif /*FAST_IPSEC*/
410                                 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
411                                         udp_append(last, ip, n,
412                                                    iphlen +
413                                                    sizeof(struct udphdr));
414                         }
415                         last = inp;
416                         /*
417                          * Don't look for additional matches if this one does
418                          * not have either the SO_REUSEPORT or SO_REUSEADDR
419                          * socket options set.  This heuristic avoids searching
420                          * through all pcbs in the common case of a non-shared
421                          * port.  It * assumes that an application will never
422                          * clear these options after setting them.
423                          */
424                         if (!(last->inp_socket->so_options &
425                             (SO_REUSEPORT | SO_REUSEADDR)))
426                                 break;
427                 }
428
429                 if (last == NULL) {
430                         /*
431                          * No matching pcb found; discard datagram.
432                          * (No need to send an ICMP Port Unreachable
433                          * for a broadcast or multicast datgram.)
434                          */
435                         udpstat.udps_noportbcast++;
436                         goto bad;
437                 }
438 #ifdef IPSEC
439                 /* check AH/ESP integrity. */
440                 if (ipsec4_in_reject_so(m, last->inp_socket)) {
441                         ipsecstat.in_polvio++;
442                         goto bad;
443                 }
444 #endif /*IPSEC*/
445 #ifdef FAST_IPSEC
446                 /* check AH/ESP integrity. */
447                 if (ipsec4_in_reject(m, last))
448                         goto bad;
449 #endif /*FAST_IPSEC*/
450                 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
451                 return;
452         }
453         /*
454          * Locate pcb for datagram.
455          */
456         inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
457             ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
458         if (inp == NULL) {
459                 if (log_in_vain) {
460                         char buf[sizeof "aaa.bbb.ccc.ddd"];
461
462                         strcpy(buf, inet_ntoa(ip->ip_dst));
463                         log(LOG_INFO,
464                             "Connection attempt to UDP %s:%d from %s:%d\n",
465                             buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
466                             ntohs(uh->uh_sport));
467                 }
468                 udpstat.udps_noport++;
469                 if (m->m_flags & (M_BCAST | M_MCAST)) {
470                         udpstat.udps_noportbcast++;
471                         goto bad;
472                 }
473                 if (blackhole)
474                         goto bad;
475 #ifdef ICMP_BANDLIM
476                 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
477                         goto bad;
478 #endif
479                 *ip = save_ip;
480                 ip->ip_len += iphlen;
481                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
482                 return;
483         }
484 #ifdef IPSEC
485         if (ipsec4_in_reject_so(m, inp->inp_socket)) {
486                 ipsecstat.in_polvio++;
487                 goto bad;
488         }
489 #endif /*IPSEC*/
490 #ifdef FAST_IPSEC
491         if (ipsec4_in_reject(m, inp))
492                 goto bad;
493 #endif /*FAST_IPSEC*/
494
495         /*
496          * Construct sockaddr format source address.
497          * Stuff source address and datagram in user buffer.
498          */
499         udp_in.sin_port = uh->uh_sport;
500         udp_in.sin_addr = ip->ip_src;
501         if ((inp->inp_flags & INP_CONTROLOPTS) ||
502             (inp->inp_socket->so_options & SO_TIMESTAMP)) {
503 #ifdef INET6
504                 if (inp->inp_vflag & INP_IPV6) {
505                         int savedflags;
506
507                         ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
508                         savedflags = inp->inp_flags;
509                         inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
510                         ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
511                         inp->inp_flags = savedflags;
512                 } else
513 #endif
514                 ip_savecontrol(inp, &opts, ip, m);
515         }
516         m_adj(m, iphlen + sizeof(struct udphdr));
517 #ifdef INET6
518         if (inp->inp_vflag & INP_IPV6) {
519                 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
520                 append_sa = (struct sockaddr *)&udp_in6;
521         } else
522 #endif
523                 append_sa = (struct sockaddr *)&udp_in;
524         if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
525                 udpstat.udps_fullsock++;
526                 goto bad;
527         }
528         sorwakeup(inp->inp_socket);
529         return;
530 bad:
531         m_freem(m);
532         if (opts)
533                 m_freem(opts);
534         return;
535 }
536
537 #ifdef INET6
538 static void
539 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
540 {
541         bzero(ip6, sizeof *ip6);
542
543         ip6->ip6_vfc = IPV6_VERSION;
544         ip6->ip6_plen = ip->ip_len;
545         ip6->ip6_nxt = ip->ip_p;
546         ip6->ip6_hlim = ip->ip_ttl;
547         ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
548                 IPV6_ADDR_INT32_SMP;
549         ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
550         ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
551 }
552 #endif
553
554 /*
555  * subroutine of udp_input(), mainly for source code readability.
556  * caller must properly init udp_ip6 and udp_in6 beforehand.
557  */
558 static void
559 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off)
560 {
561         struct sockaddr *append_sa;
562         struct mbuf *opts = NULL;
563
564         if (last->inp_flags & INP_CONTROLOPTS ||
565             last->inp_socket->so_options & SO_TIMESTAMP) {
566 #ifdef INET6
567                 if (last->inp_vflag & INP_IPV6) {
568                         int savedflags;
569
570                         if (udp_ip6.uip6_init_done == 0) {
571                                 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
572                                 udp_ip6.uip6_init_done = 1;
573                         }
574                         savedflags = last->inp_flags;
575                         last->inp_flags &= ~INP_UNMAPPABLEOPTS;
576                         ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
577                         last->inp_flags = savedflags;
578                 } else
579 #endif
580                 ip_savecontrol(last, &opts, ip, n);
581         }
582 #ifdef INET6
583         if (last->inp_vflag & INP_IPV6) {
584                 if (udp_in6.uin6_init_done == 0) {
585                         in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
586                         udp_in6.uin6_init_done = 1;
587                 }
588                 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
589         } else
590 #endif
591                 append_sa = (struct sockaddr *)&udp_in;
592         m_adj(n, off);
593         if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
594                 m_freem(n);
595                 if (opts)
596                         m_freem(opts);
597                 udpstat.udps_fullsock++;
598         } else
599                 sorwakeup(last->inp_socket);
600 }
601
602 /*
603  * Notify a udp user of an asynchronous error;
604  * just wake up so that he can collect error status.
605  */
606 void
607 udp_notify(struct inpcb *inp, int errno)
608 {
609         inp->inp_socket->so_error = errno;
610         sorwakeup(inp->inp_socket);
611         sowwakeup(inp->inp_socket);
612 }
613
614 void
615 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
616 {
617         struct ip *ip = vip;
618         struct udphdr *uh;
619         void (*notify) (struct inpcb *, int) = udp_notify;
620         struct in_addr faddr;
621         struct inpcb *inp;
622
623         faddr = ((struct sockaddr_in *)sa)->sin_addr;
624         if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
625                 return;
626
627         if (PRC_IS_REDIRECT(cmd)) {
628                 ip = NULL;
629                 notify = in_rtchange;
630         } else if (cmd == PRC_HOSTDEAD)
631                 ip = NULL;
632         else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
633                 return;
634         if (ip) {
635                 crit_enter();
636                 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
637                 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
638                                         ip->ip_src, uh->uh_sport, 0, NULL);
639                 if (inp != NULL && inp->inp_socket != NULL)
640                         (*notify)(inp, inetctlerrmap[cmd]);
641                 crit_exit();
642         } else
643                 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
644                                 notify);
645 }
646
647 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
648             in_pcblist_global, "S,xinpcb", "List of active UDP sockets");
649
650 static int
651 udp_getcred(SYSCTL_HANDLER_ARGS)
652 {
653         struct sockaddr_in addrs[2];
654         struct inpcb *inp;
655         int error;
656
657         error = suser(req->td);
658         if (error)
659                 return (error);
660         error = SYSCTL_IN(req, addrs, sizeof addrs);
661         if (error)
662                 return (error);
663         crit_enter();
664         inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
665                                 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
666         if (inp == NULL || inp->inp_socket == NULL) {
667                 error = ENOENT;
668                 goto out;
669         }
670         error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
671 out:
672         crit_exit();
673         return (error);
674 }
675
676 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
677     0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
678
679 static int
680 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr,
681            struct mbuf *control, struct thread *td)
682 {
683         struct udpiphdr *ui;
684         int len = m->m_pkthdr.len;
685         struct sockaddr_in *sin;        /* really is initialized before use */
686         int error = 0;
687
688         if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
689                 error = EMSGSIZE;
690                 goto release;
691         }
692
693         if (inp->inp_lport == 0) {      /* unbound socket */
694                 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
695                 if (error)
696                         goto release;
697                 in_pcbinswildcardhash(inp);
698         }
699
700         if (dstaddr != NULL) {          /* destination address specified */
701                 if (inp->inp_faddr.s_addr != INADDR_ANY) {
702                         /* already connected */
703                         error = EISCONN;
704                         goto release;
705                 }
706                 sin = (struct sockaddr_in *)dstaddr;
707                 prison_remote_ip(td, 0, &sin->sin_addr.s_addr);
708         } else {
709                 if (inp->inp_faddr.s_addr == INADDR_ANY) {
710                         /* no destination specified and not already connected */
711                         error = ENOTCONN;
712                         goto release;
713                 }
714                 sin = NULL;
715         }
716
717         /*
718          * Calculate data length and get a mbuf
719          * for UDP and IP headers.
720          */
721         M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
722         if (m == NULL) {
723                 error = ENOBUFS;
724                 goto release;
725         }
726
727         /*
728          * Fill in mbuf with extended UDP header
729          * and addresses and length put into network format.
730          */
731         ui = mtod(m, struct udpiphdr *);
732         bzero(ui->ui_x1, sizeof ui->ui_x1);     /* XXX still needed? */
733         ui->ui_pr = IPPROTO_UDP;
734
735         /*
736          * Set destination address.
737          */
738         if (dstaddr != NULL) {                  /* use specified destination */
739                 ui->ui_dst = sin->sin_addr;
740                 ui->ui_dport = sin->sin_port;
741         } else {                                /* use connected destination */
742                 ui->ui_dst = inp->inp_faddr;
743                 ui->ui_dport = inp->inp_fport;
744         }
745
746         /*
747          * Set source address.
748          */
749         if (inp->inp_laddr.s_addr == INADDR_ANY) {
750                 struct sockaddr_in *if_sin;
751
752                 if (dstaddr == NULL) {  
753                         /*
754                          * connect() had (or should have) failed because
755                          * the interface had no IP address, but the
756                          * application proceeded to call send() anyways.
757                          */
758                         error = ENOTCONN;
759                         goto release;
760                 }
761
762                 /* Look up outgoing interface. */
763                 if ((error = in_pcbladdr(inp, dstaddr, &if_sin)))
764                         goto release;
765                 ui->ui_src = if_sin->sin_addr;  /* use address of interface */
766         } else {
767                 ui->ui_src = inp->inp_laddr;    /* use non-null bound address */
768         }
769         ui->ui_sport = inp->inp_lport;
770         KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
771
772         ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
773
774         /*
775          * Set up checksum and output datagram.
776          */
777         if (udpcksum) {
778                 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
779                     htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
780                 m->m_pkthdr.csum_flags = CSUM_UDP;
781                 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
782         } else {
783                 ui->ui_sum = 0;
784         }
785         ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
786         ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;    /* XXX */
787         ((struct ip *)ui)->ip_tos = inp->inp_ip_tos;    /* XXX */
788         udpstat.udps_opackets++;
789
790         error = ip_output(m, inp->inp_options, &inp->inp_route,
791             (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
792             inp->inp_moptions, inp);
793
794         return (error);
795
796 release:
797         m_freem(m);
798         return (error);
799 }
800
801 u_long  udp_sendspace = 9216;           /* really max datagram size */
802                                         /* 40 1K datagrams */
803 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
804     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
805
806 u_long  udp_recvspace = 40 * (1024 +
807 #ifdef INET6
808                                       sizeof(struct sockaddr_in6)
809 #else
810                                       sizeof(struct sockaddr_in)
811 #endif
812                                       );
813 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
814     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
815
816 static int
817 udp_abort(struct socket *so)
818 {
819         struct inpcb *inp;
820
821         inp = so->so_pcb;
822         if (inp == NULL)
823                 return EINVAL;  /* ??? possible? panic instead? */
824         soisdisconnected(so);
825         crit_enter();
826         in_pcbdetach(inp);
827         crit_exit();
828         return 0;
829 }
830
831 static int
832 udp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
833 {
834         struct inpcb *inp;
835         int error;
836
837         inp = so->so_pcb;
838         if (inp != NULL)
839                 return EINVAL;
840
841         error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
842         if (error)
843                 return error;
844         crit_enter();
845         error = in_pcballoc(so, &udbinfo);
846         crit_exit();
847         if (error)
848                 return error;
849
850         inp = (struct inpcb *)so->so_pcb;
851         inp->inp_vflag |= INP_IPV4;
852         inp->inp_ip_ttl = ip_defttl;
853         return 0;
854 }
855
856 static int
857 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
858 {
859         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
860         struct inpcb *inp;
861         int error;
862
863         inp = so->so_pcb;
864         if (inp == NULL)
865                 return EINVAL;
866         crit_enter();
867         error = in_pcbbind(inp, nam, td);
868         crit_exit();
869         if (error == 0) {
870                 if (sin->sin_addr.s_addr != INADDR_ANY)
871                         inp->inp_flags |= INP_WASBOUND_NOTANY;
872                 in_pcbinswildcardhash(inp);
873         }
874         return error;
875 }
876
877 static int
878 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
879 {
880         struct inpcb *inp;
881         int error;
882         struct sockaddr_in *sin;
883
884         inp = so->so_pcb;
885         if (inp == NULL)
886                 return EINVAL;
887         if (inp->inp_faddr.s_addr != INADDR_ANY)
888                 return EISCONN;
889         error = 0;
890         crit_enter();
891         if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
892             inp->inp_laddr.s_addr == INADDR_ANY) {
893                 error = in_pcbbind(inp, NULL, td);
894         }
895         if (error == 0) {
896                 sin = (struct sockaddr_in *)nam;
897                 prison_remote_ip(td, 0, &sin->sin_addr.s_addr);
898                 if (inp->inp_flags & INP_WILDCARD)
899                         in_pcbremwildcardhash(inp);
900                 error = in_pcbconnect(inp, nam, td);
901         }
902         crit_exit();
903         if (error == 0)
904                 soisconnected(so);
905         else if (error == EAFNOSUPPORT) {       /* connection dissolved */
906                 /*
907                  * Follow traditional BSD behavior and retain
908                  * the local port binding.  But, fix the old misbehavior
909                  * of overwriting any previously bound local address.
910                  */
911                 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
912                         inp->inp_laddr.s_addr = INADDR_ANY;
913                 in_pcbinswildcardhash(inp);
914         }
915         return error;
916 }
917
918 static int
919 udp_detach(struct socket *so)
920 {
921         struct inpcb *inp;
922
923         inp = so->so_pcb;
924         if (inp == NULL)
925                 return EINVAL;
926         crit_enter();
927         in_pcbdetach(inp);
928         crit_exit();
929         return 0;
930 }
931
932 static int
933 udp_disconnect(struct socket *so)
934 {
935         struct inpcb *inp;
936
937         inp = so->so_pcb;
938         if (inp == NULL)
939                 return EINVAL;
940         if (inp->inp_faddr.s_addr == INADDR_ANY)
941                 return ENOTCONN;
942
943         crit_enter();
944         in_pcbdisconnect(inp);
945         crit_exit();
946         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
947         return 0;
948 }
949
950 static int
951 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
952             struct mbuf *control, struct thread *td)
953 {
954         struct inpcb *inp;
955
956         inp = so->so_pcb;
957         if (inp == NULL) {
958                 m_freem(m);
959                 return EINVAL;
960         }
961         return udp_output(inp, m, addr, control, td);
962 }
963
964 int
965 udp_shutdown(struct socket *so)
966 {
967         struct inpcb *inp;
968
969         inp = so->so_pcb;
970         if (inp == NULL)
971                 return EINVAL;
972         socantsendmore(so);
973         return 0;
974 }
975
976 struct pr_usrreqs udp_usrreqs = {
977         udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
978         pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
979         pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
980         pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
981         in_setsockaddr, sosendudp, soreceive, sopoll
982 };
983