2 # Copyright 1999-2004 Gentoo Technologies, Inc.
3 # Distributed under the terms of the GNU General Public License v2
5 #NB: Config is in /etc/conf.d/net
7 if [[ -n $NET_DEBUG ]]; then
14 # For pcmcia users. note that pcmcia must be added to the same
15 # runlevel as the net.* script that needs it.
21 if [[ -z "${ifconfig_IFACE}" ]]; then
22 eerror "Please make sure that /etc/conf.d/net has \$ifconfig_$IFACE set"
23 eerror "(or \$iface_$IFACE for old-style configuration)"
26 if [[ -n "${vlans_IFACE}" && ! -x /sbin/vconfig ]]; then
27 eerror "For VLAN (802.1q) support, emerge net-misc/vconfig"
32 # Fix bug 50039 (init.d/net.eth0 localization)
33 # Some other commands in this script might need to be wrapped, but
34 # we'll get them one-by-one. Note that LC_ALL trumps LC_anything_else
35 # according to locale(7)
37 LC_ALL=C /sbin/ifconfig "$@"
40 # setup_vars: setup variables based on $1 and content of /etc/conf.d/net
41 # The following variables are set, which should be declared local by
42 # the calling routine.
43 # status_IFACE (up or '')
44 # vlans_IFACE (space-separated list)
45 # ifconfig_IFACE (array of ifconfig lines, replaces iface_IFACE)
46 # dhcpcd_IFACE (command-line args for dhcpcd)
47 # routes_IFACE (array of route lines)
48 # inet6_IFACE (array of inet6 lines)
49 # ifconfig_fallback_IFACE (fallback ifconfig if dhcp fails)
51 local i iface="${1//\./_}"
53 status_IFACE="$(ifconfig ${1} 2>${devnull} | gawk '$1 == "UP" {print "up"}')"
54 eval vlans_IFACE=\"\$\{iface_${iface}_vlans\}\"
55 eval ifconfig_IFACE=( \"\$\{ifconfig_$iface\[@\]\}\" )
56 eval dhcpcd_IFACE=\"\$\{dhcpcd_$iface\}\"
57 eval routes_IFACE=( \"\$\{routes_$iface\[@\]\}\" )
58 eval inet6_IFACE=( \"\$\{inet6_$iface\[@\]\}\" )
59 eval ifconfig_fallback_IFACE=( \"\$\{ifconfig_fallback_$iface\[@\]\}\" )
61 # BACKWARD COMPATIBILITY: populate the ifconfig_IFACE array
62 # if iface_IFACE is set (fex. iface_eth0 instead of ifconfig_eth0)
63 eval local iface_IFACE=\"\$\{iface_$iface\}\"
64 if [[ -n ${iface_IFACE} && -z ${ifconfig_IFACE} ]]; then
65 # Make sure these get evaluated as arrays
66 local -a aliases broadcasts netmasks
68 # Start with the primary interface
69 ifconfig_IFACE=( "${iface_IFACE}" )
72 eval aliases=( \$\{alias_$iface\} )
73 eval broadcasts=( \$\{broadcast_$iface\} )
74 eval netmasks=( \$\{netmask_$iface\} )
75 for ((i = 0; i < ${#aliases[@]}; i = i + 1)); do
76 ifconfig_IFACE[i+1]="${aliases[i]} ${broadcasts[i]:+broadcast ${broadcasts[i]}} ${netmasks[i]:+netmask ${netmasks[i]}}"
80 # BACKWARD COMPATIBILITY: check for space-separated inet6 addresses
81 if [[ ${#inet6_IFACE[@]} == 1 && ${inet6_IFACE} == *' '* ]]; then
82 inet6_IFACE=( ${inet6_IFACE} )
87 local IFACE=${1} i x retval
88 checkconfig || return 1
90 if [[ ${ifconfig_IFACE} != dhcp ]]; then
91 # Show the address, but catch if this interface will be inet6 only
92 i=${ifconfig_IFACE%% *}
93 if [[ ${i} == *.*.*.* ]]; then
94 ebegin "Bringing ${IFACE} up (${i})"
96 ebegin "Bringing ${IFACE} up"
98 # ifconfig does not always return failure ..
99 ifconfig ${IFACE} ${ifconfig_IFACE} >${devnull} && \
100 ifconfig ${IFACE} up &>${devnull}
103 # Check that eth0 was not brought up by the kernel ...
104 if [[ ${status_IFACE} == up ]]; then
105 einfo "Keeping kernel configuration for ${IFACE}"
107 ebegin "Bringing ${IFACE} up via DHCP"
108 /sbin/dhcpcd ${dhcpcd_IFACE} ${IFACE}
111 if [[ $retval == 0 ]]; then
112 # DHCP succeeded, show address retrieved
113 i=$(ifconfig ${IFACE} | grep -m1 -o 'inet addr:[^ ]*' |
115 [[ -n ${i} ]] && einfo " ${IFACE} received address ${i}"
116 elif [[ -n "${ifconfig_fallback_IFACE}" ]]; then
117 # DHCP failed, try fallback.
118 # Show the address, but catch if this interface will be inet6 only
119 i=${ifconfig_fallback_IFACE%% *}
120 if [[ ${i} == *.*.*.* ]]; then
121 ebegin "Using fallback configuration (${i}) for ${IFACE}"
123 ebegin "Using fallback configuration for ${IFACE}"
125 ifconfig ${IFACE} ${ifconfig_fallback_IFACE} >${devnull} && \
126 ifconfig ${IFACE} up &>${devnull}
134 if [[ ${#ifconfig_IFACE[@]} -gt 1 ]]; then
135 einfo " Adding aliases"
136 for ((i = 1; i < ${#ifconfig_IFACE[@]}; i = i + 1)); do
137 ebegin " ${IFACE}:${i} (${ifconfig_IFACE[i]%% *})"
138 ifconfig ${IFACE}:${i} ${ifconfig_IFACE[i]}
143 if [[ -n ${inet6_IFACE} ]]; then
144 einfo " Adding inet6 addresses"
145 for ((i = 0; i < ${#inet6_IFACE[@]}; i = i + 1)); do
146 ebegin " ${IFACE} inet6 add ${inet6_IFACE[i]}"
147 ifconfig ${IFACE} inet6 add ${inet6_IFACE[i]} >${devnull}
153 if [[ -n ${routes_IFACE} ]]; then
154 einfo " Adding routes"
155 for ((i = 0; i < ${#routes_IFACE[@]}; i = i + 1)); do
156 ebegin " ${routes_IFACE[i]}"
157 /sbin/route add ${routes_IFACE[i]}
162 # Set default route if applicable to this interface
163 if [[ ${gateway} == ${IFACE}/* ]]; then
164 local ogw=$(/bin/netstat -rn | awk '$1 == "0.0.0.0" {print $2}')
165 local gw=${gateway#*/}
166 if [[ ${ogw} != ${gw} ]]; then
167 ebegin " Setting default gateway ($gw)"
169 # First delete any existing route if it was setup by kernel...
170 /sbin/route del default dev ${IFACE} &>${devnull}
172 # Second delete old gateway if it was set...
173 /sbin/route del default gw ${ogw} &>${devnull}
175 # Third add our new default gateway
176 /sbin/route add default gw ${gw} >${devnull}
178 true # need to have some command in here
179 # Note: This originally called stop, which is obviously
180 # wrong since it's calling with a local version of IFACE.
181 # The below code works correctly to abort configuration of
182 # the interface, but is commented because we're assuming
183 # that default route failure should not cause the interface
184 # to be unconfigured.
186 #ewarn "Aborting configuration of ${IFACE}"
193 # Enabling rp_filter causes wacky packets to be auto-dropped by
194 # the kernel. Note that we only do this if it is not set via
195 # /etc/sysctl.conf ...
196 if [[ -e /proc/sys/net/ipv4/conf/${IFACE}/rp_filter && \
197 -z "$(grep -s '^[^#]*rp_filter' /etc/sysctl.conf)" ]]; then
198 echo -n 1 > /proc/sys/net/ipv4/conf/${IFACE}/rp_filter
202 # iface_stop: bring down an interface. Don't trust information in
203 # /etc/conf.d/net since the configuration might have changed since
204 # iface_start ran. Instead query for current configuration and bring
205 # down the interface.
207 local IFACE=${1} i x aliases inet6 count
209 # Try to do a simple down (no aliases, no inet6, no dhcp)
210 aliases="$(ifconfig | grep -o "^$IFACE:[0-9]*" | tac)"
211 inet6="$(ifconfig ${IFACE} | awk '$1 == "inet6" {print $2}')"
212 if [[ -z ${aliases} && -z ${inet6} && ! -e /var/run/dhcpcd-${IFACE}.pid ]]; then
213 ebegin "Bringing ${IFACE} down"
214 ifconfig ${IFACE} down &>/dev/null
219 einfo "Bringing ${IFACE} down"
221 # Stop aliases before primary interface.
222 # Note this must be done in reverse order, since ifconfig eth0:1
223 # will remove eth0:2, etc. It might be sufficient to simply remove
224 # the base interface but we're being safe here.
225 for i in ${aliases} ${IFACE}; do
227 # Delete all the inet6 addresses for this interface
228 inet6="$(ifconfig ${i} | awk '$1 == "inet6" {print $3}')"
229 if [[ -n ${inet6} ]]; then
230 einfo " Removing inet6 addresses"
231 for x in ${inet6}; do
232 ebegin " ${IFACE} inet6 del ${x}"
233 ifconfig ${i} inet6 del ${x}
238 # Stop DHCP (should be N/A for aliases)
239 # Don't trust current configuration... investigate ourselves
240 if /sbin/dhcpcd -z ${i} &>${devnull}; then
241 ebegin " Releasing DHCP lease for ${IFACE}"
242 for ((count = 0; count < 9; count = count + 1)); do
243 /sbin/dhcpcd -z ${i} &>${devnull} || break
249 ebegin " Stopping ${i}"
250 ifconfig ${i} down &>${devnull}
258 # These variables are set by setup_vars
259 local status_IFACE vlans_IFACE dhcpcd_IFACE
260 local -a ifconfig_IFACE routes_IFACE inet6_IFACE
262 # Call user-defined preup function if it exists
263 if [[ $(type -t preup) == function ]]; then
264 einfo "Running preup function"
266 eerror "preup ${IFACE} failed"
271 # Start the primary interface and aliases
273 iface_start ${IFACE} || return 1
277 for vlan in ${vlans_IFACE}; do
278 /sbin/vconfig add ${IFACE} ${vlan} >${devnull}
279 setup_vars ${IFACE}.${vlan}
280 iface_start ${IFACE}.${vlan}
283 # Call user-defined postup function if it exists
284 if [[ $(type -t postup) == function ]]; then
285 einfo "Running postup function"
291 # Call user-defined predown function if it exists
292 if [[ $(type -t predown) == function ]]; then
293 einfo "Running predown function"
297 # Don't depend on setup_vars since configuration might have changed.
298 # Investigate current configuration instead.
300 for vlan in $(ifconfig | grep -o "^${IFACE}\.[^ ]*"); do
302 /sbin/vconfig rem ${vlan} >${devnull}
305 iface_stop ${IFACE} || return 1 # always succeeds, btw
307 # Call user-defined postdown function if it exists
308 if [[ $(type -t postdown) == function ]]; then
309 einfo "Running postdown function"