blob: eb303471bcf6c252c2017061fd866e4ccf6a1fd1 [file] [log] [blame]
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -08001/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
3 *
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 */
8
9#include <linux/types.h>
10#include <linux/init.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040011#include <linux/export.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080012#include <linux/ip.h>
13#include <linux/icmp.h>
14
15#include <linux/netfilter.h>
16#include <net/netfilter/nf_nat.h>
17#include <net/netfilter/nf_nat_core.h>
Patrick McHardyc7232c92012-08-26 19:14:06 +020018#include <net/netfilter/nf_nat_l4proto.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080019
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020020static bool
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080021icmp_in_range(const struct nf_conntrack_tuple *tuple,
22 enum nf_nat_manip_type maniptype,
23 const union nf_conntrack_man_proto *min,
24 const union nf_conntrack_man_proto *max)
25{
26 return ntohs(tuple->src.u.icmp.id) >= ntohs(min->icmp.id) &&
27 ntohs(tuple->src.u.icmp.id) <= ntohs(max->icmp.id);
28}
29
Changli Gaof43dc982010-08-02 17:20:54 +020030static void
Patrick McHardyc7232c92012-08-26 19:14:06 +020031icmp_unique_tuple(const struct nf_nat_l3proto *l3proto,
32 struct nf_conntrack_tuple *tuple,
33 const struct nf_nat_range *range,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080034 enum nf_nat_manip_type maniptype,
35 const struct nf_conn *ct)
36{
37 static u_int16_t id;
38 unsigned int range_size;
39 unsigned int i;
40
Patrick McHardyc7232c92012-08-26 19:14:06 +020041 range_size = ntohs(range->max_proto.icmp.id) -
42 ntohs(range->min_proto.icmp.id) + 1;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080043 /* If no range specified... */
Patrick McHardycbc9f2f2011-12-23 13:59:49 +010044 if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED))
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080045 range_size = 0xFFFF;
46
Changli Gao2452a992010-08-02 17:35:49 +020047 for (i = 0; ; ++id) {
Patrick McHardyc7232c92012-08-26 19:14:06 +020048 tuple->src.u.icmp.id = htons(ntohs(range->min_proto.icmp.id) +
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090049 (id % range_size));
Changli Gao2452a992010-08-02 17:35:49 +020050 if (++i == range_size || !nf_nat_used_tuple(tuple, ct))
Changli Gaof43dc982010-08-02 17:20:54 +020051 return;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080052 }
Changli Gaof43dc982010-08-02 17:20:54 +020053 return;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080054}
55
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020056static bool
Herbert Xu3db05fe2007-10-15 00:53:15 -070057icmp_manip_pkt(struct sk_buff *skb,
Patrick McHardyc7232c92012-08-26 19:14:06 +020058 const struct nf_nat_l3proto *l3proto,
59 unsigned int iphdroff, unsigned int hdroff,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080060 const struct nf_conntrack_tuple *tuple,
61 enum nf_nat_manip_type maniptype)
62{
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080063 struct icmphdr *hdr;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080064
Herbert Xu3db05fe2007-10-15 00:53:15 -070065 if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020066 return false;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080067
Herbert Xu3db05fe2007-10-15 00:53:15 -070068 hdr = (struct icmphdr *)(skb->data + hdroff);
Patrick McHardybe0ea7d2007-11-30 01:17:11 +110069 inet_proto_csum_replace2(&hdr->checksum, skb,
70 hdr->un.echo.id, tuple->src.u.icmp.id, 0);
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080071 hdr->un.echo.id = tuple->src.u.icmp.id;
Jan Engelhardtf2ea8252008-04-14 11:15:53 +020072 return true;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080073}
74
Patrick McHardyc7232c92012-08-26 19:14:06 +020075const struct nf_nat_l4proto nf_nat_l4proto_icmp = {
76 .l4proto = IPPROTO_ICMP,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080077 .manip_pkt = icmp_manip_pkt,
78 .in_range = icmp_in_range,
79 .unique_tuple = icmp_unique_tuple,
Patrick McHardye281db5c2007-03-04 15:57:25 -080080#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyc7232c92012-08-26 19:14:06 +020081 .nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080082#endif
83};