Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 1 | /* (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 Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 12 | #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 McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 18 | #include <net/netfilter/nf_nat_l4proto.h> |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 19 | |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 20 | static bool |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 21 | icmp_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 Gao | f43dc98 | 2010-08-02 17:20:54 +0200 | [diff] [blame] | 30 | static void |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 31 | icmp_unique_tuple(const struct nf_nat_l3proto *l3proto, |
| 32 | struct nf_conntrack_tuple *tuple, |
| 33 | const struct nf_nat_range *range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 34 | 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 McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 41 | range_size = ntohs(range->max_proto.icmp.id) - |
| 42 | ntohs(range->min_proto.icmp.id) + 1; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 43 | /* If no range specified... */ |
Patrick McHardy | cbc9f2f | 2011-12-23 13:59:49 +0100 | [diff] [blame] | 44 | if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 45 | range_size = 0xFFFF; |
| 46 | |
Changli Gao | 2452a99 | 2010-08-02 17:35:49 +0200 | [diff] [blame] | 47 | for (i = 0; ; ++id) { |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 48 | tuple->src.u.icmp.id = htons(ntohs(range->min_proto.icmp.id) + |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 49 | (id % range_size)); |
Changli Gao | 2452a99 | 2010-08-02 17:35:49 +0200 | [diff] [blame] | 50 | if (++i == range_size || !nf_nat_used_tuple(tuple, ct)) |
Changli Gao | f43dc98 | 2010-08-02 17:20:54 +0200 | [diff] [blame] | 51 | return; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 52 | } |
Changli Gao | f43dc98 | 2010-08-02 17:20:54 +0200 | [diff] [blame] | 53 | return; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 56 | static bool |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 57 | icmp_manip_pkt(struct sk_buff *skb, |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 58 | const struct nf_nat_l3proto *l3proto, |
| 59 | unsigned int iphdroff, unsigned int hdroff, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 60 | const struct nf_conntrack_tuple *tuple, |
| 61 | enum nf_nat_manip_type maniptype) |
| 62 | { |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 63 | struct icmphdr *hdr; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 64 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 65 | if (!skb_make_writable(skb, hdroff + sizeof(*hdr))) |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 66 | return false; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 67 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 68 | hdr = (struct icmphdr *)(skb->data + hdroff); |
Patrick McHardy | be0ea7d | 2007-11-30 01:17:11 +1100 | [diff] [blame] | 69 | inet_proto_csum_replace2(&hdr->checksum, skb, |
| 70 | hdr->un.echo.id, tuple->src.u.icmp.id, 0); |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 71 | hdr->un.echo.id = tuple->src.u.icmp.id; |
Jan Engelhardt | f2ea825 | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 72 | return true; |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 75 | const struct nf_nat_l4proto nf_nat_l4proto_icmp = { |
| 76 | .l4proto = IPPROTO_ICMP, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 77 | .manip_pkt = icmp_manip_pkt, |
| 78 | .in_range = icmp_in_range, |
| 79 | .unique_tuple = icmp_unique_tuple, |
Patrick McHardy | e281db5c | 2007-03-04 15:57:25 -0800 | [diff] [blame] | 80 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Patrick McHardy | c7232c9 | 2012-08-26 19:14:06 +0200 | [diff] [blame] | 81 | .nlattr_to_range = nf_nat_l4proto_nlattr_to_range, |
Jozsef Kadlecsik | 5b1158e | 2006-12-02 22:07:13 -0800 | [diff] [blame] | 82 | #endif |
| 83 | }; |