inet_connection_sock.c 27 KB

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