xref: /freebsd/sys/netinet6/udp6_usrreq.c (revision eacee0ff7ec955b32e09515246bd97b6edcd2b0f)
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,
453 				     (struct sockaddr *)ip6cp->ip6c_src,
454 				     uh.uh_sport, cmd, notify);
455 	} else
456 		(void) in6_pcbnotify(&udb, sa, 0, (struct sockaddr *)sa6_src,
457 				     0, cmd, notify);
458 }
459 
460 static int
461 udp6_getcred(SYSCTL_HANDLER_ARGS)
462 {
463 	struct xucred xuc;
464 	struct sockaddr_in6 addrs[2];
465 	struct inpcb *inp;
466 	int error, s;
467 
468 	error = suser(req->td->td_proc);
469 	if (error)
470 		return (error);
471 
472 	if (req->newlen != sizeof(addrs))
473 		return (EINVAL);
474 	if (req->oldlen != sizeof(struct xucred))
475 		return (EINVAL);
476 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
477 	if (error)
478 		return (error);
479 	s = splnet();
480 	inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
481 				 addrs[1].sin6_port,
482 				 &addrs[0].sin6_addr, addrs[0].sin6_port,
483 				 1, NULL);
484 	if (!inp || !inp->inp_socket) {
485 		error = ENOENT;
486 		goto out;
487 	}
488 	bzero(&xuc, sizeof(xuc));
489 	xuc.cr_uid = inp->inp_socket->so_cred->cr_uid;
490 	xuc.cr_ngroups = inp->inp_socket->so_cred->cr_ngroups;
491 	bcopy(inp->inp_socket->so_cred->cr_groups, xuc.cr_groups,
492 	    sizeof(xuc.cr_groups));
493 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
494 out:
495 	splx(s);
496 	return (error);
497 }
498 
499 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
500 	    0, 0,
501 	    udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
502 
503 static int
504 udp6_abort(struct socket *so)
505 {
506 	struct inpcb *inp;
507 	int s;
508 
509 	inp = sotoinpcb(so);
510 	if (inp == 0)
511 		return EINVAL;	/* ??? possible? panic instead? */
512 	soisdisconnected(so);
513 	s = splnet();
514 	in6_pcbdetach(inp);
515 	splx(s);
516 	return 0;
517 }
518 
519 static int
520 udp6_attach(struct socket *so, int proto, struct thread *td)
521 {
522 	struct inpcb *inp;
523 	int s, error;
524 
525 	inp = sotoinpcb(so);
526 	if (inp != 0)
527 		return EINVAL;
528 
529 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
530 		error = soreserve(so, udp_sendspace, udp_recvspace);
531 		if (error)
532 			return error;
533 	}
534 	s = splnet();
535 	error = in_pcballoc(so, &udbinfo, td);
536 	splx(s);
537 	if (error)
538 		return error;
539 	inp = (struct inpcb *)so->so_pcb;
540 	inp->inp_vflag |= INP_IPV6;
541 	inp->in6p_hops = -1;	/* use kernel default */
542 	inp->in6p_cksum = -1;	/* just to be sure */
543 	/*
544 	 * XXX: ugly!!
545 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
546 	 * because the socket may be bound to an IPv6 wildcard address,
547 	 * which may match an IPv4-mapped IPv6 address.
548 	 */
549 	inp->inp_ip_ttl = ip_defttl;
550 	return 0;
551 }
552 
553 static int
554 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
555 {
556 	struct inpcb *inp;
557 	int s, error;
558 
559 	inp = sotoinpcb(so);
560 	if (inp == 0)
561 		return EINVAL;
562 
563 	inp->inp_vflag &= ~INP_IPV4;
564 	inp->inp_vflag |= INP_IPV6;
565 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
566 		struct sockaddr_in6 *sin6_p;
567 
568 		sin6_p = (struct sockaddr_in6 *)nam;
569 
570 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
571 			inp->inp_vflag |= INP_IPV4;
572 		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
573 			struct sockaddr_in sin;
574 
575 			in6_sin6_2_sin(&sin, sin6_p);
576 			inp->inp_vflag |= INP_IPV4;
577 			inp->inp_vflag &= ~INP_IPV6;
578 			s = splnet();
579 			error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
580 			splx(s);
581 			return error;
582 		}
583 	}
584 
585 	s = splnet();
586 	error = in6_pcbbind(inp, nam, td);
587 	splx(s);
588 	return error;
589 }
590 
591 static int
592 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
593 {
594 	struct inpcb *inp;
595 	int s, error;
596 
597 	inp = sotoinpcb(so);
598 	if (inp == 0)
599 		return EINVAL;
600 
601 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
602 		struct sockaddr_in6 *sin6_p;
603 
604 		sin6_p = (struct sockaddr_in6 *)nam;
605 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
606 			struct sockaddr_in sin;
607 
608 			if (inp->inp_faddr.s_addr != INADDR_ANY)
609 				return EISCONN;
610 			in6_sin6_2_sin(&sin, sin6_p);
611 			s = splnet();
612 			error = in_pcbconnect(inp, (struct sockaddr *)&sin, td);
613 			splx(s);
614 			if (error == 0) {
615 				inp->inp_vflag |= INP_IPV4;
616 				inp->inp_vflag &= ~INP_IPV6;
617 				soisconnected(so);
618 			}
619 			return error;
620 		}
621 	}
622 
623 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
624 		return EISCONN;
625 	s = splnet();
626 	error = in6_pcbconnect(inp, nam, td);
627 	splx(s);
628 	if (error == 0) {
629 		if (ip6_mapped_addr_on) { /* should be non mapped addr */
630 			inp->inp_vflag &= ~INP_IPV4;
631 			inp->inp_vflag |= INP_IPV6;
632 		}
633 		soisconnected(so);
634 	}
635 	return error;
636 }
637 
638 static int
639 udp6_detach(struct socket *so)
640 {
641 	struct inpcb *inp;
642 	int s;
643 
644 	inp = sotoinpcb(so);
645 	if (inp == 0)
646 		return EINVAL;
647 	s = splnet();
648 	in6_pcbdetach(inp);
649 	splx(s);
650 	return 0;
651 }
652 
653 static int
654 udp6_disconnect(struct socket *so)
655 {
656 	struct inpcb *inp;
657 	int s;
658 
659 	inp = sotoinpcb(so);
660 	if (inp == 0)
661 		return EINVAL;
662 
663 	if (inp->inp_vflag & INP_IPV4) {
664 		struct pr_usrreqs *pru;
665 
666 		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
667 		return ((*pru->pru_disconnect)(so));
668 	}
669 
670 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
671 		return ENOTCONN;
672 
673 	s = splnet();
674 	in6_pcbdisconnect(inp);
675 	inp->in6p_laddr = in6addr_any;
676 	splx(s);
677 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
678 	return 0;
679 }
680 
681 static int
682 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
683 	  struct mbuf *control, struct thread *td)
684 {
685 	struct inpcb *inp;
686 	int error = 0;
687 
688 	inp = sotoinpcb(so);
689 	if (inp == 0) {
690 		error = EINVAL;
691 		goto bad;
692 	}
693 
694 	if (addr) {
695 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
696 			error = EINVAL;
697 			goto bad;
698 		}
699 		if (addr->sa_family != AF_INET6) {
700 			error = EAFNOSUPPORT;
701 			goto bad;
702 		}
703 	}
704 
705 	if (ip6_mapped_addr_on) {
706 		int hasv4addr;
707 		struct sockaddr_in6 *sin6 = 0;
708 
709 		if (addr == 0)
710 			hasv4addr = (inp->inp_vflag & INP_IPV4);
711 		else {
712 			sin6 = (struct sockaddr_in6 *)addr;
713 			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
714 				? 1 : 0;
715 		}
716 		if (hasv4addr) {
717 			struct pr_usrreqs *pru;
718 
719 			if (sin6)
720 				in6_sin6_2_sin_in_sock(addr);
721 			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
722 			error = ((*pru->pru_send)(so, flags, m, addr, control,
723 						  td));
724 			/* addr will just be freed in sendit(). */
725 			return error;
726 		}
727 	}
728 
729 	return udp6_output(inp, m, addr, control, td);
730 
731   bad:
732 	m_freem(m);
733 	return(error);
734 }
735 
736 struct pr_usrreqs udp6_usrreqs = {
737 	udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
738 	pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
739 	pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
740 	pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
741 	in6_mapped_sockaddr, sosend, soreceive, sopoll
742 };
743