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