Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/drzeus/mmc
[firefly-linux-kernel-4.4.55.git] / net / bridge / netfilter / ebt_among.c
index 392d877040d31897f063d97c49b49ff7c1949532..b595f091f35b9cf1c2fc9bd6378edbc5fc5a90a2 100644 (file)
@@ -7,15 +7,15 @@
  *  August, 2003
  *
  */
-
-#include <linux/netfilter_bridge/ebtables.h>
-#include <linux/netfilter_bridge/ebt_among.h>
 #include <linux/ip.h>
 #include <linux/if_arp.h>
 #include <linux/module.h>
+#include <linux/netfilter/x_tables.h>
+#include <linux/netfilter_bridge/ebtables.h>
+#include <linux/netfilter_bridge/ebt_among.h>
 
-static int ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh,
-                                    const char *mac, __be32 ip)
+static bool ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh,
+                                     const char *mac, __be32 ip)
 {
        /* You may be puzzled as to how this code works.
         * Some tricks were used, refer to
@@ -25,7 +25,7 @@ static int ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh,
        const struct ebt_mac_wormhash_tuple *p;
        int start, limit, i;
        uint32_t cmp[2] = { 0, 0 };
-       int key = (const unsigned char) mac[5];
+       int key = ((const unsigned char *)mac)[5];
 
        memcpy(((char *) cmp) + 2, mac, 6);
        start = wh->table[key];
@@ -33,23 +33,19 @@ static int ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh,
        if (ip) {
                for (i = start; i < limit; i++) {
                        p = &wh->pool[i];
-                       if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0]) {
-                               if (p->ip == 0 || p->ip == ip) {
-                                       return 1;
-                               }
-                       }
+                       if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
+                               if (p->ip == 0 || p->ip == ip)
+                                       return true;
                }
        } else {
                for (i = start; i < limit; i++) {
                        p = &wh->pool[i];
-                       if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0]) {
-                               if (p->ip == 0) {
-                                       return 1;
-                               }
-                       }
+                       if (cmp[1] == p->cmp[1] && cmp[0] == p->cmp[0])
+                               if (p->ip == 0)
+                                       return true;
                }
        }
-       return 0;
+       return false;
 }
 
 static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash
@@ -73,15 +69,18 @@ static int ebt_mac_wormhash_check_integrity(const struct ebt_mac_wormhash
 static int get_ip_dst(const struct sk_buff *skb, __be32 *addr)
 {
        if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
-               struct iphdr _iph, *ih;
+               const struct iphdr *ih;
+               struct iphdr _iph;
 
                ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
                if (ih == NULL)
                        return -1;
                *addr = ih->daddr;
        } else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
-               struct arphdr _arph, *ah;
-               __be32 buf, *bp;
+               const struct arphdr *ah;
+               struct arphdr _arph;
+               const __be32 *bp;
+               __be32 buf;
 
                ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
                if (ah == NULL ||
@@ -101,15 +100,18 @@ static int get_ip_dst(const struct sk_buff *skb, __be32 *addr)
 static int get_ip_src(const struct sk_buff *skb, __be32 *addr)
 {
        if (eth_hdr(skb)->h_proto == htons(ETH_P_IP)) {
-               struct iphdr _iph, *ih;
+               const struct iphdr *ih;
+               struct iphdr _iph;
 
                ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
                if (ih == NULL)
                        return -1;
                *addr = ih->saddr;
        } else if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
-               struct arphdr _arph, *ah;
-               __be32 buf, *bp;
+               const struct arphdr *ah;
+               struct arphdr _arph;
+               const __be32 *bp;
+               __be32 buf;
 
                ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
                if (ah == NULL ||
@@ -125,12 +127,10 @@ static int get_ip_src(const struct sk_buff *skb, __be32 *addr)
        return 0;
 }
 
-static int ebt_filter_among(const struct sk_buff *skb,
-                           const struct net_device *in,
-                           const struct net_device *out, const void *data,
-                           unsigned int datalen)
+static bool
+ebt_among_mt(const struct sk_buff *skb, const struct xt_match_param *par)
 {
-       struct ebt_among_info *info = (struct ebt_among_info *) data;
+       const struct ebt_among_info *info = par->matchinfo;
        const char *dmac, *smac;
        const struct ebt_mac_wormhash *wh_dst, *wh_src;
        __be32 dip = 0, sip = 0;
@@ -141,41 +141,41 @@ static int ebt_filter_among(const struct sk_buff *skb,
        if (wh_src) {
                smac = eth_hdr(skb)->h_source;
                if (get_ip_src(skb, &sip))
-                       return EBT_NOMATCH;
+                       return false;
                if (!(info->bitmask & EBT_AMONG_SRC_NEG)) {
                        /* we match only if it contains */
                        if (!ebt_mac_wormhash_contains(wh_src, smac, sip))
-                               return EBT_NOMATCH;
+                               return false;
                } else {
                        /* we match only if it DOES NOT contain */
                        if (ebt_mac_wormhash_contains(wh_src, smac, sip))
-                               return EBT_NOMATCH;
+                               return false;
                }
        }
 
        if (wh_dst) {
                dmac = eth_hdr(skb)->h_dest;
                if (get_ip_dst(skb, &dip))
-                       return EBT_NOMATCH;
+                       return false;
                if (!(info->bitmask & EBT_AMONG_DST_NEG)) {
                        /* we match only if it contains */
                        if (!ebt_mac_wormhash_contains(wh_dst, dmac, dip))
-                               return EBT_NOMATCH;
+                               return false;
                } else {
                        /* we match only if it DOES NOT contain */
                        if (ebt_mac_wormhash_contains(wh_dst, dmac, dip))
-                               return EBT_NOMATCH;
+                               return false;
                }
        }
 
