1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52#include <linux/types.h>
53#include <linux/slab.h>
54#include <linux/in.h>
55#include <linux/random.h>
56#include <linux/crypto.h>
57#include <net/sock.h>
58#include <net/ipv6.h>
59#include <net/sctp/sctp.h>
60#include <net/sctp/sm.h>
61
62
63static void sctp_endpoint_bh_rcv(struct work_struct *work);
64
65
66
67
68static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
69 struct sock *sk,
70 gfp_t gfp)
71{
72 struct sctp_hmac_algo_param *auth_hmacs = NULL;
73 struct sctp_chunks_param *auth_chunks = NULL;
74 struct sctp_shared_key *null_key;
75 int err;
76
77 memset(ep, 0, sizeof(struct sctp_endpoint));
78
79 ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp);
80 if (!ep->digest)
81 return NULL;
82
83 if (sctp_auth_enable) {
84
85
86
87
88 auth_hmacs = kzalloc(sizeof(sctp_hmac_algo_param_t) +
89 sizeof(__u16) * SCTP_AUTH_NUM_HMACS, gfp);
90 if (!auth_hmacs)
91 goto nomem;
92
93 auth_chunks = kzalloc(sizeof(sctp_chunks_param_t) +
94 SCTP_NUM_CHUNK_TYPES, gfp);
95 if (!auth_chunks)
96 goto nomem;
97
98
99
100
101
102
103 auth_hmacs->param_hdr.type = SCTP_PARAM_HMAC_ALGO;
104 auth_hmacs->param_hdr.length =
105 htons(sizeof(sctp_paramhdr_t) + 2);
106 auth_hmacs->hmac_ids[0] = htons(SCTP_AUTH_HMAC_ID_SHA1);
107
108
109 auth_chunks->param_hdr.type = SCTP_PARAM_CHUNKS;
110
111
112
113
114 if (sctp_addip_enable) {
115 auth_chunks->chunks[0] = SCTP_CID_ASCONF;
116 auth_chunks->chunks[1] = SCTP_CID_ASCONF_ACK;
117 auth_chunks->param_hdr.length =
118 htons(sizeof(sctp_paramhdr_t) + 2);
119 }
120 }
121
122
123
124 ep->base.type = SCTP_EP_TYPE_SOCKET;
125
126
127 atomic_set(&ep->base.refcnt, 1);
128 ep->base.dead = 0;
129 ep->base.malloced = 1;
130
131
132 sctp_inq_init(&ep->base.inqueue);
133
134
135 sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv);
136
137
138 sctp_bind_addr_init(&ep->base.bind_addr, 0);
139
140
141 ep->base.sk = sk;
142 sock_hold(ep->base.sk);
143
144
145 INIT_LIST_HEAD(&ep->asocs);
146
147
148 ep->sndbuf_policy = sctp_sndbuf_policy;
149
150 sk->sk_write_space = sctp_write_space;
151 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
152
153
154 ep->rcvbuf_policy = sctp_rcvbuf_policy;
155
156
157 get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
158 ep->last_key = ep->current_key = 0;
159 ep->key_changed_at = jiffies;
160
161
162 INIT_LIST_HEAD(&ep->endpoint_shared_keys);
163 null_key = sctp_auth_shkey_create(0, GFP_KERNEL);
164 if (!null_key)
165 goto nomem;
166
167 list_add(&null_key->key_list, &ep->endpoint_shared_keys);
168
169
170 err = sctp_auth_init_hmacs(ep, gfp);
171 if (err)
172 goto nomem_hmacs;
173
174
175
176
177 ep->auth_hmacs_list = auth_hmacs;
178 ep->auth_chunk_list = auth_chunks;
179
180 return ep;
181
182nomem_hmacs:
183 sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
184nomem:
185
186 kfree(auth_hmacs);
187 kfree(auth_chunks);
188 kfree(ep->digest);
189 return NULL;
190
191}
192
193
194
195
196struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp)
197{
198 struct sctp_endpoint *ep;
199
200
201 ep = t_new(struct sctp_endpoint, gfp);
202 if (!ep)
203 goto fail;
204 if (!sctp_endpoint_init(ep, sk, gfp))
205 goto fail_init;
206 ep->base.malloced = 1;
207 SCTP_DBG_OBJCNT_INC(ep);
208 return ep;
209
210fail_init:
211 kfree(ep);
212fail:
213 return NULL;
214}
215
216
217void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
218 struct sctp_association *asoc)
219{
220 struct sock *sk = ep->base.sk;
221
222
223
224
225
226 if (asoc->temp)
227 return;
228
229
230 list_add_tail(&asoc->asocs, &ep->asocs);
231
232
233 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
234 sk->sk_ack_backlog++;
235}
236
237
238
239
240void sctp_endpoint_free(struct sctp_endpoint *ep)
241{
242 ep->base.dead = 1;
243
244 ep->base.sk->sk_state = SCTP_SS_CLOSED;
245
246
247 sctp_unhash_endpoint(ep);
248
249 sctp_endpoint_put(ep);
250}
251
252
253static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
254{
255 SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
256
257
258 crypto_free_hash(sctp_sk(ep->base.sk)->hmac);
259
260
261 kfree(ep->digest);
262
263
264
265
266 sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
267 kfree(ep->auth_hmacs_list);
268 kfree(ep->auth_chunk_list);
269
270
271 sctp_auth_destroy_hmacs(ep->auth_hmacs);
272
273
274 sctp_inq_free(&ep->base.inqueue);
275 sctp_bind_addr_free(&ep->base.bind_addr);
276
277
278 if (sctp_sk(ep->base.sk)->bind_hash)
279 sctp_put_port(ep->base.sk);
280
281
282 if (ep->base.sk)
283 sock_put(ep->base.sk);
284
285
286 if (ep->base.malloced) {
287 kfree(ep);
288 SCTP_DBG_OBJCNT_DEC(ep);
289 }
290}
291
292
293void sctp_endpoint_hold(struct sctp_endpoint *ep)
294{
295 atomic_inc(&ep->base.refcnt);
296}
297
298
299
300
301void sctp_endpoint_put(struct sctp_endpoint *ep)
302{
303 if (atomic_dec_and_test(&ep->base.refcnt))
304 sctp_endpoint_destroy(ep);
305}
306
307
308struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
309 const union sctp_addr *laddr)
310{
311 struct sctp_endpoint *retval = NULL;
312
313 if (htons(ep->base.bind_addr.port) == laddr->v4.sin_port) {
314 if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
315 sctp_sk(ep->base.sk)))
316 retval = ep;
317 }
318
319 return retval;
320}
321
322
323
324
325
326static struct sctp_association *__sctp_endpoint_lookup_assoc(
327 const struct sctp_endpoint *ep,
328 const union sctp_addr *paddr,
329 struct sctp_transport **transport)
330{
331 struct sctp_association *asoc = NULL;
332 struct sctp_transport *t = NULL;
333 struct sctp_hashbucket *head;
334 struct sctp_ep_common *epb;
335 struct hlist_node *node;
336 int hash;
337 int rport;
338
339 *transport = NULL;
340 rport = ntohs(paddr->v4.sin_port);
341
342 hash = sctp_assoc_hashfn(ep->base.bind_addr.port, rport);
343 head = &sctp_assoc_hashtable[hash];
344 read_lock(&head->lock);
345 sctp_for_each_hentry(epb, node, &head->chain) {
346 asoc = sctp_assoc(epb);
347 if (asoc->ep != ep || rport != asoc->peer.port)
348 goto next;
349
350 t = sctp_assoc_lookup_paddr(asoc, paddr);
351 if (t) {
352 *transport = t;
353 break;
354 }
355next:
356 asoc = NULL;
357 }
358 read_unlock(&head->lock);
359 return asoc;
360}
361
362
363struct sctp_association *sctp_endpoint_lookup_assoc(
364 const struct sctp_endpoint *ep,
365 const union sctp_addr *paddr,
366 struct sctp_transport **transport)
367{
368 struct sctp_association *asoc;
369
370 sctp_local_bh_disable();
371 asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
372 sctp_local_bh_enable();
373
374 return asoc;
375}
376
377
378
379
380int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
381 const union sctp_addr *paddr)
382{
383 struct sctp_sockaddr_entry *addr;
384 struct sctp_bind_addr *bp;
385
386 bp = &ep->base.bind_addr;
387
388
389
390 list_for_each_entry(addr, &bp->address_list, list) {
391 if (sctp_has_association(&addr->a, paddr))
392 return 1;
393 }
394
395 return 0;
396}
397
398
399
400
401static void sctp_endpoint_bh_rcv(struct work_struct *work)
402{
403 struct sctp_endpoint *ep =
404 container_of(work, struct sctp_endpoint,
405 base.inqueue.immediate);
406 struct sctp_association *asoc;
407 struct sock *sk;
408 struct sctp_transport *transport;
409 struct sctp_chunk *chunk;
410 struct sctp_inq *inqueue;
411 sctp_subtype_t subtype;
412 sctp_state_t state;
413 int error = 0;
414 int first_time = 1;
415
416 if (ep->base.dead)
417 return;
418
419 asoc = NULL;
420 inqueue = &ep->base.inqueue;
421 sk = ep->base.sk;
422
423 while (NULL != (chunk = sctp_inq_pop(inqueue))) {
424 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
425
426
427
428
429 if (first_time && (subtype.chunk == SCTP_CID_AUTH)) {
430 struct sctp_chunkhdr *next_hdr;
431
432 next_hdr = sctp_inq_peek(inqueue);
433 if (!next_hdr)
434 goto normal;
435
436
437
438
439
440
441 if (next_hdr->type == SCTP_CID_COOKIE_ECHO) {
442 chunk->auth_chunk = skb_clone(chunk->skb,
443 GFP_ATOMIC);
444 chunk->auth = 1;
445 continue;
446 }
447 }
448normal:
449
450
451
452
453
454
455 if (NULL == chunk->asoc) {
456 asoc = sctp_endpoint_lookup_assoc(ep,
457 sctp_source(chunk),
458 &transport);
459 chunk->asoc = asoc;
460 chunk->transport = transport;
461 }
462
463 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
464 if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
465 continue;
466
467
468
469
470 if (asoc && sctp_chunk_is_data(chunk))
471 asoc->peer.last_data_from = chunk->transport;
472 else
473 SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
474
475 if (chunk->transport)
476 chunk->transport->last_time_heard = jiffies;
477
478 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
479 ep, asoc, chunk, GFP_ATOMIC);
480
481 if (error && chunk)
482 chunk->pdiscard = 1;
483
484
485
486
487 if (!sctp_sk(sk)->ep)
488 break;
489
490 if (first_time)
491 first_time = 0;
492 }
493}
494