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