udp6_usrreq.c revision 157674
1/*	$FreeBSD: head/sys/netinet6/udp6_usrreq.c 157674 2006-04-12 03:03:47Z rwatson $	*/
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
119extern	struct protosw inetsw[];
120static	void udp6_detach __P((struct socket *so));
121
122int
123udp6_input(mp, offp, proto)
124	struct mbuf **mp;
125	int *offp, proto;
126{
127	struct mbuf *m = *mp, *opts;
128	register struct ip6_hdr *ip6;
129	register struct udphdr *uh;
130	register struct inpcb *in6p;
131	int off = *offp;
132	int plen, ulen;
133	struct sockaddr_in6 fromsa;
134
135	opts = NULL;
136
137	ip6 = mtod(m, struct ip6_hdr *);
138
139	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
140		/* XXX send icmp6 host/port unreach? */
141		m_freem(m);
142		return IPPROTO_DONE;
143	}
144
145#ifndef PULLDOWN_TEST
146	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
147	ip6 = mtod(m, struct ip6_hdr *);
148	uh = (struct udphdr *)((caddr_t)ip6 + off);
149#else
150	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
151	if (!uh)
152		return IPPROTO_DONE;
153#endif
154
155	udpstat.udps_ipackets++;
156
157	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
158	ulen = ntohs((u_short)uh->uh_ulen);
159
160	if (plen != ulen) {
161		udpstat.udps_badlen++;
162		goto bad;
163	}
164
165	/*
166	 * Checksum extended UDP header and data.
167	 */
168	if (uh->uh_sum == 0) {
169		udpstat.udps_nosum++;
170		goto bad;
171	}
172	if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
173		udpstat.udps_badsum++;
174		goto bad;
175	}
176
177	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
178		struct	inpcb *last;
179
180		/*
181		 * Deliver a multicast datagram to all sockets
182		 * for which the local and remote addresses and ports match
183		 * those of the incoming datagram.  This allows more than
184		 * one process to receive multicasts on the same port.
185		 * (This really ought to be done for unicast datagrams as
186		 * well, but that would cause problems with existing
187		 * applications that open both address-specific sockets and
188		 * a wildcard socket listening to the same port -- they would
189		 * end up receiving duplicates of every unicast datagram.
190		 * Those applications open the multiple sockets to overcome an
191		 * inadequacy of the UDP socket interface, but for backwards
192		 * compatibility we avoid the problem here rather than
193		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
194		 */
195
196		/*
197		 * In a case that laddr should be set to the link-local
198		 * address (this happens in RIPng), the multicast address
199		 * specified in the received packet does not match with
200		 * laddr. To cure this situation, the matching is relaxed
201		 * if the receiving interface is the same as one specified
202		 * in the socket and if the destination multicast address
203		 * matches one of the multicast groups specified in the socket.
204		 */
205
206		/*
207		 * Construct sockaddr format source address.
208		 */
209		init_sin6(&fromsa, m);
210		fromsa.sin6_port = uh->uh_sport;
211		/*
212		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
213		 * We need udphdr for IPsec processing so we do that later.
214		 */
215
216		/*
217		 * Locate pcb(s) for datagram.
218		 * (Algorithm copied from raw_intr().)
219		 */
220		last = NULL;
221		LIST_FOREACH(in6p, &udb, inp_list) {
222			if ((in6p->inp_vflag & INP_IPV6) == 0)
223				continue;
224			if (in6p->in6p_lport != uh->uh_dport)
225				continue;
226			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
227				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
228							&ip6->ip6_dst))
229					continue;
230			}
231			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
232				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
233							&ip6->ip6_src) ||
234				    in6p->in6p_fport != uh->uh_sport)
235					continue;
236			}
237
238			if (last != NULL) {
239				struct mbuf *n;
240
241#if defined(IPSEC) || defined(FAST_IPSEC)
242				/*
243				 * Check AH/ESP integrity.
244				 */
245				if (ipsec6_in_reject(m, last)) {
246#ifdef IPSEC
247					ipsec6stat.in_polvio++;
248#endif /* IPSEC */
249					/* do not inject data into pcb */
250				} else
251#endif /*IPSEC || FAST_IPSEC*/
252				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
253					/*
254					 * KAME NOTE: do not
255					 * m_copy(m, offset, ...) above.
256					 * sbappendaddr() expects M_PKTHDR,
257					 * and m_copy() will copy M_PKTHDR
258					 * only if offset is 0.
259					 */
260					if (last->in6p_flags & IN6P_CONTROLOPTS
261					    || last->in6p_socket->so_options & SO_TIMESTAMP)
262						ip6_savecontrol(last, n, &opts);
263
264					m_adj(n, off + sizeof(struct udphdr));
265					if (sbappendaddr(&last->in6p_socket->so_rcv,
266							(struct sockaddr *)&fromsa,
267							n, opts) == 0) {
268						m_freem(n);
269						if (opts)
270							m_freem(opts);
271						udpstat.udps_fullsock++;
272					} else
273						sorwakeup(last->in6p_socket);
274					opts = NULL;
275				}
276			}
277			last = in6p;
278			/*
279			 * Don't look for additional matches if this one does
280			 * not have either the SO_REUSEPORT or SO_REUSEADDR
281			 * socket options set.  This heuristic avoids searching
282			 * through all pcbs in the common case of a non-shared
283			 * port.  It assumes that an application will never
284			 * clear these options after setting them.
285			 */
286			if ((last->in6p_socket->so_options &
287			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
288				break;
289		}
290
291		if (last == NULL) {
292			/*
293			 * No matching pcb found; discard datagram.
294			 * (No need to send an ICMP Port Unreachable
295			 * for a broadcast or multicast datgram.)
296			 */
297			udpstat.udps_noport++;
298			udpstat.udps_noportmcast++;
299			goto bad;
300		}
301#if defined(IPSEC) || defined(FAST_IPSEC)
302		/*
303		 * Check AH/ESP integrity.
304		 */
305		if (ipsec6_in_reject(m, last)) {
306#ifdef IPSEC
307			ipsec6stat.in_polvio++;
308#endif /* IPSEC */
309			goto bad;
310		}
311#endif /*IPSEC || FAST_IPSEC*/
312		if (last->in6p_flags & IN6P_CONTROLOPTS
313		    || last->in6p_socket->so_options & SO_TIMESTAMP)
314			ip6_savecontrol(last, m, &opts);
315
316		m_adj(m, off + sizeof(struct udphdr));
317		if (sbappendaddr(&last->in6p_socket->so_rcv,
318				(struct sockaddr *)&fromsa,
319				m, opts) == 0) {
320			udpstat.udps_fullsock++;
321			goto bad;
322		}
323		sorwakeup(last->in6p_socket);
324		return IPPROTO_DONE;
325	}
326	/*
327	 * Locate pcb for datagram.
328	 */
329	in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
330				  &ip6->ip6_dst, uh->uh_dport, 1,
331				  m->m_pkthdr.rcvif);
332	if (in6p == 0) {
333		if (log_in_vain) {
334			char buf[INET6_ADDRSTRLEN];
335
336			strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
337			log(LOG_INFO,
338			    "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
339			    buf, ntohs(uh->uh_dport),
340			    ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
341		}
342		udpstat.udps_noport++;
343		if (m->m_flags & M_MCAST) {
344			printf("UDP6: M_MCAST is set in a unicast packet.\n");
345			udpstat.udps_noportmcast++;
346			goto bad;
347		}
348		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
349		return IPPROTO_DONE;
350	}
351#if defined(IPSEC) || defined(FAST_IPSEC)
352	/*
353	 * Check AH/ESP integrity.
354	 */
355	if (ipsec6_in_reject(m, in6p)) {
356#ifdef IPSEC
357		ipsec6stat.in_polvio++;
358#endif /* IPSEC */
359		goto bad;
360	}
361#endif /*IPSEC || FAST_IPSEC*/
362
363	/*
364	 * Construct sockaddr format source address.
365	 * Stuff source address and datagram in user buffer.
366	 */
367	init_sin6(&fromsa, m);
368	fromsa.sin6_port = uh->uh_sport;
369	if (in6p->in6p_flags & IN6P_CONTROLOPTS
370	    || in6p->in6p_socket->so_options & SO_TIMESTAMP)
371		ip6_savecontrol(in6p, m, &opts);
372	m_adj(m, off + sizeof(struct udphdr));
373	if (sbappendaddr(&in6p->in6p_socket->so_rcv,
374			(struct sockaddr *)&fromsa, m, opts) == 0) {
375		udpstat.udps_fullsock++;
376		goto bad;
377	}
378	sorwakeup(in6p->in6p_socket);
379	return IPPROTO_DONE;
380bad:
381	if (m)
382		m_freem(m);
383	if (opts)
384		m_freem(opts);
385	return IPPROTO_DONE;
386}
387
388void
389udp6_ctlinput(cmd, sa, d)
390	int cmd;
391	struct sockaddr *sa;
392	void *d;
393{
394	struct udphdr uh;
395	struct ip6_hdr *ip6;
396	struct mbuf *m;
397	int off = 0;
398	struct ip6ctlparam *ip6cp = NULL;
399	const struct sockaddr_in6 *sa6_src = NULL;
400	void *cmdarg;
401	struct inpcb *(*notify) __P((struct inpcb *, int)) = udp_notify;
402	struct udp_portonly {
403		u_int16_t uh_sport;
404		u_int16_t uh_dport;
405	} *uhp;
406
407	if (sa->sa_family != AF_INET6 ||
408	    sa->sa_len != sizeof(struct sockaddr_in6))
409		return;
410
411	if ((unsigned)cmd >= PRC_NCMDS)
412		return;
413	if (PRC_IS_REDIRECT(cmd))
414		notify = in6_rtchange, d = NULL;
415	else if (cmd == PRC_HOSTDEAD)
416		d = NULL;
417	else if (inet6ctlerrmap[cmd] == 0)
418		return;
419
420	/* if the parameter is from icmp6, decode it. */
421	if (d != NULL) {
422		ip6cp = (struct ip6ctlparam *)d;
423		m = ip6cp->ip6c_m;
424		ip6 = ip6cp->ip6c_ip6;
425		off = ip6cp->ip6c_off;
426		cmdarg = ip6cp->ip6c_cmdarg;
427		sa6_src = ip6cp->ip6c_src;
428	} else {
429		m = NULL;
430		ip6 = NULL;
431		cmdarg = NULL;
432		sa6_src = &sa6_any;
433	}
434
435	if (ip6) {
436		/*
437		 * XXX: We assume that when IPV6 is non NULL,
438		 * M and OFF are valid.
439		 */
440
441		/* check if we can safely examine src and dst ports */
442		if (m->m_pkthdr.len < off + sizeof(*uhp))
443			return;
444
445		bzero(&uh, sizeof(uh));
446		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
447
448		(void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
449				     (struct sockaddr *)ip6cp->ip6c_src,
450				     uh.uh_sport, cmd, cmdarg, notify);
451	} else
452		(void) in6_pcbnotify(&udbinfo, sa, 0,
453				     (const struct sockaddr *)sa6_src,
454				     0, cmd, cmdarg, notify);
455}
456
457static int
458udp6_getcred(SYSCTL_HANDLER_ARGS)
459{
460	struct xucred xuc;
461	struct sockaddr_in6 addrs[2];
462	struct inpcb *inp;
463	int error;
464
465	error = suser(req->td);
466	if (error)
467		return (error);
468
469	if (req->newlen != sizeof(addrs))
470		return (EINVAL);
471	if (req->oldlen != sizeof(struct xucred))
472		return (EINVAL);
473	error = SYSCTL_IN(req, addrs, sizeof(addrs));
474	if (error)
475		return (error);
476	if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 ||
477	    (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) {
478		return (error);
479	}
480	INP_INFO_RLOCK(&udbinfo);
481	inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
482				 addrs[1].sin6_port,
483				 &addrs[0].sin6_addr, addrs[0].sin6_port,
484				 1, NULL);
485	if (inp == NULL) {
486		INP_INFO_RUNLOCK(&udbinfo);
487		return (ENOENT);
488	}
489	INP_LOCK(inp);
490	KASSERT(inp->inp_socket != NULL,
491	    ("udp6_getcred: inp_socket == NULL"));
492	cru2x(inp->inp_socket->so_cred, &xuc);
493	INP_UNLOCK(inp);
494	INP_INFO_RUNLOCK(&udbinfo);
495	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
496	return (error);
497}
498
499SYSCTL_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
503static void
504udp6_abort(struct socket *so)
505{
506	struct inpcb *inp;
507
508	inp = sotoinpcb(so);
509	KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
510
511	INP_INFO_WLOCK(&udbinfo);
512	INP_LOCK(inp);
513	soisdisconnected(so);
514	in6_pcbdetach(inp);
515	in6_pcbfree(inp);
516	INP_INFO_WUNLOCK(&udbinfo);
517}
518
519static int
520udp6_attach(struct socket *so, int proto, struct thread *td)
521{
522	struct inpcb *inp;
523	int error;
524
525	inp = sotoinpcb(so);
526	KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
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	INP_INFO_WLOCK(&udbinfo);
534	error = in_pcballoc(so, &udbinfo, "udp6inp");
535	if (error) {
536		INP_INFO_WUNLOCK(&udbinfo);
537		return error;
538	}
539	inp = (struct inpcb *)so->so_pcb;
540	INP_LOCK(inp);
541	INP_INFO_WUNLOCK(&udbinfo);
542	inp->inp_vflag |= INP_IPV6;
543	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
544		inp->inp_vflag |= INP_IPV4;
545	inp->in6p_hops = -1;	/* use kernel default */
546	inp->in6p_cksum = -1;	/* just to be sure */
547	/*
548	 * XXX: ugly!!
549	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
550	 * because the socket may be bound to an IPv6 wildcard address,
551	 * which may match an IPv4-mapped IPv6 address.
552	 */
553	inp->inp_ip_ttl = ip_defttl;
554	INP_UNLOCK(inp);
555	return 0;
556}
557
558static int
559udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
560{
561	struct inpcb *inp;
562	int error;
563
564	inp = sotoinpcb(so);
565	KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
566
567	INP_INFO_WLOCK(&udbinfo);
568	INP_LOCK(inp);
569	inp->inp_vflag &= ~INP_IPV4;
570	inp->inp_vflag |= INP_IPV6;
571	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
572		struct sockaddr_in6 *sin6_p;
573
574		sin6_p = (struct sockaddr_in6 *)nam;
575
576		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
577			inp->inp_vflag |= INP_IPV4;
578		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
579			struct sockaddr_in sin;
580
581			in6_sin6_2_sin(&sin, sin6_p);
582			inp->inp_vflag |= INP_IPV4;
583			inp->inp_vflag &= ~INP_IPV6;
584			error = in_pcbbind(inp, (struct sockaddr *)&sin,
585			    td->td_ucred);
586			goto out;
587		}
588	}
589
590	error = in6_pcbbind(inp, nam, td->td_ucred);
591out:
592	INP_UNLOCK(inp);
593	INP_INFO_WUNLOCK(&udbinfo);
594	return error;
595}
596
597static int
598udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
599{
600	struct inpcb *inp;
601	int error;
602
603	inp = sotoinpcb(so);
604	KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
605
606	INP_INFO_WLOCK(&udbinfo);
607	INP_LOCK(inp);
608	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
609		struct sockaddr_in6 *sin6_p;
610
611		sin6_p = (struct sockaddr_in6 *)nam;
612		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
613			struct sockaddr_in sin;
614
615			if (inp->inp_faddr.s_addr != INADDR_ANY)
616				return EISCONN;
617			in6_sin6_2_sin(&sin, sin6_p);
618			error = in_pcbconnect(inp, (struct sockaddr *)&sin,
619			    td->td_ucred);
620			if (error == 0) {
621				inp->inp_vflag |= INP_IPV4;
622				inp->inp_vflag &= ~INP_IPV6;
623				soisconnected(so);
624			}
625			goto out;
626		}
627	}
628	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
629		error = EISCONN;
630		goto out;
631	}
632	error = in6_pcbconnect(inp, nam, td->td_ucred);
633	if (error == 0) {
634		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
635			/* should be non mapped addr */
636			inp->inp_vflag &= ~INP_IPV4;
637			inp->inp_vflag |= INP_IPV6;
638		}
639		soisconnected(so);
640	}
641out:
642	INP_UNLOCK(inp);
643	INP_INFO_WUNLOCK(&udbinfo);
644	return error;
645}
646
647static void
648udp6_detach(struct socket *so)
649{
650	struct inpcb *inp;
651
652	inp = sotoinpcb(so);
653	KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
654
655	INP_INFO_WLOCK(&udbinfo);
656	INP_LOCK(inp);
657	in6_pcbdetach(inp);
658	in6_pcbfree(inp);
659	INP_INFO_WUNLOCK(&udbinfo);
660}
661
662static int
663udp6_disconnect(struct socket *so)
664{
665	struct inpcb *inp;
666	int error;
667
668	inp = sotoinpcb(so);
669	KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
670
671	INP_INFO_WLOCK(&udbinfo);
672	INP_LOCK(inp);
673
674#ifdef INET
675	if (inp->inp_vflag & INP_IPV4) {
676		struct pr_usrreqs *pru;
677
678		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
679		error = (*pru->pru_disconnect)(so);
680		goto out;
681	}
682#endif
683
684	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
685		error = ENOTCONN;
686		goto out;
687	}
688
689	in6_pcbdisconnect(inp);
690	inp->in6p_laddr = in6addr_any;
691	/* XXXRW: so_state locking? */
692	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
693out:
694	INP_UNLOCK(inp);
695	INP_INFO_WUNLOCK(&udbinfo);
696	return 0;
697}
698
699static int
700udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
701	  struct mbuf *control, struct thread *td)
702{
703	struct inpcb *inp;
704	int error = 0;
705
706	inp = sotoinpcb(so);
707	KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
708
709	INP_INFO_WLOCK(&udbinfo);
710	INP_LOCK(inp);
711	if (addr) {
712		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
713			error = EINVAL;
714			goto bad;
715		}
716		if (addr->sa_family != AF_INET6) {
717			error = EAFNOSUPPORT;
718			goto bad;
719		}
720	}
721
722#ifdef INET
723	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
724		int hasv4addr;
725		struct sockaddr_in6 *sin6 = 0;
726
727		if (addr == 0)
728			hasv4addr = (inp->inp_vflag & INP_IPV4);
729		else {
730			sin6 = (struct sockaddr_in6 *)addr;
731			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
732				? 1 : 0;
733		}
734		if (hasv4addr) {
735			struct pr_usrreqs *pru;
736
737			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
738			    !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
739				/*
740				 * when remote addr is IPv4-mapped
741				 * address, local addr should not be
742				 * an IPv6 address; since you cannot
743				 * determine how to map IPv6 source
744				 * address to IPv4.
745				 */
746				error = EINVAL;
747				goto out;
748			}
749			if (sin6)
750				in6_sin6_2_sin_in_sock(addr);
751			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
752			error = ((*pru->pru_send)(so, flags, m, addr, control,
753						  td));
754			/* addr will just be freed in sendit(). */
755			goto out;
756		}
757	}
758#endif
759
760	error = udp6_output(inp, m, addr, control, td);
761out:
762	INP_UNLOCK(inp);
763	INP_INFO_WUNLOCK(&udbinfo);
764	return error;
765
766  bad:
767	INP_UNLOCK(inp);
768	INP_INFO_WUNLOCK(&udbinfo);
769	m_freem(m);
770	return (error);
771}
772
773struct pr_usrreqs udp6_usrreqs = {
774	.pru_abort =		udp6_abort,
775	.pru_attach =		udp6_attach,
776	.pru_bind =		udp6_bind,
777	.pru_connect =		udp6_connect,
778	.pru_control =		in6_control,
779	.pru_detach =		udp6_detach,
780	.pru_disconnect =	udp6_disconnect,
781	.pru_peeraddr =		in6_mapped_peeraddr,
782	.pru_send =		udp6_send,
783	.pru_shutdown =		udp_shutdown,
784	.pru_sockaddr =		in6_mapped_sockaddr,
785	.pru_sosetlabel =	in_pcbsosetlabel
786};
787