📄 vlan_cli.c
字号:
/*************************************************************************
* vlan_cli.c
* 与VLAN相关的CLI接口函数
* 印朝晖2001年4月6日创建
* Harbournetworks Copyright . 2000-2001
**************************************************************************/
#ifdef _cplusplus
extern"C"{
#endif
#include "vlan_cli.h"
#include <default.h>
#include "vlan_aux.h"
#include "../switch/port.h"
#include <route/if.h>
#include <linux/in.h>
#include <mod_flag.h>
#define _Test_Debug_
#undef _Test_Debug_
unsigned long ethertype = 0x8100;
/*
0 for 0x8100 normal mode
1 for 0x8200 vMan mode
*/
/**************************************************************************
*
* Vlan CLI Declaration Part
*
***************************************************************************/
#ifdef _Test_Debug_
DEFUN(Show_compileTime_CMD,
ShowCompileTime_CMD,
"show compileTime",
DescStringCommonCreate
"Show version compile time.\n")
{
vty_out(vty,"HammerOS Version 1.2 Compile time : %s %s .\r\n",__DATE__,__TIME__);
}
#endif
/******************************************************************************
* VLAN CLI 1 Declare
* Syntax: config dot1q ethertype <ethertype>
* 语义:配置交换机的802.1q tag值
*******************************************************************************/
DEFUN(ConfigDot1q_Func,
ConfigDot1q_CMD,
"config dot1q ethertype <ethertype> ",
DescStringCommonConfig
"Configures an IEEE 802.1Q Ethertype.\n"
"Configures an IEEE 802.1Q Ethertype.\n"
"Configures an IEEE 802.1Q Ethertype value in hex,the value is default 0x8100.\n")
{
if(argc == 1)
{
return ConfigDot1q(argv[0],vty);
}
return -1;
}
/******************************************************************************
* VLAN CLI 1 Declare
* Syntax: show dot1q
* 语义:查看802.1q tag的 值
*******************************************************************************/
DEFUN(ShowDot1q_Func,
ShowDot1q_CMD,
"show dot1q",
DescStringCommonShow
"Displays switch IEEE 802.1Q Ethertype.\n")
{
if(argc == 0)
{
vty_out(vty,"Switch IEEE 802.1Q Ethertype value : 0x%x .\r\n",ethertype);
}
return -1;
}
/******************************************************************************
* VLAN CLI 1 Declare
* Syntax: create vlan <name>
* 语义:创建名字为name的VLAN,名字最多有NAME_LEN个字符
*******************************************************************************/
DEFUN(ConfigCreateVlan_Func,
ConfigCreateVlan_CMD,
"create vlan <name>",
DescStringCommonCreate
"Create vlan.\n"
"Please input vlan name you want to create.\n")
{
if(argc ==1)
{
if (!check_valid_vlanname(argv[0],vty))
{
vty_out(vty,"Please specify string begin with aphabet and contains only alphabet or number or '_' and %d char at most.%s",NAME_LEN,VTY_NEWLINE);
return -1;
}
else
{
/* reserved for vMan config */
if(!strcmp("vManConfigVlan",argv[0]))
{
vty_out(vty,"Name vManConfigVlan is reserved.\r\n");
return -1;
}
return ConfigCreateVlan(argv[0],vty);
}
}
return -1;
}
/******************************************************************************
* VLAN CLI 2 Declare
* Syntax: config vlan <name> ipaddress <A.B.C.D/M>
* 语义:配置VLAN <name> 的IP地址和掩码
*******************************************************************************/
DEFUN(ConfigVlanIPAddr_Func,
ConfigVlanIpAddr_CMD,
"config vlan <name> ipaddress <A.B.C.D/M>" ,
DescStringCommonConfig
DescStringCommonConfigVlan
DescStringCommonConfigVlanName
"Configure this vlan's ip address and netmask.\n"
"Please input one valid ipaddress and mask length you want to assign to this vlan.\n")
{
if(argc == 2)
{
return ConfigVlanIpAddr(argc,argv,vty);
}
return -1;
}
/******************************************************************************
* VLAN CLI 2 Declare
* Syntax: config vlan <name> ipaddress <A.B.C.D> [<1-32>|<A.B.C.D>]
* 语义:配置VLAN <name> 的IP地址和掩码
*******************************************************************************/
DEFUN(ConfigVlanIPAddrMask_Func,
ConfigVlanIpAddrMask_CMD,
"config vlan <name> ipaddress <A.B.C.D> <A.B.C.D>" ,
DescStringCommonConfig
DescStringCommonConfigVlan
DescStringCommonConfigVlanName
"Configure this vlan's ip address and netmask.\n"
"Please input one valid ipaddress you want to assign to this vlan.\n"
"Please input the netmask (ip address format such as 255.255.255.0).\n")
{
if(argc == 3)
{
return ConfigVlanIpAddr(argc,argv,vty);
}
return -1;
}
/******************************************************************************
* VLAN CLI 2 Declare
* Syntax: no vlan <name> ipaddress
* 语义:delete VLAN <name> 的IP地址和掩码
*******************************************************************************/
DEFUN(NoVlanIPAddr_Func,
NoVlanIpAddr_CMD,
"no vlan <name> ipaddress " ,
"Negate a command or set its defaults.\n"
DescStringCommonConfigVlan
DescStringCommonConfigVlanName
"Remove this vlan's ip address and netmask.\n")
{
if(argc == 1)
{
return NoVlanIpAddr(argc,argv,vty);
}
return -1;
}
/******************************************************************************
* VLAN CLI 3 Declare
* Syntax: config VLAN <name> Tag <1-4095>
* 语义:配置VLAN <name> 的Tag
*******************************************************************************/
DEFUN(ConfigVlanTag_Func,
ConfigVlanTag_CMD,
"config vlan <name> tag <1-4095>" ,
DescStringCommonConfig
DescStringCommonConfigVlan
DescStringCommonConfigVlanName
"Configure this vlan's tag.\n"
"Input the tag value (between 1 and 4095).\n")
{
if(argc ==2)
{
return ConfigVlanTag(argv[0],atoi(argv[1]),vty);
}
return -1;
}
/******************************************************************************
* VLAN CLI 4 Declare
* Syntax: config vlan <name> [add|delete] port <portlist> [tagged|untagged]
* 语义:配置<portlist>属于VLAN
*******************************************************************************/
DEFUN(ConfigVlanPort_Func,
ConfigVlanPort_CMD,
"config vlan <name> [add|delete] port <portlist> [tagged|untagged]" ,
DescStringCommonConfig
DescStringCommonConfigVlan
DescStringCommonConfigVlanName
"Add ports into vlan. \n"
"Delete ports from vlan. \n"
"Change port in the vlan. \n"
"Please input port number list you want change.\n"
"IEEE 802.1q tagged mode.\n"
"IEEE 802.1q untagged mode.\n")
{
if(argc == 4)
{
return ConfigVlanPort(argc,argv,vty);
}
return -1;
}
/******************************************************************************
* VLAN CLI 5 Declare
* Syntax: delete vlan <name>
* 语义:删除名字为name的VLAN
*******************************************************************************/
DEFUN(DeleteVlan_Func,
DeleteVlan_CMD,
"delete vlan <name>" ,
DescStringCommonDelete
"Delete vlan.\n"
"Please input vlan name that you want to delete.\n")
{
if(argc !=1)
return -1;
/* reserved for vMan config */
if(!strcmp("vManConfigVlan",argv[0]))
{
vty_out(vty,"Name vManConfigVlan is reserved.\r\n");
return -1;
}
return ConfigDeleteVlan(argv[0],vty);
}
/******************************************************************************
* VLAN CLI 6 Declare
* Syntax: show vlan {<name>}*1
* 语义:显示VLAN信息,缺省值为all
*******************************************************************************/
DEFUN(ShowVlan_Func,
ShowVlan_CMD,
"show vlan {<name>}*1" ,
DescStringCommonShow
"Show vlan information.\n"
"Please input the vlan name you want to show.\n")
{
if(argc == 1)
return ShowVlan(argv[0],vty);
else if(argc == 0)
return ShowVlan(NULL,vty);
else
return -1;
}
/***************************************************************************
*
* Vlan CLI Implementation Part
*
****************************************************************************/
/* zhutq add for vMan 2001.8.24*/
int createVmanConfigVlan(struct vty * vty)
{
vlanentry * v;
if(ConfigCreateVlan("vManConfigVlan",vty) != -1)
{
ethertype = 0;
v = vlan_get_by_name("vManConfigVlan");
v->flag |= VlanFlagToCPU;
ethertype = 1;
vlan_flush(v);
return -0;
}
else
{
return -1;
}
}
int deleteVmanConfigVlan(struct vty * vty)
{
vlanentry * v;
v = vlan_get_by_name("vManConfigVlan");
if(!v)
{
vty_out(vty,"vlan vManConfigVlan not exist.\r\n");
return -1;
}
else
{
ConfigDeleteVlan("vManConfigVlan",vty);
}
}
/******************************************************************************
* VLAN CLI 1 Implementaion
* Syntax: create vlan <name>
* 语义:创建名字为name的VLAN,名字最多有NAME_LEN个字符
*******************************************************************************/
int ConfigDot1q(char * mode,struct vty * vty)
{
int j;
unsigned long value;
char ** endptr;
char buf[5];
memcpy(buf,mode,4);
if(strlen(mode) > 4)
{
vty_out(vty,"invalid ethertype value.\r\n");
return -1;
}
for(j = 0; j < strlen(mode) ;j++)
{
if (!isxdigit ((int) buf[j]))
{
vty_out(vty,"invalid ethertype value.\r\n");
return -1;
}
}
value = strtoul(mode,endptr,16);
if(value < 0 || value > 0xffff)
{
vty_out(vty,"invalid ethertype value.\r\n");
return -1;
}
#if 0
if(strcmp("normal",mode) == 0)
{
if(ethertype == 0)
{
vty_out(vty,"Switch is already on ethertype normal mode.\r\n");
return -1;
}
dirver_setdevethertype(0x8100);
BEGIN_PARSE_VLAN(j)
{
vlan[j].flag |= VlanFlagToCPU;
vlan_flush(&vlan[j]);
}END_PARSE_VLAN(j)
ethertype = 0;
deleteVmanConfigVlan(vty);
return 0;
}
if(strcmp("vman",mode) == 0)
{
if(ethertype == 1)
{
vty_out(vty,"Switch is already on ethertype normal mode.\r\n");
return -1;
}
if(vlan_get_by_name("vManConfigVlan"))
{
vty_out(vty,"Please delete vlan named vManConfigVlan first.\r\n");
return -1;
}
dirver_setdevethertype(0x8200);
BEGIN_PARSE_VLAN(j)
{
vlan[j].flag &= ~VlanFlagToCPU;
vlan_flush(&vlan[j]);
}END_PARSE_VLAN(j)
ethertype = 1;
createVmanConfigVlan(vty);
return 0;
}
#endif
ethertype = value;
dirver_setdevethertype(value);
BEGIN_PARSE_VLAN(j)
{
vlan_flush(&vlan[j]);
}END_PARSE_VLAN(j)
return 1;
}
/******************************************************************************
* VLAN CLI 1 Implementaion
* Syntax: create vlan <name>
* 语义:创建名字为name的VLAN,名字最多有NAME_LEN个字符
*******************************************************************************/
int ConfigCreateVlan(char * name,struct vty * vty)
{
vlanentry * v;
/* check duplicate name */
if(vlan_get_by_name(name))
{
vty_out(vty,"vlan %s already exist in system \r\n",name);
return -1;
}
v = vlan_create(name);
if(!v)
{
vty_out(vty,"error: system not allow to create more vlan. \r\n");
return -1;
}
return 1;
}
/******************************************************************************
* VLAN CLI 2 Implementaion
* Syntax: config vlan <name> ipaddress <A.B.C.D> [<1-32>|<A.B.C.D>]
* 语义:配置VLAN <name> 的IP地址和掩码
*******************************************************************************/
int ConfigVlanIpAddr(int argc, char ** argv,struct vty * vty)
{
struct _vlanentry * v = vlan_get_by_name(argv[0]);
int masklen,mask;
long ipaddr;
char bak[20];
int pos,len;
char *strMask;
char * strIpaddr;
char * Mask;
if(!v)
{
vty_out(vty,"vlan %s not exist \r\n",argv[0]);
return -1;
}
if(argc == 2)
{
len = strlen(argv[1]);
if(len > 18)
{
vty_out(vty,"Invalid IP Address.\r\n");
return -1;
}
strncpy(bak,argv[1],len);
strMask = (char *)strchr(bak,'/');
pos = strMask - bak;
strIpaddr = kmalloc(pos + 1,VLAN_MOD_ID);
memset(strIpaddr,0,pos + 1);
Mask = kmalloc(len - pos,VLAN_MOD_ID);
memset(Mask,0,len - pos);
if(strMask)
{
memcpy(strIpaddr,bak,pos);
memcpy(Mask,strMask + 1,len - pos - 1);
}
ipaddr = get_long_from_ipdotstring(strIpaddr);
if(strIpaddr)
kfree(strIpaddr);
if(ipaddr== -1)
{
vty_out(vty,"Invalid IP Address.\r\n");
return -1;
}
if (strlen(Mask) >=7 )
{
masklen = get_masklen_from_ipdotstring(Mask);
if (masklen <= 0)
{
if (vty)
{
vty_out(vty,"Invalid netmask.%s",VTY_NEWLINE);
}
if(Mask)
kfree(Mask);
return -1;
}
}
else
{
masklen = atoi(Mask);
if (masklen <= 0)
{
if (vty)
{
vty_out(vty,"Invalid netmask.%s",VTY_NEWLINE);
}
if(Mask)
kfree(Mask);
return -1;
}
}
if(Mask)
kfree(Mask);
}
if(argc == 3)
{
ipaddr = get_long_from_ipdotstring(argv[1]);
if(ipaddr== -1)
{
vty_out(vty,"Invalid IP Address.\r\n");
return -1;
}
if (strlen(argv[2]) >=7 )
{
masklen = get_masklen_from_ipdotstring(argv[2]);
if (masklen <= 0)
{
if (vty)
{
vty_out(vty,"Invalid netmask.%s",VTY_NEWLINE);
}
return -1;
}
}
else
{
if (vty)
{
vty_out(vty,"Invalid netmask.%s",VTY_NEWLINE);
}
return -1;
}
}
if(ipaddr == 0 )
{
vty_out( vty , "Invalid IP Address.\r\n" );
return -1;
}
/* if(!(IN_CLASSA(ipaddr) || IN_CLASSB(ipaddr) || IN_CLASSC(ipaddr) || IN_CLASSD(ipaddr) || IN_EXPERIMENTAL(ipaddr)))
{
vty_out( vty , "Invalid IP Address.\r\n" );
return -1;
}
*/
if((ipaddr & 0xff000000) == 0)
{
vty_out( vty , "Invalid IP Address.\r\n" );
return -1;
}
mask = get_long_from_masklen(masklen);
/* not allow D class Address and Broadcast Address and Loopback addr */
if(IN_CLASSD(ipaddr))
{
vty_out(vty,"Can't set multicast Ip address for vlan.\r\n");
return -1;
}
if(IN_EXPERIMENTAL(ipaddr))
{
vty_out(vty,"Can't set experimnetal Ip address for vlan.\r\n");
return -1;
}
if(IN_BROADCAST(ipaddr))
{
vty_out(vty,"Can't set broadcast Ip address for vlan.\r\n");
return -1;
}
if(IN_LOOPBACK(ipaddr))
{
vty_out(vty,"Can't set loopbackIp address for vlan.\r\n");
return -1;
}
if((ipaddr | mask) == mask )
{
vty_out(vty,"Can't set network or subnetwork address for vlan.\r\n");
return -1;
}
if( (ipaddr | mask) == 0xffffffff )
{
vty_out(vty,"Can't set broadcast Ip address for vlan.\r\n");
return -1;
}
/*检查是否存在相同的网段,在1.2版本中Per VLAN Per Interface,
不允许出现冲突的网段
在这借用RM 模块的接口函数来检查,是权宜之计,风格很差,
不过到1.3整个软件以接口为中心,就在接口部分来配置地址了*/
{
struct in_addr addr;
struct interface * ifp;
addr.s_addr = ipaddr;
if(ifp = if_lookup_address(addr))
if(strcmp(ifp->name,v->name))
{
vty_out(vty,"IP address is conflict with vlan %s \r\n",ifp->name);
return -1;
}
}
return vlan_set_ipaddr(v,ipaddr,masklen);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -