xref: /freebsd/sys/netinet6/udp6_usrreq.c (revision 57c4583f70ab9d25b3aed17f20ec7843f9673539)
1 /*	$FreeBSD$	*/
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  * 4. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
62  */
63 
64 #include "opt_inet.h"
65 #include "opt_inet6.h"
66 #include "opt_ipsec.h"
67 
68 #include <sys/param.h>
69 #include <sys/errno.h>
70 #include <sys/kernel.h>
71 #include <sys/lock.h>
72 #include <sys/mbuf.h>
73 #include <sys/priv.h>
74 #include <sys/proc.h>
75 #include <sys/protosw.h>
76 #include <sys/signalvar.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/stat.h>
80 #include <sys/sx.h>
81 #include <sys/sysctl.h>
82 #include <sys/syslog.h>
83 #include <sys/systm.h>
84 
85 #include <net/if.h>
86 #include <net/if_types.h>
87 #include <net/route.h>
88 
89 #include <netinet/in.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/in_var.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip6.h>
95 #include <netinet/icmp6.h>
96 #include <netinet/ip_var.h>
97 #include <netinet/udp.h>
98 #include <netinet/udp_var.h>
99 #include <netinet6/ip6protosw.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet6/in6_pcb.h>
102 #include <netinet6/udp6_var.h>
103 #include <netinet6/scope6_var.h>
104 
105 #ifdef IPSEC
106 #include <netinet6/ipsec.h>
107 #include <netinet6/ipsec6.h>
108 #endif /* IPSEC */
109 
110 #ifdef FAST_IPSEC
111 #include <netipsec/ipsec.h>
112 #include <netipsec/ipsec6.h>
113 #endif /* FAST_IPSEC */
114 
115 /*
116  * UDP protocol inplementation.
117  * Per RFC 768, August, 1980.
118  */
119 
120 extern	struct protosw inetsw[];
121 static	void udp6_detach __P((struct socket *so));
122 
123 static void
124 udp6_append(struct inpcb *in6p, struct mbuf *n, int off,
125     struct sockaddr_in6 *fromsa)
126 {
127 	struct socket *so;
128 	struct mbuf *opts;
129 
130 	/* XXXRW: Not yet: INP_LOCK_ASSERT(in6p); */
131 
132 #if defined(IPSEC) || defined(FAST_IPSEC)
133 	/*
134 	 * Check AH/ESP integrity.
135 	 */
136 	if (ipsec6_in_reject(n, in6p)) {
137 		m_freem(n);
138 #ifdef IPSEC
139 		ipsec6stat.in_polvio++;
140 #endif /* IPSEC */
141 		return;
142 	}
143 #endif /*IPSEC || FAST_IPSEC*/
144 
145 	opts = NULL;
146 	if (in6p->in6p_flags & IN6P_CONTROLOPTS ||
147 	    in6p->inp_socket->so_options & SO_TIMESTAMP)
148 		ip6_savecontrol(in6p, n, &opts);
149 	m_adj(n, off + sizeof(struct udphdr));
150 
151 	so = in6p->inp_socket;
152 	SOCKBUF_LOCK(&so->so_rcv);
153 	if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
154 	    opts) == 0) {
155 		SOCKBUF_UNLOCK(&so->so_rcv);
156 		m_freem(n);
157 		if (opts)
158 			m_freem(opts);
159 		udpstat.udps_fullsock++;
160 	} else
161 		sorwakeup_locked(so);
162 }
163 
164 int
165 udp6_input(mp, offp, proto)
166 	struct mbuf **mp;
167 	int *offp, proto;
168 {
169 	struct mbuf *m = *mp;
170 	register struct ip6_hdr *ip6;
171 	register struct udphdr *uh;
172 	register struct inpcb *in6p;
173 	int off = *offp;
174 	int plen, ulen;
175 	struct sockaddr_in6 fromsa;
176 
177 	ip6 = mtod(m, struct ip6_hdr *);
178 
179 	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
180 		/* XXX send icmp6 host/port unreach? */
181 		m_freem(m);
182 		return IPPROTO_DONE;
183 	}
184 
185 #ifndef PULLDOWN_TEST
186 	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
187 	ip6 = mtod(m, struct ip6_hdr *);
188 	uh = (struct udphdr *)((caddr_t)ip6 + off);
189 #else
190 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
191 	if (!uh)
192 		return IPPROTO_DONE;
193 #endif
194 
195 	udpstat.udps_ipackets++;
196 
197 	plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
198 	ulen = ntohs((u_short)uh->uh_ulen);
199 
200 	if (plen != ulen) {
201 		udpstat.udps_badlen++;
202 		goto bad;
203 	}
204 
205 	/*
206 	 * Checksum extended UDP header and data.
207 	 */
208 	if (uh->uh_sum == 0) {
209 		udpstat.udps_nosum++;
210 		goto bad_unlocked;
211 	}
212 	if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
213 		udpstat.udps_badsum++;
214 		goto bad_unlocked;
215 	}
216 
217 	/*
218 	 * Construct sockaddr format source address.
219 	 */
220 	init_sin6(&fromsa, m);
221 	fromsa.sin6_port = uh->uh_sport;
222 
223 	INP_INFO_RLOCK(&udbinfo);
224 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
225 		struct	inpcb *last;
226 
227 		/*
228 		 * Deliver a multicast datagram to all sockets
229 		 * for which the local and remote addresses and ports match
230 		 * those of the incoming datagram.  This allows more than
231 		 * one process to receive multicasts on the same port.
232 		 * (This really ought to be done for unicast datagrams as
233 		 * well, but that would cause problems with existing
234 		 * applications that open both address-specific sockets and
235 		 * a wildcard socket listening to the same port -- they would
236 		 * end up receiving duplicates of every unicast datagram.
237 		 * Those applications open the multiple sockets to overcome an
238 		 * inadequacy of the UDP socket interface, but for backwards
239 		 * compatibility we avoid the problem here rather than
240 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
241 		 */
242 
243 		/*
244 		 * In a case that laddr should be set to the link-local
245 		 * address (this happens in RIPng), the multicast address
246 		 * specified in the received packet does not match with
247 		 * laddr. To cure this situation, the matching is relaxed
248 		 * if the receiving interface is the same as one specified
249 		 * in the socket and if the destination multicast address
250 		 * matches one of the multicast groups specified in the socket.
251 		 */
252 
253 		/*
254 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
255 		 * We need udphdr for IPsec processing so we do that later.
256 		 */
257 
258 		/*
259 		 * Locate pcb(s) for datagram.
260 		 * (Algorithm copied from raw_intr().)
261 		 *
262 		 * XXXRW: The individual inpcbs need to be locked in the
263 		 * style of udp_input().
264 		 */
265 		last = NULL;
266 		LIST_FOREACH(in6p, &udb, inp_list) {
267 			if ((in6p->inp_vflag & INP_IPV6) == 0)
268 				continue;
269 			if (in6p->in6p_lport != uh->uh_dport)
270 				continue;
271 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
272 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
273 							&ip6->ip6_dst))
274 					continue;
275 			}
276 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
277 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
278 							&ip6->ip6_src) ||
279 				    in6p->in6p_fport != uh->uh_sport)
280 					continue;
281 			}
282 
283 			if (last != NULL) {
284 				struct mbuf *n;
285 
286 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
287 					INP_LOCK(last);
288 					udp6_append(last, n, off, &fromsa);
289 					INP_UNLOCK(last);
290 				}
291 			}
292 			last = in6p;
293 			/*
294 			 * Don't look for additional matches if this one does
295 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
296 			 * socket options set.  This heuristic avoids searching
297 			 * through all pcbs in the common case of a non-shared
298 			 * port.  It assumes that an application will never
299 			 * clear these options after setting them.
300 			 */
301 			if ((last->in6p_socket->so_options &
302 			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
303 				break;
304 		}
305 
306 		if (last == NULL) {
307 			/*
308 			 * No matching pcb found; discard datagram.
309 			 * (No need to send an ICMP Port Unreachable
310 			 * for a broadcast or multicast datgram.)
311 			 */
312 			udpstat.udps_noport++;
313 			udpstat.udps_noportmcast++;
314 			goto bad;
315 		}
316 		INP_LOCK(last);
317 		udp6_append(last, m, off, &fromsa);
318 		INP_UNLOCK(last);
319 		INP_INFO_RUNLOCK(&udbinfo);
320 		return IPPROTO_DONE;
321 	}
322 	/*
323 	 * Locate pcb for datagram.
324 	 */
325 	in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
326 				  &ip6->ip6_dst, uh->uh_dport, 1,
327 				  m->m_pkthdr.rcvif);
328 	if (in6p == NULL) {
329 		if (log_in_vain) {
330 			char buf[INET6_ADDRSTRLEN];
331 
332 			strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
333 			log(LOG_INFO,
334 			    "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
335 			    buf, ntohs(uh->uh_dport),
336 			    ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
337 		}
338 		udpstat.udps_noport++;
339 		if (m->m_flags & M_MCAST) {
340 			printf("UDP6: M_MCAST is set in a unicast packet.\n");
341 			udpstat.udps_noportmcast++;
342 			goto bad;
343 		}
344 		INP_INFO_RUNLOCK(&udbinfo);
345 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
346 		return IPPROTO_DONE;
347 	}
348 	INP_LOCK(in6p);
349 	udp6_append(in6p, m, off, &fromsa);
350 	INP_UNLOCK(in6p);
351 	INP_INFO_RUNLOCK(&udbinfo);
352 	return IPPROTO_DONE;
353 bad:
354 	INP_INFO_RUNLOCK(&udbinfo);
355 bad_unlocked:
356 	if (m)
357 		m_freem(m);
358 	return IPPROTO_DONE;
359 }
360 
361 void
362 udp6_ctlinput(cmd, sa, d)
363 	int cmd;
364 	struct sockaddr *sa;
365 	void *d;
366 {
367 	struct udphdr uh;
368 	struct ip6_hdr *ip6;
369 	struct mbuf *m;
370 	int off = 0;
371 	struct ip6ctlparam *ip6cp = NULL;
372 	const struct sockaddr_in6 *sa6_src = NULL;
373 	void *cmdarg;
374 	struct inpcb *(*notify) __P((struct inpcb *, int)) = udp_notify;
375 	struct udp_portonly {
376 		u_int16_t uh_sport;
377 		u_int16_t uh_dport;
378 	} *uhp;
379 
380 	if (sa->sa_family != AF_INET6 ||
381 	    sa->sa_len != sizeof(struct sockaddr_in6))
382 		return;
383 
384 	if ((unsigned)cmd >= PRC_NCMDS)
385 		return;
386 	if (PRC_IS_REDIRECT(cmd))
387 		notify = in6_rtchange, d = NULL;
388 	else if (cmd == PRC_HOSTDEAD)
389 		d = NULL;
390 	else if (inet6ctlerrmap[cmd] == 0)
391 		return;
392 
393 	/* if the parameter is from icmp6, decode it. */
394 	if (d != NULL) {
395 		ip6cp = (struct ip6ctlparam *)d;
396 		m = ip6cp->ip6c_m;
397 		ip6 = ip6cp->ip6c_ip6;
398 		off = ip6cp->ip6c_off;
399 		cmdarg = ip6cp->ip6c_cmdarg;
400 		sa6_src = ip6cp->ip6c_src;
401 	} else {
402 		m = NULL;
403 		ip6 = NULL;
404 		cmdarg = NULL;
405 		sa6_src = &sa6_any;
406 	}
407 
408 	if (ip6) {
409 		/*
410 		 * XXX: We assume that when IPV6 is non NULL,
411 		 * M and OFF are valid.
412 		 */
413 
414 		/* check if we can safely examine src and dst ports */
415 		if (m->m_pkthdr.len < off + sizeof(*uhp))
416 			return;
417 
418 		bzero(&uh, sizeof(uh));
419 		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
420 
421 		(void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
422 				     (struct sockaddr *)ip6cp->ip6c_src,
423 				     uh.uh_sport, cmd, cmdarg, notify);
424 	} else
425 		(void) in6_pcbnotify(&udbinfo, sa, 0,
426 				     (const struct sockaddr *)sa6_src,
427 				     0, cmd, cmdarg, notify);
428 }
429 
430 static int
431 udp6_getcred(SYSCTL_HANDLER_ARGS)
432 {
433 	struct xucred xuc;
434 	struct sockaddr_in6 addrs[2];
435 	struct inpcb *inp;
436 	int error;
437 
438 	error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_GETCRED,
439 	    SUSER_ALLOWJAIL);
440 	if (error)
441 		return (error);
442 
443 	if (req->newlen != sizeof(addrs))
444 		return (EINVAL);
445 	if (req->oldlen != sizeof(struct xucred))
446 		return (EINVAL);
447 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
448 	if (error)
449 		return (error);
450 	if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 ||
451 	    (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) {
452 		return (error);
453 	}
454 	INP_INFO_RLOCK(&udbinfo);
455 	inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
456 				 addrs[1].sin6_port,
457 				 &addrs[0].sin6_addr, addrs[0].sin6_port,
458 				 1, NULL);
459 	if (inp == NULL) {
460 		INP_INFO_RUNLOCK(&udbinfo);
461 		return (ENOENT);
462 	}
463 	INP_LOCK(inp);
464 	KASSERT(inp->inp_socket != NULL,
465 	    ("udp6_getcred: inp_socket == NULL"));
466 	cru2x(inp->inp_socket->so_cred, &xuc);
467 	INP_UNLOCK(inp);
468 	INP_INFO_RUNLOCK(&udbinfo);
469 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
470 	return (error);
471 }
472 
473 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
474 	    0, 0,
475 	    udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
476 
477 static void
478 udp6_abort(struct socket *so)
479 {
480 	struct inpcb *inp;
481 
482 	inp = sotoinpcb(so);
483 	KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
484 
485 #ifdef INET
486 	if (inp->inp_vflag & INP_IPV4) {
487 		struct pr_usrreqs *pru;
488 
489 		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
490 		(*pru->pru_abort)(so);
491 		return;
492 	}
493 #endif
494 
495 	INP_INFO_WLOCK(&udbinfo);
496 	INP_LOCK(inp);
497 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
498 		in6_pcbdisconnect(inp);
499 		inp->in6p_laddr = in6addr_any;
500 		soisdisconnected(so);
501 	}
502 	INP_UNLOCK(inp);
503 	INP_INFO_WUNLOCK(&udbinfo);
504 }
505 
506 static int
507 udp6_attach(struct socket *so, int proto, struct thread *td)
508 {
509 	struct inpcb *inp;
510 	int error;
511 
512 	inp = sotoinpcb(so);
513 	KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
514 
515 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
516 		error = soreserve(so, udp_sendspace, udp_recvspace);
517 		if (error)
518 			return error;
519 	}
520 	INP_INFO_WLOCK(&udbinfo);
521 	error = in_pcballoc(so, &udbinfo);
522 	if (error) {
523 		INP_INFO_WUNLOCK(&udbinfo);
524 		return error;
525 	}
526 	inp = (struct inpcb *)so->so_pcb;
527 	INP_INFO_WUNLOCK(&udbinfo);
528 	inp->inp_vflag |= INP_IPV6;
529 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
530 		inp->inp_vflag |= INP_IPV4;
531 	inp->in6p_hops = -1;	/* use kernel default */
532 	inp->in6p_cksum = -1;	/* just to be sure */
533 	/*
534 	 * XXX: ugly!!
535 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
536 	 * because the socket may be bound to an IPv6 wildcard address,
537 	 * which may match an IPv4-mapped IPv6 address.
538 	 */
539 	inp->inp_ip_ttl = ip_defttl;
540 	INP_UNLOCK(inp);
541 	return 0;
542 }
543 
544 static int
545 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
546 {
547 	struct inpcb *inp;
548 	int error;
549 
550 	inp = sotoinpcb(so);
551 	KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
552 
553 	INP_INFO_WLOCK(&udbinfo);
554 	INP_LOCK(inp);
555 	inp->inp_vflag &= ~INP_IPV4;
556 	inp->inp_vflag |= INP_IPV6;
557 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
558 		struct sockaddr_in6 *sin6_p;
559 
560 		sin6_p = (struct sockaddr_in6 *)nam;
561 
562 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
563 			inp->inp_vflag |= INP_IPV4;
564 		else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
565 			struct sockaddr_in sin;
566 
567 			in6_sin6_2_sin(&sin, sin6_p);
568 			inp->inp_vflag |= INP_IPV4;
569 			inp->inp_vflag &= ~INP_IPV6;
570 			error = in_pcbbind(inp, (struct sockaddr *)&sin,
571 			    td->td_ucred);
572 			goto out;
573 		}
574 	}
575 
576 	error = in6_pcbbind(inp, nam, td->td_ucred);
577 out:
578 	INP_UNLOCK(inp);
579 	INP_INFO_WUNLOCK(&udbinfo);
580 	return error;
581 }
582 
583 static void
584 udp6_close(struct socket *so)
585 {
586 	struct inpcb *inp;
587 
588 	inp = sotoinpcb(so);
589 	KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
590 
591 #ifdef INET
592 	if (inp->inp_vflag & INP_IPV4) {
593 		struct pr_usrreqs *pru;
594 
595 		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
596 		(*pru->pru_disconnect)(so);
597 		return;
598 	}
599 #endif
600 	INP_INFO_WLOCK(&udbinfo);
601 	INP_LOCK(inp);
602 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
603 		in6_pcbdisconnect(inp);
604 		inp->in6p_laddr = in6addr_any;
605 		soisdisconnected(so);
606 	}
607 	INP_UNLOCK(inp);
608 	INP_INFO_WUNLOCK(&udbinfo);
609 }
610 
611 static int
612 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
613 {
614 	struct inpcb *inp;
615 	int error;
616 
617 	inp = sotoinpcb(so);
618 	KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
619 
620 	INP_INFO_WLOCK(&udbinfo);
621 	INP_LOCK(inp);
622 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
623 		struct sockaddr_in6 *sin6_p;
624 
625 		sin6_p = (struct sockaddr_in6 *)nam;
626 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
627 			struct sockaddr_in sin;
628 
629 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
630 				error = EISCONN;
631 				goto out;
632 			}
633 			in6_sin6_2_sin(&sin, sin6_p);
634 			error = in_pcbconnect(inp, (struct sockaddr *)&sin,
635 			    td->td_ucred);
636 			if (error == 0) {
637 				inp->inp_vflag |= INP_IPV4;
638 				inp->inp_vflag &= ~INP_IPV6;
639 				soisconnected(so);
640 			}
641 			goto out;
642 		}
643 	}
644 	if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
645 		error = EISCONN;
646 		goto out;
647 	}
648 	error = in6_pcbconnect(inp, nam, td->td_ucred);
649 	if (error == 0) {
650 		if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
651 			/* should be non mapped addr */
652 			inp->inp_vflag &= ~INP_IPV4;
653 			inp->inp_vflag |= INP_IPV6;
654 		}
655 		soisconnected(so);
656 	}
657 out:
658 	INP_UNLOCK(inp);
659 	INP_INFO_WUNLOCK(&udbinfo);
660 	return error;
661 }
662 
663 static void
664 udp6_detach(struct socket *so)
665 {
666 	struct inpcb *inp;
667 
668 	inp = sotoinpcb(so);
669 	KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
670 
671 	INP_INFO_WLOCK(&udbinfo);
672 	INP_LOCK(inp);
673 	in6_pcbdetach(inp);
674 	in6_pcbfree(inp);
675 	INP_INFO_WUNLOCK(&udbinfo);
676 }
677 
678 static int
679 udp6_disconnect(struct socket *so)
680 {
681 	struct inpcb *inp;
682 	int error;
683 
684 	inp = sotoinpcb(so);
685 	KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
686 
687 	INP_INFO_WLOCK(&udbinfo);
688 	INP_LOCK(inp);
689 
690 #ifdef INET
691 	if (inp->inp_vflag & INP_IPV4) {
692 		struct pr_usrreqs *pru;
693 
694 		pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
695 		error = (*pru->pru_disconnect)(so);
696 		goto out;
697 	}
698 #endif
699 
700 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
701 		error = ENOTCONN;
702 		goto out;
703 	}
704 
705 	in6_pcbdisconnect(inp);
706 	inp->in6p_laddr = in6addr_any;
707 	/* XXXRW: so_state locking? */
708 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
709 out:
710 	INP_UNLOCK(inp);
711 	INP_INFO_WUNLOCK(&udbinfo);
712 	return 0;
713 }
714 
715 static int
716 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
717 	  struct mbuf *control, struct thread *td)
718 {
719 	struct inpcb *inp;
720 	int error = 0;
721 
722 	inp = sotoinpcb(so);
723 	KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
724 
725 	INP_INFO_WLOCK(&udbinfo);
726 	INP_LOCK(inp);
727 	if (addr) {
728 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
729 			error = EINVAL;
730 			goto bad;
731 		}
732 		if (addr->sa_family != AF_INET6) {
733 			error = EAFNOSUPPORT;
734 			goto bad;
735 		}
736 	}
737 
738 #ifdef INET
739 	if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
740 		int hasv4addr;
741 		struct sockaddr_in6 *sin6 = 0;
742 
743 		if (addr == 0)
744 			hasv4addr = (inp->inp_vflag & INP_IPV4);
745 		else {
746 			sin6 = (struct sockaddr_in6 *)addr;
747 			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
748 				? 1 : 0;
749 		}
750 		if (hasv4addr) {
751 			struct pr_usrreqs *pru;
752 
753 			if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
754 			    !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
755 				/*
756 				 * when remote addr is IPv4-mapped
757 				 * address, local addr should not be
758 				 * an IPv6 address; since you cannot
759 				 * determine how to map IPv6 source
760 				 * address to IPv4.
761 				 */
762 				error = EINVAL;
763 				goto out;
764 			}
765 			if (sin6)
766 				in6_sin6_2_sin_in_sock(addr);
767 			pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
768 			error = ((*pru->pru_send)(so, flags, m, addr, control,
769 						  td));
770 			/* addr will just be freed in sendit(). */
771 			goto out;
772 		}
773 	}
774 #endif
775 
776 	error = udp6_output(inp, m, addr, control, td);
777 out:
778 	INP_UNLOCK(inp);
779 	INP_INFO_WUNLOCK(&udbinfo);
780 	return error;
781 
782   bad:
783 	INP_UNLOCK(inp);
784 	INP_INFO_WUNLOCK(&udbinfo);
785 	m_freem(m);
786 	return (error);
787 }
788 
789 struct pr_usrreqs udp6_usrreqs = {
790 	.pru_abort =		udp6_abort,
791 	.pru_attach =		udp6_attach,
792 	.pru_bind =		udp6_bind,
793 	.pru_connect =		udp6_connect,
794 	.pru_control =		in6_control,
795 	.pru_detach =		udp6_detach,
796 	.pru_disconnect =	udp6_disconnect,
797 	.pru_peeraddr =		in6_mapped_peeraddr,
798 	.pru_send =		udp6_send,
799 	.pru_shutdown =		udp_shutdown,
800 	.pru_sockaddr =		in6_mapped_sockaddr,
801 	.pru_sosetlabel =	in_pcbsosetlabel,
802 	.pru_close =		udp6_close
803 };
804