Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 1 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 2 | * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/icmp.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/gfp.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 11 | #include <linux/ip.h> |
| 12 | #include <linux/netfilter.h> |
| 13 | #include <linux/netfilter_ipv4.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/proc_fs.h> |
| 17 | #include <net/ip.h> |
| 18 | #include <net/checksum.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | |
| 21 | #include <net/netfilter/nf_conntrack.h> |
| 22 | #include <net/netfilter/nf_conntrack_core.h> |
Yasuyuki Kozakai | 2d59e5c | 2007-07-07 22:24:28 -0700 | [diff] [blame] | 23 | #include <net/netfilter/nf_conntrack_extend.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 24 | #include <net/netfilter/nf_nat.h> |
| 25 | #include <net/netfilter/nf_nat_rule.h> |
| 26 | #include <net/netfilter/nf_nat_protocol.h> |
| 27 | #include <net/netfilter/nf_nat_core.h> |
| 28 | #include <net/netfilter/nf_nat_helper.h> |
| 29 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 30 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 31 | #ifdef CONFIG_XFRM |
| 32 | static void nat_decode_session(struct sk_buff *skb, struct flowi *fl) |
| 33 | { |
Jan Engelhardt | 72b7294 | 2008-04-14 11:15:42 +0200 | [diff] [blame] | 34 | const struct nf_conn *ct; |
| 35 | const struct nf_conntrack_tuple *t; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 36 | enum ip_conntrack_info ctinfo; |
| 37 | enum ip_conntrack_dir dir; |
| 38 | unsigned long statusbit; |
| 39 | |
| 40 | ct = nf_ct_get(skb, &ctinfo); |
| 41 | if (ct == NULL) |
| 42 | return; |
| 43 | dir = CTINFO2DIR(ctinfo); |
| 44 | t = &ct->tuplehash[dir].tuple; |
| 45 | |
| 46 | if (dir == IP_CT_DIR_ORIGINAL) |
| 47 | statusbit = IPS_DST_NAT; |
| 48 | else |
| 49 | statusbit = IPS_SRC_NAT; |
| 50 | |
| 51 | if (ct->status & statusbit) { |
| 52 | fl->fl4_dst = t->dst.u3.ip; |
| 53 | if (t->dst.protonum == IPPROTO_TCP || |
Patrick McHardy | 6185f87 | 2008-03-20 15:15:51 +0100 | [diff] [blame] | 54 | t->dst.protonum == IPPROTO_UDP || |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 55 | t->dst.protonum == IPPROTO_UDPLITE || |
Patrick McHardy | 9d908a6 | 2008-04-14 11:15:50 +0200 | [diff] [blame] | 56 | t->dst.protonum == IPPROTO_DCCP || |
| 57 | t->dst.protonum == IPPROTO_SCTP) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 58 | fl->fl_ip_dport = t->dst.u.tcp.port; |
| 59 | } |
| 60 | |
| 61 | statusbit ^= IPS_NAT_MASK; |
| 62 | |
| 63 | if (ct->status & statusbit) { |
| 64 | fl->fl4_src = t->src.u3.ip; |
| 65 | if (t->dst.protonum == IPPROTO_TCP || |
Patrick McHardy | 6185f87 | 2008-03-20 15:15:51 +0100 | [diff] [blame] | 66 | t->dst.protonum == IPPROTO_UDP || |
Patrick McHardy | 4910a08 | 2008-03-20 15:15:57 +0100 | [diff] [blame] | 67 | t->dst.protonum == IPPROTO_UDPLITE || |
Patrick McHardy | 9d908a6 | 2008-04-14 11:15:50 +0200 | [diff] [blame] | 68 | t->dst.protonum == IPPROTO_DCCP || |
| 69 | t->dst.protonum == IPPROTO_SCTP) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 70 | fl->fl_ip_sport = t->src.u.tcp.port; |
| 71 | } |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | static unsigned int |
| 76 | nf_nat_fn(unsigned int hooknum, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 77 | struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 78 | const struct net_device *in, |
| 79 | const struct net_device *out, |
| 80 | int (*okfn)(struct sk_buff *)) |
| 81 | { |
| 82 | struct nf_conn *ct; |
| 83 | enum ip_conntrack_info ctinfo; |
| 84 | struct nf_conn_nat *nat; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 85 | /* maniptype == SRC for postrouting. */ |
| 86 | enum nf_nat_manip_type maniptype = HOOK2MANIP(hooknum); |
| 87 | |
| 88 | /* We never see fragments: conntrack defrags on pre-routing |
| 89 | and local-out, and nf_nat_out protects post-routing. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 90 | NF_CT_ASSERT(!(ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET))); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 91 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 92 | ct = nf_ct_get(skb, &ctinfo); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 93 | /* Can't track? It's not due to stress, or conntrack would |
| 94 | have dropped it. Hence it's the user's responsibilty to |
| 95 | packet filter it out, or implement conntrack/NAT for that |
| 96 | protocol. 8) --RR */ |
Patrick McHardy | 42cf800 | 2008-04-14 11:15:50 +0200 | [diff] [blame] | 97 | if (!ct) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 98 | return NF_ACCEPT; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 99 | |
| 100 | /* Don't try to NAT if this packet is not conntracked */ |
Eric Dumazet | 5bfddbd | 2010-06-08 16:09:52 +0200 | [diff] [blame] | 101 | if (nf_ct_is_untracked(ct)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 102 | return NF_ACCEPT; |
| 103 | |
| 104 | nat = nfct_nat(ct); |
Yasuyuki Kozakai | 2d59e5c | 2007-07-07 22:24:28 -0700 | [diff] [blame] | 105 | if (!nat) { |
Patrick McHardy | 8c87238 | 2008-04-14 11:15:51 +0200 | [diff] [blame] | 106 | /* NAT module was loaded late. */ |
| 107 | if (nf_ct_is_confirmed(ct)) |
| 108 | return NF_ACCEPT; |
Yasuyuki Kozakai | 2d59e5c | 2007-07-07 22:24:28 -0700 | [diff] [blame] | 109 | nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC); |
| 110 | if (nat == NULL) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 111 | pr_debug("failed to add NAT extension\n"); |
Yasuyuki Kozakai | 2d59e5c | 2007-07-07 22:24:28 -0700 | [diff] [blame] | 112 | return NF_ACCEPT; |
| 113 | } |
| 114 | } |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 115 | |
| 116 | switch (ctinfo) { |
| 117 | case IP_CT_RELATED: |
| 118 | case IP_CT_RELATED+IP_CT_IS_REPLY: |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 119 | if (ip_hdr(skb)->protocol == IPPROTO_ICMP) { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 120 | if (!nf_nat_icmp_reply_translation(ct, ctinfo, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 121 | hooknum, skb)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 122 | return NF_DROP; |
| 123 | else |
| 124 | return NF_ACCEPT; |
| 125 | } |
| 126 | /* Fall thru... (Only ICMPs can be IP_CT_IS_REPLY) */ |
| 127 | case IP_CT_NEW: |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 128 | |
| 129 | /* Seen it before? This can happen for loopback, retrans, |
| 130 | or local packets.. */ |
| 131 | if (!nf_nat_initialized(ct, maniptype)) { |
| 132 | unsigned int ret; |
| 133 | |
Patrick McHardy | c68cd6c | 2010-06-17 06:12:26 +0200 | [diff] [blame] | 134 | ret = nf_nat_rule_find(skb, hooknum, in, out, ct); |
Zhitong Wang | 2206831 | 2010-04-13 11:25:41 +0200 | [diff] [blame] | 135 | if (ret != NF_ACCEPT) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 136 | return ret; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 137 | } else |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 138 | pr_debug("Already setup manip %s for ct %p\n", |
| 139 | maniptype == IP_NAT_MANIP_SRC ? "SRC" : "DST", |
| 140 | ct); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 141 | break; |
| 142 | |
| 143 | default: |
| 144 | /* ESTABLISHED */ |
| 145 | NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED || |
| 146 | ctinfo == (IP_CT_ESTABLISHED+IP_CT_IS_REPLY)); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 149 | return nf_nat_packet(ct, ctinfo, hooknum, skb); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static unsigned int |
| 153 | nf_nat_in(unsigned int hooknum, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 154 | struct sk_buff *skb, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 155 | const struct net_device *in, |
| 156 | const struct net_device *out, |
| 157 | int (*okfn)(struct sk_buff *)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 158 | { |
| 159 | unsigned int ret; |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 160 | __be32 daddr = ip_hdr(skb)->daddr; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 161 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 162 | ret = nf_nat_fn(hooknum, skb, in, out, okfn); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 163 | if (ret != NF_DROP && ret != NF_STOLEN && |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 164 | daddr != ip_hdr(skb)->daddr) |
| 165 | skb_dst_drop(skb); |
| 166 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static unsigned int |
| 171 | nf_nat_out(unsigned int hooknum, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 172 | struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 173 | const struct net_device *in, |
| 174 | const struct net_device *out, |
| 175 | int (*okfn)(struct sk_buff *)) |
| 176 | { |
| 177 | #ifdef CONFIG_XFRM |
Jan Engelhardt | 72b7294 | 2008-04-14 11:15:42 +0200 | [diff] [blame] | 178 | const struct nf_conn *ct; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 179 | enum ip_conntrack_info ctinfo; |
| 180 | #endif |
| 181 | unsigned int ret; |
| 182 | |
| 183 | /* root is playing with raw sockets. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 184 | if (skb->len < sizeof(struct iphdr) || |
| 185 | ip_hdrlen(skb) < sizeof(struct iphdr)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 186 | return NF_ACCEPT; |
| 187 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 188 | ret = nf_nat_fn(hooknum, skb, in, out, okfn); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 189 | #ifdef CONFIG_XFRM |
| 190 | if (ret != NF_DROP && ret != NF_STOLEN && |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 191 | (ct = nf_ct_get(skb, &ctinfo)) != NULL) { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 192 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
| 193 | |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 194 | if ((ct->tuplehash[dir].tuple.src.u3.ip != |
| 195 | ct->tuplehash[!dir].tuple.dst.u3.ip) || |
| 196 | (ct->tuplehash[dir].tuple.src.u.all != |
| 197 | ct->tuplehash[!dir].tuple.dst.u.all) |
| 198 | ) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 199 | return ip_xfrm_me_harder(skb) == 0 ? ret : NF_DROP; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 200 | } |
| 201 | #endif |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | static unsigned int |
| 206 | nf_nat_local_fn(unsigned int hooknum, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 207 | struct sk_buff *skb, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 208 | const struct net_device *in, |
| 209 | const struct net_device *out, |
| 210 | int (*okfn)(struct sk_buff *)) |
| 211 | { |
Jan Engelhardt | 72b7294 | 2008-04-14 11:15:42 +0200 | [diff] [blame] | 212 | const struct nf_conn *ct; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 213 | enum ip_conntrack_info ctinfo; |
| 214 | unsigned int ret; |
| 215 | |
| 216 | /* root is playing with raw sockets. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 217 | if (skb->len < sizeof(struct iphdr) || |
| 218 | ip_hdrlen(skb) < sizeof(struct iphdr)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 219 | return NF_ACCEPT; |
| 220 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 221 | ret = nf_nat_fn(hooknum, skb, in, out, okfn); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 222 | if (ret != NF_DROP && ret != NF_STOLEN && |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 223 | (ct = nf_ct_get(skb, &ctinfo)) != NULL) { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 224 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
| 225 | |
| 226 | if (ct->tuplehash[dir].tuple.dst.u3.ip != |
Patrick McHardy | 848c29f | 2007-03-22 12:30:29 -0700 | [diff] [blame] | 227 | ct->tuplehash[!dir].tuple.src.u3.ip) { |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 228 | if (ip_route_me_harder(skb, RTN_UNSPEC)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 229 | ret = NF_DROP; |
Patrick McHardy | 848c29f | 2007-03-22 12:30:29 -0700 | [diff] [blame] | 230 | } |
| 231 | #ifdef CONFIG_XFRM |
| 232 | else if (ct->tuplehash[dir].tuple.dst.u.all != |
| 233 | ct->tuplehash[!dir].tuple.src.u.all) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 234 | if (ip_xfrm_me_harder(skb)) |
Patrick McHardy | 848c29f | 2007-03-22 12:30:29 -0700 | [diff] [blame] | 235 | ret = NF_DROP; |
| 236 | #endif |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 237 | } |
| 238 | return ret; |
| 239 | } |
| 240 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 241 | /* We must be after connection tracking and before packet filtering. */ |
| 242 | |
Patrick McHardy | 1999414 | 2007-12-05 01:23:00 -0800 | [diff] [blame] | 243 | static struct nf_hook_ops nf_nat_ops[] __read_mostly = { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 244 | /* Before packet filtering, change destination */ |
| 245 | { |
| 246 | .hook = nf_nat_in, |
| 247 | .owner = THIS_MODULE, |
Jan Engelhardt | 24c232d | 2009-06-13 06:20:29 +0200 | [diff] [blame] | 248 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 249 | .hooknum = NF_INET_PRE_ROUTING, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 250 | .priority = NF_IP_PRI_NAT_DST, |
| 251 | }, |
| 252 | /* After packet filtering, change source */ |
| 253 | { |
| 254 | .hook = nf_nat_out, |
| 255 | .owner = THIS_MODULE, |
Jan Engelhardt | 24c232d | 2009-06-13 06:20:29 +0200 | [diff] [blame] | 256 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 257 | .hooknum = NF_INET_POST_ROUTING, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 258 | .priority = NF_IP_PRI_NAT_SRC, |
| 259 | }, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 260 | /* Before packet filtering, change destination */ |
| 261 | { |
| 262 | .hook = nf_nat_local_fn, |
| 263 | .owner = THIS_MODULE, |
Jan Engelhardt | 24c232d | 2009-06-13 06:20:29 +0200 | [diff] [blame] | 264 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 265 | .hooknum = NF_INET_LOCAL_OUT, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 266 | .priority = NF_IP_PRI_NAT_DST, |
| 267 | }, |
| 268 | /* After packet filtering, change source */ |
| 269 | { |
| 270 | .hook = nf_nat_fn, |
| 271 | .owner = THIS_MODULE, |
Jan Engelhardt | 24c232d | 2009-06-13 06:20:29 +0200 | [diff] [blame] | 272 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 273 | .hooknum = NF_INET_LOCAL_IN, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 274 | .priority = NF_IP_PRI_NAT_SRC, |
| 275 | }, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | static int __init nf_nat_standalone_init(void) |
| 279 | { |
Yasuyuki Kozakai | 2d59e5c | 2007-07-07 22:24:28 -0700 | [diff] [blame] | 280 | int ret = 0; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 281 | |
Patrick McHardy | 591e620 | 2007-08-07 18:12:01 -0700 | [diff] [blame] | 282 | need_ipv4_conntrack(); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 283 | |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 284 | #ifdef CONFIG_XFRM |
| 285 | BUG_ON(ip_nat_decode_session != NULL); |
Patrick McHardy | 051578c | 2007-12-17 22:42:51 -0800 | [diff] [blame] | 286 | rcu_assign_pointer(ip_nat_decode_session, nat_decode_session); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 287 | #endif |
| 288 | ret = nf_nat_rule_init(); |
| 289 | if (ret < 0) { |
Stephen Hemminger | 654d0fb | 2010-05-13 15:02:08 +0200 | [diff] [blame] | 290 | pr_err("nf_nat_init: can't setup rules.\n"); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 291 | goto cleanup_decode_session; |
| 292 | } |
| 293 | ret = nf_register_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops)); |
| 294 | if (ret < 0) { |
Stephen Hemminger | 654d0fb | 2010-05-13 15:02:08 +0200 | [diff] [blame] | 295 | pr_err("nf_nat_init: can't register hooks.\n"); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 296 | goto cleanup_rule_init; |
| 297 | } |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 298 | return ret; |
| 299 | |
| 300 | cleanup_rule_init: |
| 301 | nf_nat_rule_cleanup(); |
| 302 | cleanup_decode_session: |
| 303 | #ifdef CONFIG_XFRM |
Patrick McHardy | 051578c | 2007-12-17 22:42:51 -0800 | [diff] [blame] | 304 | rcu_assign_pointer(ip_nat_decode_session, NULL); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 305 | synchronize_net(); |
| 306 | #endif |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | static void __exit nf_nat_standalone_fini(void) |
| 311 | { |
| 312 | nf_unregister_hooks(nf_nat_ops, ARRAY_SIZE(nf_nat_ops)); |
| 313 | nf_nat_rule_cleanup(); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 314 | #ifdef CONFIG_XFRM |
Patrick McHardy | 051578c | 2007-12-17 22:42:51 -0800 | [diff] [blame] | 315 | rcu_assign_pointer(ip_nat_decode_session, NULL); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 316 | synchronize_net(); |
| 317 | #endif |
| 318 | /* Conntrack caches are unregistered in nf_conntrack_cleanup */ |
| 319 | } |
| 320 | |
| 321 | module_init(nf_nat_standalone_init); |
| 322 | module_exit(nf_nat_standalone_fini); |
| 323 | |
| 324 | MODULE_LICENSE("GPL"); |
| 325 | MODULE_ALIAS("ip_nat"); |