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