1 /* Kernel module to match ROUTING parameters. */
3 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
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.
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>
17 #include <asm/byteorder.h>
19 #include <linux/netfilter_ipv6/ip6_tables.h>
20 #include <linux/netfilter_ipv6/ip6t_rt.h>
22 MODULE_LICENSE("GPL");
23 MODULE_DESCRIPTION("IPv6 RT match");
24 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
29 #define DEBUGP(format, args...)
32 /* Returns 1 if the id is matched by the range, 0 otherwise */
34 segsleft_match(u_int32_t min
, u_int32_t max
, u_int32_t id
, int invert
)
37 DEBUGP("rt segsleft_match:%c 0x%x <= 0x%x <= 0x%x",invert
? '!':' ',
39 r
=(id
>= min
&& id
<= max
) ^ invert
;
40 DEBUGP(" result %s\n",r
? "PASS" : "FAILED");
45 match(const struct sk_buff
*skb
,
46 const struct net_device
*in
,
47 const struct net_device
*out
,
48 const void *matchinfo
,
53 struct ipv6_rt_hdr _route
, *rh
= NULL
;
54 const struct ip6t_rt
*rtinfo
= matchinfo
;
59 unsigned int hdrlen
= 0;
61 struct in6_addr
*ap
, _addr
;
63 /* type of the 1st exthdr */
64 nexthdr
= skb
->nh
.ipv6h
->nexthdr
;
65 /* pointer to the 1st exthdr */
66 ptr
= sizeof(struct ipv6hdr
);
67 /* available length */
71 while (ip6t_ext_hdr(nexthdr
)) {
72 struct ipv6_opt_hdr _hdr
, *hp
;
74 DEBUGP("ipv6_rt header iteration \n");
76 /* Is there enough space for the next ext header? */
77 if (len
< (int)sizeof(struct ipv6_opt_hdr
))
79 /* No more exthdr -> evaluate */
80 if (nexthdr
== NEXTHDR_NONE
) {
84 if (nexthdr
== NEXTHDR_ESP
) {
88 hp
= skb_header_pointer(skb
, ptr
, sizeof(_hdr
), &_hdr
);
91 /* Calculate the header length */
92 if (nexthdr
== NEXTHDR_FRAGMENT
) {
94 } else if (nexthdr
== NEXTHDR_AUTH
)
95 hdrlen
= (hp
->hdrlen
+2)<<2;
97 hdrlen
= ipv6_optlen(hp
);
99 /* ROUTING -> evaluate */
100 if (nexthdr
== NEXTHDR_ROUTING
) {
101 temp
|= MASK_ROUTING
;
109 case NEXTHDR_ROUTING
:
110 case NEXTHDR_FRAGMENT
:
115 DEBUGP("ipv6_rt match: unknown nextheader %u\n",nexthdr
);
120 nexthdr
= hp
->nexthdr
;
123 if ( ptr
> skb
->len
) {
124 DEBUGP("ipv6_rt: new pointer is too large! \n");
129 /* ROUTING header not found */
130 if ( temp
!= MASK_ROUTING
) return 0;
132 if (len
< (int)sizeof(struct ipv6_rt_hdr
)){
138 /* Pcket smaller than its length field */
142 rh
= skb_header_pointer(skb
, ptr
, sizeof(_route
), &_route
);
145 DEBUGP("IPv6 RT LEN %u %u ", hdrlen
, rh
->hdrlen
);
146 DEBUGP("TYPE %04X ", rh
->type
);
147 DEBUGP("SGS_LEFT %u %02X\n", rh
->segments_left
, rh
->segments_left
);
149 DEBUGP("IPv6 RT segsleft %02X ",
150 (segsleft_match(rtinfo
->segsleft
[0], rtinfo
->segsleft
[1],
152 !!(rtinfo
->invflags
& IP6T_RT_INV_SGS
))));
153 DEBUGP("type %02X %02X %02X ",
154 rtinfo
->rt_type
, rh
->type
,
155 (!(rtinfo
->flags
& IP6T_RT_TYP
) ||
156 ((rtinfo
->rt_type
== rh
->type
) ^
157 !!(rtinfo
->invflags
& IP6T_RT_INV_TYP
))));
158 DEBUGP("len %02X %04X %02X ",
159 rtinfo
->hdrlen
, hdrlen
,
160 (!(rtinfo
->flags
& IP6T_RT_LEN
) ||
161 ((rtinfo
->hdrlen
== hdrlen
) ^
162 !!(rtinfo
->invflags
& IP6T_RT_INV_LEN
))));
163 DEBUGP("res %02X %02X %02X ",
164 (rtinfo
->flags
& IP6T_RT_RES
), ((struct rt0_hdr
*)rh
)->bitmap
,
165 !((rtinfo
->flags
& IP6T_RT_RES
) && (((struct rt0_hdr
*)rh
)->bitmap
)));
169 (segsleft_match(rtinfo
->segsleft
[0], rtinfo
->segsleft
[1],
171 !!(rtinfo
->invflags
& IP6T_RT_INV_SGS
)))
173 (!(rtinfo
->flags
& IP6T_RT_LEN
) ||
174 ((rtinfo
->hdrlen
== hdrlen
) ^
175 !!(rtinfo
->invflags
& IP6T_RT_INV_LEN
)))
177 (!(rtinfo
->flags
& IP6T_RT_TYP
) ||
178 ((rtinfo
->rt_type
== rh
->type
) ^
179 !!(rtinfo
->invflags
& IP6T_RT_INV_TYP
)));
181 if (ret
&& (rtinfo
->flags
& IP6T_RT_RES
)) {
182 u_int32_t
*bp
, _bitmap
;
183 bp
= skb_header_pointer(skb
,
184 ptr
+ offsetof(struct rt0_hdr
, bitmap
),
185 sizeof(_bitmap
), &_bitmap
);
190 DEBUGP("#%d ",rtinfo
->addrnr
);
191 if ( !(rtinfo
->flags
& IP6T_RT_FST
) ){
193 } else if (rtinfo
->flags
& IP6T_RT_FST_NSTRICT
) {
194 DEBUGP("Not strict ");
195 if ( rtinfo
->addrnr
> (unsigned int)((hdrlen
-8)/16) ){
196 DEBUGP("There isn't enough space\n");
201 DEBUGP("#%d ",rtinfo
->addrnr
);
202 for(temp
=0; temp
<(unsigned int)((hdrlen
-8)/16); temp
++){
203 ap
= skb_header_pointer(skb
,
205 + sizeof(struct rt0_hdr
)
206 + temp
* sizeof(_addr
),
212 if (ipv6_addr_equal(ap
, &rtinfo
->addrs
[i
])) {
213 DEBUGP("i=%d temp=%d;\n",i
,temp
);
216 if (i
==rtinfo
->addrnr
) break;
218 DEBUGP("i=%d #%d\n", i
, rtinfo
->addrnr
);
219 if (i
== rtinfo
->addrnr
)
225 if ( rtinfo
->addrnr
> (unsigned int)((hdrlen
-8)/16) ){
226 DEBUGP("There isn't enough space\n");
229 DEBUGP("#%d ",rtinfo
->addrnr
);
230 for(temp
=0; temp
<rtinfo
->addrnr
; temp
++){
231 ap
= skb_header_pointer(skb
,
233 + sizeof(struct rt0_hdr
)
234 + temp
* sizeof(_addr
),
239 if (!ipv6_addr_equal(ap
, &rtinfo
->addrs
[temp
]))
242 DEBUGP("temp=%d #%d\n", temp
, rtinfo
->addrnr
);
243 if ((temp
== rtinfo
->addrnr
) && (temp
== (unsigned int)((hdrlen
-8)/16)))
252 /* Called when user tries to insert an entry of this type. */
254 checkentry(const char *tablename
,
255 const struct ip6t_ip6
*ip
,
257 unsigned int matchinfosize
,
258 unsigned int hook_mask
)
260 const struct ip6t_rt
*rtinfo
= matchinfo
;
262 if (matchinfosize
!= IP6T_ALIGN(sizeof(struct ip6t_rt
))) {
263 DEBUGP("ip6t_rt: matchsize %u != %u\n",
264 matchinfosize
, IP6T_ALIGN(sizeof(struct ip6t_rt
)));
267 if (rtinfo
->invflags
& ~IP6T_RT_INV_MASK
) {
268 DEBUGP("ip6t_rt: unknown flags %X\n",
272 if ( (rtinfo
->flags
& (IP6T_RT_RES
|IP6T_RT_FST_MASK
)) &&
273 (!(rtinfo
->flags
& IP6T_RT_TYP
) ||
274 (rtinfo
->rt_type
!= 0) ||
275 (rtinfo
->invflags
& IP6T_RT_INV_TYP
)) ) {
276 DEBUGP("`--rt-type 0' required before `--rt-0-*'");
283 static struct ip6t_match rt_match
= {
286 .checkentry
= &checkentry
,
290 static int __init
init(void)
292 return ip6t_register_match(&rt_match
);
295 static void __exit
cleanup(void)
297 ip6t_unregister_match(&rt_match
);
301 module_exit(cleanup
);