summaryrefslogtreecommitdiff
path: root/network
blob: c2ad9baa91ba1b222cc5a6d229b83db9eb2e5ac2 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

for s in wireless bonding bridges dhcpcd; do
	[[ -f /etc/conf.d/$s ]] && . "/etc/conf.d/$s"
done

# helper function to determine if legacy network support is needed
need_legacy() {
	if [[ -z $interface ]]; then
		return 0 # need legacy
	fi

	return 1 # enough present for iproute2 support
}

deprecated() {
	printf "${C_FAIL}Warning:${C_CLEAR} This functionality is deprecated.\n"
	printf "  Please refer to /etc/rc.conf on how to define a single wired\n"
	printf "  connection, or use a utility such as netcfg.\n"
}

network_up() {
	ip link set dev $interface up || return 1

	if [[ $address ]]; then
		for var in netmask gateway; do
			if [[ -z ${!var} ]]; then
				printf "${C_FAIL}Error: static address defined without $var!\n"
				return 1
			fi
		done
		ip addr add $address/$netmask broadcast ${broadcast:-+} dev $interface || return 1
		ip route add default via $gateway || return 1
	else
		dhcpcd $DHCPCD_ARGS $interface || return 1
	fi
}

network_down() {
	if [[ -f /var/run/dhcpcd-$interface.pid ]]; then
		dhcpcd -k $interface || return 1
	else
		ip route del default || return 1
		ip addr flush dev $interface || return 1
	fi

	ip link set dev $interface down || return 1
}

ifup() {
	local ifcfg=${!1}

	if [[ ! $1 ]]; then
		echo "usage: $0 ifup <interface_name>"
		return 1
	fi

	# Get the name of the interface from the first token in the string
	if [[ $ifcfg = dhcp ]]; then
		ifname=$1
	else
		ifname=${ifcfg%% *}
	fi

	ifconfig $ifname up

	wi_up $1 || return 1

	if [[ $ifcfg = dhcp ]]; then
		# remove the .pid file if it exists
		rm -f /var/run/dhcpcd-${1}.pid >/dev/null 2>&1
		rm -f /var/run/dhcpcd-${1}.cache >/dev/null 2>&1
		dhcpcd $DHCPCD_ARGS ${1}
	else
		ifconfig $ifcfg
	fi
}

wi_up() {
	local iwcfg=wlan_$1

	[[ ${!iwcfg} ]] || return 0

	iwconfig ${!iwcfg}
	[[ $WIRELESS_TIMEOUT ]] || WIRELESS_TIMEOUT=2
	sleep $WIRELESS_TIMEOUT

	bssid=$(iwgetid $1 -ra)
	if [[ $bssid = 00:00:00:00:00:00 ]]; then
		printhl "Could not associate $1 - try increasing WIRELESS_TIMEOUT and check network is WEP or has no security"
		return 1
	fi
	return 0
}

ifdown() {
	local ifcfg=${!1}

	if [[ ! $1 ]]; then
		echo "usage: $0 ifdown <interface_name>"
		return 1
	fi

	if [[ $ifcfg = dhcp && -f /var/run/dhcpcd-${1}.pid ]]; then
		dhcpcd -k ${1} >/dev/null 2>&1
	fi
	# Always bring the interface itself down
	ifconfig ${1} down >/dev/null 2>&1
}

