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