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