iflist() {
	for ifline in ${INTERFACES[@]}; do
		if [[ $ifline = ${ifline#!} ]]; then
			printf " $ifline:\t"
		else
			printf "$ifline:\t"
		fi
		echo ${!ifline#!}
	done
}

rtup() {
	local routecfg=${!1}

	if [[ ! $1 ]]; then
		echo "usage: $0 rtup <route_name>"
		return 1
	fi

	if [[ $routecfg =~ :: ]]; then
		route -A inet6 add $routecfg
	else
		route add $routecfg
	fi
}

rtdown() {
	local routecfg=${!1}

	if [[ ! $1 ]]; then
		echo "usage: $0 rtdown <route_name>"
		return 1
	fi

	if [[ $routecfg =~ :: ]]; then
		route -A inet6 del $routecfg
	else
		route del $routecfg
	fi
}

rtlist() {
	for rtline in ${ROUTES[@]}; do
		if [[ $rtline = ${rtline#!} ]]; then
			printf " $rtline:\t"
		else
			printf "$rtline:\t"
		fi
		echo ${!rtline#!}
	done
}

bond_up() {
	for ifline in ${BOND_INTERFACES[@]}; do
		if [[ $ifline = ${ifline#!} ]]; then
			bondcfg="bond_$ifline"
			if [[ ${!bondcfg} ]]; then
				ifenslave $ifline ${!bondcfg} || error=1
			fi
		fi
	done
}

bond_down() {
	for ifline in ${BOND_INTERFACES[@]}; do
		if [[ $ifline = ${ifline#!} ]]; then
			bondcfg="bond_$ifline"
			ifenslave -d $ifline ${!bondcfg} || error=1
		fi
	done
}

bridge_up() {
	for br in ${BRIDGE_INTERFACES[@]}; do
		if [[ $br = ${br#!} ]]; then
			# if the bridge already exists, remove it
			if [[ $(ifconfig $br 2>/dev/null) ]]; then
				ifconfig $br down
				brctl delbr $br
			fi
			brctl addbr $br
			brifs="bridge_$br"
			for brif in ${!brifs}; do
				if [[ $brif = ${brif#!} ]]; then
					for ifline in ${BOND_INTERFACES[@]}; do
						if [[ $brif = $ifline && $ifline = ${ifline#!} ]]; then
							ifup $ifline
							bondcfg="bond_$ifline"
							ifenslave $ifline ${!bondcfg} || error=1
							unset bond_$ifline
						fi
					done

					brctl addif $br $brif || error=1
				fi
			done
		fi
	done
}

bridge_down() {
	for br in ${BRIDGE_INTERFACES[@]}; do
		if [[ $br = ${br#!} ]]; then
			brctl delbr $br
		fi
	done
}


case "$1" in
	start)
		# deprecation check
		need_legacy && deprecated
		if ! ck_daemon network; then
			echo "Network is already running.  Try 'network restart'"
			exit
		fi
		stat_busy "Starting Network"
		error=0
		if need_legacy; then
			# bring up bridge interfaces
			bridge_up
			# bring up ethernet interfaces
			for ifline in ${INTERFACES[@]}; do
				if [[ $ifline = ${ifline#!} ]]; then
					ifup $ifline || error=1
				fi
			done
			# bring up bond interfaces
			bond_up
			# bring up routes
			for rtline in "${ROUTES[@]}"; do
				if [ "$rtline" = "${rtline#!}" ]; then
					rtup $rtline || error=1
				fi
			done
		else
			network_up
		fi
		if ((error == 0)); then
			add_daemon network
			stat_done
		else
			stat_fail
		fi
		;;
	stop)
		# deprecation check
		need_legacy && deprecated
		if [[ $NETWORK_PERSIST =~ yes|YES && $RUNLEVEL == [06] ]]; then
			status "Skipping Network Shutdown" true
			exit 0
		fi

		stat_busy "Stopping Network"
		rm_daemon network
		error=0
		if need_legacy; then
			for rtline in "${ROUTES[@]}"; do
				if [[ $rtline = ${rtline#!} ]]; then
					rtdown $rtline || error=1
				fi
			done
			# bring down bond interfaces
			bond_down
			for ifline in ${INTERFACES[@]}; do
				if [[ $ifline = ${ifline#!} ]]; then
					ifdown $ifline || error=1
				fi
			done
			# bring down bridge interfaces
			bridge_down
		else
			network_down
		fi
		if ((error == 0)); then
			stat_done
		else
			stat_fail
		fi
		;;
	restart)
		$0 stop
		sleep 2
		$0 start
		;;
	ifup|ifdown|iflist|rtup|rtdown|rtlist)
		# deprecation check
		need_legacy && deprecated
		$1 $2
		;;
	*)
		echo "usage: $0 {start|stop|restart}"
		echo "       $0 {ifup|ifdown|iflist|rtup|rtdown|rtlist}";;
esac

# vim: set ts=2 sw=2 noet: