2 /* (C) 1999-2001 Paul `Rusty' Russell
3 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
4 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
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.
11 #include <linux/types.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>
18 #include <net/route.h>
21 #include <linux/netfilter_ipv4.h>
22 #include <net/netfilter/nf_conntrack.h>
23 #include <net/netfilter/nf_conntrack_helper.h>
24 #include <net/netfilter/nf_conntrack_l4proto.h>
25 #include <net/netfilter/nf_conntrack_l3proto.h>
26 #include <net/netfilter/nf_conntrack_zones.h>
27 #include <net/netfilter/nf_conntrack_core.h>
28 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
29 #include <net/netfilter/nf_nat_helper.h>
30 #include <net/netfilter/ipv4/nf_defrag_ipv4.h>
31 #include <net/netfilter/nf_log.h>
33 static bool ipv4_pkt_to_tuple(const struct sk_buff
*skb
, unsigned int nhoff
,
34 struct nf_conntrack_tuple
*tuple
)
38 ap
= skb_header_pointer(skb
, nhoff
+ offsetof(struct iphdr
, saddr
),
39 sizeof(u_int32_t
) * 2, _addrs
);
43 tuple
->src
.u3
.ip
= ap
[0];
44 tuple
->dst
.u3
.ip
= ap
[1];
49 static bool ipv4_invert_tuple(struct nf_conntrack_tuple
*tuple
,
50 const struct nf_conntrack_tuple
*orig
)
52 tuple
->src
.u3
.ip
= orig
->dst
.u3
.ip
;
53 tuple
->dst
.u3
.ip
= orig
->src
.u3
.ip
;
58 static int ipv4_print_tuple(struct seq_file
*s
,
59 const struct nf_conntrack_tuple
*tuple
)
61 return seq_printf(s
, "src=%pI4 dst=%pI4 ",
62 &tuple
->src
.u3
.ip
, &tuple
->dst
.u3
.ip
);
65 static int ipv4_get_l4proto(const struct sk_buff
*skb
, unsigned int nhoff
,
66 unsigned int *dataoff
, u_int8_t
*protonum
)
68 const struct iphdr
*iph
;
71 iph
= skb_header_pointer(skb
, nhoff
, sizeof(_iph
), &_iph
);
75 /* Conntrack defragments packets, we might still see fragments
76 * inside ICMP packets though. */
77 if (iph
->frag_off
& htons(IP_OFFSET
))
80 *dataoff
= nhoff
+ (iph
->ihl
<< 2);
81 *protonum
= iph
->protocol
;
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
);
94 static unsigned int ipv4_helper(unsigned int hooknum
,
96 const struct net_device
*in
,
97 const struct net_device
*out
,
98 int (*okfn
)(struct sk_buff
*))
101 enum ip_conntrack_info ctinfo
;
102 const struct nf_conn_help
*help
;
103 const struct nf_conntrack_helper
*helper
;
105 /* This is where we call the helper: as the packet goes out. */
106 ct
= nf_ct_get(skb
, &ctinfo
);
107 if (!ct
|| ctinfo
== IP_CT_RELATED_REPLY
)
110 help
= nfct_help(ct
);
114 /* rcu_read_lock()ed by nf_hook_slow */
115 helper
= rcu_dereference(help
->helper
);
119 return helper
->help(skb
, skb_network_offset(skb
) + ip_hdrlen(skb
),
123 static unsigned int ipv4_confirm(unsigned int hooknum
,
125 const struct net_device
*in
,
126 const struct net_device
*out
,
127 int (*okfn
)(struct sk_buff
*))
130 enum ip_conntrack_info ctinfo
;
132 ct
= nf_ct_get(skb
, &ctinfo
);
133 if (!ct
|| ctinfo
== IP_CT_RELATED_REPLY
)
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
)) {
139 typeof(nf_nat_seq_adjust_hook
) seq_adjust
;
141 seq_adjust
= rcu_dereference(nf_nat_seq_adjust_hook
);
143 !seq_adjust(skb
, ct
, ctinfo
, ip_hdrlen(skb
))) {
144 NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct
), drop
);
149 /* We've seen it coming out the other side: confirm it */
150 return nf_conntrack_confirm(skb
);
153 static unsigned int ipv4_conntrack_in(unsigned int hooknum
,
155 const struct net_device
*in
,
156 const struct net_device
*out
,
157 int (*okfn
)(struct sk_buff
*))
159 return nf_conntrack_in(dev_net(in
), PF_INET
, hooknum
, skb
);
162 static unsigned int ipv4_conntrack_local(unsigned int hooknum
,
164 const struct net_device
*in
,
165 const struct net_device
*out
,
166 int (*okfn
)(struct sk_buff
*))
168 /* root is playing with raw sockets. */
169 if (skb
->len
< sizeof(struct iphdr
) ||
170 ip_hdrlen(skb
) < sizeof(struct iphdr
))
172 return nf_conntrack_in(dev_net(out
), PF_INET
, hooknum
, skb
);
175 /* Connection tracking may drop packets, but never alters them, so
176 make it the first hook. */
177 static struct nf_hook_ops ipv4_conntrack_ops
[] __read_mostly
= {
179 .hook
= ipv4_conntrack_in
,
180 .owner
= THIS_MODULE
,
182 .hooknum
= NF_INET_PRE_ROUTING
,
183 .priority
= NF_IP_PRI_CONNTRACK
,
186 .hook
= ipv4_conntrack_local
,
187 .owner
= THIS_MODULE
,
189 .hooknum
= NF_INET_LOCAL_OUT
,
190 .priority
= NF_IP_PRI_CONNTRACK
,
194 .owner
= THIS_MODULE
,
196 .hooknum
= NF_INET_POST_ROUTING
,
197 .priority
= NF_IP_PRI_CONNTRACK_HELPER
,
200 .hook
= ipv4_confirm
,
201 .owner
= THIS_MODULE
,
203 .hooknum
= NF_INET_POST_ROUTING
,
204 .priority
= NF_IP_PRI_CONNTRACK_CONFIRM
,
208 .owner
= THIS_MODULE
,
210 .hooknum
= NF_INET_LOCAL_IN
,
211 .priority
= NF_IP_PRI_CONNTRACK_HELPER
,
214 .hook
= ipv4_confirm
,
215 .owner
= THIS_MODULE
,
217 .hooknum
= NF_INET_LOCAL_IN
,
218 .priority
= NF_IP_PRI_CONNTRACK_CONFIRM
,
222 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
223 static int log_invalid_proto_min
= 0;
224 static int log_invalid_proto_max
= 255;
226 static struct ctl_table ip_ct_sysctl_table
[] = {
228 .procname
= "ip_conntrack_max",
229 .maxlen
= sizeof(int),
231 .proc_handler
= proc_dointvec
,
234 .procname
= "ip_conntrack_count",
235 .maxlen
= sizeof(int),
237 .proc_handler
= proc_dointvec
,
240 .procname
= "ip_conntrack_buckets",
241 .maxlen
= sizeof(unsigned int),
243 .proc_handler
= proc_dointvec
,
246 .procname
= "ip_conntrack_checksum",
247 .maxlen
= sizeof(int),
249 .proc_handler
= proc_dointvec
,
252 .procname
= "ip_conntrack_log_invalid",
253 .maxlen
= sizeof(unsigned int),
255 .proc_handler
= proc_dointvec_minmax
,
256 .extra1
= &log_invalid_proto_min
,
257 .extra2
= &log_invalid_proto_max
,
261 #endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
263 /* Fast function for those who don't want to parse /proc (and I don't
265 /* Reversing the socket's dst/src point of view gives us the reply
268 getorigdst(struct sock
*sk
, int optval
, void __user
*user
, int *len
)
270 const struct inet_sock
*inet
= inet_sk(sk
);
271 const struct nf_conntrack_tuple_hash
*h
;
272 struct nf_conntrack_tuple tuple
;
274 memset(&tuple
, 0, sizeof(tuple
));
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
;
279 tuple
.src
.l3num
= PF_INET
;
280 tuple
.dst
.protonum
= sk
->sk_protocol
;
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");
288 if ((unsigned int) *len
< sizeof(struct sockaddr_in
)) {
289 pr_debug("SO_ORIGINAL_DST: len %d not %Zu\n",
290 *len
, sizeof(struct sockaddr_in
));
294 h
= nf_conntrack_find_get(sock_net(sk
), NF_CT_DEFAULT_ZONE
, &tuple
);
296 struct sockaddr_in sin
;
297 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
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
]
304 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
306 pr_debug("SO_ORIGINAL_DST: %pI4 %u\n",
307 &sin
.sin_addr
.s_addr
, ntohs(sin
.sin_port
));
309 if (copy_to_user(user
, &sin
, sizeof(sin
)) != 0)
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
));
320 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
322 #include <linux/netfilter/nfnetlink.h>
323 #include <linux/netfilter/nfnetlink_conntrack.h>
325 static int ipv4_tuple_to_nlattr(struct sk_buff
*skb
,
326 const struct nf_conntrack_tuple
*tuple
)
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
;
337 static 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
},
342 static int ipv4_nlattr_to_tuple(struct nlattr
*tb
[],
343 struct nf_conntrack_tuple
*t
)
345 if (!tb
[CTA_IP_V4_SRC
] || !tb
[CTA_IP_V4_DST
])
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
]);
354 static int ipv4_nlattr_tuple_size(void)
356 return nla_policy_len(ipv4_nla_policy
, CTA_IP_MAX
+ 1);
360 static struct nf_sockopt_ops so_getorigdst
= {
362 .get_optmin
= SO_ORIGINAL_DST
,
363 .get_optmax
= SO_ORIGINAL_DST
+1,
365 .owner
= THIS_MODULE
,
368 static int ipv4_init_net(struct net
*net
)
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
),
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
;
387 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 __read_mostly
= {
390 .pkt_to_tuple
= ipv4_pkt_to_tuple
,
391 .invert_tuple
= ipv4_invert_tuple
,
392 .print_tuple
= ipv4_print_tuple
,
393 .get_l4proto
= ipv4_get_l4proto
,
394 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
395 .tuple_to_nlattr
= ipv4_tuple_to_nlattr
,
396 .nlattr_tuple_size
= ipv4_nlattr_tuple_size
,
397 .nlattr_to_tuple
= ipv4_nlattr_to_tuple
,
398 .nla_policy
= ipv4_nla_policy
,
400 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
401 .ctl_table_path
= "net/ipv4/netfilter",
403 .init_net
= ipv4_init_net
,
407 module_param_call(hashsize
, nf_conntrack_set_hashsize
, param_get_uint
,
408 &nf_conntrack_htable_size
, 0600);
410 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET
));
411 MODULE_ALIAS("ip_conntrack");
412 MODULE_LICENSE("GPL");
414 static int ipv4_net_init(struct net
*net
)
418 ret
= nf_ct_l4proto_pernet_register(net
, &nf_conntrack_l4proto_tcp4
);
420 pr_err("nf_conntrack_tcp4: pernet registration failed\n");
423 ret
= nf_ct_l4proto_pernet_register(net
, &nf_conntrack_l4proto_udp4
);
425 pr_err("nf_conntrack_udp4: pernet registration failed\n");
428 ret
= nf_ct_l4proto_pernet_register(net
, &nf_conntrack_l4proto_icmp
);
430 pr_err("nf_conntrack_icmp4: pernet registration failed\n");
433 ret
= nf_ct_l3proto_pernet_register(net
, &nf_conntrack_l3proto_ipv4
);
435 pr_err("nf_conntrack_ipv4: pernet registration failed\n");
440 nf_ct_l4proto_pernet_unregister(net
, &nf_conntrack_l4proto_icmp
);
442 nf_ct_l4proto_pernet_unregister(net
, &nf_conntrack_l4proto_udp4
);
444 nf_ct_l4proto_pernet_unregister(net
, &nf_conntrack_l4proto_tcp4
);
449 static void ipv4_net_exit(struct net
*net
)
451 nf_ct_l3proto_pernet_unregister(net
, &nf_conntrack_l3proto_ipv4
);
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
);
457 static struct pernet_operations ipv4_net_ops
= {
458 .init
= ipv4_net_init
,
459 .exit
= ipv4_net_exit
,
462 static int __init
nf_conntrack_l3proto_ipv4_init(void)
467 nf_defrag_ipv4_enable();
469 ret
= nf_register_sockopt(&so_getorigdst
);
471 printk(KERN_ERR
"Unable to register netfilter socket option\n");
475 ret
= register_pernet_subsys(&ipv4_net_ops
);
477 pr_err("nf_conntrack_ipv4: can't register pernet ops\n");
478 goto cleanup_sockopt
;
481 ret
= nf_register_hooks(ipv4_conntrack_ops
,
482 ARRAY_SIZE(ipv4_conntrack_ops
));
484 pr_err("nf_conntrack_ipv4: can't register hooks.\n");
488 ret
= nf_ct_l4proto_register(&nf_conntrack_l4proto_tcp4
);
490 pr_err("nf_conntrack_ipv4: can't register tcp4 proto.\n");
494 ret
= nf_ct_l4proto_register(&nf_conntrack_l4proto_udp4
);
496 pr_err("nf_conntrack_ipv4: can't register udp4 proto.\n");
500 ret
= nf_ct_l4proto_register(&nf_conntrack_l4proto_icmp
);
502 pr_err("nf_conntrack_ipv4: can't register icmpv4 proto.\n");
506 ret
= nf_ct_l3proto_register(&nf_conntrack_l3proto_ipv4
);
508 pr_err("nf_conntrack_ipv4: can't register ipv4 proto.\n");
512 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
513 ret
= nf_conntrack_ipv4_compat_init();
518 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
520 nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv4
);
523 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmp
);
525 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp4
);
527 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp4
);
529 nf_unregister_hooks(ipv4_conntrack_ops
, ARRAY_SIZE(ipv4_conntrack_ops
));
531 unregister_pernet_subsys(&ipv4_net_ops
);
533 nf_unregister_sockopt(&so_getorigdst
);
537 static void __exit
nf_conntrack_l3proto_ipv4_fini(void)
540 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
541 nf_conntrack_ipv4_compat_fini();
543 nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv4
);
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
);
547 nf_unregister_hooks(ipv4_conntrack_ops
, ARRAY_SIZE(ipv4_conntrack_ops
));
548 unregister_pernet_subsys(&ipv4_net_ops
);
549 nf_unregister_sockopt(&so_getorigdst
);
552 module_init(nf_conntrack_l3proto_ipv4_init
);
553 module_exit(nf_conntrack_l3proto_ipv4_fini
);
555 void need_ipv4_conntrack(void)
559 EXPORT_SYMBOL_GPL(need_ipv4_conntrack
);