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