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