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