udp6_usrreq.c revision 272661
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 272661 2014-10-06 17:04:26Z 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 || ulen == plen)) {
229		/* Zero means checksum over the complete packet. */
230		if (ulen == 0)
231			ulen = plen;
232		cscov_partial = 0;
233	}
234	if (nxt == IPPROTO_UDP && plen != ulen) {
235		UDPSTAT_INC(udps_badlen);
236		goto badunlocked;
237	}
238
239	/*
240	 * Checksum extended UDP header and data.
241	 */
242	if (uh->uh_sum == 0) {
243		if (ulen > plen || ulen < sizeof(struct udphdr)) {
244			UDPSTAT_INC(udps_nosum);
245			goto badunlocked;
246		}
247	}
248
249	if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
250	    !cscov_partial) {
251		if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
252			uh_sum = m->m_pkthdr.csum_data;
253		else
254			uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
255			    m->m_pkthdr.csum_data);
256		uh_sum ^= 0xffff;
257	} else
258		uh_sum = in6_cksum(m, nxt, off, ulen);
259
260	if (uh_sum != 0) {
261		UDPSTAT_INC(udps_badsum);
262		/*goto badunlocked;*/
263	}
264
265	/*
266	 * Construct sockaddr format source address.
267	 */
268	init_sin6(&fromsa, m);
269	fromsa.sin6_port = uh->uh_sport;
270
271	pcbinfo = get_inpcbinfo(nxt);
272	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
273		struct inpcb *last;
274		struct inpcbhead *pcblist;
275		struct ip6_moptions *imo;
276
277		INP_INFO_RLOCK(pcbinfo);
278		/*
279		 * In the event that laddr should be set to the link-local
280		 * address (this happens in RIPng), the multicast address
281		 * specified in the received packet will not match laddr.  To
282		 * handle this situation, matching is relaxed if the
283		 * receiving interface is the same as one specified in the
284		 * socket and if the destination multicast address matches
285		 * one of the multicast groups specified in the socket.
286		 */
287
288		/*
289		 * KAME note: traditionally we dropped udpiphdr from mbuf
290		 * here.  We need udphdr for IPsec processing so we do that
291		 * later.
292		 */
293		pcblist = get_pcblist(nxt);
294		last = NULL;
295		LIST_FOREACH(inp, pcblist, inp_list) {
296			if ((inp->inp_vflag & INP_IPV6) == 0)
297				continue;
298			if (inp->inp_lport != uh->uh_dport)
299				continue;
300			if (inp->inp_fport != 0 &&
301			    inp->inp_fport != uh->uh_sport)
302				continue;
303			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
304				if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
305							&ip6->ip6_dst))
306					continue;
307			}
308			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
309				if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
310							&ip6->ip6_src) ||
311				    inp->inp_fport != uh->uh_sport)
312					continue;
313			}
314
315			/*
316			 * XXXRW: Because we weren't holding either the inpcb
317			 * or the hash lock when we checked for a match
318			 * before, we should probably recheck now that the
319			 * inpcb lock is (supposed to be) held.
320			 */
321
322			/*
323			 * Handle socket delivery policy for any-source
324			 * and source-specific multicast. [RFC3678]
325			 */
326			imo = inp->in6p_moptions;
327			if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
328				struct sockaddr_in6	 mcaddr;
329				int			 blocked;
330
331				INP_RLOCK(inp);
332
333				bzero(&mcaddr, sizeof(struct sockaddr_in6));
334				mcaddr.sin6_len = sizeof(struct sockaddr_in6);
335				mcaddr.sin6_family = AF_INET6;
336				mcaddr.sin6_addr = ip6->ip6_dst;
337
338				blocked = im6o_mc_filter(imo, ifp,
339					(struct sockaddr *)&mcaddr,
340					(struct sockaddr *)&fromsa);
341				if (blocked != MCAST_PASS) {
342					if (blocked == MCAST_NOTGMEMBER)
343						IP6STAT_INC(ip6s_notmember);
344					if (blocked == MCAST_NOTSMEMBER ||
345					    blocked == MCAST_MUTED)
346						UDPSTAT_INC(udps_filtermcast);
347					INP_RUNLOCK(inp); /* XXX */
348					continue;
349				}
350
351				INP_RUNLOCK(inp);
352			}
353			if (last != NULL) {
354				struct mbuf *n;
355
356				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
357					INP_RLOCK(last);
358					up = intoudpcb(last);
359					if (up->u_tun_func == NULL) {
360						udp6_append(last, n, off, &fromsa);
361					} else {
362						/*
363						 * Engage the tunneling
364						 * protocol we will have to
365						 * leave the info_lock up,
366						 * since we are hunting
367						 * through multiple UDP's.
368						 *
369						 */
370						(*up->u_tun_func)(n, off, last);
371					}
372					INP_RUNLOCK(last);
373				}
374			}
375			last = inp;
376			/*
377			 * Don't look for additional matches if this one does
378			 * not have either the SO_REUSEPORT or SO_REUSEADDR
379			 * socket options set.  This heuristic avoids
380			 * searching through all pcbs in the common case of a
381			 * non-shared port.  It assumes that an application
382			 * will never clear these options after setting them.
383			 */
384			if ((last->inp_socket->so_options &
385			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
386				break;
387		}
388
389		if (last == NULL) {
390			/*
391			 * No matching pcb found; discard datagram.  (No need
392			 * to send an ICMP Port Unreachable for a broadcast
393			 * or multicast datgram.)
394			 */
395			UDPSTAT_INC(udps_noport);
396			UDPSTAT_INC(udps_noportmcast);
397			goto badheadlocked;
398		}
399		INP_RLOCK(last);
400		INP_INFO_RUNLOCK(pcbinfo);
401		up = intoudpcb(last);
402		UDP_PROBE(receive, NULL, last, ip6, last, uh);
403		if (up->u_tun_func == NULL) {
404			udp6_append(last, m, off, &fromsa);
405		} else {
406			/*
407			 * Engage the tunneling protocol.
408			 */
409			(*up->u_tun_func)(m, off, last);
410		}
411		INP_RUNLOCK(last);
412		return (IPPROTO_DONE);
413	}
414	/*
415	 * Locate pcb for datagram.
416	 */
417
418	/*
419	 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
420	 */
421	if ((m->m_flags & M_IP6_NEXTHOP) &&
422	    (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
423		struct sockaddr_in6 *next_hop6;
424
425		next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
426
427		/*
428		 * Transparently forwarded. Pretend to be the destination.
429		 * Already got one like this?
430		 */
431		inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
432		    uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
433		    INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
434		if (!inp) {
435			/*
436			 * It's new.  Try to find the ambushing socket.
437			 * Because we've rewritten the destination address,
438			 * any hardware-generated hash is ignored.
439			 */
440			inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
441			    uh->uh_sport, &next_hop6->sin6_addr,
442			    next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
443			    uh->uh_dport, INPLOOKUP_WILDCARD |
444			    INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
445		}
446		/* Remove the tag from the packet. We don't need it anymore. */
447		m_tag_delete(m, fwd_tag);
448		m->m_flags &= ~M_IP6_NEXTHOP;
449	} else
450		inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
451		    uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
452		    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
453		    m->m_pkthdr.rcvif, m);
454	if (inp == NULL) {
455		if (udp_log_in_vain) {
456			char ip6bufs[INET6_ADDRSTRLEN];
457			char ip6bufd[INET6_ADDRSTRLEN];
458
459			log(LOG_INFO,
460			    "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
461			    ip6_sprintf(ip6bufd, &ip6->ip6_dst),
462			    ntohs(uh->uh_dport),
463			    ip6_sprintf(ip6bufs, &ip6->ip6_src),
464			    ntohs(uh->uh_sport));
465		}
466		UDPSTAT_INC(udps_noport);
467		if (m->m_flags & M_MCAST) {
468			printf("UDP6: M_MCAST is set in a unicast packet.\n");
469			UDPSTAT_INC(udps_noportmcast);
470			goto badunlocked;
471		}
472		if (V_udp_blackhole)
473			goto badunlocked;
474		if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
475			goto badunlocked;
476		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
477		return (IPPROTO_DONE);
478	}
479	INP_RLOCK_ASSERT(inp);
480	up = intoudpcb(inp);
481	if (cscov_partial) {
482		if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
483			INP_RUNLOCK(inp);
484			m_freem(m);
485			return (IPPROTO_DONE);
486		}
487	}
488	UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
489	if (up->u_tun_func == NULL) {
490		udp6_append(inp, m, off, &fromsa);
491	} else {
492		/*
493		 * Engage the tunneling protocol.
494		 */
495
496		(*up->u_tun_func)(m, off, inp);
497	}
498	INP_RUNLOCK(inp);
499	return (IPPROTO_DONE);
500
501badheadlocked:
502	INP_INFO_RUNLOCK(pcbinfo);
503badunlocked:
504	if (m)
505		m_freem(m);
506	return (IPPROTO_DONE);
507}
508
509static void
510udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d,
511    struct inpcbinfo *pcbinfo)
512{
513	struct udphdr uh;
514	struct ip6_hdr *ip6;
515	struct mbuf *m;
516	int off = 0;
517	struct ip6ctlparam *ip6cp = NULL;
518	const struct sockaddr_in6 *sa6_src = NULL;
519	void *cmdarg;
520	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
521	struct udp_portonly {
522		u_int16_t uh_sport;
523		u_int16_t uh_dport;
524	} *uhp;
525
526	if (sa->sa_family != AF_INET6 ||
527	    sa->sa_len != sizeof(struct sockaddr_in6))
528		return;
529
530	if ((unsigned)cmd >= PRC_NCMDS)
531		return;
532	if (PRC_IS_REDIRECT(cmd))
533		notify = in6_rtchange, d = NULL;
534	else if (cmd == PRC_HOSTDEAD)
535		d = NULL;
536	else if (inet6ctlerrmap[cmd] == 0)
537		return;
538
539	/* if the parameter is from icmp6, decode it. */
540	if (d != NULL) {
541		ip6cp = (struct ip6ctlparam *)d;
542		m = ip6cp->ip6c_m;
543		ip6 = ip6cp->ip6c_ip6;
544		off = ip6cp->ip6c_off;
545		cmdarg = ip6cp->ip6c_cmdarg;
546		sa6_src = ip6cp->ip6c_src;
547	} else {
548		m = NULL;
549		ip6 = NULL;
550		cmdarg = NULL;
551		sa6_src = &sa6_any;
552	}
553
554	if (ip6) {
555		/*
556		 * XXX: We assume that when IPV6 is non NULL,
557		 * M and OFF are valid.
558		 */
559
560		/* Check if we can safely examine src and dst ports. */
561		if (m->m_pkthdr.len < off + sizeof(*uhp))
562			return;
563
564		bzero(&uh, sizeof(uh));
565		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
566
567		(void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport,
568		    (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
569		    cmdarg, notify);
570	} else
571		(void)in6_pcbnotify(pcbinfo, sa, 0,
572		    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
573}
574
575void
576udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
577{
578
579	return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo));
580}
581
582void
583udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d)
584{
585
586	return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo));
587}
588
589static int
590udp6_getcred(SYSCTL_HANDLER_ARGS)
591{
592	struct xucred xuc;
593	struct sockaddr_in6 addrs[2];
594	struct inpcb *inp;
595	int error;
596
597	error = priv_check(req->td, PRIV_NETINET_GETCRED);
598	if (error)
599		return (error);
600
601	if (req->newlen != sizeof(addrs))
602		return (EINVAL);
603	if (req->oldlen != sizeof(struct xucred))
604		return (EINVAL);
605	error = SYSCTL_IN(req, addrs, sizeof(addrs));
606	if (error)
607		return (error);
608	if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
609	    (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
610		return (error);
611	}
612	inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
613	    addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
614	    INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
615	if (inp != NULL) {
616		INP_RLOCK_ASSERT(inp);
617		if (inp->inp_socket == NULL)
618			error = ENOENT;
619		if (error == 0)
620			error = cr_canseesocket(req->td->td_ucred,
621			    inp->inp_socket);
622		if (error == 0)
623			cru2x(inp->inp_cred, &xuc);
624		INP_RUNLOCK(inp);
625	} else
626		error = ENOENT;
627	if (error == 0)
628		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
629	return (error);
630}
631
632SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
633    0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
634
635static int
636udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
637    struct mbuf *control, struct thread *td)
638{
639	u_int32_t ulen = m->m_pkthdr.len;
640	u_int32_t plen = sizeof(struct udphdr) + ulen;
641	struct ip6_hdr *ip6;
642	struct udphdr *udp6;
643	struct in6_addr *laddr, *faddr, in6a;
644	struct sockaddr_in6 *sin6 = NULL;
645	struct ifnet *oifp = NULL;
646	int cscov_partial = 0;
647	int scope_ambiguous = 0;
648	u_short fport;
649	int error = 0;
650	uint8_t nxt;
651	uint16_t cscov = 0;
652	struct ip6_pktopts *optp, opt;
653	int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
654	int flags;
655	struct sockaddr_in6 tmp;
656
657	INP_WLOCK_ASSERT(inp);
658	INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
659
660	if (addr6) {
661		/* addr6 has been validated in udp6_send(). */
662		sin6 = (struct sockaddr_in6 *)addr6;
663
664		/* protect *sin6 from overwrites */
665		tmp = *sin6;
666		sin6 = &tmp;
667
668		/*
669		 * Application should provide a proper zone ID or the use of
670		 * default zone IDs should be enabled.  Unfortunately, some
671		 * applications do not behave as it should, so we need a
672		 * workaround.  Even if an appropriate ID is not determined,
673		 * we'll see if we can determine the outgoing interface.  If we
674		 * can, determine the zone ID based on the interface below.
675		 */
676		if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
677			scope_ambiguous = 1;
678		if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
679			return (error);
680	}
681
682	if (control) {
683		if ((error = ip6_setpktopts(control, &opt,
684		    inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
685			goto release;
686		optp = &opt;
687	} else
688		optp = inp->in6p_outputopts;
689
690	if (sin6) {
691		faddr = &sin6->sin6_addr;
692
693		/*
694		 * Since we saw no essential reason for calling in_pcbconnect,
695		 * we get rid of such kind of logic, and call in6_selectsrc
696		 * and in6_pcbsetport in order to fill in the local address
697		 * and the local port.
698		 */
699		if (sin6->sin6_port == 0) {
700			error = EADDRNOTAVAIL;
701			goto release;
702		}
703
704		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
705			/* how about ::ffff:0.0.0.0 case? */
706			error = EISCONN;
707			goto release;
708		}
709
710		fport = sin6->sin6_port; /* allow 0 port */
711
712		if (IN6_IS_ADDR_V4MAPPED(faddr)) {
713			if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
714				/*
715				 * I believe we should explicitly discard the
716				 * packet when mapped addresses are disabled,
717				 * rather than send the packet as an IPv6 one.
718				 * If we chose the latter approach, the packet
719				 * might be sent out on the wire based on the
720				 * default route, the situation which we'd
721				 * probably want to avoid.
722				 * (20010421 jinmei@kame.net)
723				 */
724				error = EINVAL;
725				goto release;
726			}
727			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
728			    !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
729				/*
730				 * when remote addr is an IPv4-mapped address,
731				 * local addr should not be an IPv6 address,
732				 * since you cannot determine how to map IPv6
733				 * source address to IPv4.
734				 */
735				error = EINVAL;
736				goto release;
737			}
738
739			af = AF_INET;
740		}
741
742		if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
743			error = in6_selectsrc(sin6, optp, inp, NULL,
744			    td->td_ucred, &oifp, &in6a);
745			if (error)
746				goto release;
747			if (oifp && scope_ambiguous &&
748			    (error = in6_setscope(&sin6->sin6_addr,
749			    oifp, NULL))) {
750				goto release;
751			}
752			laddr = &in6a;
753		} else
754			laddr = &inp->in6p_laddr;	/* XXX */
755		if (laddr == NULL) {
756			if (error == 0)
757				error = EADDRNOTAVAIL;
758			goto release;
759		}
760		if (inp->inp_lport == 0 &&
761		    (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0) {
762			/* Undo an address bind that may have occurred. */
763			inp->in6p_laddr = in6addr_any;
764			goto release;
765		}
766	} else {
767		if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
768			error = ENOTCONN;
769			goto release;
770		}
771		if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
772			if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
773				/*
774				 * XXX: this case would happen when the
775				 * application sets the V6ONLY flag after
776				 * connecting the foreign address.
777				 * Such applications should be fixed,
778				 * so we bark here.
779				 */
780				log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
781				    "option was set for a connected socket\n");
782				error = EINVAL;
783				goto release;
784			} else
785				af = AF_INET;
786		}
787		laddr = &inp->in6p_laddr;
788		faddr = &inp->in6p_faddr;
789		fport = inp->inp_fport;
790	}
791
792	if (af == AF_INET)
793		hlen = sizeof(struct ip);
794
795	/*
796	 * Calculate data length and get a mbuf
797	 * for UDP and IP6 headers.
798	 */
799	M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
800	if (m == 0) {
801		error = ENOBUFS;
802		goto release;
803	}
804
805	/*
806	 * Stuff checksum and output datagram.
807	 */
808	nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
809	    IPPROTO_UDP : IPPROTO_UDPLITE;
810	udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
811	udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
812	udp6->uh_dport = fport;
813	if (nxt == IPPROTO_UDPLITE) {
814		struct udpcb *up;
815
816		up = intoudpcb(inp);
817		cscov = up->u_txcslen;
818		if (cscov >= plen)
819			cscov = 0;
820		udp6->uh_ulen = htons(cscov);
821		/*
822		 * For UDP-Lite, checksum coverage length of zero means
823		 * the entire UDPLite packet is covered by the checksum.
824		 */
825		cscov_partial = (cscov == 0) ? 0 : 1;
826	} else if (plen <= 0xffff)
827		udp6->uh_ulen = htons((u_short)plen);
828	else
829		udp6->uh_ulen = 0;
830	udp6->uh_sum = 0;
831
832	switch (af) {
833	case AF_INET6:
834		ip6 = mtod(m, struct ip6_hdr *);
835		ip6->ip6_flow	= inp->inp_flow & IPV6_FLOWINFO_MASK;
836		ip6->ip6_vfc	&= ~IPV6_VERSION_MASK;
837		ip6->ip6_vfc	|= IPV6_VERSION;
838		ip6->ip6_plen	= htons((u_short)plen);
839		ip6->ip6_nxt	= nxt;
840		ip6->ip6_hlim	= in6_selecthlim(inp, NULL);
841		ip6->ip6_src	= *laddr;
842		ip6->ip6_dst	= *faddr;
843
844		if (cscov_partial) {
845			if ((udp6->uh_sum = in6_cksum(m, 0,
846			    sizeof(struct ip6_hdr), cscov)) == 0)
847				udp6->uh_sum = 0xffff;
848		} else {
849			udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
850			m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
851			m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
852		}
853
854		flags = 0;
855
856		UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
857		UDPSTAT_INC(udps_opackets);
858		error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
859		    NULL, inp);
860		break;
861	case AF_INET:
862		error = EAFNOSUPPORT;
863		goto release;
864	}
865	goto releaseopt;
866
867release:
868	m_freem(m);
869
870releaseopt:
871	if (control) {
872		ip6_clearpktopts(&opt, -1);
873		m_freem(control);
874	}
875	return (error);
876}
877
878static void
879udp6_abort(struct socket *so)
880{
881	struct inpcb *inp;
882	struct inpcbinfo *pcbinfo;
883
884	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
885	inp = sotoinpcb(so);
886	KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
887
888#ifdef INET
889	if (inp->inp_vflag & INP_IPV4) {
890		struct pr_usrreqs *pru;
891
892		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
893		(*pru->pru_abort)(so);
894		return;
895	}
896#endif
897
898	INP_WLOCK(inp);
899	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
900		INP_HASH_WLOCK(pcbinfo);
901		in6_pcbdisconnect(inp);
902		inp->in6p_laddr = in6addr_any;
903		INP_HASH_WUNLOCK(pcbinfo);
904		soisdisconnected(so);
905	}
906	INP_WUNLOCK(inp);
907}
908
909static int
910udp6_attach(struct socket *so, int proto, struct thread *td)
911{
912	struct inpcb *inp;
913	struct inpcbinfo *pcbinfo;
914	int error;
915
916	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
917	inp = sotoinpcb(so);
918	KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
919
920	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
921		error = soreserve(so, udp_sendspace, udp_recvspace);
922		if (error)
923			return (error);
924	}
925	INP_INFO_WLOCK(pcbinfo);
926	error = in_pcballoc(so, pcbinfo);
927	if (error) {
928		INP_INFO_WUNLOCK(pcbinfo);
929		return (error);
930	}
931	inp = (struct inpcb *)so->so_pcb;
932	inp->inp_vflag |= INP_IPV6;
933	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
934		inp->inp_vflag |= INP_IPV4;
935	inp->in6p_hops = -1;	/* use kernel default */
936	inp->in6p_cksum = -1;	/* just to be sure */
937	/*
938	 * XXX: ugly!!
939	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
940	 * because the socket may be bound to an IPv6 wildcard address,
941	 * which may match an IPv4-mapped IPv6 address.
942	 */
943	inp->inp_ip_ttl = V_ip_defttl;
944
945	error = udp_newudpcb(inp);
946	if (error) {
947		in_pcbdetach(inp);
948		in_pcbfree(inp);
949		INP_INFO_WUNLOCK(pcbinfo);
950		return (error);
951	}
952	INP_WUNLOCK(inp);
953	INP_INFO_WUNLOCK(pcbinfo);
954	return (0);
955}
956
957static int
958udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
959{
960	struct inpcb *inp;
961	struct inpcbinfo *pcbinfo;
962	int error;
963
964	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
965	inp = sotoinpcb(so);
966	KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
967
968	INP_WLOCK(inp);
969	INP_HASH_WLOCK(pcbinfo);
970	inp->inp_vflag &= ~INP_IPV4;
971	inp->inp_vflag |= INP_IPV6;
972	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
973		struct sockaddr_in6 *sin6_p;
974
975		sin6_p = (struct sockaddr_in6 *)nam;
976
977		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
978			inp->inp_vflag |= INP_IPV4;
979#ifdef INET
980		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
981			struct sockaddr_in sin;
982
983			in6_sin6_2_sin(&sin, sin6_p);
984			inp->inp_vflag |= INP_IPV4;
985			inp->inp_vflag &= ~INP_IPV6;
986			error = in_pcbbind(inp, (struct sockaddr *)&sin,
987			    td->td_ucred);
988			goto out;
989		}
990#endif
991	}
992
993	error = in6_pcbbind(inp, nam, td->td_ucred);
994#ifdef INET
995out:
996#endif
997	INP_HASH_WUNLOCK(pcbinfo);
998	INP_WUNLOCK(inp);
999	return (error);
1000}
1001
1002static void
1003udp6_close(struct socket *so)
1004{
1005	struct inpcb *inp;
1006	struct inpcbinfo *pcbinfo;
1007
1008	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1009	inp = sotoinpcb(so);
1010	KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1011
1012#ifdef INET
1013	if (inp->inp_vflag & INP_IPV4) {
1014		struct pr_usrreqs *pru;
1015
1016		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1017		(*pru->pru_disconnect)(so);
1018		return;
1019	}
1020#endif
1021	INP_WLOCK(inp);
1022	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1023		INP_HASH_WLOCK(pcbinfo);
1024		in6_pcbdisconnect(inp);
1025		inp->in6p_laddr = in6addr_any;
1026		INP_HASH_WUNLOCK(pcbinfo);
1027		soisdisconnected(so);
1028	}
1029	INP_WUNLOCK(inp);
1030}
1031
1032static int
1033udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1034{
1035	struct inpcb *inp;
1036	struct inpcbinfo *pcbinfo;
1037	struct sockaddr_in6 *sin6;
1038	int error;
1039
1040	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1041	inp = sotoinpcb(so);
1042	sin6 = (struct sockaddr_in6 *)nam;
1043	KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1044
1045	/*
1046	 * XXXRW: Need to clarify locking of v4/v6 flags.
1047	 */
1048	INP_WLOCK(inp);
1049#ifdef INET
1050	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1051		struct sockaddr_in sin;
1052
1053		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1054			error = EINVAL;
1055			goto out;
1056		}
1057		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1058			error = EISCONN;
1059			goto out;
1060		}
1061		in6_sin6_2_sin(&sin, sin6);
1062		inp->inp_vflag |= INP_IPV4;
1063		inp->inp_vflag &= ~INP_IPV6;
1064		error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1065		if (error != 0)
1066			goto out;
1067		INP_HASH_WLOCK(pcbinfo);
1068		error = in_pcbconnect(inp, (struct sockaddr *)&sin,
1069		    td->td_ucred);
1070		INP_HASH_WUNLOCK(pcbinfo);
1071		if (error == 0)
1072			soisconnected(so);
1073		goto out;
1074	}
1075#endif
1076	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1077		error = EISCONN;
1078		goto out;
1079	}
1080	inp->inp_vflag &= ~INP_IPV4;
1081	inp->inp_vflag |= INP_IPV6;
1082	error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1083	if (error != 0)
1084		goto out;
1085	INP_HASH_WLOCK(pcbinfo);
1086	error = in6_pcbconnect(inp, nam, td->td_ucred);
1087	INP_HASH_WUNLOCK(pcbinfo);
1088	if (error == 0)
1089		soisconnected(so);
1090out:
1091	INP_WUNLOCK(inp);
1092	return (error);
1093}
1094
1095static void
1096udp6_detach(struct socket *so)
1097{
1098	struct inpcb *inp;
1099	struct inpcbinfo *pcbinfo;
1100	struct udpcb *up;
1101
1102	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1103	inp = sotoinpcb(so);
1104	KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1105
1106	INP_INFO_WLOCK(pcbinfo);
1107	INP_WLOCK(inp);
1108	up = intoudpcb(inp);
1109	KASSERT(up != NULL, ("%s: up == NULL", __func__));
1110	in_pcbdetach(inp);
1111	in_pcbfree(inp);
1112	INP_INFO_WUNLOCK(pcbinfo);
1113	udp_discardcb(up);
1114}
1115
1116static int
1117udp6_disconnect(struct socket *so)
1118{
1119	struct inpcb *inp;
1120	struct inpcbinfo *pcbinfo;
1121	int error;
1122
1123	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1124	inp = sotoinpcb(so);
1125	KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1126
1127#ifdef INET
1128	if (inp->inp_vflag & INP_IPV4) {
1129		struct pr_usrreqs *pru;
1130
1131		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1132		(void)(*pru->pru_disconnect)(so);
1133		return (0);
1134	}
1135#endif
1136
1137	INP_WLOCK(inp);
1138
1139	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1140		error = ENOTCONN;
1141		goto out;
1142	}
1143
1144	INP_HASH_WLOCK(pcbinfo);
1145	in6_pcbdisconnect(inp);
1146	inp->in6p_laddr = in6addr_any;
1147	INP_HASH_WUNLOCK(pcbinfo);
1148	SOCK_LOCK(so);
1149	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1150	SOCK_UNLOCK(so);
1151out:
1152	INP_WUNLOCK(inp);
1153	return (0);
1154}
1155
1156static int
1157udp6_send(struct socket *so, int flags, struct mbuf *m,
1158    struct sockaddr *addr, struct mbuf *control, struct thread *td)
1159{
1160	struct inpcb *inp;
1161	struct inpcbinfo *pcbinfo;
1162	int error = 0;
1163
1164	pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1165	inp = sotoinpcb(so);
1166	KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1167
1168	INP_WLOCK(inp);
1169	if (addr) {
1170		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1171			error = EINVAL;
1172			goto bad;
1173		}
1174		if (addr->sa_family != AF_INET6) {
1175			error = EAFNOSUPPORT;
1176			goto bad;
1177		}
1178	}
1179
1180#ifdef INET
1181	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1182		int hasv4addr;
1183		struct sockaddr_in6 *sin6 = 0;
1184
1185		if (addr == 0)
1186			hasv4addr = (inp->inp_vflag & INP_IPV4);
1187		else {
1188			sin6 = (struct sockaddr_in6 *)addr;
1189			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1190			    ? 1 : 0;
1191		}
1192		if (hasv4addr) {
1193			struct pr_usrreqs *pru;
1194
1195			/*
1196			 * XXXRW: We release UDP-layer locks before calling
1197			 * udp_send() in order to avoid recursion.  However,
1198			 * this does mean there is a short window where inp's
1199			 * fields are unstable.  Could this lead to a
1200			 * potential race in which the factors causing us to
1201			 * select the UDPv4 output routine are invalidated?
1202			 */
1203			INP_WUNLOCK(inp);
1204			if (sin6)
1205				in6_sin6_2_sin_in_sock(addr);
1206			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1207			/* addr will just be freed in sendit(). */
1208			return ((*pru->pru_send)(so, flags, m, addr, control,
1209			    td));
1210		}
1211	}
1212#endif
1213#ifdef MAC
1214	mac_inpcb_create_mbuf(inp, m);
1215#endif
1216	INP_HASH_WLOCK(pcbinfo);
1217	error = udp6_output(inp, m, addr, control, td);
1218	INP_HASH_WUNLOCK(pcbinfo);
1219#ifdef INET
1220#endif
1221	INP_WUNLOCK(inp);
1222	return (error);
1223
1224bad:
1225	INP_WUNLOCK(inp);
1226	m_freem(m);
1227	return (error);
1228}
1229
1230struct pr_usrreqs udp6_usrreqs = {
1231	.pru_abort =		udp6_abort,
1232	.pru_attach =		udp6_attach,
1233	.pru_bind =		udp6_bind,
1234	.pru_connect =		udp6_connect,
1235	.pru_control =		in6_control,
1236	.pru_detach =		udp6_detach,
1237	.pru_disconnect =	udp6_disconnect,
1238	.pru_peeraddr =		in6_mapped_peeraddr,
1239	.pru_send =		udp6_send,
1240	.pru_shutdown =		udp_shutdown,
1241	.pru_sockaddr =		in6_mapped_sockaddr,
1242	.pru_soreceive =	soreceive_dgram,
1243	.pru_sosend =		sosend_dgram,
1244	.pru_sosetlabel =	in_pcbsosetlabel,
1245	.pru_close =		udp6_close
1246};
1247