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 | */ | |
9 | ||
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"); |
5fa2a760 | 24 | MODULE_DESCRIPTION("IPv6 opts match"); |
1da177e4 | 25 | MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>"); |
5fa2a760 | 26 | MODULE_ALIAS("ip6t_dst"); |
1da177e4 LT |
27 | |
28 | #if 0 | |
29 | #define DEBUGP printk | |
30 | #else | |
31 | #define DEBUGP(format, args...) | |
32 | #endif | |
33 | ||
34 | /* | |
f0daaa65 YK |
35 | * (Type & 0xC0) >> 6 |
36 | * 0 -> ignorable | |
37 | * 1 -> must drop the packet | |
38 | * 2 -> send ICMP PARM PROB regardless and drop packet | |
39 | * 3 -> Send ICMP if not a multicast address and drop packet | |
1da177e4 | 40 | * (Type & 0x20) >> 5 |
f0daaa65 YK |
41 | * 0 -> invariant |
42 | * 1 -> can change the routing | |
1da177e4 | 43 | * (Type & 0x1F) Type |
f0daaa65 YK |
44 | * 0 -> Pad1 (only 1 byte!) |
45 | * 1 -> PadN LENGTH info (total length = length + 2) | |
46 | * C0 | 2 -> JUMBO 4 x x x x ( xxxx > 64k ) | |
47 | * 5 -> RTALERT 2 x x | |
1da177e4 LT |
48 | */ |
49 | ||
1d93a9cb | 50 | static bool |
1da177e4 LT |
51 | match(const struct sk_buff *skb, |
52 | const struct net_device *in, | |
53 | const struct net_device *out, | |
c4986734 | 54 | const struct xt_match *match, |
1da177e4 LT |
55 | const void *matchinfo, |
56 | int offset, | |
57 | unsigned int protoff, | |
cff533ac | 58 | bool *hotdrop) |
1da177e4 | 59 | { |
a47362a2 JE |
60 | struct ipv6_opt_hdr _optsh; |
61 | const struct ipv6_opt_hdr *oh; | |
f0daaa65 YK |
62 | const struct ip6t_opts *optinfo = matchinfo; |
63 | unsigned int temp; | |
64 | unsigned int ptr; | |
65 | unsigned int hdrlen = 0; | |
1d93a9cb | 66 | bool ret = false; |
a47362a2 JE |
67 | u8 _opttype; |
68 | u8 _optlen; | |
69 | const u_int8_t *tp = NULL; | |
70 | const u_int8_t *lp = NULL; | |
f0daaa65 | 71 | unsigned int optlen; |
6d381634 | 72 | int err; |
f0daaa65 | 73 | |
6d381634 PM |
74 | err = ipv6_find_hdr(skb, &ptr, match->data, NULL); |
75 | if (err < 0) { | |
76 | if (err != -ENOENT) | |
cff533ac | 77 | *hotdrop = true; |
1d93a9cb | 78 | return false; |
6d381634 | 79 | } |
1da177e4 | 80 | |
f0daaa65 YK |
81 | oh = skb_header_pointer(skb, ptr, sizeof(_optsh), &_optsh); |
82 | if (oh == NULL) { | |
cff533ac | 83 | *hotdrop = true; |
1d93a9cb | 84 | return false; |
f0daaa65 YK |
85 | } |
86 | ||
87 | hdrlen = ipv6_optlen(oh); | |
88 | if (skb->len - ptr < hdrlen) { | |
89 | /* Packet smaller than it's length field */ | |
1d93a9cb | 90 | return false; |
f0daaa65 YK |
91 | } |
92 | ||
93 | DEBUGP("IPv6 OPTS LEN %u %u ", hdrlen, oh->hdrlen); | |
94 | ||
95 | DEBUGP("len %02X %04X %02X ", | |
96 | optinfo->hdrlen, hdrlen, | |
97 | (!(optinfo->flags & IP6T_OPTS_LEN) || | |
98 | ((optinfo->hdrlen == hdrlen) ^ | |
99 | !!(optinfo->invflags & IP6T_OPTS_INV_LEN)))); | |
100 | ||
101 | ret = (oh != NULL) && | |
102 | (!(optinfo->flags & IP6T_OPTS_LEN) || | |
103 | ((optinfo->hdrlen == hdrlen) ^ | |
104 | !!(optinfo->invflags & IP6T_OPTS_INV_LEN))); | |
105 | ||
106 | ptr += 2; | |
107 | hdrlen -= 2; | |
108 | if (!(optinfo->flags & IP6T_OPTS_OPTS)) { | |
109 | return ret; | |
1da177e4 LT |
110 | } else if (optinfo->flags & IP6T_OPTS_NSTRICT) { |
111 | DEBUGP("Not strict - not implemented"); | |
112 | } else { | |
113 | DEBUGP("Strict "); | |
f0daaa65 YK |
114 | DEBUGP("#%d ", optinfo->optsnr); |
115 | for (temp = 0; temp < optinfo->optsnr; temp++) { | |
1da177e4 LT |
116 | /* type field exists ? */ |
117 | if (hdrlen < 1) | |
118 | break; | |
119 | tp = skb_header_pointer(skb, ptr, sizeof(_opttype), | |
120 | &_opttype); | |
121 | if (tp == NULL) | |
122 | break; | |
123 | ||
124 | /* Type check */ | |
f0daaa65 | 125 | if (*tp != (optinfo->opts[temp] & 0xFF00) >> 8) { |
1da177e4 LT |
126 | DEBUGP("Tbad %02X %02X\n", |
127 | *tp, | |
f0daaa65 | 128 | (optinfo->opts[temp] & 0xFF00) >> 8); |
1d93a9cb | 129 | return false; |
1da177e4 LT |
130 | } else { |
131 | DEBUGP("Tok "); | |
132 | } | |
133 | /* Length check */ | |
134 | if (*tp) { | |
135 | u16 spec_len; | |
136 | ||
137 | /* length field exists ? */ | |
138 | if (hdrlen < 2) | |
139 | break; | |
140 | lp = skb_header_pointer(skb, ptr + 1, | |
141 | sizeof(_optlen), | |
142 | &_optlen); | |
143 | if (lp == NULL) | |
144 | break; | |
145 | spec_len = optinfo->opts[temp] & 0x00FF; | |
146 | ||
147 | if (spec_len != 0x00FF && spec_len != *lp) { | |
148 | DEBUGP("Lbad %02X %04X\n", *lp, | |
149 | spec_len); | |
1d93a9cb | 150 | return false; |
1da177e4 LT |
151 | } |
152 | DEBUGP("Lok "); | |
153 | optlen = *lp + 2; | |
154 | } else { | |
155 | DEBUGP("Pad1\n"); | |
156 | optlen = 1; | |
157 | } | |
158 | ||
159 | /* Step to the next */ | |
160 | DEBUGP("len%04X \n", optlen); | |
161 | ||
162 | if ((ptr > skb->len - optlen || hdrlen < optlen) && | |
7c4e36bc | 163 | temp < optinfo->optsnr - 1) { |
1da177e4 LT |
164 | DEBUGP("new pointer is too large! \n"); |
165 | break; | |
166 | } | |
167 | ptr += optlen; | |
168 | hdrlen -= optlen; | |
169 | } | |
170 | if (temp == optinfo->optsnr) | |
171 | return ret; | |
f0daaa65 | 172 | else |
1d93a9cb | 173 | return false; |
1da177e4 LT |
174 | } |
175 | ||
1d93a9cb | 176 | return false; |
1da177e4 LT |
177 | } |
178 | ||
179 | /* Called when user tries to insert an entry of this type. */ | |
ccb79bdc | 180 | static bool |
1da177e4 | 181 | checkentry(const char *tablename, |
f0daaa65 | 182 | const void *entry, |
c4986734 | 183 | const struct xt_match *match, |
f0daaa65 | 184 | void *matchinfo, |
f0daaa65 | 185 | unsigned int hook_mask) |
1da177e4 | 186 | { |
f0daaa65 YK |
187 | const struct ip6t_opts *optsinfo = matchinfo; |
188 | ||
f0daaa65 YK |
189 | if (optsinfo->invflags & ~IP6T_OPTS_INV_MASK) { |
190 | DEBUGP("ip6t_opts: unknown flags %X\n", optsinfo->invflags); | |
ccb79bdc | 191 | return false; |
f0daaa65 | 192 | } |
ccb79bdc | 193 | return true; |
1da177e4 LT |
194 | } |
195 | ||
5fa2a760 PM |
196 | static struct xt_match opts_match[] = { |
197 | { | |
198 | .name = "hbh", | |
199 | .family = AF_INET6, | |
200 | .match = match, | |
201 | .matchsize = sizeof(struct ip6t_opts), | |
202 | .checkentry = checkentry, | |
203 | .me = THIS_MODULE, | |
204 | .data = NEXTHDR_HOP, | |
205 | }, | |
206 | { | |
207 | .name = "dst", | |
208 | .family = AF_INET6, | |
209 | .match = match, | |
210 | .matchsize = sizeof(struct ip6t_opts), | |
211 | .checkentry = checkentry, | |
212 | .me = THIS_MODULE, | |
213 | .data = NEXTHDR_DEST, | |
214 | }, | |
1da177e4 LT |
215 | }; |
216 | ||
65b4b4e8 | 217 | static int __init ip6t_hbh_init(void) |
1da177e4 | 218 | { |
5fa2a760 | 219 | return xt_register_matches(opts_match, ARRAY_SIZE(opts_match)); |
1da177e4 LT |
220 | } |
221 | ||
65b4b4e8 | 222 | static void __exit ip6t_hbh_fini(void) |
1da177e4 | 223 | { |
5fa2a760 | 224 | xt_unregister_matches(opts_match, ARRAY_SIZE(opts_match)); |
1da177e4 LT |
225 | } |
226 | ||
65b4b4e8 AM |
227 | module_init(ip6t_hbh_init); |
228 | module_exit(ip6t_hbh_fini); |