Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / net / ipv4 / netfilter / ip_nat_amanda.c
1 /* Amanda extension for TCP NAT alteration.
2  * (C) 2002 by Brian J. Murrell <netfilter@interlinx.bc.ca>
3  * based on a copy of HW's ip_nat_irc.c as well as other modules
4  *
5  *      This program is free software; you can redistribute it and/or
6  *      modify it under the terms of the GNU General Public License
7  *      as published by the Free Software Foundation; either version
8  *      2 of the License, or (at your option) any later version.
9  *
10  *      Module load syntax:
11  *      insmod ip_nat_amanda.o
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/netfilter.h>
17 #include <linux/skbuff.h>
18 #include <linux/ip.h>
19 #include <linux/udp.h>
20 #include <net/tcp.h>
21 #include <net/udp.h>
22
23 #include <linux/netfilter_ipv4.h>
24 #include <linux/netfilter_ipv4/ip_nat.h>
25 #include <linux/netfilter_ipv4/ip_nat_helper.h>
26 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
27 #include <linux/netfilter_ipv4/ip_conntrack_amanda.h>
28
29
30 MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>");
31 MODULE_DESCRIPTION("Amanda NAT helper");
32 MODULE_LICENSE("GPL");
33
34 static unsigned int help(struct sk_buff **pskb,
35                          enum ip_conntrack_info ctinfo,
36                          unsigned int matchoff,
37                          unsigned int matchlen,
38                          struct ip_conntrack_expect *exp)
39 {
40         char buffer[sizeof("65535")];
41         u_int16_t port;
42         unsigned int ret;
43
44         /* Connection comes from client. */
45         exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
46         exp->dir = IP_CT_DIR_ORIGINAL;
47
48         /* When you see the packet, we need to NAT it the same as the
49          * this one (ie. same IP: it will be TCP and master is UDP). */
50         exp->expectfn = ip_nat_follow_master;
51
52         /* Try to get same port: if not, try to change it. */
53         for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
54                 exp->tuple.dst.u.tcp.port = htons(port);
55                 if (ip_conntrack_expect_related(exp) == 0)
56                         break;
57         }
58
59         if (port == 0)
60                 return NF_DROP;
61
62         sprintf(buffer, "%u", port);
63         ret = ip_nat_mangle_udp_packet(pskb, exp->master, ctinfo,
64                                        matchoff, matchlen,
65                                        buffer, strlen(buffer));
66         if (ret != NF_ACCEPT)
67                 ip_conntrack_unexpect_related(exp);
68         return ret;
69 }
70
71 static void __exit ip_nat_amanda_fini(void)
72 {
73         ip_nat_amanda_hook = NULL;
74         /* Make sure noone calls it, meanwhile. */
75         synchronize_net();
76 }
77
78 static int __init ip_nat_amanda_init(void)
79 {
80         BUG_ON(ip_nat_amanda_hook);
81         ip_nat_amanda_hook = help;
82         return 0;
83 }
84
85 module_init(ip_nat_amanda_init);
86 module_exit(ip_nat_amanda_fini);