Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> |
| 3 | * |
| 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. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 7 | */ |
| 8 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 9 | #include <linux/types.h> |
| 10 | #include <linux/ip.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> |
Patrick McHardy | 0ae2cfe | 2006-01-05 12:21:52 -0800 | [diff] [blame] | 16 | #include <net/route.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 17 | #include <net/ip.h> |
| 18 | |
| 19 | #include <linux/netfilter_ipv4.h> |
| 20 | #include <net/netfilter/nf_conntrack.h> |
| 21 | #include <net/netfilter/nf_conntrack_helper.h> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 22 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 27 | static int ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff, |
| 28 | struct nf_conntrack_tuple *tuple) |
| 29 | { |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 30 | __be32 _addrs[2], *ap; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 31 | ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr), |
| 32 | sizeof(u_int32_t) * 2, _addrs); |
| 33 | if (ap == NULL) |
| 34 | return 0; |
| 35 | |
| 36 | tuple->src.u3.ip = ap[0]; |
| 37 | tuple->dst.u3.ip = ap[1]; |
| 38 | |
| 39 | return 1; |
| 40 | } |
| 41 | |
| 42 | static int ipv4_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 43 | const struct nf_conntrack_tuple *orig) |
| 44 | { |
| 45 | tuple->src.u3.ip = orig->dst.u3.ip; |
| 46 | tuple->dst.u3.ip = orig->src.u3.ip; |
| 47 | |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | static int ipv4_print_tuple(struct seq_file *s, |
| 52 | const struct nf_conntrack_tuple *tuple) |
| 53 | { |
| 54 | return seq_printf(s, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ", |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 55 | NIPQUAD(tuple->src.u3.ip), |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 56 | NIPQUAD(tuple->dst.u3.ip)); |
| 57 | } |
| 58 | |
| 59 | static int ipv4_print_conntrack(struct seq_file *s, |
| 60 | const struct nf_conn *conntrack) |
| 61 | { |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /* Returns new sk_buff, or NULL */ |
| 66 | static struct sk_buff * |
| 67 | nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user) |
| 68 | { |
| 69 | skb_orphan(skb); |
| 70 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 71 | local_bh_disable(); |
| 72 | skb = ip_defrag(skb, user); |
| 73 | local_bh_enable(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 74 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 75 | if (skb) |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 76 | ip_send_check(ip_hdr(skb)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 77 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 78 | return skb; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame^] | 81 | static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff, |
| 82 | unsigned int *dataoff, u_int8_t *protonum) |
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 | struct iphdr _iph, *iph; |
| 85 | |
| 86 | iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph); |
| 87 | if (iph == NULL) |
| 88 | return -NF_DROP; |
| 89 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 90 | /* Never happen */ |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame^] | 91 | if (iph->frag_off & htons(IP_OFFSET)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 92 | if (net_ratelimit()) { |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame^] | 93 | printk(KERN_ERR "ipv4_get_l4proto: Frag of proto %u\n", |
| 94 | iph->protocol); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 95 | } |
| 96 | return -NF_DROP; |
| 97 | } |
| 98 | |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame^] | 99 | *dataoff = nhoff + (iph->ihl << 2); |
| 100 | *protonum = iph->protocol; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 101 | |
| 102 | return NF_ACCEPT; |
| 103 | } |
| 104 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 105 | static unsigned int ipv4_confirm(unsigned int hooknum, |
| 106 | struct sk_buff **pskb, |
| 107 | const struct net_device *in, |
| 108 | const struct net_device *out, |
| 109 | int (*okfn)(struct sk_buff *)) |
| 110 | { |
| 111 | /* We've seen it coming out the other side: confirm it */ |
| 112 | return nf_conntrack_confirm(pskb); |
| 113 | } |
| 114 | |
| 115 | static unsigned int ipv4_conntrack_help(unsigned int hooknum, |
| 116 | struct sk_buff **pskb, |
| 117 | const struct net_device *in, |
| 118 | const struct net_device *out, |
| 119 | int (*okfn)(struct sk_buff *)) |
| 120 | { |
| 121 | struct nf_conn *ct; |
| 122 | enum ip_conntrack_info ctinfo; |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 123 | struct nf_conn_help *help; |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 124 | struct nf_conntrack_helper *helper; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 125 | |
| 126 | /* This is where we call the helper: as the packet goes out. */ |
| 127 | ct = nf_ct_get(*pskb, &ctinfo); |
Patrick McHardy | 6442f1c | 2006-05-29 18:21:53 -0700 | [diff] [blame] | 128 | if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY) |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 129 | return NF_ACCEPT; |
| 130 | |
| 131 | help = nfct_help(ct); |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 132 | if (!help) |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 133 | return NF_ACCEPT; |
Patrick McHarrdy | 3c158f7 | 2007-06-05 12:55:27 -0700 | [diff] [blame] | 134 | /* rcu_read_lock()ed by nf_hook_slow */ |
| 135 | helper = rcu_dereference(help->helper); |
| 136 | if (!helper) |
| 137 | return NF_ACCEPT; |
| 138 | return helper->help(pskb, skb_network_offset(*pskb) + ip_hdrlen(*pskb), |
| 139 | ct, ctinfo); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static unsigned int ipv4_conntrack_defrag(unsigned int hooknum, |
| 143 | struct sk_buff **pskb, |
| 144 | const struct net_device *in, |
| 145 | const struct net_device *out, |
| 146 | int (*okfn)(struct sk_buff *)) |
| 147 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 148 | /* Previously seen (loopback)? Ignore. Do this before |
| 149 | fragment check. */ |
| 150 | if ((*pskb)->nfct) |
| 151 | return NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 152 | |
| 153 | /* Gather fragments. */ |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 154 | if (ip_hdr(*pskb)->frag_off & htons(IP_MF | IP_OFFSET)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 155 | *pskb = nf_ct_ipv4_gather_frags(*pskb, |
| 156 | hooknum == NF_IP_PRE_ROUTING ? |
| 157 | IP_DEFRAG_CONNTRACK_IN : |
| 158 | IP_DEFRAG_CONNTRACK_OUT); |
| 159 | if (!*pskb) |
| 160 | return NF_STOLEN; |
| 161 | } |
| 162 | return NF_ACCEPT; |
| 163 | } |
| 164 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 165 | static unsigned int ipv4_conntrack_in(unsigned int hooknum, |
| 166 | struct sk_buff **pskb, |
| 167 | const struct net_device *in, |
| 168 | const struct net_device *out, |
| 169 | int (*okfn)(struct sk_buff *)) |
| 170 | { |
| 171 | return nf_conntrack_in(PF_INET, hooknum, pskb); |
| 172 | } |
| 173 | |
| 174 | static unsigned int ipv4_conntrack_local(unsigned int hooknum, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 175 | struct sk_buff **pskb, |
| 176 | const struct net_device *in, |
| 177 | const struct net_device *out, |
| 178 | int (*okfn)(struct sk_buff *)) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 179 | { |
| 180 | /* root is playing with raw sockets. */ |
| 181 | if ((*pskb)->len < sizeof(struct iphdr) |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 182 | || ip_hdrlen(*pskb) < sizeof(struct iphdr)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 183 | if (net_ratelimit()) |
| 184 | printk("ipt_hook: happy cracking.\n"); |
| 185 | return NF_ACCEPT; |
| 186 | } |
| 187 | return nf_conntrack_in(PF_INET, hooknum, pskb); |
| 188 | } |
| 189 | |
| 190 | /* Connection tracking may drop packets, but never alters them, so |
| 191 | make it the first hook. */ |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 192 | static struct nf_hook_ops ipv4_conntrack_ops[] = { |
| 193 | { |
| 194 | .hook = ipv4_conntrack_defrag, |
| 195 | .owner = THIS_MODULE, |
| 196 | .pf = PF_INET, |
| 197 | .hooknum = NF_IP_PRE_ROUTING, |
| 198 | .priority = NF_IP_PRI_CONNTRACK_DEFRAG, |
| 199 | }, |
| 200 | { |
| 201 | .hook = ipv4_conntrack_in, |
| 202 | .owner = THIS_MODULE, |
| 203 | .pf = PF_INET, |
| 204 | .hooknum = NF_IP_PRE_ROUTING, |
| 205 | .priority = NF_IP_PRI_CONNTRACK, |
| 206 | }, |
| 207 | { |
| 208 | .hook = ipv4_conntrack_defrag, |
| 209 | .owner = THIS_MODULE, |
| 210 | .pf = PF_INET, |
| 211 | .hooknum = NF_IP_LOCAL_OUT, |
| 212 | .priority = NF_IP_PRI_CONNTRACK_DEFRAG, |
| 213 | }, |
| 214 | { |
| 215 | .hook = ipv4_conntrack_local, |
| 216 | .owner = THIS_MODULE, |
| 217 | .pf = PF_INET, |
| 218 | .hooknum = NF_IP_LOCAL_OUT, |
| 219 | .priority = NF_IP_PRI_CONNTRACK, |
| 220 | }, |
| 221 | { |
| 222 | .hook = ipv4_conntrack_help, |
| 223 | .owner = THIS_MODULE, |
| 224 | .pf = PF_INET, |
| 225 | .hooknum = NF_IP_POST_ROUTING, |
| 226 | .priority = NF_IP_PRI_CONNTRACK_HELPER, |
| 227 | }, |
| 228 | { |
| 229 | .hook = ipv4_conntrack_help, |
| 230 | .owner = THIS_MODULE, |
| 231 | .pf = PF_INET, |
| 232 | .hooknum = NF_IP_LOCAL_IN, |
| 233 | .priority = NF_IP_PRI_CONNTRACK_HELPER, |
| 234 | }, |
| 235 | { |
| 236 | .hook = ipv4_confirm, |
| 237 | .owner = THIS_MODULE, |
| 238 | .pf = PF_INET, |
| 239 | .hooknum = NF_IP_POST_ROUTING, |
| 240 | .priority = NF_IP_PRI_CONNTRACK_CONFIRM, |
| 241 | }, |
| 242 | { |
| 243 | .hook = ipv4_confirm, |
| 244 | .owner = THIS_MODULE, |
| 245 | .pf = PF_INET, |
| 246 | .hooknum = NF_IP_LOCAL_IN, |
| 247 | .priority = NF_IP_PRI_CONNTRACK_CONFIRM, |
| 248 | }, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 249 | }; |
| 250 | |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 251 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT) |
| 252 | static int log_invalid_proto_min = 0; |
| 253 | static int log_invalid_proto_max = 255; |
| 254 | |
| 255 | static ctl_table ip_ct_sysctl_table[] = { |
| 256 | { |
| 257 | .ctl_name = NET_IPV4_NF_CONNTRACK_MAX, |
| 258 | .procname = "ip_conntrack_max", |
| 259 | .data = &nf_conntrack_max, |
| 260 | .maxlen = sizeof(int), |
| 261 | .mode = 0644, |
| 262 | .proc_handler = &proc_dointvec, |
| 263 | }, |
| 264 | { |
| 265 | .ctl_name = NET_IPV4_NF_CONNTRACK_COUNT, |
| 266 | .procname = "ip_conntrack_count", |
| 267 | .data = &nf_conntrack_count, |
| 268 | .maxlen = sizeof(int), |
| 269 | .mode = 0444, |
| 270 | .proc_handler = &proc_dointvec, |
| 271 | }, |
| 272 | { |
| 273 | .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS, |
| 274 | .procname = "ip_conntrack_buckets", |
| 275 | .data = &nf_conntrack_htable_size, |
| 276 | .maxlen = sizeof(unsigned int), |
| 277 | .mode = 0444, |
| 278 | .proc_handler = &proc_dointvec, |
| 279 | }, |
| 280 | { |
| 281 | .ctl_name = NET_IPV4_NF_CONNTRACK_CHECKSUM, |
| 282 | .procname = "ip_conntrack_checksum", |
| 283 | .data = &nf_conntrack_checksum, |
| 284 | .maxlen = sizeof(int), |
| 285 | .mode = 0644, |
| 286 | .proc_handler = &proc_dointvec, |
| 287 | }, |
| 288 | { |
| 289 | .ctl_name = NET_IPV4_NF_CONNTRACK_LOG_INVALID, |
| 290 | .procname = "ip_conntrack_log_invalid", |
| 291 | .data = &nf_ct_log_invalid, |
| 292 | .maxlen = sizeof(unsigned int), |
| 293 | .mode = 0644, |
| 294 | .proc_handler = &proc_dointvec_minmax, |
| 295 | .strategy = &sysctl_intvec, |
| 296 | .extra1 = &log_invalid_proto_min, |
| 297 | .extra2 = &log_invalid_proto_max, |
| 298 | }, |
| 299 | { |
| 300 | .ctl_name = 0 |
| 301 | } |
| 302 | }; |
| 303 | #endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */ |
| 304 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 305 | /* Fast function for those who don't want to parse /proc (and I don't |
| 306 | blame them). */ |
| 307 | /* Reversing the socket's dst/src point of view gives us the reply |
| 308 | mapping. */ |
| 309 | static int |
| 310 | getorigdst(struct sock *sk, int optval, void __user *user, int *len) |
| 311 | { |
| 312 | struct inet_sock *inet = inet_sk(sk); |
| 313 | struct nf_conntrack_tuple_hash *h; |
| 314 | struct nf_conntrack_tuple tuple; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 315 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 316 | NF_CT_TUPLE_U_BLANK(&tuple); |
| 317 | tuple.src.u3.ip = inet->rcv_saddr; |
| 318 | tuple.src.u.tcp.port = inet->sport; |
| 319 | tuple.dst.u3.ip = inet->daddr; |
| 320 | tuple.dst.u.tcp.port = inet->dport; |
| 321 | tuple.src.l3num = PF_INET; |
| 322 | tuple.dst.protonum = IPPROTO_TCP; |
| 323 | |
| 324 | /* We only do TCP at the moment: is there a better way? */ |
| 325 | if (strcmp(sk->sk_prot->name, "TCP")) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 326 | pr_debug("SO_ORIGINAL_DST: Not a TCP socket\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 327 | return -ENOPROTOOPT; |
| 328 | } |
| 329 | |
| 330 | if ((unsigned int) *len < sizeof(struct sockaddr_in)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 331 | pr_debug("SO_ORIGINAL_DST: len %d not %Zu\n", |
| 332 | *len, sizeof(struct sockaddr_in)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 333 | return -EINVAL; |
| 334 | } |
| 335 | |
Patrick McHardy | 330f7db | 2007-07-07 22:28:42 -0700 | [diff] [blame] | 336 | h = nf_conntrack_find_get(&tuple); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 337 | if (h) { |
| 338 | struct sockaddr_in sin; |
| 339 | struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h); |
| 340 | |
| 341 | sin.sin_family = AF_INET; |
| 342 | sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL] |
| 343 | .tuple.dst.u.tcp.port; |
| 344 | sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL] |
| 345 | .tuple.dst.u3.ip; |
Marcel Holtmann | 6c813c3 | 2006-05-28 22:50:18 -0700 | [diff] [blame] | 346 | memset(sin.sin_zero, 0, sizeof(sin.sin_zero)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 347 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 348 | pr_debug("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n", |
| 349 | NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 350 | nf_ct_put(ct); |
| 351 | if (copy_to_user(user, &sin, sizeof(sin)) != 0) |
| 352 | return -EFAULT; |
| 353 | else |
| 354 | return 0; |
| 355 | } |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 356 | pr_debug("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n", |
| 357 | NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port), |
| 358 | NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 359 | return -ENOENT; |
| 360 | } |
| 361 | |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 362 | #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] | 363 | |
| 364 | #include <linux/netfilter/nfnetlink.h> |
| 365 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 366 | |
| 367 | static int ipv4_tuple_to_nfattr(struct sk_buff *skb, |
| 368 | const struct nf_conntrack_tuple *tuple) |
| 369 | { |
| 370 | NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t), |
| 371 | &tuple->src.u3.ip); |
| 372 | NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t), |
| 373 | &tuple->dst.u3.ip); |
| 374 | return 0; |
| 375 | |
| 376 | nfattr_failure: |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | static const size_t cta_min_ip[CTA_IP_MAX] = { |
| 381 | [CTA_IP_V4_SRC-1] = sizeof(u_int32_t), |
| 382 | [CTA_IP_V4_DST-1] = sizeof(u_int32_t), |
| 383 | }; |
| 384 | |
| 385 | static int ipv4_nfattr_to_tuple(struct nfattr *tb[], |
| 386 | struct nf_conntrack_tuple *t) |
| 387 | { |
| 388 | if (!tb[CTA_IP_V4_SRC-1] || !tb[CTA_IP_V4_DST-1]) |
| 389 | return -EINVAL; |
| 390 | |
| 391 | if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip)) |
| 392 | return -EINVAL; |
| 393 | |
Patrick McHardy | bff9a89 | 2006-12-02 22:05:08 -0800 | [diff] [blame] | 394 | t->src.u3.ip = *(__be32 *)NFA_DATA(tb[CTA_IP_V4_SRC-1]); |
| 395 | t->dst.u3.ip = *(__be32 *)NFA_DATA(tb[CTA_IP_V4_DST-1]); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | #endif |
| 400 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 401 | static struct nf_sockopt_ops so_getorigdst = { |
| 402 | .pf = PF_INET, |
| 403 | .get_optmin = SO_ORIGINAL_DST, |
| 404 | .get_optmax = SO_ORIGINAL_DST+1, |
| 405 | .get = &getorigdst, |
| 406 | }; |
| 407 | |
| 408 | struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 = { |
| 409 | .l3proto = PF_INET, |
| 410 | .name = "ipv4", |
| 411 | .pkt_to_tuple = ipv4_pkt_to_tuple, |
| 412 | .invert_tuple = ipv4_invert_tuple, |
| 413 | .print_tuple = ipv4_print_tuple, |
| 414 | .print_conntrack = ipv4_print_conntrack, |
Yasuyuki Kozakai | ffc3069 | 2007-07-14 20:44:50 -0700 | [diff] [blame^] | 415 | .get_l4proto = ipv4_get_l4proto, |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 416 | #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] | 417 | .tuple_to_nfattr = ipv4_tuple_to_nfattr, |
| 418 | .nfattr_to_tuple = ipv4_nfattr_to_tuple, |
| 419 | #endif |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 420 | #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT) |
| 421 | .ctl_table_path = nf_net_ipv4_netfilter_sysctl_path, |
| 422 | .ctl_table = ip_ct_sysctl_table, |
| 423 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 424 | .me = THIS_MODULE, |
| 425 | }; |
| 426 | |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 427 | MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET)); |
Patrick McHardy | d2483dd | 2006-12-02 22:06:05 -0800 | [diff] [blame] | 428 | MODULE_ALIAS("ip_conntrack"); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 429 | MODULE_LICENSE("GPL"); |
| 430 | |
| 431 | static int __init nf_conntrack_l3proto_ipv4_init(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 432 | { |
| 433 | int ret = 0; |
| 434 | |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 435 | need_conntrack(); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 436 | |
| 437 | ret = nf_register_sockopt(&so_getorigdst); |
| 438 | if (ret < 0) { |
| 439 | printk(KERN_ERR "Unable to register netfilter socket option\n"); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 440 | return ret; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 443 | ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 444 | if (ret < 0) { |
| 445 | printk("nf_conntrack_ipv4: can't register tcp.\n"); |
| 446 | goto cleanup_sockopt; |
| 447 | } |
| 448 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 449 | ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 450 | if (ret < 0) { |
| 451 | printk("nf_conntrack_ipv4: can't register udp.\n"); |
| 452 | goto cleanup_tcp; |
| 453 | } |
| 454 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 455 | ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 456 | if (ret < 0) { |
| 457 | printk("nf_conntrack_ipv4: can't register icmp.\n"); |
| 458 | goto cleanup_udp; |
| 459 | } |
| 460 | |
| 461 | ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4); |
| 462 | if (ret < 0) { |
| 463 | printk("nf_conntrack_ipv4: can't register ipv4\n"); |
| 464 | goto cleanup_icmp; |
| 465 | } |
| 466 | |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 467 | ret = nf_register_hooks(ipv4_conntrack_ops, |
| 468 | ARRAY_SIZE(ipv4_conntrack_ops)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 469 | if (ret < 0) { |
Patrick McHardy | 964ddaa | 2006-04-06 14:09:49 -0700 | [diff] [blame] | 470 | printk("nf_conntrack_ipv4: can't register hooks.\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 471 | goto cleanup_ipv4; |
| 472 | } |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 473 | #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT) |
| 474 | ret = nf_conntrack_ipv4_compat_init(); |
| 475 | if (ret < 0) |
| 476 | goto cleanup_hooks; |
| 477 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 478 | return ret; |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 479 | #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT) |
| 480 | cleanup_hooks: |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 481 | nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops)); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 482 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 483 | cleanup_ipv4: |
| 484 | nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4); |
| 485 | cleanup_icmp: |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 486 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 487 | cleanup_udp: |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 488 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 489 | cleanup_tcp: |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 490 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 491 | cleanup_sockopt: |
| 492 | nf_unregister_sockopt(&so_getorigdst); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 493 | return ret; |
| 494 | } |
| 495 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 496 | static void __exit nf_conntrack_l3proto_ipv4_fini(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 497 | { |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 498 | synchronize_net(); |
Patrick McHardy | e4bd8bc | 2006-11-29 02:35:22 +0100 | [diff] [blame] | 499 | #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT) |
| 500 | nf_conntrack_ipv4_compat_fini(); |
| 501 | #endif |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 502 | nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops)); |
| 503 | nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4); |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 504 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp); |
| 505 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4); |
| 506 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4); |
Patrick McHardy | 32292a7 | 2006-04-06 14:11:30 -0700 | [diff] [blame] | 507 | nf_unregister_sockopt(&so_getorigdst); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 508 | } |
| 509 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 510 | module_init(nf_conntrack_l3proto_ipv4_init); |
| 511 | module_exit(nf_conntrack_l3proto_ipv4_fini); |