63e3888d20cf676510526908d78491a387b47df1
[linux-2.6.git] / net / bridge / netfilter / ebt_ip6.c
1 /*
2  *  ebt_ip6
3  *
4  *      Authors:
5  *      Manohar Castelino <manohar.r.castelino@intel.com>
6  *      Kuo-Lang Tseng <kuo-lang.tseng@intel.com>
7  *      Jan Engelhardt <jengelh@medozas.de>
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  */
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>
21 #include <linux/netfilter/x_tables.h>
22 #include <linux/netfilter_bridge/ebtables.h>
23 #include <linux/netfilter_bridge/ebt_ip6.h>
24
25 struct tcpudphdr {
26         __be16 src;
27         __be16 dst;
28 };
29
30 static bool
31 ebt_ip6_mt(const struct sk_buff *skb, const struct xt_match_param *par)
32 {
33         const struct ebt_ip6_info *info = par->matchinfo;
34         const struct ipv6hdr *ih6;
35         struct ipv6hdr _ip6h;
36         const struct tcpudphdr *pptr;
37         struct tcpudphdr _ports;
38
39         ih6 = skb_header_pointer(skb, 0, sizeof(_ip6h), &_ip6h);
40         if (ih6 == NULL)
41                 return false;
42         if (info->bitmask & EBT_IP6_TCLASS &&
43            FWINV(info->tclass != ipv6_get_dsfield(ih6), EBT_IP6_TCLASS))
44                 return false;
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))
49                 return false;
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)
56                         return false;
57                 if (FWINV(info->protocol != nexthdr, EBT_IP6_PROTO))
58                         return false;
59                 if (!(info->bitmask & EBT_IP6_DPORT) &&
60                     !(info->bitmask & EBT_IP6_SPORT))
61                         return true;
62                 pptr = skb_header_pointer(skb, offset_ph, sizeof(_ports),
63                                           &_ports);
64                 if (pptr == NULL)
65                         return false;
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))
70                                 return false;
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))
76                         return false;
77                 }
78                 return true;
79         }
80         return true;
81 }
82
83 static bool ebt_ip6_mt_check(const struct xt_mtchk_param *par)
84 {
85         const struct ebt_entry *e = par->entryinfo;
86         struct ebt_ip6_info *info = par->matchinfo;
87
88         if (e->ethproto != htons(ETH_P_IPV6) || e->invflags & EBT_IPROTO)
89                 return false;
90         if (info->bitmask & ~EBT_IP6_MASK || info->invflags & ~EBT_IP6_MASK)
91                 return false;
92         if (info->bitmask & (EBT_IP6_DPORT | EBT_IP6_SPORT)) {
93                 if (info->invflags & EBT_IP6_PROTO)
94                         return false;
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)
100                         return false;
101         }
102         if (info->bitmask & EBT_IP6_DPORT && info->dport[0] > info->dport[1])
103                 return false;
104         if (info->bitmask & EBT_IP6_SPORT && info->sport[0] > info->sport[1])
105                 return false;
106         return true;
107 }
108
109 static struct xt_match ebt_ip6_mt_reg __read_mostly = {
110         .name           = "ip6",
111         .revision       = 0,
112         .family         = NFPROTO_BRIDGE,
113         .match          = ebt_ip6_mt,
114         .checkentry     = ebt_ip6_mt_check,
115         .matchsize      = sizeof(struct ebt_ip6_info),
116         .me             = THIS_MODULE,
117 };
118
119 static int __init ebt_ip6_init(void)
120 {
121         return xt_register_match(&ebt_ip6_mt_reg);
122 }
123
124 static void __exit ebt_ip6_fini(void)
125 {
126         xt_unregister_match(&ebt_ip6_mt_reg);
127 }
128
129 module_init(ebt_ip6_init);
130 module_exit(ebt_ip6_fini);
131 MODULE_DESCRIPTION("Ebtables: IPv6 protocol packet match");
132 MODULE_AUTHOR("Kuo-Lang Tseng <kuo-lang.tseng@intel.com>");
133 MODULE_LICENSE("GPL");