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