8eecac14ddaa9da7107ab05ada358c534b77ccfd
[safe/jmp/linux-2.6] / net / ipv6 / netfilter / ip6t_hbh.c
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
19 #include <linux/netfilter/x_tables.h>
20 #include <linux/netfilter_ipv6/ip6_tables.h>
21 #include <linux/netfilter_ipv6/ip6t_opts.h>
22
23 MODULE_LICENSE("GPL");
24 MODULE_DESCRIPTION("IPv6 opts match");
25 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
26 MODULE_ALIAS("ip6t_dst");
27
28 #if 0
29 #define DEBUGP printk
30 #else
31 #define DEBUGP(format, args...)
32 #endif
33
34 /*
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
40  *  (Type & 0x20) >> 5
41  *      0       -> invariant
42  *      1       -> can change the routing
43  *  (Type & 0x1F) Type
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
48  */
49
50 static bool
51 match(const struct sk_buff *skb,
52       const struct net_device *in,
53       const struct net_device *out,
54       const struct xt_match *match,
55       const void *matchinfo,
56       int offset,
57       unsigned int protoff,
58       bool *hotdrop)
59 {
60         struct ipv6_opt_hdr _optsh;
61         const struct ipv6_opt_hdr *oh;
62         const struct ip6t_opts *optinfo = matchinfo;
63         unsigned int temp;
64         unsigned int ptr;
65         unsigned int hdrlen = 0;
66         bool ret = false;
67         u8 _opttype;
68         u8 _optlen;
69         const u_int8_t *tp = NULL;
70         const u_int8_t *lp = NULL;
71         unsigned int optlen;
72         int err;
73
74         err = ipv6_find_hdr(skb, &ptr, match->data, NULL);
75         if (err < 0) {
76                 if (err != -ENOENT)
77                         *hotdrop = true;
78                 return false;
79         }
80
81         oh = skb_header_pointer(skb, ptr, sizeof(_optsh), &_optsh);
82         if (oh == NULL) {
83                 *hotdrop = true;
84                 return false;
85         }
86
87         hdrlen = ipv6_optlen(oh);
88         if (skb->len - ptr < hdrlen) {
89                 /* Packet smaller than it's length field */
90                 return false;
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;
110         } else if (optinfo->flags & IP6T_OPTS_NSTRICT) {
111                 DEBUGP("Not strict - not implemented");
112         } else {
113                 DEBUGP("Strict ");
114                 DEBUGP("#%d ", optinfo->optsnr);
115                 for (temp = 0; temp < optinfo->optsnr; temp++) {
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 */
125                         if (*tp != (optinfo->opts[temp] & 0xFF00) >> 8) {
126                                 DEBUGP("Tbad %02X %02X\n",
127                                        *tp,
128                                        (optinfo->opts[temp] & 0xFF00) >> 8);
129                                 return false;
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);
150                                         return false;
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) &&
163                             temp < optinfo->optsnr - 1) {
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;
172                 else
173                         return false;
174         }
175
176         return false;
177 }
178
179 /* Called when user tries to insert an entry of this type. */
180 static bool
181 checkentry(const char *tablename,
182            const void *entry,
183            const struct xt_match *match,
184            void *matchinfo,
185            unsigned int hook_mask)
186 {
187         const struct ip6t_opts *optsinfo = matchinfo;
188
189         if (optsinfo->invflags & ~IP6T_OPTS_INV_MASK) {
190                 DEBUGP("ip6t_opts: unknown flags %X\n", optsinfo->invflags);
191                 return false;
192         }
193         return true;
194 }
195
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         },
215 };
216
217 static int __init ip6t_hbh_init(void)
218 {
219         return xt_register_matches(opts_match, ARRAY_SIZE(opts_match));
220 }
221
222 static void __exit ip6t_hbh_fini(void)
223 {
224         xt_unregister_matches(opts_match, ARRAY_SIZE(opts_match));
225 }
226
227 module_init(ip6t_hbh_init);
228 module_exit(ip6t_hbh_fini);