summaryrefslogtreecommitdiffstats
path: root/package/firewall/files/lib/core_interface.sh
blob: 9da6739f0e8de948c2d93e89539899beceab8f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (C) 2009-2010 OpenWrt.org

fw_configure_interface() {
	local iface=$1
	local action=$2
	local ifname=$3

	local status;
	config_get_bool status "$iface" up "0"
	[ "$status" == 1 ] || return 0

	[ -n "$ifname" ] || {
		config_get ifname "$iface" ifname
		ifname=${ifname:-$iface}
	}
	[ "$ifname" == "lo" ] && return 0

	fw_callback pre interface

	fw__do_rules() {
		local action=$1
		local chain=$2
		local ifname=$3

		fw $action i f ${chain}_ACCEPT ACCEPT ^ { -o "$ifname" }
		fw $action i f ${chain}_ACCEPT ACCEPT ^ { -i "$ifname" }
		fw $action i f ${chain}_DROP   DROP   ^ { -o "$ifname" }
		fw $action i f ${chain}_DROP   DROP   ^ { -i "$ifname" }
		fw $action i f ${chain}_REJECT reject ^ { -o "$ifname" }
		fw $action i f ${chain}_REJECT reject ^ { -i "$ifname" }

		fw $action i n ${chain}_nat MASQUERADE ^ { -o "$ifname" }
		fw $action i f ${chain}_MSSFIX TCPMSS  ^ { -o "$ifname" -p tcp --tcp-flags SYN,RST SYN --clamp-mss-to-pmtu }

		fw $action i f input   ${chain}         $ { -i "$ifname" }
		fw $action i f forward ${chain}_forward $ { -i "$ifname" }
		fw $action i n PREROUTING ${chain}_prerouting ^ { -i "$ifname" }
		fw $action i r PREROUTING ${chain}_notrack    ^ { -i "$ifname" }
	}

	local old_zones old_ifname
	config_get old_zones core "${iface}_zone"
	[ -n "$old_zones" ] && {
		config_get old_ifname core "${iface}_ifname"
		for z in $old_zones; do
			fw_log info "removing $iface ($old_ifname) from zone $z"
			fw__do_rules del zone_$z $old_ifname

			ACTION=remove ZONE="$z" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
		done
		uci_revert_state firewall core "${iface}_zone"
		uci_revert_state firewall core "${iface}_ifname"
	}
	[ "$action" == del ] && return

	local new_zones
	load_zone() {
		fw_config_get_zone "$1"
		list_contains zone_network "$iface" || return

		fw_log info "adding $iface ($ifname) to zone $zone_name"
		fw__do_rules add zone_${zone_name} "$ifname"
		append new_zones $zone_name

		ACTION=add ZONE="$zone_name" INTERFACE="$iface" DEVICE="$ifname" /sbin/hotplug-call firewall
	}
	config_foreach load_zone zone

	uci_set_state firewall core "${iface}_zone" "$new_zones"
	uci_set_state firewall core "${iface}_ifname" "$ifname"

	fw_sysctl_interface $ifname

	fw_callback post interface
}

fw_sysctl_interface() {
	local ifname=$1
	{
		sysctl -w net.ipv4.conf.${ifname}.accept_redirects=$FW_ACCEPT_REDIRECTS
		sysctl -w net.ipv6.conf.${ifname}.accept_redirects=$FW_ACCEPT_REDIRECTS
		sysctl -w net.ipv4.conf.${ifname}.accept_source_route=$FW_ACCEPT_SRC_ROUTE
		sysctl -w net.ipv6.conf.${ifname}.accept_source_route=$FW_ACCEPT_SRC_ROUTE
	} >/dev/null 2>/dev/null
}