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