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