| 1 | #!/bin/bash |
|---|
| 2 | # Network Interface Configuration System |
|---|
| 3 | # Copyright (c) 1996-2005 Red Hat, Inc. all rights reserved. |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License, version 2, |
|---|
| 7 | # as published by the Free Software Foundation. |
|---|
| 8 | # |
|---|
| 9 | # This program is distributed in the hope that it will be useful, |
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | # GNU General Public License for more details. |
|---|
| 13 | # |
|---|
| 14 | # You should have received a copy of the GNU General Public License |
|---|
| 15 | # along with this program; if not, write to the Free Software |
|---|
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|---|
| 17 | |
|---|
| 18 | . /etc/init.d/functions |
|---|
| 19 | |
|---|
| 20 | cd /etc/sysconfig/network-scripts |
|---|
| 21 | . /etc/sysconfig/network-scripts/network-functions |
|---|
| 22 | |
|---|
| 23 | [ -f /etc/sysconfig/network ] && . /etc/sysconfig/network |
|---|
| 24 | |
|---|
| 25 | CONFIG=${1} |
|---|
| 26 | |
|---|
| 27 | need_config ${CONFIG} |
|---|
| 28 | |
|---|
| 29 | source_config |
|---|
| 30 | |
|---|
| 31 | # Old BOOTP variable |
|---|
| 32 | if [ "${BOOTP}" = "yes" ]; then |
|---|
| 33 | BOOTPROTO=bootp |
|---|
| 34 | fi |
|---|
| 35 | |
|---|
| 36 | if [ "${BOOTPROTO}" = "bootp" -o "${BOOTPROTO}" = "dhcp" ]; then |
|---|
| 37 | DYNCONFIG=true |
|---|
| 38 | fi |
|---|
| 39 | |
|---|
| 40 | # load the module associated with that device |
|---|
| 41 | # /sbin/modprobe ${REALDEVICE} |
|---|
| 42 | is_available ${REALDEVICE} |
|---|
| 43 | |
|---|
| 44 | # remap, if the device is bound with a MAC address and not the right device num |
|---|
| 45 | # bail out, if the MAC does not fit |
|---|
| 46 | if [ -n "${HWADDR}" ]; then |
|---|
| 47 | FOUNDMACADDR=`get_hwaddr ${REALDEVICE}` |
|---|
| 48 | if [ "${FOUNDMACADDR}" != "${HWADDR}" -a "${FOUNDMACADDR}" != "${MACADDR}" ]; then |
|---|
| 49 | echo $"Device ${DEVICE} has different MAC address than expected, ignoring." |
|---|
| 50 | exit 1 |
|---|
| 51 | fi |
|---|
| 52 | fi |
|---|
| 53 | |
|---|
| 54 | if [ "${TYPE}" = "Bridge" ]; then |
|---|
| 55 | if [ ! -x /usr/sbin/brctl ]; then |
|---|
| 56 | echo $"Bridge support not available: brctl not found" |
|---|
| 57 | exit 1 |
|---|
| 58 | fi |
|---|
| 59 | if [ ! -d /sys/class/net/${DEVICE}/bridge ]; then |
|---|
| 60 | /usr/sbin/brctl addbr ${DEVICE} || exit 1 |
|---|
| 61 | fi |
|---|
| 62 | [ -n "${DELAY}" ] && /usr/sbin/brctl setfd ${DEVICE} ${DELAY} |
|---|
| 63 | [ -n "${GCINT}" ] && /usr/sbin/brctl setgcint ${DEVICE} ${GCINT} |
|---|
| 64 | [ -n "${STP}" ] && /usr/sbin/brctl stp ${DEVICE} ${STP} |
|---|
| 65 | fi |
|---|
| 66 | |
|---|
| 67 | # now check the real state |
|---|
| 68 | is_available ${REALDEVICE} || { |
|---|
| 69 | if [ -n "$alias" ]; then |
|---|
| 70 | echo $"$alias device ${DEVICE} does not seem to be present, delaying initialization." |
|---|
| 71 | else |
|---|
| 72 | echo $"Device ${DEVICE} does not seem to be present, delaying initialization." |
|---|
| 73 | fi |
|---|
| 74 | exit 1 |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | # is the device wireless? If so, configure wireless device specifics |
|---|
| 78 | is_wireless_device ${DEVICE} && . /etc/sysconfig/network-scripts/ifup-wireless |
|---|
| 79 | |
|---|
| 80 | if [ -n "${BRIDGE}" -a -x /usr/sbin/brctl ]; then |
|---|
| 81 | if [ ! -d /sys/class/net/${BRIDGE}/bridge ]; then |
|---|
| 82 | /usr/sbin/brctl addbr ${BRIDGE} 2>/dev/null |
|---|
| 83 | fi |
|---|
| 84 | /sbin/ip addr flush dev ${DEVICE} 2>/dev/null |
|---|
| 85 | /sbin/ip link set dev ${DEVICE} up |
|---|
| 86 | if [ -n "$ETHTOOL_OPTS" ] ; then |
|---|
| 87 | /sbin/ethtool -s ${REALDEVICE} $ETHTOOL_OPTS |
|---|
| 88 | fi |
|---|
| 89 | [ -n "${LINKDELAY}" ] && /bin/sleep ${LINKDELAY} |
|---|
| 90 | /usr/sbin/brctl addif ${BRIDGE} ${DEVICE} |
|---|
| 91 | # Upon adding a device to a bridge, |
|---|
| 92 | # it's necessary to make radvd reload its config |
|---|
| 93 | [ -r /var/run/radvd/radvd.pid ] && kill -HUP `cat /var/run/radvd/radvd.pid` |
|---|
| 94 | exit 0 |
|---|
| 95 | fi |
|---|
| 96 | |
|---|
| 97 | # this isn't the same as the MAC in the configuration filename. It is |
|---|
| 98 | # available as a configuration option in the config file, forcing the kernel |
|---|
| 99 | # to think an ethernet card has a different MAC address than it really has. |
|---|
| 100 | if [ -n "${MACADDR}" ]; then |
|---|
| 101 | ip link set dev ${DEVICE} address ${MACADDR} |
|---|
| 102 | fi |
|---|
| 103 | if [ -n "${MTU}" ]; then |
|---|
| 104 | ip link set dev ${DEVICE} mtu ${MTU} |
|---|
| 105 | fi |
|---|
| 106 | |
|---|
| 107 | # slave device? |
|---|
| 108 | if [ "${SLAVE}" = yes -a "${ISALIAS}" = no -a "${MASTER}" != "" ]; then |
|---|
| 109 | grep -wq "${DEVICE}" /sys/class/net/${MASTER}/bonding/slaves || { |
|---|
| 110 | /sbin/ip link set dev ${DEVICE} down |
|---|
| 111 | echo "+${DEVICE}" > /sys/class/net/${MASTER}/bonding/slaves 2>/dev/null |
|---|
| 112 | } |
|---|
| 113 | if [ -n "$ETHTOOL_OPTS" ] ; then |
|---|
| 114 | /sbin/ethtool -s ${REALDEVICE} $ETHTOOL_OPTS |
|---|
| 115 | fi |
|---|
| 116 | |
|---|
| 117 | exit 0 |
|---|
| 118 | fi |
|---|
| 119 | |
|---|
| 120 | # Bonding initialization. For DHCP, we need to enslave the devices early, |
|---|
| 121 | # so it can actually get an IP. |
|---|
| 122 | if [ "$ISALIAS" = no ] && is_bonding_device ${DEVICE} ; then |
|---|
| 123 | |
|---|
| 124 | /sbin/ip link set dev ${DEVICE} down |
|---|
| 125 | |
|---|
| 126 | # add the bits to setup driver parameters here |
|---|
| 127 | for arg in $BONDING_OPTS ; do |
|---|
| 128 | key=${arg%%=*}; |
|---|
| 129 | value=${arg##*=}; |
|---|
| 130 | if [ "${key}" = "arp_ip_target" ]; then |
|---|
| 131 | OLDIFS=$IFS; |
|---|
| 132 | IFS=','; |
|---|
| 133 | for arp_ip in $value; do |
|---|
| 134 | echo +$arp_ip > /sys/class/net/${DEVICE}/bonding/$key |
|---|
| 135 | done |
|---|
| 136 | IFS=$OLDIFS; |
|---|
| 137 | else |
|---|
| 138 | echo $value > /sys/class/net/${DEVICE}/bonding/$key |
|---|
| 139 | fi |
|---|
| 140 | done |
|---|
| 141 | |
|---|
| 142 | /sbin/ip link set dev ${DEVICE} up |
|---|
| 143 | [ -n "${LINKDELAY}" ] && /bin/sleep ${LINKDELAY} |
|---|
| 144 | for device in `LANG=C grep -l "^[[:space:]]*MASTER=${DEVICE}" /etc/sysconfig/network-scripts/ifcfg-*` ; do |
|---|
| 145 | is_ignored_file "$device" && continue |
|---|
| 146 | /sbin/ifup ${device##*/} |
|---|
| 147 | done |
|---|
| 148 | |
|---|
| 149 | # add the bits to setup the needed post enslavement parameters |
|---|
| 150 | for arg in $BONDING_OPTS ; do |
|---|
| 151 | key=${arg%%=*}; |
|---|
| 152 | value=${arg##*=}; |
|---|
| 153 | if [ "${key}" = "primary" ]; then |
|---|
| 154 | echo $value > /sys/class/net/${DEVICE}/bonding/$key |
|---|
| 155 | fi |
|---|
| 156 | done |
|---|
| 157 | fi |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | if [ -n "${DYNCONFIG}" -a -x /sbin/dhclient ]; then |
|---|
| 161 | # Remove any temporary references which were previously added to dhclient config |
|---|
| 162 | if [ -w /etc/dhclient-${DEVICE}.conf ] ; then |
|---|
| 163 | LC_ALL=C grep -v "# temporary RHL ifup addition" /etc/dhclient-${DEVICE}.conf > /etc/dhclient-${DEVICE}.conf.ifupnew 2> /dev/null |
|---|
| 164 | cat /etc/dhclient-${DEVICE}.conf.ifupnew > /etc/dhclient-${DEVICE}.conf |
|---|
| 165 | rm -f /etc/dhclient-${DEVICE}.conf.ifupnew |
|---|
| 166 | fi |
|---|
| 167 | if [[ "${PERSISTENT_DHCLIENT}" = [yY1]* ]]; then |
|---|
| 168 | ONESHOT=""; |
|---|
| 169 | else |
|---|
| 170 | ONESHOT="-1"; |
|---|
| 171 | fi; |
|---|
| 172 | if [ -n "${DHCP_HOSTNAME}" ]; then |
|---|
| 173 | # Send a host-name to the DHCP server (requ. by some dhcp servers). |
|---|
| 174 | if [ -w /etc/dhclient-${DEVICE}.conf ] ; then |
|---|
| 175 | if ! LC_ALL=C grep "send *host-name *\"${DHCP_HOSTNAME}\"" /etc/dhclient-${DEVICE}.conf > /dev/null 2>&1 ; then |
|---|
| 176 | echo "send host-name \"${DHCP_HOSTNAME}\"; # temporary RHL ifup addition" >> /etc/dhclient-${DEVICE}.conf |
|---|
| 177 | fi |
|---|
| 178 | elif ! [ -e /etc/dhclient-${DEVICE}.conf ] ; then |
|---|
| 179 | echo "send host-name \"${DHCP_HOSTNAME}\"; # temporary RHL ifup addition" >> /etc/dhclient-${DEVICE}.conf |
|---|
| 180 | fi |
|---|
| 181 | fi |
|---|
| 182 | # allow users to use generic '/etc/dhclient.conf' (as documented in manpage!) |
|---|
| 183 | # if per-device file doesn't exist or is empty |
|---|
| 184 | if [ -s /etc/dhclient-${DEVICE}.conf ]; then |
|---|
| 185 | DHCLIENTCONF="-cf /etc/dhclient-${DEVICE}.conf"; |
|---|
| 186 | else |
|---|
| 187 | DHCLIENTCONF=''; |
|---|
| 188 | fi; |
|---|
| 189 | # copy any lease obtained by the initrd |
|---|
| 190 | if [ -f /dev/.dhclient-${DEVICE}.leases ] ; then |
|---|
| 191 | mv -f /dev/.dhclient-${DEVICE}.leases /var/lib/dhclient/dhclient-${DEVICE}.leases |
|---|
| 192 | [ -x /sbin/restorecon ] && restorecon /var/lib/dhclient/dhclient-${DEVICE}.leases > /dev/null 2>&1 |
|---|
| 193 | fi |
|---|
| 194 | DHCLIENTARGS="${DHCLIENTARGS} ${ONESHOT} -q ${DHCLIENTCONF} -lf /var/lib/dhclient/dhclient-${DEVICE}.leases -pf /var/run/dhclient-${DEVICE}.pid" |
|---|
| 195 | echo |
|---|
| 196 | echo -n $"Determining IP information for ${DEVICE}..." |
|---|
| 197 | if [[ "${PERSISTENT_DHCLIENT}" != [yY1]* ]] && check_link_down ${DEVICE}; then |
|---|
| 198 | echo $" failed; no link present. Check cable?" |
|---|
| 199 | ip link set dev ${DEVICE} down >/dev/null 2>&1 |
|---|
| 200 | exit 1 |
|---|
| 201 | fi |
|---|
| 202 | |
|---|
| 203 | if [ -n "$ETHTOOL_OPTS" ] ; then |
|---|
| 204 | /sbin/ethtool -s ${REALDEVICE} $ETHTOOL_OPTS |
|---|
| 205 | fi |
|---|
| 206 | |
|---|
| 207 | if /sbin/dhclient ${DHCLIENTARGS} ${DEVICE} ; then |
|---|
| 208 | echo $" done." |
|---|
| 209 | else |
|---|
| 210 | echo $" failed." |
|---|
| 211 | exit 1 |
|---|
| 212 | fi |
|---|
| 213 | # end dynamic device configuration |
|---|
| 214 | else |
|---|
| 215 | if [ -z "${IPADDR}" ]; then |
|---|
| 216 | # enable device without IP, useful for e.g. PPPoE |
|---|
| 217 | ip link set dev ${REALDEVICE} up |
|---|
| 218 | if [ -n "$ETHTOOL_OPTS" ] ; then |
|---|
| 219 | /sbin/ethtool -s ${REALDEVICE} $ETHTOOL_OPTS |
|---|
| 220 | fi |
|---|
| 221 | [ -n "${LINKDELAY}" ] && /bin/sleep ${LINKDELAY} |
|---|
| 222 | |
|---|
| 223 | /etc/sysconfig/network-scripts/ifup-ipv6 ${CONFIG} |
|---|
| 224 | exec /etc/sysconfig/network-scripts/ifup-post ${CONFIG} ${2} |
|---|
| 225 | fi |
|---|
| 226 | |
|---|
| 227 | expand_config |
|---|
| 228 | |
|---|
| 229 | [ -n "${ARP}" ] && \ |
|---|
| 230 | ip link set dev ${REALDEVICE} $(toggle_value arp $ARP) |
|---|
| 231 | |
|---|
| 232 | if ! ip link set dev ${REALDEVICE} up ; then |
|---|
| 233 | echo $"Failed to bring up ${DEVICE}." |
|---|
| 234 | exit 1 |
|---|
| 235 | fi |
|---|
| 236 | |
|---|
| 237 | if [ -n "$ETHTOOL_OPTS" ] ; then |
|---|
| 238 | /sbin/ethtool -s ${REALDEVICE} $ETHTOOL_OPTS |
|---|
| 239 | fi |
|---|
| 240 | |
|---|
| 241 | [ -n "${LINKDELAY}" ] && /bin/sleep ${LINKDELAY} |
|---|
| 242 | |
|---|
| 243 | if [ "${DEVICE}" = "lo" ]; then |
|---|
| 244 | SCOPE="scope host" |
|---|
| 245 | else |
|---|
| 246 | SCOPE=${SCOPE:-} |
|---|
| 247 | fi |
|---|
| 248 | |
|---|
| 249 | if [ -n "$SRCADDR" ]; then |
|---|
| 250 | SRC="src $SRCADDR" |
|---|
| 251 | else |
|---|
| 252 | SRC= |
|---|
| 253 | fi |
|---|
| 254 | |
|---|
| 255 | if [ "${REALDEVICE}" != "lo" ]; then |
|---|
| 256 | if ! LC_ALL=C ip addr ls ${REALDEVICE} | LC_ALL=C grep -q "${IPADDR}/${PREFIX}" ; then |
|---|
| 257 | if ! /sbin/arping -q -c 2 -w 3 -D -I ${REALDEVICE} ${IPADDR} ; then |
|---|
| 258 | echo $"Error, some other host already uses address ${IPADDR}." |
|---|
| 259 | exit 1 |
|---|
| 260 | fi |
|---|
| 261 | if ! ip addr add ${IPADDR}/${PREFIX} \ |
|---|
| 262 | brd ${BROADCAST:-+} dev ${REALDEVICE} ${SCOPE} label ${DEVICE}; then |
|---|
| 263 | echo $"Error adding address ${IPADDR} for ${DEVICE}." |
|---|
| 264 | fi |
|---|
| 265 | fi |
|---|
| 266 | |
|---|
| 267 | if [ -n "$SRCADDR" ]; then |
|---|
| 268 | sysctl -w "net.ipv4.conf.${REALDEVICE}.arp_filter=1" >/dev/null 2>&1 |
|---|
| 269 | fi |
|---|
| 270 | |
|---|
| 271 | # update ARP cache of neighboring computers |
|---|
| 272 | /sbin/arping -q -A -c 1 -I ${REALDEVICE} ${IPADDR} |
|---|
| 273 | ( sleep 2; |
|---|
| 274 | /sbin/arping -q -U -c 1 -I ${REALDEVICE} ${IPADDR} ) > /dev/null 2>&1 < /dev/null & |
|---|
| 275 | fi |
|---|
| 276 | |
|---|
| 277 | # Set a default route. |
|---|
| 278 | if [ -z "${GATEWAYDEV}" -o "${GATEWAYDEV}" = "${REALDEVICE}" ]; then |
|---|
| 279 | # set up default gateway. replace if one already exists |
|---|
| 280 | if [ -n "${GATEWAY}" -a "`ipcalc --network ${GATEWAY} ${NETMASK} 2>/dev/null`" = "NETWORK=${NETWORK}" ]; then |
|---|
| 281 | ip route replace default ${METRIC:+metric $METRIC} \ |
|---|
| 282 | via ${GATEWAY} ${WINDOW:+window $WINDOW} ${SRC} \ |
|---|
| 283 | ${GATEWAYDEV:+dev $GATEWAYDEV} |
|---|
| 284 | elif [ "${GATEWAYDEV}" = "${DEVICE}" ]; then |
|---|
| 285 | ip route replace default ${METRIC:+metric $METRIC} \ |
|---|
| 286 | ${SRC} ${WINDOW:+window $WINDOW} dev ${REALDEVICE} |
|---|
| 287 | fi |
|---|
| 288 | fi |
|---|
| 289 | fi |
|---|
| 290 | |
|---|
| 291 | # Add Zeroconf route. |
|---|
| 292 | if [ -z "${NOZEROCONF}" -a "${ISALIAS}" = "no" -a "${REALDEVICE}" != "lo" ]; then |
|---|
| 293 | ip route add 169.254.0.0/16 dev ${REALDEVICE} metric $((1000 + $(cat /sys/class/net/${REALDEVICE}/ifindex))) scope link |
|---|
| 294 | fi |
|---|
| 295 | |
|---|
| 296 | # IPv6 initialisation? |
|---|
| 297 | /etc/sysconfig/network-scripts/ifup-ipv6 ${CONFIG} |
|---|
| 298 | if [[ "${DHCPV6C}" = [Yy1]* ]] && [ -x /sbin/dhcp6c ]; then |
|---|
| 299 | /sbin/dhcp6c ${DEVICE}; |
|---|
| 300 | dhcp6_pid=(`/bin/ps -eo 'pid,args' | /bin/grep "dhcp6c ${DEVICE}" | egrep -v grep`); |
|---|
| 301 | echo ${dhcp6_pid[0]} > /var/run/dhcp6c_${DEVICE}.pid |
|---|
| 302 | fi |
|---|
| 303 | |
|---|
| 304 | if [ "${IPX}" = yes ]; then |
|---|
| 305 | /etc/sysconfig/network-scripts/ifup-ipx ${DEVICE} |
|---|
| 306 | fi |
|---|
| 307 | |
|---|
| 308 | exec /etc/sysconfig/network-scripts/ifup-post ${CONFIG} ${2} |
|---|
| 309 | |
|---|