aboutsummaryrefslogtreecommitdiff
blob: adbd3c36d559222c9231194f8558c081174b71a3 (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# Copyright (c) 2007-2008 Roy Marples <roy@marples.name>
# Released under the 2-clause BSD license.
# shellcheck shell=sh disable=SC1008

ifconfig_depend()
{
	program ifconfig
	provide interface
}

_up()
{
	ifconfig "${IFACE}" up
}

_down()
{
	ifconfig "${IFACE}" down
}

_exists()
{
	[ -e /sys/class/net/"$IFACE" ]
}

_ifindex()
{
	local index=-1
	local f v
	if [ -e /sys/class/net/"${IFACE}"/ifindex ]; then
		index=$(cat /sys/class/net/"${IFACE}"/ifindex)
	else
		for f in /sys/class/net/*/ifindex ; do
			v=$(cat $f)
			[ $v -gt $index ] && index=$v
		done
		: $(( index += 1 ))
	fi
	echo "${index}"
	return 0
}

_is_wireless()
{
	# Support new sysfs layout
	[ -d /sys/class/net/"${IFACE}"/wireless -o \
		-d /sys/class/net/"${IFACE}"/phy80211 ] && return 0

	[ ! -e /proc/net/wireless ] && return 1
	grep -q "^[[:space:]]*${IFACE}:" /proc/net/wireless
}

_set_flag()
{
	ifconfig "${IFACE}" "$1"
}

_get_mac_address()
{
	local mac=$(LC_ALL=C ifconfig "${IFACE}" | \
	sed -n -e 's/.* \(HWaddr\|ether\) \(..:..:..:..:..:..\).*/\2/p')

	case "${mac}" in
		00:00:00:00:00:00);;
		44:44:44:44:44:44);;
		FF:FF:FF:FF:FF:FF);;
		"");;
		*) echo "${mac}"; return 0;;
	esac

	return 1
}

_set_mac_address()
{
	ifconfig "${IFACE}" hw ether "$1"
}

_get_inet_address()
{
	set -- $(LC_ALL=C ifconfig "${IFACE}" |
	sed -n -e 's/.*\(inet addr:\|inet \)\([^ ]*\).*\(Mask:\|netmask \)\([^ ]*\).*/\2 \4/p')
	[ -z "$1" ] && return 1

	printf '%s' "$1"
	shift
	echo "/$(_netmask2cidr "$1")"
}

_get_inet6_address()
{
	set -- $(LC_ALL=C ifconfig "${IFACE}" |
	sed -n -e 's/.*\(inet6 addr:\|inet6\) \([^ /]*\)\(\/\| *prefixlen \)\([^ ]*\).*/\2\/\4/p')
	[ -z "$1" ] && return 1

	echo -n "$1"
}

_get_addresses_fn()
{
	local fn="$1"
	local iface=${IFACE} i=0
	local addrs="$($fn)"

	while true; do
		local IFACE="${iface}:${i}"
		_exists || break
		local addr="$($fn)"
		[ -n "${addr}" ] && addrs="${addrs}${addrs:+ }${addr}"
		: $(( i += 1 ))
	done
	echo "${addrs}"
}

_get_inet_addresses()
{
	_get_addresses_fn _get_inet_address
}

_get_inet6_addresses()
{
	_get_addresses_fn _get_inet6_address
}

_cidr2netmask()
{
	local cidr="$1" netmask="" done=0 i=0 sum=0 cur=128
	local octets= frac=

	local octets=$(( cidr / 8 ))
	local frac=$(( cidr % 8 ))
	while [ ${octets} -gt 0 ]; do
		netmask="${netmask}.255"
		: $(( octets -= 1 ))
		: $(( done += 1 ))
	done

	if [ ${done} -lt 4 ]; then
		while [ ${i} -lt ${frac} ]; do
			: $(( sum += cur ))
			: $(( cur /= 2 ))
			: $(( i += 1 ))
		done
		netmask="${netmask}.${sum}"
		: $(( done += 1 ))

		while [ ${done} -lt 4 ]; do
			netmask="${netmask}.0"
			: $(( done += 1 ))
		done
	fi

	echo "${netmask#.*}"
}

_add_address()
{
	if [ "$1" = "127.0.0.1/8" -a "${IFACE}" = "lo" ]; then
		ifconfig "${IFACE}" "$@" 2>/dev/null
		return 0
	fi

	case "$1" in
		*:*) ifconfig "${IFACE}" inet6 add "$@"; return $?;;
	esac

	# IPv4 is tricky - ifconfig requires an aliased device
	# for multiple addresses
	local iface="${IFACE}"
	if LC_ALL=C ifconfig "${iface}" | grep -Eq '\<inet (addr:)?.*'; then
		# Get the last alias made for the interface and add 1 to it
		i=$(ifconfig | sed '1!G;h;$!d' | grep -m 1 -o "^${iface}:[0-9]*" \
			| sed -n -e 's/'"${iface}"'://p')
		: $(( i = ${i:-0} + 1 ))
		iface="${iface}:${i}"
	fi

	# ifconfig doesn't like CIDR addresses
	local ip="${1%%/*}" cidr="${1##*/}" netmask=
	if [ -n "${cidr}" -a "${cidr}" != "${ip}" ]; then
		netmask="$(_cidr2netmask "${cidr}")"
		shift
		set -- "${ip}" netmask "${netmask}" "$@"
	fi

	local arg= cmd=
	while [ -n "$1" ]; do
		case "$1" in
			brd)
				if [ "$2" = "+" ]; then
					shift
				else
					cmd="${cmd} broadcast"
				fi
				;;
			peer) cmd="${cmd} pointopoint";;
			*) cmd="${cmd} $1";;
		esac
		shift
	done

	ifconfig "${iface}" ${cmd}
}

