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