int (*check)(const char *tablename, unsigned int hookmask,
           const struct ebt_entry *e, void *matchdata, unsigned int datalen);
        void (*destroy)(void *matchdata, unsigned int datalen);
+       unsigned int matchsize;
        struct module *me;
 };
 
        int (*check)(const char *tablename, unsigned int hookmask,
           const struct ebt_entry *e, void *watcherdata, unsigned int datalen);
        void (*destroy)(void *watcherdata, unsigned int datalen);
+       unsigned int targetsize;
        struct module *me;
 };
 
        int (*check)(const char *tablename, unsigned int hookmask,
           const struct ebt_entry *e, void *targetdata, unsigned int datalen);
        void (*destroy)(void *targetdata, unsigned int datalen);
+       unsigned int targetsize;
        struct module *me;
 };
 
 
  * May 2003
  *
  */
-
+#include <linux/module.h>
+#include <linux/netfilter/x_tables.h>
 #include <linux/netfilter_bridge/ebtables.h>
 #include <linux/netfilter_bridge/ebt_802_3.h>
-#include <linux/module.h>
 
 static int ebt_filter_802_3(const struct sk_buff *skb, const struct net_device *in,
    const struct net_device *out, const void *data, unsigned int datalen)
 {
        const struct ebt_802_3_info *info = data;
 
-       if (datalen < sizeof(struct ebt_802_3_info))
-               return -EINVAL;
        if (info->bitmask & ~EBT_802_3_MASK || info->invflags & ~EBT_802_3_MASK)
                return -EINVAL;
 
        .name           = EBT_802_3_MATCH,
        .match          = ebt_filter_802_3,
        .check          = ebt_802_3_check,
+       .matchsize      = XT_ALIGN(sizeof(struct ebt_802_3_info)),
        .me             = THIS_MODULE,
 };
 
 
        .name           = EBT_AMONG_MATCH,
        .match          = ebt_filter_among,
        .check          = ebt_among_check,
+       .matchsize      = -1, /* special case */
        .me             = THIS_MODULE,
 };
 
 
  *  April, 2002
  *
  */
-
-#include <linux/netfilter_bridge/ebtables.h>
-#include <linux/netfilter_bridge/ebt_arp.h>
 #include <linux/if_arp.h>
 #include <linux/if_ether.h>
 #include <linux/module.h>
+#include <linux/netfilter/x_tables.h>
+#include <linux/netfilter_bridge/ebtables.h>
+#include <linux/netfilter_bridge/ebt_arp.h>
 
 static int ebt_filter_arp(const struct sk_buff *skb, const struct net_device *in,
    const struct net_device *out, const void *data, unsigned int datalen)
 {
        const struct ebt_arp_info *info = data;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_arp_info)))
-               return -EINVAL;
        if ((e->ethproto != htons(ETH_P_ARP) &&
           e->ethproto != htons(ETH_P_RARP)) ||
           e->invflags & EBT_IPROTO)
        .name           = EBT_ARP_MATCH,
        .match          = ebt_filter_arp,
        .check          = ebt_arp_check,
+       .matchsize      = XT_ALIGN(sizeof(struct ebt_arp_info)),
        .me             = THIS_MODULE,
 };
 
 
  *  August, 2003
  *
  */
-
-#include <linux/netfilter_bridge/ebtables.h>
-#include <linux/netfilter_bridge/ebt_arpreply.h>
 #include <linux/if_arp.h>
 #include <net/arp.h>
 #include <linux/module.h>
+#include <linux/netfilter/x_tables.h>
+#include <linux/netfilter_bridge/ebtables.h>
+#include <linux/netfilter_bridge/ebt_arpreply.h>
 
 static int ebt_target_reply(struct sk_buff *skb, unsigned int hooknr,
    const struct net_device *in, const struct net_device *out,
 {
        const struct ebt_arpreply_info *info = data;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_arpreply_info)))
-               return -EINVAL;
        if (BASE_CHAIN && info->target == EBT_RETURN)
                return -EINVAL;
        if (e->ethproto != htons(ETH_P_ARP) ||
        .name           = EBT_ARPREPLY_TARGET,
        .target         = ebt_target_reply,
        .check          = ebt_target_reply_check,
+       .targetsize     = XT_ALIGN(sizeof(struct ebt_arpreply_info)),
        .me             = THIS_MODULE,
 };
 
 
  *  June, 2002
  *
  */
-
+#include <linux/module.h>
+#include <net/sock.h>
 #include <linux/netfilter.h>
+#include <linux/netfilter/x_tables.h>
 #include <linux/netfilter_bridge/ebtables.h>
 #include <linux/netfilter_bridge/ebt_nat.h>
