blob: 4617491be41e76b27ada6d843511fe75b2611624 [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>
Jan Engelhardt408ffaa2010-02-28 23:19:52 +01007 * Jan Engelhardt <jengelh@medozas.de>
Kuo-lang Tseng93f65152008-06-09 15:55:45 -07008 *
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
Florian Westphal6faee602010-12-20 15:57:47 +010025union pkthdr {
26 struct {
27 __be16 src;
28 __be16 dst;
29 } tcpudphdr;
30 struct {
31 u8 type;
32 u8 code;
33 } icmphdr;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070034};
35
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +020036static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020037ebt_ip6_mt(const struct sk_buff *skb, struct xt_action_param *par)
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070038{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020039 const struct ebt_ip6_info *info = par->matchinfo;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070040 const struct ipv6hdr *ih6;
41 struct ipv6hdr _ip6h;
Florian Westphal6faee602010-12-20 15:57:47 +010042 const union pkthdr *pptr;
43 union pkthdr _pkthdr;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070044
45 ih6 = skb_header_pointer(skb, 0, sizeof(_ip6h), &_ip6h);
46 if (ih6 == NULL)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020047 return false;
Joe Perchesc37a2df2016-06-24 13:25:22 -070048 if ((info->bitmask & EBT_IP6_TCLASS) &&
49 NF_INVF(info, EBT_IP6_TCLASS,
50 info->tclass != ipv6_get_dsfield(ih6)))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020051 return false;
Joe Perchesc37a2df2016-06-24 13:25:22 -070052 if (((info->bitmask & EBT_IP6_SOURCE) &&
53 NF_INVF(info, EBT_IP6_SOURCE,
54 ipv6_masked_addr_cmp(&ih6->saddr, &info->smsk,
55 &info->saddr))) ||
56 ((info->bitmask & EBT_IP6_DEST) &&
57 NF_INVF(info, EBT_IP6_DEST,
58 ipv6_masked_addr_cmp(&ih6->daddr, &info->dmsk,
59 &info->daddr))))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020060 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070061 if (info->bitmask & EBT_IP6_PROTO) {
62 uint8_t nexthdr = ih6->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -080063 __be16 frag_off;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070064 int offset_ph;
65
Jesse Gross75f28112011-11-30 17:05:51 -080066 offset_ph = ipv6_skip_exthdr(skb, sizeof(_ip6h), &nexthdr, &frag_off);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070067 if (offset_ph == -1)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020068 return false;
Joe Perchesc37a2df2016-06-24 13:25:22 -070069 if (NF_INVF(info, EBT_IP6_PROTO, info->protocol != nexthdr))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020070 return false;
Ian Morrisc1bc1d22015-10-26 09:10:43 +000071 if (!(info->bitmask & (EBT_IP6_DPORT |
72 EBT_IP6_SPORT | EBT_IP6_ICMP6)))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020073 return true;
Florian Westphal6faee602010-12-20 15:57:47 +010074
75 /* min icmpv6 headersize is 4, so sizeof(_pkthdr) is ok. */
76 pptr = skb_header_pointer(skb, offset_ph, sizeof(_pkthdr),
77 &_pkthdr);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070078 if (pptr == NULL)
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020079 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070080 if (info->bitmask & EBT_IP6_DPORT) {
Florian Westphal6faee602010-12-20 15:57:47 +010081 u16 dst = ntohs(pptr->tcpudphdr.dst);
Joe Perchesc37a2df2016-06-24 13:25:22 -070082 if (NF_INVF(info, EBT_IP6_DPORT,
83 dst < info->dport[0] ||
84 dst > info->dport[1]))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020085 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070086 }
87 if (info->bitmask & EBT_IP6_SPORT) {
Florian Westphal6faee602010-12-20 15:57:47 +010088 u16 src = ntohs(pptr->tcpudphdr.src);
Joe Perchesc37a2df2016-06-24 13:25:22 -070089 if (NF_INVF(info, EBT_IP6_SPORT,
90 src < info->sport[0] ||
91 src > info->sport[1]))
Jan Engelhardt8cc784e2008-10-08 11:35:13 +020092 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070093 }
Florian Westphal6faee602010-12-20 15:57:47 +010094 if ((info->bitmask & EBT_IP6_ICMP6) &&
Joe Perchesc37a2df2016-06-24 13:25:22 -070095 NF_INVF(info, EBT_IP6_ICMP6,
96 pptr->icmphdr.type < info->icmpv6_type[0] ||
97 pptr->icmphdr.type > info->icmpv6_type[1] ||
98 pptr->icmphdr.code < info->icmpv6_code[0] ||
99 pptr->icmphdr.code > info->icmpv6_code[1]))
Florian Westphal6faee602010-12-20 15:57:47 +0100100 return false;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700101 }
Jan Engelhardt8cc784e2008-10-08 11:35:13 +0200102 return true;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700103}
104
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100105static int ebt_ip6_mt_check(const struct xt_mtchk_param *par)
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700106{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200107 const struct ebt_entry *e = par->entryinfo;
108 struct ebt_ip6_info *info = par->matchinfo;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700109
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700110 if (e->ethproto != htons(ETH_P_IPV6) || e->invflags & EBT_IPROTO)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100111 return -EINVAL;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700112 if (info->bitmask & ~EBT_IP6_MASK || info->invflags & ~EBT_IP6_MASK)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100113 return -EINVAL;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700114 if (info->bitmask & (EBT_IP6_DPORT | EBT_IP6_SPORT)) {
115 if (info->invflags & EBT_IP6_PROTO)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100116 return -EINVAL;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700117 if (info->protocol != IPPROTO_TCP &&
118 info->protocol != IPPROTO_UDP &&
119 info->protocol != IPPROTO_UDPLITE &&
120 info->protocol != IPPROTO_SCTP &&
121 info->protocol != IPPROTO_DCCP)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100122 return -EINVAL;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700123 }
124 if (info->bitmask & EBT_IP6_DPORT && info->dport[0] > info->dport[1])
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100125 return -EINVAL;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700126 if (info->bitmask & EBT_IP6_SPORT && info->sport[0] > info->sport[1])
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100127 return -EINVAL;
Florian Westphal6faee602010-12-20 15:57:47 +0100128 if (info->bitmask & EBT_IP6_ICMP6) {
129 if ((info->invflags & EBT_IP6_PROTO) ||
130 info->protocol != IPPROTO_ICMPV6)
131 return -EINVAL;
132 if (info->icmpv6_type[0] > info->icmpv6_type[1] ||
133 info->icmpv6_code[0] > info->icmpv6_code[1])
134 return -EINVAL;
135 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100136 return 0;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700137}
138
Jan Engelhardt043ef462008-10-08 11:35:15 +0200139static struct xt_match ebt_ip6_mt_reg __read_mostly = {
140 .name = "ip6",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200141 .revision = 0,
142 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200143 .match = ebt_ip6_mt,
144 .checkentry = ebt_ip6_mt_check,
Florian Westphalfc0e3df2010-02-15 18:16:26 +0100145 .matchsize = sizeof(struct ebt_ip6_info),
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700146 .me = THIS_MODULE,
147};
148
149static int __init ebt_ip6_init(void)
150{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200151 return xt_register_match(&ebt_ip6_mt_reg);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700152}
153
154static void __exit ebt_ip6_fini(void)
155{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200156 xt_unregister_match(&ebt_ip6_mt_reg);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700157}
158
159module_init(ebt_ip6_init);
160module_exit(ebt_ip6_fini);
161MODULE_DESCRIPTION("Ebtables: IPv6 protocol packet match");
Jan Engelhardt8244f4b2010-02-28 23:22:04 +0100162MODULE_AUTHOR("Kuo-Lang Tseng <kuo-lang.tseng@intel.com>");
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700163MODULE_LICENSE("GPL");