xt_TCPMSS.c 8.9 KB

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