Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Kernel module to match Hop-by-Hop and Destination 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_opts.h> |
| 21 | |
| 22 | #define HOPBYHOP 0 |
| 23 | |
| 24 | MODULE_LICENSE("GPL"); |
| 25 | #if HOPBYHOP |
| 26 | MODULE_DESCRIPTION("IPv6 HbH match"); |
| 27 | #else |
| 28 | MODULE_DESCRIPTION("IPv6 DST match"); |
| 29 | #endif |
| 30 | MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>"); |
| 31 | |
| 32 | #if 0 |
| 33 | #define DEBUGP printk |
| 34 | #else |
| 35 | #define DEBUGP(format, args...) |
| 36 | #endif |
| 37 | |
| 38 | /* |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 39 | * (Type & 0xC0) >> 6 |
| 40 | * 0 -> ignorable |
| 41 | * 1 -> must drop the packet |
| 42 | * 2 -> send ICMP PARM PROB regardless and drop packet |
| 43 | * 3 -> Send ICMP if not a multicast address and drop packet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * (Type & 0x20) >> 5 |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 45 | * 0 -> invariant |
| 46 | * 1 -> can change the routing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | * (Type & 0x1F) Type |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 48 | * 0 -> Pad1 (only 1 byte!) |
| 49 | * 1 -> PadN LENGTH info (total length = length + 2) |
| 50 | * C0 | 2 -> JUMBO 4 x x x x ( xxxx > 64k ) |
| 51 | * 5 -> RTALERT 2 x x |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | */ |
| 53 | |
| 54 | static int |
| 55 | match(const struct sk_buff *skb, |
| 56 | const struct net_device *in, |
| 57 | const struct net_device *out, |
| 58 | const void *matchinfo, |
| 59 | int offset, |
| 60 | unsigned int protoff, |
| 61 | int *hotdrop) |
| 62 | { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 63 | struct ipv6_opt_hdr _optsh, *oh; |
| 64 | const struct ip6t_opts *optinfo = matchinfo; |
| 65 | unsigned int temp; |
| 66 | unsigned int ptr; |
| 67 | unsigned int hdrlen = 0; |
| 68 | unsigned int ret = 0; |
| 69 | u8 _opttype, *tp = NULL; |
| 70 | u8 _optlen, *lp = NULL; |
| 71 | unsigned int optlen; |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | #if HOPBYHOP |
Patrick McHardy | b777e0c | 2006-01-05 12:21:16 -0800 | [diff] [blame] | 74 | if (ipv6_find_hdr(skb, &ptr, NEXTHDR_HOP, NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | #else |
Patrick McHardy | b777e0c | 2006-01-05 12:21:16 -0800 | [diff] [blame] | 76 | if (ipv6_find_hdr(skb, &ptr, NEXTHDR_DEST, NULL) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | #endif |
Yasuyuki Kozakai | e674d0f | 2005-09-19 15:34:40 -0700 | [diff] [blame] | 78 | return 0; |
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 | oh = skb_header_pointer(skb, ptr, sizeof(_optsh), &_optsh); |
| 81 | if (oh == NULL) { |
| 82 | *hotdrop = 1; |
| 83 | return 0; |
| 84 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 86 | hdrlen = ipv6_optlen(oh); |
| 87 | if (skb->len - ptr < hdrlen) { |
| 88 | /* Packet smaller than it's length field */ |
| 89 | return 0; |
| 90 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 92 | DEBUGP("IPv6 OPTS LEN %u %u ", hdrlen, oh->hdrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 94 | DEBUGP("len %02X %04X %02X ", |
| 95 | optinfo->hdrlen, hdrlen, |
| 96 | (!(optinfo->flags & IP6T_OPTS_LEN) || |
| 97 | ((optinfo->hdrlen == hdrlen) ^ |
| 98 | !!(optinfo->invflags & IP6T_OPTS_INV_LEN)))); |
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 = (oh != NULL) && |
| 101 | (!(optinfo->flags & IP6T_OPTS_LEN) || |
| 102 | ((optinfo->hdrlen == hdrlen) ^ |
| 103 | !!(optinfo->invflags & IP6T_OPTS_INV_LEN))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 105 | ptr += 2; |
| 106 | hdrlen -= 2; |
| 107 | if (!(optinfo->flags & IP6T_OPTS_OPTS)) { |
| 108 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } else if (optinfo->flags & IP6T_OPTS_NSTRICT) { |
| 110 | DEBUGP("Not strict - not implemented"); |
| 111 | } else { |
| 112 | DEBUGP("Strict "); |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 113 | DEBUGP("#%d ", optinfo->optsnr); |
| 114 | for (temp = 0; temp < optinfo->optsnr; temp++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | /* type field exists ? */ |
| 116 | if (hdrlen < 1) |
| 117 | break; |
| 118 | tp = skb_header_pointer(skb, ptr, sizeof(_opttype), |
| 119 | &_opttype); |
| 120 | if (tp == NULL) |
| 121 | break; |
| 122 | |
| 123 | /* Type check */ |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 124 | if (*tp != (optinfo->opts[temp] & 0xFF00) >> 8) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | DEBUGP("Tbad %02X %02X\n", |
| 126 | *tp, |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 127 | (optinfo->opts[temp] & 0xFF00) >> 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | return 0; |
| 129 | } else { |
| 130 | DEBUGP("Tok "); |
| 131 | } |
| 132 | /* Length check */ |
| 133 | if (*tp) { |
| 134 | u16 spec_len; |
| 135 | |
| 136 | /* length field exists ? */ |
| 137 | if (hdrlen < 2) |
| 138 | break; |
| 139 | lp = skb_header_pointer(skb, ptr + 1, |
| 140 | sizeof(_optlen), |
| 141 | &_optlen); |
| 142 | if (lp == NULL) |
| 143 | break; |
| 144 | spec_len = optinfo->opts[temp] & 0x00FF; |
| 145 | |
| 146 | if (spec_len != 0x00FF && spec_len != *lp) { |
| 147 | DEBUGP("Lbad %02X %04X\n", *lp, |
| 148 | spec_len); |
| 149 | return 0; |
| 150 | } |
| 151 | DEBUGP("Lok "); |
| 152 | optlen = *lp + 2; |
| 153 | } else { |
| 154 | DEBUGP("Pad1\n"); |
| 155 | optlen = 1; |
| 156 | } |
| 157 | |
| 158 | /* Step to the next */ |
| 159 | DEBUGP("len%04X \n", optlen); |
| 160 | |
| 161 | if ((ptr > skb->len - optlen || hdrlen < optlen) && |
| 162 | (temp < optinfo->optsnr - 1)) { |
| 163 | DEBUGP("new pointer is too large! \n"); |
| 164 | break; |
| 165 | } |
| 166 | ptr += optlen; |
| 167 | hdrlen -= optlen; |
| 168 | } |
| 169 | if (temp == optinfo->optsnr) |
| 170 | return ret; |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 171 | else |
| 172 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* Called when user tries to insert an entry of this type. */ |
| 179 | static int |
| 180 | checkentry(const char *tablename, |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 181 | const void *info, |
| 182 | void *matchinfo, |
| 183 | unsigned int matchinfosize, |
| 184 | unsigned int hook_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 186 | const struct ip6t_opts *optsinfo = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 188 | if (optsinfo->invflags & ~IP6T_OPTS_INV_MASK) { |
| 189 | DEBUGP("ip6t_opts: unknown flags %X\n", optsinfo->invflags); |
| 190 | return 0; |
| 191 | } |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 192 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static struct ip6t_match opts_match = { |
| 196 | #if HOPBYHOP |
| 197 | .name = "hbh", |
| 198 | #else |
| 199 | .name = "dst", |
| 200 | #endif |
Patrick McHardy | 7f93971 | 2006-03-20 18:01:43 -0800 | [diff] [blame^] | 201 | .match = match, |
| 202 | .matchsize = sizeof(struct ip6t_opts), |
| 203 | .checkentry = checkentry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | .me = THIS_MODULE, |
| 205 | }; |
| 206 | |
| 207 | static int __init init(void) |
| 208 | { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 209 | return ip6t_register_match(&opts_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static void __exit cleanup(void) |
| 213 | { |
Yasuyuki Kozakai | f0daaa6 | 2006-01-17 02:39:39 -0800 | [diff] [blame] | 214 | ip6t_unregister_match(&opts_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | module_init(init); |
| 218 | module_exit(cleanup); |