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