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