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