0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0032
0033 #include <crypto/hash.h>
0034 #include <linux/types.h>
0035 #include <linux/kernel.h>
0036 #include <linux/ip.h>
0037 #include <linux/ipv6.h>
0038 #include <linux/net.h>
0039 #include <linux/inet.h>
0040 #include <linux/scatterlist.h>
0041 #include <linux/slab.h>
0042 #include <net/sock.h>
0043
0044 #include <linux/skbuff.h>
0045 #include <linux/random.h> /* for get_random_bytes */
0046 #include <net/sctp/sctp.h>
0047 #include <net/sctp/sm.h>
0048
0049 static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
0050 __u8 type, __u8 flags, int paylen,
0051 gfp_t gfp);
0052 static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
0053 __u8 flags, int paylen, gfp_t gfp);
0054 static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
0055 __u8 type, __u8 flags, int paylen,
0056 gfp_t gfp);
0057 static struct sctp_cookie_param *sctp_pack_cookie(
0058 const struct sctp_endpoint *ep,
0059 const struct sctp_association *asoc,
0060 const struct sctp_chunk *init_chunk,
0061 int *cookie_len,
0062 const __u8 *raw_addrs, int addrs_len);
0063 static int sctp_process_param(struct sctp_association *asoc,
0064 union sctp_params param,
0065 const union sctp_addr *peer_addr,
0066 gfp_t gfp);
0067 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
0068 const void *data);
0069
0070
0071 static void sctp_control_release_owner(struct sk_buff *skb)
0072 {
0073 struct sctp_chunk *chunk = skb_shinfo(skb)->destructor_arg;
0074
0075 if (chunk->shkey) {
0076 struct sctp_shared_key *shkey = chunk->shkey;
0077 struct sctp_association *asoc = chunk->asoc;
0078
0079
0080
0081
0082
0083 if (shkey->deactivated && !list_empty(&shkey->key_list) &&
0084 refcount_read(&shkey->refcnt) == 2) {
0085 struct sctp_ulpevent *ev;
0086
0087 ev = sctp_ulpevent_make_authkey(asoc, shkey->key_id,
0088 SCTP_AUTH_FREE_KEY,
0089 GFP_KERNEL);
0090 if (ev)
0091 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
0092 }
0093 sctp_auth_shkey_release(chunk->shkey);
0094 }
0095 }
0096
0097 static void sctp_control_set_owner_w(struct sctp_chunk *chunk)
0098 {
0099 struct sctp_association *asoc = chunk->asoc;
0100 struct sk_buff *skb = chunk->skb;
0101
0102
0103
0104
0105
0106
0107
0108
0109 if (chunk->auth) {
0110 chunk->shkey = asoc->shkey;
0111 sctp_auth_shkey_hold(chunk->shkey);
0112 }
0113 skb->sk = asoc ? asoc->base.sk : NULL;
0114 skb_shinfo(skb)->destructor_arg = chunk;
0115 skb->destructor = sctp_control_release_owner;
0116 }
0117
0118
0119 int sctp_chunk_iif(const struct sctp_chunk *chunk)
0120 {
0121 struct sk_buff *skb = chunk->skb;
0122
0123 return SCTP_INPUT_CB(skb)->af->skb_iif(skb);
0124 }
0125
0126
0127
0128
0129
0130
0131 static const struct sctp_paramhdr ecap_param = {
0132 SCTP_PARAM_ECN_CAPABLE,
0133 cpu_to_be16(sizeof(struct sctp_paramhdr)),
0134 };
0135 static const struct sctp_paramhdr prsctp_param = {
0136 SCTP_PARAM_FWD_TSN_SUPPORT,
0137 cpu_to_be16(sizeof(struct sctp_paramhdr)),
0138 };
0139
0140
0141
0142
0143 int sctp_init_cause(struct sctp_chunk *chunk, __be16 cause_code,
0144 size_t paylen)
0145 {
0146 struct sctp_errhdr err;
0147 __u16 len;
0148
0149
0150 err.cause = cause_code;
0151 len = sizeof(err) + paylen;
0152 err.length = htons(len);
0153
0154 if (skb_tailroom(chunk->skb) < len)
0155 return -ENOSPC;
0156
0157 chunk->subh.err_hdr = sctp_addto_chunk(chunk, sizeof(err), &err);
0158
0159 return 0;
0160 }
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
0207 const struct sctp_bind_addr *bp,
0208 gfp_t gfp, int vparam_len)
0209 {
0210 struct sctp_supported_ext_param ext_param;
0211 struct sctp_adaptation_ind_param aiparam;
0212 struct sctp_paramhdr *auth_chunks = NULL;
0213 struct sctp_paramhdr *auth_hmacs = NULL;
0214 struct sctp_supported_addrs_param sat;
0215 struct sctp_endpoint *ep = asoc->ep;
0216 struct sctp_chunk *retval = NULL;
0217 int num_types, addrs_len = 0;
0218 struct sctp_inithdr init;
0219 union sctp_params addrs;
0220 struct sctp_sock *sp;
0221 __u8 extensions[5];
0222 size_t chunksize;
0223 __be16 types[2];
0224 int num_ext = 0;
0225
0226
0227
0228
0229
0230
0231
0232
0233 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
0234
0235 init.init_tag = htonl(asoc->c.my_vtag);
0236 init.a_rwnd = htonl(asoc->rwnd);
0237 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
0238 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
0239 init.initial_tsn = htonl(asoc->c.initial_tsn);
0240
0241
0242 sp = sctp_sk(asoc->base.sk);
0243 num_types = sp->pf->supported_addrs(sp, types);
0244
0245 chunksize = sizeof(init) + addrs_len;
0246 chunksize += SCTP_PAD4(SCTP_SAT_LEN(num_types));
0247
0248 if (asoc->ep->ecn_enable)
0249 chunksize += sizeof(ecap_param);
0250
0251 if (asoc->ep->prsctp_enable)
0252 chunksize += sizeof(prsctp_param);
0253
0254
0255
0256
0257
0258
0259 if (asoc->ep->asconf_enable) {
0260 extensions[num_ext] = SCTP_CID_ASCONF;
0261 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
0262 num_ext += 2;
0263 }
0264
0265 if (asoc->ep->reconf_enable) {
0266 extensions[num_ext] = SCTP_CID_RECONF;
0267 num_ext += 1;
0268 }
0269
0270 if (sp->adaptation_ind)
0271 chunksize += sizeof(aiparam);
0272
0273 if (asoc->ep->intl_enable) {
0274 extensions[num_ext] = SCTP_CID_I_DATA;
0275 num_ext += 1;
0276 }
0277
0278 chunksize += vparam_len;
0279
0280
0281 if (ep->auth_enable) {
0282
0283 chunksize += sizeof(asoc->c.auth_random);
0284
0285
0286 auth_hmacs = (struct sctp_paramhdr *)asoc->c.auth_hmacs;
0287 if (auth_hmacs->length)
0288 chunksize += SCTP_PAD4(ntohs(auth_hmacs->length));
0289 else
0290 auth_hmacs = NULL;
0291
0292
0293 auth_chunks = (struct sctp_paramhdr *)asoc->c.auth_chunks;
0294 if (auth_chunks->length)
0295 chunksize += SCTP_PAD4(ntohs(auth_chunks->length));
0296 else
0297 auth_chunks = NULL;
0298
0299 extensions[num_ext] = SCTP_CID_AUTH;
0300 num_ext += 1;
0301 }
0302
0303
0304 if (num_ext)
0305 chunksize += SCTP_PAD4(sizeof(ext_param) + num_ext);
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319 retval = sctp_make_control(asoc, SCTP_CID_INIT, 0, chunksize, gfp);
0320 if (!retval)
0321 goto nodata;
0322
0323 retval->subh.init_hdr =
0324 sctp_addto_chunk(retval, sizeof(init), &init);
0325 retval->param_hdr.v =
0326 sctp_addto_chunk(retval, addrs_len, addrs.v);
0327
0328
0329
0330
0331
0332
0333
0334
0335 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
0336 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
0337 sctp_addto_chunk(retval, sizeof(sat), &sat);
0338 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
0339
0340 if (asoc->ep->ecn_enable)
0341 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
0342
0343
0344
0345
0346 if (num_ext) {
0347 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
0348 ext_param.param_hdr.length = htons(sizeof(ext_param) + num_ext);
0349 sctp_addto_chunk(retval, sizeof(ext_param), &ext_param);
0350 sctp_addto_param(retval, num_ext, extensions);
0351 }
0352
0353 if (asoc->ep->prsctp_enable)
0354 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
0355
0356 if (sp->adaptation_ind) {
0357 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
0358 aiparam.param_hdr.length = htons(sizeof(aiparam));
0359 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
0360 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
0361 }
0362
0363
0364 if (ep->auth_enable) {
0365 sctp_addto_chunk(retval, sizeof(asoc->c.auth_random),
0366 asoc->c.auth_random);
0367 if (auth_hmacs)
0368 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
0369 auth_hmacs);
0370 if (auth_chunks)
0371 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
0372 auth_chunks);
0373 }
0374 nodata:
0375 kfree(addrs.v);
0376 return retval;
0377 }
0378
0379 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
0380 const struct sctp_chunk *chunk,
0381 gfp_t gfp, int unkparam_len)
0382 {
0383 struct sctp_supported_ext_param ext_param;
0384 struct sctp_adaptation_ind_param aiparam;
0385 struct sctp_paramhdr *auth_chunks = NULL;
0386 struct sctp_paramhdr *auth_random = NULL;
0387 struct sctp_paramhdr *auth_hmacs = NULL;
0388 struct sctp_chunk *retval = NULL;
0389 struct sctp_cookie_param *cookie;
0390 struct sctp_inithdr initack;
0391 union sctp_params addrs;
0392 struct sctp_sock *sp;
0393 __u8 extensions[5];
0394 size_t chunksize;
0395 int num_ext = 0;
0396 int cookie_len;
0397 int addrs_len;
0398
0399
0400 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
0401
0402 initack.init_tag = htonl(asoc->c.my_vtag);
0403 initack.a_rwnd = htonl(asoc->rwnd);
0404 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
0405 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
0406 initack.initial_tsn = htonl(asoc->c.initial_tsn);
0407
0408
0409
0410
0411 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
0412 addrs.v, addrs_len);
0413 if (!cookie)
0414 goto nomem_cookie;
0415
0416
0417
0418
0419 sp = sctp_sk(asoc->base.sk);
0420 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
0421
0422
0423 if (asoc->peer.ecn_capable)
0424 chunksize += sizeof(ecap_param);
0425
0426 if (asoc->peer.prsctp_capable)
0427 chunksize += sizeof(prsctp_param);
0428
0429 if (asoc->peer.asconf_capable) {
0430 extensions[num_ext] = SCTP_CID_ASCONF;
0431 extensions[num_ext+1] = SCTP_CID_ASCONF_ACK;
0432 num_ext += 2;
0433 }
0434
0435 if (asoc->peer.reconf_capable) {
0436 extensions[num_ext] = SCTP_CID_RECONF;
0437 num_ext += 1;
0438 }
0439
0440 if (sp->adaptation_ind)
0441 chunksize += sizeof(aiparam);
0442
0443 if (asoc->peer.intl_capable) {
0444 extensions[num_ext] = SCTP_CID_I_DATA;
0445 num_ext += 1;
0446 }
0447
0448 if (asoc->peer.auth_capable) {
0449 auth_random = (struct sctp_paramhdr *)asoc->c.auth_random;
0450 chunksize += ntohs(auth_random->length);
0451
0452 auth_hmacs = (struct sctp_paramhdr *)asoc->c.auth_hmacs;
0453 if (auth_hmacs->length)
0454 chunksize += SCTP_PAD4(ntohs(auth_hmacs->length));
0455 else
0456 auth_hmacs = NULL;
0457
0458 auth_chunks = (struct sctp_paramhdr *)asoc->c.auth_chunks;
0459 if (auth_chunks->length)
0460 chunksize += SCTP_PAD4(ntohs(auth_chunks->length));
0461 else
0462 auth_chunks = NULL;
0463
0464 extensions[num_ext] = SCTP_CID_AUTH;
0465 num_ext += 1;
0466 }
0467
0468 if (num_ext)
0469 chunksize += SCTP_PAD4(sizeof(ext_param) + num_ext);
0470
0471
0472 retval = sctp_make_control(asoc, SCTP_CID_INIT_ACK, 0, chunksize, gfp);
0473 if (!retval)
0474 goto nomem_chunk;
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485 if (chunk->transport)
0486 retval->transport =
0487 sctp_assoc_lookup_paddr(asoc,
0488 &chunk->transport->ipaddr);
0489
0490 retval->subh.init_hdr =
0491 sctp_addto_chunk(retval, sizeof(initack), &initack);
0492 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
0493 sctp_addto_chunk(retval, cookie_len, cookie);
0494 if (asoc->peer.ecn_capable)
0495 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
0496 if (num_ext) {
0497 ext_param.param_hdr.type = SCTP_PARAM_SUPPORTED_EXT;
0498 ext_param.param_hdr.length = htons(sizeof(ext_param) + num_ext);
0499 sctp_addto_chunk(retval, sizeof(ext_param), &ext_param);
0500 sctp_addto_param(retval, num_ext, extensions);
0501 }
0502 if (asoc->peer.prsctp_capable)
0503 sctp_addto_chunk(retval, sizeof(prsctp_param), &prsctp_param);
0504
0505 if (sp->adaptation_ind) {
0506 aiparam.param_hdr.type = SCTP_PARAM_ADAPTATION_LAYER_IND;
0507 aiparam.param_hdr.length = htons(sizeof(aiparam));
0508 aiparam.adaptation_ind = htonl(sp->adaptation_ind);
0509 sctp_addto_chunk(retval, sizeof(aiparam), &aiparam);
0510 }
0511
0512 if (asoc->peer.auth_capable) {
0513 sctp_addto_chunk(retval, ntohs(auth_random->length),
0514 auth_random);
0515 if (auth_hmacs)
0516 sctp_addto_chunk(retval, ntohs(auth_hmacs->length),
0517 auth_hmacs);
0518 if (auth_chunks)
0519 sctp_addto_chunk(retval, ntohs(auth_chunks->length),
0520 auth_chunks);
0521 }
0522
0523
0524 retval->asoc = (struct sctp_association *) asoc;
0525
0526 nomem_chunk:
0527 kfree(cookie);
0528 nomem_cookie:
0529 kfree(addrs.v);
0530 return retval;
0531 }
0532
0533
0534
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566
0567 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
0568 const struct sctp_chunk *chunk)
0569 {
0570 struct sctp_chunk *retval;
0571 int cookie_len;
0572 void *cookie;
0573
0574 cookie = asoc->peer.cookie;
0575 cookie_len = asoc->peer.cookie_len;
0576
0577
0578 retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ECHO, 0,
0579 cookie_len, GFP_ATOMIC);
0580 if (!retval)
0581 goto nodata;
0582 retval->subh.cookie_hdr =
0583 sctp_addto_chunk(retval, cookie_len, cookie);
0584
0585
0586
0587
0588
0589
0590
0591
0592
0593
0594 if (chunk)
0595 retval->transport = chunk->transport;
0596
0597 nodata:
0598 return retval;
0599 }
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
0620 const struct sctp_chunk *chunk)
0621 {
0622 struct sctp_chunk *retval;
0623
0624 retval = sctp_make_control(asoc, SCTP_CID_COOKIE_ACK, 0, 0, GFP_ATOMIC);
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635 if (retval && chunk && chunk->transport)
0636 retval->transport =
0637 sctp_assoc_lookup_paddr(asoc,
0638 &chunk->transport->ipaddr);
0639
0640 return retval;
0641 }
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660
0661
0662
0663
0664
0665
0666 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
0667 const __u32 lowest_tsn,
0668 const struct sctp_chunk *chunk)
0669 {
0670 struct sctp_chunk *retval;
0671 struct sctp_cwrhdr cwr;
0672
0673 cwr.lowest_tsn = htonl(lowest_tsn);
0674 retval = sctp_make_control(asoc, SCTP_CID_ECN_CWR, 0,
0675 sizeof(cwr), GFP_ATOMIC);
0676
0677 if (!retval)
0678 goto nodata;
0679
0680 retval->subh.ecn_cwr_hdr =
0681 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692
0693 if (chunk)
0694 retval->transport = chunk->transport;
0695
0696 nodata:
0697 return retval;
0698 }
0699
0700
0701 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
0702 const __u32 lowest_tsn)
0703 {
0704 struct sctp_chunk *retval;
0705 struct sctp_ecnehdr ecne;
0706
0707 ecne.lowest_tsn = htonl(lowest_tsn);
0708 retval = sctp_make_control(asoc, SCTP_CID_ECN_ECNE, 0,
0709 sizeof(ecne), GFP_ATOMIC);
0710 if (!retval)
0711 goto nodata;
0712 retval->subh.ecne_hdr =
0713 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
0714
0715 nodata:
0716 return retval;
0717 }
0718
0719
0720
0721
0722 struct sctp_chunk *sctp_make_datafrag_empty(const struct sctp_association *asoc,
0723 const struct sctp_sndrcvinfo *sinfo,
0724 int len, __u8 flags, gfp_t gfp)
0725 {
0726 struct sctp_chunk *retval;
0727 struct sctp_datahdr dp;
0728
0729
0730
0731
0732 memset(&dp, 0, sizeof(dp));
0733 dp.ppid = sinfo->sinfo_ppid;
0734 dp.stream = htons(sinfo->sinfo_stream);
0735
0736
0737 if (sinfo->sinfo_flags & SCTP_UNORDERED)
0738 flags |= SCTP_DATA_UNORDERED;
0739
0740 retval = sctp_make_data(asoc, flags, sizeof(dp) + len, gfp);
0741 if (!retval)
0742 return NULL;
0743
0744 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
0745 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
0746
0747 return retval;
0748 }
0749
0750
0751
0752
0753
0754 struct sctp_chunk *sctp_make_sack(struct sctp_association *asoc)
0755 {
0756 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
0757 struct sctp_gap_ack_block gabs[SCTP_MAX_GABS];
0758 __u16 num_gabs, num_dup_tsns;
0759 struct sctp_transport *trans;
0760 struct sctp_chunk *retval;
0761 struct sctp_sackhdr sack;
0762 __u32 ctsn;
0763 int len;
0764
0765 memset(gabs, 0, sizeof(gabs));
0766 ctsn = sctp_tsnmap_get_ctsn(map);
0767
0768 pr_debug("%s: sackCTSNAck sent:0x%x\n", __func__, ctsn);
0769
0770
0771 num_gabs = sctp_tsnmap_num_gabs(map, gabs);
0772 num_dup_tsns = sctp_tsnmap_num_dups(map);
0773
0774
0775 sack.cum_tsn_ack = htonl(ctsn);
0776 sack.a_rwnd = htonl(asoc->a_rwnd);
0777 sack.num_gap_ack_blocks = htons(num_gabs);
0778 sack.num_dup_tsns = htons(num_dup_tsns);
0779
0780 len = sizeof(sack)
0781 + sizeof(struct sctp_gap_ack_block) * num_gabs
0782 + sizeof(__u32) * num_dup_tsns;
0783
0784
0785 retval = sctp_make_control(asoc, SCTP_CID_SACK, 0, len, GFP_ATOMIC);
0786 if (!retval)
0787 goto nodata;
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810
0811
0812
0813
0814
0815
0816
0817
0818
0819 retval->transport = asoc->peer.last_data_from;
0820
0821 retval->subh.sack_hdr =
0822 sctp_addto_chunk(retval, sizeof(sack), &sack);
0823
0824
0825 if (num_gabs)
0826 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
0827 gabs);
0828
0829
0830 if (num_dup_tsns) {
0831 asoc->stats.idupchunks += num_dup_tsns;
0832 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
0833 sctp_tsnmap_get_dups(map));
0834 }
0835
0836
0837
0838
0839
0840
0841
0842
0843 if (++asoc->peer.sack_generation == 0) {
0844 list_for_each_entry(trans, &asoc->peer.transport_addr_list,
0845 transports)
0846 trans->sack_generation = 0;
0847 asoc->peer.sack_generation = 1;
0848 }
0849 nodata:
0850 return retval;
0851 }
0852
0853
0854 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc,
0855 const struct sctp_chunk *chunk)
0856 {
0857 struct sctp_shutdownhdr shut;
0858 struct sctp_chunk *retval;
0859 __u32 ctsn;
0860
0861 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
0862 shut.cum_tsn_ack = htonl(ctsn);
0863
0864 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN, 0,
0865 sizeof(shut), GFP_ATOMIC);
0866 if (!retval)
0867 goto nodata;
0868
0869 retval->subh.shutdown_hdr =
0870 sctp_addto_chunk(retval, sizeof(shut), &shut);
0871
0872 if (chunk)
0873 retval->transport = chunk->transport;
0874 nodata:
0875 return retval;
0876 }
0877
0878 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
0879 const struct sctp_chunk *chunk)
0880 {
0881 struct sctp_chunk *retval;
0882
0883 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0,
0884 GFP_ATOMIC);
0885
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895 if (retval && chunk)
0896 retval->transport = chunk->transport;
0897
0898 return retval;
0899 }
0900
0901 struct sctp_chunk *sctp_make_shutdown_complete(
0902 const struct sctp_association *asoc,
0903 const struct sctp_chunk *chunk)
0904 {
0905 struct sctp_chunk *retval;
0906 __u8 flags = 0;
0907
0908
0909
0910
0911 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
0912
0913 retval = sctp_make_control(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags,
0914 0, GFP_ATOMIC);
0915
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926 if (retval && chunk)
0927 retval->transport = chunk->transport;
0928
0929 return retval;
0930 }
0931
0932
0933
0934
0935 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
0936 const struct sctp_chunk *chunk,
0937 const size_t hint)
0938 {
0939 struct sctp_chunk *retval;
0940 __u8 flags = 0;
0941
0942
0943
0944
0945 if (!asoc) {
0946 if (chunk && chunk->chunk_hdr &&
0947 chunk->chunk_hdr->type == SCTP_CID_INIT)
0948 flags = 0;
0949 else
0950 flags = SCTP_CHUNK_FLAG_T;
0951 }
0952
0953 retval = sctp_make_control(asoc, SCTP_CID_ABORT, flags, hint,
0954 GFP_ATOMIC);
0955
0956
0957
0958
0959
0960
0961
0962
0963
0964
0965 if (retval && chunk)
0966 retval->transport = chunk->transport;
0967
0968 return retval;
0969 }
0970
0971
0972 struct sctp_chunk *sctp_make_abort_no_data(
0973 const struct sctp_association *asoc,
0974 const struct sctp_chunk *chunk,
0975 __u32 tsn)
0976 {
0977 struct sctp_chunk *retval;
0978 __be32 payload;
0979
0980 retval = sctp_make_abort(asoc, chunk,
0981 sizeof(struct sctp_errhdr) + sizeof(tsn));
0982
0983 if (!retval)
0984 goto no_mem;
0985
0986
0987 payload = htonl(tsn);
0988 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, sizeof(payload));
0989 sctp_addto_chunk(retval, sizeof(payload), (const void *)&payload);
0990
0991
0992
0993
0994
0995
0996
0997
0998
0999
1000 if (chunk)
1001 retval->transport = chunk->transport;
1002
1003 no_mem:
1004 return retval;
1005 }
1006
1007
1008 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
1009 struct msghdr *msg,
1010 size_t paylen)
1011 {
1012 struct sctp_chunk *retval;
1013 void *payload = NULL;
1014 int err;
1015
1016 retval = sctp_make_abort(asoc, NULL,
1017 sizeof(struct sctp_errhdr) + paylen);
1018 if (!retval)
1019 goto err_chunk;
1020
1021 if (paylen) {
1022
1023 payload = kmalloc(paylen, GFP_KERNEL);
1024 if (!payload)
1025 goto err_payload;
1026
1027 err = memcpy_from_msg(payload, msg, paylen);
1028 if (err < 0)
1029 goto err_copy;
1030 }
1031
1032 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, paylen);
1033 sctp_addto_chunk(retval, paylen, payload);
1034
1035 if (paylen)
1036 kfree(payload);
1037
1038 return retval;
1039
1040 err_copy:
1041 kfree(payload);
1042 err_payload:
1043 sctp_chunk_free(retval);
1044 retval = NULL;
1045 err_chunk:
1046 return retval;
1047 }
1048
1049
1050
1051
1052 static void *sctp_addto_param(struct sctp_chunk *chunk, int len,
1053 const void *data)
1054 {
1055 int chunklen = ntohs(chunk->chunk_hdr->length);
1056 void *target;
1057
1058 target = skb_put(chunk->skb, len);
1059
1060 if (data)
1061 memcpy(target, data, len);
1062 else
1063 memset(target, 0, len);
1064
1065
1066 chunk->chunk_hdr->length = htons(chunklen + len);
1067 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1068
1069 return target;
1070 }
1071
1072
1073 struct sctp_chunk *sctp_make_abort_violation(
1074 const struct sctp_association *asoc,
1075 const struct sctp_chunk *chunk,
1076 const __u8 *payload,
1077 const size_t paylen)
1078 {
1079 struct sctp_chunk *retval;
1080 struct sctp_paramhdr phdr;
1081
1082 retval = sctp_make_abort(asoc, chunk, sizeof(struct sctp_errhdr) +
1083 paylen + sizeof(phdr));
1084 if (!retval)
1085 goto end;
1086
1087 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, paylen +
1088 sizeof(phdr));
1089
1090 phdr.type = htons(chunk->chunk_hdr->type);
1091 phdr.length = chunk->chunk_hdr->length;
1092 sctp_addto_chunk(retval, paylen, payload);
1093 sctp_addto_param(retval, sizeof(phdr), &phdr);
1094
1095 end:
1096 return retval;
1097 }
1098
1099 struct sctp_chunk *sctp_make_violation_paramlen(
1100 const struct sctp_association *asoc,
1101 const struct sctp_chunk *chunk,
1102 struct sctp_paramhdr *param)
1103 {
1104 static const char error[] = "The following parameter had invalid length:";
1105 size_t payload_len = sizeof(error) + sizeof(struct sctp_errhdr) +
1106 sizeof(*param);
1107 struct sctp_chunk *retval;
1108
1109 retval = sctp_make_abort(asoc, chunk, payload_len);
1110 if (!retval)
1111 goto nodata;
1112
1113 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION,
1114 sizeof(error) + sizeof(*param));
1115 sctp_addto_chunk(retval, sizeof(error), error);
1116 sctp_addto_param(retval, sizeof(*param), param);
1117
1118 nodata:
1119 return retval;
1120 }
1121
1122 struct sctp_chunk *sctp_make_violation_max_retrans(
1123 const struct sctp_association *asoc,
1124 const struct sctp_chunk *chunk)
1125 {
1126 static const char error[] = "Association exceeded its max_retrans count";
1127 size_t payload_len = sizeof(error) + sizeof(struct sctp_errhdr);
1128 struct sctp_chunk *retval;
1129
1130 retval = sctp_make_abort(asoc, chunk, payload_len);
1131 if (!retval)
1132 goto nodata;
1133
1134 sctp_init_cause(retval, SCTP_ERROR_PROTO_VIOLATION, sizeof(error));
1135 sctp_addto_chunk(retval, sizeof(error), error);
1136
1137 nodata:
1138 return retval;
1139 }
1140
1141 struct sctp_chunk *sctp_make_new_encap_port(const struct sctp_association *asoc,
1142 const struct sctp_chunk *chunk)
1143 {
1144 struct sctp_new_encap_port_hdr nep;
1145 struct sctp_chunk *retval;
1146
1147 retval = sctp_make_abort(asoc, chunk,
1148 sizeof(struct sctp_errhdr) + sizeof(nep));
1149 if (!retval)
1150 goto nodata;
1151
1152 sctp_init_cause(retval, SCTP_ERROR_NEW_ENCAP_PORT, sizeof(nep));
1153 nep.cur_port = SCTP_INPUT_CB(chunk->skb)->encap_port;
1154 nep.new_port = chunk->transport->encap_port;
1155 sctp_addto_chunk(retval, sizeof(nep), &nep);
1156
1157 nodata:
1158 return retval;
1159 }
1160
1161
1162 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
1163 const struct sctp_transport *transport,
1164 __u32 probe_size)
1165 {
1166 struct sctp_sender_hb_info hbinfo = {};
1167 struct sctp_chunk *retval;
1168
1169 retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT, 0,
1170 sizeof(hbinfo), GFP_ATOMIC);
1171
1172 if (!retval)
1173 goto nodata;
1174
1175 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
1176 hbinfo.param_hdr.length = htons(sizeof(hbinfo));
1177 hbinfo.daddr = transport->ipaddr;
1178 hbinfo.sent_at = jiffies;
1179 hbinfo.hb_nonce = transport->hb_nonce;
1180 hbinfo.probe_size = probe_size;
1181
1182
1183
1184
1185 retval->transport = (struct sctp_transport *) transport;
1186 retval->subh.hbs_hdr = sctp_addto_chunk(retval, sizeof(hbinfo),
1187 &hbinfo);
1188 retval->pmtu_probe = !!probe_size;
1189
1190 nodata:
1191 return retval;
1192 }
1193
1194 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
1195 const struct sctp_chunk *chunk,
1196 const void *payload,
1197 const size_t paylen)
1198 {
1199 struct sctp_chunk *retval;
1200
1201 retval = sctp_make_control(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen,
1202 GFP_ATOMIC);
1203 if (!retval)
1204 goto nodata;
1205
1206 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217 if (chunk)
1218 retval->transport = chunk->transport;
1219
1220 nodata:
1221 return retval;
1222 }
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235 struct sctp_chunk *sctp_make_pad(const struct sctp_association *asoc, int len)
1236 {
1237 struct sctp_chunk *retval;
1238
1239 retval = sctp_make_control(asoc, SCTP_CID_PAD, 0, len, GFP_ATOMIC);
1240 if (!retval)
1241 return NULL;
1242
1243 skb_put_zero(retval->skb, len);
1244 retval->chunk_hdr->length = htons(ntohs(retval->chunk_hdr->length) + len);
1245 retval->chunk_end = skb_tail_pointer(retval->skb);
1246
1247 return retval;
1248 }
1249
1250
1251
1252
1253 static struct sctp_chunk *sctp_make_op_error_space(
1254 const struct sctp_association *asoc,
1255 const struct sctp_chunk *chunk,
1256 size_t size)
1257 {
1258 struct sctp_chunk *retval;
1259
1260 retval = sctp_make_control(asoc, SCTP_CID_ERROR, 0,
1261 sizeof(struct sctp_errhdr) + size,
1262 GFP_ATOMIC);
1263 if (!retval)
1264 goto nodata;
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274 if (chunk)
1275 retval->transport = chunk->transport;
1276
1277 nodata:
1278 return retval;
1279 }
1280
1281
1282
1283
1284
1285
1286
1287
1288 static inline struct sctp_chunk *sctp_make_op_error_limited(
1289 const struct sctp_association *asoc,
1290 const struct sctp_chunk *chunk)
1291 {
1292 size_t size = SCTP_DEFAULT_MAXSEGMENT;
1293 struct sctp_sock *sp = NULL;
1294
1295 if (asoc) {
1296 size = min_t(size_t, size, asoc->pathmtu);
1297 sp = sctp_sk(asoc->base.sk);
1298 }
1299
1300 size = sctp_mtu_payload(sp, size, sizeof(struct sctp_errhdr));
1301
1302 return sctp_make_op_error_space(asoc, chunk, size);
1303 }
1304
1305
1306 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
1307 const struct sctp_chunk *chunk,
1308 __be16 cause_code, const void *payload,
1309 size_t paylen, size_t reserve_tail)
1310 {
1311 struct sctp_chunk *retval;
1312
1313 retval = sctp_make_op_error_space(asoc, chunk, paylen + reserve_tail);
1314 if (!retval)
1315 goto nodata;
1316
1317 sctp_init_cause(retval, cause_code, paylen + reserve_tail);
1318 sctp_addto_chunk(retval, paylen, payload);
1319 if (reserve_tail)
1320 sctp_addto_param(retval, reserve_tail, NULL);
1321
1322 nodata:
1323 return retval;
1324 }
1325
1326 struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc,
1327 __u16 key_id)
1328 {
1329 struct sctp_authhdr auth_hdr;
1330 struct sctp_hmac *hmac_desc;
1331 struct sctp_chunk *retval;
1332
1333
1334 hmac_desc = sctp_auth_asoc_get_hmac(asoc);
1335 if (unlikely(!hmac_desc))
1336 return NULL;
1337
1338 retval = sctp_make_control(asoc, SCTP_CID_AUTH, 0,
1339 hmac_desc->hmac_len + sizeof(auth_hdr),
1340 GFP_ATOMIC);
1341 if (!retval)
1342 return NULL;
1343
1344 auth_hdr.hmac_id = htons(hmac_desc->hmac_id);
1345 auth_hdr.shkey_id = htons(key_id);
1346
1347 retval->subh.auth_hdr = sctp_addto_chunk(retval, sizeof(auth_hdr),
1348 &auth_hdr);
1349
1350 skb_put_zero(retval->skb, hmac_desc->hmac_len);
1351
1352
1353 retval->chunk_hdr->length =
1354 htons(ntohs(retval->chunk_hdr->length) + hmac_desc->hmac_len);
1355 retval->chunk_end = skb_tail_pointer(retval->skb);
1356
1357 return retval;
1358 }
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1376 const struct sctp_association *asoc,
1377 struct sock *sk, gfp_t gfp)
1378 {
1379 struct sctp_chunk *retval;
1380
1381 retval = kmem_cache_zalloc(sctp_chunk_cachep, gfp);
1382
1383 if (!retval)
1384 goto nodata;
1385 if (!sk)
1386 pr_debug("%s: chunkifying skb:%p w/o an sk\n", __func__, skb);
1387
1388 INIT_LIST_HEAD(&retval->list);
1389 retval->skb = skb;
1390 retval->asoc = (struct sctp_association *)asoc;
1391 retval->singleton = 1;
1392
1393 retval->fast_retransmit = SCTP_CAN_FRTX;
1394
1395
1396 INIT_LIST_HEAD(&retval->transmitted_list);
1397 INIT_LIST_HEAD(&retval->frag_list);
1398 SCTP_DBG_OBJCNT_INC(chunk);
1399 refcount_set(&retval->refcnt, 1);
1400
1401 nodata:
1402 return retval;
1403 }
1404
1405
1406 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1407 union sctp_addr *dest)
1408 {
1409 memcpy(&chunk->source, src, sizeof(union sctp_addr));
1410 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1411 }
1412
1413
1414 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1415 {
1416
1417 if (chunk->transport) {
1418 return &chunk->transport->ipaddr;
1419 } else {
1420
1421 return &chunk->source;
1422 }
1423 }
1424
1425
1426
1427
1428 static struct sctp_chunk *_sctp_make_chunk(const struct sctp_association *asoc,
1429 __u8 type, __u8 flags, int paylen,
1430 gfp_t gfp)
1431 {
1432 struct sctp_chunkhdr *chunk_hdr;
1433 struct sctp_chunk *retval;
1434 struct sk_buff *skb;
1435 struct sock *sk;
1436 int chunklen;
1437
1438 chunklen = SCTP_PAD4(sizeof(*chunk_hdr) + paylen);
1439 if (chunklen > SCTP_MAX_CHUNK_LEN)
1440 goto nodata;
1441
1442
1443 skb = alloc_skb(chunklen, gfp);
1444 if (!skb)
1445 goto nodata;
1446
1447
1448 chunk_hdr = (struct sctp_chunkhdr *)skb_put(skb, sizeof(*chunk_hdr));
1449 chunk_hdr->type = type;
1450 chunk_hdr->flags = flags;
1451 chunk_hdr->length = htons(sizeof(*chunk_hdr));
1452
1453 sk = asoc ? asoc->base.sk : NULL;
1454 retval = sctp_chunkify(skb, asoc, sk, gfp);
1455 if (!retval) {
1456 kfree_skb(skb);
1457 goto nodata;
1458 }
1459
1460 retval->chunk_hdr = chunk_hdr;
1461 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(*chunk_hdr);
1462
1463
1464 if (sctp_auth_send_cid(type, asoc))
1465 retval->auth = 1;
1466
1467 return retval;
1468 nodata:
1469 return NULL;
1470 }
1471
1472 static struct sctp_chunk *sctp_make_data(const struct sctp_association *asoc,
1473 __u8 flags, int paylen, gfp_t gfp)
1474 {
1475 return _sctp_make_chunk(asoc, SCTP_CID_DATA, flags, paylen, gfp);
1476 }
1477
1478 struct sctp_chunk *sctp_make_idata(const struct sctp_association *asoc,
1479 __u8 flags, int paylen, gfp_t gfp)
1480 {
1481 return _sctp_make_chunk(asoc, SCTP_CID_I_DATA, flags, paylen, gfp);
1482 }
1483
1484 static struct sctp_chunk *sctp_make_control(const struct sctp_association *asoc,
1485 __u8 type, __u8 flags, int paylen,
1486 gfp_t gfp)
1487 {
1488 struct sctp_chunk *chunk;
1489
1490 chunk = _sctp_make_chunk(asoc, type, flags, paylen, gfp);
1491 if (chunk)
1492 sctp_control_set_owner_w(chunk);
1493
1494 return chunk;
1495 }
1496
1497
1498 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1499 {
1500 BUG_ON(!list_empty(&chunk->list));
1501 list_del_init(&chunk->transmitted_list);
1502
1503 consume_skb(chunk->skb);
1504 consume_skb(chunk->auth_chunk);
1505
1506 SCTP_DBG_OBJCNT_DEC(chunk);
1507 kmem_cache_free(sctp_chunk_cachep, chunk);
1508 }
1509
1510
1511 void sctp_chunk_free(struct sctp_chunk *chunk)
1512 {
1513
1514 if (chunk->msg)
1515 sctp_datamsg_put(chunk->msg);
1516
1517 sctp_chunk_put(chunk);
1518 }
1519
1520
1521 void sctp_chunk_hold(struct sctp_chunk *ch)
1522 {
1523 refcount_inc(&ch->refcnt);
1524 }
1525
1526
1527 void sctp_chunk_put(struct sctp_chunk *ch)
1528 {
1529 if (refcount_dec_and_test(&ch->refcnt))
1530 sctp_chunk_destroy(ch);
1531 }
1532
1533
1534
1535
1536 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1537 {
1538 int chunklen = ntohs(chunk->chunk_hdr->length);
1539 int padlen = SCTP_PAD4(chunklen) - chunklen;
1540 void *target;
1541
1542 skb_put_zero(chunk->skb, padlen);
1543 target = skb_put_data(chunk->skb, data, len);
1544
1545
1546 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1547 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1548
1549 return target;
1550 }
1551
1552
1553
1554
1555
1556 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int len,
1557 struct iov_iter *from)
1558 {
1559 void *target;
1560
1561
1562 target = skb_put(chunk->skb, len);
1563
1564
1565 if (!copy_from_iter_full(target, len, from))
1566 return -EFAULT;
1567
1568
1569 chunk->chunk_hdr->length =
1570 htons(ntohs(chunk->chunk_hdr->length) + len);
1571 chunk->chunk_end = skb_tail_pointer(chunk->skb);
1572
1573 return 0;
1574 }
1575
1576
1577
1578
1579 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1580 {
1581 struct sctp_stream *stream;
1582 struct sctp_chunk *lchunk;
1583 struct sctp_datamsg *msg;
1584 __u16 ssn, sid;
1585
1586 if (chunk->has_ssn)
1587 return;
1588
1589
1590 sid = ntohs(chunk->subh.data_hdr->stream);
1591 stream = &chunk->asoc->stream;
1592
1593
1594
1595
1596 msg = chunk->msg;
1597 list_for_each_entry(lchunk, &msg->chunks, frag_list) {
1598 if (lchunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1599 ssn = 0;
1600 } else {
1601 if (lchunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1602 ssn = sctp_ssn_next(stream, out, sid);
1603 else
1604 ssn = sctp_ssn_peek(stream, out, sid);
1605 }
1606
1607 lchunk->subh.data_hdr->ssn = htons(ssn);
1608 lchunk->has_ssn = 1;
1609 }
1610 }
1611
1612
1613
1614
1615 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1616 {
1617 if (!chunk->has_tsn) {
1618
1619
1620
1621 chunk->subh.data_hdr->tsn =
1622 htonl(sctp_association_get_next_tsn(chunk->asoc));
1623 chunk->has_tsn = 1;
1624 }
1625 }
1626
1627
1628 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1629 struct sctp_chunk *chunk,
1630 gfp_t gfp)
1631 {
1632 struct sctp_association *asoc;
1633 enum sctp_scope scope;
1634 struct sk_buff *skb;
1635
1636
1637 scope = sctp_scope(sctp_source(chunk));
1638 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1639 if (!asoc)
1640 goto nodata;
1641 asoc->temp = 1;
1642 skb = chunk->skb;
1643
1644 SCTP_INPUT_CB(skb)->af->from_skb(&asoc->c.peer_addr, skb, 1);
1645
1646 nodata:
1647 return asoc;
1648 }
1649
1650
1651
1652
1653 static struct sctp_cookie_param *sctp_pack_cookie(
1654 const struct sctp_endpoint *ep,
1655 const struct sctp_association *asoc,
1656 const struct sctp_chunk *init_chunk,
1657 int *cookie_len, const __u8 *raw_addrs,
1658 int addrs_len)
1659 {
1660 struct sctp_signed_cookie *cookie;
1661 struct sctp_cookie_param *retval;
1662 int headersize, bodysize;
1663
1664
1665
1666
1667 headersize = sizeof(struct sctp_paramhdr) +
1668 (sizeof(struct sctp_signed_cookie) -
1669 sizeof(struct sctp_cookie));
1670 bodysize = sizeof(struct sctp_cookie)
1671 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1672
1673
1674
1675
1676 if (bodysize % SCTP_COOKIE_MULTIPLE)
1677 bodysize += SCTP_COOKIE_MULTIPLE
1678 - (bodysize % SCTP_COOKIE_MULTIPLE);
1679 *cookie_len = headersize + bodysize;
1680
1681
1682
1683
1684 retval = kzalloc(*cookie_len, GFP_ATOMIC);
1685 if (!retval)
1686 goto nodata;
1687
1688 cookie = (struct sctp_signed_cookie *) retval->body;
1689
1690
1691 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1692 retval->p.length = htons(*cookie_len);
1693
1694
1695 cookie->c = asoc->c;
1696
1697 cookie->c.raw_addr_list_len = addrs_len;
1698
1699
1700 cookie->c.prsctp_capable = asoc->peer.prsctp_capable;
1701
1702
1703 cookie->c.adaptation_ind = asoc->peer.adaptation_ind;
1704
1705
1706 cookie->c.expiration = ktime_add(asoc->cookie_life,
1707 ktime_get_real());
1708
1709
1710 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1711 ntohs(init_chunk->chunk_hdr->length));
1712
1713
1714 memcpy((__u8 *)&cookie->c.peer_init[0] +
1715 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1716
1717 if (sctp_sk(ep->base.sk)->hmac) {
1718 struct crypto_shash *tfm = sctp_sk(ep->base.sk)->hmac;
1719 int err;
1720
1721
1722 err = crypto_shash_setkey(tfm, ep->secret_key,
1723 sizeof(ep->secret_key)) ?:
1724 crypto_shash_tfm_digest(tfm, (u8 *)&cookie->c, bodysize,
1725 cookie->signature);
1726 if (err)
1727 goto free_cookie;
1728 }
1729
1730 return retval;
1731
1732 free_cookie:
1733 kfree(retval);
1734 nodata:
1735 *cookie_len = 0;
1736 return NULL;
1737 }
1738
1739
1740 struct sctp_association *sctp_unpack_cookie(
1741 const struct sctp_endpoint *ep,
1742 const struct sctp_association *asoc,
1743 struct sctp_chunk *chunk, gfp_t gfp,
1744 int *error, struct sctp_chunk **errp)
1745 {
1746 struct sctp_association *retval = NULL;
1747 int headersize, bodysize, fixed_size;
1748 struct sctp_signed_cookie *cookie;
1749 struct sk_buff *skb = chunk->skb;
1750 struct sctp_cookie *bear_cookie;
1751 __u8 *digest = ep->digest;
1752 enum sctp_scope scope;
1753 unsigned int len;
1754 ktime_t kt;
1755
1756
1757
1758
1759 headersize = sizeof(struct sctp_chunkhdr) +
1760 (sizeof(struct sctp_signed_cookie) -
1761 sizeof(struct sctp_cookie));
1762 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1763 fixed_size = headersize + sizeof(struct sctp_cookie);
1764
1765
1766
1767
1768
1769 len = ntohs(chunk->chunk_hdr->length);
1770 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1771 goto malformed;
1772
1773
1774 if (bodysize % SCTP_COOKIE_MULTIPLE)
1775 goto malformed;
1776
1777
1778 cookie = chunk->subh.cookie_hdr;
1779 bear_cookie = &cookie->c;
1780
1781 if (!sctp_sk(ep->base.sk)->hmac)
1782 goto no_hmac;
1783
1784
1785 {
1786 struct crypto_shash *tfm = sctp_sk(ep->base.sk)->hmac;
1787 int err;
1788
1789 err = crypto_shash_setkey(tfm, ep->secret_key,
1790 sizeof(ep->secret_key)) ?:
1791 crypto_shash_tfm_digest(tfm, (u8 *)bear_cookie, bodysize,
1792 digest);
1793 if (err) {
1794 *error = -SCTP_IERROR_NOMEM;
1795 goto fail;
1796 }
1797 }
1798
1799 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1800 *error = -SCTP_IERROR_BAD_SIG;
1801 goto fail;
1802 }
1803
1804 no_hmac:
1805
1806
1807
1808
1809
1810
1811
1812 if (ntohl(chunk->sctp_hdr->vtag) != bear_cookie->my_vtag) {
1813 *error = -SCTP_IERROR_BAD_TAG;
1814 goto fail;
1815 }
1816
1817 if (chunk->sctp_hdr->source != bear_cookie->peer_addr.v4.sin_port ||
1818 ntohs(chunk->sctp_hdr->dest) != bear_cookie->my_port) {
1819 *error = -SCTP_IERROR_BAD_PORTS;
1820 goto fail;
1821 }
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831 if (sock_flag(ep->base.sk, SOCK_TIMESTAMP))
1832 kt = skb_get_ktime(skb);
1833 else
1834 kt = ktime_get_real();
1835
1836 if (!asoc && ktime_before(bear_cookie->expiration, kt)) {
1837 suseconds_t usecs = ktime_to_us(ktime_sub(kt, bear_cookie->expiration));
1838 __be32 n = htonl(usecs);
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848 *errp = sctp_make_op_error(asoc, chunk,
1849 SCTP_ERROR_STALE_COOKIE, &n,
1850 sizeof(n), 0);
1851 if (*errp)
1852 *error = -SCTP_IERROR_STALE_COOKIE;
1853 else
1854 *error = -SCTP_IERROR_NOMEM;
1855
1856 goto fail;
1857 }
1858
1859
1860 scope = sctp_scope(sctp_source(chunk));
1861 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1862 if (!retval) {
1863 *error = -SCTP_IERROR_NOMEM;
1864 goto fail;
1865 }
1866
1867
1868 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1869
1870
1871 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1872
1873 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1874 GFP_ATOMIC) < 0) {
1875 *error = -SCTP_IERROR_NOMEM;
1876 goto fail;
1877 }
1878
1879
1880 if (list_empty(&retval->base.bind_addr.address_list)) {
1881 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1882 sizeof(chunk->dest), SCTP_ADDR_SRC,
1883 GFP_ATOMIC);
1884 }
1885
1886 retval->next_tsn = retval->c.initial_tsn;
1887 retval->ctsn_ack_point = retval->next_tsn - 1;
1888 retval->addip_serial = retval->c.initial_tsn;
1889 retval->strreset_outseq = retval->c.initial_tsn;
1890 retval->adv_peer_ack_point = retval->ctsn_ack_point;
1891 retval->peer.prsctp_capable = retval->c.prsctp_capable;
1892 retval->peer.adaptation_ind = retval->c.adaptation_ind;
1893
1894
1895 return retval;
1896
1897 fail:
1898 if (retval)
1899 sctp_association_free(retval);
1900
1901 return NULL;
1902
1903 malformed:
1904
1905
1906
1907 *error = -SCTP_IERROR_MALFORMED;
1908 goto fail;
1909 }
1910
1911
1912
1913
1914
1915 struct __sctp_missing {
1916 __be32 num_missing;
1917 __be16 type;
1918 } __packed;
1919
1920
1921
1922
1923 static int sctp_process_missing_param(const struct sctp_association *asoc,
1924 enum sctp_param paramtype,
1925 struct sctp_chunk *chunk,
1926 struct sctp_chunk **errp)
1927 {
1928 struct __sctp_missing report;
1929 __u16 len;
1930
1931 len = SCTP_PAD4(sizeof(report));
1932
1933
1934
1935
1936 if (!*errp)
1937 *errp = sctp_make_op_error_space(asoc, chunk, len);
1938
1939 if (*errp) {
1940 report.num_missing = htonl(1);
1941 report.type = paramtype;
1942 sctp_init_cause(*errp, SCTP_ERROR_MISS_PARAM,
1943 sizeof(report));
1944 sctp_addto_chunk(*errp, sizeof(report), &report);
1945 }
1946
1947
1948 return 0;
1949 }
1950
1951
1952 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1953 struct sctp_chunk *chunk,
1954 struct sctp_chunk **errp)
1955 {
1956
1957
1958 if (!*errp)
1959 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1960
1961 if (*errp)
1962 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, 0);
1963
1964
1965 return 0;
1966 }
1967
1968 static int sctp_process_inv_paramlength(const struct sctp_association *asoc,
1969 struct sctp_paramhdr *param,
1970 const struct sctp_chunk *chunk,
1971 struct sctp_chunk **errp)
1972 {
1973
1974
1975
1976 if (*errp)
1977 sctp_chunk_free(*errp);
1978
1979
1980 *errp = sctp_make_violation_paramlen(asoc, chunk, param);
1981
1982 return 0;
1983 }
1984
1985
1986
1987
1988
1989 static int sctp_process_hn_param(const struct sctp_association *asoc,
1990 union sctp_params param,
1991 struct sctp_chunk *chunk,
1992 struct sctp_chunk **errp)
1993 {
1994 __u16 len = ntohs(param.p->length);
1995
1996
1997
1998
1999
2000
2001 if (*errp)
2002 sctp_chunk_free(*errp);
2003
2004 *errp = sctp_make_op_error(asoc, chunk, SCTP_ERROR_DNS_FAILED,
2005 param.v, len, 0);
2006
2007
2008 return 0;
2009 }
2010
2011 static int sctp_verify_ext_param(struct net *net,
2012 const struct sctp_endpoint *ep,
2013 union sctp_params param)
2014 {
2015 __u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2016 int have_asconf = 0;
2017 int have_auth = 0;
2018 int i;
2019
2020 for (i = 0; i < num_ext; i++) {
2021 switch (param.ext->chunks[i]) {
2022 case SCTP_CID_AUTH:
2023 have_auth = 1;
2024 break;
2025 case SCTP_CID_ASCONF:
2026 case SCTP_CID_ASCONF_ACK:
2027 have_asconf = 1;
2028 break;
2029 }
2030 }
2031
2032
2033
2034
2035
2036
2037 if (net->sctp.addip_noauth)
2038 return 1;
2039
2040 if (ep->asconf_enable && !have_auth && have_asconf)
2041 return 0;
2042
2043 return 1;
2044 }
2045
2046 static void sctp_process_ext_param(struct sctp_association *asoc,
2047 union sctp_params param)
2048 {
2049 __u16 num_ext = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2050 int i;
2051
2052 for (i = 0; i < num_ext; i++) {
2053 switch (param.ext->chunks[i]) {
2054 case SCTP_CID_RECONF:
2055 if (asoc->ep->reconf_enable)
2056 asoc->peer.reconf_capable = 1;
2057 break;
2058 case SCTP_CID_FWD_TSN:
2059 if (asoc->ep->prsctp_enable)
2060 asoc->peer.prsctp_capable = 1;
2061 break;
2062 case SCTP_CID_AUTH:
2063
2064
2065
2066 if (asoc->ep->auth_enable)
2067 asoc->peer.auth_capable = 1;
2068 break;
2069 case SCTP_CID_ASCONF:
2070 case SCTP_CID_ASCONF_ACK:
2071 if (asoc->ep->asconf_enable)
2072 asoc->peer.asconf_capable = 1;
2073 break;
2074 case SCTP_CID_I_DATA:
2075 if (asoc->ep->intl_enable)
2076 asoc->peer.intl_capable = 1;
2077 break;
2078 default:
2079 break;
2080 }
2081 }
2082 }
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109 static enum sctp_ierror sctp_process_unk_param(
2110 const struct sctp_association *asoc,
2111 union sctp_params param,
2112 struct sctp_chunk *chunk,
2113 struct sctp_chunk **errp)
2114 {
2115 int retval = SCTP_IERROR_NO_ERROR;
2116
2117 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
2118 case SCTP_PARAM_ACTION_DISCARD:
2119 retval = SCTP_IERROR_ERROR;
2120 break;
2121 case SCTP_PARAM_ACTION_SKIP:
2122 break;
2123 case SCTP_PARAM_ACTION_DISCARD_ERR:
2124 retval = SCTP_IERROR_ERROR;
2125 fallthrough;
2126 case SCTP_PARAM_ACTION_SKIP_ERR:
2127
2128
2129
2130 if (!*errp) {
2131 *errp = sctp_make_op_error_limited(asoc, chunk);
2132 if (!*errp) {
2133
2134
2135
2136
2137
2138 retval = SCTP_IERROR_NOMEM;
2139 break;
2140 }
2141 }
2142
2143 if (!sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
2144 ntohs(param.p->length)))
2145 sctp_addto_chunk(*errp, ntohs(param.p->length),
2146 param.v);
2147 break;
2148 default:
2149 break;
2150 }
2151
2152 return retval;
2153 }
2154
2155
2156
2157
2158
2159
2160
2161
2162 static enum sctp_ierror sctp_verify_param(struct net *net,
2163 const struct sctp_endpoint *ep,
2164 const struct sctp_association *asoc,
2165 union sctp_params param,
2166 enum sctp_cid cid,
2167 struct sctp_chunk *chunk,
2168 struct sctp_chunk **err_chunk)
2169 {
2170 struct sctp_hmac_algo_param *hmacs;
2171 int retval = SCTP_IERROR_NO_ERROR;
2172 __u16 n_elt, id = 0;
2173 int i;
2174
2175
2176
2177
2178
2179
2180 switch (param.p->type) {
2181 case SCTP_PARAM_IPV4_ADDRESS:
2182 case SCTP_PARAM_IPV6_ADDRESS:
2183 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2184 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2185 case SCTP_PARAM_STATE_COOKIE:
2186 case SCTP_PARAM_HEARTBEAT_INFO:
2187 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2188 case SCTP_PARAM_ECN_CAPABLE:
2189 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2190 break;
2191
2192 case SCTP_PARAM_SUPPORTED_EXT:
2193 if (!sctp_verify_ext_param(net, ep, param))
2194 return SCTP_IERROR_ABORT;
2195 break;
2196
2197 case SCTP_PARAM_SET_PRIMARY:
2198 if (!ep->asconf_enable)
2199 goto unhandled;
2200
2201 if (ntohs(param.p->length) < sizeof(struct sctp_addip_param) +
2202 sizeof(struct sctp_paramhdr)) {
2203 sctp_process_inv_paramlength(asoc, param.p,
2204 chunk, err_chunk);
2205 retval = SCTP_IERROR_ABORT;
2206 }
2207 break;
2208
2209 case SCTP_PARAM_HOST_NAME_ADDRESS:
2210
2211 sctp_process_hn_param(asoc, param, chunk, err_chunk);
2212 retval = SCTP_IERROR_ABORT;
2213 break;
2214
2215 case SCTP_PARAM_FWD_TSN_SUPPORT:
2216 if (ep->prsctp_enable)
2217 break;
2218 goto unhandled;
2219
2220 case SCTP_PARAM_RANDOM:
2221 if (!ep->auth_enable)
2222 goto unhandled;
2223
2224
2225
2226
2227
2228
2229 if (SCTP_AUTH_RANDOM_LENGTH != ntohs(param.p->length) -
2230 sizeof(struct sctp_paramhdr)) {
2231 sctp_process_inv_paramlength(asoc, param.p,
2232 chunk, err_chunk);
2233 retval = SCTP_IERROR_ABORT;
2234 }
2235 break;
2236
2237 case SCTP_PARAM_CHUNKS:
2238 if (!ep->auth_enable)
2239 goto unhandled;
2240
2241
2242
2243
2244
2245
2246 if (260 < ntohs(param.p->length)) {
2247 sctp_process_inv_paramlength(asoc, param.p,
2248 chunk, err_chunk);
2249 retval = SCTP_IERROR_ABORT;
2250 }
2251 break;
2252
2253 case SCTP_PARAM_HMAC_ALGO:
2254 if (!ep->auth_enable)
2255 goto unhandled;
2256
2257 hmacs = (struct sctp_hmac_algo_param *)param.p;
2258 n_elt = (ntohs(param.p->length) -
2259 sizeof(struct sctp_paramhdr)) >> 1;
2260
2261
2262
2263
2264
2265 for (i = 0; i < n_elt; i++) {
2266 id = ntohs(hmacs->hmac_ids[i]);
2267
2268 if (id == SCTP_AUTH_HMAC_ID_SHA1)
2269 break;
2270 }
2271
2272 if (id != SCTP_AUTH_HMAC_ID_SHA1) {
2273 sctp_process_inv_paramlength(asoc, param.p, chunk,
2274 err_chunk);
2275 retval = SCTP_IERROR_ABORT;
2276 }
2277 break;
2278 unhandled:
2279 default:
2280 pr_debug("%s: unrecognized param:%d for chunk:%d\n",
2281 __func__, ntohs(param.p->type), cid);
2282
2283 retval = sctp_process_unk_param(asoc, param, chunk, err_chunk);
2284 break;
2285 }
2286 return retval;
2287 }
2288
2289
2290 int sctp_verify_init(struct net *net, const struct sctp_endpoint *ep,
2291 const struct sctp_association *asoc, enum sctp_cid cid,
2292 struct sctp_init_chunk *peer_init,
2293 struct sctp_chunk *chunk, struct sctp_chunk **errp)
2294 {
2295 union sctp_params param;
2296 bool has_cookie = false;
2297 int result;
2298
2299
2300
2301
2302
2303 if (peer_init->init_hdr.num_outbound_streams == 0 ||
2304 peer_init->init_hdr.num_inbound_streams == 0 ||
2305 peer_init->init_hdr.init_tag == 0 ||
2306 ntohl(peer_init->init_hdr.a_rwnd) < SCTP_DEFAULT_MINWINDOW)
2307 return sctp_process_inv_mandatory(asoc, chunk, errp);
2308
2309 sctp_walk_params(param, peer_init, init_hdr.params) {
2310 if (param.p->type == SCTP_PARAM_STATE_COOKIE)
2311 has_cookie = true;
2312 }
2313
2314
2315
2316
2317
2318
2319
2320
2321 if (param.v != (void *)chunk->chunk_end)
2322 return sctp_process_inv_paramlength(asoc, param.p, chunk, errp);
2323
2324
2325
2326
2327 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie)
2328 return sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
2329 chunk, errp);
2330
2331
2332 sctp_walk_params(param, peer_init, init_hdr.params) {
2333 result = sctp_verify_param(net, ep, asoc, param, cid,
2334 chunk, errp);
2335 switch (result) {
2336 case SCTP_IERROR_ABORT:
2337 case SCTP_IERROR_NOMEM:
2338 return 0;
2339 case SCTP_IERROR_ERROR:
2340 return 1;
2341 case SCTP_IERROR_NO_ERROR:
2342 default:
2343 break;
2344 }
2345
2346 }
2347
2348 return 1;
2349 }
2350
2351
2352
2353
2354
2355 int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk,
2356 const union sctp_addr *peer_addr,
2357 struct sctp_init_chunk *peer_init, gfp_t gfp)
2358 {
2359 struct sctp_transport *transport;
2360 struct list_head *pos, *temp;
2361 union sctp_params param;
2362 union sctp_addr addr;
2363 struct sctp_af *af;
2364 int src_match = 0;
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376 asoc->encap_port = SCTP_INPUT_CB(chunk->skb)->encap_port;
2377 if (!sctp_assoc_add_peer(asoc, peer_addr, gfp, SCTP_ACTIVE))
2378 goto nomem;
2379
2380 if (sctp_cmp_addr_exact(sctp_source(chunk), peer_addr))
2381 src_match = 1;
2382
2383
2384 sctp_walk_params(param, peer_init, init_hdr.params) {
2385 if (!src_match &&
2386 (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
2387 param.p->type == SCTP_PARAM_IPV6_ADDRESS)) {
2388 af = sctp_get_af_specific(param_type2af(param.p->type));
2389 if (!af->from_addr_param(&addr, param.addr,
2390 chunk->sctp_hdr->source, 0))
2391 continue;
2392 if (sctp_cmp_addr_exact(sctp_source(chunk), &addr))
2393 src_match = 1;
2394 }
2395
2396 if (!sctp_process_param(asoc, param, peer_addr, gfp))
2397 goto clean_up;
2398 }
2399
2400
2401 if (!src_match)
2402 goto clean_up;
2403
2404
2405
2406
2407 if (asoc->peer.auth_capable && (!asoc->peer.peer_random ||
2408 !asoc->peer.peer_hmacs))
2409 asoc->peer.auth_capable = 0;
2410
2411
2412
2413
2414
2415
2416
2417 if (!asoc->base.net->sctp.addip_noauth &&
2418 (asoc->peer.asconf_capable && !asoc->peer.auth_capable)) {
2419 asoc->peer.addip_disabled_mask |= (SCTP_PARAM_ADD_IP |
2420 SCTP_PARAM_DEL_IP |
2421 SCTP_PARAM_SET_PRIMARY);
2422 asoc->peer.asconf_capable = 0;
2423 goto clean_up;
2424 }
2425
2426
2427 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2428 transport = list_entry(pos, struct sctp_transport, transports);
2429 if (transport->state == SCTP_UNKNOWN) {
2430 sctp_assoc_rm_peer(asoc, transport);
2431 }
2432 }
2433
2434
2435
2436
2437 asoc->peer.i.init_tag =
2438 ntohl(peer_init->init_hdr.init_tag);
2439 asoc->peer.i.a_rwnd =
2440 ntohl(peer_init->init_hdr.a_rwnd);
2441 asoc->peer.i.num_outbound_streams =
2442 ntohs(peer_init->init_hdr.num_outbound_streams);
2443 asoc->peer.i.num_inbound_streams =
2444 ntohs(peer_init->init_hdr.num_inbound_streams);
2445 asoc->peer.i.initial_tsn =
2446 ntohl(peer_init->init_hdr.initial_tsn);
2447
2448 asoc->strreset_inseq = asoc->peer.i.initial_tsn;
2449
2450
2451
2452
2453 if (asoc->c.sinit_num_ostreams >
2454 ntohs(peer_init->init_hdr.num_inbound_streams)) {
2455 asoc->c.sinit_num_ostreams =
2456 ntohs(peer_init->init_hdr.num_inbound_streams);
2457 }
2458
2459 if (asoc->c.sinit_max_instreams >
2460 ntohs(peer_init->init_hdr.num_outbound_streams)) {
2461 asoc->c.sinit_max_instreams =
2462 ntohs(peer_init->init_hdr.num_outbound_streams);
2463 }
2464
2465
2466 asoc->c.peer_vtag = asoc->peer.i.init_tag;
2467
2468
2469 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
2470
2471
2472
2473
2474
2475 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
2476 transports) {
2477 transport->ssthresh = asoc->peer.i.a_rwnd;
2478 }
2479
2480
2481 if (!sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_INITIAL,
2482 asoc->peer.i.initial_tsn, gfp))
2483 goto clean_up;
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493 if (sctp_stream_init(&asoc->stream, asoc->c.sinit_num_ostreams,
2494 asoc->c.sinit_max_instreams, gfp))
2495 goto clean_up;
2496
2497
2498 sctp_assoc_update_frag_point(asoc);
2499
2500 if (!asoc->temp && sctp_assoc_set_id(asoc, gfp))
2501 goto clean_up;
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
2514 return 1;
2515
2516 clean_up:
2517
2518 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
2519 transport = list_entry(pos, struct sctp_transport, transports);
2520 if (transport->state != SCTP_ACTIVE)
2521 sctp_assoc_rm_peer(asoc, transport);
2522 }
2523
2524 nomem:
2525 return 0;
2526 }
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540 static int sctp_process_param(struct sctp_association *asoc,
2541 union sctp_params param,
2542 const union sctp_addr *peer_addr,
2543 gfp_t gfp)
2544 {
2545 struct sctp_endpoint *ep = asoc->ep;
2546 union sctp_addr_param *addr_param;
2547 struct net *net = asoc->base.net;
2548 struct sctp_transport *t;
2549 enum sctp_scope scope;
2550 union sctp_addr addr;
2551 struct sctp_af *af;
2552 int retval = 1, i;
2553 u32 stale;
2554 __u16 sat;
2555
2556
2557
2558
2559
2560 switch (param.p->type) {
2561 case SCTP_PARAM_IPV6_ADDRESS:
2562 if (PF_INET6 != asoc->base.sk->sk_family)
2563 break;
2564 goto do_addr_param;
2565
2566 case SCTP_PARAM_IPV4_ADDRESS:
2567
2568 if (ipv6_only_sock(asoc->base.sk))
2569 break;
2570 do_addr_param:
2571 af = sctp_get_af_specific(param_type2af(param.p->type));
2572 if (!af->from_addr_param(&addr, param.addr, htons(asoc->peer.port), 0))
2573 break;
2574 scope = sctp_scope(peer_addr);
2575 if (sctp_in_scope(net, &addr, scope))
2576 if (!sctp_assoc_add_peer(asoc, &addr, gfp, SCTP_UNCONFIRMED))
2577 return 0;
2578 break;
2579
2580 case SCTP_PARAM_COOKIE_PRESERVATIVE:
2581 if (!net->sctp.cookie_preserve_enable)
2582 break;
2583
2584 stale = ntohl(param.life->lifespan_increment);
2585
2586
2587
2588
2589 asoc->cookie_life = ktime_add_ms(asoc->cookie_life, stale);
2590 break;
2591
2592 case SCTP_PARAM_HOST_NAME_ADDRESS:
2593 pr_debug("%s: unimplemented SCTP_HOST_NAME_ADDRESS\n", __func__);
2594 break;
2595
2596 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
2597
2598
2599
2600 asoc->peer.ipv4_address = 0;
2601 asoc->peer.ipv6_address = 0;
2602
2603
2604
2605
2606 if (peer_addr->sa.sa_family == AF_INET6)
2607 asoc->peer.ipv6_address = 1;
2608 else if (peer_addr->sa.sa_family == AF_INET)
2609 asoc->peer.ipv4_address = 1;
2610
2611
2612 sat = ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2613 if (sat)
2614 sat /= sizeof(__u16);
2615
2616 for (i = 0; i < sat; ++i) {
2617 switch (param.sat->types[i]) {
2618 case SCTP_PARAM_IPV4_ADDRESS:
2619 asoc->peer.ipv4_address = 1;
2620 break;
2621
2622 case SCTP_PARAM_IPV6_ADDRESS:
2623 if (PF_INET6 == asoc->base.sk->sk_family)
2624 asoc->peer.ipv6_address = 1;
2625 break;
2626
2627 case SCTP_PARAM_HOST_NAME_ADDRESS:
2628 asoc->peer.hostname_address = 1;
2629 break;
2630
2631 default:
2632 break;
2633 }
2634 }
2635 break;
2636
2637 case SCTP_PARAM_STATE_COOKIE:
2638 asoc->peer.cookie_len =
2639 ntohs(param.p->length) - sizeof(struct sctp_paramhdr);
2640 kfree(asoc->peer.cookie);
2641 asoc->peer.cookie = kmemdup(param.cookie->body, asoc->peer.cookie_len, gfp);
2642 if (!asoc->peer.cookie)
2643 retval = 0;
2644 break;
2645
2646 case SCTP_PARAM_HEARTBEAT_INFO:
2647
2648 break;
2649
2650 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
2651
2652 break;
2653
2654 case SCTP_PARAM_ECN_CAPABLE:
2655 if (asoc->ep->ecn_enable) {
2656 asoc->peer.ecn_capable = 1;
2657 break;
2658 }
2659
2660 goto fall_through;
2661
2662
2663 case SCTP_PARAM_ADAPTATION_LAYER_IND:
2664 asoc->peer.adaptation_ind = ntohl(param.aind->adaptation_ind);
2665 break;
2666
2667 case SCTP_PARAM_SET_PRIMARY:
2668 if (!ep->asconf_enable)
2669 goto fall_through;
2670
2671 addr_param = param.v + sizeof(struct sctp_addip_param);
2672
2673 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
2674 if (!af)
2675 break;
2676
2677 if (!af->from_addr_param(&addr, addr_param,
2678 htons(asoc->peer.port), 0))
2679 break;
2680
2681 if (!af->addr_valid(&addr, NULL, NULL))
2682 break;
2683
2684 t = sctp_assoc_lookup_paddr(asoc, &addr);
2685 if (!t)
2686 break;
2687
2688 sctp_assoc_set_primary(asoc, t);
2689 break;
2690
2691 case SCTP_PARAM_SUPPORTED_EXT:
2692 sctp_process_ext_param(asoc, param);
2693 break;
2694
2695 case SCTP_PARAM_FWD_TSN_SUPPORT:
2696 if (asoc->ep->prsctp_enable) {
2697 asoc->peer.prsctp_capable = 1;
2698 break;
2699 }
2700
2701 goto fall_through;
2702
2703 case SCTP_PARAM_RANDOM:
2704 if (!ep->auth_enable)
2705 goto fall_through;
2706
2707
2708 kfree(asoc->peer.peer_random);
2709 asoc->peer.peer_random = kmemdup(param.p,
2710 ntohs(param.p->length), gfp);
2711 if (!asoc->peer.peer_random) {
2712 retval = 0;
2713 break;
2714 }
2715 break;
2716
2717 case SCTP_PARAM_HMAC_ALGO:
2718 if (!ep->auth_enable)
2719 goto fall_through;
2720
2721
2722 kfree(asoc->peer.peer_hmacs);
2723 asoc->peer.peer_hmacs = kmemdup(param.p,
2724 ntohs(param.p->length), gfp);
2725 if (!asoc->peer.peer_hmacs) {
2726 retval = 0;
2727 break;
2728 }
2729
2730
2731 sctp_auth_asoc_set_default_hmac(asoc, param.hmac_algo);
2732 break;
2733
2734 case SCTP_PARAM_CHUNKS:
2735 if (!ep->auth_enable)
2736 goto fall_through;
2737
2738 kfree(asoc->peer.peer_chunks);
2739 asoc->peer.peer_chunks = kmemdup(param.p,
2740 ntohs(param.p->length), gfp);
2741 if (!asoc->peer.peer_chunks)
2742 retval = 0;
2743 break;
2744 fall_through:
2745 default:
2746
2747
2748
2749
2750
2751 pr_debug("%s: ignoring param:%d for association:%p.\n",
2752 __func__, ntohs(param.p->type), asoc);
2753 break;
2754 }
2755
2756 return retval;
2757 }
2758
2759
2760 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
2761 {
2762
2763
2764
2765 __u32 x;
2766
2767 do {
2768 get_random_bytes(&x, sizeof(__u32));
2769 } while (x == 0);
2770
2771 return x;
2772 }
2773
2774
2775 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
2776 {
2777 __u32 retval;
2778
2779 get_random_bytes(&retval, sizeof(__u32));
2780 return retval;
2781 }
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805 static struct sctp_chunk *sctp_make_asconf(struct sctp_association *asoc,
2806 union sctp_addr *addr,
2807 int vparam_len)
2808 {
2809 struct sctp_addiphdr asconf;
2810 struct sctp_chunk *retval;
2811 int length = sizeof(asconf) + vparam_len;
2812 union sctp_addr_param addrparam;
2813 int addrlen;
2814 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2815
2816 addrlen = af->to_addr_param(addr, &addrparam);
2817 if (!addrlen)
2818 return NULL;
2819 length += addrlen;
2820
2821
2822 retval = sctp_make_control(asoc, SCTP_CID_ASCONF, 0, length,
2823 GFP_ATOMIC);
2824 if (!retval)
2825 return NULL;
2826
2827 asconf.serial = htonl(asoc->addip_serial++);
2828
2829 retval->subh.addip_hdr =
2830 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
2831 retval->param_hdr.v =
2832 sctp_addto_chunk(retval, addrlen, &addrparam);
2833
2834 return retval;
2835 }
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861 struct sctp_chunk *sctp_make_asconf_update_ip(struct sctp_association *asoc,
2862 union sctp_addr *laddr,
2863 struct sockaddr *addrs,
2864 int addrcnt, __be16 flags)
2865 {
2866 union sctp_addr_param addr_param;
2867 struct sctp_addip_param param;
2868 int paramlen = sizeof(param);
2869 struct sctp_chunk *retval;
2870 int addr_param_len = 0;
2871 union sctp_addr *addr;
2872 int totallen = 0, i;
2873 int del_pickup = 0;
2874 struct sctp_af *af;
2875 void *addr_buf;
2876
2877
2878 addr_buf = addrs;
2879 for (i = 0; i < addrcnt; i++) {
2880 addr = addr_buf;
2881 af = sctp_get_af_specific(addr->v4.sin_family);
2882 addr_param_len = af->to_addr_param(addr, &addr_param);
2883
2884 totallen += paramlen;
2885 totallen += addr_param_len;
2886
2887 addr_buf += af->sockaddr_len;
2888 if (asoc->asconf_addr_del_pending && !del_pickup) {
2889
2890 totallen += paramlen;
2891 totallen += addr_param_len;
2892 del_pickup = 1;
2893
2894 pr_debug("%s: picked same-scope del_pending addr, "
2895 "totallen for all addresses is %d\n",
2896 __func__, totallen);
2897 }
2898 }
2899
2900
2901 retval = sctp_make_asconf(asoc, laddr, totallen);
2902 if (!retval)
2903 return NULL;
2904
2905
2906 addr_buf = addrs;
2907 for (i = 0; i < addrcnt; i++) {
2908 addr = addr_buf;
2909 af = sctp_get_af_specific(addr->v4.sin_family);
2910 addr_param_len = af->to_addr_param(addr, &addr_param);
2911 param.param_hdr.type = flags;
2912 param.param_hdr.length = htons(paramlen + addr_param_len);
2913 param.crr_id = htonl(i);
2914
2915 sctp_addto_chunk(retval, paramlen, ¶m);
2916 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2917
2918 addr_buf += af->sockaddr_len;
2919 }
2920 if (flags == SCTP_PARAM_ADD_IP && del_pickup) {
2921 addr = asoc->asconf_addr_del_pending;
2922 af = sctp_get_af_specific(addr->v4.sin_family);
2923 addr_param_len = af->to_addr_param(addr, &addr_param);
2924 param.param_hdr.type = SCTP_PARAM_DEL_IP;
2925 param.param_hdr.length = htons(paramlen + addr_param_len);
2926 param.crr_id = htonl(i);
2927
2928 sctp_addto_chunk(retval, paramlen, ¶m);
2929 sctp_addto_chunk(retval, addr_param_len, &addr_param);
2930 }
2931 return retval;
2932 }
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948 struct sctp_chunk *sctp_make_asconf_set_prim(struct sctp_association *asoc,
2949 union sctp_addr *addr)
2950 {
2951 struct sctp_af *af = sctp_get_af_specific(addr->v4.sin_family);
2952 union sctp_addr_param addrparam;
2953 struct sctp_addip_param param;
2954 struct sctp_chunk *retval;
2955 int len = sizeof(param);
2956 int addrlen;
2957
2958 addrlen = af->to_addr_param(addr, &addrparam);
2959 if (!addrlen)
2960 return NULL;
2961 len += addrlen;
2962
2963
2964 retval = sctp_make_asconf(asoc, addr, len);
2965 if (!retval)
2966 return NULL;
2967
2968 param.param_hdr.type = SCTP_PARAM_SET_PRIMARY;
2969 param.param_hdr.length = htons(len);
2970 param.crr_id = 0;
2971
2972 sctp_addto_chunk(retval, sizeof(param), ¶m);
2973 sctp_addto_chunk(retval, addrlen, &addrparam);
2974
2975 return retval;
2976 }
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997 static struct sctp_chunk *sctp_make_asconf_ack(const struct sctp_association *asoc,
2998 __u32 serial, int vparam_len)
2999 {
3000 struct sctp_addiphdr asconf;
3001 struct sctp_chunk *retval;
3002 int length = sizeof(asconf) + vparam_len;
3003
3004
3005 retval = sctp_make_control(asoc, SCTP_CID_ASCONF_ACK, 0, length,
3006 GFP_ATOMIC);
3007 if (!retval)
3008 return NULL;
3009
3010 asconf.serial = htonl(serial);
3011
3012 retval->subh.addip_hdr =
3013 sctp_addto_chunk(retval, sizeof(asconf), &asconf);
3014
3015 return retval;
3016 }
3017
3018
3019 static void sctp_add_asconf_response(struct sctp_chunk *chunk, __be32 crr_id,
3020 __be16 err_code,
3021 struct sctp_addip_param *asconf_param)
3022 {
3023 struct sctp_addip_param ack_param;
3024 struct sctp_errhdr err_param;
3025 int asconf_param_len = 0;
3026 int err_param_len = 0;
3027 __be16 response_type;
3028
3029 if (SCTP_ERROR_NO_ERROR == err_code) {
3030 response_type = SCTP_PARAM_SUCCESS_REPORT;
3031 } else {
3032 response_type = SCTP_PARAM_ERR_CAUSE;
3033 err_param_len = sizeof(err_param);
3034 if (asconf_param)
3035 asconf_param_len =
3036 ntohs(asconf_param->param_hdr.length);
3037 }
3038
3039
3040 ack_param.param_hdr.type = response_type;
3041 ack_param.param_hdr.length = htons(sizeof(ack_param) +
3042 err_param_len +
3043 asconf_param_len);
3044 ack_param.crr_id = crr_id;
3045 sctp_addto_chunk(chunk, sizeof(ack_param), &ack_param);
3046
3047 if (SCTP_ERROR_NO_ERROR == err_code)
3048 return;
3049
3050
3051 err_param.cause = err_code;
3052 err_param.length = htons(err_param_len + asconf_param_len);
3053 sctp_addto_chunk(chunk, err_param_len, &err_param);
3054
3055
3056 if (asconf_param)
3057 sctp_addto_chunk(chunk, asconf_param_len, asconf_param);
3058 }
3059
3060
3061 static __be16 sctp_process_asconf_param(struct sctp_association *asoc,
3062 struct sctp_chunk *asconf,
3063 struct sctp_addip_param *asconf_param)
3064 {
3065 union sctp_addr_param *addr_param;
3066 struct sctp_transport *peer;
3067 union sctp_addr addr;
3068 struct sctp_af *af;
3069
3070 addr_param = (void *)asconf_param + sizeof(*asconf_param);
3071
3072 if (asconf_param->param_hdr.type != SCTP_PARAM_ADD_IP &&
3073 asconf_param->param_hdr.type != SCTP_PARAM_DEL_IP &&
3074 asconf_param->param_hdr.type != SCTP_PARAM_SET_PRIMARY)
3075 return SCTP_ERROR_UNKNOWN_PARAM;
3076
3077 switch (addr_param->p.type) {
3078 case SCTP_PARAM_IPV6_ADDRESS:
3079 if (!asoc->peer.ipv6_address)
3080 return SCTP_ERROR_DNS_FAILED;
3081 break;
3082 case SCTP_PARAM_IPV4_ADDRESS:
3083 if (!asoc->peer.ipv4_address)
3084 return SCTP_ERROR_DNS_FAILED;
3085 break;
3086 default:
3087 return SCTP_ERROR_DNS_FAILED;
3088 }
3089
3090 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
3091 if (unlikely(!af))
3092 return SCTP_ERROR_DNS_FAILED;
3093
3094 if (!af->from_addr_param(&addr, addr_param, htons(asoc->peer.port), 0))
3095 return SCTP_ERROR_DNS_FAILED;
3096
3097
3098
3099
3100
3101
3102 if (!af->is_any(&addr) && !af->addr_valid(&addr, NULL, asconf->skb))
3103 return SCTP_ERROR_DNS_FAILED;
3104
3105 switch (asconf_param->param_hdr.type) {
3106 case SCTP_PARAM_ADD_IP:
3107
3108
3109
3110
3111 if (af->is_any(&addr))
3112 memcpy(&addr, &asconf->source, sizeof(addr));
3113
3114 if (security_sctp_bind_connect(asoc->ep->base.sk,
3115 SCTP_PARAM_ADD_IP,
3116 (struct sockaddr *)&addr,
3117 af->sockaddr_len))
3118 return SCTP_ERROR_REQ_REFUSED;
3119
3120
3121
3122
3123
3124
3125
3126
3127 peer = sctp_assoc_add_peer(asoc, &addr, GFP_ATOMIC, SCTP_UNCONFIRMED);
3128 if (!peer)
3129 return SCTP_ERROR_RSRC_LOW;
3130
3131
3132 sctp_transport_reset_hb_timer(peer);
3133 asoc->new_transport = peer;
3134 break;
3135 case SCTP_PARAM_DEL_IP:
3136
3137
3138
3139
3140
3141 if (asoc->peer.transport_count == 1)
3142 return SCTP_ERROR_DEL_LAST_IP;
3143
3144
3145
3146
3147
3148
3149
3150
3151 if (sctp_cmp_addr_exact(&asconf->source, &addr))
3152 return SCTP_ERROR_DEL_SRC_IP;
3153
3154
3155
3156
3157
3158
3159 if (af->is_any(&addr)) {
3160 sctp_assoc_set_primary(asoc, asconf->transport);
3161 sctp_assoc_del_nonprimary_peers(asoc,
3162 asconf->transport);
3163 return SCTP_ERROR_NO_ERROR;
3164 }
3165
3166
3167
3168
3169
3170
3171 peer = sctp_assoc_lookup_paddr(asoc, &addr);
3172 if (!peer)
3173 return SCTP_ERROR_DNS_FAILED;
3174
3175 sctp_assoc_rm_peer(asoc, peer);
3176 break;
3177 case SCTP_PARAM_SET_PRIMARY:
3178
3179
3180
3181
3182
3183 if (af->is_any(&addr))
3184 memcpy(&addr, sctp_source(asconf), sizeof(addr));
3185
3186 if (security_sctp_bind_connect(asoc->ep->base.sk,
3187 SCTP_PARAM_SET_PRIMARY,
3188 (struct sockaddr *)&addr,
3189 af->sockaddr_len))
3190 return SCTP_ERROR_REQ_REFUSED;
3191
3192 peer = sctp_assoc_lookup_paddr(asoc, &addr);
3193 if (!peer)
3194 return SCTP_ERROR_DNS_FAILED;
3195
3196 sctp_assoc_set_primary(asoc, peer);
3197 break;
3198 }
3199
3200 return SCTP_ERROR_NO_ERROR;
3201 }
3202
3203
3204 bool sctp_verify_asconf(const struct sctp_association *asoc,
3205 struct sctp_chunk *chunk, bool addr_param_needed,
3206 struct sctp_paramhdr **errp)
3207 {
3208 struct sctp_addip_chunk *addip;
3209 bool addr_param_seen = false;
3210 union sctp_params param;
3211
3212 addip = (struct sctp_addip_chunk *)chunk->chunk_hdr;
3213 sctp_walk_params(param, addip, addip_hdr.params) {
3214 size_t length = ntohs(param.p->length);
3215
3216 *errp = param.p;
3217 switch (param.p->type) {
3218 case SCTP_PARAM_ERR_CAUSE:
3219 break;
3220 case SCTP_PARAM_IPV4_ADDRESS:
3221 if (length != sizeof(struct sctp_ipv4addr_param))
3222 return false;
3223
3224
3225
3226 if (param.v != addip->addip_hdr.params)
3227 return false;
3228 addr_param_seen = true;
3229 break;
3230 case SCTP_PARAM_IPV6_ADDRESS:
3231 if (length != sizeof(struct sctp_ipv6addr_param))
3232 return false;
3233 if (param.v != addip->addip_hdr.params)
3234 return false;
3235 addr_param_seen = true;
3236 break;
3237 case SCTP_PARAM_ADD_IP:
3238 case SCTP_PARAM_DEL_IP:
3239 case SCTP_PARAM_SET_PRIMARY:
3240
3241 if (addr_param_needed && !addr_param_seen)
3242 return false;
3243 length = ntohs(param.addip->param_hdr.length);
3244 if (length < sizeof(struct sctp_addip_param) +
3245 sizeof(**errp))
3246 return false;
3247 break;
3248 case SCTP_PARAM_SUCCESS_REPORT:
3249 case SCTP_PARAM_ADAPTATION_LAYER_IND:
3250 if (length != sizeof(struct sctp_addip_param))
3251 return false;
3252 break;
3253 default:
3254
3255 return false;
3256 }
3257 }
3258
3259
3260 if (addr_param_needed && !addr_param_seen)
3261 return false;
3262 if (!addr_param_needed && addr_param_seen)
3263 return false;
3264 if (param.v != chunk->chunk_end)
3265 return false;
3266
3267 return true;
3268 }
3269
3270
3271
3272
3273 struct sctp_chunk *sctp_process_asconf(struct sctp_association *asoc,
3274 struct sctp_chunk *asconf)
3275 {
3276 union sctp_addr_param *addr_param;
3277 struct sctp_addip_chunk *addip;
3278 struct sctp_chunk *asconf_ack;
3279 bool all_param_pass = true;
3280 struct sctp_addiphdr *hdr;
3281 int length = 0, chunk_len;
3282 union sctp_params param;
3283 __be16 err_code;
3284 __u32 serial;
3285
3286 addip = (struct sctp_addip_chunk *)asconf->chunk_hdr;
3287 chunk_len = ntohs(asconf->chunk_hdr->length) -
3288 sizeof(struct sctp_chunkhdr);
3289 hdr = (struct sctp_addiphdr *)asconf->skb->data;
3290 serial = ntohl(hdr->serial);
3291
3292
3293 length = sizeof(*hdr);
3294 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3295 chunk_len -= length;
3296
3297
3298
3299
3300 length = ntohs(addr_param->p.length);
3301 chunk_len -= length;
3302
3303
3304
3305
3306
3307
3308 asconf_ack = sctp_make_asconf_ack(asoc, serial, chunk_len * 4);
3309 if (!asconf_ack)
3310 goto done;
3311
3312
3313 sctp_walk_params(param, addip, addip_hdr.params) {
3314
3315 if (param.p->type == SCTP_PARAM_IPV4_ADDRESS ||
3316 param.p->type == SCTP_PARAM_IPV6_ADDRESS)
3317 continue;
3318
3319 err_code = sctp_process_asconf_param(asoc, asconf,
3320 param.addip);
3321
3322
3323
3324
3325
3326
3327
3328 if (err_code != SCTP_ERROR_NO_ERROR)
3329 all_param_pass = false;
3330 if (!all_param_pass)
3331 sctp_add_asconf_response(asconf_ack, param.addip->crr_id,
3332 err_code, param.addip);
3333
3334
3335
3336
3337
3338
3339 if (err_code == SCTP_ERROR_RSRC_LOW)
3340 goto done;
3341 }
3342 done:
3343 asoc->peer.addip_serial++;
3344
3345
3346
3347
3348 if (asconf_ack) {
3349 sctp_chunk_hold(asconf_ack);
3350 list_add_tail(&asconf_ack->transmitted_list,
3351 &asoc->asconf_ack_list);
3352 }
3353
3354 return asconf_ack;
3355 }
3356
3357
3358 static void sctp_asconf_param_success(struct sctp_association *asoc,
3359 struct sctp_addip_param *asconf_param)
3360 {
3361 struct sctp_bind_addr *bp = &asoc->base.bind_addr;
3362 union sctp_addr_param *addr_param;
3363 struct sctp_sockaddr_entry *saddr;
3364 struct sctp_transport *transport;
3365 union sctp_addr addr;
3366 struct sctp_af *af;
3367
3368 addr_param = (void *)asconf_param + sizeof(*asconf_param);
3369
3370
3371 af = sctp_get_af_specific(param_type2af(addr_param->p.type));
3372 if (!af->from_addr_param(&addr, addr_param, htons(bp->port), 0))
3373 return;
3374
3375 switch (asconf_param->param_hdr.type) {
3376 case SCTP_PARAM_ADD_IP:
3377
3378
3379
3380 local_bh_disable();
3381 list_for_each_entry(saddr, &bp->address_list, list) {
3382 if (sctp_cmp_addr_exact(&saddr->a, &addr))
3383 saddr->state = SCTP_ADDR_SRC;
3384 }
3385 local_bh_enable();
3386 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3387 transports) {
3388 sctp_transport_dst_release(transport);
3389 }
3390 break;
3391 case SCTP_PARAM_DEL_IP:
3392 local_bh_disable();
3393 sctp_del_bind_addr(bp, &addr);
3394 if (asoc->asconf_addr_del_pending != NULL &&
3395 sctp_cmp_addr_exact(asoc->asconf_addr_del_pending, &addr)) {
3396 kfree(asoc->asconf_addr_del_pending);
3397 asoc->asconf_addr_del_pending = NULL;
3398 }
3399 local_bh_enable();
3400 list_for_each_entry(transport, &asoc->peer.transport_addr_list,
3401 transports) {
3402 sctp_transport_dst_release(transport);
3403 }
3404 break;
3405 default:
3406 break;
3407 }
3408 }
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419 static __be16 sctp_get_asconf_response(struct sctp_chunk *asconf_ack,
3420 struct sctp_addip_param *asconf_param,
3421 int no_err)
3422 {
3423 struct sctp_addip_param *asconf_ack_param;
3424 struct sctp_errhdr *err_param;
3425 int asconf_ack_len;
3426 __be16 err_code;
3427 int length;
3428
3429 if (no_err)
3430 err_code = SCTP_ERROR_NO_ERROR;
3431 else
3432 err_code = SCTP_ERROR_REQ_REFUSED;
3433
3434 asconf_ack_len = ntohs(asconf_ack->chunk_hdr->length) -
3435 sizeof(struct sctp_chunkhdr);
3436
3437
3438
3439
3440 length = sizeof(struct sctp_addiphdr);
3441 asconf_ack_param = (struct sctp_addip_param *)(asconf_ack->skb->data +
3442 length);
3443 asconf_ack_len -= length;
3444
3445 while (asconf_ack_len > 0) {
3446 if (asconf_ack_param->crr_id == asconf_param->crr_id) {
3447 switch (asconf_ack_param->param_hdr.type) {
3448 case SCTP_PARAM_SUCCESS_REPORT:
3449 return SCTP_ERROR_NO_ERROR;
3450 case SCTP_PARAM_ERR_CAUSE:
3451 length = sizeof(*asconf_ack_param);
3452 err_param = (void *)asconf_ack_param + length;
3453 asconf_ack_len -= length;
3454 if (asconf_ack_len > 0)
3455 return err_param->cause;
3456 else
3457 return SCTP_ERROR_INV_PARAM;
3458 break;
3459 default:
3460 return SCTP_ERROR_INV_PARAM;
3461 }
3462 }
3463
3464 length = ntohs(asconf_ack_param->param_hdr.length);
3465 asconf_ack_param = (void *)asconf_ack_param + length;
3466 asconf_ack_len -= length;
3467 }
3468
3469 return err_code;
3470 }
3471
3472
3473 int sctp_process_asconf_ack(struct sctp_association *asoc,
3474 struct sctp_chunk *asconf_ack)
3475 {
3476 struct sctp_chunk *asconf = asoc->addip_last_asconf;
3477 struct sctp_addip_param *asconf_param;
3478 __be16 err_code = SCTP_ERROR_NO_ERROR;
3479 union sctp_addr_param *addr_param;
3480 int asconf_len = asconf->skb->len;
3481 int all_param_pass = 0;
3482 int length = 0;
3483 int no_err = 1;
3484 int retval = 0;
3485
3486
3487
3488
3489 length = sizeof(struct sctp_addip_chunk);
3490 addr_param = (union sctp_addr_param *)(asconf->skb->data + length);
3491 asconf_len -= length;
3492
3493
3494
3495
3496 length = ntohs(addr_param->p.length);
3497 asconf_param = (void *)addr_param + length;
3498 asconf_len -= length;
3499
3500
3501
3502
3503
3504
3505 if (asconf_ack->skb->len == sizeof(struct sctp_addiphdr))
3506 all_param_pass = 1;
3507
3508
3509 while (asconf_len > 0) {
3510 if (all_param_pass)
3511 err_code = SCTP_ERROR_NO_ERROR;
3512 else {
3513 err_code = sctp_get_asconf_response(asconf_ack,
3514 asconf_param,
3515 no_err);
3516 if (no_err && (SCTP_ERROR_NO_ERROR != err_code))
3517 no_err = 0;
3518 }
3519
3520 switch (err_code) {
3521 case SCTP_ERROR_NO_ERROR:
3522 sctp_asconf_param_success(asoc, asconf_param);
3523 break;
3524
3525 case SCTP_ERROR_RSRC_LOW:
3526 retval = 1;
3527 break;
3528
3529 case SCTP_ERROR_UNKNOWN_PARAM:
3530
3531
3532
3533 asoc->peer.addip_disabled_mask |=
3534 asconf_param->param_hdr.type;
3535 break;
3536
3537 case SCTP_ERROR_REQ_REFUSED:
3538 case SCTP_ERROR_DEL_LAST_IP:
3539 case SCTP_ERROR_DEL_SRC_IP:
3540 default:
3541 break;
3542 }
3543
3544
3545
3546
3547 length = ntohs(asconf_param->param_hdr.length);
3548 asconf_param = (void *)asconf_param + length;
3549 asconf_len -= length;
3550 }
3551
3552 if (no_err && asoc->src_out_of_asoc_ok) {
3553 asoc->src_out_of_asoc_ok = 0;
3554 sctp_transport_immediate_rtx(asoc->peer.primary_path);
3555 }
3556
3557
3558 list_del_init(&asconf->transmitted_list);
3559 sctp_chunk_free(asconf);
3560 asoc->addip_last_asconf = NULL;
3561
3562 return retval;
3563 }
3564
3565
3566 struct sctp_chunk *sctp_make_fwdtsn(const struct sctp_association *asoc,
3567 __u32 new_cum_tsn, size_t nstreams,
3568 struct sctp_fwdtsn_skip *skiplist)
3569 {
3570 struct sctp_chunk *retval = NULL;
3571 struct sctp_fwdtsn_hdr ftsn_hdr;
3572 struct sctp_fwdtsn_skip skip;
3573 size_t hint;
3574 int i;
3575
3576 hint = (nstreams + 1) * sizeof(__u32);
3577
3578 retval = sctp_make_control(asoc, SCTP_CID_FWD_TSN, 0, hint, GFP_ATOMIC);
3579
3580 if (!retval)
3581 return NULL;
3582
3583 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3584 retval->subh.fwdtsn_hdr =
3585 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3586
3587 for (i = 0; i < nstreams; i++) {
3588 skip.stream = skiplist[i].stream;
3589 skip.ssn = skiplist[i].ssn;
3590 sctp_addto_chunk(retval, sizeof(skip), &skip);
3591 }
3592
3593 return retval;
3594 }
3595
3596 struct sctp_chunk *sctp_make_ifwdtsn(const struct sctp_association *asoc,
3597 __u32 new_cum_tsn, size_t nstreams,
3598 struct sctp_ifwdtsn_skip *skiplist)
3599 {
3600 struct sctp_chunk *retval = NULL;
3601 struct sctp_ifwdtsn_hdr ftsn_hdr;
3602 size_t hint;
3603
3604 hint = (nstreams + 1) * sizeof(__u32);
3605
3606 retval = sctp_make_control(asoc, SCTP_CID_I_FWD_TSN, 0, hint,
3607 GFP_ATOMIC);
3608 if (!retval)
3609 return NULL;
3610
3611 ftsn_hdr.new_cum_tsn = htonl(new_cum_tsn);
3612 retval->subh.ifwdtsn_hdr =
3613 sctp_addto_chunk(retval, sizeof(ftsn_hdr), &ftsn_hdr);
3614
3615 sctp_addto_chunk(retval, nstreams * sizeof(skiplist[0]), skiplist);
3616
3617 return retval;
3618 }
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635 static struct sctp_chunk *sctp_make_reconf(const struct sctp_association *asoc,
3636 int length)
3637 {
3638 struct sctp_reconf_chunk *reconf;
3639 struct sctp_chunk *retval;
3640
3641 retval = sctp_make_control(asoc, SCTP_CID_RECONF, 0, length,
3642 GFP_ATOMIC);
3643 if (!retval)
3644 return NULL;
3645
3646 reconf = (struct sctp_reconf_chunk *)retval->chunk_hdr;
3647 retval->param_hdr.v = reconf->params;
3648
3649 return retval;
3650 }
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686 struct sctp_chunk *sctp_make_strreset_req(
3687 const struct sctp_association *asoc,
3688 __u16 stream_num, __be16 *stream_list,
3689 bool out, bool in)
3690 {
3691 __u16 stream_len = stream_num * sizeof(__u16);
3692 struct sctp_strreset_outreq outreq;
3693 struct sctp_strreset_inreq inreq;
3694 struct sctp_chunk *retval;
3695 __u16 outlen, inlen;
3696
3697 outlen = (sizeof(outreq) + stream_len) * out;
3698 inlen = (sizeof(inreq) + stream_len) * in;
3699
3700 retval = sctp_make_reconf(asoc, SCTP_PAD4(outlen) + SCTP_PAD4(inlen));
3701 if (!retval)
3702 return NULL;
3703
3704 if (outlen) {
3705 outreq.param_hdr.type = SCTP_PARAM_RESET_OUT_REQUEST;
3706 outreq.param_hdr.length = htons(outlen);
3707 outreq.request_seq = htonl(asoc->strreset_outseq);
3708 outreq.response_seq = htonl(asoc->strreset_inseq - 1);
3709 outreq.send_reset_at_tsn = htonl(asoc->next_tsn - 1);
3710
3711 sctp_addto_chunk(retval, sizeof(outreq), &outreq);
3712
3713 if (stream_len)
3714 sctp_addto_chunk(retval, stream_len, stream_list);
3715 }
3716
3717 if (inlen) {
3718 inreq.param_hdr.type = SCTP_PARAM_RESET_IN_REQUEST;
3719 inreq.param_hdr.length = htons(inlen);
3720 inreq.request_seq = htonl(asoc->strreset_outseq + out);
3721
3722 sctp_addto_chunk(retval, sizeof(inreq), &inreq);
3723
3724 if (stream_len)
3725 sctp_addto_chunk(retval, stream_len, stream_list);
3726 }
3727
3728 return retval;
3729 }
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740 struct sctp_chunk *sctp_make_strreset_tsnreq(
3741 const struct sctp_association *asoc)
3742 {
3743 struct sctp_strreset_tsnreq tsnreq;
3744 __u16 length = sizeof(tsnreq);
3745 struct sctp_chunk *retval;
3746
3747 retval = sctp_make_reconf(asoc, length);
3748 if (!retval)
3749 return NULL;
3750
3751 tsnreq.param_hdr.type = SCTP_PARAM_RESET_TSN_REQUEST;
3752 tsnreq.param_hdr.length = htons(length);
3753 tsnreq.request_seq = htonl(asoc->strreset_outseq);
3754
3755 sctp_addto_chunk(retval, sizeof(tsnreq), &tsnreq);
3756
3757 return retval;
3758 }
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771 struct sctp_chunk *sctp_make_strreset_addstrm(
3772 const struct sctp_association *asoc,
3773 __u16 out, __u16 in)
3774 {
3775 struct sctp_strreset_addstrm addstrm;
3776 __u16 size = sizeof(addstrm);
3777 struct sctp_chunk *retval;
3778
3779 retval = sctp_make_reconf(asoc, (!!out + !!in) * size);
3780 if (!retval)
3781 return NULL;
3782
3783 if (out) {
3784 addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_OUT_STREAMS;
3785 addstrm.param_hdr.length = htons(size);
3786 addstrm.number_of_streams = htons(out);
3787 addstrm.request_seq = htonl(asoc->strreset_outseq);
3788 addstrm.reserved = 0;
3789
3790 sctp_addto_chunk(retval, size, &addstrm);
3791 }
3792
3793 if (in) {
3794 addstrm.param_hdr.type = SCTP_PARAM_RESET_ADD_IN_STREAMS;
3795 addstrm.param_hdr.length = htons(size);
3796 addstrm.number_of_streams = htons(in);
3797 addstrm.request_seq = htonl(asoc->strreset_outseq + !!out);
3798 addstrm.reserved = 0;
3799
3800 sctp_addto_chunk(retval, size, &addstrm);
3801 }
3802
3803 return retval;
3804 }
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817 struct sctp_chunk *sctp_make_strreset_resp(const struct sctp_association *asoc,
3818 __u32 result, __u32 sn)
3819 {
3820 struct sctp_strreset_resp resp;
3821 __u16 length = sizeof(resp);
3822 struct sctp_chunk *retval;
3823
3824 retval = sctp_make_reconf(asoc, length);
3825 if (!retval)
3826 return NULL;
3827
3828 resp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
3829 resp.param_hdr.length = htons(length);
3830 resp.response_seq = htonl(sn);
3831 resp.result = htonl(result);
3832
3833 sctp_addto_chunk(retval, sizeof(resp), &resp);
3834
3835 return retval;
3836 }
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853 struct sctp_chunk *sctp_make_strreset_tsnresp(struct sctp_association *asoc,
3854 __u32 result, __u32 sn,
3855 __u32 sender_tsn,
3856 __u32 receiver_tsn)
3857 {
3858 struct sctp_strreset_resptsn tsnresp;
3859 __u16 length = sizeof(tsnresp);
3860 struct sctp_chunk *retval;
3861
3862 retval = sctp_make_reconf(asoc, length);
3863 if (!retval)
3864 return NULL;
3865
3866 tsnresp.param_hdr.type = SCTP_PARAM_RESET_RESPONSE;
3867 tsnresp.param_hdr.length = htons(length);
3868
3869 tsnresp.response_seq = htonl(sn);
3870 tsnresp.result = htonl(result);
3871 tsnresp.senders_next_tsn = htonl(sender_tsn);
3872 tsnresp.receivers_next_tsn = htonl(receiver_tsn);
3873
3874 sctp_addto_chunk(retval, sizeof(tsnresp), &tsnresp);
3875
3876 return retval;
3877 }
3878
3879 bool sctp_verify_reconf(const struct sctp_association *asoc,
3880 struct sctp_chunk *chunk,
3881 struct sctp_paramhdr **errp)
3882 {
3883 struct sctp_reconf_chunk *hdr;
3884 union sctp_params param;
3885 __be16 last = 0;
3886 __u16 cnt = 0;
3887
3888 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3889 sctp_walk_params(param, hdr, params) {
3890 __u16 length = ntohs(param.p->length);
3891
3892 *errp = param.p;
3893 if (cnt++ > 2)
3894 return false;
3895 switch (param.p->type) {
3896 case SCTP_PARAM_RESET_OUT_REQUEST:
3897 if (length < sizeof(struct sctp_strreset_outreq) ||
3898 (last && last != SCTP_PARAM_RESET_RESPONSE &&
3899 last != SCTP_PARAM_RESET_IN_REQUEST))
3900 return false;
3901 break;
3902 case SCTP_PARAM_RESET_IN_REQUEST:
3903 if (length < sizeof(struct sctp_strreset_inreq) ||
3904 (last && last != SCTP_PARAM_RESET_OUT_REQUEST))
3905 return false;
3906 break;
3907 case SCTP_PARAM_RESET_RESPONSE:
3908 if ((length != sizeof(struct sctp_strreset_resp) &&
3909 length != sizeof(struct sctp_strreset_resptsn)) ||
3910 (last && last != SCTP_PARAM_RESET_RESPONSE &&
3911 last != SCTP_PARAM_RESET_OUT_REQUEST))
3912 return false;
3913 break;
3914 case SCTP_PARAM_RESET_TSN_REQUEST:
3915 if (length !=
3916 sizeof(struct sctp_strreset_tsnreq) || last)
3917 return false;
3918 break;
3919 case SCTP_PARAM_RESET_ADD_IN_STREAMS:
3920 if (length != sizeof(struct sctp_strreset_addstrm) ||
3921 (last && last != SCTP_PARAM_RESET_ADD_OUT_STREAMS))
3922 return false;
3923 break;
3924 case SCTP_PARAM_RESET_ADD_OUT_STREAMS:
3925 if (length != sizeof(struct sctp_strreset_addstrm) ||
3926 (last && last != SCTP_PARAM_RESET_ADD_IN_STREAMS))
3927 return false;
3928 break;
3929 default:
3930 return false;
3931 }
3932
3933 last = param.p->type;
3934 }
3935
3936 return true;
3937 }