[packages] natpmp: several fixes

- use service wrappers to launch natpmp, its builtin daemon setup is broken
- rework uci config to allow logical ifnames
- change and fix default config to use abstract "lan" and "wan" instead of hardcoded (and improperly formatted) device names

git-svn-id: svn://svn.openwrt.org/openwrt/packages@31644 3c298f89-4303-0410-b956-a3cf2f4a3e73
This commit is contained in:
jow 2012-05-07 18:21:47 +00:00
parent 6a421ce2a8
commit f90214ebbd
3 changed files with 49 additions and 37 deletions

View File

@ -1,5 +1,5 @@
# #
# Copyright (C) 2007-2011 OpenWrt.org # Copyright (C) 2007-2012 OpenWrt.org
# #
# This is free software, licensed under the GNU General Public License v2. # This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information. # See /LICENSE for more information.
@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
PKG_NAME:=natpmp PKG_NAME:=natpmp
PKG_VERSION:=0.2.3 PKG_VERSION:=0.2.3
PKG_RELEASE:=3 PKG_RELEASE:=4
PKG_SOURCE_URL:=http://download.savannah.nongnu.org/releases/natpmp/ PKG_SOURCE_URL:=http://download.savannah.nongnu.org/releases/natpmp/
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.gz

View File

@ -1,4 +1,4 @@
config natpmp config natpmp
option outbound_interface vlan0 option outbound_interface wan
option inbound_interfaces br-lan eth1 option inbound_interfaces lan
option iptables_chain natpmp option iptables_chain natpmp

View File

@ -2,6 +2,9 @@
START=70 START=70
SERVICE_DAEMONIZE=1
SERVICE_WRITE_PID=1
IP=$(which ip) IP=$(which ip)
IPTABLES=$(which iptables) IPTABLES=$(which iptables)
NATPMP=/usr/sbin/natpmp NATPMP=/usr/sbin/natpmp
@ -9,7 +12,7 @@ PIDFILE=/var/run/natpmp.pid
natpmp_config() { natpmp_config() {
local cfg="$1" local cfg="$1"
config_get PUBLIC_IF "$cfg" outbound_interface config_get PUBLIC_IF "$cfg" outbound_interface
config_get PRIVATE_IFS "$cfg" inbound_interfaces config_get PRIVATE_IFS "$cfg" inbound_interfaces
config_get IPTABLES_CHAIN "$cfg" iptables_chain config_get IPTABLES_CHAIN "$cfg" iptables_chain
@ -18,46 +21,55 @@ natpmp_config() {
start() { start() {
config_load natpmp config_load natpmp
config_foreach natpmp_config natpmp config_foreach natpmp_config natpmp
# Flush all the rules in the natpmp chain, or create it, if it doesn't exists.
$IPTABLES -t nat -F $IPTABLES_CHAIN 2>/dev/null || \
$IPTABLES -t nat -N $IPTABLES_CHAIN
# Handle all incoming connections in the natpmp chain. include /lib/network
$IPTABLES -t nat -D PREROUTING -j $IPTABLES_CHAIN 2>/dev/null || true scan_interfaces
$IPTABLES -t nat -A PREROUTING -j $IPTABLES_CHAIN
# Iterate through the private interfaces. # Flush all the rules in the natpmp chain, or create it, if it doesn't exists.
BIND_ARGS="" $IPTABLES -t nat -F $IPTABLES_CHAIN 2>/dev/null || \
for IF in $PRIVATE_IFS; do $IPTABLES -t nat -N $IPTABLES_CHAIN
# Get the IP address of this interface.
ADDR=`$IP addr show dev $IF 2>/dev/null | grep "^ *inet .* $IF\$" | cut -d " " -f 6 | cut -d / -f 1`
if [ -n "$ADDR" ] ; then
# Add the IP address to the argument list.
BIND_ARGS="$BIND_ARGS -a $ADDR"
else
echo "Could not get IP address of interface $IF. Skipping." >&2
fi
done
if [ -z "$BIND_ARGS" ] ; then # Handle all incoming connections in the natpmp chain.
echo "No IP addresses to bind to. Exiting." >&2 $IPTABLES -t nat -D PREROUTING -j $IPTABLES_CHAIN 2>/dev/null || true
exit 1 $IPTABLES -t nat -A PREROUTING -j $IPTABLES_CHAIN
fi
$NATPMP -p $PIDFILE -b -i "$PUBLIC_IF" $BIND_ARGS -- "$IPTABLES_CHAIN" # Iterate through the private interfaces.
BIND_ARGS=""
for IF in $PRIVATE_IFS; do
config_get IF "$IF" ifname "$IF"
# Get the IP address of this interface.
ADDR=`$IP addr show dev $IF 2>/dev/null | grep "^ *inet .* $IF\$" | cut -d " " -f 6 | cut -d / -f 1`
if [ -n "$ADDR" ] ; then
# Add the IP address to the argument list.
BIND_ARGS="$BIND_ARGS -a $ADDR"
else
echo "Could not get IP address of interface $IF. Skipping." >&2
fi
done
if [ -z "$BIND_ARGS" ] ; then
echo "No IP addresses to bind to. Exiting." >&2
exit 1
fi
config_get PUBLIC_IF "$PUBLIC_IF" ifname "$PUBLIC_IF"
SERVICE_PID_FILE="$PIDFILE"
service_start $NATPMP -i "$PUBLIC_IF" $BIND_ARGS -- "$IPTABLES_CHAIN"
} }
stop() { stop() {
config_load natpmp config_load natpmp
config_foreach natpmp_config natpmp config_foreach natpmp_config natpmp
# Unlink chain # Unlink chain
$IPTABLES -t nat -D PREROUTING -j $IPTABLES_CHAIN 2>/dev/null || true $IPTABLES -t nat -D PREROUTING -j $IPTABLES_CHAIN 2>/dev/null || true
# Flush all the rules in the natpmp chain # Flush all the rules in the natpmp chain
$IPTABLES -t nat -F $IPTABLES_CHAIN 2>/dev/null && \ $IPTABLES -t nat -F $IPTABLES_CHAIN 2>/dev/null && \
$IPTABLES -t nat -X $IPTABLES_CHAIN $IPTABLES -t nat -X $IPTABLES_CHAIN
kill $(cat $PIDFILE) SERVICE_PID_FILE="$PIDFILE"
service_stop $NATPMP
} }