linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / net / ipv6 / netfilter / ip6t_rt.c
1 /* Kernel module to match ROUTING 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_rt.h>
21
22 MODULE_LICENSE("GPL");
23 MODULE_DESCRIPTION("IPv6 RT match");
24 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
25
26 #if 0
27 #define DEBUGP printk
28 #else
29 #define DEBUGP(format, args...)
30 #endif
31
32 /* Returns 1 if the id is matched by the range, 0 otherwise */
33 static inline int
34 segsleft_match(u_int32_t min, u_int32_t max, u_int32_t id, int invert)
35 {
36         int r = 0;
37         DEBUGP("rt segsleft_match:%c 0x%x <= 0x%x <= 0x%x",
38                invert ? '!' : ' ', min, id, max);
39         r = (id >= min && id <= max) ^ invert;
40         DEBUGP(" result %s\n", r ? "PASS" : "FAILED");
41         return r;
42 }
43
44 static int
45 match(const struct sk_buff *skb,
46       const struct net_device *in,
47       const struct net_device *out,
48       const void *matchinfo,
49       int offset,
50       unsigned int protoff,
51       int *hotdrop)
52 {
53         struct ipv6_rt_hdr _route, *rh;
54         const struct ip6t_rt *rtinfo = matchinfo;
55         unsigned int temp;
56         unsigned int ptr;
57         unsigned int hdrlen = 0;
58         unsigned int ret = 0;
59         struct in6_addr *ap, _addr;
60         int err;
61
62         err = ipv6_find_hdr(skb, &ptr, NEXTHDR_ROUTING, NULL);
63         if (err < 0) {
64                 if (err != -ENOENT)
65                         *hotdrop = 1;
66                 return 0;
67         }
68
69         rh = skb_header_pointer(skb, ptr, sizeof(_route), &_route);
70         if (rh == NULL) {
71                 *hotdrop = 1;
72                 return 0;
73         }
74
75         hdrlen = ipv6_optlen(rh);
76         if (skb->len - ptr < hdrlen) {
77                 /* Pcket smaller than its length field */
78                 return 0;
79         }
80
81         DEBUGP("IPv6 RT LEN %u %u ", hdrlen, rh->hdrlen);
82         DEBUGP("TYPE %04X ", rh->type);
83         DEBUGP("SGS_LEFT %u %02X\n", rh->segments_left, rh->segments_left);
84
85         DEBUGP("IPv6 RT segsleft %02X ",
86                (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1],
87                                rh->segments_left,
88                                !!(rtinfo->invflags & IP6T_RT_INV_SGS))));
89         DEBUGP("type %02X %02X %02X ",
90                rtinfo->rt_type, rh->type,
91                (!(rtinfo->flags & IP6T_RT_TYP) ||
92                 ((rtinfo->rt_type == rh->type) ^
93                  !!(rtinfo->invflags & IP6T_RT_INV_TYP))));
94         DEBUGP("len %02X %04X %02X ",
95                rtinfo->hdrlen, hdrlen,
96                (!(rtinfo->flags & IP6T_RT_LEN) ||
97                 ((rtinfo->hdrlen == hdrlen) ^
98                  !!(rtinfo->invflags & IP6T_RT_INV_LEN))));
99         DEBUGP("res %02X %02X %02X ",
100                (rtinfo->flags & IP6T_RT_RES),
101                ((struct rt0_hdr *)rh)->reserved,
102                !((rtinfo->flags & IP6T_RT_RES) &&
103                  (((struct rt0_hdr *)rh)->reserved)));
104
105         ret = (rh != NULL)
106               &&
107               (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1],
108                               rh->segments_left,
109                               !!(rtinfo->invflags & IP6T_RT_INV_SGS)))
110               &&
111               (!(rtinfo->flags & IP6T_RT_LEN) ||
112                ((rtinfo->hdrlen == hdrlen) ^
113                 !!(rtinfo->invflags & IP6T_RT_INV_LEN)))
114               &&
115               (!(rtinfo->flags & IP6T_RT_TYP) ||
116                ((rtinfo->rt_type == rh->type) ^
117                 !!(rtinfo->invflags & IP6T_RT_INV_TYP)));
118
119         if (ret && (rtinfo->flags & IP6T_RT_RES)) {
120                 u_int32_t *rp, _reserved;
121                 rp = skb_header_pointer(skb,
122                                         ptr + offsetof(struct rt0_hdr,
123                                                        reserved),
124                                         sizeof(_reserved),
125                                         &_reserved);
126
127                 ret = (*rp == 0);
128         }
129
130         DEBUGP("#%d ", rtinfo->addrnr);
131         if (!(rtinfo->flags & IP6T_RT_FST)) {
132                 return ret;
133         } else if (rtinfo->flags & IP6T_RT_FST_NSTRICT) {
134                 DEBUGP("Not strict ");
135                 if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
136                         DEBUGP("There isn't enough space\n");
137                         return 0;
138                 } else {
139                         unsigned int i = 0;
140
141                         DEBUGP("#%d ", rtinfo->addrnr);
142                         for (temp = 0;
143                              temp < (unsigned int)((hdrlen - 8) / 16);
144                              temp++) {
145                                 ap = skb_header_pointer(skb,
146                                                         ptr
147                                                         + sizeof(struct rt0_hdr)
148                                                         + temp * sizeof(_addr),
149                                                         sizeof(_addr),
150                                                         &_addr);
151
152                                 BUG_ON(ap == NULL);
153
154                                 if (ipv6_addr_equal(ap, &rtinfo->addrs[i])) {
155                                         DEBUGP("i=%d temp=%d;\n", i, temp);
156                                         i++;
157                                 }
158                                 if (i == rtinfo->addrnr)
159                                         break;
160                         }
161                         DEBUGP("i=%d #%d\n", i, rtinfo->addrnr);
162                         if (i == rtinfo->addrnr)
163                                 return ret;
164                         else
165                                 return 0;
166                 }
167         } else {
168                 DEBUGP("Strict ");
169                 if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
170                         DEBUGP("There isn't enough space\n");
171                         return 0;
172                 } else {
173                         DEBUGP("#%d ", rtinfo->addrnr);
174                         for (temp = 0; temp < rtinfo->addrnr; temp++) {
175                                 ap = skb_header_pointer(skb,
176                                                         ptr
177                                                         + sizeof(struct rt0_hdr)
178                                                         + temp * sizeof(_addr),
179                                                         sizeof(_addr),
180                                                         &_addr);
181                                 BUG_ON(ap == NULL);
182
183                                 if (!ipv6_addr_equal(ap, &rtinfo->addrs[temp]))
184                                         break;
185                         }
186                         DEBUGP("temp=%d #%d\n", temp, rtinfo->addrnr);
187                         if ((temp == rtinfo->addrnr) &&
188                             (temp == (unsigned int)((hdrlen - 8) / 16)))
189                                 return ret;
190                         else
191                                 return 0;
192                 }
193         }
194
195         return 0;
196 }
197
198 /* Called when user tries to insert an entry of this type. */
199 static int
200 checkentry(const char *tablename,
201            const void *entry,
202            void *matchinfo,
203            unsigned int matchinfosize,
204            unsigned int hook_mask)
205 {
206         const struct ip6t_rt *rtinfo = matchinfo;
207
208         if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_rt))) {
209                 DEBUGP("ip6t_rt: matchsize %u != %u\n",
210                        matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_rt)));
211                 return 0;
212         }
213         if (rtinfo->invflags & ~IP6T_RT_INV_MASK) {
214                 DEBUGP("ip6t_rt: unknown flags %X\n", rtinfo->invflags);
215                 return 0;
216         }
217         if ((rtinfo->flags & (IP6T_RT_RES | IP6T_RT_FST_MASK)) &&
218             (!(rtinfo->flags & IP6T_RT_TYP) ||
219              (rtinfo->rt_type != 0) ||
220              (rtinfo->invflags & IP6T_RT_INV_TYP))) {
221                 DEBUGP("`--rt-type 0' required before `--rt-0-*'");
222                 return 0;
223         }
224
225         return 1;
226 }
227
228 static struct ip6t_match rt_match = {
229         .name           = "rt",
230         .match          = &match,
231         .checkentry     = &checkentry,
232         .me             = THIS_MODULE,
233 };
234
235 static int __init init(void)
236 {
237         return ip6t_register_match(&rt_match);
238 }
239
240 static void __exit cleanup(void)
241 {
242         ip6t_unregister_match(&rt_match);
243 }
244
245 module_init(init);
246 module_exit(cleanup);