miniupnpd: add CHECK_PORTINUSE to enable/disable port_in_use()

This commit is contained in:
Thomas Bernard 2014-03-13 14:48:52 +01:00
parent 06764123fb
commit c4e63048c4
5 changed files with 36 additions and 12 deletions

View File

@ -14,6 +14,7 @@ case "$argv" in
--leasefile) LEASEFILE=1 ;; --leasefile) LEASEFILE=1 ;;
--vendorcfg) VENDORCFG=1 ;; --vendorcfg) VENDORCFG=1 ;;
--pcp-peer) PCP_PEER=1 ;; --pcp-peer) PCP_PEER=1 ;;
--portinuse) PORTINUSE=1 ;;
--help|-h) --help|-h)
echo "Usage : $0 [options]" echo "Usage : $0 [options]"
echo " --ipv6 enable IPv6" echo " --ipv6 enable IPv6"
@ -22,6 +23,7 @@ case "$argv" in
echo " --leasefile enable lease file" echo " --leasefile enable lease file"
echo " --vendorcfg enable configuration of manufacturer info" echo " --vendorcfg enable configuration of manufacturer info"
echo " --pcp-peer enable PCP PEER operation" echo " --pcp-peer enable PCP PEER operation"
echo " --portinuse enable port in use check"
exit 1 exit 1
;; ;;
*) *)
@ -395,6 +397,14 @@ else
fi fi
echo "" >> ${CONFIGFILE} echo "" >> ${CONFIGFILE}
echo "/* Uncomment the following line to enable port in use check */" >> ${CONFIGFILE}
if [ -n "$PORTINUSE" ]; then
echo "#define CHECK_PORTINUSE" >> ${CONFIGFILE}
else
echo "/*#define CHECK_PORTINUSE*/" >> ${CONFIGFILE}
fi
echo "" >> ${CONFIGFILE}
echo "/* Define one or none of the two following macros in order to make some" >> ${CONFIGFILE} echo "/* Define one or none of the two following macros in order to make some" >> ${CONFIGFILE}
echo " * clients happy. It will change the XML Root Description of the IGD." >> ${CONFIGFILE} echo " * clients happy. It will change the XML Root Description of the IGD." >> ${CONFIGFILE}
echo " * Enabling the Layer3Forwarding Service seems to be the more compatible" >> ${CONFIGFILE} echo " * Enabling the Layer3Forwarding Service seems to be the more compatible" >> ${CONFIGFILE}
@ -497,7 +507,7 @@ else
fi fi
echo "" >> ${CONFIGFILE} echo "" >> ${CONFIGFILE}
echo "#endif" >> ${CONFIGFILE} echo "#endif /* ${CONFIGMACRO} */" >> ${CONFIGFILE}
${MV} ${CONFIGFILE} ${CONFIGFILE_FINAL} ${MV} ${CONFIGFILE} ${CONFIGFILE_FINAL}

View File

