inet_connection_sock.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. #include <net/tcp.h>
  25. #ifdef INET_CSK_DEBUG
  26. const char inet_csk_timer_bug_msg[] = "inet_csk BUG: unknown timer value\n";
  27. EXPORT_SYMBOL(inet_csk_timer_bug_msg);
  28. #endif
  29. void inet_get_local_port_range(struct net *net, int *low, int *high)
  30. {
  31. unsigned int seq;
  32. do {
  33. seq = read_seqbegin(&net->ipv4.ip_local_ports.lock);
  34. *low = net->ipv4.ip_local_ports.range[0];
  35. *high = net->ipv4.ip_local_ports.range[1];
  36. } while (read_seqretry(&net->ipv4.ip_local_ports.lock, seq));
  37. }
  38. EXPORT_SYMBOL(inet_get_local_port_range);
  39. int inet_csk_bind_conflict(const struct sock *sk,
  40. const struct inet_bind_bucket *tb, bool relax)
  41. {
  42. struct sock *sk2;
  43. int reuse = sk->sk_reuse;
  44. int reuseport = sk->sk_reuseport;
  45. kuid_t uid = sock_i_uid((struct sock *)sk);
  46. /*
  47. * Unlike other sk lookup places we do not check
  48. * for sk_net here, since _all_ the socks listed
  49. * in tb->owners list belong to the same net - the
  50. * one this bucket belongs to.
  51. */
  52. sk_for_each_bound(sk2, &tb->owners) {
  53. if (sk != sk2 &&
  54. !inet_v6_ipv6only(sk2) &&
  55. (!sk->sk_bound_dev_if ||
  56. !sk2->sk_bound_dev_if ||
  57. sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) {
  58. if ((!reuse || !sk2->sk_reuse ||
  59. sk2->sk_state == TCP_LISTEN) &&
  60. (!reuseport || !sk2->sk_reuseport ||
  61. (sk2->sk_state != TCP_TIME_WAIT &&
  62. !uid_eq(uid, sock_i_uid(sk2))))) {
  63. if (!sk2->sk_rcv_saddr || !sk->sk_rcv_saddr ||
  64. sk2->sk_rcv_saddr == sk->sk_rcv_saddr)
  65. break;
  66. }
  67. if (!relax && reuse && sk2->sk_reuse &&
  68. sk2->sk_state != TCP_LISTEN) {
  69. if (!sk2->sk_rcv_saddr || !sk->sk_rcv_saddr ||
  70. sk2->sk_rcv_saddr == sk->sk_rcv_saddr)
  71. break;
  72. }
  73. }
  74. }
  75. return sk2 != 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 inet_bind_bucket *tb;
  86. int ret, attempts = 5;
  87. struct net *net = sock_net(sk);
  88. int smallest_size = -1, smallest_rover;
  89. kuid_t uid = sock_i_uid(sk);
  90. int attempt_half = (sk->sk_reuse == SK_CAN_REUSE) ? 1 : 0;
  91. local_bh_disable();
  92. if (!snum) {
  93. int remaining, rover, low, high;
  94. again:
  95. inet_get_local_port_range(net, &low, &high);
  96. if (attempt_half) {
  97. int half = low + ((high - low) >> 1);
  98. if (attempt_half == 1)
  99. high = half;
  100. else
  101. low = half;
  102. }
  103. remaining = (high - low) + 1;
  104. smallest_rover = rover = prandom_u32() % remaining + low;
  105. smallest_size = -1;
  106. do {
  107. if (inet_is_local_reserved_port(net, rover))
  108. goto next_nolock;
  109. head = &hashinfo->bhash[inet_bhashfn(net, rover,
  110. hashinfo->bhash_size)];
  111. spin_lock(&head->lock);
  112. inet_bind_bucket_for_each(tb, &head->chain)
  113. if (net_eq(ib_net(tb), net) && tb->port == rover) {
  114. if (((tb->fastreuse > 0 &&
  115. sk->sk_reuse &&
  116. sk->sk_state != TCP_LISTEN) ||
  117. (tb->fastreuseport > 0 &&
  118. sk->sk_reuseport &&
  119. uid_eq(tb->fastuid, uid))) &&
  120. (tb->num_owners < smallest_size || smallest_size == -1)) {
  121. smallest_size = tb->num_owners;
  122. smallest_rover = rover;
  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. if (attempt_half == 1) {
  150. /* OK we now try the upper half of the range */
  151. attempt_half = 2;
  152. goto again;
  153. }
  154. goto fail;
  155. }
  156. /* OK, here is the one we will use. HEAD is
  157. * non-NULL and we hold it's mutex.
  158. */
  159. snum = rover;
  160. } else {
  161. have_snum:
  162. head = &hashinfo->bhash[inet_bhashfn(net, snum,
  163. hashinfo->bhash_size)];
  164. spin_lock(&head->lock);
  165. inet_bind_bucket_for_each(tb, &head->chain)
  166. if (net_eq(ib_net(tb), net) && tb->port == snum)
  167. goto tb_found;
  168. }
  169. tb = NULL;
  170. goto tb_not_found;
  171. tb_found:
  172. if (!hlist_empty(&tb->owners)) {
  173. if (sk->sk_reuse == SK_FORCE_REUSE)
  174. goto success;
  175. if (((tb->fastreuse > 0 &&
  176. sk->sk_reuse && sk->sk_state != TCP_LISTEN) ||
  177. (tb->fastreuseport > 0 &&
  178. sk->sk_reuseport && uid_eq(tb->fastuid, uid))) &&
  179. smallest_size == -1) {
  180. goto success;
  181. } else {
  182. ret = 1;
  183. if (inet_csk(sk)->icsk_af_ops->bind_conflict(sk, tb, true)) {
  184. if (((sk->sk_reuse && sk->sk_state != TCP_LISTEN) ||
  185. (tb->fastreuseport > 0 &&
  186. sk->sk_reuseport && uid_eq(tb->fastuid, uid))) &&
  187. smallest_size != -1 && --attempts >= 0) {
  188. spin_unlock(&head->lock);
  189. goto again;
  190. }
  191. goto fail_unlock;
  192. }
  193. }
  194. }
  195. tb_not_found:
  196. ret = 1;
  197. if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep,
  198. net, head, snum)) == NULL)
  199. goto fail_unlock;
  200. if (hlist_empty(&tb->owners)) {
  201. if (sk->sk_reuse && sk->sk_state != TCP_LISTEN)
  202. tb->fastreuse = 1;
  203. else
  204. tb->fastreuse = 0;
  205. if (sk->sk_reuseport) {
  206. tb->fastreuseport = 1;
  207. tb->fastuid = uid;
  208. } else
  209. tb->fastreuseport = 0;
  210. } else {
  211. if (tb->fastreuse &&
  212. (!sk->sk_reuse || sk->sk_state == TCP_LISTEN))
  213. tb->fastreuse = 0;
  214. if (tb->fastreuseport &&
  215. (!sk->sk_reuseport || !uid_eq(tb->fastuid, uid)))
  216. tb->fastreuseport = 0;
  217. }
  218. success:
  219. if (!inet_csk(sk)->icsk_bind_hash)
  220. inet_bind_hash(sk, tb, snum);
  221. WARN_ON(inet_csk(sk)->icsk_bind_hash != tb);
  222. ret = 0;
  223. fail_unlock:
  224. spin_unlock(&head->lock);
  225. fail:
  226. local_bh_enable();
  227. return ret;
  228. }
  229. EXPORT_SYMBOL_GPL(inet_csk_get_port);
  230. /*
  231. * Wait for an incoming connection, avoid race conditions. This must be called
  232. * with the socket locked.
  233. */
  234. static int inet_csk_wait_for_connect(struct sock *sk, long timeo)
  235. {
  236. struct inet_connection_sock *icsk = inet_csk(sk);
  237. DEFINE_WAIT(wait);
  238. int err;
  239. /*
  240. * True wake-one mechanism for incoming connections: only
  241. * one process gets woken up, not the 'whole herd'.
  242. * Since we do not 'race & poll' for established sockets
  243. * anymore, the common case will execute the loop only once.
  244. *
  245. * Subtle issue: "add_wait_queue_exclusive()" will be added
  246. * after any current non-exclusive waiters, and we know that
  247. * it will always _stay_ after any new non-exclusive waiters
  248. * because all non-exclusive waiters are added at the
  249. * beginning of the wait-queue. As such, it's ok to "drop"
  250. * our exclusiveness temporarily when we get woken up without
  251. * having to remove and re-insert us on the wait queue.
  252. */
  253. for (;;) {
  254. prepare_to_wait_exclusive(sk_sleep(sk), &wait,
  255. TASK_INTERRUPTIBLE);
  256. release_sock(sk);
  257. if (reqsk_queue_empty(&icsk->icsk_accept_queue))
  258. timeo = schedule_timeout(timeo);
  259. sched_annotate_sleep();
  260. lock_sock(sk);
  261. err = 0;
  262. if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
  263. break;
  264. err = -EINVAL;
  265. if (sk->sk_state != TCP_LISTEN)
  266. break;
  267. err = sock_intr_errno(timeo);
  268. if (signal_pending(current))
  269. break;
  270. err = -EAGAIN;
  271. if (!timeo)
  272. break;
  273. }
  274. finish_wait(sk_sleep(sk), &wait);
  275. return err;
  276. }
  277. /*
  278. * This will accept the next outstanding connection.
  279. */
  280. struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
  281. {
  282. struct inet_connection_sock *icsk = inet_csk(sk);
  283. struct request_sock_queue *queue = &icsk->icsk_accept_queue;
  284. struct request_sock *req;
  285. struct sock *newsk;
  286. int error;
  287. lock_sock(sk);
  288. /* We need to make sure that this socket is listening,
  289. * and that it has something pending.
  290. */
  291. error = -EINVAL;
  292. if (sk->sk_state != TCP_LISTEN)
  293. goto out_err;
  294. /* Find already established connection */
  295. if (reqsk_queue_empty(queue)) {
  296. long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  297. /* If this is a non blocking socket don't sleep */
  298. error = -EAGAIN;
  299. if (!timeo)
  300. goto out_err;
  301. error = inet_csk_wait_for_connect(sk, timeo);
  302. if (error)
  303. goto out_err;
  304. }
  305. req = reqsk_queue_remove(queue, sk);
  306. newsk = req->sk;
  307. if (sk->sk_protocol == IPPROTO_TCP &&
  308. tcp_rsk(req)->tfo_listener) {
  309. spin_lock_bh(&queue->fastopenq.lock);
  310. if (tcp_rsk(req)->tfo_listener) {
  311. /* We are still waiting for the final ACK from 3WHS
  312. * so can't free req now. Instead, we set req->sk to
  313. * NULL to signify that the child socket is taken
  314. * so reqsk_fastopen_remove() will free the req
  315. * when 3WHS finishes (or is aborted).
  316. */
  317. req->sk = NULL;
  318. req = NULL;
  319. }
  320. spin_unlock_bh(&queue->fastopenq.lock);
  321. }
  322. out:
  323. release_sock(sk);
  324. if (req)
  325. reqsk_put(req);
  326. return newsk;
  327. out_err:
  328. newsk = NULL;
  329. req = NULL;
  330. *err = error;
  331. goto out;
  332. }
  333. EXPORT_SYMBOL(inet_csk_accept);
  334. /*
  335. * Using different timers for retransmit, delayed acks and probes
  336. * We may wish use just one timer maintaining a list of expire jiffies
  337. * to optimize.
  338. */
  339. void inet_csk_init_xmit_timers(struct sock *sk,
  340. void (*retransmit_handler)(unsigned long),
  341. void (*delack_handler)(unsigned long),
  342. void (*keepalive_handler)(unsigned long))
  343. {
  344. struct inet_connection_sock *icsk = inet_csk(sk);
  345. setup_timer(&icsk->icsk_retransmit_timer, retransmit_handler,
  346. (unsigned long)sk);
  347. setup_timer(&icsk->icsk_delack_timer, delack_handler,
  348. (unsigned long)sk);
  349. setup_timer(&sk->sk_timer, keepalive_handler, (unsigned long)sk);
  350. icsk->icsk_pending = icsk->icsk_ack.pending = 0;
  351. }
  352. EXPORT_SYMBOL(inet_csk_init_xmit_timers);
  353. void inet_csk_clear_xmit_timers(struct sock *sk)
  354. {
  355. struct inet_connection_sock *icsk = inet_csk(sk);
  356. icsk->icsk_pending = icsk->icsk_ack.pending = icsk->icsk_ack.blocked = 0;
  357. sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
  358. sk_stop_timer(sk, &icsk->icsk_delack_timer);
  359. sk_stop_timer(sk, &sk->sk_timer);
  360. }
  361. EXPORT_SYMBOL(inet_csk_clear_xmit_timers);
  362. void inet_csk_delete_keepalive_timer(struct sock *sk)
  363. {
  364. sk_stop_timer(sk, &sk->sk_timer);
  365. }
  366. EXPORT_SYMBOL(inet_csk_delete_keepalive_timer);
  367. void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len)
  368. {
  369. sk_reset_timer(sk, &sk->sk_timer, jiffies + len);
  370. }
  371. EXPORT_SYMBOL(inet_csk_reset_keepalive_timer);
  372. struct dst_entry *inet_csk_route_req(const struct sock *sk,
  373. struct flowi4 *fl4,
  374. const struct request_sock *req)
  375. {
  376. const struct inet_request_sock *ireq = inet_rsk(req);
  377. struct net *net = read_pnet(&ireq->ireq_net);
  378. struct ip_options_rcu *opt;
  379. struct rtable *rt;
  380. opt = ireq_opt_deref(ireq);
  381. flowi4_init_output(fl4, ireq->ir_iif, ireq->ir_mark,
  382. RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
  383. sk->sk_protocol, inet_sk_flowi_flags(sk),
  384. (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr,
  385. ireq->ir_loc_addr, ireq->ir_rmt_port,
  386. htons(ireq->ir_num));
  387. security_req_classify_flow(req, flowi4_to_flowi(fl4));
  388. rt = ip_route_output_flow(net, fl4, sk);
  389. if (IS_ERR(rt))
  390. goto no_route;
  391. if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
  392. goto route_err;
  393. return &rt->dst;
  394. route_err:
  395. ip_rt_put(rt);
  396. no_route:
  397. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  398. return NULL;
  399. }
  400. EXPORT_SYMBOL_GPL(inet_csk_route_req);
  401. struct dst_entry *inet_csk_route_child_sock(const struct sock *sk,
  402. struct sock *newsk,
  403. const struct request_sock *req)
  404. {
  405. const struct inet_request_sock *ireq = inet_rsk(req);
  406. struct net *net = read_pnet(&ireq->ireq_net);
  407. struct inet_sock *newinet = inet_sk(newsk);
  408. struct ip_options_rcu *opt;
  409. struct flowi4 *fl4;
  410. struct rtable *rt;
  411. opt = rcu_dereference(ireq->ireq_opt);
  412. fl4 = &newinet->cork.fl.u.ip4;
  413. flowi4_init_output(fl4, ireq->ir_iif, ireq->ir_mark,
  414. RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
  415. sk->sk_protocol, inet_sk_flowi_flags(sk),
  416. (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr,
  417. ireq->ir_loc_addr, ireq->ir_rmt_port,
  418. htons(ireq->ir_num));
  419. security_req_classify_flow(req, flowi4_to_flowi(fl4));
  420. rt = ip_route_output_flow(net, fl4, sk);
  421. if (IS_ERR(rt))
  422. goto no_route;
  423. if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
  424. goto route_err;
  425. return &rt->dst;
  426. route_err:
  427. ip_rt_put(rt);
  428. no_route:
  429. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  430. return NULL;
  431. }
  432. EXPORT_SYMBOL_GPL(inet_csk_route_child_sock);
  433. #if IS_ENABLED(CONFIG_IPV6)
  434. #define AF_INET_FAMILY(fam) ((fam) == AF_INET)
  435. #else
  436. #define AF_INET_FAMILY(fam) true
  437. #endif
  438. /* Only thing we need from tcp.h */
  439. extern int sysctl_tcp_synack_retries;
  440. /* Decide when to expire the request and when to resend SYN-ACK */
  441. static inline void syn_ack_recalc(struct request_sock *req, const int thresh,
  442. const int max_retries,
  443. const u8 rskq_defer_accept,
  444. int *expire, int *resend)
  445. {
  446. if (!rskq_defer_accept) {
  447. *expire = req->num_timeout >= thresh;
  448. *resend = 1;
  449. return;
  450. }
  451. *expire = req->num_timeout >= thresh &&
  452. (!inet_rsk(req)->acked || req->num_timeout >= max_retries);
  453. /*
  454. * Do not resend while waiting for data after ACK,
  455. * start to resend on end of deferring period to give
  456. * last chance for data or ACK to create established socket.
  457. */
  458. *resend = !inet_rsk(req)->acked ||
  459. req->num_timeout >= rskq_defer_accept - 1;
  460. }
  461. int inet_rtx_syn_ack(const struct sock *parent, struct request_sock *req)
  462. {
  463. int err = req->rsk_ops->rtx_syn_ack(parent, req);
  464. if (!err)
  465. req->num_retrans++;
  466. return err;
  467. }
  468. EXPORT_SYMBOL(inet_rtx_syn_ack);
  469. /* return true if req was found in the ehash table */
  470. static bool reqsk_queue_unlink(struct request_sock_queue *queue,
  471. struct request_sock *req)
  472. {
  473. struct inet_hashinfo *hashinfo = req_to_sk(req)->sk_prot->h.hashinfo;
  474. bool found = false;
  475. if (sk_hashed(req_to_sk(req))) {
  476. spinlock_t *lock = inet_ehash_lockp(hashinfo, req->rsk_hash);
  477. spin_lock(lock);
  478. found = __sk_nulls_del_node_init_rcu(req_to_sk(req));
  479. spin_unlock(lock);
  480. }
  481. if (timer_pending(&req->rsk_timer) && del_timer_sync(&req->rsk_timer))
  482. reqsk_put(req);
  483. return found;
  484. }
  485. void inet_csk_reqsk_queue_drop(struct sock *sk, struct request_sock *req)
  486. {
  487. if (reqsk_queue_unlink(&inet_csk(sk)->icsk_accept_queue, req)) {
  488. reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req);
  489. reqsk_put(req);
  490. }
  491. }
  492. EXPORT_SYMBOL(inet_csk_reqsk_queue_drop);
  493. void inet_csk_reqsk_queue_drop_and_put(struct sock *sk, struct request_sock *req)
  494. {
  495. inet_csk_reqsk_queue_drop(sk, req);
  496. reqsk_put(req);
  497. }
  498. EXPORT_SYMBOL(inet_csk_reqsk_queue_drop_and_put);
  499. static void reqsk_timer_handler(unsigned long data)
  500. {
  501. struct request_sock *req = (struct request_sock *)data;
  502. struct sock *sk_listener = req->rsk_listener;
  503. struct inet_connection_sock *icsk = inet_csk(sk_listener);
  504. struct request_sock_queue *queue = &icsk->icsk_accept_queue;
  505. int qlen, expire = 0, resend = 0;
  506. int max_retries, thresh;
  507. u8 defer_accept;
  508. if (sk_state_load(sk_listener) != TCP_LISTEN)
  509. goto drop;
  510. max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
  511. thresh = max_retries;
  512. /* Normally all the openreqs are young and become mature
  513. * (i.e. converted to established socket) for first timeout.
  514. * If synack was not acknowledged for 1 second, it means
  515. * one of the following things: synack was lost, ack was lost,
  516. * rtt is high or nobody planned to ack (i.e. synflood).
  517. * When server is a bit loaded, queue is populated with old
  518. * open requests, reducing effective size of queue.
  519. * When server is well loaded, queue size reduces to zero
  520. * after several minutes of work. It is not synflood,
  521. * it is normal operation. The solution is pruning
  522. * too old entries overriding normal timeout, when
  523. * situation becomes dangerous.
  524. *
  525. * Essentially, we reserve half of room for young
  526. * embrions; and abort old ones without pity, if old
  527. * ones are about to clog our table.
  528. */
  529. qlen = reqsk_queue_len(queue);
  530. if ((qlen << 1) > max(8U, sk_listener->sk_max_ack_backlog)) {
  531. int young = reqsk_queue_len_young(queue) << 1;
  532. while (thresh > 2) {
  533. if (qlen < young)
  534. break;
  535. thresh--;
  536. young <<= 1;
  537. }
  538. }
  539. defer_accept = READ_ONCE(queue->rskq_defer_accept);
  540. if (defer_accept)
  541. max_retries = defer_accept;
  542. syn_ack_recalc(req, thresh, max_retries, defer_accept,
  543. &expire, &resend);
  544. req->rsk_ops->syn_ack_timeout(req);
  545. if (!expire &&
  546. (!resend ||
  547. !inet_rtx_syn_ack(sk_listener, req) ||
  548. inet_rsk(req)->acked)) {
  549. unsigned long timeo;
  550. if (req->num_timeout++ == 0)
  551. atomic_dec(&queue->young);
  552. timeo = min(TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
  553. mod_timer_pinned(&req->rsk_timer, jiffies + timeo);
  554. return;
  555. }
  556. drop:
  557. inet_csk_reqsk_queue_drop_and_put(sk_listener, req);
  558. }
  559. static void reqsk_queue_hash_req(struct request_sock *req,
  560. unsigned long timeout)
  561. {
  562. req->num_retrans = 0;
  563. req->num_timeout = 0;
  564. req->sk = NULL;
  565. setup_timer(&req->rsk_timer, reqsk_timer_handler, (unsigned long)req);
  566. mod_timer_pinned(&req->rsk_timer, jiffies + timeout);
  567. inet_ehash_insert(req_to_sk(req), NULL);
  568. /* before letting lookups find us, make sure all req fields
  569. * are committed to memory and refcnt initialized.
  570. */
  571. smp_wmb();
  572. atomic_set(&req->rsk_refcnt, 2 + 1);
  573. }
  574. void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
  575. unsigned long timeout)
  576. {
  577. reqsk_queue_hash_req(req, timeout);
  578. inet_csk_reqsk_queue_added(sk);
  579. }
  580. EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);
  581. /**
  582. * inet_csk_clone_lock - clone an inet socket, and lock its clone
  583. * @sk: the socket to clone
  584. * @req: request_sock
  585. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  586. *
  587. * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
  588. */
  589. struct sock *inet_csk_clone_lock(const struct sock *sk,
  590. const struct request_sock *req,
  591. const gfp_t priority)
  592. {
  593. struct sock *newsk = sk_clone_lock(sk, priority);
  594. if (newsk) {
  595. struct inet_connection_sock *newicsk = inet_csk(newsk);
  596. newsk->sk_state = TCP_SYN_RECV;
  597. newicsk->icsk_bind_hash = NULL;
  598. inet_sk(newsk)->inet_dport = inet_rsk(req)->ir_rmt_port;
  599. inet_sk(newsk)->inet_num = inet_rsk(req)->ir_num;
  600. inet_sk(newsk)->inet_sport = htons(inet_rsk(req)->ir_num);
  601. newsk->sk_write_space = sk_stream_write_space;
  602. inet_sk(newsk)->mc_list = NULL;
  603. newsk->sk_mark = inet_rsk(req)->ir_mark;
  604. atomic64_set(&newsk->sk_cookie,
  605. atomic64_read(&inet_rsk(req)->ir_cookie));
  606. newicsk->icsk_retransmits = 0;
  607. newicsk->icsk_backoff = 0;
  608. newicsk->icsk_probes_out = 0;
  609. /* Deinitialize accept_queue to trap illegal accesses. */
  610. memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
  611. security_inet_csk_clone(newsk, req);
  612. }
  613. return newsk;
  614. }
  615. EXPORT_SYMBOL_GPL(inet_csk_clone_lock);
  616. /*
  617. * At this point, there should be no process reference to this
  618. * socket, and thus no user references at all. Therefore we
  619. * can assume the socket waitqueue is inactive and nobody will
  620. * try to jump onto it.
  621. */
  622. void inet_csk_destroy_sock(struct sock *sk)
  623. {
  624. WARN_ON(sk->sk_state != TCP_CLOSE);
  625. WARN_ON(!sock_flag(sk, SOCK_DEAD));
  626. /* It cannot be in hash table! */
  627. WARN_ON(!sk_unhashed(sk));
  628. /* If it has not 0 inet_sk(sk)->inet_num, it must be bound */
  629. WARN_ON(inet_sk(sk)->inet_num && !inet_csk(sk)->icsk_bind_hash);
  630. sk->sk_prot->destroy(sk);
  631. sk_stream_kill_queues(sk);
  632. xfrm_sk_free_policy(sk);
  633. sk_refcnt_debug_release(sk);
  634. percpu_counter_dec(sk->sk_prot->orphan_count);
  635. sock_put(sk);
  636. }
  637. EXPORT_SYMBOL(inet_csk_destroy_sock);
  638. /* This function allows to force a closure of a socket after the call to
  639. * tcp/dccp_create_openreq_child().
  640. */
  641. void inet_csk_prepare_forced_close(struct sock *sk)
  642. __releases(&sk->sk_lock.slock)
  643. {
  644. /* sk_clone_lock locked the socket and set refcnt to 2 */
  645. bh_unlock_sock(sk);
  646. sock_put(sk);
  647. /* The below has to be done to allow calling inet_csk_destroy_sock */
  648. sock_set_flag(sk, SOCK_DEAD);
  649. percpu_counter_inc(sk->sk_prot->orphan_count);
  650. inet_sk(sk)->inet_num = 0;
  651. }
  652. EXPORT_SYMBOL(inet_csk_prepare_forced_close);
  653. int inet_csk_listen_start(struct sock *sk, int backlog)
  654. {
  655. struct inet_connection_sock *icsk = inet_csk(sk);
  656. struct inet_sock *inet = inet_sk(sk);
  657. reqsk_queue_alloc(&icsk->icsk_accept_queue);
  658. sk->sk_max_ack_backlog = backlog;
  659. sk->sk_ack_backlog = 0;
  660. inet_csk_delack_init(sk);
  661. /* There is race window here: we announce ourselves listening,
  662. * but this transition is still not validated by get_port().
  663. * It is OK, because this socket enters to hash table only
  664. * after validation is complete.
  665. */
  666. sk_state_store(sk, TCP_LISTEN);
  667. if (!sk->sk_prot->get_port(sk, inet->inet_num)) {
  668. inet->inet_sport = htons(inet->inet_num);
  669. sk_dst_reset(sk);
  670. sk->sk_prot->hash(sk);
  671. return 0;
  672. }
  673. sk->sk_state = TCP_CLOSE;
  674. return -EADDRINUSE;
  675. }
  676. EXPORT_SYMBOL_GPL(inet_csk_listen_start);
  677. static void inet_child_forget(struct sock *sk, struct request_sock *req,
  678. struct sock *child)
  679. {
  680. sk->sk_prot->disconnect(child, O_NONBLOCK);
  681. sock_orphan(child);
  682. percpu_counter_inc(sk->sk_prot->orphan_count);
  683. if (sk->sk_protocol == IPPROTO_TCP && tcp_rsk(req)->tfo_listener) {
  684. BUG_ON(tcp_sk(child)->fastopen_rsk != req);
  685. BUG_ON(sk != req->rsk_listener);
  686. /* Paranoid, to prevent race condition if
  687. * an inbound pkt destined for child is
  688. * blocked by sock lock in tcp_v4_rcv().
  689. * Also to satisfy an assertion in
  690. * tcp_v4_destroy_sock().
  691. */
  692. tcp_sk(child)->fastopen_rsk = NULL;
  693. }
  694. inet_csk_destroy_sock(child);
  695. }
  696. struct sock *inet_csk_reqsk_queue_add(struct sock *sk,
  697. struct request_sock *req,
  698. struct sock *child)
  699. {
  700. struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
  701. spin_lock(&queue->rskq_lock);
  702. if (unlikely(sk->sk_state != TCP_LISTEN)) {
  703. inet_child_forget(sk, req, child);
  704. child = NULL;
  705. } else {
  706. req->sk = child;
  707. req->dl_next = NULL;
  708. if (queue->rskq_accept_head == NULL)
  709. queue->rskq_accept_head = req;
  710. else
  711. queue->rskq_accept_tail->dl_next = req;
  712. queue->rskq_accept_tail = req;
  713. sk_acceptq_added(sk);
  714. }
  715. spin_unlock(&queue->rskq_lock);
  716. return child;
  717. }
  718. EXPORT_SYMBOL(inet_csk_reqsk_queue_add);
  719. struct sock *inet_csk_complete_hashdance(struct sock *sk, struct sock *child,
  720. struct request_sock *req, bool own_req)
  721. {
  722. if (own_req) {
  723. inet_csk_reqsk_queue_drop(sk, req);
  724. reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req);
  725. if (inet_csk_reqsk_queue_add(sk, req, child))
  726. return child;
  727. }
  728. /* Too bad, another child took ownership of the request, undo. */
  729. bh_unlock_sock(child);
  730. sock_put(child);
  731. return NULL;
  732. }
  733. EXPORT_SYMBOL(inet_csk_complete_hashdance);
  734. /*
  735. * This routine closes sockets which have been at least partially
  736. * opened, but not yet accepted.
  737. */
  738. void inet_csk_listen_stop(struct sock *sk)
  739. {
  740. struct inet_connection_sock *icsk = inet_csk(sk);
  741. struct request_sock_queue *queue = &icsk->icsk_accept_queue;
  742. struct request_sock *next, *req;
  743. /* Following specs, it would be better either to send FIN
  744. * (and enter FIN-WAIT-1, it is normal close)
  745. * or to send active reset (abort).
  746. * Certainly, it is pretty dangerous while synflood, but it is
  747. * bad justification for our negligence 8)
  748. * To be honest, we are not able to make either
  749. * of the variants now. --ANK
  750. */
  751. while ((req = reqsk_queue_remove(queue, sk)) != NULL) {
  752. struct sock *child = req->sk;
  753. local_bh_disable();
  754. bh_lock_sock(child);
  755. WARN_ON(sock_owned_by_user(child));
  756. sock_hold(child);
  757. inet_child_forget(sk, req, child);
  758. reqsk_put(req);
  759. bh_unlock_sock(child);
  760. local_bh_enable();
  761. sock_put(child);
  762. cond_resched();
  763. }
  764. if (queue->fastopenq.rskq_rst_head) {
  765. /* Free all the reqs queued in rskq_rst_head. */
  766. spin_lock_bh(&queue->fastopenq.lock);
  767. req = queue->fastopenq.rskq_rst_head;
  768. queue->fastopenq.rskq_rst_head = NULL;
  769. spin_unlock_bh(&queue->fastopenq.lock);
  770. while (req != NULL) {
  771. next = req->dl_next;
  772. reqsk_put(req);
  773. req = next;
  774. }
  775. }
  776. WARN_ON_ONCE(sk->sk_ack_backlog);
  777. }
  778. EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
  779. void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
  780. {
  781. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  782. const struct inet_sock *inet = inet_sk(sk);
  783. sin->sin_family = AF_INET;
  784. sin->sin_addr.s_addr = inet->inet_daddr;
  785. sin->sin_port = inet->inet_dport;
  786. }
  787. EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr);
  788. #ifdef CONFIG_COMPAT
  789. int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
  790. char __user *optval, int __user *optlen)
  791. {
  792. const struct inet_connection_sock *icsk = inet_csk(sk);
  793. if (icsk->icsk_af_ops->compat_getsockopt)
  794. return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname,
  795. optval, optlen);
  796. return icsk->icsk_af_ops->getsockopt(sk, level, optname,
  797. optval, optlen);
  798. }
  799. EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt);
  800. int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
  801. char __user *optval, unsigned int optlen)
  802. {
  803. const struct inet_connection_sock *icsk = inet_csk(sk);
  804. if (icsk->icsk_af_ops->compat_setsockopt)
  805. return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname,
  806. optval, optlen);
  807. return icsk->icsk_af_ops->setsockopt(sk, level, optname,
  808. optval, optlen);
  809. }
  810. EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt);
  811. #endif
  812. static struct dst_entry *inet_csk_rebuild_route(struct sock *sk, struct flowi *fl)
  813. {
  814. const struct inet_sock *inet = inet_sk(sk);
  815. const struct ip_options_rcu *inet_opt;
  816. __be32 daddr = inet->inet_daddr;
  817. struct flowi4 *fl4;
  818. struct rtable *rt;
  819. rcu_read_lock();
  820. inet_opt = rcu_dereference(inet->inet_opt);
  821. if (inet_opt && inet_opt->opt.srr)
  822. daddr = inet_opt->opt.faddr;
  823. fl4 = &fl->u.ip4;
  824. rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr,
  825. inet->inet_saddr, inet->inet_dport,
  826. inet->inet_sport, sk->sk_protocol,
  827. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if);
  828. if (IS_ERR(rt))
  829. rt = NULL;
  830. if (rt)
  831. sk_setup_caps(sk, &rt->dst);
  832. rcu_read_unlock();
  833. return &rt->dst;
  834. }
  835. struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu)
  836. {
  837. struct dst_entry *dst = __sk_dst_check(sk, 0);
  838. struct inet_sock *inet = inet_sk(sk);
  839. if (!dst) {
  840. dst = inet_csk_rebuild_route(sk, &inet->cork.fl);
  841. if (!dst)
  842. goto out;
  843. }
  844. dst->ops->update_pmtu(dst, sk, NULL, mtu);
  845. dst = __sk_dst_check(sk, 0);
  846. if (!dst)
  847. dst = inet_csk_rebuild_route(sk, &inet->cork.fl);
  848. out:
  849. return dst;
  850. }
  851. EXPORT_SYMBOL_GPL(inet_csk_update_pmtu);