nf_nat_proto_sctp.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
  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/sctp.h>
  11. #include <linux/module.h>
  12. #include <net/sctp/checksum.h>
  13. #include <net/netfilter/nf_nat_l4proto.h>
  14. static u_int16_t nf_sctp_port_rover;
  15. static void
  16. sctp_unique_tuple(const struct nf_nat_l3proto *l3proto,
  17. struct nf_conntrack_tuple *tuple,
  18. const struct nf_nat_range *range,
  19. enum nf_nat_manip_type maniptype,
  20. const struct nf_conn *ct)
  21. {
  22. nf_nat_l4proto_unique_tuple(l3proto, tuple, range, maniptype, ct,
  23. &nf_sctp_port_rover);
  24. }
  25. static bool
  26. sctp_manip_pkt(struct sk_buff *skb,
  27. const struct nf_nat_l3proto *l3proto,
  28. unsigned int iphdroff, unsigned int hdroff,
  29. const struct nf_conntrack_tuple *tuple,
  30. enum nf_nat_manip_type maniptype)
  31. {
  32. sctp_sctphdr_t *hdr;
  33. if (!skb_make_writable(skb, hdroff + sizeof(*hdr)))
  34. return false;
  35. hdr = (struct sctphdr *)(skb->data + hdroff);
  36. if (maniptype == NF_NAT_MANIP_SRC) {
  37. /* Get rid of src port */
  38. hdr->source = tuple->src.u.sctp.port;
  39. } else {
  40. /* Get rid of dst port */
  41. hdr->dest = tuple->dst.u.sctp.port;
  42. }
  43. hdr->checksum = sctp_compute_cksum(skb, hdroff);
  44. return true;
  45. }
  46. static const struct nf_nat_l4proto nf_nat_l4proto_sctp = {
  47. .l4proto = IPPROTO_SCTP,
  48. .manip_pkt = sctp_manip_pkt,
  49. .in_range = nf_nat_l4proto_in_range,
  50. .unique_tuple = sctp_unique_tuple,
  51. #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
  52. .nlattr_to_range = nf_nat_l4proto_nlattr_to_range,
  53. #endif
  54. };
  55. static int __init nf_nat_proto_sctp_init(void)
  56. {
  57. int err;
  58. err = nf_nat_l4proto_register(NFPROTO_IPV4, &nf_nat_l4proto_sctp);
  59. if (err < 0)
  60. goto err1;
  61. err = nf_nat_l4proto_register(NFPROTO_IPV6, &nf_nat_l4proto_sctp);
  62. if (err < 0)
  63. goto err2;
  64. return 0;
  65. err2:
  66. nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_sctp);
  67. err1:
  68. return err;
  69. }
  70. static void __exit nf_nat_proto_sctp_exit(void)
  71. {
  72. nf_nat_l4proto_unregister(NFPROTO_IPV6, &nf_nat_l4proto_sctp);
  73. nf_nat_l4proto_unregister(NFPROTO_IPV4, &nf_nat_l4proto_sctp);
  74. }
  75. module_init(nf_nat_proto_sctp_init);
  76. module_exit(nf_nat_proto_sctp_exit);
  77. MODULE_LICENSE("GPL");
  78. MODULE_DESCRIPTION("SCTP NAT protocol helper");
  79. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");