inet_connection_sock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. * Support for INET connection oriented protocols.
  7. *
  8. * Authors: See the TCP sources
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or(at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/jhash.h>
  17. #include <net/inet_connection_sock.h>
  18. #include <net/inet_hashtables.h>
  19. #include <net/inet_timewait_sock.h>
  20. #include <net/ip.h>
  21. #include <net/route.h>
  22. #include <net/tcp_states.h>
  23. #include <net/xfrm.h>
  24. #ifdef INET_CSK_DEBUG
  25. const char inet_csk_timer_bug_msg[] = "inet_csk BUG: unknown timer value\n";
  26. EXPORT_SYMBOL(inet_csk_timer_bug_msg);
  27. #endif
  28. /*
  29. * This struct holds the first and last local port number.
  30. */
  31. struct local_ports sysctl_local_ports __read_mostly = {
  32. .lock = SEQLOCK_UNLOCKED,
  33. .range = { 32768, 61000 },
  34. };
  35. void inet_get_local_port_range(int *low, int *high)
  36. {
  37. unsigned seq;
  38. do {
  39. seq = read_seqbegin(&sysctl_local_ports.lock);
  40. *low = sysctl_local_ports.range[0];
  41. *high = sysctl_local_ports.range[1];
  42. } while (read_seqretry(&sysctl_local_ports.lock, seq));
  43. }
  44. EXPORT_SYMBOL(inet_get_local_port_range);
  45. int inet_csk_bind_conflict(const struct sock *sk,
  46. const struct inet_bind_bucket *tb)
  47. {
  48. const __be32 sk_rcv_saddr = inet_rcv_saddr(sk);
  49. struct sock *sk2;
  50. struct hlist_node *node;
  51. int reuse = sk->sk_reuse;
  52. /*
  53. * Unlike other sk lookup places we do not check
  54. * for sk_net here, since _all_ the socks listed
  55. * in tb->owners list belong to the same net - the
  56. * one this bucket belongs to.
  57. */
  58. sk_for_each_bound(sk2, node, &tb->owners) {
  59. if (sk != sk2 &&
  60. !inet_v6_ipv6only(sk2) &&
  61. (!sk->sk_bound_dev_if ||
  62. !sk2->sk_bound_dev_if ||
  63. sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) {
  64. if (!reuse || !sk2->sk_reuse ||
  65. sk2->sk_state == TCP_LISTEN) {
  66. const __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
  67. if (!sk2_rcv_saddr || !sk_rcv_saddr ||
  68. sk2_rcv_saddr == sk_rcv_saddr)
  69. break;
  70. }
  71. }
  72. }
  73. return node != NULL;
  74. }
  75. EXPORT_SYMBOL_GPL(inet_csk_bind_conflict);
  76. /* Obtain a reference to a local port for the given sock,
  77. * if snum is zero it means select any available local port.
  78. */
  79. int inet_csk_get_port(struct sock *sk, unsigned short snum)
  80. {
  81. struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
  82. struct inet_bind_hashbucket *head;
  83. struct hlist_node *node;
  84. struct inet_bind_bucket *tb;
  85. int ret, attempts = 5;
  86. struct net *net = sock_net(sk);
  87. int smallest_size = -1, smallest_rover;
  88. local_bh_disable();
  89. if (!snum) {
  90. int remaining, rover, low, high;
  91. again:
  92. inet_get_local_port_range(&low, &high);
  93. remaining = (high - low) + 1;
  94. smallest_rover = rover = net_random() % remaining + low;
  95. smallest_size = -1;
  96. do {
  97. head = &hashinfo->bhash[inet_bhashfn(net, rover,
  98. hashinfo->bhash_size)];
  99. spin_lock(&head->lock);
  100. inet_bind_bucket_for_each(tb, node, &head->chain)
  101. if (ib_net(tb) == net && tb->port == rover) {
  102. if (tb->fastreuse > 0 &&
  103. sk->sk_reuse &&
  104. sk->sk_state != TCP_LISTEN &&
  105. (tb->num_owners < smallest_size || smallest_size == -1)) {
  106. smallest_size = tb->num_owners;
  107. smallest_rover = rover;
  108. if (hashinfo->bsockets > (high - low) + 1) {
  109. spin_unlock(&head->lock);
  110. snum = smallest_rover;
  111. goto have_snum;
  112. }
  113. }
  114. goto next;
  115. }
  116. break;
  117. next:
  118. spin_unlock(&head->lock);
  119. if (++rover > high)
  120. rover = low;
  121. } while (--remaining > 0);
  122. /* Exhausted local port range during search? It is not
  123. * possible for us to be holding one of the bind hash
  124. * locks if this test triggers, because if 'remaining'
  125. * drops to zero, we broke out of the do/while loop at
  126. * the top level, not from the 'break;' statement.
  127. */
  128. ret = 1;
  129. if (remaining <= 0) {
  130. if (smallest_size != -1) {
  131. snum = smallest_rover;
  132. goto have_snum;
  133. }
  134. goto fail;
  135. }
  136. /* OK, here is the one we will use. HEAD is
  137. * non-NULL and we hold it's mutex.
  138. */
  139. snum = rover;
  140. } else {
  141. have_snum:
  142. head = &hashinfo->bhash[inet_bhashfn(net, snum,
  143. hashinfo->bhash_size)];
  144. spin_lock(&head->lock);
  145. inet_bind_bucket_for_each(tb, node, &head->chain)
  146. if (ib_net(tb) == net && tb->port == snum)
  147. goto tb_found;
  148. }
  149. tb = NULL;
  150. goto tb_not_found;
  151. tb_found:
  152. if (!hlist_empty(&tb->owners)) {
  153. if (tb->fastreuse > 0 &&
  154. sk->sk_reuse && sk->sk_state != TCP_LISTEN &&
  155. smallest_size == -1) {
  156. goto success;
  157. } else {
  158. ret = 1;
  159. if (inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb)) {
  160. if (sk->sk_reuse && sk->sk_state != TCP_LISTEN && --attempts >= 0) {
  161. spin_unlock(&head->lock);
  162. goto again;
  163. }
  164. goto fail_unlock;
  165. }
  166. }
  167. }
  168. tb_not_found:
  169. ret = 1;
  170. if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep,
  171. net, head, snum)) == NULL)
  172. goto fail_unlock;
  173. if (hlist_empty(&tb->owners)) {
  174. if (sk->sk_reuse && sk->sk_state != TCP_LISTEN)
  175. tb->fastreuse = 1;
  176. else
  177. tb->fastreuse = 0;
  178. } else if (tb->fastreuse &&
  179. (!sk->sk_reuse || sk->sk_state == TCP_LISTEN))
  180. tb->fastreuse = 0;
  181. success:
  182. if (!inet_csk(sk)->icsk_bind_hash)
  183. inet_bind_hash(sk, tb, snum);
  184. WARN_ON(inet_csk(sk)->icsk_bind_hash != tb);
  185. ret = 0;
  186. fail_unlock:
  187. spin_unlock(&head->lock);
  188. fail:
  189. local_bh_enable();
  190. return ret;
  191. }
  192. EXPORT_SYMBOL_GPL(inet_csk_get_port);
  193. /*
  194. * Wait for an incoming connection, avoid race conditions. This must be called
  195. * with the socket locked.
  196. */
  197. static int inet_csk_wait_for_connect(struct sock *sk, long timeo)
  198. {
  199. struct inet_connection_sock *icsk = inet_csk(sk);
  200. DEFINE_WAIT(wait);
  201. int err;
  202. /*
  203. * True wake-one mechanism for incoming connections: only
  204. * one process gets woken up, not the 'whole herd'.
  205. * Since we do not 'race & poll' for established sockets
  206. * anymore, the common case will execute the loop only once.
  207. *
  208. * Subtle issue: "add_wait_queue_exclusive()" will be added
  209. * after any current non-exclusive waiters, and we know that
  210. * it will always _stay_ after any new non-exclusive waiters
  211. * because all non-exclusive waiters are added at the
  212. * beginning of the wait-queue. As such, it's ok to "drop"
  213. * our exclusiveness temporarily when we get woken up without
  214. * having to remove and re-insert us on the wait queue.
  215. */
  216. for (;;) {
  217. prepare_to_wait_exclusive(sk->sk_sleep, &wait,
  218. TASK_INTERRUPTIBLE);
  219. release_sock(sk);
  220. if (reqsk_queue_empty(&icsk->icsk_accept_queue))
  221. timeo = schedule_timeout(timeo);
  222. lock_sock(sk);
  223. err = 0;
  224. if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
  225. break;
  226. err = -EINVAL;
  227. if (sk->sk_state != TCP_LISTEN)
  228. break;
  229. err = sock_intr_errno(timeo);
  230. if (signal_pending(current))
  231. break;
  232. err = -EAGAIN;
  233. if (!timeo)
  234. break;
  235. }
  236. finish_wait(sk->sk_sleep, &wait);
  237. return err;
  238. }
  239. /*
  240. * This will accept the next outstanding connection.
  241. */
  242. struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
  243. {
  244. struct inet_connection_sock *icsk = inet_csk(sk);
  245. struct sock *newsk;
  246. int error;
  247. lock_sock(sk);
  248. /* We need to make sure that this socket is listening,
  249. * and that it has something pending.
  250. */
  251. error = -EINVAL;
  252. if (sk->sk_state != TCP_LISTEN)
  253. goto out_err;
  254. /* Find already established connection */
  255. if (reqsk_queue_empty(&icsk->icsk_accept_queue)) {
  256. long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  257. /* If this is a non blocking socket don't sleep */
  258. error = -EAGAIN;
  259. if (!timeo)
  260. goto out_err;
  261. error = inet_csk_wait_for_connect(sk, timeo);
  262. if (error)
  263. goto out_err;
  264. }
  265. newsk = reqsk_queue_get_child(&icsk->icsk_accept_queue, sk);
  266. WARN_ON(newsk->sk_state == TCP_SYN_RECV);
  267. out:
  268. release_sock(sk);
  269. return newsk;
  270. out_err:
  271. newsk = NULL;
  272. *err = error;
  273. goto out;
  274. }
  275. EXPORT_SYMBOL(inet_csk_accept);
  276. /*
  277. * Using different timers for retransmit, delayed acks and probes
  278. * We may wish use just one timer maintaining a list of expire jiffies
  279. * to optimize.
  280. */
  281. void inet_csk_init_xmit_timers(struct sock *sk,
  282. void (*retransmit_handler)(unsigned long),
  283. void (*delack_handler)(unsigned long),
  284. void (*keepalive_handler)(unsigned long))
  285. {
  286. struct inet_connection_sock *icsk = inet_csk(sk);
  287. setup_timer(&icsk->icsk_retransmit_timer, retransmit_handler,
  288. (unsigned long)sk);
  289. setup_timer(&icsk->icsk_delack_timer, delack_handler,
  290. (unsigned long)sk);
  291. setup_timer(&sk->sk_timer, keepalive_handler, (unsigned long)sk);
  292. icsk->icsk_pending = icsk->icsk_ack.pending = 0;
  293. }
  294. EXPORT_SYMBOL(inet_csk_init_xmit_timers);
  295. void inet_csk_clear_xmit_timers(struct sock *sk)
  296. {
  297. struct inet_connection_sock *icsk = inet_csk(sk);
  298. icsk->icsk_pending = icsk->icsk_ack.pending = icsk->icsk_ack.blocked = 0;
  299. sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
  300. sk_stop_timer(sk, &icsk->icsk_delack_timer);
  301. sk_stop_timer(sk, &sk->sk_timer);
  302. }
  303. EXPORT_SYMBOL(inet_csk_clear_xmit_timers);
  304. void inet_csk_delete_keepalive_timer(struct sock *sk)
  305. {
  306. sk_stop_timer(sk, &sk->sk_timer);
  307. }
  308. EXPORT_SYMBOL(inet_csk_delete_keepalive_timer);
  309. void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len)
  310. {
  311. sk_reset_timer(sk, &sk->sk_timer, jiffies + len);
  312. }
  313. EXPORT_SYMBOL(inet_csk_reset_keepalive_timer);
  314. struct dst_entry *inet_csk_route_req(struct sock *sk,
  315. const struct request_sock *req)
  316. {
  317. struct rtable *rt;
  318. const struct inet_request_sock *ireq = inet_rsk(req);
  319. struct ip_options *opt = inet_rsk(req)->opt;
  320. struct flowi fl = { .oif = sk->sk_bound_dev_if,
  321. .nl_u = { .ip4_u =
  322. { .daddr = ((opt && opt->srr) ?
  323. opt->faddr :
  324. ireq->rmt_addr),
  325. .saddr = ireq->loc_addr,
  326. .tos = RT_CONN_FLAGS(sk) } },
  327. .proto = sk->sk_protocol,
  328. .flags = inet_sk_flowi_flags(sk),
  329. .uli_u = { .ports =
  330. { .sport = inet_sk(sk)->sport,
  331. .dport = ireq->rmt_port } } };
  332. struct net *net = sock_net(sk);
  333. security_req_classify_flow(req, &fl);
  334. if (ip_route_output_flow(net, &rt, &fl, sk, 0))
  335. goto no_route;
  336. if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway)
  337. goto route_err;
  338. return &rt->u.dst;
  339. route_err:
  340. ip_rt_put(rt);
  341. no_route:
  342. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  343. return NULL;
  344. }
  345. EXPORT_SYMBOL_GPL(inet_csk_route_req);
  346. static inline u32 inet_synq_hash(const __be32 raddr, const __be16 rport,
  347. const u32 rnd, const u32 synq_hsize)
  348. {
  349. return jhash_2words((__force u32)raddr, (__force u32)rport, rnd) & (synq_hsize - 1);
  350. }
  351. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  352. #define AF_INET_FAMILY(fam) ((fam) == AF_INET)
  353. #else
  354. #define AF_INET_FAMILY(fam) 1
  355. #endif
  356. struct request_sock *inet_csk_search_req(const struct sock *sk,
  357. struct request_sock ***prevp,
  358. const __be16 rport, const __be32 raddr,
  359. const __be32 laddr)
  360. {
  361. const struct inet_connection_sock *icsk = inet_csk(sk);
  362. struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
  363. struct request_sock *req, **prev;
  364. for (prev = &lopt->syn_table[inet_synq_hash(raddr, rport, lopt->hash_rnd,
  365. lopt->nr_table_entries)];
  366. (req = *prev) != NULL;
  367. prev = &req->dl_next) {
  368. const struct inet_request_sock *ireq = inet_rsk(req);
  369. if (ireq->rmt_port == rport &&
  370. ireq->rmt_addr == raddr &&
  371. ireq->loc_addr == laddr &&
  372. AF_INET_FAMILY(req->rsk_ops->family)) {
  373. WARN_ON(req->sk);
  374. *prevp = prev;
  375. break;
  376. }
  377. }
  378. return req;
  379. }
  380. EXPORT_SYMBOL_GPL(inet_csk_search_req);
  381. void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
  382. unsigned long timeout)
  383. {
  384. struct inet_connection_sock *icsk = inet_csk(sk);
  385. struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
  386. const u32 h = inet_synq_hash(inet_rsk(req)->rmt_addr, inet_rsk(req)->rmt_port,
  387. lopt->hash_rnd, lopt->nr_table_entries);
  388. reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
  389. inet_csk_reqsk_queue_added(sk, timeout);
  390. }
  391. /* Only thing we need from tcp.h */
  392. extern int sysctl_tcp_synack_retries;
  393. EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);
  394. void inet_csk_reqsk_queue_prune(struct sock *parent,
  395. const unsigned long interval,
  396. const unsigned long timeout,
  397. const unsigned long max_rto)
  398. {
  399. struct inet_connection_sock *icsk = inet_csk(parent);
  400. struct request_sock_queue *queue = &icsk->icsk_accept_queue;
  401. struct listen_sock *lopt = queue->listen_opt;
  402. int max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
  403. int thresh = max_retries;
  404. unsigned long now = jiffies;
  405. struct request_sock **reqp, *req;
  406. int i, budget;
  407. if (lopt == NULL || lopt->qlen == 0)
  408. return;
  409. /* Normally all the openreqs are young and become mature
  410. * (i.e. converted to established socket) for first timeout.
  411. * If synack was not acknowledged for 3 seconds, it means
  412. * one of the following things: synack was lost, ack was lost,
  413. * rtt is high or nobody planned to ack (i.e. synflood).
  414. * When server is a bit loaded, queue is populated with old
  415. * open requests, reducing effective size of queue.
  416. * When server is well loaded, queue size reduces to zero
  417. * after several minutes of work. It is not synflood,
  418. * it is normal operation. The solution is pruning
  419. * too old entries overriding normal timeout, when
  420. * situation becomes dangerous.
  421. *
  422. * Essentially, we reserve half of room for young
  423. * embrions; and abort old ones without pity, if old
  424. * ones are about to clog our table.
  425. */
  426. if (lopt->qlen>>(lopt->max_qlen_log-1)) {
  427. int young = (lopt->qlen_young<<1);
  428. while (thresh > 2) {
  429. if (lopt->qlen < young)
  430. break;
  431. thresh--;
  432. young <<= 1;
  433. }
  434. }
  435. if (queue->rskq_defer_accept)
  436. max_retries = queue->rskq_defer_accept;
  437. budget = 2 * (lopt->nr_table_entries / (timeout / interval));
  438. i = lopt->clock_hand;
  439. do {
  440. reqp=&lopt->syn_table[i];
  441. while ((req = *reqp) != NULL) {
  442. if (time_after_eq(now, req->expires)) {
  443. if ((req->retrans < thresh ||
  444. (inet_rsk(req)->acked && req->retrans < max_retries))
  445. && !req->rsk_ops->rtx_syn_ack(parent, req)) {
  446. unsigned long timeo;
  447. if (req->retrans++ == 0)
  448. lopt->qlen_young--;
  449. timeo = min((timeout << req->retrans), max_rto);
  450. req->expires = now + timeo;
  451. reqp = &req->dl_next;
  452. continue;
  453. }
  454. /* Drop this request */
  455. inet_csk_reqsk_queue_unlink(parent, req, reqp);
  456. reqsk_queue_removed(queue, req);
  457. reqsk_free(req);
  458. continue;
  459. }
  460. reqp = &req->dl_next;
  461. }
  462. i = (i + 1) & (lopt->nr_table_entries - 1);
  463. } while (--budget > 0);
  464. lopt->clock_hand = i;
  465. if (lopt->qlen)
  466. inet_csk_reset_keepalive_timer(parent, interval);
  467. }
  468. EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune);
  469. struct sock *inet_csk_clone(struct sock *sk, const struct request_sock *req,
  470. const gfp_t priority)
  471. {
  472. struct sock *newsk = sk_clone(sk, priority);
  473. if (newsk != NULL) {
  474. struct inet_connection_sock *newicsk = inet_csk(newsk);
  475. newsk->sk_state = TCP_SYN_RECV;
  476. newicsk->icsk_bind_hash = NULL;
  477. inet_sk(newsk)->dport = inet_rsk(req)->rmt_port;
  478. inet_sk(newsk)->num = ntohs(inet_rsk(req)->loc_port);
  479. inet_sk(newsk)->sport = inet_rsk(req)->loc_port;
  480. newsk->sk_write_space = sk_stream_write_space;
  481. newicsk->icsk_retransmits = 0;
  482. newicsk->icsk_backoff = 0;
  483. newicsk->icsk_probes_out = 0;
  484. /* Deinitialize accept_queue to trap illegal accesses. */
  485. memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
  486. security_inet_csk_clone(newsk, req);
  487. }
  488. return newsk;
  489. }
  490. EXPORT_SYMBOL_GPL(inet_csk_clone);
  491. /*
  492. * At this point, there should be no process reference to this
  493. * socket, and thus no user references at all. Therefore we
  494. * can assume the socket waitqueue is inactive and nobody will
  495. * try to jump onto it.
  496. */
  497. void inet_csk_destroy_sock(struct sock *sk)
  498. {
  499. WARN_ON(sk->sk_state != TCP_CLOSE);
  500. WARN_ON(!sock_flag(sk, SOCK_DEAD));
  501. /* It cannot be in hash table! */
  502. WARN_ON(!sk_unhashed(sk));
  503. /* If it has not 0 inet_sk(sk)->num, it must be bound */
  504. WARN_ON(inet_sk(sk)->num && !inet_csk(sk)->icsk_bind_hash);
  505. sk->sk_prot->destroy(sk);
  506. sk_stream_kill_queues(sk);
  507. xfrm_sk_free_policy(sk);
  508. sk_refcnt_debug_release(sk);
  509. percpu_counter_dec(sk->sk_prot->orphan_count);
  510. sock_put(sk);
  511. }
  512. EXPORT_SYMBOL(inet_csk_destroy_sock);
  513. int inet_csk_listen_start(struct sock *sk, const int nr_table_entries)
  514. {
  515. struct inet_sock *inet = inet_sk(sk);
  516. struct inet_connection_sock *icsk = inet_csk(sk);
  517. int rc = reqsk_queue_alloc(&icsk->icsk_accept_queue, nr_table_entries);
  518. if (rc != 0)
  519. return rc;
  520. sk->sk_max_ack_backlog = 0;
  521. sk->sk_ack_backlog = 0;
  522. inet_csk_delack_init(sk);
  523. /* There is race window here: we announce ourselves listening,
  524. * but this transition is still not validated by get_port().
  525. * It is OK, because this socket enters to hash table only
  526. * after validation is complete.
  527. */
  528. sk->sk_state = TCP_LISTEN;
  529. if (!sk->sk_prot->get_port(sk, inet->num)) {
  530. inet->sport = htons(inet->num);
  531. sk_dst_reset(sk);
  532. sk->sk_prot->hash(sk);
  533. return 0;
  534. }
  535. sk->sk_state = TCP_CLOSE;
  536. __reqsk_queue_destroy(&icsk->icsk_accept_queue);
  537. return -EADDRINUSE;
  538. }
  539. EXPORT_SYMBOL_GPL(inet_csk_listen_start);
  540. /*
  541. * This routine closes sockets which have been at least partially
  542. * opened, but not yet accepted.
  543. */
  544. void inet_csk_listen_stop(struct sock *sk)
  545. {
  546. struct inet_connection_sock *icsk = inet_csk(sk);
  547. struct request_sock *acc_req;
  548. struct request_sock *req;
  549. inet_csk_delete_keepalive_timer(sk);
  550. /* make all the listen_opt local to us */
  551. acc_req = reqsk_queue_yank_acceptq(&icsk->icsk_accept_queue);
  552. /* Following specs, it would be better either to send FIN
  553. * (and enter FIN-WAIT-1, it is normal close)
  554. * or to send active reset (abort).
  555. * Certainly, it is pretty dangerous while synflood, but it is
  556. * bad justification for our negligence 8)
  557. * To be honest, we are not able to make either
  558. * of the variants now. --ANK
  559. */
  560. reqsk_queue_destroy(&icsk->icsk_accept_queue);
  561. while ((req = acc_req) != NULL) {
  562. struct sock *child = req->sk;
  563. acc_req = req->dl_next;
  564. local_bh_disable();
  565. bh_lock_sock(child);
  566. WARN_ON(sock_owned_by_user(child));
  567. sock_hold(child);
  568. sk->sk_prot->disconnect(child, O_NONBLOCK);
  569. sock_orphan(child);
  570. percpu_counter_inc(sk->sk_prot->orphan_count);
  571. inet_csk_destroy_sock(child);
  572. bh_unlock_sock(child);
  573. local_bh_enable();
  574. sock_put(child);
  575. sk_acceptq_removed(sk);
  576. __reqsk_free(req);
  577. }
  578. WARN_ON(sk->sk_ack_backlog);
  579. }
  580. EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
  581. void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
  582. {
  583. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  584. const struct inet_sock *inet = inet_sk(sk);
  585. sin->sin_family = AF_INET;
  586. sin->sin_addr.s_addr = inet->daddr;
  587. sin->sin_port = inet->dport;
  588. }
  589. EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr);
  590. #ifdef CONFIG_COMPAT
  591. int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
  592. char __user *optval, int __user *optlen)
  593. {
  594. const struct inet_connection_sock *icsk = inet_csk(sk);
  595. if (icsk->icsk_af_ops->compat_getsockopt != NULL)
  596. return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname,
  597. optval, optlen);
  598. return icsk->icsk_af_ops->getsockopt(sk, level, optname,
  599. optval, optlen);
  600. }
  601. EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt);
  602. int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
  603. char __user *optval, int optlen)
  604. {
  605. const struct inet_connection_sock *icsk = inet_csk(sk);
  606. if (icsk->icsk_af_ops->compat_setsockopt != NULL)
  607. return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname,
  608. optval, optlen);
  609. return icsk->icsk_af_ops->setsockopt(sk, level, optname,
  610. optval, optlen);
  611. }
  612. EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt);
  613. #endif