]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/udp6_output.c
This commit was generated by cvs2svn to compensate for changes in r76371,
[FreeBSD/FreeBSD.git] / sys / netinet6 / udp6_output.c
1 /*      $FreeBSD$       */
2 /*      $KAME: udp6_output.c,v 1.14 2000/06/13 10:31:23 itojun 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  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *      This product includes software developed by the University of
48  *      California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *      @(#)udp_var.h   8.1 (Berkeley) 6/10/93
66  */
67
68 #include "opt_ipsec.h"
69 #include "opt_inet.h"
70
71 #include <sys/param.h>
72 #include <sys/mbuf.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sysctl.h>
77 #include <sys/errno.h>
78 #include <sys/stat.h>
79 #include <sys/systm.h>
80 #include <sys/syslog.h>
81
82 #include <net/if.h>
83 #include <net/route.h>
84 #include <net/if_types.h>
85
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/ip6.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/in6_pcb.h>
97 #include <netinet6/udp6_var.h>
98 #include <netinet/icmp6.h>
99 #include <netinet6/ip6protosw.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 "faith.h"
109
110 #include <net/net_osdep.h>
111
112 /*
113  * UDP protocol inplementation.
114  * Per RFC 768, August, 1980.
115  */
116
117 #define in6pcb          inpcb
118 #define udp6stat        udpstat
119 #define udp6s_opackets  udps_opackets
120
121 int
122 udp6_output(in6p, m, addr6, control, p)
123         register struct in6pcb *in6p;
124         register struct mbuf *m;
125         struct mbuf *control;
126         struct sockaddr *addr6;
127         struct proc *p;
128 {
129         register u_int32_t ulen = m->m_pkthdr.len;
130         u_int32_t plen = sizeof(struct udphdr) + ulen;
131         struct ip6_hdr *ip6;
132         struct udphdr *udp6;
133         struct  in6_addr *laddr, *faddr;
134         u_short fport;
135         int error = 0;
136         struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
137         int priv;
138         int af, hlen;
139         int flags;
140         struct sockaddr_in6 tmp;
141
142         priv = 0;
143         if (p && !suser(p))
144                 priv = 1;
145         if (control) {
146                 if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
147                         goto release;
148                 in6p->in6p_outputopts = &opt;
149         }
150
151         if (addr6) {
152                 /*
153                  * IPv4 version of udp_output calls in_pcbconnect in this case,
154                  * which needs splnet and affects performance.
155                  * Since we saw no essential reason for calling in_pcbconnect,
156                  * we get rid of such kind of logic, and call in6_selectsrc
157                  * and in6_pcbsetport in order to fill in the local address
158                  * and the local port.
159                  */
160                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr6;
161                 if (sin6->sin6_port == 0) {
162                         error = EADDRNOTAVAIL;
163                         goto release;
164                 }
165
166                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
167                         error = EISCONN;
168                         goto release;
169                 }
170
171                 /* protect *sin6 from overwrites */
172                 tmp = *sin6;
173                 sin6 = &tmp;
174
175                 faddr = &sin6->sin6_addr;
176                 fport = sin6->sin6_port; /* allow 0 port */
177
178                 /* KAME hack: embed scopeid */
179                 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) {
180                         error = EINVAL;
181                         goto release;
182                 }
183
184                 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
185                         laddr = in6_selectsrc(sin6, in6p->in6p_outputopts,
186                                               in6p->in6p_moptions,
187                                               &in6p->in6p_route,
188                                               &in6p->in6p_laddr, &error);
189                 } else
190                         laddr = &in6p->in6p_laddr;      /*XXX*/
191                 if (laddr == NULL) {
192                         if (error == 0)
193                                 error = EADDRNOTAVAIL;
194                         goto release;
195                 }
196                 if (in6p->in6p_lport == 0 &&
197                     (error = in6_pcbsetport(laddr, in6p, p)) != 0)
198                         goto release;
199         } else {
200                 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
201                         error = ENOTCONN;
202                         goto release;
203                 }
204                 laddr = &in6p->in6p_laddr;
205                 faddr = &in6p->in6p_faddr;
206                 fport = in6p->in6p_fport;
207         }
208
209         if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
210                 af = AF_INET6;
211                 hlen = sizeof(struct ip6_hdr);
212         } else {
213                 af = AF_INET;
214                 hlen = sizeof(struct ip);
215         }
216
217         /*
218          * Calculate data length and get a mbuf
219          * for UDP and IP6 headers.
220          */
221         M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
222         if (m == 0) {
223                 error = ENOBUFS;
224                 goto release;
225         }
226
227         /*
228          * Stuff checksum and output datagram.
229          */
230         udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
231         udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
232         udp6->uh_dport = fport;
233         if (plen <= 0xffff)
234                 udp6->uh_ulen = htons((u_short)plen);
235         else
236                 udp6->uh_ulen = 0;
237         udp6->uh_sum = 0;
238
239         switch (af) {
240         case AF_INET6:
241                 ip6 = mtod(m, struct ip6_hdr *);
242                 ip6->ip6_flow   = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
243                 ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
244                 ip6->ip6_vfc    |= IPV6_VERSION;
245 #if 0                           /* ip6_plen will be filled in ip6_output. */
246                 ip6->ip6_plen   = htons((u_short)plen);
247 #endif
248                 ip6->ip6_nxt    = IPPROTO_UDP;
249                 ip6->ip6_hlim   = in6_selecthlim(in6p,
250                                                  in6p->in6p_route.ro_rt ?
251                                                  in6p->in6p_route.ro_rt->rt_ifp : NULL);
252                 ip6->ip6_src    = *laddr;
253                 ip6->ip6_dst    = *faddr;
254
255                 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
256                                 sizeof(struct ip6_hdr), plen)) == 0) {
257                         udp6->uh_sum = 0xffff;
258                 }
259
260                 flags = 0;
261
262                 udp6stat.udp6s_opackets++;
263 #ifdef IPSEC
264                 ipsec_setsocket(m, in6p->in6p_socket);
265 #endif /*IPSEC*/
266                 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
267                             flags, in6p->in6p_moptions, NULL);
268                 break;
269         case AF_INET:
270                 error = EAFNOSUPPORT;
271                 goto release;
272         }
273         goto releaseopt;
274
275 release:
276         m_freem(m);
277
278 releaseopt:
279         if (control) {
280                 in6p->in6p_outputopts = stickyopt;
281                 m_freem(control);
282         }
283         return(error);
284 }