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 | */ |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 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 | |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 29 | /* Returns 1 if the port is matched by the test, 0 otherwise. */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 30 | static inline bool |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 31 | ports_match_v0(const u_int16_t *portlist, enum xt_multiport_flags flags, |
| 32 | u_int8_t count, u_int16_t src, u_int16_t dst) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 33 | { |
| 34 | unsigned int i; |
| 35 | for (i = 0; i < count; i++) { |
| 36 | if (flags != XT_MULTIPORT_DESTINATION && portlist[i] == src) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 37 | return true; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 38 | |
| 39 | if (flags != XT_MULTIPORT_SOURCE && portlist[i] == dst) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 40 | return true; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 43 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* Returns 1 if the port is matched by the test, 0 otherwise. */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 47 | static inline bool |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 48 | ports_match_v1(const struct xt_multiport_v1 *minfo, |
| 49 | u_int16_t src, u_int16_t dst) |
| 50 | { |
| 51 | unsigned int i; |
| 52 | u_int16_t s, e; |
| 53 | |
| 54 | for (i = 0; i < minfo->count; i++) { |
| 55 | s = minfo->ports[i]; |
| 56 | |
| 57 | if (minfo->pflags[i]) { |
| 58 | /* range port matching */ |
| 59 | e = minfo->ports[++i]; |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 60 | pr_debug("src or dst matches with %d-%d?\n", s, e); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 61 | |
| 62 | if (minfo->flags == XT_MULTIPORT_SOURCE |
| 63 | && src >= s && src <= e) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 64 | return true ^ minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 65 | if (minfo->flags == XT_MULTIPORT_DESTINATION |
| 66 | && dst >= s && dst <= e) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 67 | return true ^ minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 68 | if (minfo->flags == XT_MULTIPORT_EITHER |
| 69 | && ((dst >= s && dst <= e) |
| 70 | || (src >= s && src <= e))) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 71 | return true ^ minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 72 | } else { |
| 73 | /* exact port matching */ |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 74 | pr_debug("src or dst matches with %d?\n", s); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 75 | |
| 76 | if (minfo->flags == XT_MULTIPORT_SOURCE |
| 77 | && src == s) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 78 | return true ^ minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 79 | if (minfo->flags == XT_MULTIPORT_DESTINATION |
| 80 | && dst == s) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 81 | return true ^ minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 82 | if (minfo->flags == XT_MULTIPORT_EITHER |
| 83 | && (src == s || dst == 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 | } |
| 86 | } |
| 87 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 88 | return minfo->invert; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 91 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 92 | 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] | 93 | { |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 94 | const __be16 *pptr; |
| 95 | __be16 _ports[2]; |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 96 | const struct xt_multiport *multiinfo = par->matchinfo; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 97 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 98 | if (par->fragoff != 0) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 99 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 100 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 101 | pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 102 | if (pptr == NULL) { |
| 103 | /* We've been asked to examine this packet, and we |
| 104 | * can't. Hence, no choice but to drop. |
| 105 | */ |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 106 | pr_debug("Dropping evil offset=0 tinygram.\n"); |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 107 | *par->hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 108 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 111 | return ports_match_v0(multiinfo->ports, multiinfo->flags, |
| 112 | multiinfo->count, ntohs(pptr[0]), ntohs(pptr[1])); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 115 | static bool |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 116 | 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] | 117 | { |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 118 | const __be16 *pptr; |
| 119 | __be16 _ports[2]; |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 120 | const struct xt_multiport_v1 *multiinfo = par->matchinfo; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 121 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 122 | if (par->fragoff != 0) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 123 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 124 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 125 | pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 126 | if (pptr == NULL) { |
| 127 | /* We've been asked to examine this packet, and we |
| 128 | * can't. Hence, no choice but to drop. |
| 129 | */ |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 130 | pr_debug("Dropping evil offset=0 tinygram.\n"); |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 131 | *par->hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 132 | return false; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1])); |
| 136 | } |
| 137 | |
Jan Engelhardt | ccb79bd | 2007-07-07 22:16:00 -0700 | [diff] [blame] | 138 | static inline bool |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 139 | check(u_int16_t proto, |
| 140 | u_int8_t ip_invflags, |
| 141 | u_int8_t match_flags, |
| 142 | u_int8_t count) |
| 143 | { |
Patrick McHardy | 957dc80 | 2006-05-29 18:19:56 -0700 | [diff] [blame] | 144 | /* Must specify supported protocol, no unknown flags or bad count */ |
| 145 | return (proto == IPPROTO_TCP || proto == IPPROTO_UDP |
Gerrit Renker | ba4e58e | 2006-11-27 11:10:57 -0800 | [diff] [blame] | 146 | || proto == IPPROTO_UDPLITE |
Patrick McHardy | 957dc80 | 2006-05-29 18:19:56 -0700 | [diff] [blame] | 147 | || proto == IPPROTO_SCTP || proto == IPPROTO_DCCP) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 148 | && !(ip_invflags & XT_INV_PROTO) |
| 149 | && (match_flags == XT_MULTIPORT_SOURCE |
| 150 | || match_flags == XT_MULTIPORT_DESTINATION |
| 151 | || match_flags == XT_MULTIPORT_EITHER) |
| 152 | && count <= XT_MULTI_PORTS; |
| 153 | } |
| 154 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame^] | 155 | static int multiport_mt_check_v0(const struct xt_mtchk_param *par) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 156 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 157 | const struct ipt_ip *ip = par->entryinfo; |
| 158 | const struct xt_multiport *multiinfo = par->matchinfo; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 159 | |
| 160 | return check(ip->proto, ip->invflags, multiinfo->flags, |
| 161 | multiinfo->count); |
| 162 | } |
| 163 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame^] | 164 | static int multiport_mt_check(const struct xt_mtchk_param *par) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 165 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 166 | const struct ipt_ip *ip = par->entryinfo; |
| 167 | const struct xt_multiport_v1 *multiinfo = par->matchinfo; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 168 | |
| 169 | return check(ip->proto, ip->invflags, multiinfo->flags, |
| 170 | multiinfo->count); |
| 171 | } |
| 172 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame^] | 173 | static int multiport_mt6_check_v0(const struct xt_mtchk_param *par) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 174 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 175 | const struct ip6t_ip6 *ip = par->entryinfo; |
| 176 | const struct xt_multiport *multiinfo = par->matchinfo; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 177 | |
| 178 | return check(ip->proto, ip->invflags, multiinfo->flags, |
| 179 | multiinfo->count); |
| 180 | } |
| 181 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame^] | 182 | static int multiport_mt6_check(const struct xt_mtchk_param *par) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 183 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 184 | const struct ip6t_ip6 *ip = par->entryinfo; |
| 185 | const struct xt_multiport_v1 *multiinfo = par->matchinfo; |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 186 | |
| 187 | return check(ip->proto, ip->invflags, multiinfo->flags, |
| 188 | multiinfo->count); |
| 189 | } |
| 190 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 191 | static struct xt_match multiport_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 192 | { |
| 193 | .name = "multiport", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 194 | .family = NFPROTO_IPV4, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 195 | .revision = 0, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 196 | .checkentry = multiport_mt_check_v0, |
| 197 | .match = multiport_mt_v0, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 198 | .matchsize = sizeof(struct xt_multiport), |
| 199 | .me = THIS_MODULE, |
| 200 | }, |
| 201 | { |
| 202 | .name = "multiport", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 203 | .family = NFPROTO_IPV4, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 204 | .revision = 1, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 205 | .checkentry = multiport_mt_check, |
| 206 | .match = multiport_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 207 | .matchsize = sizeof(struct xt_multiport_v1), |
| 208 | .me = THIS_MODULE, |
| 209 | }, |
| 210 | { |
| 211 | .name = "multiport", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 212 | .family = NFPROTO_IPV6, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 213 | .revision = 0, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 214 | .checkentry = multiport_mt6_check_v0, |
| 215 | .match = multiport_mt_v0, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 216 | .matchsize = sizeof(struct xt_multiport), |
| 217 | .me = THIS_MODULE, |
| 218 | }, |
| 219 | { |
| 220 | .name = "multiport", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 221 | .family = NFPROTO_IPV6, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 222 | .revision = 1, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 223 | .checkentry = multiport_mt6_check, |
| 224 | .match = multiport_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 225 | .matchsize = sizeof(struct xt_multiport_v1), |
| 226 | .me = THIS_MODULE, |
| 227 | }, |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 228 | }; |
| 229 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 230 | static int __init multiport_mt_init(void) |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 231 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 232 | return xt_register_matches(multiport_mt_reg, |
| 233 | ARRAY_SIZE(multiport_mt_reg)); |
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 void __exit multiport_mt_exit(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 | xt_unregister_matches(multiport_mt_reg, ARRAY_SIZE(multiport_mt_reg)); |
Yasuyuki Kozakai | a89ecb6 | 2006-04-01 02:22:54 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 241 | module_init(multiport_mt_init); |
| 242 | module_exit(multiport_mt_exit); |