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>
16 #include <linux/skbuff.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>
24 unsigned int 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 /* 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
61 static int icmp_invert_tuple(struct ip_conntrack_tuple
*tuple
,
62 const struct ip_conntrack_tuple
*orig
)
64 if (orig
->dst
.u
.icmp
.type
>= sizeof(invmap
)
65 || !invmap
[orig
->dst
.u
.icmp
.type
])
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
;
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
)
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
));
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
)
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
)
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
);
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
);
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
)
117 static const u_int8_t valid_new
[] = {
119 [ICMP_TIMESTAMP
] = 1,
120 [ICMP_INFO_REQUEST
] = 1,
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
);
132 atomic_set(&conntrack
->proto
.icmp
.count
, 0);
137 icmp_error_message(struct sk_buff
*skb
,
138 enum ip_conntrack_info
*ctinfo
,
139 unsigned int hooknum
)
141 struct ip_conntrack_tuple innertuple
, origtuple
;
146 struct ip_conntrack_protocol
*innerproto
;
147 struct ip_conntrack_tuple_hash
*h
;
150 IP_NF_ASSERT(skb
->nfct
== NULL
);
152 /* Not enough header? */
153 inside
= skb_header_pointer(skb
, skb
->nh
.iph
->ihl
*4, sizeof(_in
), &_in
);
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
);
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
);
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
);
180 ip_conntrack_proto_put(innerproto
);
182 *ctinfo
= IP_CT_RELATED
;
184 h
= ip_conntrack_find_get(&innertuple
, NULL
);
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
);
193 DEBUGP("icmp_error_track: no match\n");
196 /* Reverse direction from that found */
197 if (DIRECTION(h
) != IP_CT_DIR_REPLY
)
198 *ctinfo
+= IP_CT_IS_REPLY
;
200 if (DIRECTION(h
) == IP_CT_DIR_REPLY
)
201 *ctinfo
+= IP_CT_IS_REPLY
;
204 /* Update skb to refer to this connection */
205 skb
->nfct
= &tuplehash_to_ctrack(h
)->ct_general
;
206 skb
->nfctinfo
= *ctinfo
;
210 /* Small and modified version of icmp_rcv */
212 icmp_error(struct sk_buff
*skb
, enum ip_conntrack_info
*ctinfo
,
213 unsigned int hooknum
)
215 struct icmphdr _ih
, *icmph
;
217 /* Not enough header? */
218 icmph
= skb_header_pointer(skb
, skb
->nh
.iph
->ihl
*4, sizeof(_ih
), &_ih
);
220 if (LOG_INVALID(IPPROTO_ICMP
))
221 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
222 "ip_ct_icmp: short packet ");
226 /* See ip_conntrack_proto_tcp.c */
227 if (hooknum
!= NF_IP_PRE_ROUTING
)
228 goto checksum_skipped
;
230 switch (skb
->ip_summed
) {
232 if (!(u16
)csum_fold(skb
->csum
))
237 if (__skb_checksum_complete(skb
)) {
238 if (LOG_INVALID(IPPROTO_ICMP
))
239 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
240 "ip_ct_icmp: bad ICMP checksum ");
247 * 18 is the highest 'known' ICMP type. Anything else is a mystery
249 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
252 if (icmph
->type
> NR_ICMP_TYPES
) {
253 if (LOG_INVALID(IPPROTO_ICMP
))
254 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
255 "ip_ct_icmp: invalid ICMP type ");
259 /* Need to track icmp error message? */
260 if (icmph
->type
!= ICMP_DEST_UNREACH
261 && icmph
->type
!= ICMP_SOURCE_QUENCH
262 && icmph
->type
!= ICMP_TIME_EXCEEDED
263 && icmph
->type
!= ICMP_PARAMETERPROB
264 && icmph
->type
!= ICMP_REDIRECT
)
267 return icmp_error_message(skb
, ctinfo
, hooknum
);
270 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
271 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
272 static int icmp_tuple_to_nfattr(struct sk_buff
*skb
,
273 const struct ip_conntrack_tuple
*t
)
275 NFA_PUT(skb
, CTA_PROTO_ICMP_ID
, sizeof(u_int16_t
),
277 NFA_PUT(skb
, CTA_PROTO_ICMP_TYPE
, sizeof(u_int8_t
),
278 &t
->dst
.u
.icmp
.type
);
279 NFA_PUT(skb
, CTA_PROTO_ICMP_CODE
, sizeof(u_int8_t
),
280 &t
->dst
.u
.icmp
.code
);
288 static int icmp_nfattr_to_tuple(struct nfattr
*tb
[],
289 struct ip_conntrack_tuple
*tuple
)
291 if (!tb
[CTA_PROTO_ICMP_TYPE
-1]
292 || !tb
[CTA_PROTO_ICMP_CODE
-1]
293 || !tb
[CTA_PROTO_ICMP_ID
-1])
296 tuple
->dst
.u
.icmp
.type
=
297 *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_ICMP_TYPE
-1]);
298 tuple
->dst
.u
.icmp
.code
=
299 *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_ICMP_CODE
-1]);
300 tuple
->src
.u
.icmp
.id
=
301 *(u_int16_t
*)NFA_DATA(tb
[CTA_PROTO_ICMP_ID
-1]);
303 if (tuple
->dst
.u
.icmp
.type
>= sizeof(invmap
)
304 || !invmap
[tuple
->dst
.u
.icmp
.type
])
311 struct ip_conntrack_protocol ip_conntrack_protocol_icmp
=
313 .proto
= IPPROTO_ICMP
,
315 .pkt_to_tuple
= icmp_pkt_to_tuple
,
316 .invert_tuple
= icmp_invert_tuple
,
317 .print_tuple
= icmp_print_tuple
,
318 .print_conntrack
= icmp_print_conntrack
,
319 .packet
= icmp_packet
,
322 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
323 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
324 .tuple_to_nfattr
= icmp_tuple_to_nfattr
,
325 .nfattr_to_tuple
= icmp_nfattr_to_tuple
,