-#include <linux/module.h>
-#include <net/sock.h>
 
 static int ebt_target_dnat(struct sk_buff *skb, unsigned int hooknr,
    const struct net_device *in, const struct net_device *out,
           (hookmask & ~((1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_LOCAL_OUT)))) &&
           (strcmp(tablename, "broute") || hookmask & ~(1 << NF_BR_BROUTING)) )
                return -EINVAL;
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_nat_info)))
-               return -EINVAL;
        if (INVALID_TARGET)
                return -EINVAL;
        return 0;
        .name           = EBT_DNAT_TARGET,
        .target         = ebt_target_dnat,
        .check          = ebt_target_dnat_check,
+       .targetsize     = XT_ALIGN(sizeof(struct ebt_nat_info)),
        .me             = THIS_MODULE,
 };
 
 
  *    Innominate Security Technologies AG <mhopf@innominate.com>
  *    September, 2002
  */
-
-#include <linux/netfilter_bridge/ebtables.h>
-#include <linux/netfilter_bridge/ebt_ip.h>
 #include <linux/ip.h>
 #include <net/ip.h>
 #include <linux/in.h>
 #include <linux/module.h>
+#include <linux/netfilter/x_tables.h>
+#include <linux/netfilter_bridge/ebtables.h>
+#include <linux/netfilter_bridge/ebt_ip.h>
 
 struct tcpudphdr {
        __be16 src;
 {
        const struct ebt_ip_info *info = data;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_ip_info)))
-               return -EINVAL;
        if (e->ethproto != htons(ETH_P_IP) ||
           e->invflags & EBT_IPROTO)
                return -EINVAL;
        .name           = EBT_IP_MATCH,
        .match          = ebt_filter_ip,
        .check          = ebt_ip_check,
+       .matchsize      = XT_ALIGN(sizeof(struct ebt_ip_info)),
        .me             = THIS_MODULE,
 };
 
 
  *
  *  Jan, 2008
  */
-
-#include <linux/netfilter_bridge/ebtables.h>
-#include <linux/netfilter_bridge/ebt_ip6.h>
 #include <linux/ipv6.h>
 #include <net/ipv6.h>
 #include <linux/in.h>
 #include <linux/module.h>
 #include <net/dsfield.h>
+#include <linux/netfilter/x_tables.h>
+#include <linux/netfilter_bridge/ebtables.h>
+#include <linux/netfilter_bridge/ebt_ip6.h>
 
 struct tcpudphdr {
        __be16 src;
 {
        struct ebt_ip6_info *info = (struct ebt_ip6_info *)data;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_ip6_info)))
-               return -EINVAL;
        if (e->ethproto != htons(ETH_P_IPV6) || e->invflags & EBT_IPROTO)
                return -EINVAL;
        if (info->bitmask & ~EBT_IP6_MASK || info->invflags & ~EBT_IP6_MASK)
        .name           = EBT_IP6_MATCH,
        .match          = ebt_filter_ip6,
        .check          = ebt_ip6_check,
+       .matchsize      = XT_ALIGN(sizeof(struct ebt_ip6_info)),
        .me             = THIS_MODULE,
 };
 
 
  *  September, 2003
  *
  */
-
-#include <linux/netfilter_bridge/ebtables.h>
-#include <linux/netfilter_bridge/ebt_limit.h>
 #include <linux/module.h>
-
 #include <linux/netdevice.h>
 #include <linux/spinlock.h>
+#include <linux/netfilter/x_tables.h>
+#include <linux/netfilter_bridge/ebtables.h>
+#include <linux/netfilter_bridge/ebt_limit.h>
 
 static DEFINE_SPINLOCK(limit_lock);
 
 {
        struct ebt_limit_info *info = data;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_limit_info)))
-               return -EINVAL;
-
        /* Check for overflow. */
        if (info->burst == 0 ||
            user2credits(info->avg * info->burst) < user2credits(info->avg)) {
        .name           = EBT_LIMIT_MATCH,
        .match          = ebt_limit_match,
        .check          = ebt_limit_check,
+       .matchsize      = XT_ALIGN(sizeof(struct ebt_limit_info)),
        .me             = THIS_MODULE,
 };
 
 
  *  April, 2002
  *
  */
-
-#include <linux/netfilter_bridge/ebtables.h>
-#include <linux/netfilter_bridge/ebt_log.h>
-#include <linux/netfilter.h>
 #include <linux/module.h>
 #include <linux/ip.h>
 #include <linux/in.h>
 #include <linux/ipv6.h>
 #include <net/ipv6.h>
 #include <linux/in6.h>
