branch Attitude Adjustment packages
[12.09/packages.git] / net / miniupnpd / files / miniupnpd.firewall.hotplug
1 #!/bin/sh
2
3 miniupnpd_add_rules() {
4         local zone="$1"
5         local network="$2"
6         local iface="$3"
7
8         miniupnpd_remove_rules
9
10         logger -t miniupnpd "adding firewall rules for $iface to zone $zone"
11
12         iptables -t nat -N MINIUPNPD 2>/dev/null
13         iptables -t nat -I zone_${zone}_prerouting -i $iface -j MINIUPNPD
14         iptables -t filter -N MINIUPNPD 2>/dev/null
15         iptables -t filter -I zone_${zone}_forward -i $iface ! -o $iface -j MINIUPNPD
16
17         uci_set_state upnpd state "" state
18         uci_set_state upnpd state zone "$zone"
19         uci_set_state upnpd state ifname "$iface"
20         uci_set_state upnpd state network "$network"
21 }
22
23 miniupnpd_remove_rules() {
24         local zone="$(uci_get_state upnpd state zone)"
25         local iface="$(uci_get_state upnpd state ifname)"
26
27         [ -n "$zone" ] && [ -n "$iface" ] && {
28                 logger -t miniupnpd "removing firewall rules for $iface from zone $zone"
29
30                 while iptables -t nat -D zone_${zone}_prerouting \
31                         -i $iface -j MINIUPNPD 2>/dev/null; do :; done
32
33                 while iptables -t filter -D zone_${zone}_forward \
34                         -i $iface ! -o $iface -j MINIUPNPD 2>/dev/null; do :; done
35         }
36
37         uci_revert_state upnpd
38 }
39
40 /etc/init.d/miniupnpd enabled && [ -n "`pidof miniupnpd`" ] && {
41
42         local extif="$(uci_get upnpd config external_iface)"
43         local curif="$(uci_get_state upnpd state network)"
44
45         if [ "$ACTION" = "add" ] && [ "$INTERFACE" = "$extif" ]; then
46                 miniupnpd_add_rules "$ZONE" "$INTERFACE" "$DEVICE"
47         elif [ "$ACTION" = "remove" ] && [ "$INTERFACE" = "$curif" ]; then
48                 miniupnpd_remove_rules
49         fi
50 }