2 * Copyright (c) 2013 Patrick McHardy <kaber@trash.net>
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.
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
13 #include <linux/netfilter_ipv4/ip_tables.h>
14 #include <linux/netfilter/x_tables.h>
15 #include <linux/netfilter/xt_SYNPROXY.h>
16 #include <net/netfilter/nf_conntrack.h>
17 #include <net/netfilter/nf_conntrack_seqadj.h>
18 #include <net/netfilter/nf_conntrack_synproxy.h>
21 synproxy_build_ip(struct net *net, struct sk_buff *skb, __be32 saddr,
26 skb_reset_network_header(skb);
27 iph = (struct iphdr *)skb_put(skb, sizeof(*iph));
29 iph->ihl = sizeof(*iph) / 4;
32 iph->frag_off = htons(IP_DF);
33 iph->ttl = net->ipv4.sysctl_ip_default_ttl;
34 iph->protocol = IPPROTO_TCP;
43 synproxy_send_tcp(struct net *net,
44 const struct sk_buff *skb, struct sk_buff *nskb,
45 struct nf_conntrack *nfct, enum ip_conntrack_info ctinfo,
46 struct iphdr *niph, struct tcphdr *nth,
47 unsigned int tcp_hdr_size)
49 nth->check = ~tcp_v4_check(tcp_hdr_size, niph->saddr, niph->daddr, 0);
50 nskb->ip_summed = CHECKSUM_PARTIAL;
51 nskb->csum_start = (unsigned char *)nth - nskb->head;
52 nskb->csum_offset = offsetof(struct tcphdr, check);
54 skb_dst_set_noref(nskb, skb_dst(skb));
55 nskb->protocol = htons(ETH_P_IP);
56 if (ip_route_me_harder(net, nskb, RTN_UNSPEC))
60 nf_ct_set(nskb, (struct nf_conn *)nfct, ctinfo);
61 nf_conntrack_get(nfct);
64 ip_local_out(net, nskb->sk, nskb);
72 synproxy_send_client_synack(struct net *net,
73 const struct sk_buff *skb, const struct tcphdr *th,
74 const struct synproxy_options *opts)
77 struct iphdr *iph, *niph;
79 unsigned int tcp_hdr_size;
84 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
85 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
89 skb_reserve(nskb, MAX_TCP_HEADER);
91 niph = synproxy_build_ip(net, nskb, iph->daddr, iph->saddr);
93 skb_reset_transport_header(nskb);
94 nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
95 nth->source = th->dest;
96 nth->dest = th->source;
97 nth->seq = htonl(__cookie_v4_init_sequence(iph, th, &mss));
98 nth->ack_seq = htonl(ntohl(th->seq) + 1);
99 tcp_flag_word(nth) = TCP_FLAG_SYN | TCP_FLAG_ACK;
100 if (opts->options & XT_SYNPROXY_OPT_ECN)
101 tcp_flag_word(nth) |= TCP_FLAG_ECE;
102 nth->doff = tcp_hdr_size / 4;
107 synproxy_build_options(nth, opts);
109 synproxy_send_tcp(net, skb, nskb, skb_nfct(skb),
110 IP_CT_ESTABLISHED_REPLY, niph, nth, tcp_hdr_size);
114 synproxy_send_server_syn(struct net *net,
115 const struct sk_buff *skb, const struct tcphdr *th,
116 const struct synproxy_options *opts, u32 recv_seq)
118 struct synproxy_net *snet = synproxy_pernet(net);
119 struct sk_buff *nskb;
120 struct iphdr *iph, *niph;
122 unsigned int tcp_hdr_size;
126 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
127 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
131 skb_reserve(nskb, MAX_TCP_HEADER);
133 niph = synproxy_build_ip(net, nskb, iph->saddr, iph->daddr);
135 skb_reset_transport_header(nskb);
136 nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
137 nth->source = th->source;
138 nth->dest = th->dest;
139 nth->seq = htonl(recv_seq - 1);
140 /* ack_seq is used to relay our ISN to the synproxy hook to initialize
141 * sequence number translation once a connection tracking entry exists.
143 nth->ack_seq = htonl(ntohl(th->ack_seq) - 1);
144 tcp_flag_word(nth) = TCP_FLAG_SYN;
145 if (opts->options & XT_SYNPROXY_OPT_ECN)
146 tcp_flag_word(nth) |= TCP_FLAG_ECE | TCP_FLAG_CWR;
147 nth->doff = tcp_hdr_size / 4;
148 nth->window = th->window;
152 synproxy_build_options(nth, opts);
154 synproxy_send_tcp(net, skb, nskb, &snet->tmpl->ct_general, IP_CT_NEW,
155 niph, nth, tcp_hdr_size);
159 synproxy_send_server_ack(struct net *net,
160 const struct ip_ct_tcp *state,
161 const struct sk_buff *skb, const struct tcphdr *th,
162 const struct synproxy_options *opts)
164 struct sk_buff *nskb;
165 struct iphdr *iph, *niph;
167 unsigned int tcp_hdr_size;
171 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
172 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
176 skb_reserve(nskb, MAX_TCP_HEADER);
178 niph = synproxy_build_ip(net, nskb, iph->daddr, iph->saddr);
180 skb_reset_transport_header(nskb);
181 nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
182 nth->source = th->dest;
183 nth->dest = th->source;
184 nth->seq = htonl(ntohl(th->ack_seq));
185 nth->ack_seq = htonl(ntohl(th->seq) + 1);
186 tcp_flag_word(nth) = TCP_FLAG_ACK;
187 nth->doff = tcp_hdr_size / 4;
188 nth->window = htons(state->seen[IP_CT_DIR_ORIGINAL].td_maxwin);
192 synproxy_build_options(nth, opts);
194 synproxy_send_tcp(net, skb, nskb, NULL, 0, niph, nth, tcp_hdr_size);
198 synproxy_send_client_ack(struct net *net,
199 const struct sk_buff *skb, const struct tcphdr *th,
200 const struct synproxy_options *opts)
202 struct sk_buff *nskb;
203 struct iphdr *iph, *niph;
205 unsigned int tcp_hdr_size;
209 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
210 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
214 skb_reserve(nskb, MAX_TCP_HEADER);
216 niph = synproxy_build_ip(net, nskb, iph->saddr, iph->daddr);
218 skb_reset_transport_header(nskb);
219 nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
220 nth->source = th->source;
221 nth->dest = th->dest;
222 nth->seq = htonl(ntohl(th->seq) + 1);
223 nth->ack_seq = th->ack_seq;
224 tcp_flag_word(nth) = TCP_FLAG_ACK;
225 nth->doff = tcp_hdr_size / 4;
226 nth->window = htons(ntohs(th->window) >> opts->wscale);
230 synproxy_build_options(nth, opts);
232 synproxy_send_tcp(net, skb, nskb, skb_nfct(skb),
233 IP_CT_ESTABLISHED_REPLY, niph, nth, tcp_hdr_size);
237 synproxy_recv_client_ack(struct net *net,
238 const struct sk_buff *skb, const struct tcphdr *th,
239 struct synproxy_options *opts, u32 recv_seq)
241 struct synproxy_net *snet = synproxy_pernet(net);
244 mss = __cookie_v4_check(ip_hdr(skb), th, ntohl(th->ack_seq) - 1);
246 this_cpu_inc(snet->stats->cookie_invalid);
250 this_cpu_inc(snet->stats->cookie_valid);
252 opts->options |= XT_SYNPROXY_OPT_MSS;
254 if (opts->options & XT_SYNPROXY_OPT_TIMESTAMP)
255 synproxy_check_timestamp_cookie(opts);
257 synproxy_send_server_syn(net, skb, th, opts, recv_seq);
262 synproxy_tg4(struct sk_buff *skb, const struct xt_action_param *par)
264 const struct xt_synproxy_info *info = par->targinfo;
265 struct net *net = xt_net(par);
266 struct synproxy_net *snet = synproxy_pernet(net);
267 struct synproxy_options opts = {};
268 struct tcphdr *th, _th;
270 if (nf_ip_checksum(skb, xt_hooknum(par), par->thoff, IPPROTO_TCP))
273 th = skb_header_pointer(skb, par->thoff, sizeof(_th), &_th);
277 if (!synproxy_parse_options(skb, par->thoff, th, &opts))
280 if (th->syn && !(th->ack || th->fin || th->rst)) {
281 /* Initial SYN from client */
282 this_cpu_inc(snet->stats->syn_received);
284 if (th->ece && th->cwr)
285 opts.options |= XT_SYNPROXY_OPT_ECN;
287 opts.options &= info->options;
288 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
289 synproxy_init_timestamp_cookie(info, &opts);
291 opts.options &= ~(XT_SYNPROXY_OPT_WSCALE |
292 XT_SYNPROXY_OPT_SACK_PERM |
293 XT_SYNPROXY_OPT_ECN);
295 synproxy_send_client_synack(net, skb, th, &opts);
298 } else if (th->ack && !(th->fin || th->rst || th->syn)) {
299 /* ACK from client */
300 synproxy_recv_client_ack(net, skb, th, &opts, ntohl(th->seq));
307 static unsigned int ipv4_synproxy_hook(void *priv,
309 const struct nf_hook_state *nhs)
311 struct net *net = nhs->net;
312 struct synproxy_net *snet = synproxy_pernet(net);
313 enum ip_conntrack_info ctinfo;
315 struct nf_conn_synproxy *synproxy;
316 struct synproxy_options opts = {};
317 const struct ip_ct_tcp *state;
318 struct tcphdr *th, _th;
321 ct = nf_ct_get(skb, &ctinfo);
325 synproxy = nfct_synproxy(ct);
326 if (synproxy == NULL)
329 if (nf_is_loopback_packet(skb))
332 thoff = ip_hdrlen(skb);
333 th = skb_header_pointer(skb, thoff, sizeof(_th), &_th);
337 state = &ct->proto.tcp;
338 switch (state->state) {
339 case TCP_CONNTRACK_CLOSE:
340 if (th->rst && !test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
341 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn -
346 if (!th->syn || th->ack ||
347 CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
350 /* Reopened connection - reset the sequence number and timestamp
351 * adjustments, they will get initialized once the connection is
354 nf_ct_seqadj_init(ct, ctinfo, 0);
356 this_cpu_inc(snet->stats->conn_reopened);
359 case TCP_CONNTRACK_SYN_SENT:
360 if (!synproxy_parse_options(skb, thoff, th, &opts))
363 if (!th->syn && th->ack &&
364 CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
365 /* Keep-Alives are sent with SEG.SEQ = SND.NXT-1,
366 * therefore we need to add 1 to make the SYN sequence
367 * number match the one of first SYN.
369 if (synproxy_recv_client_ack(net, skb, th, &opts,
371 this_cpu_inc(snet->stats->cookie_retrans);
376 synproxy->isn = ntohl(th->ack_seq);
377 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
378 synproxy->its = opts.tsecr;
380 case TCP_CONNTRACK_SYN_RECV:
381 if (!th->syn || !th->ack)
384 if (!synproxy_parse_options(skb, thoff, th, &opts))
387 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
388 synproxy->tsoff = opts.tsval - synproxy->its;
390 opts.options &= ~(XT_SYNPROXY_OPT_MSS |
391 XT_SYNPROXY_OPT_WSCALE |
392 XT_SYNPROXY_OPT_SACK_PERM);
394 swap(opts.tsval, opts.tsecr);
395 synproxy_send_server_ack(net, state, skb, th, &opts);
397 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
399 swap(opts.tsval, opts.tsecr);
400 synproxy_send_client_ack(net, skb, th, &opts);
408 synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy);
412 static int synproxy_tg4_check(const struct xt_tgchk_param *par)
414 const struct ipt_entry *e = par->entryinfo;
416 if (e->ip.proto != IPPROTO_TCP ||
417 e->ip.invflags & XT_INV_PROTO)
420 return nf_ct_netns_get(par->net, par->family);
423 static void synproxy_tg4_destroy(const struct xt_tgdtor_param *par)
425 nf_ct_netns_put(par->net, par->family);
428 static struct xt_target synproxy_tg4_reg __read_mostly = {
430 .family = NFPROTO_IPV4,
431 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
432 .target = synproxy_tg4,
433 .targetsize = sizeof(struct xt_synproxy_info),
434 .checkentry = synproxy_tg4_check,
435 .destroy = synproxy_tg4_destroy,
439 static struct nf_hook_ops ipv4_synproxy_ops[] __read_mostly = {
441 .hook = ipv4_synproxy_hook,
443 .hooknum = NF_INET_LOCAL_IN,
444 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
447 .hook = ipv4_synproxy_hook,
449 .hooknum = NF_INET_POST_ROUTING,
450 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
454 static int __init synproxy_tg4_init(void)
458 err = nf_register_hooks(ipv4_synproxy_ops,
459 ARRAY_SIZE(ipv4_synproxy_ops));
463 err = xt_register_target(&synproxy_tg4_reg);
470 nf_unregister_hooks(ipv4_synproxy_ops, ARRAY_SIZE(ipv4_synproxy_ops));
475 static void __exit synproxy_tg4_exit(void)
477 xt_unregister_target(&synproxy_tg4_reg);
478 nf_unregister_hooks(ipv4_synproxy_ops, ARRAY_SIZE(ipv4_synproxy_ops));
481 module_init(synproxy_tg4_init);
482 module_exit(synproxy_tg4_exit);
484 MODULE_LICENSE("GPL");
485 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");