+#include <linux/netfilter/x_tables.h>
+#include <linux/netfilter_bridge/ebtables.h>
+#include <linux/netfilter_bridge/ebt_log.h>
+#include <linux/netfilter.h>
 
 static DEFINE_SPINLOCK(ebt_log_lock);
 
 {
        struct ebt_log_info *info = data;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_log_info)))
-               return -EINVAL;
        if (info->bitmask & ~EBT_LOG_MASK)
                return -EINVAL;
        if (info->loglevel >= 8)
        .name           = EBT_LOG_WATCHER,
        .watcher        = ebt_log,
        .check          = ebt_log_check,
+       .targetsize     = XT_ALIGN(sizeof(struct ebt_log_info)),
        .me             = THIS_MODULE,
 };
 
 
  * Marking a frame doesn't really change anything in the frame anyway.
  */
 
+#include <linux/module.h>
+#include <linux/netfilter/x_tables.h>
 #include <linux/netfilter_bridge/ebtables.h>
 #include <linux/netfilter_bridge/ebt_mark_t.h>
-#include <linux/module.h>
 
 static int ebt_target_mark(struct sk_buff *skb, unsigned int hooknr,
    const struct net_device *in, const struct net_device *out,
        const struct ebt_mark_t_info *info = data;
        int tmp;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_mark_t_info)))
-               return -EINVAL;
        tmp = info->target | ~EBT_VERDICT_BITS;
        if (BASE_CHAIN && tmp == EBT_RETURN)
                return -EINVAL;
        .name           = EBT_MARK_TARGET,
        .target         = ebt_target_mark,
        .check          = ebt_target_mark_check,
+       .targetsize     = XT_ALIGN(sizeof(struct ebt_mark_t_info)),
        .me             = THIS_MODULE,
 };
 
 
  *  July, 2002
  *
  */
-
+#include <linux/module.h>
+#include <linux/netfilter/x_tables.h>
 #include <linux/netfilter_bridge/ebtables.h>
 #include <linux/netfilter_bridge/ebt_mark_m.h>
-#include <linux/module.h>
 
 static int ebt_filter_mark(const struct sk_buff *skb,
    const struct net_device *in, const struct net_device *out, const void *data,
 {
        const struct ebt_mark_m_info *info = data;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_mark_m_info)))
-               return -EINVAL;
        if (info->bitmask & ~EBT_MARK_MASK)
                return -EINVAL;
        if ((info->bitmask & EBT_MARK_OR) && (info->bitmask & EBT_MARK_AND))
        .name           = EBT_MARK_MATCH,
        .match          = ebt_filter_mark,
        .check          = ebt_mark_check,
+       .matchsize      = XT_ALIGN(sizeof(struct ebt_mark_m_info)),
        .me             = THIS_MODULE,
 };
 
 
 
 #include <linux/module.h>
 #include <linux/spinlock.h>
+#include <linux/netfilter/x_tables.h>
 #include <linux/netfilter_bridge/ebtables.h>
 #include <linux/netfilter_bridge/ebt_nflog.h>
 #include <net/netfilter/nf_log.h>
 {
        struct ebt_nflog_info *info = (struct ebt_nflog_info *)data;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_nflog_info)))
-               return -EINVAL;
        if (info->flags & ~EBT_NFLOG_MASK)
                return -EINVAL;
        info->prefix[EBT_NFLOG_PREFIX_SIZE - 1] = '\0';
        .name = EBT_NFLOG_WATCHER,
        .watcher = ebt_nflog,
        .check = ebt_nflog_check,
+       .targetsize = XT_ALIGN(sizeof(struct ebt_nflog_info)),
        .me = THIS_MODULE,
 };
 
 
  *  April, 2003
  *
  */
-
+#include <linux/module.h>
+#include <linux/netfilter/x_tables.h>
 #include <linux/netfilter_bridge/ebtables.h>
 #include <linux/netfilter_bridge/ebt_pkttype.h>
-#include <linux/module.h>
 
 static int ebt_filter_pkttype(const struct sk_buff *skb,
    const struct net_device *in,
 {
        const struct ebt_pkttype_info *info = data;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_pkttype_info)))
-               return -EINVAL;
        if (info->invert != 0 && info->invert != 1)
                return -EINVAL;
        /* Allow any pkt_type value */
        .name           = EBT_PKTTYPE_MATCH,
        .match          = ebt_filter_pkttype,
        .check          = ebt_pkttype_check,
+       .matchsize      = XT_ALIGN(sizeof(struct ebt_pkttype_info)),
        .me             = THIS_MODULE,
 };
 
 
  *  April, 2002
  *
  */
