Skip to content

Fix authentication on a cellular network for Access Point Name (APN) #8147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion features/cellular/TESTS/socket/udp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ static void udp_network_stack()
cellular.set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
#ifdef MBED_CONF_APP_APN
CellularNetwork *network = cellular.get_network();
TEST_ASSERT(network->set_credentials(MBED_CONF_APP_APN) == NSAPI_ERROR_OK);
TEST_ASSERT(network->set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == NSAPI_ERROR_OK);
#endif
cellular_target_state = CellularConnectionFSM::STATE_CONNECTED;
TEST_ASSERT(cellular.continue_to_state(cellular_target_state) == NSAPI_ERROR_OK);
Expand Down
2 changes: 1 addition & 1 deletion features/cellular/framework/API/CellularNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ class CellularNetwork : public NetworkInterface {
*
* @return NSAPI_ERROR_OK on success
* NSAPI_ERROR_NO_CONNECTION if fails to find suitable context to activate or activation failed (if not already activated)
* NSAPI_ERROR_UNSUPPORTED if NetworkStack was not found
* NSAPI_ERROR_UNSUPPORTED if NetworkStack was not found or cellular device does not support authentication
* NSAPI_ERROR_AUTH_FAILURE if password and username were provided and authentication to network failed
* Also if PPP mode
* NSAPI_ERROR_DEVICE_ERROR on failure and check more error from nsapi_ppp_connect(...)
Expand Down
1 change: 1 addition & 0 deletions features/cellular/framework/AT/AT_CellularBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class AT_CellularBase {
enum SupportedFeature {
AT_CGSN_WITH_TYPE, // AT+CGSN without type is likely always supported similar to AT+GSN
AT_CGDATA, // alternative is to support only ATD*99***<cid>#
AT_CGAUTH, // APN authentication AT commands supported
SUPPORTED_FEATURE_END_MARK // must be last element in the array of features
};
static void set_unsupported_features(const SupportedFeature *unsupported_features);
Expand Down
17 changes: 13 additions & 4 deletions features/cellular/framework/AT/AT_CellularNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ nsapi_error_t AT_CellularNetwork::set_credentials(const char *apn,
}

if (username && (len = strlen(username)) > 0) {
if (!is_supported(AT_CGAUTH)) { // APN authentication is needed with username/password
return NSAPI_ERROR_UNSUPPORTED;
}
_uname = (char *)malloc(len * sizeof(char) + 1);
if (_uname) {
memcpy(_uname, username, len + 1);
Expand Down Expand Up @@ -279,10 +282,7 @@ nsapi_error_t AT_CellularNetwork::activate_context()
nsapi_error_t err = NSAPI_ERROR_OK;

// try to find or create context with suitable stack
if (get_context()) {
// try to authenticate user before activating or modifying context
err = do_user_authentication();
} else {
if (!get_context()) {
err = NSAPI_ERROR_NO_CONNECTION;
}

Expand Down Expand Up @@ -315,6 +315,12 @@ nsapi_error_t AT_CellularNetwork::activate_context()
_at.resp_stop();

if (!_is_context_active) {
// authenticate before activating or modifying context
if (do_user_authentication() != NSAPI_ERROR_OK) {
tr_error("Cellular authentication failed!");
return _at.unlock_return_error();
}

tr_info("Activate PDP context %d", _cid);
_at.cmd_start("AT+CGACT=1,");
_at.write_int(_cid);
Expand Down Expand Up @@ -509,6 +515,9 @@ nsapi_error_t AT_CellularNetwork::do_user_authentication()
{
// if user has defined user name and password we need to call CGAUTH before activating or modifying context
if (_pwd && _uname) {
if (!is_supported(AT_CGAUTH)) {
return NSAPI_ERROR_UNSUPPORTED;
}
_at.cmd_start("AT+CGAUTH=");
_at.write_int(_cid);
_at.write_int(_authentication_type);
Expand Down
6 changes: 6 additions & 0 deletions features/cellular/framework/AT/AT_CellularNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ class AT_CellularNetwork : public CellularNetwork, public AT_CellularBase {
*/
virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology op_rat);

/** APN user authentication
*
* @return NSAPI_ERROR_OK on success
* NSAPI_ERROR_UNSUPPORTED on authentication not supported by cellular device
* NSAPI_ERROR_AUTH_FAILURE on authentication to network failed
*/
virtual nsapi_error_t do_user_authentication();
private:
// "NO CARRIER" urc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@
using namespace events;
using namespace mbed;

static const AT_CellularBase::SupportedFeature unsupported_features[] = {
AT_CellularBase::AT_CGAUTH, // BC95_AT_Commands_Manual_V1.9
AT_CellularBase::SUPPORTED_FEATURE_END_MARK
};

QUECTEL_BC95::QUECTEL_BC95(EventQueue &queue) : AT_CellularDevice(queue)
{
AT_CellularBase::set_unsupported_features(unsupported_features);
}

QUECTEL_BC95::~QUECTEL_BC95()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,24 @@ nsapi_error_t QUECTEL_BG96_CellularNetwork::set_access_technology_impl(RadioAcce

return _at.unlock_return_error();
}

nsapi_error_t QUECTEL_BG96_CellularNetwork::do_user_authentication()
{
if (_pwd && _uname) {
_at.cmd_start("AT+QICSGP=");
_at.write_int(_cid);
_at.write_int(1); // IPv4
_at.write_string(_apn);
_at.write_string(_uname);
_at.write_string(_pwd);
_at.write_int(_authentication_type);
_at.cmd_stop();
_at.resp_start();
_at.resp_stop();
if (_at.get_last_error() != NSAPI_ERROR_OK) {
return NSAPI_ERROR_AUTH_FAILURE;
}
}

return NSAPI_ERROR_OK;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class QUECTEL_BG96_CellularNetwork : public AT_CellularNetwork {
virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opRat);

virtual bool get_modem_stack_type(nsapi_ip_stack_t requested_stack);

virtual nsapi_error_t do_user_authentication();
};

} // namespace mbed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ using namespace events;

static const AT_CellularBase::SupportedFeature unsupported_features[] = {
AT_CellularBase::AT_CGSN_WITH_TYPE, // HE910/UE910/UL865/UE866 AT Commands Reference Guide Rev. 11-2006-10-14
AT_CellularBase::AT_CGAUTH, // HE910/UE910/UL865/UE866 AT Commands Reference Guide Rev. 11-2006-10-14
AT_CellularBase::SUPPORTED_FEATURE_END_MARK
};

Expand Down