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.
8 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
9 * - enable working with Layer 3 protocol independent connection tracking.
11 * Derived from net/ipv4/netfilter/ip_conntrack_proto_icmp.c
14 #include <linux/types.h>
15 #include <linux/sched.h>
16 #include <linux/timer.h>
17 #include <linux/netfilter.h>
19 #include <linux/icmp.h>
20 #include <linux/seq_file.h>
22 #include <net/checksum.h>
23 #include <linux/netfilter_ipv4.h>
24 #include <net/netfilter/nf_conntrack_tuple.h>
25 #include <net/netfilter/nf_conntrack_protocol.h>
26 #include <net/netfilter/nf_conntrack_core.h>
28 unsigned long nf_ct_icmp_timeout
= 30*HZ
;
33 #define DEBUGP(format, args...)
36 static int icmp_pkt_to_tuple(const struct sk_buff
*skb
,
38 struct nf_conntrack_tuple
*tuple
)
40 struct icmphdr _hdr
, *hp
;
42 hp
= skb_header_pointer(skb
, dataoff
, sizeof(_hdr
), &_hdr
);
46 tuple
->dst
.u
.icmp
.type
= hp
->type
;
47 tuple
->src
.u
.icmp
.id
= hp
->un
.echo
.id
;
48 tuple
->dst
.u
.icmp
.code
= hp
->code
;
53 /* Add 1; spaces filled with 0. */
54 static const u_int8_t invmap
[] = {
55 [ICMP_ECHO
] = ICMP_ECHOREPLY
+ 1,
56 [ICMP_ECHOREPLY
] = ICMP_ECHO
+ 1,
57 [ICMP_TIMESTAMP
] = ICMP_TIMESTAMPREPLY
+ 1,
58 [ICMP_TIMESTAMPREPLY
] = ICMP_TIMESTAMP
+ 1,
59 [ICMP_INFO_REQUEST
] = ICMP_INFO_REPLY
+ 1,
60 [ICMP_INFO_REPLY
] = ICMP_INFO_REQUEST
+ 1,
61 [ICMP_ADDRESS
] = ICMP_ADDRESSREPLY
+ 1,
62 [ICMP_ADDRESSREPLY
] = ICMP_ADDRESS
+ 1
65 static int icmp_invert_tuple(struct nf_conntrack_tuple
*tuple
,
66 const struct nf_conntrack_tuple
*orig
)
68 if (orig
->dst
.u
.icmp
.type
>= sizeof(invmap
)
69 || !invmap
[orig
->dst
.u
.icmp
.type
])
72 tuple
->src
.u
.icmp
.id
= orig
->src
.u
.icmp
.id
;
73 tuple
->dst
.u
.icmp
.type
= invmap
[orig
->dst
.u
.icmp
.type
] - 1;
74 tuple
->dst
.u
.icmp
.code
= orig
->dst
.u
.icmp
.code
;
78 /* Print out the per-protocol part of the tuple. */
79 static int icmp_print_tuple(struct seq_file
*s
,
80 const struct nf_conntrack_tuple
*tuple
)
82 return seq_printf(s
, "type=%u code=%u id=%u ",
83 tuple
->dst
.u
.icmp
.type
,
84 tuple
->dst
.u
.icmp
.code
,
85 ntohs(tuple
->src
.u
.icmp
.id
));
88 /* Print out the private part of the conntrack. */
89 static int icmp_print_conntrack(struct seq_file
*s
,
90 const struct nf_conn
*conntrack
)
95 /* Returns verdict for packet, or -1 for invalid. */
96 static int icmp_packet(struct nf_conn
*ct
,
97 const struct sk_buff
*skb
,
99 enum ip_conntrack_info ctinfo
,
101 unsigned int hooknum
)
103 /* Try to delete connection immediately after all replies:
104 won't actually vanish as we still have skb, and del_timer
105 means this will only run once even if count hits zero twice
106 (theoretically possible with SMP) */
107 if (CTINFO2DIR(ctinfo
) == IP_CT_DIR_REPLY
) {
108 if (atomic_dec_and_test(&ct
->proto
.icmp
.count
)
109 && del_timer(&ct
->timeout
))
110 ct
->timeout
.function((unsigned long)ct
);
112 atomic_inc(&ct
->proto
.icmp
.count
);
113 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE
, skb
);
114 nf_ct_refresh_acct(ct
, ctinfo
, skb
, nf_ct_icmp_timeout
);
120 /* Called when a new connection for this protocol found. */
121 static int icmp_new(struct nf_conn
*conntrack
,
122 const struct sk_buff
*skb
, unsigned int dataoff
)
124 static const u_int8_t valid_new
[] = {
126 [ICMP_TIMESTAMP
] = 1,
127 [ICMP_INFO_REQUEST
] = 1,
131 if (conntrack
->tuplehash
[0].tuple
.dst
.u
.icmp
.type
>= sizeof(valid_new
)
132 || !valid_new
[conntrack
->tuplehash
[0].tuple
.dst
.u
.icmp
.type
]) {
133 /* Can't create a new ICMP `conn' with this. */
134 DEBUGP("icmp: can't create new conn with type %u\n",
135 conntrack
->tuplehash
[0].tuple
.dst
.u
.icmp
.type
);
136 NF_CT_DUMP_TUPLE(&conntrack
->tuplehash
[0].tuple
);
139 atomic_set(&conntrack
->proto
.icmp
.count
, 0);
143 extern struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4
;
144 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
146 icmp_error_message(struct sk_buff
*skb
,
147 enum ip_conntrack_info
*ctinfo
,
148 unsigned int hooknum
)
150 struct nf_conntrack_tuple innertuple
, origtuple
;
155 struct nf_conntrack_protocol
*innerproto
;
156 struct nf_conntrack_tuple_hash
*h
;
159 NF_CT_ASSERT(skb
->nfct
== NULL
);
161 /* Not enough header? */
162 inside
= skb_header_pointer(skb
, skb
->nh
.iph
->ihl
*4, sizeof(_in
), &_in
);
166 /* Ignore ICMP's containing fragments (shouldn't happen) */
167 if (inside
->ip
.frag_off
& htons(IP_OFFSET
)) {
168 DEBUGP("icmp_error_message: fragment of proto %u\n",
169 inside
->ip
.protocol
);
173 innerproto
= __nf_ct_proto_find(PF_INET
, inside
->ip
.protocol
);
174 dataoff
= skb
->nh
.iph
->ihl
*4 + sizeof(inside
->icmp
);
175 /* Are they talking about one of our connections? */
176 if (!nf_ct_get_tuple(skb
, dataoff
, dataoff
+ inside
->ip
.ihl
*4, PF_INET
,
177 inside
->ip
.protocol
, &origtuple
,
178 &nf_conntrack_l3proto_ipv4
, innerproto
)) {
179 DEBUGP("icmp_error_message: ! get_tuple p=%u",
180 inside
->ip
.protocol
);
184 /* Ordinarily, we'd expect the inverted tupleproto, but it's
185 been preserved inside the ICMP. */
186 if (!nf_ct_invert_tuple(&innertuple
, &origtuple
,
187 &nf_conntrack_l3proto_ipv4
, innerproto
)) {
188 DEBUGP("icmp_error_message: no match\n");
192 *ctinfo
= IP_CT_RELATED
;
194 h
= nf_conntrack_find_get(&innertuple
, NULL
);
196 /* Locally generated ICMPs will match inverted if they
197 haven't been SNAT'ed yet */
198 /* FIXME: NAT code has to handle half-done double NAT --RR */
199 if (hooknum
== NF_IP_LOCAL_OUT
)
200 h
= nf_conntrack_find_get(&origtuple
, NULL
);
203 DEBUGP("icmp_error_message: no match\n");
207 /* Reverse direction from that found */
208 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
)
209 *ctinfo
+= IP_CT_IS_REPLY
;
211 if (NF_CT_DIRECTION(h
) == IP_CT_DIR_REPLY
)
212 *ctinfo
+= IP_CT_IS_REPLY
;
215 /* Update skb to refer to this connection */
216 skb
->nfct
= &nf_ct_tuplehash_to_ctrack(h
)->ct_general
;
217 skb
->nfctinfo
= *ctinfo
;
221 /* Small and modified version of icmp_rcv */
223 icmp_error(struct sk_buff
*skb
, unsigned int dataoff
,
224 enum ip_conntrack_info
*ctinfo
, int pf
, unsigned int hooknum
)
226 struct icmphdr _ih
, *icmph
;
228 /* Not enough header? */
229 icmph
= skb_header_pointer(skb
, skb
->nh
.iph
->ihl
*4, sizeof(_ih
), &_ih
);
231 if (LOG_INVALID(IPPROTO_ICMP
))
232 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
233 "nf_ct_icmp: short packet ");
237 /* See ip_conntrack_proto_tcp.c */
238 if (hooknum
!= NF_IP_PRE_ROUTING
)
239 goto checksum_skipped
;
241 switch (skb
->ip_summed
) {
243 if (!(u16
)csum_fold(skb
->csum
))
245 if (LOG_INVALID(IPPROTO_ICMP
))
246 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
247 "nf_ct_icmp: bad HW ICMP checksum ");
250 if ((u16
)csum_fold(skb_checksum(skb
, 0, skb
->len
, 0))) {
251 if (LOG_INVALID(IPPROTO_ICMP
))
252 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
,
254 "nf_ct_icmp: bad ICMP checksum ");
263 * 18 is the highest 'known' ICMP type. Anything else is a mystery
265 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
268 if (icmph
->type
> NR_ICMP_TYPES
) {
269 if (LOG_INVALID(IPPROTO_ICMP
))
270 nf_log_packet(PF_INET
, 0, skb
, NULL
, NULL
, NULL
,
271 "nf_ct_icmp: invalid ICMP type ");
275 /* Need to track icmp error message? */
276 if (icmph
->type
!= ICMP_DEST_UNREACH
277 && icmph
->type
!= ICMP_SOURCE_QUENCH
278 && icmph
->type
!= ICMP_TIME_EXCEEDED
279 && icmph
->type
!= ICMP_PARAMETERPROB
280 && icmph
->type
!= ICMP_REDIRECT
)
283 return icmp_error_message(skb
, ctinfo
, hooknum
);
286 #if defined(CONFIG_NF_CT_NETLINK) || \
287 defined(CONFIG_NF_CT_NETLINK_MODULE)
289 #include <linux/netfilter/nfnetlink.h>
290 #include <linux/netfilter/nfnetlink_conntrack.h>
292 static int icmp_tuple_to_nfattr(struct sk_buff
*skb
,
293 const struct nf_conntrack_tuple
*t
)
295 NFA_PUT(skb
, CTA_PROTO_ICMP_ID
, sizeof(u_int16_t
),
297 NFA_PUT(skb
, CTA_PROTO_ICMP_TYPE
, sizeof(u_int8_t
),
298 &t
->dst
.u
.icmp
.type
);
299 NFA_PUT(skb
, CTA_PROTO_ICMP_CODE
, sizeof(u_int8_t
),
300 &t
->dst
.u
.icmp
.code
);
308 static const size_t cta_min_proto
[CTA_PROTO_MAX
] = {
309 [CTA_PROTO_ICMP_TYPE
-1] = sizeof(u_int8_t
),
310 [CTA_PROTO_ICMP_CODE
-1] = sizeof(u_int8_t
),
311 [CTA_PROTO_ICMP_ID
-1] = sizeof(u_int16_t
)
314 static int icmp_nfattr_to_tuple(struct nfattr
*tb
[],
315 struct nf_conntrack_tuple
*tuple
)
317 if (!tb
[CTA_PROTO_ICMP_TYPE
-1]
318 || !tb
[CTA_PROTO_ICMP_CODE
-1]
319 || !tb
[CTA_PROTO_ICMP_ID
-1])
322 if (nfattr_bad_size(tb
, CTA_PROTO_MAX
, cta_min_proto
))
325 tuple
->dst
.u
.icmp
.type
=
326 *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_ICMP_TYPE
-1]);
327 tuple
->dst
.u
.icmp
.code
=
328 *(u_int8_t
*)NFA_DATA(tb
[CTA_PROTO_ICMP_CODE
-1]);
329 tuple
->src
.u
.icmp
.id
=
330 *(u_int16_t
*)NFA_DATA(tb
[CTA_PROTO_ICMP_ID
-1]);
332 if (tuple
->dst
.u
.icmp
.type
>= sizeof(invmap
)
333 || !invmap
[tuple
->dst
.u
.icmp
.type
])
340 struct nf_conntrack_protocol nf_conntrack_protocol_icmp
=
342 .list
= { NULL
, NULL
},
344 .proto
= IPPROTO_ICMP
,
346 .pkt_to_tuple
= icmp_pkt_to_tuple
,
347 .invert_tuple
= icmp_invert_tuple
,
348 .print_tuple
= icmp_print_tuple
,
349 .print_conntrack
= icmp_print_conntrack
,
350 .packet
= icmp_packet
,
355 #if defined(CONFIG_NF_CT_NETLINK) || \
356 defined(CONFIG_NF_CT_NETLINK_MODULE)
357 .tuple_to_nfattr
= icmp_tuple_to_nfattr
,
358 .nfattr_to_tuple
= icmp_nfattr_to_tuple
,
362 EXPORT_SYMBOL(nf_conntrack_protocol_icmp
);