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