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>"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 25 | MODULE_DESCRIPTION("Xtables: multiple port matching for TCP, UDP, UDP-Lite, SCTP and DCCP"); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 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 |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 37 | ports_match_v0(const u_int16_t *portlist, enum xt_multiport_flags flags, |
| 38 | u_int8_t count, u_int16_t src, u_int16_t dst) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 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 |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 98 | multiport_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 99 | { |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 100 | const __be16 *pptr; |
| 101 | __be16 _ports[2]; |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 102 | const struct xt_multiport *multiinfo = par->matchinfo; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 103 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 104 | if (par->fragoff != 0) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 105 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 106 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 107 | pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 108 | if (pptr == NULL) { |
| 109 | /* We've been asked to examine this packet, and we |
| 110 | * can't. Hence, no choice but to drop. |
| 111 | */ |
| 112 | duprintf("xt_multiport: Dropping evil offset=0 tinygram.\n"); |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 113 | *par->hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 114 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 117 | return ports_match_v0(multiinfo->ports, multiinfo->flags, |
| 118 | multiinfo->count, ntohs(pptr[0]), ntohs(pptr[1])); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 121 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 122 | multiport_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 123 | { |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 124 | const __be16 *pptr; |
| 125 | __be16 _ports[2]; |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 126 | const struct xt_multiport_v1 *multiinfo = par->matchinfo; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 127 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 128 | if (par->fragoff != 0) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 129 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 130 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 131 | pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 132 | if (pptr == NULL) { |
| 133 | /* We've been asked to examine this packet, and we |
| 134 | * can't. Hence, no choice but to drop. |
| 135 | */ |
| 136 | duprintf("xt_multiport: Dropping evil offset=0 tinygram.\n"); |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 137 | *par->hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 138 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1])); |
| 142 | } |
| 143 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 144 | static inline bool |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 145 | check(u_int16_t proto, |
| 146 | u_int8_t ip_invflags, |
| 147 | u_int8_t match_flags, |
| 148 | u_int8_t count) |
| 149 | { |
Patrick McHardy | 957dc80 | 2006-05-29 18:19:56 -0700 | [diff] [blame] | 150 | /* Must specify supported protocol, no unknown flags or bad count */ |
| 151 | return (proto == IPPROTO_TCP || proto == IPPROTO_UDP |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 152 | || proto == IPPROTO_UDPLITE |
Patrick McHardy | 957dc80 | 2006-05-29 18:19:56 -0700 | [diff] [blame] | 153 | || proto == IPPROTO_SCTP || proto == IPPROTO_DCCP) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 154 | && !(ip_invflags & XT_INV_PROTO) |
| 155 | && (match_flags == XT_MULTIPORT_SOURCE |
| 156 | || match_flags == XT_MULTIPORT_DESTINATION |
| 157 | || match_flags == XT_MULTIPORT_EITHER) |
| 158 | && count <= XT_MULTI_PORTS; |
| 159 | } |
| 160 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame^] | 161 | static bool multiport_mt_check_v0(const struct xt_mtchk_param *par) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 162 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame^] | 163 | const struct ipt_ip *ip = par->entryinfo; |
| 164 | const struct xt_multiport *multiinfo = par->matchinfo; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 165 | |
| 166 | return check(ip->proto, ip->invflags, multiinfo->flags, |
| 167 | multiinfo->count); |
| 168 | } |
| 169 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame^] | 170 | static bool multiport_mt_check(const struct xt_mtchk_param *par) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 171 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame^] | 172 | const struct ipt_ip *ip = par->entryinfo; |
| 173 | const struct xt_multiport_v1 *multiinfo = par->matchinfo; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 174 | |
| 175 | return check(ip->proto, ip->invflags, multiinfo->flags, |
| 176 | multiinfo->count); |
| 177 | } |
| 178 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame^] | 179 | static bool multiport_mt6_check_v0(const struct xt_mtchk_param *par) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 180 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame^] | 181 | const struct ip6t_ip6 *ip = par->entryinfo; |
| 182 | const struct xt_multiport *multiinfo = par->matchinfo; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 183 | |
| 184 | return check(ip->proto, ip->invflags, multiinfo->flags, |
| 185 | multiinfo->count); |
| 186 | } |
| 187 | |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame^] | 188 | static bool multiport_mt6_check(const struct xt_mtchk_param *par) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 189 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame^] | 190 | const struct ip6t_ip6 *ip = par->entryinfo; |
| 191 | const struct xt_multiport_v1 *multiinfo = par->matchinfo; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 192 | |
| 193 | return check(ip->proto, ip->invflags, multiinfo->flags, |
| 194 | multiinfo->count); |
| 195 | } |
| 196 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 197 | static struct xt_match multiport_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 198 | { |
| 199 | .name = "multiport", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 200 | .family = NFPROTO_IPV4, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 201 | .revision = 0, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 202 | .checkentry = multiport_mt_check_v0, |
| 203 | .match = multiport_mt_v0, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 204 | .matchsize = sizeof(struct xt_multiport), |
| 205 | .me = THIS_MODULE, |
| 206 | }, |
| 207 | { |
| 208 | .name = "multiport", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 209 | .family = NFPROTO_IPV4, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 210 | .revision = 1, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 211 | .checkentry = multiport_mt_check, |
| 212 | .match = multiport_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 213 | .matchsize = sizeof(struct xt_multiport_v1), |
| 214 | .me = THIS_MODULE, |
| 215 | }, |
| 216 | { |
| 217 | .name = "multiport", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 218 | .family = NFPROTO_IPV6, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 219 | .revision = 0, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 220 | .checkentry = multiport_mt6_check_v0, |
| 221 | .match = multiport_mt_v0, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 222 | .matchsize = sizeof(struct xt_multiport), |
| 223 | .me = THIS_MODULE, |
| 224 | }, |
| 225 | { |
| 226 | .name = "multiport", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 227 | .family = NFPROTO_IPV6, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 228 | .revision = 1, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 229 | .checkentry = multiport_mt6_check, |
| 230 | .match = multiport_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 231 | .matchsize = sizeof(struct xt_multiport_v1), |
| 232 | .me = THIS_MODULE, |
| 233 | }, |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 234 | }; |
| 235 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 236 | static int __init multiport_mt_init(void) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 237 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 238 | return xt_register_matches(multiport_mt_reg, |
| 239 | ARRAY_SIZE(multiport_mt_reg)); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 242 | static void __exit multiport_mt_exit(void) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 243 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 244 | xt_unregister_matches(multiport_mt_reg, ARRAY_SIZE(multiport_mt_reg)); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 247 | module_init(multiport_mt_init); |
| 248 | module_exit(multiport_mt_exit); |