udp6_usrreq.c revision 54952
1/*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/*
31 * Copyright (c) 1982, 1986, 1989, 1993
32 *	The Regents of the University of California.  All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 *    notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 *    notice, this list of conditions and the following disclaimer in the
41 *    documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 *    must display the following acknowledgement:
44 *	This product includes software developed by the University of
45 *	California, Berkeley and its contributors.
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 * $FreeBSD: head/sys/netinet6/udp6_usrreq.c 54952 1999-12-21 11:14:12Z eivind $
64 */
65
66#include <sys/param.h>
67#include <sys/kernel.h>
68#include <sys/malloc.h>
69#include <sys/mbuf.h>
70#include <sys/protosw.h>
71#include <sys/socket.h>
72#include <sys/socketvar.h>
73#include <sys/sysctl.h>
74#include <sys/errno.h>
75#include <sys/stat.h>
76#include <sys/systm.h>
77#include <sys/syslog.h>
78#include <sys/proc.h>
79
80#include <net/if.h>
81#include <net/route.h>
82#include <net/if_types.h>
83
84#include <netinet/in.h>
85#include <netinet/in_systm.h>
86#include <netinet/ip.h>
87#include <netinet/in_pcb.h>
88#include <netinet/in_var.h>
89#include <netinet/ip_var.h>
90#include <netinet/udp.h>
91#include <netinet/udp_var.h>
92#include <netinet6/ip6.h>
93#include <netinet6/ip6_var.h>
94#include <netinet6/in6_pcb.h>
95#include <netinet6/icmp6.h>
96#include <netinet6/udp6_var.h>
97#include <netinet6/ip6protosw.h>
98
99#ifdef IPSEC
100#include <netinet6/ipsec.h>
101#endif /*IPSEC*/
102
103#include "faith.h"
104
105/*
106 * UDP protocol inplementation.
107 * Per RFC 768, August, 1980.
108 */
109
110extern	struct protosw inetsw[];
111static	int in6_mcmatch __P((struct inpcb *, struct in6_addr *, struct ifnet *));
112static	int udp6_detach __P((struct socket *so));
113
114static int
115in6_mcmatch(in6p, ia6, ifp)
116	struct inpcb *in6p;
117	register struct in6_addr *ia6;
118	struct ifnet *ifp;
119{
120	struct ip6_moptions *im6o = in6p->in6p_moptions;
121	struct in6_multi_mship *imm;
122
123	if (im6o == NULL)
124		return 0;
125
126	for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
127	     imm = imm->i6mm_chain.le_next) {
128		if ((ifp == NULL ||
129		     imm->i6mm_maddr->in6m_ifp == ifp) &&
130		    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
131				       ia6))
132			return 1;
133	}
134	return 0;
135}
136
137int
138udp6_input(mp, offp, proto)
139	struct mbuf **mp;
140	int *offp, proto;
141{
142	struct mbuf *m = *mp;
143	register struct ip6_hdr *ip6;
144	register struct udphdr *uh;
145	register struct inpcb *in6p;
146	struct	mbuf *opts = 0;
147	int off = *offp;
148	int plen, ulen;
149	struct sockaddr_in6 udp_in6;
150
151#if defined(NFAITH) && 0 < NFAITH
152	if (m->m_pkthdr.rcvif) {
153		if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
154			/* XXX send icmp6 host/port unreach? */
155			m_freem(m);
156			return IPPROTO_DONE;
157		}
158	}
159#endif
160	udpstat.udps_ipackets++;
161
162	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
163
164	ip6 = mtod(m, struct ip6_hdr *);
165	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
166	uh = (struct udphdr *)((caddr_t)ip6 + off);
167	ulen = ntohs((u_short)uh->uh_ulen);
168
169	if (plen != ulen) {
170		udpstat.udps_badlen++;
171		goto bad;
172	}
173
174	/*
175	 * Checksum extended UDP header and data.
176	 */
177	if (uh->uh_sum == 0)
178		udpstat.udps_nosum++;
179	else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
180		udpstat.udps_badsum++;
181		goto bad;
182	}
183
184	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
185		struct	inpcb *last;
186
187		/*
188		 * Deliver a multicast datagram to all sockets
189		 * for which the local and remote addresses and ports match
190		 * those of the incoming datagram.  This allows more than
191		 * one process to receive multicasts on the same port.
192		 * (This really ought to be done for unicast datagrams as
193		 * well, but that would cause problems with existing
194		 * applications that open both address-specific sockets and
195		 * a wildcard socket listening to the same port -- they would
196		 * end up receiving duplicates of every unicast datagram.
197		 * Those applications open the multiple sockets to overcome an
198		 * inadequacy of the UDP socket interface, but for backwards
199		 * compatibility we avoid the problem here rather than
200		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
201		 */
202
203		/*
204		 * In a case that laddr should be set to the link-local
205		 * address (this happens in RIPng), the multicast address
206		 * specified in the received packet does not match with
207		 * laddr. To cure this situation, the matching is relaxed
208		 * if the receiving interface is the same as one specified
209		 * in the socket and if the destination multicast address
210		 * matches one of the multicast groups specified in the socket.
211		 */
212
213		/*
214		 * Construct sockaddr format source address.
215		 */
216		init_sin6(&udp_in6, m); /* general init */
217		udp_in6.sin6_port = uh->uh_sport;
218		/*
219		 * KAME note: usually we drop udphdr from mbuf here.
220		 * We need udphdr for IPsec processing so we do that later.
221		 */
222
223		/*
224		 * Locate pcb(s) for datagram.
225		 * (Algorithm copied from raw_intr().)
226		 */
227		last = NULL;
228		LIST_FOREACH(in6p, &udb, inp_list) {
229			if ((in6p->inp_vflag & INP_IPV6) == 0)
230				continue;
231			if (in6p->in6p_lport != uh->uh_dport)
232				continue;
233			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
234				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
235							&ip6->ip6_dst) &&
236				    !in6_mcmatch(in6p, &ip6->ip6_dst,
237						 m->m_pkthdr.rcvif))
238					continue;
239			}
240			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
241				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
242							&ip6->ip6_src) ||
243				   in6p->in6p_fport != uh->uh_sport)
244					continue;
245			}
246
247			if (last != NULL) {
248				struct	mbuf *n;
249
250#ifdef IPSEC
251				/*
252				 * Check AH/ESP integrity.
253				 */
254				if (last != NULL &&
255				    ipsec6_in_reject_so(m, last->inp_socket)) {
256					ipsec6stat.in_polvio++;
257					/* do not inject data into pcb */
258				} else
259#endif /*IPSEC*/
260				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
261					/*
262					 * KAME NOTE: do not
263					 * m_copy(m, offset, ...) above.
264					 * sbappendaddr() expects M_PKTHDR,
265					 * and m_copy() will copy M_PKTHDR
266					 * only if offset is 0.
267					 */
268					if (last->in6p_flags & IN6P_CONTROLOPTS
269					    || last->in6p_socket->so_options & SO_TIMESTAMP)
270						ip6_savecontrol(last, &opts,
271								ip6, n);
272					m_adj(n, off + sizeof(struct udphdr));
273					if (sbappendaddr(&last->in6p_socket->so_rcv,
274							(struct sockaddr *)&udp_in6,
275							n, opts) == 0) {
276						m_freem(n);
277						if (opts)
278							m_freem(opts);
279						udpstat.udps_fullsock++;
280					} else
281						sorwakeup(last->in6p_socket);
282					opts = 0;
283				}
284			}
285			last = in6p;
286			/*
287			 * Don't look for additional matches if this one does
288			 * not have either the SO_REUSEPORT or SO_REUSEADDR
289			 * socket options set.  This heuristic avoids searching
290			 * through all pcbs in the common case of a non-shared
291			 * port.  It assumes that an application will never
292			 * clear these options after setting them.
293			 */
294			if ((last->in6p_socket->so_options &
295			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
296				break;
297		}
298
299		if (last == NULL) {
300			/*
301			 * No matching pcb found; discard datagram.
302			 * (No need to send an ICMP Port Unreachable
303			 * for a broadcast or multicast datgram.)
304			 */
305			udpstat.udps_noport++;
306			udpstat.udps_noportmcast++;
307			goto bad;
308		}
309#ifdef IPSEC
310		/*
311		 * Check AH/ESP integrity.
312		 */
313		if (last != NULL && ipsec6_in_reject_so(m, last->inp_socket)) {
314			ipsec6stat.in_polvio++;
315			goto bad;
316		}
317#endif /*IPSEC*/
318		if (last->in6p_flags & IN6P_CONTROLOPTS
319		    || last->in6p_socket->so_options & SO_TIMESTAMP)
320			ip6_savecontrol(last, &opts, ip6, m);
321
322		m_adj(m, off + sizeof(struct udphdr));
323		if (sbappendaddr(&last->in6p_socket->so_rcv,
324				(struct sockaddr *)&udp_in6,
325				m, opts) == 0) {
326			udpstat.udps_fullsock++;
327			goto bad;
328		}
329		sorwakeup(last->in6p_socket);
330		return IPPROTO_DONE;
331	}
332	/*
333	 * Locate pcb for datagram.
334	 */
335	in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
336				  &ip6->ip6_dst, uh->uh_dport, 1,
337				  m->m_pkthdr.rcvif);
338	if (in6p == 0) {
339		if (log_in_vain) {
340			char buf[INET6_ADDRSTRLEN];
341
342			strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
343			log(LOG_INFO,
344			    "Connection attempt to UDP %s:%d from %s:%d\n",
345			    buf, ntohs(uh->uh_dport),
346			    ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
347		}
348		udpstat.udps_noport++;
349		if (m->m_flags & M_MCAST) {
350			printf("UDP6: M_MCAST is set in a unicast packet.\n");
351			udpstat.udps_noportmcast++;
352			goto bad;
353		}
354		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
355		return IPPROTO_DONE;
356	}
357#ifdef IPSEC
358	/*
359	 * Check AH/ESP integrity.
360	 */
361	if (in6p != NULL && ipsec6_in_reject_so(m, in6p->in6p_socket)) {
362		ipsec6stat.in_polvio++;
363		goto bad;
364	}
365#endif /*IPSEC*/
366
367	/*
368	 * Construct sockaddr format source address.
369	 * Stuff source address and datagram in user buffer.
370	 */
371	init_sin6(&udp_in6, m); /* general init */
372	udp_in6.sin6_port = uh->uh_sport;
373	if (in6p->in6p_flags & IN6P_CONTROLOPTS
374	    || in6p->in6p_socket->so_options & SO_TIMESTAMP)
375		ip6_savecontrol(in6p, &opts, ip6, m);
376	m_adj(m, off + sizeof(struct udphdr));
377	if (sbappendaddr(&in6p->in6p_socket->so_rcv,
378			(struct sockaddr *)&udp_in6,
379			m, opts) == 0) {
380		udpstat.udps_fullsock++;
381		goto bad;
382	}
383	sorwakeup(in6p->in6p_socket);
384	return IPPROTO_DONE;
385bad:
386	if (m)
387		m_freem(m);
388	if (opts)
389		m_freem(opts);
390	return IPPROTO_DONE;
391}
392
393void
394udp6_ctlinput(cmd, sa, d)
395	int cmd;
396	struct sockaddr *sa;
397	void *d;
398{
399	register struct udphdr *uhp;
400	struct udphdr uh;
401	struct sockaddr_in6 sa6;
402	struct ip6_hdr *ip6;
403	struct mbuf *m;
404	int off;
405
406	if (sa->sa_family != AF_INET6 ||
407	    sa->sa_len != sizeof(struct sockaddr_in6))
408		return;
409
410	off = 0;
411	if (!PRC_IS_REDIRECT(cmd) &&
412	    ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
413		return;
414
415	/* if the parameter is from icmp6, decode it. */
416	if (d != NULL) {
417		struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
418		m = ip6cp->ip6c_m;
419		ip6 = ip6cp->ip6c_ip6;
420		off = ip6cp->ip6c_off;
421	} else {
422		m = NULL;
423		ip6 = NULL;
424	}
425
426	/* translate addresses into internal form */
427	sa6 = *(struct sockaddr_in6 *)sa;
428	if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr))
429		sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
430
431	if (ip6) {
432		/*
433		 * XXX: We assume that when IPV6 is non NULL,
434		 * M and OFF are valid.
435		 */
436		struct in6_addr s;
437
438		/* translate addresses into internal form */
439		memcpy(&s, &ip6->ip6_src, sizeof(s));
440		if (IN6_IS_ADDR_LINKLOCAL(&s))
441			s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
442
443		if (m->m_len < off + sizeof(uh)) {
444			/*
445			 * this should be rare case,
446			 * so we compromise on this copy...
447			 */
448			m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
449			uhp = &uh;
450		} else
451			uhp = (struct udphdr *)(mtod(m, caddr_t) + off);
452		(void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6,
453				     uhp->uh_dport, &s,
454				     uhp->uh_sport, cmd, udp_notify);
455	} else
456		(void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6, 0,
457				     &zeroin6_addr, 0, cmd, udp_notify);
458}
459
460static int
461udp6_getcred SYSCTL_HANDLER_ARGS
462{
463	struct sockaddr_in6 addrs[2];
464	struct inpcb *inp;
465	int error, s;
466
467	error = suser(req->p);
468	if (error)
469		return (error);
470	error = SYSCTL_IN(req, addrs, sizeof(addrs));
471	if (error)
472		return (error);
473	s = splnet();
474	inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
475				 addrs[1].sin6_port,
476				 &addrs[0].sin6_addr, addrs[0].sin6_port,
477				 1, NULL);
478	if (!inp || !inp->inp_socket || !inp->inp_socket->so_cred) {
479		error = ENOENT;
480		goto out;
481	}
482	error = SYSCTL_OUT(req, inp->inp_socket->so_cred,
483			   sizeof(struct ucred));
484
485out:
486	splx(s);
487	return (error);
488}
489
490SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
491	    0, 0,
492	    udp6_getcred, "S,ucred", "Get the ucred of a UDP6 connection");
493
494int
495udp6_output(in6p, m, addr6, control, p)
496	register struct inpcb *in6p;
497	struct mbuf *m;
498	struct sockaddr *addr6;
499	struct mbuf *control;
500	struct proc *p;
501{
502	register int ulen = m->m_pkthdr.len;
503	int plen = sizeof(struct udphdr) + ulen;
504	struct ip6_hdr *ip6;
505	struct udphdr *udp6;
506	struct	in6_addr laddr6;
507	int s = 0, error = 0;
508	struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
509
510	if (control) {
511		if ((error = ip6_setpktoptions(control, &opt, suser(p))) != 0)
512			goto release;
513		in6p->in6p_outputopts = &opt;
514	}
515
516	if (addr6) {
517		laddr6 = in6p->in6p_laddr;
518		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
519			error = EISCONN;
520			goto release;
521		}
522		/*
523		 * Must block input while temporarily connected.
524		 */
525		s = splnet();
526		/*
527		 * XXX: the user might want to overwrite the local address
528		 * via an ancillary data.
529		 */
530		bzero(&in6p->in6p_laddr, sizeof(struct in6_addr));
531		error = in6_pcbconnect(in6p, addr6, p);
532		if (error) {
533			splx(s);
534			goto release;
535		}
536	} else {
537		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
538			error = ENOTCONN;
539			goto release;
540		}
541	}
542	/*
543	 * Calculate data length and get a mbuf
544	 * for UDP and IP6 headers.
545	 */
546	M_PREPEND(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr),
547		  M_DONTWAIT);
548	if (m == 0) {
549		error = ENOBUFS;
550		if (addr6)
551			splx(s);
552		goto release;
553	}
554
555	/*
556	 * Stuff checksum and output datagram.
557	 */
558	ip6 = mtod(m, struct ip6_hdr *);
559	ip6->ip6_flow	= in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
560	ip6->ip6_vfc 	= IPV6_VERSION;
561	/* ip6_plen will be filled in ip6_output. */
562	ip6->ip6_nxt	= IPPROTO_UDP;
563	ip6->ip6_hlim   = in6_selecthlim(in6p,
564					 in6p->in6p_route.ro_rt ?
565					 in6p->in6p_route.ro_rt->rt_ifp :
566					 NULL);
567	ip6->ip6_src	= in6p->in6p_laddr;
568	ip6->ip6_dst	= in6p->in6p_faddr;
569
570	udp6 = (struct udphdr *)(ip6 + 1);
571	udp6->uh_sport = in6p->in6p_lport;
572	udp6->uh_dport = in6p->in6p_fport;
573	udp6->uh_ulen  = htons((u_short)plen);
574	udp6->uh_sum   = 0;
575
576	if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
577					sizeof(struct ip6_hdr), plen)) == 0) {
578		udp6->uh_sum = 0xffff;
579	}
580
581	udpstat.udps_opackets++;
582
583#ifdef IPSEC
584	m->m_pkthdr.rcvif = (struct ifnet *)in6p->in6p_socket;
585#endif /*IPSEC*/
586	error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
587			    0, in6p->in6p_moptions, NULL);
588
589	if (addr6) {
590		in6_pcbdisconnect(in6p);
591		in6p->in6p_laddr = laddr6;
592		splx(s);
593	}
594	goto releaseopt;
595
596release:
597	m_freem(m);
598
599releaseopt:
600	if (control) {
601		in6p->in6p_outputopts = stickyopt;
602		m_freem(control);
603	}
604	return(error);
605}
606
607static int
608udp6_abort(struct socket *so)
609{
610	struct inpcb *inp;
611	int s;
612
613	inp = sotoinpcb(so);
614	if (inp == 0)
615		return EINVAL;	/* ??? possible? panic instead? */
616	soisdisconnected(so);
617	s = splnet();
618	in6_pcbdetach(inp);
619	splx(s);
620	return 0;
621}
622
623static int
624udp6_attach(struct socket *so, int proto, struct proc *p)
625{
626	struct inpcb *inp;
627	int s, error;
628
629	inp = sotoinpcb(so);
630	if (inp != 0)
631		return EINVAL;
632
633	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
634		error = soreserve(so, udp_sendspace, udp_recvspace);
635		if (error)
636			return error;
637	}
638	s = splnet();
639	error = in_pcballoc(so, &udbinfo, p);
640	splx(s);
641	if (error)
642		return error;
643	inp = (struct inpcb *)so->so_pcb;
644	inp->inp_vflag |= INP_IPV6;
645	inp->in6p_hops = -1;	/* use kernel default */
646	inp->in6p_cksum = -1;	/* just to be sure */
647#ifdef IPSEC
648	error = ipsec_init_policy(so, &inp->in6p_sp);
649	if (error != 0) {
650		in6_pcbdetach(inp);
651		return (error);
652	}
653#endif /*IPSEC*/
654	return 0;
655}
656
657static int
658udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
659{
660	struct inpcb *inp;
661	int s, error;
662
663	inp = sotoinpcb(so);
664	if (inp == 0)
665		return EINVAL;
666
667	inp->inp_vflag &= ~INP_IPV4;
668	inp->inp_vflag |= INP_IPV6;
669	if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_BINDV6ONLY) == 0) {
670		struct sockaddr_in6 *sin6_p;
671
672		sin6_p = (struct sockaddr_in6 *)nam;
673
674		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
675			inp->inp_vflag |= INP_IPV4;
676		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
677			struct sockaddr_in sin;
678
679			in6_sin6_2_sin(&sin, sin6_p);
680			inp->inp_vflag |= INP_IPV4;
681			inp->inp_vflag &= ~INP_IPV6;
682			s = splnet();
683			error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
684			splx(s);
685			return error;
686		}
687	}
688
689	s = splnet();
690	error = in6_pcbbind(inp, nam, p);
691	splx(s);
692	return error;
693}
694
695static int
696udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
697{
698	struct inpcb *inp;
699	int s, error;
700
701	inp = sotoinpcb(so);
702	if (inp == 0)
703		return EINVAL;
704
705	if (ip6_mapped_addr_on) {
706		struct sockaddr_in6 *sin6_p;
707
708		sin6_p = (struct sockaddr_in6 *)nam;
709		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
710			struct sockaddr_in sin;
711
712			if (inp->inp_faddr.s_addr != INADDR_ANY)
713				return EISCONN;
714			in6_sin6_2_sin(&sin, sin6_p);
715			s = splnet();
716			error = in_pcbconnect(inp, (struct sockaddr *)&sin, p);
717			splx(s);
718			if (error == 0) {
719				inp->inp_vflag |= INP_IPV4;
720				inp->inp_vflag &= ~INP_IPV6;
721				soisconnected(so);
722			}
723			return error;
724		}
725	}
726
727	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
728		return EISCONN;
729	s = splnet();
730	error = in6_pcbconnect(inp, nam, p);
731	if (ip6_auto_flowlabel) {
732		inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
733		inp->in6p_flowinfo |=
734			(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
735	}
736	splx(s);
737	if (error == 0) {
738		if (ip6_mapped_addr_on) { /* should be non mapped addr */
739			inp->inp_vflag &= ~INP_IPV4;
740			inp->inp_vflag |= INP_IPV6;
741		}
742		soisconnected(so);
743	}
744	return error;
745}
746
747static int
748udp6_detach(struct socket *so)
749{
750	struct inpcb *inp;
751	int s;
752
753	inp = sotoinpcb(so);
754	if (inp == 0)
755		return EINVAL;
756	s = splnet();
757	in6_pcbdetach(inp);
758	splx(s);
759	return 0;
760}
761
762static int
763udp6_disconnect(struct socket *so)
764{
765	struct inpcb *inp;
766	int s;
767
768	inp = sotoinpcb(so);
769	if (inp == 0)
770		return EINVAL;
771
772	if (inp->inp_vflag & INP_IPV4) {
773		struct pr_usrreqs *pru;
774
775		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
776		return ((*pru->pru_disconnect)(so));
777	}
778
779	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
780		return ENOTCONN;
781
782	s = splnet();
783	in6_pcbdisconnect(inp);
784	inp->in6p_laddr = in6addr_any;
785	splx(s);
786	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
787	return 0;
788}
789
790static int
791udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
792	  struct mbuf *control, struct proc *p)
793{
794	struct inpcb *inp;
795
796	inp = sotoinpcb(so);
797	if (inp == 0) {
798		m_freem(m);
799		return EINVAL;
800	}
801
802	if (ip6_mapped_addr_on) {
803		int hasv4addr;
804		struct sockaddr_in6 *sin6 = 0;
805
806		if (addr == 0)
807			hasv4addr = (inp->inp_vflag & INP_IPV4);
808		else {
809			sin6 = (struct sockaddr_in6 *)addr;
810			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
811				? 1 : 0;
812		}
813		if (hasv4addr) {
814			struct pr_usrreqs *pru;
815			int error;
816
817			if (sin6)
818				in6_sin6_2_sin_in_sock(addr);
819			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
820			error = ((*pru->pru_send)(so, flags, m, addr, control,
821						  p));
822			/* addr will just be freed in sendit(). */
823			return error;
824		}
825	}
826
827	return udp6_output(inp, m, addr, control, p);
828}
829
830struct pr_usrreqs udp6_usrreqs = {
831	udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
832	pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
833	pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
834	pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
835	in6_mapped_sockaddr, sosend, soreceive, sopoll
836};
837