1 #!/bin/sh /etc/rc.common
9 IPTABLES=$(which iptables)
10 NATPMP=/usr/sbin/natpmp
11 PIDFILE=/var/run/natpmp.pid
16 config_get PUBLIC_IF "$cfg" outbound_interface
17 config_get PRIVATE_IFS "$cfg" inbound_interfaces
18 config_get IPTABLES_CHAIN "$cfg" iptables_chain
23 config_foreach natpmp_config natpmp
25 . /lib/functions/network.sh
27 # Flush all the rules in the natpmp chain, or create it, if it doesn't exists.
28 $IPTABLES -t nat -F $IPTABLES_CHAIN 2>/dev/null || \
29 $IPTABLES -t nat -N $IPTABLES_CHAIN
31 # Handle all incoming connections in the natpmp chain.
32 $IPTABLES -t nat -D PREROUTING -j $IPTABLES_CHAIN 2>/dev/null || true
33 $IPTABLES -t nat -A PREROUTING -j $IPTABLES_CHAIN
35 # Iterate through the private interfaces.
37 for IF in $PRIVATE_IFS; do
39 network_get_device dev "$IF" || dev="$IF"
41 # Get the IP address of this interface.
42 ADDR=`$IP addr show dev $dev 2>/dev/null | grep "^ *inet .* $dev\$" | cut -d " " -f 6 | cut -d / -f 1`
43 if [ -n "$ADDR" ] ; then
44 # Add the IP address to the argument list.
45 BIND_ARGS="$BIND_ARGS -a $ADDR"
47 echo "Could not get IP address of interface $dev. Skipping." >&2
51 if [ -z "$BIND_ARGS" ] ; then
52 echo "No IP addresses to bind to. Exiting." >&2
57 network_get_device pubdev "$PUBLIC_IF" || pubdev="$PUBLIC_IF"
59 SERVICE_PID_FILE="$PIDFILE"
60 service_start $NATPMP -i "$pubdev" $BIND_ARGS -- "$IPTABLES_CHAIN"
65 config_foreach natpmp_config natpmp
68 $IPTABLES -t nat -D PREROUTING -j $IPTABLES_CHAIN 2>/dev/null || true
70 # Flush all the rules in the natpmp chain
71 $IPTABLES -t nat -F $IPTABLES_CHAIN 2>/dev/null && \
72 $IPTABLES -t nat -X $IPTABLES_CHAIN
74 SERVICE_PID_FILE="$PIDFILE"