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