xref: /freebsd/sys/netinet/udp_usrreq.c (revision 10cc62b7a6c4b1a31c4d85be67abb9e2991a8d97)
1c398230bSWarner Losh /*-
26dfab5b1SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
33329b236SRobert Watson  *	The Regents of the University of California.
43329b236SRobert Watson  * All rights reserved.
5df8bae1dSRodney W. Grimes  *
6df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
7df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
8df8bae1dSRodney W. Grimes  * are met:
9df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
10df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
11df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
13df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
14df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
15df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
16df8bae1dSRodney W. Grimes  *    without specific prior written permission.
17df8bae1dSRodney W. Grimes  *
18df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
29df8bae1dSRodney W. Grimes  *
306dfab5b1SGarrett Wollman  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
31df8bae1dSRodney W. Grimes  */
32df8bae1dSRodney W. Grimes 
334b421e2dSMike Silbersack #include <sys/cdefs.h>
344b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
354b421e2dSMike Silbersack 
360b4ae859SGleb Smirnoff #include "opt_ipfw.h"
37cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
38f5514f08SRobert Watson #include "opt_ipsec.h"
39bdb3fa18SRobert Watson #include "opt_mac.h"
40cfa1ca9dSYoshinobu Inoue 
41df8bae1dSRodney W. Grimes #include <sys/param.h>
42960ed29cSSeigo Tanimura #include <sys/domain.h>
434f590175SPaul Saab #include <sys/eventhandler.h>
44960ed29cSSeigo Tanimura #include <sys/jail.h>
45b110a8a2SGarrett Wollman #include <sys/kernel.h>
46960ed29cSSeigo Tanimura #include <sys/lock.h>
47df8bae1dSRodney W. Grimes #include <sys/malloc.h>
48df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
49acd3428bSRobert Watson #include <sys/priv.h>
50490d50b6SBrian Feldman #include <sys/proc.h>
51df8bae1dSRodney W. Grimes #include <sys/protosw.h>
52960ed29cSSeigo Tanimura #include <sys/signalvar.h>
53df8bae1dSRodney W. Grimes #include <sys/socket.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55960ed29cSSeigo Tanimura #include <sys/sx.h>
56b5e8ce9fSBruce Evans #include <sys/sysctl.h>
57816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
58f5514f08SRobert Watson #include <sys/systm.h>
598781d8e9SBruce Evans 
6069c2d429SJeff Roberson #include <vm/uma.h>
61df8bae1dSRodney W. Grimes 
62df8bae1dSRodney W. Grimes #include <net/if.h>
63df8bae1dSRodney W. Grimes #include <net/route.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes #include <netinet/in.h>
66960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
67f5514f08SRobert Watson #include <netinet/in_systm.h>
68960ed29cSSeigo Tanimura #include <netinet/in_var.h>
69df8bae1dSRodney W. Grimes #include <netinet/ip.h>
70cfa1ca9dSYoshinobu Inoue #ifdef INET6
71cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
72cfa1ca9dSYoshinobu Inoue #endif
73960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
74960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
75df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
76ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
77cfa1ca9dSYoshinobu Inoue #ifdef INET6
78cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
79cfa1ca9dSYoshinobu Inoue #endif
80df8bae1dSRodney W. Grimes #include <netinet/udp.h>
81df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
82df8bae1dSRodney W. Grimes 
83b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
84b9234fafSSam Leffler #include <netipsec/ipsec.h>
853329b236SRobert Watson #endif
86b9234fafSSam Leffler 
87db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
88db4f9cc7SJonathan Lemon 
89aed55708SRobert Watson #include <security/mac/mac_framework.h>
90aed55708SRobert Watson 
91df8bae1dSRodney W. Grimes /*
92df8bae1dSRodney W. Grimes  * UDP protocol implementation.
93df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
94df8bae1dSRodney W. Grimes  */
9574eb3236SWarner Losh 
9674eb3236SWarner Losh /*
973329b236SRobert Watson  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
983329b236SRobert Watson  * removes the only data integrity mechanism for packets and malformed
99f5514f08SRobert Watson  * packets that would otherwise be discarded due to bad checksums, and may
100f5514f08SRobert Watson  * cause problems (especially for NFS data blocks).
10174eb3236SWarner Losh  */
102f5514f08SRobert Watson static int	udp_cksum = 1;
103f5514f08SRobert Watson SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udp_cksum,
1043329b236SRobert Watson     0, "");
105df8bae1dSRodney W. Grimes 
106afdb4274SRobert Watson int	udp_log_in_vain = 0;
107816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
108afdb4274SRobert Watson     &udp_log_in_vain, 0, "Log all incoming UDP packets");
109816a3d83SPoul-Henning Kamp 
11043bbb6aaSRobert Watson int	udp_blackhole = 0;
11143bbb6aaSRobert Watson SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, &udp_blackhole, 0,
1123329b236SRobert Watson     "Do not send port unreachables for refused connects");
11316f7f31fSGeoff Rehmet 
11443bbb6aaSRobert Watson u_long	udp_sendspace = 9216;		/* really max datagram size */
11543bbb6aaSRobert Watson 					/* 40 1K datagrams */
11643bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
11743bbb6aaSRobert Watson     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
11843bbb6aaSRobert Watson 
11943bbb6aaSRobert Watson u_long	udp_recvspace = 40 * (1024 +
12043bbb6aaSRobert Watson #ifdef INET6
12143bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in6)
12243bbb6aaSRobert Watson #else
12343bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in)
12443bbb6aaSRobert Watson #endif
12543bbb6aaSRobert Watson 				      );
12643bbb6aaSRobert Watson 
12743bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
12843bbb6aaSRobert Watson     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
12943bbb6aaSRobert Watson 
13076429de4SYoshinobu Inoue struct inpcbhead	udb;		/* from udp_var.h */
1317a2aab80SBrian Feldman struct inpcbinfo	udbinfo;
13215bd2b43SDavid Greenman 
13315bd2b43SDavid Greenman #ifndef UDBHASHSIZE
134c3229e05SDavid Greenman #define	UDBHASHSIZE	16
13515bd2b43SDavid Greenman #endif
13615bd2b43SDavid Greenman 
13776429de4SYoshinobu Inoue struct udpstat	udpstat;	/* from udp_var.h */
1383329b236SRobert Watson SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, &udpstat,
1393329b236SRobert Watson     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
140f2ea20e6SGarrett Wollman 
141bc725eafSRobert Watson static void	udp_detach(struct socket *so);
1424d77a549SAlfred Perlstein static int	udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1434d77a549SAlfred Perlstein 		    struct mbuf *, struct thread *);
144df8bae1dSRodney W. Grimes 
1454f590175SPaul Saab static void
1464f590175SPaul Saab udp_zone_change(void *tag)
1474f590175SPaul Saab {
1484f590175SPaul Saab 
1494f590175SPaul Saab 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
1504f590175SPaul Saab }
1514f590175SPaul Saab 
152d915b280SStephan Uphoff static int
153d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags)
154d915b280SStephan Uphoff {
155af1ee11dSRobert Watson 	struct inpcb *inp;
15608651e1fSJohn Baldwin 
157af1ee11dSRobert Watson 	inp = mem;
158d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "udpinp");
159d915b280SStephan Uphoff 	return (0);
160d915b280SStephan Uphoff }
161d915b280SStephan Uphoff 
162df8bae1dSRodney W. Grimes void
163af1ee11dSRobert Watson udp_init(void)
164df8bae1dSRodney W. Grimes {
165af1ee11dSRobert Watson 
166f76fcf6dSJeffrey Hsu 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
16715bd2b43SDavid Greenman 	LIST_INIT(&udb);
168712fc218SRobert Watson 	udbinfo.ipi_listhead = &udb;
169712fc218SRobert Watson 	udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB,
170712fc218SRobert Watson 	    &udbinfo.ipi_hashmask);
171712fc218SRobert Watson 	udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB,
172712fc218SRobert Watson 	    &udbinfo.ipi_porthashmask);
17369c2d429SJeff Roberson 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
174d915b280SStephan Uphoff 	    NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
17569c2d429SJeff Roberson 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
1764f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
1774f590175SPaul Saab 	    EVENTHANDLER_PRI_ANY);
178df8bae1dSRodney W. Grimes }
179df8bae1dSRodney W. Grimes 
18043bbb6aaSRobert Watson /*
18143bbb6aaSRobert Watson  * Subroutine of udp_input(), which appends the provided mbuf chain to the
18243bbb6aaSRobert Watson  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
18343bbb6aaSRobert Watson  * contains the source address.  If the socket ends up being an IPv6 socket,
18443bbb6aaSRobert Watson  * udp_append() will convert to a sockaddr_in6 before passing the address
18543bbb6aaSRobert Watson  * into the socket code.
18643bbb6aaSRobert Watson  */
18743bbb6aaSRobert Watson static void
18843bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
18943bbb6aaSRobert Watson     struct sockaddr_in *udp_in)
19043bbb6aaSRobert Watson {
19143bbb6aaSRobert Watson 	struct sockaddr *append_sa;
19243bbb6aaSRobert Watson 	struct socket *so;
19343bbb6aaSRobert Watson 	struct mbuf *opts = 0;
19443bbb6aaSRobert Watson #ifdef INET6
19543bbb6aaSRobert Watson 	struct sockaddr_in6 udp_in6;
19643bbb6aaSRobert Watson #endif
19743bbb6aaSRobert Watson 
198119d85f6SRobert Watson 	INP_RLOCK_ASSERT(inp);
19943bbb6aaSRobert Watson 
20043bbb6aaSRobert Watson #ifdef IPSEC
20143bbb6aaSRobert Watson 	/* Check AH/ESP integrity. */
20243bbb6aaSRobert Watson 	if (ipsec4_in_reject(n, inp)) {
20343bbb6aaSRobert Watson 		m_freem(n);
20443bbb6aaSRobert Watson 		ipsec4stat.in_polvio++;
20543bbb6aaSRobert Watson 		return;
20643bbb6aaSRobert Watson 	}
20743bbb6aaSRobert Watson #endif /* IPSEC */
20843bbb6aaSRobert Watson #ifdef MAC
20930d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, n) != 0) {
21043bbb6aaSRobert Watson 		m_freem(n);
21143bbb6aaSRobert Watson 		return;
21243bbb6aaSRobert Watson 	}
21343bbb6aaSRobert Watson #endif
21443bbb6aaSRobert Watson 	if (inp->inp_flags & INP_CONTROLOPTS ||
21543bbb6aaSRobert Watson 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
21643bbb6aaSRobert Watson #ifdef INET6
2179a38ba81SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6)
2189a38ba81SBjoern A. Zeeb 			ip6_savecontrol_v4(inp, n, &opts);
2199a38ba81SBjoern A. Zeeb 		else
22043bbb6aaSRobert Watson #endif
22143bbb6aaSRobert Watson 			ip_savecontrol(inp, &opts, ip, n);
22243bbb6aaSRobert Watson 	}
22343bbb6aaSRobert Watson #ifdef INET6
22443bbb6aaSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
22543bbb6aaSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
22643bbb6aaSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
22743bbb6aaSRobert Watson 		udp_in6.sin6_family = AF_INET6;
22843bbb6aaSRobert Watson 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
22943bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
23043bbb6aaSRobert Watson 	} else
23143bbb6aaSRobert Watson #endif
23243bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)udp_in;
23343bbb6aaSRobert Watson 	m_adj(n, off);
23443bbb6aaSRobert Watson 
23543bbb6aaSRobert Watson 	so = inp->inp_socket;
23643bbb6aaSRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
23743bbb6aaSRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
23843bbb6aaSRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
23943bbb6aaSRobert Watson 		m_freem(n);
24043bbb6aaSRobert Watson 		if (opts)
24143bbb6aaSRobert Watson 			m_freem(opts);
24243bbb6aaSRobert Watson 		udpstat.udps_fullsock++;
24343bbb6aaSRobert Watson 	} else
24443bbb6aaSRobert Watson 		sorwakeup_locked(so);
24543bbb6aaSRobert Watson }
24643bbb6aaSRobert Watson 
247df8bae1dSRodney W. Grimes void
2483329b236SRobert Watson udp_input(struct mbuf *m, int off)
249df8bae1dSRodney W. Grimes {
250cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
2513329b236SRobert Watson 	struct ip *ip;
2523329b236SRobert Watson 	struct udphdr *uh;
25371498f30SBruce M Simpson 	struct ifnet *ifp;
2543329b236SRobert Watson 	struct inpcb *inp;
255df8bae1dSRodney W. Grimes 	int len;
256df8bae1dSRodney W. Grimes 	struct ip save_ip;
257d4b509bdSRobert Watson 	struct sockaddr_in udp_in;
2580b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD
2590b4ae859SGleb Smirnoff 	struct m_tag *fwd_tag;
2600b4ae859SGleb Smirnoff #endif
261df8bae1dSRodney W. Grimes 
26271498f30SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
263df8bae1dSRodney W. Grimes 	udpstat.udps_ipackets++;
264df8bae1dSRodney W. Grimes 
265df8bae1dSRodney W. Grimes 	/*
2663329b236SRobert Watson 	 * Strip IP options, if any; should skip this, make available to
2673329b236SRobert Watson 	 * user, and use on returned packets, but we don't yet have a way to
2683329b236SRobert Watson 	 * check the checksum with options still present.
269df8bae1dSRodney W. Grimes 	 */
270df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
271df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
272df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
273df8bae1dSRodney W. Grimes 	}
274df8bae1dSRodney W. Grimes 
275df8bae1dSRodney W. Grimes 	/*
276df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
277df8bae1dSRodney W. Grimes 	 */
278df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
279df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
280df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
281df8bae1dSRodney W. Grimes 			udpstat.udps_hdrops++;
282df8bae1dSRodney W. Grimes 			return;
283df8bae1dSRodney W. Grimes 		}
284df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
285df8bae1dSRodney W. Grimes 	}
286df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
287df8bae1dSRodney W. Grimes 
2883329b236SRobert Watson 	/*
2893329b236SRobert Watson 	 * Destination port of 0 is illegal, based on RFC768.
2903329b236SRobert Watson 	 */
291686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
292f76fcf6dSJeffrey Hsu 		goto badunlocked;
293686cdd19SJun-ichiro itojun Hagino 
294df8bae1dSRodney W. Grimes 	/*
2953329b236SRobert Watson 	 * Construct sockaddr format source address.  Stuff source address
2963329b236SRobert Watson 	 * and datagram in user buffer.
297b9234fafSSam Leffler 	 */
298d4b509bdSRobert Watson 	bzero(&udp_in, sizeof(udp_in));
299d4b509bdSRobert Watson 	udp_in.sin_len = sizeof(udp_in);
300d4b509bdSRobert Watson 	udp_in.sin_family = AF_INET;
301b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
302b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
303b9234fafSSam Leffler 
304b9234fafSSam Leffler 	/*
305af1ee11dSRobert Watson 	 * Make mbuf data length reflect UDP length.  If not enough data to
306af1ee11dSRobert Watson 	 * reflect UDP length, drop.
307df8bae1dSRodney W. Grimes 	 */
308df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
309df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
3107eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
311df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
312f76fcf6dSJeffrey Hsu 			goto badunlocked;
313df8bae1dSRodney W. Grimes 		}
314df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
315df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
316df8bae1dSRodney W. Grimes 	}
3173329b236SRobert Watson 
318df8bae1dSRodney W. Grimes 	/*
3193329b236SRobert Watson 	 * Save a copy of the IP header in case we want restore it for
3203329b236SRobert Watson 	 * sending an ICMP error message in response.
321df8bae1dSRodney W. Grimes 	 */
32243bbb6aaSRobert Watson 	if (!udp_blackhole)
323df8bae1dSRodney W. Grimes 		save_ip = *ip;
324cce418d3SMatt Jacob 	else
325cce418d3SMatt Jacob 		memset(&save_ip, 0, sizeof(save_ip));
326df8bae1dSRodney W. Grimes 
327df8bae1dSRodney W. Grimes 	/*
328df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
329df8bae1dSRodney W. Grimes 	 */
3306dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
33139629c92SDavid Malone 		u_short uh_sum;
33239629c92SDavid Malone 
333db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
334db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
33539629c92SDavid Malone 				uh_sum = m->m_pkthdr.csum_data;
336db4f9cc7SJonathan Lemon 			else
33739629c92SDavid Malone 				uh_sum = in_pseudo(ip->ip_src.s_addr,
338506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
339db4f9cc7SJonathan Lemon 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
34039629c92SDavid Malone 			uh_sum ^= 0xffff;
341db4f9cc7SJonathan Lemon 		} else {
342cb342100SHajimu UMEMOTO 			char b[9];
343af1ee11dSRobert Watson 
344cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
3456effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
346df8bae1dSRodney W. Grimes 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
34739629c92SDavid Malone 			uh_sum = in_cksum(m, len + sizeof (struct ip));
348cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
349db4f9cc7SJonathan Lemon 		}
35039629c92SDavid Malone 		if (uh_sum) {
351df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
352df8bae1dSRodney W. Grimes 			m_freem(m);
353df8bae1dSRodney W. Grimes 			return;
354df8bae1dSRodney W. Grimes 		}
355fb9aaba0SRuslan Ermilov 	} else
356fb9aaba0SRuslan Ermilov 		udpstat.udps_nosum++;
357df8bae1dSRodney W. Grimes 
3580b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD
3593329b236SRobert Watson 	/*
3603329b236SRobert Watson 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
3613329b236SRobert Watson 	 */
3620b4ae859SGleb Smirnoff 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
3630b4ae859SGleb Smirnoff 	if (fwd_tag != NULL) {
3640b4ae859SGleb Smirnoff 		struct sockaddr_in *next_hop;
3650b4ae859SGleb Smirnoff 
3663329b236SRobert Watson 		/*
3673329b236SRobert Watson 		 * Do the hack.
3683329b236SRobert Watson 		 */
3690b4ae859SGleb Smirnoff 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
3700b4ae859SGleb Smirnoff 		ip->ip_dst = next_hop->sin_addr;
3710b4ae859SGleb Smirnoff 		uh->uh_dport = ntohs(next_hop->sin_port);
3723329b236SRobert Watson 
3733329b236SRobert Watson 		/*
3743329b236SRobert Watson 		 * Remove the tag from the packet.  We don't need it anymore.
3753329b236SRobert Watson 		 */
3760b4ae859SGleb Smirnoff 		m_tag_delete(m, fwd_tag);
3770b4ae859SGleb Smirnoff 	}
3780b4ae859SGleb Smirnoff #endif
3790b4ae859SGleb Smirnoff 
380f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
381df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
38271498f30SBruce M Simpson 	    in_broadcast(ip->ip_dst, ifp)) {
38382c23ebaSBill Fenner 		struct inpcb *last;
38471498f30SBruce M Simpson 		struct ip_moptions *imo;
3853329b236SRobert Watson 
386df8bae1dSRodney W. Grimes 		last = NULL;
387cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
3889c1df695SRobert Watson 			if (inp->inp_lport != uh->uh_dport)
389f76fcf6dSJeffrey Hsu 				continue;
390cfa1ca9dSYoshinobu Inoue #ifdef INET6
391369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
3929c1df695SRobert Watson 				continue;
393cfa1ca9dSYoshinobu Inoue #endif
39471498f30SBruce M Simpson 			if (inp->inp_laddr.s_addr != INADDR_ANY &&
39571498f30SBruce M Simpson 			    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
3969c1df695SRobert Watson 				continue;
39771498f30SBruce M Simpson 			if (inp->inp_faddr.s_addr != INADDR_ANY &&
39871498f30SBruce M Simpson 			    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
39971498f30SBruce M Simpson 				continue;
40071498f30SBruce M Simpson 			/*
40171498f30SBruce M Simpson 			 * XXX: Do not check source port of incoming datagram
40271498f30SBruce M Simpson 			 * unless inp_connect() has been called to bind the
40371498f30SBruce M Simpson 			 * fport part of the 4-tuple; the source could be
40471498f30SBruce M Simpson 			 * trying to talk to us with an ephemeral port.
40571498f30SBruce M Simpson 			 */
40671498f30SBruce M Simpson 			if (inp->inp_fport != 0 &&
407df8bae1dSRodney W. Grimes 			    inp->inp_fport != uh->uh_sport)
4089c1df695SRobert Watson 				continue;
40971498f30SBruce M Simpson 
410119d85f6SRobert Watson 			INP_RLOCK(inp);
411df8bae1dSRodney W. Grimes 
41283453a06SBruce M Simpson 			/*
41371498f30SBruce M Simpson 			 * Handle socket delivery policy for any-source
41471498f30SBruce M Simpson 			 * and source-specific multicast. [RFC3678]
41583453a06SBruce M Simpson 			 */
41671498f30SBruce M Simpson 			imo = inp->inp_moptions;
41771498f30SBruce M Simpson 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
41871498f30SBruce M Simpson 			    imo != NULL) {
41971498f30SBruce M Simpson 				struct sockaddr_in	 sin;
42071498f30SBruce M Simpson 				struct in_msource	*ims;
42171498f30SBruce M Simpson 				int			 blocked, mode;
42271498f30SBruce M Simpson 				size_t			 idx;
42383453a06SBruce M Simpson 
42471498f30SBruce M Simpson 				bzero(&sin, sizeof(struct sockaddr_in));
42571498f30SBruce M Simpson 				sin.sin_len = sizeof(struct sockaddr_in);
42671498f30SBruce M Simpson 				sin.sin_family = AF_INET;
42771498f30SBruce M Simpson 				sin.sin_addr = ip->ip_dst;
42871498f30SBruce M Simpson 
42971498f30SBruce M Simpson 				blocked = 0;
43071498f30SBruce M Simpson 				idx = imo_match_group(imo, ifp,
43171498f30SBruce M Simpson 				    (struct sockaddr *)&sin);
43271498f30SBruce M Simpson 				if (idx == -1) {
43371498f30SBruce M Simpson 					/*
43471498f30SBruce M Simpson 					 * No group membership for this socket.
43571498f30SBruce M Simpson 					 * Do not bump udps_noportbcast, as
43671498f30SBruce M Simpson 					 * this will happen further down.
43771498f30SBruce M Simpson 					 */
43871498f30SBruce M Simpson 					blocked++;
43971498f30SBruce M Simpson 				} else {
44071498f30SBruce M Simpson 					/*
44171498f30SBruce M Simpson 					 * Check for a multicast source filter
44271498f30SBruce M Simpson 					 * entry on this socket for this group.
44371498f30SBruce M Simpson 					 * MCAST_EXCLUDE is the default
44471498f30SBruce M Simpson 					 * behaviour.  It means default accept;
44571498f30SBruce M Simpson 					 * entries, if present, denote sources
44671498f30SBruce M Simpson 					 * to be excluded from delivery.
44771498f30SBruce M Simpson 					 */
44871498f30SBruce M Simpson 					ims = imo_match_source(imo, idx,
44971498f30SBruce M Simpson 					    (struct sockaddr *)&udp_in);
45071498f30SBruce M Simpson 					mode = imo->imo_mfilters[idx].imf_fmode;
45171498f30SBruce M Simpson 					if ((ims != NULL &&
45271498f30SBruce M Simpson 					     mode == MCAST_EXCLUDE) ||
45371498f30SBruce M Simpson 					    (ims == NULL &&
45471498f30SBruce M Simpson 					     mode == MCAST_INCLUDE)) {
45571498f30SBruce M Simpson #ifdef DIAGNOSTIC
45671498f30SBruce M Simpson 						if (bootverbose) {
45771498f30SBruce M Simpson 							printf("%s: blocked by"
45871498f30SBruce M Simpson 							    " source filter\n",
45971498f30SBruce M Simpson 							    __func__);
46071498f30SBruce M Simpson 						}
46171498f30SBruce M Simpson #endif
46271498f30SBruce M Simpson 						udpstat.udps_filtermcast++;
46371498f30SBruce M Simpson 						blocked++;
46483453a06SBruce M Simpson 					}
46583453a06SBruce M Simpson 				}
46671498f30SBruce M Simpson 				if (blocked != 0) {
467119d85f6SRobert Watson 					INP_RUNLOCK(inp);
4689c1df695SRobert Watson 					continue;
4699c1df695SRobert Watson 				}
47083453a06SBruce M Simpson 			}
471df8bae1dSRodney W. Grimes 			if (last != NULL) {
472df8bae1dSRodney W. Grimes 				struct mbuf *n;
473df8bae1dSRodney W. Grimes 
474032dcc76SLuigi Rizzo 				n = m_copy(m, 0, M_COPYALL);
475365433d9SRobert Watson 				if (n != NULL)
4763329b236SRobert Watson 					udp_append(last, ip, n, iphlen +
4773329b236SRobert Watson 					    sizeof(struct udphdr), &udp_in);
478119d85f6SRobert Watson 				INP_RUNLOCK(last);
479df8bae1dSRodney W. Grimes 			}
48082c23ebaSBill Fenner 			last = inp;
481df8bae1dSRodney W. Grimes 			/*
482df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
483df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
4843329b236SRobert Watson 			 * socket options set.  This heuristic avoids
4853329b236SRobert Watson 			 * searching through all pcbs in the common case of a
4863329b236SRobert Watson 			 * non-shared port.  It assumes that an application
4873329b236SRobert Watson 			 * will never clear these options after setting them.
488df8bae1dSRodney W. Grimes 			 */
4893329b236SRobert Watson 			if ((last->inp_socket->so_options &
4903329b236SRobert Watson 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
491df8bae1dSRodney W. Grimes 				break;
492df8bae1dSRodney W. Grimes 		}
493df8bae1dSRodney W. Grimes 
494df8bae1dSRodney W. Grimes 		if (last == NULL) {
495df8bae1dSRodney W. Grimes 			/*
4963329b236SRobert Watson 			 * No matching pcb found; discard datagram.  (No need
4973329b236SRobert Watson 			 * to send an ICMP Port Unreachable for a broadcast
4983329b236SRobert Watson 			 * or multicast datgram.)
499df8bae1dSRodney W. Grimes 			 */
500df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
50161ffc0b1SJeffrey Hsu 			goto badheadlocked;
502df8bae1dSRodney W. Grimes 		}
503d4b509bdSRobert Watson 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
504d4b509bdSRobert Watson 		    &udp_in);
505119d85f6SRobert Watson 		INP_RUNLOCK(last);
5066b8e5a98SRobert Watson 		INP_INFO_RUNLOCK(&udbinfo);
507df8bae1dSRodney W. Grimes 		return;
508df8bae1dSRodney W. Grimes 	}
5093329b236SRobert Watson 
510df8bae1dSRodney W. Grimes 	/*
5116d6a026bSDavid Greenman 	 * Locate pcb for datagram.
512df8bae1dSRodney W. Grimes 	 */
513c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
51471498f30SBruce M Simpson 	    ip->ip_dst, uh->uh_dport, 1, ifp);
51515bd2b43SDavid Greenman 	if (inp == NULL) {
516afdb4274SRobert Watson 		if (udp_log_in_vain) {
517df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
51875cfc95fSAndrey A. Chernov 
51975cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
520592071e8SBruce Evans 			log(LOG_INFO,
521592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
522592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
523592071e8SBruce Evans 			    ntohs(uh->uh_sport));
52475cfc95fSAndrey A. Chernov 		}
525df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
526df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
527df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
52861ffc0b1SJeffrey Hsu 			goto badheadlocked;
529df8bae1dSRodney W. Grimes 		}
53043bbb6aaSRobert Watson 		if (udp_blackhole)
53161ffc0b1SJeffrey Hsu 			goto badheadlocked;
5321cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
5331cbd978eSLuigi Rizzo 			goto badheadlocked;
53404287599SRuslan Ermilov 		*ip = save_ip;
53504287599SRuslan Ermilov 		ip->ip_len += iphlen;
536582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
537f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
538df8bae1dSRodney W. Grimes 		return;
539df8bae1dSRodney W. Grimes 	}
5403329b236SRobert Watson 
5413329b236SRobert Watson 	/*
5423329b236SRobert Watson 	 * Check the minimum TTL for socket.
5433329b236SRobert Watson 	 */
544119d85f6SRobert Watson 	INP_RLOCK(inp);
54510cc62b7SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
54610cc62b7SRobert Watson 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
54710cc62b7SRobert Watson 		INP_RUNLOCK(inp);
54810cc62b7SRobert Watson 		goto badunlocked;
54910cc62b7SRobert Watson 	}
550d4b509bdSRobert Watson 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
551119d85f6SRobert Watson 	INP_RUNLOCK(inp);
552df8bae1dSRodney W. Grimes 	return;
55361ffc0b1SJeffrey Hsu 
55461ffc0b1SJeffrey Hsu badheadlocked:
555f76fcf6dSJeffrey Hsu 	if (inp)
556119d85f6SRobert Watson 		INP_RUNLOCK(inp);
5576b8e5a98SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
558f76fcf6dSJeffrey Hsu badunlocked:
559df8bae1dSRodney W. Grimes 	m_freem(m);
560cfa1ca9dSYoshinobu Inoue }
561cfa1ca9dSYoshinobu Inoue 
562cfa1ca9dSYoshinobu Inoue /*
5633329b236SRobert Watson  * Notify a udp user of an asynchronous error; just wake up so that they can
5643329b236SRobert Watson  * collect error status.
565df8bae1dSRodney W. Grimes  */
5663ce144eaSJeffrey Hsu struct inpcb *
5673329b236SRobert Watson udp_notify(struct inpcb *inp, int errno)
568df8bae1dSRodney W. Grimes {
5693329b236SRobert Watson 
5708501a69cSRobert Watson 	INP_WLOCK_ASSERT(inp);
5718501a69cSRobert Watson 
572df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
573df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
574df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
5753329b236SRobert Watson 	return (inp);
576df8bae1dSRodney W. Grimes }
577df8bae1dSRodney W. Grimes 
578df8bae1dSRodney W. Grimes void
5793329b236SRobert Watson udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
580df8bae1dSRodney W. Grimes {
581c693a045SJonathan Lemon 	struct ip *ip = vip;
582c693a045SJonathan Lemon 	struct udphdr *uh;
583c693a045SJonathan Lemon 	struct in_addr faddr;
584c693a045SJonathan Lemon 	struct inpcb *inp;
585c693a045SJonathan Lemon 
586c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
587c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
588c693a045SJonathan Lemon 		return;
589df8bae1dSRodney W. Grimes 
59097d8d152SAndre Oppermann 	/*
59197d8d152SAndre Oppermann 	 * Redirects don't need to be handled up here.
59297d8d152SAndre Oppermann 	 */
59397d8d152SAndre Oppermann 	if (PRC_IS_REDIRECT(cmd))
59497d8d152SAndre Oppermann 		return;
5953329b236SRobert Watson 
59697d8d152SAndre Oppermann 	/*
59797d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
5983329b236SRobert Watson 	 *
5993329b236SRobert Watson 	 * XXX: We never get this from ICMP, otherwise it makes an excellent
6003329b236SRobert Watson 	 * DoS attack on machines with many connections.
60197d8d152SAndre Oppermann 	 */
60297d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
603af1ee11dSRobert Watson 		ip = NULL;
604d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
605df8bae1dSRodney W. Grimes 		return;
606af1ee11dSRobert Watson 	if (ip != NULL) {
607df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
608f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
609c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
610c693a045SJonathan Lemon 		    ip->ip_src, uh->uh_sport, 0, NULL);
611f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
6128501a69cSRobert Watson 			INP_WLOCK(inp);
613f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
614f5514f08SRobert Watson 				udp_notify(inp, inetctlerrmap[cmd]);
615f76fcf6dSJeffrey Hsu 			}
6168501a69cSRobert Watson 			INP_WUNLOCK(inp);
617f76fcf6dSJeffrey Hsu 		}
618f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
619df8bae1dSRodney W. Grimes 	} else
620f5514f08SRobert Watson 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd],
621f5514f08SRobert Watson 		    udp_notify);
622df8bae1dSRodney W. Grimes }
623df8bae1dSRodney W. Grimes 
6240312fbe9SPoul-Henning Kamp static int
62582d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
62698271db4SGarrett Wollman {
627277afaffSRobert Watson 	int error, i, n;
62898271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
62998271db4SGarrett Wollman 	inp_gen_t gencnt;
63098271db4SGarrett Wollman 	struct xinpgen xig;
63198271db4SGarrett Wollman 
63298271db4SGarrett Wollman 	/*
633f5514f08SRobert Watson 	 * The process of preparing the PCB list is too time-consuming and
63498271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
63598271db4SGarrett Wollman 	 */
63698271db4SGarrett Wollman 	if (req->oldptr == 0) {
63798271db4SGarrett Wollman 		n = udbinfo.ipi_count;
63898271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
63998271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
6403329b236SRobert Watson 		return (0);
64198271db4SGarrett Wollman 	}
64298271db4SGarrett Wollman 
64398271db4SGarrett Wollman 	if (req->newptr != 0)
6443329b236SRobert Watson 		return (EPERM);
64598271db4SGarrett Wollman 
64698271db4SGarrett Wollman 	/*
64798271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
64898271db4SGarrett Wollman 	 */
6494b40c56cSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
65098271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
65198271db4SGarrett Wollman 	n = udbinfo.ipi_count;
6524b40c56cSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
65398271db4SGarrett Wollman 
65447934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
6555c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
65647934cefSDon Lewis 	if (error != 0)
65747934cefSDon Lewis 		return (error);
6585c38b6dbSDon Lewis 
65998271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
66098271db4SGarrett Wollman 	xig.xig_count = n;
66198271db4SGarrett Wollman 	xig.xig_gen = gencnt;
66298271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
66398271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
66498271db4SGarrett Wollman 	if (error)
6653329b236SRobert Watson 		return (error);
66698271db4SGarrett Wollman 
667a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
66898271db4SGarrett Wollman 	if (inp_list == 0)
6693329b236SRobert Watson 		return (ENOMEM);
67098271db4SGarrett Wollman 
671f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
672712fc218SRobert Watson 	for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n;
673fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
6749622e84fSRobert Watson 		INP_RLOCK(inp);
6752ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
6762ded288cSJeffrey Hsu 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
67798271db4SGarrett Wollman 			inp_list[i++] = inp;
6789622e84fSRobert Watson 		INP_RUNLOCK(inp);
6794787fd37SPaul Saab 	}
680f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
68198271db4SGarrett Wollman 	n = i;
68298271db4SGarrett Wollman 
68398271db4SGarrett Wollman 	error = 0;
68498271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
68598271db4SGarrett Wollman 		inp = inp_list[i];
6869622e84fSRobert Watson 		INP_RLOCK(inp);
68798271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
68898271db4SGarrett Wollman 			struct xinpcb xi;
689fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
69098271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
69198271db4SGarrett Wollman 			/* XXX should avoid extra copy */
69298271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
69398271db4SGarrett Wollman 			if (inp->inp_socket)
69498271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
6954b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
6969622e84fSRobert Watson 			INP_RUNLOCK(inp);
69798271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
698d915b280SStephan Uphoff 		} else
6999622e84fSRobert Watson 			INP_RUNLOCK(inp);
70098271db4SGarrett Wollman 	}
70198271db4SGarrett Wollman 	if (!error) {
70298271db4SGarrett Wollman 		/*
7033329b236SRobert Watson 		 * Give the user an updated idea of our state.  If the
7043329b236SRobert Watson 		 * generation differs from what we told her before, she knows
7053329b236SRobert Watson 		 * that something happened while we were processing this
7063329b236SRobert Watson 		 * request, and it might be necessary to retry.
70798271db4SGarrett Wollman 		 */
708f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
70998271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
71098271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
71198271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
712f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
71398271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
71498271db4SGarrett Wollman 	}
71598271db4SGarrett Wollman 	free(inp_list, M_TEMP);
7163329b236SRobert Watson 	return (error);
71798271db4SGarrett Wollman }
71898271db4SGarrett Wollman 
71998271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
72098271db4SGarrett Wollman     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
72198271db4SGarrett Wollman 
72298271db4SGarrett Wollman static int
72382d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
724490d50b6SBrian Feldman {
725c0511d3bSBrian Feldman 	struct xucred xuc;
726490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
727490d50b6SBrian Feldman 	struct inpcb *inp;
728277afaffSRobert Watson 	int error;
729490d50b6SBrian Feldman 
73032f9753cSRobert Watson 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
731490d50b6SBrian Feldman 	if (error)
732490d50b6SBrian Feldman 		return (error);
733490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
734490d50b6SBrian Feldman 	if (error)
735490d50b6SBrian Feldman 		return (error);
736f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
737490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
738cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
7399622e84fSRobert Watson 	if (inp != NULL) {
7409622e84fSRobert Watson 		INP_RLOCK(inp);
741f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
7429622e84fSRobert Watson 		if (inp->inp_socket == NULL)
7439622e84fSRobert Watson 			error = ENOENT;
7449622e84fSRobert Watson 		if (error == 0)
7459622e84fSRobert Watson 			error = cr_canseesocket(req->td->td_ucred,
7469622e84fSRobert Watson 			    inp->inp_socket);
7479622e84fSRobert Watson 		if (error == 0)
7489622e84fSRobert Watson 			cru2x(inp->inp_socket->so_cred, &xuc);
7499622e84fSRobert Watson 		INP_RUNLOCK(inp);
7509622e84fSRobert Watson 	} else {
7519622e84fSRobert Watson 		INP_INFO_RUNLOCK(&udbinfo);
7529622e84fSRobert Watson 		error = ENOENT;
7539622e84fSRobert Watson 	}
7540e1eebb8SDon Lewis 	if (error == 0)
7550e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
756490d50b6SBrian Feldman 	return (error);
757490d50b6SBrian Feldman }
758490d50b6SBrian Feldman 
7597ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
7607ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
7617ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
762490d50b6SBrian Feldman 
763490d50b6SBrian Feldman static int
7643329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
7653329b236SRobert Watson     struct mbuf *control, struct thread *td)
766df8bae1dSRodney W. Grimes {
7673329b236SRobert Watson 	struct udpiphdr *ui;
7683329b236SRobert Watson 	int len = m->m_pkthdr.len;
76990162a4eSIan Dowse 	struct in_addr faddr, laddr;
770c557ae16SIan Dowse 	struct cmsghdr *cm;
771c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
77290162a4eSIan Dowse 	int error = 0;
7738afa2304SBruce M Simpson 	int ipflags;
77490162a4eSIan Dowse 	u_short fport, lport;
7755c32ea65SRobert Watson 	int unlock_udbinfo;
776df8bae1dSRodney W. Grimes 
7775c32ea65SRobert Watson 	/*
7785c32ea65SRobert Watson 	 * udp_output() may need to temporarily bind or connect the current
779f5514f08SRobert Watson 	 * inpcb.  As such, we don't know up front whether we will need the
780f5514f08SRobert Watson 	 * pcbinfo lock or not.  Do any work to decide what is needed up
781f5514f08SRobert Watson 	 * front before acquiring any locks.
7825c32ea65SRobert Watson 	 */
783430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
784c557ae16SIan Dowse 		if (control)
785c557ae16SIan Dowse 			m_freem(control);
7865c32ea65SRobert Watson 		m_freem(m);
7873329b236SRobert Watson 		return (EMSGSIZE);
788430d30d8SBill Fenner 	}
789430d30d8SBill Fenner 
7901b7f0384SBruce M Simpson 	src.sin_family = 0;
791c557ae16SIan Dowse 	if (control != NULL) {
792c557ae16SIan Dowse 		/*
7933329b236SRobert Watson 		 * XXX: Currently, we assume all the optional information is
7943329b236SRobert Watson 		 * stored in a single mbuf.
795c557ae16SIan Dowse 		 */
796c557ae16SIan Dowse 		if (control->m_next) {
797c557ae16SIan Dowse 			m_freem(control);
7985c32ea65SRobert Watson 			m_freem(m);
7993329b236SRobert Watson 			return (EINVAL);
800c557ae16SIan Dowse 		}
801c557ae16SIan Dowse 		for (; control->m_len > 0;
802c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
803c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
804c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
805af1ee11dSRobert Watson 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
806af1ee11dSRobert Watson 			    || cm->cmsg_len > control->m_len) {
807c557ae16SIan Dowse 				error = EINVAL;
808c557ae16SIan Dowse 				break;
809c557ae16SIan Dowse 			}
810c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
811c557ae16SIan Dowse 				continue;
812c557ae16SIan Dowse 
813c557ae16SIan Dowse 			switch (cm->cmsg_type) {
814c557ae16SIan Dowse 			case IP_SENDSRCADDR:
815c557ae16SIan Dowse 				if (cm->cmsg_len !=
816c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
817c557ae16SIan Dowse 					error = EINVAL;
818c557ae16SIan Dowse 					break;
819c557ae16SIan Dowse 				}
820c557ae16SIan Dowse 				bzero(&src, sizeof(src));
821c557ae16SIan Dowse 				src.sin_family = AF_INET;
822c557ae16SIan Dowse 				src.sin_len = sizeof(src);
823c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
824af1ee11dSRobert Watson 				src.sin_addr =
825af1ee11dSRobert Watson 				    *(struct in_addr *)CMSG_DATA(cm);
826c557ae16SIan Dowse 				break;
827af1ee11dSRobert Watson 
828c557ae16SIan Dowse 			default:
829c557ae16SIan Dowse 				error = ENOPROTOOPT;
830c557ae16SIan Dowse 				break;
831c557ae16SIan Dowse 			}
832c557ae16SIan Dowse 			if (error)
833c557ae16SIan Dowse 				break;
834c557ae16SIan Dowse 		}
835c557ae16SIan Dowse 		m_freem(control);
836c557ae16SIan Dowse 	}
8375c32ea65SRobert Watson 	if (error) {
8385c32ea65SRobert Watson 		m_freem(m);
8393329b236SRobert Watson 		return (error);
8405c32ea65SRobert Watson 	}
8415c32ea65SRobert Watson 
8421b7f0384SBruce M Simpson 	if (src.sin_family == AF_INET || addr != NULL) {
8435c32ea65SRobert Watson 		INP_INFO_WLOCK(&udbinfo);
8445c32ea65SRobert Watson 		unlock_udbinfo = 1;
8455c32ea65SRobert Watson 	} else
8465c32ea65SRobert Watson 		unlock_udbinfo = 0;
8478501a69cSRobert Watson 	INP_WLOCK(inp);
8485c32ea65SRobert Watson 
8495c32ea65SRobert Watson #ifdef MAC
85030d239bcSRobert Watson 	mac_inpcb_create_mbuf(inp, m);
8515c32ea65SRobert Watson #endif
8525c32ea65SRobert Watson 
8531b7f0384SBruce M Simpson 	/*
8541b7f0384SBruce M Simpson 	 * If the IP_SENDSRCADDR control message was specified, override the
8551b7f0384SBruce M Simpson 	 * source address for this datagram.  Its use is invalidated if the
8561b7f0384SBruce M Simpson 	 * address thus specified is incomplete or clobbers other inpcbs.
8571b7f0384SBruce M Simpson 	 */
85890162a4eSIan Dowse 	laddr = inp->inp_laddr;
85990162a4eSIan Dowse 	lport = inp->inp_lport;
8601b7f0384SBruce M Simpson 	if (src.sin_family == AF_INET) {
8611b7f0384SBruce M Simpson 		if ((lport == 0) ||
8621b7f0384SBruce M Simpson 		    (laddr.s_addr == INADDR_ANY &&
8631b7f0384SBruce M Simpson 		     src.sin_addr.s_addr == INADDR_ANY)) {
864c557ae16SIan Dowse 			error = EINVAL;
865c557ae16SIan Dowse 			goto release;
866c557ae16SIan Dowse 		}
867c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
868b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
869c557ae16SIan Dowse 		if (error)
870c557ae16SIan Dowse 			goto release;
871c557ae16SIan Dowse 	}
872c557ae16SIan Dowse 
873df8bae1dSRodney W. Grimes 	if (addr) {
87475c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
875812d8653SSam Leffler 		if (jailed(td->td_ucred))
8763329b236SRobert Watson 			prison_remote_ip(td->td_ucred, 0,
8773329b236SRobert Watson 			    &sin->sin_addr.s_addr);
878df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
879df8bae1dSRodney W. Grimes 			error = EISCONN;
880df8bae1dSRodney W. Grimes 			goto release;
881df8bae1dSRodney W. Grimes 		}
88290162a4eSIan Dowse 		error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
883b0330ed9SPawel Jakub Dawidek 		    &faddr.s_addr, &fport, NULL, td->td_ucred);
88490162a4eSIan Dowse 		if (error)
88590162a4eSIan Dowse 			goto release;
88690162a4eSIan Dowse 
88790162a4eSIan Dowse 		/* Commit the local port if newly assigned. */
88890162a4eSIan Dowse 		if (inp->inp_laddr.s_addr == INADDR_ANY &&
88990162a4eSIan Dowse 		    inp->inp_lport == 0) {
8903a1757b9SGleb Smirnoff 			/*
8913a1757b9SGleb Smirnoff 			 * Remember addr if jailed, to prevent rebinding.
8923a1757b9SGleb Smirnoff 			 */
8933a1757b9SGleb Smirnoff 			if (jailed(td->td_ucred))
8943a1757b9SGleb Smirnoff 				inp->inp_laddr = laddr;
89590162a4eSIan Dowse 			inp->inp_lport = lport;
89690162a4eSIan Dowse 			if (in_pcbinshash(inp) != 0) {
89790162a4eSIan Dowse 				inp->inp_lport = 0;
89890162a4eSIan Dowse 				error = EAGAIN;
899df8bae1dSRodney W. Grimes 				goto release;
900df8bae1dSRodney W. Grimes 			}
90190162a4eSIan Dowse 			inp->inp_flags |= INP_ANONPORT;
90290162a4eSIan Dowse 		}
903df8bae1dSRodney W. Grimes 	} else {
90490162a4eSIan Dowse 		faddr = inp->inp_faddr;
90590162a4eSIan Dowse 		fport = inp->inp_fport;
90690162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
907df8bae1dSRodney W. Grimes 			error = ENOTCONN;
908df8bae1dSRodney W. Grimes 			goto release;
909df8bae1dSRodney W. Grimes 		}
910df8bae1dSRodney W. Grimes 	}
911e6ccd709SRobert Watson 
912df8bae1dSRodney W. Grimes 	/*
913e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
914392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
915392e8407SRobert Watson 	 * since we won't use that space at this layer.
916df8bae1dSRodney W. Grimes 	 */
917e6ccd709SRobert Watson 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
918e6ccd709SRobert Watson 	if (m == NULL) {
919df8bae1dSRodney W. Grimes 		error = ENOBUFS;
92049b19bfcSBruce M Simpson 		goto release;
921df8bae1dSRodney W. Grimes 	}
922e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
923e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
924392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
925df8bae1dSRodney W. Grimes 
926df8bae1dSRodney W. Grimes 	/*
9273329b236SRobert Watson 	 * Fill in mbuf with extended UDP header and addresses and length put
9283329b236SRobert Watson 	 * into network format.
929df8bae1dSRodney W. Grimes 	 */
930df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
931db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
932df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
93390162a4eSIan Dowse 	ui->ui_src = laddr;
93490162a4eSIan Dowse 	ui->ui_dst = faddr;
93590162a4eSIan Dowse 	ui->ui_sport = lport;
93690162a4eSIan Dowse 	ui->ui_dport = fport;
937db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
938df8bae1dSRodney W. Grimes 
939b2828ad2SAndre Oppermann 	/*
940b2828ad2SAndre Oppermann 	 * Set the Don't Fragment bit in the IP header.
941b2828ad2SAndre Oppermann 	 */
942b2828ad2SAndre Oppermann 	if (inp->inp_flags & INP_DONTFRAG) {
943b2828ad2SAndre Oppermann 		struct ip *ip;
9443329b236SRobert Watson 
945b2828ad2SAndre Oppermann 		ip = (struct ip *)&ui->ui_i;
946b2828ad2SAndre Oppermann 		ip->ip_off |= IP_DF;
947b2828ad2SAndre Oppermann 	}
948b2828ad2SAndre Oppermann 
949b5d47ff5SJohn-Mark Gurney 	ipflags = 0;
950b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
951b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
952b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
953b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
9546fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
9558afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
9568afa2304SBruce M Simpson 
957df8bae1dSRodney W. Grimes 	/*
958db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
959df8bae1dSRodney W. Grimes 	 */
960f5514f08SRobert Watson 	if (udp_cksum) {
9616fbfd582SAndre Oppermann 		if (inp->inp_flags & INP_ONESBCAST)
9628a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
9638a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
964db4f9cc7SJonathan Lemon 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
965db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
966db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
9673329b236SRobert Watson 	} else
968db4f9cc7SJonathan Lemon 		ui->ui_sum = 0;
969df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
970ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
971ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
972df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
973cfa1ca9dSYoshinobu Inoue 
9745c32ea65SRobert Watson 	if (unlock_udbinfo)
9755c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
97697d8d152SAndre Oppermann 	error = ip_output(m, inp->inp_options, NULL, ipflags,
9775d846453SSam Leffler 	    inp->inp_moptions, inp);
9788501a69cSRobert Watson 	INP_WUNLOCK(inp);
979df8bae1dSRodney W. Grimes 	return (error);
980df8bae1dSRodney W. Grimes 
981df8bae1dSRodney W. Grimes release:
9828501a69cSRobert Watson 	INP_WUNLOCK(inp);
9835c32ea65SRobert Watson 	if (unlock_udbinfo)
9845c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
985df8bae1dSRodney W. Grimes 	m_freem(m);
986df8bae1dSRodney W. Grimes 	return (error);
987df8bae1dSRodney W. Grimes }
988df8bae1dSRodney W. Grimes 
989ac45e92fSRobert Watson static void
990d0390e05SGarrett Wollman udp_abort(struct socket *so)
991df8bae1dSRodney W. Grimes {
992d0390e05SGarrett Wollman 	struct inpcb *inp;
993df8bae1dSRodney W. Grimes 
994d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
99514ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
99614ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
9978501a69cSRobert Watson 	INP_WLOCK(inp);
998a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
999a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1000a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1001d0390e05SGarrett Wollman 		soisdisconnected(so);
1002a152f8a3SRobert Watson 	}
10038501a69cSRobert Watson 	INP_WUNLOCK(inp);
1004f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1005df8bae1dSRodney W. Grimes }
1006df8bae1dSRodney W. Grimes 
1007d0390e05SGarrett Wollman static int
1008b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
1009d0390e05SGarrett Wollman {
1010d0390e05SGarrett Wollman 	struct inpcb *inp;
1011277afaffSRobert Watson 	int error;
1012d0390e05SGarrett Wollman 
1013d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
101414ba8addSRobert Watson 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1015cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
1016f24618aaSRobert Watson 	if (error)
10173329b236SRobert Watson 		return (error);
1018f24618aaSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1019d915b280SStephan Uphoff 	error = in_pcballoc(so, &udbinfo);
102053b57cd1SSam Leffler 	if (error) {
102153b57cd1SSam Leffler 		INP_INFO_WUNLOCK(&udbinfo);
10223329b236SRobert Watson 		return (error);
102353b57cd1SSam Leffler 	}
1024cfa1ca9dSYoshinobu Inoue 
1025cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
1026f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1027cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1028cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
10298501a69cSRobert Watson 	INP_WUNLOCK(inp);
10303329b236SRobert Watson 	return (0);
1031df8bae1dSRodney W. Grimes }
1032d0390e05SGarrett Wollman 
1033d0390e05SGarrett Wollman static int
1034b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1035d0390e05SGarrett Wollman {
1036d0390e05SGarrett Wollman 	struct inpcb *inp;
1037277afaffSRobert Watson 	int error;
1038d0390e05SGarrett Wollman 
1039d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
104014ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
104114ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
10428501a69cSRobert Watson 	INP_WLOCK(inp);
1043b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
10448501a69cSRobert Watson 	INP_WUNLOCK(inp);
1045f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
10463329b236SRobert Watson 	return (error);
1047d0390e05SGarrett Wollman }
1048d0390e05SGarrett Wollman 
1049a152f8a3SRobert Watson static void
1050a152f8a3SRobert Watson udp_close(struct socket *so)
1051a152f8a3SRobert Watson {
1052a152f8a3SRobert Watson 	struct inpcb *inp;
1053a152f8a3SRobert Watson 
1054a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1055a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1056a152f8a3SRobert Watson 	INP_INFO_WLOCK(&udbinfo);
10578501a69cSRobert Watson 	INP_WLOCK(inp);
1058a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1059a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1060a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1061a152f8a3SRobert Watson 		soisdisconnected(so);
1062a152f8a3SRobert Watson 	}
10638501a69cSRobert Watson 	INP_WUNLOCK(inp);
1064a152f8a3SRobert Watson 	INP_INFO_WUNLOCK(&udbinfo);
1065a152f8a3SRobert Watson }
1066a152f8a3SRobert Watson 
1067d0390e05SGarrett Wollman static int
1068b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1069d0390e05SGarrett Wollman {
1070d0390e05SGarrett Wollman 	struct inpcb *inp;
1071277afaffSRobert Watson 	int error;
107275c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1073d0390e05SGarrett Wollman 
1074d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
107514ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
107614ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
10778501a69cSRobert Watson 	INP_WLOCK(inp);
1078f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
10798501a69cSRobert Watson 		INP_WUNLOCK(inp);
1080f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
10813329b236SRobert Watson 		return (EISCONN);
1082f76fcf6dSJeffrey Hsu 	}
108375c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1084812d8653SSam Leffler 	if (jailed(td->td_ucred))
1085a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1086b0330ed9SPawel Jakub Dawidek 	error = in_pcbconnect(inp, nam, td->td_ucred);
10874cc20ab1SSeigo Tanimura 	if (error == 0)
1088df8bae1dSRodney W. Grimes 		soisconnected(so);
10898501a69cSRobert Watson 	INP_WUNLOCK(inp);
1090f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
10913329b236SRobert Watson 	return (error);
1092df8bae1dSRodney W. Grimes }
1093d0390e05SGarrett Wollman 
1094bc725eafSRobert Watson static void
1095d0390e05SGarrett Wollman udp_detach(struct socket *so)
1096d0390e05SGarrett Wollman {
1097d0390e05SGarrett Wollman 	struct inpcb *inp;
1098d0390e05SGarrett Wollman 
1099d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
110014ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1101a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1102a152f8a3SRobert Watson 	    ("udp_detach: not disconnected"));
110314ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
11048501a69cSRobert Watson 	INP_WLOCK(inp);
1105d0390e05SGarrett Wollman 	in_pcbdetach(inp);
110614ba8addSRobert Watson 	in_pcbfree(inp);
1107f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1108d0390e05SGarrett Wollman }
1109d0390e05SGarrett Wollman 
1110d0390e05SGarrett Wollman static int
1111d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1112d0390e05SGarrett Wollman {
1113d0390e05SGarrett Wollman 	struct inpcb *inp;
1114d0390e05SGarrett Wollman 
1115d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
111614ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
111714ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
11188501a69cSRobert Watson 	INP_WLOCK(inp);
1119f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
11208501a69cSRobert Watson 		INP_WUNLOCK(inp);
1121f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
11223329b236SRobert Watson 		return (ENOTCONN);
1123f76fcf6dSJeffrey Hsu 	}
1124d0390e05SGarrett Wollman 
1125df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1126df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1127d45e4f99SMaxim Konovalov 	SOCK_LOCK(so);
1128d45e4f99SMaxim Konovalov 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1129d45e4f99SMaxim Konovalov 	SOCK_UNLOCK(so);
11308501a69cSRobert Watson 	INP_WUNLOCK(inp);
1131f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
11323329b236SRobert Watson 	return (0);
1133df8bae1dSRodney W. Grimes }
1134df8bae1dSRodney W. Grimes 
1135d0390e05SGarrett Wollman static int
113657bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1137b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1138d0390e05SGarrett Wollman {
1139d0390e05SGarrett Wollman 	struct inpcb *inp;
1140d0390e05SGarrett Wollman 
1141d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
114214ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
11433329b236SRobert Watson 	return (udp_output(inp, m, addr, control, td));
1144d0390e05SGarrett Wollman }
1145d0390e05SGarrett Wollman 
114676429de4SYoshinobu Inoue int
1147d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1148d0390e05SGarrett Wollman {
1149d0390e05SGarrett Wollman 	struct inpcb *inp;
1150d0390e05SGarrett Wollman 
1151d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
115214ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
11538501a69cSRobert Watson 	INP_WLOCK(inp);
1154d0390e05SGarrett Wollman 	socantsendmore(so);
11558501a69cSRobert Watson 	INP_WUNLOCK(inp);
11563329b236SRobert Watson 	return (0);
1157d0390e05SGarrett Wollman }
1158d0390e05SGarrett Wollman 
1159d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1160756d52a1SPoul-Henning Kamp 	.pru_abort =		udp_abort,
1161756d52a1SPoul-Henning Kamp 	.pru_attach =		udp_attach,
1162756d52a1SPoul-Henning Kamp 	.pru_bind =		udp_bind,
1163756d52a1SPoul-Henning Kamp 	.pru_connect =		udp_connect,
1164756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1165756d52a1SPoul-Henning Kamp 	.pru_detach =		udp_detach,
1166756d52a1SPoul-Henning Kamp 	.pru_disconnect =	udp_disconnect,
116754d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1168756d52a1SPoul-Henning Kamp 	.pru_send =		udp_send,
11695df3e839SRobert Watson 	.pru_soreceive =	soreceive_dgram,
117059b8854eSRobert Watson 	.pru_sosend =		sosend_dgram,
1171756d52a1SPoul-Henning Kamp 	.pru_shutdown =		udp_shutdown,
117254d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1173a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1174a152f8a3SRobert Watson 	.pru_close =		udp_close,
1175d0390e05SGarrett Wollman };
1176