Blame view
net/netfilter/nf_nat_amanda.c
2.46 KB
2874c5fd2
|
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
169589005
|
2 3 4 |
/* Amanda extension for TCP NAT alteration. * (C) 2002 by Brian J. Murrell <netfilter@interlinx.bc.ca> * based on a copy of HW's ip_nat_irc.c as well as other modules |
f229f6ce4
|
5 |
* (C) 2006-2012 Patrick McHardy <kaber@trash.net> |
169589005
|
6 7 8 9 10 11 |
*/ #include <linux/kernel.h> #include <linux/module.h> #include <linux/skbuff.h> #include <linux/udp.h> |
169589005
|
12 13 |
#include <net/netfilter/nf_conntrack_helper.h> #include <net/netfilter/nf_conntrack_expect.h> |
1afc56794
|
14 |
#include <net/netfilter/nf_nat_helper.h> |
169589005
|
15 |
#include <linux/netfilter/nf_conntrack_amanda.h> |
53b11308a
|
16 |
#define NAT_HELPER_NAME "amanda" |
169589005
|
17 18 19 |
MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>"); MODULE_DESCRIPTION("Amanda NAT helper"); MODULE_LICENSE("GPL"); |
53b11308a
|
20 21 22 23 |
MODULE_ALIAS_NF_NAT_HELPER(NAT_HELPER_NAME); static struct nf_conntrack_nat_helper nat_helper_amanda = NF_CT_NAT_HELPER_INIT(NAT_HELPER_NAME); |
169589005
|
24 |
|
3db05fea5
|
25 |
static unsigned int help(struct sk_buff *skb, |
169589005
|
26 |
enum ip_conntrack_info ctinfo, |
051966c0c
|
27 |
unsigned int protoff, |
169589005
|
28 29 30 31 32 33 |
unsigned int matchoff, unsigned int matchlen, struct nf_conntrack_expect *exp) { char buffer[sizeof("65535")]; u_int16_t port; |
169589005
|
34 35 36 37 38 39 40 41 42 43 44 |
/* Connection comes from client. */ exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port; exp->dir = IP_CT_DIR_ORIGINAL; /* When you see the packet, we need to NAT it the same as the * this one (ie. same IP: it will be TCP and master is UDP). */ exp->expectfn = nf_nat_follow_master; /* Try to get same port: if not, try to change it. */ for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) { |
ab0cba251
|
45 |
int res; |
5b92b61f3
|
46 |
|
169589005
|
47 |
exp->tuple.dst.u.tcp.port = htons(port); |
3c00fb0bf
|
48 |
res = nf_ct_expect_related(exp, 0); |
ab0cba251
|
49 |
if (res == 0) |
5b92b61f3
|
50 |
break; |
ab0cba251
|
51 |
else if (res != -EBUSY) { |
5b92b61f3
|
52 |
port = 0; |
169589005
|
53 |
break; |
5b92b61f3
|
54 |
} |
169589005
|
55 |
} |
b20ab9cc6
|
56 57 |
if (port == 0) { nf_ct_helper_log(skb, exp->master, "all ports in use"); |
169589005
|
58 |
return NF_DROP; |
b20ab9cc6
|
59 |
} |
169589005
|
60 61 |
sprintf(buffer, "%u", port); |
cba81cc4c
|
62 63 64 |
if (!nf_nat_mangle_udp_packet(skb, exp->master, ctinfo, protoff, matchoff, matchlen, buffer, strlen(buffer))) { |
b20ab9cc6
|
65 |
nf_ct_helper_log(skb, exp->master, "cannot mangle packet"); |
6823645d6
|
66 |
nf_ct_unexpect_related(exp); |
cba81cc4c
|
67 |
return NF_DROP; |
b20ab9cc6
|
68 |
} |
cba81cc4c
|
69 |
return NF_ACCEPT; |
169589005
|
70 71 72 73 |
} static void __exit nf_nat_amanda_fini(void) { |
53b11308a
|
74 |
nf_nat_helper_unregister(&nat_helper_amanda); |
a9b3cd7f3
|
75 |
RCU_INIT_POINTER(nf_nat_amanda_hook, NULL); |
169589005
|
76 77 78 79 80 |
synchronize_rcu(); } static int __init nf_nat_amanda_init(void) { |
d1332e0ab
|
81 |
BUG_ON(nf_nat_amanda_hook != NULL); |
53b11308a
|
82 |
nf_nat_helper_register(&nat_helper_amanda); |
a9b3cd7f3
|
83 |
RCU_INIT_POINTER(nf_nat_amanda_hook, help); |
169589005
|
84 85 86 87 88 |
return 0; } module_init(nf_nat_amanda_init); module_exit(nf_nat_amanda_fini); |