-       return EBT_MATCH;
+       return true;
 }
 
-static int ebt_among_check(const char *tablename, unsigned int hookmask,
-                          const struct ebt_entry *e, void *data,
-                          unsigned int datalen)
+static bool ebt_among_mt_check(const struct xt_mtchk_param *par)
 {
-       struct ebt_among_info *info = (struct ebt_among_info *) data;
+       const struct ebt_among_info *info = par->matchinfo;
+       const struct ebt_entry_match *em =
+               container_of(par->matchinfo, const struct ebt_entry_match, data);
        int expected_length = sizeof(struct ebt_among_info);
        const struct ebt_mac_wormhash *wh_dst, *wh_src;
        int err;
@@ -185,44 +185,48 @@ static int ebt_among_check(const char *tablename, unsigned int hookmask,
        expected_length += ebt_mac_wormhash_size(wh_dst);
        expected_length += ebt_mac_wormhash_size(wh_src);
 
-       if (datalen != EBT_ALIGN(expected_length)) {
+       if (em->match_size != EBT_ALIGN(expected_length)) {
                printk(KERN_WARNING
-                      "ebtables: among: wrong size: %d"
+                      "ebtables: among: wrong size: %d "
                       "against expected %d, rounded to %Zd\n",
-                      datalen, expected_length,
+                      em->match_size, expected_length,
                       EBT_ALIGN(expected_length));
-               return -EINVAL;
+               return false;
        }
        if (wh_dst && (err = ebt_mac_wormhash_check_integrity(wh_dst))) {
                printk(KERN_WARNING
                       "ebtables: among: dst integrity fail: %x\n", -err);
-               return -EINVAL;
+               return false;
        }
        if (wh_src && (err = ebt_mac_wormhash_check_integrity(wh_src))) {
                printk(KERN_WARNING
                       "ebtables: among: src integrity fail: %x\n", -err);
-               return -EINVAL;
+               return false;
        }
-       return 0;
+       return true;
 }
 
-static struct ebt_match filter_among = {
-       .name           = EBT_AMONG_MATCH,
-       .match          = ebt_filter_among,
-       .check          = ebt_among_check,
+static struct xt_match ebt_among_mt_reg __read_mostly = {
+       .name           = "among",
+       .revision       = 0,
+       .family         = NFPROTO_BRIDGE,
+       .match          = ebt_among_mt,
+       .checkentry     = ebt_among_mt_check,
+       .matchsize      = -1, /* special case */
        .me             = THIS_MODULE,
 };
 
 static int __init ebt_among_init(void)
 {
-       return ebt_register_match(&filter_among);
+       return xt_register_match(&ebt_among_mt_reg);
 }
 
 static void __exit ebt_among_fini(void)
 {
-       ebt_unregister_match(&filter_among);
+       xt_unregister_match(&ebt_among_mt_reg);
 }
 
 module_init(ebt_among_init);
 module_exit(ebt_among_fini);
+MODULE_DESCRIPTION("Ebtables: Combined MAC/IP address list matching");
 MODULE_LICENSE("GPL");