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