#
# Copyright (C) 2024 IOPSYS Software Solutions AB
#

include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=ebtables-extensions
PKG_VERSION:=2.0.5
PKG_LICENSE:=GPL-2.0

LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/network/ebtables-extensions.git
PKG_SOURCE_VERSION:=7357622d806833d93d317164dc6673fbf5fd1629
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MIRROR_HASH:=skip
endif

include $(INCLUDE_DIR)/package.mk

define KernelPackage/ebt-vlantranslation
  SUBMENU:=Other modules
  TITLE:=Kernel module for ebtables VLAN translation
  FILES:=$(PKG_BUILD_DIR)/src/ebt_vlantranslation.ko
  DEPENDS+=+kmod-ebtables
  AUTOLOAD:=$(call AutoLoad,30,ebt_vlantranslation)
  KCONFIG:=
endef

define KernelPackage/ebt-dscp2pbit
  SUBMENU:=Other modules
  TITLE:=Kernel module for DSCP-to-Pbit mapping
  DEPENDS+=+kmod-ebtables
  FILES:=$(PKG_BUILD_DIR)/src/ebt_dscp2pbit.ko
  AUTOLOAD:=$(call AutoLoad,30,ebt_dscp2pbit)
  KCONFIG:=
endef

define KernelPackage/ebt-dhcp
  SUBMENU:=Other modules
  TITLE:=Kernel module for ebtables DHCP snooping
  FILES:=$(PKG_BUILD_DIR)/src/ebt_dhcp.ko
  DEPENDS+=+kmod-ebtables
  AUTOLOAD:=$(call AutoLoad,30,ebt_dhcp)
  KCONFIG:=
endef

define KernelPackage/ebt-dhcpv6
  SUBMENU:=Other modules
  TITLE:=Kernel module for ebtables DHCPv6 snooping
  FILES:=$(PKG_BUILD_DIR)/src/ebt_dhcpv6.ko
  DEPENDS+=+kmod-ebtables
  AUTOLOAD:=$(call AutoLoad,30,ebt_dhcpv6)
  KCONFIG:=
endef

define KernelPackage/ebt-ra
  SUBMENU:=Other modules
  TITLE:=Kernel module for ebtables NDP/RA snooping
  FILES:=$(PKG_BUILD_DIR)/src/ebt_ra.ko
  DEPENDS+=+kmod-ebtables
  AUTOLOAD:=$(call AutoLoad,30,ebt_ra)
  KCONFIG:=
endef

define KernelPackage/ebt-dns
  SUBMENU:=Other modules
  TITLE:=Kernel module for ebtables DNS manipulation
  FILES:=$(PKG_BUILD_DIR)/src/ebt_dns.ko
  DEPENDS+=+kmod-ebtables
  AUTOLOAD:=$(call AutoLoad,30,ebt_dns)
  KCONFIG:=
endef

define KernelPackage/ebt-pppoe_disc
  SUBMENU:=Other modules
  TITLE:=Kernel module for ebtables PPPoE manipulation
  FILES:=$(PKG_BUILD_DIR)/src/ebt_pppoe_disc.ko
  DEPENDS+=+kmod-ebtables
  AUTOLOAD:=$(call AutoLoad,30,ebt_pppoe_disc)
  KCONFIG:=
endef

define KernelPackage/ebt-l2pt
  SUBMENU:=Other modules
  TITLE:=Kernel module for ebtables L2CP tunneling
  FILES:=$(PKG_BUILD_DIR)/src/ebt_l2pt.ko
  DEPENDS+=@TARGET_airoha +kmod-ebtables
  AUTOLOAD:=$(call AutoLoad,30,ebt_l2pt)
  KCONFIG:=
endef

define KernelPackage/ebt-xtarp
  SUBMENU:=Other modules
  TITLE:=Kernel module for ebtables extended ARP matching
  FILES:=$(PKG_BUILD_DIR)/src/ebt_xtarp.ko
  DEPENDS+=+kmod-ebtables
  AUTOLOAD:=$(call AutoLoad,30,ebt_xtarp)
  KCONFIG:=
endef

define KernelPackage/ebt-xtip
  SUBMENU:=Other modules
  TITLE:=Kernel module for ebtables extended IPv4 matching
  FILES:=$(PKG_BUILD_DIR)/src/ebt_xtip.ko
  DEPENDS+=+kmod-ebtables
  AUTOLOAD:=$(call AutoLoad,30,ebt_xtip)
  KCONFIG:=
endef

define KernelPackage/ebt-xtip6
  SUBMENU:=Other modules
  TITLE:=Kernel module for ebtables extended IPv6 matching
  FILES:=$(PKG_BUILD_DIR)/src/ebt_xtip6.ko
  DEPENDS+=+kmod-ebtables
  AUTOLOAD:=$(call AutoLoad,30,ebt_xtip6)
  KCONFIG:=
endef

define KernelPackage/ebt-igmpsnooping
  SUBMENU:=Other modules
  TITLE:=Kernel module for ebtables IGMP snooping
  FILES:=$(PKG_BUILD_DIR)/src/ebt_igmpsnooping.ko
  DEPENDS+=+kmod-ebtables
  # AUTOLOAD:=$(call AutoLoad,30,ebt_igmpsnooping) - Will be loaded on demand
  KCONFIG:=
