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