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.
8 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9 * - move L3 protocol dependent part to this file.
10 * 23 Mar 2004: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
11 * - add get_features() to support various size of conntrack
14 * Derived from net/ipv4/netfilter/ip_conntrack_standalone.c
17 #include <linux/types.h>
19 #include <linux/netfilter.h>
20 #include <linux/module.h>
21 #include <linux/skbuff.h>
22 #include <linux/icmp.h>
23 #include <linux/sysctl.h>
24 #include <net/route.h>
27 #include <linux/netfilter_ipv4.h>
28 #include <net/netfilter/nf_conntrack.h>
29 #include <net/netfilter/nf_conntrack_helper.h>
30 #include <net/netfilter/nf_conntrack_l4proto.h>
31 #include <net/netfilter/nf_conntrack_l3proto.h>
32 #include <net/netfilter/nf_conntrack_core.h>
33 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
38 #define DEBUGP(format, args...)
41 static int ipv4_pkt_to_tuple(const struct sk_buff
*skb
, unsigned int nhoff
,
42 struct nf_conntrack_tuple
*tuple
)
44 u_int32_t _addrs
[2], *ap
;
45 ap
= skb_header_pointer(skb
, nhoff
+ offsetof(struct iphdr
, saddr
),
46 sizeof(u_int32_t
) * 2, _addrs
);
50 tuple
->src
.u3
.ip
= ap
[0];
51 tuple
->dst
.u3
.ip
= ap
[1];
56 static int ipv4_invert_tuple(struct nf_conntrack_tuple
*tuple
,
57 const struct nf_conntrack_tuple
*orig
)
59 tuple
->src
.u3
.ip
= orig
->dst
.u3
.ip
;
60 tuple
->dst
.u3
.ip
= orig
->src
.u3
.ip
;
65 static int ipv4_print_tuple(struct seq_file
*s
,
66 const struct nf_conntrack_tuple
*tuple
)
68 return seq_printf(s
, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
69 NIPQUAD(tuple
->src
.u3
.ip
),
70 NIPQUAD(tuple
->dst
.u3
.ip
));
73 static int ipv4_print_conntrack(struct seq_file
*s
,
74 const struct nf_conn
*conntrack
)
79 /* Returns new sk_buff, or NULL */
80 static struct sk_buff
*
81 nf_ct_ipv4_gather_frags(struct sk_buff
*skb
, u_int32_t user
)
86 skb
= ip_defrag(skb
, user
);
90 ip_send_check(skb
->nh
.iph
);
96 ipv4_prepare(struct sk_buff
**pskb
, unsigned int hooknum
, unsigned int *dataoff
,
100 if ((*pskb
)->nh
.iph
->frag_off
& htons(IP_OFFSET
)) {
101 if (net_ratelimit()) {
102 printk(KERN_ERR
"ipv4_prepare: Frag of proto %u (hook=%u)\n",
103 (*pskb
)->nh
.iph
->protocol
, hooknum
);
108 *dataoff
= (*pskb
)->nh
.raw
- (*pskb
)->data
+ (*pskb
)->nh
.iph
->ihl
*4;
109 *protonum
= (*pskb
)->nh
.iph
->protocol
;
114 int nat_module_is_loaded
= 0;
115 static u_int32_t
ipv4_get_features(const struct nf_conntrack_tuple
*tuple
)
117 if (nat_module_is_loaded
)
120 return NF_CT_F_BASIC
;
123 static unsigned int ipv4_confirm(unsigned int hooknum
,
124 struct sk_buff
**pskb
,
125 const struct net_device
*in
,
126 const struct net_device
*out
,
127 int (*okfn
)(struct sk_buff
*))
129 /* We've seen it coming out the other side: confirm it */
130 return nf_conntrack_confirm(pskb
);
133 static unsigned int ipv4_conntrack_help(unsigned int hooknum
,
134 struct sk_buff
**pskb
,
135 const struct net_device
*in
,
136 const struct net_device
*out
,
137 int (*okfn
)(struct sk_buff
*))
140 enum ip_conntrack_info ctinfo
;
141 struct nf_conn_help
*help
;
143 /* This is where we call the helper: as the packet goes out. */
144 ct
= nf_ct_get(*pskb
, &ctinfo
);
145 if (!ct
|| ctinfo
== IP_CT_RELATED
+ IP_CT_IS_REPLY
)
148 help
= nfct_help(ct
);
149 if (!help
|| !help
->helper
)
152 return help
->helper
->help(pskb
,
153 (*pskb
)->nh
.raw
- (*pskb
)->data
154 + (*pskb
)->nh
.iph
->ihl
*4,
158 static unsigned int ipv4_conntrack_defrag(unsigned int hooknum
,
159 struct sk_buff
**pskb
,
160 const struct net_device
*in
,
161 const struct net_device
*out
,
162 int (*okfn
)(struct sk_buff
*))
164 #if !defined(CONFIG_IP_NF_NAT) && !defined(CONFIG_IP_NF_NAT_MODULE)
165 /* Previously seen (loopback)? Ignore. Do this before
171 /* Gather fragments. */
172 if ((*pskb
)->nh
.iph
->frag_off
& htons(IP_MF
|IP_OFFSET
)) {
173 *pskb
= nf_ct_ipv4_gather_frags(*pskb
,
174 hooknum
== NF_IP_PRE_ROUTING
?
175 IP_DEFRAG_CONNTRACK_IN
:
176 IP_DEFRAG_CONNTRACK_OUT
);
183 static unsigned int ipv4_conntrack_in(unsigned int hooknum
,
184 struct sk_buff
**pskb
,
185 const struct net_device
*in
,
186 const struct net_device
*out
,
187 int (*okfn
)(struct sk_buff
*))
189 return nf_conntrack_in(PF_INET
, hooknum
, pskb
);
192 static unsigned int ipv4_conntrack_local(unsigned int hooknum
,
193 struct sk_buff
**pskb
,
194 const struct net_device
*in
,
195 const struct net_device
*out
,
196 int (*okfn
)(struct sk_buff
*))
198 /* root is playing with raw sockets. */
199 if ((*pskb
)->len
< sizeof(struct iphdr
)
200 || (*pskb
)->nh
.iph
->ihl
* 4 < sizeof(struct iphdr
)) {
202 printk("ipt_hook: happy cracking.\n");
205 return nf_conntrack_in(PF_INET
, hooknum
, pskb
);
208 /* Connection tracking may drop packets, but never alters them, so
209 make it the first hook. */
210 static struct nf_hook_ops ipv4_conntrack_ops
[] = {
212 .hook
= ipv4_conntrack_defrag
,
213 .owner
= THIS_MODULE
,
215 .hooknum
= NF_IP_PRE_ROUTING
,
216 .priority
= NF_IP_PRI_CONNTRACK_DEFRAG
,
219 .hook
= ipv4_conntrack_in
,
220 .owner
= THIS_MODULE
,
222 .hooknum
= NF_IP_PRE_ROUTING
,
223 .priority
= NF_IP_PRI_CONNTRACK
,
226 .hook
= ipv4_conntrack_defrag
,
227 .owner
= THIS_MODULE
,
229 .hooknum
= NF_IP_LOCAL_OUT
,
230 .priority
= NF_IP_PRI_CONNTRACK_DEFRAG
,
233 .hook
= ipv4_conntrack_local
,
234 .owner
= THIS_MODULE
,
236 .hooknum
= NF_IP_LOCAL_OUT
,
237 .priority
= NF_IP_PRI_CONNTRACK
,
240 .hook
= ipv4_conntrack_help
,
241 .owner
= THIS_MODULE
,
243 .hooknum
= NF_IP_POST_ROUTING
,
244 .priority
= NF_IP_PRI_CONNTRACK_HELPER
,
247 .hook
= ipv4_conntrack_help
,
248 .owner
= THIS_MODULE
,
250 .hooknum
= NF_IP_LOCAL_IN
,
251 .priority
= NF_IP_PRI_CONNTRACK_HELPER
,
254 .hook
= ipv4_confirm
,
255 .owner
= THIS_MODULE
,
257 .hooknum
= NF_IP_POST_ROUTING
,
258 .priority
= NF_IP_PRI_CONNTRACK_CONFIRM
,
261 .hook
= ipv4_confirm
,
262 .owner
= THIS_MODULE
,
264 .hooknum
= NF_IP_LOCAL_IN
,
265 .priority
= NF_IP_PRI_CONNTRACK_CONFIRM
,
269 /* Fast function for those who don't want to parse /proc (and I don't
271 /* Reversing the socket's dst/src point of view gives us the reply
274 getorigdst(struct sock
*sk
, int optval
, void __user
*user
, int *len
)
276 struct inet_sock
*inet
= inet_sk(sk
);
277 struct nf_conntrack_tuple_hash
*h
;
278 struct nf_conntrack_tuple tuple
;
280 NF_CT_TUPLE_U_BLANK(&tuple
);
281 tuple
.src
.u3
.ip
= inet
->rcv_saddr
;
282 tuple
.src
.u
.tcp
.port
= inet
->sport
;
283 tuple
.dst
.u3
.ip
= inet
->daddr
;
284 tuple
.dst
.u
.tcp
.port
= inet
->dport
;
285 tuple
.src
.l3num
= PF_INET
;
286 tuple
.dst
.protonum
= IPPROTO_TCP
;
288 /* We only do TCP at the moment: is there a better way? */
289 if (strcmp(sk
->sk_prot
->name
, "TCP")) {
290 DEBUGP("SO_ORIGINAL_DST: Not a TCP socket\n");
294 if ((unsigned int) *len
< sizeof(struct sockaddr_in
)) {
295 DEBUGP("SO_ORIGINAL_DST: len %u not %u\n",
296 *len
, sizeof(struct sockaddr_in
));
300 h
= nf_conntrack_find_get(&tuple
, NULL
);
302 struct sockaddr_in sin
;
303 struct nf_conn
*ct
= nf_ct_tuplehash_to_ctrack(h
);
305 sin
.sin_family
= AF_INET
;
306 sin
.sin_port
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
]
307 .tuple
.dst
.u
.tcp
.port
;
308 sin
.sin_addr
.s_addr
= ct
->tuplehash
[IP_CT_DIR_ORIGINAL
]
310 memset(sin
.sin_zero
, 0, sizeof(sin
.sin_zero
));
312 DEBUGP("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
313 NIPQUAD(sin
.sin_addr
.s_addr
), ntohs(sin
.sin_port
));
315 if (copy_to_user(user
, &sin
, sizeof(sin
)) != 0)
320 DEBUGP("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
321 NIPQUAD(tuple
.src
.u3
.ip
), ntohs(tuple
.src
.u
.tcp
.port
),
322 NIPQUAD(tuple
.dst
.u3
.ip
), ntohs(tuple
.dst
.u
.tcp
.port
));
326 #if defined(CONFIG_NF_CT_NETLINK) || \
327 defined(CONFIG_NF_CT_NETLINK_MODULE)
329 #include <linux/netfilter/nfnetlink.h>
330 #include <linux/netfilter/nfnetlink_conntrack.h>
332 static int ipv4_tuple_to_nfattr(struct sk_buff
*skb
,
333 const struct nf_conntrack_tuple
*tuple
)
335 NFA_PUT(skb
, CTA_IP_V4_SRC
, sizeof(u_int32_t
),
337 NFA_PUT(skb
, CTA_IP_V4_DST
, sizeof(u_int32_t
),
345 static const size_t cta_min_ip
[CTA_IP_MAX
] = {
346 [CTA_IP_V4_SRC
-1] = sizeof(u_int32_t
),
347 [CTA_IP_V4_DST
-1] = sizeof(u_int32_t
),
350 static int ipv4_nfattr_to_tuple(struct nfattr
*tb
[],
351 struct nf_conntrack_tuple
*t
)
353 if (!tb
[CTA_IP_V4_SRC
-1] || !tb
[CTA_IP_V4_DST
-1])
356 if (nfattr_bad_size(tb
, CTA_IP_MAX
, cta_min_ip
))
360 *(u_int32_t
*)NFA_DATA(tb
[CTA_IP_V4_SRC
-1]);
362 *(u_int32_t
*)NFA_DATA(tb
[CTA_IP_V4_DST
-1]);
368 static struct nf_sockopt_ops so_getorigdst
= {
370 .get_optmin
= SO_ORIGINAL_DST
,
371 .get_optmax
= SO_ORIGINAL_DST
+1,
375 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4
= {
378 .pkt_to_tuple
= ipv4_pkt_to_tuple
,
379 .invert_tuple
= ipv4_invert_tuple
,
380 .print_tuple
= ipv4_print_tuple
,
381 .print_conntrack
= ipv4_print_conntrack
,
382 .prepare
= ipv4_prepare
,
383 .get_features
= ipv4_get_features
,
384 #if defined(CONFIG_NF_CT_NETLINK) || \
385 defined(CONFIG_NF_CT_NETLINK_MODULE)
386 .tuple_to_nfattr
= ipv4_tuple_to_nfattr
,
387 .nfattr_to_tuple
= ipv4_nfattr_to_tuple
,
392 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET
));
393 MODULE_LICENSE("GPL");
395 static int __init
nf_conntrack_l3proto_ipv4_init(void)
401 ret
= nf_register_sockopt(&so_getorigdst
);
403 printk(KERN_ERR
"Unable to register netfilter socket option\n");
407 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4
);
409 printk("nf_conntrack_ipv4: can't register tcp.\n");
410 goto cleanup_sockopt
;
413 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4
);
415 printk("nf_conntrack_ipv4: can't register udp.\n");
419 ret
= nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp
);
421 printk("nf_conntrack_ipv4: can't register icmp.\n");
425 ret
= nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4
);
427 printk("nf_conntrack_ipv4: can't register ipv4\n");
431 ret
= nf_register_hooks(ipv4_conntrack_ops
,
432 ARRAY_SIZE(ipv4_conntrack_ops
));
434 printk("nf_conntrack_ipv4: can't register hooks.\n");
440 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4
);
442 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp
);
444 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4
);
446 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4
);
448 nf_unregister_sockopt(&so_getorigdst
);
452 static void __exit
nf_conntrack_l3proto_ipv4_fini(void)
455 nf_unregister_hooks(ipv4_conntrack_ops
, ARRAY_SIZE(ipv4_conntrack_ops
));
456 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4
);
457 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp
);
458 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4
);
459 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4
);
460 nf_unregister_sockopt(&so_getorigdst
);
463 module_init(nf_conntrack_l3proto_ipv4_init
);
464 module_exit(nf_conntrack_l3proto_ipv4_fini
);
466 EXPORT_SYMBOL(nf_ct_ipv4_gather_frags
);