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