udp_usrreq.c revision 1.118 1 /* $NetBSD: udp_usrreq.c,v 1.118 2004/03/31 07:57:06 itojun 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.118 2004/03/31 07:57:06 itojun 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 goto bad;
436 }
437 if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
438 udp6stat.udp6s_badsum++;
439 goto bad;
440 }
441
442 /*
443 * Construct source and dst sockaddrs.
444 * Note that ifindex (s6_addr16[1]) is already filled.
445 */
446 bzero(&src, sizeof(src));
447 src.sin6_family = AF_INET6;
448 src.sin6_len = sizeof(struct sockaddr_in6);
449 /* KAME hack: recover scopeid */
450 (void)in6_recoverscope(&src, &ip6->ip6_src, m->m_pkthdr.rcvif);
451 src.sin6_port = uh->uh_sport;
452 bzero(&dst, sizeof(dst));
453 dst.sin6_family = AF_INET6;
454 dst.sin6_len = sizeof(struct sockaddr_in6);
455 /* KAME hack: recover scopeid */
456 (void)in6_recoverscope(&dst, &ip6->ip6_dst, m->m_pkthdr.rcvif);
457 dst.sin6_port = uh->uh_dport;
458
459 if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
460 if (m->m_flags & M_MCAST) {
461 udp6stat.udp6s_noportmcast++;
462 goto bad;
463 }
464 udp6stat.udp6s_noport++;
465 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
466 m = NULL;
467 }
468
469 bad:
470 if (m)
471 m_freem(m);
472 return IPPROTO_DONE;
473 }
474 #endif
475
476 #ifdef INET
477 static void
478 udp4_sendup(m, off, src, so)
479 struct mbuf *m;
480 int off; /* offset of data portion */
481 struct sockaddr *src;
482 struct socket *so;
483 {
484 struct mbuf *opts = NULL;
485 struct mbuf *n;
486 struct inpcb *inp = NULL;
487
488 if (!so)
489 return;
490 switch (so->so_proto->pr_domain->dom_family) {
491 case AF_INET:
492 inp = sotoinpcb(so);
493 break;
494 #ifdef INET6
495 case AF_INET6:
496 break;
497 #endif
498 default:
499 return;
500 }
501
502 #if defined(IPSEC) || defined(FAST_IPSEC)
503 /* check AH/ESP integrity. */
504 if (so != NULL && ipsec4_in_reject_so(m, so)) {
505 ipsecstat.in_polvio++;
506 if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
507 icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
508 0, 0);
509 return;
510 }
511 #endif /*IPSEC*/
512
513 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
514 if (inp && (inp->inp_flags & INP_CONTROLOPTS
515 || so->so_options & SO_TIMESTAMP)) {
516 struct ip *ip = mtod(n, struct ip *);
517 ip_savecontrol(inp, &opts, ip, n);
518 }
519
520 m_adj(n, off);
521 if (sbappendaddr(&so->so_rcv, src, n,
522 opts) == 0) {
523 m_freem(n);
524 if (opts)
525 m_freem(opts);
526 udpstat.udps_fullsock++;
527 } else
528 sorwakeup(so);
529 }
530 }
531 #endif
532
533 #ifdef INET6
534 static void
535 udp6_sendup(m, off, src, so)
536 struct mbuf *m;
537 int off; /* offset of data portion */
538 struct sockaddr *src;
539 struct socket *so;
540 {
541 struct mbuf *opts = NULL;
542 struct mbuf *n;
543 struct in6pcb *in6p = NULL;
544
545 if (!so)
546 return;
547 if (so->so_proto->pr_domain->dom_family != AF_INET6)
548 return;
549 in6p = sotoin6pcb(so);
550
551 #if defined(IPSEC) || defined(FAST_IPSEC)
552 /* check AH/ESP integrity. */
553 if (so != NULL && ipsec6_in_reject_so(m, so)) {
554 ipsec6stat.in_polvio++;
555 if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
556 icmp6_error(n, ICMP6_DST_UNREACH,
557 ICMP6_DST_UNREACH_ADMIN, 0);
558 return;
559 }
560 #endif /*IPSEC*/
561
562 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
563 if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
564 || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
565 struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
566 ip6_savecontrol(in6p, &opts, ip6, n);
567 }
568
569 m_adj(n, off);
570 if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
571 m_freem(n);
572 if (opts)
573 m_freem(opts);
574 udp6stat.udp6s_fullsock++;
575 } else
576 sorwakeup(so);
577 }
578 }
579 #endif
580
581 #ifdef INET
582 static int
583 udp4_realinput(src, dst, m, off)
584 struct sockaddr_in *src;
585 struct sockaddr_in *dst;
586 struct mbuf *m;
587 int off; /* offset of udphdr */
588 {
589 u_int16_t *sport, *dport;
590 int rcvcnt;
591 struct in_addr *src4, *dst4;
592 struct inpcb_hdr *inph;
593 struct inpcb *inp;
594
595 rcvcnt = 0;
596 off += sizeof(struct udphdr); /* now, offset of payload */
597
598 if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
599 goto bad;
600
601 src4 = &src->sin_addr;
602 sport = &src->sin_port;
603 dst4 = &dst->sin_addr;
604 dport = &dst->sin_port;
605
606 if (IN_MULTICAST(dst4->s_addr) ||
607 in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
608 /*
609 * Deliver a multicast or broadcast datagram to *all* sockets
610 * for which the local and remote addresses and ports match
611 * those of the incoming datagram. This allows more than
612 * one process to receive multi/broadcasts on the same port.
613 * (This really ought to be done for unicast datagrams as
614 * well, but that would cause problems with existing
615 * applications that open both address-specific sockets and
616 * a wildcard socket listening to the same port -- they would
617 * end up receiving duplicates of every unicast datagram.
618 * Those applications open the multiple sockets to overcome an
619 * inadequacy of the UDP socket interface, but for backwards
620 * compatibility we avoid the problem here rather than
621 * fixing the interface. Maybe 4.5BSD will remedy this?)
622 */
623
624 /*
625 * KAME note: traditionally we dropped udpiphdr from mbuf here.
626 * we need udpiphdr for IPsec processing so we do that later.
627 */
628 /*
629 * Locate pcb(s) for datagram.
630 */
631 CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
632 inp = (struct inpcb *)inph;
633 if (inp->inp_af != AF_INET)
634 continue;
635
636 if (inp->inp_lport != *dport)
637 continue;
638 if (!in_nullhost(inp->inp_laddr)) {
639 if (!in_hosteq(inp->inp_laddr, *dst4))
640 continue;
641 }
642 if (!in_nullhost(inp->inp_faddr)) {
643 if (!in_hosteq(inp->inp_faddr, *src4) ||
644 inp->inp_fport != *sport)
645 continue;
646 }
647
648 udp4_sendup(m, off, (struct sockaddr *)src,
649 inp->inp_socket);
650 rcvcnt++;
651
652 /*
653 * Don't look for additional matches if this one does
654 * not have either the SO_REUSEPORT or SO_REUSEADDR
655 * socket options set. This heuristic avoids searching
656 * through all pcbs in the common case of a non-shared
657 * port. It assumes that an application will never
658 * clear these options after setting them.
659 */
660 if ((inp->inp_socket->so_options &
661 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
662 break;
663 }
664 } else {
665 /*
666 * Locate pcb for datagram.
667 */
668 inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
669 if (inp == 0) {
670 ++udpstat.udps_pcbhashmiss;
671 inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
672 if (inp == 0)
673 return rcvcnt;
674 }
675
676 udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
677 rcvcnt++;
678 }
679
680 bad:
681 return rcvcnt;
682 }
683 #endif
684
685 #ifdef INET6
686 static int
687 udp6_realinput(af, src, dst, m, off)
688 int af; /* af on packet */
689 struct sockaddr_in6 *src;
690 struct sockaddr_in6 *dst;
691 struct mbuf *m;
692 int off; /* offset of udphdr */
693 {
694 u_int16_t sport, dport;
695 int rcvcnt;
696 struct in6_addr src6, dst6;
697 const struct in_addr *dst4;
698 struct inpcb_hdr *inph;
699 struct in6pcb *in6p;
700
701 rcvcnt = 0;
702 off += sizeof(struct udphdr); /* now, offset of payload */
703
704 if (af != AF_INET && af != AF_INET6)
705 goto bad;
706 if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
707 goto bad;
708
709 in6_embedscope(&src6, src, NULL, NULL);
710 sport = src->sin6_port;
711 in6_embedscope(&dst6, dst, NULL, NULL);
712 dport = dst->sin6_port;
713 dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
714
715 if (IN6_IS_ADDR_MULTICAST(&dst6) ||
716 (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
717 /*
718 * Deliver a multicast or broadcast datagram to *all* sockets
719 * for which the local and remote addresses and ports match
720 * those of the incoming datagram. This allows more than
721 * one process to receive multi/broadcasts on the same port.
722 * (This really ought to be done for unicast datagrams as
723 * well, but that would cause problems with existing
724 * applications that open both address-specific sockets and
725 * a wildcard socket listening to the same port -- they would
726 * end up receiving duplicates of every unicast datagram.
727 * Those applications open the multiple sockets to overcome an
728 * inadequacy of the UDP socket interface, but for backwards
729 * compatibility we avoid the problem here rather than
730 * fixing the interface. Maybe 4.5BSD will remedy this?)
731 */
732
733 /*
734 * KAME note: traditionally we dropped udpiphdr from mbuf here.
735 * we need udpiphdr for IPsec processing so we do that later.
736 */
737 /*
738 * Locate pcb(s) for datagram.
739 */
740 CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
741 in6p = (struct in6pcb *)inph;
742 if (in6p->in6p_af != AF_INET6)
743 continue;
744
745 if (in6p->in6p_lport != dport)
746 continue;
747 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
748 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6))
749 continue;
750 } else {
751 if (IN6_IS_ADDR_V4MAPPED(&dst6) &&
752 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
753 continue;
754 }
755 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
756 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
757 &src6) || in6p->in6p_fport != sport)
758 continue;
759 } else {
760 if (IN6_IS_ADDR_V4MAPPED(&src6) &&
761 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
762 continue;
763 }
764
765 udp6_sendup(m, off, (struct sockaddr *)src,
766 in6p->in6p_socket);
767 rcvcnt++;
768
769 /*
770 * Don't look for additional matches if this one does
771 * not have either the SO_REUSEPORT or SO_REUSEADDR
772 * socket options set. This heuristic avoids searching
773 * through all pcbs in the common case of a non-shared
774 * port. It assumes that an application will never
775 * clear these options after setting them.
776 */
777 if ((in6p->in6p_socket->so_options &
778 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
779 break;
780 }
781 } else {
782 /*
783 * Locate pcb for datagram.
784 */
785 in6p = in6_pcblookup_connect(&udbtable, &src6, sport,
786 &dst6, dport, 0);
787 if (in6p == 0) {
788 ++udpstat.udps_pcbhashmiss;
789 in6p = in6_pcblookup_bind(&udbtable, &dst6, dport, 0);
790 if (in6p == 0)
791 return rcvcnt;
792 }
793
794 udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
795 rcvcnt++;
796 }
797
798 bad:
799 return rcvcnt;
800 }
801 #endif
802
803 #ifdef INET
804 /*
805 * Notify a udp user of an asynchronous error;
806 * just wake up so that he can collect error status.
807 */
808 static void
809 udp_notify(inp, errno)
810 struct inpcb *inp;
811 int errno;
812 {
813
814 inp->inp_socket->so_error = errno;
815 sorwakeup(inp->inp_socket);
816 sowwakeup(inp->inp_socket);
817 }
818
819 void *
820 udp_ctlinput(cmd, sa, v)
821 int cmd;
822 struct sockaddr *sa;
823 void *v;
824 {
825 struct ip *ip = v;
826 struct udphdr *uh;
827 void (*notify) __P((struct inpcb *, int)) = udp_notify;
828 int errno;
829
830 if (sa->sa_family != AF_INET
831 || sa->sa_len != sizeof(struct sockaddr_in))
832 return NULL;
833 if ((unsigned)cmd >= PRC_NCMDS)
834 return NULL;
835 errno = inetctlerrmap[cmd];
836 if (PRC_IS_REDIRECT(cmd))
837 notify = in_rtchange, ip = 0;
838 else if (cmd == PRC_HOSTDEAD)
839 ip = 0;
840 else if (errno == 0)
841 return NULL;
842 if (ip) {
843 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
844 in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
845 ip->ip_src, uh->uh_sport, errno, notify);
846
847 /* XXX mapped address case */
848 } else
849 in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
850 notify);
851 return NULL;
852 }
853
854 int
855 #if __STDC__
856 udp_output(struct mbuf *m, ...)
857 #else
858 udp_output(m, va_alist)
859 struct mbuf *m;
860 va_dcl
861 #endif
862 {
863 struct inpcb *inp;
864 struct udpiphdr *ui;
865 int len = m->m_pkthdr.len;
866 int error = 0;
867 va_list ap;
868
869 MCLAIM(m, &udp_tx_mowner);
870 va_start(ap, m);
871 inp = va_arg(ap, struct inpcb *);
872 va_end(ap);
873
874 /*
875 * Calculate data length and get a mbuf
876 * for UDP and IP headers.
877 */
878 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
879 if (m == 0) {
880 error = ENOBUFS;
881 goto release;
882 }
883
884 /*
885 * Compute the packet length of the IP header, and
886 * punt if the length looks bogus.
887 */
888 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
889 error = EMSGSIZE;
890 goto release;
891 }
892
893 /*
894 * Fill in mbuf with extended UDP header
895 * and addresses and length put into network format.
896 */
897 ui = mtod(m, struct udpiphdr *);
898 ui->ui_pr = IPPROTO_UDP;
899 ui->ui_src = inp->inp_laddr;
900 ui->ui_dst = inp->inp_faddr;
901 ui->ui_sport = inp->inp_lport;
902 ui->ui_dport = inp->inp_fport;
903 ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
904
905 /*
906 * Set up checksum and output datagram.
907 */
908 if (udpcksum) {
909 /*
910 * XXX Cache pseudo-header checksum part for
911 * XXX "connected" UDP sockets.
912 */
913 ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
914 ui->ui_dst.s_addr, htons((u_int16_t)len +
915 sizeof(struct udphdr) + IPPROTO_UDP));
916 m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
917 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
918 } else
919 ui->ui_sum = 0;
920 ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
921 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
922 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
923 udpstat.udps_opackets++;
924
925 return (ip_output(m, inp->inp_options, &inp->inp_route,
926 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
927 inp->inp_moptions, inp->inp_socket));
928
929 release:
930 m_freem(m);
931 return (error);
932 }
933
934 int udp_sendspace = 9216; /* really max datagram size */
935 int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
936 /* 40 1K datagrams */
937
938 /*ARGSUSED*/
939 int
940 udp_usrreq(so, req, m, nam, control, p)
941 struct socket *so;
942 int req;
943 struct mbuf *m, *nam, *control;
944 struct proc *p;
945 {
946 struct inpcb *inp;
947 int s;
948 int error = 0;
949
950 if (req == PRU_CONTROL)
951 return (in_control(so, (long)m, (caddr_t)nam,
952 (struct ifnet *)control, p));
953
954 if (req == PRU_PURGEIF) {
955 in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
956 in_purgeif((struct ifnet *)control);
957 in_pcbpurgeif(&udbtable, (struct ifnet *)control);
958 return (0);
959 }
960
961 s = splsoftnet();
962 inp = sotoinpcb(so);
963 #ifdef DIAGNOSTIC
964 if (req != PRU_SEND && req != PRU_SENDOOB && control)
965 panic("udp_usrreq: unexpected control mbuf");
966 #endif
967 if (inp == 0 && req != PRU_ATTACH) {
968 error = EINVAL;
969 goto release;
970 }
971
972 /*
973 * Note: need to block udp_input while changing
974 * the udp pcb queue and/or pcb addresses.
975 */
976 switch (req) {
977
978 case PRU_ATTACH:
979 if (inp != 0) {
980 error = EISCONN;
981 break;
982 }
983 #ifdef MBUFTRACE
984 so->so_mowner = &udp_mowner;
985 so->so_rcv.sb_mowner = &udp_rx_mowner;
986 so->so_snd.sb_mowner = &udp_tx_mowner;
987 #endif
988 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
989 error = soreserve(so, udp_sendspace, udp_recvspace);
990 if (error)
991 break;
992 }
993 error = in_pcballoc(so, &udbtable);
994 if (error)
995 break;
996 inp = sotoinpcb(so);
997 inp->inp_ip.ip_ttl = ip_defttl;
998 break;
999
1000 case PRU_DETACH:
1001 in_pcbdetach(inp);
1002 break;
1003
1004 case PRU_BIND:
1005 error = in_pcbbind(inp, nam, p);
1006 break;
1007
1008 case PRU_LISTEN:
1009 error = EOPNOTSUPP;
1010 break;
1011
1012 case PRU_CONNECT:
1013 error = in_pcbconnect(inp, nam);
1014 if (error)
1015 break;
1016 soisconnected(so);
1017 break;
1018
1019 case PRU_CONNECT2:
1020 error = EOPNOTSUPP;
1021 break;
1022
1023 case PRU_DISCONNECT:
1024 /*soisdisconnected(so);*/
1025 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1026 in_pcbdisconnect(inp);
1027 inp->inp_laddr = zeroin_addr; /* XXX */
1028 in_pcbstate(inp, INP_BOUND); /* XXX */
1029 break;
1030
1031 case PRU_SHUTDOWN:
1032 socantsendmore(so);
1033 break;
1034
1035 case PRU_RCVD:
1036 error = EOPNOTSUPP;
1037 break;
1038
1039 case PRU_SEND:
1040 if (control && control->m_len) {
1041 m_freem(control);
1042 m_freem(m);
1043 error = EINVAL;
1044 break;
1045 }
1046 {
1047 struct in_addr laddr; /* XXX */
1048
1049 if (nam) {
1050 laddr = inp->inp_laddr; /* XXX */
1051 if ((so->so_state & SS_ISCONNECTED) != 0) {
1052 error = EISCONN;
1053 goto die;
1054 }
1055 error = in_pcbconnect(inp, nam);
1056 if (error)
1057 goto die;
1058 } else {
1059 if ((so->so_state & SS_ISCONNECTED) == 0) {
1060 error = ENOTCONN;
1061 goto die;
1062 }
1063 }
1064 error = udp_output(m, inp);
1065 m = NULL;
1066 if (nam) {
1067 in_pcbdisconnect(inp);
1068 inp->inp_laddr = laddr; /* XXX */
1069 in_pcbstate(inp, INP_BOUND); /* XXX */
1070 }
1071 die:
1072 if (m)
1073 m_freem(m);
1074 }
1075 break;
1076
1077 case PRU_SENSE:
1078 /*
1079 * stat: don't bother with a blocksize.
1080 */
1081 splx(s);
1082 return (0);
1083
1084 case PRU_RCVOOB:
1085 error = EOPNOTSUPP;
1086 break;
1087
1088 case PRU_SENDOOB:
1089 m_freem(control);
1090 m_freem(m);
1091 error = EOPNOTSUPP;
1092 break;
1093
1094 case PRU_SOCKADDR:
1095 in_setsockaddr(inp, nam);
1096 break;
1097
1098 case PRU_PEERADDR:
1099 in_setpeeraddr(inp, nam);
1100 break;
1101
1102 default:
1103 panic("udp_usrreq");
1104 }
1105
1106 release:
1107 splx(s);
1108 return (error);
1109 }
1110
1111 /*
1112 * Sysctl for udp variables.
1113 */
1114 SYSCTL_SETUP(sysctl_net_inet_udp_setup, "sysctl net.inet.udp subtree setup")
1115 {
1116
1117 sysctl_createv(clog, 0, NULL, NULL,
1118 CTLFLAG_PERMANENT,
1119 CTLTYPE_NODE, "net", NULL,
1120 NULL, 0, NULL, 0,
1121 CTL_NET, CTL_EOL);
1122 sysctl_createv(clog, 0, NULL, NULL,
1123 CTLFLAG_PERMANENT,
1124 CTLTYPE_NODE, "inet", NULL,
1125 NULL, 0, NULL, 0,
1126 CTL_NET, PF_INET, CTL_EOL);
1127 sysctl_createv(clog, 0, NULL, NULL,
1128 CTLFLAG_PERMANENT,
1129 CTLTYPE_NODE, "udp", NULL,
1130 NULL, 0, NULL, 0,
1131 CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
1132
1133 sysctl_createv(clog, 0, NULL, NULL,
1134 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1135 CTLTYPE_INT, "checksum", NULL,
1136 NULL, 0, &udpcksum, 0,
1137 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
1138 CTL_EOL);
1139 sysctl_createv(clog, 0, NULL, NULL,
1140 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1141 CTLTYPE_INT, "sendspace", NULL,
1142 NULL, 0, &udp_sendspace, 0,
1143 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
1144 CTL_EOL);
1145 sysctl_createv(clog, 0, NULL, NULL,
1146 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1147 CTLTYPE_INT, "recvspace", NULL,
1148 NULL, 0, &udp_recvspace, 0,
1149 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
1150 CTL_EOL);
1151 }
1152 #endif
1153