2006-11-27 19:10:57 +00:00
|
|
|
/* Kernel module to match one of a list of TCP/UDP(-Lite)/SCTP/DCCP ports:
|
|
|
|
ports are in the same place so we can treat them as equal. */
|
2006-04-01 10:22:54 +00:00
|
|
|
|
|
|
|
/* (C) 1999-2001 Paul `Rusty' Russell
|
|
|
|
* (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
2010-03-18 01:22:32 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2006-04-01 10:22:54 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/udp.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/in.h>
|
|
|
|
|
|
|
|
#include <linux/netfilter/xt_multiport.h>
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
#include <linux/netfilter_ipv4/ip_tables.h>
|
|
|
|
#include <linux/netfilter_ipv6/ip6_tables.h>
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
|
2008-01-15 07:42:28 +00:00
|
|
|
MODULE_DESCRIPTION("Xtables: multiple port matching for TCP, UDP, UDP-Lite, SCTP and DCCP");
|
2006-04-01 10:22:54 +00:00
|
|
|
MODULE_ALIAS("ipt_multiport");
|
|
|
|
MODULE_ALIAS("ip6t_multiport");
|
|
|
|
|
|
|
|
/* Returns 1 if the port is matched by the test, 0 otherwise. */
|
2007-07-08 05:15:35 +00:00
|
|
|
static inline bool
|
2006-04-01 10:22:54 +00:00
|
|
|
ports_match_v1(const struct xt_multiport_v1 *minfo,
|
|
|
|
u_int16_t src, u_int16_t dst)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
u_int16_t s, e;
|
|
|
|
|
|
|
|
for (i = 0; i < minfo->count; i++) {
|
|
|
|
s = minfo->ports[i];
|
|
|
|
|
|
|
|
if (minfo->pflags[i]) {
|
|
|
|
/* range port matching */
|
|
|
|
e = minfo->ports[++i];
|
2010-03-18 01:22:32 +00:00
|
|
|
pr_debug("src or dst matches with %d-%d?\n", s, e);
|
2006-04-01 10:22:54 +00:00
|
|
|
|
2016-09-26 13:23:37 +00:00
|
|
|
switch (minfo->flags) {
|
|
|
|
case XT_MULTIPORT_SOURCE:
|
2016-11-25 04:32:07 +00:00
|
|
|
if (src >= s && src <= e)
|
|
|
|
return true ^ minfo->invert;
|
|
|
|
break;
|
2016-09-26 13:23:37 +00:00
|
|
|
case XT_MULTIPORT_DESTINATION:
|
2016-11-25 04:32:07 +00:00
|
|
|
if (dst >= s && dst <= e)
|
|
|
|
return true ^ minfo->invert;
|
|
|
|
break;
|
2016-09-26 13:23:37 +00:00
|
|
|
case XT_MULTIPORT_EITHER:
|
2016-11-25 04:32:07 +00:00
|
|
|
if ((dst >= s && dst <= e) ||
|
|
|
|
(src >= s && src <= e))
|
|
|
|
return true ^ minfo->invert;
|
|
|
|
break;
|
2016-09-26 13:23:37 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2006-04-01 10:22:54 +00:00
|
|
|
} else {
|
|
|
|
/* exact port matching */
|
2010-03-18 01:22:32 +00:00
|
|
|
pr_debug("src or dst matches with %d?\n", s);
|
2006-04-01 10:22:54 +00:00
|
|
|
|
2016-09-26 13:23:37 +00:00
|
|
|
switch (minfo->flags) {
|
|
|
|
case XT_MULTIPORT_SOURCE:
|
2016-11-25 04:32:07 +00:00
|
|
|
if (src == s)
|
|
|
|
return true ^ minfo->invert;
|
|
|
|
break;
|
2016-09-26 13:23:37 +00:00
|
|
|
case XT_MULTIPORT_DESTINATION:
|
2016-11-25 04:32:07 +00:00
|
|
|
if (dst == s)
|
|
|
|
return true ^ minfo->invert;
|
|
|
|
break;
|
2016-09-26 13:23:37 +00:00
|
|
|
case XT_MULTIPORT_EITHER:
|
2016-11-25 04:32:07 +00:00
|
|
|
if (src == s || dst == s)
|
|
|
|
return true ^ minfo->invert;
|
|
|
|
break;
|
2016-09-26 13:23:37 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2006-04-01 10:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-12 19:15:49 +00:00
|
|
|
return minfo->invert;
|
2006-04-01 10:22:54 +00:00
|
|
|
}
|
|
|
|
|
2007-07-08 05:15:35 +00:00
|
|
|
static bool
|
2009-07-07 18:42:08 +00:00
|
|
|
multiport_mt(const struct sk_buff *skb, struct xt_action_param *par)
|
2006-04-01 10:22:54 +00:00
|
|
|
{
|
2008-04-14 07:56:05 +00:00
|
|
|
const __be16 *pptr;
|
|
|
|
__be16 _ports[2];
|
2008-10-08 09:35:18 +00:00
|
|
|
const struct xt_multiport_v1 *multiinfo = par->matchinfo;
|
2006-04-01 10:22:54 +00:00
|
|
|
|
2008-10-08 09:35:18 +00:00
|
|
|
if (par->fragoff != 0)
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2006-04-01 10:22:54 +00:00
|
|
|
|
2008-10-08 09:35:18 +00:00
|
|
|
pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports);
|
2006-04-01 10:22:54 +00:00
|
|
|
if (pptr == NULL) {
|
|
|
|
/* We've been asked to examine this packet, and we
|
|
|
|
* can't. Hence, no choice but to drop.
|
|
|
|
*/
|
2010-03-18 01:22:32 +00:00
|
|
|
pr_debug("Dropping evil offset=0 tinygram.\n");
|
2009-07-07 18:54:30 +00:00
|
|
|
par->hotdrop = true;
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2006-04-01 10:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1]));
|
|
|
|
}
|
|
|
|
|
2007-07-08 05:16:00 +00:00
|
|
|
static inline bool
|
2006-04-01 10:22:54 +00:00
|
|
|
check(u_int16_t proto,
|
|
|
|
u_int8_t ip_invflags,
|
|
|
|
u_int8_t match_flags,
|
|
|
|
u_int8_t count)
|
|
|
|
{
|
2006-05-30 01:19:56 +00:00
|
|
|
/* Must specify supported protocol, no unknown flags or bad count */
|
|
|
|
return (proto == IPPROTO_TCP || proto == IPPROTO_UDP
|
2006-11-27 19:10:57 +00:00
|
|
|
|| proto == IPPROTO_UDPLITE
|
2006-05-30 01:19:56 +00:00
|
|
|
|| proto == IPPROTO_SCTP || proto == IPPROTO_DCCP)
|
2006-04-01 10:22:54 +00:00
|
|
|
&& !(ip_invflags & XT_INV_PROTO)
|
|
|
|
&& (match_flags == XT_MULTIPORT_SOURCE
|
|
|
|
|| match_flags == XT_MULTIPORT_DESTINATION
|
|
|
|
|| match_flags == XT_MULTIPORT_EITHER)
|
|
|
|
&& count <= XT_MULTI_PORTS;
|
|
|
|
}
|
|
|
|
|
2010-03-19 16:16:42 +00:00
|
|
|
static int multiport_mt_check(const struct xt_mtchk_param *par)
|
2006-04-01 10:22:54 +00:00
|
|
|
{
|
2008-10-08 09:35:18 +00:00
|
|
|
const struct ipt_ip *ip = par->entryinfo;
|
|
|
|
const struct xt_multiport_v1 *multiinfo = par->matchinfo;
|
2006-04-01 10:22:54 +00:00
|
|
|
|
|
|
|
return check(ip->proto, ip->invflags, multiinfo->flags,
|
2010-05-02 12:04:54 +00:00
|
|
|
multiinfo->count) ? 0 : -EINVAL;
|
2006-04-01 10:22:54 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 16:16:42 +00:00
|
|
|
static int multiport_mt6_check(const struct xt_mtchk_param *par)
|
2006-04-01 10:22:54 +00:00
|
|
|
{
|
2008-10-08 09:35:18 +00:00
|
|
|
const struct ip6t_ip6 *ip = par->entryinfo;
|
|
|
|
const struct xt_multiport_v1 *multiinfo = par->matchinfo;
|
2006-04-01 10:22:54 +00:00
|
|
|
|
|
|
|
return check(ip->proto, ip->invflags, multiinfo->flags,
|
2010-05-02 12:04:54 +00:00
|
|
|
multiinfo->count) ? 0 : -EINVAL;
|
2006-04-01 10:22:54 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static struct xt_match multiport_mt_reg[] __read_mostly = {
|
2006-08-22 07:34:04 +00:00
|
|
|
{
|
|
|
|
.name = "multiport",
|
2008-10-08 09:35:01 +00:00
|
|
|
.family = NFPROTO_IPV4,
|
2006-08-22 07:34:04 +00:00
|
|
|
.revision = 1,
|
2007-12-05 07:24:03 +00:00
|
|
|
.checkentry = multiport_mt_check,
|
|
|
|
.match = multiport_mt,
|
2006-08-22 07:34:04 +00:00
|
|
|
.matchsize = sizeof(struct xt_multiport_v1),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.name = "multiport",
|
2008-10-08 09:35:01 +00:00
|
|
|
.family = NFPROTO_IPV6,
|
2006-08-22 07:34:04 +00:00
|
|
|
.revision = 1,
|
2007-12-05 07:24:03 +00:00
|
|
|
.checkentry = multiport_mt6_check,
|
|
|
|
.match = multiport_mt,
|
2006-08-22 07:34:04 +00:00
|
|
|
.matchsize = sizeof(struct xt_multiport_v1),
|
|
|
|
.me = THIS_MODULE,
|
|
|
|
},
|
2006-04-01 10:22:54 +00:00
|
|
|
};
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static int __init multiport_mt_init(void)
|
2006-04-01 10:22:54 +00:00
|
|
|
{
|
2007-12-05 07:24:03 +00:00
|
|
|
return xt_register_matches(multiport_mt_reg,
|
|
|
|
ARRAY_SIZE(multiport_mt_reg));
|
2006-04-01 10:22:54 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
static void __exit multiport_mt_exit(void)
|
2006-04-01 10:22:54 +00:00
|
|
|
{
|
2007-12-05 07:24:03 +00:00
|
|
|
xt_unregister_matches(multiport_mt_reg, ARRAY_SIZE(multiport_mt_reg));
|
2006-04-01 10:22:54 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 07:24:03 +00:00
|
|
|
module_init(multiport_mt_init);
|
|
|
|
module_exit(multiport_mt_exit);
|