nf_nat_proto_dccp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * DCCP NAT protocol helper
  3. *
  4. * Copyright (c) 2005, 2006, 2008 Patrick McHardy <kaber@trash.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/dccp.h>
  16. #include <net/netfilter/nf_conntrack.h>
  17. #include <net/netfilter/nf_nat.h>
  18. #include <net/netfilter/nf_nat_l3proto.h>
  19. #include <net/netfilter/nf_nat_l4proto.h>
  20. static u_int16_t dccp_port_rover;
  21. static void
  22. dccp_unique_tuple(const struct nf_nat_l3proto *l3proto,
  23. struct nf_conntrack_tuple *tuple,
  24. const struct nf_nat_range *range,
  25. enum nf_nat_manip_type maniptype,
  26. const struct nf_conn *ct)
  27. {
  28. nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct,
  29. &dccp_port_rover);
  30. }
  31. static bool
  32. dccp_manip_pkt(struct sk_buff *skb,
  33. const struct nf_nat_l3proto *l3proto,
  34. unsigned int iphdroff, unsigned int hdroff,
  35. const struct nf_conntrack_tuple *tuple,
  36. enum nf_nat_manip_type maniptype)
  37. {
  38. struct dccp_hdr *hdr;
  39. __be16 *portptr, oldport, newport;
  40. int hdrsize = 8; /* DCCP connection tracking guarantees this much */
  41. if (skb->len >= hdroff + sizeof(struct dccp_hdr))
  42. hdrsize = sizeof(struct dccp_hdr);
  43. if (!skb_make_writable(skb, hdroff + hdrsize))
  44. return false;
  45. hdr = (struct dccp_hdr *)(skb->data + hdroff);
  46. if (maniptype == NF_NAT_MANIP_SRC) {
  47. newport = tuple->src.u.dccp.port;
  48. portptr = &hdr->dccph_sport;
  49. } else {
  50. newport = tuple->dst.u.dccp.port;
  51. portptr = &hdr->dccph_dport;
  52. }
  53. oldport = *portptr;
  54. *portptr = newport;
  55. if (hdrsize < sizeof(*hdr))
  56. return true;
  57. l3proto->csum_update(skb, iphdroff, &hdr->dccph_checksum,
  58. tuple, maniptype);
  59. inet_proto_csum_replace2(&hdr->dccph_checksum, skb, oldport, newport,
  60. false);
  61. return true;
  62. }
  63. static const struct nf_nat_l4proto nf_nat_l4proto_dccp = {
  64. .l4proto = IPPROTO_DCCP,
  65. .manip_pkt = dccp_manip_pkt,
  66. .in_range = nf_nat_l4proto_in_range,
  67. .unique_tuple = dccp_unique_tuple,
  68. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  69. .nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
  70. #endif
  71. };
  72. static int __init nf_nat_proto_dccp_init(void)
  73. {
  74. int err;
  75. err = nf_nat_l4proto_register(NFPROTO_IPV4, &nf_nat_l4proto_dccp);
  76. if (err < 0)
  77. goto err1;
  78. err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_dccp);
  79. if (err < 0)
  80. goto err2;
  81. return 0;
  82. err2:
  83. nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_dccp);
  84. err1:
  85. return err;
  86. }
  87. static void __exit nf_nat_proto_dccp_fini(void)
  88. {
  89. nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_dccp);
  90. nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_dccp);
  91. }
  92. module_init(nf_nat_proto_dccp_init);
  93. module_exit(nf_nat_proto_dccp_fini);
  94. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  95. MODULE_DESCRIPTION("DCCP NAT protocol helper");
  96. MODULE_LICENSE("GPL");