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