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 ptr = skb_header_pointer(skb, doff + sizeof(*th), length, buf);
58 if (opsize == TCPOLEN_MSS) {
59 opts->mss_option = get_unaligned_be16(ptr);
60 opts->options |= NF_SYNPROXY_OPT_MSS;
64 if (opsize == TCPOLEN_WINDOW) {
66 if (opts->wscale > TCP_MAX_WSCALE)
67 opts->wscale = TCP_MAX_WSCALE;
68 opts->options |= NF_SYNPROXY_OPT_WSCALE;
71 case TCPOPT_TIMESTAMP:
72 if (opsize == TCPOLEN_TIMESTAMP) {
73 opts->tsval = get_unaligned_be32(ptr);
74 opts->tsecr = get_unaligned_be32(ptr + 4);
75 opts->options |= NF_SYNPROXY_OPT_TIMESTAMP;
78 case TCPOPT_SACK_PERM:
79 if (opsize == TCPOLEN_SACK_PERM)
80 opts->options |= NF_SYNPROXY_OPT_SACK_PERM;
90 EXPORT_SYMBOL_GPL(synproxy_parse_options);
93 synproxy_options_size(const struct synproxy_options *opts)
95 unsigned int size = 0;
97 if (opts->options & NF_SYNPROXY_OPT_MSS)
98 size += TCPOLEN_MSS_ALIGNED;
99 if (opts->options & NF_SYNPROXY_OPT_TIMESTAMP)
100 size += TCPOLEN_TSTAMP_ALIGNED;
101 else if (opts->options & NF_SYNPROXY_OPT_SACK_PERM)
102 size += TCPOLEN_SACKPERM_ALIGNED;
103 if (opts->options & NF_SYNPROXY_OPT_WSCALE)
104 size += TCPOLEN_WSCALE_ALIGNED;
110 synproxy_build_options(struct tcphdr *th, const struct synproxy_options *opts)
112 __be32 *ptr = (__be32 *)(th + 1);
113 u8 options = opts->options;
115 if (options & NF_SYNPROXY_OPT_MSS)
116 *ptr++ = htonl((TCPOPT_MSS << 24) |
117 (TCPOLEN_MSS << 16) |
120 if (options & NF_SYNPROXY_OPT_TIMESTAMP) {
121 if (options & NF_SYNPROXY_OPT_SACK_PERM)
122 *ptr++ = htonl((TCPOPT_SACK_PERM << 24) |
123 (TCPOLEN_SACK_PERM << 16) |
124 (TCPOPT_TIMESTAMP << 8) |
127 *ptr++ = htonl((TCPOPT_NOP << 24) |
129 (TCPOPT_TIMESTAMP << 8) |
132 *ptr++ = htonl(opts->tsval);
133 *ptr++ = htonl(opts->tsecr);
134 } else if (options & NF_SYNPROXY_OPT_SACK_PERM)
135 *ptr++ = htonl((TCPOPT_NOP << 24) |
137 (TCPOPT_SACK_PERM << 8) |
140 if (options & NF_SYNPROXY_OPT_WSCALE)
141 *ptr++ = htonl((TCPOPT_NOP << 24) |
142 (TCPOPT_WINDOW << 16) |
143 (TCPOLEN_WINDOW << 8) |
147 void synproxy_init_timestamp_cookie(const struct nf_synproxy_info *info,
148 struct synproxy_options *opts)
150 opts->tsecr = opts->tsval;
151 opts->tsval = tcp_time_stamp_raw() & ~0x3f;
153 if (opts->options & NF_SYNPROXY_OPT_WSCALE) {
154 opts->tsval |= opts->wscale;
155 opts->wscale = info->wscale;
159 if (opts->options & NF_SYNPROXY_OPT_SACK_PERM)
160 opts->tsval |= 1 << 4;
162 if (opts->options & NF_SYNPROXY_OPT_ECN)
163 opts->tsval |= 1 << 5;
165 EXPORT_SYMBOL_GPL(synproxy_init_timestamp_cookie);
168 synproxy_check_timestamp_cookie(struct synproxy_options *opts)
170 opts->wscale = opts->tsecr & 0xf;
171 if (opts->wscale != 0xf)
172 opts->options |= NF_SYNPROXY_OPT_WSCALE;
174 opts->options |= opts->tsecr & (1 << 4) ? NF_SYNPROXY_OPT_SACK_PERM : 0;
176 opts->options |= opts->tsecr & (1 << 5) ? NF_SYNPROXY_OPT_ECN : 0;
180 synproxy_tstamp_adjust(struct sk_buff *skb, unsigned int protoff,
181 struct tcphdr *th, struct nf_conn *ct,
182 enum ip_conntrack_info ctinfo,
183 const struct nf_conn_synproxy *synproxy)
185 unsigned int optoff, optend;
188 if (synproxy->tsoff == 0)
191 optoff = protoff + sizeof(struct tcphdr);
192 optend = protoff + th->doff * 4;
194 if (skb_ensure_writable(skb, optend))
197 while (optoff < optend) {
198 unsigned char *op = skb->data + optoff;
207 if (optoff + 1 == optend ||
208 optoff + op[1] > optend ||
211 if (op[0] == TCPOPT_TIMESTAMP &&
212 op[1] == TCPOLEN_TIMESTAMP) {
213 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
214 ptr = (__be32 *)&op[2];
216 *ptr = htonl(ntohl(*ptr) -
219 ptr = (__be32 *)&op[6];
221 *ptr = htonl(ntohl(*ptr) +
224 inet_proto_csum_replace4(&th->check, skb,
234 static struct nf_ct_ext_type nf_ct_synproxy_extend __read_mostly = {
235 .len = sizeof(struct nf_conn_synproxy),
236 .align = __alignof__(struct nf_conn_synproxy),
237 .id = NF_CT_EXT_SYNPROXY,
240 #ifdef CONFIG_PROC_FS
241 static void *synproxy_cpu_seq_start(struct seq_file *seq, loff_t *pos)
243 struct synproxy_net *snet = synproxy_pernet(seq_file_net(seq));
247 return SEQ_START_TOKEN;
249 for (cpu = *pos - 1; cpu < nr_cpu_ids; cpu++) {
250 if (!cpu_possible(cpu))
253 return per_cpu_ptr(snet->stats, cpu);
259 static void *synproxy_cpu_seq_next(struct seq_file *seq, void *v, loff_t *pos)
261 struct synproxy_net *snet = synproxy_pernet(seq_file_net(seq));
264 for (cpu = *pos; cpu < nr_cpu_ids; cpu++) {
265 if (!cpu_possible(cpu))
268 return per_cpu_ptr(snet->stats, cpu);
274 static void synproxy_cpu_seq_stop(struct seq_file *seq, void *v)
279 static int synproxy_cpu_seq_show(struct seq_file *seq, void *v)
281 struct synproxy_stats *stats = v;
283 if (v == SEQ_START_TOKEN) {
284 seq_puts(seq, "entries\t\tsyn_received\t"
285 "cookie_invalid\tcookie_valid\t"
286 "cookie_retrans\tconn_reopened\n");
290 seq_printf(seq, "%08x\t%08x\t%08x\t%08x\t%08x\t%08x\n", 0,
292 stats->cookie_invalid,
294 stats->cookie_retrans,
295 stats->conn_reopened);
300 static const struct seq_operations synproxy_cpu_seq_ops = {
301 .start = synproxy_cpu_seq_start,
302 .next = synproxy_cpu_seq_next,
303 .stop = synproxy_cpu_seq_stop,
304 .show = synproxy_cpu_seq_show,
307 static int __net_init synproxy_proc_init(struct net *net)
309 if (!proc_create_net("synproxy", 0444, net->proc_net_stat,
310 &synproxy_cpu_seq_ops, sizeof(struct seq_net_private)))
315 static void __net_exit synproxy_proc_exit(struct net *net)
317 remove_proc_entry("synproxy", net->proc_net_stat);
320 static int __net_init synproxy_proc_init(struct net *net)
325 static void __net_exit synproxy_proc_exit(struct net *net)
329 #endif /* CONFIG_PROC_FS */
331 static int __net_init synproxy_net_init(struct net *net)
333 struct synproxy_net *snet = synproxy_pernet(net);
337 ct = nf_ct_tmpl_alloc(net, &nf_ct_zone_dflt, GFP_KERNEL);
341 if (!nfct_seqadj_ext_add(ct))
343 if (!nfct_synproxy_ext_add(ct))
346 __set_bit(IPS_CONFIRMED_BIT, &ct->status);
347 nf_conntrack_get(&ct->ct_general);
350 snet->stats = alloc_percpu(struct synproxy_stats);
351 if (snet->stats == NULL)
354 err = synproxy_proc_init(net);
361 free_percpu(snet->stats);
368 static void __net_exit synproxy_net_exit(struct net *net)
370 struct synproxy_net *snet = synproxy_pernet(net);
372 nf_ct_put(snet->tmpl);
373 synproxy_proc_exit(net);
374 free_percpu(snet->stats);
377 static struct pernet_operations synproxy_net_ops = {
378 .init = synproxy_net_init,
379 .exit = synproxy_net_exit,
380 .id = &synproxy_net_id,
381 .size = sizeof(struct synproxy_net),
384 static int __init synproxy_core_init(void)
388 err = nf_ct_extend_register(&nf_ct_synproxy_extend);
392 err = register_pernet_subsys(&synproxy_net_ops);
399 nf_ct_extend_unregister(&nf_ct_synproxy_extend);
404 static void __exit synproxy_core_exit(void)
406 unregister_pernet_subsys(&synproxy_net_ops);
407 nf_ct_extend_unregister(&nf_ct_synproxy_extend);
410 module_init(synproxy_core_init);
411 module_exit(synproxy_core_exit);
413 static struct iphdr *
414 synproxy_build_ip(struct net *net, struct sk_buff *skb, __be32 saddr,
419 skb_reset_network_header(skb);
420 iph = skb_put(skb, sizeof(*iph));
422 iph->ihl = sizeof(*iph) / 4;
425 iph->frag_off = htons(IP_DF);
426 iph->ttl = net->ipv4.sysctl_ip_default_ttl;
427 iph->protocol = IPPROTO_TCP;
436 synproxy_send_tcp(struct net *net,
437 const struct sk_buff *skb, struct sk_buff *nskb,
438 struct nf_conntrack *nfct, enum ip_conntrack_info ctinfo,
439 struct iphdr *niph, struct tcphdr *nth,
440 unsigned int tcp_hdr_size)
442 nth->check = ~tcp_v4_check(tcp_hdr_size, niph->saddr, niph->daddr, 0);
443 nskb->ip_summed = CHECKSUM_PARTIAL;
444 nskb->csum_start = (unsigned char *)nth - nskb->head;
445 nskb->csum_offset = offsetof(struct tcphdr, check);
447 skb_dst_set_noref(nskb, skb_dst(skb));
448 nskb->protocol = htons(ETH_P_IP);
449 if (ip_route_me_harder(net, nskb, RTN_UNSPEC))
453 nf_ct_set(nskb, (struct nf_conn *)nfct, ctinfo);
454 nf_conntrack_get(nfct);
457 ip_local_out(net, nskb->sk, nskb);
465 synproxy_send_client_synack(struct net *net,
466 const struct sk_buff *skb, const struct tcphdr *th,
467 const struct synproxy_options *opts)
469 struct sk_buff *nskb;
470 struct iphdr *iph, *niph;
472 unsigned int tcp_hdr_size;
473 u16 mss = opts->mss_encode;
477 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
478 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
482 skb_reserve(nskb, MAX_TCP_HEADER);
484 niph = synproxy_build_ip(net, nskb, iph->daddr, iph->saddr);
486 skb_reset_transport_header(nskb);
487 nth = skb_put(nskb, tcp_hdr_size);
488 nth->source = th->dest;
489 nth->dest = th->source;
490 nth->seq = htonl(__cookie_v4_init_sequence(iph, th, &mss));
491 nth->ack_seq = htonl(ntohl(th->seq) + 1);
492 tcp_flag_word(nth) = TCP_FLAG_SYN | TCP_FLAG_ACK;
493 if (opts->options & NF_SYNPROXY_OPT_ECN)
494 tcp_flag_word(nth) |= TCP_FLAG_ECE;
495 nth->doff = tcp_hdr_size / 4;
500 synproxy_build_options(nth, opts);
502 synproxy_send_tcp(net, skb, nskb, skb_nfct(skb),
503 IP_CT_ESTABLISHED_REPLY, niph, nth, tcp_hdr_size);
505 EXPORT_SYMBOL_GPL(synproxy_send_client_synack);
508 synproxy_send_server_syn(struct net *net,
509 const struct sk_buff *skb, const struct tcphdr *th,
510 const struct synproxy_options *opts, u32 recv_seq)
512 struct synproxy_net *snet = synproxy_pernet(net);
513 struct sk_buff *nskb;
514 struct iphdr *iph, *niph;
516 unsigned int tcp_hdr_size;
520 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
521 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
525 skb_reserve(nskb, MAX_TCP_HEADER);
527 niph = synproxy_build_ip(net, nskb, iph->saddr, iph->daddr);
529 skb_reset_transport_header(nskb);
530 nth = skb_put(nskb, tcp_hdr_size);
531 nth->source = th->source;
532 nth->dest = th->dest;
533 nth->seq = htonl(recv_seq - 1);
534 /* ack_seq is used to relay our ISN to the synproxy hook to initialize
535 * sequence number translation once a connection tracking entry exists.
537 nth->ack_seq = htonl(ntohl(th->ack_seq) - 1);
538 tcp_flag_word(nth) = TCP_FLAG_SYN;
539 if (opts->options & NF_SYNPROXY_OPT_ECN)
540 tcp_flag_word(nth) |= TCP_FLAG_ECE | TCP_FLAG_CWR;
541 nth->doff = tcp_hdr_size / 4;
542 nth->window = th->window;
546 synproxy_build_options(nth, opts);
548 synproxy_send_tcp(net, skb, nskb, &snet->tmpl->ct_general, IP_CT_NEW,
549 niph, nth, tcp_hdr_size);
553 synproxy_send_server_ack(struct net *net,
554 const struct ip_ct_tcp *state,
555 const struct sk_buff *skb, const struct tcphdr *th,
556 const struct synproxy_options *opts)
558 struct sk_buff *nskb;
559 struct iphdr *iph, *niph;
561 unsigned int tcp_hdr_size;
565 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
566 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
570 skb_reserve(nskb, MAX_TCP_HEADER);
572 niph = synproxy_build_ip(net, nskb, iph->daddr, iph->saddr);
574 skb_reset_transport_header(nskb);
575 nth = skb_put(nskb, tcp_hdr_size);
576 nth->source = th->dest;
577 nth->dest = th->source;
578 nth->seq = htonl(ntohl(th->ack_seq));
579 nth->ack_seq = htonl(ntohl(th->seq) + 1);
580 tcp_flag_word(nth) = TCP_FLAG_ACK;
581 nth->doff = tcp_hdr_size / 4;
582 nth->window = htons(state->seen[IP_CT_DIR_ORIGINAL].td_maxwin);
586 synproxy_build_options(nth, opts);
588 synproxy_send_tcp(net, skb, nskb, NULL, 0, niph, nth, tcp_hdr_size);
592 synproxy_send_client_ack(struct net *net,
593 const struct sk_buff *skb, const struct tcphdr *th,
594 const struct synproxy_options *opts)
596 struct sk_buff *nskb;
597 struct iphdr *iph, *niph;
599 unsigned int tcp_hdr_size;
603 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
604 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
608 skb_reserve(nskb, MAX_TCP_HEADER);
610 niph = synproxy_build_ip(net, nskb, iph->saddr, iph->daddr);
612 skb_reset_transport_header(nskb);
613 nth = skb_put(nskb, tcp_hdr_size);
614 nth->source = th->source;
615 nth->dest = th->dest;
616 nth->seq = htonl(ntohl(th->seq) + 1);
617 nth->ack_seq = th->ack_seq;
618 tcp_flag_word(nth) = TCP_FLAG_ACK;
619 nth->doff = tcp_hdr_size / 4;
620 nth->window = htons(ntohs(th->window) >> opts->wscale);
624 synproxy_build_options(nth, opts);
626 synproxy_send_tcp(net, skb, nskb, skb_nfct(skb),
627 IP_CT_ESTABLISHED_REPLY, niph, nth, tcp_hdr_size);
631 synproxy_recv_client_ack(struct net *net,
632 const struct sk_buff *skb, const struct tcphdr *th,
633 struct synproxy_options *opts, u32 recv_seq)
635 struct synproxy_net *snet = synproxy_pernet(net);
638 mss = __cookie_v4_check(ip_hdr(skb), th, ntohl(th->ack_seq) - 1);
640 this_cpu_inc(snet->stats->cookie_invalid);
644 this_cpu_inc(snet->stats->cookie_valid);
645 opts->mss_option = mss;
646 opts->options |= NF_SYNPROXY_OPT_MSS;
648 if (opts->options & NF_SYNPROXY_OPT_TIMESTAMP)
649 synproxy_check_timestamp_cookie(opts);
651 synproxy_send_server_syn(net, skb, th, opts, recv_seq);
654 EXPORT_SYMBOL_GPL(synproxy_recv_client_ack);
657 ipv4_synproxy_hook(void *priv, struct sk_buff *skb,
658 const struct nf_hook_state *nhs)
660 struct net *net = nhs->net;
661 struct synproxy_net *snet = synproxy_pernet(net);
662 enum ip_conntrack_info ctinfo;
664 struct nf_conn_synproxy *synproxy;
665 struct synproxy_options opts = {};
666 const struct ip_ct_tcp *state;
667 struct tcphdr *th, _th;
670 ct = nf_ct_get(skb, &ctinfo);
674 synproxy = nfct_synproxy(ct);
678 if (nf_is_loopback_packet(skb) ||
679 ip_hdr(skb)->protocol != IPPROTO_TCP)
682 thoff = ip_hdrlen(skb);
683 th = skb_header_pointer(skb, thoff, sizeof(_th), &_th);
687 state = &ct->proto.tcp;
688 switch (state->state) {
689 case TCP_CONNTRACK_CLOSE:
690 if (th->rst && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
691 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn -
696 if (!th->syn || th->ack ||
697 CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
700 /* Reopened connection - reset the sequence number and timestamp
701 * adjustments, they will get initialized once the connection is
704 nf_ct_seqadj_init(ct, ctinfo, 0);
706 this_cpu_inc(snet->stats->conn_reopened);
709 case TCP_CONNTRACK_SYN_SENT:
710 if (!synproxy_parse_options(skb, thoff, th, &opts))
713 if (!th->syn && th->ack &&
714 CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
715 /* Keep-Alives are sent with SEG.SEQ = SND.NXT-1,
716 * therefore we need to add 1 to make the SYN sequence
717 * number match the one of first SYN.
719 if (synproxy_recv_client_ack(net, skb, th, &opts,
720 ntohl(th->seq) + 1)) {
721 this_cpu_inc(snet->stats->cookie_retrans);
729 synproxy->isn = ntohl(th->ack_seq);
730 if (opts.options & NF_SYNPROXY_OPT_TIMESTAMP)
731 synproxy->its = opts.tsecr;
733 nf_conntrack_event_cache(IPCT_SYNPROXY, ct);
735 case TCP_CONNTRACK_SYN_RECV:
736 if (!th->syn || !th->ack)
739 if (!synproxy_parse_options(skb, thoff, th, &opts))
742 if (opts.options & NF_SYNPROXY_OPT_TIMESTAMP) {
743 synproxy->tsoff = opts.tsval - synproxy->its;
744 nf_conntrack_event_cache(IPCT_SYNPROXY, ct);
747 opts.options &= ~(NF_SYNPROXY_OPT_MSS |
748 NF_SYNPROXY_OPT_WSCALE |
749 NF_SYNPROXY_OPT_SACK_PERM);
751 swap(opts.tsval, opts.tsecr);
752 synproxy_send_server_ack(net, state, skb, th, &opts);
754 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
755 nf_conntrack_event_cache(IPCT_SEQADJ, ct);
757 swap(opts.tsval, opts.tsecr);
758 synproxy_send_client_ack(net, skb, th, &opts);
766 synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy);
769 EXPORT_SYMBOL_GPL(ipv4_synproxy_hook);
771 static const struct nf_hook_ops ipv4_synproxy_ops[] = {
773 .hook = ipv4_synproxy_hook,
775 .hooknum = NF_INET_LOCAL_IN,
776 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
779 .hook = ipv4_synproxy_hook,
781 .hooknum = NF_INET_POST_ROUTING,
782 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
786 int nf_synproxy_ipv4_init(struct synproxy_net *snet, struct net *net)
790 if (snet->hook_ref4 == 0) {
791 err = nf_register_net_hooks(net, ipv4_synproxy_ops,
792 ARRAY_SIZE(ipv4_synproxy_ops));
800 EXPORT_SYMBOL_GPL(nf_synproxy_ipv4_init);
802 void nf_synproxy_ipv4_fini(struct synproxy_net *snet, struct net *net)
805 if (snet->hook_ref4 == 0)
806 nf_unregister_net_hooks(net, ipv4_synproxy_ops,
807 ARRAY_SIZE(ipv4_synproxy_ops));
809 EXPORT_SYMBOL_GPL(nf_synproxy_ipv4_fini);
811 #if IS_ENABLED(CONFIG_IPV6)
812 static struct ipv6hdr *
813 synproxy_build_ip_ipv6(struct net *net, struct sk_buff *skb,
814 const struct in6_addr *saddr,
815 const struct in6_addr *daddr)
819 skb_reset_network_header(skb);
820 iph = skb_put(skb, sizeof(*iph));
821 ip6_flow_hdr(iph, 0, 0);
822 iph->hop_limit = net->ipv6.devconf_all->hop_limit;
823 iph->nexthdr = IPPROTO_TCP;
831 synproxy_send_tcp_ipv6(struct net *net,
832 const struct sk_buff *skb, struct sk_buff *nskb,
833 struct nf_conntrack *nfct, enum ip_conntrack_info ctinfo,
834 struct ipv6hdr *niph, struct tcphdr *nth,
835 unsigned int tcp_hdr_size)
837 struct dst_entry *dst;
841 nth->check = ~tcp_v6_check(tcp_hdr_size, &niph->saddr, &niph->daddr, 0);
842 nskb->ip_summed = CHECKSUM_PARTIAL;
843 nskb->csum_start = (unsigned char *)nth - nskb->head;
844 nskb->csum_offset = offsetof(struct tcphdr, check);
846 memset(&fl6, 0, sizeof(fl6));
847 fl6.flowi6_proto = IPPROTO_TCP;
848 fl6.saddr = niph->saddr;
849 fl6.daddr = niph->daddr;
850 fl6.fl6_sport = nth->source;
851 fl6.fl6_dport = nth->dest;
852 security_skb_classify_flow((struct sk_buff *)skb,
853 flowi6_to_flowi(&fl6));
854 err = nf_ip6_route(net, &dst, flowi6_to_flowi(&fl6), false);
859 dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
863 skb_dst_set(nskb, dst);
866 nf_ct_set(nskb, (struct nf_conn *)nfct, ctinfo);
867 nf_conntrack_get(nfct);
870 ip6_local_out(net, nskb->sk, nskb);
878 synproxy_send_client_synack_ipv6(struct net *net,
879 const struct sk_buff *skb,
880 const struct tcphdr *th,
881 const struct synproxy_options *opts)
883 struct sk_buff *nskb;
884 struct ipv6hdr *iph, *niph;
886 unsigned int tcp_hdr_size;
887 u16 mss = opts->mss_encode;
891 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
892 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
896 skb_reserve(nskb, MAX_TCP_HEADER);
898 niph = synproxy_build_ip_ipv6(net, nskb, &iph->daddr, &iph->saddr);
900 skb_reset_transport_header(nskb);
901 nth = skb_put(nskb, tcp_hdr_size);
902 nth->source = th->dest;
903 nth->dest = th->source;
904 nth->seq = htonl(nf_ipv6_cookie_init_sequence(iph, th, &mss));
905 nth->ack_seq = htonl(ntohl(th->seq) + 1);
906 tcp_flag_word(nth) = TCP_FLAG_SYN | TCP_FLAG_ACK;
907 if (opts->options & NF_SYNPROXY_OPT_ECN)
908 tcp_flag_word(nth) |= TCP_FLAG_ECE;
909 nth->doff = tcp_hdr_size / 4;
914 synproxy_build_options(nth, opts);
916 synproxy_send_tcp_ipv6(net, skb, nskb, skb_nfct(skb),
917 IP_CT_ESTABLISHED_REPLY, niph, nth,
920 EXPORT_SYMBOL_GPL(synproxy_send_client_synack_ipv6);
923 synproxy_send_server_syn_ipv6(struct net *net, const struct sk_buff *skb,
924 const struct tcphdr *th,
925 const struct synproxy_options *opts, u32 recv_seq)
927 struct synproxy_net *snet = synproxy_pernet(net);
928 struct sk_buff *nskb;
929 struct ipv6hdr *iph, *niph;
931 unsigned int tcp_hdr_size;
935 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
936 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
940 skb_reserve(nskb, MAX_TCP_HEADER);
942 niph = synproxy_build_ip_ipv6(net, nskb, &iph->saddr, &iph->daddr);
944 skb_reset_transport_header(nskb);
945 nth = skb_put(nskb, tcp_hdr_size);
946 nth->source = th->source;
947 nth->dest = th->dest;
948 nth->seq = htonl(recv_seq - 1);
949 /* ack_seq is used to relay our ISN to the synproxy hook to initialize
950 * sequence number translation once a connection tracking entry exists.
952 nth->ack_seq = htonl(ntohl(th->ack_seq) - 1);
953 tcp_flag_word(nth) = TCP_FLAG_SYN;
954 if (opts->options & NF_SYNPROXY_OPT_ECN)
955 tcp_flag_word(nth) |= TCP_FLAG_ECE | TCP_FLAG_CWR;
956 nth->doff = tcp_hdr_size / 4;
957 nth->window = th->window;
961 synproxy_build_options(nth, opts);
963 synproxy_send_tcp_ipv6(net, skb, nskb, &snet->tmpl->ct_general,
964 IP_CT_NEW, niph, nth, tcp_hdr_size);
968 synproxy_send_server_ack_ipv6(struct net *net, const struct ip_ct_tcp *state,
969 const struct sk_buff *skb,
970 const struct tcphdr *th,
971 const struct synproxy_options *opts)
973 struct sk_buff *nskb;
974 struct ipv6hdr *iph, *niph;
976 unsigned int tcp_hdr_size;
980 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
981 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
985 skb_reserve(nskb, MAX_TCP_HEADER);
987 niph = synproxy_build_ip_ipv6(net, nskb, &iph->daddr, &iph->saddr);
989 skb_reset_transport_header(nskb);
990 nth = skb_put(nskb, tcp_hdr_size);
991 nth->source = th->dest;
992 nth->dest = th->source;
993 nth->seq = htonl(ntohl(th->ack_seq));
994 nth->ack_seq = htonl(ntohl(th->seq) + 1);
995 tcp_flag_word(nth) = TCP_FLAG_ACK;
996 nth->doff = tcp_hdr_size / 4;
997 nth->window = htons(state->seen[IP_CT_DIR_ORIGINAL].td_maxwin);
1001 synproxy_build_options(nth, opts);
1003 synproxy_send_tcp_ipv6(net, skb, nskb, NULL, 0, niph, nth,
1008 synproxy_send_client_ack_ipv6(struct net *net, const struct sk_buff *skb,
1009 const struct tcphdr *th,
1010 const struct synproxy_options *opts)
1012 struct sk_buff *nskb;
1013 struct ipv6hdr *iph, *niph;
1015 unsigned int tcp_hdr_size;
1017 iph = ipv6_hdr(skb);
1019 tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts);
1020 nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER,
1024 skb_reserve(nskb, MAX_TCP_HEADER);
1026 niph = synproxy_build_ip_ipv6(net, nskb, &iph->saddr, &iph->daddr);
1028 skb_reset_transport_header(nskb);
1029 nth = skb_put(nskb, tcp_hdr_size);
1030 nth->source = th->source;
1031 nth->dest = th->dest;
1032 nth->seq = htonl(ntohl(th->seq) + 1);
1033 nth->ack_seq = th->ack_seq;
1034 tcp_flag_word(nth) = TCP_FLAG_ACK;
1035 nth->doff = tcp_hdr_size / 4;
1036 nth->window = htons(ntohs(th->window) >> opts->wscale);
1040 synproxy_build_options(nth, opts);
1042 synproxy_send_tcp_ipv6(net, skb, nskb, skb_nfct(skb),
1043 IP_CT_ESTABLISHED_REPLY, niph, nth,
1048 synproxy_recv_client_ack_ipv6(struct net *net,
1049 const struct sk_buff *skb,
1050 const struct tcphdr *th,
1051 struct synproxy_options *opts, u32 recv_seq)
1053 struct synproxy_net *snet = synproxy_pernet(net);
1056 mss = nf_cookie_v6_check(ipv6_hdr(skb), th, ntohl(th->ack_seq) - 1);
1058 this_cpu_inc(snet->stats->cookie_invalid);
1062 this_cpu_inc(snet->stats->cookie_valid);
1063 opts->mss_option = mss;
1064 opts->options |= NF_SYNPROXY_OPT_MSS;
1066 if (opts->options & NF_SYNPROXY_OPT_TIMESTAMP)
1067 synproxy_check_timestamp_cookie(opts);
1069 synproxy_send_server_syn_ipv6(net, skb, th, opts, recv_seq);
1072 EXPORT_SYMBOL_GPL(synproxy_recv_client_ack_ipv6);
1075 ipv6_synproxy_hook(void *priv, struct sk_buff *skb,
1076 const struct nf_hook_state *nhs)
1078 struct net *net = nhs->net;
1079 struct synproxy_net *snet = synproxy_pernet(net);
1080 enum ip_conntrack_info ctinfo;
1082 struct nf_conn_synproxy *synproxy;
1083 struct synproxy_options opts = {};
1084 const struct ip_ct_tcp *state;
1085 struct tcphdr *th, _th;
1090 ct = nf_ct_get(skb, &ctinfo);
1094 synproxy = nfct_synproxy(ct);
1098 if (nf_is_loopback_packet(skb))
1101 nexthdr = ipv6_hdr(skb)->nexthdr;
1102 thoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
1104 if (thoff < 0 || nexthdr != IPPROTO_TCP)
1107 th = skb_header_pointer(skb, thoff, sizeof(_th), &_th);
1111 state = &ct->proto.tcp;
1112 switch (state->state) {
1113 case TCP_CONNTRACK_CLOSE:
1114 if (th->rst && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
1115 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn -
1116 ntohl(th->seq) + 1);
1120 if (!th->syn || th->ack ||
1121 CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
1124 /* Reopened connection - reset the sequence number and timestamp
1125 * adjustments, they will get initialized once the connection is
1128 nf_ct_seqadj_init(ct, ctinfo, 0);
1129 synproxy->tsoff = 0;
1130 this_cpu_inc(snet->stats->conn_reopened);
1133 case TCP_CONNTRACK_SYN_SENT:
1134 if (!synproxy_parse_options(skb, thoff, th, &opts))
1137 if (!th->syn && th->ack &&
1138 CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
1139 /* Keep-Alives are sent with SEG.SEQ = SND.NXT-1,
1140 * therefore we need to add 1 to make the SYN sequence
1141 * number match the one of first SYN.
1143 if (synproxy_recv_client_ack_ipv6(net, skb, th, &opts,
1144 ntohl(th->seq) + 1)) {
1145 this_cpu_inc(snet->stats->cookie_retrans);
1153 synproxy->isn = ntohl(th->ack_seq);
1154 if (opts.options & NF_SYNPROXY_OPT_TIMESTAMP)
1155 synproxy->its = opts.tsecr;
1157 nf_conntrack_event_cache(IPCT_SYNPROXY, ct);
1159 case TCP_CONNTRACK_SYN_RECV:
1160 if (!th->syn || !th->ack)
1163 if (!synproxy_parse_options(skb, thoff, th, &opts))
1166 if (opts.options & NF_SYNPROXY_OPT_TIMESTAMP) {
1167 synproxy->tsoff = opts.tsval - synproxy->its;
1168 nf_conntrack_event_cache(IPCT_SYNPROXY, ct);
1171 opts.options &= ~(NF_SYNPROXY_OPT_MSS |
1172 NF_SYNPROXY_OPT_WSCALE |
1173 NF_SYNPROXY_OPT_SACK_PERM);
1175 swap(opts.tsval, opts.tsecr);
1176 synproxy_send_server_ack_ipv6(net, state, skb, th, &opts);
1178 nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq));
1179 nf_conntrack_event_cache(IPCT_SEQADJ, ct);
1181 swap(opts.tsval, opts.tsecr);
1182 synproxy_send_client_ack_ipv6(net, skb, th, &opts);
1190 synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy);
1193 EXPORT_SYMBOL_GPL(ipv6_synproxy_hook);
1195 static const struct nf_hook_ops ipv6_synproxy_ops[] = {
1197 .hook = ipv6_synproxy_hook,
1199 .hooknum = NF_INET_LOCAL_IN,
1200 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
1203 .hook = ipv6_synproxy_hook,
1205 .hooknum = NF_INET_POST_ROUTING,
1206 .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1,
1211 nf_synproxy_ipv6_init(struct synproxy_net *snet, struct net *net)
1215 if (snet->hook_ref6 == 0) {
1216 err = nf_register_net_hooks(net, ipv6_synproxy_ops,
1217 ARRAY_SIZE(ipv6_synproxy_ops));
1225 EXPORT_SYMBOL_GPL(nf_synproxy_ipv6_init);
1228 nf_synproxy_ipv6_fini(struct synproxy_net *snet, struct net *net)
1231 if (snet->hook_ref6 == 0)
1232 nf_unregister_net_hooks(net, ipv6_synproxy_ops,
1233 ARRAY_SIZE(ipv6_synproxy_ops));
1235 EXPORT_SYMBOL_GPL(nf_synproxy_ipv6_fini);
1236 #endif /* CONFIG_IPV6 */
1238 MODULE_LICENSE("GPL");
1239 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
1240 MODULE_DESCRIPTION("nftables SYNPROXY expression support");