]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/bridge/netfilter/ebt_arp.c
netfilter: change return types of match functions for ebtables extensions
[karo-tx-linux.git] / net / bridge / netfilter / ebt_arp.c
1 /*
2  *  ebt_arp
3  *
4  *      Authors:
5  *      Bart De Schuymer <bdschuym@pandora.be>
6  *      Tim Gardner <timg@tpi.com>
7  *
8  *  April, 2002
9  *
10  */
11 #include <linux/if_arp.h>
12 #include <linux/if_ether.h>
13 #include <linux/module.h>
14 #include <linux/netfilter/x_tables.h>
15 #include <linux/netfilter_bridge/ebtables.h>
16 #include <linux/netfilter_bridge/ebt_arp.h>
17
18 static bool ebt_filter_arp(const struct sk_buff *skb,
19    const struct net_device *in,
20    const struct net_device *out, const void *data, unsigned int datalen)
21 {
22         const struct ebt_arp_info *info = data;
23         const struct arphdr *ah;
24         struct arphdr _arph;
25
26         ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
27         if (ah == NULL)
28                 return false;
29         if (info->bitmask & EBT_ARP_OPCODE && FWINV(info->opcode !=
30            ah->ar_op, EBT_ARP_OPCODE))
31                 return false;
32         if (info->bitmask & EBT_ARP_HTYPE && FWINV(info->htype !=
33            ah->ar_hrd, EBT_ARP_HTYPE))
34                 return false;
35         if (info->bitmask & EBT_ARP_PTYPE && FWINV(info->ptype !=
36            ah->ar_pro, EBT_ARP_PTYPE))
37                 return false;
38
39         if (info->bitmask & (EBT_ARP_SRC_IP | EBT_ARP_DST_IP | EBT_ARP_GRAT)) {
40                 const __be32 *sap, *dap;
41                 __be32 saddr, daddr;
42
43                 if (ah->ar_pln != sizeof(__be32) || ah->ar_pro != htons(ETH_P_IP))
44                         return false;
45                 sap = skb_header_pointer(skb, sizeof(struct arphdr) +
46                                         ah->ar_hln, sizeof(saddr),
47                                         &saddr);
48                 if (sap == NULL)
49                         return false;
50                 dap = skb_header_pointer(skb, sizeof(struct arphdr) +
51                                         2*ah->ar_hln+sizeof(saddr),
52                                         sizeof(daddr), &daddr);
53                 if (dap == NULL)
54                         return false;
55                 if (info->bitmask & EBT_ARP_SRC_IP &&
56                     FWINV(info->saddr != (*sap & info->smsk), EBT_ARP_SRC_IP))
57                         return false;
58                 if (info->bitmask & EBT_ARP_DST_IP &&
59                     FWINV(info->daddr != (*dap & info->dmsk), EBT_ARP_DST_IP))
60                         return false;
61                 if (info->bitmask & EBT_ARP_GRAT &&
62                     FWINV(*dap != *sap, EBT_ARP_GRAT))
63                         return false;
64         }
65
66         if (info->bitmask & (EBT_ARP_SRC_MAC | EBT_ARP_DST_MAC)) {
67                 const unsigned char *mp;
68                 unsigned char _mac[ETH_ALEN];
69                 uint8_t verdict, i;
70
71                 if (ah->ar_hln != ETH_ALEN || ah->ar_hrd != htons(ARPHRD_ETHER))
72                         return false;
73                 if (info->bitmask & EBT_ARP_SRC_MAC) {
74                         mp = skb_header_pointer(skb, sizeof(struct arphdr),
75                                                 sizeof(_mac), &_mac);
76                         if (mp == NULL)
77                                 return false;
78                         verdict = 0;
79                         for (i = 0; i < 6; i++)
80                                 verdict |= (mp[i] ^ info->smaddr[i]) &
81                                        info->smmsk[i];
82                         if (FWINV(verdict != 0, EBT_ARP_SRC_MAC))
83                                 return false;
84                 }
85
86                 if (info->bitmask & EBT_ARP_DST_MAC) {
87                         mp = skb_header_pointer(skb, sizeof(struct arphdr) +
88                                                 ah->ar_hln + ah->ar_pln,
89                                                 sizeof(_mac), &_mac);
90                         if (mp == NULL)
91                                 return false;
92                         verdict = 0;
93                         for (i = 0; i < 6; i++)
94                                 verdict |= (mp[i] ^ info->dmaddr[i]) &
95                                         info->dmmsk[i];
96                         if (FWINV(verdict != 0, EBT_ARP_DST_MAC))
97                                 return false;
98                 }
99         }
100
101         return true;
102 }
103
104 static bool ebt_arp_check(const char *tablename, unsigned int hookmask,
105    const struct ebt_entry *e, void *data, unsigned int datalen)
106 {
107         const struct ebt_arp_info *info = data;
108
109         if ((e->ethproto != htons(ETH_P_ARP) &&
110            e->ethproto != htons(ETH_P_RARP)) ||
111            e->invflags & EBT_IPROTO)
112                 return false;
113         if (info->bitmask & ~EBT_ARP_MASK || info->invflags & ~EBT_ARP_MASK)
114                 return false;
115         return true;
116 }
117
118 static struct ebt_match filter_arp __read_mostly = {
119         .name           = EBT_ARP_MATCH,
120         .match          = ebt_filter_arp,
121         .check          = ebt_arp_check,
122         .matchsize      = XT_ALIGN(sizeof(struct ebt_arp_info)),
123         .me             = THIS_MODULE,
124 };
125
126 static int __init ebt_arp_init(void)
127 {
128         return ebt_register_match(&filter_arp);
129 }
130
131 static void __exit ebt_arp_fini(void)
132 {
133         ebt_unregister_match(&filter_arp);
134 }
135
136 module_init(ebt_arp_init);
137 module_exit(ebt_arp_fini);
138 MODULE_DESCRIPTION("Ebtables: ARP protocol packet match");
139 MODULE_LICENSE("GPL");