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