[NETFILTER]: nf_log: move logging stuff to seperate header
[linux-2.6.git] / net / ipv6 / netfilter / nf_conntrack_proto_icmpv6.c
1 /*
2  * Copyright (C)2003,2004 USAGI/WIDE Project
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  * Author:
9  *      Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
10  */
11
12 #include <linux/types.h>
13 #include <linux/timer.h>
14 #include <linux/module.h>
15 #include <linux/netfilter.h>
16 #include <linux/in6.h>
17 #include <linux/icmpv6.h>
18 #include <linux/ipv6.h>
19 #include <net/ipv6.h>
20 #include <net/ip6_checksum.h>
21 #include <linux/seq_file.h>
22 #include <linux/netfilter_ipv6.h>
23 #include <net/netfilter/nf_conntrack_tuple.h>
24 #include <net/netfilter/nf_conntrack_l4proto.h>
25 #include <net/netfilter/nf_conntrack_core.h>
26 #include <net/netfilter/ipv6/nf_conntrack_icmpv6.h>
27 #include <net/netfilter/nf_log.h>
28
29 static unsigned long nf_ct_icmpv6_timeout __read_mostly = 30*HZ;
30
31 static int icmpv6_pkt_to_tuple(const struct sk_buff *skb,
32                                unsigned int dataoff,
33                                struct nf_conntrack_tuple *tuple)
34 {
35         struct icmp6hdr _hdr, *hp;
36
37         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
38         if (hp == NULL)
39                 return 0;
40         tuple->dst.u.icmp.type = hp->icmp6_type;
41         tuple->src.u.icmp.id = hp->icmp6_identifier;
42         tuple->dst.u.icmp.code = hp->icmp6_code;
43
44         return 1;
45 }
46
47 /* Add 1; spaces filled with 0. */
48 static u_int8_t invmap[] = {
49         [ICMPV6_ECHO_REQUEST - 128]     = ICMPV6_ECHO_REPLY + 1,
50         [ICMPV6_ECHO_REPLY - 128]       = ICMPV6_ECHO_REQUEST + 1,
51         [ICMPV6_NI_QUERY - 128]         = ICMPV6_NI_QUERY + 1,
52         [ICMPV6_NI_REPLY - 128]         = ICMPV6_NI_REPLY +1
53 };
54
55 static int icmpv6_invert_tuple(struct nf_conntrack_tuple *tuple,
56                                const struct nf_conntrack_tuple *orig)
57 {
58         int type = orig->dst.u.icmp.type - 128;
59         if (type < 0 || type >= sizeof(invmap) || !invmap[type])
60                 return 0;
61
62         tuple->src.u.icmp.id   = orig->src.u.icmp.id;
63         tuple->dst.u.icmp.type = invmap[type] - 1;
64         tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
65         return 1;
66 }
67
68 /* Print out the per-protocol part of the tuple. */
69 static int icmpv6_print_tuple(struct seq_file *s,
70                               const struct nf_conntrack_tuple *tuple)
71 {
72         return seq_printf(s, "type=%u code=%u id=%u ",
73                           tuple->dst.u.icmp.type,
74                           tuple->dst.u.icmp.code,
75                           ntohs(tuple->src.u.icmp.id));
76 }
77
78 /* Print out the private part of the conntrack. */
79 static int icmpv6_print_conntrack(struct seq_file *s,
80                                   const struct nf_conn *conntrack)
81 {
82         return 0;
83 }
84
85 /* Returns verdict for packet, or -1 for invalid. */
86 static int icmpv6_packet(struct nf_conn *ct,
87                        const struct sk_buff *skb,
88                        unsigned int dataoff,
89                        enum ip_conntrack_info ctinfo,
90                        int pf,
91                        unsigned int hooknum)
92 {
93         /* Try to delete connection immediately after all replies:
94            won't actually vanish as we still have skb, and del_timer
95            means this will only run once even if count hits zero twice
96            (theoretically possible with SMP) */
97         if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
98                 if (atomic_dec_and_test(&ct->proto.icmp.count)
99                     && del_timer(&ct->timeout))
100                         ct->timeout.function((unsigned long)ct);
101         } else {
102                 atomic_inc(&ct->proto.icmp.count);
103                 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
104                 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmpv6_timeout);
105         }
106
107         return NF_ACCEPT;
108 }
109
110 /* Called when a new connection for this protocol found. */
111 static int icmpv6_new(struct nf_conn *conntrack,
112                       const struct sk_buff *skb,
113                       unsigned int dataoff)
114 {
115         static u_int8_t valid_new[] = {
116                 [ICMPV6_ECHO_REQUEST - 128] = 1,
117                 [ICMPV6_NI_QUERY - 128] = 1
118         };
119         int type = conntrack->tuplehash[0].tuple.dst.u.icmp.type - 128;
120
121         if (type < 0 || type >= sizeof(valid_new) || !valid_new[type]) {
122                 /* Can't create a new ICMPv6 `conn' with this. */
123                 pr_debug("icmpv6: can't create new conn with type %u\n",
124                          type + 128);
125                 NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
126                 return 0;
127         }
128         atomic_set(&conntrack->proto.icmp.count, 0);
129         return 1;
130 }
131
132 static int
133 icmpv6_error_message(struct sk_buff *skb,
134                      unsigned int icmp6off,
135                      enum ip_conntrack_info *ctinfo,
136                      unsigned int hooknum)
137 {
138         struct nf_conntrack_tuple intuple, origtuple;
139         struct nf_conntrack_tuple_hash *h;
140         struct nf_conntrack_l4proto *inproto;
141
142         NF_CT_ASSERT(skb->nfct == NULL);
143
144         /* Are they talking about one of our connections? */
145         if (!nf_ct_get_tuplepr(skb,
146                                skb_network_offset(skb)
147                                 + sizeof(struct ipv6hdr)
148                                 + sizeof(struct icmp6hdr),
149                                PF_INET6, &origtuple)) {
150                 pr_debug("icmpv6_error: Can't get tuple\n");
151                 return -NF_ACCEPT;
152         }
153
154         /* rcu_read_lock()ed by nf_hook_slow */
155         inproto = __nf_ct_l4proto_find(PF_INET6, origtuple.dst.protonum);
156
157         /* Ordinarily, we'd expect the inverted tupleproto, but it's
158            been preserved inside the ICMP. */
159         if (!nf_ct_invert_tuple(&intuple, &origtuple,
160                                 &nf_conntrack_l3proto_ipv6, inproto)) {
161                 pr_debug("icmpv6_error: Can't invert tuple\n");
162                 return -NF_ACCEPT;
163         }
164
165         *ctinfo = IP_CT_RELATED;
166
167         h = nf_conntrack_find_get(&intuple);
168         if (!h) {
169                 pr_debug("icmpv6_error: no match\n");
170                 return -NF_ACCEPT;
171         } else {
172                 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
173                         *ctinfo += IP_CT_IS_REPLY;
174         }
175
176         /* Update skb to refer to this connection */
177         skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
178         skb->nfctinfo = *ctinfo;
179         return -NF_ACCEPT;
180 }
181
182 static int
183 icmpv6_error(struct sk_buff *skb, unsigned int dataoff,
184              enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
185 {
186         struct icmp6hdr _ih, *icmp6h;
187
188         icmp6h = skb_header_pointer(skb, dataoff, sizeof(_ih), &_ih);
189         if (icmp6h == NULL) {
190                 if (LOG_INVALID(IPPROTO_ICMPV6))
191                 nf_log_packet(PF_INET6, 0, skb, NULL, NULL, NULL,
192                               "nf_ct_icmpv6: short packet ");
193                 return -NF_ACCEPT;
194         }
195
196         if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
197             nf_ip6_checksum(skb, hooknum, dataoff, IPPROTO_ICMPV6)) {
198                 nf_log_packet(PF_INET6, 0, skb, NULL, NULL, NULL,
199                               "nf_ct_icmpv6: ICMPv6 checksum failed\n");
200                 return -NF_ACCEPT;
201         }
202
203         /* is not error message ? */
204         if (icmp6h->icmp6_type >= 128)
205                 return NF_ACCEPT;
206
207         return icmpv6_error_message(skb, dataoff, ctinfo, hooknum);
208 }
209
210 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
211
212 #include <linux/netfilter/nfnetlink.h>
213 #include <linux/netfilter/nfnetlink_conntrack.h>
214 static int icmpv6_tuple_to_nlattr(struct sk_buff *skb,
215                                   const struct nf_conntrack_tuple *t)
216 {
217         NLA_PUT_BE16(skb, CTA_PROTO_ICMPV6_ID, t->src.u.icmp.id);
218         NLA_PUT_U8(skb, CTA_PROTO_ICMPV6_TYPE, t->dst.u.icmp.type);
219         NLA_PUT_U8(skb, CTA_PROTO_ICMPV6_CODE, t->dst.u.icmp.code);
220
221         return 0;
222
223 nla_put_failure:
224         return -1;
225 }
226
227 static const struct nla_policy icmpv6_nla_policy[CTA_PROTO_MAX+1] = {
228         [CTA_PROTO_ICMPV6_TYPE] = { .type = NLA_U8 },
229         [CTA_PROTO_ICMPV6_CODE] = { .type = NLA_U8 },
230         [CTA_PROTO_ICMPV6_ID]   = { .type = NLA_U16 },
231 };
232
233 static int icmpv6_nlattr_to_tuple(struct nlattr *tb[],
234                                 struct nf_conntrack_tuple *tuple)
235 {
236         if (!tb[CTA_PROTO_ICMPV6_TYPE]
237             || !tb[CTA_PROTO_ICMPV6_CODE]
238             || !tb[CTA_PROTO_ICMPV6_ID])
239                 return -EINVAL;
240
241         tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMPV6_TYPE]);
242         tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMPV6_CODE]);
243         tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMPV6_ID]);
244
245         if (tuple->dst.u.icmp.type < 128
246             || tuple->dst.u.icmp.type - 128 >= sizeof(invmap)
247             || !invmap[tuple->dst.u.icmp.type - 128])
248                 return -EINVAL;
249
250         return 0;
251 }
252 #endif
253
254 #ifdef CONFIG_SYSCTL
255 static struct ctl_table_header *icmpv6_sysctl_header;
256 static struct ctl_table icmpv6_sysctl_table[] = {
257         {
258                 .procname       = "nf_conntrack_icmpv6_timeout",
259                 .data           = &nf_ct_icmpv6_timeout,
260                 .maxlen         = sizeof(unsigned int),
261                 .mode           = 0644,
262                 .proc_handler   = &proc_dointvec_jiffies,
263         },
264         {
265                 .ctl_name       = 0
266         }
267 };
268 #endif /* CONFIG_SYSCTL */
269
270 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 __read_mostly =
271 {
272         .l3proto                = PF_INET6,
273         .l4proto                = IPPROTO_ICMPV6,
274         .name                   = "icmpv6",
275         .pkt_to_tuple           = icmpv6_pkt_to_tuple,
276         .invert_tuple           = icmpv6_invert_tuple,
277         .print_tuple            = icmpv6_print_tuple,
278         .print_conntrack        = icmpv6_print_conntrack,
279         .packet                 = icmpv6_packet,
280         .new                    = icmpv6_new,
281         .error                  = icmpv6_error,
282 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
283         .tuple_to_nlattr        = icmpv6_tuple_to_nlattr,
284         .nlattr_to_tuple        = icmpv6_nlattr_to_tuple,
285         .nla_policy             = icmpv6_nla_policy,
286 #endif
287 #ifdef CONFIG_SYSCTL
288         .ctl_table_header       = &icmpv6_sysctl_header,
289         .ctl_table              = icmpv6_sysctl_table,
290 #endif
291 };