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.
9 #include <linux/types.h>
10 #include <linux/sched.h>
11 #include <linux/timer.h>
12 #include <linux/netfilter.h>
14 #include <linux/icmp.h>
15 #include <linux/seq_file.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
;
29 #define DEBUGP(format, args...)
32 static int icmp_pkt_to_tuple(const struct sk_buff
*skb
,
34 struct ip_conntrack_tuple
*tuple
)
36 struct icmphdr _hdr
, *hp
;
38 hp
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
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
;
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
])
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
;
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
)
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
);
104 atomic_inc(&ct
->proto
.icmp
.count
);
105 ip_ct_refresh_acct(ct
, ctinfo
, skb
, ip_ct_icmp_timeout
);
111 /* Called when a new connection for this protocol found. */
112 static int icmp_new(struct ip_conntrack
*conntrack
,
113 const struct sk_buff
*skb
)
115 static u_int8_t valid_new
[]
117 [ICMP_TIMESTAMP
] = 1,
118 [ICMP_INFO_REQUEST
] = 1,
119 [ICMP_ADDRESS
] = 1 };
121 if (conntrack
->tuplehash
[0].tuple
.dst
.u
.icmp
.type
>= sizeof(valid_new
)
122 || !valid_new
[conntrack
->tuplehash
[0].tuple
.dst
.u
.icmp
.type
]) {
123 /* Can't create a new ICMP `conn' with this. */
124 DEBUGP("icmp: can't create new conn with type %u\n",
125 conntrack
->tuplehash
[0].tuple
.dst
.u
.icmp
.type
);
126 DUMP_TUPLE(&conntrack
->tuplehash
[0].tuple
);
129 atomic_set(&conntrack
->proto
.icmp
.count
, 0);
134 icmp_error_message(struct sk_buff
*skb
,
135 enum ip_conntrack_info
*ctinfo
,
136 unsigned int hooknum
)
138 struct ip_conntrack_tuple innertuple
, origtuple
;
143 struct ip_conntrack_protocol
*innerproto
;
144 struct ip_conntrack_tuple_hash
*h
;
147 IP_NF_ASSERT(skb
->nfct
== NULL
);
149 /* Not enough header? */
150 inside
= skb_header_pointer(skb
, skb
->nh
.iph
->ihl
*4, sizeof(_in
), &_in
);
154 /* Ignore ICMP's containing fragments (shouldn't happen) */
155 if (inside
->ip
.frag_off
& htons(IP_OFFSET
)) {
156 DEBUGP("icmp_error_track: fragment of proto %u\n",
157 inside
->ip
.protocol
);
161 innerproto
= ip_ct_find_proto(inside
->ip
.protocol
);
162 dataoff
= skb
->nh
.iph
->ihl
*4 + sizeof(inside
->icmp
) + inside
->ip
.ihl
*4;
163 /* Are they talking about one of our connections? */
164 if (!ip_ct_get_tuple(&inside
->ip
, skb
, dataoff
, &origtuple
, innerproto
)) {
165 DEBUGP("icmp_error: ! get_tuple p=%u", inside
->ip
.protocol
);
169 /* Ordinarily, we'd expect the inverted tupleproto, but it's
170 been preserved inside the ICMP. */
171 if (!ip_ct_invert_tuple(&innertuple
, &origtuple
, innerproto
)) {
172 DEBUGP("icmp_error_track: Can't invert tuple\n");
176 *ctinfo
= IP_CT_RELATED
;
178 h
= ip_conntrack_find_get(&innertuple
, NULL
);
180 /* Locally generated ICMPs will match inverted if they
181 haven't been SNAT'ed yet */
182 /* FIXME: NAT code has to handle half-done double NAT --RR */
183 if (hooknum
== NF_IP_LOCAL_OUT
)
184 h
= ip_conntrack_find_get(&origtuple
, NULL
);
187 DEBUGP("icmp_error_track: no match\n");
190 /* Reverse direction from that found */
191 if (DIRECTION(h
) != IP_CT_DIR_REPLY
)
192 *ctinfo
+= IP_CT_IS_REPLY
;
194 if (DIRECTION(h
) == IP_CT_DIR_REPLY
)
195 *ctinfo
+= IP_CT_IS_REPLY
;
198 /* Update skb to refer to this connection */
199 skb
->nfct
= &tuplehash_to_ctrack(h
)->ct_general
;
200 skb
->nfctinfo
= *ctinfo
;
204 /* Small and modified version of icmp_rcv */
206 icmp_error(struct sk_buff
*skb
, enum ip_conntrack_info
*ctinfo
,
207 unsigned int hooknum
)
209 struct icmphdr _ih
, *icmph
;
211 /* Not enough header? */
212 icmph
= skb_header_pointer(skb
, skb
->nh
.iph
->ihl
*4, sizeof(_ih
), &_ih
);
214 if (LOG_INVALID(IPPROTO_ICMP
))
215 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
,
216 "ip_ct_icmp: short packet ");
220 /* See ip_conntrack_proto_tcp.c */
221 if (hooknum
!= NF_IP_PRE_ROUTING
)
222 goto checksum_skipped
;
224 switch (skb
->ip_summed
) {
226 if (!(u16
)csum_fold(skb
->csum
))
228 if (LOG_INVALID(IPPROTO_ICMP
))
229 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
,
230 "ip_ct_icmp: bad HW ICMP checksum ");
233 if ((u16
)csum_fold(skb_checksum(skb
, 0, skb
->len
, 0))) {
234 if (LOG_INVALID(IPPROTO_ICMP
))
235 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
,
236 "ip_ct_icmp: bad ICMP checksum ");
245 * 18 is the highest 'known' ICMP type. Anything else is a mystery
247 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
250 if (icmph
->type
> NR_ICMP_TYPES
) {
251 if (LOG_INVALID(IPPROTO_ICMP
))
252 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
,
253 "ip_ct_icmp: invalid ICMP type ");
257 /* Need to track icmp error message? */
258 if (icmph
->type
!= ICMP_DEST_UNREACH
259 && icmph
->type
!= ICMP_SOURCE_QUENCH
260 && icmph
->type
!= ICMP_TIME_EXCEEDED
261 && icmph
->type
!= ICMP_PARAMETERPROB
262 && icmph
->type
!= ICMP_REDIRECT
)
265 return icmp_error_message(skb
, ctinfo
, hooknum
);
268 struct ip_conntrack_protocol ip_conntrack_protocol_icmp
=
270 .proto
= IPPROTO_ICMP
,
272 .pkt_to_tuple
= icmp_pkt_to_tuple
,
273 .invert_tuple
= icmp_invert_tuple
,
274 .print_tuple
= icmp_print_tuple
,
275 .print_conntrack
= icmp_print_conntrack
,
276 .packet
= icmp_packet
,