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