output.c 13 KB

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