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