Deleted Added
full compact
udp6_usrreq.c (190963) udp6_usrreq.c (191672)
1/*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
30 * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
31 */
32
33/*-
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
35 * The Regents of the University of California.
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
63 */
64
65#include <sys/cdefs.h>
1/*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
30 * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
31 */
32
33/*-
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
35 * The Regents of the University of California.
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
63 */
64
65#include <sys/cdefs.h>
66__FBSDID("$FreeBSD: head/sys/netinet6/udp6_usrreq.c 190963 2009-04-12 11:53:12Z rwatson $");
66__FBSDID("$FreeBSD: head/sys/netinet6/udp6_usrreq.c 191672 2009-04-29 19:19:13Z bms $");
67
68#include "opt_inet.h"
69#include "opt_inet6.h"
70#include "opt_ipsec.h"
71#include "opt_mac.h"
72
73#include <sys/param.h>
74#include <sys/jail.h>
75#include <sys/kernel.h>
76#include <sys/lock.h>
77#include <sys/mbuf.h>
78#include <sys/priv.h>
79#include <sys/proc.h>
80#include <sys/protosw.h>
81#include <sys/signalvar.h>
82#include <sys/socket.h>
83#include <sys/socketvar.h>
84#include <sys/sx.h>
85#include <sys/sysctl.h>
86#include <sys/syslog.h>
87#include <sys/systm.h>
88#include <sys/vimage.h>
89
90#include <net/if.h>
91#include <net/if_types.h>
92#include <net/route.h>
93
94#include <netinet/in.h>
95#include <netinet/in_pcb.h>
96#include <netinet/in_systm.h>
97#include <netinet/in_var.h>
98#include <netinet/ip.h>
99#include <netinet/ip_icmp.h>
100#include <netinet/ip6.h>
101#include <netinet/icmp_var.h>
102#include <netinet/icmp6.h>
103#include <netinet/ip_var.h>
104#include <netinet/udp.h>
105#include <netinet/udp_var.h>
106#include <netinet/vinet.h>
107
108#include <netinet6/ip6protosw.h>
109#include <netinet6/ip6_var.h>
110#include <netinet6/in6_pcb.h>
111#include <netinet6/udp6_var.h>
112#include <netinet6/scope6_var.h>
113#include <netinet6/vinet6.h>
114
115#ifdef IPSEC
116#include <netipsec/ipsec.h>
117#include <netipsec/ipsec6.h>
118#endif /* IPSEC */
119
120#include <security/mac/mac_framework.h>
121
122/*
123 * UDP protocol implementation.
124 * Per RFC 768, August, 1980.
125 */
126
127extern struct protosw inetsw[];
128static void udp6_detach(struct socket *so);
129
130static void
131udp6_append(struct inpcb *inp, struct mbuf *n, int off,
132 struct sockaddr_in6 *fromsa)
133{
134 INIT_VNET_INET(inp->inp_vnet);
135 struct socket *so;
136 struct mbuf *opts;
137
138 INP_LOCK_ASSERT(inp);
139
140#ifdef IPSEC
141 /* Check AH/ESP integrity. */
142 if (ipsec6_in_reject(n, inp)) {
143 INIT_VNET_IPSEC(inp->inp_vnet);
144 m_freem(n);
145 V_ipsec6stat.in_polvio++;
146 return;
147 }
148#endif /* IPSEC */
149#ifdef MAC
150 if (mac_inpcb_check_deliver(inp, n) != 0) {
151 m_freem(n);
152 return;
153 }
154#endif
155 opts = NULL;
156 if (inp->inp_flags & INP_CONTROLOPTS ||
157 inp->inp_socket->so_options & SO_TIMESTAMP)
158 ip6_savecontrol(inp, n, &opts);
159 m_adj(n, off + sizeof(struct udphdr));
160
161 so = inp->inp_socket;
162 SOCKBUF_LOCK(&so->so_rcv);
163 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
164 opts) == 0) {
165 SOCKBUF_UNLOCK(&so->so_rcv);
166 m_freem(n);
167 if (opts)
168 m_freem(opts);
169 UDPSTAT_INC(udps_fullsock);
170 } else
171 sorwakeup_locked(so);
172}
173
174int
175udp6_input(struct mbuf **mp, int *offp, int proto)
176{
177 INIT_VNET_INET(curvnet);
178 INIT_VNET_INET6(curvnet);
179 struct mbuf *m = *mp;
67
68#include "opt_inet.h"
69#include "opt_inet6.h"
70#include "opt_ipsec.h"
71#include "opt_mac.h"
72
73#include <sys/param.h>
74#include <sys/jail.h>
75#include <sys/kernel.h>
76#include <sys/lock.h>
77#include <sys/mbuf.h>
78#include <sys/priv.h>
79#include <sys/proc.h>
80#include <sys/protosw.h>
81#include <sys/signalvar.h>
82#include <sys/socket.h>
83#include <sys/socketvar.h>
84#include <sys/sx.h>
85#include <sys/sysctl.h>
86#include <sys/syslog.h>
87#include <sys/systm.h>
88#include <sys/vimage.h>
89
90#include <net/if.h>
91#include <net/if_types.h>
92#include <net/route.h>
93
94#include <netinet/in.h>
95#include <netinet/in_pcb.h>
96#include <netinet/in_systm.h>
97#include <netinet/in_var.h>
98#include <netinet/ip.h>
99#include <netinet/ip_icmp.h>
100#include <netinet/ip6.h>
101#include <netinet/icmp_var.h>
102#include <netinet/icmp6.h>
103#include <netinet/ip_var.h>
104#include <netinet/udp.h>
105#include <netinet/udp_var.h>
106#include <netinet/vinet.h>
107
108#include <netinet6/ip6protosw.h>
109#include <netinet6/ip6_var.h>
110#include <netinet6/in6_pcb.h>
111#include <netinet6/udp6_var.h>
112#include <netinet6/scope6_var.h>
113#include <netinet6/vinet6.h>
114
115#ifdef IPSEC
116#include <netipsec/ipsec.h>
117#include <netipsec/ipsec6.h>
118#endif /* IPSEC */
119
120#include <security/mac/mac_framework.h>
121
122/*
123 * UDP protocol implementation.
124 * Per RFC 768, August, 1980.
125 */
126
127extern struct protosw inetsw[];
128static void udp6_detach(struct socket *so);
129
130static void
131udp6_append(struct inpcb *inp, struct mbuf *n, int off,
132 struct sockaddr_in6 *fromsa)
133{
134 INIT_VNET_INET(inp->inp_vnet);
135 struct socket *so;
136 struct mbuf *opts;
137
138 INP_LOCK_ASSERT(inp);
139
140#ifdef IPSEC
141 /* Check AH/ESP integrity. */
142 if (ipsec6_in_reject(n, inp)) {
143 INIT_VNET_IPSEC(inp->inp_vnet);
144 m_freem(n);
145 V_ipsec6stat.in_polvio++;
146 return;
147 }
148#endif /* IPSEC */
149#ifdef MAC
150 if (mac_inpcb_check_deliver(inp, n) != 0) {
151 m_freem(n);
152 return;
153 }
154#endif
155 opts = NULL;
156 if (inp->inp_flags & INP_CONTROLOPTS ||
157 inp->inp_socket->so_options & SO_TIMESTAMP)
158 ip6_savecontrol(inp, n, &opts);
159 m_adj(n, off + sizeof(struct udphdr));
160
161 so = inp->inp_socket;
162 SOCKBUF_LOCK(&so->so_rcv);
163 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
164 opts) == 0) {
165 SOCKBUF_UNLOCK(&so->so_rcv);
166 m_freem(n);
167 if (opts)
168 m_freem(opts);
169 UDPSTAT_INC(udps_fullsock);
170 } else
171 sorwakeup_locked(so);
172}
173
174int
175udp6_input(struct mbuf **mp, int *offp, int proto)
176{
177 INIT_VNET_INET(curvnet);
178 INIT_VNET_INET6(curvnet);
179 struct mbuf *m = *mp;
180 struct ifnet *ifp;
180 struct ip6_hdr *ip6;
181 struct udphdr *uh;
182 struct inpcb *inp;
183 int off = *offp;
184 int plen, ulen;
185 struct sockaddr_in6 fromsa;
186
181 struct ip6_hdr *ip6;
182 struct udphdr *uh;
183 struct inpcb *inp;
184 int off = *offp;
185 int plen, ulen;
186 struct sockaddr_in6 fromsa;
187
188 ifp = m->m_pkthdr.rcvif;
187 ip6 = mtod(m, struct ip6_hdr *);
188
189 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
190 /* XXX send icmp6 host/port unreach? */
191 m_freem(m);
192 return (IPPROTO_DONE);
193 }
194
195#ifndef PULLDOWN_TEST
196 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
197 ip6 = mtod(m, struct ip6_hdr *);
198 uh = (struct udphdr *)((caddr_t)ip6 + off);
199#else
200 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
201 if (!uh)
202 return (IPPROTO_DONE);
203#endif
204
205 UDPSTAT_INC(udps_ipackets);
206
207 /*
208 * Destination port of 0 is illegal, based on RFC768.
209 */
210 if (uh->uh_dport == 0)
211 goto badunlocked;
212
213 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
214 ulen = ntohs((u_short)uh->uh_ulen);
215
216 if (plen != ulen) {
217 UDPSTAT_INC(udps_badlen);
218 goto badunlocked;
219 }
220
221 /*
222 * Checksum extended UDP header and data.
223 */
224 if (uh->uh_sum == 0) {
225 UDPSTAT_INC(udps_nosum);
226 goto badunlocked;
227 }
228 if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
229 UDPSTAT_INC(udps_badsum);
230 goto badunlocked;
231 }
232
233 /*
234 * Construct sockaddr format source address.
235 */
236 init_sin6(&fromsa, m);
237 fromsa.sin6_port = uh->uh_sport;
238
239 INP_INFO_RLOCK(&V_udbinfo);
240 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
241 struct inpcb *last;
189 ip6 = mtod(m, struct ip6_hdr *);
190
191 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
192 /* XXX send icmp6 host/port unreach? */
193 m_freem(m);
194 return (IPPROTO_DONE);
195 }
196
197#ifndef PULLDOWN_TEST
198 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
199 ip6 = mtod(m, struct ip6_hdr *);
200 uh = (struct udphdr *)((caddr_t)ip6 + off);
201#else
202 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
203 if (!uh)
204 return (IPPROTO_DONE);
205#endif
206
207 UDPSTAT_INC(udps_ipackets);
208
209 /*
210 * Destination port of 0 is illegal, based on RFC768.
211 */
212 if (uh->uh_dport == 0)
213 goto badunlocked;
214
215 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
216 ulen = ntohs((u_short)uh->uh_ulen);
217
218 if (plen != ulen) {
219 UDPSTAT_INC(udps_badlen);
220 goto badunlocked;
221 }
222
223 /*
224 * Checksum extended UDP header and data.
225 */
226 if (uh->uh_sum == 0) {
227 UDPSTAT_INC(udps_nosum);
228 goto badunlocked;
229 }
230 if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
231 UDPSTAT_INC(udps_badsum);
232 goto badunlocked;
233 }
234
235 /*
236 * Construct sockaddr format source address.
237 */
238 init_sin6(&fromsa, m);
239 fromsa.sin6_port = uh->uh_sport;
240
241 INP_INFO_RLOCK(&V_udbinfo);
242 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
243 struct inpcb *last;
244 struct ip6_moptions *imo;
242
243 /*
244 * In the event that laddr should be set to the link-local
245 * address (this happens in RIPng), the multicast address
246 * specified in the received packet will not match laddr. To
247 * handle this situation, matching is relaxed if the
248 * receiving interface is the same as one specified in the
249 * socket and if the destination multicast address matches
250 * one of the multicast groups specified in the socket.
251 */
252
253 /*
254 * KAME note: traditionally we dropped udpiphdr from mbuf
255 * here. We need udphdr for IPsec processing so we do that
256 * later.
257 */
258 last = NULL;
259 LIST_FOREACH(inp, &V_udb, inp_list) {
260 if ((inp->inp_vflag & INP_IPV6) == 0)
261 continue;
262 if (inp->inp_lport != uh->uh_dport)
263 continue;
245
246 /*
247 * In the event that laddr should be set to the link-local
248 * address (this happens in RIPng), the multicast address
249 * specified in the received packet will not match laddr. To
250 * handle this situation, matching is relaxed if the
251 * receiving interface is the same as one specified in the
252 * socket and if the destination multicast address matches
253 * one of the multicast groups specified in the socket.
254 */
255
256 /*
257 * KAME note: traditionally we dropped udpiphdr from mbuf
258 * here. We need udphdr for IPsec processing so we do that
259 * later.
260 */
261 last = NULL;
262 LIST_FOREACH(inp, &V_udb, inp_list) {
263 if ((inp->inp_vflag & INP_IPV6) == 0)
264 continue;
265 if (inp->inp_lport != uh->uh_dport)
266 continue;
264 /*
265 * XXX: Do not check source port of incoming datagram
266 * unless inp_connect() has been called to bind the
267 * fport part of the 4-tuple; the source could be
268 * trying to talk to us with an ephemeral port.
269 */
270 if (inp->inp_fport != 0 &&
271 inp->inp_fport != uh->uh_sport)
272 continue;
273 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
274 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
275 &ip6->ip6_dst))
276 continue;
277 }
278 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
279 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
280 &ip6->ip6_src) ||
281 inp->inp_fport != uh->uh_sport)
282 continue;
283 }
284
267 if (inp->inp_fport != 0 &&
268 inp->inp_fport != uh->uh_sport)
269 continue;
270 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
271 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
272 &ip6->ip6_dst))
273 continue;
274 }
275 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
276 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
277 &ip6->ip6_src) ||
278 inp->inp_fport != uh->uh_sport)
279 continue;
280 }
281
282 INP_RLOCK(inp);
283
284 /*
285 * Handle socket delivery policy for any-source
286 * and source-specific multicast. [RFC3678]
287 */
288 imo = inp->in6p_moptions;
289 if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
290 struct sockaddr_in6 mcaddr;
291 int blocked;
292
293 bzero(&mcaddr, sizeof(struct sockaddr_in6));
294 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
295 mcaddr.sin6_family = AF_INET6;
296 mcaddr.sin6_addr = ip6->ip6_dst;
297
298 blocked = im6o_mc_filter(imo, ifp,
299 (struct sockaddr *)&mcaddr,
300 (struct sockaddr *)&fromsa);
301 if (blocked != MCAST_PASS) {
302 if (blocked == MCAST_NOTGMEMBER)
303 IP6STAT_INC(ip6s_notmember);
304 if (blocked == MCAST_NOTSMEMBER ||
305 blocked == MCAST_MUTED)
306 UDPSTAT_INC(udps_filtermcast);
307 INP_RUNLOCK(inp);
308 continue;
309 }
310 }
285 if (last != NULL) {
286 struct mbuf *n;
287
288 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
289 INP_RLOCK(last);
290 if (last->inp_ppcb != NULL) {
291 /*
292 * Engage the tunneling
293 * protocol we will have to
294 * leave the info_lock up,
295 * since we are hunting
296 * through multiple UDP's.
297 *
298 */
299 udp_tun_func_t tunnel_func;
300
301 tunnel_func = (udp_tun_func_t)last->inp_ppcb;
302 tunnel_func(n, off, last);
303 INP_RUNLOCK(last);
304 } else {
305 udp6_append(last, n, off, &fromsa);
306 INP_RUNLOCK(last);
307 }
308 }
309 }
310 last = inp;
311 /*
312 * Don't look for additional matches if this one does
313 * not have either the SO_REUSEPORT or SO_REUSEADDR
314 * socket options set. This heuristic avoids
315 * searching through all pcbs in the common case of a
316 * non-shared port. It assumes that an application
317 * will never clear these options after setting them.
318 */
319 if ((last->inp_socket->so_options &
320 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
321 break;
322 }
323
324 if (last == NULL) {
325 /*
326 * No matching pcb found; discard datagram. (No need
327 * to send an ICMP Port Unreachable for a broadcast
328 * or multicast datgram.)
329 */
330 UDPSTAT_INC(udps_noport);
331 UDPSTAT_INC(udps_noportmcast);
332 goto badheadlocked;
333 }
334 INP_RLOCK(last);
335 INP_INFO_RUNLOCK(&V_udbinfo);
336 if (last->inp_ppcb != NULL) {
337 /*
338 * Engage the tunneling protocol.
339 */
340 udp_tun_func_t tunnel_func;
341
342 tunnel_func = (udp_tun_func_t)inp->inp_ppcb;
343 tunnel_func(m, off, last);
344 INP_RUNLOCK(last);
345 return (IPPROTO_DONE);
346 }
347 udp6_append(last, m, off, &fromsa);
348 INP_RUNLOCK(last);
349 return (IPPROTO_DONE);
350 }
351 /*
352 * Locate pcb for datagram.
353 */
354 inp = in6_pcblookup_hash(&V_udbinfo, &ip6->ip6_src, uh->uh_sport,
355 &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
356 if (inp == NULL) {
357 if (udp_log_in_vain) {
358 char ip6bufs[INET6_ADDRSTRLEN];
359 char ip6bufd[INET6_ADDRSTRLEN];
360
361 log(LOG_INFO,
362 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
363 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
364 ntohs(uh->uh_dport),
365 ip6_sprintf(ip6bufs, &ip6->ip6_src),
366 ntohs(uh->uh_sport));
367 }
368 UDPSTAT_INC(udps_noport);
369 if (m->m_flags & M_MCAST) {
370 printf("UDP6: M_MCAST is set in a unicast packet.\n");
371 UDPSTAT_INC(udps_noportmcast);
372 goto badheadlocked;
373 }
374 INP_INFO_RUNLOCK(&V_udbinfo);
375 if (V_udp_blackhole)
376 goto badunlocked;
377 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
378 goto badunlocked;
379 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
380 return (IPPROTO_DONE);
381 }
382 INP_RLOCK(inp);
383 INP_INFO_RUNLOCK(&V_udbinfo);
384 if (inp->inp_ppcb != NULL) {
385 /*
386 * Engage the tunneling protocol.
387 */
388 udp_tun_func_t tunnel_func;
389
390 tunnel_func = (udp_tun_func_t)inp->inp_ppcb;
391 tunnel_func(m, off, inp);
392 INP_RUNLOCK(inp);
393 return (IPPROTO_DONE);
394 }
395 udp6_append(inp, m, off, &fromsa);
396 INP_RUNLOCK(inp);
397 return (IPPROTO_DONE);
398
399badheadlocked:
311 if (last != NULL) {
312 struct mbuf *n;
313
314 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
315 INP_RLOCK(last);
316 if (last->inp_ppcb != NULL) {
317 /*
318 * Engage the tunneling
319 * protocol we will have to
320 * leave the info_lock up,
321 * since we are hunting
322 * through multiple UDP's.
323 *
324 */
325 udp_tun_func_t tunnel_func;
326
327 tunnel_func = (udp_tun_func_t)last->inp_ppcb;
328 tunnel_func(n, off, last);
329 INP_RUNLOCK(last);
330 } else {
331 udp6_append(last, n, off, &fromsa);
332 INP_RUNLOCK(last);
333 }
334 }
335 }
336 last = inp;
337 /*
338 * Don't look for additional matches if this one does
339 * not have either the SO_REUSEPORT or SO_REUSEADDR
340 * socket options set. This heuristic avoids
341 * searching through all pcbs in the common case of a
342 * non-shared port. It assumes that an application
343 * will never clear these options after setting them.
344 */
345 if ((last->inp_socket->so_options &
346 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
347 break;
348 }
349
350 if (last == NULL) {
351 /*
352 * No matching pcb found; discard datagram. (No need
353 * to send an ICMP Port Unreachable for a broadcast
354 * or multicast datgram.)
355 */
356 UDPSTAT_INC(udps_noport);
357 UDPSTAT_INC(udps_noportmcast);
358 goto badheadlocked;
359 }
360 INP_RLOCK(last);
361 INP_INFO_RUNLOCK(&V_udbinfo);
362 if (last->inp_ppcb != NULL) {
363 /*
364 * Engage the tunneling protocol.
365 */
366 udp_tun_func_t tunnel_func;
367
368 tunnel_func = (udp_tun_func_t)inp->inp_ppcb;
369 tunnel_func(m, off, last);
370 INP_RUNLOCK(last);
371 return (IPPROTO_DONE);
372 }
373 udp6_append(last, m, off, &fromsa);
374 INP_RUNLOCK(last);
375 return (IPPROTO_DONE);
376 }
377 /*
378 * Locate pcb for datagram.
379 */
380 inp = in6_pcblookup_hash(&V_udbinfo, &ip6->ip6_src, uh->uh_sport,
381 &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
382 if (inp == NULL) {
383 if (udp_log_in_vain) {
384 char ip6bufs[INET6_ADDRSTRLEN];
385 char ip6bufd[INET6_ADDRSTRLEN];
386
387 log(LOG_INFO,
388 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
389 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
390 ntohs(uh->uh_dport),
391 ip6_sprintf(ip6bufs, &ip6->ip6_src),
392 ntohs(uh->uh_sport));
393 }
394 UDPSTAT_INC(udps_noport);
395 if (m->m_flags & M_MCAST) {
396 printf("UDP6: M_MCAST is set in a unicast packet.\n");
397 UDPSTAT_INC(udps_noportmcast);
398 goto badheadlocked;
399 }
400 INP_INFO_RUNLOCK(&V_udbinfo);
401 if (V_udp_blackhole)
402 goto badunlocked;
403 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
404 goto badunlocked;
405 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
406 return (IPPROTO_DONE);
407 }
408 INP_RLOCK(inp);
409 INP_INFO_RUNLOCK(&V_udbinfo);
410 if (inp->inp_ppcb != NULL) {
411 /*
412 * Engage the tunneling protocol.
413 */
414 udp_tun_func_t tunnel_func;
415
416 tunnel_func = (udp_tun_func_t)inp->inp_ppcb;
417 tunnel_func(m, off, inp);
418 INP_RUNLOCK(inp);
419 return (IPPROTO_DONE);
420 }
421 udp6_append(inp, m, off, &fromsa);
422 INP_RUNLOCK(inp);
423 return (IPPROTO_DONE);
424
425badheadlocked:
426 if (inp)
427 INP_RUNLOCK(inp);
400 INP_INFO_RUNLOCK(&V_udbinfo);
401badunlocked:
402 if (m)
403 m_freem(m);
404 return (IPPROTO_DONE);
405}
406
407void
408udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
409{
410 INIT_VNET_INET(curvnet);
411 struct udphdr uh;
412 struct ip6_hdr *ip6;
413 struct mbuf *m;
414 int off = 0;
415 struct ip6ctlparam *ip6cp = NULL;
416 const struct sockaddr_in6 *sa6_src = NULL;
417 void *cmdarg;
418 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
419 struct udp_portonly {
420 u_int16_t uh_sport;
421 u_int16_t uh_dport;
422 } *uhp;
423
424 if (sa->sa_family != AF_INET6 ||
425 sa->sa_len != sizeof(struct sockaddr_in6))
426 return;
427
428 if ((unsigned)cmd >= PRC_NCMDS)
429 return;
430 if (PRC_IS_REDIRECT(cmd))
431 notify = in6_rtchange, d = NULL;
432 else if (cmd == PRC_HOSTDEAD)
433 d = NULL;
434 else if (inet6ctlerrmap[cmd] == 0)
435 return;
436
437 /* if the parameter is from icmp6, decode it. */
438 if (d != NULL) {
439 ip6cp = (struct ip6ctlparam *)d;
440 m = ip6cp->ip6c_m;
441 ip6 = ip6cp->ip6c_ip6;
442 off = ip6cp->ip6c_off;
443 cmdarg = ip6cp->ip6c_cmdarg;
444 sa6_src = ip6cp->ip6c_src;
445 } else {
446 m = NULL;
447 ip6 = NULL;
448 cmdarg = NULL;
449 sa6_src = &sa6_any;
450 }
451
452 if (ip6) {
453 /*
454 * XXX: We assume that when IPV6 is non NULL,
455 * M and OFF are valid.
456 */
457
458 /* Check if we can safely examine src and dst ports. */
459 if (m->m_pkthdr.len < off + sizeof(*uhp))
460 return;
461
462 bzero(&uh, sizeof(uh));
463 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
464
465 (void) in6_pcbnotify(&V_udbinfo, sa, uh.uh_dport,
466 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
467 cmdarg, notify);
468 } else
469 (void) in6_pcbnotify(&V_udbinfo, sa, 0,
470 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
471}
472
473static int
474udp6_getcred(SYSCTL_HANDLER_ARGS)
475{
476 INIT_VNET_INET(curvnet);
477 INIT_VNET_INET6(curvnet);
478 struct xucred xuc;
479 struct sockaddr_in6 addrs[2];
480 struct inpcb *inp;
481 int error;
482
483 error = priv_check(req->td, PRIV_NETINET_GETCRED);
484 if (error)
485 return (error);
486
487 if (req->newlen != sizeof(addrs))
488 return (EINVAL);
489 if (req->oldlen != sizeof(struct xucred))
490 return (EINVAL);
491 error = SYSCTL_IN(req, addrs, sizeof(addrs));
492 if (error)
493 return (error);
494 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
495 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
496 return (error);
497 }
498 INP_INFO_RLOCK(&V_udbinfo);
499 inp = in6_pcblookup_hash(&V_udbinfo, &addrs[1].sin6_addr,
500 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 1,
501 NULL);
502 if (inp != NULL) {
503 INP_RLOCK(inp);
504 INP_INFO_RUNLOCK(&V_udbinfo);
505 if (inp->inp_socket == NULL)
506 error = ENOENT;
507 if (error == 0)
508 error = cr_canseesocket(req->td->td_ucred,
509 inp->inp_socket);
510 if (error == 0)
511 cru2x(inp->inp_cred, &xuc);
512 INP_RUNLOCK(inp);
513 } else {
514 INP_INFO_RUNLOCK(&V_udbinfo);
515 error = ENOENT;
516 }
517 if (error == 0)
518 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
519 return (error);
520}
521
522SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
523 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
524
525static int
526udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
527 struct mbuf *control, struct thread *td)
528{
529 INIT_VNET_INET(curvnet);
530 INIT_VNET_INET6(curvnet);
531 u_int32_t ulen = m->m_pkthdr.len;
532 u_int32_t plen = sizeof(struct udphdr) + ulen;
533 struct ip6_hdr *ip6;
534 struct udphdr *udp6;
535 struct in6_addr *laddr, *faddr;
536 struct sockaddr_in6 *sin6 = NULL;
537 struct ifnet *oifp = NULL;
538 int scope_ambiguous = 0;
539 u_short fport;
540 int error = 0;
541 struct ip6_pktopts *optp, opt;
542 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
543 int flags;
544 struct sockaddr_in6 tmp;
545
546 INP_WLOCK_ASSERT(inp);
547
548 if (addr6) {
549 /* addr6 has been validated in udp6_send(). */
550 sin6 = (struct sockaddr_in6 *)addr6;
551
552 /* protect *sin6 from overwrites */
553 tmp = *sin6;
554 sin6 = &tmp;
555
556 /*
557 * Application should provide a proper zone ID or the use of
558 * default zone IDs should be enabled. Unfortunately, some
559 * applications do not behave as it should, so we need a
560 * workaround. Even if an appropriate ID is not determined,
561 * we'll see if we can determine the outgoing interface. If we
562 * can, determine the zone ID based on the interface below.
563 */
564 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
565 scope_ambiguous = 1;
566 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
567 return (error);
568 }
569
570 if (control) {
571 if ((error = ip6_setpktopts(control, &opt,
572 inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
573 goto release;
574 optp = &opt;
575 } else
576 optp = inp->in6p_outputopts;
577
578 if (sin6) {
579 faddr = &sin6->sin6_addr;
580
581 /*
582 * IPv4 version of udp_output calls in_pcbconnect in this case,
583 * which needs splnet and affects performance.
584 * Since we saw no essential reason for calling in_pcbconnect,
585 * we get rid of such kind of logic, and call in6_selectsrc
586 * and in6_pcbsetport in order to fill in the local address
587 * and the local port.
588 */
589 if (sin6->sin6_port == 0) {
590 error = EADDRNOTAVAIL;
591 goto release;
592 }
593
594 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
595 /* how about ::ffff:0.0.0.0 case? */
596 error = EISCONN;
597 goto release;
598 }
599
600 fport = sin6->sin6_port; /* allow 0 port */
601
602 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
603 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
604 /*
605 * I believe we should explicitly discard the
606 * packet when mapped addresses are disabled,
607 * rather than send the packet as an IPv6 one.
608 * If we chose the latter approach, the packet
609 * might be sent out on the wire based on the
610 * default route, the situation which we'd
611 * probably want to avoid.
612 * (20010421 jinmei@kame.net)
613 */
614 error = EINVAL;
615 goto release;
616 }
617 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
618 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
619 /*
620 * when remote addr is an IPv4-mapped address,
621 * local addr should not be an IPv6 address,
622 * since you cannot determine how to map IPv6
623 * source address to IPv4.
624 */
625 error = EINVAL;
626 goto release;
627 }
628
629 af = AF_INET;
630 }
631
632 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
633 laddr = in6_selectsrc(sin6, optp, inp, NULL,
634 td->td_ucred, &oifp, &error);
635 if (oifp && scope_ambiguous &&
636 (error = in6_setscope(&sin6->sin6_addr,
637 oifp, NULL))) {
638 goto release;
639 }
640 } else
641 laddr = &inp->in6p_laddr; /* XXX */
642 if (laddr == NULL) {
643 if (error == 0)
644 error = EADDRNOTAVAIL;
645 goto release;
646 }
647 if (inp->inp_lport == 0 &&
648 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0)
649 goto release;
650 } else {
651 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
652 error = ENOTCONN;
653 goto release;
654 }
655 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
656 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
657 /*
658 * XXX: this case would happen when the
659 * application sets the V6ONLY flag after
660 * connecting the foreign address.
661 * Such applications should be fixed,
662 * so we bark here.
663 */
664 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
665 "option was set for a connected socket\n");
666 error = EINVAL;
667 goto release;
668 } else
669 af = AF_INET;
670 }
671 laddr = &inp->in6p_laddr;
672 faddr = &inp->in6p_faddr;
673 fport = inp->inp_fport;
674 }
675
676 if (af == AF_INET)
677 hlen = sizeof(struct ip);
678
679 /*
680 * Calculate data length and get a mbuf
681 * for UDP and IP6 headers.
682 */
683 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
684 if (m == 0) {
685 error = ENOBUFS;
686 goto release;
687 }
688
689 /*
690 * Stuff checksum and output datagram.
691 */
692 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
693 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
694 udp6->uh_dport = fport;
695 if (plen <= 0xffff)
696 udp6->uh_ulen = htons((u_short)plen);
697 else
698 udp6->uh_ulen = 0;
699 udp6->uh_sum = 0;
700
701 switch (af) {
702 case AF_INET6:
703 ip6 = mtod(m, struct ip6_hdr *);
704 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
705 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
706 ip6->ip6_vfc |= IPV6_VERSION;
707#if 0 /* ip6_plen will be filled in ip6_output. */
708 ip6->ip6_plen = htons((u_short)plen);
709#endif
710 ip6->ip6_nxt = IPPROTO_UDP;
711 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
712 ip6->ip6_src = *laddr;
713 ip6->ip6_dst = *faddr;
714
715 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
716 sizeof(struct ip6_hdr), plen)) == 0) {
717 udp6->uh_sum = 0xffff;
718 }
719
720 flags = 0;
721
722 UDPSTAT_INC(udps_opackets);
723 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
724 NULL, inp);
725 break;
726 case AF_INET:
727 error = EAFNOSUPPORT;
728 goto release;
729 }
730 goto releaseopt;
731
732release:
733 m_freem(m);
734
735releaseopt:
736 if (control) {
737 ip6_clearpktopts(&opt, -1);
738 m_freem(control);
739 }
740 return (error);
741}
742
743static void
744udp6_abort(struct socket *so)
745{
746 INIT_VNET_INET(so->so_vnet);
747 struct inpcb *inp;
748
749 inp = sotoinpcb(so);
750 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
751
752#ifdef INET
753 if (inp->inp_vflag & INP_IPV4) {
754 struct pr_usrreqs *pru;
755
756 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
757 (*pru->pru_abort)(so);
758 return;
759 }
760#endif
761
762 INP_INFO_WLOCK(&V_udbinfo);
763 INP_WLOCK(inp);
764 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
765 in6_pcbdisconnect(inp);
766 inp->in6p_laddr = in6addr_any;
767 soisdisconnected(so);
768 }
769 INP_WUNLOCK(inp);
770 INP_INFO_WUNLOCK(&V_udbinfo);
771}
772
773static int
774udp6_attach(struct socket *so, int proto, struct thread *td)
775{
776 INIT_VNET_INET(so->so_vnet);
777 struct inpcb *inp;
778 int error;
779
780 inp = sotoinpcb(so);
781 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
782
783 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
784 error = soreserve(so, udp_sendspace, udp_recvspace);
785 if (error)
786 return (error);
787 }
788 INP_INFO_WLOCK(&V_udbinfo);
789 error = in_pcballoc(so, &V_udbinfo);
790 if (error) {
791 INP_INFO_WUNLOCK(&V_udbinfo);
792 return (error);
793 }
794 inp = (struct inpcb *)so->so_pcb;
795 INP_INFO_WUNLOCK(&V_udbinfo);
796 inp->inp_vflag |= INP_IPV6;
797 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
798 inp->inp_vflag |= INP_IPV4;
799 inp->in6p_hops = -1; /* use kernel default */
800 inp->in6p_cksum = -1; /* just to be sure */
801 /*
802 * XXX: ugly!!
803 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
804 * because the socket may be bound to an IPv6 wildcard address,
805 * which may match an IPv4-mapped IPv6 address.
806 */
807 inp->inp_ip_ttl = V_ip_defttl;
808 INP_WUNLOCK(inp);
809 return (0);
810}
811
812static int
813udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
814{
815 INIT_VNET_INET(so->so_vnet);
816 struct inpcb *inp;
817 int error;
818
819 inp = sotoinpcb(so);
820 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
821
822 INP_INFO_WLOCK(&V_udbinfo);
823 INP_WLOCK(inp);
824 inp->inp_vflag &= ~INP_IPV4;
825 inp->inp_vflag |= INP_IPV6;
826 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
827 struct sockaddr_in6 *sin6_p;
828
829 sin6_p = (struct sockaddr_in6 *)nam;
830
831 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
832 inp->inp_vflag |= INP_IPV4;
833 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
834 struct sockaddr_in sin;
835
836 in6_sin6_2_sin(&sin, sin6_p);
837 inp->inp_vflag |= INP_IPV4;
838 inp->inp_vflag &= ~INP_IPV6;
839 error = in_pcbbind(inp, (struct sockaddr *)&sin,
840 td->td_ucred);
841 goto out;
842 }
843 }
844
845 error = in6_pcbbind(inp, nam, td->td_ucred);
846out:
847 INP_WUNLOCK(inp);
848 INP_INFO_WUNLOCK(&V_udbinfo);
849 return (error);
850}
851
852static void
853udp6_close(struct socket *so)
854{
855 INIT_VNET_INET(so->so_vnet);
856 struct inpcb *inp;
857
858 inp = sotoinpcb(so);
859 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
860
861#ifdef INET
862 if (inp->inp_vflag & INP_IPV4) {
863 struct pr_usrreqs *pru;
864
865 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
866 (*pru->pru_disconnect)(so);
867 return;
868 }
869#endif
870 INP_INFO_WLOCK(&V_udbinfo);
871 INP_WLOCK(inp);
872 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
873 in6_pcbdisconnect(inp);
874 inp->in6p_laddr = in6addr_any;
875 soisdisconnected(so);
876 }
877 INP_WUNLOCK(inp);
878 INP_INFO_WUNLOCK(&V_udbinfo);
879}
880
881static int
882udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
883{
884 INIT_VNET_INET(so->so_vnet);
885 struct inpcb *inp;
886 struct sockaddr_in6 *sin6;
887 int error;
888
889 inp = sotoinpcb(so);
890 sin6 = (struct sockaddr_in6 *)nam;
891 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
892
893 INP_INFO_WLOCK(&V_udbinfo);
894 INP_WLOCK(inp);
895 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
896 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
897 struct sockaddr_in sin;
898
899 if (inp->inp_faddr.s_addr != INADDR_ANY) {
900 error = EISCONN;
901 goto out;
902 }
903 in6_sin6_2_sin(&sin, sin6);
904 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
905 if (error != 0)
906 goto out;
907 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
908 td->td_ucred);
909 if (error == 0) {
910 inp->inp_vflag |= INP_IPV4;
911 inp->inp_vflag &= ~INP_IPV6;
912 soisconnected(so);
913 }
914 goto out;
915 }
916 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
917 error = EISCONN;
918 goto out;
919 }
920 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
921 if (error != 0)
922 goto out;
923 error = in6_pcbconnect(inp, nam, td->td_ucred);
924 if (error == 0) {
925 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
926 /* should be non mapped addr */
927 inp->inp_vflag &= ~INP_IPV4;
928 inp->inp_vflag |= INP_IPV6;
929 }
930 soisconnected(so);
931 }
932out:
933 INP_WUNLOCK(inp);
934 INP_INFO_WUNLOCK(&V_udbinfo);
935 return (error);
936}
937
938static void
939udp6_detach(struct socket *so)
940{
941 INIT_VNET_INET(so->so_vnet);
942 struct inpcb *inp;
943
944 inp = sotoinpcb(so);
945 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
946
947 INP_INFO_WLOCK(&V_udbinfo);
948 INP_WLOCK(inp);
949 in_pcbdetach(inp);
950 in_pcbfree(inp);
951 INP_INFO_WUNLOCK(&V_udbinfo);
952}
953
954static int
955udp6_disconnect(struct socket *so)
956{
957 INIT_VNET_INET(so->so_vnet);
958 struct inpcb *inp;
959 int error;
960
961 inp = sotoinpcb(so);
962 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
963
964 INP_INFO_WLOCK(&V_udbinfo);
965 INP_WLOCK(inp);
966
967#ifdef INET
968 if (inp->inp_vflag & INP_IPV4) {
969 struct pr_usrreqs *pru;
970
971 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
972 error = (*pru->pru_disconnect)(so);
973 goto out;
974 }
975#endif
976
977 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
978 error = ENOTCONN;
979 goto out;
980 }
981
982 in6_pcbdisconnect(inp);
983 inp->in6p_laddr = in6addr_any;
984 SOCK_LOCK(so);
985 so->so_state &= ~SS_ISCONNECTED; /* XXX */
986 SOCK_UNLOCK(so);
987out:
988 INP_WUNLOCK(inp);
989 INP_INFO_WUNLOCK(&V_udbinfo);
990 return (0);
991}
992
993static int
994udp6_send(struct socket *so, int flags, struct mbuf *m,
995 struct sockaddr *addr, struct mbuf *control, struct thread *td)
996{
997 INIT_VNET_INET(so->so_vnet);
998 struct inpcb *inp;
999 int error = 0;
1000
1001 inp = sotoinpcb(so);
1002 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1003
1004 INP_INFO_WLOCK(&V_udbinfo);
1005 INP_WLOCK(inp);
1006 if (addr) {
1007 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1008 error = EINVAL;
1009 goto bad;
1010 }
1011 if (addr->sa_family != AF_INET6) {
1012 error = EAFNOSUPPORT;
1013 goto bad;
1014 }
1015 }
1016
1017#ifdef INET
1018 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1019 int hasv4addr;
1020 struct sockaddr_in6 *sin6 = 0;
1021
1022 if (addr == 0)
1023 hasv4addr = (inp->inp_vflag & INP_IPV4);
1024 else {
1025 sin6 = (struct sockaddr_in6 *)addr;
1026 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1027 ? 1 : 0;
1028 }
1029 if (hasv4addr) {
1030 struct pr_usrreqs *pru;
1031
1032 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1033 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
1034 /*
1035 * When remote addr is IPv4-mapped address,
1036 * local addr should not be an IPv6 address;
1037 * since you cannot determine how to map IPv6
1038 * source address to IPv4.
1039 */
1040 error = EINVAL;
1041 goto out;
1042 }
1043
1044 /*
1045 * XXXRW: We release UDP-layer locks before calling
1046 * udp_send() in order to avoid recursion. However,
1047 * this does mean there is a short window where inp's
1048 * fields are unstable. Could this lead to a
1049 * potential race in which the factors causing us to
1050 * select the UDPv4 output routine are invalidated?
1051 */
1052 INP_WUNLOCK(inp);
1053 INP_INFO_WUNLOCK(&V_udbinfo);
1054 if (sin6)
1055 in6_sin6_2_sin_in_sock(addr);
1056 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1057 /* addr will just be freed in sendit(). */
1058 return ((*pru->pru_send)(so, flags, m, addr, control,
1059 td));
1060 }
1061 }
1062#endif
1063#ifdef MAC
1064 mac_inpcb_create_mbuf(inp, m);
1065#endif
1066 error = udp6_output(inp, m, addr, control, td);
1067out:
1068 INP_WUNLOCK(inp);
1069 INP_INFO_WUNLOCK(&V_udbinfo);
1070 return (error);
1071
1072bad:
1073 INP_WUNLOCK(inp);
1074 INP_INFO_WUNLOCK(&V_udbinfo);
1075 m_freem(m);
1076 return (error);
1077}
1078
1079struct pr_usrreqs udp6_usrreqs = {
1080 .pru_abort = udp6_abort,
1081 .pru_attach = udp6_attach,
1082 .pru_bind = udp6_bind,
1083 .pru_connect = udp6_connect,
1084 .pru_control = in6_control,
1085 .pru_detach = udp6_detach,
1086 .pru_disconnect = udp6_disconnect,
1087 .pru_peeraddr = in6_mapped_peeraddr,
1088 .pru_send = udp6_send,
1089 .pru_shutdown = udp_shutdown,
1090 .pru_sockaddr = in6_mapped_sockaddr,
1091 .pru_soreceive = soreceive_dgram,
1092 .pru_sosend = sosend_dgram,
1093 .pru_sosetlabel = in_pcbsosetlabel,
1094 .pru_close = udp6_close
1095};
428 INP_INFO_RUNLOCK(&V_udbinfo);
429badunlocked:
430 if (m)
431 m_freem(m);
432 return (IPPROTO_DONE);
433}
434
435void
436udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
437{
438 INIT_VNET_INET(curvnet);
439 struct udphdr uh;
440 struct ip6_hdr *ip6;
441 struct mbuf *m;
442 int off = 0;
443 struct ip6ctlparam *ip6cp = NULL;
444 const struct sockaddr_in6 *sa6_src = NULL;
445 void *cmdarg;
446 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
447 struct udp_portonly {
448 u_int16_t uh_sport;
449 u_int16_t uh_dport;
450 } *uhp;
451
452 if (sa->sa_family != AF_INET6 ||
453 sa->sa_len != sizeof(struct sockaddr_in6))
454 return;
455
456 if ((unsigned)cmd >= PRC_NCMDS)
457 return;
458 if (PRC_IS_REDIRECT(cmd))
459 notify = in6_rtchange, d = NULL;
460 else if (cmd == PRC_HOSTDEAD)
461 d = NULL;
462 else if (inet6ctlerrmap[cmd] == 0)
463 return;
464
465 /* if the parameter is from icmp6, decode it. */
466 if (d != NULL) {
467 ip6cp = (struct ip6ctlparam *)d;
468 m = ip6cp->ip6c_m;
469 ip6 = ip6cp->ip6c_ip6;
470 off = ip6cp->ip6c_off;
471 cmdarg = ip6cp->ip6c_cmdarg;
472 sa6_src = ip6cp->ip6c_src;
473 } else {
474 m = NULL;
475 ip6 = NULL;
476 cmdarg = NULL;
477 sa6_src = &sa6_any;
478 }
479
480 if (ip6) {
481 /*
482 * XXX: We assume that when IPV6 is non NULL,
483 * M and OFF are valid.
484 */
485
486 /* Check if we can safely examine src and dst ports. */
487 if (m->m_pkthdr.len < off + sizeof(*uhp))
488 return;
489
490 bzero(&uh, sizeof(uh));
491 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
492
493 (void) in6_pcbnotify(&V_udbinfo, sa, uh.uh_dport,
494 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
495 cmdarg, notify);
496 } else
497 (void) in6_pcbnotify(&V_udbinfo, sa, 0,
498 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
499}
500
501static int
502udp6_getcred(SYSCTL_HANDLER_ARGS)
503{
504 INIT_VNET_INET(curvnet);
505 INIT_VNET_INET6(curvnet);
506 struct xucred xuc;
507 struct sockaddr_in6 addrs[2];
508 struct inpcb *inp;
509 int error;
510
511 error = priv_check(req->td, PRIV_NETINET_GETCRED);
512 if (error)
513 return (error);
514
515 if (req->newlen != sizeof(addrs))
516 return (EINVAL);
517 if (req->oldlen != sizeof(struct xucred))
518 return (EINVAL);
519 error = SYSCTL_IN(req, addrs, sizeof(addrs));
520 if (error)
521 return (error);
522 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
523 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
524 return (error);
525 }
526 INP_INFO_RLOCK(&V_udbinfo);
527 inp = in6_pcblookup_hash(&V_udbinfo, &addrs[1].sin6_addr,
528 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 1,
529 NULL);
530 if (inp != NULL) {
531 INP_RLOCK(inp);
532 INP_INFO_RUNLOCK(&V_udbinfo);
533 if (inp->inp_socket == NULL)
534 error = ENOENT;
535 if (error == 0)
536 error = cr_canseesocket(req->td->td_ucred,
537 inp->inp_socket);
538 if (error == 0)
539 cru2x(inp->inp_cred, &xuc);
540 INP_RUNLOCK(inp);
541 } else {
542 INP_INFO_RUNLOCK(&V_udbinfo);
543 error = ENOENT;
544 }
545 if (error == 0)
546 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
547 return (error);
548}
549
550SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
551 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
552
553static int
554udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
555 struct mbuf *control, struct thread *td)
556{
557 INIT_VNET_INET(curvnet);
558 INIT_VNET_INET6(curvnet);
559 u_int32_t ulen = m->m_pkthdr.len;
560 u_int32_t plen = sizeof(struct udphdr) + ulen;
561 struct ip6_hdr *ip6;
562 struct udphdr *udp6;
563 struct in6_addr *laddr, *faddr;
564 struct sockaddr_in6 *sin6 = NULL;
565 struct ifnet *oifp = NULL;
566 int scope_ambiguous = 0;
567 u_short fport;
568 int error = 0;
569 struct ip6_pktopts *optp, opt;
570 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
571 int flags;
572 struct sockaddr_in6 tmp;
573
574 INP_WLOCK_ASSERT(inp);
575
576 if (addr6) {
577 /* addr6 has been validated in udp6_send(). */
578 sin6 = (struct sockaddr_in6 *)addr6;
579
580 /* protect *sin6 from overwrites */
581 tmp = *sin6;
582 sin6 = &tmp;
583
584 /*
585 * Application should provide a proper zone ID or the use of
586 * default zone IDs should be enabled. Unfortunately, some
587 * applications do not behave as it should, so we need a
588 * workaround. Even if an appropriate ID is not determined,
589 * we'll see if we can determine the outgoing interface. If we
590 * can, determine the zone ID based on the interface below.
591 */
592 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
593 scope_ambiguous = 1;
594 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
595 return (error);
596 }
597
598 if (control) {
599 if ((error = ip6_setpktopts(control, &opt,
600 inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
601 goto release;
602 optp = &opt;
603 } else
604 optp = inp->in6p_outputopts;
605
606 if (sin6) {
607 faddr = &sin6->sin6_addr;
608
609 /*
610 * IPv4 version of udp_output calls in_pcbconnect in this case,
611 * which needs splnet and affects performance.
612 * Since we saw no essential reason for calling in_pcbconnect,
613 * we get rid of such kind of logic, and call in6_selectsrc
614 * and in6_pcbsetport in order to fill in the local address
615 * and the local port.
616 */
617 if (sin6->sin6_port == 0) {
618 error = EADDRNOTAVAIL;
619 goto release;
620 }
621
622 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
623 /* how about ::ffff:0.0.0.0 case? */
624 error = EISCONN;
625 goto release;
626 }
627
628 fport = sin6->sin6_port; /* allow 0 port */
629
630 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
631 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
632 /*
633 * I believe we should explicitly discard the
634 * packet when mapped addresses are disabled,
635 * rather than send the packet as an IPv6 one.
636 * If we chose the latter approach, the packet
637 * might be sent out on the wire based on the
638 * default route, the situation which we'd
639 * probably want to avoid.
640 * (20010421 jinmei@kame.net)
641 */
642 error = EINVAL;
643 goto release;
644 }
645 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
646 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
647 /*
648 * when remote addr is an IPv4-mapped address,
649 * local addr should not be an IPv6 address,
650 * since you cannot determine how to map IPv6
651 * source address to IPv4.
652 */
653 error = EINVAL;
654 goto release;
655 }
656
657 af = AF_INET;
658 }
659
660 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
661 laddr = in6_selectsrc(sin6, optp, inp, NULL,
662 td->td_ucred, &oifp, &error);
663 if (oifp && scope_ambiguous &&
664 (error = in6_setscope(&sin6->sin6_addr,
665 oifp, NULL))) {
666 goto release;
667 }
668 } else
669 laddr = &inp->in6p_laddr; /* XXX */
670 if (laddr == NULL) {
671 if (error == 0)
672 error = EADDRNOTAVAIL;
673 goto release;
674 }
675 if (inp->inp_lport == 0 &&
676 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0)
677 goto release;
678 } else {
679 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
680 error = ENOTCONN;
681 goto release;
682 }
683 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
684 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
685 /*
686 * XXX: this case would happen when the
687 * application sets the V6ONLY flag after
688 * connecting the foreign address.
689 * Such applications should be fixed,
690 * so we bark here.
691 */
692 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
693 "option was set for a connected socket\n");
694 error = EINVAL;
695 goto release;
696 } else
697 af = AF_INET;
698 }
699 laddr = &inp->in6p_laddr;
700 faddr = &inp->in6p_faddr;
701 fport = inp->inp_fport;
702 }
703
704 if (af == AF_INET)
705 hlen = sizeof(struct ip);
706
707 /*
708 * Calculate data length and get a mbuf
709 * for UDP and IP6 headers.
710 */
711 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
712 if (m == 0) {
713 error = ENOBUFS;
714 goto release;
715 }
716
717 /*
718 * Stuff checksum and output datagram.
719 */
720 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
721 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
722 udp6->uh_dport = fport;
723 if (plen <= 0xffff)
724 udp6->uh_ulen = htons((u_short)plen);
725 else
726 udp6->uh_ulen = 0;
727 udp6->uh_sum = 0;
728
729 switch (af) {
730 case AF_INET6:
731 ip6 = mtod(m, struct ip6_hdr *);
732 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
733 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
734 ip6->ip6_vfc |= IPV6_VERSION;
735#if 0 /* ip6_plen will be filled in ip6_output. */
736 ip6->ip6_plen = htons((u_short)plen);
737#endif
738 ip6->ip6_nxt = IPPROTO_UDP;
739 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
740 ip6->ip6_src = *laddr;
741 ip6->ip6_dst = *faddr;
742
743 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
744 sizeof(struct ip6_hdr), plen)) == 0) {
745 udp6->uh_sum = 0xffff;
746 }
747
748 flags = 0;
749
750 UDPSTAT_INC(udps_opackets);
751 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
752 NULL, inp);
753 break;
754 case AF_INET:
755 error = EAFNOSUPPORT;
756 goto release;
757 }
758 goto releaseopt;
759
760release:
761 m_freem(m);
762
763releaseopt:
764 if (control) {
765 ip6_clearpktopts(&opt, -1);
766 m_freem(control);
767 }
768 return (error);
769}
770
771static void
772udp6_abort(struct socket *so)
773{
774 INIT_VNET_INET(so->so_vnet);
775 struct inpcb *inp;
776
777 inp = sotoinpcb(so);
778 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
779
780#ifdef INET
781 if (inp->inp_vflag & INP_IPV4) {
782 struct pr_usrreqs *pru;
783
784 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
785 (*pru->pru_abort)(so);
786 return;
787 }
788#endif
789
790 INP_INFO_WLOCK(&V_udbinfo);
791 INP_WLOCK(inp);
792 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
793 in6_pcbdisconnect(inp);
794 inp->in6p_laddr = in6addr_any;
795 soisdisconnected(so);
796 }
797 INP_WUNLOCK(inp);
798 INP_INFO_WUNLOCK(&V_udbinfo);
799}
800
801static int
802udp6_attach(struct socket *so, int proto, struct thread *td)
803{
804 INIT_VNET_INET(so->so_vnet);
805 struct inpcb *inp;
806 int error;
807
808 inp = sotoinpcb(so);
809 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
810
811 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
812 error = soreserve(so, udp_sendspace, udp_recvspace);
813 if (error)
814 return (error);
815 }
816 INP_INFO_WLOCK(&V_udbinfo);
817 error = in_pcballoc(so, &V_udbinfo);
818 if (error) {
819 INP_INFO_WUNLOCK(&V_udbinfo);
820 return (error);
821 }
822 inp = (struct inpcb *)so->so_pcb;
823 INP_INFO_WUNLOCK(&V_udbinfo);
824 inp->inp_vflag |= INP_IPV6;
825 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
826 inp->inp_vflag |= INP_IPV4;
827 inp->in6p_hops = -1; /* use kernel default */
828 inp->in6p_cksum = -1; /* just to be sure */
829 /*
830 * XXX: ugly!!
831 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
832 * because the socket may be bound to an IPv6 wildcard address,
833 * which may match an IPv4-mapped IPv6 address.
834 */
835 inp->inp_ip_ttl = V_ip_defttl;
836 INP_WUNLOCK(inp);
837 return (0);
838}
839
840static int
841udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
842{
843 INIT_VNET_INET(so->so_vnet);
844 struct inpcb *inp;
845 int error;
846
847 inp = sotoinpcb(so);
848 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
849
850 INP_INFO_WLOCK(&V_udbinfo);
851 INP_WLOCK(inp);
852 inp->inp_vflag &= ~INP_IPV4;
853 inp->inp_vflag |= INP_IPV6;
854 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
855 struct sockaddr_in6 *sin6_p;
856
857 sin6_p = (struct sockaddr_in6 *)nam;
858
859 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
860 inp->inp_vflag |= INP_IPV4;
861 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
862 struct sockaddr_in sin;
863
864 in6_sin6_2_sin(&sin, sin6_p);
865 inp->inp_vflag |= INP_IPV4;
866 inp->inp_vflag &= ~INP_IPV6;
867 error = in_pcbbind(inp, (struct sockaddr *)&sin,
868 td->td_ucred);
869 goto out;
870 }
871 }
872
873 error = in6_pcbbind(inp, nam, td->td_ucred);
874out:
875 INP_WUNLOCK(inp);
876 INP_INFO_WUNLOCK(&V_udbinfo);
877 return (error);
878}
879
880static void
881udp6_close(struct socket *so)
882{
883 INIT_VNET_INET(so->so_vnet);
884 struct inpcb *inp;
885
886 inp = sotoinpcb(so);
887 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
888
889#ifdef INET
890 if (inp->inp_vflag & INP_IPV4) {
891 struct pr_usrreqs *pru;
892
893 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
894 (*pru->pru_disconnect)(so);
895 return;
896 }
897#endif
898 INP_INFO_WLOCK(&V_udbinfo);
899 INP_WLOCK(inp);
900 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
901 in6_pcbdisconnect(inp);
902 inp->in6p_laddr = in6addr_any;
903 soisdisconnected(so);
904 }
905 INP_WUNLOCK(inp);
906 INP_INFO_WUNLOCK(&V_udbinfo);
907}
908
909static int
910udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
911{
912 INIT_VNET_INET(so->so_vnet);
913 struct inpcb *inp;
914 struct sockaddr_in6 *sin6;
915 int error;
916
917 inp = sotoinpcb(so);
918 sin6 = (struct sockaddr_in6 *)nam;
919 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
920
921 INP_INFO_WLOCK(&V_udbinfo);
922 INP_WLOCK(inp);
923 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
924 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
925 struct sockaddr_in sin;
926
927 if (inp->inp_faddr.s_addr != INADDR_ANY) {
928 error = EISCONN;
929 goto out;
930 }
931 in6_sin6_2_sin(&sin, sin6);
932 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
933 if (error != 0)
934 goto out;
935 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
936 td->td_ucred);
937 if (error == 0) {
938 inp->inp_vflag |= INP_IPV4;
939 inp->inp_vflag &= ~INP_IPV6;
940 soisconnected(so);
941 }
942 goto out;
943 }
944 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
945 error = EISCONN;
946 goto out;
947 }
948 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
949 if (error != 0)
950 goto out;
951 error = in6_pcbconnect(inp, nam, td->td_ucred);
952 if (error == 0) {
953 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
954 /* should be non mapped addr */
955 inp->inp_vflag &= ~INP_IPV4;
956 inp->inp_vflag |= INP_IPV6;
957 }
958 soisconnected(so);
959 }
960out:
961 INP_WUNLOCK(inp);
962 INP_INFO_WUNLOCK(&V_udbinfo);
963 return (error);
964}
965
966static void
967udp6_detach(struct socket *so)
968{
969 INIT_VNET_INET(so->so_vnet);
970 struct inpcb *inp;
971
972 inp = sotoinpcb(so);
973 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
974
975 INP_INFO_WLOCK(&V_udbinfo);
976 INP_WLOCK(inp);
977 in_pcbdetach(inp);
978 in_pcbfree(inp);
979 INP_INFO_WUNLOCK(&V_udbinfo);
980}
981
982static int
983udp6_disconnect(struct socket *so)
984{
985 INIT_VNET_INET(so->so_vnet);
986 struct inpcb *inp;
987 int error;
988
989 inp = sotoinpcb(so);
990 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
991
992 INP_INFO_WLOCK(&V_udbinfo);
993 INP_WLOCK(inp);
994
995#ifdef INET
996 if (inp->inp_vflag & INP_IPV4) {
997 struct pr_usrreqs *pru;
998
999 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1000 error = (*pru->pru_disconnect)(so);
1001 goto out;
1002 }
1003#endif
1004
1005 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1006 error = ENOTCONN;
1007 goto out;
1008 }
1009
1010 in6_pcbdisconnect(inp);
1011 inp->in6p_laddr = in6addr_any;
1012 SOCK_LOCK(so);
1013 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1014 SOCK_UNLOCK(so);
1015out:
1016 INP_WUNLOCK(inp);
1017 INP_INFO_WUNLOCK(&V_udbinfo);
1018 return (0);
1019}
1020
1021static int
1022udp6_send(struct socket *so, int flags, struct mbuf *m,
1023 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1024{
1025 INIT_VNET_INET(so->so_vnet);
1026 struct inpcb *inp;
1027 int error = 0;
1028
1029 inp = sotoinpcb(so);
1030 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1031
1032 INP_INFO_WLOCK(&V_udbinfo);
1033 INP_WLOCK(inp);
1034 if (addr) {
1035 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1036 error = EINVAL;
1037 goto bad;
1038 }
1039 if (addr->sa_family != AF_INET6) {
1040 error = EAFNOSUPPORT;
1041 goto bad;
1042 }
1043 }
1044
1045#ifdef INET
1046 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1047 int hasv4addr;
1048 struct sockaddr_in6 *sin6 = 0;
1049
1050 if (addr == 0)
1051 hasv4addr = (inp->inp_vflag & INP_IPV4);
1052 else {
1053 sin6 = (struct sockaddr_in6 *)addr;
1054 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1055 ? 1 : 0;
1056 }
1057 if (hasv4addr) {
1058 struct pr_usrreqs *pru;
1059
1060 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1061 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
1062 /*
1063 * When remote addr is IPv4-mapped address,
1064 * local addr should not be an IPv6 address;
1065 * since you cannot determine how to map IPv6
1066 * source address to IPv4.
1067 */
1068 error = EINVAL;
1069 goto out;
1070 }
1071
1072 /*
1073 * XXXRW: We release UDP-layer locks before calling
1074 * udp_send() in order to avoid recursion. However,
1075 * this does mean there is a short window where inp's
1076 * fields are unstable. Could this lead to a
1077 * potential race in which the factors causing us to
1078 * select the UDPv4 output routine are invalidated?
1079 */
1080 INP_WUNLOCK(inp);
1081 INP_INFO_WUNLOCK(&V_udbinfo);
1082 if (sin6)
1083 in6_sin6_2_sin_in_sock(addr);
1084 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1085 /* addr will just be freed in sendit(). */
1086 return ((*pru->pru_send)(so, flags, m, addr, control,
1087 td));
1088 }
1089 }
1090#endif
1091#ifdef MAC
1092 mac_inpcb_create_mbuf(inp, m);
1093#endif
1094 error = udp6_output(inp, m, addr, control, td);
1095out:
1096 INP_WUNLOCK(inp);
1097 INP_INFO_WUNLOCK(&V_udbinfo);
1098 return (error);
1099
1100bad:
1101 INP_WUNLOCK(inp);
1102 INP_INFO_WUNLOCK(&V_udbinfo);
1103 m_freem(m);
1104 return (error);
1105}
1106
1107struct pr_usrreqs udp6_usrreqs = {
1108 .pru_abort = udp6_abort,
1109 .pru_attach = udp6_attach,
1110 .pru_bind = udp6_bind,
1111 .pru_connect = udp6_connect,
1112 .pru_control = in6_control,
1113 .pru_detach = udp6_detach,
1114 .pru_disconnect = udp6_disconnect,
1115 .pru_peeraddr = in6_mapped_peeraddr,
1116 .pru_send = udp6_send,
1117 .pru_shutdown = udp_shutdown,
1118 .pru_sockaddr = in6_mapped_sockaddr,
1119 .pru_soreceive = soreceive_dgram,
1120 .pru_sosend = sosend_dgram,
1121 .pru_sosetlabel = in_pcbsosetlabel,
1122 .pru_close = udp6_close
1123};