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