1 /*
2 * Copyright (c) 2000-2021 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29
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 $ */
32
33 /*
34 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
35 * 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. 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.
48 *
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
59 * SUCH DAMAGE.
60 */
61
62 /*
63 * Copyright (c) 1982, 1986, 1989, 1993
64 * The Regents of the University of California. All rights reserved.
65 *
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
68 * are met:
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.
81 *
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
92 * SUCH DAMAGE.
93 *
94 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
95 */
96
97 #include <sys/param.h>
98 #include <sys/malloc.h>
99 #include <sys/mbuf.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>
109
110 #include <machine/endian.h>
111
112 #include <net/if.h>
113 #include <net/route.h>
114 #include <net/if_types.h>
115 #include <net/ntstat.h>
116
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>
132
133 #if NECP
134 #include <net/necp.h>
135 #endif /* NECP */
136
137 #include <net/net_osdep.h>
138
139 #if CONTENT_FILTER
140 #include <net/content_filter.h>
141 #endif /* CONTENT_FILTER */
142
143 /*
144 * UDP protocol inplementation.
145 * Per RFC 768, August, 1980.
146 */
147 extern int soreserveheadroom;
148
149 int
udp6_output(struct in6pcb * in6p,struct mbuf * m,struct sockaddr * addr6,struct mbuf * control,struct proc * p)150 udp6_output(struct in6pcb *in6p, struct mbuf *m, struct sockaddr *addr6,
151 struct mbuf *control, struct proc *p)
152 {
153 u_int32_t ulen = m->m_pkthdr.len;
154 u_int32_t plen = sizeof(struct udphdr) + ulen;
155 struct ip6_hdr *ip6;
156 struct udphdr *udp6;
157 struct in6_addr *laddr, *faddr;
158 u_short fport;
159 int error = 0;
160 struct ip6_pktopts opt, *optp = NULL;
161 struct ip6_moptions *im6o;
162 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
163 int flags;
164 struct sockaddr_in6 tmp;
165 struct in6_addr storage;
166 int sotc = SO_TC_UNSPEC;
167 int netsvctype = _NET_SERVICE_TYPE_UNSPEC;
168 struct ip6_out_args ip6oa;
169 struct flowadv *adv = &ip6oa.ip6oa_flowadv;
170 struct socket *so = in6p->in6p_socket;
171 struct route_in6 ro;
172 int flowadv = 0;
173 bool sndinprog_cnt_used = false;
174 #if CONTENT_FILTER
175 struct m_tag *cfil_tag = NULL;
176 bool cfil_faddr_use = false;
177 uint32_t cfil_so_state_change_cnt = 0;
178 struct sockaddr *cfil_faddr = NULL;
179 struct sockaddr_in6 *cfil_sin6 = NULL;
180 #endif
181 bool check_qos_marking_again = (so->so_flags1 & SOF1_QOSMARKING_POLICY_OVERRIDE) ? FALSE : TRUE;
182 uint32_t lifscope = IFSCOPE_NONE, fifscope = IFSCOPE_NONE;
183
184 bzero(&ip6oa, sizeof(ip6oa));
185 ip6oa.ip6oa_boundif = IFSCOPE_NONE;
186 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF;
187
188 /* Enable flow advisory only when connected */
189 flowadv = (so->so_state & SS_ISCONNECTED) ? 1 : 0;
190
191 if (flowadv && INP_WAIT_FOR_IF_FEEDBACK(in6p)) {
192 error = ENOBUFS;
193 goto release;
194 }
195
196 if (in6p->inp_flags & INP_BOUND_IF) {
197 ip6oa.ip6oa_boundif = in6p->inp_boundifp->if_index;
198 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
199 } else if (!in6_embedded_scope && IN6_IS_SCOPE_EMBED(&in6p->in6p_faddr)) {
200 ip6oa.ip6oa_boundif = in6p->inp_fifscope;
201 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
202 }
203 if (INP_NO_CELLULAR(in6p)) {
204 ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
205 }
206 if (INP_NO_EXPENSIVE(in6p)) {
207 ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE;
208 }
209 if (INP_NO_CONSTRAINED(in6p)) {
210 ip6oa.ip6oa_flags |= IP6OAF_NO_CONSTRAINED;
211 }
212 if (INP_AWDL_UNRESTRICTED(in6p)) {
213 ip6oa.ip6oa_flags |= IP6OAF_AWDL_UNRESTRICTED;
214 }
215 if (INP_INTCOPROC_ALLOWED(in6p)) {
216 ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED;
217 }
218
219 #if CONTENT_FILTER
220 /*
221 * If socket is subject to UDP Content Filter and no addr is passed in,
222 * retrieve CFIL saved state from mbuf and use it if necessary.
223 */
224 if (CFIL_DGRAM_FILTERED(so) && !addr6) {
225 cfil_tag = cfil_dgram_get_socket_state(m, &cfil_so_state_change_cnt, NULL, &cfil_faddr, NULL);
226 if (cfil_tag) {
227 cfil_sin6 = (struct sockaddr_in6 *)(void *)cfil_faddr;
228 if ((so->so_state_change_cnt != cfil_so_state_change_cnt) &&
229 (in6p->in6p_fport != cfil_sin6->sin6_port ||
230 !in6_are_addr_equal_scoped(&in6p->in6p_faddr, &cfil_sin6->sin6_addr, in6p->inp_fifscope, cfil_sin6->sin6_scope_id))) {
231 /*
232 * Socket is connected but socket state and dest addr/port changed.
233 * We need to use the saved faddr info.
234 */
235 cfil_faddr_use = true;
236 }
237 }
238 }
239 #endif
240
241 if (control) {
242 sotc = so_tc_from_control(control, &netsvctype);
243 if ((error = ip6_setpktopts(control, &opt,
244 NULL, IPPROTO_UDP)) != 0) {
245 goto release;
246 }
247 optp = &opt;
248 } else {
249 optp = in6p->in6p_outputopts;
250 }
251
252 if (sotc == SO_TC_UNSPEC) {
253 sotc = so->so_traffic_class;
254 netsvctype = so->so_netsvctype;
255 }
256 ip6oa.ip6oa_sotc = sotc;
257 ip6oa.ip6oa_netsvctype = netsvctype;
258
259 in6p->inp_sndinprog_cnt++;
260 sndinprog_cnt_used = true;
261
262 if (addr6) {
263 /*
264 * IPv4 version of udp_output calls in_pcbconnect in this case,
265 * which has its costs.
266 *
267 * Since we saw no essential reason for calling in_pcbconnect,
268 * we get rid of such kind of logic, and call in6_selectsrc
269 * and in6_pcbsetport in order to fill in the local address
270 * and the local port.
271 */
272 struct sockaddr_in6 *sin6 =
273 (struct sockaddr_in6 *)(void *)addr6;
274
275 if (sin6->sin6_port == 0) {
276 error = EADDRNOTAVAIL;
277 goto release;
278 }
279
280 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
281 /* how about ::ffff:0.0.0.0 case? */
282 error = EISCONN;
283 goto release;
284 }
285
286 /* protect *sin6 from overwrites */
287 tmp = *sin6;
288 sin6 = &tmp;
289
290 faddr = &sin6->sin6_addr;
291 fport = sin6->sin6_port; /* allow 0 port */
292
293 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
294 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
295 /*
296 * I believe we should explicitly discard the
297 * packet when mapped addresses are disabled,
298 * rather than send the packet as an IPv6 one.
299 * If we chose the latter approach, the packet
300 * might be sent out on the wire based on the
301 * default route, the situation which we'd
302 * probably want to avoid.
303 * (20010421 [email protected])
304 */
305 error = EINVAL;
306 goto release;
307 } else {
308 af = AF_INET;
309 }
310 }
311
312 /* KAME hack: embed scopeid */
313 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL,
314 optp, IN6_NULL_IF_EMBEDDED_SCOPE(&sin6->sin6_scope_id)) != 0) {
315 error = EINVAL;
316 goto release;
317 }
318 fifscope = sin6->sin6_scope_id;
319
320 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
321 struct ifnet *src_ifp = NULL;
322 laddr = in6_selectsrc(sin6, optp,
323 in6p, &in6p->in6p_route, &src_ifp, &storage,
324 ip6oa.ip6oa_boundif, &error);
325 if (src_ifp != NULL) {
326 lifscope = src_ifp->if_index;
327 ifnet_release(src_ifp);
328 }
329 } else {
330 laddr = &in6p->in6p_laddr; /* XXX */
331 lifscope = in6p->inp_lifscope;
332 }
333 if (laddr == NULL) {
334 if (error == 0) {
335 error = EADDRNOTAVAIL;
336 }
337 goto release;
338 }
339 if (in6p->in6p_lport == 0 &&
340 (error = in6_pcbsetport(laddr, in6p, p, 0)) != 0) {
341 goto release;
342 }
343 } else {
344 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
345 error = ENOTCONN;
346 goto release;
347 }
348 laddr = &in6p->in6p_laddr;
349 faddr = &in6p->in6p_faddr;
350 fport = in6p->in6p_fport;
351 fifscope = in6p->inp_fifscope;
352 lifscope = in6p->inp_lifscope;
353 #if CONTENT_FILTER
354 if (cfil_faddr_use) {
355 faddr = &((struct sockaddr_in6 *)(void *)cfil_faddr)->sin6_addr;
356 fport = ((struct sockaddr_in6 *)(void *)cfil_faddr)->sin6_port;
357 fifscope = ((struct sockaddr_in6 *)(void *)cfil_faddr)->sin6_scope_id;
358
359 /* Do not use cached route */
360 ROUTE_RELEASE(&in6p->in6p_route);
361 }
362 #endif
363 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
364 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
365 /*
366 * XXX: this case would happen when the
367 * application sets the V6ONLY flag after
368 * connecting the foreign address.
369 * Such applications should be fixed,
370 * so we bark here.
371 */
372 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
373 "option was set for a connected socket\n");
374 error = EINVAL;
375 goto release;
376 } else {
377 af = AF_INET;
378 }
379 }
380 }
381
382 if (in6p->inp_flowhash == 0) {
383 in6p->inp_flowhash = inp_calc_flowhash(in6p);
384 }
385 /* update flowinfo - RFC 6437 */
386 if (in6p->inp_flow == 0 && in6p->in6p_flags & IN6P_AUTOFLOWLABEL) {
387 in6p->inp_flow &= ~IPV6_FLOWLABEL_MASK;
388 in6p->inp_flow |=
389 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
390 }
391
392 if (af == AF_INET) {
393 hlen = sizeof(struct ip);
394 }
395
396 if (fport == htons(53) && !(so->so_flags1 & SOF1_DNS_COUNTED)) {
397 so->so_flags1 |= SOF1_DNS_COUNTED;
398 INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_dns);
399 }
400
401 /*
402 * Calculate data length and get a mbuf
403 * for UDP and IP6 headers.
404 */
405 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT, 1);
406 if (m == 0) {
407 error = ENOBUFS;
408 goto release;
409 }
410
411 /*
412 * Stuff checksum and output datagram.
413 */
414 udp6 = (struct udphdr *)(void *)(mtod(m, caddr_t) + hlen);
415 udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
416 udp6->uh_dport = fport;
417 if (plen <= 0xffff) {
418 udp6->uh_ulen = htons((u_short)plen);
419 } else {
420 udp6->uh_ulen = 0;
421 }
422 udp6->uh_sum = 0;
423
424 switch (af) {
425 case AF_INET6:
426 ip6 = mtod(m, struct ip6_hdr *);
427 ip6->ip6_flow = in6p->inp_flow & IPV6_FLOWINFO_MASK;
428 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
429 ip6->ip6_vfc |= IPV6_VERSION;
430 #if 0 /* ip6_plen will be filled in ip6_output. */
431 ip6->ip6_plen = htons((u_short)plen);
432 #endif
433 ip6->ip6_nxt = IPPROTO_UDP;
434 ip6->ip6_hlim = in6_selecthlim(in6p, in6p->in6p_route.ro_rt ?
435 in6p->in6p_route.ro_rt->rt_ifp : NULL);
436 ip6->ip6_src = *laddr;
437 ip6->ip6_dst = *faddr;
438
439 udp6->uh_sum = in6_pseudo(laddr, faddr,
440 htonl(plen + IPPROTO_UDP));
441 m->m_pkthdr.csum_flags = (CSUM_UDPIPV6 | CSUM_ZERO_INVERT);
442 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
443
444 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) {
445 ip6oa.ip6oa_flags |= IP6OAF_BOUND_SRCADDR;
446 }
447
448 flags = IPV6_OUTARGS;
449
450 udp6stat.udp6s_opackets++;
451
452 #if NECP
453 {
454 necp_kernel_policy_id policy_id;
455 necp_kernel_policy_id skip_policy_id;
456 u_int32_t route_rule_id;
457 u_int32_t pass_flags;
458
459 /*
460 * We need a route to perform NECP route rule checks
461 */
462 if (net_qos_policy_restricted != 0 &&
463 ROUTE_UNUSABLE(&in6p->inp_route)) {
464 struct sockaddr_in6 to;
465 struct sockaddr_in6 from;
466
467 ROUTE_RELEASE(&in6p->inp_route);
468
469 bzero(&from, sizeof(struct sockaddr_in6));
470 from.sin6_family = AF_INET6;
471 from.sin6_len = sizeof(struct sockaddr_in6);
472 from.sin6_addr = *laddr;
473
474 bzero(&to, sizeof(struct sockaddr_in6));
475 to.sin6_family = AF_INET6;
476 to.sin6_len = sizeof(struct sockaddr_in6);
477 to.sin6_addr = *faddr;
478
479 in6p->inp_route.ro_dst.sa_family = AF_INET6;
480 in6p->inp_route.ro_dst.sa_len = sizeof(struct sockaddr_in6);
481 ((struct sockaddr_in6 *)(void *)&in6p->inp_route.ro_dst)->sin6_addr =
482 *faddr;
483
484 if (!in6_embedded_scope) {
485 ((struct sockaddr_in6 *)(void *)&in6p->inp_route.ro_dst)->sin6_scope_id =
486 IN6_IS_SCOPE_EMBED(faddr) ? fifscope : IFSCOPE_NONE;
487 }
488 rtalloc_scoped(&in6p->inp_route, ip6oa.ip6oa_boundif);
489
490 inp_update_necp_policy(in6p, (struct sockaddr *)&from,
491 (struct sockaddr *)&to, ip6oa.ip6oa_boundif);
492 in6p->inp_policyresult.results.qos_marking_gencount = 0;
493 }
494
495 if (!necp_socket_is_allowed_to_send_recv_v6(in6p, in6p->in6p_lport, fport, laddr, faddr, NULL, 0, &policy_id, &route_rule_id, &skip_policy_id, &pass_flags)) {
496 error = EHOSTUNREACH;
497 goto release;
498 }
499
500 necp_mark_packet_from_socket(m, in6p, policy_id, route_rule_id, skip_policy_id, pass_flags);
501
502 if (net_qos_policy_restricted != 0) {
503 necp_socket_update_qos_marking(in6p, in6p->in6p_route.ro_rt, route_rule_id);
504 }
505 }
506 #endif /* NECP */
507 if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) {
508 ip6oa.ip6oa_flags |= IP6OAF_QOSMARKING_ALLOWED;
509 }
510 if (check_qos_marking_again) {
511 ip6oa.ip6oa_flags |= IP6OAF_REDO_QOSMARKING_POLICY;
512 }
513 ip6oa.qos_marking_gencount = in6p->inp_policyresult.results.qos_marking_gencount;
514
515 #if IPSEC
516 if (in6p->in6p_sp != NULL && ipsec_setsocket(m, so) != 0) {
517 error = ENOBUFS;
518 goto release;
519 }
520 #endif /*IPSEC*/
521
522 /* In case of IPv4-mapped address used in previous send */
523 if (ROUTE_UNUSABLE(&in6p->in6p_route) ||
524 rt_key(in6p->in6p_route.ro_rt)->sa_family != AF_INET6) {
525 ROUTE_RELEASE(&in6p->in6p_route);
526 }
527
528 /* Copy the cached route and take an extra reference */
529 in6p_route_copyout(in6p, &ro);
530
531 set_packet_service_class(m, so, sotc, PKT_SCF_IPV6);
532
533 m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
534 m->m_pkthdr.pkt_flowid = in6p->inp_flowhash;
535 m->m_pkthdr.pkt_proto = IPPROTO_UDP;
536 m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC);
537 if (flowadv) {
538 m->m_pkthdr.pkt_flags |= PKTF_FLOW_ADV;
539 }
540 m->m_pkthdr.tx_udp_pid = so->last_pid;
541 if (so->so_flags & SOF_DELEGATED) {
542 m->m_pkthdr.tx_udp_e_pid = so->e_pid;
543 } else {
544 m->m_pkthdr.tx_udp_e_pid = 0;
545 }
546 #if (DEBUG || DEVELOPMENT)
547 if (so->so_flags & SOF_MARK_WAKE_PKT) {
548 so->so_flags &= ~SOF_MARK_WAKE_PKT;
549 m->m_pkthdr.pkt_flags |= PKTF_WAKE_PKT;
550 }
551 #endif /* (DEBUG || DEVELOPMENT) */
552
553 im6o = in6p->in6p_moptions;
554 if (im6o != NULL) {
555 IM6O_LOCK(im6o);
556 IM6O_ADDREF_LOCKED(im6o);
557 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
558 im6o->im6o_multicast_ifp != NULL) {
559 in6p->in6p_last_outifp =
560 im6o->im6o_multicast_ifp;
561 #if SKYWALK
562 if (NETNS_TOKEN_VALID(&in6p->inp_netns_token)) {
563 netns_set_ifnet(&in6p->inp_netns_token,
564 in6p->in6p_last_outifp);
565 }
566 #endif /* SKYWALK */
567 }
568 IM6O_UNLOCK(im6o);
569 }
570
571 ip6_output_setdstifscope(m, fifscope, NULL);
572 ip6_output_setsrcifscope(m, lifscope, NULL);
573
574 socket_unlock(so, 0);
575 error = ip6_output(m, optp, &ro, flags, im6o, NULL, &ip6oa);
576 m = NULL;
577 socket_lock(so, 0);
578
579 if (im6o != NULL) {
580 IM6O_REMREF(im6o);
581 }
582
583 if (check_qos_marking_again) {
584 in6p->inp_policyresult.results.qos_marking_gencount = ip6oa.qos_marking_gencount;
585 if (ip6oa.ip6oa_flags & IP6OAF_QOSMARKING_ALLOWED) {
586 in6p->inp_socket->so_flags1 |= SOF1_QOSMARKING_ALLOWED;
587 } else {
588 in6p->inp_socket->so_flags1 &= ~SOF1_QOSMARKING_ALLOWED;
589 }
590 }
591
592 if (error == 0 && nstat_collect) {
593 boolean_t cell, wifi, wired;
594
595 if (in6p->in6p_route.ro_rt != NULL) {
596 cell = IFNET_IS_CELLULAR(in6p->in6p_route.
597 ro_rt->rt_ifp);
598 wifi = (!cell && IFNET_IS_WIFI(in6p->in6p_route.
599 ro_rt->rt_ifp));
600 wired = (!wifi && IFNET_IS_WIRED(in6p->in6p_route.
601 ro_rt->rt_ifp));
602 } else {
603 cell = wifi = wired = FALSE;
604 }
605 INP_ADD_STAT(in6p, cell, wifi, wired, txpackets, 1);
606 INP_ADD_STAT(in6p, cell, wifi, wired, txbytes, ulen);
607 inp_set_activity_bitmap(in6p);
608 }
609
610 if (flowadv && (adv->code == FADV_FLOW_CONTROLLED ||
611 adv->code == FADV_SUSPENDED)) {
612 /*
613 * Return an error to indicate
614 * that the packet has been dropped.
615 */
616 error = ENOBUFS;
617 inp_set_fc_state(in6p, adv->code);
618 }
619
620 if (ro.ro_rt != NULL) {
621 struct ifnet *outif = ro.ro_rt->rt_ifp;
622
623 so->so_pktheadroom = (uint16_t)P2ROUNDUP(
624 sizeof(struct udphdr) +
625 hlen +
626 ifnet_hdrlen(outif) +
627 ifnet_mbuf_packetpreamblelen(outif),
628 sizeof(u_int32_t));
629 }
630
631 /* Synchronize PCB cached route */
632 in6p_route_copyin(in6p, &ro);
633
634 if (in6p->in6p_route.ro_rt != NULL) {
635 struct rtentry *rt = in6p->in6p_route.ro_rt;
636 struct ifnet *outif;
637
638 if (rt->rt_flags & RTF_MULTICAST) {
639 rt = NULL; /* unusable */
640 }
641 #if CONTENT_FILTER
642 /*
643 * Discard temporary route for cfil case
644 */
645 if (cfil_faddr_use) {
646 rt = NULL; /* unusable */
647 }
648 #endif
649
650 /*
651 * Always discard the cached route for unconnected
652 * socket or if it is a multicast route.
653 */
654 if (rt == NULL) {
655 ROUTE_RELEASE(&in6p->in6p_route);
656 }
657
658 /*
659 * If the destination route is unicast, update outif
660 * with that of the route interface used by IP.
661 */
662 if (rt != NULL) {
663 /*
664 * When an NECP IP tunnel policy forces the outbound interface,
665 * ip6_output_list() informs the transport layer what is the actual
666 * outgoing interface
667 */
668 if (ip6oa.ip6oa_flags & IP6OAF_BOUND_IF) {
669 outif = ifindex2ifnet[ip6oa.ip6oa_boundif];
670 } else {
671 outif = rt->rt_ifp;
672 }
673 if (outif != NULL && outif != in6p->in6p_last_outifp) {
674 in6p->in6p_last_outifp = outif;
675 #if SKYWALK
676 if (NETNS_TOKEN_VALID(&in6p->inp_netns_token)) {
677 netns_set_ifnet(&in6p->inp_netns_token,
678 in6p->in6p_last_outifp);
679 }
680 #endif /* SKYWALK */
681
682 so->so_pktheadroom = (uint16_t)P2ROUNDUP(
683 sizeof(struct udphdr) +
684 hlen +
685 ifnet_hdrlen(outif) +
686 ifnet_mbuf_packetpreamblelen(outif),
687 sizeof(u_int32_t));
688 }
689 }
690 } else {
691 ROUTE_RELEASE(&in6p->in6p_route);
692 }
693
694 /*
695 * If output interface was cellular/expensive, and this
696 * socket is denied access to it, generate an event.
697 */
698 if (error != 0 && (ip6oa.ip6oa_flags & IP6OAF_R_IFDENIED) &&
699 (INP_NO_CELLULAR(in6p) || INP_NO_EXPENSIVE(in6p) || INP_NO_CONSTRAINED(in6p))) {
700 soevent(in6p->inp_socket, (SO_FILT_HINT_LOCKED |
701 SO_FILT_HINT_IFDENIED));
702 }
703 break;
704 case AF_INET:
705 error = EAFNOSUPPORT;
706 goto release;
707 }
708 goto releaseopt;
709
710 release:
711
712 if (m != NULL) {
713 m_freem(m);
714 }
715
716 releaseopt:
717 if (control != NULL) {
718 if (optp == &opt) {
719 ip6_clearpktopts(optp, -1);
720 }
721 m_freem(control);
722 }
723 #if CONTENT_FILTER
724 if (cfil_tag) {
725 m_tag_free(cfil_tag);
726 }
727 #endif
728 if (sndinprog_cnt_used) {
729 VERIFY(in6p->inp_sndinprog_cnt > 0);
730 if (--in6p->inp_sndinprog_cnt == 0) {
731 in6p->inp_flags &= ~(INP_FC_FEEDBACK);
732 if (in6p->inp_sndingprog_waiters > 0) {
733 wakeup(&in6p->inp_sndinprog_cnt);
734 }
735 }
736 sndinprog_cnt_used = false;
737 }
738
739 return error;
740 }
741