2 * Network Configuration Module
4 * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
26 #include <arpa/inet.h>
29 #include <sys/ioctl.h>
30 #include <vconf-keys.h>
32 #include <TapiUtility.h>
36 #ifdef USE_NETCONFIG_LOG
41 #define NETCONFIG_TAG "NETCONFIG"
42 #define __LOG(level, format, arg...) \
44 SLOG(level, NETCONFIG_TAG, format, ## arg); \
47 #define DBG(format, arg...) __LOG(LOG_DEBUG, format, ## arg)
48 #define ERR(format, arg...) __LOG(LOG_ERROR, format, ## arg)
51 #define TAPI_HANDLE_MAX 2
53 /* #define SIM_SLOT_DUAL 2 */
54 #define SIM_SLOT_SINGLE 1
56 #define VCONF_TELEPHONY_DEFAULT_DATA_SERVICE "db/telephony/dualsim/default_data_service"
58 #define DEFAULT_DATA_SERVICE_SIM1 0
59 #define DEFAULT_DATA_SERVICE_SIM2 1
61 #define SIM_RAND_DATA_LEN 16
62 #define SIM_AUTH_MAX_RESP_DATA_LEN 128
63 #define SIM_AUTH_SRES_LEN 4
64 #define SIM_AUTH_KC_LEN 8
66 #define AKA_RAND_DATA_LEN 16
67 #define AKA_AUTN_DATA_LEN 16
68 #define AKA_AUTH_RES_MAX_LEN 16
69 #define AKA_AUTH_RES_MIN_LEN 4
70 #define AKA_AUTH_CK_LEN 16
71 #define AKA_AUTH_IK_LEN 16
73 static TapiHandle *tapi_handle_dual[TAPI_HANDLE_MAX+1];
74 static TapiHandle *tapi_handle = NULL;
75 struct wifi_authentication_data **wifi_authdata_p = NULL;
77 #define wifi_authdata (*wifi_authdata_p)
79 static int _check_current_sim()
81 #if defined TIZEN_WEARABLE
85 int sim_slot_count = 0;
87 if ((vconf_get_int(VCONFKEY_TELEPHONY_SIM_SLOT_COUNT, &sim_slot_count) != 0)
88 || sim_slot_count == SIM_SLOT_SINGLE) {
89 ERR("failed to get sim slot count (%d)", sim_slot_count);
93 if (vconf_get_int(VCONF_TELEPHONY_DEFAULT_DATA_SERVICE, ¤t_sim) != 0) {
94 ERR("failed to get default data service = %d\n", current_sim);
98 DBG("default data service [SIM%d]", current_sim);
103 static TapiHandle * netconfig_tel_init(void)
105 char **cp_list = NULL;
106 int current_sim = _check_current_sim();
108 if (current_sim < 0) {
109 if (tapi_handle == NULL) {
110 tapi_handle = tel_init(NULL);
111 if (tapi_handle == NULL)
112 ERR("tel_init() Failed - modem %d", current_sim);
116 if (tapi_handle_dual[current_sim] == NULL) {
117 cp_list = tel_get_cp_name_list();
119 ERR("tel_get_cp_name_list() Failed");
123 tapi_handle_dual[current_sim] = tel_init(cp_list[current_sim]);
124 if (tapi_handle_dual[current_sim] == NULL)
125 ERR("tel_init() Failed - modem %d", current_sim);
129 return tapi_handle_dual[current_sim];
133 static void netconfig_tel_deinit(void)
135 int current_sim = _check_current_sim();
137 if (current_sim < 0) {
139 tel_deinit(tapi_handle);
144 while (tapi_handle_dual[i]) {
145 tel_deinit(tapi_handle_dual[i]);
146 tapi_handle_dual[i] = NULL;
152 void telephony_get_network_type(int *svctype, int *pstype)
154 TapiHandle *tapi_handle = NULL;
155 int telephony_svctype = 0, telephony_pstype = 0;
157 tapi_handle = (TapiHandle *)netconfig_tel_init();
159 if (NULL != tapi_handle) {
160 tel_get_property_int(tapi_handle,
161 TAPI_PROP_NETWORK_SERVICE_TYPE,
163 tel_get_property_int(tapi_handle, TAPI_PROP_NETWORK_PS_TYPE,
165 netconfig_tel_deinit();
168 *svctype = telephony_svctype;
169 *pstype = telephony_pstype;
172 gboolean telephony_wifi_get_sim_imsi(void *wifi, GDBusMethodInvocation *context)
176 TelSimImsiInfo_t imsi_info;
179 handle = (TapiHandle *)netconfig_tel_init();
180 if (handle == NULL) {
181 ERR("tapi_init failed");
182 netconfig_error_fail_get_imsi(context);
186 ERR("before tel_get_sim_imsi");
187 ret = tel_get_sim_imsi(handle, &imsi_info);
188 ERR("after tel_get_sim_imsi");
189 if (ret != TAPI_API_SUCCESS) {
190 ERR("Failed tel_get_sim_imsi() : [%d]", ret);
191 netconfig_error_fail_get_imsi(context);
195 imsi = g_strdup_printf("%s%s%s", imsi_info.szMcc,
196 imsi_info.szMnc, imsi_info.szMsin);
198 netconfig_complete_get_sim_imsi(wifi, context, imsi);
204 static void *__telephony_wifi_free_wifi_authdata(
205 struct wifi_authentication_data *data)
209 g_free(data->resp_data);
210 if (data->authentication_key)
211 g_free(data->authentication_key);
212 if (data->cipher_data)
213 g_free(data->cipher_data);
214 if (data->integrity_data)
215 g_free(data->integrity_data);
224 static void __telephony_response_aka_authentication(TapiHandle *handle,
225 int result, void *data, void *user_data)
227 if (wifi_authdata != NULL)
228 wifi_authdata = __telephony_wifi_free_wifi_authdata(wifi_authdata);
230 wifi_authdata = g_try_new0(struct wifi_authentication_data, 1);
232 if (wifi_authdata == NULL) {
233 ERR("Out of Memory!");
237 TelSimAuthenticationResponse_t *auth_resp =
238 (TelSimAuthenticationResponse_t *) data;
239 if (auth_resp == NULL) {
240 ERR("the auth response is NULL");
242 wifi_authdata->auth_result = -1;
245 wifi_authdata->auth_result = auth_resp->auth_result;
247 if (auth_resp->auth_result == TAPI_SIM_AUTH_NO_ERROR) {
248 wifi_authdata->resp_length = auth_resp->resp_length;
249 wifi_authdata->cipher_length = auth_resp->cipher_length;
250 wifi_authdata->integrity_length = auth_resp->integrity_length;
252 if (wifi_authdata->resp_data != NULL)
253 g_free(wifi_authdata->resp_data);
255 wifi_authdata->resp_data = g_strdup(auth_resp->resp_data);
257 if (wifi_authdata->cipher_data != NULL)
258 g_free(wifi_authdata->cipher_data);
260 wifi_authdata->cipher_data = g_strdup(auth_resp->cipher_data);
262 if (wifi_authdata->integrity_data != NULL)
263 g_free(wifi_authdata->integrity_data);
265 wifi_authdata->integrity_data = g_strdup(auth_resp->integrity_data);
267 ERR("the result error for aka auth : [%d]", auth_resp->auth_result);
269 if (auth_resp->auth_result == TAPI_SIM_AUTH_SQN_FAILURE ||
270 auth_resp->auth_result == TAPI_SIM_AUTH_SYNCH_FAILURE) {
271 wifi_authdata->resp_length = auth_resp->resp_length;
273 if (wifi_authdata->resp_data != NULL)
274 g_free(wifi_authdata->resp_data);
276 wifi_authdata->resp_data = g_strdup(auth_resp->resp_data);
281 netconfig_error_e telephony_wifi_req_aka_auth(GArray *rand_data, GArray *autn_data,
282 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
287 TelSimAuthenticationData_t auth_data;
289 if (!wifi_authdata_p)
290 wifi_authdata_p = data;
292 if (rand_data == NULL || autn_data == NULL)
293 return NETCONFIG_ERROR_FAILED_REQ_SIM_AUTH;
295 if (rand_data->len != AKA_RAND_DATA_LEN) {
296 ERR("wrong rand data len : [%d]", rand_data->len);
298 return NETCONFIG_ERROR_FAILED_REQ_SIM_AUTH_WRONG_PARAM;
301 if (autn_data->len != AKA_AUTN_DATA_LEN) {
302 ERR("wrong autn data len : [%d]", autn_data->len);
304 return NETCONFIG_ERROR_FAILED_REQ_SIM_AUTH_WRONG_PARAM;
307 if ((ret = g_array_get_element_size(rand_data)) != 1) {
308 ERR("wrong rand data size : [%d]", ret);
310 return NETCONFIG_ERROR_FAILED_REQ_SIM_AUTH_WRONG_PARAM;
313 if ((ret = g_array_get_element_size(autn_data)) != 1) {
314 ERR("wrong autn data size : [%d]", ret);
316 return NETCONFIG_ERROR_FAILED_REQ_SIM_AUTH_WRONG_PARAM;
319 memset(&auth_data, 0, sizeof(auth_data));
321 auth_data.auth_type = TAPI_SIM_AUTH_TYPE_3G;
322 auth_data.rand_length = AKA_RAND_DATA_LEN;
323 auth_data.autn_length = AKA_AUTN_DATA_LEN;
325 for (i = 0; i < rand_data->len; i++)
326 auth_data.rand_data[i] = g_array_index(rand_data, guint8, i);
328 for (i = 0; i < autn_data->len; i++)
329 auth_data.autn_data[i] = g_array_index(autn_data, guint8, i);
331 handle = (TapiHandle *)netconfig_tel_init();
333 return NETCONFIG_ERROR_FAILED_REQ_SIM_AUTH;
335 ret = tel_req_sim_authentication(handle, &auth_data,
336 __telephony_response_aka_authentication, NULL);
338 if (ret != TAPI_API_SUCCESS) {
339 ERR("Failed tel_req_sim_authentication() : [%d]", ret);
341 return NETCONFIG_ERROR_FAILED_REQ_SIM_AUTH;
343 return NETCONFIG_NO_ERROR;
346 static void telephony_response_sim_authentication(TapiHandle *handle,
347 int result, void *data, void *user_data)
349 if (wifi_authdata != NULL)
350 wifi_authdata = __telephony_wifi_free_wifi_authdata(wifi_authdata);
352 wifi_authdata = g_try_new0(struct wifi_authentication_data, 1);
354 if (wifi_authdata == NULL) {
355 ERR("Out of Memory!");
359 TelSimAuthenticationResponse_t *auth_resp =
360 (TelSimAuthenticationResponse_t *) data;
361 if (auth_resp == NULL) {
362 ERR("the auth response is NULL");
364 wifi_authdata->auth_result = -1;
367 wifi_authdata->auth_result = auth_resp->auth_result;
369 if (auth_resp->auth_result == TAPI_SIM_AUTH_NO_ERROR) {
370 wifi_authdata->resp_length = auth_resp->resp_length;
371 wifi_authdata->authentication_key_length =
372 auth_resp->authentication_key_length;
374 if (wifi_authdata->resp_data != NULL)
375 g_free(wifi_authdata->resp_data);
377 wifi_authdata->resp_data = g_strdup(auth_resp->resp_data);
379 if (wifi_authdata->authentication_key != NULL)
380 g_free(wifi_authdata->authentication_key);
382 wifi_authdata->authentication_key = g_strdup(auth_resp->authentication_key);
384 ERR("the result error for sim auth : [%d]", auth_resp->auth_result);
386 wifi_authdata->resp_length = 0;
387 wifi_authdata->authentication_key_length = 0;
391 gboolean telephony_wifi_req_sim_auth(GArray *rand_data,
392 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
397 TelSimAuthenticationData_t auth_data;
399 if (!wifi_authdata_p)
400 wifi_authdata_p = data;
402 if (rand_data == NULL)
405 if (rand_data->len != SIM_RAND_DATA_LEN) {
406 ERR("wrong rand data len : [%d]", rand_data->len);
408 netconfig_error_fail_req_sim_auth_wrong_param(context);
412 if ((ret = g_array_get_element_size(rand_data)) != 1) {
413 ERR("wrong rand data size : [%d]", ret);
415 netconfig_error_fail_req_sim_auth_wrong_param(context);
419 memset(&auth_data, 0, sizeof(auth_data));
421 auth_data.auth_type = TAPI_SIM_AUTH_TYPE_GSM;
422 auth_data.rand_length = SIM_RAND_DATA_LEN;
424 for (i = 0; i < rand_data->len; i++)
425 auth_data.rand_data[i] = g_array_index(rand_data, guint8, i);
427 handle = (TapiHandle *)netconfig_tel_init();
428 if (handle == NULL) {
429 netconfig_error_fail_req_sim_auth(context);
433 ret = tel_req_sim_authentication(handle,
434 &auth_data, telephony_response_sim_authentication, NULL);
435 if (ret != TAPI_API_SUCCESS) {
436 ERR("Failed tel_req_sim_authentication() : [%d]", ret);
438 netconfig_error_fail_req_sim_auth(context);
445 static void telephony_noti_sim_status_cb(TapiHandle *handle, const char *noti_id,
446 void *data, void *user_data)
448 TelSimCardStatus_t *status = data;
450 if (*status == TAPI_SIM_STATUS_SIM_INIT_COMPLETED) {
451 DBG("Turn Wi-Fi on automatically");
453 netconfig_wifi_power_on();
454 netconfig_tel_deinit();
458 gboolean telephony_tapi_check_sim_state(void)
460 int ret, card_changed;
461 TelSimCardStatus_t status = TAPI_SIM_STATUS_UNKNOWN;
462 TapiHandle *tapi_handle = NULL;
464 tapi_handle = (TapiHandle *)netconfig_tel_init();
465 if (tapi_handle == NULL) {
466 ERR("Failed to tapi init");
470 ret = tel_get_sim_init_info(tapi_handle, &status, &card_changed);
471 if (ret != TAPI_API_SUCCESS) {
472 ERR("tel_get_sim_init_info() Failed : [%d]", ret);
473 netconfig_tel_deinit();
478 case TAPI_SIM_STATUS_UNKNOWN:
479 case TAPI_SIM_STATUS_CARD_ERROR:
480 case TAPI_SIM_STATUS_CARD_NOT_PRESENT:
481 case TAPI_SIM_STATUS_CARD_BLOCKED:
482 case TAPI_SIM_STATUS_SIM_INIT_COMPLETED:
484 case TAPI_SIM_STATUS_SIM_PIN_REQUIRED:
485 case TAPI_SIM_STATUS_SIM_INITIALIZING:
486 case TAPI_SIM_STATUS_SIM_PUK_REQUIRED:
487 case TAPI_SIM_STATUS_SIM_LOCK_REQUIRED:
488 case TAPI_SIM_STATUS_SIM_NCK_REQUIRED:
489 case TAPI_SIM_STATUS_SIM_NSCK_REQUIRED:
490 case TAPI_SIM_STATUS_SIM_SPCK_REQUIRED:
491 case TAPI_SIM_STATUS_SIM_CCK_REQUIRED:
492 tel_register_noti_event(tapi_handle, TAPI_NOTI_SIM_STATUS,
493 telephony_noti_sim_status_cb, NULL);
496 ERR("not defined status(%d)", status);
500 netconfig_tel_deinit();
505 static void __telephony_wifi_clean_authentication(void)
507 netconfig_tel_deinit();
509 wifi_authdata = __telephony_wifi_free_wifi_authdata(wifi_authdata);
512 gboolean telephony_wifi_get_aka_authdata(void *wifi,
513 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
515 GArray *array = NULL;
518 if (!wifi_authdata_p)
519 wifi_authdata_p = data;
521 if (wifi_authdata == NULL) {
522 DBG("the status error : no response yet");
523 netconfig_error_fail_get_sim_auth_delay(context);
527 switch (wifi_authdata->auth_result) {
528 case TAPI_SIM_AUTH_NO_ERROR:
531 case TAPI_SIM_AUTH_SQN_FAILURE:
532 case TAPI_SIM_AUTH_SYNCH_FAILURE:
533 array = g_array_sized_new(FALSE, FALSE, sizeof(guchar),
534 wifi_authdata->resp_length+1);
535 res_len = (guchar)((wifi_authdata->resp_length-1) & 0xff);
537 g_array_append_vals(array, &res_len, 1);
538 g_array_append_vals(array, wifi_authdata->resp_data,
539 wifi_authdata->resp_length);
541 netconfig_complete_get_aka_auth(wifi, context, array);
542 g_array_free(array, TRUE);
544 __telephony_wifi_clean_authentication();
549 netconfig_error_fail_get_sim_auth_wrong_data(context);
550 __telephony_wifi_clean_authentication();
554 if ((wifi_authdata->resp_length >= AKA_AUTH_RES_MIN_LEN ||
555 wifi_authdata->resp_length <= AKA_AUTH_RES_MAX_LEN) &&
556 wifi_authdata->cipher_length == AKA_AUTH_CK_LEN &&
557 wifi_authdata->integrity_length == AKA_AUTH_IK_LEN) {
558 array = g_array_sized_new(FALSE, FALSE, sizeof(guchar),
559 wifi_authdata->resp_length+AKA_AUTH_CK_LEN+AKA_AUTH_IK_LEN+1);
561 res_len = (guchar)((wifi_authdata->resp_length-1) & 0xff);
562 g_array_append_vals(array, &res_len, 1);
563 g_array_append_vals(array, wifi_authdata->resp_data,
564 wifi_authdata->resp_length);
565 g_array_append_vals(array, wifi_authdata->cipher_data,
567 g_array_append_vals(array, wifi_authdata->integrity_data,
570 ERR("auth data length is wrong, res = [%d], Kc = [%d], Ki = [%d]",
571 wifi_authdata->resp_length, wifi_authdata->cipher_length,
572 wifi_authdata->integrity_length);
574 netconfig_error_fail_get_sim_auth_wrong_data(context);
575 __telephony_wifi_clean_authentication();
579 netconfig_complete_get_aka_auth(wifi, context, array);
580 g_array_free(array, TRUE);
581 __telephony_wifi_clean_authentication();
586 gboolean telephony_wifi_get_sim_authdata(void *wifi,
587 GDBusMethodInvocation *context, struct wifi_authentication_data **data)
589 GArray *array = NULL;
591 if (!wifi_authdata_p)
592 wifi_authdata_p = data;
594 if (wifi_authdata == NULL) {
595 DBG("the status error : no response yet");
596 netconfig_error_fail_get_sim_auth_delay(context);
600 if (wifi_authdata->auth_result == TAPI_SIM_AUTH_NO_ERROR) {
601 if (wifi_authdata->resp_length == SIM_AUTH_SRES_LEN &&
602 wifi_authdata->authentication_key_length == SIM_AUTH_KC_LEN) {
603 array = g_array_sized_new(FALSE, FALSE, sizeof(guchar),
604 SIM_AUTH_SRES_LEN+SIM_AUTH_KC_LEN);
605 g_array_append_vals(array, wifi_authdata->resp_data,
607 g_array_append_vals(array, wifi_authdata->authentication_key,
610 ERR("auth data length is wrong, SRES = [%d], Kc = [%d]",
611 wifi_authdata->resp_length,
612 wifi_authdata->authentication_key_length);
613 netconfig_error_fail_get_sim_auth_wrong_data(context);
614 __telephony_wifi_clean_authentication();
618 ERR("failed auth result = [%d]", wifi_authdata->auth_result);
619 netconfig_error_fail_get_sim_auth_wrong_data(context);
620 __telephony_wifi_clean_authentication();
624 netconfig_complete_get_sim_auth(wifi, context, array);
625 g_array_free(array, TRUE);
626 __telephony_wifi_clean_authentication();
631 extern struct netconfig_telephony_plugin_t netconfig_telephony_plugin
632 __attribute__ ((visibility("default")));
633 struct netconfig_telephony_plugin_t netconfig_telephony_plugin = {
634 telephony_get_network_type,
635 telephony_wifi_get_sim_imsi,
636 telephony_wifi_req_aka_auth,
637 telephony_wifi_req_sim_auth,
638 telephony_tapi_check_sim_state,
639 telephony_wifi_get_aka_authdata,
640 telephony_wifi_get_sim_authdata