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 
9 #include <linux/module.h>
10 #include <linux/skbuff.h>
11 #include <net/tcp.h>
12 
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>
19 
20 static struct iphdr *
21 synproxy_build_ip(struct net *net, struct sk_buff *skb, __be32 saddr,
22 		  __be32 daddr)
23 {
24 	struct iphdr *iph;
25 
26 	skb_reset_network_header(skb);
27 	iph = skb_put(skb, sizeof(*iph));
28 	iph->version	= 4;
29 	iph->ihl	= sizeof(*iph) / 4;
30 	iph->tos	= 0;
31 	iph->id		= 0;
32 	iph->frag_off	= htons(IP_DF);
33 	iph->ttl	= net->ipv4.sysctl_ip_default_ttl;
34 	iph->protocol	= IPPROTO_TCP;
35 	iph->check	= 0;
36 	iph->saddr	= saddr;
37 	iph->daddr	= daddr;
38 
39 	return iph;
40 }
41 
42 static void
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)
48 {
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);
53 
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))
57 		goto free_nskb;
58 
59 	if (nfct) {
60 		nf_ct_set(nskb, (struct nf_conn *)nfct, ctinfo);
61 		nf_conntrack_get(nfct);
62 	}
63 
64 	ip_local_out(net, nskb->sk, nskb);
65 	return;
66 
67 free_nskb:
68 	kfree_skb(nskb);
69 }
70 
71 static void
72 synproxy_send_client_synack(struct net *net,
73 			    const struct sk_buff *skb, const struct tcphdr *th,
74 			    const struct synproxy_options *opts)
75 {
76 	struct sk_buff *nskb;
77 	struct iphdr *iph, *niph;
78 	struct tcphdr *nth;
79 	unsigned int tcp_hdr_size;
80 	u16 mss = opts->mss;
81 
82 	iph = ip_hdr(skb);
83 
84 	tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
85 	nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
86 			 GFP_ATOMIC);
87 	if (nskb == NULL)
88 		return;
89 	skb_reserve(nskb, MAX_TCP_HEADER);
90 
91 	niph = synproxy_build_ip(net, nskb, iph->daddr, iph->saddr);
92 
93 	skb_reset_transport_header(nskb);
94 	nth = 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;
103 	nth->window	= 0;
104 	nth->check	= 0;
105 	nth->urg_ptr	= 0;
106 
107 	synproxy_build_options(nth, opts);
108 
109 	synproxy_send_tcp(net, skb, nskb, skb_nfct(skb),
110 			  IP_CT_ESTABLISHED_REPLY, niph, nth, tcp_hdr_size);
111 }
112 
113 static void
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)
117 {
118 	struct synproxy_net *snet = synproxy_pernet(net);
119 	struct sk_buff *nskb;
120 	struct iphdr *iph, *niph;
121 	struct tcphdr *nth;
122 	unsigned int tcp_hdr_size;
123 
124 	iph = ip_hdr(skb);
125 
126 	tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
127 	nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
128 			 GFP_ATOMIC);
129 	if (nskb == NULL)
130 		return;
131 	skb_reserve(nskb, MAX_TCP_HEADER);
132 
133 	niph = synproxy_build_ip(net, nskb, iph->saddr, iph->daddr);
134 
135 	skb_reset_transport_header(nskb);
136 	nth = 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.
142 	 */
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;
149 	nth->check	= 0;
150 	nth->urg_ptr	= 0;
151 
152 	synproxy_build_options(nth, opts);
153 
154 	synproxy_send_tcp(net, skb, nskb, &snet->tmpl->ct_general, IP_CT_NEW,
155 			  niph, nth, tcp_hdr_size);
156 }
157 
158 static void
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)
163 {
164 	struct sk_buff *nskb;
165 	struct iphdr *iph, *niph;
166 	struct tcphdr *nth;
167 	unsigned int tcp_hdr_size;
168 
169 	iph = ip_hdr(skb);
170 
171 	tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
172 	nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
173 			 GFP_ATOMIC);
174 	if (nskb == NULL)
175 		return;
176 	skb_reserve(nskb, MAX_TCP_HEADER);
177 
178 	niph = synproxy_build_ip(net, nskb, iph->daddr, iph->saddr);
179 
180 	skb_reset_transport_header(nskb);
181 	nth = 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);
189 	nth->check	= 0;
190 	nth->urg_ptr	= 0;
191 
192 	synproxy_build_options(nth, opts);
193 
194 	synproxy_send_tcp(net, skb, nskb, NULL, 0, niph, nth, tcp_hdr_size);
195 }
196 
197 static void
198 synproxy_send_client_ack(struct net *net,
199 			 const struct sk_buff *skb, const struct tcphdr *th,
200 			 const struct synproxy_options *opts)
201 {
202 	struct sk_buff *nskb;
203 	struct iphdr *iph, *niph;
204 	struct tcphdr *nth;
205 	unsigned int tcp_hdr_size;
206 
207 	iph = ip_hdr(skb);
208 
209 	tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
210 	nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
211 			 GFP_ATOMIC);
212 	if (nskb == NULL)
213 		return;
214 	skb_reserve(nskb, MAX_TCP_HEADER);
215 
216 	niph = synproxy_build_ip(net, nskb, iph->saddr, iph->daddr);
217 
218 	skb_reset_transport_header(nskb);
219 	nth = 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);
227 	nth->check	= 0;
228 	nth->urg_ptr	= 0;
229 
230 	synproxy_build_options(nth, opts);
231 
232 	synproxy_send_tcp(net, skb, nskb, skb_nfct(skb),
233 			  IP_CT_ESTABLISHED_REPLY, niph, nth, tcp_hdr_size);
234 }
235 
236 static bool
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)
240 {
241 	struct synproxy_net *snet = synproxy_pernet(net);
242 	int mss;
243 
244 	mss = __cookie_v4_check(ip_hdr(skb), th, ntohl(th->ack_seq) - 1);
245 	if (mss == 0) {
246 		this_cpu_inc(snet->stats->cookie_invalid);
247 		return false;
248 	}
249 
250 	this_cpu_inc(snet->stats->cookie_valid);
251 	opts->mss = mss;
252 	opts->options |= XT_SYNPROXY_OPT_MSS;
253 
254 	if (opts->options & XT_SYNPROXY_OPT_TIMESTAMP)
255 		synproxy_check_timestamp_cookie(opts);
256 
257 	synproxy_send_server_syn(net, skb, th, opts, recv_seq);
258 	return true;
259 }
260 
261 static unsigned int
262 synproxy_tg4(struct sk_buff *skb, const struct xt_action_param *par)
263 {
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;
269 
270 	if (nf_ip_checksum(skb, xt_hooknum(par), par->thoff, IPPROTO_TCP))
271 		return NF_DROP;
272 
273 	th = skb_header_pointer(skb, par->thoff, sizeof(_th), &_th);
274 	if (th == NULL)
275 		return NF_DROP;
276 
277 	if (!synproxy_parse_options(skb, par->thoff, th, &opts))
278 		return NF_DROP;
279 
280 	if (th->syn && !(th->ack || th->fin || th->rst)) {
281 		/* Initial SYN from client */
282 		this_cpu_inc(snet->stats->syn_received);
283 
284 		if (th->ece && th->cwr)
285 			opts.options |= XT_SYNPROXY_OPT_ECN;
286 
287 		opts.options &= info->options;
288 		if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
289 			synproxy_init_timestamp_cookie(info, &opts);
290 		else
291 			opts.options &= ~(XT_SYNPROXY_OPT_WSCALE |
292 					  XT_SYNPROXY_OPT_SACK_PERM |
293 					  XT_SYNPROXY_OPT_ECN);
294 
295 		synproxy_send_client_synack(net, skb, th, &opts);
296 		consume_skb(skb);
297 		return NF_STOLEN;
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))) {
301 			consume_skb(skb);
302 			return NF_STOLEN;
303 		} else {
304 			return NF_DROP;
305 		}
306 	}
307 
308 	return XT_CONTINUE;
309 }
310 
311 static unsigned int ipv4_synproxy_hook(void *priv,
312 				       struct sk_buff *skb,
313 				       const struct nf_hook_state *nhs)
314 {
315 	struct net *net = nhs->net;
316 	struct synproxy_net *snet = synproxy_pernet(net);
317 	enum ip_conntrack_info ctinfo;
318 	struct nf_conn *ct;
319 	struct nf_conn_synproxy *synproxy;
320 	struct synproxy_options opts = {};
321 	const struct ip_ct_tcp *state;
322 	struct tcphdr *th, _th;
323 	unsigned int thoff;
324 
325 	ct = nf_ct_get(skb, &ctinfo);
326 	if (ct == NULL)
327 		return NF_ACCEPT;
328 
329 	synproxy = nfct_synproxy(ct);
330 	if (synproxy == NULL)
331 		return NF_ACCEPT;
332 
333 	if (nf_is_loopback_packet(skb))
334 		return NF_ACCEPT;
335 
336 	thoff = ip_hdrlen(skb);
337 	th = skb_header_pointer(skb, thoff, sizeof(_th), &_th);
338 	if (th == NULL)
339 		return NF_DROP;
340 
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 -
346 						      ntohl(th->seq) + 1);
347 			break;
348 		}
349 
350 		if (!th->syn || th->ack ||
351 		    CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
352 			break;
353 
354 		/* Reopened connection - reset the sequence number and timestamp
355 		 * adjustments, they will get initialized once the connection is
356 		 * reestablished.
357 		 */
358 		nf_ct_seqadj_init(ct, ctinfo, 0);
359 		synproxy->tsoff = 0;
360 		this_cpu_inc(snet->stats->conn_reopened);
361 
362 		/* fall through */
363 	case TCP_CONNTRACK_SYN_SENT:
364 		if (!synproxy_parse_options(skb, thoff, th, &opts))
365 			return NF_DROP;
366 
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.
372 			 */
373 			if (synproxy_recv_client_ack(net, skb, th, &opts,
374 						     ntohl(th->seq) + 1)) {
375 				this_cpu_inc(snet->stats->cookie_retrans);
376 				consume_skb(skb);
377 				return NF_STOLEN;
378 			} else {
379 				return NF_DROP;
380 			}
381 		}
382 
383 		synproxy->isn = ntohl(th->ack_seq);
384 		if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
385 			synproxy->its = opts.tsecr;
386 		break;
387 	case TCP_CONNTRACK_SYN_RECV:
388 		if (!th->syn || !th->ack)
389 			break;
390 
391 		if (!synproxy_parse_options(skb, thoff, th, &opts))
392 			return NF_DROP;
393 
394 		if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
395 			synproxy->tsoff = opts.tsval - synproxy->its;
396 
397 		opts.options &= ~(XT_SYNPROXY_OPT_MSS |
398 				  XT_SYNPROXY_OPT_WSCALE |
399 				  XT_SYNPROXY_OPT_SACK_PERM);
400 
401 		swap(opts.tsval, opts.tsecr);
402 		synproxy_send_server_ack(net, state, skb, th, &opts);
403 
404 		nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
405 
406 		swap(opts.tsval, opts.tsecr);
407 		synproxy_send_client_ack(net, skb, th, &opts);
408 
409 		consume_skb(skb);
410 		return NF_STOLEN;
411 	default:
412 		break;
413 	}
414 
415 	synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy);
416 	return NF_ACCEPT;
417 }
418 
419 static const struct nf_hook_ops ipv4_synproxy_ops[] = {
420 	{
421 		.hook		= ipv4_synproxy_hook,
422 		.pf		= NFPROTO_IPV4,
423 		.hooknum	= NF_INET_LOCAL_IN,
424 		.priority	= NF_IP_PRI_CONNTRACK_CONFIRM - 1,
425 	},
426 	{
427 		.hook		= ipv4_synproxy_hook,
428 		.pf		= NFPROTO_IPV4,
429 		.hooknum	= NF_INET_POST_ROUTING,
430 		.priority	= NF_IP_PRI_CONNTRACK_CONFIRM - 1,
431 	},
432 };
433 
434 static int synproxy_tg4_check(const struct xt_tgchk_param *par)
435 {
436 	struct synproxy_net *snet = synproxy_pernet(par->net);
437 	const struct ipt_entry *e = par->entryinfo;
438 	int err;
439 
440 	if (e->ip.proto != IPPROTO_TCP ||
441 	    e->ip.invflags & XT_INV_PROTO)
442 		return -EINVAL;
443 
444 	err = nf_ct_netns_get(par->net, par->family);
445 	if (err)
446 		return err;
447 
448 	if (snet->hook_ref4 == 0) {
449 		err = nf_register_net_hooks(par->net, ipv4_synproxy_ops,
450 					    ARRAY_SIZE(ipv4_synproxy_ops));
451 		if (err) {
452 			nf_ct_netns_put(par->net, par->family);
453 			return err;
454 		}
455 	}
456 
457 	snet->hook_ref4++;
458 	return err;
459 }
460 
461 static void synproxy_tg4_destroy(const struct xt_tgdtor_param *par)
462 {
463 	struct synproxy_net *snet = synproxy_pernet(par->net);
464 
465 	snet->hook_ref4--;
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);
470 }
471 
472 static struct xt_target synproxy_tg4_reg __read_mostly = {
473 	.name		= "SYNPROXY",
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,
480 	.me		= THIS_MODULE,
481 };
482 
483 static int __init synproxy_tg4_init(void)
484 {
485 	return xt_register_target(&synproxy_tg4_reg);
486 }
487 
488 static void __exit synproxy_tg4_exit(void)
489 {
490 	xt_unregister_target(&synproxy_tg4_reg);
491 }
492 
493 module_init(synproxy_tg4_init);
494 module_exit(synproxy_tg4_exit);
495 
496 MODULE_LICENSE("GPL");
497 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
498