Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 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/ip6_checksum.h> |
| 12 | #include <net/ip6_route.h> |
| 13 | #include <net/tcp.h> |
| 14 | |
| 15 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 16 | #include <linux/netfilter/x_tables.h> |
| 17 | #include <linux/netfilter/xt_SYNPROXY.h> |
| 18 | #include <net/netfilter/nf_conntrack.h> |
| 19 | #include <net/netfilter/nf_conntrack_seqadj.h> |
| 20 | #include <net/netfilter/nf_conntrack_synproxy.h> |
| 21 | |
| 22 | static struct ipv6hdr * |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 23 | synproxy_build_ip(struct net *net, struct sk_buff *skb, |
| 24 | const struct in6_addr *saddr, |
| 25 | const struct in6_addr *daddr) |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 26 | { |
| 27 | struct ipv6hdr *iph; |
| 28 | |
| 29 | skb_reset_network_header(skb); |
| 30 | iph = (struct ipv6hdr *)skb_put(skb, sizeof(*iph)); |
| 31 | ip6_flow_hdr(iph, 0, 0); |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 32 | iph->hop_limit = net->ipv6.devconf_all->hop_limit; |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 33 | iph->nexthdr = IPPROTO_TCP; |
| 34 | iph->saddr = *saddr; |
| 35 | iph->daddr = *daddr; |
| 36 | |
| 37 | return iph; |
| 38 | } |
| 39 | |
| 40 | static void |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 41 | synproxy_send_tcp(struct net *net, |
Phil Sutter | 96fffb4 | 2015-08-09 13:14:15 +0200 | [diff] [blame] | 42 | const struct sk_buff *skb, struct sk_buff *nskb, |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 43 | struct nf_conntrack *nfct, enum ip_conntrack_info ctinfo, |
| 44 | struct ipv6hdr *niph, struct tcphdr *nth, |
| 45 | unsigned int tcp_hdr_size) |
| 46 | { |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 47 | struct dst_entry *dst; |
| 48 | struct flowi6 fl6; |
| 49 | |
| 50 | nth->check = ~tcp_v6_check(tcp_hdr_size, &niph->saddr, &niph->daddr, 0); |
| 51 | nskb->ip_summed = CHECKSUM_PARTIAL; |
| 52 | nskb->csum_start = (unsigned char *)nth - nskb->head; |
| 53 | nskb->csum_offset = offsetof(struct tcphdr, check); |
| 54 | |
| 55 | memset(&fl6, 0, sizeof(fl6)); |
| 56 | fl6.flowi6_proto = IPPROTO_TCP; |
| 57 | fl6.saddr = niph->saddr; |
| 58 | fl6.daddr = niph->daddr; |
| 59 | fl6.fl6_sport = nth->source; |
| 60 | fl6.fl6_dport = nth->dest; |
| 61 | security_skb_classify_flow((struct sk_buff *)skb, flowi6_to_flowi(&fl6)); |
| 62 | dst = ip6_route_output(net, NULL, &fl6); |
Liping Zhang | d2b484b | 2016-04-22 02:56:57 -0700 | [diff] [blame] | 63 | if (dst->error) { |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 64 | dst_release(dst); |
| 65 | goto free_nskb; |
| 66 | } |
| 67 | dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0); |
| 68 | if (IS_ERR(dst)) |
| 69 | goto free_nskb; |
| 70 | |
| 71 | skb_dst_set(nskb, dst); |
| 72 | |
| 73 | if (nfct) { |
| 74 | nskb->nfct = nfct; |
| 75 | nskb->nfctinfo = ctinfo; |
| 76 | nf_conntrack_get(nfct); |
| 77 | } |
| 78 | |
Eric W. Biederman | 33224b1 | 2015-10-07 16:48:46 -0500 | [diff] [blame] | 79 | ip6_local_out(net, nskb->sk, nskb); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 80 | return; |
| 81 | |
| 82 | free_nskb: |
| 83 | kfree_skb(nskb); |
| 84 | } |
| 85 | |
| 86 | static void |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 87 | synproxy_send_client_synack(struct net *net, |
Phil Sutter | 96fffb4 | 2015-08-09 13:14:15 +0200 | [diff] [blame] | 88 | const struct sk_buff *skb, const struct tcphdr *th, |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 89 | const struct synproxy_options *opts) |
| 90 | { |
| 91 | struct sk_buff *nskb; |
| 92 | struct ipv6hdr *iph, *niph; |
| 93 | struct tcphdr *nth; |
| 94 | unsigned int tcp_hdr_size; |
| 95 | u16 mss = opts->mss; |
| 96 | |
| 97 | iph = ipv6_hdr(skb); |
| 98 | |
| 99 | tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts); |
| 100 | nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER, |
| 101 | GFP_ATOMIC); |
| 102 | if (nskb == NULL) |
| 103 | return; |
| 104 | skb_reserve(nskb, MAX_TCP_HEADER); |
| 105 | |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 106 | niph = synproxy_build_ip(net, nskb, &iph->daddr, &iph->saddr); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 107 | |
| 108 | skb_reset_transport_header(nskb); |
| 109 | nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size); |
| 110 | nth->source = th->dest; |
| 111 | nth->dest = th->source; |
| 112 | nth->seq = htonl(__cookie_v6_init_sequence(iph, th, &mss)); |
| 113 | nth->ack_seq = htonl(ntohl(th->seq) + 1); |
| 114 | tcp_flag_word(nth) = TCP_FLAG_SYN | TCP_FLAG_ACK; |
| 115 | if (opts->options & XT_SYNPROXY_OPT_ECN) |
| 116 | tcp_flag_word(nth) |= TCP_FLAG_ECE; |
| 117 | nth->doff = tcp_hdr_size / 4; |
| 118 | nth->window = 0; |
| 119 | nth->check = 0; |
| 120 | nth->urg_ptr = 0; |
| 121 | |
| 122 | synproxy_build_options(nth, opts); |
| 123 | |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 124 | synproxy_send_tcp(net, skb, nskb, skb->nfct, IP_CT_ESTABLISHED_REPLY, |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 125 | niph, nth, tcp_hdr_size); |
| 126 | } |
| 127 | |
| 128 | static void |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 129 | synproxy_send_server_syn(struct net *net, |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 130 | const struct sk_buff *skb, const struct tcphdr *th, |
| 131 | const struct synproxy_options *opts, u32 recv_seq) |
| 132 | { |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 133 | struct synproxy_net *snet = synproxy_pernet(net); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 134 | struct sk_buff *nskb; |
| 135 | struct ipv6hdr *iph, *niph; |
| 136 | struct tcphdr *nth; |
| 137 | unsigned int tcp_hdr_size; |
| 138 | |
| 139 | iph = ipv6_hdr(skb); |
| 140 | |
| 141 | tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts); |
| 142 | nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER, |
| 143 | GFP_ATOMIC); |
| 144 | if (nskb == NULL) |
| 145 | return; |
| 146 | skb_reserve(nskb, MAX_TCP_HEADER); |
| 147 | |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 148 | niph = synproxy_build_ip(net, nskb, &iph->saddr, &iph->daddr); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 149 | |
| 150 | skb_reset_transport_header(nskb); |
| 151 | nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size); |
| 152 | nth->source = th->source; |
| 153 | nth->dest = th->dest; |
| 154 | nth->seq = htonl(recv_seq - 1); |
| 155 | /* ack_seq is used to relay our ISN to the synproxy hook to initialize |
| 156 | * sequence number translation once a connection tracking entry exists. |
| 157 | */ |
| 158 | nth->ack_seq = htonl(ntohl(th->ack_seq) - 1); |
| 159 | tcp_flag_word(nth) = TCP_FLAG_SYN; |
| 160 | if (opts->options & XT_SYNPROXY_OPT_ECN) |
| 161 | tcp_flag_word(nth) |= TCP_FLAG_ECE | TCP_FLAG_CWR; |
| 162 | nth->doff = tcp_hdr_size / 4; |
| 163 | nth->window = th->window; |
| 164 | nth->check = 0; |
| 165 | nth->urg_ptr = 0; |
| 166 | |
| 167 | synproxy_build_options(nth, opts); |
| 168 | |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 169 | synproxy_send_tcp(net, skb, nskb, &snet->tmpl->ct_general, IP_CT_NEW, |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 170 | niph, nth, tcp_hdr_size); |
| 171 | } |
| 172 | |
| 173 | static void |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 174 | synproxy_send_server_ack(struct net *net, |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 175 | const struct ip_ct_tcp *state, |
| 176 | const struct sk_buff *skb, const struct tcphdr *th, |
| 177 | const struct synproxy_options *opts) |
| 178 | { |
| 179 | struct sk_buff *nskb; |
| 180 | struct ipv6hdr *iph, *niph; |
| 181 | struct tcphdr *nth; |
| 182 | unsigned int tcp_hdr_size; |
| 183 | |
| 184 | iph = ipv6_hdr(skb); |
| 185 | |
| 186 | tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts); |
| 187 | nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER, |
| 188 | GFP_ATOMIC); |
| 189 | if (nskb == NULL) |
| 190 | return; |
| 191 | skb_reserve(nskb, MAX_TCP_HEADER); |
| 192 | |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 193 | niph = synproxy_build_ip(net, nskb, &iph->daddr, &iph->saddr); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 194 | |
| 195 | skb_reset_transport_header(nskb); |
| 196 | nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size); |
| 197 | nth->source = th->dest; |
| 198 | nth->dest = th->source; |
| 199 | nth->seq = htonl(ntohl(th->ack_seq)); |
| 200 | nth->ack_seq = htonl(ntohl(th->seq) + 1); |
| 201 | tcp_flag_word(nth) = TCP_FLAG_ACK; |
| 202 | nth->doff = tcp_hdr_size / 4; |
| 203 | nth->window = htons(state->seen[IP_CT_DIR_ORIGINAL].td_maxwin); |
| 204 | nth->check = 0; |
| 205 | nth->urg_ptr = 0; |
| 206 | |
| 207 | synproxy_build_options(nth, opts); |
| 208 | |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 209 | synproxy_send_tcp(net, skb, nskb, NULL, 0, niph, nth, tcp_hdr_size); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static void |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 213 | synproxy_send_client_ack(struct net *net, |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 214 | const struct sk_buff *skb, const struct tcphdr *th, |
| 215 | const struct synproxy_options *opts) |
| 216 | { |
| 217 | struct sk_buff *nskb; |
| 218 | struct ipv6hdr *iph, *niph; |
| 219 | struct tcphdr *nth; |
| 220 | unsigned int tcp_hdr_size; |
| 221 | |
| 222 | iph = ipv6_hdr(skb); |
| 223 | |
| 224 | tcp_hdr_size = sizeof(*nth) + synproxy_options_size(opts); |
| 225 | nskb = alloc_skb(sizeof(*niph) + tcp_hdr_size + MAX_TCP_HEADER, |
| 226 | GFP_ATOMIC); |
| 227 | if (nskb == NULL) |
| 228 | return; |
| 229 | skb_reserve(nskb, MAX_TCP_HEADER); |
| 230 | |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 231 | niph = synproxy_build_ip(net, nskb, &iph->saddr, &iph->daddr); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 232 | |
| 233 | skb_reset_transport_header(nskb); |
| 234 | nth = (struct tcphdr *)skb_put(nskb, tcp_hdr_size); |
| 235 | nth->source = th->source; |
| 236 | nth->dest = th->dest; |
| 237 | nth->seq = htonl(ntohl(th->seq) + 1); |
| 238 | nth->ack_seq = th->ack_seq; |
| 239 | tcp_flag_word(nth) = TCP_FLAG_ACK; |
| 240 | nth->doff = tcp_hdr_size / 4; |
Florian Westphal | 851345c | 2015-08-28 00:16:21 +0200 | [diff] [blame] | 241 | nth->window = htons(ntohs(th->window) >> opts->wscale); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 242 | nth->check = 0; |
| 243 | nth->urg_ptr = 0; |
| 244 | |
| 245 | synproxy_build_options(nth, opts); |
| 246 | |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 247 | synproxy_send_tcp(net, skb, nskb, skb->nfct, IP_CT_ESTABLISHED_REPLY, |
Ian Morris | 7695495 | 2015-10-11 17:32:16 +0100 | [diff] [blame] | 248 | niph, nth, tcp_hdr_size); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | static bool |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 252 | synproxy_recv_client_ack(struct net *net, |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 253 | const struct sk_buff *skb, const struct tcphdr *th, |
| 254 | struct synproxy_options *opts, u32 recv_seq) |
| 255 | { |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 256 | struct synproxy_net *snet = synproxy_pernet(net); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 257 | int mss; |
| 258 | |
| 259 | mss = __cookie_v6_check(ipv6_hdr(skb), th, ntohl(th->ack_seq) - 1); |
| 260 | if (mss == 0) { |
| 261 | this_cpu_inc(snet->stats->cookie_invalid); |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | this_cpu_inc(snet->stats->cookie_valid); |
| 266 | opts->mss = mss; |
Martin Topholm | a6441b7 | 2013-11-14 15:35:30 +0100 | [diff] [blame] | 267 | opts->options |= XT_SYNPROXY_OPT_MSS; |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 268 | |
| 269 | if (opts->options & XT_SYNPROXY_OPT_TIMESTAMP) |
| 270 | synproxy_check_timestamp_cookie(opts); |
| 271 | |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 272 | synproxy_send_server_syn(net, skb, th, opts, recv_seq); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | |
| 276 | static unsigned int |
| 277 | synproxy_tg6(struct sk_buff *skb, const struct xt_action_param *par) |
| 278 | { |
| 279 | const struct xt_synproxy_info *info = par->targinfo; |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 280 | struct net *net = par->net; |
| 281 | struct synproxy_net *snet = synproxy_pernet(net); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 282 | struct synproxy_options opts = {}; |
| 283 | struct tcphdr *th, _th; |
| 284 | |
| 285 | if (nf_ip6_checksum(skb, par->hooknum, par->thoff, IPPROTO_TCP)) |
| 286 | return NF_DROP; |
| 287 | |
| 288 | th = skb_header_pointer(skb, par->thoff, sizeof(_th), &_th); |
| 289 | if (th == NULL) |
| 290 | return NF_DROP; |
| 291 | |
Patrick McHardy | f4a87e7 | 2013-09-30 08:51:46 +0100 | [diff] [blame] | 292 | if (!synproxy_parse_options(skb, par->thoff, th, &opts)) |
| 293 | return NF_DROP; |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 294 | |
Jesper Dangaard Brouer | 775ada6 | 2013-08-28 15:14:38 +0200 | [diff] [blame] | 295 | if (th->syn && !(th->ack || th->fin || th->rst)) { |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 296 | /* Initial SYN from client */ |
| 297 | this_cpu_inc(snet->stats->syn_received); |
| 298 | |
| 299 | if (th->ece && th->cwr) |
| 300 | opts.options |= XT_SYNPROXY_OPT_ECN; |
| 301 | |
| 302 | opts.options &= info->options; |
| 303 | if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP) |
| 304 | synproxy_init_timestamp_cookie(info, &opts); |
| 305 | else |
| 306 | opts.options &= ~(XT_SYNPROXY_OPT_WSCALE | |
| 307 | XT_SYNPROXY_OPT_SACK_PERM | |
| 308 | XT_SYNPROXY_OPT_ECN); |
| 309 | |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 310 | synproxy_send_client_synack(net, skb, th, &opts); |
Jesper Dangaard Brouer | 7cc9eb6 | 2013-08-29 12:18:46 +0200 | [diff] [blame] | 311 | return NF_DROP; |
| 312 | |
| 313 | } else if (th->ack && !(th->fin || th->rst || th->syn)) { |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 314 | /* ACK from client */ |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 315 | synproxy_recv_client_ack(net, skb, th, &opts, ntohl(th->seq)); |
Jesper Dangaard Brouer | 7cc9eb6 | 2013-08-29 12:18:46 +0200 | [diff] [blame] | 316 | return NF_DROP; |
| 317 | } |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 318 | |
Jesper Dangaard Brouer | 7cc9eb6 | 2013-08-29 12:18:46 +0200 | [diff] [blame] | 319 | return XT_CONTINUE; |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Eric W. Biederman | 06198b3 | 2015-09-18 14:33:06 -0500 | [diff] [blame] | 322 | static unsigned int ipv6_synproxy_hook(void *priv, |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 323 | struct sk_buff *skb, |
David S. Miller | 238e54c | 2015-04-03 20:32:56 -0400 | [diff] [blame] | 324 | const struct nf_hook_state *nhs) |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 325 | { |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 326 | struct net *net = nhs->net; |
| 327 | struct synproxy_net *snet = synproxy_pernet(net); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 328 | enum ip_conntrack_info ctinfo; |
| 329 | struct nf_conn *ct; |
| 330 | struct nf_conn_synproxy *synproxy; |
| 331 | struct synproxy_options opts = {}; |
| 332 | const struct ip_ct_tcp *state; |
| 333 | struct tcphdr *th, _th; |
| 334 | __be16 frag_off; |
| 335 | u8 nexthdr; |
| 336 | int thoff; |
| 337 | |
| 338 | ct = nf_ct_get(skb, &ctinfo); |
| 339 | if (ct == NULL) |
| 340 | return NF_ACCEPT; |
| 341 | |
| 342 | synproxy = nfct_synproxy(ct); |
| 343 | if (synproxy == NULL) |
| 344 | return NF_ACCEPT; |
| 345 | |
| 346 | if (nf_is_loopback_packet(skb)) |
| 347 | return NF_ACCEPT; |
| 348 | |
| 349 | nexthdr = ipv6_hdr(skb)->nexthdr; |
| 350 | thoff = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr, |
| 351 | &frag_off); |
| 352 | if (thoff < 0) |
| 353 | return NF_ACCEPT; |
| 354 | |
| 355 | th = skb_header_pointer(skb, thoff, sizeof(_th), &_th); |
| 356 | if (th == NULL) |
| 357 | return NF_DROP; |
| 358 | |
| 359 | state = &ct->proto.tcp; |
| 360 | switch (state->state) { |
| 361 | case TCP_CONNTRACK_CLOSE: |
| 362 | if (th->rst && !test_bit(IPS_SEEN_REPLY_BIT, &ct->status)) { |
| 363 | nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - |
| 364 | ntohl(th->seq) + 1); |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | if (!th->syn || th->ack || |
| 369 | CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) |
| 370 | break; |
| 371 | |
| 372 | /* Reopened connection - reset the sequence number and timestamp |
| 373 | * adjustments, they will get initialized once the connection is |
| 374 | * reestablished. |
| 375 | */ |
| 376 | nf_ct_seqadj_init(ct, ctinfo, 0); |
| 377 | synproxy->tsoff = 0; |
| 378 | this_cpu_inc(snet->stats->conn_reopened); |
| 379 | |
| 380 | /* fall through */ |
| 381 | case TCP_CONNTRACK_SYN_SENT: |
Patrick McHardy | f4a87e7 | 2013-09-30 08:51:46 +0100 | [diff] [blame] | 382 | if (!synproxy_parse_options(skb, thoff, th, &opts)) |
| 383 | return NF_DROP; |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 384 | |
| 385 | if (!th->syn && th->ack && |
| 386 | CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) { |
| 387 | /* Keep-Alives are sent with SEG.SEQ = SND.NXT-1, |
| 388 | * therefore we need to add 1 to make the SYN sequence |
| 389 | * number match the one of first SYN. |
| 390 | */ |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 391 | if (synproxy_recv_client_ack(net, skb, th, &opts, |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 392 | ntohl(th->seq) + 1)) |
| 393 | this_cpu_inc(snet->stats->cookie_retrans); |
| 394 | |
| 395 | return NF_DROP; |
| 396 | } |
| 397 | |
| 398 | synproxy->isn = ntohl(th->ack_seq); |
| 399 | if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP) |
| 400 | synproxy->its = opts.tsecr; |
| 401 | break; |
| 402 | case TCP_CONNTRACK_SYN_RECV: |
| 403 | if (!th->syn || !th->ack) |
| 404 | break; |
| 405 | |
Patrick McHardy | f4a87e7 | 2013-09-30 08:51:46 +0100 | [diff] [blame] | 406 | if (!synproxy_parse_options(skb, thoff, th, &opts)) |
| 407 | return NF_DROP; |
| 408 | |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 409 | if (opts.options & XT_SYNPROXY_OPT_TIMESTAMP) |
| 410 | synproxy->tsoff = opts.tsval - synproxy->its; |
| 411 | |
| 412 | opts.options &= ~(XT_SYNPROXY_OPT_MSS | |
| 413 | XT_SYNPROXY_OPT_WSCALE | |
| 414 | XT_SYNPROXY_OPT_SACK_PERM); |
| 415 | |
| 416 | swap(opts.tsval, opts.tsecr); |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 417 | synproxy_send_server_ack(net, state, skb, th, &opts); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 418 | |
| 419 | nf_ct_seqadj_init(ct, ctinfo, synproxy->isn - ntohl(th->seq)); |
| 420 | |
| 421 | swap(opts.tsval, opts.tsecr); |
Liping Zhang | 8fef24c | 2016-03-28 22:27:27 +0800 | [diff] [blame] | 422 | synproxy_send_client_ack(net, skb, th, &opts); |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 423 | |
| 424 | consume_skb(skb); |
| 425 | return NF_STOLEN; |
| 426 | default: |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | synproxy_tstamp_adjust(skb, thoff, th, ct, ctinfo, synproxy); |
| 431 | return NF_ACCEPT; |
| 432 | } |
| 433 | |
| 434 | static int synproxy_tg6_check(const struct xt_tgchk_param *par) |
| 435 | { |
| 436 | const struct ip6t_entry *e = par->entryinfo; |
| 437 | |
| 438 | if (!(e->ipv6.flags & IP6T_F_PROTO) || |
| 439 | e->ipv6.proto != IPPROTO_TCP || |
| 440 | e->ipv6.invflags & XT_INV_PROTO) |
| 441 | return -EINVAL; |
| 442 | |
| 443 | return nf_ct_l3proto_try_module_get(par->family); |
| 444 | } |
| 445 | |
| 446 | static void synproxy_tg6_destroy(const struct xt_tgdtor_param *par) |
| 447 | { |
| 448 | nf_ct_l3proto_module_put(par->family); |
| 449 | } |
| 450 | |
| 451 | static struct xt_target synproxy_tg6_reg __read_mostly = { |
| 452 | .name = "SYNPROXY", |
| 453 | .family = NFPROTO_IPV6, |
Patrick McHardy | f01b392 | 2013-12-08 16:52:31 +0000 | [diff] [blame] | 454 | .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD), |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 455 | .target = synproxy_tg6, |
| 456 | .targetsize = sizeof(struct xt_synproxy_info), |
| 457 | .checkentry = synproxy_tg6_check, |
| 458 | .destroy = synproxy_tg6_destroy, |
| 459 | .me = THIS_MODULE, |
| 460 | }; |
| 461 | |
| 462 | static struct nf_hook_ops ipv6_synproxy_ops[] __read_mostly = { |
| 463 | { |
| 464 | .hook = ipv6_synproxy_hook, |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 465 | .pf = NFPROTO_IPV6, |
| 466 | .hooknum = NF_INET_LOCAL_IN, |
| 467 | .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1, |
| 468 | }, |
| 469 | { |
| 470 | .hook = ipv6_synproxy_hook, |
Patrick McHardy | 4ad3622 | 2013-08-27 08:50:16 +0200 | [diff] [blame] | 471 | .pf = NFPROTO_IPV6, |
| 472 | .hooknum = NF_INET_POST_ROUTING, |
| 473 | .priority = NF_IP_PRI_CONNTRACK_CONFIRM - 1, |
| 474 | }, |
| 475 | }; |
| 476 | |
| 477 | static int __init synproxy_tg6_init(void) |
| 478 | { |
| 479 | int err; |
| 480 | |
| 481 | err = nf_register_hooks(ipv6_synproxy_ops, |
| 482 | ARRAY_SIZE(ipv6_synproxy_ops)); |
| 483 | if (err < 0) |
| 484 | goto err1; |
| 485 | |
| 486 | err = xt_register_target(&synproxy_tg6_reg); |
| 487 | if (err < 0) |
| 488 | goto err2; |
| 489 | |
| 490 | return 0; |
| 491 | |
| 492 | err2: |
| 493 | nf_unregister_hooks(ipv6_synproxy_ops, ARRAY_SIZE(ipv6_synproxy_ops)); |
| 494 | err1: |
| 495 | return err; |
| 496 | } |
| 497 | |
| 498 | static void __exit synproxy_tg6_exit(void) |
| 499 | { |
| 500 | xt_unregister_target(&synproxy_tg6_reg); |
| 501 | nf_unregister_hooks(ipv6_synproxy_ops, ARRAY_SIZE(ipv6_synproxy_ops)); |
| 502 | } |
| 503 | |
| 504 | module_init(synproxy_tg6_init); |
| 505 | module_exit(synproxy_tg6_exit); |
| 506 | |
| 507 | MODULE_LICENSE("GPL"); |
| 508 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |