Blame view
net/sctp/endpointola.c
12.2 KB
60c778b25 [SCTP]: Stop clai... |
1 |
/* SCTP kernel implementation |
1da177e4c Linux-2.6.12-rc2 |
2 3 4 5 6 7 8 |
* Copyright (c) 1999-2000 Cisco, Inc. * Copyright (c) 1999-2001 Motorola, Inc. * Copyright (c) 2001-2002 International Business Machines, Corp. * Copyright (c) 2001 Intel Corp. * Copyright (c) 2001 Nokia, Inc. * Copyright (c) 2001 La Monte H.P. Yarroll * |
60c778b25 [SCTP]: Stop clai... |
9 |
* This file is part of the SCTP kernel implementation |
1da177e4c Linux-2.6.12-rc2 |
10 11 12 |
* * This abstraction represents an SCTP endpoint. * |
60c778b25 [SCTP]: Stop clai... |
13 |
* The SCTP implementation is free software; |
1da177e4c Linux-2.6.12-rc2 |
14 15 16 17 18 |
* you can redistribute it and/or modify it under the terms of * the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * |
60c778b25 [SCTP]: Stop clai... |
19 |
* The SCTP implementation is distributed in the hope that it |
1da177e4c Linux-2.6.12-rc2 |
20 21 22 23 24 25 |
* will be useful, but WITHOUT ANY WARRANTY; without even the implied * ************************ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License |
4b2f13a25 sctp: Fix FSF add... |
26 27 |
* along with GNU CC; see the file COPYING. If not, see * <http://www.gnu.org/licenses/>. |
1da177e4c Linux-2.6.12-rc2 |
28 29 30 |
* * Please send any bug reports or fixes you make to the * email address(es): |
91705c61b net: sctp: trivia... |
31 |
* lksctp developers <linux-sctp@vger.kernel.org> |
1da177e4c Linux-2.6.12-rc2 |
32 |
* |
1da177e4c Linux-2.6.12-rc2 |
33 34 35 36 37 38 |
* Written or modified by: * La Monte H.P. Yarroll <piggy@acm.org> * Karl Knutson <karl@athena.chicago.il.us> * Jon Grimm <jgrimm@austin.ibm.com> * Daisy Chang <daisyc@us.ibm.com> * Dajiang Zhang <dajiang.zhang@nokia.com> |
1da177e4c Linux-2.6.12-rc2 |
39 40 41 |
*/ #include <linux/types.h> |
1da177e4c Linux-2.6.12-rc2 |
42 43 44 |
#include <linux/slab.h> #include <linux/in.h> #include <linux/random.h> /* get_random_bytes() */ |
1da177e4c Linux-2.6.12-rc2 |
45 46 47 48 49 50 |
#include <net/sock.h> #include <net/ipv6.h> #include <net/sctp/sctp.h> #include <net/sctp/sm.h> /* Forward declarations for internal helpers. */ |
c4028958b WorkStruct: make ... |
51 |
static void sctp_endpoint_bh_rcv(struct work_struct *work); |
1da177e4c Linux-2.6.12-rc2 |
52 53 54 55 56 |
/* * Initialize the base fields of the endpoint structure. */ static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep, |
3182cd84f [SCTP]: __nocast ... |
57 |
struct sock *sk, |
dd0fc66fb [PATCH] gfp flags... |
58 |
gfp_t gfp) |
1da177e4c Linux-2.6.12-rc2 |
59 |
{ |
e1fc3b14f sctp: Make sysctl... |
60 |
struct net *net = sock_net(sk); |
a29a5bd4f [SCTP]: Implement... |
61 62 63 64 |
struct sctp_hmac_algo_param *auth_hmacs = NULL; struct sctp_chunks_param *auth_chunks = NULL; struct sctp_shared_key *null_key; int err; |
b68dbcab1 [SCTP]: Fix warning |
65 66 67 |
ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp); if (!ep->digest) return NULL; |
b14878ccb net: sctp: cache ... |
68 69 |
ep->auth_enable = net->sctp.auth_enable; if (ep->auth_enable) { |
a29a5bd4f [SCTP]: Implement... |
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
/* Allocate space for HMACS and CHUNKS authentication * variables. There are arrays that we encode directly * into parameters to make the rest of the operations easier. */ auth_hmacs = kzalloc(sizeof(sctp_hmac_algo_param_t) + sizeof(__u16) * SCTP_AUTH_NUM_HMACS, gfp); if (!auth_hmacs) goto nomem; auth_chunks = kzalloc(sizeof(sctp_chunks_param_t) + SCTP_NUM_CHUNK_TYPES, gfp); if (!auth_chunks) goto nomem; /* Initialize the HMACS parameter. * SCTP-AUTH: Section 3.3 * Every endpoint supporting SCTP chunk authentication MUST * support the HMAC based on the SHA-1 algorithm. */ auth_hmacs->param_hdr.type = SCTP_PARAM_HMAC_ALGO; auth_hmacs->param_hdr.length = htons(sizeof(sctp_paramhdr_t) + 2); auth_hmacs->hmac_ids[0] = htons(SCTP_AUTH_HMAC_ID_SHA1); /* Initialize the CHUNKS parameter */ auth_chunks->param_hdr.type = SCTP_PARAM_CHUNKS; |
5e739d175 sctp: fix potenti... |
96 |
auth_chunks->param_hdr.length = htons(sizeof(sctp_paramhdr_t)); |
a29a5bd4f [SCTP]: Implement... |
97 98 99 100 |
/* If the Add-IP functionality is enabled, we must * authenticate, ASCONF and ASCONF-ACK chunks */ |
e1fc3b14f sctp: Make sysctl... |
101 |
if (net->sctp.addip_enable) { |
a29a5bd4f [SCTP]: Implement... |
102 103 |
auth_chunks->chunks[0] = SCTP_CID_ASCONF; auth_chunks->chunks[1] = SCTP_CID_ASCONF_ACK; |
cb0dc77de net: fix sctp bre... |
104 105 |
auth_chunks->param_hdr.length = htons(sizeof(sctp_paramhdr_t) + 2); |
a29a5bd4f [SCTP]: Implement... |
106 107 |
} } |
1da177e4c Linux-2.6.12-rc2 |
108 109 110 111 112 113 |
/* Initialize the base structure. */ /* What type of endpoint are we? */ ep->base.type = SCTP_EP_TYPE_SOCKET; /* Initialize the basic object fields. */ atomic_set(&ep->base.refcnt, 1); |
0022d2dd4 net: sctp: minor:... |
114 |
ep->base.dead = false; |
1da177e4c Linux-2.6.12-rc2 |
115 116 117 118 119 |
/* Create an input queue. */ sctp_inq_init(&ep->base.inqueue); /* Set its top-half handler */ |
c4028958b WorkStruct: make ... |
120 |
sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv); |
1da177e4c Linux-2.6.12-rc2 |
121 122 123 |
/* Initialize the bind addr area */ sctp_bind_addr_init(&ep->base.bind_addr, 0); |
1da177e4c Linux-2.6.12-rc2 |
124 125 126 127 128 129 130 |
/* Remember who we are attached to. */ ep->base.sk = sk; sock_hold(ep->base.sk); /* Create the lists of associations. */ INIT_LIST_HEAD(&ep->asocs); |
1da177e4c Linux-2.6.12-rc2 |
131 |
/* Use SCTP specific send buffer space queues. */ |
e1fc3b14f sctp: Make sysctl... |
132 |
ep->sndbuf_policy = net->sctp.sndbuf_policy; |
4d93df0ab [SCTP]: Rewrite o... |
133 |
|
561b1733a sctp: avoid irq l... |
134 |
sk->sk_data_ready = sctp_data_ready; |
1da177e4c Linux-2.6.12-rc2 |
135 136 |
sk->sk_write_space = sctp_write_space; sock_set_flag(sk, SOCK_USE_WRITE_QUEUE); |
049b3ff5a [SCTP]: Include u... |
137 |
/* Get the receive buffer policy for this endpoint */ |
e1fc3b14f sctp: Make sysctl... |
138 |
ep->rcvbuf_policy = net->sctp.rcvbuf_policy; |
049b3ff5a [SCTP]: Include u... |
139 |
|
1da177e4c Linux-2.6.12-rc2 |
140 |
/* Initialize the secret key used with cookie. */ |
570617e79 net: sctp: remove... |
141 |
get_random_bytes(ep->secret_key, sizeof(ep->secret_key)); |
1da177e4c Linux-2.6.12-rc2 |
142 |
|
a29a5bd4f [SCTP]: Implement... |
143 144 |
/* SCTP-AUTH extensions*/ INIT_LIST_HEAD(&ep->endpoint_shared_keys); |
81ce0dbc1 sctp: use the pas... |
145 |
null_key = sctp_auth_shkey_create(0, gfp); |
a29a5bd4f [SCTP]: Implement... |
146 147 148 149 |
if (!null_key) goto nomem; list_add(&null_key->key_list, &ep->endpoint_shared_keys); |
02582e9bc treewide: fix typ... |
150 |
/* Allocate and initialize transorms arrays for supported HMACs. */ |
a29a5bd4f [SCTP]: Implement... |
151 152 153 154 155 156 157 158 159 |
err = sctp_auth_init_hmacs(ep, gfp); if (err) goto nomem_hmacs; /* Add the null key to the endpoint shared keys list and * set the hmcas and chunks pointers. */ ep->auth_hmacs_list = auth_hmacs; ep->auth_chunk_list = auth_chunks; |
28aa4c26f sctp: add SCTP_PR... |
160 |
ep->prsctp_enable = net->sctp.prsctp_enable; |
a29a5bd4f [SCTP]: Implement... |
161 |
|
1da177e4c Linux-2.6.12-rc2 |
162 |
return ep; |
a29a5bd4f [SCTP]: Implement... |
163 164 165 166 167 168 169 170 171 |
nomem_hmacs: sctp_auth_destroy_keys(&ep->endpoint_shared_keys); nomem: /* Free all allocations */ kfree(auth_hmacs); kfree(auth_chunks); kfree(ep->digest); return NULL; |
1da177e4c Linux-2.6.12-rc2 |
172 173 174 175 176 |
} /* Create a sctp_endpoint with all that boring stuff initialized. * Returns NULL if there isn't enough memory. */ |
dd0fc66fb [PATCH] gfp flags... |
177 |
struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp) |
1da177e4c Linux-2.6.12-rc2 |
178 179 180 181 |
{ struct sctp_endpoint *ep; /* Build a local endpoint. */ |
939cfa75a net: sctp: get ri... |
182 |
ep = kzalloc(sizeof(*ep), gfp); |
1da177e4c Linux-2.6.12-rc2 |
183 184 |
if (!ep) goto fail; |
939cfa75a net: sctp: get ri... |
185 |
|
1da177e4c Linux-2.6.12-rc2 |
186 187 |
if (!sctp_endpoint_init(ep, sk, gfp)) goto fail_init; |
ff2266cdd net: sctp: remove... |
188 |
|
1da177e4c Linux-2.6.12-rc2 |
189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
SCTP_DBG_OBJCNT_INC(ep); return ep; fail_init: kfree(ep); fail: return NULL; } /* Add an association to an endpoint. */ void sctp_endpoint_add_asoc(struct sctp_endpoint *ep, struct sctp_association *asoc) { struct sock *sk = ep->base.sk; |
de76e695a [SCTP]: Remove te... |
203 204 205 206 207 208 |
/* If this is a temporary association, don't bother * since we'll be removing it shortly and don't * want anyone to find it anyway. */ if (asoc->temp) return; |
1da177e4c Linux-2.6.12-rc2 |
209 210 211 212 213 214 215 216 217 218 219 220 221 |
/* Now just add it to our list of asocs */ list_add_tail(&asoc->asocs, &ep->asocs); /* Increment the backlog value for a TCP-style listening socket. */ if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING)) sk->sk_ack_backlog++; } /* Free the endpoint structure. Delay cleanup until * all users have released their reference count on this structure. */ void sctp_endpoint_free(struct sctp_endpoint *ep) { |
0022d2dd4 net: sctp: minor:... |
222 |
ep->base.dead = true; |
cfdeef328 [SCTP]: Unhash th... |
223 224 225 226 227 |
ep->base.sk->sk_state = SCTP_SS_CLOSED; /* Unlink this endpoint, so we can't find it again! */ sctp_unhash_endpoint(ep); |
1da177e4c Linux-2.6.12-rc2 |
228 229 230 231 232 233 |
sctp_endpoint_put(ep); } /* Final destructor for endpoint. */ static void sctp_endpoint_destroy(struct sctp_endpoint *ep) { |
0a2fbac19 net: sctp: decoup... |
234 |
struct sock *sk; |
1da177e4c Linux-2.6.12-rc2 |
235 |
|
bb33381d0 net: sctp: rework... |
236 237 238 239 240 |
if (unlikely(!ep->base.dead)) { WARN(1, "Attempt to destroy undead endpoint %p! ", ep); return; } |
1da177e4c Linux-2.6.12-rc2 |
241 |
|
b68dbcab1 [SCTP]: Fix warning |
242 243 |
/* Free the digest buffer */ kfree(ep->digest); |
a29a5bd4f [SCTP]: Implement... |
244 245 246 247 248 249 250 251 252 |
/* SCTP-AUTH: Free up AUTH releated data such as shared keys * chunks and hmacs arrays that were allocated */ sctp_auth_destroy_keys(&ep->endpoint_shared_keys); kfree(ep->auth_hmacs_list); kfree(ep->auth_chunk_list); /* AUTH - Free any allocated HMAC transform containers */ sctp_auth_destroy_hmacs(ep->auth_hmacs); |
1da177e4c Linux-2.6.12-rc2 |
253 254 255 |
/* Cleanup. */ sctp_inq_free(&ep->base.inqueue); sctp_bind_addr_free(&ep->base.bind_addr); |
570617e79 net: sctp: remove... |
256 |
memset(ep->secret_key, 0, sizeof(ep->secret_key)); |
b5c37fe6e net: sctp: sctp_e... |
257 |
|
1da177e4c Linux-2.6.12-rc2 |
258 |
/* Give up our hold on the sock. */ |
0a2fbac19 net: sctp: decoup... |
259 260 261 262 263 264 265 266 |
sk = ep->base.sk; if (sk != NULL) { /* Remove and free the port */ if (sctp_sk(sk)->bind_hash) sctp_put_port(sk); sock_put(sk); } |
1da177e4c Linux-2.6.12-rc2 |
267 |
|
ff2266cdd net: sctp: remove... |
268 269 |
kfree(ep); SCTP_DBG_OBJCNT_DEC(ep); |
1da177e4c Linux-2.6.12-rc2 |
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
} /* Hold a reference to an endpoint. */ void sctp_endpoint_hold(struct sctp_endpoint *ep) { atomic_inc(&ep->base.refcnt); } /* Release a reference to an endpoint and clean up if there are * no more references. */ void sctp_endpoint_put(struct sctp_endpoint *ep) { if (atomic_dec_and_test(&ep->base.refcnt)) sctp_endpoint_destroy(ep); } /* Is this the endpoint we are looking for? */ struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep, |
4cdadcbcb sctp: Make the en... |
289 |
struct net *net, |
1da177e4c Linux-2.6.12-rc2 |
290 291 |
const union sctp_addr *laddr) { |
559cf710b [SCTP]: Convert b... |
292 |
struct sctp_endpoint *retval = NULL; |
1da177e4c Linux-2.6.12-rc2 |
293 |
|
4cdadcbcb sctp: Make the en... |
294 295 |
if ((htons(ep->base.bind_addr.port) == laddr->v4.sin_port) && net_eq(sock_net(ep->base.sk), net)) { |
1da177e4c Linux-2.6.12-rc2 |
296 |
if (sctp_bind_addr_match(&ep->base.bind_addr, laddr, |
559cf710b [SCTP]: Convert b... |
297 |
sctp_sk(ep->base.sk))) |
1da177e4c Linux-2.6.12-rc2 |
298 |
retval = ep; |
1da177e4c Linux-2.6.12-rc2 |
299 |
} |
1da177e4c Linux-2.6.12-rc2 |
300 301 302 303 |
return retval; } /* Find the association that goes with this chunk. |
4f0087812 sctp: apply rhash... |
304 305 |
* We lookup the transport from hashtable at first, then get association * through t->assoc. |
1da177e4c Linux-2.6.12-rc2 |
306 |
*/ |
c79c06669 sctp: remove the ... |
307 |
struct sctp_association *sctp_endpoint_lookup_assoc( |
1da177e4c Linux-2.6.12-rc2 |
308 309 310 311 |
const struct sctp_endpoint *ep, const union sctp_addr *paddr, struct sctp_transport **transport) { |
123ed979e SCTP: Use hashed ... |
312 |
struct sctp_association *asoc = NULL; |
4f0087812 sctp: apply rhash... |
313 |
struct sctp_transport *t; |
1da177e4c Linux-2.6.12-rc2 |
314 |
|
123ed979e SCTP: Use hashed ... |
315 |
*transport = NULL; |
deb85a6ec sctp: bail from s... |
316 317 318 319 320 321 |
/* If the local port is not set, there can't be any associations * on this endpoint. */ if (!ep->base.bind_addr.port) goto out; |
4f0087812 sctp: apply rhash... |
322 |
t = sctp_epaddr_lookup_transport(ep, paddr); |
dd7445ad6 sctp: the temp as... |
323 |
if (!t) |
4f0087812 sctp: apply rhash... |
324 |
goto out; |
deb85a6ec sctp: bail from s... |
325 |
|
4f0087812 sctp: apply rhash... |
326 327 |
*transport = t; asoc = t->asoc; |
deb85a6ec sctp: bail from s... |
328 |
out: |
123ed979e SCTP: Use hashed ... |
329 |
return asoc; |
1da177e4c Linux-2.6.12-rc2 |
330 |
} |
1da177e4c Linux-2.6.12-rc2 |
331 332 333 334 335 336 |
/* Look for any peeled off association from the endpoint that matches the * given peer address. */ int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep, const union sctp_addr *paddr) { |
1da177e4c Linux-2.6.12-rc2 |
337 338 |
struct sctp_sockaddr_entry *addr; struct sctp_bind_addr *bp; |
4110cc255 sctp: Make the as... |
339 |
struct net *net = sock_net(ep->base.sk); |
1da177e4c Linux-2.6.12-rc2 |
340 |
|
1da177e4c Linux-2.6.12-rc2 |
341 |
bp = &ep->base.bind_addr; |
559cf710b [SCTP]: Convert b... |
342 343 344 345 |
/* This function is called with the socket lock held, * so the address_list can not change. */ list_for_each_entry(addr, &bp->address_list, list) { |
4110cc255 sctp: Make the as... |
346 |
if (sctp_has_association(net, &addr->a, paddr)) |
1da177e4c Linux-2.6.12-rc2 |
347 |
return 1; |
1da177e4c Linux-2.6.12-rc2 |
348 |
} |
1da177e4c Linux-2.6.12-rc2 |
349 350 351 352 353 354 355 |
return 0; } /* Do delayed input processing. This is scheduled by sctp_rcv(). * This may be called on BH or task time. */ |
c4028958b WorkStruct: make ... |
356 |
static void sctp_endpoint_bh_rcv(struct work_struct *work) |
1da177e4c Linux-2.6.12-rc2 |
357 |
{ |
c4028958b WorkStruct: make ... |
358 359 360 |
struct sctp_endpoint *ep = container_of(work, struct sctp_endpoint, base.inqueue.immediate); |
1da177e4c Linux-2.6.12-rc2 |
361 362 |
struct sctp_association *asoc; struct sock *sk; |
55e26eb95 sctp: Push struct... |
363 |
struct net *net; |
1da177e4c Linux-2.6.12-rc2 |
364 365 366 367 368 369 |
struct sctp_transport *transport; struct sctp_chunk *chunk; struct sctp_inq *inqueue; sctp_subtype_t subtype; sctp_state_t state; int error = 0; |
42b2aa86c treewide: Fix typ... |
370 |
int first_time = 1; /* is this the first time through the loop */ |
1da177e4c Linux-2.6.12-rc2 |
371 372 373 374 375 376 377 |
if (ep->base.dead) return; asoc = NULL; inqueue = &ep->base.inqueue; sk = ep->base.sk; |
55e26eb95 sctp: Push struct... |
378 |
net = sock_net(sk); |
1da177e4c Linux-2.6.12-rc2 |
379 380 381 |
while (NULL != (chunk = sctp_inq_pop(inqueue))) { subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type); |
bbd0d5980 [SCTP]: Implement... |
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
/* If the first chunk in the packet is AUTH, do special * processing specified in Section 6.3 of SCTP-AUTH spec */ if (first_time && (subtype.chunk == SCTP_CID_AUTH)) { struct sctp_chunkhdr *next_hdr; next_hdr = sctp_inq_peek(inqueue); if (!next_hdr) goto normal; /* If the next chunk is COOKIE-ECHO, skip the AUTH * chunk while saving a pointer to it so we can do * Authentication later (during cookie-echo * processing). */ if (next_hdr->type == SCTP_CID_COOKIE_ECHO) { chunk->auth_chunk = skb_clone(chunk->skb, GFP_ATOMIC); chunk->auth = 1; continue; } } normal: |
1da177e4c Linux-2.6.12-rc2 |
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
/* We might have grown an association since last we * looked, so try again. * * This happens when we've just processed our * COOKIE-ECHO chunk. */ if (NULL == chunk->asoc) { asoc = sctp_endpoint_lookup_assoc(ep, sctp_source(chunk), &transport); chunk->asoc = asoc; chunk->transport = transport; } state = asoc ? asoc->state : SCTP_STATE_CLOSED; |
bbd0d5980 [SCTP]: Implement... |
420 421 |
if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth) continue; |
1da177e4c Linux-2.6.12-rc2 |
422 423 424 425 426 427 |
/* Remember where the last DATA chunk came from so we * know where to send the SACK. */ if (asoc && sctp_chunk_is_data(chunk)) asoc->peer.last_data_from = chunk->transport; |
196d67593 sctp: Add support... |
428 |
else { |
b01a24078 sctp: Make the mi... |
429 |
SCTP_INC_STATS(sock_net(ep->base.sk), SCTP_MIB_INCTRLCHUNKS); |
196d67593 sctp: Add support... |
430 431 432 |
if (asoc) asoc->stats.ictrlchunks++; } |
1da177e4c Linux-2.6.12-rc2 |
433 434 |
if (chunk->transport) |
e575235fc net: sctp: migrat... |
435 |
chunk->transport->last_time_heard = ktime_get(); |
1da177e4c Linux-2.6.12-rc2 |
436 |
|
55e26eb95 sctp: Push struct... |
437 |
error = sctp_do_sm(net, SCTP_EVENT_T_CHUNK, subtype, state, |
d808ad9ab [NET] SCTP: Fix w... |
438 |
ep, asoc, chunk, GFP_ATOMIC); |
1da177e4c Linux-2.6.12-rc2 |
439 440 441 442 443 444 445 446 447 |
if (error && chunk) chunk->pdiscard = 1; /* Check to see if the endpoint is freed in response to * the incoming chunk. If so, get out of the while loop. */ if (!sctp_sk(sk)->ep) break; |
bbd0d5980 [SCTP]: Implement... |
448 449 450 |
if (first_time) first_time = 0; |
1da177e4c Linux-2.6.12-rc2 |
451 452 |
} } |