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