ipt_ECN.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* iptables module for the IPv4 and TCP ECN bits, Version 1.5
  2. *
  3. * (C) 2002 by Harald Welte <laforge@netfilter.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/in.h>
  11. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/ip.h>
  14. #include <net/ip.h>
  15. #include <linux/tcp.h>
  16. #include <net/checksum.h>
  17. #include <linux/netfilter/x_tables.h>
  18. #include <linux/netfilter_ipv4/ip_tables.h>
  19. #include <linux/netfilter_ipv4/ipt_ECN.h>
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  22. MODULE_DESCRIPTION("Xtables: Explicit Congestion Notification (ECN) flag modification");
  23. /* set ECT codepoint from IP header.
  24. * return false if there was an error. */
  25. static inline bool
  26. set_ect_ip(struct sk_buff *skb, const struct ipt_ECN_info *einfo)
  27. {
  28. struct iphdr *iph = ip_hdr(skb);
  29. if ((iph->tos & IPT_ECN_IP_MASK) != (einfo->ip_ect & IPT_ECN_IP_MASK)) {
  30. __u8 oldtos;
  31. if (!skb_make_writable(skb, sizeof(struct iphdr)))
  32. return false;
  33. iph = ip_hdr(skb);
  34. oldtos = iph->tos;
  35. iph->tos &= ~IPT_ECN_IP_MASK;
  36. iph->tos |= (einfo->ip_ect & IPT_ECN_IP_MASK);
  37. csum_replace2(&iph->check, htons(oldtos), htons(iph->tos));
  38. }
  39. return true;
  40. }
  41. /* Return false if there was an error. */
  42. static inline bool
  43. set_ect_tcp(struct sk_buff *skb, const struct ipt_ECN_info *einfo)
  44. {
  45. struct tcphdr _tcph, *tcph;
  46. __be16 oldval;
  47. /* Not enough header? */
  48. tcph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_tcph), &_tcph);
  49. if (!tcph)
  50. return false;
  51. if ((!(einfo->operation & IPT_ECN_OP_SET_ECE) ||
  52. tcph->ece == einfo->proto.tcp.ece) &&
  53. (!(einfo->operation & IPT_ECN_OP_SET_CWR) ||
  54. tcph->cwr == einfo->proto.tcp.cwr))
  55. return true;
  56. if (!skb_make_writable(skb, ip_hdrlen(skb) + sizeof(*tcph)))
  57. return false;
  58. tcph = (void *)ip_hdr(skb) + ip_hdrlen(skb);
  59. oldval = ((__be16 *)tcph)[6];
  60. if (einfo->operation & IPT_ECN_OP_SET_ECE)
  61. tcph->ece = einfo->proto.tcp.ece;
  62. if (einfo->operation & IPT_ECN_OP_SET_CWR)
  63. tcph->cwr = einfo->proto.tcp.cwr;
  64. inet_proto_csum_replace2(&tcph->check, skb,
  65. oldval, ((__be16 *)tcph)[6], false);
  66. return true;
  67. }
  68. static unsigned int
  69. ecn_tg(struct sk_buff *skb, const struct xt_action_param *par)
  70. {
  71. const struct ipt_ECN_info *einfo = par->targinfo;
  72. if (einfo->operation & IPT_ECN_OP_SET_IP)
  73. if (!set_ect_ip(skb, einfo))
  74. return NF_DROP;
  75. if (einfo->operation & (IPT_ECN_OP_SET_ECE | IPT_ECN_OP_SET_CWR) &&
  76. ip_hdr(skb)->protocol == IPPROTO_TCP)
  77. if (!set_ect_tcp(skb, einfo))
  78. return NF_DROP;
  79. return XT_CONTINUE;
  80. }
  81. static int ecn_tg_check(const struct xt_tgchk_param *par)
  82. {
  83. const struct ipt_ECN_info *einfo = par->targinfo;
  84. const struct ipt_entry *e = par->entryinfo;
  85. if (einfo->operation & IPT_ECN_OP_MASK) {
  86. pr_info("unsupported ECN operation %x\n", einfo->operation);
  87. return -EINVAL;
  88. }
  89. if (einfo->ip_ect & ~IPT_ECN_IP_MASK) {
  90. pr_info("new ECT codepoint %x out of mask\n", einfo->ip_ect);
  91. return -EINVAL;
  92. }
  93. if ((einfo->operation & (IPT_ECN_OP_SET_ECE|IPT_ECN_OP_SET_CWR)) &&
  94. (e->ip.proto != IPPROTO_TCP || (e->ip.invflags & XT_INV_PROTO))) {
  95. pr_info("cannot use TCP operations on a non-tcp rule\n");
  96. return -EINVAL;
  97. }
  98. return 0;
  99. }
  100. static struct xt_target ecn_tg_reg __read_mostly = {
  101. .name = "ECN",
  102. .family = NFPROTO_IPV4,
  103. .target = ecn_tg,
  104. .targetsize = sizeof(struct ipt_ECN_info),
  105. .table = "mangle",
  106. .checkentry = ecn_tg_check,
  107. .me = THIS_MODULE,
  108. };
  109. static int __init ecn_tg_init(void)
  110. {
  111. return xt_register_target(&ecn_tg_reg);
  112. }
  113. static void __exit ecn_tg_exit(void)
  114. {
  115. xt_unregister_target(&ecn_tg_reg);
  116. }
  117. module_init(ecn_tg_init);
  118. module_exit(ecn_tg_exit);