inet_connection_sock.c 26 KB

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