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