xref: /freebsd/sys/netinet/udp_usrreq.c (revision 2ad872c5794e4c26fdf6ed219ad3f09ca0d5304a)
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.48 1998/08/24 07:47:39 dfr 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, "");
79 
80 static struct	inpcbhead udb;		/* from udp_var.h */
81 static 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, "");
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)
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 	int s = 0, error = 0;
466 
467 	if (control)
468 		m_freem(control);		/* XXX */
469 
470 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
471 		error = EMSGSIZE;
472 		goto release;
473 	}
474 
475 	if (addr) {
476 		laddr = inp->inp_laddr;
477 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
478 			error = EISCONN;
479 			goto release;
480 		}
481 		/*
482 		 * Must block input while temporarily connected.
483 		 */
484 		s = splnet();
485 		error = in_pcbconnect(inp, addr, p);
486 		if (error) {
487 			splx(s);
488 			goto release;
489 		}
490 	} else {
491 		if (inp->inp_faddr.s_addr == INADDR_ANY) {
492 			error = ENOTCONN;
493 			goto release;
494 		}
495 	}
496 	/*
497 	 * Calculate data length and get a mbuf
498 	 * for UDP and IP headers.
499 	 */
500 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
501 	if (m == 0) {
502 		error = ENOBUFS;
503 		if (addr)
504 			splx(s);
505 		goto release;
506 	}
507 
508 	/*
509 	 * Fill in mbuf with extended UDP header
510 	 * and addresses and length put into network format.
511 	 */
512 	ui = mtod(m, struct udpiphdr *);
513 	bzero(ui->ui_x1, sizeof(ui->ui_x1));
514 	ui->ui_pr = IPPROTO_UDP;
515 	ui->ui_len = htons((u_short)len + sizeof (struct udphdr));
516 	ui->ui_src = inp->inp_laddr;
517 	ui->ui_dst = inp->inp_faddr;
518 	ui->ui_sport = inp->inp_lport;
519 	ui->ui_dport = inp->inp_fport;
520 	ui->ui_ulen = ui->ui_len;
521 
522 	/*
523 	 * Stuff checksum and output datagram.
524 	 */
525 	ui->ui_sum = 0;
526 	if (udpcksum) {
527 	    if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
528 		ui->ui_sum = 0xffff;
529 	}
530 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
531 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
532 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
533 	udpstat.udps_opackets++;
534 	error = ip_output(m, inp->inp_options, &inp->inp_route,
535 	    inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
536 	    inp->inp_moptions);
537 
538 	if (addr) {
539 		in_pcbdisconnect(inp);
540 		inp->inp_laddr = laddr;	/* XXX rehash? */
541 		splx(s);
542 	}
543 	return (error);
544 
545 release:
546 	m_freem(m);
547 	return (error);
548 }
549 
550 static u_long	udp_sendspace = 9216;		/* really max datagram size */
551 					/* 40 1K datagrams */
552 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
553 	&udp_sendspace, 0, "");
554 
555 static u_long	udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
556 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
557 	&udp_recvspace, 0, "");
558 
559 static int
560 udp_abort(struct socket *so)
561 {
562 	struct inpcb *inp;
563 	int s;
564 
565 	inp = sotoinpcb(so);
566 	if (inp == 0)
567 		return EINVAL;	/* ??? possible? panic instead? */
568 	soisdisconnected(so);
569 	s = splnet();
570 	in_pcbdetach(inp);
571 	splx(s);
572 	return 0;
573 }
574 
575 static int
576 udp_attach(struct socket *so, int proto, struct proc *p)
577 {
578 	struct inpcb *inp;
579 	int s, error;
580 
581 	inp = sotoinpcb(so);
582 	if (inp != 0)
583 		return EINVAL;
584 
585 	s = splnet();
586 	error = in_pcballoc(so, &udbinfo, p);
587 	splx(s);
588 	if (error)
589 		return error;
590 	error = soreserve(so, udp_sendspace, udp_recvspace);
591 	if (error)
592 		return error;
593 	((struct inpcb *) so->so_pcb)->inp_ip_ttl = ip_defttl;
594 	return 0;
595 }
596 
597 static int
598 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
599 {
600 	struct inpcb *inp;
601 	int s, error;
602 
603 	inp = sotoinpcb(so);
604 	if (inp == 0)
605 		return EINVAL;
606 	s = splnet();
607 	error = in_pcbbind(inp, nam, p);
608 	splx(s);
609 	return error;
610 }
611 
612 static int
613 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
614 {
615 	struct inpcb *inp;
616 	int s, error;
617 
618 	inp = sotoinpcb(so);
619 	if (inp == 0)
620 		return EINVAL;
621 	if (inp->inp_faddr.s_addr != INADDR_ANY)
622 		return EISCONN;
623 	s = splnet();
624 	error = in_pcbconnect(inp, nam, p);
625 	splx(s);
626 	if (error == 0)
627 		soisconnected(so);
628 	return error;
629 }
630 
631 static int
632 udp_detach(struct socket *so)
633 {
634 	struct inpcb *inp;
635 	int s;
636 
637 	inp = sotoinpcb(so);
638 	if (inp == 0)
639 		return EINVAL;
640 	s = splnet();
641 	in_pcbdetach(inp);
642 	splx(s);
643 	return 0;
644 }
645 
646 static int
647 udp_disconnect(struct socket *so)
648 {
649 	struct inpcb *inp;
650 	int s;
651 
652 	inp = sotoinpcb(so);
653 	if (inp == 0)
654 		return EINVAL;
655 	if (inp->inp_faddr.s_addr == INADDR_ANY)
656 		return ENOTCONN;
657 
658 	s = splnet();
659 	in_pcbdisconnect(inp);
660 	inp->inp_laddr.s_addr = INADDR_ANY;
661 	splx(s);
662 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
663 	return 0;
664 }
665 
666 static int
667 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
668 	    struct mbuf *control, struct proc *p)
669 {
670 	struct inpcb *inp;
671 
672 	inp = sotoinpcb(so);
673 	if (inp == 0) {
674 		m_freem(m);
675 		return EINVAL;
676 	}
677 	return udp_output(inp, m, addr, control, p);
678 }
679 
680 static int
681 udp_shutdown(struct socket *so)
682 {
683 	struct inpcb *inp;
684 
685 	inp = sotoinpcb(so);
686 	if (inp == 0)
687 		return EINVAL;
688 	socantsendmore(so);
689 	return 0;
690 }
691 
692 struct pr_usrreqs udp_usrreqs = {
693 	udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
694 	pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
695 	pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
696 	pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
697 	in_setsockaddr, sosend, soreceive, sopoll
698 };
699 
700