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.179.4.1 retrieving revision 1.197 diff -u -p -r1.179.4.1 -r1.197 --- src/sys/netinet/udp_usrreq.c 2011/05/31 03:05:08 1.179.4.1 +++ src/sys/netinet/udp_usrreq.c 2014/05/20 19:04:00 1.197 @@ -1,4 +1,4 @@ -/* $NetBSD: udp_usrreq.c,v 1.179.4.1 2011/05/31 03:05:08 rmind Exp $ */ +/* $NetBSD: udp_usrreq.c,v 1.197 2014/05/20 19:04:00 rmind Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -61,7 +61,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.179.4.1 2011/05/31 03:05:08 rmind Exp $"); +__KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.197 2014/05/20 19:04:00 rmind Exp $"); #include "opt_inet.h" #include "opt_compat_netbsd.h" @@ -73,6 +73,7 @@ __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c #include #include #include +#include #include #include #include @@ -118,9 +119,7 @@ __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c #include #endif -#include - -#ifdef FAST_IPSEC +#ifdef IPSEC #include #include #include @@ -128,14 +127,7 @@ __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c #ifdef INET6 #include #endif -#endif /* FAST_IPSEC */ - -#ifdef IPSEC -#include -#include -#include -#include -#endif /* IPSEC */ +#endif /* IPSEC */ #ifdef COMPAT_50 #include @@ -157,7 +149,7 @@ struct inpcbtable udbtable; percpu_t *udpstat_percpu; #ifdef INET -#ifdef IPSEC_NAT_T +#ifdef IPSEC static int udp4_espinudp (struct mbuf **, int, struct sockaddr *, struct socket *); #endif @@ -183,6 +175,12 @@ static void udp_notify (struct inpcb *, #endif int udbhashsize = UDBHASHSIZE; +/* + * For send - really max datagram size; for receive - 40 1K datagrams. + */ +static int udp_sendspace = 9216; +static int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); + #ifdef MBUFTRACE struct mowner udp_mowner = MOWNER_INIT("udp", ""); struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx"); @@ -234,24 +232,35 @@ EVCNT_ATTACH_STATIC(udp6_swcsum); static void sysctl_net_inet_udp_setup(struct sysctllog **); -void -udp_init(void) +static int +do_udpinit(void) { - sysctl_net_inet_udp_setup(NULL); - in_pcbinit(&udbtable, udbhashsize, udbhashsize); + udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS); MOWNER_ATTACH(&udp_tx_mowner); MOWNER_ATTACH(&udp_rx_mowner); MOWNER_ATTACH(&udp_mowner); -#ifdef INET - udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS); -#endif -#ifdef INET6 - udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS); -#endif + return 0; +} + +void +udp_init_common(void) +{ + static ONCE_DECL(doudpinit); + + RUN_ONCE(&doudpinit, do_udpinit); +} + +void +udp_init(void) +{ + + sysctl_net_inet_udp_setup(NULL); + + udp_init_common(); } /* @@ -413,6 +422,14 @@ 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; @@ -635,7 +652,7 @@ udp4_sendup(struct mbuf *m, int off /* o return; } -#if defined(IPSEC) || defined(FAST_IPSEC) +#if defined(IPSEC) /* check AH/ESP integrity. */ if (so != NULL && ipsec4_in_reject_so(m, so)) { IPSEC_STATINC(IPSEC_STAT_IN_POLVIO); @@ -685,7 +702,7 @@ udp6_sendup(struct mbuf *m, int off /* o return; in6p = sotoin6pcb(so); -#if defined(IPSEC) || defined(FAST_IPSEC) +#if defined(IPSEC) /* check AH/ESP integrity. */ if (so != NULL && ipsec6_in_reject_so(m, so)) { IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO); @@ -767,7 +784,7 @@ udp4_realinput(struct sockaddr_in *src, /* * Locate pcb(s) for datagram. */ - CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) { + TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) { inp = (struct inpcb *)inph; if (inp->inp_af != AF_INET) continue; @@ -813,7 +830,7 @@ udp4_realinput(struct sockaddr_in *src, return rcvcnt; } -#ifdef IPSEC_NAT_T +#ifdef IPSEC /* Handle ESP over UDP */ if (inp->inp_flags & INP_ESPINUDP_ALL) { struct sockaddr *sa = (struct sockaddr *)src; @@ -912,7 +929,7 @@ udp6_realinput(int af, struct sockaddr_i /* * Locate pcb(s) for datagram. */ - CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) { + TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) { in6p = (struct in6pcb *)inph; if (in6p->in6p_af != AF_INET6) continue; @@ -1067,7 +1084,6 @@ udp_ctloutput(int op, struct socket *so, break; switch(optval) { -#ifdef IPSEC_NAT_T case 0: inp->inp_flags &= ~INP_ESPINUDP_ALL; break; @@ -1081,13 +1097,12 @@ 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; @@ -1182,25 +1197,66 @@ release: return (error); } -int udp_sendspace = 9216; /* really max datagram size */ -int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); - /* 40 1K datagrams */ +static int +udp_attach(struct socket *so, int proto) +{ + struct inpcb *inp; + int error; + + KASSERT(sotoinpcb(so) == NULL); -/*ARGSUSED*/ -int + /* Assign the lock (must happen even if we will error out). */ + sosetlock(so); + +#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) { + return error; + } + } + + error = in_pcballoc(so, &udbtable); + if (error) { + return error; + } + inp = sotoinpcb(so); + inp->inp_ip.ip_ttl = ip_defttl; + KASSERT(solocked(so)); + + return error; +} + +static void +udp_detach(struct socket *so) +{ + struct inpcb *inp; + + KASSERT(solocked(so)); + inp = sotoinpcb(so); + KASSERT(inp != NULL); + in_pcbdetach(inp); +} + +static int udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, - struct mbuf *control, struct lwp *l) + struct mbuf *control, struct lwp *l) { struct inpcb *inp; - int s; - int error = 0; + int s, error = 0; - if (req == PRU_CONTROL) - return (in_control(so, (long)m, (void *)nam, - (struct ifnet *)control, l)); + KASSERT(req != PRU_ATTACH); + KASSERT(req != PRU_DETACH); + if (req == PRU_CONTROL) { + return in_control(so, (long)m, (void *)nam, + (struct ifnet *)control, l); + } s = splsoftnet(); - if (req == PRU_PURGEIF) { mutex_enter(softnet_lock); in_pcbpurgeif0(&udbtable, (struct ifnet *)control); @@ -1208,19 +1264,16 @@ udp_usrreq(struct socket *so, int req, s in_pcbpurgeif(&udbtable, (struct ifnet *)control); mutex_exit(softnet_lock); splx(s); - return (0); + return 0; } + KASSERT(solocked(so)); inp = sotoinpcb(so); -#ifdef DIAGNOSTIC - if (req != PRU_SEND && req != PRU_SENDOOB && control) - panic("udp_usrreq: unexpected control mbuf"); -#endif - if (req == PRU_ATTACH) { - sosetlock(so); - } else if (inp == 0) { - error = EINVAL; - goto release; + + KASSERT(!control || (req == PRU_SEND || req == PRU_SENDOOB)); + if (inp == NULL) { + splx(s); + return EINVAL; } /* @@ -1228,33 +1281,6 @@ udp_usrreq(struct socket *so, int req, s * the udp pcb queue and/or pcb addresses. */ switch (req) { - - case PRU_ATTACH: - if (inp != 0) { - 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) - break; - } - error = in_pcballoc(so, &udbtable); - if (error) - break; - inp = sotoinpcb(so); - inp->inp_ip.ip_ttl = ip_defttl; - break; - - case PRU_DETACH: - in_pcbdetach(inp); - break; - case PRU_BIND: error = in_pcbbind(inp, nam, l); break; @@ -1300,6 +1326,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,10 +1383,9 @@ udp_usrreq(struct socket *so, int req, s default: panic("udp_usrreq"); } - -release: splx(s); - return (error); + + return error; } static int @@ -1378,11 +1404,6 @@ sysctl_net_inet_udp_setup(struct sysctll sysctl_createv(clog, 0, NULL, NULL, CTLFLAG_PERMANENT, - CTLTYPE_NODE, "net", NULL, - NULL, 0, NULL, 0, - CTL_NET, CTL_EOL); - sysctl_createv(clog, 0, NULL, NULL, - CTLFLAG_PERMANENT, CTLTYPE_NODE, "inet", NULL, NULL, 0, NULL, 0, CTL_NET, PF_INET, CTL_EOL); @@ -1446,7 +1467,7 @@ udp_statinc(u_int stat) UDP_STATINC(stat); } -#if (defined INET && defined IPSEC_NAT_T) +#if defined(INET) && defined(IPSEC) /* * Returns: * 1 if the packet was processed @@ -1464,7 +1485,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; @@ -1492,6 +1512,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; } @@ -1551,16 +1573,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 @@ -1569,20 +1584,32 @@ 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); +#ifdef IPSEC + 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 + +PR_WRAP_USRREQS(udp) +#define udp_attach udp_attach_wrapper +#define udp_detach udp_detach_wrapper +#define udp_usrreq udp_usrreq_wrapper + +const struct pr_usrreqs udp_usrreqs = { + .pr_attach = udp_attach, + .pr_detach = udp_detach, + .pr_generic = udp_usrreq, +};