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