Merge branch 'devel' of master.kernel.org:/home/rmk/linux-2.6-mmc
[linux-2.6] / 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 struct xt_match *match,
49       const void *matchinfo,
50       int offset,
51       unsigned int protoff,
52       int *hotdrop)
53 {
54         struct ipv6_rt_hdr _route, *rh;
55         const struct ip6t_rt *rtinfo = matchinfo;
56         unsigned int temp;
57         unsigned int ptr;
58         unsigned int hdrlen = 0;
59         unsigned int ret = 0;
60         struct in6_addr *ap, _addr;
61
62         if (ipv6_find_hdr(skb, &ptr, NEXTHDR_ROUTING, NULL) < 0)
63                 return 0;
64
65         rh = skb_header_pointer(skb, ptr, sizeof(_route), &_route);
66         if (rh == NULL) {
67                 *hotdrop = 1;
68                 return 0;
69         }
70
71         hdrlen = ipv6_optlen(rh);
72         if (skb->len - ptr < hdrlen) {
73                 /* Pcket smaller than its length field */
74                 return 0;
75         }
76
77         DEBUGP("IPv6 RT LEN %u %u ", hdrlen, rh->hdrlen);
78         DEBUGP("TYPE %04X ", rh->type);
79         DEBUGP("SGS_LEFT %u %02X\n", rh->segments_left, rh->segments_left);
80
81         DEBUGP("IPv6 RT segsleft %02X ",
82                (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1],
83                                rh->segments_left,
84                                !!(rtinfo->invflags & IP6T_RT_INV_SGS))));
85         DEBUGP("type %02X %02X %02X ",
86                rtinfo->rt_type, rh->type,
87                (!(rtinfo->flags & IP6T_RT_TYP) ||
88                 ((rtinfo->rt_type == rh->type) ^
89                  !!(rtinfo->invflags & IP6T_RT_INV_TYP))));
90         DEBUGP("len %02X %04X %02X ",
91                rtinfo->hdrlen, hdrlen,
92                (!(rtinfo->flags & IP6T_RT_LEN) ||
93                 ((rtinfo->hdrlen == hdrlen) ^
94                  !!(rtinfo->invflags & IP6T_RT_INV_LEN))));
95         DEBUGP("res %02X %02X %02X ",
96                (rtinfo->flags & IP6T_RT_RES),
97                ((struct rt0_hdr *)rh)->reserved,
98                !((rtinfo->flags & IP6T_RT_RES) &&
99                  (((struct rt0_hdr *)rh)->reserved)));
100
101         ret = (rh != NULL)
102               &&
103               (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1],
104                               rh->segments_left,
105                               !!(rtinfo->invflags & IP6T_RT_INV_SGS)))
106               &&
107               (!(rtinfo->flags & IP6T_RT_LEN) ||
108                ((rtinfo->hdrlen == hdrlen) ^
109                 !!(rtinfo->invflags & IP6T_RT_INV_LEN)))
110               &&
111               (!(rtinfo->flags & IP6T_RT_TYP) ||
112                ((rtinfo->rt_type == rh->type) ^
113                 !!(rtinfo->invflags & IP6T_RT_INV_TYP)));
114
115         if (ret && (rtinfo->flags & IP6T_RT_RES)) {
116                 u_int32_t *rp, _reserved;
117                 rp = skb_header_pointer(skb,
118                                         ptr + offsetof(struct rt0_hdr,
119                                                        reserved),
120                                         sizeof(_reserved),
121                                         &_reserved);
122
123                 ret = (*rp == 0);
124         }
125
126         DEBUGP("#%d ", rtinfo->addrnr);
127         if (!(rtinfo->flags & IP6T_RT_FST)) {
128                 return ret;
129         } else if (rtinfo->flags & IP6T_RT_FST_NSTRICT) {
130                 DEBUGP("Not strict ");
131                 if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
132                         DEBUGP("There isn't enough space\n");
133                         return 0;
134                 } else {
135                         unsigned int i = 0;
136
137                         DEBUGP("#%d ", rtinfo->addrnr);
138                         for (temp = 0;
139                              temp < (unsigned int)((hdrlen - 8) / 16);
140                              temp++) {
141                                 ap = skb_header_pointer(skb,
142                                                         ptr
143                                                         + sizeof(struct rt0_hdr)
144                                                         + temp * sizeof(_addr),
145                                                         sizeof(_addr),
146                                                         &_addr);
147
148                                 BUG_ON(ap == NULL);
149
150                                 if (ipv6_addr_equal(ap, &rtinfo->addrs[i])) {
151                                         DEBUGP("i=%d temp=%d;\n", i, temp);
152                                         i++;
153                                 }
154                                 if (i == rtinfo->addrnr)
155                                         break;
156                         }
157                         DEBUGP("i=%d #%d\n", i, rtinfo->addrnr);
158                         if (i == rtinfo->addrnr)
159                                 return ret;
160                         else
161                                 return 0;
162                 }
163         } else {
164                 DEBUGP("Strict ");
165                 if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
166                         DEBUGP("There isn't enough space\n");
167                         return 0;
168                 } else {
169                         DEBUGP("#%d ", rtinfo->addrnr);
170                         for (temp = 0; temp < rtinfo->addrnr; temp++) {
171                                 ap = skb_header_pointer(skb,
172                                                         ptr
173                                                         + sizeof(struct rt0_hdr)
174                                                         + temp * sizeof(_addr),
175                                                         sizeof(_addr),
176                                                         &_addr);
177                                 BUG_ON(ap == NULL);
178
179                                 if (!ipv6_addr_equal(ap, &rtinfo->addrs[temp]))
180                                         break;
181                         }
182                         DEBUGP("temp=%d #%d\n", temp, rtinfo->addrnr);
183                         if ((temp == rtinfo->addrnr) &&
184                             (temp == (unsigned int)((hdrlen - 8) / 16)))
185                                 return ret;
186                         else
187                                 return 0;
188                 }
189         }
190
191         return 0;
192 }
193
194 /* Called when user tries to insert an entry of this type. */
195 static int
196 checkentry(const char *tablename,
197            const void *entry,
198            const struct xt_match *match,
199            void *matchinfo,
200            unsigned int matchinfosize,
201            unsigned int hook_mask)
202 {
203         const struct ip6t_rt *rtinfo = matchinfo;
204
205         if (rtinfo->invflags & ~IP6T_RT_INV_MASK) {
206                 DEBUGP("ip6t_rt: unknown flags %X\n", rtinfo->invflags);
207                 return 0;
208         }
209         if ((rtinfo->flags & (IP6T_RT_RES | IP6T_RT_FST_MASK)) &&
210             (!(rtinfo->flags & IP6T_RT_TYP) ||
211              (rtinfo->rt_type != 0) ||
212              (rtinfo->invflags & IP6T_RT_INV_TYP))) {
213                 DEBUGP("`--rt-type 0' required before `--rt-0-*'");
214                 return 0;
215         }
216
217         return 1;
218 }
219
220 static struct ip6t_match rt_match = {
221         .name           = "rt",
222         .match          = match,
223         .matchsize      = sizeof(struct ip6t_rt),
224         .checkentry     = checkentry,
225         .me             = THIS_MODULE,
226 };
227
228 static int __init ip6t_rt_init(void)
229 {
230         return ip6t_register_match(&rt_match);
231 }
232
233 static void __exit ip6t_rt_fini(void)
234 {
235         ip6t_unregister_match(&rt_match);
236 }
237
238 module_init(ip6t_rt_init);
239 module_exit(ip6t_rt_fini);