Annotation of src/sys/netinet/udp_usrreq.c, Revision 1.146.2.1
1.146.2.1! rpaulo 1: /* $NetBSD: udp_usrreq.c,v 1.146 2006/01/21 00:15:36 rpaulo 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:
63: #include <sys/cdefs.h>
1.146.2.1! rpaulo 64: __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.146 2006/01/21 00:15:36 rpaulo Exp $");
1.50 thorpej 65:
1.77 soda 66: #include "opt_inet.h"
1.50 thorpej 67: #include "opt_ipsec.h"
1.78 thorpej 68: #include "opt_inet_csum.h"
1.64 ws 69: #include "opt_ipkdb.h"
1.101 martin 70: #include "opt_mbuftrace.h"
1.1 cgd 71:
1.5 mycroft 72: #include <sys/param.h>
73: #include <sys/malloc.h>
74: #include <sys/mbuf.h>
75: #include <sys/protosw.h>
76: #include <sys/socket.h>
77: #include <sys/socketvar.h>
1.13 mycroft 78: #include <sys/errno.h>
1.5 mycroft 79: #include <sys/stat.h>
1.27 christos 80: #include <sys/systm.h>
81: #include <sys/proc.h>
1.53 itojun 82: #include <sys/domain.h>
1.27 christos 83: #include <sys/sysctl.h>
1.1 cgd 84:
1.5 mycroft 85: #include <net/if.h>
86: #include <net/route.h>
1.1 cgd 87:
1.5 mycroft 88: #include <netinet/in.h>
89: #include <netinet/in_systm.h>
1.15 cgd 90: #include <netinet/in_var.h>
1.5 mycroft 91: #include <netinet/ip.h>
92: #include <netinet/in_pcb.h>
93: #include <netinet/ip_var.h>
94: #include <netinet/ip_icmp.h>
95: #include <netinet/udp.h>
96: #include <netinet/udp_var.h>
1.1 cgd 97:
1.130 manu 98: #ifdef IPSEC_NAT_T
99: #include <netinet6/ipsec.h>
100: #include <netinet6/esp.h>
101: #endif
102:
1.53 itojun 103: #ifdef INET6
104: #include <netinet/ip6.h>
105: #include <netinet/icmp6.h>
106: #include <netinet6/ip6_var.h>
107: #include <netinet6/udp6_var.h>
1.146 rpaulo 108: #include <netinet6/scope6_var.h>
1.53 itojun 109: #endif
110:
111: #ifndef INET6
112: /* always need ip6.h for IP6_EXTHDR_GET */
113: #include <netinet/ip6.h>
114: #endif
115:
1.76 itojun 116: #include "faith.h"
117: #if defined(NFAITH) && NFAITH > 0
118: #include <net/if_faith.h>
119: #endif
120:
1.27 christos 121: #include <machine/stdarg.h>
122:
1.105 jonathan 123: #ifdef FAST_IPSEC
124: #include <netipsec/ipsec.h>
1.121 jonathan 125: #include <netipsec/ipsec_var.h> /* XXX ipsecstat namespace */
1.105 jonathan 126: #ifdef INET6
127: #include <netipsec/ipsec6.h>
128: #endif
129: #endif /* FAST_IPSEC*/
130:
1.48 itojun 131: #ifdef IPSEC
132: #include <netinet6/ipsec.h>
133: #include <netkey/key.h>
134: #endif /*IPSEC*/
135:
1.64 ws 136: #ifdef IPKDB
137: #include <ipkdb/ipkdb.h>
138: #endif
139:
1.8 mycroft 140: /*
141: * UDP protocol implementation.
142: * Per RFC 768, August, 1980.
143: */
144: #ifndef COMPAT_42
145: int udpcksum = 1;
146: #else
147: int udpcksum = 0; /* XXX */
148: #endif
1.141 yamt 149: int udp_do_loopback_cksum = 0;
1.93 matt 150:
151: struct inpcbtable udbtable;
152: struct udpstat udpstat;
1.8 mycroft 153:
1.72 itojun 154: #ifdef INET
1.130 manu 155: #ifdef IPSEC_NAT_T
1.144 manu 156: static int udp4_espinudp (struct mbuf **, int, struct sockaddr *,
1.130 manu 157: struct socket *);
158: #endif
1.119 matt 159: static void udp4_sendup (struct mbuf *, int, struct sockaddr *,
160: struct socket *);
161: static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *,
1.144 manu 162: struct mbuf **, int);
1.129 yamt 163: static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int);
1.72 itojun 164: #endif
1.53 itojun 165: #ifdef INET6
1.119 matt 166: static void udp6_sendup (struct mbuf *, int, struct sockaddr *,
167: struct socket *);
168: static int udp6_realinput (int, struct sockaddr_in6 *,
169: struct sockaddr_in6 *, struct mbuf *, int);
1.129 yamt 170: static int udp6_input_checksum(struct mbuf *, const struct udphdr *, int, int);
1.53 itojun 171: #endif
1.72 itojun 172: #ifdef INET
1.119 matt 173: static void udp_notify (struct inpcb *, int);
1.72 itojun 174: #endif
1.7 mycroft 175:
1.26 mycroft 176: #ifndef UDBHASHSIZE
177: #define UDBHASHSIZE 128
178: #endif
179: int udbhashsize = UDBHASHSIZE;
180:
1.98 matt 181: #ifdef MBUFTRACE
182: struct mowner udp_mowner = { "udp" };
183: struct mowner udp_rx_mowner = { "udp", "rx" };
184: struct mowner udp_tx_mowner = { "udp", "tx" };
185: #endif
186:
1.78 thorpej 187: #ifdef UDP_CSUM_COUNTERS
188: #include <sys/device.h>
189:
1.140 yamt 190: #if defined(INET)
1.78 thorpej 191: struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
192: NULL, "udp", "hwcsum bad");
193: struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
194: NULL, "udp", "hwcsum ok");
195: struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
196: NULL, "udp", "hwcsum data");
197: struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
198: NULL, "udp", "swcsum");
199:
1.120 matt 200: EVCNT_ATTACH_STATIC(udp_hwcsum_bad);
201: EVCNT_ATTACH_STATIC(udp_hwcsum_ok);
202: EVCNT_ATTACH_STATIC(udp_hwcsum_data);
203: EVCNT_ATTACH_STATIC(udp_swcsum);
1.140 yamt 204: #endif /* defined(INET) */
205:
206: #if defined(INET6)
207: struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
208: NULL, "udp6", "hwcsum bad");
209: struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
210: NULL, "udp6", "hwcsum ok");
211: struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
212: NULL, "udp6", "hwcsum data");
213: struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
214: NULL, "udp6", "swcsum");
215:
216: EVCNT_ATTACH_STATIC(udp6_hwcsum_bad);
217: EVCNT_ATTACH_STATIC(udp6_hwcsum_ok);
218: EVCNT_ATTACH_STATIC(udp6_hwcsum_data);
219: EVCNT_ATTACH_STATIC(udp6_swcsum);
220: #endif /* defined(INET6) */
221:
222: #define UDP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++
1.120 matt 223:
1.78 thorpej 224: #else
225:
226: #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */
227:
228: #endif /* UDP_CSUM_COUNTERS */
229:
1.7 mycroft 230: void
1.119 matt 231: udp_init(void)
1.1 cgd 232: {
1.18 mycroft 233:
1.35 mycroft 234: in_pcbinit(&udbtable, udbhashsize, udbhashsize);
1.78 thorpej 235:
1.98 matt 236: MOWNER_ATTACH(&udp_tx_mowner);
237: MOWNER_ATTACH(&udp_rx_mowner);
238: MOWNER_ATTACH(&udp_mowner);
1.1 cgd 239: }
240:
1.129 yamt 241: /*
242: * Checksum extended UDP header and data.
243: */
244:
245: int
246: udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh,
247: int iphlen, int len)
248: {
249:
250: switch (af) {
1.72 itojun 251: #ifdef INET
1.129 yamt 252: case AF_INET:
253: return udp4_input_checksum(m, uh, iphlen, len);
254: #endif
255: #ifdef INET6
256: case AF_INET6:
257: return udp6_input_checksum(m, uh, iphlen, len);
258: #endif
259: }
260: #ifdef DIAGNOSTIC
261: panic("udp_input_checksum: unknown af %d", af);
262: #endif
263: /* NOTREACHED */
264: return -1;
265: }
266:
267: #ifdef INET
268:
269: /*
270: * Checksum extended UDP header and data.
271: */
272:
273: static int
274: udp4_input_checksum(struct mbuf *m, const struct udphdr *uh,
275: int iphlen, int len)
276: {
277:
278: /*
279: * XXX it's better to record and check if this mbuf is
280: * already checked.
281: */
282:
283: if (uh->uh_sum == 0)
284: return 0;
285:
286: switch (m->m_pkthdr.csum_flags &
287: ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
288: M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
289: case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
290: UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
291: goto badcsum;
292:
293: case M_CSUM_UDPv4|M_CSUM_DATA: {
294: u_int32_t hw_csum = m->m_pkthdr.csum_data;
295:
296: UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
297: if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) {
298: const struct ip *ip =
299: mtod(m, const struct ip *);
300:
301: hw_csum = in_cksum_phdr(ip->ip_src.s_addr,
302: ip->ip_dst.s_addr,
303: htons(hw_csum + len + IPPROTO_UDP));
304: }
305: if ((hw_csum ^ 0xffff) != 0)
306: goto badcsum;
307: break;
308: }
309:
310: case M_CSUM_UDPv4:
311: /* Checksum was okay. */
312: UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
313: break;
314:
315: default:
316: /*
317: * Need to compute it ourselves. Maybe skip checksum
318: * on loopback interfaces.
319: */
320: if (__predict_true(!(m->m_pkthdr.rcvif->if_flags &
321: IFF_LOOPBACK) ||
322: udp_do_loopback_cksum)) {
323: UDP_CSUM_COUNTER_INCR(&udp_swcsum);
324: if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
325: goto badcsum;
326: }
327: break;
328: }
329:
330: return 0;
331:
332: badcsum:
333: udpstat.udps_badsum++;
334: return -1;
335: }
336:
1.7 mycroft 337: void
1.27 christos 338: udp_input(struct mbuf *m, ...)
1.1 cgd 339: {
1.53 itojun 340: va_list ap;
341: struct sockaddr_in src, dst;
342: struct ip *ip;
343: struct udphdr *uh;
1.97 simonb 344: int iphlen;
1.53 itojun 345: int len;
346: int n;
1.96 itojun 347: u_int16_t ip_len;
1.53 itojun 348:
349: va_start(ap, m);
350: iphlen = va_arg(ap, int);
1.97 simonb 351: (void)va_arg(ap, int); /* ignore value, advance ap */
1.53 itojun 352: va_end(ap);
353:
1.98 matt 354: MCLAIM(m, &udp_rx_mowner);
1.53 itojun 355: udpstat.udps_ipackets++;
356:
357: /*
358: * Get IP and UDP header together in first mbuf.
359: */
360: ip = mtod(m, struct ip *);
361: IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
362: if (uh == NULL) {
363: udpstat.udps_hdrops++;
364: return;
365: }
1.95 thorpej 366: KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53 itojun 367:
1.57 itojun 368: /* destination port of 0 is illegal, based on RFC768. */
369: if (uh->uh_dport == 0)
370: goto bad;
371:
1.53 itojun 372: /*
373: * Make mbuf data length reflect UDP length.
374: * If not enough data to reflect UDP length, drop.
375: */
1.96 itojun 376: ip_len = ntohs(ip->ip_len);
1.53 itojun 377: len = ntohs((u_int16_t)uh->uh_ulen);
1.96 itojun 378: if (ip_len != iphlen + len) {
379: if (ip_len < iphlen + len || len < sizeof(struct udphdr)) {
1.53 itojun 380: udpstat.udps_badlen++;
381: goto bad;
382: }
1.96 itojun 383: m_adj(m, iphlen + len - ip_len);
1.53 itojun 384: }
385:
386: /*
387: * Checksum extended UDP header and data.
388: */
1.129 yamt 389: if (udp4_input_checksum(m, uh, iphlen, len))
390: goto badcsum;
1.53 itojun 391:
392: /* construct source and dst sockaddrs. */
393: bzero(&src, sizeof(src));
394: src.sin_family = AF_INET;
395: src.sin_len = sizeof(struct sockaddr_in);
396: bcopy(&ip->ip_src, &src.sin_addr, sizeof(src.sin_addr));
397: src.sin_port = uh->uh_sport;
398: bzero(&dst, sizeof(dst));
399: dst.sin_family = AF_INET;
400: dst.sin_len = sizeof(struct sockaddr_in);
401: bcopy(&ip->ip_dst, &dst.sin_addr, sizeof(dst.sin_addr));
402: dst.sin_port = uh->uh_dport;
403:
1.144 manu 404: if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) {
405: udpstat.udps_hdrops++;
406: return;
407: }
1.53 itojun 408: #ifdef INET6
409: if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
410: struct sockaddr_in6 src6, dst6;
411:
412: bzero(&src6, sizeof(src6));
413: src6.sin6_family = AF_INET6;
414: src6.sin6_len = sizeof(struct sockaddr_in6);
415: src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
416: bcopy(&ip->ip_src, &src6.sin6_addr.s6_addr[12],
417: sizeof(ip->ip_src));
418: src6.sin6_port = uh->uh_sport;
419: bzero(&dst6, sizeof(dst6));
420: dst6.sin6_family = AF_INET6;
421: dst6.sin6_len = sizeof(struct sockaddr_in6);
422: dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
423: bcopy(&ip->ip_dst, &dst6.sin6_addr.s6_addr[12],
424: sizeof(ip->ip_dst));
425: dst6.sin6_port = uh->uh_dport;
426:
427: n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
428: }
429: #endif
430:
431: if (n == 0) {
432: if (m->m_flags & (M_BCAST | M_MCAST)) {
433: udpstat.udps_noportbcast++;
434: goto bad;
435: }
1.61 itojun 436: udpstat.udps_noport++;
1.64 ws 437: #ifdef IPKDB
1.53 itojun 438: if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
439: m, iphlen + sizeof(struct udphdr),
440: m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
441: /*
442: * It was a debugger connect packet,
443: * just drop it now
444: */
445: goto bad;
446: }
447: #endif
448: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
449: m = NULL;
450: }
451:
452: bad:
453: if (m)
454: m_freem(m);
1.78 thorpej 455: return;
456:
457: badcsum:
458: m_freem(m);
1.53 itojun 459: }
1.72 itojun 460: #endif
1.53 itojun 461:
462: #ifdef INET6
1.129 yamt 463: static int
464: udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
465: {
466:
1.140 yamt 467: /*
468: * XXX it's better to record and check if this mbuf is
469: * already checked.
470: */
471:
1.129 yamt 472: if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
473: goto good;
474: }
475: if (uh->uh_sum == 0) {
476: udp6stat.udp6s_nosum++;
477: goto bad;
478: }
1.140 yamt 479:
480: switch (m->m_pkthdr.csum_flags &
481: ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv6) |
482: M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
483: case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD:
484: UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad);
1.129 yamt 485: udp6stat.udp6s_badsum++;
486: goto bad;
1.140 yamt 487:
488: #if 0 /* notyet */
489: case M_CSUM_UDPv6|M_CSUM_DATA:
490: #endif
491:
492: case M_CSUM_UDPv6:
493: /* Checksum was okay. */
494: UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok);
495: break;
496:
497: default:
498: /*
499: * Need to compute it ourselves. Maybe skip checksum
500: * on loopback interfaces.
501: */
502: UDP_CSUM_COUNTER_INCR(&udp6_swcsum);
503: if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
504: udp6stat.udp6s_badsum++;
505: goto bad;
506: }
1.129 yamt 507: }
508:
509: good:
510: return 0;
511: bad:
512: return -1;
513: }
514:
1.53 itojun 515: int
1.119 matt 516: udp6_input(struct mbuf **mp, int *offp, int proto)
1.53 itojun 517: {
518: struct mbuf *m = *mp;
519: int off = *offp;
520: struct sockaddr_in6 src, dst;
521: struct ip6_hdr *ip6;
522: struct udphdr *uh;
523: u_int32_t plen, ulen;
524:
1.76 itojun 525: ip6 = mtod(m, struct ip6_hdr *);
526:
1.53 itojun 527: #if defined(NFAITH) && 0 < NFAITH
1.76 itojun 528: if (faithprefix(&ip6->ip6_dst)) {
529: /* send icmp6 host unreach? */
530: m_freem(m);
531: return IPPROTO_DONE;
1.53 itojun 532: }
533: #endif
534:
535: udp6stat.udp6s_ipackets++;
536:
537: /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */
538: plen = m->m_pkthdr.len - off;
539: IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
540: if (uh == NULL) {
541: ip6stat.ip6s_tooshort++;
542: return IPPROTO_DONE;
543: }
1.95 thorpej 544: KASSERT(UDP_HDR_ALIGNED_P(uh));
1.53 itojun 545: ulen = ntohs((u_short)uh->uh_ulen);
1.69 itojun 546: /*
547: * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
548: * iff payload length > 0xffff.
549: */
1.53 itojun 550: if (ulen == 0 && plen > 0xffff)
551: ulen = plen;
552:
553: if (plen != ulen) {
554: udp6stat.udp6s_badlen++;
1.54 itojun 555: goto bad;
556: }
557:
1.57 itojun 558: /* destination port of 0 is illegal, based on RFC768. */
559: if (uh->uh_dport == 0)
560: goto bad;
561:
1.54 itojun 562: /* Be proactive about malicious use of IPv4 mapped address */
563: if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
564: IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
565: /* XXX stat */
1.53 itojun 566: goto bad;
567: }
568:
569: /*
1.125 thorpej 570: * Checksum extended UDP header and data. Maybe skip checksum
571: * on loopback interfaces.
1.53 itojun 572: */
1.129 yamt 573: if (udp6_input_checksum(m, uh, off, ulen))
574: goto bad;
1.53 itojun 575:
576: /*
577: * Construct source and dst sockaddrs.
578: */
579: bzero(&src, sizeof(src));
580: src.sin6_family = AF_INET6;
581: src.sin6_len = sizeof(struct sockaddr_in6);
1.146 rpaulo 582: src.sin6_addr = ip6->ip6_src;
1.53 itojun 583: src.sin6_port = uh->uh_sport;
584: bzero(&dst, sizeof(dst));
585: dst.sin6_family = AF_INET6;
586: dst.sin6_len = sizeof(struct sockaddr_in6);
1.146 rpaulo 587: dst.sin6_addr = ip6->ip6_dst;
1.53 itojun 588: dst.sin6_port = uh->uh_dport;
589:
590: if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
591: if (m->m_flags & M_MCAST) {
592: udp6stat.udp6s_noportmcast++;
593: goto bad;
594: }
1.61 itojun 595: udp6stat.udp6s_noport++;
1.53 itojun 596: icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
597: m = NULL;
598: }
599:
600: bad:
601: if (m)
602: m_freem(m);
603: return IPPROTO_DONE;
604: }
605: #endif
606:
1.72 itojun 607: #ifdef INET
1.53 itojun 608: static void
1.119 matt 609: udp4_sendup(struct mbuf *m, int off /* offset of data portion */,
610: struct sockaddr *src, struct socket *so)
1.53 itojun 611: {
612: struct mbuf *opts = NULL;
613: struct mbuf *n;
614: struct inpcb *inp = NULL;
615:
616: if (!so)
617: return;
618: switch (so->so_proto->pr_domain->dom_family) {
619: case AF_INET:
620: inp = sotoinpcb(so);
621: break;
622: #ifdef INET6
623: case AF_INET6:
624: break;
625: #endif
626: default:
627: return;
628: }
629:
1.105 jonathan 630: #if defined(IPSEC) || defined(FAST_IPSEC)
1.53 itojun 631: /* check AH/ESP integrity. */
632: if (so != NULL && ipsec4_in_reject_so(m, so)) {
633: ipsecstat.in_polvio++;
1.110 itojun 634: if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
635: icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT,
636: 0, 0);
1.53 itojun 637: return;
638: }
639: #endif /*IPSEC*/
640:
641: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
642: if (inp && (inp->inp_flags & INP_CONTROLOPTS
643: || so->so_options & SO_TIMESTAMP)) {
644: struct ip *ip = mtod(n, struct ip *);
645: ip_savecontrol(inp, &opts, ip, n);
646: }
647:
648: m_adj(n, off);
649: if (sbappendaddr(&so->so_rcv, src, n,
650: opts) == 0) {
651: m_freem(n);
652: if (opts)
653: m_freem(opts);
1.124 darrenr 654: so->so_rcv.sb_overflowed++;
1.81 enami 655: udpstat.udps_fullsock++;
1.53 itojun 656: } else
657: sorwakeup(so);
658: }
659: }
1.72 itojun 660: #endif
1.53 itojun 661:
662: #ifdef INET6
663: static void
1.119 matt 664: udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
665: struct sockaddr *src, struct socket *so)
1.53 itojun 666: {
667: struct mbuf *opts = NULL;
668: struct mbuf *n;
669: struct in6pcb *in6p = NULL;
670:
671: if (!so)
672: return;
673: if (so->so_proto->pr_domain->dom_family != AF_INET6)
674: return;
675: in6p = sotoin6pcb(so);
676:
1.105 jonathan 677: #if defined(IPSEC) || defined(FAST_IPSEC)
1.53 itojun 678: /* check AH/ESP integrity. */
679: if (so != NULL && ipsec6_in_reject_so(m, so)) {
680: ipsec6stat.in_polvio++;
1.111 mycroft 681: if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
1.110 itojun 682: icmp6_error(n, ICMP6_DST_UNREACH,
683: ICMP6_DST_UNREACH_ADMIN, 0);
1.53 itojun 684: return;
685: }
686: #endif /*IPSEC*/
687:
688: if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
689: if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
690: || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
691: struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
692: ip6_savecontrol(in6p, &opts, ip6, n);
693: }
694:
695: m_adj(n, off);
696: if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
697: m_freem(n);
698: if (opts)
699: m_freem(opts);
1.124 darrenr 700: so->so_rcv.sb_overflowed++;
1.53 itojun 701: udp6stat.udp6s_fullsock++;
702: } else
703: sorwakeup(so);
704: }
705: }
706: #endif
707:
1.72 itojun 708: #ifdef INET
1.53 itojun 709: static int
1.119 matt 710: udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst,
1.144 manu 711: struct mbuf **mp, int off /* offset of udphdr */)
1.53 itojun 712: {
713: u_int16_t *sport, *dport;
714: int rcvcnt;
715: struct in_addr *src4, *dst4;
1.109 itojun 716: struct inpcb_hdr *inph;
1.53 itojun 717: struct inpcb *inp;
1.144 manu 718: struct mbuf *m = *mp;
1.53 itojun 719:
720: rcvcnt = 0;
721: off += sizeof(struct udphdr); /* now, offset of payload */
722:
723: if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
724: goto bad;
725:
726: src4 = &src->sin_addr;
727: sport = &src->sin_port;
728: dst4 = &dst->sin_addr;
729: dport = &dst->sin_port;
730:
1.73 itojun 731: if (IN_MULTICAST(dst4->s_addr) ||
1.53 itojun 732: in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
733: /*
734: * Deliver a multicast or broadcast datagram to *all* sockets
735: * for which the local and remote addresses and ports match
736: * those of the incoming datagram. This allows more than
737: * one process to receive multi/broadcasts on the same port.
738: * (This really ought to be done for unicast datagrams as
739: * well, but that would cause problems with existing
740: * applications that open both address-specific sockets and
741: * a wildcard socket listening to the same port -- they would
742: * end up receiving duplicates of every unicast datagram.
743: * Those applications open the multiple sockets to overcome an
744: * inadequacy of the UDP socket interface, but for backwards
745: * compatibility we avoid the problem here rather than
746: * fixing the interface. Maybe 4.5BSD will remedy this?)
747: */
748:
749: /*
1.92 itojun 750: * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71 itojun 751: * we need udpiphdr for IPsec processing so we do that later.
1.53 itojun 752: */
753: /*
754: * Locate pcb(s) for datagram.
755: */
1.109 itojun 756: CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
757: inp = (struct inpcb *)inph;
758: if (inp->inp_af != AF_INET)
759: continue;
760:
1.53 itojun 761: if (inp->inp_lport != *dport)
762: continue;
763: if (!in_nullhost(inp->inp_laddr)) {
764: if (!in_hosteq(inp->inp_laddr, *dst4))
765: continue;
766: }
767: if (!in_nullhost(inp->inp_faddr)) {
768: if (!in_hosteq(inp->inp_faddr, *src4) ||
769: inp->inp_fport != *sport)
770: continue;
771: }
772:
773: udp4_sendup(m, off, (struct sockaddr *)src,
774: inp->inp_socket);
775: rcvcnt++;
776:
777: /*
778: * Don't look for additional matches if this one does
779: * not have either the SO_REUSEPORT or SO_REUSEADDR
780: * socket options set. This heuristic avoids searching
781: * through all pcbs in the common case of a non-shared
782: * port. It assumes that an application will never
783: * clear these options after setting them.
784: */
785: if ((inp->inp_socket->so_options &
786: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
787: break;
788: }
789: } else {
790: /*
791: * Locate pcb for datagram.
792: */
793: inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
794: if (inp == 0) {
795: ++udpstat.udps_pcbhashmiss;
796: inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
1.82 itojun 797: if (inp == 0)
1.53 itojun 798: return rcvcnt;
799: }
800:
1.130 manu 801: #ifdef IPSEC_NAT_T
802: /* Handle ESP over UDP */
803: if (inp->inp_flags & INP_ESPINUDP_ALL) {
804: struct sockaddr *sa = (struct sockaddr *)src;
805:
1.144 manu 806: switch(udp4_espinudp(mp, off, sa, inp->inp_socket)) {
807: case -1: /* Error, m was freeed */
808: rcvcnt = -1;
809: goto bad;
810: break;
811:
812: case 1: /* ESP over UDP */
1.130 manu 813: rcvcnt++;
814: goto bad;
1.144 manu 815: break;
816:
817: case 0: /* plain UDP */
818: default: /* Unexpected */
819: /*
820: * Normal UDP processing will take place
821: * m may have changed.
822: */
823: m = *mp;
824: break;
1.130 manu 825: }
826: }
827: #endif
828:
1.53 itojun 829: udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
830: rcvcnt++;
831: }
832:
833: bad:
834: return rcvcnt;
835: }
1.72 itojun 836: #endif
1.53 itojun 837:
838: #ifdef INET6
839: static int
1.119 matt 840: udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
841: struct mbuf *m, int off)
1.53 itojun 842: {
1.79 itojun 843: u_int16_t sport, dport;
1.53 itojun 844: int rcvcnt;
1.146 rpaulo 845: struct in6_addr src6, *dst6;
1.79 itojun 846: const struct in_addr *dst4;
1.109 itojun 847: struct inpcb_hdr *inph;
1.53 itojun 848: struct in6pcb *in6p;
849:
850: rcvcnt = 0;
851: off += sizeof(struct udphdr); /* now, offset of payload */
852:
853: if (af != AF_INET && af != AF_INET6)
854: goto bad;
855: if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
856: goto bad;
857:
1.146 rpaulo 858: src6 = src->sin6_addr;
859: if (sa6_recoverscope(src) != 0) {
860: /* XXX: should be impossible. */
861: goto bad;
862: }
1.79 itojun 863: sport = src->sin6_port;
1.146 rpaulo 864:
1.79 itojun 865: dport = dst->sin6_port;
1.88 itojun 866: dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
1.146 rpaulo 867: dst6 = &dst->sin6_addr;
1.53 itojun 868:
1.146 rpaulo 869: if (IN6_IS_ADDR_MULTICAST(dst6) ||
1.79 itojun 870: (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
1.53 itojun 871: /*
872: * Deliver a multicast or broadcast datagram to *all* sockets
873: * for which the local and remote addresses and ports match
874: * those of the incoming datagram. This allows more than
875: * one process to receive multi/broadcasts on the same port.
876: * (This really ought to be done for unicast datagrams as
877: * well, but that would cause problems with existing
878: * applications that open both address-specific sockets and
879: * a wildcard socket listening to the same port -- they would
880: * end up receiving duplicates of every unicast datagram.
881: * Those applications open the multiple sockets to overcome an
882: * inadequacy of the UDP socket interface, but for backwards
883: * compatibility we avoid the problem here rather than
884: * fixing the interface. Maybe 4.5BSD will remedy this?)
885: */
886:
887: /*
1.92 itojun 888: * KAME note: traditionally we dropped udpiphdr from mbuf here.
1.71 itojun 889: * we need udpiphdr for IPsec processing so we do that later.
1.53 itojun 890: */
891: /*
892: * Locate pcb(s) for datagram.
893: */
1.109 itojun 894: CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
895: in6p = (struct in6pcb *)inph;
896: if (in6p->in6p_af != AF_INET6)
897: continue;
898:
1.79 itojun 899: if (in6p->in6p_lport != dport)
1.53 itojun 900: continue;
901: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1.146 rpaulo 902: if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
903: dst6))
1.53 itojun 904: continue;
1.92 itojun 905: } else {
1.146 rpaulo 906: if (IN6_IS_ADDR_V4MAPPED(dst6) &&
1.85 itojun 907: (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55 itojun 908: continue;
909: }
1.53 itojun 910: if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1.79 itojun 911: if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
912: &src6) || in6p->in6p_fport != sport)
1.53 itojun 913: continue;
1.92 itojun 914: } else {
1.79 itojun 915: if (IN6_IS_ADDR_V4MAPPED(&src6) &&
1.85 itojun 916: (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
1.55 itojun 917: continue;
918: }
1.53 itojun 919:
920: udp6_sendup(m, off, (struct sockaddr *)src,
921: in6p->in6p_socket);
922: rcvcnt++;
923:
924: /*
925: * Don't look for additional matches if this one does
926: * not have either the SO_REUSEPORT or SO_REUSEADDR
927: * socket options set. This heuristic avoids searching
928: * through all pcbs in the common case of a non-shared
929: * port. It assumes that an application will never
930: * clear these options after setting them.
931: */
932: if ((in6p->in6p_socket->so_options &
933: (SO_REUSEPORT|SO_REUSEADDR)) == 0)
934: break;
935: }
936: } else {
937: /*
938: * Locate pcb for datagram.
939: */
1.146 rpaulo 940: in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
941: dport, 0);
1.53 itojun 942: if (in6p == 0) {
943: ++udpstat.udps_pcbhashmiss;
1.146 rpaulo 944: in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
1.82 itojun 945: if (in6p == 0)
1.53 itojun 946: return rcvcnt;
947: }
948:
949: udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
950: rcvcnt++;
951: }
952:
953: bad:
954: return rcvcnt;
955: }
956: #endif
1.1 cgd 957:
1.72 itojun 958: #ifdef INET
1.1 cgd 959: /*
960: * Notify a udp user of an asynchronous error;
961: * just wake up so that he can collect error status.
962: */
1.7 mycroft 963: static void
1.119 matt 964: udp_notify(struct inpcb *inp, int errno)
1.1 cgd 965: {
966: inp->inp_socket->so_error = errno;
967: sorwakeup(inp->inp_socket);
968: sowwakeup(inp->inp_socket);
969: }
970:
1.27 christos 971: void *
1.119 matt 972: udp_ctlinput(int cmd, struct sockaddr *sa, void *v)
1.1 cgd 973: {
1.66 augustss 974: struct ip *ip = v;
975: struct udphdr *uh;
1.119 matt 976: void (*notify)(struct inpcb *, int) = udp_notify;
1.21 mycroft 977: int errno;
1.1 cgd 978:
1.53 itojun 979: if (sa->sa_family != AF_INET
980: || sa->sa_len != sizeof(struct sockaddr_in))
1.51 itojun 981: return NULL;
1.20 mycroft 982: if ((unsigned)cmd >= PRC_NCMDS)
1.27 christos 983: return NULL;
1.20 mycroft 984: errno = inetctlerrmap[cmd];
1.18 mycroft 985: if (PRC_IS_REDIRECT(cmd))
1.19 mycroft 986: notify = in_rtchange, ip = 0;
1.18 mycroft 987: else if (cmd == PRC_HOSTDEAD)
1.19 mycroft 988: ip = 0;
1.23 cgd 989: else if (errno == 0)
1.27 christos 990: return NULL;
1.19 mycroft 991: if (ip) {
1.1 cgd 992: uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
1.34 mycroft 993: in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
994: ip->ip_src, uh->uh_sport, errno, notify);
1.53 itojun 995:
996: /* XXX mapped address case */
1.19 mycroft 997: } else
1.34 mycroft 998: in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
999: notify);
1.27 christos 1000: return NULL;
1.1 cgd 1001: }
1002:
1.7 mycroft 1003: int
1.131 perry 1004: udp_ctloutput(op, so, level, optname, mp)
1.130 manu 1005: int op;
1006: struct socket *so;
1007: int level, optname;
1008: struct mbuf **mp;
1009: {
1010: int s;
1011: int error = 0;
1012: struct mbuf *m;
1013: struct inpcb *inp;
1014: int family;
1015:
1016: family = so->so_proto->pr_domain->dom_family;
1017:
1018: s = splsoftnet();
1019: switch (family) {
1020: #ifdef INET
1021: case PF_INET:
1022: if (level != IPPROTO_UDP) {
1023: error = ip_ctloutput(op, so, level, optname, mp);
1024: goto end;
1025: }
1026: break;
1027: #endif
1028: #ifdef INET6
1029: case PF_INET6:
1030: if (level != IPPROTO_UDP) {
1031: error = ip6_ctloutput(op, so, level, optname, mp);
1032: goto end;
1033: }
1034: break;
1035: #endif
1036: default:
1037: error = EAFNOSUPPORT;
1038: goto end;
1039: break;
1040: }
1041:
1042:
1043: switch (op) {
1044: case PRCO_SETOPT:
1045: m = *mp;
1046: inp = sotoinpcb(so);
1047:
1048: switch (optname) {
1049: case UDP_ENCAP:
1050: if (m == NULL || m->m_len < sizeof (int)) {
1051: error = EINVAL;
1052: goto end;
1053: }
1.131 perry 1054:
1.130 manu 1055: switch(*mtod(m, int *)) {
1056: #ifdef IPSEC_NAT_T
1057: case 0:
1058: inp->inp_flags &= ~INP_ESPINUDP_ALL;
1059: break;
1060:
1061: case UDP_ENCAP_ESPINUDP:
1062: inp->inp_flags &= ~INP_ESPINUDP_ALL;
1063: inp->inp_flags |= INP_ESPINUDP;
1064: break;
1.131 perry 1065:
1.130 manu 1066: case UDP_ENCAP_ESPINUDP_NON_IKE:
1067: inp->inp_flags &= ~INP_ESPINUDP_ALL;
1068: inp->inp_flags |= INP_ESPINUDP_NON_IKE;
1069: break;
1070: #endif
1071: default:
1072: error = EINVAL;
1073: goto end;
1074: break;
1075: }
1076: break;
1077:
1078: default:
1079: error = ENOPROTOOPT;
1080: goto end;
1081: break;
1082: }
1083: break;
1084:
1085: default:
1086: error = EINVAL;
1087: goto end;
1088: break;
1.131 perry 1089: }
1090:
1.130 manu 1091: end:
1092: splx(s);
1093: return error;
1094: }
1.131 perry 1095:
1.130 manu 1096:
1097: int
1.27 christos 1098: udp_output(struct mbuf *m, ...)
1099: {
1.66 augustss 1100: struct inpcb *inp;
1101: struct udpiphdr *ui;
1.125 thorpej 1102: struct route *ro;
1.66 augustss 1103: int len = m->m_pkthdr.len;
1.31 mycroft 1104: int error = 0;
1.27 christos 1105: va_list ap;
1106:
1.98 matt 1107: MCLAIM(m, &udp_tx_mowner);
1.27 christos 1108: va_start(ap, m);
1109: inp = va_arg(ap, struct inpcb *);
1110: va_end(ap);
1.1 cgd 1111:
1112: /*
1113: * Calculate data length and get a mbuf
1114: * for UDP and IP headers.
1115: */
1.13 mycroft 1116: M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
1117: if (m == 0) {
1118: error = ENOBUFS;
1.39 thorpej 1119: goto release;
1120: }
1121:
1122: /*
1123: * Compute the packet length of the IP header, and
1124: * punt if the length looks bogus.
1125: */
1.96 itojun 1126: if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1.39 thorpej 1127: error = EMSGSIZE;
1.13 mycroft 1128: goto release;
1129: }
1.1 cgd 1130:
1131: /*
1132: * Fill in mbuf with extended UDP header
1133: * and addresses and length put into network format.
1134: */
1135: ui = mtod(m, struct udpiphdr *);
1136: ui->ui_pr = IPPROTO_UDP;
1137: ui->ui_src = inp->inp_laddr;
1138: ui->ui_dst = inp->inp_faddr;
1139: ui->ui_sport = inp->inp_lport;
1140: ui->ui_dport = inp->inp_fport;
1.78 thorpej 1141: ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
1.1 cgd 1142:
1.125 thorpej 1143: ro = &inp->inp_route;
1144:
1.1 cgd 1145: /*
1.78 thorpej 1146: * Set up checksum and output datagram.
1.1 cgd 1147: */
1148: if (udpcksum) {
1.78 thorpej 1149: /*
1150: * XXX Cache pseudo-header checksum part for
1151: * XXX "connected" UDP sockets.
1152: */
1153: ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
1154: ui->ui_dst.s_addr, htons((u_int16_t)len +
1155: sizeof(struct udphdr) + IPPROTO_UDP));
1.135 yamt 1156: m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
1.78 thorpej 1157: m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1158: } else
1159: ui->ui_sum = 0;
1.96 itojun 1160: ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
1.1 cgd 1161: ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */
1162: ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */
1163: udpstat.udps_opackets++;
1.48 itojun 1164:
1.125 thorpej 1165: return (ip_output(m, inp->inp_options, ro,
1.12 mycroft 1166: inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
1.107 itojun 1167: inp->inp_moptions, inp->inp_socket));
1.1 cgd 1168:
1169: release:
1170: m_freem(m);
1171: return (error);
1172: }
1173:
1.42 thorpej 1174: int udp_sendspace = 9216; /* really max datagram size */
1175: int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
1.1 cgd 1176: /* 40 1K datagrams */
1177:
1178: /*ARGSUSED*/
1.7 mycroft 1179: int
1.119 matt 1180: udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
1.145 christos 1181: struct mbuf *control, struct lwp *l)
1.1 cgd 1182: {
1.66 augustss 1183: struct inpcb *inp;
1.145 christos 1184: struct proc *p;
1.1 cgd 1185: int s;
1.66 augustss 1186: int error = 0;
1.1 cgd 1187:
1.145 christos 1188: p = l ? l->l_proc : NULL;
1.1 cgd 1189: if (req == PRU_CONTROL)
1.31 mycroft 1190: return (in_control(so, (long)m, (caddr_t)nam,
1.30 mycroft 1191: (struct ifnet *)control, p));
1.59 thorpej 1192:
1.60 thorpej 1193: if (req == PRU_PURGEIF) {
1.80 itojun 1194: in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
1.60 thorpej 1195: in_purgeif((struct ifnet *)control);
1196: in_pcbpurgeif(&udbtable, (struct ifnet *)control);
1.59 thorpej 1197: return (0);
1198: }
1.31 mycroft 1199:
1200: s = splsoftnet();
1201: inp = sotoinpcb(so);
1.32 mycroft 1202: #ifdef DIAGNOSTIC
1203: if (req != PRU_SEND && req != PRU_SENDOOB && control)
1204: panic("udp_usrreq: unexpected control mbuf");
1205: #endif
1.31 mycroft 1206: if (inp == 0 && req != PRU_ATTACH) {
1207: error = EINVAL;
1208: goto release;
1209: }
1210:
1.1 cgd 1211: /*
1212: * Note: need to block udp_input while changing
1213: * the udp pcb queue and/or pcb addresses.
1214: */
1215: switch (req) {
1216:
1217: case PRU_ATTACH:
1.31 mycroft 1218: if (inp != 0) {
1219: error = EISCONN;
1.1 cgd 1220: break;
1221: }
1.98 matt 1222: #ifdef MBUFTRACE
1223: so->so_mowner = &udp_mowner;
1224: so->so_rcv.sb_mowner = &udp_rx_mowner;
1225: so->so_snd.sb_mowner = &udp_tx_mowner;
1226: #endif
1.31 mycroft 1227: if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1228: error = soreserve(so, udp_sendspace, udp_recvspace);
1229: if (error)
1230: break;
1231: }
1.18 mycroft 1232: error = in_pcballoc(so, &udbtable);
1.1 cgd 1233: if (error)
1234: break;
1.31 mycroft 1235: inp = sotoinpcb(so);
1236: inp->inp_ip.ip_ttl = ip_defttl;
1.1 cgd 1237: break;
1238:
1239: case PRU_DETACH:
1.31 mycroft 1240: in_pcbdetach(inp);
1.1 cgd 1241: break;
1242:
1243: case PRU_BIND:
1.31 mycroft 1244: error = in_pcbbind(inp, nam, p);
1.1 cgd 1245: break;
1246:
1247: case PRU_LISTEN:
1248: error = EOPNOTSUPP;
1249: break;
1250:
1251: case PRU_CONNECT:
1.143 dsl 1252: error = in_pcbconnect(inp, nam, p);
1.31 mycroft 1253: if (error)
1.1 cgd 1254: break;
1.31 mycroft 1255: soisconnected(so);
1.1 cgd 1256: break;
1257:
1258: case PRU_CONNECT2:
1259: error = EOPNOTSUPP;
1260: break;
1261:
1262: case PRU_DISCONNECT:
1.31 mycroft 1263: /*soisdisconnected(so);*/
1264: so->so_state &= ~SS_ISCONNECTED; /* XXX */
1.1 cgd 1265: in_pcbdisconnect(inp);
1.34 mycroft 1266: inp->inp_laddr = zeroin_addr; /* XXX */
1.35 mycroft 1267: in_pcbstate(inp, INP_BOUND); /* XXX */
1.1 cgd 1268: break;
1269:
1270: case PRU_SHUTDOWN:
1271: socantsendmore(so);
1272: break;
1273:
1.31 mycroft 1274: case PRU_RCVD:
1275: error = EOPNOTSUPP;
1.1 cgd 1276: break;
1277:
1.31 mycroft 1278: case PRU_SEND:
1.32 mycroft 1279: if (control && control->m_len) {
1280: m_freem(control);
1281: m_freem(m);
1282: error = EINVAL;
1283: break;
1284: }
1.31 mycroft 1285: {
1.35 mycroft 1286: struct in_addr laddr; /* XXX */
1.1 cgd 1287:
1.31 mycroft 1288: if (nam) {
1.35 mycroft 1289: laddr = inp->inp_laddr; /* XXX */
1.31 mycroft 1290: if ((so->so_state & SS_ISCONNECTED) != 0) {
1291: error = EISCONN;
1.32 mycroft 1292: goto die;
1.31 mycroft 1293: }
1.143 dsl 1294: error = in_pcbconnect(inp, nam, p);
1.100 matt 1295: if (error)
1296: goto die;
1.31 mycroft 1297: } else {
1298: if ((so->so_state & SS_ISCONNECTED) == 0) {
1299: error = ENOTCONN;
1.32 mycroft 1300: goto die;
1.31 mycroft 1301: }
1302: }
1.33 mycroft 1303: error = udp_output(m, inp);
1.100 matt 1304: m = NULL;
1.31 mycroft 1305: if (nam) {
1306: in_pcbdisconnect(inp);
1.35 mycroft 1307: inp->inp_laddr = laddr; /* XXX */
1308: in_pcbstate(inp, INP_BOUND); /* XXX */
1.31 mycroft 1309: }
1.100 matt 1310: die:
1311: if (m)
1312: m_freem(m);
1.31 mycroft 1313: }
1.1 cgd 1314: break;
1315:
1316: case PRU_SENSE:
1317: /*
1318: * stat: don't bother with a blocksize.
1319: */
1.31 mycroft 1320: splx(s);
1.1 cgd 1321: return (0);
1322:
1.31 mycroft 1323: case PRU_RCVOOB:
1324: error = EOPNOTSUPP;
1325: break;
1326:
1.1 cgd 1327: case PRU_SENDOOB:
1.32 mycroft 1328: m_freem(control);
1.31 mycroft 1329: m_freem(m);
1.1 cgd 1330: error = EOPNOTSUPP;
1331: break;
1332:
1.31 mycroft 1333: case PRU_SOCKADDR:
1334: in_setsockaddr(inp, nam);
1335: break;
1336:
1337: case PRU_PEERADDR:
1338: in_setpeeraddr(inp, nam);
1339: break;
1.1 cgd 1340:
1341: default:
1342: panic("udp_usrreq");
1343: }
1344:
1345: release:
1.31 mycroft 1346: splx(s);
1.1 cgd 1347: return (error);
1.13 mycroft 1348: }
1349:
1350: /*
1351: * Sysctl for udp variables.
1352: */
1.114 atatat 1353: SYSCTL_SETUP(sysctl_net_inet_udp_setup, "sysctl net.inet.udp subtree setup")
1.13 mycroft 1354: {
1.114 atatat 1355:
1.116 atatat 1356: sysctl_createv(clog, 0, NULL, NULL,
1357: CTLFLAG_PERMANENT,
1.114 atatat 1358: CTLTYPE_NODE, "net", NULL,
1359: NULL, 0, NULL, 0,
1360: CTL_NET, CTL_EOL);
1.116 atatat 1361: sysctl_createv(clog, 0, NULL, NULL,
1362: CTLFLAG_PERMANENT,
1.114 atatat 1363: CTLTYPE_NODE, "inet", NULL,
1364: NULL, 0, NULL, 0,
1365: CTL_NET, PF_INET, CTL_EOL);
1.116 atatat 1366: sysctl_createv(clog, 0, NULL, NULL,
1367: CTLFLAG_PERMANENT,
1.122 atatat 1368: CTLTYPE_NODE, "udp",
1369: SYSCTL_DESCR("UDPv4 related settings"),
1.114 atatat 1370: NULL, 0, NULL, 0,
1371: CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL);
1372:
1.116 atatat 1373: sysctl_createv(clog, 0, NULL, NULL,
1374: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122 atatat 1375: CTLTYPE_INT, "checksum",
1.123 heas 1376: SYSCTL_DESCR("Compute UDP checksums"),
1.114 atatat 1377: NULL, 0, &udpcksum, 0,
1378: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM,
1379: CTL_EOL);
1.116 atatat 1380: sysctl_createv(clog, 0, NULL, NULL,
1381: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122 atatat 1382: CTLTYPE_INT, "sendspace",
1383: SYSCTL_DESCR("Default UDP send buffer size"),
1.114 atatat 1384: NULL, 0, &udp_sendspace, 0,
1385: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE,
1386: CTL_EOL);
1.116 atatat 1387: sysctl_createv(clog, 0, NULL, NULL,
1388: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.122 atatat 1389: CTLTYPE_INT, "recvspace",
1390: SYSCTL_DESCR("Default UDP receive buffer size"),
1.114 atatat 1391: NULL, 0, &udp_recvspace, 0,
1392: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE,
1393: CTL_EOL);
1.125 thorpej 1394: sysctl_createv(clog, 0, NULL, NULL,
1395: CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1396: CTLTYPE_INT, "do_loopback_cksum",
1397: SYSCTL_DESCR("Perform UDP checksum on loopback"),
1398: NULL, 0, &udp_do_loopback_cksum, 0,
1399: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM,
1400: CTL_EOL);
1.132 atatat 1401: sysctl_createv(clog, 0, NULL, NULL,
1402: CTLFLAG_PERMANENT,
1.134 atatat 1403: CTLTYPE_STRUCT, "pcblist",
1.132 atatat 1404: SYSCTL_DESCR("UDP protocol control block list"),
1405: sysctl_inpcblist, 0, &udbtable, 0,
1406: CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE,
1407: CTL_EOL);
1.139 elad 1408: sysctl_createv(clog, 0, NULL, NULL,
1409: CTLFLAG_PERMANENT,
1410: CTLTYPE_STRUCT, "stats",
1411: SYSCTL_DESCR("UDP statistics"),
1412: NULL, 0, &udpstat, sizeof(udpstat),
1413: CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS,
1414: CTL_EOL);
1.1 cgd 1415: }
1.72 itojun 1416: #endif
1.130 manu 1417:
1418: #if (defined INET && defined IPSEC_NAT_T)
1419: /*
1420: * Returns:
1421: * 1 if the packet was processed
1422: * 0 if normal UDP processing should take place
1.144 manu 1423: * -1 if an error occurent and m was freed
1.130 manu 1424: */
1425: static int
1.144 manu 1426: udp4_espinudp(mp, off, src, so)
1427: struct mbuf **mp;
1.130 manu 1428: int off;
1429: struct sockaddr *src;
1430: struct socket *so;
1431: {
1432: size_t len;
1433: caddr_t data;
1434: struct inpcb *inp;
1435: size_t skip = 0;
1436: size_t minlen;
1437: size_t iphdrlen;
1438: struct ip *ip;
1439: struct mbuf *n;
1.136 manu 1440: struct m_tag *tag;
1441: struct udphdr *udphdr;
1442: u_int16_t sport, dport;
1.144 manu 1443: struct mbuf *m = *mp;
1.130 manu 1444:
1.131 perry 1445: /*
1.130 manu 1446: * Collapse the mbuf chain if the first mbuf is too short
1447: * The longest case is: UDP + non ESP marker + ESP
1448: */
1449: minlen = off + sizeof(u_int64_t) + sizeof(struct esp);
1450: if (minlen > m->m_pkthdr.len)
1451: minlen = m->m_pkthdr.len;
1452:
1453: if (m->m_len < minlen) {
1.144 manu 1454: if ((*mp = m_pullup(m, minlen)) == NULL) {
1.130 manu 1455: printf("udp4_espinudp: m_pullup failed\n");
1.144 manu 1456: return -1;
1.130 manu 1457: }
1.144 manu 1458: m = *mp;
1.130 manu 1459: }
1460:
1.131 perry 1461: len = m->m_len - off;
1.130 manu 1462: data = mtod(m, caddr_t) + off;
1463: inp = sotoinpcb(so);
1464:
1465: /* Ignore keepalive packets */
1466: if ((len == 1) && (data[0] == '\xff')) {
1467: return 1;
1468: }
1469:
1.131 perry 1470: /*
1471: * Check that the payload is long enough to hold
1.130 manu 1472: * an ESP header and compute the length of encapsulation
1.131 perry 1473: * header to remove
1.130 manu 1474: */
1475: if (inp->inp_flags & INP_ESPINUDP) {
1476: u_int32_t *st = (u_int32_t *)data;
1477:
1478: if ((len <= sizeof(struct esp)) || (*st == 0))
1479: return 0; /* Normal UDP processing */
1480:
1481: skip = sizeof(struct udphdr);
1482: }
1483:
1484: if (inp->inp_flags & INP_ESPINUDP_NON_IKE) {
1.142 kleink 1485: u_int32_t *st = (u_int32_t *)data;
1.130 manu 1486:
1487: if ((len <= sizeof(u_int64_t) + sizeof(struct esp))
1.142 kleink 1488: || ((st[0] | st[1]) != 0))
1.130 manu 1489: return 0; /* Normal UDP processing */
1.131 perry 1490:
1.130 manu 1491: skip = sizeof(struct udphdr) + sizeof(u_int64_t);
1492: }
1493:
1494: /*
1.136 manu 1495: * Get the UDP ports. They are handled in network
1496: * order everywhere in IPSEC_NAT_T code.
1497: */
1498: udphdr = (struct udphdr *)(data - skip);
1499: sport = udphdr->uh_sport;
1500: dport = udphdr->uh_dport;
1501:
1502: /*
1.130 manu 1503: * Remove the UDP header (and possibly the non ESP marker)
1504: * IP header lendth is iphdrlen
1.131 perry 1505: * Before:
1.130 manu 1506: * <--- off --->
1507: * +----+------+-----+
1508: * | IP | UDP | ESP |
1509: * +----+------+-----+
1510: * <-skip->
1511: * After:
1512: * +----+-----+
1513: * | IP | ESP |
1514: * +----+-----+
1515: * <-skip->
1516: */
1517: iphdrlen = off - sizeof(struct udphdr);
1518: memmove(mtod(m, caddr_t) + skip, mtod(m, caddr_t), iphdrlen);
1519: m_adj(m, skip);
1520:
1521: ip = mtod(m, struct ip *);
1522: ip->ip_len = htons(ntohs(ip->ip_len) - skip);
1523: ip->ip_p = IPPROTO_ESP;
1524:
1525: /*
1.131 perry 1526: * Copy the mbuf to avoid multiple free, as both
1527: * esp4_input (which we call) and udp_input (which
1.130 manu 1528: * called us) free the mbuf.
1529: */
1530: if ((n = m_dup(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
1531: printf("udp4_espinudp: m_dup failed\n");
1532: return 0;
1533: }
1534:
1.136 manu 1535: /*
1536: * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember
1537: * the source UDP port. This is required if we want
1538: * to select the right SPD for multiple hosts behind
1539: * same NAT
1540: */
1.137 manu 1541: if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS,
1542: sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) {
1543: printf("udp4_espinudp: m_tag_get failed\n");
1.138 manu 1544: m_freem(n);
1.137 manu 1545: return 0;
1546: }
1.136 manu 1547: ((u_int16_t *)(tag + 1))[0] = sport;
1548: ((u_int16_t *)(tag + 1))[1] = dport;
1549: m_tag_prepend(n, tag);
1550:
1.130 manu 1551: esp4_input(n, iphdrlen);
1552:
1553: /* We handled it, it shoudln't be handled by UDP */
1554: return 1;
1555: }
1556: #endif
CVSweb <webmaster@jp.NetBSD.org>