xref: /freebsd/sys/netinet/udp_usrreq.c (revision 48d48eb980f98e4631d96fd8f28f987d15f32496)
1c398230bSWarner Losh /*-
26dfab5b1SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
33329b236SRobert Watson  *	The Regents of the University of California.
43144b7d3SRobert Watson  * Copyright (c) 2008 Robert N. M. Watson
53329b236SRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
316dfab5b1SGarrett Wollman  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
344b421e2dSMike Silbersack #include <sys/cdefs.h>
354b421e2dSMike Silbersack __FBSDID("$FreeBSD$");
364b421e2dSMike Silbersack 
370b4ae859SGleb Smirnoff #include "opt_ipfw.h"
38cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
39f5514f08SRobert Watson #include "opt_ipsec.h"
40bdb3fa18SRobert Watson #include "opt_mac.h"
41cfa1ca9dSYoshinobu Inoue 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43960ed29cSSeigo Tanimura #include <sys/domain.h>
444f590175SPaul Saab #include <sys/eventhandler.h>
45960ed29cSSeigo Tanimura #include <sys/jail.h>
46b110a8a2SGarrett Wollman #include <sys/kernel.h>
47960ed29cSSeigo Tanimura #include <sys/lock.h>
48df8bae1dSRodney W. Grimes #include <sys/malloc.h>
49df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
50acd3428bSRobert Watson #include <sys/priv.h>
51490d50b6SBrian Feldman #include <sys/proc.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
53960ed29cSSeigo Tanimura #include <sys/signalvar.h>
54df8bae1dSRodney W. Grimes #include <sys/socket.h>
55df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
56960ed29cSSeigo Tanimura #include <sys/sx.h>
57b5e8ce9fSBruce Evans #include <sys/sysctl.h>
58816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
59f5514f08SRobert Watson #include <sys/systm.h>
608781d8e9SBruce Evans 
6169c2d429SJeff Roberson #include <vm/uma.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <net/if.h>
64df8bae1dSRodney W. Grimes #include <net/route.h>
65df8bae1dSRodney W. Grimes 
66df8bae1dSRodney W. Grimes #include <netinet/in.h>
67960ed29cSSeigo Tanimura #include <netinet/in_pcb.h>
68f5514f08SRobert Watson #include <netinet/in_systm.h>
69960ed29cSSeigo Tanimura #include <netinet/in_var.h>
70df8bae1dSRodney W. Grimes #include <netinet/ip.h>
71cfa1ca9dSYoshinobu Inoue #ifdef INET6
72cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
73cfa1ca9dSYoshinobu Inoue #endif
74960ed29cSSeigo Tanimura #include <netinet/ip_icmp.h>
75960ed29cSSeigo Tanimura #include <netinet/icmp_var.h>
76df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
77ef39adf0SAndre Oppermann #include <netinet/ip_options.h>
78cfa1ca9dSYoshinobu Inoue #ifdef INET6
79cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
80cfa1ca9dSYoshinobu Inoue #endif
81df8bae1dSRodney W. Grimes #include <netinet/udp.h>
82df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
83df8bae1dSRodney W. Grimes 
84b2630c29SGeorge V. Neville-Neil #ifdef IPSEC
85b9234fafSSam Leffler #include <netipsec/ipsec.h>
863329b236SRobert Watson #endif
87b9234fafSSam Leffler 
88db4f9cc7SJonathan Lemon #include <machine/in_cksum.h>
89db4f9cc7SJonathan Lemon 
90aed55708SRobert Watson #include <security/mac/mac_framework.h>
91aed55708SRobert Watson 
92df8bae1dSRodney W. Grimes /*
93df8bae1dSRodney W. Grimes  * UDP protocol implementation.
94df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
95df8bae1dSRodney W. Grimes  */
9674eb3236SWarner Losh 
9774eb3236SWarner Losh /*
983329b236SRobert Watson  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
993329b236SRobert Watson  * removes the only data integrity mechanism for packets and malformed
100f5514f08SRobert Watson  * packets that would otherwise be discarded due to bad checksums, and may
101f5514f08SRobert Watson  * cause problems (especially for NFS data blocks).
10274eb3236SWarner Losh  */
103f5514f08SRobert Watson static int	udp_cksum = 1;
104f5514f08SRobert Watson SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udp_cksum,
10541698ebfSTom Rhodes     0, "compute udp checksum");
106df8bae1dSRodney W. Grimes 
107afdb4274SRobert Watson int	udp_log_in_vain = 0;
108816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
109afdb4274SRobert Watson     &udp_log_in_vain, 0, "Log all incoming UDP packets");
110816a3d83SPoul-Henning Kamp 
11143bbb6aaSRobert Watson int	udp_blackhole = 0;
11243bbb6aaSRobert Watson SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, &udp_blackhole, 0,
1133329b236SRobert Watson     "Do not send port unreachables for refused connects");
11416f7f31fSGeoff Rehmet 
11543bbb6aaSRobert Watson u_long	udp_sendspace = 9216;		/* really max datagram size */
11643bbb6aaSRobert Watson 					/* 40 1K datagrams */
11743bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
11843bbb6aaSRobert Watson     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
11943bbb6aaSRobert Watson 
12043bbb6aaSRobert Watson u_long	udp_recvspace = 40 * (1024 +
12143bbb6aaSRobert Watson #ifdef INET6
12243bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in6)
12343bbb6aaSRobert Watson #else
12443bbb6aaSRobert Watson 				      sizeof(struct sockaddr_in)
12543bbb6aaSRobert Watson #endif
12643bbb6aaSRobert Watson 				      );
12743bbb6aaSRobert Watson 
12843bbb6aaSRobert Watson SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
12943bbb6aaSRobert Watson     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
13043bbb6aaSRobert Watson 
13176429de4SYoshinobu Inoue struct inpcbhead	udb;		/* from udp_var.h */
1327a2aab80SBrian Feldman struct inpcbinfo	udbinfo;
13315bd2b43SDavid Greenman 
13415bd2b43SDavid Greenman #ifndef UDBHASHSIZE
135e2ed8f35SAlexander Motin #define	UDBHASHSIZE	128
13615bd2b43SDavid Greenman #endif
13715bd2b43SDavid Greenman 
13876429de4SYoshinobu Inoue struct udpstat	udpstat;	/* from udp_var.h */
1393329b236SRobert Watson SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, &udpstat,
1403329b236SRobert Watson     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
141f2ea20e6SGarrett Wollman 
142bc725eafSRobert Watson static void	udp_detach(struct socket *so);
1434d77a549SAlfred Perlstein static int	udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
1444d77a549SAlfred Perlstein 		    struct mbuf *, struct thread *);
145df8bae1dSRodney W. Grimes 
1464f590175SPaul Saab static void
1474f590175SPaul Saab udp_zone_change(void *tag)
1484f590175SPaul Saab {
1494f590175SPaul Saab 
1504f590175SPaul Saab 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
1514f590175SPaul Saab }
1524f590175SPaul Saab 
153d915b280SStephan Uphoff static int
154d915b280SStephan Uphoff udp_inpcb_init(void *mem, int size, int flags)
155d915b280SStephan Uphoff {
156af1ee11dSRobert Watson 	struct inpcb *inp;
15708651e1fSJohn Baldwin 
158af1ee11dSRobert Watson 	inp = mem;
159d915b280SStephan Uphoff 	INP_LOCK_INIT(inp, "inp", "udpinp");
160d915b280SStephan Uphoff 	return (0);
161d915b280SStephan Uphoff }
162d915b280SStephan Uphoff 
163df8bae1dSRodney W. Grimes void
164af1ee11dSRobert Watson udp_init(void)
165df8bae1dSRodney W. Grimes {
166af1ee11dSRobert Watson 
167f76fcf6dSJeffrey Hsu 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
16815bd2b43SDavid Greenman 	LIST_INIT(&udb);
169712fc218SRobert Watson 	udbinfo.ipi_listhead = &udb;
170712fc218SRobert Watson 	udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB,
171712fc218SRobert Watson 	    &udbinfo.ipi_hashmask);
172712fc218SRobert Watson 	udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB,
173712fc218SRobert Watson 	    &udbinfo.ipi_porthashmask);
17469c2d429SJeff Roberson 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
175d915b280SStephan Uphoff 	    NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
17669c2d429SJeff Roberson 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
1774f590175SPaul Saab 	EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
1784f590175SPaul Saab 	    EVENTHANDLER_PRI_ANY);
179df8bae1dSRodney W. Grimes }
180df8bae1dSRodney W. Grimes 
18143bbb6aaSRobert Watson /*
18243bbb6aaSRobert Watson  * Subroutine of udp_input(), which appends the provided mbuf chain to the
18343bbb6aaSRobert Watson  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
18443bbb6aaSRobert Watson  * contains the source address.  If the socket ends up being an IPv6 socket,
18543bbb6aaSRobert Watson  * udp_append() will convert to a sockaddr_in6 before passing the address
18643bbb6aaSRobert Watson  * into the socket code.
18743bbb6aaSRobert Watson  */
18843bbb6aaSRobert Watson static void
18943bbb6aaSRobert Watson udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
19043bbb6aaSRobert Watson     struct sockaddr_in *udp_in)
19143bbb6aaSRobert Watson {
19243bbb6aaSRobert Watson 	struct sockaddr *append_sa;
19343bbb6aaSRobert Watson 	struct socket *so;
19443bbb6aaSRobert Watson 	struct mbuf *opts = 0;
19543bbb6aaSRobert Watson #ifdef INET6
19643bbb6aaSRobert Watson 	struct sockaddr_in6 udp_in6;
19743bbb6aaSRobert Watson #endif
19843bbb6aaSRobert Watson 
199119d85f6SRobert Watson 	INP_RLOCK_ASSERT(inp);
20043bbb6aaSRobert Watson 
20143bbb6aaSRobert Watson #ifdef IPSEC
20243bbb6aaSRobert Watson 	/* Check AH/ESP integrity. */
20343bbb6aaSRobert Watson 	if (ipsec4_in_reject(n, inp)) {
20443bbb6aaSRobert Watson 		m_freem(n);
20543bbb6aaSRobert Watson 		ipsec4stat.in_polvio++;
20643bbb6aaSRobert Watson 		return;
20743bbb6aaSRobert Watson 	}
20843bbb6aaSRobert Watson #endif /* IPSEC */
20943bbb6aaSRobert Watson #ifdef MAC
21030d239bcSRobert Watson 	if (mac_inpcb_check_deliver(inp, n) != 0) {
21143bbb6aaSRobert Watson 		m_freem(n);
21243bbb6aaSRobert Watson 		return;
21343bbb6aaSRobert Watson 	}
21443bbb6aaSRobert Watson #endif
21543bbb6aaSRobert Watson 	if (inp->inp_flags & INP_CONTROLOPTS ||
21643bbb6aaSRobert Watson 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
21743bbb6aaSRobert Watson #ifdef INET6
2189a38ba81SBjoern A. Zeeb 		if (inp->inp_vflag & INP_IPV6)
21948d48eb9SBjoern A. Zeeb 			(void)ip6_savecontrol_v4(inp, n, &opts, NULL);
2209a38ba81SBjoern A. Zeeb 		else
22143bbb6aaSRobert Watson #endif
22243bbb6aaSRobert Watson 			ip_savecontrol(inp, &opts, ip, n);
22343bbb6aaSRobert Watson 	}
22443bbb6aaSRobert Watson #ifdef INET6
22543bbb6aaSRobert Watson 	if (inp->inp_vflag & INP_IPV6) {
22643bbb6aaSRobert Watson 		bzero(&udp_in6, sizeof(udp_in6));
22743bbb6aaSRobert Watson 		udp_in6.sin6_len = sizeof(udp_in6);
22843bbb6aaSRobert Watson 		udp_in6.sin6_family = AF_INET6;
22943bbb6aaSRobert Watson 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
23043bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)&udp_in6;
23143bbb6aaSRobert Watson 	} else
23243bbb6aaSRobert Watson #endif
23343bbb6aaSRobert Watson 		append_sa = (struct sockaddr *)udp_in;
23443bbb6aaSRobert Watson 	m_adj(n, off);
23543bbb6aaSRobert Watson 
23643bbb6aaSRobert Watson 	so = inp->inp_socket;
23743bbb6aaSRobert Watson 	SOCKBUF_LOCK(&so->so_rcv);
23843bbb6aaSRobert Watson 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
23943bbb6aaSRobert Watson 		SOCKBUF_UNLOCK(&so->so_rcv);
24043bbb6aaSRobert Watson 		m_freem(n);
24143bbb6aaSRobert Watson 		if (opts)
24243bbb6aaSRobert Watson 			m_freem(opts);
24343bbb6aaSRobert Watson 		udpstat.udps_fullsock++;
24443bbb6aaSRobert Watson 	} else
24543bbb6aaSRobert Watson 		sorwakeup_locked(so);
24643bbb6aaSRobert Watson }
24743bbb6aaSRobert Watson 
248df8bae1dSRodney W. Grimes void
2493329b236SRobert Watson udp_input(struct mbuf *m, int off)
250df8bae1dSRodney W. Grimes {
251cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
2523329b236SRobert Watson 	struct ip *ip;
2533329b236SRobert Watson 	struct udphdr *uh;
25471498f30SBruce M Simpson 	struct ifnet *ifp;
2553329b236SRobert Watson 	struct inpcb *inp;
256df8bae1dSRodney W. Grimes 	int len;
257df8bae1dSRodney W. Grimes 	struct ip save_ip;
258d4b509bdSRobert Watson 	struct sockaddr_in udp_in;
2590b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD
2600b4ae859SGleb Smirnoff 	struct m_tag *fwd_tag;
2610b4ae859SGleb Smirnoff #endif
262df8bae1dSRodney W. Grimes 
26371498f30SBruce M Simpson 	ifp = m->m_pkthdr.rcvif;
264df8bae1dSRodney W. Grimes 	udpstat.udps_ipackets++;
265df8bae1dSRodney W. Grimes 
266df8bae1dSRodney W. Grimes 	/*
2673329b236SRobert Watson 	 * Strip IP options, if any; should skip this, make available to
2683329b236SRobert Watson 	 * user, and use on returned packets, but we don't yet have a way to
2693329b236SRobert Watson 	 * check the checksum with options still present.
270df8bae1dSRodney W. Grimes 	 */
271df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
272df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
273df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
274df8bae1dSRodney W. Grimes 	}
275df8bae1dSRodney W. Grimes 
276df8bae1dSRodney W. Grimes 	/*
277df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
278df8bae1dSRodney W. Grimes 	 */
279df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
280df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
281df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
282df8bae1dSRodney W. Grimes 			udpstat.udps_hdrops++;
283df8bae1dSRodney W. Grimes 			return;
284df8bae1dSRodney W. Grimes 		}
285df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
286df8bae1dSRodney W. Grimes 	}
287df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
288df8bae1dSRodney W. Grimes 
2893329b236SRobert Watson 	/*
2903329b236SRobert Watson 	 * Destination port of 0 is illegal, based on RFC768.
2913329b236SRobert Watson 	 */
292686cdd19SJun-ichiro itojun Hagino 	if (uh->uh_dport == 0)
293f76fcf6dSJeffrey Hsu 		goto badunlocked;
294686cdd19SJun-ichiro itojun Hagino 
295df8bae1dSRodney W. Grimes 	/*
2963329b236SRobert Watson 	 * Construct sockaddr format source address.  Stuff source address
2973329b236SRobert Watson 	 * and datagram in user buffer.
298b9234fafSSam Leffler 	 */
299d4b509bdSRobert Watson 	bzero(&udp_in, sizeof(udp_in));
300d4b509bdSRobert Watson 	udp_in.sin_len = sizeof(udp_in);
301d4b509bdSRobert Watson 	udp_in.sin_family = AF_INET;
302b9234fafSSam Leffler 	udp_in.sin_port = uh->uh_sport;
303b9234fafSSam Leffler 	udp_in.sin_addr = ip->ip_src;
304b9234fafSSam Leffler 
305b9234fafSSam Leffler 	/*
306af1ee11dSRobert Watson 	 * Make mbuf data length reflect UDP length.  If not enough data to
307af1ee11dSRobert Watson 	 * reflect UDP length, drop.
308df8bae1dSRodney W. Grimes 	 */
309df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
310df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
3117eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
312df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
313f76fcf6dSJeffrey Hsu 			goto badunlocked;
314df8bae1dSRodney W. Grimes 		}
315df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
316df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
317df8bae1dSRodney W. Grimes 	}
3183329b236SRobert Watson 
319df8bae1dSRodney W. Grimes 	/*
3203329b236SRobert Watson 	 * Save a copy of the IP header in case we want restore it for
3213329b236SRobert Watson 	 * sending an ICMP error message in response.
322df8bae1dSRodney W. Grimes 	 */
32343bbb6aaSRobert Watson 	if (!udp_blackhole)
324df8bae1dSRodney W. Grimes 		save_ip = *ip;
325cce418d3SMatt Jacob 	else
326cce418d3SMatt Jacob 		memset(&save_ip, 0, sizeof(save_ip));
327df8bae1dSRodney W. Grimes 
328df8bae1dSRodney W. Grimes 	/*
329df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
330df8bae1dSRodney W. Grimes 	 */
3316dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
33239629c92SDavid Malone 		u_short uh_sum;
33339629c92SDavid Malone 
334db4f9cc7SJonathan Lemon 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
335db4f9cc7SJonathan Lemon 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
33639629c92SDavid Malone 				uh_sum = m->m_pkthdr.csum_data;
337db4f9cc7SJonathan Lemon 			else
33839629c92SDavid Malone 				uh_sum = in_pseudo(ip->ip_src.s_addr,
339506f4949SRuslan Ermilov 				    ip->ip_dst.s_addr, htonl((u_short)len +
340db4f9cc7SJonathan Lemon 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
34139629c92SDavid Malone 			uh_sum ^= 0xffff;
342db4f9cc7SJonathan Lemon 		} else {
343cb342100SHajimu UMEMOTO 			char b[9];
344af1ee11dSRobert Watson 
345cb342100SHajimu UMEMOTO 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
3466effc713SDoug Rabson 			bzero(((struct ipovly *)ip)->ih_x1, 9);
347df8bae1dSRodney W. Grimes 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
34839629c92SDavid Malone 			uh_sum = in_cksum(m, len + sizeof (struct ip));
349cb342100SHajimu UMEMOTO 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
350db4f9cc7SJonathan Lemon 		}
35139629c92SDavid Malone 		if (uh_sum) {
352df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
353df8bae1dSRodney W. Grimes 			m_freem(m);
354df8bae1dSRodney W. Grimes 			return;
355df8bae1dSRodney W. Grimes 		}
356fb9aaba0SRuslan Ermilov 	} else
357fb9aaba0SRuslan Ermilov 		udpstat.udps_nosum++;
358df8bae1dSRodney W. Grimes 
3590b4ae859SGleb Smirnoff #ifdef IPFIREWALL_FORWARD
3603329b236SRobert Watson 	/*
3613329b236SRobert Watson 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
3623329b236SRobert Watson 	 */
3630b4ae859SGleb Smirnoff 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
3640b4ae859SGleb Smirnoff 	if (fwd_tag != NULL) {
3650b4ae859SGleb Smirnoff 		struct sockaddr_in *next_hop;
3660b4ae859SGleb Smirnoff 
3673329b236SRobert Watson 		/*
3683329b236SRobert Watson 		 * Do the hack.
3693329b236SRobert Watson 		 */
3700b4ae859SGleb Smirnoff 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
3710b4ae859SGleb Smirnoff 		ip->ip_dst = next_hop->sin_addr;
3720b4ae859SGleb Smirnoff 		uh->uh_dport = ntohs(next_hop->sin_port);
3733329b236SRobert Watson 
3743329b236SRobert Watson 		/*
3753329b236SRobert Watson 		 * Remove the tag from the packet.  We don't need it anymore.
3763329b236SRobert Watson 		 */
3770b4ae859SGleb Smirnoff 		m_tag_delete(m, fwd_tag);
3780b4ae859SGleb Smirnoff 	}
3790b4ae859SGleb Smirnoff #endif
3800b4ae859SGleb Smirnoff 
381f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
382df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
38371498f30SBruce M Simpson 	    in_broadcast(ip->ip_dst, ifp)) {
38482c23ebaSBill Fenner 		struct inpcb *last;
38571498f30SBruce M Simpson 		struct ip_moptions *imo;
3863329b236SRobert Watson 
387df8bae1dSRodney W. Grimes 		last = NULL;
388cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
3899c1df695SRobert Watson 			if (inp->inp_lport != uh->uh_dport)
390f76fcf6dSJeffrey Hsu 				continue;
391cfa1ca9dSYoshinobu Inoue #ifdef INET6
392369dc8ceSEivind Eklund 			if ((inp->inp_vflag & INP_IPV4) == 0)
3939c1df695SRobert Watson 				continue;
394cfa1ca9dSYoshinobu Inoue #endif
39571498f30SBruce M Simpson 			if (inp->inp_laddr.s_addr != INADDR_ANY &&
39671498f30SBruce M Simpson 			    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
3979c1df695SRobert Watson 				continue;
39871498f30SBruce M Simpson 			if (inp->inp_faddr.s_addr != INADDR_ANY &&
39971498f30SBruce M Simpson 			    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
40071498f30SBruce M Simpson 				continue;
40171498f30SBruce M Simpson 			/*
40271498f30SBruce M Simpson 			 * XXX: Do not check source port of incoming datagram
40371498f30SBruce M Simpson 			 * unless inp_connect() has been called to bind the
40471498f30SBruce M Simpson 			 * fport part of the 4-tuple; the source could be
40571498f30SBruce M Simpson 			 * trying to talk to us with an ephemeral port.
40671498f30SBruce M Simpson 			 */
40771498f30SBruce M Simpson 			if (inp->inp_fport != 0 &&
408df8bae1dSRodney W. Grimes 			    inp->inp_fport != uh->uh_sport)
4099c1df695SRobert Watson 				continue;
41071498f30SBruce M Simpson 
411119d85f6SRobert Watson 			INP_RLOCK(inp);
412df8bae1dSRodney W. Grimes 
41383453a06SBruce M Simpson 			/*
41471498f30SBruce M Simpson 			 * Handle socket delivery policy for any-source
41571498f30SBruce M Simpson 			 * and source-specific multicast. [RFC3678]
41683453a06SBruce M Simpson 			 */
41771498f30SBruce M Simpson 			imo = inp->inp_moptions;
41871498f30SBruce M Simpson 			if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
41971498f30SBruce M Simpson 			    imo != NULL) {
42071498f30SBruce M Simpson 				struct sockaddr_in	 sin;
42171498f30SBruce M Simpson 				struct in_msource	*ims;
42271498f30SBruce M Simpson 				int			 blocked, mode;
42371498f30SBruce M Simpson 				size_t			 idx;
42483453a06SBruce M Simpson 
42571498f30SBruce M Simpson 				bzero(&sin, sizeof(struct sockaddr_in));
42671498f30SBruce M Simpson 				sin.sin_len = sizeof(struct sockaddr_in);
42771498f30SBruce M Simpson 				sin.sin_family = AF_INET;
42871498f30SBruce M Simpson 				sin.sin_addr = ip->ip_dst;
42971498f30SBruce M Simpson 
43071498f30SBruce M Simpson 				blocked = 0;
43171498f30SBruce M Simpson 				idx = imo_match_group(imo, ifp,
43271498f30SBruce M Simpson 				    (struct sockaddr *)&sin);
43371498f30SBruce M Simpson 				if (idx == -1) {
43471498f30SBruce M Simpson 					/*
43571498f30SBruce M Simpson 					 * No group membership for this socket.
43671498f30SBruce M Simpson 					 * Do not bump udps_noportbcast, as
43771498f30SBruce M Simpson 					 * this will happen further down.
43871498f30SBruce M Simpson 					 */
43971498f30SBruce M Simpson 					blocked++;
44071498f30SBruce M Simpson 				} else {
44171498f30SBruce M Simpson 					/*
44271498f30SBruce M Simpson 					 * Check for a multicast source filter
44371498f30SBruce M Simpson 					 * entry on this socket for this group.
44471498f30SBruce M Simpson 					 * MCAST_EXCLUDE is the default
44571498f30SBruce M Simpson 					 * behaviour.  It means default accept;
44671498f30SBruce M Simpson 					 * entries, if present, denote sources
44771498f30SBruce M Simpson 					 * to be excluded from delivery.
44871498f30SBruce M Simpson 					 */
44971498f30SBruce M Simpson 					ims = imo_match_source(imo, idx,
45071498f30SBruce M Simpson 					    (struct sockaddr *)&udp_in);
45171498f30SBruce M Simpson 					mode = imo->imo_mfilters[idx].imf_fmode;
45271498f30SBruce M Simpson 					if ((ims != NULL &&
45371498f30SBruce M Simpson 					     mode == MCAST_EXCLUDE) ||
45471498f30SBruce M Simpson 					    (ims == NULL &&
45571498f30SBruce M Simpson 					     mode == MCAST_INCLUDE)) {
45671498f30SBruce M Simpson #ifdef DIAGNOSTIC
45771498f30SBruce M Simpson 						if (bootverbose) {
45871498f30SBruce M Simpson 							printf("%s: blocked by"
45971498f30SBruce M Simpson 							    " source filter\n",
46071498f30SBruce M Simpson 							    __func__);
46171498f30SBruce M Simpson 						}
46271498f30SBruce M Simpson #endif
46371498f30SBruce M Simpson 						udpstat.udps_filtermcast++;
46471498f30SBruce M Simpson 						blocked++;
46583453a06SBruce M Simpson 					}
46683453a06SBruce M Simpson 				}
46771498f30SBruce M Simpson 				if (blocked != 0) {
468119d85f6SRobert Watson 					INP_RUNLOCK(inp);
4699c1df695SRobert Watson 					continue;
4709c1df695SRobert Watson 				}
47183453a06SBruce M Simpson 			}
472df8bae1dSRodney W. Grimes 			if (last != NULL) {
473df8bae1dSRodney W. Grimes 				struct mbuf *n;
474df8bae1dSRodney W. Grimes 
475032dcc76SLuigi Rizzo 				n = m_copy(m, 0, M_COPYALL);
476365433d9SRobert Watson 				if (n != NULL)
4773329b236SRobert Watson 					udp_append(last, ip, n, iphlen +
4783329b236SRobert Watson 					    sizeof(struct udphdr), &udp_in);
479119d85f6SRobert Watson 				INP_RUNLOCK(last);
480df8bae1dSRodney W. Grimes 			}
48182c23ebaSBill Fenner 			last = inp;
482df8bae1dSRodney W. Grimes 			/*
483df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
484df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
4853329b236SRobert Watson 			 * socket options set.  This heuristic avoids
4863329b236SRobert Watson 			 * searching through all pcbs in the common case of a
4873329b236SRobert Watson 			 * non-shared port.  It assumes that an application
4883329b236SRobert Watson 			 * will never clear these options after setting them.
489df8bae1dSRodney W. Grimes 			 */
4903329b236SRobert Watson 			if ((last->inp_socket->so_options &
4913329b236SRobert Watson 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
492df8bae1dSRodney W. Grimes 				break;
493df8bae1dSRodney W. Grimes 		}
494df8bae1dSRodney W. Grimes 
495df8bae1dSRodney W. Grimes 		if (last == NULL) {
496df8bae1dSRodney W. Grimes 			/*
4973329b236SRobert Watson 			 * No matching pcb found; discard datagram.  (No need
4983329b236SRobert Watson 			 * to send an ICMP Port Unreachable for a broadcast
4993329b236SRobert Watson 			 * or multicast datgram.)
500df8bae1dSRodney W. Grimes 			 */
501df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
50261ffc0b1SJeffrey Hsu 			goto badheadlocked;
503df8bae1dSRodney W. Grimes 		}
504d4b509bdSRobert Watson 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
505d4b509bdSRobert Watson 		    &udp_in);
506119d85f6SRobert Watson 		INP_RUNLOCK(last);
5076b8e5a98SRobert Watson 		INP_INFO_RUNLOCK(&udbinfo);
508df8bae1dSRodney W. Grimes 		return;
509df8bae1dSRodney W. Grimes 	}
5103329b236SRobert Watson 
511df8bae1dSRodney W. Grimes 	/*
5126d6a026bSDavid Greenman 	 * Locate pcb for datagram.
513df8bae1dSRodney W. Grimes 	 */
514c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
51571498f30SBruce M Simpson 	    ip->ip_dst, uh->uh_dport, 1, ifp);
51615bd2b43SDavid Greenman 	if (inp == NULL) {
517afdb4274SRobert Watson 		if (udp_log_in_vain) {
518df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
51975cfc95fSAndrey A. Chernov 
52075cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
521592071e8SBruce Evans 			log(LOG_INFO,
522592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
523592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
524592071e8SBruce Evans 			    ntohs(uh->uh_sport));
52575cfc95fSAndrey A. Chernov 		}
526df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
527df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
528df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
52961ffc0b1SJeffrey Hsu 			goto badheadlocked;
530df8bae1dSRodney W. Grimes 		}
53143bbb6aaSRobert Watson 		if (udp_blackhole)
53261ffc0b1SJeffrey Hsu 			goto badheadlocked;
5331cbd978eSLuigi Rizzo 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
5341cbd978eSLuigi Rizzo 			goto badheadlocked;
53504287599SRuslan Ermilov 		*ip = save_ip;
53604287599SRuslan Ermilov 		ip->ip_len += iphlen;
537582a7760SBruce Evans 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
538f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
539df8bae1dSRodney W. Grimes 		return;
540df8bae1dSRodney W. Grimes 	}
5413329b236SRobert Watson 
5423329b236SRobert Watson 	/*
5433329b236SRobert Watson 	 * Check the minimum TTL for socket.
5443329b236SRobert Watson 	 */
545119d85f6SRobert Watson 	INP_RLOCK(inp);
54610cc62b7SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
54710cc62b7SRobert Watson 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
54810cc62b7SRobert Watson 		INP_RUNLOCK(inp);
54910cc62b7SRobert Watson 		goto badunlocked;
55010cc62b7SRobert Watson 	}
551d4b509bdSRobert Watson 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
552119d85f6SRobert Watson 	INP_RUNLOCK(inp);
553df8bae1dSRodney W. Grimes 	return;
55461ffc0b1SJeffrey Hsu 
55561ffc0b1SJeffrey Hsu badheadlocked:
556f76fcf6dSJeffrey Hsu 	if (inp)
557119d85f6SRobert Watson 		INP_RUNLOCK(inp);
5586b8e5a98SRobert Watson 	INP_INFO_RUNLOCK(&udbinfo);
559f76fcf6dSJeffrey Hsu badunlocked:
560df8bae1dSRodney W. Grimes 	m_freem(m);
561cfa1ca9dSYoshinobu Inoue }
562cfa1ca9dSYoshinobu Inoue 
563cfa1ca9dSYoshinobu Inoue /*
5643329b236SRobert Watson  * Notify a udp user of an asynchronous error; just wake up so that they can
5653329b236SRobert Watson  * collect error status.
566df8bae1dSRodney W. Grimes  */
5673ce144eaSJeffrey Hsu struct inpcb *
5683329b236SRobert Watson udp_notify(struct inpcb *inp, int errno)
569df8bae1dSRodney W. Grimes {
5703329b236SRobert Watson 
571ac9ae279SRobert Watson 	/*
572ac9ae279SRobert Watson 	 * While udp_ctlinput() always calls udp_notify() with a read lock
573ac9ae279SRobert Watson 	 * when invoking it directly, in_pcbnotifyall() currently uses write
574ac9ae279SRobert Watson 	 * locks due to sharing code with TCP.  For now, accept either a read
575ac9ae279SRobert Watson 	 * or a write lock, but a read lock is sufficient.
576ac9ae279SRobert Watson 	 */
577ac9ae279SRobert Watson 	INP_LOCK_ASSERT(inp);
5788501a69cSRobert Watson 
579df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
580df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
581df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
5823329b236SRobert Watson 	return (inp);
583df8bae1dSRodney W. Grimes }
584df8bae1dSRodney W. Grimes 
585df8bae1dSRodney W. Grimes void
5863329b236SRobert Watson udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
587df8bae1dSRodney W. Grimes {
588c693a045SJonathan Lemon 	struct ip *ip = vip;
589c693a045SJonathan Lemon 	struct udphdr *uh;
590c693a045SJonathan Lemon 	struct in_addr faddr;
591c693a045SJonathan Lemon 	struct inpcb *inp;
592c693a045SJonathan Lemon 
593c693a045SJonathan Lemon 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
594c693a045SJonathan Lemon 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
595c693a045SJonathan Lemon 		return;
596df8bae1dSRodney W. Grimes 
59797d8d152SAndre Oppermann 	/*
59897d8d152SAndre Oppermann 	 * Redirects don't need to be handled up here.
59997d8d152SAndre Oppermann 	 */
60097d8d152SAndre Oppermann 	if (PRC_IS_REDIRECT(cmd))
60197d8d152SAndre Oppermann 		return;
6023329b236SRobert Watson 
60397d8d152SAndre Oppermann 	/*
60497d8d152SAndre Oppermann 	 * Hostdead is ugly because it goes linearly through all PCBs.
6053329b236SRobert Watson 	 *
6063329b236SRobert Watson 	 * XXX: We never get this from ICMP, otherwise it makes an excellent
6073329b236SRobert Watson 	 * DoS attack on machines with many connections.
60897d8d152SAndre Oppermann 	 */
60997d8d152SAndre Oppermann 	if (cmd == PRC_HOSTDEAD)
610af1ee11dSRobert Watson 		ip = NULL;
611d1c54148SJesper Skriver 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
612df8bae1dSRodney W. Grimes 		return;
613af1ee11dSRobert Watson 	if (ip != NULL) {
614df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
615f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
616c693a045SJonathan Lemon 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
617c693a045SJonathan Lemon 		    ip->ip_src, uh->uh_sport, 0, NULL);
618f76fcf6dSJeffrey Hsu 		if (inp != NULL) {
619ac9ae279SRobert Watson 			INP_RLOCK(inp);
620f76fcf6dSJeffrey Hsu 			if (inp->inp_socket != NULL) {
621f5514f08SRobert Watson 				udp_notify(inp, inetctlerrmap[cmd]);
622f76fcf6dSJeffrey Hsu 			}
623ac9ae279SRobert Watson 			INP_RUNLOCK(inp);
624f76fcf6dSJeffrey Hsu 		}
625f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
626df8bae1dSRodney W. Grimes 	} else
627f5514f08SRobert Watson 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd],
628f5514f08SRobert Watson 		    udp_notify);
629df8bae1dSRodney W. Grimes }
630df8bae1dSRodney W. Grimes 
6310312fbe9SPoul-Henning Kamp static int
63282d9ae4eSPoul-Henning Kamp udp_pcblist(SYSCTL_HANDLER_ARGS)
63398271db4SGarrett Wollman {
634277afaffSRobert Watson 	int error, i, n;
63598271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
63698271db4SGarrett Wollman 	inp_gen_t gencnt;
63798271db4SGarrett Wollman 	struct xinpgen xig;
63898271db4SGarrett Wollman 
63998271db4SGarrett Wollman 	/*
640f5514f08SRobert Watson 	 * The process of preparing the PCB list is too time-consuming and
64198271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
64298271db4SGarrett Wollman 	 */
64398271db4SGarrett Wollman 	if (req->oldptr == 0) {
64498271db4SGarrett Wollman 		n = udbinfo.ipi_count;
64598271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
64698271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
6473329b236SRobert Watson 		return (0);
64898271db4SGarrett Wollman 	}
64998271db4SGarrett Wollman 
65098271db4SGarrett Wollman 	if (req->newptr != 0)
6513329b236SRobert Watson 		return (EPERM);
65298271db4SGarrett Wollman 
65398271db4SGarrett Wollman 	/*
65498271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
65598271db4SGarrett Wollman 	 */
6564b40c56cSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
65798271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
65898271db4SGarrett Wollman 	n = udbinfo.ipi_count;
6594b40c56cSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
66098271db4SGarrett Wollman 
66147934cefSDon Lewis 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
6625c38b6dbSDon Lewis 		+ n * sizeof(struct xinpcb));
66347934cefSDon Lewis 	if (error != 0)
66447934cefSDon Lewis 		return (error);
6655c38b6dbSDon Lewis 
66698271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
66798271db4SGarrett Wollman 	xig.xig_count = n;
66898271db4SGarrett Wollman 	xig.xig_gen = gencnt;
66998271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
67098271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
67198271db4SGarrett Wollman 	if (error)
6723329b236SRobert Watson 		return (error);
67398271db4SGarrett Wollman 
674a163d034SWarner Losh 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
67598271db4SGarrett Wollman 	if (inp_list == 0)
6763329b236SRobert Watson 		return (ENOMEM);
67798271db4SGarrett Wollman 
678f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
679712fc218SRobert Watson 	for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n;
680fc2ffbe6SPoul-Henning Kamp 	     inp = LIST_NEXT(inp, inp_list)) {
6819622e84fSRobert Watson 		INP_RLOCK(inp);
6822ded288cSJeffrey Hsu 		if (inp->inp_gencnt <= gencnt &&
6832ded288cSJeffrey Hsu 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
68498271db4SGarrett Wollman 			inp_list[i++] = inp;
6859622e84fSRobert Watson 		INP_RUNLOCK(inp);
6864787fd37SPaul Saab 	}
687f76fcf6dSJeffrey Hsu 	INP_INFO_RUNLOCK(&udbinfo);
68898271db4SGarrett Wollman 	n = i;
68998271db4SGarrett Wollman 
69098271db4SGarrett Wollman 	error = 0;
69198271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
69298271db4SGarrett Wollman 		inp = inp_list[i];
6939622e84fSRobert Watson 		INP_RLOCK(inp);
69498271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
69598271db4SGarrett Wollman 			struct xinpcb xi;
696fd94099eSColin Percival 			bzero(&xi, sizeof(xi));
69798271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
69898271db4SGarrett Wollman 			/* XXX should avoid extra copy */
69998271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
70098271db4SGarrett Wollman 			if (inp->inp_socket)
70198271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
7024b40c56cSJeffrey Hsu 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
7039622e84fSRobert Watson 			INP_RUNLOCK(inp);
70498271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
705d915b280SStephan Uphoff 		} else
7069622e84fSRobert Watson 			INP_RUNLOCK(inp);
70798271db4SGarrett Wollman 	}
70898271db4SGarrett Wollman 	if (!error) {
70998271db4SGarrett Wollman 		/*
7103329b236SRobert Watson 		 * Give the user an updated idea of our state.  If the
7113329b236SRobert Watson 		 * generation differs from what we told her before, she knows
7123329b236SRobert Watson 		 * that something happened while we were processing this
7133329b236SRobert Watson 		 * request, and it might be necessary to retry.
71498271db4SGarrett Wollman 		 */
715f76fcf6dSJeffrey Hsu 		INP_INFO_RLOCK(&udbinfo);
71698271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
71798271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
71898271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
719f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
72098271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
72198271db4SGarrett Wollman 	}
72298271db4SGarrett Wollman 	free(inp_list, M_TEMP);
7233329b236SRobert Watson 	return (error);
72498271db4SGarrett Wollman }
72598271db4SGarrett Wollman 
72698271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
72798271db4SGarrett Wollman     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
72898271db4SGarrett Wollman 
72998271db4SGarrett Wollman static int
73082d9ae4eSPoul-Henning Kamp udp_getcred(SYSCTL_HANDLER_ARGS)
731490d50b6SBrian Feldman {
732c0511d3bSBrian Feldman 	struct xucred xuc;
733490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
734490d50b6SBrian Feldman 	struct inpcb *inp;
735277afaffSRobert Watson 	int error;
736490d50b6SBrian Feldman 
73732f9753cSRobert Watson 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
738490d50b6SBrian Feldman 	if (error)
739490d50b6SBrian Feldman 		return (error);
740490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
741490d50b6SBrian Feldman 	if (error)
742490d50b6SBrian Feldman 		return (error);
743f76fcf6dSJeffrey Hsu 	INP_INFO_RLOCK(&udbinfo);
744490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
745cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
7469622e84fSRobert Watson 	if (inp != NULL) {
7479622e84fSRobert Watson 		INP_RLOCK(inp);
748f76fcf6dSJeffrey Hsu 		INP_INFO_RUNLOCK(&udbinfo);
7499622e84fSRobert Watson 		if (inp->inp_socket == NULL)
7509622e84fSRobert Watson 			error = ENOENT;
7519622e84fSRobert Watson 		if (error == 0)
7529622e84fSRobert Watson 			error = cr_canseesocket(req->td->td_ucred,
7539622e84fSRobert Watson 			    inp->inp_socket);
7549622e84fSRobert Watson 		if (error == 0)
7559622e84fSRobert Watson 			cru2x(inp->inp_socket->so_cred, &xuc);
7569622e84fSRobert Watson 		INP_RUNLOCK(inp);
7579622e84fSRobert Watson 	} else {
7589622e84fSRobert Watson 		INP_INFO_RUNLOCK(&udbinfo);
7599622e84fSRobert Watson 		error = ENOENT;
7609622e84fSRobert Watson 	}
7610e1eebb8SDon Lewis 	if (error == 0)
7620e1eebb8SDon Lewis 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
763490d50b6SBrian Feldman 	return (error);
764490d50b6SBrian Feldman }
765490d50b6SBrian Feldman 
7667ce87f12SDavid Malone SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
7677ce87f12SDavid Malone     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
7687ce87f12SDavid Malone     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
769490d50b6SBrian Feldman 
770490d50b6SBrian Feldman static int
7713329b236SRobert Watson udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
7723329b236SRobert Watson     struct mbuf *control, struct thread *td)
773df8bae1dSRodney W. Grimes {
7743329b236SRobert Watson 	struct udpiphdr *ui;
7753329b236SRobert Watson 	int len = m->m_pkthdr.len;
77690162a4eSIan Dowse 	struct in_addr faddr, laddr;
777c557ae16SIan Dowse 	struct cmsghdr *cm;
778c557ae16SIan Dowse 	struct sockaddr_in *sin, src;
77990162a4eSIan Dowse 	int error = 0;
7808afa2304SBruce M Simpson 	int ipflags;
78190162a4eSIan Dowse 	u_short fport, lport;
7825c32ea65SRobert Watson 	int unlock_udbinfo;
783df8bae1dSRodney W. Grimes 
7845c32ea65SRobert Watson 	/*
7855c32ea65SRobert Watson 	 * udp_output() may need to temporarily bind or connect the current
786f5514f08SRobert Watson 	 * inpcb.  As such, we don't know up front whether we will need the
787f5514f08SRobert Watson 	 * pcbinfo lock or not.  Do any work to decide what is needed up
788f5514f08SRobert Watson 	 * front before acquiring any locks.
7895c32ea65SRobert Watson 	 */
790430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
791c557ae16SIan Dowse 		if (control)
792c557ae16SIan Dowse 			m_freem(control);
7935c32ea65SRobert Watson 		m_freem(m);
7943329b236SRobert Watson 		return (EMSGSIZE);
795430d30d8SBill Fenner 	}
796430d30d8SBill Fenner 
7971b7f0384SBruce M Simpson 	src.sin_family = 0;
798c557ae16SIan Dowse 	if (control != NULL) {
799c557ae16SIan Dowse 		/*
8003329b236SRobert Watson 		 * XXX: Currently, we assume all the optional information is
8013329b236SRobert Watson 		 * stored in a single mbuf.
802c557ae16SIan Dowse 		 */
803c557ae16SIan Dowse 		if (control->m_next) {
804c557ae16SIan Dowse 			m_freem(control);
8055c32ea65SRobert Watson 			m_freem(m);
8063329b236SRobert Watson 			return (EINVAL);
807c557ae16SIan Dowse 		}
808c557ae16SIan Dowse 		for (; control->m_len > 0;
809c557ae16SIan Dowse 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
810c557ae16SIan Dowse 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
811c557ae16SIan Dowse 			cm = mtod(control, struct cmsghdr *);
812af1ee11dSRobert Watson 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
813af1ee11dSRobert Watson 			    || cm->cmsg_len > control->m_len) {
814c557ae16SIan Dowse 				error = EINVAL;
815c557ae16SIan Dowse 				break;
816c557ae16SIan Dowse 			}
817c557ae16SIan Dowse 			if (cm->cmsg_level != IPPROTO_IP)
818c557ae16SIan Dowse 				continue;
819c557ae16SIan Dowse 
820c557ae16SIan Dowse 			switch (cm->cmsg_type) {
821c557ae16SIan Dowse 			case IP_SENDSRCADDR:
822c557ae16SIan Dowse 				if (cm->cmsg_len !=
823c557ae16SIan Dowse 				    CMSG_LEN(sizeof(struct in_addr))) {
824c557ae16SIan Dowse 					error = EINVAL;
825c557ae16SIan Dowse 					break;
826c557ae16SIan Dowse 				}
827c557ae16SIan Dowse 				bzero(&src, sizeof(src));
828c557ae16SIan Dowse 				src.sin_family = AF_INET;
829c557ae16SIan Dowse 				src.sin_len = sizeof(src);
830c557ae16SIan Dowse 				src.sin_port = inp->inp_lport;
831af1ee11dSRobert Watson 				src.sin_addr =
832af1ee11dSRobert Watson 				    *(struct in_addr *)CMSG_DATA(cm);
833c557ae16SIan Dowse 				break;
834af1ee11dSRobert Watson 
835c557ae16SIan Dowse 			default:
836c557ae16SIan Dowse 				error = ENOPROTOOPT;
837c557ae16SIan Dowse 				break;
838c557ae16SIan Dowse 			}
839c557ae16SIan Dowse 			if (error)
840c557ae16SIan Dowse 				break;
841c557ae16SIan Dowse 		}
842c557ae16SIan Dowse 		m_freem(control);
843c557ae16SIan Dowse 	}
8445c32ea65SRobert Watson 	if (error) {
8455c32ea65SRobert Watson 		m_freem(m);
8463329b236SRobert Watson 		return (error);
8475c32ea65SRobert Watson 	}
8485c32ea65SRobert Watson 
84943cc0bc1SRobert Watson 	/*
85043cc0bc1SRobert Watson 	 * Depending on whether or not the application has bound or connected
851ca528788SRobert Watson 	 * the socket, we may have to do varying levels of work.  The optimal
852ca528788SRobert Watson 	 * case is for a connected UDP socket, as a global lock isn't
853ca528788SRobert Watson 	 * required at all.
85443cc0bc1SRobert Watson 	 *
85543cc0bc1SRobert Watson 	 * In order to decide which we need, we require stability of the
85643cc0bc1SRobert Watson 	 * inpcb binding, which we ensure by acquiring a read lock on the
85743cc0bc1SRobert Watson 	 * inpcb.  This doesn't strictly follow the lock order, so we play
85843cc0bc1SRobert Watson 	 * the trylock and retry game; note that we may end up with more
85943cc0bc1SRobert Watson 	 * conservative locks than required the second time around, so later
86043cc0bc1SRobert Watson 	 * assertions have to accept that.  Further analysis of the number of
86143cc0bc1SRobert Watson 	 * misses under contention is required.
86243cc0bc1SRobert Watson 	 */
86343cc0bc1SRobert Watson 	sin = (struct sockaddr_in *)addr;
86443cc0bc1SRobert Watson 	INP_RLOCK(inp);
86543cc0bc1SRobert Watson 	if (sin != NULL &&
86643cc0bc1SRobert Watson 	    (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
86743cc0bc1SRobert Watson 		INP_RUNLOCK(inp);
8685c32ea65SRobert Watson 		INP_INFO_WLOCK(&udbinfo);
8698501a69cSRobert Watson 		INP_WLOCK(inp);
87043cc0bc1SRobert Watson 		unlock_udbinfo = 2;
87143cc0bc1SRobert Watson 	} else if ((sin != NULL && (
87243cc0bc1SRobert Watson 	    (sin->sin_addr.s_addr == INADDR_ANY) ||
87343cc0bc1SRobert Watson 	    (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
87443cc0bc1SRobert Watson 	    (inp->inp_laddr.s_addr == INADDR_ANY) ||
87543cc0bc1SRobert Watson 	    (inp->inp_lport == 0))) ||
87643cc0bc1SRobert Watson 	    (src.sin_family == AF_INET)) {
87743cc0bc1SRobert Watson 		if (!INP_INFO_TRY_RLOCK(&udbinfo)) {
87843cc0bc1SRobert Watson 			INP_RUNLOCK(inp);
87943cc0bc1SRobert Watson 			INP_INFO_RLOCK(&udbinfo);
880948d0fc9SRobert Watson 			INP_RLOCK(inp);
881948d0fc9SRobert Watson 		}
88243cc0bc1SRobert Watson 		unlock_udbinfo = 1;
88343cc0bc1SRobert Watson 	} else
88443cc0bc1SRobert Watson 		unlock_udbinfo = 0;
8855c32ea65SRobert Watson 
8861b7f0384SBruce M Simpson 	/*
8871b7f0384SBruce M Simpson 	 * If the IP_SENDSRCADDR control message was specified, override the
8881b7f0384SBruce M Simpson 	 * source address for this datagram.  Its use is invalidated if the
8891b7f0384SBruce M Simpson 	 * address thus specified is incomplete or clobbers other inpcbs.
8901b7f0384SBruce M Simpson 	 */
89190162a4eSIan Dowse 	laddr = inp->inp_laddr;
89290162a4eSIan Dowse 	lport = inp->inp_lport;
8931b7f0384SBruce M Simpson 	if (src.sin_family == AF_INET) {
894c4d585aeSRobert Watson 		INP_INFO_LOCK_ASSERT(&udbinfo);
8951b7f0384SBruce M Simpson 		if ((lport == 0) ||
8961b7f0384SBruce M Simpson 		    (laddr.s_addr == INADDR_ANY &&
8971b7f0384SBruce M Simpson 		     src.sin_addr.s_addr == INADDR_ANY)) {
898c557ae16SIan Dowse 			error = EINVAL;
899c557ae16SIan Dowse 			goto release;
900c557ae16SIan Dowse 		}
901c557ae16SIan Dowse 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
902b0330ed9SPawel Jakub Dawidek 		    &laddr.s_addr, &lport, td->td_ucred);
903c557ae16SIan Dowse 		if (error)
904c557ae16SIan Dowse 			goto release;
905c557ae16SIan Dowse 	}
906c557ae16SIan Dowse 
9073144b7d3SRobert Watson 	/*
9083144b7d3SRobert Watson 	 * If a UDP socket has been connected, then a local address/port will
9093144b7d3SRobert Watson 	 * have been selected and bound.
9103144b7d3SRobert Watson 	 *
91143cc0bc1SRobert Watson 	 * If a UDP socket has not been connected to, then an explicit
9123144b7d3SRobert Watson 	 * destination address must be used, in which case a local
9133144b7d3SRobert Watson 	 * address/port may not have been selected and bound.
9143144b7d3SRobert Watson 	 */
91543cc0bc1SRobert Watson 	if (sin != NULL) {
916c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
917df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
918df8bae1dSRodney W. Grimes 			error = EISCONN;
919df8bae1dSRodney W. Grimes 			goto release;
920df8bae1dSRodney W. Grimes 		}
9213144b7d3SRobert Watson 
9223144b7d3SRobert Watson 		/*
9233144b7d3SRobert Watson 		 * Jail may rewrite the destination address, so let it do
9243144b7d3SRobert Watson 		 * that before we use it.
9253144b7d3SRobert Watson 		 */
9263144b7d3SRobert Watson 		if (jailed(td->td_ucred))
9273144b7d3SRobert Watson 			prison_remote_ip(td->td_ucred, 0,
9283144b7d3SRobert Watson 			    &sin->sin_addr.s_addr);
9293144b7d3SRobert Watson 
9303144b7d3SRobert Watson 		/*
93143cc0bc1SRobert Watson 		 * If a local address or port hasn't yet been selected, or if
93243cc0bc1SRobert Watson 		 * the destination address needs to be rewritten due to using
93343cc0bc1SRobert Watson 		 * a special INADDR_ constant, invoke in_pcbconnect_setup()
93443cc0bc1SRobert Watson 		 * to do the heavy lifting.  Once a port is selected, we
93543cc0bc1SRobert Watson 		 * commit the binding back to the socket; we also commit the
93643cc0bc1SRobert Watson 		 * binding of the address if in jail.
93743cc0bc1SRobert Watson 		 *
93843cc0bc1SRobert Watson 		 * If we already have a valid binding and we're not
93943cc0bc1SRobert Watson 		 * requesting a destination address rewrite, use a fast path.
9403144b7d3SRobert Watson 		 */
94143cc0bc1SRobert Watson 		if (inp->inp_laddr.s_addr == INADDR_ANY ||
94243cc0bc1SRobert Watson 		    inp->inp_lport == 0 ||
94343cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_ANY ||
94443cc0bc1SRobert Watson 		    sin->sin_addr.s_addr == INADDR_BROADCAST) {
94543cc0bc1SRobert Watson 			INP_INFO_LOCK_ASSERT(&udbinfo);
94643cc0bc1SRobert Watson 			error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
94743cc0bc1SRobert Watson 			    &lport, &faddr.s_addr, &fport, NULL,
94843cc0bc1SRobert Watson 			    td->td_ucred);
94990162a4eSIan Dowse 			if (error)
95090162a4eSIan Dowse 				goto release;
95190162a4eSIan Dowse 
95243cc0bc1SRobert Watson 			/*
95343cc0bc1SRobert Watson 			 * XXXRW: Why not commit the port if the address is
95443cc0bc1SRobert Watson 			 * !INADDR_ANY?
95543cc0bc1SRobert Watson 			 */
95690162a4eSIan Dowse 			/* Commit the local port if newly assigned. */
95790162a4eSIan Dowse 			if (inp->inp_laddr.s_addr == INADDR_ANY &&
95890162a4eSIan Dowse 			    inp->inp_lport == 0) {
959c4d585aeSRobert Watson 				INP_INFO_WLOCK_ASSERT(&udbinfo);
960c4d585aeSRobert Watson 				INP_WLOCK_ASSERT(inp);
9613a1757b9SGleb Smirnoff 				/*
96243cc0bc1SRobert Watson 				 * Remember addr if jailed, to prevent
96343cc0bc1SRobert Watson 				 * rebinding.
9643a1757b9SGleb Smirnoff 				 */
9653a1757b9SGleb Smirnoff 				if (jailed(td->td_ucred))
9663a1757b9SGleb Smirnoff 					inp->inp_laddr = laddr;
96790162a4eSIan Dowse 				inp->inp_lport = lport;
96890162a4eSIan Dowse 				if (in_pcbinshash(inp) != 0) {
96990162a4eSIan Dowse 					inp->inp_lport = 0;
97090162a4eSIan Dowse 					error = EAGAIN;
971df8bae1dSRodney W. Grimes 					goto release;
972df8bae1dSRodney W. Grimes 				}
97390162a4eSIan Dowse 				inp->inp_flags |= INP_ANONPORT;
97490162a4eSIan Dowse 			}
975df8bae1dSRodney W. Grimes 		} else {
97643cc0bc1SRobert Watson 			faddr = sin->sin_addr;
97743cc0bc1SRobert Watson 			fport = sin->sin_port;
97843cc0bc1SRobert Watson 		}
97943cc0bc1SRobert Watson 	} else {
980c4d585aeSRobert Watson 		INP_LOCK_ASSERT(inp);
98190162a4eSIan Dowse 		faddr = inp->inp_faddr;
98290162a4eSIan Dowse 		fport = inp->inp_fport;
98390162a4eSIan Dowse 		if (faddr.s_addr == INADDR_ANY) {
984df8bae1dSRodney W. Grimes 			error = ENOTCONN;
985df8bae1dSRodney W. Grimes 			goto release;
986df8bae1dSRodney W. Grimes 		}
987df8bae1dSRodney W. Grimes 	}
988e6ccd709SRobert Watson 
989df8bae1dSRodney W. Grimes 	/*
990e6ccd709SRobert Watson 	 * Calculate data length and get a mbuf for UDP, IP, and possible
991392e8407SRobert Watson 	 * link-layer headers.  Immediate slide the data pointer back forward
992392e8407SRobert Watson 	 * since we won't use that space at this layer.
993df8bae1dSRodney W. Grimes 	 */
994e6ccd709SRobert Watson 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
995e6ccd709SRobert Watson 	if (m == NULL) {
996df8bae1dSRodney W. Grimes 		error = ENOBUFS;
99749b19bfcSBruce M Simpson 		goto release;
998df8bae1dSRodney W. Grimes 	}
999e6ccd709SRobert Watson 	m->m_data += max_linkhdr;
1000e6ccd709SRobert Watson 	m->m_len -= max_linkhdr;
1001392e8407SRobert Watson 	m->m_pkthdr.len -= max_linkhdr;
1002df8bae1dSRodney W. Grimes 
1003df8bae1dSRodney W. Grimes 	/*
10043329b236SRobert Watson 	 * Fill in mbuf with extended UDP header and addresses and length put
10053329b236SRobert Watson 	 * into network format.
1006df8bae1dSRodney W. Grimes 	 */
1007df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
1008db4f9cc7SJonathan Lemon 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
1009df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
101090162a4eSIan Dowse 	ui->ui_src = laddr;
101190162a4eSIan Dowse 	ui->ui_dst = faddr;
101290162a4eSIan Dowse 	ui->ui_sport = lport;
101390162a4eSIan Dowse 	ui->ui_dport = fport;
1014db4f9cc7SJonathan Lemon 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1015df8bae1dSRodney W. Grimes 
1016b2828ad2SAndre Oppermann 	/*
1017b2828ad2SAndre Oppermann 	 * Set the Don't Fragment bit in the IP header.
1018b2828ad2SAndre Oppermann 	 */
1019b2828ad2SAndre Oppermann 	if (inp->inp_flags & INP_DONTFRAG) {
1020b2828ad2SAndre Oppermann 		struct ip *ip;
10213329b236SRobert Watson 
1022b2828ad2SAndre Oppermann 		ip = (struct ip *)&ui->ui_i;
1023b2828ad2SAndre Oppermann 		ip->ip_off |= IP_DF;
1024b2828ad2SAndre Oppermann 	}
1025b2828ad2SAndre Oppermann 
1026b5d47ff5SJohn-Mark Gurney 	ipflags = 0;
1027b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_DONTROUTE)
1028b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ROUTETOIF;
1029b5d47ff5SJohn-Mark Gurney 	if (inp->inp_socket->so_options & SO_BROADCAST)
1030b5d47ff5SJohn-Mark Gurney 		ipflags |= IP_ALLOWBROADCAST;
10316fbfd582SAndre Oppermann 	if (inp->inp_flags & INP_ONESBCAST)
10328afa2304SBruce M Simpson 		ipflags |= IP_SENDONES;
10338afa2304SBruce M Simpson 
10341175d9d5SRobert Watson #ifdef MAC
10351175d9d5SRobert Watson 	mac_inpcb_create_mbuf(inp, m);
10361175d9d5SRobert Watson #endif
10371175d9d5SRobert Watson 
1038df8bae1dSRodney W. Grimes 	/*
1039db4f9cc7SJonathan Lemon 	 * Set up checksum and output datagram.
1040df8bae1dSRodney W. Grimes 	 */
1041f5514f08SRobert Watson 	if (udp_cksum) {
10426fbfd582SAndre Oppermann 		if (inp->inp_flags & INP_ONESBCAST)
10438a538743SBruce M Simpson 			faddr.s_addr = INADDR_BROADCAST;
10448a538743SBruce M Simpson 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1045db4f9cc7SJonathan Lemon 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
1046db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_flags = CSUM_UDP;
1047db4f9cc7SJonathan Lemon 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
10483329b236SRobert Watson 	} else
1049db4f9cc7SJonathan Lemon 		ui->ui_sum = 0;
1050df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
1051ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
1052ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
1053df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
1054cfa1ca9dSYoshinobu Inoue 
105543cc0bc1SRobert Watson 	if (unlock_udbinfo == 2)
10565c32ea65SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
105743cc0bc1SRobert Watson 	else if (unlock_udbinfo == 1)
105843cc0bc1SRobert Watson 		INP_INFO_RUNLOCK(&udbinfo);
105997d8d152SAndre Oppermann 	error = ip_output(m, inp->inp_options, NULL, ipflags,
10605d846453SSam Leffler 	    inp->inp_moptions, inp);
106143cc0bc1SRobert Watson 	if (unlock_udbinfo == 2)
10628501a69cSRobert Watson 		INP_WUNLOCK(inp);
1063948d0fc9SRobert Watson 	else
1064948d0fc9SRobert Watson 		INP_RUNLOCK(inp);
1065df8bae1dSRodney W. Grimes 	return (error);
1066df8bae1dSRodney W. Grimes 
1067df8bae1dSRodney W. Grimes release:
106843cc0bc1SRobert Watson 	if (unlock_udbinfo == 2) {
1069948d0fc9SRobert Watson 		INP_WUNLOCK(inp);
107043cc0bc1SRobert Watson 		INP_INFO_WUNLOCK(&udbinfo);
107143cc0bc1SRobert Watson 	} else if (unlock_udbinfo == 1) {
107243cc0bc1SRobert Watson 		INP_RUNLOCK(inp);
107343cc0bc1SRobert Watson 		INP_INFO_RUNLOCK(&udbinfo);
1074948d0fc9SRobert Watson 	} else
1075948d0fc9SRobert Watson 		INP_RUNLOCK(inp);
1076df8bae1dSRodney W. Grimes 	m_freem(m);
1077df8bae1dSRodney W. Grimes 	return (error);
1078df8bae1dSRodney W. Grimes }
1079df8bae1dSRodney W. Grimes 
1080ac45e92fSRobert Watson static void
1081d0390e05SGarrett Wollman udp_abort(struct socket *so)
1082df8bae1dSRodney W. Grimes {
1083d0390e05SGarrett Wollman 	struct inpcb *inp;
1084df8bae1dSRodney W. Grimes 
1085d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
108614ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
108714ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
10888501a69cSRobert Watson 	INP_WLOCK(inp);
1089a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1090a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1091a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1092d0390e05SGarrett Wollman 		soisdisconnected(so);
1093a152f8a3SRobert Watson 	}
10948501a69cSRobert Watson 	INP_WUNLOCK(inp);
1095f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1096df8bae1dSRodney W. Grimes }
1097df8bae1dSRodney W. Grimes 
1098d0390e05SGarrett Wollman static int
1099b40ce416SJulian Elischer udp_attach(struct socket *so, int proto, struct thread *td)
1100d0390e05SGarrett Wollman {
1101d0390e05SGarrett Wollman 	struct inpcb *inp;
1102277afaffSRobert Watson 	int error;
1103d0390e05SGarrett Wollman 
1104d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
110514ba8addSRobert Watson 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1106cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
1107f24618aaSRobert Watson 	if (error)
11083329b236SRobert Watson 		return (error);
1109f24618aaSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
1110d915b280SStephan Uphoff 	error = in_pcballoc(so, &udbinfo);
111153b57cd1SSam Leffler 	if (error) {
111253b57cd1SSam Leffler 		INP_INFO_WUNLOCK(&udbinfo);
11133329b236SRobert Watson 		return (error);
111453b57cd1SSam Leffler 	}
1115cfa1ca9dSYoshinobu Inoue 
1116cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
1117f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1118cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
1119cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
11208501a69cSRobert Watson 	INP_WUNLOCK(inp);
11213329b236SRobert Watson 	return (0);
1122df8bae1dSRodney W. Grimes }
1123d0390e05SGarrett Wollman 
1124d0390e05SGarrett Wollman static int
1125b40ce416SJulian Elischer udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1126d0390e05SGarrett Wollman {
1127d0390e05SGarrett Wollman 	struct inpcb *inp;
1128277afaffSRobert Watson 	int error;
1129d0390e05SGarrett Wollman 
1130d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
113114ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
113214ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
11338501a69cSRobert Watson 	INP_WLOCK(inp);
1134b0330ed9SPawel Jakub Dawidek 	error = in_pcbbind(inp, nam, td->td_ucred);
11358501a69cSRobert Watson 	INP_WUNLOCK(inp);
1136f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
11373329b236SRobert Watson 	return (error);
1138d0390e05SGarrett Wollman }
1139d0390e05SGarrett Wollman 
1140a152f8a3SRobert Watson static void
1141a152f8a3SRobert Watson udp_close(struct socket *so)
1142a152f8a3SRobert Watson {
1143a152f8a3SRobert Watson 	struct inpcb *inp;
1144a152f8a3SRobert Watson 
1145a152f8a3SRobert Watson 	inp = sotoinpcb(so);
1146a152f8a3SRobert Watson 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1147a152f8a3SRobert Watson 	INP_INFO_WLOCK(&udbinfo);
11488501a69cSRobert Watson 	INP_WLOCK(inp);
1149a152f8a3SRobert Watson 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1150a152f8a3SRobert Watson 		in_pcbdisconnect(inp);
1151a152f8a3SRobert Watson 		inp->inp_laddr.s_addr = INADDR_ANY;
1152a152f8a3SRobert Watson 		soisdisconnected(so);
1153a152f8a3SRobert Watson 	}
11548501a69cSRobert Watson 	INP_WUNLOCK(inp);
1155a152f8a3SRobert Watson 	INP_INFO_WUNLOCK(&udbinfo);
1156a152f8a3SRobert Watson }
1157a152f8a3SRobert Watson 
1158d0390e05SGarrett Wollman static int
1159b40ce416SJulian Elischer udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1160d0390e05SGarrett Wollman {
1161d0390e05SGarrett Wollman 	struct inpcb *inp;
1162277afaffSRobert Watson 	int error;
116375c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
1164d0390e05SGarrett Wollman 
1165d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
116614ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
116714ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
11688501a69cSRobert Watson 	INP_WLOCK(inp);
1169f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
11708501a69cSRobert Watson 		INP_WUNLOCK(inp);
1171f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
11723329b236SRobert Watson 		return (EISCONN);
1173f76fcf6dSJeffrey Hsu 	}
117475c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
1175812d8653SSam Leffler 	if (jailed(td->td_ucred))
1176a854ed98SJohn Baldwin 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1177b0330ed9SPawel Jakub Dawidek 	error = in_pcbconnect(inp, nam, td->td_ucred);
11784cc20ab1SSeigo Tanimura 	if (error == 0)
1179df8bae1dSRodney W. Grimes 		soisconnected(so);
11808501a69cSRobert Watson 	INP_WUNLOCK(inp);
1181f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
11823329b236SRobert Watson 	return (error);
1183df8bae1dSRodney W. Grimes }
1184d0390e05SGarrett Wollman 
1185bc725eafSRobert Watson static void
1186d0390e05SGarrett Wollman udp_detach(struct socket *so)
1187d0390e05SGarrett Wollman {
1188d0390e05SGarrett Wollman 	struct inpcb *inp;
1189d0390e05SGarrett Wollman 
1190d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
119114ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1192a152f8a3SRobert Watson 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1193a152f8a3SRobert Watson 	    ("udp_detach: not disconnected"));
119414ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
11958501a69cSRobert Watson 	INP_WLOCK(inp);
1196d0390e05SGarrett Wollman 	in_pcbdetach(inp);
119714ba8addSRobert Watson 	in_pcbfree(inp);
1198f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
1199d0390e05SGarrett Wollman }
1200d0390e05SGarrett Wollman 
1201d0390e05SGarrett Wollman static int
1202d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
1203d0390e05SGarrett Wollman {
1204d0390e05SGarrett Wollman 	struct inpcb *inp;
1205d0390e05SGarrett Wollman 
1206d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
120714ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
120814ba8addSRobert Watson 	INP_INFO_WLOCK(&udbinfo);
12098501a69cSRobert Watson 	INP_WLOCK(inp);
1210f76fcf6dSJeffrey Hsu 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
12118501a69cSRobert Watson 		INP_WUNLOCK(inp);
1212f76fcf6dSJeffrey Hsu 		INP_INFO_WUNLOCK(&udbinfo);
12133329b236SRobert Watson 		return (ENOTCONN);
1214f76fcf6dSJeffrey Hsu 	}
1215d0390e05SGarrett Wollman 
1216df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
1217df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
1218d45e4f99SMaxim Konovalov 	SOCK_LOCK(so);
1219d45e4f99SMaxim Konovalov 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1220d45e4f99SMaxim Konovalov 	SOCK_UNLOCK(so);
12218501a69cSRobert Watson 	INP_WUNLOCK(inp);
1222f76fcf6dSJeffrey Hsu 	INP_INFO_WUNLOCK(&udbinfo);
12233329b236SRobert Watson 	return (0);
1224df8bae1dSRodney W. Grimes }
1225df8bae1dSRodney W. Grimes 
1226d0390e05SGarrett Wollman static int
122757bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1228b40ce416SJulian Elischer     struct mbuf *control, struct thread *td)
1229d0390e05SGarrett Wollman {
1230d0390e05SGarrett Wollman 	struct inpcb *inp;
1231d0390e05SGarrett Wollman 
1232d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
123314ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
12343329b236SRobert Watson 	return (udp_output(inp, m, addr, control, td));
1235d0390e05SGarrett Wollman }
1236d0390e05SGarrett Wollman 
123776429de4SYoshinobu Inoue int
1238d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
1239d0390e05SGarrett Wollman {
1240d0390e05SGarrett Wollman 	struct inpcb *inp;
1241d0390e05SGarrett Wollman 
1242d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
124314ba8addSRobert Watson 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
12448501a69cSRobert Watson 	INP_WLOCK(inp);
1245d0390e05SGarrett Wollman 	socantsendmore(so);
12468501a69cSRobert Watson 	INP_WUNLOCK(inp);
12473329b236SRobert Watson 	return (0);
1248d0390e05SGarrett Wollman }
1249d0390e05SGarrett Wollman 
1250d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
1251756d52a1SPoul-Henning Kamp 	.pru_abort =		udp_abort,
1252756d52a1SPoul-Henning Kamp 	.pru_attach =		udp_attach,
1253756d52a1SPoul-Henning Kamp 	.pru_bind =		udp_bind,
1254756d52a1SPoul-Henning Kamp 	.pru_connect =		udp_connect,
1255756d52a1SPoul-Henning Kamp 	.pru_control =		in_control,
1256756d52a1SPoul-Henning Kamp 	.pru_detach =		udp_detach,
1257756d52a1SPoul-Henning Kamp 	.pru_disconnect =	udp_disconnect,
125854d642bbSRobert Watson 	.pru_peeraddr =		in_getpeeraddr,
1259756d52a1SPoul-Henning Kamp 	.pru_send =		udp_send,
12605df3e839SRobert Watson 	.pru_soreceive =	soreceive_dgram,
126159b8854eSRobert Watson 	.pru_sosend =		sosend_dgram,
1262756d52a1SPoul-Henning Kamp 	.pru_shutdown =		udp_shutdown,
126354d642bbSRobert Watson 	.pru_sockaddr =		in_getsockaddr,
1264a152f8a3SRobert Watson 	.pru_sosetlabel =	in_pcbsosetlabel,
1265a152f8a3SRobert Watson 	.pru_close =		udp_close,
1266d0390e05SGarrett Wollman };
1267