Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 1 | /* Kernel module to match one of a list of TCP/UDP(-Lite)/SCTP/DCCP ports: |
| 2 | ports are in the same place so we can treat them as equal. */ |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 3 | |
| 4 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 5 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/types.h> |
| 14 | #include <linux/udp.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/in.h> |
| 17 | |
| 18 | #include <linux/netfilter/xt_multiport.h> |
| 19 | #include <linux/netfilter/x_tables.h> |
| 20 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 21 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 22 | |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 25 | MODULE_DESCRIPTION("x_tables multiple port match module"); |
| 26 | MODULE_ALIAS("ipt_multiport"); |
| 27 | MODULE_ALIAS("ip6t_multiport"); |
| 28 | |
| 29 | #if 0 |
| 30 | #define duprintf(format, args...) printk(format , ## args) |
| 31 | #else |
| 32 | #define duprintf(format, args...) |
| 33 | #endif |
| 34 | |
| 35 | /* Returns 1 if the port is matched by the test, 0 otherwise. */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 36 | static inline bool |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 37 | ports_match(const u_int16_t *portlist, enum xt_multiport_flags flags, |
| 38 | u_int8_t count, u_int16_t src, u_int16_t dst) |
| 39 | { |
| 40 | unsigned int i; |
| 41 | for (i = 0; i < count; i++) { |
| 42 | if (flags != XT_MULTIPORT_DESTINATION && portlist[i] == src) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 43 | return true; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 44 | |
| 45 | if (flags != XT_MULTIPORT_SOURCE && portlist[i] == dst) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 46 | return true; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 49 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /* Returns 1 if the port is matched by the test, 0 otherwise. */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 53 | static inline bool |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 54 | ports_match_v1(const struct xt_multiport_v1 *minfo, |
| 55 | u_int16_t src, u_int16_t dst) |
| 56 | { |
| 57 | unsigned int i; |
| 58 | u_int16_t s, e; |
| 59 | |
| 60 | for (i = 0; i < minfo->count; i++) { |
| 61 | s = minfo->ports[i]; |
| 62 | |
| 63 | if (minfo->pflags[i]) { |
| 64 | /* range port matching */ |
| 65 | e = minfo->ports[++i]; |
| 66 | duprintf("src or dst matches with %d-%d?\n", s, e); |
| 67 | |
| 68 | if (minfo->flags == XT_MULTIPORT_SOURCE |
| 69 | && src >= s && src <= e) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 70 | return true ^ minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 71 | if (minfo->flags == XT_MULTIPORT_DESTINATION |
| 72 | && dst >= s && dst <= e) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 73 | return true ^ minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 74 | if (minfo->flags == XT_MULTIPORT_EITHER |
| 75 | && ((dst >= s && dst <= e) |
| 76 | || (src >= s && src <= e))) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 77 | return true ^ minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 78 | } else { |
| 79 | /* exact port matching */ |
| 80 | duprintf("src or dst matches with %d?\n", s); |
| 81 | |
| 82 | if (minfo->flags == XT_MULTIPORT_SOURCE |
| 83 | && src == s) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 84 | return true ^ minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 85 | if (minfo->flags == XT_MULTIPORT_DESTINATION |
| 86 | && dst == s) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 87 | return true ^ minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 88 | if (minfo->flags == XT_MULTIPORT_EITHER |
| 89 | && (src == s || dst == s)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 90 | return true ^ minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 94 | return minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 97 | static bool |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 98 | match(const struct sk_buff *skb, |
| 99 | const struct net_device *in, |
| 100 | const struct net_device *out, |
| 101 | const struct xt_match *match, |
| 102 | const void *matchinfo, |
| 103 | int offset, |
| 104 | unsigned int protoff, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 105 | bool *hotdrop) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 106 | { |
Al Viro | 98a4a86 | 2006-11-08 00:26:51 -0800 | [diff] [blame] | 107 | __be16 _ports[2], *pptr; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 108 | const struct xt_multiport *multiinfo = matchinfo; |
| 109 | |
| 110 | if (offset) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 111 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 112 | |
| 113 | pptr = skb_header_pointer(skb, protoff, sizeof(_ports), _ports); |
| 114 | if (pptr == NULL) { |
| 115 | /* We've been asked to examine this packet, and we |
| 116 | * can't. Hence, no choice but to drop. |
| 117 | */ |
| 118 | duprintf("xt_multiport: Dropping evil offset=0 tinygram.\n"); |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 119 | *hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 120 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | return ports_match(multiinfo->ports, |
| 124 | multiinfo->flags, multiinfo->count, |
| 125 | ntohs(pptr[0]), ntohs(pptr[1])); |
| 126 | } |
| 127 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 128 | static bool |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 129 | match_v1(const struct sk_buff *skb, |
| 130 | const struct net_device *in, |
| 131 | const struct net_device *out, |
| 132 | const struct xt_match *match, |
| 133 | const void *matchinfo, |
| 134 | int offset, |
| 135 | unsigned int protoff, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 136 | bool *hotdrop) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 137 | { |
Al Viro | 98a4a86 | 2006-11-08 00:26:51 -0800 | [diff] [blame] | 138 | __be16 _ports[2], *pptr; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 139 | const struct xt_multiport_v1 *multiinfo = matchinfo; |
| 140 | |
| 141 | if (offset) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 142 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 143 | |
| 144 | pptr = skb_header_pointer(skb, protoff, sizeof(_ports), _ports); |
| 145 | if (pptr == NULL) { |
| 146 | /* We've been asked to examine this packet, and we |
| 147 | * can't. Hence, no choice but to drop. |
| 148 | */ |
| 149 | duprintf("xt_multiport: Dropping evil offset=0 tinygram.\n"); |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 150 | *hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 151 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1])); |
| 155 | } |
| 156 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 157 | static inline bool |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 158 | check(u_int16_t proto, |
| 159 | u_int8_t ip_invflags, |
| 160 | u_int8_t match_flags, |
| 161 | u_int8_t count) |
| 162 | { |
Patrick McHardy | 957dc80 | 2006-05-29 18:19:56 -0700 | [diff] [blame] | 163 | /* Must specify supported protocol, no unknown flags or bad count */ |
| 164 | return (proto == IPPROTO_TCP || proto == IPPROTO_UDP |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 165 | || proto == IPPROTO_UDPLITE |
Patrick McHardy | 957dc80 | 2006-05-29 18:19:56 -0700 | [diff] [blame] | 166 | || proto == IPPROTO_SCTP || proto == IPPROTO_DCCP) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 167 | && !(ip_invflags & XT_INV_PROTO) |
| 168 | && (match_flags == XT_MULTIPORT_SOURCE |
| 169 | || match_flags == XT_MULTIPORT_DESTINATION |
| 170 | || match_flags == XT_MULTIPORT_EITHER) |
| 171 | && count <= XT_MULTI_PORTS; |
| 172 | } |
| 173 | |
| 174 | /* Called when user tries to insert an entry of this type. */ |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 175 | static bool |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 176 | checkentry(const char *tablename, |
| 177 | const void *info, |
| 178 | const struct xt_match *match, |
| 179 | void *matchinfo, |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 180 | unsigned int hook_mask) |
| 181 | { |
| 182 | const struct ipt_ip *ip = info; |
| 183 | const struct xt_multiport *multiinfo = matchinfo; |
| 184 | |
| 185 | return check(ip->proto, ip->invflags, multiinfo->flags, |
| 186 | multiinfo->count); |
| 187 | } |
| 188 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 189 | static bool |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 190 | checkentry_v1(const char *tablename, |
| 191 | const void *info, |
| 192 | const struct xt_match *match, |
| 193 | void *matchinfo, |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 194 | unsigned int hook_mask) |
| 195 | { |
| 196 | const struct ipt_ip *ip = info; |
| 197 | const struct xt_multiport_v1 *multiinfo = matchinfo; |
| 198 | |
| 199 | return check(ip->proto, ip->invflags, multiinfo->flags, |
| 200 | multiinfo->count); |
| 201 | } |
| 202 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 203 | static bool |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 204 | checkentry6(const char *tablename, |
| 205 | const void *info, |
| 206 | const struct xt_match *match, |
| 207 | void *matchinfo, |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 208 | unsigned int hook_mask) |
| 209 | { |
| 210 | const struct ip6t_ip6 *ip = info; |
| 211 | const struct xt_multiport *multiinfo = matchinfo; |
| 212 | |
| 213 | return check(ip->proto, ip->invflags, multiinfo->flags, |
| 214 | multiinfo->count); |
| 215 | } |
| 216 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 217 | static bool |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 218 | checkentry6_v1(const char *tablename, |
| 219 | const void *info, |
| 220 | const struct xt_match *match, |
| 221 | void *matchinfo, |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 222 | unsigned int hook_mask) |
| 223 | { |
| 224 | const struct ip6t_ip6 *ip = info; |
| 225 | const struct xt_multiport_v1 *multiinfo = matchinfo; |
| 226 | |
| 227 | return check(ip->proto, ip->invflags, multiinfo->flags, |
| 228 | multiinfo->count); |
| 229 | } |
| 230 | |
Patrick McHardy | 9f15c53 | 2007-07-07 22:22:02 -0700 | [diff] [blame] | 231 | static struct xt_match xt_multiport_match[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 232 | { |
| 233 | .name = "multiport", |
| 234 | .family = AF_INET, |
| 235 | .revision = 0, |
| 236 | .checkentry = checkentry, |
| 237 | .match = match, |
| 238 | .matchsize = sizeof(struct xt_multiport), |
| 239 | .me = THIS_MODULE, |
| 240 | }, |
| 241 | { |
| 242 | .name = "multiport", |
| 243 | .family = AF_INET, |
| 244 | .revision = 1, |
| 245 | .checkentry = checkentry_v1, |
| 246 | .match = match_v1, |
| 247 | .matchsize = sizeof(struct xt_multiport_v1), |
| 248 | .me = THIS_MODULE, |
| 249 | }, |
| 250 | { |
| 251 | .name = "multiport", |
| 252 | .family = AF_INET6, |
| 253 | .revision = 0, |
| 254 | .checkentry = checkentry6, |
| 255 | .match = match, |
| 256 | .matchsize = sizeof(struct xt_multiport), |
| 257 | .me = THIS_MODULE, |
| 258 | }, |
| 259 | { |
| 260 | .name = "multiport", |
| 261 | .family = AF_INET6, |
| 262 | .revision = 1, |
| 263 | .checkentry = checkentry6_v1, |
| 264 | .match = match_v1, |
| 265 | .matchsize = sizeof(struct xt_multiport_v1), |
| 266 | .me = THIS_MODULE, |
| 267 | }, |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | static int __init xt_multiport_init(void) |
| 271 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 272 | return xt_register_matches(xt_multiport_match, |
| 273 | ARRAY_SIZE(xt_multiport_match)); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static void __exit xt_multiport_fini(void) |
| 277 | { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 278 | xt_unregister_matches(xt_multiport_match, |
| 279 | ARRAY_SIZE(xt_multiport_match)); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | module_init(xt_multiport_init); |
| 283 | module_exit(xt_multiport_fini); |