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