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