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