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