inet_connection_sock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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 array holds the first and last local port number.
  30. */
  31. int sysctl_local_port_range[2] = { 32768, 61000 };
  32. DEFINE_SEQLOCK(sysctl_port_range_lock);
  33. void inet_get_local_port_range(int *low, int *high)
  34. {
  35. unsigned seq;
  36. do {
  37. seq = read_seqbegin(&sysctl_port_range_lock);
  38. *low = sysctl_local_port_range[0];
  39. *high = sysctl_local_port_range[1];
  40. } while (read_seqretry(&sysctl_port_range_lock, seq));
  41. }
  42. EXPORT_SYMBOL(inet_get_local_port_range);
  43. int inet_csk_bind_conflict(const struct sock *sk,
  44. const struct inet_bind_bucket *tb)
  45. {
  46. const __be32 sk_rcv_saddr = inet_rcv_saddr(sk);
  47. struct sock *sk2;
  48. struct hlist_node *node;
  49. int reuse = sk->sk_reuse;
  50. sk_for_each_bound(sk2, node, &tb->owners) {
  51. if (sk != sk2 &&
  52. !inet_v6_ipv6only(sk2) &&
  53. (!sk->sk_bound_dev_if ||
  54. !sk2->sk_bound_dev_if ||
  55. sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) {
  56. if (!reuse || !sk2->sk_reuse ||
  57. sk2->sk_state == TCP_LISTEN) {
  58. const __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
  59. if (!sk2_rcv_saddr || !sk_rcv_saddr ||
  60. sk2_rcv_saddr == sk_rcv_saddr)
  61. break;
  62. }
  63. }
  64. }
  65. return node != NULL;
  66. }
  67. EXPORT_SYMBOL_GPL(inet_csk_bind_conflict);
  68. /* Obtain a reference to a local port for the given sock,
  69. * if snum is zero it means select any available local port.
  70. */
  71. int inet_csk_get_port(struct inet_hashinfo *hashinfo,
  72. struct sock *sk, unsigned short snum,
  73. int (*bind_conflict)(const struct sock *sk,
  74. const struct inet_bind_bucket *tb))
  75. {
  76. struct inet_bind_hashbucket *head;
  77. struct hlist_node *node;
  78. struct inet_bind_bucket *tb;
  79. int ret;
  80. local_bh_disable();
  81. if (!snum) {
  82. int remaining, rover, low, high;
  83. inet_get_local_port_range(&low, &high);
  84. remaining = high - low;
  85. rover = net_random() % remaining + low;
  86. do {
  87. head = &hashinfo->bhash[inet_bhashfn(rover, hashinfo->bhash_size)];
  88. spin_lock(&head->lock);
  89. inet_bind_bucket_for_each(tb, node, &head->chain)
  90. if (tb->port == rover)
  91. goto next;
  92. break;
  93. next:
  94. spin_unlock(&head->lock);
  95. if (++rover > high)
  96. rover = low;
  97. } while (--remaining > 0);
  98. /* Exhausted local port range during search? It is not
  99. * possible for us to be holding one of the bind hash
  100. * locks if this test triggers, because if 'remaining'
  101. * drops to zero, we broke out of the do/while loop at
  102. * the top level, not from the 'break;' statement.
  103. */
  104. ret = 1;
  105. if (remaining <= 0)
  106. goto fail;
  107. /* OK, here is the one we will use. HEAD is
  108. * non-NULL and we hold it's mutex.
  109. */
  110. snum = rover;
  111. } else {
  112. head = &hashinfo->bhash[inet_bhashfn(snum, hashinfo->bhash_size)];
  113. spin_lock(&head->lock);
  114. inet_bind_bucket_for_each(tb, node, &head->chain)
  115. if (tb->port == snum)
  116. goto tb_found;
  117. }
  118. tb = NULL;
  119. goto tb_not_found;
  120. tb_found:
  121. if (!hlist_empty(&tb->owners)) {
  122. if (sk->sk_reuse > 1)
  123. goto success;
  124. if (tb->fastreuse > 0 &&
  125. sk->sk_reuse && sk->sk_state != TCP_LISTEN) {
  126. goto success;
  127. } else {
  128. ret = 1;
  129. if (bind_conflict(sk, tb))
  130. goto fail_unlock;
  131. }
  132. }
  133. tb_not_found:
  134. ret = 1;
  135. if (!tb && (tb = inet_bind_bucket_create(hashinfo->bind_bucket_cachep, head, snum)) == NULL)
  136. goto fail_unlock;
  137. if (hlist_empty(&tb->owners)) {
  138. if (sk->sk_reuse && sk->sk_state != TCP_LISTEN)
  139. tb->fastreuse = 1;
  140. else
  141. tb->fastreuse = 0;
  142. } else if (tb->fastreuse &&
  143. (!sk->sk_reuse || sk->sk_state == TCP_LISTEN))
  144. tb->fastreuse = 0;
  145. success:
  146. if (!inet_csk(sk)->icsk_bind_hash)
  147. inet_bind_hash(sk, tb, snum);
  148. BUG_TRAP(inet_csk(sk)->icsk_bind_hash == tb);
  149. ret = 0;
  150. fail_unlock:
  151. spin_unlock(&head->lock);
  152. fail:
  153. local_bh_enable();
  154. return ret;
  155. }
  156. EXPORT_SYMBOL_GPL(inet_csk_get_port);
  157. /*
  158. * Wait for an incoming connection, avoid race conditions. This must be called
  159. * with the socket locked.
  160. */
  161. static int inet_csk_wait_for_connect(struct sock *sk, long timeo)
  162. {
  163. struct inet_connection_sock *icsk = inet_csk(sk);
  164. DEFINE_WAIT(wait);
  165. int err;
  166. /*
  167. * True wake-one mechanism for incoming connections: only
  168. * one process gets woken up, not the 'whole herd'.
  169. * Since we do not 'race & poll' for established sockets
  170. * anymore, the common case will execute the loop only once.
  171. *
  172. * Subtle issue: "add_wait_queue_exclusive()" will be added
  173. * after any current non-exclusive waiters, and we know that
  174. * it will always _stay_ after any new non-exclusive waiters
  175. * because all non-exclusive waiters are added at the
  176. * beginning of the wait-queue. As such, it's ok to "drop"
  177. * our exclusiveness temporarily when we get woken up without
  178. * having to remove and re-insert us on the wait queue.
  179. */
  180. for (;;) {
  181. prepare_to_wait_exclusive(sk->sk_sleep, &wait,
  182. TASK_INTERRUPTIBLE);
  183. release_sock(sk);
  184. if (reqsk_queue_empty(&icsk->icsk_accept_queue))
  185. timeo = schedule_timeout(timeo);
  186. lock_sock(sk);
  187. err = 0;
  188. if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
  189. break;
  190. err = -EINVAL;
  191. if (sk->sk_state != TCP_LISTEN)
  192. break;
  193. err = sock_intr_errno(timeo);
  194. if (signal_pending(current))
  195. break;
  196. err = -EAGAIN;
  197. if (!timeo)
  198. break;
  199. }
  200. finish_wait(sk->sk_sleep, &wait);
  201. return err;
  202. }
  203. /*
  204. * This will accept the next outstanding connection.
  205. */
  206. struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
  207. {
  208. struct inet_connection_sock *icsk = inet_csk(sk);
  209. struct sock *newsk;
  210. int error;
  211. lock_sock(sk);
  212. /* We need to make sure that this socket is listening,
  213. * and that it has something pending.
  214. */
  215. error = -EINVAL;
  216. if (sk->sk_state != TCP_LISTEN)
  217. goto out_err;
  218. /* Find already established connection */
  219. if (reqsk_queue_empty(&icsk->icsk_accept_queue)) {
  220. long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  221. /* If this is a non blocking socket don't sleep */
  222. error = -EAGAIN;
  223. if (!timeo)
  224. goto out_err;
  225. error = inet_csk_wait_for_connect(sk, timeo);
  226. if (error)
  227. goto out_err;
  228. }
  229. newsk = reqsk_queue_get_child(&icsk->icsk_accept_queue, sk);
  230. BUG_TRAP(newsk->sk_state != TCP_SYN_RECV);
  231. out:
  232. release_sock(sk);
  233. return newsk;
  234. out_err:
  235. newsk = NULL;
  236. *err = error;
  237. goto out;
  238. }
  239. EXPORT_SYMBOL(inet_csk_accept);
  240. /*
  241. * Using different timers for retransmit, delayed acks and probes
  242. * We may wish use just one timer maintaining a list of expire jiffies
  243. * to optimize.
  244. */
  245. void inet_csk_init_xmit_timers(struct sock *sk,
  246. void (*retransmit_handler)(unsigned long),
  247. void (*delack_handler)(unsigned long),
  248. void (*keepalive_handler)(unsigned long))
  249. {
  250. struct inet_connection_sock *icsk = inet_csk(sk);
  251. init_timer(&icsk->icsk_retransmit_timer);
  252. init_timer(&icsk->icsk_delack_timer);
  253. init_timer(&sk->sk_timer);
  254. icsk->icsk_retransmit_timer.function = retransmit_handler;
  255. icsk->icsk_delack_timer.function = delack_handler;
  256. sk->sk_timer.function = keepalive_handler;
  257. icsk->icsk_retransmit_timer.data =
  258. icsk->icsk_delack_timer.data =
  259. sk->sk_timer.data = (unsigned long)sk;
  260. icsk->icsk_pending = icsk->icsk_ack.pending = 0;
  261. }
  262. EXPORT_SYMBOL(inet_csk_init_xmit_timers);
  263. void inet_csk_clear_xmit_timers(struct sock *sk)
  264. {
  265. struct inet_connection_sock *icsk = inet_csk(sk);
  266. icsk->icsk_pending = icsk->icsk_ack.pending = icsk->icsk_ack.blocked = 0;
  267. sk_stop_timer(sk, &icsk->icsk_retransmit_timer);
  268. sk_stop_timer(sk, &icsk->icsk_delack_timer);
  269. sk_stop_timer(sk, &sk->sk_timer);
  270. }
  271. EXPORT_SYMBOL(inet_csk_clear_xmit_timers);
  272. void inet_csk_delete_keepalive_timer(struct sock *sk)
  273. {
  274. sk_stop_timer(sk, &sk->sk_timer);
  275. }
  276. EXPORT_SYMBOL(inet_csk_delete_keepalive_timer);
  277. void inet_csk_reset_keepalive_timer(struct sock *sk, unsigned long len)
  278. {
  279. sk_reset_timer(sk, &sk->sk_timer, jiffies + len);
  280. }
  281. EXPORT_SYMBOL(inet_csk_reset_keepalive_timer);
  282. struct dst_entry* inet_csk_route_req(struct sock *sk,
  283. const struct request_sock *req)
  284. {
  285. struct rtable *rt;
  286. const struct inet_request_sock *ireq = inet_rsk(req);
  287. struct ip_options *opt = inet_rsk(req)->opt;
  288. struct flowi fl = { .oif = sk->sk_bound_dev_if,
  289. .nl_u = { .ip4_u =
  290. { .daddr = ((opt && opt->srr) ?
  291. opt->faddr :
  292. ireq->rmt_addr),
  293. .saddr = ireq->loc_addr,
  294. .tos = RT_CONN_FLAGS(sk) } },
  295. .proto = sk->sk_protocol,
  296. .uli_u = { .ports =
  297. { .sport = inet_sk(sk)->sport,
  298. .dport = ireq->rmt_port } } };
  299. security_req_classify_flow(req, &fl);
  300. if (ip_route_output_flow(&rt, &fl, sk, 0)) {
  301. IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  302. return NULL;
  303. }
  304. if (opt && opt->is_strictroute && rt->rt_dst != rt->rt_gateway) {
  305. ip_rt_put(rt);
  306. IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
  307. return NULL;
  308. }
  309. return &rt->u.dst;
  310. }
  311. EXPORT_SYMBOL_GPL(inet_csk_route_req);
  312. static inline u32 inet_synq_hash(const __be32 raddr, const __be16 rport,
  313. const u32 rnd, const u32 synq_hsize)
  314. {
  315. return jhash_2words((__force u32)raddr, (__force u32)rport, rnd) & (synq_hsize - 1);
  316. }
  317. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  318. #define AF_INET_FAMILY(fam) ((fam) == AF_INET)
  319. #else
  320. #define AF_INET_FAMILY(fam) 1
  321. #endif
  322. struct request_sock *inet_csk_search_req(const struct sock *sk,
  323. struct request_sock ***prevp,
  324. const __be16 rport, const __be32 raddr,
  325. const __be32 laddr)
  326. {
  327. const struct inet_connection_sock *icsk = inet_csk(sk);
  328. struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
  329. struct request_sock *req, **prev;
  330. for (prev = &lopt->syn_table[inet_synq_hash(raddr, rport, lopt->hash_rnd,
  331. lopt->nr_table_entries)];
  332. (req = *prev) != NULL;
  333. prev = &req->dl_next) {
  334. const struct inet_request_sock *ireq = inet_rsk(req);
  335. if (ireq->rmt_port == rport &&
  336. ireq->rmt_addr == raddr &&
  337. ireq->loc_addr == laddr &&
  338. AF_INET_FAMILY(req->rsk_ops->family)) {
  339. BUG_TRAP(!req->sk);
  340. *prevp = prev;
  341. break;
  342. }
  343. }
  344. return req;
  345. }
  346. EXPORT_SYMBOL_GPL(inet_csk_search_req);
  347. void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
  348. unsigned long timeout)
  349. {
  350. struct inet_connection_sock *icsk = inet_csk(sk);
  351. struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
  352. const u32 h = inet_synq_hash(inet_rsk(req)->rmt_addr, inet_rsk(req)->rmt_port,
  353. lopt->hash_rnd, lopt->nr_table_entries);
  354. reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
  355. inet_csk_reqsk_queue_added(sk, timeout);
  356. }
  357. /* Only thing we need from tcp.h */
  358. extern int sysctl_tcp_synack_retries;
  359. EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_hash_add);
  360. void inet_csk_reqsk_queue_prune(struct sock *parent,
  361. const unsigned long interval,
  362. const unsigned long timeout,
  363. const unsigned long max_rto)
  364. {
  365. struct inet_connection_sock *icsk = inet_csk(parent);
  366. struct request_sock_queue *queue = &icsk->icsk_accept_queue;
  367. struct listen_sock *lopt = queue->listen_opt;
  368. int max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
  369. int thresh = max_retries;
  370. unsigned long now = jiffies;
  371. struct request_sock **reqp, *req;
  372. int i, budget;
  373. if (lopt == NULL || lopt->qlen == 0)
  374. return;
  375. /* Normally all the openreqs are young and become mature
  376. * (i.e. converted to established socket) for first timeout.
  377. * If synack was not acknowledged for 3 seconds, it means
  378. * one of the following things: synack was lost, ack was lost,
  379. * rtt is high or nobody planned to ack (i.e. synflood).
  380. * When server is a bit loaded, queue is populated with old
  381. * open requests, reducing effective size of queue.
  382. * When server is well loaded, queue size reduces to zero
  383. * after several minutes of work. It is not synflood,
  384. * it is normal operation. The solution is pruning
  385. * too old entries overriding normal timeout, when
  386. * situation becomes dangerous.
  387. *
  388. * Essentially, we reserve half of room for young
  389. * embrions; and abort old ones without pity, if old
  390. * ones are about to clog our table.
  391. */
  392. if (lopt->qlen>>(lopt->max_qlen_log-1)) {
  393. int young = (lopt->qlen_young<<1);
  394. while (thresh > 2) {
  395. if (lopt->qlen < young)
  396. break;
  397. thresh--;
  398. young <<= 1;
  399. }
  400. }
  401. if (queue->rskq_defer_accept)
  402. max_retries = queue->rskq_defer_accept;
  403. budget = 2 * (lopt->nr_table_entries / (timeout / interval));
  404. i = lopt->clock_hand;
  405. do {
  406. reqp=&lopt->syn_table[i];
  407. while ((req = *reqp) != NULL) {
  408. if (time_after_eq(now, req->expires)) {
  409. if ((req->retrans < thresh ||
  410. (inet_rsk(req)->acked && req->retrans < max_retries))
  411. && !req->rsk_ops->rtx_syn_ack(parent, req, NULL)) {
  412. unsigned long timeo;
  413. if (req->retrans++ == 0)
  414. lopt->qlen_young--;
  415. timeo = min((timeout << req->retrans), max_rto);
  416. req->expires = now + timeo;
  417. reqp = &req->dl_next;
  418. continue;
  419. }
  420. /* Drop this request */
  421. inet_csk_reqsk_queue_unlink(parent, req, reqp);
  422. reqsk_queue_removed(queue, req);
  423. reqsk_free(req);
  424. continue;
  425. }
  426. reqp = &req->dl_next;
  427. }
  428. i = (i + 1) & (lopt->nr_table_entries - 1);
  429. } while (--budget > 0);
  430. lopt->clock_hand = i;
  431. if (lopt->qlen)
  432. inet_csk_reset_keepalive_timer(parent, interval);
  433. }
  434. EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune);
  435. struct sock *inet_csk_clone(struct sock *sk, const struct request_sock *req,
  436. const gfp_t priority)
  437. {
  438. struct sock *newsk = sk_clone(sk, priority);
  439. if (newsk != NULL) {
  440. struct inet_connection_sock *newicsk = inet_csk(newsk);
  441. newsk->sk_state = TCP_SYN_RECV;
  442. newicsk->icsk_bind_hash = NULL;
  443. inet_sk(newsk)->dport = inet_rsk(req)->rmt_port;
  444. newsk->sk_write_space = sk_stream_write_space;
  445. newicsk->icsk_retransmits = 0;
  446. newicsk->icsk_backoff = 0;
  447. newicsk->icsk_probes_out = 0;
  448. /* Deinitialize accept_queue to trap illegal accesses. */
  449. memset(&newicsk->icsk_accept_queue, 0, sizeof(newicsk->icsk_accept_queue));
  450. security_inet_csk_clone(newsk, req);
  451. }
  452. return newsk;
  453. }
  454. EXPORT_SYMBOL_GPL(inet_csk_clone);
  455. /*
  456. * At this point, there should be no process reference to this
  457. * socket, and thus no user references at all. Therefore we
  458. * can assume the socket waitqueue is inactive and nobody will
  459. * try to jump onto it.
  460. */
  461. void inet_csk_destroy_sock(struct sock *sk)
  462. {
  463. BUG_TRAP(sk->sk_state == TCP_CLOSE);
  464. BUG_TRAP(sock_flag(sk, SOCK_DEAD));
  465. /* It cannot be in hash table! */
  466. BUG_TRAP(sk_unhashed(sk));
  467. /* If it has not 0 inet_sk(sk)->num, it must be bound */
  468. BUG_TRAP(!inet_sk(sk)->num || inet_csk(sk)->icsk_bind_hash);
  469. sk->sk_prot->destroy(sk);
  470. sk_stream_kill_queues(sk);
  471. xfrm_sk_free_policy(sk);
  472. sk_refcnt_debug_release(sk);
  473. atomic_dec(sk->sk_prot->orphan_count);
  474. sock_put(sk);
  475. }
  476. EXPORT_SYMBOL(inet_csk_destroy_sock);
  477. int inet_csk_listen_start(struct sock *sk, const int nr_table_entries)
  478. {
  479. struct inet_sock *inet = inet_sk(sk);
  480. struct inet_connection_sock *icsk = inet_csk(sk);
  481. int rc = reqsk_queue_alloc(&icsk->icsk_accept_queue, nr_table_entries);
  482. if (rc != 0)
  483. return rc;
  484. sk->sk_max_ack_backlog = 0;
  485. sk->sk_ack_backlog = 0;
  486. inet_csk_delack_init(sk);
  487. /* There is race window here: we announce ourselves listening,
  488. * but this transition is still not validated by get_port().
  489. * It is OK, because this socket enters to hash table only
  490. * after validation is complete.
  491. */
  492. sk->sk_state = TCP_LISTEN;
  493. if (!sk->sk_prot->get_port(sk, inet->num)) {
  494. inet->sport = htons(inet->num);
  495. sk_dst_reset(sk);
  496. sk->sk_prot->hash(sk);
  497. return 0;
  498. }
  499. sk->sk_state = TCP_CLOSE;
  500. __reqsk_queue_destroy(&icsk->icsk_accept_queue);
  501. return -EADDRINUSE;
  502. }
  503. EXPORT_SYMBOL_GPL(inet_csk_listen_start);
  504. /*
  505. * This routine closes sockets which have been at least partially
  506. * opened, but not yet accepted.
  507. */
  508. void inet_csk_listen_stop(struct sock *sk)
  509. {
  510. struct inet_connection_sock *icsk = inet_csk(sk);
  511. struct request_sock *acc_req;
  512. struct request_sock *req;
  513. inet_csk_delete_keepalive_timer(sk);
  514. /* make all the listen_opt local to us */
  515. acc_req = reqsk_queue_yank_acceptq(&icsk->icsk_accept_queue);
  516. /* Following specs, it would be better either to send FIN
  517. * (and enter FIN-WAIT-1, it is normal close)
  518. * or to send active reset (abort).
  519. * Certainly, it is pretty dangerous while synflood, but it is
  520. * bad justification for our negligence 8)
  521. * To be honest, we are not able to make either
  522. * of the variants now. --ANK
  523. */
  524. reqsk_queue_destroy(&icsk->icsk_accept_queue);
  525. while ((req = acc_req) != NULL) {
  526. struct sock *child = req->sk;
  527. acc_req = req->dl_next;
  528. local_bh_disable();
  529. bh_lock_sock(child);
  530. BUG_TRAP(!sock_owned_by_user(child));
  531. sock_hold(child);
  532. sk->sk_prot->disconnect(child, O_NONBLOCK);
  533. sock_orphan(child);
  534. atomic_inc(sk->sk_prot->orphan_count);
  535. inet_csk_destroy_sock(child);
  536. bh_unlock_sock(child);
  537. local_bh_enable();
  538. sock_put(child);
  539. sk_acceptq_removed(sk);
  540. __reqsk_free(req);
  541. }
  542. BUG_TRAP(!sk->sk_ack_backlog);
  543. }
  544. EXPORT_SYMBOL_GPL(inet_csk_listen_stop);
  545. void inet_csk_addr2sockaddr(struct sock *sk, struct sockaddr *uaddr)
  546. {
  547. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  548. const struct inet_sock *inet = inet_sk(sk);
  549. sin->sin_family = AF_INET;
  550. sin->sin_addr.s_addr = inet->daddr;
  551. sin->sin_port = inet->dport;
  552. }
  553. EXPORT_SYMBOL_GPL(inet_csk_addr2sockaddr);
  554. int inet_csk_ctl_sock_create(struct socket **sock, unsigned short family,
  555. unsigned short type, unsigned char protocol)
  556. {
  557. int rc = sock_create_kern(family, type, protocol, sock);
  558. if (rc == 0) {
  559. (*sock)->sk->sk_allocation = GFP_ATOMIC;
  560. inet_sk((*sock)->sk)->uc_ttl = -1;
  561. /*
  562. * Unhash it so that IP input processing does not even see it,
  563. * we do not wish this socket to see incoming packets.
  564. */
  565. (*sock)->sk->sk_prot->unhash((*sock)->sk);
  566. }
  567. return rc;
  568. }
  569. EXPORT_SYMBOL_GPL(inet_csk_ctl_sock_create);
  570. #ifdef CONFIG_COMPAT
  571. int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
  572. char __user *optval, int __user *optlen)
  573. {
  574. const struct inet_connection_sock *icsk = inet_csk(sk);
  575. if (icsk->icsk_af_ops->compat_getsockopt != NULL)
  576. return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname,
  577. optval, optlen);
  578. return icsk->icsk_af_ops->getsockopt(sk, level, optname,
  579. optval, optlen);
  580. }
  581. EXPORT_SYMBOL_GPL(inet_csk_compat_getsockopt);
  582. int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
  583. char __user *optval, int optlen)
  584. {
  585. const struct inet_connection_sock *icsk = inet_csk(sk);
  586. if (icsk->icsk_af_ops->compat_setsockopt != NULL)
  587. return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname,
  588. optval, optlen);
  589. return icsk->icsk_af_ops->setsockopt(sk, level, optname,
  590. optval, optlen);
  591. }
  592. EXPORT_SYMBOL_GPL(inet_csk_compat_setsockopt);
  593. #endif