| 1 | /* |
| 2 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 3 | * operating system. INET is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * Implementation of the Transmission Control Protocol(TCP). |
| 7 | * |
| 8 | * IPv4 specific functions |
| 9 | * |
| 10 | * |
| 11 | * code split from: |
| 12 | * linux/ipv4/tcp.c |
| 13 | * linux/ipv4/tcp_input.c |
| 14 | * linux/ipv4/tcp_output.c |
| 15 | * |
| 16 | * See tcp.c for author information |
| 17 | * |
| 18 | * This program is free software; you can redistribute it and/or |
| 19 | * modify it under the terms of the GNU General Public License |
| 20 | * as published by the Free Software Foundation; either version |
| 21 | * 2 of the License, or (at your option) any later version. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * Changes: |
| 26 | * David S. Miller : New socket lookup architecture. |
| 27 | * This code is dedicated to John Dyson. |
| 28 | * David S. Miller : Change semantics of established hash, |
| 29 | * half is devoted to TIME_WAIT sockets |
| 30 | * and the rest go in the other half. |
| 31 | * Andi Kleen : Add support for syncookies and fixed |
| 32 | * some bugs: ip options weren't passed to |
| 33 | * the TCP layer, missed a check for an |
| 34 | * ACK bit. |
| 35 | * Andi Kleen : Implemented fast path mtu discovery. |
| 36 | * Fixed many serious bugs in the |
| 37 | * request_sock handling and moved |
| 38 | * most of it into the af independent code. |
| 39 | * Added tail drop and some other bugfixes. |
| 40 | * Added new listen semantics. |
| 41 | * Mike McLagan : Routing by source |
| 42 | * Juan Jose Ciarlante: ip_dynaddr bits |
| 43 | * Andi Kleen: various fixes. |
| 44 | * Vitaly E. Lavrov : Transparent proxy revived after year |
| 45 | * coma. |
| 46 | * Andi Kleen : Fix new listen. |
| 47 | * Andi Kleen : Fix accept error reporting. |
| 48 | * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which |
| 49 | * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind |
| 50 | * a single port at the same time. |
| 51 | */ |
| 52 | |
| 53 | #define pr_fmt(fmt) "TCP: " fmt |
| 54 | |
| 55 | #include <linux/bottom_half.h> |
| 56 | #include <linux/types.h> |
| 57 | #include <linux/fcntl.h> |
| 58 | #include <linux/module.h> |
| 59 | #include <linux/random.h> |
| 60 | #include <linux/cache.h> |
| 61 | #include <linux/jhash.h> |
| 62 | #include <linux/init.h> |
| 63 | #include <linux/times.h> |
| 64 | #include <linux/slab.h> |
| 65 | |
| 66 | #include <net/net_namespace.h> |
| 67 | #include <net/icmp.h> |
| 68 | #include <net/inet_hashtables.h> |
| 69 | #include <net/tcp.h> |
| 70 | #include <net/transp_v6.h> |
| 71 | #include <net/ipv6.h> |
| 72 | #include <net/inet_common.h> |
| 73 | #include <net/timewait_sock.h> |
| 74 | #include <net/xfrm.h> |
| 75 | #include <net/netdma.h> |
| 76 | #include <net/secure_seq.h> |
| 77 | #include <net/tcp_memcontrol.h> |
| 78 | |
| 79 | #include <linux/inet.h> |
| 80 | #include <linux/ipv6.h> |
| 81 | #include <linux/stddef.h> |
| 82 | #include <linux/proc_fs.h> |
| 83 | #include <linux/seq_file.h> |
| 84 | |
| 85 | #include <linux/crypto.h> |
| 86 | #include <linux/scatterlist.h> |
| 87 | |
| 88 | int sysctl_tcp_tw_reuse __read_mostly; |
| 89 | int sysctl_tcp_low_latency __read_mostly; |
| 90 | EXPORT_SYMBOL(sysctl_tcp_low_latency); |
| 91 | |
| 92 | |
| 93 | #ifdef CONFIG_TCP_MD5SIG |
| 94 | static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key, |
| 95 | __be32 daddr, __be32 saddr, const struct tcphdr *th); |
| 96 | #endif |
| 97 | |
| 98 | struct inet_hashinfo tcp_hashinfo; |
| 99 | EXPORT_SYMBOL(tcp_hashinfo); |
| 100 | |
| 101 | static inline __u32 tcp_v4_init_sequence(const struct sk_buff *skb) |
| 102 | { |
| 103 | return secure_tcp_sequence_number(ip_hdr(skb)->daddr, |
| 104 | ip_hdr(skb)->saddr, |
| 105 | tcp_hdr(skb)->dest, |
| 106 | tcp_hdr(skb)->source); |
| 107 | } |
| 108 | |
| 109 | int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp) |
| 110 | { |
| 111 | const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw); |
| 112 | struct tcp_sock *tp = tcp_sk(sk); |
| 113 | |
| 114 | /* With PAWS, it is safe from the viewpoint |
| 115 | of data integrity. Even without PAWS it is safe provided sequence |
| 116 | spaces do not overlap i.e. at data rates <= 80Mbit/sec. |
| 117 | |
| 118 | Actually, the idea is close to VJ's one, only timestamp cache is |
| 119 | held not per host, but per port pair and TW bucket is used as state |
| 120 | holder. |
| 121 | |
| 122 | If TW bucket has been already destroyed we fall back to VJ's scheme |
| 123 | and use initial timestamp retrieved from peer table. |
| 124 | */ |
| 125 | if (tcptw->tw_ts_recent_stamp && |
| 126 | (twp == NULL || (sysctl_tcp_tw_reuse && |
| 127 | get_seconds() - tcptw->tw_ts_recent_stamp > 1))) { |
| 128 | tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2; |
| 129 | if (tp->write_seq == 0) |
| 130 | tp->write_seq = 1; |
| 131 | tp->rx_opt.ts_recent = tcptw->tw_ts_recent; |
| 132 | tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp; |
| 133 | sock_hold(sktw); |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | EXPORT_SYMBOL_GPL(tcp_twsk_unique); |
| 140 | |
| 141 | /* This will initiate an outgoing connection. */ |
| 142 | int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
| 143 | { |
| 144 | struct sockaddr_in *usin = (struct sockaddr_in *)uaddr; |
| 145 | struct inet_sock *inet = inet_sk(sk); |
| 146 | struct tcp_sock *tp = tcp_sk(sk); |
| 147 | __be16 orig_sport, orig_dport; |
| 148 | __be32 daddr, nexthop; |
| 149 | struct flowi4 *fl4; |
| 150 | struct rtable *rt; |
| 151 | int err; |
| 152 | struct ip_options_rcu *inet_opt; |
| 153 | |
| 154 | if (addr_len < sizeof(struct sockaddr_in)) |
| 155 | return -EINVAL; |
| 156 | |
| 157 | if (usin->sin_family != AF_INET) |
| 158 | return -EAFNOSUPPORT; |
| 159 | |
| 160 | nexthop = daddr = usin->sin_addr.s_addr; |
| 161 | inet_opt = rcu_dereference_protected(inet->inet_opt, |
| 162 | sock_owned_by_user(sk)); |
| 163 | if (inet_opt && inet_opt->opt.srr) { |
| 164 | if (!daddr) |
| 165 | return -EINVAL; |
| 166 | nexthop = inet_opt->opt.faddr; |
| 167 | } |
| 168 | |
| 169 | orig_sport = inet->inet_sport; |
| 170 | orig_dport = usin->sin_port; |
| 171 | fl4 = &inet->cork.fl.u.ip4; |
| 172 | rt = ip_route_connect(fl4, nexthop, inet->inet_saddr, |
| 173 | RT_CONN_FLAGS(sk), sk->sk_bound_dev_if, |
| 174 | IPPROTO_TCP, |
| 175 | orig_sport, orig_dport, sk, true); |
| 176 | if (IS_ERR(rt)) { |
| 177 | err = PTR_ERR(rt); |
| 178 | if (err == -ENETUNREACH) |
| 179 | IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES); |
| 180 | return err; |
| 181 | } |
| 182 | |
| 183 | if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) { |
| 184 | ip_rt_put(rt); |
| 185 | return -ENETUNREACH; |
| 186 | } |
| 187 | |
| 188 | if (!inet_opt || !inet_opt->opt.srr) |
| 189 | daddr = fl4->daddr; |
| 190 | |
| 191 | if (!inet->inet_saddr) |
| 192 | inet->inet_saddr = fl4->saddr; |
| 193 | inet->inet_rcv_saddr = inet->inet_saddr; |
| 194 | |
| 195 | if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) { |
| 196 | /* Reset inherited state */ |
| 197 | tp->rx_opt.ts_recent = 0; |
| 198 | tp->rx_opt.ts_recent_stamp = 0; |
| 199 | if (likely(!tp->repair)) |
| 200 | tp->write_seq = 0; |
| 201 | } |
| 202 | |
| 203 | if (tcp_death_row.sysctl_tw_recycle && |
| 204 | !tp->rx_opt.ts_recent_stamp && fl4->daddr == daddr) |
| 205 | tcp_fetch_timewait_stamp(sk, &rt->dst); |
| 206 | |
| 207 | inet->inet_dport = usin->sin_port; |
| 208 | inet->inet_daddr = daddr; |
| 209 | |
| 210 | inet_csk(sk)->icsk_ext_hdr_len = 0; |
| 211 | if (inet_opt) |
| 212 | inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen; |
| 213 | |
| 214 | tp->rx_opt.mss_clamp = TCP_MSS_DEFAULT; |
| 215 | |
| 216 | /* Socket identity is still unknown (sport may be zero). |
| 217 | * However we set state to SYN-SENT and not releasing socket |
| 218 | * lock select source port, enter ourselves into the hash tables and |
| 219 | * complete initialization after this. |
| 220 | */ |
| 221 | tcp_set_state(sk, TCP_SYN_SENT); |
| 222 | err = inet_hash_connect(&tcp_death_row, sk); |
| 223 | if (err) |
| 224 | goto failure; |
| 225 | |
| 226 | rt = ip_route_newports(fl4, rt, orig_sport, orig_dport, |
| 227 | inet->inet_sport, inet->inet_dport, sk); |
| 228 | if (IS_ERR(rt)) { |
| 229 | err = PTR_ERR(rt); |
| 230 | rt = NULL; |
| 231 | goto failure; |
| 232 | } |
| 233 | /* OK, now commit destination to socket. */ |
| 234 | sk->sk_gso_type = SKB_GSO_TCPV4; |
| 235 | sk_setup_caps(sk, &rt->dst); |
| 236 | |
| 237 | if (!tp->write_seq && likely(!tp->repair)) |
| 238 | tp->write_seq = secure_tcp_sequence_number(inet->inet_saddr, |
| 239 | inet->inet_daddr, |
| 240 | inet->inet_sport, |
| 241 | usin->sin_port); |
| 242 | |
| 243 | inet->inet_id = tp->write_seq ^ jiffies; |
| 244 | |
| 245 | err = tcp_connect(sk); |
| 246 | |
| 247 | rt = NULL; |
| 248 | if (err) |
| 249 | goto failure; |
| 250 | |
| 251 | return 0; |
| 252 | |
| 253 | failure: |
| 254 | /* |
| 255 | * This unhashes the socket and releases the local port, |
| 256 | * if necessary. |
| 257 | */ |
| 258 | tcp_set_state(sk, TCP_CLOSE); |
| 259 | ip_rt_put(rt); |
| 260 | sk->sk_route_caps = 0; |
| 261 | inet->inet_dport = 0; |
| 262 | return err; |
| 263 | } |
| 264 | EXPORT_SYMBOL(tcp_v4_connect); |
| 265 | |
| 266 | /* |
| 267 | * This routine reacts to ICMP_FRAG_NEEDED mtu indications as defined in RFC1191. |
| 268 | * It can be called through tcp_release_cb() if socket was owned by user |
| 269 | * at the time tcp_v4_err() was called to handle ICMP message. |
| 270 | */ |
| 271 | void tcp_v4_mtu_reduced(struct sock *sk) |
| 272 | { |
| 273 | struct inet_sock *inet = inet_sk(sk); |
| 274 | struct dst_entry *dst; |
| 275 | u32 mtu; |
| 276 | |
| 277 | if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) |
| 278 | return; |
| 279 | mtu = tcp_sk(sk)->mtu_info; |
| 280 | dst = inet_csk_update_pmtu(sk, mtu); |
| 281 | if (!dst) |
| 282 | return; |
| 283 | |
| 284 | /* Something is about to be wrong... Remember soft error |
| 285 | * for the case, if this connection will not able to recover. |
| 286 | */ |
| 287 | if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst)) |
| 288 | sk->sk_err_soft = EMSGSIZE; |
| 289 | |
| 290 | mtu = dst_mtu(dst); |
| 291 | |
| 292 | if (inet->pmtudisc != IP_PMTUDISC_DONT && |
| 293 | inet_csk(sk)->icsk_pmtu_cookie > mtu) { |
| 294 | tcp_sync_mss(sk, mtu); |
| 295 | |
| 296 | /* Resend the TCP packet because it's |
| 297 | * clear that the old packet has been |
| 298 | * dropped. This is the new "fast" path mtu |
| 299 | * discovery. |
| 300 | */ |
| 301 | tcp_simple_retransmit(sk); |
| 302 | } /* else let the usual retransmit timer handle it */ |
| 303 | } |
| 304 | EXPORT_SYMBOL(tcp_v4_mtu_reduced); |
| 305 | |
| 306 | static void do_redirect(struct sk_buff *skb, struct sock *sk) |
| 307 | { |
| 308 | struct dst_entry *dst = __sk_dst_check(sk, 0); |
| 309 | |
| 310 | if (dst) |
| 311 | dst->ops->redirect(dst, sk, skb); |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * This routine is called by the ICMP module when it gets some |
| 316 | * sort of error condition. If err < 0 then the socket should |
| 317 | * be closed and the error returned to the user. If err > 0 |
| 318 | * it's just the icmp type << 8 | icmp code. After adjustment |
| 319 | * header points to the first 8 bytes of the tcp header. We need |
| 320 | * to find the appropriate port. |
| 321 | * |
| 322 | * The locking strategy used here is very "optimistic". When |
| 323 | * someone else accesses the socket the ICMP is just dropped |
| 324 | * and for some paths there is no check at all. |
| 325 | * A more general error queue to queue errors for later handling |
| 326 | * is probably better. |
| 327 | * |
| 328 | */ |
| 329 | |
| 330 | void tcp_v4_err(struct sk_buff *icmp_skb, u32 info) |
| 331 | { |
| 332 | const struct iphdr *iph = (const struct iphdr *)icmp_skb->data; |
| 333 | struct tcphdr *th = (struct tcphdr *)(icmp_skb->data + (iph->ihl << 2)); |
| 334 | struct inet_connection_sock *icsk; |
| 335 | struct tcp_sock *tp; |
| 336 | struct inet_sock *inet; |
| 337 | const int type = icmp_hdr(icmp_skb)->type; |
| 338 | const int code = icmp_hdr(icmp_skb)->code; |
| 339 | struct sock *sk; |
| 340 | struct sk_buff *skb; |
| 341 | struct request_sock *req; |
| 342 | __u32 seq; |
| 343 | __u32 remaining; |
| 344 | int err; |
| 345 | struct net *net = dev_net(icmp_skb->dev); |
| 346 | |
| 347 | if (icmp_skb->len < (iph->ihl << 2) + 8) { |
| 348 | ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | sk = inet_lookup(net, &tcp_hashinfo, iph->daddr, th->dest, |
| 353 | iph->saddr, th->source, inet_iif(icmp_skb)); |
| 354 | if (!sk) { |
| 355 | ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS); |
| 356 | return; |
| 357 | } |
| 358 | if (sk->sk_state == TCP_TIME_WAIT) { |
| 359 | inet_twsk_put(inet_twsk(sk)); |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | bh_lock_sock(sk); |
| 364 | /* If too many ICMPs get dropped on busy |
| 365 | * servers this needs to be solved differently. |
| 366 | * We do take care of PMTU discovery (RFC1191) special case : |
| 367 | * we can receive locally generated ICMP messages while socket is held. |
| 368 | */ |
| 369 | if (sock_owned_by_user(sk)) { |
| 370 | if (!(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)) |
| 371 | NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS); |
| 372 | } |
| 373 | if (sk->sk_state == TCP_CLOSE) |
| 374 | goto out; |
| 375 | |
| 376 | if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) { |
| 377 | NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP); |
| 378 | goto out; |
| 379 | } |
| 380 | |
| 381 | icsk = inet_csk(sk); |
| 382 | tp = tcp_sk(sk); |
| 383 | req = tp->fastopen_rsk; |
| 384 | seq = ntohl(th->seq); |
| 385 | if (sk->sk_state != TCP_LISTEN && |
| 386 | !between(seq, tp->snd_una, tp->snd_nxt) && |
| 387 | (req == NULL || seq != tcp_rsk(req)->snt_isn)) { |
| 388 | /* For a Fast Open socket, allow seq to be snt_isn. */ |
| 389 | NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS); |
| 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | switch (type) { |
| 394 | case ICMP_REDIRECT: |
| 395 | if (!sock_owned_by_user(sk)) |
| 396 | do_redirect(icmp_skb, sk); |
| 397 | goto out; |
| 398 | case ICMP_SOURCE_QUENCH: |
| 399 | /* Just silently ignore these. */ |
| 400 | goto out; |
| 401 | case ICMP_PARAMETERPROB: |
| 402 | err = EPROTO; |
| 403 | break; |
| 404 | case ICMP_DEST_UNREACH: |
| 405 | if (code > NR_ICMP_UNREACH) |
| 406 | goto out; |
| 407 | |
| 408 | if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */ |
| 409 | /* We are not interested in TCP_LISTEN and open_requests |
| 410 | * (SYN-ACKs send out by Linux are always <576bytes so |
| 411 | * they should go through unfragmented). |
| 412 | */ |
| 413 | if (sk->sk_state == TCP_LISTEN) |
| 414 | goto out; |
| 415 | |
| 416 | tp->mtu_info = info; |
| 417 | if (!sock_owned_by_user(sk)) { |
| 418 | tcp_v4_mtu_reduced(sk); |
| 419 | } else { |
| 420 | if (!test_and_set_bit(TCP_MTU_REDUCED_DEFERRED, &tp->tsq_flags)) |
| 421 | sock_hold(sk); |
| 422 | } |
| 423 | goto out; |
| 424 | } |
| 425 | |
| 426 | err = icmp_err_convert[code].errno; |
| 427 | /* check if icmp_skb allows revert of backoff |
| 428 | * (see draft-zimmermann-tcp-lcd) */ |
| 429 | if (code != ICMP_NET_UNREACH && code != ICMP_HOST_UNREACH) |
| 430 | break; |
| 431 | if (seq != tp->snd_una || !icsk->icsk_retransmits || |
| 432 | !icsk->icsk_backoff) |
| 433 | break; |
| 434 | |
| 435 | /* XXX (TFO) - revisit the following logic for TFO */ |
| 436 | |
| 437 | if (sock_owned_by_user(sk)) |
| 438 | break; |
| 439 | |
| 440 | icsk->icsk_backoff--; |
| 441 | inet_csk(sk)->icsk_rto = (tp->srtt ? __tcp_set_rto(tp) : |
| 442 | TCP_TIMEOUT_INIT) << icsk->icsk_backoff; |
| 443 | tcp_bound_rto(sk); |
| 444 | |
| 445 | skb = tcp_write_queue_head(sk); |
| 446 | BUG_ON(!skb); |
| 447 | |
| 448 | remaining = icsk->icsk_rto - min(icsk->icsk_rto, |
| 449 | tcp_time_stamp - TCP_SKB_CB(skb)->when); |
| 450 | |
| 451 | if (remaining) { |
| 452 | inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, |
| 453 | remaining, TCP_RTO_MAX); |
| 454 | } else { |
| 455 | /* RTO revert clocked out retransmission. |
| 456 | * Will retransmit now */ |
| 457 | tcp_retransmit_timer(sk); |
| 458 | } |
| 459 | |
| 460 | break; |
| 461 | case ICMP_TIME_EXCEEDED: |
| 462 | err = EHOSTUNREACH; |
| 463 | break; |
| 464 | default: |
| 465 | goto out; |
| 466 | } |
| 467 | |
| 468 | /* XXX (TFO) - if it's a TFO socket and has been accepted, rather |
| 469 | * than following the TCP_SYN_RECV case and closing the socket, |
| 470 | * we ignore the ICMP error and keep trying like a fully established |
| 471 | * socket. Is this the right thing to do? |
| 472 | */ |
| 473 | if (req && req->sk == NULL) |
| 474 | goto out; |
| 475 | |
| 476 | switch (sk->sk_state) { |
| 477 | struct request_sock *req, **prev; |
| 478 | case TCP_LISTEN: |
| 479 | if (sock_owned_by_user(sk)) |
| 480 | goto out; |
| 481 | |
| 482 | req = inet_csk_search_req(sk, &prev, th->dest, |
| 483 | iph->daddr, iph->saddr); |
| 484 | if (!req) |
| 485 | goto out; |
| 486 | |
| 487 | /* ICMPs are not backlogged, hence we cannot get |
| 488 | an established socket here. |
| 489 | */ |
| 490 | WARN_ON(req->sk); |
| 491 | |
| 492 | if (seq != tcp_rsk(req)->snt_isn) { |
| 493 | NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS); |
| 494 | goto out; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * Still in SYN_RECV, just remove it silently. |
| 499 | * There is no good way to pass the error to the newly |
| 500 | * created socket, and POSIX does not want network |
| 501 | * errors returned from accept(). |
| 502 | */ |
| 503 | inet_csk_reqsk_queue_drop(sk, req, prev); |
| 504 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS); |
| 505 | goto out; |
| 506 | |
| 507 | case TCP_SYN_SENT: |
| 508 | case TCP_SYN_RECV: /* Cannot happen. |
| 509 | It can f.e. if SYNs crossed, |
| 510 | or Fast Open. |
| 511 | */ |
| 512 | if (!sock_owned_by_user(sk)) { |
| 513 | sk->sk_err = err; |
| 514 | |
| 515 | sk->sk_error_report(sk); |
| 516 | |
| 517 | tcp_done(sk); |
| 518 | } else { |
| 519 | sk->sk_err_soft = err; |
| 520 | } |
| 521 | goto out; |
| 522 | } |
| 523 | |
| 524 | /* If we've already connected we will keep trying |
| 525 | * until we time out, or the user gives up. |
| 526 | * |
| 527 | * rfc1122 4.2.3.9 allows to consider as hard errors |
| 528 | * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too, |
| 529 | * but it is obsoleted by pmtu discovery). |
| 530 | * |
| 531 | * Note, that in modern internet, where routing is unreliable |
| 532 | * and in each dark corner broken firewalls sit, sending random |
| 533 | * errors ordered by their masters even this two messages finally lose |
| 534 | * their original sense (even Linux sends invalid PORT_UNREACHs) |
| 535 | * |
| 536 | * Now we are in compliance with RFCs. |
| 537 | * --ANK (980905) |
| 538 | */ |
| 539 | |
| 540 | inet = inet_sk(sk); |
| 541 | if (!sock_owned_by_user(sk) && inet->recverr) { |
| 542 | sk->sk_err = err; |
| 543 | sk->sk_error_report(sk); |
| 544 | } else { /* Only an error on timeout */ |
| 545 | sk->sk_err_soft = err; |
| 546 | } |
| 547 | |
| 548 | out: |
| 549 | bh_unlock_sock(sk); |
| 550 | sock_put(sk); |
| 551 | } |
| 552 | |
| 553 | static void __tcp_v4_send_check(struct sk_buff *skb, |
| 554 | __be32 saddr, __be32 daddr) |
| 555 | { |
| 556 | struct tcphdr *th = tcp_hdr(skb); |
| 557 | |
| 558 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
| 559 | th->check = ~tcp_v4_check(skb->len, saddr, daddr, 0); |
| 560 | skb->csum_start = skb_transport_header(skb) - skb->head; |
| 561 | skb->csum_offset = offsetof(struct tcphdr, check); |
| 562 | } else { |
| 563 | th->check = tcp_v4_check(skb->len, saddr, daddr, |
| 564 | csum_partial(th, |
| 565 | th->doff << 2, |
| 566 | skb->csum)); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /* This routine computes an IPv4 TCP checksum. */ |
| 571 | void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb) |
| 572 | { |
| 573 | const struct inet_sock *inet = inet_sk(sk); |
| 574 | |
| 575 | __tcp_v4_send_check(skb, inet->inet_saddr, inet->inet_daddr); |
| 576 | } |
| 577 | EXPORT_SYMBOL(tcp_v4_send_check); |
| 578 | |
| 579 | int tcp_v4_gso_send_check(struct sk_buff *skb) |
| 580 | { |
| 581 | const struct iphdr *iph; |
| 582 | struct tcphdr *th; |
| 583 | |
| 584 | if (!pskb_may_pull(skb, sizeof(*th))) |
| 585 | return -EINVAL; |
| 586 | |
| 587 | iph = ip_hdr(skb); |
| 588 | th = tcp_hdr(skb); |
| 589 | |
| 590 | th->check = 0; |
| 591 | skb->ip_summed = CHECKSUM_PARTIAL; |
| 592 | __tcp_v4_send_check(skb, iph->saddr, iph->daddr); |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | /* |
| 597 | * This routine will send an RST to the other tcp. |
| 598 | * |
| 599 | * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.) |
| 600 | * for reset. |
| 601 | * Answer: if a packet caused RST, it is not for a socket |
| 602 | * existing in our system, if it is matched to a socket, |
| 603 | * it is just duplicate segment or bug in other side's TCP. |
| 604 | * So that we build reply only basing on parameters |
| 605 | * arrived with segment. |
| 606 | * Exception: precedence violation. We do not implement it in any case. |
| 607 | */ |
| 608 | |
| 609 | static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb) |
| 610 | { |
| 611 | const struct tcphdr *th = tcp_hdr(skb); |
| 612 | struct { |
| 613 | struct tcphdr th; |
| 614 | #ifdef CONFIG_TCP_MD5SIG |
| 615 | __be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)]; |
| 616 | #endif |
| 617 | } rep; |
| 618 | struct ip_reply_arg arg; |
| 619 | #ifdef CONFIG_TCP_MD5SIG |
| 620 | struct tcp_md5sig_key *key; |
| 621 | const __u8 *hash_location = NULL; |
| 622 | unsigned char newhash[16]; |
| 623 | int genhash; |
| 624 | struct sock *sk1 = NULL; |
| 625 | #endif |
| 626 | struct net *net; |
| 627 | |
| 628 | /* Never send a reset in response to a reset. */ |
| 629 | if (th->rst) |
| 630 | return; |
| 631 | |
| 632 | if (skb_rtable(skb)->rt_type != RTN_LOCAL) |
| 633 | return; |
| 634 | |
| 635 | /* Swap the send and the receive. */ |
| 636 | memset(&rep, 0, sizeof(rep)); |
| 637 | rep.th.dest = th->source; |
| 638 | rep.th.source = th->dest; |
| 639 | rep.th.doff = sizeof(struct tcphdr) / 4; |
| 640 | rep.th.rst = 1; |
| 641 | |
| 642 | if (th->ack) { |
| 643 | rep.th.seq = th->ack_seq; |
| 644 | } else { |
| 645 | rep.th.ack = 1; |
| 646 | rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin + |
| 647 | skb->len - (th->doff << 2)); |
| 648 | } |
| 649 | |
| 650 | memset(&arg, 0, sizeof(arg)); |
| 651 | arg.iov[0].iov_base = (unsigned char *)&rep; |
| 652 | arg.iov[0].iov_len = sizeof(rep.th); |
| 653 | |
| 654 | net = sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev); |
| 655 | #ifdef CONFIG_TCP_MD5SIG |
| 656 | hash_location = tcp_parse_md5sig_option(th); |
| 657 | if (!sk && hash_location) { |
| 658 | /* |
| 659 | * active side is lost. Try to find listening socket through |
| 660 | * source port, and then find md5 key through listening socket. |
| 661 | * we are not loose security here: |
| 662 | * Incoming packet is checked with md5 hash with finding key, |
| 663 | * no RST generated if md5 hash doesn't match. |
| 664 | */ |
| 665 | sk1 = __inet_lookup_listener(net, |
| 666 | &tcp_hashinfo, ip_hdr(skb)->saddr, |
| 667 | th->source, ip_hdr(skb)->daddr, |
| 668 | ntohs(th->source), inet_iif(skb)); |
| 669 | /* don't send rst if it can't find key */ |
| 670 | if (!sk1) |
| 671 | return; |
| 672 | rcu_read_lock(); |
| 673 | key = tcp_md5_do_lookup(sk1, (union tcp_md5_addr *) |
| 674 | &ip_hdr(skb)->saddr, AF_INET); |
| 675 | if (!key) |
| 676 | goto release_sk1; |
| 677 | |
| 678 | genhash = tcp_v4_md5_hash_skb(newhash, key, NULL, NULL, skb); |
| 679 | if (genhash || memcmp(hash_location, newhash, 16) != 0) |
| 680 | goto release_sk1; |
| 681 | } else { |
| 682 | key = sk ? tcp_md5_do_lookup(sk, (union tcp_md5_addr *) |
| 683 | &ip_hdr(skb)->saddr, |
| 684 | AF_INET) : NULL; |
| 685 | } |
| 686 | |
| 687 | if (key) { |
| 688 | rep.opt[0] = htonl((TCPOPT_NOP << 24) | |
| 689 | (TCPOPT_NOP << 16) | |
| 690 | (TCPOPT_MD5SIG << 8) | |
| 691 | TCPOLEN_MD5SIG); |
| 692 | /* Update length and the length the header thinks exists */ |
| 693 | arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED; |
| 694 | rep.th.doff = arg.iov[0].iov_len / 4; |
| 695 | |
| 696 | tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[1], |
| 697 | key, ip_hdr(skb)->saddr, |
| 698 | ip_hdr(skb)->daddr, &rep.th); |
| 699 | } |
| 700 | #endif |
| 701 | arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr, |
| 702 | ip_hdr(skb)->saddr, /* XXX */ |
| 703 | arg.iov[0].iov_len, IPPROTO_TCP, 0); |
| 704 | arg.csumoffset = offsetof(struct tcphdr, check) / 2; |
| 705 | arg.flags = (sk && inet_sk(sk)->transparent) ? IP_REPLY_ARG_NOSRCCHECK : 0; |
| 706 | /* When socket is gone, all binding information is lost. |
| 707 | * routing might fail in this case. No choice here, if we choose to force |
| 708 | * input interface, we will misroute in case of asymmetric route. |
| 709 | */ |
| 710 | if (sk) |
| 711 | arg.bound_dev_if = sk->sk_bound_dev_if; |
| 712 | |
| 713 | arg.tos = ip_hdr(skb)->tos; |
| 714 | arg.uid = sock_net_uid(net, sk && sk_fullsock(sk) ? sk : NULL); |
| 715 | ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk), |
| 716 | skb, ip_hdr(skb)->saddr, |
| 717 | ip_hdr(skb)->daddr, &arg, arg.iov[0].iov_len); |
| 718 | |
| 719 | TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS); |
| 720 | TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS); |
| 721 | |
| 722 | #ifdef CONFIG_TCP_MD5SIG |
| 723 | release_sk1: |
| 724 | if (sk1) { |
| 725 | rcu_read_unlock(); |
| 726 | sock_put(sk1); |
| 727 | } |
| 728 | #endif |
| 729 | } |
| 730 | |
| 731 | /* The code following below sending ACKs in SYN-RECV and TIME-WAIT states |
| 732 | outside socket context is ugly, certainly. What can I do? |
| 733 | */ |
| 734 | |
| 735 | static void tcp_v4_send_ack(const struct sock *sk, struct sk_buff *skb, |
| 736 | u32 seq, u32 ack, |
| 737 | u32 win, u32 tsval, u32 tsecr, int oif, |
| 738 | struct tcp_md5sig_key *key, |
| 739 | int reply_flags, u8 tos) |
| 740 | { |
| 741 | const struct tcphdr *th = tcp_hdr(skb); |
| 742 | struct { |
| 743 | struct tcphdr th; |
| 744 | __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2) |
| 745 | #ifdef CONFIG_TCP_MD5SIG |
| 746 | + (TCPOLEN_MD5SIG_ALIGNED >> 2) |
| 747 | #endif |
| 748 | ]; |
| 749 | } rep; |
| 750 | struct ip_reply_arg arg; |
| 751 | struct net *net = sock_net(sk); |
| 752 | |
| 753 | memset(&rep.th, 0, sizeof(struct tcphdr)); |
| 754 | memset(&arg, 0, sizeof(arg)); |
| 755 | |
| 756 | arg.iov[0].iov_base = (unsigned char *)&rep; |
| 757 | arg.iov[0].iov_len = sizeof(rep.th); |
| 758 | if (tsecr) { |
| 759 | rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | |
| 760 | (TCPOPT_TIMESTAMP << 8) | |
| 761 | TCPOLEN_TIMESTAMP); |
| 762 | rep.opt[1] = htonl(tsval); |
| 763 | rep.opt[2] = htonl(tsecr); |
| 764 | arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED; |
| 765 | } |
| 766 | |
| 767 | /* Swap the send and the receive. */ |
| 768 | rep.th.dest = th->source; |
| 769 | rep.th.source = th->dest; |
| 770 | rep.th.doff = arg.iov[0].iov_len / 4; |
| 771 | rep.th.seq = htonl(seq); |
| 772 | rep.th.ack_seq = htonl(ack); |
| 773 | rep.th.ack = 1; |
| 774 | rep.th.window = htons(win); |
| 775 | |
| 776 | #ifdef CONFIG_TCP_MD5SIG |
| 777 | if (key) { |
| 778 | int offset = (tsecr) ? 3 : 0; |
| 779 | |
| 780 | rep.opt[offset++] = htonl((TCPOPT_NOP << 24) | |
| 781 | (TCPOPT_NOP << 16) | |
| 782 | (TCPOPT_MD5SIG << 8) | |
| 783 | TCPOLEN_MD5SIG); |
| 784 | arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED; |
| 785 | rep.th.doff = arg.iov[0].iov_len/4; |
| 786 | |
| 787 | tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[offset], |
| 788 | key, ip_hdr(skb)->saddr, |
| 789 | ip_hdr(skb)->daddr, &rep.th); |
| 790 | } |
| 791 | #endif |
| 792 | arg.flags = reply_flags; |
| 793 | arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr, |
| 794 | ip_hdr(skb)->saddr, /* XXX */ |
| 795 | arg.iov[0].iov_len, IPPROTO_TCP, 0); |
| 796 | arg.csumoffset = offsetof(struct tcphdr, check) / 2; |
| 797 | if (oif) |
| 798 | arg.bound_dev_if = oif; |
| 799 | arg.tos = tos; |
| 800 | arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL); |
| 801 | ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk), |
| 802 | skb, ip_hdr(skb)->saddr, |
| 803 | ip_hdr(skb)->daddr, &arg, arg.iov[0].iov_len); |
| 804 | |
| 805 | TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS); |
| 806 | } |
| 807 | |
| 808 | static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb) |
| 809 | { |
| 810 | struct inet_timewait_sock *tw = inet_twsk(sk); |
| 811 | struct tcp_timewait_sock *tcptw = tcp_twsk(sk); |
| 812 | |
| 813 | tcp_v4_send_ack(sk, skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt, |
| 814 | tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale, |
| 815 | tcp_time_stamp + tcptw->tw_ts_offset, |
| 816 | tcptw->tw_ts_recent, |
| 817 | tw->tw_bound_dev_if, |
| 818 | tcp_twsk_md5_key(tcptw), |
| 819 | tw->tw_transparent ? IP_REPLY_ARG_NOSRCCHECK : 0, |
| 820 | tw->tw_tos |
| 821 | ); |
| 822 | |
| 823 | inet_twsk_put(tw); |
| 824 | } |
| 825 | |
| 826 | static void tcp_v4_reqsk_send_ack(struct sock *sk, struct sk_buff *skb, |
| 827 | struct request_sock *req) |
| 828 | { |
| 829 | /* sk->sk_state == TCP_LISTEN -> for regular TCP_SYN_RECV |
| 830 | * sk->sk_state == TCP_SYN_RECV -> for Fast Open. |
| 831 | */ |
| 832 | tcp_v4_send_ack(sk, skb, (sk->sk_state == TCP_LISTEN) ? |
| 833 | tcp_rsk(req)->snt_isn + 1 : tcp_sk(sk)->snd_nxt, |
| 834 | tcp_rsk(req)->rcv_nxt, |
| 835 | req->rcv_wnd >> inet_rsk(req)->rcv_wscale, |
| 836 | tcp_time_stamp, |
| 837 | req->ts_recent, |
| 838 | 0, |
| 839 | tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&ip_hdr(skb)->daddr, |
| 840 | AF_INET), |
| 841 | inet_rsk(req)->no_srccheck ? IP_REPLY_ARG_NOSRCCHECK : 0, |
| 842 | ip_hdr(skb)->tos); |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * Send a SYN-ACK after having received a SYN. |
| 847 | * This still operates on a request_sock only, not on a big |
| 848 | * socket. |
| 849 | */ |
| 850 | static int tcp_v4_send_synack(struct sock *sk, struct dst_entry *dst, |
| 851 | struct request_sock *req, |
| 852 | u16 queue_mapping, |
| 853 | bool nocache) |
| 854 | { |
| 855 | const struct inet_request_sock *ireq = inet_rsk(req); |
| 856 | struct flowi4 fl4; |
| 857 | int err = -1; |
| 858 | struct sk_buff * skb; |
| 859 | |
| 860 | /* First, grab a route. */ |
| 861 | if (!dst && (dst = inet_csk_route_req(sk, &fl4, req)) == NULL) |
| 862 | return -1; |
| 863 | |
| 864 | skb = tcp_make_synack(sk, dst, req, NULL); |
| 865 | |
| 866 | if (skb) { |
| 867 | __tcp_v4_send_check(skb, ireq->loc_addr, ireq->rmt_addr); |
| 868 | |
| 869 | skb_set_queue_mapping(skb, queue_mapping); |
| 870 | err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr, |
| 871 | ireq->rmt_addr, |
| 872 | ireq->opt); |
| 873 | err = net_xmit_eval(err); |
| 874 | if (!tcp_rsk(req)->snt_synack && !err) |
| 875 | tcp_rsk(req)->snt_synack = tcp_time_stamp; |
| 876 | } |
| 877 | |
| 878 | return err; |
| 879 | } |
| 880 | |
| 881 | static int tcp_v4_rtx_synack(struct sock *sk, struct request_sock *req) |
| 882 | { |
| 883 | int res = tcp_v4_send_synack(sk, NULL, req, 0, false); |
| 884 | |
| 885 | if (!res) |
| 886 | TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_RETRANSSEGS); |
| 887 | return res; |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * IPv4 request_sock destructor. |
| 892 | */ |
| 893 | static void tcp_v4_reqsk_destructor(struct request_sock *req) |
| 894 | { |
| 895 | kfree(inet_rsk(req)->opt); |
| 896 | } |
| 897 | |
| 898 | /* |
| 899 | * Return true if a syncookie should be sent |
| 900 | */ |
| 901 | bool tcp_syn_flood_action(struct sock *sk, |
| 902 | const struct sk_buff *skb, |
| 903 | const char *proto) |
| 904 | { |
| 905 | const char *msg = "Dropping request"; |
| 906 | bool want_cookie = false; |
| 907 | struct listen_sock *lopt; |
| 908 | |
| 909 | |
| 910 | |
| 911 | #ifdef CONFIG_SYN_COOKIES |
| 912 | if (sysctl_tcp_syncookies) { |
| 913 | msg = "Sending cookies"; |
| 914 | want_cookie = true; |
| 915 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES); |
| 916 | } else |
| 917 | #endif |
| 918 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP); |
| 919 | |
| 920 | lopt = inet_csk(sk)->icsk_accept_queue.listen_opt; |
| 921 | if (!lopt->synflood_warned) { |
| 922 | lopt->synflood_warned = 1; |
| 923 | pr_info("%s: Possible SYN flooding on port %d. %s. Check SNMP counters.\n", |
| 924 | proto, ntohs(tcp_hdr(skb)->dest), msg); |
| 925 | } |
| 926 | return want_cookie; |
| 927 | } |
| 928 | EXPORT_SYMBOL(tcp_syn_flood_action); |
| 929 | |
| 930 | /* |
| 931 | * Save and compile IPv4 options into the request_sock if needed. |
| 932 | */ |
| 933 | static struct ip_options_rcu *tcp_v4_save_options(struct sk_buff *skb) |
| 934 | { |
| 935 | const struct ip_options *opt = &(IPCB(skb)->opt); |
| 936 | struct ip_options_rcu *dopt = NULL; |
| 937 | |
| 938 | if (opt && opt->optlen) { |
| 939 | int opt_size = sizeof(*dopt) + opt->optlen; |
| 940 | |
| 941 | dopt = kmalloc(opt_size, GFP_ATOMIC); |
| 942 | if (dopt) { |
| 943 | if (ip_options_echo(&dopt->opt, skb)) { |
| 944 | kfree(dopt); |
| 945 | dopt = NULL; |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | return dopt; |
| 950 | } |
| 951 | |
| 952 | #ifdef CONFIG_TCP_MD5SIG |
| 953 | /* |
| 954 | * RFC2385 MD5 checksumming requires a mapping of |
| 955 | * IP address->MD5 Key. |
| 956 | * We need to maintain these in the sk structure. |
| 957 | */ |
| 958 | |
| 959 | /* Find the Key structure for an address. */ |
| 960 | struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk, |
| 961 | const union tcp_md5_addr *addr, |
| 962 | int family) |
| 963 | { |
| 964 | struct tcp_sock *tp = tcp_sk(sk); |
| 965 | struct tcp_md5sig_key *key; |
| 966 | unsigned int size = sizeof(struct in_addr); |
| 967 | struct tcp_md5sig_info *md5sig; |
| 968 | |
| 969 | /* caller either holds rcu_read_lock() or socket lock */ |
| 970 | md5sig = rcu_dereference_check(tp->md5sig_info, |
| 971 | sock_owned_by_user(sk) || |
| 972 | lockdep_is_held(&sk->sk_lock.slock)); |
| 973 | if (!md5sig) |
| 974 | return NULL; |
| 975 | #if IS_ENABLED(CONFIG_IPV6) |
| 976 | if (family == AF_INET6) |
| 977 | size = sizeof(struct in6_addr); |
| 978 | #endif |
| 979 | hlist_for_each_entry_rcu(key, &md5sig->head, node) { |
| 980 | if (key->family != family) |
| 981 | continue; |
| 982 | if (!memcmp(&key->addr, addr, size)) |
| 983 | return key; |
| 984 | } |
| 985 | return NULL; |
| 986 | } |
| 987 | EXPORT_SYMBOL(tcp_md5_do_lookup); |
| 988 | |
| 989 | struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk, |
| 990 | struct sock *addr_sk) |
| 991 | { |
| 992 | union tcp_md5_addr *addr; |
| 993 | |
| 994 | addr = (union tcp_md5_addr *)&inet_sk(addr_sk)->inet_daddr; |
| 995 | return tcp_md5_do_lookup(sk, addr, AF_INET); |
| 996 | } |
| 997 | EXPORT_SYMBOL(tcp_v4_md5_lookup); |
| 998 | |
| 999 | static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk, |
| 1000 | struct request_sock *req) |
| 1001 | { |
| 1002 | union tcp_md5_addr *addr; |
| 1003 | |
| 1004 | addr = (union tcp_md5_addr *)&inet_rsk(req)->rmt_addr; |
| 1005 | return tcp_md5_do_lookup(sk, addr, AF_INET); |
| 1006 | } |
| 1007 | |
| 1008 | /* This can be called on a newly created socket, from other files */ |
| 1009 | int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr, |
| 1010 | int family, const u8 *newkey, u8 newkeylen, gfp_t gfp) |
| 1011 | { |
| 1012 | /* Add Key to the list */ |
| 1013 | struct tcp_md5sig_key *key; |
| 1014 | struct tcp_sock *tp = tcp_sk(sk); |
| 1015 | struct tcp_md5sig_info *md5sig; |
| 1016 | |
| 1017 | key = tcp_md5_do_lookup(sk, addr, family); |
| 1018 | if (key) { |
| 1019 | /* Pre-existing entry - just update that one. */ |
| 1020 | memcpy(key->key, newkey, newkeylen); |
| 1021 | key->keylen = newkeylen; |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
| 1025 | md5sig = rcu_dereference_protected(tp->md5sig_info, |
| 1026 | sock_owned_by_user(sk) || |
| 1027 | lockdep_is_held(&sk->sk_lock.slock)); |
| 1028 | if (!md5sig) { |
| 1029 | md5sig = kmalloc(sizeof(*md5sig), gfp); |
| 1030 | if (!md5sig) |
| 1031 | return -ENOMEM; |
| 1032 | |
| 1033 | sk_nocaps_add(sk, NETIF_F_GSO_MASK); |
| 1034 | INIT_HLIST_HEAD(&md5sig->head); |
| 1035 | rcu_assign_pointer(tp->md5sig_info, md5sig); |
| 1036 | } |
| 1037 | |
| 1038 | key = sock_kmalloc(sk, sizeof(*key), gfp); |
| 1039 | if (!key) |
| 1040 | return -ENOMEM; |
| 1041 | if (hlist_empty(&md5sig->head) && !tcp_alloc_md5sig_pool(sk)) { |
| 1042 | sock_kfree_s(sk, key, sizeof(*key)); |
| 1043 | return -ENOMEM; |
| 1044 | } |
| 1045 | |
| 1046 | memcpy(key->key, newkey, newkeylen); |
| 1047 | key->keylen = newkeylen; |
| 1048 | key->family = family; |
| 1049 | memcpy(&key->addr, addr, |
| 1050 | (family == AF_INET6) ? sizeof(struct in6_addr) : |
| 1051 | sizeof(struct in_addr)); |
| 1052 | hlist_add_head_rcu(&key->node, &md5sig->head); |
| 1053 | return 0; |
| 1054 | } |
| 1055 | EXPORT_SYMBOL(tcp_md5_do_add); |
| 1056 | |
| 1057 | int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr, int family) |
| 1058 | { |
| 1059 | struct tcp_sock *tp = tcp_sk(sk); |
| 1060 | struct tcp_md5sig_key *key; |
| 1061 | struct tcp_md5sig_info *md5sig; |
| 1062 | |
| 1063 | key = tcp_md5_do_lookup(sk, addr, family); |
| 1064 | if (!key) |
| 1065 | return -ENOENT; |
| 1066 | hlist_del_rcu(&key->node); |
| 1067 | atomic_sub(sizeof(*key), &sk->sk_omem_alloc); |
| 1068 | kfree_rcu(key, rcu); |
| 1069 | md5sig = rcu_dereference_protected(tp->md5sig_info, |
| 1070 | sock_owned_by_user(sk)); |
| 1071 | if (hlist_empty(&md5sig->head)) |
| 1072 | tcp_free_md5sig_pool(); |
| 1073 | return 0; |
| 1074 | } |
| 1075 | EXPORT_SYMBOL(tcp_md5_do_del); |
| 1076 | |
| 1077 | static void tcp_clear_md5_list(struct sock *sk) |
| 1078 | { |
| 1079 | struct tcp_sock *tp = tcp_sk(sk); |
| 1080 | struct tcp_md5sig_key *key; |
| 1081 | struct hlist_node *n; |
| 1082 | struct tcp_md5sig_info *md5sig; |
| 1083 | |
| 1084 | md5sig = rcu_dereference_protected(tp->md5sig_info, 1); |
| 1085 | |
| 1086 | if (!hlist_empty(&md5sig->head)) |
| 1087 | tcp_free_md5sig_pool(); |
| 1088 | hlist_for_each_entry_safe(key, n, &md5sig->head, node) { |
| 1089 | hlist_del_rcu(&key->node); |
| 1090 | atomic_sub(sizeof(*key), &sk->sk_omem_alloc); |
| 1091 | kfree_rcu(key, rcu); |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval, |
| 1096 | int optlen) |
| 1097 | { |
| 1098 | struct tcp_md5sig cmd; |
| 1099 | struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr; |
| 1100 | |
| 1101 | if (optlen < sizeof(cmd)) |
| 1102 | return -EINVAL; |
| 1103 | |
| 1104 | if (copy_from_user(&cmd, optval, sizeof(cmd))) |
| 1105 | return -EFAULT; |
| 1106 | |
| 1107 | if (sin->sin_family != AF_INET) |
| 1108 | return -EINVAL; |
| 1109 | |
| 1110 | if (!cmd.tcpm_key || !cmd.tcpm_keylen) |
| 1111 | return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin->sin_addr.s_addr, |
| 1112 | AF_INET); |
| 1113 | |
| 1114 | if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN) |
| 1115 | return -EINVAL; |
| 1116 | |
| 1117 | return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin->sin_addr.s_addr, |
| 1118 | AF_INET, cmd.tcpm_key, cmd.tcpm_keylen, |
| 1119 | GFP_KERNEL); |
| 1120 | } |
| 1121 | |
| 1122 | static int tcp_v4_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp, |
| 1123 | __be32 daddr, __be32 saddr, int nbytes) |
| 1124 | { |
| 1125 | struct tcp4_pseudohdr *bp; |
| 1126 | struct scatterlist sg; |
| 1127 | |
| 1128 | bp = &hp->md5_blk.ip4; |
| 1129 | |
| 1130 | /* |
| 1131 | * 1. the TCP pseudo-header (in the order: source IP address, |
| 1132 | * destination IP address, zero-padded protocol number, and |
| 1133 | * segment length) |
| 1134 | */ |
| 1135 | bp->saddr = saddr; |
| 1136 | bp->daddr = daddr; |
| 1137 | bp->pad = 0; |
| 1138 | bp->protocol = IPPROTO_TCP; |
| 1139 | bp->len = cpu_to_be16(nbytes); |
| 1140 | |
| 1141 | sg_init_one(&sg, bp, sizeof(*bp)); |
| 1142 | return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp)); |
| 1143 | } |
| 1144 | |
| 1145 | static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key, |
| 1146 | __be32 daddr, __be32 saddr, const struct tcphdr *th) |
| 1147 | { |
| 1148 | struct tcp_md5sig_pool *hp; |
| 1149 | struct hash_desc *desc; |
| 1150 | |
| 1151 | hp = tcp_get_md5sig_pool(); |
| 1152 | if (!hp) |
| 1153 | goto clear_hash_noput; |
| 1154 | desc = &hp->md5_desc; |
| 1155 | |
| 1156 | if (crypto_hash_init(desc)) |
| 1157 | goto clear_hash; |
| 1158 | if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2)) |
| 1159 | goto clear_hash; |
| 1160 | if (tcp_md5_hash_header(hp, th)) |
| 1161 | goto clear_hash; |
| 1162 | if (tcp_md5_hash_key(hp, key)) |
| 1163 | goto clear_hash; |
| 1164 | if (crypto_hash_final(desc, md5_hash)) |
| 1165 | goto clear_hash; |
| 1166 | |
| 1167 | tcp_put_md5sig_pool(); |
| 1168 | return 0; |
| 1169 | |
| 1170 | clear_hash: |
| 1171 | tcp_put_md5sig_pool(); |
| 1172 | clear_hash_noput: |
| 1173 | memset(md5_hash, 0, 16); |
| 1174 | return 1; |
| 1175 | } |
| 1176 | |
| 1177 | int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key, |
| 1178 | const struct sock *sk, const struct request_sock *req, |
| 1179 | const struct sk_buff *skb) |
| 1180 | { |
| 1181 | struct tcp_md5sig_pool *hp; |
| 1182 | struct hash_desc *desc; |
| 1183 | const struct tcphdr *th = tcp_hdr(skb); |
| 1184 | __be32 saddr, daddr; |
| 1185 | |
| 1186 | if (sk) { |
| 1187 | saddr = inet_sk(sk)->inet_saddr; |
| 1188 | daddr = inet_sk(sk)->inet_daddr; |
| 1189 | } else if (req) { |
| 1190 | saddr = inet_rsk(req)->loc_addr; |
| 1191 | daddr = inet_rsk(req)->rmt_addr; |
| 1192 | } else { |
| 1193 | const struct iphdr *iph = ip_hdr(skb); |
| 1194 | saddr = iph->saddr; |
| 1195 | daddr = iph->daddr; |
| 1196 | } |
| 1197 | |
| 1198 | hp = tcp_get_md5sig_pool(); |
| 1199 | if (!hp) |
| 1200 | goto clear_hash_noput; |
| 1201 | desc = &hp->md5_desc; |
| 1202 | |
| 1203 | if (crypto_hash_init(desc)) |
| 1204 | goto clear_hash; |
| 1205 | |
| 1206 | if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, skb->len)) |
| 1207 | goto clear_hash; |
| 1208 | if (tcp_md5_hash_header(hp, th)) |
| 1209 | goto clear_hash; |
| 1210 | if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2)) |
| 1211 | goto clear_hash; |
| 1212 | if (tcp_md5_hash_key(hp, key)) |
| 1213 | goto clear_hash; |
| 1214 | if (crypto_hash_final(desc, md5_hash)) |
| 1215 | goto clear_hash; |
| 1216 | |
| 1217 | tcp_put_md5sig_pool(); |
| 1218 | return 0; |
| 1219 | |
| 1220 | clear_hash: |
| 1221 | tcp_put_md5sig_pool(); |
| 1222 | clear_hash_noput: |
| 1223 | memset(md5_hash, 0, 16); |
| 1224 | return 1; |
| 1225 | } |
| 1226 | EXPORT_SYMBOL(tcp_v4_md5_hash_skb); |
| 1227 | |
| 1228 | static bool tcp_v4_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb) |
| 1229 | { |
| 1230 | /* |
| 1231 | * This gets called for each TCP segment that arrives |
| 1232 | * so we want to be efficient. |
| 1233 | * We have 3 drop cases: |
| 1234 | * o No MD5 hash and one expected. |
| 1235 | * o MD5 hash and we're not expecting one. |
| 1236 | * o MD5 hash and its wrong. |
| 1237 | */ |
| 1238 | const __u8 *hash_location = NULL; |
| 1239 | struct tcp_md5sig_key *hash_expected; |
| 1240 | const struct iphdr *iph = ip_hdr(skb); |
| 1241 | const struct tcphdr *th = tcp_hdr(skb); |
| 1242 | int genhash; |
| 1243 | unsigned char newhash[16]; |
| 1244 | |
| 1245 | hash_expected = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&iph->saddr, |
| 1246 | AF_INET); |
| 1247 | hash_location = tcp_parse_md5sig_option(th); |
| 1248 | |
| 1249 | /* We've parsed the options - do we have a hash? */ |
| 1250 | if (!hash_expected && !hash_location) |
| 1251 | return false; |
| 1252 | |
| 1253 | if (hash_expected && !hash_location) { |
| 1254 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND); |
| 1255 | return true; |
| 1256 | } |
| 1257 | |
| 1258 | if (!hash_expected && hash_location) { |
| 1259 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED); |
| 1260 | return true; |
| 1261 | } |
| 1262 | |
| 1263 | /* Okay, so this is hash_expected and hash_location - |
| 1264 | * so we need to calculate the checksum. |
| 1265 | */ |
| 1266 | genhash = tcp_v4_md5_hash_skb(newhash, |
| 1267 | hash_expected, |
| 1268 | NULL, NULL, skb); |
| 1269 | |
| 1270 | if (genhash || memcmp(hash_location, newhash, 16) != 0) { |
| 1271 | net_info_ratelimited("MD5 Hash failed for (%pI4, %d)->(%pI4, %d)%s\n", |
| 1272 | &iph->saddr, ntohs(th->source), |
| 1273 | &iph->daddr, ntohs(th->dest), |
| 1274 | genhash ? " tcp_v4_calc_md5_hash failed" |
| 1275 | : ""); |
| 1276 | return true; |
| 1277 | } |
| 1278 | return false; |
| 1279 | } |
| 1280 | |
| 1281 | #endif |
| 1282 | |
| 1283 | struct request_sock_ops tcp_request_sock_ops __read_mostly = { |
| 1284 | .family = PF_INET, |
| 1285 | .obj_size = sizeof(struct tcp_request_sock), |
| 1286 | .rtx_syn_ack = tcp_v4_rtx_synack, |
| 1287 | .send_ack = tcp_v4_reqsk_send_ack, |
| 1288 | .destructor = tcp_v4_reqsk_destructor, |
| 1289 | .send_reset = tcp_v4_send_reset, |
| 1290 | .syn_ack_timeout = tcp_syn_ack_timeout, |
| 1291 | }; |
| 1292 | |
| 1293 | #ifdef CONFIG_TCP_MD5SIG |
| 1294 | static const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = { |
| 1295 | .md5_lookup = tcp_v4_reqsk_md5_lookup, |
| 1296 | .calc_md5_hash = tcp_v4_md5_hash_skb, |
| 1297 | }; |
| 1298 | #endif |
| 1299 | |
| 1300 | static bool tcp_fastopen_check(struct sock *sk, struct sk_buff *skb, |
| 1301 | struct request_sock *req, |
| 1302 | struct tcp_fastopen_cookie *foc, |
| 1303 | struct tcp_fastopen_cookie *valid_foc) |
| 1304 | { |
| 1305 | bool skip_cookie = false; |
| 1306 | struct fastopen_queue *fastopenq; |
| 1307 | |
| 1308 | if (likely(!fastopen_cookie_present(foc))) { |
| 1309 | /* See include/net/tcp.h for the meaning of these knobs */ |
| 1310 | if ((sysctl_tcp_fastopen & TFO_SERVER_ALWAYS) || |
| 1311 | ((sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_REQD) && |
| 1312 | (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1))) |
| 1313 | skip_cookie = true; /* no cookie to validate */ |
| 1314 | else |
| 1315 | return false; |
| 1316 | } |
| 1317 | fastopenq = inet_csk(sk)->icsk_accept_queue.fastopenq; |
| 1318 | /* A FO option is present; bump the counter. */ |
| 1319 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVE); |
| 1320 | |
| 1321 | /* Make sure the listener has enabled fastopen, and we don't |
| 1322 | * exceed the max # of pending TFO requests allowed before trying |
| 1323 | * to validating the cookie in order to avoid burning CPU cycles |
| 1324 | * unnecessarily. |
| 1325 | * |
| 1326 | * XXX (TFO) - The implication of checking the max_qlen before |
| 1327 | * processing a cookie request is that clients can't differentiate |
| 1328 | * between qlen overflow causing Fast Open to be disabled |
| 1329 | * temporarily vs a server not supporting Fast Open at all. |
| 1330 | */ |
| 1331 | if ((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) == 0 || |
| 1332 | fastopenq == NULL || fastopenq->max_qlen == 0) |
| 1333 | return false; |
| 1334 | |
| 1335 | if (fastopenq->qlen >= fastopenq->max_qlen) { |
| 1336 | struct request_sock *req1; |
| 1337 | spin_lock(&fastopenq->lock); |
| 1338 | req1 = fastopenq->rskq_rst_head; |
| 1339 | if ((req1 == NULL) || time_after(req1->expires, jiffies)) { |
| 1340 | spin_unlock(&fastopenq->lock); |
| 1341 | NET_INC_STATS_BH(sock_net(sk), |
| 1342 | LINUX_MIB_TCPFASTOPENLISTENOVERFLOW); |
| 1343 | /* Avoid bumping LINUX_MIB_TCPFASTOPENPASSIVEFAIL*/ |
| 1344 | foc->len = -1; |
| 1345 | return false; |
| 1346 | } |
| 1347 | fastopenq->rskq_rst_head = req1->dl_next; |
| 1348 | fastopenq->qlen--; |
| 1349 | spin_unlock(&fastopenq->lock); |
| 1350 | reqsk_free(req1); |
| 1351 | } |
| 1352 | if (skip_cookie) { |
| 1353 | tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->end_seq; |
| 1354 | return true; |
| 1355 | } |
| 1356 | if (foc->len == TCP_FASTOPEN_COOKIE_SIZE) { |
| 1357 | if ((sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_CHKED) == 0) { |
| 1358 | tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr, valid_foc); |
| 1359 | if ((valid_foc->len != TCP_FASTOPEN_COOKIE_SIZE) || |
| 1360 | memcmp(&foc->val[0], &valid_foc->val[0], |
| 1361 | TCP_FASTOPEN_COOKIE_SIZE) != 0) |
| 1362 | return false; |
| 1363 | valid_foc->len = -1; |
| 1364 | } |
| 1365 | /* Acknowledge the data received from the peer. */ |
| 1366 | tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->end_seq; |
| 1367 | return true; |
| 1368 | } else if (foc->len == 0) { /* Client requesting a cookie */ |
| 1369 | tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr, valid_foc); |
| 1370 | NET_INC_STATS_BH(sock_net(sk), |
| 1371 | LINUX_MIB_TCPFASTOPENCOOKIEREQD); |
| 1372 | } else { |
| 1373 | /* Client sent a cookie with wrong size. Treat it |
| 1374 | * the same as invalid and return a valid one. |
| 1375 | */ |
| 1376 | tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr, valid_foc); |
| 1377 | } |
| 1378 | return false; |
| 1379 | } |
| 1380 | |
| 1381 | static int tcp_v4_conn_req_fastopen(struct sock *sk, |
| 1382 | struct sk_buff *skb, |
| 1383 | struct sk_buff *skb_synack, |
| 1384 | struct request_sock *req) |
| 1385 | { |
| 1386 | struct tcp_sock *tp = tcp_sk(sk); |
| 1387 | struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue; |
| 1388 | const struct inet_request_sock *ireq = inet_rsk(req); |
| 1389 | struct sock *child; |
| 1390 | int err; |
| 1391 | |
| 1392 | req->num_retrans = 0; |
| 1393 | req->num_timeout = 0; |
| 1394 | req->sk = NULL; |
| 1395 | |
| 1396 | child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL); |
| 1397 | if (child == NULL) { |
| 1398 | NET_INC_STATS_BH(sock_net(sk), |
| 1399 | LINUX_MIB_TCPFASTOPENPASSIVEFAIL); |
| 1400 | kfree_skb(skb_synack); |
| 1401 | return -1; |
| 1402 | } |
| 1403 | err = ip_build_and_send_pkt(skb_synack, sk, ireq->loc_addr, |
| 1404 | ireq->rmt_addr, ireq->opt); |
| 1405 | err = net_xmit_eval(err); |
| 1406 | if (!err) |
| 1407 | tcp_rsk(req)->snt_synack = tcp_time_stamp; |
| 1408 | /* XXX (TFO) - is it ok to ignore error and continue? */ |
| 1409 | |
| 1410 | spin_lock(&queue->fastopenq->lock); |
| 1411 | queue->fastopenq->qlen++; |
| 1412 | spin_unlock(&queue->fastopenq->lock); |
| 1413 | |
| 1414 | /* Initialize the child socket. Have to fix some values to take |
| 1415 | * into account the child is a Fast Open socket and is created |
| 1416 | * only out of the bits carried in the SYN packet. |
| 1417 | */ |
| 1418 | tp = tcp_sk(child); |
| 1419 | |
| 1420 | tp->fastopen_rsk = req; |
| 1421 | /* Do a hold on the listner sk so that if the listener is being |
| 1422 | * closed, the child that has been accepted can live on and still |
| 1423 | * access listen_lock. |
| 1424 | */ |
| 1425 | sock_hold(sk); |
| 1426 | tcp_rsk(req)->listener = sk; |
| 1427 | |
| 1428 | /* RFC1323: The window in SYN & SYN/ACK segments is never |
| 1429 | * scaled. So correct it appropriately. |
| 1430 | */ |
| 1431 | tp->snd_wnd = ntohs(tcp_hdr(skb)->window); |
| 1432 | tp->max_window = tp->snd_wnd; |
| 1433 | |
| 1434 | /* Activate the retrans timer so that SYNACK can be retransmitted. |
| 1435 | * The request socket is not added to the SYN table of the parent |
| 1436 | * because it's been added to the accept queue directly. |
| 1437 | */ |
| 1438 | inet_csk_reset_xmit_timer(child, ICSK_TIME_RETRANS, |
| 1439 | TCP_TIMEOUT_INIT, TCP_RTO_MAX); |
| 1440 | |
| 1441 | /* Add the child socket directly into the accept queue */ |
| 1442 | inet_csk_reqsk_queue_add(sk, req, child); |
| 1443 | |
| 1444 | /* Now finish processing the fastopen child socket. */ |
| 1445 | inet_csk(child)->icsk_af_ops->rebuild_header(child); |
| 1446 | tcp_init_congestion_control(child); |
| 1447 | tcp_mtup_init(child); |
| 1448 | tcp_init_buffer_space(child); |
| 1449 | tcp_init_metrics(child); |
| 1450 | |
| 1451 | /* Queue the data carried in the SYN packet. We need to first |
| 1452 | * bump skb's refcnt because the caller will attempt to free it. |
| 1453 | * |
| 1454 | * XXX (TFO) - we honor a zero-payload TFO request for now. |
| 1455 | * (Any reason not to?) |
| 1456 | */ |
| 1457 | if (TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq + 1) { |
| 1458 | /* Don't queue the skb if there is no payload in SYN. |
| 1459 | * XXX (TFO) - How about SYN+FIN? |
| 1460 | */ |
| 1461 | tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; |
| 1462 | } else { |
| 1463 | skb = skb_get(skb); |
| 1464 | skb_dst_drop(skb); |
| 1465 | __skb_pull(skb, tcp_hdr(skb)->doff * 4); |
| 1466 | skb_set_owner_r(skb, child); |
| 1467 | __skb_queue_tail(&child->sk_receive_queue, skb); |
| 1468 | tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq; |
| 1469 | tp->syn_data_acked = 1; |
| 1470 | tp->bytes_received = TCP_SKB_CB(skb)->end_seq - TCP_SKB_CB(skb)->seq - 1; |
| 1471 | } |
| 1472 | sk->sk_data_ready(sk); |
| 1473 | bh_unlock_sock(child); |
| 1474 | sock_put(child); |
| 1475 | WARN_ON(req->sk == NULL); |
| 1476 | return 0; |
| 1477 | } |
| 1478 | |
| 1479 | int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb) |
| 1480 | { |
| 1481 | struct tcp_options_received tmp_opt; |
| 1482 | struct request_sock *req; |
| 1483 | struct inet_request_sock *ireq; |
| 1484 | struct tcp_sock *tp = tcp_sk(sk); |
| 1485 | struct dst_entry *dst = NULL; |
| 1486 | __be32 saddr = ip_hdr(skb)->saddr; |
| 1487 | __be32 daddr = ip_hdr(skb)->daddr; |
| 1488 | __u32 isn = TCP_SKB_CB(skb)->when; |
| 1489 | bool want_cookie = false; |
| 1490 | struct flowi4 fl4; |
| 1491 | struct tcp_fastopen_cookie foc = { .len = -1 }; |
| 1492 | struct tcp_fastopen_cookie valid_foc = { .len = -1 }; |
| 1493 | struct sk_buff *skb_synack; |
| 1494 | int do_fastopen; |
| 1495 | |
| 1496 | /* Never answer to SYNs send to broadcast or multicast */ |
| 1497 | if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) |
| 1498 | goto drop; |
| 1499 | |
| 1500 | /* TW buckets are converted to open requests without |
| 1501 | * limitations, they conserve resources and peer is |
| 1502 | * evidently real one. |
| 1503 | */ |
| 1504 | if (inet_csk_reqsk_queue_is_full(sk) && !isn) { |
| 1505 | want_cookie = tcp_syn_flood_action(sk, skb, "TCP"); |
| 1506 | if (!want_cookie) |
| 1507 | goto drop; |
| 1508 | } |
| 1509 | |
| 1510 | /* Accept backlog is full. If we have already queued enough |
| 1511 | * of warm entries in syn queue, drop request. It is better than |
| 1512 | * clogging syn queue with openreqs with exponentially increasing |
| 1513 | * timeout. |
| 1514 | */ |
| 1515 | if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1) { |
| 1516 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS); |
| 1517 | goto drop; |
| 1518 | } |
| 1519 | |
| 1520 | req = inet_reqsk_alloc(&tcp_request_sock_ops); |
| 1521 | if (!req) |
| 1522 | goto drop; |
| 1523 | |
| 1524 | #ifdef CONFIG_TCP_MD5SIG |
| 1525 | tcp_rsk(req)->af_specific = &tcp_request_sock_ipv4_ops; |
| 1526 | #endif |
| 1527 | |
| 1528 | tcp_clear_options(&tmp_opt); |
| 1529 | tmp_opt.mss_clamp = TCP_MSS_DEFAULT; |
| 1530 | tmp_opt.user_mss = tp->rx_opt.user_mss; |
| 1531 | tcp_parse_options(skb, &tmp_opt, 0, want_cookie ? NULL : &foc); |
| 1532 | |
| 1533 | if (want_cookie && !tmp_opt.saw_tstamp) |
| 1534 | tcp_clear_options(&tmp_opt); |
| 1535 | |
| 1536 | tmp_opt.tstamp_ok = tmp_opt.saw_tstamp; |
| 1537 | tcp_openreq_init(req, &tmp_opt, skb); |
| 1538 | |
| 1539 | ireq = inet_rsk(req); |
| 1540 | ireq->loc_addr = daddr; |
| 1541 | ireq->rmt_addr = saddr; |
| 1542 | ireq->no_srccheck = inet_sk(sk)->transparent; |
| 1543 | ireq->opt = tcp_v4_save_options(skb); |
| 1544 | ireq->ir_mark = inet_request_mark(sk, skb); |
| 1545 | |
| 1546 | if (security_inet_conn_request(sk, skb, req)) |
| 1547 | goto drop_and_free; |
| 1548 | |
| 1549 | if (!want_cookie || tmp_opt.tstamp_ok) |
| 1550 | TCP_ECN_create_request(req, skb, sock_net(sk)); |
| 1551 | |
| 1552 | if (want_cookie) { |
| 1553 | isn = cookie_v4_init_sequence(sk, skb, &req->mss); |
| 1554 | req->cookie_ts = tmp_opt.tstamp_ok; |
| 1555 | } else if (!isn) { |
| 1556 | /* VJ's idea. We save last timestamp seen |
| 1557 | * from the destination in peer table, when entering |
| 1558 | * state TIME-WAIT, and check against it before |
| 1559 | * accepting new connection request. |
| 1560 | * |
| 1561 | * If "isn" is not zero, this request hit alive |
| 1562 | * timewait bucket, so that all the necessary checks |
| 1563 | * are made in the function processing timewait state. |
| 1564 | */ |
| 1565 | if (tmp_opt.saw_tstamp && |
| 1566 | tcp_death_row.sysctl_tw_recycle && |
| 1567 | (dst = inet_csk_route_req(sk, &fl4, req)) != NULL && |
| 1568 | fl4.daddr == saddr) { |
| 1569 | if (!tcp_peer_is_proven(req, dst, true)) { |
| 1570 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED); |
| 1571 | goto drop_and_release; |
| 1572 | } |
| 1573 | } |
| 1574 | /* Kill the following clause, if you dislike this way. */ |
| 1575 | else if (!sysctl_tcp_syncookies && |
| 1576 | (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) < |
| 1577 | (sysctl_max_syn_backlog >> 2)) && |
| 1578 | !tcp_peer_is_proven(req, dst, false)) { |
| 1579 | /* Without syncookies last quarter of |
| 1580 | * backlog is filled with destinations, |
| 1581 | * proven to be alive. |
| 1582 | * It means that we continue to communicate |
| 1583 | * to destinations, already remembered |
| 1584 | * to the moment of synflood. |
| 1585 | */ |
| 1586 | LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("drop open request from %pI4/%u\n"), |
| 1587 | &saddr, ntohs(tcp_hdr(skb)->source)); |
| 1588 | goto drop_and_release; |
| 1589 | } |
| 1590 | |
| 1591 | isn = tcp_v4_init_sequence(skb); |
| 1592 | } |
| 1593 | tcp_rsk(req)->snt_isn = isn; |
| 1594 | |
| 1595 | if (dst == NULL) { |
| 1596 | dst = inet_csk_route_req(sk, &fl4, req); |
| 1597 | if (dst == NULL) |
| 1598 | goto drop_and_free; |
| 1599 | } |
| 1600 | do_fastopen = tcp_fastopen_check(sk, skb, req, &foc, &valid_foc); |
| 1601 | |
| 1602 | /* We don't call tcp_v4_send_synack() directly because we need |
| 1603 | * to make sure a child socket can be created successfully before |
| 1604 | * sending back synack! |
| 1605 | * |
| 1606 | * XXX (TFO) - Ideally one would simply call tcp_v4_send_synack() |
| 1607 | * (or better yet, call tcp_send_synack() in the child context |
| 1608 | * directly, but will have to fix bunch of other code first) |
| 1609 | * after syn_recv_sock() except one will need to first fix the |
| 1610 | * latter to remove its dependency on the current implementation |
| 1611 | * of tcp_v4_send_synack()->tcp_select_initial_window(). |
| 1612 | */ |
| 1613 | skb_synack = tcp_make_synack(sk, dst, req, |
| 1614 | fastopen_cookie_present(&valid_foc) ? &valid_foc : NULL); |
| 1615 | |
| 1616 | if (skb_synack) { |
| 1617 | __tcp_v4_send_check(skb_synack, ireq->loc_addr, ireq->rmt_addr); |
| 1618 | skb_set_queue_mapping(skb_synack, skb_get_queue_mapping(skb)); |
| 1619 | } else |
| 1620 | goto drop_and_free; |
| 1621 | |
| 1622 | if (likely(!do_fastopen)) { |
| 1623 | int err; |
| 1624 | err = ip_build_and_send_pkt(skb_synack, sk, ireq->loc_addr, |
| 1625 | ireq->rmt_addr, ireq->opt); |
| 1626 | err = net_xmit_eval(err); |
| 1627 | if (err || want_cookie) |
| 1628 | goto drop_and_free; |
| 1629 | |
| 1630 | tcp_rsk(req)->snt_synack = tcp_time_stamp; |
| 1631 | tcp_rsk(req)->listener = NULL; |
| 1632 | /* Add the request_sock to the SYN table */ |
| 1633 | inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT); |
| 1634 | if (fastopen_cookie_present(&foc) && foc.len != 0) |
| 1635 | NET_INC_STATS_BH(sock_net(sk), |
| 1636 | LINUX_MIB_TCPFASTOPENPASSIVEFAIL); |
| 1637 | } else if (tcp_v4_conn_req_fastopen(sk, skb, skb_synack, req)) |
| 1638 | goto drop_and_free; |
| 1639 | |
| 1640 | return 0; |
| 1641 | |
| 1642 | drop_and_release: |
| 1643 | dst_release(dst); |
| 1644 | drop_and_free: |
| 1645 | reqsk_free(req); |
| 1646 | drop: |
| 1647 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS); |
| 1648 | return 0; |
| 1649 | } |
| 1650 | EXPORT_SYMBOL(tcp_v4_conn_request); |
| 1651 | |
| 1652 | |
| 1653 | /* |
| 1654 | * The three way handshake has completed - we got a valid synack - |
| 1655 | * now create the new socket. |
| 1656 | */ |
| 1657 | struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb, |
| 1658 | struct request_sock *req, |
| 1659 | struct dst_entry *dst) |
| 1660 | { |
| 1661 | struct inet_request_sock *ireq; |
| 1662 | struct inet_sock *newinet; |
| 1663 | struct tcp_sock *newtp; |
| 1664 | struct sock *newsk; |
| 1665 | #ifdef CONFIG_TCP_MD5SIG |
| 1666 | struct tcp_md5sig_key *key; |
| 1667 | #endif |
| 1668 | struct ip_options_rcu *inet_opt; |
| 1669 | |
| 1670 | if (sk_acceptq_is_full(sk)) |
| 1671 | goto exit_overflow; |
| 1672 | |
| 1673 | newsk = tcp_create_openreq_child(sk, req, skb); |
| 1674 | if (!newsk) |
| 1675 | goto exit_nonewsk; |
| 1676 | |
| 1677 | newsk->sk_gso_type = SKB_GSO_TCPV4; |
| 1678 | inet_sk_rx_dst_set(newsk, skb); |
| 1679 | |
| 1680 | newtp = tcp_sk(newsk); |
| 1681 | newinet = inet_sk(newsk); |
| 1682 | ireq = inet_rsk(req); |
| 1683 | newinet->inet_daddr = ireq->rmt_addr; |
| 1684 | newinet->inet_rcv_saddr = ireq->loc_addr; |
| 1685 | newinet->inet_saddr = ireq->loc_addr; |
| 1686 | inet_opt = ireq->opt; |
| 1687 | rcu_assign_pointer(newinet->inet_opt, inet_opt); |
| 1688 | ireq->opt = NULL; |
| 1689 | newinet->mc_index = inet_iif(skb); |
| 1690 | newinet->mc_ttl = ip_hdr(skb)->ttl; |
| 1691 | newinet->rcv_tos = ip_hdr(skb)->tos; |
| 1692 | inet_csk(newsk)->icsk_ext_hdr_len = 0; |
| 1693 | if (inet_opt) |
| 1694 | inet_csk(newsk)->icsk_ext_hdr_len = inet_opt->opt.optlen; |
| 1695 | newinet->inet_id = newtp->write_seq ^ jiffies; |
| 1696 | |
| 1697 | if (!dst) { |
| 1698 | dst = inet_csk_route_child_sock(sk, newsk, req); |
| 1699 | if (!dst) |
| 1700 | goto put_and_exit; |
| 1701 | } else { |
| 1702 | /* syncookie case : see end of cookie_v4_check() */ |
| 1703 | } |
| 1704 | sk_setup_caps(newsk, dst); |
| 1705 | |
| 1706 | tcp_mtup_init(newsk); |
| 1707 | tcp_sync_mss(newsk, dst_mtu(dst)); |
| 1708 | newtp->advmss = dst_metric_advmss(dst); |
| 1709 | if (tcp_sk(sk)->rx_opt.user_mss && |
| 1710 | tcp_sk(sk)->rx_opt.user_mss < newtp->advmss) |
| 1711 | newtp->advmss = tcp_sk(sk)->rx_opt.user_mss; |
| 1712 | |
| 1713 | tcp_initialize_rcv_mss(newsk); |
| 1714 | tcp_synack_rtt_meas(newsk, req); |
| 1715 | newtp->total_retrans = req->num_retrans; |
| 1716 | |
| 1717 | #ifdef CONFIG_TCP_MD5SIG |
| 1718 | /* Copy over the MD5 key from the original socket */ |
| 1719 | key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&newinet->inet_daddr, |
| 1720 | AF_INET); |
| 1721 | if (key != NULL) { |
| 1722 | /* |
| 1723 | * We're using one, so create a matching key |
| 1724 | * on the newsk structure. If we fail to get |
| 1725 | * memory, then we end up not copying the key |
| 1726 | * across. Shucks. |
| 1727 | */ |
| 1728 | tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newinet->inet_daddr, |
| 1729 | AF_INET, key->key, key->keylen, GFP_ATOMIC); |
| 1730 | sk_nocaps_add(newsk, NETIF_F_GSO_MASK); |
| 1731 | } |
| 1732 | #endif |
| 1733 | |
| 1734 | if (__inet_inherit_port(sk, newsk) < 0) |
| 1735 | goto put_and_exit; |
| 1736 | __inet_hash_nolisten(newsk, NULL); |
| 1737 | |
| 1738 | return newsk; |
| 1739 | |
| 1740 | exit_overflow: |
| 1741 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS); |
| 1742 | exit_nonewsk: |
| 1743 | dst_release(dst); |
| 1744 | exit: |
| 1745 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS); |
| 1746 | return NULL; |
| 1747 | put_and_exit: |
| 1748 | inet_csk_prepare_forced_close(newsk); |
| 1749 | tcp_done(newsk); |
| 1750 | goto exit; |
| 1751 | } |
| 1752 | EXPORT_SYMBOL(tcp_v4_syn_recv_sock); |
| 1753 | |
| 1754 | static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb) |
| 1755 | { |
| 1756 | struct tcphdr *th = tcp_hdr(skb); |
| 1757 | const struct iphdr *iph = ip_hdr(skb); |
| 1758 | struct sock *nsk; |
| 1759 | struct request_sock **prev; |
| 1760 | /* Find possible connection requests. */ |
| 1761 | struct request_sock *req = inet_csk_search_req(sk, &prev, th->source, |
| 1762 | iph->saddr, iph->daddr); |
| 1763 | if (req) |
| 1764 | return tcp_check_req(sk, skb, req, prev, false); |
| 1765 | |
| 1766 | nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr, |
| 1767 | th->source, iph->daddr, th->dest, inet_iif(skb)); |
| 1768 | |
| 1769 | if (nsk) { |
| 1770 | if (nsk->sk_state != TCP_TIME_WAIT) { |
| 1771 | bh_lock_sock(nsk); |
| 1772 | return nsk; |
| 1773 | } |
| 1774 | inet_twsk_put(inet_twsk(nsk)); |
| 1775 | return NULL; |
| 1776 | } |
| 1777 | |
| 1778 | #ifdef CONFIG_SYN_COOKIES |
| 1779 | if (!th->syn) |
| 1780 | sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt)); |
| 1781 | #endif |
| 1782 | return sk; |
| 1783 | } |
| 1784 | |
| 1785 | static __sum16 tcp_v4_checksum_init(struct sk_buff *skb) |
| 1786 | { |
| 1787 | const struct iphdr *iph = ip_hdr(skb); |
| 1788 | |
| 1789 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
| 1790 | if (!tcp_v4_check(skb->len, iph->saddr, |
| 1791 | iph->daddr, skb->csum)) { |
| 1792 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1793 | return 0; |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, |
| 1798 | skb->len, IPPROTO_TCP, 0); |
| 1799 | |
| 1800 | if (skb->len <= 76) { |
| 1801 | return __skb_checksum_complete(skb); |
| 1802 | } |
| 1803 | return 0; |
| 1804 | } |
| 1805 | |
| 1806 | |
| 1807 | /* The socket must have it's spinlock held when we get |
| 1808 | * here. |
| 1809 | * |
| 1810 | * We have a potential double-lock case here, so even when |
| 1811 | * doing backlog processing we use the BH locking scheme. |
| 1812 | * This is because we cannot sleep with the original spinlock |
| 1813 | * held. |
| 1814 | */ |
| 1815 | int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb) |
| 1816 | { |
| 1817 | struct sock *rsk; |
| 1818 | #ifdef CONFIG_TCP_MD5SIG |
| 1819 | /* |
| 1820 | * We really want to reject the packet as early as possible |
| 1821 | * if: |
| 1822 | * o We're expecting an MD5'd packet and this is no MD5 tcp option |
| 1823 | * o There is an MD5 option and we're not expecting one |
| 1824 | */ |
| 1825 | if (tcp_v4_inbound_md5_hash(sk, skb)) |
| 1826 | goto discard; |
| 1827 | #endif |
| 1828 | |
| 1829 | if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */ |
| 1830 | struct dst_entry *dst = sk->sk_rx_dst; |
| 1831 | |
| 1832 | sock_rps_save_rxhash(sk, skb); |
| 1833 | if (dst) { |
| 1834 | if (inet_sk(sk)->rx_dst_ifindex != skb->skb_iif || |
| 1835 | dst->ops->check(dst, 0) == NULL) { |
| 1836 | dst_release(dst); |
| 1837 | sk->sk_rx_dst = NULL; |
| 1838 | } |
| 1839 | } |
| 1840 | if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) { |
| 1841 | rsk = sk; |
| 1842 | goto reset; |
| 1843 | } |
| 1844 | return 0; |
| 1845 | } |
| 1846 | |
| 1847 | if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb)) |
| 1848 | goto csum_err; |
| 1849 | |
| 1850 | if (sk->sk_state == TCP_LISTEN) { |
| 1851 | struct sock *nsk = tcp_v4_hnd_req(sk, skb); |
| 1852 | if (!nsk) |
| 1853 | goto discard; |
| 1854 | |
| 1855 | if (nsk != sk) { |
| 1856 | sock_rps_save_rxhash(nsk, skb); |
| 1857 | if (tcp_child_process(sk, nsk, skb)) { |
| 1858 | rsk = nsk; |
| 1859 | goto reset; |
| 1860 | } |
| 1861 | return 0; |
| 1862 | } |
| 1863 | } else |
| 1864 | sock_rps_save_rxhash(sk, skb); |
| 1865 | |
| 1866 | if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) { |
| 1867 | rsk = sk; |
| 1868 | goto reset; |
| 1869 | } |
| 1870 | return 0; |
| 1871 | |
| 1872 | reset: |
| 1873 | tcp_v4_send_reset(rsk, skb); |
| 1874 | discard: |
| 1875 | kfree_skb(skb); |
| 1876 | /* Be careful here. If this function gets more complicated and |
| 1877 | * gcc suffers from register pressure on the x86, sk (in %ebx) |
| 1878 | * might be destroyed here. This current version compiles correctly, |
| 1879 | * but you have been warned. |
| 1880 | */ |
| 1881 | return 0; |
| 1882 | |
| 1883 | csum_err: |
| 1884 | TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_CSUMERRORS); |
| 1885 | TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS); |
| 1886 | goto discard; |
| 1887 | } |
| 1888 | EXPORT_SYMBOL(tcp_v4_do_rcv); |
| 1889 | |
| 1890 | void tcp_v4_early_demux(struct sk_buff *skb) |
| 1891 | { |
| 1892 | const struct iphdr *iph; |
| 1893 | const struct tcphdr *th; |
| 1894 | struct sock *sk; |
| 1895 | |
| 1896 | if (skb->pkt_type != PACKET_HOST) |
| 1897 | return; |
| 1898 | |
| 1899 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct tcphdr))) |
| 1900 | return; |
| 1901 | |
| 1902 | iph = ip_hdr(skb); |
| 1903 | th = tcp_hdr(skb); |
| 1904 | |
| 1905 | if (th->doff < sizeof(struct tcphdr) / 4) |
| 1906 | return; |
| 1907 | |
| 1908 | sk = __inet_lookup_established(dev_net(skb->dev), &tcp_hashinfo, |
| 1909 | iph->saddr, th->source, |
| 1910 | iph->daddr, ntohs(th->dest), |
| 1911 | skb->skb_iif); |
| 1912 | if (sk) { |
| 1913 | skb->sk = sk; |
| 1914 | skb->destructor = sock_edemux; |
| 1915 | if (sk->sk_state != TCP_TIME_WAIT) { |
| 1916 | struct dst_entry *dst = ACCESS_ONCE(sk->sk_rx_dst); |
| 1917 | |
| 1918 | if (dst) |
| 1919 | dst = dst_check(dst, 0); |
| 1920 | if (dst && |
| 1921 | inet_sk(sk)->rx_dst_ifindex == skb->skb_iif) |
| 1922 | skb_dst_set_noref(skb, dst); |
| 1923 | } |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | /* Packet is added to VJ-style prequeue for processing in process |
| 1928 | * context, if a reader task is waiting. Apparently, this exciting |
| 1929 | * idea (VJ's mail "Re: query about TCP header on tcp-ip" of 07 Sep 93) |
| 1930 | * failed somewhere. Latency? Burstiness? Well, at least now we will |
| 1931 | * see, why it failed. 8)8) --ANK |
| 1932 | * |
| 1933 | */ |
| 1934 | bool tcp_prequeue(struct sock *sk, struct sk_buff *skb) |
| 1935 | { |
| 1936 | struct tcp_sock *tp = tcp_sk(sk); |
| 1937 | |
| 1938 | if (sysctl_tcp_low_latency || !tp->ucopy.task) |
| 1939 | return false; |
| 1940 | |
| 1941 | if (skb->len <= tcp_hdrlen(skb) && |
| 1942 | skb_queue_len(&tp->ucopy.prequeue) == 0) |
| 1943 | return false; |
| 1944 | |
| 1945 | skb_dst_force(skb); |
| 1946 | __skb_queue_tail(&tp->ucopy.prequeue, skb); |
| 1947 | tp->ucopy.memory += skb->truesize; |
| 1948 | if (tp->ucopy.memory > sk->sk_rcvbuf) { |
| 1949 | struct sk_buff *skb1; |
| 1950 | |
| 1951 | BUG_ON(sock_owned_by_user(sk)); |
| 1952 | |
| 1953 | while ((skb1 = __skb_dequeue(&tp->ucopy.prequeue)) != NULL) { |
| 1954 | sk_backlog_rcv(sk, skb1); |
| 1955 | NET_INC_STATS_BH(sock_net(sk), |
| 1956 | LINUX_MIB_TCPPREQUEUEDROPPED); |
| 1957 | } |
| 1958 | |
| 1959 | tp->ucopy.memory = 0; |
| 1960 | } else if (skb_queue_len(&tp->ucopy.prequeue) == 1) { |
| 1961 | wake_up_interruptible_sync_poll(sk_sleep(sk), |
| 1962 | POLLIN | POLLRDNORM | POLLRDBAND); |
| 1963 | if (!inet_csk_ack_scheduled(sk)) |
| 1964 | inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, |
| 1965 | (3 * tcp_rto_min(sk)) / 4, |
| 1966 | TCP_RTO_MAX); |
| 1967 | } |
| 1968 | return true; |
| 1969 | } |
| 1970 | EXPORT_SYMBOL(tcp_prequeue); |
| 1971 | |
| 1972 | int tcp_filter(struct sock *sk, struct sk_buff *skb) |
| 1973 | { |
| 1974 | struct tcphdr *th = (struct tcphdr *)skb->data; |
| 1975 | unsigned int eaten = skb->len; |
| 1976 | int err; |
| 1977 | |
| 1978 | err = sk_filter_trim_cap(sk, skb, th->doff * 4); |
| 1979 | if (!err) { |
| 1980 | eaten -= skb->len; |
| 1981 | TCP_SKB_CB(skb)->end_seq -= eaten; |
| 1982 | } |
| 1983 | return err; |
| 1984 | } |
| 1985 | EXPORT_SYMBOL(tcp_filter); |
| 1986 | |
| 1987 | /* |
| 1988 | * From tcp_input.c |
| 1989 | */ |
| 1990 | |
| 1991 | int tcp_v4_rcv(struct sk_buff *skb) |
| 1992 | { |
| 1993 | const struct iphdr *iph; |
| 1994 | const struct tcphdr *th; |
| 1995 | struct sock *sk; |
| 1996 | int ret; |
| 1997 | struct net *net = dev_net(skb->dev); |
| 1998 | |
| 1999 | if (skb->pkt_type != PACKET_HOST) |
| 2000 | goto discard_it; |
| 2001 | |
| 2002 | /* Count it even if it's bad */ |
| 2003 | TCP_INC_STATS_BH(net, TCP_MIB_INSEGS); |
| 2004 | |
| 2005 | if (!pskb_may_pull(skb, sizeof(struct tcphdr))) |
| 2006 | goto discard_it; |
| 2007 | |
| 2008 | th = tcp_hdr(skb); |
| 2009 | |
| 2010 | if (th->doff < sizeof(struct tcphdr) / 4) |
| 2011 | goto bad_packet; |
| 2012 | if (!pskb_may_pull(skb, th->doff * 4)) |
| 2013 | goto discard_it; |
| 2014 | |
| 2015 | /* An explanation is required here, I think. |
| 2016 | * Packet length and doff are validated by header prediction, |
| 2017 | * provided case of th->doff==0 is eliminated. |
| 2018 | * So, we defer the checks. */ |
| 2019 | if (!skb_csum_unnecessary(skb) && tcp_v4_checksum_init(skb)) |
| 2020 | goto csum_error; |
| 2021 | |
| 2022 | th = tcp_hdr(skb); |
| 2023 | iph = ip_hdr(skb); |
| 2024 | TCP_SKB_CB(skb)->seq = ntohl(th->seq); |
| 2025 | TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin + |
| 2026 | skb->len - th->doff * 4); |
| 2027 | TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq); |
| 2028 | TCP_SKB_CB(skb)->when = 0; |
| 2029 | TCP_SKB_CB(skb)->ip_dsfield = ipv4_get_dsfield(iph); |
| 2030 | TCP_SKB_CB(skb)->sacked = 0; |
| 2031 | |
| 2032 | sk = __inet_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest); |
| 2033 | if (!sk) |
| 2034 | goto no_tcp_socket; |
| 2035 | |
| 2036 | process: |
| 2037 | if (sk->sk_state == TCP_TIME_WAIT) |
| 2038 | goto do_time_wait; |
| 2039 | |
| 2040 | if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) { |
| 2041 | NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP); |
| 2042 | goto discard_and_relse; |
| 2043 | } |
| 2044 | |
| 2045 | if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) |
| 2046 | goto discard_and_relse; |
| 2047 | nf_reset(skb); |
| 2048 | |
| 2049 | if (tcp_filter(sk, skb)) |
| 2050 | goto discard_and_relse; |
| 2051 | th = (const struct tcphdr *)skb->data; |
| 2052 | iph = ip_hdr(skb); |
| 2053 | |
| 2054 | skb->dev = NULL; |
| 2055 | |
| 2056 | bh_lock_sock_nested(sk); |
| 2057 | tcp_sk(sk)->segs_in += max_t(u16, 1, skb_shinfo(skb)->gso_segs); |
| 2058 | ret = 0; |
| 2059 | if (!sock_owned_by_user(sk)) { |
| 2060 | #ifdef CONFIG_NET_DMA |
| 2061 | struct tcp_sock *tp = tcp_sk(sk); |
| 2062 | if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list) |
| 2063 | tp->ucopy.dma_chan = net_dma_find_channel(); |
| 2064 | if (tp->ucopy.dma_chan) |
| 2065 | ret = tcp_v4_do_rcv(sk, skb); |
| 2066 | else |
| 2067 | #endif |
| 2068 | { |
| 2069 | if (!tcp_prequeue(sk, skb)) |
| 2070 | ret = tcp_v4_do_rcv(sk, skb); |
| 2071 | } |
| 2072 | } else if (unlikely(sk_add_backlog(sk, skb, |
| 2073 | sk->sk_rcvbuf + sk->sk_sndbuf))) { |
| 2074 | bh_unlock_sock(sk); |
| 2075 | NET_INC_STATS_BH(net, LINUX_MIB_TCPBACKLOGDROP); |
| 2076 | goto discard_and_relse; |
| 2077 | } |
| 2078 | bh_unlock_sock(sk); |
| 2079 | |
| 2080 | sock_put(sk); |
| 2081 | |
| 2082 | return ret; |
| 2083 | |
| 2084 | no_tcp_socket: |
| 2085 | if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) |
| 2086 | goto discard_it; |
| 2087 | |
| 2088 | if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) { |
| 2089 | csum_error: |
| 2090 | TCP_INC_STATS_BH(net, TCP_MIB_CSUMERRORS); |
| 2091 | bad_packet: |
| 2092 | TCP_INC_STATS_BH(net, TCP_MIB_INERRS); |
| 2093 | } else { |
| 2094 | tcp_v4_send_reset(NULL, skb); |
| 2095 | } |
| 2096 | |
| 2097 | discard_it: |
| 2098 | /* Discard frame. */ |
| 2099 | kfree_skb(skb); |
| 2100 | return 0; |
| 2101 | |
| 2102 | discard_and_relse: |
| 2103 | sock_put(sk); |
| 2104 | goto discard_it; |
| 2105 | |
| 2106 | do_time_wait: |
| 2107 | if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) { |
| 2108 | inet_twsk_put(inet_twsk(sk)); |
| 2109 | goto discard_it; |
| 2110 | } |
| 2111 | |
| 2112 | if (skb->len < (th->doff << 2)) { |
| 2113 | inet_twsk_put(inet_twsk(sk)); |
| 2114 | goto bad_packet; |
| 2115 | } |
| 2116 | if (tcp_checksum_complete(skb)) { |
| 2117 | inet_twsk_put(inet_twsk(sk)); |
| 2118 | goto csum_error; |
| 2119 | } |
| 2120 | switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) { |
| 2121 | case TCP_TW_SYN: { |
| 2122 | struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev), |
| 2123 | &tcp_hashinfo, |
| 2124 | iph->saddr, th->source, |
| 2125 | iph->daddr, th->dest, |
| 2126 | inet_iif(skb)); |
| 2127 | if (sk2) { |
| 2128 | inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row); |
| 2129 | inet_twsk_put(inet_twsk(sk)); |
| 2130 | sk = sk2; |
| 2131 | goto process; |
| 2132 | } |
| 2133 | /* Fall through to ACK */ |
| 2134 | } |
| 2135 | case TCP_TW_ACK: |
| 2136 | tcp_v4_timewait_ack(sk, skb); |
| 2137 | break; |
| 2138 | case TCP_TW_RST: |
| 2139 | goto no_tcp_socket; |
| 2140 | case TCP_TW_SUCCESS:; |
| 2141 | } |
| 2142 | goto discard_it; |
| 2143 | } |
| 2144 | |
| 2145 | static struct timewait_sock_ops tcp_timewait_sock_ops = { |
| 2146 | .twsk_obj_size = sizeof(struct tcp_timewait_sock), |
| 2147 | .twsk_unique = tcp_twsk_unique, |
| 2148 | .twsk_destructor= tcp_twsk_destructor, |
| 2149 | }; |
| 2150 | |
| 2151 | void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb) |
| 2152 | { |
| 2153 | struct dst_entry *dst = skb_dst(skb); |
| 2154 | |
| 2155 | dst_hold(dst); |
| 2156 | sk->sk_rx_dst = dst; |
| 2157 | inet_sk(sk)->rx_dst_ifindex = skb->skb_iif; |
| 2158 | } |
| 2159 | EXPORT_SYMBOL(inet_sk_rx_dst_set); |
| 2160 | |
| 2161 | const struct inet_connection_sock_af_ops ipv4_specific = { |
| 2162 | .queue_xmit = ip_queue_xmit, |
| 2163 | .send_check = tcp_v4_send_check, |
| 2164 | .rebuild_header = inet_sk_rebuild_header, |
| 2165 | .sk_rx_dst_set = inet_sk_rx_dst_set, |
| 2166 | .conn_request = tcp_v4_conn_request, |
| 2167 | .syn_recv_sock = tcp_v4_syn_recv_sock, |
| 2168 | .net_header_len = sizeof(struct iphdr), |
| 2169 | .setsockopt = ip_setsockopt, |
| 2170 | .getsockopt = ip_getsockopt, |
| 2171 | .addr2sockaddr = inet_csk_addr2sockaddr, |
| 2172 | .sockaddr_len = sizeof(struct sockaddr_in), |
| 2173 | .bind_conflict = inet_csk_bind_conflict, |
| 2174 | #ifdef CONFIG_COMPAT |
| 2175 | .compat_setsockopt = compat_ip_setsockopt, |
| 2176 | .compat_getsockopt = compat_ip_getsockopt, |
| 2177 | #endif |
| 2178 | .mtu_reduced = tcp_v4_mtu_reduced, |
| 2179 | }; |
| 2180 | EXPORT_SYMBOL(ipv4_specific); |
| 2181 | |
| 2182 | #ifdef CONFIG_TCP_MD5SIG |
| 2183 | static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = { |
| 2184 | .md5_lookup = tcp_v4_md5_lookup, |
| 2185 | .calc_md5_hash = tcp_v4_md5_hash_skb, |
| 2186 | .md5_parse = tcp_v4_parse_md5_keys, |
| 2187 | }; |
| 2188 | #endif |
| 2189 | |
| 2190 | /* NOTE: A lot of things set to zero explicitly by call to |
| 2191 | * sk_alloc() so need not be done here. |
| 2192 | */ |
| 2193 | static int tcp_v4_init_sock(struct sock *sk) |
| 2194 | { |
| 2195 | struct inet_connection_sock *icsk = inet_csk(sk); |
| 2196 | |
| 2197 | tcp_init_sock(sk); |
| 2198 | |
| 2199 | icsk->icsk_af_ops = &ipv4_specific; |
| 2200 | |
| 2201 | #ifdef CONFIG_TCP_MD5SIG |
| 2202 | tcp_sk(sk)->af_specific = &tcp_sock_ipv4_specific; |
| 2203 | #endif |
| 2204 | |
| 2205 | return 0; |
| 2206 | } |
| 2207 | |
| 2208 | void tcp_v4_destroy_sock(struct sock *sk) |
| 2209 | { |
| 2210 | struct tcp_sock *tp = tcp_sk(sk); |
| 2211 | |
| 2212 | tcp_clear_xmit_timers(sk); |
| 2213 | |
| 2214 | tcp_cleanup_congestion_control(sk); |
| 2215 | |
| 2216 | /* Cleanup up the write buffer. */ |
| 2217 | tcp_write_queue_purge(sk); |
| 2218 | |
| 2219 | /* Cleans up our, hopefully empty, out_of_order_queue. */ |
| 2220 | __skb_queue_purge(&tp->out_of_order_queue); |
| 2221 | |
| 2222 | #ifdef CONFIG_TCP_MD5SIG |
| 2223 | /* Clean up the MD5 key list, if any */ |
| 2224 | if (tp->md5sig_info) { |
| 2225 | tcp_clear_md5_list(sk); |
| 2226 | kfree_rcu(tp->md5sig_info, rcu); |
| 2227 | tp->md5sig_info = NULL; |
| 2228 | } |
| 2229 | #endif |
| 2230 | |
| 2231 | #ifdef CONFIG_NET_DMA |
| 2232 | /* Cleans up our sk_async_wait_queue */ |
| 2233 | __skb_queue_purge(&sk->sk_async_wait_queue); |
| 2234 | #endif |
| 2235 | |
| 2236 | /* Clean prequeue, it must be empty really */ |
| 2237 | __skb_queue_purge(&tp->ucopy.prequeue); |
| 2238 | |
| 2239 | /* Clean up a referenced TCP bind bucket. */ |
| 2240 | if (inet_csk(sk)->icsk_bind_hash) |
| 2241 | inet_put_port(sk); |
| 2242 | |
| 2243 | BUG_ON(tp->fastopen_rsk != NULL); |
| 2244 | |
| 2245 | /* If socket is aborted during connect operation */ |
| 2246 | tcp_free_fastopen_req(tp); |
| 2247 | |
| 2248 | sk_sockets_allocated_dec(sk); |
| 2249 | sock_release_memcg(sk); |
| 2250 | } |
| 2251 | EXPORT_SYMBOL(tcp_v4_destroy_sock); |
| 2252 | |
| 2253 | #ifdef CONFIG_PROC_FS |
| 2254 | /* Proc filesystem TCP sock list dumping. */ |
| 2255 | |
| 2256 | static inline struct inet_timewait_sock *tw_head(struct hlist_nulls_head *head) |
| 2257 | { |
| 2258 | return hlist_nulls_empty(head) ? NULL : |
| 2259 | list_entry(head->first, struct inet_timewait_sock, tw_node); |
| 2260 | } |
| 2261 | |
| 2262 | static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw) |
| 2263 | { |
| 2264 | return !is_a_nulls(tw->tw_node.next) ? |
| 2265 | hlist_nulls_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL; |
| 2266 | } |
| 2267 | |
| 2268 | /* |
| 2269 | * Get next listener socket follow cur. If cur is NULL, get first socket |
| 2270 | * starting from bucket given in st->bucket; when st->bucket is zero the |
| 2271 | * very first socket in the hash table is returned. |
| 2272 | */ |
| 2273 | static void *listening_get_next(struct seq_file *seq, void *cur) |
| 2274 | { |
| 2275 | struct inet_connection_sock *icsk; |
| 2276 | struct hlist_nulls_node *node; |
| 2277 | struct sock *sk = cur; |
| 2278 | struct inet_listen_hashbucket *ilb; |
| 2279 | struct tcp_iter_state *st = seq->private; |
| 2280 | struct net *net = seq_file_net(seq); |
| 2281 | |
| 2282 | if (!sk) { |
| 2283 | ilb = &tcp_hashinfo.listening_hash[st->bucket]; |
| 2284 | spin_lock_bh(&ilb->lock); |
| 2285 | sk = sk_nulls_head(&ilb->head); |
| 2286 | st->offset = 0; |
| 2287 | goto get_sk; |
| 2288 | } |
| 2289 | ilb = &tcp_hashinfo.listening_hash[st->bucket]; |
| 2290 | ++st->num; |
| 2291 | ++st->offset; |
| 2292 | |
| 2293 | if (st->state == TCP_SEQ_STATE_OPENREQ) { |
| 2294 | struct request_sock *req = cur; |
| 2295 | |
| 2296 | icsk = inet_csk(st->syn_wait_sk); |
| 2297 | req = req->dl_next; |
| 2298 | while (1) { |
| 2299 | while (req) { |
| 2300 | if (req->rsk_ops->family == st->family) { |
| 2301 | cur = req; |
| 2302 | goto out; |
| 2303 | } |
| 2304 | req = req->dl_next; |
| 2305 | } |
| 2306 | if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries) |
| 2307 | break; |
| 2308 | get_req: |
| 2309 | req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket]; |
| 2310 | } |
| 2311 | sk = sk_nulls_next(st->syn_wait_sk); |
| 2312 | st->state = TCP_SEQ_STATE_LISTENING; |
| 2313 | read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
| 2314 | } else { |
| 2315 | icsk = inet_csk(sk); |
| 2316 | read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
| 2317 | if (reqsk_queue_len(&icsk->icsk_accept_queue)) |
| 2318 | goto start_req; |
| 2319 | read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
| 2320 | sk = sk_nulls_next(sk); |
| 2321 | } |
| 2322 | get_sk: |
| 2323 | sk_nulls_for_each_from(sk, node) { |
| 2324 | if (!net_eq(sock_net(sk), net)) |
| 2325 | continue; |
| 2326 | if (sk->sk_family == st->family) { |
| 2327 | cur = sk; |
| 2328 | goto out; |
| 2329 | } |
| 2330 | icsk = inet_csk(sk); |
| 2331 | read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
| 2332 | if (reqsk_queue_len(&icsk->icsk_accept_queue)) { |
| 2333 | start_req: |
| 2334 | st->uid = sock_i_uid(sk); |
| 2335 | st->syn_wait_sk = sk; |
| 2336 | st->state = TCP_SEQ_STATE_OPENREQ; |
| 2337 | st->sbucket = 0; |
| 2338 | goto get_req; |
| 2339 | } |
| 2340 | read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
| 2341 | } |
| 2342 | spin_unlock_bh(&ilb->lock); |
| 2343 | st->offset = 0; |
| 2344 | if (++st->bucket < INET_LHTABLE_SIZE) { |
| 2345 | ilb = &tcp_hashinfo.listening_hash[st->bucket]; |
| 2346 | spin_lock_bh(&ilb->lock); |
| 2347 | sk = sk_nulls_head(&ilb->head); |
| 2348 | goto get_sk; |
| 2349 | } |
| 2350 | cur = NULL; |
| 2351 | out: |
| 2352 | return cur; |
| 2353 | } |
| 2354 | |
| 2355 | static void *listening_get_idx(struct seq_file *seq, loff_t *pos) |
| 2356 | { |
| 2357 | struct tcp_iter_state *st = seq->private; |
| 2358 | void *rc; |
| 2359 | |
| 2360 | st->bucket = 0; |
| 2361 | st->offset = 0; |
| 2362 | rc = listening_get_next(seq, NULL); |
| 2363 | |
| 2364 | while (rc && *pos) { |
| 2365 | rc = listening_get_next(seq, rc); |
| 2366 | --*pos; |
| 2367 | } |
| 2368 | return rc; |
| 2369 | } |
| 2370 | |
| 2371 | static inline bool empty_bucket(struct tcp_iter_state *st) |
| 2372 | { |
| 2373 | return hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].chain) && |
| 2374 | hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].twchain); |
| 2375 | } |
| 2376 | |
| 2377 | /* |
| 2378 | * Get first established socket starting from bucket given in st->bucket. |
| 2379 | * If st->bucket is zero, the very first socket in the hash is returned. |
| 2380 | */ |
| 2381 | static void *established_get_first(struct seq_file *seq) |
| 2382 | { |
| 2383 | struct tcp_iter_state *st = seq->private; |
| 2384 | struct net *net = seq_file_net(seq); |
| 2385 | void *rc = NULL; |
| 2386 | |
| 2387 | st->offset = 0; |
| 2388 | for (; st->bucket <= tcp_hashinfo.ehash_mask; ++st->bucket) { |
| 2389 | struct sock *sk; |
| 2390 | struct hlist_nulls_node *node; |
| 2391 | struct inet_timewait_sock *tw; |
| 2392 | spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket); |
| 2393 | |
| 2394 | /* Lockless fast path for the common case of empty buckets */ |
| 2395 | if (empty_bucket(st)) |
| 2396 | continue; |
| 2397 | |
| 2398 | spin_lock_bh(lock); |
| 2399 | sk_nulls_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) { |
| 2400 | if (sk->sk_family != st->family || |
| 2401 | !net_eq(sock_net(sk), net)) { |
| 2402 | continue; |
| 2403 | } |
| 2404 | rc = sk; |
| 2405 | goto out; |
| 2406 | } |
| 2407 | st->state = TCP_SEQ_STATE_TIME_WAIT; |
| 2408 | inet_twsk_for_each(tw, node, |
| 2409 | &tcp_hashinfo.ehash[st->bucket].twchain) { |
| 2410 | if (tw->tw_family != st->family || |
| 2411 | !net_eq(twsk_net(tw), net)) { |
| 2412 | continue; |
| 2413 | } |
| 2414 | rc = tw; |
| 2415 | goto out; |
| 2416 | } |
| 2417 | spin_unlock_bh(lock); |
| 2418 | st->state = TCP_SEQ_STATE_ESTABLISHED; |
| 2419 | } |
| 2420 | out: |
| 2421 | return rc; |
| 2422 | } |
| 2423 | |
| 2424 | static void *established_get_next(struct seq_file *seq, void *cur) |
| 2425 | { |
| 2426 | struct sock *sk = cur; |
| 2427 | struct inet_timewait_sock *tw; |
| 2428 | struct hlist_nulls_node *node; |
| 2429 | struct tcp_iter_state *st = seq->private; |
| 2430 | struct net *net = seq_file_net(seq); |
| 2431 | |
| 2432 | ++st->num; |
| 2433 | ++st->offset; |
| 2434 | |
| 2435 | if (st->state == TCP_SEQ_STATE_TIME_WAIT) { |
| 2436 | tw = cur; |
| 2437 | tw = tw_next(tw); |
| 2438 | get_tw: |
| 2439 | while (tw && (tw->tw_family != st->family || !net_eq(twsk_net(tw), net))) { |
| 2440 | tw = tw_next(tw); |
| 2441 | } |
| 2442 | if (tw) { |
| 2443 | cur = tw; |
| 2444 | goto out; |
| 2445 | } |
| 2446 | spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket)); |
| 2447 | st->state = TCP_SEQ_STATE_ESTABLISHED; |
| 2448 | |
| 2449 | /* Look for next non empty bucket */ |
| 2450 | st->offset = 0; |
| 2451 | while (++st->bucket <= tcp_hashinfo.ehash_mask && |
| 2452 | empty_bucket(st)) |
| 2453 | ; |
| 2454 | if (st->bucket > tcp_hashinfo.ehash_mask) |
| 2455 | return NULL; |
| 2456 | |
| 2457 | spin_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket)); |
| 2458 | sk = sk_nulls_head(&tcp_hashinfo.ehash[st->bucket].chain); |
| 2459 | } else |
| 2460 | sk = sk_nulls_next(sk); |
| 2461 | |
| 2462 | sk_nulls_for_each_from(sk, node) { |
| 2463 | if (sk->sk_family == st->family && net_eq(sock_net(sk), net)) |
| 2464 | goto found; |
| 2465 | } |
| 2466 | |
| 2467 | st->state = TCP_SEQ_STATE_TIME_WAIT; |
| 2468 | tw = tw_head(&tcp_hashinfo.ehash[st->bucket].twchain); |
| 2469 | goto get_tw; |
| 2470 | found: |
| 2471 | cur = sk; |
| 2472 | out: |
| 2473 | return cur; |
| 2474 | } |
| 2475 | |
| 2476 | static void *established_get_idx(struct seq_file *seq, loff_t pos) |
| 2477 | { |
| 2478 | struct tcp_iter_state *st = seq->private; |
| 2479 | void *rc; |
| 2480 | |
| 2481 | st->bucket = 0; |
| 2482 | rc = established_get_first(seq); |
| 2483 | |
| 2484 | while (rc && pos) { |
| 2485 | rc = established_get_next(seq, rc); |
| 2486 | --pos; |
| 2487 | } |
| 2488 | return rc; |
| 2489 | } |
| 2490 | |
| 2491 | static void *tcp_get_idx(struct seq_file *seq, loff_t pos) |
| 2492 | { |
| 2493 | void *rc; |
| 2494 | struct tcp_iter_state *st = seq->private; |
| 2495 | |
| 2496 | st->state = TCP_SEQ_STATE_LISTENING; |
| 2497 | rc = listening_get_idx(seq, &pos); |
| 2498 | |
| 2499 | if (!rc) { |
| 2500 | st->state = TCP_SEQ_STATE_ESTABLISHED; |
| 2501 | rc = established_get_idx(seq, pos); |
| 2502 | } |
| 2503 | |
| 2504 | return rc; |
| 2505 | } |
| 2506 | |
| 2507 | static void *tcp_seek_last_pos(struct seq_file *seq) |
| 2508 | { |
| 2509 | struct tcp_iter_state *st = seq->private; |
| 2510 | int offset = st->offset; |
| 2511 | int orig_num = st->num; |
| 2512 | void *rc = NULL; |
| 2513 | |
| 2514 | switch (st->state) { |
| 2515 | case TCP_SEQ_STATE_OPENREQ: |
| 2516 | case TCP_SEQ_STATE_LISTENING: |
| 2517 | if (st->bucket >= INET_LHTABLE_SIZE) |
| 2518 | break; |
| 2519 | st->state = TCP_SEQ_STATE_LISTENING; |
| 2520 | rc = listening_get_next(seq, NULL); |
| 2521 | while (offset-- && rc) |
| 2522 | rc = listening_get_next(seq, rc); |
| 2523 | if (rc) |
| 2524 | break; |
| 2525 | st->bucket = 0; |
| 2526 | /* Fallthrough */ |
| 2527 | case TCP_SEQ_STATE_ESTABLISHED: |
| 2528 | case TCP_SEQ_STATE_TIME_WAIT: |
| 2529 | st->state = TCP_SEQ_STATE_ESTABLISHED; |
| 2530 | if (st->bucket > tcp_hashinfo.ehash_mask) |
| 2531 | break; |
| 2532 | rc = established_get_first(seq); |
| 2533 | while (offset-- && rc) |
| 2534 | rc = established_get_next(seq, rc); |
| 2535 | } |
| 2536 | |
| 2537 | st->num = orig_num; |
| 2538 | |
| 2539 | return rc; |
| 2540 | } |
| 2541 | |
| 2542 | static void *tcp_seq_start(struct seq_file *seq, loff_t *pos) |
| 2543 | { |
| 2544 | struct tcp_iter_state *st = seq->private; |
| 2545 | void *rc; |
| 2546 | |
| 2547 | if (*pos && *pos == st->last_pos) { |
| 2548 | rc = tcp_seek_last_pos(seq); |
| 2549 | if (rc) |
| 2550 | goto out; |
| 2551 | } |
| 2552 | |
| 2553 | st->state = TCP_SEQ_STATE_LISTENING; |
| 2554 | st->num = 0; |
| 2555 | st->bucket = 0; |
| 2556 | st->offset = 0; |
| 2557 | rc = *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 2558 | |
| 2559 | out: |
| 2560 | st->last_pos = *pos; |
| 2561 | return rc; |
| 2562 | } |
| 2563 | |
| 2564 | static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2565 | { |
| 2566 | struct tcp_iter_state *st = seq->private; |
| 2567 | void *rc = NULL; |
| 2568 | |
| 2569 | if (v == SEQ_START_TOKEN) { |
| 2570 | rc = tcp_get_idx(seq, 0); |
| 2571 | goto out; |
| 2572 | } |
| 2573 | |
| 2574 | switch (st->state) { |
| 2575 | case TCP_SEQ_STATE_OPENREQ: |
| 2576 | case TCP_SEQ_STATE_LISTENING: |
| 2577 | rc = listening_get_next(seq, v); |
| 2578 | if (!rc) { |
| 2579 | st->state = TCP_SEQ_STATE_ESTABLISHED; |
| 2580 | st->bucket = 0; |
| 2581 | st->offset = 0; |
| 2582 | rc = established_get_first(seq); |
| 2583 | } |
| 2584 | break; |
| 2585 | case TCP_SEQ_STATE_ESTABLISHED: |
| 2586 | case TCP_SEQ_STATE_TIME_WAIT: |
| 2587 | rc = established_get_next(seq, v); |
| 2588 | break; |
| 2589 | } |
| 2590 | out: |
| 2591 | ++*pos; |
| 2592 | st->last_pos = *pos; |
| 2593 | return rc; |
| 2594 | } |
| 2595 | |
| 2596 | static void tcp_seq_stop(struct seq_file *seq, void *v) |
| 2597 | { |
| 2598 | struct tcp_iter_state *st = seq->private; |
| 2599 | |
| 2600 | switch (st->state) { |
| 2601 | case TCP_SEQ_STATE_OPENREQ: |
| 2602 | if (v) { |
| 2603 | struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk); |
| 2604 | read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
| 2605 | } |
| 2606 | case TCP_SEQ_STATE_LISTENING: |
| 2607 | if (v != SEQ_START_TOKEN) |
| 2608 | spin_unlock_bh(&tcp_hashinfo.listening_hash[st->bucket].lock); |
| 2609 | break; |
| 2610 | case TCP_SEQ_STATE_TIME_WAIT: |
| 2611 | case TCP_SEQ_STATE_ESTABLISHED: |
| 2612 | if (v) |
| 2613 | spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket)); |
| 2614 | break; |
| 2615 | } |
| 2616 | } |
| 2617 | |
| 2618 | int tcp_seq_open(struct inode *inode, struct file *file) |
| 2619 | { |
| 2620 | struct tcp_seq_afinfo *afinfo = PDE_DATA(inode); |
| 2621 | struct tcp_iter_state *s; |
| 2622 | int err; |
| 2623 | |
| 2624 | err = seq_open_net(inode, file, &afinfo->seq_ops, |
| 2625 | sizeof(struct tcp_iter_state)); |
| 2626 | if (err < 0) |
| 2627 | return err; |
| 2628 | |
| 2629 | s = ((struct seq_file *)file->private_data)->private; |
| 2630 | s->family = afinfo->family; |
| 2631 | s->last_pos = 0; |
| 2632 | return 0; |
| 2633 | } |
| 2634 | EXPORT_SYMBOL(tcp_seq_open); |
| 2635 | |
| 2636 | int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo) |
| 2637 | { |
| 2638 | int rc = 0; |
| 2639 | struct proc_dir_entry *p; |
| 2640 | |
| 2641 | afinfo->seq_ops.start = tcp_seq_start; |
| 2642 | afinfo->seq_ops.next = tcp_seq_next; |
| 2643 | afinfo->seq_ops.stop = tcp_seq_stop; |
| 2644 | |
| 2645 | p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net, |
| 2646 | afinfo->seq_fops, afinfo); |
| 2647 | if (!p) |
| 2648 | rc = -ENOMEM; |
| 2649 | return rc; |
| 2650 | } |
| 2651 | EXPORT_SYMBOL(tcp_proc_register); |
| 2652 | |
| 2653 | void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo) |
| 2654 | { |
| 2655 | remove_proc_entry(afinfo->name, net->proc_net); |
| 2656 | } |
| 2657 | EXPORT_SYMBOL(tcp_proc_unregister); |
| 2658 | |
| 2659 | static void get_openreq4(const struct sock *sk, const struct request_sock *req, |
| 2660 | struct seq_file *f, int i, kuid_t uid, int *len) |
| 2661 | { |
| 2662 | const struct inet_request_sock *ireq = inet_rsk(req); |
| 2663 | long delta = req->expires - jiffies; |
| 2664 | |
| 2665 | seq_printf(f, "%4d: %08X:%04X %08X:%04X" |
| 2666 | " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %pK%n", |
| 2667 | i, |
| 2668 | ireq->loc_addr, |
| 2669 | ntohs(inet_sk(sk)->inet_sport), |
| 2670 | ireq->rmt_addr, |
| 2671 | ntohs(ireq->rmt_port), |
| 2672 | TCP_SYN_RECV, |
| 2673 | 0, 0, /* could print option size, but that is af dependent. */ |
| 2674 | 1, /* timers active (only the expire timer) */ |
| 2675 | jiffies_delta_to_clock_t(delta), |
| 2676 | req->num_timeout, |
| 2677 | from_kuid_munged(seq_user_ns(f), uid), |
| 2678 | 0, /* non standard timer */ |
| 2679 | 0, /* open_requests have no inode */ |
| 2680 | atomic_read(&sk->sk_refcnt), |
| 2681 | req, |
| 2682 | len); |
| 2683 | } |
| 2684 | |
| 2685 | static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len) |
| 2686 | { |
| 2687 | int timer_active; |
| 2688 | unsigned long timer_expires; |
| 2689 | const struct tcp_sock *tp = tcp_sk(sk); |
| 2690 | const struct inet_connection_sock *icsk = inet_csk(sk); |
| 2691 | const struct inet_sock *inet = inet_sk(sk); |
| 2692 | struct fastopen_queue *fastopenq = icsk->icsk_accept_queue.fastopenq; |
| 2693 | __be32 dest = inet->inet_daddr; |
| 2694 | __be32 src = inet->inet_rcv_saddr; |
| 2695 | __u16 destp = ntohs(inet->inet_dport); |
| 2696 | __u16 srcp = ntohs(inet->inet_sport); |
| 2697 | int rx_queue; |
| 2698 | |
| 2699 | if (icsk->icsk_pending == ICSK_TIME_RETRANS || |
| 2700 | icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS || |
| 2701 | icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) { |
| 2702 | timer_active = 1; |
| 2703 | timer_expires = icsk->icsk_timeout; |
| 2704 | } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { |
| 2705 | timer_active = 4; |
| 2706 | timer_expires = icsk->icsk_timeout; |
| 2707 | } else if (timer_pending(&sk->sk_timer)) { |
| 2708 | timer_active = 2; |
| 2709 | timer_expires = sk->sk_timer.expires; |
| 2710 | } else { |
| 2711 | timer_active = 0; |
| 2712 | timer_expires = jiffies; |
| 2713 | } |
| 2714 | |
| 2715 | if (sk->sk_state == TCP_LISTEN) |
| 2716 | rx_queue = sk->sk_ack_backlog; |
| 2717 | else |
| 2718 | /* |
| 2719 | * because we dont lock socket, we might find a transient negative value |
| 2720 | */ |
| 2721 | rx_queue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0); |
| 2722 | |
| 2723 | seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX " |
| 2724 | "%08X %5d %8d %lu %d %pK %lu %lu %u %u %d%n", |
| 2725 | i, src, srcp, dest, destp, sk->sk_state, |
| 2726 | tp->write_seq - tp->snd_una, |
| 2727 | rx_queue, |
| 2728 | timer_active, |
| 2729 | jiffies_delta_to_clock_t(timer_expires - jiffies), |
| 2730 | icsk->icsk_retransmits, |
| 2731 | from_kuid_munged(seq_user_ns(f), sock_i_uid(sk)), |
| 2732 | icsk->icsk_probes_out, |
| 2733 | sock_i_ino(sk), |
| 2734 | atomic_read(&sk->sk_refcnt), sk, |
| 2735 | jiffies_to_clock_t(icsk->icsk_rto), |
| 2736 | jiffies_to_clock_t(icsk->icsk_ack.ato), |
| 2737 | (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong, |
| 2738 | tp->snd_cwnd, |
| 2739 | sk->sk_state == TCP_LISTEN ? |
| 2740 | (fastopenq ? fastopenq->max_qlen : 0) : |
| 2741 | (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh), |
| 2742 | len); |
| 2743 | } |
| 2744 | |
| 2745 | static void get_timewait4_sock(const struct inet_timewait_sock *tw, |
| 2746 | struct seq_file *f, int i, int *len) |
| 2747 | { |
| 2748 | __be32 dest, src; |
| 2749 | __u16 destp, srcp; |
| 2750 | long delta = tw->tw_ttd - jiffies; |
| 2751 | |
| 2752 | dest = tw->tw_daddr; |
| 2753 | src = tw->tw_rcv_saddr; |
| 2754 | destp = ntohs(tw->tw_dport); |
| 2755 | srcp = ntohs(tw->tw_sport); |
| 2756 | |
| 2757 | seq_printf(f, "%4d: %08X:%04X %08X:%04X" |
| 2758 | " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK%n", |
| 2759 | i, src, srcp, dest, destp, tw->tw_substate, 0, 0, |
| 2760 | 3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0, |
| 2761 | atomic_read(&tw->tw_refcnt), tw, len); |
| 2762 | } |
| 2763 | |
| 2764 | #define TMPSZ 150 |
| 2765 | |
| 2766 | static int tcp4_seq_show(struct seq_file *seq, void *v) |
| 2767 | { |
| 2768 | struct tcp_iter_state *st; |
| 2769 | int len; |
| 2770 | |
| 2771 | if (v == SEQ_START_TOKEN) { |
| 2772 | seq_printf(seq, "%-*s\n", TMPSZ - 1, |
| 2773 | " sl local_address rem_address st tx_queue " |
| 2774 | "rx_queue tr tm->when retrnsmt uid timeout " |
| 2775 | "inode"); |
| 2776 | goto out; |
| 2777 | } |
| 2778 | st = seq->private; |
| 2779 | |
| 2780 | switch (st->state) { |
| 2781 | case TCP_SEQ_STATE_LISTENING: |
| 2782 | case TCP_SEQ_STATE_ESTABLISHED: |
| 2783 | get_tcp4_sock(v, seq, st->num, &len); |
| 2784 | break; |
| 2785 | case TCP_SEQ_STATE_OPENREQ: |
| 2786 | get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len); |
| 2787 | break; |
| 2788 | case TCP_SEQ_STATE_TIME_WAIT: |
| 2789 | get_timewait4_sock(v, seq, st->num, &len); |
| 2790 | break; |
| 2791 | } |
| 2792 | seq_printf(seq, "%*s\n", TMPSZ - 1 - len, ""); |
| 2793 | out: |
| 2794 | return 0; |
| 2795 | } |
| 2796 | |
| 2797 | static const struct file_operations tcp_afinfo_seq_fops = { |
| 2798 | .owner = THIS_MODULE, |
| 2799 | .open = tcp_seq_open, |
| 2800 | .read = seq_read, |
| 2801 | .llseek = seq_lseek, |
| 2802 | .release = seq_release_net |
| 2803 | }; |
| 2804 | |
| 2805 | static struct tcp_seq_afinfo tcp4_seq_afinfo = { |
| 2806 | .name = "tcp", |
| 2807 | .family = AF_INET, |
| 2808 | .seq_fops = &tcp_afinfo_seq_fops, |
| 2809 | .seq_ops = { |
| 2810 | .show = tcp4_seq_show, |
| 2811 | }, |
| 2812 | }; |
| 2813 | |
| 2814 | static int __net_init tcp4_proc_init_net(struct net *net) |
| 2815 | { |
| 2816 | return tcp_proc_register(net, &tcp4_seq_afinfo); |
| 2817 | } |
| 2818 | |
| 2819 | static void __net_exit tcp4_proc_exit_net(struct net *net) |
| 2820 | { |
| 2821 | tcp_proc_unregister(net, &tcp4_seq_afinfo); |
| 2822 | } |
| 2823 | |
| 2824 | static struct pernet_operations tcp4_net_ops = { |
| 2825 | .init = tcp4_proc_init_net, |
| 2826 | .exit = tcp4_proc_exit_net, |
| 2827 | }; |
| 2828 | |
| 2829 | int __init tcp4_proc_init(void) |
| 2830 | { |
| 2831 | return register_pernet_subsys(&tcp4_net_ops); |
| 2832 | } |
| 2833 | |
| 2834 | void tcp4_proc_exit(void) |
| 2835 | { |
| 2836 | unregister_pernet_subsys(&tcp4_net_ops); |
| 2837 | } |
| 2838 | #endif /* CONFIG_PROC_FS */ |
| 2839 | |
| 2840 | struct sk_buff **tcp4_gro_receive(struct sk_buff **head, struct sk_buff *skb) |
| 2841 | { |
| 2842 | const struct iphdr *iph = skb_gro_network_header(skb); |
| 2843 | __wsum wsum; |
| 2844 | __sum16 sum; |
| 2845 | |
| 2846 | switch (skb->ip_summed) { |
| 2847 | case CHECKSUM_COMPLETE: |
| 2848 | if (!tcp_v4_check(skb_gro_len(skb), iph->saddr, iph->daddr, |
| 2849 | skb->csum)) { |
| 2850 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 2851 | break; |
| 2852 | } |
| 2853 | flush: |
| 2854 | NAPI_GRO_CB(skb)->flush = 1; |
| 2855 | return NULL; |
| 2856 | |
| 2857 | case CHECKSUM_NONE: |
| 2858 | wsum = csum_tcpudp_nofold(iph->saddr, iph->daddr, |
| 2859 | skb_gro_len(skb), IPPROTO_TCP, 0); |
| 2860 | sum = csum_fold(skb_checksum(skb, |
| 2861 | skb_gro_offset(skb), |
| 2862 | skb_gro_len(skb), |
| 2863 | wsum)); |
| 2864 | if (sum) |
| 2865 | goto flush; |
| 2866 | |
| 2867 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 2868 | break; |
| 2869 | } |
| 2870 | |
| 2871 | return tcp_gro_receive(head, skb); |
| 2872 | } |
| 2873 | |
| 2874 | int tcp4_gro_complete(struct sk_buff *skb) |
| 2875 | { |
| 2876 | const struct iphdr *iph = ip_hdr(skb); |
| 2877 | struct tcphdr *th = tcp_hdr(skb); |
| 2878 | |
| 2879 | th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb), |
| 2880 | iph->saddr, iph->daddr, 0); |
| 2881 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4; |
| 2882 | |
| 2883 | return tcp_gro_complete(skb); |
| 2884 | } |
| 2885 | |
| 2886 | struct proto tcp_prot = { |
| 2887 | .name = "TCP", |
| 2888 | .owner = THIS_MODULE, |
| 2889 | .close = tcp_close, |
| 2890 | .connect = tcp_v4_connect, |
| 2891 | .disconnect = tcp_disconnect, |
| 2892 | .accept = inet_csk_accept, |
| 2893 | .ioctl = tcp_ioctl, |
| 2894 | .init = tcp_v4_init_sock, |
| 2895 | .destroy = tcp_v4_destroy_sock, |
| 2896 | .shutdown = tcp_shutdown, |
| 2897 | .setsockopt = tcp_setsockopt, |
| 2898 | .getsockopt = tcp_getsockopt, |
| 2899 | .recvmsg = tcp_recvmsg, |
| 2900 | .sendmsg = tcp_sendmsg, |
| 2901 | .sendpage = tcp_sendpage, |
| 2902 | .backlog_rcv = tcp_v4_do_rcv, |
| 2903 | .release_cb = tcp_release_cb, |
| 2904 | .hash = inet_hash, |
| 2905 | .unhash = inet_unhash, |
| 2906 | .get_port = inet_csk_get_port, |
| 2907 | .enter_memory_pressure = tcp_enter_memory_pressure, |
| 2908 | .sockets_allocated = &tcp_sockets_allocated, |
| 2909 | .orphan_count = &tcp_orphan_count, |
| 2910 | .memory_allocated = &tcp_memory_allocated, |
| 2911 | .memory_pressure = &tcp_memory_pressure, |
| 2912 | .sysctl_wmem = sysctl_tcp_wmem, |
| 2913 | .sysctl_rmem = sysctl_tcp_rmem, |
| 2914 | .max_header = MAX_TCP_HEADER, |
| 2915 | .obj_size = sizeof(struct tcp_sock), |
| 2916 | .slab_flags = SLAB_DESTROY_BY_RCU, |
| 2917 | .twsk_prot = &tcp_timewait_sock_ops, |
| 2918 | .rsk_prot = &tcp_request_sock_ops, |
| 2919 | .h.hashinfo = &tcp_hashinfo, |
| 2920 | .no_autobind = true, |
| 2921 | #ifdef CONFIG_COMPAT |
| 2922 | .compat_setsockopt = compat_tcp_setsockopt, |
| 2923 | .compat_getsockopt = compat_tcp_getsockopt, |
| 2924 | #endif |
| 2925 | #ifdef CONFIG_MEMCG_KMEM |
| 2926 | .init_cgroup = tcp_init_cgroup, |
| 2927 | .destroy_cgroup = tcp_destroy_cgroup, |
| 2928 | .proto_cgroup = tcp_proto_cgroup, |
| 2929 | #endif |
| 2930 | .diag_destroy = tcp_abort, |
| 2931 | }; |
| 2932 | EXPORT_SYMBOL(tcp_prot); |
| 2933 | |
| 2934 | static void __net_exit tcp_sk_exit(struct net *net) |
| 2935 | { |
| 2936 | int cpu; |
| 2937 | |
| 2938 | for_each_possible_cpu(cpu) |
| 2939 | inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.tcp_sk, cpu)); |
| 2940 | free_percpu(net->ipv4.tcp_sk); |
| 2941 | } |
| 2942 | |
| 2943 | static int __net_init tcp_sk_init(struct net *net) |
| 2944 | { |
| 2945 | int res, cpu; |
| 2946 | |
| 2947 | net->ipv4.tcp_sk = alloc_percpu(struct sock *); |
| 2948 | if (!net->ipv4.tcp_sk) |
| 2949 | return -ENOMEM; |
| 2950 | |
| 2951 | for_each_possible_cpu(cpu) { |
| 2952 | struct sock *sk; |
| 2953 | |
| 2954 | res = inet_ctl_sock_create(&sk, PF_INET, SOCK_RAW, |
| 2955 | IPPROTO_TCP, net); |
| 2956 | if (res) |
| 2957 | goto fail; |
| 2958 | *per_cpu_ptr(net->ipv4.tcp_sk, cpu) = sk; |
| 2959 | } |
| 2960 | net->ipv4.sysctl_tcp_ecn = 2; |
| 2961 | return 0; |
| 2962 | |
| 2963 | fail: |
| 2964 | tcp_sk_exit(net); |
| 2965 | |
| 2966 | return res; |
| 2967 | } |
| 2968 | |
| 2969 | static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list) |
| 2970 | { |
| 2971 | inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET); |
| 2972 | } |
| 2973 | |
| 2974 | static struct pernet_operations __net_initdata tcp_sk_ops = { |
| 2975 | .init = tcp_sk_init, |
| 2976 | .exit = tcp_sk_exit, |
| 2977 | .exit_batch = tcp_sk_exit_batch, |
| 2978 | }; |
| 2979 | |
| 2980 | void __init tcp_v4_init(void) |
| 2981 | { |
| 2982 | inet_hashinfo_init(&tcp_hashinfo); |
| 2983 | if (register_pernet_subsys(&tcp_sk_ops)) |
| 2984 | panic("Failed to create the TCP control socket.\n"); |
| 2985 | } |