Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Kernel module to match Hop-by-Hop and Destination parameters. */ |
2 | ||
3 | /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | */ | |
ff67e4e4 | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4 LT |
10 | #include <linux/module.h> |
11 | #include <linux/skbuff.h> | |
12 | #include <linux/ipv6.h> | |
13 | #include <linux/types.h> | |
14 | #include <net/checksum.h> | |
15 | #include <net/ipv6.h> | |
16 | ||
17 | #include <asm/byteorder.h> | |
18 | ||
6709dbbb | 19 | #include <linux/netfilter/x_tables.h> |
1da177e4 LT |
20 | #include <linux/netfilter_ipv6/ip6_tables.h> |
21 | #include <linux/netfilter_ipv6/ip6t_opts.h> | |
22 | ||
1da177e4 | 23 | MODULE_LICENSE("GPL"); |
2ae15b64 | 24 | MODULE_DESCRIPTION("Xtables: IPv6 Hop-By-Hop and Destination Header match"); |
1da177e4 | 25 | MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>"); |
5fa2a760 | 26 | MODULE_ALIAS("ip6t_dst"); |
1da177e4 | 27 | |
1da177e4 | 28 | /* |
f0daaa65 YK |
29 | * (Type & 0xC0) >> 6 |
30 | * 0 -> ignorable | |
31 | * 1 -> must drop the packet | |
32 | * 2 -> send ICMP PARM PROB regardless and drop packet | |
33 | * 3 -> Send ICMP if not a multicast address and drop packet | |
1da177e4 | 34 | * (Type & 0x20) >> 5 |
f0daaa65 YK |
35 | * 0 -> invariant |
36 | * 1 -> can change the routing | |
1da177e4 | 37 | * (Type & 0x1F) Type |
f0daaa65 YK |
38 | * 0 -> Pad1 (only 1 byte!) |
39 | * 1 -> PadN LENGTH info (total length = length + 2) | |
40 | * C0 | 2 -> JUMBO 4 x x x x ( xxxx > 64k ) | |
41 | * 5 -> RTALERT 2 x x | |
1da177e4 LT |
42 | */ |
43 | ||
4f948db1 JE |
44 | static struct xt_match hbh_mt6_reg[] __read_mostly; |
45 | ||
1d93a9cb | 46 | static bool |
62fc8051 | 47 | hbh_mt6(const struct sk_buff *skb, struct xt_action_param *par) |
1da177e4 | 48 | { |
a47362a2 JE |
49 | struct ipv6_opt_hdr _optsh; |
50 | const struct ipv6_opt_hdr *oh; | |
f7108a20 | 51 | const struct ip6t_opts *optinfo = par->matchinfo; |
f0daaa65 | 52 | unsigned int temp; |
84018f55 | 53 | unsigned int ptr = 0; |
f0daaa65 | 54 | unsigned int hdrlen = 0; |
1d93a9cb | 55 | bool ret = false; |
a47362a2 JE |
56 | u8 _opttype; |
57 | u8 _optlen; | |
58 | const u_int8_t *tp = NULL; | |
59 | const u_int8_t *lp = NULL; | |
f0daaa65 | 60 | unsigned int optlen; |
6d381634 | 61 | int err; |
f0daaa65 | 62 | |
4f948db1 JE |
63 | err = ipv6_find_hdr(skb, &ptr, |
64 | (par->match == &hbh_mt6_reg[0]) ? | |
84018f55 | 65 | NEXTHDR_HOP : NEXTHDR_DEST, NULL, NULL); |
6d381634 PM |
66 | if (err < 0) { |
67 | if (err != -ENOENT) | |
b4ba2611 | 68 | par->hotdrop = true; |
1d93a9cb | 69 | return false; |
6d381634 | 70 | } |
1da177e4 | 71 | |
f0daaa65 YK |
72 | oh = skb_header_pointer(skb, ptr, sizeof(_optsh), &_optsh); |
73 | if (oh == NULL) { | |
b4ba2611 | 74 | par->hotdrop = true; |
1d93a9cb | 75 | return false; |
f0daaa65 YK |
76 | } |
77 | ||
78 | hdrlen = ipv6_optlen(oh); | |
79 | if (skb->len - ptr < hdrlen) { | |
80 | /* Packet smaller than it's length field */ | |
1d93a9cb | 81 | return false; |
f0daaa65 YK |
82 | } |
83 | ||
0d53778e | 84 | pr_debug("IPv6 OPTS LEN %u %u ", hdrlen, oh->hdrlen); |
f0daaa65 | 85 | |
0d53778e PM |
86 | pr_debug("len %02X %04X %02X ", |
87 | optinfo->hdrlen, hdrlen, | |
88 | (!(optinfo->flags & IP6T_OPTS_LEN) || | |
89 | ((optinfo->hdrlen == hdrlen) ^ | |
90 | !!(optinfo->invflags & IP6T_OPTS_INV_LEN)))); | |
f0daaa65 YK |
91 | |
92 | ret = (oh != NULL) && | |
93 | (!(optinfo->flags & IP6T_OPTS_LEN) || | |
94 | ((optinfo->hdrlen == hdrlen) ^ | |
95 | !!(optinfo->invflags & IP6T_OPTS_INV_LEN))); | |
96 | ||
97 | ptr += 2; | |
98 | hdrlen -= 2; | |
99 | if (!(optinfo->flags & IP6T_OPTS_OPTS)) { | |
100 | return ret; | |
1da177e4 | 101 | } else { |
0d53778e PM |
102 | pr_debug("Strict "); |
103 | pr_debug("#%d ", optinfo->optsnr); | |
f0daaa65 | 104 | for (temp = 0; temp < optinfo->optsnr; temp++) { |
1da177e4 LT |
105 | /* type field exists ? */ |
106 | if (hdrlen < 1) | |
107 | break; | |
108 | tp = skb_header_pointer(skb, ptr, sizeof(_opttype), | |
109 | &_opttype); | |
110 | if (tp == NULL) | |
111 | break; | |
112 | ||
113 | /* Type check */ | |
f0daaa65 | 114 | if (*tp != (optinfo->opts[temp] & 0xFF00) >> 8) { |
0d53778e PM |
115 | pr_debug("Tbad %02X %02X\n", *tp, |
116 | (optinfo->opts[temp] & 0xFF00) >> 8); | |
1d93a9cb | 117 | return false; |
1da177e4 | 118 | } else { |
0d53778e | 119 | pr_debug("Tok "); |
1da177e4 LT |
120 | } |
121 | /* Length check */ | |
122 | if (*tp) { | |
123 | u16 spec_len; | |
124 | ||
125 | /* length field exists ? */ | |
126 | if (hdrlen < 2) | |
127 | break; | |
128 | lp = skb_header_pointer(skb, ptr + 1, | |
129 | sizeof(_optlen), | |
130 | &_optlen); | |
131 | if (lp == NULL) | |
132 | break; | |
133 | spec_len = optinfo->opts[temp] & 0x00FF; | |
134 | ||
135 | if (spec_len != 0x00FF && spec_len != *lp) { | |
0d53778e PM |
136 | pr_debug("Lbad %02X %04X\n", *lp, |
137 | spec_len); | |
1d93a9cb | 138 | return false; |
1da177e4 | 139 | } |
0d53778e | 140 | pr_debug("Lok "); |
1da177e4 LT |
141 | optlen = *lp + 2; |
142 | } else { | |
0d53778e | 143 | pr_debug("Pad1\n"); |
1da177e4 LT |
144 | optlen = 1; |
145 | } | |
146 | ||
147 | /* Step to the next */ | |
b1383380 | 148 | pr_debug("len%04X\n", optlen); |
1da177e4 LT |
149 | |
150 | if ((ptr > skb->len - optlen || hdrlen < optlen) && | |
7c4e36bc | 151 | temp < optinfo->optsnr - 1) { |
b1383380 | 152 | pr_debug("new pointer is too large!\n"); |
1da177e4 LT |
153 | break; |
154 | } | |
155 | ptr += optlen; | |
156 | hdrlen -= optlen; | |
157 | } | |
158 | if (temp == optinfo->optsnr) | |
159 | return ret; | |
f0daaa65 | 160 | else |
1d93a9cb | 161 | return false; |
1da177e4 LT |
162 | } |
163 | ||
1d93a9cb | 164 | return false; |
1da177e4 LT |
165 | } |
166 | ||
b0f38452 | 167 | static int hbh_mt6_check(const struct xt_mtchk_param *par) |
1da177e4 | 168 | { |
9b4fce7a | 169 | const struct ip6t_opts *optsinfo = par->matchinfo; |
f0daaa65 | 170 | |
f0daaa65 | 171 | if (optsinfo->invflags & ~IP6T_OPTS_INV_MASK) { |
ff67e4e4 | 172 | pr_debug("unknown flags %X\n", optsinfo->invflags); |
bd414ee6 | 173 | return -EINVAL; |
f0daaa65 | 174 | } |
8ca31ce5 YK |
175 | |
176 | if (optsinfo->flags & IP6T_OPTS_NSTRICT) { | |
ff67e4e4 | 177 | pr_debug("Not strict - not implemented"); |
bd414ee6 | 178 | return -EINVAL; |
8ca31ce5 YK |
179 | } |
180 | ||
bd414ee6 | 181 | return 0; |
1da177e4 LT |
182 | } |
183 | ||
d3c5ee6d | 184 | static struct xt_match hbh_mt6_reg[] __read_mostly = { |
5fa2a760 | 185 | { |
4f948db1 | 186 | /* Note, hbh_mt6 relies on the order of hbh_mt6_reg */ |
5fa2a760 | 187 | .name = "hbh", |
ee999d8b | 188 | .family = NFPROTO_IPV6, |
d3c5ee6d | 189 | .match = hbh_mt6, |
5fa2a760 | 190 | .matchsize = sizeof(struct ip6t_opts), |
d3c5ee6d | 191 | .checkentry = hbh_mt6_check, |
5fa2a760 | 192 | .me = THIS_MODULE, |
5fa2a760 PM |
193 | }, |
194 | { | |
195 | .name = "dst", | |
ee999d8b | 196 | .family = NFPROTO_IPV6, |
d3c5ee6d | 197 | .match = hbh_mt6, |
5fa2a760 | 198 | .matchsize = sizeof(struct ip6t_opts), |
d3c5ee6d | 199 | .checkentry = hbh_mt6_check, |
5fa2a760 | 200 | .me = THIS_MODULE, |
5fa2a760 | 201 | }, |
1da177e4 LT |
202 | }; |
203 | ||
d3c5ee6d | 204 | static int __init hbh_mt6_init(void) |
1da177e4 | 205 | { |
d3c5ee6d | 206 | return xt_register_matches(hbh_mt6_reg, ARRAY_SIZE(hbh_mt6_reg)); |
1da177e4 LT |
207 | } |
208 | ||
d3c5ee6d | 209 | static void __exit hbh_mt6_exit(void) |
1da177e4 | 210 | { |
d3c5ee6d | 211 | xt_unregister_matches(hbh_mt6_reg, ARRAY_SIZE(hbh_mt6_reg)); |
1da177e4 LT |
212 | } |
213 | ||
d3c5ee6d JE |
214 | module_init(hbh_mt6_init); |
215 | module_exit(hbh_mt6_exit); |