udp6_usrreq.c revision 1.1.2.1 1 1.1.2.1 itojun /*
2 1.1.2.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 1.1.2.1 itojun * All rights reserved.
4 1.1.2.1 itojun *
5 1.1.2.1 itojun * Redistribution and use in source and binary forms, with or without
6 1.1.2.1 itojun * modification, are permitted provided that the following conditions
7 1.1.2.1 itojun * are met:
8 1.1.2.1 itojun * 1. Redistributions of source code must retain the above copyright
9 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer.
10 1.1.2.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
11 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer in the
12 1.1.2.1 itojun * documentation and/or other materials provided with the distribution.
13 1.1.2.1 itojun * 3. Neither the name of the project nor the names of its contributors
14 1.1.2.1 itojun * may be used to endorse or promote products derived from this software
15 1.1.2.1 itojun * without specific prior written permission.
16 1.1.2.1 itojun *
17 1.1.2.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.1.2.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1.2.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1.2.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.1.2.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1.2.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1.2.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1.2.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1.2.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1.2.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1.2.1 itojun * SUCH DAMAGE.
28 1.1.2.1 itojun */
29 1.1.2.1 itojun
30 1.1.2.1 itojun /*
31 1.1.2.1 itojun * Copyright (c) 1982, 1986, 1989, 1993
32 1.1.2.1 itojun * The Regents of the University of California. All rights reserved.
33 1.1.2.1 itojun *
34 1.1.2.1 itojun * Redistribution and use in source and binary forms, with or without
35 1.1.2.1 itojun * modification, are permitted provided that the following conditions
36 1.1.2.1 itojun * are met:
37 1.1.2.1 itojun * 1. Redistributions of source code must retain the above copyright
38 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer.
39 1.1.2.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
40 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer in the
41 1.1.2.1 itojun * documentation and/or other materials provided with the distribution.
42 1.1.2.1 itojun * 3. All advertising materials mentioning features or use of this software
43 1.1.2.1 itojun * must display the following acknowledgement:
44 1.1.2.1 itojun * This product includes software developed by the University of
45 1.1.2.1 itojun * California, Berkeley and its contributors.
46 1.1.2.1 itojun * 4. Neither the name of the University nor the names of its contributors
47 1.1.2.1 itojun * may be used to endorse or promote products derived from this software
48 1.1.2.1 itojun * without specific prior written permission.
49 1.1.2.1 itojun *
50 1.1.2.1 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 1.1.2.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 1.1.2.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 1.1.2.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 1.1.2.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 1.1.2.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 1.1.2.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 1.1.2.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 1.1.2.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 1.1.2.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 1.1.2.1 itojun * SUCH DAMAGE.
61 1.1.2.1 itojun *
62 1.1.2.1 itojun * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
63 1.1.2.1 itojun */
64 1.1.2.1 itojun
65 1.1.2.1 itojun #include <sys/param.h>
66 1.1.2.1 itojun #include <sys/malloc.h>
67 1.1.2.1 itojun #include <sys/mbuf.h>
68 1.1.2.1 itojun #include <sys/protosw.h>
69 1.1.2.1 itojun #include <sys/socket.h>
70 1.1.2.1 itojun #include <sys/socketvar.h>
71 1.1.2.1 itojun #include <sys/errno.h>
72 1.1.2.1 itojun #include <sys/stat.h>
73 1.1.2.1 itojun #include <sys/systm.h>
74 1.1.2.1 itojun #ifdef __NetBSD__
75 1.1.2.1 itojun #include <sys/proc.h>
76 1.1.2.1 itojun #endif
77 1.1.2.1 itojun
78 1.1.2.1 itojun #include <net/if.h>
79 1.1.2.1 itojun #include <net/route.h>
80 1.1.2.1 itojun #include <net/if_types.h>
81 1.1.2.1 itojun
82 1.1.2.1 itojun #include <netinet/in.h>
83 1.1.2.1 itojun #include <netinet/in_var.h>
84 1.1.2.1 itojun #include <netinet6/in6_systm.h>
85 1.1.2.1 itojun #include <netinet6/ip6.h>
86 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
87 1.1.2.1 itojun #include <netinet/in_pcb.h>
88 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
89 1.1.2.1 itojun #include <netinet6/in6_pcb.h>
90 1.1.2.1 itojun #include <netinet6/ip6_var.h>
91 1.1.2.1 itojun #include <netinet6/icmp6.h>
92 1.1.2.1 itojun #include <netinet6/udp6.h>
93 1.1.2.1 itojun #include <netinet6/udp6_var.h>
94 1.1.2.1 itojun
95 1.1.2.1 itojun #ifdef IPSEC
96 1.1.2.1 itojun #include <netinet6/ipsec.h>
97 1.1.2.1 itojun #endif /*IPSEC*/
98 1.1.2.1 itojun
99 1.1.2.1 itojun #include "faith.h"
100 1.1.2.1 itojun
101 1.1.2.1 itojun /*
102 1.1.2.1 itojun * UDP protocol inplementation.
103 1.1.2.1 itojun * Per RFC 768, August, 1980.
104 1.1.2.1 itojun */
105 1.1.2.1 itojun
106 1.1.2.1 itojun struct in6pcb *udp6_last_in6pcb = &udb6;
107 1.1.2.1 itojun
108 1.1.2.1 itojun static int in6_mcmatch __P((struct in6pcb *, struct in6_addr *, struct ifnet *));
109 1.1.2.1 itojun static void udp6_detach __P((struct in6pcb *));
110 1.1.2.1 itojun static void udp6_notify __P((struct in6pcb *, int));
111 1.1.2.1 itojun
112 1.1.2.1 itojun void
113 1.1.2.1 itojun udp6_init()
114 1.1.2.1 itojun {
115 1.1.2.1 itojun udb6.in6p_next = udb6.in6p_prev = &udb6;
116 1.1.2.1 itojun }
117 1.1.2.1 itojun
118 1.1.2.1 itojun static int
119 1.1.2.1 itojun in6_mcmatch(in6p, ia6, ifp)
120 1.1.2.1 itojun struct in6pcb *in6p;
121 1.1.2.1 itojun register struct in6_addr *ia6;
122 1.1.2.1 itojun struct ifnet *ifp;
123 1.1.2.1 itojun {
124 1.1.2.1 itojun struct ip6_moptions *im6o = in6p->in6p_moptions;
125 1.1.2.1 itojun struct in6_multi_mship *imm;
126 1.1.2.1 itojun
127 1.1.2.1 itojun if (im6o == NULL)
128 1.1.2.1 itojun return 0;
129 1.1.2.1 itojun
130 1.1.2.1 itojun for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
131 1.1.2.1 itojun imm = imm->i6mm_chain.le_next) {
132 1.1.2.1 itojun if ((ifp == NULL ||
133 1.1.2.1 itojun imm->i6mm_maddr->in6m_ifp == ifp) &&
134 1.1.2.1 itojun IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
135 1.1.2.1 itojun ia6))
136 1.1.2.1 itojun return 1;
137 1.1.2.1 itojun }
138 1.1.2.1 itojun return 0;
139 1.1.2.1 itojun }
140 1.1.2.1 itojun
141 1.1.2.1 itojun int
142 1.1.2.1 itojun udp6_input(mp, offp, proto)
143 1.1.2.1 itojun struct mbuf **mp;
144 1.1.2.1 itojun int *offp, proto;
145 1.1.2.1 itojun {
146 1.1.2.1 itojun struct mbuf *m = *mp;
147 1.1.2.1 itojun register struct ip6_hdr *ip6;
148 1.1.2.1 itojun register struct udphdr *uh;
149 1.1.2.1 itojun register struct in6pcb *in6p;
150 1.1.2.1 itojun struct mbuf *opts = 0;
151 1.1.2.1 itojun int off = *offp;
152 1.1.2.1 itojun int plen, ulen;
153 1.1.2.1 itojun struct sockaddr_in6 udp_in6;
154 1.1.2.1 itojun
155 1.1.2.1 itojun #if defined(NFAITH) && 0 < NFAITH
156 1.1.2.1 itojun if (m->m_pkthdr.rcvif) {
157 1.1.2.1 itojun if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
158 1.1.2.1 itojun /* send icmp6 host unreach? */
159 1.1.2.1 itojun m_freem(m);
160 1.1.2.1 itojun return IPPROTO_DONE;
161 1.1.2.1 itojun }
162 1.1.2.1 itojun }
163 1.1.2.1 itojun #endif
164 1.1.2.1 itojun udp6stat.udp6s_ipackets++;
165 1.1.2.1 itojun
166 1.1.2.1 itojun IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
167 1.1.2.1 itojun
168 1.1.2.1 itojun ip6 = mtod(m, struct ip6_hdr *);
169 1.1.2.1 itojun plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
170 1.1.2.1 itojun uh = (struct udphdr *)((caddr_t)ip6 + off);
171 1.1.2.1 itojun ulen = ntohs((u_short)uh->uh_ulen);
172 1.1.2.1 itojun
173 1.1.2.1 itojun if (plen != ulen) {
174 1.1.2.1 itojun udp6stat.udp6s_badlen++;
175 1.1.2.1 itojun goto bad;
176 1.1.2.1 itojun }
177 1.1.2.1 itojun
178 1.1.2.1 itojun /*
179 1.1.2.1 itojun * Checksum extended UDP header and data.
180 1.1.2.1 itojun */
181 1.1.2.1 itojun if (uh->uh_sum == 0)
182 1.1.2.1 itojun udp6stat.udp6s_nosum++;
183 1.1.2.1 itojun else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
184 1.1.2.1 itojun udp6stat.udp6s_badsum++;
185 1.1.2.1 itojun goto bad;
186 1.1.2.1 itojun }
187 1.1.2.1 itojun
188 1.1.2.1 itojun if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
189 1.1.2.1 itojun struct in6pcb *last;
190 1.1.2.1 itojun
191 1.1.2.1 itojun /*
192 1.1.2.1 itojun * Deliver a multicast datagram to all sockets
193 1.1.2.1 itojun * for which the local and remote addresses and ports match
194 1.1.2.1 itojun * those of the incoming datagram. This allows more than
195 1.1.2.1 itojun * one process to receive multicasts on the same port.
196 1.1.2.1 itojun * (This really ought to be done for unicast datagrams as
197 1.1.2.1 itojun * well, but that would cause problems with existing
198 1.1.2.1 itojun * applications that open both address-specific sockets and
199 1.1.2.1 itojun * a wildcard socket listening to the same port -- they would
200 1.1.2.1 itojun * end up receiving duplicates of every unicast datagram.
201 1.1.2.1 itojun * Those applications open the multiple sockets to overcome an
202 1.1.2.1 itojun * inadequacy of the UDP socket interface, but for backwards
203 1.1.2.1 itojun * compatibility we avoid the problem here rather than
204 1.1.2.1 itojun * fixing the interface. Maybe 4.5BSD will remedy this?)
205 1.1.2.1 itojun */
206 1.1.2.1 itojun
207 1.1.2.1 itojun /*
208 1.1.2.1 itojun * In a case that laddr should be set to the link-local
209 1.1.2.1 itojun * address (this happens in RIPng), the multicast address
210 1.1.2.1 itojun * specified in the received packet does not match with
211 1.1.2.1 itojun * laddr. To cure this situation, the matching is relaxed
212 1.1.2.1 itojun * if the receiving interface is the same as one specified
213 1.1.2.1 itojun * in the socket and if the destination multicast address
214 1.1.2.1 itojun * matches one of the multicast groups specified in the socket.
215 1.1.2.1 itojun */
216 1.1.2.1 itojun
217 1.1.2.1 itojun /*
218 1.1.2.1 itojun * Construct sockaddr format source address.
219 1.1.2.1 itojun */
220 1.1.2.1 itojun bzero(&udp_in6, sizeof(udp_in6));
221 1.1.2.1 itojun udp_in6.sin6_len = sizeof(struct sockaddr_in6);
222 1.1.2.1 itojun udp_in6.sin6_family = AF_INET6;
223 1.1.2.1 itojun udp_in6.sin6_port = uh->uh_sport;
224 1.1.2.1 itojun udp_in6.sin6_addr = ip6->ip6_src;
225 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr))
226 1.1.2.1 itojun udp_in6.sin6_addr.s6_addr16[1] = 0;
227 1.1.2.1 itojun if (m->m_pkthdr.rcvif) {
228 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr)) {
229 1.1.2.1 itojun udp_in6.sin6_scope_id =
230 1.1.2.1 itojun m->m_pkthdr.rcvif->if_index;
231 1.1.2.1 itojun } else
232 1.1.2.1 itojun udp_in6.sin6_scope_id = 0;
233 1.1.2.1 itojun } else
234 1.1.2.1 itojun udp_in6.sin6_scope_id = 0;
235 1.1.2.1 itojun /*
236 1.1.2.1 itojun * KAME note: usually we drop udphdr from mbuf here.
237 1.1.2.1 itojun * We need udphdr for IPsec processing so we do that later.
238 1.1.2.1 itojun */
239 1.1.2.1 itojun
240 1.1.2.1 itojun /*
241 1.1.2.1 itojun * Locate pcb(s) for datagram.
242 1.1.2.1 itojun * (Algorithm copied from raw_intr().)
243 1.1.2.1 itojun */
244 1.1.2.1 itojun last = NULL;
245 1.1.2.1 itojun for (in6p = udb6.in6p_next;
246 1.1.2.1 itojun in6p != &udb6;
247 1.1.2.1 itojun in6p = in6p->in6p_next) {
248 1.1.2.1 itojun if (in6p->in6p_lport != uh->uh_dport)
249 1.1.2.1 itojun continue;
250 1.1.2.1 itojun if (!IN6_IS_ADDR_ANY(&in6p->in6p_laddr)) {
251 1.1.2.1 itojun if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
252 1.1.2.1 itojun &ip6->ip6_dst) &&
253 1.1.2.1 itojun !in6_mcmatch(in6p, &ip6->ip6_dst,
254 1.1.2.1 itojun m->m_pkthdr.rcvif))
255 1.1.2.1 itojun continue;
256 1.1.2.1 itojun }
257 1.1.2.1 itojun if (!IN6_IS_ADDR_ANY(&in6p->in6p_faddr)) {
258 1.1.2.1 itojun if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
259 1.1.2.1 itojun &ip6->ip6_src) ||
260 1.1.2.1 itojun in6p->in6p_fport != uh->uh_sport)
261 1.1.2.1 itojun continue;
262 1.1.2.1 itojun }
263 1.1.2.1 itojun
264 1.1.2.1 itojun if (last != NULL) {
265 1.1.2.1 itojun struct mbuf *n;
266 1.1.2.1 itojun
267 1.1.2.1 itojun #ifdef IPSEC
268 1.1.2.1 itojun /*
269 1.1.2.1 itojun * Check AH/ESP integrity.
270 1.1.2.1 itojun */
271 1.1.2.1 itojun if (last != NULL && ipsec6_in_reject(m, last)) {
272 1.1.2.1 itojun ipsec6stat.in_polvio++;
273 1.1.2.1 itojun /* do not inject data into pcb */
274 1.1.2.1 itojun } else
275 1.1.2.1 itojun #endif /*IPSEC*/
276 1.1.2.1 itojun if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
277 1.1.2.1 itojun /*
278 1.1.2.1 itojun * KAME NOTE: do not
279 1.1.2.1 itojun * m_copy(m, offset, ...) above.
280 1.1.2.1 itojun * sbappendaddr() expects M_PKTHDR,
281 1.1.2.1 itojun * and m_copy() will copy M_PKTHDR
282 1.1.2.1 itojun * only if offset is 0.
283 1.1.2.1 itojun */
284 1.1.2.1 itojun if (last->in6p_flags & IN6P_CONTROLOPTS) {
285 1.1.2.1 itojun ip6_savecontrol(last, &opts,
286 1.1.2.1 itojun ip6, n);
287 1.1.2.1 itojun }
288 1.1.2.1 itojun
289 1.1.2.1 itojun m_adj(m, off + sizeof(struct udphdr));
290 1.1.2.1 itojun if (sbappendaddr(&last->in6p_socket->so_rcv,
291 1.1.2.1 itojun (struct sockaddr *)&udp_in6,
292 1.1.2.1 itojun n, opts) == 0) {
293 1.1.2.1 itojun m_freem(n);
294 1.1.2.1 itojun if (opts)
295 1.1.2.1 itojun m_freem(opts);
296 1.1.2.1 itojun udp6stat.udp6s_fullsock++;
297 1.1.2.1 itojun } else
298 1.1.2.1 itojun sorwakeup(last->in6p_socket);
299 1.1.2.1 itojun opts = 0;
300 1.1.2.1 itojun }
301 1.1.2.1 itojun }
302 1.1.2.1 itojun last = in6p;
303 1.1.2.1 itojun /*
304 1.1.2.1 itojun * Don't look for additional matches if this one does
305 1.1.2.1 itojun * not have either the SO_REUSEPORT or SO_REUSEADDR
306 1.1.2.1 itojun * socket options set. This heuristic avoids searching
307 1.1.2.1 itojun * through all pcbs in the common case of a non-shared
308 1.1.2.1 itojun * port. It assumes that an application will never
309 1.1.2.1 itojun * clear these options after setting them.
310 1.1.2.1 itojun */
311 1.1.2.1 itojun if ((last->in6p_socket->so_options &
312 1.1.2.1 itojun (SO_REUSEPORT|SO_REUSEADDR)) == 0)
313 1.1.2.1 itojun break;
314 1.1.2.1 itojun }
315 1.1.2.1 itojun
316 1.1.2.1 itojun if (last == NULL) {
317 1.1.2.1 itojun /*
318 1.1.2.1 itojun * No matching pcb found; discard datagram.
319 1.1.2.1 itojun * (No need to send an ICMP Port Unreachable
320 1.1.2.1 itojun * for a broadcast or multicast datgram.)
321 1.1.2.1 itojun */
322 1.1.2.1 itojun udp6stat.udp6s_noport++;
323 1.1.2.1 itojun udp6stat.udp6s_noportmcast++;
324 1.1.2.1 itojun goto bad;
325 1.1.2.1 itojun }
326 1.1.2.1 itojun #ifdef IPSEC
327 1.1.2.1 itojun /*
328 1.1.2.1 itojun * Check AH/ESP integrity.
329 1.1.2.1 itojun */
330 1.1.2.1 itojun if (last != NULL && ipsec6_in_reject(m, last)) {
331 1.1.2.1 itojun ipsec6stat.in_polvio++;
332 1.1.2.1 itojun goto bad;
333 1.1.2.1 itojun }
334 1.1.2.1 itojun #endif /*IPSEC*/
335 1.1.2.1 itojun if (last->in6p_flags & IN6P_CONTROLOPTS)
336 1.1.2.1 itojun ip6_savecontrol(last, &opts, ip6, m);
337 1.1.2.1 itojun
338 1.1.2.1 itojun m_adj(m, off + sizeof(struct udphdr));
339 1.1.2.1 itojun if (sbappendaddr(&last->in6p_socket->so_rcv,
340 1.1.2.1 itojun (struct sockaddr *)&udp_in6,
341 1.1.2.1 itojun m, opts) == 0) {
342 1.1.2.1 itojun udp6stat.udp6s_fullsock++;
343 1.1.2.1 itojun goto bad;
344 1.1.2.1 itojun }
345 1.1.2.1 itojun sorwakeup(last->in6p_socket);
346 1.1.2.1 itojun return IPPROTO_DONE;
347 1.1.2.1 itojun }
348 1.1.2.1 itojun /*
349 1.1.2.1 itojun * Locate pcb for datagram.
350 1.1.2.1 itojun */
351 1.1.2.1 itojun in6p = udp6_last_in6pcb;
352 1.1.2.1 itojun if (in6p->in6p_lport != uh->uh_dport ||
353 1.1.2.1 itojun in6p->in6p_fport != uh->uh_sport ||
354 1.1.2.1 itojun !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src) ||
355 1.1.2.1 itojun !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) {
356 1.1.2.1 itojun in6p = in6_pcblookup(&udb6,
357 1.1.2.1 itojun &ip6->ip6_src, uh->uh_sport,
358 1.1.2.1 itojun &ip6->ip6_dst, uh->uh_dport,
359 1.1.2.1 itojun IN6PLOOKUP_WILDCARD);
360 1.1.2.1 itojun if (in6p)
361 1.1.2.1 itojun udp6_last_in6pcb = in6p;
362 1.1.2.1 itojun udp6stat.udp6ps_pcbcachemiss++;
363 1.1.2.1 itojun }
364 1.1.2.1 itojun if (in6p == 0) {
365 1.1.2.1 itojun udp6stat.udp6s_noport++;
366 1.1.2.1 itojun if (m->m_flags & M_MCAST) {
367 1.1.2.1 itojun printf("UDP6: M_MCAST is set in a unicast packet.\n");
368 1.1.2.1 itojun udp6stat.udp6s_noportmcast++;
369 1.1.2.1 itojun goto bad;
370 1.1.2.1 itojun }
371 1.1.2.1 itojun icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
372 1.1.2.1 itojun return IPPROTO_DONE;
373 1.1.2.1 itojun }
374 1.1.2.1 itojun #ifdef IPSEC
375 1.1.2.1 itojun /*
376 1.1.2.1 itojun * Check AH/ESP integrity.
377 1.1.2.1 itojun */
378 1.1.2.1 itojun if (in6p != NULL && ipsec6_in_reject(m, in6p)) {
379 1.1.2.1 itojun ipsec6stat.in_polvio++;
380 1.1.2.1 itojun goto bad;
381 1.1.2.1 itojun }
382 1.1.2.1 itojun #endif /*IPSEC*/
383 1.1.2.1 itojun
384 1.1.2.1 itojun /*
385 1.1.2.1 itojun * Construct sockaddr format source address.
386 1.1.2.1 itojun * Stuff source address and datagram in user buffer.
387 1.1.2.1 itojun */
388 1.1.2.1 itojun bzero(&udp_in6, sizeof(udp_in6));
389 1.1.2.1 itojun udp_in6.sin6_len = sizeof(struct sockaddr_in6);
390 1.1.2.1 itojun udp_in6.sin6_family = AF_INET6;
391 1.1.2.1 itojun udp_in6.sin6_port = uh->uh_sport;
392 1.1.2.1 itojun udp_in6.sin6_addr = ip6->ip6_src;
393 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr))
394 1.1.2.1 itojun udp_in6.sin6_addr.s6_addr16[1] = 0;
395 1.1.2.1 itojun if (m->m_pkthdr.rcvif) {
396 1.1.2.1 itojun if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr))
397 1.1.2.1 itojun udp_in6.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
398 1.1.2.1 itojun else
399 1.1.2.1 itojun udp_in6.sin6_scope_id = 0;
400 1.1.2.1 itojun } else
401 1.1.2.1 itojun udp_in6.sin6_scope_id = 0;
402 1.1.2.1 itojun if (in6p->in6p_flags & IN6P_CONTROLOPTS)
403 1.1.2.1 itojun ip6_savecontrol(in6p, &opts, ip6, m);
404 1.1.2.1 itojun
405 1.1.2.1 itojun m_adj(m, off + sizeof(struct udphdr));
406 1.1.2.1 itojun if (sbappendaddr(&in6p->in6p_socket->so_rcv,
407 1.1.2.1 itojun (struct sockaddr *)&udp_in6,
408 1.1.2.1 itojun m, opts) == 0) {
409 1.1.2.1 itojun udp6stat.udp6s_fullsock++;
410 1.1.2.1 itojun goto bad;
411 1.1.2.1 itojun }
412 1.1.2.1 itojun sorwakeup(in6p->in6p_socket);
413 1.1.2.1 itojun return IPPROTO_DONE;
414 1.1.2.1 itojun bad:
415 1.1.2.1 itojun if (m)
416 1.1.2.1 itojun m_freem(m);
417 1.1.2.1 itojun if (opts)
418 1.1.2.1 itojun m_freem(opts);
419 1.1.2.1 itojun return IPPROTO_DONE;
420 1.1.2.1 itojun }
421 1.1.2.1 itojun
422 1.1.2.1 itojun /*
423 1.1.2.1 itojun * Notify a udp user of an asynchronous error;
424 1.1.2.1 itojun * just wake up so tat he can collect error status.
425 1.1.2.1 itojun */
426 1.1.2.1 itojun static void
427 1.1.2.1 itojun udp6_notify(in6p, errno)
428 1.1.2.1 itojun register struct in6pcb *in6p;
429 1.1.2.1 itojun int errno;
430 1.1.2.1 itojun {
431 1.1.2.1 itojun in6p->in6p_socket->so_error = errno;
432 1.1.2.1 itojun sorwakeup(in6p->in6p_socket);
433 1.1.2.1 itojun sowwakeup(in6p->in6p_socket);
434 1.1.2.1 itojun }
435 1.1.2.1 itojun
436 1.1.2.1 itojun void
437 1.1.2.1 itojun udp6_ctlinput(cmd, sa, ip6, m, off)
438 1.1.2.1 itojun int cmd;
439 1.1.2.1 itojun struct sockaddr *sa;
440 1.1.2.1 itojun register struct ip6_hdr *ip6;
441 1.1.2.1 itojun struct mbuf *m;
442 1.1.2.1 itojun int off;
443 1.1.2.1 itojun {
444 1.1.2.1 itojun register struct udphdr *uhp;
445 1.1.2.1 itojun struct udphdr uh;
446 1.1.2.1 itojun
447 1.1.2.1 itojun #if 0
448 1.1.2.1 itojun if (cmd == PRC_IFNEWADDR)
449 1.1.2.1 itojun in6_mrejoin(&udb6);
450 1.1.2.1 itojun else
451 1.1.2.1 itojun #endif
452 1.1.2.1 itojun if (!PRC_IS_REDIRECT(cmd) &&
453 1.1.2.1 itojun ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
454 1.1.2.1 itojun return;
455 1.1.2.1 itojun if (ip6) {
456 1.1.2.1 itojun /*
457 1.1.2.1 itojun * XXX: We assume that when IPV6 is non NULL,
458 1.1.2.1 itojun * M and OFF are valid.
459 1.1.2.1 itojun */
460 1.1.2.1 itojun if (m->m_len < off + sizeof(uh)) {
461 1.1.2.1 itojun /*
462 1.1.2.1 itojun * this should be rare case,
463 1.1.2.1 itojun * so we compromise on this copy...
464 1.1.2.1 itojun */
465 1.1.2.1 itojun m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
466 1.1.2.1 itojun uhp = &uh;
467 1.1.2.1 itojun } else
468 1.1.2.1 itojun uhp = (struct udphdr *)(mtod(m, caddr_t) + off);
469 1.1.2.1 itojun (void) in6_pcbnotify(&udb6, sa, uhp->uh_dport, &ip6->ip6_src,
470 1.1.2.1 itojun uhp->uh_sport, cmd, udp6_notify);
471 1.1.2.1 itojun } else {
472 1.1.2.1 itojun (void) in6_pcbnotify(&udb6, sa, 0, &zeroin6_addr, 0, cmd,
473 1.1.2.1 itojun udp6_notify);
474 1.1.2.1 itojun }
475 1.1.2.1 itojun }
476 1.1.2.1 itojun
477 1.1.2.1 itojun int
478 1.1.2.1 itojun udp6_output(in6p, m, addr6, control)
479 1.1.2.1 itojun register struct in6pcb *in6p;
480 1.1.2.1 itojun register struct mbuf *m;
481 1.1.2.1 itojun struct mbuf *addr6, *control;
482 1.1.2.1 itojun {
483 1.1.2.1 itojun register int ulen = m->m_pkthdr.len;
484 1.1.2.1 itojun int plen = sizeof(struct udphdr) + ulen;
485 1.1.2.1 itojun struct ip6_hdr *ip6;
486 1.1.2.1 itojun struct udphdr *udp6;
487 1.1.2.1 itojun struct in6_addr laddr6;
488 1.1.2.1 itojun int s = 0, error = 0;
489 1.1.2.1 itojun struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
490 1.1.2.1 itojun int priv = 0;
491 1.1.2.1 itojun struct proc *p = curproc; /* XXX */
492 1.1.2.1 itojun
493 1.1.2.1 itojun if (p && !suser(p->p_ucred, &p->p_acflag))
494 1.1.2.1 itojun priv = 1;
495 1.1.2.1 itojun if (control) {
496 1.1.2.1 itojun if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
497 1.1.2.1 itojun goto release;
498 1.1.2.1 itojun in6p->in6p_outputopts = &opt;
499 1.1.2.1 itojun }
500 1.1.2.1 itojun
501 1.1.2.1 itojun if (addr6) {
502 1.1.2.1 itojun laddr6 = in6p->in6p_laddr;
503 1.1.2.1 itojun if (!IN6_IS_ADDR_ANY(&in6p->in6p_faddr)) {
504 1.1.2.1 itojun error = EISCONN;
505 1.1.2.1 itojun goto release;
506 1.1.2.1 itojun }
507 1.1.2.1 itojun /*
508 1.1.2.1 itojun * Must block input while temporarily connected.
509 1.1.2.1 itojun */
510 1.1.2.1 itojun s = splnet();
511 1.1.2.1 itojun error = in6_pcbconnect(in6p, addr6);
512 1.1.2.1 itojun if (error) {
513 1.1.2.1 itojun splx(s);
514 1.1.2.1 itojun goto release;
515 1.1.2.1 itojun }
516 1.1.2.1 itojun } else {
517 1.1.2.1 itojun if (IN6_IS_ADDR_ANY(&in6p->in6p_faddr)) {
518 1.1.2.1 itojun error = ENOTCONN;
519 1.1.2.1 itojun goto release;
520 1.1.2.1 itojun }
521 1.1.2.1 itojun }
522 1.1.2.1 itojun /*
523 1.1.2.1 itojun * Calculate data length and get a mbuf
524 1.1.2.1 itojun * for UDP and IP6 headers.
525 1.1.2.1 itojun */
526 1.1.2.1 itojun M_PREPEND(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr), M_DONTWAIT);
527 1.1.2.1 itojun if (m == 0) {
528 1.1.2.1 itojun error = ENOBUFS;
529 1.1.2.1 itojun goto release;
530 1.1.2.1 itojun }
531 1.1.2.1 itojun
532 1.1.2.1 itojun /*
533 1.1.2.1 itojun * Stuff checksum and output datagram.
534 1.1.2.1 itojun */
535 1.1.2.1 itojun ip6 = mtod(m, struct ip6_hdr *);
536 1.1.2.1 itojun ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
537 1.1.2.1 itojun ip6->ip6_vfc = IPV6_VERSION;
538 1.1.2.1 itojun #if 0 /* ip6_plen will be filled in ip6_output. */
539 1.1.2.1 itojun ip6->ip6_plen = htons((u_short)plen);
540 1.1.2.1 itojun #endif
541 1.1.2.1 itojun ip6->ip6_nxt = IPPROTO_UDP;
542 1.1.2.1 itojun ip6->ip6_hlim = in6p->in6p_ip6.ip6_hlim; /* XXX */
543 1.1.2.1 itojun ip6->ip6_src = in6p->in6p_laddr;
544 1.1.2.1 itojun ip6->ip6_dst = in6p->in6p_faddr;
545 1.1.2.1 itojun
546 1.1.2.1 itojun udp6 = (struct udphdr *)(ip6 + 1);
547 1.1.2.1 itojun udp6->uh_sport = in6p->in6p_lport;
548 1.1.2.1 itojun udp6->uh_dport = in6p->in6p_fport;
549 1.1.2.1 itojun udp6->uh_ulen = htons((u_short)plen);
550 1.1.2.1 itojun udp6->uh_sum = 0;
551 1.1.2.1 itojun
552 1.1.2.1 itojun if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
553 1.1.2.1 itojun sizeof(struct ip6_hdr), plen)) == 0) {
554 1.1.2.1 itojun udp6->uh_sum = 0xffff;
555 1.1.2.1 itojun }
556 1.1.2.1 itojun
557 1.1.2.1 itojun udp6stat.udp6s_opackets++;
558 1.1.2.1 itojun
559 1.1.2.1 itojun #ifdef IPSEC
560 1.1.2.1 itojun m->m_pkthdr.rcvif = (struct ifnet *)in6p->in6p_socket;
561 1.1.2.1 itojun #endif /*IPSEC*/
562 1.1.2.1 itojun error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
563 1.1.2.1 itojun 0, in6p->in6p_moptions);
564 1.1.2.1 itojun
565 1.1.2.1 itojun if (addr6) {
566 1.1.2.1 itojun in6_pcbdisconnect(in6p);
567 1.1.2.1 itojun in6p->in6p_laddr = laddr6;
568 1.1.2.1 itojun splx(s);
569 1.1.2.1 itojun }
570 1.1.2.1 itojun goto releaseopt;
571 1.1.2.1 itojun
572 1.1.2.1 itojun release:
573 1.1.2.1 itojun m_freem(m);
574 1.1.2.1 itojun
575 1.1.2.1 itojun releaseopt:
576 1.1.2.1 itojun if (control) {
577 1.1.2.1 itojun in6p->in6p_outputopts = stickyopt;
578 1.1.2.1 itojun m_freem(control);
579 1.1.2.1 itojun }
580 1.1.2.1 itojun return(error);
581 1.1.2.1 itojun }
582 1.1.2.1 itojun
583 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
584 1.1.2.1 itojun void udp_detach __P((struct inpcb *inp));
585 1.1.2.1 itojun int udp_usrreq __P((struct socket *so, int req, struct mbuf *m,
586 1.1.2.1 itojun struct mbuf *addr, struct mbuf *control));
587 1.1.2.1 itojun int udp_output __P((struct inpcb *, struct mbuf *, struct mbuf *,
588 1.1.2.1 itojun struct mbuf *));
589 1.1.2.1 itojun
590 1.1.2.1 itojun /*
591 1.1.2.1 itojun * Do V4 mapped addr related binding work.
592 1.1.2.1 itojun * Return 1 when V6 bind is not necessary.
593 1.1.2.1 itojun * Return 0 when V6 bind may be necessary.
594 1.1.2.1 itojun */
595 1.1.2.1 itojun static int
596 1.1.2.1 itojun udp6_mapped_bind(struct socket *so, struct mbuf *addr6, int *error_p)
597 1.1.2.1 itojun {
598 1.1.2.1 itojun struct inpcb *inp = sotoinpcb(so);
599 1.1.2.1 itojun struct sockaddr_in6 *sin6_p;
600 1.1.2.1 itojun int s, unspec;
601 1.1.2.1 itojun
602 1.1.2.1 itojun sin6_p = mtod(addr6, struct sockaddr_in6 *);
603 1.1.2.1 itojun unspec = (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) ? 1 : 0;
604 1.1.2.1 itojun if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr) || unspec) {
605 1.1.2.1 itojun
606 1.1.2.1 itojun /* Attach v4 if notyet */
607 1.1.2.1 itojun if (inp == 0) {
608 1.1.2.1 itojun *error_p = udp_usrreq(so, PRU_ATTACH, 0, 0, 0);
609 1.1.2.1 itojun if (*error_p)
610 1.1.2.1 itojun return 1;
611 1.1.2.1 itojun inp = sotoinpcb(so);
612 1.1.2.1 itojun }
613 1.1.2.1 itojun in6_sin6_2_sin_in_m(addr6);
614 1.1.2.1 itojun s = splnet();
615 1.1.2.1 itojun *error_p = in_pcbbind(inp, addr6);
616 1.1.2.1 itojun splx(s);
617 1.1.2.1 itojun if (*error_p)
618 1.1.2.1 itojun return 1;
619 1.1.2.1 itojun in6_sin_2_v4mapsin6_in_m(addr6);
620 1.1.2.1 itojun if (unspec) {
621 1.1.2.1 itojun sin6_p = mtod(addr6, struct sockaddr_in6 *);
622 1.1.2.1 itojun sin6_p->sin6_addr.s6_addr32[2] = 0;
623 1.1.2.1 itojun return 0;
624 1.1.2.1 itojun } else
625 1.1.2.1 itojun return 1;
626 1.1.2.1 itojun }
627 1.1.2.1 itojun return 0;
628 1.1.2.1 itojun }
629 1.1.2.1 itojun
630 1.1.2.1 itojun /*
631 1.1.2.1 itojun * Do V4 mapped addr related connect work.
632 1.1.2.1 itojun * Return 1 when V6 connect is not necessary.
633 1.1.2.1 itojun * Return 0 when V6 connect is necessary.
634 1.1.2.1 itojun */
635 1.1.2.1 itojun static int
636 1.1.2.1 itojun udp6_mapped_connect(struct socket *so, struct mbuf *addr6, int *error_p)
637 1.1.2.1 itojun {
638 1.1.2.1 itojun struct inpcb *inp = sotoinpcb(so);
639 1.1.2.1 itojun struct sockaddr_in6 *sin6_p;
640 1.1.2.1 itojun int s;
641 1.1.2.1 itojun
642 1.1.2.1 itojun sin6_p = mtod(addr6, struct sockaddr_in6 *);
643 1.1.2.1 itojun if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
644 1.1.2.1 itojun
645 1.1.2.1 itojun /* Attach v4 if notyet */
646 1.1.2.1 itojun if (inp == 0) {
647 1.1.2.1 itojun *error_p = udp_usrreq(so, PRU_ATTACH, 0, 0, 0);
648 1.1.2.1 itojun if (*error_p)
649 1.1.2.1 itojun return 1;
650 1.1.2.1 itojun inp = sotoinpcb(so);
651 1.1.2.1 itojun }
652 1.1.2.1 itojun if (inp->inp_faddr.s_addr != INADDR_ANY) {
653 1.1.2.1 itojun *error_p = EISCONN;
654 1.1.2.1 itojun return 1;
655 1.1.2.1 itojun }
656 1.1.2.1 itojun in6_sin6_2_sin_in_m(addr6);
657 1.1.2.1 itojun s = splnet();
658 1.1.2.1 itojun *error_p = in_pcbconnect(inp, addr6);
659 1.1.2.1 itojun splx(s);
660 1.1.2.1 itojun if (*error_p)
661 1.1.2.1 itojun return 1;
662 1.1.2.1 itojun else
663 1.1.2.1 itojun soisconnected(so);
664 1.1.2.1 itojun in6_sin_2_v4mapsin6_in_m(addr6);
665 1.1.2.1 itojun return 1;
666 1.1.2.1 itojun }
667 1.1.2.1 itojun return 0;
668 1.1.2.1 itojun }
669 1.1.2.1 itojun
670 1.1.2.1 itojun /*
671 1.1.2.1 itojun * Do V4 mapped addr related disconnect work.
672 1.1.2.1 itojun * Return 1 when v4 is connected.
673 1.1.2.1 itojun * Return 0 when V4 is not connected.
674 1.1.2.1 itojun */
675 1.1.2.1 itojun static int
676 1.1.2.1 itojun udp6_mapped_disconnect(struct socket *so, struct mbuf *addr6, int *error_p)
677 1.1.2.1 itojun {
678 1.1.2.1 itojun struct inpcb *inp = sotoinpcb(so);
679 1.1.2.1 itojun int s;
680 1.1.2.1 itojun
681 1.1.2.1 itojun if (inp) {
682 1.1.2.1 itojun if (inp->inp_faddr.s_addr == INADDR_ANY)
683 1.1.2.1 itojun return 0;
684 1.1.2.1 itojun else {
685 1.1.2.1 itojun s = splnet();
686 1.1.2.1 itojun in_pcbdisconnect(inp);
687 1.1.2.1 itojun inp->inp_laddr.s_addr = INADDR_ANY;
688 1.1.2.1 itojun splx(s);
689 1.1.2.1 itojun return 1;
690 1.1.2.1 itojun }
691 1.1.2.1 itojun } else
692 1.1.2.1 itojun return 0;
693 1.1.2.1 itojun }
694 1.1.2.1 itojun
695 1.1.2.1 itojun /*
696 1.1.2.1 itojun * Do V4 mapped addr related sending work.
697 1.1.2.1 itojun * Return 2 when v4 attach is failed.
698 1.1.2.1 itojun * Return 1 when v6 send is not necessary.
699 1.1.2.1 itojun * Return 0 when v6 send is necessary.
700 1.1.2.1 itojun */
701 1.1.2.1 itojun static int
702 1.1.2.1 itojun udp6_mapped_send(struct socket *so,
703 1.1.2.1 itojun struct mbuf *m,
704 1.1.2.1 itojun struct mbuf *addr6,
705 1.1.2.1 itojun struct mbuf *control,
706 1.1.2.1 itojun int *error_p)
707 1.1.2.1 itojun {
708 1.1.2.1 itojun struct inpcb *inp = sotoinpcb(so);
709 1.1.2.1 itojun struct in6pcb *in6p = sotoin6pcb(so);
710 1.1.2.1 itojun struct sockaddr_in6 *sin6_p = 0;
711 1.1.2.1 itojun int hasv4addr;
712 1.1.2.1 itojun
713 1.1.2.1 itojun if (addr6 == 0) {
714 1.1.2.1 itojun if (!IN6_IS_ADDR_ANY(&in6p->in6p_faddr))
715 1.1.2.1 itojun hasv4addr = 0;
716 1.1.2.1 itojun else
717 1.1.2.1 itojun hasv4addr = (inp &&
718 1.1.2.1 itojun inp->inp_faddr.s_addr != INADDR_ANY) ? 1 : 0;
719 1.1.2.1 itojun } else {
720 1.1.2.1 itojun sin6_p = mtod(addr6, struct sockaddr_in6 *);
721 1.1.2.1 itojun hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr) ? 1 : 0;
722 1.1.2.1 itojun }
723 1.1.2.1 itojun if (hasv4addr) {
724 1.1.2.1 itojun
725 1.1.2.1 itojun /* Attach v4 if notyet */
726 1.1.2.1 itojun if (inp == 0) {
727 1.1.2.1 itojun *error_p = udp_usrreq(so, PRU_ATTACH, 0, 0, 0);
728 1.1.2.1 itojun if (*error_p)
729 1.1.2.1 itojun return 2;
730 1.1.2.1 itojun inp = sotoinpcb(so);
731 1.1.2.1 itojun }
732 1.1.2.1 itojun if (sin6_p)
733 1.1.2.1 itojun in6_sin6_2_sin_in_m(addr6);
734 1.1.2.1 itojun *error_p = udp_output(inp, m, addr6, control);
735 1.1.2.1 itojun if (sin6_p)
736 1.1.2.1 itojun in6_sin_2_v4mapsin6_in_m(addr6);
737 1.1.2.1 itojun return 1;
738 1.1.2.1 itojun }
739 1.1.2.1 itojun return 0;
740 1.1.2.1 itojun }
741 1.1.2.1 itojun
742 1.1.2.1 itojun /*
743 1.1.2.1 itojun * Do V4 mapped addr related get sockaddr work.
744 1.1.2.1 itojun * Return 1 when v6 get sockaddr is not necessary.
745 1.1.2.1 itojun * Return 0 when v6 get sockaddr is necessary.
746 1.1.2.1 itojun */
747 1.1.2.1 itojun int
748 1.1.2.1 itojun udp6_mapped_sockaddr(struct socket *so, struct mbuf *addr6, int *error_p)
749 1.1.2.1 itojun {
750 1.1.2.1 itojun struct inpcb *inp = sotoinpcb(so);
751 1.1.2.1 itojun struct in6pcb *in6p = sotoin6pcb(so);
752 1.1.2.1 itojun int hasv4addr;
753 1.1.2.1 itojun
754 1.1.2.1 itojun if (inp == 0)
755 1.1.2.1 itojun return 0;
756 1.1.2.1 itojun hasv4addr = (inp->inp_laddr.s_addr == INADDR_ANY) ? 0 : 1;
757 1.1.2.1 itojun if (hasv4addr && IN6_IS_ADDR_ANY(&in6p->in6p_laddr)) {
758 1.1.2.1 itojun
759 1.1.2.1 itojun in_setsockaddr(inp, addr6);
760 1.1.2.1 itojun in6_sin_2_v4mapsin6_in_m(addr6);
761 1.1.2.1 itojun return 1;
762 1.1.2.1 itojun }
763 1.1.2.1 itojun return 0;
764 1.1.2.1 itojun }
765 1.1.2.1 itojun
766 1.1.2.1 itojun /*
767 1.1.2.1 itojun * Do V4 mapped addr related get peeraddr work.
768 1.1.2.1 itojun * Return 1 when v6 get peeraddr is not necessary.
769 1.1.2.1 itojun * Return 0 when v6 get peeraddr is necessary.
770 1.1.2.1 itojun */
771 1.1.2.1 itojun int
772 1.1.2.1 itojun udp6_mapped_peeraddr(struct socket *so, struct mbuf *addr6, int *error_p)
773 1.1.2.1 itojun {
774 1.1.2.1 itojun struct inpcb *inp = sotoinpcb(so);
775 1.1.2.1 itojun struct in6pcb *in6p = sotoin6pcb(so);
776 1.1.2.1 itojun int hasv4addr;
777 1.1.2.1 itojun
778 1.1.2.1 itojun if (inp == 0)
779 1.1.2.1 itojun return 0;
780 1.1.2.1 itojun hasv4addr = (inp->inp_faddr.s_addr == INADDR_ANY) ? 0 : 1;
781 1.1.2.1 itojun if (hasv4addr && IN6_IS_ADDR_ANY(&in6p->in6p_faddr)) {
782 1.1.2.1 itojun
783 1.1.2.1 itojun in_setpeeraddr(inp, addr6);
784 1.1.2.1 itojun in6_sin_2_v4mapsin6_in_m(addr6);
785 1.1.2.1 itojun return 1;
786 1.1.2.1 itojun }
787 1.1.2.1 itojun return 0;
788 1.1.2.1 itojun }
789 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
790 1.1.2.1 itojun
791 1.1.2.1 itojun extern int udp6_sendspace;
792 1.1.2.1 itojun extern int udp6_recvspace;
793 1.1.2.1 itojun
794 1.1.2.1 itojun int
795 1.1.2.1 itojun udp6_usrreq(so, req, m, addr6, control, p)
796 1.1.2.1 itojun struct socket *so;
797 1.1.2.1 itojun int req;
798 1.1.2.1 itojun struct mbuf *m, *addr6, *control;
799 1.1.2.1 itojun struct proc *p;
800 1.1.2.1 itojun {
801 1.1.2.1 itojun struct in6pcb *in6p = sotoin6pcb(so);
802 1.1.2.1 itojun int error = 0;
803 1.1.2.1 itojun int s;
804 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
805 1.1.2.1 itojun struct inpcb *inp = sotoinpcb(so);
806 1.1.2.1 itojun int hasv4addr;
807 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
808 1.1.2.1 itojun
809 1.1.2.1 itojun /*
810 1.1.2.1 itojun * MAPPED_ADDR implementation info:
811 1.1.2.1 itojun * Mapped addr support for PRU_CONTROL is not necessary.
812 1.1.2.1 itojun * Because typical user of PRU_CONTROL is such as ifconfig,
813 1.1.2.1 itojun * and they don't associate any addr to their socket. Then
814 1.1.2.1 itojun * socket family is only hint about the PRU_CONTROL'ed address
815 1.1.2.1 itojun * family, especially when getting addrs from kernel.
816 1.1.2.1 itojun * So AF_INET socket need to be used to control AF_INET addrs,
817 1.1.2.1 itojun * and AF_INET6 socket for AF_INET6 addrs.
818 1.1.2.1 itojun */
819 1.1.2.1 itojun if (req == PRU_CONTROL)
820 1.1.2.1 itojun return(in6_control(so, (u_long)m, (caddr_t)addr6,
821 1.1.2.1 itojun (struct ifnet *)control, p));
822 1.1.2.1 itojun
823 1.1.2.1 itojun if (in6p == NULL && req != PRU_ATTACH) {
824 1.1.2.1 itojun error = EINVAL;
825 1.1.2.1 itojun goto release;
826 1.1.2.1 itojun }
827 1.1.2.1 itojun
828 1.1.2.1 itojun switch (req) {
829 1.1.2.1 itojun case PRU_ATTACH:
830 1.1.2.1 itojun /*
831 1.1.2.1 itojun * MAPPED_ADDR implementation spec:
832 1.1.2.1 itojun * Always attach for IPv6,
833 1.1.2.1 itojun * and only when necessary for IPv4.
834 1.1.2.1 itojun */
835 1.1.2.1 itojun if (in6p != NULL
836 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
837 1.1.2.1 itojun /*
838 1.1.2.1 itojun * MAPPED_ADDR implementation spec:
839 1.1.2.1 itojun * When PRU_ATTACH, each of in6p and inp must be NULL.
840 1.1.2.1 itojun */
841 1.1.2.1 itojun || inp != NULL
842 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
843 1.1.2.1 itojun ) {
844 1.1.2.1 itojun error = EINVAL;
845 1.1.2.1 itojun break;
846 1.1.2.1 itojun }
847 1.1.2.1 itojun s = splnet();
848 1.1.2.1 itojun error = in6_pcballoc(so, &udb6);
849 1.1.2.1 itojun splx(s);
850 1.1.2.1 itojun if (error)
851 1.1.2.1 itojun break;
852 1.1.2.1 itojun error = soreserve(so, udp6_sendspace, udp6_recvspace);
853 1.1.2.1 itojun if (error)
854 1.1.2.1 itojun break;
855 1.1.2.1 itojun in6p = sotoin6pcb(so);
856 1.1.2.1 itojun in6p->in6p_ip6.ip6_hlim = ip6_defhlim;
857 1.1.2.1 itojun in6p->in6p_cksum = -1; /* just to be sure */
858 1.1.2.1 itojun #ifdef IPSEC
859 1.1.2.1 itojun error = ipsec_init_policy(&in6p->in6p_sp);
860 1.1.2.1 itojun #endif /*IPSEC*/
861 1.1.2.1 itojun break;
862 1.1.2.1 itojun
863 1.1.2.1 itojun case PRU_DETACH:
864 1.1.2.1 itojun udp6_detach(in6p);
865 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
866 1.1.2.1 itojun if (inp)
867 1.1.2.1 itojun udp_detach(inp);
868 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
869 1.1.2.1 itojun break;
870 1.1.2.1 itojun
871 1.1.2.1 itojun case PRU_BIND:
872 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
873 1.1.2.1 itojun if (ip6_mapped_addr_on)
874 1.1.2.1 itojun if (udp6_mapped_bind(so, addr6, &error))
875 1.1.2.1 itojun break;
876 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
877 1.1.2.1 itojun s = splnet();
878 1.1.2.1 itojun error = in6_pcbbind(in6p, addr6);
879 1.1.2.1 itojun splx(s);
880 1.1.2.1 itojun break;
881 1.1.2.1 itojun
882 1.1.2.1 itojun case PRU_LISTEN:
883 1.1.2.1 itojun error = EOPNOTSUPP;
884 1.1.2.1 itojun break;
885 1.1.2.1 itojun
886 1.1.2.1 itojun case PRU_CONNECT:
887 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
888 1.1.2.1 itojun if (ip6_mapped_addr_on)
889 1.1.2.1 itojun if (udp6_mapped_connect(so, addr6, &error))
890 1.1.2.1 itojun break;
891 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
892 1.1.2.1 itojun if (!IN6_IS_ADDR_ANY(&in6p->in6p_faddr)) {
893 1.1.2.1 itojun error = EISCONN;
894 1.1.2.1 itojun break;
895 1.1.2.1 itojun }
896 1.1.2.1 itojun s = splnet();
897 1.1.2.1 itojun error = in6_pcbconnect(in6p, addr6);
898 1.1.2.1 itojun if (ip6_auto_flowlabel) {
899 1.1.2.1 itojun in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
900 1.1.2.1 itojun in6p->in6p_flowinfo |=
901 1.1.2.1 itojun (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
902 1.1.2.1 itojun }
903 1.1.2.1 itojun splx(s);
904 1.1.2.1 itojun if (error == 0)
905 1.1.2.1 itojun soisconnected(so);
906 1.1.2.1 itojun break;
907 1.1.2.1 itojun
908 1.1.2.1 itojun case PRU_CONNECT2:
909 1.1.2.1 itojun error = EOPNOTSUPP;
910 1.1.2.1 itojun break;
911 1.1.2.1 itojun
912 1.1.2.1 itojun case PRU_ACCEPT:
913 1.1.2.1 itojun error = EOPNOTSUPP;
914 1.1.2.1 itojun break;
915 1.1.2.1 itojun
916 1.1.2.1 itojun case PRU_DISCONNECT:
917 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
918 1.1.2.1 itojun /*
919 1.1.2.1 itojun * Should only one of IPv4 or IPv6 is connected at most,
920 1.1.2.1 itojun * but we check either and (if connected)disconnect them.
921 1.1.2.1 itojun */
922 1.1.2.1 itojun hasv4addr = udp6_mapped_disconnect(so, addr6, &error);
923 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
924 1.1.2.1 itojun if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
925 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
926 1.1.2.1 itojun if (hasv4addr == 0)
927 1.1.2.1 itojun error = ENOTCONN;
928 1.1.2.1 itojun else
929 1.1.2.1 itojun so->so_state &= ~SS_ISCONNECTED; /* XXX */
930 1.1.2.1 itojun #else /* MAPPED_ADDR_ENABLED */
931 1.1.2.1 itojun error = ENOTCONN;
932 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
933 1.1.2.1 itojun break;
934 1.1.2.1 itojun }
935 1.1.2.1 itojun s = splnet();
936 1.1.2.1 itojun in6_pcbdisconnect(in6p);
937 1.1.2.1 itojun bzero((caddr_t)&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
938 1.1.2.1 itojun splx(s);
939 1.1.2.1 itojun so->so_state &= ~SS_ISCONNECTED; /* XXX */
940 1.1.2.1 itojun break;
941 1.1.2.1 itojun
942 1.1.2.1 itojun case PRU_SHUTDOWN:
943 1.1.2.1 itojun socantsendmore(so);
944 1.1.2.1 itojun break;
945 1.1.2.1 itojun
946 1.1.2.1 itojun case PRU_SEND:
947 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
948 1.1.2.1 itojun if (ip6_mapped_addr_on) {
949 1.1.2.1 itojun int ret_val;
950 1.1.2.1 itojun
951 1.1.2.1 itojun ret_val = udp6_mapped_send(so, m, addr6, control,
952 1.1.2.1 itojun &error);
953 1.1.2.1 itojun if (ret_val != 0) {
954 1.1.2.1 itojun if (ret_val == 1)
955 1.1.2.1 itojun return (error);
956 1.1.2.1 itojun else /* ret_val shoud be 2. v4 attach failed */
957 1.1.2.1 itojun break;
958 1.1.2.1 itojun }
959 1.1.2.1 itojun /* else proceed to udp6_output() */
960 1.1.2.1 itojun }
961 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
962 1.1.2.1 itojun return(udp6_output(in6p, m, addr6, control));
963 1.1.2.1 itojun
964 1.1.2.1 itojun case PRU_ABORT:
965 1.1.2.1 itojun soisdisconnected(so);
966 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
967 1.1.2.1 itojun if (inp)
968 1.1.2.1 itojun udp_detach(inp);
969 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
970 1.1.2.1 itojun udp6_detach(in6p);
971 1.1.2.1 itojun break;
972 1.1.2.1 itojun
973 1.1.2.1 itojun case PRU_SOCKADDR:
974 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
975 1.1.2.1 itojun if (ip6_mapped_addr_on)
976 1.1.2.1 itojun if (udp6_mapped_sockaddr(so, addr6, &error))
977 1.1.2.1 itojun break;
978 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
979 1.1.2.1 itojun in6_setsockaddr(in6p, addr6);
980 1.1.2.1 itojun break;
981 1.1.2.1 itojun
982 1.1.2.1 itojun case PRU_PEERADDR:
983 1.1.2.1 itojun #ifdef MAPPED_ADDR_ENABLED
984 1.1.2.1 itojun if (ip6_mapped_addr_on)
985 1.1.2.1 itojun if (udp6_mapped_peeraddr(so, addr6, &error))
986 1.1.2.1 itojun break;
987 1.1.2.1 itojun #endif /* MAPPED_ADDR_ENABLED */
988 1.1.2.1 itojun in6_setpeeraddr(in6p, addr6);
989 1.1.2.1 itojun break;
990 1.1.2.1 itojun
991 1.1.2.1 itojun case PRU_SENSE:
992 1.1.2.1 itojun /*
993 1.1.2.1 itojun * stat: don't bother with a blocksize
994 1.1.2.1 itojun */
995 1.1.2.1 itojun return(0);
996 1.1.2.1 itojun
997 1.1.2.1 itojun case PRU_SENDOOB:
998 1.1.2.1 itojun case PRU_FASTTIMO:
999 1.1.2.1 itojun case PRU_SLOWTIMO:
1000 1.1.2.1 itojun case PRU_PROTORCV:
1001 1.1.2.1 itojun case PRU_PROTOSEND:
1002 1.1.2.1 itojun error = EOPNOTSUPP;
1003 1.1.2.1 itojun break;
1004 1.1.2.1 itojun
1005 1.1.2.1 itojun case PRU_RCVD:
1006 1.1.2.1 itojun case PRU_RCVOOB:
1007 1.1.2.1 itojun return(EOPNOTSUPP); /* do not free mbuf's */
1008 1.1.2.1 itojun
1009 1.1.2.1 itojun default:
1010 1.1.2.1 itojun panic("udp6_usrreq");
1011 1.1.2.1 itojun }
1012 1.1.2.1 itojun
1013 1.1.2.1 itojun release:
1014 1.1.2.1 itojun if (control) {
1015 1.1.2.1 itojun printf("udp control data unexpectedly retained\n");
1016 1.1.2.1 itojun m_freem(control);
1017 1.1.2.1 itojun }
1018 1.1.2.1 itojun if (m)
1019 1.1.2.1 itojun m_freem(m);
1020 1.1.2.1 itojun return(error);
1021 1.1.2.1 itojun }
1022 1.1.2.1 itojun
1023 1.1.2.1 itojun static void
1024 1.1.2.1 itojun udp6_detach(in6p)
1025 1.1.2.1 itojun struct in6pcb *in6p;
1026 1.1.2.1 itojun {
1027 1.1.2.1 itojun int s = splnet();
1028 1.1.2.1 itojun
1029 1.1.2.1 itojun if (in6p == udp6_last_in6pcb)
1030 1.1.2.1 itojun udp6_last_in6pcb = &udb6;
1031 1.1.2.1 itojun in6_pcbdetach(in6p);
1032 1.1.2.1 itojun splx(s);
1033 1.1.2.1 itojun }
1034 1.1.2.1 itojun
1035 1.1.2.1 itojun #ifdef __bsdi__
1036 1.1.2.1 itojun int *udp6_sysvars[] = UDP6CTL_VARS;
1037 1.1.2.1 itojun
1038 1.1.2.1 itojun int
1039 1.1.2.1 itojun udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1040 1.1.2.1 itojun int *name;
1041 1.1.2.1 itojun u_int namelen;
1042 1.1.2.1 itojun void *oldp;
1043 1.1.2.1 itojun size_t *oldlenp;
1044 1.1.2.1 itojun void *newp;
1045 1.1.2.1 itojun size_t newlen;
1046 1.1.2.1 itojun {
1047 1.1.2.1 itojun if (name[0] >= UDP6CTL_MAXID)
1048 1.1.2.1 itojun return (EOPNOTSUPP);
1049 1.1.2.1 itojun switch (name[0]) {
1050 1.1.2.1 itojun case UDP6CTL_STATS:
1051 1.1.2.1 itojun return sysctl_rdtrunc(oldp, oldlenp, newp, &udp6stat,
1052 1.1.2.1 itojun sizeof(udp6stat));
1053 1.1.2.1 itojun
1054 1.1.2.1 itojun default:
1055 1.1.2.1 itojun return (sysctl_int_arr(udp6_sysvars, name, namelen,
1056 1.1.2.1 itojun oldp, oldlenp, newp, newlen));
1057 1.1.2.1 itojun }
1058 1.1.2.1 itojun }
1059 1.1.2.1 itojun #endif /*__bsdi__*/
1060 1.1.2.1 itojun
1061 1.1.2.1 itojun #ifdef __NetBSD__
1062 1.1.2.1 itojun #include <vm/vm.h>
1063 1.1.2.1 itojun #include <sys/sysctl.h>
1064 1.1.2.1 itojun
1065 1.1.2.1 itojun int
1066 1.1.2.1 itojun udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1067 1.1.2.1 itojun int *name;
1068 1.1.2.1 itojun u_int namelen;
1069 1.1.2.1 itojun void *oldp;
1070 1.1.2.1 itojun size_t *oldlenp;
1071 1.1.2.1 itojun void *newp;
1072 1.1.2.1 itojun size_t newlen;
1073 1.1.2.1 itojun {
1074 1.1.2.1 itojun /* All sysctl names at this level are terminal. */
1075 1.1.2.1 itojun if (namelen != 1)
1076 1.1.2.1 itojun return ENOTDIR;
1077 1.1.2.1 itojun
1078 1.1.2.1 itojun switch (name[0]) {
1079 1.1.2.1 itojun
1080 1.1.2.1 itojun case UDP6CTL_SENDMAX:
1081 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1082 1.1.2.1 itojun &udp6_sendspace);
1083 1.1.2.1 itojun case UDP6CTL_RECVSPACE:
1084 1.1.2.1 itojun return sysctl_int(oldp, oldlenp, newp, newlen,
1085 1.1.2.1 itojun &udp6_recvspace);
1086 1.1.2.1 itojun default:
1087 1.1.2.1 itojun return ENOPROTOOPT;
1088 1.1.2.1 itojun }
1089 1.1.2.1 itojun /* NOTREACHED */
1090 1.1.2.1 itojun }
1091 1.1.2.1 itojun #endif
1092