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