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