1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
25struct tcpudphdr {
26 __be16 src;
27 __be16 dst;
28};
29
30static bool
31ebt_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 struct in6_addr tmp_addr;
39 int i;
40
41 ih6 = skb_header_pointer(skb, 0, sizeof(_ip6h), &_ip6h);
42 if (ih6 == NULL)
43 return false;
44 if (info->bitmask & EBT_IP6_TCLASS &&
45 FWINV(info->tclass != ipv6_get_dsfield(ih6), EBT_IP6_TCLASS))
46 return false;
47 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))
53 return false;
54 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))
59 return false;
60 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)
66 return false;
67 if (FWINV(info->protocol != nexthdr, EBT_IP6_PROTO))
68 return false;
69 if (!(info->bitmask & EBT_IP6_DPORT) &&
70 !(info->bitmask & EBT_IP6_SPORT))
71 return true;
72 pptr = skb_header_pointer(skb, offset_ph, sizeof(_ports),
73 &_ports);
74 if (pptr == NULL)
75 return false;
76 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))
80 return false;
81 }
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))
86 return false;
87 }
88 return true;
89 }
90 return true;
91}
92
93static bool ebt_ip6_mt_check(const struct xt_mtchk_param *par)
94{
95 const struct ebt_entry *e = par->entryinfo;
96 struct ebt_ip6_info *info = par->matchinfo;
97
98 if (e->ethproto != htons(ETH_P_IPV6) || e->invflags & EBT_IPROTO)
99 return false;
100 if (info->bitmask & ~EBT_IP6_MASK || info->invflags & ~EBT_IP6_MASK)
101 return false;
102 if (info->bitmask & (EBT_IP6_DPORT | EBT_IP6_SPORT)) {
103 if (info->invflags & EBT_IP6_PROTO)
104 return false;
105 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)
110 return false;
111 }
112 if (info->bitmask & EBT_IP6_DPORT && info->dport[0] > info->dport[1])
113 return false;
114 if (info->bitmask & EBT_IP6_SPORT && info->sport[0] > info->sport[1])
115 return false;
116 return true;
117}
118
119static struct xt_match ebt_ip6_mt_reg __read_mostly = {
120 .name = "ip6",
121 .revision = 0,
122 .family = NFPROTO_BRIDGE,
123 .match = ebt_ip6_mt,
124 .checkentry = ebt_ip6_mt_check,
125 .matchsize = XT_ALIGN(sizeof(struct ebt_ip6_info)),
126 .me = THIS_MODULE,
127};
128
129static int __init ebt_ip6_init(void)
130{
131 return xt_register_match(&ebt_ip6_mt_reg);
132}
133
134static void __exit ebt_ip6_fini(void)
135{
136 xt_unregister_match(&ebt_ip6_mt_reg);
137}
138
139module_init(ebt_ip6_init);
140module_exit(ebt_ip6_fini);
141MODULE_DESCRIPTION("Ebtables: IPv6 protocol packet match");
142MODULE_LICENSE("GPL");
143