1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
9 #include <linux/types.h>
11 #include <linux/netfilter.h>
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/icmp.h>
15 #include <linux/sysctl.h>
16 #include <net/route.h>
19 #include <linux/netfilter_ipv4.h>
20 #include <net/netfilter/nf_conntrack.h>
21 #include <net/netfilter/nf_conntrack_helper.h>
22 #include <net/netfilter/nf_conntrack_l4proto.h>
23 #include <net/netfilter/nf_conntrack_l3proto.h>
24 #include <net/netfilter/nf_conntrack_core.h>
25 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
26 #include <net/netfilter/nf_nat_helper.h>
28 int (*nf_nat_seq_adjust_hook)(struct sk_buff *skb,
30 enum ip_conntrack_info ctinfo);
31 EXPORT_SYMBOL_GPL(nf_nat_seq_adjust_hook);
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=%u.%u.%u.%u dst=%u.%u.%u.%u ",
62 NIPQUAD(tuple->src.u3.ip),
63 NIPQUAD(tuple->dst.u3.ip));
66 /* Returns new sk_buff, or NULL */
67 static int nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user)
74 err = ip_defrag(skb, user);
78 ip_send_check(ip_hdr(skb));
83 static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
84 unsigned int *dataoff, u_int8_t *protonum)
86 const struct iphdr *iph;
89 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
93 /* Conntrack defragments packets, we might still see fragments
94 * inside ICMP packets though. */
95 if (iph->frag_off & htons(IP_OFFSET))
98 *dataoff = nhoff + (iph->ihl << 2);
99 *protonum = iph->protocol;
104 static unsigned int ipv4_confirm(unsigned int hooknum,
106 const struct net_device *in,
107 const struct net_device *out,
108 int (*okfn)(struct sk_buff *))
111 enum ip_conntrack_info ctinfo;
112 const struct nf_conn_help *help;
113 const struct nf_conntrack_helper *helper;
116 /* This is where we call the helper: as the packet goes out. */
117 ct = nf_ct_get(skb, &ctinfo);
118 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
121 help = nfct_help(ct);
125 /* rcu_read_lock()ed by nf_hook_slow */
126 helper = rcu_dereference(help->helper);
130 ret = helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb),
132 if (ret != NF_ACCEPT)
135 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
136 typeof(nf_nat_seq_adjust_hook) seq_adjust;
138 seq_adjust = rcu_dereference(nf_nat_seq_adjust_hook);
139 if (!seq_adjust || !seq_adjust(skb, ct, ctinfo))
143 /* We've seen it coming out the other side: confirm it */
144 return nf_conntrack_confirm(skb);
147 static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
149 const struct net_device *in,
150 const struct net_device *out,
151 int (*okfn)(struct sk_buff *))
153 /* Previously seen (loopback)? Ignore. Do this before
158 /* Gather fragments. */
159 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
160 if (nf_ct_ipv4_gather_frags(skb,
161 hooknum == NF_INET_PRE_ROUTING ?
162 IP_DEFRAG_CONNTRACK_IN :
163 IP_DEFRAG_CONNTRACK_OUT))
169 static unsigned int ipv4_conntrack_in(unsigned int hooknum,
171 const struct net_device *in,
172 const struct net_device *out,
173 int (*okfn)(struct sk_buff *))
175 return nf_conntrack_in(dev_net(in), PF_INET, hooknum, skb);
178 static unsigned int ipv4_conntrack_local(unsigned int hooknum,
180 const struct net_device *in,
181 const struct net_device *out,
182 int (*okfn)(struct sk_buff *))
184 /* root is playing with raw sockets. */
185 if (skb->len < sizeof(struct iphdr) ||
186 ip_hdrlen(skb) < sizeof(struct iphdr)) {
188 printk("ipt_hook: happy cracking.\n");
191 return nf_conntrack_in(dev_net(out), PF_INET, hooknum, skb);
194 /* Connection tracking may drop packets, but never alters them, so
195 make it the first hook. */
196 static struct nf_hook_ops ipv4_conntrack_ops[] __read_mostly = {
198 .hook = ipv4_conntrack_defrag,
199 .owner = THIS_MODULE,
201 .hooknum = NF_INET_PRE_ROUTING,
202 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
205 .hook = ipv4_conntrack_in,
206 .owner = THIS_MODULE,
208 .hooknum = NF_INET_PRE_ROUTING,
209 .priority = NF_IP_PRI_CONNTRACK,
212 .hook = ipv4_conntrack_defrag,
213 .owner = THIS_MODULE,
215 .hooknum = NF_INET_LOCAL_OUT,
216 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
219 .hook = ipv4_conntrack_local,
220 .owner = THIS_MODULE,
222 .hooknum = NF_INET_LOCAL_OUT,
223 .priority = NF_IP_PRI_CONNTRACK,
226 .hook = ipv4_confirm,
227 .owner = THIS_MODULE,
229 .hooknum = NF_INET_POST_ROUTING,
230 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
233 .hook = ipv4_confirm,
234 .owner = THIS_MODULE,
236 .hooknum = NF_INET_LOCAL_IN,
237 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
241 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
242 static int log_invalid_proto_min = 0;
243 static int log_invalid_proto_max = 255;
245 static ctl_table ip_ct_sysctl_table[] = {
247 .ctl_name = NET_IPV4_NF_CONNTRACK_MAX,
248 .procname = "ip_conntrack_max",
249 .data = &nf_conntrack_max,
250 .maxlen = sizeof(int),
252 .proc_handler = &proc_dointvec,
255 .ctl_name = NET_IPV4_NF_CONNTRACK_COUNT,
256 .procname = "ip_conntrack_count",
257 .data = &init_net.ct.count,
258 .maxlen = sizeof(int),
260 .proc_handler = &proc_dointvec,
263 .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS,
264 .procname = "ip_conntrack_buckets",
265 .data = &nf_conntrack_htable_size,
266 .maxlen = sizeof(unsigned int),
268 .proc_handler = &proc_dointvec,
271 .ctl_name = NET_IPV4_NF_CONNTRACK_CHECKSUM,
272 .procname = "ip_conntrack_checksum",
273 .data = &init_net.ct.sysctl_checksum,
274 .maxlen = sizeof(int),
276 .proc_handler = &proc_dointvec,
279 .ctl_name = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
280 .procname = "ip_conntrack_log_invalid",
281 .data = &nf_ct_log_invalid,
282 .maxlen = sizeof(unsigned int),
284 .proc_handler = &proc_dointvec_minmax,
285 .strategy = &sysctl_intvec,
286 .extra1 = &log_invalid_proto_min,
287 .extra2 = &log_invalid_proto_max,
293 #endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
295 /* Fast function for those who don't want to parse /proc (and I don't
297 /* Reversing the socket's dst/src point of view gives us the reply
300 getorigdst(struct sock *sk, int optval, void __user *user, int *len)
302 const struct inet_sock *inet = inet_sk(sk);
303 const struct nf_conntrack_tuple_hash *h;
304 struct nf_conntrack_tuple tuple;
306 memset(&tuple, 0, sizeof(tuple));
307 tuple.src.u3.ip = inet->rcv_saddr;
308 tuple.src.u.tcp.port = inet->sport;
309 tuple.dst.u3.ip = inet->daddr;
310 tuple.dst.u.tcp.port = inet->dport;
311 tuple.src.l3num = PF_INET;
312 tuple.dst.protonum = IPPROTO_TCP;
314 /* We only do TCP at the moment: is there a better way? */
315 if (strcmp(sk->sk_prot->name, "TCP")) {
316 pr_debug("SO_ORIGINAL_DST: Not a TCP socket\n");
320 if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
321 pr_debug("SO_ORIGINAL_DST: len %d not %Zu\n",
322 *len, sizeof(struct sockaddr_in));
326 h = nf_conntrack_find_get(sock_net(sk), &tuple);
328 struct sockaddr_in sin;
329 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
331 sin.sin_family = AF_INET;
332 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
333 .tuple.dst.u.tcp.port;
334 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
336 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
338 pr_debug("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
339 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
341 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
346 pr_debug("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
347 NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port),
348 NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port));
352 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
354 #include <linux/netfilter/nfnetlink.h>
355 #include <linux/netfilter/nfnetlink_conntrack.h>
357 static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
358 const struct nf_conntrack_tuple *tuple)
360 NLA_PUT_BE32(skb, CTA_IP_V4_SRC, tuple->src.u3.ip);
361 NLA_PUT_BE32(skb, CTA_IP_V4_DST, tuple->dst.u3.ip);
368 static const struct nla_policy ipv4_nla_policy[CTA_IP_MAX+1] = {
369 [CTA_IP_V4_SRC] = { .type = NLA_U32 },
370 [CTA_IP_V4_DST] = { .type = NLA_U32 },
373 static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
374 struct nf_conntrack_tuple *t)
376 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
379 t->src.u3.ip = nla_get_be32(tb[CTA_IP_V4_SRC]);
380 t->dst.u3.ip = nla_get_be32(tb[CTA_IP_V4_DST]);
386 static struct nf_sockopt_ops so_getorigdst = {
388 .get_optmin = SO_ORIGINAL_DST,
389 .get_optmax = SO_ORIGINAL_DST+1,
391 .owner = THIS_MODULE,
394 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 __read_mostly = {
397 .pkt_to_tuple = ipv4_pkt_to_tuple,
398 .invert_tuple = ipv4_invert_tuple,
399 .print_tuple = ipv4_print_tuple,
400 .get_l4proto = ipv4_get_l4proto,
401 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
402 .tuple_to_nlattr = ipv4_tuple_to_nlattr,
403 .nlattr_to_tuple = ipv4_nlattr_to_tuple,
404 .nla_policy = ipv4_nla_policy,
406 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
407 .ctl_table_path = nf_net_ipv4_netfilter_sysctl_path,
408 .ctl_table = ip_ct_sysctl_table,
413 module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
414 &nf_conntrack_htable_size, 0600);
416 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
417 MODULE_ALIAS("ip_conntrack");
418 MODULE_LICENSE("GPL");
420 static int __init nf_conntrack_l3proto_ipv4_init(void)
426 ret = nf_register_sockopt(&so_getorigdst);
428 printk(KERN_ERR "Unable to register netfilter socket option\n");
432 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4);
434 printk("nf_conntrack_ipv4: can't register tcp.\n");
435 goto cleanup_sockopt;
438 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4);
440 printk("nf_conntrack_ipv4: can't register udp.\n");
444 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp);
446 printk("nf_conntrack_ipv4: can't register icmp.\n");
450 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4);
452 printk("nf_conntrack_ipv4: can't register ipv4\n");
456 ret = nf_register_hooks(ipv4_conntrack_ops,
457 ARRAY_SIZE(ipv4_conntrack_ops));
459 printk("nf_conntrack_ipv4: can't register hooks.\n");
462 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
463 ret = nf_conntrack_ipv4_compat_init();
468 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
470 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
473 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
475 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
477 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
479 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
481 nf_unregister_sockopt(&so_getorigdst);
485 static void __exit nf_conntrack_l3proto_ipv4_fini(void)
488 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
489 nf_conntrack_ipv4_compat_fini();
491 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
492 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
493 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
494 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
495 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
496 nf_unregister_sockopt(&so_getorigdst);
499 module_init(nf_conntrack_l3proto_ipv4_init);
500 module_exit(nf_conntrack_l3proto_ipv4_fini);
502 void need_ipv4_conntrack(void)
506 EXPORT_SYMBOL_GPL(need_ipv4_conntrack);