udp6_usrreq.c revision 1.91.2.2 1 1.91.2.1 tls /* $NetBSD: udp6_usrreq.c,v 1.91.2.2 2017/12/03 11:39:05 jdolecek Exp $ */
2 1.43 itojun /* $KAME: udp6_usrreq.c,v 1.86 2001/05/27 17:33:00 itojun Exp $ */
3 1.3 thorpej
4 1.2 itojun /*
5 1.2 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.2 itojun * All rights reserved.
7 1.27 itojun *
8 1.2 itojun * Redistribution and use in source and binary forms, with or without
9 1.2 itojun * modification, are permitted provided that the following conditions
10 1.2 itojun * are met:
11 1.2 itojun * 1. Redistributions of source code must retain the above copyright
12 1.2 itojun * notice, this list of conditions and the following disclaimer.
13 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.2 itojun * notice, this list of conditions and the following disclaimer in the
15 1.2 itojun * documentation and/or other materials provided with the distribution.
16 1.2 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.2 itojun * may be used to endorse or promote products derived from this software
18 1.2 itojun * without specific prior written permission.
19 1.27 itojun *
20 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.2 itojun * SUCH DAMAGE.
31 1.2 itojun */
32 1.2 itojun
33 1.2 itojun /*
34 1.2 itojun * Copyright (c) 1982, 1986, 1989, 1993
35 1.2 itojun * The Regents of the University of California. All rights reserved.
36 1.2 itojun *
37 1.2 itojun * Redistribution and use in source and binary forms, with or without
38 1.2 itojun * modification, are permitted provided that the following conditions
39 1.2 itojun * are met:
40 1.2 itojun * 1. Redistributions of source code must retain the above copyright
41 1.2 itojun * notice, this list of conditions and the following disclaimer.
42 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
43 1.2 itojun * notice, this list of conditions and the following disclaimer in the
44 1.2 itojun * documentation and/or other materials provided with the distribution.
45 1.56 agc * 3. Neither the name of the University nor the names of its contributors
46 1.2 itojun * may be used to endorse or promote products derived from this software
47 1.2 itojun * without specific prior written permission.
48 1.2 itojun *
49 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 1.2 itojun * SUCH DAMAGE.
60 1.2 itojun *
61 1.2 itojun * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
62 1.2 itojun */
63 1.51 lukem
64 1.51 lukem #include <sys/cdefs.h>
65 1.91.2.1 tls __KERNEL_RCSID(0, "$NetBSD: udp6_usrreq.c,v 1.91.2.2 2017/12/03 11:39:05 jdolecek Exp $");
66 1.90 christos
67 1.91.2.2 jdolecek #ifdef _KERNEL_OPT
68 1.90 christos #include "opt_inet.h"
69 1.91.2.1 tls #include "opt_inet_csum.h"
70 1.91.2.2 jdolecek #include "opt_ipsec.h"
71 1.91.2.2 jdolecek #include "opt_net_mpsafe.h"
72 1.91.2.2 jdolecek #endif
73 1.2 itojun
74 1.2 itojun #include <sys/param.h>
75 1.2 itojun #include <sys/mbuf.h>
76 1.2 itojun #include <sys/protosw.h>
77 1.2 itojun #include <sys/socket.h>
78 1.2 itojun #include <sys/socketvar.h>
79 1.2 itojun #include <sys/systm.h>
80 1.2 itojun #include <sys/proc.h>
81 1.8 itojun #include <sys/syslog.h>
82 1.90 christos #include <sys/domain.h>
83 1.50 simonb #include <sys/sysctl.h>
84 1.2 itojun
85 1.2 itojun #include <net/if.h>
86 1.2 itojun #include <net/if_types.h>
87 1.2 itojun
88 1.2 itojun #include <netinet/in.h>
89 1.2 itojun #include <netinet/in_var.h>
90 1.14 itojun #include <netinet/in_systm.h>
91 1.91.2.1 tls #include <netinet/in_offload.h>
92 1.14 itojun #include <netinet/ip.h>
93 1.14 itojun #include <netinet/ip_var.h>
94 1.14 itojun #include <netinet/in_pcb.h>
95 1.14 itojun #include <netinet/udp.h>
96 1.14 itojun #include <netinet/udp_var.h>
97 1.91.2.1 tls #include <netinet/udp_private.h>
98 1.91.2.1 tls
99 1.23 itojun #include <netinet/ip6.h>
100 1.91.2.1 tls #include <netinet/icmp6.h>
101 1.27 itojun #include <netinet6/ip6_var.h>
102 1.91.2.1 tls #include <netinet6/ip6_private.h>
103 1.2 itojun #include <netinet6/in6_pcb.h>
104 1.2 itojun #include <netinet6/udp6_var.h>
105 1.82 thorpej #include <netinet6/udp6_private.h>
106 1.14 itojun #include <netinet6/ip6protosw.h>
107 1.91.2.1 tls #include <netinet6/scope6_var.h>
108 1.2 itojun
109 1.91.2.2 jdolecek #ifdef IPSEC
110 1.91.2.2 jdolecek #include <netipsec/ipsec.h>
111 1.91.2.2 jdolecek #include <netipsec/ipsec_var.h>
112 1.91.2.2 jdolecek #include <netipsec/ipsec_private.h>
113 1.91.2.2 jdolecek #ifdef INET6
114 1.91.2.2 jdolecek #include <netipsec/ipsec6.h>
115 1.91.2.2 jdolecek #endif
116 1.91.2.2 jdolecek #endif /* IPSEC */
117 1.91.2.2 jdolecek
118 1.2 itojun #include "faith.h"
119 1.41 itojun #if defined(NFAITH) && NFAITH > 0
120 1.41 itojun #include <net/if_faith.h>
121 1.41 itojun #endif
122 1.2 itojun
123 1.2 itojun /*
124 1.73 rpaulo * UDP protocol implementation.
125 1.2 itojun * Per RFC 768, August, 1980.
126 1.2 itojun */
127 1.2 itojun
128 1.58 itojun extern struct inpcbtable udbtable;
129 1.82 thorpej
130 1.82 thorpej percpu_t *udp6stat_percpu;
131 1.2 itojun
132 1.91.2.1 tls /* UDP on IP6 parameters */
133 1.91.2.1 tls static int udp6_sendspace = 9216; /* really max datagram size */
134 1.91.2.1 tls static int udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6));
135 1.91.2.1 tls /* 40 1K datagrams */
136 1.91.2.1 tls
137 1.78 dyoung static void udp6_notify(struct in6pcb *, int);
138 1.88 pooka static void sysctl_net_inet6_udp6_setup(struct sysctllog **);
139 1.2 itojun
140 1.91.2.1 tls #ifdef UDP_CSUM_COUNTERS
141 1.91.2.1 tls #include <sys/device.h>
142 1.91.2.1 tls struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
143 1.91.2.1 tls NULL, "udp6", "hwcsum bad");
144 1.91.2.1 tls struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
145 1.91.2.1 tls NULL, "udp6", "hwcsum ok");
146 1.91.2.1 tls struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
147 1.91.2.1 tls NULL, "udp6", "hwcsum data");
148 1.91.2.1 tls struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
149 1.91.2.1 tls NULL, "udp6", "swcsum");
150 1.91.2.1 tls
151 1.91.2.1 tls EVCNT_ATTACH_STATIC(udp6_hwcsum_bad);
152 1.91.2.1 tls EVCNT_ATTACH_STATIC(udp6_hwcsum_ok);
153 1.91.2.1 tls EVCNT_ATTACH_STATIC(udp6_hwcsum_data);
154 1.91.2.1 tls EVCNT_ATTACH_STATIC(udp6_swcsum);
155 1.91.2.1 tls
156 1.91.2.1 tls #define UDP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
157 1.91.2.1 tls #else
158 1.91.2.1 tls #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */
159 1.91.2.1 tls #endif
160 1.91.2.1 tls
161 1.2 itojun void
162 1.81 matt udp6_init(void)
163 1.2 itojun {
164 1.88 pooka sysctl_net_inet6_udp6_setup(NULL);
165 1.91.2.1 tls udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
166 1.91.2.1 tls
167 1.91.2.1 tls udp_init_common();
168 1.2 itojun }
169 1.2 itojun
170 1.2 itojun /*
171 1.2 itojun * Notify a udp user of an asynchronous error;
172 1.27 itojun * just wake up so that he can collect error status.
173 1.2 itojun */
174 1.2 itojun static void
175 1.76 dyoung udp6_notify(struct in6pcb *in6p, int errno)
176 1.2 itojun {
177 1.2 itojun in6p->in6p_socket->so_error = errno;
178 1.2 itojun sorwakeup(in6p->in6p_socket);
179 1.2 itojun sowwakeup(in6p->in6p_socket);
180 1.2 itojun }
181 1.2 itojun
182 1.84 ad void *
183 1.76 dyoung udp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
184 1.2 itojun {
185 1.2 itojun struct udphdr uh;
186 1.39 itojun struct ip6_hdr *ip6;
187 1.76 dyoung const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
188 1.14 itojun struct mbuf *m;
189 1.14 itojun int off;
190 1.40 itojun void *cmdarg;
191 1.40 itojun struct ip6ctlparam *ip6cp = NULL;
192 1.40 itojun const struct sockaddr_in6 *sa6_src = NULL;
193 1.76 dyoung void (*notify)(struct in6pcb *, int) = udp6_notify;
194 1.40 itojun struct udp_portonly {
195 1.40 itojun u_int16_t uh_sport;
196 1.40 itojun u_int16_t uh_dport;
197 1.40 itojun } *uhp;
198 1.2 itojun
199 1.10 itojun if (sa->sa_family != AF_INET6 ||
200 1.10 itojun sa->sa_len != sizeof(struct sockaddr_in6))
201 1.84 ad return NULL;
202 1.14 itojun
203 1.25 itojun if ((unsigned)cmd >= PRC_NCMDS)
204 1.84 ad return NULL;
205 1.25 itojun if (PRC_IS_REDIRECT(cmd))
206 1.25 itojun notify = in6_rtchange, d = NULL;
207 1.25 itojun else if (cmd == PRC_HOSTDEAD)
208 1.25 itojun d = NULL;
209 1.47 itojun else if (cmd == PRC_MSGSIZE) {
210 1.47 itojun /* special code is present, see below */
211 1.47 itojun notify = in6_rtchange;
212 1.47 itojun }
213 1.25 itojun else if (inet6ctlerrmap[cmd] == 0)
214 1.84 ad return NULL;
215 1.7 itojun
216 1.14 itojun /* if the parameter is from icmp6, decode it. */
217 1.14 itojun if (d != NULL) {
218 1.40 itojun ip6cp = (struct ip6ctlparam *)d;
219 1.14 itojun m = ip6cp->ip6c_m;
220 1.14 itojun ip6 = ip6cp->ip6c_ip6;
221 1.14 itojun off = ip6cp->ip6c_off;
222 1.40 itojun cmdarg = ip6cp->ip6c_cmdarg;
223 1.40 itojun sa6_src = ip6cp->ip6c_src;
224 1.14 itojun } else {
225 1.14 itojun m = NULL;
226 1.14 itojun ip6 = NULL;
227 1.40 itojun cmdarg = NULL;
228 1.40 itojun sa6_src = &sa6_any;
229 1.60 christos off = 0;
230 1.14 itojun }
231 1.14 itojun
232 1.2 itojun if (ip6) {
233 1.2 itojun /*
234 1.2 itojun * XXX: We assume that when IPV6 is non NULL,
235 1.2 itojun * M and OFF are valid.
236 1.2 itojun */
237 1.33 itojun
238 1.33 itojun /* check if we can safely examine src and dst ports */
239 1.42 itojun if (m->m_pkthdr.len < off + sizeof(*uhp)) {
240 1.42 itojun if (cmd == PRC_MSGSIZE)
241 1.42 itojun icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
242 1.84 ad return NULL;
243 1.42 itojun }
244 1.7 itojun
245 1.87 cegger memset(&uh, 0, sizeof(uh));
246 1.77 christos m_copydata(m, off, sizeof(*uhp), (void *)&uh);
247 1.34 itojun
248 1.34 itojun if (cmd == PRC_MSGSIZE) {
249 1.36 itojun int valid = 0;
250 1.40 itojun
251 1.34 itojun /*
252 1.34 itojun * Check to see if we have a valid UDP socket
253 1.34 itojun * corresponding to the address in the ICMPv6 message
254 1.34 itojun * payload.
255 1.34 itojun */
256 1.58 itojun if (in6_pcblookup_connect(&udbtable, &sa6->sin6_addr,
257 1.68 christos uh.uh_dport, (const struct in6_addr *)&sa6_src->sin6_addr,
258 1.89 dyoung uh.uh_sport, 0, 0))
259 1.36 itojun valid++;
260 1.34 itojun #if 0
261 1.34 itojun /*
262 1.34 itojun * As the use of sendto(2) is fairly popular,
263 1.34 itojun * we may want to allow non-connected pcb too.
264 1.34 itojun * But it could be too weak against attacks...
265 1.34 itojun * We should at least check if the local address (= s)
266 1.34 itojun * is really ours.
267 1.34 itojun */
268 1.58 itojun else if (in6_pcblookup_bind(&udbtable, &sa6->sin6_addr,
269 1.58 itojun uh.uh_dport, 0))
270 1.36 itojun valid++;
271 1.34 itojun #endif
272 1.34 itojun
273 1.34 itojun /*
274 1.40 itojun * Depending on the value of "valid" and routing table
275 1.40 itojun * size (mtudisc_{hi,lo}wat), we will:
276 1.46 itojun * - recalculate the new MTU and create the
277 1.40 itojun * corresponding routing entry, or
278 1.40 itojun * - ignore the MTU change notification.
279 1.34 itojun */
280 1.36 itojun icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
281 1.34 itojun
282 1.40 itojun /*
283 1.74 rpaulo * regardless of if we called
284 1.74 rpaulo * icmp6_mtudisc_update(), we need to call
285 1.74 rpaulo * in6_pcbnotify(), to notify path MTU change
286 1.74 rpaulo * to the userland (RFC3542), because some
287 1.74 rpaulo * unconnected sockets may share the same
288 1.40 itojun * destination and want to know the path MTU.
289 1.40 itojun */
290 1.34 itojun }
291 1.34 itojun
292 1.58 itojun (void) in6_pcbnotify(&udbtable, sa, uh.uh_dport,
293 1.91.2.2 jdolecek sin6tocsa(sa6_src), uh.uh_sport, cmd, cmdarg,
294 1.40 itojun notify);
295 1.2 itojun } else {
296 1.58 itojun (void) in6_pcbnotify(&udbtable, sa, 0,
297 1.91.2.2 jdolecek sin6tocsa(sa6_src), 0, cmd, cmdarg, notify);
298 1.2 itojun }
299 1.84 ad return NULL;
300 1.2 itojun }
301 1.2 itojun
302 1.90 christos int
303 1.90 christos udp6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
304 1.90 christos {
305 1.90 christos int s;
306 1.90 christos int error = 0;
307 1.90 christos int family;
308 1.90 christos
309 1.90 christos family = so->so_proto->pr_domain->dom_family;
310 1.90 christos
311 1.90 christos s = splsoftnet();
312 1.90 christos switch (family) {
313 1.90 christos #ifdef INET
314 1.90 christos case PF_INET:
315 1.90 christos if (sopt->sopt_level != IPPROTO_UDP) {
316 1.90 christos error = ip_ctloutput(op, so, sopt);
317 1.90 christos goto end;
318 1.90 christos }
319 1.90 christos break;
320 1.90 christos #endif
321 1.90 christos #ifdef INET6
322 1.90 christos case PF_INET6:
323 1.90 christos if (sopt->sopt_level != IPPROTO_UDP) {
324 1.90 christos error = ip6_ctloutput(op, so, sopt);
325 1.90 christos goto end;
326 1.90 christos }
327 1.90 christos break;
328 1.90 christos #endif
329 1.90 christos default:
330 1.90 christos error = EAFNOSUPPORT;
331 1.90 christos goto end;
332 1.90 christos }
333 1.91 christos error = EINVAL;
334 1.90 christos
335 1.90 christos end:
336 1.90 christos splx(s);
337 1.90 christos return error;
338 1.90 christos }
339 1.90 christos
340 1.91.2.1 tls static void
341 1.91.2.1 tls udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
342 1.91.2.1 tls struct sockaddr *src, struct socket *so)
343 1.91.2.1 tls {
344 1.91.2.1 tls struct mbuf *opts = NULL;
345 1.91.2.1 tls struct mbuf *n;
346 1.91.2.2 jdolecek struct in6pcb *in6p;
347 1.91.2.1 tls
348 1.91.2.2 jdolecek KASSERT(so != NULL);
349 1.91.2.2 jdolecek KASSERT(so->so_proto->pr_domain->dom_family == AF_INET6);
350 1.91.2.1 tls in6p = sotoin6pcb(so);
351 1.91.2.2 jdolecek KASSERT(in6p != NULL);
352 1.91.2.1 tls
353 1.91.2.1 tls #if defined(IPSEC)
354 1.91.2.1 tls /* check AH/ESP integrity. */
355 1.91.2.2 jdolecek if (ipsec_used && ipsec6_in_reject(m, in6p)) {
356 1.91.2.1 tls IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
357 1.91.2.1 tls if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
358 1.91.2.1 tls icmp6_error(n, ICMP6_DST_UNREACH,
359 1.91.2.1 tls ICMP6_DST_UNREACH_ADMIN, 0);
360 1.91.2.1 tls return;
361 1.91.2.1 tls }
362 1.91.2.1 tls #endif /*IPSEC*/
363 1.91.2.1 tls
364 1.91.2.1 tls if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
365 1.91.2.2 jdolecek if (in6p->in6p_flags & IN6P_CONTROLOPTS
366 1.91.2.2 jdolecek || SOOPT_TIMESTAMP(in6p->in6p_socket->so_options)) {
367 1.91.2.1 tls struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
368 1.91.2.1 tls ip6_savecontrol(in6p, &opts, ip6, n);
369 1.91.2.1 tls }
370 1.90 christos
371 1.91.2.1 tls m_adj(n, off);
372 1.91.2.1 tls if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
373 1.91.2.1 tls m_freem(n);
374 1.91.2.1 tls if (opts)
375 1.91.2.1 tls m_freem(opts);
376 1.91.2.1 tls so->so_rcv.sb_overflowed++;
377 1.91.2.1 tls UDP6_STATINC(UDP6_STAT_FULLSOCK);
378 1.91.2.1 tls } else
379 1.91.2.1 tls sorwakeup(so);
380 1.91.2.1 tls }
381 1.91.2.1 tls }
382 1.2 itojun
383 1.2 itojun int
384 1.91.2.1 tls udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
385 1.91.2.1 tls struct mbuf *m, int off)
386 1.91.2.1 tls {
387 1.91.2.1 tls u_int16_t sport, dport;
388 1.91.2.1 tls int rcvcnt;
389 1.91.2.1 tls struct in6_addr src6, *dst6;
390 1.91.2.1 tls const struct in_addr *dst4;
391 1.91.2.1 tls struct inpcb_hdr *inph;
392 1.91.2.1 tls struct in6pcb *in6p;
393 1.91.2.1 tls
394 1.91.2.1 tls rcvcnt = 0;
395 1.91.2.1 tls off += sizeof(struct udphdr); /* now, offset of payload */
396 1.91.2.1 tls
397 1.91.2.1 tls if (af != AF_INET && af != AF_INET6)
398 1.91.2.1 tls goto bad;
399 1.91.2.1 tls if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
400 1.91.2.1 tls goto bad;
401 1.91.2.1 tls
402 1.91.2.1 tls src6 = src->sin6_addr;
403 1.91.2.1 tls if (sa6_recoverscope(src) != 0) {
404 1.91.2.1 tls /* XXX: should be impossible. */
405 1.91.2.1 tls goto bad;
406 1.91.2.1 tls }
407 1.91.2.1 tls sport = src->sin6_port;
408 1.91.2.1 tls
409 1.91.2.1 tls dport = dst->sin6_port;
410 1.91.2.1 tls dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
411 1.91.2.1 tls dst6 = &dst->sin6_addr;
412 1.91.2.1 tls
413 1.91.2.1 tls if (IN6_IS_ADDR_MULTICAST(dst6) ||
414 1.91.2.1 tls (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
415 1.91.2.1 tls /*
416 1.91.2.1 tls * Deliver a multicast or broadcast datagram to *all* sockets
417 1.91.2.1 tls * for which the local and remote addresses and ports match
418 1.91.2.1 tls * those of the incoming datagram. This allows more than
419 1.91.2.1 tls * one process to receive multi/broadcasts on the same port.
420 1.91.2.1 tls * (This really ought to be done for unicast datagrams as
421 1.91.2.1 tls * well, but that would cause problems with existing
422 1.91.2.1 tls * applications that open both address-specific sockets and
423 1.91.2.1 tls * a wildcard socket listening to the same port -- they would
424 1.91.2.1 tls * end up receiving duplicates of every unicast datagram.
425 1.91.2.1 tls * Those applications open the multiple sockets to overcome an
426 1.91.2.1 tls * inadequacy of the UDP socket interface, but for backwards
427 1.91.2.1 tls * compatibility we avoid the problem here rather than
428 1.91.2.1 tls * fixing the interface. Maybe 4.5BSD will remedy this?)
429 1.91.2.1 tls */
430 1.91.2.1 tls
431 1.91.2.1 tls /*
432 1.91.2.1 tls * KAME note: traditionally we dropped udpiphdr from mbuf here.
433 1.91.2.1 tls * we need udpiphdr for IPsec processing so we do that later.
434 1.91.2.1 tls */
435 1.91.2.1 tls /*
436 1.91.2.1 tls * Locate pcb(s) for datagram.
437 1.91.2.1 tls */
438 1.91.2.1 tls TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
439 1.91.2.1 tls in6p = (struct in6pcb *)inph;
440 1.91.2.1 tls if (in6p->in6p_af != AF_INET6)
441 1.91.2.1 tls continue;
442 1.91.2.1 tls
443 1.91.2.1 tls if (in6p->in6p_lport != dport)
444 1.91.2.1 tls continue;
445 1.91.2.1 tls if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
446 1.91.2.1 tls if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
447 1.91.2.1 tls dst6))
448 1.91.2.1 tls continue;
449 1.91.2.1 tls } else {
450 1.91.2.1 tls if (IN6_IS_ADDR_V4MAPPED(dst6) &&
451 1.91.2.1 tls (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
452 1.91.2.1 tls continue;
453 1.91.2.1 tls }
454 1.91.2.1 tls if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
455 1.91.2.1 tls if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
456 1.91.2.1 tls &src6) || in6p->in6p_fport != sport)
457 1.91.2.1 tls continue;
458 1.91.2.1 tls } else {
459 1.91.2.1 tls if (IN6_IS_ADDR_V4MAPPED(&src6) &&
460 1.91.2.1 tls (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
461 1.91.2.1 tls continue;
462 1.91.2.1 tls }
463 1.91.2.1 tls
464 1.91.2.2 jdolecek udp6_sendup(m, off, sin6tosa(src), in6p->in6p_socket);
465 1.91.2.1 tls rcvcnt++;
466 1.91.2.1 tls
467 1.91.2.1 tls /*
468 1.91.2.1 tls * Don't look for additional matches if this one does
469 1.91.2.1 tls * not have either the SO_REUSEPORT or SO_REUSEADDR
470 1.91.2.1 tls * socket options set. This heuristic avoids searching
471 1.91.2.1 tls * through all pcbs in the common case of a non-shared
472 1.91.2.1 tls * port. It assumes that an application will never
473 1.91.2.1 tls * clear these options after setting them.
474 1.91.2.1 tls */
475 1.91.2.1 tls if ((in6p->in6p_socket->so_options &
476 1.91.2.1 tls (SO_REUSEPORT|SO_REUSEADDR)) == 0)
477 1.91.2.1 tls break;
478 1.91.2.1 tls }
479 1.91.2.1 tls } else {
480 1.91.2.1 tls /*
481 1.91.2.1 tls * Locate pcb for datagram.
482 1.91.2.1 tls */
483 1.91.2.1 tls in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
484 1.91.2.1 tls dport, 0, 0);
485 1.91.2.1 tls if (in6p == 0) {
486 1.91.2.1 tls UDP_STATINC(UDP_STAT_PCBHASHMISS);
487 1.91.2.1 tls in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
488 1.91.2.1 tls if (in6p == 0)
489 1.91.2.1 tls return rcvcnt;
490 1.91.2.1 tls }
491 1.91.2.1 tls
492 1.91.2.2 jdolecek udp6_sendup(m, off, sin6tosa(src), in6p->in6p_socket);
493 1.91.2.1 tls rcvcnt++;
494 1.91.2.1 tls }
495 1.91.2.1 tls
496 1.91.2.1 tls bad:
497 1.91.2.1 tls return rcvcnt;
498 1.91.2.1 tls }
499 1.91.2.1 tls
500 1.91.2.1 tls int
501 1.91.2.1 tls udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
502 1.2 itojun {
503 1.2 itojun
504 1.27 itojun /*
505 1.91.2.1 tls * XXX it's better to record and check if this mbuf is
506 1.91.2.1 tls * already checked.
507 1.91.2.1 tls */
508 1.91.2.1 tls
509 1.91.2.1 tls if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
510 1.91.2.1 tls goto good;
511 1.91.2.1 tls }
512 1.91.2.1 tls if (uh->uh_sum == 0) {
513 1.91.2.1 tls UDP6_STATINC(UDP6_STAT_NOSUM);
514 1.91.2.1 tls goto bad;
515 1.91.2.1 tls }
516 1.91.2.1 tls
517 1.91.2.1 tls switch (m->m_pkthdr.csum_flags &
518 1.91.2.2 jdolecek ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_UDPv6) |
519 1.91.2.1 tls M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
520 1.91.2.1 tls case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD:
521 1.91.2.1 tls UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad);
522 1.91.2.1 tls UDP6_STATINC(UDP6_STAT_BADSUM);
523 1.91.2.1 tls goto bad;
524 1.91.2.1 tls
525 1.91.2.1 tls #if 0 /* notyet */
526 1.91.2.1 tls case M_CSUM_UDPv6|M_CSUM_DATA:
527 1.91.2.1 tls #endif
528 1.91.2.1 tls
529 1.91.2.1 tls case M_CSUM_UDPv6:
530 1.91.2.1 tls /* Checksum was okay. */
531 1.91.2.1 tls UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok);
532 1.91.2.1 tls break;
533 1.91.2.1 tls
534 1.91.2.1 tls default:
535 1.91.2.1 tls /*
536 1.91.2.1 tls * Need to compute it ourselves. Maybe skip checksum
537 1.91.2.1 tls * on loopback interfaces.
538 1.91.2.1 tls */
539 1.91.2.1 tls UDP_CSUM_COUNTER_INCR(&udp6_swcsum);
540 1.91.2.1 tls if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
541 1.91.2.1 tls UDP6_STATINC(UDP6_STAT_BADSUM);
542 1.91.2.1 tls goto bad;
543 1.91.2.1 tls }
544 1.91.2.1 tls }
545 1.91.2.1 tls
546 1.91.2.1 tls good:
547 1.91.2.1 tls return 0;
548 1.91.2.1 tls bad:
549 1.91.2.1 tls return -1;
550 1.91.2.1 tls }
551 1.91.2.1 tls
552 1.91.2.1 tls int
553 1.91.2.1 tls udp6_input(struct mbuf **mp, int *offp, int proto)
554 1.91.2.1 tls {
555 1.91.2.1 tls struct mbuf *m = *mp;
556 1.91.2.1 tls int off = *offp;
557 1.91.2.1 tls struct sockaddr_in6 src, dst;
558 1.91.2.1 tls struct ip6_hdr *ip6;
559 1.91.2.1 tls struct udphdr *uh;
560 1.91.2.1 tls u_int32_t plen, ulen;
561 1.91.2.1 tls
562 1.91.2.1 tls ip6 = mtod(m, struct ip6_hdr *);
563 1.91.2.1 tls
564 1.91.2.1 tls #if defined(NFAITH) && 0 < NFAITH
565 1.91.2.1 tls if (faithprefix(&ip6->ip6_dst)) {
566 1.91.2.1 tls /* send icmp6 host unreach? */
567 1.91.2.1 tls m_freem(m);
568 1.91.2.1 tls return IPPROTO_DONE;
569 1.91.2.1 tls }
570 1.91.2.1 tls #endif
571 1.91.2.1 tls
572 1.91.2.1 tls UDP6_STATINC(UDP6_STAT_IPACKETS);
573 1.91.2.1 tls
574 1.91.2.1 tls /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */
575 1.91.2.1 tls plen = m->m_pkthdr.len - off;
576 1.91.2.1 tls IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
577 1.91.2.1 tls if (uh == NULL) {
578 1.91.2.1 tls IP6_STATINC(IP6_STAT_TOOSHORT);
579 1.91.2.1 tls return IPPROTO_DONE;
580 1.91.2.1 tls }
581 1.91.2.2 jdolecek /*
582 1.91.2.2 jdolecek * Enforce alignment requirements that are violated in
583 1.91.2.2 jdolecek * some cases, see kern/50766 for details.
584 1.91.2.2 jdolecek */
585 1.91.2.2 jdolecek if (UDP_HDR_ALIGNED_P(uh) == 0) {
586 1.91.2.2 jdolecek m = m_copyup(m, off + sizeof(struct udphdr), 0);
587 1.91.2.2 jdolecek if (m == NULL) {
588 1.91.2.2 jdolecek IP6_STATINC(IP6_STAT_TOOSHORT);
589 1.91.2.2 jdolecek return IPPROTO_DONE;
590 1.91.2.2 jdolecek }
591 1.91.2.2 jdolecek ip6 = mtod(m, struct ip6_hdr *);
592 1.91.2.2 jdolecek uh = (struct udphdr *)(mtod(m, char *) + off);
593 1.91.2.2 jdolecek }
594 1.91.2.1 tls KASSERT(UDP_HDR_ALIGNED_P(uh));
595 1.91.2.1 tls ulen = ntohs((u_short)uh->uh_ulen);
596 1.91.2.1 tls /*
597 1.91.2.1 tls * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
598 1.91.2.1 tls * iff payload length > 0xffff.
599 1.91.2.1 tls */
600 1.91.2.1 tls if (ulen == 0 && plen > 0xffff)
601 1.91.2.1 tls ulen = plen;
602 1.91.2.1 tls
603 1.91.2.1 tls if (plen != ulen) {
604 1.91.2.1 tls UDP6_STATINC(UDP6_STAT_BADLEN);
605 1.91.2.1 tls goto bad;
606 1.91.2.1 tls }
607 1.91.2.1 tls
608 1.91.2.1 tls /* destination port of 0 is illegal, based on RFC768. */
609 1.91.2.1 tls if (uh->uh_dport == 0)
610 1.91.2.1 tls goto bad;
611 1.91.2.1 tls
612 1.91.2.1 tls /* Be proactive about malicious use of IPv4 mapped address */
613 1.91.2.1 tls if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
614 1.91.2.1 tls IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
615 1.91.2.1 tls /* XXX stat */
616 1.91.2.1 tls goto bad;
617 1.91.2.1 tls }
618 1.91.2.1 tls
619 1.91.2.1 tls /*
620 1.91.2.1 tls * Checksum extended UDP header and data. Maybe skip checksum
621 1.91.2.1 tls * on loopback interfaces.
622 1.91.2.1 tls */
623 1.91.2.1 tls if (udp6_input_checksum(m, uh, off, ulen))
624 1.91.2.1 tls goto bad;
625 1.91.2.1 tls
626 1.91.2.1 tls /*
627 1.91.2.1 tls * Construct source and dst sockaddrs.
628 1.91.2.1 tls */
629 1.91.2.1 tls memset(&src, 0, sizeof(src));
630 1.91.2.1 tls src.sin6_family = AF_INET6;
631 1.91.2.1 tls src.sin6_len = sizeof(struct sockaddr_in6);
632 1.91.2.1 tls src.sin6_addr = ip6->ip6_src;
633 1.91.2.1 tls src.sin6_port = uh->uh_sport;
634 1.91.2.1 tls memset(&dst, 0, sizeof(dst));
635 1.91.2.1 tls dst.sin6_family = AF_INET6;
636 1.91.2.1 tls dst.sin6_len = sizeof(struct sockaddr_in6);
637 1.91.2.1 tls dst.sin6_addr = ip6->ip6_dst;
638 1.91.2.1 tls dst.sin6_port = uh->uh_dport;
639 1.91.2.1 tls
640 1.91.2.1 tls if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
641 1.91.2.1 tls if (m->m_flags & M_MCAST) {
642 1.91.2.1 tls UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
643 1.91.2.1 tls goto bad;
644 1.91.2.1 tls }
645 1.91.2.1 tls UDP6_STATINC(UDP6_STAT_NOPORT);
646 1.91.2.1 tls icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
647 1.91.2.1 tls m = NULL;
648 1.91.2.1 tls }
649 1.91.2.1 tls
650 1.91.2.1 tls bad:
651 1.91.2.1 tls if (m)
652 1.91.2.1 tls m_freem(m);
653 1.91.2.1 tls return IPPROTO_DONE;
654 1.91.2.1 tls }
655 1.91.2.1 tls
656 1.91.2.1 tls static int
657 1.91.2.1 tls udp6_attach(struct socket *so, int proto)
658 1.91.2.1 tls {
659 1.91.2.1 tls struct in6pcb *in6p;
660 1.91.2.1 tls int s, error;
661 1.91.2.1 tls
662 1.91.2.1 tls KASSERT(sotoin6pcb(so) == NULL);
663 1.91.2.1 tls sosetlock(so);
664 1.91.2.1 tls
665 1.91.2.1 tls /*
666 1.91.2.1 tls * MAPPED_ADDR implementation spec:
667 1.91.2.1 tls * Always attach for IPv6, and only when necessary for IPv4.
668 1.91.2.1 tls */
669 1.91.2.1 tls s = splsoftnet();
670 1.91.2.1 tls error = in6_pcballoc(so, &udbtable);
671 1.91.2.1 tls splx(s);
672 1.91.2.1 tls if (error) {
673 1.91.2.1 tls return error;
674 1.91.2.1 tls }
675 1.91.2.1 tls error = soreserve(so, udp6_sendspace, udp6_recvspace);
676 1.91.2.1 tls if (error) {
677 1.91.2.1 tls return error;
678 1.91.2.1 tls }
679 1.91.2.1 tls in6p = sotoin6pcb(so);
680 1.91.2.1 tls in6p->in6p_cksum = -1; /* just to be sure */
681 1.91.2.1 tls
682 1.91.2.1 tls KASSERT(solocked(so));
683 1.91.2.1 tls return 0;
684 1.91.2.1 tls }
685 1.91.2.1 tls
686 1.91.2.1 tls static void
687 1.91.2.1 tls udp6_detach(struct socket *so)
688 1.91.2.1 tls {
689 1.91.2.1 tls struct in6pcb *in6p = sotoin6pcb(so);
690 1.91.2.1 tls int s;
691 1.91.2.1 tls
692 1.91.2.1 tls KASSERT(solocked(so));
693 1.91.2.1 tls KASSERT(in6p != NULL);
694 1.91.2.1 tls
695 1.91.2.1 tls s = splsoftnet();
696 1.91.2.1 tls in6_pcbdetach(in6p);
697 1.91.2.1 tls splx(s);
698 1.91.2.1 tls }
699 1.91.2.1 tls
700 1.91.2.1 tls static int
701 1.91.2.2 jdolecek udp6_accept(struct socket *so, struct sockaddr *nam)
702 1.91.2.1 tls {
703 1.91.2.1 tls KASSERT(solocked(so));
704 1.91.2.1 tls
705 1.91.2.1 tls return EOPNOTSUPP;
706 1.91.2.1 tls }
707 1.91.2.1 tls
708 1.91.2.1 tls static int
709 1.91.2.2 jdolecek udp6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
710 1.91.2.1 tls {
711 1.91.2.1 tls struct in6pcb *in6p = sotoin6pcb(so);
712 1.91.2.2 jdolecek struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
713 1.91.2.1 tls int error = 0;
714 1.91.2.1 tls int s;
715 1.91.2.1 tls
716 1.91.2.1 tls KASSERT(solocked(so));
717 1.91.2.1 tls KASSERT(in6p != NULL);
718 1.91.2.1 tls
719 1.91.2.1 tls s = splsoftnet();
720 1.91.2.2 jdolecek error = in6_pcbbind(in6p, sin6, l);
721 1.91.2.1 tls splx(s);
722 1.91.2.1 tls return error;
723 1.91.2.1 tls }
724 1.91.2.1 tls
725 1.91.2.1 tls static int
726 1.91.2.1 tls udp6_listen(struct socket *so, struct lwp *l)
727 1.91.2.1 tls {
728 1.91.2.1 tls KASSERT(solocked(so));
729 1.91.2.1 tls
730 1.91.2.1 tls return EOPNOTSUPP;
731 1.91.2.1 tls }
732 1.91.2.1 tls
733 1.91.2.1 tls static int
734 1.91.2.2 jdolecek udp6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
735 1.91.2.1 tls {
736 1.91.2.1 tls struct in6pcb *in6p = sotoin6pcb(so);
737 1.91.2.1 tls int error = 0;
738 1.91.2.1 tls int s;
739 1.91.2.1 tls
740 1.91.2.1 tls KASSERT(solocked(so));
741 1.91.2.1 tls KASSERT(in6p != NULL);
742 1.91.2.1 tls
743 1.91.2.1 tls if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
744 1.91.2.1 tls return EISCONN;
745 1.91.2.1 tls s = splsoftnet();
746 1.91.2.2 jdolecek error = in6_pcbconnect(in6p, (struct sockaddr_in6 *)nam, l);
747 1.91.2.1 tls splx(s);
748 1.91.2.1 tls if (error == 0)
749 1.91.2.1 tls soisconnected(so);
750 1.91.2.1 tls
751 1.91.2.1 tls return error;
752 1.91.2.1 tls }
753 1.91.2.1 tls
754 1.91.2.1 tls static int
755 1.91.2.1 tls udp6_connect2(struct socket *so, struct socket *so2)
756 1.91.2.1 tls {
757 1.91.2.1 tls KASSERT(solocked(so));
758 1.91.2.1 tls
759 1.91.2.1 tls return EOPNOTSUPP;
760 1.91.2.1 tls }
761 1.91.2.1 tls
762 1.91.2.1 tls static int
763 1.91.2.1 tls udp6_disconnect(struct socket *so)
764 1.91.2.1 tls {
765 1.91.2.1 tls struct in6pcb *in6p = sotoin6pcb(so);
766 1.91.2.1 tls int s;
767 1.91.2.1 tls
768 1.91.2.1 tls KASSERT(solocked(so));
769 1.91.2.1 tls KASSERT(in6p != NULL);
770 1.91.2.1 tls
771 1.91.2.1 tls if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
772 1.91.2.1 tls return ENOTCONN;
773 1.91.2.1 tls
774 1.91.2.1 tls s = splsoftnet();
775 1.91.2.1 tls in6_pcbdisconnect(in6p);
776 1.91.2.1 tls memset((void *)&in6p->in6p_laddr, 0, sizeof(in6p->in6p_laddr));
777 1.91.2.1 tls splx(s);
778 1.91.2.1 tls
779 1.91.2.1 tls so->so_state &= ~SS_ISCONNECTED; /* XXX */
780 1.91.2.1 tls in6_pcbstate(in6p, IN6P_BOUND); /* XXX */
781 1.91.2.1 tls return 0;
782 1.91.2.1 tls }
783 1.91.2.1 tls
784 1.91.2.1 tls static int
785 1.91.2.1 tls udp6_shutdown(struct socket *so)
786 1.91.2.1 tls {
787 1.91.2.1 tls int s;
788 1.91.2.1 tls
789 1.91.2.1 tls s = splsoftnet();
790 1.91.2.1 tls socantsendmore(so);
791 1.91.2.1 tls splx(s);
792 1.91.2.1 tls
793 1.91.2.1 tls return 0;
794 1.91.2.1 tls }
795 1.91.2.1 tls
796 1.91.2.1 tls static int
797 1.91.2.1 tls udp6_abort(struct socket *so)
798 1.91.2.1 tls {
799 1.91.2.1 tls int s;
800 1.91.2.1 tls
801 1.91.2.1 tls KASSERT(solocked(so));
802 1.91.2.1 tls KASSERT(sotoin6pcb(so) != NULL);
803 1.91.2.1 tls
804 1.91.2.1 tls s = splsoftnet();
805 1.91.2.1 tls soisdisconnected(so);
806 1.91.2.1 tls in6_pcbdetach(sotoin6pcb(so));
807 1.91.2.1 tls splx(s);
808 1.91.2.1 tls
809 1.91.2.1 tls return 0;
810 1.91.2.1 tls }
811 1.91.2.1 tls
812 1.91.2.1 tls static int
813 1.91.2.1 tls udp6_ioctl(struct socket *so, u_long cmd, void *addr6, struct ifnet *ifp)
814 1.91.2.1 tls {
815 1.91.2.1 tls /*
816 1.2 itojun * MAPPED_ADDR implementation info:
817 1.2 itojun * Mapped addr support for PRU_CONTROL is not necessary.
818 1.2 itojun * Because typical user of PRU_CONTROL is such as ifconfig,
819 1.2 itojun * and they don't associate any addr to their socket. Then
820 1.2 itojun * socket family is only hint about the PRU_CONTROL'ed address
821 1.2 itojun * family, especially when getting addrs from kernel.
822 1.2 itojun * So AF_INET socket need to be used to control AF_INET addrs,
823 1.2 itojun * and AF_INET6 socket for AF_INET6 addrs.
824 1.2 itojun */
825 1.91.2.1 tls return in6_control(so, cmd, addr6, ifp);
826 1.91.2.1 tls }
827 1.2 itojun
828 1.91.2.1 tls static int
829 1.91.2.1 tls udp6_stat(struct socket *so, struct stat *ub)
830 1.91.2.1 tls {
831 1.91.2.1 tls KASSERT(solocked(so));
832 1.2 itojun
833 1.91.2.1 tls /* stat: don't bother with a blocksize */
834 1.91.2.1 tls return 0;
835 1.91.2.1 tls }
836 1.2 itojun
837 1.91.2.1 tls static int
838 1.91.2.2 jdolecek udp6_peeraddr(struct socket *so, struct sockaddr *nam)
839 1.91.2.1 tls {
840 1.91.2.1 tls KASSERT(solocked(so));
841 1.91.2.1 tls KASSERT(sotoin6pcb(so) != NULL);
842 1.91.2.1 tls KASSERT(nam != NULL);
843 1.2 itojun
844 1.91.2.2 jdolecek in6_setpeeraddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
845 1.91.2.1 tls return 0;
846 1.91.2.1 tls }
847 1.2 itojun
848 1.91.2.1 tls static int
849 1.91.2.2 jdolecek udp6_sockaddr(struct socket *so, struct sockaddr *nam)
850 1.91.2.1 tls {
851 1.91.2.1 tls KASSERT(solocked(so));
852 1.91.2.1 tls KASSERT(sotoin6pcb(so) != NULL);
853 1.91.2.1 tls KASSERT(nam != NULL);
854 1.2 itojun
855 1.91.2.2 jdolecek in6_setsockaddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
856 1.91.2.1 tls return 0;
857 1.91.2.1 tls }
858 1.2 itojun
859 1.91.2.1 tls static int
860 1.91.2.1 tls udp6_rcvd(struct socket *so, int flags, struct lwp *l)
861 1.91.2.1 tls {
862 1.91.2.1 tls KASSERT(solocked(so));
863 1.2 itojun
864 1.91.2.1 tls return EOPNOTSUPP;
865 1.91.2.1 tls }
866 1.2 itojun
867 1.91.2.1 tls static int
868 1.91.2.1 tls udp6_recvoob(struct socket *so, struct mbuf *m, int flags)
869 1.91.2.1 tls {
870 1.91.2.1 tls KASSERT(solocked(so));
871 1.2 itojun
872 1.91.2.1 tls return EOPNOTSUPP;
873 1.91.2.1 tls }
874 1.2 itojun
875 1.91.2.1 tls static int
876 1.91.2.2 jdolecek udp6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
877 1.91.2.1 tls struct mbuf *control, struct lwp *l)
878 1.91.2.1 tls {
879 1.91.2.1 tls struct in6pcb *in6p = sotoin6pcb(so);
880 1.91.2.1 tls int error = 0;
881 1.91.2.1 tls int s;
882 1.91.2.1 tls
883 1.91.2.1 tls KASSERT(solocked(so));
884 1.91.2.1 tls KASSERT(in6p != NULL);
885 1.91.2.1 tls KASSERT(m != NULL);
886 1.91.2.1 tls
887 1.91.2.1 tls s = splsoftnet();
888 1.91.2.2 jdolecek error = udp6_output(in6p, m, (struct sockaddr_in6 *)nam, control, l);
889 1.91.2.1 tls splx(s);
890 1.2 itojun
891 1.91.2.1 tls return error;
892 1.91.2.1 tls }
893 1.91.2.1 tls
894 1.91.2.1 tls static int
895 1.91.2.1 tls udp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
896 1.91.2.1 tls {
897 1.91.2.1 tls KASSERT(solocked(so));
898 1.91.2.1 tls
899 1.91.2.1 tls if (m)
900 1.91.2.1 tls m_freem(m);
901 1.91.2.1 tls if (control)
902 1.91.2.1 tls m_freem(control);
903 1.91.2.1 tls
904 1.91.2.1 tls return EOPNOTSUPP;
905 1.91.2.1 tls }
906 1.91.2.1 tls
907 1.91.2.1 tls static int
908 1.91.2.1 tls udp6_purgeif(struct socket *so, struct ifnet *ifp)
909 1.91.2.1 tls {
910 1.91.2.1 tls
911 1.91.2.1 tls mutex_enter(softnet_lock);
912 1.91.2.1 tls in6_pcbpurgeif0(&udbtable, ifp);
913 1.91.2.2 jdolecek #ifdef NET_MPSAFE
914 1.91.2.2 jdolecek mutex_exit(softnet_lock);
915 1.91.2.2 jdolecek #endif
916 1.91.2.1 tls in6_purgeif(ifp);
917 1.91.2.2 jdolecek #ifdef NET_MPSAFE
918 1.91.2.2 jdolecek mutex_enter(softnet_lock);
919 1.91.2.2 jdolecek #endif
920 1.91.2.1 tls in6_pcbpurgeif(&udbtable, ifp);
921 1.91.2.1 tls mutex_exit(softnet_lock);
922 1.91.2.1 tls
923 1.91.2.1 tls return 0;
924 1.91.2.1 tls }
925 1.91.2.1 tls
926 1.82 thorpej static int
927 1.82 thorpej sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS)
928 1.82 thorpej {
929 1.82 thorpej
930 1.86 thorpej return (NETSTAT_SYSCTL(udp6stat_percpu, UDP6_NSTATS));
931 1.82 thorpej }
932 1.82 thorpej
933 1.88 pooka static void
934 1.88 pooka sysctl_net_inet6_udp6_setup(struct sysctllog **clog)
935 1.2 itojun {
936 1.91.2.1 tls
937 1.62 atatat sysctl_createv(clog, 0, NULL, NULL,
938 1.62 atatat CTLFLAG_PERMANENT,
939 1.61 atatat CTLTYPE_NODE, "inet6", NULL,
940 1.61 atatat NULL, 0, NULL, 0,
941 1.61 atatat CTL_NET, PF_INET6, CTL_EOL);
942 1.62 atatat sysctl_createv(clog, 0, NULL, NULL,
943 1.62 atatat CTLFLAG_PERMANENT,
944 1.63 atatat CTLTYPE_NODE, "udp6",
945 1.63 atatat SYSCTL_DESCR("UDPv6 related settings"),
946 1.61 atatat NULL, 0, NULL, 0,
947 1.61 atatat CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
948 1.61 atatat
949 1.62 atatat sysctl_createv(clog, 0, NULL, NULL,
950 1.62 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
951 1.63 atatat CTLTYPE_INT, "sendspace",
952 1.63 atatat SYSCTL_DESCR("Default UDP send buffer size"),
953 1.61 atatat NULL, 0, &udp6_sendspace, 0,
954 1.61 atatat CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
955 1.61 atatat CTL_EOL);
956 1.62 atatat sysctl_createv(clog, 0, NULL, NULL,
957 1.62 atatat CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
958 1.63 atatat CTLTYPE_INT, "recvspace",
959 1.63 atatat SYSCTL_DESCR("Default UDP receive buffer size"),
960 1.61 atatat NULL, 0, &udp6_recvspace, 0,
961 1.61 atatat CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
962 1.61 atatat CTL_EOL);
963 1.64 thorpej sysctl_createv(clog, 0, NULL, NULL,
964 1.64 thorpej CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
965 1.64 thorpej CTLTYPE_INT, "do_loopback_cksum",
966 1.64 thorpej SYSCTL_DESCR("Perform UDP checksum on loopback"),
967 1.64 thorpej NULL, 0, &udp_do_loopback_cksum, 0,
968 1.64 thorpej CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
969 1.64 thorpej CTL_EOL);
970 1.65 atatat sysctl_createv(clog, 0, NULL, NULL,
971 1.65 atatat CTLFLAG_PERMANENT,
972 1.67 atatat CTLTYPE_STRUCT, "pcblist",
973 1.65 atatat SYSCTL_DESCR("UDP protocol control block list"),
974 1.65 atatat sysctl_inpcblist, 0, &udbtable, 0,
975 1.65 atatat CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
976 1.65 atatat CTL_EOL);
977 1.70 rpaulo sysctl_createv(clog, 0, NULL, NULL,
978 1.70 rpaulo CTLFLAG_PERMANENT,
979 1.70 rpaulo CTLTYPE_STRUCT, "stats",
980 1.70 rpaulo SYSCTL_DESCR("UDPv6 statistics"),
981 1.82 thorpej sysctl_net_inet6_udp6_stats, 0, NULL, 0,
982 1.70 rpaulo CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
983 1.70 rpaulo CTL_EOL);
984 1.2 itojun }
985 1.82 thorpej
986 1.82 thorpej void
987 1.82 thorpej udp6_statinc(u_int stat)
988 1.82 thorpej {
989 1.82 thorpej
990 1.82 thorpej KASSERT(stat < UDP6_NSTATS);
991 1.82 thorpej UDP6_STATINC(stat);
992 1.82 thorpej }
993 1.91.2.1 tls
994 1.91.2.1 tls PR_WRAP_USRREQS(udp6)
995 1.91.2.1 tls #define udp6_attach udp6_attach_wrapper
996 1.91.2.1 tls #define udp6_detach udp6_detach_wrapper
997 1.91.2.1 tls #define udp6_accept udp6_accept_wrapper
998 1.91.2.1 tls #define udp6_bind udp6_bind_wrapper
999 1.91.2.1 tls #define udp6_listen udp6_listen_wrapper
1000 1.91.2.1 tls #define udp6_connect udp6_connect_wrapper
1001 1.91.2.1 tls #define udp6_connect2 udp6_connect2_wrapper
1002 1.91.2.1 tls #define udp6_disconnect udp6_disconnect_wrapper
1003 1.91.2.1 tls #define udp6_shutdown udp6_shutdown_wrapper
1004 1.91.2.1 tls #define udp6_abort udp6_abort_wrapper
1005 1.91.2.1 tls #define udp6_ioctl udp6_ioctl_wrapper
1006 1.91.2.1 tls #define udp6_stat udp6_stat_wrapper
1007 1.91.2.1 tls #define udp6_peeraddr udp6_peeraddr_wrapper
1008 1.91.2.1 tls #define udp6_sockaddr udp6_sockaddr_wrapper
1009 1.91.2.1 tls #define udp6_rcvd udp6_rcvd_wrapper
1010 1.91.2.1 tls #define udp6_recvoob udp6_recvoob_wrapper
1011 1.91.2.1 tls #define udp6_send udp6_send_wrapper
1012 1.91.2.1 tls #define udp6_sendoob udp6_sendoob_wrapper
1013 1.91.2.1 tls #define udp6_purgeif udp6_purgeif_wrapper
1014 1.91.2.1 tls
1015 1.91.2.1 tls const struct pr_usrreqs udp6_usrreqs = {
1016 1.91.2.1 tls .pr_attach = udp6_attach,
1017 1.91.2.1 tls .pr_detach = udp6_detach,
1018 1.91.2.1 tls .pr_accept = udp6_accept,
1019 1.91.2.1 tls .pr_bind = udp6_bind,
1020 1.91.2.1 tls .pr_listen = udp6_listen,
1021 1.91.2.1 tls .pr_connect = udp6_connect,
1022 1.91.2.1 tls .pr_connect2 = udp6_connect2,
1023 1.91.2.1 tls .pr_disconnect = udp6_disconnect,
1024 1.91.2.1 tls .pr_shutdown = udp6_shutdown,
1025 1.91.2.1 tls .pr_abort = udp6_abort,
1026 1.91.2.1 tls .pr_ioctl = udp6_ioctl,
1027 1.91.2.1 tls .pr_stat = udp6_stat,
1028 1.91.2.1 tls .pr_peeraddr = udp6_peeraddr,
1029 1.91.2.1 tls .pr_sockaddr = udp6_sockaddr,
1030 1.91.2.1 tls .pr_rcvd = udp6_rcvd,
1031 1.91.2.1 tls .pr_recvoob = udp6_recvoob,
1032 1.91.2.1 tls .pr_send = udp6_send,
1033 1.91.2.1 tls .pr_sendoob = udp6_sendoob,
1034 1.91.2.1 tls .pr_purgeif = udp6_purgeif,
1035 1.91.2.1 tls };
1036