udp6_usrreq.c revision 1.55.2.1 1 /* $NetBSD: udp6_usrreq.c,v 1.55.2.1 2003/07/02 15:27:02 darrenr Exp $ */
2 /* $KAME: udp6_usrreq.c,v 1.86 2001/05/27 17:33:00 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1989, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: udp6_usrreq.c,v 1.55.2.1 2003/07/02 15:27:02 darrenr Exp $");
70
71 #include "opt_ipsec.h"
72
73 #include <sys/param.h>
74 #include <sys/malloc.h>
75 #include <sys/mbuf.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/errno.h>
80 #include <sys/stat.h>
81 #include <sys/systm.h>
82 #include <sys/proc.h>
83 #include <sys/syslog.h>
84 #include <sys/sysctl.h>
85
86 #include <net/if.h>
87 #include <net/route.h>
88 #include <net/if_types.h>
89
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/ip_var.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet/udp.h>
97 #include <netinet/udp_var.h>
98 #include <netinet/ip6.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/in6_pcb.h>
101 #include <netinet/icmp6.h>
102 #include <netinet6/udp6_var.h>
103 #include <netinet6/ip6protosw.h>
104
105 #ifdef IPSEC
106 #include <netinet6/ipsec.h>
107 #endif /* IPSEC */
108
109 #include "faith.h"
110 #if defined(NFAITH) && NFAITH > 0
111 #include <net/if_faith.h>
112 #endif
113
114 /*
115 * UDP protocol inplementation.
116 * Per RFC 768, August, 1980.
117 */
118
119 struct in6pcb udb6;
120 struct in6pcb *udp6_last_in6pcb = &udb6;
121 struct udp6stat udp6stat;
122
123 static void udp6_detach __P((struct in6pcb *));
124 static void udp6_notify __P((struct in6pcb *, int));
125
126 void
127 udp6_init()
128 {
129 udb6.in6p_next = udb6.in6p_prev = &udb6;
130 }
131
132 /*
133 * Notify a udp user of an asynchronous error;
134 * just wake up so that he can collect error status.
135 */
136 static void
137 udp6_notify(in6p, errno)
138 struct in6pcb *in6p;
139 int errno;
140 {
141 in6p->in6p_socket->so_error = errno;
142 sorwakeup(in6p->in6p_socket);
143 sowwakeup(in6p->in6p_socket);
144 }
145
146 void
147 udp6_ctlinput(cmd, sa, d)
148 int cmd;
149 struct sockaddr *sa;
150 void *d;
151 {
152 struct udphdr uh;
153 struct ip6_hdr *ip6;
154 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
155 struct mbuf *m;
156 int off;
157 void *cmdarg;
158 struct ip6ctlparam *ip6cp = NULL;
159 const struct sockaddr_in6 *sa6_src = NULL;
160 void (*notify) __P((struct in6pcb *, int)) = udp6_notify;
161 struct udp_portonly {
162 u_int16_t uh_sport;
163 u_int16_t uh_dport;
164 } *uhp;
165
166 if (sa->sa_family != AF_INET6 ||
167 sa->sa_len != sizeof(struct sockaddr_in6))
168 return;
169
170 if ((unsigned)cmd >= PRC_NCMDS)
171 return;
172 if (PRC_IS_REDIRECT(cmd))
173 notify = in6_rtchange, d = NULL;
174 else if (cmd == PRC_HOSTDEAD)
175 d = NULL;
176 else if (cmd == PRC_MSGSIZE) {
177 /* special code is present, see below */
178 notify = in6_rtchange;
179 }
180 else if (inet6ctlerrmap[cmd] == 0)
181 return;
182
183 /* if the parameter is from icmp6, decode it. */
184 if (d != NULL) {
185 ip6cp = (struct ip6ctlparam *)d;
186 m = ip6cp->ip6c_m;
187 ip6 = ip6cp->ip6c_ip6;
188 off = ip6cp->ip6c_off;
189 cmdarg = ip6cp->ip6c_cmdarg;
190 sa6_src = ip6cp->ip6c_src;
191 } else {
192 m = NULL;
193 ip6 = NULL;
194 cmdarg = NULL;
195 sa6_src = &sa6_any;
196 }
197
198 if (ip6) {
199 /*
200 * XXX: We assume that when IPV6 is non NULL,
201 * M and OFF are valid.
202 */
203
204 /* check if we can safely examine src and dst ports */
205 if (m->m_pkthdr.len < off + sizeof(*uhp)) {
206 if (cmd == PRC_MSGSIZE)
207 icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
208 return;
209 }
210
211 bzero(&uh, sizeof(uh));
212 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
213
214 if (cmd == PRC_MSGSIZE) {
215 int valid = 0;
216
217 /*
218 * Check to see if we have a valid UDP socket
219 * corresponding to the address in the ICMPv6 message
220 * payload.
221 */
222 if (in6_pcblookup_connect(&udb6, &sa6->sin6_addr,
223 uh.uh_dport, (struct in6_addr *)&sa6_src->sin6_addr,
224 uh.uh_sport, 0))
225 valid++;
226 #if 0
227 /*
228 * As the use of sendto(2) is fairly popular,
229 * we may want to allow non-connected pcb too.
230 * But it could be too weak against attacks...
231 * We should at least check if the local address (= s)
232 * is really ours.
233 */
234 else if (in6_pcblookup_bind(&udb6, &sa6->sin6_addr,
235 uh.uh_dport, 0))
236 valid++;
237 #endif
238
239 /*
240 * Depending on the value of "valid" and routing table
241 * size (mtudisc_{hi,lo}wat), we will:
242 * - recalculate the new MTU and create the
243 * corresponding routing entry, or
244 * - ignore the MTU change notification.
245 */
246 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
247
248 /*
249 * regardless of if we called icmp6_mtudisc_update(),
250 * we need to call in6_pcbnotify(), to notify path
251 * MTU change to the userland (2292bis-02), because
252 * some unconnected sockets may share the same
253 * destination and want to know the path MTU.
254 */
255 }
256
257 (void) in6_pcbnotify(&udb6, sa, uh.uh_dport,
258 (struct sockaddr *)sa6_src, uh.uh_sport, cmd, cmdarg,
259 notify);
260 } else {
261 (void) in6_pcbnotify(&udb6, sa, 0, (struct sockaddr *)sa6_src,
262 0, cmd, cmdarg, notify);
263 }
264 }
265
266 extern int udp6_sendspace;
267 extern int udp6_recvspace;
268
269 int
270 udp6_usrreq(so, req, m, addr6, control, l)
271 struct socket *so;
272 int req;
273 struct mbuf *m, *addr6, *control;
274 struct lwp *l;
275 {
276 struct in6pcb *in6p = sotoin6pcb(so);
277 struct proc *p;
278 int error = 0;
279 int s;
280
281 p = l ? l->l_proc : NULL;
282 /*
283 * MAPPED_ADDR implementation info:
284 * Mapped addr support for PRU_CONTROL is not necessary.
285 * Because typical user of PRU_CONTROL is such as ifconfig,
286 * and they don't associate any addr to their socket. Then
287 * socket family is only hint about the PRU_CONTROL'ed address
288 * family, especially when getting addrs from kernel.
289 * So AF_INET socket need to be used to control AF_INET addrs,
290 * and AF_INET6 socket for AF_INET6 addrs.
291 */
292 if (req == PRU_CONTROL)
293 return (in6_control(so, (u_long)m, (caddr_t)addr6,
294 (struct ifnet *)control, p));
295
296 if (req == PRU_PURGEIF) {
297 in6_pcbpurgeif0(&udb6, (struct ifnet *)control);
298 in6_purgeif((struct ifnet *)control);
299 in6_pcbpurgeif(&udb6, (struct ifnet *)control);
300 return (0);
301 }
302
303 if (in6p == NULL && req != PRU_ATTACH) {
304 error = EINVAL;
305 goto release;
306 }
307
308 switch (req) {
309 case PRU_ATTACH:
310 /*
311 * MAPPED_ADDR implementation spec:
312 * Always attach for IPv6,
313 * and only when necessary for IPv4.
314 */
315 if (in6p != NULL) {
316 error = EINVAL;
317 break;
318 }
319 s = splsoftnet();
320 error = in6_pcballoc(so, &udb6);
321 splx(s);
322 if (error)
323 break;
324 error = soreserve(so, udp6_sendspace, udp6_recvspace);
325 if (error)
326 break;
327 in6p = sotoin6pcb(so);
328 in6p->in6p_cksum = -1; /* just to be sure */
329 break;
330
331 case PRU_DETACH:
332 udp6_detach(in6p);
333 break;
334
335 case PRU_BIND:
336 s = splsoftnet();
337 error = in6_pcbbind(in6p, addr6, p);
338 splx(s);
339 break;
340
341 case PRU_LISTEN:
342 error = EOPNOTSUPP;
343 break;
344
345 case PRU_CONNECT:
346 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
347 error = EISCONN;
348 break;
349 }
350 s = splsoftnet();
351 error = in6_pcbconnect(in6p, addr6);
352 if (ip6_auto_flowlabel) {
353 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
354 in6p->in6p_flowinfo |=
355 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
356 }
357 splx(s);
358 if (error == 0)
359 soisconnected(so);
360 break;
361
362 case PRU_CONNECT2:
363 error = EOPNOTSUPP;
364 break;
365
366 case PRU_ACCEPT:
367 error = EOPNOTSUPP;
368 break;
369
370 case PRU_DISCONNECT:
371 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
372 error = ENOTCONN;
373 break;
374 }
375 s = splsoftnet();
376 in6_pcbdisconnect(in6p);
377 bzero((caddr_t)&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
378 splx(s);
379 so->so_state &= ~SS_ISCONNECTED; /* XXX */
380 break;
381
382 case PRU_SHUTDOWN:
383 socantsendmore(so);
384 break;
385
386 case PRU_SEND:
387 return (udp6_output(in6p, m, addr6, control, p));
388
389 case PRU_ABORT:
390 soisdisconnected(so);
391 udp6_detach(in6p);
392 break;
393
394 case PRU_SOCKADDR:
395 in6_setsockaddr(in6p, addr6);
396 break;
397
398 case PRU_PEERADDR:
399 in6_setpeeraddr(in6p, addr6);
400 break;
401
402 case PRU_SENSE:
403 /*
404 * stat: don't bother with a blocksize
405 */
406 return (0);
407
408 case PRU_SENDOOB:
409 case PRU_FASTTIMO:
410 case PRU_SLOWTIMO:
411 case PRU_PROTORCV:
412 case PRU_PROTOSEND:
413 error = EOPNOTSUPP;
414 break;
415
416 case PRU_RCVD:
417 case PRU_RCVOOB:
418 return (EOPNOTSUPP); /* do not free mbuf's */
419
420 default:
421 panic("udp6_usrreq");
422 }
423
424 release:
425 if (control)
426 m_freem(control);
427 if (m)
428 m_freem(m);
429 return (error);
430 }
431
432 static void
433 udp6_detach(in6p)
434 struct in6pcb *in6p;
435 {
436 int s = splsoftnet();
437
438 if (in6p == udp6_last_in6pcb)
439 udp6_last_in6pcb = &udb6;
440 in6_pcbdetach(in6p);
441 splx(s);
442 }
443
444 int
445 udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
446 int *name;
447 u_int namelen;
448 void *oldp;
449 size_t *oldlenp;
450 void *newp;
451 size_t newlen;
452 {
453 /* All sysctl names at this level are terminal. */
454 if (namelen != 1)
455 return ENOTDIR;
456
457 switch (name[0]) {
458
459 case UDP6CTL_SENDSPACE:
460 return sysctl_int(oldp, oldlenp, newp, newlen,
461 &udp6_sendspace);
462 case UDP6CTL_RECVSPACE:
463 return sysctl_int(oldp, oldlenp, newp, newlen,
464 &udp6_recvspace);
465 default:
466 return ENOPROTOOPT;
467 }
468 /* NOTREACHED */
469 }
470