From 85bdc5b94c8a1730d3077f361297028cfd8b9b78 Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sun, 16 Sep 2018 20:17:57 +0200 Subject: MTK mt76 hwnat implementation --- .../files/usr/sbin/7621_15d_hwnat_disable.sh | 4 + .../files/usr/sbin/7621_15d_hwnat_enable.sh | 4 + package/base-files/files/usr/sbin/hwnat-disable.sh | 4 + package/base-files/files/usr/sbin/hwnat-enable.sh | 4 + package/ramips/applications/hwnat/Makefile | 62 ++ package/ramips/applications/hwnat/files/hwnat | 18 + package/ramips/applications/hwnat/src/Makefile | 24 + package/ramips/applications/hwnat/src/ac.c | 269 ++++++++ package/ramips/applications/hwnat/src/ac_api.c | 52 ++ package/ramips/applications/hwnat/src/ac_api.h | 6 + package/ramips/applications/hwnat/src/acl.c | 365 ++++++++++ package/ramips/applications/hwnat/src/acl_api.c | 52 ++ package/ramips/applications/hwnat/src/acl_api.h | 16 + package/ramips/applications/hwnat/src/hw_nat.c | 631 +++++++++++++++++ package/ramips/applications/hwnat/src/hwnat_api.c | 763 +++++++++++++++++++++ package/ramips/applications/hwnat/src/hwnat_api.h | 44 ++ package/ramips/applications/hwnat/src/mtr.c | 322 +++++++++ package/ramips/applications/hwnat/src/mtr_api.c | 54 ++ package/ramips/applications/hwnat/src/mtr_api.h | 16 + package/ramips/applications/hwnat/src/util.c | 87 +++ package/ramips/applications/hwnat/src/util.h | 2 + 21 files changed, 2799 insertions(+) create mode 100755 package/base-files/files/usr/sbin/7621_15d_hwnat_disable.sh create mode 100755 package/base-files/files/usr/sbin/7621_15d_hwnat_enable.sh create mode 100755 package/base-files/files/usr/sbin/hwnat-disable.sh create mode 100755 package/base-files/files/usr/sbin/hwnat-enable.sh create mode 100755 package/ramips/applications/hwnat/Makefile create mode 100755 package/ramips/applications/hwnat/files/hwnat create mode 100755 package/ramips/applications/hwnat/src/Makefile create mode 100755 package/ramips/applications/hwnat/src/ac.c create mode 100755 package/ramips/applications/hwnat/src/ac_api.c create mode 100755 package/ramips/applications/hwnat/src/ac_api.h create mode 100755 package/ramips/applications/hwnat/src/acl.c create mode 100755 package/ramips/applications/hwnat/src/acl_api.c create mode 100755 package/ramips/applications/hwnat/src/acl_api.h create mode 100755 package/ramips/applications/hwnat/src/hw_nat.c create mode 100755 package/ramips/applications/hwnat/src/hwnat_api.c create mode 100755 package/ramips/applications/hwnat/src/hwnat_api.h create mode 100755 package/ramips/applications/hwnat/src/mtr.c create mode 100755 package/ramips/applications/hwnat/src/mtr_api.c create mode 100755 package/ramips/applications/hwnat/src/mtr_api.h create mode 100755 package/ramips/applications/hwnat/src/util.c create mode 100755 package/ramips/applications/hwnat/src/util.h diff --git a/package/base-files/files/usr/sbin/7621_15d_hwnat_disable.sh b/package/base-files/files/usr/sbin/7621_15d_hwnat_disable.sh new file mode 100755 index 0000000..3837e88 --- /dev/null +++ b/package/base-files/files/usr/sbin/7621_15d_hwnat_disable.sh @@ -0,0 +1,4 @@ +iwpriv ra0 set hw_nat_register=0 +iwpriv rax0 set hw_nat_register=0 +rmmod hw_nat.ko + diff --git a/package/base-files/files/usr/sbin/7621_15d_hwnat_enable.sh b/package/base-files/files/usr/sbin/7621_15d_hwnat_enable.sh new file mode 100755 index 0000000..c0fd623 --- /dev/null +++ b/package/base-files/files/usr/sbin/7621_15d_hwnat_enable.sh @@ -0,0 +1,4 @@ +insmod /lib/modules/ralink/hw_nat.ko +iwpriv ra0 set hw_nat_register=1 +iwpriv rax0 set hw_nat_register=1 + diff --git a/package/base-files/files/usr/sbin/hwnat-disable.sh b/package/base-files/files/usr/sbin/hwnat-disable.sh new file mode 100755 index 0000000..3837e88 --- /dev/null +++ b/package/base-files/files/usr/sbin/hwnat-disable.sh @@ -0,0 +1,4 @@ +iwpriv ra0 set hw_nat_register=0 +iwpriv rax0 set hw_nat_register=0 +rmmod hw_nat.ko + diff --git a/package/base-files/files/usr/sbin/hwnat-enable.sh b/package/base-files/files/usr/sbin/hwnat-enable.sh new file mode 100755 index 0000000..c0fd623 --- /dev/null +++ b/package/base-files/files/usr/sbin/hwnat-enable.sh @@ -0,0 +1,4 @@ +insmod /lib/modules/ralink/hw_nat.ko +iwpriv ra0 set hw_nat_register=1 +iwpriv rax0 set hw_nat_register=1 + diff --git a/package/ramips/applications/hwnat/Makefile b/package/ramips/applications/hwnat/Makefile new file mode 100755 index 0000000..77a2ead --- /dev/null +++ b/package/ramips/applications/hwnat/Makefile @@ -0,0 +1,62 @@ +# +# hua.shao@mediatek.com +# +# MTK Property Software. +# + +include $(TOPDIR)/rules.mk + +PKG_NAME:=hwnat +PKG_RELEASE:=4200 + +PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME) +PKG_KCONFIG:=RALINK_MT7620 RALINK_MT7621 RALINK_MT7628 RALINK_MT7623 HNAT_V2 +PKG_CONFIG_DEPENDS:=$(foreach c, $(PKG_KCONFIG),$(if $(CONFIG_$c),CONFIG_$(c))) + + +include $(INCLUDE_DIR)/package.mk +include $(INCLUDE_DIR)/kernel.mk + +define Package/hwnat + SECTION:=MTK Properties + CATEGORY:=MTK Properties + TITLE:=Hardware NAT command + DEPENDS:=@PACKAGE_kmod-hw_nat + SUBMENU:=Applications +endef + +define Package/hwnat/description + An program to query info from hwnat driver +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) + $(CP) ./src/* $(PKG_BUILD_DIR)/ +endef + +TARGET_CFLAGS += \ + -I$(LINUX_DIR)/include \ + -I$(LINUX_DIR)/arch/mips/include \ + -I$(LINUX_DIR)/net/nat/hw_nat/ \ + $(foreach c, $(PKG_KCONFIG),$(if $(CONFIG_$c),-DCONFIG_$(c)=$(CONFIG_$c))) + +MAKE_FLAGS += \ + CFLAGS="$(TARGET_CFLAGS)" + + +define Build/Configure +endef + +define Package/hwnat/install + $(INSTALL_DIR) $(1)/bin + $(INSTALL_DIR) $(1)/etc/init.d/ + $(INSTALL_BIN) $(PKG_BUILD_DIR)/ac $(1)/bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/acl $(1)/bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/hwnat $(1)/bin + $(INSTALL_BIN) $(PKG_BUILD_DIR)/mtr $(1)/bin + $(INSTALL_BIN) ./files/* $(1)/etc/init.d +endef + + +$(eval $(call BuildPackage,hwnat)) + diff --git a/package/ramips/applications/hwnat/files/hwnat b/package/ramips/applications/hwnat/files/hwnat new file mode 100755 index 0000000..ce350f3 --- /dev/null +++ b/package/ramips/applications/hwnat/files/hwnat @@ -0,0 +1,18 @@ +#!/bin/sh /etc/rc.common + +START=91 +STOP=91 + +USE_PROCD=1 + +start_service() { +# insmod /lib/modules/ralink/hw_nat.ko + mknod /dev/hwnat0 c 220 0 +} + +stop_service() { +# rmmod hw_nat.ko + echo 'do nothing' > /dev/null +} + + diff --git a/package/ramips/applications/hwnat/src/Makefile b/package/ramips/applications/hwnat/src/Makefile new file mode 100755 index 0000000..c19b166 --- /dev/null +++ b/package/ramips/applications/hwnat/src/Makefile @@ -0,0 +1,24 @@ +EXEC = hwnat acl ac mtr + +HWNAT_OBJS = hw_nat.o hwnat_api.o util.o +AC_OBJS = ac.o ac_api.o util.o +ACL_OBJS = acl.o acl_api.o util.o +MTR_OBJS = mtr.o mtr_api.o util.o + +all: $(EXEC) + +hwnat: $(HWNAT_OBJS) $(CONF_H) + $(CC) $(LDFLAGS) -o $@ $(HWNAT_OBJS) + +acl: $(ACL_OBJS) + $(CC) $(LDFLAGS) -o $@ $(ACL_OBJS) + +ac: $(AC_OBJS) + $(CC) $(LDFLAGS) -o $@ $(AC_OBJS) + +mtr: $(MTR_OBJS) + $(CC) $(LDFLAGS) -o $@ $(MTR_OBJS) + +clean: + -rm -f $(EXEC) *.elf *.gdb *.o + diff --git a/package/ramips/applications/hwnat/src/ac.c b/package/ramips/applications/hwnat/src/ac.c new file mode 100755 index 0000000..ed8acdc --- /dev/null +++ b/package/ramips/applications/hwnat/src/ac.c @@ -0,0 +1,269 @@ +#include +#include +#include +#include +#include +#include + +#include "ac_ioctl.h" +#include "ac_api.h" + +void show_usage(void) +{ + + printf("Add Mac Upload Accounting Rule\n"); + printf("ac -a -m [Mac] \n"); + + printf("Add Mac Download Accounting Rule\n"); + printf("ac -b -m [Mac] \n"); + + printf("Del Mac Upload Accounting Rule\n"); + printf("ac -c -m [Mac]\n"); + + printf("Del Mac download Accounting Rule\n"); + printf("ac -d -m [Mac]\n"); + + printf("Add Vlan Upload Accounting Rule\n"); + printf("ac -A -k [Vlan] \n"); + + printf("Add Vlan Download Accounting Rule\n"); + printf("ac -B -k [Vlan] \n"); + + printf("Del Vlan Upload Accounting Rule\n"); + printf("ac -C -k [Vlan]\n"); + + printf("Del Vlan download Accounting Rule\n"); + printf("ac -D -k [Vlan]\n"); + + printf("Add IP Upload Accounting Rule\n"); + printf("ac -e -i [IpS] -j [IpE]\n"); + + printf("Add IP Download Accounting Rule\n"); + printf("ac -f -i [IpS] -j [IpE] \n"); + + printf("Del IP Upload Accounting Rule\n"); + printf("ac -g -i [IpS] -j [IpE] \n"); + + printf("Del IP Download Accounting Rule\n"); + printf("ac -h -i [IpS] -j [IpE]\n"); + + printf("Show Upload Packet Count of the Mac\n"); + printf("ac -p -m [Mac] \n"); + + printf("Show Download Packet Count of the Mac\n"); + printf("ac -q -m [Mac]\n"); + + printf("Show Upload Byte Count of the Mac\n"); + printf("ac -r -m [Mac]\n"); + + printf("Show Download Byte Count of the Mac\n"); + printf("ac -s -m [Mac]\n"); + + printf("Show Upload Packet Count of the Vlan\n"); + printf("ac -P -k [Vlan] \n"); + + printf("Show Download Packet Count of the Vlan\n"); + printf("ac -Q -k [Vlan]\n"); + + printf("Show Upload Byte Count of the Vlan\n"); + printf("ac -R -k [Vlan]\n"); + + printf("Show Download Byte Count of the Vlan\n"); + printf("ac -S -k [Vlan]\n"); + + printf("Show Upload Packet Count of the IP\n"); + printf("ac -t -i [IpS] -j [IpE]\n"); + + printf("Show Download Packet Count of the IP\n"); + printf("ac -u -i [IpS] -j [IpE]\n"); + + printf("Show Upload Byte Count of the IP\n"); + printf("ac -v -i [IpS] -j [IpE]\n"); + + printf("Show Download Byte Count of the IP\n"); + printf("ac -w -i [IpS] -j [IpE]\n"); + + printf("Clear Ac Table\n"); + printf("ac -z\n"); + +} + +int main(int argc, char *argv[]) +{ + int opt; + char options[] = "ABCDabcdefghPQRSpqrstuvwz?m:i:j:k:"; + int fd; + int method=-1; + struct ac_args args; + int result; + + + fd = open("/dev/"AC_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"AC_DEVNAME); + return 0; + } + + if(argc < 2) + { + show_usage(); + close(fd); + return 0; + } + + while ((opt = getopt (argc, argv, options)) != -1) + { + switch (opt) + { + case 'a': + method=AC_ADD_MAC_UL_ENTRY; + break; + case 'b': + method=AC_ADD_MAC_DL_ENTRY; + break; + case 'c': + method=AC_DEL_MAC_UL_ENTRY; + break; + case 'd': + method=AC_DEL_MAC_DL_ENTRY; + break; + case 'e': + method=AC_ADD_IP_UL_ENTRY; + break; + case 'f': + method=AC_ADD_IP_DL_ENTRY; + break; + case 'g': + method=AC_DEL_IP_UL_ENTRY; + break; + case 'h': + method=AC_DEL_IP_DL_ENTRY; + break; + case 'A': + method=AC_ADD_VLAN_UL_ENTRY; + break; + case 'B': + method=AC_ADD_VLAN_DL_ENTRY; + break; + case 'C': + method=AC_DEL_VLAN_UL_ENTRY; + break; + case 'D': + method=AC_DEL_VLAN_DL_ENTRY; + break; + case 'p': + method=AC_GET_MAC_UL_PKT_CNT; + break; + case 'q': + method=AC_GET_MAC_DL_PKT_CNT; + break; + case 'r': + method=AC_GET_MAC_UL_BYTE_CNT; + break; + case 's': + method=AC_GET_MAC_DL_BYTE_CNT; + break; + case 't': + method=AC_GET_IP_UL_PKT_CNT; + break; + case 'u': + method=AC_GET_IP_DL_PKT_CNT; + break; + case 'v': + method=AC_GET_IP_UL_BYTE_CNT; + break; + case 'w': + method=AC_GET_IP_DL_BYTE_CNT; + break; + case 'P': + method=AC_GET_VLAN_UL_PKT_CNT; + break; + case 'Q': + method=AC_GET_VLAN_DL_PKT_CNT; + break; + case 'R': + method=AC_GET_VLAN_UL_BYTE_CNT; + break; + case 'S': + method=AC_GET_VLAN_DL_BYTE_CNT; + break; + case 'z': /* CleanTbl */ + method=AC_CLEAN_TBL; + break; + case 'm': /* Mac */ + str_to_mac(args.mac, optarg); + break; + case 'i': /* IP */ + str_to_ip(&args.ip_s, optarg); + break; + case 'j': + str_to_ip(&args.ip_e, optarg); + break; + case 'k': + args.vid = strtoll(optarg, NULL, 10); + break; + + case '?': /* Help */ + show_usage(); + break; + } + } + + + switch(method) + { + case AC_ADD_VLAN_UL_ENTRY: + case AC_ADD_VLAN_DL_ENTRY: + case AC_ADD_MAC_UL_ENTRY: + case AC_ADD_MAC_DL_ENTRY: + case AC_ADD_IP_UL_ENTRY: + case AC_ADD_IP_DL_ENTRY: + case AC_CLEAN_TBL: + SetAcEntry(&args, method); + result = args.result; + break; + case AC_DEL_VLAN_UL_ENTRY: + case AC_DEL_VLAN_DL_ENTRY: + case AC_DEL_MAC_UL_ENTRY: + case AC_DEL_MAC_DL_ENTRY: + case AC_DEL_IP_UL_ENTRY: + case AC_DEL_IP_DL_ENTRY: + SetAcEntry(&args, method); + result = args.result; + break; + case AC_GET_VLAN_UL_PKT_CNT: + case AC_GET_VLAN_DL_PKT_CNT: + case AC_GET_MAC_UL_PKT_CNT: + case AC_GET_MAC_DL_PKT_CNT: + case AC_GET_IP_UL_PKT_CNT: + case AC_GET_IP_DL_PKT_CNT: + case AC_GET_VLAN_UL_BYTE_CNT: + case AC_GET_VLAN_DL_BYTE_CNT: + case AC_GET_MAC_UL_BYTE_CNT: + case AC_GET_MAC_DL_BYTE_CNT: + case AC_GET_IP_UL_BYTE_CNT: + case AC_GET_IP_DL_BYTE_CNT: + result = GetAcEntry(&args, method); + printf("Count=%lld\n",args.cnt); + break; + default: + result = AC_FAIL; + } + + if(result == AC_SUCCESS) + { + printf("done\n"); + } + else if (result == AC_TBL_FULL) + { + printf("table full\n"); + } + else + { + printf("fail\n"); + } + + close(fd); + return 0; +} diff --git a/package/ramips/applications/hwnat/src/ac_api.c b/package/ramips/applications/hwnat/src/ac_api.c new file mode 100755 index 0000000..0d1832a --- /dev/null +++ b/package/ramips/applications/hwnat/src/ac_api.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include "ac_ioctl.h" + +int SetAcEntry(struct ac_args *opt, unsigned int cmd) +{ + int fd; + + fd = open("/dev/"AC_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"AC_DEVNAME); + return AC_FAIL; + } + + if(ioctl(fd, cmd, opt)<0) + { + printf("AC_API: ioctl error\n"); + close(fd); + return AC_FAIL; + } + + close(fd); + return AC_SUCCESS; +} + + +int GetAcEntry(struct ac_args *opt, unsigned int cmd) +{ + int fd; + + fd = open("/dev/"AC_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"AC_DEVNAME); + return AC_FAIL; + } + + if(ioctl(fd, cmd, opt)<0) + { + printf("AC_API: ioctl error\n"); + close(fd); + return AC_FAIL; + } + + close(fd); + return AC_SUCCESS; +} diff --git a/package/ramips/applications/hwnat/src/ac_api.h b/package/ramips/applications/hwnat/src/ac_api.h new file mode 100755 index 0000000..dd5a40f --- /dev/null +++ b/package/ramips/applications/hwnat/src/ac_api.h @@ -0,0 +1,6 @@ +#ifndef __AC_API +#define __AC_API + +int SetAcEntry(struct ac_args *opt, unsigned int cmd); +int GetAcEntry(struct ac_args *opt, unsigned int cmd); +#endif diff --git a/package/ramips/applications/hwnat/src/acl.c b/package/ramips/applications/hwnat/src/acl.c new file mode 100755 index 0000000..6eb6f1e --- /dev/null +++ b/package/ramips/applications/hwnat/src/acl.c @@ -0,0 +1,365 @@ +#include +#include +#include +#include +#include +#include + +#include "acl_ioctl.h" +#include "acl_api.h" + +void show_usage(void) +{ + printf("Add SDMAC Entry for Any Protocol\n"); + printf("acl -A -n [SDMAC] -U[UP] -u [Allow/Deny/FP]\n"); + printf("Ex: acl -A -n 00:11:22:33:44:55 -u Deny \n\n"); + printf("Ex: acl -A -n 00:11:22:33:44:55 -U 3 -u FP \n\n"); + + printf("Add SMAC to DIP Entry for Any Protocol\n"); + printf("acl -a -n [SMAC] -q [DipS] -r [DipE] -U[UP] -u [Allow/Deny/FP]\n"); + printf("Ex: acl -a -n 00:11:22:33:44:55 -q 10.10.10.3 -r 10.10.10.5 -u Deny \n\n"); + printf("Ex: acl -a -n 00:11:22:33:44:55 -q 10.10.10.3 -r 10.10.10.5 -U 3 -u FP \n\n"); + + printf("Add SMAC to DIP Entry for Tcp Protocol\n"); + printf("acl -b -n [SMAC] -q [DipS] -r [DipE] -s [DpS] -t [DpE] -U[UP] -u [Allow/Deny/FP]\n"); + printf("Ex: acl -b -n 00:11:22:33:44:55 -q 10.10.10.3 -r 10.10.10.5 -s 1 -t 1024 -u Deny\n\n"); + + printf("SMAC to DIP Entry for Udp Protocol\n"); + printf("acl -c -n [SMAC] -q [DipS] -r [DipE] -s [DpS] -t [DpE] -U[UP] -u [Allow/Deny/FP]\n"); + printf("Ex: acl -c -n 00:11:22:33:44:55 -q 10.10.10.3 -r 10.10.10.5 -s 1 -t 1024 -u Deny\n\n"); + + printf("Del SDMAC Entry for Any Protocol\n"); + printf("acl -D -n [SMAC]\n"); + printf("Ex: acl -D -n 00:11:22:33:44:55 \n\n"); + + printf("Del SMAC to DIP Entry for Any Protocol\n"); + printf("acl -d -n [SMAC] -q [DipS] -r [DipE]\n"); + printf("Ex: acl -d -n 00:11:22:33:44:55 -q 10.10.10.3 -r 10.10.10.5\n\n"); + + printf("Del SMAC to DIP Entry for Tcp Protocol\n"); + printf("acl -e -n [SMAC] -q [DipS] -r [DipE] -s [DpS] -t [DpE]\n"); + printf("Ex: acl -e -n 00:11:22:33:44:55 -q 10.10.10.3 -r 10.10.10.5 -s 1 -t 1024\n\n"); + + printf("Del SMAC to DIP Entry for Udp Protocol\n"); + printf("acl -f -n [SMAC] -q [DipS] -r [DipE] -s [DpS] -t [DpE]\n"); + printf("Ex: acl -f -n 00:11:22:33:44:55 -q 10.10.10.3 -r 10.10.10.5 -s 1 -t 1024\n\n"); + + printf("Add SIP to DIP Entry for Any Protocol\n"); + printf("acl -H -o [SipS] -p [SipE] -q [DipS] -r [DipE] -U[UP] -u [Allow/Deny/FP]\n"); + printf("Ex: acl -H -o 10.10.10.3 -p 10.10.10.5 -q 10.10.20.3 -r 10.10.20.3 -u Deny\n\n"); + + + printf("Add SIP to DIP Entry for Tcp Protocol\n"); + printf("acl -h -o [SipS] -p [SipE] -q [DipS] -r [DipE] -s [DpS] -t [DpE] -U[UP] -u [Allow/Deny/FP]\n"); + printf("Ex: acl -h -o 10.10.10.3 -p 10.10.10.5 -q 10.10.20.3 -r 10.10.20.3 -s 1 -t 1024 -u Deny\n\n"); + + + printf("Add SIP to DIP Entry for Udp Protocol\n"); + printf("acl -i -o [SipS] -p [SipE] -q [DipS] -r [DipE] -s [DpS] -t [DpE]-U[UP] -u [Allow/Deny/FP]\n"); + printf("Ex: acl -i -o 10.10.10.3 -p 10.10.10.5 -q 10.10.20.3 -r 10.10.20.3 -s 1 -t 1024 -u Deny\n\n"); + + printf("Del SIP to DIP Entry for Any Protocol\n"); + printf("acl -j -o [SipS] -p [SipE] -q [DipS] -r [DipE]\n"); + printf("Ex: acl -j -o 10.10.10.3 -p 10.10.10.5 -q 10.10.20.3 -r 10.10.20.3\n\n"); + + + printf("Del SIP to DIP Entry for Tcp Protocol\n"); + printf("acl -k -o [SipS] -p [SipE] -q [DipS] -r [DipE] -s [DpS] -t [DpE]\n"); + printf("Ex: acl -k -o 10.10.10.3 -p 10.10.10.5 -q 10.10.20.3 -r 10.10.20.3 -s 1 -t 1024\n\n"); + + + printf("Del SIP to DIP Entry for Udp Protocol\n"); + printf("acl -l -o [SipS] -p [SipE] -q [DipS] -r [DipE] -s [DpS] -t [DpE]\n"); + printf("Ex: acl -l -o 10.10.10.3 -p 10.10.10.5 -q 10.10.20.3 -r 10.10.20.3 -s 1 -t 1024\n\n"); + printf("Del All Entries\n"); + printf("acl -m\n\n"); + + printf("Add S/DMAC ETYPE VID PROTOCOL SIP DIP SP DP Entry\n"); + printf("acl -E -n[SMAC] -N[DMAC] -P[ESW Port] -Z[Ethertype] -S[Protocol] -o[SipS] -p[SipE] -q[DipS] -r[DipE] -s[DpS] -t[DpE] -v[SpS] -x[SpE] -y[TosS] -z[TosE] -F[TCP/UDP/ANY] -V[VID] -u[Allow/Deny/FP]\n"); + printf("Ex: acl -E -o 10.10.10.3 -p 10.10.10.5 -q 10.10.20.3 -r 10.10.20.3 -s 1 -t 1024 -F UDP -u Deny\n\n"); + + + printf("Del S/DMAC ETYPE VID PROTOCOL SIP DIP SP DP Entry\n"); + printf("acl -G -n[SMAC] -N[DMAC] -P[ESW Port] -Z[Ethertype] -S[Protocol] -o[SipS] -p[SipE] -q[DipS] -r[DipE] -s[DpS] -t[DpE] -v[SpS] -x[SpE] -y[TosS] -z[TosE] -F[TCP/UDP/ANY] -V[VID] \n"); + printf("Ex: acl -G -o 10.10.10.3 -p 10.10.10.5 -q 10.10.20.3 -r 10.10.20.3 -s 1 -t 1024 -F UDP\n\n"); + + printf("Show All ACL Entry\n"); + printf("Ex: acl -g\n\n"); + +} + +int main(int argc, char *argv[]) +{ + int opt; + char options[] = "AabcDdEefGgHhijklm?F:n:N:o:P:p:q:r:s:S:t:u:U:v:x:y:V:z:Z:"; + int fd; + struct acl_args args; + struct acl_list_args *args2; + int method=-1; + int result; + int i; + + memset(&args, 0, sizeof(struct acl_args)); + args.pn = 7; /* Default do not care*/ + /* Max 511 acl entries */ + args2=malloc(sizeof(struct acl_list_args) + sizeof(struct acl_args)*511); + if (NULL == args2) + { + printf(" Allocate memory for acl_list_args and acl_args failed.\n"); + return 0; + } + fd = open("/dev/"ACL_DEVNAME, O_RDONLY); + + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"ACL_DEVNAME); + free(args2); + return 0; + } + + if(argc < 2) + { + show_usage(); + free(args2); + close(fd); + return 0; + } + + while ((opt = getopt (argc, argv, options)) != -1) + { + switch (opt) + { + case 'A': + method=ACL_ADD_SDMAC_ANY; + break; + case 'a': + method=ACL_ADD_SMAC_DIP_ANY; + break; + case 'b': + method=ACL_ADD_SMAC_DIP_TCP; + break; + case 'c': + method=ACL_ADD_SMAC_DIP_UDP; + break; + case 'D': + method=ACL_DEL_SDMAC_ANY; + break; + case 'd': + method=ACL_DEL_SMAC_DIP_ANY; + break; + case 'e': + method=ACL_DEL_SMAC_DIP_TCP; + break; + case 'E': + method=ACL_ADD_SMAC_DMAC_ETYPE_VID_SIP_DIP_TOS_PORT; + break; + case 'F': + if(strcasecmp(optarg,"TCP")==0) + { + args.L4=ACL_PROTO_TCP; + } + else if(strcasecmp(optarg,"UDP")==0) + { + args.L4=ACL_PROTO_UDP; + } + else if(strcasecmp(optarg,"ANY")==0) + { + args.L4=ACL_PROTO_ANY; + } + else + { + printf("Error: -t TCP or UDP or ANY\n"); + free(args2); + close(fd); + return 0; + } + + break; + case 'G': + method=ACL_DEL_SMAC_DMAC_ETYPE_VID_SIP_DIP_TOS_PORT; + break; + case 'f': + method=ACL_DEL_SMAC_DIP_UDP; + break; + case 'H': + method=ACL_ADD_SIP_DIP_ANY; + break; + case 'g': + method = ACL_GET_ALL_ENTRIES; + break; + case 'h': + method=ACL_ADD_SIP_DIP_TCP; + break; + case 'i': + method=ACL_ADD_SIP_DIP_UDP; + break; + case 'j': + method=ACL_DEL_SIP_DIP_ANY; + break; + case 'k': + method=ACL_DEL_SIP_DIP_TCP; + break; + case 'l': + method=ACL_DEL_SIP_DIP_UDP; + break; + case 'm': + method=ACL_CLEAN_TBL; + break; + case 'n': /* source mac address */ + str_to_mac(args.mac, optarg); + break; + case 'N': /* destination mac address */ + str_to_mac(args.dmac, optarg); + break; + case 'o': /* start of sip */ + str_to_ip(&args.sip_s, optarg); + break; + case 'p': /* end of sip */ + str_to_ip(&args.sip_e, optarg); + break; + case 'P': /* Port Number */ + args.pn=strtoll(optarg, NULL, 10); + break; + case 'q': /* start of dip */ + str_to_ip(&args.dip_s, optarg); + break; + case 'r': /* end of dip */ + str_to_ip(&args.dip_e, optarg); + break; + case 's': /* start of dp */ + args.dp_s=strtoll(optarg, NULL, 10); + break; + case 't': /* end of dp */ + args.dp_e=strtoll(optarg, NULL, 10); + break; + case 'S': /* Protocol */ + args.protocol=strtoll(optarg, NULL, 10); + break; + + case 'v': /* start of sp */ + args.sp_s=strtoll(optarg, NULL, 10); + break; + case 'x': /* end of sp */ + args.sp_e=strtoll(optarg, NULL, 10); + break; + case 'y': /* start of tos */ + args.tos_s=strtoll(optarg, NULL, 10); + break; + case 'z': /* end of tos */ + args.tos_e=strtoll(optarg, NULL, 10); + break; + case 'Z': /* ethertype */ + args.ethertype=strtoll(optarg, NULL, 16); + break; + case 'V': /* VID */ + args.vid=strtoll(optarg, NULL, 10); + break; + case 'u': /* Deny/Allow */ + if(strcasecmp(optarg,"Deny")==0) + { + args.method=ACL_DENY_RULE; + } + else if(strcasecmp(optarg,"Allow")==0) + { + args.method=ACL_ALLOW_RULE; + } + else if(strcasecmp(optarg,"FP")==0) + { + args.method=ACL_PRIORITY_RULE; + } + else + { + printf("Error: -t Deny or Allow\n"); + free(args2); + close(fd); + return 0; + } + break; + case 'U': /* User Priority */ + args.up=strtoll(optarg, NULL, 10); + break; + case '?': + default: + show_usage(); + free(args2); + close(fd); + return 0; + } + } + switch(method) + { + case ACL_ADD_SDMAC_ANY: + case ACL_ADD_ETYPE_ANY: + case ACL_ADD_SMAC_DIP_ANY: + case ACL_ADD_SMAC_DIP_TCP: + case ACL_ADD_SMAC_DIP_UDP: + case ACL_DEL_SDMAC_ANY: + case ACL_DEL_ETYPE_ANY: + case ACL_DEL_SMAC_DIP_ANY: + case ACL_DEL_SMAC_DIP_TCP: + case ACL_DEL_SMAC_DIP_UDP: + case ACL_ADD_SIP_DIP_ANY: + case ACL_ADD_SIP_DIP_TCP: + case ACL_ADD_SIP_DIP_UDP: + case ACL_ADD_SMAC_DMAC_ETYPE_VID_SIP_DIP_TOS_PORT: + case ACL_DEL_SIP_DIP_ANY: + case ACL_DEL_SIP_DIP_TCP: + case ACL_DEL_SIP_DIP_UDP: + case ACL_DEL_SMAC_DMAC_ETYPE_VID_SIP_DIP_TOS_PORT: + case ACL_CLEAN_TBL: + SetAclEntry(&args, method); + result = args.result; + break; + case ACL_GET_ALL_ENTRIES: + AclGetAllEntries(args2); + result = args2->result; + + printf("Total Entry Count = %d\n",args2->num_of_entries); + for(i=0; inum_of_entries; i++) + { + printf("#%d :SMAC=%02X:%02X:%02X:%02X:%02X:%02X => DMAC=%02X:%02X:%02X:%02X:%02X:%02X PROTOCOL=0x%2x\n", \ + i, args2->entries[i].mac[0], args2->entries[i].mac[1], args2->entries[i].mac[2], \ + args2->entries[i].mac[3], args2->entries[i].mac[4], args2->entries[i].mac[5], \ + args2->entries[i].dmac[0], args2->entries[i].dmac[1],args2->entries[i].dmac[2], \ + args2->entries[i].dmac[3], args2->entries[i].dmac[4],args2->entries[i].dmac[5], \ + args2->entries[i].protocol); + + printf(" :SIP %u.%u.%u.%u->%u.%u.%u.%u=>DIP %u.%u.%u.%u->%u.%u.%u.%u SP %d->%d=>DP %d->%d TOS:0x%2x->0x%2x VID:%d ETYPE=0x%4x TCP_UDP=0/TCP=1/UDP=2:%d PN:%d\n\r", \ + NIPQUAD(args2->entries[i].sip_s), \ + NIPQUAD(args2->entries[i].sip_e), \ + NIPQUAD(args2->entries[i].dip_s), \ + NIPQUAD(args2->entries[i].dip_e), \ + args2->entries[i].sp_s, \ + args2->entries[i].sp_e, \ + args2->entries[i].dp_s, \ + args2->entries[i].dp_e, \ + args2->entries[i].tos_s, \ + args2->entries[i].tos_e, \ + args2->entries[i].vid, \ + args2->entries[i].ethertype, \ + args2->entries[i].L4, \ + args2->entries[i].pn); + } + break; + default: + result = ACL_FAIL; + } + + + if(result == ACL_SUCCESS) + { + printf("done\n"); + } + else if (result == ACL_TBL_FULL) + { + printf("table full\n"); + } + else + { + printf("fail\n"); + } + + free(args2); + close(fd); + return 0; +} diff --git a/package/ramips/applications/hwnat/src/acl_api.c b/package/ramips/applications/hwnat/src/acl_api.c new file mode 100755 index 0000000..13d97b0 --- /dev/null +++ b/package/ramips/applications/hwnat/src/acl_api.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include +#include "acl_ioctl.h" + +int SetAclEntry(struct acl_args *opt, unsigned int cmd) +{ + int fd; + + fd = open("/dev/"ACL_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"ACL_DEVNAME); + return ACL_FAIL; + } + + if(ioctl(fd, cmd, opt)<0) + { + printf("ACL_API: ioctl error\n"); + close(fd); + return ACL_FAIL; + } + + close(fd); + return ACL_SUCCESS; +} +int AclGetAllEntries(struct acl_list_args *opt) +{ + int fd=0; + + fd = open("/dev/"ACL_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"ACL_DEVNAME); + return ACL_FAIL; + } + + if(ioctl(fd, ACL_GET_ALL_ENTRIES, opt)<0) + { + printf("ACL_API: ioctl error\n"); + close(fd); + return ACL_FAIL; + } + + close(fd); + + return ACL_SUCCESS; + +} diff --git a/package/ramips/applications/hwnat/src/acl_api.h b/package/ramips/applications/hwnat/src/acl_api.h new file mode 100755 index 0000000..9b6f574 --- /dev/null +++ b/package/ramips/applications/hwnat/src/acl_api.h @@ -0,0 +1,16 @@ +#ifndef __ACL_API +#define __ACL_API + +#define NIPQUAD(addr) \ + ((unsigned char *)&addr)[3], \ + ((unsigned char *)&addr)[2], \ + ((unsigned char *)&addr)[1], \ + ((unsigned char *)&addr)[0] +#define NIPHALF(addr) \ + ((unsigned short *)&addr)[0], \ + ((unsigned short *)&addr)[1] + +int SetAclEntry(struct acl_args *opt, unsigned int cmd); +int AclGetAllEntries(struct acl_list_args *opt); + +#endif diff --git a/package/ramips/applications/hwnat/src/hw_nat.c b/package/ramips/applications/hwnat/src/hw_nat.c new file mode 100755 index 0000000..35c4be6 --- /dev/null +++ b/package/ramips/applications/hwnat/src/hw_nat.c @@ -0,0 +1,631 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "hwnat_ioctl.h" +#include "hwnat_api.h" + +void show_usage(void) +{ + printf("Show Foe Entry\n"); + printf("hw_nat -c [entry_num]\n"); + printf("Ex: hw_nat -c 1234\n\n"); + + printf("Set Debug Level (0:disable) \n"); + printf("hw_nat -d [0~7]\n"); + printf("Ex: hw_nat -d \n\n"); + + printf("Show All Foe Invalid Entry\n"); + printf("Ex: hw_nat -e\n\n"); + + printf("Show All Foe Unbinded Entry\n"); + printf("Ex: hw_nat -f\n\n"); + + printf("Show All Foe Binded Entry\n"); + printf("Ex: hw_nat -g\n\n"); + printf("Unbind Foe Entry\n"); + printf("hw_nat -x [entry_num]\n"); + printf("Ex: hw_nat -x 1234\n\n"); + +#if !defined (CONFIG_HNAT_V2) + printf("Enable DSCP Remark\n"); + printf("Ex: hw_nat -A [0/1]\n\n"); + + printf("Enable VLAN Priority Remark\n"); + printf("Ex: hw_nat -B [0/1]\n\n"); + + printf("Set weight of FOE priority decision in resolution\n"); + printf("Ex: hw_nat -C [0~7]\n\n"); + + printf("Set weight of ACL priority decision in resolution\n"); + printf("Ex: hw_nat -D [0~7]\n\n"); + + printf("Set weight of DSCP priority decision in resolution\n"); + printf("Ex: hw_nat -E [0~7]\n\n"); + + printf("Set weight of VLAN priority decision in resolution\n"); + printf("Ex: hw_nat -F [0~7]\n\n"); + + printf("Set mapping of DSCP set to UP\n"); + printf("Ex: hw_nat -G [DSCP_SET:0~7][UP:0~7]\n\n"); + + printf("Set mapping of UP to Inprofile DSCP\n"); + printf("Ex: hw_nat -H [UP:0~7][DSCP:0~63]\n\n"); + + printf("Set mapping of UP to Outprofile DSCP\n"); + printf("Ex: hw_nat -I [UP:0~7][DSCP:0~63]\n\n"); + + printf("Set mapping of UP to VLAN Priority\n"); + printf("Ex: hw_nat -J [UP:0~7][VPRI:0~7]\n\n"); + + printf("Set mapping of UP to Access Category\n"); + printf("Ex: hw_nat -K [UP:0~7][AC:0~3]\n\n"); + + printf("Set HNAT PreACL/PreMeter/PreAC/PostMeter/PostAC table size (d=383, 32, 32, 32, 32)\n"); + printf("Ex: hw_nat -P [0~511][0~64][0~64][0~64][0~64]\n"); + printf("NOTE: Total 511 rules, PreAC+PostAC<=64, PreMeter+PostMeter<=64\n\n"); + + printf("Set HNAT QOS Mode\n"); +#if defined (CONFIG_RALINK_RT3352) || defined (CONFIG_RALINK_RT6855) + printf("Ex: hw_nat -L [0:WRR, 1:SPQ, 2:Q3>WRR(Q2,Q1,Q0), 3:Q3>Q2>WRR(Q1,Q0)]\n\n"); +#else + printf("Ex: hw_nat -L [0:WRR, 1:SPQ, 2:Q3>WRR(Q2,Q1,Q0)]\n\n"); +#endif + + printf("Set the weight of GDMA Scheduler\n"); + printf("hw_nat -M Q3(1/2/4/8) Q2(1/2/4/8) Q1(1/2/4/8) Q0(1/2/4/8)\n\n"); + printf("hw_nat -M 8 4 2 1\n\n"); +#else + printf("Get ByteCNT and PktCnt of AG_IDX\n"); + printf("Ex: hw_nat -A [AG index]\n\n"); + +#if defined (CONFIG_PPE_MCAST) + printf("Add member port in multicast entry\n"); + printf("Ex: hw_nat -B [vid] [mac] [px_en] [px_qos_en] [mc_qos_qid]\n\n"); + + printf("Del member port multicast entry\n"); + printf("Ex: hw_nat -C [vid] [mac] [px_en] [px_qos_en] [mc_qos_qid]\n\n"); + + printf("Dump all multicast entry\n"); + printf("Ex: hw_nat -D\n\n"); +#endif +#endif + + printf("Set PPE Cofigurations:\n"); + printf("Set HNAT binding threshold per second (d=30)\n"); + printf("Ex: hw_nat -N [1~65535]\n\n"); + + printf("Set HNAT Max entries allowed build when Free Entries>3/4, >1/2, <1/2 (d=100, 50, 25)\n"); + printf("Ex: hw_nat -O [1~16383][1~16383][1~16383]\n\n"); + + + printf("Set HNAT TCP/UDP keepalive interval (d=1, 1)(unit:4sec)\n"); + printf("Ex: hw_nat -Q [1~255][1~255]\n\n"); + + printf("Set HNAT Life time of unbind entry (d=3)(unit:1Sec)\n"); + printf("Ex: hw_nat -T [1~255]\n\n"); + + printf("Set HNAT Life time of Binded TCP/UDP/FIN entry(d=5, 5, 5)(unit:1Sec) \n"); + printf("Ex: hw_nat -U [1~65535][1~65535][1~65535]\n\n"); + + printf("Set LAN/WAN port VLAN ID\n"); + printf("Ex: hw_nat -V [LAN_VID] [WAN_VID]\n\n"); + printf("Ex: hw_nat -V 1 2\n\n"); + + printf("Only Speed UP (0=Upstream, 1=Downstream, 2=Bi-Direction) flow \n"); + printf("Ex: hw_nat -Z 1\n\n"); + +} + +int main(int argc, char *argv[]) +{ + int opt; +#if !defined (CONFIG_HNAT_V2) + char options[] = "efg?c:d:A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:T:U:V:Z:"; +#else + char options[] = "aefg?c:x:d:A:B:C:DN:O:P:Q:T:U:V:Z:"; +#endif + int fd, method = -1; + int i=0; + unsigned int entry_num; + unsigned int debug; + unsigned int dir; + struct hwnat_args *args; + struct hwnat_tuple args2; +#if !defined (CONFIG_HNAT_V2) + struct hwnat_qos_args args3; +#else + struct hwnat_ac_args args3; +#endif + struct hwnat_config_args args4; + +#if defined (CONFIG_PPE_MCAST) + struct hwnat_mcast_args args5; + unsigned char mac[6]; +#endif + + int result; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return 0; + } + + if(argc < 2) + { + show_usage(); + close(fd); + return 0; + } + + /* Max table size is 16K */ + args=malloc(sizeof(struct hwnat_args)+sizeof(struct hwnat_tuple)*1024*16); + if (NULL == args) + { + printf(" Allocate memory for hwnat_args and hwnat_tuple failed.\n"); + close(fd); + return 0; + } + + while ((opt = getopt (argc, argv, options)) != -1) + { + switch (opt) + { +#if defined (CONFIG_HNAT_V2) + case 'a': + method = HW_NAT_DUMP_CACHE_ENTRY; + break; +#endif + case 'c': + method = HW_NAT_DUMP_ENTRY; + entry_num = strtoll(optarg, NULL, 10); + break; + case 'x': + method = HW_NAT_UNBIND_ENTRY; + entry_num = strtoll(optarg, NULL, 10); + break; + case 'd': + method = HW_NAT_DEBUG; + debug = strtoll(optarg, NULL, 10); + break; + case 'e': + method = HW_NAT_GET_ALL_ENTRIES; + args->entry_state=0; /* invalid entry */ + break; + case 'f': + method = HW_NAT_GET_ALL_ENTRIES; + args->entry_state=1; /* unbinded entry */ + break; + case 'g': + method = HW_NAT_GET_ALL_ENTRIES; + args->entry_state=2; /* binded entry */ + break; +#if !defined (CONFIG_HNAT_V2) + case 'A': + method = HW_NAT_DSCP_REMARK; + args3.enable = strtoll(optarg, NULL, 10); + break; + case 'B': + method = HW_NAT_VPRI_REMARK; + args3.enable = strtoll(optarg, NULL, 10); + break; + case 'C': + method = HW_NAT_FOE_WEIGHT; + args3.weight = strtoll(optarg, NULL, 10); + break; + case 'D': + method = HW_NAT_ACL_WEIGHT; + args3.weight = strtoll(optarg, NULL, 10); + break; + case 'E': + method = HW_NAT_DSCP_WEIGHT; + args3.weight = strtoll(optarg, NULL, 10); + break; + case 'F': + method = HW_NAT_VPRI_WEIGHT; + args3.weight = strtoll(optarg, NULL, 10); + break; + case 'G': + method = HW_NAT_DSCP_UP; + args3.dscp_set = strtoll(argv[2], NULL, 10); + args3.up = strtoll(argv[3], NULL, 10); + break; + case 'H': + method = HW_NAT_UP_IDSCP; + + args3.up = strtoll(argv[2], NULL, 10); + args3.dscp = strtoll(argv[3], NULL, 10); + break; + case 'I': + method = HW_NAT_UP_ODSCP; + args3.up = strtoll(argv[2], NULL, 10); + args3.dscp = strtoll(argv[3], NULL, 10); + break; + case 'J': + method = HW_NAT_UP_VPRI; + args3.up = strtoll(argv[2], NULL, 10); + args3.vpri = strtoll(argv[3], NULL, 10); + break; + case 'K': + method = HW_NAT_UP_AC; + args3.up = strtoll(argv[2], NULL, 10); + args3.ac = strtoll(argv[3], NULL, 10); + break; + case 'L': + method = HW_NAT_SCH_MODE; + args3.mode = strtoll(argv[2], NULL, 10); + break; + case 'M': + method = HW_NAT_SCH_WEIGHT; + args3.weight3 = strtoll(argv[2], NULL, 10); + args3.weight2 = strtoll(argv[3], NULL, 10); + args3.weight1 = strtoll(argv[4], NULL, 10); + args3.weight0 = strtoll(argv[5], NULL, 10); + break; +#else + case 'A': + method = HW_NAT_GET_AC_CNT; + args3.ag_index = strtoll(optarg, NULL, 10); + break; +#if defined (CONFIG_PPE_MCAST) + case 'B': + method = HW_NAT_MCAST_INS; + args5.mc_vid = strtoll(argv[2], NULL, 10); + str_to_mac(mac, argv[3]); + memcpy(args5.dst_mac, mac, sizeof(mac)); + args5.mc_px_en = strtoll(argv[4], NULL, 10); + args5.mc_px_qos_en = strtoll(argv[5], NULL, 10); + args5.mc_qos_qid = strtoll(argv[6], NULL, 10); + break; + case 'C': + method = HW_NAT_MCAST_DEL; + args5.mc_vid = strtoll(argv[2], NULL, 10); + str_to_mac(mac, argv[3]); + memcpy(args5.dst_mac, mac, sizeof(mac)); + memcpy(args5.dst_mac, mac, sizeof(mac)); + args5.mc_px_en = strtoll(argv[4], NULL, 10); + args5.mc_px_qos_en = strtoll(argv[5], NULL, 10); + args5.mc_qos_qid = strtoll(argv[6], NULL, 10); + break; + case 'D': + method = HW_NAT_MCAST_DUMP; + break; +#endif + +#endif + case 'N': + method = HW_NAT_BIND_THRESHOLD; + args4.bind_threshold = strtoll(argv[2], NULL, 10); + break; + case 'O': + method = HW_NAT_MAX_ENTRY_LMT; + args4.foe_qut_lmt = strtoll(argv[2], NULL, 10); + args4.foe_half_lmt = strtoll(argv[3], NULL, 10); + args4.foe_full_lmt = strtoll(argv[4], NULL, 10); + break; + case 'P': + method = HW_NAT_RULE_SIZE; + args4.pre_acl = strtoll(argv[2], NULL, 10); + args4.pre_meter = strtoll(argv[3], NULL, 10); + args4.pre_ac = strtoll(argv[4], NULL, 10); + args4.post_meter = strtoll(argv[5], NULL, 10); + args4.post_ac = strtoll(argv[6], NULL, 10); + break; + case 'Q': + method = HW_NAT_KA_INTERVAL; + args4.foe_tcp_ka = strtoll(argv[2], NULL, 10); + args4.foe_udp_ka = strtoll(argv[3], NULL, 10); + break; + case 'T': + method = HW_NAT_UB_LIFETIME; + args4.foe_unb_dlta = strtoll(argv[2], NULL, 10); + break; + case 'U': + method = HW_NAT_BIND_LIFETIME; + args4.foe_tcp_dlta = strtoll(argv[2], NULL, 10); + args4.foe_udp_dlta = strtoll(argv[3], NULL, 10); + args4.foe_fin_dlta = strtoll(argv[4], NULL, 10); + break; + case 'V': + method = HW_NAT_VLAN_ID; + args4.lan_vid = strtoll(argv[2], NULL, 10); + args4.wan_vid = strtoll(argv[3], NULL, 10); + break; + case 'Z': + method = HW_NAT_BIND_DIRECTION; + args4.bind_dir = strtoll(optarg, NULL, 10); + break; + case '?': + show_usage(); + + } + } + + + switch(method) + { + case HW_NAT_GET_ALL_ENTRIES: + HwNatGetAllEntries(args); + + printf("Total Entry Count = %d\n",args->num_of_entries); + for(i=0; inum_of_entries; i++) + { + if(args->entries[i].pkt_type==0) //IPV4_NAPT + { + printf("IPv4_NAPT=%d : %u.%u.%u.%u:%d->%u.%u.%u.%u:%d => %u.%u.%u.%u:%d->%u.%u.%u.%u:%d\n", \ + args->entries[i].hash_index, \ + NIPQUAD(args->entries[i].ing_sipv4), \ + args->entries[i].ing_sp, \ + NIPQUAD(args->entries[i].ing_dipv4), \ + args->entries[i].ing_dp, \ + NIPQUAD(args->entries[i].eg_sipv4), \ + args->entries[i].eg_sp, \ + NIPQUAD(args->entries[i].eg_dipv4), \ + args->entries[i].eg_dp); + } + else if(args->entries[i].pkt_type==1) //IPV4_NAT + { + printf("IPv4_NAT=%d : %u.%u.%u.%u->%u.%u.%u.%u => %u.%u.%u.%u->%u.%u.%u.%u\n", \ + args->entries[i].hash_index, \ + NIPQUAD(args->entries[i].ing_sipv4), \ + NIPQUAD(args->entries[i].ing_dipv4), \ + NIPQUAD(args->entries[i].eg_sipv4), \ + NIPQUAD(args->entries[i].eg_dipv4)); + } + else if(args->entries[i].pkt_type==2) //IPV6_ROUTING + { + printf("IPv6_1T= %d /DIP: %x:%x:%x:%x:%x:%x:%x:%x\n", \ + args->entries[i].hash_index, \ + NIPHALF(args->entries[i].ing_dipv6_0), \ + NIPHALF(args->entries[i].ing_dipv6_1), \ + NIPHALF(args->entries[i].ing_dipv6_2), \ + NIPHALF(args->entries[i].ing_dipv6_3)); + } + else if(args->entries[i].pkt_type==3) //IPV4_DSLITE + { + printf("DS-Lite= %d : %u.%u.%u.%u:%d->%u.%u.%u.%u:%d (%x:%x:%x:%x:%x:%x:%x:%x -> %x:%x:%x:%x:%x:%x:%x:%x) \n", \ + args->entries[i].hash_index, \ + NIPQUAD(args->entries[i].ing_sipv4), \ + args->entries[i].ing_sp, \ + NIPQUAD(args->entries[i].ing_dipv4), \ + args->entries[i].ing_dp, \ + NIPHALF(args->entries[i].eg_sipv6_0), \ + NIPHALF(args->entries[i].eg_sipv6_1), \ + NIPHALF(args->entries[i].eg_sipv6_2), \ + NIPHALF(args->entries[i].eg_sipv6_3), \ + NIPHALF(args->entries[i].eg_dipv6_0), \ + NIPHALF(args->entries[i].eg_dipv6_1), \ + NIPHALF(args->entries[i].eg_dipv6_2), \ + NIPHALF(args->entries[i].eg_dipv6_3)); + } + else if(args->entries[i].pkt_type==4) //IPV6_3T_ROUTE + { + printf("IPv6_3T= %d SIP: %x:%x:%x:%x:%x:%x:%x:%x DIP: %x:%x:%x:%x:%x:%x:%x:%x\n", \ + args->entries[i].hash_index, \ + NIPHALF(args->entries[i].ing_sipv6_0), \ + NIPHALF(args->entries[i].ing_sipv6_1), \ + NIPHALF(args->entries[i].ing_sipv6_2), \ + NIPHALF(args->entries[i].ing_sipv6_3), \ + NIPHALF(args->entries[i].ing_dipv6_0), \ + NIPHALF(args->entries[i].ing_dipv6_1), \ + NIPHALF(args->entries[i].ing_dipv6_2), \ + NIPHALF(args->entries[i].ing_dipv6_3)); + } + else if(args->entries[i].pkt_type==5) //IPV6_5T_ROUTE + { + if(args->entries[i].ipv6_flowlabel==1) + { + printf("IPv6_5T= %d SIP: %x:%x:%x:%x:%x:%x:%x:%x DIP: %x:%x:%x:%x:%x:%x:%x:%x (Flow Label=%x)\n", \ + args->entries[i].hash_index, \ + NIPHALF(args->entries[i].ing_sipv6_0), \ + NIPHALF(args->entries[i].ing_sipv6_1), \ + NIPHALF(args->entries[i].ing_sipv6_2), \ + NIPHALF(args->entries[i].ing_sipv6_3), \ + NIPHALF(args->entries[i].ing_dipv6_0), \ + NIPHALF(args->entries[i].ing_dipv6_1), \ + NIPHALF(args->entries[i].ing_dipv6_2), \ + NIPHALF(args->entries[i].ing_dipv6_3), \ + ((args->entries[i].ing_sp << 16) | (args->entries[i].ing_dp))&0xFFFFF); + } + else + { + printf("IPv6_5T= %d SIP: %x:%x:%x:%x:%x:%x:%x:%x (SP:%d) DIP: %x:%x:%x:%x:%x:%x:%x:%x (DP=%d)\n", \ + args->entries[i].hash_index, \ + NIPHALF(args->entries[i].ing_sipv6_0), \ + NIPHALF(args->entries[i].ing_sipv6_1), \ + NIPHALF(args->entries[i].ing_sipv6_2), \ + NIPHALF(args->entries[i].ing_sipv6_3), \ + args->entries[i].ing_sp, \ + NIPHALF(args->entries[i].ing_dipv6_0), \ + NIPHALF(args->entries[i].ing_dipv6_1), \ + NIPHALF(args->entries[i].ing_dipv6_2), \ + NIPHALF(args->entries[i].ing_dipv6_3), \ + args->entries[i].ing_dp); + } + + } + else if(args->entries[i].pkt_type==7) //IPV6_6RD + { + if(args->entries[i].ipv6_flowlabel==1) + { + printf("6RD= %d %x:%x:%x:%x:%x:%x:%x:%x->%x:%x:%x:%x:%x:%x:%x:%x [Flow Label=%x]\n", \ + args->entries[i].hash_index, \ + NIPHALF(args->entries[i].ing_sipv6_0), \ + NIPHALF(args->entries[i].ing_sipv6_1), \ + NIPHALF(args->entries[i].ing_sipv6_2), \ + NIPHALF(args->entries[i].ing_sipv6_3), \ + NIPHALF(args->entries[i].ing_dipv6_0), \ + NIPHALF(args->entries[i].ing_dipv6_1), \ + NIPHALF(args->entries[i].ing_dipv6_2), \ + NIPHALF(args->entries[i].ing_dipv6_3), \ + ((args->entries[i].ing_sp << 16) | (args->entries[i].ing_dp))&0xFFFFF); + printf("(%u.%u.%u.%u->%u.%u.%u.%u)\n", NIPQUAD(args->entries[i].eg_sipv4), NIPQUAD(args->entries[i].eg_dipv4)); + } + else + { + printf("6RD= %d /SIP: %x:%x:%x:%x:%x:%x:%x:%x [SP:%d] /DIP: %x:%x:%x:%x:%x:%x:%x:%x [DP=%d]", \ + args->entries[i].hash_index, \ + NIPHALF(args->entries[i].ing_sipv6_0), \ + NIPHALF(args->entries[i].ing_sipv6_1), \ + NIPHALF(args->entries[i].ing_sipv6_2), \ + NIPHALF(args->entries[i].ing_sipv6_3), \ + args->entries[i].ing_sp, \ + NIPHALF(args->entries[i].ing_dipv6_0), \ + NIPHALF(args->entries[i].ing_dipv6_1), \ + NIPHALF(args->entries[i].ing_dipv6_2), \ + NIPHALF(args->entries[i].ing_dipv6_3), \ + args->entries[i].ing_dp); + printf("(%u.%u.%u.%u->%u.%u.%u.%u)\n", NIPQUAD(args->entries[i].eg_sipv4), NIPQUAD(args->entries[i].eg_dipv4)); + } + } + else + { + printf("unknown packet type! (pkt_type=%d) \n", args->entries[i].pkt_type); + } + } + result = args->result; + break; +#if defined (CONFIG_HNAT_V2) + case HW_NAT_DUMP_CACHE_ENTRY: + result = HwNatCacheDumpEntry(); + break; +#endif + case HW_NAT_DUMP_ENTRY: + result = HwNatDumpEntry(entry_num); + break; + case HW_NAT_UNBIND_ENTRY: + result = HwNatUnBindEntry(entry_num); + break; + case HW_NAT_DEBUG: + result = HwNatDebug(debug); + break; +#if !defined (CONFIG_HNAT_V2) + case HW_NAT_DSCP_REMARK: + HwNatDscpRemarkEbl(&args3); + result = args3.result; + break; + case HW_NAT_VPRI_REMARK: + HwNatVpriRemarkEbl(&args3); + result = args3.result; + break; + case HW_NAT_FOE_WEIGHT: + HwNatSetFoeWeight(&args3); + result = args3.result; + break; + case HW_NAT_ACL_WEIGHT: + HwNatSetAclWeight(&args3); + result = args3.result; + break; + case HW_NAT_DSCP_WEIGHT: + HwNatSetDscpWeight(&args3); + result = args3.result; + break; + case HW_NAT_VPRI_WEIGHT: + HwNatSetVpriWeight(&args3); + result = args3.result; + break; + case HW_NAT_DSCP_UP: + HwNatSetDscp_Up(&args3); + result = args3.result; + break; + case HW_NAT_UP_IDSCP: + HwNatSetUp_InDscp(&args3); + result = args3.result; + break; + case HW_NAT_UP_ODSCP: + HwNatSetUp_OutDscp(&args3); + result = args3.result; + break; + case HW_NAT_UP_VPRI: + HwNatSetUp_Vpri(&args3); + result = args3.result; + break; + case HW_NAT_UP_AC: + HwNatSetUp_Ac(&args3); + result = args3.result; + break; + case HW_NAT_SCH_MODE: + HwNatSetSchMode(&args3); + result = args3.result; + break; + case HW_NAT_SCH_WEIGHT: + HwNatSetSchWeight(&args3); + result = args3.result; + break; +#else + case HW_NAT_GET_AC_CNT: + HwNatGetAGCnt(&args3); + printf("Byte cnt=%llu\n", args3.ag_byte_cnt); + printf("Pkt cnt=%llu\n", args3.ag_pkt_cnt); + result = args3.result; + break; +#endif + case HW_NAT_BIND_THRESHOLD: + HwNatSetBindThreshold(&args4); + result = args4.result; + break; + case HW_NAT_MAX_ENTRY_LMT: + HwNatSetMaxEntryRateLimit(&args4); + result = args4.result; + break; + case HW_NAT_RULE_SIZE: + HwNatSetRuleSize(&args4); + result = args4.result; + break; + case HW_NAT_KA_INTERVAL: + HwNatSetKaInterval(&args4); + result = args4.result; + break; + case HW_NAT_UB_LIFETIME: + HwNatSetUnbindLifeTime(&args4); + result = args4.result; + break; + case HW_NAT_BIND_LIFETIME: + HwNatSetBindLifeTime(&args4); + result = args4.result; + break; + case HW_NAT_VLAN_ID: + result = HwNatSetVID(&args4); + break; + case HW_NAT_BIND_DIRECTION: + result = HwNatSetBindDir(&args4); + break; +#if defined (CONFIG_PPE_MCAST) + case HW_NAT_MCAST_INS: + result = HwNatMcastIns(&args5); + break; + case HW_NAT_MCAST_DEL: + result = HwNatMcastDel(&args5); + break; + case HW_NAT_MCAST_DUMP: + result = HwNatMcastDump(); + break; +#endif + default: + result = HWNAT_FAIL; + + } + + if(result==HWNAT_SUCCESS) + { + printf("done\n"); + } + else if(result==HWNAT_ENTRY_NOT_FOUND) + { + printf("entry not found\n"); + } + else + { + printf("fail\n"); + } + + free(args); + close(fd); + return 0; +} diff --git a/package/ramips/applications/hwnat/src/hwnat_api.c b/package/ramips/applications/hwnat/src/hwnat_api.c new file mode 100755 index 0000000..f66509f --- /dev/null +++ b/package/ramips/applications/hwnat/src/hwnat_api.c @@ -0,0 +1,763 @@ +#include +#include +#include +#include +#include +#include +#include +#include "hwnat_ioctl.h" + + + +int HwNatDumpEntry(unsigned int entry_num) +{ + struct hwnat_args opt; + int fd; + + opt.entry_num=entry_num; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_DUMP_ENTRY, &opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; +} + +int HwNatBindEntry(unsigned int entry_num) +{ + struct hwnat_args opt; + int fd; + + opt.entry_num=entry_num; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_BIND_ENTRY, &opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; +} + +int HwNatUnBindEntry(unsigned int entry_num) +{ + struct hwnat_args opt; + int fd; + + opt.entry_num=entry_num; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_UNBIND_ENTRY, &opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; +} + +int HwNatInvalidEntry(unsigned int entry_num) +{ + struct hwnat_args opt; + int fd; + + opt.entry_num=entry_num; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_INVALID_ENTRY, &opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; +} + +#if !defined (CONFIG_HNAT_V2) +/*hnat qos*/ +int HwNatDscpRemarkEbl(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_DSCP_REMARK, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatVpriRemarkEbl(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_VPRI_REMARK, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetFoeWeight(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_FOE_WEIGHT, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetAclWeight(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_ACL_WEIGHT, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetDscpWeight(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_DSCP_WEIGHT, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetVpriWeight(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_VPRI_WEIGHT, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetDscp_Up(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_DSCP_UP, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetUp_InDscp(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_UP_IDSCP, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetUp_OutDscp(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_UP_ODSCP, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetUp_Vpri(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_UP_VPRI, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} +int HwNatSetUp_Ac(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_UP_AC, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetSchMode(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_SCH_MODE, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetSchWeight(struct hwnat_qos_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_SCH_WEIGHT, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} +#else + +int HwNatCacheDumpEntry(void) +{ + struct hwnat_args opt; + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_DUMP_CACHE_ENTRY, &opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; +} + +int HwNatGetAGCnt(struct hwnat_ac_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_GET_AC_CNT, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} +#endif + +int HwNatSetBindThreshold(struct hwnat_config_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_BIND_THRESHOLD, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetMaxEntryRateLimit(struct hwnat_config_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_MAX_ENTRY_LMT, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + + +int HwNatSetRuleSize(struct hwnat_config_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_RULE_SIZE, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetKaInterval(struct hwnat_config_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_KA_INTERVAL, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + + + + +int HwNatSetUnbindLifeTime(struct hwnat_config_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_UB_LIFETIME, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetBindLifeTime(struct hwnat_config_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_BIND_LIFETIME, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; + +} + +int HwNatSetVID(struct hwnat_config_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_VLAN_ID, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; +} + +int HwNatSetBindDir(struct hwnat_config_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_BIND_DIRECTION, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; +} + +int HwNatGetAllEntries(struct hwnat_args *opt) +{ + int fd=0; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_GET_ALL_ENTRIES, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + + + return HWNAT_SUCCESS; + +} + +int HwNatDebug(unsigned int debug) +{ + struct hwnat_args opt; + int fd; + + opt.debug=debug; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_DEBUG, &opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; +} + +#ifdef CONFIG_PPE_MCAST +int HwNatMcastIns(struct hwnat_mcast_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_MCAST_INS, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; +} + +int HwNatMcastDel(struct hwnat_mcast_args *opt) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_MCAST_DEL, opt)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; +} + +int HwNatMcastDump(void) +{ + int fd; + + fd = open("/dev/"HW_NAT_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"HW_NAT_DEVNAME); + return HWNAT_FAIL; + } + + if(ioctl(fd, HW_NAT_MCAST_DUMP, NULL)<0) + { + printf("HW_NAT_API: ioctl error\n"); + close(fd); + return HWNAT_FAIL; + } + + close(fd); + return HWNAT_SUCCESS; +} + +#endif diff --git a/package/ramips/applications/hwnat/src/hwnat_api.h b/package/ramips/applications/hwnat/src/hwnat_api.h new file mode 100755 index 0000000..542f205 --- /dev/null +++ b/package/ramips/applications/hwnat/src/hwnat_api.h @@ -0,0 +1,44 @@ +#ifndef __HW_NAT_API +#define __HW_NAT_API + +#define NIPQUAD(addr) \ + ((unsigned char *)&addr)[3], \ + ((unsigned char *)&addr)[2], \ + ((unsigned char *)&addr)[1], \ + ((unsigned char *)&addr)[0] +#define NIPHALF(addr) \ + ((unsigned short *)&addr)[1], \ + ((unsigned short *)&addr)[0] + +int HwNatDumpEntry(unsigned int entry_num); +int HwNatBindEntry(unsigned int entry_num); +int HwNatUnBindEntry(unsigned int entry_num); +int HwNatInvalidEntry(unsigned int entry_num); +int HwNatDscpRemarkEbl(struct hwnat_qos_args *opt); +int HwNatVpriRemarkEbl(struct hwnat_qos_args *opt); +int HwNatSetFoeWeight(struct hwnat_qos_args *opt); +int HwNatSetAclWeight(struct hwnat_qos_args *opt); +int HwNatSetDscpWeight(struct hwnat_qos_args *opt); +int HwNatSetVpriWeight(struct hwnat_qos_args *opt); +int HwNatSetDscp_Up(struct hwnat_qos_args *opt); +int HwNatSetUp_InDscp(struct hwnat_qos_args *opt); +int HwNatSetUp_OutDscp(struct hwnat_qos_args *opt); +int HwNatSetUp_Vpri(struct hwnat_qos_args *opt); +int HwNatSetUp_Ac(struct hwnat_qos_args *opt); +int HwNatSetSchMode(struct hwnat_qos_args *opt); +int HwNatSetSchWeight(struct hwnat_qos_args *opt); +int HwNatSetBindThreshold(struct hwnat_config_args *opt); +int HwNatSetMaxEntryRateLimit(struct hwnat_config_args *opt); +int HwNatSetRuleSize(struct hwnat_config_args *opt); +int HwNatSetKaInterval(struct hwnat_config_args *opt); +int HwNatSetUnbindLifeTime(struct hwnat_config_args *opt); +int HwNatSetBindLifeTime(struct hwnat_config_args *opt); +int HwNatSetVID(struct hwnat_config_args *opt); +int HwNatSetBindDir(struct hwnat_config_args *opt); +int HwNatGetAllEntries(struct hwnat_args *opt); +int HwNatDebug(unsigned int debug); +int HwNatGetAGCnt(struct hwnat_ac_args *opt); +int HwNatMcastIns(struct hwnat_mcast_args *opt); +int HwNatMcastDel(struct hwnat_mcast_args *opt); +int HwNatMcastDump(void); +#endif diff --git a/package/ramips/applications/hwnat/src/mtr.c b/package/ramips/applications/hwnat/src/mtr.c new file mode 100755 index 0000000..e9c0c89 --- /dev/null +++ b/package/ramips/applications/hwnat/src/mtr.c @@ -0,0 +1,322 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "linux/autoconf.h" +#include "mtr_ioctl.h" +#include "mtr_api.h" + + +void show_usage(void) +{ + + printf("Add Mac Upload Meter Rule\n"); + printf("mtr -a -m [Mac] -t [KB/s] -s [BucketSize:4K/8K/16K/32K or 0~127]\n"); + printf(" -u [MtrIntval:1ms/10ms/50ms/100ms/500ms/1000ms/5000ms/10000ms] -v [Base:Byte/Pkt]\n"); + printf("ByteBase:mtr -a -m 00:11:22:33:44:55 -t 10 -s 8K -v Byte\n\n"); + printf("PktBase:mtr -a -m 00:11:22:33:44:55 -s 100 -u 1ms -v Pkt\n\n"); + + printf("Add Mac Download Meter Rule\n"); + printf("mtr -b -m [Mac] -t [KB/s] -s [BucketSize:4K/8K/16K/32K or 0~127]\n"); + printf(" -u [MtrIntval:1ms/10ms/50ms/100ms/500ms/1000ms/5000ms/10000ms] -v [Base:Byte/Pkt]\n"); + printf("ByteBase: mtr -b -m 00:11:22:33:44:55 -t 10 -s 8K -v Byte\n\n"); + printf("PktBase: mtr -b -m 00:11:22:33:44:55 -s 100 -u 1ms -v Pkt\n\n"); + printf("Del Mac Upload Meter Rule\n"); + printf("mtr -c -m [Mac]\n"); + printf("Ex: mtr -c -m 00:11:22:33:44:55\n\n"); + + printf("Del Mac download Meter Rule\n"); + printf("mtr -d -m [Mac]\n"); + printf("Ex: mtr -d -m 00:11:22:33:44:55\n\n"); + + + printf("Add IP Upload Meter Rule\n"); + printf("mtr -e -i [IpS] -j [IpE] -t [KB/s] -s [BucketSize:4K/8K/16K/32K or 0~127]\n"); + printf(" -u [MtrIntval:1ms/10ms/50ms/100ms/500ms/1000ms/5000ms/10000ms] -v [Base:Byte/Pkt]\n"); + printf("ByteBase: mtr -e -i 10.10.10.3 -j 10.10.10.3 -t 10 -s 8K -v Byte\n\n"); + printf("PktBase: mtr -e -i 10.10.10.3 -j 10.10.10.3 -s 100 -u 1ms -v Pkt\n\n"); + + printf("Add IP Download Meter Rule\n"); + printf("mtr -f -i [IpS] -j [IpE] -t [KB/s] -s [BucketSize:4K/8K/16K/32K or 0~127] \n"); + printf(" -u [MtrIntval:1ms/10ms/50ms/100ms/500ms/1000ms/5000ms/10000ms] -v [Base:Byte/Pkt]\n"); + printf("ByteBase: mtr -f -i 10.10.10.3 -t 10 -s 8K -v Byte\n\n"); + printf("PktBase: mtr -f -i 10.10.10.3 -j 10.10.10.3 -s 100 -u 50ms -v Pkt\n\n"); + + printf("Del IP Upload Meter Rule\n"); + printf("mtr -g -i [IpS] -j [IpE]\n"); + printf("mtr -g -i 10.10.10.3 -j 10.10.10.3\n\n"); + + printf("Del IP Download Meter Rule\n"); + printf("mtr -h -i [IpS] -j [IpE]\n"); + printf("mtr -h -i 10.10.10.3 -j 10.10.10.3\n\n"); + + printf("Clear Meter Table\n"); + printf("mtr -z\n\n"); + + printf("Add SYN Meter Rule: mtr -k\n"); + printf("Add FIN Meter Rule: mtr -l\n"); + printf("Add UDP Meter Rule: mtr -n\n"); + printf("Add ICMP Meter Rule: -o\n"); + printf(" -t [KB/s] -s [BucketSize:4K/8K/16K/32K or 0~127]\n"); + printf(" -u [MtrIntval:1ms/10ms/50ms/100ms/500ms/1000ms/5000ms/10000ms] -v [Base:Byte/Pkt]\n\n"); + printf("Del SYN Meter Rule:mtr -p\n"); + printf("Del FIN Meter Rule:mtr -q\n"); + printf("Del UDP Meter Rule:mtr -r\n"); + printf("Del ICMP Meter Rule:mtr -y\n\n"); + + printf("Get All Mtr Entries: mtr -w\n"); +} + +int main(int argc, char *argv[]) +{ + int opt; + char options[] = "abcdefghklnopqryz?m:i:j:t:s:u:v:w"; + + int fd; + int method=-1; + struct mtr_args args; + struct mtr_list_args *args2; + int result; + int i; + + args2=malloc(sizeof(struct mtr_list_args) + sizeof(struct mtr_args)*511); + if (NULL == args2) + { + printf(" allocate memory for mtr_list_args and mtr_args failed.\n"); + return 0; + } + fd = open("/dev/"MTR_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"MTR_DEVNAME); + free(args2); + return 0; + } + + if(argc < 2) + { + show_usage(); + free(args2); + close(fd); + return 0; + } + + + while ((opt = getopt (argc, argv, options)) != -1) + { + switch (opt) + { + case 'a': + method=MTR_ADD_MAC_UL_ENTRY; + break; + case 'b': + method=MTR_ADD_MAC_DL_ENTRY; + break; + case 'c': + method=MTR_DEL_MAC_UL_ENTRY; + break; + case 'd': + method=MTR_DEL_MAC_DL_ENTRY; + break; + case 'e': + method=MTR_ADD_IP_UL_ENTRY; + break; + case 'f': + method=MTR_ADD_IP_DL_ENTRY; + break; + case 'g': + method=MTR_DEL_IP_UL_ENTRY; + break; + case 'h': + method=MTR_DEL_IP_DL_ENTRY; + break; + case 'k': + method=MTR_ADD_SYN_ENTRY; + break; + case 'l': + method=MTR_ADD_FIN_ENTRY; + break; + case 'n': + method=MTR_ADD_UDP_ENTRY; + break; + case 'o': + method=MTR_ADD_ICMP_ENTRY; + break; + case 'p': + method=MTR_DEL_SYN_ENTRY; + break; + case 'q': + method=MTR_DEL_FIN_ENTRY; + break; + case 'r': + method=MTR_DEL_UDP_ENTRY; + break; + case 'y': + method=MTR_DEL_ICMP_ENTRY; + break; + case 'z': /* CleanTbl */ + method=MTR_CLEAN_TBL; + break; + case 'm': /* Mac */ + str_to_mac(args.mac, optarg); + break; + case 'i': /* IpS */ + str_to_ip(&args.ip_s, optarg); + break; + case 'j': /* IpE */ + str_to_ip(&args.ip_e, optarg); + break; + case 't': /* TokenRate */ + args.token_rate=strtoll(optarg, NULL, 10); + break; + case 's': /* Bucket Size */ + if(strcasecmp(optarg,"4K")==0) + { + args.bk_size=0; + } + else if(strcasecmp(optarg,"8K")==0) + { + args.bk_size=1; + } + else if(strcasecmp(optarg,"16K")==0) + { + args.bk_size=2; + } + else if(strcasecmp(optarg,"32K")==0) + { + args.bk_size=3; + } + else + { + args.bk_size=strtoll(optarg, NULL, 10); + } + break; + case 'u': + if(strcasecmp(optarg,"1ms")==0) + { + args.mtr_intval=_1MS; + } + else if(strcasecmp(optarg,"10ms")==0) + { + args.mtr_intval=_10MS; + } + else if(strcasecmp(optarg,"50ms")==0) + { + args.mtr_intval=_50MS; + } + else if(strcasecmp(optarg,"100ms")==0) + { + args.mtr_intval=_100MS; + } + else if(strcasecmp(optarg,"500ms")==0) + { + args.mtr_intval=_500MS; + } + else if(strcasecmp(optarg,"1000ms")==0) + { + args.mtr_intval=_1000MS; + } + else if(strcasecmp(optarg,"5000ms")==0) + { + args.mtr_intval=_5000MS; + } + else if(strcasecmp(optarg,"10000ms")==0) + { + args.mtr_intval=_10000MS; + } + else + { + printf("Error: -u 10ms/50ms/100ms/500ms/1000ms/5000ms/10000ms\n"); + free(args2); + close(fd); + return 0; + } + break; + case 'v': + if(strcasecmp(optarg,"Byte")==0) + { + args.mtr_mode=0; + } + else if(strcasecmp(optarg,"Pkt")==0) + { + args.mtr_mode=1; + } + else + { + printf("Error: -v Byte/Pkt\n"); + free(args2); + close(fd); + return 0; + } + break; + case 'w': + method=MTR_GET_ALL_ENTRIES; + break; + case '?': /* Help */ + show_usage(); + break; + } + } + + switch(method) + { + case MTR_ADD_MAC_UL_ENTRY: + case MTR_ADD_MAC_DL_ENTRY: + case MTR_DEL_MAC_UL_ENTRY: + case MTR_DEL_MAC_DL_ENTRY: + case MTR_ADD_IP_UL_ENTRY: + case MTR_ADD_IP_DL_ENTRY: + case MTR_DEL_IP_UL_ENTRY: + case MTR_DEL_IP_DL_ENTRY: + case MTR_CLEAN_TBL: + case MTR_ADD_SYN_ENTRY: + case MTR_ADD_FIN_ENTRY: + case MTR_ADD_UDP_ENTRY: + case MTR_ADD_ICMP_ENTRY: + case MTR_DEL_SYN_ENTRY: + case MTR_DEL_FIN_ENTRY: + case MTR_DEL_UDP_ENTRY: + case MTR_DEL_ICMP_ENTRY: + SetMtrEntry(&args, method); + result = args.result; + break; + case MTR_GET_ALL_ENTRIES: + MtrGetAllEntries(args2); + result = args2->result; + + printf("Total Entry Count = %d\n",args2->num_of_entries); + for(i=0; inum_of_entries; i++) + { + printf("#%d :MAC=%02X:%02X:%02X:%02X:%02X:%02X\n", \ + i, args2->entries[i].mac[0], args2->entries[i].mac[1], args2->entries[i].mac[2], \ + args2->entries[i].mac[3], args2->entries[i].mac[4], args2->entries[i].mac[5]); + printf(" :SIP %u.%u.%u.%u->%u.%u.%u.%u\n\r", NIPQUAD(args2->entries[i].ip_s), NIPQUAD(args2->entries[i].ip_e)); + printf(" :BucketSize=%d Token_Rate:%d MtrInterval=%d\n", args2->entries[i].bk_size, args2->entries[i].token_rate, args2->entries[i].mtr_intval); + } + break; + default: + result = MTR_FAIL; + } + + + if(result == MTR_TBL_FULL) + { + printf("table full\n"); + } + else if(result == MTR_FAIL) + { + printf("fail\n"); + } + else + { + printf("done\n"); + } + + free(args2); + close(fd); + return 0; +} diff --git a/package/ramips/applications/hwnat/src/mtr_api.c b/package/ramips/applications/hwnat/src/mtr_api.c new file mode 100755 index 0000000..2512478 --- /dev/null +++ b/package/ramips/applications/hwnat/src/mtr_api.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include +#include "mtr_ioctl.h" + +int SetMtrEntry(struct mtr_args *opt, unsigned int cmd) +{ + int fd; + + fd = open("/dev/"MTR_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"MTR_DEVNAME); + return MTR_FAIL; + } + + if(ioctl(fd, cmd, opt)<0) + { + printf("MTR_API: ioctl error\n"); + close(fd); + return MTR_FAIL; + } + + close(fd); + return MTR_SUCCESS; +} + +int MtrGetAllEntries(struct mtr_list_args *opt) +{ + int fd=0; + + fd = open("/dev/"MTR_DEVNAME, O_RDONLY); + if (fd < 0) + { + printf("Open %s pseudo device failed\n","/dev/"MTR_DEVNAME); + return MTR_FAIL; + } + + if(ioctl(fd, MTR_GET_ALL_ENTRIES, opt)<0) + { + printf("MTR_API: ioctl error\n"); + close(fd); + return MTR_FAIL; + } + + close(fd); + + return MTR_SUCCESS; + +} + diff --git a/package/ramips/applications/hwnat/src/mtr_api.h b/package/ramips/applications/hwnat/src/mtr_api.h new file mode 100755 index 0000000..dd3fbb9 --- /dev/null +++ b/package/ramips/applications/hwnat/src/mtr_api.h @@ -0,0 +1,16 @@ +#ifndef __MTR_API +#define __MTR_API + +#define NIPQUAD(addr) \ + ((unsigned char *)&addr)[3], \ + ((unsigned char *)&addr)[2], \ + ((unsigned char *)&addr)[1], \ + ((unsigned char *)&addr)[0] +#define NIPHALF(addr) \ + ((unsigned short *)&addr)[1], \ + ((unsigned short *)&addr)[0] + +int SetMtrEntry(struct mtr_args *opt, unsigned int cmd); +int MtrGetAllEntries(struct mtr_list_args *opt); + +#endif diff --git a/package/ramips/applications/hwnat/src/util.c b/package/ramips/applications/hwnat/src/util.c new file mode 100755 index 0000000..257202d --- /dev/null +++ b/package/ramips/applications/hwnat/src/util.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +//#include +#include + +int +getnext ( + char * src, + int separator, + char * dest +) +{ + char * c; + int len; + + if ( (src == NULL) || (dest == NULL) ) + { + return -1; + } + + c = strchr(src, separator); + if (c == NULL) + { + strcpy(dest, src); + return -1; + } + len = c - src; + strncpy(dest, src, len); + dest[len] = '\0'; + return len + 1; +} + +int +str_to_mac ( + unsigned char * mac, + char * str +) +{ + int len; + char * ptr = str; + char buf[128]; + int i; + + for (i = 0; i < 5; i++) + { + if ((len = getnext(ptr, ':', buf)) == -1) + { + return 1; /* parse error */ + } + mac[i] = strtol(buf, NULL, 16); + ptr += len; + } + mac[5] = strtol(ptr, NULL, 16); + + return 0; +} + +int +str_to_ip ( + unsigned long * ip, + char * str +) +{ + int len; + char * ptr = str; + char buf[128]; + unsigned char c[4]; + int i; + + for (i = 0; i < 3; ++i) + { + if ((len = getnext(ptr, '.', buf)) == -1) + { + return 1; /* parse error */ + } + c[i] = atoi(buf); + ptr += len; + } + c[3] = atoi(ptr); + *ip = (c[0]<<24) + (c[1]<<16) + (c[2]<<8) + c[3]; + return 0; +} + + diff --git a/package/ramips/applications/hwnat/src/util.h b/package/ramips/applications/hwnat/src/util.h new file mode 100755 index 0000000..b6dbaf3 --- /dev/null +++ b/package/ramips/applications/hwnat/src/util.h @@ -0,0 +1,2 @@ +int str_to_mac (unsigned char *mac, char *str); +int str_to_ip (unsigned long *ip, char *str); -- cgit v1.1