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