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 if (synproxy_recv_client_ack(net, skb, th, &opts, ntohl(th->seq))) {
311 static unsigned int ipv4_synproxy_hook(void *priv,
313 const struct nf_hook_state *nhs)
315 struct net *net = nhs->net;
316 struct synproxy_net *snet = synproxy_pernet(net);
317 enum ip_conntrack_info ctinfo;
319 struct nf_conn_synproxy *synproxy;
320 struct synproxy_options opts = {};
321 const struct ip_ct_tcp *state;
322 struct tcphdr *th, _th;
325 ct = nf_ct_get(skb, &ctinfo);
329 synproxy = nfct_synproxy(ct);
330 if (synproxy == NULL)
333 if (nf_is_loopback_packet(skb))
336 thoff = ip_hdrlen(skb);
337 th = skb_header_pointer(skb, thoff, sizeof(_th), &_th);
341 state = &ct->proto.tcp;
342 switch (state->state) {
343 case TCP_CONNTRACK_CLOSE:
344 if (th->rst && !test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
345 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn -
350 if (!th->syn || th->ack ||
351 CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
354 /* Reopened connection - reset the sequence number and timestamp
355 * adjustments, they will get initialized once the connection is
358 nf_ct_seqadj_init(ct, ctinfo, 0);
360 this_cpu_inc(snet->stats->conn_reopened);
363 case TCP_CONNTRACK_SYN_SENT:
364 if (!synproxy_parse_options(skb, thoff, th, &opts))
367 if (!th->syn && th->ack &&
368 CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
369 /* Keep-Alives are sent with SEG.SEQ = SND.NXT-1,
370 * therefore we need to add 1 to make the SYN sequence
371 * number match the one of first SYN.
373 if (synproxy_recv_client_ack(net, skb, th, &opts,
374 ntohl(th->seq) + 1)) {
375 this_cpu_inc(snet->stats->cookie_retrans);
383 synproxy->isn = ntohl(th->ack_seq);
384 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
385 synproxy->its = opts.tsecr;
387 case TCP_CONNTRACK_SYN_RECV:
388 if (!th->syn || !th->ack)
391 if (!synproxy_parse_options(skb, thoff, th, &opts))
394 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
395 synproxy->tsoff = opts.tsval - synproxy->its;
397 opts.options &= ~(XT_SYNPROXY_OPT_MSS |
398 XT_SYNPROXY_OPT_WSCALE |
399 XT_SYNPROXY_OPT_SACK_PERM);
401 swap(opts.tsval, opts.tsecr);
402 synproxy_send_server_ack(net, state, skb, th, &opts);
404 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
406 swap(opts.tsval, opts.tsecr);
407 synproxy_send_client_ack(net, skb, th, &opts);
415 synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy);
419 static struct nf_hook_ops ipv4_synproxy_ops[] __read_mostly = {
421 .hook = ipv4_synproxy_hook,
423 .hooknum = NF_INET_LOCAL_IN,
424 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
427 .hook = ipv4_synproxy_hook,
429 .hooknum = NF_INET_POST_ROUTING,
430 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
434 static int synproxy_tg4_check(const struct xt_tgchk_param *par)
436 struct synproxy_net *snet = synproxy_pernet(par->net);
437 const struct ipt_entry *e = par->entryinfo;
440 if (e->ip.proto != IPPROTO_TCP ||
441 e->ip.invflags & XT_INV_PROTO)
444 err = nf_ct_netns_get(par->net, par->family);
448 if (snet->hook_ref4 == 0) {
449 err = nf_register_net_hooks(par->net, ipv4_synproxy_ops,
450 ARRAY_SIZE(ipv4_synproxy_ops));
452 nf_ct_netns_put(par->net, par->family);
461 static void synproxy_tg4_destroy(const struct xt_tgdtor_param *par)
463 struct synproxy_net *snet = synproxy_pernet(par->net);
466 if (snet->hook_ref4 == 0)
467 nf_unregister_net_hooks(par->net, ipv4_synproxy_ops,
468 ARRAY_SIZE(ipv4_synproxy_ops));
469 nf_ct_netns_put(par->net, par->family);
472 static struct xt_target synproxy_tg4_reg __read_mostly = {
474 .family = NFPROTO_IPV4,
475 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
476 .target = synproxy_tg4,
477 .targetsize = sizeof(struct xt_synproxy_info),
478 .checkentry = synproxy_tg4_check,
479 .destroy = synproxy_tg4_destroy,
483 static int __init synproxy_tg4_init(void)
485 return xt_register_target(&synproxy_tg4_reg);
488 static void __exit synproxy_tg4_exit(void)
490 xt_unregister_target(&synproxy_tg4_reg);
493 module_init(synproxy_tg4_init);
494 module_exit(synproxy_tg4_exit);
496 MODULE_LICENSE("GPL");
497 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");