output.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. /*
  42. * FIXME: study DCCP_PKT_SYNC[ACK] to see what is the right thing
  43. * to do here...
  44. */
  45. dccp_inc_seqno(&dp->dccps_gss);
  46. dcb->dccpd_seq = dp->dccps_gss;
  47. dccp_insert_options(sk, skb);
  48. switch (dcb->dccpd_type) {
  49. case DCCP_PKT_DATA:
  50. set_ack = 0;
  51. break;
  52. case DCCP_PKT_SYNC:
  53. case DCCP_PKT_SYNCACK:
  54. ackno = dcb->dccpd_seq;
  55. break;
  56. }
  57. skb->h.raw = skb_push(skb, dccp_header_size);
  58. dh = dccp_hdr(skb);
  59. /* Data packets are not cloned as they are never retransmitted */
  60. if (skb_cloned(skb))
  61. skb_set_owner_w(skb, sk);
  62. /* Build DCCP header and checksum it. */
  63. memset(dh, 0, dccp_header_size);
  64. dh->dccph_type = dcb->dccpd_type;
  65. dh->dccph_sport = inet->sport;
  66. dh->dccph_dport = inet->dport;
  67. dh->dccph_doff = (dccp_header_size + dcb->dccpd_opt_len) / 4;
  68. dh->dccph_ccval = dcb->dccpd_ccval;
  69. /* XXX For now we're using only 48 bits sequence numbers */
  70. dh->dccph_x = 1;
  71. dp->dccps_awh = dp->dccps_gss;
  72. dccp_hdr_set_seq(dh, dp->dccps_gss);
  73. if (set_ack)
  74. dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), ackno);
  75. switch (dcb->dccpd_type) {
  76. case DCCP_PKT_REQUEST:
  77. dccp_hdr_request(skb)->dccph_req_service = dcb->dccpd_service;
  78. break;
  79. case DCCP_PKT_RESET:
  80. dccp_hdr_reset(skb)->dccph_reset_code = dcb->dccpd_reset_code;
  81. break;
  82. }
  83. dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr,
  84. inet->daddr);
  85. if (dcb->dccpd_type == DCCP_PKT_ACK ||
  86. dcb->dccpd_type == DCCP_PKT_DATAACK)
  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 IPv6.
  108. * mss_now = pmtu - tp->af_specific->net_header_len - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext);
  109. */
  110. mss_now = pmtu - sizeof(struct iphdr) - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext);
  111. /* Now subtract optional transport overhead */
  112. mss_now -= dp->dccps_ext_header_len;
  113. /*
  114. * FIXME: this should come from the CCID infrastructure, where, say,
  115. * TFRC will say it wants TIMESTAMPS, ELAPSED time, etc, for now lets
  116. * put a rough estimate for NDP + TIMESTAMP + TIMESTAMP_ECHO + ELAPSED
  117. * TIME + TFRC_OPT_LOSS_EVENT_RATE + TFRC_OPT_RECEIVE_RATE + padding to
  118. * make it a multiple of 4
  119. */
  120. mss_now -= ((5 + 6 + 10 + 6 + 6 + 6 + 3) / 4) * 4;
  121. /* And store cached results */
  122. dp->dccps_pmtu_cookie = pmtu;
  123. dp->dccps_mss_cache = mss_now;
  124. return mss_now;
  125. }
  126. int dccp_write_xmit(struct sock *sk, struct sk_buff *skb, const int len)
  127. {
  128. const struct dccp_sock *dp = dccp_sk(sk);
  129. int err = ccid_hc_tx_send_packet(dp->dccps_hc_tx_ccid, sk, skb, len);
  130. if (err == 0) {
  131. const struct dccp_ackpkts *ap = dp->dccps_hc_rx_ackpkts;
  132. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  133. if (sk->sk_state == DCCP_PARTOPEN) {
  134. /* See 8.1.5. Handshake Completion */
  135. inet_csk_schedule_ack(sk);
  136. inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
  137. inet_csk(sk)->icsk_rto,
  138. DCCP_RTO_MAX);
  139. dcb->dccpd_type = DCCP_PKT_DATAACK;
  140. /*
  141. * FIXME: we really should have a
  142. * dccps_ack_pending or use icsk.
  143. */
  144. } else if (inet_csk_ack_scheduled(sk) ||
  145. (dp->dccps_options.dccpo_send_ack_vector &&
  146. ap->dccpap_buf_ackno != DCCP_MAX_SEQNO + 1 &&
  147. ap->dccpap_ack_seqno == DCCP_MAX_SEQNO + 1))
  148. dcb->dccpd_type = DCCP_PKT_DATAACK;
  149. else
  150. dcb->dccpd_type = DCCP_PKT_DATA;
  151. err = dccp_transmit_skb(sk, skb);
  152. ccid_hc_tx_packet_sent(dp->dccps_hc_tx_ccid, sk, 0, len);
  153. }
  154. return err;
  155. }
  156. int dccp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
  157. {
  158. if (inet_sk_rebuild_header(sk) != 0)
  159. return -EHOSTUNREACH; /* Routing failure or similar. */
  160. return dccp_transmit_skb(sk, (skb_cloned(skb) ?
  161. pskb_copy(skb, GFP_ATOMIC):
  162. skb_clone(skb, GFP_ATOMIC)));
  163. }
  164. struct sk_buff *dccp_make_response(struct sock *sk, struct dst_entry *dst,
  165. struct request_sock *req)
  166. {
  167. struct dccp_hdr *dh;
  168. const int dccp_header_size = sizeof(struct dccp_hdr) +
  169. sizeof(struct dccp_hdr_ext) +
  170. sizeof(struct dccp_hdr_response);
  171. struct sk_buff *skb = sock_wmalloc(sk, MAX_HEADER + DCCP_MAX_OPT_LEN +
  172. dccp_header_size, 1,
  173. GFP_ATOMIC);
  174. if (skb == NULL)
  175. return NULL;
  176. /* Reserve space for headers. */
  177. skb_reserve(skb, MAX_HEADER + DCCP_MAX_OPT_LEN + dccp_header_size);
  178. skb->dst = dst_clone(dst);
  179. skb->csum = 0;
  180. DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_RESPONSE;
  181. DCCP_SKB_CB(skb)->dccpd_seq = dccp_rsk(req)->dreq_iss;
  182. dccp_insert_options(sk, skb);
  183. skb->h.raw = skb_push(skb, dccp_header_size);
  184. dh = dccp_hdr(skb);
  185. memset(dh, 0, dccp_header_size);
  186. dh->dccph_sport = inet_sk(sk)->sport;
  187. dh->dccph_dport = inet_rsk(req)->rmt_port;
  188. dh->dccph_doff = (dccp_header_size + DCCP_SKB_CB(skb)->dccpd_opt_len) / 4;
  189. dh->dccph_type = DCCP_PKT_RESPONSE;
  190. dh->dccph_x = 1;
  191. dccp_hdr_set_seq(dh, dccp_rsk(req)->dreq_iss);
  192. dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dccp_rsk(req)->dreq_isr);
  193. dh->dccph_checksum = dccp_v4_checksum(skb, inet_rsk(req)->loc_addr,
  194. inet_rsk(req)->rmt_addr);
  195. DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
  196. return skb;
  197. }
  198. struct sk_buff *dccp_make_reset(struct sock *sk, struct dst_entry *dst,
  199. const enum dccp_reset_codes code)
  200. {
  201. struct dccp_hdr *dh;
  202. struct dccp_sock *dp = dccp_sk(sk);
  203. const int dccp_header_size = sizeof(struct dccp_hdr) +
  204. sizeof(struct dccp_hdr_ext) +
  205. sizeof(struct dccp_hdr_reset);
  206. struct sk_buff *skb = sock_wmalloc(sk, MAX_HEADER + DCCP_MAX_OPT_LEN +
  207. dccp_header_size, 1,
  208. GFP_ATOMIC);
  209. if (skb == NULL)
  210. return NULL;
  211. /* Reserve space for headers. */
  212. skb_reserve(skb, MAX_HEADER + DCCP_MAX_OPT_LEN + dccp_header_size);
  213. skb->dst = dst_clone(dst);
  214. skb->csum = 0;
  215. dccp_inc_seqno(&dp->dccps_gss);
  216. DCCP_SKB_CB(skb)->dccpd_reset_code = code;
  217. DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_RESET;
  218. DCCP_SKB_CB(skb)->dccpd_seq = dp->dccps_gss;
  219. dccp_insert_options(sk, skb);
  220. skb->h.raw = skb_push(skb, dccp_header_size);
  221. dh = dccp_hdr(skb);
  222. memset(dh, 0, dccp_header_size);
  223. dh->dccph_sport = inet_sk(sk)->sport;
  224. dh->dccph_dport = inet_sk(sk)->dport;
  225. dh->dccph_doff = (dccp_header_size + DCCP_SKB_CB(skb)->dccpd_opt_len) / 4;
  226. dh->dccph_type = DCCP_PKT_RESET;
  227. dh->dccph_x = 1;
  228. dccp_hdr_set_seq(dh, dp->dccps_gss);
  229. dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dp->dccps_gsr);
  230. dccp_hdr_reset(skb)->dccph_reset_code = code;
  231. dh->dccph_checksum = dccp_v4_checksum(skb, inet_sk(sk)->saddr,
  232. inet_sk(sk)->daddr);
  233. DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
  234. return skb;
  235. }
  236. /*
  237. * Do all connect socket setups that can be done AF independent.
  238. */
  239. static inline void dccp_connect_init(struct sock *sk)
  240. {
  241. struct dst_entry *dst = __sk_dst_get(sk);
  242. struct inet_connection_sock *icsk = inet_csk(sk);
  243. sk->sk_err = 0;
  244. sock_reset_flag(sk, SOCK_DONE);
  245. dccp_sync_mss(sk, dst_mtu(dst));
  246. /*
  247. * FIXME: set dp->{dccps_swh,dccps_swl}, with
  248. * something like dccp_inc_seq
  249. */
  250. icsk->icsk_retransmits = 0;
  251. }
  252. int dccp_connect(struct sock *sk)
  253. {
  254. struct sk_buff *skb;
  255. struct inet_connection_sock *icsk = inet_csk(sk);
  256. dccp_connect_init(sk);
  257. skb = alloc_skb(MAX_DCCP_HEADER + 15, sk->sk_allocation);
  258. if (unlikely(skb == NULL))
  259. return -ENOBUFS;
  260. /* Reserve space for headers. */
  261. skb_reserve(skb, MAX_DCCP_HEADER);
  262. DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_REQUEST;
  263. /* FIXME: set service to something meaningful, coming
  264. * from userspace*/
  265. DCCP_SKB_CB(skb)->dccpd_service = 0;
  266. skb->csum = 0;
  267. skb_set_owner_w(skb, sk);
  268. BUG_TRAP(sk->sk_send_head == NULL);
  269. sk->sk_send_head = skb;
  270. dccp_transmit_skb(sk, skb_clone(skb, GFP_KERNEL));
  271. DCCP_INC_STATS(DCCP_MIB_ACTIVEOPENS);
  272. /* Timer for repeating the REQUEST until an answer. */
  273. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  274. icsk->icsk_rto, DCCP_RTO_MAX);
  275. return 0;
  276. }
  277. void dccp_send_ack(struct sock *sk)
  278. {
  279. /* If we have been reset, we may not send again. */
  280. if (sk->sk_state != DCCP_CLOSED) {
  281. struct sk_buff *skb = alloc_skb(MAX_DCCP_HEADER, GFP_ATOMIC);
  282. if (skb == NULL) {
  283. inet_csk_schedule_ack(sk);
  284. inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN;
  285. inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, TCP_DELACK_MAX, TCP_RTO_MAX);
  286. return;
  287. }
  288. /* Reserve space for headers */
  289. skb_reserve(skb, MAX_DCCP_HEADER);
  290. skb->csum = 0;
  291. DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_ACK;
  292. skb_set_owner_w(skb, sk);
  293. dccp_transmit_skb(sk, skb);
  294. }
  295. }
  296. EXPORT_SYMBOL_GPL(dccp_send_ack);
  297. void dccp_send_delayed_ack(struct sock *sk)
  298. {
  299. struct inet_connection_sock *icsk = inet_csk(sk);
  300. /*
  301. * FIXME: tune this timer. elapsed time fixes the skew, so no problem
  302. * with using 2s, and active senders also piggyback the ACK into a
  303. * DATAACK packet, so this is really for quiescent senders.
  304. */
  305. unsigned long timeout = jiffies + 2 * HZ;
  306. /* Use new timeout only if there wasn't a older one earlier. */
  307. if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) {
  308. /* If delack timer was blocked or is about to expire,
  309. * send ACK now.
  310. *
  311. * FIXME: check the "about to expire" part
  312. */
  313. if (icsk->icsk_ack.blocked) {
  314. dccp_send_ack(sk);
  315. return;
  316. }
  317. if (!time_before(timeout, icsk->icsk_ack.timeout))
  318. timeout = icsk->icsk_ack.timeout;
  319. }
  320. icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
  321. icsk->icsk_ack.timeout = timeout;
  322. sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
  323. }
  324. void dccp_send_sync(struct sock *sk, u64 seq)
  325. {
  326. /*
  327. * We are not putting this on the write queue, so
  328. * dccp_transmit_skb() will set the ownership to this
  329. * sock.
  330. */
  331. struct sk_buff *skb = alloc_skb(MAX_DCCP_HEADER, GFP_ATOMIC);
  332. if (skb == NULL)
  333. /* FIXME: how to make sure the sync is sent? */
  334. return;
  335. /* Reserve space for headers and prepare control bits. */
  336. skb_reserve(skb, MAX_DCCP_HEADER);
  337. skb->csum = 0;
  338. DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_SYNC;
  339. DCCP_SKB_CB(skb)->dccpd_seq = seq;
  340. skb_set_owner_w(skb, sk);
  341. dccp_transmit_skb(sk, skb);
  342. }
  343. /* Send a DCCP_PKT_CLOSE/CLOSEREQ. The caller locks the socket for us. This cannot be
  344. * allowed to fail queueing a DCCP_PKT_CLOSE/CLOSEREQ frame under any circumstances.
  345. */
  346. void dccp_send_close(struct sock *sk)
  347. {
  348. struct dccp_sock *dp = dccp_sk(sk);
  349. struct sk_buff *skb;
  350. /* Socket is locked, keep trying until memory is available. */
  351. for (;;) {
  352. skb = alloc_skb(sk->sk_prot->max_header, GFP_KERNEL);
  353. if (skb != NULL)
  354. break;
  355. yield();
  356. }
  357. /* Reserve space for headers and prepare control bits. */
  358. skb_reserve(skb, sk->sk_prot->max_header);
  359. skb->csum = 0;
  360. DCCP_SKB_CB(skb)->dccpd_type = dp->dccps_role == DCCP_ROLE_CLIENT ? DCCP_PKT_CLOSE : DCCP_PKT_CLOSEREQ;
  361. skb_set_owner_w(skb, sk);
  362. dccp_transmit_skb(sk, skb);
  363. ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
  364. ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
  365. }