2 * Copyright (c) 2000-2018 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/in_tclass.h>
121 #include <netinet/ip.h>
122 #include <netinet/ip_var.h>
123 #include <netinet/in_pcb.h>
124 #include <netinet/udp.h>
125 #include <netinet/udp_var.h>
126 #include <netinet/ip6.h>
127 #include <netinet6/ip6_var.h>
128 #include <netinet6/in6_pcb.h>
129 #include <netinet6/udp6_var.h>
130 #include <netinet/icmp6.h>
131 #include <netinet6/ip6protosw.h>
134 #include <net/necp.h>
137 #include <net/net_osdep.h>
140 * UDP protocol inplementation.
141 * Per RFC 768, August, 1980.
143 extern int soreserveheadroom
;
146 udp6_output(struct in6pcb
*in6p
, struct mbuf
*m
, struct sockaddr
*addr6
,
147 struct mbuf
*control
, struct proc
*p
)
149 u_int32_t ulen
= m
->m_pkthdr
.len
;
150 u_int32_t plen
= sizeof (struct udphdr
) + ulen
;
153 struct in6_addr
*laddr
, *faddr
;
156 struct ip6_pktopts opt
, *optp
= NULL
;
157 struct ip6_moptions
*im6o
;
158 int af
= AF_INET6
, hlen
= sizeof (struct ip6_hdr
);
160 struct sockaddr_in6 tmp
;
161 struct in6_addr storage
;
162 int sotc
= SO_TC_UNSPEC
;
163 int netsvctype
= _NET_SERVICE_TYPE_UNSPEC
;
164 struct ip6_out_args ip6oa
;
165 struct flowadv
*adv
= &ip6oa
.ip6oa_flowadv
;
166 struct socket
*so
= in6p
->in6p_socket
;
170 bzero(&ip6oa
, sizeof(ip6oa
));
171 ip6oa
.ip6oa_boundif
= IFSCOPE_NONE
;
172 ip6oa
.ip6oa_flags
= IP6OAF_SELECT_SRCIF
;
174 /* Enable flow advisory only when connected */
175 flowadv
= (so
->so_state
& SS_ISCONNECTED
) ? 1 : 0;
177 if (flowadv
&& INP_WAIT_FOR_IF_FEEDBACK(in6p
)) {
182 if (in6p
->inp_flags
& INP_BOUND_IF
) {
183 ip6oa
.ip6oa_boundif
= in6p
->inp_boundifp
->if_index
;
184 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_IF
;
186 if (INP_NO_CELLULAR(in6p
))
187 ip6oa
.ip6oa_flags
|= IP6OAF_NO_CELLULAR
;
188 if (INP_NO_EXPENSIVE(in6p
))
189 ip6oa
.ip6oa_flags
|= IP6OAF_NO_EXPENSIVE
;
190 if (INP_AWDL_UNRESTRICTED(in6p
))
191 ip6oa
.ip6oa_flags
|= IP6OAF_AWDL_UNRESTRICTED
;
192 if (INP_INTCOPROC_ALLOWED(in6p
))
193 ip6oa
.ip6oa_flags
|= IP6OAF_INTCOPROC_ALLOWED
;
196 sotc
= so_tc_from_control(control
, &netsvctype
);
197 if ((error
= ip6_setpktopts(control
, &opt
,
198 NULL
, IPPROTO_UDP
)) != 0)
202 optp
= in6p
->in6p_outputopts
;
204 if (sotc
== SO_TC_UNSPEC
) {
205 sotc
= so
->so_traffic_class
;
206 netsvctype
= so
->so_netsvctype
;
208 ip6oa
.ip6oa_sotc
= sotc
;
209 ip6oa
.ip6oa_netsvctype
= netsvctype
;
213 * IPv4 version of udp_output calls in_pcbconnect in this case,
214 * which has its costs.
216 * Since we saw no essential reason for calling in_pcbconnect,
217 * we get rid of such kind of logic, and call in6_selectsrc
218 * and in6_pcbsetport in order to fill in the local address
219 * and the local port.
221 struct sockaddr_in6
*sin6
=
222 (struct sockaddr_in6
*)(void *)addr6
;
224 if (sin6
->sin6_port
== 0) {
225 error
= EADDRNOTAVAIL
;
229 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
)) {
230 /* how about ::ffff:0.0.0.0 case? */
235 /* protect *sin6 from overwrites */
239 faddr
= &sin6
->sin6_addr
;
240 fport
= sin6
->sin6_port
; /* allow 0 port */
242 if (IN6_IS_ADDR_V4MAPPED(faddr
)) {
243 if ((in6p
->in6p_flags
& IN6P_IPV6_V6ONLY
)) {
245 * I believe we should explicitly discard the
246 * packet when mapped addresses are disabled,
247 * rather than send the packet as an IPv6 one.
248 * If we chose the latter approach, the packet
249 * might be sent out on the wire based on the
250 * default route, the situation which we'd
251 * probably want to avoid.
252 * (20010421 jinmei@kame.net)
261 /* KAME hack: embed scopeid */
262 if (in6_embedscope(&sin6
->sin6_addr
, sin6
, in6p
, NULL
,
268 if (!IN6_IS_ADDR_V4MAPPED(faddr
)) {
269 laddr
= in6_selectsrc(sin6
, optp
,
270 in6p
, &in6p
->in6p_route
, NULL
, &storage
,
271 ip6oa
.ip6oa_boundif
, &error
);
273 laddr
= &in6p
->in6p_laddr
; /* XXX */
276 error
= EADDRNOTAVAIL
;
279 if (in6p
->in6p_lport
== 0 &&
280 (error
= in6_pcbsetport(laddr
, in6p
, p
, 0)) != 0)
283 if (IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
)) {
287 if (IN6_IS_ADDR_V4MAPPED(&in6p
->in6p_faddr
)) {
288 if ((in6p
->in6p_flags
& IN6P_IPV6_V6ONLY
)) {
290 * XXX: this case would happen when the
291 * application sets the V6ONLY flag after
292 * connecting the foreign address.
293 * Such applications should be fixed,
296 log(LOG_INFO
, "udp6_output: IPV6_V6ONLY "
297 "option was set for a connected socket\n");
303 laddr
= &in6p
->in6p_laddr
;
304 faddr
= &in6p
->in6p_faddr
;
305 fport
= in6p
->in6p_fport
;
308 if (in6p
->inp_flowhash
== 0)
309 in6p
->inp_flowhash
= inp_calc_flowhash(in6p
);
310 /* update flowinfo - RFC 6437 */
311 if (in6p
->inp_flow
== 0 && in6p
->in6p_flags
& IN6P_AUTOFLOWLABEL
) {
312 in6p
->inp_flow
&= ~IPV6_FLOWLABEL_MASK
;
314 (htonl(in6p
->inp_flowhash
) & IPV6_FLOWLABEL_MASK
);
318 hlen
= sizeof (struct ip
);
320 if (fport
== htons(53) && !(so
->so_flags1
& SOF1_DNS_COUNTED
)) {
321 so
->so_flags1
|= SOF1_DNS_COUNTED
;
322 INC_ATOMIC_INT64_LIM(net_api_stats
.nas_socket_inet_dgram_dns
);
326 * Calculate data length and get a mbuf
327 * for UDP and IP6 headers.
329 M_PREPEND(m
, hlen
+ sizeof (struct udphdr
), M_DONTWAIT
, 1);
336 * Stuff checksum and output datagram.
338 udp6
= (struct udphdr
*)(void *)(mtod(m
, caddr_t
) + hlen
);
339 udp6
->uh_sport
= in6p
->in6p_lport
; /* lport is always set in the PCB */
340 udp6
->uh_dport
= fport
;
342 udp6
->uh_ulen
= htons((u_short
)plen
);
349 ip6
= mtod(m
, struct ip6_hdr
*);
350 ip6
->ip6_flow
= in6p
->inp_flow
& IPV6_FLOWINFO_MASK
;
351 ip6
->ip6_vfc
&= ~IPV6_VERSION_MASK
;
352 ip6
->ip6_vfc
|= IPV6_VERSION
;
353 #if 0 /* ip6_plen will be filled in ip6_output. */
354 ip6
->ip6_plen
= htons((u_short
)plen
);
356 ip6
->ip6_nxt
= IPPROTO_UDP
;
357 ip6
->ip6_hlim
= in6_selecthlim(in6p
, in6p
->in6p_route
.ro_rt
?
358 in6p
->in6p_route
.ro_rt
->rt_ifp
: NULL
);
359 ip6
->ip6_src
= *laddr
;
360 ip6
->ip6_dst
= *faddr
;
362 udp6
->uh_sum
= in6_pseudo(laddr
, faddr
,
363 htonl(plen
+ IPPROTO_UDP
));
364 m
->m_pkthdr
.csum_flags
= (CSUM_UDPIPV6
|CSUM_ZERO_INVERT
);
365 m
->m_pkthdr
.csum_data
= offsetof(struct udphdr
, uh_sum
);
367 if (!IN6_IS_ADDR_UNSPECIFIED(laddr
))
368 ip6oa
.ip6oa_flags
|= IP6OAF_BOUND_SRCADDR
;
370 flags
= IPV6_OUTARGS
;
372 udp6stat
.udp6s_opackets
++;
376 necp_kernel_policy_id policy_id
;
377 u_int32_t route_rule_id
;
380 * We need a route to perform NECP route rule checks
382 if (net_qos_policy_restricted
!= 0 &&
383 ROUTE_UNUSABLE(&in6p
->inp_route
)) {
384 struct sockaddr_in6 to
;
385 struct sockaddr_in6 from
;
387 ROUTE_RELEASE(&in6p
->inp_route
);
389 bzero(&from
, sizeof(struct sockaddr_in6
));
390 from
.sin6_family
= AF_INET6
;
391 from
.sin6_len
= sizeof(struct sockaddr_in6
);
392 from
.sin6_addr
= *laddr
;
394 bzero(&to
, sizeof(struct sockaddr_in6
));
395 to
.sin6_family
= AF_INET6
;
396 to
.sin6_len
= sizeof(struct sockaddr_in6
);
397 to
.sin6_addr
= *faddr
;
399 in6p
->inp_route
.ro_dst
.sa_family
= AF_INET6
;
400 in6p
->inp_route
.ro_dst
.sa_len
= sizeof(struct sockaddr_in6
);
401 ((struct sockaddr_in6
*)(void *)&in6p
->inp_route
.ro_dst
)->sin6_addr
=
404 rtalloc_scoped(&in6p
->inp_route
, ip6oa
.ip6oa_boundif
);
406 inp_update_necp_policy(in6p
, (struct sockaddr
*)&from
,
407 (struct sockaddr
*)&to
, ip6oa
.ip6oa_boundif
);
408 in6p
->inp_policyresult
.results
.qos_marking_gencount
= 0;
411 if (!necp_socket_is_allowed_to_send_recv_v6(in6p
, in6p
->in6p_lport
, fport
, laddr
, faddr
, NULL
, &policy_id
, &route_rule_id
)) {
412 error
= EHOSTUNREACH
;
416 necp_mark_packet_from_socket(m
, in6p
, policy_id
, route_rule_id
);
418 if (net_qos_policy_restricted
!= 0) {
419 necp_socket_update_qos_marking(in6p
, in6p
->in6p_route
.ro_rt
,
420 NULL
, route_rule_id
);
424 if ((so
->so_flags1
& SOF1_QOSMARKING_ALLOWED
))
425 ip6oa
.ip6oa_flags
|= IP6OAF_QOSMARKING_ALLOWED
;
428 if (in6p
->in6p_sp
!= NULL
&& ipsec_setsocket(m
, so
) != 0) {
434 /* In case of IPv4-mapped address used in previous send */
435 if (ROUTE_UNUSABLE(&in6p
->in6p_route
) ||
436 rt_key(in6p
->in6p_route
.ro_rt
)->sa_family
!= AF_INET6
)
437 ROUTE_RELEASE(&in6p
->in6p_route
);
439 /* Copy the cached route and take an extra reference */
440 in6p_route_copyout(in6p
, &ro
);
442 set_packet_service_class(m
, so
, sotc
, PKT_SCF_IPV6
);
444 m
->m_pkthdr
.pkt_flowsrc
= FLOWSRC_INPCB
;
445 m
->m_pkthdr
.pkt_flowid
= in6p
->inp_flowhash
;
446 m
->m_pkthdr
.pkt_proto
= IPPROTO_UDP
;
447 m
->m_pkthdr
.pkt_flags
|= (PKTF_FLOW_ID
| PKTF_FLOW_LOCALSRC
);
449 m
->m_pkthdr
.pkt_flags
|= PKTF_FLOW_ADV
;
451 im6o
= in6p
->in6p_moptions
;
454 IM6O_ADDREF_LOCKED(im6o
);
455 if (IN6_IS_ADDR_MULTICAST(&ip6
->ip6_dst
) &&
456 im6o
->im6o_multicast_ifp
!= NULL
) {
457 in6p
->in6p_last_outifp
=
458 im6o
->im6o_multicast_ifp
;
463 in6p
->inp_sndinprog_cnt
++;
465 socket_unlock(so
, 0);
466 error
= ip6_output(m
, optp
, &ro
, flags
, im6o
, NULL
, &ip6oa
);
473 if (error
== 0 && nstat_collect
) {
474 boolean_t cell
, wifi
, wired
;
476 if (in6p
->in6p_route
.ro_rt
!= NULL
) {
477 cell
= IFNET_IS_CELLULAR(in6p
->in6p_route
.
479 wifi
= (!cell
&& IFNET_IS_WIFI(in6p
->in6p_route
.
481 wired
= (!wifi
&& IFNET_IS_WIRED(in6p
->in6p_route
.
484 cell
= wifi
= wired
= FALSE
;
486 INP_ADD_STAT(in6p
, cell
, wifi
, wired
, txpackets
, 1);
487 INP_ADD_STAT(in6p
, cell
, wifi
, wired
, txbytes
, ulen
);
488 inp_set_activity_bitmap(in6p
);
491 if (flowadv
&& (adv
->code
== FADV_FLOW_CONTROLLED
||
492 adv
->code
== FADV_SUSPENDED
)) {
494 * Return an error to indicate
495 * that the packet has been dropped.
498 inp_set_fc_state(in6p
, adv
->code
);
501 VERIFY(in6p
->inp_sndinprog_cnt
> 0);
502 if ( --in6p
->inp_sndinprog_cnt
== 0)
503 in6p
->inp_flags
&= ~(INP_FC_FEEDBACK
);
505 if (ro
.ro_rt
!= NULL
) {
506 struct ifnet
*outif
= ro
.ro_rt
->rt_ifp
;
508 so
->so_pktheadroom
= P2ROUNDUP(
509 sizeof(struct udphdr
) +
511 ifnet_hdrlen(outif
) +
512 ifnet_mbuf_packetpreamblelen(outif
),
516 /* Synchronize PCB cached route */
517 in6p_route_copyin(in6p
, &ro
);
519 if (in6p
->in6p_route
.ro_rt
!= NULL
) {
520 struct rtentry
*rt
= in6p
->in6p_route
.ro_rt
;
523 if (rt
->rt_flags
& RTF_MULTICAST
)
524 rt
= NULL
; /* unusable */
527 * Always discard the cached route for unconnected
528 * socket or if it is a multicast route.
531 ROUTE_RELEASE(&in6p
->in6p_route
);
534 * If the destination route is unicast, update outif
535 * with that of the route interface used by IP.
538 (outif
= rt
->rt_ifp
) != in6p
->in6p_last_outifp
) {
539 in6p
->in6p_last_outifp
= outif
;
541 so
->so_pktheadroom
= P2ROUNDUP(
542 sizeof(struct udphdr
) +
544 ifnet_hdrlen(outif
) +
545 ifnet_mbuf_packetpreamblelen(outif
),
549 ROUTE_RELEASE(&in6p
->in6p_route
);
553 * If output interface was cellular/expensive, and this
554 * socket is denied access to it, generate an event.
556 if (error
!= 0 && (ip6oa
.ip6oa_retflags
& IP6OARF_IFDENIED
) &&
557 (INP_NO_CELLULAR(in6p
) || INP_NO_EXPENSIVE(in6p
)))
558 soevent(in6p
->inp_socket
, (SO_FILT_HINT_LOCKED
|
559 SO_FILT_HINT_IFDENIED
));
562 error
= EAFNOSUPPORT
;
572 if (control
!= NULL
) {
574 ip6_clearpktopts(optp
, -1);