inet_connection_sock.c 22 KB

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