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-28 16:36:24.000000000 -0400
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-28 16:36:24.000000000 -0400
16 +#ifndef _XT_SETXID_H_target
17 +#define _XT_SETXID_H_target
24 +struct xt_setxid_target_info_v1 {
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-28 16:36:24.000000000 -0400
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
39 #define ipt_mark_target_info_v1 xt_mark_target_info_v1
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-28 16:36:24.000000000 -0400
45 +#ifndef _IPT_SETXID_H_target
46 +#define _IPT_SETXID_H_target
48 +/* Backwards compatibility for old userspace */
50 +#include <linux/netfilter/xt_SETXID.h>
53 +#define IPT_SET_PACKET_XID XT_SET_PACKET_XID
55 +#define ipt_setxid_target_info_v1 xt_setxid_target_info_v1
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-28 16:36:24.000000000 -0400
62 /* Storage reserved for other modules: */
63 union nf_conntrack_proto proto;
65 + /* PLANETLAB. VNET-specific */
66 + int xid[IP_CT_DIR_MAX];
68 /* features dynamically at the end: helper, nat (both optional) */
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-28 16:36:24.000000000 -0400
76 To compile it as a module, choose M here. If unsure, say N.
78 +config NETFILTER_XT_TARGET_SETXID
79 + tristate '"SETXID" target support'
80 + depends on NETFILTER_XTABLES
82 + This option adds a `SETXID' target, which allows you to alter the
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-28 16:36:24.000000000 -0400
92 obj-$(CONFIG_NETFILTER_XTABLES) += x_tables.o xt_tcpudp.o
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-28 16:36:24.000000000 -0400
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;
109 write_unlock_bh(&nf_conntrack_lock);
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-08-04 16:44:16.000000000 -0400
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.
121 #include <linux/module.h>
122 +#include <linux/version.h>
123 #include <linux/skbuff.h>
124 #include <linux/ip.h>
125 +#include <net/udp.h>
126 #include <net/checksum.h>
127 +#include <net/route.h>
128 +#include <net/inet_hashtables.h>
130 +#include <net/netfilter/nf_conntrack.h>
131 #include <linux/netfilter/x_tables.h>
132 #include <linux/netfilter/xt_MARK.h>
135 MODULE_ALIAS("ipt_MARK");
136 MODULE_ALIAS("ip6t_MARK");
138 +static inline u_int16_t
139 +get_dst_port(struct nf_conntrack_tuple *tuple)
141 + switch (tuple->dst.protonum) {
143 + /* XXX Truncate 32-bit GRE key to 16 bits */
144 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11)
145 + return tuple->dst.u.gre.key;
147 + return htons(ntohl(tuple->dst.u.gre.key));
150 + /* Bind on ICMP echo ID */
151 + return tuple->src.u.icmp.id;
153 + return tuple->dst.u.tcp.port;
155 + return tuple->dst.u.udp.port;
157 + return tuple->dst.u.all;
161 +static inline u_int16_t
162 +get_src_port(struct nf_conntrack_tuple *tuple)
164 + switch (tuple->dst.protonum) {
166 + /* XXX Truncate 32-bit GRE key to 16 bits */
167 + return htons(ntohl(tuple->src.u.gre.key));
169 + /* Bind on ICMP echo ID */
170 + return tuple->src.u.icmp.id;
172 + return tuple->src.u.tcp.port;
174 + return tuple->src.u.udp.port;
176 + return tuple->src.u.all;
181 target_v0(struct sk_buff **pskb,
182 const struct net_device *in,
187 +extern DEFINE_PER_CPU(int, sknid_elevator);
188 +static struct sock *__udp4_lib_lookup(__be32 saddr, __be16 sport,
189 + __be32 daddr, __be16 dport,
190 + int dif, struct hlist_head udptable[])
192 + struct sock *sk, *result = NULL;
193 + struct hlist_node *node;
194 + unsigned short hnum = ntohs(dport);
197 + read_lock(&udp_hash_lock);
199 + sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) {
200 + struct inet_sock *inet = inet_sk(sk);
202 + if (sk->sk_hash == hnum && !ipv6_only_sock(sk)) {
203 + int score = (sk->sk_family == PF_INET ? 1 : 0);
205 + if (inet->rcv_saddr) {
206 + if (inet->rcv_saddr != daddr)
210 + /* block non nx_info ips */
211 + if (!v4_addr_in_nx_info(sk->sk_nx_info,
212 + daddr, NXA_MASK_BIND))
216 + if (inet->daddr != saddr)
221 + if (inet->dport != sport)
225 + if (sk->sk_bound_dev_if) {
226 + if (sk->sk_bound_dev_if != dif)
233 + } else if (score > badness) {
242 + read_unlock(&udp_hash_lock);
246 +#define related(ct) (ct==(IP_CT_IS_REPLY + IP_CT_RELATED))
249 target_v1(struct sk_buff **pskb,
250 const struct net_device *in,
252 const void *targinfo)
254 const struct xt_mark_target_info_v1 *markinfo = targinfo;
256 + enum ip_conntrack_info ctinfo;
257 + struct sock *connection_sk;
259 + struct nf_conn *ct;
260 + extern struct inet_hashinfo tcp_hashinfo;
261 + enum ip_conntrack_dir dir;
265 + u_int16_t proto, src_port;
271 switch (markinfo->mode) {
273 @@ -58,13 +180,121 @@
275 mark = (*pskb)->mark | markinfo->mark;
278 + case XT_MARK_COPYXID:
279 + dif = ((struct rtable *)(*pskb)->dst)->rt_iif;
281 + ct = nf_ct_get((*pskb), &ctinfo);
285 + dir = CTINFO2DIR(ctinfo);
286 + src_ip = ct->tuplehash[dir].tuple.src.u3.ip;
287 + dst_ip = ct->tuplehash[dir].tuple.dst.u3.ip;
288 + src_port = get_src_port(&ct->tuplehash[dir].tuple);
289 + proto = ct->tuplehash[dir].tuple.dst.protonum;
291 + ip = ct->tuplehash[dir].tuple.dst.u3.ip;
292 + port = get_dst_port(&ct->tuplehash[dir].tuple);
295 + if ((*pskb)->mark>0) /* The packet is marked, it's going out */
297 + ct->xid[0]=(*pskb)->mark;
300 + if (ct->xid[0] > 0) {
304 + else if (proto == 17) {
306 + if (!(*pskb)->mark) {
307 + sk = __udp4_lib_lookup(src_ip, src_port, ip, port,
310 + if (sk && hooknum==NF_IP_LOCAL_IN) {
319 + if ((*pskb)->mark>0) /* The packet is marked, it's going out */
321 + ct->xid[0]=(*pskb)->mark;
324 + else if (proto == 6) {
326 + /* It's a listening socket */
327 + connection_sk = (*pskb)->sk;
328 + sock_hold(connection_sk);
330 + else /* It might be a connected socket */
331 + connection_sk = inet_lookup_established(&tcp_hashinfo, src_ip, src_port, ip, port, dif);
334 + if (connection_sk /* Well, some kind of TCP socket */) {
335 + if (connection_sk->sk_peercred.uid == 0 || connection_sk->sk_peercred.uid == (__u32) -1) {
336 + /* Normal case - the peercred on the socket is not set */
337 + connection_sk->sk_peercred.gid = connection_sk->sk_peercred.uid = ct->xid[!dir];
339 + else /* Exceptional case - the peercred was set using SET_PEERCRED. Somebody wants us
340 + to mark packets with some arbitrary value.*/
341 + mark=connection_sk->sk_peercred.uid;
343 + /* Has this connection already been tagged? */
344 + if (ct->xid[dir] < 1) {
345 + /* No - let's tag it */
346 + ct->xid[dir]=connection_sk->sk_nid;
349 + if (mark==-1 && (connection_sk->sk_nid != 0))
350 + mark = ct->xid[dir];
353 + if (connection_sk->sk_state == TCP_TIME_WAIT) {
354 + inet_twsk_put(inet_twsk(connection_sk));
358 + sock_put(connection_sk);
361 + /* Covers CoDemux case */
362 + if (mark < 1 && (ct->xid[dir]>0)) {
363 + mark = ct->xid[dir];
366 + if (mark < 1 && (ct->xid[!dir]>0)) {
367 + mark = ct->xid[!dir];
370 + /* All else failed. Is this a connection over raw sockets? That explains
371 + * why we couldn't get anything out of skb->sk, or look up a "real" connection.*/
372 + if (ct->xid[dir]<1) {
373 + if ((*pskb)->skb_tag) {
374 + ct->xid[dir]=(*pskb)->skb_tag;
381 (*pskb)->mark = mark;
384 + curtag=&__get_cpu_var(sknid_elevator);
385 + if (mark > 0 && *curtag==-2 && hooknum==NF_IP_LOCAL_IN)
394 checkentry_v0(const char *tablename,
398 if (markinfo->mode != XT_MARK_SET
399 && markinfo->mode != XT_MARK_AND
400 - && markinfo->mode != XT_MARK_OR) {
401 + && markinfo->mode != XT_MARK_OR
402 + && markinfo->mode != XT_MARK_COPYXID) {
403 printk(KERN_WARNING "MARK: unknown mode %u\n",
406 diff -Nurb linux-2.6.22-521/net/netfilter/xt_SETXID.c linux-2.6.22-522/net/netfilter/xt_SETXID.c
407 --- linux-2.6.22-521/net/netfilter/xt_SETXID.c 1969-12-31 19:00:00.000000000 -0500
408 +++ linux-2.6.22-522/net/netfilter/xt_SETXID.c 2008-07-28 16:36:24.000000000 -0400
410 +#include <linux/module.h>
411 +#include <linux/skbuff.h>
412 +#include <linux/ip.h>
413 +#include <net/checksum.h>
414 +#include <linux/vs_network.h>
416 +#include <linux/netfilter/x_tables.h>
417 +#include <linux/netfilter/xt_SETXID.h>
419 +MODULE_LICENSE("GPL");
421 +MODULE_DESCRIPTION("");
422 +MODULE_ALIAS("ipt_SETXID");
425 +target_v1(struct sk_buff **pskb,
426 + const struct net_device *in,
427 + const struct net_device *out,
428 + unsigned int hooknum,
429 + const struct xt_target *target,
430 + const void *targinfo)
432 + const struct xt_setxid_target_info_v1 *setxidinfo = targinfo;
434 + switch (setxidinfo->mode) {
435 + case XT_SET_PACKET_XID:
436 + (*pskb)->skb_tag = setxidinfo->mark;
439 + return XT_CONTINUE;
444 +checkentry_v1(const char *tablename,
446 + const struct xt_target *target,
448 + unsigned int hook_mask)
450 + struct xt_setxid_target_info_v1 *setxidinfo = targinfo;
452 + if (setxidinfo->mode != XT_SET_PACKET_XID) {
453 + printk(KERN_WARNING "SETXID: unknown mode %u\n",
461 +static struct xt_target xt_setxid_target[] = {
466 + .checkentry = checkentry_v1,
467 + .target = target_v1,
468 + .targetsize = sizeof(struct xt_setxid_target_info_v1),
474 +static int __init init(void)
478 + err = xt_register_targets(xt_setxid_target, ARRAY_SIZE(xt_setxid_target));
482 +static void __exit fini(void)
484 + xt_unregister_targets(xt_setxid_target, ARRAY_SIZE(xt_setxid_target));