1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2013 Patrick McHardy <kaber@trash.net>
6 #include <linux/module.h>
7 #include <linux/skbuff.h>
8 #include <asm/unaligned.h>
10 #include <net/netns/generic.h>
11 #include <linux/proc_fs.h>
13 #include <linux/netfilter_ipv6.h>
14 #include <linux/netfilter/nf_synproxy.h>
16 #include <net/netfilter/nf_conntrack.h>
17 #include <net/netfilter/nf_conntrack_ecache.h>
18 #include <net/netfilter/nf_conntrack_extend.h>
19 #include <net/netfilter/nf_conntrack_seqadj.h>
20 #include <net/netfilter/nf_conntrack_synproxy.h>
21 #include <net/netfilter/nf_conntrack_zones.h>
22 #include <net/netfilter/nf_synproxy.h>
24 unsigned int synproxy_net_id;
25 EXPORT_SYMBOL_GPL(synproxy_net_id);
28 synproxy_parse_options(const struct sk_buff *skb, unsigned int doff,
29 const struct tcphdr *th, struct synproxy_options *opts)
31 int length = (th->doff * 4) - sizeof(*th);
34 if (unlikely(length < 0))
37 ptr = skb_header_pointer(skb, doff + sizeof(*th), length, buf);
63 if (opsize == TCPOLEN_MSS) {
64 opts->mss_option = get_unaligned_be16(ptr);
65 opts->options |= NF_SYNPROXY_OPT_MSS;
69 if (opsize == TCPOLEN_WINDOW) {
71 if (opts->wscale > TCP_MAX_WSCALE)
72 opts->wscale = TCP_MAX_WSCALE;
73 opts->options |= NF_SYNPROXY_OPT_WSCALE;
76 case TCPOPT_TIMESTAMP:
77 if (opsize == TCPOLEN_TIMESTAMP) {
78 opts->tsval = get_unaligned_be32(ptr);
79 opts->tsecr = get_unaligned_be32(ptr + 4);
80 opts->options |= NF_SYNPROXY_OPT_TIMESTAMP;
83 case TCPOPT_SACK_PERM:
84 if (opsize == TCPOLEN_SACK_PERM)
85 opts->options |= NF_SYNPROXY_OPT_SACK_PERM;
95 EXPORT_SYMBOL_GPL(synproxy_parse_options);
98 synproxy_options_size(const struct synproxy_options *opts)
100 unsigned int size = 0;
102 if (opts->options & NF_SYNPROXY_OPT_MSS)
103 size += TCPOLEN_MSS_ALIGNED;
104 if (opts->options & NF_SYNPROXY_OPT_TIMESTAMP)
105 size += TCPOLEN_TSTAMP_ALIGNED;
106 else if (opts->options & NF_SYNPROXY_OPT_SACK_PERM)
107 size += TCPOLEN_SACKPERM_ALIGNED;
108 if (opts->options & NF_SYNPROXY_OPT_WSCALE)
109 size += TCPOLEN_WSCALE_ALIGNED;
115 synproxy_build_options(struct tcphdr *th, const struct synproxy_options *opts)
117 __be32 *ptr = (__be32 *)(th + 1);
118 u8 options = opts->options;
120 if (options & NF_SYNPROXY_OPT_MSS)
121 *ptr++ = htonl((TCPOPT_MSS << 24) |
122 (TCPOLEN_MSS << 16) |
125 if (options & NF_SYNPROXY_OPT_TIMESTAMP) {
126 if (options & NF_SYNPROXY_OPT_SACK_PERM)
127 *ptr++ = htonl((TCPOPT_SACK_PERM << 24) |
128 (TCPOLEN_SACK_PERM << 16) |
129 (TCPOPT_TIMESTAMP << 8) |
132 *ptr++ = htonl((TCPOPT_NOP << 24) |
134 (TCPOPT_TIMESTAMP << 8) |
137 *ptr++ = htonl(opts->tsval);
138 *ptr++ = htonl(opts->tsecr);
139 } else if (options & NF_SYNPROXY_OPT_SACK_PERM)
140 *ptr++ = htonl((TCPOPT_NOP << 24) |
142 (TCPOPT_SACK_PERM << 8) |
145 if (options & NF_SYNPROXY_OPT_WSCALE)
146 *ptr++ = htonl((TCPOPT_NOP << 24) |
147 (TCPOPT_WINDOW << 16) |
148 (TCPOLEN_WINDOW << 8) |
152 void synproxy_init_timestamp_cookie(const struct nf_synproxy_info *info,
153 struct synproxy_options *opts)
155 opts->tsecr = opts->tsval;
156 opts->tsval = tcp_time_stamp_raw() & ~0x3f;
158 if (opts->options & NF_SYNPROXY_OPT_WSCALE) {
159 opts->tsval |= opts->wscale;
160 opts->wscale = info->wscale;
164 if (opts->options & NF_SYNPROXY_OPT_SACK_PERM)
165 opts->tsval |= 1 << 4;
167 if (opts->options & NF_SYNPROXY_OPT_ECN)
168 opts->tsval |= 1 << 5;
170 EXPORT_SYMBOL_GPL(synproxy_init_timestamp_cookie);
173 synproxy_check_timestamp_cookie(struct synproxy_options *opts)
175 opts->wscale = opts->tsecr & 0xf;
176 if (opts->wscale != 0xf)
177 opts->options |= NF_SYNPROXY_OPT_WSCALE;
179 opts->options |= opts->tsecr & (1 << 4) ? NF_SYNPROXY_OPT_SACK_PERM : 0;
181 opts->options |= opts->tsecr & (1 << 5) ? NF_SYNPROXY_OPT_ECN : 0;
185 synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff,
186 struct tcphdr *th, struct nf_conn *ct,
187 enum ip_conntrack_info ctinfo,
188 const struct nf_conn_synproxy *synproxy)
190 unsigned int optoff, optend;
193 if (synproxy->tsoff == 0)
196 optoff = protoff + sizeof(struct tcphdr);
197 optend = protoff + th->doff * 4;
199 if (skb_ensure_writable(skb, optend))
202 while (optoff < optend) {
203 unsigned char *op = skb->data + optoff;
212 if (optoff + 1 == optend ||
213 optoff + op[1] > optend ||
216 if (op[0] == TCPOPT_TIMESTAMP &&
217 op[1] == TCPOLEN_TIMESTAMP) {
218 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
219 ptr = (__be32 *)&op[2];
221 *ptr = htonl(ntohl(*ptr) -
224 ptr = (__be32 *)&op[6];
226 *ptr = htonl(ntohl(*ptr) +
229 inet_proto_csum_replace4(&th->check, skb,
239 static struct nf_ct_ext_type nf_ct_synproxy_extend __read_mostly = {
240 .len = sizeof(struct nf_conn_synproxy),
241 .align = __alignof__(struct nf_conn_synproxy),
242 .id = NF_CT_EXT_SYNPROXY,
245 #ifdef CONFIG_PROC_FS
246 static void *synproxy_cpu_seq_start(struct seq_file *seq, loff_t *pos)
248 struct synproxy_net *snet = synproxy_pernet(seq_file_net(seq));
252 return SEQ_START_TOKEN;
254 for (cpu = *pos - 1; cpu < nr_cpu_ids; cpu++) {
255 if (!cpu_possible(cpu))
258 return per_cpu_ptr(snet->stats, cpu);
264 static void *synproxy_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
266 struct synproxy_net *snet = synproxy_pernet(seq_file_net(seq));
269 for (cpu = *pos; cpu < nr_cpu_ids; cpu++) {
270 if (!cpu_possible(cpu))
273 return per_cpu_ptr(snet->stats, cpu);
279 static void synproxy_cpu_seq_stop(struct seq_file *seq, void *v)
284 static int synproxy_cpu_seq_show(struct seq_file *seq, void *v)
286 struct synproxy_stats *stats = v;
288 if (v == SEQ_START_TOKEN) {
289 seq_puts(seq, "entries\t\tsyn_received\t"
290 "cookie_invalid\tcookie_valid\t"
291 "cookie_retrans\tconn_reopened\n");
295 seq_printf(seq, "%08x\t%08x\t%08x\t%08x\t%08x\t%08x\n", 0,
297 stats->cookie_invalid,
299 stats->cookie_retrans,
300 stats->conn_reopened);
305 static const struct seq_operations synproxy_cpu_seq_ops = {
306 .start = synproxy_cpu_seq_start,
307 .next = synproxy_cpu_seq_next,
308 .stop = synproxy_cpu_seq_stop,
309 .show = synproxy_cpu_seq_show,
312 static int __net_init synproxy_proc_init(struct net *net)
314 if (!proc_create_net("synproxy", 0444, net->proc_net_stat,
315 &synproxy_cpu_seq_ops, sizeof(struct seq_net_private)))
320 static void __net_exit synproxy_proc_exit(struct net *net)
322 remove_proc_entry("synproxy", net->proc_net_stat);
325 static int __net_init synproxy_proc_init(struct net *net)
330 static void __net_exit synproxy_proc_exit(struct net *net)
334 #endif /* CONFIG_PROC_FS */
336 static int __net_init synproxy_net_init(struct net *net)
338 struct synproxy_net *snet = synproxy_pernet(net);
342 ct = nf_ct_tmpl_alloc(net, &nf_ct_zone_dflt, GFP_KERNEL);
346 if (!nfct_seqadj_ext_add(ct))
348 if (!nfct_synproxy_ext_add(ct))
351 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
354 snet->stats = alloc_percpu(struct synproxy_stats);
355 if (snet->stats == NULL)
358 err = synproxy_proc_init(net);
365 free_percpu(snet->stats);
372 static void __net_exit synproxy_net_exit(struct net *net)
374 struct synproxy_net *snet = synproxy_pernet(net);
376 nf_ct_put(snet->tmpl);
377 synproxy_proc_exit(net);
378 free_percpu(snet->stats);
381 static struct pernet_operations synproxy_net_ops = {
382 .init = synproxy_net_init,
383 .exit = synproxy_net_exit,
384 .id = &synproxy_net_id,
385 .size = sizeof(struct synproxy_net),
388 static int __init synproxy_core_init(void)
392 err = nf_ct_extend_register(&nf_ct_synproxy_extend);
396 err = register_pernet_subsys(&synproxy_net_ops);
403 nf_ct_extend_unregister(&nf_ct_synproxy_extend);
408 static void __exit synproxy_core_exit(void)
410 unregister_pernet_subsys(&synproxy_net_ops);
411 nf_ct_extend_unregister(&nf_ct_synproxy_extend);
414 module_init(synproxy_core_init);
415 module_exit(synproxy_core_exit);
417 static struct iphdr *
418 synproxy_build_ip(struct net *net, struct sk_buff *skb, __be32 saddr,
423 skb_reset_network_header(skb);
424 iph = skb_put(skb, sizeof(*iph));
426 iph->ihl = sizeof(*iph) / 4;
429 iph->frag_off = htons(IP_DF);
430 iph->ttl = net->ipv4.sysctl_ip_default_ttl;
431 iph->protocol = IPPROTO_TCP;
440 synproxy_send_tcp(struct net *net,
441 const struct sk_buff *skb, struct sk_buff *nskb,
442 struct nf_conntrack *nfct, enum ip_conntrack_info ctinfo,
443 struct iphdr *niph, struct tcphdr *nth,
444 unsigned int tcp_hdr_size)
446 nth->check = ~tcp_v4_check(tcp_hdr_size, niph->saddr, niph->daddr, 0);
447 nskb->ip_summed = CHECKSUM_PARTIAL;
448 nskb->csum_start = (unsigned char *)nth - nskb->head;
449 nskb->csum_offset = offsetof(struct tcphdr, check);
451 skb_dst_set_noref(nskb, skb_dst(skb));
452 nskb->protocol = htons(ETH_P_IP);
453 if (ip_route_me_harder(net, nskb->sk, nskb, RTN_UNSPEC))
457 nf_ct_set(nskb, (struct nf_conn *)nfct, ctinfo);
458 nf_conntrack_get(nfct);
461 ip_local_out(net, nskb->sk, nskb);
469 synproxy_send_client_synack(struct net *net,
470 const struct sk_buff *skb, const struct tcphdr *th,
471 const struct synproxy_options *opts)
473 struct sk_buff *nskb;
474 struct iphdr *iph, *niph;
476 unsigned int tcp_hdr_size;
477 u16 mss = opts->mss_encode;
481 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
482 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
486 skb_reserve(nskb, MAX_TCP_HEADER);
488 niph = synproxy_build_ip(net, nskb, iph->daddr, iph->saddr);
490 skb_reset_transport_header(nskb);
491 nth = skb_put(nskb, tcp_hdr_size);
492 nth->source = th->dest;
493 nth->dest = th->source;
494 nth->seq = htonl(__cookie_v4_init_sequence(iph, th, &mss));
495 nth->ack_seq = htonl(ntohl(th->seq) + 1);
496 tcp_flag_word(nth) = TCP_FLAG_SYN | TCP_FLAG_ACK;
497 if (opts->options & NF_SYNPROXY_OPT_ECN)
498 tcp_flag_word(nth) |= TCP_FLAG_ECE;
499 nth->doff = tcp_hdr_size / 4;
504 synproxy_build_options(nth, opts);
506 synproxy_send_tcp(net, skb, nskb, skb_nfct(skb),
507 IP_CT_ESTABLISHED_REPLY, niph, nth, tcp_hdr_size);
509 EXPORT_SYMBOL_GPL(synproxy_send_client_synack);
512 synproxy_send_server_syn(struct net *net,
513 const struct sk_buff *skb, const struct tcphdr *th,
514 const struct synproxy_options *opts, u32 recv_seq)
516 struct synproxy_net *snet = synproxy_pernet(net);
517 struct sk_buff *nskb;
518 struct iphdr *iph, *niph;
520 unsigned int tcp_hdr_size;
524 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
525 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
529 skb_reserve(nskb, MAX_TCP_HEADER);
531 niph = synproxy_build_ip(net, nskb, iph->saddr, iph->daddr);
533 skb_reset_transport_header(nskb);
534 nth = skb_put(nskb, tcp_hdr_size);
535 nth->source = th->source;
536 nth->dest = th->dest;
537 nth->seq = htonl(recv_seq - 1);
538 /* ack_seq is used to relay our ISN to the synproxy hook to initialize
539 * sequence number translation once a connection tracking entry exists.
541 nth->ack_seq = htonl(ntohl(th->ack_seq) - 1);
542 tcp_flag_word(nth) = TCP_FLAG_SYN;
543 if (opts->options & NF_SYNPROXY_OPT_ECN)
544 tcp_flag_word(nth) |= TCP_FLAG_ECE | TCP_FLAG_CWR;
545 nth->doff = tcp_hdr_size / 4;
546 nth->window = th->window;
550 synproxy_build_options(nth, opts);
552 synproxy_send_tcp(net, skb, nskb, &snet->tmpl->ct_general, IP_CT_NEW,
553 niph, nth, tcp_hdr_size);
557 synproxy_send_server_ack(struct net *net,
558 const struct ip_ct_tcp *state,
559 const struct sk_buff *skb, const struct tcphdr *th,
560 const struct synproxy_options *opts)
562 struct sk_buff *nskb;
563 struct iphdr *iph, *niph;
565 unsigned int tcp_hdr_size;
569 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
570 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
574 skb_reserve(nskb, MAX_TCP_HEADER);
576 niph = synproxy_build_ip(net, nskb, iph->daddr, iph->saddr);
578 skb_reset_transport_header(nskb);
579 nth = skb_put(nskb, tcp_hdr_size);
580 nth->source = th->dest;
581 nth->dest = th->source;
582 nth->seq = htonl(ntohl(th->ack_seq));
583 nth->ack_seq = htonl(ntohl(th->seq) + 1);
584 tcp_flag_word(nth) = TCP_FLAG_ACK;
585 nth->doff = tcp_hdr_size / 4;
586 nth->window = htons(state->seen[IP_CT_DIR_ORIGINAL].td_maxwin);
590 synproxy_build_options(nth, opts);
592 synproxy_send_tcp(net, skb, nskb, NULL, 0, niph, nth, tcp_hdr_size);
596 synproxy_send_client_ack(struct net *net,
597 const struct sk_buff *skb, const struct tcphdr *th,
598 const struct synproxy_options *opts)
600 struct sk_buff *nskb;
601 struct iphdr *iph, *niph;
603 unsigned int tcp_hdr_size;
607 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
608 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
612 skb_reserve(nskb, MAX_TCP_HEADER);
614 niph = synproxy_build_ip(net, nskb, iph->saddr, iph->daddr);
616 skb_reset_transport_header(nskb);
617 nth = skb_put(nskb, tcp_hdr_size);
618 nth->source = th->source;
619 nth->dest = th->dest;
620 nth->seq = htonl(ntohl(th->seq) + 1);
621 nth->ack_seq = th->ack_seq;
622 tcp_flag_word(nth) = TCP_FLAG_ACK;
623 nth->doff = tcp_hdr_size / 4;
624 nth->window = htons(ntohs(th->window) >> opts->wscale);
628 synproxy_build_options(nth, opts);
630 synproxy_send_tcp(net, skb, nskb, skb_nfct(skb),
631 IP_CT_ESTABLISHED_REPLY, niph, nth, tcp_hdr_size);
635 synproxy_recv_client_ack(struct net *net,
636 const struct sk_buff *skb, const struct tcphdr *th,
637 struct synproxy_options *opts, u32 recv_seq)
639 struct synproxy_net *snet = synproxy_pernet(net);
642 mss = __cookie_v4_check(ip_hdr(skb), th, ntohl(th->ack_seq) - 1);
644 this_cpu_inc(snet->stats->cookie_invalid);
648 this_cpu_inc(snet->stats->cookie_valid);
649 opts->mss_option = mss;
650 opts->options |= NF_SYNPROXY_OPT_MSS;
652 if (opts->options & NF_SYNPROXY_OPT_TIMESTAMP)
653 synproxy_check_timestamp_cookie(opts);
655 synproxy_send_server_syn(net, skb, th, opts, recv_seq);
658 EXPORT_SYMBOL_GPL(synproxy_recv_client_ack);
661 ipv4_synproxy_hook(void *priv, struct sk_buff *skb,
662 const struct nf_hook_state *nhs)
664 struct net *net = nhs->net;
665 struct synproxy_net *snet = synproxy_pernet(net);
666 enum ip_conntrack_info ctinfo;
668 struct nf_conn_synproxy *synproxy;
669 struct synproxy_options opts = {};
670 const struct ip_ct_tcp *state;
671 struct tcphdr *th, _th;
674 ct = nf_ct_get(skb, &ctinfo);
678 synproxy = nfct_synproxy(ct);
682 if (nf_is_loopback_packet(skb) ||
683 ip_hdr(skb)->protocol != IPPROTO_TCP)
686 thoff = ip_hdrlen(skb);
687 th = skb_header_pointer(skb, thoff, sizeof(_th), &_th);
691 state = &ct->proto.tcp;
692 switch (state->state) {
693 case TCP_CONNTRACK_CLOSE:
694 if (th->rst && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
695 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn -
700 if (!th->syn || th->ack ||
701 CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
704 /* Reopened connection - reset the sequence number and timestamp
705 * adjustments, they will get initialized once the connection is
708 nf_ct_seqadj_init(ct, ctinfo, 0);
710 this_cpu_inc(snet->stats->conn_reopened);
712 case TCP_CONNTRACK_SYN_SENT:
713 if (!synproxy_parse_options(skb, thoff, th, &opts))
716 if (!th->syn && th->ack &&
717 CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
718 /* Keep-Alives are sent with SEG.SEQ = SND.NXT-1,
719 * therefore we need to add 1 to make the SYN sequence
720 * number match the one of first SYN.
722 if (synproxy_recv_client_ack(net, skb, th, &opts,
723 ntohl(th->seq) + 1)) {
724 this_cpu_inc(snet->stats->cookie_retrans);
732 synproxy->isn = ntohl(th->ack_seq);
733 if (opts.options & NF_SYNPROXY_OPT_TIMESTAMP)
734 synproxy->its = opts.tsecr;
736 nf_conntrack_event_cache(IPCT_SYNPROXY, ct);
738 case TCP_CONNTRACK_SYN_RECV:
739 if (!th->syn || !th->ack)
742 if (!synproxy_parse_options(skb, thoff, th, &opts))
745 if (opts.options & NF_SYNPROXY_OPT_TIMESTAMP) {
746 synproxy->tsoff = opts.tsval - synproxy->its;
747 nf_conntrack_event_cache(IPCT_SYNPROXY, ct);
750 opts.options &= ~(NF_SYNPROXY_OPT_MSS |
751 NF_SYNPROXY_OPT_WSCALE |
752 NF_SYNPROXY_OPT_SACK_PERM);
754 swap(opts.tsval, opts.tsecr);
755 synproxy_send_server_ack(net, state, skb, th, &opts);
757 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
758 nf_conntrack_event_cache(IPCT_SEQADJ, ct);
760 swap(opts.tsval, opts.tsecr);
761 synproxy_send_client_ack(net, skb, th, &opts);
769 synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy);
772 EXPORT_SYMBOL_GPL(ipv4_synproxy_hook);
774 static const struct nf_hook_ops ipv4_synproxy_ops[] = {
776 .hook = ipv4_synproxy_hook,
778 .hooknum = NF_INET_LOCAL_IN,
779 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
782 .hook = ipv4_synproxy_hook,
784 .hooknum = NF_INET_POST_ROUTING,
785 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
789 int nf_synproxy_ipv4_init(struct synproxy_net *snet, struct net *net)
793 if (snet->hook_ref4 == 0) {
794 err = nf_register_net_hooks(net, ipv4_synproxy_ops,
795 ARRAY_SIZE(ipv4_synproxy_ops));
803 EXPORT_SYMBOL_GPL(nf_synproxy_ipv4_init);
805 void nf_synproxy_ipv4_fini(struct synproxy_net *snet, struct net *net)
808 if (snet->hook_ref4 == 0)
809 nf_unregister_net_hooks(net, ipv4_synproxy_ops,
810 ARRAY_SIZE(ipv4_synproxy_ops));
812 EXPORT_SYMBOL_GPL(nf_synproxy_ipv4_fini);
814 #if IS_ENABLED(CONFIG_IPV6)
815 static struct ipv6hdr *
816 synproxy_build_ip_ipv6(struct net *net, struct sk_buff *skb,
817 const struct in6_addr *saddr,
818 const struct in6_addr *daddr)
822 skb_reset_network_header(skb);
823 iph = skb_put(skb, sizeof(*iph));
824 ip6_flow_hdr(iph, 0, 0);
825 iph->hop_limit = net->ipv6.devconf_all->hop_limit;
826 iph->nexthdr = IPPROTO_TCP;
834 synproxy_send_tcp_ipv6(struct net *net,
835 const struct sk_buff *skb, struct sk_buff *nskb,
836 struct nf_conntrack *nfct, enum ip_conntrack_info ctinfo,
837 struct ipv6hdr *niph, struct tcphdr *nth,
838 unsigned int tcp_hdr_size)
840 struct dst_entry *dst;
844 nth->check = ~tcp_v6_check(tcp_hdr_size, &niph->saddr, &niph->daddr, 0);
845 nskb->ip_summed = CHECKSUM_PARTIAL;
846 nskb->csum_start = (unsigned char *)nth - nskb->head;
847 nskb->csum_offset = offsetof(struct tcphdr, check);
849 memset(&fl6, 0, sizeof(fl6));
850 fl6.flowi6_proto = IPPROTO_TCP;
851 fl6.saddr = niph->saddr;
852 fl6.daddr = niph->daddr;
853 fl6.fl6_sport = nth->source;
854 fl6.fl6_dport = nth->dest;
855 security_skb_classify_flow((struct sk_buff *)skb,
856 flowi6_to_flowi_common(&fl6));
857 err = nf_ip6_route(net, &dst, flowi6_to_flowi(&fl6), false);
862 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
866 skb_dst_set(nskb, dst);
869 nf_ct_set(nskb, (struct nf_conn *)nfct, ctinfo);
870 nf_conntrack_get(nfct);
873 ip6_local_out(net, nskb->sk, nskb);
881 synproxy_send_client_synack_ipv6(struct net *net,
882 const struct sk_buff *skb,
883 const struct tcphdr *th,
884 const struct synproxy_options *opts)
886 struct sk_buff *nskb;
887 struct ipv6hdr *iph, *niph;
889 unsigned int tcp_hdr_size;
890 u16 mss = opts->mss_encode;
894 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
895 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
899 skb_reserve(nskb, MAX_TCP_HEADER);
901 niph = synproxy_build_ip_ipv6(net, nskb, &iph->daddr, &iph->saddr);
903 skb_reset_transport_header(nskb);
904 nth = skb_put(nskb, tcp_hdr_size);
905 nth->source = th->dest;
906 nth->dest = th->source;
907 nth->seq = htonl(nf_ipv6_cookie_init_sequence(iph, th, &mss));
908 nth->ack_seq = htonl(ntohl(th->seq) + 1);
909 tcp_flag_word(nth) = TCP_FLAG_SYN | TCP_FLAG_ACK;
910 if (opts->options & NF_SYNPROXY_OPT_ECN)
911 tcp_flag_word(nth) |= TCP_FLAG_ECE;
912 nth->doff = tcp_hdr_size / 4;
917 synproxy_build_options(nth, opts);
919 synproxy_send_tcp_ipv6(net, skb, nskb, skb_nfct(skb),
920 IP_CT_ESTABLISHED_REPLY, niph, nth,
923 EXPORT_SYMBOL_GPL(synproxy_send_client_synack_ipv6);
926 synproxy_send_server_syn_ipv6(struct net *net, const struct sk_buff *skb,
927 const struct tcphdr *th,
928 const struct synproxy_options *opts, u32 recv_seq)
930 struct synproxy_net *snet = synproxy_pernet(net);
931 struct sk_buff *nskb;
932 struct ipv6hdr *iph, *niph;
934 unsigned int tcp_hdr_size;
938 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
939 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
943 skb_reserve(nskb, MAX_TCP_HEADER);
945 niph = synproxy_build_ip_ipv6(net, nskb, &iph->saddr, &iph->daddr);
947 skb_reset_transport_header(nskb);
948 nth = skb_put(nskb, tcp_hdr_size);
949 nth->source = th->source;
950 nth->dest = th->dest;
951 nth->seq = htonl(recv_seq - 1);
952 /* ack_seq is used to relay our ISN to the synproxy hook to initialize
953 * sequence number translation once a connection tracking entry exists.
955 nth->ack_seq = htonl(ntohl(th->ack_seq) - 1);
956 tcp_flag_word(nth) = TCP_FLAG_SYN;
957 if (opts->options & NF_SYNPROXY_OPT_ECN)
958 tcp_flag_word(nth) |= TCP_FLAG_ECE | TCP_FLAG_CWR;
959 nth->doff = tcp_hdr_size / 4;
960 nth->window = th->window;
964 synproxy_build_options(nth, opts);
966 synproxy_send_tcp_ipv6(net, skb, nskb, &snet->tmpl->ct_general,
967 IP_CT_NEW, niph, nth, tcp_hdr_size);
971 synproxy_send_server_ack_ipv6(struct net *net, const struct ip_ct_tcp *state,
972 const struct sk_buff *skb,
973 const struct tcphdr *th,
974 const struct synproxy_options *opts)
976 struct sk_buff *nskb;
977 struct ipv6hdr *iph, *niph;
979 unsigned int tcp_hdr_size;
983 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
984 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
988 skb_reserve(nskb, MAX_TCP_HEADER);
990 niph = synproxy_build_ip_ipv6(net, nskb, &iph->daddr, &iph->saddr);
992 skb_reset_transport_header(nskb);
993 nth = skb_put(nskb, tcp_hdr_size);
994 nth->source = th->dest;
995 nth->dest = th->source;
996 nth->seq = htonl(ntohl(th->ack_seq));
997 nth->ack_seq = htonl(ntohl(th->seq) + 1);
998 tcp_flag_word(nth) = TCP_FLAG_ACK;
999 nth->doff = tcp_hdr_size / 4;
1000 nth->window = htons(state->seen[IP_CT_DIR_ORIGINAL].td_maxwin);
1004 synproxy_build_options(nth, opts);
1006 synproxy_send_tcp_ipv6(net, skb, nskb, NULL, 0, niph, nth,
1011 synproxy_send_client_ack_ipv6(struct net *net, const struct sk_buff *skb,
1012 const struct tcphdr *th,
1013 const struct synproxy_options *opts)
1015 struct sk_buff *nskb;
1016 struct ipv6hdr *iph, *niph;
1018 unsigned int tcp_hdr_size;
1020 iph = ipv6_hdr(skb);
1022 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
1023 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
1027 skb_reserve(nskb, MAX_TCP_HEADER);
1029 niph = synproxy_build_ip_ipv6(net, nskb, &iph->saddr, &iph->daddr);
1031 skb_reset_transport_header(nskb);
1032 nth = skb_put(nskb, tcp_hdr_size);
1033 nth->source = th->source;
1034 nth->dest = th->dest;
1035 nth->seq = htonl(ntohl(th->seq) + 1);
1036 nth->ack_seq = th->ack_seq;
1037 tcp_flag_word(nth) = TCP_FLAG_ACK;
1038 nth->doff = tcp_hdr_size / 4;
1039 nth->window = htons(ntohs(th->window) >> opts->wscale);
1043 synproxy_build_options(nth, opts);
1045 synproxy_send_tcp_ipv6(net, skb, nskb, skb_nfct(skb),
1046 IP_CT_ESTABLISHED_REPLY, niph, nth,
1051 synproxy_recv_client_ack_ipv6(struct net *net,
1052 const struct sk_buff *skb,
1053 const struct tcphdr *th,
1054 struct synproxy_options *opts, u32 recv_seq)
1056 struct synproxy_net *snet = synproxy_pernet(net);
1059 mss = nf_cookie_v6_check(ipv6_hdr(skb), th, ntohl(th->ack_seq) - 1);
1061 this_cpu_inc(snet->stats->cookie_invalid);
1065 this_cpu_inc(snet->stats->cookie_valid);
1066 opts->mss_option = mss;
1067 opts->options |= NF_SYNPROXY_OPT_MSS;
1069 if (opts->options & NF_SYNPROXY_OPT_TIMESTAMP)
1070 synproxy_check_timestamp_cookie(opts);
1072 synproxy_send_server_syn_ipv6(net, skb, th, opts, recv_seq);
1075 EXPORT_SYMBOL_GPL(synproxy_recv_client_ack_ipv6);
1078 ipv6_synproxy_hook(void *priv, struct sk_buff *skb,
1079 const struct nf_hook_state *nhs)
1081 struct net *net = nhs->net;
1082 struct synproxy_net *snet = synproxy_pernet(net);
1083 enum ip_conntrack_info ctinfo;
1085 struct nf_conn_synproxy *synproxy;
1086 struct synproxy_options opts = {};
1087 const struct ip_ct_tcp *state;
1088 struct tcphdr *th, _th;
1093 ct = nf_ct_get(skb, &ctinfo);
1097 synproxy = nfct_synproxy(ct);
1101 if (nf_is_loopback_packet(skb))
1104 nexthdr = ipv6_hdr(skb)->nexthdr;
1105 thoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
1107 if (thoff < 0 || nexthdr != IPPROTO_TCP)
1110 th = skb_header_pointer(skb, thoff, sizeof(_th), &_th);
1114 state = &ct->proto.tcp;
1115 switch (state->state) {
1116 case TCP_CONNTRACK_CLOSE:
1117 if (th->rst && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
1118 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn -
1119 ntohl(th->seq) + 1);
1123 if (!th->syn || th->ack ||
1124 CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
1127 /* Reopened connection - reset the sequence number and timestamp
1128 * adjustments, they will get initialized once the connection is
1131 nf_ct_seqadj_init(ct, ctinfo, 0);
1132 synproxy->tsoff = 0;
1133 this_cpu_inc(snet->stats->conn_reopened);
1135 case TCP_CONNTRACK_SYN_SENT:
1136 if (!synproxy_parse_options(skb, thoff, th, &opts))
1139 if (!th->syn && th->ack &&
1140 CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
1141 /* Keep-Alives are sent with SEG.SEQ = SND.NXT-1,
1142 * therefore we need to add 1 to make the SYN sequence
1143 * number match the one of first SYN.
1145 if (synproxy_recv_client_ack_ipv6(net, skb, th, &opts,
1146 ntohl(th->seq) + 1)) {
1147 this_cpu_inc(snet->stats->cookie_retrans);
1155 synproxy->isn = ntohl(th->ack_seq);
1156 if (opts.options & NF_SYNPROXY_OPT_TIMESTAMP)
1157 synproxy->its = opts.tsecr;
1159 nf_conntrack_event_cache(IPCT_SYNPROXY, ct);
1161 case TCP_CONNTRACK_SYN_RECV:
1162 if (!th->syn || !th->ack)
1165 if (!synproxy_parse_options(skb, thoff, th, &opts))
1168 if (opts.options & NF_SYNPROXY_OPT_TIMESTAMP) {
1169 synproxy->tsoff = opts.tsval - synproxy->its;
1170 nf_conntrack_event_cache(IPCT_SYNPROXY, ct);
1173 opts.options &= ~(NF_SYNPROXY_OPT_MSS |
1174 NF_SYNPROXY_OPT_WSCALE |
1175 NF_SYNPROXY_OPT_SACK_PERM);
1177 swap(opts.tsval, opts.tsecr);
1178 synproxy_send_server_ack_ipv6(net, state, skb, th, &opts);
1180 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
1181 nf_conntrack_event_cache(IPCT_SEQADJ, ct);
1183 swap(opts.tsval, opts.tsecr);
1184 synproxy_send_client_ack_ipv6(net, skb, th, &opts);
1192 synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy);
1195 EXPORT_SYMBOL_GPL(ipv6_synproxy_hook);
1197 static const struct nf_hook_ops ipv6_synproxy_ops[] = {
1199 .hook = ipv6_synproxy_hook,
1201 .hooknum = NF_INET_LOCAL_IN,
1202 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
1205 .hook = ipv6_synproxy_hook,
1207 .hooknum = NF_INET_POST_ROUTING,
1208 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
1213 nf_synproxy_ipv6_init(struct synproxy_net *snet, struct net *net)
1217 if (snet->hook_ref6 == 0) {
1218 err = nf_register_net_hooks(net, ipv6_synproxy_ops,
1219 ARRAY_SIZE(ipv6_synproxy_ops));
1227 EXPORT_SYMBOL_GPL(nf_synproxy_ipv6_init);
1230 nf_synproxy_ipv6_fini(struct synproxy_net *snet, struct net *net)
1233 if (snet->hook_ref6 == 0)
1234 nf_unregister_net_hooks(net, ipv6_synproxy_ops,
1235 ARRAY_SIZE(ipv6_synproxy_ops));
1237 EXPORT_SYMBOL_GPL(nf_synproxy_ipv6_fini);
1238 #endif /* CONFIG_IPV6 */
1240 MODULE_LICENSE("GPL");
1241 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1242 MODULE_DESCRIPTION("nftables SYNPROXY expression support");