@ -292,12 +292,14 @@ void ProcessIncomingNATPMPPacket(int s, unsigned char *msg_buff, int len,
continue; continue;
} }
any_eport_allowed = 1; /* at lease one eport is allowed */ any_eport_allowed = 1; /* at lease one eport is allowed */
#ifdef CHECK_PORTINUSE
if (port_in_use(ext_if_name, eport, proto, senderaddrstr, iport)) { if (port_in_use(ext_if_name, eport, proto, senderaddrstr, iport)) {
syslog(LOG_INFO, "port %hu protocol %s already in use", eport, (proto==IPPROTO_TCP)?"tcp":"udp"); syslog(LOG_INFO, "port %hu protocol %s already in use", eport, (proto==IPPROTO_TCP)?"tcp":"udp");
eport++; eport++;
if(eport == 0) eport++; /* skip port zero */ if(eport == 0) eport++; /* skip port zero */
continue; continue;
} }
#endif
r = get_redirect_rule(ext_if_name, eport, proto, r = get_redirect_rule(ext_if_name, eport, proto,
iaddr_old, sizeof(iaddr_old), iaddr_old, sizeof(iaddr_old),
&iport_old, 0, 0, 0, 0, &iport_old, 0, 0, 0, 0,

View File

@ -1,6 +1,6 @@
/* */ /* $Id $ */
/* MiniUPnP project /* MiniUPnP project
* (c) 2007-2013 Thomas Bernard * (c) 2007-2014 Thomas Bernard
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* This software is subject to the conditions detailed * This software is subject to the conditions detailed
* in the LICENCE file provided within the distribution */ * in the LICENCE file provided within the distribution */
@ -21,10 +21,16 @@
#include "getifaddr.h" #include "getifaddr.h"
#include "portinuse.h" #include "portinuse.h"
#if defined(USE_NETFILTER)
#include "netfilter/iptcrdr.h" #include "netfilter/iptcrdr.h"
#endif
#ifdef CHECK_PORTINUSE
#if defined(USE_NETFILTER)
/* Hardcoded for now. Ideally would come from .conf file */ /* Hardcoded for now. Ideally would come from .conf file */
char *chains_to_check[] = { "PREROUTING" , 0 }; char *chains_to_check[] = { "PREROUTING" , 0 };
#endif
int port_in_use(const char *if_name, unsigned eport, int proto, const char *iaddr, unsigned iport) int port_in_use(const char *if_name, unsigned eport, int proto, const char *iaddr, unsigned iport)
{ {
@ -72,6 +78,7 @@ int port_in_use(const char *if_name, unsigned eport, int proto, const char *iadd
} }
fclose(f); fclose(f);
#if defined(USE_NETFILTER)
if (!found) { if (!found) {
char iaddr_old[16]; char iaddr_old[16];
unsigned short iport_old; unsigned short iport_old;
@ -92,6 +99,7 @@ int port_in_use(const char *if_name, unsigned eport, int proto, const char *iadd
i++; i++;
} }
} }
#endif /* USE_NETFILTER */
return found; return found;
} }
#endif /* CHECK_PORTINUSE */

View File

@ -1,19 +1,22 @@
/* */ /* $Id $ */
/* MiniUPnP project /* MiniUPnP project
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
* (c) 2006-2011 Thomas Bernard * (c) 2006-2014 Thomas Bernard
* This software is subject to the conditions detailed * This software is subject to the conditions detailed
* in the LICENCE file provided within the distribution */ * in the LICENCE file provided within the distribution */
#ifndef __PORTINUSE_H__ #ifndef __PORTINUSE_H__
#define __PORTINUSE_H__ #define __PORTINUSE_H__
#ifdef CHECK_PORTINUSE
/* portinuse() /* portinuse()
* determine wither a port is already in use * determine wether a port is already in use
* on a given interface. * on a given interface.
* returns: 0 not in use, 1 in use */ * returns: 0 not in use, 1 in use */
int int
port_in_use(const char *if_name, unsigned port, int proto, const char *iaddr, unsigned iport); port_in_use(const char *if_name,
unsigned port, int proto,
const char *iaddr, unsigned iport);
#endif /* CHECK_PORTINUSE */
#endif #endif

View File

@ -295,10 +295,11 @@ upnp_redirect(const char * rhost, unsigned short eport,
eport, protocol, iaddr_old, iport_old); eport, protocol, iaddr_old, iport_old);
return -2; return -2;
} }
} #ifdef CHECK_PORTINUSE
else if (port_in_use(ext_if_name, eport, proto, iaddr, iport)) { } else if (port_in_use(ext_if_name, eport, proto, iaddr, iport)) {
syslog(LOG_INFO, "port %hu protocol %s already in use", eport, protocol); syslog(LOG_INFO, "port %hu protocol %s already in use", eport, protocol);
return -2; return -2;
#endif /* CHECK_PORTINUSE */
} else { } else {
timestamp = (leaseduration > 0) ? time(NULL) + leaseduration : 0; timestamp = (leaseduration > 0) ? time(NULL) + leaseduration : 0;
syslog(LOG_INFO, "redirecting port %hu to %s:%hu protocol %s for: %s", syslog(LOG_INFO, "redirecting port %hu to %s:%hu protocol %s for: %s",