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