1 #include <linux/types.h>
2 #include <linux/init.h>
3 #include <linux/netfilter.h>
4 #include <linux/ip.h>
5 #include <linux/icmp.h>
6 #include <linux/if.h>
7 
8 #include <linux/netfilter_ipv4/ip_nat.h>
9 #include <linux/netfilter_ipv4/ip_nat_rule.h>
10 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
11 
12 static int
icmp_in_range(const struct ip_conntrack_tuple * tuple,enum ip_nat_manip_type maniptype,const union ip_conntrack_manip_proto * min,const union ip_conntrack_manip_proto * max)13 icmp_in_range(const struct ip_conntrack_tuple *tuple,
14 	      enum ip_nat_manip_type maniptype,
15 	      const union ip_conntrack_manip_proto *min,
16 	      const union ip_conntrack_manip_proto *max)
17 {
18 	return (tuple->src.u.icmp.id >= min->icmp.id
19 		&& tuple->src.u.icmp.id <= max->icmp.id);
20 }
21 
22 static int
icmp_unique_tuple(struct ip_conntrack_tuple * tuple,const struct ip_nat_range * range,enum ip_nat_manip_type maniptype,const struct ip_conntrack * conntrack)23 icmp_unique_tuple(struct ip_conntrack_tuple *tuple,
24 		  const struct ip_nat_range *range,
25 		  enum ip_nat_manip_type maniptype,
26 		  const struct ip_conntrack *conntrack)
27 {
28 	static u_int16_t id = 0;
29 	unsigned int range_size;
30 	unsigned int i;
31 
32 	range_size = ntohs(range->max.icmp.id) - ntohs(range->min.icmp.id) + 1;
33 	/* If no range specified... */
34 	if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED))
35 		range_size = 0xFFFF;
36 
37 	for (i = 0; i < range_size; i++, id++) {
38 		tuple->src.u.icmp.id = htons(ntohs(range->min.icmp.id) +
39 		                             (id % range_size));
40 		if (!ip_nat_used_tuple(tuple, conntrack))
41 			return 1;
42 	}
43 	return 0;
44 }
45 
46 static void
icmp_manip_pkt(struct iphdr * iph,size_t len,const struct ip_conntrack_manip * manip,enum ip_nat_manip_type maniptype)47 icmp_manip_pkt(struct iphdr *iph, size_t len,
48 	       const struct ip_conntrack_manip *manip,
49 	       enum ip_nat_manip_type maniptype)
50 {
51 	struct icmphdr *hdr = (struct icmphdr *)((u_int32_t *)iph + iph->ihl);
52 
53 	hdr->checksum = ip_nat_cheat_check(hdr->un.echo.id ^ 0xFFFF,
54 					   manip->u.icmp.id,
55 					   hdr->checksum);
56 	hdr->un.echo.id = manip->u.icmp.id;
57 }
58 
59 static unsigned int
icmp_print(char * buffer,const struct ip_conntrack_tuple * match,const struct ip_conntrack_tuple * mask)60 icmp_print(char *buffer,
61 	   const struct ip_conntrack_tuple *match,
62 	   const struct ip_conntrack_tuple *mask)
63 {
64 	unsigned int len = 0;
65 
66 	if (mask->src.u.icmp.id)
67 		len += sprintf(buffer + len, "id=%u ",
68 			       ntohs(match->src.u.icmp.id));
69 
70 	if (mask->dst.u.icmp.type)
71 		len += sprintf(buffer + len, "type=%u ",
72 			       ntohs(match->dst.u.icmp.type));
73 
74 	if (mask->dst.u.icmp.code)
75 		len += sprintf(buffer + len, "code=%u ",
76 			       ntohs(match->dst.u.icmp.code));
77 
78 	return len;
79 }
80 
81 static unsigned int
icmp_print_range(char * buffer,const struct ip_nat_range * range)82 icmp_print_range(char *buffer, const struct ip_nat_range *range)
83 {
84 	if (range->min.icmp.id != 0 || range->max.icmp.id != 0xFFFF)
85 		return sprintf(buffer, "id %u-%u ",
86 			       ntohs(range->min.icmp.id),
87 			       ntohs(range->max.icmp.id));
88 	else return 0;
89 }
90 
91 struct ip_nat_protocol ip_nat_protocol_icmp
92 = { { NULL, NULL }, "ICMP", IPPROTO_ICMP,
93     icmp_manip_pkt,
94     icmp_in_range,
95     icmp_unique_tuple,
96     icmp_print,
97     icmp_print_range
98 };
99