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