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