[PATCH] stop_machine.c copyright
[linux-2.6/sactl.git] / net / netfilter / xt_multiport.c
blobd3aefd38093042f6d12b85f11b807544ffb597ee
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>
16 #include <linux/in.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");
29 #if 0
30 #define duprintf(format, args...) printk(format , ## args)
31 #else
32 #define duprintf(format, args...)
33 #endif
35 /* Returns 1 if the port is matched by the test, 0 otherwise. */
36 static inline int
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)
40 unsigned int i;
41 for (i = 0; i < count; i++) {
42 if (flags != XT_MULTIPORT_DESTINATION && portlist[i] == src)
43 return 1;
45 if (flags != XT_MULTIPORT_SOURCE && portlist[i] == dst)
46 return 1;
49 return 0;
52 /* Returns 1 if the port is matched by the test, 0 otherwise. */
53 static inline int
54 ports_match_v1(const struct xt_multiport_v1 *minfo,
55 u_int16_t src, u_int16_t dst)
57 unsigned int i;
58 u_int16_t s, e;
60 for (i = 0; i < minfo->count; i++) {
61 s = minfo->ports[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;
78 } else {
79 /* exact port matching */
80 duprintf("src or dst matches with %d?\n", s);
82 if (minfo->flags == XT_MULTIPORT_SOURCE
83 && src == s)
84 return 1 ^ minfo->invert;
85 if (minfo->flags == XT_MULTIPORT_DESTINATION
86 && dst == s)
87 return 1 ^ minfo->invert;
88 if (minfo->flags == XT_MULTIPORT_EITHER
89 && (src == s || dst == s))
90 return 1 ^ minfo->invert;
94 return minfo->invert;
97 static int
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,
103 int offset,
104 unsigned int protoff,
105 int *hotdrop)
107 u16 _ports[2], *pptr;
108 const struct xt_multiport *multiinfo = matchinfo;
110 if (offset)
111 return 0;
113 pptr = skb_header_pointer(skb, protoff, sizeof(_ports), _ports);
114 if (pptr == NULL) {
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");
119 *hotdrop = 1;
120 return 0;
123 return ports_match(multiinfo->ports,
124 multiinfo->flags, multiinfo->count,
125 ntohs(pptr[0]), ntohs(pptr[1]));
128 static int
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,
134 int offset,
135 unsigned int protoff,
136 int *hotdrop)
138 u16 _ports[2], *pptr;
139 const struct xt_multiport_v1 *multiinfo = matchinfo;
141 if (offset)
142 return 0;
144 pptr = skb_header_pointer(skb, protoff, sizeof(_ports), _ports);
145 if (pptr == NULL) {
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");
150 *hotdrop = 1;
151 return 0;
154 return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1]));
157 static inline int
158 check(u_int16_t proto,
159 u_int8_t ip_invflags,
160 u_int8_t match_flags,
161 u_int8_t count)
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. */
174 static int
175 checkentry(const char *tablename,
176 const void *info,
177 const struct xt_match *match,
178 void *matchinfo,
179 unsigned int hook_mask)
181 const struct ipt_ip *ip = info;
182 const struct xt_multiport *multiinfo = matchinfo;
184 return check(ip->proto, ip->invflags, multiinfo->flags,
185 multiinfo->count);
188 static int
189 checkentry_v1(const char *tablename,
190 const void *info,
191 const struct xt_match *match,
192 void *matchinfo,
193 unsigned int hook_mask)
195 const struct ipt_ip *ip = info;
196 const struct xt_multiport_v1 *multiinfo = matchinfo;
198 return check(ip->proto, ip->invflags, multiinfo->flags,
199 multiinfo->count);
202 static int
203 checkentry6(const char *tablename,
204 const void *info,
205 const struct xt_match *match,
206 void *matchinfo,
207 unsigned int hook_mask)
209 const struct ip6t_ip6 *ip = info;
210 const struct xt_multiport *multiinfo = matchinfo;
212 return check(ip->proto, ip->invflags, multiinfo->flags,
213 multiinfo->count);
216 static int
217 checkentry6_v1(const char *tablename,
218 const void *info,
219 const struct xt_match *match,
220 void *matchinfo,
221 unsigned int hook_mask)
223 const struct ip6t_ip6 *ip = info;
224 const struct xt_multiport_v1 *multiinfo = matchinfo;
226 return check(ip->proto, ip->invflags, multiinfo->flags,
227 multiinfo->count);
230 static struct xt_match xt_multiport_match[] = {
232 .name = "multiport",
233 .family = AF_INET,
234 .revision = 0,
235 .checkentry = checkentry,
236 .match = match,
237 .matchsize = sizeof(struct xt_multiport),
238 .me = THIS_MODULE,
241 .name = "multiport",
242 .family = AF_INET,
243 .revision = 1,
244 .checkentry = checkentry_v1,
245 .match = match_v1,
246 .matchsize = sizeof(struct xt_multiport_v1),
247 .me = THIS_MODULE,
250 .name = "multiport",
251 .family = AF_INET6,
252 .revision = 0,
253 .checkentry = checkentry6,
254 .match = match,
255 .matchsize = sizeof(struct xt_multiport),
256 .me = THIS_MODULE,
259 .name = "multiport",
260 .family = AF_INET6,
261 .revision = 1,
262 .checkentry = checkentry6_v1,
263 .match = match_v1,
264 .matchsize = sizeof(struct xt_multiport_v1),
265 .me = THIS_MODULE,
269 static int __init xt_multiport_init(void)
271 return xt_register_matches(xt_multiport_match,
272 ARRAY_SIZE(xt_multiport_match));
275 static void __exit xt_multiport_fini(void)
277 xt_unregister_matches(xt_multiport_match,
278 ARRAY_SIZE(xt_multiport_match));
281 module_init(xt_multiport_init);
282 module_exit(xt_multiport_fini);