Skip to content

Commit 827bf12

Browse files
Sridhar Samudraladavem330
authored andcommitted
[SCTP]: Re-order SCTP initializations to avoid race with sctp_rcv()
Signed-off-by: Sridhar Samudrala <sri@us.ibm.com> Signed-off-by: Vlad Yasevich <vladislav.yasevich@hp.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ce5325c commit 827bf12

3 files changed

Lines changed: 79 additions & 53 deletions

File tree

‎include/net/sctp/sctp.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,15 @@ static inline int sctp_sysctl_jiffies_ms(ctl_table *table, int __user *name, int
378378

379379
int sctp_v6_init(void);
380380
void sctp_v6_exit(void);
381+
int sctp_v6_add_protocol(void);
382+
void sctp_v6_del_protocol(void);
381383

382384
#else /* #ifdef defined(CONFIG_IPV6) */
383385

384386
static inline int sctp_v6_init(void) { return 0; }
385387
static inline void sctp_v6_exit(void) { return; }
388+
static inline int sctp_v6_add_protocol(void) { return 0; }
389+
static inline void sctp_v6_del_protocol(void) { return; }
386390

387391
#endif /* #if defined(CONFIG_IPV6) */
388392

‎net/sctp/ipv6.c‎

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -992,45 +992,52 @@ static struct sctp_pf sctp_pf_inet6_specific = {
992992
.af = &sctp_ipv6_specific,
993993
};
994994

995-
/* Initialize IPv6 support and register with inet6 stack. */
995+
/* Initialize IPv6 support and register with socket layer. */
996996
int sctp_v6_init(void)
997997
{
998-
int rc = proto_register(&sctpv6_prot, 1);
998+
int rc;
999999

1000+
/* Register the SCTP specific PF_INET6 functions. */
1001+
sctp_register_pf(&sctp_pf_inet6_specific, PF_INET6);
1002+
1003+
/* Register the SCTP specific AF_INET6 functions. */
1004+
sctp_register_af(&sctp_ipv6_specific);
1005+
1006+
rc = proto_register(&sctpv6_prot, 1);
10001007
if (rc)
1001-
goto out;
1002-
/* Register inet6 protocol. */
1003-
rc = -EAGAIN;
1004-
if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
1005-
goto out_unregister_sctp_proto;
1008+
return rc;
10061009

10071010
/* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
10081011
inet6_register_protosw(&sctpv6_seqpacket_protosw);
10091012
inet6_register_protosw(&sctpv6_stream_protosw);
10101013

1011-
/* Register the SCTP specific PF_INET6 functions. */
1012-
sctp_register_pf(&sctp_pf_inet6_specific, PF_INET6);
1013-
1014-
/* Register the SCTP specific AF_INET6 functions. */
1015-
sctp_register_af(&sctp_ipv6_specific);
1014+
return 0;
1015+
}
10161016

1017+
/* Register with inet6 layer. */
1018+
int sctp_v6_add_protocol(void)
1019+
{
10171020
/* Register notifier for inet6 address additions/deletions. */
10181021
register_inet6addr_notifier(&sctp_inet6addr_notifier);
1019-
rc = 0;
1020-
out:
1021-
return rc;
1022-
out_unregister_sctp_proto:
1023-
proto_unregister(&sctpv6_prot);
1024-
goto out;
1022+
1023+
if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
1024+
return -EAGAIN;
1025+
1026+
return 0;
10251027
}
10261028

10271029
/* IPv6 specific exit support. */
10281030
void sctp_v6_exit(void)
10291031
{
1030-
list_del(&sctp_ipv6_specific.list);
1031-
inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
10321032
inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
10331033
inet6_unregister_protosw(&sctpv6_stream_protosw);
1034-
unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
10351034
proto_unregister(&sctpv6_prot);
1035+
list_del(&sctp_ipv6_specific.list);
1036+
}
1037+
1038+
/* Unregister with inet6 layer. */
1039+
void sctp_v6_del_protocol(void)
1040+
{
1041+
inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
1042+
unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
10361043
}

‎net/sctp/protocol.c‎

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -975,28 +975,14 @@ SCTP_STATIC __init int sctp_init(void)
975975
if (!sctp_sanity_check())
976976
goto out;
977977

978-
status = proto_register(&sctp_prot, 1);
979-
if (status)
980-
goto out;
981-
982-
/* Add SCTP to inet_protos hash table. */
983-
status = -EAGAIN;
984-
if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
985-
goto err_add_protocol;
986-
987-
/* Add SCTP(TCP and UDP style) to inetsw linked list. */
988-
inet_register_protosw(&sctp_seqpacket_protosw);
989-
inet_register_protosw(&sctp_stream_protosw);
990-
991-
/* Allocate a cache pools. */
978+
/* Allocate bind_bucket and chunk caches. */
992979
status = -ENOBUFS;
993980
sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
994981
sizeof(struct sctp_bind_bucket),
995982
0, SLAB_HWCACHE_ALIGN,
996983
NULL, NULL);
997-
998984
if (!sctp_bucket_cachep)
999-
goto err_bucket_cachep;
985+
goto out;
1000986

