blob: 4fa90c86fdb5dbc155f953f8ac3139c31dd2c56b [file] [log] [blame]
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001/* 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 Kozakaia89ecb62006-04-01 02:22:54 -08003
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 Engelhardtbe91fd52010-03-18 02:22:32 +010011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080012#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
23MODULE_LICENSE("GPL");
24MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080025MODULE_DESCRIPTION("Xtables: multiple port matching for TCP, UDP, UDP-Lite, SCTP and DCCP");
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080026MODULE_ALIAS("ipt_multiport");
27MODULE_ALIAS("ip6t_multiport");
28
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080029/* Returns 1 if the port is matched by the test, 0 otherwise. */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070030static inline bool
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080031ports_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 Kozakaia89ecb62006-04-01 02:22:54 -080033{
34 unsigned int i;
35 for (i = 0; i < count; i++) {
36 if (flags != XT_MULTIPORT_DESTINATION && portlist[i] == src)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070037 return true;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080038
39 if (flags != XT_MULTIPORT_SOURCE && portlist[i] == dst)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070040 return true;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080041 }
42
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070043 return false;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080044}
45
46/* Returns 1 if the port is matched by the test, 0 otherwise. */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070047static inline bool
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080048ports_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 Engelhardtbe91fd52010-03-18 02:22:32 +010060 pr_debug("src or dst matches with %d-%d?\n", s, e);
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080061
62 if (minfo->flags == XT_MULTIPORT_SOURCE
63 && src >= s && src <= e)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070064 return true ^ minfo->invert;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080065 if (minfo->flags == XT_MULTIPORT_DESTINATION
66 && dst >= s && dst <= e)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070067 return true ^ minfo->invert;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080068 if (minfo->flags == XT_MULTIPORT_EITHER
69 && ((dst >= s && dst <= e)
70 || (src >= s && src <= e)))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070071 return true ^ minfo->invert;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080072 } else {
73 /* exact port matching */
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010074 pr_debug("src or dst matches with %d?\n", s);
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080075
76 if (minfo->flags == XT_MULTIPORT_SOURCE
77 && src == s)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070078 return true ^ minfo->invert;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080079 if (minfo->flags == XT_MULTIPORT_DESTINATION
80 && dst == s)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070081 return true ^ minfo->invert;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080082 if (minfo->flags == XT_MULTIPORT_EITHER
83 && (src == s || dst == s))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070084 return true ^ minfo->invert;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080085 }
86 }
87
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080088 return minfo->invert;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080089}
90
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070091static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +020092multiport_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par)
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080093{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020094 const __be16 *pptr;
95 __be16 _ports[2];
Jan Engelhardtf7108a22008-10-08 11:35:18 +020096 const struct xt_multiport *multiinfo = par->matchinfo;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -080097
Jan Engelhardtf7108a22008-10-08 11:35:18 +020098 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070099 return false;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800100
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200101 pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports);
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800102 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 Engelhardtbe91fd52010-03-18 02:22:32 +0100106 pr_debug("Dropping evil offset=0 tinygram.\n");
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200107 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700108 return false;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800109 }
110
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800111 return ports_match_v0(multiinfo->ports, multiinfo->flags,
112 multiinfo->count, ntohs(pptr[0]), ntohs(pptr[1]));
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800113}
114
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700115static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200116multiport_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800117{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200118 const __be16 *pptr;
119 __be16 _ports[2];
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200120 const struct xt_multiport_v1 *multiinfo = par->matchinfo;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800121
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200122 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700123 return false;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800124
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200125 pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports);
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800126 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 Engelhardtbe91fd52010-03-18 02:22:32 +0100130 pr_debug("Dropping evil offset=0 tinygram.\n");
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200131 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700132 return false;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800133 }
134
135 return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1]));
136}
137
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700138static inline bool
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800139check(u_int16_t proto,
140 u_int8_t ip_invflags,
141 u_int8_t match_flags,
142 u_int8_t count)
143{
Patrick McHardy957dc802006-05-29 18:19:56 -0700144 /* Must specify supported protocol, no unknown flags or bad count */
145 return (proto == IPPROTO_TCP || proto == IPPROTO_UDP
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800146 || proto == IPPROTO_UDPLITE
Patrick McHardy957dc802006-05-29 18:19:56 -0700147 || proto == IPPROTO_SCTP || proto == IPPROTO_DCCP)
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800148 && !(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 Engelhardt9b4fce72008-10-08 11:35:18 +0200155static bool multiport_mt_check_v0(const struct xt_mtchk_param *par)
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800156{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200157 const struct ipt_ip *ip = par->entryinfo;
158 const struct xt_multiport *multiinfo = par->matchinfo;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800159
160 return check(ip->proto, ip->invflags, multiinfo->flags,
161 multiinfo->count);
162}
163
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200164static bool multiport_mt_check(const struct xt_mtchk_param *par)
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800165{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200166 const struct ipt_ip *ip = par->entryinfo;
167 const struct xt_multiport_v1 *multiinfo = par->matchinfo;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800168
169 return check(ip->proto, ip->invflags, multiinfo->flags,
170 multiinfo->count);
171}
172
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200173static bool multiport_mt6_check_v0(const struct xt_mtchk_param *par)
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800174{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200175 const struct ip6t_ip6 *ip = par->entryinfo;
176 const struct xt_multiport *multiinfo = par->matchinfo;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800177
178 return check(ip->proto, ip->invflags, multiinfo->flags,
179 multiinfo->count);
180}
181
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200182static bool multiport_mt6_check(const struct xt_mtchk_param *par)
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800183{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200184 const struct ip6t_ip6 *ip = par->entryinfo;
185 const struct xt_multiport_v1 *multiinfo = par->matchinfo;
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800186
187 return check(ip->proto, ip->invflags, multiinfo->flags,
188 multiinfo->count);
189}
190
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800191static struct xt_match multiport_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700192 {
193 .name = "multiport",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200194 .family = NFPROTO_IPV4,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700195 .revision = 0,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800196 .checkentry = multiport_mt_check_v0,
197 .match = multiport_mt_v0,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700198 .matchsize = sizeof(struct xt_multiport),
199 .me = THIS_MODULE,
200 },
201 {
202 .name = "multiport",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200203 .family = NFPROTO_IPV4,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700204 .revision = 1,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800205 .checkentry = multiport_mt_check,
206 .match = multiport_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700207 .matchsize = sizeof(struct xt_multiport_v1),
208 .me = THIS_MODULE,
209 },
210 {
211 .name = "multiport",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200212 .family = NFPROTO_IPV6,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700213 .revision = 0,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800214 .checkentry = multiport_mt6_check_v0,
215 .match = multiport_mt_v0,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700216 .matchsize = sizeof(struct xt_multiport),
217 .me = THIS_MODULE,
218 },
219 {
220 .name = "multiport",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200221 .family = NFPROTO_IPV6,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700222 .revision = 1,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800223 .checkentry = multiport_mt6_check,
224 .match = multiport_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700225 .matchsize = sizeof(struct xt_multiport_v1),
226 .me = THIS_MODULE,
227 },
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800228};
229
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800230static int __init multiport_mt_init(void)
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800231{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800232 return xt_register_matches(multiport_mt_reg,
233 ARRAY_SIZE(multiport_mt_reg));
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800234}
235
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800236static void __exit multiport_mt_exit(void)
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800237{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800238 xt_unregister_matches(multiport_mt_reg, ARRAY_SIZE(multiport_mt_reg));
Yasuyuki Kozakaia89ecb62006-04-01 02:22:54 -0800239}
240
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800241module_init(multiport_mt_init);
242module_exit(multiport_mt_exit);