xt_TCPMSS.c 8.9 KB

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