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