2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 /* $FreeBSD: src/sys/netinet6/udp6_usrreq.c,v 1.6.2.6 2001/07/29 19:32:40 ume Exp $ */
30 /* $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $ */
33 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the project nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * Copyright (c) 1982, 1986, 1989, 1993
63 * The Regents of the University of California. All rights reserved.
65 * Redistribution and use in source and binary forms, with or without
66 * modification, are permitted provided that the following conditions
68 * 1. Redistributions of source code must retain the above copyright
69 * notice, this list of conditions and the following disclaimer.
70 * 2. Redistributions in binary form must reproduce the above copyright
71 * notice, this list of conditions and the following disclaimer in the
72 * documentation and/or other materials provided with the distribution.
73 * 3. All advertising materials mentioning features or use of this software
74 * must display the following acknowledgement:
75 * This product includes software developed by the University of
76 * California, Berkeley and its contributors.
77 * 4. Neither the name of the University nor the names of its contributors
78 * may be used to endorse or promote products derived from this software
79 * without specific prior written permission.
81 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
82 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
83 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
84 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
85 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
86 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
87 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
88 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
89 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
90 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
93 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
96 #include <sys/param.h>
97 #include <sys/kernel.h>
98 #include <sys/malloc.h>
100 #include <sys/protosw.h>
101 #include <sys/socket.h>
102 #include <sys/socketvar.h>
103 #include <sys/sysctl.h>
104 #include <sys/errno.h>
105 #include <sys/stat.h>
106 #include <sys/systm.h>
107 #include <sys/syslog.h>
108 #include <sys/proc.h>
109 #include <sys/kauth.h>
112 #include <net/route.h>
113 #include <net/if_types.h>
114 #include <net/ntstat.h>
116 #include <netinet/in.h>
117 #include <netinet/in_systm.h>
118 #include <netinet/ip.h>
119 #include <netinet/in_pcb.h>
120 #include <netinet/in_var.h>
121 #include <netinet/ip_var.h>
122 #include <netinet/udp.h>
123 #include <netinet/udp_var.h>
124 #include <netinet/ip6.h>
125 #include <netinet6/ip6_var.h>
126 #include <netinet6/in6_pcb.h>
127 #include <netinet/icmp6.h>
128 #include <netinet6/udp6_var.h>
129 #include <netinet6/ip6protosw.h>
132 #include <netinet6/ipsec.h>
133 #include <netinet6/ipsec6.h>
134 extern int ipsec_bypass
;
138 * UDP protocol inplementation.
139 * Per RFC 768, August, 1980.
142 extern struct protosw inetsw
[];
143 static int udp6_detach(struct socket
*so
);
144 static void udp6_append(struct inpcb
*, struct ip6_hdr
*,
145 struct sockaddr_in6
*, struct mbuf
*, int);
147 extern void ipfwsyslog( int level
, const char *format
,...);
148 extern int fw_verbose
;
151 #define log_in_vain_log( a ) { \
152 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
158 #define log_in_vain_log( a ) { log a; }
162 * subroutine of udp6_input(), mainly for source code readability.
165 udp6_append(struct inpcb
*last
, __unused
struct ip6_hdr
*ip6
,
166 struct sockaddr_in6
*udp_in6
, struct mbuf
*n
, int off
)
168 struct mbuf
*opts
= NULL
;
171 if (mac_inpcb_check_deliver(last
, n
, AF_INET6
, SOCK_DGRAM
) != 0) {
176 if ((last
->in6p_flags
& IN6P_CONTROLOPTS
) != 0 ||
177 (last
->in6p_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
178 (last
->in6p_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
179 ret
= ip6_savecontrol(last
, n
, &opts
);
188 locked_add_64(&last
->inp_stat
->rxpackets
, 1);
189 locked_add_64(&last
->inp_stat
->rxbytes
, n
->m_pkthdr
.len
);
191 so_recv_data_stat(last
->in6p_socket
, n
, 0);
192 if (sbappendaddr(&last
->in6p_socket
->so_rcv
,
193 (struct sockaddr
*)udp_in6
, n
, opts
, NULL
) == 0)
194 udpstat
.udps_fullsock
++;
196 sorwakeup(last
->in6p_socket
);
205 #pragma unused(proto)
206 struct mbuf
*m
= *mp
;
208 register struct ip6_hdr
*ip6
;
209 register struct udphdr
*uh
;
210 register struct inpcb
*in6p
;
211 struct mbuf
*opts
= NULL
;
213 int plen
, ulen
, ret
= 0;
214 struct sockaddr_in6 udp_in6
;
215 struct inpcbinfo
*pcbinfo
= &udbinfo
;
216 struct sockaddr_in6 fromsa
;
218 IP6_EXTHDR_CHECK(m
, off
, sizeof(struct udphdr
), return IPPROTO_DONE
);
220 ifp
= m
->m_pkthdr
.rcvif
;
221 ip6
= mtod(m
, struct ip6_hdr
*);
223 #if defined(NFAITH) && 0 < NFAITH
224 if (faithprefix(&ip6
->ip6_dst
)) {
225 /* XXX send icmp6 host/port unreach? */
231 udpstat
.udps_ipackets
++;
233 plen
= ntohs(ip6
->ip6_plen
) - off
+ sizeof(*ip6
);
234 uh
= (struct udphdr
*)((caddr_t
)ip6
+ off
);
235 ulen
= ntohs((u_short
)uh
->uh_ulen
);
238 udpstat
.udps_badlen
++;
242 /* destination port of 0 is illegal, based on RFC768. */
243 if (uh
->uh_dport
== 0)
247 * Checksum extended UDP header and data.
250 if ((apple_hwcksum_rx
!= 0) && (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
251 uh
->uh_sum
= m
->m_pkthdr
.csum_data
;
252 uh
->uh_sum
^= 0xffff;
255 if (in6_cksum(m
, IPPROTO_UDP
, off
, ulen
) != 0) {
256 udpstat
.udps_badsum
++;
263 udpstat
.udps_nosum
++;
267 * Construct sockaddr format source address.
269 init_sin6(&fromsa
, m
);
270 fromsa
.sin6_port
= uh
->uh_sport
;
273 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
274 int reuse_sock
= 0, mcast_delivered
= 0;
275 struct ip6_moptions
*imo
;
276 struct mbuf
*n
= NULL
;
279 * Deliver a multicast datagram to all sockets
280 * for which the local and remote addresses and ports match
281 * those of the incoming datagram. This allows more than
282 * one process to receive multicasts on the same port.
283 * (This really ought to be done for unicast datagrams as
284 * well, but that would cause problems with existing
285 * applications that open both address-specific sockets and
286 * a wildcard socket listening to the same port -- they would
287 * end up receiving duplicates of every unicast datagram.
288 * Those applications open the multiple sockets to overcome an
289 * inadequacy of the UDP socket interface, but for backwards
290 * compatibility we avoid the problem here rather than
291 * fixing the interface. Maybe 4.5BSD will remedy this?)
295 * In a case that laddr should be set to the link-local
296 * address (this happens in RIPng), the multicast address
297 * specified in the received packet does not match with
298 * laddr. To cure this situation, the matching is relaxed
299 * if the receiving interface is the same as one specified
300 * in the socket and if the destination multicast address
301 * matches one of the multicast groups specified in the socket.
305 * Construct sockaddr format source address.
307 init_sin6(&udp_in6
, m
); /* general init */
308 udp_in6
.sin6_port
= uh
->uh_sport
;
310 * KAME note: usually we drop udphdr from mbuf here.
311 * We need udphdr for IPsec processing so we do that later.
315 * Locate pcb(s) for datagram.
316 * (Algorithm copied from raw_intr().)
318 lck_rw_lock_shared(pcbinfo
->mtx
);
320 LIST_FOREACH(in6p
, &udb
, inp_list
) {
322 if ((in6p
->inp_vflag
& INP_IPV6
) == 0)
325 if (in_pcb_checkstate(in6p
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
)
328 udp_lock(in6p
->in6p_socket
, 1, 0);
330 if (in_pcb_checkstate(in6p
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
331 udp_unlock(in6p
->in6p_socket
, 1, 0);
334 if (in6p
->in6p_lport
!= uh
->uh_dport
) {
335 udp_unlock(in6p
->in6p_socket
, 1, 0);
340 * Handle socket delivery policy for any-source
341 * and source-specific multicast. [RFC3678]
343 imo
= in6p
->in6p_moptions
;
344 if (imo
&& IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
)) {
345 struct sockaddr_in6 mcaddr
;
349 bzero(&mcaddr
, sizeof(struct sockaddr_in6
));
350 mcaddr
.sin6_len
= sizeof(struct sockaddr_in6
);
351 mcaddr
.sin6_family
= AF_INET6
;
352 mcaddr
.sin6_addr
= ip6
->ip6_dst
;
354 blocked
= im6o_mc_filter(imo
, ifp
,
355 (struct sockaddr
*)&mcaddr
,
356 (struct sockaddr
*)&fromsa
);
358 if (blocked
!= MCAST_PASS
) {
359 udp_unlock(in6p
->in6p_socket
, 1, 0);
363 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
)) {
364 if (!IN6_ARE_ADDR_EQUAL(&in6p
->in6p_faddr
,
366 in6p
->in6p_fport
!= uh
->uh_sport
) {
367 udp_unlock(in6p
->in6p_socket
, 1, 0);
372 reuse_sock
= in6p
->inp_socket
->so_options
&
373 (SO_REUSEPORT
| SO_REUSEADDR
);
378 /* Check AH/ESP integrity. */
379 if (ipsec_bypass
== 0) {
380 if (ipsec6_in_reject_so(m
, in6p
->inp_socket
)) {
381 IPSEC_STAT_INCREMENT(ipsec6stat
.in_polvio
);
382 /* do not inject data to pcb */
391 * m_copy(m, offset, ...) below.
392 * sbappendaddr() expects M_PKTHDR,
393 * and m_copy() will copy M_PKTHDR
394 * only if offset is 0.
397 n
= m_copy(m
, 0, M_COPYALL
);
398 udp6_append(in6p
, ip6
, &udp_in6
, m
,
399 off
+ sizeof (struct udphdr
));
402 udp_unlock(in6p
->in6p_socket
, 1, 0);
405 * Don't look for additional matches if this one does
406 * not have either the SO_REUSEPORT or SO_REUSEADDR
407 * socket options set. This heuristic avoids searching
408 * through all pcbs in the common case of a non-shared
409 * port. It assumes that an application will never
410 * clear these options after setting them.
412 if (reuse_sock
== 0 || ((m
= n
) == NULL
))
415 * Recompute IP and UDP header pointers for new mbuf
417 ip6
= mtod(m
, struct ip6_hdr
*);
418 uh
= (struct udphdr
*)((caddr_t
)ip6
+ off
);
420 lck_rw_done(pcbinfo
->mtx
);
422 if (mcast_delivered
== 0) {
424 * No matching pcb found; discard datagram.
425 * (No need to send an ICMP Port Unreachable
426 * for a broadcast or multicast datgram.)
428 udpstat
.udps_noport
++;
430 udpstat
.udps_noportmcast
++;
435 if (reuse_sock
!= 0) /* free the extra copy of mbuf */
440 * Locate pcb for datagram.
442 in6p
= in6_pcblookup_hash(&udbinfo
, &ip6
->ip6_src
, uh
->uh_sport
,
443 &ip6
->ip6_dst
, uh
->uh_dport
, 1,
447 char buf
[INET6_ADDRSTRLEN
];
449 strlcpy(buf
, ip6_sprintf(&ip6
->ip6_dst
), sizeof(buf
));
450 if (log_in_vain
!= 3)
452 "Connection attempt to UDP %s:%d from %s:%d\n",
453 buf
, ntohs(uh
->uh_dport
),
454 ip6_sprintf(&ip6
->ip6_src
), ntohs(uh
->uh_sport
));
455 else if (!(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
456 !IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &ip6
->ip6_src
))
457 log_in_vain_log((LOG_INFO
,
458 "Connection attempt to UDP %s:%d from %s:%d\n",
459 buf
, ntohs(uh
->uh_dport
),
460 ip6_sprintf(&ip6
->ip6_src
), ntohs(uh
->uh_sport
)));
462 udpstat
.udps_noport
++;
463 if (m
->m_flags
& M_MCAST
) {
464 printf("UDP6: M_MCAST is set in a unicast packet.\n");
466 udpstat
.udps_noportmcast
++;
470 icmp6_error(m
, ICMP6_DST_UNREACH
, ICMP6_DST_UNREACH_NOPORT
, 0);
475 * Check AH/ESP integrity.
477 if (ipsec_bypass
== 0) {
478 if (ipsec6_in_reject_so(m
, in6p
->in6p_socket
)) {
479 IPSEC_STAT_INCREMENT(ipsec6stat
.in_polvio
);
480 in_pcb_checkstate(in6p
, WNT_RELEASE
, 0);
487 * Construct sockaddr format source address.
488 * Stuff source address and datagram in user buffer.
490 udp_lock(in6p
->in6p_socket
, 1, 0);
492 if (in_pcb_checkstate(in6p
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
493 udp_unlock(in6p
->in6p_socket
, 1, 0);
497 init_sin6(&udp_in6
, m
); /* general init */
498 udp_in6
.sin6_port
= uh
->uh_sport
;
499 if ((in6p
->in6p_flags
& IN6P_CONTROLOPTS
) != 0 ||
500 (in6p
->in6p_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
501 (in6p
->in6p_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
502 ret
= ip6_savecontrol(in6p
, m
, &opts
);
504 udp_unlock(in6p
->in6p_socket
, 1, 0);
508 m_adj(m
, off
+ sizeof(struct udphdr
));
510 locked_add_64(&in6p
->inp_stat
->rxpackets
, 1);
511 locked_add_64(&in6p
->inp_stat
->rxbytes
, m
->m_pkthdr
.len
);
513 so_recv_data_stat(in6p
->in6p_socket
, m
, 0);
514 if (sbappendaddr(&in6p
->in6p_socket
->so_rcv
,
515 (struct sockaddr
*)&udp_in6
,
516 m
, opts
, NULL
) == 0) {
519 udpstat
.udps_fullsock
++;
520 udp_unlock(in6p
->in6p_socket
, 1, 0);
523 sorwakeup(in6p
->in6p_socket
);
524 udp_unlock(in6p
->in6p_socket
, 1, 0);
544 struct ip6ctlparam
*ip6cp
= NULL
;
545 const struct sockaddr_in6
*sa6_src
= NULL
;
546 void (*notify
)(struct inpcb
*, int) = udp_notify
;
547 struct udp_portonly
{
552 if (sa
->sa_family
!= AF_INET6
||
553 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
556 if ((unsigned)cmd
>= PRC_NCMDS
)
558 if (PRC_IS_REDIRECT(cmd
))
559 notify
= in6_rtchange
, d
= NULL
;
560 else if (cmd
== PRC_HOSTDEAD
)
562 else if (inet6ctlerrmap
[cmd
] == 0)
565 /* if the parameter is from icmp6, decode it. */
567 ip6cp
= (struct ip6ctlparam
*)d
;
569 ip6
= ip6cp
->ip6c_ip6
;
570 off
= ip6cp
->ip6c_off
;
571 sa6_src
= ip6cp
->ip6c_src
;
580 * XXX: We assume that when IPV6 is non NULL,
581 * M and OFF are valid.
584 /* check if we can safely examine src and dst ports */
585 if (m
->m_pkthdr
.len
< off
+ sizeof(*uhp
))
588 bzero(&uh
, sizeof(uh
));
589 m_copydata(m
, off
, sizeof(*uhp
), (caddr_t
)&uh
);
591 (void) in6_pcbnotify(&udbinfo
, sa
, uh
.uh_dport
,
592 (struct sockaddr
*)ip6cp
->ip6c_src
,
593 uh
.uh_sport
, cmd
, NULL
, notify
);
595 (void) in6_pcbnotify(&udbinfo
, sa
, 0, (struct sockaddr
*)&sa6_src
,
596 0, cmd
, NULL
, notify
);
601 udp6_getcred SYSCTL_HANDLER_ARGS
603 struct sockaddr_in6 addrs
[2];
607 error
= proc_suser(req
->p
);
611 if (req
->newlen
!= sizeof(addrs
))
613 if (req
->oldlen
!= sizeof(*(kauth_cred_t
)0))
615 error
= SYSCTL_IN(req
, addrs
, sizeof(addrs
));
619 inp
= in6_pcblookup_hash(&udbinfo
, &addrs
[1].sin6_addr
,
621 &addrs
[0].sin6_addr
, addrs
[0].sin6_port
,
623 if (!inp
|| !inp
->inp_socket
|| !inp
->inp_socket
->so_cred
) {
628 * XXX This should not be copying out a credential!!!! This
629 * XXX is an opaque type, and is not intended to be introspected,
630 * XXX and the size of this structure *WILL* change as planned MACF
631 * XXX and kauth changes go forward.
633 error
= SYSCTL_OUT(req
, inp
->inp_socket
->so_cred
->pc_ucred
,
634 sizeof(*(kauth_cred_t
)0));
641 SYSCTL_PROC(_net_inet6_udp6
, OID_AUTO
, getcred
, CTLTYPE_OPAQUE
|CTLFLAG_RW
,
643 udp6_getcred
, "S,ucred", "Get the ucred of a UDP6 connection");
647 udp6_abort(struct socket
*so
)
653 return EINVAL
; /* ??? possible? panic instead? */
654 soisdisconnected(so
);
660 udp6_attach(struct socket
*so
, __unused
int proto
, struct proc
*p
)
669 error
= in_pcballoc(so
, &udbinfo
, p
);
673 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
674 error
= soreserve(so
, udp_sendspace
, udp_recvspace
);
678 inp
= (struct inpcb
*)so
->so_pcb
;
679 inp
->inp_vflag
|= INP_IPV6
;
680 if (ip6_mapped_addr_on
)
681 inp
->inp_vflag
|= INP_IPV4
;
682 inp
->in6p_hops
= -1; /* use kernel default */
683 inp
->in6p_cksum
= -1; /* just to be sure */
686 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
687 * because the socket may be bound to an IPv6 wildcard address,
688 * which may match an IPv4-mapped IPv6 address.
690 inp
->inp_ip_ttl
= ip_defttl
;
691 nstat_udp_new_pcb(inp
);
696 udp6_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
705 inp
->inp_vflag
&= ~INP_IPV4
;
706 inp
->inp_vflag
|= INP_IPV6
;
707 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
708 struct sockaddr_in6
*sin6_p
;
710 sin6_p
= (struct sockaddr_in6
*)nam
;
712 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p
->sin6_addr
))
713 inp
->inp_vflag
|= INP_IPV4
;
714 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p
->sin6_addr
)) {
715 struct sockaddr_in sin
;
717 in6_sin6_2_sin(&sin
, sin6_p
);
718 inp
->inp_vflag
|= INP_IPV4
;
719 inp
->inp_vflag
&= ~INP_IPV6
;
720 error
= in_pcbbind(inp
, (struct sockaddr
*)&sin
, p
);
725 error
= in6_pcbbind(inp
, nam
, p
);
730 udp6_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
739 if ((inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
740 struct sockaddr_in6
*sin6_p
;
742 sin6_p
= (struct sockaddr_in6
*)nam
;
743 if (IN6_IS_ADDR_V4MAPPED(&sin6_p
->sin6_addr
)) {
744 struct sockaddr_in sin
;
746 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
748 in6_sin6_2_sin(&sin
, sin6_p
);
749 error
= in_pcbconnect(inp
, (struct sockaddr
*)&sin
, p
, NULL
);
751 inp
->inp_vflag
|= INP_IPV4
;
752 inp
->inp_vflag
&= ~INP_IPV6
;
759 if (!IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
))
761 error
= in6_pcbconnect(inp
, nam
, p
);
763 if (ip6_mapped_addr_on
|| (inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) { /* should be non mapped addr */
764 inp
->inp_vflag
&= ~INP_IPV4
;
765 inp
->inp_vflag
|= INP_IPV6
;
773 udp6_detach(struct socket
*so
)
785 udp6_disconnect(struct socket
*so
)
793 if (inp
->inp_vflag
& INP_IPV4
) {
794 struct pr_usrreqs
*pru
;
796 pru
= ip_protox
[IPPROTO_UDP
]->pr_usrreqs
;
797 return ((*pru
->pru_disconnect
)(so
));
800 if (IN6_IS_ADDR_UNSPECIFIED(&inp
->in6p_faddr
))
803 in6_pcbdisconnect(inp
);
804 inp
->in6p_laddr
= in6addr_any
;
805 inp
->in6p_last_outif
= 0;
806 so
->so_state
&= ~SS_ISCONNECTED
; /* XXX */
811 udp6_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*addr
,
812 struct mbuf
*control
, struct proc
*p
)
824 if (addr
->sa_len
!= sizeof(struct sockaddr_in6
)) {
828 if (addr
->sa_family
!= AF_INET6
) {
829 error
= EAFNOSUPPORT
;
834 if (ip6_mapped_addr_on
|| (inp
->inp_flags
& IN6P_IPV6_V6ONLY
) == 0) {
836 struct sockaddr_in6
*sin6
= 0;
839 hasv4addr
= (inp
->inp_vflag
& INP_IPV4
);
841 sin6
= (struct sockaddr_in6
*)addr
;
842 hasv4addr
= IN6_IS_ADDR_V4MAPPED(&sin6
->sin6_addr
)
846 struct pr_usrreqs
*pru
;
849 in6_sin6_2_sin_in_sock(addr
);
850 pru
= ip_protox
[IPPROTO_UDP
]->pr_usrreqs
;
851 error
= ((*pru
->pru_send
)(so
, flags
, m
, addr
, control
,
853 /* addr will just be freed in sendit(). */
858 return udp6_output(inp
, m
, addr
, control
, p
);
865 struct pr_usrreqs udp6_usrreqs
= {
866 udp6_abort
, pru_accept_notsupp
, udp6_attach
, udp6_bind
, udp6_connect
,
867 pru_connect2_notsupp
, in6_control
, udp6_detach
, udp6_disconnect
,
868 pru_listen_notsupp
, in6_mapped_peeraddr
, pru_rcvd_notsupp
,
869 pru_rcvoob_notsupp
, udp6_send
, pru_sense_null
, udp_shutdown
,
870 in6_mapped_sockaddr
, sosend
, soreceive
, pru_sopoll_notsupp