CONNMARK getsockopt/setsockopt functionality
Subsystems (2)
RFC 1/1 CONNMARK getsockopt/setsockopt functionality
Hi,
While thinking on problems with traffic control in user-space proxies,
we came to the conclusion, that under some conditions manually setting the connmark value of an already established connection would be really useful. (For example, it would be possible to traffic-limit based on outband authentication decisions.)
I've created a patch for connmark, which registers a getsockopt() and
a setsockopt() function for getting/setting the connection mark. The patch is for Linux 2.4.25 + CONNMARK from POM-ng, but theoretically it should apply on any 2.4.x version plus the newest connmark patch.
It would be great if this patch could be merged into connmark, as I
think it would be really userful for all kinds of user-space proxies.
As always, any comments are welcome. The patch is absolutely a
proof-of-concept implementation, so beware... :)
Signature · 2 lines
Regards, Krisztian KOVACS
include/linux/netfilter_ipv4.h +1 -0
@@ -64,6 +64,7 @@/* 2.2 firewalling (+ masq) went from 64 through 76 */ /* 2.4 firewalling went 64 through 67. */ #define SO_ORIGINAL_DST 80 +#define SO_CONNMARK 81#ifdef __KERNEL__#ifdef CONFIG_NETFILTER_DEBUG
net/ipv4/netfilter/ip_conntrack_core.c +140 -6
183 lines not on this page · raw message
More recent patches modify files in netfilter_cmarksockopt.
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_ipv4/ipt_CONNMARK.h>
/* IP address to connect to */
#define DEST_IP "10.30.0.6"
int main()
{
int sock;
struct sockaddr_in sin;
struct ipt_connmark_target_info markinfo;
unsigned long mark;
socklen_t len;
char ch;
char buf[] = "almafa, kortefa, szilvafa\n";
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
perror("socket");
return -1;
}
sin.sin_family = AF_INET;
inet_aton(DEST_IP, &sin.sin_addr);
sin.sin_port = htons(10080);
if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) == -1)
{
perror("connect");
return -1;
}
/* mark connection */
markinfo.mark = 42;
markinfo.mask = 0xff;
markinfo.mode = IPT_CONNMARK_SET;
if (setsockopt(sock, SOL_IP, SO_CONNMARK, &markinfo, sizeof(markinfo)) == -1)
{
perror("setsockopt(SOL_IP, SO_CONNMARK)");
return -1;
}
if (write(sock, buf, strlen(buf)) == -1)
{
perror("write");
return -1;
}
/* get mark */
len = sizeof(mark);
if (getsockopt(sock, SOL_IP, SO_CONNMARK, &mark, &len) == -1)
{
perror("getsockopt(SOL_IP, SO_CONNMARK)");
return -1;
}
fprintf(stderr, "mark: %lu\n", mark);
while (read(sock, &ch, 1) == 1)
{
putchar(ch);
}
close(sock); return 0;
}
Hi,
Quoted 10 lines · KOVACS Krisztian
While thinking on problems with traffic control in user-space proxies,
we came to the conclusion, that under some conditions manually setting the connmark value of an already established connection would be really useful. (For example, it would be possible to traffic-limit based on outband authentication decisions.)I've created a patch for connmark, which registers a
getsockopt()and
asetsockopt()function for getting/setting the connection mark. The patch is for Linux 2.4.25 + CONNMARK from POM-ng, but theoretically it should apply on any 2.4.x version plus the newest connmark patch.
I believe ctnetlink would be the proper tool to do conntrack manipulation from userspace.
Best regards,
Jozsef
-
E-mail : [email protected], [email protected] PGP key : http://www.kfki.hu/~kadlec/pgp_public_key.txt
Address : KFKI Research Institute for Particle and Nuclear Physics
H-1525 Budapest 114, POB. 49, Hungary
Hi,
2004-05-24, h keltezéssel 09:31-kor Jozsef Kadlecsik ezt írta:
Quoted 6 lines
Quoted 6 lines
While thinking on problems with traffic control in user-space proxies,
we came to the conclusion, that under some conditions manually setting the connmark value of an already established connection would be really useful. (For example, it would be possible to traffic-limit based on outband authentication decisions.)
Quoted 7 lines
Quoted 4 lines
I've created a patch for connmark, which registers a
getsockopt()and
asetsockopt()function for getting/setting the connection mark. The patch is for Linux 2.4.25 + CONNMARK from POM-ng, but theoretically it should apply on any 2.4.x version plus the newest connmark patch.I believe ctnetlink would be the proper tool to do conntrack manipulation from userspace.
I completely agree. In fact, it was one of the proposed solutions for
the problem in my initial correspondence with Henrik. However, I don't consider ctnetlink ready for prime time (e.g. to be used in a production environment), and my patch has a lot smaller footprint than ctnetlink.
Of course, ctnetlink will be the proper tool, if it will be stable
enough for production use. Until then, I think that a simple/working solution is better than nothing...
BTW, what are the plans for Linux 2.7? Will ctnetlink/netfilter-ha and
pkttables be integrated? (ctnetlink would be especially important...)
Signature · 2 lines
Regards, Krisztian KOVACS
Hi Krisztian,
Quoted 4 lines
I've created a patch for connmark, which registers a
getsockopt()and
asetsockopt()function for getting/setting the connection mark. The patch is for Linux 2.4.25 + CONNMARK from POM-ng, but theoretically it should apply on any 2.4.x version plus the newest connmark patch.
I think I found a small problem with this patch. I'm using 2.4.26 plus latest CVS POM-ng, and after patching with your patch, the kernel doesn't compile.
This is because after patching, ip_conntrack_core.c contains a new function get_sk_proto(struct sock *sk) which checks sk->proto->name, but sk doesn't have an member "proto". It does have a member "prot", and when I change proto to prot, the kernel compiles again (but as yet I have no idea whether your patch actually works :-).
Patch to your patch is attached.
Cheers, Chris.
Signature · 4 lines
_ __ __ _ / __/ / ,__(_)_ | Chris Wilson -- UNIX Firewall Lead Developer | / (_ ,\/ _/ /_ \ | NetServers.co.uk http://www.netservers.co.uk | \__/_/_/_//_/___/ | 21 Signet Court, Cambridge, UK. 01223 576516 |
local/connmark_sockopt.patch +2 -2
@@ -67,9 +67,9 @@ +static int +get_sk_proto(struct sock *sk) +{ -+ if (strcmp(sk->proto->name, "TCP") == 0) ++ if (strcmp(sk->prot->name, "TCP") == 0) + return IPPROTO_TCP; -+ else if (strcmp(sk->proto->name, "UDP") == 0) ++ else if (strcmp(sk->prot->name, "UDP") == 0) + return IPPROTO_UDP; + else + return 0;
Hi Chris,
2004-05-24, h keltezéssel 13:17-kor Chris Wilson ezt írta:
Quoted 3 lines
I think I found a small problem with this patch. I'm using 2.4.26 plus latest CVS POM-ng, and after patching with your patch, the kernel doesn't compile.
Thanks for the fix. Your patch is obviously correct, somehow I
succeeded in messing up the patch :)
I hope it's otherwise correct.
Signature · 2 lines
Regards, Krisztian KOVACS
Hi Krisztian,
Quoted 4 lines
Thanks for the fix. Your patch is obviously correct, somehow I
succeeded in messing up the patch :)I hope it's otherwise correct.
I'm afraid I think I found another problem, although it may not be the fault of your patch. After applying it, I cannot load the ip_conntrack module anymore (and therefore not iptable_nat, which depends on it).
ip_conntrack generates the following error on startup:
Unable to register netfilter socket option
Perhaps someone else is using socket option 81? How would I find out?
Cheers, Chris.
Signature · 4 lines
_ __ __ _ / __/ / ,__(_)_ | Chris Wilson -- UNIX Firewall Lead Developer | / (_ ,\/ _/ /_ \ | NetServers.co.uk http://www.netservers.co.uk | \__/_/_/_//_/___/ | 21 Signet Court, Cambridge, UK. 01223 576516 |
Hi,
2004-05-28, p keltezéssel 12:43-kor Chris Wilson ezt írta:
Quoted 9 lines
Quoted 4 lines
Thanks for the fix. Your patch is obviously correct, somehow I
succeeded in messing up the patch :)I hope it's otherwise correct.
I'm afraid I think I found another problem, although it may not be the fault of your patch. After applying it, I cannot load the
ip_conntrackmodule anymore (and therefore notiptable_nat, which depends on it).
Quoted 5 lines
ip_conntrackgenerates the following error on startup:Unable to register netfilter socket option
Perhaps someone else is using socket option 81? How would I find out?
Strange, it works for me... :) BTW, SOL_IP setsockopts are handled byip_setsockopt(), which calls nf_setsockopt() for from the default branch of the switch. nf_register_sockopt fails only if you try to register some option more than once through that funcion...
http://lxr.linux.no/source/net/core/netfilter.c?v=2.4.26#L90
Signature · 2 lines
Regards, Krisztian KOVACS