udp6_usrreq.c revision 127711
1/*	$FreeBSD: head/sys/netinet6/udp6_usrreq.c 127711 2004-04-01 13:48:23Z suz $	*/
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/errno.h>
74#include <sys/kernel.h>
75#include <sys/lock.h>
76#include <sys/mbuf.h>
77#include <sys/proc.h>
78#include <sys/protosw.h>
79#include <sys/signalvar.h>
80#include <sys/socket.h>
81#include <sys/socketvar.h>
82#include <sys/stat.h>
83#include <sys/sx.h>
84#include <sys/sysctl.h>
85#include <sys/syslog.h>
86#include <sys/systm.h>
87
88#include <net/if.h>
89#include <net/if_types.h>
90#include <net/route.h>
91
92#include <netinet/in.h>
93#include <netinet/in_pcb.h>
94#include <netinet/in_systm.h>
95#include <netinet/in_var.h>
96#include <netinet/ip.h>
97#include <netinet/ip6.h>
98#include <netinet/icmp6.h>
99#include <netinet/ip_var.h>
100#include <netinet/udp.h>
101#include <netinet/udp_var.h>
102#include <netinet6/ip6protosw.h>
103#include <netinet6/ip6_var.h>
104#include <netinet6/in6_pcb.h>
105#include <netinet6/udp6_var.h>
106
107#ifdef IPSEC
108#include <netinet6/ipsec.h>
109#include <netinet6/ipsec6.h>
110#endif /* IPSEC */
111
112#ifdef FAST_IPSEC
113#include <netipsec/ipsec.h>
114#include <netipsec/ipsec6.h>
115#endif /* FAST_IPSEC */
116
117/*
118 * UDP protocol inplementation.
119 * Per RFC 768, August, 1980.
120 */
121
122extern	struct protosw inetsw[];
123static	int udp6_detach __P((struct socket *so));
124
125int
126udp6_input(mp, offp, proto)
127	struct mbuf **mp;
128	int *offp, proto;
129{
130	struct mbuf *m = *mp, *opts;
131	register struct ip6_hdr *ip6;
132	register struct udphdr *uh;
133	register struct inpcb *in6p;
134	int off = *offp;
135	int plen, ulen;
136	struct sockaddr_in6 fromsa;
137
138	opts = NULL;
139
140	ip6 = mtod(m, struct ip6_hdr *);
141
142	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
143		/* XXX send icmp6 host/port unreach? */
144		m_freem(m);
145		return IPPROTO_DONE;
146	}
147
148#ifndef PULLDOWN_TEST
149	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
150	ip6 = mtod(m, struct ip6_hdr *);
151	uh = (struct udphdr *)((caddr_t)ip6 + off);
152#else
153	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
154	if (!uh)
155		return IPPROTO_DONE;
156#endif
157
158	udpstat.udps_ipackets++;
159
160	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
161	ulen = ntohs((u_short)uh->uh_ulen);
162
163	if (plen != ulen) {
164		udpstat.udps_badlen++;
165		goto bad;
166	}
167
168	/*
169	 * Checksum extended UDP header and data.
170	 */
171	if (uh->uh_sum == 0) {
172		udpstat.udps_nosum++;
173		goto bad;
174	}
175	if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
176		udpstat.udps_badsum++;
177		goto bad;
178	}
179
180	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
181		struct	inpcb *last;
182
183		/*
184		 * Deliver a multicast datagram to all sockets
185		 * for which the local and remote addresses and ports match
186		 * those of the incoming datagram.  This allows more than
187		 * one process to receive multicasts on the same port.
188		 * (This really ought to be done for unicast datagrams as
189		 * well, but that would cause problems with existing
190		 * applications that open both address-specific sockets and
191		 * a wildcard socket listening to the same port -- they would
192		 * end up receiving duplicates of every unicast datagram.
193		 * Those applications open the multiple sockets to overcome an
194		 * inadequacy of the UDP socket interface, but for backwards
195		 * compatibility we avoid the problem here rather than
196		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
197		 */
198
199		/*
200		 * In a case that laddr should be set to the link-local
201		 * address (this happens in RIPng), the multicast address
202		 * specified in the received packet does not match with
203		 * laddr. To cure this situation, the matching is relaxed
204		 * if the receiving interface is the same as one specified
205		 * in the socket and if the destination multicast address
206		 * matches one of the multicast groups specified in the socket.
207		 */
208
209		/*
210		 * Construct sockaddr format source address.
211		 */
212		init_sin6(&fromsa, m);
213		fromsa.sin6_port = uh->uh_sport;
214		/*
215		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
216		 * We need udphdr for IPsec processing so we do that later.
217		 */
218
219		/*
220		 * Locate pcb(s) for datagram.
221		 * (Algorithm copied from raw_intr().)
222		 */
223		last = NULL;
224		LIST_FOREACH(in6p, &udb, inp_list) {
225			if ((in6p->inp_vflag & INP_IPV6) == 0)
226				continue;
227			if (in6p->in6p_lport != uh->uh_dport)
228				continue;
229			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
230				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
231							&ip6->ip6_dst))
232					continue;
233			}
234			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
235				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
236							&ip6->ip6_src) ||
237				    in6p->in6p_fport != uh->uh_sport)
238					continue;
239			}
240
241			if (last != NULL) {
242				struct mbuf *n;
243
244#if defined(IPSEC) || defined(FAST_IPSEC)
245				/*
246				 * Check AH/ESP integrity.
247				 */
248				if (ipsec6_in_reject(m, last)) {
249#ifdef IPSEC
250					ipsec6stat.in_polvio++;
251#endif /* IPSEC */
252					/* do not inject data into pcb */
253				} else
254#endif /*IPSEC || FAST_IPSEC*/
255				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
256					/*
257					 * KAME NOTE: do not
258					 * m_copy(m, offset, ...) above.
259					 * sbappendaddr() expects M_PKTHDR,
260					 * and m_copy() will copy M_PKTHDR
261					 * only if offset is 0.
262					 */
263					if (last->in6p_flags & IN6P_CONTROLOPTS
264					    || last->in6p_socket->so_options & SO_TIMESTAMP)
265						ip6_savecontrol(last, n, &opts);
266
267					m_adj(n, off + sizeof(struct udphdr));
268					if (sbappendaddr(&last->in6p_socket->so_rcv,
269							(struct sockaddr *)&fromsa,
270							n, opts) == 0) {
271						m_freem(n);
272						if (opts)
273							m_freem(opts);
274						udpstat.udps_fullsock++;
275					} else
276						sorwakeup(last->in6p_socket);
277					opts = NULL;
278				}
279			}
280			last = in6p;
281			/*
282			 * Don't look for additional matches if this one does
283			 * not have either the SO_REUSEPORT or SO_REUSEADDR
284			 * socket options set.  This heuristic avoids searching
285			 * through all pcbs in the common case of a non-shared
286			 * port.  It assumes that an application will never
287			 * clear these options after setting them.
288			 */
289			if ((last->in6p_socket->so_options &
290			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
291				break;
292		}
293
294		if (last == NULL) {
295			/*
296			 * No matching pcb found; discard datagram.
297			 * (No need to send an ICMP Port Unreachable
298			 * for a broadcast or multicast datgram.)
299			 */
300			udpstat.udps_noport++;
301			udpstat.udps_noportmcast++;
302			goto bad;
303		}
304#if defined(IPSEC) || defined(FAST_IPSEC)
305		/*
306		 * Check AH/ESP integrity.
307		 */
308		if (ipsec6_in_reject(m, last)) {
309#ifdef IPSEC
310			ipsec6stat.in_polvio++;
311#endif /* IPSEC */
312			goto bad;
313		}
314#endif /*IPSEC || FAST_IPSEC*/
315		if (last->in6p_flags & IN6P_CONTROLOPTS
316		    || last->in6p_socket->so_options & SO_TIMESTAMP)
317			ip6_savecontrol(last, m, &opts);
318
319		m_adj(m, off + sizeof(struct udphdr));
320		if (sbappendaddr(&last->in6p_socket->so_rcv,
321				(struct sockaddr *)&fromsa,
322				m, opts) == 0) {
323			udpstat.udps_fullsock++;
324			goto bad;
325		}
326		sorwakeup(last->in6p_socket);
327		return IPPROTO_DONE;
328	}
329	/*
330	 * Locate pcb for datagram.
331	 */
332	in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
333				  &ip6->ip6_dst, uh->uh_dport, 1,
334				  m->m_pkthdr.rcvif);
335	if (in6p == 0) {
336		if (log_in_vain) {
337			char buf[INET6_ADDRSTRLEN];
338
339			strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
340			log(LOG_INFO,
341			    "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
342			    buf, ntohs(uh->uh_dport),
343			    ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
344		}
345		udpstat.udps_noport++;
346		if (m->m_flags & M_MCAST) {
347			printf("UDP6: M_MCAST is set in a unicast packet.\n");
348			udpstat.udps_noportmcast++;
349			goto bad;
350		}
351		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
352		return IPPROTO_DONE;
353	}
354#if defined(IPSEC) || defined(FAST_IPSEC)
355	/*
356	 * Check AH/ESP integrity.
357	 */
358	if (ipsec6_in_reject(m, in6p)) {
359#ifdef IPSEC
360		ipsec6stat.in_polvio++;
361#endif /* IPSEC */
362		goto bad;
363	}
364#endif /*IPSEC || FAST_IPSEC*/
365
366	/*
367	 * Construct sockaddr format source address.
368	 * Stuff source address and datagram in user buffer.
369	 */
370	init_sin6(&fromsa, m);
371	fromsa.sin6_port = uh->uh_sport;
372	if (in6p->in6p_flags & IN6P_CONTROLOPTS
373	    || in6p->in6p_socket->so_options & SO_TIMESTAMP)
374		ip6_savecontrol(in6p, m, &opts);
375	m_adj(m, off + sizeof(struct udphdr));
376	if (sbappendaddr(&in6p->in6p_socket->so_rcv,
377			(struct sockaddr *)&fromsa, m, opts) == 0) {
378		udpstat.udps_fullsock++;
379		goto bad;
380	}
381	sorwakeup(in6p->in6p_socket);
382	return IPPROTO_DONE;
383bad:
384	if (m)
385		m_freem(m);
386	if (opts)
387		m_freem(opts);
388	return IPPROTO_DONE;
389}
390
391void
392udp6_ctlinput(cmd, sa, d)
393	int cmd;
394	struct sockaddr *sa;
395	void *d;
396{
397	struct udphdr uh;
398	struct ip6_hdr *ip6;
399	struct mbuf *m;
400	int off = 0;
401	struct ip6ctlparam *ip6cp = NULL;
402	const struct sockaddr_in6 *sa6_src = NULL;
403	void *cmdarg;
404	struct inpcb *(*notify) __P((struct inpcb *, int)) = udp_notify;
405	struct udp_portonly {
406		u_int16_t uh_sport;
407		u_int16_t uh_dport;
408	} *uhp;
409
410	if (sa->sa_family != AF_INET6 ||
411	    sa->sa_len != sizeof(struct sockaddr_in6))
412		return;
413
414	if ((unsigned)cmd >= PRC_NCMDS)
415		return;
416	if (PRC_IS_REDIRECT(cmd))
417		notify = in6_rtchange, d = NULL;
418	else if (cmd == PRC_HOSTDEAD)
419		d = NULL;
420	else if (inet6ctlerrmap[cmd] == 0)
421		return;
422
423	/* if the parameter is from icmp6, decode it. */
424	if (d != NULL) {
425		ip6cp = (struct ip6ctlparam *)d;
426		m = ip6cp->ip6c_m;
427		ip6 = ip6cp->ip6c_ip6;
428		off = ip6cp->ip6c_off;
429		cmdarg = ip6cp->ip6c_cmdarg;
430		sa6_src = ip6cp->ip6c_src;
431	} else {
432		m = NULL;
433		ip6 = NULL;
434		cmdarg = NULL;
435		sa6_src = &sa6_any;
436	}
437
438	if (ip6) {
439		/*
440		 * XXX: We assume that when IPV6 is non NULL,
441		 * M and OFF are valid.
442		 */
443
444		/* check if we can safely examine src and dst ports */
445		if (m->m_pkthdr.len < off + sizeof(*uhp))
446			return;
447
448		bzero(&uh, sizeof(uh));
449		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
450
451		(void) in6_pcbnotify(&udb, sa, uh.uh_dport,
452				     (struct sockaddr *)ip6cp->ip6c_src,
453				     uh.uh_sport, cmd, cmdarg, notify);
454	} else
455		(void) in6_pcbnotify(&udb, sa, 0,
456				     (const struct sockaddr *)sa6_src,
457				     0, cmd, cmdarg, notify);
458}
459
460static int
461udp6_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);
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	cru2x(inp->inp_socket->so_cred, &xuc);
489	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
490out:
491	splx(s);
492	return (error);
493}
494
495SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
496	    0, 0,
497	    udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
498
499static int
500udp6_abort(struct socket *so)
501{
502	struct inpcb *inp;
503	int s;
504
505	inp = sotoinpcb(so);
506	if (inp == 0)
507		return EINVAL;	/* ??? possible? panic instead? */
508	soisdisconnected(so);
509	s = splnet();
510	in6_pcbdetach(inp);
511	splx(s);
512	return 0;
513}
514
515static int
516udp6_attach(struct socket *so, int proto, struct thread *td)
517{
518	struct inpcb *inp;
519	int s, error;
520
521	inp = sotoinpcb(so);
522	if (inp != 0)
523		return EINVAL;
524
525	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
526		error = soreserve(so, udp_sendspace, udp_recvspace);
527		if (error)
528			return error;
529	}
530	s = splnet();
531	error = in_pcballoc(so, &udbinfo, "udp6inp");
532	splx(s);
533	if (error)
534		return error;
535	inp = (struct inpcb *)so->so_pcb;
536	inp->inp_vflag |= INP_IPV6;
537	if (!ip6_v6only)
538		inp->inp_vflag |= INP_IPV4;
539	inp->in6p_hops = -1;	/* use kernel default */
540	inp->in6p_cksum = -1;	/* just to be sure */
541	/*
542	 * XXX: ugly!!
543	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
544	 * because the socket may be bound to an IPv6 wildcard address,
545	 * which may match an IPv4-mapped IPv6 address.
546	 */
547	inp->inp_ip_ttl = ip_defttl;
548	return 0;
549}
550
551static int
552udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
553{
554	struct inpcb *inp;
555	int s, error;
556
557	inp = sotoinpcb(so);
558	if (inp == 0)
559		return EINVAL;
560
561	inp->inp_vflag &= ~INP_IPV4;
562	inp->inp_vflag |= INP_IPV6;
563	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
564		struct sockaddr_in6 *sin6_p;
565
566		sin6_p = (struct sockaddr_in6 *)nam;
567
568		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
569			inp->inp_vflag |= INP_IPV4;
570		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
571			struct sockaddr_in sin;
572
573			in6_sin6_2_sin(&sin, sin6_p);
574			inp->inp_vflag |= INP_IPV4;
575			inp->inp_vflag &= ~INP_IPV6;
576			s = splnet();
577			error = in_pcbbind(inp, (struct sockaddr *)&sin,
578			    td->td_ucred);
579			splx(s);
580			return error;
581		}
582	}
583
584	s = splnet();
585	error = in6_pcbbind(inp, nam, td->td_ucred);
586	splx(s);
587	return error;
588}
589
590static int
591udp6_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,
612			    td->td_ucred);
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	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
623		return EISCONN;
624	s = splnet();
625	error = in6_pcbconnect(inp, nam, td->td_ucred);
626	splx(s);
627	if (error == 0) {
628		if (!ip6_v6only) { /* 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
637static int
638udp6_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
652static int
653udp6_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#ifdef INET
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#endif
670
671	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
672		return ENOTCONN;
673
674	s = splnet();
675	in6_pcbdisconnect(inp);
676	inp->in6p_laddr = in6addr_any;
677	splx(s);
678	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
679	return 0;
680}
681
682static int
683udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
684	  struct mbuf *control, struct thread *td)
685{
686	struct inpcb *inp;
687	int error = 0;
688
689	inp = sotoinpcb(so);
690	if (inp == 0) {
691		error = EINVAL;
692		goto bad;
693	}
694
695	if (addr) {
696		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
697			error = EINVAL;
698			goto bad;
699		}
700		if (addr->sa_family != AF_INET6) {
701			error = EAFNOSUPPORT;
702			goto bad;
703		}
704	}
705
706#ifdef INET
707	if (!ip6_v6only) {
708		int hasv4addr;
709		struct sockaddr_in6 *sin6 = 0;
710
711		if (addr == 0)
712			hasv4addr = (inp->inp_vflag & INP_IPV4);
713		else {
714			sin6 = (struct sockaddr_in6 *)addr;
715			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
716				? 1 : 0;
717		}
718		if (hasv4addr) {
719			struct pr_usrreqs *pru;
720
721			if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
722				/*
723				 * since a user of this socket set the
724				 * IPV6_V6ONLY flag, we discard this
725				 * datagram destined to a v4 addr.
726				 */
727				return EINVAL;
728			}
729			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
730			    !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
731				/*
732				 * when remote addr is IPv4-mapped
733				 * address, local addr should not be
734				 * an IPv6 address; since you cannot
735				 * determine how to map IPv6 source
736				 * address to IPv4.
737				 */
738				return EINVAL;
739			}
740			if (sin6)
741				in6_sin6_2_sin_in_sock(addr);
742			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
743			error = ((*pru->pru_send)(so, flags, m, addr, control,
744						  td));
745			/* addr will just be freed in sendit(). */
746			return error;
747		}
748	}
749#endif
750
751	return udp6_output(inp, m, addr, control, td);
752
753  bad:
754	m_freem(m);
755	return (error);
756}
757
758struct pr_usrreqs udp6_usrreqs = {
759	udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
760	pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
761	pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
762	pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
763	in6_mapped_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel
764};
765