xref: /freebsd/sys/netinet/udp_usrreq.c (revision cfa1ca9dfa0ee5bed5cc1cb0b07820701abbb431)
1df8bae1dSRodney W. Grimes /*
26dfab5b1SGarrett Wollman  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
336dfab5b1SGarrett Wollman  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
38cfa1ca9dSYoshinobu Inoue 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
4026f9a767SRodney W. Grimes #include <sys/systm.h>
41b110a8a2SGarrett Wollman #include <sys/kernel.h>
42df8bae1dSRodney W. Grimes #include <sys/malloc.h>
43df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
44cfa1ca9dSYoshinobu Inoue #include <sys/domain.h>
45490d50b6SBrian Feldman #include <sys/proc.h>
46df8bae1dSRodney W. Grimes #include <sys/protosw.h>
47df8bae1dSRodney W. Grimes #include <sys/socket.h>
48df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
49b5e8ce9fSBruce Evans #include <sys/sysctl.h>
50816a3d83SPoul-Henning Kamp #include <sys/syslog.h>
518781d8e9SBruce Evans 
523d4d47f3SGarrett Wollman #include <vm/vm_zone.h>
53df8bae1dSRodney W. Grimes 
54df8bae1dSRodney W. Grimes #include <net/if.h>
55df8bae1dSRodney W. Grimes #include <net/route.h>
56df8bae1dSRodney W. Grimes 
57df8bae1dSRodney W. Grimes #include <netinet/in.h>
58df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
59df8bae1dSRodney W. Grimes #include <netinet/ip.h>
60cfa1ca9dSYoshinobu Inoue #ifdef INET6
61cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
62cfa1ca9dSYoshinobu Inoue #endif
63df8bae1dSRodney W. Grimes #include <netinet/in_pcb.h>
64b5e8ce9fSBruce Evans #include <netinet/in_var.h>
65df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
66cfa1ca9dSYoshinobu Inoue #ifdef INET6
67cfa1ca9dSYoshinobu Inoue #include <netinet6/ip6_var.h>
68cfa1ca9dSYoshinobu Inoue #endif
69df8bae1dSRodney W. Grimes #include <netinet/ip_icmp.h>
7051508de1SMatthew Dillon #include <netinet/icmp_var.h>
71df8bae1dSRodney W. Grimes #include <netinet/udp.h>
72df8bae1dSRodney W. Grimes #include <netinet/udp_var.h>
73df8bae1dSRodney W. Grimes 
74cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
75cfa1ca9dSYoshinobu Inoue #include <netinet6/ipsec.h>
76cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
77cfa1ca9dSYoshinobu Inoue 
78df8bae1dSRodney W. Grimes /*
79df8bae1dSRodney W. Grimes  * UDP protocol implementation.
80df8bae1dSRodney W. Grimes  * Per RFC 768, August, 1980.
81df8bae1dSRodney W. Grimes  */
82df8bae1dSRodney W. Grimes #ifndef	COMPAT_42
830312fbe9SPoul-Henning Kamp static int	udpcksum = 1;
84df8bae1dSRodney W. Grimes #else
850312fbe9SPoul-Henning Kamp static int	udpcksum = 0;		/* XXX */
86df8bae1dSRodney W. Grimes #endif
870312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
880312fbe9SPoul-Henning Kamp 		&udpcksum, 0, "");
89df8bae1dSRodney W. Grimes 
9076429de4SYoshinobu Inoue int	log_in_vain = 0;
91816a3d83SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
923d177f46SBill Fumerola     &log_in_vain, 0, "Log all incoming UDP packets");
93816a3d83SPoul-Henning Kamp 
9416f7f31fSGeoff Rehmet static int	blackhole = 0;
9516f7f31fSGeoff Rehmet SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
9616f7f31fSGeoff Rehmet 	&blackhole, 0, "Do not send port unreachables for refused connects");
9716f7f31fSGeoff Rehmet 
9876429de4SYoshinobu Inoue struct	inpcbhead udb;		/* from udp_var.h */
99cfa1ca9dSYoshinobu Inoue #define	udb6	udb  /* for KAME src sync over BSD*'s */
1007a2aab80SBrian Feldman struct	inpcbinfo udbinfo;
10115bd2b43SDavid Greenman 
10215bd2b43SDavid Greenman #ifndef UDBHASHSIZE
103c3229e05SDavid Greenman #define UDBHASHSIZE 16
10415bd2b43SDavid Greenman #endif
10515bd2b43SDavid Greenman 
10676429de4SYoshinobu Inoue struct	udpstat udpstat;	/* from udp_var.h */
1070312fbe9SPoul-Henning Kamp SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD,
1083d177f46SBill Fumerola     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
109f2ea20e6SGarrett Wollman 
1100312fbe9SPoul-Henning Kamp static struct	sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
111cfa1ca9dSYoshinobu Inoue #ifdef INET6
112cfa1ca9dSYoshinobu Inoue struct udp_in6 {
113cfa1ca9dSYoshinobu Inoue 	struct sockaddr_in6	uin6_sin;
114cfa1ca9dSYoshinobu Inoue 	u_char			uin6_init_done : 1;
115cfa1ca9dSYoshinobu Inoue } udp_in6 = {
116cfa1ca9dSYoshinobu Inoue 	{ sizeof(udp_in6.uin6_sin), AF_INET6 },
117cfa1ca9dSYoshinobu Inoue 	0
118cfa1ca9dSYoshinobu Inoue };
119cfa1ca9dSYoshinobu Inoue struct udp_ip6 {
120cfa1ca9dSYoshinobu Inoue 	struct ip6_hdr		uip6_ip6;
121cfa1ca9dSYoshinobu Inoue 	u_char			uip6_init_done : 1;
122cfa1ca9dSYoshinobu Inoue } udp_ip6;
123cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
124df8bae1dSRodney W. Grimes 
125cfa1ca9dSYoshinobu Inoue static void udp_append __P((struct inpcb *last, struct ip *ip,
126cfa1ca9dSYoshinobu Inoue 			    struct mbuf *n, int off));
127cfa1ca9dSYoshinobu Inoue #ifdef INET6
128cfa1ca9dSYoshinobu Inoue static void ip_2_ip6_hdr __P((struct ip6_hdr *ip6, struct ip *ip));
129cfa1ca9dSYoshinobu Inoue #endif
130cfa1ca9dSYoshinobu Inoue 
131cfa1ca9dSYoshinobu Inoue static int udp_detach __P((struct socket *so));
13257bf258eSGarrett Wollman static	int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *,
133a29f300eSGarrett Wollman 			    struct mbuf *, struct proc *));
134df8bae1dSRodney W. Grimes 
135df8bae1dSRodney W. Grimes void
136df8bae1dSRodney W. Grimes udp_init()
137df8bae1dSRodney W. Grimes {
13815bd2b43SDavid Greenman 	LIST_INIT(&udb);
13915bd2b43SDavid Greenman 	udbinfo.listhead = &udb;
140ddd79a97SDavid Greenman 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
1418781d8e9SBruce Evans 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
1428781d8e9SBruce Evans 					&udbinfo.porthashmask);
14398271db4SGarrett Wollman 	udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
1443d4d47f3SGarrett Wollman 				 ZONE_INTERRUPT, 0);
145df8bae1dSRodney W. Grimes }
146df8bae1dSRodney W. Grimes 
147df8bae1dSRodney W. Grimes void
148cfa1ca9dSYoshinobu Inoue udp_input(m, off, proto)
149df8bae1dSRodney W. Grimes 	register struct mbuf *m;
150cfa1ca9dSYoshinobu Inoue 	int off, proto;
151df8bae1dSRodney W. Grimes {
152cfa1ca9dSYoshinobu Inoue 	int iphlen = off;
153df8bae1dSRodney W. Grimes 	register struct ip *ip;
154df8bae1dSRodney W. Grimes 	register struct udphdr *uh;
155df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
156df8bae1dSRodney W. Grimes 	struct mbuf *opts = 0;
157df8bae1dSRodney W. Grimes 	int len;
158df8bae1dSRodney W. Grimes 	struct ip save_ip;
159cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
160df8bae1dSRodney W. Grimes 
161df8bae1dSRodney W. Grimes 	udpstat.udps_ipackets++;
162df8bae1dSRodney W. Grimes 
163df8bae1dSRodney W. Grimes 	/*
164df8bae1dSRodney W. Grimes 	 * Strip IP options, if any; should skip this,
165df8bae1dSRodney W. Grimes 	 * make available to user, and use on returned packets,
166df8bae1dSRodney W. Grimes 	 * but we don't yet have a way to check the checksum
167df8bae1dSRodney W. Grimes 	 * with options still present.
168df8bae1dSRodney W. Grimes 	 */
169df8bae1dSRodney W. Grimes 	if (iphlen > sizeof (struct ip)) {
170df8bae1dSRodney W. Grimes 		ip_stripoptions(m, (struct mbuf *)0);
171df8bae1dSRodney W. Grimes 		iphlen = sizeof(struct ip);
172df8bae1dSRodney W. Grimes 	}
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes 	/*
175df8bae1dSRodney W. Grimes 	 * Get IP and UDP header together in first mbuf.
176df8bae1dSRodney W. Grimes 	 */
177df8bae1dSRodney W. Grimes 	ip = mtod(m, struct ip *);
178df8bae1dSRodney W. Grimes 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
179df8bae1dSRodney W. Grimes 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
180df8bae1dSRodney W. Grimes 			udpstat.udps_hdrops++;
181df8bae1dSRodney W. Grimes 			return;
182df8bae1dSRodney W. Grimes 		}
183df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
184df8bae1dSRodney W. Grimes 	}
185df8bae1dSRodney W. Grimes 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
186df8bae1dSRodney W. Grimes 
187df8bae1dSRodney W. Grimes 	/*
188df8bae1dSRodney W. Grimes 	 * Make mbuf data length reflect UDP length.
189df8bae1dSRodney W. Grimes 	 * If not enough data to reflect UDP length, drop.
190df8bae1dSRodney W. Grimes 	 */
191df8bae1dSRodney W. Grimes 	len = ntohs((u_short)uh->uh_ulen);
192df8bae1dSRodney W. Grimes 	if (ip->ip_len != len) {
1937eb7a449SAndras Olah 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
194df8bae1dSRodney W. Grimes 			udpstat.udps_badlen++;
195df8bae1dSRodney W. Grimes 			goto bad;
196df8bae1dSRodney W. Grimes 		}
197df8bae1dSRodney W. Grimes 		m_adj(m, len - ip->ip_len);
198df8bae1dSRodney W. Grimes 		/* ip->ip_len = len; */
199df8bae1dSRodney W. Grimes 	}
200df8bae1dSRodney W. Grimes 	/*
201df8bae1dSRodney W. Grimes 	 * Save a copy of the IP header in case we want restore it
202df8bae1dSRodney W. Grimes 	 * for sending an ICMP error message in response.
203df8bae1dSRodney W. Grimes 	 */
204df8bae1dSRodney W. Grimes 	save_ip = *ip;
205df8bae1dSRodney W. Grimes 
206df8bae1dSRodney W. Grimes 	/*
207df8bae1dSRodney W. Grimes 	 * Checksum extended UDP header and data.
208df8bae1dSRodney W. Grimes 	 */
2096dfab5b1SGarrett Wollman 	if (uh->uh_sum) {
2106effc713SDoug Rabson 		bzero(((struct ipovly *)ip)->ih_x1, 9);
211df8bae1dSRodney W. Grimes 		((struct ipovly *)ip)->ih_len = uh->uh_ulen;
212623ae52eSPoul-Henning Kamp 		uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
213623ae52eSPoul-Henning Kamp 		if (uh->uh_sum) {
214df8bae1dSRodney W. Grimes 			udpstat.udps_badsum++;
215df8bae1dSRodney W. Grimes 			m_freem(m);
216df8bae1dSRodney W. Grimes 			return;
217df8bae1dSRodney W. Grimes 		}
218df8bae1dSRodney W. Grimes 	}
219df8bae1dSRodney W. Grimes 
220df8bae1dSRodney W. Grimes 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
221df8bae1dSRodney W. Grimes 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
22282c23ebaSBill Fenner 		struct inpcb *last;
223df8bae1dSRodney W. Grimes 		/*
224df8bae1dSRodney W. Grimes 		 * Deliver a multicast or broadcast datagram to *all* sockets
225df8bae1dSRodney W. Grimes 		 * for which the local and remote addresses and ports match
226df8bae1dSRodney W. Grimes 		 * those of the incoming datagram.  This allows more than
227df8bae1dSRodney W. Grimes 		 * one process to receive multi/broadcasts on the same port.
228df8bae1dSRodney W. Grimes 		 * (This really ought to be done for unicast datagrams as
229df8bae1dSRodney W. Grimes 		 * well, but that would cause problems with existing
230df8bae1dSRodney W. Grimes 		 * applications that open both address-specific sockets and
231df8bae1dSRodney W. Grimes 		 * a wildcard socket listening to the same port -- they would
232df8bae1dSRodney W. Grimes 		 * end up receiving duplicates of every unicast datagram.
233df8bae1dSRodney W. Grimes 		 * Those applications open the multiple sockets to overcome an
234df8bae1dSRodney W. Grimes 		 * inadequacy of the UDP socket interface, but for backwards
235df8bae1dSRodney W. Grimes 		 * compatibility we avoid the problem here rather than
236df8bae1dSRodney W. Grimes 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
237df8bae1dSRodney W. Grimes 		 */
238df8bae1dSRodney W. Grimes 
239df8bae1dSRodney W. Grimes 		/*
240df8bae1dSRodney W. Grimes 		 * Construct sockaddr format source address.
241df8bae1dSRodney W. Grimes 		 */
242df8bae1dSRodney W. Grimes 		udp_in.sin_port = uh->uh_sport;
243df8bae1dSRodney W. Grimes 		udp_in.sin_addr = ip->ip_src;
244df8bae1dSRodney W. Grimes 		/*
245df8bae1dSRodney W. Grimes 		 * Locate pcb(s) for datagram.
246df8bae1dSRodney W. Grimes 		 * (Algorithm copied from raw_intr().)
247df8bae1dSRodney W. Grimes 		 */
248df8bae1dSRodney W. Grimes 		last = NULL;
249cfa1ca9dSYoshinobu Inoue #ifdef INET6
250cfa1ca9dSYoshinobu Inoue 		udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
251cfa1ca9dSYoshinobu Inoue #endif
252cfa1ca9dSYoshinobu Inoue 		LIST_FOREACH(inp, &udb, inp_list) {
253cfa1ca9dSYoshinobu Inoue #ifdef INET6
254cfa1ca9dSYoshinobu Inoue 			if ((inp->inp_vflag & INP_IPV4) == NULL)
255cfa1ca9dSYoshinobu Inoue 				continue;
256cfa1ca9dSYoshinobu Inoue #endif
257df8bae1dSRodney W. Grimes 			if (inp->inp_lport != uh->uh_dport)
258df8bae1dSRodney W. Grimes 				continue;
259df8bae1dSRodney W. Grimes 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
260df8bae1dSRodney W. Grimes 				if (inp->inp_laddr.s_addr !=
261df8bae1dSRodney W. Grimes 				    ip->ip_dst.s_addr)
262df8bae1dSRodney W. Grimes 					continue;
263df8bae1dSRodney W. Grimes 			}
264df8bae1dSRodney W. Grimes 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
265df8bae1dSRodney W. Grimes 				if (inp->inp_faddr.s_addr !=
266df8bae1dSRodney W. Grimes 				    ip->ip_src.s_addr ||
267df8bae1dSRodney W. Grimes 				    inp->inp_fport != uh->uh_sport)
268df8bae1dSRodney W. Grimes 					continue;
269df8bae1dSRodney W. Grimes 			}
270df8bae1dSRodney W. Grimes 
271df8bae1dSRodney W. Grimes 			if (last != NULL) {
272df8bae1dSRodney W. Grimes 				struct mbuf *n;
273df8bae1dSRodney W. Grimes 
274cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
275cfa1ca9dSYoshinobu Inoue 				/* check AH/ESP integrity. */
276cfa1ca9dSYoshinobu Inoue 				if (ipsec4_in_reject_so(m, last->inp_socket))
277cfa1ca9dSYoshinobu Inoue 					ipsecstat.in_polvio++;
278cfa1ca9dSYoshinobu Inoue 					/* do not inject data to pcb */
279cfa1ca9dSYoshinobu Inoue 				else
280cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
281cfa1ca9dSYoshinobu Inoue 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
282cfa1ca9dSYoshinobu Inoue 					udp_append(last, ip, n,
283cfa1ca9dSYoshinobu Inoue 						   iphlen +
284cfa1ca9dSYoshinobu Inoue 						   sizeof(struct udphdr));
285df8bae1dSRodney W. Grimes 			}
28682c23ebaSBill Fenner 			last = inp;
287df8bae1dSRodney W. Grimes 			/*
288df8bae1dSRodney W. Grimes 			 * Don't look for additional matches if this one does
289df8bae1dSRodney W. Grimes 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
290df8bae1dSRodney W. Grimes 			 * socket options set.  This heuristic avoids searching
291df8bae1dSRodney W. Grimes 			 * through all pcbs in the common case of a non-shared
292df8bae1dSRodney W. Grimes 			 * port.  It * assumes that an application will never
293df8bae1dSRodney W. Grimes 			 * clear these options after setting them.
294df8bae1dSRodney W. Grimes 			 */
295694ad0a9SSteve Price 			if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
296df8bae1dSRodney W. Grimes 				break;
297df8bae1dSRodney W. Grimes 		}
298df8bae1dSRodney W. Grimes 
299df8bae1dSRodney W. Grimes 		if (last == NULL) {
300df8bae1dSRodney W. Grimes 			/*
301df8bae1dSRodney W. Grimes 			 * No matching pcb found; discard datagram.
302df8bae1dSRodney W. Grimes 			 * (No need to send an ICMP Port Unreachable
303df8bae1dSRodney W. Grimes 			 * for a broadcast or multicast datgram.)
304df8bae1dSRodney W. Grimes 			 */
305df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
306df8bae1dSRodney W. Grimes 			goto bad;
307df8bae1dSRodney W. Grimes 		}
308cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
309cfa1ca9dSYoshinobu Inoue 		/* check AH/ESP integrity. */
310cfa1ca9dSYoshinobu Inoue 		if (ipsec4_in_reject_so(m, last->inp_socket)) {
311cfa1ca9dSYoshinobu Inoue 			ipsecstat.in_polvio++;
312df8bae1dSRodney W. Grimes 			goto bad;
313df8bae1dSRodney W. Grimes 		}
314cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
315cfa1ca9dSYoshinobu Inoue 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
316df8bae1dSRodney W. Grimes 		return;
317df8bae1dSRodney W. Grimes 	}
318df8bae1dSRodney W. Grimes 	/*
3196d6a026bSDavid Greenman 	 * Locate pcb for datagram.
320df8bae1dSRodney W. Grimes 	 */
321c3229e05SDavid Greenman 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
322cfa1ca9dSYoshinobu Inoue 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
32315bd2b43SDavid Greenman 	if (inp == NULL) {
32475cfc95fSAndrey A. Chernov 		if (log_in_vain) {
325df5c0b8aSBill Fenner 			char buf[4*sizeof "123"];
32675cfc95fSAndrey A. Chernov 
32775cfc95fSAndrey A. Chernov 			strcpy(buf, inet_ntoa(ip->ip_dst));
328592071e8SBruce Evans 			log(LOG_INFO,
329592071e8SBruce Evans 			    "Connection attempt to UDP %s:%d from %s:%d\n",
330592071e8SBruce Evans 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
331592071e8SBruce Evans 			    ntohs(uh->uh_sport));
33275cfc95fSAndrey A. Chernov 		}
333df8bae1dSRodney W. Grimes 		udpstat.udps_noport++;
334df8bae1dSRodney W. Grimes 		if (m->m_flags & (M_BCAST | M_MCAST)) {
335df8bae1dSRodney W. Grimes 			udpstat.udps_noportbcast++;
336df8bae1dSRodney W. Grimes 			goto bad;
337df8bae1dSRodney W. Grimes 		}
338df8bae1dSRodney W. Grimes 		*ip = save_ip;
33951508de1SMatthew Dillon #ifdef ICMP_BANDLIM
34051508de1SMatthew Dillon 		if (badport_bandlim(0) < 0)
34151508de1SMatthew Dillon 			goto bad;
34251508de1SMatthew Dillon #endif
34316f7f31fSGeoff Rehmet 		if (!blackhole)
344df8bae1dSRodney W. Grimes 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
34512b4fd06SPoul-Henning Kamp 		else
34612b4fd06SPoul-Henning Kamp 			goto bad;
347df8bae1dSRodney W. Grimes 		return;
348df8bae1dSRodney W. Grimes 	}
349cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
350cfa1ca9dSYoshinobu Inoue 	if (ipsec4_in_reject_so(m, inp->inp_socket)) {
351cfa1ca9dSYoshinobu Inoue 		ipsecstat.in_polvio++;
352cfa1ca9dSYoshinobu Inoue 		goto bad;
353cfa1ca9dSYoshinobu Inoue 	}
354cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
355df8bae1dSRodney W. Grimes 
356df8bae1dSRodney W. Grimes 	/*
357df8bae1dSRodney W. Grimes 	 * Construct sockaddr format source address.
358df8bae1dSRodney W. Grimes 	 * Stuff source address and datagram in user buffer.
359df8bae1dSRodney W. Grimes 	 */
360df8bae1dSRodney W. Grimes 	udp_in.sin_port = uh->uh_sport;
361df8bae1dSRodney W. Grimes 	udp_in.sin_addr = ip->ip_src;
36282dab6ceSGarrett Wollman 	if (inp->inp_flags & INP_CONTROLOPTS
363cfa1ca9dSYoshinobu Inoue 	    || inp->inp_socket->so_options & SO_TIMESTAMP) {
364cfa1ca9dSYoshinobu Inoue #ifdef INET6
365cfa1ca9dSYoshinobu Inoue 		if (inp->inp_vflag & INP_IPV6) {
366cfa1ca9dSYoshinobu Inoue 			int savedflags;
367cfa1ca9dSYoshinobu Inoue 
368cfa1ca9dSYoshinobu Inoue 			ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
369cfa1ca9dSYoshinobu Inoue 			savedflags = inp->inp_flags;
370cfa1ca9dSYoshinobu Inoue 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
371cfa1ca9dSYoshinobu Inoue 			ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
372cfa1ca9dSYoshinobu Inoue 			inp->inp_flags = savedflags;
373cfa1ca9dSYoshinobu Inoue 		} else
374cfa1ca9dSYoshinobu Inoue #endif
37582c23ebaSBill Fenner 		ip_savecontrol(inp, &opts, ip, m);
376cfa1ca9dSYoshinobu Inoue 	}
377df8bae1dSRodney W. Grimes 	iphlen += sizeof(struct udphdr);
378cfa1ca9dSYoshinobu Inoue 	m_adj(m, iphlen);
379cfa1ca9dSYoshinobu Inoue #ifdef INET6
380cfa1ca9dSYoshinobu Inoue 	if (inp->inp_vflag & INP_IPV6) {
381cfa1ca9dSYoshinobu Inoue 		in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
382cfa1ca9dSYoshinobu Inoue 		append_sa = (struct sockaddr *)&udp_in6;
383cfa1ca9dSYoshinobu Inoue 	} else
384cfa1ca9dSYoshinobu Inoue #endif
385cfa1ca9dSYoshinobu Inoue 	append_sa = (struct sockaddr *)&udp_in;
386cfa1ca9dSYoshinobu Inoue 	if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
387df8bae1dSRodney W. Grimes 		udpstat.udps_fullsock++;
388df8bae1dSRodney W. Grimes 		goto bad;
389df8bae1dSRodney W. Grimes 	}
390df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
391df8bae1dSRodney W. Grimes 	return;
392df8bae1dSRodney W. Grimes bad:
393df8bae1dSRodney W. Grimes 	m_freem(m);
394df8bae1dSRodney W. Grimes 	if (opts)
395df8bae1dSRodney W. Grimes 		m_freem(opts);
396cfa1ca9dSYoshinobu Inoue 	return;
397cfa1ca9dSYoshinobu Inoue }
398cfa1ca9dSYoshinobu Inoue 
399cfa1ca9dSYoshinobu Inoue #if defined(INET6)
400cfa1ca9dSYoshinobu Inoue static void
401cfa1ca9dSYoshinobu Inoue ip_2_ip6_hdr(ip6, ip)
402cfa1ca9dSYoshinobu Inoue 	struct ip6_hdr *ip6;
403cfa1ca9dSYoshinobu Inoue 	struct ip *ip;
404cfa1ca9dSYoshinobu Inoue {
405cfa1ca9dSYoshinobu Inoue 	bzero(ip6, sizeof(*ip6));
406cfa1ca9dSYoshinobu Inoue 
407cfa1ca9dSYoshinobu Inoue 	ip6->ip6_vfc = IPV6_VERSION;
408cfa1ca9dSYoshinobu Inoue 	ip6->ip6_plen = ip->ip_len;
409cfa1ca9dSYoshinobu Inoue 	ip6->ip6_nxt = ip->ip_p;
410cfa1ca9dSYoshinobu Inoue 	ip6->ip6_hlim = ip->ip_ttl;
411cfa1ca9dSYoshinobu Inoue 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
412cfa1ca9dSYoshinobu Inoue 		IPV6_ADDR_INT32_SMP;
413cfa1ca9dSYoshinobu Inoue 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
414cfa1ca9dSYoshinobu Inoue 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
415cfa1ca9dSYoshinobu Inoue }
416cfa1ca9dSYoshinobu Inoue #endif
417cfa1ca9dSYoshinobu Inoue 
418cfa1ca9dSYoshinobu Inoue /*
419cfa1ca9dSYoshinobu Inoue  * subroutine of udp_input(), mainly for source code readability.
420cfa1ca9dSYoshinobu Inoue  * caller must properly init udp_ip6 and udp_in6 beforehand.
421cfa1ca9dSYoshinobu Inoue  */
422cfa1ca9dSYoshinobu Inoue static void
423cfa1ca9dSYoshinobu Inoue udp_append(last, ip, n, off)
424cfa1ca9dSYoshinobu Inoue 	struct inpcb *last;
425cfa1ca9dSYoshinobu Inoue 	struct ip *ip;
426cfa1ca9dSYoshinobu Inoue 	struct mbuf *n;
427cfa1ca9dSYoshinobu Inoue 	int off;
428cfa1ca9dSYoshinobu Inoue {
429cfa1ca9dSYoshinobu Inoue 	struct sockaddr *append_sa;
430cfa1ca9dSYoshinobu Inoue 	struct mbuf *opts = 0;
431cfa1ca9dSYoshinobu Inoue 
432cfa1ca9dSYoshinobu Inoue 	if (last->inp_flags & INP_CONTROLOPTS ||
433cfa1ca9dSYoshinobu Inoue 	    last->inp_socket->so_options & SO_TIMESTAMP) {
434cfa1ca9dSYoshinobu Inoue #ifdef INET6
435cfa1ca9dSYoshinobu Inoue 		if (last->inp_vflag & INP_IPV6) {
436cfa1ca9dSYoshinobu Inoue 			int savedflags;
437cfa1ca9dSYoshinobu Inoue 
438cfa1ca9dSYoshinobu Inoue 			if (udp_ip6.uip6_init_done == 0) {
439cfa1ca9dSYoshinobu Inoue 				ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
440cfa1ca9dSYoshinobu Inoue 				udp_ip6.uip6_init_done = 1;
441cfa1ca9dSYoshinobu Inoue 			}
442cfa1ca9dSYoshinobu Inoue 			savedflags = last->inp_flags;
443cfa1ca9dSYoshinobu Inoue 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
444cfa1ca9dSYoshinobu Inoue 			ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
445cfa1ca9dSYoshinobu Inoue 			last->inp_flags = savedflags;
446cfa1ca9dSYoshinobu Inoue 		} else
447cfa1ca9dSYoshinobu Inoue #endif
448cfa1ca9dSYoshinobu Inoue 		ip_savecontrol(last, &opts, ip, n);
449cfa1ca9dSYoshinobu Inoue 	}
450cfa1ca9dSYoshinobu Inoue #ifdef INET6
451cfa1ca9dSYoshinobu Inoue 	if (last->inp_vflag & INP_IPV6) {
452cfa1ca9dSYoshinobu Inoue 		if (udp_in6.uin6_init_done == 0) {
453cfa1ca9dSYoshinobu Inoue 			in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
454cfa1ca9dSYoshinobu Inoue 			udp_in6.uin6_init_done = 1;
455cfa1ca9dSYoshinobu Inoue 		}
456cfa1ca9dSYoshinobu Inoue 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
457cfa1ca9dSYoshinobu Inoue 	} else
458cfa1ca9dSYoshinobu Inoue #endif
459cfa1ca9dSYoshinobu Inoue 	append_sa = (struct sockaddr *)&udp_in;
460cfa1ca9dSYoshinobu Inoue 	m_adj(n, off);
461cfa1ca9dSYoshinobu Inoue 	if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
462cfa1ca9dSYoshinobu Inoue 		m_freem(n);
463cfa1ca9dSYoshinobu Inoue 		if (opts)
464cfa1ca9dSYoshinobu Inoue 			m_freem(opts);
465cfa1ca9dSYoshinobu Inoue 		udpstat.udps_fullsock++;
466cfa1ca9dSYoshinobu Inoue 	} else
467cfa1ca9dSYoshinobu Inoue 		sorwakeup(last->inp_socket);
468df8bae1dSRodney W. Grimes }
469df8bae1dSRodney W. Grimes 
470df8bae1dSRodney W. Grimes /*
471df8bae1dSRodney W. Grimes  * Notify a udp user of an asynchronous error;
472df8bae1dSRodney W. Grimes  * just wake up so that he can collect error status.
473df8bae1dSRodney W. Grimes  */
47476429de4SYoshinobu Inoue void
475df8bae1dSRodney W. Grimes udp_notify(inp, errno)
476df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
477df8bae1dSRodney W. Grimes 	int errno;
478df8bae1dSRodney W. Grimes {
479df8bae1dSRodney W. Grimes 	inp->inp_socket->so_error = errno;
480df8bae1dSRodney W. Grimes 	sorwakeup(inp->inp_socket);
481df8bae1dSRodney W. Grimes 	sowwakeup(inp->inp_socket);
482df8bae1dSRodney W. Grimes }
483df8bae1dSRodney W. Grimes 
484df8bae1dSRodney W. Grimes void
485b62d102cSBruce Evans udp_ctlinput(cmd, sa, vip)
486df8bae1dSRodney W. Grimes 	int cmd;
487df8bae1dSRodney W. Grimes 	struct sockaddr *sa;
488b62d102cSBruce Evans 	void *vip;
489df8bae1dSRodney W. Grimes {
490b62d102cSBruce Evans 	register struct ip *ip = vip;
491df8bae1dSRodney W. Grimes 	register struct udphdr *uh;
492df8bae1dSRodney W. Grimes 
493df8bae1dSRodney W. Grimes 	if (!PRC_IS_REDIRECT(cmd) &&
494df8bae1dSRodney W. Grimes 	    ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0))
495df8bae1dSRodney W. Grimes 		return;
496df8bae1dSRodney W. Grimes 	if (ip) {
497df8bae1dSRodney W. Grimes 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
498df8bae1dSRodney W. Grimes 		in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
499df8bae1dSRodney W. Grimes 			cmd, udp_notify);
500df8bae1dSRodney W. Grimes 	} else
501df8bae1dSRodney W. Grimes 		in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
502df8bae1dSRodney W. Grimes }
503df8bae1dSRodney W. Grimes 
5040312fbe9SPoul-Henning Kamp static int
50598271db4SGarrett Wollman udp_pcblist SYSCTL_HANDLER_ARGS
50698271db4SGarrett Wollman {
50798271db4SGarrett Wollman 	int error, i, n, s;
50898271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
50998271db4SGarrett Wollman 	inp_gen_t gencnt;
51098271db4SGarrett Wollman 	struct xinpgen xig;
51198271db4SGarrett Wollman 
51298271db4SGarrett Wollman 	/*
51398271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
51498271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
51598271db4SGarrett Wollman 	 */
51698271db4SGarrett Wollman 	if (req->oldptr == 0) {
51798271db4SGarrett Wollman 		n = udbinfo.ipi_count;
51898271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
51998271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
52098271db4SGarrett Wollman 		return 0;
52198271db4SGarrett Wollman 	}
52298271db4SGarrett Wollman 
52398271db4SGarrett Wollman 	if (req->newptr != 0)
52498271db4SGarrett Wollman 		return EPERM;
52598271db4SGarrett Wollman 
52698271db4SGarrett Wollman 	/*
52798271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
52898271db4SGarrett Wollman 	 */
52998271db4SGarrett Wollman 	s = splnet();
53098271db4SGarrett Wollman 	gencnt = udbinfo.ipi_gencnt;
53198271db4SGarrett Wollman 	n = udbinfo.ipi_count;
53298271db4SGarrett Wollman 	splx(s);
53398271db4SGarrett Wollman 
53498271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
53598271db4SGarrett Wollman 	xig.xig_count = n;
53698271db4SGarrett Wollman 	xig.xig_gen = gencnt;
53798271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
53898271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
53998271db4SGarrett Wollman 	if (error)
54098271db4SGarrett Wollman 		return error;
54198271db4SGarrett Wollman 
54298271db4SGarrett Wollman 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
54398271db4SGarrett Wollman 	if (inp_list == 0)
54498271db4SGarrett Wollman 		return ENOMEM;
54598271db4SGarrett Wollman 
54698271db4SGarrett Wollman 	s = splnet();
54798271db4SGarrett Wollman 	for (inp = udbinfo.listhead->lh_first, i = 0; inp && i < n;
54898271db4SGarrett Wollman 	     inp = inp->inp_list.le_next) {
54975c13541SPoul-Henning Kamp 		if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
55098271db4SGarrett Wollman 			inp_list[i++] = inp;
55198271db4SGarrett Wollman 	}
55298271db4SGarrett Wollman 	splx(s);
55398271db4SGarrett Wollman 	n = i;
55498271db4SGarrett Wollman 
55598271db4SGarrett Wollman 	error = 0;
55698271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
55798271db4SGarrett Wollman 		inp = inp_list[i];
55898271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
55998271db4SGarrett Wollman 			struct xinpcb xi;
56098271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
56198271db4SGarrett Wollman 			/* XXX should avoid extra copy */
56298271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
56398271db4SGarrett Wollman 			if (inp->inp_socket)
56498271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
56598271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
56698271db4SGarrett Wollman 		}
56798271db4SGarrett Wollman 	}
56898271db4SGarrett Wollman 	if (!error) {
56998271db4SGarrett Wollman 		/*
57098271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
57198271db4SGarrett Wollman 		 * If the generation differs from what we told
57298271db4SGarrett Wollman 		 * her before, she knows that something happened
57398271db4SGarrett Wollman 		 * while we were processing this request, and it
57498271db4SGarrett Wollman 		 * might be necessary to retry.
57598271db4SGarrett Wollman 		 */
57698271db4SGarrett Wollman 		s = splnet();
57798271db4SGarrett Wollman 		xig.xig_gen = udbinfo.ipi_gencnt;
57898271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
57998271db4SGarrett Wollman 		xig.xig_count = udbinfo.ipi_count;
58098271db4SGarrett Wollman 		splx(s);
58198271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
58298271db4SGarrett Wollman 	}
58398271db4SGarrett Wollman 	free(inp_list, M_TEMP);
58498271db4SGarrett Wollman 	return error;
58598271db4SGarrett Wollman }
58698271db4SGarrett Wollman 
58798271db4SGarrett Wollman SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
58898271db4SGarrett Wollman 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
58998271db4SGarrett Wollman 
59098271db4SGarrett Wollman static int
591490d50b6SBrian Feldman udp_getcred SYSCTL_HANDLER_ARGS
592490d50b6SBrian Feldman {
593490d50b6SBrian Feldman 	struct sockaddr_in addrs[2];
594490d50b6SBrian Feldman 	struct inpcb *inp;
595490d50b6SBrian Feldman 	int error, s;
596490d50b6SBrian Feldman 
597490d50b6SBrian Feldman 	error = suser(req->p);
598490d50b6SBrian Feldman 	if (error)
599490d50b6SBrian Feldman 		return (error);
600490d50b6SBrian Feldman 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
601490d50b6SBrian Feldman 	if (error)
602490d50b6SBrian Feldman 		return (error);
603490d50b6SBrian Feldman 	s = splnet();
604490d50b6SBrian Feldman 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
605cfa1ca9dSYoshinobu Inoue 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
6062f9a2132SBrian Feldman 	if (inp == NULL || inp->inp_socket == NULL) {
607490d50b6SBrian Feldman 		error = ENOENT;
608490d50b6SBrian Feldman 		goto out;
609490d50b6SBrian Feldman 	}
6102f9a2132SBrian Feldman 	error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
611490d50b6SBrian Feldman out:
612490d50b6SBrian Feldman 	splx(s);
613490d50b6SBrian Feldman 	return (error);
614490d50b6SBrian Feldman }
615490d50b6SBrian Feldman 
616490d50b6SBrian Feldman SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
617490d50b6SBrian Feldman     0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
618490d50b6SBrian Feldman 
619490d50b6SBrian Feldman static int
620a29f300eSGarrett Wollman udp_output(inp, m, addr, control, p)
621df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
622df8bae1dSRodney W. Grimes 	register struct mbuf *m;
62357bf258eSGarrett Wollman 	struct sockaddr *addr;
62457bf258eSGarrett Wollman 	struct mbuf *control;
625a29f300eSGarrett Wollman 	struct proc *p;
626df8bae1dSRodney W. Grimes {
627df8bae1dSRodney W. Grimes 	register struct udpiphdr *ui;
628df8bae1dSRodney W. Grimes 	register int len = m->m_pkthdr.len;
629df8bae1dSRodney W. Grimes 	struct in_addr laddr;
63075c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
63126f9a767SRodney W. Grimes 	int s = 0, error = 0;
632df8bae1dSRodney W. Grimes 
633df8bae1dSRodney W. Grimes 	if (control)
634df8bae1dSRodney W. Grimes 		m_freem(control);		/* XXX */
635df8bae1dSRodney W. Grimes 
636430d30d8SBill Fenner 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
637430d30d8SBill Fenner 		error = EMSGSIZE;
638430d30d8SBill Fenner 		goto release;
639430d30d8SBill Fenner 	}
640430d30d8SBill Fenner 
641df8bae1dSRodney W. Grimes 	if (addr) {
64275c13541SPoul-Henning Kamp 		sin = (struct sockaddr_in *)addr;
64375c13541SPoul-Henning Kamp 		prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
644df8bae1dSRodney W. Grimes 		laddr = inp->inp_laddr;
645df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
646df8bae1dSRodney W. Grimes 			error = EISCONN;
647df8bae1dSRodney W. Grimes 			goto release;
648df8bae1dSRodney W. Grimes 		}
649df8bae1dSRodney W. Grimes 		/*
650df8bae1dSRodney W. Grimes 		 * Must block input while temporarily connected.
651df8bae1dSRodney W. Grimes 		 */
652df8bae1dSRodney W. Grimes 		s = splnet();
653a29f300eSGarrett Wollman 		error = in_pcbconnect(inp, addr, p);
654df8bae1dSRodney W. Grimes 		if (error) {
655df8bae1dSRodney W. Grimes 			splx(s);
656df8bae1dSRodney W. Grimes 			goto release;
657df8bae1dSRodney W. Grimes 		}
658df8bae1dSRodney W. Grimes 	} else {
659df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr == INADDR_ANY) {
660df8bae1dSRodney W. Grimes 			error = ENOTCONN;
661df8bae1dSRodney W. Grimes 			goto release;
662df8bae1dSRodney W. Grimes 		}
663df8bae1dSRodney W. Grimes 	}
664df8bae1dSRodney W. Grimes 	/*
665df8bae1dSRodney W. Grimes 	 * Calculate data length and get a mbuf
666df8bae1dSRodney W. Grimes 	 * for UDP and IP headers.
667df8bae1dSRodney W. Grimes 	 */
668df8bae1dSRodney W. Grimes 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
669df8bae1dSRodney W. Grimes 	if (m == 0) {
670df8bae1dSRodney W. Grimes 		error = ENOBUFS;
6711c09f774SGarrett Wollman 		if (addr)
6721c09f774SGarrett Wollman 			splx(s);
673df8bae1dSRodney W. Grimes 		goto release;
674df8bae1dSRodney W. Grimes 	}
675df8bae1dSRodney W. Grimes 
676df8bae1dSRodney W. Grimes 	/*
677df8bae1dSRodney W. Grimes 	 * Fill in mbuf with extended UDP header
678df8bae1dSRodney W. Grimes 	 * and addresses and length put into network format.
679df8bae1dSRodney W. Grimes 	 */
680df8bae1dSRodney W. Grimes 	ui = mtod(m, struct udpiphdr *);
6816effc713SDoug Rabson 	bzero(ui->ui_x1, sizeof(ui->ui_x1));
682df8bae1dSRodney W. Grimes 	ui->ui_pr = IPPROTO_UDP;
683df8bae1dSRodney W. Grimes 	ui->ui_len = htons((u_short)len + sizeof (struct udphdr));
684df8bae1dSRodney W. Grimes 	ui->ui_src = inp->inp_laddr;
685df8bae1dSRodney W. Grimes 	ui->ui_dst = inp->inp_faddr;
686df8bae1dSRodney W. Grimes 	ui->ui_sport = inp->inp_lport;
687df8bae1dSRodney W. Grimes 	ui->ui_dport = inp->inp_fport;
688df8bae1dSRodney W. Grimes 	ui->ui_ulen = ui->ui_len;
689df8bae1dSRodney W. Grimes 
690df8bae1dSRodney W. Grimes 	/*
691df8bae1dSRodney W. Grimes 	 * Stuff checksum and output datagram.
692df8bae1dSRodney W. Grimes 	 */
693df8bae1dSRodney W. Grimes 	ui->ui_sum = 0;
694df8bae1dSRodney W. Grimes 	if (udpcksum) {
695df8bae1dSRodney W. Grimes 	    if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0)
696df8bae1dSRodney W. Grimes 		ui->ui_sum = 0xffff;
697df8bae1dSRodney W. Grimes 	}
698df8bae1dSRodney W. Grimes 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
699ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
700ca98b82cSDavid Greenman 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
701df8bae1dSRodney W. Grimes 	udpstat.udps_opackets++;
702cfa1ca9dSYoshinobu Inoue 
703cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
704cfa1ca9dSYoshinobu Inoue 	m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket;
705cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
706cfa1ca9dSYoshinobu Inoue 
707df8bae1dSRodney W. Grimes 	error = ip_output(m, inp->inp_options, &inp->inp_route,
708df8bae1dSRodney W. Grimes 	    inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
709df8bae1dSRodney W. Grimes 	    inp->inp_moptions);
710df8bae1dSRodney W. Grimes 
711df8bae1dSRodney W. Grimes 	if (addr) {
712df8bae1dSRodney W. Grimes 		in_pcbdisconnect(inp);
71357bf258eSGarrett Wollman 		inp->inp_laddr = laddr;	/* XXX rehash? */
714df8bae1dSRodney W. Grimes 		splx(s);
715df8bae1dSRodney W. Grimes 	}
716df8bae1dSRodney W. Grimes 	return (error);
717df8bae1dSRodney W. Grimes 
718df8bae1dSRodney W. Grimes release:
719df8bae1dSRodney W. Grimes 	m_freem(m);
720df8bae1dSRodney W. Grimes 	return (error);
721df8bae1dSRodney W. Grimes }
722df8bae1dSRodney W. Grimes 
72376429de4SYoshinobu Inoue u_long	udp_sendspace = 9216;		/* really max datagram size */
724df8bae1dSRodney W. Grimes 					/* 40 1K datagrams */
7250312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
7263d177f46SBill Fumerola     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
7270312fbe9SPoul-Henning Kamp 
728cfa1ca9dSYoshinobu Inoue u_long	udp_recvspace = 40 * (1024 +
729cfa1ca9dSYoshinobu Inoue #ifdef INET6
730cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in6)
731cfa1ca9dSYoshinobu Inoue #else
732cfa1ca9dSYoshinobu Inoue 				      sizeof(struct sockaddr_in)
733cfa1ca9dSYoshinobu Inoue #endif
734cfa1ca9dSYoshinobu Inoue 				      );
7350312fbe9SPoul-Henning Kamp SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
7363d177f46SBill Fumerola     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
737df8bae1dSRodney W. Grimes 
738d0390e05SGarrett Wollman static int
739d0390e05SGarrett Wollman udp_abort(struct socket *so)
740df8bae1dSRodney W. Grimes {
741d0390e05SGarrett Wollman 	struct inpcb *inp;
742df8bae1dSRodney W. Grimes 	int s;
743df8bae1dSRodney W. Grimes 
744d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
745d0390e05SGarrett Wollman 	if (inp == 0)
746d0390e05SGarrett Wollman 		return EINVAL;	/* ??? possible? panic instead? */
747d0390e05SGarrett Wollman 	soisdisconnected(so);
748d0390e05SGarrett Wollman 	s = splnet();
749d0390e05SGarrett Wollman 	in_pcbdetach(inp);
750d0390e05SGarrett Wollman 	splx(s);
751d0390e05SGarrett Wollman 	return 0;
752df8bae1dSRodney W. Grimes }
753df8bae1dSRodney W. Grimes 
754d0390e05SGarrett Wollman static int
755a29f300eSGarrett Wollman udp_attach(struct socket *so, int proto, struct proc *p)
756d0390e05SGarrett Wollman {
757d0390e05SGarrett Wollman 	struct inpcb *inp;
758d0390e05SGarrett Wollman 	int s, error;
759d0390e05SGarrett Wollman 
760d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
761d0390e05SGarrett Wollman 	if (inp != 0)
762d0390e05SGarrett Wollman 		return EINVAL;
763d0390e05SGarrett Wollman 
764cfa1ca9dSYoshinobu Inoue 	error = soreserve(so, udp_sendspace, udp_recvspace);
765cfa1ca9dSYoshinobu Inoue 	if (error)
766cfa1ca9dSYoshinobu Inoue 		return error;
767df8bae1dSRodney W. Grimes 	s = splnet();
768a29f300eSGarrett Wollman 	error = in_pcballoc(so, &udbinfo, p);
769df8bae1dSRodney W. Grimes 	splx(s);
770df8bae1dSRodney W. Grimes 	if (error)
771d0390e05SGarrett Wollman 		return error;
772cfa1ca9dSYoshinobu Inoue 
773cfa1ca9dSYoshinobu Inoue 	inp = (struct inpcb *)so->so_pcb;
774cfa1ca9dSYoshinobu Inoue #ifdef INET6
775cfa1ca9dSYoshinobu Inoue 	inp->inp_vflag |= INP_IPV4;
776cfa1ca9dSYoshinobu Inoue #endif
777cfa1ca9dSYoshinobu Inoue 	inp->inp_ip_ttl = ip_defttl;
778cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
779cfa1ca9dSYoshinobu Inoue 	error = ipsec_init_policy(so, &inp->inp_sp);
780cfa1ca9dSYoshinobu Inoue 	if (error != 0) {
781cfa1ca9dSYoshinobu Inoue 		in_pcbdetach(inp);
782d0390e05SGarrett Wollman 		return error;
783cfa1ca9dSYoshinobu Inoue 	}
784cfa1ca9dSYoshinobu Inoue #endif /*IPSEC*/
785d0390e05SGarrett Wollman 	return 0;
786df8bae1dSRodney W. Grimes }
787d0390e05SGarrett Wollman 
788d0390e05SGarrett Wollman static int
78957bf258eSGarrett Wollman udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
790d0390e05SGarrett Wollman {
791d0390e05SGarrett Wollman 	struct inpcb *inp;
792d0390e05SGarrett Wollman 	int s, error;
793d0390e05SGarrett Wollman 
794d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
795d0390e05SGarrett Wollman 	if (inp == 0)
796d0390e05SGarrett Wollman 		return EINVAL;
797df8bae1dSRodney W. Grimes 	s = splnet();
798a29f300eSGarrett Wollman 	error = in_pcbbind(inp, nam, p);
799d0390e05SGarrett Wollman 	splx(s);
800d0390e05SGarrett Wollman 	return error;
801d0390e05SGarrett Wollman }
802d0390e05SGarrett Wollman 
803d0390e05SGarrett Wollman static int
80457bf258eSGarrett Wollman udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
805d0390e05SGarrett Wollman {
806d0390e05SGarrett Wollman 	struct inpcb *inp;
807d0390e05SGarrett Wollman 	int s, error;
80875c13541SPoul-Henning Kamp 	struct sockaddr_in *sin;
809d0390e05SGarrett Wollman 
810d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
811d0390e05SGarrett Wollman 	if (inp == 0)
812d0390e05SGarrett Wollman 		return EINVAL;
813d0390e05SGarrett Wollman 	if (inp->inp_faddr.s_addr != INADDR_ANY)
814d0390e05SGarrett Wollman 		return EISCONN;
815d0390e05SGarrett Wollman 	s = splnet();
81675c13541SPoul-Henning Kamp 	sin = (struct sockaddr_in *)nam;
81775c13541SPoul-Henning Kamp 	prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
818a29f300eSGarrett Wollman 	error = in_pcbconnect(inp, nam, p);
819df8bae1dSRodney W. Grimes 	splx(s);
820df8bae1dSRodney W. Grimes 	if (error == 0)
821df8bae1dSRodney W. Grimes 		soisconnected(so);
822d0390e05SGarrett Wollman 	return error;
823df8bae1dSRodney W. Grimes }
824d0390e05SGarrett Wollman 
825d0390e05SGarrett Wollman static int
826d0390e05SGarrett Wollman udp_detach(struct socket *so)
827d0390e05SGarrett Wollman {
828d0390e05SGarrett Wollman 	struct inpcb *inp;
829d0390e05SGarrett Wollman 	int s;
830d0390e05SGarrett Wollman 
831d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
832d0390e05SGarrett Wollman 	if (inp == 0)
833d0390e05SGarrett Wollman 		return EINVAL;
834d0390e05SGarrett Wollman 	s = splnet();
835d0390e05SGarrett Wollman 	in_pcbdetach(inp);
836d0390e05SGarrett Wollman 	splx(s);
837d0390e05SGarrett Wollman 	return 0;
838d0390e05SGarrett Wollman }
839d0390e05SGarrett Wollman 
840d0390e05SGarrett Wollman static int
841d0390e05SGarrett Wollman udp_disconnect(struct socket *so)
842d0390e05SGarrett Wollman {
843d0390e05SGarrett Wollman 	struct inpcb *inp;
844d0390e05SGarrett Wollman 	int s;
845d0390e05SGarrett Wollman 
846d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
847d0390e05SGarrett Wollman 	if (inp == 0)
848d0390e05SGarrett Wollman 		return EINVAL;
849d0390e05SGarrett Wollman 	if (inp->inp_faddr.s_addr == INADDR_ANY)
850d0390e05SGarrett Wollman 		return ENOTCONN;
851d0390e05SGarrett Wollman 
852df8bae1dSRodney W. Grimes 	s = splnet();
853df8bae1dSRodney W. Grimes 	in_pcbdisconnect(inp);
854df8bae1dSRodney W. Grimes 	inp->inp_laddr.s_addr = INADDR_ANY;
855df8bae1dSRodney W. Grimes 	splx(s);
856df8bae1dSRodney W. Grimes 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
857d0390e05SGarrett Wollman 	return 0;
858df8bae1dSRodney W. Grimes }
859df8bae1dSRodney W. Grimes 
860d0390e05SGarrett Wollman static int
86157bf258eSGarrett Wollman udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
862a29f300eSGarrett Wollman 	    struct mbuf *control, struct proc *p)
863d0390e05SGarrett Wollman {
864d0390e05SGarrett Wollman 	struct inpcb *inp;
865d0390e05SGarrett Wollman 
866d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
867d0390e05SGarrett Wollman 	if (inp == 0) {
868d0390e05SGarrett Wollman 		m_freem(m);
869d0390e05SGarrett Wollman 		return EINVAL;
870d0390e05SGarrett Wollman 	}
871a29f300eSGarrett Wollman 	return udp_output(inp, m, addr, control, p);
872d0390e05SGarrett Wollman }
873d0390e05SGarrett Wollman 
87476429de4SYoshinobu Inoue int
875d0390e05SGarrett Wollman udp_shutdown(struct socket *so)
876d0390e05SGarrett Wollman {
877d0390e05SGarrett Wollman 	struct inpcb *inp;
878d0390e05SGarrett Wollman 
879d0390e05SGarrett Wollman 	inp = sotoinpcb(so);
880d0390e05SGarrett Wollman 	if (inp == 0)
881d0390e05SGarrett Wollman 		return EINVAL;
882d0390e05SGarrett Wollman 	socantsendmore(so);
883d0390e05SGarrett Wollman 	return 0;
884d0390e05SGarrett Wollman }
885d0390e05SGarrett Wollman 
886d0390e05SGarrett Wollman struct pr_usrreqs udp_usrreqs = {
887117bcae7SGarrett Wollman 	udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
888117bcae7SGarrett Wollman 	pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
889117bcae7SGarrett Wollman 	pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
890117bcae7SGarrett Wollman 	pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
891f8f6cbbaSPeter Wemm 	in_setsockaddr, sosend, soreceive, sopoll
892d0390e05SGarrett Wollman };
89351508de1SMatthew Dillon 
894