xt_TCPOPTSTRIP.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * A module for stripping a specific TCP option from TCP packets.
  3. *
  4. * Copyright (C) 2007 Sven Schnelle <svens@bitebene.org>
  5. * Copyright © CC Computer Consultants GmbH, 2007
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/ip.h>
  14. #include <linux/ipv6.h>
  15. #include <linux/tcp.h>
  16. #include <net/ipv6.h>
  17. #include <net/tcp.h>
  18. #include <linux/netfilter/x_tables.h>
  19. #include <linux/netfilter/xt_TCPOPTSTRIP.h>
  20. static inline unsigned int optlen(const u_int8_t *opt, unsigned int offset)
  21. {
  22. /* Beware zero-length options: make finite progress */
  23. if (opt[offset] <= TCPOPT_NOP || opt[offset+1] == 0)
  24. return 1;
  25. else
  26. return opt[offset+1];
  27. }
  28. static unsigned int
  29. tcpoptstrip_mangle_packet(struct sk_buff *skb,
  30. const struct xt_tcpoptstrip_target_info *info,
  31. unsigned int tcphoff, unsigned int minlen)
  32. {
  33. unsigned int optl, i, j;
  34. struct tcphdr *tcph;
  35. u_int16_t n, o;
  36. u_int8_t *opt;
  37. if (!skb_make_writable(skb, skb->len))
  38. return NF_DROP;
  39. tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
  40. opt = (u_int8_t *)tcph;
  41. /*
  42. * Walk through all TCP options - if we find some option to remove,
  43. * set all octets to %TCPOPT_NOP and adjust checksum.
  44. */
  45. for (i = sizeof(struct tcphdr); i < tcp_hdrlen(skb); i += optl) {
  46. optl = optlen(opt, i);
  47. if (i + optl > tcp_hdrlen(skb))
  48. break;
  49. if (!tcpoptstrip_test_bit(info->strip_bmap, opt[i]))
  50. continue;
  51. for (j = 0; j < optl; ++j) {
  52. o = opt[i+j];
  53. n = TCPOPT_NOP;
  54. if ((i + j) % 2 == 0) {
  55. o <<= 8;
  56. n <<= 8;
  57. }
  58. inet_proto_csum_replace2(&tcph->check, skb, htons(o),
  59. htons(n), 0);
  60. }
  61. memset(opt + i, TCPOPT_NOP, optl);
  62. }
  63. return XT_CONTINUE;
  64. }
  65. static unsigned int
  66. tcpoptstrip_tg4(struct sk_buff *skb, const struct xt_action_param *par)
  67. {
  68. return tcpoptstrip_mangle_packet(skb, par->targinfo, ip_hdrlen(skb),
  69. sizeof(struct iphdr) + sizeof(struct tcphdr));
  70. }
  71. #if defined(CONFIG_IP6_NF_MANGLE) || defined(CONFIG_IP6_NF_MANGLE_MODULE)
  72. static unsigned int
  73. tcpoptstrip_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  74. {
  75. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  76. int tcphoff;
  77. u_int8_t nexthdr;
  78. nexthdr = ipv6h->nexthdr;
  79. tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr);
  80. if (tcphoff < 0)
  81. return NF_DROP;
  82. return tcpoptstrip_mangle_packet(skb, par->targinfo, tcphoff,
  83. sizeof(*ipv6h) + sizeof(struct tcphdr));
  84. }
  85. #endif
  86. static struct xt_target tcpoptstrip_tg_reg[] __read_mostly = {
  87. {
  88. .name = "TCPOPTSTRIP",
  89. .family = NFPROTO_IPV4,
  90. .table = "mangle",
  91. .proto = IPPROTO_TCP,
  92. .target = tcpoptstrip_tg4,
  93. .targetsize = sizeof(struct xt_tcpoptstrip_target_info),
  94. .me = THIS_MODULE,
  95. },
  96. #if defined(CONFIG_IP6_NF_MANGLE) || defined(CONFIG_IP6_NF_MANGLE_MODULE)
  97. {
  98. .name = "TCPOPTSTRIP",
  99. .family = NFPROTO_IPV6,
  100. .table = "mangle",
  101. .proto = IPPROTO_TCP,
  102. .target = tcpoptstrip_tg6,
  103. .targetsize = sizeof(struct xt_tcpoptstrip_target_info),
  104. .me = THIS_MODULE,
  105. },
  106. #endif
  107. };
  108. static int __init tcpoptstrip_tg_init(void)
  109. {
  110. return xt_register_targets(tcpoptstrip_tg_reg,
  111. ARRAY_SIZE(tcpoptstrip_tg_reg));
  112. }
  113. static void __exit tcpoptstrip_tg_exit(void)
  114. {
  115. xt_unregister_targets(tcpoptstrip_tg_reg,
  116. ARRAY_SIZE(tcpoptstrip_tg_reg));
  117. }
  118. module_init(tcpoptstrip_tg_init);
  119. module_exit(tcpoptstrip_tg_exit);
  120. MODULE_AUTHOR("Sven Schnelle <svens@bitebene.org>, Jan Engelhardt <jengelh@medozas.de>");
  121. MODULE_DESCRIPTION("Xtables: TCP option stripping");
  122. MODULE_LICENSE("GPL");
  123. MODULE_ALIAS("ipt_TCPOPTSTRIP");
  124. MODULE_ALIAS("ip6t_TCPOPTSTRIP");