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.168.2.1 retrieving revision 1.188 diff -u -p -r1.168.2.1 -r1.188 --- src/sys/netinet/udp_usrreq.c 2008/05/18 12:35:30 1.168.2.1 +++ src/sys/netinet/udp_usrreq.c 2013/06/04 22:47:37 1.188 @@ -1,4 +1,4 @@ -/* $NetBSD: udp_usrreq.c,v 1.168.2.1 2008/05/18 12:35:30 yamt Exp $ */ +/* $NetBSD: udp_usrreq.c,v 1.188 2013/06/04 22:47:37 christos Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -61,9 +61,10 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.168.2.1 2008/05/18 12:35:30 yamt Exp $"); +__KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.188 2013/06/04 22:47:37 christos Exp $"); #include "opt_inet.h" +#include "opt_compat_netbsd.h" #include "opt_ipsec.h" #include "opt_inet_csum.h" #include "opt_ipkdb.h" @@ -117,8 +118,6 @@ __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c #include #endif -#include - #ifdef FAST_IPSEC #include #include @@ -129,12 +128,9 @@ __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c #endif #endif /* FAST_IPSEC */ -#ifdef IPSEC -#include -#include -#include -#include -#endif /* IPSEC */ +#ifdef COMPAT_50 +#include +#endif #ifdef IPKDB #include @@ -152,10 +148,8 @@ struct inpcbtable udbtable; percpu_t *udpstat_percpu; #ifdef INET -#ifdef IPSEC_NAT_T static int udp4_espinudp (struct mbuf **, int, struct sockaddr *, struct socket *); -#endif static void udp4_sendup (struct mbuf *, int, struct sockaddr *, struct socket *); static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *, @@ -227,10 +221,14 @@ EVCNT_ATTACH_STATIC(udp6_swcsum); #endif /* UDP_CSUM_COUNTERS */ +static void sysctl_net_inet_udp_setup(struct sysctllog **); + void udp_init(void) { + sysctl_net_inet_udp_setup(NULL); + in_pcbinit(&udbtable, udbhashsize, udbhashsize); MOWNER_ATTACH(&udp_tx_mowner); @@ -404,22 +402,30 @@ udp_input(struct mbuf *m, ...) UDP_STATINC(UDP_STAT_HDROPS); return; } + if (m == NULL) { + /* + * packet has been processed by ESP stuff - + * e.g. dropped NAT-T-keep-alive-packet ... + */ + return; + } + ip = mtod(m, struct ip *); #ifdef INET6 if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) { struct sockaddr_in6 src6, dst6; - bzero(&src6, sizeof(src6)); + memset(&src6, 0, sizeof(src6)); src6.sin6_family = AF_INET6; src6.sin6_len = sizeof(struct sockaddr_in6); src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff; - bcopy(&ip->ip_src, &src6.sin6_addr.s6_addr[12], + memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src, sizeof(ip->ip_src)); src6.sin6_port = uh->uh_sport; - bzero(&dst6, sizeof(dst6)); + memset(&dst6, 0, sizeof(dst6)); dst6.sin6_family = AF_INET6; dst6.sin6_len = sizeof(struct sockaddr_in6); dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff; - bcopy(&ip->ip_dst, &dst6.sin6_addr.s6_addr[12], + memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst, sizeof(ip->ip_dst)); dst6.sin6_port = uh->uh_dport; @@ -575,12 +581,12 @@ udp6_input(struct mbuf **mp, int *offp, /* * Construct source and dst sockaddrs. */ - bzero(&src, sizeof(src)); + memset(&src, 0, sizeof(src)); src.sin6_family = AF_INET6; src.sin6_len = sizeof(struct sockaddr_in6); src.sin6_addr = ip6->ip6_src; src.sin6_port = uh->uh_sport; - bzero(&dst, sizeof(dst)); + memset(&dst, 0, sizeof(dst)); dst.sin6_family = AF_INET6; dst.sin6_len = sizeof(struct sockaddr_in6); dst.sin6_addr = ip6->ip6_dst; @@ -626,7 +632,7 @@ udp4_sendup(struct mbuf *m, int off /* o return; } -#if defined(IPSEC) || defined(FAST_IPSEC) +#if defined(FAST_IPSEC) /* check AH/ESP integrity. */ if (so != NULL && ipsec4_in_reject_so(m, so)) { IPSEC_STATINC(IPSEC_STAT_IN_POLVIO); @@ -639,6 +645,9 @@ udp4_sendup(struct mbuf *m, int off /* o if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) { if (inp && (inp->inp_flags & INP_CONTROLOPTS +#ifdef SO_OTIMESTAMP + || so->so_options & SO_OTIMESTAMP +#endif || so->so_options & SO_TIMESTAMP)) { struct ip *ip = mtod(n, struct ip *); ip_savecontrol(inp, &opts, ip, n); @@ -673,7 +682,7 @@ udp6_sendup(struct mbuf *m, int off /* o return; in6p = sotoin6pcb(so); -#if defined(IPSEC) || defined(FAST_IPSEC) +#if defined(FAST_IPSEC) /* check AH/ESP integrity. */ if (so != NULL && ipsec6_in_reject_so(m, so)) { IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO); @@ -686,7 +695,10 @@ udp6_sendup(struct mbuf *m, int off /* o if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) { if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS - || in6p->in6p_socket->so_options & SO_TIMESTAMP)) { +#ifdef SO_OTIMESTAMP + || in6p->in6p_socket->so_options & SO_OTIMESTAMP +#endif + || in6p->in6p_socket->so_options & SO_TIMESTAMP)) { struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *); ip6_savecontrol(in6p, &opts, ip6, n); } @@ -789,7 +801,8 @@ udp4_realinput(struct sockaddr_in *src, /* * Locate pcb for datagram. */ - inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport); + inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, + *dport, 0); if (inp == 0) { UDP_STATINC(UDP_STAT_PCBHASHMISS); inp = in_pcblookup_bind(&udbtable, *dst4, *dport); @@ -797,7 +810,6 @@ udp4_realinput(struct sockaddr_in *src, return rcvcnt; } -#ifdef IPSEC_NAT_T /* Handle ESP over UDP */ if (inp->inp_flags & INP_ESPINUDP_ALL) { struct sockaddr *sa = (struct sockaddr *)src; @@ -823,7 +835,12 @@ udp4_realinput(struct sockaddr_in *src, break; } } -#endif + + /* + * Check the minimum TTL for socket. + */ + if (mtod(m, struct ip *)->ip_ttl < inp->inp_ip_minttl) + goto bad; udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket); rcvcnt++; @@ -937,7 +954,7 @@ udp6_realinput(int af, struct sockaddr_i * Locate pcb for datagram. */ in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6, - dport, 0); + dport, 0, 0); if (in6p == 0) { UDP_STATINC(UDP_STAT_PCBHASHMISS); in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0); @@ -1000,14 +1017,13 @@ udp_ctlinput(int cmd, const struct socka } int -udp_ctloutput(int op, struct socket *so, int level, int optname, - struct mbuf **mp) +udp_ctloutput(int op, struct socket *so, struct sockopt *sopt) { int s; int error = 0; - struct mbuf *m; struct inpcb *inp; int family; + int optval; family = so->so_proto->pr_domain->dom_family; @@ -1015,16 +1031,16 @@ udp_ctloutput(int op, struct socket *so, switch (family) { #ifdef INET case PF_INET: - if (level != IPPROTO_UDP) { - error = ip_ctloutput(op, so, level, optname, mp); + if (sopt->sopt_level != IPPROTO_UDP) { + error = ip_ctloutput(op, so, sopt); goto end; } break; #endif #ifdef INET6 case PF_INET6: - if (level != IPPROTO_UDP) { - error = ip6_ctloutput(op, so, level, optname, mp); + if (sopt->sopt_level != IPPROTO_UDP) { + error = ip6_ctloutput(op, so, sopt); goto end; } break; @@ -1037,18 +1053,15 @@ udp_ctloutput(int op, struct socket *so, switch (op) { case PRCO_SETOPT: - m = *mp; inp = sotoinpcb(so); - switch (optname) { + switch (sopt->sopt_name) { case UDP_ENCAP: - if (m == NULL || m->m_len != sizeof(int)) { - error = EINVAL; + error = sockopt_getint(sopt, &optval); + if (error) break; - } - switch(*mtod(m, int *)) { -#ifdef IPSEC_NAT_T + switch(optval) { case 0: inp->inp_flags &= ~INP_ESPINUDP_ALL; break; @@ -1062,20 +1075,16 @@ udp_ctloutput(int op, struct socket *so, inp->inp_flags &= ~INP_ESPINUDP_ALL; inp->inp_flags |= INP_ESPINUDP_NON_IKE; break; -#endif default: error = EINVAL; break; } break; - + default: error = ENOPROTOOPT; break; } - if (m != NULL) { - m_free(m); - } break; default: @@ -1284,6 +1293,7 @@ udp_usrreq(struct socket *so, int req, s { struct in_addr laddr; /* XXX */ + memset(&laddr, 0, sizeof laddr); if (nam) { laddr = inp->inp_laddr; /* XXX */ if ((so->so_state & SS_ISCONNECTED) != 0) { @@ -1356,9 +1366,9 @@ sysctl_net_inet_udp_stats(SYSCTLFN_ARGS) /* * Sysctl for udp variables. */ -SYSCTL_SETUP(sysctl_net_inet_udp_setup, "sysctl net.inet.udp subtree setup") +static void +sysctl_net_inet_udp_setup(struct sysctllog **clog) { - sysctl_createv(clog, 0, NULL, NULL, CTLFLAG_PERMANENT, CTLTYPE_NODE, "net", NULL, @@ -1429,7 +1439,7 @@ udp_statinc(u_int stat) UDP_STATINC(stat); } -#if (defined INET && defined IPSEC_NAT_T) +#if defined(INET) /* * Returns: * 1 if the packet was processed @@ -1447,7 +1457,6 @@ udp4_espinudp(struct mbuf **mp, int off, size_t minlen; size_t iphdrlen; struct ip *ip; - struct mbuf *n; struct m_tag *tag; struct udphdr *udphdr; u_int16_t sport, dport; @@ -1475,6 +1484,8 @@ udp4_espinudp(struct mbuf **mp, int off, /* Ignore keepalive packets */ if ((len == 1) && (*(unsigned char *)data == 0xff)) { + m_free(m); + *mp = NULL; /* avoid any further processiong by caller ... */ return 1; } @@ -1534,16 +1545,9 @@ udp4_espinudp(struct mbuf **mp, int off, ip->ip_p = IPPROTO_ESP; /* - * Copy the mbuf to avoid multiple free, as both - * esp4_input (which we call) and udp_input (which - * called us) free the mbuf. - */ - if ((n = m_dup(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { - printf("udp4_espinudp: m_dup failed\n"); - return 0; - } - - /* + * We have modified the packet - it is now ESP, so we should not + * return to UDP processing ... + * * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember * the source UDP port. This is required if we want * to select the right SPD for multiple hosts behind @@ -1552,20 +1556,21 @@ udp4_espinudp(struct mbuf **mp, int off, if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS, sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) { printf("udp4_espinudp: m_tag_get failed\n"); - m_freem(n); - return 0; + m_freem(m); + return -1; } ((u_int16_t *)(tag + 1))[0] = sport; ((u_int16_t *)(tag + 1))[1] = dport; - m_tag_prepend(n, tag); + m_tag_prepend(m, tag); #ifdef FAST_IPSEC - ipsec4_common_input(n, iphdrlen, IPPROTO_ESP); + ipsec4_common_input(m, iphdrlen, IPPROTO_ESP); #else - esp4_input(n, iphdrlen); + esp4_input(m, iphdrlen); #endif - /* We handled it, it shoudln't be handled by UDP */ + /* We handled it, it shouldn't be handled by UDP */ + *mp = NULL; /* avoid free by caller ... */ return 1; } #endif