Merge tag 'linux-kselftest-5.3-rc1' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / net / bridge / netfilter / ebt_ip6.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
93f65158
KT
2/*
3 * ebt_ip6
4 *
5 * Authors:
6 * Manohar Castelino <manohar.r.castelino@intel.com>
7 * Kuo-Lang Tseng <kuo-lang.tseng@intel.com>
408ffaa4 8 * Jan Engelhardt <jengelh@medozas.de>
93f65158
KT
9 *
10 * Summary:
11 * This is just a modification of the IPv4 code written by
12 * Bart De Schuymer <bdschuym@pandora.be>
13 * with the changes required to support IPv6
14 *
15 * Jan, 2008
16 */
93f65158
KT
17#include <linux/ipv6.h>
18#include <net/ipv6.h>
19#include <linux/in.h>
20#include <linux/module.h>
21#include <net/dsfield.h>
18219d3f
JE
22#include <linux/netfilter/x_tables.h>
23#include <linux/netfilter_bridge/ebtables.h>
24#include <linux/netfilter_bridge/ebt_ip6.h>
93f65158 25
6faee60a
FW
26union pkthdr {
27 struct {
28 __be16 src;
29 __be16 dst;
30 } tcpudphdr;
31 struct {
32 u8 type;
33 u8 code;
34 } icmphdr;
93f65158
KT
35};
36
2d06d4a5 37static bool
62fc8051 38ebt_ip6_mt(const struct sk_buff *skb, struct xt_action_param *par)
93f65158 39{
f7108a20 40 const struct ebt_ip6_info *info = par->matchinfo;
93f65158
KT
41 const struct ipv6hdr *ih6;
42 struct ipv6hdr _ip6h;
6faee60a
FW
43 const union pkthdr *pptr;
44 union pkthdr _pkthdr;
93f65158
KT
45
46 ih6 = skb_header_pointer(skb, 0, sizeof(_ip6h), &_ip6h);
47 if (ih6 == NULL)
8cc784ee 48 return false;
c37a2dfa
JP
49 if ((info->bitmask & EBT_IP6_TCLASS) &&
50 NF_INVF(info, EBT_IP6_TCLASS,
51 info->tclass != ipv6_get_dsfield(ih6)))
8cc784ee 52 return false;
c37a2dfa
JP
53 if (((info->bitmask & EBT_IP6_SOURCE) &&
54 NF_INVF(info, EBT_IP6_SOURCE,
55 ipv6_masked_addr_cmp(&ih6->saddr, &info->smsk,
56 &info->saddr))) ||
57 ((info->bitmask & EBT_IP6_DEST) &&
58 NF_INVF(info, EBT_IP6_DEST,
59 ipv6_masked_addr_cmp(&ih6->daddr, &info->dmsk,
60 &info->daddr))))
8cc784ee 61 return false;
93f65158
KT
62 if (info->bitmask & EBT_IP6_PROTO) {
63 uint8_t nexthdr = ih6->nexthdr;
75f2811c 64 __be16 frag_off;
93f65158
KT
65 int offset_ph;
66
75f2811c 67 offset_ph = ipv6_skip_exthdr(skb, sizeof(_ip6h), &nexthdr, &frag_off);
93f65158 68 if (offset_ph == -1)
8cc784ee 69 return false;
c37a2dfa 70 if (NF_INVF(info, EBT_IP6_PROTO, info->protocol != nexthdr))
8cc784ee 71 return false;
c1bc1d25
IM
72 if (!(info->bitmask & (EBT_IP6_DPORT |
73 EBT_IP6_SPORT | EBT_IP6_ICMP6)))
8cc784ee 74 return true;
6faee60a
FW
75
76 /* min icmpv6 headersize is 4, so sizeof(_pkthdr) is ok. */
77 pptr = skb_header_pointer(skb, offset_ph, sizeof(_pkthdr),
78 &_pkthdr);
93f65158 79 if (pptr == NULL)
8cc784ee 80 return false;
93f65158 81 if (info->bitmask & EBT_IP6_DPORT) {
6faee60a 82 u16 dst = ntohs(pptr->tcpudphdr.dst);
c37a2dfa
JP
83 if (NF_INVF(info, EBT_IP6_DPORT,
84 dst < info->dport[0] ||
85 dst > info->dport[1]))
8cc784ee 86 return false;
93f65158
KT
87 }
88 if (info->bitmask & EBT_IP6_SPORT) {
6faee60a 89 u16 src = ntohs(pptr->tcpudphdr.src);
c37a2dfa
JP
90 if (NF_INVF(info, EBT_IP6_SPORT,
91 src < info->sport[0] ||
92 src > info->sport[1]))
c816c255 93 return false;
93f65158 94 }
6faee60a 95 if ((info->bitmask & EBT_IP6_ICMP6) &&
c37a2dfa
JP
96 NF_INVF(info, EBT_IP6_ICMP6,
97 pptr->icmphdr.type < info->icmpv6_type[0] ||
98 pptr->icmphdr.type > info->icmpv6_type[1] ||
99 pptr->icmphdr.code < info->icmpv6_code[0] ||
100 pptr->icmphdr.code > info->icmpv6_code[1]))
6faee60a 101 return false;
93f65158 102 }
8cc784ee 103 return true;
93f65158
KT
104}
105
b0f38452 106static int ebt_ip6_mt_check(const struct xt_mtchk_param *par)
93f65158 107{
9b4fce7a
JE
108 const struct ebt_entry *e = par->entryinfo;
109 struct ebt_ip6_info *info = par->matchinfo;
93f65158 110
93f65158 111 if (e->ethproto != htons(ETH_P_IPV6) || e->invflags & EBT_IPROTO)
bd414ee6 112 return -EINVAL;
93f65158 113 if (info->bitmask & ~EBT_IP6_MASK || info->invflags & ~EBT_IP6_MASK)
bd414ee6 114 return -EINVAL;
93f65158
KT
115 if (info->bitmask & (EBT_IP6_DPORT | EBT_IP6_SPORT)) {
116 if (info->invflags & EBT_IP6_PROTO)
bd414ee6 117 return -EINVAL;
93f65158
KT
118 if (info->protocol != IPPROTO_TCP &&
119 info->protocol != IPPROTO_UDP &&
120 info->protocol != IPPROTO_UDPLITE &&
121 info->protocol != IPPROTO_SCTP &&
122 info->protocol != IPPROTO_DCCP)
bd414ee6 123 return -EINVAL;
93f65158
KT
124 }
125 if (info->bitmask & EBT_IP6_DPORT && info->dport[0] > info->dport[1])
bd414ee6 126 return -EINVAL;
93f65158 127 if (info->bitmask & EBT_IP6_SPORT && info->sport[0] > info->sport[1])
bd414ee6 128 return -EINVAL;
6faee60a
FW
129 if (info->bitmask & EBT_IP6_ICMP6) {
130 if ((info->invflags & EBT_IP6_PROTO) ||
131 info->protocol != IPPROTO_ICMPV6)
132 return -EINVAL;
133 if (info->icmpv6_type[0] > info->icmpv6_type[1] ||
134 info->icmpv6_code[0] > info->icmpv6_code[1])
135 return -EINVAL;
136 }
bd414ee6 137 return 0;
93f65158
KT
138}
139
043ef46c
JE
140static struct xt_match ebt_ip6_mt_reg __read_mostly = {
141 .name = "ip6",
001a18d3
JE
142 .revision = 0,
143 .family = NFPROTO_BRIDGE,
2d06d4a5
JE
144 .match = ebt_ip6_mt,
145 .checkentry = ebt_ip6_mt_check,
fc0e3df4 146 .matchsize = sizeof(struct ebt_ip6_info),
93f65158
KT
147 .me = THIS_MODULE,
148};
149
150static int __init ebt_ip6_init(void)
151{
043ef46c 152 return xt_register_match(&ebt_ip6_mt_reg);
93f65158
KT
153}
154
155static void __exit ebt_ip6_fini(void)
156{
043ef46c 157 xt_unregister_match(&ebt_ip6_mt_reg);
93f65158
KT
158}
159
160module_init(ebt_ip6_init);
161module_exit(ebt_ip6_fini);
162MODULE_DESCRIPTION("Ebtables: IPv6 protocol packet match");
8244f4ba 163MODULE_AUTHOR("Kuo-Lang Tseng <kuo-lang.tseng@intel.com>");
93f65158 164MODULE_LICENSE("GPL");