|
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 34-44
Link Here
|
| 34 |
# The following variables are set, which should be declared local by |
35 |
# The following variables are set, which should be declared local by |
| 35 |
# the calling routine. |
36 |
# the calling routine. |
| 36 |
# status_IFACE (up or '') |
37 |
# status_IFACE (up or '') |
| 37 |
# vlans_IFACE (space-separated list) |
38 |
# vlans_IFACE (space-separated list) |
| 38 |
# ifconfig_IFACE (array of ifconfig lines, replaces iface_IFACE) |
39 |
# ifconfig_IFACE (array of ifconfig lines, replaces iface_IFACE) |
| 39 |
# dhcpcd_IFACE (command-line args for dhcpcd) |
40 |
# dhcpcd_IFACE (command-line args for dhcpcd) |
| 40 |
# routes_IFACE (array of route lines) |
41 |
# routes_IFACE (array of route lines) |
| 41 |
# inet6_IFACE (array of inet6 lines) |
42 |
# inet6_IFACE (array of inet6 lines) |
|
|
43 |
# ipaddr_IFACE (array of ip addr lines) |
| 44 |
# iproute_IFACE (array of ip route commands) |
| 45 |
|
| 42 |
setup_vars() { |
46 |
setup_vars() { |
| 43 |
local i iface="${1//\./_}" |
47 |
local i iface="${1//\./_}" |
| 44 |
|
48 |
|
|
Lines 48-54
Link Here
|
| 48 |
eval dhcpcd_IFACE=\"\$\{dhcpcd_$iface\}\" |
52 |
eval dhcpcd_IFACE=\"\$\{dhcpcd_$iface\}\" |
| 49 |
eval routes_IFACE=( \"\$\{routes_$iface\[@\]\}\" ) |
53 |
eval routes_IFACE=( \"\$\{routes_$iface\[@\]\}\" ) |
| 50 |
eval inet6_IFACE=( \"\$\{inet6_$iface\[@\]\}\" ) |
54 |
eval inet6_IFACE=( \"\$\{inet6_$iface\[@\]\}\" ) |
| 51 |
|
55 |
# iproute2 style config vars |
|
|
56 |
eval ipaddr_IFACE=( \"\$\{ipaddr_$iface\[@\]\}\" ) |
| 57 |
eval iproute_IFACE=( \"\$\{iproute_$iface\[@\]\}\" ) |
| 58 |
|
| 52 |
# BACKWARD COMPATIBILITY: populate the ifconfig_IFACE array |
59 |
# BACKWARD COMPATIBILITY: populate the ifconfig_IFACE array |
| 53 |
# if iface_IFACE is set (fex. iface_eth0 instead of ifconfig_eth0) |
60 |
# if iface_IFACE is set (fex. iface_eth0 instead of ifconfig_eth0) |
| 54 |
eval local iface_IFACE=\"\$\{iface_$iface\}\" |
61 |
eval local iface_IFACE=\"\$\{iface_$iface\}\" |
|
Lines 77-84
Link Here
|
| 77 |
iface_start() { |
84 |
iface_start() { |
| 78 |
local IFACE=${1} i x |
85 |
local IFACE=${1} i x |
| 79 |
checkconfig || return 1 |
86 |
checkconfig || return 1 |
|
|
87 |
|
| 88 |
# Do dhcp if it is enabled in config |
| 89 |
if [[ ${ifconfig_IFACE} = dhcp ]] || [[ ${iproute_IFACE} = dhcp ]]; then |
| 90 |
# Check that eth0 was not brought up by the kernel ... |
| 91 |
if [[ ${status_IFACE} == up ]]; then |
| 92 |
einfo "Keeping kernel configuration for ${IFACE}" |
| 93 |
else |
| 94 |
ebegin "Bringing ${IFACE} up via DHCP" |
| 95 |
/sbin/dhcpcd ${dhcpcd_IFACE} ${IFACE} |
| 96 |
eend $? || return $? |
| 97 |
fi |
| 98 |
# Show address retrieved |
| 99 |
i=$(ifconfig ${IFACE} | grep -m1 -o 'inet addr:[^ ]*' | cut -d: -f2) |
| 100 |
[[ -n ${i} ]] && einfo " ${IFACE} received address ${i}" |
| 101 |
# If not a dhcp interface, do ip address stype if config is in this format. |
| 102 |
elif [[ -n ${ipaddr_IFACE} ]]; then |
| 103 |
ebegin "Bringing ${IFACE} up" |
| 104 |
# Force link to up as inet6 will not work without this |
| 105 |
/sbin/ip link set dev ${IFACE} up &>${devnull} |
| 106 |
# Why bother having the aliases in a diffrent section if they can be handled here |
| 107 |
# This covers inet6 addresses as well |
| 108 |
if [[ ${#ipaddr_IFACE[@]} -gt 0 ]]; then |
| 109 |
for ((i = 0; i < ${#ipaddr_IFACE[@]}; i = i + 1)); do |
| 110 |
if [[ ${i} > 0 ]]; then |
| 111 |
einfo " ${IFACE}:${i} ${ipaddr_IFACE[i]%% *}" |
| 112 |
/sbin/ip addr add dev ${IFACE} ${ipaddr_IFACE[i]} label ${IFACE}:${i} >${devnull} && \ |
| 113 |
/sbin/ip addr show dev ${IFACE} | grep "${ipaddr_IFACE[i]%% *}" &>${devnull} |
| 114 |
eend $? || return $? |
| 115 |
else |
| 116 |
einfo " ${IFACE} ${ipaddr_IFACE[i]%% *}" |
| 117 |
/sbin/ip addr add dev ${IFACE} ${ipaddr_IFACE[i]} >${devnull} && \ |
| 118 |
/sbin/ip addr show dev ${IFACE} | grep "${ipaddr_IFACE[i]%% *}" &>${devnull} |
| 119 |
eend $? || return $? |
| 120 |
fi |
| 80 |
|
121 |
|
| 81 |
if [[ ${ifconfig_IFACE} != dhcp ]]; then |
122 |
done |
|
|
123 |
fi |
| 124 |
# Last do ifconfig/iface style addresses |
| 125 |
elif [[ -n ${ifconfig_IFACE} ]]; then |
| 82 |
# Show the address, but catch if this interface will be inet6 only |
126 |
# Show the address, but catch if this interface will be inet6 only |
| 83 |
i=${ifconfig_IFACE%% *} |
127 |
i=${ifconfig_IFACE%% *} |
| 84 |
if [[ ${i} == *.*.*.* ]]; then |
128 |
if [[ ${i} == *.*.*.* ]]; then |
|
Lines 90-110
Link Here
|
| 90 |
/sbin/ifconfig ${IFACE} ${ifconfig_IFACE} >${devnull} && \ |
134 |
/sbin/ifconfig ${IFACE} ${ifconfig_IFACE} >${devnull} && \ |
| 91 |
/sbin/ifconfig ${IFACE} &>${devnull} |
135 |
/sbin/ifconfig ${IFACE} &>${devnull} |
| 92 |
eend $? || return $? |
136 |
eend $? || return $? |
| 93 |
else |
|
|
| 94 |
# Check that eth0 was not brought up by the kernel ... |
| 95 |
if [[ ${status_IFACE} == up ]]; then |
| 96 |
einfo "Keeping kernel configuration for ${IFACE}" |
| 97 |
else |
| 98 |
ebegin "Bringing ${IFACE} up via DHCP" |
| 99 |
/sbin/dhcpcd ${dhcpcd_IFACE} ${IFACE} |
| 100 |
eend $? || return $? |
| 101 |
fi |
| 102 |
# Show address retrieved |
| 103 |
i=$(ifconfig ${IFACE} | grep -m1 -o 'inet addr:[^ ]*' | cut -d: -f2) |
| 104 |
[[ -n ${i} ]] && einfo " ${IFACE} received address ${i}" |
| 105 |
fi |
137 |
fi |
| 106 |
|
138 |
# Only add aliases if in ifconfig/iface format as when in ipaddr format |
| 107 |
if [[ ${#ifconfig_IFACE[@]} -gt 1 ]]; then |
139 |
# aliases get added along with the primary ip address. |
|
|
140 |
if [[ ${#ifconfig_IFACE[@]} -gt 1 ]] && [[ -z ${ipaddr_IFACE} ]]; then |
| 108 |
einfo " Adding aliases" |
141 |
einfo " Adding aliases" |
| 109 |
for ((i = 1; i < ${#ifconfig_IFACE[@]}; i = i + 1)); do |
142 |
for ((i = 1; i < ${#ifconfig_IFACE[@]}; i = i + 1)); do |
| 110 |
ebegin " ${IFACE}:${i} (${ifconfig_IFACE[i]%% *})" |
143 |
ebegin " ${IFACE}:${i} (${ifconfig_IFACE[i]%% *})" |
|
Lines 112-119
Link Here
|
| 112 |
eend $? |
145 |
eend $? |
| 113 |
done |
146 |
done |
| 114 |
fi |
147 |
fi |
| 115 |
|
148 |
# Only add inet6 addresses if not using ipaddr format. |
| 116 |
if [[ -n ${inet6_IFACE} ]]; then |
149 |
if [[ -n ${inet6_IFACE} ]] && [[ -z ${ipaddr_IFACE} ]]; then |
| 117 |
einfo " Adding inet6 addresses" |
150 |
einfo " Adding inet6 addresses" |
| 118 |
for ((i = 0; i < ${#inet6_IFACE[@]}; i = i + 1)); do |
151 |
for ((i = 0; i < ${#inet6_IFACE[@]}; i = i + 1)); do |
| 119 |
ebegin " ${IFACE} inet6 add ${inet6_IFACE[i]}" |
152 |
ebegin " ${IFACE} inet6 add ${inet6_IFACE[i]}" |
|
Lines 121-129
Link Here
|
| 121 |
eend $? |
154 |
eend $? |
| 122 |
done |
155 |
done |
| 123 |
fi |
156 |
fi |
| 124 |
|
157 |
# Set routes with ip route and print them - this can also include default route |
| 125 |
# Set static routes |
158 |
if [[ -n ${iproute_IFACE} ]]; then |
| 126 |
if [[ -n ${routes_IFACE} ]]; then |
159 |
einfo " Adding routes" |
|
|
160 |
for ((i = 0; i < ${#iproute_IFACE[@]}; i = i + 1)); do |
| 161 |
ebegin " ${iproute_IFACE[i]}" |
| 162 |
/sbin/ip route add dev ${IFACE} ${iproute_IFACE[i]} |
| 163 |
eend $? |
| 164 |
done |
| 165 |
# Set static routes with the route command only if iproute was not called |
| 166 |
elif [[ -n ${routes_IFACE} ]] ; then |
| 127 |
einfo " Adding routes" |
167 |
einfo " Adding routes" |
| 128 |
for ((i = 0; i < ${#routes_IFACE[@]}; i = i + 1)); do |
168 |
for ((i = 0; i < ${#routes_IFACE[@]}; i = i + 1)); do |
| 129 |
ebegin " ${routes_IFACE[i]}" |
169 |
ebegin " ${routes_IFACE[i]}" |
|
Lines 131-137
Link Here
|
| 131 |
eend $? |
171 |
eend $? |
| 132 |
done |
172 |
done |
| 133 |
fi |
173 |
fi |
| 134 |
|
|
|
| 135 |
# Set default route if applicable to this interface |
174 |
# Set default route if applicable to this interface |
| 136 |
if [[ ${gateway} == ${IFACE}/* ]]; then |
175 |
if [[ ${gateway} == ${IFACE}/* ]]; then |
| 137 |
local ogw=$(/bin/netstat -rn | awk '$1 == "0.0.0.0" {print $2}') |
176 |
local ogw=$(/bin/netstat -rn | awk '$1 == "0.0.0.0" {print $2}') |
|
Lines 177-236
Link Here
|
| 177 |
# iface_start ran. Instead query for current configuration and bring |
216 |
# iface_start ran. Instead query for current configuration and bring |
| 178 |
# down the interface. |
217 |
# down the interface. |
| 179 |
iface_stop() { |
218 |
iface_stop() { |
| 180 |
local IFACE=${1} i x aliases inet6 count |
219 |
local IFACE=${1} i x count addresses |
| 181 |
|
220 |
# Check and see if interface is dhcp, if so release ip the address. |
| 182 |
# Try to do a simple down (no aliases, no inet6, no dhcp) |
221 |
# This should stop the interface if the only address on it is DHCP. |
| 183 |
aliases="$(/sbin/ifconfig | grep -o "^$IFACE:[0-9]*" | tac)" |
222 |
if [[ -e /var/run/dhcpcd-${IFACE}.pid ]]; then |
| 184 |
inet6="$(ifconfig ${IFACE} | awk '$1 == "inet6" {print $2}')" |
223 |
ebegin "Releasing DHCP lease for ${IFACE}" |
| 185 |
if [[ -z ${aliases} && -z ${inet6} && ! -e /var/run/dhcpcd-${IFACE}.pid ]]; then |
224 |
for ((count = 0; count < 9; count = count + 1)); do |
|
|
225 |
/sbin/dhcpcd -z ${i} &>${devnull} || break |
| 226 |
sleep 1 |
| 227 |
done |
| 228 |
[[ ${count} -lt 9 ]] |
| 229 |
eend $? "Timed out" |
| 230 |
fi |
| 231 |
|
| 232 |
# Gather a list of all the addresses on the specified interface. |
| 233 |
# inet6 addresses will be in the list along with inet addresses. |
| 234 |
addresses="$(/sbin/ip address show ${IFACE} | grep 'inet.*' | awk '{print $2}' | tac)" |
| 235 |
# Bring down each ip address on interface, this can be inet or inet6. |
| 236 |
# This is done in reverse order to stop any possible errors that |
| 237 |
# might crop up. Removing a primary address will cause all secondary |
| 238 |
# address on the interface that are on the same subnet to dissapear. |
| 239 |
if [[ -n ${addresses} ]]; then |
| 186 |
ebegin "Bringing ${IFACE} down" |
240 |
ebegin "Bringing ${IFACE} down" |
| 187 |
/sbin/ifconfig ${IFACE} down &>/dev/null |
241 |
for x in ${addresses}; do |
| 188 |
eend 0 |
242 |
ebegin " ${IFACE} del ${x}" |
| 189 |
return 0 |
243 |
/sbin/ip addr del dev ${IFACE} ${x} |
| 190 |
fi |
244 |
eend $? |
| 191 |
|
245 |
done |
| 192 |
einfo "Bringing ${IFACE} down" |
246 |
fi |
| 193 |
|
247 |
# Stop the interface if it shows link state to be up. |
| 194 |
# Stop aliases before primary interface. |
248 |
if /sbin/ip address show ${IFACE} | grep -o "UP" &>${devnull}; then |
| 195 |
# Note this must be done in reverse order, since ifconfig eth0:1 |
249 |
ebegin " Stopping ${IFACE}" |
| 196 |
# will remove eth0:2, etc. It might be sufficient to simply remove |
250 |
/sbin/ip link set dev ${IFACE} down &>${devnull} |
| 197 |
# the base interface but we're being safe here. |
251 |
eend $? |
| 198 |
for i in ${aliases} ${IFACE}; do |
252 |
fi |
| 199 |
|
|
|
| 200 |
# Delete all the inet6 addresses for this interface |
| 201 |
inet6="$(ifconfig ${i} | awk '$1 == "inet6" {print $2}')" |
| 202 |
if [[ -n ${inet6} ]]; then |
| 203 |
einfo " Removing inet6 addresses" |
| 204 |
for x in ${inet6}; do |
| 205 |
ebegin " ${IFACE} inet6 del ${x}" |
| 206 |
/sbin/ifconfig ${i} inet6 del ${x} |
| 207 |
eend $? |
| 208 |
done |
| 209 |
fi |
| 210 |
|
| 211 |
# Stop DHCP (should be N/A for aliases) |
| 212 |
# Don't trust current configuration... investigate ourselves |
| 213 |
if /sbin/dhcpcd -z ${i} &>${devnull}; then |
| 214 |
ebegin " Releasing DHCP lease for ${IFACE}" |
| 215 |
for ((count = 0; count < 9; count = count + 1)); do |
| 216 |
/sbin/dhcpcd -z ${i} &>${devnull} || break |
| 217 |
sleep 1 |
| 218 |
done |
| 219 |
[[ ${count} -lt 9 ]] |
| 220 |
eend $? "Timed out" |
| 221 |
fi |
| 222 |
ebegin " Stopping ${i}" |
| 223 |
/sbin/ifconfig ${i} down &>${devnull} |
| 224 |
eend 0 |
| 225 |
done |
| 226 |
|
| 227 |
return 0 |
253 |
return 0 |
| 228 |
} |
254 |
} |
| 229 |
|
255 |
|
| 230 |
start() { |
256 |
start() { |
| 231 |
# These variables are set by setup_vars |
257 |
# These variables are set by setup_vars |
| 232 |
local status_IFACE vlans_IFACE dhcpcd_IFACE |
258 |
local status_IFACE vlans_IFACE dhcpcd_IFACE |
| 233 |
local -a ifconfig_IFACE routes_IFACE inet6_IFACE |
259 |
local -a ifconfig_IFACE routes_IFACE inet6_IFACE iproute_IFACE ipaddr_IFACE |
| 234 |
|
260 |
|
| 235 |
# Call user-defined preup function if it exists |
261 |
# Call user-defined preup function if it exists |
| 236 |
if [[ $(type -t preup) == function ]]; then |
262 |
if [[ $(type -t preup) == function ]]; then |