Permalink
Browse files

Add initial switch support for the pfSense PHP module.

  • Loading branch information...
1 parent 495b80b commit eebf78e8ecd23522c7fc43ca3b49ece35a200a0a @loos-br loos-br committed Nov 22, 2016
@@ -1,7 +1,7 @@
# $FreeBSD$
PORTNAME= pfSense
-PORTVERSION= 0.21
+PORTVERSION= 0.22
CATEGORIES= devel
MASTER_SITES= #
DISTFILES= #
@@ -13,16 +13,18 @@ COMMENT= Library for getting useful info
LIB_DEPENDS= libvici.so:security/strongswan
-OPTIONS_DEFINE= IPFW
-OPTIONS_DEFAULT=IPFW
+OPTIONS_DEFINE= IPFW ETHERSWITCH
+OPTIONS_DEFAULT=IPFW ETHERSWITCH
IPFW_DESC= Enable IPFW functions
+ETHERSWITCH_DESC= Enable etherswitch functions
USES= php:ext
CFLAGS+= -I/usr/local/include -I. -DTCPSTATES
CFLAGS+= -Wall -Wredundant-decls -Wnested-externs -Winline
IPFW_CFLAGS= -DIPFW_FUNCTIONS
+ETHERSWITCH_CFLAGS= -DETHERSWITCH_FUNCTIONS
CONFIGURE_ARGS= --enable-pfSense
@@ -0,0 +1,103 @@
+/*
+ * $FreeBSD$
+ */
+
+#ifndef __SYS_DEV_ETHERSWITCH_ETHERSWITCH_H
+#define __SYS_DEV_ETHERSWITCH_ETHERSWITCH_H
+
+#include <sys/ioccom.h>
+
+#ifdef _KERNEL
+extern devclass_t etherswitch_devclass;
+extern driver_t etherswitch_driver;
+#endif /* _KERNEL */
+
+struct etherswitch_reg {
+ uint16_t reg;
+ uint16_t val;
+};
+typedef struct etherswitch_reg etherswitch_reg_t;
+
+struct etherswitch_phyreg {
+ uint16_t phy;
+ uint16_t reg;
+ uint16_t val;
+};
+typedef struct etherswitch_phyreg etherswitch_phyreg_t;
+
+#define ETHERSWITCH_NAMEMAX 64
+#define ETHERSWITCH_VID_MASK 0xfff
+#define ETHERSWITCH_VID_VALID (1 << 12)
+#define ETHERSWITCH_VLAN_ISL (1 << 0) /* ISL */
+#define ETHERSWITCH_VLAN_PORT (1 << 1) /* Port based vlan */
+#define ETHERSWITCH_VLAN_DOT1Q (1 << 2) /* 802.1q */
+#define ETHERSWITCH_VLAN_DOT1Q_4K (1 << 3) /* 4k support on 802.1q */
+#define ETHERSWITCH_VLAN_DOUBLE_TAG (1 << 4) /* Q-in-Q */
+#define ETHERSWITCH_VLAN_CAPS_BITS \
+"\020\1ISL\2PORT\3DOT1Q\4DOT1Q4K\5QinQ"
+
+struct etherswitch_info {
+ int es_nports;
+ int es_nvlangroups;
+ char es_name[ETHERSWITCH_NAMEMAX];
+ uint32_t es_vlan_caps;
+};
+typedef struct etherswitch_info etherswitch_info_t;
+
+#define ETHERSWITCH_CONF_FLAGS (1 << 0)
+#define ETHERSWITCH_CONF_MIRROR (1 << 1)
+#define ETHERSWITCH_CONF_VLAN_MODE (1 << 2)
+
+struct etherswitch_conf {
+ uint32_t cmd; /* What to configure */
+ uint32_t vlan_mode; /* Switch VLAN mode */
+};
+typedef struct etherswitch_conf etherswitch_conf_t;
+
+#define ETHERSWITCH_PORT_CPU (1 << 0)
+#define ETHERSWITCH_PORT_STRIPTAG (1 << 1)
+#define ETHERSWITCH_PORT_ADDTAG (1 << 2)
+#define ETHERSWITCH_PORT_FIRSTLOCK (1 << 3)
+#define ETHERSWITCH_PORT_DROPUNTAGGED (1 << 4)
+#define ETHERSWITCH_PORT_DOUBLE_TAG (1 << 5)
+#define ETHERSWITCH_PORT_INGRESS (1 << 6)
+#define ETHERSWITCH_PORT_FLAGS_BITS \
+"\020\1CPUPORT\2STRIPTAG\3ADDTAG\4FIRSTLOCK\5DROPUNTAGGED\6QinQ\7INGRESS"
+
+struct etherswitch_port {
+ int es_port;
+ int es_pvid;
+ uint32_t es_flags;
+ union {
+ struct ifreq es_uifr;
+ struct ifmediareq es_uifmr;
+ } es_ifu;
+#define es_ifr es_ifu.es_uifr
+#define es_ifmr es_ifu.es_uifmr
+};
+typedef struct etherswitch_port etherswitch_port_t;
+
+struct etherswitch_vlangroup {
+ int es_vlangroup;
+ int es_vid;
+ int es_member_ports;
+ int es_untagged_ports;
+ int es_fid;
+};
+typedef struct etherswitch_vlangroup etherswitch_vlangroup_t;
+
+#define ETHERSWITCH_PORTMASK(_port) (1 << (_port))
+
+#define IOETHERSWITCHGETINFO _IOR('i', 1, etherswitch_info_t)
+#define IOETHERSWITCHGETREG _IOWR('i', 2, etherswitch_reg_t)
+#define IOETHERSWITCHSETREG _IOW('i', 3, etherswitch_reg_t)
+#define IOETHERSWITCHGETPORT _IOWR('i', 4, etherswitch_port_t)
+#define IOETHERSWITCHSETPORT _IOW('i', 5, etherswitch_port_t)
+#define IOETHERSWITCHGETVLANGROUP _IOWR('i', 6, etherswitch_vlangroup_t)
+#define IOETHERSWITCHSETVLANGROUP _IOW('i', 7, etherswitch_vlangroup_t)
+#define IOETHERSWITCHGETPHYREG _IOWR('i', 8, etherswitch_phyreg_t)
+#define IOETHERSWITCHSETPHYREG _IOW('i', 9, etherswitch_phyreg_t)
+#define IOETHERSWITCHGETCONF _IOR('i', 10, etherswitch_conf_t)
+#define IOETHERSWITCHSETCONF _IOW('i', 11, etherswitch_conf_t)
+
+#endif
@@ -107,6 +107,10 @@ IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "config.h"
#endif
+#ifdef ETHERSWITCH_FUNCTIONS
+#include "etherswitch.h"
+#endif
+
#include "ipfw2.h"
#include "php.h"
#include "php_ini.h"
@@ -163,6 +167,11 @@ static zend_function_entry pfSense_functions[] = {
PHP_FE(pfSense_ipfw_tables_list, NULL)
PHP_FE(pfSense_ipfw_pipe, NULL)
#endif
+#ifdef ETHERSWITCH_FUNCTIONS
+ PHP_FE(pfSense_etherswitch_open, NULL)
+ PHP_FE(pfSense_etherswitch_close, NULL)
+ PHP_FE(pfSense_etherswitch_getinfo, NULL)
+#endif
PHP_FE(pfSense_ipsec_list_sa, NULL)
{NULL, NULL, NULL}
};
@@ -440,6 +449,9 @@ PHP_MINIT_FUNCTION(pfSense_socket)
fcntl(PFSENSE_G(ipfw), F_SETFD, fcntl(PFSENSE_G(ipfw), F_GETFD, 0) | FD_CLOEXEC);
#endif
+#ifdef ETHERSWITCH_FUNCTIONS
+ PFSENSE_G(etherswitch) = -1;
+#endif
/* Create a new socket node */
if (NgMkSockNode(NULL, &csock, NULL) < 0)
csock = -1;
@@ -1580,6 +1592,88 @@ PHP_FUNCTION(pfSense_ipfw_tables_list)
}
#endif
+#ifdef ETHERSWITCH_FUNCTIONS
+static int
+etherswitch_open(const char *dev)
+{
+
+ PFSENSE_G(etherswitch) = open(dev, O_RDONLY);
+ if (PFSENSE_G(etherswitch) == -1)
+ return (-1);
+
+ return (0);
+}
+
+PHP_FUNCTION(pfSense_etherswitch_open)
+{
+ char *devname;
+ long devnamelen;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
+ &devname, &devnamelen) == FAILURE) {
+ RETURN_FALSE;
+ }
+
+ if (devnamelen == 0)
+ devname = "/dev/etherswitch0";
+
+ if (PFSENSE_G(etherswitch) != -1)
+ RETURN_TRUE;
+
+ if (etherswitch_open(devname) == -1)
+ RETURN_FALSE;
+
+ RETURN_TRUE;
+}
+
+PHP_FUNCTION(pfSense_etherswitch_close)
+{
+ if (PFSENSE_G(etherswitch) == -1)
+ RETURN_TRUE;
+ close(PFSENSE_G(etherswitch));
+ PFSENSE_G(etherswitch) = -1;
+ RETURN_TRUE;
+}
+
+PHP_FUNCTION(pfSense_etherswitch_getinfo)
+{
+ etherswitch_conf_t conf;
+ etherswitch_info_t info;
+ zval *caps;
+
+ if (PFSENSE_G(etherswitch) == -1)
+ if (etherswitch_open("/dev/etherswitch0") == -1)
+ RETURN_NULL();
+ memset(&info, 0, sizeof(info));
+ if (ioctl(PFSENSE_G(etherswitch), IOETHERSWITCHGETINFO, &info) != 0)
+ RETURN_NULL();
+ memset(&conf, 0, sizeof(conf));
+ if (ioctl(PFSENSE_G(etherswitch), IOETHERSWITCHGETCONF, &conf) != 0)
+ RETURN_NULL();
+
+ array_init(return_value);
+ add_assoc_string(return_value, "name", info.es_name, 1);
+ add_assoc_long(return_value, "nports", info.es_nports);
+ add_assoc_long(return_value, "nvlangroups", info.es_nvlangroups);
+
+ ALLOC_INIT_ZVAL(caps);
+ array_init(caps);
+
+ if (info.es_vlan_caps & ETHERSWITCH_VLAN_ISL)
+ add_assoc_long(caps, "ISL", 1);
+ if (info.es_vlan_caps & ETHERSWITCH_VLAN_PORT)
+ add_assoc_long(caps, "PORT", 1);
+ if (info.es_vlan_caps & ETHERSWITCH_VLAN_DOT1Q)
+ add_assoc_long(caps, "DOT1Q", 1);
+ if (info.es_vlan_caps & ETHERSWITCH_VLAN_DOT1Q_4K)
+ add_assoc_long(caps, "DOT1Q4K", 1);
+ if (info.es_vlan_caps & ETHERSWITCH_VLAN_DOUBLE_TAG)
+ add_assoc_long(caps, "QinQ", 1);
+
+ add_assoc_zval(return_value, "caps", caps);
+}
+#endif
+
#ifdef DHCP_INTEGRATION
PHP_FUNCTION(pfSense_open_dhcpd)
{
@@ -41,6 +41,9 @@ ZEND_BEGIN_MODULE_GLOBALS(pfSense)
#ifdef IPFW_FUNCTIONS
int ipfw;
#endif
+#ifdef ETHERSWITCH_FUNCTIONS
+ int etherswitch;
+#endif
int csock;
ZEND_END_MODULE_GLOBALS(pfSense)
@@ -111,6 +114,11 @@ PHP_FUNCTION(pfSense_ipfw_table_lookup);
PHP_FUNCTION(pfSense_ipfw_tables_list);
PHP_FUNCTION(pfSense_ipfw_pipe);
#endif
+#ifdef ETHERSWITCH_FUNCTIONS
+PHP_FUNCTION(pfSense_etherswitch_open);
+PHP_FUNCTION(pfSense_etherswitch_close);
+PHP_FUNCTION(pfSense_etherswitch_getinfo);
+#endif
PHP_FUNCTION(pfSense_ipsec_list_sa);
extern zend_module_entry pfSense_module_entry;

0 comments on commit eebf78e

Please sign in to comment.