]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/bridge/netfilter/ebt_ip6.c
netfilter: xtables: change matches to return error code
[net-next-2.6.git] / net / bridge / netfilter / ebt_ip6.c
CommitLineData
93f65158
KT
1/*
2 * ebt_ip6
3 *
4 * Authors:
5 * Manohar Castelino <manohar.r.castelino@intel.com>
6 * Kuo-Lang Tseng <kuo-lang.tseng@intel.com>
408ffaa4 7 * Jan Engelhardt <jengelh@medozas.de>
93f65158
KT
8 *
9 * Summary:
10 * This is just a modification of the IPv4 code written by
11 * Bart De Schuymer <bdschuym@pandora.be>
12 * with the changes required to support IPv6
13 *
14 * Jan, 2008
15 */
93f65158
KT
16#include <linux/ipv6.h>
17#include <net/ipv6.h>
18#include <linux/in.h>
19#include <linux/module.h>
20#include <net/dsfield.h>
18219d3f
JE
21#include <linux/netfilter/x_tables.h>
22#include <linux/netfilter_bridge/ebtables.h>
23#include <linux/netfilter_bridge/ebt_ip6.h>
93f65158
KT
24
25struct tcpudphdr {
26 __be16 src;
27 __be16 dst;
28};
29
2d06d4a5 30static bool
f7108a20 31ebt_ip6_mt(const struct sk_buff *skb, const struct xt_match_param *par)
93f65158 32{
f7108a20 33 const struct ebt_ip6_info *info = par->matchinfo;
93f65158
KT
34 const struct ipv6hdr *ih6;
35 struct ipv6hdr _ip6h;
36 const struct tcpudphdr *pptr;
37 struct tcpudphdr _ports;
93f65158
KT
38
39 ih6 = skb_header_pointer(skb, 0, sizeof(_ip6h), &_ip6h);
40 if (ih6 == NULL)
8cc784ee 41 return false;
93f65158
KT
42 if (info->bitmask & EBT_IP6_TCLASS &&
43 FWINV(info->tclass != ipv6_get_dsfield(ih6), EBT_IP6_TCLASS))
8cc784ee 44 return false;
0898f99a
YH
45 if (FWINV(ipv6_masked_addr_cmp(&ih6->saddr, &info->smsk,
46 &info->saddr), EBT_IP6_SOURCE) ||
47 FWINV(ipv6_masked_addr_cmp(&ih6->daddr, &info->dmsk,
48 &info->daddr), EBT_IP6_DEST))
8cc784ee 49 return false;
93f65158
KT
50 if (info->bitmask & EBT_IP6_PROTO) {
51 uint8_t nexthdr = ih6->nexthdr;
52 int offset_ph;
53
54 offset_ph = ipv6_skip_exthdr(skb, sizeof(_ip6h), &nexthdr);
55 if (offset_ph == -1)
8cc784ee 56 return false;
93f65158 57 if (FWINV(info->protocol != nexthdr, EBT_IP6_PROTO))
8cc784ee 58 return false;
93f65158
KT
59 if (!(info->bitmask & EBT_IP6_DPORT) &&
60 !(info->bitmask & EBT_IP6_SPORT))
8cc784ee 61 return true;
93f65158
KT
62 pptr = skb_header_pointer(skb, offset_ph, sizeof(_ports),
63 &_ports);
64 if (pptr == NULL)
8cc784ee 65 return false;
93f65158
KT
66 if (info->bitmask & EBT_IP6_DPORT) {
67 u32 dst = ntohs(pptr->dst);
68 if (FWINV(dst < info->dport[0] ||
69 dst > info->dport[1], EBT_IP6_DPORT))
8cc784ee 70 return false;
93f65158
KT
71 }
72 if (info->bitmask & EBT_IP6_SPORT) {
73 u32 src = ntohs(pptr->src);
74 if (FWINV(src < info->sport[0] ||
75 src > info->sport[1], EBT_IP6_SPORT))
8cc784ee 76 return false;
93f65158 77 }
8cc784ee 78 return true;
93f65158 79 }
8cc784ee 80 return true;
93f65158
KT
81}
82
b0f38452 83static int ebt_ip6_mt_check(const struct xt_mtchk_param *par)
93f65158 84{
9b4fce7a
JE
85 const struct ebt_entry *e = par->entryinfo;
86 struct ebt_ip6_info *info = par->matchinfo;
93f65158 87
93f65158 88 if (e->ethproto != htons(ETH_P_IPV6) || e->invflags & EBT_IPROTO)
bd414ee6 89 return -EINVAL;
93f65158 90 if (info->bitmask & ~EBT_IP6_MASK || info->invflags & ~EBT_IP6_MASK)
bd414ee6 91 return -EINVAL;
93f65158
KT
92 if (info->bitmask & (EBT_IP6_DPORT | EBT_IP6_SPORT)) {
93 if (info->invflags & EBT_IP6_PROTO)
bd414ee6 94 return -EINVAL;
93f65158
KT
95 if (info->protocol != IPPROTO_TCP &&
96 info->protocol != IPPROTO_UDP &&
97 info->protocol != IPPROTO_UDPLITE &&
98 info->protocol != IPPROTO_SCTP &&
99 info->protocol != IPPROTO_DCCP)
bd414ee6 100 return -EINVAL;
93f65158
KT
101 }
102 if (info->bitmask & EBT_IP6_DPORT && info->dport[0] > info->dport[1])
bd414ee6 103 return -EINVAL;
93f65158 104 if (info->bitmask & EBT_IP6_SPORT && info->sport[0] > info->sport[1])
bd414ee6
JE
105 return -EINVAL;
106 return 0;
93f65158
KT
107}
108
043ef46c
JE
109static struct xt_match ebt_ip6_mt_reg __read_mostly = {
110 .name = "ip6",
001a18d3
JE
111 .revision = 0,
112 .family = NFPROTO_BRIDGE,
2d06d4a5
JE
113 .match = ebt_ip6_mt,
114 .checkentry = ebt_ip6_mt_check,
fc0e3df4 115 .matchsize = sizeof(struct ebt_ip6_info),
93f65158
KT
116 .me = THIS_MODULE,
117};
118
119static int __init ebt_ip6_init(void)
120{
043ef46c 121 return xt_register_match(&ebt_ip6_mt_reg);
93f65158
KT
122}
123
124static void __exit ebt_ip6_fini(void)
125{
043ef46c 126 xt_unregister_match(&ebt_ip6_mt_reg);
93f65158
KT
127}
128
129module_init(ebt_ip6_init);
130module_exit(ebt_ip6_fini);
131MODULE_DESCRIPTION("Ebtables: IPv6 protocol packet match");
8244f4ba 132MODULE_AUTHOR("Kuo-Lang Tseng <kuo-lang.tseng@intel.com>");
93f65158 133MODULE_LICENSE("GPL");