inet_connection_sock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. .flags = inet_sk_flowi_flags(sk),
  299. .uli_u = { .ports =
  300. { .sport = inet_sk(sk)->sport,
  301. .dport = ireq->rmt_port } } };
  302. struct net *net = sock_net(sk);
  303. security_req_classify_flow(req, &fl);
  304. if (ip_route_output_flow(net, &rt, &fl, sk, 0)) {
  305. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  306. return NULL;
  307. }
  308. if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway) {
  309. ip_rt_put(rt);
  310. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  311. return NULL;
  312. }
  313. return &rt->u.dst;
  314. }
  315. EXPORT_SYMBOL_GPL(inet_csk_route_req);
  316. static inline u32 inet_synq_hash(const __be32 raddr, const __be16 rport,
  317. const u32 rnd, const u32 synq_hsize)
  318. {
  319. return jhash_2words((__force u32)raddr, (__force u32)rport, rnd) & (synq_hsize - 1);
  320. }
  321. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  322. #define AF_INET_FAMILY(fam) ((fam) == AF_INET)
  323. #else
  324. #define AF_INET_FAMILY(fam) 1
  325. #endif
  326. struct request_sock *inet_csk_search_req(const struct sock *sk,
  327. struct request_sock ***prevp,
  328. const __be16 rport, const __be32 raddr,
  329. const __be32 laddr)
  330. {
  331. const struct inet_connection_sock *icsk = inet_csk(sk);
  332. struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
  333. struct request_sock *req, **prev;
  334. for (prev = &lopt->syn_table[inet_synq_hash(raddr, rport, lopt->hash_rnd,
  335. lopt->nr_table_entries)];
  336. (req = *prev) != NULL;
  337. prev = &req->dl_next) {
  338. const struct inet_request_sock *ireq = inet_rsk(req);
  339. if (ireq->rmt_port == rport &&
  340. ireq->rmt_addr == raddr &&
  341. ireq->loc_addr == laddr &&
  342. AF_INET_FAMILY(req->rsk_ops->family)) {
  343. WARN_ON(req->sk);
  344. *prevp = prev;
  345. break;
  346. }
  347. }
  348. return req;
  349. }
  350. EXPORT_SYMBOL_GPL(inet_csk_search_req);
  351. void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
  352. unsigned long timeout)
  353. {
  354. struct inet_connection_sock *icsk = inet_csk(sk);
  355. struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
  356. const u32 h = inet_synq_hash(inet_rsk(req)->rmt_addr, inet_rsk(req)->rmt_port,
  357. lopt->hash_rnd, lopt->nr_table_entries);
  358. reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
  359. inet_csk_reqsk_queue_added(sk, timeout);
  360. }
  361. /* Only thing we need from tcp.h */
  362. extern int sysctl_tcp_synack_retries;
  363. EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);
  364. void inet_csk_reqsk_queue_prune(struct sock *parent,
  365. const unsigned long interval,
  366. const unsigned long timeout,
  367. const unsigned long max_rto)
  368. {
  369. struct inet_connection_sock *icsk = inet_csk(parent);
  370. struct request_sock_queue *queue = &icsk->icsk_accept_queue;
  371. struct listen_sock *lopt = queue->listen_opt;
  372. int max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
  373. int thresh = max_retries;
  374. unsigned long now = jiffies;
  375. struct request_sock **reqp, *req;
  376. int i, budget;
  377. if (lopt == NULL || lopt->qlen == 0)
  378. return;
  379. /* Normally all the openreqs are young and become mature
  380. * (i.e. converted to established socket) for first timeout.
  381. * If synack was not acknowledged for 3 seconds, it means
  382. * one of the following things: synack was lost, ack was lost,
  383. * rtt is high or nobody planned to ack (i.e. synflood).
  384. * When server is a bit loaded, queue is populated with old
  385. * open requests, reducing effective size of queue.
  386. * When server is well loaded, queue size reduces to zero
  387. * after several minutes of work. It is not synflood,
  388. * it is normal operation. The solution is pruning
  389. * too old entries overriding normal timeout, when
  390. * situation becomes dangerous.
  391. *
  392. * Essentially, we reserve half of room for young
  393. * embrions; and abort old ones without pity, if old
  394. * ones are about to clog our table.
  395. */
  396. if (lopt->qlen>>(lopt->max_qlen_log-1)) {
  397. int young = (lopt->qlen_young<<1);
  398. while (thresh > 2) {
  399. if (lopt->qlen < young)
  400. break;
  401. thresh--;
  402. young <<= 1;
  403. }
  404. }
  405. if (queue->rskq_defer_accept)
  406. max_retries = queue->rskq_defer_accept;
  407. budget = 2 * (lopt->nr_table_entries / (timeout / interval));
  408. i = lopt->clock_hand;
  409. do {
  410. reqp=&lopt->syn_table[i];
  411. while ((req = *reqp) != NULL) {
  412. if (time_after_eq(now, req->expires)) {
  413. if ((req->retrans < thresh ||
  414. (inet_rsk(req)->acked && req->retrans < max_retries))
  415. && !req->rsk_ops->rtx_syn_ack(parent, req)) {
  416. unsigned long timeo;
  417. if (req->retrans++ == 0)
  418. lopt->qlen_young--;
  419. timeo = min((timeout << req->retrans), max_rto);
  420. req->expires = now + timeo;
  421. reqp = &req->dl_next;
  422. continue;
  423. }
  424. /* Drop this request */
  425. inet_csk_reqsk_queue_unlink(parent, req, reqp);
  426. reqsk_queue_removed(queue, req);
  427. reqsk_free(req);
  428. continue;
  429. }
  430. reqp = &req->dl_next;
  431. }
  432. i = (i + 1) & (lopt->nr_table_entries - 1);
  433. } while (--budget > 0);
  434. lopt->clock_hand = i;
  435. if (lopt->qlen)
  436. inet_csk_reset_keepalive_timer(parent, interval);
  437. }
  438. EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune);
  439. struct sock *inet_csk_clone(struct sock *sk, const struct request_sock *req,
  440. const gfp_t priority)
  441. {
  442. struct sock *newsk = sk_clone(sk, priority);
  443. if (newsk != NULL) {
  444. struct inet_connection_sock *newicsk = inet_csk(newsk);
  445. newsk->sk_state = TCP_SYN_RECV;
  446. newicsk->icsk_bind_hash = NULL;
  447. inet_sk(newsk)->dport = inet_rsk(req)->rmt_port;
  448. inet_sk(newsk)->num = ntohs(inet_rsk(req)->loc_port);
  449. inet_sk(newsk)->sport = inet_rsk(req)->loc_port;
  450. newsk->sk_write_space = sk_stream_write_space;
  451. newicsk->icsk_retransmits = 0;
  452. newicsk->icsk_backoff = 0;
  453. newicsk->icsk_probes_out = 0;
  454. /* Deinitialize accept_queue to trap illegal accesses. */
  455. memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
  456. security_inet_csk_clone(newsk, req);
  457. }
  458. return newsk;
  459. }
  460. EXPORT_SYMBOL_GPL(inet_csk_clone);
  461. /*
  462. * At this point, there should be no process reference to this
  463. * socket, and thus no user references at all. Therefore we
  464. * can assume the socket waitqueue is inactive and nobody will
  465. * try to jump onto it.
  466. */
  467. void inet_csk_destroy_sock(struct sock *sk)
  468. {
  469. WARN_ON(sk->sk_state != TCP_CLOSE);
  470. WARN_ON(!sock_flag(sk, SOCK_DEAD));
  471. /* It cannot be in hash table! */
  472. WARN_ON(!sk_unhashed(sk));
  473. /* If it has not 0 inet_sk(sk)->num, it must be bound */
  474. WARN_ON(inet_sk(sk)->num && !inet_csk(sk)->icsk_bind_hash);
  475. sk->sk_prot->destroy(sk);
  476. sk_stream_kill_queues(sk);
  477. xfrm_sk_free_policy(sk);
  478. sk_refcnt_debug_release(sk);
  479. atomic_dec(sk->sk_prot->orphan_count);
  480. sock_put(sk);
  481. }
  482. EXPORT_SYMBOL(inet_csk_destroy_sock);
  483. int inet_csk_listen_start(struct sock *sk, const int nr_table_entries)
  484. {
  485. struct inet_sock *inet = inet_sk(sk);
  486. struct inet_connection_sock *icsk = inet_csk(sk);
  487. int rc = reqsk_queue_alloc(&icsk->icsk_accept_queue, nr_table_entries);
  488. if (rc != 0)
  489. return rc;
  490. sk->sk_max_ack_backlog = 0;
  491. sk->sk_ack_backlog = 0;
  492. inet_csk_delack_init(sk);
  493. /* There is race window here: we announce ourselves listening,
  494. * but this transition is still not validated by get_port().
  495. * It is OK, because this socket enters to hash table only
  496. * after validation is complete.
  497. */
  498. sk->sk_state = TCP_LISTEN;
  499. if (!sk->sk_prot->get_port(sk, inet->num)) {
  500. inet->sport = htons(inet->num);
  501. sk_dst_reset(sk);
  502. sk->sk_prot->hash(sk);
  503. return 0;
  504. }
  505. sk->sk_state = TCP_CLOSE;
  506. __reqsk_queue_destroy(&icsk->icsk_accept_queue);
  507. return -EADDRINUSE;
  508. }
  509. EXPORT_SYMBOL_GPL(inet_csk_listen_start);
  510. /*
  511. * This routine closes sockets which have been at least partially
  512. * opened, but not yet accepted.
  513. */
  514. void inet_csk_listen_stop(struct sock *sk)
  515. {
  516. struct inet_connection_sock *icsk = inet_csk(sk);
  517. struct request_sock *acc_req;
  518. struct request_sock *req;
  519. inet_csk_delete_keepalive_timer(sk);
  520. /* make all the listen_opt local to us */
  521. acc_req = reqsk_queue_yank_acceptq(&icsk->icsk_accept_queue);
  522. /* Following specs, it would be better either to send FIN
  523. * (and enter FIN-WAIT-1, it is normal close)
  524. * or to send active reset (abort).
  525. * Certainly, it is pretty dangerous while synflood, but it is
  526. * bad justification for our negligence 8)
  527. * To be honest, we are not able to make either
  528. * of the variants now. --ANK
  529. */
  530. reqsk_queue_destroy(&icsk->icsk_accept_queue);
  531. while ((req = acc_req) != NULL) {
  532. struct sock *child = req->sk;
  533. acc_req = req->dl_next;
  534. local_bh_disable();
  535. bh_lock_sock(child);
  536. WARN_ON(sock_owned_by_user(child));
  537. sock_hold(child);
  538. sk->sk_prot->disconnect(child, O_NONBLOCK);
  539. sock_orphan(child);
  540. atomic_inc(sk->sk_prot->orphan_count);
  541. inet_csk_destroy_sock(child);
  542. bh_unlock_sock(child);
  543. local_bh_enable();
  544. sock_put(child);
  545. sk_acceptq_removed(sk);
  546. __reqsk_free(req);
  547. }
  548. WARN_ON(sk->sk_ack_backlog);
  549. }
  550. EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
  551. void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
  552. {
  553. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  554. const struct inet_sock *inet = inet_sk(sk);
  555. sin->sin_family = AF_INET;
  556. sin->sin_addr.s_addr = inet->daddr;
  557. sin->sin_port = inet->dport;
  558. }
  559. EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr);
  560. #ifdef CONFIG_COMPAT
  561. int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
  562. char __user *optval, int __user *optlen)
  563. {
  564. const struct inet_connection_sock *icsk = inet_csk(sk);
  565. if (icsk->icsk_af_ops->compat_getsockopt != NULL)
  566. return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname,
  567. optval, optlen);
  568. return icsk->icsk_af_ops->getsockopt(sk, level, optname,
  569. optval, optlen);
  570. }
  571. EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt);
  572. int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
  573. char __user *optval, int optlen)
  574. {
  575. const struct inet_connection_sock *icsk = inet_csk(sk);
  576. if (icsk->icsk_af_ops->compat_setsockopt != NULL)
  577. return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname,
  578. optval, optlen);
  579. return icsk->icsk_af_ops->setsockopt(sk, level, optname,
  580. optval, optlen);
  581. }
  582. EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt);
  583. #endif