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