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