inet_connection_sock.c 19 KB

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