Break paragraph with driver rewrite.
[netbsd-mini2440.git] / sys / netinet6 / udp6_output.c
blob5a64227a599380921c7f6e7fd77e354f336dc3fb
1 /* $NetBSD: udp6_output.c,v 1.36 2008/05/13 17:53:52 dyoung 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.36 2008/05/13 17:53:52 dyoung 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>
82 #include <net/if.h>
83 #include <net/route.h>
84 #include <net/if_types.h>
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
90 #include <netinet/ip_var.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet/udp.h>
93 #include <netinet/udp_var.h>
94 #include <netinet/udp_private.h>
95 #include <netinet/ip6.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/in6_pcb.h>
98 #include <netinet6/udp6_var.h>
99 #include <netinet6/udp6_private.h>
100 #include <netinet/icmp6.h>
101 #include <netinet6/ip6protosw.h>
102 #include <netinet6/scope6_var.h>
104 #include "faith.h"
106 #include <net/net_osdep.h>
109 * UDP protocol inplementation.
110 * Per RFC 768, August, 1980.
114 udp6_output(struct in6pcb *in6p, struct mbuf *m, struct mbuf *addr6,
115 struct mbuf *control, struct lwp *l)
117 struct rtentry *rt;
118 u_int32_t ulen = m->m_pkthdr.len;
119 u_int32_t plen = sizeof(struct udphdr) + ulen;
120 struct ip6_hdr *ip6;
121 struct udphdr *udp6;
122 struct in6_addr *laddr, *faddr;
123 struct in6_addr laddr_mapped; /* XXX ugly */
124 struct sockaddr_in6 *sin6 = NULL;
125 struct ifnet *oifp = NULL;
126 int scope_ambiguous = 0;
127 u_int16_t fport;
128 int error = 0;
129 struct ip6_pktopts *optp, opt;
130 int priv;
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 priv = 0;
140 if (l && !kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
141 NULL))
142 priv = 1;
144 if (addr6) {
145 if (addr6->m_len != sizeof(*sin6)) {
146 error = EINVAL;
147 goto release;
149 sin6 = mtod(addr6, struct sockaddr_in6 *);
150 if (sin6->sin6_family != AF_INET6) {
151 error = EAFNOSUPPORT;
152 goto release;
155 /* protect *sin6 from overwrites */
156 tmp = *sin6;
157 sin6 = &tmp;
160 * Application should provide a proper zone ID or the use of
161 * default zone IDs should be enabled. Unfortunately, some
162 * applications do not behave as it should, so we need a
163 * workaround. Even if an appropriate ID is not determined,
164 * we'll see if we can determine the outgoing interface. If we
165 * can, determine the zone ID based on the interface below.
167 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
168 scope_ambiguous = 1;
169 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
170 return (error);
173 if (control) {
174 if ((error = ip6_setpktopts(control, &opt,
175 in6p->in6p_outputopts, priv, IPPROTO_UDP)) != 0)
176 goto release;
177 optp = &opt;
178 } else
179 optp = in6p->in6p_outputopts;
182 if (sin6) {
183 faddr = &sin6->sin6_addr;
186 * IPv4 version of udp_output calls in_pcbconnect in this case,
187 * which needs splnet and affects performance.
188 * Since we saw no essential reason for calling in_pcbconnect,
189 * we get rid of such kind of logic, and call in6_selectsrc
190 * and in6_pcbsetport in order to fill in the local address
191 * and the local port.
193 if (sin6->sin6_port == 0) {
194 error = EADDRNOTAVAIL;
195 goto release;
198 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
199 /* how about ::ffff:0.0.0.0 case? */
200 error = EISCONN;
201 goto release;
205 faddr = &sin6->sin6_addr;
206 fport = sin6->sin6_port; /* allow 0 port */
208 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
209 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
212 * I believe we should explicitly discard the
213 * packet when mapped addresses are disabled,
214 * rather than send the packet as an IPv6 one.
215 * If we chose the latter approach, the packet
216 * might be sent out on the wire based on the
217 * default route, the situation which we'd
218 * probably want to avoid.
219 * (20010421 jinmei@kame.net)
221 error = EINVAL;
222 goto release;
224 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
225 && !IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
227 * when remote addr is an IPv4-mapped address,
228 * local addr should not be an IPv6 address,
229 * since you cannot determine how to map IPv6
230 * source address to IPv4.
232 error = EINVAL;
233 goto release;
236 af = AF_INET;
239 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
240 laddr = in6_selectsrc(sin6, optp,
241 in6p->in6p_moptions,
242 &in6p->in6p_route,
243 &in6p->in6p_laddr, &oifp, &error);
244 if (oifp && scope_ambiguous &&
245 (error = in6_setscope(&sin6->sin6_addr,
246 oifp, NULL))) {
247 goto release;
249 } else {
251 * XXX: freebsd[34] does not have in_selectsrc, but
252 * we can omit the whole part because freebsd4 calls
253 * udp_output() directly in this case, and thus we'll
254 * never see this path.
256 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
257 struct sockaddr_in *sinp, sin_dst;
258 struct in_addr ina;
260 memcpy(&ina, &faddr->s6_addr[12], sizeof(ina));
261 sockaddr_in_init(&sin_dst, &ina, 0);
262 sinp = in_selectsrc(&sin_dst, &in6p->in6p_route,
263 in6p->in6p_socket->so_options, NULL,
264 &error);
265 if (sinp == NULL) {
266 if (error == 0)
267 error = EADDRNOTAVAIL;
268 goto release;
270 memset(&laddr_mapped, 0, sizeof(laddr_mapped));
271 laddr_mapped.s6_addr16[5] = 0xffff; /* ugly */
272 memcpy(&laddr_mapped.s6_addr[12],
273 &sinp->sin_addr,
274 sizeof(sinp->sin_addr));
275 laddr = &laddr_mapped;
276 } else
278 laddr = &in6p->in6p_laddr; /* XXX */
281 if (laddr == NULL) {
282 if (error == 0)
283 error = EADDRNOTAVAIL;
284 goto release;
286 if (in6p->in6p_lport == 0 &&
287 (error = in6_pcbsetport(laddr, in6p, l)) != 0)
288 goto release;
289 } else {
290 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
291 error = ENOTCONN;
292 goto release;
294 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
295 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
298 * XXX: this case would happen when the
299 * application sets the V6ONLY flag after
300 * connecting the foreign address.
301 * Such applications should be fixed,
302 * so we bark here.
304 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
305 "option was set for a connected socket\n");
306 error = EINVAL;
307 goto release;
308 } else
309 af = AF_INET;
311 laddr = &in6p->in6p_laddr;
312 faddr = &in6p->in6p_faddr;
313 fport = in6p->in6p_fport;
316 if (af == AF_INET)
317 hlen = sizeof(struct ip);
320 * Calculate data length and get a mbuf
321 * for UDP and IP6 headers.
323 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
324 if (m == 0) {
325 error = ENOBUFS;
326 goto release;
330 * Stuff checksum and output datagram.
332 udp6 = (struct udphdr *)(mtod(m, char *) + hlen);
333 udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
334 udp6->uh_dport = fport;
335 if (plen <= 0xffff)
336 udp6->uh_ulen = htons((u_int16_t)plen);
337 else
338 udp6->uh_ulen = 0;
339 udp6->uh_sum = 0;
341 switch (af) {
342 case AF_INET6:
343 ip6 = mtod(m, struct ip6_hdr *);
344 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
345 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
346 ip6->ip6_vfc |= IPV6_VERSION;
347 #if 0 /* ip6_plen will be filled in ip6_output. */
348 ip6->ip6_plen = htons((u_int16_t)plen);
349 #endif
350 ip6->ip6_nxt = IPPROTO_UDP;
351 ip6->ip6_hlim = in6_selecthlim(in6p,
352 (rt = rtcache_validate(&in6p->in6p_route)) != NULL
353 ? rt->rt_ifp : NULL);
354 ip6->ip6_src = *laddr;
355 ip6->ip6_dst = *faddr;
357 udp6->uh_sum = in6_cksum_phdr(laddr, faddr,
358 htonl(plen), htonl(IPPROTO_UDP));
359 m->m_pkthdr.csum_flags = M_CSUM_UDPv6;
360 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
362 UDP6_STATINC(UDP6_STAT_OPACKETS);
363 error = ip6_output(m, optp, &in6p->in6p_route, 0,
364 in6p->in6p_moptions, in6p->in6p_socket, NULL);
365 break;
366 case AF_INET:
367 #ifdef INET
368 /* can't transmit jumbogram over IPv4 */
369 if (plen > 0xffff) {
370 error = EMSGSIZE;
371 goto release;
374 ip = mtod(m, struct ip *);
375 ui = (struct udpiphdr *)ip;
376 memset(ui->ui_x1, 0, sizeof(ui->ui_x1));
377 ui->ui_pr = IPPROTO_UDP;
378 ui->ui_len = htons(plen);
379 memcpy(&ui->ui_src, &laddr->s6_addr[12], sizeof(ui->ui_src));
380 ui->ui_ulen = ui->ui_len;
382 flags = (in6p->in6p_socket->so_options &
383 (SO_DONTROUTE | SO_BROADCAST));
384 memcpy(&ui->ui_dst, &faddr->s6_addr[12], sizeof(ui->ui_dst));
386 udp6->uh_sum = in_cksum(m, hlen + plen);
387 if (udp6->uh_sum == 0)
388 udp6->uh_sum = 0xffff;
390 ip->ip_len = htons(hlen + plen);
391 ip->ip_ttl = in6_selecthlim(in6p, NULL); /* XXX */
392 ip->ip_tos = 0; /* XXX */
394 UDP_STATINC(UDP_STAT_OPACKETS);
395 error = ip_output(m, NULL, &in6p->in6p_route, flags /* XXX */,
396 (struct ip_moptions *)NULL,
397 (struct socket *)in6p->in6p_socket);
398 break;
399 #else
400 error = EAFNOSUPPORT;
401 goto release;
402 #endif
404 goto releaseopt;
406 release:
407 m_freem(m);
409 releaseopt:
410 if (control) {
411 ip6_clearpktopts(&opt, -1);
412 m_freem(control);
414 return (error);