This commit was generated by cvs2svn to compensate for changes in r1129,
[linux-2.6.git] / net / ipv6 / netfilter / ip6t_dst.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        0
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         int err;
73
74 #if HOPBYHOP
75         err = ipv6_find_hdr(skb, &ptr, NEXTHDR_HOP, NULL);
76 #else
77         err = ipv6_find_hdr(skb, &ptr, NEXTHDR_DEST, NULL);
78 #endif
79         if (err < 0) {
80                 if (err != -ENOENT)
81                         *hotdrop = 1;
82                 return 0;
83         }
84
85         oh = skb_header_pointer(skb, ptr, sizeof(_optsh), &_optsh);
86         if (oh == NULL) {
87                 *hotdrop = 1;
88                 return 0;
89         }
90
91         hdrlen = ipv6_optlen(oh);
92         if (skb->len - ptr < hdrlen) {
93                 /* Packet smaller than it's length field */
94                 return 0;
95         }
96
97         DEBUGP("IPv6 OPTS LEN %u %u ", hdrlen, oh->hdrlen);
98
99         DEBUGP("len %02X %04X %02X ",
100                optinfo->hdrlen, hdrlen,
101                (!(optinfo->flags & IP6T_OPTS_LEN) ||
102                 ((optinfo->hdrlen == hdrlen) ^
103                  !!(optinfo->invflags & IP6T_OPTS_INV_LEN))));
104
105         ret = (oh != NULL) &&
106               (!(optinfo->flags & IP6T_OPTS_LEN) ||
107                ((optinfo->hdrlen == hdrlen) ^
108                 !!(optinfo->invflags & IP6T_OPTS_INV_LEN)));
109
110         ptr += 2;
111         hdrlen -= 2;
112         if (!(optinfo->flags & IP6T_OPTS_OPTS)) {
113                 return ret;
114         } else if (optinfo->flags & IP6T_OPTS_NSTRICT) {
115                 DEBUGP("Not strict - not implemented");
116         } else {
117                 DEBUGP("Strict ");
118                 DEBUGP("#%d ", optinfo->optsnr);
119                 for (temp = 0; temp < optinfo->optsnr; temp++) {
120                         /* type field exists ? */
121                         if (hdrlen < 1)
122                                 break;
123                         tp = skb_header_pointer(skb, ptr, sizeof(_opttype),
124                                                 &_opttype);
125                         if (tp == NULL)
126                                 break;
127
128                         /* Type check */
129                         if (*tp != (optinfo->opts[temp] & 0xFF00) >> 8) {
130                                 DEBUGP("Tbad %02X %02X\n",
131                                        *tp,
132                                        (optinfo->opts[temp] & 0xFF00) >> 8);
133                                 return 0;
134                         } else {
135                                 DEBUGP("Tok ");
136                         }
137                         /* Length check */
138                         if (*tp) {
139                                 u16 spec_len;
140
141                                 /* length field exists ? */
142                                 if (hdrlen < 2)
143                                         break;
144                                 lp = skb_header_pointer(skb, ptr + 1,
145                                                         sizeof(_optlen),
146                                                         &_optlen);
147                                 if (lp == NULL)
148                                         break;
149                                 spec_len = optinfo->opts[temp] & 0x00FF;
150
151                                 if (spec_len != 0x00FF && spec_len != *lp) {
152                                         DEBUGP("Lbad %02X %04X\n", *lp,
153                                                spec_len);
154                                         return 0;
155                                 }
156                                 DEBUGP("Lok ");
157                                 optlen = *lp + 2;
158                         } else {
159                                 DEBUGP("Pad1\n");
160                                 optlen = 1;
161                         }
162
163                         /* Step to the next */
164                         DEBUGP("len%04X \n", optlen);
165
166                         if ((ptr > skb->len - optlen || hdrlen < optlen) &&
167                             (temp < optinfo->optsnr - 1)) {
168                                 DEBUGP("new pointer is too large! \n");
169                                 break;
170                         }
171                         ptr += optlen;
172                         hdrlen -= optlen;
173                 }
174                 if (temp == optinfo->optsnr)
175                         return ret;
176                 else
177                         return 0;
178         }
179
180         return 0;
181 }
182
183 /* Called when user tries to insert an entry of this type. */
184 static int
185 checkentry(const char *tablename,
186            const void *info,
187            void *matchinfo,
188            unsigned int matchinfosize,
189            unsigned int hook_mask)
190 {
191         const struct ip6t_opts *optsinfo = matchinfo;
192
193         if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_opts))) {
194                 DEBUGP("ip6t_opts: matchsize %u != %u\n",
195                        matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_opts)));
196                 return 0;
197         }
198         if (optsinfo->invflags & ~IP6T_OPTS_INV_MASK) {
199                 DEBUGP("ip6t_opts: unknown flags %X\n", optsinfo->invflags);
200                 return 0;
201         }
202
203         return 1;
204 }
205
206 static struct ip6t_match opts_match = {
207 #if HOPBYHOP
208         .name           = "hbh",
209 #else
210         .name           = "dst",
211 #endif
212         .match          = &match,
213         .checkentry     = &checkentry,
214         .me             = THIS_MODULE,
215 };
216
217 static int __init init(void)
218 {
219         return ip6t_register_match(&opts_match);
220 }
221
222 static void __exit cleanup(void)
223 {
224         ip6t_unregister_match(&opts_match);
225 }
226
227 module_init(init);
228 module_exit(cleanup);