xt_TCPOPTSTRIP.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_action_param *par,
  31. unsigned int tcphoff, unsigned int minlen)
  32. {
  33. const struct xt_tcpoptstrip_target_info *info = par->targinfo;
  34. unsigned int optl, i, j;
  35. struct tcphdr *tcph;
  36. u_int16_t n, o;
  37. u_int8_t *opt;
  38. int len, tcp_hdrlen;
  39. /* This is a fragment, no TCP header is available */
  40. if (par->fragoff != 0)
  41. return XT_CONTINUE;
  42. if (!skb_make_writable(skb, skb->len))
  43. return NF_DROP;
  44. len = skb->len - tcphoff;
  45. if (len < (int)sizeof(struct tcphdr))
  46. return NF_DROP;
  47. tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
  48. tcp_hdrlen = tcph->doff * 4;
  49. if (len < tcp_hdrlen)
  50. return NF_DROP;
  51. opt = (u_int8_t *)tcph;
  52. /*
  53. * Walk through all TCP options - if we find some option to remove,
  54. * set all octets to %TCPOPT_NOP and adjust checksum.
  55. */
  56. for (i = sizeof(struct tcphdr); i < tcp_hdrlen - 1; i += optl) {
  57. optl = optlen(opt, i);
  58. if (i + optl > tcp_hdrlen)
  59. break;
  60. if (!tcpoptstrip_test_bit(info->strip_bmap, opt[i]))
  61. continue;
  62. for (j = 0; j < optl; ++j) {
  63. o = opt[i+j];
  64. n = TCPOPT_NOP;
  65. if ((i + j) % 2 == 0) {
  66. o <<= 8;
  67. n <<= 8;
  68. }
  69. inet_proto_csum_replace2(&tcph->check, skb, htons(o),
  70. htons(n), false);
  71. }
  72. memset(opt + i, TCPOPT_NOP, optl);
  73. }
  74. return XT_CONTINUE;
  75. }
  76. static unsigned int
  77. tcpoptstrip_tg4(struct sk_buff *skb, const struct xt_action_param *par)
  78. {
  79. return tcpoptstrip_mangle_packet(skb, par, ip_hdrlen(skb),
  80. sizeof(struct iphdr) + sizeof(struct tcphdr));
  81. }
  82. #if IS_ENABLED(CONFIG_IP6_NF_MANGLE)
  83. static unsigned int
  84. tcpoptstrip_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  85. {
  86. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  87. int tcphoff;
  88. u_int8_t nexthdr;
  89. __be16 frag_off;
  90. nexthdr = ipv6h->nexthdr;
  91. tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr, &frag_off);
  92. if (tcphoff < 0)
  93. return NF_DROP;
  94. return tcpoptstrip_mangle_packet(skb, par, tcphoff,
  95. sizeof(*ipv6h) + sizeof(struct tcphdr));
  96. }
  97. #endif
  98. static struct xt_target tcpoptstrip_tg_reg[] __read_mostly = {
  99. {
  100. .name = "TCPOPTSTRIP",
  101. .family = NFPROTO_IPV4,
  102. .table = "mangle",
  103. .proto = IPPROTO_TCP,
  104. .target = tcpoptstrip_tg4,
  105. .targetsize = sizeof(struct xt_tcpoptstrip_target_info),
  106. .me = THIS_MODULE,
  107. },
  108. #if IS_ENABLED(CONFIG_IP6_NF_MANGLE)
  109. {
  110. .name = "TCPOPTSTRIP",
  111. .family = NFPROTO_IPV6,
  112. .table = "mangle",
  113. .proto = IPPROTO_TCP,
  114. .target = tcpoptstrip_tg6,
  115. .targetsize = sizeof(struct xt_tcpoptstrip_target_info),
  116. .me = THIS_MODULE,
  117. },
  118. #endif
  119. };
  120. static int __init tcpoptstrip_tg_init(void)
  121. {
  122. return xt_register_targets(tcpoptstrip_tg_reg,
  123. ARRAY_SIZE(tcpoptstrip_tg_reg));
  124. }
  125. static void __exit tcpoptstrip_tg_exit(void)
  126. {
  127. xt_unregister_targets(tcpoptstrip_tg_reg,
  128. ARRAY_SIZE(tcpoptstrip_tg_reg));
  129. }
  130. module_init(tcpoptstrip_tg_init);
  131. module_exit(tcpoptstrip_tg_exit);
  132. MODULE_AUTHOR("Sven Schnelle <svens@bitebene.org>, Jan Engelhardt <jengelh@medozas.de>");
  133. MODULE_DESCRIPTION("Xtables: TCP option stripping");
  134. MODULE_LICENSE("GPL");
  135. MODULE_ALIAS("ipt_TCPOPTSTRIP");
  136. MODULE_ALIAS("ip6t_TCPOPTSTRIP");