]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/udp_usrreq.c
Add additional udbinfo and inpcb locking assertions to udp_output(); for
[FreeBSD/FreeBSD.git] / sys / netinet / udp_usrreq.c
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3  *      The Regents of the University of California.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "opt_ipfw.h"
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
39 #include "opt_mac.h"
40
41 #include <sys/param.h>
42 #include <sys/domain.h>
43 #include <sys/eventhandler.h>
44 #include <sys/jail.h>
45 #include <sys/kernel.h>
46 #include <sys/lock.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/protosw.h>
52 #include <sys/signalvar.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/sx.h>
56 #include <sys/sysctl.h>
57 #include <sys/syslog.h>
58 #include <sys/systm.h>
59
60 #include <vm/uma.h>
61
62 #include <net/if.h>
63 #include <net/route.h>
64
65 #include <netinet/in.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip.h>
70 #ifdef INET6
71 #include <netinet/ip6.h>
72 #endif
73 #include <netinet/ip_icmp.h>
74 #include <netinet/icmp_var.h>
75 #include <netinet/ip_var.h>
76 #include <netinet/ip_options.h>
77 #ifdef INET6
78 #include <netinet6/ip6_var.h>
79 #endif
80 #include <netinet/udp.h>
81 #include <netinet/udp_var.h>
82
83 #ifdef IPSEC
84 #include <netipsec/ipsec.h>
85 #endif
86
87 #include <machine/in_cksum.h>
88
89 #include <security/mac/mac_framework.h>
90
91 /*
92  * UDP protocol implementation.
93  * Per RFC 768, August, 1980.
94  */
95
96 /*
97  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
98  * removes the only data integrity mechanism for packets and malformed
99  * packets that would otherwise be discarded due to bad checksums, and may
100  * cause problems (especially for NFS data blocks).
101  */
102 static int      udp_cksum = 1;
103 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udp_cksum,
104     0, "");
105
106 int     udp_log_in_vain = 0;
107 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
108     &udp_log_in_vain, 0, "Log all incoming UDP packets");
109
110 int     udp_blackhole = 0;
111 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, &udp_blackhole, 0,
112     "Do not send port unreachables for refused connects");
113
114 u_long  udp_sendspace = 9216;           /* really max datagram size */
115                                         /* 40 1K datagrams */
116 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
117     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
118
119 u_long  udp_recvspace = 40 * (1024 +
120 #ifdef INET6
121                                       sizeof(struct sockaddr_in6)
122 #else
123                                       sizeof(struct sockaddr_in)
124 #endif
125                                       );
126
127 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
128     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
129
130 struct inpcbhead        udb;            /* from udp_var.h */
131 struct inpcbinfo        udbinfo;
132
133 #ifndef UDBHASHSIZE
134 #define UDBHASHSIZE     16
135 #endif
136
137 struct udpstat  udpstat;        /* from udp_var.h */
138 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, &udpstat,
139     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
140
141 static void     udp_detach(struct socket *so);
142 static int      udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
143                     struct mbuf *, struct thread *);
144
145 static void
146 udp_zone_change(void *tag)
147 {
148
149         uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
150 }
151
152 static int
153 udp_inpcb_init(void *mem, int size, int flags)
154 {
155         struct inpcb *inp;
156
157         inp = mem;
158         INP_LOCK_INIT(inp, "inp", "udpinp");
159         return (0);
160 }
161
162 void
163 udp_init(void)
164 {
165
166         INP_INFO_LOCK_INIT(&udbinfo, "udp");
167         LIST_INIT(&udb);
168         udbinfo.ipi_listhead = &udb;
169         udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB,
170             &udbinfo.ipi_hashmask);
171         udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB,
172             &udbinfo.ipi_porthashmask);
173         udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
174             NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
175         uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
176         EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
177             EVENTHANDLER_PRI_ANY);
178 }
179
180 /*
181  * Subroutine of udp_input(), which appends the provided mbuf chain to the
182  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
183  * contains the source address.  If the socket ends up being an IPv6 socket,
184  * udp_append() will convert to a sockaddr_in6 before passing the address
185  * into the socket code.
186  */
187 static void
188 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
189     struct sockaddr_in *udp_in)
190 {
191         struct sockaddr *append_sa;
192         struct socket *so;
193         struct mbuf *opts = 0;
194 #ifdef INET6
195         struct sockaddr_in6 udp_in6;
196 #endif
197
198         INP_RLOCK_ASSERT(inp);
199
200 #ifdef IPSEC
201         /* Check AH/ESP integrity. */
202         if (ipsec4_in_reject(n, inp)) {
203                 m_freem(n);
204                 ipsec4stat.in_polvio++;
205                 return;
206         }
207 #endif /* IPSEC */
208 #ifdef MAC
209         if (mac_inpcb_check_deliver(inp, n) != 0) {
210                 m_freem(n);
211                 return;
212         }
213 #endif
214         if (inp->inp_flags & INP_CONTROLOPTS ||
215             inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
216 #ifdef INET6
217                 if (inp->inp_vflag & INP_IPV6)
218                         ip6_savecontrol_v4(inp, n, &opts);
219                 else
220 #endif
221                         ip_savecontrol(inp, &opts, ip, n);
222         }
223 #ifdef INET6
224         if (inp->inp_vflag & INP_IPV6) {
225                 bzero(&udp_in6, sizeof(udp_in6));
226                 udp_in6.sin6_len = sizeof(udp_in6);
227                 udp_in6.sin6_family = AF_INET6;
228                 in6_sin_2_v4mapsin6(udp_in, &udp_in6);
229                 append_sa = (struct sockaddr *)&udp_in6;
230         } else
231 #endif
232                 append_sa = (struct sockaddr *)udp_in;
233         m_adj(n, off);
234
235         so = inp->inp_socket;
236         SOCKBUF_LOCK(&so->so_rcv);
237         if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
238                 SOCKBUF_UNLOCK(&so->so_rcv);
239                 m_freem(n);
240                 if (opts)
241                         m_freem(opts);
242                 udpstat.udps_fullsock++;
243         } else
244                 sorwakeup_locked(so);
245 }
246
247 void
248 udp_input(struct mbuf *m, int off)
249 {
250         int iphlen = off;
251         struct ip *ip;
252         struct udphdr *uh;
253         struct ifnet *ifp;
254         struct inpcb *inp;
255         int len;
256         struct ip save_ip;
257         struct sockaddr_in udp_in;
258 #ifdef IPFIREWALL_FORWARD
259         struct m_tag *fwd_tag;
260 #endif
261
262         ifp = m->m_pkthdr.rcvif;
263         udpstat.udps_ipackets++;
264
265         /*
266          * Strip IP options, if any; should skip this, make available to
267          * user, and use on returned packets, but we don't yet have a way to
268          * check the checksum with options still present.
269          */
270         if (iphlen > sizeof (struct ip)) {
271                 ip_stripoptions(m, (struct mbuf *)0);
272                 iphlen = sizeof(struct ip);
273         }
274
275         /*
276          * Get IP and UDP header together in first mbuf.
277          */
278         ip = mtod(m, struct ip *);
279         if (m->m_len < iphlen + sizeof(struct udphdr)) {
280                 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
281                         udpstat.udps_hdrops++;
282                         return;
283                 }
284                 ip = mtod(m, struct ip *);
285         }
286         uh = (struct udphdr *)((caddr_t)ip + iphlen);
287
288         /*
289          * Destination port of 0 is illegal, based on RFC768.
290          */
291         if (uh->uh_dport == 0)
292                 goto badunlocked;
293
294         /*
295          * Construct sockaddr format source address.  Stuff source address
296          * and datagram in user buffer.
297          */
298         bzero(&udp_in, sizeof(udp_in));
299         udp_in.sin_len = sizeof(udp_in);
300         udp_in.sin_family = AF_INET;
301         udp_in.sin_port = uh->uh_sport;
302         udp_in.sin_addr = ip->ip_src;
303
304         /*
305          * Make mbuf data length reflect UDP length.  If not enough data to
306          * reflect UDP length, drop.
307          */
308         len = ntohs((u_short)uh->uh_ulen);
309         if (ip->ip_len != len) {
310                 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
311                         udpstat.udps_badlen++;
312                         goto badunlocked;
313                 }
314                 m_adj(m, len - ip->ip_len);
315                 /* ip->ip_len = len; */
316         }
317
318         /*
319          * Save a copy of the IP header in case we want restore it for
320          * sending an ICMP error message in response.
321          */
322         if (!udp_blackhole)
323                 save_ip = *ip;
324         else
325                 memset(&save_ip, 0, sizeof(save_ip));
326
327         /*
328          * Checksum extended UDP header and data.
329          */
330         if (uh->uh_sum) {
331                 u_short uh_sum;
332
333                 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
334                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
335                                 uh_sum = m->m_pkthdr.csum_data;
336                         else
337                                 uh_sum = in_pseudo(ip->ip_src.s_addr,
338                                     ip->ip_dst.s_addr, htonl((u_short)len +
339                                     m->m_pkthdr.csum_data + IPPROTO_UDP));
340                         uh_sum ^= 0xffff;
341                 } else {
342                         char b[9];
343
344                         bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
345                         bzero(((struct ipovly *)ip)->ih_x1, 9);
346                         ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
347                         uh_sum = in_cksum(m, len + sizeof (struct ip));
348                         bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
349                 }
350                 if (uh_sum) {
351                         udpstat.udps_badsum++;
352                         m_freem(m);
353                         return;
354                 }
355         } else
356                 udpstat.udps_nosum++;
357
358 #ifdef IPFIREWALL_FORWARD
359         /*
360          * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
361          */
362         fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
363         if (fwd_tag != NULL) {
364                 struct sockaddr_in *next_hop;
365
366                 /*
367                  * Do the hack.
368                  */
369                 next_hop = (struct sockaddr_in *)(fwd_tag + 1);
370                 ip->ip_dst = next_hop->sin_addr;
371                 uh->uh_dport = ntohs(next_hop->sin_port);
372
373                 /*
374                  * Remove the tag from the packet.  We don't need it anymore.
375                  */
376                 m_tag_delete(m, fwd_tag);
377         }
378 #endif
379
380         INP_INFO_RLOCK(&udbinfo);
381         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
382             in_broadcast(ip->ip_dst, ifp)) {
383                 struct inpcb *last;
384                 struct ip_moptions *imo;
385
386                 last = NULL;
387                 LIST_FOREACH(inp, &udb, inp_list) {
388                         if (inp->inp_lport != uh->uh_dport)
389                                 continue;
390 #ifdef INET6
391                         if ((inp->inp_vflag & INP_IPV4) == 0)
392                                 continue;
393 #endif
394                         if (inp->inp_laddr.s_addr != INADDR_ANY &&
395                             inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
396                                 continue;
397                         if (inp->inp_faddr.s_addr != INADDR_ANY &&
398                             inp->inp_faddr.s_addr != ip->ip_src.s_addr)
399                                 continue;
400                         /*
401                          * XXX: Do not check source port of incoming datagram
402                          * unless inp_connect() has been called to bind the
403                          * fport part of the 4-tuple; the source could be
404                          * trying to talk to us with an ephemeral port.
405                          */
406                         if (inp->inp_fport != 0 &&
407                             inp->inp_fport != uh->uh_sport)
408                                 continue;
409
410                         INP_RLOCK(inp);
411
412                         /*
413                          * Handle socket delivery policy for any-source
414                          * and source-specific multicast. [RFC3678]
415                          */
416                         imo = inp->inp_moptions;
417                         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
418                             imo != NULL) {
419                                 struct sockaddr_in       sin;
420                                 struct in_msource       *ims;
421                                 int                      blocked, mode;
422                                 size_t                   idx;
423
424                                 bzero(&sin, sizeof(struct sockaddr_in));
425                                 sin.sin_len = sizeof(struct sockaddr_in);
426                                 sin.sin_family = AF_INET;
427                                 sin.sin_addr = ip->ip_dst;
428
429                                 blocked = 0;
430                                 idx = imo_match_group(imo, ifp,
431                                     (struct sockaddr *)&sin);
432                                 if (idx == -1) {
433                                         /*
434                                          * No group membership for this socket.
435                                          * Do not bump udps_noportbcast, as
436                                          * this will happen further down.
437                                          */
438                                         blocked++;
439                                 } else {
440                                         /*
441                                          * Check for a multicast source filter
442                                          * entry on this socket for this group.
443                                          * MCAST_EXCLUDE is the default
444                                          * behaviour.  It means default accept;
445                                          * entries, if present, denote sources
446                                          * to be excluded from delivery.
447                                          */
448                                         ims = imo_match_source(imo, idx,
449                                             (struct sockaddr *)&udp_in);
450                                         mode = imo->imo_mfilters[idx].imf_fmode;
451                                         if ((ims != NULL &&
452                                              mode == MCAST_EXCLUDE) ||
453                                             (ims == NULL &&
454                                              mode == MCAST_INCLUDE)) {
455 #ifdef DIAGNOSTIC
456                                                 if (bootverbose) {
457                                                         printf("%s: blocked by"
458                                                             " source filter\n",
459                                                             __func__);
460                                                 }
461 #endif
462                                                 udpstat.udps_filtermcast++;
463                                                 blocked++;
464                                         }
465                                 }
466                                 if (blocked != 0) {
467                                         INP_RUNLOCK(inp);
468                                         continue;
469                                 }
470                         }
471                         if (last != NULL) {
472                                 struct mbuf *n;
473
474                                 n = m_copy(m, 0, M_COPYALL);
475                                 if (n != NULL)
476                                         udp_append(last, ip, n, iphlen +
477                                             sizeof(struct udphdr), &udp_in);
478                                 INP_RUNLOCK(last);
479                         }
480                         last = inp;
481                         /*
482                          * Don't look for additional matches if this one does
483                          * not have either the SO_REUSEPORT or SO_REUSEADDR
484                          * socket options set.  This heuristic avoids
485                          * searching through all pcbs in the common case of a
486                          * non-shared port.  It assumes that an application
487                          * will never clear these options after setting them.
488                          */
489                         if ((last->inp_socket->so_options &
490                             (SO_REUSEPORT|SO_REUSEADDR)) == 0)
491                                 break;
492                 }
493
494                 if (last == NULL) {
495                         /*
496                          * No matching pcb found; discard datagram.  (No need
497                          * to send an ICMP Port Unreachable for a broadcast
498                          * or multicast datgram.)
499                          */
500                         udpstat.udps_noportbcast++;
501                         goto badheadlocked;
502                 }
503                 udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
504                     &udp_in);
505                 INP_RUNLOCK(last);
506                 INP_INFO_RUNLOCK(&udbinfo);
507                 return;
508         }
509
510         /*
511          * Locate pcb for datagram.
512          */
513         inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
514             ip->ip_dst, uh->uh_dport, 1, ifp);
515         if (inp == NULL) {
516                 if (udp_log_in_vain) {
517                         char buf[4*sizeof "123"];
518
519                         strcpy(buf, inet_ntoa(ip->ip_dst));
520                         log(LOG_INFO,
521                             "Connection attempt to UDP %s:%d from %s:%d\n",
522                             buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
523                             ntohs(uh->uh_sport));
524                 }
525                 udpstat.udps_noport++;
526                 if (m->m_flags & (M_BCAST | M_MCAST)) {
527                         udpstat.udps_noportbcast++;
528                         goto badheadlocked;
529                 }
530                 if (udp_blackhole)
531                         goto badheadlocked;
532                 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
533                         goto badheadlocked;
534                 *ip = save_ip;
535                 ip->ip_len += iphlen;
536                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
537                 INP_INFO_RUNLOCK(&udbinfo);
538                 return;
539         }
540
541         /*
542          * Check the minimum TTL for socket.
543          */
544         INP_RLOCK(inp);
545         INP_INFO_RUNLOCK(&udbinfo);
546         if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
547                 INP_RUNLOCK(inp);
548                 goto badunlocked;
549         }
550         udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
551         INP_RUNLOCK(inp);
552         return;
553
554 badheadlocked:
555         if (inp)
556                 INP_RUNLOCK(inp);
557         INP_INFO_RUNLOCK(&udbinfo);
558 badunlocked:
559         m_freem(m);
560 }
561
562 /*
563  * Notify a udp user of an asynchronous error; just wake up so that they can
564  * collect error status.
565  */
566 struct inpcb *
567 udp_notify(struct inpcb *inp, int errno)
568 {
569
570         INP_WLOCK_ASSERT(inp);
571
572         inp->inp_socket->so_error = errno;
573         sorwakeup(inp->inp_socket);
574         sowwakeup(inp->inp_socket);
575         return (inp);
576 }
577
578 void
579 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
580 {
581         struct ip *ip = vip;
582         struct udphdr *uh;
583         struct in_addr faddr;
584         struct inpcb *inp;
585
586         faddr = ((struct sockaddr_in *)sa)->sin_addr;
587         if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
588                 return;
589
590         /*
591          * Redirects don't need to be handled up here.
592          */
593         if (PRC_IS_REDIRECT(cmd))
594                 return;
595
596         /*
597          * Hostdead is ugly because it goes linearly through all PCBs.
598          *
599          * XXX: We never get this from ICMP, otherwise it makes an excellent
600          * DoS attack on machines with many connections.
601          */
602         if (cmd == PRC_HOSTDEAD)
603                 ip = NULL;
604         else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
605                 return;
606         if (ip != NULL) {
607                 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
608                 INP_INFO_RLOCK(&udbinfo);
609                 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
610                     ip->ip_src, uh->uh_sport, 0, NULL);
611                 if (inp != NULL) {
612                         INP_WLOCK(inp);
613                         if (inp->inp_socket != NULL) {
614                                 udp_notify(inp, inetctlerrmap[cmd]);
615                         }
616                         INP_WUNLOCK(inp);
617                 }
618                 INP_INFO_RUNLOCK(&udbinfo);
619         } else
620                 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd],
621                     udp_notify);
622 }
623
624 static int
625 udp_pcblist(SYSCTL_HANDLER_ARGS)
626 {
627         int error, i, n;
628         struct inpcb *inp, **inp_list;
629         inp_gen_t gencnt;
630         struct xinpgen xig;
631
632         /*
633          * The process of preparing the PCB list is too time-consuming and
634          * resource-intensive to repeat twice on every request.
635          */
636         if (req->oldptr == 0) {
637                 n = udbinfo.ipi_count;
638                 req->oldidx = 2 * (sizeof xig)
639                         + (n + n/8) * sizeof(struct xinpcb);
640                 return (0);
641         }
642
643         if (req->newptr != 0)
644                 return (EPERM);
645
646         /*
647          * OK, now we're committed to doing something.
648          */
649         INP_INFO_RLOCK(&udbinfo);
650         gencnt = udbinfo.ipi_gencnt;
651         n = udbinfo.ipi_count;
652         INP_INFO_RUNLOCK(&udbinfo);
653
654         error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
655                 + n * sizeof(struct xinpcb));
656         if (error != 0)
657                 return (error);
658
659         xig.xig_len = sizeof xig;
660         xig.xig_count = n;
661         xig.xig_gen = gencnt;
662         xig.xig_sogen = so_gencnt;
663         error = SYSCTL_OUT(req, &xig, sizeof xig);
664         if (error)
665                 return (error);
666
667         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
668         if (inp_list == 0)
669                 return (ENOMEM);
670
671         INP_INFO_RLOCK(&udbinfo);
672         for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n;
673              inp = LIST_NEXT(inp, inp_list)) {
674                 INP_RLOCK(inp);
675                 if (inp->inp_gencnt <= gencnt &&
676                     cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
677                         inp_list[i++] = inp;
678                 INP_RUNLOCK(inp);
679         }
680         INP_INFO_RUNLOCK(&udbinfo);
681         n = i;
682
683         error = 0;
684         for (i = 0; i < n; i++) {
685                 inp = inp_list[i];
686                 INP_RLOCK(inp);
687                 if (inp->inp_gencnt <= gencnt) {
688                         struct xinpcb xi;
689                         bzero(&xi, sizeof(xi));
690                         xi.xi_len = sizeof xi;
691                         /* XXX should avoid extra copy */
692                         bcopy(inp, &xi.xi_inp, sizeof *inp);
693                         if (inp->inp_socket)
694                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
695                         xi.xi_inp.inp_gencnt = inp->inp_gencnt;
696                         INP_RUNLOCK(inp);
697                         error = SYSCTL_OUT(req, &xi, sizeof xi);
698                 } else
699                         INP_RUNLOCK(inp);
700         }
701         if (!error) {
702                 /*
703                  * Give the user an updated idea of our state.  If the
704                  * generation differs from what we told her before, she knows
705                  * that something happened while we were processing this
706                  * request, and it might be necessary to retry.
707                  */
708                 INP_INFO_RLOCK(&udbinfo);
709                 xig.xig_gen = udbinfo.ipi_gencnt;
710                 xig.xig_sogen = so_gencnt;
711                 xig.xig_count = udbinfo.ipi_count;
712                 INP_INFO_RUNLOCK(&udbinfo);
713                 error = SYSCTL_OUT(req, &xig, sizeof xig);
714         }
715         free(inp_list, M_TEMP);
716         return (error);
717 }
718
719 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
720     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
721
722 static int
723 udp_getcred(SYSCTL_HANDLER_ARGS)
724 {
725         struct xucred xuc;
726         struct sockaddr_in addrs[2];
727         struct inpcb *inp;
728         int error;
729
730         error = priv_check(req->td, PRIV_NETINET_GETCRED);
731         if (error)
732                 return (error);
733         error = SYSCTL_IN(req, addrs, sizeof(addrs));
734         if (error)
735                 return (error);
736         INP_INFO_RLOCK(&udbinfo);
737         inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
738                                 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
739         if (inp != NULL) {
740                 INP_RLOCK(inp);
741                 INP_INFO_RUNLOCK(&udbinfo);
742                 if (inp->inp_socket == NULL)
743                         error = ENOENT;
744                 if (error == 0)
745                         error = cr_canseesocket(req->td->td_ucred,
746                             inp->inp_socket);
747                 if (error == 0)
748                         cru2x(inp->inp_socket->so_cred, &xuc);
749                 INP_RUNLOCK(inp);
750         } else {
751                 INP_INFO_RUNLOCK(&udbinfo);
752                 error = ENOENT;
753         }
754         if (error == 0)
755                 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
756         return (error);
757 }
758
759 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
760     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
761     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
762
763 static int
764 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
765     struct mbuf *control, struct thread *td)
766 {
767         struct udpiphdr *ui;
768         int len = m->m_pkthdr.len;
769         struct in_addr faddr, laddr;
770         struct cmsghdr *cm;
771         struct sockaddr_in *sin, src;
772         int error = 0;
773         int ipflags;
774         u_short fport, lport;
775         int unlock_udbinfo;
776
777         /*
778          * udp_output() may need to temporarily bind or connect the current
779          * inpcb.  As such, we don't know up front whether we will need the
780          * pcbinfo lock or not.  Do any work to decide what is needed up
781          * front before acquiring any locks.
782          */
783         if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
784                 if (control)
785                         m_freem(control);
786                 m_freem(m);
787                 return (EMSGSIZE);
788         }
789
790         src.sin_family = 0;
791         if (control != NULL) {
792                 /*
793                  * XXX: Currently, we assume all the optional information is
794                  * stored in a single mbuf.
795                  */
796                 if (control->m_next) {
797                         m_freem(control);
798                         m_freem(m);
799                         return (EINVAL);
800                 }
801                 for (; control->m_len > 0;
802                     control->m_data += CMSG_ALIGN(cm->cmsg_len),
803                     control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
804                         cm = mtod(control, struct cmsghdr *);
805                         if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
806                             || cm->cmsg_len > control->m_len) {
807                                 error = EINVAL;
808                                 break;
809                         }
810                         if (cm->cmsg_level != IPPROTO_IP)
811                                 continue;
812
813                         switch (cm->cmsg_type) {
814                         case IP_SENDSRCADDR:
815                                 if (cm->cmsg_len !=
816                                     CMSG_LEN(sizeof(struct in_addr))) {
817                                         error = EINVAL;
818                                         break;
819                                 }
820                                 bzero(&src, sizeof(src));
821                                 src.sin_family = AF_INET;
822                                 src.sin_len = sizeof(src);
823                                 src.sin_port = inp->inp_lport;
824                                 src.sin_addr =
825                                     *(struct in_addr *)CMSG_DATA(cm);
826                                 break;
827
828                         default:
829                                 error = ENOPROTOOPT;
830                                 break;
831                         }
832                         if (error)
833                                 break;
834                 }
835                 m_freem(control);
836         }
837         if (error) {
838                 m_freem(m);
839                 return (error);
840         }
841
842         if (src.sin_family == AF_INET || addr != NULL) {
843                 INP_INFO_WLOCK(&udbinfo);
844                 INP_WLOCK(inp);
845                 unlock_udbinfo = 1;
846         } else {
847                 unlock_udbinfo = 0;
848                 INP_RLOCK(inp);
849         }
850
851 #ifdef MAC
852         mac_inpcb_create_mbuf(inp, m);
853 #endif
854
855         /*
856          * If the IP_SENDSRCADDR control message was specified, override the
857          * source address for this datagram.  Its use is invalidated if the
858          * address thus specified is incomplete or clobbers other inpcbs.
859          */
860         laddr = inp->inp_laddr;
861         lport = inp->inp_lport;
862         if (src.sin_family == AF_INET) {
863                 INP_INFO_LOCK_ASSERT(&udbinfo);
864                 if ((lport == 0) ||
865                     (laddr.s_addr == INADDR_ANY &&
866                      src.sin_addr.s_addr == INADDR_ANY)) {
867                         error = EINVAL;
868                         goto release;
869                 }
870                 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
871                     &laddr.s_addr, &lport, td->td_ucred);
872                 if (error)
873                         goto release;
874         }
875
876         if (addr) {
877                 INP_INFO_LOCK_ASSERT(&udbinfo);
878                 INP_LOCK_ASSERT(inp);
879                 sin = (struct sockaddr_in *)addr;
880                 if (jailed(td->td_ucred))
881                         prison_remote_ip(td->td_ucred, 0,
882                             &sin->sin_addr.s_addr);
883                 if (inp->inp_faddr.s_addr != INADDR_ANY) {
884                         error = EISCONN;
885                         goto release;
886                 }
887                 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
888                     &faddr.s_addr, &fport, NULL, td->td_ucred);
889                 if (error)
890                         goto release;
891
892                 /* Commit the local port if newly assigned. */
893                 if (inp->inp_laddr.s_addr == INADDR_ANY &&
894                     inp->inp_lport == 0) {
895                         INP_INFO_WLOCK_ASSERT(&udbinfo);
896                         INP_WLOCK_ASSERT(inp);
897                         /*
898                          * Remember addr if jailed, to prevent rebinding.
899                          */
900                         if (jailed(td->td_ucred))
901                                 inp->inp_laddr = laddr;
902                         inp->inp_lport = lport;
903                         if (in_pcbinshash(inp) != 0) {
904                                 inp->inp_lport = 0;
905                                 error = EAGAIN;
906                                 goto release;
907                         }
908                         inp->inp_flags |= INP_ANONPORT;
909                 }
910         } else {
911                 INP_LOCK_ASSERT(inp);
912                 faddr = inp->inp_faddr;
913                 fport = inp->inp_fport;
914                 if (faddr.s_addr == INADDR_ANY) {
915                         error = ENOTCONN;
916                         goto release;
917                 }
918         }
919
920         /*
921          * Calculate data length and get a mbuf for UDP, IP, and possible
922          * link-layer headers.  Immediate slide the data pointer back forward
923          * since we won't use that space at this layer.
924          */
925         M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
926         if (m == NULL) {
927                 error = ENOBUFS;
928                 goto release;
929         }
930         m->m_data += max_linkhdr;
931         m->m_len -= max_linkhdr;
932         m->m_pkthdr.len -= max_linkhdr;
933
934         /*
935          * Fill in mbuf with extended UDP header and addresses and length put
936          * into network format.
937          */
938         ui = mtod(m, struct udpiphdr *);
939         bzero(ui->ui_x1, sizeof(ui->ui_x1));    /* XXX still needed? */
940         ui->ui_pr = IPPROTO_UDP;
941         ui->ui_src = laddr;
942         ui->ui_dst = faddr;
943         ui->ui_sport = lport;
944         ui->ui_dport = fport;
945         ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
946
947         /*
948          * Set the Don't Fragment bit in the IP header.
949          */
950         if (inp->inp_flags & INP_DONTFRAG) {
951                 struct ip *ip;
952
953                 ip = (struct ip *)&ui->ui_i;
954                 ip->ip_off |= IP_DF;
955         }
956
957         ipflags = 0;
958         if (inp->inp_socket->so_options & SO_DONTROUTE)
959                 ipflags |= IP_ROUTETOIF;
960         if (inp->inp_socket->so_options & SO_BROADCAST)
961                 ipflags |= IP_ALLOWBROADCAST;
962         if (inp->inp_flags & INP_ONESBCAST)
963                 ipflags |= IP_SENDONES;
964
965         /*
966          * Set up checksum and output datagram.
967          */
968         if (udp_cksum) {
969                 if (inp->inp_flags & INP_ONESBCAST)
970                         faddr.s_addr = INADDR_BROADCAST;
971                 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
972                     htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
973                 m->m_pkthdr.csum_flags = CSUM_UDP;
974                 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
975         } else
976                 ui->ui_sum = 0;
977         ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
978         ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;    /* XXX */
979         ((struct ip *)ui)->ip_tos = inp->inp_ip_tos;    /* XXX */
980         udpstat.udps_opackets++;
981
982         if (unlock_udbinfo)
983                 INP_INFO_WUNLOCK(&udbinfo);
984         error = ip_output(m, inp->inp_options, NULL, ipflags,
985             inp->inp_moptions, inp);
986         if (unlock_udbinfo)
987                 INP_WUNLOCK(inp);
988         else
989                 INP_RUNLOCK(inp);
990         return (error);
991
992 release:
993         if (unlock_udbinfo) {
994                 INP_INFO_WUNLOCK(&udbinfo);
995                 INP_WUNLOCK(inp);
996         } else
997                 INP_RUNLOCK(inp);
998         m_freem(m);
999         return (error);
1000 }
1001
1002 static void
1003 udp_abort(struct socket *so)
1004 {
1005         struct inpcb *inp;
1006
1007         inp = sotoinpcb(so);
1008         KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
1009         INP_INFO_WLOCK(&udbinfo);
1010         INP_WLOCK(inp);
1011         if (inp->inp_faddr.s_addr != INADDR_ANY) {
1012                 in_pcbdisconnect(inp);
1013                 inp->inp_laddr.s_addr = INADDR_ANY;
1014                 soisdisconnected(so);
1015         }
1016         INP_WUNLOCK(inp);
1017         INP_INFO_WUNLOCK(&udbinfo);
1018 }
1019
1020 static int
1021 udp_attach(struct socket *so, int proto, struct thread *td)
1022 {
1023         struct inpcb *inp;
1024         int error;
1025
1026         inp = sotoinpcb(so);
1027         KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1028         error = soreserve(so, udp_sendspace, udp_recvspace);
1029         if (error)
1030                 return (error);
1031         INP_INFO_WLOCK(&udbinfo);
1032         error = in_pcballoc(so, &udbinfo);
1033         if (error) {
1034                 INP_INFO_WUNLOCK(&udbinfo);
1035                 return (error);
1036         }
1037
1038         inp = (struct inpcb *)so->so_pcb;
1039         INP_INFO_WUNLOCK(&udbinfo);
1040         inp->inp_vflag |= INP_IPV4;
1041         inp->inp_ip_ttl = ip_defttl;
1042         INP_WUNLOCK(inp);
1043         return (0);
1044 }
1045
1046 static int
1047 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1048 {
1049         struct inpcb *inp;
1050         int error;
1051
1052         inp = sotoinpcb(so);
1053         KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1054         INP_INFO_WLOCK(&udbinfo);
1055         INP_WLOCK(inp);
1056         error = in_pcbbind(inp, nam, td->td_ucred);
1057         INP_WUNLOCK(inp);
1058         INP_INFO_WUNLOCK(&udbinfo);
1059         return (error);
1060 }
1061
1062 static void
1063 udp_close(struct socket *so)
1064 {
1065         struct inpcb *inp;
1066
1067         inp = sotoinpcb(so);
1068         KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1069         INP_INFO_WLOCK(&udbinfo);
1070         INP_WLOCK(inp);
1071         if (inp->inp_faddr.s_addr != INADDR_ANY) {
1072                 in_pcbdisconnect(inp);
1073                 inp->inp_laddr.s_addr = INADDR_ANY;
1074                 soisdisconnected(so);
1075         }
1076         INP_WUNLOCK(inp);
1077         INP_INFO_WUNLOCK(&udbinfo);
1078 }
1079
1080 static int
1081 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1082 {
1083         struct inpcb *inp;
1084         int error;
1085         struct sockaddr_in *sin;
1086
1087         inp = sotoinpcb(so);
1088         KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1089         INP_INFO_WLOCK(&udbinfo);
1090         INP_WLOCK(inp);
1091         if (inp->inp_faddr.s_addr != INADDR_ANY) {
1092                 INP_WUNLOCK(inp);
1093                 INP_INFO_WUNLOCK(&udbinfo);
1094                 return (EISCONN);
1095         }
1096         sin = (struct sockaddr_in *)nam;
1097         if (jailed(td->td_ucred))
1098                 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1099         error = in_pcbconnect(inp, nam, td->td_ucred);
1100         if (error == 0)
1101                 soisconnected(so);
1102         INP_WUNLOCK(inp);
1103         INP_INFO_WUNLOCK(&udbinfo);
1104         return (error);
1105 }
1106
1107 static void
1108 udp_detach(struct socket *so)
1109 {
1110         struct inpcb *inp;
1111
1112         inp = sotoinpcb(so);
1113         KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1114         KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1115             ("udp_detach: not disconnected"));
1116         INP_INFO_WLOCK(&udbinfo);
1117         INP_WLOCK(inp);
1118         in_pcbdetach(inp);
1119         in_pcbfree(inp);
1120         INP_INFO_WUNLOCK(&udbinfo);
1121 }
1122
1123 static int
1124 udp_disconnect(struct socket *so)
1125 {
1126         struct inpcb *inp;
1127
1128         inp = sotoinpcb(so);
1129         KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1130         INP_INFO_WLOCK(&udbinfo);
1131         INP_WLOCK(inp);
1132         if (inp->inp_faddr.s_addr == INADDR_ANY) {
1133                 INP_WUNLOCK(inp);
1134                 INP_INFO_WUNLOCK(&udbinfo);
1135                 return (ENOTCONN);
1136         }
1137
1138         in_pcbdisconnect(inp);
1139         inp->inp_laddr.s_addr = INADDR_ANY;
1140         SOCK_LOCK(so);
1141         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
1142         SOCK_UNLOCK(so);
1143         INP_WUNLOCK(inp);
1144         INP_INFO_WUNLOCK(&udbinfo);
1145         return (0);
1146 }
1147
1148 static int
1149 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1150     struct mbuf *control, struct thread *td)
1151 {
1152         struct inpcb *inp;
1153
1154         inp = sotoinpcb(so);
1155         KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1156         return (udp_output(inp, m, addr, control, td));
1157 }
1158
1159 int
1160 udp_shutdown(struct socket *so)
1161 {
1162         struct inpcb *inp;
1163
1164         inp = sotoinpcb(so);
1165         KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1166         INP_WLOCK(inp);
1167         socantsendmore(so);
1168         INP_WUNLOCK(inp);
1169         return (0);
1170 }
1171
1172 struct pr_usrreqs udp_usrreqs = {
1173         .pru_abort =            udp_abort,
1174         .pru_attach =           udp_attach,
1175         .pru_bind =             udp_bind,
1176         .pru_connect =          udp_connect,
1177         .pru_control =          in_control,
1178         .pru_detach =           udp_detach,
1179         .pru_disconnect =       udp_disconnect,
1180         .pru_peeraddr =         in_getpeeraddr,
1181         .pru_send =             udp_send,
1182         .pru_soreceive =        soreceive_dgram,
1183         .pru_sosend =           sosend_dgram,
1184         .pru_shutdown =         udp_shutdown,
1185         .pru_sockaddr =         in_getsockaddr,
1186         .pru_sosetlabel =       in_pcbsosetlabel,
1187         .pru_close =            udp_close,
1188 };