- more 2.6.17 port work (still does not build)
[linux-flexiantxendom0-3.2.10.git] / net / ipv4 / netfilter / ipt_multiport.c
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. */
3
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  */
11
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/udp.h>
15 #include <linux/skbuff.h>
16
17 #include <linux/netfilter_ipv4/ipt_multiport.h>
18 #include <linux/netfilter_ipv4/ip_tables.h>
19
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
22 MODULE_DESCRIPTION("iptables multiple port match module");
23
24 #if 0
25 #define duprintf(format, args...) printk(format , ## args)
26 #else
27 #define duprintf(format, args...)
28 #endif
29
30 /* Returns 1 if the port is matched by the test, 0 otherwise. */
31 static inline int
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)
34 {
35         unsigned int i;
36         for (i=0; i<count; i++) {
37                 if (flags != IPT_MULTIPORT_DESTINATION
38                     && portlist[i] == src)
39                         return 1;
40
41                 if (flags != IPT_MULTIPORT_SOURCE
42                     && portlist[i] == dst)
43                         return 1;
44         }
45
46         return 0;
47 }
48
49 /* Returns 1 if the port is matched by the test, 0 otherwise. */
50 static inline int
51 ports_match_v1(const struct ipt_multiport_v1 *minfo,
52                u_int16_t src, u_int16_t dst)
53 {
54         unsigned int i;
55         u_int16_t s, e;
56
57         for (i=0; i < minfo->count; i++) {
58                 s = minfo->ports[i];
59
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);
64
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;
75                 } else {
76                         /* exact port matching */
77                         duprintf("src or dst matches with %d?\n", s);
78
79                         if (minfo->flags == IPT_MULTIPORT_SOURCE
80                             && src == s)
81                                 return 1 ^ minfo->invert;
82                         if (minfo->flags == IPT_MULTIPORT_DESTINATION
83                             && dst == s)
84                                 return 1 ^ minfo->invert;
85                         if (minfo->flags == IPT_MULTIPORT_EITHER
86                             && (src == s || dst == s))
87                                 return 1 ^ minfo->invert;
88                 }
89         }
90  
91         return minfo->invert;
92 }
93
94 static int
95 match(const struct sk_buff *skb,
96       const struct net_device *in,
97       const struct net_device *out,
98       const void *matchinfo,
99       int offset,
100       unsigned int protoff,
101       int *hotdrop)
102 {
103         u16 _ports[2], *pptr;
104         const struct ipt_multiport *multiinfo = matchinfo;
105
106         if (offset)
107                 return 0;
108
109         pptr = skb_header_pointer(skb, protoff,
110                                   sizeof(_ports), _ports);
111         if (pptr == NULL) {
112                 /* We've been asked to examine this packet, and we
113                  * can't.  Hence, no choice but to drop.
114                  */
115                 duprintf("ipt_multiport:"
116                          " Dropping evil offset=0 tinygram.\n");
117                 *hotdrop = 1;
118                 return 0;
119         }
120
121         return ports_match(multiinfo->ports,
122                            multiinfo->flags, multiinfo->count,
123                            ntohs(pptr[0]), ntohs(pptr[1]));
124 }
125
126 static int
127 match_v1(const struct sk_buff *skb,
128          const struct net_device *in,
129          const struct net_device *out,
130          const void *matchinfo,
131          int offset,
132          unsigned int protoff,
133          int *hotdrop)
134 {
135         u16 _ports[2], *pptr;
136         const struct ipt_multiport_v1 *multiinfo = matchinfo;
137
138         if (offset)
139                 return 0;
140
141         pptr = skb_header_pointer(skb, protoff,
142                                   sizeof(_ports), _ports);
143         if (pptr == NULL) {
144                 /* We've been asked to examine this packet, and we
145                  * can't.  Hence, no choice but to drop.
146                  */
147                 duprintf("ipt_multiport:"
148                          " Dropping evil offset=0 tinygram.\n");
149                 *hotdrop = 1;
150                 return 0;
151         }
152
153         return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1]));
154 }
155
156 /* Called when user tries to insert an entry of this type. */
157 static int
158 checkentry(const char *tablename,
159            const void *ip,
160            void *matchinfo,
161            unsigned int matchsize,
162            unsigned int hook_mask)
163 {
164         return (matchsize == IPT_ALIGN(sizeof(struct ipt_multiport)));
165 }
166
167 static int
168 checkentry_v1(const char *tablename,
169               const void *ip,
170               void *matchinfo,
171               unsigned int matchsize,
172               unsigned int hook_mask)
173 {
174         return (matchsize == IPT_ALIGN(sizeof(struct ipt_multiport_v1)));
175 }
176
177 static struct ipt_match multiport_match = {
178         .name           = "multiport",
179         .revision       = 0,
180         .match          = &match,
181         .checkentry     = &checkentry,
182         .me             = THIS_MODULE,
183 };
184
185 static struct ipt_match multiport_match_v1 = {
186         .name           = "multiport",
187         .revision       = 1,
188         .match          = &match_v1,
189         .checkentry     = &checkentry_v1,
190         .me             = THIS_MODULE,
191 };
192
193 static int __init init(void)
194 {
195         int err;
196
197         err = ipt_register_match(&multiport_match);
198         if (!err) {
199                 err = ipt_register_match(&multiport_match_v1);
200                 if (err)
201                         ipt_unregister_match(&multiport_match);
202         }
203
204         return err;
205 }
206
207 static void __exit fini(void)
208 {
209         ipt_unregister_match(&multiport_match);
210         ipt_unregister_match(&multiport_match_v1);
211 }
212
213 module_init(init);
214 module_exit(fini);