-
-#include <linux/netfilter.h>
-#include <linux/netfilter_bridge/ebtables.h>
-#include <linux/netfilter_bridge/ebt_redirect.h>
 #include <linux/module.h>
 #include <net/sock.h>
 #include "../br_private.h"
+#include <linux/netfilter.h>
+#include <linux/netfilter/x_tables.h>
+#include <linux/netfilter_bridge/ebtables.h>
+#include <linux/netfilter_bridge/ebt_redirect.h>
 
 static int ebt_target_redirect(struct sk_buff *skb, unsigned int hooknr,
    const struct net_device *in, const struct net_device *out,
 {
        const struct ebt_redirect_info *info = data;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_redirect_info)))
-               return -EINVAL;
        if (BASE_CHAIN && info->target == EBT_RETURN)
                return -EINVAL;
        CLEAR_BASE_CHAIN_BIT;
        .name           = EBT_REDIRECT_TARGET,
        .target         = ebt_target_redirect,
        .check          = ebt_target_redirect_check,
+       .targetsize     = XT_ALIGN(sizeof(struct ebt_redirect_info)),
        .me             = THIS_MODULE,
 };
 
 
  *  June, 2002
  *
  */
-
-#include <linux/netfilter.h>
-#include <linux/netfilter_bridge/ebtables.h>
-#include <linux/netfilter_bridge/ebt_nat.h>
 #include <linux/module.h>
 #include <net/sock.h>
 #include <linux/if_arp.h>
 #include <net/arp.h>
+#include <linux/netfilter.h>
+#include <linux/netfilter/x_tables.h>
+#include <linux/netfilter_bridge/ebtables.h>
+#include <linux/netfilter_bridge/ebt_nat.h>
 
 static int ebt_target_snat(struct sk_buff *skb, unsigned int hooknr,
    const struct net_device *in, const struct net_device *out,
        const struct ebt_nat_info *info = data;
        int tmp;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_nat_info)))
-               return -EINVAL;
        tmp = info->target | ~EBT_VERDICT_BITS;
        if (BASE_CHAIN && tmp == EBT_RETURN)
                return -EINVAL;
        .name           = EBT_SNAT_TARGET,
        .target         = ebt_target_snat,
        .check          = ebt_target_snat_check,
+       .targetsize     = XT_ALIGN(sizeof(struct ebt_nat_info)),
        .me             = THIS_MODULE,
 };
 
 
  *
  *  July, 2003
  */
-
-#include <linux/netfilter_bridge/ebtables.h>
-#include <linux/netfilter_bridge/ebt_stp.h>
 #include <linux/etherdevice.h>
 #include <linux/module.h>
+#include <linux/netfilter/x_tables.h>
+#include <linux/netfilter_bridge/ebtables.h>
+#include <linux/netfilter_bridge/ebt_stp.h>
 
 #define BPDU_TYPE_CONFIG 0
 #define BPDU_TYPE_TCN 0x80
    const struct ebt_entry *e, void *data, unsigned int datalen)
 {
        const struct ebt_stp_info *info = data;
-       const unsigned int len = EBT_ALIGN(sizeof(struct ebt_stp_info));
        const uint8_t bridge_ula[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00};
        const uint8_t msk[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
 
        if (info->bitmask & ~EBT_STP_MASK || info->invflags & ~EBT_STP_MASK ||
            !(info->bitmask & EBT_STP_MASK))
                return -EINVAL;
-       if (datalen != len)
-               return -EINVAL;
        /* Make sure the match only receives stp frames */
        if (compare_ether_addr(e->destmac, bridge_ula) ||
            compare_ether_addr(e->destmsk, msk) || !(e->bitmask & EBT_DESTMAC))
        .name           = EBT_STP_MATCH,
        .match          = ebt_filter_stp,
        .check          = ebt_stp_check,
+       .matchsize      = XT_ALIGN(sizeof(struct ebt_stp_info)),
        .me             = THIS_MODULE,
 };
 
 
 #include <linux/timer.h>
 #include <linux/netlink.h>
 #include <linux/netdevice.h>
+#include <linux/netfilter/x_tables.h>
 #include <linux/netfilter_bridge/ebtables.h>
 #include <linux/netfilter_bridge/ebt_ulog.h>
 #include <net/netfilter/nf_log.h>
 {
        struct ebt_ulog_info *uloginfo = data;
 
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_ulog_info)) ||
-           uloginfo->nlgroup > 31)
+       if (uloginfo->nlgroup > 31)
                return -EINVAL;
 
        uloginfo->prefix[EBT_ULOG_PREFIX_LEN - 1] = '\0';
        .name           = EBT_ULOG_WATCHER,
        .watcher        = ebt_ulog,
        .check          = ebt_ulog_check,
