udp_usrreq.c revision 1.89 1 /* $NetBSD: udp_usrreq.c,v 1.89 2001/11/04 20:55:29 matt 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 CIRCLEQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue) {
639 if (inp->inp_lport != *dport)
640 continue;
641 if (!in_nullhost(inp->inp_laddr)) {
642 if (!in_hosteq(inp->inp_laddr, *dst4))
643 continue;
644 }
645 if (!in_nullhost(inp->inp_faddr)) {
646 if (!in_hosteq(inp->inp_faddr, *src4) ||
647 inp->inp_fport != *sport)
648 continue;
649 }
650
651 last = inp;
652 udp4_sendup(m, off, (struct sockaddr *)src,
653 inp->inp_socket);
654 rcvcnt++;
655
656 /*
657 * Don't look for additional matches if this one does
658 * not have either the SO_REUSEPORT or SO_REUSEADDR
659 * socket options set. This heuristic avoids searching
660 * through all pcbs in the common case of a non-shared
661 * port. It assumes that an application will never
662 * clear these options after setting them.
663 */
664 if ((inp->inp_socket->so_options &
665 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
666 break;
667 }
668 } else {
669 /*
670 * Locate pcb for datagram.
671 */
672 inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
673 if (inp == 0) {
674 ++udpstat.udps_pcbhashmiss;
675 inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
676 if (inp == 0)
677 return rcvcnt;
678 }
679
680 udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
681 rcvcnt++;
682 }
683
684 bad:
685 return rcvcnt;
686 }
687 #endif
688
689 #ifdef INET6
690 static int
691 in6_mcmatch(in6p, ia6, ifp)
692 struct in6pcb *in6p;
693 struct in6_addr *ia6;
694 struct ifnet *ifp;
695 {
696 struct ip6_moptions *im6o = in6p->in6p_moptions;
697 struct in6_multi_mship *imm;
698
699 if (im6o == NULL)
700 return 0;
701
702 LIST_FOREACH(imm, &im6o->im6o_memberships, i6mm_chain) {
703 if ((ifp == NULL || imm->i6mm_maddr->in6m_ifp == ifp) &&
704 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, ia6))
705 return 1;
706 }
707 return 0;
708 }
709
710 static int
711 udp6_realinput(af, src, dst, m, off)
712 int af; /* af on packet */
713 struct sockaddr_in6 *src;
714 struct sockaddr_in6 *dst;
715 struct mbuf *m;
716 int off; /* offset of udphdr */
717 {
718 u_int16_t sport, dport;
719 int rcvcnt;
720 struct in6_addr src6, dst6;
721 const struct in_addr *dst4;
722 struct in6pcb *in6p;
723
724 rcvcnt = 0;
725 off += sizeof(struct udphdr); /* now, offset of payload */
726
727 if (af != AF_INET && af != AF_INET6)
728 goto bad;
729 if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
730 goto bad;
731
732 in6_embedscope(&src6, src, NULL, NULL);
733 sport = src->sin6_port;
734 in6_embedscope(&dst6, dst, NULL, NULL);
735 dport = dst->sin6_port;
736 dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
737
738 if (IN6_IS_ADDR_MULTICAST(&dst6) ||
739 (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
740 struct in6pcb *last;
741 /*
742 * Deliver a multicast or broadcast datagram to *all* sockets
743 * for which the local and remote addresses and ports match
744 * those of the incoming datagram. This allows more than
745 * one process to receive multi/broadcasts on the same port.
746 * (This really ought to be done for unicast datagrams as
747 * well, but that would cause problems with existing
748 * applications that open both address-specific sockets and
749 * a wildcard socket listening to the same port -- they would
750 * end up receiving duplicates of every unicast datagram.
751 * Those applications open the multiple sockets to overcome an
752 * inadequacy of the UDP socket interface, but for backwards
753 * compatibility we avoid the problem here rather than
754 * fixing the interface. Maybe 4.5BSD will remedy this?)
755 */
756
757 /*
758 * KAME note: usually we drop udpiphdr from mbuf here.
759 * we need udpiphdr for IPsec processing so we do that later.
760 */
761 /*
762 * Locate pcb(s) for datagram.
763 */
764 for (in6p = udb6.in6p_next; in6p != &udb6;
765 in6p = in6p->in6p_next) {
766 if (in6p->in6p_lport != dport)
767 continue;
768 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
769 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6) &&
770 !in6_mcmatch(in6p, &dst6, m->m_pkthdr.rcvif))
771 continue;
772 }
773 else {
774 if (IN6_IS_ADDR_V4MAPPED(&dst6) &&
775 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
776 continue;
777 }
778 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
779 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
780 &src6) || in6p->in6p_fport != sport)
781 continue;
782 }
783 else {
784 if (IN6_IS_ADDR_V4MAPPED(&src6) &&
785 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
786 continue;
787 }
788
789 last = in6p;
790 udp6_sendup(m, off, (struct sockaddr *)src,
791 in6p->in6p_socket);
792 rcvcnt++;
793
794 /*
795 * Don't look for additional matches if this one does
796 * not have either the SO_REUSEPORT or SO_REUSEADDR
797 * socket options set. This heuristic avoids searching
798 * through all pcbs in the common case of a non-shared
799 * port. It assumes that an application will never
800 * clear these options after setting them.
801 */
802 if ((in6p->in6p_socket->so_options &
803 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
804 break;
805 }
806 } else {
807 /*
808 * Locate pcb for datagram.
809 */
810 in6p = in6_pcblookup_connect(&udb6, &src6, sport,
811 &dst6, dport, 0);
812 if (in6p == 0) {
813 ++udpstat.udps_pcbhashmiss;
814 in6p = in6_pcblookup_bind(&udb6, &dst6, dport, 0);
815 if (in6p == 0)
816 return rcvcnt;
817 }
818
819 udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
820 rcvcnt++;
821 }
822
823 bad:
824 return rcvcnt;
825 }
826 #endif
827
828 #ifdef INET
829 /*
830 * Notify a udp user of an asynchronous error;
831 * just wake up so that he can collect error status.
832 */
833 static void
834 udp_notify(inp, errno)
835 struct inpcb *inp;
836 int errno;
837 {
838
839 inp->inp_socket->so_error = errno;
840 sorwakeup(inp->inp_socket);
841 sowwakeup(inp->inp_socket);
842 }
843
844 void *
845 udp_ctlinput(cmd, sa, v)
846 int cmd;
847 struct sockaddr *sa;
848 void *v;
849 {
850 struct ip *ip = v;
851 struct udphdr *uh;
852 void (*notify) __P((struct inpcb *, int)) = udp_notify;
853 int errno;
854
855 if (sa->sa_family != AF_INET
856 || sa->sa_len != sizeof(struct sockaddr_in))
857 return NULL;
858 if ((unsigned)cmd >= PRC_NCMDS)
859 return NULL;
860 errno = inetctlerrmap[cmd];
861 if (PRC_IS_REDIRECT(cmd))
862 notify = in_rtchange, ip = 0;
863 else if (cmd == PRC_HOSTDEAD)
864 ip = 0;
865 else if (errno == 0)
866 return NULL;
867 if (ip) {
868 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
869 in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
870 ip->ip_src, uh->uh_sport, errno, notify);
871
872 /* XXX mapped address case */
873 } else
874 in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
875 notify);
876 return NULL;
877 }
878
879 int
880 #if __STDC__
881 udp_output(struct mbuf *m, ...)
882 #else
883 udp_output(m, va_alist)
884 struct mbuf *m;
885 va_dcl
886 #endif
887 {
888 struct inpcb *inp;
889 struct udpiphdr *ui;
890 int len = m->m_pkthdr.len;
891 int error = 0;
892 va_list ap;
893
894 va_start(ap, m);
895 inp = va_arg(ap, struct inpcb *);
896 va_end(ap);
897
898 /*
899 * Calculate data length and get a mbuf
900 * for UDP and IP headers.
901 */
902 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
903 if (m == 0) {
904 error = ENOBUFS;
905 goto release;
906 }
907
908 /*
909 * Compute the packet length of the IP header, and
910 * punt if the length looks bogus.
911 */
912 if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
913 error = EMSGSIZE;
914 goto release;
915 }
916
917 /*
918 * Fill in mbuf with extended UDP header
919 * and addresses and length put into network format.
920 */
921 ui = mtod(m, struct udpiphdr *);
922 ui->ui_pr = IPPROTO_UDP;
923 ui->ui_src = inp->inp_laddr;
924 ui->ui_dst = inp->inp_faddr;
925 ui->ui_sport = inp->inp_lport;
926 ui->ui_dport = inp->inp_fport;
927 ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
928
929 /*
930 * Set up checksum and output datagram.
931 */
932 if (udpcksum) {
933 /*
934 * XXX Cache pseudo-header checksum part for
935 * XXX "connected" UDP sockets.
936 */
937 ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
938 ui->ui_dst.s_addr, htons((u_int16_t)len +
939 sizeof(struct udphdr) + IPPROTO_UDP));
940 m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
941 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
942 } else
943 ui->ui_sum = 0;
944 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
945 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
946 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
947 udpstat.udps_opackets++;
948
949 #ifdef IPSEC
950 if (ipsec_setsocket(m, inp->inp_socket) != 0) {
951 error = ENOBUFS;
952 goto release;
953 }
954 #endif /*IPSEC*/
955
956 return (ip_output(m, inp->inp_options, &inp->inp_route,
957 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
958 inp->inp_moptions));
959
960 release:
961 m_freem(m);
962 return (error);
963 }
964
965 int udp_sendspace = 9216; /* really max datagram size */
966 int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
967 /* 40 1K datagrams */
968
969 /*ARGSUSED*/
970 int
971 udp_usrreq(so, req, m, nam, control, p)
972 struct socket *so;
973 int req;
974 struct mbuf *m, *nam, *control;
975 struct proc *p;
976 {
977 struct inpcb *inp;
978 int s;
979 int error = 0;
980
981 if (req == PRU_CONTROL)
982 return (in_control(so, (long)m, (caddr_t)nam,
983 (struct ifnet *)control, p));
984
985 if (req == PRU_PURGEIF) {
986 in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
987 in_purgeif((struct ifnet *)control);
988 in_pcbpurgeif(&udbtable, (struct ifnet *)control);
989 return (0);
990 }
991
992 s = splsoftnet();
993 inp = sotoinpcb(so);
994 #ifdef DIAGNOSTIC
995 if (req != PRU_SEND && req != PRU_SENDOOB && control)
996 panic("udp_usrreq: unexpected control mbuf");
997 #endif
998 if (inp == 0 && req != PRU_ATTACH) {
999 error = EINVAL;
1000 goto release;
1001 }
1002
1003 /*
1004 * Note: need to block udp_input while changing
1005 * the udp pcb queue and/or pcb addresses.
1006 */
1007 switch (req) {
1008
1009 case PRU_ATTACH:
1010 if (inp != 0) {
1011 error = EISCONN;
1012 break;
1013 }
1014 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1015 error = soreserve(so, udp_sendspace, udp_recvspace);
1016 if (error)
1017 break;
1018 }
1019 error = in_pcballoc(so, &udbtable);
1020 if (error)
1021 break;
1022 inp = sotoinpcb(so);
1023 inp->inp_ip.ip_ttl = ip_defttl;
1024 break;
1025
1026 case PRU_DETACH:
1027 in_pcbdetach(inp);
1028 break;
1029
1030 case PRU_BIND:
1031 error = in_pcbbind(inp, nam, p);
1032 break;
1033
1034 case PRU_LISTEN:
1035 error = EOPNOTSUPP;
1036 break;
1037
1038 case PRU_CONNECT:
1039 error = in_pcbconnect(inp, nam);
1040 if (error)
1041 break;
1042 soisconnected(so);
1043 break;
1044
1045 case PRU_CONNECT2:
1046 error = EOPNOTSUPP;
1047 break;
1048
1049 case PRU_DISCONNECT:
1050 /*soisdisconnected(so);*/
1051 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1052 in_pcbdisconnect(inp);
1053 inp->inp_laddr = zeroin_addr; /* XXX */
1054 in_pcbstate(inp, INP_BOUND); /* XXX */
1055 break;
1056
1057 case PRU_SHUTDOWN:
1058 socantsendmore(so);
1059 break;
1060
1061 case PRU_RCVD:
1062 error = EOPNOTSUPP;
1063 break;
1064
1065 case PRU_SEND:
1066 if (control && control->m_len) {
1067 m_freem(control);
1068 m_freem(m);
1069 error = EINVAL;
1070 break;
1071 }
1072 {
1073 struct in_addr laddr; /* XXX */
1074
1075 if (nam) {
1076 laddr = inp->inp_laddr; /* XXX */
1077 if ((so->so_state & SS_ISCONNECTED) != 0) {
1078 error = EISCONN;
1079 goto die;
1080 }
1081 error = in_pcbconnect(inp, nam);
1082 if (error) {
1083 die:
1084 m_freem(m);
1085 break;
1086 }
1087 } else {
1088 if ((so->so_state & SS_ISCONNECTED) == 0) {
1089 error = ENOTCONN;
1090 goto die;
1091 }
1092 }
1093 error = udp_output(m, inp);
1094 if (nam) {
1095 in_pcbdisconnect(inp);
1096 inp->inp_laddr = laddr; /* XXX */
1097 in_pcbstate(inp, INP_BOUND); /* XXX */
1098 }
1099 }
1100 break;
1101
1102 case PRU_SENSE:
1103 /*
1104 * stat: don't bother with a blocksize.
1105 */
1106 splx(s);
1107 return (0);
1108
1109 case PRU_RCVOOB:
1110 error = EOPNOTSUPP;
1111 break;
1112
1113 case PRU_SENDOOB:
1114 m_freem(control);
1115 m_freem(m);
1116 error = EOPNOTSUPP;
1117 break;
1118
1119 case PRU_SOCKADDR:
1120 in_setsockaddr(inp, nam);
1121 break;
1122
1123 case PRU_PEERADDR:
1124 in_setpeeraddr(inp, nam);
1125 break;
1126
1127 default:
1128 panic("udp_usrreq");
1129 }
1130
1131 release:
1132 splx(s);
1133 return (error);
1134 }
1135
1136 /*
1137 * Sysctl for udp variables.
1138 */
1139 int
1140 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1141 int *name;
1142 u_int namelen;
1143 void *oldp;
1144 size_t *oldlenp;
1145 void *newp;
1146 size_t newlen;
1147 {
1148 /* All sysctl names at this level are terminal. */
1149 if (namelen != 1)
1150 return (ENOTDIR);
1151
1152 switch (name[0]) {
1153 case UDPCTL_CHECKSUM:
1154 return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
1155 case UDPCTL_SENDSPACE:
1156 return (sysctl_int(oldp, oldlenp, newp, newlen,
1157 &udp_sendspace));
1158 case UDPCTL_RECVSPACE:
1159 return (sysctl_int(oldp, oldlenp, newp, newlen,
1160 &udp_recvspace));
1161 default:
1162 return (ENOPROTOOPT);
1163 }
1164 /* NOTREACHED */
1165 }
1166 #endif
1167