Added support of Multiple same SSIDs including band steering.
[platform/upstream/connman.git] / plugins / loopback.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 <stdio.h>
27
28 #include <errno.h>
29 #include <unistd.h>
30 #include <limits.h>
31 #include <string.h>
32 #include <sys/ioctl.h>
33 #include <sys/inotify.h>
34 #include <sys/socket.h>
35 #include <arpa/inet.h>
36 #include <net/if.h>
37
38 #include <glib.h>
39 #include <glib/gprintf.h>
40
41
42 #define CONNMAN_API_SUBJECT_TO_CHANGE
43 #include <connman/plugin.h>
44 #include <connman/utsname.h>
45 #include <connman/log.h>
46 #include <connman/inet.h>
47
48 static in_addr_t loopback_address;
49 static in_addr_t loopback_netmask;
50
51 static char system_hostname[HOST_NAME_MAX + 1];
52
53 static void create_hostname(void)
54 {
55         const char *name = "localhost";
56
57         if (sethostname(name, strlen(name)) < 0)
58                 connman_error("Failed to set hostname to %s", name);
59
60         strncpy(system_hostname, name, HOST_NAME_MAX);
61 }
62
63 static int setup_hostname(void)
64 {
65         char name[HOST_NAME_MAX + 1];
66
67         memset(system_hostname, 0, sizeof(system_hostname));
68
69 #if defined TIZEN_EXT
70                 FILE *fp = NULL;
71 #define WIFI_MAC "/opt/etc/.mac.info"
72                 {
73                         char* rv = 0;
74                         gchar* dev_id = "TIZEN";
75                         char wifi_mac[HOST_NAME_MAX + 1];
76
77                         fp = fopen(WIFI_MAC, "r");
78                         if(!fp){
79                                 connman_error("Failed to get current hostname");
80                                 strncpy(system_hostname, dev_id, strlen(dev_id));
81                                 goto host_name_end;
82                         }
83
84                         rv = fgets(wifi_mac, HOST_NAME_MAX, fp);
85                         if(!rv){
86                                 connman_error("Failed to get current hostname");
87                                 strncpy(system_hostname, dev_id, strlen(dev_id));
88                                 fclose(fp);
89                                 goto host_name_end;
90                         }
91
92                         dev_id = g_base64_encode((const guchar *)wifi_mac, strlen(wifi_mac));
93                         g_sprintf(system_hostname, "TIZEN-%s", dev_id);
94                         g_free(dev_id);
95                         fclose(fp);
96                 }
97
98         host_name_end :
99 #else
100                 if (gethostname(system_hostname, HOST_NAME_MAX) < 0) {
101                         connman_error("Failed to get current hostname");
102                         return -EIO;
103                 }
104 #endif
105                 if (strlen(system_hostname) > 0 &&
106                                         strcmp(system_hostname, "(none)") != 0)
107                         connman_info("System hostname is %s", system_hostname);
108                 else
109                         create_hostname();
110
111         memset(name, 0, sizeof(name));
112
113         if (getdomainname(name, HOST_NAME_MAX) < 0) {
114                 connman_error("Failed to get current domainname");
115                 return -EIO;
116         }
117
118         if (strlen(name) > 0 && strcmp(name, "(none)") != 0)
119                 connman_info("System domainname is %s", name);
120
121         return 0;
122 }
123
124 static bool valid_loopback(int sk, struct ifreq *ifr)
125 {
126         struct sockaddr_in *addr;
127         int err;
128         char buf[INET_ADDRSTRLEN];
129
130         /* It is possible to end up in situations in which the
131          * loopback interface is up but has no valid address. In that
132          * case, we expect EADDRNOTAVAIL and should return FALSE.
133          */
134
135         err = ioctl(sk, SIOCGIFADDR, ifr);
136         if (err < 0) {
137                 err = -errno;
138                 connman_error("Getting address failed (%s)", strerror(-err));
139                 return err != -EADDRNOTAVAIL ? TRUE : FALSE;
140         }
141
142         addr = (struct sockaddr_in *) &ifr->ifr_addr;
143         if (addr->sin_addr.s_addr != loopback_address) {
144                 connman_warn("Invalid loopback address %s",
145                         inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
146                 return false;
147         }
148
149         err = ioctl(sk, SIOCGIFNETMASK, ifr);
150         if (err < 0) {
151                 err = -errno;
152                 connman_error("Getting netmask failed (%s)", strerror(-err));
153                 return true;
154         }
155
156         addr = (struct sockaddr_in *) &ifr->ifr_netmask;
157         if (addr->sin_addr.s_addr != loopback_netmask) {
158                 connman_warn("Invalid loopback netmask %s",
159                         inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)));
160                 return false;
161         }
162
163         return true;
164 }
165
166 static int setup_loopback(void)
167 {
168         struct ifreq ifr;
169         struct sockaddr_in addr;
170         int sk, err;
171
172         sk = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
173         if (sk < 0)
174                 return -errno;
175
176         memset(&ifr, 0, sizeof(ifr));
177         strcpy(ifr.ifr_name, "lo");
178
179         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
180                 err = -errno;
181                 goto done;
182         }
183
184         if (ifr.ifr_flags & IFF_UP) {
185                 connman_info("Checking loopback interface settings");
186                 if (valid_loopback(sk, &ifr)) {
187                         err = -EALREADY;
188                         goto done;
189                 }
190
191                 connman_warn("Correcting wrong loopback settings");
192         }
193
194         memset(&addr, 0, sizeof(addr));
195         addr.sin_family = AF_INET;
196         addr.sin_addr.s_addr = loopback_address;
197         memcpy(&ifr.ifr_addr, &addr, sizeof(ifr.ifr_addr));
198
199         err = ioctl(sk, SIOCSIFADDR, &ifr);
200         if (err < 0) {
201                 err = -errno;
202                 connman_error("Setting address failed (%s)", strerror(-err));
203                 goto done;
204         }
205
206         memset(&addr, 0, sizeof(addr));
207         addr.sin_family = AF_INET;
208         addr.sin_addr.s_addr = loopback_netmask;
209         memcpy(&ifr.ifr_netmask, &addr, sizeof(ifr.ifr_netmask));
210
211         err = ioctl(sk, SIOCSIFNETMASK, &ifr);
212         if (err < 0) {
213                 err = -errno;
214                 connman_error("Setting netmask failed (%s)", strerror(-err));
215                 goto done;
216         }
217
218         if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
219                 err = -errno;
220                 goto done;
221         }
222
223         ifr.ifr_flags |= IFF_UP;
224
225         if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
226                 err = -errno;
227                 connman_error("Activating loopback interface failed (%s)",
228                                                         strerror(-err));
229                 goto done;
230         }
231
232 done:
233         close(sk);
234
235         return err;
236 }
237
238 static const char *loopback_get_hostname(void)
239 {
240         return system_hostname;
241 }
242
243 static int loopback_set_hostname(const char *hostname)
244 {
245         const char *ptr;
246         int err, len;
247
248         if (g_strcmp0(hostname, "<hostname>") == 0)
249                 return 0;
250
251         len = strlen(hostname);
252
253         if (!connman_inet_check_hostname(hostname, len))
254                 return -EINVAL;
255
256         if ((ptr = strstr(hostname, ".")))
257                 len = ptr - hostname;
258
259         if (sethostname(hostname, len) < 0) {
260                 err = -errno;
261                 connman_error("Failed to set hostname to %s", hostname);
262                 return err;
263         }
264
265         connman_info("Setting hostname to %s", hostname);
266
267         return 0;
268 }
269
270 static int loopback_set_domainname(const char *domainname)
271 {
272         int err, len;
273
274         len = strlen(domainname);
275
276         if (!connman_inet_check_hostname(domainname, len))
277                 return -EINVAL;
278
279         if (setdomainname(domainname, len) < 0) {
280                 err = -errno;
281                 connman_error("Failed to set domainname to %s", domainname);
282                 return err;
283         }
284
285         connman_info("Setting domainname to %s", domainname);
286
287         return 0;
288 }
289
290 static struct connman_utsname_driver loopback_driver = {
291         .name           = "loopback",
292         .get_hostname   = loopback_get_hostname,
293         .set_hostname   = loopback_set_hostname,
294         .set_domainname = loopback_set_domainname,
295 };
296
297 static int loopback_init(void)
298 {
299         loopback_address = inet_addr("127.0.0.1");
300         loopback_netmask = inet_addr("255.0.0.0");
301
302         setup_loopback();
303
304         setup_hostname();
305
306         connman_utsname_driver_register(&loopback_driver);
307
308         return 0;
309 }
310
311 static void loopback_exit(void)
312 {
313         connman_utsname_driver_unregister(&loopback_driver);
314 }
315
316 CONNMAN_PLUGIN_DEFINE(loopback, "Loopback device plugin", VERSION,
317                 CONNMAN_PLUGIN_PRIORITY_HIGH, loopback_init, loopback_exit)