1001987
sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
1002988
sizeof(struct sctp_chunk),
@@ -1153,6 +1139,14 @@ SCTP_STATIC __init int sctp_init(void)
11531139
INIT_LIST_HEAD(&sctp_address_families);
11541140
sctp_register_af(&sctp_ipv4_specific);
11551141

1142+
status = proto_register(&sctp_prot, 1);
1143+
if (status)
1144+
goto err_proto_register;
1145+
1146+
/* Register SCTP(UDP and TCP style) with socket layer. */
1147+
inet_register_protosw(&sctp_seqpacket_protosw);
1148+
inet_register_protosw(&sctp_stream_protosw);
1149+
11561150
status = sctp_v6_init();
11571151
if (status)
11581152
goto err_v6_init;
@@ -1166,19 +1160,39 @@ SCTP_STATIC __init int sctp_init(void)
11661160

11671161
/* Initialize the local address list. */
11681162
INIT_LIST_HEAD(&sctp_local_addr_list);
1169-
11701163
sctp_get_local_addr_list();
11711164

11721165
/* Register notifier for inet address additions/deletions. */
11731166
register_inetaddr_notifier(&sctp_inetaddr_notifier);
11741167

1168+
/* Register SCTP with inet layer. */
1169+
if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0) {
1170+
status = -EAGAIN;
1171+
goto err_add_protocol;
1172+
}
1173+
1174+
/* Register SCTP with inet6 layer. */
1175+
status = sctp_v6_add_protocol();
1176+
if (status)
1177+
goto err_v6_add_protocol;
1178+
11751179
__unsafe(THIS_MODULE);
11761180
status = 0;
11771181
out:
11781182
return status;
1183+
err_v6_add_protocol:
1184+
inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1185+
unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1186+
err_add_protocol:
1187+
sctp_free_local_addr_list();
1188+
sock_release(sctp_ctl_socket);
11791189
err_ctl_sock_init:
11801190
sctp_v6_exit();
11811191
err_v6_init:
1192+
inet_unregister_protosw(&sctp_stream_protosw);
1193+
inet_unregister_protosw(&sctp_seqpacket_protosw);
1194+
proto_unregister(&sctp_prot);
1195+
err_proto_register:
11821196
sctp_sysctl_unregister();
11831197
list_del(&sctp_ipv4_specific.list);
11841198
free_pages((unsigned long)sctp_port_hashtable,
@@ -1192,19 +1206,13 @@ SCTP_STATIC __init int sctp_init(void)
11921206
sizeof(struct sctp_hashbucket)));
11931207
err_ahash_alloc:
11941208
sctp_dbg_objcnt_exit();
1195-
err_init_proc:
11961209
sctp_proc_exit();
1210+
err_init_proc:
11971211
cleanup_sctp_mibs();
11981212
err_init_mibs:
11991213
kmem_cache_destroy(sctp_chunk_cachep);
12001214
err_chunk_cachep:
12011215
kmem_cache_destroy(sctp_bucket_cachep);
1202-
err_bucket_cachep:
1203-
inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1204-
inet_unregister_protosw(&sctp_seqpacket_protosw);
1205-
inet_unregister_protosw(&sctp_stream_protosw);
1206-
err_add_protocol:
1207-
proto_unregister(&sctp_prot);
12081216
goto out;
12091217
}
12101218

@@ -1215,16 +1223,26 @@ SCTP_STATIC __exit void sctp_exit(void)
12151223
* up all the remaining associations and all that memory.
12161224
*/
12171225

1218-
/* Unregister notifier for inet address additions/deletions. */
1219-
unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1226+
/* Unregister with inet6/inet layers. */
1227+
sctp_v6_del_protocol();
1228+
inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
12201229

12211230
/* Free the local address list. */
12221231
sctp_free_local_addr_list();
12231232

12241233
/* Free the control endpoint. */
12251234
sock_release(sctp_ctl_socket);
12261235

1236+
/* Cleanup v6 initializations. */
12271237
sctp_v6_exit();
1238+
1239+
/* Unregister with socket layer. */
1240+
inet_unregister_protosw(&sctp_stream_protosw);
1241+
inet_unregister_protosw(&sctp_seqpacket_protosw);
1242+
1243+
/* Unregister notifier for inet address additions/deletions. */
1244+
unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1245+
12281246
sctp_sysctl_unregister();
12291247
list_del(&sctp_ipv4_specific.list);
12301248

@@ -1236,16 +1254,13 @@ SCTP_STATIC __exit void sctp_exit(void)
12361254
get_order(sctp_port_hashsize *
12371255
sizeof(struct sctp_bind_hashbucket)));
12381256

1239-
kmem_cache_destroy(sctp_chunk_cachep);
1240-
kmem_cache_destroy(sctp_bucket_cachep);
1241-
12421257
sctp_dbg_objcnt_exit();
12431258
sctp_proc_exit();
12441259
cleanup_sctp_mibs();
12451260

1246-
inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1247-
inet_unregister_protosw(&sctp_seqpacket_protosw);
1248-
inet_unregister_protosw(&sctp_stream_protosw);
1261+
kmem_cache_destroy(sctp_chunk_cachep);
1262+
kmem_cache_destroy(sctp_bucket_cachep);
1263+
12491264
proto_unregister(&sctp_prot);
12501265
}
12511266

0 commit comments

Comments
 (0)