Modified br2684 patch to remove unresolved symbol, should now work, needs testing
[openwrt/svn-archive/archive.git] / openwrt / target / linux / generic-2.4 / patches / 619-nefilter_multiport_backport.patch
1 diff -urN linux.old/include/linux/netfilter_ipv4/ipt_multiport.h linux.dev/include/linux/netfilter_ipv4/ipt_multiport.h
2 --- linux.old/include/linux/netfilter_ipv4/ipt_multiport.h 2000-12-11 22:31:30.000000000 +0100
3 +++ linux.dev/include/linux/netfilter_ipv4/ipt_multiport.h 2006-02-04 05:23:54.318518250 +0100
4 @@ -11,11 +11,12 @@
5
6 #define IPT_MULTI_PORTS 15
7
8 -/* Must fit inside union ipt_matchinfo: 16 bytes */
9 -struct ipt_multiport
10 +struct ipt_multiport_v1
11 {
12 u_int8_t flags; /* Type of comparison */
13 u_int8_t count; /* Number of ports */
14 u_int16_t ports[IPT_MULTI_PORTS]; /* Ports */
15 + u_int8_t pflags[IPT_MULTI_PORTS]; /* Port flags */
16 + u_int8_t invert; /* Invert flag */
17 };
18 #endif /*_IPT_MULTIPORT_H*/
19 diff -urN linux.old/net/ipv4/netfilter/ipt_multiport.c linux.dev/net/ipv4/netfilter/ipt_multiport.c
20 --- linux.old/net/ipv4/netfilter/ipt_multiport.c 2003-06-13 16:51:39.000000000 +0200
21 +++ linux.dev/net/ipv4/netfilter/ipt_multiport.c 2006-02-04 05:34:27.362081000 +0100
22 @@ -1,5 +1,14 @@
23 /* Kernel module to match one of a list of TCP/UDP ports: ports are in
24 the same place so we can treat them as equal. */
25 +
26 +/* (C) 1999-2001 Paul `Rusty' Russell
27 + * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
28 + *
29 + * This program is free software; you can redistribute it and/or modify
30 + * it under the terms of the GNU General Public License version 2 as
31 + * published by the Free Software Foundation.
32 + */
33 +
34 #include <linux/module.h>
35 #include <linux/types.h>
36 #include <linux/udp.h>
37 @@ -8,97 +17,136 @@
38 #include <linux/netfilter_ipv4/ipt_multiport.h>
39 #include <linux/netfilter_ipv4/ip_tables.h>
40
41 +MODULE_LICENSE("GPL");
42 +MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
43 +MODULE_DESCRIPTION("iptables multiple port match module");
44 +
45 #if 0
46 #define duprintf(format, args...) printk(format , ## args)
47 #else
48 #define duprintf(format, args...)
49 #endif
50
51 +/* from linux 2.6 skbuff.h */
52 +static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
53 + int len, void *buffer)
54 +{
55 + int hlen = skb_headlen(skb);
56 +
57 + if (hlen - offset >= len)
58 + return skb->data + offset;
59 +
60 + if (skb_copy_bits(skb, offset, buffer, len) < 0)
61 + return NULL;
62 +
63 + return buffer;
64 +}
65 +
66 +
67 /* Returns 1 if the port is matched by the test, 0 otherwise. */
68 static inline int
69 -ports_match(const u_int16_t *portlist, enum ipt_multiport_flags flags,
70 - u_int8_t count, u_int16_t src, u_int16_t dst)
71 +ports_match_v1(const struct ipt_multiport_v1 *minfo,
72 + u_int16_t src, u_int16_t dst)
73 {
74 unsigned int i;
75 - for (i=0; i<count; i++) {
76 - if (flags != IPT_MULTIPORT_DESTINATION
77 - && portlist[i] == src)
78 - return 1;
79 -
80 - if (flags != IPT_MULTIPORT_SOURCE
81 - && portlist[i] == dst)
82 - return 1;
83 - }
84 + u_int16_t s, e;
85
86 - return 0;
87 + for (i=0; i < minfo->count; i++) {
88 + s = minfo->ports[i];
89 +
90 + if (minfo->pflags[i]) {
91 + /* range port matching */
92 + e = minfo->ports[++i];
93 + duprintf("src or dst matches with %d-%d?\n", s, e);
94 +
95 + if (minfo->flags == IPT_MULTIPORT_SOURCE
96 + && src >= s && src <= e)
97 + return 1 ^ minfo->invert;
98 + if (minfo->flags == IPT_MULTIPORT_DESTINATION
99 + && dst >= s && dst <= e)
100 + return 1 ^ minfo->invert;
101 + if (minfo->flags == IPT_MULTIPORT_EITHER
102 + && ((dst >= s && dst <= e)
103 + || (src >= s && src <= e)))
104 + return 1 ^ minfo->invert;
105 + } else {
106 + /* exact port matching */
107 + duprintf("src or dst matches with %d?\n", s);
108 +
109 + if (minfo->flags == IPT_MULTIPORT_SOURCE
110 + && src == s)
111 + return 1 ^ minfo->invert;
112 + if (minfo->flags == IPT_MULTIPORT_DESTINATION
113 + && dst == s)
114 + return 1 ^ minfo->invert;
115 + if (minfo->flags == IPT_MULTIPORT_EITHER
116 + && (src == s || dst == s))
117 + return 1 ^ minfo->invert;
118 + }
119 + }
120 +
121 + return minfo->invert;
122 }
123
124 static int
125 -match(const struct sk_buff *skb,
126 - const struct net_device *in,
127 - const struct net_device *out,
128 - const void *matchinfo,
129 - int offset,
130 - const void *hdr,
131 - u_int16_t datalen,
132 - int *hotdrop)
133 +match_v1(const struct sk_buff *skb,
134 + const struct net_device *in,
135 + const struct net_device *out,
136 + const void *matchinfo,
137 + int offset,
138 + int *hotdrop)
139 {
140 - const struct udphdr *udp = hdr;
141 - const struct ipt_multiport *multiinfo = matchinfo;
142 + u16 _ports[2], *pptr;
143 + const struct ipt_multiport_v1 *multiinfo = matchinfo;
144 +
145 + if (offset)
146 + return 0;
147
148 - /* Must be big enough to read ports. */
149 - if (offset == 0 && datalen < sizeof(struct udphdr)) {
150 + pptr = skb_header_pointer(skb, skb->nh.iph->ihl * 4,
151 + sizeof(_ports), _ports);
152 + if (pptr == NULL) {
153 /* We've been asked to examine this packet, and we
154 - can't. Hence, no choice but to drop. */
155 - duprintf("ipt_multiport:"
156 - " Dropping evil offset=0 tinygram.\n");
157 - *hotdrop = 1;
158 - return 0;
159 + * can't. Hence, no choice but to drop.
160 + */
161 + duprintf("ipt_multiport:"
162 + " Dropping evil offset=0 tinygram.\n");
163 + *hotdrop = 1;
164 + return 0;
165 }
166
167 - /* Must not be a fragment. */
168 - return !offset
169 - && ports_match(multiinfo->ports,
170 - multiinfo->flags, multiinfo->count,
171 - ntohs(udp->source), ntohs(udp->dest));
172 + return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1]));
173 }
174
175 -/* Called when user tries to insert an entry of this type. */
176 static int
177 -checkentry(const char *tablename,
178 - const struct ipt_ip *ip,
179 - void *matchinfo,
180 - unsigned int matchsize,
181 - unsigned int hook_mask)
182 +checkentry_v1(const char *tablename,
183 + const struct ipt_ip *ip,
184 + void *matchinfo,
185 + unsigned int matchsize,
186 + unsigned int hook_mask)
187 {
188 - const struct ipt_multiport *multiinfo = matchinfo;
189 -
190 - if (matchsize != IPT_ALIGN(sizeof(struct ipt_multiport)))
191 - return 0;
192 -
193 - /* Must specify proto == TCP/UDP, no unknown flags or bad count */
194 - return (ip->proto == IPPROTO_TCP || ip->proto == IPPROTO_UDP)
195 - && !(ip->invflags & IPT_INV_PROTO)
196 - && matchsize == IPT_ALIGN(sizeof(struct ipt_multiport))
197 - && (multiinfo->flags == IPT_MULTIPORT_SOURCE
198 - || multiinfo->flags == IPT_MULTIPORT_DESTINATION
199 - || multiinfo->flags == IPT_MULTIPORT_EITHER)
200 - && multiinfo->count <= IPT_MULTI_PORTS;
201 + return (matchsize == IPT_ALIGN(sizeof(struct ipt_multiport_v1)));
202 }
203
204 -static struct ipt_match multiport_match
205 -= { { NULL, NULL }, "multiport", &match, &checkentry, NULL, THIS_MODULE };
206 +static struct ipt_match multiport_match_v1 = {
207 + .name = "multiport",
208 + .match = &match_v1,
209 + .checkentry = &checkentry_v1,
210 + .me = THIS_MODULE,
211 +};
212
213 static int __init init(void)
214 {
215 - return ipt_register_match(&multiport_match);
216 + int err;
217 +
218 + err = ipt_register_match(&multiport_match_v1);
219 +
220 + return err;
221 }
222
223 static void __exit fini(void)
224 {
225 - ipt_unregister_match(&multiport_match);
226 + ipt_unregister_match(&multiport_match_v1);
227 }
228
229 module_init(init);
230 module_exit(fini);
231 -MODULE_LICENSE("GPL");