#!/bin/bash
# $Id: 50-ifup.dev 301 2004-12-04 12:56:34Z zoz $
#
# Network hotplug policy agent for Linux 2.6 kernels
#
# Kernel NET hotplug params include:
#
#        ACTION=%s [add or remove]
#        INTERFACE=%s
#

# Protect against renaming
if [ -n "$DEVNAME" -a "$INTERFACE" != "$DEVNAME" ]; then
    INTERFACE=$DEVNAME
    export INTERFACE
fi

: ${INTERFACE?Bad invocation: \$INTERFACE is not set}

SYSFS=needed
cd /etc/hotplug
. ./hotplug.functions

case $ACTION in

    add|register)
        case $INTERFACE in

            # interfaces that are registered after being "up" (?)
            modem*|dsl*|ppp*|ippp*|isdn*|plip*|lo*|irda*|dummy*|ipsec*|tun*|tap*|wifi*)
                debug_mesg Event for $INTERFACE will be ignored
                exit 0
                ;;
            # interfaces that are registered then brought up

            *)
                # There are some NICs which set their MAC address when loading
                # their firmware and unfortunately load the firmware not until
                # they are brought up. Therefore we set them up and down.
		if [ -f $SYSFS/class/net/$INTERFACE/address ]; then
                    macadd=`sed "s/[:0]//g" $SYSFS/class/net/$INTERFACE/address`
                    if [ -z "$macadd" ] ; then
                        ip link set up dev $INTERFACE
                        ip link set down dev $INTERFACE
                        debug_mesg "MAC address after ip up/down: " \
                            `cat $SYSFS/class/net/$INTERFACE/address`
                    fi
		fi
                debug_mesg invoke ifup $INTERFACE -o hotplug
                exec /sbin/ifup $INTERFACE -o hotplug
                ;;

        esac
        ;;

    remove|unregister)
        case $INTERFACE in

            # interfaces that are registered after being "up" (?)
            modem*|dsl*|ppp*|ippp*|isdn*|plip*|lo*|irda*|dummy*|ipsec*|tun*|tap*|wifi*)
                debug_mesg Event for $INTERFACE will be ignored
                exit 0
                ;;
            # interfaces that are registered then brought up

            *)
                debug_mesg invoke ifdown $INTERFACE -o hotplug
                exec /sbin/ifdown $INTERFACE -o hotplug
                ;;

        esac
        ;;

     *)
        debug_mesg NET $ACTION event not supported
        exit 1
        ;;

esac
