udp6_usrreq.c revision 1.113 1 /* $NetBSD: udp6_usrreq.c,v 1.113 2014/08/05 07:55:32 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.113 2014/08/05 07:55:32 rtr 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 (ipsec_used && 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 static int
681 udp6_accept(struct socket *so, struct mbuf *nam)
682 {
683 KASSERT(solocked(so));
684
685 return EOPNOTSUPP;
686 }
687
688 static int
689 udp6_bind(struct socket *so, struct mbuf *nam, struct lwp *l)
690 {
691 struct in6pcb *in6p = sotoin6pcb(so);
692 int error = 0;
693 int s;
694
695 KASSERT(solocked(so));
696 KASSERT(in6p != NULL);
697
698 s = splsoftnet();
699 error = in6_pcbbind(in6p, nam, l);
700 splx(s);
701 return error;
702 }
703
704 static int
705 udp6_listen(struct socket *so, struct lwp *l)
706 {
707 KASSERT(solocked(so));
708
709 return EOPNOTSUPP;
710 }
711
712 static int
713 udp6_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
714 {
715 struct in6pcb *in6p = sotoin6pcb(so);
716 int error = 0;
717 int s;
718
719 KASSERT(solocked(so));
720 KASSERT(in6p != NULL);
721
722 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
723 return EISCONN;
724 s = splsoftnet();
725 error = in6_pcbconnect(in6p, nam, l);
726 splx(s);
727 if (error == 0)
728 soisconnected(so);
729
730 return error;
731 }
732
733 static int
734 udp6_disconnect(struct socket *so)
735 {
736 struct in6pcb *in6p = sotoin6pcb(so);
737 int s;
738
739 KASSERT(solocked(so));
740 KASSERT(in6p != NULL);
741
742 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
743 return ENOTCONN;
744
745 s = splsoftnet();
746 in6_pcbdisconnect(in6p);
747 memset((void *)&in6p->in6p_laddr, 0, sizeof(in6p->in6p_laddr));
748 splx(s);
749
750 so->so_state &= ~SS_ISCONNECTED; /* XXX */
751 in6_pcbstate(in6p, IN6P_BOUND); /* XXX */
752 return 0;
753 }
754
755 static int
756 udp6_shutdown(struct socket *so)
757 {
758 int s;
759
760 s = splsoftnet();
761 socantsendmore(so);
762 splx(s);
763
764 return 0;
765 }
766
767 static int
768 udp6_abort(struct socket *so)
769 {
770 int s;
771
772 KASSERT(solocked(so));
773 KASSERT(sotoin6pcb(so) != NULL);
774
775 s = splsoftnet();
776 soisdisconnected(so);
777 in6_pcbdetach(sotoin6pcb(so));
778 splx(s);
779
780 return 0;
781 }
782
783 static int
784 udp6_ioctl(struct socket *so, u_long cmd, void *addr6, struct ifnet *ifp)
785 {
786 /*
787 * MAPPED_ADDR implementation info:
788 * Mapped addr support for PRU_CONTROL is not necessary.
789 * Because typical user of PRU_CONTROL is such as ifconfig,
790 * and they don't associate any addr to their socket. Then
791 * socket family is only hint about the PRU_CONTROL'ed address
792 * family, especially when getting addrs from kernel.
793 * So AF_INET socket need to be used to control AF_INET addrs,
794 * and AF_INET6 socket for AF_INET6 addrs.
795 */
796 return in6_control(so, cmd, addr6, ifp);
797 }
798
799 static int
800 udp6_stat(struct socket *so, struct stat *ub)
801 {
802 KASSERT(solocked(so));
803
804 /* stat: don't bother with a blocksize */
805 return 0;
806 }
807
808 static int
809 udp6_peeraddr(struct socket *so, struct mbuf *nam)
810 {
811 KASSERT(solocked(so));
812 KASSERT(sotoin6pcb(so) != NULL);
813 KASSERT(nam != NULL);
814
815 in6_setpeeraddr(sotoin6pcb(so), nam);
816 return 0;
817 }
818
819 static int
820 udp6_sockaddr(struct socket *so, struct mbuf *nam)
821 {
822 KASSERT(solocked(so));
823 KASSERT(sotoin6pcb(so) != NULL);
824 KASSERT(nam != NULL);
825
826 in6_setsockaddr(sotoin6pcb(so), nam);
827 return 0;
828 }
829
830 static int
831 udp6_recvoob(struct socket *so, struct mbuf *m, int flags)
832 {
833 KASSERT(solocked(so));
834
835 return EOPNOTSUPP;
836 }
837
838 static int
839 udp6_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
840 struct mbuf *control, struct lwp *l)
841 {
842 struct in6pcb *in6p = sotoin6pcb(so);
843 int error = 0;
844 int s;
845
846 KASSERT(solocked(so));
847 KASSERT(in6p != NULL);
848 KASSERT(m != NULL);
849
850 s = splsoftnet();
851 error = udp6_output(in6p, m, nam, control, l);
852 splx(s);
853
854 return error;
855 }
856
857 static int
858 udp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
859 {
860 KASSERT(solocked(so));
861
862 if (m)
863 m_freem(m);
864 if (control)
865 m_freem(control);
866
867 return EOPNOTSUPP;
868 }
869
870 int
871 udp6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr6,
872 struct mbuf *control, struct lwp *l)
873 {
874 struct in6pcb *in6p = sotoin6pcb(so);
875 int error = 0;
876
877 KASSERT(req != PRU_ATTACH);
878 KASSERT(req != PRU_DETACH);
879 KASSERT(req != PRU_ACCEPT);
880 KASSERT(req != PRU_BIND);
881 KASSERT(req != PRU_LISTEN);
882 KASSERT(req != PRU_CONNECT);
883 KASSERT(req != PRU_DISCONNECT);
884 KASSERT(req != PRU_SHUTDOWN);
885 KASSERT(req != PRU_ABORT);
886 KASSERT(req != PRU_CONTROL);
887 KASSERT(req != PRU_SENSE);
888 KASSERT(req != PRU_PEERADDR);
889 KASSERT(req != PRU_SOCKADDR);
890 KASSERT(req != PRU_RCVOOB);
891 KASSERT(req != PRU_SEND);
892 KASSERT(req != PRU_SENDOOB);
893
894 if (req == PRU_PURGEIF) {
895 mutex_enter(softnet_lock);
896 in6_pcbpurgeif0(&udbtable, (struct ifnet *)control);
897 in6_purgeif((struct ifnet *)control);
898 in6_pcbpurgeif(&udbtable, (struct ifnet *)control);
899 mutex_exit(softnet_lock);
900 return 0;
901 }
902 if (in6p == NULL) {
903 error = EINVAL;
904 goto release;
905 }
906
907 switch (req) {
908 case PRU_CONNECT2:
909 case PRU_FASTTIMO:
910 case PRU_SLOWTIMO:
911 case PRU_PROTORCV:
912 case PRU_PROTOSEND:
913 error = EOPNOTSUPP;
914 break;
915
916 case PRU_RCVD:
917 return EOPNOTSUPP; /* do not free mbuf's */
918
919 default:
920 panic("udp6_usrreq");
921 }
922
923 release:
924 if (control != NULL)
925 m_freem(control);
926 if (m != NULL)
927 m_freem(m);
928 return error;
929 }
930
931 static int
932 sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS)
933 {
934
935 return (NETSTAT_SYSCTL(udp6stat_percpu, UDP6_NSTATS));
936 }
937
938 static void
939 sysctl_net_inet6_udp6_setup(struct sysctllog **clog)
940 {
941
942 sysctl_createv(clog, 0, NULL, NULL,
943 CTLFLAG_PERMANENT,
944 CTLTYPE_NODE, "inet6", NULL,
945 NULL, 0, NULL, 0,
946 CTL_NET, PF_INET6, CTL_EOL);
947 sysctl_createv(clog, 0, NULL, NULL,
948 CTLFLAG_PERMANENT,
949 CTLTYPE_NODE, "udp6",
950 SYSCTL_DESCR("UDPv6 related settings"),
951 NULL, 0, NULL, 0,
952 CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
953
954 sysctl_createv(clog, 0, NULL, NULL,
955 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
956 CTLTYPE_INT, "sendspace",
957 SYSCTL_DESCR("Default UDP send buffer size"),
958 NULL, 0, &udp6_sendspace, 0,
959 CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
960 CTL_EOL);
961 sysctl_createv(clog, 0, NULL, NULL,
962 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
963 CTLTYPE_INT, "recvspace",
964 SYSCTL_DESCR("Default UDP receive buffer size"),
965 NULL, 0, &udp6_recvspace, 0,
966 CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
967 CTL_EOL);
968 sysctl_createv(clog, 0, NULL, NULL,
969 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
970 CTLTYPE_INT, "do_loopback_cksum",
971 SYSCTL_DESCR("Perform UDP checksum on loopback"),
972 NULL, 0, &udp_do_loopback_cksum, 0,
973 CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
974 CTL_EOL);
975 sysctl_createv(clog, 0, NULL, NULL,
976 CTLFLAG_PERMANENT,
977 CTLTYPE_STRUCT, "pcblist",
978 SYSCTL_DESCR("UDP protocol control block list"),
979 sysctl_inpcblist, 0, &udbtable, 0,
980 CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
981 CTL_EOL);
982 sysctl_createv(clog, 0, NULL, NULL,
983 CTLFLAG_PERMANENT,
984 CTLTYPE_STRUCT, "stats",
985 SYSCTL_DESCR("UDPv6 statistics"),
986 sysctl_net_inet6_udp6_stats, 0, NULL, 0,
987 CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
988 CTL_EOL);
989 }
990
991 void
992 udp6_statinc(u_int stat)
993 {
994
995 KASSERT(stat < UDP6_NSTATS);
996 UDP6_STATINC(stat);
997 }
998
999 PR_WRAP_USRREQS(udp6)
1000 #define udp6_attach udp6_attach_wrapper
1001 #define udp6_detach udp6_detach_wrapper
1002 #define udp6_accept udp6_accept_wrapper
1003 #define udp6_bind udp6_bind_wrapper
1004 #define udp6_listen udp6_listen_wrapper
1005 #define udp6_connect udp6_connect_wrapper
1006 #define udp6_disconnect udp6_disconnect_wrapper
1007 #define udp6_shutdown udp6_shutdown_wrapper
1008 #define udp6_abort udp6_abort_wrapper
1009 #define udp6_ioctl udp6_ioctl_wrapper
1010 #define udp6_stat udp6_stat_wrapper
1011 #define udp6_peeraddr udp6_peeraddr_wrapper
1012 #define udp6_sockaddr udp6_sockaddr_wrapper
1013 #define udp6_recvoob udp6_recvoob_wrapper
1014 #define udp6_send udp6_send_wrapper
1015 #define udp6_sendoob udp6_sendoob_wrapper
1016 #define udp6_usrreq udp6_usrreq_wrapper
1017
1018 const struct pr_usrreqs udp6_usrreqs = {
1019 .pr_attach = udp6_attach,
1020 .pr_detach = udp6_detach,
1021 .pr_accept = udp6_accept,
1022 .pr_bind = udp6_bind,
1023 .pr_listen = udp6_listen,
1024 .pr_connect = udp6_connect,
1025 .pr_disconnect = udp6_disconnect,
1026 .pr_shutdown = udp6_shutdown,
1027 .pr_abort = udp6_abort,
1028 .pr_ioctl = udp6_ioctl,
1029 .pr_stat = udp6_stat,
1030 .pr_peeraddr = udp6_peeraddr,
1031 .pr_sockaddr = udp6_sockaddr,
1032 .pr_recvoob = udp6_recvoob,
1033 .pr_send = udp6_send,
1034 .pr_sendoob = udp6_sendoob,
1035 .pr_generic = udp6_usrreq,
1036 };
1037