Commit | Line | Data |
---|---|---|
5b1158e9 JK |
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> | |
bc3b2d7f | 11 | #include <linux/export.h> |
5b1158e9 JK |
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> | |
c7232c99 | 18 | #include <net/netfilter/nf_nat_l4proto.h> |
5b1158e9 | 19 | |
f2ea825f | 20 | static bool |
5b1158e9 JK |
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 | ||
f43dc98b | 30 | static void |
c7232c99 PM |
31 | icmp_unique_tuple(const struct nf_nat_l3proto *l3proto, |
32 | struct nf_conntrack_tuple *tuple, | |
33 | const struct nf_nat_range *range, | |
5b1158e9 JK |
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 | ||
c7232c99 PM |
41 | range_size = ntohs(range->max_proto.icmp.id) - |
42 | ntohs(range->min_proto.icmp.id) + 1; | |
5b1158e9 | 43 | /* If no range specified... */ |
cbc9f2f4 | 44 | if (!(range->flags & NF_NAT_RANGE_PROTO_SPECIFIED)) |
5b1158e9 JK |
45 | range_size = 0xFFFF; |
46 | ||
2452a99d | 47 | for (i = 0; ; ++id) { |
c7232c99 | 48 | tuple->src.u.icmp.id = htons(ntohs(range->min_proto.icmp.id) + |
e905a9ed | 49 | (id % range_size)); |
2452a99d | 50 | if (++i == range_size || !nf_nat_used_tuple(tuple, ct)) |
f43dc98b | 51 | return; |
5b1158e9 | 52 | } |
f43dc98b | 53 | return; |
5b1158e9 JK |
54 | } |
55 | ||
f2ea825f | 56 | static bool |
3db05fea | 57 | icmp_manip_pkt(struct sk_buff *skb, |
c7232c99 PM |
58 | const struct nf_nat_l3proto *l3proto, |
59 | unsigned int iphdroff, unsigned int hdroff, | |
5b1158e9 JK |
60 | const struct nf_conntrack_tuple *tuple, |
61 | enum nf_nat_manip_type maniptype) | |
62 | { | |
5b1158e9 | 63 | struct icmphdr *hdr; |
5b1158e9 | 64 | |
3db05fea | 65 | if (!skb_make_writable(skb, hdroff + sizeof(*hdr))) |
f2ea825f | 66 | return false; |
5b1158e9 | 67 | |
3db05fea | 68 | hdr = (struct icmphdr *)(skb->data + hdroff); |
be0ea7d5 | 69 | inet_proto_csum_replace2(&hdr->checksum, skb, |
4b048d6d | 70 | hdr->un.echo.id, tuple->src.u.icmp.id, false); |
5b1158e9 | 71 | hdr->un.echo.id = tuple->src.u.icmp.id; |
f2ea825f | 72 | return true; |
5b1158e9 JK |
73 | } |
74 | ||
c7232c99 PM |
75 | const struct nf_nat_l4proto nf_nat_l4proto_icmp = { |
76 | .l4proto = IPPROTO_ICMP, | |
5b1158e9 JK |
77 | .manip_pkt = icmp_manip_pkt, |
78 | .in_range = icmp_in_range, | |
79 | .unique_tuple = icmp_unique_tuple, | |
24de3d37 | 80 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
c7232c99 | 81 | .nlattr_to_range = nf_nat_l4proto_nlattr_to_range, |
5b1158e9 JK |
82 | #endif |
83 | }; |