Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 37 | * request_sock handling and moved |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * most of it into the af independent code. |
| 39 | * Added tail drop and some other bugfixes. |
Stephen Hemminger | caa20d9a | 2005-11-10 17:13:47 -0800 | [diff] [blame] | 40 | * Added new listen semantics. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
| 54 | #include <linux/types.h> |
| 55 | #include <linux/fcntl.h> |
| 56 | #include <linux/module.h> |
| 57 | #include <linux/random.h> |
| 58 | #include <linux/cache.h> |
| 59 | #include <linux/jhash.h> |
| 60 | #include <linux/init.h> |
| 61 | #include <linux/times.h> |
| 62 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 63 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #include <net/icmp.h> |
Arnaldo Carvalho de Melo | 304a161 | 2005-08-09 19:59:20 -0700 | [diff] [blame] | 65 | #include <net/inet_hashtables.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | #include <net/tcp.h> |
Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 67 | #include <net/transp_v6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <net/ipv6.h> |
| 69 | #include <net/inet_common.h> |
Arnaldo Carvalho de Melo | 6d6ee43 | 2005-12-13 23:25:19 -0800 | [diff] [blame] | 70 | #include <net/timewait_sock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | #include <net/xfrm.h> |
Chris Leech | 1a2449a | 2006-05-23 18:05:53 -0700 | [diff] [blame] | 72 | #include <net/netdma.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | #include <linux/inet.h> |
| 75 | #include <linux/ipv6.h> |
| 76 | #include <linux/stddef.h> |
| 77 | #include <linux/proc_fs.h> |
| 78 | #include <linux/seq_file.h> |
| 79 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 80 | #include <linux/crypto.h> |
| 81 | #include <linux/scatterlist.h> |
| 82 | |
Brian Haley | ab32ea5 | 2006-09-22 14:15:41 -0700 | [diff] [blame] | 83 | int sysctl_tcp_tw_reuse __read_mostly; |
| 84 | int sysctl_tcp_low_latency __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 87 | #ifdef CONFIG_TCP_MD5SIG |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 88 | static struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk, |
| 89 | __be32 addr); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 90 | static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key, |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 91 | __be32 saddr, __be32 daddr, |
YOSHIFUJI Hideaki | 076fb72 | 2008-04-17 12:48:12 +0900 | [diff] [blame] | 92 | struct tcphdr *th, unsigned int tcplen); |
YOSHIFUJI Hideaki | 9501f97 | 2008-04-18 12:45:16 +0900 | [diff] [blame] | 93 | #else |
| 94 | static inline |
| 95 | struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr) |
| 96 | { |
| 97 | return NULL; |
| 98 | } |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 99 | #endif |
| 100 | |
Arnaldo Carvalho de Melo | 0f7ff92 | 2005-08-09 19:59:44 -0700 | [diff] [blame] | 101 | struct inet_hashinfo __cacheline_aligned tcp_hashinfo = { |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 102 | .lhash_lock = __RW_LOCK_UNLOCKED(tcp_hashinfo.lhash_lock), |
| 103 | .lhash_users = ATOMIC_INIT(0), |
| 104 | .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(tcp_hashinfo.lhash_wait), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Gerrit Renker | a94f723 | 2006-11-10 14:06:49 -0800 | [diff] [blame] | 107 | static inline __u32 tcp_v4_init_sequence(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 109 | return secure_tcp_sequence_number(ip_hdr(skb)->daddr, |
| 110 | ip_hdr(skb)->saddr, |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 111 | tcp_hdr(skb)->dest, |
| 112 | tcp_hdr(skb)->source); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Arnaldo Carvalho de Melo | 6d6ee43 | 2005-12-13 23:25:19 -0800 | [diff] [blame] | 115 | int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp) |
| 116 | { |
| 117 | const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw); |
| 118 | struct tcp_sock *tp = tcp_sk(sk); |
| 119 | |
| 120 | /* With PAWS, it is safe from the viewpoint |
| 121 | of data integrity. Even without PAWS it is safe provided sequence |
| 122 | spaces do not overlap i.e. at data rates <= 80Mbit/sec. |
| 123 | |
| 124 | Actually, the idea is close to VJ's one, only timestamp cache is |
| 125 | held not per host, but per port pair and TW bucket is used as state |
| 126 | holder. |
| 127 | |
| 128 | If TW bucket has been already destroyed we fall back to VJ's scheme |
| 129 | and use initial timestamp retrieved from peer table. |
| 130 | */ |
| 131 | if (tcptw->tw_ts_recent_stamp && |
| 132 | (twp == NULL || (sysctl_tcp_tw_reuse && |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 133 | get_seconds() - tcptw->tw_ts_recent_stamp > 1))) { |
Arnaldo Carvalho de Melo | 6d6ee43 | 2005-12-13 23:25:19 -0800 | [diff] [blame] | 134 | tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2; |
| 135 | if (tp->write_seq == 0) |
| 136 | tp->write_seq = 1; |
| 137 | tp->rx_opt.ts_recent = tcptw->tw_ts_recent; |
| 138 | tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp; |
| 139 | sock_hold(sktw); |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | EXPORT_SYMBOL_GPL(tcp_twsk_unique); |
| 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | /* This will initiate an outgoing connection. */ |
| 149 | int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len) |
| 150 | { |
| 151 | struct inet_sock *inet = inet_sk(sk); |
| 152 | struct tcp_sock *tp = tcp_sk(sk); |
| 153 | struct sockaddr_in *usin = (struct sockaddr_in *)uaddr; |
| 154 | struct rtable *rt; |
Al Viro | bada8ad | 2006-09-26 21:27:15 -0700 | [diff] [blame] | 155 | __be32 daddr, nexthop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | int tmp; |
| 157 | int err; |
| 158 | |
| 159 | if (addr_len < sizeof(struct sockaddr_in)) |
| 160 | return -EINVAL; |
| 161 | |
| 162 | if (usin->sin_family != AF_INET) |
| 163 | return -EAFNOSUPPORT; |
| 164 | |
| 165 | nexthop = daddr = usin->sin_addr.s_addr; |
| 166 | if (inet->opt && inet->opt->srr) { |
| 167 | if (!daddr) |
| 168 | return -EINVAL; |
| 169 | nexthop = inet->opt->faddr; |
| 170 | } |
| 171 | |
| 172 | tmp = ip_route_connect(&rt, nexthop, inet->saddr, |
| 173 | RT_CONN_FLAGS(sk), sk->sk_bound_dev_if, |
| 174 | IPPROTO_TCP, |
David S. Miller | 8eb9086 | 2007-02-08 02:09:21 -0800 | [diff] [blame] | 175 | inet->sport, usin->sin_port, sk, 1); |
Wei Dong | 584bdf8 | 2007-05-31 22:49:28 -0700 | [diff] [blame] | 176 | if (tmp < 0) { |
| 177 | if (tmp == -ENETUNREACH) |
| 178 | IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return tmp; |
Wei Dong | 584bdf8 | 2007-05-31 22:49:28 -0700 | [diff] [blame] | 180 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) { |
| 183 | ip_rt_put(rt); |
| 184 | return -ENETUNREACH; |
| 185 | } |
| 186 | |
| 187 | if (!inet->opt || !inet->opt->srr) |
| 188 | daddr = rt->rt_dst; |
| 189 | |
| 190 | if (!inet->saddr) |
| 191 | inet->saddr = rt->rt_src; |
| 192 | inet->rcv_saddr = inet->saddr; |
| 193 | |
| 194 | if (tp->rx_opt.ts_recent_stamp && inet->daddr != daddr) { |
| 195 | /* Reset inherited state */ |
| 196 | tp->rx_opt.ts_recent = 0; |
| 197 | tp->rx_opt.ts_recent_stamp = 0; |
| 198 | tp->write_seq = 0; |
| 199 | } |
| 200 | |
Arnaldo Carvalho de Melo | 295ff7e | 2005-08-09 20:44:40 -0700 | [diff] [blame] | 201 | if (tcp_death_row.sysctl_tw_recycle && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | !tp->rx_opt.ts_recent_stamp && rt->rt_dst == daddr) { |
| 203 | struct inet_peer *peer = rt_get_peer(rt); |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 204 | /* |
| 205 | * VJ's idea. We save last timestamp seen from |
| 206 | * the destination in peer table, when entering state |
| 207 | * TIME-WAIT * and initialize rx_opt.ts_recent from it, |
| 208 | * when trying new connection. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | */ |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 210 | if (peer != NULL && |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 211 | peer->tcp_ts_stamp + TCP_PAWS_MSL >= get_seconds()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp; |
| 213 | tp->rx_opt.ts_recent = peer->tcp_ts; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | inet->dport = usin->sin_port; |
| 218 | inet->daddr = daddr; |
| 219 | |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 220 | inet_csk(sk)->icsk_ext_hdr_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | if (inet->opt) |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 222 | inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | tp->rx_opt.mss_clamp = 536; |
| 225 | |
| 226 | /* Socket identity is still unknown (sport may be zero). |
| 227 | * However we set state to SYN-SENT and not releasing socket |
| 228 | * lock select source port, enter ourselves into the hash tables and |
| 229 | * complete initialization after this. |
| 230 | */ |
| 231 | tcp_set_state(sk, TCP_SYN_SENT); |
Arnaldo Carvalho de Melo | a7f5e7f | 2005-12-13 23:25:31 -0800 | [diff] [blame] | 232 | err = inet_hash_connect(&tcp_death_row, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | if (err) |
| 234 | goto failure; |
| 235 | |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 236 | err = ip_route_newports(&rt, IPPROTO_TCP, |
| 237 | inet->sport, inet->dport, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (err) |
| 239 | goto failure; |
| 240 | |
| 241 | /* OK, now commit destination to socket. */ |
Herbert Xu | bcd7611 | 2006-06-30 13:36:35 -0700 | [diff] [blame] | 242 | sk->sk_gso_type = SKB_GSO_TCPV4; |
Arnaldo Carvalho de Melo | 6cbb0df | 2005-08-09 19:49:02 -0700 | [diff] [blame] | 243 | sk_setup_caps(sk, &rt->u.dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | if (!tp->write_seq) |
| 246 | tp->write_seq = secure_tcp_sequence_number(inet->saddr, |
| 247 | inet->daddr, |
| 248 | inet->sport, |
| 249 | usin->sin_port); |
| 250 | |
| 251 | inet->id = tp->write_seq ^ jiffies; |
| 252 | |
| 253 | err = tcp_connect(sk); |
| 254 | rt = NULL; |
| 255 | if (err) |
| 256 | goto failure; |
| 257 | |
| 258 | return 0; |
| 259 | |
| 260 | failure: |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 261 | /* |
| 262 | * This unhashes the socket and releases the local port, |
| 263 | * if necessary. |
| 264 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | tcp_set_state(sk, TCP_CLOSE); |
| 266 | ip_rt_put(rt); |
| 267 | sk->sk_route_caps = 0; |
| 268 | inet->dport = 0; |
| 269 | return err; |
| 270 | } |
| 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | /* |
| 273 | * This routine does path mtu discovery as defined in RFC1191. |
| 274 | */ |
Stephen Hemminger | 40efc6f | 2006-01-03 16:03:49 -0800 | [diff] [blame] | 275 | static void do_pmtu_discovery(struct sock *sk, struct iphdr *iph, u32 mtu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
| 277 | struct dst_entry *dst; |
| 278 | struct inet_sock *inet = inet_sk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
| 280 | /* We are not interested in TCP_LISTEN and open_requests (SYN-ACKs |
| 281 | * send out by Linux are always <576bytes so they should go through |
| 282 | * unfragmented). |
| 283 | */ |
| 284 | if (sk->sk_state == TCP_LISTEN) |
| 285 | return; |
| 286 | |
| 287 | /* We don't check in the destentry if pmtu discovery is forbidden |
| 288 | * on this route. We just assume that no packet_to_big packets |
| 289 | * are send back when pmtu discovery is not active. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 290 | * There is a small race when the user changes this flag in the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | * route, but I think that's acceptable. |
| 292 | */ |
| 293 | if ((dst = __sk_dst_check(sk, 0)) == NULL) |
| 294 | return; |
| 295 | |
| 296 | dst->ops->update_pmtu(dst, mtu); |
| 297 | |
| 298 | /* Something is about to be wrong... Remember soft error |
| 299 | * for the case, if this connection will not able to recover. |
| 300 | */ |
| 301 | if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst)) |
| 302 | sk->sk_err_soft = EMSGSIZE; |
| 303 | |
| 304 | mtu = dst_mtu(dst); |
| 305 | |
| 306 | if (inet->pmtudisc != IP_PMTUDISC_DONT && |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 307 | inet_csk(sk)->icsk_pmtu_cookie > mtu) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | tcp_sync_mss(sk, mtu); |
| 309 | |
| 310 | /* Resend the TCP packet because it's |
| 311 | * clear that the old packet has been |
| 312 | * dropped. This is the new "fast" path mtu |
| 313 | * discovery. |
| 314 | */ |
| 315 | tcp_simple_retransmit(sk); |
| 316 | } /* else let the usual retransmit timer handle it */ |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * This routine is called by the ICMP module when it gets some |
| 321 | * sort of error condition. If err < 0 then the socket should |
| 322 | * be closed and the error returned to the user. If err > 0 |
| 323 | * it's just the icmp type << 8 | icmp code. After adjustment |
| 324 | * header points to the first 8 bytes of the tcp header. We need |
| 325 | * to find the appropriate port. |
| 326 | * |
| 327 | * The locking strategy used here is very "optimistic". When |
| 328 | * someone else accesses the socket the ICMP is just dropped |
| 329 | * and for some paths there is no check at all. |
| 330 | * A more general error queue to queue errors for later handling |
| 331 | * is probably better. |
| 332 | * |
| 333 | */ |
| 334 | |
| 335 | void tcp_v4_err(struct sk_buff *skb, u32 info) |
| 336 | { |
| 337 | struct iphdr *iph = (struct iphdr *)skb->data; |
| 338 | struct tcphdr *th = (struct tcphdr *)(skb->data + (iph->ihl << 2)); |
| 339 | struct tcp_sock *tp; |
| 340 | struct inet_sock *inet; |
Arnaldo Carvalho de Melo | 88c7664 | 2007-03-13 14:43:18 -0300 | [diff] [blame] | 341 | const int type = icmp_hdr(skb)->type; |
| 342 | const int code = icmp_hdr(skb)->code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | struct sock *sk; |
| 344 | __u32 seq; |
| 345 | int err; |
Pavel Emelyanov | fd54d71 | 2008-07-14 23:01:40 -0700 | [diff] [blame] | 346 | struct net *net = dev_net(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | if (skb->len < (iph->ihl << 2) + 8) { |
Pavel Emelyanov | dcfc23c | 2008-07-14 23:03:00 -0700 | [diff] [blame^] | 349 | ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | |
Pavel Emelyanov | fd54d71 | 2008-07-14 23:01:40 -0700 | [diff] [blame] | 353 | sk = inet_lookup(net, &tcp_hashinfo, iph->daddr, th->dest, |
Pavel Emelyanov | c67499c | 2008-01-31 05:06:40 -0800 | [diff] [blame] | 354 | iph->saddr, th->source, inet_iif(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | if (!sk) { |
Pavel Emelyanov | dcfc23c | 2008-07-14 23:03:00 -0700 | [diff] [blame^] | 356 | ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | return; |
| 358 | } |
| 359 | if (sk->sk_state == TCP_TIME_WAIT) { |
YOSHIFUJI Hideaki | 9469c7b | 2006-10-10 19:41:46 -0700 | [diff] [blame] | 360 | inet_twsk_put(inet_twsk(sk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | return; |
| 362 | } |
| 363 | |
| 364 | bh_lock_sock(sk); |
| 365 | /* If too many ICMPs get dropped on busy |
| 366 | * servers this needs to be solved differently. |
| 367 | */ |
| 368 | if (sock_owned_by_user(sk)) |
| 369 | NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS); |
| 370 | |
| 371 | if (sk->sk_state == TCP_CLOSE) |
| 372 | goto out; |
| 373 | |
| 374 | tp = tcp_sk(sk); |
| 375 | seq = ntohl(th->seq); |
| 376 | if (sk->sk_state != TCP_LISTEN && |
| 377 | !between(seq, tp->snd_una, tp->snd_nxt)) { |
Eric Dumazet | 06ca719 | 2006-10-20 00:22:25 -0700 | [diff] [blame] | 378 | NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | goto out; |
| 380 | } |
| 381 | |
| 382 | switch (type) { |
| 383 | case ICMP_SOURCE_QUENCH: |
| 384 | /* Just silently ignore these. */ |
| 385 | goto out; |
| 386 | case ICMP_PARAMETERPROB: |
| 387 | err = EPROTO; |
| 388 | break; |
| 389 | case ICMP_DEST_UNREACH: |
| 390 | if (code > NR_ICMP_UNREACH) |
| 391 | goto out; |
| 392 | |
| 393 | if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */ |
| 394 | if (!sock_owned_by_user(sk)) |
| 395 | do_pmtu_discovery(sk, iph, info); |
| 396 | goto out; |
| 397 | } |
| 398 | |
| 399 | err = icmp_err_convert[code].errno; |
| 400 | break; |
| 401 | case ICMP_TIME_EXCEEDED: |
| 402 | err = EHOSTUNREACH; |
| 403 | break; |
| 404 | default: |
| 405 | goto out; |
| 406 | } |
| 407 | |
| 408 | switch (sk->sk_state) { |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 409 | struct request_sock *req, **prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | case TCP_LISTEN: |
| 411 | if (sock_owned_by_user(sk)) |
| 412 | goto out; |
| 413 | |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 414 | req = inet_csk_search_req(sk, &prev, th->dest, |
| 415 | iph->daddr, iph->saddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | if (!req) |
| 417 | goto out; |
| 418 | |
| 419 | /* ICMPs are not backlogged, hence we cannot get |
| 420 | an established socket here. |
| 421 | */ |
| 422 | BUG_TRAP(!req->sk); |
| 423 | |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 424 | if (seq != tcp_rsk(req)->snt_isn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS); |
| 426 | goto out; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Still in SYN_RECV, just remove it silently. |
| 431 | * There is no good way to pass the error to the newly |
| 432 | * created socket, and POSIX does not want network |
| 433 | * errors returned from accept(). |
| 434 | */ |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 435 | inet_csk_reqsk_queue_drop(sk, req, prev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | goto out; |
| 437 | |
| 438 | case TCP_SYN_SENT: |
| 439 | case TCP_SYN_RECV: /* Cannot happen. |
| 440 | It can f.e. if SYNs crossed. |
| 441 | */ |
| 442 | if (!sock_owned_by_user(sk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | sk->sk_err = err; |
| 444 | |
| 445 | sk->sk_error_report(sk); |
| 446 | |
| 447 | tcp_done(sk); |
| 448 | } else { |
| 449 | sk->sk_err_soft = err; |
| 450 | } |
| 451 | goto out; |
| 452 | } |
| 453 | |
| 454 | /* If we've already connected we will keep trying |
| 455 | * until we time out, or the user gives up. |
| 456 | * |
| 457 | * rfc1122 4.2.3.9 allows to consider as hard errors |
| 458 | * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too, |
| 459 | * but it is obsoleted by pmtu discovery). |
| 460 | * |
| 461 | * Note, that in modern internet, where routing is unreliable |
| 462 | * and in each dark corner broken firewalls sit, sending random |
| 463 | * errors ordered by their masters even this two messages finally lose |
| 464 | * their original sense (even Linux sends invalid PORT_UNREACHs) |
| 465 | * |
| 466 | * Now we are in compliance with RFCs. |
| 467 | * --ANK (980905) |
| 468 | */ |
| 469 | |
| 470 | inet = inet_sk(sk); |
| 471 | if (!sock_owned_by_user(sk) && inet->recverr) { |
| 472 | sk->sk_err = err; |
| 473 | sk->sk_error_report(sk); |
| 474 | } else { /* Only an error on timeout */ |
| 475 | sk->sk_err_soft = err; |
| 476 | } |
| 477 | |
| 478 | out: |
| 479 | bh_unlock_sock(sk); |
| 480 | sock_put(sk); |
| 481 | } |
| 482 | |
| 483 | /* This routine computes an IPv4 TCP checksum. */ |
Arnaldo Carvalho de Melo | 8292a17 | 2005-12-13 23:15:52 -0800 | [diff] [blame] | 484 | void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
| 486 | struct inet_sock *inet = inet_sk(sk); |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 487 | struct tcphdr *th = tcp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 489 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Frederik Deweerdt | ba7808e | 2007-02-04 20:15:27 -0800 | [diff] [blame] | 490 | th->check = ~tcp_v4_check(len, inet->saddr, |
| 491 | inet->daddr, 0); |
Herbert Xu | 663ead3 | 2007-04-09 11:59:07 -0700 | [diff] [blame] | 492 | skb->csum_start = skb_transport_header(skb) - skb->head; |
Al Viro | ff1dcad | 2006-11-20 18:07:29 -0800 | [diff] [blame] | 493 | skb->csum_offset = offsetof(struct tcphdr, check); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } else { |
Frederik Deweerdt | ba7808e | 2007-02-04 20:15:27 -0800 | [diff] [blame] | 495 | th->check = tcp_v4_check(len, inet->saddr, inet->daddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | csum_partial((char *)th, |
| 497 | th->doff << 2, |
| 498 | skb->csum)); |
| 499 | } |
| 500 | } |
| 501 | |
Herbert Xu | a430a43 | 2006-07-08 13:34:56 -0700 | [diff] [blame] | 502 | int tcp_v4_gso_send_check(struct sk_buff *skb) |
| 503 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 504 | const struct iphdr *iph; |
Herbert Xu | a430a43 | 2006-07-08 13:34:56 -0700 | [diff] [blame] | 505 | struct tcphdr *th; |
| 506 | |
| 507 | if (!pskb_may_pull(skb, sizeof(*th))) |
| 508 | return -EINVAL; |
| 509 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 510 | iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 511 | th = tcp_hdr(skb); |
Herbert Xu | a430a43 | 2006-07-08 13:34:56 -0700 | [diff] [blame] | 512 | |
| 513 | th->check = 0; |
Frederik Deweerdt | ba7808e | 2007-02-04 20:15:27 -0800 | [diff] [blame] | 514 | th->check = ~tcp_v4_check(skb->len, iph->saddr, iph->daddr, 0); |
Herbert Xu | 663ead3 | 2007-04-09 11:59:07 -0700 | [diff] [blame] | 515 | skb->csum_start = skb_transport_header(skb) - skb->head; |
Al Viro | ff1dcad | 2006-11-20 18:07:29 -0800 | [diff] [blame] | 516 | skb->csum_offset = offsetof(struct tcphdr, check); |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 517 | skb->ip_summed = CHECKSUM_PARTIAL; |
Herbert Xu | a430a43 | 2006-07-08 13:34:56 -0700 | [diff] [blame] | 518 | return 0; |
| 519 | } |
| 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | /* |
| 522 | * This routine will send an RST to the other tcp. |
| 523 | * |
| 524 | * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.) |
| 525 | * for reset. |
| 526 | * Answer: if a packet caused RST, it is not for a socket |
| 527 | * existing in our system, if it is matched to a socket, |
| 528 | * it is just duplicate segment or bug in other side's TCP. |
| 529 | * So that we build reply only basing on parameters |
| 530 | * arrived with segment. |
| 531 | * Exception: precedence violation. We do not implement it in any case. |
| 532 | */ |
| 533 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 534 | static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | { |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 536 | struct tcphdr *th = tcp_hdr(skb); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 537 | struct { |
| 538 | struct tcphdr th; |
| 539 | #ifdef CONFIG_TCP_MD5SIG |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 540 | __be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)]; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 541 | #endif |
| 542 | } rep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | struct ip_reply_arg arg; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 544 | #ifdef CONFIG_TCP_MD5SIG |
| 545 | struct tcp_md5sig_key *key; |
| 546 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | |
| 548 | /* Never send a reset in response to a reset. */ |
| 549 | if (th->rst) |
| 550 | return; |
| 551 | |
Eric Dumazet | ee6b967 | 2008-03-05 18:30:47 -0800 | [diff] [blame] | 552 | if (skb->rtable->rt_type != RTN_LOCAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return; |
| 554 | |
| 555 | /* Swap the send and the receive. */ |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 556 | memset(&rep, 0, sizeof(rep)); |
| 557 | rep.th.dest = th->source; |
| 558 | rep.th.source = th->dest; |
| 559 | rep.th.doff = sizeof(struct tcphdr) / 4; |
| 560 | rep.th.rst = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
| 562 | if (th->ack) { |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 563 | rep.th.seq = th->ack_seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | } else { |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 565 | rep.th.ack = 1; |
| 566 | rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin + |
| 567 | skb->len - (th->doff << 2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | } |
| 569 | |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 570 | memset(&arg, 0, sizeof(arg)); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 571 | arg.iov[0].iov_base = (unsigned char *)&rep; |
| 572 | arg.iov[0].iov_len = sizeof(rep.th); |
| 573 | |
| 574 | #ifdef CONFIG_TCP_MD5SIG |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 575 | key = sk ? tcp_v4_md5_do_lookup(sk, ip_hdr(skb)->daddr) : NULL; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 576 | if (key) { |
| 577 | rep.opt[0] = htonl((TCPOPT_NOP << 24) | |
| 578 | (TCPOPT_NOP << 16) | |
| 579 | (TCPOPT_MD5SIG << 8) | |
| 580 | TCPOLEN_MD5SIG); |
| 581 | /* Update length and the length the header thinks exists */ |
| 582 | arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED; |
| 583 | rep.th.doff = arg.iov[0].iov_len / 4; |
| 584 | |
| 585 | tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[1], |
| 586 | key, |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 587 | ip_hdr(skb)->daddr, |
| 588 | ip_hdr(skb)->saddr, |
YOSHIFUJI Hideaki | 076fb72 | 2008-04-17 12:48:12 +0900 | [diff] [blame] | 589 | &rep.th, arg.iov[0].iov_len); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 590 | } |
| 591 | #endif |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 592 | arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr, |
| 593 | ip_hdr(skb)->saddr, /* XXX */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | sizeof(struct tcphdr), IPPROTO_TCP, 0); |
| 595 | arg.csumoffset = offsetof(struct tcphdr, check) / 2; |
| 596 | |
Denis V. Lunev | 7feb49c | 2008-04-03 14:32:00 -0700 | [diff] [blame] | 597 | ip_send_reply(dev_net(skb->dst->dev)->ipv4.tcp_sock, skb, |
| 598 | &arg, arg.iov[0].iov_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
| 600 | TCP_INC_STATS_BH(TCP_MIB_OUTSEGS); |
| 601 | TCP_INC_STATS_BH(TCP_MIB_OUTRSTS); |
| 602 | } |
| 603 | |
| 604 | /* The code following below sending ACKs in SYN-RECV and TIME-WAIT states |
| 605 | outside socket context is ugly, certainly. What can I do? |
| 606 | */ |
| 607 | |
YOSHIFUJI Hideaki | 9501f97 | 2008-04-18 12:45:16 +0900 | [diff] [blame] | 608 | static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack, |
| 609 | u32 win, u32 ts, int oif, |
| 610 | struct tcp_md5sig_key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | { |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 612 | struct tcphdr *th = tcp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | struct { |
| 614 | struct tcphdr th; |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 615 | __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2) |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 616 | #ifdef CONFIG_TCP_MD5SIG |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 617 | + (TCPOLEN_MD5SIG_ALIGNED >> 2) |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 618 | #endif |
| 619 | ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | } rep; |
| 621 | struct ip_reply_arg arg; |
| 622 | |
| 623 | memset(&rep.th, 0, sizeof(struct tcphdr)); |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 624 | memset(&arg, 0, sizeof(arg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
| 626 | arg.iov[0].iov_base = (unsigned char *)&rep; |
| 627 | arg.iov[0].iov_len = sizeof(rep.th); |
| 628 | if (ts) { |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 629 | rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | |
| 630 | (TCPOPT_TIMESTAMP << 8) | |
| 631 | TCPOLEN_TIMESTAMP); |
| 632 | rep.opt[1] = htonl(tcp_time_stamp); |
| 633 | rep.opt[2] = htonl(ts); |
Craig Schlenter | cb48cfe | 2007-01-09 00:11:15 -0800 | [diff] [blame] | 634 | arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | /* Swap the send and the receive. */ |
| 638 | rep.th.dest = th->source; |
| 639 | rep.th.source = th->dest; |
| 640 | rep.th.doff = arg.iov[0].iov_len / 4; |
| 641 | rep.th.seq = htonl(seq); |
| 642 | rep.th.ack_seq = htonl(ack); |
| 643 | rep.th.ack = 1; |
| 644 | rep.th.window = htons(win); |
| 645 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 646 | #ifdef CONFIG_TCP_MD5SIG |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 647 | if (key) { |
| 648 | int offset = (ts) ? 3 : 0; |
| 649 | |
| 650 | rep.opt[offset++] = htonl((TCPOPT_NOP << 24) | |
| 651 | (TCPOPT_NOP << 16) | |
| 652 | (TCPOPT_MD5SIG << 8) | |
| 653 | TCPOLEN_MD5SIG); |
| 654 | arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED; |
| 655 | rep.th.doff = arg.iov[0].iov_len/4; |
| 656 | |
| 657 | tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[offset], |
| 658 | key, |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 659 | ip_hdr(skb)->daddr, |
| 660 | ip_hdr(skb)->saddr, |
YOSHIFUJI Hideaki | 076fb72 | 2008-04-17 12:48:12 +0900 | [diff] [blame] | 661 | &rep.th, arg.iov[0].iov_len); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 662 | } |
| 663 | #endif |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 664 | arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr, |
| 665 | ip_hdr(skb)->saddr, /* XXX */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | arg.iov[0].iov_len, IPPROTO_TCP, 0); |
| 667 | arg.csumoffset = offsetof(struct tcphdr, check) / 2; |
YOSHIFUJI Hideaki | 9501f97 | 2008-04-18 12:45:16 +0900 | [diff] [blame] | 668 | if (oif) |
| 669 | arg.bound_dev_if = oif; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | |
Denis V. Lunev | 7feb49c | 2008-04-03 14:32:00 -0700 | [diff] [blame] | 671 | ip_send_reply(dev_net(skb->dev)->ipv4.tcp_sock, skb, |
| 672 | &arg, arg.iov[0].iov_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
| 674 | TCP_INC_STATS_BH(TCP_MIB_OUTSEGS); |
| 675 | } |
| 676 | |
| 677 | static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb) |
| 678 | { |
Arnaldo Carvalho de Melo | 8feaf0c | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 679 | struct inet_timewait_sock *tw = inet_twsk(sk); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 680 | struct tcp_timewait_sock *tcptw = tcp_twsk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
YOSHIFUJI Hideaki | 9501f97 | 2008-04-18 12:45:16 +0900 | [diff] [blame] | 682 | tcp_v4_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt, |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 683 | tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale, |
YOSHIFUJI Hideaki | 9501f97 | 2008-04-18 12:45:16 +0900 | [diff] [blame] | 684 | tcptw->tw_ts_recent, |
| 685 | tw->tw_bound_dev_if, |
| 686 | tcp_twsk_md5_key(tcptw) |
| 687 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Arnaldo Carvalho de Melo | 8feaf0c | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 689 | inet_twsk_put(tw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 692 | static void tcp_v4_reqsk_send_ack(struct sk_buff *skb, |
| 693 | struct request_sock *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
YOSHIFUJI Hideaki | 9501f97 | 2008-04-18 12:45:16 +0900 | [diff] [blame] | 695 | tcp_v4_send_ack(skb, tcp_rsk(req)->snt_isn + 1, |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 696 | tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd, |
YOSHIFUJI Hideaki | 9501f97 | 2008-04-18 12:45:16 +0900 | [diff] [blame] | 697 | req->ts_recent, |
| 698 | 0, |
| 699 | tcp_v4_md5_do_lookup(skb->sk, ip_hdr(skb)->daddr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | /* |
Kris Katterjohn | 9bf1d83 | 2008-02-17 22:29:19 -0800 | [diff] [blame] | 703 | * Send a SYN-ACK after having received a SYN. |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 704 | * This still operates on a request_sock only, not on a big |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | * socket. |
| 706 | */ |
Denis V. Lunev | fd80eb9 | 2008-02-29 11:43:03 -0800 | [diff] [blame] | 707 | static int __tcp_v4_send_synack(struct sock *sk, struct request_sock *req, |
| 708 | struct dst_entry *dst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | { |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 710 | const struct inet_request_sock *ireq = inet_rsk(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | int err = -1; |
| 712 | struct sk_buff * skb; |
| 713 | |
| 714 | /* First, grab a route. */ |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 715 | if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL) |
Denis V. Lunev | fd80eb9 | 2008-02-29 11:43:03 -0800 | [diff] [blame] | 716 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | |
| 718 | skb = tcp_make_synack(sk, dst, req); |
| 719 | |
| 720 | if (skb) { |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 721 | struct tcphdr *th = tcp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | |
Frederik Deweerdt | ba7808e | 2007-02-04 20:15:27 -0800 | [diff] [blame] | 723 | th->check = tcp_v4_check(skb->len, |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 724 | ireq->loc_addr, |
| 725 | ireq->rmt_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | csum_partial((char *)th, skb->len, |
| 727 | skb->csum)); |
| 728 | |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 729 | err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr, |
| 730 | ireq->rmt_addr, |
| 731 | ireq->opt); |
Gerrit Renker | b9df3cb | 2006-11-14 11:21:36 -0200 | [diff] [blame] | 732 | err = net_xmit_eval(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | dst_release(dst); |
| 736 | return err; |
| 737 | } |
| 738 | |
Denis V. Lunev | fd80eb9 | 2008-02-29 11:43:03 -0800 | [diff] [blame] | 739 | static int tcp_v4_send_synack(struct sock *sk, struct request_sock *req) |
| 740 | { |
| 741 | return __tcp_v4_send_synack(sk, req, NULL); |
| 742 | } |
| 743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | /* |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 745 | * IPv4 request_sock destructor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | */ |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 747 | static void tcp_v4_reqsk_destructor(struct request_sock *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { |
Jesper Juhl | a51482b | 2005-11-08 09:41:34 -0800 | [diff] [blame] | 749 | kfree(inet_rsk(req)->opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Arnaldo Carvalho de Melo | 80e40da | 2006-01-04 01:58:06 -0200 | [diff] [blame] | 752 | #ifdef CONFIG_SYN_COOKIES |
Stephen Hemminger | 40efc6f | 2006-01-03 16:03:49 -0800 | [diff] [blame] | 753 | static void syn_flood_warning(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | { |
| 755 | static unsigned long warntime; |
| 756 | |
| 757 | if (time_after(jiffies, (warntime + HZ * 60))) { |
| 758 | warntime = jiffies; |
| 759 | printk(KERN_INFO |
| 760 | "possible SYN flooding on port %d. Sending cookies.\n", |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 761 | ntohs(tcp_hdr(skb)->dest)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | } |
| 763 | } |
Arnaldo Carvalho de Melo | 80e40da | 2006-01-04 01:58:06 -0200 | [diff] [blame] | 764 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
| 766 | /* |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 767 | * Save and compile IPv4 options into the request_sock if needed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | */ |
Stephen Hemminger | 40efc6f | 2006-01-03 16:03:49 -0800 | [diff] [blame] | 769 | static struct ip_options *tcp_v4_save_options(struct sock *sk, |
| 770 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | { |
| 772 | struct ip_options *opt = &(IPCB(skb)->opt); |
| 773 | struct ip_options *dopt = NULL; |
| 774 | |
| 775 | if (opt && opt->optlen) { |
| 776 | int opt_size = optlength(opt); |
| 777 | dopt = kmalloc(opt_size, GFP_ATOMIC); |
| 778 | if (dopt) { |
| 779 | if (ip_options_echo(dopt, skb)) { |
| 780 | kfree(dopt); |
| 781 | dopt = NULL; |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | return dopt; |
| 786 | } |
| 787 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 788 | #ifdef CONFIG_TCP_MD5SIG |
| 789 | /* |
| 790 | * RFC2385 MD5 checksumming requires a mapping of |
| 791 | * IP address->MD5 Key. |
| 792 | * We need to maintain these in the sk structure. |
| 793 | */ |
| 794 | |
| 795 | /* Find the Key structure for an address. */ |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 796 | static struct tcp_md5sig_key * |
| 797 | tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr) |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 798 | { |
| 799 | struct tcp_sock *tp = tcp_sk(sk); |
| 800 | int i; |
| 801 | |
| 802 | if (!tp->md5sig_info || !tp->md5sig_info->entries4) |
| 803 | return NULL; |
| 804 | for (i = 0; i < tp->md5sig_info->entries4; i++) { |
| 805 | if (tp->md5sig_info->keys4[i].addr == addr) |
David S. Miller | f8ab18d | 2007-09-28 15:18:35 -0700 | [diff] [blame] | 806 | return &tp->md5sig_info->keys4[i].base; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 807 | } |
| 808 | return NULL; |
| 809 | } |
| 810 | |
| 811 | struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk, |
| 812 | struct sock *addr_sk) |
| 813 | { |
| 814 | return tcp_v4_md5_do_lookup(sk, inet_sk(addr_sk)->daddr); |
| 815 | } |
| 816 | |
| 817 | EXPORT_SYMBOL(tcp_v4_md5_lookup); |
| 818 | |
Adrian Bunk | f5b99bc | 2006-11-30 17:22:29 -0800 | [diff] [blame] | 819 | static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk, |
| 820 | struct request_sock *req) |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 821 | { |
| 822 | return tcp_v4_md5_do_lookup(sk, inet_rsk(req)->rmt_addr); |
| 823 | } |
| 824 | |
| 825 | /* This can be called on a newly created socket, from other files */ |
| 826 | int tcp_v4_md5_do_add(struct sock *sk, __be32 addr, |
| 827 | u8 *newkey, u8 newkeylen) |
| 828 | { |
| 829 | /* Add Key to the list */ |
Matthias M. Dellweg | b0a713e | 2007-10-29 20:55:27 -0700 | [diff] [blame] | 830 | struct tcp_md5sig_key *key; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 831 | struct tcp_sock *tp = tcp_sk(sk); |
| 832 | struct tcp4_md5sig_key *keys; |
| 833 | |
Matthias M. Dellweg | b0a713e | 2007-10-29 20:55:27 -0700 | [diff] [blame] | 834 | key = tcp_v4_md5_do_lookup(sk, addr); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 835 | if (key) { |
| 836 | /* Pre-existing entry - just update that one. */ |
Matthias M. Dellweg | b0a713e | 2007-10-29 20:55:27 -0700 | [diff] [blame] | 837 | kfree(key->key); |
| 838 | key->key = newkey; |
| 839 | key->keylen = newkeylen; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 840 | } else { |
Arnaldo Carvalho de Melo | f668593 | 2006-11-17 11:06:01 -0200 | [diff] [blame] | 841 | struct tcp_md5sig_info *md5sig; |
| 842 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 843 | if (!tp->md5sig_info) { |
Arnaldo Carvalho de Melo | f668593 | 2006-11-17 11:06:01 -0200 | [diff] [blame] | 844 | tp->md5sig_info = kzalloc(sizeof(*tp->md5sig_info), |
| 845 | GFP_ATOMIC); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 846 | if (!tp->md5sig_info) { |
| 847 | kfree(newkey); |
| 848 | return -ENOMEM; |
| 849 | } |
David S. Miller | 3d7dbea | 2007-06-12 14:36:42 -0700 | [diff] [blame] | 850 | sk->sk_route_caps &= ~NETIF_F_GSO_MASK; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 851 | } |
| 852 | if (tcp_alloc_md5sig_pool() == NULL) { |
| 853 | kfree(newkey); |
| 854 | return -ENOMEM; |
| 855 | } |
Arnaldo Carvalho de Melo | f668593 | 2006-11-17 11:06:01 -0200 | [diff] [blame] | 856 | md5sig = tp->md5sig_info; |
| 857 | |
| 858 | if (md5sig->alloced4 == md5sig->entries4) { |
| 859 | keys = kmalloc((sizeof(*keys) * |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 860 | (md5sig->entries4 + 1)), GFP_ATOMIC); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 861 | if (!keys) { |
| 862 | kfree(newkey); |
| 863 | tcp_free_md5sig_pool(); |
| 864 | return -ENOMEM; |
| 865 | } |
| 866 | |
Arnaldo Carvalho de Melo | f668593 | 2006-11-17 11:06:01 -0200 | [diff] [blame] | 867 | if (md5sig->entries4) |
| 868 | memcpy(keys, md5sig->keys4, |
| 869 | sizeof(*keys) * md5sig->entries4); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 870 | |
| 871 | /* Free old key list, and reference new one */ |
YOSHIFUJI Hideaki | a80cc20 | 2007-11-20 17:30:06 -0800 | [diff] [blame] | 872 | kfree(md5sig->keys4); |
Arnaldo Carvalho de Melo | f668593 | 2006-11-17 11:06:01 -0200 | [diff] [blame] | 873 | md5sig->keys4 = keys; |
| 874 | md5sig->alloced4++; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 875 | } |
Arnaldo Carvalho de Melo | f668593 | 2006-11-17 11:06:01 -0200 | [diff] [blame] | 876 | md5sig->entries4++; |
David S. Miller | f8ab18d | 2007-09-28 15:18:35 -0700 | [diff] [blame] | 877 | md5sig->keys4[md5sig->entries4 - 1].addr = addr; |
| 878 | md5sig->keys4[md5sig->entries4 - 1].base.key = newkey; |
| 879 | md5sig->keys4[md5sig->entries4 - 1].base.keylen = newkeylen; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 880 | } |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | EXPORT_SYMBOL(tcp_v4_md5_do_add); |
| 885 | |
| 886 | static int tcp_v4_md5_add_func(struct sock *sk, struct sock *addr_sk, |
| 887 | u8 *newkey, u8 newkeylen) |
| 888 | { |
| 889 | return tcp_v4_md5_do_add(sk, inet_sk(addr_sk)->daddr, |
| 890 | newkey, newkeylen); |
| 891 | } |
| 892 | |
| 893 | int tcp_v4_md5_do_del(struct sock *sk, __be32 addr) |
| 894 | { |
| 895 | struct tcp_sock *tp = tcp_sk(sk); |
| 896 | int i; |
| 897 | |
| 898 | for (i = 0; i < tp->md5sig_info->entries4; i++) { |
| 899 | if (tp->md5sig_info->keys4[i].addr == addr) { |
| 900 | /* Free the key */ |
David S. Miller | f8ab18d | 2007-09-28 15:18:35 -0700 | [diff] [blame] | 901 | kfree(tp->md5sig_info->keys4[i].base.key); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 902 | tp->md5sig_info->entries4--; |
| 903 | |
| 904 | if (tp->md5sig_info->entries4 == 0) { |
| 905 | kfree(tp->md5sig_info->keys4); |
| 906 | tp->md5sig_info->keys4 = NULL; |
Leigh Brown | 8228a18d | 2006-12-17 17:12:30 -0800 | [diff] [blame] | 907 | tp->md5sig_info->alloced4 = 0; |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 908 | } else if (tp->md5sig_info->entries4 != i) { |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 909 | /* Need to do some manipulation */ |
YOSHIFUJI Hideaki | 354faf0 | 2007-11-20 17:30:31 -0800 | [diff] [blame] | 910 | memmove(&tp->md5sig_info->keys4[i], |
| 911 | &tp->md5sig_info->keys4[i+1], |
| 912 | (tp->md5sig_info->entries4 - i) * |
| 913 | sizeof(struct tcp4_md5sig_key)); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 914 | } |
| 915 | tcp_free_md5sig_pool(); |
| 916 | return 0; |
| 917 | } |
| 918 | } |
| 919 | return -ENOENT; |
| 920 | } |
| 921 | |
| 922 | EXPORT_SYMBOL(tcp_v4_md5_do_del); |
| 923 | |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 924 | static void tcp_v4_clear_md5_list(struct sock *sk) |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 925 | { |
| 926 | struct tcp_sock *tp = tcp_sk(sk); |
| 927 | |
| 928 | /* Free each key, then the set of key keys, |
| 929 | * the crypto element, and then decrement our |
| 930 | * hold on the last resort crypto. |
| 931 | */ |
| 932 | if (tp->md5sig_info->entries4) { |
| 933 | int i; |
| 934 | for (i = 0; i < tp->md5sig_info->entries4; i++) |
David S. Miller | f8ab18d | 2007-09-28 15:18:35 -0700 | [diff] [blame] | 935 | kfree(tp->md5sig_info->keys4[i].base.key); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 936 | tp->md5sig_info->entries4 = 0; |
| 937 | tcp_free_md5sig_pool(); |
| 938 | } |
| 939 | if (tp->md5sig_info->keys4) { |
| 940 | kfree(tp->md5sig_info->keys4); |
| 941 | tp->md5sig_info->keys4 = NULL; |
| 942 | tp->md5sig_info->alloced4 = 0; |
| 943 | } |
| 944 | } |
| 945 | |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 946 | static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval, |
| 947 | int optlen) |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 948 | { |
| 949 | struct tcp_md5sig cmd; |
| 950 | struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr; |
| 951 | u8 *newkey; |
| 952 | |
| 953 | if (optlen < sizeof(cmd)) |
| 954 | return -EINVAL; |
| 955 | |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 956 | if (copy_from_user(&cmd, optval, sizeof(cmd))) |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 957 | return -EFAULT; |
| 958 | |
| 959 | if (sin->sin_family != AF_INET) |
| 960 | return -EINVAL; |
| 961 | |
| 962 | if (!cmd.tcpm_key || !cmd.tcpm_keylen) { |
| 963 | if (!tcp_sk(sk)->md5sig_info) |
| 964 | return -ENOENT; |
| 965 | return tcp_v4_md5_do_del(sk, sin->sin_addr.s_addr); |
| 966 | } |
| 967 | |
| 968 | if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN) |
| 969 | return -EINVAL; |
| 970 | |
| 971 | if (!tcp_sk(sk)->md5sig_info) { |
| 972 | struct tcp_sock *tp = tcp_sk(sk); |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 973 | struct tcp_md5sig_info *p = kzalloc(sizeof(*p), GFP_KERNEL); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 974 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 975 | if (!p) |
| 976 | return -EINVAL; |
| 977 | |
| 978 | tp->md5sig_info = p; |
David S. Miller | 3d7dbea | 2007-06-12 14:36:42 -0700 | [diff] [blame] | 979 | sk->sk_route_caps &= ~NETIF_F_GSO_MASK; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 980 | } |
| 981 | |
Arnaldo Carvalho de Melo | f668593 | 2006-11-17 11:06:01 -0200 | [diff] [blame] | 982 | newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 983 | if (!newkey) |
| 984 | return -ENOMEM; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 985 | return tcp_v4_md5_do_add(sk, sin->sin_addr.s_addr, |
| 986 | newkey, cmd.tcpm_keylen); |
| 987 | } |
| 988 | |
| 989 | static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key, |
| 990 | __be32 saddr, __be32 daddr, |
YOSHIFUJI Hideaki | 076fb72 | 2008-04-17 12:48:12 +0900 | [diff] [blame] | 991 | struct tcphdr *th, |
YOSHIFUJI Hideaki | 9cb5734 | 2008-01-12 02:16:03 -0800 | [diff] [blame] | 992 | unsigned int tcplen) |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 993 | { |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 994 | struct tcp_md5sig_pool *hp; |
| 995 | struct tcp4_pseudohdr *bp; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 996 | int err; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 997 | |
| 998 | /* |
| 999 | * Okay, so RFC2385 is turned on for this connection, |
| 1000 | * so we need to generate the MD5 hash for the packet now. |
| 1001 | */ |
| 1002 | |
| 1003 | hp = tcp_get_md5sig_pool(); |
| 1004 | if (!hp) |
| 1005 | goto clear_hash_noput; |
| 1006 | |
| 1007 | bp = &hp->md5_blk.ip4; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1008 | |
| 1009 | /* |
YOSHIFUJI Hideaki | 8d26d76 | 2008-04-17 13:19:16 +0900 | [diff] [blame] | 1010 | * The TCP pseudo-header (in the order: source IP address, |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1011 | * destination IP address, zero-padded protocol number, and |
| 1012 | * segment length) |
| 1013 | */ |
| 1014 | bp->saddr = saddr; |
| 1015 | bp->daddr = daddr; |
| 1016 | bp->pad = 0; |
YOSHIFUJI Hideaki | 076fb72 | 2008-04-17 12:48:12 +0900 | [diff] [blame] | 1017 | bp->protocol = IPPROTO_TCP; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1018 | bp->len = htons(tcplen); |
David S. Miller | c7da57a | 2007-10-26 00:41:21 -0700 | [diff] [blame] | 1019 | |
YOSHIFUJI Hideaki | 8d26d76 | 2008-04-17 13:19:16 +0900 | [diff] [blame] | 1020 | err = tcp_calc_md5_hash(md5_hash, key, sizeof(*bp), |
| 1021 | th, tcplen, hp); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1022 | if (err) |
| 1023 | goto clear_hash; |
| 1024 | |
YOSHIFUJI Hideaki | 8d26d76 | 2008-04-17 13:19:16 +0900 | [diff] [blame] | 1025 | /* Free up the crypto pool */ |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1026 | tcp_put_md5sig_pool(); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1027 | out: |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1028 | return 0; |
| 1029 | clear_hash: |
| 1030 | tcp_put_md5sig_pool(); |
| 1031 | clear_hash_noput: |
| 1032 | memset(md5_hash, 0, 16); |
| 1033 | goto out; |
| 1034 | } |
| 1035 | |
| 1036 | int tcp_v4_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key, |
| 1037 | struct sock *sk, |
| 1038 | struct dst_entry *dst, |
| 1039 | struct request_sock *req, |
YOSHIFUJI Hideaki | 076fb72 | 2008-04-17 12:48:12 +0900 | [diff] [blame] | 1040 | struct tcphdr *th, |
YOSHIFUJI Hideaki | 9cb5734 | 2008-01-12 02:16:03 -0800 | [diff] [blame] | 1041 | unsigned int tcplen) |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1042 | { |
| 1043 | __be32 saddr, daddr; |
| 1044 | |
| 1045 | if (sk) { |
| 1046 | saddr = inet_sk(sk)->saddr; |
| 1047 | daddr = inet_sk(sk)->daddr; |
| 1048 | } else { |
| 1049 | struct rtable *rt = (struct rtable *)dst; |
| 1050 | BUG_ON(!rt); |
| 1051 | saddr = rt->rt_src; |
| 1052 | daddr = rt->rt_dst; |
| 1053 | } |
| 1054 | return tcp_v4_do_calc_md5_hash(md5_hash, key, |
| 1055 | saddr, daddr, |
YOSHIFUJI Hideaki | 076fb72 | 2008-04-17 12:48:12 +0900 | [diff] [blame] | 1056 | th, tcplen); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | EXPORT_SYMBOL(tcp_v4_calc_md5_hash); |
| 1060 | |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 1061 | static int tcp_v4_inbound_md5_hash(struct sock *sk, struct sk_buff *skb) |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1062 | { |
| 1063 | /* |
| 1064 | * This gets called for each TCP segment that arrives |
| 1065 | * so we want to be efficient. |
| 1066 | * We have 3 drop cases: |
| 1067 | * o No MD5 hash and one expected. |
| 1068 | * o MD5 hash and we're not expecting one. |
| 1069 | * o MD5 hash and its wrong. |
| 1070 | */ |
| 1071 | __u8 *hash_location = NULL; |
| 1072 | struct tcp_md5sig_key *hash_expected; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1073 | const struct iphdr *iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 1074 | struct tcphdr *th = tcp_hdr(skb); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1075 | int genhash; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1076 | unsigned char newhash[16]; |
| 1077 | |
| 1078 | hash_expected = tcp_v4_md5_do_lookup(sk, iph->saddr); |
YOSHIFUJI Hideaki | 7d5d552 | 2008-04-17 12:29:53 +0900 | [diff] [blame] | 1079 | hash_location = tcp_parse_md5sig_option(th); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1080 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1081 | /* We've parsed the options - do we have a hash? */ |
| 1082 | if (!hash_expected && !hash_location) |
| 1083 | return 0; |
| 1084 | |
| 1085 | if (hash_expected && !hash_location) { |
Leigh Brown | a9fc00c | 2006-12-17 17:13:10 -0800 | [diff] [blame] | 1086 | LIMIT_NETDEBUG(KERN_INFO "MD5 Hash expected but NOT found " |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1087 | "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n", |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 1088 | NIPQUAD(iph->saddr), ntohs(th->source), |
| 1089 | NIPQUAD(iph->daddr), ntohs(th->dest)); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1090 | return 1; |
| 1091 | } |
| 1092 | |
| 1093 | if (!hash_expected && hash_location) { |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 1094 | LIMIT_NETDEBUG(KERN_INFO "MD5 Hash NOT expected but found " |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1095 | "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n", |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 1096 | NIPQUAD(iph->saddr), ntohs(th->source), |
| 1097 | NIPQUAD(iph->daddr), ntohs(th->dest)); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1098 | return 1; |
| 1099 | } |
| 1100 | |
| 1101 | /* Okay, so this is hash_expected and hash_location - |
| 1102 | * so we need to calculate the checksum. |
| 1103 | */ |
| 1104 | genhash = tcp_v4_do_calc_md5_hash(newhash, |
| 1105 | hash_expected, |
| 1106 | iph->saddr, iph->daddr, |
YOSHIFUJI Hideaki | 076fb72 | 2008-04-17 12:48:12 +0900 | [diff] [blame] | 1107 | th, skb->len); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1108 | |
| 1109 | if (genhash || memcmp(hash_location, newhash, 16) != 0) { |
| 1110 | if (net_ratelimit()) { |
| 1111 | printk(KERN_INFO "MD5 Hash failed for " |
| 1112 | "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)%s\n", |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 1113 | NIPQUAD(iph->saddr), ntohs(th->source), |
| 1114 | NIPQUAD(iph->daddr), ntohs(th->dest), |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1115 | genhash ? " tcp_v4_calc_md5_hash failed" : ""); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1116 | } |
| 1117 | return 1; |
| 1118 | } |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | #endif |
| 1123 | |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 1124 | struct request_sock_ops tcp_request_sock_ops __read_mostly = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | .family = PF_INET, |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 1126 | .obj_size = sizeof(struct tcp_request_sock), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | .rtx_syn_ack = tcp_v4_send_synack, |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 1128 | .send_ack = tcp_v4_reqsk_send_ack, |
| 1129 | .destructor = tcp_v4_reqsk_destructor, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | .send_reset = tcp_v4_send_reset, |
| 1131 | }; |
| 1132 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1133 | #ifdef CONFIG_TCP_MD5SIG |
Andrew Morton | b6332e6 | 2006-11-30 19:16:28 -0800 | [diff] [blame] | 1134 | static struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = { |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1135 | .md5_lookup = tcp_v4_reqsk_md5_lookup, |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1136 | }; |
Andrew Morton | b6332e6 | 2006-11-30 19:16:28 -0800 | [diff] [blame] | 1137 | #endif |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1138 | |
Arnaldo Carvalho de Melo | 6d6ee43 | 2005-12-13 23:25:19 -0800 | [diff] [blame] | 1139 | static struct timewait_sock_ops tcp_timewait_sock_ops = { |
| 1140 | .twsk_obj_size = sizeof(struct tcp_timewait_sock), |
| 1141 | .twsk_unique = tcp_twsk_unique, |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1142 | .twsk_destructor= tcp_twsk_destructor, |
Arnaldo Carvalho de Melo | 6d6ee43 | 2005-12-13 23:25:19 -0800 | [diff] [blame] | 1143 | }; |
| 1144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb) |
| 1146 | { |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 1147 | struct inet_request_sock *ireq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | struct tcp_options_received tmp_opt; |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 1149 | struct request_sock *req; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1150 | __be32 saddr = ip_hdr(skb)->saddr; |
| 1151 | __be32 daddr = ip_hdr(skb)->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | __u32 isn = TCP_SKB_CB(skb)->when; |
| 1153 | struct dst_entry *dst = NULL; |
| 1154 | #ifdef CONFIG_SYN_COOKIES |
| 1155 | int want_cookie = 0; |
| 1156 | #else |
| 1157 | #define want_cookie 0 /* Argh, why doesn't gcc optimize this :( */ |
| 1158 | #endif |
| 1159 | |
| 1160 | /* Never answer to SYNs send to broadcast or multicast */ |
Eric Dumazet | ee6b967 | 2008-03-05 18:30:47 -0800 | [diff] [blame] | 1161 | if (skb->rtable->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | goto drop; |
| 1163 | |
| 1164 | /* TW buckets are converted to open requests without |
| 1165 | * limitations, they conserve resources and peer is |
| 1166 | * evidently real one. |
| 1167 | */ |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1168 | if (inet_csk_reqsk_queue_is_full(sk) && !isn) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | #ifdef CONFIG_SYN_COOKIES |
| 1170 | if (sysctl_tcp_syncookies) { |
| 1171 | want_cookie = 1; |
| 1172 | } else |
| 1173 | #endif |
| 1174 | goto drop; |
| 1175 | } |
| 1176 | |
| 1177 | /* Accept backlog is full. If we have already queued enough |
| 1178 | * of warm entries in syn queue, drop request. It is better than |
| 1179 | * clogging syn queue with openreqs with exponentially increasing |
| 1180 | * timeout. |
| 1181 | */ |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1182 | if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | goto drop; |
| 1184 | |
Arnaldo Carvalho de Melo | ce4a7d0 | 2008-06-10 12:39:35 -0700 | [diff] [blame] | 1185 | req = inet_reqsk_alloc(&tcp_request_sock_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | if (!req) |
| 1187 | goto drop; |
| 1188 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1189 | #ifdef CONFIG_TCP_MD5SIG |
| 1190 | tcp_rsk(req)->af_specific = &tcp_request_sock_ipv4_ops; |
| 1191 | #endif |
| 1192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | tcp_clear_options(&tmp_opt); |
| 1194 | tmp_opt.mss_clamp = 536; |
| 1195 | tmp_opt.user_mss = tcp_sk(sk)->rx_opt.user_mss; |
| 1196 | |
| 1197 | tcp_parse_options(skb, &tmp_opt, 0); |
| 1198 | |
Florian Westphal | 4dfc281 | 2008-04-10 03:12:40 -0700 | [diff] [blame] | 1199 | if (want_cookie && !tmp_opt.saw_tstamp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | tcp_clear_options(&tmp_opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
| 1202 | if (tmp_opt.saw_tstamp && !tmp_opt.rcv_tsval) { |
| 1203 | /* Some OSes (unknown ones, but I see them on web server, which |
| 1204 | * contains information interesting only for windows' |
| 1205 | * users) do not send their stamp in SYN. It is easy case. |
| 1206 | * We simply do not advertise TS support. |
| 1207 | */ |
| 1208 | tmp_opt.saw_tstamp = 0; |
| 1209 | tmp_opt.tstamp_ok = 0; |
| 1210 | } |
| 1211 | tmp_opt.tstamp_ok = tmp_opt.saw_tstamp; |
| 1212 | |
| 1213 | tcp_openreq_init(req, &tmp_opt, skb); |
| 1214 | |
Venkat Yekkirala | 4237c75 | 2006-07-24 23:32:50 -0700 | [diff] [blame] | 1215 | if (security_inet_conn_request(sk, skb, req)) |
| 1216 | goto drop_and_free; |
| 1217 | |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 1218 | ireq = inet_rsk(req); |
| 1219 | ireq->loc_addr = daddr; |
| 1220 | ireq->rmt_addr = saddr; |
| 1221 | ireq->opt = tcp_v4_save_options(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | if (!want_cookie) |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 1223 | TCP_ECN_create_request(req, tcp_hdr(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | |
| 1225 | if (want_cookie) { |
| 1226 | #ifdef CONFIG_SYN_COOKIES |
| 1227 | syn_flood_warning(skb); |
Florian Westphal | 4dfc281 | 2008-04-10 03:12:40 -0700 | [diff] [blame] | 1228 | req->cookie_ts = tmp_opt.tstamp_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | #endif |
| 1230 | isn = cookie_v4_init_sequence(sk, skb, &req->mss); |
| 1231 | } else if (!isn) { |
| 1232 | struct inet_peer *peer = NULL; |
| 1233 | |
| 1234 | /* VJ's idea. We save last timestamp seen |
| 1235 | * from the destination in peer table, when entering |
| 1236 | * state TIME-WAIT, and check against it before |
| 1237 | * accepting new connection request. |
| 1238 | * |
| 1239 | * If "isn" is not zero, this request hit alive |
| 1240 | * timewait bucket, so that all the necessary checks |
| 1241 | * are made in the function processing timewait state. |
| 1242 | */ |
| 1243 | if (tmp_opt.saw_tstamp && |
Arnaldo Carvalho de Melo | 295ff7e | 2005-08-09 20:44:40 -0700 | [diff] [blame] | 1244 | tcp_death_row.sysctl_tw_recycle && |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1245 | (dst = inet_csk_route_req(sk, req)) != NULL && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | (peer = rt_get_peer((struct rtable *)dst)) != NULL && |
| 1247 | peer->v4daddr == saddr) { |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 1248 | if (get_seconds() < peer->tcp_ts_stamp + TCP_PAWS_MSL && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | (s32)(peer->tcp_ts - req->ts_recent) > |
| 1250 | TCP_PAWS_WINDOW) { |
| 1251 | NET_INC_STATS_BH(LINUX_MIB_PAWSPASSIVEREJECTED); |
Denis V. Lunev | 7cd04fa | 2008-03-03 11:59:32 -0800 | [diff] [blame] | 1252 | goto drop_and_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | /* Kill the following clause, if you dislike this way. */ |
| 1256 | else if (!sysctl_tcp_syncookies && |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1257 | (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) < |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | (sysctl_max_syn_backlog >> 2)) && |
| 1259 | (!peer || !peer->tcp_ts_stamp) && |
| 1260 | (!dst || !dst_metric(dst, RTAX_RTT))) { |
| 1261 | /* Without syncookies last quarter of |
| 1262 | * backlog is filled with destinations, |
| 1263 | * proven to be alive. |
| 1264 | * It means that we continue to communicate |
| 1265 | * to destinations, already remembered |
| 1266 | * to the moment of synflood. |
| 1267 | */ |
Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 1268 | LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open " |
YOSHIFUJI Hideaki | a7d632b | 2008-04-14 04:09:00 -0700 | [diff] [blame] | 1269 | "request from " NIPQUAD_FMT "/%u\n", |
Patrick McHardy | 64ce207 | 2005-08-09 20:50:53 -0700 | [diff] [blame] | 1270 | NIPQUAD(saddr), |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 1271 | ntohs(tcp_hdr(skb)->source)); |
Denis V. Lunev | 7cd04fa | 2008-03-03 11:59:32 -0800 | [diff] [blame] | 1272 | goto drop_and_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Gerrit Renker | a94f723 | 2006-11-10 14:06:49 -0800 | [diff] [blame] | 1275 | isn = tcp_v4_init_sequence(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | } |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 1277 | tcp_rsk(req)->snt_isn = isn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | |
Denis V. Lunev | 7cd04fa | 2008-03-03 11:59:32 -0800 | [diff] [blame] | 1279 | if (__tcp_v4_send_synack(sk, req, dst) || want_cookie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | goto drop_and_free; |
| 1281 | |
Denis V. Lunev | 7cd04fa | 2008-03-03 11:59:32 -0800 | [diff] [blame] | 1282 | inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | return 0; |
| 1284 | |
Denis V. Lunev | 7cd04fa | 2008-03-03 11:59:32 -0800 | [diff] [blame] | 1285 | drop_and_release: |
| 1286 | dst_release(dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | drop_and_free: |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 1288 | reqsk_free(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | drop: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | |
| 1294 | /* |
| 1295 | * The three way handshake has completed - we got a valid synack - |
| 1296 | * now create the new socket. |
| 1297 | */ |
| 1298 | struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb, |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 1299 | struct request_sock *req, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | struct dst_entry *dst) |
| 1301 | { |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 1302 | struct inet_request_sock *ireq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | struct inet_sock *newinet; |
| 1304 | struct tcp_sock *newtp; |
| 1305 | struct sock *newsk; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1306 | #ifdef CONFIG_TCP_MD5SIG |
| 1307 | struct tcp_md5sig_key *key; |
| 1308 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
| 1310 | if (sk_acceptq_is_full(sk)) |
| 1311 | goto exit_overflow; |
| 1312 | |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1313 | if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | goto exit; |
| 1315 | |
| 1316 | newsk = tcp_create_openreq_child(sk, req, skb); |
| 1317 | if (!newsk) |
| 1318 | goto exit; |
| 1319 | |
Herbert Xu | bcd7611 | 2006-06-30 13:36:35 -0700 | [diff] [blame] | 1320 | newsk->sk_gso_type = SKB_GSO_TCPV4; |
Arnaldo Carvalho de Melo | 6cbb0df | 2005-08-09 19:49:02 -0700 | [diff] [blame] | 1321 | sk_setup_caps(newsk, dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | |
| 1323 | newtp = tcp_sk(newsk); |
| 1324 | newinet = inet_sk(newsk); |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 1325 | ireq = inet_rsk(req); |
| 1326 | newinet->daddr = ireq->rmt_addr; |
| 1327 | newinet->rcv_saddr = ireq->loc_addr; |
| 1328 | newinet->saddr = ireq->loc_addr; |
| 1329 | newinet->opt = ireq->opt; |
| 1330 | ireq->opt = NULL; |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1331 | newinet->mc_index = inet_iif(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1332 | newinet->mc_ttl = ip_hdr(skb)->ttl; |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 1333 | inet_csk(newsk)->icsk_ext_hdr_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | if (newinet->opt) |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 1335 | inet_csk(newsk)->icsk_ext_hdr_len = newinet->opt->optlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | newinet->id = newtp->write_seq ^ jiffies; |
| 1337 | |
John Heffner | 5d424d5 | 2006-03-20 17:53:41 -0800 | [diff] [blame] | 1338 | tcp_mtup_init(newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | tcp_sync_mss(newsk, dst_mtu(dst)); |
| 1340 | newtp->advmss = dst_metric(dst, RTAX_ADVMSS); |
| 1341 | tcp_initialize_rcv_mss(newsk); |
| 1342 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1343 | #ifdef CONFIG_TCP_MD5SIG |
| 1344 | /* Copy over the MD5 key from the original socket */ |
| 1345 | if ((key = tcp_v4_md5_do_lookup(sk, newinet->daddr)) != NULL) { |
| 1346 | /* |
| 1347 | * We're using one, so create a matching key |
| 1348 | * on the newsk structure. If we fail to get |
| 1349 | * memory, then we end up not copying the key |
| 1350 | * across. Shucks. |
| 1351 | */ |
Arnaldo Carvalho de Melo | f668593 | 2006-11-17 11:06:01 -0200 | [diff] [blame] | 1352 | char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC); |
| 1353 | if (newkey != NULL) |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1354 | tcp_v4_md5_do_add(newsk, inet_sk(sk)->daddr, |
| 1355 | newkey, key->keylen); |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1356 | } |
| 1357 | #endif |
| 1358 | |
Arnaldo Carvalho de Melo | ab1e0a1 | 2008-02-03 04:06:04 -0800 | [diff] [blame] | 1359 | __inet_hash_nolisten(newsk); |
| 1360 | __inet_inherit_port(sk, newsk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | |
| 1362 | return newsk; |
| 1363 | |
| 1364 | exit_overflow: |
| 1365 | NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS); |
| 1366 | exit: |
| 1367 | NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS); |
| 1368 | dst_release(dst); |
| 1369 | return NULL; |
| 1370 | } |
| 1371 | |
| 1372 | static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb) |
| 1373 | { |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 1374 | struct tcphdr *th = tcp_hdr(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1375 | const struct iphdr *iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | struct sock *nsk; |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 1377 | struct request_sock **prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | /* Find possible connection requests. */ |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1379 | struct request_sock *req = inet_csk_search_req(sk, &prev, th->source, |
| 1380 | iph->saddr, iph->daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | if (req) |
| 1382 | return tcp_check_req(sk, skb, req, prev); |
| 1383 | |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1384 | nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr, |
Pavel Emelyanov | c67499c | 2008-01-31 05:06:40 -0800 | [diff] [blame] | 1385 | th->source, iph->daddr, th->dest, inet_iif(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | |
| 1387 | if (nsk) { |
| 1388 | if (nsk->sk_state != TCP_TIME_WAIT) { |
| 1389 | bh_lock_sock(nsk); |
| 1390 | return nsk; |
| 1391 | } |
YOSHIFUJI Hideaki | 9469c7b | 2006-10-10 19:41:46 -0700 | [diff] [blame] | 1392 | inet_twsk_put(inet_twsk(nsk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | return NULL; |
| 1394 | } |
| 1395 | |
| 1396 | #ifdef CONFIG_SYN_COOKIES |
| 1397 | if (!th->rst && !th->syn && th->ack) |
| 1398 | sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt)); |
| 1399 | #endif |
| 1400 | return sk; |
| 1401 | } |
| 1402 | |
Al Viro | b51655b | 2006-11-14 21:40:42 -0800 | [diff] [blame] | 1403 | static __sum16 tcp_v4_checksum_init(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1405 | const struct iphdr *iph = ip_hdr(skb); |
| 1406 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 1407 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1408 | if (!tcp_v4_check(skb->len, iph->saddr, |
| 1409 | iph->daddr, skb->csum)) { |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 1410 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | return 0; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 1412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | } |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 1414 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1415 | skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 1416 | skb->len, IPPROTO_TCP, 0); |
| 1417 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | if (skb->len <= 76) { |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 1419 | return __skb_checksum_complete(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | } |
| 1421 | return 0; |
| 1422 | } |
| 1423 | |
| 1424 | |
| 1425 | /* The socket must have it's spinlock held when we get |
| 1426 | * here. |
| 1427 | * |
| 1428 | * We have a potential double-lock case here, so even when |
| 1429 | * doing backlog processing we use the BH locking scheme. |
| 1430 | * This is because we cannot sleep with the original spinlock |
| 1431 | * held. |
| 1432 | */ |
| 1433 | int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb) |
| 1434 | { |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1435 | struct sock *rsk; |
| 1436 | #ifdef CONFIG_TCP_MD5SIG |
| 1437 | /* |
| 1438 | * We really want to reject the packet as early as possible |
| 1439 | * if: |
| 1440 | * o We're expecting an MD5'd packet and this is no MD5 tcp option |
| 1441 | * o There is an MD5 option and we're not expecting one |
| 1442 | */ |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 1443 | if (tcp_v4_inbound_md5_hash(sk, skb)) |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1444 | goto discard; |
| 1445 | #endif |
| 1446 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */ |
| 1448 | TCP_CHECK_TIMER(sk); |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 1449 | if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) { |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1450 | rsk = sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | goto reset; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1452 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | TCP_CHECK_TIMER(sk); |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
Arnaldo Carvalho de Melo | ab6a5bb | 2007-03-18 17:43:48 -0700 | [diff] [blame] | 1457 | if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | goto csum_err; |
| 1459 | |
| 1460 | if (sk->sk_state == TCP_LISTEN) { |
| 1461 | struct sock *nsk = tcp_v4_hnd_req(sk, skb); |
| 1462 | if (!nsk) |
| 1463 | goto discard; |
| 1464 | |
| 1465 | if (nsk != sk) { |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1466 | if (tcp_child_process(sk, nsk, skb)) { |
| 1467 | rsk = nsk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | goto reset; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1469 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | return 0; |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | TCP_CHECK_TIMER(sk); |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 1475 | if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) { |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1476 | rsk = sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | goto reset; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | TCP_CHECK_TIMER(sk); |
| 1480 | return 0; |
| 1481 | |
| 1482 | reset: |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1483 | tcp_v4_send_reset(rsk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | discard: |
| 1485 | kfree_skb(skb); |
| 1486 | /* Be careful here. If this function gets more complicated and |
| 1487 | * gcc suffers from register pressure on the x86, sk (in %ebx) |
| 1488 | * might be destroyed here. This current version compiles correctly, |
| 1489 | * but you have been warned. |
| 1490 | */ |
| 1491 | return 0; |
| 1492 | |
| 1493 | csum_err: |
| 1494 | TCP_INC_STATS_BH(TCP_MIB_INERRS); |
| 1495 | goto discard; |
| 1496 | } |
| 1497 | |
| 1498 | /* |
| 1499 | * From tcp_input.c |
| 1500 | */ |
| 1501 | |
| 1502 | int tcp_v4_rcv(struct sk_buff *skb) |
| 1503 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1504 | const struct iphdr *iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | struct tcphdr *th; |
| 1506 | struct sock *sk; |
| 1507 | int ret; |
| 1508 | |
| 1509 | if (skb->pkt_type != PACKET_HOST) |
| 1510 | goto discard_it; |
| 1511 | |
| 1512 | /* Count it even if it's bad */ |
| 1513 | TCP_INC_STATS_BH(TCP_MIB_INSEGS); |
| 1514 | |
| 1515 | if (!pskb_may_pull(skb, sizeof(struct tcphdr))) |
| 1516 | goto discard_it; |
| 1517 | |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 1518 | th = tcp_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | |
| 1520 | if (th->doff < sizeof(struct tcphdr) / 4) |
| 1521 | goto bad_packet; |
| 1522 | if (!pskb_may_pull(skb, th->doff * 4)) |
| 1523 | goto discard_it; |
| 1524 | |
| 1525 | /* An explanation is required here, I think. |
| 1526 | * Packet length and doff are validated by header prediction, |
Stephen Hemminger | caa20d9a | 2005-11-10 17:13:47 -0800 | [diff] [blame] | 1527 | * provided case of th->doff==0 is eliminated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1528 | * So, we defer the checks. */ |
Herbert Xu | 6047637 | 2007-04-09 11:59:39 -0700 | [diff] [blame] | 1529 | if (!skb_csum_unnecessary(skb) && tcp_v4_checksum_init(skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | goto bad_packet; |
| 1531 | |
Arnaldo Carvalho de Melo | aa8223c | 2007-04-10 21:04:22 -0700 | [diff] [blame] | 1532 | th = tcp_hdr(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1533 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | TCP_SKB_CB(skb)->seq = ntohl(th->seq); |
| 1535 | TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin + |
| 1536 | skb->len - th->doff * 4); |
| 1537 | TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq); |
| 1538 | TCP_SKB_CB(skb)->when = 0; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1539 | TCP_SKB_CB(skb)->flags = iph->tos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | TCP_SKB_CB(skb)->sacked = 0; |
| 1541 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1542 | sk = __inet_lookup(dev_net(skb->dev), &tcp_hashinfo, iph->saddr, |
Pavel Emelyanov | c67499c | 2008-01-31 05:06:40 -0800 | [diff] [blame] | 1543 | th->source, iph->daddr, th->dest, inet_iif(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | if (!sk) |
| 1545 | goto no_tcp_socket; |
| 1546 | |
| 1547 | process: |
| 1548 | if (sk->sk_state == TCP_TIME_WAIT) |
| 1549 | goto do_time_wait; |
| 1550 | |
| 1551 | if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) |
| 1552 | goto discard_and_relse; |
Patrick McHardy | b59c270 | 2006-01-06 23:06:10 -0800 | [diff] [blame] | 1553 | nf_reset(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | |
Dmitry Mishin | fda9ef5 | 2006-08-31 15:28:39 -0700 | [diff] [blame] | 1555 | if (sk_filter(sk, skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | goto discard_and_relse; |
| 1557 | |
| 1558 | skb->dev = NULL; |
| 1559 | |
Ingo Molnar | c636618 | 2006-07-03 00:25:13 -0700 | [diff] [blame] | 1560 | bh_lock_sock_nested(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | ret = 0; |
| 1562 | if (!sock_owned_by_user(sk)) { |
Chris Leech | 1a2449a | 2006-05-23 18:05:53 -0700 | [diff] [blame] | 1563 | #ifdef CONFIG_NET_DMA |
| 1564 | struct tcp_sock *tp = tcp_sk(sk); |
| 1565 | if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list) |
| 1566 | tp->ucopy.dma_chan = get_softnet_dma(); |
| 1567 | if (tp->ucopy.dma_chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | ret = tcp_v4_do_rcv(sk, skb); |
Chris Leech | 1a2449a | 2006-05-23 18:05:53 -0700 | [diff] [blame] | 1569 | else |
| 1570 | #endif |
| 1571 | { |
| 1572 | if (!tcp_prequeue(sk, skb)) |
| 1573 | ret = tcp_v4_do_rcv(sk, skb); |
| 1574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | } else |
| 1576 | sk_add_backlog(sk, skb); |
| 1577 | bh_unlock_sock(sk); |
| 1578 | |
| 1579 | sock_put(sk); |
| 1580 | |
| 1581 | return ret; |
| 1582 | |
| 1583 | no_tcp_socket: |
| 1584 | if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) |
| 1585 | goto discard_it; |
| 1586 | |
| 1587 | if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) { |
| 1588 | bad_packet: |
| 1589 | TCP_INC_STATS_BH(TCP_MIB_INERRS); |
| 1590 | } else { |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1591 | tcp_v4_send_reset(NULL, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | discard_it: |
| 1595 | /* Discard frame. */ |
| 1596 | kfree_skb(skb); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1597 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | |
| 1599 | discard_and_relse: |
| 1600 | sock_put(sk); |
| 1601 | goto discard_it; |
| 1602 | |
| 1603 | do_time_wait: |
| 1604 | if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) { |
YOSHIFUJI Hideaki | 9469c7b | 2006-10-10 19:41:46 -0700 | [diff] [blame] | 1605 | inet_twsk_put(inet_twsk(sk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | goto discard_it; |
| 1607 | } |
| 1608 | |
| 1609 | if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) { |
| 1610 | TCP_INC_STATS_BH(TCP_MIB_INERRS); |
YOSHIFUJI Hideaki | 9469c7b | 2006-10-10 19:41:46 -0700 | [diff] [blame] | 1611 | inet_twsk_put(inet_twsk(sk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | goto discard_it; |
| 1613 | } |
YOSHIFUJI Hideaki | 9469c7b | 2006-10-10 19:41:46 -0700 | [diff] [blame] | 1614 | switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | case TCP_TW_SYN: { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1616 | struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev), |
Pavel Emelyanov | c67499c | 2008-01-31 05:06:40 -0800 | [diff] [blame] | 1617 | &tcp_hashinfo, |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1618 | iph->daddr, th->dest, |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1619 | inet_iif(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | if (sk2) { |
YOSHIFUJI Hideaki | 9469c7b | 2006-10-10 19:41:46 -0700 | [diff] [blame] | 1621 | inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row); |
| 1622 | inet_twsk_put(inet_twsk(sk)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | sk = sk2; |
| 1624 | goto process; |
| 1625 | } |
| 1626 | /* Fall through to ACK */ |
| 1627 | } |
| 1628 | case TCP_TW_ACK: |
| 1629 | tcp_v4_timewait_ack(sk, skb); |
| 1630 | break; |
| 1631 | case TCP_TW_RST: |
| 1632 | goto no_tcp_socket; |
| 1633 | case TCP_TW_SUCCESS:; |
| 1634 | } |
| 1635 | goto discard_it; |
| 1636 | } |
| 1637 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | /* VJ's idea. Save last timestamp seen from this destination |
| 1639 | * and hold it at least for normal timewait interval to use for duplicate |
| 1640 | * segment detection in subsequent connections, before they enter synchronized |
| 1641 | * state. |
| 1642 | */ |
| 1643 | |
| 1644 | int tcp_v4_remember_stamp(struct sock *sk) |
| 1645 | { |
| 1646 | struct inet_sock *inet = inet_sk(sk); |
| 1647 | struct tcp_sock *tp = tcp_sk(sk); |
| 1648 | struct rtable *rt = (struct rtable *)__sk_dst_get(sk); |
| 1649 | struct inet_peer *peer = NULL; |
| 1650 | int release_it = 0; |
| 1651 | |
| 1652 | if (!rt || rt->rt_dst != inet->daddr) { |
| 1653 | peer = inet_getpeer(inet->daddr, 1); |
| 1654 | release_it = 1; |
| 1655 | } else { |
| 1656 | if (!rt->peer) |
| 1657 | rt_bind_peer(rt, 1); |
| 1658 | peer = rt->peer; |
| 1659 | } |
| 1660 | |
| 1661 | if (peer) { |
| 1662 | if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 || |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 1663 | (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | peer->tcp_ts_stamp <= tp->rx_opt.ts_recent_stamp)) { |
| 1665 | peer->tcp_ts_stamp = tp->rx_opt.ts_recent_stamp; |
| 1666 | peer->tcp_ts = tp->rx_opt.ts_recent; |
| 1667 | } |
| 1668 | if (release_it) |
| 1669 | inet_putpeer(peer); |
| 1670 | return 1; |
| 1671 | } |
| 1672 | |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
Arnaldo Carvalho de Melo | 8feaf0c | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 1676 | int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1677 | { |
Arnaldo Carvalho de Melo | 8feaf0c | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 1678 | struct inet_peer *peer = inet_getpeer(tw->tw_daddr, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
| 1680 | if (peer) { |
Arnaldo Carvalho de Melo | 8feaf0c | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 1681 | const struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw); |
| 1682 | |
| 1683 | if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 || |
James Morris | 9d729f7 | 2007-03-04 16:12:44 -0800 | [diff] [blame] | 1684 | (peer->tcp_ts_stamp + TCP_PAWS_MSL < get_seconds() && |
Arnaldo Carvalho de Melo | 8feaf0c | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 1685 | peer->tcp_ts_stamp <= tcptw->tw_ts_recent_stamp)) { |
| 1686 | peer->tcp_ts_stamp = tcptw->tw_ts_recent_stamp; |
| 1687 | peer->tcp_ts = tcptw->tw_ts_recent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | } |
| 1689 | inet_putpeer(peer); |
| 1690 | return 1; |
| 1691 | } |
| 1692 | |
| 1693 | return 0; |
| 1694 | } |
| 1695 | |
Arnaldo Carvalho de Melo | 8292a17 | 2005-12-13 23:15:52 -0800 | [diff] [blame] | 1696 | struct inet_connection_sock_af_ops ipv4_specific = { |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1697 | .queue_xmit = ip_queue_xmit, |
| 1698 | .send_check = tcp_v4_send_check, |
| 1699 | .rebuild_header = inet_sk_rebuild_header, |
| 1700 | .conn_request = tcp_v4_conn_request, |
| 1701 | .syn_recv_sock = tcp_v4_syn_recv_sock, |
| 1702 | .remember_stamp = tcp_v4_remember_stamp, |
| 1703 | .net_header_len = sizeof(struct iphdr), |
| 1704 | .setsockopt = ip_setsockopt, |
| 1705 | .getsockopt = ip_getsockopt, |
| 1706 | .addr2sockaddr = inet_csk_addr2sockaddr, |
| 1707 | .sockaddr_len = sizeof(struct sockaddr_in), |
Arnaldo Carvalho de Melo | ab1e0a1 | 2008-02-03 04:06:04 -0800 | [diff] [blame] | 1708 | .bind_conflict = inet_csk_bind_conflict, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1709 | #ifdef CONFIG_COMPAT |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 1710 | .compat_setsockopt = compat_ip_setsockopt, |
| 1711 | .compat_getsockopt = compat_ip_getsockopt, |
Dmitry Mishin | 3fdadf7 | 2006-03-20 22:45:21 -0800 | [diff] [blame] | 1712 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | }; |
| 1714 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1715 | #ifdef CONFIG_TCP_MD5SIG |
Andrew Morton | b6332e6 | 2006-11-30 19:16:28 -0800 | [diff] [blame] | 1716 | static struct tcp_sock_af_ops tcp_sock_ipv4_specific = { |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1717 | .md5_lookup = tcp_v4_md5_lookup, |
| 1718 | .calc_md5_hash = tcp_v4_calc_md5_hash, |
| 1719 | .md5_add = tcp_v4_md5_add_func, |
| 1720 | .md5_parse = tcp_v4_parse_md5_keys, |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1721 | }; |
Andrew Morton | b6332e6 | 2006-11-30 19:16:28 -0800 | [diff] [blame] | 1722 | #endif |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | /* NOTE: A lot of things set to zero explicitly by call to |
| 1725 | * sk_alloc() so need not be done here. |
| 1726 | */ |
| 1727 | static int tcp_v4_init_sock(struct sock *sk) |
| 1728 | { |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 1729 | struct inet_connection_sock *icsk = inet_csk(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | struct tcp_sock *tp = tcp_sk(sk); |
| 1731 | |
| 1732 | skb_queue_head_init(&tp->out_of_order_queue); |
| 1733 | tcp_init_xmit_timers(sk); |
| 1734 | tcp_prequeue_init(tp); |
| 1735 | |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 1736 | icsk->icsk_rto = TCP_TIMEOUT_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | tp->mdev = TCP_TIMEOUT_INIT; |
| 1738 | |
| 1739 | /* So many TCP implementations out there (incorrectly) count the |
| 1740 | * initial SYN frame in their delayed-ACK and congestion control |
| 1741 | * algorithms that we must have the following bandaid to talk |
| 1742 | * efficiently to them. -DaveM |
| 1743 | */ |
| 1744 | tp->snd_cwnd = 2; |
| 1745 | |
| 1746 | /* See draft-stevens-tcpca-spec-01 for discussion of the |
| 1747 | * initialization of these values. |
| 1748 | */ |
| 1749 | tp->snd_ssthresh = 0x7fffffff; /* Infinity */ |
| 1750 | tp->snd_cwnd_clamp = ~0; |
David S. Miller | c1b4a7e | 2005-07-05 15:24:38 -0700 | [diff] [blame] | 1751 | tp->mss_cache = 536; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | |
| 1753 | tp->reordering = sysctl_tcp_reordering; |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 1754 | icsk->icsk_ca_ops = &tcp_init_congestion_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | |
| 1756 | sk->sk_state = TCP_CLOSE; |
| 1757 | |
| 1758 | sk->sk_write_space = sk_stream_write_space; |
| 1759 | sock_set_flag(sk, SOCK_USE_WRITE_QUEUE); |
| 1760 | |
Arnaldo Carvalho de Melo | 8292a17 | 2005-12-13 23:15:52 -0800 | [diff] [blame] | 1761 | icsk->icsk_af_ops = &ipv4_specific; |
Arnaldo Carvalho de Melo | d83d846 | 2005-12-13 23:26:10 -0800 | [diff] [blame] | 1762 | icsk->icsk_sync_mss = tcp_sync_mss; |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1763 | #ifdef CONFIG_TCP_MD5SIG |
| 1764 | tp->af_specific = &tcp_sock_ipv4_specific; |
| 1765 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
| 1767 | sk->sk_sndbuf = sysctl_tcp_wmem[1]; |
| 1768 | sk->sk_rcvbuf = sysctl_tcp_rmem[1]; |
| 1769 | |
| 1770 | atomic_inc(&tcp_sockets_allocated); |
| 1771 | |
| 1772 | return 0; |
| 1773 | } |
| 1774 | |
Brian Haley | 7d06b2e | 2008-06-14 17:04:49 -0700 | [diff] [blame] | 1775 | void tcp_v4_destroy_sock(struct sock *sk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | { |
| 1777 | struct tcp_sock *tp = tcp_sk(sk); |
| 1778 | |
| 1779 | tcp_clear_xmit_timers(sk); |
| 1780 | |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 1781 | tcp_cleanup_congestion_control(sk); |
Stephen Hemminger | 317a76f | 2005-06-23 12:19:55 -0700 | [diff] [blame] | 1782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | /* Cleanup up the write buffer. */ |
David S. Miller | fe067e8 | 2007-03-07 12:12:44 -0800 | [diff] [blame] | 1784 | tcp_write_queue_purge(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | |
| 1786 | /* Cleans up our, hopefully empty, out_of_order_queue. */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1787 | __skb_queue_purge(&tp->out_of_order_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | |
YOSHIFUJI Hideaki | cfb6eeb | 2006-11-14 19:07:45 -0800 | [diff] [blame] | 1789 | #ifdef CONFIG_TCP_MD5SIG |
| 1790 | /* Clean up the MD5 key list, if any */ |
| 1791 | if (tp->md5sig_info) { |
| 1792 | tcp_v4_clear_md5_list(sk); |
| 1793 | kfree(tp->md5sig_info); |
| 1794 | tp->md5sig_info = NULL; |
| 1795 | } |
| 1796 | #endif |
| 1797 | |
Chris Leech | 1a2449a | 2006-05-23 18:05:53 -0700 | [diff] [blame] | 1798 | #ifdef CONFIG_NET_DMA |
| 1799 | /* Cleans up our sk_async_wait_queue */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1800 | __skb_queue_purge(&sk->sk_async_wait_queue); |
Chris Leech | 1a2449a | 2006-05-23 18:05:53 -0700 | [diff] [blame] | 1801 | #endif |
| 1802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | /* Clean prequeue, it must be empty really */ |
| 1804 | __skb_queue_purge(&tp->ucopy.prequeue); |
| 1805 | |
| 1806 | /* Clean up a referenced TCP bind bucket. */ |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1807 | if (inet_csk(sk)->icsk_bind_hash) |
Arnaldo Carvalho de Melo | ab1e0a1 | 2008-02-03 04:06:04 -0800 | [diff] [blame] | 1808 | inet_put_port(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | |
| 1810 | /* |
| 1811 | * If sendmsg cached page exists, toss it. |
| 1812 | */ |
| 1813 | if (sk->sk_sndmsg_page) { |
| 1814 | __free_page(sk->sk_sndmsg_page); |
| 1815 | sk->sk_sndmsg_page = NULL; |
| 1816 | } |
| 1817 | |
| 1818 | atomic_dec(&tcp_sockets_allocated); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | EXPORT_SYMBOL(tcp_v4_destroy_sock); |
| 1822 | |
| 1823 | #ifdef CONFIG_PROC_FS |
| 1824 | /* Proc filesystem TCP sock list dumping. */ |
| 1825 | |
Arnaldo Carvalho de Melo | 8feaf0c | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 1826 | static inline struct inet_timewait_sock *tw_head(struct hlist_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | { |
| 1828 | return hlist_empty(head) ? NULL : |
Arnaldo Carvalho de Melo | 8feaf0c | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 1829 | list_entry(head->first, struct inet_timewait_sock, tw_node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | } |
| 1831 | |
Arnaldo Carvalho de Melo | 8feaf0c | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 1832 | static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | { |
| 1834 | return tw->tw_node.next ? |
| 1835 | hlist_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL; |
| 1836 | } |
| 1837 | |
| 1838 | static void *listening_get_next(struct seq_file *seq, void *cur) |
| 1839 | { |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1840 | struct inet_connection_sock *icsk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | struct hlist_node *node; |
| 1842 | struct sock *sk = cur; |
| 1843 | struct tcp_iter_state* st = seq->private; |
Denis V. Lunev | a4146b1 | 2008-04-13 22:11:14 -0700 | [diff] [blame] | 1844 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | |
| 1846 | if (!sk) { |
| 1847 | st->bucket = 0; |
Arnaldo Carvalho de Melo | 6e04e02 | 2005-08-09 20:07:35 -0700 | [diff] [blame] | 1848 | sk = sk_head(&tcp_hashinfo.listening_hash[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | goto get_sk; |
| 1850 | } |
| 1851 | |
| 1852 | ++st->num; |
| 1853 | |
| 1854 | if (st->state == TCP_SEQ_STATE_OPENREQ) { |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 1855 | struct request_sock *req = cur; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 1857 | icsk = inet_csk(st->syn_wait_sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | req = req->dl_next; |
| 1859 | while (1) { |
| 1860 | while (req) { |
Daniel Lezcano | f40c817 | 2008-03-21 04:13:54 -0700 | [diff] [blame] | 1861 | if (req->rsk_ops->family == st->family && |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1862 | net_eq(sock_net(req->sk), net)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | cur = req; |
| 1864 | goto out; |
| 1865 | } |
| 1866 | req = req->dl_next; |
| 1867 | } |
Eric Dumazet | 72a3eff | 2006-11-16 02:30:37 -0800 | [diff] [blame] | 1868 | if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | break; |
| 1870 | get_req: |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1871 | req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | } |
| 1873 | sk = sk_next(st->syn_wait_sk); |
| 1874 | st->state = TCP_SEQ_STATE_LISTENING; |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1875 | read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | } else { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1877 | icsk = inet_csk(sk); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1878 | read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
| 1879 | if (reqsk_queue_len(&icsk->icsk_accept_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | goto start_req; |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1881 | read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | sk = sk_next(sk); |
| 1883 | } |
| 1884 | get_sk: |
| 1885 | sk_for_each_from(sk, node) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1886 | if (sk->sk_family == st->family && net_eq(sock_net(sk), net)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | cur = sk; |
| 1888 | goto out; |
| 1889 | } |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1890 | icsk = inet_csk(sk); |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1891 | read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
| 1892 | if (reqsk_queue_len(&icsk->icsk_accept_queue)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | start_req: |
| 1894 | st->uid = sock_i_uid(sk); |
| 1895 | st->syn_wait_sk = sk; |
| 1896 | st->state = TCP_SEQ_STATE_OPENREQ; |
| 1897 | st->sbucket = 0; |
| 1898 | goto get_req; |
| 1899 | } |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 1900 | read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | } |
Arnaldo Carvalho de Melo | 0f7ff92 | 2005-08-09 19:59:44 -0700 | [diff] [blame] | 1902 | if (++st->bucket < INET_LHTABLE_SIZE) { |
Arnaldo Carvalho de Melo | 6e04e02 | 2005-08-09 20:07:35 -0700 | [diff] [blame] | 1903 | sk = sk_head(&tcp_hashinfo.listening_hash[st->bucket]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | goto get_sk; |
| 1905 | } |
| 1906 | cur = NULL; |
| 1907 | out: |
| 1908 | return cur; |
| 1909 | } |
| 1910 | |
| 1911 | static void *listening_get_idx(struct seq_file *seq, loff_t *pos) |
| 1912 | { |
| 1913 | void *rc = listening_get_next(seq, NULL); |
| 1914 | |
| 1915 | while (rc && *pos) { |
| 1916 | rc = listening_get_next(seq, rc); |
| 1917 | --*pos; |
| 1918 | } |
| 1919 | return rc; |
| 1920 | } |
| 1921 | |
| 1922 | static void *established_get_first(struct seq_file *seq) |
| 1923 | { |
| 1924 | struct tcp_iter_state* st = seq->private; |
Denis V. Lunev | a4146b1 | 2008-04-13 22:11:14 -0700 | [diff] [blame] | 1925 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | void *rc = NULL; |
| 1927 | |
Arnaldo Carvalho de Melo | 6e04e02 | 2005-08-09 20:07:35 -0700 | [diff] [blame] | 1928 | for (st->bucket = 0; st->bucket < tcp_hashinfo.ehash_size; ++st->bucket) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | struct sock *sk; |
| 1930 | struct hlist_node *node; |
Arnaldo Carvalho de Melo | 8feaf0c | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 1931 | struct inet_timewait_sock *tw; |
Eric Dumazet | 230140c | 2007-11-07 02:40:20 -0800 | [diff] [blame] | 1932 | rwlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | |
Eric Dumazet | 230140c | 2007-11-07 02:40:20 -0800 | [diff] [blame] | 1934 | read_lock_bh(lock); |
Arnaldo Carvalho de Melo | 6e04e02 | 2005-08-09 20:07:35 -0700 | [diff] [blame] | 1935 | sk_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) { |
Daniel Lezcano | f40c817 | 2008-03-21 04:13:54 -0700 | [diff] [blame] | 1936 | if (sk->sk_family != st->family || |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1937 | !net_eq(sock_net(sk), net)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1938 | continue; |
| 1939 | } |
| 1940 | rc = sk; |
| 1941 | goto out; |
| 1942 | } |
| 1943 | st->state = TCP_SEQ_STATE_TIME_WAIT; |
Arnaldo Carvalho de Melo | 8feaf0c | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 1944 | inet_twsk_for_each(tw, node, |
Eric Dumazet | dbca9b275 | 2007-02-08 14:16:46 -0800 | [diff] [blame] | 1945 | &tcp_hashinfo.ehash[st->bucket].twchain) { |
Pavel Emelyanov | 28518fc | 2008-03-21 15:52:00 -0700 | [diff] [blame] | 1946 | if (tw->tw_family != st->family || |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1947 | !net_eq(twsk_net(tw), net)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | continue; |
| 1949 | } |
| 1950 | rc = tw; |
| 1951 | goto out; |
| 1952 | } |
Eric Dumazet | 230140c | 2007-11-07 02:40:20 -0800 | [diff] [blame] | 1953 | read_unlock_bh(lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | st->state = TCP_SEQ_STATE_ESTABLISHED; |
| 1955 | } |
| 1956 | out: |
| 1957 | return rc; |
| 1958 | } |
| 1959 | |
| 1960 | static void *established_get_next(struct seq_file *seq, void *cur) |
| 1961 | { |
| 1962 | struct sock *sk = cur; |
Arnaldo Carvalho de Melo | 8feaf0c | 2005-08-09 20:09:30 -0700 | [diff] [blame] | 1963 | struct inet_timewait_sock *tw; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | struct hlist_node *node; |
| 1965 | struct tcp_iter_state* st = seq->private; |
Denis V. Lunev | a4146b1 | 2008-04-13 22:11:14 -0700 | [diff] [blame] | 1966 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | |
| 1968 | ++st->num; |
| 1969 | |
| 1970 | if (st->state == TCP_SEQ_STATE_TIME_WAIT) { |
| 1971 | tw = cur; |
| 1972 | tw = tw_next(tw); |
| 1973 | get_tw: |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1974 | while (tw && (tw->tw_family != st->family || !net_eq(twsk_net(tw), net))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | tw = tw_next(tw); |
| 1976 | } |
| 1977 | if (tw) { |
| 1978 | cur = tw; |
| 1979 | goto out; |
| 1980 | } |
Eric Dumazet | 230140c | 2007-11-07 02:40:20 -0800 | [diff] [blame] | 1981 | read_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | st->state = TCP_SEQ_STATE_ESTABLISHED; |
| 1983 | |
Arnaldo Carvalho de Melo | 6e04e02 | 2005-08-09 20:07:35 -0700 | [diff] [blame] | 1984 | if (++st->bucket < tcp_hashinfo.ehash_size) { |
Eric Dumazet | 230140c | 2007-11-07 02:40:20 -0800 | [diff] [blame] | 1985 | read_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket)); |
Arnaldo Carvalho de Melo | 6e04e02 | 2005-08-09 20:07:35 -0700 | [diff] [blame] | 1986 | sk = sk_head(&tcp_hashinfo.ehash[st->bucket].chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | } else { |
| 1988 | cur = NULL; |
| 1989 | goto out; |
| 1990 | } |
| 1991 | } else |
| 1992 | sk = sk_next(sk); |
| 1993 | |
| 1994 | sk_for_each_from(sk, node) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1995 | if (sk->sk_family == st->family && net_eq(sock_net(sk), net)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | goto found; |
| 1997 | } |
| 1998 | |
| 1999 | st->state = TCP_SEQ_STATE_TIME_WAIT; |
Eric Dumazet | dbca9b275 | 2007-02-08 14:16:46 -0800 | [diff] [blame] | 2000 | tw = tw_head(&tcp_hashinfo.ehash[st->bucket].twchain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | goto get_tw; |
| 2002 | found: |
| 2003 | cur = sk; |
| 2004 | out: |
| 2005 | return cur; |
| 2006 | } |
| 2007 | |
| 2008 | static void *established_get_idx(struct seq_file *seq, loff_t pos) |
| 2009 | { |
| 2010 | void *rc = established_get_first(seq); |
| 2011 | |
| 2012 | while (rc && pos) { |
| 2013 | rc = established_get_next(seq, rc); |
| 2014 | --pos; |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 2015 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | return rc; |
| 2017 | } |
| 2018 | |
| 2019 | static void *tcp_get_idx(struct seq_file *seq, loff_t pos) |
| 2020 | { |
| 2021 | void *rc; |
| 2022 | struct tcp_iter_state* st = seq->private; |
| 2023 | |
Arnaldo Carvalho de Melo | f3f05f7 | 2005-08-09 20:08:09 -0700 | [diff] [blame] | 2024 | inet_listen_lock(&tcp_hashinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | st->state = TCP_SEQ_STATE_LISTENING; |
| 2026 | rc = listening_get_idx(seq, &pos); |
| 2027 | |
| 2028 | if (!rc) { |
Arnaldo Carvalho de Melo | f3f05f7 | 2005-08-09 20:08:09 -0700 | [diff] [blame] | 2029 | inet_listen_unlock(&tcp_hashinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | st->state = TCP_SEQ_STATE_ESTABLISHED; |
| 2031 | rc = established_get_idx(seq, pos); |
| 2032 | } |
| 2033 | |
| 2034 | return rc; |
| 2035 | } |
| 2036 | |
| 2037 | static void *tcp_seq_start(struct seq_file *seq, loff_t *pos) |
| 2038 | { |
| 2039 | struct tcp_iter_state* st = seq->private; |
| 2040 | st->state = TCP_SEQ_STATE_LISTENING; |
| 2041 | st->num = 0; |
| 2042 | return *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 2043 | } |
| 2044 | |
| 2045 | static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2046 | { |
| 2047 | void *rc = NULL; |
| 2048 | struct tcp_iter_state* st; |
| 2049 | |
| 2050 | if (v == SEQ_START_TOKEN) { |
| 2051 | rc = tcp_get_idx(seq, 0); |
| 2052 | goto out; |
| 2053 | } |
| 2054 | st = seq->private; |
| 2055 | |
| 2056 | switch (st->state) { |
| 2057 | case TCP_SEQ_STATE_OPENREQ: |
| 2058 | case TCP_SEQ_STATE_LISTENING: |
| 2059 | rc = listening_get_next(seq, v); |
| 2060 | if (!rc) { |
Arnaldo Carvalho de Melo | f3f05f7 | 2005-08-09 20:08:09 -0700 | [diff] [blame] | 2061 | inet_listen_unlock(&tcp_hashinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | st->state = TCP_SEQ_STATE_ESTABLISHED; |
| 2063 | rc = established_get_first(seq); |
| 2064 | } |
| 2065 | break; |
| 2066 | case TCP_SEQ_STATE_ESTABLISHED: |
| 2067 | case TCP_SEQ_STATE_TIME_WAIT: |
| 2068 | rc = established_get_next(seq, v); |
| 2069 | break; |
| 2070 | } |
| 2071 | out: |
| 2072 | ++*pos; |
| 2073 | return rc; |
| 2074 | } |
| 2075 | |
| 2076 | static void tcp_seq_stop(struct seq_file *seq, void *v) |
| 2077 | { |
| 2078 | struct tcp_iter_state* st = seq->private; |
| 2079 | |
| 2080 | switch (st->state) { |
| 2081 | case TCP_SEQ_STATE_OPENREQ: |
| 2082 | if (v) { |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 2083 | struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk); |
| 2084 | read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | } |
| 2086 | case TCP_SEQ_STATE_LISTENING: |
| 2087 | if (v != SEQ_START_TOKEN) |
Arnaldo Carvalho de Melo | f3f05f7 | 2005-08-09 20:08:09 -0700 | [diff] [blame] | 2088 | inet_listen_unlock(&tcp_hashinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | break; |
| 2090 | case TCP_SEQ_STATE_TIME_WAIT: |
| 2091 | case TCP_SEQ_STATE_ESTABLISHED: |
| 2092 | if (v) |
Eric Dumazet | 230140c | 2007-11-07 02:40:20 -0800 | [diff] [blame] | 2093 | read_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | break; |
| 2095 | } |
| 2096 | } |
| 2097 | |
| 2098 | static int tcp_seq_open(struct inode *inode, struct file *file) |
| 2099 | { |
| 2100 | struct tcp_seq_afinfo *afinfo = PDE(inode)->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2101 | struct tcp_iter_state *s; |
Denis V. Lunev | 52d6f3f | 2008-04-13 22:12:41 -0700 | [diff] [blame] | 2102 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | |
Denis V. Lunev | 52d6f3f | 2008-04-13 22:12:41 -0700 | [diff] [blame] | 2104 | err = seq_open_net(inode, file, &afinfo->seq_ops, |
| 2105 | sizeof(struct tcp_iter_state)); |
| 2106 | if (err < 0) |
| 2107 | return err; |
Daniel Lezcano | f40c817 | 2008-03-21 04:13:54 -0700 | [diff] [blame] | 2108 | |
Denis V. Lunev | 52d6f3f | 2008-04-13 22:12:41 -0700 | [diff] [blame] | 2109 | s = ((struct seq_file *)file->private_data)->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | s->family = afinfo->family; |
Daniel Lezcano | f40c817 | 2008-03-21 04:13:54 -0700 | [diff] [blame] | 2111 | return 0; |
| 2112 | } |
| 2113 | |
Daniel Lezcano | 6f8b13b | 2008-03-21 04:14:45 -0700 | [diff] [blame] | 2114 | int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | { |
| 2116 | int rc = 0; |
| 2117 | struct proc_dir_entry *p; |
| 2118 | |
Denis V. Lunev | 68fcadd | 2008-04-13 22:13:30 -0700 | [diff] [blame] | 2119 | afinfo->seq_fops.open = tcp_seq_open; |
| 2120 | afinfo->seq_fops.read = seq_read; |
| 2121 | afinfo->seq_fops.llseek = seq_lseek; |
| 2122 | afinfo->seq_fops.release = seq_release_net; |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 2123 | |
Denis V. Lunev | 9427c4b | 2008-04-13 22:12:13 -0700 | [diff] [blame] | 2124 | afinfo->seq_ops.start = tcp_seq_start; |
| 2125 | afinfo->seq_ops.next = tcp_seq_next; |
| 2126 | afinfo->seq_ops.stop = tcp_seq_stop; |
| 2127 | |
Denis V. Lunev | 84841c3 | 2008-05-02 04:10:08 -0700 | [diff] [blame] | 2128 | p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net, |
| 2129 | &afinfo->seq_fops, afinfo); |
| 2130 | if (!p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2131 | rc = -ENOMEM; |
| 2132 | return rc; |
| 2133 | } |
| 2134 | |
Daniel Lezcano | 6f8b13b | 2008-03-21 04:14:45 -0700 | [diff] [blame] | 2135 | void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | { |
Daniel Lezcano | 6f8b13b | 2008-03-21 04:14:45 -0700 | [diff] [blame] | 2137 | proc_net_remove(net, afinfo->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | } |
| 2139 | |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 2140 | static void get_openreq4(struct sock *sk, struct request_sock *req, |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2141 | struct seq_file *f, int i, int uid, int *len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | { |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 2143 | const struct inet_request_sock *ireq = inet_rsk(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | int ttd = req->expires - jiffies; |
| 2145 | |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2146 | seq_printf(f, "%4d: %08X:%04X %08X:%04X" |
| 2147 | " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p%n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | i, |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 2149 | ireq->loc_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | ntohs(inet_sk(sk)->sport), |
Arnaldo Carvalho de Melo | 2e6599c | 2005-06-18 22:46:52 -0700 | [diff] [blame] | 2151 | ireq->rmt_addr, |
| 2152 | ntohs(ireq->rmt_port), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | TCP_SYN_RECV, |
| 2154 | 0, 0, /* could print option size, but that is af dependent. */ |
| 2155 | 1, /* timers active (only the expire timer) */ |
| 2156 | jiffies_to_clock_t(ttd), |
| 2157 | req->retrans, |
| 2158 | uid, |
| 2159 | 0, /* non standard timer */ |
| 2160 | 0, /* open_requests have no inode */ |
| 2161 | atomic_read(&sk->sk_refcnt), |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2162 | req, |
| 2163 | len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2166 | static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | { |
| 2168 | int timer_active; |
| 2169 | unsigned long timer_expires; |
Ilpo Järvinen | cf4c6bf | 2007-02-22 01:13:58 -0800 | [diff] [blame] | 2170 | struct tcp_sock *tp = tcp_sk(sk); |
| 2171 | const struct inet_connection_sock *icsk = inet_csk(sk); |
| 2172 | struct inet_sock *inet = inet_sk(sk); |
Al Viro | 714e85b | 2006-11-14 20:51:49 -0800 | [diff] [blame] | 2173 | __be32 dest = inet->daddr; |
| 2174 | __be32 src = inet->rcv_saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | __u16 destp = ntohs(inet->dport); |
| 2176 | __u16 srcp = ntohs(inet->sport); |
| 2177 | |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 2178 | if (icsk->icsk_pending == ICSK_TIME_RETRANS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2179 | timer_active = 1; |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 2180 | timer_expires = icsk->icsk_timeout; |
| 2181 | } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | timer_active = 4; |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 2183 | timer_expires = icsk->icsk_timeout; |
Ilpo Järvinen | cf4c6bf | 2007-02-22 01:13:58 -0800 | [diff] [blame] | 2184 | } else if (timer_pending(&sk->sk_timer)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | timer_active = 2; |
Ilpo Järvinen | cf4c6bf | 2007-02-22 01:13:58 -0800 | [diff] [blame] | 2186 | timer_expires = sk->sk_timer.expires; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | } else { |
| 2188 | timer_active = 0; |
| 2189 | timer_expires = jiffies; |
| 2190 | } |
| 2191 | |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2192 | seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX " |
Stephen Hemminger | 7be8735 | 2008-06-27 20:00:19 -0700 | [diff] [blame] | 2193 | "%08X %5d %8d %lu %d %p %lu %lu %u %u %d%n", |
Ilpo Järvinen | cf4c6bf | 2007-02-22 01:13:58 -0800 | [diff] [blame] | 2194 | i, src, srcp, dest, destp, sk->sk_state, |
Sridhar Samudrala | 47da8ee | 2006-06-27 13:29:00 -0700 | [diff] [blame] | 2195 | tp->write_seq - tp->snd_una, |
Ilpo Järvinen | cf4c6bf | 2007-02-22 01:13:58 -0800 | [diff] [blame] | 2196 | sk->sk_state == TCP_LISTEN ? sk->sk_ack_backlog : |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 2197 | (tp->rcv_nxt - tp->copied_seq), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | timer_active, |
| 2199 | jiffies_to_clock_t(timer_expires - jiffies), |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 2200 | icsk->icsk_retransmits, |
Ilpo Järvinen | cf4c6bf | 2007-02-22 01:13:58 -0800 | [diff] [blame] | 2201 | sock_i_uid(sk), |
Arnaldo Carvalho de Melo | 6687e98 | 2005-08-10 04:03:31 -0300 | [diff] [blame] | 2202 | icsk->icsk_probes_out, |
Ilpo Järvinen | cf4c6bf | 2007-02-22 01:13:58 -0800 | [diff] [blame] | 2203 | sock_i_ino(sk), |
| 2204 | atomic_read(&sk->sk_refcnt), sk, |
Stephen Hemminger | 7be8735 | 2008-06-27 20:00:19 -0700 | [diff] [blame] | 2205 | jiffies_to_clock_t(icsk->icsk_rto), |
| 2206 | jiffies_to_clock_t(icsk->icsk_ack.ato), |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 2207 | (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | tp->snd_cwnd, |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2209 | tp->snd_ssthresh >= 0xFFFF ? -1 : tp->snd_ssthresh, |
| 2210 | len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | } |
| 2212 | |
Arnaldo Carvalho de Melo | 7174259 | 2006-11-17 10:57:30 -0200 | [diff] [blame] | 2213 | static void get_timewait4_sock(struct inet_timewait_sock *tw, |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2214 | struct seq_file *f, int i, int *len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | { |
Al Viro | 23f33c2 | 2006-09-27 18:43:50 -0700 | [diff] [blame] | 2216 | __be32 dest, src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | __u16 destp, srcp; |
| 2218 | int ttd = tw->tw_ttd - jiffies; |
| 2219 | |
| 2220 | if (ttd < 0) |
| 2221 | ttd = 0; |
| 2222 | |
| 2223 | dest = tw->tw_daddr; |
| 2224 | src = tw->tw_rcv_saddr; |
| 2225 | destp = ntohs(tw->tw_dport); |
| 2226 | srcp = ntohs(tw->tw_sport); |
| 2227 | |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2228 | seq_printf(f, "%4d: %08X:%04X %08X:%04X" |
| 2229 | " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p%n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | i, src, srcp, dest, destp, tw->tw_substate, 0, 0, |
| 2231 | 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0, |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2232 | atomic_read(&tw->tw_refcnt), tw, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | #define TMPSZ 150 |
| 2236 | |
| 2237 | static int tcp4_seq_show(struct seq_file *seq, void *v) |
| 2238 | { |
| 2239 | struct tcp_iter_state* st; |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2240 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | |
| 2242 | if (v == SEQ_START_TOKEN) { |
| 2243 | seq_printf(seq, "%-*s\n", TMPSZ - 1, |
| 2244 | " sl local_address rem_address st tx_queue " |
| 2245 | "rx_queue tr tm->when retrnsmt uid timeout " |
| 2246 | "inode"); |
| 2247 | goto out; |
| 2248 | } |
| 2249 | st = seq->private; |
| 2250 | |
| 2251 | switch (st->state) { |
| 2252 | case TCP_SEQ_STATE_LISTENING: |
| 2253 | case TCP_SEQ_STATE_ESTABLISHED: |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2254 | get_tcp4_sock(v, seq, st->num, &len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | break; |
| 2256 | case TCP_SEQ_STATE_OPENREQ: |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2257 | get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | break; |
| 2259 | case TCP_SEQ_STATE_TIME_WAIT: |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2260 | get_timewait4_sock(v, seq, st->num, &len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2261 | break; |
| 2262 | } |
Pavel Emelyanov | 5e659e4 | 2008-04-24 01:02:16 -0700 | [diff] [blame] | 2263 | seq_printf(seq, "%*s\n", TMPSZ - 1 - len, ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | out: |
| 2265 | return 0; |
| 2266 | } |
| 2267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | static struct tcp_seq_afinfo tcp4_seq_afinfo = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | .name = "tcp", |
| 2270 | .family = AF_INET, |
Denis V. Lunev | 5f4472c | 2008-04-13 22:13:53 -0700 | [diff] [blame] | 2271 | .seq_fops = { |
| 2272 | .owner = THIS_MODULE, |
| 2273 | }, |
Denis V. Lunev | 9427c4b | 2008-04-13 22:12:13 -0700 | [diff] [blame] | 2274 | .seq_ops = { |
| 2275 | .show = tcp4_seq_show, |
| 2276 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | }; |
| 2278 | |
Pavel Emelyanov | 757764f | 2008-03-24 14:56:02 -0700 | [diff] [blame] | 2279 | static int tcp4_proc_init_net(struct net *net) |
| 2280 | { |
| 2281 | return tcp_proc_register(net, &tcp4_seq_afinfo); |
| 2282 | } |
| 2283 | |
| 2284 | static void tcp4_proc_exit_net(struct net *net) |
| 2285 | { |
| 2286 | tcp_proc_unregister(net, &tcp4_seq_afinfo); |
| 2287 | } |
| 2288 | |
| 2289 | static struct pernet_operations tcp4_net_ops = { |
| 2290 | .init = tcp4_proc_init_net, |
| 2291 | .exit = tcp4_proc_exit_net, |
| 2292 | }; |
| 2293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | int __init tcp4_proc_init(void) |
| 2295 | { |
Pavel Emelyanov | 757764f | 2008-03-24 14:56:02 -0700 | [diff] [blame] | 2296 | return register_pernet_subsys(&tcp4_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | void tcp4_proc_exit(void) |
| 2300 | { |
Pavel Emelyanov | 757764f | 2008-03-24 14:56:02 -0700 | [diff] [blame] | 2301 | unregister_pernet_subsys(&tcp4_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | } |
| 2303 | #endif /* CONFIG_PROC_FS */ |
| 2304 | |
| 2305 | struct proto tcp_prot = { |
| 2306 | .name = "TCP", |
| 2307 | .owner = THIS_MODULE, |
| 2308 | .close = tcp_close, |
| 2309 | .connect = tcp_v4_connect, |
| 2310 | .disconnect = tcp_disconnect, |
Arnaldo Carvalho de Melo | 463c84b | 2005-08-09 20:10:42 -0700 | [diff] [blame] | 2311 | .accept = inet_csk_accept, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | .ioctl = tcp_ioctl, |
| 2313 | .init = tcp_v4_init_sock, |
| 2314 | .destroy = tcp_v4_destroy_sock, |
| 2315 | .shutdown = tcp_shutdown, |
| 2316 | .setsockopt = tcp_setsockopt, |
| 2317 | .getsockopt = tcp_getsockopt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | .recvmsg = tcp_recvmsg, |
| 2319 | .backlog_rcv = tcp_v4_do_rcv, |
Arnaldo Carvalho de Melo | ab1e0a1 | 2008-02-03 04:06:04 -0800 | [diff] [blame] | 2320 | .hash = inet_hash, |
| 2321 | .unhash = inet_unhash, |
| 2322 | .get_port = inet_csk_get_port, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | .enter_memory_pressure = tcp_enter_memory_pressure, |
| 2324 | .sockets_allocated = &tcp_sockets_allocated, |
Arnaldo Carvalho de Melo | 0a5578c | 2005-08-09 20:11:41 -0700 | [diff] [blame] | 2325 | .orphan_count = &tcp_orphan_count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2326 | .memory_allocated = &tcp_memory_allocated, |
| 2327 | .memory_pressure = &tcp_memory_pressure, |
| 2328 | .sysctl_mem = sysctl_tcp_mem, |
| 2329 | .sysctl_wmem = sysctl_tcp_wmem, |
| 2330 | .sysctl_rmem = sysctl_tcp_rmem, |
| 2331 | .max_header = MAX_TCP_HEADER, |
| 2332 | .obj_size = sizeof(struct tcp_sock), |
Arnaldo Carvalho de Melo | 6d6ee43 | 2005-12-13 23:25:19 -0800 | [diff] [blame] | 2333 | .twsk_prot = &tcp_timewait_sock_ops, |
Arnaldo Carvalho de Melo | 60236fd | 2005-06-18 22:47:21 -0700 | [diff] [blame] | 2334 | .rsk_prot = &tcp_request_sock_ops, |
Pavel Emelyanov | 39d8cda | 2008-03-22 16:50:58 -0700 | [diff] [blame] | 2335 | .h.hashinfo = &tcp_hashinfo, |
Arnaldo Carvalho de Melo | 543d9cf | 2006-03-20 22:48:35 -0800 | [diff] [blame] | 2336 | #ifdef CONFIG_COMPAT |
| 2337 | .compat_setsockopt = compat_tcp_setsockopt, |
| 2338 | .compat_getsockopt = compat_tcp_getsockopt, |
| 2339 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | }; |
| 2341 | |
Denis V. Lunev | 046ee90 | 2008-04-03 14:31:33 -0700 | [diff] [blame] | 2342 | |
| 2343 | static int __net_init tcp_sk_init(struct net *net) |
| 2344 | { |
| 2345 | return inet_ctl_sock_create(&net->ipv4.tcp_sock, |
| 2346 | PF_INET, SOCK_RAW, IPPROTO_TCP, net); |
| 2347 | } |
| 2348 | |
| 2349 | static void __net_exit tcp_sk_exit(struct net *net) |
| 2350 | { |
| 2351 | inet_ctl_sock_destroy(net->ipv4.tcp_sock); |
| 2352 | } |
| 2353 | |
| 2354 | static struct pernet_operations __net_initdata tcp_sk_ops = { |
| 2355 | .init = tcp_sk_init, |
| 2356 | .exit = tcp_sk_exit, |
| 2357 | }; |
| 2358 | |
Denis V. Lunev | 9b0f976 | 2008-02-29 11:13:15 -0800 | [diff] [blame] | 2359 | void __init tcp_v4_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | { |
Denis V. Lunev | 046ee90 | 2008-04-03 14:31:33 -0700 | [diff] [blame] | 2361 | if (register_pernet_device(&tcp_sk_ops)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | panic("Failed to create the TCP control socket.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | EXPORT_SYMBOL(ipv4_specific); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | EXPORT_SYMBOL(tcp_hashinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | EXPORT_SYMBOL(tcp_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | EXPORT_SYMBOL(tcp_v4_conn_request); |
| 2369 | EXPORT_SYMBOL(tcp_v4_connect); |
| 2370 | EXPORT_SYMBOL(tcp_v4_do_rcv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | EXPORT_SYMBOL(tcp_v4_remember_stamp); |
| 2372 | EXPORT_SYMBOL(tcp_v4_send_check); |
| 2373 | EXPORT_SYMBOL(tcp_v4_syn_recv_sock); |
| 2374 | |
| 2375 | #ifdef CONFIG_PROC_FS |
| 2376 | EXPORT_SYMBOL(tcp_proc_register); |
| 2377 | EXPORT_SYMBOL(tcp_proc_unregister); |
| 2378 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | EXPORT_SYMBOL(sysctl_tcp_low_latency); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | |