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 	    ip_hdr(skb)->protocol != IPPROTO_TCP)
335 		return NF_ACCEPT;
336 
337 	thoff = ip_hdrlen(skb);
338 	th = skb_header_pointer(skb, thoff, sizeof(_th), &_th);
339 	if (th == NULL)
340 		return NF_DROP;
341 
342 	state = &ct->proto.tcp;
343 	switch (state->state) {
344 	case TCP_CONNTRACK_CLOSE:
345 		if (th->rst && !test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
346 			nf_ct_seqadj_init(ct, ctinfo, synproxy->isn -
347 						      ntohl(th->seq) + 1);
348 			break;
349 		}
350 
351 		if (!th->syn || th->ack ||
352 		    CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
353 			break;
354 
355 		/* Reopened connection - reset the sequence number and timestamp
356 		 * adjustments, they will get initialized once the connection is
357 		 * reestablished.
358 		 */
359 		nf_ct_seqadj_init(ct, ctinfo, 0);
360 		synproxy->tsoff = 0;
361 		this_cpu_inc(snet->stats->conn_reopened);
362 
363 		/* fall through */
364 	case TCP_CONNTRACK_SYN_SENT:
365 		if (!synproxy_parse_options(skb, thoff, th, &opts))
366 			return NF_DROP;
367 
368 		if (!th->syn && th->ack &&
369 		    CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
370 			/* Keep-Alives are sent with SEG.SEQ = SND.NXT-1,
371 			 * therefore we need to add 1 to make the SYN sequence
372 			 * number match the one of first SYN.
373 			 */
374 			if (synproxy_recv_client_ack(net, skb, th, &opts,
375 						     ntohl(th->seq) + 1)) {
376 				this_cpu_inc(snet->stats->cookie_retrans);
377 				consume_skb(skb);
378 				return NF_STOLEN;
379 			} else {
380 				return NF_DROP;
381 			}
382 		}
383 
384 		synproxy->isn = ntohl(th->ack_seq);
385 		if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
386 			synproxy->its = opts.tsecr;
387 		break;
388 	case TCP_CONNTRACK_SYN_RECV:
389 		if (!th->syn || !th->ack)
390 			break;
391 
392 		if (!synproxy_parse_options(skb, thoff, th, &opts))
393 			return NF_DROP;
394 
395 		if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
396 			synproxy->tsoff = opts.tsval - synproxy->its;
397 
398 		opts.options &= ~(XT_SYNPROXY_OPT_MSS |
399 				  XT_SYNPROXY_OPT_WSCALE |
400 				  XT_SYNPROXY_OPT_SACK_PERM);
401 
402 		swap(opts.tsval, opts.tsecr);
403 		synproxy_send_server_ack(net, state, skb, th, &opts);
404 
405 		nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
406 
407 		swap(opts.tsval, opts.tsecr);
408 		synproxy_send_client_ack(net, skb, th, &opts);
409 
410 		consume_skb(skb);
411 		return NF_STOLEN;
412 	default:
413 		break;
414 	}
415 
416 	synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy);
417 	return NF_ACCEPT;
418 }
419 
420 static const struct nf_hook_ops ipv4_synproxy_ops[] = {
421 	{
422 		.hook		= ipv4_synproxy_hook,
423 		.pf		= NFPROTO_IPV4,
424 		.hooknum	= NF_INET_LOCAL_IN,
425 		.priority	= NF_IP_PRI_CONNTRACK_CONFIRM - 1,
426 	},
427 	{
428 		.hook		= ipv4_synproxy_hook,
429 		.pf		= NFPROTO_IPV4,
430 		.hooknum	= NF_INET_POST_ROUTING,
431 		.priority	= NF_IP_PRI_CONNTRACK_CONFIRM - 1,
432 	},
433 };
434 
435 static int synproxy_tg4_check(const struct xt_tgchk_param *par)
436 {
437 	struct synproxy_net *snet = synproxy_pernet(par->net);
438 	const struct ipt_entry *e = par->entryinfo;
439 	int err;
440 
441 	if (e->ip.proto != IPPROTO_TCP ||
442 	    e->ip.invflags & XT_INV_PROTO)
443 		return -EINVAL;
444 
445 	err = nf_ct_netns_get(par->net, par->family);
446 	if (err)
447 		return err;
448 
449 	if (snet->hook_ref4 == 0) {
450 		err = nf_register_net_hooks(par->net, ipv4_synproxy_ops,
451 					    ARRAY_SIZE(ipv4_synproxy_ops));
452 		if (err) {
453 			nf_ct_netns_put(par->net, par->family);
454 			return err;
455 		}
456 	}
457 
458 	snet->hook_ref4++;
459 	return err;
460 }
461 
462 static void synproxy_tg4_destroy(const struct xt_tgdtor_param *par)
463 {
464 	struct synproxy_net *snet = synproxy_pernet(par->net);
465 
466 	snet->hook_ref4--;
467 	if (snet->hook_ref4 == 0)
468 		nf_unregister_net_hooks(par->net, ipv4_synproxy_ops,
469 					ARRAY_SIZE(ipv4_synproxy_ops));
470 	nf_ct_netns_put(par->net, par->family);
471 }
472 
473 static struct xt_target synproxy_tg4_reg __read_mostly = {
474 	.name		= "SYNPROXY",
475 	.family		= NFPROTO_IPV4,
476 	.hooks		= (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
477 	.target		= synproxy_tg4,
478 	.targetsize	= sizeof(struct xt_synproxy_info),
479 	.checkentry	= synproxy_tg4_check,
480 	.destroy	= synproxy_tg4_destroy,
481 	.me		= THIS_MODULE,
482 };
483 
484 static int __init synproxy_tg4_init(void)
485 {
486 	return xt_register_target(&synproxy_tg4_reg);
487 }
488 
489 static void __exit synproxy_tg4_exit(void)
490 {
491 	xt_unregister_target(&synproxy_tg4_reg);
492 }
493 
494 module_init(synproxy_tg4_init);
495 module_exit(synproxy_tg4_exit);
496 
497 MODULE_LICENSE("GPL");
498 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
499