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