Skip to content

Commit 07d9396

Browse files
Vlad Yasevichdavem330
authored andcommitted
[SCTP]: Set assoc_id correctly during INIT collision.
During the INIT/COOKIE-ACK collision cases, it's possible to get into a situation where the association id is not yet set at the time of the user event generation. As a result, user events have an association id set to 0 which will confuse applications. This happens if we hit case B of duplicate cookie processing. In the particular example found and provided by Oscar Isaula <Oscar.Isaula@motorola.com>, flow looks like this: A B ---- INIT-------> (lost) <---------INIT------ ---- INIT-ACK---> <------ Cookie ECHO When the Cookie Echo is received, we end up trying to update the association that was created on A as a result of the (lost) INIT, but that association doesn't have the ID set yet. Signed-off-by: Vlad Yasevich <vladislav.yasevich@hp.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 827bf12 commit 07d9396

6 files changed

Lines changed: 80 additions & 31 deletions

File tree

‎include/net/sctp/command.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ typedef enum {
100100
SCTP_CMD_T3_RTX_TIMERS_STOP, /* Stops T3-rtx pending timers */
101101
SCTP_CMD_FORCE_PRIM_RETRAN, /* Forces retrans. over primary path. */
102102
SCTP_CMD_SET_SK_ERR, /* Set sk_err */
103+
SCTP_CMD_ASSOC_CHANGE, /* generate and send assoc_change event */
104+
SCTP_CMD_ADAPTATION_IND, /* generate and send adaptation event */
103105
SCTP_CMD_LAST
104106
} sctp_verb_t;
105107

‎include/net/sctp/structs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,7 @@ int sctp_assoc_set_bind_addr_from_ep(struct sctp_association *,
18571857
int sctp_assoc_set_bind_addr_from_cookie(struct sctp_association *,
18581858
struct sctp_cookie*,
18591859
gfp_t gfp);
1860+
int sctp_assoc_set_id(struct sctp_association *, gfp_t);
18601861

18611862
int sctp_cmp_addr_exact(const union sctp_addr *ss1,
18621863
const union sctp_addr *ss2);

‎net/sctp/associola.c‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,13 @@ void sctp_assoc_update(struct sctp_association *asoc,
11031103
asoc->ssnmap = new->ssnmap;
11041104
new->ssnmap = NULL;
11051105
}
1106+
1107+
if (!asoc->assoc_id) {
1108+
/* get a new association id since we don't have one
1109+
* yet.
1110+
*/
1111+
sctp_assoc_set_id(asoc, GFP_ATOMIC);
1112+
}
11061113
}
11071114
}
11081115

@@ -1375,3 +1382,25 @@ int sctp_assoc_lookup_laddr(struct sctp_association *asoc,
13751382
sctp_read_unlock(&asoc->base.addr_lock);
13761383
return found;
13771384
}
1385+
1386+
/* Set an association id for a given association */
1387+
int sctp_assoc_set_id(struct sctp_association *asoc, gfp_t gfp)
1388+
{
1389+
int assoc_id;
1390+
int error = 0;
1391+
retry:
1392+
if (unlikely(!idr_pre_get(&sctp_assocs_id, gfp)))
1393+
return -ENOMEM;
1394+
1395+
spin_lock_bh(&sctp_assocs_id_lock);
1396+
error = idr_get_new_above(&sctp_assocs_id, (void *)asoc,
1397+
1, &assoc_id);
1398+
spin_unlock_bh(&sctp_assocs_id_lock);
1399+
if (error == -EAGAIN)
1400+
goto retry;
1401+
else if (error)
1402+
return error;
1403+
1404+
asoc->assoc_id = (sctp_assoc_t) assoc_id;
1405+
return error;
1406+
}

‎net/sctp/sm_make_chunk.c‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,27 +1939,16 @@ int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
19391939
* association.
19401940
*/
19411941
if (!asoc->temp) {
1942-
int assoc_id;
19431942
int error;
19441943

19451944
asoc->ssnmap = sctp_ssnmap_new(asoc->c.sinit_max_instreams,
19461945
asoc->c.sinit_num_ostreams, gfp);
19471946
if (!asoc->ssnmap)
19481947
goto clean_up;
19491948

1950-
retry:
1951-
if (unlikely(!idr_pre_get(&sctp_assocs_id, gfp)))
1949+
error = sctp_assoc_set_id(asoc, gfp);
1950+
if (error)
19521951
goto clean_up;
1953-
spin_lock_bh(&sctp_assocs_id_lock);
1954-
error = idr_get_new_above(&sctp_assocs_id, (void *)asoc, 1,
1955-
&assoc_id);
1956-
spin_unlock_bh(&sctp_assocs_id_lock);
1957-
if (error == -EAGAIN)
1958-
goto retry;
1959-
else if (error)
1960-
goto clean_up;
1961-
1962-
asoc->assoc_id = (sctp_assoc_t) assoc_id;
19631952
}
19641953

