#!/bin/sh

####################
#                  #
#  VLAN filtering  #
#                  #
####################


############
# CHANGELOG
#===========
# 2010/12/07    CRMS00278858    Claire Dechriste
#               add sliceid command


# debug mode
#set -x


#
# constants
#

# define tools
ETH=/bin/eth
DBG=/usr/bin/dbg
CLISET=/usr/bin/CLISettings

# define ports
PORT_PC=0
PORT_LAN=1
PORT_INT=8

# define masks
MASK_PC=0x1
MASK_ALL=0x103

# global control 5 register address
ADDR_GCR5=0x3041a030


#
# check vlan use
#

# get vlan status
vlanenabled=`$CLISET get CURRENT ENETCFG_VLAN_ENABLE | tr [A-Z] [a-z]`

# if vlan is used
if [ "$vlanenabled" = "true" ]; then
	# get vlan id
 	vlanid=`$CLISET get CURRENT ENETCFG_VLAN`
else
	# no vlan => no filtering, bye bye
	printf "No VLAN to filter.\n"
	exit 0
fi


#
# check pc port status
#

# get pc port status
pcport=`$CLISET get CURRENT SECUCFG_PC_PORT | tr [A-Z] [a-z]`

# if port is disabled
if [ "$pcport" = "true" ]; then
	# no filtering needed, exit
	printf "PC port disabled, no filtering.\n"
	exit 0
fi


#
# check filtering option
#

# get filtering status
filteron=`$CLISET get DM SECUCFG_PC_PORT_VLAN_FILTER | tr [A-Z] [a-z]`
if [ "$filteron" != "true" ]; then
	printf "VLAN filtering disabled.\n"
	exit 0
fi


#
# configure vlan
#

# check if vlan is enabled
if [ "`$ETH vlan en ?`" = "disable" ]; then
	# no, initialize vlan first
	$ETH vlan init
	# then enable it
	$ETH vlan en 1
fi

# populate table for all possible vlan
$ETH vlan table create all
# set mask for all vlan
$ETH vlan table add all $MASK_ALL 0
# delete phone vlan
$ETH vlan table del $vlanid
# recreate phone vlan
$ETH vlan table create $vlanid
# set specific mask for the vlan (INT and LAN)
$ETH vlan table add $vlanid 0x102 0


#
# allow non 802.1q frames when vlan is enabled
#

# NOTE: must be done after any vlan init as the latest is
# clearing the untagged frames preservation bit (it is
# also cleared by default at boot)

# get current value of global control 5 register
tmp="0x`$DBG rl $ADDR_GCR5 | sed \"s/$ADDR_GCR5: \(.*\)/\1/\"`"

# check if En_Preserv_non_1q_frame bit has not been set
if [ $(($tmp & 0x40)) -eq 0 ]; then
	# set bit 6
	tmp="$(($tmp | 0x40))"

	# write new register value
	$DBG wl $ADDR_GCR5 $tmp > /dev/null
fi


#
# configure cfp rule
#

# init a new struct
$ETH cfp rule struct init
# enable it
$ETH cfp rule struct en 1
# set related ports
$ETH cfp rule struct portmap $MASK_PC
#crms00278858
$ETH cfp rule struct sliceid 0
# set customer tag search
# XXX EEEEEEK no details given on mask parameter
$ETH cfp rule struct ctag 0x3 $vlanid 0xFFF
# set ation to: drop
$ETH cfp rule struct actiondrop
# set action: vlan bypass
$ETH cfp rule struct actionvlanbypass
# finish rule by adding it
$ETH cfp rule add -1

printf "VLAN (%d) filtering enabled.\n" "$vlanid"

