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