Merge master.kernel.org:/pub/scm/linux/kernel/git/sam/kbuild
[linux-2.6] / net / dccp / minisocks.c
1 /*
2  *  net/dccp/minisocks.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 #include <linux/timer.h>
17
18 #include <net/sock.h>
19 #include <net/xfrm.h>
20 #include <net/inet_timewait_sock.h>
21
22 #include "ccid.h"
23 #include "dccp.h"
24
25 struct inet_timewait_death_row dccp_death_row = {
26         .sysctl_max_tw_buckets = NR_FILE * 2,
27         .period         = DCCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
28         .death_lock     = SPIN_LOCK_UNLOCKED,
29         .hashinfo       = &dccp_hashinfo,
30         .tw_timer       = TIMER_INITIALIZER(inet_twdr_hangman, 0,
31                                             (unsigned long)&dccp_death_row),
32         .twkill_work    = __WORK_INITIALIZER(dccp_death_row.twkill_work,
33                                              inet_twdr_twkill_work,
34                                              &dccp_death_row),
35 /* Short-time timewait calendar */
36
37         .twcal_hand     = -1,
38         .twcal_timer    = TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
39                                             (unsigned long)&dccp_death_row),
40 };
41
42 void dccp_time_wait(struct sock *sk, int state, int timeo)
43 {
44         struct inet_timewait_sock *tw = NULL;
45
46         if (dccp_death_row.tw_count < dccp_death_row.sysctl_max_tw_buckets)
47                 tw = inet_twsk_alloc(sk, state);
48
49         if (tw != NULL) {
50                 const struct inet_connection_sock *icsk = inet_csk(sk);
51                 const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
52
53                 /* Linkage updates. */
54                 __inet_twsk_hashdance(tw, sk, &dccp_hashinfo);
55
56                 /* Get the TIME_WAIT timeout firing. */
57                 if (timeo < rto)
58                         timeo = rto;
59
60                 tw->tw_timeout = DCCP_TIMEWAIT_LEN;
61                 if (state == DCCP_TIME_WAIT)
62                         timeo = DCCP_TIMEWAIT_LEN;
63
64                 inet_twsk_schedule(tw, &dccp_death_row, timeo,
65                                    DCCP_TIMEWAIT_LEN);
66                 inet_twsk_put(tw);
67         } else {
68                 /* Sorry, if we're out of memory, just CLOSE this
69                  * socket up.  We've got bigger problems than
70                  * non-graceful socket closings.
71                  */
72                 LIMIT_NETDEBUG(KERN_INFO "DCCP: time wait bucket "
73                                          "table overflow\n");
74         }
75
76         dccp_done(sk);
77 }
78
79 struct sock *dccp_create_openreq_child(struct sock *sk,
80                                        const struct request_sock *req,
81                                        const struct sk_buff *skb)
82 {
83         /*
84          * Step 3: Process LISTEN state
85          *
86          * // Generate a new socket and switch to that socket
87          * Set S := new socket for this port pair
88          */
89         struct sock *newsk = inet_csk_clone(sk, req, GFP_ATOMIC);
90
91         if (newsk != NULL) {
92                 const struct dccp_request_sock *dreq = dccp_rsk(req);
93                 struct inet_connection_sock *newicsk = inet_csk(sk);
94                 struct dccp_sock *newdp = dccp_sk(newsk);
95
96                 newdp->dccps_hc_rx_ackpkts = NULL;
97                 newdp->dccps_role = DCCP_ROLE_SERVER;
98                 newicsk->icsk_rto = DCCP_TIMEOUT_INIT;
99
100                 if (newdp->dccps_options.dccpo_send_ack_vector) {
101                         newdp->dccps_hc_rx_ackpkts =
102                                 dccp_ackpkts_alloc(DCCP_MAX_ACK_VECTOR_LEN,
103                                                    GFP_ATOMIC);
104                         /*
105                          * XXX: We're using the same CCIDs set on the parent,
106                          * i.e. sk_clone copied the master sock and left the
107                          * CCID pointers for this child, that is why we do the
108                          * __ccid_get calls.
109                          */
110                         if (unlikely(newdp->dccps_hc_rx_ackpkts == NULL))
111                                 goto out_free;
112                 }
113
114                 if (unlikely(ccid_hc_rx_init(newdp->dccps_hc_rx_ccid,
115                                              newsk) != 0 ||
116                              ccid_hc_tx_init(newdp->dccps_hc_tx_ccid,
117                                              newsk) != 0)) {
118                         dccp_ackpkts_free(newdp->dccps_hc_rx_ackpkts);
119                         ccid_hc_rx_exit(newdp->dccps_hc_rx_ccid, newsk);
120                         ccid_hc_tx_exit(newdp->dccps_hc_tx_ccid, newsk);
121 out_free:
122                         /* It is still raw copy of parent, so invalidate
123                          * destructor and make plain sk_free() */
124                         newsk->sk_destruct = NULL;
125                         sk_free(newsk);
126                         return NULL;
127                 }
128
129                 __ccid_get(newdp->dccps_hc_rx_ccid);
130                 __ccid_get(newdp->dccps_hc_tx_ccid);
131
132                 /*
133                  * Step 3: Process LISTEN state
134                  *
135                  *      Choose S.ISS (initial seqno) or set from Init Cookie
136                  *      Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init
137                  *      Cookie
138                  */
139
140                 /* See dccp_v4_conn_request */
141                 newdp->dccps_options.dccpo_sequence_window = req->rcv_wnd;
142
143                 newdp->dccps_gar = newdp->dccps_isr = dreq->dreq_isr;
144                 dccp_update_gsr(newsk, dreq->dreq_isr);
145
146                 newdp->dccps_iss = dreq->dreq_iss;
147                 dccp_update_gss(newsk, dreq->dreq_iss);
148
149                 /*
150                  * SWL and AWL are initially adjusted so that they are not less than
151                  * the initial Sequence Numbers received and sent, respectively:
152                  *      SWL := max(GSR + 1 - floor(W/4), ISR),
153                  *      AWL := max(GSS - W' + 1, ISS).
154                  * These adjustments MUST be applied only at the beginning of the
155                  * connection.
156                  */
157                 dccp_set_seqno(&newdp->dccps_swl,
158                                max48(newdp->dccps_swl, newdp->dccps_isr));
159                 dccp_set_seqno(&newdp->dccps_awl,
160                                max48(newdp->dccps_awl, newdp->dccps_iss));
161
162                 dccp_init_xmit_timers(newsk);
163
164                 DCCP_INC_STATS_BH(DCCP_MIB_PASSIVEOPENS);
165         }
166         return newsk;
167 }
168
169 /* 
170  * Process an incoming packet for RESPOND sockets represented
171  * as an request_sock.
172  */
173 struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
174                             struct request_sock *req,
175                             struct request_sock **prev)
176 {
177         struct sock *child = NULL;
178
179         /* Check for retransmitted REQUEST */
180         if (dccp_hdr(skb)->dccph_type == DCCP_PKT_REQUEST) {
181                 if (after48(DCCP_SKB_CB(skb)->dccpd_seq,
182                             dccp_rsk(req)->dreq_isr)) {
183                         struct dccp_request_sock *dreq = dccp_rsk(req);
184
185                         dccp_pr_debug("Retransmitted REQUEST\n");
186                         /* Send another RESPONSE packet */
187                         dccp_set_seqno(&dreq->dreq_iss, dreq->dreq_iss + 1);
188                         dccp_set_seqno(&dreq->dreq_isr,
189                                        DCCP_SKB_CB(skb)->dccpd_seq);
190                         req->rsk_ops->rtx_syn_ack(sk, req, NULL);
191                 }
192                 /* Network Duplicate, discard packet */
193                 return NULL;
194         }
195
196         DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_PACKET_ERROR;
197
198         if (dccp_hdr(skb)->dccph_type != DCCP_PKT_ACK &&
199             dccp_hdr(skb)->dccph_type != DCCP_PKT_DATAACK)
200                 goto drop;
201
202         /* Invalid ACK */
203         if (DCCP_SKB_CB(skb)->dccpd_ack_seq != dccp_rsk(req)->dreq_iss) {
204                 dccp_pr_debug("Invalid ACK number: ack_seq=%llu, "
205                               "dreq_iss=%llu\n",
206                               (unsigned long long)
207                               DCCP_SKB_CB(skb)->dccpd_ack_seq,
208                               (unsigned long long)
209                               dccp_rsk(req)->dreq_iss);
210                 goto drop;
211         }
212
213         child = dccp_v4_request_recv_sock(sk, skb, req, NULL);
214         if (child == NULL)
215                 goto listen_overflow;
216
217         /* FIXME: deal with options */
218
219         inet_csk_reqsk_queue_unlink(sk, req, prev);
220         inet_csk_reqsk_queue_removed(sk, req);
221         inet_csk_reqsk_queue_add(sk, req, child);
222 out:
223         return child;
224 listen_overflow:
225         dccp_pr_debug("listen_overflow!\n");
226         DCCP_SKB_CB(skb)->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
227 drop:
228         if (dccp_hdr(skb)->dccph_type != DCCP_PKT_RESET)
229                 req->rsk_ops->send_reset(skb);
230
231         inet_csk_reqsk_queue_drop(sk, req, prev);
232         goto out;
233 }
234
235 /*
236  *  Queue segment on the new socket if the new socket is active,
237  *  otherwise we just shortcircuit this and continue with
238  *  the new socket.
239  */
240 int dccp_child_process(struct sock *parent, struct sock *child,
241                        struct sk_buff *skb)
242 {
243         int ret = 0;
244         const int state = child->sk_state;
245
246         if (!sock_owned_by_user(child)) {
247                 ret = dccp_rcv_state_process(child, skb, dccp_hdr(skb),
248                                              skb->len);
249
250                 /* Wakeup parent, send SIGIO */
251                 if (state == DCCP_RESPOND && child->sk_state != state)
252                         parent->sk_data_ready(parent, 0);
253         } else {
254                 /* Alas, it is possible again, because we do lookup
255                  * in main socket hash table and lock on listening
256                  * socket does not protect us more.
257                  */
258                 sk_add_backlog(child, skb);
259         }
260
261         bh_unlock_sock(child);
262         sock_put(child);
263         return ret;
264 }