[IP]: Introduce ip_hdrlen()
[pohmelfs.git] / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4.c
blobfa14eb77f9b687f6cbe7f069757d5a50d594234a
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
12 * structures.
14 * Derived from net/ipv4/netfilter/ip_conntrack_standalone.c
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>
24 #include <net/route.h>
25 #include <net/ip.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>
35 #if 0
36 #define DEBUGP printk
37 #else
38 #define DEBUGP(format, args...)
39 #endif
41 static int ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
42 struct nf_conntrack_tuple *tuple)
44 __be32 _addrs[2], *ap;
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;
50 tuple->src.u3.ip = ap[0];
51 tuple->dst.u3.ip = ap[1];
53 return 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;
62 return 1;
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)
76 return 0;
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)
83 skb_orphan(skb);
85 local_bh_disable();
86 skb = ip_defrag(skb, user);
87 local_bh_enable();
89 if (skb)
90 ip_send_check(skb->nh.iph);
92 return skb;
95 static int
96 ipv4_prepare(struct sk_buff **pskb, unsigned int hooknum, unsigned int *dataoff,
97 u_int8_t *protonum)
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);
105 return -NF_DROP;
108 *dataoff = skb_network_offset(*pskb) + ip_hdrlen(*pskb);
109 *protonum = (*pskb)->nh.iph->protocol;
111 return NF_ACCEPT;
114 int nf_nat_module_is_loaded = 0;
115 EXPORT_SYMBOL_GPL(nf_nat_module_is_loaded);
117 static u_int32_t ipv4_get_features(const struct nf_conntrack_tuple *tuple)
119 if (nf_nat_module_is_loaded)
120 return NF_CT_F_NAT;
122 return NF_CT_F_BASIC;
125 static 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 *))
131 /* We've seen it coming out the other side: confirm it */
132 return nf_conntrack_confirm(pskb);
135 static 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 *))
141 struct nf_conn *ct;
142 enum ip_conntrack_info ctinfo;
143 struct nf_conn_help *help;
145 /* This is where we call the helper: as the packet goes out. */
146 ct = nf_ct_get(*pskb, &ctinfo);
147 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
148 return NF_ACCEPT;
150 help = nfct_help(ct);
151 if (!help || !help->helper)
152 return NF_ACCEPT;
154 return help->helper->help(pskb,
155 skb_network_offset(*pskb) + ip_hdrlen(*pskb),
156 ct, ctinfo);
159 static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
160 struct sk_buff **pskb,
161 const struct net_device *in,
162 const struct net_device *out,
163 int (*okfn)(struct sk_buff *))
165 #if !defined(CONFIG_IP_NF_NAT) && !defined(CONFIG_IP_NF_NAT_MODULE)
166 /* Previously seen (loopback)? Ignore. Do this before
167 fragment check. */
168 if ((*pskb)->nfct)
169 return NF_ACCEPT;
170 #endif
172 /* Gather fragments. */
173 if ((*pskb)->nh.iph->frag_off & htons(IP_MF|IP_OFFSET)) {
174 *pskb = nf_ct_ipv4_gather_frags(*pskb,
175 hooknum == NF_IP_PRE_ROUTING ?
176 IP_DEFRAG_CONNTRACK_IN :
177 IP_DEFRAG_CONNTRACK_OUT);
178 if (!*pskb)
179 return NF_STOLEN;
181 return NF_ACCEPT;
184 static unsigned int ipv4_conntrack_in(unsigned int hooknum,
185 struct sk_buff **pskb,
186 const struct net_device *in,
187 const struct net_device *out,
188 int (*okfn)(struct sk_buff *))
190 return nf_conntrack_in(PF_INET, hooknum, pskb);
193 static unsigned int ipv4_conntrack_local(unsigned int hooknum,
194 struct sk_buff **pskb,
195 const struct net_device *in,
196 const struct net_device *out,
197 int (*okfn)(struct sk_buff *))
199 /* root is playing with raw sockets. */
200 if ((*pskb)->len < sizeof(struct iphdr)
201 || ip_hdrlen(*pskb) < sizeof(struct iphdr)) {
202 if (net_ratelimit())
203 printk("ipt_hook: happy cracking.\n");
204 return NF_ACCEPT;
206 return nf_conntrack_in(PF_INET, hooknum, pskb);
209 /* Connection tracking may drop packets, but never alters them, so
210 make it the first hook. */
211 static struct nf_hook_ops ipv4_conntrack_ops[] = {
213 .hook = ipv4_conntrack_defrag,
214 .owner = THIS_MODULE,
215 .pf = PF_INET,
216 .hooknum = NF_IP_PRE_ROUTING,
217 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
220 .hook = ipv4_conntrack_in,
221 .owner = THIS_MODULE,
222 .pf = PF_INET,
223 .hooknum = NF_IP_PRE_ROUTING,
224 .priority = NF_IP_PRI_CONNTRACK,
227 .hook = ipv4_conntrack_defrag,
228 .owner = THIS_MODULE,
229 .pf = PF_INET,
230 .hooknum = NF_IP_LOCAL_OUT,
231 .priority = NF_IP_PRI_CONNTRACK_DEFRAG,
234 .hook = ipv4_conntrack_local,
235 .owner = THIS_MODULE,
236 .pf = PF_INET,
237 .hooknum = NF_IP_LOCAL_OUT,
238 .priority = NF_IP_PRI_CONNTRACK,
241 .hook = ipv4_conntrack_help,
242 .owner = THIS_MODULE,
243 .pf = PF_INET,
244 .hooknum = NF_IP_POST_ROUTING,
245 .priority = NF_IP_PRI_CONNTRACK_HELPER,
248 .hook = ipv4_conntrack_help,
249 .owner = THIS_MODULE,
250 .pf = PF_INET,
251 .hooknum = NF_IP_LOCAL_IN,
252 .priority = NF_IP_PRI_CONNTRACK_HELPER,
255 .hook = ipv4_confirm,
256 .owner = THIS_MODULE,
257 .pf = PF_INET,
258 .hooknum = NF_IP_POST_ROUTING,
259 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
262 .hook = ipv4_confirm,
263 .owner = THIS_MODULE,
264 .pf = PF_INET,
265 .hooknum = NF_IP_LOCAL_IN,
266 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
270 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
271 static int log_invalid_proto_min = 0;
272 static int log_invalid_proto_max = 255;
274 static ctl_table ip_ct_sysctl_table[] = {
276 .ctl_name = NET_IPV4_NF_CONNTRACK_MAX,
277 .procname = "ip_conntrack_max",
278 .data = &nf_conntrack_max,
279 .maxlen = sizeof(int),
280 .mode = 0644,
281 .proc_handler = &proc_dointvec,
284 .ctl_name = NET_IPV4_NF_CONNTRACK_COUNT,
285 .procname = "ip_conntrack_count",
286 .data = &nf_conntrack_count,
287 .maxlen = sizeof(int),
288 .mode = 0444,
289 .proc_handler = &proc_dointvec,
292 .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS,
293 .procname = "ip_conntrack_buckets",
294 .data = &nf_conntrack_htable_size,
295 .maxlen = sizeof(unsigned int),
296 .mode = 0444,
297 .proc_handler = &proc_dointvec,
300 .ctl_name = NET_IPV4_NF_CONNTRACK_CHECKSUM,
301 .procname = "ip_conntrack_checksum",
302 .data = &nf_conntrack_checksum,
303 .maxlen = sizeof(int),
304 .mode = 0644,
305 .proc_handler = &proc_dointvec,
308 .ctl_name = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
309 .procname = "ip_conntrack_log_invalid",
310 .data = &nf_ct_log_invalid,
311 .maxlen = sizeof(unsigned int),
312 .mode = 0644,
313 .proc_handler = &proc_dointvec_minmax,
314 .strategy = &sysctl_intvec,
315 .extra1 = &log_invalid_proto_min,
316 .extra2 = &log_invalid_proto_max,
319 .ctl_name = 0
322 #endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
324 /* Fast function for those who don't want to parse /proc (and I don't
325 blame them). */
326 /* Reversing the socket's dst/src point of view gives us the reply
327 mapping. */
328 static int
329 getorigdst(struct sock *sk, int optval, void __user *user, int *len)
331 struct inet_sock *inet = inet_sk(sk);
332 struct nf_conntrack_tuple_hash *h;
333 struct nf_conntrack_tuple tuple;
335 NF_CT_TUPLE_U_BLANK(&tuple);
336 tuple.src.u3.ip = inet->rcv_saddr;
337 tuple.src.u.tcp.port = inet->sport;
338 tuple.dst.u3.ip = inet->daddr;
339 tuple.dst.u.tcp.port = inet->dport;
340 tuple.src.l3num = PF_INET;
341 tuple.dst.protonum = IPPROTO_TCP;
343 /* We only do TCP at the moment: is there a better way? */
344 if (strcmp(sk->sk_prot->name, "TCP")) {
345 DEBUGP("SO_ORIGINAL_DST: Not a TCP socket\n");
346 return -ENOPROTOOPT;
349 if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
350 DEBUGP("SO_ORIGINAL_DST: len %u not %u\n",
351 *len, sizeof(struct sockaddr_in));
352 return -EINVAL;
355 h = nf_conntrack_find_get(&tuple, NULL);
356 if (h) {
357 struct sockaddr_in sin;
358 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
360 sin.sin_family = AF_INET;
361 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
362 .tuple.dst.u.tcp.port;
363 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
364 .tuple.dst.u3.ip;
365 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
367 DEBUGP("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
368 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
369 nf_ct_put(ct);
370 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
371 return -EFAULT;
372 else
373 return 0;
375 DEBUGP("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
376 NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port),
377 NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port));
378 return -ENOENT;
381 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
383 #include <linux/netfilter/nfnetlink.h>
384 #include <linux/netfilter/nfnetlink_conntrack.h>
386 static int ipv4_tuple_to_nfattr(struct sk_buff *skb,
387 const struct nf_conntrack_tuple *tuple)
389 NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t),
390 &tuple->src.u3.ip);
391 NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t),
392 &tuple->dst.u3.ip);
393 return 0;
395 nfattr_failure:
396 return -1;
399 static const size_t cta_min_ip[CTA_IP_MAX] = {
400 [CTA_IP_V4_SRC-1] = sizeof(u_int32_t),
401 [CTA_IP_V4_DST-1] = sizeof(u_int32_t),
404 static int ipv4_nfattr_to_tuple(struct nfattr *tb[],
405 struct nf_conntrack_tuple *t)
407 if (!tb[CTA_IP_V4_SRC-1] || !tb[CTA_IP_V4_DST-1])
408 return -EINVAL;
410 if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
411 return -EINVAL;
413 t->src.u3.ip = *(__be32 *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
414 t->dst.u3.ip = *(__be32 *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
416 return 0;
418 #endif
420 static struct nf_sockopt_ops so_getorigdst = {
421 .pf = PF_INET,
422 .get_optmin = SO_ORIGINAL_DST,
423 .get_optmax = SO_ORIGINAL_DST+1,
424 .get = &getorigdst,
427 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 = {
428 .l3proto = PF_INET,
429 .name = "ipv4",
430 .pkt_to_tuple = ipv4_pkt_to_tuple,
431 .invert_tuple = ipv4_invert_tuple,
432 .print_tuple = ipv4_print_tuple,
433 .print_conntrack = ipv4_print_conntrack,
434 .prepare = ipv4_prepare,
435 .get_features = ipv4_get_features,
436 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
437 .tuple_to_nfattr = ipv4_tuple_to_nfattr,
438 .nfattr_to_tuple = ipv4_nfattr_to_tuple,
439 #endif
440 #if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
441 .ctl_table_path = nf_net_ipv4_netfilter_sysctl_path,
442 .ctl_table = ip_ct_sysctl_table,
443 #endif
444 .me = THIS_MODULE,
447 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
448 MODULE_ALIAS("ip_conntrack");
449 MODULE_LICENSE("GPL");
451 static int __init nf_conntrack_l3proto_ipv4_init(void)
453 int ret = 0;
455 need_conntrack();
457 ret = nf_register_sockopt(&so_getorigdst);
458 if (ret < 0) {
459 printk(KERN_ERR "Unable to register netfilter socket option\n");
460 return ret;
463 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4);
464 if (ret < 0) {
465 printk("nf_conntrack_ipv4: can't register tcp.\n");
466 goto cleanup_sockopt;
469 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4);
470 if (ret < 0) {
471 printk("nf_conntrack_ipv4: can't register udp.\n");
472 goto cleanup_tcp;
475 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp);
476 if (ret < 0) {
477 printk("nf_conntrack_ipv4: can't register icmp.\n");
478 goto cleanup_udp;
481 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4);
482 if (ret < 0) {
483 printk("nf_conntrack_ipv4: can't register ipv4\n");
484 goto cleanup_icmp;
487 ret = nf_register_hooks(ipv4_conntrack_ops,
488 ARRAY_SIZE(ipv4_conntrack_ops));
489 if (ret < 0) {
490 printk("nf_conntrack_ipv4: can't register hooks.\n");
491 goto cleanup_ipv4;
493 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
494 ret = nf_conntrack_ipv4_compat_init();
495 if (ret < 0)
496 goto cleanup_hooks;
497 #endif
498 return ret;
499 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
500 cleanup_hooks:
501 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
502 #endif
503 cleanup_ipv4:
504 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
505 cleanup_icmp:
506 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
507 cleanup_udp:
508 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
509 cleanup_tcp:
510 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
511 cleanup_sockopt:
512 nf_unregister_sockopt(&so_getorigdst);
513 return ret;
516 static void __exit nf_conntrack_l3proto_ipv4_fini(void)
518 synchronize_net();
519 #if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
520 nf_conntrack_ipv4_compat_fini();
521 #endif
522 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
523 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
524 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
525 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
526 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
527 nf_unregister_sockopt(&so_getorigdst);
530 module_init(nf_conntrack_l3proto_ipv4_init);
531 module_exit(nf_conntrack_l3proto_ipv4_fini);