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