KOVACS Krisztian | 73e4022 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 1 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 2 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 3 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 8 | */ |
| 9 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 10 | #include <linux/types.h> |
| 11 | #include <linux/ip.h> |
| 12 | #include <linux/netfilter.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/skbuff.h> |
| 15 | #include <linux/icmp.h> |
| 16 | #include <linux/sysctl.h> |
Patrick McHardy | 0ae2cfe | 2006-01-05 12:21:52 -0800 | [diff] [blame] | 17 | #include <net/route.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 18 | #include <net/ip.h> |
| 19 | |
| 20 | #include <linux/netfilter_ipv4.h> |
| 21 | #include <net/netfilter/nf_conntrack.h> |
| 22 | #include <net/netfilter/nf_conntrack_helper.h> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 23 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 24 | #include <net/netfilter/nf_conntrack_l3proto.h> |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 25 | #include <net/netfilter/nf_conntrack_zones.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 26 | #include <net/netfilter/nf_conntrack_core.h> |
| 27 | #include <net/netfilter/ipv4/nf_conntrack_ipv4.h> |
Patrick McHardy | dd13b01 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 28 | #include <net/netfilter/nf_nat_helper.h> |
KOVACS Krisztian | 73e4022 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 29 | #include <net/netfilter/ipv4/nf_defrag_ipv4.h> |
Patrick McHardy | 74f7a65 | 2009-08-25 15:33:08 +0200 | [diff] [blame] | 30 | #include <net/netfilter/nf_log.h> |
Patrick McHardy | dd13b01 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 31 | |
| 32 | int (*nf_nat_seq_adjust_hook)(struct sk_buff *skb, |
| 33 | struct nf_conn *ct, |
| 34 | enum ip_conntrack_info ctinfo); |
| 35 | EXPORT_SYMBOL_GPL(nf_nat_seq_adjust_hook); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 36 | |
Jan Engelhardt | 8ce8439 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 37 | static bool ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff, |
| 38 | struct nf_conntrack_tuple *tuple) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 39 | { |
Jan Engelhardt | 3294858 | 2008-01-31 04:53:24 -0800 | [diff] [blame] | 40 | const __be32 *ap; |
| 41 | __be32 _addrs[2]; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 42 | ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr), |
| 43 | sizeof(u_int32_t) * 2, _addrs); |
| 44 | if (ap == NULL) |
Jan Engelhardt | 8ce8439 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 45 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 46 | |
| 47 | tuple->src.u3.ip = ap[0]; |
| 48 | tuple->dst.u3.ip = ap[1]; |
| 49 | |
Jan Engelhardt | 8ce8439 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 50 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Jan Engelhardt | 8ce8439 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 53 | static bool ipv4_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 54 | const struct nf_conntrack_tuple *orig) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 55 | { |
| 56 | tuple->src.u3.ip = orig->dst.u3.ip; |
| 57 | tuple->dst.u3.ip = orig->src.u3.ip; |
| 58 | |
Jan Engelhardt | 8ce8439 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 59 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static int ipv4_print_tuple(struct seq_file *s, |
| 63 | const struct nf_conntrack_tuple *tuple) |
| 64 | { |
Harvey Harrison | cffee38 | 2008-10-31 00:53:08 -0700 | [diff] [blame] | 65 | return seq_printf(s, "src=%pI4 dst=%pI4 ", |
| 66 | &tuple->src.u3.ip, &tuple->dst.u3.ip); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame] | 69 | static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff, |
| 70 | unsigned int *dataoff, u_int8_t *protonum) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 71 | { |
Jan Engelhardt | 3294858 | 2008-01-31 04:53:24 -0800 | [diff] [blame] | 72 | const struct iphdr *iph; |
| 73 | struct iphdr _iph; |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame] | 74 | |
| 75 | iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph); |
| 76 | if (iph == NULL) |
| 77 | return -NF_DROP; |
| 78 | |
Patrick McHardy | 0fb9670 | 2007-09-11 11:27:01 +0200 | [diff] [blame] | 79 | /* Conntrack defragments packets, we might still see fragments |
| 80 | * inside ICMP packets though. */ |
| 81 | if (iph->frag_off & htons(IP_OFFSET)) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 82 | return -NF_DROP; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 83 | |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame] | 84 | *dataoff = nhoff + (iph->ihl << 2); |
| 85 | *protonum = iph->protocol; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 86 | |
| 87 | return NF_ACCEPT; |
| 88 | } |
| 89 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 90 | static unsigned int ipv4_confirm(unsigned int hooknum, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 91 | struct sk_buff *skb, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 92 | const struct net_device *in, |
| 93 | const struct net_device *out, |
| 94 | int (*okfn)(struct sk_buff *)) |
| 95 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 96 | struct nf_conn *ct; |
| 97 | enum ip_conntrack_info ctinfo; |
Jan Engelhardt | 3294858 | 2008-01-31 04:53:24 -0800 | [diff] [blame] | 98 | const struct nf_conn_help *help; |
| 99 | const struct nf_conntrack_helper *helper; |
Patrick McHardy | dd13b01 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 100 | unsigned int ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 101 | |
| 102 | /* This is where we call the helper: as the packet goes out. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 103 | ct = nf_ct_get(skb, &ctinfo); |
Patrick McHardy | 6442f1c | 2006-05-29 18:21:53 -0700 | [diff] [blame] | 104 | if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY) |
Patrick McHardy | dd13b01 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 105 | goto out; |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 106 | |
| 107 | help = nfct_help(ct); |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 108 | if (!help) |
Patrick McHardy | dd13b01 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 109 | goto out; |
| 110 | |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 111 | /* rcu_read_lock()ed by nf_hook_slow */ |
| 112 | helper = rcu_dereference(help->helper); |
| 113 | if (!helper) |
Patrick McHardy | dd13b01 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 114 | goto out; |
| 115 | |
| 116 | ret = helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb), |
| 117 | ct, ctinfo); |
Patrick McHardy | 74f7a65 | 2009-08-25 15:33:08 +0200 | [diff] [blame] | 118 | if (ret != NF_ACCEPT) { |
| 119 | nf_log_packet(NFPROTO_IPV4, hooknum, skb, in, out, NULL, |
| 120 | "nf_ct_%s: dropping packet", helper->name); |
Patrick McHardy | dd13b01 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 121 | return ret; |
Patrick McHardy | 74f7a65 | 2009-08-25 15:33:08 +0200 | [diff] [blame] | 122 | } |
Patrick McHardy | dd13b01 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 123 | |
| 124 | if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) { |
| 125 | typeof(nf_nat_seq_adjust_hook) seq_adjust; |
| 126 | |
| 127 | seq_adjust = rcu_dereference(nf_nat_seq_adjust_hook); |
Pablo Neira Ayuso | 1db7a74 | 2009-03-16 15:18:50 +0100 | [diff] [blame] | 128 | if (!seq_adjust || !seq_adjust(skb, ct, ctinfo)) { |
| 129 | NF_CT_STAT_INC_ATOMIC(nf_ct_net(ct), drop); |
Patrick McHardy | dd13b01 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 130 | return NF_DROP; |
Pablo Neira Ayuso | 1db7a74 | 2009-03-16 15:18:50 +0100 | [diff] [blame] | 131 | } |
Patrick McHardy | dd13b01 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 132 | } |
| 133 | out: |
| 134 | /* We've seen it coming out the other side: confirm it */ |
| 135 | return nf_conntrack_confirm(skb); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 138 | static unsigned int ipv4_conntrack_in(unsigned int hooknum, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 139 | struct sk_buff *skb, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 140 | const struct net_device *in, |
| 141 | const struct net_device *out, |
| 142 | int (*okfn)(struct sk_buff *)) |
| 143 | { |
Alexey Dobriyan | a702a65 | 2008-10-08 11:35:04 +0200 | [diff] [blame] | 144 | return nf_conntrack_in(dev_net(in), PF_INET, hooknum, skb); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static unsigned int ipv4_conntrack_local(unsigned int hooknum, |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 148 | struct sk_buff *skb, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 149 | const struct net_device *in, |
| 150 | const struct net_device *out, |
| 151 | int (*okfn)(struct sk_buff *)) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 152 | { |
| 153 | /* root is playing with raw sockets. */ |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 154 | if (skb->len < sizeof(struct iphdr) || |
Patrick McHardy | 8884310 | 2009-01-12 00:06:00 +0000 | [diff] [blame] | 155 | ip_hdrlen(skb) < sizeof(struct iphdr)) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 156 | return NF_ACCEPT; |
Alexey Dobriyan | a702a65 | 2008-10-08 11:35:04 +0200 | [diff] [blame] | 157 | return nf_conntrack_in(dev_net(out), PF_INET, hooknum, skb); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* Connection tracking may drop packets, but never alters them, so |
| 161 | make it the first hook. */ |
Patrick McHardy | 1999414 | 2007-12-05 01:23:00 -0800 | [diff] [blame] | 162 | static struct nf_hook_ops ipv4_conntrack_ops[] __read_mostly = { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 163 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 164 | .hook = ipv4_conntrack_in, |
| 165 | .owner = THIS_MODULE, |
Jan Engelhardt | 57750a2 | 2009-06-13 06:22:18 +0200 | [diff] [blame] | 166 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 167 | .hooknum = NF_INET_PRE_ROUTING, |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 168 | .priority = NF_IP_PRI_CONNTRACK, |
| 169 | }, |
| 170 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 171 | .hook = ipv4_conntrack_local, |
| 172 | .owner = THIS_MODULE, |
Jan Engelhardt | 57750a2 | 2009-06-13 06:22:18 +0200 | [diff] [blame] | 173 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 174 | .hooknum = NF_INET_LOCAL_OUT, |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 175 | .priority = NF_IP_PRI_CONNTRACK, |
| 176 | }, |
| 177 | { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 178 | .hook = ipv4_confirm, |
| 179 | .owner = THIS_MODULE, |
Jan Engelhardt | 57750a2 | 2009-06-13 06:22:18 +0200 | [diff] [blame] | 180 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 181 | .hooknum = NF_INET_POST_ROUTING, |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 182 | .priority = NF_IP_PRI_CONNTRACK_CONFIRM, |
| 183 | }, |
| 184 | { |
| 185 | .hook = ipv4_confirm, |
| 186 | .owner = THIS_MODULE, |
Jan Engelhardt | 57750a2 | 2009-06-13 06:22:18 +0200 | [diff] [blame] | 187 | .pf = NFPROTO_IPV4, |
Patrick McHardy | 6e23ae2 | 2007-11-19 18:53:30 -0800 | [diff] [blame] | 188 | .hooknum = NF_INET_LOCAL_IN, |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 189 | .priority = NF_IP_PRI_CONNTRACK_CONFIRM, |
| 190 | }, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 191 | }; |
| 192 | |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 193 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT) |
| 194 | static int log_invalid_proto_min = 0; |
| 195 | static int log_invalid_proto_max = 255; |
| 196 | |
| 197 | static ctl_table ip_ct_sysctl_table[] = { |
| 198 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 199 | .procname = "ip_conntrack_max", |
| 200 | .data = &nf_conntrack_max, |
| 201 | .maxlen = sizeof(int), |
| 202 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 203 | .proc_handler = proc_dointvec, |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 204 | }, |
| 205 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 206 | .procname = "ip_conntrack_count", |
Alexey Dobriyan | 49ac871 | 2008-10-08 11:35:03 +0200 | [diff] [blame] | 207 | .data = &init_net.ct.count, |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 208 | .maxlen = sizeof(int), |
| 209 | .mode = 0444, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 210 | .proc_handler = proc_dointvec, |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 211 | }, |
| 212 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 213 | .procname = "ip_conntrack_buckets", |
Patrick McHardy | d696c7b | 2010-02-08 11:18:07 -0800 | [diff] [blame] | 214 | .data = &init_net.ct.htable_size, |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 215 | .maxlen = sizeof(unsigned int), |
| 216 | .mode = 0444, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 217 | .proc_handler = proc_dointvec, |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 218 | }, |
| 219 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 220 | .procname = "ip_conntrack_checksum", |
Alexey Dobriyan | c04d055 | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 221 | .data = &init_net.ct.sysctl_checksum, |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 222 | .maxlen = sizeof(int), |
| 223 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 224 | .proc_handler = proc_dointvec, |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 225 | }, |
| 226 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 227 | .procname = "ip_conntrack_log_invalid", |
Alexey Dobriyan | c2a2c7e | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 228 | .data = &init_net.ct.sysctl_log_invalid, |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 229 | .maxlen = sizeof(unsigned int), |
| 230 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 231 | .proc_handler = proc_dointvec_minmax, |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 232 | .extra1 = &log_invalid_proto_min, |
| 233 | .extra2 = &log_invalid_proto_max, |
| 234 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 235 | { } |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 236 | }; |
| 237 | #endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */ |
| 238 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 239 | /* Fast function for those who don't want to parse /proc (and I don't |
| 240 | blame them). */ |
| 241 | /* Reversing the socket's dst/src point of view gives us the reply |
| 242 | mapping. */ |
| 243 | static int |
| 244 | getorigdst(struct sock *sk, int optval, void __user *user, int *len) |
| 245 | { |
Jan Engelhardt | 3294858 | 2008-01-31 04:53:24 -0800 | [diff] [blame] | 246 | const struct inet_sock *inet = inet_sk(sk); |
| 247 | const struct nf_conntrack_tuple_hash *h; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 248 | struct nf_conntrack_tuple tuple; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 249 | |
Philip Craig | 443a70d | 2008-04-29 03:35:10 -0700 | [diff] [blame] | 250 | memset(&tuple, 0, sizeof(tuple)); |
Eric Dumazet | c720c7e | 2009-10-15 06:30:45 +0000 | [diff] [blame] | 251 | tuple.src.u3.ip = inet->inet_rcv_saddr; |
| 252 | tuple.src.u.tcp.port = inet->inet_sport; |
| 253 | tuple.dst.u3.ip = inet->inet_daddr; |
| 254 | tuple.dst.u.tcp.port = inet->inet_dport; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 255 | tuple.src.l3num = PF_INET; |
Rafael Laufer | 5498127 | 2009-08-10 10:08:27 +0200 | [diff] [blame] | 256 | tuple.dst.protonum = sk->sk_protocol; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 257 | |
Rafael Laufer | 5498127 | 2009-08-10 10:08:27 +0200 | [diff] [blame] | 258 | /* We only do TCP and SCTP at the moment: is there a better way? */ |
| 259 | if (sk->sk_protocol != IPPROTO_TCP && sk->sk_protocol != IPPROTO_SCTP) { |
| 260 | pr_debug("SO_ORIGINAL_DST: Not a TCP/SCTP socket\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 261 | return -ENOPROTOOPT; |
| 262 | } |
| 263 | |
| 264 | if ((unsigned int) *len < sizeof(struct sockaddr_in)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 265 | pr_debug("SO_ORIGINAL_DST: len %d not %Zu\n", |
| 266 | *len, sizeof(struct sockaddr_in)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 267 | return -EINVAL; |
| 268 | } |
| 269 | |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 270 | h = nf_conntrack_find_get(sock_net(sk), NF_CT_DEFAULT_ZONE, &tuple); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 271 | if (h) { |
| 272 | struct sockaddr_in sin; |
| 273 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); |
| 274 | |
| 275 | sin.sin_family = AF_INET; |
| 276 | sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL] |
| 277 | .tuple.dst.u.tcp.port; |
| 278 | sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL] |
| 279 | .tuple.dst.u3.ip; |
Marcel Holtmann | 6c813c3 | 2006-05-28 22:50:18 -0700 | [diff] [blame] | 280 | memset(sin.sin_zero, 0, sizeof(sin.sin_zero)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 281 | |
Harvey Harrison | cffee38 | 2008-10-31 00:53:08 -0700 | [diff] [blame] | 282 | pr_debug("SO_ORIGINAL_DST: %pI4 %u\n", |
| 283 | &sin.sin_addr.s_addr, ntohs(sin.sin_port)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 284 | nf_ct_put(ct); |
| 285 | if (copy_to_user(user, &sin, sizeof(sin)) != 0) |
| 286 | return -EFAULT; |
| 287 | else |
| 288 | return 0; |
| 289 | } |
Harvey Harrison | cffee38 | 2008-10-31 00:53:08 -0700 | [diff] [blame] | 290 | pr_debug("SO_ORIGINAL_DST: Can't find %pI4/%u-%pI4/%u.\n", |
| 291 | &tuple.src.u3.ip, ntohs(tuple.src.u.tcp.port), |
| 292 | &tuple.dst.u3.ip, ntohs(tuple.dst.u.tcp.port)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 293 | return -ENOENT; |
| 294 | } |
| 295 | |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 296 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 297 | |
| 298 | #include <linux/netfilter/nfnetlink.h> |
| 299 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 300 | |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 301 | static int ipv4_tuple_to_nlattr(struct sk_buff *skb, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 302 | const struct nf_conntrack_tuple *tuple) |
| 303 | { |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 304 | NLA_PUT_BE32(skb, CTA_IP_V4_SRC, tuple->src.u3.ip); |
| 305 | NLA_PUT_BE32(skb, CTA_IP_V4_DST, tuple->dst.u3.ip); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 306 | return 0; |
| 307 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 308 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 309 | return -1; |
| 310 | } |
| 311 | |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 312 | static const struct nla_policy ipv4_nla_policy[CTA_IP_MAX+1] = { |
| 313 | [CTA_IP_V4_SRC] = { .type = NLA_U32 }, |
| 314 | [CTA_IP_V4_DST] = { .type = NLA_U32 }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 315 | }; |
| 316 | |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 317 | static int ipv4_nlattr_to_tuple(struct nlattr *tb[], |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 318 | struct nf_conntrack_tuple *t) |
| 319 | { |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 320 | if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 321 | return -EINVAL; |
| 322 | |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 323 | t->src.u3.ip = nla_get_be32(tb[CTA_IP_V4_SRC]); |
| 324 | t->dst.u3.ip = nla_get_be32(tb[CTA_IP_V4_DST]); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 325 | |
| 326 | return 0; |
| 327 | } |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 328 | |
| 329 | static int ipv4_nlattr_tuple_size(void) |
| 330 | { |
| 331 | return nla_policy_len(ipv4_nla_policy, CTA_IP_MAX + 1); |
| 332 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 333 | #endif |
| 334 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 335 | static struct nf_sockopt_ops so_getorigdst = { |
| 336 | .pf = PF_INET, |
| 337 | .get_optmin = SO_ORIGINAL_DST, |
| 338 | .get_optmax = SO_ORIGINAL_DST+1, |
| 339 | .get = &getorigdst, |
Neil Horman | 16fcec3 | 2007-09-11 11:28:26 +0200 | [diff] [blame] | 340 | .owner = THIS_MODULE, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 341 | }; |
| 342 | |
Patrick McHardy | 61075af | 2007-07-14 20:48:19 -0700 | [diff] [blame] | 343 | struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 __read_mostly = { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 344 | .l3proto = PF_INET, |
| 345 | .name = "ipv4", |
| 346 | .pkt_to_tuple = ipv4_pkt_to_tuple, |
| 347 | .invert_tuple = ipv4_invert_tuple, |
| 348 | .print_tuple = ipv4_print_tuple, |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame] | 349 | .get_l4proto = ipv4_get_l4proto, |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 350 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 351 | .tuple_to_nlattr = ipv4_tuple_to_nlattr, |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 352 | .nlattr_tuple_size = ipv4_nlattr_tuple_size, |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 353 | .nlattr_to_tuple = ipv4_nlattr_to_tuple, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 354 | .nla_policy = ipv4_nla_policy, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 355 | #endif |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 356 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT) |
| 357 | .ctl_table_path = nf_net_ipv4_netfilter_sysctl_path, |
| 358 | .ctl_table = ip_ct_sysctl_table, |
| 359 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 360 | .me = THIS_MODULE, |
| 361 | }; |
| 362 | |
Patrick McHardy | fae718d | 2007-12-24 21:09:10 -0800 | [diff] [blame] | 363 | module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint, |
| 364 | &nf_conntrack_htable_size, 0600); |
| 365 | |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 366 | MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET)); |
Patrick McHardy | d2483dd | 2006-12-02 22:06:05 -0800 | [diff] [blame] | 367 | MODULE_ALIAS("ip_conntrack"); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 368 | MODULE_LICENSE("GPL"); |
| 369 | |
| 370 | static int __init nf_conntrack_l3proto_ipv4_init(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 371 | { |
| 372 | int ret = 0; |
| 373 | |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 374 | need_conntrack(); |
KOVACS Krisztian | 73e4022 | 2008-10-08 11:35:12 +0200 | [diff] [blame] | 375 | nf_defrag_ipv4_enable(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 376 | |
| 377 | ret = nf_register_sockopt(&so_getorigdst); |
| 378 | if (ret < 0) { |
| 379 | printk(KERN_ERR "Unable to register netfilter socket option\n"); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 380 | return ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 381 | } |
| 382 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 383 | ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 384 | if (ret < 0) { |
Stephen Hemminger | 654d0fb | 2010-05-13 15:02:08 +0200 | [diff] [blame] | 385 | pr_err("nf_conntrack_ipv4: can't register tcp.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 386 | goto cleanup_sockopt; |
| 387 | } |
| 388 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 389 | ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 390 | if (ret < 0) { |
Stephen Hemminger | 654d0fb | 2010-05-13 15:02:08 +0200 | [diff] [blame] | 391 | pr_err("nf_conntrack_ipv4: can't register udp.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 392 | goto cleanup_tcp; |
| 393 | } |
| 394 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 395 | ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 396 | if (ret < 0) { |
Stephen Hemminger | 654d0fb | 2010-05-13 15:02:08 +0200 | [diff] [blame] | 397 | pr_err("nf_conntrack_ipv4: can't register icmp.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 398 | goto cleanup_udp; |
| 399 | } |
| 400 | |
| 401 | ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4); |
| 402 | if (ret < 0) { |
Stephen Hemminger | 654d0fb | 2010-05-13 15:02:08 +0200 | [diff] [blame] | 403 | pr_err("nf_conntrack_ipv4: can't register ipv4\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 404 | goto cleanup_icmp; |
| 405 | } |
| 406 | |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 407 | ret = nf_register_hooks(ipv4_conntrack_ops, |
| 408 | ARRAY_SIZE(ipv4_conntrack_ops)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 409 | if (ret < 0) { |
Stephen Hemminger | 654d0fb | 2010-05-13 15:02:08 +0200 | [diff] [blame] | 410 | pr_err("nf_conntrack_ipv4: can't register hooks.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 411 | goto cleanup_ipv4; |
| 412 | } |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 413 | #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT) |
| 414 | ret = nf_conntrack_ipv4_compat_init(); |
| 415 | if (ret < 0) |
| 416 | goto cleanup_hooks; |
| 417 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 418 | return ret; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 419 | #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT) |
| 420 | cleanup_hooks: |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 421 | nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops)); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 422 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 423 | cleanup_ipv4: |
| 424 | nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4); |
| 425 | cleanup_icmp: |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 426 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 427 | cleanup_udp: |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 428 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 429 | cleanup_tcp: |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 430 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 431 | cleanup_sockopt: |
| 432 | nf_unregister_sockopt(&so_getorigdst); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 433 | return ret; |
| 434 | } |
| 435 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 436 | static void __exit nf_conntrack_l3proto_ipv4_fini(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 437 | { |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 438 | synchronize_net(); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 439 | #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT) |
| 440 | nf_conntrack_ipv4_compat_fini(); |
| 441 | #endif |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 442 | nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops)); |
| 443 | nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4); |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 444 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp); |
| 445 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4); |
| 446 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 447 | nf_unregister_sockopt(&so_getorigdst); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 448 | } |
| 449 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 450 | module_init(nf_conntrack_l3proto_ipv4_init); |
| 451 | module_exit(nf_conntrack_l3proto_ipv4_fini); |
Patrick McHardy | 591e620 | 2007-08-07 18:12:01 -0700 | [diff] [blame] | 452 | |
| 453 | void need_ipv4_conntrack(void) |
| 454 | { |
| 455 | return; |
| 456 | } |
| 457 | EXPORT_SYMBOL_GPL(need_ipv4_conntrack); |