udp6_usrreq.c revision 1.90 1 /* $NetBSD: udp6_usrreq.c,v 1.90 2011/09/24 17:22:14 christos 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.90 2011/09/24 17:22:14 christos 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/domain.h>
81 #include <sys/sysctl.h>
82
83 #include <net/if.h>
84 #include <net/route.h>
85 #include <net/if_types.h>
86
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/udp.h>
94 #include <netinet/udp_var.h>
95 #include <netinet/rfc6056.h>
96 #include <netinet/ip6.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/in6_pcb.h>
99 #include <netinet/icmp6.h>
100 #include <netinet6/udp6_var.h>
101 #include <netinet6/udp6_private.h>
102 #include <netinet6/ip6protosw.h>
103 #include <netinet/in_offload.h>
104
105 #include "faith.h"
106 #if defined(NFAITH) && NFAITH > 0
107 #include <net/if_faith.h>
108 #endif
109
110 /*
111 * UDP protocol implementation.
112 * Per RFC 768, August, 1980.
113 */
114
115 extern struct inpcbtable udbtable;
116
117 percpu_t *udp6stat_percpu;
118
119 static void udp6_notify(struct in6pcb *, int);
120 static void sysctl_net_inet6_udp6_setup(struct sysctllog **);
121
122 void
123 udp6_init(void)
124 {
125
126 sysctl_net_inet6_udp6_setup(NULL);
127 }
128
129 /*
130 * Notify a udp user of an asynchronous error;
131 * just wake up so that he can collect error status.
132 */
133 static void
134 udp6_notify(struct in6pcb *in6p, int errno)
135 {
136 in6p->in6p_socket->so_error = errno;
137 sorwakeup(in6p->in6p_socket);
138 sowwakeup(in6p->in6p_socket);
139 }
140
141 void *
142 udp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
143 {
144 struct udphdr uh;
145 struct ip6_hdr *ip6;
146 const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
147 struct mbuf *m;
148 int off;
149 void *cmdarg;
150 struct ip6ctlparam *ip6cp = NULL;
151 const struct sockaddr_in6 *sa6_src = NULL;
152 void (*notify)(struct in6pcb *, int) = udp6_notify;
153 struct udp_portonly {
154 u_int16_t uh_sport;
155 u_int16_t uh_dport;
156 } *uhp;
157
158 if (sa->sa_family != AF_INET6 ||
159 sa->sa_len != sizeof(struct sockaddr_in6))
160 return NULL;
161
162 if ((unsigned)cmd >= PRC_NCMDS)
163 return NULL;
164 if (PRC_IS_REDIRECT(cmd))
165 notify = in6_rtchange, d = NULL;
166 else if (cmd == PRC_HOSTDEAD)
167 d = NULL;
168 else if (cmd == PRC_MSGSIZE) {
169 /* special code is present, see below */
170 notify = in6_rtchange;
171 }
172 else if (inet6ctlerrmap[cmd] == 0)
173 return NULL;
174
175 /* if the parameter is from icmp6, decode it. */
176 if (d != NULL) {
177 ip6cp = (struct ip6ctlparam *)d;
178 m = ip6cp->ip6c_m;
179 ip6 = ip6cp->ip6c_ip6;
180 off = ip6cp->ip6c_off;
181 cmdarg = ip6cp->ip6c_cmdarg;
182 sa6_src = ip6cp->ip6c_src;
183 } else {
184 m = NULL;
185 ip6 = NULL;
186 cmdarg = NULL;
187 sa6_src = &sa6_any;
188 off = 0;
189 }
190
191 if (ip6) {
192 /*
193 * XXX: We assume that when IPV6 is non NULL,
194 * M and OFF are valid.
195 */
196
197 /* check if we can safely examine src and dst ports */
198 if (m->m_pkthdr.len < off + sizeof(*uhp)) {
199 if (cmd == PRC_MSGSIZE)
200 icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
201 return NULL;
202 }
203
204 memset(&uh, 0, sizeof(uh));
205 m_copydata(m, off, sizeof(*uhp), (void *)&uh);
206
207 if (cmd == PRC_MSGSIZE) {
208 int valid = 0;
209
210 /*
211 * Check to see if we have a valid UDP socket
212 * corresponding to the address in the ICMPv6 message
213 * payload.
214 */
215 if (in6_pcblookup_connect(&udbtable, &sa6->sin6_addr,
216 uh.uh_dport, (const struct in6_addr *)&sa6_src->sin6_addr,
217 uh.uh_sport, 0, 0))
218 valid++;
219 #if 0
220 /*
221 * As the use of sendto(2) is fairly popular,
222 * we may want to allow non-connected pcb too.
223 * But it could be too weak against attacks...
224 * We should at least check if the local address (= s)
225 * is really ours.
226 */
227 else if (in6_pcblookup_bind(&udbtable, &sa6->sin6_addr,
228 uh.uh_dport, 0))
229 valid++;
230 #endif
231
232 /*
233 * Depending on the value of "valid" and routing table
234 * size (mtudisc_{hi,lo}wat), we will:
235 * - recalculate the new MTU and create the
236 * corresponding routing entry, or
237 * - ignore the MTU change notification.
238 */
239 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
240
241 /*
242 * regardless of if we called
243 * icmp6_mtudisc_update(), we need to call
244 * in6_pcbnotify(), to notify path MTU change
245 * to the userland (RFC3542), because some
246 * unconnected sockets may share the same
247 * destination and want to know the path MTU.
248 */
249 }
250
251 (void) in6_pcbnotify(&udbtable, sa, uh.uh_dport,
252 (const struct sockaddr *)sa6_src, uh.uh_sport, cmd, cmdarg,
253 notify);
254 } else {
255 (void) in6_pcbnotify(&udbtable, sa, 0,
256 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
257 }
258 return NULL;
259 }
260
261 int
262 udp6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
263 {
264 int s;
265 int error = 0;
266 struct inpcb *inp;
267 int family;
268 int optval;
269
270 family = so->so_proto->pr_domain->dom_family;
271
272 s = splsoftnet();
273 switch (family) {
274 #ifdef INET
275 case PF_INET:
276 if (sopt->sopt_level != IPPROTO_UDP) {
277 error = ip_ctloutput(op, so, sopt);
278 goto end;
279 }
280 break;
281 #endif
282 #ifdef INET6
283 case PF_INET6:
284 if (sopt->sopt_level != IPPROTO_UDP) {
285 error = ip6_ctloutput(op, so, sopt);
286 goto end;
287 }
288 break;
289 #endif
290 default:
291 error = EAFNOSUPPORT;
292 goto end;
293 }
294
295 switch (op) {
296 case PRCO_SETOPT:
297 inp = sotoinpcb(so);
298
299 switch (sopt->sopt_name) {
300 case UDP_RFC6056ALGO:
301 error = sockopt_getint(sopt, &optval);
302 if (error)
303 break;
304
305 error = rfc6056_algo_index_select(
306 (struct inpcb_hdr *)inp, optval);
307 break;
308
309 default:
310 error = ENOPROTOOPT;
311 break;
312 }
313 break;
314
315 default:
316 error = EINVAL;
317 break;
318 }
319
320 end:
321 splx(s);
322 return error;
323 }
324
325
326 extern int udp6_sendspace;
327 extern int udp6_recvspace;
328
329 int
330 udp6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr6,
331 struct mbuf *control, struct lwp *l)
332 {
333 struct in6pcb *in6p = sotoin6pcb(so);
334 int error = 0;
335 int s;
336
337 /*
338 * MAPPED_ADDR implementation info:
339 * Mapped addr support for PRU_CONTROL is not necessary.
340 * Because typical user of PRU_CONTROL is such as ifconfig,
341 * and they don't associate any addr to their socket. Then
342 * socket family is only hint about the PRU_CONTROL'ed address
343 * family, especially when getting addrs from kernel.
344 * So AF_INET socket need to be used to control AF_INET addrs,
345 * and AF_INET6 socket for AF_INET6 addrs.
346 */
347 if (req == PRU_CONTROL)
348 return in6_control(so, (u_long)m, (void *)addr6,
349 (struct ifnet *)control, l);
350
351 if (req == PRU_PURGEIF) {
352 mutex_enter(softnet_lock);
353 in6_pcbpurgeif0(&udbtable, (struct ifnet *)control);
354 in6_purgeif((struct ifnet *)control);
355 in6_pcbpurgeif(&udbtable, (struct ifnet *)control);
356 mutex_exit(softnet_lock);
357 return 0;
358 }
359
360 if (req == PRU_ATTACH)
361 sosetlock(so);
362 else if (in6p == NULL) {
363 error = EINVAL;
364 goto release;
365 }
366
367 switch (req) {
368 case PRU_ATTACH:
369 /*
370 * MAPPED_ADDR implementation spec:
371 * Always attach for IPv6,
372 * and only when necessary for IPv4.
373 */
374 if (in6p != NULL) {
375 error = EINVAL;
376 break;
377 }
378 s = splsoftnet();
379 error = in6_pcballoc(so, &udbtable);
380 splx(s);
381 if (error)
382 break;
383 error = soreserve(so, udp6_sendspace, udp6_recvspace);
384 if (error)
385 break;
386 in6p = sotoin6pcb(so);
387 in6p->in6p_cksum = -1; /* just to be sure */
388 break;
389
390 case PRU_DETACH:
391 in6_pcbdetach(in6p);
392 break;
393
394 case PRU_BIND:
395 s = splsoftnet();
396 error = in6_pcbbind(in6p, addr6, l);
397 splx(s);
398 break;
399
400 case PRU_CONNECT:
401 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
402 error = EISCONN;
403 break;
404 }
405 s = splsoftnet();
406 error = in6_pcbconnect(in6p, addr6, l);
407 splx(s);
408 if (error == 0)
409 soisconnected(so);
410 break;
411
412 case PRU_DISCONNECT:
413 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
414 error = ENOTCONN;
415 break;
416 }
417 s = splsoftnet();
418 in6_pcbdisconnect(in6p);
419 memset((void *)&in6p->in6p_laddr, 0, sizeof(in6p->in6p_laddr));
420 splx(s);
421 so->so_state &= ~SS_ISCONNECTED; /* XXX */
422 in6_pcbstate(in6p, IN6P_BOUND); /* XXX */
423 break;
424
425 case PRU_SHUTDOWN:
426 socantsendmore(so);
427 break;
428
429 case PRU_SEND:
430 s = splsoftnet();
431 error = udp6_output(in6p, m, addr6, control, l);
432 splx(s);
433 return error;
434
435 case PRU_ABORT:
436 soisdisconnected(so);
437 in6_pcbdetach(in6p);
438 break;
439
440 case PRU_SOCKADDR:
441 in6_setsockaddr(in6p, addr6);
442 break;
443
444 case PRU_PEERADDR:
445 in6_setpeeraddr(in6p, addr6);
446 break;
447
448 case PRU_SENSE:
449 /*
450 * stat: don't bother with a blocksize
451 */
452 return 0;
453
454 case PRU_LISTEN:
455 case PRU_CONNECT2:
456 case PRU_ACCEPT:
457 case PRU_SENDOOB:
458 case PRU_FASTTIMO:
459 case PRU_SLOWTIMO:
460 case PRU_PROTORCV:
461 case PRU_PROTOSEND:
462 error = EOPNOTSUPP;
463 break;
464
465 case PRU_RCVD:
466 case PRU_RCVOOB:
467 return EOPNOTSUPP; /* do not free mbuf's */
468
469 default:
470 panic("udp6_usrreq");
471 }
472
473 release:
474 if (control != NULL)
475 m_freem(control);
476 if (m != NULL)
477 m_freem(m);
478 return error;
479 }
480
481 static int
482 sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS)
483 {
484
485 return (NETSTAT_SYSCTL(udp6stat_percpu, UDP6_NSTATS));
486 }
487
488 static void
489 sysctl_net_inet6_udp6_setup(struct sysctllog **clog)
490 {
491 const struct sysctlnode *rfc6056_node;
492
493 sysctl_createv(clog, 0, NULL, NULL,
494 CTLFLAG_PERMANENT,
495 CTLTYPE_NODE, "net", NULL,
496 NULL, 0, NULL, 0,
497 CTL_NET, CTL_EOL);
498 sysctl_createv(clog, 0, NULL, NULL,
499 CTLFLAG_PERMANENT,
500 CTLTYPE_NODE, "inet6", NULL,
501 NULL, 0, NULL, 0,
502 CTL_NET, PF_INET6, CTL_EOL);
503 sysctl_createv(clog, 0, NULL, NULL,
504 CTLFLAG_PERMANENT,
505 CTLTYPE_NODE, "udp6",
506 SYSCTL_DESCR("UDPv6 related settings"),
507 NULL, 0, NULL, 0,
508 CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
509
510 sysctl_createv(clog, 0, NULL, NULL,
511 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
512 CTLTYPE_INT, "sendspace",
513 SYSCTL_DESCR("Default UDP send buffer size"),
514 NULL, 0, &udp6_sendspace, 0,
515 CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
516 CTL_EOL);
517 sysctl_createv(clog, 0, NULL, NULL,
518 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
519 CTLTYPE_INT, "recvspace",
520 SYSCTL_DESCR("Default UDP receive buffer size"),
521 NULL, 0, &udp6_recvspace, 0,
522 CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
523 CTL_EOL);
524 sysctl_createv(clog, 0, NULL, NULL,
525 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
526 CTLTYPE_INT, "do_loopback_cksum",
527 SYSCTL_DESCR("Perform UDP checksum on loopback"),
528 NULL, 0, &udp_do_loopback_cksum, 0,
529 CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
530 CTL_EOL);
531 sysctl_createv(clog, 0, NULL, NULL,
532 CTLFLAG_PERMANENT,
533 CTLTYPE_STRUCT, "pcblist",
534 SYSCTL_DESCR("UDP protocol control block list"),
535 sysctl_inpcblist, 0, &udbtable, 0,
536 CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
537 CTL_EOL);
538 sysctl_createv(clog, 0, NULL, NULL,
539 CTLFLAG_PERMANENT,
540 CTLTYPE_STRUCT, "stats",
541 SYSCTL_DESCR("UDPv6 statistics"),
542 sysctl_net_inet6_udp6_stats, 0, NULL, 0,
543 CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
544 CTL_EOL);
545 /* RFC6056 subtree */
546 sysctl_createv(clog, 0, NULL, &rfc6056_node,
547 CTLFLAG_PERMANENT,
548 CTLTYPE_NODE, "rfc6056",
549 SYSCTL_DESCR("RFC 6056"),
550 NULL, 0, NULL, 0,
551 CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE, CTL_EOL);
552 sysctl_createv(clog, 0, &rfc6056_node, NULL,
553 CTLFLAG_PERMANENT,
554 CTLTYPE_STRING, "available",
555 SYSCTL_DESCR("RFC 6056 available algorithms"),
556 sysctl_rfc6056_available, 0, NULL, RFC6056_MAXLEN,
557 CTL_CREATE, CTL_EOL);
558 sysctl_createv(clog, 0, &rfc6056_node, NULL,
559 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
560 CTLTYPE_STRING, "selected",
561 SYSCTL_DESCR("RFC 6056 selected algorithm"),
562 sysctl_rfc6056_selected6, 0, NULL, RFC6056_MAXLEN,
563 CTL_CREATE, CTL_EOL);
564 }
565
566 void
567 udp6_statinc(u_int stat)
568 {
569
570 KASSERT(stat < UDP6_NSTATS);
571 UDP6_STATINC(stat);
572 }
573