vlan: Move vlan_set_encap_proto() to vlan header file
[GitHub/mt8127/android_kernel_alcatel_ttab.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 24
6faee60a
FW
25union pkthdr {
26 struct {
27 __be16 src;
28 __be16 dst;
29 } tcpudphdr;
30 struct {
31 u8 type;
32 u8 code;
33 } icmphdr;
93f65158
KT
34};
35
2d06d4a5 36static bool
62fc8051 37ebt_ip6_mt(const struct sk_buff *skb, struct xt_action_param *par)
93f65158 38{
f7108a20 39 const struct ebt_ip6_info *info = par->matchinfo;
93f65158
KT
40 const struct ipv6hdr *ih6;
41 struct ipv6hdr _ip6h;
6faee60a
FW
42 const union pkthdr *pptr;
43 union pkthdr _pkthdr;
93f65158
KT
44
45 ih6 = skb_header_pointer(skb, 0, sizeof(_ip6h), &_ip6h);
46 if (ih6 == NULL)
8cc784ee 47 return false;
93f65158
KT
48 if (info->bitmask & EBT_IP6_TCLASS &&
49 FWINV(info->tclass != ipv6_get_dsfield(ih6), EBT_IP6_TCLASS))
8cc784ee 50 return false;
0898f99a
YH
51 if (FWINV(ipv6_masked_addr_cmp(&ih6->saddr, &info->smsk,
52 &info->saddr), EBT_IP6_SOURCE) ||
53 FWINV(ipv6_masked_addr_cmp(&ih6->daddr, &info->dmsk,
54 &info->daddr), EBT_IP6_DEST))
8cc784ee 55 return false;
93f65158
KT
56 if (info->bitmask & EBT_IP6_PROTO) {
57 uint8_t nexthdr = ih6->nexthdr;
58 int offset_ph;
59
60 offset_ph = ipv6_skip_exthdr(skb, sizeof(_ip6h), &nexthdr);
61 if (offset_ph == -1)
8cc784ee 62 return false;
93f65158 63 if (FWINV(info->protocol != nexthdr, EBT_IP6_PROTO))
8cc784ee 64 return false;
6faee60a
FW
65 if (!(info->bitmask & ( EBT_IP6_DPORT |
66 EBT_IP6_SPORT | EBT_IP6_ICMP6)))
8cc784ee 67 return true;
6faee60a
FW
68
69 /* min icmpv6 headersize is 4, so sizeof(_pkthdr) is ok. */
70 pptr = skb_header_pointer(skb, offset_ph, sizeof(_pkthdr),
71 &_pkthdr);
93f65158 72 if (pptr == NULL)
8cc784ee 73 return false;
93f65158 74 if (info->bitmask & EBT_IP6_DPORT) {
6faee60a 75 u16 dst = ntohs(pptr->tcpudphdr.dst);
93f65158
KT
76 if (FWINV(dst < info->dport[0] ||
77 dst > info->dport[1], EBT_IP6_DPORT))
8cc784ee 78 return false;
93f65158
KT
79 }
80 if (info->bitmask & EBT_IP6_SPORT) {
6faee60a 81 u16 src = ntohs(pptr->tcpudphdr.src);
93f65158
KT
82 if (FWINV(src < info->sport[0] ||
83 src > info->sport[1], EBT_IP6_SPORT))
8cc784ee 84 return false;
93f65158 85 }
6faee60a
FW
86 if ((info->bitmask & EBT_IP6_ICMP6) &&
87 FWINV(pptr->icmphdr.type < info->icmpv6_type[0] ||
88 pptr->icmphdr.type > info->icmpv6_type[1] ||
89 pptr->icmphdr.code < info->icmpv6_code[0] ||
90 pptr->icmphdr.code > info->icmpv6_code[1],
91 EBT_IP6_ICMP6))
92 return false;
93f65158 93 }
8cc784ee 94 return true;
93f65158
KT
95}
96
b0f38452 97static int ebt_ip6_mt_check(const struct xt_mtchk_param *par)
93f65158 98{
9b4fce7a
JE
99 const struct ebt_entry *e = par->entryinfo;
100 struct ebt_ip6_info *info = par->matchinfo;
93f65158 101
93f65158 102 if (e->ethproto != htons(ETH_P_IPV6) || e->invflags & EBT_IPROTO)
bd414ee6 103 return -EINVAL;
93f65158 104 if (info->bitmask & ~EBT_IP6_MASK || info->invflags & ~EBT_IP6_MASK)
bd414ee6 105 return -EINVAL;
93f65158
KT
106 if (info->bitmask & (EBT_IP6_DPORT | EBT_IP6_SPORT)) {
107 if (info->invflags & EBT_IP6_PROTO)
bd414ee6 108 return -EINVAL;
93f65158
KT
109 if (info->protocol != IPPROTO_TCP &&
110 info->protocol != IPPROTO_UDP &&
111 info->protocol != IPPROTO_UDPLITE &&
112 info->protocol != IPPROTO_SCTP &&
113 info->protocol != IPPROTO_DCCP)
bd414ee6 114 return -EINVAL;
93f65158
KT
115 }
116 if (info->bitmask & EBT_IP6_DPORT && info->dport[0] > info->dport[1])
bd414ee6 117 return -EINVAL;
93f65158 118 if (info->bitmask & EBT_IP6_SPORT && info->sport[0] > info->sport[1])
bd414ee6 119 return -EINVAL;
6faee60a
FW
120 if (info->bitmask & EBT_IP6_ICMP6) {
121 if ((info->invflags & EBT_IP6_PROTO) ||
122 info->protocol != IPPROTO_ICMPV6)
123 return -EINVAL;
124 if (info->icmpv6_type[0] > info->icmpv6_type[1] ||
125 info->icmpv6_code[0] > info->icmpv6_code[1])
126 return -EINVAL;
127 }
bd414ee6 128 return 0;
93f65158
KT
129}
130
043ef46c
JE
131static struct xt_match ebt_ip6_mt_reg __read_mostly = {
132 .name = "ip6",
001a18d3
JE
133 .revision = 0,
134 .family = NFPROTO_BRIDGE,
2d06d4a5
JE
135 .match = ebt_ip6_mt,
136 .checkentry = ebt_ip6_mt_check,
fc0e3df4 137 .matchsize = sizeof(struct ebt_ip6_info),
93f65158
KT
138 .me = THIS_MODULE,
139};
140
141static int __init ebt_ip6_init(void)
142{
043ef46c 143 return xt_register_match(&ebt_ip6_mt_reg);
93f65158
KT
144}
145
146static void __exit ebt_ip6_fini(void)
147{
043ef46c 148 xt_unregister_match(&ebt_ip6_mt_reg);
93f65158
KT
149}
150
151module_init(ebt_ip6_init);
152module_exit(ebt_ip6_fini);
153MODULE_DESCRIPTION("Ebtables: IPv6 protocol packet match");
8244f4ba 154MODULE_AUTHOR("Kuo-Lang Tseng <kuo-lang.tseng@intel.com>");
93f65158 155MODULE_LICENSE("GPL");