xref: /dragonfly/sys/netinet6/udp6_output.c (revision 2b3f93ea6d1f70880f3e87f3c2cbe0dc0bfc9332)
1 /*        $FreeBSD: src/sys/netinet6/udp6_output.c,v 1.1.2.6 2003/01/23 21:06:47 sam Exp $          */
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  * 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.
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_inet.h"
65 #include "opt_inet6.h"
66 
67 #include <sys/param.h>
68 #include <sys/malloc.h>
69 #include <sys/mbuf.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/sysctl.h>
74 #include <sys/errno.h>
75 #include <sys/stat.h>
76 #include <sys/systm.h>
77 #include <sys/proc.h>
78 #include <sys/caps.h>
79 #include <sys/syslog.h>
80 #include <sys/jail.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 #include <net/net_osdep.h>
102 
103 /*
104  * UDP protocol inplementation.
105  * Per RFC 768, August, 1980.
106  */
107 
108 #define in6pcb                inpcb
109 #define udp6stat    udp_stat
110 #define udp6s_opackets        udps_opackets
111 
112 int
udp6_output(struct in6pcb * in6p,struct mbuf * m,struct sockaddr * addr6,struct mbuf * control,struct thread * td)113 udp6_output(struct in6pcb *in6p, struct mbuf *m, struct sockaddr *addr6,
114               struct mbuf *control, struct thread *td)
115 {
116           u_int32_t ulen = m->m_pkthdr.len;
117           u_int32_t plen = sizeof(struct udphdr) + ulen;
118           struct ip6_hdr *ip6;
119           struct udphdr *udp6;
120           struct in6_addr *laddr, *faddr;
121           u_short fport;
122           int error = 0;
123           struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
124           int priv;
125           int hlen = sizeof(struct ip6_hdr);
126           struct sockaddr_in6 tmp;
127 
128           /*
129            * 1 if privileged, 0 if not
130            */
131           priv = !caps_priv_check_td(td, SYSCAP_RESTRICTEDROOT);
132 
133           if (control) {
134                     if ((error = ip6_setpktoptions(control, &opt,
135                         in6p->in6p_outputopts,
136                         IPPROTO_UDP, priv)) != 0)
137                               goto release;
138                     in6p->in6p_outputopts = &opt;
139           }
140 
141           if (addr6) {
142                     /*
143                      * IPv4 version of udp_output calls in_pcbconnect in this case,
144                      * which needs splnet and affects performance.
145                      * Since we saw no essential reason for calling in_pcbconnect,
146                      * we get rid of such kind of logic, and call in6_selectsrc
147                      * and in6_pcbsetlport in order to fill in the local address
148                      * and the local port.
149                      */
150                     struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr6;
151 
152                     /* Caller should have rejected the v4-mapped address */
153                     KASSERT(!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr),
154                         ("v4-mapped address"));
155 
156                     if (sin6->sin6_port == 0) {
157                               error = EADDRNOTAVAIL;
158                               goto release;
159                     }
160 
161                     if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
162                               /* how about ::ffff:0.0.0.0 case? */
163                               error = EISCONN;
164                               goto release;
165                     }
166                     if (!prison_remote_ip(td, addr6)) {
167                               error = EAFNOSUPPORT; /* IPv4 only jail */
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                     laddr = in6_selectsrc(sin6, in6p->in6p_outputopts,
185                                               in6p->in6p_moptions,
186                                               &in6p->in6p_route,
187                                               &in6p->in6p_laddr, &error, NULL);
188                     if (laddr == NULL) {
189                               if (error == 0)
190                                         error = EADDRNOTAVAIL;
191                               goto release;
192                     }
193                     if (in6p->in6p_lport == 0) {
194                               if ((error = in6_pcbsetlport(laddr, in6p, td)) != 0)
195                                         goto release;
196                               in_pcbinswildcardhash(in6p);
197                     }
198           } else {
199                     if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
200                               error = ENOTCONN;
201                               goto release;
202                     }
203 
204                     /* Connection to v4-mapped address should have been rejected */
205                     KASSERT(!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr),
206                         ("bound to v4-mapped address"));
207 
208                     laddr = &in6p->in6p_laddr;
209                     faddr = &in6p->in6p_faddr;
210                     fport = in6p->in6p_fport;
211           }
212 
213           /*
214            * Calculate data length and get a mbuf
215            * for UDP and IP6 headers.
216            */
217           M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
218           if (m == NULL) {
219                     error = ENOBUFS;
220                     goto release;
221           }
222 
223           /*
224            * Stuff checksum and output datagram.
225            */
226           udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
227           udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
228           udp6->uh_dport = fport;
229           if (plen <= 0xffff)
230                     udp6->uh_ulen = htons((u_short)plen);
231           else
232                     udp6->uh_ulen = 0;
233           udp6->uh_sum = 0;
234 
235           ip6 = mtod(m, struct ip6_hdr *);
236           ip6->ip6_flow       = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
237           ip6->ip6_vfc        &= ~IPV6_VERSION_MASK;
238           ip6->ip6_vfc        |= IPV6_VERSION;
239 #if 0                                   /* ip6_plen will be filled in ip6_output. */
240           ip6->ip6_plen       = htons((u_short)plen);
241 #endif
242           ip6->ip6_nxt        = IPPROTO_UDP;
243           ip6->ip6_hlim       = in6_selecthlim(in6p,
244                                                    in6p->in6p_route.ro_rt ?
245                                                    in6p->in6p_route.ro_rt->rt_ifp : NULL);
246           ip6->ip6_src        = *laddr;
247           ip6->ip6_dst        = *faddr;
248 
249           if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
250                               sizeof(struct ip6_hdr), plen)) == 0) {
251                     udp6->uh_sum = 0xffff;
252           }
253 
254           udp6stat.udp6s_opackets++;
255           error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route, 0,
256               in6p->in6p_moptions, NULL, in6p);
257           goto releaseopt;
258 
259 release:
260           m_freem(m);
261 
262 releaseopt:
263           if (control) {
264                     ip6_clearpktopts(in6p->in6p_outputopts, -1);
265                     in6p->in6p_outputopts = stickyopt;
266                     m_freem(control);
267           }
268           return (error);
269 }
270