udp_usrreq.c revision 1.88 1 /* $NetBSD: udp_usrreq.c,v 1.88 2001/11/02 02:37:50 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
65 */
66
67 #include "opt_inet.h"
68 #include "opt_ipsec.h"
69 #include "opt_inet_csum.h"
70 #include "opt_ipkdb.h"
71
72 #include <sys/param.h>
73 #include <sys/malloc.h>
74 #include <sys/mbuf.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/errno.h>
79 #include <sys/stat.h>
80 #include <sys/systm.h>
81 #include <sys/proc.h>
82 #include <sys/domain.h>
83 #include <sys/sysctl.h>
84
85 #include <net/if.h>
86 #include <net/route.h>
87
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/in_var.h>
91 #include <netinet/ip.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/ip_var.h>
94 #include <netinet/ip_icmp.h>
95 #include <netinet/udp.h>
96 #include <netinet/udp_var.h>
97
98 #ifdef INET6
99 #include <netinet/ip6.h>
100 #include <netinet/icmp6.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/in6_pcb.h>
103 #include <netinet6/udp6_var.h>
104 #endif
105
106 #ifdef PULLDOWN_TEST
107 #ifndef INET6
108 /* always need ip6.h for IP6_EXTHDR_GET */
109 #include <netinet/ip6.h>
110 #endif
111 #endif
112
113 #include "faith.h"
114 #if defined(NFAITH) && NFAITH > 0
115 #include <net/if_faith.h>
116 #endif
117
118 #include <machine/stdarg.h>
119
120 #ifdef IPSEC
121 #include <netinet6/ipsec.h>
122 #include <netkey/key.h>
123 #endif /*IPSEC*/
124
125 #ifdef IPKDB
126 #include <ipkdb/ipkdb.h>
127 #endif
128
129 /*
130 * UDP protocol implementation.
131 * Per RFC 768, August, 1980.
132 */
133 #ifndef COMPAT_42
134 int udpcksum = 1;
135 #else
136 int udpcksum = 0; /* XXX */
137 #endif
138
139 #ifdef INET
140 static void udp4_sendup __P((struct mbuf *, int, struct sockaddr *,
141 struct socket *));
142 static int udp4_realinput __P((struct sockaddr_in *, struct sockaddr_in *,
143 struct mbuf *, int));
144 #endif
145 #ifdef INET6
146 static void udp6_sendup __P((struct mbuf *, int, struct sockaddr *,
147 struct socket *));
148 static int in6_mcmatch __P((struct in6pcb *, struct in6_addr *,
149 struct ifnet *));
150 static int udp6_realinput __P((int, struct sockaddr_in6 *,
151 struct sockaddr_in6 *, struct mbuf *, int));
152 #endif
153 #ifdef INET
154 static void udp_notify __P((struct inpcb *, int));
155 #endif
156
157 #ifndef UDBHASHSIZE
158 #define UDBHASHSIZE 128
159 #endif
160 int udbhashsize = UDBHASHSIZE;
161
162 #ifdef UDP_CSUM_COUNTERS
163 #include <sys/device.h>
164
165 struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
166 NULL, "udp", "hwcsum bad");
167 struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
168 NULL, "udp", "hwcsum ok");
169 struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
170 NULL, "udp", "hwcsum data");
171 struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
172 NULL, "udp", "swcsum");
173
174 #define UDP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
175
176 #else
177
178 #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */
179
180 #endif /* UDP_CSUM_COUNTERS */
181
182 void
183 udp_init()
184 {
185
186 #ifdef INET
187 in_pcbinit(&udbtable, udbhashsize, udbhashsize);
188 #endif
189
190 #ifdef UDP_CSUM_COUNTERS
191 evcnt_attach_static(&udp_hwcsum_bad);
192 evcnt_attach_static(&udp_hwcsum_ok);
193 evcnt_attach_static(&udp_hwcsum_data);
194 evcnt_attach_static(&udp_swcsum);
195 #endif /* UDP_CSUM_COUNTERS */
196 }
197
198 #ifdef INET
199 void
200 #if __STDC__
201 udp_input(struct mbuf *m, ...)
202 #else
203 udp_input(m, va_alist)
204 struct mbuf *m;
205 va_dcl
206 #endif
207 {
208 va_list ap;
209 struct sockaddr_in src, dst;
210 struct ip *ip;
211 struct udphdr *uh;
212 int iphlen, proto;
213 int len;
214 int n;
215
216 va_start(ap, m);
217 iphlen = va_arg(ap, int);
218 proto = va_arg(ap, int);
219 va_end(ap);
220
221 udpstat.udps_ipackets++;
222
223 #ifndef PULLDOWN_TEST
224 /*
225 * Strip IP options, if any; should skip this,
226 * make available to user, and use on returned packets,
227 * but we don't yet have a way to check the checksum
228 * with options still present.
229 */
230 if (iphlen > sizeof (struct ip)) {
231 ip_stripoptions(m, (struct mbuf *)0);
232 iphlen = sizeof(struct ip);
233 }
234 #else
235 /*
236 * we may enable the above code if we save and pass IPv4 options
237 * to the userland.
238 */
239 #endif
240
241 /*
242 * Get IP and UDP header together in first mbuf.
243 */
244 ip = mtod(m, struct ip *);
245 #ifndef PULLDOWN_TEST
246 if (m->m_len < iphlen + sizeof(struct udphdr)) {
247 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
248 udpstat.udps_hdrops++;
249 return;
250 }
251 ip = mtod(m, struct ip *);
252 }
253 uh = (struct udphdr *)((caddr_t)ip + iphlen);
254 #else
255 IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
256 if (uh == NULL) {
257 udpstat.udps_hdrops++;
258 return;
259 }
260 #endif
261
262 /* destination port of 0 is illegal, based on RFC768. */
263 if (uh->uh_dport == 0)
264 goto bad;
265
266 /*
267 * Make mbuf data length reflect UDP length.
268 * If not enough data to reflect UDP length, drop.
269 */
270 len = ntohs((u_int16_t)uh->uh_ulen);
271 if (ip->ip_len != iphlen + len) {
272 if (ip->ip_len < iphlen + len || len < sizeof(struct udphdr)) {
273 udpstat.udps_badlen++;
274 goto bad;
275 }
276 m_adj(m, iphlen + len - ip->ip_len);
277 }
278
279 /*
280 * Checksum extended UDP header and data.
281 */
282 if (uh->uh_sum) {
283 switch (m->m_pkthdr.csum_flags &
284 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
285 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
286 case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
287 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
288 goto badcsum;
289
290 case M_CSUM_UDPv4|M_CSUM_DATA:
291 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
292 if ((m->m_pkthdr.csum_data ^ 0xffff) != 0)
293 goto badcsum;
294 break;
295
296 case M_CSUM_UDPv4:
297 /* Checksum was okay. */
298 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
299 break;
300
301 default:
302 /* Need to compute it ourselves. */
303 UDP_CSUM_COUNTER_INCR(&udp_swcsum);
304 if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
305 goto badcsum;
306 break;
307 }
308 }
309
310 /* construct source and dst sockaddrs. */
311 bzero(&src, sizeof(src));
312 src.sin_family = AF_INET;
313 src.sin_len = sizeof(struct sockaddr_in);
314 bcopy(&ip->ip_src, &src.sin_addr, sizeof(src.sin_addr));
315 src.sin_port = uh->uh_sport;
316 bzero(&dst, sizeof(dst));
317 dst.sin_family = AF_INET;
318 dst.sin_len = sizeof(struct sockaddr_in);
319 bcopy(&ip->ip_dst, &dst.sin_addr, sizeof(dst.sin_addr));
320 dst.sin_port = uh->uh_dport;
321
322 n = udp4_realinput(&src, &dst, m, iphlen);
323 #ifdef INET6
324 if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
325 struct sockaddr_in6 src6, dst6;
326
327 bzero(&src6, sizeof(src6));
328 src6.sin6_family = AF_INET6;
329 src6.sin6_len = sizeof(struct sockaddr_in6);
330 src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
331 bcopy(&ip->ip_src, &src6.sin6_addr.s6_addr[12],
332 sizeof(ip->ip_src));
333 src6.sin6_port = uh->uh_sport;
334 bzero(&dst6, sizeof(dst6));
335 dst6.sin6_family = AF_INET6;
336 dst6.sin6_len = sizeof(struct sockaddr_in6);
337 dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
338 bcopy(&ip->ip_dst, &dst6.sin6_addr.s6_addr[12],
339 sizeof(ip->ip_dst));
340 dst6.sin6_port = uh->uh_dport;
341
342 n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
343 }
344 #endif
345
346 if (n == 0) {
347 if (m->m_flags & (M_BCAST | M_MCAST)) {
348 udpstat.udps_noportbcast++;
349 goto bad;
350 }
351 udpstat.udps_noport++;
352 #ifdef IPKDB
353 if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
354 m, iphlen + sizeof(struct udphdr),
355 m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
356 /*
357 * It was a debugger connect packet,
358 * just drop it now
359 */
360 goto bad;
361 }
362 #endif
363 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
364 m = NULL;
365 }
366
367 bad:
368 if (m)
369 m_freem(m);
370 return;
371
372 badcsum:
373 m_freem(m);
374 udpstat.udps_badsum++;
375 }
376 #endif
377
378 #ifdef INET6
379 int
380 udp6_input(mp, offp, proto)
381 struct mbuf **mp;
382 int *offp, proto;
383 {
384 struct mbuf *m = *mp;
385 int off = *offp;
386 struct sockaddr_in6 src, dst;
387 struct ip6_hdr *ip6;
388 struct udphdr *uh;
389 u_int32_t plen, ulen;
390
391 #ifndef PULLDOWN_TEST
392 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
393 #endif
394 ip6 = mtod(m, struct ip6_hdr *);
395
396 #if defined(NFAITH) && 0 < NFAITH
397 if (faithprefix(&ip6->ip6_dst)) {
398 /* send icmp6 host unreach? */
399 m_freem(m);
400 return IPPROTO_DONE;
401 }
402 #endif
403
404 udp6stat.udp6s_ipackets++;
405
406 /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */
407 plen = m->m_pkthdr.len - off;
408 #ifndef PULLDOWN_TEST
409 uh = (struct udphdr *)((caddr_t)ip6 + off);
410 #else
411 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
412 if (uh == NULL) {
413 ip6stat.ip6s_tooshort++;
414 return IPPROTO_DONE;
415 }
416 #endif
417 ulen = ntohs((u_short)uh->uh_ulen);
418 /*
419 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
420 * iff payload length > 0xffff.
421 */
422 if (ulen == 0 && plen > 0xffff)
423 ulen = plen;
424
425 if (plen != ulen) {
426 udp6stat.udp6s_badlen++;
427 goto bad;
428 }
429
430 /* destination port of 0 is illegal, based on RFC768. */
431 if (uh->uh_dport == 0)
432 goto bad;
433
434 /* Be proactive about malicious use of IPv4 mapped address */
435 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
436 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
437 /* XXX stat */
438 goto bad;
439 }
440
441 /*
442 * Checksum extended UDP header and data.
443 */
444 if (uh->uh_sum == 0)
445 udp6stat.udp6s_nosum++;
446 else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
447 udp6stat.udp6s_badsum++;
448 goto bad;
449 }
450
451 /*
452 * Construct source and dst sockaddrs.
453 * Note that ifindex (s6_addr16[1]) is already filled.
454 */
455 bzero(&src, sizeof(src));
456 src.sin6_family = AF_INET6;
457 src.sin6_len = sizeof(struct sockaddr_in6);
458 /* KAME hack: recover scopeid */
459 (void)in6_recoverscope(&src, &ip6->ip6_src, m->m_pkthdr.rcvif);
460 src.sin6_port = uh->uh_sport;
461 bzero(&dst, sizeof(dst));
462 dst.sin6_family = AF_INET6;
463 dst.sin6_len = sizeof(struct sockaddr_in6);
464 /* KAME hack: recover scopeid */
465 (void)in6_recoverscope(&dst, &ip6->ip6_dst, m->m_pkthdr.rcvif);
466 dst.sin6_port = uh->uh_dport;
467
468 if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
469 if (m->m_flags & M_MCAST) {
470 udp6stat.udp6s_noportmcast++;
471 goto bad;
472 }
473 udp6stat.udp6s_noport++;
474 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
475 m = NULL;
476 }
477
478 bad:
479 if (m)
480 m_freem(m);
481 return IPPROTO_DONE;
482 }
483 #endif
484
485 #ifdef INET
486 static void
487 udp4_sendup(m, off, src, so)
488 struct mbuf *m;
489 int off; /* offset of data portion */
490 struct sockaddr *src;
491 struct socket *so;
492 {
493 struct mbuf *opts = NULL;
494 struct mbuf *n;
495 struct inpcb *inp = NULL;
496 #ifdef INET6
497 struct in6pcb *in6p = NULL;
498 #endif
499
500 if (!so)
501 return;
502 switch (so->so_proto->pr_domain->dom_family) {
503 case AF_INET:
504 inp = sotoinpcb(so);
505 break;
506 #ifdef INET6
507 case AF_INET6:
508 in6p = sotoin6pcb(so);
509 break;
510 #endif
511 default:
512 return;
513 }
514
515 #ifdef IPSEC
516 /* check AH/ESP integrity. */
517 if (so != NULL && ipsec4_in_reject_so(m, so)) {
518 ipsecstat.in_polvio++;
519 return;
520 }
521 #endif /*IPSEC*/
522
523 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
524 if (inp && (inp->inp_flags & INP_CONTROLOPTS
525 || so->so_options & SO_TIMESTAMP)) {
526 struct ip *ip = mtod(n, struct ip *);
527 ip_savecontrol(inp, &opts, ip, n);
528 }
529
530 m_adj(n, off);
531 if (sbappendaddr(&so->so_rcv, src, n,
532 opts) == 0) {
533 m_freem(n);
534 if (opts)
535 m_freem(opts);
536 udpstat.udps_fullsock++;
537 } else
538 sorwakeup(so);
539 }
540 }
541 #endif
542
543 #ifdef INET6
544 static void
545 udp6_sendup(m, off, src, so)
546 struct mbuf *m;
547 int off; /* offset of data portion */
548 struct sockaddr *src;
549 struct socket *so;
550 {
551 struct mbuf *opts = NULL;
552 struct mbuf *n;
553 struct in6pcb *in6p = NULL;
554
555 if (!so)
556 return;
557 if (so->so_proto->pr_domain->dom_family != AF_INET6)
558 return;
559 in6p = sotoin6pcb(so);
560
561 #ifdef IPSEC
562 /* check AH/ESP integrity. */
563 if (so != NULL && ipsec6_in_reject_so(m, so)) {
564 ipsec6stat.in_polvio++;
565 return;
566 }
567 #endif /*IPSEC*/
568
569 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
570 if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
571 || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
572 struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
573 ip6_savecontrol(in6p, &opts, ip6, n);
574 }
575
576 m_adj(n, off);
577 if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
578 m_freem(n);
579 if (opts)
580 m_freem(opts);
581 udp6stat.udp6s_fullsock++;
582 } else
583 sorwakeup(so);
584 }
585 }
586 #endif
587
588 #ifdef INET
589 static int
590 udp4_realinput(src, dst, m, off)
591 struct sockaddr_in *src;
592 struct sockaddr_in *dst;
593 struct mbuf *m;
594 int off; /* offset of udphdr */
595 {
596 u_int16_t *sport, *dport;
597 int rcvcnt;
598 struct in_addr *src4, *dst4;
599 struct inpcb *inp;
600
601 rcvcnt = 0;
602 off += sizeof(struct udphdr); /* now, offset of payload */
603
604 if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
605 goto bad;
606
607 src4 = &src->sin_addr;
608 sport = &src->sin_port;
609 dst4 = &dst->sin_addr;
610 dport = &dst->sin_port;
611
612 if (IN_MULTICAST(dst4->s_addr) ||
613 in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
614 struct inpcb *last;
615 /*
616 * Deliver a multicast or broadcast datagram to *all* sockets
617 * for which the local and remote addresses and ports match
618 * those of the incoming datagram. This allows more than
619 * one process to receive multi/broadcasts on the same port.
620 * (This really ought to be done for unicast datagrams as
621 * well, but that would cause problems with existing
622 * applications that open both address-specific sockets and
623 * a wildcard socket listening to the same port -- they would
624 * end up receiving duplicates of every unicast datagram.
625 * Those applications open the multiple sockets to overcome an
626 * inadequacy of the UDP socket interface, but for backwards
627 * compatibility we avoid the problem here rather than
628 * fixing the interface. Maybe 4.5BSD will remedy this?)
629 */
630
631 /*
632 * KAME note: usually we drop udpiphdr from mbuf here.
633 * we need udpiphdr for IPsec processing so we do that later.
634 */
635 /*
636 * Locate pcb(s) for datagram.
637 */
638 for (inp = udbtable.inpt_queue.cqh_first;
639 inp != (struct inpcb *)&udbtable.inpt_queue;
640 inp = inp->inp_queue.cqe_next) {
641 if (inp->inp_lport != *dport)
642 continue;
643 if (!in_nullhost(inp->inp_laddr)) {
644 if (!in_hosteq(inp->inp_laddr, *dst4))
645 continue;
646 }
647 if (!in_nullhost(inp->inp_faddr)) {
648 if (!in_hosteq(inp->inp_faddr, *src4) ||
649 inp->inp_fport != *sport)
650 continue;
651 }
652
653 last = inp;
654 udp4_sendup(m, off, (struct sockaddr *)src,
655 inp->inp_socket);
656 rcvcnt++;
657
658 /*
659 * Don't look for additional matches if this one does
660 * not have either the SO_REUSEPORT or SO_REUSEADDR
661 * socket options set. This heuristic avoids searching
662 * through all pcbs in the common case of a non-shared
663 * port. It assumes that an application will never
664 * clear these options after setting them.
665 */
666 if ((inp->inp_socket->so_options &
667 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
668 break;
669 }
670 } else {
671 /*
672 * Locate pcb for datagram.
673 */
674 inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
675 if (inp == 0) {
676 ++udpstat.udps_pcbhashmiss;
677 inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
678 if (inp == 0)
679 return rcvcnt;
680 }
681
682 udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
683 rcvcnt++;
684 }
685
686 bad:
687 return rcvcnt;
688 }
689 #endif
690
691 #ifdef INET6
692 static int
693 in6_mcmatch(in6p, ia6, ifp)
694 struct in6pcb *in6p;
695 struct in6_addr *ia6;
696 struct ifnet *ifp;
697 {
698 struct ip6_moptions *im6o = in6p->in6p_moptions;
699 struct in6_multi_mship *imm;
700
701 if (im6o == NULL)
702 return 0;
703
704 for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
705 imm = imm->i6mm_chain.le_next) {
706 if ((ifp == NULL ||
707 imm->i6mm_maddr->in6m_ifp == ifp) &&
708 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
709 ia6))
710 return 1;
711 }
712 return 0;
713 }
714
715 static int
716 udp6_realinput(af, src, dst, m, off)
717 int af; /* af on packet */
718 struct sockaddr_in6 *src;
719 struct sockaddr_in6 *dst;
720 struct mbuf *m;
721 int off; /* offset of udphdr */
722 {
723 u_int16_t sport, dport;
724 int rcvcnt;
725 struct in6_addr src6, dst6;
726 const struct in_addr *dst4;
727 struct in6pcb *in6p;
728
729 rcvcnt = 0;
730 off += sizeof(struct udphdr); /* now, offset of payload */
731
732 if (af != AF_INET && af != AF_INET6)
733 goto bad;
734 if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
735 goto bad;
736
737 in6_embedscope(&src6, src, NULL, NULL);
738 sport = src->sin6_port;
739 in6_embedscope(&dst6, dst, NULL, NULL);
740 dport = dst->sin6_port;
741 dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
742
743 if (IN6_IS_ADDR_MULTICAST(&dst6) ||
744 (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
745 struct in6pcb *last;
746 /*
747 * Deliver a multicast or broadcast datagram to *all* sockets
748 * for which the local and remote addresses and ports match
749 * those of the incoming datagram. This allows more than
750 * one process to receive multi/broadcasts on the same port.
751 * (This really ought to be done for unicast datagrams as
752 * well, but that would cause problems with existing
753 * applications that open both address-specific sockets and
754 * a wildcard socket listening to the same port -- they would
755 * end up receiving duplicates of every unicast datagram.
756 * Those applications open the multiple sockets to overcome an
757 * inadequacy of the UDP socket interface, but for backwards
758 * compatibility we avoid the problem here rather than
759 * fixing the interface. Maybe 4.5BSD will remedy this?)
760 */
761
762 /*
763 * KAME note: usually we drop udpiphdr from mbuf here.
764 * we need udpiphdr for IPsec processing so we do that later.
765 */
766 /*
767 * Locate pcb(s) for datagram.
768 */
769 for (in6p = udb6.in6p_next; in6p != &udb6;
770 in6p = in6p->in6p_next) {
771 if (in6p->in6p_lport != dport)
772 continue;
773 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
774 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6) &&
775 !in6_mcmatch(in6p, &dst6, m->m_pkthdr.rcvif))
776 continue;
777 }
778 else {
779 if (IN6_IS_ADDR_V4MAPPED(&dst6) &&
780 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
781 continue;
782 }
783 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
784 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
785 &src6) || in6p->in6p_fport != sport)
786 continue;
787 }
788 else {
789 if (IN6_IS_ADDR_V4MAPPED(&src6) &&
790 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
791 continue;
792 }
793
794 last = in6p;
795 udp6_sendup(m, off, (struct sockaddr *)src,
796 in6p->in6p_socket);
797 rcvcnt++;
798
799 /*
800 * Don't look for additional matches if this one does
801 * not have either the SO_REUSEPORT or SO_REUSEADDR
802 * socket options set. This heuristic avoids searching
803 * through all pcbs in the common case of a non-shared
804 * port. It assumes that an application will never
805 * clear these options after setting them.
806 */
807 if ((in6p->in6p_socket->so_options &
808 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
809 break;
810 }
811 } else {
812 /*
813 * Locate pcb for datagram.
814 */
815 in6p = in6_pcblookup_connect(&udb6, &src6, sport,
816 &dst6, dport, 0);
817 if (in6p == 0) {
818 ++udpstat.udps_pcbhashmiss;
819 in6p = in6_pcblookup_bind(&udb6, &dst6, dport, 0);
820 if (in6p == 0)
821 return rcvcnt;
822 }
823
824 udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
825 rcvcnt++;
826 }
827
828 bad:
829 return rcvcnt;
830 }
831 #endif
832
833 #ifdef INET
834 /*
835 * Notify a udp user of an asynchronous error;
836 * just wake up so that he can collect error status.
837 */
838 static void
839 udp_notify(inp, errno)
840 struct inpcb *inp;
841 int errno;
842 {
843
844 inp->inp_socket->so_error = errno;
845 sorwakeup(inp->inp_socket);
846 sowwakeup(inp->inp_socket);
847 }
848
849 void *
850 udp_ctlinput(cmd, sa, v)
851 int cmd;
852 struct sockaddr *sa;
853 void *v;
854 {
855 struct ip *ip = v;
856 struct udphdr *uh;
857 void (*notify) __P((struct inpcb *, int)) = udp_notify;
858 int errno;
859
860 if (sa->sa_family != AF_INET
861 || sa->sa_len != sizeof(struct sockaddr_in))
862 return NULL;
863 if ((unsigned)cmd >= PRC_NCMDS)
864 return NULL;
865 errno = inetctlerrmap[cmd];
866 if (PRC_IS_REDIRECT(cmd))
867 notify = in_rtchange, ip = 0;
868 else if (cmd == PRC_HOSTDEAD)
869 ip = 0;
870 else if (errno == 0)
871 return NULL;
872 if (ip) {
873 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
874 in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
875 ip->ip_src, uh->uh_sport, errno, notify);
876
877 /* XXX mapped address case */
878 } else
879 in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
880 notify);
881 return NULL;
882 }
883
884 int
885 #if __STDC__
886 udp_output(struct mbuf *m, ...)
887 #else
888 udp_output(m, va_alist)
889 struct mbuf *m;
890 va_dcl
891 #endif
892 {
893 struct inpcb *inp;
894 struct udpiphdr *ui;
895 int len = m->m_pkthdr.len;
896 int error = 0;
897 va_list ap;
898
899 va_start(ap, m);
900 inp = va_arg(ap, struct inpcb *);
901 va_end(ap);
902
903 /*
904 * Calculate data length and get a mbuf
905 * for UDP and IP headers.
906 */
907 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
908 if (m == 0) {
909 error = ENOBUFS;
910 goto release;
911 }
912
913 /*
914 * Compute the packet length of the IP header, and
915 * punt if the length looks bogus.
916 */
917 if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
918 error = EMSGSIZE;
919 goto release;
920 }
921
922 /*
923 * Fill in mbuf with extended UDP header
924 * and addresses and length put into network format.
925 */
926 ui = mtod(m, struct udpiphdr *);
927 ui->ui_pr = IPPROTO_UDP;
928 ui->ui_src = inp->inp_laddr;
929 ui->ui_dst = inp->inp_faddr;
930 ui->ui_sport = inp->inp_lport;
931 ui->ui_dport = inp->inp_fport;
932 ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
933
934 /*
935 * Set up checksum and output datagram.
936 */
937 if (udpcksum) {
938 /*
939 * XXX Cache pseudo-header checksum part for
940 * XXX "connected" UDP sockets.
941 */
942 ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
943 ui->ui_dst.s_addr, htons((u_int16_t)len +
944 sizeof(struct udphdr) + IPPROTO_UDP));
945 m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
946 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
947 } else
948 ui->ui_sum = 0;
949 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
950 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
951 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
952 udpstat.udps_opackets++;
953
954 #ifdef IPSEC
955 if (ipsec_setsocket(m, inp->inp_socket) != 0) {
956 error = ENOBUFS;
957 goto release;
958 }
959 #endif /*IPSEC*/
960
961 return (ip_output(m, inp->inp_options, &inp->inp_route,
962 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
963 inp->inp_moptions));
964
965 release:
966 m_freem(m);
967 return (error);
968 }
969
970 int udp_sendspace = 9216; /* really max datagram size */
971 int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
972 /* 40 1K datagrams */
973
974 /*ARGSUSED*/
975 int
976 udp_usrreq(so, req, m, nam, control, p)
977 struct socket *so;
978 int req;
979 struct mbuf *m, *nam, *control;
980 struct proc *p;
981 {
982 struct inpcb *inp;
983 int s;
984 int error = 0;
985
986 if (req == PRU_CONTROL)
987 return (in_control(so, (long)m, (caddr_t)nam,
988 (struct ifnet *)control, p));
989
990 if (req == PRU_PURGEIF) {
991 in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
992 in_purgeif((struct ifnet *)control);
993 in_pcbpurgeif(&udbtable, (struct ifnet *)control);
994 return (0);
995 }
996
997 s = splsoftnet();
998 inp = sotoinpcb(so);
999 #ifdef DIAGNOSTIC
1000 if (req != PRU_SEND && req != PRU_SENDOOB && control)
1001 panic("udp_usrreq: unexpected control mbuf");
1002 #endif
1003 if (inp == 0 && req != PRU_ATTACH) {
1004 error = EINVAL;
1005 goto release;
1006 }
1007
1008 /*
1009 * Note: need to block udp_input while changing
1010 * the udp pcb queue and/or pcb addresses.
1011 */
1012 switch (req) {
1013
1014 case PRU_ATTACH:
1015 if (inp != 0) {
1016 error = EISCONN;
1017 break;
1018 }
1019 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1020 error = soreserve(so, udp_sendspace, udp_recvspace);
1021 if (error)
1022 break;
1023 }
1024 error = in_pcballoc(so, &udbtable);
1025 if (error)
1026 break;
1027 inp = sotoinpcb(so);
1028 inp->inp_ip.ip_ttl = ip_defttl;
1029 break;
1030
1031 case PRU_DETACH:
1032 in_pcbdetach(inp);
1033 break;
1034
1035 case PRU_BIND:
1036 error = in_pcbbind(inp, nam, p);
1037 break;
1038
1039 case PRU_LISTEN:
1040 error = EOPNOTSUPP;
1041 break;
1042
1043 case PRU_CONNECT:
1044 error = in_pcbconnect(inp, nam);
1045 if (error)
1046 break;
1047 soisconnected(so);
1048 break;
1049
1050 case PRU_CONNECT2:
1051 error = EOPNOTSUPP;
1052 break;
1053
1054 case PRU_DISCONNECT:
1055 /*soisdisconnected(so);*/
1056 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1057 in_pcbdisconnect(inp);
1058 inp->inp_laddr = zeroin_addr; /* XXX */
1059 in_pcbstate(inp, INP_BOUND); /* XXX */
1060 break;
1061
1062 case PRU_SHUTDOWN:
1063 socantsendmore(so);
1064 break;
1065
1066 case PRU_RCVD:
1067 error = EOPNOTSUPP;
1068 break;
1069
1070 case PRU_SEND:
1071 if (control && control->m_len) {
1072 m_freem(control);
1073 m_freem(m);
1074 error = EINVAL;
1075 break;
1076 }
1077 {
1078 struct in_addr laddr; /* XXX */
1079
1080 if (nam) {
1081 laddr = inp->inp_laddr; /* XXX */
1082 if ((so->so_state & SS_ISCONNECTED) != 0) {
1083 error = EISCONN;
1084 goto die;
1085 }
1086 error = in_pcbconnect(inp, nam);
1087 if (error) {
1088 die:
1089 m_freem(m);
1090 break;
1091 }
1092 } else {
1093 if ((so->so_state & SS_ISCONNECTED) == 0) {
1094 error = ENOTCONN;
1095 goto die;
1096 }
1097 }
1098 error = udp_output(m, inp);
1099 if (nam) {
1100 in_pcbdisconnect(inp);
1101 inp->inp_laddr = laddr; /* XXX */
1102 in_pcbstate(inp, INP_BOUND); /* XXX */
1103 }
1104 }
1105 break;
1106
1107 case PRU_SENSE:
1108 /*
1109 * stat: don't bother with a blocksize.
1110 */
1111 splx(s);
1112 return (0);
1113
1114 case PRU_RCVOOB:
1115 error = EOPNOTSUPP;
1116 break;
1117
1118 case PRU_SENDOOB:
1119 m_freem(control);
1120 m_freem(m);
1121 error = EOPNOTSUPP;
1122 break;
1123
1124 case PRU_SOCKADDR:
1125 in_setsockaddr(inp, nam);
1126 break;
1127
1128 case PRU_PEERADDR:
1129 in_setpeeraddr(inp, nam);
1130 break;
1131
1132 default:
1133 panic("udp_usrreq");
1134 }
1135
1136 release:
1137 splx(s);
1138 return (error);
1139 }
1140
1141 /*
1142 * Sysctl for udp variables.
1143 */
1144 int
1145 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1146 int *name;
1147 u_int namelen;
1148 void *oldp;
1149 size_t *oldlenp;
1150 void *newp;
1151 size_t newlen;
1152 {
1153 /* All sysctl names at this level are terminal. */
1154 if (namelen != 1)
1155 return (ENOTDIR);
1156
1157 switch (name[0]) {
1158 case UDPCTL_CHECKSUM:
1159 return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
1160 case UDPCTL_SENDSPACE:
1161 return (sysctl_int(oldp, oldlenp, newp, newlen,
1162 &udp_sendspace));
1163 case UDPCTL_RECVSPACE:
1164 return (sysctl_int(oldp, oldlenp, newp, newlen,
1165 &udp_recvspace));
1166 default:
1167 return (ENOPROTOOPT);
1168 }
1169 /* NOTREACHED */
1170 }
1171 #endif
1172