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