udp6_usrreq.c revision 272628
1/*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * Copyright (c) 2010-2011 Juniper Networks, Inc.
4 * Copyright (c) 2014 Kevin Lo
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Robert N. M. Watson under
8 * contract to Juniper Networks, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 *	$KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
35 *	$KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
36 */
37
38/*-
39 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
40 *	The Regents of the University of California.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 *    notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 *    notice, this list of conditions and the following disclaimer in the
50 *    documentation and/or other materials provided with the distribution.
51 * 4. Neither the name of the University nor the names of its contributors
52 *    may be used to endorse or promote products derived from this software
53 *    without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
68 */
69
70#include <sys/cdefs.h>
71__FBSDID("$FreeBSD: stable/10/sys/netinet6/udp6_usrreq.c 272628 2014-10-06 13:16:37Z tuexen $");
72
73#include "opt_inet.h"
74#include "opt_inet6.h"
75#include "opt_ipfw.h"
76#include "opt_ipsec.h"
77#include "opt_kdtrace.h"
78
79#include <sys/param.h>
80#include <sys/jail.h>
81#include <sys/kernel.h>
82#include <sys/lock.h>
83#include <sys/mbuf.h>
84#include <sys/priv.h>
85#include <sys/proc.h>
86#include <sys/protosw.h>
87#include <sys/sdt.h>
88#include <sys/signalvar.h>
89#include <sys/socket.h>
90#include <sys/socketvar.h>
91#include <sys/sx.h>
92#include <sys/sysctl.h>
93#include <sys/syslog.h>
94#include <sys/systm.h>
95
96#include <net/if.h>
97#include <net/if_types.h>
98#include <net/route.h>
99
100#include <netinet/in.h>
101#include <netinet/in_kdtrace.h>
102#include <netinet/in_pcb.h>
103#include <netinet/in_systm.h>
104#include <netinet/in_var.h>
105#include <netinet/ip.h>
106#include <netinet/ip_icmp.h>
107#include <netinet/ip6.h>
108#include <netinet/icmp_var.h>
109#include <netinet/icmp6.h>
110#include <netinet/ip_var.h>
111#include <netinet/udp.h>
112#include <netinet/udp_var.h>
113#include <netinet/udplite.h>
114
115#include <netinet6/ip6protosw.h>
116#include <netinet6/ip6_var.h>
117#include <netinet6/in6_pcb.h>
118#include <netinet6/udp6_var.h>
119#include <netinet6/scope6_var.h>
120
121#ifdef IPSEC
122#include <netipsec/ipsec.h>
123#include <netipsec/ipsec6.h>
124#endif /* IPSEC */
125
126#include <security/mac/mac_framework.h>
127
128/*
129 * UDP protocol implementation.
130 * Per RFC 768, August, 1980.
131 */
132
133extern struct protosw	inetsw[];
134static void		udp6_detach(struct socket *so);
135
136static void
137udp6_append(struct inpcb *inp, struct mbuf *n, int off,
138    struct sockaddr_in6 *fromsa)
139{
140	struct socket *so;
141	struct mbuf *opts;
142
143	INP_LOCK_ASSERT(inp);
144
145#ifdef IPSEC
146	/* Check AH/ESP integrity. */
147	if (ipsec6_in_reject(n, inp)) {
148		m_freem(n);
149		IPSEC6STAT_INC(ips_in_polvio);
150		return;
151	}
152#endif /* IPSEC */
153#ifdef MAC
154	if (mac_inpcb_check_deliver(inp, n) != 0) {
155		m_freem(n);
156		return;
157	}
158#endif
159	opts = NULL;
160	if (inp->inp_flags & INP_CONTROLOPTS ||
161	    inp->inp_socket->so_options & SO_TIMESTAMP)
162		ip6_savecontrol(inp, n, &opts);
163	m_adj(n, off + sizeof(struct udphdr));
164
165	so = inp->inp_socket;
166	SOCKBUF_LOCK(&so->so_rcv);
167	if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
168	    opts) == 0) {
169		SOCKBUF_UNLOCK(&so->so_rcv);
170		m_freem(n);
171		if (opts)
172			m_freem(opts);
173		UDPSTAT_INC(udps_fullsock);
174	} else
175		sorwakeup_locked(so);
176}
177
178int
179udp6_input(struct mbuf **mp, int *offp, int proto)
180{
181	struct mbuf *m = *mp;
182	struct ifnet *ifp;
183	struct ip6_hdr *ip6;
184	struct udphdr *uh;
185	struct inpcb *inp;
186	struct inpcbinfo *pcbinfo;
187	struct udpcb *up;
188	int off = *offp;
189	int cscov_partial;
190	int plen, ulen;
191	struct sockaddr_in6 fromsa;
192	struct m_tag *fwd_tag;
193	uint16_t uh_sum;
194	uint8_t nxt;
195
196	ifp = m->m_pkthdr.rcvif;
197	ip6 = mtod(m, struct ip6_hdr *);
198
199	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
200		/* XXX send icmp6 host/port unreach? */
201		m_freem(m);
202		return (IPPROTO_DONE);
203	}
204
205#ifndef PULLDOWN_TEST
206	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
207	ip6 = mtod(m, struct ip6_hdr *);
208	uh = (struct udphdr *)((caddr_t)ip6 + off);
209#else
210	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
211	if (!uh)
212		return (IPPROTO_DONE);
213#endif
214
215	UDPSTAT_INC(udps_ipackets);
216
217	/*
218	 * Destination port of 0 is illegal, based on RFC768.
219	 */
220	if (uh->uh_dport == 0)
221		goto badunlocked;
222
223	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
224	ulen = ntohs((u_short)uh->uh_ulen);
225
226	nxt = ip6->ip6_nxt;
227	cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
228	if (nxt == IPPROTO_UDPLITE && ulen == 0) {
229		/* Zero means checksum over the complete packet. */
230		ulen = plen;
231		cscov_partial = 0;
232	}
233	if (nxt == IPPROTO_UDP && plen != ulen) {
234		UDPSTAT_INC(udps_badlen);
235		goto badunlocked;
236	}
237
238	/*
239	 * Checksum extended UDP header and data.
240	 */
241	if (uh->uh_sum == 0) {
242		if (ulen > plen || ulen < sizeof(struct udphdr)) {
243			UDPSTAT_INC(udps_nosum);
244			goto badunlocked;
245		}
246	}
247
248	if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
249	    !cscov_partial) {
250		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
251			uh_sum = m->m_pkthdr.csum_data;
252		else
253			uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
254			    m->m_pkthdr.csum_data);
255		uh_sum ^= 0xffff;
256	} else
257		uh_sum = in6_cksum(m, nxt, off, ulen);
258
259	if (uh_sum != 0) {
260		UDPSTAT_INC(udps_badsum);
261		goto badunlocked;
262	}
263
264	/*
265	 * Construct sockaddr format source address.
266	 */
267	init_sin6(&fromsa, m);
268	fromsa.sin6_port = uh->uh_sport;
269
270	pcbinfo = get_inpcbinfo(nxt);
271	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
272		struct inpcb *last;
273		struct inpcbhead *pcblist;
274		struct ip6_moptions *imo;
275
276		INP_INFO_RLOCK(pcbinfo);
277		/*
278		 * In the event that laddr should be set to the link-local
279		 * address (this happens in RIPng), the multicast address
280		 * specified in the received packet will not match laddr.  To
281		 * handle this situation, matching is relaxed if the
282		 * receiving interface is the same as one specified in the
283		 * socket and if the destination multicast address matches
284		 * one of the multicast groups specified in the socket.
285		 */
286
287		/*
288		 * KAME note: traditionally we dropped udpiphdr from mbuf
289		 * here.  We need udphdr for IPsec processing so we do that
290		 * later.
291		 */
292		pcblist = get_pcblist(nxt);
293		last = NULL;
294		LIST_FOREACH(inp, pcblist, inp_list) {
295			if ((inp->inp_vflag & INP_IPV6) == 0)
296				continue;
297			if (inp->inp_lport != uh->uh_dport)
298				continue;
299			if (inp->inp_fport != 0 &&
300			    inp->inp_fport != uh->uh_sport)
301				continue;
302			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
303				if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
304							&ip6->ip6_dst))
305					continue;
306			}
307			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
308				if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
309							&ip6->ip6_src) ||
310				    inp->inp_fport != uh->uh_sport)
311					continue;
312			}
313
314			/*
315			 * XXXRW: Because we weren't holding either the inpcb
316			 * or the hash lock when we checked for a match
317			 * before, we should probably recheck now that the
318			 * inpcb lock is (supposed to be) held.
319			 */
320
321			/*
322			 * Handle socket delivery policy for any-source
323			 * and source-specific multicast. [RFC3678]
324			 */
325			imo = inp->in6p_moptions;
326			if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
327				struct sockaddr_in6	 mcaddr;
328				int			 blocked;
329
330				INP_RLOCK(inp);
331
332				bzero(&mcaddr, sizeof(struct sockaddr_in6));
333				mcaddr.sin6_len = sizeof(struct sockaddr_in6);
334				mcaddr.sin6_family = AF_INET6;
335				mcaddr.sin6_addr = ip6->ip6_dst;
336
337				blocked = im6o_mc_filter(imo, ifp,
338					(struct sockaddr *)&mcaddr,
339					(struct sockaddr *)&fromsa);
340				if (blocked != MCAST_PASS) {
341					if (blocked == MCAST_NOTGMEMBER)
342						IP6STAT_INC(ip6s_notmember);
343					if (blocked == MCAST_NOTSMEMBER ||
344					    blocked == MCAST_MUTED)
345						UDPSTAT_INC(udps_filtermcast);
346					INP_RUNLOCK(inp); /* XXX */
347					continue;
348				}
349
350				INP_RUNLOCK(inp);
351			}
352			if (last != NULL) {
353				struct mbuf *n;
354
355				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
356					INP_RLOCK(last);
357					up = intoudpcb(last);
358					if (up->u_tun_func == NULL) {
359						udp6_append(last, n, off, &fromsa);
360					} else {
361						/*
362						 * Engage the tunneling
363						 * protocol we will have to
364						 * leave the info_lock up,
365						 * since we are hunting
366						 * through multiple UDP's.
367						 *
368						 */
369						(*up->u_tun_func)(n, off, last);
370					}
371					INP_RUNLOCK(last);
372				}
373			}
374			last = inp;
375			/*
376			 * Don't look for additional matches if this one does
377			 * not have either the SO_REUSEPORT or SO_REUSEADDR
378			 * socket options set.  This heuristic avoids
379			 * searching through all pcbs in the common case of a
380			 * non-shared port.  It assumes that an application
381			 * will never clear these options after setting them.
382			 */
383			if ((last->inp_socket->so_options &
384			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
385				break;
386		}
387
388		if (last == NULL) {
389			/*
390			 * No matching pcb found; discard datagram.  (No need
391			 * to send an ICMP Port Unreachable for a broadcast
392			 * or multicast datgram.)
393			 */
394			UDPSTAT_INC(udps_noport);
395			UDPSTAT_INC(udps_noportmcast);
396			goto badheadlocked;
397		}
398		INP_RLOCK(last);
399		INP_INFO_RUNLOCK(pcbinfo);
400		up = intoudpcb(last);
401		UDP_PROBE(receive, NULL, last, ip6, last, uh);
402		if (up->u_tun_func == NULL) {
403			udp6_append(last, m, off, &fromsa);
404		} else {
405			/*
406			 * Engage the tunneling protocol.
407			 */
408			(*up->u_tun_func)(m, off, last);
409		}
410		INP_RUNLOCK(last);
411		return (IPPROTO_DONE);
412	}
413	/*
414	 * Locate pcb for datagram.
415	 */
416
417	/*
418	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
419	 */
420	if ((m->m_flags & M_IP6_NEXTHOP) &&
421	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
422		struct sockaddr_in6 *next_hop6;
423
424		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
425
426		/*
427		 * Transparently forwarded. Pretend to be the destination.
428		 * Already got one like this?
429		 */
430		inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
431		    uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
432		    INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
433		if (!inp) {
434			/*
435			 * It's new.  Try to find the ambushing socket.
436			 * Because we've rewritten the destination address,
437			 * any hardware-generated hash is ignored.
438			 */
439			inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
440			    uh->uh_sport, &next_hop6->sin6_addr,
441			    next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
442			    uh->uh_dport, INPLOOKUP_WILDCARD |
443			    INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
444		}
445		/* Remove the tag from the packet. We don't need it anymore. */
446		m_tag_delete(m, fwd_tag);
447		m->m_flags &= ~M_IP6_NEXTHOP;
448	} else
449		inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
450		    uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
451		    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
452		    m->m_pkthdr.rcvif, m);
453	if (inp == NULL) {
454		if (udp_log_in_vain) {
455			char ip6bufs[INET6_ADDRSTRLEN];
456			char ip6bufd[INET6_ADDRSTRLEN];
457
458			log(LOG_INFO,
459			    "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
460			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
461			    ntohs(uh->uh_dport),
462			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
463			    ntohs(uh->uh_sport));
464		}
465		UDPSTAT_INC(udps_noport);
466		if (m->m_flags & M_MCAST) {
467			printf("UDP6: M_MCAST is set in a unicast packet.\n");
468			UDPSTAT_INC(udps_noportmcast);
469			goto badunlocked;
470		}
471		if (V_udp_blackhole)
472			goto badunlocked;
473		if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
474			goto badunlocked;
475		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
476		return (IPPROTO_DONE);
477	}
478	INP_RLOCK_ASSERT(inp);
479	up = intoudpcb(inp);
480	if (cscov_partial) {
481		if (up->u_rxcslen > ulen) {
482			INP_RUNLOCK(inp);
483			m_freem(m);
484			return (IPPROTO_DONE);
485		}
486	}
487	UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
488	if (up->u_tun_func == NULL) {
489		udp6_append(inp, m, off, &fromsa);
490	} else {
491		/*
492		 * Engage the tunneling protocol.
493		 */
494
495		(*up->u_tun_func)(m, off, inp);
496	}
497	INP_RUNLOCK(inp);
498	return (IPPROTO_DONE);
499
500badheadlocked:
501	INP_INFO_RUNLOCK(pcbinfo);
502badunlocked:
503	if (m)
504		m_freem(m);
505	return (IPPROTO_DONE);
506}
507
508static void
509udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d,
510    struct inpcbinfo *pcbinfo)
511{
512	struct udphdr uh;
513	struct ip6_hdr *ip6;
514	struct mbuf *m;
515	int off = 0;
516	struct ip6ctlparam *ip6cp = NULL;
517	const struct sockaddr_in6 *sa6_src = NULL;
518	void *cmdarg;
519	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
520	struct udp_portonly {
521		u_int16_t uh_sport;
522		u_int16_t uh_dport;
523	} *uhp;
524
525	if (sa->sa_family != AF_INET6 ||
526	    sa->sa_len != sizeof(struct sockaddr_in6))
527		return;
528
529	if ((unsigned)cmd >= PRC_NCMDS)
530		return;
531	if (PRC_IS_REDIRECT(cmd))
532		notify = in6_rtchange, d = NULL;
533	else if (cmd == PRC_HOSTDEAD)
534		d = NULL;
535	else if (inet6ctlerrmap[cmd] == 0)
536		return;
537
538	/* if the parameter is from icmp6, decode it. */
539	if (d != NULL) {
540		ip6cp = (struct ip6ctlparam *)d;
541		m = ip6cp->ip6c_m;
542		ip6 = ip6cp->ip6c_ip6;
543		off = ip6cp->ip6c_off;
544		cmdarg = ip6cp->ip6c_cmdarg;
545		sa6_src = ip6cp->ip6c_src;
546	} else {
547		m = NULL;
548		ip6 = NULL;
549		cmdarg = NULL;
550		sa6_src = &sa6_any;
551	}
552
553	if (ip6) {
554		/*
555		 * XXX: We assume that when IPV6 is non NULL,
556		 * M and OFF are valid.
557		 */
558
559		/* Check if we can safely examine src and dst ports. */
560		if (m->m_pkthdr.len < off + sizeof(*uhp))
561			return;
562
563		bzero(&uh, sizeof(uh));
564		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
565
566		(void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport,
567		    (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
568		    cmdarg, notify);
569	} else
570		(void)in6_pcbnotify(pcbinfo, sa, 0,
571		    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
572}
573
574void
575udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
576{
577
578	return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo));
579}
580
581void
582udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d)
583{
584
585	return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo));
586}
587
588static int
589udp6_getcred(SYSCTL_HANDLER_ARGS)
590{
591	struct xucred xuc;
592	struct sockaddr_in6 addrs[2];
593	struct inpcb *inp;
594	int error;
595
596	error = priv_check(req->td, PRIV_NETINET_GETCRED);
597	if (error)
598		return (error);
599
600	if (req->newlen != sizeof(addrs))
601		return (EINVAL);
602	if (req->oldlen != sizeof(struct xucred))
603		return (EINVAL);
604	error = SYSCTL_IN(req, addrs, sizeof(addrs));
605	if (error)
606		return (error);
607	if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
608	    (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
609		return (error);
610	}
611	inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
612	    addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
613	    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
614	if (inp != NULL) {
615		INP_RLOCK_ASSERT(inp);
616		if (inp->inp_socket == NULL)
617			error = ENOENT;
618		if (error == 0)
619			error = cr_canseesocket(req->td->td_ucred,
620			    inp->inp_socket);
621		if (error == 0)
622			cru2x(inp->inp_cred, &xuc);
623		INP_RUNLOCK(inp);
624	} else
625		error = ENOENT;
626	if (error == 0)
627		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
628	return (error);
629}
630
631SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
632    0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
633
634static int
635udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
636    struct mbuf *control, struct thread *td)
637{
638	u_int32_t ulen = m->m_pkthdr.len;
639	u_int32_t plen = sizeof(struct udphdr) + ulen;
640	struct ip6_hdr *ip6;
641	struct udphdr *udp6;
642	struct in6_addr *laddr, *faddr, in6a;
643	struct sockaddr_in6 *sin6 = NULL;
644	struct ifnet *oifp = NULL;
645	int cscov_partial = 0;
646	int scope_ambiguous = 0;
647	u_short fport;
648	int error = 0;
649	uint8_t nxt;
650	uint16_t cscov = 0;
651	struct ip6_pktopts *optp, opt;
652	int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
653	int flags;
654	struct sockaddr_in6 tmp;
655
656	INP_WLOCK_ASSERT(inp);
657	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
658
659	if (addr6) {
660		/* addr6 has been validated in udp6_send(). */
661		sin6 = (struct sockaddr_in6 *)addr6;
662
663		/* protect *sin6 from overwrites */
664		tmp = *sin6;
665		sin6 = &tmp;
666
667		/*
668		 * Application should provide a proper zone ID or the use of
669		 * default zone IDs should be enabled.  Unfortunately, some
670		 * applications do not behave as it should, so we need a
671		 * workaround.  Even if an appropriate ID is not determined,
672		 * we'll see if we can determine the outgoing interface.  If we
673		 * can, determine the zone ID based on the interface below.
674		 */
675		if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
676			scope_ambiguous = 1;
677		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
678			return (error);
679	}
680
681	if (control) {
682		if ((error = ip6_setpktopts(control, &opt,
683		    inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
684			goto release;
685		optp = &opt;
686	} else
687		optp = inp->in6p_outputopts;
688
689	if (sin6) {
690		faddr = &sin6->sin6_addr;
691
692		/*
693		 * Since we saw no essential reason for calling in_pcbconnect,
694		 * we get rid of such kind of logic, and call in6_selectsrc
695		 * and in6_pcbsetport in order to fill in the local address
696		 * and the local port.
697		 */
698		if (sin6->sin6_port == 0) {
699			error = EADDRNOTAVAIL;
700			goto release;
701		}
702
703		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
704			/* how about ::ffff:0.0.0.0 case? */
705			error = EISCONN;
706			goto release;
707		}
708
709		fport = sin6->sin6_port; /* allow 0 port */
710
711		if (IN6_IS_ADDR_V4MAPPED(faddr)) {
712			if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
713				/*
714				 * I believe we should explicitly discard the
715				 * packet when mapped addresses are disabled,
716				 * rather than send the packet as an IPv6 one.
717				 * If we chose the latter approach, the packet
718				 * might be sent out on the wire based on the
719				 * default route, the situation which we'd
720				 * probably want to avoid.
721				 * (20010421 jinmei@kame.net)
722				 */
723				error = EINVAL;
724				goto release;
725			}
726			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
727			    !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
728				/*
729				 * when remote addr is an IPv4-mapped address,
730				 * local addr should not be an IPv6 address,
731				 * since you cannot determine how to map IPv6
732				 * source address to IPv4.
733				 */
734				error = EINVAL;
735				goto release;
736			}
737
738			af = AF_INET;
739		}
740
741		if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
742			error = in6_selectsrc(sin6, optp, inp, NULL,
743			    td->td_ucred, &oifp, &in6a);
744			if (error)
745				goto release;
746			if (oifp && scope_ambiguous &&
747			    (error = in6_setscope(&sin6->sin6_addr,
748			    oifp, NULL))) {
749				goto release;
750			}
751			laddr = &in6a;
752		} else
753			laddr = &inp->in6p_laddr;	/* XXX */
754		if (laddr == NULL) {
755			if (error == 0)
756				error = EADDRNOTAVAIL;
757			goto release;
758		}
759		if (inp->inp_lport == 0 &&
760		    (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0) {
761			/* Undo an address bind that may have occurred. */
762			inp->in6p_laddr = in6addr_any;
763			goto release;
764		}
765	} else {
766		if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
767			error = ENOTCONN;
768			goto release;
769		}
770		if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
771			if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
772				/*
773				 * XXX: this case would happen when the
774				 * application sets the V6ONLY flag after
775				 * connecting the foreign address.
776				 * Such applications should be fixed,
777				 * so we bark here.
778				 */
779				log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
780				    "option was set for a connected socket\n");
781				error = EINVAL;
782				goto release;
783			} else
784				af = AF_INET;
785		}
786		laddr = &inp->in6p_laddr;
787		faddr = &inp->in6p_faddr;
788		fport = inp->inp_fport;
789	}
790
791	if (af == AF_INET)
792		hlen = sizeof(struct ip);
793
794	/*
795	 * Calculate data length and get a mbuf
796	 * for UDP and IP6 headers.
797	 */
798	M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
799	if (m == 0) {
800		error = ENOBUFS;
801		goto release;
802	}
803
804	/*
805	 * Stuff checksum and output datagram.
806	 */
807	nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
808	    IPPROTO_UDP : IPPROTO_UDPLITE;
809	udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
810	udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
811	udp6->uh_dport = fport;
812	if (nxt == IPPROTO_UDPLITE) {
813		struct udpcb *up;
814
815		up = intoudpcb(inp);
816		cscov = up->u_txcslen;
817		if (cscov >= plen)
818			cscov = 0;
819		udp6->uh_ulen = htons(cscov);
820		/*
821		 * For UDP-Lite, checksum coverage length of zero means
822		 * the entire UDPLite packet is covered by the checksum.
823		 */
824		cscov_partial = (cscov == 0) ? 0 : 1;
825	} else if (plen <= 0xffff)
826		udp6->uh_ulen = htons((u_short)plen);
827	else
828		udp6->uh_ulen = 0;
829	udp6->uh_sum = 0;
830
831	switch (af) {
832	case AF_INET6:
833		ip6 = mtod(m, struct ip6_hdr *);
834		ip6->ip6_flow	= inp->inp_flow & IPV6_FLOWINFO_MASK;
835		ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
836		ip6->ip6_vfc	|= IPV6_VERSION;
837		ip6->ip6_plen	= htons((u_short)plen);
838		ip6->ip6_nxt	= nxt;
839		ip6->ip6_hlim	= in6_selecthlim(inp, NULL);
840		ip6->ip6_src	= *laddr;
841		ip6->ip6_dst	= *faddr;
842
843		if (cscov_partial) {
844			if ((udp6->uh_sum = in6_cksum(m, 0,
845			    sizeof(struct ip6_hdr), cscov)) == 0)
846				udp6->uh_sum = 0xffff;
847		} else {
848			udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
849			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
850			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
851		}
852
853		flags = 0;
854
855		UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
856		UDPSTAT_INC(udps_opackets);
857		error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
858		    NULL, inp);
859		break;
860	case AF_INET:
861		error = EAFNOSUPPORT;
862		goto release;
863	}
864	goto releaseopt;
865
866release:
867	m_freem(m);
868
869releaseopt:
870	if (control) {
871		ip6_clearpktopts(&opt, -1);
872		m_freem(control);
873	}
874	return (error);
875}
876
877static void
878udp6_abort(struct socket *so)
879{
880	struct inpcb *inp;
881	struct inpcbinfo *pcbinfo;
882
883	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
884	inp = sotoinpcb(so);
885	KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
886
887#ifdef INET
888	if (inp->inp_vflag & INP_IPV4) {
889		struct pr_usrreqs *pru;
890
891		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
892		(*pru->pru_abort)(so);
893		return;
894	}
895#endif
896
897	INP_WLOCK(inp);
898	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
899		INP_HASH_WLOCK(pcbinfo);
900		in6_pcbdisconnect(inp);
901		inp->in6p_laddr = in6addr_any;
902		INP_HASH_WUNLOCK(pcbinfo);
903		soisdisconnected(so);
904	}
905	INP_WUNLOCK(inp);
906}
907
908static int
909udp6_attach(struct socket *so, int proto, struct thread *td)
910{
911	struct inpcb *inp;
912	struct inpcbinfo *pcbinfo;
913	int error;
914
915	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
916	inp = sotoinpcb(so);
917	KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
918
919	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
920		error = soreserve(so, udp_sendspace, udp_recvspace);
921		if (error)
922			return (error);
923	}
924	INP_INFO_WLOCK(pcbinfo);
925	error = in_pcballoc(so, pcbinfo);
926	if (error) {
927		INP_INFO_WUNLOCK(pcbinfo);
928		return (error);
929	}
930	inp = (struct inpcb *)so->so_pcb;
931	inp->inp_vflag |= INP_IPV6;
932	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
933		inp->inp_vflag |= INP_IPV4;
934	inp->in6p_hops = -1;	/* use kernel default */
935	inp->in6p_cksum = -1;	/* just to be sure */
936	/*
937	 * XXX: ugly!!
938	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
939	 * because the socket may be bound to an IPv6 wildcard address,
940	 * which may match an IPv4-mapped IPv6 address.
941	 */
942	inp->inp_ip_ttl = V_ip_defttl;
943
944	error = udp_newudpcb(inp);
945	if (error) {
946		in_pcbdetach(inp);
947		in_pcbfree(inp);
948		INP_INFO_WUNLOCK(pcbinfo);
949		return (error);
950	}
951	INP_WUNLOCK(inp);
952	INP_INFO_WUNLOCK(pcbinfo);
953	return (0);
954}
955
956static int
957udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
958{
959	struct inpcb *inp;
960	struct inpcbinfo *pcbinfo;
961	int error;
962
963	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
964	inp = sotoinpcb(so);
965	KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
966
967	INP_WLOCK(inp);
968	INP_HASH_WLOCK(pcbinfo);
969	inp->inp_vflag &= ~INP_IPV4;
970	inp->inp_vflag |= INP_IPV6;
971	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
972		struct sockaddr_in6 *sin6_p;
973
974		sin6_p = (struct sockaddr_in6 *)nam;
975
976		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
977			inp->inp_vflag |= INP_IPV4;
978#ifdef INET
979		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
980			struct sockaddr_in sin;
981
982			in6_sin6_2_sin(&sin, sin6_p);
983			inp->inp_vflag |= INP_IPV4;
984			inp->inp_vflag &= ~INP_IPV6;
985			error = in_pcbbind(inp, (struct sockaddr *)&sin,
986			    td->td_ucred);
987			goto out;
988		}
989#endif
990	}
991
992	error = in6_pcbbind(inp, nam, td->td_ucred);
993#ifdef INET
994out:
995#endif
996	INP_HASH_WUNLOCK(pcbinfo);
997	INP_WUNLOCK(inp);
998	return (error);
999}
1000
1001static void
1002udp6_close(struct socket *so)
1003{
1004	struct inpcb *inp;
1005	struct inpcbinfo *pcbinfo;
1006
1007	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1008	inp = sotoinpcb(so);
1009	KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1010
1011#ifdef INET
1012	if (inp->inp_vflag & INP_IPV4) {
1013		struct pr_usrreqs *pru;
1014
1015		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1016		(*pru->pru_disconnect)(so);
1017		return;
1018	}
1019#endif
1020	INP_WLOCK(inp);
1021	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1022		INP_HASH_WLOCK(pcbinfo);
1023		in6_pcbdisconnect(inp);
1024		inp->in6p_laddr = in6addr_any;
1025		INP_HASH_WUNLOCK(pcbinfo);
1026		soisdisconnected(so);
1027	}
1028	INP_WUNLOCK(inp);
1029}
1030
1031static int
1032udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1033{
1034	struct inpcb *inp;
1035	struct inpcbinfo *pcbinfo;
1036	struct sockaddr_in6 *sin6;
1037	int error;
1038
1039	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1040	inp = sotoinpcb(so);
1041	sin6 = (struct sockaddr_in6 *)nam;
1042	KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1043
1044	/*
1045	 * XXXRW: Need to clarify locking of v4/v6 flags.
1046	 */
1047	INP_WLOCK(inp);
1048#ifdef INET
1049	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1050		struct sockaddr_in sin;
1051
1052		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1053			error = EINVAL;
1054			goto out;
1055		}
1056		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1057			error = EISCONN;
1058			goto out;
1059		}
1060		in6_sin6_2_sin(&sin, sin6);
1061		inp->inp_vflag |= INP_IPV4;
1062		inp->inp_vflag &= ~INP_IPV6;
1063		error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1064		if (error != 0)
1065			goto out;
1066		INP_HASH_WLOCK(pcbinfo);
1067		error = in_pcbconnect(inp, (struct sockaddr *)&sin,
1068		    td->td_ucred);
1069		INP_HASH_WUNLOCK(pcbinfo);
1070		if (error == 0)
1071			soisconnected(so);
1072		goto out;
1073	}
1074#endif
1075	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1076		error = EISCONN;
1077		goto out;
1078	}
1079	inp->inp_vflag &= ~INP_IPV4;
1080	inp->inp_vflag |= INP_IPV6;
1081	error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1082	if (error != 0)
1083		goto out;
1084	INP_HASH_WLOCK(pcbinfo);
1085	error = in6_pcbconnect(inp, nam, td->td_ucred);
1086	INP_HASH_WUNLOCK(pcbinfo);
1087	if (error == 0)
1088		soisconnected(so);
1089out:
1090	INP_WUNLOCK(inp);
1091	return (error);
1092}
1093
1094static void
1095udp6_detach(struct socket *so)
1096{
1097	struct inpcb *inp;
1098	struct inpcbinfo *pcbinfo;
1099	struct udpcb *up;
1100
1101	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1102	inp = sotoinpcb(so);
1103	KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1104
1105	INP_INFO_WLOCK(pcbinfo);
1106	INP_WLOCK(inp);
1107	up = intoudpcb(inp);
1108	KASSERT(up != NULL, ("%s: up == NULL", __func__));
1109	in_pcbdetach(inp);
1110	in_pcbfree(inp);
1111	INP_INFO_WUNLOCK(pcbinfo);
1112	udp_discardcb(up);
1113}
1114
1115static int
1116udp6_disconnect(struct socket *so)
1117{
1118	struct inpcb *inp;
1119	struct inpcbinfo *pcbinfo;
1120	int error;
1121
1122	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1123	inp = sotoinpcb(so);
1124	KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1125
1126#ifdef INET
1127	if (inp->inp_vflag & INP_IPV4) {
1128		struct pr_usrreqs *pru;
1129
1130		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1131		(void)(*pru->pru_disconnect)(so);
1132		return (0);
1133	}
1134#endif
1135
1136	INP_WLOCK(inp);
1137
1138	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1139		error = ENOTCONN;
1140		goto out;
1141	}
1142
1143	INP_HASH_WLOCK(pcbinfo);
1144	in6_pcbdisconnect(inp);
1145	inp->in6p_laddr = in6addr_any;
1146	INP_HASH_WUNLOCK(pcbinfo);
1147	SOCK_LOCK(so);
1148	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1149	SOCK_UNLOCK(so);
1150out:
1151	INP_WUNLOCK(inp);
1152	return (0);
1153}
1154
1155static int
1156udp6_send(struct socket *so, int flags, struct mbuf *m,
1157    struct sockaddr *addr, struct mbuf *control, struct thread *td)
1158{
1159	struct inpcb *inp;
1160	struct inpcbinfo *pcbinfo;
1161	int error = 0;
1162
1163	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1164	inp = sotoinpcb(so);
1165	KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1166
1167	INP_WLOCK(inp);
1168	if (addr) {
1169		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1170			error = EINVAL;
1171			goto bad;
1172		}
1173		if (addr->sa_family != AF_INET6) {
1174			error = EAFNOSUPPORT;
1175			goto bad;
1176		}
1177	}
1178
1179#ifdef INET
1180	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1181		int hasv4addr;
1182		struct sockaddr_in6 *sin6 = 0;
1183
1184		if (addr == 0)
1185			hasv4addr = (inp->inp_vflag & INP_IPV4);
1186		else {
1187			sin6 = (struct sockaddr_in6 *)addr;
1188			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1189			    ? 1 : 0;
1190		}
1191		if (hasv4addr) {
1192			struct pr_usrreqs *pru;
1193
1194			/*
1195			 * XXXRW: We release UDP-layer locks before calling
1196			 * udp_send() in order to avoid recursion.  However,
1197			 * this does mean there is a short window where inp's
1198			 * fields are unstable.  Could this lead to a
1199			 * potential race in which the factors causing us to
1200			 * select the UDPv4 output routine are invalidated?
1201			 */
1202			INP_WUNLOCK(inp);
1203			if (sin6)
1204				in6_sin6_2_sin_in_sock(addr);
1205			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1206			/* addr will just be freed in sendit(). */
1207			return ((*pru->pru_send)(so, flags, m, addr, control,
1208			    td));
1209		}
1210	}
1211#endif
1212#ifdef MAC
1213	mac_inpcb_create_mbuf(inp, m);
1214#endif
1215	INP_HASH_WLOCK(pcbinfo);
1216	error = udp6_output(inp, m, addr, control, td);
1217	INP_HASH_WUNLOCK(pcbinfo);
1218#ifdef INET
1219#endif
1220	INP_WUNLOCK(inp);
1221	return (error);
1222
1223bad:
1224	INP_WUNLOCK(inp);
1225	m_freem(m);
1226	return (error);
1227}
1228
1229struct pr_usrreqs udp6_usrreqs = {
1230	.pru_abort =		udp6_abort,
1231	.pru_attach =		udp6_attach,
1232	.pru_bind =		udp6_bind,
1233	.pru_connect =		udp6_connect,
1234	.pru_control =		in6_control,
1235	.pru_detach =		udp6_detach,
1236	.pru_disconnect =	udp6_disconnect,
1237	.pru_peeraddr =		in6_mapped_peeraddr,
1238	.pru_send =		udp6_send,
1239	.pru_shutdown =		udp_shutdown,
1240	.pru_sockaddr =		in6_mapped_sockaddr,
1241	.pru_soreceive =	soreceive_dgram,
1242	.pru_sosend =		sosend_dgram,
1243	.pru_sosetlabel =	in_pcbsosetlabel,
1244	.pru_close =		udp6_close
1245};
1246