udp_usrreq.c revision 1.259 1 /* $NetBSD: udp_usrreq.c,v 1.259 2020/08/20 21:21:32 riastradh 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.259 2020/08/20 21:21:32 riastradh Exp $");
70
71 #ifdef _KERNEL_OPT
72 #include "opt_inet.h"
73 #include "opt_ipsec.h"
74 #include "opt_inet_csum.h"
75 #include "opt_mbuftrace.h"
76 #include "opt_net_mpsafe.h"
77 #endif
78
79 #include <sys/param.h>
80 #include <sys/mbuf.h>
81 #include <sys/once.h>
82 #include <sys/protosw.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/systm.h>
86 #include <sys/proc.h>
87 #include <sys/domain.h>
88 #include <sys/sysctl.h>
89
90 #include <net/if.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 <netinet6/ip6_var.h>
106 #include <netinet6/ip6_private.h>
107 #include <netinet6/in6_pcb.h>
108 #include <netinet6/udp6_var.h>
109 #include <netinet6/udp6_private.h>
110 #endif
111
112 #ifndef INET6
113 #include <netinet/ip6.h>
114 #endif
115
116 #ifdef IPSEC
117 #include <netipsec/ipsec.h>
118 #include <netipsec/esp.h>
119 #endif
120
121 int udpcksum = 1;
122 int udp_do_loopback_cksum = 0;
123
124 struct inpcbtable udbtable;
125
126 percpu_t *udpstat_percpu;
127
128 #ifdef INET
129 #ifdef IPSEC
130 static int udp4_espinudp(struct mbuf **, int);
131 #endif
132 static void udp4_sendup(struct mbuf *, int, struct sockaddr *,
133 struct socket *);
134 static int udp4_realinput(struct sockaddr_in *, struct sockaddr_in *,
135 struct mbuf **, int);
136 static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
137 #endif
138 #ifdef INET
139 static void udp_notify (struct inpcb *, int);
140 #endif
141
142 #ifndef UDBHASHSIZE
143 #define UDBHASHSIZE 128
144 #endif
145 int udbhashsize = UDBHASHSIZE;
146
147 /*
148 * For send - really max datagram size; for receive - 40 1K datagrams.
149 */
150 static int udp_sendspace = 9216;
151 static int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
152
153 #ifdef MBUFTRACE
154 struct mowner udp_mowner = MOWNER_INIT("udp", "");
155 struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx");
156 struct mowner udp_tx_mowner = MOWNER_INIT("udp", "tx");
157 #endif
158
159 #ifdef UDP_CSUM_COUNTERS
160 #include <sys/device.h>
161
162 #if defined(INET)
163 struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
164 NULL, "udp", "hwcsum bad");
165 struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
166 NULL, "udp", "hwcsum ok");
167 struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
168 NULL, "udp", "hwcsum data");
169 struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
170 NULL, "udp", "swcsum");
171
172 EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
173 EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
174 EVCNT_ATTACH_STATIC(udp_hwcsum_data);
175 EVCNT_ATTACH_STATIC(udp_swcsum);
176 #endif /* defined(INET) */
177
178 #define UDP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
179 #else
180 #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */
181 #endif /* UDP_CSUM_COUNTERS */
182
183 static void sysctl_net_inet_udp_setup(struct sysctllog **);
184
185 static int
186 do_udpinit(void)
187 {
188
189 in_pcbinit(&udbtable, udbhashsize, udbhashsize);
190 udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
191
192 MOWNER_ATTACH(&udp_tx_mowner);
193 MOWNER_ATTACH(&udp_rx_mowner);
194 MOWNER_ATTACH(&udp_mowner);
195
196 return 0;
197 }
198
199 void
200 udp_init_common(void)
201 {
202 static ONCE_DECL(doudpinit);
203
204 RUN_ONCE(&doudpinit, do_udpinit);
205 }
206
207 void
208 udp_init(void)
209 {
210
211 sysctl_net_inet_udp_setup(NULL);
212
213 udp_init_common();
214 }
215
216 /*
217 * Checksum extended UDP header and data.
218 */
219 int
220 udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
221 int iphlen, int len)
222 {
223
224 switch (af) {
225 #ifdef INET
226 case AF_INET:
227 return udp4_input_checksum(m, uh, iphlen, len);
228 #endif
229 #ifdef INET6
230 case AF_INET6:
231 return udp6_input_checksum(m, uh, iphlen, len);
232 #endif
233 }
234 #ifdef DIAGNOSTIC
235 panic("udp_input_checksum: unknown af %d", af);
236 #endif
237 /* NOTREACHED */
238 return -1;
239 }
240
241 #ifdef INET
242
243 /*
244 * Checksum extended UDP header and data.
245 */
246 static int
247 udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
248 int iphlen, int len)
249 {
250
251 /*
252 * XXX it's better to record and check if this mbuf is
253 * already checked.
254 */
255
256 if (uh->uh_sum == 0)
257 return 0;
258
259 switch (m->m_pkthdr.csum_flags &
260 ((m_get_rcvif_NOMPSAFE(m)->if_csum_flags_rx & M_CSUM_UDPv4) |
261 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
262 case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
263 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
264 goto badcsum;
265
266 case M_CSUM_UDPv4|M_CSUM_DATA: {
267 u_int32_t hw_csum = m->m_pkthdr.csum_data;
268
269 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
270 if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
271 const struct ip *ip =
272 mtod(m, const struct ip *);
273
274 hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
275 ip->ip_dst.s_addr,
276 htons(hw_csum + len + IPPROTO_UDP));
277 }
278 if ((hw_csum ^ 0xffff) != 0)
279 goto badcsum;
280 break;
281 }
282
283 case M_CSUM_UDPv4:
284 /* Checksum was okay. */
285 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
286 break;
287
288 default:
289 /*
290 * Need to compute it ourselves. Maybe skip checksum
291 * on loopback interfaces.
292 */
293 if (__predict_true(!(m_get_rcvif_NOMPSAFE(m)->if_flags &
294 IFF_LOOPBACK) ||
295 udp_do_loopback_cksum)) {
296 UDP_CSUM_COUNTER_INCR(&udp_swcsum);
297 if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
298 goto badcsum;
299 }
300 break;
301 }
302
303 return 0;
304
305 badcsum:
306 UDP_STATINC(UDP_STAT_BADSUM);
307 return -1;
308 }
309
310 void
311 udp_input(struct mbuf *m, int off, int proto)
312 {
313 struct sockaddr_in src, dst;
314 struct ip *ip;
315 struct udphdr *uh;
316 int iphlen = off;
317 int len;
318 int n;
319 u_int16_t ip_len;
320
321 MCLAIM(m, &udp_rx_mowner);
322 UDP_STATINC(UDP_STAT_IPACKETS);
323
324 /*
325 * Get IP and UDP header together in first mbuf.
326 */
327 ip = mtod(m, struct ip *);
328 M_REGION_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
329 if (uh == NULL) {
330 UDP_STATINC(UDP_STAT_HDROPS);
331 return;
332 }
333
334 /*
335 * Enforce alignment requirements that are violated in
336 * some cases, see kern/50766 for details.
337 */
338 if (UDP_HDR_ALIGNED_P(uh) == 0) {
339 m = m_copyup(m, iphlen + sizeof(struct udphdr), 0);
340 if (m == NULL) {
341 UDP_STATINC(UDP_STAT_HDROPS);
342 return;
343 }
344 ip = mtod(m, struct ip *);
345 uh = (struct udphdr *)(mtod(m, char *) + iphlen);
346 }
347 KASSERT(UDP_HDR_ALIGNED_P(uh));
348
349 /* destination port of 0 is illegal, based on RFC768. */
350 if (uh->uh_dport == 0)
351 goto bad;
352
353 /*
354 * Make mbuf data length reflect UDP length.
355 * If not enough data to reflect UDP length, drop.
356 */
357 ip_len = ntohs(ip->ip_len);
358 len = ntohs((u_int16_t)uh->uh_ulen);
359 if (len < sizeof(struct udphdr)) {
360 UDP_STATINC(UDP_STAT_BADLEN);
361 goto bad;
362 }
363 if (ip_len != iphlen + len) {
364 if (ip_len < iphlen + len) {
365 UDP_STATINC(UDP_STAT_BADLEN);
366 goto bad;
367 }
368 m_adj(m, iphlen + len - ip_len);
369 }
370
371 /*
372 * Checksum extended UDP header and data.
373 */
374 if (udp4_input_checksum(m, uh, iphlen, len))
375 goto badcsum;
376
377 /* construct source and dst sockaddrs. */
378 sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
379 sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
380
381 if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
382 UDP_STATINC(UDP_STAT_HDROPS);
383 return;
384 }
385 if (m == NULL) {
386 /*
387 * packet has been processed by ESP stuff -
388 * e.g. dropped NAT-T-keep-alive-packet ...
389 */
390 return;
391 }
392
393 ip = mtod(m, struct ip *);
394 M_REGION_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
395 if (uh == NULL) {
396 UDP_STATINC(UDP_STAT_HDROPS);
397 return;
398 }
399 /* XXX Re-enforce alignment? */
400
401 #ifdef INET6
402 if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
403 struct sockaddr_in6 src6, dst6;
404
405 memset(&src6, 0, sizeof(src6));
406 src6.sin6_family = AF_INET6;
407 src6.sin6_len = sizeof(struct sockaddr_in6);
408 in6_in_2_v4mapin6(&ip->ip_src, &src6.sin6_addr);
409 src6.sin6_port = uh->uh_sport;
410 memset(&dst6, 0, sizeof(dst6));
411 dst6.sin6_family = AF_INET6;
412 dst6.sin6_len = sizeof(struct sockaddr_in6);
413 in6_in_2_v4mapin6(&ip->ip_dst, &dst6.sin6_addr);
414 dst6.sin6_port = uh->uh_dport;
415
416 n += udp6_realinput(AF_INET, &src6, &dst6, &m, iphlen);
417 }
418 #endif
419
420 if (n == 0) {
421 if (m->m_flags & (M_BCAST | M_MCAST)) {
422 UDP_STATINC(UDP_STAT_NOPORTBCAST);
423 goto bad;
424 }
425 UDP_STATINC(UDP_STAT_NOPORT);
426 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
427 m = NULL;
428 }
429
430 bad:
431 if (m)
432 m_freem(m);
433 return;
434
435 badcsum:
436 m_freem(m);
437 }
438 #endif
439
440 #ifdef INET
441 static void
442 udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
443 struct sockaddr *src, struct socket *so)
444 {
445 struct mbuf *opts = NULL;
446 struct mbuf *n;
447 struct inpcb *inp;
448
449 KASSERT(so != NULL);
450 KASSERT(so->so_proto->pr_domain->dom_family == AF_INET);
451 inp = sotoinpcb(so);
452 KASSERT(inp != NULL);
453
454 #if defined(IPSEC)
455 if (ipsec_used && ipsec_in_reject(m, inp)) {
456 if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
457 icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
458 0, 0);
459 return;
460 }
461 #endif
462
463 if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
464 if (inp->inp_flags & INP_CONTROLOPTS ||
465 SOOPT_TIMESTAMP(so->so_options)) {
466 struct ip *ip = mtod(n, struct ip *);
467 ip_savecontrol(inp, &opts, ip, n);
468 }
469
470 m_adj(n, off);
471 if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
472 m_freem(n);
473 if (opts)
474 m_freem(opts);
475 UDP_STATINC(UDP_STAT_FULLSOCK);
476 soroverflow(so);
477 } else
478 sorwakeup(so);
479 }
480 }
481 #endif
482
483 #ifdef INET
484 static int
485 udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
486 struct mbuf **mp, int off /* offset of udphdr */)
487 {
488 u_int16_t *sport, *dport;
489 int rcvcnt;
490 struct in_addr *src4, *dst4;
491 struct inpcb_hdr *inph;
492 struct inpcb *inp;
493 struct mbuf *m = *mp;
494
495 rcvcnt = 0;
496 off += sizeof(struct udphdr); /* now, offset of payload */
497
498 if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
499 goto bad;
500
501 src4 = &src->sin_addr;
502 sport = &src->sin_port;
503 dst4 = &dst->sin_addr;
504 dport = &dst->sin_port;
505
506 if (IN_MULTICAST(dst4->s_addr) ||
507 in_broadcast(*dst4, m_get_rcvif_NOMPSAFE(m))) {
508 /*
509 * Deliver a multicast or broadcast datagram to *all* sockets
510 * for which the local and remote addresses and ports match
511 * those of the incoming datagram. This allows more than
512 * one process to receive multi/broadcasts on the same port.
513 * (This really ought to be done for unicast datagrams as
514 * well, but that would cause problems with existing
515 * applications that open both address-specific sockets and
516 * a wildcard socket listening to the same port -- they would
517 * end up receiving duplicates of every unicast datagram.
518 * Those applications open the multiple sockets to overcome an
519 * inadequacy of the UDP socket interface, but for backwards
520 * compatibility we avoid the problem here rather than
521 * fixing the interface. Maybe 4.5BSD will remedy this?)
522 */
523
524 /*
525 * KAME note: traditionally we dropped udpiphdr from mbuf here.
526 * we need udpiphdr for IPsec processing so we do that later.
527 */
528 /*
529 * Locate pcb(s) for datagram.
530 */
531 TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
532 inp = (struct inpcb *)inph;
533 if (inp->inp_af != AF_INET)
534 continue;
535
536 if (inp->inp_lport != *dport)
537 continue;
538 if (!in_nullhost(inp->inp_laddr)) {
539 if (!in_hosteq(inp->inp_laddr, *dst4))
540 continue;
541 }
542 if (!in_nullhost(inp->inp_faddr)) {
543 if (!in_hosteq(inp->inp_faddr, *src4) ||
544 inp->inp_fport != *sport)
545 continue;
546 }
547
548 udp4_sendup(m, off, (struct sockaddr *)src,
549 inp->inp_socket);
550 rcvcnt++;
551
552 /*
553 * Don't look for additional matches if this one does
554 * not have either the SO_REUSEPORT or SO_REUSEADDR
555 * socket options set. This heuristic avoids searching
556 * through all pcbs in the common case of a non-shared
557 * port. It assumes that an application will never
558 * clear these options after setting them.
559 */
560 if ((inp->inp_socket->so_options &
561 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
562 break;
563 }
564 } else {
565 /*
566 * Locate pcb for datagram.
567 */
568 inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4,
569 *dport, 0);
570 if (inp == 0) {
571 UDP_STATINC(UDP_STAT_PCBHASHMISS);
572 inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
573 if (inp == 0)
574 return rcvcnt;
575 }
576
577 #ifdef IPSEC
578 /* Handle ESP over UDP */
579 if (inp->inp_flags & INP_ESPINUDP) {
580 switch (udp4_espinudp(mp, off)) {
581 case -1: /* Error, m was freed */
582 rcvcnt = -1;
583 goto bad;
584
585 case 1: /* ESP over UDP */
586 rcvcnt++;
587 goto bad;
588
589 case 0: /* plain UDP */
590 default: /* Unexpected */
591 /*
592 * Normal UDP processing will take place,
593 * m may have changed.
594 */
595 m = *mp;
596 break;
597 }
598 }
599 #endif
600 if (inp->inp_overudp_cb != NULL) {
601 int ret;
602 ret = inp->inp_overudp_cb(mp, off, inp->inp_socket,
603 sintosa(src), inp->inp_overudp_arg);
604 switch (ret) {
605 case -1: /* Error, m was freed */
606 rcvcnt = -1;
607 goto bad;
608
609 case 1: /* Foo over UDP */
610 KASSERT(*mp == NULL);
611 rcvcnt++;
612 goto bad;
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
625 /*
626 * Check the minimum TTL for socket.
627 */
628 if (mtod(m, struct ip *)->ip_ttl < inp->inp_ip_minttl)
629 goto bad;
630
631 udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
632 rcvcnt++;
633 }
634
635 bad:
636 return rcvcnt;
637 }
638 #endif
639
640 #ifdef INET
641 /*
642 * Notify a udp user of an asynchronous error;
643 * just wake up so that he can collect error status.
644 */
645 static void
646 udp_notify(struct inpcb *inp, int errno)
647 {
648 inp->inp_socket->so_error = errno;
649 sorwakeup(inp->inp_socket);
650 sowwakeup(inp->inp_socket);
651 }
652
653 void *
654 udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
655 {
656 struct ip *ip = v;
657 struct udphdr *uh;
658 void (*notify)(struct inpcb *, int) = udp_notify;
659 int errno;
660
661 if (sa->sa_family != AF_INET ||
662 sa->sa_len != sizeof(struct sockaddr_in))
663 return NULL;
664 if ((unsigned)cmd >= PRC_NCMDS)
665 return NULL;
666
667 errno = inetctlerrmap[cmd];
668 if (PRC_IS_REDIRECT(cmd)) {
669 notify = in_rtchange;
670 ip = NULL;
671 } else if (cmd == PRC_HOSTDEAD) {
672 ip = NULL;
673 } else if (errno == 0) {
674 return NULL;
675 }
676
677 if (ip) {
678 uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
679 in_pcbnotify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
680 ip->ip_src, uh->uh_sport, errno, notify);
681 /* XXX mapped address case */
682 } else {
683 in_pcbnotifyall(&udbtable, satocsin(sa)->sin_addr, errno,
684 notify);
685 }
686
687 return NULL;
688 }
689
690 int
691 udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
692 {
693 int s;
694 int error = 0;
695 struct inpcb *inp;
696 int family;
697 int optval;
698
699 family = so->so_proto->pr_domain->dom_family;
700
701 s = splsoftnet();
702 switch (family) {
703 #ifdef INET
704 case PF_INET:
705 if (sopt->sopt_level != IPPROTO_UDP) {
706 error = ip_ctloutput(op, so, sopt);
707 goto end;
708 }
709 break;
710 #endif
711 #ifdef INET6
712 case PF_INET6:
713 if (sopt->sopt_level != IPPROTO_UDP) {
714 error = ip6_ctloutput(op, so, sopt);
715 goto end;
716 }
717 break;
718 #endif
719 default:
720 error = EAFNOSUPPORT;
721 goto end;
722 }
723
724
725 switch (op) {
726 case PRCO_SETOPT:
727 inp = sotoinpcb(so);
728
729 switch (sopt->sopt_name) {
730 case UDP_ENCAP:
731 error = sockopt_getint(sopt, &optval);
732 if (error)
733 break;
734
735 switch(optval) {
736 case 0:
737 inp->inp_flags &= ~INP_ESPINUDP;
738 break;
739
740 case UDP_ENCAP_ESPINUDP:
741 inp->inp_flags |= INP_ESPINUDP;
742 break;
743
744 default:
745 error = EINVAL;
746 break;
747 }
748 break;
749
750 default:
751 error = ENOPROTOOPT;
752 break;
753 }
754 break;
755
756 default:
757 error = EINVAL;
758 break;
759 }
760
761 end:
762 splx(s);
763 return error;
764 }
765
766 int
767 udp_output(struct mbuf *m, struct inpcb *inp, struct mbuf *control,
768 struct lwp *l)
769 {
770 struct udpiphdr *ui;
771 struct route *ro;
772 struct ip_pktopts pktopts;
773 kauth_cred_t cred;
774 int len = m->m_pkthdr.len;
775 int error, flags = 0;
776
777 MCLAIM(m, &udp_tx_mowner);
778
779 /*
780 * Calculate data length and get a mbuf
781 * for UDP and IP headers.
782 */
783 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
784 if (m == NULL) {
785 error = ENOBUFS;
786 goto release;
787 }
788
789 /*
790 * Compute the packet length of the IP header, and
791 * punt if the length looks bogus.
792 */
793 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
794 error = EMSGSIZE;
795 goto release;
796 }
797
798 if (l == NULL)
799 cred = NULL;
800 else
801 cred = l->l_cred;
802
803 /* Setup IP outgoing packet options */
804 memset(&pktopts, 0, sizeof(pktopts));
805 error = ip_setpktopts(control, &pktopts, &flags, inp, cred);
806 if (error != 0)
807 goto release;
808
809 if (control != NULL) {
810 m_freem(control);
811 control = NULL;
812 }
813
814 /*
815 * Fill in mbuf with extended UDP header
816 * and addresses and length put into network format.
817 */
818 ui = mtod(m, struct udpiphdr *);
819 ui->ui_pr = IPPROTO_UDP;
820 ui->ui_src = pktopts.ippo_laddr.sin_addr;
821 ui->ui_dst = inp->inp_faddr;
822 ui->ui_sport = inp->inp_lport;
823 ui->ui_dport = inp->inp_fport;
824 ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
825
826 ro = &inp->inp_route;
827
828 /*
829 * Set up checksum and output datagram.
830 */
831 if (udpcksum) {
832 /*
833 * XXX Cache pseudo-header checksum part for
834 * XXX "connected" UDP sockets.
835 */
836 ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
837 ui->ui_dst.s_addr, htons((u_int16_t)len +
838 sizeof(struct udphdr) + IPPROTO_UDP));
839 m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
840 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
841 } else
842 ui->ui_sum = 0;
843
844 ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
845 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
846 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
847 UDP_STATINC(UDP_STAT_OPACKETS);
848
849 flags |= inp->inp_socket->so_options & (SO_DONTROUTE|SO_BROADCAST);
850 return ip_output(m, inp->inp_options, ro, flags, pktopts.ippo_imo, inp);
851
852 release:
853 if (control != NULL)
854 m_freem(control);
855 m_freem(m);
856 return error;
857 }
858
859 static int
860 udp_attach(struct socket *so, int proto)
861 {
862 struct inpcb *inp;
863 int error;
864
865 KASSERT(sotoinpcb(so) == NULL);
866
867 /* Assign the lock (must happen even if we will error out). */
868 sosetlock(so);
869
870 #ifdef MBUFTRACE
871 so->so_mowner = &udp_mowner;
872 so->so_rcv.sb_mowner = &udp_rx_mowner;
873 so->so_snd.sb_mowner = &udp_tx_mowner;
874 #endif
875 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
876 error = soreserve(so, udp_sendspace, udp_recvspace);
877 if (error) {
878 return error;
879 }
880 }
881
882 error = in_pcballoc(so, &udbtable);
883 if (error) {
884 return error;
885 }
886 inp = sotoinpcb(so);
887 inp->inp_ip.ip_ttl = ip_defttl;
888 KASSERT(solocked(so));
889
890 return error;
891 }
892
893 static void
894 udp_detach(struct socket *so)
895 {
896 struct inpcb *inp;
897
898 KASSERT(solocked(so));
899 inp = sotoinpcb(so);
900 KASSERT(inp != NULL);
901 in_pcbdetach(inp);
902 }
903
904 static int
905 udp_accept(struct socket *so, struct sockaddr *nam)
906 {
907 KASSERT(solocked(so));
908
909 panic("udp_accept");
910
911 return EOPNOTSUPP;
912 }
913
914 static int
915 udp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
916 {
917 struct inpcb *inp = sotoinpcb(so);
918 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
919 int error = 0;
920 int s;
921
922 KASSERT(solocked(so));
923 KASSERT(inp != NULL);
924 KASSERT(nam != NULL);
925
926 s = splsoftnet();
927 error = in_pcbbind(inp, sin, l);
928 splx(s);
929
930 return error;
931 }
932
933 static int
934 udp_listen(struct socket *so, struct lwp *l)
935 {
936 KASSERT(solocked(so));
937
938 return EOPNOTSUPP;
939 }
940
941 static int
942 udp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
943 {
944 struct inpcb *inp = sotoinpcb(so);
945 int error = 0;
946 int s;
947
948 KASSERT(solocked(so));
949 KASSERT(inp != NULL);
950 KASSERT(nam != NULL);
951
952 s = splsoftnet();
953 error = in_pcbconnect(inp, (struct sockaddr_in *)nam, l);
954 if (! error)
955 soisconnected(so);
956 splx(s);
957 return error;
958 }
959
960 static int
961 udp_connect2(struct socket *so, struct socket *so2)
962 {
963 KASSERT(solocked(so));
964
965 return EOPNOTSUPP;
966 }
967
968 static int
969 udp_disconnect(struct socket *so)
970 {
971 struct inpcb *inp = sotoinpcb(so);
972 int s;
973
974 KASSERT(solocked(so));
975 KASSERT(inp != NULL);
976
977 s = splsoftnet();
978 /*soisdisconnected(so);*/
979 so->so_state &= ~SS_ISCONNECTED; /* XXX */
980 in_pcbdisconnect(inp);
981 inp->inp_laddr = zeroin_addr; /* XXX */
982 in_pcbstate(inp, INP_BOUND); /* XXX */
983 splx(s);
984
985 return 0;
986 }
987
988 static int
989 udp_shutdown(struct socket *so)
990 {
991 int s;
992
993 KASSERT(solocked(so));
994
995 s = splsoftnet();
996 socantsendmore(so);
997 splx(s);
998
999 return 0;
1000 }
1001
1002 static int
1003 udp_abort(struct socket *so)
1004 {
1005 KASSERT(solocked(so));
1006
1007 panic("udp_abort");
1008
1009 return EOPNOTSUPP;
1010 }
1011
1012 static int
1013 udp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
1014 {
1015 return in_control(so, cmd, nam, ifp);
1016 }
1017
1018 static int
1019 udp_stat(struct socket *so, struct stat *ub)
1020 {
1021 KASSERT(solocked(so));
1022
1023 /* stat: don't bother with a blocksize. */
1024 return 0;
1025 }
1026
1027 static int
1028 udp_peeraddr(struct socket *so, struct sockaddr *nam)
1029 {
1030 int s;
1031
1032 KASSERT(solocked(so));
1033 KASSERT(sotoinpcb(so) != NULL);
1034 KASSERT(nam != NULL);
1035
1036 s = splsoftnet();
1037 in_setpeeraddr(sotoinpcb(so), (struct sockaddr_in *)nam);
1038 splx(s);
1039
1040 return 0;
1041 }
1042
1043 static int
1044 udp_sockaddr(struct socket *so, struct sockaddr *nam)
1045 {
1046 int s;
1047
1048 KASSERT(solocked(so));
1049 KASSERT(sotoinpcb(so) != NULL);
1050 KASSERT(nam != NULL);
1051
1052 s = splsoftnet();
1053 in_setsockaddr(sotoinpcb(so), (struct sockaddr_in *)nam);
1054 splx(s);
1055
1056 return 0;
1057 }
1058
1059 static int
1060 udp_rcvd(struct socket *so, int flags, struct lwp *l)
1061 {
1062 KASSERT(solocked(so));
1063
1064 return EOPNOTSUPP;
1065 }
1066
1067 static int
1068 udp_recvoob(struct socket *so, struct mbuf *m, int flags)
1069 {
1070 KASSERT(solocked(so));
1071
1072 return EOPNOTSUPP;
1073 }
1074
1075 int
1076 udp_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
1077 struct mbuf *control, struct lwp *l)
1078 {
1079 struct inpcb *inp = sotoinpcb(so);
1080 int error = 0;
1081 struct in_addr laddr; /* XXX */
1082 int s;
1083
1084 KASSERT(solocked(so));
1085 KASSERT(inp != NULL);
1086 KASSERT(m != NULL);
1087
1088 memset(&laddr, 0, sizeof laddr);
1089
1090 s = splsoftnet();
1091 if (nam) {
1092 laddr = inp->inp_laddr; /* XXX */
1093 if ((so->so_state & SS_ISCONNECTED) != 0) {
1094 error = EISCONN;
1095 goto die;
1096 }
1097 error = in_pcbconnect(inp, (struct sockaddr_in *)nam, l);
1098 if (error)
1099 goto die;
1100 } else {
1101 if ((so->so_state & SS_ISCONNECTED) == 0) {
1102 error = ENOTCONN;
1103 goto die;
1104 }
1105 }
1106 error = udp_output(m, inp, control, l);
1107 m = NULL;
1108 control = NULL;
1109 if (nam) {
1110 in_pcbdisconnect(inp);
1111 inp->inp_laddr = laddr; /* XXX */
1112 in_pcbstate(inp, INP_BOUND); /* XXX */
1113 }
1114 die:
1115 if (m != NULL)
1116 m_freem(m);
1117 if (control != NULL)
1118 m_freem(control);
1119
1120 splx(s);
1121 return error;
1122 }
1123
1124 static int
1125 udp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
1126 {
1127 KASSERT(solocked(so));
1128
1129 m_freem(m);
1130 m_freem(control);
1131
1132 return EOPNOTSUPP;
1133 }
1134
1135 static int
1136 udp_purgeif(struct socket *so, struct ifnet *ifp)
1137 {
1138 int s;
1139
1140 s = splsoftnet();
1141 mutex_enter(softnet_lock);
1142 in_pcbpurgeif0(&udbtable, ifp);
1143 #ifdef NET_MPSAFE
1144 mutex_exit(softnet_lock);
1145 #endif
1146 in_purgeif(ifp);
1147 #ifdef NET_MPSAFE
1148 mutex_enter(softnet_lock);
1149 #endif
1150 in_pcbpurgeif(&udbtable, ifp);
1151 mutex_exit(softnet_lock);
1152 splx(s);
1153
1154 return 0;
1155 }
1156
1157 static int
1158 sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
1159 {
1160
1161 return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
1162 }
1163
1164 /*
1165 * Sysctl for udp variables.
1166 */
1167 static void
1168 sysctl_net_inet_udp_setup(struct sysctllog **clog)
1169 {
1170
1171 sysctl_createv(clog, 0, NULL, NULL,
1172 CTLFLAG_PERMANENT,
1173 CTLTYPE_NODE, "inet", NULL,
1174 NULL, 0, NULL, 0,
1175 CTL_NET, PF_INET, CTL_EOL);
1176 sysctl_createv(clog, 0, NULL, NULL,
1177 CTLFLAG_PERMANENT,
1178 CTLTYPE_NODE, "udp",
1179 SYSCTL_DESCR("UDPv4 related settings"),
1180 NULL, 0, NULL, 0,
1181 CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
1182
1183 sysctl_createv(clog, 0, NULL, NULL,
1184 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1185 CTLTYPE_INT, "checksum",
1186 SYSCTL_DESCR("Compute UDP checksums"),
1187 NULL, 0, &udpcksum, 0,
1188 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
1189 CTL_EOL);
1190 sysctl_createv(clog, 0, NULL, NULL,
1191 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1192 CTLTYPE_INT, "sendspace",
1193 SYSCTL_DESCR("Default UDP send buffer size"),
1194 NULL, 0, &udp_sendspace, 0,
1195 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
1196 CTL_EOL);
1197 sysctl_createv(clog, 0, NULL, NULL,
1198 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1199 CTLTYPE_INT, "recvspace",
1200 SYSCTL_DESCR("Default UDP receive buffer size"),
1201 NULL, 0, &udp_recvspace, 0,
1202 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
1203 CTL_EOL);
1204 sysctl_createv(clog, 0, NULL, NULL,
1205 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1206 CTLTYPE_INT, "do_loopback_cksum",
1207 SYSCTL_DESCR("Perform UDP checksum on loopback"),
1208 NULL, 0, &udp_do_loopback_cksum, 0,
1209 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
1210 CTL_EOL);
1211 sysctl_createv(clog, 0, NULL, NULL,
1212 CTLFLAG_PERMANENT,
1213 CTLTYPE_STRUCT, "pcblist",
1214 SYSCTL_DESCR("UDP protocol control block list"),
1215 sysctl_inpcblist, 0, &udbtable, 0,
1216 CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
1217 CTL_EOL);
1218 sysctl_createv(clog, 0, NULL, NULL,
1219 CTLFLAG_PERMANENT,
1220 CTLTYPE_STRUCT, "stats",
1221 SYSCTL_DESCR("UDP statistics"),
1222 sysctl_net_inet_udp_stats, 0, NULL, 0,
1223 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
1224 CTL_EOL);
1225 }
1226 #endif
1227
1228 void
1229 udp_statinc(u_int stat)
1230 {
1231
1232 KASSERT(stat < UDP_NSTATS);
1233 UDP_STATINC(stat);
1234 }
1235
1236 #if defined(INET) && defined(IPSEC)
1237 /*
1238 * Handle ESP-in-UDP packets (RFC3948).
1239 *
1240 * We need to distinguish between ESP packets and IKE packets. We do so by
1241 * looking at the Non-ESP marker. If IKE, we process the UDP packet as usual.
1242 * Otherwise, ESP, we invoke IPsec.
1243 *
1244 * Returns:
1245 * 1 if the packet was processed
1246 * 0 if normal UDP processing should take place
1247 * -1 if an error occurred and m was freed
1248 */
1249 static int
1250 udp4_espinudp(struct mbuf **mp, int off)
1251 {
1252 const size_t skip = sizeof(struct udphdr);
1253 size_t len;
1254 uint8_t *data;
1255 size_t minlen;
1256 size_t iphdrlen;
1257 struct ip *ip;
1258 struct m_tag *tag;
1259 struct udphdr *udphdr;
1260 u_int16_t sport, dport;
1261 struct mbuf *m = *mp;
1262 uint32_t *marker;
1263
1264 minlen = off + sizeof(struct esp);
1265 if (minlen > m->m_pkthdr.len)
1266 minlen = m->m_pkthdr.len;
1267
1268 if (m->m_len < minlen) {
1269 if ((*mp = m_pullup(m, minlen)) == NULL) {
1270 return -1;
1271 }
1272 m = *mp;
1273 }
1274
1275 len = m->m_len - off;
1276 data = mtod(m, uint8_t *) + off;
1277
1278 /* Ignore keepalive packets. */
1279 if ((len == 1) && (*data == 0xff)) {
1280 m_freem(m);
1281 *mp = NULL; /* avoid any further processing by caller */
1282 return 1;
1283 }
1284
1285 /* Handle Non-ESP marker (32bit). If zero, then IKE. */
1286 marker = (uint32_t *)data;
1287 if (len <= sizeof(uint32_t))
1288 return 0;
1289 if (marker[0] == 0)
1290 return 0;
1291
1292 /*
1293 * Get the UDP ports. They are handled in network order
1294 * everywhere in the IPSEC_NAT_T code.
1295 */
1296 udphdr = (struct udphdr *)((char *)data - skip);
1297 sport = udphdr->uh_sport;
1298 dport = udphdr->uh_dport;
1299
1300 /*
1301 * Remove the UDP header, plus a possible marker. IP header
1302 * length is iphdrlen.
1303 *
1304 * Before:
1305 * <--- off --->
1306 * +----+------+-----+
1307 * | IP | UDP | ESP |
1308 * +----+------+-----+
1309 * <-skip->
1310 * After:
1311 * +----+-----+
1312 * | IP | ESP |
1313 * +----+-----+
1314 * <-skip->
1315 */
1316 iphdrlen = off - sizeof(struct udphdr);
1317 memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
1318 m_adj(m, skip);
1319
1320 ip = mtod(m, struct ip *);
1321 ip->ip_len = htons(ntohs(ip->ip_len) - skip);
1322 ip->ip_p = IPPROTO_ESP;
1323
1324 /*
1325 * We have modified the packet - it is now ESP, so we should not
1326 * return to UDP processing.
1327 *
1328 * Add a PACKET_TAG_IPSEC_NAT_T_PORTS tag to remember the source
1329 * UDP port. This is required if we want to select the right SPD
1330 * for multiple hosts behind same NAT.
1331 */
1332 if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
1333 sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
1334 m_freem(m);
1335 return -1;
1336 }
1337 ((u_int16_t *)(tag + 1))[0] = sport;
1338 ((u_int16_t *)(tag + 1))[1] = dport;
1339 m_tag_prepend(m, tag);
1340
1341 if (ipsec_used)
1342 ipsec4_common_input(m, iphdrlen, IPPROTO_ESP);
1343 else
1344 m_freem(m);
1345
1346 /* We handled it, it shouldn't be handled by UDP */
1347 *mp = NULL; /* avoid free by caller ... */
1348 return 1;
1349 }
1350 #endif
1351
1352 PR_WRAP_USRREQS(udp)
1353 #define udp_attach udp_attach_wrapper
1354 #define udp_detach udp_detach_wrapper
1355 #define udp_accept udp_accept_wrapper
1356 #define udp_bind udp_bind_wrapper
1357 #define udp_listen udp_listen_wrapper
1358 #define udp_connect udp_connect_wrapper
1359 #define udp_connect2 udp_connect2_wrapper
1360 #define udp_disconnect udp_disconnect_wrapper
1361 #define udp_shutdown udp_shutdown_wrapper
1362 #define udp_abort udp_abort_wrapper
1363 #define udp_ioctl udp_ioctl_wrapper
1364 #define udp_stat udp_stat_wrapper
1365 #define udp_peeraddr udp_peeraddr_wrapper
1366 #define udp_sockaddr udp_sockaddr_wrapper
1367 #define udp_rcvd udp_rcvd_wrapper
1368 #define udp_recvoob udp_recvoob_wrapper
1369 #define udp_send udp_send_wrapper
1370 #define udp_sendoob udp_sendoob_wrapper
1371 #define udp_purgeif udp_purgeif_wrapper
1372
1373 const struct pr_usrreqs udp_usrreqs = {
1374 .pr_attach = udp_attach,
1375 .pr_detach = udp_detach,
1376 .pr_accept = udp_accept,
1377 .pr_bind = udp_bind,
1378 .pr_listen = udp_listen,
1379 .pr_connect = udp_connect,
1380 .pr_connect2 = udp_connect2,
1381 .pr_disconnect = udp_disconnect,
1382 .pr_shutdown = udp_shutdown,
1383 .pr_abort = udp_abort,
1384 .pr_ioctl = udp_ioctl,
1385 .pr_stat = udp_stat,
1386 .pr_peeraddr = udp_peeraddr,
1387 .pr_sockaddr = udp_sockaddr,
1388 .pr_rcvd = udp_rcvd,
1389 .pr_recvoob = udp_recvoob,
1390 .pr_send = udp_send,
1391 .pr_sendoob = udp_sendoob,
1392 .pr_purgeif = udp_purgeif,
1393 };
1394