Blame view

net/netfilter/nf_nat_amanda.c 2.45 KB
169589005   Patrick McHardy   [NETFILTER]: nf_c...
1
2
3
  /* 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   Patrick McHardy   netfilter: add my...
4
   * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
169589005   Patrick McHardy   [NETFILTER]: nf_c...
5
6
7
8
9
10
11
12
13
14
15
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version
   * 2 of the License, or (at your option) any later version.
   */
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/skbuff.h>
  #include <linux/udp.h>
169589005   Patrick McHardy   [NETFILTER]: nf_c...
16
17
  #include <net/netfilter/nf_conntrack_helper.h>
  #include <net/netfilter/nf_conntrack_expect.h>
1afc56794   Pablo Neira Ayuso   netfilter: nf_ct_...
18
  #include <net/netfilter/nf_nat_helper.h>
169589005   Patrick McHardy   [NETFILTER]: nf_c...
19
20
21
22
23
24
  #include <linux/netfilter/nf_conntrack_amanda.h>
  
  MODULE_AUTHOR("Brian J. Murrell <netfilter@interlinx.bc.ca>");
  MODULE_DESCRIPTION("Amanda NAT helper");
  MODULE_LICENSE("GPL");
  MODULE_ALIAS("ip_nat_amanda");
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
25
  static unsigned int help(struct sk_buff *skb,
169589005   Patrick McHardy   [NETFILTER]: nf_c...
26
  			 enum ip_conntrack_info ctinfo,
051966c0c   Patrick McHardy   netfilter: nf_nat...
27
  			 unsigned int protoff,
169589005   Patrick McHardy   [NETFILTER]: nf_c...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  			 unsigned int matchoff,
  			 unsigned int matchlen,
  			 struct nf_conntrack_expect *exp)
  {
  	char buffer[sizeof("65535")];
  	u_int16_t port;
  	unsigned int ret;
  
  	/* 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   Eric Dumazet   netfilter: nf_nat...
46
  		int res;
5b92b61f3   Pablo Neira Ayuso   netfilter: nf_nat...
47

169589005   Patrick McHardy   [NETFILTER]: nf_c...
48
  		exp->tuple.dst.u.tcp.port = htons(port);
ab0cba251   Eric Dumazet   netfilter: nf_nat...
49
50
  		res = nf_ct_expect_related(exp);
  		if (res == 0)
5b92b61f3   Pablo Neira Ayuso   netfilter: nf_nat...
51
  			break;
ab0cba251   Eric Dumazet   netfilter: nf_nat...
52
  		else if (res != -EBUSY) {
5b92b61f3   Pablo Neira Ayuso   netfilter: nf_nat...
53
  			port = 0;
169589005   Patrick McHardy   [NETFILTER]: nf_c...
54
  			break;
5b92b61f3   Pablo Neira Ayuso   netfilter: nf_nat...
55
  		}
169589005   Patrick McHardy   [NETFILTER]: nf_c...
56
  	}
b20ab9cc6   Pablo Neira Ayuso   netfilter: nf_ct_...
57
58
  	if (port == 0) {
  		nf_ct_helper_log(skb, exp->master, "all ports in use");
169589005   Patrick McHardy   [NETFILTER]: nf_c...
59
  		return NF_DROP;
b20ab9cc6   Pablo Neira Ayuso   netfilter: nf_ct_...
60
  	}
169589005   Patrick McHardy   [NETFILTER]: nf_c...
61
62
  
  	sprintf(buffer, "%u", port);
3db05fea5   Herbert Xu   [NETFILTER]: Repl...
63
  	ret = nf_nat_mangle_udp_packet(skb, exp->master, ctinfo,
051966c0c   Patrick McHardy   netfilter: nf_nat...
64
  				       protoff, matchoff, matchlen,
169589005   Patrick McHardy   [NETFILTER]: nf_c...
65
  				       buffer, strlen(buffer));
b20ab9cc6   Pablo Neira Ayuso   netfilter: nf_ct_...
66
67
  	if (ret != NF_ACCEPT) {
  		nf_ct_helper_log(skb, exp->master, "cannot mangle packet");
6823645d6   Patrick McHardy   [NETFILTER]: nf_c...
68
  		nf_ct_unexpect_related(exp);
b20ab9cc6   Pablo Neira Ayuso   netfilter: nf_ct_...
69
  	}
169589005   Patrick McHardy   [NETFILTER]: nf_c...
70
71
72
73
74
  	return ret;
  }
  
  static void __exit nf_nat_amanda_fini(void)
  {
a9b3cd7f3   Stephen Hemminger   rcu: convert uses...
75
  	RCU_INIT_POINTER(nf_nat_amanda_hook, NULL);
169589005   Patrick McHardy   [NETFILTER]: nf_c...
76
77
78
79
80
  	synchronize_rcu();
  }
  
  static int __init nf_nat_amanda_init(void)
  {
d1332e0ab   Patrick McHardy   [NETFILTER]: remo...
81
  	BUG_ON(nf_nat_amanda_hook != NULL);
a9b3cd7f3   Stephen Hemminger   rcu: convert uses...
82
  	RCU_INIT_POINTER(nf_nat_amanda_hook, help);
169589005   Patrick McHardy   [NETFILTER]: nf_c...
83
84
85
86
87
  	return 0;
  }
  
  module_init(nf_nat_amanda_init);
  module_exit(nf_nat_amanda_fini);