netfilter: xtables: move extension arguments into compound structure (1/6)
[deliverable/linux.git] / net / bridge / netfilter / ebt_arp.c
CommitLineData
1da177e4
LT
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 */
1da177e4
LT
11#include <linux/if_arp.h>
12#include <linux/if_ether.h>
13#include <linux/module.h>
18219d3f
JE
14#include <linux/netfilter/x_tables.h>
15#include <linux/netfilter_bridge/ebtables.h>
16#include <linux/netfilter_bridge/ebt_arp.h>
1da177e4 17
2d06d4a5 18static bool
f7108a20 19ebt_arp_mt(const struct sk_buff *skb, const struct xt_match_param *par)
1da177e4 20{
f7108a20 21 const struct ebt_arp_info *info = par->matchinfo;
abfdf1c4
JE
22 const struct arphdr *ah;
23 struct arphdr _arph;
1da177e4
LT
24
25 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
26 if (ah == NULL)
8cc784ee 27 return false;
1da177e4
LT
28 if (info->bitmask & EBT_ARP_OPCODE && FWINV(info->opcode !=
29 ah->ar_op, EBT_ARP_OPCODE))
8cc784ee 30 return false;
1da177e4
LT
31 if (info->bitmask & EBT_ARP_HTYPE && FWINV(info->htype !=
32 ah->ar_hrd, EBT_ARP_HTYPE))
8cc784ee 33 return false;
1da177e4
LT
34 if (info->bitmask & EBT_ARP_PTYPE && FWINV(info->ptype !=
35 ah->ar_pro, EBT_ARP_PTYPE))
8cc784ee 36 return false;
1da177e4 37
e011ff48 38 if (info->bitmask & (EBT_ARP_SRC_IP | EBT_ARP_DST_IP | EBT_ARP_GRAT)) {
abfdf1c4
JE
39 const __be32 *sap, *dap;
40 __be32 saddr, daddr;
1da177e4 41
c15bf6e6 42 if (ah->ar_pln != sizeof(__be32) || ah->ar_pro != htons(ETH_P_IP))
8cc784ee 43 return false;
c15bf6e6
BDS
44 sap = skb_header_pointer(skb, sizeof(struct arphdr) +
45 ah->ar_hln, sizeof(saddr),
46 &saddr);
47 if (sap == NULL)
8cc784ee 48 return false;
c15bf6e6
BDS
49 dap = skb_header_pointer(skb, sizeof(struct arphdr) +
50 2*ah->ar_hln+sizeof(saddr),
51 sizeof(daddr), &daddr);
52 if (dap == NULL)
8cc784ee 53 return false;
c15bf6e6
BDS
54 if (info->bitmask & EBT_ARP_SRC_IP &&
55 FWINV(info->saddr != (*sap & info->smsk), EBT_ARP_SRC_IP))
8cc784ee 56 return false;
c15bf6e6
BDS
57 if (info->bitmask & EBT_ARP_DST_IP &&
58 FWINV(info->daddr != (*dap & info->dmsk), EBT_ARP_DST_IP))
8cc784ee 59 return false;
c15bf6e6
BDS
60 if (info->bitmask & EBT_ARP_GRAT &&
61 FWINV(*dap != *sap, EBT_ARP_GRAT))
8cc784ee 62 return false;
1da177e4
LT
63 }
64
65 if (info->bitmask & (EBT_ARP_SRC_MAC | EBT_ARP_DST_MAC)) {
abfdf1c4
JE
66 const unsigned char *mp;
67 unsigned char _mac[ETH_ALEN];
1da177e4
LT
68 uint8_t verdict, i;
69
c15bf6e6 70 if (ah->ar_hln != ETH_ALEN || ah->ar_hrd != htons(ARPHRD_ETHER))
8cc784ee 71 return false;
1da177e4
LT
72 if (info->bitmask & EBT_ARP_SRC_MAC) {
73 mp = skb_header_pointer(skb, sizeof(struct arphdr),
74 sizeof(_mac), &_mac);
75 if (mp == NULL)
8cc784ee 76 return false;
1da177e4
LT
77 verdict = 0;
78 for (i = 0; i < 6; i++)
79 verdict |= (mp[i] ^ info->smaddr[i]) &
80 info->smmsk[i];
81 if (FWINV(verdict != 0, EBT_ARP_SRC_MAC))
8cc784ee 82 return false;
1da177e4
LT
83 }
84
85 if (info->bitmask & EBT_ARP_DST_MAC) {
86 mp = skb_header_pointer(skb, sizeof(struct arphdr) +
87 ah->ar_hln + ah->ar_pln,
88 sizeof(_mac), &_mac);
89 if (mp == NULL)
8cc784ee 90 return false;
1da177e4
LT
91 verdict = 0;
92 for (i = 0; i < 6; i++)
93 verdict |= (mp[i] ^ info->dmaddr[i]) &
94 info->dmmsk[i];
95 if (FWINV(verdict != 0, EBT_ARP_DST_MAC))
8cc784ee 96 return false;
1da177e4
LT
97 }
98 }
99
8cc784ee 100 return true;
1da177e4
LT
101}
102
2d06d4a5
JE
103static bool
104ebt_arp_mt_check(const char *table, const void *entry,
105 const struct xt_match *match, void *data,
106 unsigned int hook_mask)
1da177e4 107{
abfdf1c4 108 const struct ebt_arp_info *info = data;
2d06d4a5 109 const struct ebt_entry *e = entry;
1da177e4 110
1da177e4
LT
111 if ((e->ethproto != htons(ETH_P_ARP) &&
112 e->ethproto != htons(ETH_P_RARP)) ||
113 e->invflags & EBT_IPROTO)
19eda879 114 return false;
1da177e4 115 if (info->bitmask & ~EBT_ARP_MASK || info->invflags & ~EBT_ARP_MASK)
19eda879
JE
116 return false;
117 return true;
1da177e4
LT
118}
119
043ef46c
JE
120static struct xt_match ebt_arp_mt_reg __read_mostly = {
121 .name = "arp",
001a18d3
JE
122 .revision = 0,
123 .family = NFPROTO_BRIDGE,
2d06d4a5
JE
124 .match = ebt_arp_mt,
125 .checkentry = ebt_arp_mt_check,
18219d3f 126 .matchsize = XT_ALIGN(sizeof(struct ebt_arp_info)),
1da177e4
LT
127 .me = THIS_MODULE,
128};
129
65b4b4e8 130static int __init ebt_arp_init(void)
1da177e4 131{
043ef46c 132 return xt_register_match(&ebt_arp_mt_reg);
1da177e4
LT
133}
134
65b4b4e8 135static void __exit ebt_arp_fini(void)
1da177e4 136{
043ef46c 137 xt_unregister_match(&ebt_arp_mt_reg);
1da177e4
LT
138}
139
65b4b4e8
AM
140module_init(ebt_arp_init);
141module_exit(ebt_arp_fini);
f776c4cd 142MODULE_DESCRIPTION("Ebtables: ARP protocol packet match");
1da177e4 143MODULE_LICENSE("GPL");
This page took 0.456167 seconds and 5 git commands to generate.