]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/udp6_output.c
This commit was generated by cvs2svn to compensate for changes in r159609,
[FreeBSD/FreeBSD.git] / sys / netinet6 / udp6_output.c
1 /*      $FreeBSD$       */
2 /*      $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $    */
3
4 /*-
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
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.
19  *
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.
31  */
32
33 /*-
34  * Copyright (c) 1982, 1986, 1989, 1993
35  *      The Regents of the University of California.  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  * 4. 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.
48  *
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.
60  *
61  *      @(#)udp_var.h   8.1 (Berkeley) 6/10/93
62  */
63
64 #include "opt_ipsec.h"
65 #include "opt_inet.h"
66 #include "opt_inet6.h"
67
68 #include <sys/param.h>
69 #include <sys/proc.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/sysctl.h>
76 #include <sys/errno.h>
77 #include <sys/stat.h>
78 #include <sys/systm.h>
79 #include <sys/syslog.h>
80
81 #include <net/if.h>
82 #include <net/route.h>
83 #include <net/if_types.h>
84
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/udp.h>
92 #include <netinet/udp_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet6/in6_pcb.h>
96 #include <netinet6/udp6_var.h>
97 #include <netinet/icmp6.h>
98 #include <netinet6/ip6protosw.h>
99 #include <netinet6/scope6_var.h>
100
101 #ifdef IPSEC
102 #include <netinet6/ipsec.h>
103 #ifdef INET6
104 #include <netinet6/ipsec6.h>
105 #endif
106 #endif /* IPSEC */
107
108 #include <net/net_osdep.h>
109
110 /*
111  * UDP protocol inplementation.
112  * Per RFC 768, August, 1980.
113  */
114
115 #define in6pcb          inpcb
116 #define udp6stat        udpstat
117 #define udp6s_opackets  udps_opackets
118
119 int
120 udp6_output(in6p, m, addr6, control, td)
121         struct in6pcb *in6p;
122         struct mbuf *m;
123         struct mbuf *control;
124         struct sockaddr *addr6;
125         struct thread *td;
126 {
127         u_int32_t ulen = m->m_pkthdr.len;
128         u_int32_t plen = sizeof(struct udphdr) + ulen;
129         struct ip6_hdr *ip6;
130         struct udphdr *udp6;
131         struct in6_addr *laddr, *faddr;
132         struct sockaddr_in6 *sin6 = NULL;
133         struct ifnet *oifp = NULL;
134         int scope_ambiguous = 0;
135         u_short fport;
136         int error = 0;
137         struct ip6_pktopts *optp, opt;
138         int priv;
139         int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
140         int flags;
141         struct sockaddr_in6 tmp;
142
143         INP_LOCK_ASSERT(in6p);
144
145         priv = 0;
146         if (td && !suser(td))
147                 priv = 1;
148
149         if (addr6) {
150                 /* addr6 has been validated in udp6_send(). */
151                 sin6 = (struct sockaddr_in6 *)addr6;
152
153                 /* protect *sin6 from overwrites */
154                 tmp = *sin6;
155                 sin6 = &tmp;
156
157                 /*
158                  * Application should provide a proper zone ID or the use of
159                  * default zone IDs should be enabled.  Unfortunately, some
160                  * applications do not behave as it should, so we need a
161                  * workaround.  Even if an appropriate ID is not determined,
162                  * we'll see if we can determine the outgoing interface.  If we
163                  * can, determine the zone ID based on the interface below.
164                  */
165                 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
166                         scope_ambiguous = 1;
167                 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
168                         return (error);
169         }
170
171         if (control) {
172                 if ((error = ip6_setpktopts(control, &opt,
173                     in6p->in6p_outputopts, priv, IPPROTO_UDP)) != 0)
174                         goto release;
175                 optp = &opt;
176         } else
177                 optp = in6p->in6p_outputopts;
178
179         if (sin6) {
180                 faddr = &sin6->sin6_addr;
181
182                 /*
183                  * IPv4 version of udp_output calls in_pcbconnect in this case,
184                  * which needs splnet and affects performance.
185                  * Since we saw no essential reason for calling in_pcbconnect,
186                  * we get rid of such kind of logic, and call in6_selectsrc
187                  * and in6_pcbsetport in order to fill in the local address
188                  * and the local port.
189                  */
190                 if (sin6->sin6_port == 0) {
191                         error = EADDRNOTAVAIL;
192                         goto release;
193                 }
194
195                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
196                         /* how about ::ffff:0.0.0.0 case? */
197                         error = EISCONN;
198                         goto release;
199                 }
200
201                 fport = sin6->sin6_port; /* allow 0 port */
202
203                 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
204                         if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
205                                 /*
206                                  * I believe we should explicitly discard the
207                                  * packet when mapped addresses are disabled,
208                                  * rather than send the packet as an IPv6 one.
209                                  * If we chose the latter approach, the packet
210                                  * might be sent out on the wire based on the
211                                  * default route, the situation which we'd
212                                  * probably want to avoid.
213                                  * (20010421 jinmei@kame.net)
214                                  */
215                                 error = EINVAL;
216                                 goto release;
217                         }
218                         if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
219                             !IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
220                                 /*
221                                  * when remote addr is an IPv4-mapped address,
222                                  * local addr should not be an IPv6 address,
223                                  * since you cannot determine how to map IPv6
224                                  * source address to IPv4.
225                                  */
226                                 error = EINVAL;
227                                 goto release;
228                         }
229
230                         af = AF_INET;
231                 }
232
233                 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
234                         laddr = in6_selectsrc(sin6, optp, in6p->in6p_moptions,
235                             NULL, &in6p->in6p_laddr, &oifp, &error);
236                         if (oifp && scope_ambiguous &&
237                             (error = in6_setscope(&sin6->sin6_addr,
238                             oifp, NULL))) {
239                                 goto release;
240                         }
241                 } else
242                         laddr = &in6p->in6p_laddr;      /* XXX */
243                 if (laddr == NULL) {
244                         if (error == 0)
245                                 error = EADDRNOTAVAIL;
246                         goto release;
247                 }
248                 if (in6p->in6p_lport == 0 &&
249                     (error = in6_pcbsetport(laddr, in6p, td->td_ucred)) != 0)
250                         goto release;
251         } else {
252                 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
253                         error = ENOTCONN;
254                         goto release;
255                 }
256                 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
257                         if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
258                                 /*
259                                  * XXX: this case would happen when the
260                                  * application sets the V6ONLY flag after
261                                  * connecting the foreign address.
262                                  * Such applications should be fixed,
263                                  * so we bark here.
264                                  */
265                                 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
266                                     "option was set for a connected socket\n");
267                                 error = EINVAL;
268                                 goto release;
269                         } else
270                                 af = AF_INET;
271                 }
272                 laddr = &in6p->in6p_laddr;
273                 faddr = &in6p->in6p_faddr;
274                 fport = in6p->in6p_fport;
275         }
276
277         if (af == AF_INET)
278                 hlen = sizeof(struct ip);
279
280         /*
281          * Calculate data length and get a mbuf
282          * for UDP and IP6 headers.
283          */
284         M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
285         if (m == 0) {
286                 error = ENOBUFS;
287                 goto release;
288         }
289
290         /*
291          * Stuff checksum and output datagram.
292          */
293         udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
294         udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
295         udp6->uh_dport = fport;
296         if (plen <= 0xffff)
297                 udp6->uh_ulen = htons((u_short)plen);
298         else
299                 udp6->uh_ulen = 0;
300         udp6->uh_sum = 0;
301
302         switch (af) {
303         case AF_INET6:
304                 ip6 = mtod(m, struct ip6_hdr *);
305                 ip6->ip6_flow   = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
306                 ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
307                 ip6->ip6_vfc    |= IPV6_VERSION;
308 #if 0                           /* ip6_plen will be filled in ip6_output. */
309                 ip6->ip6_plen   = htons((u_short)plen);
310 #endif
311                 ip6->ip6_nxt    = IPPROTO_UDP;
312                 ip6->ip6_hlim   = in6_selecthlim(in6p, NULL);
313                 ip6->ip6_src    = *laddr;
314                 ip6->ip6_dst    = *faddr;
315
316                 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
317                                 sizeof(struct ip6_hdr), plen)) == 0) {
318                         udp6->uh_sum = 0xffff;
319                 }
320
321                 flags = 0;
322
323                 udp6stat.udp6s_opackets++;
324                 error = ip6_output(m, optp, NULL, flags, in6p->in6p_moptions,
325                     NULL, in6p);
326                 break;
327         case AF_INET:
328                 error = EAFNOSUPPORT;
329                 goto release;
330         }
331         goto releaseopt;
332
333 release:
334         m_freem(m);
335
336 releaseopt:
337         if (control) {
338                 ip6_clearpktopts(&opt, -1);
339                 m_freem(control);
340         }
341         return (error);
342 }