xref: /netbsd/sys/netinet6/udp6_usrreq.c (revision c4a72b64)
1 /*	$NetBSD: udp6_usrreq.c,v 1.53 2002/09/11 02:46:47 itojun 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.53 2002/09/11 02:46:47 itojun 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, p)
271 	struct socket *so;
272 	int req;
273 	struct mbuf *m, *addr6, *control;
274 	struct proc *p;
275 {
276 	struct	in6pcb *in6p = sotoin6pcb(so);
277 	int	error = 0;
278 	int	s;
279 
280 	/*
281 	 * MAPPED_ADDR implementation info:
282 	 *  Mapped addr support for PRU_CONTROL is not necessary.
283 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
284 	 *  and they don't associate any addr to their socket.  Then
285 	 *  socket family is only hint about the PRU_CONTROL'ed address
286 	 *  family, especially when getting addrs from kernel.
287 	 *  So AF_INET socket need to be used to control AF_INET addrs,
288 	 *  and AF_INET6 socket for AF_INET6 addrs.
289 	 */
290 	if (req == PRU_CONTROL)
291 		return (in6_control(so, (u_long)m, (caddr_t)addr6,
292 				   (struct ifnet *)control, p));
293 
294 	if (req == PRU_PURGEIF) {
295 		in6_pcbpurgeif0(&udb6, (struct ifnet *)control);
296 		in6_purgeif((struct ifnet *)control);
297 		in6_pcbpurgeif(&udb6, (struct ifnet *)control);
298 		return (0);
299 	}
300 
301 	if (in6p == NULL && req != PRU_ATTACH) {
302 		error = EINVAL;
303 		goto release;
304 	}
305 
306 	switch (req) {
307 	case PRU_ATTACH:
308 		/*
309 		 * MAPPED_ADDR implementation spec:
310 		 *  Always attach for IPv6,
311 		 *  and only when necessary for IPv4.
312 		 */
313 		if (in6p != NULL) {
314 			error = EINVAL;
315 			break;
316 		}
317 		s = splsoftnet();
318 		error = in6_pcballoc(so, &udb6);
319 		splx(s);
320 		if (error)
321 			break;
322 		error = soreserve(so, udp6_sendspace, udp6_recvspace);
323 		if (error)
324 			break;
325 		in6p = sotoin6pcb(so);
326 		in6p->in6p_cksum = -1;	/* just to be sure */
327 		break;
328 
329 	case PRU_DETACH:
330 		udp6_detach(in6p);
331 		break;
332 
333 	case PRU_BIND:
334 		s = splsoftnet();
335 		error = in6_pcbbind(in6p, addr6, p);
336 		splx(s);
337 		break;
338 
339 	case PRU_LISTEN:
340 		error = EOPNOTSUPP;
341 		break;
342 
343 	case PRU_CONNECT:
344 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
345 			error = EISCONN;
346 			break;
347 		}
348 		s = splsoftnet();
349 		error = in6_pcbconnect(in6p, addr6);
350 		if (ip6_auto_flowlabel) {
351 			in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
352 			in6p->in6p_flowinfo |=
353 				(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
354 		}
355 		splx(s);
356 		if (error == 0)
357 			soisconnected(so);
358 		break;
359 
360 	case PRU_CONNECT2:
361 		error = EOPNOTSUPP;
362 		break;
363 
364 	case PRU_ACCEPT:
365 		error = EOPNOTSUPP;
366 		break;
367 
368 	case PRU_DISCONNECT:
369 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
370 			error = ENOTCONN;
371 			break;
372 		}
373 		s = splsoftnet();
374 		in6_pcbdisconnect(in6p);
375 		bzero((caddr_t)&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
376 		splx(s);
377 		so->so_state &= ~SS_ISCONNECTED;		/* XXX */
378 		break;
379 
380 	case PRU_SHUTDOWN:
381 		socantsendmore(so);
382 		break;
383 
384 	case PRU_SEND:
385 		return (udp6_output(in6p, m, addr6, control, p));
386 
387 	case PRU_ABORT:
388 		soisdisconnected(so);
389 		udp6_detach(in6p);
390 		break;
391 
392 	case PRU_SOCKADDR:
393 		in6_setsockaddr(in6p, addr6);
394 		break;
395 
396 	case PRU_PEERADDR:
397 		in6_setpeeraddr(in6p, addr6);
398 		break;
399 
400 	case PRU_SENSE:
401 		/*
402 		 * stat: don't bother with a blocksize
403 		 */
404 		return (0);
405 
406 	case PRU_SENDOOB:
407 	case PRU_FASTTIMO:
408 	case PRU_SLOWTIMO:
409 	case PRU_PROTORCV:
410 	case PRU_PROTOSEND:
411 		error = EOPNOTSUPP;
412 		break;
413 
414 	case PRU_RCVD:
415 	case PRU_RCVOOB:
416 		return (EOPNOTSUPP);	/* do not free mbuf's */
417 
418 	default:
419 		panic("udp6_usrreq");
420 	}
421 
422 release:
423 	if (control)
424 		m_freem(control);
425 	if (m)
426 		m_freem(m);
427 	return (error);
428 }
429 
430 static void
431 udp6_detach(in6p)
432 	struct in6pcb *in6p;
433 {
434 	int s = splsoftnet();
435 
436 	if (in6p == udp6_last_in6pcb)
437 		udp6_last_in6pcb = &udb6;
438 	in6_pcbdetach(in6p);
439 	splx(s);
440 }
441 
442 int
443 udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
444 	int *name;
445 	u_int namelen;
446 	void *oldp;
447 	size_t *oldlenp;
448 	void *newp;
449 	size_t newlen;
450 {
451 	/* All sysctl names at this level are terminal. */
452 	if (namelen != 1)
453 		return ENOTDIR;
454 
455 	switch (name[0]) {
456 
457 	case UDP6CTL_SENDSPACE:
458 		return sysctl_int(oldp, oldlenp, newp, newlen,
459 		    &udp6_sendspace);
460 	case UDP6CTL_RECVSPACE:
461 		return sysctl_int(oldp, oldlenp, newp, newlen,
462 		    &udp6_recvspace);
463 	default:
464 		return ENOPROTOOPT;
465 	}
466 	/* NOTREACHED */
467 }
468