drivers: power: report battery voltage in AOSP compatible format
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4.c
CommitLineData
73e4022f 1
9fb9cbb1
YK
2/* (C) 1999-2001 Paul `Rusty' Russell
3 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
f229f6ce 4 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
9fb9cbb1
YK
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9fb9cbb1
YK
9 */
10
9fb9cbb1
YK
11#include <linux/types.h>
12#include <linux/ip.h>
13#include <linux/netfilter.h>
14#include <linux/module.h>
15#include <linux/skbuff.h>
16#include <linux/icmp.h>
17#include <linux/sysctl.h>
0ae2cfe7 18#include <net/route.h>
9fb9cbb1
YK
19#include <net/ip.h>
20
21#include <linux/netfilter_ipv4.h>
22#include <net/netfilter/nf_conntrack.h>
23#include <net/netfilter/nf_conntrack_helper.h>
605dcad6 24#include <net/netfilter/nf_conntrack_l4proto.h>
9fb9cbb1 25#include <net/netfilter/nf_conntrack_l3proto.h>
5d0aa2cc 26#include <net/netfilter/nf_conntrack_zones.h>
9fb9cbb1
YK
27#include <net/netfilter/nf_conntrack_core.h>
28#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
dd13b010 29#include <net/netfilter/nf_nat_helper.h>
73e4022f 30#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
74f7a655 31#include <net/netfilter/nf_log.h>
dd13b010 32
8ce8439a
JE
33static bool ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
34 struct nf_conntrack_tuple *tuple)
9fb9cbb1 35{
32948588
JE
36 const __be32 *ap;
37 __be32 _addrs[2];
9fb9cbb1
YK
38 ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr),
39 sizeof(u_int32_t) * 2, _addrs);
40 if (ap == NULL)
8ce8439a 41 return false;
9fb9cbb1
YK
42
43 tuple->src.u3.ip = ap[0];
44 tuple->dst.u3.ip = ap[1];
45
8ce8439a 46 return true;
9fb9cbb1
YK
47}
48
8ce8439a
JE
49static bool ipv4_invert_tuple(struct nf_conntrack_tuple *tuple,
50 const struct nf_conntrack_tuple *orig)
9fb9cbb1
YK
51{
52 tuple->src.u3.ip = orig->dst.u3.ip;
53 tuple->dst.u3.ip = orig->src.u3.ip;
54
8ce8439a 55 return true;
9fb9cbb1
YK
56}
57
58static int ipv4_print_tuple(struct seq_file *s,
59 const struct nf_conntrack_tuple *tuple)
60{
cffee385
HH
61 return seq_printf(s, "src=%pI4 dst=%pI4 ",
62 &tuple->src.u3.ip, &tuple->dst.u3.ip);
9fb9cbb1
YK
63}
64
ffc30690
YK
65static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
66 unsigned int *dataoff, u_int8_t *protonum)
9fb9cbb1 67{
32948588
JE
68 const struct iphdr *iph;
69 struct iphdr _iph;
ffc30690
YK
70
71 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
72 if (iph == NULL)
8430eac2 73 return -NF_ACCEPT;
ffc30690 74
0fb96701
PM
75 /* Conntrack defragments packets, we might still see fragments
76 * inside ICMP packets though. */
77 if (iph->frag_off & htons(IP_OFFSET))
8430eac2 78 return -NF_ACCEPT;
9fb9cbb1 79
ffc30690
YK
80 *dataoff = nhoff + (iph->ihl << 2);
81 *protonum = iph->protocol;
9fb9cbb1 82
07153c6e
JK
83 /* Check bogus IP headers */
84 if (*dataoff > skb->len) {
85 pr_debug("nf_conntrack_ipv4: bogus IPv4 packet: "
86 "nhoff %u, ihl %u, skblen %u\n",
87 nhoff, iph->ihl << 2, skb->len);
88 return -NF_ACCEPT;
89 }
90
9fb9cbb1
YK
91 return NF_ACCEPT;
92}
93
12f7a505
PNA
94static unsigned int ipv4_helper(unsigned int hooknum,
95 struct sk_buff *skb,
96 const struct net_device *in,
97 const struct net_device *out,
98 int (*okfn)(struct sk_buff *))
9fb9cbb1
YK
99{
100 struct nf_conn *ct;
101 enum ip_conntrack_info ctinfo;
32948588
JE
102 const struct nf_conn_help *help;
103 const struct nf_conntrack_helper *helper;
9fb9cbb1
YK
104
105 /* This is where we call the helper: as the packet goes out. */
3db05fea 106 ct = nf_ct_get(skb, &ctinfo);
fb048833 107 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
12f7a505 108 return NF_ACCEPT;
dc808fe2
HW
109
110 help = nfct_help(ct);
3c158f7f 111 if (!help)
12f7a505 112 return NF_ACCEPT;
dd13b010 113
3c158f7f
PM
114 /* rcu_read_lock()ed by nf_hook_slow */
115 helper = rcu_dereference(help->helper);
116 if (!helper)
12f7a505 117 return NF_ACCEPT;
dd13b010 118
b20ab9cc
PNA
119 return helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb),
120 ct, ctinfo);
12f7a505
PNA
121}
122
123static unsigned int ipv4_confirm(unsigned int hooknum,
124 struct sk_buff *skb,
125 const struct net_device *in,
126 const struct net_device *out,
127 int (*okfn)(struct sk_buff *))
128{
129 struct nf_conn *ct;
130 enum ip_conntrack_info ctinfo;
131
132 ct = nf_ct_get(skb, &ctinfo);
133 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
134 goto out;
dd13b010 135
42c1edd3
JA
136 /* adjust seqs for loopback traffic only in outgoing direction */
137 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
138 !nf_is_loopback_packet(skb)) {
dd13b010
PM
139 typeof(nf_nat_seq_adjust_hook) seq_adjust;
140
141 seq_adjust = rcu_dereference(nf_nat_seq_adjust_hook);
051966c0
PM
142 if (!seq_adjust ||
143 !seq_adjust(skb, ct, ctinfo, ip_hdrlen(skb))) {
1db7a748 144 NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop);
dd13b010 145 return NF_DROP;
1db7a748 146 }
dd13b010
PM
147 }
148out:
149 /* We've seen it coming out the other side: confirm it */
150 return nf_conntrack_confirm(skb);
9fb9cbb1
YK
151}
152
9fb9cbb1 153static unsigned int ipv4_conntrack_in(unsigned int hooknum,
3db05fea 154 struct sk_buff *skb,
9fb9cbb1
YK
155 const struct net_device *in,
156 const struct net_device *out,
157 int (*okfn)(struct sk_buff *))
158{
a702a65f 159 return nf_conntrack_in(dev_net(in), PF_INET, hooknum, skb);
9fb9cbb1
YK
160}
161
162static unsigned int ipv4_conntrack_local(unsigned int hooknum,
3db05fea 163 struct sk_buff *skb,
e905a9ed
YH
164 const struct net_device *in,
165 const struct net_device *out,
166 int (*okfn)(struct sk_buff *))
9fb9cbb1
YK
167{
168 /* root is playing with raw sockets. */
3db05fea 169 if (skb->len < sizeof(struct iphdr) ||
88843104 170 ip_hdrlen(skb) < sizeof(struct iphdr))
9fb9cbb1 171 return NF_ACCEPT;
a702a65f 172 return nf_conntrack_in(dev_net(out), PF_INET, hooknum, skb);
9fb9cbb1
YK
173}
174
175/* Connection tracking may drop packets, but never alters them, so
176 make it the first hook. */
1999414a 177static struct nf_hook_ops ipv4_conntrack_ops[] __read_mostly = {
964ddaa1
PM
178 {
179 .hook = ipv4_conntrack_in,
180 .owner = THIS_MODULE,
57750a22 181 .pf = NFPROTO_IPV4,
6e23ae2a 182 .hooknum = NF_INET_PRE_ROUTING,
964ddaa1
PM
183 .priority = NF_IP_PRI_CONNTRACK,
184 },
964ddaa1
PM
185 {
186 .hook = ipv4_conntrack_local,
187 .owner = THIS_MODULE,
57750a22 188 .pf = NFPROTO_IPV4,
6e23ae2a 189 .hooknum = NF_INET_LOCAL_OUT,
964ddaa1
PM
190 .priority = NF_IP_PRI_CONNTRACK,
191 },
12f7a505
PNA
192 {
193 .hook = ipv4_helper,
194 .owner = THIS_MODULE,
195 .pf = NFPROTO_IPV4,
196 .hooknum = NF_INET_POST_ROUTING,
197 .priority = NF_IP_PRI_CONNTRACK_HELPER,
198 },
964ddaa1
PM
199 {
200 .hook = ipv4_confirm,
201 .owner = THIS_MODULE,
57750a22 202 .pf = NFPROTO_IPV4,
6e23ae2a 203 .hooknum = NF_INET_POST_ROUTING,
964ddaa1
PM
204 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
205 },
12f7a505
PNA
206 {
207 .hook = ipv4_helper,
208 .owner = THIS_MODULE,
209 .pf = NFPROTO_IPV4,
210 .hooknum = NF_INET_LOCAL_IN,
211 .priority = NF_IP_PRI_CONNTRACK_HELPER,
212 },
964ddaa1
PM
213 {
214 .hook = ipv4_confirm,
215 .owner = THIS_MODULE,
57750a22 216 .pf = NFPROTO_IPV4,
6e23ae2a 217 .hooknum = NF_INET_LOCAL_IN,
964ddaa1
PM
218 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
219 },
9fb9cbb1
YK
220};
221
a999e683
PM
222#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
223static int log_invalid_proto_min = 0;
224static int log_invalid_proto_max = 255;
225
226static ctl_table ip_ct_sysctl_table[] = {
227 {
a999e683 228 .procname = "ip_conntrack_max",
a999e683
PM
229 .maxlen = sizeof(int),
230 .mode = 0644,
6d9f239a 231 .proc_handler = proc_dointvec,
a999e683
PM
232 },
233 {
a999e683 234 .procname = "ip_conntrack_count",
a999e683
PM
235 .maxlen = sizeof(int),
236 .mode = 0444,
6d9f239a 237 .proc_handler = proc_dointvec,
a999e683
PM
238 },
239 {
a999e683 240 .procname = "ip_conntrack_buckets",
a999e683
PM
241 .maxlen = sizeof(unsigned int),
242 .mode = 0444,
6d9f239a 243 .proc_handler = proc_dointvec,
a999e683
PM
244 },
245 {
a999e683 246 .procname = "ip_conntrack_checksum",
a999e683
PM
247 .maxlen = sizeof(int),
248 .mode = 0644,
6d9f239a 249 .proc_handler = proc_dointvec,
a999e683
PM
250 },
251 {
a999e683 252 .procname = "ip_conntrack_log_invalid",
a999e683
PM
253 .maxlen = sizeof(unsigned int),
254 .mode = 0644,
6d9f239a 255 .proc_handler = proc_dointvec_minmax,
a999e683
PM
256 .extra1 = &log_invalid_proto_min,
257 .extra2 = &log_invalid_proto_max,
258 },
f8572d8f 259 { }
a999e683
PM
260};
261#endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
262
9fb9cbb1
YK
263/* Fast function for those who don't want to parse /proc (and I don't
264 blame them). */
265/* Reversing the socket's dst/src point of view gives us the reply
266 mapping. */
267static int
268getorigdst(struct sock *sk, int optval, void __user *user, int *len)
269{
32948588
JE
270 const struct inet_sock *inet = inet_sk(sk);
271 const struct nf_conntrack_tuple_hash *h;
9fb9cbb1 272 struct nf_conntrack_tuple tuple;
e905a9ed 273
443a70d5 274 memset(&tuple, 0, sizeof(tuple));
c720c7e8
ED
275 tuple.src.u3.ip = inet->inet_rcv_saddr;
276 tuple.src.u.tcp.port = inet->inet_sport;
277 tuple.dst.u3.ip = inet->inet_daddr;
278 tuple.dst.u.tcp.port = inet->inet_dport;
9fb9cbb1 279 tuple.src.l3num = PF_INET;
54981279 280 tuple.dst.protonum = sk->sk_protocol;
9fb9cbb1 281
54981279
RL
282 /* We only do TCP and SCTP at the moment: is there a better way? */
283 if (sk->sk_protocol != IPPROTO_TCP && sk->sk_protocol != IPPROTO_SCTP) {
284 pr_debug("SO_ORIGINAL_DST: Not a TCP/SCTP socket\n");
9fb9cbb1
YK
285 return -ENOPROTOOPT;
286 }
287
288 if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
0d53778e
PM
289 pr_debug("SO_ORIGINAL_DST: len %d not %Zu\n",
290 *len, sizeof(struct sockaddr_in));
9fb9cbb1
YK
291 return -EINVAL;
292 }
293
5d0aa2cc 294 h = nf_conntrack_find_get(sock_net(sk), NF_CT_DEFAULT_ZONE, &tuple);
9fb9cbb1
YK
295 if (h) {
296 struct sockaddr_in sin;
297 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
298
299 sin.sin_family = AF_INET;
300 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
301 .tuple.dst.u.tcp.port;
302 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
303 .tuple.dst.u3.ip;
6c813c3f 304 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
9fb9cbb1 305
cffee385
HH
306 pr_debug("SO_ORIGINAL_DST: %pI4 %u\n",
307 &sin.sin_addr.s_addr, ntohs(sin.sin_port));
9fb9cbb1
YK
308 nf_ct_put(ct);
309 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
310 return -EFAULT;
311 else
312 return 0;
313 }
cffee385
HH
314 pr_debug("SO_ORIGINAL_DST: Can't find %pI4/%u-%pI4/%u.\n",
315 &tuple.src.u3.ip, ntohs(tuple.src.u.tcp.port),
316 &tuple.dst.u3.ip, ntohs(tuple.dst.u.tcp.port));
9fb9cbb1
YK
317 return -ENOENT;
318}
319
e281db5c 320#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c1d10adb
PNA
321
322#include <linux/netfilter/nfnetlink.h>
323#include <linux/netfilter/nfnetlink_conntrack.h>
324
fdf70832 325static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
c1d10adb
PNA
326 const struct nf_conntrack_tuple *tuple)
327{
d317e4f6
DM
328 if (nla_put_be32(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) ||
329 nla_put_be32(skb, CTA_IP_V4_DST, tuple->dst.u3.ip))
330 goto nla_put_failure;
c1d10adb
PNA
331 return 0;
332
df6fb868 333nla_put_failure:
c1d10adb
PNA
334 return -1;
335}
336
f73e924c
PM
337static const struct nla_policy ipv4_nla_policy[CTA_IP_MAX+1] = {
338 [CTA_IP_V4_SRC] = { .type = NLA_U32 },
339 [CTA_IP_V4_DST] = { .type = NLA_U32 },
c1d10adb
PNA
340};
341
fdf70832 342static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
c1d10adb
PNA
343 struct nf_conntrack_tuple *t)
344{
df6fb868 345 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
c1d10adb
PNA
346 return -EINVAL;
347
77236b6e
PM
348 t->src.u3.ip = nla_get_be32(tb[CTA_IP_V4_SRC]);
349 t->dst.u3.ip = nla_get_be32(tb[CTA_IP_V4_DST]);
c1d10adb
PNA
350
351 return 0;
352}
a400c30e
HE
353
354static int ipv4_nlattr_tuple_size(void)
355{
356 return nla_policy_len(ipv4_nla_policy, CTA_IP_MAX + 1);
357}
c1d10adb
PNA
358#endif
359
9fb9cbb1
YK
360static struct nf_sockopt_ops so_getorigdst = {
361 .pf = PF_INET,
362 .get_optmin = SO_ORIGINAL_DST,
363 .get_optmax = SO_ORIGINAL_DST+1,
364 .get = &getorigdst,
16fcec35 365 .owner = THIS_MODULE,
9fb9cbb1
YK
366};
367
3ea04dd3
G
368static int ipv4_init_net(struct net *net)
369{
370#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
371 struct nf_ip_net *in = &net->ct.nf_ct_proto;
372 in->ctl_table = kmemdup(ip_ct_sysctl_table,
373 sizeof(ip_ct_sysctl_table),
374 GFP_KERNEL);
375 if (!in->ctl_table)
376 return -ENOMEM;
377
378 in->ctl_table[0].data = &nf_conntrack_max;
379 in->ctl_table[1].data = &net->ct.count;
380 in->ctl_table[2].data = &net->ct.htable_size;
381 in->ctl_table[3].data = &net->ct.sysctl_checksum;
382 in->ctl_table[4].data = &net->ct.sysctl_log_invalid;
383#endif
384 return 0;
385}
386
61075af5 387struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 __read_mostly = {
9fb9cbb1
YK
388 .l3proto = PF_INET,
389 .name = "ipv4",
390 .pkt_to_tuple = ipv4_pkt_to_tuple,
391 .invert_tuple = ipv4_invert_tuple,
392 .print_tuple = ipv4_print_tuple,
ffc30690 393 .get_l4proto = ipv4_get_l4proto,
e281db5c 394#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
fdf70832 395 .tuple_to_nlattr = ipv4_tuple_to_nlattr,
a400c30e 396 .nlattr_tuple_size = ipv4_nlattr_tuple_size,
fdf70832 397 .nlattr_to_tuple = ipv4_nlattr_to_tuple,
f73e924c 398 .nla_policy = ipv4_nla_policy,
a999e683
PM
399#endif
400#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
f99e8f71 401 .ctl_table_path = "net/ipv4/netfilter",
c1d10adb 402#endif
3ea04dd3 403 .init_net = ipv4_init_net,
9fb9cbb1
YK
404 .me = THIS_MODULE,
405};
406
fae718dd
PM
407module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
408 &nf_conntrack_htable_size, 0600);
409
32292a7f 410MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
d2483dde 411MODULE_ALIAS("ip_conntrack");
32292a7f
PM
412MODULE_LICENSE("GPL");
413
3ea04dd3
G
414static int ipv4_net_init(struct net *net)
415{
416 int ret = 0;
417
c296bb4d 418 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_tcp4);
3ea04dd3 419 if (ret < 0) {
c296bb4d 420 pr_err("nf_conntrack_tcp4: pernet registration failed\n");
3ea04dd3
G
421 goto out_tcp;
422 }
c296bb4d 423 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udp4);
3ea04dd3 424 if (ret < 0) {
c296bb4d 425 pr_err("nf_conntrack_udp4: pernet registration failed\n");
3ea04dd3
G
426 goto out_udp;
427 }
c296bb4d 428 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_icmp);
3ea04dd3 429 if (ret < 0) {
c296bb4d 430 pr_err("nf_conntrack_icmp4: pernet registration failed\n");
3ea04dd3
G
431 goto out_icmp;
432 }
6330750d 433 ret = nf_ct_l3proto_pernet_register(net, &nf_conntrack_l3proto_ipv4);
3ea04dd3 434 if (ret < 0) {
6330750d 435 pr_err("nf_conntrack_ipv4: pernet registration failed\n");
3ea04dd3
G
436 goto out_ipv4;
437 }
438 return 0;
439out_ipv4:
c296bb4d 440 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmp);
3ea04dd3 441out_icmp:
c296bb4d 442 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp4);
3ea04dd3 443out_udp:
c296bb4d 444 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp4);
3ea04dd3
G
445out_tcp:
446 return ret;
447}
448
449static void ipv4_net_exit(struct net *net)
450{
6330750d 451 nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv4);
c296bb4d
G
452 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmp);
453 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp4);
454 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp4);
3ea04dd3
G
455}
456
457static struct pernet_operations ipv4_net_ops = {
458 .init = ipv4_net_init,
459 .exit = ipv4_net_exit,
460};
461
32292a7f 462static int __init nf_conntrack_l3proto_ipv4_init(void)
9fb9cbb1
YK
463{
464 int ret = 0;
465
32292a7f 466 need_conntrack();
73e4022f 467 nf_defrag_ipv4_enable();
9fb9cbb1
YK
468
469 ret = nf_register_sockopt(&so_getorigdst);
470 if (ret < 0) {
471 printk(KERN_ERR "Unable to register netfilter socket option\n");
32292a7f 472 return ret;
9fb9cbb1
YK
473 }
474
3ea04dd3 475 ret = register_pernet_subsys(&ipv4_net_ops);
9fb9cbb1 476 if (ret < 0) {
3ea04dd3 477 pr_err("nf_conntrack_ipv4: can't register pernet ops\n");
9fb9cbb1
YK
478 goto cleanup_sockopt;
479 }
480
964ddaa1
PM
481 ret = nf_register_hooks(ipv4_conntrack_ops,
482 ARRAY_SIZE(ipv4_conntrack_ops));
9fb9cbb1 483 if (ret < 0) {
654d0fbd 484 pr_err("nf_conntrack_ipv4: can't register hooks.\n");
3ea04dd3 485 goto cleanup_pernet;
9fb9cbb1 486 }
6330750d 487
c296bb4d
G
488 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_tcp4);
489 if (ret < 0) {
490 pr_err("nf_conntrack_ipv4: can't register tcp4 proto.\n");
491 goto cleanup_hooks;
492 }
493
494 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udp4);
495 if (ret < 0) {
496 pr_err("nf_conntrack_ipv4: can't register udp4 proto.\n");
497 goto cleanup_tcp4;
498 }
499
500 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_icmp);
501 if (ret < 0) {
502 pr_err("nf_conntrack_ipv4: can't register icmpv4 proto.\n");
503 goto cleanup_udp4;
504 }
505
6330750d
G
506 ret = nf_ct_l3proto_register(&nf_conntrack_l3proto_ipv4);
507 if (ret < 0) {
508 pr_err("nf_conntrack_ipv4: can't register ipv4 proto.\n");
c296bb4d 509 goto cleanup_icmpv4;
6330750d
G
510 }
511
e4bd8bce
PM
512#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
513 ret = nf_conntrack_ipv4_compat_init();
514 if (ret < 0)
6330750d 515 goto cleanup_proto;
e4bd8bce 516#endif
9fb9cbb1 517 return ret;
e4bd8bce 518#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
6330750d
G
519 cleanup_proto:
520 nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
521#endif
c296bb4d
G
522 cleanup_icmpv4:
523 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmp);
524 cleanup_udp4:
525 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp4);
526 cleanup_tcp4:
527 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
e4bd8bce 528 cleanup_hooks:
e905a9ed 529 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
3ea04dd3
G
530 cleanup_pernet:
531 unregister_pernet_subsys(&ipv4_net_ops);
9fb9cbb1
YK
532 cleanup_sockopt:
533 nf_unregister_sockopt(&so_getorigdst);
9fb9cbb1
YK
534 return ret;
535}
536
65b4b4e8 537static void __exit nf_conntrack_l3proto_ipv4_fini(void)
9fb9cbb1 538{
32292a7f 539 synchronize_net();
e4bd8bce
PM
540#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
541 nf_conntrack_ipv4_compat_fini();
542#endif
6330750d 543 nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
c296bb4d
G
544 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmp);
545 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp4);
546 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
32292a7f 547 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
3ea04dd3 548 unregister_pernet_subsys(&ipv4_net_ops);
32292a7f 549 nf_unregister_sockopt(&so_getorigdst);
9fb9cbb1
YK
550}
551
65b4b4e8
AM
552module_init(nf_conntrack_l3proto_ipv4_init);
553module_exit(nf_conntrack_l3proto_ipv4_fini);
591e6206
PM
554
555void need_ipv4_conntrack(void)
556{
557 return;
558}
559EXPORT_SYMBOL_GPL(need_ipv4_conntrack);