]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/udp6_output.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / bsd / netinet6 / udp6_output.c
1 /*
2 * Copyright (c) 2000-2019 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
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 #if CONTENT_FILTER
174 struct m_tag *cfil_tag = NULL;
175 bool cfil_faddr_use = false;
176 bool sndinprog_cnt_used = 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
182 bzero(&ip6oa, sizeof(ip6oa));
183 ip6oa.ip6oa_boundif = IFSCOPE_NONE;
184 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF;
185
186 /* Enable flow advisory only when connected */
187 flowadv = (so->so_state & SS_ISCONNECTED) ? 1 : 0;
188
189 if (flowadv && INP_WAIT_FOR_IF_FEEDBACK(in6p)) {
190 error = ENOBUFS;
191 goto release;
192 }
193
194 if (in6p->inp_flags & INP_BOUND_IF) {
195 ip6oa.ip6oa_boundif = in6p->inp_boundifp->if_index;
196 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
197 }
198 if (INP_NO_CELLULAR(in6p)) {
199 ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
200 }
201 if (INP_NO_EXPENSIVE(in6p)) {
202 ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE;
203 }
204 if (INP_NO_CONSTRAINED(in6p)) {
205 ip6oa.ip6oa_flags |= IP6OAF_NO_CONSTRAINED;
206 }
207 if (INP_AWDL_UNRESTRICTED(in6p)) {
208 ip6oa.ip6oa_flags |= IP6OAF_AWDL_UNRESTRICTED;
209 }
210 if (INP_INTCOPROC_ALLOWED(in6p)) {
211 ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED;
212 }
213
214 #if CONTENT_FILTER
215 /*
216 * If socket is subject to UDP Content Filter and no addr is passed in,
217 * retrieve CFIL saved state from mbuf and use it if necessary.
218 */
219 if (so->so_cfil_db && !addr6) {
220 cfil_tag = cfil_dgram_get_socket_state(m, &cfil_so_state_change_cnt, NULL, &cfil_faddr, NULL);
221 if (cfil_tag) {
222 cfil_sin6 = (struct sockaddr_in6 *)(void *)cfil_faddr;
223 if ((so->so_state_change_cnt != cfil_so_state_change_cnt) &&
224 (in6p->in6p_fport != cfil_sin6->sin6_port ||
225 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &cfil_sin6->sin6_addr))) {
226 /*
227 * Socket is connected but socket state and dest addr/port changed.
228 * We need to use the saved faddr info.
229 */
230 cfil_faddr_use = true;
231 }
232 }
233 }
234 #endif
235
236 if (control) {
237 sotc = so_tc_from_control(control, &netsvctype);
238 if ((error = ip6_setpktopts(control, &opt,
239 NULL, IPPROTO_UDP)) != 0) {
240 goto release;
241 }
242 optp = &opt;
243 } else {
244 optp = in6p->in6p_outputopts;
245 }
246
247 if (sotc == SO_TC_UNSPEC) {
248 sotc = so->so_traffic_class;
249 netsvctype = so->so_netsvctype;
250 }
251 ip6oa.ip6oa_sotc = sotc;
252 ip6oa.ip6oa_netsvctype = netsvctype;
253
254 in6p->inp_sndinprog_cnt++;
255 sndinprog_cnt_used = true;
256
257 if (addr6) {
258 /*
259 * IPv4 version of udp_output calls in_pcbconnect in this case,
260 * which has its costs.
261 *
262 * Since we saw no essential reason for calling in_pcbconnect,
263 * we get rid of such kind of logic, and call in6_selectsrc
264 * and in6_pcbsetport in order to fill in the local address
265 * and the local port.
266 */
267 struct sockaddr_in6 *sin6 =
268 (struct sockaddr_in6 *)(void *)addr6;
269
270 if (sin6->sin6_port == 0) {
271 error = EADDRNOTAVAIL;
272 goto release;
273 }
274
275 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
276 /* how about ::ffff:0.0.0.0 case? */
277 error = EISCONN;
278 goto release;
279 }
280
281 /* protect *sin6 from overwrites */
282 tmp = *sin6;
283 sin6 = &tmp;
284
285 faddr = &sin6->sin6_addr;
286 fport = sin6->sin6_port; /* allow 0 port */
287
288 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
289 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
290 /*
291 * I believe we should explicitly discard the
292 * packet when mapped addresses are disabled,
293 * rather than send the packet as an IPv6 one.
294 * If we chose the latter approach, the packet
295 * might be sent out on the wire based on the
296 * default route, the situation which we'd
297 * probably want to avoid.
298 * (20010421 jinmei@kame.net)
299 */
300 error = EINVAL;
301 goto release;
302 } else {
303 af = AF_INET;
304 }
305 }
306
307 /* KAME hack: embed scopeid */
308 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL,
309 optp) != 0) {
310 error = EINVAL;
311 goto release;
312 }
313
314 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
315 laddr = in6_selectsrc(sin6, optp,
316 in6p, &in6p->in6p_route, NULL, &storage,
317 ip6oa.ip6oa_boundif, &error);
318 } else {
319 laddr = &in6p->in6p_laddr; /* XXX */
320 }
321 if (laddr == NULL) {
322 if (error == 0) {
323 error = EADDRNOTAVAIL;
324 }
325 goto release;
326 }
327 if (in6p->in6p_lport == 0 &&
328 (error = in6_pcbsetport(laddr, in6p, p, 0)) != 0) {
329 goto release;
330 }
331 } else {
332 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
333 error = ENOTCONN;
334 goto release;
335 }
336 laddr = &in6p->in6p_laddr;
337 faddr = &in6p->in6p_faddr;
338 fport = in6p->in6p_fport;
339 #if CONTENT_FILTER
340 if (cfil_faddr_use) {
341 faddr = &((struct sockaddr_in6 *)(void *)cfil_faddr)->sin6_addr;
342 fport = ((struct sockaddr_in6 *)(void *)cfil_faddr)->sin6_port;
343
344 /* Do not use cached route */
345 ROUTE_RELEASE(&in6p->in6p_route);
346 }
347 #endif
348 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
349 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
350 /*
351 * XXX: this case would happen when the
352 * application sets the V6ONLY flag after
353 * connecting the foreign address.
354 * Such applications should be fixed,
355 * so we bark here.
356 */
357 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
358 "option was set for a connected socket\n");
359 error = EINVAL;
360 goto release;
361 } else {
362 af = AF_INET;
363 }
364 }
365 }
366
367 if (in6p->inp_flowhash == 0) {
368 in6p->inp_flowhash = inp_calc_flowhash(in6p);
369 }
370 /* update flowinfo - RFC 6437 */
371 if (in6p->inp_flow == 0 && in6p->in6p_flags & IN6P_AUTOFLOWLABEL) {
372 in6p->inp_flow &= ~IPV6_FLOWLABEL_MASK;
373 in6p->inp_flow |=
374 (htonl(in6p->inp_flowhash) & IPV6_FLOWLABEL_MASK);
375 }
376
377 if (af == AF_INET) {
378 hlen = sizeof(struct ip);
379 }
380
381 if (fport == htons(53) && !(so->so_flags1 & SOF1_DNS_COUNTED)) {
382 so->so_flags1 |= SOF1_DNS_COUNTED;
383 INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_dns);
384 }
385
386 /*
387 * Calculate data length and get a mbuf
388 * for UDP and IP6 headers.
389 */
390 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT, 1);
391 if (m == 0) {
392 error = ENOBUFS;
393 goto release;
394 }
395
396 /*
397 * Stuff checksum and output datagram.
398 */
399 udp6 = (struct udphdr *)(void *)(mtod(m, caddr_t) + hlen);
400 udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
401 udp6->uh_dport = fport;
402 if (plen <= 0xffff) {
403 udp6->uh_ulen = htons((u_short)plen);
404 } else {
405 udp6->uh_ulen = 0;
406 }
407 udp6->uh_sum = 0;
408
409 switch (af) {
410 case AF_INET6:
411 ip6 = mtod(m, struct ip6_hdr *);
412 ip6->ip6_flow = in6p->inp_flow & IPV6_FLOWINFO_MASK;
413 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
414 ip6->ip6_vfc |= IPV6_VERSION;
415 #if 0 /* ip6_plen will be filled in ip6_output. */
416 ip6->ip6_plen = htons((u_short)plen);
417 #endif
418 ip6->ip6_nxt = IPPROTO_UDP;
419 ip6->ip6_hlim = in6_selecthlim(in6p, in6p->in6p_route.ro_rt ?
420 in6p->in6p_route.ro_rt->rt_ifp : NULL);
421 ip6->ip6_src = *laddr;
422 ip6->ip6_dst = *faddr;
423
424 udp6->uh_sum = in6_pseudo(laddr, faddr,
425 htonl(plen + IPPROTO_UDP));
426 m->m_pkthdr.csum_flags = (CSUM_UDPIPV6 | CSUM_ZERO_INVERT);
427 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
428
429 if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) {
430 ip6oa.ip6oa_flags |= IP6OAF_BOUND_SRCADDR;
431 }
432
433 flags = IPV6_OUTARGS;
434
435 udp6stat.udp6s_opackets++;
436
437 #if NECP
438 {
439 necp_kernel_policy_id policy_id;
440 necp_kernel_policy_id skip_policy_id;
441 u_int32_t route_rule_id;
442
443 /*
444 * We need a route to perform NECP route rule checks
445 */
446 if (net_qos_policy_restricted != 0 &&
447 ROUTE_UNUSABLE(&in6p->inp_route)) {
448 struct sockaddr_in6 to;
449 struct sockaddr_in6 from;
450
451 ROUTE_RELEASE(&in6p->inp_route);
452
453 bzero(&from, sizeof(struct sockaddr_in6));
454 from.sin6_family = AF_INET6;
455 from.sin6_len = sizeof(struct sockaddr_in6);
456 from.sin6_addr = *laddr;
457
458 bzero(&to, sizeof(struct sockaddr_in6));
459 to.sin6_family = AF_INET6;
460 to.sin6_len = sizeof(struct sockaddr_in6);
461 to.sin6_addr = *faddr;
462
463 in6p->inp_route.ro_dst.sa_family = AF_INET6;
464 in6p->inp_route.ro_dst.sa_len = sizeof(struct sockaddr_in6);
465 ((struct sockaddr_in6 *)(void *)&in6p->inp_route.ro_dst)->sin6_addr =
466 *faddr;
467
468 rtalloc_scoped(&in6p->inp_route, ip6oa.ip6oa_boundif);
469
470 inp_update_necp_policy(in6p, (struct sockaddr *)&from,
471 (struct sockaddr *)&to, ip6oa.ip6oa_boundif);
472 in6p->inp_policyresult.results.qos_marking_gencount = 0;
473 }
474
475 if (!necp_socket_is_allowed_to_send_recv_v6(in6p, in6p->in6p_lport, fport, laddr, faddr, NULL, &policy_id, &route_rule_id, &skip_policy_id)) {
476 error = EHOSTUNREACH;
477 goto release;
478 }
479
480 necp_mark_packet_from_socket(m, in6p, policy_id, route_rule_id, skip_policy_id);
481
482 if (net_qos_policy_restricted != 0) {
483 necp_socket_update_qos_marking(in6p, in6p->in6p_route.ro_rt,
484 NULL, route_rule_id);
485 }
486 }
487 #endif /* NECP */
488 if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) {
489 ip6oa.ip6oa_flags |= IP6OAF_QOSMARKING_ALLOWED;
490 }
491
492 #if IPSEC
493 if (in6p->in6p_sp != NULL && ipsec_setsocket(m, so) != 0) {
494 error = ENOBUFS;
495 goto release;
496 }
497 #endif /*IPSEC*/
498
499 /* In case of IPv4-mapped address used in previous send */
500 if (ROUTE_UNUSABLE(&in6p->in6p_route) ||
501 rt_key(in6p->in6p_route.ro_rt)->sa_family != AF_INET6) {
502 ROUTE_RELEASE(&in6p->in6p_route);
503 }
504
505 /* Copy the cached route and take an extra reference */
506 in6p_route_copyout(in6p, &ro);
507
508 set_packet_service_class(m, so, sotc, PKT_SCF_IPV6);
509
510 m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
511 m->m_pkthdr.pkt_flowid = in6p->inp_flowhash;
512 m->m_pkthdr.pkt_proto = IPPROTO_UDP;
513 m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC);
514 if (flowadv) {
515 m->m_pkthdr.pkt_flags |= PKTF_FLOW_ADV;
516 }
517 m->m_pkthdr.tx_udp_pid = so->last_pid;
518 if (so->so_flags & SOF_DELEGATED) {
519 m->m_pkthdr.tx_udp_e_pid = so->e_pid;
520 } else {
521 m->m_pkthdr.tx_udp_e_pid = 0;
522 }
523
524 im6o = in6p->in6p_moptions;
525 if (im6o != NULL) {
526 IM6O_LOCK(im6o);
527 IM6O_ADDREF_LOCKED(im6o);
528 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
529 im6o->im6o_multicast_ifp != NULL) {
530 in6p->in6p_last_outifp =
531 im6o->im6o_multicast_ifp;
532 }
533 IM6O_UNLOCK(im6o);
534 }
535
536 socket_unlock(so, 0);
537 error = ip6_output(m, optp, &ro, flags, im6o, NULL, &ip6oa);
538 m = NULL;
539 socket_lock(so, 0);
540
541 if (im6o != NULL) {
542 IM6O_REMREF(im6o);
543 }
544
545 if (error == 0 && nstat_collect) {
546 boolean_t cell, wifi, wired;
547
548 if (in6p->in6p_route.ro_rt != NULL) {
549 cell = IFNET_IS_CELLULAR(in6p->in6p_route.
550 ro_rt->rt_ifp);
551 wifi = (!cell && IFNET_IS_WIFI(in6p->in6p_route.
552 ro_rt->rt_ifp));
553 wired = (!wifi && IFNET_IS_WIRED(in6p->in6p_route.
554 ro_rt->rt_ifp));
555 } else {
556 cell = wifi = wired = FALSE;
557 }
558 INP_ADD_STAT(in6p, cell, wifi, wired, txpackets, 1);
559 INP_ADD_STAT(in6p, cell, wifi, wired, txbytes, ulen);
560 inp_set_activity_bitmap(in6p);
561 }
562
563 if (flowadv && (adv->code == FADV_FLOW_CONTROLLED ||
564 adv->code == FADV_SUSPENDED)) {
565 /*
566 * Return an error to indicate
567 * that the packet has been dropped.
568 */
569 error = ENOBUFS;
570 inp_set_fc_state(in6p, adv->code);
571 }
572
573 if (ro.ro_rt != NULL) {
574 struct ifnet *outif = ro.ro_rt->rt_ifp;
575
576 so->so_pktheadroom = P2ROUNDUP(
577 sizeof(struct udphdr) +
578 hlen +
579 ifnet_hdrlen(outif) +
580 ifnet_mbuf_packetpreamblelen(outif),
581 sizeof(u_int32_t));
582 }
583
584 /* Synchronize PCB cached route */
585 in6p_route_copyin(in6p, &ro);
586
587 if (in6p->in6p_route.ro_rt != NULL) {
588 struct rtentry *rt = in6p->in6p_route.ro_rt;
589 struct ifnet *outif;
590
591 if (rt->rt_flags & RTF_MULTICAST) {
592 rt = NULL; /* unusable */
593 }
594 #if CONTENT_FILTER
595 /*
596 * Discard temporary route for cfil case
597 */
598 if (cfil_faddr_use) {
599 rt = NULL; /* unusable */
600 }
601 #endif
602
603 /*
604 * Always discard the cached route for unconnected
605 * socket or if it is a multicast route.
606 */
607 if (rt == NULL) {
608 ROUTE_RELEASE(&in6p->in6p_route);
609 }
610
611 /*
612 * If the destination route is unicast, update outif
613 * with that of the route interface used by IP.
614 */
615 if (rt != NULL) {
616 /*
617 * When an NECP IP tunnel policy forces the outbound interface,
618 * ip6_output_list() informs the transport layer what is the actual
619 * outgoing interface
620 */
621 if (ip6oa.ip6oa_flags & IP6OAF_BOUND_IF) {
622 outif = ifindex2ifnet[ip6oa.ip6oa_boundif];
623 } else {
624 outif = rt->rt_ifp;
625 }
626 if (outif != NULL && outif != in6p->in6p_last_outifp) {
627 in6p->in6p_last_outifp = outif;
628
629 so->so_pktheadroom = P2ROUNDUP(
630 sizeof(struct udphdr) +
631 hlen +
632 ifnet_hdrlen(outif) +
633 ifnet_mbuf_packetpreamblelen(outif),
634 sizeof(u_int32_t));
635 }
636 }
637 } else {
638 ROUTE_RELEASE(&in6p->in6p_route);
639 }
640
641 /*
642 * If output interface was cellular/expensive, and this
643 * socket is denied access to it, generate an event.
644 */
645 if (error != 0 && (ip6oa.ip6oa_retflags & IP6OARF_IFDENIED) &&
646 (INP_NO_CELLULAR(in6p) || INP_NO_EXPENSIVE(in6p) || INP_NO_CONSTRAINED(in6p))) {
647 soevent(in6p->inp_socket, (SO_FILT_HINT_LOCKED |
648 SO_FILT_HINT_IFDENIED));
649 }
650 break;
651 case AF_INET:
652 error = EAFNOSUPPORT;
653 goto release;
654 }
655 goto releaseopt;
656
657 release:
658
659 if (m != NULL) {
660 m_freem(m);
661 }
662
663 releaseopt:
664 if (control != NULL) {
665 if (optp == &opt) {
666 ip6_clearpktopts(optp, -1);
667 }
668 m_freem(control);
669 }
670 #if CONTENT_FILTER
671 if (cfil_tag) {
672 m_tag_free(cfil_tag);
673 }
674 #endif
675 if (sndinprog_cnt_used) {
676 VERIFY(in6p->inp_sndinprog_cnt > 0);
677 if (--in6p->inp_sndinprog_cnt == 0) {
678 in6p->inp_flags &= ~(INP_FC_FEEDBACK);
679 if (in6p->inp_sndingprog_waiters > 0) {
680 wakeup(&in6p->inp_sndinprog_cnt);
681 }
682 }
683 sndinprog_cnt_used = false;
684 }
685
686 return error;
687 }