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