inet_connection_sock.c 21 KB

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