Annotation of src/sys/netinet/udp_usrreq.c, Revision 1.219
1.219 ! rtr 1: /* $NetBSD: udp_usrreq.c,v 1.218 2015/04/03 20:01:07 rtr Exp $ */
1.48 itojun 2:
3: /*
4: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5: * All rights reserved.
1.94 itojun 6: *
1.48 itojun 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.
1.94 itojun 18: *
1.48 itojun 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: */
1.14 cgd 31:
1.1 cgd 32: /*
1.44 thorpej 33: * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
1.13 mycroft 34: * The Regents of the University of California. All rights reserved.
1.1 cgd 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.
1.104 agc 44: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 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: *
1.44 thorpej 60: * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
1.1 cgd 61: */
1.91 lukem 62:
1.198 rmind 63: /*
64: * UDP protocol implementation.
65: * Per RFC 768, August, 1980.
66: */
67:
1.91 lukem 68: #include <sys/cdefs.h>
1.219 ! rtr 69: __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.218 2015/04/03 20:01:07 rtr Exp $");
1.50 thorpej 70:
1.77 soda 71: #include "opt_inet.h"
1.174 christos 72: #include "opt_compat_netbsd.h"
1.50 thorpej 73: #include "opt_ipsec.h"
1.78 thorpej 74: #include "opt_inet_csum.h"
1.64 ws 75: #include "opt_ipkdb.h"
1.101 martin 76: #include "opt_mbuftrace.h"
1.1 cgd 77:
1.5 mycroft 78: #include <sys/param.h>
79: #include <sys/mbuf.h>
1.192 pooka 80: #include <sys/once.h>
1.5 mycroft 81: #include <sys/protosw.h>
82: #include <sys/socket.h>
83: #include <sys/socketvar.h>
1.27 christos 84: #include <sys/systm.h>
85: #include <sys/proc.h>
1.53 itojun 86: #include <sys/domain.h>
1.27 christos 87: #include <sys/sysctl.h>
1.1 cgd 88:
1.5 mycroft 89: #include <net/if.h>
90: #include <net/route.h>
1.1 cgd 91:
1.5 mycroft 92: #include <netinet/in.h>
93: #include <netinet/in_systm.h>
1.15 cgd 94: #include <netinet/in_var.h>
1.5 mycroft 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>
1.166 thorpej 101: #include <netinet/udp_private.h>
1.1 cgd 102:
1.53 itojun 103: #ifdef INET6
104: #include <netinet/ip6.h>
105: #include <netinet/icmp6.h>
106: #include <netinet6/ip6_var.h>
1.167 thorpej 107: #include <netinet6/ip6_private.h>
1.53 itojun 108: #include <netinet6/in6_pcb.h>
109: #include <netinet6/udp6_var.h>
1.168 thorpej 110: #include <netinet6/udp6_private.h>
1.53 itojun 111: #endif
112:
113: #ifndef INET6
114: /* always need ip6.h for IP6_EXTHDR_GET */
115: #include <netinet/ip6.h>
116: #endif
117:
1.190 christos 118: #ifdef IPSEC
1.105 jonathan 119: #include <netipsec/ipsec.h>
1.169 thorpej 120: #include <netipsec/ipsec_var.h>
121: #include <netipsec/ipsec_private.h>
1.147 christos 122: #include <netipsec/esp.h>
1.105 jonathan 123: #ifdef INET6
124: #include <netipsec/ipsec6.h>
125: #endif
1.190 christos 126: #endif /* IPSEC */
1.105 jonathan 127:
1.174 christos 128: #ifdef COMPAT_50
129: #include <compat/sys/socket.h>
130: #endif
131:
1.64 ws 132: #ifdef IPKDB
133: #include <ipkdb/ipkdb.h>
134: #endif
135:
1.8 mycroft 136: int udpcksum = 1;
1.141 yamt 137: int udp_do_loopback_cksum = 0;
1.93 matt 138:
139: struct inpcbtable udbtable;
1.164 thorpej 140:
1.166 thorpej 141: percpu_t *udpstat_percpu;
1.8 mycroft 142:
1.72 itojun 143: #ifdef INET
1.190 christos 144: #ifdef IPSEC
1.144 manu 145: static int udp4_espinudp (struct mbuf **, int, struct sockaddr *,
1.130 manu 146: struct socket *);
1.189 christos 147: #endif
1.119 matt 148: static void udp4_sendup (struct mbuf *, int, struct sockaddr *,
149: struct socket *);
150: static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
1.144 manu 151: struct mbuf **, int);
1.129 yamt 152: static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
1.72 itojun 153: #endif
154: #ifdef INET
1.119 matt 155: static void udp_notify (struct inpcb *, int);
1.72 itojun 156: #endif
1.7 mycroft 157:
1.26 mycroft 158: #ifndef UDBHASHSIZE
159: #define UDBHASHSIZE 128
160: #endif
161: int udbhashsize = UDBHASHSIZE;
162:
1.196 rmind 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:
1.98 matt 169: #ifdef MBUFTRACE
1.150 dogcow 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");
1.98 matt 173: #endif
174:
1.78 thorpej 175: #ifdef UDP_CSUM_COUNTERS
176: #include <sys/device.h>
177:
1.140 yamt 178: #if defined(INET)
1.78 thorpej 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:
1.120 matt 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);
1.140 yamt 192: #endif /* defined(INET) */
193:
194: #define UDP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
1.78 thorpej 195: #else
196: #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */
197: #endif /* UDP_CSUM_COUNTERS */
198:
1.179 pooka 199: static void sysctl_net_inet_udp_setup(struct sysctllog **);
200:
1.192 pooka 201: static int
202: do_udpinit(void)
1.1 cgd 203: {
1.18 mycroft 204:
1.35 mycroft 205: in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.193 pooka 206: udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS);
1.78 thorpej 207:
1.98 matt 208: MOWNER_ATTACH(&udp_tx_mowner);
209: MOWNER_ATTACH(&udp_rx_mowner);
210: MOWNER_ATTACH(&udp_mowner);
1.166 thorpej 211:
1.192 pooka 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();
1.1 cgd 230: }
231:
1.129 yamt 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) {
1.72 itojun 242: #ifdef INET
1.129 yamt 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:
1.166 thorpej 324: UDP_STATINC(UDP_STAT_BADSUM);
1.129 yamt 325: return -1;
326: }
327:
1.7 mycroft 328: void
1.27 christos 329: udp_input(struct mbuf *m, ...)
1.1 cgd 330: {
1.53 itojun 331: va_list ap;
332: struct sockaddr_in src, dst;
333: struct ip *ip;
334: struct udphdr *uh;
1.97 simonb 335: int iphlen;
1.53 itojun 336: int len;
337: int n;
1.96 itojun 338: u_int16_t ip_len;
1.53 itojun 339:
340: va_start(ap, m);
341: iphlen = va_arg(ap, int);
1.97 simonb 342: (void)va_arg(ap, int); /* ignore value, advance ap */
1.53 itojun 343: va_end(ap);
344:
1.98 matt 345: MCLAIM(m, &udp_rx_mowner);
1.166 thorpej 346: UDP_STATINC(UDP_STAT_IPACKETS);
1.53 itojun 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) {
1.166 thorpej 354: UDP_STATINC(UDP_STAT_HDROPS);
1.53 itojun 355: return;
356: }
1.95 thorpej 357: KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53 itojun 358:
1.57 itojun 359: /* destination port of 0 is illegal, based on RFC768. */
360: if (uh->uh_dport == 0)
361: goto bad;
362:
1.53 itojun 363: /*
364: * Make mbuf data length reflect UDP length.
365: * If not enough data to reflect UDP length, drop.
366: */
1.96 itojun 367: ip_len = ntohs(ip->ip_len);
1.53 itojun 368: len = ntohs((u_int16_t)uh->uh_ulen);
1.96 itojun 369: if (ip_len != iphlen + len) {
370: if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
1.166 thorpej 371: UDP_STATINC(UDP_STAT_BADLEN);
1.53 itojun 372: goto bad;
373: }
1.96 itojun 374: m_adj(m, iphlen + len - ip_len);
1.53 itojun 375: }
376:
377: /*
378: * Checksum extended UDP header and data.
379: */
1.129 yamt 380: if (udp4_input_checksum(m, uh, iphlen, len))
381: goto badcsum;
1.53 itojun 382:
383: /* construct source and dst sockaddrs. */
1.159 dyoung 384: sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport);
385: sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport);
1.53 itojun 386:
1.144 manu 387: if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
1.166 thorpej 388: UDP_STATINC(UDP_STAT_HDROPS);
1.144 manu 389: return;
390: }
1.188 christos 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 *);
1.53 itojun 399: #ifdef INET6
400: if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
401: struct sockaddr_in6 src6, dst6;
402:
1.175 cegger 403: memset(&src6, 0, sizeof(src6));
1.53 itojun 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;
1.177 tsutsui 407: memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src,
1.53 itojun 408: sizeof(ip->ip_src));
409: src6.sin6_port = uh->uh_sport;
1.175 cegger 410: memset(&dst6, 0, sizeof(dst6));
1.53 itojun 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;
1.177 tsutsui 414: memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst,
1.53 itojun 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)) {
1.166 thorpej 424: UDP_STATINC(UDP_STAT_NOPORTBCAST);
1.53 itojun 425: goto bad;
426: }
1.166 thorpej 427: UDP_STATINC(UDP_STAT_NOPORT);
1.64 ws 428: #ifdef IPKDB
1.53 itojun 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);
1.78 thorpej 446: return;
447:
448: badcsum:
449: m_freem(m);
1.53 itojun 450: }
1.72 itojun 451: #endif
1.53 itojun 452:
1.72 itojun 453: #ifdef INET
1.53 itojun 454: static void
1.119 matt 455: udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
456: struct sockaddr *src, struct socket *so)
1.53 itojun 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:
1.190 christos 476: #if defined(IPSEC)
1.53 itojun 477: /* check AH/ESP integrity. */
1.199 christos 478: if (ipsec_used && so != NULL && ipsec4_in_reject_so(m, so)) {
1.169 thorpej 479: IPSEC_STATINC(IPSEC_STAT_IN_POLVIO);
1.162 dyoung 480: if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
1.110 itojun 481: icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
482: 0, 0);
1.53 itojun 483: return;
484: }
485: #endif /*IPSEC*/
486:
1.162 dyoung 487: if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
1.53 itojun 488: if (inp && (inp->inp_flags & INP_CONTROLOPTS
1.174 christos 489: #ifdef SO_OTIMESTAMP
490: || so->so_options & SO_OTIMESTAMP
491: #endif
1.53 itojun 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);
1.124 darrenr 503: so->so_rcv.sb_overflowed++;
1.166 thorpej 504: UDP_STATINC(UDP_STAT_FULLSOCK);
1.53 itojun 505: } else
506: sorwakeup(so);
507: }
508: }
1.72 itojun 509: #endif
1.53 itojun 510:
1.72 itojun 511: #ifdef INET
1.53 itojun 512: static int
1.119 matt 513: udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
1.144 manu 514: struct mbuf **mp, int off /* offset of udphdr */)
1.53 itojun 515: {
516: u_int16_t *sport, *dport;
517: int rcvcnt;
518: struct in_addr *src4, *dst4;
1.109 itojun 519: struct inpcb_hdr *inph;
1.53 itojun 520: struct inpcb *inp;
1.144 manu 521: struct mbuf *m = *mp;
1.53 itojun 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:
1.73 itojun 534: if (IN_MULTICAST(dst4->s_addr) ||
1.53 itojun 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: /*
1.92 itojun 553: * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71 itojun 554: * we need udpiphdr for IPsec processing so we do that later.
1.53 itojun 555: */
556: /*
557: * Locate pcb(s) for datagram.
558: */
1.191 christos 559: TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
1.109 itojun 560: inp = (struct inpcb *)inph;
561: if (inp->inp_af != AF_INET)
562: continue;
563:
1.53 itojun 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: */
1.180 dyoung 596: inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4,
597: *dport, 0);
1.53 itojun 598: if (inp == 0) {
1.166 thorpej 599: UDP_STATINC(UDP_STAT_PCBHASHMISS);
1.53 itojun 600: inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
1.82 itojun 601: if (inp == 0)
1.53 itojun 602: return rcvcnt;
603: }
604:
1.190 christos 605: #ifdef IPSEC
1.130 manu 606: /* Handle ESP over UDP */
607: if (inp->inp_flags & INP_ESPINUDP_ALL) {
608: struct sockaddr *sa = (struct sockaddr *)src;
609:
1.144 manu 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 */
1.130 manu 617: rcvcnt++;
618: goto bad;
1.144 manu 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;
1.130 manu 629: }
630: }
1.189 christos 631: #endif
1.130 manu 632:
1.178 minskim 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:
1.53 itojun 639: udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
640: rcvcnt++;
641: }
642:
643: bad:
644: return rcvcnt;
645: }
1.72 itojun 646: #endif
1.53 itojun 647:
1.72 itojun 648: #ifdef INET
1.1 cgd 649: /*
650: * Notify a udp user of an asynchronous error;
651: * just wake up so that he can collect error status.
652: */
1.7 mycroft 653: static void
1.119 matt 654: udp_notify(struct inpcb *inp, int errno)
1.1 cgd 655: {
656: inp->inp_socket->so_error = errno;
657: sorwakeup(inp->inp_socket);
658: sowwakeup(inp->inp_socket);
659: }
660:
1.27 christos 661: void *
1.157 dyoung 662: udp_ctlinput(int cmd, const struct sockaddr *sa, void *v)
1.1 cgd 663: {
1.66 augustss 664: struct ip *ip = v;
665: struct udphdr *uh;
1.119 matt 666: void (*notify)(struct inpcb *, int) = udp_notify;
1.21 mycroft 667: int errno;
1.1 cgd 668:
1.53 itojun 669: if (sa->sa_family != AF_INET
670: || sa->sa_len != sizeof(struct sockaddr_in))
1.51 itojun 671: return NULL;
1.20 mycroft 672: if ((unsigned)cmd >= PRC_NCMDS)
1.27 christos 673: return NULL;
1.20 mycroft 674: errno = inetctlerrmap[cmd];
1.18 mycroft 675: if (PRC_IS_REDIRECT(cmd))
1.19 mycroft 676: notify = in_rtchange, ip = 0;
1.18 mycroft 677: else if (cmd == PRC_HOSTDEAD)
1.19 mycroft 678: ip = 0;
1.23 cgd 679: else if (errno == 0)
1.27 christos 680: return NULL;
1.19 mycroft 681: if (ip) {
1.158 christos 682: uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2));
1.157 dyoung 683: in_pcbnotify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport,
1.34 mycroft 684: ip->ip_src, uh->uh_sport, errno, notify);
1.53 itojun 685:
686: /* XXX mapped address case */
1.19 mycroft 687: } else
1.157 dyoung 688: in_pcbnotifyall(&udbtable, satocsin(sa)->sin_addr, errno,
1.34 mycroft 689: notify);
1.27 christos 690: return NULL;
1.1 cgd 691: }
692:
1.7 mycroft 693: int
1.173 plunky 694: udp_ctloutput(int op, struct socket *so, struct sockopt *sopt)
1.130 manu 695: {
696: int s;
697: int error = 0;
698: struct inpcb *inp;
699: int family;
1.173 plunky 700: int optval;
1.130 manu 701:
702: family = so->so_proto->pr_domain->dom_family;
703:
704: s = splsoftnet();
705: switch (family) {
706: #ifdef INET
707: case PF_INET:
1.173 plunky 708: if (sopt->sopt_level != IPPROTO_UDP) {
709: error = ip_ctloutput(op, so, sopt);
1.130 manu 710: goto end;
711: }
712: break;
713: #endif
714: #ifdef INET6
715: case PF_INET6:
1.173 plunky 716: if (sopt->sopt_level != IPPROTO_UDP) {
717: error = ip6_ctloutput(op, so, sopt);
1.130 manu 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:
1.173 plunky 732: switch (sopt->sopt_name) {
1.130 manu 733: case UDP_ENCAP:
1.173 plunky 734: error = sockopt_getint(sopt, &optval);
735: if (error)
1.153 yamt 736: break;
1.131 perry 737:
1.173 plunky 738: switch(optval) {
1.130 manu 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;
1.131 perry 747:
1.130 manu 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;
1.183 christos 757:
1.130 manu 758: default:
759: error = ENOPROTOOPT;
760: break;
761: }
762: break;
763:
764: default:
765: error = EINVAL;
766: break;
1.131 perry 767: }
768:
1.130 manu 769: end:
770: splx(s);
771: return error;
772: }
1.131 perry 773:
1.130 manu 774:
775: int
1.27 christos 776: udp_output(struct mbuf *m, ...)
777: {
1.66 augustss 778: struct inpcb *inp;
779: struct udpiphdr *ui;
1.125 thorpej 780: struct route *ro;
1.66 augustss 781: int len = m->m_pkthdr.len;
1.31 mycroft 782: int error = 0;
1.27 christos 783: va_list ap;
784:
1.98 matt 785: MCLAIM(m, &udp_tx_mowner);
1.27 christos 786: va_start(ap, m);
787: inp = va_arg(ap, struct inpcb *);
788: va_end(ap);
1.1 cgd 789:
790: /*
791: * Calculate data length and get a mbuf
792: * for UDP and IP headers.
793: */
1.13 mycroft 794: M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
795: if (m == 0) {
796: error = ENOBUFS;
1.39 thorpej 797: goto release;
798: }
799:
800: /*
801: * Compute the packet length of the IP header, and
802: * punt if the length looks bogus.
803: */
1.96 itojun 804: if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1.39 thorpej 805: error = EMSGSIZE;
1.13 mycroft 806: goto release;
807: }
1.1 cgd 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;
1.78 thorpej 819: ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
1.1 cgd 820:
1.125 thorpej 821: ro = &inp->inp_route;
822:
1.1 cgd 823: /*
1.78 thorpej 824: * Set up checksum and output datagram.
1.1 cgd 825: */
826: if (udpcksum) {
1.78 thorpej 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));
1.135 yamt 834: m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
1.78 thorpej 835: m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
836: } else
837: ui->ui_sum = 0;
1.96 itojun 838: ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
1.1 cgd 839: ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
840: ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
1.166 thorpej 841: UDP_STATINC(UDP_STAT_OPACKETS);
1.48 itojun 842:
1.125 thorpej 843: return (ip_output(m, inp->inp_options, ro,
1.12 mycroft 844: inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.107 itojun 845: inp->inp_moptions, inp->inp_socket));
1.1 cgd 846:
847: release:
848: m_freem(m);
849: return (error);
850: }
851:
1.196 rmind 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: }
1.1 cgd 896:
1.195 rmind 897: static int
1.219 ! rtr 898: udp_accept(struct socket *so, struct sockaddr *nam)
1.208 rtr 899: {
900: KASSERT(solocked(so));
901:
902: panic("udp_accept");
1.210 rtr 903:
1.208 rtr 904: return EOPNOTSUPP;
905: }
906:
907: static int
1.218 rtr 908: udp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
1.210 rtr 909: {
910: struct inpcb *inp = sotoinpcb(so);
1.218 rtr 911: struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1.210 rtr 912: int error = 0;
913: int s;
914:
915: KASSERT(solocked(so));
916: KASSERT(inp != NULL);
917: KASSERT(nam != NULL);
918:
919: s = splsoftnet();
1.218 rtr 920: error = in_pcbbind(inp, sin, l);
1.210 rtr 921: splx(s);
922:
923: return error;
924: }
925:
926: static int
1.214 rtr 927: udp_listen(struct socket *so, struct lwp *l)
1.210 rtr 928: {
929: KASSERT(solocked(so));
930:
931: return EOPNOTSUPP;
932: }
933:
1.211 rtr 934: static int
1.214 rtr 935: udp_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
1.211 rtr 936: {
937: struct inpcb *inp = sotoinpcb(so);
938: int error = 0;
939: int s;
940:
941: KASSERT(solocked(so));
942: KASSERT(inp != NULL);
943: KASSERT(nam != NULL);
944:
945: s = splsoftnet();
1.214 rtr 946: error = in_pcbconnect(inp, nam, l);
1.211 rtr 947: if (! error)
948: soisconnected(so);
949: splx(s);
950: return error;
951: }
1.210 rtr 952:
953: static int
1.217 rtr 954: udp_connect2(struct socket *so, struct socket *so2)
955: {
956: KASSERT(solocked(so));
957:
958: return EOPNOTSUPP;
959: }
960:
961: static int
1.212 rtr 962: udp_disconnect(struct socket *so)
963: {
964: struct inpcb *inp = sotoinpcb(so);
965: int s;
966:
967: KASSERT(solocked(so));
968: KASSERT(inp != NULL);
969:
970: s = splsoftnet();
971: /*soisdisconnected(so);*/
972: so->so_state &= ~SS_ISCONNECTED; /* XXX */
973: in_pcbdisconnect(inp);
974: inp->inp_laddr = zeroin_addr; /* XXX */
975: in_pcbstate(inp, INP_BOUND); /* XXX */
976: splx(s);
977:
978: return 0;
979: }
980:
981: static int
982: udp_shutdown(struct socket *so)
983: {
984: int s;
985:
986: KASSERT(solocked(so));
987:
988: s = splsoftnet();
989: socantsendmore(so);
990: splx(s);
991:
992: return 0;
993: }
994:
995: static int
996: udp_abort(struct socket *so)
997: {
998: KASSERT(solocked(so));
999:
1000: panic("udp_abort");
1001:
1002: return EOPNOTSUPP;
1003: }
1004:
1005: static int
1.202 rtr 1006: udp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
1.200 rtr 1007: {
1.202 rtr 1008: return in_control(so, cmd, nam, ifp);
1.200 rtr 1009: }
1010:
1011: static int
1.203 rtr 1012: udp_stat(struct socket *so, struct stat *ub)
1013: {
1.206 rtr 1014: KASSERT(solocked(so));
1015:
1.205 rtr 1016: /* stat: don't bother with a blocksize. */
1017: return 0;
1.203 rtr 1018: }
1019:
1020: static int
1.219 ! rtr 1021: udp_peeraddr(struct socket *so, struct sockaddr *nam)
1.207 rtr 1022: {
1.213 rtr 1023: int s;
1024:
1.207 rtr 1025: KASSERT(solocked(so));
1026: KASSERT(sotoinpcb(so) != NULL);
1027: KASSERT(nam != NULL);
1028:
1.213 rtr 1029: s = splsoftnet();
1.219 ! rtr 1030: in_setpeeraddr(sotoinpcb(so), (struct sockaddr_in *)nam);
1.213 rtr 1031: splx(s);
1032:
1.207 rtr 1033: return 0;
1034: }
1035:
1036: static int
1.219 ! rtr 1037: udp_sockaddr(struct socket *so, struct sockaddr *nam)
1.207 rtr 1038: {
1.213 rtr 1039: int s;
1040:
1.207 rtr 1041: KASSERT(solocked(so));
1042: KASSERT(sotoinpcb(so) != NULL);
1043: KASSERT(nam != NULL);
1044:
1.213 rtr 1045: s = splsoftnet();
1.219 ! rtr 1046: in_setsockaddr(sotoinpcb(so), (struct sockaddr_in *)nam);
1.213 rtr 1047: splx(s);
1048:
1.207 rtr 1049: return 0;
1050: }
1051:
1052: static int
1.216 rtr 1053: udp_rcvd(struct socket *so, int flags, struct lwp *l)
1054: {
1055: KASSERT(solocked(so));
1056:
1057: return EOPNOTSUPP;
1058: }
1059:
1060: static int
1.209 rtr 1061: udp_recvoob(struct socket *so, struct mbuf *m, int flags)
1062: {
1063: KASSERT(solocked(so));
1064:
1065: return EOPNOTSUPP;
1066: }
1067:
1068: static int
1.215 rtr 1069: udp_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
1070: struct mbuf *control, struct lwp *l)
1071: {
1072: struct inpcb *inp = sotoinpcb(so);
1073: int error = 0;
1074: struct in_addr laddr; /* XXX */
1075: int s;
1076:
1077: KASSERT(solocked(so));
1078: KASSERT(inp != NULL);
1079: KASSERT(m != NULL);
1080:
1081: if (control && control->m_len) {
1082: m_freem(control);
1083: m_freem(m);
1084: return EINVAL;
1085: }
1086:
1087: memset(&laddr, 0, sizeof laddr);
1088:
1089: s = splsoftnet();
1090: if (nam) {
1091: laddr = inp->inp_laddr; /* XXX */
1092: if ((so->so_state & SS_ISCONNECTED) != 0) {
1093: error = EISCONN;
1094: goto die;
1095: }
1096: error = in_pcbconnect(inp, nam, l);
1097: if (error)
1098: goto die;
1099: } else {
1100: if ((so->so_state & SS_ISCONNECTED) == 0) {
1101: error = ENOTCONN;
1102: goto die;
1103: }
1104: }
1105: error = udp_output(m, inp);
1106: m = NULL;
1107: if (nam) {
1108: in_pcbdisconnect(inp);
1109: inp->inp_laddr = laddr; /* XXX */
1110: in_pcbstate(inp, INP_BOUND); /* XXX */
1111: }
1112: die:
1113: if (m)
1114: m_freem(m);
1115:
1116: splx(s);
1117: return error;
1118: }
1119:
1120: static int
1.209 rtr 1121: udp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
1122: {
1123: KASSERT(solocked(so));
1124:
1125: m_freem(m);
1126: m_freem(control);
1127:
1128: return EOPNOTSUPP;
1129: }
1130:
1131: static int
1.217 rtr 1132: udp_purgeif(struct socket *so, struct ifnet *ifp)
1133: {
1134: int s;
1135:
1136: s = splsoftnet();
1137: mutex_enter(softnet_lock);
1138: in_pcbpurgeif0(&udbtable, ifp);
1139: in_purgeif(ifp);
1140: in_pcbpurgeif(&udbtable, ifp);
1141: mutex_exit(softnet_lock);
1142: splx(s);
1143:
1144: return 0;
1145: }
1146:
1147: static int
1.119 matt 1148: udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
1.195 rmind 1149: struct mbuf *control, struct lwp *l)
1.1 cgd 1150: {
1.196 rmind 1151: KASSERT(req != PRU_ATTACH);
1152: KASSERT(req != PRU_DETACH);
1.208 rtr 1153: KASSERT(req != PRU_ACCEPT);
1.210 rtr 1154: KASSERT(req != PRU_BIND);
1155: KASSERT(req != PRU_LISTEN);
1.211 rtr 1156: KASSERT(req != PRU_CONNECT);
1.217 rtr 1157: KASSERT(req != PRU_CONNECT2);
1.212 rtr 1158: KASSERT(req != PRU_DISCONNECT);
1159: KASSERT(req != PRU_SHUTDOWN);
1160: KASSERT(req != PRU_ABORT);
1.200 rtr 1161: KASSERT(req != PRU_CONTROL);
1.203 rtr 1162: KASSERT(req != PRU_SENSE);
1.207 rtr 1163: KASSERT(req != PRU_PEERADDR);
1164: KASSERT(req != PRU_SOCKADDR);
1.216 rtr 1165: KASSERT(req != PRU_RCVD);
1.209 rtr 1166: KASSERT(req != PRU_RCVOOB);
1.215 rtr 1167: KASSERT(req != PRU_SEND);
1.209 rtr 1168: KASSERT(req != PRU_SENDOOB);
1.217 rtr 1169: KASSERT(req != PRU_PURGEIF);
1.31 mycroft 1170:
1.196 rmind 1171: KASSERT(solocked(so));
1172:
1.217 rtr 1173: if (sotoinpcb(so) == NULL)
1.196 rmind 1174: return EINVAL;
1.31 mycroft 1175:
1.217 rtr 1176: panic("udp_usrreq");
1.1 cgd 1177:
1.217 rtr 1178: return 0;
1.13 mycroft 1179: }
1180:
1.166 thorpej 1181: static int
1182: sysctl_net_inet_udp_stats(SYSCTLFN_ARGS)
1183: {
1184:
1.172 thorpej 1185: return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS));
1.166 thorpej 1186: }
1187:
1.13 mycroft 1188: /*
1189: * Sysctl for udp variables.
1190: */
1.179 pooka 1191: static void
1192: sysctl_net_inet_udp_setup(struct sysctllog **clog)
1.13 mycroft 1193: {
1.194 pooka 1194:
1.116 atatat 1195: sysctl_createv(clog, 0, NULL, NULL,
1196: CTLFLAG_PERMANENT,
1.114 atatat 1197: CTLTYPE_NODE, "inet", NULL,
1198: NULL, 0, NULL, 0,
1199: CTL_NET, PF_INET, CTL_EOL);
1.116 atatat 1200: sysctl_createv(clog, 0, NULL, NULL,
1201: CTLFLAG_PERMANENT,
1.122 atatat 1202: CTLTYPE_NODE, "udp",
1203: SYSCTL_DESCR("UDPv4 related settings"),
1.114 atatat 1204: NULL, 0, NULL, 0,
1205: CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
1206:
1.116 atatat 1207: sysctl_createv(clog, 0, NULL, NULL,
1208: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122 atatat 1209: CTLTYPE_INT, "checksum",
1.123 heas 1210: SYSCTL_DESCR("Compute UDP checksums"),
1.114 atatat 1211: NULL, 0, &udpcksum, 0,
1212: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
1213: CTL_EOL);
1.116 atatat 1214: sysctl_createv(clog, 0, NULL, NULL,
1215: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122 atatat 1216: CTLTYPE_INT, "sendspace",
1217: SYSCTL_DESCR("Default UDP send buffer size"),
1.114 atatat 1218: NULL, 0, &udp_sendspace, 0,
1219: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
1220: CTL_EOL);
1.116 atatat 1221: sysctl_createv(clog, 0, NULL, NULL,
1222: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122 atatat 1223: CTLTYPE_INT, "recvspace",
1224: SYSCTL_DESCR("Default UDP receive buffer size"),
1.114 atatat 1225: NULL, 0, &udp_recvspace, 0,
1226: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
1227: CTL_EOL);
1.125 thorpej 1228: sysctl_createv(clog, 0, NULL, NULL,
1229: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1230: CTLTYPE_INT, "do_loopback_cksum",
1231: SYSCTL_DESCR("Perform UDP checksum on loopback"),
1232: NULL, 0, &udp_do_loopback_cksum, 0,
1233: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
1234: CTL_EOL);
1.132 atatat 1235: sysctl_createv(clog, 0, NULL, NULL,
1236: CTLFLAG_PERMANENT,
1.134 atatat 1237: CTLTYPE_STRUCT, "pcblist",
1.132 atatat 1238: SYSCTL_DESCR("UDP protocol control block list"),
1239: sysctl_inpcblist, 0, &udbtable, 0,
1240: CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
1241: CTL_EOL);
1.139 elad 1242: sysctl_createv(clog, 0, NULL, NULL,
1243: CTLFLAG_PERMANENT,
1244: CTLTYPE_STRUCT, "stats",
1245: SYSCTL_DESCR("UDP statistics"),
1.166 thorpej 1246: sysctl_net_inet_udp_stats, 0, NULL, 0,
1.139 elad 1247: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
1248: CTL_EOL);
1.1 cgd 1249: }
1.72 itojun 1250: #endif
1.130 manu 1251:
1.166 thorpej 1252: void
1253: udp_statinc(u_int stat)
1254: {
1255:
1256: KASSERT(stat < UDP_NSTATS);
1257: UDP_STATINC(stat);
1258: }
1259:
1.190 christos 1260: #if defined(INET) && defined(IPSEC)
1.130 manu 1261: /*
1262: * Returns:
1263: * 1 if the packet was processed
1264: * 0 if normal UDP processing should take place
1.144 manu 1265: * -1 if an error occurent and m was freed
1.130 manu 1266: */
1267: static int
1.154 yamt 1268: udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src,
1.151 christos 1269: struct socket *so)
1.130 manu 1270: {
1271: size_t len;
1.158 christos 1272: void *data;
1.130 manu 1273: struct inpcb *inp;
1274: size_t skip = 0;
1275: size_t minlen;
1276: size_t iphdrlen;
1277: struct ip *ip;
1.136 manu 1278: struct m_tag *tag;
1279: struct udphdr *udphdr;
1280: u_int16_t sport, dport;
1.144 manu 1281: struct mbuf *m = *mp;
1.130 manu 1282:
1.131 perry 1283: /*
1.130 manu 1284: * Collapse the mbuf chain if the first mbuf is too short
1285: * The longest case is: UDP + non ESP marker + ESP
1286: */
1287: minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
1288: if (minlen > m->m_pkthdr.len)
1289: minlen = m->m_pkthdr.len;
1290:
1291: if (m->m_len < minlen) {
1.144 manu 1292: if ((*mp = m_pullup(m, minlen)) == NULL) {
1.130 manu 1293: printf("udp4_espinudp: m_pullup failed\n");
1.144 manu 1294: return -1;
1.130 manu 1295: }
1.144 manu 1296: m = *mp;
1.130 manu 1297: }
1298:
1.131 perry 1299: len = m->m_len - off;
1.158 christos 1300: data = mtod(m, char *) + off;
1.130 manu 1301: inp = sotoinpcb(so);
1302:
1303: /* Ignore keepalive packets */
1.158 christos 1304: if ((len == 1) && (*(unsigned char *)data == 0xff)) {
1.188 christos 1305: m_free(m);
1306: *mp = NULL; /* avoid any further processiong by caller ... */
1.130 manu 1307: return 1;
1308: }
1309:
1.131 perry 1310: /*
1311: * Check that the payload is long enough to hold
1.130 manu 1312: * an ESP header and compute the length of encapsulation
1.131 perry 1313: * header to remove
1.130 manu 1314: */
1315: if (inp->inp_flags & INP_ESPINUDP) {
1316: u_int32_t *st = (u_int32_t *)data;
1317:
1318: if ((len <= sizeof(struct esp)) || (*st == 0))
1319: return 0; /* Normal UDP processing */
1320:
1321: skip = sizeof(struct udphdr);
1322: }
1323:
1324: if (inp->inp_flags & INP_ESPINUDP_NON_IKE) {
1.142 kleink 1325: u_int32_t *st = (u_int32_t *)data;
1.130 manu 1326:
1327: if ((len <= sizeof(u_int64_t) + sizeof(struct esp))
1.142 kleink 1328: || ((st[0] | st[1]) != 0))
1.130 manu 1329: return 0; /* Normal UDP processing */
1.131 perry 1330:
1.130 manu 1331: skip = sizeof(struct udphdr) + sizeof(u_int64_t);
1332: }
1333:
1334: /*
1.136 manu 1335: * Get the UDP ports. They are handled in network
1336: * order everywhere in IPSEC_NAT_T code.
1337: */
1.158 christos 1338: udphdr = (struct udphdr *)((char *)data - skip);
1.136 manu 1339: sport = udphdr->uh_sport;
1340: dport = udphdr->uh_dport;
1341:
1342: /*
1.130 manu 1343: * Remove the UDP header (and possibly the non ESP marker)
1344: * IP header lendth is iphdrlen
1.131 perry 1345: * Before:
1.130 manu 1346: * <--- off --->
1347: * +----+------+-----+
1348: * | IP | UDP | ESP |
1349: * +----+------+-----+
1350: * <-skip->
1351: * After:
1352: * +----+-----+
1353: * | IP | ESP |
1354: * +----+-----+
1355: * <-skip->
1356: */
1357: iphdrlen = off - sizeof(struct udphdr);
1.158 christos 1358: memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen);
1.130 manu 1359: m_adj(m, skip);
1360:
1361: ip = mtod(m, struct ip *);
1362: ip->ip_len = htons(ntohs(ip->ip_len) - skip);
1363: ip->ip_p = IPPROTO_ESP;
1364:
1365: /*
1.188 christos 1366: * We have modified the packet - it is now ESP, so we should not
1367: * return to UDP processing ...
1368: *
1.136 manu 1369: * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
1370: * the source UDP port. This is required if we want
1371: * to select the right SPD for multiple hosts behind
1372: * same NAT
1373: */
1.137 manu 1374: if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
1375: sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
1376: printf("udp4_espinudp: m_tag_get failed\n");
1.188 christos 1377: m_freem(m);
1378: return -1;
1.137 manu 1379: }
1.136 manu 1380: ((u_int16_t *)(tag + 1))[0] = sport;
1381: ((u_int16_t *)(tag + 1))[1] = dport;
1.188 christos 1382: m_tag_prepend(m, tag);
1.136 manu 1383:
1.190 christos 1384: #ifdef IPSEC
1.199 christos 1385: if (ipsec_used)
1386: ipsec4_common_input(m, iphdrlen, IPPROTO_ESP);
1387: /* XXX: else */
1.147 christos 1388: #else
1.188 christos 1389: esp4_input(m, iphdrlen);
1.147 christos 1390: #endif
1.130 manu 1391:
1.185 liamjfoy 1392: /* We handled it, it shouldn't be handled by UDP */
1.188 christos 1393: *mp = NULL; /* avoid free by caller ... */
1.130 manu 1394: return 1;
1395: }
1396: #endif
1.195 rmind 1397:
1.197 rmind 1398: PR_WRAP_USRREQS(udp)
1399: #define udp_attach udp_attach_wrapper
1400: #define udp_detach udp_detach_wrapper
1.208 rtr 1401: #define udp_accept udp_accept_wrapper
1.210 rtr 1402: #define udp_bind udp_bind_wrapper
1403: #define udp_listen udp_listen_wrapper
1.211 rtr 1404: #define udp_connect udp_connect_wrapper
1.217 rtr 1405: #define udp_connect2 udp_connect2_wrapper
1.212 rtr 1406: #define udp_disconnect udp_disconnect_wrapper
1407: #define udp_shutdown udp_shutdown_wrapper
1408: #define udp_abort udp_abort_wrapper
1.200 rtr 1409: #define udp_ioctl udp_ioctl_wrapper
1.203 rtr 1410: #define udp_stat udp_stat_wrapper
1.207 rtr 1411: #define udp_peeraddr udp_peeraddr_wrapper
1412: #define udp_sockaddr udp_sockaddr_wrapper
1.216 rtr 1413: #define udp_rcvd udp_rcvd_wrapper
1.209 rtr 1414: #define udp_recvoob udp_recvoob_wrapper
1.215 rtr 1415: #define udp_send udp_send_wrapper
1.209 rtr 1416: #define udp_sendoob udp_sendoob_wrapper
1.217 rtr 1417: #define udp_purgeif udp_purgeif_wrapper
1.195 rmind 1418: #define udp_usrreq udp_usrreq_wrapper
1419:
1420: const struct pr_usrreqs udp_usrreqs = {
1.196 rmind 1421: .pr_attach = udp_attach,
1422: .pr_detach = udp_detach,
1.208 rtr 1423: .pr_accept = udp_accept,
1.210 rtr 1424: .pr_bind = udp_bind,
1425: .pr_listen = udp_listen,
1.211 rtr 1426: .pr_connect = udp_connect,
1.217 rtr 1427: .pr_connect2 = udp_connect2,
1.212 rtr 1428: .pr_disconnect = udp_disconnect,
1429: .pr_shutdown = udp_shutdown,
1430: .pr_abort = udp_abort,
1.200 rtr 1431: .pr_ioctl = udp_ioctl,
1.203 rtr 1432: .pr_stat = udp_stat,
1.207 rtr 1433: .pr_peeraddr = udp_peeraddr,
1434: .pr_sockaddr = udp_sockaddr,
1.216 rtr 1435: .pr_rcvd = udp_rcvd,
1.209 rtr 1436: .pr_recvoob = udp_recvoob,
1.215 rtr 1437: .pr_send = udp_send,
1.209 rtr 1438: .pr_sendoob = udp_sendoob,
1.217 rtr 1439: .pr_purgeif = udp_purgeif,
1.195 rmind 1440: .pr_generic = udp_usrreq,
1441: };
CVSweb <webmaster@jp.NetBSD.org>