udp_usrreq.c revision 1.190.2.5 1 /* $NetBSD: udp_usrreq.c,v 1.190.2.5 2014/05/18 17:46:13 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 /*
64 * UDP protocol implementation.
65 * Per RFC 768, August, 1980.
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.190.2.5 2014/05/18 17:46:13 rmind Exp $");
70
71 #include "opt_inet.h"
72 #include "opt_compat_netbsd.h"
73 #include "opt_ipsec.h"
74 #include "opt_inet_csum.h"
75 #include "opt_ipkdb.h"
76 #include "opt_mbuftrace.h"
77
78 #include <sys/param.h>
79 #include <sys/mbuf.h>
80 #include <sys/once.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/systm.h>
85 #include <sys/kmem.h>
86 #include <sys/domain.h>
87 #include <sys/sysctl.h>
88
89 #include <net/if.h>
90 #include <net/route.h>
91
92 #include <netinet/in.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip.h>
96 #include <netinet/in_pcb.h>
97 #include <netinet/ip_var.h>
98 #include <netinet/ip_icmp.h>
99 #include <netinet/udp.h>
100 #include <netinet/udp_var.h>
101 #include <netinet/udp_private.h>
102
103 #ifdef INET6
104 #include <netinet/ip6.h>
105 #include <netinet/icmp6.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet6/ip6_private.h>
108 #include <netinet6/in6_pcb.h>
109 #include <netinet6/udp6_var.h>
110 #include <netinet6/udp6_private.h>
111 #endif
112
113 #ifndef INET6
114 /* always need ip6.h for IP6_EXTHDR_GET */
115 #include <netinet/ip6.h>
116 #endif
117
118 #ifdef IPSEC
119 #include <netipsec/ipsec.h>
120 #include <netipsec/ipsec_var.h>
121 #include <netipsec/ipsec_private.h>
122 #include <netipsec/esp.h>
123 #ifdef INET6
124 #include <netipsec/ipsec6.h>
125 #endif
126 #endif /* IPSEC */
127
128 #ifdef COMPAT_50
129 #include <compat/sys/socket.h>
130 #endif
131
132 #ifdef IPKDB
133 #include <ipkdb/ipkdb.h>
134 #endif
135
136 int udpcksum = 1;
137 int udp_do_loopback_cksum = 0;
138
139 inpcbtable_t * udbtable __read_mostly;
140 percpu_t * udpstat_percpu;
141
142 #ifdef INET
143 #ifdef IPSEC
144 static int udp4_espinudp (struct mbuf **, int, struct sockaddr *,
145 struct socket *);
146 #endif
147 static void udp4_sendup (struct mbuf *, int, struct sockaddr *,
148 struct socket *);
149 static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
150 struct mbuf **, int);
151 static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
152 #endif
153
154 #ifndef UDBHASHSIZE
155 #define UDBHASHSIZE 128
156 #endif
157 int udbhashsize = UDBHASHSIZE;
158
159 static int udp_sendspace = 9216;
160 static int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
161
162 #ifdef MBUFTRACE
163 struct mowner udp_mowner = MOWNER_INIT("udp", "");
164 struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx");
165 struct mowner udp_tx_mowner = MOWNER_INIT("udp", "tx");
166 #endif
167
168 #ifdef UDP_CSUM_COUNTERS
169 #include <sys/device.h>
170
171 #if defined(INET)
172 struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
173 NULL, "udp", "hwcsum bad");
174 struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
175 NULL, "udp", "hwcsum ok");
176 struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
177 NULL, "udp", "hwcsum data");
178 struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
179 NULL, "udp", "swcsum");
180
181 EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
182 EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
183 EVCNT_ATTACH_STATIC(udp_hwcsum_data);
184 EVCNT_ATTACH_STATIC(udp_swcsum);
185 #endif /* defined(INET) */
186
187 #define UDP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
188 #else
189 #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */
190 #endif /* UDP_CSUM_COUNTERS */
191
192 static void sysctl_net_inet_udp_setup(struct sysctllog **);
193
194 static int
195 do_udpinit(void)
196 {
197 udbtable = inpcb_init(udbhashsize, udbhashsize, 0);
198 udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
199
200 MOWNER_ATTACH(&udp_tx_mowner);
201 MOWNER_ATTACH(&udp_rx_mowner);
202 MOWNER_ATTACH(&udp_mowner);
203
204 return 0;
205 }
206
207 void
208 udp_init_common(void)
209 {
210 static ONCE_DECL(doudpinit);
211
212 RUN_ONCE(&doudpinit, do_udpinit);
213 }
214
215 void
216 udp_init(void)
217 {
218
219 sysctl_net_inet_udp_setup(NULL);
220
221 udp_init_common();
222 }
223
224 /*
225 * Checksum extended UDP header and data.
226 */
227
228 int
229 udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
230 int iphlen, int len)
231 {
232 switch (af) {
233 #ifdef INET
234 case AF_INET:
235 return udp4_input_checksum(m, uh, iphlen, len);
236 #endif
237 #ifdef INET6
238 case AF_INET6:
239 return udp6_input_checksum(m, uh, iphlen, len);
240 #endif
241 default:
242 KASSERT(false);
243 }
244 return -1;
245 }
246
247 #ifdef INET
248
249 /*
250 * Checksum extended UDP header and data.
251 */
252
253 static int
254 udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
255 int iphlen, int len)
256 {
257
258 /*
259 * XXX it's better to record and check if this mbuf is
260 * already checked.
261 */
262
263 if (uh->uh_sum == 0)
264 return 0;
265
266 switch (m->m_pkthdr.csum_flags &
267 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
268 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
269 case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
270 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
271 goto badcsum;
272
273 case M_CSUM_UDPv4|M_CSUM_DATA: {
274 u_int32_t hw_csum = m->m_pkthdr.csum_data;
275
276 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
277 if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
278 const struct ip *ip =
279 mtod(m, const struct ip *);
280
281 hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
282 ip->ip_dst.s_addr,
283 htons(hw_csum + len + IPPROTO_UDP));
284 }
285 if ((hw_csum ^ 0xffff) != 0)
286 goto badcsum;
287 break;
288 }
289
290 case M_CSUM_UDPv4:
291 /* Checksum was okay. */
292 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
293 break;
294
295 default:
296 /*
297 * Need to compute it ourselves. Maybe skip checksum
298 * on loopback interfaces.
299 */
300 if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
301 IFF_LOOPBACK) ||
302 udp_do_loopback_cksum)) {
303 UDP_CSUM_COUNTER_INCR(&udp_swcsum);
304 if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
305 goto badcsum;
306 }
307 break;
308 }
309
310 return 0;
311
312 badcsum:
313 UDP_STATINC(UDP_STAT_BADSUM);
314 return -1;
315 }
316
317 void
318 udp_input(struct mbuf *m, ...)
319 {
320 va_list ap;
321 struct sockaddr_in src, dst;
322 struct ip *ip;
323 struct udphdr *uh;
324 int iphlen, len, n;
325 uint16_t ip_len;
326
327 va_start(ap, m);
328 iphlen = va_arg(ap, int);
329 (void)va_arg(ap, int); /* ignore value, advance ap */
330 va_end(ap);
331
332 MCLAIM(m, &udp_rx_mowner);
333 UDP_STATINC(UDP_STAT_IPACKETS);
334
335 /*
336 * Get IP and UDP header together in first mbuf.
337 */
338 ip = mtod(m, struct ip *);
339 IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
340 if (uh == NULL) {
341 UDP_STATINC(UDP_STAT_HDROPS);
342 return;
343 }
344 KASSERT(UDP_HDR_ALIGNED_P(uh));
345
346 /* Destination port of 0 is illegal, based on RFC 768. */
347 if (uh->uh_dport == 0)
348 goto bad;
349
350 /*
351 * Make mbuf data length reflect UDP length.
352 * If not enough data to reflect UDP length, drop.
353 */
354 ip_len = ntohs(ip->ip_len);
355 len = ntohs((uint16_t)uh->uh_ulen);
356 if (ip_len != iphlen + len) {
357 if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
358 UDP_STATINC(UDP_STAT_BADLEN);
359 goto bad;
360 }
361 m_adj(m, iphlen + len - ip_len);
362 }
363
364 /*
365 * Checksum extended UDP header and data.
366 */
367 if (udp4_input_checksum(m, uh, iphlen, len))
368 goto bad;
369
370 /* construct source and dst sockaddrs. */
371 sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
372 sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
373
374 if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
375 UDP_STATINC(UDP_STAT_HDROPS);
376 return;
377 }
378 if (m == NULL) {
379 /*
380 * packet has been processed by ESP stuff -
381 * e.g. dropped NAT-T-keep-alive-packet ...
382 */
383 return;
384 }
385 ip = mtod(m, struct ip *);
386 #ifdef INET6
387 if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
388 struct sockaddr_in6 src6, dst6;
389
390 memset(&src6, 0, sizeof(src6));
391 src6.sin6_family = AF_INET6;
392 src6.sin6_len = sizeof(struct sockaddr_in6);
393 src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
394 memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src,
395 sizeof(ip->ip_src));
396 src6.sin6_port = uh->uh_sport;
397 memset(&dst6, 0, sizeof(dst6));
398 dst6.sin6_family = AF_INET6;
399 dst6.sin6_len = sizeof(struct sockaddr_in6);
400 dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
401 memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst,
402 sizeof(ip->ip_dst));
403 dst6.sin6_port = uh->uh_dport;
404
405 n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
406 }
407 #endif
408
409 if (n == 0) {
410 if (m->m_flags & (M_BCAST | M_MCAST)) {
411 UDP_STATINC(UDP_STAT_NOPORTBCAST);
412 goto bad;
413 }
414 UDP_STATINC(UDP_STAT_NOPORT);
415 #ifdef IPKDB
416 if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
417 m, iphlen + sizeof(struct udphdr),
418 m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
419 /*
420 * It was a debugger connect packet,
421 * just drop it now
422 */
423 goto bad;
424 }
425 #endif
426 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
427 m = NULL;
428 }
429 bad:
430 if (m) {
431 m_freem(m);
432 }
433 return;
434 }
435
436 static void
437 udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
438 struct sockaddr *src, struct socket *so)
439 {
440 struct mbuf *opts = NULL;
441 struct mbuf *n;
442 inpcb_t *inp = NULL;
443
444 if (!so)
445 return;
446 switch (so->so_proto->pr_domain->dom_family) {
447 case AF_INET:
448 inp = sotoinpcb(so);
449 break;
450 #ifdef INET6
451 case AF_INET6:
452 break;
453 #endif
454 default:
455 return;
456 }
457
458 #if defined(IPSEC)
459 /* check AH/ESP integrity. */
460 if (so != NULL && ipsec4_in_reject_so(m, so)) {
461 IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
462 if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
463 icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
464 0, 0);
465 return;
466 }
467 #endif /*IPSEC*/
468
469 if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
470 if (inp && ((inpcb_get_flags(inp) & INP_CONTROLOPTS) != 0
471 #ifdef SO_OTIMESTAMP
472 || so->so_options & SO_OTIMESTAMP
473 #endif
474 || so->so_options & SO_TIMESTAMP)) {
475 struct ip *ip = mtod(n, struct ip *);
476 ip_savecontrol(inp, &opts, ip, n);
477 }
478
479 m_adj(n, off);
480 if (sbappendaddr(&so->so_rcv, src, n,
481 opts) == 0) {
482 m_freem(n);
483 if (opts)
484 m_freem(opts);
485 so->so_rcv.sb_overflowed++;
486 UDP_STATINC(UDP_STAT_FULLSOCK);
487 } else
488 sorwakeup(so);
489 }
490 }
491
492 struct udp_pcb_ctx {
493 struct mbuf * mbuf;
494 struct sockaddr_in * src;
495 struct sockaddr_in * dst;
496 int off;
497 int rcvcnt;
498 };
499
500 static int
501 udp4_pcb_process(inpcb_t *inp, void *arg)
502 {
503 struct udp_pcb_ctx *uctx = arg;
504 struct in_addr dst4 = uctx->dst->sin_addr;
505 in_port_t dport = uctx->dst->sin_port;
506 struct in_addr laddr, faddr;
507 in_port_t lport, fport;
508 struct socket *so;
509
510 inpcb_get_ports(inp, &lport, &fport);
511 if (lport != dport) {
512 return 0;
513 }
514 inpcb_get_addrs(inp, &laddr, &faddr);
515 if (!in_nullhost(laddr) && !in_hosteq(laddr, dst4)) {
516 return 0;
517 }
518 if (!in_nullhost(faddr)) {
519 struct in_addr src4 = uctx->src->sin_addr;
520 in_port_t sport = uctx->src->sin_port;
521
522 if (!in_hosteq(faddr, src4) || fport != sport) {
523 return 0;
524 }
525 }
526
527 so = inpcb_get_socket(inp);
528 udp4_sendup(uctx->mbuf, uctx->off, (struct sockaddr *)uctx->src, so);
529 uctx->rcvcnt++;
530
531 /*
532 * Do not look for additional matches if this one does not have
533 * either the SO_REUSEPORT or SO_REUSEADDR socket options set.
534 * This heuristic avoids searching through all PCBs in the common
535 * case of a non-shared port. It assumes that an application will
536 * never clear these options after setting them.
537 */
538 if ((so->so_options & (SO_REUSEPORT|SO_REUSEADDR)) == 0) {
539 return EJUSTRETURN;
540 }
541 return 0;
542 }
543
544 static int
545 udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
546 struct mbuf **mp, int off /* offset of udphdr */)
547 {
548 in_port_t *sport, *dport;
549 struct in_addr *src4, *dst4;
550 inpcb_t *inp;
551 struct mbuf *m = *mp;
552 int rcvcnt;
553
554 rcvcnt = 0;
555 off += sizeof(struct udphdr); /* now, offset of payload */
556
557 if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
558 goto bad;
559
560 src4 = &src->sin_addr;
561 sport = &src->sin_port;
562 dst4 = &dst->sin_addr;
563 dport = &dst->sin_port;
564
565 if (IN_MULTICAST(dst4->s_addr) ||
566 in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
567 struct udp_pcb_ctx uctx = {
568 .mbuf = m, .src = src, .dst = dst,
569 .off = off, .rcvcnt = 0
570 };
571 int error;
572
573 /*
574 * Deliver a multicast or broadcast datagram to *all* sockets
575 * for which the local and remote addresses and ports match
576 * those of the incoming datagram. This allows more than
577 * one process to receive multi/broadcasts on the same port.
578 */
579 error = inpcb_foreach(udbtable, AF_INET,
580 udp4_pcb_process, &uctx);
581 KASSERT(error == 0 || error == EJUSTRETURN);
582 rcvcnt = uctx.rcvcnt;
583 } else {
584 /*
585 * Locate PCB for datagram.
586 */
587 struct socket *so;
588
589 inp = inpcb_lookup(udbtable, *src4, *sport, *dst4, *dport, NULL);
590 if (inp == NULL) {
591 UDP_STATINC(UDP_STAT_PCBHASHMISS);
592 inp = inpcb_lookup_bound(udbtable, *dst4, *dport);
593 if (inp == NULL)
594 return rcvcnt;
595 }
596 so = inpcb_get_socket(inp);
597
598 #ifdef IPSEC
599 /* Handle ESP over UDP */
600 if (inpcb_get_flags(inp) & INP_ESPINUDP_ALL) {
601 struct sockaddr *sa = (struct sockaddr *)src;
602
603 switch (udp4_espinudp(mp, off, sa, so)) {
604 case -1: /* Error, m was freeed */
605 rcvcnt = -1;
606 goto bad;
607 break;
608
609 case 1: /* ESP over UDP */
610 rcvcnt++;
611 goto bad;
612 break;
613
614 case 0: /* plain UDP */
615 default: /* Unexpected */
616 /*
617 * Normal UDP processing will take place
618 * m may have changed.
619 */
620 m = *mp;
621 break;
622 }
623 }
624 #endif
625
626 /*
627 * Check the minimum TTL for socket.
628 */
629 if (mtod(m, struct ip *)->ip_ttl < inpcb_get_minttl(inp)) {
630 goto bad;
631 }
632 udp4_sendup(m, off, (struct sockaddr *)src, so);
633 rcvcnt++;
634 }
635
636 bad:
637 return rcvcnt;
638 }
639 #endif
640
641 #ifdef INET
642 /*
643 * Notify a UDP user of an asynchronous error;
644 * just wake up so that he can collect error status.
645 */
646 static void
647 udp_notify(inpcb_t *inp, int errno)
648 {
649 struct socket *so = inpcb_get_socket(inp);
650
651 so->so_error = errno;
652 sorwakeup(so);
653 sowwakeup(so);
654 }
655
656 void *
657 udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
658 {
659 struct ip *ip = v;
660 struct udphdr *uh;
661 int errno;
662 bool rdr;
663
664 if (sa->sa_family != AF_INET ||
665 sa->sa_len != sizeof(struct sockaddr_in))
666 return NULL;
667 if ((unsigned)cmd >= PRC_NCMDS)
668 return NULL;
669 errno = inetctlerrmap[cmd];
670
671 rdr = PRC_IS_REDIRECT(cmd);
672 if (rdr || cmd == PRC_HOSTDEAD || ip == NULL) {
673 inpcb_notifyall(udbtable, satocsin(sa)->sin_addr,
674 errno, rdr ? inpcb_rtchange : udp_notify);
675 return NULL;
676 } else if (errno == 0) {
677 return NULL;
678 }
679
680 /* Note: mapped address case */
681 uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
682 inpcb_notify(udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
683 ip->ip_src, uh->uh_sport, errno, udp_notify);
684 return NULL;
685 }
686
687 int
688 udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
689 {
690 int family, optval, inpflags, error = 0;
691 inpcb_t *inp;
692
693 KASSERT(solocked(so));
694
695 family = so->so_proto->pr_domain->dom_family;
696
697 switch (family) {
698 #ifdef INET
699 case PF_INET:
700 if (sopt->sopt_level != IPPROTO_UDP) {
701 return ip_ctloutput(op, so, sopt);
702 }
703 break;
704 #endif
705 #ifdef INET6
706 case PF_INET6:
707 if (sopt->sopt_level != IPPROTO_UDP) {
708 return ip6_ctloutput(op, so, sopt);
709 }
710 break;
711 #endif
712 default:
713 return EAFNOSUPPORT;
714 }
715
716 switch (op) {
717 case PRCO_SETOPT:
718 inp = sotoinpcb(so);
719
720 switch (sopt->sopt_name) {
721 case UDP_ENCAP:
722 error = sockopt_getint(sopt, &optval);
723 if (error)
724 break;
725
726 inpflags = inpcb_get_flags(inp);
727 switch(optval) {
728 case 0:
729 inpflags &= ~INP_ESPINUDP_ALL;
730 break;
731
732 case UDP_ENCAP_ESPINUDP:
733 inpflags &= ~INP_ESPINUDP_ALL;
734 inpflags |= INP_ESPINUDP;
735 break;
736
737 case UDP_ENCAP_ESPINUDP_NON_IKE:
738 inpflags &= ~INP_ESPINUDP_ALL;
739 inpflags |= INP_ESPINUDP_NON_IKE;
740 break;
741 default:
742 error = EINVAL;
743 break;
744 }
745 inpcb_set_flags(inp, inpflags);
746 break;
747
748 default:
749 error = ENOPROTOOPT;
750 break;
751 }
752 break;
753
754 default:
755 error = EINVAL;
756 break;
757 }
758
759 return error;
760 }
761
762 static int
763 udp_output(struct mbuf *m, inpcb_t *inp)
764 {
765 struct socket *so;
766 struct udpiphdr *ui;
767 struct route *ro;
768 int len = m->m_pkthdr.len;
769 int error = 0;
770
771 MCLAIM(m, &udp_tx_mowner);
772 so = inpcb_get_socket(inp);
773 KASSERT(solocked(so));
774
775 /*
776 * Calculate data length and get a mbuf for UDP and IP headers.
777 */
778 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
779 if (m == NULL) {
780 error = ENOBUFS;
781 goto release;
782 }
783
784 /*
785 * Compute the packet length of the IP header, and
786 * punt if the length looks bogus.
787 */
788 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
789 error = EMSGSIZE;
790 goto release;
791 }
792
793 /*
794 * Fill in mbuf with extended UDP header.
795 */
796 ui = mtod(m, struct udpiphdr *);
797 ui->ui_pr = IPPROTO_UDP;
798 inpcb_get_addrs(inp, &ui->ui_src, &ui->ui_dst);
799 inpcb_get_ports(inp, &ui->ui_sport, &ui->ui_dport);
800 ui->ui_ulen = htons((uint16_t)len + sizeof(struct udphdr));
801
802 /*
803 * Set up checksum and output datagram.
804 */
805 if (udpcksum) {
806 /*
807 * XXX Cache pseudo-header checksum part for
808 * XXX "connected" UDP sockets.
809 */
810 ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
811 ui->ui_dst.s_addr, htons((u_int16_t)len +
812 sizeof(struct udphdr) + IPPROTO_UDP));
813 m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
814 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
815 } else
816 ui->ui_sum = 0;
817
818 struct ip *ui_ip = (struct ip *)ui;
819 ui_ip->ip_len = htons(sizeof (struct udpiphdr) + len);
820
821 struct ip *inp_ip = in_getiphdr(inp);
822 ui_ip->ip_ttl = inp_ip->ip_ttl; /* XXX */
823 ui_ip->ip_tos = inp_ip->ip_tos; /* XXX */
824 UDP_STATINC(UDP_STAT_OPACKETS);
825
826 ro = inpcb_get_route(inp);
827 return ip_output(m, inpcb_get_options(inp), ro,
828 so->so_options & (SO_DONTROUTE | SO_BROADCAST),
829 inpcb_get_moptions(inp), so);
830
831 release:
832 m_freem(m);
833 return error;
834 }
835
836 static int
837 udp_attach(struct socket *so, int proto)
838 {
839 inpcb_t *inp;
840 struct ip *ip;
841 int error;
842
843 KASSERT(sotoinpcb(so) == NULL);
844
845 /* Assign the lock (must happen if we will error out). */
846 sosetlock(so);
847
848 #ifdef MBUFTRACE
849 so->so_mowner = &udp_mowner;
850 so->so_rcv.sb_mowner = &udp_rx_mowner;
851 so->so_snd.sb_mowner = &udp_tx_mowner;
852 #endif
853 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
854 error = soreserve(so, udp_sendspace, udp_recvspace);
855 if (error) {
856 return error;
857 }
858 }
859
860 error = inpcb_create(so, udbtable);
861 if (error) {
862 return error;
863 }
864 inp = sotoinpcb(so);
865 ip = in_getiphdr(inp);
866 ip->ip_ttl = ip_defttl;
867 return error;
868 }
869
870 static void
871 udp_detach(struct socket *so)
872 {
873 inpcb_t *inp;
874
875 KASSERT(solocked(so));
876 inp = sotoinpcb(so);
877 KASSERT(inp != NULL);
878 inpcb_destroy(inp);
879 }
880
881 static int
882 udp_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
883 struct mbuf *control)
884 {
885 inpcb_t *inp;
886 struct in_addr laddr;
887 int error;
888
889 KASSERT(solocked(so));
890 inp = sotoinpcb(so);
891
892 if (control && control->m_len) {
893 m_freem(control);
894 m_freem(m);
895 return EINVAL;
896 }
897
898 if ((so->so_state & SS_ISCONNECTED) != 0) {
899 m_freem(m);
900 return nam ? EISCONN : ENOTCONN;
901 }
902
903 if (nam) {
904 /*
905 * XXX: sendto() case - temporarily connect the socket
906 * to the destination, send and then disconnect. Also,
907 * preserve the local address as it may be changed.
908 */
909 inpcb_get_addrs(inp, &laddr, NULL);
910 if ((error = inpcb_connect(inp, nam, curlwp)) != 0) {
911 m_freem(m);
912 return error;
913 }
914 }
915 error = udp_output(m, inp);
916 if (nam) {
917 inpcb_disconnect(inp);
918 inpcb_set_addrs(inp, &laddr, NULL);
919 inpcb_set_state(inp, INP_BOUND);
920 }
921 return error;
922 }
923
924 static int
925 udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
926 struct mbuf *control, struct lwp *l)
927 {
928 inpcb_t *inp;
929 int error = 0;
930
931 KASSERT(req != PRU_ATTACH);
932 KASSERT(req != PRU_DETACH);
933
934 if (req == PRU_CONTROL) {
935 KERNEL_LOCK(1, NULL);
936 error = in_control(so, (long)m, nam, (ifnet_t *)control, l);
937 KERNEL_UNLOCK_ONE(NULL);
938 return error;
939 }
940 if (req == PRU_PURGEIF) {
941 KERNEL_LOCK(1, NULL);
942 mutex_enter(softnet_lock);
943 inpcb_purgeif0(udbtable, (ifnet_t *)control);
944 in_purgeif((ifnet_t *)control);
945 inpcb_purgeif(udbtable, (ifnet_t *)control);
946 mutex_exit(softnet_lock);
947 KERNEL_UNLOCK_ONE(NULL);
948 return 0;
949 }
950
951 KASSERT(solocked(so));
952 inp = sotoinpcb(so);
953
954 KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB));
955 if (inp == NULL) {
956 return EINVAL;
957 }
958
959 switch (req) {
960 case PRU_BIND:
961 error = inpcb_bind(inp, nam, l);
962 break;
963
964 case PRU_LISTEN:
965 error = EOPNOTSUPP;
966 break;
967
968 case PRU_CONNECT:
969 error = inpcb_connect(inp, nam, l);
970 if (error)
971 break;
972 soisconnected(so);
973 break;
974
975 case PRU_CONNECT2:
976 error = EOPNOTSUPP;
977 break;
978
979 case PRU_DISCONNECT:
980 /*soisdisconnected(so);*/
981 so->so_state &= ~SS_ISCONNECTED; /* XXX */
982 inpcb_disconnect(inp);
983 break;
984
985 case PRU_SHUTDOWN:
986 socantsendmore(so);
987 break;
988
989 case PRU_RCVD:
990 error = EOPNOTSUPP;
991 break;
992
993 case PRU_SEND:
994 error = udp_send(so, m, nam, control);
995 break;
996
997 case PRU_SENSE:
998 /*
999 * stat: don't bother with a blocksize.
1000 */
1001 return 0;
1002
1003 case PRU_RCVOOB:
1004 error = EOPNOTSUPP;
1005 break;
1006
1007 case PRU_SENDOOB:
1008 m_freem(control);
1009 m_freem(m);
1010 error = EOPNOTSUPP;
1011 break;
1012
1013 case PRU_SOCKADDR:
1014 inpcb_fetch_sockaddr(inp, nam);
1015 break;
1016
1017 case PRU_PEERADDR:
1018 inpcb_fetch_peeraddr(inp, nam);
1019 break;
1020
1021 default:
1022 panic("udp_usrreq");
1023 }
1024
1025 return error;
1026 }
1027
1028 static int
1029 sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
1030 {
1031
1032 return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
1033 }
1034
1035 /*
1036 * Sysctl for udp variables.
1037 */
1038 static void
1039 sysctl_net_inet_udp_setup(struct sysctllog **clog)
1040 {
1041
1042 sysctl_createv(clog, 0, NULL, NULL,
1043 CTLFLAG_PERMANENT,
1044 CTLTYPE_NODE, "inet", NULL,
1045 NULL, 0, NULL, 0,
1046 CTL_NET, PF_INET, CTL_EOL);
1047 sysctl_createv(clog, 0, NULL, NULL,
1048 CTLFLAG_PERMANENT,
1049 CTLTYPE_NODE, "udp",
1050 SYSCTL_DESCR("UDPv4 related settings"),
1051 NULL, 0, NULL, 0,
1052 CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
1053
1054 sysctl_createv(clog, 0, NULL, NULL,
1055 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1056 CTLTYPE_INT, "checksum",
1057 SYSCTL_DESCR("Compute UDP checksums"),
1058 NULL, 0, &udpcksum, 0,
1059 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
1060 CTL_EOL);
1061 sysctl_createv(clog, 0, NULL, NULL,
1062 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1063 CTLTYPE_INT, "sendspace",
1064 SYSCTL_DESCR("Default UDP send buffer size"),
1065 NULL, 0, &udp_sendspace, 0,
1066 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
1067 CTL_EOL);
1068 sysctl_createv(clog, 0, NULL, NULL,
1069 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1070 CTLTYPE_INT, "recvspace",
1071 SYSCTL_DESCR("Default UDP receive buffer size"),
1072 NULL, 0, &udp_recvspace, 0,
1073 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
1074 CTL_EOL);
1075 sysctl_createv(clog, 0, NULL, NULL,
1076 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1077 CTLTYPE_INT, "do_loopback_cksum",
1078 SYSCTL_DESCR("Perform UDP checksum on loopback"),
1079 NULL, 0, &udp_do_loopback_cksum, 0,
1080 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
1081 CTL_EOL);
1082 sysctl_createv(clog, 0, NULL, NULL,
1083 CTLFLAG_PERMANENT,
1084 CTLTYPE_STRUCT, "pcblist",
1085 SYSCTL_DESCR("UDP protocol control block list"),
1086 sysctl_inpcblist, 0, udbtable, 0,
1087 CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
1088 CTL_EOL);
1089 sysctl_createv(clog, 0, NULL, NULL,
1090 CTLFLAG_PERMANENT,
1091 CTLTYPE_STRUCT, "stats",
1092 SYSCTL_DESCR("UDP statistics"),
1093 sysctl_net_inet_udp_stats, 0, NULL, 0,
1094 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
1095 CTL_EOL);
1096 }
1097 #endif
1098
1099 void
1100 udp_statinc(u_int stat)
1101 {
1102
1103 KASSERT(stat < UDP_NSTATS);
1104 UDP_STATINC(stat);
1105 }
1106
1107 #if defined(INET) && defined(IPSEC)
1108 /*
1109 * Returns:
1110 * 1 if the packet was processed
1111 * 0 if normal UDP processing should take place
1112 * -1 if an error occurent and m was freed
1113 */
1114 static int
1115 udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src,
1116 struct socket *so)
1117 {
1118 size_t len;
1119 void *data;
1120 inpcb_t *inp;
1121 size_t skip = 0;
1122 size_t minlen;
1123 size_t iphdrlen;
1124 struct ip *ip;
1125 struct m_tag *tag;
1126 struct udphdr *udphdr;
1127 in_port_t sport, dport;
1128 struct mbuf *m = *mp;
1129 int inpflags;
1130
1131 /*
1132 * Collapse the mbuf chain if the first mbuf is too short
1133 * The longest case is: UDP + non ESP marker + ESP
1134 */
1135 minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
1136 if (minlen > m->m_pkthdr.len)
1137 minlen = m->m_pkthdr.len;
1138
1139 if (m->m_len < minlen) {
1140 if ((*mp = m_pullup(m, minlen)) == NULL) {
1141 printf("udp4_espinudp: m_pullup failed\n");
1142 return -1;
1143 }
1144 m = *mp;
1145 }
1146
1147 len = m->m_len - off;
1148 data = mtod(m, char *) + off;
1149 inp = sotoinpcb(so);
1150
1151 /* Ignore keepalive packets */
1152 if ((len == 1) && (*(unsigned char *)data == 0xff)) {
1153 m_free(m);
1154 *mp = NULL; /* avoid any further processiong by caller ... */
1155 return 1;
1156 }
1157 inpflags = inpcb_get_flags(inp);
1158
1159 /*
1160 * Check that the payload is long enough to hold
1161 * an ESP header and compute the length of encapsulation
1162 * header to remove
1163 */
1164 if (inpflags & INP_ESPINUDP) {
1165 u_int32_t *st = (u_int32_t *)data;
1166
1167 if ((len <= sizeof(struct esp)) || (*st == 0))
1168 return 0; /* Normal UDP processing */
1169
1170 skip = sizeof(struct udphdr);
1171 }
1172
1173 if (inpflags & INP_ESPINUDP_NON_IKE) {
1174 u_int32_t *st = (u_int32_t *)data;
1175
1176 if ((len <= sizeof(u_int64_t) + sizeof(struct esp))
1177 || ((st[0] | st[1]) != 0))
1178 return 0; /* Normal UDP processing */
1179
1180 skip = sizeof(struct udphdr) + sizeof(u_int64_t);
1181 }
1182
1183 /*
1184 * Get the UDP ports. They are handled in network
1185 * order everywhere in IPSEC_NAT_T code.
1186 */
1187 udphdr = (struct udphdr *)((char *)data - skip);
1188 sport = udphdr->uh_sport;
1189 dport = udphdr->uh_dport;
1190
1191 /*
1192 * Remove the UDP header (and possibly the non ESP marker)
1193 * IP header lendth is iphdrlen
1194 * Before:
1195 * <--- off --->
1196 * +----+------+-----+
1197 * | IP | UDP | ESP |
1198 * +----+------+-----+
1199 * <-skip->
1200 * After:
1201 * +----+-----+
1202 * | IP | ESP |
1203 * +----+-----+
1204 * <-skip->
1205 */
1206 iphdrlen = off - sizeof(struct udphdr);
1207 memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
1208 m_adj(m, skip);
1209
1210 ip = mtod(m, struct ip *);
1211 ip->ip_len = htons(ntohs(ip->ip_len) - skip);
1212 ip->ip_p = IPPROTO_ESP;
1213
1214 /*
1215 * We have modified the packet - it is now ESP, so we should not
1216 * return to UDP processing ...
1217 *
1218 * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
1219 * the source UDP port. This is required if we want
1220 * to select the right SPD for multiple hosts behind
1221 * same NAT
1222 */
1223 if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
1224 sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
1225 printf("udp4_espinudp: m_tag_get failed\n");
1226 m_freem(m);
1227 return -1;
1228 }
1229 ((u_int16_t *)(tag + 1))[0] = sport;
1230 ((u_int16_t *)(tag + 1))[1] = dport;
1231 m_tag_prepend(m, tag);
1232
1233 #ifdef IPSEC
1234 ipsec4_common_input(m, iphdrlen, IPPROTO_ESP);
1235 #else
1236 esp4_input(m, iphdrlen);
1237 #endif
1238
1239 /* We handled it, it shouldn't be handled by UDP */
1240 *mp = NULL; /* avoid free by caller ... */
1241 return 1;
1242 }
1243 #endif
1244
1245 const struct pr_usrreqs udp_usrreqs = {
1246 .pr_attach = udp_attach,
1247 .pr_detach = udp_detach,
1248 .pr_generic = udp_usrreq,
1249 };
1250