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 | */ |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 24 | MODULE_DESCRIPTION("Xtables: IPv6 Routing Header match"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>"); |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /* Returns 1 if the id is matched by the range, 0 otherwise */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 28 | static inline bool |
| 29 | segsleft_match(u_int32_t min, u_int32_t max, u_int32_t id, bool invert) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 31 | bool r; |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 32 | pr_debug("segsleft_match:%c 0x%x <= 0x%x <= 0x%x\n", |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 33 | invert ? '!' : ' ', min, id, max); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 34 | r = (id >= min && id <= max) ^ invert; |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 35 | pr_debug(" result %s\n", r ? "PASS" : "FAILED"); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 36 | return r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 39 | static bool rt_mt6(const struct sk_buff *skb, const struct xt_match_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 41 | struct ipv6_rt_hdr _route; |
| 42 | const struct ipv6_rt_hdr *rh; |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 43 | const struct ip6t_rt *rtinfo = par->matchinfo; |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 44 | unsigned int temp; |
| 45 | unsigned int ptr; |
| 46 | unsigned int hdrlen = 0; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 47 | bool ret = false; |
Jan Engelhardt | a47362a | 2007-07-07 22:16:55 -0700 | [diff] [blame] | 48 | struct in6_addr _addr; |
| 49 | const struct in6_addr *ap; |
Patrick McHardy | 6d38163 | 2006-10-24 16:15:10 -0700 | [diff] [blame] | 50 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Patrick McHardy | 6d38163 | 2006-10-24 16:15:10 -0700 | [diff] [blame] | 52 | err = ipv6_find_hdr(skb, &ptr, NEXTHDR_ROUTING, NULL); |
| 53 | if (err < 0) { |
| 54 | if (err != -ENOENT) |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 55 | *par->hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 56 | return false; |
Patrick McHardy | 6d38163 | 2006-10-24 16:15:10 -0700 | [diff] [blame] | 57 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 59 | rh = skb_header_pointer(skb, ptr, sizeof(_route), &_route); |
| 60 | if (rh == NULL) { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 61 | *par->hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 62 | return false; |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 63 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 65 | hdrlen = ipv6_optlen(rh); |
| 66 | if (skb->len - ptr < hdrlen) { |
| 67 | /* Pcket smaller than its length field */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 68 | return false; |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 69 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 71 | pr_debug("IPv6 RT LEN %u %u ", hdrlen, rh->hdrlen); |
| 72 | pr_debug("TYPE %04X ", rh->type); |
| 73 | pr_debug("SGS_LEFT %u %02X\n", rh->segments_left, rh->segments_left); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 75 | pr_debug("IPv6 RT segsleft %02X ", |
| 76 | segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1], |
| 77 | rh->segments_left, |
| 78 | !!(rtinfo->invflags & IP6T_RT_INV_SGS))); |
| 79 | pr_debug("type %02X %02X %02X ", |
| 80 | rtinfo->rt_type, rh->type, |
| 81 | (!(rtinfo->flags & IP6T_RT_TYP) || |
| 82 | ((rtinfo->rt_type == rh->type) ^ |
| 83 | !!(rtinfo->invflags & IP6T_RT_INV_TYP)))); |
| 84 | pr_debug("len %02X %04X %02X ", |
| 85 | rtinfo->hdrlen, hdrlen, |
| 86 | !(rtinfo->flags & IP6T_RT_LEN) || |
| 87 | ((rtinfo->hdrlen == hdrlen) ^ |
| 88 | !!(rtinfo->invflags & IP6T_RT_INV_LEN))); |
| 89 | pr_debug("res %02X %02X %02X ", |
| 90 | rtinfo->flags & IP6T_RT_RES, |
| 91 | ((const struct rt0_hdr *)rh)->reserved, |
| 92 | !((rtinfo->flags & IP6T_RT_RES) && |
| 93 | (((const struct rt0_hdr *)rh)->reserved))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 95 | ret = (rh != NULL) && |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 96 | (segsleft_match(rtinfo->segsleft[0], rtinfo->segsleft[1], |
| 97 | rh->segments_left, |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 98 | !!(rtinfo->invflags & IP6T_RT_INV_SGS))) && |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 99 | (!(rtinfo->flags & IP6T_RT_LEN) || |
| 100 | ((rtinfo->hdrlen == hdrlen) ^ |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 101 | !!(rtinfo->invflags & IP6T_RT_INV_LEN))) && |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 102 | (!(rtinfo->flags & IP6T_RT_TYP) || |
| 103 | ((rtinfo->rt_type == rh->type) ^ |
| 104 | !!(rtinfo->invflags & IP6T_RT_INV_TYP))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | if (ret && (rtinfo->flags & IP6T_RT_RES)) { |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 107 | const u_int32_t *rp; |
| 108 | u_int32_t _reserved; |
Brian Haley | e6df439 | 2005-09-10 00:15:06 -0700 | [diff] [blame] | 109 | rp = skb_header_pointer(skb, |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 110 | ptr + offsetof(struct rt0_hdr, |
| 111 | reserved), |
| 112 | sizeof(_reserved), |
| 113 | &_reserved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Brian Haley | e6df439 | 2005-09-10 00:15:06 -0700 | [diff] [blame] | 115 | ret = (*rp == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 118 | pr_debug("#%d ", rtinfo->addrnr); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 119 | if (!(rtinfo->flags & IP6T_RT_FST)) { |
| 120 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } else if (rtinfo->flags & IP6T_RT_FST_NSTRICT) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 122 | pr_debug("Not strict "); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 123 | if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 124 | pr_debug("There isn't enough space\n"); |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 125 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } else { |
| 127 | unsigned int i = 0; |
| 128 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 129 | pr_debug("#%d ", rtinfo->addrnr); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 130 | for (temp = 0; |
| 131 | temp < (unsigned int)((hdrlen - 8) / 16); |
| 132 | temp++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | ap = skb_header_pointer(skb, |
| 134 | ptr |
| 135 | + sizeof(struct rt0_hdr) |
| 136 | + temp * sizeof(_addr), |
| 137 | sizeof(_addr), |
| 138 | &_addr); |
| 139 | |
| 140 | BUG_ON(ap == NULL); |
| 141 | |
| 142 | if (ipv6_addr_equal(ap, &rtinfo->addrs[i])) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 143 | pr_debug("i=%d temp=%d;\n", i, temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | i++; |
| 145 | } |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 146 | if (i == rtinfo->addrnr) |
| 147 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 149 | pr_debug("i=%d #%d\n", i, rtinfo->addrnr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | if (i == rtinfo->addrnr) |
| 151 | return ret; |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 152 | else |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 153 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | } else { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 156 | pr_debug("Strict "); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 157 | if (rtinfo->addrnr > (unsigned int)((hdrlen - 8) / 16)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 158 | pr_debug("There isn't enough space\n"); |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 159 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } else { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 161 | pr_debug("#%d ", rtinfo->addrnr); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 162 | for (temp = 0; temp < rtinfo->addrnr; temp++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | ap = skb_header_pointer(skb, |
| 164 | ptr |
| 165 | + sizeof(struct rt0_hdr) |
| 166 | + temp * sizeof(_addr), |
| 167 | sizeof(_addr), |
| 168 | &_addr); |
| 169 | BUG_ON(ap == NULL); |
| 170 | |
| 171 | if (!ipv6_addr_equal(ap, &rtinfo->addrs[temp])) |
| 172 | break; |
| 173 | } |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 174 | pr_debug("temp=%d #%d\n", temp, rtinfo->addrnr); |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 175 | if (temp == rtinfo->addrnr && |
| 176 | temp == (unsigned int)((hdrlen - 8) / 16)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return ret; |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 178 | else |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 179 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 183 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame^] | 186 | static int rt_mt6_check(const struct xt_mtchk_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 188 | const struct ip6t_rt *rtinfo = par->matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 190 | if (rtinfo->invflags & ~IP6T_RT_INV_MASK) { |
Jan Engelhardt | ff67e4e | 2010-03-19 21:08:16 +0100 | [diff] [blame] | 191 | pr_debug("unknown flags %X\n", rtinfo->invflags); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 192 | return false; |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 193 | } |
| 194 | if ((rtinfo->flags & (IP6T_RT_RES | IP6T_RT_FST_MASK)) && |
| 195 | (!(rtinfo->flags & IP6T_RT_TYP) || |
| 196 | (rtinfo->rt_type != 0) || |
| 197 | (rtinfo->invflags & IP6T_RT_INV_TYP))) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 198 | pr_debug("`--rt-type 0' required before `--rt-0-*'"); |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 199 | return false; |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 202 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 205 | static struct xt_match rt_mt6_reg __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | .name = "rt", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 207 | .family = NFPROTO_IPV6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 208 | .match = rt_mt6, |
Patrick McHardy | 7f93971 | 2006-03-20 18:01:43 -0800 | [diff] [blame] | 209 | .matchsize = sizeof(struct ip6t_rt), |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 210 | .checkentry = rt_mt6_check, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | .me = THIS_MODULE, |
| 212 | }; |
| 213 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 214 | static int __init rt_mt6_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 216 | return xt_register_match(&rt_mt6_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 219 | static void __exit rt_mt6_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 221 | xt_unregister_match(&rt_mt6_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 224 | module_init(rt_mt6_init); |
| 225 | module_exit(rt_mt6_exit); |