1 /* $NetBSD: udp6_usrreq.c,v 1.87 2009/03/18 16:00:23 cegger Exp $ */
2 /* $KAME: udp6_usrreq.c,v 1.86 2001/05/27 17:33:00 itojun Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 * Copyright (c) 1982, 1986, 1989, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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
61 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: udp6_usrreq.c,v 1.87 2009/03/18 16:00:23 cegger Exp $");
67 #include <sys/param.h>
68 #include <sys/malloc.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/errno.h>
75 #include <sys/systm.h>
77 #include <sys/syslog.h>
78 #include <sys/sysctl.h>
81 #include <net/route.h>
82 #include <net/if_types.h>
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/udp6_private.h>
98 #include <netinet6/ip6protosw.h>
99 #include <netinet/in_offload.h>
102 #if defined(NFAITH) && NFAITH > 0
103 #include <net/if_faith.h>
107 * UDP protocol implementation.
108 * Per RFC 768, August, 1980.
111 extern struct inpcbtable udbtable
;
113 percpu_t
*udp6stat_percpu
;
115 static void udp6_notify(struct in6pcb
*, int);
116 static void sysctl_net_inet6_udp6_setup(struct sysctllog
**);
122 sysctl_net_inet6_udp6_setup(NULL
);
126 * Notify a udp user of an asynchronous error;
127 * just wake up so that he can collect error status.
130 udp6_notify(struct in6pcb
*in6p
, int errno
)
132 in6p
->in6p_socket
->so_error
= errno
;
133 sorwakeup(in6p
->in6p_socket
);
134 sowwakeup(in6p
->in6p_socket
);
138 udp6_ctlinput(int cmd
, const struct sockaddr
*sa
, void *d
)
142 const struct sockaddr_in6
*sa6
= (const struct sockaddr_in6
*)sa
;
146 struct ip6ctlparam
*ip6cp
= NULL
;
147 const struct sockaddr_in6
*sa6_src
= NULL
;
148 void (*notify
)(struct in6pcb
*, int) = udp6_notify
;
149 struct udp_portonly
{
154 if (sa
->sa_family
!= AF_INET6
||
155 sa
->sa_len
!= sizeof(struct sockaddr_in6
))
158 if ((unsigned)cmd
>= PRC_NCMDS
)
160 if (PRC_IS_REDIRECT(cmd
))
161 notify
= in6_rtchange
, d
= NULL
;
162 else if (cmd
== PRC_HOSTDEAD
)
164 else if (cmd
== PRC_MSGSIZE
) {
165 /* special code is present, see below */
166 notify
= in6_rtchange
;
168 else if (inet6ctlerrmap
[cmd
] == 0)
171 /* if the parameter is from icmp6, decode it. */
173 ip6cp
= (struct ip6ctlparam
*)d
;
175 ip6
= ip6cp
->ip6c_ip6
;
176 off
= ip6cp
->ip6c_off
;
177 cmdarg
= ip6cp
->ip6c_cmdarg
;
178 sa6_src
= ip6cp
->ip6c_src
;
189 * XXX: We assume that when IPV6 is non NULL,
190 * M and OFF are valid.
193 /* check if we can safely examine src and dst ports */
194 if (m
->m_pkthdr
.len
< off
+ sizeof(*uhp
)) {
195 if (cmd
== PRC_MSGSIZE
)
196 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, 0);
200 memset(&uh
, 0, sizeof(uh
));
201 m_copydata(m
, off
, sizeof(*uhp
), (void *)&uh
);
203 if (cmd
== PRC_MSGSIZE
) {
207 * Check to see if we have a valid UDP socket
208 * corresponding to the address in the ICMPv6 message
211 if (in6_pcblookup_connect(&udbtable
, &sa6
->sin6_addr
,
212 uh
.uh_dport
, (const struct in6_addr
*)&sa6_src
->sin6_addr
,
217 * As the use of sendto(2) is fairly popular,
218 * we may want to allow non-connected pcb too.
219 * But it could be too weak against attacks...
220 * We should at least check if the local address (= s)
223 else if (in6_pcblookup_bind(&udbtable
, &sa6
->sin6_addr
,
229 * Depending on the value of "valid" and routing table
230 * size (mtudisc_{hi,lo}wat), we will:
231 * - recalculate the new MTU and create the
232 * corresponding routing entry, or
233 * - ignore the MTU change notification.
235 icmp6_mtudisc_update((struct ip6ctlparam
*)d
, valid
);
238 * regardless of if we called
239 * icmp6_mtudisc_update(), we need to call
240 * in6_pcbnotify(), to notify path MTU change
241 * to the userland (RFC3542), because some
242 * unconnected sockets may share the same
243 * destination and want to know the path MTU.
247 (void) in6_pcbnotify(&udbtable
, sa
, uh
.uh_dport
,
248 (const struct sockaddr
*)sa6_src
, uh
.uh_sport
, cmd
, cmdarg
,
251 (void) in6_pcbnotify(&udbtable
, sa
, 0,
252 (const struct sockaddr
*)sa6_src
, 0, cmd
, cmdarg
, notify
);
257 extern int udp6_sendspace
;
258 extern int udp6_recvspace
;
261 udp6_usrreq(struct socket
*so
, int req
, struct mbuf
*m
, struct mbuf
*addr6
,
262 struct mbuf
*control
, struct lwp
*l
)
264 struct in6pcb
*in6p
= sotoin6pcb(so
);
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.
278 if (req
== PRU_CONTROL
)
279 return in6_control(so
, (u_long
)m
, (void *)addr6
,
280 (struct ifnet
*)control
, l
);
282 if (req
== PRU_PURGEIF
) {
283 mutex_enter(softnet_lock
);
284 in6_pcbpurgeif0(&udbtable
, (struct ifnet
*)control
);
285 in6_purgeif((struct ifnet
*)control
);
286 in6_pcbpurgeif(&udbtable
, (struct ifnet
*)control
);
287 mutex_exit(softnet_lock
);
291 if (req
== PRU_ATTACH
)
293 else if (in6p
== NULL
) {
301 * MAPPED_ADDR implementation spec:
302 * Always attach for IPv6,
303 * and only when necessary for IPv4.
310 error
= in6_pcballoc(so
, &udbtable
);
314 error
= soreserve(so
, udp6_sendspace
, udp6_recvspace
);
317 in6p
= sotoin6pcb(so
);
318 in6p
->in6p_cksum
= -1; /* just to be sure */
327 error
= in6_pcbbind(in6p
, addr6
, l
);
332 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
)) {
337 error
= in6_pcbconnect(in6p
, addr6
, l
);
344 if (IN6_IS_ADDR_UNSPECIFIED(&in6p
->in6p_faddr
)) {
349 in6_pcbdisconnect(in6p
);
350 memset((void *)&in6p
->in6p_laddr
, 0, sizeof(in6p
->in6p_laddr
));
352 so
->so_state
&= ~SS_ISCONNECTED
; /* XXX */
353 in6_pcbstate(in6p
, IN6P_BOUND
); /* XXX */
362 error
= udp6_output(in6p
, m
, addr6
, control
, l
);
367 soisdisconnected(so
);
372 in6_setsockaddr(in6p
, addr6
);
376 in6_setpeeraddr(in6p
, addr6
);
381 * stat: don't bother with a blocksize
398 return EOPNOTSUPP
; /* do not free mbuf's */
401 panic("udp6_usrreq");
413 sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS
)
416 return (NETSTAT_SYSCTL(udp6stat_percpu
, UDP6_NSTATS
));
420 sysctl_net_inet6_udp6_setup(struct sysctllog
**clog
)
422 sysctl_createv(clog
, 0, NULL
, NULL
,
424 CTLTYPE_NODE
, "net", NULL
,
427 sysctl_createv(clog
, 0, NULL
, NULL
,
429 CTLTYPE_NODE
, "inet6", NULL
,
431 CTL_NET
, PF_INET6
, CTL_EOL
);
432 sysctl_createv(clog
, 0, NULL
, NULL
,
434 CTLTYPE_NODE
, "udp6",
435 SYSCTL_DESCR("UDPv6 related settings"),
437 CTL_NET
, PF_INET6
, IPPROTO_UDP
, CTL_EOL
);
439 sysctl_createv(clog
, 0, NULL
, NULL
,
440 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
441 CTLTYPE_INT
, "sendspace",
442 SYSCTL_DESCR("Default UDP send buffer size"),
443 NULL
, 0, &udp6_sendspace
, 0,
444 CTL_NET
, PF_INET6
, IPPROTO_UDP
, UDP6CTL_SENDSPACE
,
446 sysctl_createv(clog
, 0, NULL
, NULL
,
447 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
448 CTLTYPE_INT
, "recvspace",
449 SYSCTL_DESCR("Default UDP receive buffer size"),
450 NULL
, 0, &udp6_recvspace
, 0,
451 CTL_NET
, PF_INET6
, IPPROTO_UDP
, UDP6CTL_RECVSPACE
,
453 sysctl_createv(clog
, 0, NULL
, NULL
,
454 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
455 CTLTYPE_INT
, "do_loopback_cksum",
456 SYSCTL_DESCR("Perform UDP checksum on loopback"),
457 NULL
, 0, &udp_do_loopback_cksum
, 0,
458 CTL_NET
, PF_INET6
, IPPROTO_UDP
, UDP6CTL_LOOPBACKCKSUM
,
460 sysctl_createv(clog
, 0, NULL
, NULL
,
462 CTLTYPE_STRUCT
, "pcblist",
463 SYSCTL_DESCR("UDP protocol control block list"),
464 sysctl_inpcblist
, 0, &udbtable
, 0,
465 CTL_NET
, PF_INET6
, IPPROTO_UDP
, CTL_CREATE
,
467 sysctl_createv(clog
, 0, NULL
, NULL
,
469 CTLTYPE_STRUCT
, "stats",
470 SYSCTL_DESCR("UDPv6 statistics"),
471 sysctl_net_inet6_udp6_stats
, 0, NULL
, 0,
472 CTL_NET
, PF_INET6
, IPPROTO_UDP
, UDP6CTL_STATS
,
477 udp6_statinc(u_int stat
)
480 KASSERT(stat
< UDP6_NSTATS
);