xref: /freebsd-src/sys/netinet6/udp6_usrreq.c (revision 3abc9103eb34adbb48853f2361206eba207d6c4f)
1 /*	$FreeBSD$	*/
2 /*	$KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $	*/
3 
4 /*-
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 /*-
34  * Copyright (c) 1982, 1986, 1989, 1993
35  *	The Regents of the University of California.
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 4. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
63  */
64 
65 #include "opt_inet.h"
66 #include "opt_inet6.h"
67 #include "opt_ipsec.h"
68 
69 #include <sys/param.h>
70 #include <sys/errno.h>
71 #include <sys/kernel.h>
72 #include <sys/lock.h>
73 #include <sys/mbuf.h>
74 #include <sys/priv.h>
75 #include <sys/proc.h>
76 #include <sys/protosw.h>
77 #include <sys/signalvar.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/stat.h>
81 #include <sys/sx.h>
82 #include <sys/sysctl.h>
83 #include <sys/syslog.h>
84 #include <sys/systm.h>
85 
86 #include <net/if.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
89 
90 #include <netinet/in.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip.h>
95 #include <netinet/ip6.h>
96 #include <netinet/icmp6.h>
97 #include <netinet/ip_var.h>
98 #include <netinet/udp.h>
99 #include <netinet/udp_var.h>
100 #include <netinet6/ip6protosw.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/in6_pcb.h>
103 #include <netinet6/udp6_var.h>
104 #include <netinet6/scope6_var.h>
105 
106 #ifdef IPSEC
107 #include <netipsec/ipsec.h>
108 #include <netipsec/ipsec6.h>
109 #endif /* IPSEC */
110 
111 /*
112  * UDP protocol inplementation.
113  * Per RFC 768, August, 1980.
114  */
115 
116 extern struct protosw	inetsw[];
117 static void		udp6_detach(struct socket *so);
118 
119 static void
120 udp6_append(struct inpcb *in6p, struct mbuf *n, int off,
121     struct sockaddr_in6 *fromsa)
122 {
123 	struct socket *so;
124 	struct mbuf *opts;
125 
126 	INP_LOCK_ASSERT(in6p);
127 
128 #ifdef IPSEC
129 	/* Check AH/ESP integrity. */
130 	if (ipsec6_in_reject(n, in6p)) {
131 		m_freem(n);
132 		ipsec6stat.in_polvio++;
133 		return;
134 	}
135 #endif /* IPSEC */
136 
137 	opts = NULL;
138 	if (in6p->in6p_flags & IN6P_CONTROLOPTS ||
139 	    in6p->inp_socket->so_options & SO_TIMESTAMP)
140 		ip6_savecontrol(in6p, n, &opts);
141 	m_adj(n, off + sizeof(struct udphdr));
142 
143 	so = in6p->inp_socket;
144 	SOCKBUF_LOCK(&so->so_rcv);
145 	if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
146 	    opts) == 0) {
147 		SOCKBUF_UNLOCK(&so->so_rcv);
148 		m_freem(n);
149 		if (opts)
150 			m_freem(opts);
151 		udpstat.udps_fullsock++;
152 	} else
153 		sorwakeup_locked(so);
154 }
155 
156 int
157 udp6_input(struct mbuf **mp, int *offp, int proto)
158 {
159 	struct mbuf *m = *mp;
160 	struct ip6_hdr *ip6;
161 	struct udphdr *uh;
162 	struct inpcb *in6p;
163 	int off = *offp;
164 	int plen, ulen;
165 	struct sockaddr_in6 fromsa;
166 
167 	ip6 = mtod(m, struct ip6_hdr *);
168 
169 	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
170 		/* XXX send icmp6 host/port unreach? */
171 		m_freem(m);
172 		return (IPPROTO_DONE);
173 	}
174 
175 #ifndef PULLDOWN_TEST
176 	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
177 	ip6 = mtod(m, struct ip6_hdr *);
178 	uh = (struct udphdr *)((caddr_t)ip6 + off);
179 #else
180 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
181 	if (!uh)
182 		return (IPPROTO_DONE);
183 #endif
184 
185 	udpstat.udps_ipackets++;
186 
187 	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
188 	ulen = ntohs((u_short)uh->uh_ulen);
189 
190 	if (plen != ulen) {
191 		udpstat.udps_badlen++;
192 		goto badunlocked;
193 	}
194 
195 	/*
196 	 * Checksum extended UDP header and data.
197 	 */
198 	if (uh->uh_sum == 0) {
199 		udpstat.udps_nosum++;
200 		goto badunlocked;
201 	}
202 	if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
203 		udpstat.udps_badsum++;
204 		goto badunlocked;
205 	}
206 
207 	/*
208 	 * Construct sockaddr format source address.
209 	 */
210 	init_sin6(&fromsa, m);
211 	fromsa.sin6_port = uh->uh_sport;
212 
213 	INP_INFO_RLOCK(&udbinfo);
214 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
215 		struct inpcb *last;
216 
217 		/*
218 		 * In the event that laddr should be set to the link-local
219 		 * address (this happens in RIPng), the multicast address
220 		 * specified in the received packet will not match laddr.  To
221 		 * handle this situation, matching is relaxed if the
222 		 * receiving interface is the same as one specified in the
223 		 * socket and if the destination multicast address matches
224 		 * one of the multicast groups specified in the socket.
225 		 */
226 
227 		/*
228 		 * KAME note: traditionally we dropped udpiphdr from mbuf
229 		 * here.  We need udphdr for IPsec processing so we do that
230 		 * later.
231 		 */
232 		last = NULL;
233 		LIST_FOREACH(in6p, &udb, inp_list) {
234 			if ((in6p->inp_vflag & INP_IPV6) == 0)
235 				continue;
236 			if (in6p->in6p_lport != uh->uh_dport)
237 				continue;
238 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
239 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
240 							&ip6->ip6_dst))
241 					continue;
242 			}
243 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
244 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
245 							&ip6->ip6_src) ||
246 				    in6p->in6p_fport != uh->uh_sport)
247 					continue;
248 			}
249 
250 			if (last != NULL) {
251 				struct mbuf *n;
252 
253 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
254 					INP_LOCK(last);
255 					udp6_append(last, n, off, &fromsa);
256 					INP_UNLOCK(last);
257 				}
258 			}
259 			last = in6p;
260 			/*
261 			 * Don't look for additional matches if this one does
262 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
263 			 * socket options set.  This heuristic avoids
264 			 * searching through all pcbs in the common case of a
265 			 * non-shared port.  It assumes that an application
266 			 * will never clear these options after setting them.
267 			 */
268 			if ((last->in6p_socket->so_options &
269 			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
270 				break;
271 		}
272 
273 		if (last == NULL) {
274 			/*
275 			 * No matching pcb found; discard datagram.  (No need
276 			 * to send an ICMP Port Unreachable for a broadcast
277 			 * or multicast datgram.)
278 			 */
279 			udpstat.udps_noport++;
280 			udpstat.udps_noportmcast++;
281 			goto badheadlocked;
282 		}
283 		INP_LOCK(last);
284 		udp6_append(last, m, off, &fromsa);
285 		INP_UNLOCK(last);
286 		INP_INFO_RUNLOCK(&udbinfo);
287 		return (IPPROTO_DONE);
288 	}
289 	/*
290 	 * Locate pcb for datagram.
291 	 */
292 	in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
293 	    &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
294 	if (in6p == NULL) {
295 		if (udp_log_in_vain) {
296 			char ip6bufs[INET6_ADDRSTRLEN];
297 			char ip6bufd[INET6_ADDRSTRLEN];
298 
299 			log(LOG_INFO,
300 			    "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
301 			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
302 			    ntohs(uh->uh_dport),
303 			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
304 			    ntohs(uh->uh_sport));
305 		}
306 		udpstat.udps_noport++;
307 		if (m->m_flags & M_MCAST) {
308 			printf("UDP6: M_MCAST is set in a unicast packet.\n");
309 			udpstat.udps_noportmcast++;
310 			goto badheadlocked;
311 		}
312 		INP_INFO_RUNLOCK(&udbinfo);
313 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
314 		return (IPPROTO_DONE);
315 	}
316 	INP_LOCK(in6p);
317 	udp6_append(in6p, m, off, &fromsa);
318 	INP_UNLOCK(in6p);
319 	INP_INFO_RUNLOCK(&udbinfo);
320 	return (IPPROTO_DONE);
321 badheadlocked:
322 	INP_INFO_RUNLOCK(&udbinfo);
323 badunlocked:
324 	if (m)
325 		m_freem(m);
326 	return (IPPROTO_DONE);
327 }
328 
329 void
330 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
331 {
332 	struct udphdr uh;
333 	struct ip6_hdr *ip6;
334 	struct mbuf *m;
335 	int off = 0;
336 	struct ip6ctlparam *ip6cp = NULL;
337 	const struct sockaddr_in6 *sa6_src = NULL;
338 	void *cmdarg;
339 	struct inpcb *(*notify) __P((struct inpcb *, int)) = udp_notify;
340 	struct udp_portonly {
341 		u_int16_t uh_sport;
342 		u_int16_t uh_dport;
343 	} *uhp;
344 
345 	if (sa->sa_family != AF_INET6 ||
346 	    sa->sa_len != sizeof(struct sockaddr_in6))
347 		return;
348 
349 	if ((unsigned)cmd >= PRC_NCMDS)
350 		return;
351 	if (PRC_IS_REDIRECT(cmd))
352 		notify = in6_rtchange, d = NULL;
353 	else if (cmd == PRC_HOSTDEAD)
354 		d = NULL;
355 	else if (inet6ctlerrmap[cmd] == 0)
356 		return;
357 
358 	/* if the parameter is from icmp6, decode it. */
359 	if (d != NULL) {
360 		ip6cp = (struct ip6ctlparam *)d;
361 		m = ip6cp->ip6c_m;
362 		ip6 = ip6cp->ip6c_ip6;
363 		off = ip6cp->ip6c_off;
364 		cmdarg = ip6cp->ip6c_cmdarg;
365 		sa6_src = ip6cp->ip6c_src;
366 	} else {
367 		m = NULL;
368 		ip6 = NULL;
369 		cmdarg = NULL;
370 		sa6_src = &sa6_any;
371 	}
372 
373 	if (ip6) {
374 		/*
375 		 * XXX: We assume that when IPV6 is non NULL,
376 		 * M and OFF are valid.
377 		 */
378 
379 		/* Check if we can safely examine src and dst ports. */
380 		if (m->m_pkthdr.len < off + sizeof(*uhp))
381 			return;
382 
383 		bzero(&uh, sizeof(uh));
384 		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
385 
386 		(void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
387 		    (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
388 		    cmdarg, notify);
389 	} else
390 		(void) in6_pcbnotify(&udbinfo, sa, 0,
391 		    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
392 }
393 
394 static int
395 udp6_getcred(SYSCTL_HANDLER_ARGS)
396 {
397 	struct xucred xuc;
398 	struct sockaddr_in6 addrs[2];
399 	struct inpcb *inp;
400 	int error;
401 
402 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
403 	if (error)
404 		return (error);
405 
406 	if (req->newlen != sizeof(addrs))
407 		return (EINVAL);
408 	if (req->oldlen != sizeof(struct xucred))
409 		return (EINVAL);
410 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
411 	if (error)
412 		return (error);
413 	if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 ||
414 	    (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) {
415 		return (error);
416 	}
417 	INP_INFO_RLOCK(&udbinfo);
418 	inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
419 	    addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 1,
420 	    NULL);
421 	if (inp == NULL) {
422 		INP_INFO_RUNLOCK(&udbinfo);
423 		return (ENOENT);
424 	}
425 	INP_LOCK(inp);
426 	KASSERT(inp->inp_socket != NULL,
427 	    ("udp6_getcred: inp_socket == NULL"));
428 	/*
429 	 * XXXRW: There should be a scoping access control check here.
430 	 */
431 	cru2x(inp->inp_socket->so_cred, &xuc);
432 	INP_UNLOCK(inp);
433 	INP_INFO_RUNLOCK(&udbinfo);
434 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
435 	return (error);
436 }
437 
438 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
439     0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
440 
441 static void
442 udp6_abort(struct socket *so)
443 {
444 	struct inpcb *inp;
445 
446 	inp = sotoinpcb(so);
447 	KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
448 
449 #ifdef INET
450 	if (inp->inp_vflag & INP_IPV4) {
451 		struct pr_usrreqs *pru;
452 
453 		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
454 		(*pru->pru_abort)(so);
455 		return;
456 	}
457 #endif
458 
459 	INP_INFO_WLOCK(&udbinfo);
460 	INP_LOCK(inp);
461 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
462 		in6_pcbdisconnect(inp);
463 		inp->in6p_laddr = in6addr_any;
464 		soisdisconnected(so);
465 	}
466 	INP_UNLOCK(inp);
467 	INP_INFO_WUNLOCK(&udbinfo);
468 }
469 
470 static int
471 udp6_attach(struct socket *so, int proto, struct thread *td)
472 {
473 	struct inpcb *inp;
474 	int error;
475 
476 	inp = sotoinpcb(so);
477 	KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
478 
479 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
480 		error = soreserve(so, udp_sendspace, udp_recvspace);
481 		if (error)
482 			return (error);
483 	}
484 	INP_INFO_WLOCK(&udbinfo);
485 	error = in_pcballoc(so, &udbinfo);
486 	if (error) {
487 		INP_INFO_WUNLOCK(&udbinfo);
488 		return (error);
489 	}
490 	inp = (struct inpcb *)so->so_pcb;
491 	INP_INFO_WUNLOCK(&udbinfo);
492 	inp->inp_vflag |= INP_IPV6;
493 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
494 		inp->inp_vflag |= INP_IPV4;
495 	inp->in6p_hops = -1;	/* use kernel default */
496 	inp->in6p_cksum = -1;	/* just to be sure */
497 	/*
498 	 * XXX: ugly!!
499 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
500 	 * because the socket may be bound to an IPv6 wildcard address,
501 	 * which may match an IPv4-mapped IPv6 address.
502 	 */
503 	inp->inp_ip_ttl = ip_defttl;
504 	INP_UNLOCK(inp);
505 	return (0);
506 }
507 
508 static int
509 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
510 {
511 	struct inpcb *inp;
512 	int error;
513 
514 	inp = sotoinpcb(so);
515 	KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
516 
517 	INP_INFO_WLOCK(&udbinfo);
518 	INP_LOCK(inp);
519 	inp->inp_vflag &= ~INP_IPV4;
520 	inp->inp_vflag |= INP_IPV6;
521 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
522 		struct sockaddr_in6 *sin6_p;
523 
524 		sin6_p = (struct sockaddr_in6 *)nam;
525 
526 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
527 			inp->inp_vflag |= INP_IPV4;
528 		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
529 			struct sockaddr_in sin;
530 
531 			in6_sin6_2_sin(&sin, sin6_p);
532 			inp->inp_vflag |= INP_IPV4;
533 			inp->inp_vflag &= ~INP_IPV6;
534 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
535 			    td->td_ucred);
536 			goto out;
537 		}
538 	}
539 
540 	error = in6_pcbbind(inp, nam, td->td_ucred);
541 out:
542 	INP_UNLOCK(inp);
543 	INP_INFO_WUNLOCK(&udbinfo);
544 	return (error);
545 }
546 
547 static void
548 udp6_close(struct socket *so)
549 {
550 	struct inpcb *inp;
551 
552 	inp = sotoinpcb(so);
553 	KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
554 
555 #ifdef INET
556 	if (inp->inp_vflag & INP_IPV4) {
557 		struct pr_usrreqs *pru;
558 
559 		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
560 		(*pru->pru_disconnect)(so);
561 		return;
562 	}
563 #endif
564 	INP_INFO_WLOCK(&udbinfo);
565 	INP_LOCK(inp);
566 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
567 		in6_pcbdisconnect(inp);
568 		inp->in6p_laddr = in6addr_any;
569 		soisdisconnected(so);
570 	}
571 	INP_UNLOCK(inp);
572 	INP_INFO_WUNLOCK(&udbinfo);
573 }
574 
575 static int
576 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
577 {
578 	struct inpcb *inp;
579 	int error;
580 
581 	inp = sotoinpcb(so);
582 	KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
583 
584 	INP_INFO_WLOCK(&udbinfo);
585 	INP_LOCK(inp);
586 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
587 		struct sockaddr_in6 *sin6_p;
588 
589 		sin6_p = (struct sockaddr_in6 *)nam;
590 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
591 			struct sockaddr_in sin;
592 
593 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
594 				error = EISCONN;
595 				goto out;
596 			}
597 			in6_sin6_2_sin(&sin, sin6_p);
598 			error = in_pcbconnect(inp, (struct sockaddr *)&sin,
599 			    td->td_ucred);
600 			if (error == 0) {
601 				inp->inp_vflag |= INP_IPV4;
602 				inp->inp_vflag &= ~INP_IPV6;
603 				soisconnected(so);
604 			}
605 			goto out;
606 		}
607 	}
608 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
609 		error = EISCONN;
610 		goto out;
611 	}
612 	error = in6_pcbconnect(inp, nam, td->td_ucred);
613 	if (error == 0) {
614 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
615 			/* should be non mapped addr */
616 			inp->inp_vflag &= ~INP_IPV4;
617 			inp->inp_vflag |= INP_IPV6;
618 		}
619 		soisconnected(so);
620 	}
621 out:
622 	INP_UNLOCK(inp);
623 	INP_INFO_WUNLOCK(&udbinfo);
624 	return (error);
625 }
626 
627 static void
628 udp6_detach(struct socket *so)
629 {
630 	struct inpcb *inp;
631 
632 	inp = sotoinpcb(so);
633 	KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
634 
635 	INP_INFO_WLOCK(&udbinfo);
636 	INP_LOCK(inp);
637 	in6_pcbdetach(inp);
638 	in6_pcbfree(inp);
639 	INP_INFO_WUNLOCK(&udbinfo);
640 }
641 
642 static int
643 udp6_disconnect(struct socket *so)
644 {
645 	struct inpcb *inp;
646 	int error;
647 
648 	inp = sotoinpcb(so);
649 	KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
650 
651 	INP_INFO_WLOCK(&udbinfo);
652 	INP_LOCK(inp);
653 
654 #ifdef INET
655 	if (inp->inp_vflag & INP_IPV4) {
656 		struct pr_usrreqs *pru;
657 
658 		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
659 		error = (*pru->pru_disconnect)(so);
660 		goto out;
661 	}
662 #endif
663 
664 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
665 		error = ENOTCONN;
666 		goto out;
667 	}
668 
669 	in6_pcbdisconnect(inp);
670 	inp->in6p_laddr = in6addr_any;
671 	/* XXXRW: so_state locking? */
672 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
673 out:
674 	INP_UNLOCK(inp);
675 	INP_INFO_WUNLOCK(&udbinfo);
676 	return (0);
677 }
678 
679 static int
680 udp6_send(struct socket *so, int flags, struct mbuf *m,
681     struct sockaddr *addr, struct mbuf *control, struct thread *td)
682 {
683 	struct inpcb *inp;
684 	int error = 0;
685 
686 	inp = sotoinpcb(so);
687 	KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
688 
689 	INP_INFO_WLOCK(&udbinfo);
690 	INP_LOCK(inp);
691 	if (addr) {
692 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
693 			error = EINVAL;
694 			goto bad;
695 		}
696 		if (addr->sa_family != AF_INET6) {
697 			error = EAFNOSUPPORT;
698 			goto bad;
699 		}
700 	}
701 
702 #ifdef INET
703 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
704 		int hasv4addr;
705 		struct sockaddr_in6 *sin6 = 0;
706 
707 		if (addr == 0)
708 			hasv4addr = (inp->inp_vflag & INP_IPV4);
709 		else {
710 			sin6 = (struct sockaddr_in6 *)addr;
711 			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
712 			    ? 1 : 0;
713 		}
714 		if (hasv4addr) {
715 			struct pr_usrreqs *pru;
716 
717 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
718 			    !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
719 				/*
720 				 * When remote addr is IPv4-mapped address,
721 				 * local addr should not be an IPv6 address;
722 				 * since you cannot determine how to map IPv6
723 				 * source address to IPv4.
724 				 */
725 				error = EINVAL;
726 				goto out;
727 			}
728 			if (sin6)
729 				in6_sin6_2_sin_in_sock(addr);
730 			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
731 			error = ((*pru->pru_send)(so, flags, m, addr, control,
732 			    td));
733 			/* addr will just be freed in sendit(). */
734 			goto out;
735 		}
736 	}
737 #endif
738 
739 	error = udp6_output(inp, m, addr, control, td);
740 out:
741 	INP_UNLOCK(inp);
742 	INP_INFO_WUNLOCK(&udbinfo);
743 	return (error);
744 
745 bad:
746 	INP_UNLOCK(inp);
747 	INP_INFO_WUNLOCK(&udbinfo);
748 	m_freem(m);
749 	return (error);
750 }
751 
752 struct pr_usrreqs udp6_usrreqs = {
753 	.pru_abort =		udp6_abort,
754 	.pru_attach =		udp6_attach,
755 	.pru_bind =		udp6_bind,
756 	.pru_connect =		udp6_connect,
757 	.pru_control =		in6_control,
758 	.pru_detach =		udp6_detach,
759 	.pru_disconnect =	udp6_disconnect,
760 	.pru_peeraddr =		in6_mapped_peeraddr,
761 	.pru_send =		udp6_send,
762 	.pru_shutdown =		udp_shutdown,
763 	.pru_sockaddr =		in6_mapped_sockaddr,
764 	.pru_sosetlabel =	in_pcbsosetlabel,
765 	.pru_close =		udp6_close
766 };
767