xref: /freebsd/sys/netinet6/udp6_usrreq.c (revision bcb298fa9e23c1192c5707086a67d3b396186abc)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * Copyright (c) 2010-2011 Juniper Networks, Inc.
6  * Copyright (c) 2014 Kevin Lo
7  * All rights reserved.
8  *
9  * Portions of this software were developed by Robert N. M. Watson under
10  * contract to Juniper Networks, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the project nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	$KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
37  *	$KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
38  */
39 
40 /*-
41  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
42  *	The Regents of the University of California.
43  * All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  */
69 
70 #include <sys/cdefs.h>
71 #include "opt_inet.h"
72 #include "opt_inet6.h"
73 #include "opt_ipsec.h"
74 #include "opt_route.h"
75 #include "opt_rss.h"
76 
77 #include <sys/param.h>
78 #include <sys/domain.h>
79 #include <sys/jail.h>
80 #include <sys/kernel.h>
81 #include <sys/lock.h>
82 #include <sys/mbuf.h>
83 #include <sys/priv.h>
84 #include <sys/proc.h>
85 #include <sys/protosw.h>
86 #include <sys/sdt.h>
87 #include <sys/signalvar.h>
88 #include <sys/socket.h>
89 #include <sys/socketvar.h>
90 #include <sys/sx.h>
91 #include <sys/sysctl.h>
92 #include <sys/syslog.h>
93 #include <sys/systm.h>
94 
95 #include <net/if.h>
96 #include <net/if_var.h>
97 #include <net/if_types.h>
98 #include <net/route.h>
99 #include <net/rss_config.h>
100 
101 #include <netinet/in.h>
102 #include <netinet/in_kdtrace.h>
103 #include <netinet/in_pcb.h>
104 #include <netinet/in_systm.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip.h>
107 #include <netinet/ip6.h>
108 #include <netinet/icmp6.h>
109 #include <netinet/ip_var.h>
110 #include <netinet/udp.h>
111 #include <netinet/udp_var.h>
112 #include <netinet/udplite.h>
113 
114 #include <netinet6/ip6_var.h>
115 #include <netinet6/in6_fib.h>
116 #include <netinet6/in6_pcb.h>
117 #include <netinet6/in6_rss.h>
118 #include <netinet6/udp6_var.h>
119 #include <netinet6/scope6_var.h>
120 
121 #include <netipsec/ipsec_support.h>
122 
123 #include <security/mac/mac_framework.h>
124 
125 VNET_DEFINE(int, zero_checksum_port) = 0;
126 #define	V_zero_checksum_port	VNET(zero_checksum_port)
127 SYSCTL_INT(_net_inet6_udp6, OID_AUTO, rfc6935_port, CTLFLAG_VNET | CTLFLAG_RW,
128     &VNET_NAME(zero_checksum_port), 0,
129     "Zero UDP checksum allowed for traffic to/from this port.");
130 
131 /*
132  * UDP protocol implementation.
133  * Per RFC 768, August, 1980.
134  */
135 
136 static void		udp6_detach(struct socket *so);
137 
138 static int
udp6_append(struct inpcb * inp,struct mbuf * n,int off,struct sockaddr_in6 * fromsa)139 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
140     struct sockaddr_in6 *fromsa)
141 {
142 	struct socket *so;
143 	struct mbuf *opts = NULL, *tmp_opts;
144 	struct udpcb *up;
145 
146 	INP_LOCK_ASSERT(inp);
147 
148 	/*
149 	 * Engage the tunneling protocol.
150 	 */
151 	up = intoudpcb(inp);
152 	if (up->u_tun_func != NULL) {
153 		bool filtered;
154 
155 		in_pcbref(inp);
156 		INP_RUNLOCK(inp);
157 		filtered = (*up->u_tun_func)(n, off, inp,
158 		    (struct sockaddr *)&fromsa[0], up->u_tun_ctx);
159 		INP_RLOCK(inp);
160 		if (in_pcbrele_rlocked(inp))
161 			return (1);
162 		if (filtered) {
163 			INP_RUNLOCK(inp);
164 			return (1);
165 		}
166 	}
167 
168 	off += sizeof(struct udphdr);
169 
170 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
171 	/* Check AH/ESP integrity. */
172 	if (IPSEC_ENABLED(ipv6)) {
173 		if (IPSEC_CHECK_POLICY(ipv6, n, inp) != 0) {
174 			m_freem(n);
175 			return (0);
176 		}
177 
178 		/* IPSec UDP encaps. */
179 		if ((up->u_flags & UF_ESPINUDP) != 0 &&
180 		    UDPENCAP_INPUT(ipv6, n, off, AF_INET6) != 0) {
181 			return (0); /* Consumed. */
182 		}
183 	}
184 #endif /* IPSEC */
185 #ifdef MAC
186 	if (mac_inpcb_check_deliver(inp, n) != 0) {
187 		m_freem(n);
188 		return (0);
189 	}
190 #endif
191 	opts = NULL;
192 	if (inp->inp_flags & INP_CONTROLOPTS ||
193 	    inp->inp_socket->so_options & SO_TIMESTAMP)
194 		ip6_savecontrol(inp, n, &opts);
195 	if ((inp->inp_vflag & INP_IPV6) && (inp->inp_flags2 & INP_ORIGDSTADDR)) {
196 		tmp_opts = sbcreatecontrol(&fromsa[1],
197 		    sizeof(struct sockaddr_in6), IPV6_ORIGDSTADDR,
198 		    IPPROTO_IPV6, M_NOWAIT);
199                 if (tmp_opts) {
200                         if (opts) {
201                                 tmp_opts->m_next = opts;
202                                 opts = tmp_opts;
203                         } else
204                                 opts = tmp_opts;
205                 }
206 	}
207 	m_adj(n, off);
208 
209 	so = inp->inp_socket;
210 	SOCKBUF_LOCK(&so->so_rcv);
211 	if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&fromsa[0], n,
212 	    opts) == 0) {
213 		soroverflow_locked(so);
214 		m_freem(n);
215 		if (opts)
216 			m_freem(opts);
217 		UDPSTAT_INC(udps_fullsock);
218 	} else
219 		sorwakeup_locked(so);
220 	return (0);
221 }
222 
223 struct udp6_multi_match_ctx {
224 	struct ip6_hdr *ip6;
225 	struct udphdr *uh;
226 };
227 
228 static bool
udp6_multi_match(const struct inpcb * inp,void * v)229 udp6_multi_match(const struct inpcb *inp, void *v)
230 {
231 	struct udp6_multi_match_ctx *ctx = v;
232 
233 	if ((inp->inp_vflag & INP_IPV6) == 0)
234 		return(false);
235 	if (inp->inp_lport != ctx->uh->uh_dport)
236 		return(false);
237 	if (inp->inp_fport != 0 && inp->inp_fport != ctx->uh->uh_sport)
238 		return(false);
239 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
240 	    !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ctx->ip6->ip6_dst))
241 		return (false);
242 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
243 	    (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ctx->ip6->ip6_src) ||
244 	    inp->inp_fport != ctx->uh->uh_sport))
245 		return (false);
246 
247 	return (true);
248 }
249 
250 static int
udp6_multi_input(struct mbuf * m,int off,int proto,struct sockaddr_in6 * fromsa)251 udp6_multi_input(struct mbuf *m, int off, int proto,
252     struct sockaddr_in6 *fromsa)
253 {
254 	struct udp6_multi_match_ctx ctx;
255 	struct inpcb_iterator inpi = INP_ITERATOR(udp_get_inpcbinfo(proto),
256 	    INPLOOKUP_RLOCKPCB, udp6_multi_match, &ctx);
257 	struct inpcb *inp;
258 	struct ip6_moptions *imo;
259 	struct mbuf *n;
260 	int appends = 0;
261 
262 	/*
263 	 * In the event that laddr should be set to the link-local
264 	 * address (this happens in RIPng), the multicast address
265 	 * specified in the received packet will not match laddr.  To
266 	 * handle this situation, matching is relaxed if the
267 	 * receiving interface is the same as one specified in the
268 	 * socket and if the destination multicast address matches
269 	 * one of the multicast groups specified in the socket.
270 	 */
271 
272 	/*
273 	 * KAME note: traditionally we dropped udpiphdr from mbuf
274 	 * here.  We need udphdr for IPsec processing so we do that
275 	 * later.
276 	 */
277 	ctx.ip6 = mtod(m, struct ip6_hdr *);
278 	ctx.uh = (struct udphdr *)((char *)ctx.ip6 + off);
279 	while ((inp = inp_next(&inpi)) != NULL) {
280 		INP_RLOCK_ASSERT(inp);
281 		/*
282 		 * XXXRW: Because we weren't holding either the inpcb
283 		 * or the hash lock when we checked for a match
284 		 * before, we should probably recheck now that the
285 		 * inpcb lock is (supposed to be) held.
286 		 */
287 		/*
288 		 * Handle socket delivery policy for any-source
289 		 * and source-specific multicast. [RFC3678]
290 		 */
291 		if ((imo = inp->in6p_moptions) != NULL) {
292 			struct sockaddr_in6	 mcaddr;
293 			int			 blocked;
294 
295 			bzero(&mcaddr, sizeof(struct sockaddr_in6));
296 			mcaddr.sin6_len = sizeof(struct sockaddr_in6);
297 			mcaddr.sin6_family = AF_INET6;
298 			mcaddr.sin6_addr = ctx.ip6->ip6_dst;
299 
300 			blocked = im6o_mc_filter(imo, m->m_pkthdr.rcvif,
301 				(struct sockaddr *)&mcaddr,
302 				(struct sockaddr *)&fromsa[0]);
303 			if (blocked != MCAST_PASS) {
304 				if (blocked == MCAST_NOTGMEMBER)
305 					IP6STAT_INC(ip6s_notmember);
306 				if (blocked == MCAST_NOTSMEMBER ||
307 				    blocked == MCAST_MUTED)
308 					UDPSTAT_INC(udps_filtermcast);
309 				continue;
310 			}
311 		}
312 		if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) {
313 			if (proto == IPPROTO_UDPLITE)
314 				UDPLITE_PROBE(receive, NULL, inp, ctx.ip6,
315 				    inp, ctx.uh);
316 			else
317 				UDP_PROBE(receive, NULL, inp, ctx.ip6, inp,
318 				    ctx.uh);
319 			if (udp6_append(inp, n, off, fromsa)) {
320 				break;
321 			} else
322 				appends++;
323 		}
324 		/*
325 		 * Don't look for additional matches if this one does
326 		 * not have either the SO_REUSEPORT or SO_REUSEADDR
327 		 * socket options set.  This heuristic avoids
328 		 * searching through all pcbs in the common case of a
329 		 * non-shared port.  It assumes that an application
330 		 * will never clear these options after setting them.
331 		 */
332 		if ((inp->inp_socket->so_options &
333 		     (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) {
334 			INP_RUNLOCK(inp);
335 			break;
336 		}
337 	}
338 	m_freem(m);
339 
340 	if (appends == 0) {
341 		/*
342 		 * No matching pcb found; discard datagram.  (No need
343 		 * to send an ICMP Port Unreachable for a broadcast
344 		 * or multicast datgram.)
345 		 */
346 		UDPSTAT_INC(udps_noport);
347 		UDPSTAT_INC(udps_noportmcast);
348 	}
349 
350 	return (IPPROTO_DONE);
351 }
352 
353 int
udp6_input(struct mbuf ** mp,int * offp,int proto)354 udp6_input(struct mbuf **mp, int *offp, int proto)
355 {
356 	struct mbuf *m = *mp;
357 	struct ip6_hdr *ip6;
358 	struct udphdr *uh;
359 	struct inpcb *inp;
360 	struct inpcbinfo *pcbinfo;
361 	struct udpcb *up;
362 	int off = *offp;
363 	int cscov_partial;
364 	int plen, ulen;
365 	int lookupflags;
366 	struct sockaddr_in6 fromsa[2];
367 	struct m_tag *fwd_tag;
368 	uint16_t uh_sum;
369 	uint8_t nxt;
370 
371 	NET_EPOCH_ASSERT();
372 
373 	if (m->m_len < off + sizeof(struct udphdr)) {
374 		m = m_pullup(m, off + sizeof(struct udphdr));
375 		if (m == NULL) {
376 			IP6STAT_INC(ip6s_exthdrtoolong);
377 			*mp = NULL;
378 			return (IPPROTO_DONE);
379 		}
380 	}
381 	ip6 = mtod(m, struct ip6_hdr *);
382 	uh = (struct udphdr *)((caddr_t)ip6 + off);
383 
384 	UDPSTAT_INC(udps_ipackets);
385 
386 	/*
387 	 * Destination port of 0 is illegal, based on RFC768.
388 	 */
389 	if (uh->uh_dport == 0)
390 		goto badunlocked;
391 
392 	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
393 	ulen = ntohs((u_short)uh->uh_ulen);
394 
395 	nxt = proto;
396 	cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
397 	if (nxt == IPPROTO_UDPLITE) {
398 		/* Zero means checksum over the complete packet. */
399 		if (ulen == 0)
400 			ulen = plen;
401 		if (ulen == plen)
402 			cscov_partial = 0;
403 		if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) {
404 			/* XXX: What is the right UDPLite MIB counter? */
405 			goto badunlocked;
406 		}
407 		if (uh->uh_sum == 0) {
408 			/* XXX: What is the right UDPLite MIB counter? */
409 			goto badunlocked;
410 		}
411 	} else {
412 		if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) {
413 			UDPSTAT_INC(udps_badlen);
414 			goto badunlocked;
415 		}
416 		if (uh->uh_sum == 0) {
417 			UDPSTAT_INC(udps_nosum);
418 			/*
419 			 * dport 0 was rejected earlier so this is OK even if
420 			 * zero_checksum_port is 0 (which is its default value).
421 			 */
422 			if (ntohs(uh->uh_dport) == V_zero_checksum_port)
423 				goto skip_checksum;
424 			else
425 				goto badunlocked;
426 		}
427 	}
428 
429 	if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
430 	    !cscov_partial) {
431 		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
432 			uh_sum = m->m_pkthdr.csum_data;
433 		else
434 			uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
435 			    m->m_pkthdr.csum_data);
436 		uh_sum ^= 0xffff;
437 	} else if (m->m_pkthdr.csum_flags & CSUM_IP6_UDP) {
438 		/*
439 		 * Packet from local host (maybe from a VM).
440 		 * Checksum not required.
441 		 */
442 		uh_sum = 0;
443 	} else
444 		uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen);
445 
446 	if (uh_sum != 0) {
447 		UDPSTAT_INC(udps_badsum);
448 		goto badunlocked;
449 	}
450 
451 skip_checksum:
452 	/*
453 	 * Construct sockaddr format source address.
454 	 */
455 	init_sin6(&fromsa[0], m, 0);
456 	fromsa[0].sin6_port = uh->uh_sport;
457 	init_sin6(&fromsa[1], m, 1);
458 	fromsa[1].sin6_port = uh->uh_dport;
459 
460 	pcbinfo = udp_get_inpcbinfo(nxt);
461 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst))  {
462 		*mp = NULL;
463 		return (udp6_multi_input(m, off, proto, fromsa));
464 	}
465 
466 	/*
467 	 * Locate pcb for datagram.
468 	 */
469 	lookupflags = INPLOOKUP_RLOCKPCB |
470 	    (V_udp_bind_all_fibs ? 0 : INPLOOKUP_FIB);
471 
472 	/*
473 	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
474 	 */
475 	if ((m->m_flags & M_IP6_NEXTHOP) &&
476 	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
477 		struct sockaddr_in6 *next_hop6;
478 
479 		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
480 
481 		/*
482 		 * Transparently forwarded. Pretend to be the destination.
483 		 * Already got one like this?
484 		 */
485 		inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
486 		    uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
487 		    lookupflags, m->m_pkthdr.rcvif, m);
488 		if (!inp) {
489 			/*
490 			 * It's new.  Try to find the ambushing socket.
491 			 * Because we've rewritten the destination address,
492 			 * any hardware-generated hash is ignored.
493 			 */
494 			inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
495 			    uh->uh_sport, &next_hop6->sin6_addr,
496 			    next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
497 			    uh->uh_dport, INPLOOKUP_WILDCARD | lookupflags,
498 			    m->m_pkthdr.rcvif);
499 		}
500 		/* Remove the tag from the packet. We don't need it anymore. */
501 		m_tag_delete(m, fwd_tag);
502 		m->m_flags &= ~M_IP6_NEXTHOP;
503 	} else
504 		inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
505 		    uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
506 		    INPLOOKUP_WILDCARD | lookupflags,
507 		    m->m_pkthdr.rcvif, m);
508 	if (inp == NULL) {
509 		if (V_udp_log_in_vain) {
510 			char ip6bufs[INET6_ADDRSTRLEN];
511 			char ip6bufd[INET6_ADDRSTRLEN];
512 
513 			log(LOG_INFO,
514 			    "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
515 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
516 			    ntohs(uh->uh_dport),
517 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
518 			    ntohs(uh->uh_sport));
519 		}
520 		if (nxt == IPPROTO_UDPLITE)
521 			UDPLITE_PROBE(receive, NULL, NULL, ip6, NULL, uh);
522 		else
523 			UDP_PROBE(receive, NULL, NULL, ip6, NULL, uh);
524 		UDPSTAT_INC(udps_noport);
525 		if (m->m_flags & M_MCAST) {
526 			printf("UDP6: M_MCAST is set in a unicast packet.\n");
527 			UDPSTAT_INC(udps_noportmcast);
528 			goto badunlocked;
529 		}
530 		if (V_udp_blackhole && (V_udp_blackhole_local ||
531 		    !in6_localip(&ip6->ip6_src)))
532 			goto badunlocked;
533 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
534 		*mp = NULL;
535 		return (IPPROTO_DONE);
536 	}
537 	INP_RLOCK_ASSERT(inp);
538 	up = intoudpcb(inp);
539 	if (cscov_partial) {
540 		if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
541 			INP_RUNLOCK(inp);
542 			m_freem(m);
543 			*mp = NULL;
544 			return (IPPROTO_DONE);
545 		}
546 	}
547 	if (nxt == IPPROTO_UDPLITE)
548 		UDPLITE_PROBE(receive, NULL, inp, ip6, inp, uh);
549 	else
550 		UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
551 	if (udp6_append(inp, m, off, fromsa) == 0)
552 		INP_RUNLOCK(inp);
553 	*mp = NULL;
554 	return (IPPROTO_DONE);
555 
556 badunlocked:
557 	m_freem(m);
558 	*mp = NULL;
559 	return (IPPROTO_DONE);
560 }
561 
562 static void
udp6_common_ctlinput(struct ip6ctlparam * ip6cp,struct inpcbinfo * pcbinfo)563 udp6_common_ctlinput(struct ip6ctlparam *ip6cp, struct inpcbinfo *pcbinfo)
564 {
565 	struct udphdr uh;
566 	struct ip6_hdr *ip6;
567 	struct mbuf *m;
568 	struct inpcb *inp;
569 	int errno, off = 0;
570 	struct udp_portonly {
571 		u_int16_t uh_sport;
572 		u_int16_t uh_dport;
573 	} *uhp;
574 
575 	if ((errno = icmp6_errmap(ip6cp->ip6c_icmp6)) == 0)
576 		return;
577 
578 	m = ip6cp->ip6c_m;
579 	ip6 = ip6cp->ip6c_ip6;
580 	off = ip6cp->ip6c_off;
581 
582 	/* Check if we can safely examine src and dst ports. */
583 	if (m->m_pkthdr.len < off + sizeof(*uhp))
584 		return;
585 
586 	bzero(&uh, sizeof(uh));
587 	m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
588 
589 	/* Check to see if its tunneled */
590 	inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_dst, uh.uh_dport,
591 	    &ip6->ip6_src, uh.uh_sport, INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
592 	    m->m_pkthdr.rcvif, m);
593 	if (inp != NULL) {
594 		struct udpcb *up;
595 		udp_tun_icmp_t *func;
596 
597 		up = intoudpcb(inp);
598 		func = up->u_icmp_func;
599 		INP_RUNLOCK(inp);
600 		if (func != NULL)
601 			func(ip6cp);
602 	}
603 	in6_pcbnotify(pcbinfo, ip6cp->ip6c_finaldst, uh.uh_dport,
604 	    ip6cp->ip6c_src, uh.uh_sport, errno, ip6cp->ip6c_cmdarg,
605 	    udp_notify);
606 }
607 
608 static void
udp6_ctlinput(struct ip6ctlparam * ctl)609 udp6_ctlinput(struct ip6ctlparam *ctl)
610 {
611 
612 	return (udp6_common_ctlinput(ctl, &V_udbinfo));
613 }
614 
615 static void
udplite6_ctlinput(struct ip6ctlparam * ctl)616 udplite6_ctlinput(struct ip6ctlparam *ctl)
617 {
618 
619 	return (udp6_common_ctlinput(ctl, &V_ulitecbinfo));
620 }
621 
622 static int
udp6_getcred(SYSCTL_HANDLER_ARGS)623 udp6_getcred(SYSCTL_HANDLER_ARGS)
624 {
625 	struct xucred xuc;
626 	struct sockaddr_in6 addrs[2];
627 	struct epoch_tracker et;
628 	struct inpcb *inp;
629 	int error;
630 
631 	if (req->newptr == NULL)
632 		return (EINVAL);
633 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
634 	if (error)
635 		return (error);
636 
637 	if (req->newlen != sizeof(addrs))
638 		return (EINVAL);
639 	if (req->oldlen != sizeof(struct xucred))
640 		return (EINVAL);
641 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
642 	if (error)
643 		return (error);
644 	if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
645 	    (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
646 		return (error);
647 	}
648 	NET_EPOCH_ENTER(et);
649 	inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
650 	    addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
651 	    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
652 	NET_EPOCH_EXIT(et);
653 	if (inp != NULL) {
654 		INP_RLOCK_ASSERT(inp);
655 		if (inp->inp_socket == NULL)
656 			error = ENOENT;
657 		if (error == 0)
658 			error = cr_canseesocket(req->td->td_ucred,
659 			    inp->inp_socket);
660 		if (error == 0)
661 			cru2x(inp->inp_cred, &xuc);
662 		INP_RUNLOCK(inp);
663 	} else
664 		error = ENOENT;
665 	if (error == 0)
666 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
667 	return (error);
668 }
669 
670 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred,
671     CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_MPSAFE,
672     0, 0, udp6_getcred, "S,xucred",
673     "Get the xucred of a UDP6 connection");
674 
675 static int
udp6_send(struct socket * so,int flags_arg,struct mbuf * m,struct sockaddr * addr6,struct mbuf * control,struct thread * td)676 udp6_send(struct socket *so, int flags_arg, struct mbuf *m,
677     struct sockaddr *addr6, struct mbuf *control, struct thread *td)
678 {
679 	struct inpcb *inp;
680 	struct ip6_hdr *ip6;
681 	struct udphdr *udp6;
682 	struct in6_addr *laddr, *faddr, in6a;
683 	struct ip6_pktopts *optp, opt;
684 	struct sockaddr_in6 *sin6, tmp;
685 	struct epoch_tracker et;
686 	int cscov_partial, error, flags, hlen, scope_ambiguous;
687 	u_int32_t ulen, plen;
688 	uint16_t cscov;
689 	u_short fport;
690 	uint8_t nxt;
691 
692 	if (addr6) {
693 		error = 0;
694 		if (addr6->sa_family != AF_INET6)
695 			error = EAFNOSUPPORT;
696 		else if (addr6->sa_len != sizeof(struct sockaddr_in6))
697 			error = EINVAL;
698 		if (__predict_false(error != 0)) {
699 			m_freem(control);
700 			m_freem(m);
701 			return (error);
702 		}
703 	}
704 
705 	sin6 = (struct sockaddr_in6 *)addr6;
706 
707 	/*
708 	 * In contrast to IPv4 we do not validate the max. packet length
709 	 * here due to IPv6 Jumbograms (RFC2675).
710 	 */
711 
712 	scope_ambiguous = 0;
713 	if (sin6) {
714 		/* Protect *addr6 from overwrites. */
715 		tmp = *sin6;
716 		sin6 = &tmp;
717 
718 		/*
719 		 * Application should provide a proper zone ID or the use of
720 		 * default zone IDs should be enabled.  Unfortunately, some
721 		 * applications do not behave as it should, so we need a
722 		 * workaround.  Even if an appropriate ID is not determined,
723 		 * we'll see if we can determine the outgoing interface.  If we
724 		 * can, determine the zone ID based on the interface below.
725 		 */
726 		if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
727 			scope_ambiguous = 1;
728 		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) {
729 			if (control)
730 				m_freem(control);
731 			m_freem(m);
732 			return (error);
733 		}
734 	}
735 
736 	inp = sotoinpcb(so);
737 	KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
738 	/*
739 	 * In the following cases we want a write lock on the inp for either
740 	 * local operations or for possible route cache updates in the IPv6
741 	 * output path:
742 	 * - on connected sockets (sin6 is NULL) for route cache updates,
743 	 * - when we are not bound to an address and source port (it is
744 	 *   in6_pcbsetport() which will require the write lock).
745 	 *
746 	 * We check the inp fields before actually locking the inp, so
747 	 * here exists a race, and we may WLOCK the inp and end with already
748 	 * bound one by other thread. This is fine.
749 	 */
750 	if (sin6 == NULL || (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
751 	    inp->inp_lport == 0))
752 		INP_WLOCK(inp);
753 	else
754 		INP_RLOCK(inp);
755 
756 	nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
757 	    IPPROTO_UDP : IPPROTO_UDPLITE;
758 
759 #ifdef INET
760 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
761 		int hasv4addr;
762 
763 		if (sin6 == NULL)
764 			hasv4addr = (inp->inp_vflag & INP_IPV4);
765 		else
766 			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
767 			    ? 1 : 0;
768 		if (hasv4addr) {
769 			/*
770 			 * XXXRW: We release UDP-layer locks before calling
771 			 * udp_send() in order to avoid recursion.  However,
772 			 * this does mean there is a short window where inp's
773 			 * fields are unstable.  Could this lead to a
774 			 * potential race in which the factors causing us to
775 			 * select the UDPv4 output routine are invalidated?
776 			 */
777 			INP_UNLOCK(inp);
778 			if (sin6)
779 				in6_sin6_2_sin_in_sock((struct sockaddr *)sin6);
780 			/* addr will just be freed in sendit(). */
781 			return (udp_send(so, flags_arg | PRUS_IPV6, m,
782 			    (struct sockaddr *)sin6, control, td));
783 		}
784 	} else
785 #endif
786 	if (sin6 && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
787 		/*
788 		 * Given this is either an IPv6-only socket or no INET is
789 		 * supported we will fail the send if the given destination
790 		 * address is a v4mapped address.
791 		 */
792 		INP_UNLOCK(inp);
793 		m_freem(m);
794 		m_freem(control);
795 		return (EINVAL);
796 	}
797 
798 	NET_EPOCH_ENTER(et);
799 	if (control) {
800 		if ((error = ip6_setpktopts(control, &opt,
801 		    inp->in6p_outputopts, td->td_ucred, nxt)) != 0) {
802 			goto release;
803 		}
804 		optp = &opt;
805 	} else
806 		optp = inp->in6p_outputopts;
807 
808 	if (sin6) {
809 		/*
810 		 * Since we saw no essential reason for calling in_pcbconnect,
811 		 * we get rid of such kind of logic, and call in6_selectsrc
812 		 * and in6_pcbsetport in order to fill in the local address
813 		 * and the local port.
814 		 */
815 		if (sin6->sin6_port == 0) {
816 			error = EADDRNOTAVAIL;
817 			goto release;
818 		}
819 
820 		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
821 			/* how about ::ffff:0.0.0.0 case? */
822 			error = EISCONN;
823 			goto release;
824 		}
825 
826 		/*
827 		 * Given we handle the v4mapped case in the INET block above
828 		 * assert here that it must not happen anymore.
829 		 */
830 		KASSERT(!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr),
831 		    ("%s: sin6(%p)->sin6_addr is v4mapped which we "
832 		    "should have handled.", __func__, sin6));
833 
834 		/* This only requires read-locking. */
835 		error = in6_selectsrc_socket(sin6, optp, inp,
836 		    td->td_ucred, scope_ambiguous, &in6a, NULL);
837 		if (error)
838 			goto release;
839 		laddr = &in6a;
840 
841 		if (inp->inp_lport == 0) {
842 			struct inpcbinfo *pcbinfo;
843 
844 			INP_WLOCK_ASSERT(inp);
845 
846 			pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
847 			INP_HASH_WLOCK(pcbinfo);
848 			error = in6_pcbsetport(laddr, inp, td->td_ucred);
849 			INP_HASH_WUNLOCK(pcbinfo);
850 			if (error != 0) {
851 				/* Undo an address bind that may have occurred. */
852 				inp->in6p_laddr = in6addr_any;
853 				goto release;
854 			}
855 		}
856 		faddr = &sin6->sin6_addr;
857 		fport = sin6->sin6_port; /* allow 0 port */
858 
859 	} else {
860 		if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
861 			error = ENOTCONN;
862 			goto release;
863 		}
864 		laddr = &inp->in6p_laddr;
865 		faddr = &inp->in6p_faddr;
866 		fport = inp->inp_fport;
867 	}
868 
869 	ulen = m->m_pkthdr.len;
870 	plen = sizeof(struct udphdr) + ulen;
871 	hlen = sizeof(struct ip6_hdr);
872 
873 	/*
874 	 * Calculate data length and get a mbuf for UDP, IP6, and possible
875 	 * link-layer headers.  Immediate slide the data pointer back forward
876 	 * since we won't use that space at this layer.
877 	 */
878 	M_PREPEND(m, hlen + sizeof(struct udphdr) + max_linkhdr, M_NOWAIT);
879 	if (m == NULL) {
880 		error = ENOBUFS;
881 		goto release;
882 	}
883 	m->m_data += max_linkhdr;
884 	m->m_len -= max_linkhdr;
885 	m->m_pkthdr.len -= max_linkhdr;
886 
887 	/*
888 	 * Stuff checksum and output datagram.
889 	 */
890 	cscov = cscov_partial = 0;
891 	udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
892 	udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
893 	udp6->uh_dport = fport;
894 	if (nxt == IPPROTO_UDPLITE) {
895 		struct udpcb *up;
896 
897 		up = intoudpcb(inp);
898 		cscov = up->u_txcslen;
899 		if (cscov >= plen)
900 			cscov = 0;
901 		udp6->uh_ulen = htons(cscov);
902 		/*
903 		 * For UDP-Lite, checksum coverage length of zero means
904 		 * the entire UDPLite packet is covered by the checksum.
905 		 */
906 		cscov_partial = (cscov == 0) ? 0 : 1;
907 	} else if (plen <= 0xffff)
908 		udp6->uh_ulen = htons((u_short)plen);
909 	else
910 		udp6->uh_ulen = 0;
911 	udp6->uh_sum = 0;
912 
913 	ip6 = mtod(m, struct ip6_hdr *);
914 	ip6->ip6_flow	= inp->inp_flow & IPV6_FLOWINFO_MASK;
915 	ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
916 	ip6->ip6_vfc	|= IPV6_VERSION;
917 	ip6->ip6_plen	= htons((u_short)plen);
918 	ip6->ip6_nxt	= nxt;
919 	ip6->ip6_hlim	= in6_selecthlim(inp, NULL);
920 	ip6->ip6_src	= *laddr;
921 	ip6->ip6_dst	= *faddr;
922 
923 #ifdef MAC
924 	mac_inpcb_create_mbuf(inp, m);
925 #endif
926 
927 	if (cscov_partial) {
928 		if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
929 		    sizeof(struct ip6_hdr), plen, cscov)) == 0)
930 			udp6->uh_sum = 0xffff;
931 	} else {
932 		udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
933 		m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
934 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
935 	}
936 
937 	flags = 0;
938 #if defined(ROUTE_MPATH) || defined(RSS)
939 	if (CALC_FLOWID_OUTBOUND_SENDTO) {
940 		uint32_t hash_type, hash_val;
941 		uint8_t pr;
942 
943 		pr = inp->inp_socket->so_proto->pr_protocol;
944 
945 		hash_val = fib6_calc_packet_hash(laddr, faddr,
946 		    inp->inp_lport, fport, pr, &hash_type);
947 		m->m_pkthdr.flowid = hash_val;
948 		M_HASHTYPE_SET(m, hash_type);
949 	}
950 	/* do not use inp flowid */
951 	flags |= IP_NODEFAULTFLOWID;
952 #endif
953 
954 	UDPSTAT_INC(udps_opackets);
955 	if (nxt == IPPROTO_UDPLITE)
956 		UDPLITE_PROBE(send, NULL, inp, ip6, inp, udp6);
957 	else
958 		UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
959 	error = ip6_output(m, optp,
960 	    INP_WLOCKED(inp) ? &inp->inp_route6 : NULL, flags,
961 	    inp->in6p_moptions, NULL, inp);
962 	INP_UNLOCK(inp);
963 	NET_EPOCH_EXIT(et);
964 
965 	if (control) {
966 		ip6_clearpktopts(&opt, -1);
967 		m_freem(control);
968 	}
969 	return (error);
970 
971 release:
972 	INP_UNLOCK(inp);
973 	NET_EPOCH_EXIT(et);
974 	if (control) {
975 		ip6_clearpktopts(&opt, -1);
976 		m_freem(control);
977 	}
978 	m_freem(m);
979 
980 	return (error);
981 }
982 
983 static void
udp6_abort(struct socket * so)984 udp6_abort(struct socket *so)
985 {
986 	struct inpcb *inp;
987 	struct inpcbinfo *pcbinfo;
988 
989 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
990 	inp = sotoinpcb(so);
991 	KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
992 
993 	INP_WLOCK(inp);
994 #ifdef INET
995 	if (inp->inp_vflag & INP_IPV4) {
996 		INP_WUNLOCK(inp);
997 		udp_abort(so);
998 		return;
999 	}
1000 #endif
1001 
1002 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1003 		INP_HASH_WLOCK(pcbinfo);
1004 		in6_pcbdisconnect(inp);
1005 		INP_HASH_WUNLOCK(pcbinfo);
1006 		soisdisconnected(so);
1007 	}
1008 	INP_WUNLOCK(inp);
1009 }
1010 
1011 static int
udp6_attach(struct socket * so,int proto,struct thread * td)1012 udp6_attach(struct socket *so, int proto, struct thread *td)
1013 {
1014 	struct inpcbinfo *pcbinfo;
1015 	struct inpcb *inp;
1016 	struct udpcb *up;
1017 	int error;
1018 
1019 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1020 	inp = sotoinpcb(so);
1021 	KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
1022 
1023 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1024 		error = soreserve(so, udp_sendspace, udp_recvspace);
1025 		if (error)
1026 			return (error);
1027 	}
1028 	error = in_pcballoc(so, pcbinfo);
1029 	if (error)
1030 		return (error);
1031 	inp = (struct inpcb *)so->so_pcb;
1032 	inp->in6p_cksum = -1;	/* just to be sure */
1033 	/*
1034 	 * XXX: ugly!!
1035 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
1036 	 * because the socket may be bound to an IPv6 wildcard address,
1037 	 * which may match an IPv4-mapped IPv6 address.
1038 	 */
1039 	inp->inp_ip_ttl = V_ip_defttl;
1040 	up = intoudpcb(inp);
1041 	bzero(&up->u_start_zero, u_zero_size);
1042 	INP_WUNLOCK(inp);
1043 	return (0);
1044 }
1045 
1046 static int
udp6_bind(struct socket * so,struct sockaddr * nam,struct thread * td)1047 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1048 {
1049 	struct sockaddr_in6 *sin6_p;
1050 	struct inpcb *inp;
1051 	struct inpcbinfo *pcbinfo;
1052 	int error;
1053 	u_char vflagsav;
1054 
1055 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1056 	inp = sotoinpcb(so);
1057 	KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
1058 
1059 	if (nam->sa_family != AF_INET6)
1060 		return (EAFNOSUPPORT);
1061 	if (nam->sa_len != sizeof(struct sockaddr_in6))
1062 		return (EINVAL);
1063 
1064 	sin6_p = (struct sockaddr_in6 *)nam;
1065 
1066 	INP_WLOCK(inp);
1067 	INP_HASH_WLOCK(pcbinfo);
1068 	vflagsav = inp->inp_vflag;
1069 	inp->inp_vflag &= ~INP_IPV4;
1070 	inp->inp_vflag |= INP_IPV6;
1071 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1072 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
1073 			inp->inp_vflag |= INP_IPV4;
1074 #ifdef INET
1075 		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
1076 			struct sockaddr_in sin;
1077 
1078 			in6_sin6_2_sin(&sin, sin6_p);
1079 			inp->inp_vflag |= INP_IPV4;
1080 			inp->inp_vflag &= ~INP_IPV6;
1081 			error = in_pcbbind(inp, &sin,
1082 			    V_udp_bind_all_fibs ? 0 : INPBIND_FIB,
1083 			    td->td_ucred);
1084 			goto out;
1085 		}
1086 #endif
1087 	}
1088 
1089 	error = in6_pcbbind(inp, sin6_p, V_udp_bind_all_fibs ? 0 : INPBIND_FIB,
1090 	    td->td_ucred);
1091 #ifdef INET
1092 out:
1093 #endif
1094 	if (error != 0)
1095 		inp->inp_vflag = vflagsav;
1096 	INP_HASH_WUNLOCK(pcbinfo);
1097 	INP_WUNLOCK(inp);
1098 	return (error);
1099 }
1100 
1101 static void
udp6_close(struct socket * so)1102 udp6_close(struct socket *so)
1103 {
1104 	struct inpcb *inp;
1105 	struct inpcbinfo *pcbinfo;
1106 
1107 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1108 	inp = sotoinpcb(so);
1109 	KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1110 
1111 	INP_WLOCK(inp);
1112 #ifdef INET
1113 	if (inp->inp_vflag & INP_IPV4) {
1114 		INP_WUNLOCK(inp);
1115 		(void)udp_disconnect(so);
1116 		return;
1117 	}
1118 #endif
1119 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1120 		INP_HASH_WLOCK(pcbinfo);
1121 		in6_pcbdisconnect(inp);
1122 		INP_HASH_WUNLOCK(pcbinfo);
1123 		soisdisconnected(so);
1124 	}
1125 	INP_WUNLOCK(inp);
1126 }
1127 
1128 static int
udp6_connect(struct socket * so,struct sockaddr * nam,struct thread * td)1129 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1130 {
1131 	struct epoch_tracker et;
1132 	struct inpcb *inp;
1133 	struct inpcbinfo *pcbinfo;
1134 	struct sockaddr_in6 *sin6;
1135 	int error;
1136 	u_char vflagsav;
1137 
1138 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1139 	inp = sotoinpcb(so);
1140 	KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1141 
1142 	sin6 = (struct sockaddr_in6 *)nam;
1143 	if (sin6->sin6_family != AF_INET6)
1144 		return (EAFNOSUPPORT);
1145 	if (sin6->sin6_len != sizeof(*sin6))
1146 		return (EINVAL);
1147 
1148 	/*
1149 	 * XXXRW: Need to clarify locking of v4/v6 flags.
1150 	 */
1151 	INP_WLOCK(inp);
1152 #ifdef INET
1153 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1154 		struct sockaddr_in sin;
1155 
1156 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1157 			error = EINVAL;
1158 			goto out;
1159 		}
1160 		if ((inp->inp_vflag & INP_IPV4) == 0) {
1161 			error = EAFNOSUPPORT;
1162 			goto out;
1163 		}
1164 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1165 			error = EISCONN;
1166 			goto out;
1167 		}
1168 		in6_sin6_2_sin(&sin, sin6);
1169 		error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1170 		if (error != 0)
1171 			goto out;
1172 		vflagsav = inp->inp_vflag;
1173 		inp->inp_vflag |= INP_IPV4;
1174 		inp->inp_vflag &= ~INP_IPV6;
1175 		NET_EPOCH_ENTER(et);
1176 		INP_HASH_WLOCK(pcbinfo);
1177 		error = in_pcbconnect(inp, &sin, td->td_ucred);
1178 		INP_HASH_WUNLOCK(pcbinfo);
1179 		NET_EPOCH_EXIT(et);
1180 		/*
1181 		 * If connect succeeds, mark socket as connected. If
1182 		 * connect fails and socket is unbound, reset inp_vflag
1183 		 * field.
1184 		 */
1185 		if (error == 0)
1186 			soisconnected(so);
1187 		else if (inp->inp_laddr.s_addr == INADDR_ANY &&
1188 		    inp->inp_lport == 0)
1189 			inp->inp_vflag = vflagsav;
1190 		goto out;
1191 	} else {
1192 		if ((inp->inp_vflag & INP_IPV6) == 0) {
1193 			error = EAFNOSUPPORT;
1194 			goto out;
1195 		}
1196 	}
1197 #endif
1198 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1199 		error = EISCONN;
1200 		goto out;
1201 	}
1202 	error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1203 	if (error != 0)
1204 		goto out;
1205 	vflagsav = inp->inp_vflag;
1206 	inp->inp_vflag &= ~INP_IPV4;
1207 	inp->inp_vflag |= INP_IPV6;
1208 	NET_EPOCH_ENTER(et);
1209 	INP_HASH_WLOCK(pcbinfo);
1210 	error = in6_pcbconnect(inp, sin6, td->td_ucred, true);
1211 	INP_HASH_WUNLOCK(pcbinfo);
1212 	NET_EPOCH_EXIT(et);
1213 	/*
1214 	 * If connect succeeds, mark socket as connected. If
1215 	 * connect fails and socket is unbound, reset inp_vflag
1216 	 * field.
1217 	 */
1218 	if (error == 0)
1219 		soisconnected(so);
1220 	else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1221 	    inp->inp_lport == 0)
1222 		inp->inp_vflag = vflagsav;
1223 out:
1224 	INP_WUNLOCK(inp);
1225 	return (error);
1226 }
1227 
1228 static void
udp6_detach(struct socket * so)1229 udp6_detach(struct socket *so)
1230 {
1231 	struct inpcb *inp;
1232 
1233 	inp = sotoinpcb(so);
1234 	KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1235 
1236 	INP_WLOCK(inp);
1237 	in_pcbfree(inp);
1238 }
1239 
1240 static int
udp6_disconnect(struct socket * so)1241 udp6_disconnect(struct socket *so)
1242 {
1243 	struct inpcb *inp;
1244 	struct inpcbinfo *pcbinfo;
1245 
1246 	pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1247 	inp = sotoinpcb(so);
1248 	KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1249 
1250 	INP_WLOCK(inp);
1251 #ifdef INET
1252 	if (inp->inp_vflag & INP_IPV4) {
1253 		INP_WUNLOCK(inp);
1254 		(void)udp_disconnect(so);
1255 		return (0);
1256 	}
1257 #endif
1258 
1259 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1260 		INP_WUNLOCK(inp);
1261 		return (ENOTCONN);
1262 	}
1263 
1264 	INP_HASH_WLOCK(pcbinfo);
1265 	in6_pcbdisconnect(inp);
1266 	INP_HASH_WUNLOCK(pcbinfo);
1267 	SOCK_LOCK(so);
1268 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1269 	SOCK_UNLOCK(so);
1270 	INP_WUNLOCK(inp);
1271 	return (0);
1272 }
1273 
1274 #define	UDP6_PROTOSW							\
1275 	.pr_type =		SOCK_DGRAM,				\
1276 	.pr_flags =		PR_ATOMIC|PR_ADDR|PR_CAPATTACH,		\
1277 	.pr_ctloutput =		udp_ctloutput,				\
1278 	.pr_abort =		udp6_abort,				\
1279 	.pr_attach =		udp6_attach,				\
1280 	.pr_bind =		udp6_bind,				\
1281 	.pr_connect =		udp6_connect,				\
1282 	.pr_control =		in6_control,				\
1283 	.pr_detach =		udp6_detach,				\
1284 	.pr_disconnect =	udp6_disconnect,			\
1285 	.pr_peeraddr =		in6_mapped_peeraddr,			\
1286 	.pr_send =		udp6_send,				\
1287 	.pr_shutdown =		udp_shutdown,				\
1288 	.pr_sockaddr =		in6_mapped_sockaddr,			\
1289 	.pr_soreceive =		soreceive_dgram,			\
1290 	.pr_sosend =		sosend_dgram,				\
1291 	.pr_sosetlabel =	in_pcbsosetlabel,			\
1292 	.pr_close =		udp6_close
1293 
1294 struct protosw udp6_protosw = {
1295 	.pr_protocol =		IPPROTO_UDP,
1296 	UDP6_PROTOSW
1297 };
1298 
1299 struct protosw udplite6_protosw = {
1300 	.pr_protocol =		IPPROTO_UDPLITE,
1301 	UDP6_PROTOSW
1302 };
1303 
1304 static void
udp6_init(void * arg __unused)1305 udp6_init(void *arg __unused)
1306 {
1307 
1308 	IP6PROTO_REGISTER(IPPROTO_UDP, udp6_input, udp6_ctlinput);
1309 	IP6PROTO_REGISTER(IPPROTO_UDPLITE, udp6_input, udplite6_ctlinput);
1310 }
1311 SYSINIT(udp6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, udp6_init, NULL);
1312