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