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