Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | ||
4acdbdbe | 59 | if (port == 0) |
1da177e4 | 60 | return NF_DROP; |
1da177e4 LT |
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 | ||
65b4b4e8 | 71 | static void __exit ip_nat_amanda_fini(void) |
1da177e4 LT |
72 | { |
73 | ip_nat_amanda_hook = NULL; | |
74 | /* Make sure noone calls it, meanwhile. */ | |
75 | synchronize_net(); | |
76 | } | |
77 | ||
65b4b4e8 | 78 | static int __init ip_nat_amanda_init(void) |
1da177e4 LT |
79 | { |
80 | BUG_ON(ip_nat_amanda_hook); | |
81 | ip_nat_amanda_hook = help; | |
82 | return 0; | |
83 | } | |
84 | ||
65b4b4e8 AM |
85 | module_init(ip_nat_amanda_init); |
86 | module_exit(ip_nat_amanda_fini); |