blob: bbf2534ef026e37cb604394dd9af48f7b7e82cd9 [file] [log] [blame]
Kuo-lang Tseng93f65152008-06-09 15:55:45 -07001/*
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@computergmbh.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 */
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070016#include <linux/ipv6.h>
17#include <net/ipv6.h>
18#include <linux/in.h>
19#include <linux/module.h>
20#include <net/dsfield.h>
Jan Engelhardt18219d32008-10-08 11:35:13 +020021#include <linux/netfilter/x_tables.h>
22#include <linux/netfilter_bridge/ebtables.h>
23#include <linux/netfilter_bridge/ebt_ip6.h>
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070024
25struct tcpudphdr {
26 __be16 src;
27 __be16 dst;
28};
29
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +020030static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +020031ebt_ip6_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070032{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020033 const struct ebt_ip6_info *info = par->matchinfo;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070034 const struct ipv6hdr *ih6;
35 struct ipv6hdr _ip6h;
36 const struct tcpudphdr *pptr;
37 struct tcpudphdr _ports;
38 struct in6_addr tmp_addr;
39 int i;
40
41 ih6 = skb_header_pointer(skb, 0, sizeof(_ip6h), &_ip6h);
42 if (ih6 == NULL)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020043 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070044 if (info->bitmask & EBT_IP6_TCLASS &&
45 FWINV(info->tclass != ipv6_get_dsfield(ih6), EBT_IP6_TCLASS))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020046 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070047 for (i = 0; i < 4; i++)
48 tmp_addr.in6_u.u6_addr32[i] = ih6->saddr.in6_u.u6_addr32[i] &
49 info->smsk.in6_u.u6_addr32[i];
50 if (info->bitmask & EBT_IP6_SOURCE &&
51 FWINV((ipv6_addr_cmp(&tmp_addr, &info->saddr) != 0),
52 EBT_IP6_SOURCE))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020053 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070054 for (i = 0; i < 4; i++)
55 tmp_addr.in6_u.u6_addr32[i] = ih6->daddr.in6_u.u6_addr32[i] &
56 info->dmsk.in6_u.u6_addr32[i];
57 if (info->bitmask & EBT_IP6_DEST &&
58 FWINV((ipv6_addr_cmp(&tmp_addr, &info->daddr) != 0), EBT_IP6_DEST))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020059 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070060 if (info->bitmask & EBT_IP6_PROTO) {
61 uint8_t nexthdr = ih6->nexthdr;
62 int offset_ph;
63
64 offset_ph = ipv6_skip_exthdr(skb, sizeof(_ip6h), &nexthdr);
65 if (offset_ph == -1)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020066 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070067 if (FWINV(info->protocol != nexthdr, EBT_IP6_PROTO))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020068 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070069 if (!(info->bitmask & EBT_IP6_DPORT) &&
70 !(info->bitmask & EBT_IP6_SPORT))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020071 return true;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070072 pptr = skb_header_pointer(skb, offset_ph, sizeof(_ports),
73 &_ports);
74 if (pptr == NULL)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020075 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070076 if (info->bitmask & EBT_IP6_DPORT) {
77 u32 dst = ntohs(pptr->dst);
78 if (FWINV(dst < info->dport[0] ||
79 dst > info->dport[1], EBT_IP6_DPORT))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020080 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070081 }
82 if (info->bitmask & EBT_IP6_SPORT) {
83 u32 src = ntohs(pptr->src);
84 if (FWINV(src < info->sport[0] ||
85 src > info->sport[1], EBT_IP6_SPORT))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020086 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070087 }
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020088 return true;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070089 }
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020090 return true;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070091}
92
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020093static bool ebt_ip6_mt_check(const struct xt_mtchk_param *par)
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070094{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020095 const struct ebt_entry *e = par->entryinfo;
96 struct ebt_ip6_info *info = par->matchinfo;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070097
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070098 if (e->ethproto != htons(ETH_P_IPV6) || e->invflags & EBT_IPROTO)
Jan Engelhardt19eda872008-10-08 11:35:13 +020099 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700100 if (info->bitmask & ~EBT_IP6_MASK || info->invflags & ~EBT_IP6_MASK)
Jan Engelhardt19eda872008-10-08 11:35:13 +0200101 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700102 if (info->bitmask & (EBT_IP6_DPORT | EBT_IP6_SPORT)) {
103 if (info->invflags & EBT_IP6_PROTO)
Jan Engelhardt19eda872008-10-08 11:35:13 +0200104 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700105 if (info->protocol != IPPROTO_TCP &&
106 info->protocol != IPPROTO_UDP &&
107 info->protocol != IPPROTO_UDPLITE &&
108 info->protocol != IPPROTO_SCTP &&
109 info->protocol != IPPROTO_DCCP)
Jan Engelhardt19eda872008-10-08 11:35:13 +0200110 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700111 }
112 if (info->bitmask & EBT_IP6_DPORT && info->dport[0] > info->dport[1])
Jan Engelhardt19eda872008-10-08 11:35:13 +0200113 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700114 if (info->bitmask & EBT_IP6_SPORT && info->sport[0] > info->sport[1])
Jan Engelhardt19eda872008-10-08 11:35:13 +0200115 return false;
116 return true;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700117}
118
Jan Engelhardt043ef462008-10-08 11:35:15 +0200119static struct xt_match ebt_ip6_mt_reg __read_mostly = {
120 .name = "ip6",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200121 .revision = 0,
122 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200123 .match = ebt_ip6_mt,
124 .checkentry = ebt_ip6_mt_check,
Florian Westphalfc0e3df2010-02-15 18:16:26 +0100125 .matchsize = sizeof(struct ebt_ip6_info),
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700126 .me = THIS_MODULE,
127};
128
129static int __init ebt_ip6_init(void)
130{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200131 return xt_register_match(&ebt_ip6_mt_reg);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700132}
133
134static void __exit ebt_ip6_fini(void)
135{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200136 xt_unregister_match(&ebt_ip6_mt_reg);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700137}
138
139module_init(ebt_ip6_init);
140module_exit(ebt_ip6_fini);
141MODULE_DESCRIPTION("Ebtables: IPv6 protocol packet match");
142MODULE_LICENSE("GPL");