wifi: Add support for autoscan request
[framework/connectivity/connman.git] / plugins / l2tp.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2010  BMW Car IT GmbH. All rights reserved.
6  *  Copyright (C) 2012  Intel Corporation. All rights reserved.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License version 2 as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33
34 #include <stdio.h>
35 #include <net/if.h>
36
37 #include <glib.h>
38
39 #define CONNMAN_API_SUBJECT_TO_CHANGE
40 #include <connman/plugin.h>
41 #include <connman/provider.h>
42 #include <connman/log.h>
43 #include <connman/task.h>
44 #include <connman/dbus.h>
45 #include <connman/inet.h>
46
47 #include "vpn.h"
48
49 #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
50
51 enum {
52         OPT_STRING = 1,
53         OPT_BOOL = 2,
54 };
55
56 enum {
57         OPT_ALL = 1,
58         OPT_L2G = 2,
59         OPT_L2  = 3,
60         OPT_PPPD = 4,
61 };
62
63 struct {
64         const char *cm_opt;
65         const char *pppd_opt;
66         int sub;
67         const char *vpn_default;
68         int type;
69 } pppd_options[] = {
70         { "L2TP.User", "name", OPT_ALL, NULL, OPT_STRING },
71         { "L2TP.BPS", "bps", OPT_L2, NULL, OPT_STRING },
72         { "L2TP.LengthBit", "length bit", OPT_L2, NULL, OPT_STRING },
73         { "L2TP.Challenge", "challenge", OPT_L2, NULL, OPT_STRING },
74         { "L2TP.DefaultRoute", "defaultroute", OPT_L2, NULL, OPT_STRING },
75         { "L2TP.FlowBit", "flow bit", OPT_L2, NULL, OPT_STRING },
76         { "L2TP.TunnelRWS", "tunnel rws", OPT_L2, NULL, OPT_STRING },
77         { "L2TP.Exclusive", "exclusive", OPT_L2, NULL, OPT_STRING },
78         { "L2TP.Autodial", "autodial", OPT_L2, "yes", OPT_STRING },
79         { "L2TP.Redial", "redial", OPT_L2, "yes", OPT_STRING },
80         { "L2TP.RedialTimeout", "redial timeout", OPT_L2, "10", OPT_STRING },
81         { "L2TP.MaxRedials", "max redials", OPT_L2, NULL, OPT_STRING },
82         { "L2TP.RequirePAP", "require pap", OPT_L2, "no", OPT_STRING },
83         { "L2TP.RequireCHAP", "require chap", OPT_L2, "yes", OPT_STRING },
84         { "L2TP.ReqAuth", "require authentication", OPT_L2, "no", OPT_STRING },
85         { "L2TP.AccessControl", "access control", OPT_L2G, "yes", OPT_STRING },
86         { "L2TP.AuthFile", "auth file", OPT_L2G, NULL, OPT_STRING },
87         { "L2TP.ForceUserSpace", "force userspace", OPT_L2G, NULL, OPT_STRING },
88         { "L2TP.ListenAddr", "listen-addr", OPT_L2G, NULL, OPT_STRING },
89         { "L2TP.Rand Source", "rand source", OPT_L2G, NULL, OPT_STRING },
90         { "L2TP.IPsecSaref", "ipsec saref", OPT_L2G, NULL, OPT_STRING },
91         { "L2TP.Port", "port", OPT_L2G, NULL, OPT_STRING },
92         { "L2TP.EchoFailure", "lcp-echo-failure", OPT_PPPD, "0", OPT_STRING },
93         { "L2TP.EchoInterval", "lcp-echo-interval", OPT_PPPD, "0", OPT_STRING },
94         { "L2TP.Debug", "debug", OPT_PPPD, NULL, OPT_STRING },
95         { "L2TP.RefuseEAP", "refuse-eap", OPT_PPPD, NULL, OPT_BOOL },
96         { "L2TP.RefusePAP", "refuse-pap", OPT_PPPD, NULL, OPT_BOOL },
97         { "L2TP.RefuseCHAP", "refuse-chap", OPT_PPPD, NULL, OPT_BOOL },
98         { "L2TP.RefuseMSCHAP", "refuse-mschap", OPT_PPPD, NULL, OPT_BOOL },
99         { "L2TP.RefuseMSCHAP2", "refuse-mschapv2", OPT_PPPD, NULL, OPT_BOOL },
100         { "L2TP.NoBSDComp", "nobsdcomp", OPT_PPPD, NULL, OPT_BOOL },
101         { "L2TP.NoPcomp", "nopcomp", OPT_PPPD, NULL, OPT_BOOL },
102         { "L2TP.UseAccomp", "accomp", OPT_PPPD, NULL, OPT_BOOL },
103         { "L2TP.NoDeflate", "nodeflatey", OPT_PPPD, NULL, OPT_BOOL },
104         { "L2TP.ReqMPPE", "require-mppe", OPT_PPPD, NULL, OPT_BOOL },
105         { "L2TP.ReqMPPE40", "require-mppe-40", OPT_PPPD, NULL, OPT_BOOL },
106         { "L2TP.ReqMPPE128", "require-mppe-128", OPT_PPPD, NULL, OPT_BOOL },
107         { "L2TP.ReqMPPEStateful", "mppe-stateful", OPT_PPPD, NULL, OPT_BOOL },
108         { "L2TP.NoVJ", "no-vj-comp", OPT_PPPD, NULL, OPT_BOOL },
109 };
110
111 static DBusConnection *connection;
112
113 static DBusMessage *l2tp_get_sec(struct connman_task *task,
114                         DBusMessage *msg, void *user_data)
115 {
116         const char *user, *passwd;
117         struct connman_provider *provider = user_data;
118
119         if (dbus_message_get_no_reply(msg) == FALSE) {
120                 DBusMessage *reply;
121
122                 user = connman_provider_get_string(provider, "L2TP.User");
123                 passwd = connman_provider_get_string(provider, "L2TP.Password");
124
125                 if (user == NULL || strlen(user) == 0 ||
126                                 passwd == NULL || strlen(passwd) == 0)
127                         return NULL;
128
129                 reply = dbus_message_new_method_return(msg);
130                 if (reply == NULL)
131                         return NULL;
132
133                 dbus_message_append_args(reply, DBUS_TYPE_STRING, &user,
134                                                 DBUS_TYPE_STRING, &passwd,
135                                                 DBUS_TYPE_INVALID);
136
137                 return reply;
138         }
139
140         return NULL;
141 }
142
143 static int l2tp_notify(DBusMessage *msg, struct connman_provider *provider)
144 {
145         DBusMessageIter iter, dict;
146         const char *reason, *key, *value;
147         char *addressv4 = NULL, *netmask = NULL, *gateway = NULL;
148         char *ifname = NULL, *nameservers = NULL;
149         struct connman_ipaddress *ipaddress = NULL;
150
151         dbus_message_iter_init(msg, &iter);
152
153         dbus_message_iter_get_basic(&iter, &reason);
154         dbus_message_iter_next(&iter);
155
156         if (!provider) {
157                 connman_error("No provider found");
158                 return VPN_STATE_FAILURE;
159         }
160
161         if (strcmp(reason, "auth failed") == 0)
162                 return VPN_STATE_AUTH_FAILURE;
163
164         if (strcmp(reason, "connect"))
165                 return VPN_STATE_DISCONNECT;
166
167         dbus_message_iter_recurse(&iter, &dict);
168
169         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
170                 DBusMessageIter entry;
171
172                 dbus_message_iter_recurse(&dict, &entry);
173                 dbus_message_iter_get_basic(&entry, &key);
174                 dbus_message_iter_next(&entry);
175                 dbus_message_iter_get_basic(&entry, &value);
176
177                 DBG("%s = %s", key, value);
178
179                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
180                         connman_provider_set_string(provider, "Address", value);
181                         addressv4 = g_strdup(value);
182                 }
183
184                 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
185                         connman_provider_set_string(provider, "Netmask", value);
186                         netmask = g_strdup(value);
187                 }
188
189                 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
190                         connman_provider_set_string(provider, "DNS", value);
191                         nameservers = g_strdup(value);
192                 }
193
194                 if (!strcmp(key, "INTERNAL_IFNAME"))
195                         ifname = g_strdup(value);
196
197                 dbus_message_iter_next(&dict);
198         }
199
200         if (vpn_set_ifname(provider, ifname) < 0) {
201                 g_free(ifname);
202                 g_free(addressv4);
203                 g_free(netmask);
204                 g_free(nameservers);
205                 return VPN_STATE_FAILURE;
206         }
207
208         if (addressv4 != NULL)
209                 ipaddress = connman_ipaddress_alloc(AF_INET);
210
211         g_free(ifname);
212
213         if (ipaddress == NULL) {
214                 connman_error("No IP address for provider");
215                 g_free(addressv4);
216                 g_free(netmask);
217                 g_free(nameservers);
218                 return VPN_STATE_FAILURE;
219         }
220
221         value = connman_provider_get_string(provider, "Host");
222         if (value != NULL) {
223                 connman_provider_set_string(provider, "Gateway", value);
224                 gateway = g_strdup(value);
225         }
226
227         if (addressv4 != NULL)
228                 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
229                                         gateway);
230
231         connman_provider_set_ipaddress(provider, ipaddress);
232         connman_provider_set_nameservers(provider, nameservers);
233
234         g_free(addressv4);
235         g_free(netmask);
236         g_free(gateway);
237         g_free(nameservers);
238         connman_ipaddress_free(ipaddress);
239
240         return VPN_STATE_CONNECT;
241 }
242
243 static int l2tp_save(struct connman_provider *provider, GKeyFile *keyfile)
244 {
245         const char *option;
246         int i;
247
248         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
249                 if (strncmp(pppd_options[i].cm_opt, "L2TP.", 5) == 0) {
250                         option = connman_provider_get_string(provider,
251                                                         pppd_options[i].cm_opt);
252                         if (option == NULL)
253                                 continue;
254
255                         g_key_file_set_string(keyfile,
256                                         connman_provider_get_save_group(provider),
257                                         pppd_options[i].cm_opt, option);
258                 }
259         }
260         return 0;
261 }
262
263 static ssize_t full_write(int fd, const void *buf, size_t len)
264 {
265         ssize_t byte_write;
266
267         while (len) {
268                 byte_write = write(fd, buf, len);
269                 if (byte_write < 0) {
270                         connman_error("failed to write config to l2tp: %s\n",
271                                         strerror(errno));
272                         return byte_write;
273                 }
274                 len -= byte_write;
275                 buf += byte_write;
276         }
277
278         return 0;
279 }
280
281 static ssize_t l2tp_write_bool_option(int fd,
282                                         const char *key, const char *value)
283 {
284         gchar *buf;
285         ssize_t ret = 0;
286
287         if (key != NULL && value != NULL) {
288                 if (strcmp(value, "yes") == 0) {
289                         buf = g_strdup_printf("%s\n", key);
290                         ret = full_write(fd, buf, strlen(buf));
291
292                         g_free(buf);
293                 }
294         }
295
296         return ret;
297 }
298
299 static int l2tp_write_option(int fd, const char *key, const char *value)
300 {
301         gchar *buf;
302         ssize_t ret = 0;
303
304         if (key != NULL) {
305                 if (value != NULL)
306                         buf = g_strdup_printf("%s %s\n", key, value);
307                 else
308                         buf = g_strdup_printf("%s\n", key);
309
310                 ret = full_write(fd, buf, strlen(buf));
311
312                 g_free(buf);
313         }
314
315         return ret;
316 }
317
318 static int l2tp_write_section(int fd, const char *key, const char *value)
319 {
320         gchar *buf;
321         ssize_t ret = 0;
322
323         if (key != NULL && value != NULL) {
324                 buf = g_strdup_printf("%s = %s\n", key, value);
325                 ret = full_write(fd, buf, strlen(buf));
326
327                 g_free(buf);
328         }
329
330         return ret;
331 }
332
333 static int write_pppd_option(struct connman_provider *provider, int fd)
334 {
335         int i;
336         const char *opt_s;
337
338         l2tp_write_option(fd, "nodetach", NULL);
339         l2tp_write_option(fd, "lock", NULL);
340         l2tp_write_option(fd, "usepeerdns", NULL);
341         l2tp_write_option(fd, "noipdefault", NULL);
342         l2tp_write_option(fd, "noauth", NULL);
343         l2tp_write_option(fd, "nodefaultroute", NULL);
344         l2tp_write_option(fd, "ipparam", "l2tp_plugin");
345
346         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
347                 if (pppd_options[i].sub != OPT_ALL &&
348                         pppd_options[i].sub != OPT_PPPD)
349                         continue;
350
351                 opt_s = connman_provider_get_string(provider,
352                                         pppd_options[i].cm_opt);
353                 if (!opt_s)
354                         opt_s = pppd_options[i].vpn_default;
355
356                 if (!opt_s)
357                         continue;
358
359                 if (pppd_options[i].type == OPT_STRING)
360                         l2tp_write_option(fd,
361                                 pppd_options[i].pppd_opt, opt_s);
362                 else if (pppd_options[i].type == OPT_BOOL)
363                         l2tp_write_bool_option(fd,
364                                 pppd_options[i].pppd_opt, opt_s);
365         }
366
367         l2tp_write_option(fd, "plugin",
368                                 SCRIPTDIR "/libppp-plugin.so");
369
370         return 0;
371 }
372
373
374 static int l2tp_write_fields(struct connman_provider *provider,
375                                                 int fd, int sub)
376 {
377         int i;
378         const char *opt_s;
379
380         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
381                 if (pppd_options[i].sub != sub)
382                         continue;
383
384                 opt_s = connman_provider_get_string(provider,
385                                         pppd_options[i].cm_opt);
386                 if (!opt_s)
387                         opt_s = pppd_options[i].vpn_default;
388
389                 if (!opt_s)
390                         continue;
391
392                 if (pppd_options[i].type == OPT_STRING)
393                         l2tp_write_section(fd,
394                                 pppd_options[i].pppd_opt, opt_s);
395                 else if (pppd_options[i].type == OPT_BOOL)
396                         l2tp_write_bool_option(fd,
397                                 pppd_options[i].pppd_opt, opt_s);
398         }
399
400         return 0;
401 }
402
403 static int l2tp_write_config(struct connman_provider *provider,
404                                         const char *pppd_name, int fd)
405 {
406         const char *option;
407
408         l2tp_write_option(fd, "[global]", NULL);
409         l2tp_write_fields(provider, fd, OPT_L2G);
410
411         l2tp_write_option(fd, "[lac l2tp]", NULL);
412
413         option = connman_provider_get_string(provider, "Host");
414         l2tp_write_option(fd, "lns =", option);
415
416         l2tp_write_fields(provider, fd, OPT_ALL);
417         l2tp_write_fields(provider, fd, OPT_L2);
418
419         l2tp_write_option(fd, "pppoptfile =", pppd_name);
420
421         return 0;
422 }
423
424 static void l2tp_died(struct connman_task *task, int exit_code, void *user_data)
425 {
426         char *conf_file;
427
428         vpn_died(task, exit_code, user_data);
429
430         conf_file = g_strdup_printf("/var/run/connman/connman-xl2tpd.conf");
431         unlink(conf_file);
432         g_free(conf_file);
433
434         conf_file = g_strdup_printf("/var/run/connman/connman-ppp-option.conf");
435         unlink(conf_file);
436         g_free(conf_file);
437 }
438
439 static int l2tp_connect(struct connman_provider *provider,
440                 struct connman_task *task, const char *if_name)
441 {
442         const char *host;
443         char *l2tp_name, *pppd_name;
444         int l2tp_fd, pppd_fd;
445         int err;
446
447         if (connman_task_set_notify(task, "getsec",
448                                         l2tp_get_sec, provider))
449                 return -ENOMEM;
450
451         host = connman_provider_get_string(provider, "Host");
452         if (host == NULL) {
453                 connman_error("Host not set; cannot enable VPN");
454                 return -EINVAL;
455         }
456
457         l2tp_name = g_strdup_printf("/var/run/connman/connman-xl2tpd.conf");
458
459         l2tp_fd = open(l2tp_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
460         if (l2tp_fd < 0) {
461                 g_free(l2tp_name);
462                 connman_error("Error writing l2tp config");
463                 return -EIO;
464         }
465
466         pppd_name = g_strdup_printf("/var/run/connman/connman-ppp-option.conf");
467
468         pppd_fd = open(pppd_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
469         if (pppd_fd < 0) {
470                 connman_error("Error writing pppd config");
471                 g_free(l2tp_name);
472                 g_free(pppd_name);
473                 close(l2tp_fd);
474                 return -EIO;
475         }
476
477         l2tp_write_config(provider, pppd_name, l2tp_fd);
478
479         write_pppd_option(provider, pppd_fd);
480
481         connman_task_add_argument(task, "-D", NULL);
482         connman_task_add_argument(task, "-c", l2tp_name);
483
484         g_free(l2tp_name);
485         g_free(pppd_name);
486
487         err = connman_task_run(task, l2tp_died, provider,
488                                 NULL, NULL, NULL);
489         if (err < 0) {
490                 connman_error("l2tp failed to start");
491                 return -EIO;
492         }
493
494         return 0;
495 }
496
497 static int l2tp_error_code(int exit_code)
498 {
499         switch (exit_code) {
500         case 1:
501                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
502         default:
503                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
504         }
505 }
506
507 static struct vpn_driver vpn_driver = {
508         .flags          = VPN_FLAG_NO_TUN,
509         .notify         = l2tp_notify,
510         .connect        = l2tp_connect,
511         .error_code     = l2tp_error_code,
512         .save           = l2tp_save,
513 };
514
515 static int l2tp_init(void)
516 {
517         connection = connman_dbus_get_connection();
518
519         return vpn_register("l2tp", &vpn_driver, L2TP);
520 }
521
522 static void l2tp_exit(void)
523 {
524         vpn_unregister("l2tp");
525
526         dbus_connection_unref(connection);
527 }
528
529 CONNMAN_PLUGIN_DEFINE(l2tp, "l2tp plugin", VERSION,
530         CONNMAN_PLUGIN_PRIORITY_DEFAULT, l2tp_init, l2tp_exit)