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