1 /* Kernel module to match one of a list of TCP/UDP/SCTP/DCCP 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>
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>
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
25 MODULE_DESCRIPTION("x_tables multiple port match module");
26 MODULE_ALIAS("ipt_multiport");
27 MODULE_ALIAS("ip6t_multiport");
30 #define duprintf(format, args...) printk(format , ## args)
32 #define duprintf(format, args...)
35 /* Returns 1 if the port is matched by the test, 0 otherwise. */
37 ports_match(const u_int16_t
*portlist
, enum xt_multiport_flags flags
,
38 u_int8_t count
, u_int16_t src
, u_int16_t dst
)
41 for (i
= 0; i
< count
; i
++) {
42 if (flags
!= XT_MULTIPORT_DESTINATION
&& portlist
[i
] == src
)
45 if (flags
!= XT_MULTIPORT_SOURCE
&& portlist
[i
] == dst
)
52 /* Returns 1 if the port is matched by the test, 0 otherwise. */
54 ports_match_v1(const struct xt_multiport_v1
*minfo
,
55 u_int16_t src
, u_int16_t dst
)
60 for (i
= 0; i
< minfo
->count
; i
++) {
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
);
68 if (minfo
->flags
== XT_MULTIPORT_SOURCE
69 && src
>= s
&& src
<= e
)
70 return 1 ^ minfo
->invert
;
71 if (minfo
->flags
== XT_MULTIPORT_DESTINATION
72 && dst
>= s
&& dst
<= e
)
73 return 1 ^ minfo
->invert
;
74 if (minfo
->flags
== XT_MULTIPORT_EITHER
75 && ((dst
>= s
&& dst
<= e
)
76 || (src
>= s
&& src
<= e
)))
77 return 1 ^ minfo
->invert
;
79 /* exact port matching */
80 duprintf("src or dst matches with %d?\n", s
);
82 if (minfo
->flags
== XT_MULTIPORT_SOURCE
84 return 1 ^ minfo
->invert
;
85 if (minfo
->flags
== XT_MULTIPORT_DESTINATION
87 return 1 ^ minfo
->invert
;
88 if (minfo
->flags
== XT_MULTIPORT_EITHER
89 && (src
== s
|| dst
== s
))
90 return 1 ^ minfo
->invert
;
98 match(const struct sk_buff
*skb
,
99 const struct net_device
*in
,
100 const struct net_device
*out
,
101 const struct xt_match
*match
,
102 const void *matchinfo
,
104 unsigned int protoff
,
107 u16 _ports
[2], *pptr
;
108 const struct xt_multiport
*multiinfo
= matchinfo
;
113 pptr
= skb_header_pointer(skb
, protoff
, sizeof(_ports
), _ports
);
115 /* We've been asked to examine this packet, and we
116 * can't. Hence, no choice but to drop.
118 duprintf("xt_multiport: Dropping evil offset=0 tinygram.\n");
123 return ports_match(multiinfo
->ports
,
124 multiinfo
->flags
, multiinfo
->count
,
125 ntohs(pptr
[0]), ntohs(pptr
[1]));
129 match_v1(const struct sk_buff
*skb
,
130 const struct net_device
*in
,
131 const struct net_device
*out
,
132 const struct xt_match
*match
,
133 const void *matchinfo
,
135 unsigned int protoff
,
138 u16 _ports
[2], *pptr
;
139 const struct xt_multiport_v1
*multiinfo
= matchinfo
;
144 pptr
= skb_header_pointer(skb
, protoff
, 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("xt_multiport: Dropping evil offset=0 tinygram.\n");
154 return ports_match_v1(multiinfo
, ntohs(pptr
[0]), ntohs(pptr
[1]));
158 check(u_int16_t proto
,
159 u_int8_t ip_invflags
,
160 u_int8_t match_flags
,
163 /* Must specify supported protocol, no unknown flags or bad count */
164 return (proto
== IPPROTO_TCP
|| proto
== IPPROTO_UDP
165 || proto
== IPPROTO_SCTP
|| proto
== IPPROTO_DCCP
)
166 && !(ip_invflags
& XT_INV_PROTO
)
167 && (match_flags
== XT_MULTIPORT_SOURCE
168 || match_flags
== XT_MULTIPORT_DESTINATION
169 || match_flags
== XT_MULTIPORT_EITHER
)
170 && count
<= XT_MULTI_PORTS
;
173 /* Called when user tries to insert an entry of this type. */
175 checkentry(const char *tablename
,
177 const struct xt_match
*match
,
179 unsigned int matchsize
,
180 unsigned int hook_mask
)
182 const struct ipt_ip
*ip
= info
;
183 const struct xt_multiport
*multiinfo
= matchinfo
;
185 return check(ip
->proto
, ip
->invflags
, multiinfo
->flags
,
190 checkentry_v1(const char *tablename
,
192 const struct xt_match
*match
,
194 unsigned int matchsize
,
195 unsigned int hook_mask
)
197 const struct ipt_ip
*ip
= info
;
198 const struct xt_multiport_v1
*multiinfo
= matchinfo
;
200 return check(ip
->proto
, ip
->invflags
, multiinfo
->flags
,
205 checkentry6(const char *tablename
,
207 const struct xt_match
*match
,
209 unsigned int matchsize
,
210 unsigned int hook_mask
)
212 const struct ip6t_ip6
*ip
= info
;
213 const struct xt_multiport
*multiinfo
= matchinfo
;
215 return check(ip
->proto
, ip
->invflags
, multiinfo
->flags
,
220 checkentry6_v1(const char *tablename
,
222 const struct xt_match
*match
,
224 unsigned int matchsize
,
225 unsigned int hook_mask
)
227 const struct ip6t_ip6
*ip
= info
;
228 const struct xt_multiport_v1
*multiinfo
= matchinfo
;
230 return check(ip
->proto
, ip
->invflags
, multiinfo
->flags
,
234 static struct xt_match multiport_match
= {
237 .matchsize
= sizeof(struct xt_multiport
),
239 .checkentry
= &checkentry
,
244 static struct xt_match multiport_match_v1
= {
247 .matchsize
= sizeof(struct xt_multiport_v1
),
249 .checkentry
= &checkentry_v1
,
254 static struct xt_match multiport6_match
= {
257 .matchsize
= sizeof(struct xt_multiport
),
259 .checkentry
= &checkentry6
,
264 static struct xt_match multiport6_match_v1
= {
267 .matchsize
= sizeof(struct xt_multiport_v1
),
269 .checkentry
= &checkentry6_v1
,
274 static int __init
xt_multiport_init(void)
278 ret
= xt_register_match(&multiport_match
);
282 ret
= xt_register_match(&multiport_match_v1
);
284 goto out_unreg_multi_v0
;
286 ret
= xt_register_match(&multiport6_match
);
288 goto out_unreg_multi_v1
;
290 ret
= xt_register_match(&multiport6_match_v1
);
292 goto out_unreg_multi6_v0
;
297 xt_unregister_match(&multiport6_match
);
299 xt_unregister_match(&multiport_match_v1
);
301 xt_unregister_match(&multiport_match
);
306 static void __exit
xt_multiport_fini(void)
308 xt_unregister_match(&multiport_match
);
309 xt_unregister_match(&multiport_match_v1
);
310 xt_unregister_match(&multiport6_match
);
311 xt_unregister_match(&multiport6_match_v1
);
314 module_init(xt_multiport_init
);
315 module_exit(xt_multiport_fini
);