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