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