NETFILTER: remove unnecessary goto statement for error recovery
[tomato.git] / release / src-rt / linux / linux-2.6 / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4.c
blobec6d4d3df1af0d87eafa58c2b59bee868564cbc8
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.
7 */
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>
16 #include <net/route.h>
17 #include <net/ip.h>
19 #include <linux/netfilter_ipv4.h>
20 #include <net/netfilter/nf_conntrack.h>
21 #include <net/netfilter/nf_conntrack_helper.h>
22 #include <net/netfilter/nf_conntrack_l4proto.h>
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>
27 #if 0
28 #define DEBUGP printk
29 #else
30 #define DEBUGP(format, args...)
31 #endif
33 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE) || defined(HNDCTF)
34 int ipv4_conntrack_fastnat = 0;
35 EXPORT_SYMBOL_GPL(ipv4_conntrack_fastnat);
36 #endif
38 static int ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
39 struct nf_conntrack_tuple *tuple)
41 __be32 _addrs[2], *ap;
42 ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr),
43 sizeof(u_int32_t) * 2, _addrs);
44 if (ap == NULL)
45 return 0;
47 tuple->src.u3.ip = ap[0];
48 tuple->dst.u3.ip = ap[1];
50 return 1;
53 static int ipv4_invert_tuple(struct nf_conntrack_tuple *tuple,
54 const struct nf_conntrack_tuple *orig)
56 tuple->src.u3.ip = orig->dst.u3.ip;
57 tuple->dst.u3.ip = orig->src.u3.ip;
59 return 1;
62 static int ipv4_print_tuple(struct seq_file *s,
63 const struct nf_conntrack_tuple *tuple)
65 return seq_printf(s, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
66 NIPQUAD(tuple->src.u3.ip),
67 NIPQUAD(tuple->dst.u3.ip));
70 /* Returns new sk_buff, or NULL */
71 #if !defined(CONFIG_BCM_NAT) && !defined(CONFIG_BCM_NAT_MODULE)
72 static
73 #endif
74 int nf_ct_ipv4_gather_frags(struct sk_buff *skb, u_int32_t user)
76 int err;
78 skb_orphan(skb);
80 local_bh_disable();
81 err = ip_defrag(skb, user);
82 local_bh_enable();
84 if (!err)
85 ip_send_check(ip_hdr(skb));
87 return err;
90 static int
91 ipv4_prepare(struct sk_buff *skb, unsigned int hooknum, unsigned int *dataoff,
92 u_int8_t *protonum)
94 /* Never happen */
95 if (ip_hdr(skb)->frag_off & htons(IP_OFFSET)) {
96 if (net_ratelimit()) {
97 printk(KERN_ERR "ipv4_prepare: Frag of proto %u (hook=%u)\n",
98 ip_hdr(skb)->protocol, hooknum);
100 return -NF_DROP;
103 *dataoff = skb_network_offset(skb) + ip_hdrlen(skb);
104 *protonum = ip_hdr(skb)->protocol;
106 return NF_ACCEPT;
109 int nf_nat_module_is_loaded = 0;
110 EXPORT_SYMBOL_GPL(nf_nat_module_is_loaded);
112 static u_int32_t ipv4_get_features(const struct nf_conntrack_tuple *tuple)
114 if (nf_nat_module_is_loaded)
115 return NF_CT_F_NAT;
117 return NF_CT_F_BASIC;
120 static unsigned int ipv4_confirm(unsigned int hooknum,
121 struct sk_buff *skb,
122 const struct net_device *in,
123 const struct net_device *out,
124 int (*okfn)(struct sk_buff *))
126 /* We've seen it coming out the other side: confirm it */
127 return nf_conntrack_confirm(skb);
130 static unsigned int ipv4_conntrack_help(unsigned int hooknum,
131 struct sk_buff *skb,
132 const struct net_device *in,
133 const struct net_device *out,
134 int (*okfn)(struct sk_buff *))
136 struct nf_conn *ct;
137 enum ip_conntrack_info ctinfo;
138 struct nf_conn_help *help;
139 struct nf_conntrack_helper *helper;
141 /* This is where we call the helper: as the packet goes out. */
142 ct = nf_ct_get(skb, &ctinfo);
143 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
144 return NF_ACCEPT;
146 help = nfct_help(ct);
147 if (!help)
148 return NF_ACCEPT;
149 /* rcu_read_lock()ed by nf_hook_slow */
150 helper = rcu_dereference(help->helper);
151 if (!helper)
152 return NF_ACCEPT;
153 return helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb),
154 ct, ctinfo);
157 static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
158 struct sk_buff *skb,
159 const struct net_device *in,
160 const struct net_device *out,
161 int (*okfn)(struct sk_buff *))
163 /* Previously seen (loopback)? Ignore. Do this before
164 fragment check. */
165 if (skb->nfct)
166 return NF_ACCEPT;
168 /* Gather fragments. */
169 if (ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) {
170 if (nf_ct_ipv4_gather_frags(skb,
171 hooknum == NF_IP_PRE_ROUTING ?
172 IP_DEFRAG_CONNTRACK_IN :
173 IP_DEFRAG_CONNTRACK_OUT))
174 return NF_STOLEN;
176 return NF_ACCEPT;
179 static unsigned int ipv4_conntrack_in(unsigned int hooknum,
180 struct sk_buff *skb,
181 const struct net_device *in,
182 const struct net_device *out,
183 int (*okfn)(struct sk_buff *))
185 return nf_conntrack_in(PF_INET, hooknum, skb);
188 static unsigned int ipv4_conntrack_local(unsigned int hooknum,
189 struct sk_buff *skb,
190 const struct net_device *in,
191 const struct net_device *out,
192 int (*okfn)(struct sk_buff *))
194 /* root is playing with raw sockets. */
195 if (skb->len < sizeof(struct iphdr) ||
196 ip_hdrlen(skb) < sizeof(struct iphdr)) {
197 if (net_ratelimit())
198 printk("ipt_hook: happy cracking.\n");
199 return NF_ACCEPT;
201 return nf_conntrack_in(PF_INET, hooknum, skb);
204 /* Connection tracking may drop packets, but never alters them, so
205 make it the first hook. */
206 static struct nf_hook_ops ipv4_conntrack_ops[] = {
208 .hook = ipv4_conntrack_defrag,
209 .owner = THIS_MODULE,
210 .pf = PF_INET,
211 .hooknum = NF_IP_PRE_ROUTING,
212 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
215 .hook = ipv4_conntrack_in,
216 .owner = THIS_MODULE,
217 .pf = PF_INET,
218 .hooknum = NF_IP_PRE_ROUTING,
219 .priority = NF_IP_PRI_CONNTRACK,
222 .hook = ipv4_conntrack_defrag,
223 .owner = THIS_MODULE,
224 .pf = PF_INET,
225 .hooknum = NF_IP_LOCAL_OUT,
226 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
229 .hook = ipv4_conntrack_local,
230 .owner = THIS_MODULE,
231 .pf = PF_INET,
232 .hooknum = NF_IP_LOCAL_OUT,
233 .priority = NF_IP_PRI_CONNTRACK,
236 .hook = ipv4_conntrack_help,
237 .owner = THIS_MODULE,
238 .pf = PF_INET,
239 .hooknum = NF_IP_POST_ROUTING,
240 .priority = NF_IP_PRI_CONNTRACK_HELPER,
243 .hook = ipv4_conntrack_help,
244 .owner = THIS_MODULE,
245 .pf = PF_INET,
246 .hooknum = NF_IP_LOCAL_IN,
247 .priority = NF_IP_PRI_CONNTRACK_HELPER,
250 .hook = ipv4_confirm,
251 .owner = THIS_MODULE,
252 .pf = PF_INET,
253 .hooknum = NF_IP_POST_ROUTING,
254 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
257 .hook = ipv4_confirm,
258 .owner = THIS_MODULE,
259 .pf = PF_INET,
260 .hooknum = NF_IP_LOCAL_IN,
261 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
265 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
266 static int log_invalid_proto_min = 0;
267 static int log_invalid_proto_max = 255;
269 static ctl_table ip_ct_sysctl_table[] = {
271 .ctl_name = NET_IPV4_NF_CONNTRACK_MAX,
272 .procname = "ip_conntrack_max",
273 .data = &nf_conntrack_max,
274 .maxlen = sizeof(int),
275 .mode = 0644,
276 .proc_handler = &proc_dointvec,
279 .ctl_name = NET_IPV4_NF_CONNTRACK_COUNT,
280 .procname = "ip_conntrack_count",
281 .data = &nf_conntrack_count,
282 .maxlen = sizeof(int),
283 .mode = 0444,
284 .proc_handler = &proc_dointvec,
287 .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS,
288 .procname = "ip_conntrack_buckets",
289 .data = &nf_conntrack_htable_size,
290 .maxlen = sizeof(unsigned int),
291 .mode = 0444,
292 .proc_handler = &proc_dointvec,
295 .ctl_name = NET_IPV4_NF_CONNTRACK_CHECKSUM,
296 .procname = "ip_conntrack_checksum",
297 .data = &nf_conntrack_checksum,
298 .maxlen = sizeof(int),
299 .mode = 0644,
300 .proc_handler = &proc_dointvec,
303 .ctl_name = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
304 .procname = "ip_conntrack_log_invalid",
305 .data = &nf_ct_log_invalid,
306 .maxlen = sizeof(unsigned int),
307 .mode = 0644,
308 .proc_handler = &proc_dointvec_minmax,
309 .strategy = &sysctl_intvec,
310 .extra1 = &log_invalid_proto_min,
311 .extra2 = &log_invalid_proto_max,
313 #if defined(CONFIG_BCM_NAT) || defined(CONFIG_BCM_NAT_MODULE) || defined(HNDCTF)
315 .ctl_name = NET_IPV4_CONNTRACK_FASTNAT,
316 .procname = "ip_conntrack_fastnat",
317 .data = &ipv4_conntrack_fastnat,
318 .maxlen = sizeof(int),
319 .mode = 0644,
320 .proc_handler = &proc_dointvec,
322 #endif
324 .ctl_name = 0
327 #endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
329 /* Fast function for those who don't want to parse /proc (and I don't
330 blame them). */
331 /* Reversing the socket's dst/src point of view gives us the reply
332 mapping. */
333 static int
334 getorigdst(struct sock *sk, int optval, void __user *user, int *len)
336 struct inet_sock *inet = inet_sk(sk);
337 struct nf_conntrack_tuple_hash *h;
338 struct nf_conntrack_tuple tuple;
340 memset(&tuple, 0, sizeof(tuple));
341 tuple.src.u3.ip = inet->rcv_saddr;
342 tuple.src.u.tcp.port = inet->sport;
343 tuple.dst.u3.ip = inet->daddr;
344 tuple.dst.u.tcp.port = inet->dport;
345 tuple.src.l3num = PF_INET;
346 tuple.dst.protonum = IPPROTO_TCP;
348 /* We only do TCP at the moment: is there a better way? */
349 if (strcmp(sk->sk_prot->name, "TCP")) {
350 DEBUGP("SO_ORIGINAL_DST: Not a TCP socket\n");
351 return -ENOPROTOOPT;
354 if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
355 DEBUGP("SO_ORIGINAL_DST: len %u not %u\n",
356 *len, sizeof(struct sockaddr_in));
357 return -EINVAL;
360 h = nf_conntrack_find_get(&tuple, NULL);
361 if (h) {
362 struct sockaddr_in sin;
363 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
365 sin.sin_family = AF_INET;
366 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
367 .tuple.dst.u.tcp.port;
368 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
369 .tuple.dst.u3.ip;
370 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
372 DEBUGP("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
373 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
374 nf_ct_put(ct);
375 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
376 return -EFAULT;
377 else
378 return 0;
380 DEBUGP("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
381 NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port),
382 NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port));
383 return -ENOENT;
386 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
388 #include <linux/netfilter/nfnetlink.h>
389 #include <linux/netfilter/nfnetlink_conntrack.h>
391 static int ipv4_tuple_to_nfattr(struct sk_buff *skb,
392 const struct nf_conntrack_tuple *tuple)
394 NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t),
395 &tuple->src.u3.ip);
396 NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t),
397 &tuple->dst.u3.ip);
398 return 0;
400 nfattr_failure:
401 return -1;
404 static const size_t cta_min_ip[CTA_IP_MAX] = {
405 [CTA_IP_V4_SRC-1] = sizeof(u_int32_t),
406 [CTA_IP_V4_DST-1] = sizeof(u_int32_t),
409 static int ipv4_nfattr_to_tuple(struct nfattr *tb[],
410 struct nf_conntrack_tuple *t)
412 if (!tb[CTA_IP_V4_SRC-1] || !tb[CTA_IP_V4_DST-1])
413 return -EINVAL;
415 if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
416 return -EINVAL;
418 t->src.u3.ip = *(__be32 *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
419 t->dst.u3.ip = *(__be32 *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
421 return 0;
423 #endif
425 static struct nf_sockopt_ops so_getorigdst = {
426 .pf = PF_INET,
427 .get_optmin = SO_ORIGINAL_DST,
428 .get_optmax = SO_ORIGINAL_DST+1,
429 .get = &getorigdst,
432 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 = {
433 .l3proto = PF_INET,
434 .name = "ipv4",
435 .pkt_to_tuple = ipv4_pkt_to_tuple,
436 .invert_tuple = ipv4_invert_tuple,
437 .print_tuple = ipv4_print_tuple,
438 .prepare = ipv4_prepare,
439 .get_features = ipv4_get_features,
440 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
441 .tuple_to_nfattr = ipv4_tuple_to_nfattr,
442 .nfattr_to_tuple = ipv4_nfattr_to_tuple,
443 #endif
444 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
445 .ctl_table_path = nf_net_ipv4_netfilter_sysctl_path,
446 .ctl_table = ip_ct_sysctl_table,
447 #endif
448 .me = THIS_MODULE,
451 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
452 MODULE_ALIAS("ip_conntrack");
453 MODULE_LICENSE("GPL");
455 static int __init nf_conntrack_l3proto_ipv4_init(void)
457 int ret = 0;
459 need_conntrack();
461 ret = nf_register_sockopt(&so_getorigdst);
462 if (ret < 0) {
463 printk(KERN_ERR "Unable to register netfilter socket option\n");
464 return ret;
467 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4);
468 if (ret < 0) {
469 printk("nf_conntrack_ipv4: can't register tcp.\n");
470 goto cleanup_sockopt;
473 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4);
474 if (ret < 0) {
475 printk("nf_conntrack_ipv4: can't register udp.\n");
476 goto cleanup_tcp;
479 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp);
480 if (ret < 0) {
481 printk("nf_conntrack_ipv4: can't register icmp.\n");
482 goto cleanup_udp;
485 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4);
486 if (ret < 0) {
487 printk("nf_conntrack_ipv4: can't register ipv4\n");
488 goto cleanup_icmp;
491 ret = nf_register_hooks(ipv4_conntrack_ops,
492 ARRAY_SIZE(ipv4_conntrack_ops));
493 if (ret < 0) {
494 printk("nf_conntrack_ipv4: can't register hooks.\n");
495 goto cleanup_ipv4;
497 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
498 ret = nf_conntrack_ipv4_compat_init();
499 if (ret < 0)
500 goto cleanup_hooks;
501 #endif
502 return ret;
503 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
504 cleanup_hooks:
505 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
506 #endif
507 cleanup_ipv4:
508 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
509 cleanup_icmp:
510 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
511 cleanup_udp:
512 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
513 cleanup_tcp:
514 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
515 cleanup_sockopt:
516 nf_unregister_sockopt(&so_getorigdst);
517 return ret;
520 static void __exit nf_conntrack_l3proto_ipv4_fini(void)
522 synchronize_net();
523 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
524 nf_conntrack_ipv4_compat_fini();
525 #endif
526 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
527 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
528 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
529 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
530 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
531 nf_unregister_sockopt(&so_getorigdst);
534 module_init(nf_conntrack_l3proto_ipv4_init);
535 module_exit(nf_conntrack_l3proto_ipv4_fini);