No empty .Rs/.Re
[netbsd-mini2440.git] / sys / netinet6 / udp6_output.c
blob8eb977b3d2f5d0a331e5c9a867793ac85ec0cf3f
1 /* $NetBSD: udp6_output.c,v 1.38 2009/04/30 18:18:34 elad Exp $ */
2 /* $KAME: udp6_output.c,v 1.43 2001/10/15 09:19:52 itojun Exp $ */
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
34 * Copyright (c) 1982, 1986, 1989, 1993
35 * The Regents of the University of California. All rights reserved.
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 University 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.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
61 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: udp6_output.c,v 1.38 2009/04/30 18:18:34 elad Exp $");
67 #include "opt_inet.h"
69 #include <sys/param.h>
70 #include <sys/malloc.h>
71 #include <sys/mbuf.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/errno.h>
76 #include <sys/stat.h>
77 #include <sys/systm.h>
78 #include <sys/proc.h>
79 #include <sys/syslog.h>
80 #include <sys/kauth.h>
81 #include <sys/domain.h>
83 #include <net/if.h>
84 #include <net/route.h>
85 #include <net/if_types.h>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/udp.h>
94 #include <netinet/udp_var.h>
95 #include <netinet/udp_private.h>
96 #include <netinet/ip6.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/in6_pcb.h>
99 #include <netinet6/udp6_var.h>
100 #include <netinet6/udp6_private.h>
101 #include <netinet/icmp6.h>
102 #include <netinet6/ip6protosw.h>
103 #include <netinet6/scope6_var.h>
105 #include "faith.h"
107 #include <net/net_osdep.h>
110 * UDP protocol inplementation.
111 * Per RFC 768, August, 1980.
115 udp6_output(struct in6pcb *in6p, struct mbuf *m, struct mbuf *addr6,
116 struct mbuf *control, struct lwp *l)
118 struct rtentry *rt;
119 u_int32_t ulen = m->m_pkthdr.len;
120 u_int32_t plen = sizeof(struct udphdr) + ulen;
121 struct ip6_hdr *ip6;
122 struct udphdr *udp6;
123 struct in6_addr *laddr, *faddr;
124 struct in6_addr laddr_mapped; /* XXX ugly */
125 struct sockaddr_in6 *sin6 = NULL;
126 struct ifnet *oifp = NULL;
127 int scope_ambiguous = 0;
128 u_int16_t fport;
129 int error = 0;
130 struct ip6_pktopts *optp, opt;
131 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
132 #ifdef INET
133 struct ip *ip;
134 struct udpiphdr *ui;
135 int flags = 0;
136 #endif
137 struct sockaddr_in6 tmp;
139 if (addr6) {
140 if (addr6->m_len != sizeof(*sin6)) {
141 error = EINVAL;
142 goto release;
144 sin6 = mtod(addr6, struct sockaddr_in6 *);
145 if (sin6->sin6_family != AF_INET6) {
146 error = EAFNOSUPPORT;
147 goto release;
150 /* protect *sin6 from overwrites */
151 tmp = *sin6;
152 sin6 = &tmp;
155 * Application should provide a proper zone ID or the use of
156 * default zone IDs should be enabled. Unfortunately, some
157 * applications do not behave as it should, so we need a
158 * workaround. Even if an appropriate ID is not determined,
159 * we'll see if we can determine the outgoing interface. If we
160 * can, determine the zone ID based on the interface below.
162 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
163 scope_ambiguous = 1;
164 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
165 return (error);
168 if (control) {
169 if ((error = ip6_setpktopts(control, &opt,
170 in6p->in6p_outputopts, l->l_cred, IPPROTO_UDP)) != 0)
171 goto release;
172 optp = &opt;
173 } else
174 optp = in6p->in6p_outputopts;
177 if (sin6) {
178 faddr = &sin6->sin6_addr;
181 * IPv4 version of udp_output calls in_pcbconnect in this case,
182 * which needs splnet and affects performance.
183 * Since we saw no essential reason for calling in_pcbconnect,
184 * we get rid of such kind of logic, and call in6_selectsrc
185 * and in6_pcbsetport in order to fill in the local address
186 * and the local port.
188 if (sin6->sin6_port == 0) {
189 error = EADDRNOTAVAIL;
190 goto release;
193 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
194 /* how about ::ffff:0.0.0.0 case? */
195 error = EISCONN;
196 goto release;
200 faddr = &sin6->sin6_addr;
201 fport = sin6->sin6_port; /* allow 0 port */
203 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
204 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
207 * I believe we should explicitly discard the
208 * packet when mapped addresses are disabled,
209 * rather than send the packet as an IPv6 one.
210 * If we chose the latter approach, the packet
211 * might be sent out on the wire based on the
212 * default route, the situation which we'd
213 * probably want to avoid.
214 * (20010421 jinmei@kame.net)
216 error = EINVAL;
217 goto release;
219 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
220 && !IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
222 * when remote addr is an IPv4-mapped address,
223 * local addr should not be an IPv6 address,
224 * since you cannot determine how to map IPv6
225 * source address to IPv4.
227 error = EINVAL;
228 goto release;
231 af = AF_INET;
234 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
235 laddr = in6_selectsrc(sin6, optp,
236 in6p->in6p_moptions,
237 &in6p->in6p_route,
238 &in6p->in6p_laddr, &oifp, &error);
239 if (oifp && scope_ambiguous &&
240 (error = in6_setscope(&sin6->sin6_addr,
241 oifp, NULL))) {
242 goto release;
244 } else {
246 * XXX: freebsd[34] does not have in_selectsrc, but
247 * we can omit the whole part because freebsd4 calls
248 * udp_output() directly in this case, and thus we'll
249 * never see this path.
251 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
252 struct sockaddr_in *sinp, sin_dst;
253 struct in_addr ina;
255 memcpy(&ina, &faddr->s6_addr[12], sizeof(ina));
256 sockaddr_in_init(&sin_dst, &ina, 0);
257 sinp = in_selectsrc(&sin_dst, &in6p->in6p_route,
258 in6p->in6p_socket->so_options, NULL,
259 &error);
260 if (sinp == NULL) {
261 if (error == 0)
262 error = EADDRNOTAVAIL;
263 goto release;
265 memset(&laddr_mapped, 0, sizeof(laddr_mapped));
266 laddr_mapped.s6_addr16[5] = 0xffff; /* ugly */
267 memcpy(&laddr_mapped.s6_addr[12],
268 &sinp->sin_addr,
269 sizeof(sinp->sin_addr));
270 laddr = &laddr_mapped;
271 } else
273 laddr = &in6p->in6p_laddr; /* XXX */
276 if (laddr == NULL) {
277 if (error == 0)
278 error = EADDRNOTAVAIL;
279 goto release;
281 if (in6p->in6p_lport == 0) {
283 * Craft a sockaddr_in6 for the local endpoint. Use the
284 * "any" as a base, set the address, and recover the
285 * scope.
287 struct sockaddr_in6 lsin6 =
288 *((const struct sockaddr_in6 *)in6p->in6p_socket->so_proto->pr_domain->dom_sa_any);
289 lsin6.sin6_addr = *laddr;
290 error = sa6_recoverscope(&lsin6);
291 if (error)
292 goto release;
293 error = in6_pcbsetport(&lsin6, in6p, l);
294 if (error)
295 goto release;
297 } else {
298 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
299 error = ENOTCONN;
300 goto release;
302 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
303 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
306 * XXX: this case would happen when the
307 * application sets the V6ONLY flag after
308 * connecting the foreign address.
309 * Such applications should be fixed,
310 * so we bark here.
312 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
313 "option was set for a connected socket\n");
314 error = EINVAL;
315 goto release;
316 } else
317 af = AF_INET;
319 laddr = &in6p->in6p_laddr;
320 faddr = &in6p->in6p_faddr;
321 fport = in6p->in6p_fport;
324 if (af == AF_INET)
325 hlen = sizeof(struct ip);
328 * Calculate data length and get a mbuf
329 * for UDP and IP6 headers.
331 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
332 if (m == 0) {
333 error = ENOBUFS;
334 goto release;
338 * Stuff checksum and output datagram.
340 udp6 = (struct udphdr *)(mtod(m, char *) + hlen);
341 udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
342 udp6->uh_dport = fport;
343 if (plen <= 0xffff)
344 udp6->uh_ulen = htons((u_int16_t)plen);
345 else
346 udp6->uh_ulen = 0;
347 udp6->uh_sum = 0;
349 switch (af) {
350 case AF_INET6:
351 ip6 = mtod(m, struct ip6_hdr *);
352 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
353 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
354 ip6->ip6_vfc |= IPV6_VERSION;
355 #if 0 /* ip6_plen will be filled in ip6_output. */
356 ip6->ip6_plen = htons((u_int16_t)plen);
357 #endif
358 ip6->ip6_nxt = IPPROTO_UDP;
359 ip6->ip6_hlim = in6_selecthlim(in6p,
360 (rt = rtcache_validate(&in6p->in6p_route)) != NULL
361 ? rt->rt_ifp : NULL);
362 ip6->ip6_src = *laddr;
363 ip6->ip6_dst = *faddr;
365 udp6->uh_sum = in6_cksum_phdr(laddr, faddr,
366 htonl(plen), htonl(IPPROTO_UDP));
367 m->m_pkthdr.csum_flags = M_CSUM_UDPv6;
368 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
370 UDP6_STATINC(UDP6_STAT_OPACKETS);
371 error = ip6_output(m, optp, &in6p->in6p_route, 0,
372 in6p->in6p_moptions, in6p->in6p_socket, NULL);
373 break;
374 case AF_INET:
375 #ifdef INET
376 /* can't transmit jumbogram over IPv4 */
377 if (plen > 0xffff) {
378 error = EMSGSIZE;
379 goto release;
382 ip = mtod(m, struct ip *);
383 ui = (struct udpiphdr *)ip;
384 memset(ui->ui_x1, 0, sizeof(ui->ui_x1));
385 ui->ui_pr = IPPROTO_UDP;
386 ui->ui_len = htons(plen);
387 memcpy(&ui->ui_src, &laddr->s6_addr[12], sizeof(ui->ui_src));
388 ui->ui_ulen = ui->ui_len;
390 flags = (in6p->in6p_socket->so_options &
391 (SO_DONTROUTE | SO_BROADCAST));
392 memcpy(&ui->ui_dst, &faddr->s6_addr[12], sizeof(ui->ui_dst));
394 udp6->uh_sum = in_cksum(m, hlen + plen);
395 if (udp6->uh_sum == 0)
396 udp6->uh_sum = 0xffff;
398 ip->ip_len = htons(hlen + plen);
399 ip->ip_ttl = in6_selecthlim(in6p, NULL); /* XXX */
400 ip->ip_tos = 0; /* XXX */
402 UDP_STATINC(UDP_STAT_OPACKETS);
403 error = ip_output(m, NULL, &in6p->in6p_route, flags /* XXX */,
404 (struct ip_moptions *)NULL,
405 (struct socket *)in6p->in6p_socket);
406 break;
407 #else
408 error = EAFNOSUPPORT;
409 goto release;
410 #endif
412 goto releaseopt;
414 release:
415 m_freem(m);
417 releaseopt:
418 if (control) {
419 ip6_clearpktopts(&opt, -1);
420 m_freem(control);
422 return (error);