Lines 19-27
Link Here
|
19 |
} |
19 |
} |
20 |
|
20 |
|
21 |
checkconfig() { |
21 |
checkconfig() { |
22 |
if [[ -z "${ifconfig_IFACE}" ]]; then |
22 |
if [[ -z "${ifconfig_IFACE}" ]] && [[ -z "${ipaddr_IFACE}" ]]; then |
23 |
eerror "Please make sure that /etc/conf.d/net has \$ifconfig_$IFACE set" |
23 |
eerror "Please make sure that /etc/conf.d/net has \$ifconfig_$IFACE set" |
24 |
eerror "(or \$iface_$IFACE for old-style configuration)" |
24 |
eerror "(or \$iface_$IFACE for old-style configuration)" |
|
|
25 |
eerror "(or \$ipaddr_$IFACE for the iproute2 way)" |
25 |
return 1 |
26 |
return 1 |
26 |
fi |
27 |
fi |
27 |
if [[ -n "${vlans_IFACE}" && ! -x /sbin/vconfig ]]; then |
28 |
if [[ -n "${vlans_IFACE}" && ! -x /sbin/vconfig ]]; then |
Lines 42-53
Link Here
|
42 |
# The following variables are set, which should be declared local by |
43 |
# The following variables are set, which should be declared local by |
43 |
# the calling routine. |
44 |
# the calling routine. |
44 |
# status_IFACE (up or '') |
45 |
# status_IFACE (up or '') |
45 |
# vlans_IFACE (space-separated list) |
46 |
# vlans_IFACE (space-separated list) |
46 |
# ifconfig_IFACE (array of ifconfig lines, replaces iface_IFACE) |
47 |
# ifconfig_IFACE (array of ifconfig lines, replaces iface_IFACE) |
47 |
# dhcpcd_IFACE (command-line args for dhcpcd) |
48 |
# dhcpcd_IFACE (command-line args for dhcpcd) |
48 |
# routes_IFACE (array of route lines) |
49 |
# routes_IFACE (array of route lines) |
49 |
# inet6_IFACE (array of inet6 lines) |
50 |
# inet6_IFACE (array of inet6 lines) |
50 |
# ifconfig_fallback_IFACE (fallback ifconfig if dhcp fails) |
51 |
# ifconfig_fallback_IFACE (fallback ifconfig if dhcp fails) |
|
|
52 |
# ipaddr_IFACE (array of ip addr lines) |
53 |
# iproute_IFACE (array of ip route commands) |
54 |
|
51 |
setup_vars() { |
55 |
setup_vars() { |
52 |
local i iface="${1//\./_}" |
56 |
local i iface="${1//\./_}" |
53 |
|
57 |
|
Lines 59-64
Link Here
|
59 |
eval inet6_IFACE=( \"\$\{inet6_$iface\[@\]\}\" ) |
63 |
eval inet6_IFACE=( \"\$\{inet6_$iface\[@\]\}\" ) |
60 |
eval ifconfig_fallback_IFACE=( \"\$\{ifconfig_fallback_$iface\[@\]\}\" ) |
64 |
eval ifconfig_fallback_IFACE=( \"\$\{ifconfig_fallback_$iface\[@\]\}\" ) |
61 |
|
65 |
|
|
|
66 |
# iproute2 style config vars |
67 |
eval ipaddr_IFACE=( \"\$\{ipaddr_$iface\[@\]\}\" ) |
68 |
eval iproute_IFACE=( \"\$\{iproute_$iface\[@\]\}\" ) |
69 |
|
62 |
# BACKWARD COMPATIBILITY: populate the ifconfig_IFACE array |
70 |
# BACKWARD COMPATIBILITY: populate the ifconfig_IFACE array |
63 |
# if iface_IFACE is set (fex. iface_eth0 instead of ifconfig_eth0) |
71 |
# if iface_IFACE is set (fex. iface_eth0 instead of ifconfig_eth0) |
64 |
eval local iface_IFACE=\"\$\{iface_$iface\}\" |
72 |
eval local iface_IFACE=\"\$\{iface_$iface\}\" |
Lines 87-106
Link Here
|
87 |
iface_start() { |
95 |
iface_start() { |
88 |
local IFACE=${1} i x retval |
96 |
local IFACE=${1} i x retval |
89 |
checkconfig || return 1 |
97 |
checkconfig || return 1 |
90 |
|
98 |
|
91 |
if [[ ${ifconfig_IFACE} != dhcp ]]; then |
99 |
# Do dhcp if it is enabled in config |
92 |
# Show the address, but catch if this interface will be inet6 only |
100 |
if [[ ${ifconfig_IFACE} = dhcp ]] || [[ ${ipaddr_IFACE} = dhcp ]]; then |
93 |
i=${ifconfig_IFACE%% *} |
|
|
94 |
if [[ ${i} == *.*.*.* ]]; then |
95 |
ebegin "Bringing ${IFACE} up (${i})" |
96 |
else |
97 |
ebegin "Bringing ${IFACE} up" |
98 |
fi |
99 |
# ifconfig does not always return failure .. |
100 |
ifconfig ${IFACE} ${ifconfig_IFACE} >${devnull} && \ |
101 |
ifconfig ${IFACE} up &>${devnull} |
102 |
eend $? || return $? |
103 |
else |
104 |
# Check that eth0 was not brought up by the kernel ... |
101 |
# Check that eth0 was not brought up by the kernel ... |
105 |
if [[ ${status_IFACE} == up ]]; then |
102 |
if [[ ${status_IFACE} == up ]]; then |
106 |
einfo "Keeping kernel configuration for ${IFACE}" |
103 |
einfo "Keeping kernel configuration for ${IFACE}" |
Lines 130-138
Link Here
|
130 |
return $retval |
127 |
return $retval |
131 |
fi |
128 |
fi |
132 |
fi |
129 |
fi |
|
|
130 |
# Show address retrieved |
131 |
i=$(ifconfig ${IFACE} | grep -m1 -o 'inet addr:[^ ]*' | cut -d: -f2) |
132 |
[[ -n ${i} ]] && einfo " ${IFACE} received address ${i}" |
133 |
# If not a dhcp interface, do ip address stype if config is in this format. |
134 |
elif [[ -n ${ipaddr_IFACE} ]]; then |
135 |
ebegin "Bringing ${IFACE} up" |
136 |
# Force link to up as inet6 will not work without this |
137 |
/sbin/ip link set dev ${IFACE} up &>${devnull} |
138 |
# Why bother having the aliases in a diffrent section if they can be handled here |
139 |
# This covers inet6 addresses as well |
140 |
if [[ ${#ipaddr_IFACE[@]} -gt 0 ]]; then |
141 |
for ((i = 0; i < ${#ipaddr_IFACE[@]}; i = i + 1)); do |
142 |
if [[ ${i} > 0 ]]; then |
143 |
einfo " ${IFACE}:${i} ${ipaddr_IFACE[i]%% *}" |
144 |
/sbin/ip addr add dev ${IFACE} ${ipaddr_IFACE[i]} label ${IFACE}:${i} >${devnull} && \ |
145 |
/sbin/ip addr show dev ${IFACE} | grep "${ipaddr_IFACE[i]%% *}" &>${devnull} |
146 |
eend $? || return $? |
147 |
else |
148 |
einfo " ${IFACE} ${ipaddr_IFACE[i]%% *}" |
149 |
/sbin/ip addr add dev ${IFACE} ${ipaddr_IFACE[i]} >${devnull} && \ |
150 |
/sbin/ip addr show dev ${IFACE} | grep "${ipaddr_IFACE[i]%% *}" &>${devnull} |
151 |
eend $? || return $? |
152 |
fi |
153 |
|
154 |
done |
155 |
fi |
156 |
# Last do ifconfig/iface style addresses |
157 |
elif [[ -n ${ifconfig_IFACE} ]]; then |
158 |
# Show the address, but catch if this interface will be inet6 only |
159 |
i=${ifconfig_IFACE%% *} |
160 |
if [[ ${i} == *.*.*.* ]]; then |
161 |
ebegin "Bringing ${IFACE} up (${i})" |
162 |
else |
163 |
ebegin "Bringing ${IFACE} up" |
164 |
fi |
165 |
# ifconfig does not always return failure .. |
166 |
ifconfig ${IFACE} ${ifconfig_IFACE} >${devnull} && \ |
167 |
ifconfig ${IFACE} up &>${devnull} |
168 |
eend $? || return $? |
133 |
fi |
169 |
fi |
134 |
|
170 |
|
135 |
if [[ ${#ifconfig_IFACE[@]} -gt 1 ]]; then |
171 |
# Only add aliases if in ifconfig/iface format as when in ipaddr format |
|
|
172 |
# aliases get added along with the primary ip address. |
173 |
if [[ ${#ifconfig_IFACE[@]} -gt 1 ]] && [[ -z ${ipaddr_IFACE ]]; then |
136 |
einfo " Adding aliases" |
174 |
einfo " Adding aliases" |
137 |
for ((i = 1; i < ${#ifconfig_IFACE[@]}; i = i + 1)); do |
175 |
for ((i = 1; i < ${#ifconfig_IFACE[@]}; i = i + 1)); do |
138 |
ebegin " ${IFACE}:${i} (${ifconfig_IFACE[i]%% *})" |
176 |
ebegin " ${IFACE}:${i} (${ifconfig_IFACE[i]%% *})" |
Lines 140-147
Link Here
|
140 |
eend $? |
178 |
eend $? |
141 |
done |
179 |
done |
142 |
fi |
180 |
fi |
143 |
|
181 |
# Only add inet6 addresses if not using ipaddr format. |
144 |
if [[ -n ${inet6_IFACE} ]]; then |
182 |
if [[ -n ${inet6_IFACE} ]] && [[ -z ${ipaddr_IFACE} ]]; then |
145 |
einfo " Adding inet6 addresses" |
183 |
einfo " Adding inet6 addresses" |
146 |
for ((i = 0; i < ${#inet6_IFACE[@]}; i = i + 1)); do |
184 |
for ((i = 0; i < ${#inet6_IFACE[@]}; i = i + 1)); do |
147 |
ebegin " ${IFACE} inet6 add ${inet6_IFACE[i]}" |
185 |
ebegin " ${IFACE} inet6 add ${inet6_IFACE[i]}" |
Lines 149-157
Link Here
|
149 |
eend $? |
187 |
eend $? |
150 |
done |
188 |
done |
151 |
fi |
189 |
fi |
152 |
|
190 |
# Set routes with ip route and print them - this can also include default route |
153 |
# Set static routes |
191 |
if [[ -n ${iproute_IFACE} ]]; then |
154 |
if [[ -n ${routes_IFACE} ]]; then |
192 |
einfo " Adding routes" |
|
|
193 |
for ((i = 0; i < ${#iproute_IFACE[@]}; i = i + 1)); do |
194 |
ebegin " ${iproute_IFACE[i]}" |
195 |
/sbin/ip route add dev ${IFACE} ${iproute_IFACE[i]} |
196 |
eend $? |
197 |
done |
198 |
# Set static routes with the route command only if iproute was not called |
199 |
elif [[ -n ${routes_IFACE} ]] ; then |
155 |
einfo " Adding routes" |
200 |
einfo " Adding routes" |
156 |
for ((i = 0; i < ${#routes_IFACE[@]}; i = i + 1)); do |
201 |
for ((i = 0; i < ${#routes_IFACE[@]}; i = i + 1)); do |
157 |
ebegin " ${routes_IFACE[i]}" |
202 |
ebegin " ${routes_IFACE[i]}" |
Lines 159-165
Link Here
|
159 |
eend $? |
204 |
eend $? |
160 |
done |
205 |
done |
161 |
fi |
206 |
fi |
162 |
|
|
|
163 |
# Set default route if applicable to this interface |
207 |
# Set default route if applicable to this interface |
164 |
if [[ ${gateway} == ${IFACE}/* ]]; then |
208 |
if [[ ${gateway} == ${IFACE}/* ]]; then |
165 |
local ogw=$(/bin/netstat -rn | awk '$1 == "0.0.0.0" {print $2}') |
209 |
local ogw=$(/bin/netstat -rn | awk '$1 == "0.0.0.0" {print $2}') |
Lines 205-264
Link Here
|
205 |
# iface_start ran. Instead query for current configuration and bring |
249 |
# iface_start ran. Instead query for current configuration and bring |
206 |
# down the interface. |
250 |
# down the interface. |
207 |
iface_stop() { |
251 |
iface_stop() { |
208 |
local IFACE=${1} i x aliases inet6 count |
252 |
local IFACE=${1} i x count addresses |
|
|
253 |
# Check and see if interface is dhcp, if so release ip the address. |
254 |
# This should stop the interface if the only address on it is DHCP. |
255 |
if [[ -e /var/run/dhcpcd-${IFACE}.pid ]]; then |
256 |
ebegin "Releasing DHCP lease for ${IFACE}" |
257 |
for ((count = 0; count < 9; count = count + 1)); do |
258 |
/sbin/dhcpcd -z ${i} &>${devnull} || break |
259 |
sleep 1 |
260 |
done |
261 |
[[ ${count} -lt 9 ]] |
262 |
eend $? "Timed out" |
263 |
fi |
209 |
|
264 |
|
210 |
# Try to do a simple down (no aliases, no inet6, no dhcp) |
265 |
# Gather a list of all the addresses on the specified interface. |
211 |
aliases="$(ifconfig | grep -o "^$IFACE:[0-9]*" | tac)" |
266 |
# inet6 addresses will be in the list along with inet addresses. |
212 |
inet6="$(ifconfig ${IFACE} | awk '$1 == "inet6" {print $2}')" |
267 |
addresses="$(/sbin/ip address show ${IFACE} | grep 'inet.*' | awk '{print $2}' | tac)" |
213 |
if [[ -z ${aliases} && -z ${inet6} && ! -e /var/run/dhcpcd-${IFACE}.pid ]]; then |
268 |
# Bring down each ip address on interface, this can be inet or inet6. |
|
|
269 |
# This is done in reverse order to stop any possible errors that |
270 |
# might crop up. Removing a primary address will cause all secondary |
271 |
# address on the interface that are on the same subnet to dissapear. |
272 |
if [[ -n ${addresses} ]]; then |
214 |
ebegin "Bringing ${IFACE} down" |
273 |
ebegin "Bringing ${IFACE} down" |
215 |
ifconfig ${IFACE} down &>/dev/null |
274 |
for x in ${addresses}; do |
216 |
eend 0 |
275 |
ebegin " ${IFACE} del ${x}" |
217 |
return 0 |
276 |
/sbin/ip addr del dev ${IFACE} ${x} |
218 |
fi |
277 |
eend $? |
219 |
|
278 |
done |
220 |
einfo "Bringing ${IFACE} down" |
279 |
fi |
221 |
|
280 |
# Stop the interface if it shows link state to be up. |
222 |
# Stop aliases before primary interface. |
281 |
if /sbin/ip address show ${IFACE} | grep -o "UP" &>${devnull}; then |
223 |
# Note this must be done in reverse order, since ifconfig eth0:1 |
282 |
ebegin " Stopping ${IFACE}" |
224 |
# will remove eth0:2, etc. It might be sufficient to simply remove |
283 |
/sbin/ip link set dev ${IFACE} down &>${devnull} |
225 |
# the base interface but we're being safe here. |
284 |
eend $? |
226 |
for i in ${aliases} ${IFACE}; do |
285 |
fi |
227 |
|
|
|
228 |
# Delete all the inet6 addresses for this interface |
229 |
inet6="$(ifconfig ${i} | awk '$1 == "inet6" {print $3}')" |
230 |
if [[ -n ${inet6} ]]; then |
231 |
einfo " Removing inet6 addresses" |
232 |
for x in ${inet6}; do |
233 |
ebegin " ${IFACE} inet6 del ${x}" |
234 |
ifconfig ${i} inet6 del ${x} |
235 |
eend $? |
236 |
done |
237 |
fi |
238 |
|
239 |
# Stop DHCP (should be N/A for aliases) |
240 |
# Don't trust current configuration... investigate ourselves |
241 |
if /sbin/dhcpcd -z ${i} &>${devnull}; then |
242 |
ebegin " Releasing DHCP lease for ${IFACE}" |
243 |
for ((count = 0; count < 9; count = count + 1)); do |
244 |
/sbin/dhcpcd -z ${i} &>${devnull} || break |
245 |
sleep 1 |
246 |
done |
247 |
[[ ${count} -lt 9 ]] |
248 |
eend $? "Timed out" |
249 |
fi |
250 |
ebegin " Stopping ${i}" |
251 |
ifconfig ${i} down &>${devnull} |
252 |
eend 0 |
253 |
done |
254 |
|
255 |
return 0 |
286 |
return 0 |
256 |
} |
287 |
} |
257 |
|
288 |
|
258 |
start() { |
289 |
start() { |
259 |
# These variables are set by setup_vars |
290 |
# These variables are set by setup_vars |
260 |
local status_IFACE vlans_IFACE dhcpcd_IFACE |
291 |
local status_IFACE vlans_IFACE dhcpcd_IFACE |
261 |
local -a ifconfig_IFACE routes_IFACE inet6_IFACE |
292 |
local -a ifconfig_IFACE routes_IFACE inet6_IFACE iproute_IFACE ipaddr_IFACE |
262 |
|
293 |
|
263 |
# Call user-defined preup function if it exists |
294 |
# Call user-defined preup function if it exists |
264 |
if [[ $(type -t preup) == function ]]; then |
295 |
if [[ $(type -t preup) == function ]]; then |