output.c 14 KB

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