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