Copy from trunk
[linux-2.6.git] / linux-2.6-520-vnet+.patch
1 diff -Nurb linux-2.6.22-510/include/linux/netfilter/xt_MARK.h linux-2.6.22-520/include/linux/netfilter/xt_MARK.h
2 --- linux-2.6.22-510/include/linux/netfilter/xt_MARK.h  2007-07-08 19:32:17.000000000 -0400
3 +++ linux-2.6.22-520/include/linux/netfilter/xt_MARK.h  2008-03-20 01:27:27.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-510/include/linux/netfilter/xt_SETXID.h linux-2.6.22-520/include/linux/netfilter/xt_SETXID.h
13 --- linux-2.6.22-510/include/linux/netfilter/xt_SETXID.h        1969-12-31 19:00:00.000000000 -0500
14 +++ linux-2.6.22-520/include/linux/netfilter/xt_SETXID.h        2008-03-20 01:27:27.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-510/include/linux/netfilter_ipv4/ipt_MARK.h linux-2.6.22-520/include/linux/netfilter_ipv4/ipt_MARK.h
31 --- linux-2.6.22-510/include/linux/netfilter_ipv4/ipt_MARK.h    2007-07-08 19:32:17.000000000 -0400
32 +++ linux-2.6.22-520/include/linux/netfilter_ipv4/ipt_MARK.h    2008-03-20 01:27:27.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-510/include/linux/netfilter_ipv4/ipt_SETXID.h linux-2.6.22-520/include/linux/netfilter_ipv4/ipt_SETXID.h
42 --- linux-2.6.22-510/include/linux/netfilter_ipv4/ipt_SETXID.h  1969-12-31 19:00:00.000000000 -0500
43 +++ linux-2.6.22-520/include/linux/netfilter_ipv4/ipt_SETXID.h  2008-03-20 01:27:27.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-510/include/linux/skbuff.h linux-2.6.22-520/include/linux/skbuff.h
59 --- linux-2.6.22-510/include/linux/skbuff.h     2007-07-08 19:32:17.000000000 -0400
60 +++ linux-2.6.22-520/include/linux/skbuff.h     2008-03-20 01:27:27.000000000 -0400
61 @@ -302,6 +302,7 @@
62  #endif
63  
64         __u32                   mark;
65 +#define skb_tag                        mark
66  
67         sk_buff_data_t          transport_header;
68         sk_buff_data_t          network_header;
69 diff -Nurb linux-2.6.22-510/include/linux/socket.h linux-2.6.22-520/include/linux/socket.h
70 --- linux-2.6.22-510/include/linux/socket.h     2007-07-08 19:32:17.000000000 -0400
71 +++ linux-2.6.22-520/include/linux/socket.h     2008-03-20 01:27:27.000000000 -0400
72 @@ -288,6 +288,8 @@
73  #define SOL_TIPC       271
74  #define SOL_RXRPC      272
75  
76 +#define SO_SETXID      SO_PEERCRED
77 +
78  /* IPX options */
79  #define IPX_TYPE       1
80  
81 diff -Nurb linux-2.6.22-510/include/linux/vserver/network.h linux-2.6.22-520/include/linux/vserver/network.h
82 --- linux-2.6.22-510/include/linux/vserver/network.h    2008-03-20 01:27:21.000000000 -0400
83 +++ linux-2.6.22-520/include/linux/vserver/network.h    2008-03-20 01:27:27.000000000 -0400
84 @@ -44,6 +44,8 @@
85  /* network caps */
86  
87  #define NXC_RAW_ICMP           0x00000100
88 +#define NXC_RAW_SOCKET         0x00000200
89 +#define NXC_RAW_SEND           0x00000400
90  
91  
92  /* address types */
93 diff -Nurb linux-2.6.22-510/include/linux/vserver/network.h.orig.orig linux-2.6.22-520/include/linux/vserver/network.h.orig.orig
94 --- linux-2.6.22-510/include/linux/vserver/network.h.orig.orig  1969-12-31 19:00:00.000000000 -0500
95 +++ linux-2.6.22-520/include/linux/vserver/network.h.orig.orig  2008-03-20 01:27:27.000000000 -0400
96 @@ -0,0 +1,143 @@
97 +#ifndef _VX_NETWORK_H
98 +#define _VX_NETWORK_H
99 +
100 +#include <linux/types.h>
101 +
102 +
103 +#define MAX_N_CONTEXT  65535   /* Arbitrary limit */
104 +
105 +
106 +/* network flags */
107 +
108 +#define NXF_INFO_PRIVATE       0x00000008
109 +
110 +#define NXF_SINGLE_IP          0x00000100
111 +#define NXF_LBACK_REMAP                0x00000200
112 +
113 +#define NXF_HIDE_NETIF         0x02000000
114 +#define NXF_HIDE_LBACK         0x04000000
115 +
116 +#define NXF_STATE_SETUP                (1ULL << 32)
117 +#define NXF_STATE_ADMIN                (1ULL << 34)
118 +
119 +#define NXF_SC_HELPER          (1ULL << 36)
120 +#define NXF_PERSISTENT         (1ULL << 38)
121 +
122 +#define NXF_ONE_TIME           (0x0005ULL << 32)
123 +
124 +
125 +#define        NXF_INIT_SET            (__nxf_init_set())
126 +
127 +static inline uint64_t __nxf_init_set(void) {
128 +       return    NXF_STATE_ADMIN
129 +#ifdef CONFIG_VSERVER_AUTO_LBACK
130 +               | NXF_LBACK_REMAP
131 +               | NXF_HIDE_LBACK
132 +#endif
133 +#ifdef CONFIG_VSERVER_AUTO_SINGLE
134 +               | NXF_SINGLE_IP
135 +#endif
136 +               | NXF_HIDE_NETIF;
137 +}
138 +
139 +
140 +/* network caps */
141 +
142 +#define NXC_RAW_ICMP           0x00000100
143 +
144 +
145 +/* address types */
146 +
147 +#define NXA_TYPE_IPV4          0x0001
148 +#define NXA_TYPE_IPV6          0x0002
149 +
150 +#define NXA_TYPE_NONE          0x0000
151 +#define NXA_TYPE_ANY           0x00FF
152 +
153 +#define NXA_TYPE_ADDR          0x0010
154 +#define NXA_TYPE_MASK          0x0020
155 +#define NXA_TYPE_RANGE         0x0040
156 +
157 +#define NXA_MASK_ALL           (NXA_TYPE_ADDR | NXA_TYPE_MASK | NXA_TYPE_RANGE)
158 +
159 +#define NXA_MOD_BCAST          0x0100
160 +#define NXA_MOD_LBACK          0x0200
161 +
162 +#define NXA_LOOPBACK           0x1000
163 +
164 +#define NXA_MASK_BIND          (NXA_MASK_ALL | NXA_MOD_BCAST | NXA_MOD_LBACK)
165 +#define NXA_MASK_SHOW          (NXA_MASK_ALL | NXA_LOOPBACK)
166 +
167 +#ifdef __KERNEL__
168 +
169 +#include <linux/list.h>
170 +#include <linux/spinlock.h>
171 +#include <linux/rcupdate.h>
172 +#include <linux/in.h>
173 +#include <linux/in6.h>
174 +#include <asm/atomic.h>
175 +
176 +struct nx_addr_v4 {
177 +       struct nx_addr_v4 *next;
178 +       struct in_addr ip[2];
179 +       struct in_addr mask;
180 +       uint16_t type;
181 +       uint16_t flags;
182 +};
183 +
184 +struct nx_addr_v6 {
185 +       struct nx_addr_v6 *next;
186 +       struct in6_addr ip;
187 +       struct in6_addr mask;
188 +       uint32_t prefix;
189 +       uint16_t type;
190 +       uint16_t flags;
191 +};
192 +
193 +struct nx_info {
194 +       struct hlist_node nx_hlist;     /* linked list of nxinfos */
195 +       nid_t nx_id;                    /* vnet id */
196 +       atomic_t nx_usecnt;             /* usage count */
197 +       atomic_t nx_tasks;              /* tasks count */
198 +       int nx_state;                   /* context state */
199 +
200 +       uint64_t nx_flags;              /* network flag word */
201 +       uint64_t nx_ncaps;              /* network capabilities */
202 +
203 +       struct in_addr v4_lback;        /* Loopback address */
204 +       struct in_addr v4_bcast;        /* Broadcast address */
205 +       struct nx_addr_v4 v4;           /* First/Single ipv4 address */
206 +#ifdef CONFIG_IPV6
207 +       struct nx_addr_v6 v6;           /* First/Single ipv6 address */
208 +#endif
209 +       char nx_name[65];               /* network context name */
210 +};
211 +
212 +
213 +/* status flags */
214 +
215 +#define NXS_HASHED      0x0001
216 +#define NXS_SHUTDOWN    0x0100
217 +#define NXS_RELEASED    0x8000
218 +
219 +extern struct nx_info *lookup_nx_info(int);
220 +
221 +extern int get_nid_list(int, unsigned int *, int);
222 +extern int nid_is_hashed(nid_t);
223 +
224 +extern int nx_migrate_task(struct task_struct *, struct nx_info *);
225 +
226 +extern long vs_net_change(struct nx_info *, unsigned int);
227 +
228 +struct sock;
229 +
230 +
231 +#define NX_IPV4(n)     ((n)->v4.type != NXA_TYPE_NONE)
232 +#ifdef  CONFIG_IPV6
233 +#define NX_IPV6(n)     ((n)->v6.type != NXA_TYPE_NONE)
234 +#else
235 +#define NX_IPV6(n)     (0)
236 +#endif
237 +
238 +#endif /* __KERNEL__ */
239 +#endif /* _VX_NETWORK_H */
240 diff -Nurb linux-2.6.22-510/include/net/netfilter/nf_conntrack.h linux-2.6.22-520/include/net/netfilter/nf_conntrack.h
241 --- linux-2.6.22-510/include/net/netfilter/nf_conntrack.h       2007-07-08 19:32:17.000000000 -0400
242 +++ linux-2.6.22-520/include/net/netfilter/nf_conntrack.h       2008-03-20 01:27:27.000000000 -0400
243 @@ -131,6 +131,9 @@
244         /* Storage reserved for other modules: */
245         union nf_conntrack_proto proto;
246  
247 +       /* PLANETLAB. VNET-specific */
248 +       xid_t xid[IP_CT_DIR_MAX];
249 +
250         /* features dynamically at the end: helper, nat (both optional) */
251         char data[0];
252  };
253 diff -Nurb linux-2.6.22-510/include/net/raw.h linux-2.6.22-520/include/net/raw.h
254 --- linux-2.6.22-510/include/net/raw.h  2007-07-08 19:32:17.000000000 -0400
255 +++ linux-2.6.22-520/include/net/raw.h  2008-03-20 01:27:27.000000000 -0400
256 @@ -36,7 +36,7 @@
257  
258  extern struct sock *__raw_v4_lookup(struct sock *sk, unsigned short num,
259                                     __be32 raddr, __be32 laddr,
260 -                                   int dif);
261 +                                   int dif, int tag);
262  
263  extern int raw_v4_input(struct sk_buff *skb, struct iphdr *iph, int hash);
264  
265 diff -Nurb linux-2.6.22-510/net/core/skbuff.c linux-2.6.22-520/net/core/skbuff.c
266 --- linux-2.6.22-510/net/core/skbuff.c  2007-07-08 19:32:17.000000000 -0400
267 +++ linux-2.6.22-520/net/core/skbuff.c  2008-03-20 01:27:27.000000000 -0400
268 @@ -56,6 +56,7 @@
269  #include <linux/rtnetlink.h>
270  #include <linux/init.h>
271  #include <linux/scatterlist.h>
272 +#include <linux/vs_network.h>
273  
274  #include <net/protocol.h>
275  #include <net/dst.h>
276 @@ -174,6 +175,7 @@
277         skb->data = data;
278         skb_reset_tail_pointer(skb);
279         skb->end = skb->tail + size;
280 +       skb->skb_tag = nx_current_nid();
281         /* make sure we initialize shinfo sequentially */
282         shinfo = skb_shinfo(skb);
283         atomic_set(&shinfo->dataref, 1);
284 @@ -443,6 +445,8 @@
285         C(tail);
286         C(end);
287  
288 +       /* Sapan: Cloned skbs aren't owned by anyone. Let the cloner decide who it belongs to. */
289 +
290         atomic_inc(&(skb_shinfo(skb)->dataref));
291         skb->cloned = 1;
292  
293 @@ -492,6 +496,7 @@
294         new->tc_index   = old->tc_index;
295  #endif
296         skb_copy_secmark(new, old);
297 +       new->skb_tag = old->skb_tag;
298         atomic_set(&new->users, 1);
299         skb_shinfo(new)->gso_size = skb_shinfo(old)->gso_size;
300         skb_shinfo(new)->gso_segs = skb_shinfo(old)->gso_segs;
301 diff -Nurb linux-2.6.22-510/net/core/sock.c linux-2.6.22-520/net/core/sock.c
302 --- linux-2.6.22-510/net/core/sock.c    2008-03-20 01:27:22.000000000 -0400
303 +++ linux-2.6.22-520/net/core/sock.c    2008-03-20 01:27:27.000000000 -0400
304 @@ -444,6 +444,19 @@
305                 }
306                 goto set_sndbuf;
307  
308 +       case SO_SETXID:
309 +               if (current_vx_info()) {
310 +                       ret = -EPERM;
311 +                       break;
312 +               }
313 +               if (val < 0 || val > MAX_S_CONTEXT) {
314 +                       ret = -EINVAL;
315 +                       break;
316 +               }
317 +               sk->sk_xid = val;
318 +               sk->sk_nid = val;
319 +               break;
320 +
321         case SO_RCVBUF:
322                 /* Don't error on this BSD doesn't and if you think
323                    about it this is right. Otherwise apps have to
324 @@ -573,7 +586,7 @@
325                 char devname[IFNAMSIZ];
326  
327                 /* Sorry... */
328 -               if (!capable(CAP_NET_RAW)) {
329 +               if (!nx_capable(CAP_NET_RAW, NXC_RAW_SOCKET)) {
330                         ret = -EPERM;
331                         break;
332                 }
333 diff -Nurb linux-2.6.22-510/net/ipv4/af_inet.c linux-2.6.22-520/net/ipv4/af_inet.c
334 --- linux-2.6.22-510/net/ipv4/af_inet.c 2008-03-20 01:27:22.000000000 -0400
335 +++ linux-2.6.22-520/net/ipv4/af_inet.c 2008-03-20 01:27:27.000000000 -0400
336 @@ -178,6 +178,8 @@
337                         return -EAGAIN;
338                 }
339                 inet->sport = htons(inet->num);
340 +               sk->sk_xid = vx_current_xid();
341 +               sk->sk_nid = nx_current_nid();
342         }
343         release_sock(sk);
344         return 0;
345 @@ -312,6 +314,9 @@
346         if ((protocol == IPPROTO_ICMP) &&
347                 nx_capable(answer->capability, NXC_RAW_ICMP))
348                 goto override;
349 +       if (sock->type == SOCK_RAW &&
350 +               nx_capable(answer->capability, NXC_RAW_SOCKET))
351 +               goto override;
352         if (answer->capability > 0 && !capable(answer->capability))
353                 goto out_rcu_unlock;
354  override:
355 diff -Nurb linux-2.6.22-510/net/ipv4/icmp.c linux-2.6.22-520/net/ipv4/icmp.c
356 --- linux-2.6.22-510/net/ipv4/icmp.c    2008-03-20 01:27:26.000000000 -0400
357 +++ linux-2.6.22-520/net/ipv4/icmp.c    2008-03-20 01:27:27.000000000 -0400
358 @@ -709,7 +709,7 @@
359         if ((raw_sk = sk_head(&raw_v4_htable[hash])) != NULL) {
360                 while ((raw_sk = __raw_v4_lookup(raw_sk, protocol, iph->daddr,
361                                                  iph->saddr,
362 -                                                skb->dev->ifindex)) != NULL) {
363 +                                                skb->dev->ifindex, skb->skb_tag)) != NULL) {
364                         raw_err(raw_sk, skb, info);
365                         raw_sk = sk_next(raw_sk);
366                         iph = (struct iphdr *)skb->data;
367 diff -Nurb linux-2.6.22-510/net/ipv4/ip_options.c linux-2.6.22-520/net/ipv4/ip_options.c
368 --- linux-2.6.22-510/net/ipv4/ip_options.c      2007-07-08 19:32:17.000000000 -0400
369 +++ linux-2.6.22-520/net/ipv4/ip_options.c      2008-03-20 01:27:27.000000000 -0400
370 @@ -409,7 +409,7 @@
371                                         optptr[2] += 8;
372                                         break;
373                                       default:
374 -                                       if (!skb && !capable(CAP_NET_RAW)) {
375 +                                       if (!skb && !nx_capable(CAP_NET_RAW, NXC_RAW_SOCKET)) {
376                                                 pp_ptr = optptr + 3;
377                                                 goto error;
378                                         }
379 @@ -445,7 +445,7 @@
380                                 opt->router_alert = optptr - iph;
381                         break;
382                       case IPOPT_CIPSO:
383 -                       if ((!skb && !capable(CAP_NET_RAW)) || opt->cipso) {
384 +                       if ((!skb && !nx_capable(CAP_NET_RAW, NXC_RAW_SOCKET)) || opt->cipso) {
385                                 pp_ptr = optptr;
386                                 goto error;
387                         }
388 @@ -458,7 +458,7 @@
389                       case IPOPT_SEC:
390                       case IPOPT_SID:
391                       default:
392 -                       if (!skb && !capable(CAP_NET_RAW)) {
393 +                       if (!skb && !nx_capable(CAP_NET_RAW, NXC_RAW_SOCKET)) {
394                                 pp_ptr = optptr;
395                                 goto error;
396                         }
397 diff -Nurb linux-2.6.22-510/net/ipv4/netfilter/ipt_LOG.c linux-2.6.22-520/net/ipv4/netfilter/ipt_LOG.c
398 --- linux-2.6.22-510/net/ipv4/netfilter/ipt_LOG.c       2008-03-20 01:27:10.000000000 -0400
399 +++ linux-2.6.22-520/net/ipv4/netfilter/ipt_LOG.c       2008-03-20 01:27:27.000000000 -0400
400 @@ -49,6 +49,8 @@
401         else
402                 logflags = NF_LOG_MASK;
403  
404 +       printk("TAG=%d ", skb->skb_tag);
405 +
406         ih = skb_header_pointer(skb, iphoff, sizeof(_iph), &_iph);
407         if (ih == NULL) {
408                 printk("TRUNCATED");
409 diff -Nurb linux-2.6.22-510/net/ipv4/raw.c linux-2.6.22-520/net/ipv4/raw.c
410 --- linux-2.6.22-510/net/ipv4/raw.c     2008-03-20 01:27:22.000000000 -0400
411 +++ linux-2.6.22-520/net/ipv4/raw.c     2008-03-20 01:27:27.000000000 -0400
412 @@ -103,7 +103,7 @@
413  
414  struct sock *__raw_v4_lookup(struct sock *sk, unsigned short num,
415                              __be32 raddr, __be32 laddr,
416 -                            int dif)
417 +                            int dif, int tag)
418  {
419         struct hlist_node *node;
420  
421 @@ -112,6 +112,7 @@
422  
423                 if (inet->num == num                                    &&
424                     !(inet->daddr && inet->daddr != raddr)              &&
425 +                   (!sk->sk_nx_info || tag == 1 || sk->sk_nid == tag)  &&
426                     v4_sock_addr_match(sk->sk_nx_info, inet, laddr)     &&
427                     !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
428                         goto found; /* gotcha */
429 @@ -161,7 +162,7 @@
430                 goto out;
431         sk = __raw_v4_lookup(__sk_head(head), iph->protocol,
432                              iph->saddr, iph->daddr,
433 -                            skb->dev->ifindex);
434 +                            skb->dev->ifindex, skb->skb_tag);
435  
436         while (sk) {
437                 delivered = 1;
438 @@ -174,7 +175,7 @@
439                 }
440                 sk = __raw_v4_lookup(sk_next(sk), iph->protocol,
441                                      iph->saddr, iph->daddr,
442 -                                    skb->dev->ifindex);
443 +                                    skb->dev->ifindex, skb->skb_tag);
444         }
445  out:
446         read_unlock(&raw_v4_lock);
447 @@ -315,7 +316,7 @@
448         }
449  
450         err = -EPERM;
451 -       if (!nx_check(0, VS_ADMIN) && !capable(CAP_NET_RAW) &&
452 +       if (!nx_check(0, VS_ADMIN) && !nx_capable(CAP_NET_RAW, NXC_RAW_SOCKET) &&
453                 sk->sk_nx_info &&
454                 !v4_addr_in_nx_info(sk->sk_nx_info, iph->saddr, NXA_MASK_BIND))
455                 goto error_free;
456 diff -Nurb linux-2.6.22-510/net/netfilter/Kconfig linux-2.6.22-520/net/netfilter/Kconfig
457 --- linux-2.6.22-510/net/netfilter/Kconfig      2007-07-08 19:32:17.000000000 -0400
458 +++ linux-2.6.22-520/net/netfilter/Kconfig      2008-03-20 01:27:27.000000000 -0400
459 @@ -389,6 +389,13 @@
460  
461           To compile it as a module, choose M here.  If unsure, say N.
462  
463 +config NETFILTER_XT_TARGET_SETXID
464 +       tristate '"SETXID" target support'
465 +       depends on NETFILTER_XTABLES
466 +       help
467 +         This option adds a `SETXID' target, which allows you to alter the
468 +         xid of a socket.
469 +
470  config NETFILTER_XT_MATCH_COMMENT
471         tristate  '"comment" match support'
472         depends on NETFILTER_XTABLES
473 diff -Nurb linux-2.6.22-510/net/netfilter/Makefile linux-2.6.22-520/net/netfilter/Makefile
474 --- linux-2.6.22-510/net/netfilter/Makefile     2007-07-08 19:32:17.000000000 -0400
475 +++ linux-2.6.22-520/net/netfilter/Makefile     2008-03-20 01:27:27.000000000 -0400
476 @@ -37,6 +37,7 @@
477  obj-$(CONFIG_NETFILTER_XTABLES) += x_tables.o xt_tcpudp.o
478  
479  # targets
480 +obj-$(CONFIG_NETFILTER_XT_TARGET_SETXID) += xt_SETXID.o
481  obj-$(CONFIG_NETFILTER_XT_TARGET_CLASSIFY) += xt_CLASSIFY.o
482  obj-$(CONFIG_NETFILTER_XT_TARGET_CONNMARK) += xt_CONNMARK.o
483  obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
484 diff -Nurb linux-2.6.22-510/net/netfilter/nf_conntrack_core.c linux-2.6.22-520/net/netfilter/nf_conntrack_core.c
485 --- linux-2.6.22-510/net/netfilter/nf_conntrack_core.c  2007-07-08 19:32:17.000000000 -0400
486 +++ linux-2.6.22-520/net/netfilter/nf_conntrack_core.c  2008-03-20 01:27:27.000000000 -0400
487 @@ -726,6 +726,8 @@
488  
489         /* Overload tuple linked list to put us in unconfirmed list. */
490         list_add(&conntrack->tuplehash[IP_CT_DIR_ORIGINAL].list, &unconfirmed);
491 +       conntrack->xid[IP_CT_DIR_ORIGINAL] = -1;
492 +       conntrack->xid[IP_CT_DIR_REPLY] = -1;
493  
494         write_unlock_bh(&nf_conntrack_lock);
495  
496 diff -Nurb linux-2.6.22-510/net/netfilter/xt_MARK.c linux-2.6.22-520/net/netfilter/xt_MARK.c
497 --- linux-2.6.22-510/net/netfilter/xt_MARK.c    2007-07-08 19:32:17.000000000 -0400
498 +++ linux-2.6.22-520/net/netfilter/xt_MARK.c    2008-03-20 01:30:14.000000000 -0400
499 @@ -5,13 +5,18 @@
500   * This program is free software; you can redistribute it and/or modify
501   * it under the terms of the GNU General Public License version 2 as
502   * published by the Free Software Foundation.
503 + *
504   */
505  
506  #include <linux/module.h>
507 +#include <linux/version.h>
508  #include <linux/skbuff.h>
509  #include <linux/ip.h>
510  #include <net/checksum.h>
511 +#include <net/route.h>
512 +#include <net/inet_hashtables.h>
513  
514 +#include <net/netfilter/nf_conntrack.h>
515  #include <linux/netfilter/x_tables.h>
516  #include <linux/netfilter/xt_MARK.h>
517  
518 @@ -21,6 +26,48 @@
519  MODULE_ALIAS("ipt_MARK");
520  MODULE_ALIAS("ip6t_MARK");
521  
522 +static inline u_int16_t
523 +get_dst_port(struct nf_conntrack_tuple *tuple)
524 +{
525 +       switch (tuple->dst.protonum) {
526 +       case IPPROTO_GRE:
527 +               /* XXX Truncate 32-bit GRE key to 16 bits */
528 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
529 +               return tuple->dst.u.gre.key;
530 +#else
531 +               return htons(ntohl(tuple->dst.u.gre.key));
532 +#endif  
533 +       case IPPROTO_ICMP:
534 +               /* Bind on ICMP echo ID */
535 +               return tuple->src.u.icmp.id;
536 +       case IPPROTO_TCP:
537 +               return tuple->dst.u.tcp.port;
538 +       case IPPROTO_UDP:
539 +               return tuple->dst.u.udp.port;
540 +       default:
541 +               return tuple->dst.u.all;
542 +       }
543 +}
544 +
545 +static inline u_int16_t
546 +get_src_port(struct nf_conntrack_tuple *tuple)
547 +{
548 +       switch (tuple->dst.protonum) {
549 +       case IPPROTO_GRE:
550 +               /* XXX Truncate 32-bit GRE key to 16 bits */
551 +               return htons(ntohl(tuple->src.u.gre.key));
552 +       case IPPROTO_ICMP:
553 +               /* Bind on ICMP echo ID */
554 +               return tuple->src.u.icmp.id;
555 +       case IPPROTO_TCP:
556 +               return tuple->src.u.tcp.port;
557 +       case IPPROTO_UDP:
558 +               return tuple->src.u.udp.port;
559 +       default:
560 +               return tuple->src.u.all;
561 +       }
562 +}
563 +
564  static unsigned int
565  target_v0(struct sk_buff **pskb,
566           const struct net_device *in,
567 @@ -44,7 +91,7 @@
568           const void *targinfo)
569  {
570         const struct xt_mark_target_info_v1 *markinfo = targinfo;
571 -       int mark = 0;
572 +       int mark = -1;
573  
574         switch (markinfo->mode) {
575         case XT_MARK_SET:
576 @@ -58,8 +105,61 @@
577         case XT_MARK_OR:
578                 mark = (*pskb)->mark | markinfo->mark;
579                 break;
580 +
581 +       case XT_MARK_COPYXID: {
582 +               enum ip_conntrack_info ctinfo;
583 +               struct sock *connection_sk;
584 +               int dif;
585 +
586 +               struct nf_conn *ct = nf_ct_get((*pskb), &ctinfo);
587 +               extern struct inet_hashinfo tcp_hashinfo;
588 +               enum ip_conntrack_dir dir;
589 +               if (!ct) 
590 +                       break;
591 +
592 +               dir = CTINFO2DIR(ctinfo);
593 +               u_int32_t src_ip = ct->tuplehash[dir].tuple.src.u3.ip;
594 +               u_int16_t src_port = get_src_port(&ct->tuplehash[dir].tuple);
595 +               u_int16_t proto = ct->tuplehash[dir].tuple.dst.protonum;
596 +
597 +               u_int32_t ip;
598 +               u_int16_t port;
599 +
600 +               dif = ((struct rtable *)(*pskb)->dst)->rt_iif;
601 +               ip = ct->tuplehash[dir].tuple.dst.u3.ip;
602 +               port = get_dst_port(&ct->tuplehash[dir].tuple);
603 +
604 +               if (proto == 1 || proto == 17) {
605 +                       if (((*pskb)->mark!=-1) && (*pskb)->mark)
606 +                               ct->xid[0]=(*pskb)->mark;
607 +                       if (ct->xid[0]) 
608 +                               mark = ct->xid[0];
609 +
610 +               }
611 +               else if (proto == 6) { 
612 +                       /*
613 +                               if ((*pskb)->sk) 
614 +                                       connection_sk = (*pskb)->sk;
615 +                               else {
616 +                                       connection_sk = inet_lookup(&tcp_hashinfo, src_ip, src_port, ip, port, dif);
617 +                               }
618 +                               */
619 +                       connection_sk = (*pskb)->sk;
620 +
621 +                               if (connection_sk) {
622 +                                       connection_sk->sk_peercred.gid = connection_sk->sk_peercred.uid = ct->xid[dir];
623 +                                       ct->xid[!dir]=connection_sk->sk_xid;
624 +                                       if (connection_sk->sk_xid != 0) 
625 +                                               mark = connection_sk->sk_xid;
626 +                                       if (connection_sk != (*pskb)->sk)
627 +                                               sock_put(connection_sk);
628 +                               }
629 +                               break;
630 +                               }
631 +                             }
632         }
633  
634 +       if (mark != -1)
635         (*pskb)->mark = mark;
636         return XT_CONTINUE;
637  }
638 @@ -92,7 +192,8 @@
639  
640         if (markinfo->mode != XT_MARK_SET
641             && markinfo->mode != XT_MARK_AND
642 -           && markinfo->mode != XT_MARK_OR) {
643 +           && markinfo->mode != XT_MARK_OR
644 +           && markinfo->mode != XT_MARK_COPYXID) {
645                 printk(KERN_WARNING "MARK: unknown mode %u\n",
646                        markinfo->mode);
647                 return 0;
648 diff -Nurb linux-2.6.22-510/net/netfilter/xt_MARK.c.orig linux-2.6.22-520/net/netfilter/xt_MARK.c.orig
649 --- linux-2.6.22-510/net/netfilter/xt_MARK.c.orig       1969-12-31 19:00:00.000000000 -0500
650 +++ linux-2.6.22-520/net/netfilter/xt_MARK.c.orig       2008-03-20 01:27:27.000000000 -0400
651 @@ -0,0 +1,284 @@
652 +/* This is a module which is used for setting the NFMARK field of an skb. */
653 +
654 +/* (C) 1999-2001 Marc Boucher <marc@mbsi.ca>
655 + *
656 + * This program is free software; you can redistribute it and/or modify
657 + * it under the terms of the GNU General Public License version 2 as
658 + * published by the Free Software Foundation.
659 + *
660 + */
661 +
662 +#include <linux/module.h>
663 +#include <linux/version.h>
664 +#include <linux/skbuff.h>
665 +#include <linux/ip.h>
666 +#include <net/checksum.h>
667 +#include <net/route.h>
668 +#include <net/inet_hashtables.h>
669 +
670 +#include <net/netfilter/nf_conntrack.h>
671 +#include <linux/netfilter/x_tables.h>
672 +#include <linux/netfilter/xt_MARK.h>
673 +
674 +MODULE_LICENSE("GPL");
675 +MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
676 +MODULE_DESCRIPTION("ip[6]tables MARK modification module");
677 +MODULE_ALIAS("ipt_MARK");
678 +MODULE_ALIAS("ip6t_MARK");
679 +
680 +static inline u_int16_t
681 +get_dst_port(struct nf_conntrack_tuple *tuple)
682 +{
683 +       switch (tuple->dst.protonum) {
684 +       case IPPROTO_GRE:
685 +               /* XXX Truncate 32-bit GRE key to 16 bits */
686 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
687 +               return tuple->dst.u.gre.key;
688 +#else
689 +               return htons(ntohl(tuple->dst.u.gre.key));
690 +#endif  
691 +       case IPPROTO_ICMP:
692 +               /* Bind on ICMP echo ID */
693 +               return tuple->src.u.icmp.id;
694 +       case IPPROTO_TCP:
695 +               return tuple->dst.u.tcp.port;
696 +       case IPPROTO_UDP:
697 +               return tuple->dst.u.udp.port;
698 +       default:
699 +               return tuple->dst.u.all;
700 +       }
701 +}
702 +
703 +static inline u_int16_t
704 +get_src_port(struct nf_conntrack_tuple *tuple)
705 +{
706 +       switch (tuple->dst.protonum) {
707 +       case IPPROTO_GRE:
708 +               /* XXX Truncate 32-bit GRE key to 16 bits */
709 +               return htons(ntohl(tuple->src.u.gre.key));
710 +       case IPPROTO_ICMP:
711 +               /* Bind on ICMP echo ID */
712 +               return tuple->src.u.icmp.id;
713 +       case IPPROTO_TCP:
714 +               return tuple->src.u.tcp.port;
715 +       case IPPROTO_UDP:
716 +               return tuple->src.u.udp.port;
717 +       default:
718 +               return tuple->src.u.all;
719 +       }
720 +}
721 +
722 +static unsigned int
723 +target_v0(struct sk_buff **pskb,
724 +         const struct net_device *in,
725 +         const struct net_device *out,
726 +         unsigned int hooknum,
727 +         const struct xt_target *target,
728 +         const void *targinfo)
729 +{
730 +       const struct xt_mark_target_info *markinfo = targinfo;
731 +
732 +       (*pskb)->mark = markinfo->mark;
733 +       return XT_CONTINUE;
734 +}
735 +
736 +static unsigned int
737 +target_v1(struct sk_buff **pskb,
738 +         const struct net_device *in,
739 +         const struct net_device *out,
740 +         unsigned int hooknum,
741 +         const struct xt_target *target,
742 +         const void *targinfo)
743 +{
744 +       const struct xt_mark_target_info_v1 *markinfo = targinfo;
745 +       int mark = -1;
746 +
747 +       switch (markinfo->mode) {
748 +       case XT_MARK_SET:
749 +               mark = markinfo->mark;
750 +               break;
751 +
752 +       case XT_MARK_AND:
753 +               mark = (*pskb)->mark & markinfo->mark;
754 +               break;
755 +
756 +       case XT_MARK_OR:
757 +               mark = (*pskb)->mark | markinfo->mark;
758 +               break;
759 +
760 +       case XT_MARK_COPYXID: {
761 +               enum ip_conntrack_info ctinfo;
762 +               struct sock *connection_sk=NULL;
763 +               int dif;
764 +
765 +               struct nf_conn *ct = nf_ct_get((*pskb), &ctinfo);
766 +               extern struct inet_hashinfo tcp_hashinfo;
767 +               enum ip_conntrack_dir dir;
768 +               if (!ct) 
769 +                       break;
770 +
771 +               dir = CTINFO2DIR(ctinfo);
772 +               u_int32_t src_ip = ct->tuplehash[dir].tuple.src.u3.ip;
773 +               u_int16_t src_port = get_src_port(&ct->tuplehash[dir].tuple);
774 +               u_int16_t proto = ct->tuplehash[dir].tuple.dst.protonum;
775 +
776 +               u_int32_t ip;
777 +               u_int16_t port;
778 +
779 +               dif = ((struct rtable *)(*pskb)->dst)->rt_iif;
780 +               ip = ct->tuplehash[dir].tuple.dst.u3.ip;
781 +               port = get_dst_port(&ct->tuplehash[dir].tuple);
782 +
783 +               if (proto == 1) {
784 +                       if (((*pskb)->mark!=-1) && (*pskb)->mark)
785 +                               ct->xid[0]=(*pskb)->mark;
786 +                       if (ct->xid[0]) 
787 +                               mark = ct->xid[0];
788 +                       printk(KERN_CRIT "%d %d\n",ct->xid[0],(*pskb)->mark);
789 +
790 +               }
791 +               else if (proto == 6) {
792 +                               if ((*pskb)->sk) 
793 +                                       connection_sk = (*pskb)->sk;
794 +                               else {
795 +                                       connection_sk = inet_lookup(&tcp_hashinfo, src_ip, src_port, ip, port, dif);
796 +                               }
797 +
798 +                               if (connection_sk) {
799 +                                       connection_sk->sk_peercred.gid = connection_sk->sk_peercred.uid = ct->xid[dir];
800 +                                       ct->xid[!dir]=connection_sk->sk_xid;
801 +                                       if (connection_sk->sk_xid != 0) 
802 +                                               mark = connection_sk->sk_xid;
803 +                                       if (connection_sk != (*pskb)->sk)
804 +                                               sock_put(connection_sk);
805 +                               }
806 +                               break;
807 +                               }
808 +                             }
809 +       }
810 +
811 +       if (mark != -1)
812 +       (*pskb)->mark = mark;
813 +       return XT_CONTINUE;
814 +}
815 +
816 +
817 +static int
818 +checkentry_v0(const char *tablename,
819 +             const void *entry,
820 +             const struct xt_target *target,
821 +             void *targinfo,
822 +             unsigned int hook_mask)
823 +{
824 +       struct xt_mark_target_info *markinfo = targinfo;
825 +
826 +       if (markinfo->mark > 0xffffffff) {
827 +               printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
828 +               return 0;
829 +       }
830 +       return 1;
831 +}
832 +
833 +static int
834 +checkentry_v1(const char *tablename,
835 +             const void *entry,
836 +             const struct xt_target *target,
837 +             void *targinfo,
838 +             unsigned int hook_mask)
839 +{
840 +       struct xt_mark_target_info_v1 *markinfo = targinfo;
841 +
842 +       if (markinfo->mode != XT_MARK_SET
843 +           && markinfo->mode != XT_MARK_AND
844 +           && markinfo->mode != XT_MARK_OR
845 +           && markinfo->mode != XT_MARK_COPYXID) {
846 +               printk(KERN_WARNING "MARK: unknown mode %u\n",
847 +                      markinfo->mode);
848 +               return 0;
849 +       }
850 +       if (markinfo->mark > 0xffffffff) {
851 +               printk(KERN_WARNING "MARK: Only supports 32bit wide mark\n");
852 +               return 0;
853 +       }
854 +       return 1;
855 +}
856 +
857 +#ifdef CONFIG_COMPAT
858 +struct compat_xt_mark_target_info_v1 {
859 +       compat_ulong_t  mark;
860 +       u_int8_t        mode;
861 +       u_int8_t        __pad1;
862 +       u_int16_t       __pad2;
863 +};
864 +
865 +static void compat_from_user_v1(void *dst, void *src)
866 +{
867 +       struct compat_xt_mark_target_info_v1 *cm = src;
868 +       struct xt_mark_target_info_v1 m = {
869 +               .mark   = cm->mark,
870 +               .mode   = cm->mode,
871 +       };
872 +       memcpy(dst, &m, sizeof(m));
873 +}
874 +
875 +static int compat_to_user_v1(void __user *dst, void *src)
876 +{
877 +       struct xt_mark_target_info_v1 *m = src;
878 +       struct compat_xt_mark_target_info_v1 cm = {
879 +               .mark   = m->mark,
880 +               .mode   = m->mode,
881 +       };
882 +       return copy_to_user(dst, &cm, sizeof(cm)) ? -EFAULT : 0;
883 +}
884 +#endif /* CONFIG_COMPAT */
885 +
886 +static struct xt_target xt_mark_target[] = {
887 +       {
888 +               .name           = "MARK",
889 +               .family         = AF_INET,
890 +               .revision       = 0,
891 +               .checkentry     = checkentry_v0,
892 +               .target         = target_v0,
893 +               .targetsize     = sizeof(struct xt_mark_target_info),
894 +               .table          = "mangle",
895 +               .me             = THIS_MODULE,
896 +       },
897 +       {
898 +               .name           = "MARK",
899 +               .family         = AF_INET,
900 +               .revision       = 1,
901 +               .checkentry     = checkentry_v1,
902 +               .target         = target_v1,
903 +               .targetsize     = sizeof(struct xt_mark_target_info_v1),
904 +#ifdef CONFIG_COMPAT
905 +               .compatsize     = sizeof(struct compat_xt_mark_target_info_v1),
906 +               .compat_from_user = compat_from_user_v1,
907 +               .compat_to_user = compat_to_user_v1,
908 +#endif
909 +               .table          = "mangle",
910 +               .me             = THIS_MODULE,
911 +       },
912 +       {
913 +               .name           = "MARK",
914 +               .family         = AF_INET6,
915 +               .revision       = 0,
916 +               .checkentry     = checkentry_v0,
917 +               .target         = target_v0,
918 +               .targetsize     = sizeof(struct xt_mark_target_info),
919 +               .table          = "mangle",
920 +               .me             = THIS_MODULE,
921 +       },
922 +};
923 +
924 +static int __init xt_mark_init(void)
925 +{
926 +       return xt_register_targets(xt_mark_target, ARRAY_SIZE(xt_mark_target));
927 +}
928 +
929 +static void __exit xt_mark_fini(void)
930 +{
931 +       xt_unregister_targets(xt_mark_target, ARRAY_SIZE(xt_mark_target));
932 +}
933 +
934 +module_init(xt_mark_init);
935 +module_exit(xt_mark_fini);
936 diff -Nurb linux-2.6.22-510/net/netfilter/xt_SETXID.c linux-2.6.22-520/net/netfilter/xt_SETXID.c
937 --- linux-2.6.22-510/net/netfilter/xt_SETXID.c  1969-12-31 19:00:00.000000000 -0500
938 +++ linux-2.6.22-520/net/netfilter/xt_SETXID.c  2008-03-20 01:27:27.000000000 -0400
939 @@ -0,0 +1,79 @@
940 +#include <linux/module.h>
941 +#include <linux/skbuff.h>
942 +#include <linux/ip.h>
943 +#include <net/checksum.h>
944 +#include <linux/vs_network.h>
945 +
946 +#include <linux/netfilter/x_tables.h>
947 +#include <linux/netfilter/xt_SETXID.h>
948 +
949 +MODULE_LICENSE("GPL");
950 +MODULE_AUTHOR("");
951 +MODULE_DESCRIPTION("");
952 +MODULE_ALIAS("ipt_SETXID");
953 +
954 +static unsigned int
955 +target_v1(struct sk_buff **pskb,
956 +         const struct net_device *in,
957 +         const struct net_device *out,
958 +         unsigned int hooknum,
959 +         const struct xt_target *target,
960 +         const void *targinfo)
961 +{
962 +       const struct xt_setxid_target_info_v1 *setxidinfo = targinfo;
963 +
964 +       switch (setxidinfo->mode) {
965 +       case XT_SET_PACKET_XID:
966 +                (*pskb)->skb_tag = setxidinfo->mark;
967 +               break;
968 +       }
969 +       return XT_CONTINUE;
970 +}
971 +
972 +
973 +static int
974 +checkentry_v1(const char *tablename,
975 +             const void *entry,
976 +             const struct xt_target *target,
977 +             void *targinfo,
978 +             unsigned int hook_mask)
979 +{
980 +       struct xt_setxid_target_info_v1 *setxidinfo = targinfo;
981 +
982 +       if (setxidinfo->mode != XT_SET_PACKET_XID) {
983 +               printk(KERN_WARNING "SETXID: unknown mode %u\n",
984 +                      setxidinfo->mode);
985 +               return 0;
986 +       }
987 +
988 +       return 1;
989 +}
990 +
991 +static struct xt_target xt_setxid_target[] = {
992 +       {
993 +               .name           = "SETXID",
994 +               .family         = AF_INET,
995 +               .revision       = 1,
996 +               .checkentry     = checkentry_v1,
997 +               .target         = target_v1,
998 +               .targetsize     = sizeof(struct xt_setxid_target_info_v1),
999 +               .table          = "mangle",
1000 +               .me             = THIS_MODULE,
1001 +       }
1002 +};
1003 +
1004 +static int __init init(void)
1005 +{
1006 +       int err;
1007 +
1008 +       err = xt_register_targets(xt_setxid_target, ARRAY_SIZE(xt_setxid_target));
1009 +       return err;
1010 +}
1011 +
1012 +static void __exit fini(void)
1013 +{
1014 +       xt_unregister_targets(xt_setxid_target, ARRAY_SIZE(xt_setxid_target));
1015 +}
1016 +
1017 +module_init(init);
1018 +module_exit(fini);
1019 diff -Nurb linux-2.6.22-510/net/packet/af_packet.c linux-2.6.22-520/net/packet/af_packet.c
1020 --- linux-2.6.22-510/net/packet/af_packet.c     2007-07-08 19:32:17.000000000 -0400
1021 +++ linux-2.6.22-520/net/packet/af_packet.c     2008-03-20 01:27:27.000000000 -0400
1022 @@ -78,6 +78,7 @@
1023  #include <linux/poll.h>
1024  #include <linux/module.h>
1025  #include <linux/init.h>
1026 +#include <linux/vs_network.h>
1027  
1028  #ifdef CONFIG_INET
1029  #include <net/inet_common.h>
1030 @@ -324,6 +325,9 @@
1031         __be16 proto=0;
1032         int err;
1033  
1034 +       if (!nx_capable(CAP_NET_RAW, NXC_RAW_SEND))
1035 +               return -EPERM;
1036 +
1037         /*
1038          *      Get and verify the address.
1039          */
1040 @@ -420,6 +424,10 @@
1041                                       unsigned int res)
1042  {
1043         struct sk_filter *filter;
1044 +       int tag = skb->skb_tag;
1045 +
1046 +       if (sk->sk_nx_info && !(tag == 1 || sk->sk_nid == tag))
1047 +               return 0;
1048  
1049         rcu_read_lock_bh();
1050         filter = rcu_dereference(sk->sk_filter);
1051 @@ -711,6 +719,9 @@
1052         unsigned char *addr;
1053         int ifindex, err, reserve = 0;
1054  
1055 +       if (!nx_capable(CAP_NET_RAW, NXC_RAW_SEND))
1056 +               return -EPERM;
1057 +
1058         /*
1059          *      Get and verify the address.
1060          */
1061 @@ -984,7 +995,7 @@
1062         __be16 proto = (__force __be16)protocol; /* weird, but documented */
1063         int err;
1064  
1065 -       if (!capable(CAP_NET_RAW))
1066 +       if (!nx_capable(CAP_NET_RAW, NXC_RAW_SOCKET))
1067                 return -EPERM;
1068         if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW &&
1069             sock->type != SOCK_PACKET)