inet_connection_sock.c 25 KB

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