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