#!/bin/bash
# VLAN Interface Configuration
# Modeled after existing Redhat startup scripts.
#
# This software may be freely redistributed under the terms of the GNU
# public license.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Written by Dale Bewley <dale@bewley.net>
# with contributions from Pascal DeMilly <list.vlan@newgenesys.com>

PATH=/sbin:/usr/sbin:/bin:/usr/bin
VCONFIG=/usr/sbin/vconfig

cd /etc/sysconfig/network-scripts
. network-functions
. /etc/rc.d/init.d/functions
# vconfig defaults such as device name types
. /etc/sysconfig/vlan

CONFIG=$1
[ -f "${CONFIG}" ] || CONFIG=ifcfg-${1}
source_config

if [ "${2}" = "boot" -a "${ONBOOT}" = "no" ]; then
  exit
fi

# we need the hardware name to bind the vlan too. default to eth0 if missing
if [ -z "$PHYSDEVICE" ]; then
  echo "Missing PHYSDEVICE in $CONFIG. Defaulting to eth0."
  logger -p daemon.info -t ifup-vlan \
    $"Missing PHYSDEVICE in $CONFIG. Defaulting to eth0."
  PHYSDEVICE=eth0
fi

# DEVICE could be eth0.1 or vlan1 or... Except eth0.1 cuases problems for ifup. 
# Don't use device names like that right now.
VLAN=`echo $DEVICE | sed "s/^[^0-9]*//"`
VLAN=`echo $VLAN | sed "s/^[0-9]*\.//"`

if [ -z "${NAME_TYPE}" ]; then
  NAME_TYPE=VLAN_PLUS_VID_NO_PAD
fi

[ -x ${VCONFIG} ] || {
  echo $"${VCONFIG} does not exist or is not executable"
  echo $"ifup-vlan for ${DEVICE} exiting"
  logger -p daemon.info -t ifup-vlan \
    $"${VCONFIG} does not exist or is not executable for ${DEVICE}"
  exit 1
}

# do we have 802.1q support in the kernel?
[ -d /proc/net/vlan ] || modprobe 8021q || {
    echo "Module 8021q not found or support for vlan not included in kernel"
    echo "ifup-vlan for $DEVICE exiting"
    logger -p daemon.info -t ifup-vlan \
      "Module 8021q not found or support for vlan not included in kernel"
    exit 1
}

# because this is global it should really only be set once, 
# not once for each device
$VCONFIG set_name_type $NAME_TYPE

# create vlan interface
(logger -p daemon.info -t ifup-vlan \
  $"vconfig adding vlan ${VLAN} as ${DEVICE} on dev ${PHYSDEVICE}" &)&
[ -f "/proc/net/vlan/$DEVICE" ] || $VCONFIG add $PHYSDEVICE $VLAN

# set flags. flag 1, $REORDER_HDR, is 0 or 1
if [ -n "$REORDER_HDR" ] ; then
  $VCONFIG set_flag $DEVICE 1 $REORDER_HDR
fi

# Setup ingress/egress priority and qos
(logger -p daemon.info -t ifup-vlan \
  $"setting ingress/egress priority and qos maps for ${DEVICE}" &)&
for ((que=0;que<=7;que++)); do
  eval in_prio='$'INGRESS_PRIORITY_$que
  eval in_qos='$'INGRESS_QOS_$que
  if [ -n "$in_prio" -a -n "$in_qos" ] ; then
    $VCONFIG set_ingress_map $DEVICE $in_prio $in_qos
  fi
  eval eg_prio='$'EGRESS_PRIORITY_$que
  eval eg_qos='$'EGRESS_QOS_$que
  if [ -n "$eg_prio" -a -n "$eg_qos" ] ; then
    $VCONFIG set_egress_map $DEVICE $eg_prio $eg_qos
  fi
done

# configure IP settings
ifconfig $DEVICE $IPADDR broadcast $BROADCAST netmask $NETMASK
exec /etc/sysconfig/network-scripts/ifup-post ${CONFIG} ${2}
