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