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.h>
20 #include <linux/netfilter_ipv4.h>
21 #include <linux/netfilter_ipv4/ip_conntrack.h>
22 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
23 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
25 unsigned long ip_ct_icmp_timeout
= 30*HZ
;
30 #define DEBUGP(format, args...)
33 static int icmp_pkt_to_tuple(const struct sk_buff
*skb
,
35 struct ip_conntrack_tuple
*tuple
)
37 struct icmphdr _hdr
, *hp
;
39 hp
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
43 tuple
->dst
.u
.icmp
.type
= hp
->type
;
44 tuple
->src
.u
.icmp
.id
= hp
->un
.echo
.id
;
45 tuple
->dst
.u
.icmp
.code
= hp
->code
;
50 /* Add 1; spaces filled with 0. */
51 static const u_int8_t invmap
[] = {
52 [ICMP_ECHO
] = ICMP_ECHOREPLY
+ 1,
53 [ICMP_ECHOREPLY
] = ICMP_ECHO
+ 1,
54 [ICMP_TIMESTAMP
] = ICMP_TIMESTAMPREPLY
+ 1,
55 [ICMP_TIMESTAMPREPLY
] = ICMP_TIMESTAMP
+ 1,
56 [ICMP_INFO_REQUEST
] = ICMP_INFO_REPLY
+ 1,
57 [ICMP_INFO_REPLY
] = ICMP_INFO_REQUEST
+ 1,
58 [ICMP_ADDRESS
] = ICMP_ADDRESSREPLY
+ 1,
59 [ICMP_ADDRESSREPLY
] = ICMP_ADDRESS
+ 1
62 static int icmp_invert_tuple(struct ip_conntrack_tuple
*tuple
,
63 const struct ip_conntrack_tuple
*orig
)
65 if (orig
->dst
.u
.icmp
.type
>= sizeof(invmap
)
66 || !invmap
[orig
->dst
.u
.icmp
.type
])
69 tuple
->src
.u
.icmp
.id
= orig
->src
.u
.icmp
.id
;
70 tuple
->dst
.u
.icmp
.type
= invmap
[orig
->dst
.u
.icmp
.type
] - 1;
71 tuple
->dst
.u
.icmp
.code
= orig
->dst
.u
.icmp
.code
;
75 /* Print out the per-protocol part of the tuple. */
76 static int icmp_print_tuple(struct seq_file
*s
,
77 const struct ip_conntrack_tuple
*tuple
)
79 return seq_printf(s
, "type=%u code=%u id=%u ",
80 tuple
->dst
.u
.icmp
.type
,
81 tuple
->dst
.u
.icmp
.code
,
82 ntohs(tuple
->src
.u
.icmp
.id
));
85 /* Print out the private part of the conntrack. */
86 static int icmp_print_conntrack(struct seq_file
*s
,
87 const struct ip_conntrack
*conntrack
)
92 /* Returns verdict for packet, or -1 for invalid. */
93 static int icmp_packet(struct ip_conntrack
*ct
,
94 const struct sk_buff
*skb
,
95 enum ip_conntrack_info ctinfo
)
97 /* Try to delete connection immediately after all replies:
98 won't actually vanish as we still have skb, and del_timer
99 means this will only run once even if count hits zero twice
100 (theoretically possible with SMP) */
101 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
) {
102 if (atomic_dec_and_test(&ct
->proto
.icmp
.count
)
103 && del_timer(&ct
->timeout
))
104 ct
->timeout
.function((unsigned long)ct
);
106 atomic_inc(&ct
->proto
.icmp
.count
);
107 ip_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE
, skb
);
108 ip_ct_refresh_acct(ct
, ctinfo
, skb
, ip_ct_icmp_timeout
);
114 /* Called when a new connection for this protocol found. */
115 static int icmp_new(struct ip_conntrack
*conntrack
,
116 const struct sk_buff
*skb
)
118 static const u_int8_t valid_new
[] = {
120 [ICMP_TIMESTAMP
] = 1,
121 [ICMP_INFO_REQUEST
] = 1,
125 if (conntrack
->tuplehash
[0].tuple
.dst
.u
.icmp
.type
>= sizeof(valid_new
)
126 || !valid_new
[conntrack
->tuplehash
[0].tuple
.dst
.u
.icmp
.type
]) {
127 /* Can't create a new ICMP `conn' with this. */
128 DEBUGP("icmp: can't create new conn with type %u\n",
129 conntrack
->tuplehash
[0].tuple
.dst
.u
.icmp
.type
);
130 DUMP_TUPLE(&conntrack
->tuplehash
[0].tuple
);
133 atomic_set(&conntrack
->proto
.icmp
.count
, 0);
138 icmp_error_message(struct sk_buff
*skb
,
139 enum ip_conntrack_info
*ctinfo
,
140 unsigned int hooknum
)
142 struct ip_conntrack_tuple innertuple
, origtuple
;
147 struct ip_conntrack_protocol
*innerproto
;
148 struct ip_conntrack_tuple_hash
*h
;
151 IP_NF_ASSERT(skb
->nfct
== NULL
);
153 /* Not enough header? */
154 inside
= skb_header_pointer(skb
, skb
->nh
.iph
->ihl
*4, sizeof(_in
), &_in
);
158 /* Ignore ICMP's containing fragments (shouldn't happen) */
159 if (inside
->ip
.frag_off
& htons(IP_OFFSET
)) {
160 DEBUGP("icmp_error_track: fragment of proto %u\n",
161 inside
->ip
.protocol
);
165 innerproto
= ip_conntrack_proto_find_get(inside
->ip
.protocol
);
166 dataoff
= skb
->nh
.iph
->ihl
*4 + sizeof(inside
->icmp
) + inside
->ip
.ihl
*4;
167 /* Are they talking about one of our connections? */
168 if (!ip_ct_get_tuple(&inside
->ip
, skb
, dataoff
, &origtuple
, innerproto
)) {
169 DEBUGP("icmp_error: ! get_tuple p=%u", inside
->ip
.protocol
);
170 ip_conntrack_proto_put(innerproto
);
174 /* Ordinarily, we'd expect the inverted tupleproto, but it's
175 been preserved inside the ICMP. */
176 if (!ip_ct_invert_tuple(&innertuple
, &origtuple
, innerproto
)) {
177 DEBUGP("icmp_error_track: Can't invert tuple\n");
178 ip_conntrack_proto_put(innerproto
);
181 ip_conntrack_proto_put(innerproto
);
183 *ctinfo
= IP_CT_RELATED
;
185 h
= ip_conntrack_find_get(&innertuple
, NULL
);
187 /* Locally generated ICMPs will match inverted if they
188 haven't been SNAT'ed yet */
189 /* FIXME: NAT code has to handle half-done double NAT --RR */
190 if (hooknum
== NF_IP_LOCAL_OUT
)
191 h
= ip_conntrack_find_get(&origtuple
, NULL
);
194 DEBUGP("icmp_error_track: no match\n");
197 /* Reverse direction from that found */
198 if (DIRECTION(h
) != IP_CT_DIR_REPLY
)
199 *ctinfo
+= IP_CT_IS_REPLY
;
201 if (DIRECTION(h
) == IP_CT_DIR_REPLY
)
202 *ctinfo
+= IP_CT_IS_REPLY
;
205 /* Update skb to refer to this connection */
206 skb
->nfct
= &tuplehash_to_ctrack(h
)->ct_general
;
207 skb
->nfctinfo
= *ctinfo
;
211 /* Small and modified version of icmp_rcv */
213 icmp_error(struct sk_buff
*skb
, enum ip_conntrack_info
*ctinfo
,
214 unsigned int hooknum
)
216 struct icmphdr _ih
, *icmph
;
218 /* Not enough header? */
219 icmph
= skb_header_pointer(skb
, skb
->nh
.iph
->ihl
*4, sizeof(_ih
), &_ih
);
221 if (LOG_INVALID(IPPROTO_ICMP
))
222 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
223 "ip_ct_icmp: short packet ");
227 /* See ip_conntrack_proto_tcp.c */
228 if (hooknum
!= NF_IP_PRE_ROUTING
)
229 goto checksum_skipped
;
231 switch (skb
->ip_summed
) {
233 if (!(u16
)csum_fold(skb
->csum
))
238 if (__skb_checksum_complete(skb
)) {
239 if (LOG_INVALID(IPPROTO_ICMP
))
240 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
241 "ip_ct_icmp: bad ICMP checksum ");
248 * 18 is the highest 'known' ICMP type. Anything else is a mystery
250 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
253 if (icmph
->type
> NR_ICMP_TYPES
) {
254 if (LOG_INVALID(IPPROTO_ICMP
))
255 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
256 "ip_ct_icmp: invalid ICMP type ");
260 /* Need to track icmp error message? */
261 if (icmph
->type
!= ICMP_DEST_UNREACH
262 && icmph
->type
!= ICMP_SOURCE_QUENCH
263 && icmph
->type
!= ICMP_TIME_EXCEEDED
264 && icmph
->type
!= ICMP_PARAMETERPROB
265 && icmph
->type
!= ICMP_REDIRECT
)
268 return icmp_error_message(skb
, ctinfo
, hooknum
);
271 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
272 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
273 static int icmp_tuple_to_nfattr(struct sk_buff
*skb
,
274 const struct ip_conntrack_tuple
*t
)
276 NFA_PUT(skb
, CTA_PROTO_ICMP_ID
, sizeof(u_int16_t
),
278 NFA_PUT(skb
, CTA_PROTO_ICMP_TYPE
, sizeof(u_int8_t
),
279 &t
->dst
.u
.icmp
.type
);
280 NFA_PUT(skb
, CTA_PROTO_ICMP_CODE
, sizeof(u_int8_t
),
281 &t
->dst
.u
.icmp
.code
);
289 static int icmp_nfattr_to_tuple(struct nfattr
*tb
[],
290 struct ip_conntrack_tuple
*tuple
)
292 if (!tb
[CTA_PROTO_ICMP_TYPE
-1]
293 || !tb
[CTA_PROTO_ICMP_CODE
-1]
294 || !tb
[CTA_PROTO_ICMP_ID
-1])
297 tuple
->dst
.u
.icmp
.type
=
298 *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_ICMP_TYPE
-1]);
299 tuple
->dst
.u
.icmp
.code
=
300 *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_ICMP_CODE
-1]);
301 tuple
->src
.u
.icmp
.id
=
302 *(u_int16_t
*)NFA_DATA(tb
[CTA_PROTO_ICMP_ID
-1]);
304 if (tuple
->dst
.u
.icmp
.type
>= sizeof(invmap
)
305 || !invmap
[tuple
->dst
.u
.icmp
.type
])
312 struct ip_conntrack_protocol ip_conntrack_protocol_icmp
=
314 .proto
= IPPROTO_ICMP
,
316 .pkt_to_tuple
= icmp_pkt_to_tuple
,
317 .invert_tuple
= icmp_invert_tuple
,
318 .print_tuple
= icmp_print_tuple
,
319 .print_conntrack
= icmp_print_conntrack
,
320 .packet
= icmp_packet
,
323 #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
324 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
325 .tuple_to_nfattr
= icmp_tuple_to_nfattr
,
326 .nfattr_to_tuple
= icmp_nfattr_to_tuple
,