inet_connection_sock.c 26 KB

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