inet_connection_sock.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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. struct rtable *rt;
  371. const struct inet_request_sock *ireq = inet_rsk(req);
  372. struct ip_options_rcu *opt = inet_rsk(req)->opt;
  373. struct net *net = sock_net(sk);
  374. int flags = inet_sk_flowi_flags(sk);
  375. flowi4_init_output(fl4, sk->sk_bound_dev_if, ireq->ir_mark,
  376. RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
  377. sk->sk_protocol,
  378. flags,
  379. (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr,
  380. ireq->ir_loc_addr, ireq->ir_rmt_port, inet_sk(sk)->inet_sport);
  381. security_req_classify_flow(req, flowi4_to_flowi(fl4));
  382. rt = ip_route_output_flow(net, fl4, sk);
  383. if (IS_ERR(rt))
  384. goto no_route;
  385. if (opt && opt->opt.is_strictroute && rt->rt_uses_gateway)
  386. goto route_err;
  387. return &rt->dst;
  388. route_err:
  389. ip_rt_put(rt);
  390. no_route:
  391. IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES);
  392. return NULL;
  393. }
  394. EXPORT_SYMBOL_GPL(inet_csk_route_req);
  395. struct dst_entry *inet_csk_route_child_sock(struct sock *sk,
  396. struct sock *newsk,
  397. const struct request_sock *req)
  398. {
  399. const struct inet_request_sock *ireq = inet_rsk(req);
  400. struct inet_sock *newinet = inet_sk(newsk);
  401. struct ip_options_rcu *opt;
  402. struct net *net = sock_net(sk);
  403. struct flowi4 *fl4;
  404. struct rtable *rt;
  405. fl4 = &newinet->cork.fl.u.ip4;
  406. rcu_read_lock();
  407. opt = rcu_dereference(newinet->inet_opt);
  408. flowi4_init_output(fl4, sk->sk_bound_dev_if, inet_rsk(req)->ir_mark,
  409. RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
  410. sk->sk_protocol, inet_sk_flowi_flags(sk),
  411. (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr,
  412. ireq->ir_loc_addr, ireq->ir_rmt_port, inet_sk(sk)->inet_sport);
  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. write_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. write_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. static void reqsk_timer_handler(unsigned long data)
  512. {
  513. struct request_sock *req = (struct request_sock *)data;
  514. struct sock *sk_listener = req->rsk_listener;
  515. struct inet_connection_sock *icsk = inet_csk(sk_listener);
  516. struct request_sock_queue *queue = &icsk->icsk_accept_queue;
  517. struct listen_sock *lopt = queue->listen_opt;
  518. int expire = 0, resend = 0;
  519. int max_retries, thresh;
  520. if (sk_listener->sk_state != TCP_LISTEN || !lopt) {
  521. reqsk_put(req);
  522. return;
  523. }
  524. max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
  525. thresh = max_retries;
  526. /* Normally all the openreqs are young and become mature
  527. * (i.e. converted to established socket) for first timeout.
  528. * If synack was not acknowledged for 1 second, it means
  529. * one of the following things: synack was lost, ack was lost,
  530. * rtt is high or nobody planned to ack (i.e. synflood).
  531. * When server is a bit loaded, queue is populated with old
  532. * open requests, reducing effective size of queue.
  533. * When server is well loaded, queue size reduces to zero
  534. * after several minutes of work. It is not synflood,
  535. * it is normal operation. The solution is pruning
  536. * too old entries overriding normal timeout, when
  537. * situation becomes dangerous.
  538. *
  539. * Essentially, we reserve half of room for young
  540. * embrions; and abort old ones without pity, if old
  541. * ones are about to clog our table.
  542. */
  543. if (listen_sock_qlen(lopt) >> (lopt->max_qlen_log - 1)) {
  544. int young = listen_sock_young(lopt) << 1;
  545. while (thresh > 2) {
  546. if (listen_sock_qlen(lopt) < young)
  547. break;
  548. thresh--;
  549. young <<= 1;
  550. }
  551. }
  552. if (queue->rskq_defer_accept)
  553. max_retries = queue->rskq_defer_accept;
  554. syn_ack_recalc(req, thresh, max_retries, queue->rskq_defer_accept,
  555. &expire, &resend);
  556. req->rsk_ops->syn_ack_timeout(sk_listener, req);
  557. if (!expire &&
  558. (!resend ||
  559. !inet_rtx_syn_ack(sk_listener, req) ||
  560. inet_rsk(req)->acked)) {
  561. unsigned long timeo;
  562. if (req->num_timeout++ == 0)
  563. atomic_inc(&lopt->young_dec);
  564. timeo = min(TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
  565. mod_timer_pinned(&req->rsk_timer, jiffies + timeo);
  566. return;
  567. }
  568. inet_csk_reqsk_queue_drop(sk_listener, req);
  569. reqsk_put(req);
  570. }
  571. void reqsk_queue_hash_req(struct request_sock_queue *queue,
  572. u32 hash, struct request_sock *req,
  573. unsigned long timeout)
  574. {
  575. struct listen_sock *lopt = queue->listen_opt;
  576. req->num_retrans = 0;
  577. req->num_timeout = 0;
  578. req->sk = NULL;
  579. /* before letting lookups find us, make sure all req fields
  580. * are committed to memory and refcnt initialized.
  581. */
  582. smp_wmb();
  583. atomic_set(&req->rsk_refcnt, 2);
  584. setup_timer(&req->rsk_timer, reqsk_timer_handler, (unsigned long)req);
  585. req->rsk_hash = hash;
  586. write_lock(&queue->syn_wait_lock);
  587. req->dl_next = lopt->syn_table[hash];
  588. lopt->syn_table[hash] = req;
  589. write_unlock(&queue->syn_wait_lock);
  590. mod_timer_pinned(&req->rsk_timer, jiffies + timeout);
  591. }
  592. EXPORT_SYMBOL(reqsk_queue_hash_req);
  593. /**
  594. * inet_csk_clone_lock - clone an inet socket, and lock its clone
  595. * @sk: the socket to clone
  596. * @req: request_sock
  597. * @priority: for allocation (%GFP_KERNEL, %GFP_ATOMIC, etc)
  598. *
  599. * Caller must unlock socket even in error path (bh_unlock_sock(newsk))
  600. */
  601. struct sock *inet_csk_clone_lock(const struct sock *sk,
  602. const struct request_sock *req,
  603. const gfp_t priority)
  604. {
  605. struct sock *newsk = sk_clone_lock(sk, priority);
  606. if (newsk != NULL) {
  607. struct inet_connection_sock *newicsk = inet_csk(newsk);
  608. newsk->sk_state = TCP_SYN_RECV;
  609. newicsk->icsk_bind_hash = NULL;
  610. inet_sk(newsk)->inet_dport = inet_rsk(req)->ir_rmt_port;
  611. inet_sk(newsk)->inet_num = inet_rsk(req)->ir_num;
  612. inet_sk(newsk)->inet_sport = htons(inet_rsk(req)->ir_num);
  613. newsk->sk_write_space = sk_stream_write_space;
  614. newsk->sk_mark = inet_rsk(req)->ir_mark;
  615. atomic64_set(&newsk->sk_cookie,
  616. atomic64_read(&inet_rsk(req)->ir_cookie));
  617. newicsk->icsk_retransmits = 0;
  618. newicsk->icsk_backoff = 0;
  619. newicsk->icsk_probes_out = 0;
  620. /* Deinitialize accept_queue to trap illegal accesses. */
  621. memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
  622. security_inet_csk_clone(newsk, req);
  623. }
  624. return newsk;
  625. }
  626. EXPORT_SYMBOL_GPL(inet_csk_clone_lock);
  627. /*
  628. * At this point, there should be no process reference to this
  629. * socket, and thus no user references at all. Therefore we
  630. * can assume the socket waitqueue is inactive and nobody will
  631. * try to jump onto it.
  632. */
  633. void inet_csk_destroy_sock(struct sock *sk)
  634. {
  635. WARN_ON(sk->sk_state != TCP_CLOSE);
  636. WARN_ON(!sock_flag(sk, SOCK_DEAD));
  637. /* It cannot be in hash table! */
  638. WARN_ON(!sk_unhashed(sk));
  639. /* If it has not 0 inet_sk(sk)->inet_num, it must be bound */
  640. WARN_ON(inet_sk(sk)->inet_num && !inet_csk(sk)->icsk_bind_hash);
  641. sk->sk_prot->destroy(sk);
  642. sk_stream_kill_queues(sk);
  643. xfrm_sk_free_policy(sk);
  644. sk_refcnt_debug_release(sk);
  645. percpu_counter_dec(sk->sk_prot->orphan_count);
  646. sock_put(sk);
  647. }
  648. EXPORT_SYMBOL(inet_csk_destroy_sock);
  649. /* This function allows to force a closure of a socket after the call to
  650. * tcp/dccp_create_openreq_child().
  651. */
  652. void inet_csk_prepare_forced_close(struct sock *sk)
  653. __releases(&sk->sk_lock.slock)
  654. {
  655. /* sk_clone_lock locked the socket and set refcnt to 2 */
  656. bh_unlock_sock(sk);
  657. sock_put(sk);
  658. /* The below has to be done to allow calling inet_csk_destroy_sock */
  659. sock_set_flag(sk, SOCK_DEAD);
  660. percpu_counter_inc(sk->sk_prot->orphan_count);
  661. inet_sk(sk)->inet_num = 0;
  662. }
  663. EXPORT_SYMBOL(inet_csk_prepare_forced_close);
  664. int inet_csk_listen_start(struct sock *sk, const int nr_table_entries)
  665. {
  666. struct inet_sock *inet = inet_sk(sk);
  667. struct inet_connection_sock *icsk = inet_csk(sk);
  668. int rc = reqsk_queue_alloc(&icsk->icsk_accept_queue, nr_table_entries);
  669. if (rc != 0)
  670. return rc;
  671. sk->sk_max_ack_backlog = 0;
  672. sk->sk_ack_backlog = 0;
  673. inet_csk_delack_init(sk);
  674. /* There is race window here: we announce ourselves listening,
  675. * but this transition is still not validated by get_port().
  676. * It is OK, because this socket enters to hash table only
  677. * after validation is complete.
  678. */
  679. sk->sk_state = TCP_LISTEN;
  680. if (!sk->sk_prot->get_port(sk, inet->inet_num)) {
  681. inet->inet_sport = htons(inet->inet_num);
  682. sk_dst_reset(sk);
  683. sk->sk_prot->hash(sk);
  684. return 0;
  685. }
  686. sk->sk_state = TCP_CLOSE;
  687. __reqsk_queue_destroy(&icsk->icsk_accept_queue);
  688. return -EADDRINUSE;
  689. }
  690. EXPORT_SYMBOL_GPL(inet_csk_listen_start);
  691. /*
  692. * This routine closes sockets which have been at least partially
  693. * opened, but not yet accepted.
  694. */
  695. void inet_csk_listen_stop(struct sock *sk)
  696. {
  697. struct inet_connection_sock *icsk = inet_csk(sk);
  698. struct request_sock_queue *queue = &icsk->icsk_accept_queue;
  699. struct request_sock *acc_req;
  700. struct request_sock *req;
  701. /* make all the listen_opt local to us */
  702. acc_req = reqsk_queue_yank_acceptq(queue);
  703. /* Following specs, it would be better either to send FIN
  704. * (and enter FIN-WAIT-1, it is normal close)
  705. * or to send active reset (abort).
  706. * Certainly, it is pretty dangerous while synflood, but it is
  707. * bad justification for our negligence 8)
  708. * To be honest, we are not able to make either
  709. * of the variants now. --ANK
  710. */
  711. reqsk_queue_destroy(queue);
  712. while ((req = acc_req) != NULL) {
  713. struct sock *child = req->sk;
  714. acc_req = req->dl_next;
  715. local_bh_disable();
  716. bh_lock_sock(child);
  717. WARN_ON(sock_owned_by_user(child));
  718. sock_hold(child);
  719. sk->sk_prot->disconnect(child, O_NONBLOCK);
  720. sock_orphan(child);
  721. percpu_counter_inc(sk->sk_prot->orphan_count);
  722. if (sk->sk_protocol == IPPROTO_TCP && tcp_rsk(req)->tfo_listener) {
  723. BUG_ON(tcp_sk(child)->fastopen_rsk != req);
  724. BUG_ON(sk != req->rsk_listener);
  725. /* Paranoid, to prevent race condition if
  726. * an inbound pkt destined for child is
  727. * blocked by sock lock in tcp_v4_rcv().
  728. * Also to satisfy an assertion in
  729. * tcp_v4_destroy_sock().
  730. */
  731. tcp_sk(child)->fastopen_rsk = NULL;
  732. }
  733. inet_csk_destroy_sock(child);
  734. bh_unlock_sock(child);
  735. local_bh_enable();
  736. sock_put(child);
  737. sk_acceptq_removed(sk);
  738. reqsk_put(req);
  739. }
  740. if (queue->fastopenq != NULL) {
  741. /* Free all the reqs queued in rskq_rst_head. */
  742. spin_lock_bh(&queue->fastopenq->lock);
  743. acc_req = queue->fastopenq->rskq_rst_head;
  744. queue->fastopenq->rskq_rst_head = NULL;
  745. spin_unlock_bh(&queue->fastopenq->lock);
  746. while ((req = acc_req) != NULL) {
  747. acc_req = req->dl_next;
  748. reqsk_put(req);
  749. }
  750. }
  751. WARN_ON(sk->sk_ack_backlog);
  752. }
  753. EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
  754. void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
  755. {
  756. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  757. const struct inet_sock *inet = inet_sk(sk);
  758. sin->sin_family = AF_INET;
  759. sin->sin_addr.s_addr = inet->inet_daddr;
  760. sin->sin_port = inet->inet_dport;
  761. }
  762. EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr);
  763. #ifdef CONFIG_COMPAT
  764. int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
  765. char __user *optval, int __user *optlen)
  766. {
  767. const struct inet_connection_sock *icsk = inet_csk(sk);
  768. if (icsk->icsk_af_ops->compat_getsockopt != NULL)
  769. return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname,
  770. optval, optlen);
  771. return icsk->icsk_af_ops->getsockopt(sk, level, optname,
  772. optval, optlen);
  773. }
  774. EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt);
  775. int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
  776. char __user *optval, unsigned int optlen)
  777. {
  778. const struct inet_connection_sock *icsk = inet_csk(sk);
  779. if (icsk->icsk_af_ops->compat_setsockopt != NULL)
  780. return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname,
  781. optval, optlen);
  782. return icsk->icsk_af_ops->setsockopt(sk, level, optname,
  783. optval, optlen);
  784. }
  785. EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt);
  786. #endif
  787. static struct dst_entry *inet_csk_rebuild_route(struct sock *sk, struct flowi *fl)
  788. {
  789. const struct inet_sock *inet = inet_sk(sk);
  790. const struct ip_options_rcu *inet_opt;
  791. __be32 daddr = inet->inet_daddr;
  792. struct flowi4 *fl4;
  793. struct rtable *rt;
  794. rcu_read_lock();
  795. inet_opt = rcu_dereference(inet->inet_opt);
  796. if (inet_opt && inet_opt->opt.srr)
  797. daddr = inet_opt->opt.faddr;
  798. fl4 = &fl->u.ip4;
  799. rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr,
  800. inet->inet_saddr, inet->inet_dport,
  801. inet->inet_sport, sk->sk_protocol,
  802. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if);
  803. if (IS_ERR(rt))
  804. rt = NULL;
  805. if (rt)
  806. sk_setup_caps(sk, &rt->dst);
  807. rcu_read_unlock();
  808. return &rt->dst;
  809. }
  810. struct dst_entry *inet_csk_update_pmtu(struct sock *sk, u32 mtu)
  811. {
  812. struct dst_entry *dst = __sk_dst_check(sk, 0);
  813. struct inet_sock *inet = inet_sk(sk);
  814. if (!dst) {
  815. dst = inet_csk_rebuild_route(sk, &inet->cork.fl);
  816. if (!dst)
  817. goto out;
  818. }
  819. dst->ops->update_pmtu(dst, sk, NULL, mtu);
  820. dst = __sk_dst_check(sk, 0);
  821. if (!dst)
  822. dst = inet_csk_rebuild_route(sk, &inet->cork.fl);
  823. out:
  824. return dst;
  825. }
  826. EXPORT_SYMBOL_GPL(inet_csk_update_pmtu);