]>
Commit | Line | Data |
---|---|---|
b0d623f7 | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2000-2011 Apple Inc. All rights reserved. |
b0d623f7 A |
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 | ||
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> | |
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 | #ifdef IPSEC | |
133 | #include <netinet6/ipsec.h> | |
134 | #ifdef INET6 | |
135 | #include <netinet6/ipsec6.h> | |
136 | #endif | |
137 | extern int ipsec_bypass; | |
138 | #endif /*IPSEC*/ | |
139 | ||
140 | #include "faith.h" | |
141 | ||
142 | #include <net/net_osdep.h> | |
143 | ||
144 | /* | |
145 | * UDP protocol inplementation. | |
146 | * Per RFC 768, August, 1980. | |
147 | */ | |
148 | ||
149 | #define in6pcb inpcb | |
150 | #define udp6stat udpstat | |
151 | #define udp6s_opackets udps_opackets | |
152 | ||
91447636 A |
153 | static __inline__ u_int16_t |
154 | get_socket_id(struct socket * s) | |
155 | { | |
156 | u_int16_t val; | |
157 | ||
158 | if (s == NULL) { | |
159 | return (0); | |
160 | } | |
b0d623f7 | 161 | val = (u_int16_t)(((uintptr_t)s) / sizeof(struct socket)); |
91447636 A |
162 | if (val == 0) { |
163 | val = 0xffff; | |
164 | } | |
165 | return (val); | |
166 | } | |
167 | ||
9bccf70c A |
168 | int |
169 | udp6_output(in6p, m, addr6, control, p) | |
170 | struct in6pcb *in6p; | |
171 | struct mbuf *m; | |
172 | struct mbuf *control; | |
173 | struct sockaddr *addr6; | |
174 | struct proc *p; | |
175 | { | |
176 | u_int32_t ulen = m->m_pkthdr.len; | |
177 | u_int32_t plen = sizeof(struct udphdr) + ulen; | |
178 | struct ip6_hdr *ip6; | |
179 | struct udphdr *udp6; | |
180 | struct in6_addr *laddr, *faddr; | |
181 | u_short fport; | |
182 | int error = 0; | |
6d2010ae A |
183 | struct ip6_pktopts opt, *optp = NULL; |
184 | struct ip6_moptions *im6o; | |
9bccf70c A |
185 | int af = AF_INET6, hlen = sizeof(struct ip6_hdr); |
186 | int flags; | |
187 | struct sockaddr_in6 tmp; | |
91447636 | 188 | struct in6_addr storage; |
6d2010ae A |
189 | mbuf_traffic_class_t mtc = MBUF_TC_UNSPEC; |
190 | struct ip6_out_args ip6oa = { IFSCOPE_NONE, 0 }; | |
191 | ||
192 | if (in6p->inp_flags & INP_BOUND_IF) | |
193 | ip6oa.ip6oa_boundif = in6p->inp_boundif; | |
9bccf70c | 194 | |
6d2010ae | 195 | ip6oa.ip6oa_nocell = (in6p->inp_flags & INP_NO_IFT_CELLULAR) ? 1 : 0; |
b0d623f7 | 196 | |
9bccf70c | 197 | if (control) { |
d41d1dae | 198 | mtc = mbuf_traffic_class_from_control(control); |
d41d1dae | 199 | |
6d2010ae | 200 | if ((error = ip6_setpktopts(control, &opt, NULL, IPPROTO_UDP)) != 0) |
9bccf70c | 201 | goto release; |
6d2010ae A |
202 | optp = &opt; |
203 | } else | |
204 | optp = in6p->in6p_outputopts; | |
9bccf70c A |
205 | |
206 | if (addr6) { | |
207 | /* | |
208 | * IPv4 version of udp_output calls in_pcbconnect in this case, | |
209 | * which needs splnet and affects performance. | |
210 | * Since we saw no essential reason for calling in_pcbconnect, | |
211 | * we get rid of such kind of logic, and call in6_selectsrc | |
212 | * and in6_pcbsetport in order to fill in the local address | |
213 | * and the local port. | |
214 | */ | |
215 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr6; | |
216 | if (sin6->sin6_port == 0) { | |
217 | error = EADDRNOTAVAIL; | |
218 | goto release; | |
219 | } | |
220 | ||
221 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { | |
222 | /* how about ::ffff:0.0.0.0 case? */ | |
223 | error = EISCONN; | |
224 | goto release; | |
225 | } | |
226 | ||
227 | /* protect *sin6 from overwrites */ | |
228 | tmp = *sin6; | |
229 | sin6 = &tmp; | |
230 | ||
231 | faddr = &sin6->sin6_addr; | |
232 | fport = sin6->sin6_port; /* allow 0 port */ | |
233 | ||
234 | if (IN6_IS_ADDR_V4MAPPED(faddr)) { | |
235 | if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) { | |
236 | /* | |
237 | * I believe we should explicitly discard the | |
238 | * packet when mapped addresses are disabled, | |
239 | * rather than send the packet as an IPv6 one. | |
240 | * If we chose the latter approach, the packet | |
241 | * might be sent out on the wire based on the | |
242 | * default route, the situation which we'd | |
243 | * probably want to avoid. | |
244 | * (20010421 jinmei@kame.net) | |
245 | */ | |
246 | error = EINVAL; | |
247 | goto release; | |
248 | } else | |
249 | af = AF_INET; | |
250 | } | |
251 | ||
252 | /* KAME hack: embed scopeid */ | |
6d2010ae A |
253 | if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL, |
254 | optp) != 0) { | |
9bccf70c A |
255 | error = EINVAL; |
256 | goto release; | |
257 | } | |
258 | ||
259 | if (!IN6_IS_ADDR_V4MAPPED(faddr)) { | |
6d2010ae A |
260 | laddr = in6_selectsrc(sin6, optp, |
261 | in6p, &in6p->in6p_route, NULL, &storage, | |
262 | ip6oa.ip6oa_boundif, &error); | |
9bccf70c A |
263 | } else |
264 | laddr = &in6p->in6p_laddr; /* XXX */ | |
265 | if (laddr == NULL) { | |
266 | if (error == 0) | |
267 | error = EADDRNOTAVAIL; | |
268 | goto release; | |
269 | } | |
270 | if (in6p->in6p_lport == 0 && | |
91447636 | 271 | (error = in6_pcbsetport(laddr, in6p, p, 0)) != 0) |
9bccf70c A |
272 | goto release; |
273 | } else { | |
274 | if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { | |
275 | error = ENOTCONN; | |
276 | goto release; | |
277 | } | |
278 | if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) { | |
279 | if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) { | |
280 | /* | |
281 | * XXX: this case would happen when the | |
282 | * application sets the V6ONLY flag after | |
283 | * connecting the foreign address. | |
284 | * Such applications should be fixed, | |
285 | * so we bark here. | |
286 | */ | |
287 | log(LOG_INFO, "udp6_output: IPV6_V6ONLY " | |
288 | "option was set for a connected socket\n"); | |
289 | error = EINVAL; | |
290 | goto release; | |
291 | } else | |
292 | af = AF_INET; | |
293 | } | |
294 | laddr = &in6p->in6p_laddr; | |
295 | faddr = &in6p->in6p_faddr; | |
296 | fport = in6p->in6p_fport; | |
297 | } | |
298 | ||
299 | if (af == AF_INET) | |
300 | hlen = sizeof(struct ip); | |
301 | ||
302 | /* | |
303 | * Calculate data length and get a mbuf | |
304 | * for UDP and IP6 headers. | |
305 | */ | |
306 | M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT); | |
307 | if (m == 0) { | |
308 | error = ENOBUFS; | |
309 | goto release; | |
310 | } | |
311 | ||
312 | /* | |
313 | * Stuff checksum and output datagram. | |
314 | */ | |
315 | udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen); | |
316 | udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */ | |
317 | udp6->uh_dport = fport; | |
318 | if (plen <= 0xffff) | |
319 | udp6->uh_ulen = htons((u_short)plen); | |
320 | else | |
321 | udp6->uh_ulen = 0; | |
322 | udp6->uh_sum = 0; | |
323 | ||
324 | switch (af) { | |
325 | case AF_INET6: | |
326 | ip6 = mtod(m, struct ip6_hdr *); | |
327 | ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK; | |
328 | ip6->ip6_vfc &= ~IPV6_VERSION_MASK; | |
329 | ip6->ip6_vfc |= IPV6_VERSION; | |
330 | #if 0 /* ip6_plen will be filled in ip6_output. */ | |
331 | ip6->ip6_plen = htons((u_short)plen); | |
332 | #endif | |
333 | ip6->ip6_nxt = IPPROTO_UDP; | |
334 | ip6->ip6_hlim = in6_selecthlim(in6p, | |
335 | 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 | ||
6d2010ae A |
340 | udp6->uh_sum = in6_cksum_phdr(laddr, faddr, |
341 | htonl(plen), htonl(IPPROTO_UDP)); | |
342 | m->m_pkthdr.csum_flags = CSUM_UDPIPV6; | |
343 | m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); | |
9bccf70c | 344 | |
6d2010ae | 345 | flags = IPV6_OUTARGS; |
9bccf70c A |
346 | |
347 | udp6stat.udp6s_opackets++; | |
348 | #ifdef IPSEC | |
349 | if (ipsec_bypass == 0 && ipsec_setsocket(m, in6p->in6p_socket) != 0) { | |
350 | error = ENOBUFS; | |
351 | goto release; | |
352 | } | |
353 | #endif /*IPSEC*/ | |
91447636 | 354 | m->m_pkthdr.socket_id = get_socket_id(in6p->in6p_socket); |
6d2010ae A |
355 | |
356 | set_packet_tclass(m, in6p->in6p_socket, mtc, 1); | |
357 | ||
358 | im6o = in6p->in6p_moptions; | |
359 | if (im6o != NULL) | |
360 | IM6O_ADDREF(im6o); | |
361 | ||
362 | error = ip6_output(m, optp, &in6p->in6p_route, | |
363 | flags, im6o, NULL, &ip6oa); | |
364 | ||
365 | if (im6o != NULL) | |
366 | IM6O_REMREF(im6o); | |
d41d1dae | 367 | |
6d2010ae A |
368 | if (error == 0 && nstat_collect) { |
369 | locked_add_64(&in6p->inp_stat->txpackets, 1); | |
370 | locked_add_64(&in6p->inp_stat->txbytes, ulen); | |
371 | } | |
d1ecb069 | 372 | |
6d2010ae A |
373 | if (in6p->in6p_route.ro_rt != NULL) { |
374 | struct rtentry *rt = in6p->in6p_route.ro_rt; | |
375 | unsigned int outif; | |
376 | ||
377 | if ((rt->rt_flags & RTF_MULTICAST) || | |
378 | in6p->in6p_socket == NULL || | |
379 | !(in6p->in6p_socket->so_state & SS_ISCONNECTED)) { | |
380 | rt = NULL; /* unusable */ | |
381 | } | |
382 | /* | |
383 | * Always discard the cached route for unconnected | |
384 | * socket or if it is a multicast route. | |
385 | */ | |
386 | if (rt == NULL) { | |
387 | rtfree(in6p->in6p_route.ro_rt); | |
388 | in6p->in6p_route.ro_rt = NULL; | |
389 | } | |
390 | /* | |
391 | * If this is a connected socket and the destination | |
392 | * route is not multicast, update outif with that of | |
393 | * the route interface index used by IP. | |
394 | */ | |
395 | if (rt != NULL && (outif = rt->rt_ifp->if_index) != | |
396 | in6p->in6p_last_outif) | |
397 | in6p->in6p_last_outif = outif; | |
d1ecb069 | 398 | } |
9bccf70c A |
399 | break; |
400 | case AF_INET: | |
401 | error = EAFNOSUPPORT; | |
402 | goto release; | |
403 | } | |
404 | goto releaseopt; | |
405 | ||
406 | release: | |
407 | m_freem(m); | |
408 | ||
409 | releaseopt: | |
410 | if (control) { | |
6d2010ae A |
411 | if (optp == &opt) |
412 | ip6_clearpktopts(optp, -1); | |
9bccf70c A |
413 | m_freem(control); |
414 | } | |
415 | return(error); | |
416 | } |