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