adding the patch file too
[linux-2.6.git] / linux-2.6-522-iptables-connection-tagging.patch
1 diff -Nurb linux-2.6.22-521/include/linux/netfilter/xt_MARK.h linux-2.6.22-522/include/linux/netfilter/xt_MARK.h
2 --- linux-2.6.22-521/include/linux/netfilter/xt_MARK.h  2007-07-08 19:32:17.000000000 -0400
3 +++ linux-2.6.22-522/include/linux/netfilter/xt_MARK.h  2008-07-13 23:58:41.000000000 -0400
4 @@ -11,6 +11,7 @@
5         XT_MARK_SET=0,
6         XT_MARK_AND,
7         XT_MARK_OR,
8 +       XT_MARK_COPYXID,
9  };
10  
11  struct xt_mark_target_info_v1 {
12 diff -Nurb linux-2.6.22-521/include/linux/netfilter/xt_SETXID.h linux-2.6.22-522/include/linux/netfilter/xt_SETXID.h
13 --- linux-2.6.22-521/include/linux/netfilter/xt_SETXID.h        1969-12-31 19:00:00.000000000 -0500
14 +++ linux-2.6.22-522/include/linux/netfilter/xt_SETXID.h        2008-07-13 23:58:41.000000000 -0400
15 @@ -0,0 +1,14 @@
16 +#ifndef _XT_SETXID_H_target
17 +#define _XT_SETXID_H_target
18 +
19 +/* Version 1 */
20 +enum {
21 +       XT_SET_PACKET_XID=0
22 +};
23 +
24 +struct xt_setxid_target_info_v1 {
25 +       unsigned long mark;
26 +       u_int8_t mode;
27 +};
28 +
29 +#endif /*_XT_SETXID_H_target*/
30 diff -Nurb linux-2.6.22-521/include/linux/netfilter_ipv4/ipt_MARK.h linux-2.6.22-522/include/linux/netfilter_ipv4/ipt_MARK.h
31 --- linux-2.6.22-521/include/linux/netfilter_ipv4/ipt_MARK.h    2007-07-08 19:32:17.000000000 -0400
32 +++ linux-2.6.22-522/include/linux/netfilter_ipv4/ipt_MARK.h    2008-07-13 23:58:41.000000000 -0400
33 @@ -12,6 +12,7 @@
34  #define IPT_MARK_SET   XT_MARK_SET
35  #define IPT_MARK_AND   XT_MARK_AND
36  #define        IPT_MARK_OR     XT_MARK_OR
37 +#define IPT_MARK_COPYXID       XT_MARK_COPYXID
38  
39  #define ipt_mark_target_info_v1 xt_mark_target_info_v1
40  
41 diff -Nurb linux-2.6.22-521/include/linux/netfilter_ipv4/ipt_SETXID.h linux-2.6.22-522/include/linux/netfilter_ipv4/ipt_SETXID.h
42 --- linux-2.6.22-521/include/linux/netfilter_ipv4/ipt_SETXID.h  1969-12-31 19:00:00.000000000 -0500
43 +++ linux-2.6.22-522/include/linux/netfilter_ipv4/ipt_SETXID.h  2008-07-13 23:58:41.000000000 -0400
44 @@ -0,0 +1,13 @@
45 +#ifndef _IPT_SETXID_H_target
46 +#define _IPT_SETXID_H_target
47 +
48 +/* Backwards compatibility for old userspace */
49 +
50 +#include <linux/netfilter/xt_SETXID.h>
51 +
52 +/* Version 1 */
53 +#define IPT_SET_PACKET_XID     XT_SET_PACKET_XID
54 +
55 +#define ipt_setxid_target_info_v1 xt_setxid_target_info_v1
56 +
57 +#endif /*_IPT_SETXID_H_target*/
58 diff -Nurb linux-2.6.22-521/include/net/netfilter/nf_conntrack.h linux-2.6.22-522/include/net/netfilter/nf_conntrack.h
59 --- linux-2.6.22-521/include/net/netfilter/nf_conntrack.h       2007-07-08 19:32:17.000000000 -0400
60 +++ linux-2.6.22-522/include/net/netfilter/nf_conntrack.h       2008-07-13 23:58:41.000000000 -0400
61 @@ -131,6 +131,9 @@
62         /* Storage reserved for other modules: */
63         union nf_conntrack_proto proto;
64  
65 +       /* PLANETLAB. VNET-specific */
66 +       int xid[IP_CT_DIR_MAX];
67 +
68         /* features dynamically at the end: helper, nat (both optional) */
69         char data[0];
70  };
71 diff -Nurb linux-2.6.22-521/net/netfilter/Kconfig linux-2.6.22-522/net/netfilter/Kconfig
72 --- linux-2.6.22-521/net/netfilter/Kconfig      2007-07-08 19:32:17.000000000 -0400
73 +++ linux-2.6.22-522/net/netfilter/Kconfig      2008-07-13 23:58:41.000000000 -0400
74 @@ -389,6 +389,13 @@
75  
76           To compile it as a module, choose M here.  If unsure, say N.
77  
78 +config NETFILTER_XT_TARGET_SETXID
79 +       tristate '"SETXID" target support'
80 +       depends on NETFILTER_XTABLES
81 +       help
82 +         This option adds a `SETXID' target, which allows you to alter the
83 +         xid of a socket.
84 +
85  config NETFILTER_XT_MATCH_COMMENT
86         tristate  '"comment" match support'
87         depends on NETFILTER_XTABLES
88 diff -Nurb linux-2.6.22-521/net/netfilter/Makefile linux-2.6.22-522/net/netfilter/Makefile
89 --- linux-2.6.22-521/net/netfilter/Makefile     2007-07-08 19:32:17.000000000 -0400
90 +++ linux-2.6.22-522/net/netfilter/Makefile     2008-07-13 23:58:41.000000000 -0400
91 @@ -37,6 +37,7 @@
92  obj-$(CONFIG_NETFILTER_XTABLES) += x_tables.o xt_tcpudp.o
93  
94  # targets
95 +obj-$(CONFIG_NETFILTER_XT_TARGET_SETXID) += xt_SETXID.o
96  obj-$(CONFIG_NETFILTER_XT_TARGET_CLASSIFY) += xt_CLASSIFY.o
97  obj-$(CONFIG_NETFILTER_XT_TARGET_CONNMARK) += xt_CONNMARK.o
98  obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
99 diff -Nurb linux-2.6.22-521/net/netfilter/nf_conntrack_core.c linux-2.6.22-522/net/netfilter/nf_conntrack_core.c
100 --- linux-2.6.22-521/net/netfilter/nf_conntrack_core.c  2007-07-08 19:32:17.000000000 -0400
101 +++ linux-2.6.22-522/net/netfilter/nf_conntrack_core.c  2008-07-13 23:58:41.000000000 -0400
102 @@ -726,6 +726,8 @@
103  
104         /* Overload tuple linked list to put us in unconfirmed list. */
105         list_add(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].list, &unconfirmed);
106 +       conntrack->xid[IP_CT_DIR_ORIGINAL] = -1;
107 +       conntrack->xid[IP_CT_DIR_REPLY] = -1;
108  
109         write_unlock_bh(&nf_conntrack_lock);
110  
111 diff -Nurb linux-2.6.22-521/net/netfilter/xt_MARK.c linux-2.6.22-522/net/netfilter/xt_MARK.c
112 --- linux-2.6.22-521/net/netfilter/xt_MARK.c    2007-07-08 19:32:17.000000000 -0400
113 +++ linux-2.6.22-522/net/netfilter/xt_MARK.c    2008-07-15 11:37:03.000000000 -0400
114 @@ -5,13 +5,18 @@
115   * This program is free software; you can redistribute it and/or modify
116   * it under the terms of the GNU General Public License version 2 as
117   * published by the Free Software Foundation.
118 + *
119   */
120  
121  #include <linux/module.h>
122 +#include <linux/version.h>
123  #include <linux/skbuff.h>
124  #include <linux/ip.h>
125  #include <net/checksum.h>
126 +#include <net/route.h>
127 +#include <net/inet_hashtables.h>
128  
129 +#include <net/netfilter/nf_conntrack.h>
130  #include <linux/netfilter/x_tables.h>
131  #include <linux/netfilter/xt_MARK.h>
132  
133 @@ -21,6 +26,48 @@
134  MODULE_ALIAS("ipt_MARK");
135  MODULE_ALIAS("ip6t_MARK");
136  
137 +static inline u_int16_t
138 +get_dst_port(struct nf_conntrack_tuple *tuple)
139 +{
140 +       switch (tuple->dst.protonum) {
141 +       case IPPROTO_GRE:
142 +               /* XXX Truncate 32-bit GRE key to 16 bits */
143 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
144 +               return tuple->dst.u.gre.key;
145 +#else
146 +               return htons(ntohl(tuple->dst.u.gre.key));
147 +#endif  
148 +       case IPPROTO_ICMP:
149 +               /* Bind on ICMP echo ID */
150 +               return tuple->src.u.icmp.id;
151 +       case IPPROTO_TCP:
152 +               return tuple->dst.u.tcp.port;
153 +       case IPPROTO_UDP:
154 +               return tuple->dst.u.udp.port;
155 +       default:
156 +               return tuple->dst.u.all;
157 +       }
158 +}
159 +
160 +static inline u_int16_t
161 +get_src_port(struct nf_conntrack_tuple *tuple)
162 +{
163 +       switch (tuple->dst.protonum) {
164 +       case IPPROTO_GRE:
165 +               /* XXX Truncate 32-bit GRE key to 16 bits */
166 +               return htons(ntohl(tuple->src.u.gre.key));
167 +       case IPPROTO_ICMP:
168 +               /* Bind on ICMP echo ID */
169 +               return tuple->src.u.icmp.id;
170 +       case IPPROTO_TCP:
171 +               return tuple->src.u.tcp.port;
172 +       case IPPROTO_UDP:
173 +               return tuple->src.u.udp.port;
174 +       default:
175 +               return tuple->src.u.all;
176 +       }
177 +}
178 +
179  static unsigned int
180  target_v0(struct sk_buff **pskb,
181           const struct net_device *in,
182 @@ -35,6 +82,8 @@
183         return XT_CONTINUE;
184  }
185  
186 +extern DEFINE_PER_CPU(int, sknid_elevator);
187 +
188  static unsigned int
189  target_v1(struct sk_buff **pskb,
190           const struct net_device *in,
191 @@ -44,7 +93,20 @@
192           const void *targinfo)
193  {
194         const struct xt_mark_target_info_v1 *markinfo = targinfo;
195 -       int mark = 0;
196 +       enum ip_conntrack_info ctinfo;
197 +      struct sock *connection_sk;
198 +      int dif;
199 +      struct nf_conn *ct;
200 +      extern struct inet_hashinfo tcp_hashinfo;
201 +      enum ip_conntrack_dir dir;
202 +      int *curtag;
203 +      u_int32_t src_ip;
204 +      u_int32_t dst_ip;
205 +      u_int16_t proto, src_port;
206 +      u_int32_t ip;
207 +      u_int16_t port;
208 +
209 +       int mark = -1;
210  
211         switch (markinfo->mode) {
212         case XT_MARK_SET:
213 @@ -58,13 +120,75 @@
214         case XT_MARK_OR:
215                 mark = (*pskb)->mark | markinfo->mark;
216                 break;
217 +
218 +               case XT_MARK_COPYXID: 
219 +                                             
220 +                                             ct = nf_ct_get((*pskb), &ctinfo);
221 +                                             if (!ct) 
222 +                                                     break;
223 +
224 +                                             dir = CTINFO2DIR(ctinfo);
225 +                                             src_ip = ct->tuplehash[dir].tuple.src.u3.ip;
226 +                                             dst_ip = ct->tuplehash[dir].tuple.dst.u3.ip;
227 +                                             src_port = get_src_port(&ct->tuplehash[dir].tuple);
228 +                                             proto = ct->tuplehash[dir].tuple.dst.protonum;
229 +
230 +                                             dif = ((struct rtable *)(*pskb)->dst)->rt_iif;
231 +                                             ip = ct->tuplehash[dir].tuple.dst.u3.ip;
232 +                                             port = get_dst_port(&ct->tuplehash[dir].tuple);
233 +
234 +                                             if (proto == 1 || proto == 17) {
235 +                                                     if ((*pskb)->mark>0) /* The packet is marked, it's going out */
236 +                                                     {
237 +                                                               //if (ct->xid[0]>0 && ct->xid[0]!=(*pskb)->mark)
238 +                                                                       /*printk(KERN_CRIT "xt_MARK log: %d/%d/%d/%d\n",ct->xid[0],(*pskb)->mark,hooknum==NF_IP_LOCAL_IN,proto);*/
239 +
240 +                                                               ct->xid[0]=(*pskb)->mark;
241         }
242  
243 +                                                     if (ct->xid[0] > 0) {
244 +                                                             mark = ct->xid[0];
245 +                                                     }
246 +
247 +                                             }
248 +                                             else if (proto == 6) { 
249 +                                                     if ((*pskb)->sk) {
250 +                                                             connection_sk = (*pskb)->sk;
251 +                                                             sock_hold(connection_sk);
252 +                                                     }
253 +                                                     else 
254 +                                                             connection_sk = inet_lookup_established(&tcp_hashinfo, src_ip, src_port, ip, port, dif);
255 +                                                             
256 +
257 +                                                     if (connection_sk) {
258 +                                                             connection_sk->sk_peercred.gid = connection_sk->sk_peercred.uid = ct->xid[dir];
259 +                                                             ct->xid[!dir]=connection_sk->sk_nid;
260 +                                                             if (connection_sk->sk_nid != 0) 
261 +                                                                     mark = connection_sk->sk_nid;
262 +                                                             if (connection_sk->sk_state == TCP_TIME_WAIT) {
263 +                                                                     inet_twsk_put(inet_twsk(connection_sk));
264 +                                                                     break;
265 +                                                             }
266 +                                                             else
267 +                                                                     sock_put(connection_sk);
268 +                                                     }
269 +                                                     else 
270 +                                                             mark = -1 ; 
271 +                                             }
272 +                                             break;
273 +       }
274 +       if (mark != -1) {
275         (*pskb)->mark = mark;
276 +       }
277 +
278 +       curtag=&__get_cpu_var(sknid_elevator);
279 +       if (mark > 0 && *curtag==-2) 
280 +       {
281 +               *curtag = mark;
282 +       }
283         return XT_CONTINUE;
284  }
285  
286 -
287  static int
288  checkentry_v0(const char *tablename,
289               const void *entry,
290 @@ -92,7 +216,8 @@
291  
292         if (markinfo->mode != XT_MARK_SET
293             && markinfo->mode != XT_MARK_AND
294 -           && markinfo->mode != XT_MARK_OR) {
295 +           && markinfo->mode != XT_MARK_OR
296 +           && markinfo->mode != XT_MARK_COPYXID) {
297                 printk(KERN_WARNING "MARK: unknown mode %u\n",
298                        markinfo->mode);
299                 return 0;
300 diff -Nurb linux-2.6.22-521/net/netfilter/xt_SETXID.c linux-2.6.22-522/net/netfilter/xt_SETXID.c
301 --- linux-2.6.22-521/net/netfilter/xt_SETXID.c  1969-12-31 19:00:00.000000000 -0500
302 +++ linux-2.6.22-522/net/netfilter/xt_SETXID.c  2008-07-13 23:58:41.000000000 -0400
303 @@ -0,0 +1,79 @@
304 +#include <linux/module.h>
305 +#include <linux/skbuff.h>
306 +#include <linux/ip.h>
307 +#include <net/checksum.h>
308 +#include <linux/vs_network.h>
309 +
310 +#include <linux/netfilter/x_tables.h>
311 +#include <linux/netfilter/xt_SETXID.h>
312 +
313 +MODULE_LICENSE("GPL");
314 +MODULE_AUTHOR("");
315 +MODULE_DESCRIPTION("");
316 +MODULE_ALIAS("ipt_SETXID");
317 +
318 +static unsigned int
319 +target_v1(struct sk_buff **pskb,
320 +         const struct net_device *in,
321 +         const struct net_device *out,
322 +         unsigned int hooknum,
323 +         const struct xt_target *target,
324 +         const void *targinfo)
325 +{
326 +       const struct xt_setxid_target_info_v1 *setxidinfo = targinfo;
327 +
328 +       switch (setxidinfo->mode) {
329 +       case XT_SET_PACKET_XID:
330 +                (*pskb)->skb_tag = setxidinfo->mark;
331 +               break;
332 +       }
333 +       return XT_CONTINUE;
334 +}
335 +
336 +
337 +static int
338 +checkentry_v1(const char *tablename,
339 +             const void *entry,
340 +             const struct xt_target *target,
341 +             void *targinfo,
342 +             unsigned int hook_mask)
343 +{
344 +       struct xt_setxid_target_info_v1 *setxidinfo = targinfo;
345 +
346 +       if (setxidinfo->mode != XT_SET_PACKET_XID) {
347 +               printk(KERN_WARNING "SETXID: unknown mode %u\n",
348 +                      setxidinfo->mode);
349 +               return 0;
350 +       }
351 +
352 +       return 1;
353 +}
354 +
355 +static struct xt_target xt_setxid_target[] = {
356 +       {
357 +               .name           = "SETXID",
358 +               .family         = AF_INET,
359 +               .revision       = 1,
360 +               .checkentry     = checkentry_v1,
361 +               .target         = target_v1,
362 +               .targetsize     = sizeof(struct xt_setxid_target_info_v1),
363 +               .table          = "mangle",
364 +               .me             = THIS_MODULE,
365 +       }
366 +};
367 +
368 +static int __init init(void)
369 +{
370 +       int err;
371 +
372 +       err = xt_register_targets(xt_setxid_target, ARRAY_SIZE(xt_setxid_target));
373 +       return err;
374 +}
375 +
376 +static void __exit fini(void)
377 +{
378 +       xt_unregister_targets(xt_setxid_target, ARRAY_SIZE(xt_setxid_target));
379 +}
380 +
381 +module_init(init);
382 +module_exit(fini);