/net/ipv4/netfilter/nf_nat_proto_icmp.c

https://bitbucket.org/abioy/linux · C · 84 lines · 66 code · 10 blank · 8 comment · 5 complexity · b083f61129443b70539f85b2c7a5091e MD5 · raw file

  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. #include <linux/types.h>
  9. #include <linux/init.h>
  10. #include <linux/ip.h>
  11. #include <linux/icmp.h>
  12. #include <linux/netfilter.h>
  13. #include <net/netfilter/nf_nat.h>
  14. #include <net/netfilter/nf_nat_core.h>
  15. #include <net/netfilter/nf_nat_rule.h>
  16. #include <net/netfilter/nf_nat_protocol.h>
  17. static bool
  18. icmp_in_range(const struct nf_conntrack_tuple *tuple,
  19. enum nf_nat_manip_type maniptype,
  20. const union nf_conntrack_man_proto *min,
  21. const union nf_conntrack_man_proto *max)
  22. {
  23. return ntohs(tuple->src.u.icmp.id) >= ntohs(min->icmp.id) &&
  24. ntohs(tuple->src.u.icmp.id) <= ntohs(max->icmp.id);
  25. }
  26. static bool
  27. icmp_unique_tuple(struct nf_conntrack_tuple *tuple,
  28. const struct nf_nat_range *range,
  29. enum nf_nat_manip_type maniptype,
  30. const struct nf_conn *ct)
  31. {
  32. static u_int16_t id;
  33. unsigned int range_size;
  34. unsigned int i;
  35. range_size = ntohs(range->max.icmp.id) - ntohs(range->min.icmp.id) + 1;
  36. /* If no range specified... */
  37. if (!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED))
  38. range_size = 0xFFFF;
  39. for (i = 0; i < range_size; i++, id++) {
  40. tuple->src.u.icmp.id = htons(ntohs(range->min.icmp.id) +
  41. (id % range_size));
  42. if (!nf_nat_used_tuple(tuple, ct))
  43. return true;
  44. }
  45. return false;
  46. }
  47. static bool
  48. icmp_manip_pkt(struct sk_buff *skb,
  49. unsigned int iphdroff,
  50. const struct nf_conntrack_tuple *tuple,
  51. enum nf_nat_manip_type maniptype)
  52. {
  53. const struct iphdr *iph = (struct iphdr *)(skb->data + iphdroff);
  54. struct icmphdr *hdr;
  55. unsigned int hdroff = iphdroff + iph->ihl*4;
  56. if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
  57. return false;
  58. hdr = (struct icmphdr *)(skb->data + hdroff);
  59. inet_proto_csum_replace2(&hdr->checksum, skb,
  60. hdr->un.echo.id, tuple->src.u.icmp.id, 0);
  61. hdr->un.echo.id = tuple->src.u.icmp.id;
  62. return true;
  63. }
  64. const struct nf_nat_protocol nf_nat_protocol_icmp = {
  65. .protonum = IPPROTO_ICMP,
  66. .me = THIS_MODULE,
  67. .manip_pkt = icmp_manip_pkt,
  68. .in_range = icmp_in_range,
  69. .unique_tuple = icmp_unique_tuple,
  70. #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
  71. .range_to_nlattr = nf_nat_proto_range_to_nlattr,
  72. .nlattr_to_range = nf_nat_proto_nlattr_to_range,
  73. #endif
  74. };