inet_connection_sock.c 19 KB

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