]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/udp6_output.c
2 * Copyright (c) 2000-2011 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@
30 /* $FreeBSD: src/sys/netinet6/udp6_output.c,v 1.1.2.3 2001/08/31 13:49:58 jlemon Exp $ */
31 /* $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $ */
34 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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. Neither the name of the project 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.
49 * THIS SOFTWARE IS PROVIDED BY THE PROJECT 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 PROJECT 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
63 * Copyright (c) 1982, 1986, 1989, 1993
64 * The Regents of the University of California. All rights reserved.
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 * notice, this list of conditions and the following disclaimer in the
73 * documentation and/or other materials provided with the distribution.
74 * 3. All advertising materials mentioning features or use of this software
75 * must display the following acknowledgement:
76 * This product includes software developed by the University of
77 * California, Berkeley and its contributors.
78 * 4. Neither the name of the University nor the names of its contributors
79 * may be used to endorse or promote products derived from this software
80 * without specific prior written permission.
82 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
83 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
86 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
97 #include <sys/param.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/proc.h>
108 #include <sys/syslog.h>
110 #include <machine/endian.h>
113 #include <net/route.h>
114 #include <net/if_types.h>
115 #include <net/ntstat.h>
117 #include <netinet/in.h>
118 #include <netinet/in_var.h>
119 #include <netinet/in_systm.h>
120 #include <netinet/ip.h>
121 #include <netinet/ip_var.h>
122 #include <netinet/in_pcb.h>
123 #include <netinet/udp.h>
124 #include <netinet/udp_var.h>
125 #include <netinet/ip6.h>
126 #include <netinet6/ip6_var.h>
127 #include <netinet6/in6_pcb.h>
128 #include <netinet6/udp6_var.h>
129 #include <netinet/icmp6.h>
130 #include <netinet6/ip6protosw.h>
133 #include <netinet6/ipsec.h>
135 #include <netinet6/ipsec6.h>
137 extern int ipsec_bypass
;
142 #include <net/net_osdep.h>
145 * UDP protocol inplementation.
146 * Per RFC 768, August, 1980.
150 #define udp6stat udpstat
151 #define udp6s_opackets udps_opackets
153 static __inline__ u_int16_t
154 get_socket_id(struct socket
* s
)
161 val
= (u_int16_t
)(((uintptr_t)s
) / sizeof(struct socket
));
169 udp6_output(in6p
, m
, addr6
, control
, p
)
172 struct mbuf
*control
;
173 struct sockaddr
*addr6
;
176 u_int32_t ulen
= m
->m_pkthdr
.len
;
177 u_int32_t plen
= sizeof(struct udphdr
) + ulen
;
180 struct in6_addr
*laddr
, *faddr
;
183 struct ip6_pktopts opt
, *optp
= NULL
;
184 struct ip6_moptions
*im6o
;
185 int af
= AF_INET6
, hlen
= sizeof(struct ip6_hdr
);
187 struct sockaddr_in6 tmp
;
188 struct in6_addr storage
;
189 mbuf_traffic_class_t mtc
= MBUF_TC_UNSPEC
;
190 struct ip6_out_args ip6oa
= { IFSCOPE_NONE
, 0 };
192 if (in6p
->inp_flags
& INP_BOUND_IF
)
193 ip6oa
.ip6oa_boundif
= in6p
->inp_boundif
;
195 ip6oa
.ip6oa_nocell
= (in6p
->inp_flags
& INP_NO_IFT_CELLULAR
) ? 1 : 0;
198 mtc
= mbuf_traffic_class_from_control(control
);
200 if ((error
= ip6_setpktopts(control
, &opt
, NULL
, IPPROTO_UDP
)) != 0)
204 optp
= in6p
->in6p_outputopts
;
208 * IPv4 version of udp_output calls in_pcbconnect in this case,
209 * which needs splnet and affects performance.
210 * Since we saw no essential reason for calling in_pcbconnect,
211 * we get rid of such kind of logic, and call in6_selectsrc
212 * and in6_pcbsetport in order to fill in the local address
213 * and the local port.
215 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)addr6
;
216 if (sin6
->sin6_port
== 0) {
217 error
= EADDRNOTAVAIL
;
221 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
)) {
222 /* how about ::ffff:0.0.0.0 case? */
227 /* protect *sin6 from overwrites */
231 faddr
= &sin6
->sin6_addr
;
232 fport
= sin6
->sin6_port
; /* allow 0 port */
234 if (IN6_IS_ADDR_V4MAPPED(faddr
)) {
235 if ((in6p
->in6p_flags
& IN6P_IPV6_V6ONLY
)) {
237 * I believe we should explicitly discard the
238 * packet when mapped addresses are disabled,
239 * rather than send the packet as an IPv6 one.
240 * If we chose the latter approach, the packet
241 * might be sent out on the wire based on the
242 * default route, the situation which we'd
243 * probably want to avoid.
244 * (20010421 jinmei@kame.net)
252 /* KAME hack: embed scopeid */
253 if (in6_embedscope(&sin6
->sin6_addr
, sin6
, in6p
, NULL
,
259 if (!IN6_IS_ADDR_V4MAPPED(faddr
)) {
260 laddr
= in6_selectsrc(sin6
, optp
,
261 in6p
, &in6p
->in6p_route
, NULL
, &storage
,
262 ip6oa
.ip6oa_boundif
, &error
);
264 laddr
= &in6p
->in6p_laddr
; /* XXX */
267 error
= EADDRNOTAVAIL
;
270 if (in6p
->in6p_lport
== 0 &&
271 (error
= in6_pcbsetport(laddr
, in6p
, p
, 0)) != 0)
274 if (IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
)) {
278 if (IN6_IS_ADDR_V4MAPPED(&in6p
->in6p_faddr
)) {
279 if ((in6p
->in6p_flags
& IN6P_IPV6_V6ONLY
)) {
281 * XXX: this case would happen when the
282 * application sets the V6ONLY flag after
283 * connecting the foreign address.
284 * Such applications should be fixed,
287 log(LOG_INFO
, "udp6_output: IPV6_V6ONLY "
288 "option was set for a connected socket\n");
294 laddr
= &in6p
->in6p_laddr
;
295 faddr
= &in6p
->in6p_faddr
;
296 fport
= in6p
->in6p_fport
;
300 hlen
= sizeof(struct ip
);
303 * Calculate data length and get a mbuf
304 * for UDP and IP6 headers.
306 M_PREPEND(m
, hlen
+ sizeof(struct udphdr
), M_DONTWAIT
);
313 * Stuff checksum and output datagram.
315 udp6
= (struct udphdr
*)(mtod(m
, caddr_t
) + hlen
);
316 udp6
->uh_sport
= in6p
->in6p_lport
; /* lport is always set in the PCB */
317 udp6
->uh_dport
= fport
;
319 udp6
->uh_ulen
= htons((u_short
)plen
);
326 ip6
= mtod(m
, struct ip6_hdr
*);
327 ip6
->ip6_flow
= in6p
->in6p_flowinfo
& IPV6_FLOWINFO_MASK
;
328 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
329 ip6
->ip6_vfc
|= IPV6_VERSION
;
330 #if 0 /* ip6_plen will be filled in ip6_output. */
331 ip6
->ip6_plen
= htons((u_short
)plen
);
333 ip6
->ip6_nxt
= IPPROTO_UDP
;
334 ip6
->ip6_hlim
= in6_selecthlim(in6p
,
335 in6p
->in6p_route
.ro_rt
?
336 in6p
->in6p_route
.ro_rt
->rt_ifp
: NULL
);
337 ip6
->ip6_src
= *laddr
;
338 ip6
->ip6_dst
= *faddr
;
340 udp6
->uh_sum
= in6_cksum_phdr(laddr
, faddr
,
341 htonl(plen
), htonl(IPPROTO_UDP
));
342 m
->m_pkthdr
.csum_flags
= CSUM_UDPIPV6
;
343 m
->m_pkthdr
.csum_data
= offsetof(struct udphdr
, uh_sum
);
345 flags
= IPV6_OUTARGS
;
347 udp6stat
.udp6s_opackets
++;
349 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, in6p
->in6p_socket
) != 0) {
354 m
->m_pkthdr
.socket_id
= get_socket_id(in6p
->in6p_socket
);
356 set_packet_tclass(m
, in6p
->in6p_socket
, mtc
, 1);
358 im6o
= in6p
->in6p_moptions
;
362 error
= ip6_output(m
, optp
, &in6p
->in6p_route
,
363 flags
, im6o
, NULL
, &ip6oa
);
368 if (error
== 0 && nstat_collect
) {
369 locked_add_64(&in6p
->inp_stat
->txpackets
, 1);
370 locked_add_64(&in6p
->inp_stat
->txbytes
, ulen
);
373 if (in6p
->in6p_route
.ro_rt
!= NULL
) {
374 struct rtentry
*rt
= in6p
->in6p_route
.ro_rt
;
377 if ((rt
->rt_flags
& RTF_MULTICAST
) ||
378 in6p
->in6p_socket
== NULL
||
379 !(in6p
->in6p_socket
->so_state
& SS_ISCONNECTED
)) {
380 rt
= NULL
; /* unusable */
383 * Always discard the cached route for unconnected
384 * socket or if it is a multicast route.
387 rtfree(in6p
->in6p_route
.ro_rt
);
388 in6p
->in6p_route
.ro_rt
= NULL
;
391 * If this is a connected socket and the destination
392 * route is not multicast, update outif with that of
393 * the route interface index used by IP.
395 if (rt
!= NULL
&& (outif
= rt
->rt_ifp
->if_index
) !=
396 in6p
->in6p_last_outif
)
397 in6p
->in6p_last_outif
= outif
;
401 error
= EAFNOSUPPORT
;
412 ip6_clearpktopts(optp
, -1);