blob: ae1a71a97132bd5a5a49996e95c81e7c08b8c60e [file] [log] [blame]
KOVACS Krisztian73e40222008-10-08 11:35:12 +02001
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08002/* (C) 1999-2001 Paul `Rusty' Russell
3 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
Patrick McHardyf229f6c2013-04-06 15:24:29 +02004 * (C) 2006-2012 Patrick McHardy <kaber@trash.net>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08005 *
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.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08009 */
10
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080011#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>
Patrick McHardy0ae2cfe2006-01-05 12:21:52 -080018#include <net/route.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080019#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>
Martin Josefsson605dcad2006-11-29 02:35:06 +010024#include <net/netfilter/nf_conntrack_l4proto.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080025#include <net/netfilter/nf_conntrack_l3proto.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010026#include <net/netfilter/nf_conntrack_zones.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080027#include <net/netfilter/nf_conntrack_core.h>
Patrick McHardy41d73ec2013-08-27 08:50:12 +020028#include <net/netfilter/nf_conntrack_seqadj.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080029#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
Patrick McHardydd13b012008-04-14 11:15:52 +020030#include <net/netfilter/nf_nat_helper.h>
KOVACS Krisztian73e40222008-10-08 11:35:12 +020031#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
Patrick McHardy74f7a652009-08-25 15:33:08 +020032#include <net/netfilter/nf_log.h>
Patrick McHardydd13b012008-04-14 11:15:52 +020033
Jan Engelhardt8ce84392008-04-14 11:15:52 +020034static bool ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
35 struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080036{
Jan Engelhardt32948582008-01-31 04:53:24 -080037 const __be32 *ap;
38 __be32 _addrs[2];
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080039 ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr),
40 sizeof(u_int32_t) * 2, _addrs);
41 if (ap == NULL)
Jan Engelhardt8ce84392008-04-14 11:15:52 +020042 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080043
44 tuple->src.u3.ip = ap[0];
45 tuple->dst.u3.ip = ap[1];
46
Jan Engelhardt8ce84392008-04-14 11:15:52 +020047 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080048}
49
Jan Engelhardt8ce84392008-04-14 11:15:52 +020050static bool ipv4_invert_tuple(struct nf_conntrack_tuple *tuple,
51 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080052{
53 tuple->src.u3.ip = orig->dst.u3.ip;
54 tuple->dst.u3.ip = orig->src.u3.ip;
55
Jan Engelhardt8ce84392008-04-14 11:15:52 +020056 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080057}
58
Joe Perches824f1fb2014-09-29 16:08:22 -070059static void ipv4_print_tuple(struct seq_file *s,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080060 const struct nf_conntrack_tuple *tuple)
61{
Joe Perches824f1fb2014-09-29 16:08:22 -070062 seq_printf(s, "src=%pI4 dst=%pI4 ",
63 &tuple->src.u3.ip, &tuple->dst.u3.ip);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080064}
65
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070066static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
67 unsigned int *dataoff, u_int8_t *protonum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080068{
Jan Engelhardt32948582008-01-31 04:53:24 -080069 const struct iphdr *iph;
70 struct iphdr _iph;
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070071
72 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
73 if (iph == NULL)
Jozsef Kadlecsik8430eac2012-04-09 16:32:16 +020074 return -NF_ACCEPT;
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070075
Patrick McHardy0fb96702007-09-11 11:27:01 +020076 /* Conntrack defragments packets, we might still see fragments
77 * inside ICMP packets though. */
78 if (iph->frag_off & htons(IP_OFFSET))
Jozsef Kadlecsik8430eac2012-04-09 16:32:16 +020079 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080080
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -070081 *dataoff = nhoff + (iph->ihl << 2);
82 *protonum = iph->protocol;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080083
Jozsef Kadlecsik07153c62012-04-03 22:02:01 +020084 /* Check bogus IP headers */
85 if (*dataoff > skb->len) {
86 pr_debug("nf_conntrack_ipv4: bogus IPv4 packet: "
87 "nhoff %u, ihl %u, skblen %u\n",
88 nhoff, iph->ihl << 2, skb->len);
89 return -NF_ACCEPT;
90 }
91
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080092 return NF_ACCEPT;
93}
94
Eric W. Biederman06198b32015-09-18 14:33:06 -050095static unsigned int ipv4_helper(void *priv,
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +020096 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -040097 const struct nf_hook_state *state)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080098{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080099 struct nf_conn *ct;
100 enum ip_conntrack_info ctinfo;
Jan Engelhardt32948582008-01-31 04:53:24 -0800101 const struct nf_conn_help *help;
102 const struct nf_conntrack_helper *helper;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800103
104 /* This is where we call the helper: as the packet goes out. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700105 ct = nf_ct_get(skb, &ctinfo);
Eric Dumazetfb048832011-05-19 15:44:27 +0200106 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200107 return NF_ACCEPT;
Harald Weltedc808fe2006-03-20 17:56:32 -0800108
109 help = nfct_help(ct);
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700110 if (!help)
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200111 return NF_ACCEPT;
Patrick McHardydd13b012008-04-14 11:15:52 +0200112
Patrick McHarrdy3c158f72007-06-05 12:55:27 -0700113 /* rcu_read_lock()ed by nf_hook_slow */
114 helper = rcu_dereference(help->helper);
115 if (!helper)
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200116 return NF_ACCEPT;
Patrick McHardydd13b012008-04-14 11:15:52 +0200117
Pablo Neira Ayusob20ab9cc2013-02-10 18:56:56 +0100118 return helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb),
119 ct, ctinfo);
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200120}
121
Eric W. Biederman06198b32015-09-18 14:33:06 -0500122static unsigned int ipv4_confirm(void *priv,
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200123 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400124 const struct nf_hook_state *state)
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200125{
126 struct nf_conn *ct;
127 enum ip_conntrack_info ctinfo;
128
129 ct = nf_ct_get(skb, &ctinfo);
130 if (!ct || ctinfo == IP_CT_RELATED_REPLY)
131 goto out;
Patrick McHardydd13b012008-04-14 11:15:52 +0200132
Julian Anastasov42c1edd2011-06-16 17:29:22 +0200133 /* adjust seqs for loopback traffic only in outgoing direction */
134 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
135 !nf_is_loopback_packet(skb)) {
Patrick McHardy41d73ec2013-08-27 08:50:12 +0200136 if (!nf_ct_seq_adjust(skb, ct, ctinfo, ip_hdrlen(skb))) {
Pablo Neira Ayuso1db7a742009-03-16 15:18:50 +0100137 NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop);
Patrick McHardydd13b012008-04-14 11:15:52 +0200138 return NF_DROP;
Pablo Neira Ayuso1db7a742009-03-16 15:18:50 +0100139 }
Patrick McHardydd13b012008-04-14 11:15:52 +0200140 }
141out:
142 /* We've seen it coming out the other side: confirm it */
143 return nf_conntrack_confirm(skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800144}
145
Eric W. Biederman06198b32015-09-18 14:33:06 -0500146static unsigned int ipv4_conntrack_in(void *priv,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700147 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400148 const struct nf_hook_state *state)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800149{
Eric W. Biederman082a7582015-09-18 14:32:56 -0500150 return nf_conntrack_in(state->net, PF_INET, state->hook, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800151}
152
Eric W. Biederman06198b32015-09-18 14:33:06 -0500153static unsigned int ipv4_conntrack_local(void *priv,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700154 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400155 const struct nf_hook_state *state)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800156{
157 /* root is playing with raw sockets. */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700158 if (skb->len < sizeof(struct iphdr) ||
Patrick McHardy88843102009-01-12 00:06:00 +0000159 ip_hdrlen(skb) < sizeof(struct iphdr))
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800160 return NF_ACCEPT;
Eric W. Biederman082a7582015-09-18 14:32:56 -0500161 return nf_conntrack_in(state->net, PF_INET, state->hook, skb);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800162}
163
164/* Connection tracking may drop packets, but never alters them, so
165 make it the first hook. */
Patrick McHardy19994142007-12-05 01:23:00 -0800166static struct nf_hook_ops ipv4_conntrack_ops[] __read_mostly = {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700167 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700168 .hook = ipv4_conntrack_in,
Jan Engelhardt57750a22009-06-13 06:22:18 +0200169 .pf = NFPROTO_IPV4,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800170 .hooknum = NF_INET_PRE_ROUTING,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700171 .priority = NF_IP_PRI_CONNTRACK,
172 },
173 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700174 .hook = ipv4_conntrack_local,
Jan Engelhardt57750a22009-06-13 06:22:18 +0200175 .pf = NFPROTO_IPV4,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800176 .hooknum = NF_INET_LOCAL_OUT,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700177 .priority = NF_IP_PRI_CONNTRACK,
178 },
179 {
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200180 .hook = ipv4_helper,
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200181 .pf = NFPROTO_IPV4,
182 .hooknum = NF_INET_POST_ROUTING,
183 .priority = NF_IP_PRI_CONNTRACK_HELPER,
184 },
185 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700186 .hook = ipv4_confirm,
Jan Engelhardt57750a22009-06-13 06:22:18 +0200187 .pf = NFPROTO_IPV4,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800188 .hooknum = NF_INET_POST_ROUTING,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700189 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
190 },
191 {
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200192 .hook = ipv4_helper,
Pablo Neira Ayuso12f7a502012-05-13 21:44:54 +0200193 .pf = NFPROTO_IPV4,
194 .hooknum = NF_INET_LOCAL_IN,
195 .priority = NF_IP_PRI_CONNTRACK_HELPER,
196 },
197 {
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700198 .hook = ipv4_confirm,
Jan Engelhardt57750a22009-06-13 06:22:18 +0200199 .pf = NFPROTO_IPV4,
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800200 .hooknum = NF_INET_LOCAL_IN,
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700201 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
202 },
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800203};
204
Patrick McHardya999e682006-11-29 02:35:20 +0100205#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
206static int log_invalid_proto_min = 0;
207static int log_invalid_proto_max = 255;
208
Joe Perchesfe2c6332013-06-11 23:04:25 -0700209static struct ctl_table ip_ct_sysctl_table[] = {
Patrick McHardya999e682006-11-29 02:35:20 +0100210 {
Patrick McHardya999e682006-11-29 02:35:20 +0100211 .procname = "ip_conntrack_max",
Patrick McHardya999e682006-11-29 02:35:20 +0100212 .maxlen = sizeof(int),
213 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800214 .proc_handler = proc_dointvec,
Patrick McHardya999e682006-11-29 02:35:20 +0100215 },
216 {
Patrick McHardya999e682006-11-29 02:35:20 +0100217 .procname = "ip_conntrack_count",
Patrick McHardya999e682006-11-29 02:35:20 +0100218 .maxlen = sizeof(int),
219 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800220 .proc_handler = proc_dointvec,
Patrick McHardya999e682006-11-29 02:35:20 +0100221 },
222 {
Patrick McHardya999e682006-11-29 02:35:20 +0100223 .procname = "ip_conntrack_buckets",
Patrick McHardya999e682006-11-29 02:35:20 +0100224 .maxlen = sizeof(unsigned int),
225 .mode = 0444,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800226 .proc_handler = proc_dointvec,
Patrick McHardya999e682006-11-29 02:35:20 +0100227 },
228 {
Patrick McHardya999e682006-11-29 02:35:20 +0100229 .procname = "ip_conntrack_checksum",
Patrick McHardya999e682006-11-29 02:35:20 +0100230 .maxlen = sizeof(int),
231 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800232 .proc_handler = proc_dointvec,
Patrick McHardya999e682006-11-29 02:35:20 +0100233 },
234 {
Patrick McHardya999e682006-11-29 02:35:20 +0100235 .procname = "ip_conntrack_log_invalid",
Patrick McHardya999e682006-11-29 02:35:20 +0100236 .maxlen = sizeof(unsigned int),
237 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800238 .proc_handler = proc_dointvec_minmax,
Patrick McHardya999e682006-11-29 02:35:20 +0100239 .extra1 = &log_invalid_proto_min,
240 .extra2 = &log_invalid_proto_max,
241 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800242 { }
Patrick McHardya999e682006-11-29 02:35:20 +0100243};
244#endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
245
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800246/* Fast function for those who don't want to parse /proc (and I don't
247 blame them). */
248/* Reversing the socket's dst/src point of view gives us the reply
249 mapping. */
250static int
251getorigdst(struct sock *sk, int optval, void __user *user, int *len)
252{
Jan Engelhardt32948582008-01-31 04:53:24 -0800253 const struct inet_sock *inet = inet_sk(sk);
254 const struct nf_conntrack_tuple_hash *h;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800255 struct nf_conntrack_tuple tuple;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900256
Philip Craig443a70d2008-04-29 03:35:10 -0700257 memset(&tuple, 0, sizeof(tuple));
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000258 tuple.src.u3.ip = inet->inet_rcv_saddr;
259 tuple.src.u.tcp.port = inet->inet_sport;
260 tuple.dst.u3.ip = inet->inet_daddr;
261 tuple.dst.u.tcp.port = inet->inet_dport;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800262 tuple.src.l3num = PF_INET;
Rafael Laufer54981272009-08-10 10:08:27 +0200263 tuple.dst.protonum = sk->sk_protocol;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800264
Rafael Laufer54981272009-08-10 10:08:27 +0200265 /* We only do TCP and SCTP at the moment: is there a better way? */
266 if (sk->sk_protocol != IPPROTO_TCP && sk->sk_protocol != IPPROTO_SCTP) {
267 pr_debug("SO_ORIGINAL_DST: Not a TCP/SCTP socket\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800268 return -ENOPROTOOPT;
269 }
270
271 if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700272 pr_debug("SO_ORIGINAL_DST: len %d not %Zu\n",
273 *len, sizeof(struct sockaddr_in));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800274 return -EINVAL;
275 }
276
Daniel Borkmann308ac912015-08-08 21:40:01 +0200277 h = nf_conntrack_find_get(sock_net(sk), &nf_ct_zone_dflt, &tuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800278 if (h) {
279 struct sockaddr_in sin;
280 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
281
282 sin.sin_family = AF_INET;
283 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
284 .tuple.dst.u.tcp.port;
285 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
286 .tuple.dst.u3.ip;
Marcel Holtmann6c813c32006-05-28 22:50:18 -0700287 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800288
Harvey Harrisoncffee382008-10-31 00:53:08 -0700289 pr_debug("SO_ORIGINAL_DST: %pI4 %u\n",
290 &sin.sin_addr.s_addr, ntohs(sin.sin_port));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800291 nf_ct_put(ct);
292 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
293 return -EFAULT;
294 else
295 return 0;
296 }
Harvey Harrisoncffee382008-10-31 00:53:08 -0700297 pr_debug("SO_ORIGINAL_DST: Can't find %pI4/%u-%pI4/%u.\n",
298 &tuple.src.u3.ip, ntohs(tuple.src.u.tcp.port),
299 &tuple.dst.u3.ip, ntohs(tuple.dst.u.tcp.port));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800300 return -ENOENT;
301}
302
Duan Jiong24de3d32014-06-30 09:19:32 +0800303#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800304
305#include <linux/netfilter/nfnetlink.h>
306#include <linux/netfilter/nfnetlink_conntrack.h>
307
Patrick McHardyfdf70832007-09-28 14:37:41 -0700308static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800309 const struct nf_conntrack_tuple *tuple)
310{
Jiri Benc930345e2015-03-29 16:59:25 +0200311 if (nla_put_in_addr(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) ||
312 nla_put_in_addr(skb, CTA_IP_V4_DST, tuple->dst.u3.ip))
David S. Millerd317e4f2012-04-01 20:40:20 -0400313 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800314 return 0;
315
Patrick McHardydf6fb862007-09-28 14:37:03 -0700316nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800317 return -1;
318}
319
Patrick McHardyf73e9242007-09-28 14:39:55 -0700320static const struct nla_policy ipv4_nla_policy[CTA_IP_MAX+1] = {
321 [CTA_IP_V4_SRC] = { .type = NLA_U32 },
322 [CTA_IP_V4_DST] = { .type = NLA_U32 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800323};
324
Patrick McHardyfdf70832007-09-28 14:37:41 -0700325static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800326 struct nf_conntrack_tuple *t)
327{
Patrick McHardydf6fb862007-09-28 14:37:03 -0700328 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800329 return -EINVAL;
330
Jiri Benc67b61f62015-03-29 16:59:26 +0200331 t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]);
332 t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800333
334 return 0;
335}
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100336
337static int ipv4_nlattr_tuple_size(void)
338{
339 return nla_policy_len(ipv4_nla_policy, CTA_IP_MAX + 1);
340}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800341#endif
342
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800343static struct nf_sockopt_ops so_getorigdst = {
344 .pf = PF_INET,
345 .get_optmin = SO_ORIGINAL_DST,
346 .get_optmax = SO_ORIGINAL_DST+1,
Himangi Saraogi5bd3a762014-07-25 01:47:16 +0530347 .get = getorigdst,
Neil Horman16fcec32007-09-11 11:28:26 +0200348 .owner = THIS_MODULE,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800349};
350
Gao feng3ea04dd2012-05-28 21:04:16 +0000351static int ipv4_init_net(struct net *net)
352{
353#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
354 struct nf_ip_net *in = &net->ct.nf_ct_proto;
355 in->ctl_table = kmemdup(ip_ct_sysctl_table,
356 sizeof(ip_ct_sysctl_table),
357 GFP_KERNEL);
358 if (!in->ctl_table)
359 return -ENOMEM;
360
361 in->ctl_table[0].data = &nf_conntrack_max;
362 in->ctl_table[1].data = &net->ct.count;
Florian Westphal56d52d42016-05-02 18:39:55 +0200363 in->ctl_table[2].data = &nf_conntrack_htable_size;
Gao feng3ea04dd2012-05-28 21:04:16 +0000364 in->ctl_table[3].data = &net->ct.sysctl_checksum;
365 in->ctl_table[4].data = &net->ct.sysctl_log_invalid;
366#endif
367 return 0;
368}
369
Patrick McHardy61075af2007-07-14 20:48:19 -0700370struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 __read_mostly = {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800371 .l3proto = PF_INET,
372 .name = "ipv4",
373 .pkt_to_tuple = ipv4_pkt_to_tuple,
374 .invert_tuple = ipv4_invert_tuple,
375 .print_tuple = ipv4_print_tuple,
Yasuyuki Kozakaiffc30692007-07-14 20:44:50 -0700376 .get_l4proto = ipv4_get_l4proto,
Duan Jiong24de3d32014-06-30 09:19:32 +0800377#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700378 .tuple_to_nlattr = ipv4_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100379 .nlattr_tuple_size = ipv4_nlattr_tuple_size,
Patrick McHardyfdf70832007-09-28 14:37:41 -0700380 .nlattr_to_tuple = ipv4_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700381 .nla_policy = ipv4_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800382#endif
Patrick McHardya999e682006-11-29 02:35:20 +0100383#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
Eric W. Biedermanf99e8f72012-04-19 13:43:55 +0000384 .ctl_table_path = "net/ipv4/netfilter",
Patrick McHardya999e682006-11-29 02:35:20 +0100385#endif
Gao feng3ea04dd2012-05-28 21:04:16 +0000386 .init_net = ipv4_init_net,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800387 .me = THIS_MODULE,
388};
389
Patrick McHardyfae718d2007-12-24 21:09:10 -0800390module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
391 &nf_conntrack_htable_size, 0600);
392
Patrick McHardy32292a72006-04-06 14:11:30 -0700393MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
Patrick McHardyd2483dd2006-12-02 22:06:05 -0800394MODULE_ALIAS("ip_conntrack");
Patrick McHardy32292a72006-04-06 14:11:30 -0700395MODULE_LICENSE("GPL");
396
Gao feng3ea04dd2012-05-28 21:04:16 +0000397static int ipv4_net_init(struct net *net)
398{
399 int ret = 0;
400
Gao fengc296bb42013-01-23 12:51:10 +0100401 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_tcp4);
Gao feng3ea04dd2012-05-28 21:04:16 +0000402 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100403 pr_err("nf_conntrack_tcp4: pernet registration failed\n");
Gao feng3ea04dd2012-05-28 21:04:16 +0000404 goto out_tcp;
405 }
Gao fengc296bb42013-01-23 12:51:10 +0100406 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_udp4);
Gao feng3ea04dd2012-05-28 21:04:16 +0000407 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100408 pr_err("nf_conntrack_udp4: pernet registration failed\n");
Gao feng3ea04dd2012-05-28 21:04:16 +0000409 goto out_udp;
410 }
Gao fengc296bb42013-01-23 12:51:10 +0100411 ret = nf_ct_l4proto_pernet_register(net, &nf_conntrack_l4proto_icmp);
Gao feng3ea04dd2012-05-28 21:04:16 +0000412 if (ret < 0) {
Gao fengc296bb42013-01-23 12:51:10 +0100413 pr_err("nf_conntrack_icmp4: pernet registration failed\n");
Gao feng3ea04dd2012-05-28 21:04:16 +0000414 goto out_icmp;
415 }
Gao feng63307502013-01-21 22:10:33 +0000416 ret = nf_ct_l3proto_pernet_register(net, &nf_conntrack_l3proto_ipv4);
Gao feng3ea04dd2012-05-28 21:04:16 +0000417 if (ret < 0) {
Gao feng63307502013-01-21 22:10:33 +0000418 pr_err("nf_conntrack_ipv4: pernet registration failed\n");
Gao feng3ea04dd2012-05-28 21:04:16 +0000419 goto out_ipv4;
420 }
421 return 0;
422out_ipv4:
Gao fengc296bb42013-01-23 12:51:10 +0100423 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmp);
Gao feng3ea04dd2012-05-28 21:04:16 +0000424out_icmp:
Gao fengc296bb42013-01-23 12:51:10 +0100425 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp4);
Gao feng3ea04dd2012-05-28 21:04:16 +0000426out_udp:
Gao fengc296bb42013-01-23 12:51:10 +0100427 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp4);
Gao feng3ea04dd2012-05-28 21:04:16 +0000428out_tcp:
429 return ret;
430}
431
432static void ipv4_net_exit(struct net *net)
433{
Gao feng63307502013-01-21 22:10:33 +0000434 nf_ct_l3proto_pernet_unregister(net, &nf_conntrack_l3proto_ipv4);
Gao fengc296bb42013-01-23 12:51:10 +0100435 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_icmp);
436 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_udp4);
437 nf_ct_l4proto_pernet_unregister(net, &nf_conntrack_l4proto_tcp4);
Gao feng3ea04dd2012-05-28 21:04:16 +0000438}
439
440static struct pernet_operations ipv4_net_ops = {
441 .init = ipv4_net_init,
442 .exit = ipv4_net_exit,
443};
444
Patrick McHardy32292a72006-04-06 14:11:30 -0700445static int __init nf_conntrack_l3proto_ipv4_init(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800446{
447 int ret = 0;
448
Patrick McHardy32292a72006-04-06 14:11:30 -0700449 need_conntrack();
KOVACS Krisztian73e40222008-10-08 11:35:12 +0200450 nf_defrag_ipv4_enable();
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800451
452 ret = nf_register_sockopt(&so_getorigdst);
453 if (ret < 0) {
Bastian Stender09605cc2015-11-13 11:40:34 +0100454 pr_err("Unable to register netfilter socket option\n");
Patrick McHardy32292a72006-04-06 14:11:30 -0700455 return ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800456 }
457
Gao feng3ea04dd2012-05-28 21:04:16 +0000458 ret = register_pernet_subsys(&ipv4_net_ops);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800459 if (ret < 0) {
Gao feng3ea04dd2012-05-28 21:04:16 +0000460 pr_err("nf_conntrack_ipv4: can't register pernet ops\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800461 goto cleanup_sockopt;
462 }
463
Patrick McHardy964ddaa2006-04-06 14:09:49 -0700464 ret = nf_register_hooks(ipv4_conntrack_ops,
465 ARRAY_SIZE(ipv4_conntrack_ops));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800466 if (ret < 0) {
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200467 pr_err("nf_conntrack_ipv4: can't register hooks.\n");
Gao feng3ea04dd2012-05-28 21:04:16 +0000468 goto cleanup_pernet;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800469 }
Gao feng63307502013-01-21 22:10:33 +0000470
Gao fengc296bb42013-01-23 12:51:10 +0100471 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_tcp4);
472 if (ret < 0) {
473 pr_err("nf_conntrack_ipv4: can't register tcp4 proto.\n");
474 goto cleanup_hooks;
475 }
476
477 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_udp4);
478 if (ret < 0) {
479 pr_err("nf_conntrack_ipv4: can't register udp4 proto.\n");
480 goto cleanup_tcp4;
481 }
482
483 ret = nf_ct_l4proto_register(&nf_conntrack_l4proto_icmp);
484 if (ret < 0) {
485 pr_err("nf_conntrack_ipv4: can't register icmpv4 proto.\n");
486 goto cleanup_udp4;
487 }
488
Gao feng63307502013-01-21 22:10:33 +0000489 ret = nf_ct_l3proto_register(&nf_conntrack_l3proto_ipv4);
490 if (ret < 0) {
491 pr_err("nf_conntrack_ipv4: can't register ipv4 proto.\n");
Gao fengc296bb42013-01-23 12:51:10 +0100492 goto cleanup_icmpv4;
Gao feng63307502013-01-21 22:10:33 +0000493 }
494
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100495#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
496 ret = nf_conntrack_ipv4_compat_init();
497 if (ret < 0)
Gao feng63307502013-01-21 22:10:33 +0000498 goto cleanup_proto;
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100499#endif
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800500 return ret;
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100501#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
Gao feng63307502013-01-21 22:10:33 +0000502 cleanup_proto:
503 nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
504#endif
Gao fengc296bb42013-01-23 12:51:10 +0100505 cleanup_icmpv4:
506 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmp);
507 cleanup_udp4:
508 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp4);
509 cleanup_tcp4:
510 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100511 cleanup_hooks:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900512 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
Gao feng3ea04dd2012-05-28 21:04:16 +0000513 cleanup_pernet:
514 unregister_pernet_subsys(&ipv4_net_ops);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800515 cleanup_sockopt:
516 nf_unregister_sockopt(&so_getorigdst);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800517 return ret;
518}
519
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800520static void __exit nf_conntrack_l3proto_ipv4_fini(void)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800521{
Patrick McHardy32292a72006-04-06 14:11:30 -0700522 synchronize_net();
Patrick McHardye4bd8bc2006-11-29 02:35:22 +0100523#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
524 nf_conntrack_ipv4_compat_fini();
525#endif
Gao feng63307502013-01-21 22:10:33 +0000526 nf_ct_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
Gao fengc296bb42013-01-23 12:51:10 +0100527 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_icmp);
528 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_udp4);
529 nf_ct_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
Patrick McHardy32292a72006-04-06 14:11:30 -0700530 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
Gao feng3ea04dd2012-05-28 21:04:16 +0000531 unregister_pernet_subsys(&ipv4_net_ops);
Patrick McHardy32292a72006-04-06 14:11:30 -0700532 nf_unregister_sockopt(&so_getorigdst);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800533}
534
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800535module_init(nf_conntrack_l3proto_ipv4_init);
536module_exit(nf_conntrack_l3proto_ipv4_fini);