Merge remote-tracking branches 'asoc/fix/adsp', 'asoc/fix/atmel', 'asoc/fix/hdac...
[linux-2.6-microblaze.git] / net / ipv4 / netfilter / ipt_SYNPROXY.c
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 = (struct iphdr *)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 = (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
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 = (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.
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 = (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);
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 = (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);
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                 return NF_DROP;
297
298         } else if (th->ack && !(th->fin || th->rst || th->syn)) {
299                 /* ACK from client */
300                 synproxy_recv_client_ack(net, skb, th, &opts, ntohl(th->seq));
301                 return NF_DROP;
302         }
303
304         return XT_CONTINUE;
305 }
306
307 static unsigned int ipv4_synproxy_hook(void *priv,
308                                        struct sk_buff *skb,
309                                        const struct nf_hook_state *nhs)
310 {
311         struct net *net = nhs->net;
312         struct synproxy_net *snet = synproxy_pernet(net);
313         enum ip_conntrack_info ctinfo;
314         struct nf_conn *ct;
315         struct nf_conn_synproxy *synproxy;
316         struct synproxy_options opts = {};
317         const struct ip_ct_tcp *state;
318         struct tcphdr *th, _th;
319         unsigned int thoff;
320
321         ct = nf_ct_get(skb, &ctinfo);
322         if (ct == NULL)
323                 return NF_ACCEPT;
324
325         synproxy = nfct_synproxy(ct);
326         if (synproxy == NULL)
327                 return NF_ACCEPT;
328
329         if (nf_is_loopback_packet(skb))
330                 return NF_ACCEPT;
331
332         thoff = ip_hdrlen(skb);
333         th = skb_header_pointer(skb, thoff, sizeof(_th), &_th);
334         if (th == NULL)
335                 return NF_DROP;
336
337         state = &ct->proto.tcp;
338         switch (state->state) {
339         case TCP_CONNTRACK_CLOSE:
340                 if (th->rst && !test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) {
341                         nf_ct_seqadj_init(ct, ctinfo, synproxy->isn -
342                                                       ntohl(th->seq) + 1);
343                         break;
344                 }
345
346                 if (!th->syn || th->ack ||
347                     CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
348                         break;
349
350                 /* Reopened connection - reset the sequence number and timestamp
351                  * adjustments, they will get initialized once the connection is
352                  * reestablished.
353                  */
354                 nf_ct_seqadj_init(ct, ctinfo, 0);
355                 synproxy->tsoff = 0;
356                 this_cpu_inc(snet->stats->conn_reopened);
357
358                 /* fall through */
359         case TCP_CONNTRACK_SYN_SENT:
360                 if (!synproxy_parse_options(skb, thoff, th, &opts))
361                         return NF_DROP;
362
363                 if (!th->syn && th->ack &&
364                     CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
365                         /* Keep-Alives are sent with SEG.SEQ = SND.NXT-1,
366                          * therefore we need to add 1 to make the SYN sequence
367                          * number match the one of first SYN.
368                          */
369                         if (synproxy_recv_client_ack(net, skb, th, &opts,
370                                                      ntohl(th->seq) + 1))
371                                 this_cpu_inc(snet->stats->cookie_retrans);
372
373                         return NF_DROP;
374                 }
375
376                 synproxy->isn = ntohl(th->ack_seq);
377                 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
378                         synproxy->its = opts.tsecr;
379                 break;
380         case TCP_CONNTRACK_SYN_RECV:
381                 if (!th->syn || !th->ack)
382                         break;
383
384                 if (!synproxy_parse_options(skb, thoff, th, &opts))
385                         return NF_DROP;
386
387                 if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP)
388                         synproxy->tsoff = opts.tsval - synproxy->its;
389
390                 opts.options &= ~(XT_SYNPROXY_OPT_MSS |
391                                   XT_SYNPROXY_OPT_WSCALE |
392                                   XT_SYNPROXY_OPT_SACK_PERM);
393
394                 swap(opts.tsval, opts.tsecr);
395                 synproxy_send_server_ack(net, state, skb, th, &opts);
396
397                 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
398
399                 swap(opts.tsval, opts.tsecr);
400                 synproxy_send_client_ack(net, skb, th, &opts);
401
402                 consume_skb(skb);
403                 return NF_STOLEN;
404         default:
405                 break;
406         }
407
408         synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy);
409         return NF_ACCEPT;
410 }
411
412 static int synproxy_tg4_check(const struct xt_tgchk_param *par)
413 {
414         const struct ipt_entry *e = par->entryinfo;
415
416         if (e->ip.proto != IPPROTO_TCP ||
417             e->ip.invflags & XT_INV_PROTO)
418                 return -EINVAL;
419
420         return nf_ct_netns_get(par->net, par->family);
421 }
422
423 static void synproxy_tg4_destroy(const struct xt_tgdtor_param *par)
424 {
425         nf_ct_netns_put(par->net, par->family);
426 }
427
428 static struct xt_target synproxy_tg4_reg __read_mostly = {
429         .name           = "SYNPROXY",
430         .family         = NFPROTO_IPV4,
431         .hooks          = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
432         .target         = synproxy_tg4,
433         .targetsize     = sizeof(struct xt_synproxy_info),
434         .checkentry     = synproxy_tg4_check,
435         .destroy        = synproxy_tg4_destroy,
436         .me             = THIS_MODULE,
437 };
438
439 static struct nf_hook_ops ipv4_synproxy_ops[] __read_mostly = {
440         {
441                 .hook           = ipv4_synproxy_hook,
442                 .pf             = NFPROTO_IPV4,
443                 .hooknum        = NF_INET_LOCAL_IN,
444                 .priority       = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
445         },
446         {
447                 .hook           = ipv4_synproxy_hook,
448                 .pf             = NFPROTO_IPV4,
449                 .hooknum        = NF_INET_POST_ROUTING,
450                 .priority       = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
451         },
452 };
453
454 static int __init synproxy_tg4_init(void)
455 {
456         int err;
457
458         err = nf_register_hooks(ipv4_synproxy_ops,
459                                 ARRAY_SIZE(ipv4_synproxy_ops));
460         if (err < 0)
461                 goto err1;
462
463         err = xt_register_target(&synproxy_tg4_reg);
464         if (err < 0)
465                 goto err2;
466
467         return 0;
468
469 err2:
470         nf_unregister_hooks(ipv4_synproxy_ops, ARRAY_SIZE(ipv4_synproxy_ops));
471 err1:
472         return err;
473 }
474
475 static void __exit synproxy_tg4_exit(void)
476 {
477         xt_unregister_target(&synproxy_tg4_reg);
478         nf_unregister_hooks(ipv4_synproxy_ops, ARRAY_SIZE(ipv4_synproxy_ops));
479 }
480
481 module_init(synproxy_tg4_init);
482 module_exit(synproxy_tg4_exit);
483
484 MODULE_LICENSE("GPL");
485 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");