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