summaryrefslogtreecommitdiffhomepage
path: root/contrib/package/freifunk-policyrouting/files/etc/hotplug.d/iface/30-policyrouting
blob: c4ae38d9723a7f89f5907b95054ab5e32c2abe69 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/sh

. /lib/functions.sh
. /lib/functions/network.sh

proto="4"
[ -f /proc/net/ipv6_route ] && proto="4 6"

config_load freifunk-policyrouting
config_get enable pr enable
config_get fallback pr fallback
config_get zones pr zones

if [ "$ACTION" = "ifup" ] && [ "$enable" = "1" ]; then
	network_get_subnet net $INTERFACE
	network_get_subnet6 net6 $INTERFACE
	network_get_physdev dev $INTERFACE

	if [ "$net" != "" -a -n "$dev" ]; then
		eval $(/bin/ipcalc.sh $net)
		if [ "$PREFIX" != "0" ]; then
			if [ ! "$(ip r s t olsr-default |grep "throw $NETWORK/$PREFIX")" ]; then
				ip r a throw $NETWORK/$PREFIX table olsr-default
				if [ "$?" = 0 ]; then
					logger -s -t policyrouting "Add route: throw $NETWORK/$PREFIX table olsr-default"
				else
					logger -s -t policyrouting "Error! Could not add route: throw $NETWORK/$PREFIX table olsr-default"
				fi
			fi
		fi

		if [ -n "$net6" ]; then
			if [ ! "$(ip -6 r s t olsr-default |grep "throw $net6")" ]; then
				rule="throw $net6 table olsr-default dev $dev"
				ip -6 r a $rule
				if [ "$?" = 0 ]; then
					logger -s -t policyrouting "Add route: $rule (IPv6)"
				else
					logger -s -t policyrouting "Error! Could not add route: $rule (IPv6)"
				fi
			fi
		fi

		networks=""
		for z in $zones; do
			network_zone="$(uci -q get firewall.zone_${z}.network)"
			if [ -z "$network_zone" ]; then
				network_zone="$z"
			fi
			networks="$networks $network_zone"
		done
		for n in $networks; do
			if [ "$INTERFACE" = "$n" ]; then
				for p in $proto; do
					if [ ! "$(ip -$p ru s | grep "from all iif $dev lookup olsr-default")" ]; then
						ip -$p rule add dev "$dev" lookup olsr-default prio 20000
						if [ "$?" = 0 ]; then
							logger -s -t policyrouting "Use mesh gateway for interface $dev (IPv$p)"
							if [ -z "$(uci -P /var/state get freifunk-policyrouting.${INTERFACE})" ]; then
								uci -P /var/state set freifunk-policyrouting.${INTERFACE}="state"
							fi
							uci -P /var/state set freifunk-policyrouting.${INTERFACE}.device="$dev"
						else
							logger -s -t policyrouting "Error: Could not add rule: dev "$dev" lookup olsr-default prio 20000 (IPv$p)"
						fi
					fi
				done
			fi
		done
	fi
fi

if [ "$ACTION" = "ifdown" ]; then
	dev="$(uci -q -P /var/state get freifunk-policyrouting.${INTERFACE}.device)"
	if [ -n "$dev" ]; then
		networks=""
		for z in $zones; do
			network_zone="$(uci -q get firewall.zone_${z}.network)"
			if [ -z "$network_zone" ]; then
				network_zone="$z"
			fi
			networks="$networks $network_zone"
		done
		for n in $networks; do
			if [ "$INTERFACE" = "$n" ]; then
				for p in $proto; do
					if [ "$(ip -$p ru s | grep "from all iif $dev lookup olsr-default")" ]; then
						ip -$p rule del dev "$dev" lookup olsr-default prio 20000
						if [ "$?" = 0 ]; then
							logger -s -t policyrouting "Remove rule: dev "$dev" lookup olsr-default prio 20000 (IPv$p)"
						else
							logger -s -t policyrouting "Error! Could not remove rule: dev "$dev" lookup olsr-default prio 20000 (IPv$p)"
						fi
					fi
				done
			fi
		done
	fi
fi