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