1 /* Kernel module to match one of a list of TCP/UDP ports: ports are in
2 the same place so we can treat them as equal. */
4 /* (C) 1999-2001 Paul `Rusty' Russell
5 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/udp.h>
15 #include <linux/skbuff.h>
17 #include <linux/netfilter_ipv4/ipt_multiport.h>
18 #include <linux/netfilter_ipv4/ip_tables.h>
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
22 MODULE_DESCRIPTION("iptables multiple port match module");
25 #define duprintf(format, args...) printk(format , ## args)
27 #define duprintf(format, args...)
30 /* Returns 1 if the port is matched by the test, 0 otherwise. */
32 ports_match(const u_int16_t *portlist, enum ipt_multiport_flags flags,
33 u_int8_t count, u_int16_t src, u_int16_t dst)
36 for (i=0; i<count; i++) {
37 if (flags != IPT_MULTIPORT_DESTINATION
38 && portlist[i] == src)
41 if (flags != IPT_MULTIPORT_SOURCE
42 && portlist[i] == dst)
49 /* Returns 1 if the port is matched by the test, 0 otherwise. */
51 ports_match_v1(const struct ipt_multiport_v1 *minfo,
52 u_int16_t src, u_int16_t dst)
57 for (i=0; i < minfo->count; i++) {
60 if (minfo->pflags[i]) {
61 /* range port matching */
62 e = minfo->ports[++i];
63 duprintf("src or dst matches with %d-%d?\n", s, e);
65 if (minfo->flags == IPT_MULTIPORT_SOURCE
66 && src >= s && src <= e)
67 return 1 ^ minfo->invert;
68 if (minfo->flags == IPT_MULTIPORT_DESTINATION
69 && dst >= s && dst <= e)
70 return 1 ^ minfo->invert;
71 if (minfo->flags == IPT_MULTIPORT_EITHER
72 && ((dst >= s && dst <= e)
73 || (src >= s && src <= e)))
74 return 1 ^ minfo->invert;
76 /* exact port matching */
77 duprintf("src or dst matches with %d?\n", s);
79 if (minfo->flags == IPT_MULTIPORT_SOURCE
81 return 1 ^ minfo->invert;
82 if (minfo->flags == IPT_MULTIPORT_DESTINATION
84 return 1 ^ minfo->invert;
85 if (minfo->flags == IPT_MULTIPORT_EITHER
86 && (src == s || dst == s))
87 return 1 ^ minfo->invert;
95 match(const struct sk_buff *skb,
96 const struct net_device *in,
97 const struct net_device *out,
98 const struct xt_match *match,
99 const void *matchinfo,
101 unsigned int protoff,
104 u16 _ports[2], *pptr;
105 const struct ipt_multiport *multiinfo = matchinfo;
110 pptr = skb_header_pointer(skb, protoff,
111 sizeof(_ports), _ports);
113 /* We've been asked to examine this packet, and we
114 * can't. Hence, no choice but to drop.
116 duprintf("ipt_multiport:"
117 " Dropping evil offset=0 tinygram.\n");
122 return ports_match(multiinfo->ports,
123 multiinfo->flags, multiinfo->count,
124 ntohs(pptr[0]), ntohs(pptr[1]));
128 match_v1(const struct sk_buff *skb,
129 const struct net_device *in,
130 const struct net_device *out,
131 const struct xt_match *match,
132 const void *matchinfo,
134 unsigned int protoff,
137 u16 _ports[2], *pptr;
138 const struct ipt_multiport_v1 *multiinfo = matchinfo;
143 pptr = skb_header_pointer(skb, protoff,
144 sizeof(_ports), _ports);
146 /* We've been asked to examine this packet, and we
147 * can't. Hence, no choice but to drop.
149 duprintf("ipt_multiport:"
150 " Dropping evil offset=0 tinygram.\n");
155 return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1]));
158 static struct ipt_match multiport_match = {
162 .matchsize = sizeof(struct ipt_multiport),
166 static struct ipt_match multiport_match_v1 = {
170 .matchsize = sizeof(struct ipt_multiport_v1),
174 static int __init init(void)
178 err = ipt_register_match(&multiport_match);
180 err = ipt_register_match(&multiport_match_v1);
182 ipt_unregister_match(&multiport_match);
188 static void __exit fini(void)
190 ipt_unregister_match(&multiport_match);
191 ipt_unregister_match(&multiport_match_v1);