+       .targetsize     = XT_ALIGN(sizeof(struct ebt_ulog_info)),
        .me             = THIS_MODULE,
 };
 
 
 #include <linux/if_vlan.h>
 #include <linux/module.h>
 #include <linux/moduleparam.h>
+#include <linux/netfilter/x_tables.h>
 #include <linux/netfilter_bridge/ebtables.h>
 #include <linux/netfilter_bridge/ebt_vlan.h>
 
 {
        struct ebt_vlan_info *info = data;
 
-       /* Parameters buffer overflow check */
-       if (datalen != EBT_ALIGN(sizeof(struct ebt_vlan_info))) {
-               DEBUG_MSG
-                   ("passed size %d is not eq to ebt_vlan_info (%Zd)\n",
-                    datalen, sizeof(struct ebt_vlan_info));
-               return -EINVAL;
-       }
-
        /* Is it 802.1Q frame checked? */
        if (e->ethproto != htons(ETH_P_8021Q)) {
                DEBUG_MSG
        .name           = EBT_VLAN_MATCH,
        .match          = ebt_filter_vlan,
        .check          = ebt_check_vlan,
+       .matchsize      = XT_ALIGN(sizeof(struct ebt_vlan_info)),
        .me             = THIS_MODULE,
 };
 
 
 #include <linux/kmod.h>
 #include <linux/module.h>
 #include <linux/vmalloc.h>
+#include <linux/netfilter/x_tables.h>
 #include <linux/netfilter_bridge/ebtables.h>
 #include <linux/spinlock.h>
 #include <linux/mutex.h>
 static LIST_HEAD(ebt_matches);
 static LIST_HEAD(ebt_watchers);
 
-static struct ebt_target ebt_standard_target =
-{ {NULL, NULL}, EBT_STANDARD_TARGET, NULL, NULL, NULL, NULL};
+static struct ebt_target ebt_standard_target = {
+       .name = "standard",
+};
 
 static inline int ebt_do_watcher (struct ebt_entry_watcher *w,
    const struct sk_buff *skb, unsigned int hooknr, const struct net_device *in,
                return -ENOENT;
        }
        mutex_unlock(&ebt_mutex);
+       if (XT_ALIGN(match->matchsize) != m->match_size &&
+           match->matchsize != -1) {
+               /*
+                * ebt_among is exempt from centralized matchsize checking
+                * because it uses a dynamic-size data set.
+                */
+               printk(KERN_WARNING "ebtables: %s match: "
+                      "invalid size %Zu != %u\n",
+                      match->name, XT_ALIGN(match->matchsize), m->match_size);
+               module_put(match->me);
+               return -EINVAL;
+       }
        if (match->check &&
           match->check(name, hookmask, e, m->data, m->match_size) != 0) {
                BUGPRINT("match->check failed\n");
                return -ENOENT;
        }
        mutex_unlock(&ebt_mutex);
+       if (XT_ALIGN(watcher->targetsize) != w->watcher_size) {
+               printk(KERN_WARNING "ebtables: %s watcher: "
+                      "invalid size %Zu != %u\n",
+                      watcher->name, XT_ALIGN(watcher->targetsize),
+                      w->watcher_size);
+               module_put(watcher->me);
+               return -EINVAL;
+       }
        if (watcher->check &&
           watcher->check(name, hookmask, e, w->data, w->watcher_size) != 0) {
                BUGPRINT("watcher->check failed\n");
                        ret = -EFAULT;
                        goto cleanup_watchers;
                }
-       } else if (t->target_size > gap - sizeof(struct ebt_entry_target) ||
-          (t->u.target->check &&
-          t->u.target->check(name, hookmask, e, t->data, t->target_size) != 0)){
+       } else if (t->target_size > gap - sizeof(struct ebt_entry_target)) {
+               module_put(t->u.target->me);
+               ret = -EFAULT;
+               goto cleanup_watchers;
+       } else if (XT_ALIGN(target->targetsize) != t->target_size) {
+               printk(KERN_WARNING "ebtables: %s target: "
+                      "invalid size %Zu != %u\n",
+                      target->name, XT_ALIGN(target->targetsize),
+                      t->target_size);
+               module_put(t->u.target->me);
+               ret = -EINVAL;
+               goto cleanup_watchers;
+       } else if (t->u.target->check &&
+           t->u.target->check(name, hookmask, e, t->data, t->target_size) != 0) {
                module_put(t->u.target->me);
                ret = -EFAULT;
                goto cleanup_watchers;