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 sk_buff
*skb
, __be32 saddr
, __be32 daddr
)
24 struct net
*net
= sock_net(skb
->sk
);
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(const struct synproxy_net
*snet
,
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 struct net
*net
= nf_ct_net(snet
->tmpl
);
51 nth
->check
= ~tcp_v4_check(tcp_hdr_size
, niph
->saddr
, niph
->daddr
, 0);
52 nskb
->ip_summed
= CHECKSUM_PARTIAL
;
53 nskb
->csum_start
= (unsigned char *)nth
- nskb
->head
;
54 nskb
->csum_offset
= offsetof(struct tcphdr
, check
);
56 skb_dst_set_noref(nskb
, skb_dst(skb
));
57 nskb
->protocol
= htons(ETH_P_IP
);
58 if (ip_route_me_harder(net
, nskb
, RTN_UNSPEC
))
63 nskb
->nfctinfo
= ctinfo
;
64 nf_conntrack_get(nfct
);
67 ip_local_out(net
, nskb
->sk
, nskb
);
75 synproxy_send_client_synack(const struct synproxy_net
*snet
,
76 const struct sk_buff
*skb
, const struct tcphdr
*th
,
77 const struct synproxy_options
*opts
)
80 struct iphdr
*iph
, *niph
;
82 unsigned int tcp_hdr_size
;
87 tcp_hdr_size
= sizeof(*nth
) + synproxy_options_size(opts
);
88 nskb
= alloc_skb(sizeof(*niph
) + tcp_hdr_size
+ MAX_TCP_HEADER
,
92 skb_reserve(nskb
, MAX_TCP_HEADER
);
94 niph
= synproxy_build_ip(nskb
, iph
->daddr
, iph
->saddr
);
96 skb_reset_transport_header(nskb
);
97 nth
= (struct tcphdr
*)skb_put(nskb
, tcp_hdr_size
);
98 nth
->source
= th
->dest
;
99 nth
->dest
= th
->source
;
100 nth
->seq
= htonl(__cookie_v4_init_sequence(iph
, th
, &mss
));
101 nth
->ack_seq
= htonl(ntohl(th
->seq
) + 1);
102 tcp_flag_word(nth
) = TCP_FLAG_SYN
| TCP_FLAG_ACK
;
103 if (opts
->options
& XT_SYNPROXY_OPT_ECN
)
104 tcp_flag_word(nth
) |= TCP_FLAG_ECE
;
105 nth
->doff
= tcp_hdr_size
/ 4;
110 synproxy_build_options(nth
, opts
);
112 synproxy_send_tcp(snet
, skb
, nskb
, skb
->nfct
, IP_CT_ESTABLISHED_REPLY
,
113 niph
, nth
, tcp_hdr_size
);
117 synproxy_send_server_syn(const struct synproxy_net
*snet
,
118 const struct sk_buff
*skb
, const struct tcphdr
*th
,
119 const struct synproxy_options
*opts
, u32 recv_seq
)
121 struct sk_buff
*nskb
;
122 struct iphdr
*iph
, *niph
;
124 unsigned int tcp_hdr_size
;
128 tcp_hdr_size
= sizeof(*nth
) + synproxy_options_size(opts
);
129 nskb
= alloc_skb(sizeof(*niph
) + tcp_hdr_size
+ MAX_TCP_HEADER
,
133 skb_reserve(nskb
, MAX_TCP_HEADER
);
135 niph
= synproxy_build_ip(nskb
, iph
->saddr
, iph
->daddr
);
137 skb_reset_transport_header(nskb
);
138 nth
= (struct tcphdr
*)skb_put(nskb
, tcp_hdr_size
);
139 nth
->source
= th
->source
;
140 nth
->dest
= th
->dest
;
141 nth
->seq
= htonl(recv_seq
- 1);
142 /* ack_seq is used to relay our ISN to the synproxy hook to initialize
143 * sequence number translation once a connection tracking entry exists.
145 nth
->ack_seq
= htonl(ntohl(th
->ack_seq
) - 1);
146 tcp_flag_word(nth
) = TCP_FLAG_SYN
;
147 if (opts
->options
& XT_SYNPROXY_OPT_ECN
)
148 tcp_flag_word(nth
) |= TCP_FLAG_ECE
| TCP_FLAG_CWR
;
149 nth
->doff
= tcp_hdr_size
/ 4;
150 nth
->window
= th
->window
;
154 synproxy_build_options(nth
, opts
);
156 synproxy_send_tcp(snet
, skb
, nskb
, &snet
->tmpl
->ct_general
, IP_CT_NEW
,
157 niph
, nth
, tcp_hdr_size
);
161 synproxy_send_server_ack(const struct synproxy_net
*snet
,
162 const struct ip_ct_tcp
*state
,
163 const struct sk_buff
*skb
, const struct tcphdr
*th
,
164 const struct synproxy_options
*opts
)
166 struct sk_buff
*nskb
;
167 struct iphdr
*iph
, *niph
;
169 unsigned int tcp_hdr_size
;
173 tcp_hdr_size
= sizeof(*nth
) + synproxy_options_size(opts
);
174 nskb
= alloc_skb(sizeof(*niph
) + tcp_hdr_size
+ MAX_TCP_HEADER
,
178 skb_reserve(nskb
, MAX_TCP_HEADER
);
180 niph
= synproxy_build_ip(nskb
, iph
->daddr
, iph
->saddr
);
182 skb_reset_transport_header(nskb
);
183 nth
= (struct tcphdr
*)skb_put(nskb
, tcp_hdr_size
);
184 nth
->source
= th
->dest
;
185 nth
->dest
= th
->source
;
186 nth
->seq
= htonl(ntohl(th
->ack_seq
));
187 nth
->ack_seq
= htonl(ntohl(th
->seq
) + 1);
188 tcp_flag_word(nth
) = TCP_FLAG_ACK
;
189 nth
->doff
= tcp_hdr_size
/ 4;
190 nth
->window
= htons(state
->seen
[IP_CT_DIR_ORIGINAL
].td_maxwin
);
194 synproxy_build_options(nth
, opts
);
196 synproxy_send_tcp(snet
, skb
, nskb
, NULL
, 0, niph
, nth
, tcp_hdr_size
);
200 synproxy_send_client_ack(const struct synproxy_net
*snet
,
201 const struct sk_buff
*skb
, const struct tcphdr
*th
,
202 const struct synproxy_options
*opts
)
204 struct sk_buff
*nskb
;
205 struct iphdr
*iph
, *niph
;
207 unsigned int tcp_hdr_size
;
211 tcp_hdr_size
= sizeof(*nth
) + synproxy_options_size(opts
);
212 nskb
= alloc_skb(sizeof(*niph
) + tcp_hdr_size
+ MAX_TCP_HEADER
,
216 skb_reserve(nskb
, MAX_TCP_HEADER
);
218 niph
= synproxy_build_ip(nskb
, iph
->saddr
, iph
->daddr
);
220 skb_reset_transport_header(nskb
);
221 nth
= (struct tcphdr
*)skb_put(nskb
, tcp_hdr_size
);
222 nth
->source
= th
->source
;
223 nth
->dest
= th
->dest
;
224 nth
->seq
= htonl(ntohl(th
->seq
) + 1);
225 nth
->ack_seq
= th
->ack_seq
;
226 tcp_flag_word(nth
) = TCP_FLAG_ACK
;
227 nth
->doff
= tcp_hdr_size
/ 4;
228 nth
->window
= htons(ntohs(th
->window
) >> opts
->wscale
);
232 synproxy_build_options(nth
, opts
);
234 synproxy_send_tcp(snet
, skb
, nskb
, skb
->nfct
, IP_CT_ESTABLISHED_REPLY
,
235 niph
, nth
, tcp_hdr_size
);
239 synproxy_recv_client_ack(const struct synproxy_net
*snet
,
240 const struct sk_buff
*skb
, const struct tcphdr
*th
,
241 struct synproxy_options
*opts
, u32 recv_seq
)
245 mss
= __cookie_v4_check(ip_hdr(skb
), th
, ntohl(th
->ack_seq
) - 1);
247 this_cpu_inc(snet
->stats
->cookie_invalid
);
251 this_cpu_inc(snet
->stats
->cookie_valid
);
253 opts
->options
|= XT_SYNPROXY_OPT_MSS
;
255 if (opts
->options
& XT_SYNPROXY_OPT_TIMESTAMP
)
256 synproxy_check_timestamp_cookie(opts
);
258 synproxy_send_server_syn(snet
, skb
, th
, opts
, recv_seq
);
263 synproxy_tg4(struct sk_buff
*skb
, const struct xt_action_param
*par
)
265 const struct xt_synproxy_info
*info
= par
->targinfo
;
266 struct synproxy_net
*snet
= synproxy_pernet(par
->net
);
267 struct synproxy_options opts
= {};
268 struct tcphdr
*th
, _th
;
270 if (nf_ip_checksum(skb
, par
->hooknum
, 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(snet
, skb
, th
, &opts
);
298 } else if (th
->ack
&& !(th
->fin
|| th
->rst
|| th
->syn
)) {
299 /* ACK from client */
300 synproxy_recv_client_ack(snet
, skb
, th
, &opts
, ntohl(th
->seq
));
307 static unsigned int ipv4_synproxy_hook(void *priv
,
309 const struct nf_hook_state
*nhs
)
311 struct synproxy_net
*snet
= synproxy_pernet(nhs
->net
);
312 enum ip_conntrack_info ctinfo
;
314 struct nf_conn_synproxy
*synproxy
;
315 struct synproxy_options opts
= {};
316 const struct ip_ct_tcp
*state
;
317 struct tcphdr
*th
, _th
;
320 ct
= nf_ct_get(skb
, &ctinfo
);
324 synproxy
= nfct_synproxy(ct
);
325 if (synproxy
== NULL
)
328 if (nf_is_loopback_packet(skb
))
331 thoff
= ip_hdrlen(skb
);
332 th
= skb_header_pointer(skb
, thoff
, sizeof(_th
), &_th
);
336 state
= &ct
->proto
.tcp
;
337 switch (state
->state
) {
338 case TCP_CONNTRACK_CLOSE
:
339 if (th
->rst
&& !test_bit(IPS_SEEN_REPLY_BIT
, &ct
->status
)) {
340 nf_ct_seqadj_init(ct
, ctinfo
, synproxy
->isn
-
345 if (!th
->syn
|| th
->ack
||
346 CTINFO2DIR(ctinfo
) != IP_CT_DIR_ORIGINAL
)
349 /* Reopened connection - reset the sequence number and timestamp
350 * adjustments, they will get initialized once the connection is
353 nf_ct_seqadj_init(ct
, ctinfo
, 0);
355 this_cpu_inc(snet
->stats
->conn_reopened
);
358 case TCP_CONNTRACK_SYN_SENT
:
359 if (!synproxy_parse_options(skb
, thoff
, th
, &opts
))
362 if (!th
->syn
&& th
->ack
&&
363 CTINFO2DIR(ctinfo
) == IP_CT_DIR_ORIGINAL
) {
364 /* Keep-Alives are sent with SEG.SEQ = SND.NXT-1,
365 * therefore we need to add 1 to make the SYN sequence
366 * number match the one of first SYN.
368 if (synproxy_recv_client_ack(snet
, skb
, th
, &opts
,
370 this_cpu_inc(snet
->stats
->cookie_retrans
);
375 synproxy
->isn
= ntohl(th
->ack_seq
);
376 if (opts
.options
& XT_SYNPROXY_OPT_TIMESTAMP
)
377 synproxy
->its
= opts
.tsecr
;
379 case TCP_CONNTRACK_SYN_RECV
:
380 if (!th
->syn
|| !th
->ack
)
383 if (!synproxy_parse_options(skb
, thoff
, th
, &opts
))
386 if (opts
.options
& XT_SYNPROXY_OPT_TIMESTAMP
)
387 synproxy
->tsoff
= opts
.tsval
- synproxy
->its
;
389 opts
.options
&= ~(XT_SYNPROXY_OPT_MSS
|
390 XT_SYNPROXY_OPT_WSCALE
|
391 XT_SYNPROXY_OPT_SACK_PERM
);
393 swap(opts
.tsval
, opts
.tsecr
);
394 synproxy_send_server_ack(snet
, state
, skb
, th
, &opts
);
396 nf_ct_seqadj_init(ct
, ctinfo
, synproxy
->isn
- ntohl(th
->seq
));
398 swap(opts
.tsval
, opts
.tsecr
);
399 synproxy_send_client_ack(snet
, skb
, th
, &opts
);
407 synproxy_tstamp_adjust(skb
, thoff
, th
, ct
, ctinfo
, synproxy
);
411 static int synproxy_tg4_check(const struct xt_tgchk_param
*par
)
413 const struct ipt_entry
*e
= par
->entryinfo
;
415 if (e
->ip
.proto
!= IPPROTO_TCP
||
416 e
->ip
.invflags
& XT_INV_PROTO
)
419 return nf_ct_l3proto_try_module_get(par
->family
);
422 static void synproxy_tg4_destroy(const struct xt_tgdtor_param
*par
)
424 nf_ct_l3proto_module_put(par
->family
);
427 static struct xt_target synproxy_tg4_reg __read_mostly
= {
429 .family
= NFPROTO_IPV4
,
430 .hooks
= (1 << NF_INET_LOCAL_IN
) | (1 << NF_INET_FORWARD
),
431 .target
= synproxy_tg4
,
432 .targetsize
= sizeof(struct xt_synproxy_info
),
433 .checkentry
= synproxy_tg4_check
,
434 .destroy
= synproxy_tg4_destroy
,
438 static struct nf_hook_ops ipv4_synproxy_ops
[] __read_mostly
= {
440 .hook
= ipv4_synproxy_hook
,
442 .hooknum
= NF_INET_LOCAL_IN
,
443 .priority
= NF_IP_PRI_CONNTRACK_CONFIRM
- 1,
446 .hook
= ipv4_synproxy_hook
,
448 .hooknum
= NF_INET_POST_ROUTING
,
449 .priority
= NF_IP_PRI_CONNTRACK_CONFIRM
- 1,
453 static int __init
synproxy_tg4_init(void)
457 err
= nf_register_hooks(ipv4_synproxy_ops
,
458 ARRAY_SIZE(ipv4_synproxy_ops
));
462 err
= xt_register_target(&synproxy_tg4_reg
);
469 nf_unregister_hooks(ipv4_synproxy_ops
, ARRAY_SIZE(ipv4_synproxy_ops
));
474 static void __exit
synproxy_tg4_exit(void)
476 xt_unregister_target(&synproxy_tg4_reg
);
477 nf_unregister_hooks(ipv4_synproxy_ops
, ARRAY_SIZE(ipv4_synproxy_ops
));
480 module_init(synproxy_tg4_init
);
481 module_exit(synproxy_tg4_exit
);
483 MODULE_LICENSE("GPL");
484 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");