inet_connection_sock.c 26 KB

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