xt_TCPMSS.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * This is a module which is used for setting the MSS option in TCP packets.
  3. *
  4. * Copyright (C) 2000 Marc Boucher <marc@mbsi.ca>
  5. * Copyright (C) 2007 Patrick McHardy <kaber@trash.net>
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/ip.h>
  15. #include <linux/gfp.h>
  16. #include <linux/ipv6.h>
  17. #include <linux/tcp.h>
  18. #include <net/dst.h>
  19. #include <net/flow.h>
  20. #include <net/ipv6.h>
  21. #include <net/route.h>
  22. #include <net/tcp.h>
  23. #include <linux/netfilter_ipv4/ip_tables.h>
  24. #include <linux/netfilter_ipv6/ip6_tables.h>
  25. #include <linux/netfilter/x_tables.h>
  26. #include <linux/netfilter/xt_tcpudp.h>
  27. #include <linux/netfilter/xt_TCPMSS.h>
  28. MODULE_LICENSE("GPL");
  29. MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
  30. MODULE_DESCRIPTION("Xtables: TCP Maximum Segment Size (MSS) adjustment");
  31. MODULE_ALIAS("ipt_TCPMSS");
  32. MODULE_ALIAS("ip6t_TCPMSS");
  33. static inline unsigned int
  34. optlen(const u_int8_t *opt, unsigned int offset)
  35. {
  36. /* Beware zero-length options: make finite progress */
  37. if (opt[offset] <= TCPOPT_NOP || opt[offset+1] == 0)
  38. return 1;
  39. else
  40. return opt[offset+1];
  41. }
  42. static u_int32_t tcpmss_reverse_mtu(struct net *net,
  43. const struct sk_buff *skb,
  44. unsigned int family)
  45. {
  46. struct flowi fl;
  47. const struct nf_afinfo *ai;
  48. struct rtable *rt = NULL;
  49. u_int32_t mtu = ~0U;
  50. if (family == PF_INET) {
  51. struct flowi4 *fl4 = &fl.u.ip4;
  52. memset(fl4, 0, sizeof(*fl4));
  53. fl4->daddr = ip_hdr(skb)->saddr;
  54. } else {
  55. struct flowi6 *fl6 = &fl.u.ip6;
  56. memset(fl6, 0, sizeof(*fl6));
  57. fl6->daddr = ipv6_hdr(skb)->saddr;
  58. }
  59. rcu_read_lock();
  60. ai = nf_get_afinfo(family);
  61. if (ai != NULL)
  62. ai->route(net, (struct dst_entry **)&rt, &fl, false);
  63. rcu_read_unlock();
  64. if (rt != NULL) {
  65. mtu = dst_mtu(&rt->dst);
  66. dst_release(&rt->dst);
  67. }
  68. return mtu;
  69. }
  70. static int
  71. tcpmss_mangle_packet(struct sk_buff *skb,
  72. const struct xt_action_param *par,
  73. unsigned int family,
  74. unsigned int tcphoff,
  75. unsigned int minlen)
  76. {
  77. const struct xt_tcpmss_info *info = par->targinfo;
  78. struct tcphdr *tcph;
  79. int len, tcp_hdrlen;
  80. unsigned int i;
  81. __be16 oldval;
  82. u16 newmss;
  83. u8 *opt;
  84. /* This is a fragment, no TCP header is available */
  85. if (par->fragoff != 0)
  86. return 0;
  87. if (!skb_make_writable(skb, skb->len))
  88. return -1;
  89. len = skb->len - tcphoff;
  90. if (len < (int)sizeof(struct tcphdr))
  91. return -1;
  92. tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
  93. tcp_hdrlen = tcph->doff * 4;
  94. if (len < tcp_hdrlen)
  95. return -1;
  96. if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
  97. struct net *net = dev_net(par->in ? par->in : par->out);
  98. unsigned int in_mtu = tcpmss_reverse_mtu(net, skb, family);
  99. if (dst_mtu(skb_dst(skb)) <= minlen) {
  100. net_err_ratelimited("unknown or invalid path-MTU (%u)\n",
  101. dst_mtu(skb_dst(skb)));
  102. return -1;
  103. }
  104. if (in_mtu <= minlen) {
  105. net_err_ratelimited("unknown or invalid path-MTU (%u)\n",
  106. in_mtu);
  107. return -1;
  108. }
  109. newmss = min(dst_mtu(skb_dst(skb)), in_mtu) - minlen;
  110. } else
  111. newmss = info->mss;
  112. opt = (u_int8_t *)tcph;
  113. for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
  114. if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
  115. u_int16_t oldmss;
  116. oldmss = (opt[i+2] << 8) | opt[i+3];
  117. /* Never increase MSS, even when setting it, as
  118. * doing so results in problems for hosts that rely
  119. * on MSS being set correctly.
  120. */
  121. if (oldmss <= newmss)
  122. return 0;
  123. opt[i+2] = (newmss & 0xff00) >> 8;
  124. opt[i+3] = newmss & 0x00ff;
  125. inet_proto_csum_replace2(&tcph->check, skb,
  126. htons(oldmss), htons(newmss),
  127. 0);
  128. return 0;
  129. }
  130. }
  131. /* There is data after the header so the option can't be added
  132. * without moving it, and doing so may make the SYN packet
  133. * itself too large. Accept the packet unmodified instead.
  134. */
  135. if (len > tcp_hdrlen)
  136. return 0;
  137. /*
  138. * MSS Option not found ?! add it..
  139. */
  140. if (skb_tailroom(skb) < TCPOLEN_MSS) {
  141. if (pskb_expand_head(skb, 0,
  142. TCPOLEN_MSS - skb_tailroom(skb),
  143. GFP_ATOMIC))
  144. return -1;
  145. tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
  146. }
  147. skb_put(skb, TCPOLEN_MSS);
  148. /*
  149. * IPv4: RFC 1122 states "If an MSS option is not received at
  150. * connection setup, TCP MUST assume a default send MSS of 536".
  151. * IPv6: RFC 2460 states IPv6 has a minimum MTU of 1280 and a minimum
  152. * length IPv6 header of 60, ergo the default MSS value is 1220
  153. * Since no MSS was provided, we must use the default values
  154. */
  155. if (par->family == NFPROTO_IPV4)
  156. newmss = min(newmss, (u16)536);
  157. else
  158. newmss = min(newmss, (u16)1220);
  159. opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
  160. memmove(opt + TCPOLEN_MSS, opt, len - sizeof(struct tcphdr));
  161. inet_proto_csum_replace2(&tcph->check, skb,
  162. htons(len), htons(len + TCPOLEN_MSS), 1);
  163. opt[0] = TCPOPT_MSS;
  164. opt[1] = TCPOLEN_MSS;
  165. opt[2] = (newmss & 0xff00) >> 8;
  166. opt[3] = newmss & 0x00ff;
  167. inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), 0);
  168. oldval = ((__be16 *)tcph)[6];
  169. tcph->doff += TCPOLEN_MSS/4;
  170. inet_proto_csum_replace2(&tcph->check, skb,
  171. oldval, ((__be16 *)tcph)[6], 0);
  172. return TCPOLEN_MSS;
  173. }
  174. static unsigned int
  175. tcpmss_tg4(struct sk_buff *skb, const struct xt_action_param *par)
  176. {
  177. struct iphdr *iph = ip_hdr(skb);
  178. __be16 newlen;
  179. int ret;
  180. ret = tcpmss_mangle_packet(skb, par,
  181. PF_INET,
  182. iph->ihl * 4,
  183. sizeof(*iph) + sizeof(struct tcphdr));
  184. if (ret < 0)
  185. return NF_DROP;
  186. if (ret > 0) {
  187. iph = ip_hdr(skb);
  188. newlen = htons(ntohs(iph->tot_len) + ret);
  189. csum_replace2(&iph->check, iph->tot_len, newlen);
  190. iph->tot_len = newlen;
  191. }
  192. return XT_CONTINUE;
  193. }
  194. #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
  195. static unsigned int
  196. tcpmss_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  197. {
  198. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  199. u8 nexthdr;
  200. __be16 frag_off;
  201. int tcphoff;
  202. int ret;
  203. nexthdr = ipv6h->nexthdr;
  204. tcphoff = ipv6_skip_exthdr(skb, sizeof(*ipv6h), &nexthdr, &frag_off);
  205. if (tcphoff < 0)
  206. return NF_DROP;
  207. ret = tcpmss_mangle_packet(skb, par,
  208. PF_INET6,
  209. tcphoff,
  210. sizeof(*ipv6h) + sizeof(struct tcphdr));
  211. if (ret < 0)
  212. return NF_DROP;
  213. if (ret > 0) {
  214. ipv6h = ipv6_hdr(skb);
  215. ipv6h->payload_len = htons(ntohs(ipv6h->payload_len) + ret);
  216. }
  217. return XT_CONTINUE;
  218. }
  219. #endif
  220. /* Must specify -p tcp --syn */
  221. static inline bool find_syn_match(const struct xt_entry_match *m)
  222. {
  223. const struct xt_tcp *tcpinfo = (const struct xt_tcp *)m->data;
  224. if (strcmp(m->u.kernel.match->name, "tcp") == 0 &&
  225. tcpinfo->flg_cmp & TCPHDR_SYN &&
  226. !(tcpinfo->invflags & XT_TCP_INV_FLAGS))
  227. return true;
  228. return false;
  229. }
  230. static int tcpmss_tg4_check(const struct xt_tgchk_param *par)
  231. {
  232. const struct xt_tcpmss_info *info = par->targinfo;
  233. const struct ipt_entry *e = par->entryinfo;
  234. const struct xt_entry_match *ematch;
  235. if (info->mss == XT_TCPMSS_CLAMP_PMTU &&
  236. (par->hook_mask & ~((1 << NF_INET_FORWARD) |
  237. (1 << NF_INET_LOCAL_OUT) |
  238. (1 << NF_INET_POST_ROUTING))) != 0) {
  239. pr_info("path-MTU clamping only supported in "
  240. "FORWARD, OUTPUT and POSTROUTING hooks\n");
  241. return -EINVAL;
  242. }
  243. xt_ematch_foreach(ematch, e)
  244. if (find_syn_match(ematch))
  245. return 0;
  246. pr_info("Only works on TCP SYN packets\n");
  247. return -EINVAL;
  248. }
  249. #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
  250. static int tcpmss_tg6_check(const struct xt_tgchk_param *par)
  251. {
  252. const struct xt_tcpmss_info *info = par->targinfo;
  253. const struct ip6t_entry *e = par->entryinfo;
  254. const struct xt_entry_match *ematch;
  255. if (info->mss == XT_TCPMSS_CLAMP_PMTU &&
  256. (par->hook_mask & ~((1 << NF_INET_FORWARD) |
  257. (1 << NF_INET_LOCAL_OUT) |
  258. (1 << NF_INET_POST_ROUTING))) != 0) {
  259. pr_info("path-MTU clamping only supported in "
  260. "FORWARD, OUTPUT and POSTROUTING hooks\n");
  261. return -EINVAL;
  262. }
  263. xt_ematch_foreach(ematch, e)
  264. if (find_syn_match(ematch))
  265. return 0;
  266. pr_info("Only works on TCP SYN packets\n");
  267. return -EINVAL;
  268. }
  269. #endif
  270. static struct xt_target tcpmss_tg_reg[] __read_mostly = {
  271. {
  272. .family = NFPROTO_IPV4,
  273. .name = "TCPMSS",
  274. .checkentry = tcpmss_tg4_check,
  275. .target = tcpmss_tg4,
  276. .targetsize = sizeof(struct xt_tcpmss_info),
  277. .proto = IPPROTO_TCP,
  278. .me = THIS_MODULE,
  279. },
  280. #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
  281. {
  282. .family = NFPROTO_IPV6,
  283. .name = "TCPMSS",
  284. .checkentry = tcpmss_tg6_check,
  285. .target = tcpmss_tg6,
  286. .targetsize = sizeof(struct xt_tcpmss_info),
  287. .proto = IPPROTO_TCP,
  288. .me = THIS_MODULE,
  289. },
  290. #endif
  291. };
  292. static int __init tcpmss_tg_init(void)
  293. {
  294. return xt_register_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg));
  295. }
  296. static void __exit tcpmss_tg_exit(void)
  297. {
  298. xt_unregister_targets(tcpmss_tg_reg, ARRAY_SIZE(tcpmss_tg_reg));
  299. }
  300. module_init(tcpmss_tg_init);
  301. module_exit(tcpmss_tg_exit);