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