Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/netinet/udp_usrreq.c,v rcsdiff: /ftp/cvs/cvsroot/src/sys/netinet/udp_usrreq.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.91 retrieving revision 1.107 diff -u -p -r1.91 -r1.107 --- src/sys/netinet/udp_usrreq.c 2001/11/13 00:32:42 1.91 +++ src/sys/netinet/udp_usrreq.c 2003/08/22 21:53:06 1.107 @@ -1,9 +1,9 @@ -/* $NetBSD: udp_usrreq.c,v 1.91 2001/11/13 00:32:42 lukem Exp $ */ +/* $NetBSD: udp_usrreq.c,v 1.107 2003/08/22 21:53:06 itojun Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -15,7 +15,7 @@ * 3. Neither the name of the project nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -41,11 +41,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -65,12 +61,13 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.91 2001/11/13 00:32:42 lukem Exp $"); +__KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.107 2003/08/22 21:53:06 itojun Exp $"); #include "opt_inet.h" #include "opt_ipsec.h" #include "opt_inet_csum.h" #include "opt_ipkdb.h" +#include "opt_mbuftrace.h" #include #include @@ -106,12 +103,10 @@ __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c #include #endif -#ifdef PULLDOWN_TEST #ifndef INET6 /* always need ip6.h for IP6_EXTHDR_GET */ #include #endif -#endif #include "faith.h" #if defined(NFAITH) && NFAITH > 0 @@ -120,6 +115,13 @@ __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c #include +#ifdef FAST_IPSEC +#include +#ifdef INET6 +#include +#endif +#endif /* FAST_IPSEC*/ + #ifdef IPSEC #include #include @@ -139,6 +141,9 @@ int udpcksum = 1; int udpcksum = 0; /* XXX */ #endif +struct inpcbtable udbtable; +struct udpstat udpstat; + #ifdef INET static void udp4_sendup __P((struct mbuf *, int, struct sockaddr *, struct socket *)); @@ -160,6 +165,12 @@ static void udp_notify __P((struct inpcb #endif int udbhashsize = UDBHASHSIZE; +#ifdef MBUFTRACE +struct mowner udp_mowner = { "udp" }; +struct mowner udp_rx_mowner = { "udp", "rx" }; +struct mowner udp_tx_mowner = { "udp", "tx" }; +#endif + #ifdef UDP_CSUM_COUNTERS #include @@ -194,6 +205,10 @@ udp_init() evcnt_attach_static(&udp_hwcsum_data); evcnt_attach_static(&udp_swcsum); #endif /* UDP_CSUM_COUNTERS */ + + MOWNER_ATTACH(&udp_tx_mowner); + MOWNER_ATTACH(&udp_rx_mowner); + MOWNER_ATTACH(&udp_mowner); } #ifdef INET @@ -210,55 +225,29 @@ udp_input(m, va_alist) struct sockaddr_in src, dst; struct ip *ip; struct udphdr *uh; - int iphlen, proto; + int iphlen; int len; int n; + u_int16_t ip_len; va_start(ap, m); iphlen = va_arg(ap, int); - proto = va_arg(ap, int); + (void)va_arg(ap, int); /* ignore value, advance ap */ va_end(ap); + MCLAIM(m, &udp_rx_mowner); udpstat.udps_ipackets++; -#ifndef PULLDOWN_TEST - /* - * Strip IP options, if any; should skip this, - * make available to user, and use on returned packets, - * but we don't yet have a way to check the checksum - * with options still present. - */ - if (iphlen > sizeof (struct ip)) { - ip_stripoptions(m, (struct mbuf *)0); - iphlen = sizeof(struct ip); - } -#else - /* - * we may enable the above code if we save and pass IPv4 options - * to the userland. - */ -#endif - /* * Get IP and UDP header together in first mbuf. */ ip = mtod(m, struct ip *); -#ifndef PULLDOWN_TEST - if (m->m_len < iphlen + sizeof(struct udphdr)) { - if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { - udpstat.udps_hdrops++; - return; - } - ip = mtod(m, struct ip *); - } - uh = (struct udphdr *)((caddr_t)ip + iphlen); -#else IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr)); if (uh == NULL) { udpstat.udps_hdrops++; return; } -#endif + KASSERT(UDP_HDR_ALIGNED_P(uh)); /* destination port of 0 is illegal, based on RFC768. */ if (uh->uh_dport == 0) @@ -268,13 +257,14 @@ udp_input(m, va_alist) * Make mbuf data length reflect UDP length. * If not enough data to reflect UDP length, drop. */ + ip_len = ntohs(ip->ip_len); len = ntohs((u_int16_t)uh->uh_ulen); - if (ip->ip_len != iphlen + len) { - if (ip->ip_len < iphlen + len || len < sizeof(struct udphdr)) { + if (ip_len != iphlen + len) { + if (ip_len < iphlen + len || len < sizeof(struct udphdr)) { udpstat.udps_badlen++; goto bad; } - m_adj(m, iphlen + len - ip->ip_len); + m_adj(m, iphlen + len - ip_len); } /* @@ -288,11 +278,16 @@ udp_input(m, va_alist) UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad); goto badcsum; - case M_CSUM_UDPv4|M_CSUM_DATA: + case M_CSUM_UDPv4|M_CSUM_DATA: { + u_int32_t hw_csum = m->m_pkthdr.csum_data; UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data); - if ((m->m_pkthdr.csum_data ^ 0xffff) != 0) + if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) + hw_csum = in_cksum_phdr(ip->ip_src.s_addr, ip->ip_dst.s_addr, + htonl(hw_csum + ntohs(ip->ip_len) + IPPROTO_UDP)); + if ((hw_csum ^ 0xffff) != 0) goto badcsum; break; + } case M_CSUM_UDPv4: /* Checksum was okay. */ @@ -389,9 +384,6 @@ udp6_input(mp, offp, proto) struct udphdr *uh; u_int32_t plen, ulen; -#ifndef PULLDOWN_TEST - IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE); -#endif ip6 = mtod(m, struct ip6_hdr *); #if defined(NFAITH) && 0 < NFAITH @@ -406,15 +398,12 @@ udp6_input(mp, offp, proto) /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */ plen = m->m_pkthdr.len - off; -#ifndef PULLDOWN_TEST - uh = (struct udphdr *)((caddr_t)ip6 + off); -#else IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr)); if (uh == NULL) { ip6stat.ip6s_tooshort++; return IPPROTO_DONE; } -#endif + KASSERT(UDP_HDR_ALIGNED_P(uh)); ulen = ntohs((u_short)uh->uh_ulen); /* * RFC2675 section 4: jumbograms will have 0 in the UDP header field, @@ -494,9 +483,6 @@ udp4_sendup(m, off, src, so) struct mbuf *opts = NULL; struct mbuf *n; struct inpcb *inp = NULL; -#ifdef INET6 - struct in6pcb *in6p = NULL; -#endif if (!so) return; @@ -506,14 +492,13 @@ udp4_sendup(m, off, src, so) break; #ifdef INET6 case AF_INET6: - in6p = sotoin6pcb(so); break; #endif default: return; } -#ifdef IPSEC +#if defined(IPSEC) || defined(FAST_IPSEC) /* check AH/ESP integrity. */ if (so != NULL && ipsec4_in_reject_so(m, so)) { ipsecstat.in_polvio++; @@ -559,7 +544,7 @@ udp6_sendup(m, off, src, so) return; in6p = sotoin6pcb(so); -#ifdef IPSEC +#if defined(IPSEC) || defined(FAST_IPSEC) /* check AH/ESP integrity. */ if (so != NULL && ipsec6_in_reject_so(m, so)) { ipsec6stat.in_polvio++; @@ -612,7 +597,6 @@ udp4_realinput(src, dst, m, off) if (IN_MULTICAST(dst4->s_addr) || in_broadcast(*dst4, m->m_pkthdr.rcvif)) { - struct inpcb *last; /* * Deliver a multicast or broadcast datagram to *all* sockets * for which the local and remote addresses and ports match @@ -630,7 +614,7 @@ udp4_realinput(src, dst, m, off) */ /* - * KAME note: usually we drop udpiphdr from mbuf here. + * KAME note: traditionally we dropped udpiphdr from mbuf here. * we need udpiphdr for IPsec processing so we do that later. */ /* @@ -649,7 +633,6 @@ udp4_realinput(src, dst, m, off) continue; } - last = inp; udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket); rcvcnt++; @@ -718,7 +701,6 @@ udp6_realinput(af, src, dst, m, off) if (IN6_IS_ADDR_MULTICAST(&dst6) || (af == AF_INET && IN_MULTICAST(dst4->s_addr))) { - struct in6pcb *last; /* * Deliver a multicast or broadcast datagram to *all* sockets * for which the local and remote addresses and ports match @@ -736,7 +718,7 @@ udp6_realinput(af, src, dst, m, off) */ /* - * KAME note: usually we drop udpiphdr from mbuf here. + * KAME note: traditionally we dropped udpiphdr from mbuf here. * we need udpiphdr for IPsec processing so we do that later. */ /* @@ -749,8 +731,7 @@ udp6_realinput(af, src, dst, m, off) if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6)) continue; - } - else { + } else { if (IN6_IS_ADDR_V4MAPPED(&dst6) && (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) continue; @@ -759,14 +740,12 @@ udp6_realinput(af, src, dst, m, off) if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &src6) || in6p->in6p_fport != sport) continue; - } - else { + } else { if (IN6_IS_ADDR_V4MAPPED(&src6) && (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) continue; } - last = in6p; udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket); rcvcnt++; @@ -871,6 +850,7 @@ udp_output(m, va_alist) int error = 0; va_list ap; + MCLAIM(m, &udp_tx_mowner); va_start(ap, m); inp = va_arg(ap, struct inpcb *); va_end(ap); @@ -889,7 +869,7 @@ udp_output(m, va_alist) * Compute the packet length of the IP header, and * punt if the length looks bogus. */ - if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) { + if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { error = EMSGSIZE; goto release; } @@ -921,7 +901,7 @@ udp_output(m, va_alist) m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); } else ui->ui_sum = 0; - ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; + ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len); ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ udpstat.udps_opackets++; @@ -935,7 +915,7 @@ udp_output(m, va_alist) return (ip_output(m, inp->inp_options, &inp->inp_route, inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), - inp->inp_moptions)); + inp->inp_moptions, inp->inp_socket)); release: m_freem(m); @@ -991,6 +971,11 @@ udp_usrreq(so, req, m, nam, control, p) error = EISCONN; break; } +#ifdef MBUFTRACE + so->so_mowner = &udp_mowner; + so->so_rcv.sb_mowner = &udp_rx_mowner; + so->so_snd.sb_mowner = &udp_tx_mowner; +#endif if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { error = soreserve(so, udp_sendspace, udp_recvspace); if (error) @@ -1031,6 +1016,11 @@ udp_usrreq(so, req, m, nam, control, p) so->so_state &= ~SS_ISCONNECTED; /* XXX */ in_pcbdisconnect(inp); inp->inp_laddr = zeroin_addr; /* XXX */ + if (inp->inp_ia != NULL) { + LIST_REMOVE(inp, inp_ialink); + IFAFREE(&inp->inp_ia->ia_ifa); + inp->inp_ia = NULL; + } in_pcbstate(inp, INP_BOUND); /* XXX */ break; @@ -1059,11 +1049,8 @@ udp_usrreq(so, req, m, nam, control, p) goto die; } error = in_pcbconnect(inp, nam); - if (error) { - die: - m_freem(m); - break; - } + if (error) + goto die; } else { if ((so->so_state & SS_ISCONNECTED) == 0) { error = ENOTCONN; @@ -1071,11 +1058,20 @@ udp_usrreq(so, req, m, nam, control, p) } } error = udp_output(m, inp); + m = NULL; if (nam) { in_pcbdisconnect(inp); inp->inp_laddr = laddr; /* XXX */ in_pcbstate(inp, INP_BOUND); /* XXX */ } + die: + if (inp->inp_ia != NULL && in_nullhost(inp->inp_laddr)) { + LIST_REMOVE(inp, inp_ialink); + IFAFREE(&inp->inp_ia->ia_ifa); + inp->inp_ia = NULL; + } + if (m) + m_freem(m); } break; @@ -1136,7 +1132,7 @@ udp_sysctl(name, namelen, oldp, oldlenp, return (sysctl_int(oldp, oldlenp, newp, newlen, &udp_sendspace)); case UDPCTL_RECVSPACE: - return (sysctl_int(oldp, oldlenp, newp, newlen, + return (sysctl_int(oldp, oldlenp, newp, newlen, &udp_recvspace)); default: return (ENOPROTOOPT);