[package] firewall:
[openwrt/svn-archive/archive.git] / package / firewall / files / lib / core_interface.sh
1 # Copyright (C) 2009-2010 OpenWrt.org
2
3 fw_configure_interface() {
4 local iface=$1
5 local action=$2
6 local ifname=$3
7 local aliasnet=$4
8
9 [ "$action" == "add" ] && {
10 local status=$(uci_get_state network "$iface" up 0)
11 [ "$status" == 1 ] || [ -n "$aliasnet" ] || return 0
12 }
13
14 [ -n "$ifname" ] || {
15 ifname=$(uci_get_state network "$iface" ifname "$iface")
16 ifname="${ifname%%:*}"
17 }
18
19 [ "$ifname" == "lo" ] && return 0
20
21 fw_callback pre interface
22
23 fw__do_rules() {
24 local action=$1
25 local zone=$2
26 local chain=zone_${zone}
27 local ifname=$3
28 local subnet=$4
29
30 local inet onet
31 local mode=$(fw_get_family_mode x $zone i)
32
33 case "$mode/$subnet" in
34 # Zone supports v6 only or dual, need v6
35 G6/*:*|i/*:*)
36 inet="-s $subnet -d ::/0"
37 onet="-s ::/0 -d $subnet"
38 mode=6
39 ;;
40
41 # Zone supports v4 only or dual, need v4
42 G4/*.*.*.*|i/*.*.*.*)
43 inet="-s $subnet -d 0.0.0.0/0"
44 onet="-s 0.0.0.0/0 -d $subnet"
45 mode=4
46 ;;
47
48 # Need v6 while zone is v4
49 */*:*) fw_log info "zone $zone does not support IPv6 address family, skipping"; return ;;
50
51 # Need v4 while zone is v6
52 */*.*) fw_log info "zone $zone does not support IPv4 address family, skipping"; return ;;
53 esac
54
55 fw $action $mode f ${chain}_ACCEPT ACCEPT $ { -o "$ifname" $onet }
56 fw $action $mode f ${chain}_ACCEPT ACCEPT $ { -i "$ifname" $inet }
57 fw $action $mode f ${chain}_DROP DROP $ { -o "$ifname" $onet }
58 fw $action $mode f ${chain}_DROP DROP $ { -i "$ifname" $inet }
59 fw $action $mode f ${chain}_REJECT reject $ { -o "$ifname" $onet }
60 fw $action $mode f ${chain}_REJECT reject $ { -i "$ifname" $inet }
61
62 fw $action $mode n ${chain}_nat MASQUERADE $ { -o "$ifname" $onet }
63 fw $action $mode f ${chain}_MSSFIX TCPMSS $ { -o "$ifname" -p tcp --tcp-flags SYN,RST SYN --clamp-mss-to-pmtu $onet }
64
65 fw $action $mode f input ${chain} $ { -i "$ifname" $inet }
66 fw $action $mode f forward ${chain}_forward $ { -i "$ifname" $inet }
67 fw $action $mode n PREROUTING ${chain}_prerouting $ { -i "$ifname" $inet }
68 fw $action $mode r PREROUTING ${chain}_notrack $ { -i "$ifname" $inet }
69 }
70
71 local old_zones old_ifname old_subnets
72 config_get old_zones core "${iface}_zone"
73 [ -n "$old_zones" ] && {
74 config_get old_ifname core "${iface}_ifname"
75 config_get old_subnets core "${iface}_subnets"
76
77 local z
78 for z in $old_zones; do
79 local n
80 for n in ${old_subnets:-""}; do
81 fw_log info "removing $iface ($old_ifname${n:+ alias $n}) from zone $z"
82 fw__do_rules del $z $old_ifname $n
83 done
84
85 [ -n "$old_subnets" ] || ACTION=remove ZONE="$z" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
86 done
87
88 local old_aliases
89 config_get old_aliases core "${iface}_aliases"
90
91 local a
92 for a in $old_aliases; do
93 fw_configure_interface "$a" del "$old_ifname"
94 done
95
96 uci_revert_state firewall core "${iface}_zone"
97 uci_revert_state firewall core "${iface}_ifname"
98 uci_revert_state firewall core "${iface}_subnets"
99 uci_revert_state firewall core "${iface}_aliases"
100 }
101
102 [ "$action" == del ] && return
103
104 [ -z "$aliasnet" ] && {
105 local aliases
106 config_get aliases "$iface" aliases
107
108 local a
109 for a in $aliases; do
110 local ipaddr netmask ip6addr
111 config_get ipaddr "$a" ipaddr
112 config_get netmask "$a" netmask
113 config_get ip6addr "$a" ip6addr
114
115 [ -n "$ipaddr" ] && fw_configure_interface "$a" add "" "$ipaddr${netmask:+/$netmask}"
116 [ -n "$ip6addr" ] && fw_configure_interface "$a" add "" "$ip6addr"
117 done
118
119 fw_sysctl_interface $ifname
120 fw_callback post interface
121
122 uci_set_state firewall core "${iface}_aliases" "$aliases"
123 } || {
124 local subnets=
125 config_get subnets core "${iface}_subnets"
126 append subnets "$aliasnet"
127
128 config_set core "${iface}_subnets" "$subnets"
129 uci_set_state firewall core "${iface}_subnets" "$subnets"
130 }
131
132 local new_zones=
133 load_zone() {
134 fw_config_get_zone "$1"
135 list_contains zone_network "$iface" || return
136
137 fw_log info "adding $iface ($ifname${aliasnet:+ alias $aliasnet}) to zone $zone_name"
138 fw__do_rules add ${zone_name} "$ifname" $aliasnet
139 append new_zones $zone_name
140
141 [ -n "$aliasnet" ] || ACTION=add ZONE="$zone_name" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
142 }
143 config_foreach load_zone zone
144
145 uci_set_state firewall core "${iface}_zone" "$new_zones"
146 uci_set_state firewall core "${iface}_ifname" "$ifname"
147 }
148
149 fw_sysctl_interface() {
150 local ifname=$1
151 {
152 sysctl -w net.ipv4.conf.${ifname}.accept_redirects=$FW_ACCEPT_REDIRECTS
153 sysctl -w net.ipv6.conf.${ifname}.accept_redirects=$FW_ACCEPT_REDIRECTS
154 sysctl -w net.ipv4.conf.${ifname}.accept_source_route=$FW_ACCEPT_SRC_ROUTE
155 sysctl -w net.ipv6.conf.${ifname}.accept_source_route=$FW_ACCEPT_SRC_ROUTE
156 } >/dev/null 2>/dev/null
157 }
158