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