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