inet_connection_sock.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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 (net_eq(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 (atomic_read(&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 (net_eq(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 &&
  161. smallest_size != -1 && --attempts >= 0) {
  162. spin_unlock(&head->lock);
  163. goto again;
  164. }
  165. goto fail_unlock;
  166. }
  167. }
  168. }
  169. tb_not_found:
  170. ret = 1;
  171. if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep,
  172. net, head, snum)) == NULL)
  173. goto fail_unlock;
  174. if (hlist_empty(&tb->owners)) {
  175. if (sk->sk_reuse && sk->sk_state != TCP_LISTEN)
  176. tb->fastreuse = 1;
  177. else
  178. tb->fastreuse = 0;
  179. } else if (tb->fastreuse &&
  180. (!sk->sk_reuse || sk->sk_state == TCP_LISTEN))
  181. tb->fastreuse = 0;
  182. success:
  183. if (!inet_csk(sk)->icsk_bind_hash)
  184. inet_bind_hash(sk, tb, snum);
  185. WARN_ON(inet_csk(sk)->icsk_bind_hash != tb);
  186. ret = 0;
  187. fail_unlock:
  188. spin_unlock(&head->lock);
  189. fail:
  190. local_bh_enable();
  191. return ret;
  192. }
  193. EXPORT_SYMBOL_GPL(inet_csk_get_port);
  194. /*
  195. * Wait for an incoming connection, avoid race conditions. This must be called
  196. * with the socket locked.
  197. */
  198. static int inet_csk_wait_for_connect(struct sock *sk, long timeo)
  199. {
  200. struct inet_connection_sock *icsk = inet_csk(sk);
  201. DEFINE_WAIT(wait);
  202. int err;
  203. /*
  204. * True wake-one mechanism for incoming connections: only
  205. * one process gets woken up, not the 'whole herd'.
  206. * Since we do not 'race & poll' for established sockets
  207. * anymore, the common case will execute the loop only once.
  208. *
  209. * Subtle issue: "add_wait_queue_exclusive()" will be added
  210. * after any current non-exclusive waiters, and we know that
  211. * it will always _stay_ after any new non-exclusive waiters
  212. * because all non-exclusive waiters are added at the
  213. * beginning of the wait-queue. As such, it's ok to "drop"
  214. * our exclusiveness temporarily when we get woken up without
  215. * having to remove and re-insert us on the wait queue.
  216. */
  217. for (;;) {
  218. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  219. TASK_INTERRUPTIBLE);
  220. release_sock(sk);
  221. if (reqsk_queue_empty(&icsk->icsk_accept_queue))
  222. timeo = schedule_timeout(timeo);
  223. lock_sock(sk);
  224. err = 0;
  225. if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
  226. break;
  227. err = -EINVAL;
  228. if (sk->sk_state != TCP_LISTEN)
  229. break;
  230. err = sock_intr_errno(timeo);
  231. if (signal_pending(current))
  232. break;
  233. err = -EAGAIN;
  234. if (!timeo)
  235. break;
  236. }
  237. finish_wait(sk_sleep(sk), &wait);
  238. return err;
  239. }
  240. /*
  241. * This will accept the next outstanding connection.
  242. */
  243. struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
  244. {
  245. struct inet_connection_sock *icsk = inet_csk(sk);
  246. struct sock *newsk;
  247. int error;
  248. lock_sock(sk);
  249. /* We need to make sure that this socket is listening,
  250. * and that it has something pending.
  251. */
  252. error = -EINVAL;
  253. if (sk->sk_state != TCP_LISTEN)
  254. goto out_err;
  255. /* Find already established connection */
  256. if (reqsk_queue_empty(&icsk->icsk_accept_queue)) {
  257. long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  258. /* If this is a non blocking socket don't sleep */
  259. error = -EAGAIN;
  260. if (!timeo)
  261. goto out_err;
  262. error = inet_csk_wait_for_connect(sk, timeo);
  263. if (error)
  264. goto out_err;
  265. }
  266. newsk = reqsk_queue_get_child(&icsk->icsk_accept_queue, sk);
  267. WARN_ON(newsk->sk_state == TCP_SYN_RECV);
  268. out:
  269. release_sock(sk);
  270. return newsk;
  271. out_err:
  272. newsk = NULL;
  273. *err = error;
  274. goto out;
  275. }
  276. EXPORT_SYMBOL(inet_csk_accept);
  277. /*
  278. * Using different timers for retransmit, delayed acks and probes
  279. * We may wish use just one timer maintaining a list of expire jiffies
  280. * to optimize.
  281. */
  282. void inet_csk_init_xmit_timers(struct sock *sk,
  283. void (*retransmit_handler)(unsigned long),
  284. void (*delack_handler)(unsigned long),
  285. void (*keepalive_handler)(unsigned long))
  286. {
  287. struct inet_connection_sock *icsk = inet_csk(sk);
  288. setup_timer(&icsk->icsk_retransmit_timer, retransmit_handler,
  289. (unsigned long)sk);
  290. setup_timer(&icsk->icsk_delack_timer, delack_handler,
  291. (unsigned long)sk);
  292. setup_timer(&sk->sk_timer, keepalive_handler, (unsigned long)sk);
  293. icsk->icsk_pending = icsk->icsk_ack.pending = 0;
  294. }
  295. EXPORT_SYMBOL(inet_csk_init_xmit_timers);
  296. void inet_csk_clear_xmit_timers(struct sock *sk)
  297. {
  298. struct inet_connection_sock *icsk = inet_csk(sk);
  299. icsk->icsk_pending = icsk->icsk_ack.pending = icsk->icsk_ack.blocked = 0;
  300. sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
  301. sk_stop_timer(sk, &icsk->icsk_delack_timer);
  302. sk_stop_timer(sk, &sk->sk_timer);
  303. }
  304. EXPORT_SYMBOL(inet_csk_clear_xmit_timers);
  305. void inet_csk_delete_keepalive_timer(struct sock *sk)
  306. {
  307. sk_stop_timer(sk, &sk->sk_timer);
  308. }
  309. EXPORT_SYMBOL(inet_csk_delete_keepalive_timer);
  310. void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len)
  311. {
  312. sk_reset_timer(sk, &sk->sk_timer, jiffies + len);
  313. }
  314. EXPORT_SYMBOL(inet_csk_reset_keepalive_timer);
  315. struct dst_entry *inet_csk_route_req(struct sock *sk,
  316. const struct request_sock *req)
  317. {
  318. struct rtable *rt;
  319. const struct inet_request_sock *ireq = inet_rsk(req);
  320. struct ip_options *opt = inet_rsk(req)->opt;
  321. struct flowi fl = { .oif = sk->sk_bound_dev_if,
  322. .mark = sk->sk_mark,
  323. .nl_u = { .ip4_u =
  324. { .daddr = ((opt && opt->srr) ?
  325. opt->faddr :
  326. ireq->rmt_addr),
  327. .saddr = ireq->loc_addr,
  328. .tos = RT_CONN_FLAGS(sk) } },
  329. .proto = sk->sk_protocol,
  330. .flags = inet_sk_flowi_flags(sk),
  331. .uli_u = { .ports =
  332. { .sport = inet_sk(sk)->inet_sport,
  333. .dport = ireq->rmt_port } } };
  334. struct net *net = sock_net(sk);
  335. security_req_classify_flow(req, &fl);
  336. if (ip_route_output_flow(net, &rt, &fl, sk, 0))
  337. goto no_route;
  338. if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway)
  339. goto route_err;
  340. return &rt->u.dst;
  341. route_err:
  342. ip_rt_put(rt);
  343. no_route:
  344. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  345. return NULL;
  346. }
  347. EXPORT_SYMBOL_GPL(inet_csk_route_req);
  348. static inline u32 inet_synq_hash(const __be32 raddr, const __be16 rport,
  349. const u32 rnd, const u32 synq_hsize)
  350. {
  351. return jhash_2words((__force u32)raddr, (__force u32)rport, rnd) & (synq_hsize - 1);
  352. }
  353. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  354. #define AF_INET_FAMILY(fam) ((fam) == AF_INET)
  355. #else
  356. #define AF_INET_FAMILY(fam) 1
  357. #endif
  358. struct request_sock *inet_csk_search_req(const struct sock *sk,
  359. struct request_sock ***prevp,
  360. const __be16 rport, const __be32 raddr,
  361. const __be32 laddr)
  362. {
  363. const struct inet_connection_sock *icsk = inet_csk(sk);
  364. struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
  365. struct request_sock *req, **prev;
  366. for (prev = &lopt->syn_table[inet_synq_hash(raddr, rport, lopt->hash_rnd,
  367. lopt->nr_table_entries)];
  368. (req = *prev) != NULL;
  369. prev = &req->dl_next) {
  370. const struct inet_request_sock *ireq = inet_rsk(req);
  371. if (ireq->rmt_port == rport &&
  372. ireq->rmt_addr == raddr &&
  373. ireq->loc_addr == laddr &&
  374. AF_INET_FAMILY(req->rsk_ops->family)) {
  375. WARN_ON(req->sk);
  376. *prevp = prev;
  377. break;
  378. }
  379. }
  380. return req;
  381. }
  382. EXPORT_SYMBOL_GPL(inet_csk_search_req);
  383. void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
  384. unsigned long timeout)
  385. {
  386. struct inet_connection_sock *icsk = inet_csk(sk);
  387. struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
  388. const u32 h = inet_synq_hash(inet_rsk(req)->rmt_addr, inet_rsk(req)->rmt_port,
  389. lopt->hash_rnd, lopt->nr_table_entries);
  390. reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
  391. inet_csk_reqsk_queue_added(sk, timeout);
  392. }
  393. /* Only thing we need from tcp.h */
  394. extern int sysctl_tcp_synack_retries;
  395. EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);
  396. /* Decide when to expire the request and when to resend SYN-ACK */
  397. static inline void syn_ack_recalc(struct request_sock *req, const int thresh,
  398. const int max_retries,
  399. const u8 rskq_defer_accept,
  400. int *expire, int *resend)
  401. {
  402. if (!rskq_defer_accept) {
  403. *expire = req->retrans >= thresh;
  404. *resend = 1;
  405. return;
  406. }
  407. *expire = req->retrans >= thresh &&
  408. (!inet_rsk(req)->acked || req->retrans >= max_retries);
  409. /*
  410. * Do not resend while waiting for data after ACK,
  411. * start to resend on end of deferring period to give
  412. * last chance for data or ACK to create established socket.
  413. */
  414. *resend = !inet_rsk(req)->acked ||
  415. req->retrans >= rskq_defer_accept - 1;
  416. }
  417. void inet_csk_reqsk_queue_prune(struct sock *parent,
  418. const unsigned long interval,
  419. const unsigned long timeout,
  420. const unsigned long max_rto)
  421. {
  422. struct inet_connection_sock *icsk = inet_csk(parent);
  423. struct request_sock_queue *queue = &icsk->icsk_accept_queue;
  424. struct listen_sock *lopt = queue->listen_opt;
  425. int max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
  426. int thresh = max_retries;
  427. unsigned long now = jiffies;
  428. struct request_sock **reqp, *req;
  429. int i, budget;
  430. if (lopt == NULL || lopt->qlen == 0)
  431. return;
  432. /* Normally all the openreqs are young and become mature
  433. * (i.e. converted to established socket) for first timeout.
  434. * If synack was not acknowledged for 3 seconds, it means
  435. * one of the following things: synack was lost, ack was lost,
  436. * rtt is high or nobody planned to ack (i.e. synflood).
  437. * When server is a bit loaded, queue is populated with old
  438. * open requests, reducing effective size of queue.
  439. * When server is well loaded, queue size reduces to zero
  440. * after several minutes of work. It is not synflood,
  441. * it is normal operation. The solution is pruning
  442. * too old entries overriding normal timeout, when
  443. * situation becomes dangerous.
  444. *
  445. * Essentially, we reserve half of room for young
  446. * embrions; and abort old ones without pity, if old
  447. * ones are about to clog our table.
  448. */
  449. if (lopt->qlen>>(lopt->max_qlen_log-1)) {
  450. int young = (lopt->qlen_young<<1);
  451. while (thresh > 2) {
  452. if (lopt->qlen < young)
  453. break;
  454. thresh--;
  455. young <<= 1;
  456. }
  457. }
  458. if (queue->rskq_defer_accept)
  459. max_retries = queue->rskq_defer_accept;
  460. budget = 2 * (lopt->nr_table_entries / (timeout / interval));
  461. i = lopt->clock_hand;
  462. do {
  463. reqp=&lopt->syn_table[i];
  464. while ((req = *reqp) != NULL) {
  465. if (time_after_eq(now, req->expires)) {
  466. int expire = 0, resend = 0;
  467. syn_ack_recalc(req, thresh, max_retries,
  468. queue->rskq_defer_accept,
  469. &expire, &resend);
  470. if (req->rsk_ops->syn_ack_timeout)
  471. req->rsk_ops->syn_ack_timeout(parent, req);
  472. if (!expire &&
  473. (!resend ||
  474. !req->rsk_ops->rtx_syn_ack(parent, req, NULL) ||
  475. inet_rsk(req)->acked)) {
  476. unsigned long timeo;
  477. if (req->retrans++ == 0)
  478. lopt->qlen_young--;
  479. timeo = min((timeout << req->retrans), max_rto);
  480. req->expires = now + timeo;
  481. reqp = &req->dl_next;
  482. continue;
  483. }
  484. /* Drop this request */
  485. inet_csk_reqsk_queue_unlink(parent, req, reqp);
  486. reqsk_queue_removed(queue, req);
  487. reqsk_free(req);
  488. continue;
  489. }
  490. reqp = &req->dl_next;
  491. }
  492. i = (i + 1) & (lopt->nr_table_entries - 1);
  493. } while (--budget > 0);
  494. lopt->clock_hand = i;
  495. if (lopt->qlen)
  496. inet_csk_reset_keepalive_timer(parent, interval);
  497. }
  498. EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune);
  499. struct sock *inet_csk_clone(struct sock *sk, const struct request_sock *req,
  500. const gfp_t priority)
  501. {
  502. struct sock *newsk = sk_clone(sk, priority);
  503. if (newsk != NULL) {
  504. struct inet_connection_sock *newicsk = inet_csk(newsk);
  505. newsk->sk_state = TCP_SYN_RECV;
  506. newicsk->icsk_bind_hash = NULL;
  507. inet_sk(newsk)->inet_dport = inet_rsk(req)->rmt_port;
  508. inet_sk(newsk)->inet_num = ntohs(inet_rsk(req)->loc_port);
  509. inet_sk(newsk)->inet_sport = inet_rsk(req)->loc_port;
  510. newsk->sk_write_space = sk_stream_write_space;
  511. newicsk->icsk_retransmits = 0;
  512. newicsk->icsk_backoff = 0;
  513. newicsk->icsk_probes_out = 0;
  514. /* Deinitialize accept_queue to trap illegal accesses. */
  515. memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
  516. security_inet_csk_clone(newsk, req);
  517. }
  518. return newsk;
  519. }
  520. EXPORT_SYMBOL_GPL(inet_csk_clone);
  521. /*
  522. * At this point, there should be no process reference to this
  523. * socket, and thus no user references at all. Therefore we
  524. * can assume the socket waitqueue is inactive and nobody will
  525. * try to jump onto it.
  526. */
  527. void inet_csk_destroy_sock(struct sock *sk)
  528. {
  529. WARN_ON(sk->sk_state != TCP_CLOSE);
  530. WARN_ON(!sock_flag(sk, SOCK_DEAD));
  531. /* It cannot be in hash table! */
  532. WARN_ON(!sk_unhashed(sk));
  533. /* If it has not 0 inet_sk(sk)->inet_num, it must be bound */
  534. WARN_ON(inet_sk(sk)->inet_num && !inet_csk(sk)->icsk_bind_hash);
  535. sk->sk_prot->destroy(sk);
  536. sk_stream_kill_queues(sk);
  537. xfrm_sk_free_policy(sk);
  538. sk_refcnt_debug_release(sk);
  539. percpu_counter_dec(sk->sk_prot->orphan_count);
  540. sock_put(sk);
  541. }
  542. EXPORT_SYMBOL(inet_csk_destroy_sock);
  543. int inet_csk_listen_start(struct sock *sk, const int nr_table_entries)
  544. {
  545. struct inet_sock *inet = inet_sk(sk);
  546. struct inet_connection_sock *icsk = inet_csk(sk);
  547. int rc = reqsk_queue_alloc(&icsk->icsk_accept_queue, nr_table_entries);
  548. if (rc != 0)
  549. return rc;
  550. sk->sk_max_ack_backlog = 0;
  551. sk->sk_ack_backlog = 0;
  552. inet_csk_delack_init(sk);
  553. /* There is race window here: we announce ourselves listening,
  554. * but this transition is still not validated by get_port().
  555. * It is OK, because this socket enters to hash table only
  556. * after validation is complete.
  557. */
  558. sk->sk_state = TCP_LISTEN;
  559. if (!sk->sk_prot->get_port(sk, inet->inet_num)) {
  560. inet->inet_sport = htons(inet->inet_num);
  561. sk_dst_reset(sk);
  562. sk->sk_prot->hash(sk);
  563. return 0;
  564. }
  565. sk->sk_state = TCP_CLOSE;
  566. __reqsk_queue_destroy(&icsk->icsk_accept_queue);
  567. return -EADDRINUSE;
  568. }
  569. EXPORT_SYMBOL_GPL(inet_csk_listen_start);
  570. /*
  571. * This routine closes sockets which have been at least partially
  572. * opened, but not yet accepted.
  573. */
  574. void inet_csk_listen_stop(struct sock *sk)
  575. {
  576. struct inet_connection_sock *icsk = inet_csk(sk);
  577. struct request_sock *acc_req;
  578. struct request_sock *req;
  579. inet_csk_delete_keepalive_timer(sk);
  580. /* make all the listen_opt local to us */
  581. acc_req = reqsk_queue_yank_acceptq(&icsk->icsk_accept_queue);
  582. /* Following specs, it would be better either to send FIN
  583. * (and enter FIN-WAIT-1, it is normal close)
  584. * or to send active reset (abort).
  585. * Certainly, it is pretty dangerous while synflood, but it is
  586. * bad justification for our negligence 8)
  587. * To be honest, we are not able to make either
  588. * of the variants now. --ANK
  589. */
  590. reqsk_queue_destroy(&icsk->icsk_accept_queue);
  591. while ((req = acc_req) != NULL) {
  592. struct sock *child = req->sk;
  593. acc_req = req->dl_next;
  594. local_bh_disable();
  595. bh_lock_sock(child);
  596. WARN_ON(sock_owned_by_user(child));
  597. sock_hold(child);
  598. sk->sk_prot->disconnect(child, O_NONBLOCK);
  599. sock_orphan(child);
  600. percpu_counter_inc(sk->sk_prot->orphan_count);
  601. inet_csk_destroy_sock(child);
  602. bh_unlock_sock(child);
  603. local_bh_enable();
  604. sock_put(child);
  605. sk_acceptq_removed(sk);
  606. __reqsk_free(req);
  607. }
  608. WARN_ON(sk->sk_ack_backlog);
  609. }
  610. EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
  611. void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
  612. {
  613. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  614. const struct inet_sock *inet = inet_sk(sk);
  615. sin->sin_family = AF_INET;
  616. sin->sin_addr.s_addr = inet->inet_daddr;
  617. sin->sin_port = inet->inet_dport;
  618. }
  619. EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr);
  620. #ifdef CONFIG_COMPAT
  621. int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
  622. char __user *optval, int __user *optlen)
  623. {
  624. const struct inet_connection_sock *icsk = inet_csk(sk);
  625. if (icsk->icsk_af_ops->compat_getsockopt != NULL)
  626. return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname,
  627. optval, optlen);
  628. return icsk->icsk_af_ops->getsockopt(sk, level, optname,
  629. optval, optlen);
  630. }
  631. EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt);
  632. int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
  633. char __user *optval, unsigned int optlen)
  634. {
  635. const struct inet_connection_sock *icsk = inet_csk(sk);
  636. if (icsk->icsk_af_ops->compat_setsockopt != NULL)
  637. return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname,
  638. optval, optlen);
  639. return icsk->icsk_af_ops->setsockopt(sk, level, optname,
  640. optval, optlen);
  641. }
  642. EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt);
  643. #endif