]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/ipv4/netfilter/ipt_SYNPROXY.c
netfilter: ipt_SYNPROXY: Pass snet into synproxy_send_tcp
[mirror_ubuntu-artful-kernel.git] / net / ipv4 / netfilter / ipt_SYNPROXY.c
CommitLineData
48b1de4c
PM
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
20static struct iphdr *
ba6d0564 21synproxy_build_ip(struct sk_buff *skb, __be32 saddr, __be32 daddr)
48b1de4c
PM
22{
23 struct iphdr *iph;
24
25 skb_reset_network_header(skb);
26 iph = (struct iphdr *)skb_put(skb, sizeof(*iph));
27 iph->version = 4;
28 iph->ihl = sizeof(*iph) / 4;
29 iph->tos = 0;
30 iph->id = 0;
31 iph->frag_off = htons(IP_DF);
32 iph->ttl = sysctl_ip_default_ttl;
33 iph->protocol = IPPROTO_TCP;
34 iph->check = 0;
35 iph->saddr = saddr;
36 iph->daddr = daddr;
37
38 return iph;
39}
40
41static void
6a1d689d
EB
42synproxy_send_tcp(const struct synproxy_net *snet,
43 const struct sk_buff *skb, struct sk_buff *nskb,
48b1de4c
PM
44 struct nf_conntrack *nfct, enum ip_conntrack_info ctinfo,
45 struct iphdr *niph, struct tcphdr *nth,
46 unsigned int tcp_hdr_size)
47{
48 nth->check = ~tcp_v4_check(tcp_hdr_size, niph->saddr, niph->daddr, 0);
49 nskb->ip_summed = CHECKSUM_PARTIAL;
50 nskb->csum_start = (unsigned char *)nth - nskb->head;
51 nskb->csum_offset = offsetof(struct tcphdr, check);
52
53 skb_dst_set_noref(nskb, skb_dst(skb));
54 nskb->protocol = htons(ETH_P_IP);
55 if (ip_route_me_harder(nskb, RTN_UNSPEC))
56 goto free_nskb;
57
58 if (nfct) {
59 nskb->nfct = nfct;
60 nskb->nfctinfo = ctinfo;
61 nf_conntrack_get(nfct);
62 }
63
64 ip_local_out(nskb);
65 return;
66
67free_nskb:
68 kfree_skb(nskb);
69}
70
71static void
6a1d689d
EB
72synproxy_send_client_synack(const struct synproxy_net *snet,
73 const struct sk_buff *skb, const struct tcphdr *th,
48b1de4c
PM
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(nskb, iph->daddr, iph->saddr);
92
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;
103 nth->window = 0;
104 nth->check = 0;
105 nth->urg_ptr = 0;
106
107 synproxy_build_options(nth, opts);
108
6a1d689d 109 synproxy_send_tcp(snet, skb, nskb, skb->nfct, IP_CT_ESTABLISHED_REPLY,
48b1de4c
PM
110 niph, nth, tcp_hdr_size);
111}
112
113static void
114synproxy_send_server_syn(const struct synproxy_net *snet,
115 const struct sk_buff *skb, const struct tcphdr *th,
116 const struct synproxy_options *opts, u32 recv_seq)
117{
118 struct sk_buff *nskb;
119 struct iphdr *iph, *niph;
120 struct tcphdr *nth;
121 unsigned int tcp_hdr_size;
122
123 iph = ip_hdr(skb);
124
125 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
126 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
127 GFP_ATOMIC);
128 if (nskb == NULL)
129 return;
130 skb_reserve(nskb, MAX_TCP_HEADER);
131
132 niph = synproxy_build_ip(nskb, iph->saddr, iph->daddr);
133
134 skb_reset_transport_header(nskb);
135 nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
136 nth->source = th->source;
137 nth->dest = th->dest;
138 nth->seq = htonl(recv_seq - 1);
139 /* ack_seq is used to relay our ISN to the synproxy hook to initialize
140 * sequence number translation once a connection tracking entry exists.
141 */
142 nth->ack_seq = htonl(ntohl(th->ack_seq) - 1);
143 tcp_flag_word(nth) = TCP_FLAG_SYN;
144 if (opts->options & XT_SYNPROXY_OPT_ECN)
145 tcp_flag_word(nth) |= TCP_FLAG_ECE | TCP_FLAG_CWR;
146 nth->doff = tcp_hdr_size / 4;
147 nth->window = th->window;
148 nth->check = 0;
149 nth->urg_ptr = 0;
150
151 synproxy_build_options(nth, opts);
152
6a1d689d 153 synproxy_send_tcp(snet, skb, nskb, &snet->tmpl->ct_general, IP_CT_NEW,
48b1de4c
PM
154 niph, nth, tcp_hdr_size);
155}
156
157static void
158synproxy_send_server_ack(const struct synproxy_net *snet,
159 const struct ip_ct_tcp *state,
160 const struct sk_buff *skb, const struct tcphdr *th,
161 const struct synproxy_options *opts)
162{
163 struct sk_buff *nskb;
164 struct iphdr *iph, *niph;
165 struct tcphdr *nth;
166 unsigned int tcp_hdr_size;
167
168 iph = ip_hdr(skb);
169
170 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
171 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
172 GFP_ATOMIC);
173 if (nskb == NULL)
174 return;
175 skb_reserve(nskb, MAX_TCP_HEADER);
176
177 niph = synproxy_build_ip(nskb, iph->daddr, iph->saddr);
178
179 skb_reset_transport_header(nskb);
180 nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
181 nth->source = th->dest;
182 nth->dest = th->source;
183 nth->seq = htonl(ntohl(th->ack_seq));
184 nth->ack_seq = htonl(ntohl(th->seq) + 1);
185 tcp_flag_word(nth) = TCP_FLAG_ACK;
186 nth->doff = tcp_hdr_size / 4;
187 nth->window = htons(state->seen[IP_CT_DIR_ORIGINAL].td_maxwin);
188 nth->check = 0;
189 nth->urg_ptr = 0;
190
191 synproxy_build_options(nth, opts);
192
6a1d689d 193 synproxy_send_tcp(snet, skb, nskb, NULL, 0, niph, nth, tcp_hdr_size);
48b1de4c
PM
194}
195
196static void
197synproxy_send_client_ack(const struct synproxy_net *snet,
198 const struct sk_buff *skb, const struct tcphdr *th,
199 const struct synproxy_options *opts)
200{
201 struct sk_buff *nskb;
202 struct iphdr *iph, *niph;
203 struct tcphdr *nth;
204 unsigned int tcp_hdr_size;
205
206 iph = ip_hdr(skb);
207
208 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
209 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
210 GFP_ATOMIC);
211 if (nskb == NULL)
212 return;
213 skb_reserve(nskb, MAX_TCP_HEADER);
214
215 niph = synproxy_build_ip(nskb, iph->saddr, iph->daddr);
216
217 skb_reset_transport_header(nskb);
218 nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size);
219 nth->source = th->source;
220 nth->dest = th->dest;
221 nth->seq = htonl(ntohl(th->seq) + 1);
222 nth->ack_seq = th->ack_seq;
223 tcp_flag_word(nth) = TCP_FLAG_ACK;
224 nth->doff = tcp_hdr_size / 4;
ba6d0564 225 nth->window = htons(ntohs(th->window) >> opts->wscale);
48b1de4c
PM
226 nth->check = 0;
227 nth->urg_ptr = 0;
228
229 synproxy_build_options(nth, opts);
230
6a1d689d 231 synproxy_send_tcp(snet, skb, nskb, skb->nfct, IP_CT_ESTABLISHED_REPLY,
3c16241c 232 niph, nth, tcp_hdr_size);
48b1de4c
PM
233}
234
235static bool
236synproxy_recv_client_ack(const struct synproxy_net *snet,
237 const struct sk_buff *skb, const struct tcphdr *th,
238 struct synproxy_options *opts, u32 recv_seq)
239{
240 int mss;
241
242 mss = __cookie_v4_check(ip_hdr(skb), th, ntohl(th->ack_seq) - 1);
243 if (mss == 0) {
244 this_cpu_inc(snet->stats->cookie_invalid);
245 return false;
246 }
247
248 this_cpu_inc(snet->stats->cookie_valid);
249 opts->mss = mss;
a6441b7a 250 opts->options |= XT_SYNPROXY_OPT_MSS;
48b1de4c
PM
251
252 if (opts->options & XT_SYNPROXY_OPT_TIMESTAMP)
253 synproxy_check_timestamp_cookie(opts);
254
255 synproxy_send_server_syn(snet, skb, th, opts, recv_seq);
256 return true;
257}
258
259static unsigned int
260synproxy_tg4(struct sk_buff *skb, const struct xt_action_param *par)
261{
262 const struct xt_synproxy_info *info = par->targinfo;
686c9b50 263 struct synproxy_net *snet = synproxy_pernet(par->net);
48b1de4c
PM
264 struct synproxy_options opts = {};
265 struct tcphdr *th, _th;
266
267 if (nf_ip_checksum(skb, par->hooknum, par->thoff, IPPROTO_TCP))
268 return NF_DROP;
269
270 th = skb_header_pointer(skb, par->thoff, sizeof(_th), &_th);
271 if (th == NULL)
272 return NF_DROP;
273
f4a87e7b
PM
274 if (!synproxy_parse_options(skb, par->thoff, th, &opts))
275 return NF_DROP;
48b1de4c 276
775ada6d 277 if (th->syn && !(th->ack || th->fin || th->rst)) {
48b1de4c
PM
278 /* Initial SYN from client */
279 this_cpu_inc(snet->stats->syn_received);
280
281 if (th->ece && th->cwr)
282 opts.options |= XT_SYNPROXY_OPT_ECN;
283
284 opts.options &= info->options;
285 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
286 synproxy_init_timestamp_cookie(info, &opts);
287 else
288 opts.options &= ~(XT_SYNPROXY_OPT_WSCALE |
289 XT_SYNPROXY_OPT_SACK_PERM |
290 XT_SYNPROXY_OPT_ECN);
291
6a1d689d 292 synproxy_send_client_synack(snet, skb, th, &opts);
7cc9eb6e
JDB
293 return NF_DROP;
294
295 } else if (th->ack && !(th->fin || th->rst || th->syn)) {
48b1de4c
PM
296 /* ACK from client */
297 synproxy_recv_client_ack(snet, skb, th, &opts, ntohl(th->seq));
7cc9eb6e
JDB
298 return NF_DROP;
299 }
48b1de4c 300
7cc9eb6e 301 return XT_CONTINUE;
48b1de4c
PM
302}
303
06198b34 304static unsigned int ipv4_synproxy_hook(void *priv,
48b1de4c 305 struct sk_buff *skb,
238e54c9 306 const struct nf_hook_state *nhs)
48b1de4c 307{
9dff2c96 308 struct synproxy_net *snet = synproxy_pernet(nhs->net);
48b1de4c
PM
309 enum ip_conntrack_info ctinfo;
310 struct nf_conn *ct;
311 struct nf_conn_synproxy *synproxy;
312 struct synproxy_options opts = {};
313 const struct ip_ct_tcp *state;
314 struct tcphdr *th, _th;
315 unsigned int thoff;
316
317 ct = nf_ct_get(skb, &ctinfo);
318 if (ct == NULL)
319 return NF_ACCEPT;
320
321 synproxy = nfct_synproxy(ct);
322 if (synproxy == NULL)
323 return NF_ACCEPT;
324
325 if (nf_is_loopback_packet(skb))
326 return NF_ACCEPT;
327
328 thoff = ip_hdrlen(skb);
329 th = skb_header_pointer(skb, thoff, sizeof(_th), &_th);
330 if (th == NULL)
331 return NF_DROP;
332
333 state = &ct->proto.tcp;
334 switch (state->state) {
335 case TCP_CONNTRACK_CLOSE:
336 if (th->rst && !test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
337 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn -
338 ntohl(th->seq) + 1);
339 break;
340 }
341
342 if (!th->syn || th->ack ||
343 CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
344 break;
345
346 /* Reopened connection - reset the sequence number and timestamp
347 * adjustments, they will get initialized once the connection is
348 * reestablished.
349 */
350 nf_ct_seqadj_init(ct, ctinfo, 0);
351 synproxy->tsoff = 0;
352 this_cpu_inc(snet->stats->conn_reopened);
353
354 /* fall through */
355 case TCP_CONNTRACK_SYN_SENT:
f4a87e7b
PM
356 if (!synproxy_parse_options(skb, thoff, th, &opts))
357 return NF_DROP;
48b1de4c
PM
358
359 if (!th->syn && th->ack &&
360 CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
361 /* Keep-Alives are sent with SEG.SEQ = SND.NXT-1,
362 * therefore we need to add 1 to make the SYN sequence
363 * number match the one of first SYN.
364 */
365 if (synproxy_recv_client_ack(snet, skb, th, &opts,
366 ntohl(th->seq) + 1))
367 this_cpu_inc(snet->stats->cookie_retrans);
368
369 return NF_DROP;
370 }
371
372 synproxy->isn = ntohl(th->ack_seq);
373 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
374 synproxy->its = opts.tsecr;
375 break;
376 case TCP_CONNTRACK_SYN_RECV:
377 if (!th->syn || !th->ack)
378 break;
379
f4a87e7b
PM
380 if (!synproxy_parse_options(skb, thoff, th, &opts))
381 return NF_DROP;
382
48b1de4c
PM
383 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
384 synproxy->tsoff = opts.tsval - synproxy->its;
385
386 opts.options &= ~(XT_SYNPROXY_OPT_MSS |
387 XT_SYNPROXY_OPT_WSCALE |
388 XT_SYNPROXY_OPT_SACK_PERM);
389
390 swap(opts.tsval, opts.tsecr);
391 synproxy_send_server_ack(snet, state, skb, th, &opts);
392
393 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
394
395 swap(opts.tsval, opts.tsecr);
396 synproxy_send_client_ack(snet, skb, th, &opts);
397
398 consume_skb(skb);
399 return NF_STOLEN;
400 default:
401 break;
402 }
403
404 synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy);
405 return NF_ACCEPT;
406}
407
408static int synproxy_tg4_check(const struct xt_tgchk_param *par)
409{
410 const struct ipt_entry *e = par->entryinfo;
411
412 if (e->ip.proto != IPPROTO_TCP ||
413 e->ip.invflags & XT_INV_PROTO)
414 return -EINVAL;
415
416 return nf_ct_l3proto_try_module_get(par->family);
417}
418
419static void synproxy_tg4_destroy(const struct xt_tgdtor_param *par)
420{
421 nf_ct_l3proto_module_put(par->family);
422}
423
424static struct xt_target synproxy_tg4_reg __read_mostly = {
425 .name = "SYNPROXY",
426 .family = NFPROTO_IPV4,
f01b3926 427 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
48b1de4c
PM
428 .target = synproxy_tg4,
429 .targetsize = sizeof(struct xt_synproxy_info),
430 .checkentry = synproxy_tg4_check,
431 .destroy = synproxy_tg4_destroy,
432 .me = THIS_MODULE,
433};
434
435static struct nf_hook_ops ipv4_synproxy_ops[] __read_mostly = {
436 {
437 .hook = ipv4_synproxy_hook,
438 .owner = THIS_MODULE,
439 .pf = NFPROTO_IPV4,
440 .hooknum = NF_INET_LOCAL_IN,
441 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
442 },
443 {
444 .hook = ipv4_synproxy_hook,
445 .owner = THIS_MODULE,
446 .pf = NFPROTO_IPV4,
447 .hooknum = NF_INET_POST_ROUTING,
448 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
449 },
450};
451
452static int __init synproxy_tg4_init(void)
453{
454 int err;
455
456 err = nf_register_hooks(ipv4_synproxy_ops,
457 ARRAY_SIZE(ipv4_synproxy_ops));
458 if (err < 0)
459 goto err1;
460
461 err = xt_register_target(&synproxy_tg4_reg);
462 if (err < 0)
463 goto err2;
464
465 return 0;
466
467err2:
468 nf_unregister_hooks(ipv4_synproxy_ops, ARRAY_SIZE(ipv4_synproxy_ops));
469err1:
470 return err;
471}
472
473static void __exit synproxy_tg4_exit(void)
474{
475 xt_unregister_target(&synproxy_tg4_reg);
476 nf_unregister_hooks(ipv4_synproxy_ops, ARRAY_SIZE(ipv4_synproxy_ops));
477}
478
479module_init(synproxy_tg4_init);
480module_exit(synproxy_tg4_exit);
481
482MODULE_LICENSE("GPL");
483MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");