output.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * net/dccp/output.c
  3. *
  4. * An implementation of the DCCP protocol
  5. * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/config.h>
  13. #include <linux/dccp.h>
  14. #include <linux/kernel.h>
  15. #include <linux/skbuff.h>
  16. #include <net/inet_sock.h>
  17. #include <net/sock.h>
  18. #include "ackvec.h"
  19. #include "ccid.h"
  20. #include "dccp.h"
  21. static inline void dccp_event_ack_sent(struct sock *sk)
  22. {
  23. inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
  24. }
  25. static void dccp_skb_entail(struct sock *sk, struct sk_buff *skb)
  26. {
  27. skb_set_owner_w(skb, sk);
  28. WARN_ON(sk->sk_send_head);
  29. sk->sk_send_head = skb;
  30. }
  31. /*
  32. * All SKB's seen here are completely headerless. It is our
  33. * job to build the DCCP header, and pass the packet down to
  34. * IP so it can do the same plus pass the packet off to the
  35. * device.
  36. */
  37. static int dccp_transmit_skb(struct sock *sk, struct sk_buff *skb)
  38. {
  39. if (likely(skb != NULL)) {
  40. const struct inet_sock *inet = inet_sk(sk);
  41. const struct inet_connection_sock *icsk = inet_csk(sk);
  42. struct dccp_sock *dp = dccp_sk(sk);
  43. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  44. struct dccp_hdr *dh;
  45. /* XXX For now we're using only 48 bits sequence numbers */
  46. const u32 dccp_header_size = sizeof(*dh) +
  47. sizeof(struct dccp_hdr_ext) +
  48. dccp_packet_hdr_len(dcb->dccpd_type);
  49. int err, set_ack = 1;
  50. u64 ackno = dp->dccps_gsr;
  51. dccp_inc_seqno(&dp->dccps_gss);
  52. switch (dcb->dccpd_type) {
  53. case DCCP_PKT_DATA:
  54. set_ack = 0;
  55. /* fall through */
  56. case DCCP_PKT_DATAACK:
  57. break;
  58. case DCCP_PKT_REQUEST:
  59. set_ack = 0;
  60. /* fall through */
  61. case DCCP_PKT_SYNC:
  62. case DCCP_PKT_SYNCACK:
  63. ackno = dcb->dccpd_seq;
  64. /* fall through */
  65. default:
  66. /*
  67. * Only data packets should come through with skb->sk
  68. * set.
  69. */
  70. WARN_ON(skb->sk);
  71. skb_set_owner_w(skb, sk);
  72. break;
  73. }
  74. dcb->dccpd_seq = dp->dccps_gss;
  75. if (dccp_insert_options(sk, skb)) {
  76. kfree_skb(skb);
  77. return -EPROTO;
  78. }
  79. skb->h.raw = skb_push(skb, dccp_header_size);
  80. dh = dccp_hdr(skb);
  81. /* Build DCCP header and checksum it. */
  82. memset(dh, 0, dccp_header_size);
  83. dh->dccph_type = dcb->dccpd_type;
  84. dh->dccph_sport = inet->sport;
  85. dh->dccph_dport = inet->dport;
  86. dh->dccph_doff = (dccp_header_size + dcb->dccpd_opt_len) / 4;
  87. dh->dccph_ccval = dcb->dccpd_ccval;
  88. /* XXX For now we're using only 48 bits sequence numbers */
  89. dh->dccph_x = 1;
  90. dp->dccps_awh = dp->dccps_gss;
  91. dccp_hdr_set_seq(dh, dp->dccps_gss);
  92. if (set_ack)
  93. dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), ackno);
  94. switch (dcb->dccpd_type) {
  95. case DCCP_PKT_REQUEST:
  96. dccp_hdr_request(skb)->dccph_req_service =
  97. dp->dccps_service;
  98. break;
  99. case DCCP_PKT_RESET:
  100. dccp_hdr_reset(skb)->dccph_reset_code =
  101. dcb->dccpd_reset_code;
  102. break;
  103. }
  104. icsk->icsk_af_ops->send_check(sk, skb->len, skb);
  105. if (set_ack)
  106. dccp_event_ack_sent(sk);
  107. DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
  108. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  109. err = icsk->icsk_af_ops->queue_xmit(skb, 0);
  110. if (err <= 0)
  111. return err;
  112. /* NET_XMIT_CN is special. It does not guarantee,
  113. * that this packet is lost. It tells that device
  114. * is about to start to drop packets or already
  115. * drops some packets of the same priority and
  116. * invokes us to send less aggressively.
  117. */
  118. return err == NET_XMIT_CN ? 0 : err;
  119. }
  120. return -ENOBUFS;
  121. }
  122. unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu)
  123. {
  124. struct inet_connection_sock *icsk = inet_csk(sk);
  125. struct dccp_sock *dp = dccp_sk(sk);
  126. int mss_now = (pmtu - icsk->icsk_af_ops->net_header_len -
  127. sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext));
  128. /* Now subtract optional transport overhead */
  129. mss_now -= icsk->icsk_ext_hdr_len;
  130. /*
  131. * FIXME: this should come from the CCID infrastructure, where, say,
  132. * TFRC will say it wants TIMESTAMPS, ELAPSED time, etc, for now lets
  133. * put a rough estimate for NDP + TIMESTAMP + TIMESTAMP_ECHO + ELAPSED
  134. * TIME + TFRC_OPT_LOSS_EVENT_RATE + TFRC_OPT_RECEIVE_RATE + padding to
  135. * make it a multiple of 4
  136. */
  137. mss_now -= ((5 + 6 + 10 + 6 + 6 + 6 + 3) / 4) * 4;
  138. /* And store cached results */
  139. icsk->icsk_pmtu_cookie = pmtu;
  140. dp->dccps_mss_cache = mss_now;
  141. return mss_now;
  142. }
  143. EXPORT_SYMBOL_GPL(dccp_sync_mss);
  144. void dccp_write_space(struct sock *sk)
  145. {
  146. read_lock(&sk->sk_callback_lock);
  147. if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
  148. wake_up_interruptible(sk->sk_sleep);
  149. /* Should agree with poll, otherwise some programs break */
  150. if (sock_writeable(sk))
  151. sk_wake_async(sk, 2, POLL_OUT);
  152. read_unlock(&sk->sk_callback_lock);
  153. }
  154. /**
  155. * dccp_wait_for_ccid - Wait for ccid to tell us we can send a packet
  156. * @sk: socket to wait for
  157. * @timeo: for how long
  158. */
  159. static int dccp_wait_for_ccid(struct sock *sk, struct sk_buff *skb,
  160. long *timeo)
  161. {
  162. struct dccp_sock *dp = dccp_sk(sk);
  163. DEFINE_WAIT(wait);
  164. long delay;
  165. int rc;
  166. while (1) {
  167. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  168. if (sk->sk_err || (sk->sk_shutdown & SEND_SHUTDOWN))
  169. goto do_error;
  170. if (!*timeo)
  171. goto do_nonblock;
  172. if (signal_pending(current))
  173. goto do_interrupted;
  174. rc = ccid_hc_tx_send_packet(dp->dccps_hc_tx_ccid, sk, skb,
  175. skb->len);
  176. if (rc <= 0)
  177. break;
  178. delay = msecs_to_jiffies(rc);
  179. if (delay > *timeo || delay < 0)
  180. goto do_nonblock;
  181. sk->sk_write_pending++;
  182. release_sock(sk);
  183. *timeo -= schedule_timeout(delay);
  184. lock_sock(sk);
  185. sk->sk_write_pending--;
  186. }
  187. out:
  188. finish_wait(sk->sk_sleep, &wait);
  189. return rc;
  190. do_error:
  191. rc = -EPIPE;
  192. goto out;
  193. do_nonblock:
  194. rc = -EAGAIN;
  195. goto out;
  196. do_interrupted:
  197. rc = sock_intr_errno(*timeo);
  198. goto out;
  199. }
  200. int dccp_write_xmit(struct sock *sk, struct sk_buff *skb, long *timeo)
  201. {
  202. const struct dccp_sock *dp = dccp_sk(sk);
  203. int err = ccid_hc_tx_send_packet(dp->dccps_hc_tx_ccid, sk, skb,
  204. skb->len);
  205. if (err > 0)
  206. err = dccp_wait_for_ccid(sk, skb, timeo);
  207. if (err == 0) {
  208. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  209. const int len = skb->len;
  210. if (sk->sk_state == DCCP_PARTOPEN) {
  211. /* See 8.1.5. Handshake Completion */
  212. inet_csk_schedule_ack(sk);
  213. inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
  214. inet_csk(sk)->icsk_rto,
  215. DCCP_RTO_MAX);
  216. dcb->dccpd_type = DCCP_PKT_DATAACK;
  217. } else if (dccp_ack_pending(sk))
  218. dcb->dccpd_type = DCCP_PKT_DATAACK;
  219. else
  220. dcb->dccpd_type = DCCP_PKT_DATA;
  221. err = dccp_transmit_skb(sk, skb);
  222. ccid_hc_tx_packet_sent(dp->dccps_hc_tx_ccid, sk, 0, len);
  223. } else
  224. kfree_skb(skb);
  225. return err;
  226. }
  227. int dccp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
  228. {
  229. if (inet_csk(sk)->icsk_af_ops->rebuild_header(sk) != 0)
  230. return -EHOSTUNREACH; /* Routing failure or similar. */
  231. return dccp_transmit_skb(sk, (skb_cloned(skb) ?
  232. pskb_copy(skb, GFP_ATOMIC):
  233. skb_clone(skb, GFP_ATOMIC)));
  234. }
  235. struct sk_buff *dccp_make_response(struct sock *sk, struct dst_entry *dst,
  236. struct request_sock *req)
  237. {
  238. struct dccp_hdr *dh;
  239. struct dccp_request_sock *dreq;
  240. const u32 dccp_header_size = sizeof(struct dccp_hdr) +
  241. sizeof(struct dccp_hdr_ext) +
  242. sizeof(struct dccp_hdr_response);
  243. struct sk_buff *skb = sock_wmalloc(sk, sk->sk_prot->max_header, 1,
  244. GFP_ATOMIC);
  245. if (skb == NULL)
  246. return NULL;
  247. /* Reserve space for headers. */
  248. skb_reserve(skb, sk->sk_prot->max_header);
  249. skb->dst = dst_clone(dst);
  250. skb->csum = 0;
  251. dreq = dccp_rsk(req);
  252. DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_RESPONSE;
  253. DCCP_SKB_CB(skb)->dccpd_seq = dreq->dreq_iss;
  254. if (dccp_insert_options(sk, skb)) {
  255. kfree_skb(skb);
  256. return NULL;
  257. }
  258. skb->h.raw = skb_push(skb, dccp_header_size);
  259. dh = dccp_hdr(skb);
  260. memset(dh, 0, dccp_header_size);
  261. dh->dccph_sport = inet_sk(sk)->sport;
  262. dh->dccph_dport = inet_rsk(req)->rmt_port;
  263. dh->dccph_doff = (dccp_header_size +
  264. DCCP_SKB_CB(skb)->dccpd_opt_len) / 4;
  265. dh->dccph_type = DCCP_PKT_RESPONSE;
  266. dh->dccph_x = 1;
  267. dccp_hdr_set_seq(dh, dreq->dreq_iss);
  268. dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dreq->dreq_isr);
  269. dccp_hdr_response(skb)->dccph_resp_service = dreq->dreq_service;
  270. DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
  271. return skb;
  272. }
  273. EXPORT_SYMBOL_GPL(dccp_make_response);
  274. static struct sk_buff *dccp_make_reset(struct sock *sk, struct dst_entry *dst,
  275. const enum dccp_reset_codes code)
  276. {
  277. struct dccp_hdr *dh;
  278. struct dccp_sock *dp = dccp_sk(sk);
  279. const u32 dccp_header_size = sizeof(struct dccp_hdr) +
  280. sizeof(struct dccp_hdr_ext) +
  281. sizeof(struct dccp_hdr_reset);
  282. struct sk_buff *skb = sock_wmalloc(sk, sk->sk_prot->max_header, 1,
  283. GFP_ATOMIC);
  284. if (skb == NULL)
  285. return NULL;
  286. /* Reserve space for headers. */
  287. skb_reserve(skb, sk->sk_prot->max_header);
  288. skb->dst = dst_clone(dst);
  289. skb->csum = 0;
  290. dccp_inc_seqno(&dp->dccps_gss);
  291. DCCP_SKB_CB(skb)->dccpd_reset_code = code;
  292. DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_RESET;
  293. DCCP_SKB_CB(skb)->dccpd_seq = dp->dccps_gss;
  294. if (dccp_insert_options(sk, skb)) {
  295. kfree_skb(skb);
  296. return NULL;
  297. }
  298. skb->h.raw = skb_push(skb, dccp_header_size);
  299. dh = dccp_hdr(skb);
  300. memset(dh, 0, dccp_header_size);
  301. dh->dccph_sport = inet_sk(sk)->sport;
  302. dh->dccph_dport = inet_sk(sk)->dport;
  303. dh->dccph_doff = (dccp_header_size +
  304. DCCP_SKB_CB(skb)->dccpd_opt_len) / 4;
  305. dh->dccph_type = DCCP_PKT_RESET;
  306. dh->dccph_x = 1;
  307. dccp_hdr_set_seq(dh, dp->dccps_gss);
  308. dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dp->dccps_gsr);
  309. dccp_hdr_reset(skb)->dccph_reset_code = code;
  310. inet_csk(sk)->icsk_af_ops->send_check(sk, skb->len, skb);
  311. DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
  312. return skb;
  313. }
  314. int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code)
  315. {
  316. /*
  317. * FIXME: what if rebuild_header fails?
  318. * Should we be doing a rebuild_header here?
  319. */
  320. int err = inet_sk_rebuild_header(sk);
  321. if (err == 0) {
  322. struct sk_buff *skb = dccp_make_reset(sk, sk->sk_dst_cache,
  323. code);
  324. if (skb != NULL) {
  325. memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
  326. err = inet_csk(sk)->icsk_af_ops->queue_xmit(skb, 0);
  327. if (err == NET_XMIT_CN)
  328. err = 0;
  329. }
  330. }
  331. return err;
  332. }
  333. /*
  334. * Do all connect socket setups that can be done AF independent.
  335. */
  336. static inline void dccp_connect_init(struct sock *sk)
  337. {
  338. struct dccp_sock *dp = dccp_sk(sk);
  339. struct dst_entry *dst = __sk_dst_get(sk);
  340. struct inet_connection_sock *icsk = inet_csk(sk);
  341. sk->sk_err = 0;
  342. sock_reset_flag(sk, SOCK_DONE);
  343. dccp_sync_mss(sk, dst_mtu(dst));
  344. dccp_update_gss(sk, dp->dccps_iss);
  345. /*
  346. * SWL and AWL are initially adjusted so that they are not less than
  347. * the initial Sequence Numbers received and sent, respectively:
  348. * SWL := max(GSR + 1 - floor(W/4), ISR),
  349. * AWL := max(GSS - W' + 1, ISS).
  350. * These adjustments MUST be applied only at the beginning of the
  351. * connection.
  352. */
  353. dccp_set_seqno(&dp->dccps_awl, max48(dp->dccps_awl, dp->dccps_iss));
  354. icsk->icsk_retransmits = 0;
  355. }
  356. int dccp_connect(struct sock *sk)
  357. {
  358. struct sk_buff *skb;
  359. struct inet_connection_sock *icsk = inet_csk(sk);
  360. dccp_connect_init(sk);
  361. skb = alloc_skb(sk->sk_prot->max_header, sk->sk_allocation);
  362. if (unlikely(skb == NULL))
  363. return -ENOBUFS;
  364. /* Reserve space for headers. */
  365. skb_reserve(skb, sk->sk_prot->max_header);
  366. DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_REQUEST;
  367. skb->csum = 0;
  368. dccp_skb_entail(sk, skb);
  369. dccp_transmit_skb(sk, skb_clone(skb, GFP_KERNEL));
  370. DCCP_INC_STATS(DCCP_MIB_ACTIVEOPENS);
  371. /* Timer for repeating the REQUEST until an answer. */
  372. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  373. icsk->icsk_rto, DCCP_RTO_MAX);
  374. return 0;
  375. }
  376. EXPORT_SYMBOL_GPL(dccp_connect);
  377. void dccp_send_ack(struct sock *sk)
  378. {
  379. /* If we have been reset, we may not send again. */
  380. if (sk->sk_state != DCCP_CLOSED) {
  381. struct sk_buff *skb = alloc_skb(sk->sk_prot->max_header,
  382. GFP_ATOMIC);
  383. if (skb == NULL) {
  384. inet_csk_schedule_ack(sk);
  385. inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN;
  386. inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
  387. TCP_DELACK_MAX,
  388. DCCP_RTO_MAX);
  389. return;
  390. }
  391. /* Reserve space for headers */
  392. skb_reserve(skb, sk->sk_prot->max_header);
  393. skb->csum = 0;
  394. DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_ACK;
  395. dccp_transmit_skb(sk, skb);
  396. }
  397. }
  398. EXPORT_SYMBOL_GPL(dccp_send_ack);
  399. void dccp_send_delayed_ack(struct sock *sk)
  400. {
  401. struct inet_connection_sock *icsk = inet_csk(sk);
  402. /*
  403. * FIXME: tune this timer. elapsed time fixes the skew, so no problem
  404. * with using 2s, and active senders also piggyback the ACK into a
  405. * DATAACK packet, so this is really for quiescent senders.
  406. */
  407. unsigned long timeout = jiffies + 2 * HZ;
  408. /* Use new timeout only if there wasn't a older one earlier. */
  409. if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) {
  410. /* If delack timer was blocked or is about to expire,
  411. * send ACK now.
  412. *
  413. * FIXME: check the "about to expire" part
  414. */
  415. if (icsk->icsk_ack.blocked) {
  416. dccp_send_ack(sk);
  417. return;
  418. }
  419. if (!time_before(timeout, icsk->icsk_ack.timeout))
  420. timeout = icsk->icsk_ack.timeout;
  421. }
  422. icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
  423. icsk->icsk_ack.timeout = timeout;
  424. sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
  425. }
  426. void dccp_send_sync(struct sock *sk, const u64 seq,
  427. const enum dccp_pkt_type pkt_type)
  428. {
  429. /*
  430. * We are not putting this on the write queue, so
  431. * dccp_transmit_skb() will set the ownership to this
  432. * sock.
  433. */
  434. struct sk_buff *skb = alloc_skb(sk->sk_prot->max_header, GFP_ATOMIC);
  435. if (skb == NULL)
  436. /* FIXME: how to make sure the sync is sent? */
  437. return;
  438. /* Reserve space for headers and prepare control bits. */
  439. skb_reserve(skb, sk->sk_prot->max_header);
  440. skb->csum = 0;
  441. DCCP_SKB_CB(skb)->dccpd_type = pkt_type;
  442. DCCP_SKB_CB(skb)->dccpd_seq = seq;
  443. dccp_transmit_skb(sk, skb);
  444. }
  445. EXPORT_SYMBOL_GPL(dccp_send_sync);
  446. /*
  447. * Send a DCCP_PKT_CLOSE/CLOSEREQ. The caller locks the socket for us. This
  448. * cannot be allowed to fail queueing a DCCP_PKT_CLOSE/CLOSEREQ frame under
  449. * any circumstances.
  450. */
  451. void dccp_send_close(struct sock *sk, const int active)
  452. {
  453. struct dccp_sock *dp = dccp_sk(sk);
  454. struct sk_buff *skb;
  455. const gfp_t prio = active ? GFP_KERNEL : GFP_ATOMIC;
  456. skb = alloc_skb(sk->sk_prot->max_header, prio);
  457. if (skb == NULL)
  458. return;
  459. /* Reserve space for headers and prepare control bits. */
  460. skb_reserve(skb, sk->sk_prot->max_header);
  461. skb->csum = 0;
  462. DCCP_SKB_CB(skb)->dccpd_type = dp->dccps_role == DCCP_ROLE_CLIENT ?
  463. DCCP_PKT_CLOSE : DCCP_PKT_CLOSEREQ;
  464. if (active) {
  465. dccp_skb_entail(sk, skb);
  466. dccp_transmit_skb(sk, skb_clone(skb, prio));
  467. } else
  468. dccp_transmit_skb(sk, skb);
  469. }