_add_route()
{
	local inet6= family=

	if [ "$1" = "-A" -o "$1" = "-f" -o "$1" = "-family" ]; then
		family="-A $2"
		shift; shift
	elif [ "$1" = "-4" ]; then
	    family="-A inet"
		shift
	elif [ "$1" = "-6" ]; then
	    family="-A inet6"
		shift
	fi

	if [ -n "${metric}" ]; then
		set -- "$@" metric ${metric}
	fi

	if [ $# -eq 3 ]; then
		set -- "$1" "$2" gw "$3"
	elif [ "$3" = "via" ]; then
		local one=$1 two=$2
		shift; shift; shift
		set -- "${one}" "${two}" gw "$@"
	fi

	if [ "$1" = "-net" -o "$1" = "-host" ]; then
		if [ "${2##*:}" != "$2" ]; then shift
		elif [ "$2" = "default" ]; then shift
		fi
	fi

	route ${family} add "$@" dev "${IFACE}"
}

_delete_addresses()
{
	# We don't remove addresses from aliases
	case "${IFACE}" in
		*:*) return 0;;
	esac

	einfo "Removing addresses"
	eindent
	# iproute2 can add many addresses to an iface unlike ifconfig ...
	# iproute2 added addresses cause problems for ifconfig
	# as we delete an address, a new one appears, so we have to
	# keep polling
	while true; do
		local addr=$(_get_inet_address)
		[ -z "${addr}" ] && break

		if [ "${addr}" = "127.0.0.1/8" ]; then
			# Don't delete the loopback address
			[ "${IFACE}" = "lo" -o "${IFACE}" = "lo0" ] && break
		fi
		einfo "${addr}"
		ifconfig "${IFACE}" 0.0.0.0 || break
	done

	# Remove IPv6 addresses
	local addr=
	for addr in $(LC_ALL=C ifconfig "${IFACE}" | \
		sed -n -e 's/^.*\(inet6 addr:\|inet6\) \([^ ]*\) .*\(Scope:[^L]\|scopeid [^<]*<[^l]\).*/\2/p'); do
		if [ "${IFACE}" = "lo" ]; then
			case "${addr}" in
				"::1/128"|"/128") continue;;
			esac
		fi
		einfo "${addr}"
		ifconfig "${IFACE}" inet6 del "${addr}"
	done

	return 0
}

_has_carrier()
{
	return 0
}

_tunnel()
{
	iptunnel "$@"
}

ifconfig_pre_start()
{
	local tunnel=
	eval tunnel=\$iptunnel_${IFVAR}
	if [ -n "${tunnel}" ]; then
		# Set our base metric to 1000
		metric=1000
		ebegin "Creating tunnel ${IFVAR}"
		iptunnel add ${tunnel}
		eend $? || return 1
		_up
	fi

	# MTU support
	local mtu=
	eval mtu=\$mtu_${IFVAR}
	[ -n "${mtu}" ] && ifconfig "${IFACE}" mtu "${mtu}"

	# TX Queue Length support
	local len=
	eval len=\$txqueuelen_${IFVAR}
	[ -n "${len}" ] && ifconfig "${IFACE}" txqueuelen "${len}"

	return 0
}

ifconfig_post_stop()
{
	# Don't delete sit0 as it's a special tunnel
	[ "${IFACE}" = "sit0" ] && return 0

	[ -z "$(iptunnel show "${IFACE}" 2>/dev/null)" ] && return 0

	ebegin "Destroying tunnel ${IFACE}"
	iptunnel del "${IFACE}"
	eend $?
}

# Is the interface administratively/operationally up?
# The 'UP' status in ifconfig/iproute2 is the administrative status
# Operational state is available in iproute2 output as 'state UP', or the
# operstate sysfs variable.
# 0: up
# 1: down
# 2: invalid arguments
is_admin_up()
{
	local iface="$1"
	[ -z "$iface" ] && iface="$IFACE"
	ifconfig "${iface}" | \
	sed -n '1,1{ /flags=.*[<,]UP[,>]/{ q 0 }}; q 1; '
}

is_oper_up()
{
	local iface="$1"
	[ -z "$iface" ] && iface="$IFACE"
	read state </sys/class/net/"${iface}"/operstate
	[ "x$state" = "up" ]
}