5 * Copyright (C) 2013 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <arpa/inet.h>
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/plugin.h>
36 #include <connman/dbus.h>
37 #include <connman/technology.h>
38 #include <connman/provision.h>
44 #define NEARD_SERVICE "org.neard"
45 #define NEARD_PATH "/"
46 #define NEARD_MANAGER_INTERFACE "org.neard.Manager"
47 #define NEARD_AGENT_INTERFACE "org.neard.HandoverAgent"
48 #define NEARD_ERROR_INTERFACE "org.neard.HandoverAgent.Error"
50 #define AGENT_PATH "/net/connman/neard_handover_agent"
51 #define AGENT_TYPE "wifi"
53 #define NEARD_SERVICE_GROUP "service_neard_provision"
55 /* Configuration keys */
56 #define SERVICE_KEY_TYPE "Type"
57 #define SERVICE_KEY_SSID "SSID"
58 #define SERVICE_KEY_PASSPHRASE "Passphrase"
59 #define SERVICE_KEY_HIDDEN "Hidden"
61 struct data_elements {
68 DE_AUTHENTICATION_TYPE = 0,
74 static const struct data_elements DEs[DE_MAX] = {
76 { 0x1027, 64, FALSE },
77 { 0x1045, 32, FALSE },
80 #define DE_VAL_OPEN 0x0001
81 #define DE_VAL_PSK 0x0022
88 static DBusConnection *connection = NULL;
89 DBusPendingCall *register_call = NULL;
90 bool agent_registered = false;
91 static guint watch_id = 0;
93 static int set_2b_into_tlv(uint8_t *tlv_msg, uint16_t val)
98 ins = (uint8_t *) &ne_val;
107 static int set_byte_array_into_tlv(uint8_t *tlv_msg,
108 uint8_t *array, int length)
110 memcpy((void *)tlv_msg, (void *)array, length);
114 static uint8_t *encode_to_tlv(const char *ssid, const char *psk, int *length)
116 uint16_t ssid_len, psk_len;
120 if (!ssid || !length)
123 ssid_len = strlen(ssid);
125 *length = 6 + 4 + ssid_len;
127 psk_len = strlen(psk);
128 *length += 4 + psk_len;
132 tlv_msg = g_try_malloc0(sizeof(uint8_t) * (*length));
136 pos += set_2b_into_tlv(tlv_msg+pos, DEs[DE_SSID].value);
137 pos += set_2b_into_tlv(tlv_msg+pos, ssid_len);
138 pos += set_byte_array_into_tlv(tlv_msg+pos, (uint8_t *)ssid, ssid_len);
140 pos += set_2b_into_tlv(tlv_msg+pos, DEs[DE_AUTHENTICATION_TYPE].value);
141 pos += set_2b_into_tlv(tlv_msg+pos,
142 DEs[DE_AUTHENTICATION_TYPE].length);
144 pos += set_2b_into_tlv(tlv_msg+pos, DE_VAL_PSK);
145 pos += set_2b_into_tlv(tlv_msg+pos, DEs[DE_NETWORK_KEY].value);
146 pos += set_2b_into_tlv(tlv_msg+pos, psk_len);
147 pos += set_byte_array_into_tlv(tlv_msg+pos,
148 (uint8_t *)psk, psk_len);
150 pos += set_2b_into_tlv(tlv_msg+pos, DE_VAL_OPEN);
155 static DBusMessage *get_reply_on_error(DBusMessage *message, int error)
157 if (error == ENOTSUP)
158 return g_dbus_create_error(message,
159 NEARD_ERROR_INTERFACE ".NotSupported",
160 "Operation is not supported");
162 return g_dbus_create_error(message,
163 NEARD_ERROR_INTERFACE ".Failed", "Invalid parameters");
166 static int parse_request_oob_params(DBusMessage *message,
167 const uint8_t **tlv_msg, int *length)
169 DBusMessageIter iter;
170 DBusMessageIter array;
171 DBusMessageIter dict_entry;
175 if (!tlv_msg || !length)
178 dbus_message_iter_init(message, &iter);
180 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
183 dbus_message_iter_recurse(&iter, &array);
184 arg_type = dbus_message_iter_get_arg_type(&array);
185 if (arg_type != DBUS_TYPE_DICT_ENTRY)
188 while (arg_type != DBUS_TYPE_INVALID) {
189 dbus_message_iter_recurse(&array, &dict_entry);
190 if (dbus_message_iter_get_arg_type(&dict_entry) !=
194 dbus_message_iter_get_basic(&dict_entry, &key);
195 if (g_strcmp0(key, "WSC") == 0) {
196 DBusMessageIter value;
197 DBusMessageIter fixed_array;
199 dbus_message_iter_next(&dict_entry);
200 dbus_message_iter_recurse(&dict_entry, &value);
202 if (dbus_message_iter_get_arg_type(&value) !=
206 dbus_message_iter_recurse(&value, &fixed_array);
207 dbus_message_iter_get_fixed_array(&fixed_array,
212 dbus_message_iter_next(&array);
213 arg_type = dbus_message_iter_get_arg_type(&array);
219 static DBusMessage *create_request_oob_reply(DBusMessage *message)
222 DBusMessageIter iter;
223 DBusMessageIter dict;
224 const char *ssid, *psk;
228 if (!connman_technology_get_wifi_tethering(&ssid, &psk))
229 return get_reply_on_error(message, ENOTSUP);
231 tlv_msg = encode_to_tlv(ssid, psk, &length);
233 return get_reply_on_error(message, ENOTSUP);
235 reply = dbus_message_new_method_return(message);
239 dbus_message_iter_init_append(reply, &iter);
241 connman_dbus_dict_open(&iter, &dict);
243 connman_dbus_dict_append_fixed_array(&dict, "WSC",
244 DBUS_TYPE_BYTE, &tlv_msg, length);
246 dbus_message_iter_close_container(&iter, &dict);
254 static DBusMessage *request_oob_method(DBusConnection *dbus_conn,
255 DBusMessage *message, void *user_data)
257 DBusMessageIter iter;
261 dbus_message_iter_init(message, &iter);
263 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
264 return get_reply_on_error(message, EINVAL);
266 return create_request_oob_reply(message);
269 static void free_wifi_sc(struct wifi_sc *wsc)
275 g_free(wsc->passphrase);
280 static inline int get_2b_from_tlv(const uint8_t *tlv_msg, uint16_t *val)
282 *val = ntohs(tlv_msg[0] | (tlv_msg[1] << 8));
287 static uint8_t *get_byte_array_from_tlv(const uint8_t *tlv_msg, int length)
291 array = g_try_malloc0(sizeof(uint8_t) * length);
295 memcpy((void *)array, (void *)tlv_msg, length*sizeof(uint8_t));
300 static char *get_string_from_tlv(const uint8_t *tlv_msg, int length)
304 str = g_try_malloc0((sizeof(char) * length) + 1);
308 memcpy((void *)str, (void *)tlv_msg, length*sizeof(char));
313 static inline DEid get_de_id(uint16_t attr)
317 for (i = 0; i < DE_MAX && DEs[i].value != 0; i++) {
318 if (attr == DEs[i].value)
325 static inline bool is_de_length_fine(DEid id, uint16_t length)
327 if (DEs[id].fixed_length)
328 return (length == DEs[id].length);
330 return (length <= DEs[id].length);
333 static char *get_hexstr_from_byte_array(const uint8_t *bt, int length)
338 hex_str = g_try_malloc0(((length*2)+1) * sizeof(char));
342 for (i = 0, j = 0; i < length; i++, j += 2)
343 snprintf(hex_str+j, 3, "%02x", bt[i]);
348 static struct wifi_sc *decode_from_tlv(const uint8_t *tlv_msg, int length)
356 if (!tlv_msg || length == 0)
359 wsc = g_try_malloc0(sizeof(struct wifi_sc));
364 while (pos < length) {
365 pos += get_2b_from_tlv(tlv_msg+pos, &attr);
366 pos += get_2b_from_tlv(tlv_msg+pos, &len);
368 if (len > length - pos)
371 id = get_de_id(attr);
377 if (!is_de_length_fine(id, len))
381 case DE_AUTHENTICATION_TYPE:
384 wsc->passphrase = get_string_from_tlv(tlv_msg+pos,
388 bt = get_byte_array_from_tlv(tlv_msg+pos, len);
389 wsc->ssid = get_hexstr_from_byte_array(bt, len);
394 /* Falling back. Should never happen though. */
409 static int handle_wcs_data(const uint8_t *tlv_msg, int length)
411 struct wifi_sc *wsc = NULL;
412 GKeyFile *keyfile = NULL;
415 wsc = decode_from_tlv(tlv_msg, length);
422 keyfile = g_key_file_new();
428 g_key_file_set_string(keyfile, NEARD_SERVICE_GROUP,
429 SERVICE_KEY_TYPE, "wifi");
430 g_key_file_set_string(keyfile, NEARD_SERVICE_GROUP,
431 SERVICE_KEY_SSID, wsc->ssid);
432 g_key_file_set_boolean(keyfile, NEARD_SERVICE_GROUP,
433 SERVICE_KEY_HIDDEN, TRUE);
436 g_key_file_set_string(keyfile, NEARD_SERVICE_GROUP,
437 SERVICE_KEY_PASSPHRASE, wsc->passphrase);
439 ret = connman_config_provision_mutable_service(keyfile);
442 g_key_file_free(keyfile);
447 static DBusMessage *push_oob_method(DBusConnection *dbus_conn,
448 DBusMessage *message, void *user_data)
450 const uint8_t *tlv_msg;
456 if (parse_request_oob_params(message, &tlv_msg, &length) != 0)
457 return get_reply_on_error(message, err);
459 err = handle_wcs_data(tlv_msg, length);
461 return get_reply_on_error(message, err);
463 return g_dbus_create_reply(message, DBUS_TYPE_INVALID);
466 static DBusMessage *release_method(DBusConnection *dbus_conn,
467 DBusMessage *message, void *user_data)
471 agent_registered = false;
472 g_dbus_unregister_interface(connection,
473 AGENT_PATH, NEARD_AGENT_INTERFACE);
475 return g_dbus_create_reply(message, DBUS_TYPE_INVALID);
478 static const GDBusMethodTable neard_methods[] = {
479 { GDBUS_ASYNC_METHOD("RequestOOB",
480 GDBUS_ARGS({ "data", "a{sv}" }),
481 GDBUS_ARGS({ "data", "a{sv}" }), request_oob_method) },
482 { GDBUS_ASYNC_METHOD("PushOOB",
483 GDBUS_ARGS({ "data", "a{sv}"}), NULL, push_oob_method) },
484 { GDBUS_METHOD("Release", NULL, NULL, release_method) },
488 static void cleanup_register_call(void)
491 dbus_pending_call_cancel(register_call);
492 dbus_pending_call_unref(register_call);
493 register_call = NULL;
497 static void register_agent_cb(DBusPendingCall *pending, void *user_data)
501 if (!dbus_pending_call_get_completed(pending))
504 register_call = NULL;
506 reply = dbus_pending_call_steal_reply(pending);
510 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
511 g_dbus_unregister_interface(connection,
512 AGENT_PATH, NEARD_AGENT_INTERFACE);
514 agent_registered = true;
516 dbus_message_unref(reply);
518 dbus_pending_call_unref(pending);
521 static void register_agent(void)
523 const char *path = AGENT_PATH;
524 const char *type = AGENT_TYPE;
525 DBusMessage *message;
527 message = dbus_message_new_method_call(NEARD_SERVICE, NEARD_PATH,
528 NEARD_MANAGER_INTERFACE,
529 "RegisterHandoverAgent");
533 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
534 &path, DBUS_TYPE_STRING, &type, DBUS_TYPE_INVALID);
536 if (!dbus_connection_send_with_reply(connection, message,
537 ®ister_call, TIMEOUT))
540 if (!dbus_pending_call_set_notify(register_call,
541 register_agent_cb, NULL, NULL))
542 cleanup_register_call();
545 dbus_message_unref(message);
548 static void unregister_agent(void)
550 const char *path = AGENT_PATH;
551 const char *type = AGENT_TYPE;
552 DBusMessage *message;
554 if (!agent_registered)
555 return cleanup_register_call();
557 agent_registered = false;
559 message = dbus_message_new_method_call(NEARD_SERVICE, NEARD_PATH,
560 NEARD_MANAGER_INTERFACE,
561 "UnregisterHandoverAgent");
563 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH,
564 &path, DBUS_TYPE_STRING, &type, DBUS_TYPE_INVALID);
565 g_dbus_send_message(connection, message);
568 g_dbus_unregister_interface(connection,
569 AGENT_PATH, NEARD_AGENT_INTERFACE);
572 static void neard_is_present(DBusConnection *conn, void *user_data)
576 if (agent_registered)
579 if (g_dbus_register_interface(connection, AGENT_PATH,
580 NEARD_AGENT_INTERFACE, neard_methods,
581 NULL, NULL, NULL, NULL))
585 static void neard_is_out(DBusConnection *conn, void *user_data)
589 if (agent_registered) {
590 g_dbus_unregister_interface(connection,
591 AGENT_PATH, NEARD_AGENT_INTERFACE);
592 agent_registered = false;
595 cleanup_register_call();
598 static int neard_init(void)
600 connection = connman_dbus_get_connection();
604 watch_id = g_dbus_add_service_watch(connection, NEARD_SERVICE,
605 neard_is_present, neard_is_out,
608 dbus_connection_unref(connection);
615 static void neard_exit(void)
620 g_dbus_remove_watch(connection, watch_id);
622 dbus_connection_unref(connection);
625 CONNMAN_PLUGIN_DEFINE(neard, "Neard handover plugin", VERSION,
626 CONNMAN_PLUGIN_PRIORITY_DEFAULT, neard_init, neard_exit)