~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/netfilter/ipt_SYNPROXY.c

Version: ~ [ linux-5.15-rc1 ] ~ [ linux-5.14.5 ] ~ [ linux-5.13.18 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.66 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.147 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.206 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.246 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.282 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.283 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp