netfilter: xtables: move extension arguments into compound structure (1/6)
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / bridge / netfilter / ebt_ip.c
1 /*
2 * ebt_ip
3 *
4 * Authors:
5 * Bart De Schuymer <bdschuym@pandora.be>
6 *
7 * April, 2002
8 *
9 * Changes:
10 * added ip-sport and ip-dport
11 * Innominate Security Technologies AG <mhopf@innominate.com>
12 * September, 2002
13 */
14 #include <linux/ip.h>
15 #include <net/ip.h>
16 #include <linux/in.h>
17 #include <linux/module.h>
18 #include <linux/netfilter/x_tables.h>
19 #include <linux/netfilter_bridge/ebtables.h>
20 #include <linux/netfilter_bridge/ebt_ip.h>
21
22 struct tcpudphdr {
23 __be16 src;
24 __be16 dst;
25 };
26
27 static bool
28 ebt_ip_mt(const struct sk_buff *skb, const struct xt_match_param *par)
29 {
30 const struct ebt_ip_info *info = par->matchinfo;
31 const struct iphdr *ih;
32 struct iphdr _iph;
33 const struct tcpudphdr *pptr;
34 struct tcpudphdr _ports;
35
36 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
37 if (ih == NULL)
38 return false;
39 if (info->bitmask & EBT_IP_TOS &&
40 FWINV(info->tos != ih->tos, EBT_IP_TOS))
41 return false;
42 if (info->bitmask & EBT_IP_SOURCE &&
43 FWINV((ih->saddr & info->smsk) !=
44 info->saddr, EBT_IP_SOURCE))
45 return false;
46 if ((info->bitmask & EBT_IP_DEST) &&
47 FWINV((ih->daddr & info->dmsk) !=
48 info->daddr, EBT_IP_DEST))
49 return false;
50 if (info->bitmask & EBT_IP_PROTO) {
51 if (FWINV(info->protocol != ih->protocol, EBT_IP_PROTO))
52 return false;
53 if (!(info->bitmask & EBT_IP_DPORT) &&
54 !(info->bitmask & EBT_IP_SPORT))
55 return true;
56 if (ntohs(ih->frag_off) & IP_OFFSET)
57 return false;
58 pptr = skb_header_pointer(skb, ih->ihl*4,
59 sizeof(_ports), &_ports);
60 if (pptr == NULL)
61 return false;
62 if (info->bitmask & EBT_IP_DPORT) {
63 u32 dst = ntohs(pptr->dst);
64 if (FWINV(dst < info->dport[0] ||
65 dst > info->dport[1],
66 EBT_IP_DPORT))
67 return false;
68 }
69 if (info->bitmask & EBT_IP_SPORT) {
70 u32 src = ntohs(pptr->src);
71 if (FWINV(src < info->sport[0] ||
72 src > info->sport[1],
73 EBT_IP_SPORT))
74 return false;
75 }
76 }
77 return true;
78 }
79
80 static bool
81 ebt_ip_mt_check(const char *table, const void *entry,
82 const struct xt_match *match, void *data,
83 unsigned int hook_mask)
84 {
85 const struct ebt_ip_info *info = data;
86 const struct ebt_entry *e = entry;
87
88 if (e->ethproto != htons(ETH_P_IP) ||
89 e->invflags & EBT_IPROTO)
90 return false;
91 if (info->bitmask & ~EBT_IP_MASK || info->invflags & ~EBT_IP_MASK)
92 return false;
93 if (info->bitmask & (EBT_IP_DPORT | EBT_IP_SPORT)) {
94 if (info->invflags & EBT_IP_PROTO)
95 return false;
96 if (info->protocol != IPPROTO_TCP &&
97 info->protocol != IPPROTO_UDP &&
98 info->protocol != IPPROTO_UDPLITE &&
99 info->protocol != IPPROTO_SCTP &&
100 info->protocol != IPPROTO_DCCP)
101 return false;
102 }
103 if (info->bitmask & EBT_IP_DPORT && info->dport[0] > info->dport[1])
104 return false;
105 if (info->bitmask & EBT_IP_SPORT && info->sport[0] > info->sport[1])
106 return false;
107 return true;
108 }
109
110 static struct xt_match ebt_ip_mt_reg __read_mostly = {
111 .name = "ip",
112 .revision = 0,
113 .family = NFPROTO_BRIDGE,
114 .match = ebt_ip_mt,
115 .checkentry = ebt_ip_mt_check,
116 .matchsize = XT_ALIGN(sizeof(struct ebt_ip_info)),
117 .me = THIS_MODULE,
118 };
119
120 static int __init ebt_ip_init(void)
121 {
122 return xt_register_match(&ebt_ip_mt_reg);
123 }
124
125 static void __exit ebt_ip_fini(void)
126 {
127 xt_unregister_match(&ebt_ip_mt_reg);
128 }
129
130 module_init(ebt_ip_init);
131 module_exit(ebt_ip_fini);
132 MODULE_DESCRIPTION("Ebtables: IPv4 protocol packet match");
133 MODULE_LICENSE("GPL");