udp_usrreq.c revision 1.120 1 /* $NetBSD: udp_usrreq.c,v 1.120 2004/05/01 02:20:43 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. 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.120 2004/05/01 02:20:43 matt 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 (struct mbuf *, int, struct sockaddr *,
149 struct socket *);
150 static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
151 struct mbuf *, int);
152 #endif
153 #ifdef INET6
154 static void udp6_sendup (struct mbuf *, int, struct sockaddr *,
155 struct socket *);
156 static int udp6_realinput (int, struct sockaddr_in6 *,
157 struct sockaddr_in6 *, struct mbuf *, int);
158 #endif
159 #ifdef INET
160 static void udp_notify (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 EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
189 EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
190 EVCNT_ATTACH_STATIC(udp_hwcsum_data);
191 EVCNT_ATTACH_STATIC(udp_swcsum);
192
193 #else
194
195 #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */
196
197 #endif /* UDP_CSUM_COUNTERS */
198
199 void
200 udp_init(void)
201 {
202
203 in_pcbinit(&udbtable, udbhashsize, udbhashsize);
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 udp_input(struct mbuf *m, ...)
213 {
214 va_list ap;
215 struct sockaddr_in src, dst;
216 struct ip *ip;
217 struct udphdr *uh;
218 int iphlen;
219 int len;
220 int n;
221 u_int16_t ip_len;
222
223 va_start(ap, m);
224 iphlen = va_arg(ap, int);
225 (void)va_arg(ap, int); /* ignore value, advance ap */
226 va_end(ap);
227
228 MCLAIM(m, &udp_rx_mowner);
229 udpstat.udps_ipackets++;
230
231 /*
232 * Get IP and UDP header together in first mbuf.
233 */
234 ip = mtod(m, struct ip *);
235 IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
236 if (uh == NULL) {
237 udpstat.udps_hdrops++;
238 return;
239 }
240 KASSERT(UDP_HDR_ALIGNED_P(uh));
241
242 /* destination port of 0 is illegal, based on RFC768. */
243 if (uh->uh_dport == 0)
244 goto bad;
245
246 /*
247 * Make mbuf data length reflect UDP length.
248 * If not enough data to reflect UDP length, drop.
249 */
250 ip_len = ntohs(ip->ip_len);
251 len = ntohs((u_int16_t)uh->uh_ulen);
252 if (ip_len != iphlen + len) {
253 if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
254 udpstat.udps_badlen++;
255 goto bad;
256 }
257 m_adj(m, iphlen + len - ip_len);
258 }
259
260 /*
261 * Checksum extended UDP header and data.
262 */
263 if (uh->uh_sum) {
264 switch (m->m_pkthdr.csum_flags &
265 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
266 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
267 case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
268 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
269 goto badcsum;
270
271 case M_CSUM_UDPv4|M_CSUM_DATA: {
272 u_int32_t hw_csum = m->m_pkthdr.csum_data;
273 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
274 if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR)
275 hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
276 ip->ip_dst.s_addr,
277 htons(hw_csum + len + IPPROTO_UDP));
278 if ((hw_csum ^ 0xffff) != 0)
279 goto badcsum;
280 break;
281 }
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(struct mbuf **mp, int *offp, int proto)
368 {
369 struct mbuf *m = *mp;
370 int off = *offp;
371 struct sockaddr_in6 src, dst;
372 struct ip6_hdr *ip6;
373 struct udphdr *uh;
374 u_int32_t plen, ulen;
375
376 ip6 = mtod(m, struct ip6_hdr *);
377
378 #if defined(NFAITH) && 0 < NFAITH
379 if (faithprefix(&ip6->ip6_dst)) {
380 /* send icmp6 host unreach? */
381 m_freem(m);
382 return IPPROTO_DONE;
383 }
384 #endif
385
386 udp6stat.udp6s_ipackets++;
387
388 /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */
389 plen = m->m_pkthdr.len - off;
390 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
391 if (uh == NULL) {
392 ip6stat.ip6s_tooshort++;
393 return IPPROTO_DONE;
394 }
395 KASSERT(UDP_HDR_ALIGNED_P(uh));
396 ulen = ntohs((u_short)uh->uh_ulen);
397 /*
398 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
399 * iff payload length > 0xffff.
400 */
401 if (ulen == 0 && plen > 0xffff)
402 ulen = plen;
403
404 if (plen != ulen) {
405 udp6stat.udp6s_badlen++;
406 goto bad;
407 }
408
409 /* destination port of 0 is illegal, based on RFC768. */
410 if (uh->uh_dport == 0)
411 goto bad;
412
413 /* Be proactive about malicious use of IPv4 mapped address */
414 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
415 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
416 /* XXX stat */
417 goto bad;
418 }
419
420 /*
421 * Checksum extended UDP header and data.
422 */
423 if (uh->uh_sum == 0) {
424 udp6stat.udp6s_nosum++;
425 goto bad;
426 }
427 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(struct mbuf *m, int off /* offset of data portion */,
469 struct sockaddr *src, struct socket *so)
470 {
471 struct mbuf *opts = NULL;
472 struct mbuf *n;
473 struct inpcb *inp = NULL;
474
475 if (!so)
476 return;
477 switch (so->so_proto->pr_domain->dom_family) {
478 case AF_INET:
479 inp = sotoinpcb(so);
480 break;
481 #ifdef INET6
482 case AF_INET6:
483 break;
484 #endif
485 default:
486 return;
487 }
488
489 #if defined(IPSEC) || defined(FAST_IPSEC)
490 /* check AH/ESP integrity. */
491 if (so != NULL && ipsec4_in_reject_so(m, so)) {
492 ipsecstat.in_polvio++;
493 if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
494 icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
495 0, 0);
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(struct mbuf *m, int off /* offset of data portion */,
523 struct sockaddr *src, struct socket *so)
524 {
525 struct mbuf *opts = NULL;
526 struct mbuf *n;
527 struct in6pcb *in6p = NULL;
528
529 if (!so)
530 return;
531 if (so->so_proto->pr_domain->dom_family != AF_INET6)
532 return;
533 in6p = sotoin6pcb(so);
534
535 #if defined(IPSEC) || defined(FAST_IPSEC)
536 /* check AH/ESP integrity. */
537 if (so != NULL && ipsec6_in_reject_so(m, so)) {
538 ipsec6stat.in_polvio++;
539 if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
540 icmp6_error(n, ICMP6_DST_UNREACH,
541 ICMP6_DST_UNREACH_ADMIN, 0);
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(struct sockaddr_in *src, struct sockaddr_in *dst,
568 struct mbuf *m, int off /* offset of udphdr */)
569 {
570 u_int16_t *sport, *dport;
571 int rcvcnt;
572 struct in_addr *src4, *dst4;
573 struct inpcb_hdr *inph;
574 struct inpcb *inp;
575
576 rcvcnt = 0;
577 off += sizeof(struct udphdr); /* now, offset of payload */
578
579 if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
580 goto bad;
581
582 src4 = &src->sin_addr;
583 sport = &src->sin_port;
584 dst4 = &dst->sin_addr;
585 dport = &dst->sin_port;
586
587 if (IN_MULTICAST(dst4->s_addr) ||
588 in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
589 /*
590 * Deliver a multicast or broadcast datagram to *all* sockets
591 * for which the local and remote addresses and ports match
592 * those of the incoming datagram. This allows more than
593 * one process to receive multi/broadcasts on the same port.
594 * (This really ought to be done for unicast datagrams as
595 * well, but that would cause problems with existing
596 * applications that open both address-specific sockets and
597 * a wildcard socket listening to the same port -- they would
598 * end up receiving duplicates of every unicast datagram.
599 * Those applications open the multiple sockets to overcome an
600 * inadequacy of the UDP socket interface, but for backwards
601 * compatibility we avoid the problem here rather than
602 * fixing the interface. Maybe 4.5BSD will remedy this?)
603 */
604
605 /*
606 * KAME note: traditionally we dropped udpiphdr from mbuf here.
607 * we need udpiphdr for IPsec processing so we do that later.
608 */
609 /*
610 * Locate pcb(s) for datagram.
611 */
612 CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
613 inp = (struct inpcb *)inph;
614 if (inp->inp_af != AF_INET)
615 continue;
616
617 if (inp->inp_lport != *dport)
618 continue;
619 if (!in_nullhost(inp->inp_laddr)) {
620 if (!in_hosteq(inp->inp_laddr, *dst4))
621 continue;
622 }
623 if (!in_nullhost(inp->inp_faddr)) {
624 if (!in_hosteq(inp->inp_faddr, *src4) ||
625 inp->inp_fport != *sport)
626 continue;
627 }
628
629 udp4_sendup(m, off, (struct sockaddr *)src,
630 inp->inp_socket);
631 rcvcnt++;
632
633 /*
634 * Don't look for additional matches if this one does
635 * not have either the SO_REUSEPORT or SO_REUSEADDR
636 * socket options set. This heuristic avoids searching
637 * through all pcbs in the common case of a non-shared
638 * port. It assumes that an application will never
639 * clear these options after setting them.
640 */
641 if ((inp->inp_socket->so_options &
642 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
643 break;
644 }
645 } else {
646 /*
647 * Locate pcb for datagram.
648 */
649 inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
650 if (inp == 0) {
651 ++udpstat.udps_pcbhashmiss;
652 inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
653 if (inp == 0)
654 return rcvcnt;
655 }
656
657 udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
658 rcvcnt++;
659 }
660
661 bad:
662 return rcvcnt;
663 }
664 #endif
665
666 #ifdef INET6
667 static int
668 udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
669 struct mbuf *m, int off)
670 {
671 u_int16_t sport, dport;
672 int rcvcnt;
673 struct in6_addr src6, dst6;
674 const struct in_addr *dst4;
675 struct inpcb_hdr *inph;
676 struct in6pcb *in6p;
677
678 rcvcnt = 0;
679 off += sizeof(struct udphdr); /* now, offset of payload */
680
681 if (af != AF_INET && af != AF_INET6)
682 goto bad;
683 if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
684 goto bad;
685
686 in6_embedscope(&src6, src, NULL, NULL);
687 sport = src->sin6_port;
688 in6_embedscope(&dst6, dst, NULL, NULL);
689 dport = dst->sin6_port;
690 dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
691
692 if (IN6_IS_ADDR_MULTICAST(&dst6) ||
693 (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
694 /*
695 * Deliver a multicast or broadcast datagram to *all* sockets
696 * for which the local and remote addresses and ports match
697 * those of the incoming datagram. This allows more than
698 * one process to receive multi/broadcasts on the same port.
699 * (This really ought to be done for unicast datagrams as
700 * well, but that would cause problems with existing
701 * applications that open both address-specific sockets and
702 * a wildcard socket listening to the same port -- they would
703 * end up receiving duplicates of every unicast datagram.
704 * Those applications open the multiple sockets to overcome an
705 * inadequacy of the UDP socket interface, but for backwards
706 * compatibility we avoid the problem here rather than
707 * fixing the interface. Maybe 4.5BSD will remedy this?)
708 */
709
710 /*
711 * KAME note: traditionally we dropped udpiphdr from mbuf here.
712 * we need udpiphdr for IPsec processing so we do that later.
713 */
714 /*
715 * Locate pcb(s) for datagram.
716 */
717 CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
718 in6p = (struct in6pcb *)inph;
719 if (in6p->in6p_af != AF_INET6)
720 continue;
721
722 if (in6p->in6p_lport != dport)
723 continue;
724 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
725 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6))
726 continue;
727 } else {
728 if (IN6_IS_ADDR_V4MAPPED(&dst6) &&
729 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
730 continue;
731 }
732 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
733 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
734 &src6) || in6p->in6p_fport != sport)
735 continue;
736 } else {
737 if (IN6_IS_ADDR_V4MAPPED(&src6) &&
738 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
739 continue;
740 }
741
742 udp6_sendup(m, off, (struct sockaddr *)src,
743 in6p->in6p_socket);
744 rcvcnt++;
745
746 /*
747 * Don't look for additional matches if this one does
748 * not have either the SO_REUSEPORT or SO_REUSEADDR
749 * socket options set. This heuristic avoids searching
750 * through all pcbs in the common case of a non-shared
751 * port. It assumes that an application will never
752 * clear these options after setting them.
753 */
754 if ((in6p->in6p_socket->so_options &
755 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
756 break;
757 }
758 } else {
759 /*
760 * Locate pcb for datagram.
761 */
762 in6p = in6_pcblookup_connect(&udbtable, &src6, sport,
763 &dst6, dport, 0);
764 if (in6p == 0) {
765 ++udpstat.udps_pcbhashmiss;
766 in6p = in6_pcblookup_bind(&udbtable, &dst6, dport, 0);
767 if (in6p == 0)
768 return rcvcnt;
769 }
770
771 udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
772 rcvcnt++;
773 }
774
775 bad:
776 return rcvcnt;
777 }
778 #endif
779
780 #ifdef INET
781 /*
782 * Notify a udp user of an asynchronous error;
783 * just wake up so that he can collect error status.
784 */
785 static void
786 udp_notify(struct inpcb *inp, int errno)
787 {
788 inp->inp_socket->so_error = errno;
789 sorwakeup(inp->inp_socket);
790 sowwakeup(inp->inp_socket);
791 }
792
793 void *
794 udp_ctlinput(int cmd, struct sockaddr *sa, void *v)
795 {
796 struct ip *ip = v;
797 struct udphdr *uh;
798 void (*notify)(struct inpcb *, int) = udp_notify;
799 int errno;
800
801 if (sa->sa_family != AF_INET
802 || sa->sa_len != sizeof(struct sockaddr_in))
803 return NULL;
804 if ((unsigned)cmd >= PRC_NCMDS)
805 return NULL;
806 errno = inetctlerrmap[cmd];
807 if (PRC_IS_REDIRECT(cmd))
808 notify = in_rtchange, ip = 0;
809 else if (cmd == PRC_HOSTDEAD)
810 ip = 0;
811 else if (errno == 0)
812 return NULL;
813 if (ip) {
814 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
815 in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
816 ip->ip_src, uh->uh_sport, errno, notify);
817
818 /* XXX mapped address case */
819 } else
820 in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
821 notify);
822 return NULL;
823 }
824
825 int
826 udp_output(struct mbuf *m, ...)
827 {
828 struct inpcb *inp;
829 struct udpiphdr *ui;
830 int len = m->m_pkthdr.len;
831 int error = 0;
832 va_list ap;
833
834 MCLAIM(m, &udp_tx_mowner);
835 va_start(ap, m);
836 inp = va_arg(ap, struct inpcb *);
837 va_end(ap);
838
839 /*
840 * Calculate data length and get a mbuf
841 * for UDP and IP headers.
842 */
843 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
844 if (m == 0) {
845 error = ENOBUFS;
846 goto release;
847 }
848
849 /*
850 * Compute the packet length of the IP header, and
851 * punt if the length looks bogus.
852 */
853 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
854 error = EMSGSIZE;
855 goto release;
856 }
857
858 /*
859 * Fill in mbuf with extended UDP header
860 * and addresses and length put into network format.
861 */
862 ui = mtod(m, struct udpiphdr *);
863 ui->ui_pr = IPPROTO_UDP;
864 ui->ui_src = inp->inp_laddr;
865 ui->ui_dst = inp->inp_faddr;
866 ui->ui_sport = inp->inp_lport;
867 ui->ui_dport = inp->inp_fport;
868 ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
869
870 /*
871 * Set up checksum and output datagram.
872 */
873 if (udpcksum) {
874 /*
875 * XXX Cache pseudo-header checksum part for
876 * XXX "connected" UDP sockets.
877 */
878 ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
879 ui->ui_dst.s_addr, htons((u_int16_t)len +
880 sizeof(struct udphdr) + IPPROTO_UDP));
881 m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
882 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
883 } else
884 ui->ui_sum = 0;
885 ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
886 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
887 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
888 udpstat.udps_opackets++;
889
890 return (ip_output(m, inp->inp_options, &inp->inp_route,
891 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
892 inp->inp_moptions, inp->inp_socket));
893
894 release:
895 m_freem(m);
896 return (error);
897 }
898
899 int udp_sendspace = 9216; /* really max datagram size */
900 int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
901 /* 40 1K datagrams */
902
903 /*ARGSUSED*/
904 int
905 udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
906 struct mbuf *control, struct proc *p)
907 {
908 struct inpcb *inp;
909 int s;
910 int error = 0;
911
912 if (req == PRU_CONTROL)
913 return (in_control(so, (long)m, (caddr_t)nam,
914 (struct ifnet *)control, p));
915
916 if (req == PRU_PURGEIF) {
917 in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
918 in_purgeif((struct ifnet *)control);
919 in_pcbpurgeif(&udbtable, (struct ifnet *)control);
920 return (0);
921 }
922
923 s = splsoftnet();
924 inp = sotoinpcb(so);
925 #ifdef DIAGNOSTIC
926 if (req != PRU_SEND && req != PRU_SENDOOB && control)
927 panic("udp_usrreq: unexpected control mbuf");
928 #endif
929 if (inp == 0 && req != PRU_ATTACH) {
930 error = EINVAL;
931 goto release;
932 }
933
934 /*
935 * Note: need to block udp_input while changing
936 * the udp pcb queue and/or pcb addresses.
937 */
938 switch (req) {
939
940 case PRU_ATTACH:
941 if (inp != 0) {
942 error = EISCONN;
943 break;
944 }
945 #ifdef MBUFTRACE
946 so->so_mowner = &udp_mowner;
947 so->so_rcv.sb_mowner = &udp_rx_mowner;
948 so->so_snd.sb_mowner = &udp_tx_mowner;
949 #endif
950 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
951 error = soreserve(so, udp_sendspace, udp_recvspace);
952 if (error)
953 break;
954 }
955 error = in_pcballoc(so, &udbtable);
956 if (error)
957 break;
958 inp = sotoinpcb(so);
959 inp->inp_ip.ip_ttl = ip_defttl;
960 break;
961
962 case PRU_DETACH:
963 in_pcbdetach(inp);
964 break;
965
966 case PRU_BIND:
967 error = in_pcbbind(inp, nam, p);
968 break;
969
970 case PRU_LISTEN:
971 error = EOPNOTSUPP;
972 break;
973
974 case PRU_CONNECT:
975 error = in_pcbconnect(inp, nam);
976 if (error)
977 break;
978 soisconnected(so);
979 break;
980
981 case PRU_CONNECT2:
982 error = EOPNOTSUPP;
983 break;
984
985 case PRU_DISCONNECT:
986 /*soisdisconnected(so);*/
987 so->so_state &= ~SS_ISCONNECTED; /* XXX */
988 in_pcbdisconnect(inp);
989 inp->inp_laddr = zeroin_addr; /* XXX */
990 in_pcbstate(inp, INP_BOUND); /* XXX */
991 break;
992
993 case PRU_SHUTDOWN:
994 socantsendmore(so);
995 break;
996
997 case PRU_RCVD:
998 error = EOPNOTSUPP;
999 break;
1000
1001 case PRU_SEND:
1002 if (control && control->m_len) {
1003 m_freem(control);
1004 m_freem(m);
1005 error = EINVAL;
1006 break;
1007 }
1008 {
1009 struct in_addr laddr; /* XXX */
1010
1011 if (nam) {
1012 laddr = inp->inp_laddr; /* XXX */
1013 if ((so->so_state & SS_ISCONNECTED) != 0) {
1014 error = EISCONN;
1015 goto die;
1016 }
1017 error = in_pcbconnect(inp, nam);
1018 if (error)
1019 goto die;
1020 } else {
1021 if ((so->so_state & SS_ISCONNECTED) == 0) {
1022 error = ENOTCONN;
1023 goto die;
1024 }
1025 }
1026 error = udp_output(m, inp);
1027 m = NULL;
1028 if (nam) {
1029 in_pcbdisconnect(inp);
1030 inp->inp_laddr = laddr; /* XXX */
1031 in_pcbstate(inp, INP_BOUND); /* XXX */
1032 }
1033 die:
1034 if (m)
1035 m_freem(m);
1036 }
1037 break;
1038
1039 case PRU_SENSE:
1040 /*
1041 * stat: don't bother with a blocksize.
1042 */
1043 splx(s);
1044 return (0);
1045
1046 case PRU_RCVOOB:
1047 error = EOPNOTSUPP;
1048 break;
1049
1050 case PRU_SENDOOB:
1051 m_freem(control);
1052 m_freem(m);
1053 error = EOPNOTSUPP;
1054 break;
1055
1056 case PRU_SOCKADDR:
1057 in_setsockaddr(inp, nam);
1058 break;
1059
1060 case PRU_PEERADDR:
1061 in_setpeeraddr(inp, nam);
1062 break;
1063
1064 default:
1065 panic("udp_usrreq");
1066 }
1067
1068 release:
1069 splx(s);
1070 return (error);
1071 }
1072
1073 /*
1074 * Sysctl for udp variables.
1075 */
1076 SYSCTL_SETUP(sysctl_net_inet_udp_setup, "sysctl net.inet.udp subtree setup")
1077 {
1078
1079 sysctl_createv(clog, 0, NULL, NULL,
1080 CTLFLAG_PERMANENT,
1081 CTLTYPE_NODE, "net", NULL,
1082 NULL, 0, NULL, 0,
1083 CTL_NET, CTL_EOL);
1084 sysctl_createv(clog, 0, NULL, NULL,
1085 CTLFLAG_PERMANENT,
1086 CTLTYPE_NODE, "inet", NULL,
1087 NULL, 0, NULL, 0,
1088 CTL_NET, PF_INET, CTL_EOL);
1089 sysctl_createv(clog, 0, NULL, NULL,
1090 CTLFLAG_PERMANENT,
1091 CTLTYPE_NODE, "udp", NULL,
1092 NULL, 0, NULL, 0,
1093 CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
1094
1095 sysctl_createv(clog, 0, NULL, NULL,
1096 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1097 CTLTYPE_INT, "checksum", NULL,
1098 NULL, 0, &udpcksum, 0,
1099 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
1100 CTL_EOL);
1101 sysctl_createv(clog, 0, NULL, NULL,
1102 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1103 CTLTYPE_INT, "sendspace", NULL,
1104 NULL, 0, &udp_sendspace, 0,
1105 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
1106 CTL_EOL);
1107 sysctl_createv(clog, 0, NULL, NULL,
1108 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1109 CTLTYPE_INT, "recvspace", NULL,
1110 NULL, 0, &udp_recvspace, 0,
1111 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
1112 CTL_EOL);
1113 }
1114 #endif
1115