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/x_tables.h>
20 #include <linux/netfilter_ipv6/ip6_tables.h>
21 #include <linux/netfilter_ipv6/ip6t_rt.h>
23 MODULE_LICENSE("GPL");
24 MODULE_DESCRIPTION("IPv6 RT match");
25 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
30 #define DEBUGP(format, args...)
33 /* Returns 1 if the id is matched by the range, 0 otherwise */
35 segsleft_match(u_int32_t min, u_int32_t max, u_int32_t id, bool invert)
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");
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,
55 struct ipv6_rt_hdr _route;
56 const struct ipv6_rt_hdr *rh;
57 const struct ip6t_rt *rtinfo = matchinfo;
60 unsigned int hdrlen = 0;
62 struct in6_addr _addr;
63 const struct in6_addr *ap;
66 err = ipv6_find_hdr(skb, &ptr, NEXTHDR_ROUTING, NULL);
73 rh = skb_header_pointer(skb, ptr, sizeof(_route), &_route);
79 hdrlen = ipv6_optlen(rh);
80 if (skb->len - ptr < hdrlen) {
81 /* Pcket smaller than its length field */
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);
89 DEBUGP("IPv6 RT segsleft %02X ",
90 segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1],
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)));
111 (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1],
113 !!(rtinfo->invflags & IP6T_RT_INV_SGS)))
115 (!(rtinfo->flags & IP6T_RT_LEN) ||
116 ((rtinfo->hdrlen == hdrlen) ^
117 !!(rtinfo->invflags & IP6T_RT_INV_LEN)))
119 (!(rtinfo->flags & IP6T_RT_TYP) ||
120 ((rtinfo->rt_type == rh->type) ^
121 !!(rtinfo->invflags & IP6T_RT_INV_TYP)));
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,
134 DEBUGP("#%d ", rtinfo->addrnr);
135 if (!(rtinfo->flags & IP6T_RT_FST)) {
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");
145 DEBUGP("#%d ", rtinfo->addrnr);
147 temp < (unsigned int)((hdrlen - 8) / 16);
149 ap = skb_header_pointer(skb,
151 + sizeof(struct rt0_hdr)
152 + temp * sizeof(_addr),
158 if (ipv6_addr_equal(ap, &rtinfo->addrs[i])) {
159 DEBUGP("i=%d temp=%d;\n", i, temp);
162 if (i == rtinfo->addrnr)
165 DEBUGP("i=%d #%d\n", i, rtinfo->addrnr);
166 if (i == rtinfo->addrnr)
173 if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) {
174 DEBUGP("There isn't enough space\n");
177 DEBUGP("#%d ", rtinfo->addrnr);
178 for (temp = 0; temp < rtinfo->addrnr; temp++) {
179 ap = skb_header_pointer(skb,
181 + sizeof(struct rt0_hdr)
182 + temp * sizeof(_addr),
187 if (!ipv6_addr_equal(ap, &rtinfo->addrs[temp]))
190 DEBUGP("temp=%d #%d\n", temp, rtinfo->addrnr);
191 if (temp == rtinfo->addrnr &&
192 temp == (unsigned int)((hdrlen - 8) / 16))
202 /* Called when user tries to insert an entry of this type. */
204 checkentry(const char *tablename,
206 const struct xt_match *match,
208 unsigned int hook_mask)
210 const struct ip6t_rt *rtinfo = matchinfo;
212 if (rtinfo->invflags & ~IP6T_RT_INV_MASK) {
213 DEBUGP("ip6t_rt: unknown flags %X\n", rtinfo->invflags);
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-*'");
227 static struct xt_match rt_match = {
231 .matchsize = sizeof(struct ip6t_rt),
232 .checkentry = checkentry,
236 static int __init ip6t_rt_init(void)
238 return xt_register_match(&rt_match);
241 static void __exit ip6t_rt_fini(void)
243 xt_unregister_match(&rt_match);
246 module_init(ip6t_rt_init);
247 module_exit(ip6t_rt_fini);