inet_connection_sock.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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 = ireq->opt;
  379. struct rtable *rt;
  380. flowi4_init_output(fl4, ireq->ir_iif, ireq->ir_mark,
  381. RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
  382. sk->sk_protocol, inet_sk_flowi_flags(sk),
  383. (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr,
  384. ireq->ir_loc_addr, ireq->ir_rmt_port,
  385. htons(ireq->ir_num));
  386. security_req_classify_flow(req, flowi4_to_flowi(fl4));
  387. rt = ip_route_output_flow(net, fl4, sk);
  388. if (IS_ERR(rt))
  389. goto no_route;
  390. if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
  391. goto route_err;
  392. return &rt->dst;
  393. route_err:
  394. ip_rt_put(rt);
  395. no_route:
  396. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  397. return NULL;
  398. }
  399. EXPORT_SYMBOL_GPL(inet_csk_route_req);
  400. struct dst_entry *inet_csk_route_child_sock(const struct sock *sk,
  401. struct sock *newsk,
  402. const struct request_sock *req)
  403. {
  404. const struct inet_request_sock *ireq = inet_rsk(req);
  405. struct net *net = read_pnet(&ireq->ireq_net);
  406. struct inet_sock *newinet = inet_sk(newsk);
  407. struct ip_options_rcu *opt;
  408. struct flowi4 *fl4;
  409. struct rtable *rt;
  410. fl4 = &newinet->cork.fl.u.ip4;
  411. rcu_read_lock();
  412. opt = rcu_dereference(newinet->inet_opt);
  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. rcu_read_unlock();
  426. return &rt->dst;
  427. route_err:
  428. ip_rt_put(rt);
  429. no_route:
  430. rcu_read_unlock();
  431. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  432. return NULL;
  433. }
  434. EXPORT_SYMBOL_GPL(inet_csk_route_child_sock);
  435. #if IS_ENABLED(CONFIG_IPV6)
  436. #define AF_INET_FAMILY(fam) ((fam) == AF_INET)
  437. #else
  438. #define AF_INET_FAMILY(fam) true
  439. #endif
  440. /* Only thing we need from tcp.h */
  441. extern int sysctl_tcp_synack_retries;
  442. /* Decide when to expire the request and when to resend SYN-ACK */
  443. static inline void syn_ack_recalc(struct request_sock *req, const int thresh,
  444. const int max_retries,
  445. const u8 rskq_defer_accept,
  446. int *expire, int *resend)
  447. {
  448. if (!rskq_defer_accept) {
  449. *expire = req->num_timeout >= thresh;
  450. *resend = 1;
  451. return;
  452. }
  453. *expire = req->num_timeout >= thresh &&
  454. (!inet_rsk(req)->acked || req->num_timeout >= max_retries);
  455. /*
  456. * Do not resend while waiting for data after ACK,
  457. * start to resend on end of deferring period to give
  458. * last chance for data or ACK to create established socket.
  459. */
  460. *resend = !inet_rsk(req)->acked ||
  461. req->num_timeout >= rskq_defer_accept - 1;
  462. }
  463. int inet_rtx_syn_ack(const struct sock *parent, struct request_sock *req)
  464. {
  465. int err = req->rsk_ops->rtx_syn_ack(parent, req);
  466. if (!err)
  467. req->num_retrans++;
  468. return err;
  469. }
  470. EXPORT_SYMBOL(inet_rtx_syn_ack);
  471. /* return true if req was found in the ehash table */
  472. static bool reqsk_queue_unlink(struct request_sock_queue *queue,
  473. struct request_sock *req)
  474. {
  475. struct inet_hashinfo *hashinfo = req_to_sk(req)->sk_prot->h.hashinfo;
  476. bool found = false;
  477. if (sk_hashed(req_to_sk(req))) {
  478. spinlock_t *lock = inet_ehash_lockp(hashinfo, req->rsk_hash);
  479. spin_lock(lock);
  480. found = __sk_nulls_del_node_init_rcu(req_to_sk(req));
  481. spin_unlock(lock);
  482. }
  483. if (timer_pending(&req->rsk_timer) && del_timer_sync(&req->rsk_timer))
  484. reqsk_put(req);
  485. return found;
  486. }
  487. void inet_csk_reqsk_queue_drop(struct sock *sk, struct request_sock *req)
  488. {
  489. if (reqsk_queue_unlink(&inet_csk(sk)->icsk_accept_queue, req)) {
  490. reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req);
  491. reqsk_put(req);
  492. }
  493. }
  494. EXPORT_SYMBOL(inet_csk_reqsk_queue_drop);
  495. void inet_csk_reqsk_queue_drop_and_put(struct sock *sk, struct request_sock *req)
  496. {
  497. inet_csk_reqsk_queue_drop(sk, req);
  498. reqsk_put(req);
  499. }
  500. EXPORT_SYMBOL(inet_csk_reqsk_queue_drop_and_put);
  501. static void reqsk_timer_handler(unsigned long data)
  502. {
  503. struct request_sock *req = (struct request_sock *)data;
  504. struct sock *sk_listener = req->rsk_listener;
  505. struct inet_connection_sock *icsk = inet_csk(sk_listener);
  506. struct request_sock_queue *queue = &icsk->icsk_accept_queue;
  507. int qlen, expire = 0, resend = 0;
  508. int max_retries, thresh;
  509. u8 defer_accept;
  510. if (sk_state_load(sk_listener) != TCP_LISTEN)
  511. goto drop;
  512. max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
  513. thresh = max_retries;
  514. /* Normally all the openreqs are young and become mature
  515. * (i.e. converted to established socket) for first timeout.
  516. * If synack was not acknowledged for 1 second, it means
  517. * one of the following things: synack was lost, ack was lost,
  518. * rtt is high or nobody planned to ack (i.e. synflood).
  519. * When server is a bit loaded, queue is populated with old
  520. * open requests, reducing effective size of queue.
  521. * When server is well loaded, queue size reduces to zero
  522. * after several minutes of work. It is not synflood,
  523. * it is normal operation. The solution is pruning
  524. * too old entries overriding normal timeout, when
  525. * situation becomes dangerous.
  526. *
  527. * Essentially, we reserve half of room for young
  528. * embrions; and abort old ones without pity, if old
  529. * ones are about to clog our table.
  530. */
  531. qlen = reqsk_queue_len(queue);
  532. if ((qlen << 1) > max(8U, sk_listener->sk_max_ack_backlog)) {
  533. int young = reqsk_queue_len_young(queue) << 1;
  534. while (thresh > 2) {
  535. if (qlen < young)
  536. break;
  537. thresh--;
  538. young <<= 1;
  539. }
  540. }
  541. defer_accept = READ_ONCE(queue->rskq_defer_accept);
  542. if (defer_accept)
  543. max_retries = defer_accept;
  544. syn_ack_recalc(req, thresh, max_retries, defer_accept,
  545. &expire, &resend);
  546. req->rsk_ops->syn_ack_timeout(req);
  547. if (!expire &&
  548. (!resend ||
  549. !inet_rtx_syn_ack(sk_listener, req) ||
  550. inet_rsk(req)->acked)) {
  551. unsigned long timeo;
  552. if (req->num_timeout++ == 0)
  553. atomic_dec(&queue->young);
  554. timeo = min(TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
  555. mod_timer_pinned(&req->rsk_timer, jiffies + timeo);
  556. return;
  557. }
  558. drop:
  559. inet_csk_reqsk_queue_drop_and_put(sk_listener, req);
  560. }
  561. static void reqsk_queue_hash_req(struct request_sock *req,
  562. unsigned long timeout)
  563. {
  564. req->num_retrans = 0;
  565. req->num_timeout = 0;
  566. req->sk = NULL;
  567. setup_timer(&req->rsk_timer, reqsk_timer_handler, (unsigned long)req);
  568. mod_timer_pinned(&req->rsk_timer, jiffies + timeout);
  569. inet_ehash_insert(req_to_sk(req), NULL);
  570. /* before letting lookups find us, make sure all req fields
  571. * are committed to memory and refcnt initialized.
  572. */
  573. smp_wmb();
  574. atomic_set(&req->rsk_refcnt, 2 + 1);
  575. }
  576. void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
  577. unsigned long timeout)
  578. {
  579. reqsk_queue_hash_req(req, timeout);
  580. inet_csk_reqsk_queue_added(sk);
  581. }
  582. EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);
  583. /**
  584. * inet_csk_clone_lock - clone an inet socket, and lock its clone
  585. * @sk: the socket to clone
  586. * @req: request_sock
  587. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  588. *
  589. * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
  590. */
  591. struct sock *inet_csk_clone_lock(const struct sock *sk,
  592. const struct request_sock *req,
  593. const gfp_t priority)
  594. {
  595. struct sock *newsk = sk_clone_lock(sk, priority);
  596. if (newsk) {
  597. struct inet_connection_sock *newicsk = inet_csk(newsk);
  598. newsk->sk_state = TCP_SYN_RECV;
  599. newicsk->icsk_bind_hash = NULL;
  600. inet_sk(newsk)->inet_dport = inet_rsk(req)->ir_rmt_port;
  601. inet_sk(newsk)->inet_num = inet_rsk(req)->ir_num;
  602. inet_sk(newsk)->inet_sport = htons(inet_rsk(req)->ir_num);
  603. newsk->sk_write_space = sk_stream_write_space;
  604. newsk->sk_mark = inet_rsk(req)->ir_mark;
  605. atomic64_set(&newsk->sk_cookie,
  606. atomic64_read(&inet_rsk(req)->ir_cookie));
  607. newicsk->icsk_retransmits = 0;
  608. newicsk->icsk_backoff = 0;
  609. newicsk->icsk_probes_out = 0;
  610. /* Deinitialize accept_queue to trap illegal accesses. */
  611. memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
  612. security_inet_csk_clone(newsk, req);
  613. }
  614. return newsk;
  615. }
  616. EXPORT_SYMBOL_GPL(inet_csk_clone_lock);
  617. /*
  618. * At this point, there should be no process reference to this
  619. * socket, and thus no user references at all. Therefore we
  620. * can assume the socket waitqueue is inactive and nobody will
  621. * try to jump onto it.
  622. */
  623. void inet_csk_destroy_sock(struct sock *sk)
  624. {
  625. WARN_ON(sk->sk_state != TCP_CLOSE);
  626. WARN_ON(!sock_flag(sk, SOCK_DEAD));
  627. /* It cannot be in hash table! */
  628. WARN_ON(!sk_unhashed(sk));
  629. /* If it has not 0 inet_sk(sk)->inet_num, it must be bound */
  630. WARN_ON(inet_sk(sk)->inet_num && !inet_csk(sk)->icsk_bind_hash);
  631. sk->sk_prot->destroy(sk);
  632. sk_stream_kill_queues(sk);
  633. xfrm_sk_free_policy(sk);
  634. sk_refcnt_debug_release(sk);
  635. percpu_counter_dec(sk->sk_prot->orphan_count);
  636. sock_put(sk);
  637. }
  638. EXPORT_SYMBOL(inet_csk_destroy_sock);
  639. /* This function allows to force a closure of a socket after the call to
  640. * tcp/dccp_create_openreq_child().
  641. */
  642. void inet_csk_prepare_forced_close(struct sock *sk)
  643. __releases(&sk->sk_lock.slock)
  644. {
  645. /* sk_clone_lock locked the socket and set refcnt to 2 */
  646. bh_unlock_sock(sk);
  647. sock_put(sk);
  648. /* The below has to be done to allow calling inet_csk_destroy_sock */
  649. sock_set_flag(sk, SOCK_DEAD);
  650. percpu_counter_inc(sk->sk_prot->orphan_count);
  651. inet_sk(sk)->inet_num = 0;
  652. }
  653. EXPORT_SYMBOL(inet_csk_prepare_forced_close);
  654. int inet_csk_listen_start(struct sock *sk, int backlog)
  655. {
  656. struct inet_connection_sock *icsk = inet_csk(sk);
  657. struct inet_sock *inet = inet_sk(sk);
  658. reqsk_queue_alloc(&icsk->icsk_accept_queue);
  659. sk->sk_max_ack_backlog = backlog;
  660. sk->sk_ack_backlog = 0;
  661. inet_csk_delack_init(sk);
  662. /* There is race window here: we announce ourselves listening,
  663. * but this transition is still not validated by get_port().
  664. * It is OK, because this socket enters to hash table only
  665. * after validation is complete.
  666. */
  667. sk_state_store(sk, TCP_LISTEN);
  668. if (!sk->sk_prot->get_port(sk, inet->inet_num)) {
  669. inet->inet_sport = htons(inet->inet_num);
  670. sk_dst_reset(sk);
  671. sk->sk_prot->hash(sk);
  672. return 0;
  673. }
  674. sk->sk_state = TCP_CLOSE;
  675. return -EADDRINUSE;
  676. }
  677. EXPORT_SYMBOL_GPL(inet_csk_listen_start);
  678. static void inet_child_forget(struct sock *sk, struct request_sock *req,
  679. struct sock *child)
  680. {
  681. sk->sk_prot->disconnect(child, O_NONBLOCK);
  682. sock_orphan(child);
  683. percpu_counter_inc(sk->sk_prot->orphan_count);
  684. if (sk->sk_protocol == IPPROTO_TCP && tcp_rsk(req)->tfo_listener) {
  685. BUG_ON(tcp_sk(child)->fastopen_rsk != req);
  686. BUG_ON(sk != req->rsk_listener);
  687. /* Paranoid, to prevent race condition if
  688. * an inbound pkt destined for child is
  689. * blocked by sock lock in tcp_v4_rcv().
  690. * Also to satisfy an assertion in
  691. * tcp_v4_destroy_sock().
  692. */
  693. tcp_sk(child)->fastopen_rsk = NULL;
  694. }
  695. inet_csk_destroy_sock(child);
  696. reqsk_put(req);
  697. }
  698. void inet_csk_reqsk_queue_add(struct sock *sk, struct request_sock *req,
  699. struct sock *child)
  700. {
  701. struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
  702. spin_lock(&queue->rskq_lock);
  703. if (unlikely(sk->sk_state != TCP_LISTEN)) {
  704. inet_child_forget(sk, req, child);
  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. }
  717. EXPORT_SYMBOL(inet_csk_reqsk_queue_add);
  718. struct sock *inet_csk_complete_hashdance(struct sock *sk, struct sock *child,
  719. struct request_sock *req, bool own_req)
  720. {
  721. if (own_req) {
  722. inet_csk_reqsk_queue_drop(sk, req);
  723. reqsk_queue_removed(&inet_csk(sk)->icsk_accept_queue, req);
  724. inet_csk_reqsk_queue_add(sk, req, child);
  725. /* Warning: caller must not call reqsk_put(req);
  726. * child stole last reference on it.
  727. */
  728. return child;
  729. }
  730. /* Too bad, another child took ownership of the request, undo. */
  731. bh_unlock_sock(child);
  732. sock_put(child);
  733. return NULL;
  734. }
  735. EXPORT_SYMBOL(inet_csk_complete_hashdance);
  736. /*
  737. * This routine closes sockets which have been at least partially
  738. * opened, but not yet accepted.
  739. */
  740. void inet_csk_listen_stop(struct sock *sk)
  741. {
  742. struct inet_connection_sock *icsk = inet_csk(sk);
  743. struct request_sock_queue *queue = &icsk->icsk_accept_queue;
  744. struct request_sock *next, *req;
  745. /* Following specs, it would be better either to send FIN
  746. * (and enter FIN-WAIT-1, it is normal close)
  747. * or to send active reset (abort).
  748. * Certainly, it is pretty dangerous while synflood, but it is
  749. * bad justification for our negligence 8)
  750. * To be honest, we are not able to make either
  751. * of the variants now. --ANK
  752. */
  753. while ((req = reqsk_queue_remove(queue, sk)) != NULL) {
  754. struct sock *child = req->sk;
  755. local_bh_disable();
  756. bh_lock_sock(child);
  757. WARN_ON(sock_owned_by_user(child));
  758. sock_hold(child);
  759. inet_child_forget(sk, req, child);
  760. bh_unlock_sock(child);
  761. local_bh_enable();
  762. sock_put(child);
  763. cond_resched();
  764. }
  765. if (queue->fastopenq.rskq_rst_head) {
  766. /* Free all the reqs queued in rskq_rst_head. */
  767. spin_lock_bh(&queue->fastopenq.lock);
  768. req = queue->fastopenq.rskq_rst_head;
  769. queue->fastopenq.rskq_rst_head = NULL;
  770. spin_unlock_bh(&queue->fastopenq.lock);
  771. while (req != NULL) {
  772. next = req->dl_next;
  773. reqsk_put(req);
  774. req = next;
  775. }
  776. }
  777. WARN_ON_ONCE(sk->sk_ack_backlog);
  778. }
  779. EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
  780. void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
  781. {
  782. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  783. const struct inet_sock *inet = inet_sk(sk);
  784. sin->sin_family = AF_INET;
  785. sin->sin_addr.s_addr = inet->inet_daddr;
  786. sin->sin_port = inet->inet_dport;
  787. }
  788. EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr);
  789. #ifdef CONFIG_COMPAT
  790. int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
  791. char __user *optval, int __user *optlen)
  792. {
  793. const struct inet_connection_sock *icsk = inet_csk(sk);
  794. if (icsk->icsk_af_ops->compat_getsockopt)
  795. return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname,
  796. optval, optlen);
  797. return icsk->icsk_af_ops->getsockopt(sk, level, optname,
  798. optval, optlen);
  799. }
  800. EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt);
  801. int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
  802. char __user *optval, unsigned int optlen)
  803. {
  804. const struct inet_connection_sock *icsk = inet_csk(sk);
  805. if (icsk->icsk_af_ops->compat_setsockopt)
  806. return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname,
  807. optval, optlen);
  808. return icsk->icsk_af_ops->setsockopt(sk, level, optname,
  809. optval, optlen);
  810. }
  811. EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt);
  812. #endif
  813. static struct dst_entry *inet_csk_rebuild_route(struct sock *sk, struct flowi *fl)
  814. {
  815. const struct inet_sock *inet = inet_sk(sk);
  816. const struct ip_options_rcu *inet_opt;
  817. __be32 daddr = inet->inet_daddr;
  818. struct flowi4 *fl4;
  819. struct rtable *rt;
  820. rcu_read_lock();
  821. inet_opt = rcu_dereference(inet->inet_opt);
  822. if (inet_opt && inet_opt->opt.srr)
  823. daddr = inet_opt->opt.faddr;
  824. fl4 = &fl->u.ip4;
  825. rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr,
  826. inet->inet_saddr, inet->inet_dport,
  827. inet->inet_sport, sk->sk_protocol,
  828. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if);
  829. if (IS_ERR(rt))
  830. rt = NULL;
  831. if (rt)
  832. sk_setup_caps(sk, &rt->dst);
  833. rcu_read_unlock();
  834. return &rt->dst;
  835. }
  836. struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu)
  837. {
  838. struct dst_entry *dst = __sk_dst_check(sk, 0);
  839. struct inet_sock *inet = inet_sk(sk);
  840. if (!dst) {
  841. dst = inet_csk_rebuild_route(sk, &inet->cork.fl);
  842. if (!dst)
  843. goto out;
  844. }
  845. dst->ops->update_pmtu(dst, sk, NULL, mtu);
  846. dst = __sk_dst_check(sk, 0);
  847. if (!dst)
  848. dst = inet_csk_rebuild_route(sk, &inet->cork.fl);
  849. out:
  850. return dst;
  851. }
  852. EXPORT_SYMBOL_GPL(inet_csk_update_pmtu);