fce8902bf80336857e8cee328ce96a7a484fcf42
[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) 2011  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, "connect"))
162                 return VPN_STATE_DISCONNECT;
163
164         dbus_message_iter_recurse(&iter, &dict);
165
166         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
167                 DBusMessageIter entry;
168
169                 dbus_message_iter_recurse(&dict, &entry);
170                 dbus_message_iter_get_basic(&entry, &key);
171                 dbus_message_iter_next(&entry);
172                 dbus_message_iter_get_basic(&entry, &value);
173
174                 DBG("%s = %s", key, value);
175
176                 if (!strcmp(key, "INTERNAL_IP4_ADDRESS")) {
177                         connman_provider_set_string(provider, "Address", value);
178                         addressv4 = g_strdup(value);
179                 }
180
181                 if (!strcmp(key, "INTERNAL_IP4_NETMASK")) {
182                         connman_provider_set_string(provider, "Netmask", value);
183                         netmask = g_strdup(value);
184                 }
185
186                 if (!strcmp(key, "INTERNAL_IP4_DNS")) {
187                         connman_provider_set_string(provider, "DNS", value);
188                         nameservers = g_strdup(value);
189                 }
190
191                 if (!strcmp(key, "INTERNAL_IFNAME"))
192                         ifname = g_strdup(value);
193
194                 dbus_message_iter_next(&dict);
195         }
196
197         if (vpn_set_ifname(provider, ifname) < 0) {
198                 g_free(ifname);
199                 g_free(addressv4);
200                 g_free(netmask);
201                 g_free(nameservers);
202                 return VPN_STATE_FAILURE;
203         }
204
205         if (addressv4 != NULL)
206                 ipaddress = connman_ipaddress_alloc(AF_INET);
207
208         g_free(ifname);
209
210         if (ipaddress == NULL) {
211                 connman_error("No IP address for provider");
212                 g_free(addressv4);
213                 g_free(netmask);
214                 g_free(nameservers);
215                 return VPN_STATE_FAILURE;
216         }
217
218         value = connman_provider_get_string(provider, "Host");
219         if (value != NULL) {
220                 connman_provider_set_string(provider, "Gateway", value);
221                 gateway = g_strdup(value);
222         }
223
224         if (addressv4 != NULL)
225                 connman_ipaddress_set_ipv4(ipaddress, addressv4, netmask,
226                                         gateway);
227
228         connman_provider_set_ipaddress(provider, ipaddress);
229         connman_provider_set_nameservers(provider, nameservers);
230
231         g_free(addressv4);
232         g_free(netmask);
233         g_free(gateway);
234         g_free(nameservers);
235         connman_ipaddress_free(ipaddress);
236
237         return VPN_STATE_CONNECT;
238 }
239
240 static ssize_t full_write(int fd, const void *buf, size_t len)
241 {
242         ssize_t byte_write;
243
244         while (len) {
245                 byte_write = write(fd, buf, len);
246                 if (byte_write < 0) {
247                         connman_error("failed to write config to l2tp: %s\n",
248                                         strerror(errno));
249                         return byte_write;
250                 }
251                 len -= byte_write;
252                 buf += byte_write;
253         }
254
255         return 0;
256 }
257
258 static ssize_t l2tp_write_bool_option(int fd,
259                                         const char *key, const char *value)
260 {
261         gchar *buf;
262         ssize_t ret = 0;
263
264         if (key != NULL && value != NULL) {
265                 if (strcmp(value, "yes") == 0) {
266                         buf = g_strdup_printf("%s\n", key);
267                         ret = full_write(fd, buf, strlen(buf));
268
269                         g_free(buf);
270                 }
271         }
272
273         return ret;
274 }
275
276 static int l2tp_write_option(int fd, const char *key, const char *value)
277 {
278         gchar *buf;
279         ssize_t ret = 0;
280
281         if (key != NULL) {
282                 if (value != NULL)
283                         buf = g_strdup_printf("%s %s\n", key, value);
284                 else
285                         buf = g_strdup_printf("%s\n", key);
286
287                 ret = full_write(fd, buf, strlen(buf));
288
289                 g_free(buf);
290         }
291
292         return ret;
293 }
294
295 static int l2tp_write_section(int fd, const char *key, const char *value)
296 {
297         gchar *buf;
298         ssize_t ret = 0;
299
300         if (key != NULL && value != NULL) {
301                 buf = g_strdup_printf("%s = %s\n", key, value);
302                 ret = full_write(fd, buf, strlen(buf));
303
304                 g_free(buf);
305         }
306
307         return ret;
308 }
309
310 static int write_pppd_option(struct connman_provider *provider, int fd)
311 {
312         int i;
313         const char *opt_s;
314
315         l2tp_write_option(fd, "nodetach", NULL);
316         l2tp_write_option(fd, "lock", NULL);
317         l2tp_write_option(fd, "usepeerdns", NULL);
318         l2tp_write_option(fd, "noipdefault", NULL);
319         l2tp_write_option(fd, "noauth", NULL);
320         l2tp_write_option(fd, "nodefaultroute", NULL);
321         l2tp_write_option(fd, "ipparam", "l2tp_plugin");
322
323         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
324                 if (pppd_options[i].sub != OPT_ALL &&
325                         pppd_options[i].sub != OPT_PPPD)
326                         continue;
327
328                 opt_s = connman_provider_get_string(provider,
329                                         pppd_options[i].cm_opt);
330                 if (!opt_s)
331                         opt_s = pppd_options[i].vpn_default;
332
333                 if (!opt_s)
334                         continue;
335
336                 if (pppd_options[i].type == OPT_STRING)
337                         l2tp_write_option(fd,
338                                 pppd_options[i].pppd_opt, opt_s);
339                 else if (pppd_options[i].type == OPT_BOOL)
340                         l2tp_write_bool_option(fd,
341                                 pppd_options[i].pppd_opt, opt_s);
342         }
343
344         l2tp_write_option(fd, "plugin",
345                                 SCRIPTDIR "/libppp-plugin.so");
346
347         return 0;
348 }
349
350
351 static int l2tp_write_fields(struct connman_provider *provider,
352                                                 int fd, int sub)
353 {
354         int i;
355         const char *opt_s;
356
357         for (i = 0; i < (int)ARRAY_SIZE(pppd_options); i++) {
358                 if (pppd_options[i].sub != sub)
359                         continue;
360
361                 opt_s = connman_provider_get_string(provider,
362                                         pppd_options[i].cm_opt);
363                 if (!opt_s)
364                         opt_s = pppd_options[i].vpn_default;
365
366                 if (!opt_s)
367                         continue;
368
369                 if (pppd_options[i].type == OPT_STRING)
370                         l2tp_write_section(fd,
371                                 pppd_options[i].pppd_opt, opt_s);
372                 else if (pppd_options[i].type == OPT_BOOL)
373                         l2tp_write_bool_option(fd,
374                                 pppd_options[i].pppd_opt, opt_s);
375         }
376
377         return 0;
378 }
379
380 static int l2tp_write_config(struct connman_provider *provider,
381                                         const char *pppd_name, int fd)
382 {
383         const char *option;
384
385         l2tp_write_option(fd, "[global]", NULL);
386         l2tp_write_fields(provider, fd, OPT_L2G);
387
388         l2tp_write_option(fd, "[lac l2tp]", NULL);
389
390         option = connman_provider_get_string(provider, "Host");
391         l2tp_write_option(fd, "lns =", option);
392
393         l2tp_write_fields(provider, fd, OPT_ALL);
394         l2tp_write_fields(provider, fd, OPT_L2);
395
396         l2tp_write_option(fd, "pppoptfile =", pppd_name);
397
398         return 0;
399 }
400
401 static void l2tp_died(struct connman_task *task, int exit_code, void *user_data)
402 {
403         char *conf_file;
404
405         vpn_died(task, exit_code, user_data);
406
407         conf_file = g_strdup_printf("/var/run/connman/connman-xl2tpd.conf");
408         unlink(conf_file);
409         g_free(conf_file);
410
411         conf_file = g_strdup_printf("/var/run/connman/connman-ppp-option.conf");
412         unlink(conf_file);
413         g_free(conf_file);
414 }
415
416 static int l2tp_connect(struct connman_provider *provider,
417                 struct connman_task *task, const char *if_name)
418 {
419         const char *host;
420         char *l2tp_name, *pppd_name;
421         int l2tp_fd, pppd_fd;
422         int err;
423
424         if (connman_task_set_notify(task, "getsec",
425                                         l2tp_get_sec, provider))
426                 return -ENOMEM;
427
428         host = connman_provider_get_string(provider, "Host");
429         if (host == NULL) {
430                 connman_error("Host not set; cannot enable VPN");
431                 return -EINVAL;
432         }
433
434         l2tp_name = g_strdup_printf("/var/run/connman/connman-xl2tpd.conf");
435
436         l2tp_fd = open(l2tp_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
437         if (l2tp_fd < 0) {
438                 g_free(l2tp_name);
439                 connman_error("Error writing l2tp config");
440                 return -EIO;
441         }
442
443         pppd_name = g_strdup_printf("/var/run/connman/connman-ppp-option.conf");
444
445         pppd_fd = open(pppd_name, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
446         if (pppd_fd < 0) {
447                 connman_error("Error writing pppd config");
448                 g_free(l2tp_name);
449                 g_free(pppd_name);
450                 close(l2tp_fd);
451                 return -EIO;
452         }
453
454         l2tp_write_config(provider, pppd_name, l2tp_fd);
455
456         write_pppd_option(provider, pppd_fd);
457
458         connman_task_add_argument(task, "-D", NULL);
459         connman_task_add_argument(task, "-c", l2tp_name);
460
461         g_free(l2tp_name);
462         g_free(pppd_name);
463
464         err = connman_task_run(task, l2tp_died, provider,
465                                 NULL, NULL, NULL);
466         if (err < 0) {
467                 connman_error("l2tp failed to start");
468                 return -EIO;
469         }
470
471         return 0;
472 }
473
474 static int l2tp_error_code(int exit_code)
475 {
476
477         switch (exit_code) {
478         case 1:
479                 return CONNMAN_PROVIDER_ERROR_CONNECT_FAILED;
480         case 2:
481                 return CONNMAN_PROVIDER_ERROR_LOGIN_FAILED;
482         default:
483                 return CONNMAN_PROVIDER_ERROR_UNKNOWN;
484         }
485 }
486
487 static struct vpn_driver vpn_driver = {
488         .flags          = VPN_FLAG_NO_TUN,
489         .notify         = l2tp_notify,
490         .connect        = l2tp_connect,
491         .error_code     = l2tp_error_code,
492 };
493
494 static int l2tp_init(void)
495 {
496         connection = connman_dbus_get_connection();
497
498         return vpn_register("l2tp", &vpn_driver, L2TP);
499 }
500
501 static void l2tp_exit(void)
502 {
503         vpn_unregister("l2tp");
504
505         dbus_connection_unref(connection);
506 }
507
508 CONNMAN_PLUGIN_DEFINE(l2tp, "l2tp plugin", VERSION,
509         CONNMAN_PLUGIN_PRIORITY_DEFAULT, l2tp_init, l2tp_exit)