output.c 13 KB

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