19651954
/* ADDIP Section 4.1 ASCONF Chunk Procedures

‎net/sctp/sm_sideeffect.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,33 @@ static void sctp_cmd_set_sk_err(struct sctp_association *asoc, int error)
862862
sk->sk_err = error;
863863
}
864864

865+
/* Helper function to generate an association change event */
866+
static void sctp_cmd_assoc_change(sctp_cmd_seq_t *commands,
867+
struct sctp_association *asoc,
868+
u8 state)
869+
{
870+
struct sctp_ulpevent *ev;
871+
872+
ev = sctp_ulpevent_make_assoc_change(asoc, 0, state, 0,
873+
asoc->c.sinit_num_ostreams,
874+
asoc->c.sinit_max_instreams,
875+
NULL, GFP_ATOMIC);
876+
if (ev)
877+
sctp_ulpq_tail_event(&asoc->ulpq, ev);
878+
}
879+
880+
/* Helper function to generate an adaptation indication event */
881+
static void sctp_cmd_adaptation_ind(sctp_cmd_seq_t *commands,
882+
struct sctp_association *asoc)
883+
{
884+
struct sctp_ulpevent *ev;
885+
886+
ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
887+
888+
if (ev)
889+
sctp_ulpq_tail_event(&asoc->ulpq, ev);
890+
}
891+
865892
/* These three macros allow us to pull the debugging code out of the
866893
* main flow of sctp_do_sm() to keep attention focused on the real
867894
* functionality there.
@@ -1485,6 +1512,14 @@ static int sctp_cmd_interpreter(sctp_event_t event_type,
14851512
case SCTP_CMD_SET_SK_ERR:
14861513
sctp_cmd_set_sk_err(asoc, cmd->obj.error);
14871514
break;
1515+
case SCTP_CMD_ASSOC_CHANGE:
1516+
sctp_cmd_assoc_change(commands, asoc,
1517+
cmd->obj.u8);
1518+
break;
1519+
case SCTP_CMD_ADAPTATION_IND:
1520+
sctp_cmd_adaptation_ind(commands, asoc);
1521+
break;
1522+
14881523
default:
14891524
printk(KERN_WARNING "Impossible command: %u, %p\n",
14901525
cmd->verb, cmd->obj.ptr);

‎net/sctp/sm_statefuns.c‎

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,6 @@ static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
16561656
struct sctp_association *new_asoc)
16571657
{
16581658
sctp_init_chunk_t *peer_init;
1659-
struct sctp_ulpevent *ev;
16601659
struct sctp_chunk *repl;
16611660

16621661
/* new_asoc is a brand-new association, so these are not yet
@@ -1687,34 +1686,28 @@ static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
16871686
* D) IMPLEMENTATION NOTE: An implementation may choose to
16881687
* send the Communication Up notification to the SCTP user
16891688
* upon reception of a valid COOKIE ECHO chunk.
1689+
*
1690+
* Sadly, this needs to be implemented as a side-effect, because
1691+
* we are not guaranteed to have set the association id of the real
1692+
* association and so these notifications need to be delayed until
1693+
* the association id is allocated.
16901694
*/
1691-
ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP, 0,
1692-
new_asoc->c.sinit_num_ostreams,
1693-
new_asoc->c.sinit_max_instreams,
1694-
NULL, GFP_ATOMIC);
1695-
if (!ev)
1696-
goto nomem_ev;
16971695

1698-
sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1696+
sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
16991697

17001698
/* Sockets API Draft Section 5.3.1.6
17011699
* When a peer sends a Adaptation Layer Indication parameter , SCTP
17021700
* delivers this notification to inform the application that of the
17031701
* peers requested adaptation layer.
1702+
*
1703+
* This also needs to be done as a side effect for the same reason as
1704+
* above.
17041705
*/
1705-
if (asoc->peer.adaptation_ind) {
1706-
ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
1707-
if (!ev)
1708-
goto nomem_ev;
1709-
1710-
sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1711-
SCTP_ULPEVENT(ev));
1712-
}
1706+
if (asoc->peer.adaptation_ind)
1707+
sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
17131708

17141709
return SCTP_DISPOSITION_CONSUME;
17151710

1716-
nomem_ev:
1717-
sctp_chunk_free(repl);
17181711
nomem:
17191712
return SCTP_DISPOSITION_NOMEM;
17201713
}

0 commit comments

Comments
 (0)