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