3ec8f7154b790ce7902c5cb9f3b4e6b41d8f258b
[platform/upstream/connman.git] / src / device.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2014  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/ioctl.h>
32 #include <net/ethernet.h>
33 #include <net/if.h>
34
35 #include "connman.h"
36
37 static GSList *device_list = NULL;
38 static gchar **device_filter = NULL;
39 static gchar **nodevice_filter = NULL;
40
41 enum connman_pending_type {
42         PENDING_NONE    = 0,
43         PENDING_ENABLE  = 1,
44         PENDING_DISABLE = 2,
45 };
46
47 struct connman_device {
48         int refcount;
49         enum connman_device_type type;
50         enum connman_pending_type powered_pending;      /* Indicates a pending
51                                                          * enable/disable
52                                                          * request
53                                                          */
54         bool powered;
55         bool scanning;
56         char *name;
57         char *node;
58         char *address;
59         char *interface;
60         char *ident;
61         char *path;
62         int index;
63         guint pending_timeout;
64
65         struct connman_device_driver *driver;
66         void *driver_data;
67
68         char *last_network;
69         struct connman_network *network;
70         GHashTable *networks;
71 };
72
73 static void clear_pending_trigger(struct connman_device *device)
74 {
75         if (device->pending_timeout > 0) {
76                 g_source_remove(device->pending_timeout);
77                 device->pending_timeout = 0;
78         }
79 }
80
81 static const char *type2description(enum connman_device_type type)
82 {
83         switch (type) {
84         case CONNMAN_DEVICE_TYPE_UNKNOWN:
85         case CONNMAN_DEVICE_TYPE_VENDOR:
86                 break;
87         case CONNMAN_DEVICE_TYPE_ETHERNET:
88                 return "Ethernet";
89         case CONNMAN_DEVICE_TYPE_WIFI:
90                 return "Wireless";
91         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
92                 return "Bluetooth";
93         case CONNMAN_DEVICE_TYPE_GPS:
94                 return "GPS";
95         case CONNMAN_DEVICE_TYPE_CELLULAR:
96                 return "Cellular";
97         case CONNMAN_DEVICE_TYPE_GADGET:
98                 return "Gadget";
99
100         }
101
102         return NULL;
103 }
104
105 static const char *type2string(enum connman_device_type type)
106 {
107         switch (type) {
108         case CONNMAN_DEVICE_TYPE_UNKNOWN:
109         case CONNMAN_DEVICE_TYPE_VENDOR:
110                 break;
111         case CONNMAN_DEVICE_TYPE_ETHERNET:
112                 return "ethernet";
113         case CONNMAN_DEVICE_TYPE_WIFI:
114                 return "wifi";
115         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
116                 return "bluetooth";
117         case CONNMAN_DEVICE_TYPE_GPS:
118                 return "gps";
119         case CONNMAN_DEVICE_TYPE_CELLULAR:
120                 return "cellular";
121         case CONNMAN_DEVICE_TYPE_GADGET:
122                 return "gadget";
123
124         }
125
126         return NULL;
127 }
128
129 enum connman_service_type __connman_device_get_service_type(
130                                 struct connman_device *device)
131 {
132         enum connman_device_type type = connman_device_get_type(device);
133
134         switch (type) {
135         case CONNMAN_DEVICE_TYPE_UNKNOWN:
136         case CONNMAN_DEVICE_TYPE_VENDOR:
137         case CONNMAN_DEVICE_TYPE_GPS:
138                 break;
139         case CONNMAN_DEVICE_TYPE_ETHERNET:
140                 return CONNMAN_SERVICE_TYPE_ETHERNET;
141         case CONNMAN_DEVICE_TYPE_WIFI:
142                 return CONNMAN_SERVICE_TYPE_WIFI;
143         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
144                 return CONNMAN_SERVICE_TYPE_BLUETOOTH;
145         case CONNMAN_DEVICE_TYPE_CELLULAR:
146                 return CONNMAN_SERVICE_TYPE_CELLULAR;
147         case CONNMAN_DEVICE_TYPE_GADGET:
148                 return CONNMAN_SERVICE_TYPE_GADGET;
149
150         }
151
152         return CONNMAN_SERVICE_TYPE_UNKNOWN;
153 }
154
155 static gboolean device_pending_reset(gpointer user_data)
156 {
157         struct connman_device *device = user_data;
158
159         DBG("device %p", device);
160
161         /* Power request timedout, reset power pending state. */
162         device->pending_timeout = 0;
163         device->powered_pending = PENDING_NONE;
164
165         return FALSE;
166 }
167
168 int __connman_device_enable(struct connman_device *device)
169 {
170         int err;
171
172         DBG("device %p", device);
173
174         if (!device->driver || !device->driver->enable)
175                 return -EOPNOTSUPP;
176
177         /* There is an ongoing power disable request. */
178         if (device->powered_pending == PENDING_DISABLE)
179                 return -EBUSY;
180
181         if (device->powered_pending == PENDING_ENABLE)
182                 return -EALREADY;
183
184         if (device->powered_pending == PENDING_NONE && device->powered)
185                 return -EALREADY;
186
187         if (device->index > 0) {
188                 err = connman_inet_ifup(device->index);
189                 if (err < 0 && err != -EALREADY)
190                         return err;
191         }
192
193         device->powered_pending = PENDING_ENABLE;
194
195         err = device->driver->enable(device);
196         /*
197          * device gets enabled right away.
198          * Invoke the callback
199          */
200         if (err == 0) {
201                 connman_device_set_powered(device, true);
202                 goto done;
203         }
204
205         if (err == -EALREADY) {
206                 /* If device is already powered, but connman is not updated */
207                 connman_device_set_powered(device, true);
208                 goto done;
209         }
210         /*
211          * if err == -EINPROGRESS, then the DBus call to the respective daemon
212          * was successful. We set a 4 sec timeout so if the daemon never
213          * returns a reply, we would reset the pending request.
214          */
215         if (err == -EINPROGRESS)
216                 device->pending_timeout = g_timeout_add_seconds(4,
217                                         device_pending_reset, device);
218 done:
219         return err;
220 }
221
222 int __connman_device_disable(struct connman_device *device)
223 {
224         int err;
225
226         DBG("device %p", device);
227
228         /* Ongoing power enable request */
229         if (device->powered_pending == PENDING_ENABLE)
230                 return -EBUSY;
231
232         if (device->powered_pending == PENDING_DISABLE)
233                 return -EALREADY;
234
235         if (device->powered_pending == PENDING_NONE && !device->powered)
236                 return -EALREADY;
237
238         device->powered_pending = PENDING_DISABLE;
239
240         if (device->network) {
241                 struct connman_service *service =
242                         connman_service_lookup_from_network(device->network);
243
244                 if (service)
245                         __connman_service_disconnect(service);
246                 else
247                         connman_network_set_connected(device->network, false);
248         }
249
250         if (!device->driver || !device->driver->disable)
251                 return -EOPNOTSUPP;
252
253         err = device->driver->disable(device);
254         if (err == 0 || err == -EALREADY) {
255                 connman_device_set_powered(device, false);
256                 goto done;
257         }
258
259         if (err == -EINPROGRESS)
260                 device->pending_timeout = g_timeout_add_seconds(4,
261                                         device_pending_reset, device);
262 done:
263         return err;
264 }
265
266 static void probe_driver(struct connman_device_driver *driver)
267 {
268         GSList *list;
269
270         DBG("driver %p name %s", driver, driver->name);
271
272         for (list = device_list; list; list = list->next) {
273                 struct connman_device *device = list->data;
274
275                 if (device->driver)
276                         continue;
277
278                 if (driver->type != device->type)
279                         continue;
280
281                 if (driver->probe(device) < 0)
282                         continue;
283
284                 device->driver = driver;
285
286                 __connman_technology_add_device(device);
287         }
288 }
289
290 static void remove_device(struct connman_device *device)
291 {
292         DBG("device %p", device);
293
294         __connman_device_disable(device);
295
296         __connman_technology_remove_device(device);
297
298         if (device->driver->remove)
299                 device->driver->remove(device);
300
301         device->driver = NULL;
302 }
303
304 static void remove_driver(struct connman_device_driver *driver)
305 {
306         GSList *list;
307
308         DBG("driver %p name %s", driver, driver->name);
309
310         for (list = device_list; list; list = list->next) {
311                 struct connman_device *device = list->data;
312
313                 if (device->driver == driver)
314                         remove_device(device);
315         }
316 }
317
318 bool __connman_device_has_driver(struct connman_device *device)
319 {
320         if (!device || !device->driver)
321                 return false;
322
323         return true;
324 }
325
326 static GSList *driver_list = NULL;
327
328 static gint compare_priority(gconstpointer a, gconstpointer b)
329 {
330         const struct connman_device_driver *driver1 = a;
331         const struct connman_device_driver *driver2 = b;
332
333         return driver2->priority - driver1->priority;
334 }
335
336 /**
337  * connman_device_driver_register:
338  * @driver: device driver definition
339  *
340  * Register a new device driver
341  *
342  * Returns: %0 on success
343  */
344 int connman_device_driver_register(struct connman_device_driver *driver)
345 {
346         DBG("driver %p name %s", driver, driver->name);
347
348         driver_list = g_slist_insert_sorted(driver_list, driver,
349                                                         compare_priority);
350         probe_driver(driver);
351
352         return 0;
353 }
354
355 /**
356  * connman_device_driver_unregister:
357  * @driver: device driver definition
358  *
359  * Remove a previously registered device driver
360  */
361 void connman_device_driver_unregister(struct connman_device_driver *driver)
362 {
363         DBG("driver %p name %s", driver, driver->name);
364
365         driver_list = g_slist_remove(driver_list, driver);
366
367         remove_driver(driver);
368 }
369
370 static void free_network(gpointer data)
371 {
372         struct connman_network *network = data;
373
374         DBG("network %p", network);
375
376         __connman_network_set_device(network, NULL);
377
378         connman_network_unref(network);
379 }
380
381 static void device_destruct(struct connman_device *device)
382 {
383         DBG("device %p name %s", device, device->name);
384
385         clear_pending_trigger(device);
386
387         g_hash_table_destroy(device->networks);
388         device->networks = NULL;
389
390         g_free(device->ident);
391         g_free(device->node);
392         g_free(device->name);
393         g_free(device->address);
394         g_free(device->interface);
395         g_free(device->path);
396
397         g_free(device->last_network);
398
399         g_free(device);
400 }
401
402 /**
403  * connman_device_create:
404  * @node: device node name (for example an address)
405  * @type: device type
406  *
407  * Allocate a new device of given #type and assign the #node name to it.
408  *
409  * Returns: a newly-allocated #connman_device structure
410  */
411 struct connman_device *connman_device_create(const char *node,
412                                                 enum connman_device_type type)
413 {
414         struct connman_device *device;
415
416         DBG("node %s type %d", node, type);
417
418         device = g_try_new0(struct connman_device, 1);
419         if (!device)
420                 return NULL;
421
422         DBG("device %p", device);
423
424         device->refcount = 1;
425
426         device->type = type;
427         device->name = g_strdup(type2description(device->type));
428
429         device->networks = g_hash_table_new_full(g_str_hash, g_str_equal,
430                                                 g_free, free_network);
431
432         device_list = g_slist_prepend(device_list, device);
433
434         return device;
435 }
436
437 /**
438  * connman_device_ref:
439  * @device: device structure
440  *
441  * Increase reference counter of device
442  */
443 struct connman_device *connman_device_ref_debug(struct connman_device *device,
444                                 const char *file, int line, const char *caller)
445 {
446         DBG("%p ref %d by %s:%d:%s()", device, device->refcount + 1,
447                 file, line, caller);
448
449         __sync_fetch_and_add(&device->refcount, 1);
450
451         return device;
452 }
453
454 /**
455  * connman_device_unref:
456  * @device: device structure
457  *
458  * Decrease reference counter of device
459  */
460 void connman_device_unref_debug(struct connman_device *device,
461                                 const char *file, int line, const char *caller)
462 {
463         DBG("%p ref %d by %s:%d:%s()", device, device->refcount - 1,
464                 file, line, caller);
465
466         if (__sync_fetch_and_sub(&device->refcount, 1) != 1)
467                 return;
468
469         if (device->driver) {
470                 device->driver->remove(device);
471                 device->driver = NULL;
472         }
473
474         device_list = g_slist_remove(device_list, device);
475
476         device_destruct(device);
477 }
478
479 const char *__connman_device_get_type(struct connman_device *device)
480 {
481         return type2string(device->type);
482 }
483
484 /**
485  * connman_device_get_type:
486  * @device: device structure
487  *
488  * Get type of device
489  */
490 enum connman_device_type connman_device_get_type(struct connman_device *device)
491 {
492         return device->type;
493 }
494
495 /**
496  * connman_device_set_index:
497  * @device: device structure
498  * @index: index number
499  *
500  * Set index number of device
501  */
502 void connman_device_set_index(struct connman_device *device, int index)
503 {
504         device->index = index;
505 }
506
507 /**
508  * connman_device_get_index:
509  * @device: device structure
510  *
511  * Get index number of device
512  */
513 int connman_device_get_index(struct connman_device *device)
514 {
515         return device->index;
516 }
517
518 /**
519  * connman_device_set_interface:
520  * @device: device structure
521  * @interface: interface name
522  *
523  * Set interface name of device
524  */
525 void connman_device_set_interface(struct connman_device *device,
526                                                 const char *interface)
527 {
528         g_free(device->interface);
529         device->interface = g_strdup(interface);
530
531         if (!device->name) {
532                 const char *str = type2description(device->type);
533                 if (str && device->interface)
534                         device->name = g_strdup_printf("%s (%s)", str,
535                                                         device->interface);
536         }
537 }
538
539 /**
540  * connman_device_set_ident:
541  * @device: device structure
542  * @ident: unique identifier
543  *
544  * Set unique identifier of device
545  */
546 void connman_device_set_ident(struct connman_device *device,
547                                                         const char *ident)
548 {
549         g_free(device->ident);
550         device->ident = g_strdup(ident);
551 }
552
553 const char *connman_device_get_ident(struct connman_device *device)
554 {
555         return device->ident;
556 }
557
558 /**
559  * connman_device_set_powered:
560  * @device: device structure
561  * @powered: powered state
562  *
563  * Change power state of device
564  */
565 int connman_device_set_powered(struct connman_device *device,
566                                                 bool powered)
567 {
568         enum connman_service_type type;
569
570         DBG("device %p powered %d", device, powered);
571
572         if (device->powered == powered)
573                 return -EALREADY;
574
575         clear_pending_trigger(device);
576
577         device->powered_pending = PENDING_NONE;
578
579         device->powered = powered;
580
581         type = __connman_device_get_service_type(device);
582
583         if (!device->powered) {
584                 __connman_technology_disabled(type);
585                 return 0;
586         }
587
588         __connman_technology_enabled(type);
589
590         device->scanning = false;
591
592         if (device->driver && device->driver->scan)
593                 device->driver->scan(CONNMAN_SERVICE_TYPE_UNKNOWN, device,
594                                         NULL, 0, NULL, NULL, NULL, NULL);
595
596         return 0;
597 }
598
599 bool connman_device_get_powered(struct connman_device *device)
600 {
601         return device->powered;
602 }
603
604 static int device_scan(enum connman_service_type type,
605                                 struct connman_device *device)
606 {
607         if (!device->driver || !device->driver->scan)
608                 return -EOPNOTSUPP;
609
610         if (!device->powered)
611                 return -ENOLINK;
612
613         return device->driver->scan(type, device, NULL, 0,
614                                         NULL, NULL, NULL, NULL);
615 }
616
617 int __connman_device_disconnect(struct connman_device *device)
618 {
619         GHashTableIter iter;
620         gpointer key, value;
621
622         DBG("device %p", device);
623
624         g_hash_table_iter_init(&iter, device->networks);
625
626         while (g_hash_table_iter_next(&iter, &key, &value)) {
627                 struct connman_network *network = value;
628
629                 if (connman_network_get_connecting(network)) {
630                         /*
631                          * Skip network in the process of connecting.
632                          * This is a workaround for WiFi networks serviced
633                          * by the supplicant plugin that hold a reference
634                          * to the network.  If we disconnect the network
635                          * here then the referenced object will not be
636                          * registered and usage (like launching DHCP client)
637                          * will fail.  There is nothing to be gained by
638                          * removing the network here anyway.
639                          */
640                         connman_warn("Skipping disconnect of %s, network is connecting.",
641                                 connman_network_get_identifier(network));
642                         continue;
643                 }
644
645                 __connman_network_disconnect(network);
646         }
647
648         return 0;
649 }
650
651 int connman_device_reconnect_service(struct connman_device *device)
652 {
653         DBG("device %p", device);
654
655         __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
656
657         return 0;
658 }
659
660 static void mark_network_available(gpointer key, gpointer value,
661                                                         gpointer user_data)
662 {
663         struct connman_network *network = value;
664
665         connman_network_set_available(network, true);
666 }
667
668 static void mark_network_unavailable(gpointer key, gpointer value,
669                                                         gpointer user_data)
670 {
671         struct connman_network *network = value;
672
673         if (connman_network_get_connected(network) ||
674                         connman_network_get_connecting(network))
675                 return;
676
677         connman_network_set_available(network, false);
678 }
679
680 static gboolean remove_unavailable_network(gpointer key, gpointer value,
681                                                         gpointer user_data)
682 {
683         struct connman_network *network = value;
684
685         if (connman_network_get_connected(network))
686                 return FALSE;
687
688         if (connman_network_get_available(network))
689                 return FALSE;
690
691         return TRUE;
692 }
693
694 void __connman_device_cleanup_networks(struct connman_device *device)
695 {
696         g_hash_table_foreach_remove(device->networks,
697                                         remove_unavailable_network, NULL);
698 }
699
700 bool connman_device_get_scanning(struct connman_device *device)
701 {
702         return device->scanning;
703 }
704
705 void connman_device_reset_scanning(struct connman_device *device)
706 {
707         g_hash_table_foreach(device->networks,
708                                 mark_network_available, NULL);
709 }
710
711 /**
712  * connman_device_set_scanning:
713  * @device: device structure
714  * @scanning: scanning state
715  *
716  * Change scanning state of device
717  */
718 int connman_device_set_scanning(struct connman_device *device,
719                                 enum connman_service_type type, bool scanning)
720 {
721         DBG("device %p scanning %d", device, scanning);
722
723         if (!device->driver || !device->driver->scan)
724                 return -EINVAL;
725
726         if (device->scanning == scanning)
727                 return -EALREADY;
728
729         device->scanning = scanning;
730
731         if (scanning) {
732                 __connman_technology_scan_started(device);
733
734                 g_hash_table_foreach(device->networks,
735                                         mark_network_unavailable, NULL);
736
737                 return 0;
738         }
739
740         __connman_device_cleanup_networks(device);
741
742         __connman_technology_scan_stopped(device, type);
743
744         __connman_service_auto_connect(CONNMAN_SERVICE_CONNECT_REASON_AUTO);
745
746         return 0;
747 }
748
749 /**
750  * connman_device_set_string:
751  * @device: device structure
752  * @key: unique identifier
753  * @value: string value
754  *
755  * Set string value for specific key
756  */
757 int connman_device_set_string(struct connman_device *device,
758                                         const char *key, const char *value)
759 {
760         DBG("device %p key %s value %s", device, key, value);
761
762         if (g_str_equal(key, "Address")) {
763                 g_free(device->address);
764                 device->address = g_strdup(value);
765         } else if (g_str_equal(key, "Name")) {
766                 g_free(device->name);
767                 device->name = g_strdup(value);
768         } else if (g_str_equal(key, "Node")) {
769                 g_free(device->node);
770                 device->node = g_strdup(value);
771         } else if (g_str_equal(key, "Path")) {
772                 g_free(device->path);
773                 device->path = g_strdup(value);
774         } else {
775                 return -EINVAL;
776         }
777
778         return 0;
779 }
780
781 /**
782  * connman_device_get_string:
783  * @device: device structure
784  * @key: unique identifier
785  *
786  * Get string value for specific key
787  */
788 const char *connman_device_get_string(struct connman_device *device,
789                                                         const char *key)
790 {
791         DBG("device %p key %s", device, key);
792
793         if (g_str_equal(key, "Address"))
794                 return device->address;
795         else if (g_str_equal(key, "Name"))
796                 return device->name;
797         else if (g_str_equal(key, "Node"))
798                 return device->node;
799         else if (g_str_equal(key, "Interface"))
800                 return device->interface;
801         else if (g_str_equal(key, "Path"))
802                 return device->path;
803
804         return NULL;
805 }
806
807 /**
808  * connman_device_add_network:
809  * @device: device structure
810  * @network: network structure
811  *
812  * Add new network to the device
813  */
814 int connman_device_add_network(struct connman_device *device,
815                                         struct connman_network *network)
816 {
817         const char *identifier = connman_network_get_identifier(network);
818
819         DBG("device %p network %p", device, network);
820
821         if (!identifier)
822                 return -EINVAL;
823
824         connman_network_ref(network);
825
826         __connman_network_set_device(network, device);
827
828         g_hash_table_replace(device->networks, g_strdup(identifier),
829                                                                 network);
830
831         return 0;
832 }
833
834 /**
835  * connman_device_get_network:
836  * @device: device structure
837  * @identifier: network identifier
838  *
839  * Get network for given identifier
840  */
841 struct connman_network *connman_device_get_network(struct connman_device *device,
842                                                         const char *identifier)
843 {
844         DBG("device %p identifier %s", device, identifier);
845
846         return g_hash_table_lookup(device->networks, identifier);
847 }
848
849 /**
850  * connman_device_remove_network:
851  * @device: device structure
852  * @identifier: network identifier
853  *
854  * Remove network for given identifier
855  */
856 int connman_device_remove_network(struct connman_device *device,
857                                                 struct connman_network *network)
858 {
859         const char *identifier;
860
861         DBG("device %p network %p", device, network);
862
863         if (!network)
864                 return 0;
865
866         identifier = connman_network_get_identifier(network);
867         g_hash_table_remove(device->networks, identifier);
868
869         return 0;
870 }
871
872 void __connman_device_set_network(struct connman_device *device,
873                                         struct connman_network *network)
874 {
875         const char *name;
876
877         if (!device)
878                 return;
879
880         if (device->network == network)
881                 return;
882
883         if (network) {
884                 name = connman_network_get_string(network, "Name");
885                 g_free(device->last_network);
886                 device->last_network = g_strdup(name);
887
888                 device->network = network;
889         } else {
890                 g_free(device->last_network);
891                 device->last_network = NULL;
892
893                 device->network = NULL;
894         }
895 }
896
897 static bool match_driver(struct connman_device *device,
898                                         struct connman_device_driver *driver)
899 {
900         if (device->type == driver->type ||
901                         driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
902                 return true;
903
904         return false;
905 }
906
907 /**
908  * connman_device_register:
909  * @device: device structure
910  *
911  * Register device with the system
912  */
913 int connman_device_register(struct connman_device *device)
914 {
915         GSList *list;
916
917         DBG("device %p name %s", device, device->name);
918
919         if (device->driver)
920                 return -EALREADY;
921
922         for (list = driver_list; list; list = list->next) {
923                 struct connman_device_driver *driver = list->data;
924
925                 if (!match_driver(device, driver))
926                         continue;
927
928                 DBG("driver %p name %s", driver, driver->name);
929
930                 if (driver->probe(device) == 0) {
931                         device->driver = driver;
932                         break;
933                 }
934         }
935
936         if (!device->driver)
937                 return 0;
938
939         return __connman_technology_add_device(device);
940 }
941
942 /**
943  * connman_device_unregister:
944  * @device: device structure
945  *
946  * Unregister device with the system
947  */
948 void connman_device_unregister(struct connman_device *device)
949 {
950         DBG("device %p name %s", device, device->name);
951
952         if (!device->driver)
953                 return;
954
955         remove_device(device);
956 }
957
958 /**
959  * connman_device_get_data:
960  * @device: device structure
961  *
962  * Get private device data pointer
963  */
964 void *connman_device_get_data(struct connman_device *device)
965 {
966         return device->driver_data;
967 }
968
969 /**
970  * connman_device_set_data:
971  * @device: device structure
972  * @data: data pointer
973  *
974  * Set private device data pointer
975  */
976 void connman_device_set_data(struct connman_device *device, void *data)
977 {
978         device->driver_data = data;
979 }
980
981 struct connman_device *__connman_device_find_device(
982                                 enum connman_service_type type)
983 {
984         GSList *list;
985
986         for (list = device_list; list; list = list->next) {
987                 struct connman_device *device = list->data;
988                 enum connman_service_type service_type =
989                         __connman_device_get_service_type(device);
990
991                 if (service_type != type)
992                         continue;
993
994                 return device;
995         }
996
997         return NULL;
998 }
999
1000 struct connman_device *connman_device_find_by_index(int index)
1001 {
1002         GSList *list;
1003
1004         for (list = device_list; list; list = list->next) {
1005                 struct connman_device *device = list->data;
1006                 if (device->index == index)
1007                         return device;
1008         }
1009
1010         return NULL;
1011 }
1012
1013 /**
1014  * connman_device_set_regdom
1015  * @device: device structure
1016  * @alpha2: string representing regulatory domain
1017  *
1018  * Set regulatory domain on device basis
1019  */
1020 int connman_device_set_regdom(struct connman_device *device,
1021                                                 const char *alpha2)
1022 {
1023         if (!device->driver || !device->driver->set_regdom)
1024                 return -ENOTSUP;
1025
1026         if (!device->powered)
1027                 return -EINVAL;
1028
1029         return device->driver->set_regdom(device, alpha2);
1030 }
1031
1032 /**
1033  * connman_device_regdom_notify
1034  * @device: device structure
1035  * @alpha2: string representing regulatory domain
1036  *
1037  * Notify on setting regulatory domain on device basis
1038  */
1039 void connman_device_regdom_notify(struct connman_device *device,
1040                                         int result, const char *alpha2)
1041 {
1042         __connman_technology_notify_regdom_by_device(device, result, alpha2);
1043 }
1044
1045 #if defined TIZEN_EXT
1046 static int device_specific_scan(enum connman_service_type type,
1047                                 struct connman_device *device,
1048                                 int scan_type, GSList *specific_scan_list)
1049 {
1050         if (!device->driver || !device->driver->specific_scan)
1051                 return -EOPNOTSUPP;
1052
1053         if (!device->powered)
1054                 return -ENOLINK;
1055
1056         return device->driver->specific_scan(type, device, scan_type,
1057                         specific_scan_list, NULL);
1058 }
1059
1060 int __connman_device_request_specific_scan(enum connman_service_type type,
1061                                 int scan_type, GSList *specific_scan_list)
1062 {
1063         bool success = false;
1064         int last_err = -ENOSYS;
1065         GSList *list;
1066         int err;
1067
1068         switch (type) {
1069         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1070         case CONNMAN_SERVICE_TYPE_SYSTEM:
1071         case CONNMAN_SERVICE_TYPE_ETHERNET:
1072         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1073         case CONNMAN_SERVICE_TYPE_CELLULAR:
1074         case CONNMAN_SERVICE_TYPE_GPS:
1075         case CONNMAN_SERVICE_TYPE_VPN:
1076         case CONNMAN_SERVICE_TYPE_GADGET:
1077                 return -EOPNOTSUPP;
1078         case CONNMAN_SERVICE_TYPE_WIFI:
1079         case CONNMAN_SERVICE_TYPE_P2P:
1080                 break;
1081         }
1082
1083         for (list = device_list; list; list = list->next) {
1084                 struct connman_device *device = list->data;
1085                 enum connman_service_type service_type =
1086                         __connman_device_get_service_type(device);
1087
1088                 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
1089                         if (type == CONNMAN_SERVICE_TYPE_P2P) {
1090                                 if (service_type != CONNMAN_SERVICE_TYPE_WIFI)
1091                                         continue;
1092                         } else if (service_type != type)
1093                                 continue;
1094                 }
1095
1096                 err = device_specific_scan(type, device, scan_type, specific_scan_list);
1097                 if (err == 0 || err == -EINPROGRESS) {
1098                         success = true;
1099                 } else {
1100                         last_err = err;
1101                         DBG("device %p err %d", device, err);
1102                 }
1103         }
1104
1105         if (success)
1106                 return 0;
1107
1108         return last_err;
1109 }
1110 #endif
1111
1112 int __connman_device_request_scan(enum connman_service_type type)
1113 {
1114         bool success = false;
1115         int last_err = -ENOSYS;
1116         GSList *list;
1117         int err;
1118
1119         switch (type) {
1120         case CONNMAN_SERVICE_TYPE_UNKNOWN:
1121         case CONNMAN_SERVICE_TYPE_SYSTEM:
1122         case CONNMAN_SERVICE_TYPE_ETHERNET:
1123         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
1124         case CONNMAN_SERVICE_TYPE_CELLULAR:
1125         case CONNMAN_SERVICE_TYPE_GPS:
1126         case CONNMAN_SERVICE_TYPE_VPN:
1127         case CONNMAN_SERVICE_TYPE_GADGET:
1128                 return -EOPNOTSUPP;
1129         case CONNMAN_SERVICE_TYPE_WIFI:
1130         case CONNMAN_SERVICE_TYPE_P2P:
1131                 break;
1132         }
1133
1134         for (list = device_list; list; list = list->next) {
1135                 struct connman_device *device = list->data;
1136                 enum connman_service_type service_type =
1137                         __connman_device_get_service_type(device);
1138
1139                 if (service_type != CONNMAN_SERVICE_TYPE_UNKNOWN) {
1140                         if (type == CONNMAN_SERVICE_TYPE_P2P) {
1141                                 if (service_type != CONNMAN_SERVICE_TYPE_WIFI)
1142                                         continue;
1143                         } else if (service_type != type)
1144                                 continue;
1145                 }
1146
1147                 err = device_scan(type, device);
1148 #if defined TIZEN_EXT
1149                 /* When Scan is already in progress then return Error so that
1150                  * wifi-manager can block the scan-done signal to be sent to
1151                  * application and start requested scan after scan already in progress
1152                  * is completed then notify to application about the scan event */
1153                 if (err == 0 || err == -EINPROGRESS) {
1154 #else
1155                 if (err == 0 || err == -EALREADY || err == -EINPROGRESS) {
1156 #endif
1157                         success = true;
1158                 } else {
1159                         last_err = err;
1160                         DBG("device %p err %d", device, err);
1161                 }
1162         }
1163
1164         if (success)
1165                 return 0;
1166
1167         return last_err;
1168 }
1169
1170 int __connman_device_request_hidden_scan(struct connman_device *device,
1171                                 const char *ssid, unsigned int ssid_len,
1172                                 const char *identity, const char *passphrase,
1173                                 const char *security, void *user_data)
1174 {
1175         DBG("device %p", device);
1176
1177         if (!device || !device->driver ||
1178                         !device->driver->scan)
1179                 return -EINVAL;
1180
1181         return device->driver->scan(CONNMAN_SERVICE_TYPE_UNKNOWN,
1182                                         device, ssid, ssid_len, identity,
1183                                         passphrase, security, user_data);
1184 }
1185
1186 #if defined TIZEN_EXT
1187 char *index2ident(int index, const char *prefix)
1188 #else
1189 static char *index2ident(int index, const char *prefix)
1190 #endif
1191 {
1192         struct ifreq ifr;
1193         struct ether_addr eth;
1194         char *str;
1195         int sk, err, len;
1196
1197         if (index < 0)
1198                 return NULL;
1199
1200         sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1201         if (sk < 0)
1202                 return NULL;
1203
1204         memset(&ifr, 0, sizeof(ifr));
1205         ifr.ifr_ifindex = index;
1206
1207         err = ioctl(sk, SIOCGIFNAME, &ifr);
1208
1209         if (err == 0)
1210                 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
1211
1212         close(sk);
1213
1214         if (err < 0)
1215                 return NULL;
1216
1217         len = prefix ? strlen(prefix) + 18 : 18;
1218
1219         str = g_malloc(len);
1220         if (!str)
1221                 return NULL;
1222
1223         memcpy(&eth, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
1224         snprintf(str, len, "%s%02x%02x%02x%02x%02x%02x",
1225                                                 prefix ? prefix : "",
1226                                                 eth.ether_addr_octet[0],
1227                                                 eth.ether_addr_octet[1],
1228                                                 eth.ether_addr_octet[2],
1229                                                 eth.ether_addr_octet[3],
1230                                                 eth.ether_addr_octet[4],
1231                                                 eth.ether_addr_octet[5]);
1232
1233         return str;
1234 }
1235
1236 #if defined TIZEN_EXT
1237 char *index2addr(int index)
1238 #else
1239 static char *index2addr(int index)
1240 #endif
1241 {
1242         struct ifreq ifr;
1243         struct ether_addr eth;
1244         char *str;
1245         int sk, err;
1246
1247         if (index < 0)
1248                 return NULL;
1249
1250         sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
1251         if (sk < 0)
1252                 return NULL;
1253
1254         memset(&ifr, 0, sizeof(ifr));
1255         ifr.ifr_ifindex = index;
1256
1257         err = ioctl(sk, SIOCGIFNAME, &ifr);
1258
1259         if (err == 0)
1260                 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
1261
1262         close(sk);
1263
1264         if (err < 0)
1265                 return NULL;
1266
1267         str = g_malloc(18);
1268         if (!str)
1269                 return NULL;
1270
1271         memcpy(&eth, &ifr.ifr_hwaddr.sa_data, sizeof(eth));
1272         snprintf(str, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
1273                                                 eth.ether_addr_octet[0],
1274                                                 eth.ether_addr_octet[1],
1275                                                 eth.ether_addr_octet[2],
1276                                                 eth.ether_addr_octet[3],
1277                                                 eth.ether_addr_octet[4],
1278                                                 eth.ether_addr_octet[5]);
1279
1280         return str;
1281 }
1282
1283 struct connman_device *connman_device_create_from_index(int index)
1284 {
1285         enum connman_device_type type;
1286         struct connman_device *device;
1287         char *devname, *ident = NULL;
1288         char *addr = NULL, *name = NULL;
1289
1290         if (index < 0)
1291                 return NULL;
1292
1293         devname = connman_inet_ifname(index);
1294         if (!devname)
1295                 return NULL;
1296
1297         if (__connman_device_isfiltered(devname)) {
1298                 connman_info("Ignoring interface %s (filtered)", devname);
1299                 g_free(devname);
1300                 return NULL;
1301         }
1302
1303         type = __connman_rtnl_get_device_type(index);
1304
1305         switch (type) {
1306         case CONNMAN_DEVICE_TYPE_UNKNOWN:
1307                 connman_info("Ignoring interface %s (type unknown)", devname);
1308                 g_free(devname);
1309                 return NULL;
1310         case CONNMAN_DEVICE_TYPE_ETHERNET:
1311         case CONNMAN_DEVICE_TYPE_GADGET:
1312         case CONNMAN_DEVICE_TYPE_WIFI:
1313                 name = index2ident(index, "");
1314                 addr = index2addr(index);
1315                 break;
1316         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1317         case CONNMAN_DEVICE_TYPE_CELLULAR:
1318         case CONNMAN_DEVICE_TYPE_GPS:
1319         case CONNMAN_DEVICE_TYPE_VENDOR:
1320                 name = g_strdup(devname);
1321                 break;
1322         }
1323
1324         device = connman_device_create(name, type);
1325         if (!device)
1326                 goto done;
1327
1328         switch (type) {
1329         case CONNMAN_DEVICE_TYPE_UNKNOWN:
1330         case CONNMAN_DEVICE_TYPE_VENDOR:
1331         case CONNMAN_DEVICE_TYPE_GPS:
1332                 break;
1333         case CONNMAN_DEVICE_TYPE_ETHERNET:
1334         case CONNMAN_DEVICE_TYPE_GADGET:
1335                 ident = index2ident(index, NULL);
1336                 break;
1337         case CONNMAN_DEVICE_TYPE_WIFI:
1338                 ident = index2ident(index, NULL);
1339                 break;
1340         case CONNMAN_DEVICE_TYPE_BLUETOOTH:
1341                 break;
1342         case CONNMAN_DEVICE_TYPE_CELLULAR:
1343                 ident = index2ident(index, NULL);
1344                 break;
1345         }
1346
1347         connman_device_set_index(device, index);
1348         connman_device_set_interface(device, devname);
1349
1350         if (ident) {
1351                 connman_device_set_ident(device, ident);
1352                 g_free(ident);
1353         }
1354
1355         connman_device_set_string(device, "Address", addr);
1356
1357 done:
1358         g_free(devname);
1359         g_free(name);
1360         g_free(addr);
1361
1362         return device;
1363 }
1364
1365 bool __connman_device_isfiltered(const char *devname)
1366 {
1367         char **pattern;
1368         char **blacklisted_interfaces;
1369         bool match;
1370
1371         if (!device_filter)
1372                 goto nodevice;
1373
1374         for (pattern = device_filter, match = false; *pattern; pattern++) {
1375                 if (g_pattern_match_simple(*pattern, devname)) {
1376                         match = true;
1377                         break;
1378                 }
1379         }
1380
1381         if (!match) {
1382                 DBG("ignoring device %s (match)", devname);
1383                 return true;
1384         }
1385
1386 nodevice:
1387         if (g_pattern_match_simple("dummy*", devname)) {
1388                 DBG("ignoring dummy networking devices");
1389                 return true;
1390         }
1391
1392         if (!nodevice_filter)
1393                 goto list;
1394
1395         for (pattern = nodevice_filter; *pattern; pattern++) {
1396                 if (g_pattern_match_simple(*pattern, devname)) {
1397                         DBG("ignoring device %s (no match)", devname);
1398                         return true;
1399                 }
1400         }
1401
1402 list:
1403         if (__connman_inet_isrootnfs_device(devname)) {
1404                 DBG("ignoring device %s (rootnfs)", devname);
1405                 return true;
1406         }
1407
1408         blacklisted_interfaces =
1409                 connman_setting_get_string_list("NetworkInterfaceBlacklist");
1410         if (!blacklisted_interfaces)
1411                 return false;
1412
1413         for (pattern = blacklisted_interfaces; *pattern; pattern++) {
1414                 if (g_str_has_prefix(devname, *pattern)) {
1415                         DBG("ignoring device %s (blacklist)", devname);
1416                         return true;
1417                 }
1418         }
1419
1420         return false;
1421 }
1422
1423 static void cleanup_devices(void)
1424 {
1425         /*
1426          * Check what interfaces are currently up and if connman is
1427          * suppose to handle the interface, then cleanup the mess
1428          * related to that interface. There might be weird routes etc
1429          * that are related to that interface and that might confuse
1430          * connmand. So in this case we just turn the interface down
1431          * so that kernel removes routes/addresses automatically and
1432          * then proceed the startup.
1433          *
1434          * Note that this cleanup must be done before rtnl/detect code
1435          * has activated interface watches.
1436          */
1437
1438         char **interfaces;
1439         int i;
1440
1441         interfaces = __connman_inet_get_running_interfaces();
1442
1443         if (!interfaces)
1444                 return;
1445
1446         for (i = 0; interfaces[i]; i++) {
1447                 bool filtered;
1448                 int index;
1449                 struct sockaddr_in sin_addr, sin_mask;
1450
1451                 filtered = __connman_device_isfiltered(interfaces[i]);
1452                 if (filtered)
1453                         continue;
1454
1455                 index = connman_inet_ifindex(interfaces[i]);
1456                 if (index < 0)
1457                         continue;
1458
1459                 if (!__connman_inet_get_address_netmask(index, &sin_addr,
1460                                                         &sin_mask)) {
1461                         char *address = g_strdup(inet_ntoa(sin_addr.sin_addr));
1462                         char *netmask = g_strdup(inet_ntoa(sin_mask.sin_addr));
1463
1464                         if (__connman_config_address_provisioned(address,
1465                                                                 netmask)) {
1466                                 DBG("Skip %s which is already provisioned "
1467                                         "with %s/%s", interfaces[i], address,
1468                                         netmask);
1469                                 g_free(address);
1470                                 g_free(netmask);
1471                                 continue;
1472                         }
1473
1474                         g_free(address);
1475                         g_free(netmask);
1476                 }
1477
1478                 DBG("cleaning up %s index %d", interfaces[i], index);
1479
1480 #if defined TIZEN_EXT
1481                 if (strcmp(interfaces[i], "wlan0") != 0)
1482 #endif
1483                 connman_inet_ifdown(index);
1484
1485                 /*
1486                  * ConnMan will turn the interface UP automatically so
1487                  * no need to do it here.
1488                  */
1489         }
1490
1491         g_strfreev(interfaces);
1492 }
1493
1494 int __connman_device_init(const char *device, const char *nodevice)
1495 {
1496         DBG("");
1497
1498         if (device)
1499                 device_filter = g_strsplit(device, ",", -1);
1500
1501         if (nodevice)
1502                 nodevice_filter = g_strsplit(nodevice, ",", -1);
1503
1504         cleanup_devices();
1505
1506         return 0;
1507 }
1508
1509 void __connman_device_cleanup(void)
1510 {
1511         DBG("");
1512
1513         g_strfreev(nodevice_filter);
1514         g_strfreev(device_filter);
1515 }