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