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