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