inet_connection_sock.c 19 KB

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