[PATCH] binfmt: fix uaccess handling
[linux-2.6/openmoko-kernel/knife-kernel.git] / net / dccp / input.c
blob7371a2f3acf4f2826f4b47124a56fc3d0879af37
1 /*
2 * net/dccp/input.c
3 *
4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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.
13 #include <linux/dccp.h>
14 #include <linux/skbuff.h>
16 #include <net/sock.h>
18 #include "ackvec.h"
19 #include "ccid.h"
20 #include "dccp.h"
22 static void dccp_fin(struct sock *sk, struct sk_buff *skb)
24 sk->sk_shutdown |= RCV_SHUTDOWN;
25 sock_set_flag(sk, SOCK_DONE);
26 __skb_pull(skb, dccp_hdr(skb)->dccph_doff * 4);
27 __skb_queue_tail(&sk->sk_receive_queue, skb);
28 skb_set_owner_r(skb, sk);
29 sk->sk_data_ready(sk, 0);
32 static void dccp_rcv_close(struct sock *sk, struct sk_buff *skb)
34 dccp_send_reset(sk, DCCP_RESET_CODE_CLOSED);
35 dccp_fin(sk, skb);
36 dccp_set_state(sk, DCCP_CLOSED);
37 sk_wake_async(sk, 1, POLL_HUP);
40 static void dccp_rcv_closereq(struct sock *sk, struct sk_buff *skb)
43 * Step 7: Check for unexpected packet types
44 * If (S.is_server and P.type == CloseReq)
45 * Send Sync packet acknowledging P.seqno
46 * Drop packet and return
48 if (dccp_sk(sk)->dccps_role != DCCP_ROLE_CLIENT) {
49 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
50 return;
53 if (sk->sk_state != DCCP_CLOSING)
54 dccp_set_state(sk, DCCP_CLOSING);
55 dccp_send_close(sk, 0);
58 static void dccp_event_ack_recv(struct sock *sk, struct sk_buff *skb)
60 struct dccp_sock *dp = dccp_sk(sk);
62 if (dccp_msk(sk)->dccpms_send_ack_vector)
63 dccp_ackvec_check_rcv_ackno(dp->dccps_hc_rx_ackvec, sk,
64 DCCP_SKB_CB(skb)->dccpd_ack_seq);
67 static int dccp_check_seqno(struct sock *sk, struct sk_buff *skb)
69 const struct dccp_hdr *dh = dccp_hdr(skb);
70 struct dccp_sock *dp = dccp_sk(sk);
71 u64 lswl, lawl;
74 * Step 5: Prepare sequence numbers for Sync
75 * If P.type == Sync or P.type == SyncAck,
76 * If S.AWL <= P.ackno <= S.AWH and P.seqno >= S.SWL,
77 * / * P is valid, so update sequence number variables
78 * accordingly. After this update, P will pass the tests
79 * in Step 6. A SyncAck is generated if necessary in
80 * Step 15 * /
81 * Update S.GSR, S.SWL, S.SWH
82 * Otherwise,
83 * Drop packet and return
85 if (dh->dccph_type == DCCP_PKT_SYNC ||
86 dh->dccph_type == DCCP_PKT_SYNCACK) {
87 if (between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
88 dp->dccps_awl, dp->dccps_awh) &&
89 !before48(DCCP_SKB_CB(skb)->dccpd_seq, dp->dccps_swl))
90 dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
91 else
92 return -1;
96 * Step 6: Check sequence numbers
97 * Let LSWL = S.SWL and LAWL = S.AWL
98 * If P.type == CloseReq or P.type == Close or P.type == Reset,
99 * LSWL := S.GSR + 1, LAWL := S.GAR
100 * If LSWL <= P.seqno <= S.SWH
101 * and (P.ackno does not exist or LAWL <= P.ackno <= S.AWH),
102 * Update S.GSR, S.SWL, S.SWH
103 * If P.type != Sync,
104 * Update S.GAR
105 * Otherwise,
106 * Send Sync packet acknowledging P.seqno
107 * Drop packet and return
109 lswl = dp->dccps_swl;
110 lawl = dp->dccps_awl;
112 if (dh->dccph_type == DCCP_PKT_CLOSEREQ ||
113 dh->dccph_type == DCCP_PKT_CLOSE ||
114 dh->dccph_type == DCCP_PKT_RESET) {
115 lswl = dp->dccps_gsr;
116 dccp_inc_seqno(&lswl);
117 lawl = dp->dccps_gar;
120 if (between48(DCCP_SKB_CB(skb)->dccpd_seq, lswl, dp->dccps_swh) &&
121 (DCCP_SKB_CB(skb)->dccpd_ack_seq == DCCP_PKT_WITHOUT_ACK_SEQ ||
122 between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
123 lawl, dp->dccps_awh))) {
124 dccp_update_gsr(sk, DCCP_SKB_CB(skb)->dccpd_seq);
126 if (dh->dccph_type != DCCP_PKT_SYNC &&
127 (DCCP_SKB_CB(skb)->dccpd_ack_seq !=
128 DCCP_PKT_WITHOUT_ACK_SEQ))
129 dp->dccps_gar = DCCP_SKB_CB(skb)->dccpd_ack_seq;
130 } else {
131 DCCP_WARN("DCCP: Step 6 failed for %s packet, "
132 "(LSWL(%llu) <= P.seqno(%llu) <= S.SWH(%llu)) and "
133 "(P.ackno %s or LAWL(%llu) <= P.ackno(%llu) <= S.AWH(%llu), "
134 "sending SYNC...\n", dccp_packet_name(dh->dccph_type),
135 (unsigned long long) lswl,
136 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq,
137 (unsigned long long) dp->dccps_swh,
138 (DCCP_SKB_CB(skb)->dccpd_ack_seq ==
139 DCCP_PKT_WITHOUT_ACK_SEQ) ? "doesn't exist" : "exists",
140 (unsigned long long) lawl,
141 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_ack_seq,
142 (unsigned long long) dp->dccps_awh);
143 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq, DCCP_PKT_SYNC);
144 return -1;
147 return 0;
150 static int __dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
151 const struct dccp_hdr *dh, const unsigned len)
153 struct dccp_sock *dp = dccp_sk(sk);
155 switch (dccp_hdr(skb)->dccph_type) {
156 case DCCP_PKT_DATAACK:
157 case DCCP_PKT_DATA:
159 * FIXME: check if sk_receive_queue is full, schedule DATA_DROPPED
160 * option if it is.
162 __skb_pull(skb, dh->dccph_doff * 4);
163 __skb_queue_tail(&sk->sk_receive_queue, skb);
164 skb_set_owner_r(skb, sk);
165 sk->sk_data_ready(sk, 0);
166 return 0;
167 case DCCP_PKT_ACK:
168 goto discard;
169 case DCCP_PKT_RESET:
171 * Step 9: Process Reset
172 * If P.type == Reset,
173 * Tear down connection
174 * S.state := TIMEWAIT
175 * Set TIMEWAIT timer
176 * Drop packet and return
178 dccp_fin(sk, skb);
179 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
180 return 0;
181 case DCCP_PKT_CLOSEREQ:
182 dccp_rcv_closereq(sk, skb);
183 goto discard;
184 case DCCP_PKT_CLOSE:
185 dccp_rcv_close(sk, skb);
186 return 0;
187 case DCCP_PKT_REQUEST:
188 /* Step 7
189 * or (S.is_server and P.type == Response)
190 * or (S.is_client and P.type == Request)
191 * or (S.state >= OPEN and P.type == Request
192 * and P.seqno >= S.OSR)
193 * or (S.state >= OPEN and P.type == Response
194 * and P.seqno >= S.OSR)
195 * or (S.state == RESPOND and P.type == Data),
196 * Send Sync packet acknowledging P.seqno
197 * Drop packet and return
199 if (dp->dccps_role != DCCP_ROLE_LISTEN)
200 goto send_sync;
201 goto check_seq;
202 case DCCP_PKT_RESPONSE:
203 if (dp->dccps_role != DCCP_ROLE_CLIENT)
204 goto send_sync;
205 check_seq:
206 if (!before48(DCCP_SKB_CB(skb)->dccpd_seq, dp->dccps_osr)) {
207 send_sync:
208 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
209 DCCP_PKT_SYNC);
211 break;
212 case DCCP_PKT_SYNC:
213 dccp_send_sync(sk, DCCP_SKB_CB(skb)->dccpd_seq,
214 DCCP_PKT_SYNCACK);
216 * From RFC 4340, sec. 5.7
218 * As with DCCP-Ack packets, DCCP-Sync and DCCP-SyncAck packets
219 * MAY have non-zero-length application data areas, whose
220 * contents receivers MUST ignore.
222 goto discard;
225 DCCP_INC_STATS_BH(DCCP_MIB_INERRS);
226 discard:
227 __kfree_skb(skb);
228 return 0;
231 int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
232 const struct dccp_hdr *dh, const unsigned len)
234 struct dccp_sock *dp = dccp_sk(sk);
236 if (dccp_check_seqno(sk, skb))
237 goto discard;
239 if (dccp_parse_options(sk, skb))
240 goto discard;
242 if (DCCP_SKB_CB(skb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
243 dccp_event_ack_recv(sk, skb);
245 if (dccp_msk(sk)->dccpms_send_ack_vector &&
246 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
247 DCCP_SKB_CB(skb)->dccpd_seq,
248 DCCP_ACKVEC_STATE_RECEIVED))
249 goto discard;
251 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
252 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
254 return __dccp_rcv_established(sk, skb, dh, len);
255 discard:
256 __kfree_skb(skb);
257 return 0;
260 EXPORT_SYMBOL_GPL(dccp_rcv_established);
262 static int dccp_rcv_request_sent_state_process(struct sock *sk,
263 struct sk_buff *skb,
264 const struct dccp_hdr *dh,
265 const unsigned len)
268 * Step 4: Prepare sequence numbers in REQUEST
269 * If S.state == REQUEST,
270 * If (P.type == Response or P.type == Reset)
271 * and S.AWL <= P.ackno <= S.AWH,
272 * / * Set sequence number variables corresponding to the
273 * other endpoint, so P will pass the tests in Step 6 * /
274 * Set S.GSR, S.ISR, S.SWL, S.SWH
275 * / * Response processing continues in Step 10; Reset
276 * processing continues in Step 9 * /
278 if (dh->dccph_type == DCCP_PKT_RESPONSE) {
279 const struct inet_connection_sock *icsk = inet_csk(sk);
280 struct dccp_sock *dp = dccp_sk(sk);
282 /* Stop the REQUEST timer */
283 inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
284 BUG_TRAP(sk->sk_send_head != NULL);
285 __kfree_skb(sk->sk_send_head);
286 sk->sk_send_head = NULL;
288 if (!between48(DCCP_SKB_CB(skb)->dccpd_ack_seq,
289 dp->dccps_awl, dp->dccps_awh)) {
290 dccp_pr_debug("invalid ackno: S.AWL=%llu, "
291 "P.ackno=%llu, S.AWH=%llu \n",
292 (unsigned long long)dp->dccps_awl,
293 (unsigned long long)DCCP_SKB_CB(skb)->dccpd_ack_seq,
294 (unsigned long long)dp->dccps_awh);
295 goto out_invalid_packet;
298 if (dccp_parse_options(sk, skb))
299 goto out_invalid_packet;
301 if (dccp_msk(sk)->dccpms_send_ack_vector &&
302 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
303 DCCP_SKB_CB(skb)->dccpd_seq,
304 DCCP_ACKVEC_STATE_RECEIVED))
305 goto out_invalid_packet; /* FIXME: change error code */
307 dp->dccps_isr = DCCP_SKB_CB(skb)->dccpd_seq;
308 dccp_update_gsr(sk, dp->dccps_isr);
310 * SWL and AWL are initially adjusted so that they are not less than
311 * the initial Sequence Numbers received and sent, respectively:
312 * SWL := max(GSR + 1 - floor(W/4), ISR),
313 * AWL := max(GSS - W' + 1, ISS).
314 * These adjustments MUST be applied only at the beginning of the
315 * connection.
317 * AWL was adjusted in dccp_v4_connect -acme
319 dccp_set_seqno(&dp->dccps_swl,
320 max48(dp->dccps_swl, dp->dccps_isr));
322 dccp_sync_mss(sk, icsk->icsk_pmtu_cookie);
325 * Step 10: Process REQUEST state (second part)
326 * If S.state == REQUEST,
327 * / * If we get here, P is a valid Response from the
328 * server (see Step 4), and we should move to
329 * PARTOPEN state. PARTOPEN means send an Ack,
330 * don't send Data packets, retransmit Acks
331 * periodically, and always include any Init Cookie
332 * from the Response * /
333 * S.state := PARTOPEN
334 * Set PARTOPEN timer
335 * Continue with S.state == PARTOPEN
336 * / * Step 12 will send the Ack completing the
337 * three-way handshake * /
339 dccp_set_state(sk, DCCP_PARTOPEN);
341 /* Make sure socket is routed, for correct metrics. */
342 icsk->icsk_af_ops->rebuild_header(sk);
344 if (!sock_flag(sk, SOCK_DEAD)) {
345 sk->sk_state_change(sk);
346 sk_wake_async(sk, 0, POLL_OUT);
349 if (sk->sk_write_pending || icsk->icsk_ack.pingpong ||
350 icsk->icsk_accept_queue.rskq_defer_accept) {
351 /* Save one ACK. Data will be ready after
352 * several ticks, if write_pending is set.
354 * It may be deleted, but with this feature tcpdumps
355 * look so _wonderfully_ clever, that I was not able
356 * to stand against the temptation 8) --ANK
359 * OK, in DCCP we can as well do a similar trick, its
360 * even in the draft, but there is no need for us to
361 * schedule an ack here, as dccp_sendmsg does this for
362 * us, also stated in the draft. -acme
364 __kfree_skb(skb);
365 return 0;
367 dccp_send_ack(sk);
368 return -1;
371 out_invalid_packet:
372 /* dccp_v4_do_rcv will send a reset */
373 DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
374 return 1;
377 static int dccp_rcv_respond_partopen_state_process(struct sock *sk,
378 struct sk_buff *skb,
379 const struct dccp_hdr *dh,
380 const unsigned len)
382 int queued = 0;
384 switch (dh->dccph_type) {
385 case DCCP_PKT_RESET:
386 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
387 break;
388 case DCCP_PKT_DATA:
389 if (sk->sk_state == DCCP_RESPOND)
390 break;
391 case DCCP_PKT_DATAACK:
392 case DCCP_PKT_ACK:
394 * FIXME: we should be reseting the PARTOPEN (DELACK) timer
395 * here but only if we haven't used the DELACK timer for
396 * something else, like sending a delayed ack for a TIMESTAMP
397 * echo, etc, for now were not clearing it, sending an extra
398 * ACK when there is nothing else to do in DELACK is not a big
399 * deal after all.
402 /* Stop the PARTOPEN timer */
403 if (sk->sk_state == DCCP_PARTOPEN)
404 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
406 dccp_sk(sk)->dccps_osr = DCCP_SKB_CB(skb)->dccpd_seq;
407 dccp_set_state(sk, DCCP_OPEN);
409 if (dh->dccph_type == DCCP_PKT_DATAACK ||
410 dh->dccph_type == DCCP_PKT_DATA) {
411 __dccp_rcv_established(sk, skb, dh, len);
412 queued = 1; /* packet was queued
413 (by __dccp_rcv_established) */
415 break;
418 return queued;
421 int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
422 struct dccp_hdr *dh, unsigned len)
424 struct dccp_sock *dp = dccp_sk(sk);
425 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
426 const int old_state = sk->sk_state;
427 int queued = 0;
430 * Step 3: Process LISTEN state
432 * If S.state == LISTEN,
433 * If P.type == Request or P contains a valid Init Cookie option,
434 * (* Must scan the packet's options to check for Init
435 * Cookies. Only Init Cookies are processed here,
436 * however; other options are processed in Step 8. This
437 * scan need only be performed if the endpoint uses Init
438 * Cookies *)
439 * (* Generate a new socket and switch to that socket *)
440 * Set S := new socket for this port pair
441 * S.state = RESPOND
442 * Choose S.ISS (initial seqno) or set from Init Cookies
443 * Initialize S.GAR := S.ISS
444 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
445 * Cookies Continue with S.state == RESPOND
446 * (* A Response packet will be generated in Step 11 *)
447 * Otherwise,
448 * Generate Reset(No Connection) unless P.type == Reset
449 * Drop packet and return
451 if (sk->sk_state == DCCP_LISTEN) {
452 if (dh->dccph_type == DCCP_PKT_REQUEST) {
453 if (inet_csk(sk)->icsk_af_ops->conn_request(sk,
454 skb) < 0)
455 return 1;
457 /* FIXME: do congestion control initialization */
458 goto discard;
460 if (dh->dccph_type == DCCP_PKT_RESET)
461 goto discard;
463 /* Caller (dccp_v4_do_rcv) will send Reset */
464 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
465 return 1;
468 if (sk->sk_state != DCCP_REQUESTING) {
469 if (dccp_check_seqno(sk, skb))
470 goto discard;
473 * Step 8: Process options and mark acknowledgeable
475 if (dccp_parse_options(sk, skb))
476 goto discard;
478 if (dcb->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
479 dccp_event_ack_recv(sk, skb);
481 if (dccp_msk(sk)->dccpms_send_ack_vector &&
482 dccp_ackvec_add(dp->dccps_hc_rx_ackvec, sk,
483 DCCP_SKB_CB(skb)->dccpd_seq,
484 DCCP_ACKVEC_STATE_RECEIVED))
485 goto discard;
487 ccid_hc_rx_packet_recv(dp->dccps_hc_rx_ccid, sk, skb);
488 ccid_hc_tx_packet_recv(dp->dccps_hc_tx_ccid, sk, skb);
492 * Step 9: Process Reset
493 * If P.type == Reset,
494 * Tear down connection
495 * S.state := TIMEWAIT
496 * Set TIMEWAIT timer
497 * Drop packet and return
499 if (dh->dccph_type == DCCP_PKT_RESET) {
501 * Queue the equivalent of TCP fin so that dccp_recvmsg
502 * exits the loop
504 dccp_fin(sk, skb);
505 dccp_time_wait(sk, DCCP_TIME_WAIT, 0);
506 return 0;
508 * Step 7: Check for unexpected packet types
509 * If (S.is_server and P.type == CloseReq)
510 * or (S.is_server and P.type == Response)
511 * or (S.is_client and P.type == Request)
512 * or (S.state == RESPOND and P.type == Data),
513 * Send Sync packet acknowledging P.seqno
514 * Drop packet and return
516 } else if ((dp->dccps_role != DCCP_ROLE_CLIENT &&
517 (dh->dccph_type == DCCP_PKT_RESPONSE ||
518 dh->dccph_type == DCCP_PKT_CLOSEREQ)) ||
519 (dp->dccps_role == DCCP_ROLE_CLIENT &&
520 dh->dccph_type == DCCP_PKT_REQUEST) ||
521 (sk->sk_state == DCCP_RESPOND &&
522 dh->dccph_type == DCCP_PKT_DATA)) {
523 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNC);
524 goto discard;
525 } else if (dh->dccph_type == DCCP_PKT_CLOSEREQ) {
526 dccp_rcv_closereq(sk, skb);
527 goto discard;
528 } else if (dh->dccph_type == DCCP_PKT_CLOSE) {
529 dccp_rcv_close(sk, skb);
530 return 0;
533 if (unlikely(dh->dccph_type == DCCP_PKT_SYNC)) {
534 dccp_send_sync(sk, dcb->dccpd_seq, DCCP_PKT_SYNCACK);
535 goto discard;
538 switch (sk->sk_state) {
539 case DCCP_CLOSED:
540 dcb->dccpd_reset_code = DCCP_RESET_CODE_NO_CONNECTION;
541 return 1;
543 case DCCP_REQUESTING:
544 /* FIXME: do congestion control initialization */
546 queued = dccp_rcv_request_sent_state_process(sk, skb, dh, len);
547 if (queued >= 0)
548 return queued;
550 __kfree_skb(skb);
551 return 0;
553 case DCCP_RESPOND:
554 case DCCP_PARTOPEN:
555 queued = dccp_rcv_respond_partopen_state_process(sk, skb,
556 dh, len);
557 break;
560 if (dh->dccph_type == DCCP_PKT_ACK ||
561 dh->dccph_type == DCCP_PKT_DATAACK) {
562 switch (old_state) {
563 case DCCP_PARTOPEN:
564 sk->sk_state_change(sk);
565 sk_wake_async(sk, 0, POLL_OUT);
566 break;
570 if (!queued) {
571 discard:
572 __kfree_skb(skb);
574 return 0;
577 EXPORT_SYMBOL_GPL(dccp_rcv_state_process);