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