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