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