udp6_usrreq.c revision 171609
1/*	$FreeBSD: head/sys/netinet6/udp6_usrreq.c 171609 2007-07-27 08:25:02Z rwatson $	*/
2/*	$KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $	*/
3/*	$KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $	*/
4
5/*-
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34/*-
35 * Copyright (c) 1982, 1986, 1989, 1993
36 *	The Regents of the University of California.
37 * All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 *    notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 *    notice, this list of conditions and the following disclaimer in the
46 *    documentation and/or other materials provided with the distribution.
47 * 4. Neither the name of the University nor the names of its contributors
48 *    may be used to endorse or promote products derived from this software
49 *    without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
64 */
65
66#include "opt_inet.h"
67#include "opt_inet6.h"
68#include "opt_ipsec.h"
69#include "opt_mac.h"
70
71#include <sys/param.h>
72#include <sys/errno.h>
73#include <sys/kernel.h>
74#include <sys/lock.h>
75#include <sys/mbuf.h>
76#include <sys/priv.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/ip_icmp.h>
98#include <netinet/ip6.h>
99#include <netinet/icmp_var.h>
100#include <netinet/icmp6.h>
101#include <netinet/ip_var.h>
102#include <netinet/udp.h>
103#include <netinet/udp_var.h>
104#include <netinet6/ip6protosw.h>
105#include <netinet6/ip6_var.h>
106#include <netinet6/in6_pcb.h>
107#include <netinet6/udp6_var.h>
108#include <netinet6/scope6_var.h>
109
110#ifdef IPSEC
111#include <netipsec/ipsec.h>
112#include <netipsec/ipsec6.h>
113#endif /* IPSEC */
114
115#include <security/mac/mac_framework.h>
116
117/*
118 * UDP protocol inplementation.
119 * Per RFC 768, August, 1980.
120 */
121
122extern struct protosw	inetsw[];
123static void		udp6_detach(struct socket *so);
124
125static void
126udp6_append(struct inpcb *in6p, struct mbuf *n, int off,
127    struct sockaddr_in6 *fromsa)
128{
129	struct socket *so;
130	struct mbuf *opts;
131
132	INP_LOCK_ASSERT(in6p);
133
134#ifdef IPSEC
135	/* Check AH/ESP integrity. */
136	if (ipsec6_in_reject(n, in6p)) {
137		m_freem(n);
138		ipsec6stat.in_polvio++;
139		return;
140	}
141#endif /* IPSEC */
142#ifdef MAC
143	if (mac_check_inpcb_deliver(in6p, n) != 0) {
144		m_freem(n);
145		return;
146	}
147#endif
148	opts = NULL;
149	if (in6p->in6p_flags & IN6P_CONTROLOPTS ||
150	    in6p->inp_socket->so_options & SO_TIMESTAMP)
151		ip6_savecontrol(in6p, n, &opts);
152	m_adj(n, off + sizeof(struct udphdr));
153
154	so = in6p->inp_socket;
155	SOCKBUF_LOCK(&so->so_rcv);
156	if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
157	    opts) == 0) {
158		SOCKBUF_UNLOCK(&so->so_rcv);
159		m_freem(n);
160		if (opts)
161			m_freem(opts);
162		udpstat.udps_fullsock++;
163	} else
164		sorwakeup_locked(so);
165}
166
167int
168udp6_input(struct mbuf **mp, int *offp, int proto)
169{
170	struct mbuf *m = *mp;
171	struct ip6_hdr *ip6;
172	struct udphdr *uh;
173	struct inpcb *in6p;
174	int off = *offp;
175	int plen, ulen;
176	struct sockaddr_in6 fromsa;
177
178	ip6 = mtod(m, struct ip6_hdr *);
179
180	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
181		/* XXX send icmp6 host/port unreach? */
182		m_freem(m);
183		return (IPPROTO_DONE);
184	}
185
186#ifndef PULLDOWN_TEST
187	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
188	ip6 = mtod(m, struct ip6_hdr *);
189	uh = (struct udphdr *)((caddr_t)ip6 + off);
190#else
191	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
192	if (!uh)
193		return (IPPROTO_DONE);
194#endif
195
196	udpstat.udps_ipackets++;
197
198	/*
199	 * Destination port of 0 is illegal, based on RFC768.
200	 */
201	if (uh->uh_dport == 0)
202		goto badunlocked;
203
204	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
205	ulen = ntohs((u_short)uh->uh_ulen);
206
207	if (plen != ulen) {
208		udpstat.udps_badlen++;
209		goto badunlocked;
210	}
211
212	/*
213	 * Checksum extended UDP header and data.
214	 */
215	if (uh->uh_sum == 0) {
216		udpstat.udps_nosum++;
217		goto badunlocked;
218	}
219	if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
220		udpstat.udps_badsum++;
221		goto badunlocked;
222	}
223
224	/*
225	 * Construct sockaddr format source address.
226	 */
227	init_sin6(&fromsa, m);
228	fromsa.sin6_port = uh->uh_sport;
229
230	INP_INFO_RLOCK(&udbinfo);
231	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
232		struct inpcb *last;
233
234		/*
235		 * In the event that laddr should be set to the link-local
236		 * address (this happens in RIPng), the multicast address
237		 * specified in the received packet will not match laddr.  To
238		 * handle this situation, matching is relaxed if the
239		 * receiving interface is the same as one specified in the
240		 * socket and if the destination multicast address matches
241		 * one of the multicast groups specified in the socket.
242		 */
243
244		/*
245		 * KAME note: traditionally we dropped udpiphdr from mbuf
246		 * here.  We need udphdr for IPsec processing so we do that
247		 * later.
248		 */
249		last = NULL;
250		LIST_FOREACH(in6p, &udb, inp_list) {
251			if ((in6p->inp_vflag & INP_IPV6) == 0)
252				continue;
253			if (in6p->in6p_lport != uh->uh_dport)
254				continue;
255			/*
256			 * XXX: Do not check source port of incoming datagram
257			 * unless inp_connect() has been called to bind the
258			 * fport part of the 4-tuple; the source could be
259			 * trying to talk to us with an ephemeral port.
260			 */
261			if (in6p->inp_fport != 0 &&
262			    in6p->inp_fport != uh->uh_sport)
263				continue;
264			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
265				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
266							&ip6->ip6_dst))
267					continue;
268			}
269			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
270				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
271							&ip6->ip6_src) ||
272				    in6p->in6p_fport != uh->uh_sport)
273					continue;
274			}
275
276			if (last != NULL) {
277				struct mbuf *n;
278
279				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
280					INP_LOCK(last);
281					udp6_append(last, n, off, &fromsa);
282					INP_UNLOCK(last);
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
290			 * searching through all pcbs in the common case of a
291			 * non-shared port.  It assumes that an application
292			 * will never 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.  (No need
302			 * to send an ICMP Port Unreachable for a broadcast
303			 * or multicast datgram.)
304			 */
305			udpstat.udps_noport++;
306			udpstat.udps_noportmcast++;
307			goto badheadlocked;
308		}
309		INP_LOCK(last);
310		udp6_append(last, m, off, &fromsa);
311		INP_UNLOCK(last);
312		INP_INFO_RUNLOCK(&udbinfo);
313		return (IPPROTO_DONE);
314	}
315	/*
316	 * Locate pcb for datagram.
317	 */
318	in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
319	    &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
320	if (in6p == NULL) {
321		if (udp_log_in_vain) {
322			char ip6bufs[INET6_ADDRSTRLEN];
323			char ip6bufd[INET6_ADDRSTRLEN];
324
325			log(LOG_INFO,
326			    "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
327			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
328			    ntohs(uh->uh_dport),
329			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
330			    ntohs(uh->uh_sport));
331		}
332		udpstat.udps_noport++;
333		if (m->m_flags & M_MCAST) {
334			printf("UDP6: M_MCAST is set in a unicast packet.\n");
335			udpstat.udps_noportmcast++;
336			goto badheadlocked;
337		}
338		INP_INFO_RUNLOCK(&udbinfo);
339		if (udp_blackhole)
340			goto badunlocked;
341		if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
342			goto badunlocked;
343		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
344		return (IPPROTO_DONE);
345	}
346	INP_LOCK(in6p);
347	udp6_append(in6p, m, off, &fromsa);
348	INP_UNLOCK(in6p);
349	INP_INFO_RUNLOCK(&udbinfo);
350	return (IPPROTO_DONE);
351
352badheadlocked:
353	INP_INFO_RUNLOCK(&udbinfo);
354badunlocked:
355	if (m)
356		m_freem(m);
357	return (IPPROTO_DONE);
358}
359
360void
361udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
362{
363	struct udphdr uh;
364	struct ip6_hdr *ip6;
365	struct mbuf *m;
366	int off = 0;
367	struct ip6ctlparam *ip6cp = NULL;
368	const struct sockaddr_in6 *sa6_src = NULL;
369	void *cmdarg;
370	struct inpcb *(*notify) __P((struct inpcb *, int)) = udp_notify;
371	struct udp_portonly {
372		u_int16_t uh_sport;
373		u_int16_t uh_dport;
374	} *uhp;
375
376	if (sa->sa_family != AF_INET6 ||
377	    sa->sa_len != sizeof(struct sockaddr_in6))
378		return;
379
380	if ((unsigned)cmd >= PRC_NCMDS)
381		return;
382	if (PRC_IS_REDIRECT(cmd))
383		notify = in6_rtchange, d = NULL;
384	else if (cmd == PRC_HOSTDEAD)
385		d = NULL;
386	else if (inet6ctlerrmap[cmd] == 0)
387		return;
388
389	/* if the parameter is from icmp6, decode it. */
390	if (d != NULL) {
391		ip6cp = (struct ip6ctlparam *)d;
392		m = ip6cp->ip6c_m;
393		ip6 = ip6cp->ip6c_ip6;
394		off = ip6cp->ip6c_off;
395		cmdarg = ip6cp->ip6c_cmdarg;
396		sa6_src = ip6cp->ip6c_src;
397	} else {
398		m = NULL;
399		ip6 = NULL;
400		cmdarg = NULL;
401		sa6_src = &sa6_any;
402	}
403
404	if (ip6) {
405		/*
406		 * XXX: We assume that when IPV6 is non NULL,
407		 * M and OFF are valid.
408		 */
409
410		/* Check if we can safely examine src and dst ports. */
411		if (m->m_pkthdr.len < off + sizeof(*uhp))
412			return;
413
414		bzero(&uh, sizeof(uh));
415		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
416
417		(void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
418		    (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
419		    cmdarg, notify);
420	} else
421		(void) in6_pcbnotify(&udbinfo, sa, 0,
422		    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
423}
424
425static int
426udp6_getcred(SYSCTL_HANDLER_ARGS)
427{
428	struct xucred xuc;
429	struct sockaddr_in6 addrs[2];
430	struct inpcb *inp;
431	int error;
432
433	error = priv_check(req->td, PRIV_NETINET_GETCRED);
434	if (error)
435		return (error);
436
437	if (req->newlen != sizeof(addrs))
438		return (EINVAL);
439	if (req->oldlen != sizeof(struct xucred))
440		return (EINVAL);
441	error = SYSCTL_IN(req, addrs, sizeof(addrs));
442	if (error)
443		return (error);
444	if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 ||
445	    (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) {
446		return (error);
447	}
448	INP_INFO_RLOCK(&udbinfo);
449	inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
450	    addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 1,
451	    NULL);
452	if (inp == NULL) {
453		INP_INFO_RUNLOCK(&udbinfo);
454		return (ENOENT);
455	}
456	INP_LOCK(inp);
457	if (inp->inp_socket == NULL) {
458		error = ENOENT;
459		goto out;
460	}
461	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
462	if (error)
463		goto out;
464	cru2x(inp->inp_socket->so_cred, &xuc);
465out:
466	INP_UNLOCK(inp);
467	INP_INFO_RUNLOCK(&udbinfo);
468	if (error == 0)
469		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
470	return (error);
471}
472
473SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
474    0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
475
476#define in6pcb		inpcb
477#define udp6stat	udpstat
478#define udp6s_opackets	udps_opackets
479
480static int
481udp6_output(struct in6pcb *in6p, struct mbuf *m, struct sockaddr *addr6,
482    struct mbuf *control, struct thread *td)
483{
484	u_int32_t ulen = m->m_pkthdr.len;
485	u_int32_t plen = sizeof(struct udphdr) + ulen;
486	struct ip6_hdr *ip6;
487	struct udphdr *udp6;
488	struct in6_addr *laddr, *faddr;
489	struct sockaddr_in6 *sin6 = NULL;
490	struct ifnet *oifp = NULL;
491	int scope_ambiguous = 0;
492	u_short fport;
493	int error = 0;
494	struct ip6_pktopts *optp, opt;
495	int priv;
496	int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
497	int flags;
498	struct sockaddr_in6 tmp;
499
500	INP_LOCK_ASSERT(in6p);
501
502	priv = 0;
503	if (td && !suser(td))
504		priv = 1;
505
506	if (addr6) {
507		/* addr6 has been validated in udp6_send(). */
508		sin6 = (struct sockaddr_in6 *)addr6;
509
510		/* protect *sin6 from overwrites */
511		tmp = *sin6;
512		sin6 = &tmp;
513
514		/*
515		 * Application should provide a proper zone ID or the use of
516		 * default zone IDs should be enabled.  Unfortunately, some
517		 * applications do not behave as it should, so we need a
518		 * workaround.  Even if an appropriate ID is not determined,
519		 * we'll see if we can determine the outgoing interface.  If we
520		 * can, determine the zone ID based on the interface below.
521		 */
522		if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
523			scope_ambiguous = 1;
524		if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
525			return (error);
526	}
527
528	if (control) {
529		if ((error = ip6_setpktopts(control, &opt,
530		    in6p->in6p_outputopts, priv, IPPROTO_UDP)) != 0)
531			goto release;
532		optp = &opt;
533	} else
534		optp = in6p->in6p_outputopts;
535
536	if (sin6) {
537		faddr = &sin6->sin6_addr;
538
539		/*
540		 * IPv4 version of udp_output calls in_pcbconnect in this case,
541		 * which needs splnet and affects performance.
542		 * Since we saw no essential reason for calling in_pcbconnect,
543		 * we get rid of such kind of logic, and call in6_selectsrc
544		 * and in6_pcbsetport in order to fill in the local address
545		 * and the local port.
546		 */
547		if (sin6->sin6_port == 0) {
548			error = EADDRNOTAVAIL;
549			goto release;
550		}
551
552		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
553			/* how about ::ffff:0.0.0.0 case? */
554			error = EISCONN;
555			goto release;
556		}
557
558		fport = sin6->sin6_port; /* allow 0 port */
559
560		if (IN6_IS_ADDR_V4MAPPED(faddr)) {
561			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
562				/*
563				 * I believe we should explicitly discard the
564				 * packet when mapped addresses are disabled,
565				 * rather than send the packet as an IPv6 one.
566				 * If we chose the latter approach, the packet
567				 * might be sent out on the wire based on the
568				 * default route, the situation which we'd
569				 * probably want to avoid.
570				 * (20010421 jinmei@kame.net)
571				 */
572				error = EINVAL;
573				goto release;
574			}
575			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
576			    !IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
577				/*
578				 * when remote addr is an IPv4-mapped address,
579				 * local addr should not be an IPv6 address,
580				 * since you cannot determine how to map IPv6
581				 * source address to IPv4.
582				 */
583				error = EINVAL;
584				goto release;
585			}
586
587			af = AF_INET;
588		}
589
590		if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
591			laddr = in6_selectsrc(sin6, optp, in6p->in6p_moptions,
592			    NULL, &in6p->in6p_laddr, &oifp, &error);
593			if (oifp && scope_ambiguous &&
594			    (error = in6_setscope(&sin6->sin6_addr,
595			    oifp, NULL))) {
596				goto release;
597			}
598		} else
599			laddr = &in6p->in6p_laddr;	/* XXX */
600		if (laddr == NULL) {
601			if (error == 0)
602				error = EADDRNOTAVAIL;
603			goto release;
604		}
605		if (in6p->in6p_lport == 0 &&
606		    (error = in6_pcbsetport(laddr, in6p, td->td_ucred)) != 0)
607			goto release;
608	} else {
609		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
610			error = ENOTCONN;
611			goto release;
612		}
613		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
614			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
615				/*
616				 * XXX: this case would happen when the
617				 * application sets the V6ONLY flag after
618				 * connecting the foreign address.
619				 * Such applications should be fixed,
620				 * so we bark here.
621				 */
622				log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
623				    "option was set for a connected socket\n");
624				error = EINVAL;
625				goto release;
626			} else
627				af = AF_INET;
628		}
629		laddr = &in6p->in6p_laddr;
630		faddr = &in6p->in6p_faddr;
631		fport = in6p->in6p_fport;
632	}
633
634	if (af == AF_INET)
635		hlen = sizeof(struct ip);
636
637	/*
638	 * Calculate data length and get a mbuf
639	 * for UDP and IP6 headers.
640	 */
641	M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
642	if (m == 0) {
643		error = ENOBUFS;
644		goto release;
645	}
646
647	/*
648	 * Stuff checksum and output datagram.
649	 */
650	udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
651	udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
652	udp6->uh_dport = fport;
653	if (plen <= 0xffff)
654		udp6->uh_ulen = htons((u_short)plen);
655	else
656		udp6->uh_ulen = 0;
657	udp6->uh_sum = 0;
658
659	switch (af) {
660	case AF_INET6:
661		ip6 = mtod(m, struct ip6_hdr *);
662		ip6->ip6_flow	= in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
663		ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
664		ip6->ip6_vfc	|= IPV6_VERSION;
665#if 0				/* ip6_plen will be filled in ip6_output. */
666		ip6->ip6_plen	= htons((u_short)plen);
667#endif
668		ip6->ip6_nxt	= IPPROTO_UDP;
669		ip6->ip6_hlim	= in6_selecthlim(in6p, NULL);
670		ip6->ip6_src	= *laddr;
671		ip6->ip6_dst	= *faddr;
672
673		if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
674				sizeof(struct ip6_hdr), plen)) == 0) {
675			udp6->uh_sum = 0xffff;
676		}
677
678		flags = 0;
679
680		udp6stat.udp6s_opackets++;
681		error = ip6_output(m, optp, NULL, flags, in6p->in6p_moptions,
682		    NULL, in6p);
683		break;
684	case AF_INET:
685		error = EAFNOSUPPORT;
686		goto release;
687	}
688	goto releaseopt;
689
690release:
691	m_freem(m);
692
693releaseopt:
694	if (control) {
695		ip6_clearpktopts(&opt, -1);
696		m_freem(control);
697	}
698	return (error);
699}
700
701static void
702udp6_abort(struct socket *so)
703{
704	struct inpcb *inp;
705
706	inp = sotoinpcb(so);
707	KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
708
709#ifdef INET
710	if (inp->inp_vflag & INP_IPV4) {
711		struct pr_usrreqs *pru;
712
713		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
714		(*pru->pru_abort)(so);
715		return;
716	}
717#endif
718
719	INP_INFO_WLOCK(&udbinfo);
720	INP_LOCK(inp);
721	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
722		in6_pcbdisconnect(inp);
723		inp->in6p_laddr = in6addr_any;
724		soisdisconnected(so);
725	}
726	INP_UNLOCK(inp);
727	INP_INFO_WUNLOCK(&udbinfo);
728}
729
730static int
731udp6_attach(struct socket *so, int proto, struct thread *td)
732{
733	struct inpcb *inp;
734	int error;
735
736	inp = sotoinpcb(so);
737	KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
738
739	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
740		error = soreserve(so, udp_sendspace, udp_recvspace);
741		if (error)
742			return (error);
743	}
744	INP_INFO_WLOCK(&udbinfo);
745	error = in_pcballoc(so, &udbinfo);
746	if (error) {
747		INP_INFO_WUNLOCK(&udbinfo);
748		return (error);
749	}
750	inp = (struct inpcb *)so->so_pcb;
751	INP_INFO_WUNLOCK(&udbinfo);
752	inp->inp_vflag |= INP_IPV6;
753	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
754		inp->inp_vflag |= INP_IPV4;
755	inp->in6p_hops = -1;	/* use kernel default */
756	inp->in6p_cksum = -1;	/* just to be sure */
757	/*
758	 * XXX: ugly!!
759	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
760	 * because the socket may be bound to an IPv6 wildcard address,
761	 * which may match an IPv4-mapped IPv6 address.
762	 */
763	inp->inp_ip_ttl = ip_defttl;
764	INP_UNLOCK(inp);
765	return (0);
766}
767
768static int
769udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
770{
771	struct inpcb *inp;
772	int error;
773
774	inp = sotoinpcb(so);
775	KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
776
777	INP_INFO_WLOCK(&udbinfo);
778	INP_LOCK(inp);
779	inp->inp_vflag &= ~INP_IPV4;
780	inp->inp_vflag |= INP_IPV6;
781	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
782		struct sockaddr_in6 *sin6_p;
783
784		sin6_p = (struct sockaddr_in6 *)nam;
785
786		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
787			inp->inp_vflag |= INP_IPV4;
788		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
789			struct sockaddr_in sin;
790
791			in6_sin6_2_sin(&sin, sin6_p);
792			inp->inp_vflag |= INP_IPV4;
793			inp->inp_vflag &= ~INP_IPV6;
794			error = in_pcbbind(inp, (struct sockaddr *)&sin,
795			    td->td_ucred);
796			goto out;
797		}
798	}
799
800	error = in6_pcbbind(inp, nam, td->td_ucred);
801out:
802	INP_UNLOCK(inp);
803	INP_INFO_WUNLOCK(&udbinfo);
804	return (error);
805}
806
807static void
808udp6_close(struct socket *so)
809{
810	struct inpcb *inp;
811
812	inp = sotoinpcb(so);
813	KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
814
815#ifdef INET
816	if (inp->inp_vflag & INP_IPV4) {
817		struct pr_usrreqs *pru;
818
819		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
820		(*pru->pru_disconnect)(so);
821		return;
822	}
823#endif
824	INP_INFO_WLOCK(&udbinfo);
825	INP_LOCK(inp);
826	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
827		in6_pcbdisconnect(inp);
828		inp->in6p_laddr = in6addr_any;
829		soisdisconnected(so);
830	}
831	INP_UNLOCK(inp);
832	INP_INFO_WUNLOCK(&udbinfo);
833}
834
835static int
836udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
837{
838	struct inpcb *inp;
839	int error;
840
841	inp = sotoinpcb(so);
842	KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
843
844	INP_INFO_WLOCK(&udbinfo);
845	INP_LOCK(inp);
846	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
847		struct sockaddr_in6 *sin6_p;
848
849		sin6_p = (struct sockaddr_in6 *)nam;
850		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
851			struct sockaddr_in sin;
852
853			if (inp->inp_faddr.s_addr != INADDR_ANY) {
854				error = EISCONN;
855				goto out;
856			}
857			in6_sin6_2_sin(&sin, sin6_p);
858			error = in_pcbconnect(inp, (struct sockaddr *)&sin,
859			    td->td_ucred);
860			if (error == 0) {
861				inp->inp_vflag |= INP_IPV4;
862				inp->inp_vflag &= ~INP_IPV6;
863				soisconnected(so);
864			}
865			goto out;
866		}
867	}
868	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
869		error = EISCONN;
870		goto out;
871	}
872	error = in6_pcbconnect(inp, nam, td->td_ucred);
873	if (error == 0) {
874		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
875			/* should be non mapped addr */
876			inp->inp_vflag &= ~INP_IPV4;
877			inp->inp_vflag |= INP_IPV6;
878		}
879		soisconnected(so);
880	}
881out:
882	INP_UNLOCK(inp);
883	INP_INFO_WUNLOCK(&udbinfo);
884	return (error);
885}
886
887static void
888udp6_detach(struct socket *so)
889{
890	struct inpcb *inp;
891
892	inp = sotoinpcb(so);
893	KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
894
895	INP_INFO_WLOCK(&udbinfo);
896	INP_LOCK(inp);
897	in6_pcbdetach(inp);
898	in6_pcbfree(inp);
899	INP_INFO_WUNLOCK(&udbinfo);
900}
901
902static int
903udp6_disconnect(struct socket *so)
904{
905	struct inpcb *inp;
906	int error;
907
908	inp = sotoinpcb(so);
909	KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
910
911	INP_INFO_WLOCK(&udbinfo);
912	INP_LOCK(inp);
913
914#ifdef INET
915	if (inp->inp_vflag & INP_IPV4) {
916		struct pr_usrreqs *pru;
917
918		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
919		error = (*pru->pru_disconnect)(so);
920		goto out;
921	}
922#endif
923
924	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
925		error = ENOTCONN;
926		goto out;
927	}
928
929	in6_pcbdisconnect(inp);
930	inp->in6p_laddr = in6addr_any;
931	/* XXXRW: so_state locking? */
932	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
933out:
934	INP_UNLOCK(inp);
935	INP_INFO_WUNLOCK(&udbinfo);
936	return (0);
937}
938
939static int
940udp6_send(struct socket *so, int flags, struct mbuf *m,
941    struct sockaddr *addr, struct mbuf *control, struct thread *td)
942{
943	struct inpcb *inp;
944	int error = 0;
945
946	inp = sotoinpcb(so);
947	KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
948
949	INP_INFO_WLOCK(&udbinfo);
950	INP_LOCK(inp);
951	if (addr) {
952		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
953			error = EINVAL;
954			goto bad;
955		}
956		if (addr->sa_family != AF_INET6) {
957			error = EAFNOSUPPORT;
958			goto bad;
959		}
960	}
961
962#ifdef INET
963	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
964		int hasv4addr;
965		struct sockaddr_in6 *sin6 = 0;
966
967		if (addr == 0)
968			hasv4addr = (inp->inp_vflag & INP_IPV4);
969		else {
970			sin6 = (struct sockaddr_in6 *)addr;
971			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
972			    ? 1 : 0;
973		}
974		if (hasv4addr) {
975			struct pr_usrreqs *pru;
976
977			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
978			    !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
979				/*
980				 * When remote addr is IPv4-mapped address,
981				 * local addr should not be an IPv6 address;
982				 * since you cannot determine how to map IPv6
983				 * source address to IPv4.
984				 */
985				error = EINVAL;
986				goto out;
987			}
988			if (sin6)
989				in6_sin6_2_sin_in_sock(addr);
990			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
991			error = ((*pru->pru_send)(so, flags, m, addr, control,
992			    td));
993			/* addr will just be freed in sendit(). */
994			goto out;
995		}
996	}
997#endif
998#ifdef MAC
999	mac_create_mbuf_from_inpcb(inp, m);
1000#endif
1001	error = udp6_output(inp, m, addr, control, td);
1002out:
1003	INP_UNLOCK(inp);
1004	INP_INFO_WUNLOCK(&udbinfo);
1005	return (error);
1006
1007bad:
1008	INP_UNLOCK(inp);
1009	INP_INFO_WUNLOCK(&udbinfo);
1010	m_freem(m);
1011	return (error);
1012}
1013
1014struct pr_usrreqs udp6_usrreqs = {
1015	.pru_abort =		udp6_abort,
1016	.pru_attach =		udp6_attach,
1017	.pru_bind =		udp6_bind,
1018	.pru_connect =		udp6_connect,
1019	.pru_control =		in6_control,
1020	.pru_detach =		udp6_detach,
1021	.pru_disconnect =	udp6_disconnect,
1022	.pru_peeraddr =		in6_mapped_peeraddr,
1023	.pru_send =		udp6_send,
1024	.pru_shutdown =		udp_shutdown,
1025	.pru_sockaddr =		in6_mapped_sockaddr,
1026	.pru_sosetlabel =	in_pcbsosetlabel,
1027	.pru_close =		udp6_close
1028};
1029