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