summaryrefslogtreecommitdiff
path: root/package/network/ipv6/6relayd/files/6relayd.init
blob: f35f80c609dea426a99c14ab13969f1cfc6c372b (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/sh /etc/rc.common
# Copyright (c) 2011-2012 OpenWrt.org
START=80

start_6relayd() {
	local cfg="$1"
	local mode="$2"
	local args=""

	SERVICE_DAEMONIZE=1
	SERVICE_WRITE_PID=1
	SERVICE_PID_FILE="/var/run/6relayd-$cfg.pid"
	[ -f "$SERVICE_PID_FILE" ] && {
		if grep -q 6relayd "/proc/$(cat $SERVICE_PID_FILE)/cmdline"; then
			return 0
		else
			rm -f "$SERVICE_PID_FILE"
		fi
	}

	# Detect master interface
	local master masterif
	config_get masterif "$cfg" master
	network_get_device master "$masterif"
	[ -z "$master" ] && master="."

	# Detect slave interfaces
	local slaves=""
	local slaveifs
	config_get slaveifs "$cfg" network
	for slaveif in $slaveifs; do
		local slave
		network_get_device slave "$slaveif"
		# Compatibility with old config format
		if [ "$mode" = "relay" -a "$master" = "." ]; then
			[ -z "$slave" ] && return 0
			master="$slave"
		else
			[ -n "$slave" ] && append slaves "$slave"
			echo 1 > "/proc/sys/net/ipv6/conf/$slave/forwarding"
		fi
	done

	# Bail if no slaves are active
	[ -z "$slaves" ] && return 0

	# Configure services
	local rd dhcpv6 ndp
	config_get rd "$cfg" rd
	config_get dhcpv6 "$cfg" dhcpv6
	config_get ndp "$cfg" ndp

	# Test for fallback mode
	local fallback fallback_relay
	config_get fallback_relay "$cfg" fallback_relay

	if [ -n "$fallback_relay" -a "$master" != "." ]; then
		local prefix
		network_get_prefix6 prefix "$masterif"
		[ -z "$prefix" ] && fallback=1
	fi

	if [ -n "$fallback" ]; then
		for service in $fallback_relay; do
			eval "$service=relay"
		done
	fi

	# Configure feature options
	local always_rewrite_dns
	config_get_bool always_rewrite_dns "$cfg" always_rewrite_dns 0
	[ "$always_rewrite_dns" -eq 1 ] && append args "-n"

	local always_assume_default
	config_get_bool always_assume_default "$cfg" always_assume_default 0
	[ "$always_assume_default" -eq 1 ] && append args "-u"

	[ "$ndp" = "relay" ] && append args "-N -s -l"
	[ "$rd" = "relay" ] && append args "-Rrelay"
	[ "$rd" = "server" ] && append args "-Rserver"
	[ "$dhcpv6" = "relay" ] && append args "-Drelay"
	[ "$dhcpv6" = "server" ] && append args "-Dserver"
	[ "$ndp" != "relay" -a "$rd" != "relay" -a "$dhcpv6" != "relay" ] && master="."

	service_start /usr/sbin/6relayd $args $master $slaves
}

restart_affected_6relayd() {
	local cfg="$1"
	local net="$2"
	local mode="$3"

	local master slaves
	config_get master "$cfg" master
	config_get slaves "$cfg" network

	for iface in $master $slaves; do
		if [ "$iface" = "$net" ]; then
			SERVICE_PID_FILE="/var/run/6relayd-$cfg.pid"
			service_stop /usr/sbin/6relayd
			rm -f "$SERVICE_PID_FILE"
			start_6relayd "$cfg" "$mode"
		fi
	done
}

restart_affected() {
	. /lib/functions/service.sh
	. /lib/functions/network.sh
	local net="$1"
	config_load 6relayd
	config_foreach restart_affected_6relayd server "$net"
	config_foreach restart_affected_6relayd relay "$net" "relay"
}

stop() {
	. /lib/functions/service.sh
	for pid in /var/run/6relayd-*.pid; do
		SERVICE_PID_FILE="$pid"
		service_stop /usr/sbin/6relayd
		rm -f "$SERVICE_PID_FILE"
	done
}

start() {
	. /lib/functions/service.sh
	. /lib/functions/network.sh
	config_load 6relayd
	config_foreach start_6relayd server
	config_foreach start_6relayd relay relay
}