Merge ../linux-2.6
[pandora-kernel.git] / net / ipv4 / netfilter / ip_conntrack_proto_icmp.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/sched.h>
11 #include <linux/timer.h>
12 #include <linux/netfilter.h>
13 #include <linux/in.h>
14 #include <linux/icmp.h>
15 #include <linux/seq_file.h>
16 #include <net/ip.h>
17 #include <net/checksum.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4.h>
20 #include <linux/netfilter_ipv4/ip_conntrack.h>
21 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
22 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
23
24 unsigned long ip_ct_icmp_timeout = 30*HZ;
25
26 #if 0
27 #define DEBUGP printk
28 #else
29 #define DEBUGP(format, args...)
30 #endif
31
32 static int icmp_pkt_to_tuple(const struct sk_buff *skb,
33                              unsigned int dataoff,
34                              struct ip_conntrack_tuple *tuple)
35 {
36         struct icmphdr _hdr, *hp;
37
38         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
39         if (hp == NULL)
40                 return 0;
41
42         tuple->dst.u.icmp.type = hp->type;
43         tuple->src.u.icmp.id = hp->un.echo.id;
44         tuple->dst.u.icmp.code = hp->code;
45
46         return 1;
47 }
48
49 static int icmp_invert_tuple(struct ip_conntrack_tuple *tuple,
50                              const struct ip_conntrack_tuple *orig)
51 {
52         /* Add 1; spaces filled with 0. */
53         static u_int8_t invmap[]
54                 = { [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
55                     [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
56                     [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
57                     [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
58                     [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
59                     [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
60                     [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
61                     [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1};
62
63         if (orig->dst.u.icmp.type >= sizeof(invmap)
64             || !invmap[orig->dst.u.icmp.type])
65                 return 0;
66
67         tuple->src.u.icmp.id = orig->src.u.icmp.id;
68         tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
69         tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
70         return 1;
71 }
72
73 /* Print out the per-protocol part of the tuple. */
74 static int icmp_print_tuple(struct seq_file *s,
75                             const struct ip_conntrack_tuple *tuple)
76 {
77         return seq_printf(s, "type=%u code=%u id=%u ",
78                           tuple->dst.u.icmp.type,
79                           tuple->dst.u.icmp.code,
80                           ntohs(tuple->src.u.icmp.id));
81 }
82
83 /* Print out the private part of the conntrack. */
84 static int icmp_print_conntrack(struct seq_file *s,
85                                 const struct ip_conntrack *conntrack)
86 {
87         return 0;
88 }
89
90 /* Returns verdict for packet, or -1 for invalid. */
91 static int icmp_packet(struct ip_conntrack *ct,
92                        const struct sk_buff *skb,
93                        enum ip_conntrack_info ctinfo)
94 {
95         /* Try to delete connection immediately after all replies:
96            won't actually vanish as we still have skb, and del_timer
97            means this will only run once even if count hits zero twice
98            (theoretically possible with SMP) */
99         if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
100                 if (atomic_dec_and_test(&ct->proto.icmp.count)
101                     && del_timer(&ct->timeout))
102                         ct->timeout.function((unsigned long)ct);
103         } else {
104                 atomic_inc(&ct->proto.icmp.count);
105                 ip_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
106                 ip_ct_refresh_acct(ct, ctinfo, skb, ip_ct_icmp_timeout);
107         }
108
109         return NF_ACCEPT;
110 }
111
112 static u_int8_t valid_new[] = { 
113         [ICMP_ECHO] = 1,
114         [ICMP_TIMESTAMP] = 1,
115         [ICMP_INFO_REQUEST] = 1,
116         [ICMP_ADDRESS] = 1 
117 };
118
119 /* Called when a new connection for this protocol found. */
120 static int icmp_new(struct ip_conntrack *conntrack,
121                     const struct sk_buff *skb)
122 {
123         if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
124             || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
125                 /* Can't create a new ICMP `conn' with this. */
126                 DEBUGP("icmp: can't create new conn with type %u\n",
127                        conntrack->tuplehash[0].tuple.dst.u.icmp.type);
128                 DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
129                 return 0;
130         }
131         atomic_set(&conntrack->proto.icmp.count, 0);
132         return 1;
133 }
134
135 static int
136 icmp_error_message(struct sk_buff *skb,
137                    enum ip_conntrack_info *ctinfo,
138                    unsigned int hooknum)
139 {
140         struct ip_conntrack_tuple innertuple, origtuple;
141         struct {
142                 struct icmphdr icmp;
143                 struct iphdr ip;
144         } _in, *inside;
145         struct ip_conntrack_protocol *innerproto;
146         struct ip_conntrack_tuple_hash *h;
147         int dataoff;
148
149         IP_NF_ASSERT(skb->nfct == NULL);
150
151         /* Not enough header? */
152         inside = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_in), &_in);
153         if (inside == NULL)
154                 return -NF_ACCEPT;
155
156         /* Ignore ICMP's containing fragments (shouldn't happen) */
157         if (inside->ip.frag_off & htons(IP_OFFSET)) {
158                 DEBUGP("icmp_error_track: fragment of proto %u\n",
159                        inside->ip.protocol);
160                 return -NF_ACCEPT;
161         }
162
163         innerproto = ip_conntrack_proto_find_get(inside->ip.protocol);
164         dataoff = skb->nh.iph->ihl*4 + sizeof(inside->icmp) + inside->ip.ihl*4;
165         /* Are they talking about one of our connections? */
166         if (!ip_ct_get_tuple(&inside->ip, skb, dataoff, &origtuple, innerproto)) {
167                 DEBUGP("icmp_error: ! get_tuple p=%u", inside->ip.protocol);
168                 ip_conntrack_proto_put(innerproto);
169                 return -NF_ACCEPT;
170         }
171
172         /* Ordinarily, we'd expect the inverted tupleproto, but it's
173            been preserved inside the ICMP. */
174         if (!ip_ct_invert_tuple(&innertuple, &origtuple, innerproto)) {
175                 DEBUGP("icmp_error_track: Can't invert tuple\n");
176                 ip_conntrack_proto_put(innerproto);
177                 return -NF_ACCEPT;
178         }
179         ip_conntrack_proto_put(innerproto);
180
181         *ctinfo = IP_CT_RELATED;
182
183         h = ip_conntrack_find_get(&innertuple, NULL);
184         if (!h) {
185                 /* Locally generated ICMPs will match inverted if they
186                    haven't been SNAT'ed yet */
187                 /* FIXME: NAT code has to handle half-done double NAT --RR */
188                 if (hooknum == NF_IP_LOCAL_OUT)
189                         h = ip_conntrack_find_get(&origtuple, NULL);
190
191                 if (!h) {
192                         DEBUGP("icmp_error_track: no match\n");
193                         return -NF_ACCEPT;
194                 }
195                 /* Reverse direction from that found */
196                 if (DIRECTION(h) != IP_CT_DIR_REPLY)
197                         *ctinfo += IP_CT_IS_REPLY;
198         } else {
199                 if (DIRECTION(h) == IP_CT_DIR_REPLY)
200                         *ctinfo += IP_CT_IS_REPLY;
201         }
202
203         /* Update skb to refer to this connection */
204         skb->nfct = &tuplehash_to_ctrack(h)->ct_general;
205         skb->nfctinfo = *ctinfo;
206         return -NF_ACCEPT;
207 }
208
209 /* Small and modified version of icmp_rcv */
210 static int
211 icmp_error(struct sk_buff *skb, enum ip_conntrack_info *ctinfo,
212            unsigned int hooknum)
213 {
214         struct icmphdr _ih, *icmph;
215
216         /* Not enough header? */
217         icmph = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_ih), &_ih);
218         if (icmph == NULL) {
219                 if (LOG_INVALID(IPPROTO_ICMP))
220                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
221                                       "ip_ct_icmp: short packet ");
222                 return -NF_ACCEPT;
223         }
224
225         /* See ip_conntrack_proto_tcp.c */
226         if (hooknum != NF_IP_PRE_ROUTING)
227                 goto checksum_skipped;
228
229         switch (skb->ip_summed) {
230         case CHECKSUM_HW:
231                 if (!(u16)csum_fold(skb->csum)) 
232                         break;
233                 if (LOG_INVALID(IPPROTO_ICMP))
234                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
235                                       "ip_ct_icmp: bad HW ICMP checksum ");
236                 return -NF_ACCEPT;
237         case CHECKSUM_NONE:
238                 if ((u16)csum_fold(skb_checksum(skb, 0, skb->len, 0))) {
239                         if (LOG_INVALID(IPPROTO_ICMP))
240                                 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
241                                               "ip_ct_icmp: bad ICMP checksum ");
242                         return -NF_ACCEPT;
243                 }
244         default:
245                 break;
246         }
247
248 checksum_skipped:
249         /*
250          *      18 is the highest 'known' ICMP type. Anything else is a mystery
251          *
252          *      RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently
253          *                discarded.
254          */
255         if (icmph->type > NR_ICMP_TYPES) {
256                 if (LOG_INVALID(IPPROTO_ICMP))
257                         nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
258                                       "ip_ct_icmp: invalid ICMP type ");
259                 return -NF_ACCEPT;
260         }
261
262         /* Need to track icmp error message? */
263         if (icmph->type != ICMP_DEST_UNREACH
264             && icmph->type != ICMP_SOURCE_QUENCH
265             && icmph->type != ICMP_TIME_EXCEEDED
266             && icmph->type != ICMP_PARAMETERPROB
267             && icmph->type != ICMP_REDIRECT)
268                 return NF_ACCEPT;
269
270         return icmp_error_message(skb, ctinfo, hooknum);
271 }
272
273 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
274     defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
275 static int icmp_tuple_to_nfattr(struct sk_buff *skb,
276                                 const struct ip_conntrack_tuple *t)
277 {
278         NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t),
279                 &t->src.u.icmp.id);
280         NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t),
281                 &t->dst.u.icmp.type);
282         NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t),
283                 &t->dst.u.icmp.code);
284
285         if (t->dst.u.icmp.type >= sizeof(valid_new) 
286             || !valid_new[t->dst.u.icmp.type])
287                 return -EINVAL;
288
289         return 0;
290
291 nfattr_failure:
292         return -1;
293 }
294
295 static int icmp_nfattr_to_tuple(struct nfattr *tb[],
296                                 struct ip_conntrack_tuple *tuple)
297 {
298         if (!tb[CTA_PROTO_ICMP_TYPE-1]
299             || !tb[CTA_PROTO_ICMP_CODE-1]
300             || !tb[CTA_PROTO_ICMP_ID-1])
301                 return -1;
302
303         tuple->dst.u.icmp.type = 
304                         *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]);
305         tuple->dst.u.icmp.code =
306                         *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]);
307         tuple->src.u.icmp.id =
308                         *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]);
309
310         return 0;
311 }
312 #endif
313
314 struct ip_conntrack_protocol ip_conntrack_protocol_icmp =
315 {
316         .proto                  = IPPROTO_ICMP,
317         .name                   = "icmp",
318         .pkt_to_tuple           = icmp_pkt_to_tuple,
319         .invert_tuple           = icmp_invert_tuple,
320         .print_tuple            = icmp_print_tuple,
321         .print_conntrack        = icmp_print_conntrack,
322         .packet                 = icmp_packet,
323         .new                    = icmp_new,
324         .error                  = icmp_error,
325 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
326     defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
327         .tuple_to_nfattr        = icmp_tuple_to_nfattr,
328         .nfattr_to_tuple        = icmp_nfattr_to_tuple,
329 #endif
330 };