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