inet_connection_sock.c 26 KB

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