[NETFILTER]: connection tracking event notifiers
[linux-2.6.git] / net / ipv4 / netfilter / ip_conntrack_proto_icmp.c
blobdca1f63d6f516a7df665d023d1252a883503a6cf
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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 */
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>
24 unsigned long ip_ct_icmp_timeout = 30*HZ;
26 #if 0
27 #define DEBUGP printk
28 #else
29 #define DEBUGP(format, args...)
30 #endif
32 static int icmp_pkt_to_tuple(const struct sk_buff *skb,
33 unsigned int dataoff,
34 struct ip_conntrack_tuple *tuple)
36 struct icmphdr _hdr, *hp;
38 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
39 if (hp == NULL)
40 return 0;
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;
46 return 1;
49 static int icmp_invert_tuple(struct ip_conntrack_tuple *tuple,
50 const struct ip_conntrack_tuple *orig)
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};
63 if (orig->dst.u.icmp.type >= sizeof(invmap)
64 || !invmap[orig->dst.u.icmp.type])
65 return 0;
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;
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)
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));
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)
87 return 0;
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)
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);
109 return NF_ACCEPT;
112 /* Called when a new connection for this protocol found. */
113 static int icmp_new(struct ip_conntrack *conntrack,
114 const struct sk_buff *skb)
116 static u_int8_t valid_new[]
117 = { [ICMP_ECHO] = 1,
118 [ICMP_TIMESTAMP] = 1,
119 [ICMP_INFO_REQUEST] = 1,
120 [ICMP_ADDRESS] = 1 };
122 if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
123 || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
124 /* Can't create a new ICMP `conn' with this. */
125 DEBUGP("icmp: can't create new conn with type %u\n",
126 conntrack->tuplehash[0].tuple.dst.u.icmp.type);
127 DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
128 return 0;
130 atomic_set(&conntrack->proto.icmp.count, 0);
131 return 1;
134 static int
135 icmp_error_message(struct sk_buff *skb,
136 enum ip_conntrack_info *ctinfo,
137 unsigned int hooknum)
139 struct ip_conntrack_tuple innertuple, origtuple;
140 struct {
141 struct icmphdr icmp;
142 struct iphdr ip;
143 } _in, *inside;
144 struct ip_conntrack_protocol *innerproto;
145 struct ip_conntrack_tuple_hash *h;
146 int dataoff;
148 IP_NF_ASSERT(skb->nfct == NULL);
150 /* Not enough header? */
151 inside = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_in), &_in);
152 if (inside == NULL)
153 return NF_ACCEPT;
155 /* Ignore ICMP's containing fragments (shouldn't happen) */
156 if (inside->ip.frag_off & htons(IP_OFFSET)) {
157 DEBUGP("icmp_error_track: fragment of proto %u\n",
158 inside->ip.protocol);
159 return NF_ACCEPT;
162 innerproto = ip_ct_find_proto(inside->ip.protocol);
163 dataoff = skb->nh.iph->ihl*4 + sizeof(inside->icmp) + inside->ip.ihl*4;
164 /* Are they talking about one of our connections? */
165 if (!ip_ct_get_tuple(&inside->ip, skb, dataoff, &origtuple, innerproto)) {
166 DEBUGP("icmp_error: ! get_tuple p=%u", inside->ip.protocol);
167 return NF_ACCEPT;
170 /* Ordinarily, we'd expect the inverted tupleproto, but it's
171 been preserved inside the ICMP. */
172 if (!ip_ct_invert_tuple(&innertuple, &origtuple, innerproto)) {
173 DEBUGP("icmp_error_track: Can't invert tuple\n");
174 return NF_ACCEPT;
177 *ctinfo = IP_CT_RELATED;
179 h = ip_conntrack_find_get(&innertuple, NULL);
180 if (!h) {
181 /* Locally generated ICMPs will match inverted if they
182 haven't been SNAT'ed yet */
183 /* FIXME: NAT code has to handle half-done double NAT --RR */
184 if (hooknum == NF_IP_LOCAL_OUT)
185 h = ip_conntrack_find_get(&origtuple, NULL);
187 if (!h) {
188 DEBUGP("icmp_error_track: no match\n");
189 return NF_ACCEPT;
191 /* Reverse direction from that found */
192 if (DIRECTION(h) != IP_CT_DIR_REPLY)
193 *ctinfo += IP_CT_IS_REPLY;
194 } else {
195 if (DIRECTION(h) == IP_CT_DIR_REPLY)
196 *ctinfo += IP_CT_IS_REPLY;
199 /* Update skb to refer to this connection */
200 skb->nfct = &tuplehash_to_ctrack(h)->ct_general;
201 skb->nfctinfo = *ctinfo;
202 return -NF_ACCEPT;
205 /* Small and modified version of icmp_rcv */
206 static int
207 icmp_error(struct sk_buff *skb, enum ip_conntrack_info *ctinfo,
208 unsigned int hooknum)
210 struct icmphdr _ih, *icmph;
212 /* Not enough header? */
213 icmph = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_ih), &_ih);
214 if (icmph == NULL) {
215 if (LOG_INVALID(IPPROTO_ICMP))
216 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
217 "ip_ct_icmp: short packet ");
218 return -NF_ACCEPT;
221 /* See ip_conntrack_proto_tcp.c */
222 if (hooknum != NF_IP_PRE_ROUTING)
223 goto checksum_skipped;
225 switch (skb->ip_summed) {
226 case CHECKSUM_HW:
227 if (!(u16)csum_fold(skb->csum))
228 break;
229 if (LOG_INVALID(IPPROTO_ICMP))
230 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
231 "ip_ct_icmp: bad HW ICMP checksum ");
232 return -NF_ACCEPT;
233 case CHECKSUM_NONE:
234 if ((u16)csum_fold(skb_checksum(skb, 0, skb->len, 0))) {
235 if (LOG_INVALID(IPPROTO_ICMP))
236 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
237 "ip_ct_icmp: bad ICMP checksum ");
238 return -NF_ACCEPT;
240 default:
241 break;
244 checksum_skipped:
246 * 18 is the highest 'known' ICMP type. Anything else is a mystery
248 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
249 * discarded.
251 if (icmph->type > NR_ICMP_TYPES) {
252 if (LOG_INVALID(IPPROTO_ICMP))
253 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
254 "ip_ct_icmp: invalid ICMP type ");
255 return -NF_ACCEPT;
258 /* Need to track icmp error message? */
259 if (icmph->type != ICMP_DEST_UNREACH
260 && icmph->type != ICMP_SOURCE_QUENCH
261 && icmph->type != ICMP_TIME_EXCEEDED
262 && icmph->type != ICMP_PARAMETERPROB
263 && icmph->type != ICMP_REDIRECT)
264 return NF_ACCEPT;
266 return icmp_error_message(skb, ctinfo, hooknum);
269 struct ip_conntrack_protocol ip_conntrack_protocol_icmp =
271 .proto = IPPROTO_ICMP,
272 .name = "icmp",
273 .pkt_to_tuple = icmp_pkt_to_tuple,
274 .invert_tuple = icmp_invert_tuple,
275 .print_tuple = icmp_print_tuple,
276 .print_conntrack = icmp_print_conntrack,
277 .packet = icmp_packet,
278 .new = icmp_new,
279 .error = icmp_error,