endef

define KernelPackage/ebt-mldsnooping
  SUBMENU:=Other modules
  TITLE:=Kernel module for ebtables MLD snooping
  FILES:=$(PKG_BUILD_DIR)/src/ebt_mldsnooping.ko
  DEPENDS+=+kmod-ebtables
  # AUTOLOAD:=$(call AutoLoad,30,ebt_mldsnooping) - Will be loaded on demand
  KCONFIG:=
endef

define KernelPackage/ebt-dhcp/description
	Kernel module to enable DHCP snooping for ebtables
endef

define KernelPackage/ebt-dhcpv6/description
	Kernel module to enable DHCPv6 snooping for ebtables
endef

define KernelPackage/ebt-ra/description
	Kernel module to enable NDP/RA snooping for ebtables
endef

define KernelPackage/ebt-dns/description
	Kernel module to enable DNS manipulation for ebtables
endef

define KernelPackage/ebt-pppoe_disc/description
	Kernel module to enable PPPoE manipulation for ebtables
endef

define KernelPackage/ebt-l2pt/description
	Kernel module to enable L2CP tunneling for ebtables
endef

define KernelPackage/ebt-xtarp/description
	Kernel module to enable extended ARP matching for ebtables
endef

define KernelPackage/ebt-xtip/description
	Kernel module to enable extended IPv4 matching for ebtables
endef

define KernelPackage/ebt-xtip6/description
	Kernel module to enable extended IPv6 matching for ebtables
endef

define KernelPackage/ebt-vlantranslation/description
	Kernel module to enable VLAN translation for ebtables
endef

define KernelPackage/ebt-dscp2pbit/description
	Kernel module to enable DSCP-to-Pbit mapping for ebtables
endef

define KernelPackage/ebt-igmpsnooping/description
	Kernel module to enable IGMP snooping for ebtables
endef

define KernelPackage/ebt-mldsnooping/description
	Kernel module to enable MLD snooping for ebtables
endef

-include ../../broadcom/bcmkernel/bcm-kernel-toolchain.mk

define Build/Prepare

ifeq ($(LOCAL_DEV),1)
	$(CP) -rf ./ebtables-extensions/* $(PKG_BUILD_DIR)/
else
	$(Build/Prepare/Default)
endif
	$(CP) $(PKG_BUILD_DIR)/src/ebt_vlantranslation.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_dscp2pbit.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_dhcp.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_dhcpv6.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_ra.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_dns.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_pppoe_disc.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
ifeq ($(CONFIG_TARGET_airoha),y)
	$(CP) $(PKG_BUILD_DIR)/src/ebt_l2pt.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
endif
	$(CP) $(PKG_BUILD_DIR)/src/ebt_xtarp.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_xtip.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_xtip6.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_igmpsnooping.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_mldsnooping.h $(LINUX_DIR)/include/uapi/linux/netfilter_bridge/
endef

define Build/InstallDev
	$(INSTALL_DIR) $(1)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_vlantranslation.h $(1)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_dscp2pbit.h $(1)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_dhcp.h $(1)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_dhcpv6.h $(1)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_ra.h $(1)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_dns.h $(1)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_pppoe_disc.h $(1)/include/uapi/linux/netfilter_bridge/
ifeq ($(CONFIG_TARGET_airoha),y)
	$(CP) $(PKG_BUILD_DIR)/src/ebt_l2pt.h $(1)/include/uapi/linux/netfilter_bridge/
endif
	$(CP) $(PKG_BUILD_DIR)/src/ebt_xtarp.h $(1)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_xtip.h $(1)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_xtip6.h $(1)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_igmpsnooping.h $(1)/include/uapi/linux/netfilter_bridge/
	$(CP) $(PKG_BUILD_DIR)/src/ebt_mldsnooping.h $(1)/include/uapi/linux/netfilter_bridge/
endef

ifeq ($(CONFIG_TARGET_airoha),y)
  KERNEL_MAKE_FLAGS += PLATFORM="ECONET"
endif

define Build/Compile
	$(KERNEL_MAKE) M="$(PKG_BUILD_DIR)/src" modules
endef

$(eval $(call KernelPackage,ebt-vlantranslation))
$(eval $(call KernelPackage,ebt-dscp2pbit))
$(eval $(call KernelPackage,ebt-dhcp))
$(eval $(call KernelPackage,ebt-dhcpv6))
$(eval $(call KernelPackage,ebt-ra))
$(eval $(call KernelPackage,ebt-dns))
$(eval $(call KernelPackage,ebt-pppoe_disc))
$(eval $(call KernelPackage,ebt-l2pt))
$(eval $(call KernelPackage,ebt-xtarp))
$(eval $(call KernelPackage,ebt-xtip))
$(eval $(call KernelPackage,ebt-xtip6))
$(eval $(call KernelPackage,ebt-igmpsnooping))
$(eval $(call KernelPackage,ebt-mldsnooping))
