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 | |
| 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 | { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 36 | int r = 0; |
| 37 | DEBUGP("rt segsleft_match:%c 0x%x <= 0x%x <= 0x%x", |
| 38 | invert ? '!' : ' ', min, id, max); |
| 39 | r = (id >= min && id <= max) ^ invert; |
| 40 | DEBUGP(" result %s\n", r ? "PASS" : "FAILED"); |
| 41 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Patrick McHardy | b777e0c | 2006-01-05 12:21:16 -0800 | [diff] [blame] | 61 | if (ipv6_find_hdr(skb, &ptr, NEXTHDR_ROUTING, NULL) < 0) |
Yasuyuki Kozakai | e674d0f | 2005-09-19 15:34:40 -0700 | [diff] [blame] | 62 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 64 | rh = skb_header_pointer(skb, ptr, sizeof(_route), &_route); |
| 65 | if (rh == NULL) { |
| 66 | *hotdrop = 1; |
| 67 | return 0; |
| 68 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 70 | hdrlen = ipv6_optlen(rh); |
| 71 | if (skb->len - ptr < hdrlen) { |
| 72 | /* Pcket smaller than its length field */ |
| 73 | return 0; |
| 74 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 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), |
| 96 | ((struct rt0_hdr *)rh)->reserved, |
| 97 | !((rtinfo->flags & IP6T_RT_RES) && |
| 98 | (((struct rt0_hdr *)rh)->reserved))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 100 | ret = (rh != NULL) |
| 101 | && |
| 102 | (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1], |
| 103 | rh->segments_left, |
| 104 | !!(rtinfo->invflags & IP6T_RT_INV_SGS))) |
| 105 | && |
| 106 | (!(rtinfo->flags & IP6T_RT_LEN) || |
| 107 | ((rtinfo->hdrlen == hdrlen) ^ |
| 108 | !!(rtinfo->invflags & IP6T_RT_INV_LEN))) |
| 109 | && |
| 110 | (!(rtinfo->flags & IP6T_RT_TYP) || |
| 111 | ((rtinfo->rt_type == rh->type) ^ |
| 112 | !!(rtinfo->invflags & IP6T_RT_INV_TYP))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | if (ret && (rtinfo->flags & IP6T_RT_RES)) { |
Brian Haley | e6df439 | 2005-09-10 00:15:06 -0700 | [diff] [blame] | 115 | u_int32_t *rp, _reserved; |
| 116 | rp = skb_header_pointer(skb, |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 117 | ptr + offsetof(struct rt0_hdr, |
| 118 | reserved), |
| 119 | sizeof(_reserved), |
| 120 | &_reserved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Brian Haley | e6df439 | 2005-09-10 00:15:06 -0700 | [diff] [blame] | 122 | ret = (*rp == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 125 | DEBUGP("#%d ", rtinfo->addrnr); |
| 126 | if (!(rtinfo->flags & IP6T_RT_FST)) { |
| 127 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } else if (rtinfo->flags & IP6T_RT_FST_NSTRICT) { |
| 129 | DEBUGP("Not strict "); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 130 | if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | DEBUGP("There isn't enough space\n"); |
| 132 | return 0; |
| 133 | } else { |
| 134 | unsigned int i = 0; |
| 135 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 136 | DEBUGP("#%d ", rtinfo->addrnr); |
| 137 | for (temp = 0; |
| 138 | temp < (unsigned int)((hdrlen - 8) / 16); |
| 139 | temp++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | ap = skb_header_pointer(skb, |
| 141 | ptr |
| 142 | + sizeof(struct rt0_hdr) |
| 143 | + temp * sizeof(_addr), |
| 144 | sizeof(_addr), |
| 145 | &_addr); |
| 146 | |
| 147 | BUG_ON(ap == NULL); |
| 148 | |
| 149 | if (ipv6_addr_equal(ap, &rtinfo->addrs[i])) { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 150 | DEBUGP("i=%d temp=%d;\n", i, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | i++; |
| 152 | } |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 153 | if (i == rtinfo->addrnr) |
| 154 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | } |
| 156 | DEBUGP("i=%d #%d\n", i, rtinfo->addrnr); |
| 157 | if (i == rtinfo->addrnr) |
| 158 | return ret; |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 159 | else |
| 160 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | } else { |
| 163 | DEBUGP("Strict "); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 164 | if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | DEBUGP("There isn't enough space\n"); |
| 166 | return 0; |
| 167 | } else { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 168 | DEBUGP("#%d ", rtinfo->addrnr); |
| 169 | for (temp = 0; temp < rtinfo->addrnr; temp++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | ap = skb_header_pointer(skb, |
| 171 | ptr |
| 172 | + sizeof(struct rt0_hdr) |
| 173 | + temp * sizeof(_addr), |
| 174 | sizeof(_addr), |
| 175 | &_addr); |
| 176 | BUG_ON(ap == NULL); |
| 177 | |
| 178 | if (!ipv6_addr_equal(ap, &rtinfo->addrs[temp])) |
| 179 | break; |
| 180 | } |
| 181 | DEBUGP("temp=%d #%d\n", temp, rtinfo->addrnr); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 182 | if ((temp == rtinfo->addrnr) && |
| 183 | (temp == (unsigned int)((hdrlen - 8) / 16))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | return ret; |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 185 | else |
| 186 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | /* Called when user tries to insert an entry of this type. */ |
| 194 | static int |
| 195 | checkentry(const char *tablename, |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 196 | const void *entry, |
| 197 | void *matchinfo, |
| 198 | unsigned int matchinfosize, |
| 199 | unsigned int hook_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 201 | const struct ip6t_rt *rtinfo = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 203 | if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_rt))) { |
| 204 | DEBUGP("ip6t_rt: matchsize %u != %u\n", |
| 205 | matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_rt))); |
| 206 | return 0; |
| 207 | } |
| 208 | if (rtinfo->invflags & ~IP6T_RT_INV_MASK) { |
| 209 | DEBUGP("ip6t_rt: unknown flags %X\n", rtinfo->invflags); |
| 210 | return 0; |
| 211 | } |
| 212 | if ((rtinfo->flags & (IP6T_RT_RES | IP6T_RT_FST_MASK)) && |
| 213 | (!(rtinfo->flags & IP6T_RT_TYP) || |
| 214 | (rtinfo->rt_type != 0) || |
| 215 | (rtinfo->invflags & IP6T_RT_INV_TYP))) { |
| 216 | DEBUGP("`--rt-type 0' required before `--rt-0-*'"); |
| 217 | return 0; |
| 218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 220 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static struct ip6t_match rt_match = { |
| 224 | .name = "rt", |
| 225 | .match = &match, |
| 226 | .checkentry = &checkentry, |
| 227 | .me = THIS_MODULE, |
| 228 | }; |
| 229 | |
| 230 | static int __init init(void) |
| 231 | { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 232 | return ip6t_register_match(&rt_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static void __exit cleanup(void) |
| 236 | { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 237 | ip6t_unregister_match(&rt_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | module_init(init); |
| 241 | module_exit(cleanup); |