udp_usrreq.c revision 1.178 1 /* $NetBSD: udp_usrreq.c,v 1.178 2009/07/19 23:17:33 minskim 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.178 2009/07/19 23:17:33 minskim Exp $");
65
66 #include "opt_inet.h"
67 #include "opt_compat_netbsd.h"
68 #include "opt_ipsec.h"
69 #include "opt_inet_csum.h"
70 #include "opt_ipkdb.h"
71 #include "opt_mbuftrace.h"
72
73 #include <sys/param.h>
74 #include <sys/malloc.h>
75 #include <sys/mbuf.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/errno.h>
80 #include <sys/stat.h>
81 #include <sys/systm.h>
82 #include <sys/proc.h>
83 #include <sys/domain.h>
84 #include <sys/sysctl.h>
85
86 #include <net/if.h>
87 #include <net/route.h>
88
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/ip_var.h>
95 #include <netinet/ip_icmp.h>
96 #include <netinet/udp.h>
97 #include <netinet/udp_var.h>
98 #include <netinet/udp_private.h>
99
100 #ifdef INET6
101 #include <netinet/ip6.h>
102 #include <netinet/icmp6.h>
103 #include <netinet6/ip6_var.h>
104 #include <netinet6/ip6_private.h>
105 #include <netinet6/in6_pcb.h>
106 #include <netinet6/udp6_var.h>
107 #include <netinet6/udp6_private.h>
108 #include <netinet6/scope6_var.h>
109 #endif
110
111 #ifndef INET6
112 /* always need ip6.h for IP6_EXTHDR_GET */
113 #include <netinet/ip6.h>
114 #endif
115
116 #include "faith.h"
117 #if defined(NFAITH) && NFAITH > 0
118 #include <net/if_faith.h>
119 #endif
120
121 #include <machine/stdarg.h>
122
123 #ifdef FAST_IPSEC
124 #include <netipsec/ipsec.h>
125 #include <netipsec/ipsec_var.h>
126 #include <netipsec/ipsec_private.h>
127 #include <netipsec/esp.h>
128 #ifdef INET6
129 #include <netipsec/ipsec6.h>
130 #endif
131 #endif /* FAST_IPSEC */
132
133 #ifdef IPSEC
134 #include <netinet6/ipsec.h>
135 #include <netinet6/ipsec_private.h>
136 #include <netinet6/esp.h>
137 #include <netkey/key.h>
138 #endif /* IPSEC */
139
140 #ifdef COMPAT_50
141 #include <compat/sys/socket.h>
142 #endif
143
144 #ifdef IPKDB
145 #include <ipkdb/ipkdb.h>
146 #endif
147
148 /*
149 * UDP protocol implementation.
150 * Per RFC 768, August, 1980.
151 */
152 int udpcksum = 1;
153 int udp_do_loopback_cksum = 0;
154
155 struct inpcbtable udbtable;
156
157 percpu_t *udpstat_percpu;
158
159 #ifdef INET
160 #ifdef IPSEC_NAT_T
161 static int udp4_espinudp (struct mbuf **, int, struct sockaddr *,
162 struct socket *);
163 #endif
164 static void udp4_sendup (struct mbuf *, int, struct sockaddr *,
165 struct socket *);
166 static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
167 struct mbuf **, int);
168 static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
169 #endif
170 #ifdef INET6
171 static void udp6_sendup (struct mbuf *, int, struct sockaddr *,
172 struct socket *);
173 static int udp6_realinput (int, struct sockaddr_in6 *,
174 struct sockaddr_in6 *, struct mbuf *, int);
175 static int udp6_input_checksum(struct mbuf *, const struct udphdr *, int, int);
176 #endif
177 #ifdef INET
178 static void udp_notify (struct inpcb *, int);
179 #endif
180
181 #ifndef UDBHASHSIZE
182 #define UDBHASHSIZE 128
183 #endif
184 int udbhashsize = UDBHASHSIZE;
185
186 #ifdef MBUFTRACE
187 struct mowner udp_mowner = MOWNER_INIT("udp", "");
188 struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx");
189 struct mowner udp_tx_mowner = MOWNER_INIT("udp", "tx");
190 #endif
191
192 #ifdef UDP_CSUM_COUNTERS
193 #include <sys/device.h>
194
195 #if defined(INET)
196 struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
197 NULL, "udp", "hwcsum bad");
198 struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
199 NULL, "udp", "hwcsum ok");
200 struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
201 NULL, "udp", "hwcsum data");
202 struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
203 NULL, "udp", "swcsum");
204
205 EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
206 EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
207 EVCNT_ATTACH_STATIC(udp_hwcsum_data);
208 EVCNT_ATTACH_STATIC(udp_swcsum);
209 #endif /* defined(INET) */
210
211 #if defined(INET6)
212 struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
213 NULL, "udp6", "hwcsum bad");
214 struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
215 NULL, "udp6", "hwcsum ok");
216 struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
217 NULL, "udp6", "hwcsum data");
218 struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
219 NULL, "udp6", "swcsum");
220
221 EVCNT_ATTACH_STATIC(udp6_hwcsum_bad);
222 EVCNT_ATTACH_STATIC(udp6_hwcsum_ok);
223 EVCNT_ATTACH_STATIC(udp6_hwcsum_data);
224 EVCNT_ATTACH_STATIC(udp6_swcsum);
225 #endif /* defined(INET6) */
226
227 #define UDP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
228
229 #else
230
231 #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */
232
233 #endif /* UDP_CSUM_COUNTERS */
234
235 void
236 udp_init(void)
237 {
238
239 in_pcbinit(&udbtable, udbhashsize, udbhashsize);
240
241 MOWNER_ATTACH(&udp_tx_mowner);
242 MOWNER_ATTACH(&udp_rx_mowner);
243 MOWNER_ATTACH(&udp_mowner);
244
245 #ifdef INET
246 udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
247 #endif
248 #ifdef INET6
249 udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
250 #endif
251 }
252
253 /*
254 * Checksum extended UDP header and data.
255 */
256
257 int
258 udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
259 int iphlen, int len)
260 {
261
262 switch (af) {
263 #ifdef INET
264 case AF_INET:
265 return udp4_input_checksum(m, uh, iphlen, len);
266 #endif
267 #ifdef INET6
268 case AF_INET6:
269 return udp6_input_checksum(m, uh, iphlen, len);
270 #endif
271 }
272 #ifdef DIAGNOSTIC
273 panic("udp_input_checksum: unknown af %d", af);
274 #endif
275 /* NOTREACHED */
276 return -1;
277 }
278
279 #ifdef INET
280
281 /*
282 * Checksum extended UDP header and data.
283 */
284
285 static int
286 udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
287 int iphlen, int len)
288 {
289
290 /*
291 * XXX it's better to record and check if this mbuf is
292 * already checked.
293 */
294
295 if (uh->uh_sum == 0)
296 return 0;
297
298 switch (m->m_pkthdr.csum_flags &
299 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
300 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
301 case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
302 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
303 goto badcsum;
304
305 case M_CSUM_UDPv4|M_CSUM_DATA: {
306 u_int32_t hw_csum = m->m_pkthdr.csum_data;
307
308 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
309 if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
310 const struct ip *ip =
311 mtod(m, const struct ip *);
312
313 hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
314 ip->ip_dst.s_addr,
315 htons(hw_csum + len + IPPROTO_UDP));
316 }
317 if ((hw_csum ^ 0xffff) != 0)
318 goto badcsum;
319 break;
320 }
321
322 case M_CSUM_UDPv4:
323 /* Checksum was okay. */
324 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
325 break;
326
327 default:
328 /*
329 * Need to compute it ourselves. Maybe skip checksum
330 * on loopback interfaces.
331 */
332 if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
333 IFF_LOOPBACK) ||
334 udp_do_loopback_cksum)) {
335 UDP_CSUM_COUNTER_INCR(&udp_swcsum);
336 if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
337 goto badcsum;
338 }
339 break;
340 }
341
342 return 0;
343
344 badcsum:
345 UDP_STATINC(UDP_STAT_BADSUM);
346 return -1;
347 }
348
349 void
350 udp_input(struct mbuf *m, ...)
351 {
352 va_list ap;
353 struct sockaddr_in src, dst;
354 struct ip *ip;
355 struct udphdr *uh;
356 int iphlen;
357 int len;
358 int n;
359 u_int16_t ip_len;
360
361 va_start(ap, m);
362 iphlen = va_arg(ap, int);
363 (void)va_arg(ap, int); /* ignore value, advance ap */
364 va_end(ap);
365
366 MCLAIM(m, &udp_rx_mowner);
367 UDP_STATINC(UDP_STAT_IPACKETS);
368
369 /*
370 * Get IP and UDP header together in first mbuf.
371 */
372 ip = mtod(m, struct ip *);
373 IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
374 if (uh == NULL) {
375 UDP_STATINC(UDP_STAT_HDROPS);
376 return;
377 }
378 KASSERT(UDP_HDR_ALIGNED_P(uh));
379
380 /* destination port of 0 is illegal, based on RFC768. */
381 if (uh->uh_dport == 0)
382 goto bad;
383
384 /*
385 * Make mbuf data length reflect UDP length.
386 * If not enough data to reflect UDP length, drop.
387 */
388 ip_len = ntohs(ip->ip_len);
389 len = ntohs((u_int16_t)uh->uh_ulen);
390 if (ip_len != iphlen + len) {
391 if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
392 UDP_STATINC(UDP_STAT_BADLEN);
393 goto bad;
394 }
395 m_adj(m, iphlen + len - ip_len);
396 }
397
398 /*
399 * Checksum extended UDP header and data.
400 */
401 if (udp4_input_checksum(m, uh, iphlen, len))
402 goto badcsum;
403
404 /* construct source and dst sockaddrs. */
405 sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
406 sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
407
408 if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
409 UDP_STATINC(UDP_STAT_HDROPS);
410 return;
411 }
412 #ifdef INET6
413 if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
414 struct sockaddr_in6 src6, dst6;
415
416 memset(&src6, 0, sizeof(src6));
417 src6.sin6_family = AF_INET6;
418 src6.sin6_len = sizeof(struct sockaddr_in6);
419 src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
420 memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src,
421 sizeof(ip->ip_src));
422 src6.sin6_port = uh->uh_sport;
423 memset(&dst6, 0, sizeof(dst6));
424 dst6.sin6_family = AF_INET6;
425 dst6.sin6_len = sizeof(struct sockaddr_in6);
426 dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
427 memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst,
428 sizeof(ip->ip_dst));
429 dst6.sin6_port = uh->uh_dport;
430
431 n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
432 }
433 #endif
434
435 if (n == 0) {
436 if (m->m_flags & (M_BCAST | M_MCAST)) {
437 UDP_STATINC(UDP_STAT_NOPORTBCAST);
438 goto bad;
439 }
440 UDP_STATINC(UDP_STAT_NOPORT);
441 #ifdef IPKDB
442 if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
443 m, iphlen + sizeof(struct udphdr),
444 m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
445 /*
446 * It was a debugger connect packet,
447 * just drop it now
448 */
449 goto bad;
450 }
451 #endif
452 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
453 m = NULL;
454 }
455
456 bad:
457 if (m)
458 m_freem(m);
459 return;
460
461 badcsum:
462 m_freem(m);
463 }
464 #endif
465
466 #ifdef INET6
467 static int
468 udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
469 {
470
471 /*
472 * XXX it's better to record and check if this mbuf is
473 * already checked.
474 */
475
476 if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
477 goto good;
478 }
479 if (uh->uh_sum == 0) {
480 UDP6_STATINC(UDP6_STAT_NOSUM);
481 goto bad;
482 }
483
484 switch (m->m_pkthdr.csum_flags &
485 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv6) |
486 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
487 case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD:
488 UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad);
489 UDP6_STATINC(UDP6_STAT_BADSUM);
490 goto bad;
491
492 #if 0 /* notyet */
493 case M_CSUM_UDPv6|M_CSUM_DATA:
494 #endif
495
496 case M_CSUM_UDPv6:
497 /* Checksum was okay. */
498 UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok);
499 break;
500
501 default:
502 /*
503 * Need to compute it ourselves. Maybe skip checksum
504 * on loopback interfaces.
505 */
506 UDP_CSUM_COUNTER_INCR(&udp6_swcsum);
507 if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
508 UDP6_STATINC(UDP6_STAT_BADSUM);
509 goto bad;
510 }
511 }
512
513 good:
514 return 0;
515 bad:
516 return -1;
517 }
518
519 int
520 udp6_input(struct mbuf **mp, int *offp, int proto)
521 {
522 struct mbuf *m = *mp;
523 int off = *offp;
524 struct sockaddr_in6 src, dst;
525 struct ip6_hdr *ip6;
526 struct udphdr *uh;
527 u_int32_t plen, ulen;
528
529 ip6 = mtod(m, struct ip6_hdr *);
530
531 #if defined(NFAITH) && 0 < NFAITH
532 if (faithprefix(&ip6->ip6_dst)) {
533 /* send icmp6 host unreach? */
534 m_freem(m);
535 return IPPROTO_DONE;
536 }
537 #endif
538
539 UDP6_STATINC(UDP6_STAT_IPACKETS);
540
541 /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */
542 plen = m->m_pkthdr.len - off;
543 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
544 if (uh == NULL) {
545 IP6_STATINC(IP6_STAT_TOOSHORT);
546 return IPPROTO_DONE;
547 }
548 KASSERT(UDP_HDR_ALIGNED_P(uh));
549 ulen = ntohs((u_short)uh->uh_ulen);
550 /*
551 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
552 * iff payload length > 0xffff.
553 */
554 if (ulen == 0 && plen > 0xffff)
555 ulen = plen;
556
557 if (plen != ulen) {
558 UDP6_STATINC(UDP6_STAT_BADLEN);
559 goto bad;
560 }
561
562 /* destination port of 0 is illegal, based on RFC768. */
563 if (uh->uh_dport == 0)
564 goto bad;
565
566 /* Be proactive about malicious use of IPv4 mapped address */
567 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
568 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
569 /* XXX stat */
570 goto bad;
571 }
572
573 /*
574 * Checksum extended UDP header and data. Maybe skip checksum
575 * on loopback interfaces.
576 */
577 if (udp6_input_checksum(m, uh, off, ulen))
578 goto bad;
579
580 /*
581 * Construct source and dst sockaddrs.
582 */
583 memset(&src, 0, sizeof(src));
584 src.sin6_family = AF_INET6;
585 src.sin6_len = sizeof(struct sockaddr_in6);
586 src.sin6_addr = ip6->ip6_src;
587 src.sin6_port = uh->uh_sport;
588 memset(&dst, 0, sizeof(dst));
589 dst.sin6_family = AF_INET6;
590 dst.sin6_len = sizeof(struct sockaddr_in6);
591 dst.sin6_addr = ip6->ip6_dst;
592 dst.sin6_port = uh->uh_dport;
593
594 if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
595 if (m->m_flags & M_MCAST) {
596 UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
597 goto bad;
598 }
599 UDP6_STATINC(UDP6_STAT_NOPORT);
600 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
601 m = NULL;
602 }
603
604 bad:
605 if (m)
606 m_freem(m);
607 return IPPROTO_DONE;
608 }
609 #endif
610
611 #ifdef INET
612 static void
613 udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
614 struct sockaddr *src, struct socket *so)
615 {
616 struct mbuf *opts = NULL;
617 struct mbuf *n;
618 struct inpcb *inp = NULL;
619
620 if (!so)
621 return;
622 switch (so->so_proto->pr_domain->dom_family) {
623 case AF_INET:
624 inp = sotoinpcb(so);
625 break;
626 #ifdef INET6
627 case AF_INET6:
628 break;
629 #endif
630 default:
631 return;
632 }
633
634 #if defined(IPSEC) || defined(FAST_IPSEC)
635 /* check AH/ESP integrity. */
636 if (so != NULL && ipsec4_in_reject_so(m, so)) {
637 IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
638 if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
639 icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
640 0, 0);
641 return;
642 }
643 #endif /*IPSEC*/
644
645 if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
646 if (inp && (inp->inp_flags & INP_CONTROLOPTS
647 #ifdef SO_OTIMESTAMP
648 || so->so_options & SO_OTIMESTAMP
649 #endif
650 || so->so_options & SO_TIMESTAMP)) {
651 struct ip *ip = mtod(n, struct ip *);
652 ip_savecontrol(inp, &opts, ip, n);
653 }
654
655 m_adj(n, off);
656 if (sbappendaddr(&so->so_rcv, src, n,
657 opts) == 0) {
658 m_freem(n);
659 if (opts)
660 m_freem(opts);
661 so->so_rcv.sb_overflowed++;
662 UDP_STATINC(UDP_STAT_FULLSOCK);
663 } else
664 sorwakeup(so);
665 }
666 }
667 #endif
668
669 #ifdef INET6
670 static void
671 udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
672 struct sockaddr *src, struct socket *so)
673 {
674 struct mbuf *opts = NULL;
675 struct mbuf *n;
676 struct in6pcb *in6p = NULL;
677
678 if (!so)
679 return;
680 if (so->so_proto->pr_domain->dom_family != AF_INET6)
681 return;
682 in6p = sotoin6pcb(so);
683
684 #if defined(IPSEC) || defined(FAST_IPSEC)
685 /* check AH/ESP integrity. */
686 if (so != NULL && ipsec6_in_reject_so(m, so)) {
687 IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
688 if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
689 icmp6_error(n, ICMP6_DST_UNREACH,
690 ICMP6_DST_UNREACH_ADMIN, 0);
691 return;
692 }
693 #endif /*IPSEC*/
694
695 if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
696 if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
697 #ifdef SO_OTIMESTAMP
698 || in6p->in6p_socket->so_options & SO_OTIMESTAMP
699 #endif
700 || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
701 struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
702 ip6_savecontrol(in6p, &opts, ip6, n);
703 }
704
705 m_adj(n, off);
706 if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
707 m_freem(n);
708 if (opts)
709 m_freem(opts);
710 so->so_rcv.sb_overflowed++;
711 UDP6_STATINC(UDP6_STAT_FULLSOCK);
712 } else
713 sorwakeup(so);
714 }
715 }
716 #endif
717
718 #ifdef INET
719 static int
720 udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
721 struct mbuf **mp, int off /* offset of udphdr */)
722 {
723 u_int16_t *sport, *dport;
724 int rcvcnt;
725 struct in_addr *src4, *dst4;
726 struct inpcb_hdr *inph;
727 struct inpcb *inp;
728 struct mbuf *m = *mp;
729
730 rcvcnt = 0;
731 off += sizeof(struct udphdr); /* now, offset of payload */
732
733 if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
734 goto bad;
735
736 src4 = &src->sin_addr;
737 sport = &src->sin_port;
738 dst4 = &dst->sin_addr;
739 dport = &dst->sin_port;
740
741 if (IN_MULTICAST(dst4->s_addr) ||
742 in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
743 /*
744 * Deliver a multicast or broadcast datagram to *all* sockets
745 * for which the local and remote addresses and ports match
746 * those of the incoming datagram. This allows more than
747 * one process to receive multi/broadcasts on the same port.
748 * (This really ought to be done for unicast datagrams as
749 * well, but that would cause problems with existing
750 * applications that open both address-specific sockets and
751 * a wildcard socket listening to the same port -- they would
752 * end up receiving duplicates of every unicast datagram.
753 * Those applications open the multiple sockets to overcome an
754 * inadequacy of the UDP socket interface, but for backwards
755 * compatibility we avoid the problem here rather than
756 * fixing the interface. Maybe 4.5BSD will remedy this?)
757 */
758
759 /*
760 * KAME note: traditionally we dropped udpiphdr from mbuf here.
761 * we need udpiphdr for IPsec processing so we do that later.
762 */
763 /*
764 * Locate pcb(s) for datagram.
765 */
766 CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
767 inp = (struct inpcb *)inph;
768 if (inp->inp_af != AF_INET)
769 continue;
770
771 if (inp->inp_lport != *dport)
772 continue;
773 if (!in_nullhost(inp->inp_laddr)) {
774 if (!in_hosteq(inp->inp_laddr, *dst4))
775 continue;
776 }
777 if (!in_nullhost(inp->inp_faddr)) {
778 if (!in_hosteq(inp->inp_faddr, *src4) ||
779 inp->inp_fport != *sport)
780 continue;
781 }
782
783 udp4_sendup(m, off, (struct sockaddr *)src,
784 inp->inp_socket);
785 rcvcnt++;
786
787 /*
788 * Don't look for additional matches if this one does
789 * not have either the SO_REUSEPORT or SO_REUSEADDR
790 * socket options set. This heuristic avoids searching
791 * through all pcbs in the common case of a non-shared
792 * port. It assumes that an application will never
793 * clear these options after setting them.
794 */
795 if ((inp->inp_socket->so_options &
796 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
797 break;
798 }
799 } else {
800 /*
801 * Locate pcb for datagram.
802 */
803 inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
804 if (inp == 0) {
805 UDP_STATINC(UDP_STAT_PCBHASHMISS);
806 inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
807 if (inp == 0)
808 return rcvcnt;
809 }
810
811 #ifdef IPSEC_NAT_T
812 /* Handle ESP over UDP */
813 if (inp->inp_flags & INP_ESPINUDP_ALL) {
814 struct sockaddr *sa = (struct sockaddr *)src;
815
816 switch(udp4_espinudp(mp, off, sa, inp->inp_socket)) {
817 case -1: /* Error, m was freeed */
818 rcvcnt = -1;
819 goto bad;
820 break;
821
822 case 1: /* ESP over UDP */
823 rcvcnt++;
824 goto bad;
825 break;
826
827 case 0: /* plain UDP */
828 default: /* Unexpected */
829 /*
830 * Normal UDP processing will take place
831 * m may have changed.
832 */
833 m = *mp;
834 break;
835 }
836 }
837 #endif
838
839 /*
840 * Check the minimum TTL for socket.
841 */
842 if (mtod(m, struct ip *)->ip_ttl < inp->inp_ip_minttl)
843 goto bad;
844
845 udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
846 rcvcnt++;
847 }
848
849 bad:
850 return rcvcnt;
851 }
852 #endif
853
854 #ifdef INET6
855 static int
856 udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
857 struct mbuf *m, int off)
858 {
859 u_int16_t sport, dport;
860 int rcvcnt;
861 struct in6_addr src6, *dst6;
862 const struct in_addr *dst4;
863 struct inpcb_hdr *inph;
864 struct in6pcb *in6p;
865
866 rcvcnt = 0;
867 off += sizeof(struct udphdr); /* now, offset of payload */
868
869 if (af != AF_INET && af != AF_INET6)
870 goto bad;
871 if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
872 goto bad;
873
874 src6 = src->sin6_addr;
875 if (sa6_recoverscope(src) != 0) {
876 /* XXX: should be impossible. */
877 goto bad;
878 }
879 sport = src->sin6_port;
880
881 dport = dst->sin6_port;
882 dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
883 dst6 = &dst->sin6_addr;
884
885 if (IN6_IS_ADDR_MULTICAST(dst6) ||
886 (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
887 /*
888 * Deliver a multicast or broadcast datagram to *all* sockets
889 * for which the local and remote addresses and ports match
890 * those of the incoming datagram. This allows more than
891 * one process to receive multi/broadcasts on the same port.
892 * (This really ought to be done for unicast datagrams as
893 * well, but that would cause problems with existing
894 * applications that open both address-specific sockets and
895 * a wildcard socket listening to the same port -- they would
896 * end up receiving duplicates of every unicast datagram.
897 * Those applications open the multiple sockets to overcome an
898 * inadequacy of the UDP socket interface, but for backwards
899 * compatibility we avoid the problem here rather than
900 * fixing the interface. Maybe 4.5BSD will remedy this?)
901 */
902
903 /*
904 * KAME note: traditionally we dropped udpiphdr from mbuf here.
905 * we need udpiphdr for IPsec processing so we do that later.
906 */
907 /*
908 * Locate pcb(s) for datagram.
909 */
910 CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
911 in6p = (struct in6pcb *)inph;
912 if (in6p->in6p_af != AF_INET6)
913 continue;
914
915 if (in6p->in6p_lport != dport)
916 continue;
917 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
918 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
919 dst6))
920 continue;
921 } else {
922 if (IN6_IS_ADDR_V4MAPPED(dst6) &&
923 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
924 continue;
925 }
926 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
927 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
928 &src6) || in6p->in6p_fport != sport)
929 continue;
930 } else {
931 if (IN6_IS_ADDR_V4MAPPED(&src6) &&
932 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
933 continue;
934 }
935
936 udp6_sendup(m, off, (struct sockaddr *)src,
937 in6p->in6p_socket);
938 rcvcnt++;
939
940 /*
941 * Don't look for additional matches if this one does
942 * not have either the SO_REUSEPORT or SO_REUSEADDR
943 * socket options set. This heuristic avoids searching
944 * through all pcbs in the common case of a non-shared
945 * port. It assumes that an application will never
946 * clear these options after setting them.
947 */
948 if ((in6p->in6p_socket->so_options &
949 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
950 break;
951 }
952 } else {
953 /*
954 * Locate pcb for datagram.
955 */
956 in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
957 dport, 0);
958 if (in6p == 0) {
959 UDP_STATINC(UDP_STAT_PCBHASHMISS);
960 in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
961 if (in6p == 0)
962 return rcvcnt;
963 }
964
965 udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
966 rcvcnt++;
967 }
968
969 bad:
970 return rcvcnt;
971 }
972 #endif
973
974 #ifdef INET
975 /*
976 * Notify a udp user of an asynchronous error;
977 * just wake up so that he can collect error status.
978 */
979 static void
980 udp_notify(struct inpcb *inp, int errno)
981 {
982 inp->inp_socket->so_error = errno;
983 sorwakeup(inp->inp_socket);
984 sowwakeup(inp->inp_socket);
985 }
986
987 void *
988 udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
989 {
990 struct ip *ip = v;
991 struct udphdr *uh;
992 void (*notify)(struct inpcb *, int) = udp_notify;
993 int errno;
994
995 if (sa->sa_family != AF_INET
996 || sa->sa_len != sizeof(struct sockaddr_in))
997 return NULL;
998 if ((unsigned)cmd >= PRC_NCMDS)
999 return NULL;
1000 errno = inetctlerrmap[cmd];
1001 if (PRC_IS_REDIRECT(cmd))
1002 notify = in_rtchange, ip = 0;
1003 else if (cmd == PRC_HOSTDEAD)
1004 ip = 0;
1005 else if (errno == 0)
1006 return NULL;
1007 if (ip) {
1008 uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
1009 in_pcbnotify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
1010 ip->ip_src, uh->uh_sport, errno, notify);
1011
1012 /* XXX mapped address case */
1013 } else
1014 in_pcbnotifyall(&udbtable, satocsin(sa)->sin_addr, errno,
1015 notify);
1016 return NULL;
1017 }
1018
1019 int
1020 udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1021 {
1022 int s;
1023 int error = 0;
1024 struct inpcb *inp;
1025 int family;
1026 int optval;
1027
1028 family = so->so_proto->pr_domain->dom_family;
1029
1030 s = splsoftnet();
1031 switch (family) {
1032 #ifdef INET
1033 case PF_INET:
1034 if (sopt->sopt_level != IPPROTO_UDP) {
1035 error = ip_ctloutput(op, so, sopt);
1036 goto end;
1037 }
1038 break;
1039 #endif
1040 #ifdef INET6
1041 case PF_INET6:
1042 if (sopt->sopt_level != IPPROTO_UDP) {
1043 error = ip6_ctloutput(op, so, sopt);
1044 goto end;
1045 }
1046 break;
1047 #endif
1048 default:
1049 error = EAFNOSUPPORT;
1050 goto end;
1051 }
1052
1053
1054 switch (op) {
1055 case PRCO_SETOPT:
1056 inp = sotoinpcb(so);
1057
1058 switch (sopt->sopt_name) {
1059 case UDP_ENCAP:
1060 error = sockopt_getint(sopt, &optval);
1061 if (error)
1062 break;
1063
1064 switch(optval) {
1065 #ifdef IPSEC_NAT_T
1066 case 0:
1067 inp->inp_flags &= ~INP_ESPINUDP_ALL;
1068 break;
1069
1070 case UDP_ENCAP_ESPINUDP:
1071 inp->inp_flags &= ~INP_ESPINUDP_ALL;
1072 inp->inp_flags |= INP_ESPINUDP;
1073 break;
1074
1075 case UDP_ENCAP_ESPINUDP_NON_IKE:
1076 inp->inp_flags &= ~INP_ESPINUDP_ALL;
1077 inp->inp_flags |= INP_ESPINUDP_NON_IKE;
1078 break;
1079 #endif
1080 default:
1081 error = EINVAL;
1082 break;
1083 }
1084 break;
1085
1086 default:
1087 error = ENOPROTOOPT;
1088 break;
1089 }
1090 break;
1091
1092 default:
1093 error = EINVAL;
1094 break;
1095 }
1096
1097 end:
1098 splx(s);
1099 return error;
1100 }
1101
1102
1103 int
1104 udp_output(struct mbuf *m, ...)
1105 {
1106 struct inpcb *inp;
1107 struct udpiphdr *ui;
1108 struct route *ro;
1109 int len = m->m_pkthdr.len;
1110 int error = 0;
1111 va_list ap;
1112
1113 MCLAIM(m, &udp_tx_mowner);
1114 va_start(ap, m);
1115 inp = va_arg(ap, struct inpcb *);
1116 va_end(ap);
1117
1118 /*
1119 * Calculate data length and get a mbuf
1120 * for UDP and IP headers.
1121 */
1122 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
1123 if (m == 0) {
1124 error = ENOBUFS;
1125 goto release;
1126 }
1127
1128 /*
1129 * Compute the packet length of the IP header, and
1130 * punt if the length looks bogus.
1131 */
1132 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1133 error = EMSGSIZE;
1134 goto release;
1135 }
1136
1137 /*
1138 * Fill in mbuf with extended UDP header
1139 * and addresses and length put into network format.
1140 */
1141 ui = mtod(m, struct udpiphdr *);
1142 ui->ui_pr = IPPROTO_UDP;
1143 ui->ui_src = inp->inp_laddr;
1144 ui->ui_dst = inp->inp_faddr;
1145 ui->ui_sport = inp->inp_lport;
1146 ui->ui_dport = inp->inp_fport;
1147 ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
1148
1149 ro = &inp->inp_route;
1150
1151 /*
1152 * Set up checksum and output datagram.
1153 */
1154 if (udpcksum) {
1155 /*
1156 * XXX Cache pseudo-header checksum part for
1157 * XXX "connected" UDP sockets.
1158 */
1159 ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
1160 ui->ui_dst.s_addr, htons((u_int16_t)len +
1161 sizeof(struct udphdr) + IPPROTO_UDP));
1162 m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
1163 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1164 } else
1165 ui->ui_sum = 0;
1166 ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
1167 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
1168 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
1169 UDP_STATINC(UDP_STAT_OPACKETS);
1170
1171 return (ip_output(m, inp->inp_options, ro,
1172 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1173 inp->inp_moptions, inp->inp_socket));
1174
1175 release:
1176 m_freem(m);
1177 return (error);
1178 }
1179
1180 int udp_sendspace = 9216; /* really max datagram size */
1181 int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1182 /* 40 1K datagrams */
1183
1184 /*ARGSUSED*/
1185 int
1186 udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
1187 struct mbuf *control, struct lwp *l)
1188 {
1189 struct inpcb *inp;
1190 int s;
1191 int error = 0;
1192
1193 if (req == PRU_CONTROL)
1194 return (in_control(so, (long)m, (void *)nam,
1195 (struct ifnet *)control, l));
1196
1197 s = splsoftnet();
1198
1199 if (req == PRU_PURGEIF) {
1200 mutex_enter(softnet_lock);
1201 in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
1202 in_purgeif((struct ifnet *)control);
1203 in_pcbpurgeif(&udbtable, (struct ifnet *)control);
1204 mutex_exit(softnet_lock);
1205 splx(s);
1206 return (0);
1207 }
1208
1209 inp = sotoinpcb(so);
1210 #ifdef DIAGNOSTIC
1211 if (req != PRU_SEND && req != PRU_SENDOOB && control)
1212 panic("udp_usrreq: unexpected control mbuf");
1213 #endif
1214 if (req == PRU_ATTACH) {
1215 sosetlock(so);
1216 } else if (inp == 0) {
1217 error = EINVAL;
1218 goto release;
1219 }
1220
1221 /*
1222 * Note: need to block udp_input while changing
1223 * the udp pcb queue and/or pcb addresses.
1224 */
1225 switch (req) {
1226
1227 case PRU_ATTACH:
1228 if (inp != 0) {
1229 error = EISCONN;
1230 break;
1231 }
1232 #ifdef MBUFTRACE
1233 so->so_mowner = &udp_mowner;
1234 so->so_rcv.sb_mowner = &udp_rx_mowner;
1235 so->so_snd.sb_mowner = &udp_tx_mowner;
1236 #endif
1237 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1238 error = soreserve(so, udp_sendspace, udp_recvspace);
1239 if (error)
1240 break;
1241 }
1242 error = in_pcballoc(so, &udbtable);
1243 if (error)
1244 break;
1245 inp = sotoinpcb(so);
1246 inp->inp_ip.ip_ttl = ip_defttl;
1247 break;
1248
1249 case PRU_DETACH:
1250 in_pcbdetach(inp);
1251 break;
1252
1253 case PRU_BIND:
1254 error = in_pcbbind(inp, nam, l);
1255 break;
1256
1257 case PRU_LISTEN:
1258 error = EOPNOTSUPP;
1259 break;
1260
1261 case PRU_CONNECT:
1262 error = in_pcbconnect(inp, nam, l);
1263 if (error)
1264 break;
1265 soisconnected(so);
1266 break;
1267
1268 case PRU_CONNECT2:
1269 error = EOPNOTSUPP;
1270 break;
1271
1272 case PRU_DISCONNECT:
1273 /*soisdisconnected(so);*/
1274 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1275 in_pcbdisconnect(inp);
1276 inp->inp_laddr = zeroin_addr; /* XXX */
1277 in_pcbstate(inp, INP_BOUND); /* XXX */
1278 break;
1279
1280 case PRU_SHUTDOWN:
1281 socantsendmore(so);
1282 break;
1283
1284 case PRU_RCVD:
1285 error = EOPNOTSUPP;
1286 break;
1287
1288 case PRU_SEND:
1289 if (control && control->m_len) {
1290 m_freem(control);
1291 m_freem(m);
1292 error = EINVAL;
1293 break;
1294 }
1295 {
1296 struct in_addr laddr; /* XXX */
1297
1298 if (nam) {
1299 laddr = inp->inp_laddr; /* XXX */
1300 if ((so->so_state & SS_ISCONNECTED) != 0) {
1301 error = EISCONN;
1302 goto die;
1303 }
1304 error = in_pcbconnect(inp, nam, l);
1305 if (error)
1306 goto die;
1307 } else {
1308 if ((so->so_state & SS_ISCONNECTED) == 0) {
1309 error = ENOTCONN;
1310 goto die;
1311 }
1312 }
1313 error = udp_output(m, inp);
1314 m = NULL;
1315 if (nam) {
1316 in_pcbdisconnect(inp);
1317 inp->inp_laddr = laddr; /* XXX */
1318 in_pcbstate(inp, INP_BOUND); /* XXX */
1319 }
1320 die:
1321 if (m)
1322 m_freem(m);
1323 }
1324 break;
1325
1326 case PRU_SENSE:
1327 /*
1328 * stat: don't bother with a blocksize.
1329 */
1330 splx(s);
1331 return (0);
1332
1333 case PRU_RCVOOB:
1334 error = EOPNOTSUPP;
1335 break;
1336
1337 case PRU_SENDOOB:
1338 m_freem(control);
1339 m_freem(m);
1340 error = EOPNOTSUPP;
1341 break;
1342
1343 case PRU_SOCKADDR:
1344 in_setsockaddr(inp, nam);
1345 break;
1346
1347 case PRU_PEERADDR:
1348 in_setpeeraddr(inp, nam);
1349 break;
1350
1351 default:
1352 panic("udp_usrreq");
1353 }
1354
1355 release:
1356 splx(s);
1357 return (error);
1358 }
1359
1360 static int
1361 sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
1362 {
1363
1364 return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
1365 }
1366
1367 /*
1368 * Sysctl for udp variables.
1369 */
1370 SYSCTL_SETUP(sysctl_net_inet_udp_setup, "sysctl net.inet.udp subtree setup")
1371 {
1372
1373 sysctl_createv(clog, 0, NULL, NULL,
1374 CTLFLAG_PERMANENT,
1375 CTLTYPE_NODE, "net", NULL,
1376 NULL, 0, NULL, 0,
1377 CTL_NET, CTL_EOL);
1378 sysctl_createv(clog, 0, NULL, NULL,
1379 CTLFLAG_PERMANENT,
1380 CTLTYPE_NODE, "inet", NULL,
1381 NULL, 0, NULL, 0,
1382 CTL_NET, PF_INET, CTL_EOL);
1383 sysctl_createv(clog, 0, NULL, NULL,
1384 CTLFLAG_PERMANENT,
1385 CTLTYPE_NODE, "udp",
1386 SYSCTL_DESCR("UDPv4 related settings"),
1387 NULL, 0, NULL, 0,
1388 CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
1389
1390 sysctl_createv(clog, 0, NULL, NULL,
1391 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1392 CTLTYPE_INT, "checksum",
1393 SYSCTL_DESCR("Compute UDP checksums"),
1394 NULL, 0, &udpcksum, 0,
1395 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
1396 CTL_EOL);
1397 sysctl_createv(clog, 0, NULL, NULL,
1398 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1399 CTLTYPE_INT, "sendspace",
1400 SYSCTL_DESCR("Default UDP send buffer size"),
1401 NULL, 0, &udp_sendspace, 0,
1402 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
1403 CTL_EOL);
1404 sysctl_createv(clog, 0, NULL, NULL,
1405 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1406 CTLTYPE_INT, "recvspace",
1407 SYSCTL_DESCR("Default UDP receive buffer size"),
1408 NULL, 0, &udp_recvspace, 0,
1409 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
1410 CTL_EOL);
1411 sysctl_createv(clog, 0, NULL, NULL,
1412 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1413 CTLTYPE_INT, "do_loopback_cksum",
1414 SYSCTL_DESCR("Perform UDP checksum on loopback"),
1415 NULL, 0, &udp_do_loopback_cksum, 0,
1416 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
1417 CTL_EOL);
1418 sysctl_createv(clog, 0, NULL, NULL,
1419 CTLFLAG_PERMANENT,
1420 CTLTYPE_STRUCT, "pcblist",
1421 SYSCTL_DESCR("UDP protocol control block list"),
1422 sysctl_inpcblist, 0, &udbtable, 0,
1423 CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
1424 CTL_EOL);
1425 sysctl_createv(clog, 0, NULL, NULL,
1426 CTLFLAG_PERMANENT,
1427 CTLTYPE_STRUCT, "stats",
1428 SYSCTL_DESCR("UDP statistics"),
1429 sysctl_net_inet_udp_stats, 0, NULL, 0,
1430 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
1431 CTL_EOL);
1432 }
1433 #endif
1434
1435 void
1436 udp_statinc(u_int stat)
1437 {
1438
1439 KASSERT(stat < UDP_NSTATS);
1440 UDP_STATINC(stat);
1441 }
1442
1443 #if (defined INET && defined IPSEC_NAT_T)
1444 /*
1445 * Returns:
1446 * 1 if the packet was processed
1447 * 0 if normal UDP processing should take place
1448 * -1 if an error occurent and m was freed
1449 */
1450 static int
1451 udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src,
1452 struct socket *so)
1453 {
1454 size_t len;
1455 void *data;
1456 struct inpcb *inp;
1457 size_t skip = 0;
1458 size_t minlen;
1459 size_t iphdrlen;
1460 struct ip *ip;
1461 struct mbuf *n;
1462 struct m_tag *tag;
1463 struct udphdr *udphdr;
1464 u_int16_t sport, dport;
1465 struct mbuf *m = *mp;
1466
1467 /*
1468 * Collapse the mbuf chain if the first mbuf is too short
1469 * The longest case is: UDP + non ESP marker + ESP
1470 */
1471 minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
1472 if (minlen > m->m_pkthdr.len)
1473 minlen = m->m_pkthdr.len;
1474
1475 if (m->m_len < minlen) {
1476 if ((*mp = m_pullup(m, minlen)) == NULL) {
1477 printf("udp4_espinudp: m_pullup failed\n");
1478 return -1;
1479 }
1480 m = *mp;
1481 }
1482
1483 len = m->m_len - off;
1484 data = mtod(m, char *) + off;
1485 inp = sotoinpcb(so);
1486
1487 /* Ignore keepalive packets */
1488 if ((len == 1) && (*(unsigned char *)data == 0xff)) {
1489 return 1;
1490 }
1491
1492 /*
1493 * Check that the payload is long enough to hold
1494 * an ESP header and compute the length of encapsulation
1495 * header to remove
1496 */
1497 if (inp->inp_flags & INP_ESPINUDP) {
1498 u_int32_t *st = (u_int32_t *)data;
1499
1500 if ((len <= sizeof(struct esp)) || (*st == 0))
1501 return 0; /* Normal UDP processing */
1502
1503 skip = sizeof(struct udphdr);
1504 }
1505
1506 if (inp->inp_flags & INP_ESPINUDP_NON_IKE) {
1507 u_int32_t *st = (u_int32_t *)data;
1508
1509 if ((len <= sizeof(u_int64_t) + sizeof(struct esp))
1510 || ((st[0] | st[1]) != 0))
1511 return 0; /* Normal UDP processing */
1512
1513 skip = sizeof(struct udphdr) + sizeof(u_int64_t);
1514 }
1515
1516 /*
1517 * Get the UDP ports. They are handled in network
1518 * order everywhere in IPSEC_NAT_T code.
1519 */
1520 udphdr = (struct udphdr *)((char *)data - skip);
1521 sport = udphdr->uh_sport;
1522 dport = udphdr->uh_dport;
1523
1524 /*
1525 * Remove the UDP header (and possibly the non ESP marker)
1526 * IP header lendth is iphdrlen
1527 * Before:
1528 * <--- off --->
1529 * +----+------+-----+
1530 * | IP | UDP | ESP |
1531 * +----+------+-----+
1532 * <-skip->
1533 * After:
1534 * +----+-----+
1535 * | IP | ESP |
1536 * +----+-----+
1537 * <-skip->
1538 */
1539 iphdrlen = off - sizeof(struct udphdr);
1540 memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
1541 m_adj(m, skip);
1542
1543 ip = mtod(m, struct ip *);
1544 ip->ip_len = htons(ntohs(ip->ip_len) - skip);
1545 ip->ip_p = IPPROTO_ESP;
1546
1547 /*
1548 * Copy the mbuf to avoid multiple free, as both
1549 * esp4_input (which we call) and udp_input (which
1550 * called us) free the mbuf.
1551 */
1552 if ((n = m_dup(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
1553 printf("udp4_espinudp: m_dup failed\n");
1554 return 0;
1555 }
1556
1557 /*
1558 * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
1559 * the source UDP port. This is required if we want
1560 * to select the right SPD for multiple hosts behind
1561 * same NAT
1562 */
1563 if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
1564 sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
1565 printf("udp4_espinudp: m_tag_get failed\n");
1566 m_freem(n);
1567 return 0;
1568 }
1569 ((u_int16_t *)(tag + 1))[0] = sport;
1570 ((u_int16_t *)(tag + 1))[1] = dport;
1571 m_tag_prepend(n, tag);
1572
1573 #ifdef FAST_IPSEC
1574 ipsec4_common_input(n, iphdrlen, IPPROTO_ESP);
1575 #else
1576 esp4_input(n, iphdrlen);
1577 #endif
1578
1579 /* We handled it, it shoudln't be handled by UDP */
1580 return 1;
1581 }
1582 #endif
1583