Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame^] | 19 | #include <linux/netfilter/x_tables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 int |
| 35 | segsleft_match(u_int32_t min, u_int32_t max, u_int32_t id, int invert) |
| 36 | { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 37 | int r = 0; |
| 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static int |
| 46 | match(const struct sk_buff *skb, |
| 47 | const struct net_device *in, |
| 48 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 49 | const struct xt_match *match, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | const void *matchinfo, |
| 51 | int offset, |
| 52 | unsigned int protoff, |
| 53 | int *hotdrop) |
| 54 | { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 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 | unsigned int ret = 0; |
| 61 | struct in6_addr *ap, _addr; |
Patrick McHardy | 6d38163 | 2006-10-24 16:15:10 -0700 | [diff] [blame] | 62 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Patrick McHardy | 6d38163 | 2006-10-24 16:15:10 -0700 | [diff] [blame] | 64 | err = ipv6_find_hdr(skb, &ptr, NEXTHDR_ROUTING, NULL); |
| 65 | if (err < 0) { |
| 66 | if (err != -ENOENT) |
| 67 | *hotdrop = 1; |
Yasuyuki Kozakai | e674d0f | 2005-09-19 15:34:40 -0700 | [diff] [blame] | 68 | return 0; |
Patrick McHardy | 6d38163 | 2006-10-24 16:15:10 -0700 | [diff] [blame] | 69 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 71 | rh = skb_header_pointer(skb, ptr, sizeof(_route), &_route); |
| 72 | if (rh == NULL) { |
| 73 | *hotdrop = 1; |
| 74 | return 0; |
| 75 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 77 | hdrlen = ipv6_optlen(rh); |
| 78 | if (skb->len - ptr < hdrlen) { |
| 79 | /* Pcket smaller than its length field */ |
| 80 | return 0; |
| 81 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 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))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 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))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | if (ret && (rtinfo->flags & IP6T_RT_RES)) { |
Brian Haley | e6df439 | 2005-09-10 00:15:06 -0700 | [diff] [blame] | 122 | u_int32_t *rp, _reserved; |
| 123 | rp = skb_header_pointer(skb, |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 124 | ptr + offsetof(struct rt0_hdr, |
| 125 | reserved), |
| 126 | sizeof(_reserved), |
| 127 | &_reserved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Brian Haley | e6df439 | 2005-09-10 00:15:06 -0700 | [diff] [blame] | 129 | ret = (*rp == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 132 | DEBUGP("#%d ", rtinfo->addrnr); |
| 133 | if (!(rtinfo->flags & IP6T_RT_FST)) { |
| 134 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } else if (rtinfo->flags & IP6T_RT_FST_NSTRICT) { |
| 136 | DEBUGP("Not strict "); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 137 | if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | DEBUGP("There isn't enough space\n"); |
| 139 | return 0; |
| 140 | } else { |
| 141 | unsigned int i = 0; |
| 142 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 143 | DEBUGP("#%d ", rtinfo->addrnr); |
| 144 | for (temp = 0; |
| 145 | temp < (unsigned int)((hdrlen - 8) / 16); |
| 146 | temp++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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])) { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 157 | DEBUGP("i=%d temp=%d;\n", i, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | i++; |
| 159 | } |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 160 | if (i == rtinfo->addrnr) |
| 161 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | DEBUGP("i=%d #%d\n", i, rtinfo->addrnr); |
| 164 | if (i == rtinfo->addrnr) |
| 165 | return ret; |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 166 | else |
| 167 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | } else { |
| 170 | DEBUGP("Strict "); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 171 | if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | DEBUGP("There isn't enough space\n"); |
| 173 | return 0; |
| 174 | } else { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 175 | DEBUGP("#%d ", rtinfo->addrnr); |
| 176 | for (temp = 0; temp < rtinfo->addrnr; temp++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 189 | if ((temp == rtinfo->addrnr) && |
| 190 | (temp == (unsigned int)((hdrlen - 8) / 16))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | return ret; |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 192 | else |
| 193 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /* Called when user tries to insert an entry of this type. */ |
| 201 | static int |
| 202 | checkentry(const char *tablename, |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 203 | const void *entry, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 204 | const struct xt_match *match, |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 205 | void *matchinfo, |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 206 | unsigned int hook_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 208 | const struct ip6t_rt *rtinfo = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 210 | if (rtinfo->invflags & ~IP6T_RT_INV_MASK) { |
| 211 | DEBUGP("ip6t_rt: unknown flags %X\n", rtinfo->invflags); |
| 212 | return 0; |
| 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 0; |
| 220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 222 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame^] | 225 | static struct xt_match rt_match = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | .name = "rt", |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame^] | 227 | .family = AF_INET6, |
Patrick McHardy | 7f93971 | 2006-03-20 18:01:43 -0800 | [diff] [blame] | 228 | .match = match, |
| 229 | .matchsize = sizeof(struct ip6t_rt), |
| 230 | .checkentry = checkentry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | .me = THIS_MODULE, |
| 232 | }; |
| 233 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 234 | static int __init ip6t_rt_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | { |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame^] | 236 | return xt_register_match(&rt_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 239 | static void __exit ip6t_rt_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | { |
Jan Engelhardt | 6709dbb | 2007-02-07 15:11:19 -0800 | [diff] [blame^] | 241 | xt_unregister_match(&rt_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 244 | module_init(ip6t_rt_init); |
| 245 | module_exit(ip6t_rt_fini); |