ipt_SYNPROXY.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (c) 2013 Patrick McHardy <kaber@trash.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <net/tcp.h>
  11. #include <linux/netfilter_ipv4/ip_tables.h>
  12. #include <linux/netfilter/x_tables.h>
  13. #include <linux/netfilter/xt_SYNPROXY.h>
  14. #include <net/netfilter/nf_conntrack.h>
  15. #include <net/netfilter/nf_conntrack_seqadj.h>
  16. #include <net/netfilter/nf_conntrack_synproxy.h>
  17. static struct iphdr *
  18. synproxy_build_ip(struct sk_buff *skb, __be32 saddr, __be32 daddr)
  19. {
  20. struct iphdr *iph;
  21. skb_reset_network_header(skb);
  22. iph = (struct iphdr *)skb_put(skb, sizeof(*iph));
  23. iph->version = 4;
  24. iph->ihl = sizeof(*iph) / 4;
  25. iph->tos = 0;
  26. iph->id = 0;
  27. iph->frag_off = htons(IP_DF);
  28. iph->ttl = sysctl_ip_default_ttl;
  29. iph->protocol = IPPROTO_TCP;
  30. iph->check = 0;
  31. iph->saddr = saddr;
  32. iph->daddr = daddr;
  33. return iph;
  34. }
  35. static void
  36. synproxy_send_tcp(const struct synproxy_net *snet,
  37. const struct sk_buff *skb, struct sk_buff *nskb,
  38. struct nf_conntrack *nfct, enum ip_conntrack_info ctinfo,
  39. struct iphdr *niph, struct tcphdr *nth,
  40. unsigned int tcp_hdr_size)
  41. {
  42. struct net *net = nf_ct_net(snet->tmpl);
  43. nth->check = ~tcp_v4_check(tcp_hdr_size, niph->saddr, niph->daddr, 0);
  44. nskb->ip_summed = CHECKSUM_PARTIAL;
  45. nskb->csum_start = (unsigned char *)nth - nskb->head;
  46. nskb->csum_offset = offsetof(struct tcphdr, check);
  47. skb_dst_set_noref(nskb, skb_dst(skb));
  48. nskb->protocol = htons(ETH_P_IP);
  49. if (ip_route_me_harder(net, nskb, RTN_UNSPEC))
  50. goto free_nskb;
  51. if (nfct) {
  52. nskb->nfct = nfct;
  53. nskb->nfctinfo = ctinfo;
  54. nf_conntrack_get(nfct);
  55. }
  56. ip_local_out(net, nskb->sk, nskb);
  57. return;
  58. free_nskb:
  59. kfree_skb(nskb);
  60. }
  61. static void
  62. synproxy_send_client_synack(const struct synproxy_net *snet,
  63. const struct sk_buff *skb, const struct tcphdr *th,
  64. const struct synproxy_options *opts)
  65. {
  66. struct sk_buff *nskb;
  67. struct iphdr *iph, *niph;
  68. struct tcphdr *nth;
  69. unsigned int tcp_hdr_size;
  70. u16 mss = opts->mss;
  71. iph = ip_hdr(skb);
  72. tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
  73. nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
  74. GFP_ATOMIC);
  75. if (nskb == NULL)
  76. return;
  77. skb_reserve(nskb, MAX_TCP_HEADER);
  78. niph = synproxy_build_ip(nskb, iph->daddr, iph->saddr);
  79. skb_reset_transport_header(nskb);
  80. nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
  81. nth->source = th->dest;
  82. nth->dest = th->source;
  83. nth->seq = htonl(__cookie_v4_init_sequence(iph, th, &mss));
  84. nth->ack_seq = htonl(ntohl(th->seq) + 1);
  85. tcp_flag_word(nth) = TCP_FLAG_SYN | TCP_FLAG_ACK;
  86. if (opts->options & XT_SYNPROXY_OPT_ECN)
  87. tcp_flag_word(nth) |= TCP_FLAG_ECE;
  88. nth->doff = tcp_hdr_size / 4;
  89. nth->window = 0;
  90. nth->check = 0;
  91. nth->urg_ptr = 0;
  92. synproxy_build_options(nth, opts);
  93. synproxy_send_tcp(snet, skb, nskb, skb->nfct, IP_CT_ESTABLISHED_REPLY,
  94. niph, nth, tcp_hdr_size);
  95. }
  96. static void
  97. synproxy_send_server_syn(const struct synproxy_net *snet,
  98. const struct sk_buff *skb, const struct tcphdr *th,
  99. const struct synproxy_options *opts, u32 recv_seq)
  100. {
  101. struct sk_buff *nskb;
  102. struct iphdr *iph, *niph;
  103. struct tcphdr *nth;
  104. unsigned int tcp_hdr_size;
  105. iph = ip_hdr(skb);
  106. tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
  107. nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
  108. GFP_ATOMIC);
  109. if (nskb == NULL)
  110. return;
  111. skb_reserve(nskb, MAX_TCP_HEADER);
  112. niph = synproxy_build_ip(nskb, iph->saddr, iph->daddr);
  113. skb_reset_transport_header(nskb);
  114. nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
  115. nth->source = th->source;
  116. nth->dest = th->dest;
  117. nth->seq = htonl(recv_seq - 1);
  118. /* ack_seq is used to relay our ISN to the synproxy hook to initialize
  119. * sequence number translation once a connection tracking entry exists.
  120. */
  121. nth->ack_seq = htonl(ntohl(th->ack_seq) - 1);
  122. tcp_flag_word(nth) = TCP_FLAG_SYN;
  123. if (opts->options & XT_SYNPROXY_OPT_ECN)
  124. tcp_flag_word(nth) |= TCP_FLAG_ECE | TCP_FLAG_CWR;
  125. nth->doff = tcp_hdr_size / 4;
  126. nth->window = th->window;
  127. nth->check = 0;
  128. nth->urg_ptr = 0;
  129. synproxy_build_options(nth, opts);
  130. synproxy_send_tcp(snet, skb, nskb, &snet->tmpl->ct_general, IP_CT_NEW,
  131. niph, nth, tcp_hdr_size);
  132. }
  133. static void
  134. synproxy_send_server_ack(const struct synproxy_net *snet,
  135. const struct ip_ct_tcp *state,
  136. const struct sk_buff *skb, const struct tcphdr *th,
  137. const struct synproxy_options *opts)
  138. {
  139. struct sk_buff *nskb;
  140. struct iphdr *iph, *niph;
  141. struct tcphdr *nth;
  142. unsigned int tcp_hdr_size;
  143. iph = ip_hdr(skb);
  144. tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
  145. nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
  146. GFP_ATOMIC);
  147. if (nskb == NULL)
  148. return;
  149. skb_reserve(nskb, MAX_TCP_HEADER);
  150. niph = synproxy_build_ip(nskb, iph->daddr, iph->saddr);
  151. skb_reset_transport_header(nskb);
  152. nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
  153. nth->source = th->dest;
  154. nth->dest = th->source;
  155. nth->seq = htonl(ntohl(th->ack_seq));
  156. nth->ack_seq = htonl(ntohl(th->seq) + 1);
  157. tcp_flag_word(nth) = TCP_FLAG_ACK;
  158. nth->doff = tcp_hdr_size / 4;
  159. nth->window = htons(state->seen[IP_CT_DIR_ORIGINAL].td_maxwin);
  160. nth->check = 0;
  161. nth->urg_ptr = 0;
  162. synproxy_build_options(nth, opts);
  163. synproxy_send_tcp(snet, skb, nskb, NULL, 0, niph, nth, tcp_hdr_size);
  164. }
  165. static void
  166. synproxy_send_client_ack(const struct synproxy_net *snet,
  167. const struct sk_buff *skb, const struct tcphdr *th,
  168. const struct synproxy_options *opts)
  169. {
  170. struct sk_buff *nskb;
  171. struct iphdr *iph, *niph;
  172. struct tcphdr *nth;
  173. unsigned int tcp_hdr_size;
  174. iph = ip_hdr(skb);
  175. tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
  176. nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
  177. GFP_ATOMIC);
  178. if (nskb == NULL)
  179. return;
  180. skb_reserve(nskb, MAX_TCP_HEADER);
  181. niph = synproxy_build_ip(nskb, iph->saddr, iph->daddr);
  182. skb_reset_transport_header(nskb);
  183. nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
  184. nth->source = th->source;
  185. nth->dest = th->dest;
  186. nth->seq = htonl(ntohl(th->seq) + 1);
  187. nth->ack_seq = th->ack_seq;
  188. tcp_flag_word(nth) = TCP_FLAG_ACK;
  189. nth->doff = tcp_hdr_size / 4;
  190. nth->window = htons(ntohs(th->window) >> opts->wscale);
  191. nth->check = 0;
  192. nth->urg_ptr = 0;
  193. synproxy_build_options(nth, opts);
  194. synproxy_send_tcp(snet, skb, nskb, skb->nfct, IP_CT_ESTABLISHED_REPLY,
  195. niph, nth, tcp_hdr_size);
  196. }
  197. static bool
  198. synproxy_recv_client_ack(const struct synproxy_net *snet,
  199. const struct sk_buff *skb, const struct tcphdr *th,
  200. struct synproxy_options *opts, u32 recv_seq)
  201. {
  202. int mss;
  203. mss = __cookie_v4_check(ip_hdr(skb), th, ntohl(th->ack_seq) - 1);
  204. if (mss == 0) {
  205. this_cpu_inc(snet->stats->cookie_invalid);
  206. return false;
  207. }
  208. this_cpu_inc(snet->stats->cookie_valid);
  209. opts->mss = mss;
  210. opts->options |= XT_SYNPROXY_OPT_MSS;
  211. if (opts->options & XT_SYNPROXY_OPT_TIMESTAMP)
  212. synproxy_check_timestamp_cookie(opts);
  213. synproxy_send_server_syn(snet, skb, th, opts, recv_seq);
  214. return true;
  215. }
  216. static unsigned int
  217. synproxy_tg4(struct sk_buff *skb, const struct xt_action_param *par)
  218. {
  219. const struct xt_synproxy_info *info = par->targinfo;
  220. struct synproxy_net *snet = synproxy_pernet(par->net);
  221. struct synproxy_options opts = {};
  222. struct tcphdr *th, _th;
  223. if (nf_ip_checksum(skb, par->hooknum, par->thoff, IPPROTO_TCP))
  224. return NF_DROP;
  225. th = skb_header_pointer(skb, par->thoff, sizeof(_th), &_th);
  226. if (th == NULL)
  227. return NF_DROP;
  228. if (!synproxy_parse_options(skb, par->thoff, th, &opts))
  229. return NF_DROP;
  230. if (th->syn && !(th->ack || th->fin || th->rst)) {
  231. /* Initial SYN from client */
  232. this_cpu_inc(snet->stats->syn_received);
  233. if (th->ece && th->cwr)
  234. opts.options |= XT_SYNPROXY_OPT_ECN;
  235. opts.options &= info->options;
  236. if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
  237. synproxy_init_timestamp_cookie(info, &opts);
  238. else
  239. opts.options &= ~(XT_SYNPROXY_OPT_WSCALE |
  240. XT_SYNPROXY_OPT_SACK_PERM |
  241. XT_SYNPROXY_OPT_ECN);
  242. synproxy_send_client_synack(snet, skb, th, &opts);
  243. return NF_DROP;
  244. } else if (th->ack && !(th->fin || th->rst || th->syn)) {
  245. /* ACK from client */
  246. synproxy_recv_client_ack(snet, skb, th, &opts, ntohl(th->seq));
  247. return NF_DROP;
  248. }
  249. return XT_CONTINUE;
  250. }
  251. static unsigned int ipv4_synproxy_hook(void *priv,
  252. struct sk_buff *skb,
  253. const struct nf_hook_state *nhs)
  254. {
  255. struct synproxy_net *snet = synproxy_pernet(nhs->net);
  256. enum ip_conntrack_info ctinfo;
  257. struct nf_conn *ct;
  258. struct nf_conn_synproxy *synproxy;
  259. struct synproxy_options opts = {};
  260. const struct ip_ct_tcp *state;
  261. struct tcphdr *th, _th;
  262. unsigned int thoff;
  263. ct = nf_ct_get(skb, &ctinfo);
  264. if (ct == NULL)
  265. return NF_ACCEPT;
  266. synproxy = nfct_synproxy(ct);
  267. if (synproxy == NULL)
  268. return NF_ACCEPT;
  269. if (nf_is_loopback_packet(skb))
  270. return NF_ACCEPT;
  271. thoff = ip_hdrlen(skb);
  272. th = skb_header_pointer(skb, thoff, sizeof(_th), &_th);
  273. if (th == NULL)
  274. return NF_DROP;
  275. state = &ct->proto.tcp;
  276. switch (state->state) {
  277. case TCP_CONNTRACK_CLOSE:
  278. if (th->rst && !test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
  279. nf_ct_seqadj_init(ct, ctinfo, synproxy->isn -
  280. ntohl(th->seq) + 1);
  281. break;
  282. }
  283. if (!th->syn || th->ack ||
  284. CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
  285. break;
  286. /* Reopened connection - reset the sequence number and timestamp
  287. * adjustments, they will get initialized once the connection is
  288. * reestablished.
  289. */
  290. nf_ct_seqadj_init(ct, ctinfo, 0);
  291. synproxy->tsoff = 0;
  292. this_cpu_inc(snet->stats->conn_reopened);
  293. /* fall through */
  294. case TCP_CONNTRACK_SYN_SENT:
  295. if (!synproxy_parse_options(skb, thoff, th, &opts))
  296. return NF_DROP;
  297. if (!th->syn && th->ack &&
  298. CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
  299. /* Keep-Alives are sent with SEG.SEQ = SND.NXT-1,
  300. * therefore we need to add 1 to make the SYN sequence
  301. * number match the one of first SYN.
  302. */
  303. if (synproxy_recv_client_ack(snet, skb, th, &opts,
  304. ntohl(th->seq) + 1))
  305. this_cpu_inc(snet->stats->cookie_retrans);
  306. return NF_DROP;
  307. }
  308. synproxy->isn = ntohl(th->ack_seq);
  309. if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
  310. synproxy->its = opts.tsecr;
  311. break;
  312. case TCP_CONNTRACK_SYN_RECV:
  313. if (!th->syn || !th->ack)
  314. break;
  315. if (!synproxy_parse_options(skb, thoff, th, &opts))
  316. return NF_DROP;
  317. if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
  318. synproxy->tsoff = opts.tsval - synproxy->its;
  319. opts.options &= ~(XT_SYNPROXY_OPT_MSS |
  320. XT_SYNPROXY_OPT_WSCALE |
  321. XT_SYNPROXY_OPT_SACK_PERM);
  322. swap(opts.tsval, opts.tsecr);
  323. synproxy_send_server_ack(snet, state, skb, th, &opts);
  324. nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
  325. swap(opts.tsval, opts.tsecr);
  326. synproxy_send_client_ack(snet, skb, th, &opts);
  327. consume_skb(skb);
  328. return NF_STOLEN;
  329. default:
  330. break;
  331. }
  332. synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy);
  333. return NF_ACCEPT;
  334. }
  335. static int synproxy_tg4_check(const struct xt_tgchk_param *par)
  336. {
  337. const struct ipt_entry *e = par->entryinfo;
  338. if (e->ip.proto != IPPROTO_TCP ||
  339. e->ip.invflags & XT_INV_PROTO)
  340. return -EINVAL;
  341. return nf_ct_l3proto_try_module_get(par->family);
  342. }
  343. static void synproxy_tg4_destroy(const struct xt_tgdtor_param *par)
  344. {
  345. nf_ct_l3proto_module_put(par->family);
  346. }
  347. static struct xt_target synproxy_tg4_reg __read_mostly = {
  348. .name = "SYNPROXY",
  349. .family = NFPROTO_IPV4,
  350. .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
  351. .target = synproxy_tg4,
  352. .targetsize = sizeof(struct xt_synproxy_info),
  353. .checkentry = synproxy_tg4_check,
  354. .destroy = synproxy_tg4_destroy,
  355. .me = THIS_MODULE,
  356. };
  357. static struct nf_hook_ops ipv4_synproxy_ops[] __read_mostly = {
  358. {
  359. .hook = ipv4_synproxy_hook,
  360. .pf = NFPROTO_IPV4,
  361. .hooknum = NF_INET_LOCAL_IN,
  362. .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
  363. },
  364. {
  365. .hook = ipv4_synproxy_hook,
  366. .pf = NFPROTO_IPV4,
  367. .hooknum = NF_INET_POST_ROUTING,
  368. .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
  369. },
  370. };
  371. static int __init synproxy_tg4_init(void)
  372. {
  373. int err;
  374. err = nf_register_hooks(ipv4_synproxy_ops,
  375. ARRAY_SIZE(ipv4_synproxy_ops));
  376. if (err < 0)
  377. goto err1;
  378. err = xt_register_target(&synproxy_tg4_reg);
  379. if (err < 0)
  380. goto err2;
  381. return 0;
  382. err2:
  383. nf_unregister_hooks(ipv4_synproxy_ops, ARRAY_SIZE(ipv4_synproxy_ops));
  384. err1:
  385. return err;
  386. }
  387. static void __exit synproxy_tg4_exit(void)
  388. {
  389. xt_unregister_target(&synproxy_tg4_reg);
  390. nf_unregister_hooks(ipv4_synproxy_ops, ARRAY_SIZE(ipv4_synproxy_ops));
  391. }
  392. module_init(synproxy_tg4_init);
  393. module_exit(synproxy_tg4_exit);
  394. MODULE_LICENSE("GPL");
  395. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");