]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
pkt_sched: Add peek emulation for non-work-conserving qdiscs.
[net-next-2.6.git] / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4.c
CommitLineData
73e4022f 1
9fb9cbb1
YK
2/* (C) 1999-2001 Paul `Rusty' Russell
3 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9fb9cbb1
YK
8 */
9
9fb9cbb1
YK
10#include <linux/types.h>
11#include <linux/ip.h>
12#include <linux/netfilter.h>
13#include <linux/module.h>
14#include <linux/skbuff.h>
15#include <linux/icmp.h>
16#include <linux/sysctl.h>
0ae2cfe7 17#include <net/route.h>
9fb9cbb1
YK
18#include <net/ip.h>
19
20#include <linux/netfilter_ipv4.h>
21#include <net/netfilter/nf_conntrack.h>
22#include <net/netfilter/nf_conntrack_helper.h>
605dcad6 23#include <net/netfilter/nf_conntrack_l4proto.h>
9fb9cbb1
YK
24#include <net/netfilter/nf_conntrack_l3proto.h>
25#include <net/netfilter/nf_conntrack_core.h>
26#include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
dd13b010 27#include <net/netfilter/nf_nat_helper.h>
73e4022f 28#include <net/netfilter/ipv4/nf_defrag_ipv4.h>
dd13b010
PM
29
30int (*nf_nat_seq_adjust_hook)(struct sk_buff *skb,
31 struct nf_conn *ct,
32 enum ip_conntrack_info ctinfo);
33EXPORT_SYMBOL_GPL(nf_nat_seq_adjust_hook);
9fb9cbb1 34
8ce8439a
JE
35static bool ipv4_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
36 struct nf_conntrack_tuple *tuple)
9fb9cbb1 37{
32948588
JE
38 const __be32 *ap;
39 __be32 _addrs[2];
9fb9cbb1
YK
40 ap = skb_header_pointer(skb, nhoff + offsetof(struct iphdr, saddr),
41 sizeof(u_int32_t) * 2, _addrs);
42 if (ap == NULL)
8ce8439a 43 return false;
9fb9cbb1
YK
44
45 tuple->src.u3.ip = ap[0];
46 tuple->dst.u3.ip = ap[1];
47
8ce8439a 48 return true;
9fb9cbb1
YK
49}
50
8ce8439a
JE
51static bool ipv4_invert_tuple(struct nf_conntrack_tuple *tuple,
52 const struct nf_conntrack_tuple *orig)
9fb9cbb1
YK
53{
54 tuple->src.u3.ip = orig->dst.u3.ip;
55 tuple->dst.u3.ip = orig->src.u3.ip;
56
8ce8439a 57 return true;
9fb9cbb1
YK
58}
59
60static int ipv4_print_tuple(struct seq_file *s,
61 const struct nf_conntrack_tuple *tuple)
62{
63 return seq_printf(s, "src=%u.%u.%u.%u dst=%u.%u.%u.%u ",
e905a9ed 64 NIPQUAD(tuple->src.u3.ip),
9fb9cbb1
YK
65 NIPQUAD(tuple->dst.u3.ip));
66}
67
ffc30690
YK
68static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
69 unsigned int *dataoff, u_int8_t *protonum)
9fb9cbb1 70{
32948588
JE
71 const struct iphdr *iph;
72 struct iphdr _iph;
ffc30690
YK
73
74 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
75 if (iph == NULL)
76 return -NF_DROP;
77
0fb96701
PM
78 /* Conntrack defragments packets, we might still see fragments
79 * inside ICMP packets though. */
80 if (iph->frag_off & htons(IP_OFFSET))
9fb9cbb1 81 return -NF_DROP;
9fb9cbb1 82
ffc30690
YK
83 *dataoff = nhoff + (iph->ihl << 2);
84 *protonum = iph->protocol;
9fb9cbb1
YK
85
86 return NF_ACCEPT;
87}
88
9fb9cbb1 89static unsigned int ipv4_confirm(unsigned int hooknum,
3db05fea 90 struct sk_buff *skb,
9fb9cbb1
YK
91 const struct net_device *in,
92 const struct net_device *out,
93 int (*okfn)(struct sk_buff *))
9fb9cbb1
YK
94{
95 struct nf_conn *ct;
96 enum ip_conntrack_info ctinfo;
32948588
JE
97 const struct nf_conn_help *help;
98 const struct nf_conntrack_helper *helper;
dd13b010 99 unsigned int ret;
9fb9cbb1
YK
100
101 /* This is where we call the helper: as the packet goes out. */
3db05fea 102 ct = nf_ct_get(skb, &ctinfo);
6442f1cf 103 if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
dd13b010 104 goto out;
dc808fe2
HW
105
106 help = nfct_help(ct);
3c158f7f 107 if (!help)
dd13b010
PM
108 goto out;
109
3c158f7f
PM
110 /* rcu_read_lock()ed by nf_hook_slow */
111 helper = rcu_dereference(help->helper);
112 if (!helper)
dd13b010
PM
113 goto out;
114
115 ret = helper->help(skb, skb_network_offset(skb) + ip_hdrlen(skb),
116 ct, ctinfo);
117 if (ret != NF_ACCEPT)
118 return ret;
119
120 if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status)) {
121 typeof(nf_nat_seq_adjust_hook) seq_adjust;
122
123 seq_adjust = rcu_dereference(nf_nat_seq_adjust_hook);
124 if (!seq_adjust || !seq_adjust(skb, ct, ctinfo))
125 return NF_DROP;
126 }
127out:
128 /* We've seen it coming out the other side: confirm it */
129 return nf_conntrack_confirm(skb);
9fb9cbb1
YK
130}
131
9fb9cbb1 132static unsigned int ipv4_conntrack_in(unsigned int hooknum,
3db05fea 133 struct sk_buff *skb,
9fb9cbb1
YK
134 const struct net_device *in,
135 const struct net_device *out,
136 int (*okfn)(struct sk_buff *))
137{
a702a65f 138 return nf_conntrack_in(dev_net(in), PF_INET, hooknum, skb);
9fb9cbb1
YK
139}
140
141static unsigned int ipv4_conntrack_local(unsigned int hooknum,
3db05fea 142 struct sk_buff *skb,
e905a9ed
YH
143 const struct net_device *in,
144 const struct net_device *out,
145 int (*okfn)(struct sk_buff *))
9fb9cbb1
YK
146{
147 /* root is playing with raw sockets. */
3db05fea
HX
148 if (skb->len < sizeof(struct iphdr) ||
149 ip_hdrlen(skb) < sizeof(struct iphdr)) {
9fb9cbb1
YK
150 if (net_ratelimit())
151 printk("ipt_hook: happy cracking.\n");
152 return NF_ACCEPT;
153 }
a702a65f 154 return nf_conntrack_in(dev_net(out), PF_INET, hooknum, skb);
9fb9cbb1
YK
155}
156
157/* Connection tracking may drop packets, but never alters them, so
158 make it the first hook. */
1999414a 159static struct nf_hook_ops ipv4_conntrack_ops[] __read_mostly = {
964ddaa1
PM
160 {
161 .hook = ipv4_conntrack_in,
162 .owner = THIS_MODULE,
163 .pf = PF_INET,
6e23ae2a 164 .hooknum = NF_INET_PRE_ROUTING,
964ddaa1
PM
165 .priority = NF_IP_PRI_CONNTRACK,
166 },
964ddaa1
PM
167 {
168 .hook = ipv4_conntrack_local,
169 .owner = THIS_MODULE,
170 .pf = PF_INET,
6e23ae2a 171 .hooknum = NF_INET_LOCAL_OUT,
964ddaa1
PM
172 .priority = NF_IP_PRI_CONNTRACK,
173 },
964ddaa1
PM
174 {
175 .hook = ipv4_confirm,
176 .owner = THIS_MODULE,
177 .pf = PF_INET,
6e23ae2a 178 .hooknum = NF_INET_POST_ROUTING,
964ddaa1
PM
179 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
180 },
181 {
182 .hook = ipv4_confirm,
183 .owner = THIS_MODULE,
184 .pf = PF_INET,
6e23ae2a 185 .hooknum = NF_INET_LOCAL_IN,
964ddaa1
PM
186 .priority = NF_IP_PRI_CONNTRACK_CONFIRM,
187 },
9fb9cbb1
YK
188};
189
a999e683
PM
190#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
191static int log_invalid_proto_min = 0;
192static int log_invalid_proto_max = 255;
193
194static ctl_table ip_ct_sysctl_table[] = {
195 {
196 .ctl_name = NET_IPV4_NF_CONNTRACK_MAX,
197 .procname = "ip_conntrack_max",
198 .data = &nf_conntrack_max,
199 .maxlen = sizeof(int),
200 .mode = 0644,
201 .proc_handler = &proc_dointvec,
202 },
203 {
204 .ctl_name = NET_IPV4_NF_CONNTRACK_COUNT,
205 .procname = "ip_conntrack_count",
49ac8713 206 .data = &init_net.ct.count,
a999e683
PM
207 .maxlen = sizeof(int),
208 .mode = 0444,
209 .proc_handler = &proc_dointvec,
210 },
211 {
212 .ctl_name = NET_IPV4_NF_CONNTRACK_BUCKETS,
213 .procname = "ip_conntrack_buckets",
214 .data = &nf_conntrack_htable_size,
215 .maxlen = sizeof(unsigned int),
216 .mode = 0444,
217 .proc_handler = &proc_dointvec,
218 },
219 {
220 .ctl_name = NET_IPV4_NF_CONNTRACK_CHECKSUM,
221 .procname = "ip_conntrack_checksum",
c04d0552 222 .data = &init_net.ct.sysctl_checksum,
a999e683
PM
223 .maxlen = sizeof(int),
224 .mode = 0644,
225 .proc_handler = &proc_dointvec,
226 },
227 {
228 .ctl_name = NET_IPV4_NF_CONNTRACK_LOG_INVALID,
229 .procname = "ip_conntrack_log_invalid",
c2a2c7e0 230 .data = &init_net.ct.sysctl_log_invalid,
a999e683
PM
231 .maxlen = sizeof(unsigned int),
232 .mode = 0644,
233 .proc_handler = &proc_dointvec_minmax,
234 .strategy = &sysctl_intvec,
235 .extra1 = &log_invalid_proto_min,
236 .extra2 = &log_invalid_proto_max,
237 },
238 {
239 .ctl_name = 0
240 }
241};
242#endif /* CONFIG_SYSCTL && CONFIG_NF_CONNTRACK_PROC_COMPAT */
243
9fb9cbb1
YK
244/* Fast function for those who don't want to parse /proc (and I don't
245 blame them). */
246/* Reversing the socket's dst/src point of view gives us the reply
247 mapping. */
248static int
249getorigdst(struct sock *sk, int optval, void __user *user, int *len)
250{
32948588
JE
251 const struct inet_sock *inet = inet_sk(sk);
252 const struct nf_conntrack_tuple_hash *h;
9fb9cbb1 253 struct nf_conntrack_tuple tuple;
e905a9ed 254
443a70d5 255 memset(&tuple, 0, sizeof(tuple));
9fb9cbb1
YK
256 tuple.src.u3.ip = inet->rcv_saddr;
257 tuple.src.u.tcp.port = inet->sport;
258 tuple.dst.u3.ip = inet->daddr;
259 tuple.dst.u.tcp.port = inet->dport;
260 tuple.src.l3num = PF_INET;
261 tuple.dst.protonum = IPPROTO_TCP;
262
263 /* We only do TCP at the moment: is there a better way? */
264 if (strcmp(sk->sk_prot->name, "TCP")) {
0d53778e 265 pr_debug("SO_ORIGINAL_DST: Not a TCP socket\n");
9fb9cbb1
YK
266 return -ENOPROTOOPT;
267 }
268
269 if ((unsigned int) *len < sizeof(struct sockaddr_in)) {
0d53778e
PM
270 pr_debug("SO_ORIGINAL_DST: len %d not %Zu\n",
271 *len, sizeof(struct sockaddr_in));
9fb9cbb1
YK
272 return -EINVAL;
273 }
274
400dad39 275 h = nf_conntrack_find_get(sock_net(sk), &tuple);
9fb9cbb1
YK
276 if (h) {
277 struct sockaddr_in sin;
278 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
279
280 sin.sin_family = AF_INET;
281 sin.sin_port = ct->tuplehash[IP_CT_DIR_ORIGINAL]
282 .tuple.dst.u.tcp.port;
283 sin.sin_addr.s_addr = ct->tuplehash[IP_CT_DIR_ORIGINAL]
284 .tuple.dst.u3.ip;
6c813c3f 285 memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
9fb9cbb1 286
0d53778e
PM
287 pr_debug("SO_ORIGINAL_DST: %u.%u.%u.%u %u\n",
288 NIPQUAD(sin.sin_addr.s_addr), ntohs(sin.sin_port));
9fb9cbb1
YK
289 nf_ct_put(ct);
290 if (copy_to_user(user, &sin, sizeof(sin)) != 0)
291 return -EFAULT;
292 else
293 return 0;
294 }
0d53778e
PM
295 pr_debug("SO_ORIGINAL_DST: Can't find %u.%u.%u.%u/%u-%u.%u.%u.%u/%u.\n",
296 NIPQUAD(tuple.src.u3.ip), ntohs(tuple.src.u.tcp.port),
297 NIPQUAD(tuple.dst.u3.ip), ntohs(tuple.dst.u.tcp.port));
9fb9cbb1
YK
298 return -ENOENT;
299}
300
e281db5c 301#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
c1d10adb
PNA
302
303#include <linux/netfilter/nfnetlink.h>
304#include <linux/netfilter/nfnetlink_conntrack.h>
305
fdf70832 306static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
c1d10adb
PNA
307 const struct nf_conntrack_tuple *tuple)
308{
77236b6e
PM
309 NLA_PUT_BE32(skb, CTA_IP_V4_SRC, tuple->src.u3.ip);
310 NLA_PUT_BE32(skb, CTA_IP_V4_DST, tuple->dst.u3.ip);
c1d10adb
PNA
311 return 0;
312
df6fb868 313nla_put_failure:
c1d10adb
PNA
314 return -1;
315}
316
f73e924c
PM
317static const struct nla_policy ipv4_nla_policy[CTA_IP_MAX+1] = {
318 [CTA_IP_V4_SRC] = { .type = NLA_U32 },
319 [CTA_IP_V4_DST] = { .type = NLA_U32 },
c1d10adb
PNA
320};
321
fdf70832 322static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
c1d10adb
PNA
323 struct nf_conntrack_tuple *t)
324{
df6fb868 325 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
c1d10adb
PNA
326 return -EINVAL;
327
77236b6e
PM
328 t->src.u3.ip = nla_get_be32(tb[CTA_IP_V4_SRC]);
329 t->dst.u3.ip = nla_get_be32(tb[CTA_IP_V4_DST]);
c1d10adb
PNA
330
331 return 0;
332}
333#endif
334
9fb9cbb1
YK
335static struct nf_sockopt_ops so_getorigdst = {
336 .pf = PF_INET,
337 .get_optmin = SO_ORIGINAL_DST,
338 .get_optmax = SO_ORIGINAL_DST+1,
339 .get = &getorigdst,
16fcec35 340 .owner = THIS_MODULE,
9fb9cbb1
YK
341};
342
61075af5 343struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 __read_mostly = {
9fb9cbb1
YK
344 .l3proto = PF_INET,
345 .name = "ipv4",
346 .pkt_to_tuple = ipv4_pkt_to_tuple,
347 .invert_tuple = ipv4_invert_tuple,
348 .print_tuple = ipv4_print_tuple,
ffc30690 349 .get_l4proto = ipv4_get_l4proto,
e281db5c 350#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
fdf70832
PM
351 .tuple_to_nlattr = ipv4_tuple_to_nlattr,
352 .nlattr_to_tuple = ipv4_nlattr_to_tuple,
f73e924c 353 .nla_policy = ipv4_nla_policy,
a999e683
PM
354#endif
355#if defined(CONFIG_SYSCTL) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
356 .ctl_table_path = nf_net_ipv4_netfilter_sysctl_path,
357 .ctl_table = ip_ct_sysctl_table,
c1d10adb 358#endif
9fb9cbb1
YK
359 .me = THIS_MODULE,
360};
361
fae718dd
PM
362module_param_call(hashsize, nf_conntrack_set_hashsize, param_get_uint,
363 &nf_conntrack_htable_size, 0600);
364
32292a7f 365MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET));
d2483dde 366MODULE_ALIAS("ip_conntrack");
32292a7f
PM
367MODULE_LICENSE("GPL");
368
369static int __init nf_conntrack_l3proto_ipv4_init(void)
9fb9cbb1
YK
370{
371 int ret = 0;
372
32292a7f 373 need_conntrack();
73e4022f 374 nf_defrag_ipv4_enable();
9fb9cbb1
YK
375
376 ret = nf_register_sockopt(&so_getorigdst);
377 if (ret < 0) {
378 printk(KERN_ERR "Unable to register netfilter socket option\n");
32292a7f 379 return ret;
9fb9cbb1
YK
380 }
381
605dcad6 382 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp4);
9fb9cbb1
YK
383 if (ret < 0) {
384 printk("nf_conntrack_ipv4: can't register tcp.\n");
385 goto cleanup_sockopt;
386 }
387
605dcad6 388 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp4);
9fb9cbb1
YK
389 if (ret < 0) {
390 printk("nf_conntrack_ipv4: can't register udp.\n");
391 goto cleanup_tcp;
392 }
393
605dcad6 394 ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmp);
9fb9cbb1
YK
395 if (ret < 0) {
396 printk("nf_conntrack_ipv4: can't register icmp.\n");
397 goto cleanup_udp;
398 }
399
400 ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv4);
401 if (ret < 0) {
402 printk("nf_conntrack_ipv4: can't register ipv4\n");
403 goto cleanup_icmp;
404 }
405
964ddaa1
PM
406 ret = nf_register_hooks(ipv4_conntrack_ops,
407 ARRAY_SIZE(ipv4_conntrack_ops));
9fb9cbb1 408 if (ret < 0) {
964ddaa1 409 printk("nf_conntrack_ipv4: can't register hooks.\n");
9fb9cbb1
YK
410 goto cleanup_ipv4;
411 }
e4bd8bce
PM
412#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
413 ret = nf_conntrack_ipv4_compat_init();
414 if (ret < 0)
415 goto cleanup_hooks;
416#endif
9fb9cbb1 417 return ret;
e4bd8bce
PM
418#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
419 cleanup_hooks:
e905a9ed 420 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
e4bd8bce 421#endif
9fb9cbb1
YK
422 cleanup_ipv4:
423 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
424 cleanup_icmp:
605dcad6 425 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
9fb9cbb1 426 cleanup_udp:
605dcad6 427 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
9fb9cbb1 428 cleanup_tcp:
605dcad6 429 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
9fb9cbb1
YK
430 cleanup_sockopt:
431 nf_unregister_sockopt(&so_getorigdst);
9fb9cbb1
YK
432 return ret;
433}
434
65b4b4e8 435static void __exit nf_conntrack_l3proto_ipv4_fini(void)
9fb9cbb1 436{
32292a7f 437 synchronize_net();
e4bd8bce
PM
438#if defined(CONFIG_PROC_FS) && defined(CONFIG_NF_CONNTRACK_PROC_COMPAT)
439 nf_conntrack_ipv4_compat_fini();
440#endif
32292a7f
PM
441 nf_unregister_hooks(ipv4_conntrack_ops, ARRAY_SIZE(ipv4_conntrack_ops));
442 nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv4);
605dcad6
MJ
443 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmp);
444 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp4);
445 nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp4);
32292a7f 446 nf_unregister_sockopt(&so_getorigdst);
9fb9cbb1
YK
447}
448
65b4b4e8
AM
449module_init(nf_conntrack_l3proto_ipv4_init);
450module_exit(nf_conntrack_l3proto_ipv4_fini);
591e6206
PM
451
452void need_ipv4_conntrack(void)
453{
454 return;
455}
456EXPORT_SYMBOL_GPL(need_ipv4_conntrack);